9 #include <linux/hdreg.h>
13 #include <sys/inotify.h>
15 #include <sys/types.h>
22 #include "include/log.h"
23 #include "include/list.h"
24 #include "include/sys.h"
25 #include "include/signal.h"
26 #include "include/timer.h"
27 #include "include/autofs.h"
28 #include "include/ucix.h"
29 #include "include/fs.h"
30 #include "include/mount.h"
32 int mount_new(char *path, char *dev);
34 static struct list_head mounts;
37 struct list_head list;
51 static char *fs_names[] = {
67 #define MAX_MOUNTED 32
68 #define MAX_MOUNT_NAME 32
70 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
71 static int mounted_count = 0;
72 extern char uci_path[32];
74 static void mount_dump_uci_state(void)
76 struct uci_context *ctx;
78 char mountd[] = {"mountd"};
79 char type[] = {"mountd_disc"};
81 unsigned long long int size = 0;
82 unlink("/var/state/mountd");
83 ctx = ucix_init("mountd");
84 uci_set_savedir(ctx, "/var/state/");
85 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
86 list_for_each(p, &mounts)
88 struct mount *q = container_of(p, struct mount, list);
92 ucix_add_section(ctx, mountd, q->serial, type);
95 ucix_add_option(ctx, mountd, q->serial, "disc", t);
96 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
97 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
98 ucix_add_option(ctx, mountd, q->serial, t, (q->mounted)?("1"):("0"));
99 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
100 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
101 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
102 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
103 ucix_add_option(ctx, mountd, q->serial, t, q->size);
104 if(q->fs > MBR && q->fs <= LASTFS)
106 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
107 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
111 if((!q->ignore) && q->size && q->sector_size)
112 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
114 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
115 ucix_add_option_int(ctx, mountd, mountd, "total", size);
116 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
117 ucix_save_state(ctx, "mountd");
121 static struct mount* mount_find(char *name, char *dev)
124 list_for_each(p, &mounts)
126 struct mount *q = container_of(p, struct mount, list);
128 if(!strcmp(q->name, name))
131 if(!strcmp(q->dev, dev))
137 static void mount_add_list(char *name, char *dev, char *serial,
138 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
141 char tmp[64], tmp2[64];
143 mount = malloc(sizeof(struct mount));
144 INIT_LIST_HEAD(&mount->list);
145 strncpy(mount->vendor, vendor, 64);
146 strncpy(mount->model, model, 64);
147 strncpy(mount->rev, rev, 64);
148 strncpy(mount->name, name, 64);
149 strncpy(mount->dev, dev, 64);
150 strncpy(mount->serial, serial, 64);
151 strncpy(mount->size, size, 64);
152 strncpy(mount->sector_size, sector_size, 64);
153 mount->ignore = ignore;
156 list_add(&mount->list, &mounts);
159 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
160 snprintf(tmp, 64, "%s%s", uci_path, name);
161 snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
163 if (!mount_new("/tmp/run/mountd/", dev))
164 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
168 static int mount_check_disc(char *disc)
170 FILE *fp = fopen("/proc/mounts", "r");
175 log_printf("error reading /proc/mounts");
178 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
182 t = strstr(tmp, " ");
187 l = snprintf(tmp2, 31, "/dev/%s", disc);
189 if(!strncmp(tmp, tmp2, l))
197 static int mount_wait_for_disc(char *disc)
202 int ret = mount_check_disc(disc);
210 int mount_new(char *path, char *dev)
216 mount = mount_find(0, dev);
219 log_printf("request for invalid path %s%s\n", path, dev);
222 if(mount->ignore || mount->mounted || mount->fs == EXTENDED)
224 snprintf(tmp, 256, "%s%s", path, mount->dev);
225 log_printf("mounting %s\n", tmp);
228 pid = autofs_safe_fork();
231 char *options, *fstype;
232 if(mount->fs == EXFAT)
234 options = "rw,uid=1000,gid=1000";
239 options = "rw,uid=1000,gid=1000";
242 if(mount->fs == EXT4)
244 options = "rw,defaults";
247 if(mount->fs == EXT3)
249 options = "rw,defaults";
252 if(mount->fs == EXT2)
254 options = "rw,defaults";
257 if(mount->fs == HFSPLUS)
259 options = "rw,defaults,uid=1000,gid=1000";
262 if(mount->fs == HFSPLUSJOURNAL)
264 options = "ro,defaults,uid=1000,gid=1000";
267 if(mount->fs == NTFS)
272 if(mount->fs > MBR && mount->fs <= LASTFS)
274 struct uci_context *ctx;
275 char *uci_options, *uci_fstype;
276 ctx = ucix_init("mountd");
277 if(fs_names[mount->fs])
279 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
280 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
282 options = uci_options;
285 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
286 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
290 exit(WEXITSTATUS(ret));
292 pid = waitpid(pid, &ret, 0);
293 ret = WEXITSTATUS(ret);
294 log_printf("----------> mount ret = %d\n", ret);
295 if (ret && ret != 0xff) {
299 if(mount_wait_for_disc(mount->dev) == 0)
302 mount_dump_uci_state();
307 int mount_remove(char *path, char *dev)
312 snprintf(tmp, 256, "%s%s", path, dev);
313 log_printf("%s has expired... unmounting\n", tmp);
314 ret = system_printf("/bin/umount %s", tmp);
318 mount = mount_find(0, dev);
321 log_printf("finished unmounting\n");
322 mount_dump_uci_state();
326 static int dir_sort(const struct dirent **a, const struct dirent **b)
331 static int dir_filter(const struct dirent *a)
333 if(strstr(a->d_name, ":"))
338 static char* mount_get_serial(char *dev)
341 static char tmp2[64];
343 static struct hd_driveid hd;
346 static char disc_id[13];
347 snprintf(tmp, 64, "/dev/%s", dev);
348 disc = open(tmp, O_RDONLY);
351 log_printf("Trying to open unknown disc\n");
354 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
357 serial = (char*)hd.serial_no;
358 /* if we failed, it probably a usb storage device */
359 /* there must be a better way for this */
362 struct dirent **namelist;
363 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
368 char *t = strstr(namelist[n]->d_name, ":");
376 id = atoi(namelist[n]->d_name);
378 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
379 ret = stat(tmp3, &buf);
382 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
383 ret = stat(tmp3, &buf);
388 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
389 fp = fopen(tmp2, "r");
392 while(fgets(tmp2, 64, fp) != NULL)
394 serial = strstr(tmp2, "Serial Number:");
397 serial += strlen("Serial Number: ");
398 serial[strlen(serial) - 1] = '\0';
414 log_printf("could not find a serial number for the device %s\n", dev);
416 /* serial string id is cheap, but makes the discs anonymous */
417 unsigned char uniq[6];
418 unsigned int *u = (unsigned int*) uniq;
419 int l = strlen(serial);
421 memset(disc_id, 0, 13);
423 for(i = 0; i < l; i++)
425 uniq[i%6] += serial[i];
427 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
428 //log_printf("Serial number - %s %s\n", serial, disc_id);
431 sprintf(disc_id, "000000000000");
435 static void mount_dev_add(char *dev)
437 struct mount *mount = mount_find(0, dev);
447 struct uci_context *ctx;
452 char sector_size[64];
458 if (!strncmp(name, "mmcblk", 6))
461 s = mount_get_serial(name);
465 if (!strncmp(name, "mmcblk", 6)) {
466 snprintf(tmp, 64, "part%s", &dev[8]);
467 snprintf(node, 64, "SD-P%s", &dev[8]);
470 snprintf(tmp, 64, "part%s", &dev[3]);
471 snprintf(node, 64, "USB-%s", &dev[2]);
473 if(node[4] >= 'a' && node[4] <= 'z')
478 ctx = ucix_init("mountd");
479 p = ucix_get_option(ctx, "mountd", s, tmp);
488 snprintf(node, 64, "%s", p);
493 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
494 fp = fopen(tmp, "r");
497 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
498 fp = fopen(tmp, "r");
501 snprintf(model, 64, "unknown");
503 fgets(model, 64, fp);
504 model[strlen(model) - 1] = '\0';;
507 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
508 fp = fopen(tmp, "r");
511 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
512 fp = fopen(tmp, "r");
515 snprintf(vendor, 64, "unknown");
517 fgets(vendor, 64, fp);
518 vendor[strlen(vendor) - 1] = '\0';
521 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
522 fp = fopen(tmp, "r");
525 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
526 fp = fopen(tmp, "r");
529 snprintf(rev, 64, "unknown");
532 rev[strlen(rev) - 1] = '\0';
535 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
536 fp = fopen(tmp, "r");
539 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
540 fp = fopen(tmp, "r");
543 snprintf(size, 64, "unknown");
546 size[strlen(size) - 1] = '\0';
551 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
552 fp = fopen(tmp, "r");
554 snprintf(sector_size, 64, "unknown");
556 fgets(sector_size, 64, fp);
557 sector_size[strlen(sector_size) - 1] = '\0';
560 snprintf(tmp, 64, "/dev/%s", dev);
562 if (fs <= MBR || fs > LASTFS) {
565 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
566 mount_dump_uci_state();
570 static void mount_dev_del(char *dev)
572 struct mount *mount = mount_find(0, dev);
578 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
579 log_printf("%s has dissappeared ... unmounting\n", tmp);
580 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
581 system_printf("/bin/umount %s", tmp);
583 snprintf(tmp, 64, "%s%s", uci_path, mount->name);
585 mount_dump_uci_state();
590 void mount_dump_list(void)
593 list_for_each(p, &mounts)
595 struct mount *q = container_of(p, struct mount, list);
596 log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
600 char* is_mounted(char *block, char *path)
603 for(i = 0; i < mounted_count; i++)
606 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
607 return &mounted[i][0][0];
609 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
610 return &mounted[i][0][0];
615 static void mount_check_mount_list(void)
617 FILE *fp = fopen("/proc/mounts", "r");
622 log_printf("error reading /proc/mounts");
626 while(fgets(tmp, 256, fp) != NULL)
629 t = strstr(tmp, " ");
635 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
642 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
649 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
650 /* printf("%s %s %s\n",
651 mounted[mounted_count][0],
652 mounted[mounted_count][1],
653 mounted[mounted_count][2]);*/
654 if(mounted_count < MAX_MOUNTED - 1)
657 log_printf("found more than %d mounts \n", MAX_MOUNTED);
662 /* FIXME: we need more intelligence here */
663 static int dir_filter2(const struct dirent *a)
665 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
670 static char block[MAX_BLOCK][MAX_BLOCK];
671 static int blk_cnt = 0;
673 static int check_block(char *b)
676 for(i = 0; i < blk_cnt; i++)
678 if(!strcmp(block[i], b))
684 static void mount_enum_drives(void)
686 struct dirent **namelist, **namelist2;
687 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
694 if(blk_cnt < MAX_BLOCK)
698 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
699 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
704 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
710 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
721 struct mount *q = container_of(p, struct mount, list);
723 struct uci_context *ctx;
726 snprintf(tmp, 64, "part%s", &q->dev[3]);
727 ctx = ucix_init("mountd");
728 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
736 } else if(!strcmp(t, "1"))
738 if(strncmp(q->name, "Disc-", 5))
740 } else if(strcmp(q->name, t))
745 if(!check_block(q->dev)||del)
747 mount_dev_del(q->dev);
748 p->prev->next = p->next;
749 p->next->prev = p->prev;
751 log_printf("removing %s\n", q->dev);
752 snprintf(tmp, 64, "%s%s", "/tmp/run/mountd/", q->dev);
754 snprintf(tmp, 64, "%s%s", uci_path, q->name);
756 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
758 mount_dump_uci_state();
759 system_printf("/etc/fonstated/ReloadSamba");
763 for(i = 0; i < blk_cnt; i++)
764 mount_dev_add(block[i]);
767 static void mount_check_enum(void)
769 waitpid(-1, 0, WNOHANG);
773 void mount_init(void)
775 INIT_LIST_HEAD(&mounts);
776 timer_add(mount_check_mount_list, 2);
777 timer_add(mount_check_enum, 1);
778 mount_check_mount_list();