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 struct list_head mounts;
37 struct list_head list;
65 #define MAX_MOUNTED 32
66 #define MAX_MOUNT_NAME 32
68 char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
69 int mounted_count = 0;
70 extern char uci_path[32];
72 static void mount_dump_uci_state(void)
74 struct uci_context *ctx;
76 char mountd[] = {"mountd"};
77 char type[] = {"mountd_disc"};
79 unsigned long long int size = 0;
80 unlink("/var/state/mountd");
81 ctx = ucix_init("mountd");
82 uci_set_savedir(ctx, "/var/state/");
83 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
84 list_for_each(p, &mounts)
86 struct mount *q = container_of(p, struct mount, list);
90 ucix_add_section(ctx, mountd, q->serial, type);
93 ucix_add_option(ctx, mountd, q->serial, "disc", t);
94 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
95 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
96 ucix_add_option(ctx, mountd, q->serial, t, (q->mounted)?("1"):("0"));
97 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
98 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
99 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
100 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
101 ucix_add_option(ctx, mountd, q->serial, t, q->size);
102 if(q->fs > MBR && q->fs <= EXT4)
104 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
105 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
109 if((!q->ignore) && q->size && q->sector_size)
110 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
112 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
113 ucix_add_option_int(ctx, mountd, mountd, "total", size);
114 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
115 ucix_save_state(ctx, "mountd");
119 static struct mount* mount_find(char *name, char *dev)
122 list_for_each(p, &mounts)
124 struct mount *q = container_of(p, struct mount, list);
126 if(!strcmp(q->name, name))
129 if(!strcmp(q->dev, dev))
135 static void mount_add_list(char *name, char *dev, char *serial,
136 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
139 char tmp[64], tmp2[64];
140 if(fs <= MBR || fs > EXT4)
142 mount = malloc(sizeof(struct mount));
143 INIT_LIST_HEAD(&mount->list);
144 strncpy(mount->vendor, vendor, 64);
145 strncpy(mount->model, model, 64);
146 strncpy(mount->rev, rev, 64);
147 strncpy(mount->name, name, 64);
148 strncpy(mount->dev, dev, 64);
149 strncpy(mount->serial, serial, 64);
150 strncpy(mount->size, size, 64);
151 strncpy(mount->sector_size, sector_size, 64);
152 mount->ignore = ignore;
155 list_add(&mount->list, &mounts);
156 if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= EXT4))
158 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
159 snprintf(tmp, 64, "%s%s", uci_path, name);
160 snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
162 mount_new("/tmp/run/mountd/", dev);
166 static int mount_check_disc(char *disc)
168 FILE *fp = fopen("/proc/mounts", "r");
173 log_printf("error reading /proc/mounts");
177 while((fgets(tmp, 256, fp) > 0) && (avail == -1))
181 t = strstr(tmp, " ");
186 l = snprintf(tmp2, 31, "/dev/%s", disc);
188 if(!strncmp(tmp, tmp2, l))
196 static int mount_wait_for_disc(char *disc)
201 int ret = mount_check_disc(disc);
209 int mount_new(char *path, char *dev)
215 mount = mount_find(0, dev);
218 log_printf("request for invalid path %s%s\n", path, dev);
221 if(mount->ignore || mount->mounted || mount->fs == EXTENDED)
223 snprintf(tmp, 256, "%s%s", path, mount->dev);
224 log_printf("mounting %s\n", tmp);
227 pid = autofs_safe_fork();
232 log_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
233 ret = system_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
235 if(mount->fs == EXT4)
237 log_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
238 ret = system_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
240 if(mount->fs == EXT3)
242 log_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
243 ret = system_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
245 if(mount->fs == EXT2)
247 log_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
248 ret = system_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
250 if(mount->fs == HFSPLUS)
252 log_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
253 ret = system_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
255 if(mount->fs == NTFS)
257 log_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
258 ret = system_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
260 exit(WEXITSTATUS(ret));
262 pid = waitpid(pid, &ret, 0);
263 ret = WEXITSTATUS(ret);
264 log_printf("----------> mount ret = %d\n", ret);
265 if(ret && (ret != 0xff))
267 if(mount_wait_for_disc(mount->dev) == 0)
270 mount_dump_uci_state();
275 int mount_remove(char *path, char *dev)
280 snprintf(tmp, 256, "%s%s", path, dev);
281 log_printf("%s has expired... unmounting\n", tmp);
282 ret = system_printf("/bin/umount %s", tmp);
286 mount = mount_find(0, dev);
289 log_printf("finished unmounting\n");
290 mount_dump_uci_state();
294 static int dir_sort(const struct dirent **a, const struct dirent **b)
299 static int dir_filter(const struct dirent *a)
301 if(strstr(a->d_name, ":"))
306 static char* mount_get_serial(char *dev)
309 static char tmp2[64];
311 static struct hd_driveid hd;
314 static char disc_id[13];
315 snprintf(tmp, 64, "/dev/%s", dev);
316 disc = open(tmp, O_RDONLY);
319 log_printf("Trying to open unknown disc\n");
322 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
325 serial = (char*)hd.serial_no;
326 /* if we failed, it probably a usb storage device */
327 /* there must be a better way for this */
330 struct dirent **namelist;
331 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
336 char *t = strstr(namelist[n]->d_name, ":");
344 id = atoi(namelist[n]->d_name);
346 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
347 ret = stat(tmp3, &buf);
350 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
351 ret = stat(tmp3, &buf);
356 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
357 fp = fopen(tmp2, "r");
360 while(fgets(tmp2, 64, fp) > 0)
362 serial = strstr(tmp2, "Serial Number:");
365 serial += strlen("Serial Number: ");
366 serial[strlen(serial) - 1] = '\0';
382 log_printf("could not find a serial number for the device %s\n", dev);
384 /* serial string id is cheap, but makes the discs anonymous */
385 unsigned char uniq[6];
386 unsigned int *u = (unsigned int*) uniq;
387 int l = strlen(serial);
389 memset(disc_id, 0, 13);
391 for(i = 0; i < l; i++)
393 uniq[i%6] += serial[i];
395 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
396 //log_printf("Serial number - %s %s\n", serial, disc_id);
399 sprintf(disc_id, "000000000000");
403 static void mount_dev_add(char *dev)
405 struct mount *mount = mount_find(0, dev);
415 struct uci_context *ctx;
420 char sector_size[64];
425 if (!strncmp(name, "mmcblk", 6))
428 s = mount_get_serial(name);
432 if (!strncmp(name, "mmcblk", 6)) {
433 snprintf(tmp, 64, "part%s", &dev[8]);
434 snprintf(node, 64, "SD-P%s", &dev[8]);
437 snprintf(tmp, 64, "part%s", &dev[3]);
438 snprintf(node, 64, "USB-%s", &dev[2]);
440 if(node[4] >= 'a' && node[4] <= 'z')
445 ctx = ucix_init("mountd");
446 p = ucix_get_option(ctx, "mountd", s, tmp);
455 snprintf(node, 64, "%s", p);
460 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
461 fp = fopen(tmp, "r");
464 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
465 fp = fopen(tmp, "r");
468 snprintf(model, 64, "unknown");
470 fgets(model, 64, fp);
471 model[strlen(model) - 1] = '\0';;
474 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
475 fp = fopen(tmp, "r");
478 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
479 fp = fopen(tmp, "r");
482 snprintf(vendor, 64, "unknown");
484 fgets(vendor, 64, fp);
485 vendor[strlen(vendor) - 1] = '\0';
488 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
489 fp = fopen(tmp, "r");
492 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
493 fp = fopen(tmp, "r");
496 snprintf(rev, 64, "unknown");
499 rev[strlen(rev) - 1] = '\0';
502 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
503 fp = fopen(tmp, "r");
506 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
507 fp = fopen(tmp, "r");
510 snprintf(size, 64, "unknown");
513 size[strlen(size) - 1] = '\0';
518 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
519 fp = fopen(tmp, "r");
521 snprintf(sector_size, 64, "unknown");
523 fgets(sector_size, 64, fp);
524 sector_size[strlen(sector_size) - 1] = '\0';
527 snprintf(tmp, 64, "/dev/%s", dev);
528 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, detect_fs(tmp));
529 mount_dump_uci_state();
533 static void mount_dev_del(char *dev)
535 struct mount *mount = mount_find(0, dev);
541 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
542 log_printf("%s has dissappeared ... unmounting\n", tmp);
543 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
544 system_printf("/bin/umount %s", tmp);
546 snprintf(tmp, 64, "%s%s", uci_path, mount->name);
548 mount_dump_uci_state();
553 void mount_dump_list(void)
556 list_for_each(p, &mounts)
558 struct mount *q = container_of(p, struct mount, list);
559 log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
563 char* is_mounted(char *block, char *path)
566 for(i = 0; i < mounted_count; i++)
569 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
570 return &mounted[i][0][0];
572 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
573 return &mounted[i][0][0];
578 static void mount_check_mount_list(void)
580 FILE *fp = fopen("/proc/mounts", "r");
585 log_printf("error reading /proc/mounts");
590 while(fgets(tmp, 256, fp) > 0)
593 t = strstr(tmp, " ");
599 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
606 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
613 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
614 /* printf("%s %s %s\n",
615 mounted[mounted_count][0],
616 mounted[mounted_count][1],
617 mounted[mounted_count][2]);*/
618 if(mounted_count < MAX_MOUNTED - 1)
621 log_printf("found more than %d mounts \n", MAX_MOUNTED);
626 /* FIXME: we need more intelligence here */
627 static int dir_filter2(const struct dirent *a)
629 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
634 char block[MAX_BLOCK][MAX_BLOCK];
637 static int check_block(char *b)
640 for(i = 0; i < blk_cnt; i++)
642 if(!strcmp(block[i], b))
648 static void mount_enum_drives(void)
650 struct dirent **namelist, **namelist2;
651 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
658 if(blk_cnt < MAX_BLOCK)
662 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
663 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
666 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
679 struct mount *q = container_of(p, struct mount, list);
681 struct uci_context *ctx;
684 snprintf(tmp, 64, "part%s", &q->dev[3]);
685 ctx = ucix_init("mountd");
686 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
694 } else if(!strcmp(t, "1"))
696 if(strncmp(q->name, "Disc-", 5))
698 } else if(strcmp(q->name, t))
703 if(!check_block(q->dev)||del)
705 mount_dev_del(q->dev);
706 p->prev->next = p->next;
707 p->next->prev = p->prev;
709 log_printf("removing %s\n", q->dev);
710 snprintf(tmp, 64, "%s%s", uci_path, q->name);
712 system_printf("/etc/mountd/event remove %s %s", q->dev, q->name);
714 mount_dump_uci_state();
715 system_printf("/etc/fonstated/ReloadSamba");
719 for(i = 0; i < blk_cnt; i++)
720 mount_dev_add(block[i]);
723 static void mount_check_enum(void)
725 waitpid(-1, 0, WNOHANG);
729 void mount_init(void)
731 INIT_LIST_HEAD(&mounts);
732 timer_add(mount_check_mount_list, 2);
733 timer_add(mount_check_enum, 1);
734 mount_check_mount_list();