10 #include <linux/hdreg.h>
14 #include <sys/inotify.h>
15 #include <sys/mount.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 static struct list_head mounts;
36 * enum status - status of mount entry
38 * @STATUS_UNMOUNTED: currently not mounted
39 * @STATUS_MOUNTED: mounted & ready for usage
40 * @STATUS_EXPIRED: mount expired & *temporary* unmounted
41 * @STATUS_IGNORE: entry should be ignored and never mounted
51 struct list_head list;
64 static char *fs_names[] = {
80 #define MAX_MOUNTED 32
81 #define MAX_MOUNT_NAME 32
83 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
84 static int mounted_count = 0;
85 extern char uci_path[32];
87 static void mount_dump_uci_state(void)
89 struct uci_context *ctx;
91 char mountd[] = {"mountd"};
92 char type[] = {"mountd_disc"};
94 unsigned long long int size = 0;
95 unlink("/var/state/mountd");
96 ctx = ucix_init("mountd");
97 uci_set_savedir(ctx, "/var/state/");
98 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
99 list_for_each(p, &mounts)
101 struct mount *q = container_of(p, struct mount, list);
103 if(q->fs == EXTENDED)
105 ucix_add_section(ctx, mountd, q->serial, type);
108 ucix_add_option(ctx, mountd, q->serial, "disc", t);
109 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
110 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
111 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
112 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
113 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
114 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
115 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
116 ucix_add_option(ctx, mountd, q->serial, t, q->size);
117 if(q->fs > MBR && q->fs <= LASTFS)
119 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
120 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
122 if (q->status == STATUS_MOUNTED)
124 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
125 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
127 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
128 ucix_add_option_int(ctx, mountd, mountd, "total", size);
129 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
130 ucix_save_state(ctx, "mountd");
134 static struct mount* mount_find(char *name, char *dev)
137 list_for_each(p, &mounts)
139 struct mount *q = container_of(p, struct mount, list);
141 if(!strcmp(q->name, name))
144 if(!strcmp(q->dev, dev))
150 static void mount_add_list(char *name, char *dev, char *serial,
151 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
154 char dev_path[64], dev_link[64], tmp[64];
156 mount = malloc(sizeof(struct mount));
157 INIT_LIST_HEAD(&mount->list);
158 strncpy(mount->vendor, vendor, 64);
159 strncpy(mount->model, model, 64);
160 strncpy(mount->rev, rev, 64);
161 strncpy(mount->name, name, 64);
162 strncpy(mount->dev, dev, 64);
163 strncpy(mount->serial, serial, 64);
164 strncpy(mount->size, size, 64);
165 strncpy(mount->sector_size, sector_size, 64);
166 mount->status = STATUS_UNMOUNTED;
168 list_add(&mount->list, &mounts);
171 mount->status = STATUS_IGNORE;
175 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
177 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, name);
178 snprintf(dev_path, sizeof(dev_path), "%s%s", "/tmp/run/mountd/", dev);
179 /* If link aleady exists - replace it */
180 if (lstat(dev_link, &st) == 0 && S_ISLNK(st.st_mode)) {
181 snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
182 symlink(dev_path, tmp);
183 rename(tmp, dev_link);
185 symlink(dev_path, dev_link);
187 if (!mount_new("/tmp/run/mountd/", dev))
188 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
192 static int mount_check_disc(char *disc)
194 FILE *fp = fopen("/proc/mounts", "r");
199 log_printf("error reading /proc/mounts");
202 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
206 t = strstr(tmp, " ");
211 l = snprintf(tmp2, 31, "/dev/%s", disc);
213 if(!strncmp(tmp, tmp2, l))
221 static int mount_wait_for_disc(char *disc)
226 int ret = mount_check_disc(disc);
234 int mount_new(char *path, char *dev)
240 mount = mount_find(0, dev);
243 log_printf("request for invalid path %s%s\n", path, dev);
246 if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
248 snprintf(tmp, 256, "%s%s", path, mount->dev);
249 log_printf("mounting %s\n", tmp);
252 pid = autofs_safe_fork();
255 char *options, *fstype;
256 if(mount->fs == EXFAT)
258 options = "rw,uid=1000,gid=1000";
263 options = "rw,uid=1000,gid=1000";
266 if(mount->fs == EXT4)
268 options = "rw,defaults";
271 if(mount->fs == EXT3)
273 options = "rw,defaults";
276 if(mount->fs == EXT2)
278 options = "rw,defaults";
281 if(mount->fs == HFSPLUS)
283 options = "rw,defaults,uid=1000,gid=1000";
286 if(mount->fs == HFSPLUSJOURNAL)
288 options = "ro,defaults,uid=1000,gid=1000";
291 if(mount->fs == NTFS)
296 if(mount->fs > MBR && mount->fs <= LASTFS)
298 struct uci_context *ctx;
299 char *uci_options, *uci_fstype;
300 ctx = ucix_init("mountd");
301 if(fs_names[mount->fs])
303 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
304 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
306 options = uci_options;
309 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
310 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
314 exit(WEXITSTATUS(ret));
316 pid = waitpid(pid, &ret, 0);
317 ret = WEXITSTATUS(ret);
318 log_printf("----------> mount ret = %d\n", ret);
319 if (ret && ret != 0xff) {
323 if(mount_wait_for_disc(mount->dev) == 0)
325 mount->status = STATUS_MOUNTED;
326 mount_dump_uci_state();
331 int mount_remove(char *path, char *dev)
336 snprintf(tmp, 256, "%s%s", path, dev);
337 log_printf("device %s has expired... unmounting %s\n", dev, tmp);
338 ret = system_printf("/bin/umount %s", tmp);
342 mount = mount_find(0, dev);
344 mount->status = STATUS_EXPIRED;
345 log_printf("finished unmounting\n");
346 mount_dump_uci_state();
350 static int dir_sort(const struct dirent **a, const struct dirent **b)
355 static int dir_filter(const struct dirent *a)
357 if(strstr(a->d_name, ":"))
362 static char* mount_get_serial(char *dev)
365 static char tmp2[64];
367 static struct hd_driveid hd;
370 static char disc_id[13];
371 snprintf(tmp, 64, "/dev/%s", dev);
372 disc = open(tmp, O_RDONLY);
375 log_printf("Trying to open unknown disc\n");
378 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
381 serial = (char*)hd.serial_no;
382 /* if we failed, it probably a usb storage device */
383 /* there must be a better way for this */
386 struct dirent **namelist;
387 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
392 char *t = strstr(namelist[n]->d_name, ":");
397 char tmp3[strlen(namelist[n]->d_name) + strlen(dev) + 31];
400 id = atoi(namelist[n]->d_name);
403 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
404 ret = stat(tmp3, &buf);
407 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
408 ret = stat(tmp3, &buf);
413 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
414 fp = fopen(tmp2, "r");
417 while(fgets(tmp2, 64, fp) != NULL)
419 serial = strstr(tmp2, "Serial Number:");
422 serial += strlen("Serial Number: ");
423 serial[strlen(serial) - 1] = '\0';
439 log_printf("could not find a serial number for the device %s\n", dev);
441 /* serial string id is cheap, but makes the discs anonymous */
442 unsigned char uniq[6];
443 unsigned int *u = (unsigned int*) uniq;
444 int l = strlen(serial);
446 memset(disc_id, 0, 13);
448 for(i = 0; i < l; i++)
450 uniq[i%6] += serial[i];
452 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
453 //log_printf("Serial number - %s %s\n", serial, disc_id);
456 sprintf(disc_id, "000000000000");
460 static void mount_dev_add(char *dev)
462 struct mount *mount = mount_find(0, dev);
472 struct uci_context *ctx;
477 char sector_size[64];
483 if (!strncmp(name, "mmcblk", 6))
486 s = mount_get_serial(name);
490 if (!strncmp(name, "mmcblk", 6)) {
491 snprintf(tmp, 64, "part%s", &dev[8]);
492 snprintf(node, 64, "SD-P%s", &dev[8]);
495 snprintf(tmp, 64, "part%s", &dev[3]);
496 snprintf(node, 64, "USB-%s", &dev[2]);
498 if(node[4] >= 'a' && node[4] <= 'z')
503 ctx = ucix_init("mountd");
504 p = ucix_get_option(ctx, "mountd", s, tmp);
513 snprintf(node, 64, "%s", p);
518 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
519 fp = fopen(tmp, "r");
522 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
523 fp = fopen(tmp, "r");
526 snprintf(model, 64, "unknown");
528 fgets(model, 64, fp);
529 model[strlen(model) - 1] = '\0';;
532 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
533 fp = fopen(tmp, "r");
536 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
537 fp = fopen(tmp, "r");
540 snprintf(vendor, 64, "unknown");
542 fgets(vendor, 64, fp);
543 vendor[strlen(vendor) - 1] = '\0';
546 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
547 fp = fopen(tmp, "r");
550 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
551 fp = fopen(tmp, "r");
554 snprintf(rev, 64, "unknown");
557 rev[strlen(rev) - 1] = '\0';
560 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
561 fp = fopen(tmp, "r");
564 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
565 fp = fopen(tmp, "r");
568 snprintf(size, 64, "unknown");
571 size[strlen(size) - 1] = '\0';
576 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
577 fp = fopen(tmp, "r");
579 snprintf(sector_size, 64, "unknown");
581 fgets(sector_size, 64, fp);
582 sector_size[strlen(sector_size) - 1] = '\0';
585 snprintf(tmp, 64, "/dev/%s", dev);
587 if (fs <= MBR || fs > LASTFS) {
590 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
591 mount_dump_uci_state();
595 static int mount_dev_del(struct mount *mount)
600 if (mount->status == STATUS_MOUNTED) {
601 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
602 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
605 umount2(tmp, MNT_DETACH);
608 mount_dump_uci_state();
614 void mount_dump_list(void)
617 list_for_each(p, &mounts)
619 struct mount *q = container_of(p, struct mount, list);
620 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
624 char* is_mounted(char *block, char *path)
627 for(i = 0; i < mounted_count; i++)
630 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
631 return &mounted[i][0][0];
633 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
634 return &mounted[i][0][0];
639 static void mount_update_mount_list(void)
641 FILE *fp = fopen("/proc/mounts", "r");
646 log_printf("error reading /proc/mounts");
650 while(fgets(tmp, 256, fp) != NULL)
654 if (mounted_count + 1 > MAX_MOUNTED) {
655 log_printf("found more than %d mounts \n", MAX_MOUNTED);
659 t = strstr(tmp, " ");
665 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
672 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
679 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
680 /* printf("%s %s %s\n",
681 mounted[mounted_count][0],
682 mounted[mounted_count][1],
683 mounted[mounted_count][2]);*/
690 /* FIXME: we need more intelligence here */
691 static int dir_filter2(const struct dirent *a)
693 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
698 static char block[MAX_BLOCK][MAX_BLOCK];
699 static int blk_cnt = 0;
701 static int check_block(char *b)
704 for(i = 0; i < blk_cnt; i++)
706 if(!strcmp(block[i], b))
712 static void mount_enum_drives(void)
714 struct dirent **namelist, **namelist2;
715 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
722 if(blk_cnt < MAX_BLOCK)
726 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
727 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
732 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
738 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
749 struct mount *q = container_of(p, struct mount, list);
751 struct uci_context *ctx;
754 snprintf(tmp, 64, "part%s", &q->dev[3]);
755 ctx = ucix_init("mountd");
756 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
758 if (t && q->status != STATUS_MOUNTED)
762 if (q->status != STATUS_IGNORE)
764 } else if(!strcmp(t, "1"))
766 if(strncmp(q->name, "Disc-", 5))
768 } else if(strcmp(q->name, t))
773 if(!check_block(q->dev)||del)
775 if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
779 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
781 err = mount_dev_del(q);
783 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, q->name);
785 /* Create "tmp" symlink to non-existing path */
786 snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
787 symlink("## DEVICE MISSING ##", tmp);
789 /* Replace old symlink with the not working one */
790 rename(tmp, dev_link);
792 log_printf("unlinking %s\n", dev_link);
797 p->prev->next = p->next;
798 p->next->prev = p->prev;
802 mount_dump_uci_state();
803 system_printf("/etc/fonstated/ReloadSamba");
807 for(i = 0; i < blk_cnt; i++)
808 mount_dev_add(block[i]);
811 static void mount_check_enum(void)
813 waitpid(-1, 0, WNOHANG);
817 void mount_init(void)
819 INIT_LIST_HEAD(&mounts);
820 timer_add(mount_update_mount_list, 2);
821 timer_add(mount_check_enum, 1);
822 mount_update_mount_list();