ddcd5b303fb9766191adbd3147debce0ced71881
[oweals/mountd.git] / mount.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <sys/ioctl.h>
10 #include <linux/hdreg.h>
11 #include <scsi/sg.h>
12 #include <dirent.h>
13 #include <sys/wait.h>
14 #include <sys/inotify.h>
15 #include <sys/mount.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <glob.h>
19 #include <libgen.h>
20 #include <poll.h>
21 #include <dirent.h>
22 #include <syslog.h>
23
24 #include "include/log.h"
25 #include "include/list.h"
26 #include "include/sys.h"
27 #include "include/signal.h"
28 #include "include/timer.h"
29 #include "include/autofs.h"
30 #include "include/ucix.h"
31 #include "include/fs.h"
32 #include "include/mount.h"
33
34 int mount_new(char *path, char *dev);
35
36 static struct list_head mounts;
37
38 /**
39  * enum status - status of mount entry
40  *
41  * @STATUS_UNMOUNTED: currently not mounted
42  * @STATUS_MOUNTED: mounted & ready for usage
43  * @STATUS_EXPIRED: mount expired & *temporary* unmounted
44  * @STATUS_IGNORE: entry should be ignored and never mounted
45  */
46 enum status {
47         STATUS_UNMOUNTED = 0,
48         STATUS_MOUNTED,
49         STATUS_EXPIRED,
50         STATUS_IGNORE,
51 };
52
53 struct mount {
54         struct list_head list;
55         char name[64];
56         char dev[64];
57         char serial[64];
58         char vendor[64];
59         char model[64];
60         char rev[64];
61         enum status status;
62         char size[64];
63         char sector_size[64];
64         int fs;
65 };
66
67 static char *fs_names[] = {
68         "",
69         "",
70         "mbr",
71         "ext2",
72         "ext3",
73         "fat",
74         "hfsplus",
75         "",
76         "ntfs",
77         "",
78         "exfat",
79         "ext4",
80         "hfsplusjournal"
81 };
82
83 #define MAX_MOUNTED             32
84 #define MAX_MOUNT_NAME  32
85
86 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
87 static int mounted_count = 0;
88 extern char uci_path[32];
89
90 static void mount_dump_uci_state(void)
91 {
92         struct uci_context *ctx;
93         struct list_head *p;
94         char mountd[] = {"mountd"};
95         char type[] = {"mountd_disc"};
96         int mounted = 0;
97         unsigned long long int size = 0;
98         unlink("/var/state/mountd");
99         ctx = ucix_init("mountd");
100         uci_set_savedir(ctx, "/var/state/");
101         ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
102         list_for_each(p, &mounts)
103         {
104                 struct mount *q = container_of(p, struct mount, list);
105                 char t[64];
106                 if(q->fs == EXTENDED)
107                         continue;
108                 ucix_add_section(ctx, mountd, q->serial, type);
109                 strcpy(t, q->dev);
110                 t[3] = '\0';
111                 ucix_add_option(ctx, mountd, q->serial, "disc", t);
112                 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
113                 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
114                 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
115                 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
116                 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
117                 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
118                 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
119                 ucix_add_option(ctx, mountd, q->serial, t, q->size);
120                 if(q->fs > MBR && q->fs <= LASTFS)
121                 {
122                         snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
123                         ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
124                 }
125                 if (q->status == STATUS_MOUNTED)
126                         mounted++;
127                 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
128                         size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
129         }
130         ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
131         ucix_add_option_int(ctx, mountd, mountd, "total", size);
132         system_printf("echo -n %llu > /tmp/run/mountd_size", size);
133         ucix_save_state(ctx, "mountd");
134         ucix_cleanup(ctx);
135 }
136
137 static struct mount* mount_find(char *name, char *dev)
138 {
139         struct list_head *p;
140         list_for_each(p, &mounts)
141         {
142                 struct mount *q = container_of(p, struct mount, list);
143                 if(name)
144                         if(!strcmp(q->name, name))
145                                 return q;
146                 if(dev)
147                         if(!strcmp(q->dev, dev))
148                                 return q;
149         }
150         return 0;
151 }
152
153 static void mount_add_list(char *name, char *dev, char *serial,
154         char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
155 {
156         struct mount *mount;
157         char dev_path[64], dev_link[64], tmp[64];
158
159         mount  = malloc(sizeof(struct mount));
160         INIT_LIST_HEAD(&mount->list);
161         strncpy(mount->vendor, vendor, 64);
162         strncpy(mount->model, model, 64);
163         strncpy(mount->rev, rev, 64);
164         strncpy(mount->name, name, 64);
165         strncpy(mount->dev, dev, 64);
166         strncpy(mount->serial, serial, 64);
167         strncpy(mount->size, size, 64);
168         strncpy(mount->sector_size, sector_size, 64);
169         mount->status = STATUS_UNMOUNTED;
170         mount->fs = fs;
171         list_add(&mount->list, &mounts);
172
173         if (ignore) {
174                 mount->status = STATUS_IGNORE;
175         } else {
176                 struct stat st;
177
178                 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
179
180                 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, name);
181                 snprintf(dev_path, sizeof(dev_path), "%s%s", "/tmp/run/mountd/", dev);
182                 /* If link aleady exists - replace it */
183                 if (lstat(dev_link, &st) ==  0 && S_ISLNK(st.st_mode)) {
184                         snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
185                         symlink(dev_path, tmp);
186                         rename(tmp, dev_link);
187                 } else {
188                         symlink(dev_path, dev_link);
189                 }
190                 if (!mount_new("/tmp/run/mountd/", dev))
191                         system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
192         }
193 }
194
195 static int mount_check_disc(char *disc)
196 {
197         FILE *fp = fopen("/proc/mounts", "r");
198         char tmp[256];
199         int avail = -1;
200         if(!fp)
201         {
202                 log_printf("error reading /proc/mounts");
203                 return avail;
204         }
205         while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
206         {
207                 char *t;
208                 char tmp2[32];
209                 t = strstr(tmp, " ");
210                 if(t)
211                 {
212                         int l;
213                         *t = '\0';
214                         l = snprintf(tmp2, 31, "/dev/%s", disc);
215
216                         if(!strncmp(tmp, tmp2, l))
217                                 avail = 0;
218                 }
219         }
220         fclose(fp);
221         return avail;
222 }
223
224 static int mount_wait_for_disc(char *disc)
225 {
226         int i = 10;
227         while(i--)
228         {
229                 int ret = mount_check_disc(disc);
230                 if(!ret)
231                         return ret;
232                 poll(0, 0, 100);
233         }
234         return -1;
235 }
236
237 int mount_new(char *path, char *dev)
238 {
239         struct mount *mount;
240         char tmp[256];
241         int ret = 1;
242         pid_t pid;
243         mount = mount_find(0, dev);
244         if(!mount)
245         {
246                 log_printf("request for invalid path %s%s\n", path, dev);
247                 return -1;
248         }
249         if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
250                 return -1;
251         snprintf(tmp, 256, "%s%s", path, mount->dev);
252         log_printf("mounting %s\n", tmp);
253         mkdir(tmp, 777);
254
255         pid = autofs_safe_fork();
256         if(!pid)
257         {
258                 char *options, *fstype;
259                 if(mount->fs == EXFAT)
260                 {
261                         options = "rw,uid=1000,gid=1000";
262                         fstype = "exfat";
263                 }
264                 if(mount->fs == FAT)
265                 {
266                         options = "rw,uid=1000,gid=1000";
267                         fstype = "vfat";
268                 }
269                 if(mount->fs == EXT4)
270                 {
271                         options = "rw,defaults";
272                         fstype = "ext4";
273                 }
274                 if(mount->fs == EXT3)
275                 {
276                         options = "rw,defaults";
277                         fstype = "ext3";
278                 }
279                 if(mount->fs == EXT2)
280                 {
281                         options = "rw,defaults";
282                         fstype = "ext2";
283                 }
284                 if(mount->fs == HFSPLUS)
285                 {
286                         options = "rw,defaults,uid=1000,gid=1000";
287                         fstype = "hfsplus";
288                 }
289                 if(mount->fs == HFSPLUSJOURNAL)
290                 {
291                         options = "ro,defaults,uid=1000,gid=1000";
292                         fstype = "hfsplus";
293                 }
294                 if(mount->fs == NTFS)
295                 {
296                         options = "force";
297                         fstype = "ntfs-3g";
298                 }
299                 if(mount->fs > MBR && mount->fs <= LASTFS)
300                 {
301                         struct uci_context *ctx;
302                         char *uci_options, *uci_fstype;
303                         ctx = ucix_init("mountd");
304                         if(fs_names[mount->fs])
305                         {
306                                 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
307                                 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
308                                 if(uci_options)
309                                         options = uci_options;
310                                 if(uci_fstype)
311                                         fstype = uci_fstype;
312                                 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
313                                 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
314                         }
315                         ucix_cleanup(ctx);
316                 }
317                 exit(WEXITSTATUS(ret));
318         }
319         pid = waitpid(pid, &ret, 0);
320         ret = WEXITSTATUS(ret);
321         log_printf("----------> mount ret = %d\n", ret);
322         if (ret && ret != 0xff) {
323                 rmdir(tmp);
324                 return -1;
325         }
326         if(mount_wait_for_disc(mount->dev) == 0)
327         {
328                 mount->status = STATUS_MOUNTED;
329                 mount_dump_uci_state();
330         } else return -1;
331         return 0;
332 }
333
334 int mount_remove(char *path, char *dev)
335 {
336         struct mount *mount;
337         char tmp[256];
338         int ret;
339         snprintf(tmp, 256, "%s%s", path, dev);
340         log_printf("device %s has expired... unmounting %s\n", dev, tmp);
341         ret = system_printf("/bin/umount %s", tmp);
342         if(ret != 0)
343                 return 0;
344         rmdir(tmp);
345         mount = mount_find(0, dev);
346         if(mount)
347                 mount->status = STATUS_EXPIRED;
348         log_printf("finished unmounting\n");
349         mount_dump_uci_state();
350         return 0;
351 }
352
353 static int dir_sort(const struct dirent **a, const struct dirent **b)
354 {
355         return 0;
356 }
357
358 static int dir_filter(const struct dirent *a)
359 {
360         if(strstr(a->d_name, ":"))
361                 return 1;
362         return 0;
363 }
364
365 static char* mount_get_serial(char *dev)
366 {
367         static char tmp[64];
368         static char tmp2[64];
369         int disc;
370         static struct hd_driveid hd;
371         int i;
372         static char *serial;
373         static char disc_id[13];
374         snprintf(tmp, 64, "/dev/%s", dev);
375         disc = open(tmp, O_RDONLY);
376         if(!disc)
377         {
378                 log_printf("Trying to open unknown disc\n");
379                 return 0;
380         }
381         i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
382         close(disc);
383         if(!i)
384                 serial = (char*)hd.serial_no;
385         /* if we failed, it probably a usb storage device */
386         /* there must be a better way for this */
387         if(i)
388         {
389                 struct dirent **namelist;
390                 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
391                 if(n > 0)
392                 {
393                         while(n--)
394                         {
395                                 char *t = strstr(namelist[n]->d_name, ":");
396                                 if(t)
397                                 {
398                                         int id;
399                                         struct stat buf;
400                                         char tmp3[64];
401                                         int ret;
402                                         *t = 0;
403                                         id = atoi(namelist[n]->d_name);
404                                         *t = ':';
405                                         sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
406                                         ret = stat(tmp3, &buf);
407                                         if(ret)
408                                         {
409                                                 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
410                                                 ret = stat(tmp3, &buf);
411                                         }
412                                         if(!ret)
413                                         {
414                                                 FILE *fp;
415                                                 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
416                                                 fp = fopen(tmp2, "r");
417                                                 if(fp)
418                                                 {
419                                                         while(fgets(tmp2, 64, fp) != NULL)
420                                                         {
421                                                                 serial = strstr(tmp2, "Serial Number:");
422                                                                 if(serial)
423                                                                 {
424                                                                         serial += strlen("Serial Number: ");
425                                                                         serial[strlen(serial) - 1] = '\0';
426                                                                         i = 0;
427                                                                         break;
428                                                                 }
429                                                         }
430                                                         fclose(fp);
431                                                 }
432                                         }
433                                 }
434                                 free(namelist[n]);
435                         }
436                         free(namelist);
437                 }
438         }
439         if(i)
440         {
441                 log_printf("could not find a serial number for the device %s\n", dev);
442         } else {
443                 /* serial string id is cheap, but makes the discs anonymous */
444                 unsigned char uniq[6];
445                 unsigned int *u = (unsigned int*) uniq;
446                 int l = strlen(serial);
447                 int i;
448                 memset(disc_id, 0, 13);
449                 memset(uniq, 0, 6);
450                 for(i = 0; i < l; i++)
451                 {
452                         uniq[i%6] += serial[i];
453                 }
454                 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
455                 //log_printf("Serial number - %s %s\n", serial, disc_id);
456                 return disc_id;
457         }
458         sprintf(disc_id, "000000000000");
459         return disc_id;
460 }
461
462 static void mount_dev_add(char *dev)
463 {
464         struct mount *mount = mount_find(0, dev);
465         if(!mount)
466         {
467                 char node[64];
468                 char name[64];
469                 int ignore = 0;
470                 char *s;
471                 char tmp[64];
472                 char tmp2[64];
473                 char *p;
474                 struct uci_context *ctx;
475                 char vendor[64];
476                 char model[64];
477                 char rev[64];
478                 char size[64];
479                 char sector_size[64];
480                 FILE *fp;
481                 int offset = 3;
482                 int fs;
483
484                 strcpy(name, dev);
485                 if (!strncmp(name, "mmcblk", 6))
486                         offset = 7;
487                 name[offset] = '\0';
488                 s = mount_get_serial(name);
489                 if(!s) {
490                         return;
491                 }
492                 if (!strncmp(name, "mmcblk", 6)) {
493                         snprintf(tmp, 64, "part%s", &dev[8]);
494                         snprintf(node, 64, "SD-P%s", &dev[8]);
495
496                 } else {
497                         snprintf(tmp, 64, "part%s", &dev[3]);
498                         snprintf(node, 64, "USB-%s", &dev[2]);
499                 }
500                 if(node[4] >= 'a' && node[4] <= 'z')
501                 {
502                         node[4] -= 'a';
503                         node[4] += 'A';
504                 }
505                 ctx = ucix_init("mountd");
506                 p = ucix_get_option(ctx, "mountd", s, tmp);
507                 ucix_cleanup(ctx);
508                 if(p)
509                 {
510                         if(strlen(p) == 1)
511                         {
512                                 if(*p == '0')
513                                         ignore = 1;
514                         } else {
515                                 snprintf(node, 64, "%s", p);
516                         }
517                 }
518                 strcpy(name, dev);
519                 name[3] = '\0';
520                 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
521                 fp = fopen(tmp, "r");
522                 if(!fp)
523                 {
524                         snprintf(tmp, 64, "/sys/block/%s/device/model", name);
525                         fp = fopen(tmp, "r");
526                 }
527                 if(!fp)
528                         snprintf(model, 64, "unknown");
529                 else {
530                         fgets(model, 64, fp);
531                         model[strlen(model) - 1] = '\0';;
532                         fclose(fp);
533                 }
534                 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
535                 fp = fopen(tmp, "r");
536                 if(!fp)
537                 {
538                         snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
539                         fp = fopen(tmp, "r");
540                 }
541                 if(!fp)
542                         snprintf(vendor, 64, "unknown");
543                 else {
544                         fgets(vendor, 64, fp);
545                         vendor[strlen(vendor) - 1] = '\0';
546                         fclose(fp);
547                 }
548                 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
549                 fp = fopen(tmp, "r");
550                 if(!fp)
551                 {
552                         snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
553                         fp = fopen(tmp, "r");
554                 }
555                 if(!fp)
556                         snprintf(rev, 64, "unknown");
557                 else {
558                         fgets(rev, 64, fp);
559                         rev[strlen(rev) - 1] = '\0';
560                         fclose(fp);
561                 }
562                 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
563                 fp = fopen(tmp, "r");
564                 if(!fp)
565                 {
566                         snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
567                         fp = fopen(tmp, "r");
568                 }
569                 if(!fp)
570                         snprintf(size, 64, "unknown");
571                 else {
572                         fgets(size, 64, fp);
573                         size[strlen(size) - 1] = '\0';
574                         fclose(fp);
575                 }
576                 strcpy(tmp2, dev);
577                 tmp2[3] = '\0';
578                 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
579                 fp = fopen(tmp, "r");
580                 if(!fp)
581                         snprintf(sector_size, 64, "unknown");
582                 else {
583                         fgets(sector_size, 64, fp);
584                         sector_size[strlen(sector_size) - 1] = '\0';
585                         fclose(fp);
586                 }
587                 snprintf(tmp, 64, "/dev/%s", dev);
588                 fs = detect_fs(tmp);
589                 if (fs <= MBR || fs > LASTFS) {
590                         ignore = 1;
591                 }
592                 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
593                 mount_dump_uci_state();
594         }
595 }
596
597 static int mount_dev_del(struct mount *mount)
598 {
599         char tmp[256];
600         int err = 0;
601
602         if (mount->status == STATUS_MOUNTED) {
603                 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
604                 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
605                 if (umount(tmp)) {
606                         err = -errno;
607                 }
608                 rmdir(tmp);
609                 mount_dump_uci_state();
610         }
611
612         return err;
613 }
614
615 void mount_dump_list(void)
616 {
617         struct list_head *p;
618         list_for_each(p, &mounts)
619         {
620                 struct mount *q = container_of(p, struct mount, list);
621                 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
622         }
623 }
624
625 char* is_mounted(char *block, char *path)
626 {
627         int i;
628         for(i = 0; i < mounted_count; i++)
629         {
630                 if(block)
631                         if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
632                                 return &mounted[i][0][0];
633                 if(path)
634                         if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
635                                 return &mounted[i][0][0];
636         }
637         return 0;
638 }
639
640 static void mount_update_mount_list(void)
641 {
642         FILE *fp = fopen("/proc/mounts", "r");
643         char tmp[256];
644
645         if(!fp)
646         {
647                 log_printf("error reading /proc/mounts");
648                 return;
649         }
650         mounted_count = 0;
651         while(fgets(tmp, 256, fp) != NULL)
652         {
653                 char *t, *t2;
654
655                 if (mounted_count + 1 > MAX_MOUNTED) {
656                         log_printf("found more than %d mounts \n", MAX_MOUNTED);
657                         break;
658                 }
659
660                 t = strstr(tmp, " ");
661                 if(t)
662                 {
663                         *t = '\0';
664                         t++;
665                 } else t = tmp;
666                 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
667                 t2 = strstr(t, " ");
668                 if(t2)
669                 {
670                         *t2 = '\0';
671                         t2++;
672                 } else t2 = t;
673                 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
674                 t = strstr(t2, " ");
675                 if(t)
676                 {
677                         *t = '\0';
678                         t++;
679                 } else t = tmp;
680                 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
681         /*      printf("%s %s %s\n",
682                         mounted[mounted_count][0],
683                         mounted[mounted_count][1],
684                         mounted[mounted_count][2]);*/
685
686                 mounted_count++;
687         }
688         fclose(fp);
689 }
690
691 /* FIXME: we need more intelligence here */
692 static int dir_filter2(const struct dirent *a)
693 {
694         if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
695                 return 1;
696         return 0;
697 }
698 #define MAX_BLOCK       64
699 static char block[MAX_BLOCK][MAX_BLOCK];
700 static int blk_cnt = 0;
701
702 static int check_block(char *b)
703 {
704         int i;
705         for(i = 0; i < blk_cnt; i++)
706         {
707                 if(!strcmp(block[i], b))
708                         return 1;
709         }
710         return 0;
711 }
712
713 static void mount_enum_drives(void)
714 {
715         struct dirent **namelist, **namelist2;
716         int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
717         struct list_head *p;
718         blk_cnt = 0;
719         if(n > 0)
720         {
721                 while(n--)
722                 {
723                         if(blk_cnt < MAX_BLOCK)
724                         {
725                                 int m;
726                                 char tmp[64];
727                                 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
728                                 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
729                                 if(m > 0)
730                                 {
731                                         while(m--)
732                                         {
733                                                 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
734                                                 blk_cnt++;
735                                                 free(namelist2[m]);
736                                         }
737                                         free(namelist2);
738                                 } else {
739                                         strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
740                                         blk_cnt++;
741                                 }
742                         }
743                         free(namelist[n]);
744                 }
745                 free(namelist);
746         }
747         p = mounts.next;
748         while(p != &mounts)
749         {
750                 struct mount *q = container_of(p, struct mount, list);
751                 char tmp[64];
752                 struct uci_context *ctx;
753                 int del = 0;
754                 char *t;
755                 snprintf(tmp, 64, "part%s", &q->dev[3]);
756                 ctx = ucix_init("mountd");
757                 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
758                 ucix_cleanup(ctx);
759                 if (t && q->status != STATUS_MOUNTED)
760                 {
761                         if(!strcmp(t, "0"))
762                         {
763                                 if (q->status != STATUS_IGNORE)
764                                         del = 1;
765                         } else if(!strcmp(t, "1"))
766                         {
767                                 if(strncmp(q->name, "Disc-", 5))
768                                         del = 1;
769                         } else if(strcmp(q->name, t))
770                         {
771                                 del = 1;
772                         }
773                 }
774                 if(!check_block(q->dev)||del)
775                 {
776                         int err;
777
778                         err = mount_dev_del(q);
779                         if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
780                                 char dev_link[64];
781
782                                 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, q->name);
783                                 if (err == -EBUSY) {
784                                         /* Create "tmp" symlink to non-existing path */
785                                         snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
786                                         symlink("## DEVICE MISSING ##", tmp);
787
788                                         /* Replace old symlink with the not working one */
789                                         rename(tmp, dev_link);
790                                 } else {
791                                         log_printf("unlinking %s\n", dev_link);
792                                         unlink(dev_link);
793                                 }
794                                 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
795                         }
796
797                         p->prev->next = p->next;
798                         p->next->prev = p->prev;
799                         p = p->next;
800                         free(q);
801
802                         mount_dump_uci_state();
803                         system_printf("/etc/fonstated/ReloadSamba");
804                 } else p = p->next;
805         }
806
807         for(i = 0; i < blk_cnt; i++)
808                 mount_dev_add(block[i]);
809 }
810
811 static void mount_check_enum(void)
812 {
813         waitpid(-1, 0, WNOHANG);
814         mount_enum_drives();
815 }
816
817 void mount_init(void)
818 {
819         INIT_LIST_HEAD(&mounts);
820         timer_add(mount_update_mount_list, 2);
821         timer_add(mount_check_enum, 1);
822         mount_update_mount_list();
823 }