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 * enum status - status of mount entry
39 * @STATUS_UNMOUNTED: currently not mounted
40 * @STATUS_MOUNTED: mounted & ready for usage
41 * @STATUS_IGNORE: entry should be ignored and never mounted
50 struct list_head list;
63 static char *fs_names[] = {
79 #define MAX_MOUNTED 32
80 #define MAX_MOUNT_NAME 32
82 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
83 static int mounted_count = 0;
84 extern char uci_path[32];
86 static void mount_dump_uci_state(void)
88 struct uci_context *ctx;
90 char mountd[] = {"mountd"};
91 char type[] = {"mountd_disc"};
93 unsigned long long int size = 0;
94 unlink("/var/state/mountd");
95 ctx = ucix_init("mountd");
96 uci_set_savedir(ctx, "/var/state/");
97 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
98 list_for_each(p, &mounts)
100 struct mount *q = container_of(p, struct mount, list);
102 if(q->fs == EXTENDED)
104 ucix_add_section(ctx, mountd, q->serial, type);
107 ucix_add_option(ctx, mountd, q->serial, "disc", t);
108 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
109 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
110 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
111 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
112 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
113 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
114 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
115 ucix_add_option(ctx, mountd, q->serial, t, q->size);
116 if(q->fs > MBR && q->fs <= LASTFS)
118 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
119 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
121 if (q->status == STATUS_MOUNTED)
123 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
124 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
126 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
127 ucix_add_option_int(ctx, mountd, mountd, "total", size);
128 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
129 ucix_save_state(ctx, "mountd");
133 static struct mount* mount_find(char *name, char *dev)
136 list_for_each(p, &mounts)
138 struct mount *q = container_of(p, struct mount, list);
140 if(!strcmp(q->name, name))
143 if(!strcmp(q->dev, dev))
149 static void mount_add_list(char *name, char *dev, char *serial,
150 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
153 char tmp[64], tmp2[64];
155 mount = malloc(sizeof(struct mount));
156 INIT_LIST_HEAD(&mount->list);
157 strncpy(mount->vendor, vendor, 64);
158 strncpy(mount->model, model, 64);
159 strncpy(mount->rev, rev, 64);
160 strncpy(mount->name, name, 64);
161 strncpy(mount->dev, dev, 64);
162 strncpy(mount->serial, serial, 64);
163 strncpy(mount->size, size, 64);
164 strncpy(mount->sector_size, sector_size, 64);
165 mount->status = STATUS_UNMOUNTED;
167 list_add(&mount->list, &mounts);
170 mount->status = STATUS_IGNORE;
172 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
173 snprintf(tmp, 64, "%s%s", uci_path, name);
174 snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
176 if (!mount_new("/tmp/run/mountd/", dev))
177 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
181 static int mount_check_disc(char *disc)
183 FILE *fp = fopen("/proc/mounts", "r");
188 log_printf("error reading /proc/mounts");
191 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
195 t = strstr(tmp, " ");
200 l = snprintf(tmp2, 31, "/dev/%s", disc);
202 if(!strncmp(tmp, tmp2, l))
210 static int mount_wait_for_disc(char *disc)
215 int ret = mount_check_disc(disc);
223 int mount_new(char *path, char *dev)
229 mount = mount_find(0, dev);
232 log_printf("request for invalid path %s%s\n", path, dev);
235 if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
237 snprintf(tmp, 256, "%s%s", path, mount->dev);
238 log_printf("mounting %s\n", tmp);
241 pid = autofs_safe_fork();
244 char *options, *fstype;
245 if(mount->fs == EXFAT)
247 options = "rw,uid=1000,gid=1000";
252 options = "rw,uid=1000,gid=1000";
255 if(mount->fs == EXT4)
257 options = "rw,defaults";
260 if(mount->fs == EXT3)
262 options = "rw,defaults";
265 if(mount->fs == EXT2)
267 options = "rw,defaults";
270 if(mount->fs == HFSPLUS)
272 options = "rw,defaults,uid=1000,gid=1000";
275 if(mount->fs == HFSPLUSJOURNAL)
277 options = "ro,defaults,uid=1000,gid=1000";
280 if(mount->fs == NTFS)
285 if(mount->fs > MBR && mount->fs <= LASTFS)
287 struct uci_context *ctx;
288 char *uci_options, *uci_fstype;
289 ctx = ucix_init("mountd");
290 if(fs_names[mount->fs])
292 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
293 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
295 options = uci_options;
298 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
299 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
303 exit(WEXITSTATUS(ret));
305 pid = waitpid(pid, &ret, 0);
306 ret = WEXITSTATUS(ret);
307 log_printf("----------> mount ret = %d\n", ret);
308 if (ret && ret != 0xff) {
312 if(mount_wait_for_disc(mount->dev) == 0)
314 mount->status = STATUS_MOUNTED;
315 mount_dump_uci_state();
320 int mount_remove(char *path, char *dev)
325 snprintf(tmp, 256, "%s%s", path, dev);
326 log_printf("%s has expired... unmounting\n", tmp);
327 ret = system_printf("/bin/umount %s", tmp);
331 mount = mount_find(0, dev);
333 mount->status = STATUS_UNMOUNTED;
334 log_printf("finished unmounting\n");
335 mount_dump_uci_state();
339 static int dir_sort(const struct dirent **a, const struct dirent **b)
344 static int dir_filter(const struct dirent *a)
346 if(strstr(a->d_name, ":"))
351 static char* mount_get_serial(char *dev)
354 static char tmp2[64];
356 static struct hd_driveid hd;
359 static char disc_id[13];
360 snprintf(tmp, 64, "/dev/%s", dev);
361 disc = open(tmp, O_RDONLY);
364 log_printf("Trying to open unknown disc\n");
367 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
370 serial = (char*)hd.serial_no;
371 /* if we failed, it probably a usb storage device */
372 /* there must be a better way for this */
375 struct dirent **namelist;
376 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
381 char *t = strstr(namelist[n]->d_name, ":");
389 id = atoi(namelist[n]->d_name);
391 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
392 ret = stat(tmp3, &buf);
395 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
396 ret = stat(tmp3, &buf);
401 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
402 fp = fopen(tmp2, "r");
405 while(fgets(tmp2, 64, fp) != NULL)
407 serial = strstr(tmp2, "Serial Number:");
410 serial += strlen("Serial Number: ");
411 serial[strlen(serial) - 1] = '\0';
427 log_printf("could not find a serial number for the device %s\n", dev);
429 /* serial string id is cheap, but makes the discs anonymous */
430 unsigned char uniq[6];
431 unsigned int *u = (unsigned int*) uniq;
432 int l = strlen(serial);
434 memset(disc_id, 0, 13);
436 for(i = 0; i < l; i++)
438 uniq[i%6] += serial[i];
440 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
441 //log_printf("Serial number - %s %s\n", serial, disc_id);
444 sprintf(disc_id, "000000000000");
448 static void mount_dev_add(char *dev)
450 struct mount *mount = mount_find(0, dev);
460 struct uci_context *ctx;
465 char sector_size[64];
471 if (!strncmp(name, "mmcblk", 6))
474 s = mount_get_serial(name);
478 if (!strncmp(name, "mmcblk", 6)) {
479 snprintf(tmp, 64, "part%s", &dev[8]);
480 snprintf(node, 64, "SD-P%s", &dev[8]);
483 snprintf(tmp, 64, "part%s", &dev[3]);
484 snprintf(node, 64, "USB-%s", &dev[2]);
486 if(node[4] >= 'a' && node[4] <= 'z')
491 ctx = ucix_init("mountd");
492 p = ucix_get_option(ctx, "mountd", s, tmp);
501 snprintf(node, 64, "%s", p);
506 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
507 fp = fopen(tmp, "r");
510 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
511 fp = fopen(tmp, "r");
514 snprintf(model, 64, "unknown");
516 fgets(model, 64, fp);
517 model[strlen(model) - 1] = '\0';;
520 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
521 fp = fopen(tmp, "r");
524 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
525 fp = fopen(tmp, "r");
528 snprintf(vendor, 64, "unknown");
530 fgets(vendor, 64, fp);
531 vendor[strlen(vendor) - 1] = '\0';
534 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
535 fp = fopen(tmp, "r");
538 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
539 fp = fopen(tmp, "r");
542 snprintf(rev, 64, "unknown");
545 rev[strlen(rev) - 1] = '\0';
548 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
549 fp = fopen(tmp, "r");
552 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
553 fp = fopen(tmp, "r");
556 snprintf(size, 64, "unknown");
559 size[strlen(size) - 1] = '\0';
564 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
565 fp = fopen(tmp, "r");
567 snprintf(sector_size, 64, "unknown");
569 fgets(sector_size, 64, fp);
570 sector_size[strlen(sector_size) - 1] = '\0';
573 snprintf(tmp, 64, "/dev/%s", dev);
575 if (fs <= MBR || fs > LASTFS) {
578 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
579 mount_dump_uci_state();
583 static void mount_dev_del(struct mount *mount)
587 if (mount->status == STATUS_MOUNTED) {
588 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
589 log_printf("%s has dissappeared ... unmounting\n", tmp);
590 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
591 system_printf("/bin/umount %s", tmp);
593 snprintf(tmp, 64, "%s%s", uci_path, mount->name);
595 mount_dump_uci_state();
599 void mount_dump_list(void)
602 list_for_each(p, &mounts)
604 struct mount *q = container_of(p, struct mount, list);
605 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
609 char* is_mounted(char *block, char *path)
612 for(i = 0; i < mounted_count; i++)
615 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
616 return &mounted[i][0][0];
618 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
619 return &mounted[i][0][0];
624 static void mount_update_mount_list(void)
626 FILE *fp = fopen("/proc/mounts", "r");
631 log_printf("error reading /proc/mounts");
635 while(fgets(tmp, 256, fp) != NULL)
639 if (mounted_count + 1 > MAX_MOUNTED) {
640 log_printf("found more than %d mounts \n", MAX_MOUNTED);
644 t = strstr(tmp, " ");
650 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
657 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
664 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
665 /* printf("%s %s %s\n",
666 mounted[mounted_count][0],
667 mounted[mounted_count][1],
668 mounted[mounted_count][2]);*/
675 /* FIXME: we need more intelligence here */
676 static int dir_filter2(const struct dirent *a)
678 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
683 static char block[MAX_BLOCK][MAX_BLOCK];
684 static int blk_cnt = 0;
686 static int check_block(char *b)
689 for(i = 0; i < blk_cnt; i++)
691 if(!strcmp(block[i], b))
697 static void mount_enum_drives(void)
699 struct dirent **namelist, **namelist2;
700 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
707 if(blk_cnt < MAX_BLOCK)
711 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
712 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
717 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
723 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
734 struct mount *q = container_of(p, struct mount, list);
736 struct uci_context *ctx;
739 snprintf(tmp, 64, "part%s", &q->dev[3]);
740 ctx = ucix_init("mountd");
741 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
743 if (t && q->status != STATUS_MOUNTED)
747 if (q->status != STATUS_IGNORE)
749 } else if(!strcmp(t, "1"))
751 if(strncmp(q->name, "Disc-", 5))
753 } else if(strcmp(q->name, t))
758 if(!check_block(q->dev)||del)
761 p->prev->next = p->next;
762 p->next->prev = p->prev;
764 log_printf("removing %s\n", q->dev);
765 if (q->status == STATUS_MOUNTED) {
766 snprintf(tmp, 64, "%s%s", "/tmp/run/mountd/", q->dev);
768 snprintf(tmp, 64, "%s%s", uci_path, q->name);
770 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
773 mount_dump_uci_state();
774 system_printf("/etc/fonstated/ReloadSamba");
778 for(i = 0; i < blk_cnt; i++)
779 mount_dev_add(block[i]);
782 static void mount_check_enum(void)
784 waitpid(-1, 0, WNOHANG);
788 void mount_init(void)
790 INIT_LIST_HEAD(&mounts);
791 timer_add(mount_update_mount_list, 2);
792 timer_add(mount_check_enum, 1);
793 mount_update_mount_list();