9 #include <linux/hdreg.h>
13 #include <sys/inotify.h>
15 #include <sys/types.h>
21 #include "include/log.h"
22 #include "include/list.h"
23 #include "include/sys.h"
24 #include "include/signal.h"
25 #include "include/timer.h"
26 #include "include/autofs.h"
27 #include "include/ucix.h"
28 #include "include/fs.h"
29 #include "include/mount.h"
31 int mount_new(char *path, char *dev);
33 struct list_head mounts;
36 struct list_head list;
64 #define MAX_MOUNTED 32
65 #define MAX_MOUNT_NAME 32
67 char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
68 int mounted_count = 0;
69 extern char uci_path[32];
71 static void mount_dump_uci_state(void)
73 struct uci_context *ctx;
75 char mountd[] = {"mountd"};
76 char type[] = {"mountd_disc"};
78 unsigned long long int size = 0;
79 unlink("/var/state/mountd");
80 ctx = ucix_init("mountd");
81 uci_set_savedir(ctx, "/var/state/");
82 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
83 list_for_each(p, &mounts)
85 struct mount *q = container_of(p, struct mount, list);
89 ucix_add_section(ctx, mountd, q->serial, type);
92 ucix_add_option(ctx, mountd, q->serial, "disc", t);
93 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
94 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
95 ucix_add_option(ctx, mountd, q->serial, t, (q->mounted)?("1"):("0"));
96 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
97 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
98 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
99 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
100 ucix_add_option(ctx, mountd, q->serial, t, q->size);
101 if(q->fs > MBR && q->fs <= EXT4)
103 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
104 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
108 if((!q->ignore) && q->size && q->sector_size)
109 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
111 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
112 ucix_add_option_int(ctx, mountd, mountd, "total", size);
113 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
114 ucix_save_state(ctx, "mountd");
118 static struct mount* mount_find(char *name, char *dev)
121 list_for_each(p, &mounts)
123 struct mount *q = container_of(p, struct mount, list);
125 if(!strcmp(q->name, name))
128 if(!strcmp(q->dev, dev))
134 static void mount_add_list(char *name, char *dev, char *serial,
135 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
138 char tmp[64], tmp2[64];
139 if(fs <= MBR || fs > EXT4)
141 mount = malloc(sizeof(struct mount));
142 INIT_LIST_HEAD(&mount->list);
143 strncpy(mount->vendor, vendor, 64);
144 strncpy(mount->model, model, 64);
145 strncpy(mount->rev, rev, 64);
146 strncpy(mount->name, name, 64);
147 strncpy(mount->dev, dev, 64);
148 strncpy(mount->serial, serial, 64);
149 strncpy(mount->size, size, 64);
150 strncpy(mount->sector_size, sector_size, 64);
151 mount->ignore = ignore;
154 list_add(&mount->list, &mounts);
155 if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= EXT4))
157 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
158 snprintf(tmp, 64, "%s%s", uci_path, name);
159 snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
161 mount_new("/tmp/run/mountd/", dev);
165 static int mount_check_disc(char *disc)
167 FILE *fp = fopen("/proc/mounts", "r");
172 log_printf("error reading /proc/mounts");
176 while((fgets(tmp, 256, fp) > 0) && (avail == -1))
180 t = strstr(tmp, " ");
185 l = snprintf(tmp2, 31, "/dev/%s", disc);
187 if(!strncmp(tmp, tmp2, l))
195 static int mount_wait_for_disc(char *disc)
200 int ret = mount_check_disc(disc);
208 int mount_new(char *path, char *dev)
214 mount = mount_find(0, dev);
217 log_printf("request for invalid path %s%s\n", path, dev);
220 if(mount->ignore || mount->mounted || mount->fs == EXTENDED)
222 snprintf(tmp, 256, "%s%s", path, mount->dev);
223 log_printf("mounting %s\n", tmp);
226 pid = autofs_safe_fork();
231 log_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
232 ret = system_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
234 if(mount->fs == EXT4)
236 log_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
237 ret = system_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
239 if(mount->fs == EXT3)
241 log_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
242 ret = system_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
244 if(mount->fs == EXT2)
246 log_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
247 ret = system_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
249 if(mount->fs == HFSPLUS)
251 log_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
252 ret = system_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
254 if(mount->fs == NTFS)
256 log_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
257 ret = system_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
259 exit(WEXITSTATUS(ret));
261 pid = waitpid(pid, &ret, 0);
262 ret = WEXITSTATUS(ret);
263 log_printf("----------> mount ret = %d\n", ret);
264 if(ret && (ret != 0xff))
266 if(mount_wait_for_disc(mount->dev) == 0)
269 mount_dump_uci_state();
274 int mount_remove(char *path, char *dev)
279 snprintf(tmp, 256, "%s%s", path, dev);
280 log_printf("%s has expired... unmounting\n", tmp);
281 ret = system_printf("/bin/umount %s", tmp);
285 mount = mount_find(0, dev);
288 log_printf("finished unmounting\n");
289 mount_dump_uci_state();
293 static int dir_sort(const struct dirent **a, const struct dirent **b)
298 static int dir_filter(const struct dirent *a)
300 if(strstr(a->d_name, ":"))
305 static char* mount_get_serial(char *dev)
308 static char tmp2[64];
310 static struct hd_driveid hd;
313 snprintf(tmp, 64, "/dev/%s", dev);
314 disc = open(tmp, O_RDONLY);
317 log_printf("Trying to open unknown disc\n");
320 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
323 serial = (char*)hd.serial_no;
324 /* if we failed, it probably a usb storage device */
325 /* there must be a better way for this */
328 struct dirent **namelist;
329 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
334 char *t = strstr(namelist[n]->d_name, ":");
342 id = atoi(namelist[n]->d_name);
344 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
345 ret = stat(tmp3, &buf);
348 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
349 ret = stat(tmp3, &buf);
354 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
355 fp = fopen(tmp2, "r");
358 while(fgets(tmp2, 64, fp) > 0)
360 serial = strstr(tmp2, "Serial Number:");
363 serial += strlen("Serial Number: ");
364 serial[strlen(serial) - 1] = '\0';
380 log_printf("could not find a serial number for the device %s\n", dev);
382 /* serial string id is cheap, but makes the discs anonymous */
383 unsigned char uniq[6];
384 unsigned int *u = (unsigned int*) uniq;
385 int l = strlen(serial);
387 static char disc_id[13];
388 memset(disc_id, 0, 13);
390 for(i = 0; i < l; i++)
392 uniq[i%6] += serial[i];
394 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
395 //log_printf("Serial number - %s %s\n", serial, disc_id);
401 static void mount_dev_add(char *dev)
403 struct mount *mount = mount_find(0, dev);
413 struct uci_context *ctx;
418 char sector_size[64];
422 s = mount_get_serial(name);
425 snprintf(tmp, 64, "part%s", &dev[3]);
426 snprintf(node, 64, "Disc-%s", &dev[2]);
427 if(node[5] >= 'a' && node[5] <= 'z')
432 ctx = ucix_init("mountd");
433 p = ucix_get_option(ctx, "mountd", s, tmp);
442 snprintf(node, 64, "%s", p);
447 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
448 fp = fopen(tmp, "r");
451 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
452 fp = fopen(tmp, "r");
455 snprintf(model, 64, "unknown");
457 fgets(model, 64, fp);
458 model[strlen(model) - 1] = '\0';;
461 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
462 fp = fopen(tmp, "r");
465 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
466 fp = fopen(tmp, "r");
469 snprintf(vendor, 64, "unknown");
471 fgets(vendor, 64, fp);
472 vendor[strlen(vendor) - 1] = '\0';
475 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
476 fp = fopen(tmp, "r");
479 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
480 fp = fopen(tmp, "r");
483 snprintf(rev, 64, "unknown");
486 rev[strlen(rev) - 1] = '\0';
489 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
490 fp = fopen(tmp, "r");
493 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
494 fp = fopen(tmp, "r");
497 snprintf(size, 64, "unknown");
500 size[strlen(size) - 1] = '\0';
505 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
506 fp = fopen(tmp, "r");
508 snprintf(sector_size, 64, "unknown");
510 fgets(sector_size, 64, fp);
511 sector_size[strlen(sector_size) - 1] = '\0';
514 snprintf(tmp, 64, "/dev/%s", dev);
515 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, detect_fs(tmp));
516 mount_dump_uci_state();
520 static void mount_dev_del(char *dev)
522 struct mount *mount = mount_find(0, dev);
528 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
529 log_printf("%s has dissappeared ... unmounting\n", tmp);
530 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
531 system_printf("/bin/umount %s", tmp);
533 snprintf(tmp, 64, "%s%s", uci_path, mount->name);
535 mount_dump_uci_state();
540 void mount_dump_list(void)
543 list_for_each(p, &mounts)
545 struct mount *q = container_of(p, struct mount, list);
546 log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
550 char* is_mounted(char *block, char *path)
553 for(i = 0; i < mounted_count; i++)
556 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
557 return &mounted[i][0][0];
559 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
560 return &mounted[i][0][0];
565 static void mount_check_mount_list(void)
567 FILE *fp = fopen("/proc/mounts", "r");
572 log_printf("error reading /proc/mounts");
577 while(fgets(tmp, 256, fp) > 0)
580 t = strstr(tmp, " ");
586 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
593 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
600 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
601 /* printf("%s %s %s\n",
602 mounted[mounted_count][0],
603 mounted[mounted_count][1],
604 mounted[mounted_count][2]);*/
605 if(mounted_count < MAX_MOUNTED - 1)
608 log_printf("found more than %d mounts \n", MAX_MOUNTED);
613 /* FIXME: we need more intelligence here */
614 static int dir_filter2(const struct dirent *a)
616 if(/*strcmp(a->d_name, "sda") &&*/(!strncmp(a->d_name, "sd", 2)))
621 char block[MAX_BLOCK][MAX_BLOCK];
624 static int check_block(char *b)
627 for(i = 0; i < blk_cnt; i++)
629 if(!strcmp(block[i], b))
635 static void mount_enum_drives(void)
637 struct dirent **namelist, **namelist2;
638 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
645 if(blk_cnt < MAX_BLOCK)
649 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
650 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
653 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
666 struct mount *q = container_of(p, struct mount, list);
668 struct uci_context *ctx;
671 snprintf(tmp, 64, "part%s", &q->dev[3]);
672 ctx = ucix_init("mountd");
673 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
681 } else if(!strcmp(t, "1"))
683 if(strncmp(q->name, "Disc-", 5))
685 } else if(strcmp(q->name, t))
690 if(!check_block(q->dev)||del)
692 mount_dev_del(q->dev);
693 p->prev->next = p->next;
694 p->next->prev = p->prev;
696 log_printf("removing %s\n", q->dev);
697 snprintf(tmp, 64, "%s%s", uci_path, q->name);
699 system_printf("/etc/mountd/event remove %s %s", q->dev, q->name);
701 mount_dump_uci_state();
702 system_printf("/etc/fonstated/ReloadSamba");
706 for(i = 0; i < blk_cnt; i++)
707 mount_dev_add(block[i]);
710 static void mount_check_enum(void)
712 waitpid(-1, 0, WNOHANG);
716 void mount_init(void)
718 INIT_LIST_HEAD(&mounts);
719 timer_add(mount_check_mount_list, 2);
720 timer_add(mount_check_enum, 1);
721 mount_check_mount_list();