10 #include <linux/hdreg.h>
14 #include <sys/inotify.h>
15 #include <sys/mount.h>
17 #include <sys/types.h>
24 #include "include/log.h"
25 #include "include/list.h"
26 #include "include/sys.h"
27 #include "include/signal.h"
28 #include "include/timer.h"
29 #include "include/autofs.h"
30 #include "include/ucix.h"
31 #include "include/fs.h"
32 #include "include/mount.h"
34 int mount_new(char *path, char *dev);
36 static struct list_head mounts;
39 * enum status - status of mount entry
41 * @STATUS_UNMOUNTED: currently not mounted
42 * @STATUS_MOUNTED: mounted & ready for usage
43 * @STATUS_EXPIRED: mount expired & *temporary* unmounted
44 * @STATUS_IGNORE: entry should be ignored and never mounted
54 struct list_head list;
67 static char *fs_names[] = {
83 #define MAX_MOUNTED 32
84 #define MAX_MOUNT_NAME 32
86 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
87 static int mounted_count = 0;
88 extern char uci_path[32];
90 static void mount_dump_uci_state(void)
92 struct uci_context *ctx;
94 char mountd[] = {"mountd"};
95 char type[] = {"mountd_disc"};
97 unsigned long long int size = 0;
98 unlink("/var/state/mountd");
99 ctx = ucix_init("mountd");
100 uci_set_savedir(ctx, "/var/state/");
101 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
102 list_for_each(p, &mounts)
104 struct mount *q = container_of(p, struct mount, list);
106 if(q->fs == EXTENDED)
108 ucix_add_section(ctx, mountd, q->serial, type);
111 ucix_add_option(ctx, mountd, q->serial, "disc", t);
112 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
113 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
114 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
115 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
116 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
117 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
118 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
119 ucix_add_option(ctx, mountd, q->serial, t, q->size);
120 if(q->fs > MBR && q->fs <= LASTFS)
122 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
123 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
125 if (q->status == STATUS_MOUNTED)
127 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
128 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
130 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
131 ucix_add_option_int(ctx, mountd, mountd, "total", size);
132 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
133 ucix_save_state(ctx, "mountd");
137 static struct mount* mount_find(char *name, char *dev)
140 list_for_each(p, &mounts)
142 struct mount *q = container_of(p, struct mount, list);
144 if(!strcmp(q->name, name))
147 if(!strcmp(q->dev, dev))
153 static void mount_add_list(char *name, char *dev, char *serial,
154 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
157 char dev_path[64], dev_link[64], tmp[64];
159 mount = malloc(sizeof(struct mount));
160 INIT_LIST_HEAD(&mount->list);
161 strncpy(mount->vendor, vendor, 64);
162 strncpy(mount->model, model, 64);
163 strncpy(mount->rev, rev, 64);
164 strncpy(mount->name, name, 64);
165 strncpy(mount->dev, dev, 64);
166 strncpy(mount->serial, serial, 64);
167 strncpy(mount->size, size, 64);
168 strncpy(mount->sector_size, sector_size, 64);
169 mount->status = STATUS_UNMOUNTED;
171 list_add(&mount->list, &mounts);
174 mount->status = STATUS_IGNORE;
178 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
180 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, name);
181 snprintf(dev_path, sizeof(dev_path), "%s%s", "/tmp/run/mountd/", dev);
182 /* If link aleady exists - replace it */
183 if (lstat(dev_link, &st) == 0 && S_ISLNK(st.st_mode)) {
184 snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
185 symlink(dev_path, tmp);
186 rename(tmp, dev_link);
188 symlink(dev_path, dev_link);
190 if (!mount_new("/tmp/run/mountd/", dev))
191 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
195 static int mount_check_disc(char *disc)
197 FILE *fp = fopen("/proc/mounts", "r");
202 log_printf("error reading /proc/mounts");
205 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
209 t = strstr(tmp, " ");
214 l = snprintf(tmp2, 31, "/dev/%s", disc);
216 if(!strncmp(tmp, tmp2, l))
224 static int mount_wait_for_disc(char *disc)
229 int ret = mount_check_disc(disc);
237 int mount_new(char *path, char *dev)
243 mount = mount_find(0, dev);
246 log_printf("request for invalid path %s%s\n", path, dev);
249 if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
251 snprintf(tmp, 256, "%s%s", path, mount->dev);
252 log_printf("mounting %s\n", tmp);
255 pid = autofs_safe_fork();
258 char *options, *fstype;
259 if(mount->fs == EXFAT)
261 options = "rw,uid=1000,gid=1000";
266 options = "rw,uid=1000,gid=1000";
269 if(mount->fs == EXT4)
271 options = "rw,defaults";
274 if(mount->fs == EXT3)
276 options = "rw,defaults";
279 if(mount->fs == EXT2)
281 options = "rw,defaults";
284 if(mount->fs == HFSPLUS)
286 options = "rw,defaults,uid=1000,gid=1000";
289 if(mount->fs == HFSPLUSJOURNAL)
291 options = "ro,defaults,uid=1000,gid=1000";
294 if(mount->fs == NTFS)
299 if(mount->fs > MBR && mount->fs <= LASTFS)
301 struct uci_context *ctx;
302 char *uci_options, *uci_fstype;
303 ctx = ucix_init("mountd");
304 if(fs_names[mount->fs])
306 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
307 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
309 options = uci_options;
312 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
313 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
317 exit(WEXITSTATUS(ret));
319 pid = waitpid(pid, &ret, 0);
320 ret = WEXITSTATUS(ret);
321 log_printf("----------> mount ret = %d\n", ret);
322 if (ret && ret != 0xff) {
326 if(mount_wait_for_disc(mount->dev) == 0)
328 mount->status = STATUS_MOUNTED;
329 mount_dump_uci_state();
334 int mount_remove(char *path, char *dev)
339 snprintf(tmp, 256, "%s%s", path, dev);
340 log_printf("device %s has expired... unmounting %s\n", dev, tmp);
341 ret = system_printf("/bin/umount %s", tmp);
345 mount = mount_find(0, dev);
347 mount->status = STATUS_EXPIRED;
348 log_printf("finished unmounting\n");
349 mount_dump_uci_state();
353 static int dir_sort(const struct dirent **a, const struct dirent **b)
358 static int dir_filter(const struct dirent *a)
360 if(strstr(a->d_name, ":"))
365 static char* mount_get_serial(char *dev)
368 static char tmp2[64];
370 static struct hd_driveid hd;
373 static char disc_id[13];
374 snprintf(tmp, 64, "/dev/%s", dev);
375 disc = open(tmp, O_RDONLY);
378 log_printf("Trying to open unknown disc\n");
381 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
384 serial = (char*)hd.serial_no;
385 /* if we failed, it probably a usb storage device */
386 /* there must be a better way for this */
389 struct dirent **namelist;
390 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
395 char *t = strstr(namelist[n]->d_name, ":");
403 id = atoi(namelist[n]->d_name);
405 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
406 ret = stat(tmp3, &buf);
409 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
410 ret = stat(tmp3, &buf);
415 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
416 fp = fopen(tmp2, "r");
419 while(fgets(tmp2, 64, fp) != NULL)
421 serial = strstr(tmp2, "Serial Number:");
424 serial += strlen("Serial Number: ");
425 serial[strlen(serial) - 1] = '\0';
441 log_printf("could not find a serial number for the device %s\n", dev);
443 /* serial string id is cheap, but makes the discs anonymous */
444 unsigned char uniq[6];
445 unsigned int *u = (unsigned int*) uniq;
446 int l = strlen(serial);
448 memset(disc_id, 0, 13);
450 for(i = 0; i < l; i++)
452 uniq[i%6] += serial[i];
454 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
455 //log_printf("Serial number - %s %s\n", serial, disc_id);
458 sprintf(disc_id, "000000000000");
462 static void mount_dev_add(char *dev)
464 struct mount *mount = mount_find(0, dev);
474 struct uci_context *ctx;
479 char sector_size[64];
485 if (!strncmp(name, "mmcblk", 6))
488 s = mount_get_serial(name);
492 if (!strncmp(name, "mmcblk", 6)) {
493 snprintf(tmp, 64, "part%s", &dev[8]);
494 snprintf(node, 64, "SD-P%s", &dev[8]);
497 snprintf(tmp, 64, "part%s", &dev[3]);
498 snprintf(node, 64, "USB-%s", &dev[2]);
500 if(node[4] >= 'a' && node[4] <= 'z')
505 ctx = ucix_init("mountd");
506 p = ucix_get_option(ctx, "mountd", s, tmp);
515 snprintf(node, 64, "%s", p);
520 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
521 fp = fopen(tmp, "r");
524 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
525 fp = fopen(tmp, "r");
528 snprintf(model, 64, "unknown");
530 fgets(model, 64, fp);
531 model[strlen(model) - 1] = '\0';;
534 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
535 fp = fopen(tmp, "r");
538 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
539 fp = fopen(tmp, "r");
542 snprintf(vendor, 64, "unknown");
544 fgets(vendor, 64, fp);
545 vendor[strlen(vendor) - 1] = '\0';
548 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
549 fp = fopen(tmp, "r");
552 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
553 fp = fopen(tmp, "r");
556 snprintf(rev, 64, "unknown");
559 rev[strlen(rev) - 1] = '\0';
562 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
563 fp = fopen(tmp, "r");
566 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
567 fp = fopen(tmp, "r");
570 snprintf(size, 64, "unknown");
573 size[strlen(size) - 1] = '\0';
578 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
579 fp = fopen(tmp, "r");
581 snprintf(sector_size, 64, "unknown");
583 fgets(sector_size, 64, fp);
584 sector_size[strlen(sector_size) - 1] = '\0';
587 snprintf(tmp, 64, "/dev/%s", dev);
589 if (fs <= MBR || fs > LASTFS) {
592 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
593 mount_dump_uci_state();
597 static int mount_dev_del(struct mount *mount)
602 if (mount->status == STATUS_MOUNTED) {
603 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
604 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
607 umount2(tmp, MNT_DETACH);
610 mount_dump_uci_state();
616 void mount_dump_list(void)
619 list_for_each(p, &mounts)
621 struct mount *q = container_of(p, struct mount, list);
622 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
626 char* is_mounted(char *block, char *path)
629 for(i = 0; i < mounted_count; i++)
632 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
633 return &mounted[i][0][0];
635 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
636 return &mounted[i][0][0];
641 static void mount_update_mount_list(void)
643 FILE *fp = fopen("/proc/mounts", "r");
648 log_printf("error reading /proc/mounts");
652 while(fgets(tmp, 256, fp) != NULL)
656 if (mounted_count + 1 > MAX_MOUNTED) {
657 log_printf("found more than %d mounts \n", MAX_MOUNTED);
661 t = strstr(tmp, " ");
667 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
674 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
681 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
682 /* printf("%s %s %s\n",
683 mounted[mounted_count][0],
684 mounted[mounted_count][1],
685 mounted[mounted_count][2]);*/
692 /* FIXME: we need more intelligence here */
693 static int dir_filter2(const struct dirent *a)
695 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
700 static char block[MAX_BLOCK][MAX_BLOCK];
701 static int blk_cnt = 0;
703 static int check_block(char *b)
706 for(i = 0; i < blk_cnt; i++)
708 if(!strcmp(block[i], b))
714 static void mount_enum_drives(void)
716 struct dirent **namelist, **namelist2;
717 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
724 if(blk_cnt < MAX_BLOCK)
728 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
729 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
734 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
740 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
751 struct mount *q = container_of(p, struct mount, list);
753 struct uci_context *ctx;
756 snprintf(tmp, 64, "part%s", &q->dev[3]);
757 ctx = ucix_init("mountd");
758 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
760 if (t && q->status != STATUS_MOUNTED)
764 if (q->status != STATUS_IGNORE)
766 } else if(!strcmp(t, "1"))
768 if(strncmp(q->name, "Disc-", 5))
770 } else if(strcmp(q->name, t))
775 if(!check_block(q->dev)||del)
777 if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
781 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
783 err = mount_dev_del(q);
785 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, q->name);
787 /* Create "tmp" symlink to non-existing path */
788 snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
789 symlink("## DEVICE MISSING ##", tmp);
791 /* Replace old symlink with the not working one */
792 rename(tmp, dev_link);
794 log_printf("unlinking %s\n", dev_link);
799 p->prev->next = p->next;
800 p->next->prev = p->prev;
804 mount_dump_uci_state();
805 system_printf("/etc/fonstated/ReloadSamba");
809 for(i = 0; i < blk_cnt; i++)
810 mount_dev_add(block[i]);
813 static void mount_check_enum(void)
815 waitpid(-1, 0, WNOHANG);
819 void mount_init(void)
821 INIT_LIST_HEAD(&mounts);
822 timer_add(mount_update_mount_list, 2);
823 timer_add(mount_check_enum, 1);
824 mount_update_mount_list();