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_EXPIRED: mount expired & *temporary* unmounted
42 * @STATUS_IGNORE: entry should be ignored and never mounted
52 struct list_head list;
65 static char *fs_names[] = {
81 #define MAX_MOUNTED 32
82 #define MAX_MOUNT_NAME 32
84 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
85 static int mounted_count = 0;
86 extern char uci_path[32];
88 static void mount_dump_uci_state(void)
90 struct uci_context *ctx;
92 char mountd[] = {"mountd"};
93 char type[] = {"mountd_disc"};
95 unsigned long long int size = 0;
96 unlink("/var/state/mountd");
97 ctx = ucix_init("mountd");
98 uci_set_savedir(ctx, "/var/state/");
99 ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
100 list_for_each(p, &mounts)
102 struct mount *q = container_of(p, struct mount, list);
104 if(q->fs == EXTENDED)
106 ucix_add_section(ctx, mountd, q->serial, type);
109 ucix_add_option(ctx, mountd, q->serial, "disc", t);
110 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
111 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
112 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
113 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
114 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
115 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
116 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
117 ucix_add_option(ctx, mountd, q->serial, t, q->size);
118 if(q->fs > MBR && q->fs <= LASTFS)
120 snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
121 ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
123 if (q->status == STATUS_MOUNTED)
125 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
126 size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
128 ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
129 ucix_add_option_int(ctx, mountd, mountd, "total", size);
130 system_printf("echo -n %llu > /tmp/run/mountd_size", size);
131 ucix_save_state(ctx, "mountd");
135 static struct mount* mount_find(char *name, char *dev)
138 list_for_each(p, &mounts)
140 struct mount *q = container_of(p, struct mount, list);
142 if(!strcmp(q->name, name))
145 if(!strcmp(q->dev, dev))
151 static void mount_add_list(char *name, char *dev, char *serial,
152 char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
155 char dev_path[64], dev_link[64];
157 mount = malloc(sizeof(struct mount));
158 INIT_LIST_HEAD(&mount->list);
159 strncpy(mount->vendor, vendor, 64);
160 strncpy(mount->model, model, 64);
161 strncpy(mount->rev, rev, 64);
162 strncpy(mount->name, name, 64);
163 strncpy(mount->dev, dev, 64);
164 strncpy(mount->serial, serial, 64);
165 strncpy(mount->size, size, 64);
166 strncpy(mount->sector_size, sector_size, 64);
167 mount->status = STATUS_UNMOUNTED;
169 list_add(&mount->list, &mounts);
172 mount->status = STATUS_IGNORE;
174 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
175 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, name);
176 snprintf(dev_path, sizeof(dev_path), "%s%s", "/tmp/run/mountd/", dev);
177 symlink(dev_path, dev_link);
178 if (!mount_new("/tmp/run/mountd/", dev))
179 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
183 static int mount_check_disc(char *disc)
185 FILE *fp = fopen("/proc/mounts", "r");
190 log_printf("error reading /proc/mounts");
193 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
197 t = strstr(tmp, " ");
202 l = snprintf(tmp2, 31, "/dev/%s", disc);
204 if(!strncmp(tmp, tmp2, l))
212 static int mount_wait_for_disc(char *disc)
217 int ret = mount_check_disc(disc);
225 int mount_new(char *path, char *dev)
231 mount = mount_find(0, dev);
234 log_printf("request for invalid path %s%s\n", path, dev);
237 if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
239 snprintf(tmp, 256, "%s%s", path, mount->dev);
240 log_printf("mounting %s\n", tmp);
243 pid = autofs_safe_fork();
246 char *options, *fstype;
247 if(mount->fs == EXFAT)
249 options = "rw,uid=1000,gid=1000";
254 options = "rw,uid=1000,gid=1000";
257 if(mount->fs == EXT4)
259 options = "rw,defaults";
262 if(mount->fs == EXT3)
264 options = "rw,defaults";
267 if(mount->fs == EXT2)
269 options = "rw,defaults";
272 if(mount->fs == HFSPLUS)
274 options = "rw,defaults,uid=1000,gid=1000";
277 if(mount->fs == HFSPLUSJOURNAL)
279 options = "ro,defaults,uid=1000,gid=1000";
282 if(mount->fs == NTFS)
287 if(mount->fs > MBR && mount->fs <= LASTFS)
289 struct uci_context *ctx;
290 char *uci_options, *uci_fstype;
291 ctx = ucix_init("mountd");
292 if(fs_names[mount->fs])
294 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
295 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
297 options = uci_options;
300 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
301 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
305 exit(WEXITSTATUS(ret));
307 pid = waitpid(pid, &ret, 0);
308 ret = WEXITSTATUS(ret);
309 log_printf("----------> mount ret = %d\n", ret);
310 if (ret && ret != 0xff) {
314 if(mount_wait_for_disc(mount->dev) == 0)
316 mount->status = STATUS_MOUNTED;
317 mount_dump_uci_state();
322 int mount_remove(char *path, char *dev)
327 snprintf(tmp, 256, "%s%s", path, dev);
328 log_printf("device %s has expired... unmounting %s\n", dev, tmp);
329 ret = system_printf("/bin/umount %s", tmp);
333 mount = mount_find(0, dev);
335 mount->status = STATUS_EXPIRED;
336 log_printf("finished unmounting\n");
337 mount_dump_uci_state();
341 static int dir_sort(const struct dirent **a, const struct dirent **b)
346 static int dir_filter(const struct dirent *a)
348 if(strstr(a->d_name, ":"))
353 static char* mount_get_serial(char *dev)
356 static char tmp2[64];
358 static struct hd_driveid hd;
361 static char disc_id[13];
362 snprintf(tmp, 64, "/dev/%s", dev);
363 disc = open(tmp, O_RDONLY);
366 log_printf("Trying to open unknown disc\n");
369 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
372 serial = (char*)hd.serial_no;
373 /* if we failed, it probably a usb storage device */
374 /* there must be a better way for this */
377 struct dirent **namelist;
378 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
383 char *t = strstr(namelist[n]->d_name, ":");
391 id = atoi(namelist[n]->d_name);
393 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
394 ret = stat(tmp3, &buf);
397 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
398 ret = stat(tmp3, &buf);
403 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
404 fp = fopen(tmp2, "r");
407 while(fgets(tmp2, 64, fp) != NULL)
409 serial = strstr(tmp2, "Serial Number:");
412 serial += strlen("Serial Number: ");
413 serial[strlen(serial) - 1] = '\0';
429 log_printf("could not find a serial number for the device %s\n", dev);
431 /* serial string id is cheap, but makes the discs anonymous */
432 unsigned char uniq[6];
433 unsigned int *u = (unsigned int*) uniq;
434 int l = strlen(serial);
436 memset(disc_id, 0, 13);
438 for(i = 0; i < l; i++)
440 uniq[i%6] += serial[i];
442 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
443 //log_printf("Serial number - %s %s\n", serial, disc_id);
446 sprintf(disc_id, "000000000000");
450 static void mount_dev_add(char *dev)
452 struct mount *mount = mount_find(0, dev);
462 struct uci_context *ctx;
467 char sector_size[64];
473 if (!strncmp(name, "mmcblk", 6))
476 s = mount_get_serial(name);
480 if (!strncmp(name, "mmcblk", 6)) {
481 snprintf(tmp, 64, "part%s", &dev[8]);
482 snprintf(node, 64, "SD-P%s", &dev[8]);
485 snprintf(tmp, 64, "part%s", &dev[3]);
486 snprintf(node, 64, "USB-%s", &dev[2]);
488 if(node[4] >= 'a' && node[4] <= 'z')
493 ctx = ucix_init("mountd");
494 p = ucix_get_option(ctx, "mountd", s, tmp);
503 snprintf(node, 64, "%s", p);
508 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
509 fp = fopen(tmp, "r");
512 snprintf(tmp, 64, "/sys/block/%s/device/model", name);
513 fp = fopen(tmp, "r");
516 snprintf(model, 64, "unknown");
518 fgets(model, 64, fp);
519 model[strlen(model) - 1] = '\0';;
522 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
523 fp = fopen(tmp, "r");
526 snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
527 fp = fopen(tmp, "r");
530 snprintf(vendor, 64, "unknown");
532 fgets(vendor, 64, fp);
533 vendor[strlen(vendor) - 1] = '\0';
536 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
537 fp = fopen(tmp, "r");
540 snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
541 fp = fopen(tmp, "r");
544 snprintf(rev, 64, "unknown");
547 rev[strlen(rev) - 1] = '\0';
550 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
551 fp = fopen(tmp, "r");
554 snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
555 fp = fopen(tmp, "r");
558 snprintf(size, 64, "unknown");
561 size[strlen(size) - 1] = '\0';
566 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
567 fp = fopen(tmp, "r");
569 snprintf(sector_size, 64, "unknown");
571 fgets(sector_size, 64, fp);
572 sector_size[strlen(sector_size) - 1] = '\0';
575 snprintf(tmp, 64, "/dev/%s", dev);
577 if (fs <= MBR || fs > LASTFS) {
580 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
581 mount_dump_uci_state();
585 static void mount_dev_del(struct mount *mount)
589 if (mount->status == STATUS_MOUNTED) {
590 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
591 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
592 system_printf("/bin/umount %s", tmp);
594 mount_dump_uci_state();
598 void mount_dump_list(void)
601 list_for_each(p, &mounts)
603 struct mount *q = container_of(p, struct mount, list);
604 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
608 char* is_mounted(char *block, char *path)
611 for(i = 0; i < mounted_count; i++)
614 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
615 return &mounted[i][0][0];
617 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
618 return &mounted[i][0][0];
623 static void mount_update_mount_list(void)
625 FILE *fp = fopen("/proc/mounts", "r");
630 log_printf("error reading /proc/mounts");
634 while(fgets(tmp, 256, fp) != NULL)
638 if (mounted_count + 1 > MAX_MOUNTED) {
639 log_printf("found more than %d mounts \n", MAX_MOUNTED);
643 t = strstr(tmp, " ");
649 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
656 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
663 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
664 /* printf("%s %s %s\n",
665 mounted[mounted_count][0],
666 mounted[mounted_count][1],
667 mounted[mounted_count][2]);*/
674 /* FIXME: we need more intelligence here */
675 static int dir_filter2(const struct dirent *a)
677 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
682 static char block[MAX_BLOCK][MAX_BLOCK];
683 static int blk_cnt = 0;
685 static int check_block(char *b)
688 for(i = 0; i < blk_cnt; i++)
690 if(!strcmp(block[i], b))
696 static void mount_enum_drives(void)
698 struct dirent **namelist, **namelist2;
699 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
706 if(blk_cnt < MAX_BLOCK)
710 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
711 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
716 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
722 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
733 struct mount *q = container_of(p, struct mount, list);
735 struct uci_context *ctx;
738 snprintf(tmp, 64, "part%s", &q->dev[3]);
739 ctx = ucix_init("mountd");
740 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
742 if (t && q->status != STATUS_MOUNTED)
746 if (q->status != STATUS_IGNORE)
748 } else if(!strcmp(t, "1"))
750 if(strncmp(q->name, "Disc-", 5))
752 } else if(strcmp(q->name, t))
757 if(!check_block(q->dev)||del)
760 if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
761 snprintf(tmp, 64, "%s%s", uci_path, q->name);
762 log_printf("unlinking %s\n", tmp);
764 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
767 p->prev->next = p->next;
768 p->next->prev = p->prev;
772 mount_dump_uci_state();
773 system_printf("/etc/fonstated/ReloadSamba");
777 for(i = 0; i < blk_cnt; i++)
778 mount_dev_add(block[i]);
781 static void mount_check_enum(void)
783 waitpid(-1, 0, WNOHANG);
787 void mount_init(void)
789 INIT_LIST_HEAD(&mounts);
790 timer_add(mount_update_mount_list, 2);
791 timer_add(mount_check_enum, 1);
792 mount_update_mount_list();