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, ":");
400 id = atoi(namelist[n]->d_name);
402 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
403 ret = stat(tmp3, &buf);
406 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
407 ret = stat(tmp3, &buf);
412 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
413 fp = fopen(tmp2, "r");
416 while(fgets(tmp2, 64, fp) != NULL)
418 serial = strstr(tmp2, "Serial Number:");
421 serial += strlen("Serial Number: ");
422 serial[strlen(serial) - 1] = '\0';
438 log_printf("could not find a serial number for the device %s\n", dev);
440 /* serial string id is cheap, but makes the discs anonymous */
441 unsigned char uniq[6];
442 unsigned int *u = (unsigned int*) uniq;
443 int l = strlen(serial);
445 memset(disc_id, 0, 13);
447 for(i = 0; i < l; i++)
449 uniq[i%6] += serial[i];
451 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
452 //log_printf("Serial number - %s %s\n", serial, disc_id);
455 sprintf(disc_id, "000000000000");
459 static void mount_dev_add(char *dev)
461 struct mount *mount = mount_find(0, dev);
471 struct uci_context *ctx;
476 char sector_size[64];
482 if (!strncmp(name, "mmcblk", 6))
485 s = mount_get_serial(name);
489 if (!strncmp(name, "mmcblk", 6)) {
490 snprintf(tmp, 64, "part%s", &dev[8]);
491 snprintf(node, 64, "SD-P%s", &dev[8]);
494 snprintf(tmp, 64, "part%s", &dev[3]);
495 snprintf(node, 64, "USB-%s", &dev[2]);
497 if(node[4] >= 'a' && node[4] <= 'z')
502 ctx = ucix_init("mountd");
503 p = ucix_get_option(ctx, "mountd", s, tmp);
512 snprintf(node, 64, "%s", p);
517 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
518 fp = fopen(tmp, "r");
521 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
522 fp = fopen(tmp, "r");
525 snprintf(model, 64, "unknown");
527 fgets(model, 64, fp);
528 model[strlen(model) - 1] = '\0';;
531 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
532 fp = fopen(tmp, "r");
535 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
536 fp = fopen(tmp, "r");
539 snprintf(vendor, 64, "unknown");
541 fgets(vendor, 64, fp);
542 vendor[strlen(vendor) - 1] = '\0';
545 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
546 fp = fopen(tmp, "r");
549 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
550 fp = fopen(tmp, "r");
553 snprintf(rev, 64, "unknown");
556 rev[strlen(rev) - 1] = '\0';
559 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
560 fp = fopen(tmp, "r");
563 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
564 fp = fopen(tmp, "r");
567 snprintf(size, 64, "unknown");
570 size[strlen(size) - 1] = '\0';
575 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
576 fp = fopen(tmp, "r");
578 snprintf(sector_size, 64, "unknown");
580 fgets(sector_size, 64, fp);
581 sector_size[strlen(sector_size) - 1] = '\0';
584 snprintf(tmp, 64, "/dev/%s", dev);
586 if (fs <= MBR || fs > LASTFS) {
589 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
590 mount_dump_uci_state();
594 static int mount_dev_del(struct mount *mount)
599 if (mount->status == STATUS_MOUNTED) {
600 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
601 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
604 umount2(tmp, MNT_DETACH);
607 mount_dump_uci_state();
613 void mount_dump_list(void)
616 list_for_each(p, &mounts)
618 struct mount *q = container_of(p, struct mount, list);
619 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
623 char* is_mounted(char *block, char *path)
626 for(i = 0; i < mounted_count; i++)
629 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
630 return &mounted[i][0][0];
632 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
633 return &mounted[i][0][0];
638 static void mount_update_mount_list(void)
640 FILE *fp = fopen("/proc/mounts", "r");
645 log_printf("error reading /proc/mounts");
649 while(fgets(tmp, 256, fp) != NULL)
653 if (mounted_count + 1 > MAX_MOUNTED) {
654 log_printf("found more than %d mounts \n", MAX_MOUNTED);
658 t = strstr(tmp, " ");
664 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
671 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
678 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
679 /* printf("%s %s %s\n",
680 mounted[mounted_count][0],
681 mounted[mounted_count][1],
682 mounted[mounted_count][2]);*/
689 /* FIXME: we need more intelligence here */
690 static int dir_filter2(const struct dirent *a)
692 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
697 static char block[MAX_BLOCK][MAX_BLOCK];
698 static int blk_cnt = 0;
700 static int check_block(char *b)
703 for(i = 0; i < blk_cnt; i++)
705 if(!strcmp(block[i], b))
711 static void mount_enum_drives(void)
713 struct dirent **namelist, **namelist2;
714 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
721 if(blk_cnt < MAX_BLOCK)
725 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
726 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
731 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
737 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
748 struct mount *q = container_of(p, struct mount, list);
750 struct uci_context *ctx;
753 snprintf(tmp, 64, "part%s", &q->dev[3]);
754 ctx = ucix_init("mountd");
755 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
757 if (t && q->status != STATUS_MOUNTED)
761 if (q->status != STATUS_IGNORE)
763 } else if(!strcmp(t, "1"))
765 if(strncmp(q->name, "Disc-", 5))
767 } else if(strcmp(q->name, t))
772 if(!check_block(q->dev)||del)
774 if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
778 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
780 err = mount_dev_del(q);
782 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, q->name);
784 /* Create "tmp" symlink to non-existing path */
785 snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
786 symlink("## DEVICE MISSING ##", tmp);
788 /* Replace old symlink with the not working one */
789 rename(tmp, dev_link);
791 log_printf("unlinking %s\n", dev_link);
796 p->prev->next = p->next;
797 p->next->prev = p->prev;
801 mount_dump_uci_state();
802 system_printf("/etc/fonstated/ReloadSamba");
806 for(i = 0; i < blk_cnt; i++)
807 mount_dev_add(block[i]);
810 static void mount_check_enum(void)
812 waitpid(-1, 0, WNOHANG);
816 void mount_init(void)
818 INIT_LIST_HEAD(&mounts);
819 timer_add(mount_update_mount_list, 2);
820 timer_add(mount_check_enum, 1);
821 mount_update_mount_list();