kmodloader: support loading modules from a different prefix
[project/ubox.git] / block.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _GNU_SOURCE
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <libgen.h>
20 #include <glob.h>
21
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/swap.h>
25 #include <sys/mount.h>
26 #include <sys/wait.h>
27
28 #include <uci.h>
29 #include <uci_blob.h>
30
31 #include <libubox/list.h>
32 #include <libubox/vlist.h>
33 #include <libubox/blobmsg_json.h>
34 #include <libubox/avl-cmp.h>
35
36 #include "libblkid-tiny/libblkid-tiny.h"
37
38 enum {
39         TYPE_MOUNT,
40         TYPE_SWAP,
41 };
42
43 struct mount {
44         struct vlist_node node;
45         int type;
46
47         char *target;
48         char *path;
49         char *options;
50         char *uuid;
51         char *label;
52         char *device;
53         int extroot;
54         int overlay;
55         int disabled_fsck;
56         unsigned int prio;
57 };
58
59 static struct vlist_tree mounts;
60 static struct blob_buf b;
61 static LIST_HEAD(devices);
62 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
63 static unsigned int delay_root;
64
65 enum {
66         CFG_ANON_MOUNT,
67         CFG_ANON_SWAP,
68         CFG_AUTO_MOUNT,
69         CFG_AUTO_SWAP,
70         CFG_DELAY_ROOT,
71         CFG_CHECK_FS,
72         __CFG_MAX
73 };
74
75 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
76         [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
77         [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
78         [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
79         [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
80         [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
81         [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
82 };
83
84 enum {
85         MOUNT_UUID,
86         MOUNT_LABEL,
87         MOUNT_ENABLE,
88         MOUNT_TARGET,
89         MOUNT_DEVICE,
90         MOUNT_OPTIONS,
91         __MOUNT_MAX
92 };
93
94 static const struct uci_blob_param_list config_attr_list = {
95         .n_params = __CFG_MAX,
96         .params = config_policy,
97 };
98
99 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
100         [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
101         [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
102         [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
103         [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
104         [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
105         [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
106 };
107
108 static const struct uci_blob_param_list mount_attr_list = {
109         .n_params = __MOUNT_MAX,
110         .params = mount_policy,
111 };
112
113 enum {
114         SWAP_ENABLE,
115         SWAP_UUID,
116         SWAP_DEVICE,
117         SWAP_PRIO,
118         __SWAP_MAX
119 };
120
121 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
122         [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
123         [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
124         [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
125         [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
126 };
127
128 static const struct uci_blob_param_list swap_attr_list = {
129         .n_params = __SWAP_MAX,
130         .params = swap_policy,
131 };
132
133 static char *blobmsg_get_strdup(struct blob_attr *attr)
134 {
135         if (!attr)
136                 return NULL;
137
138         return strdup(blobmsg_get_string(attr));
139 }
140
141 static char *blobmsg_get_basename(struct blob_attr *attr)
142 {
143         if (!attr)
144                 return NULL;
145
146         return strdup(basename(blobmsg_get_string(attr)));
147 }
148
149 static int mount_add(struct uci_section *s)
150 {
151         struct blob_attr *tb[__MOUNT_MAX] = { 0 };
152         struct mount *m;
153
154         blob_buf_init(&b, 0);
155         uci_to_blob(&b, s, &mount_attr_list);
156         blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
157
158         if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
159                 return -1;
160
161         if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
162                 return -1;
163
164         m = malloc(sizeof(struct mount));
165         m->type = TYPE_MOUNT;
166         m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
167         m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
168         m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
169         m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
170         m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
171         m->overlay = m->extroot = 0;
172         if (m->target && !strcmp(m->target, "/"))
173                 m->extroot = 1;
174         if (m->target && !strcmp(m->target, "/overlay"))
175                 m->extroot = m->overlay = 1;
176
177         if (m->uuid)
178                 vlist_add(&mounts, &m->node, m->uuid);
179         else if (m->label)
180                 vlist_add(&mounts, &m->node, m->label);
181         else if (m->device)
182                 vlist_add(&mounts, &m->node, m->device);
183
184         return 0;
185 }
186
187 static int swap_add(struct uci_section *s)
188 {
189         struct blob_attr *tb[__SWAP_MAX] = { 0 };
190         struct mount *m;
191
192         blob_buf_init(&b, 0);
193         uci_to_blob(&b, s, &swap_attr_list);
194         blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
195
196         if (!tb[SWAP_UUID] && !tb[SWAP_DEVICE])
197                 return -1;
198
199         m = malloc(sizeof(struct mount));
200         memset(m, 0, sizeof(struct mount));
201         m->type = TYPE_SWAP;
202         m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
203         m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
204         if (tb[SWAP_PRIO])
205                 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
206         if (m->prio)
207                 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
208
209         if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
210                 vlist_add(&mounts, &m->node, (m->uuid) ? (m->uuid) : (m->device));
211
212         return 0;
213 }
214
215 static int global_add(struct uci_section *s)
216 {
217         struct blob_attr *tb[__CFG_MAX] = { 0 };
218
219         blob_buf_init(&b, 0);
220         uci_to_blob(&b, s, &config_attr_list);
221         blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
222
223         if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
224                 anon_mount = 1;
225         if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
226                 anon_swap = 1;
227
228         if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
229                 auto_mount = 1;
230         if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
231                 auto_swap = 1;
232
233         if (tb[CFG_DELAY_ROOT])
234                 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
235
236         if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
237                 check_fs = 1;
238
239         return 0;
240 }
241
242 static struct mount* find_swap(const char *uuid, const char *device)
243 {
244         struct mount *m;
245
246         vlist_for_each_element(&mounts, m, node) {
247                 if (m->type != TYPE_SWAP)
248                         continue;
249                 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
250                         return m;
251                 if (device && m->device && !strcmp(m->device, device))
252                         return m;
253         }
254
255         return NULL;
256 }
257
258 static struct mount* find_block(const char *uuid, const char *label, const char *device,
259                                 const char *target)
260 {
261         struct mount *m;
262
263         vlist_for_each_element(&mounts, m, node) {
264                 if (m->type != TYPE_MOUNT)
265                         continue;
266                 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
267                         return m;
268                 if (m->label && label && !strcmp(m->label, label))
269                         return m;
270                 if (m->target && target && !strcmp(m->target, target))
271                         return m;
272                 if (m->device && device && !strcmp(m->device, device))
273                         return m;
274         }
275
276         return NULL;
277 }
278
279 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
280                           struct vlist_node *node_old)
281 {
282 }
283
284 static int config_load(char *cfg)
285 {
286         struct uci_context *ctx;
287         struct uci_package *pkg;
288         struct uci_element *e;
289
290         vlist_init(&mounts, avl_strcmp, mounts_update);
291
292         ctx = uci_alloc_context();
293         if (cfg) {
294                 char path[32];
295                 snprintf(path, 32, "%s/etc/config", cfg);
296                 uci_set_confdir(ctx, path);
297         }
298
299         if (uci_load(ctx, "fstab", &pkg))
300                 return -1;
301
302         vlist_update(&mounts);
303         uci_foreach_element(&pkg->sections, e) {
304                 struct uci_section *s = uci_to_section(e);
305
306                 if (!strcmp(s->type, "mount"))
307                         mount_add(s);
308                 if (!strcmp(s->type, "swap"))
309                         swap_add(s);
310                 if (!strcmp(s->type, "global"))
311                         global_add(s);
312         }
313         vlist_flush(&mounts);
314
315         return 0;
316 }
317
318 static int _cache_load(const char *path)
319 {
320         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
321         int j;
322         glob_t gl;
323
324         if (glob(path, gl_flags, NULL, &gl) < 0)
325                 return -1;
326
327         for (j = 0; j < gl.gl_pathc; j++) {
328                 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
329                 memset(pr, 0, sizeof(struct blkid_struct_probe));
330                 probe_block(gl.gl_pathv[j], pr);
331                 if (pr->err || !pr->id)
332                         free(pr);
333                 else
334                         list_add_tail(&pr->list, &devices);
335         }
336
337         globfree(&gl);
338
339         return 0;
340 }
341
342 static void cache_load(int mtd)
343 {
344         if (mtd)
345                 _cache_load("/dev/mtdblock*");
346         _cache_load("/dev/mmcblk*");
347         _cache_load("/dev/sd*");
348         _cache_load("/dev/sdc*");
349         _cache_load("/dev/hd*");
350         _cache_load("/dev/md*");
351         _cache_load("/dev/mapper/*");
352 }
353
354 static int print_block_info(struct blkid_struct_probe *pr)
355 {
356         printf("%s:", pr->dev);
357         if (pr->uuid[0])
358                 printf(" UUID=\"%s\"", pr->uuid);
359
360         if (pr->label[0])
361                 printf(" LABEL=\"%s\"", pr->label);
362
363         if (pr->name[0])
364                 printf(" NAME=\"%s\"", pr->name);
365
366         if (pr->version[0])
367                 printf(" VERSION=\"%s\"", pr->version);
368
369         printf(" TYPE=\"%s\"\n", pr->id->name);
370
371         return 0;
372 }
373
374 static int print_block_uci(struct blkid_struct_probe *pr)
375 {
376         if (!strcmp(pr->id->name, "swap")) {
377                 printf("config 'swap'\n");
378         } else {
379                 printf("config 'mount'\n");
380                 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
381         }
382         if (pr->uuid[0])
383                 printf("\toption\tuuid\t'%s'\n", pr->uuid);
384         else
385                 printf("\toption\tdevice\t'%s'\n", pr->dev);
386         printf("\toption\tenabled\t'0'\n\n");
387
388         return 0;
389 }
390
391 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
392 {
393         struct blkid_struct_probe *pr = NULL;
394
395         if (uuid)
396                 list_for_each_entry(pr, &devices, list)
397                         if (!strcmp(pr->uuid, uuid))
398                                 return pr;
399
400         if (label)
401                 list_for_each_entry(pr, &devices, list)
402                         if (strcmp(pr->label, label))
403                                 return pr;
404
405         if (path)
406                 list_for_each_entry(pr, &devices, list)
407                         if (!strcmp(pr->dev, path))
408                                 return pr;
409
410         return NULL;
411 }
412
413 static char* find_mount_point(char *block)
414 {
415         FILE *fp = fopen("/proc/mounts", "r");
416         static char line[256];
417         int len = strlen(block);
418         char *point = NULL;
419
420         if(!fp)
421                 return NULL;
422
423         while (fgets(line, sizeof(line), fp)) {
424                 if (!strncmp(line, block, len)) {
425                         char *p = &line[len + 1];
426                         char *t = strstr(p, " ");
427
428                         if (!t)
429                                 return NULL;
430                         *t = '\0';
431                         point = p;
432                         break;
433                 }
434         }
435
436         fclose(fp);
437
438         return point;
439 }
440
441 static void mkdir_p(char *dir)
442 {
443         char *l = strrchr(dir, '/');
444
445         if (l) {
446                 *l = '\0';
447                 mkdir_p(dir);
448                 *l = '/';
449                 mkdir(dir, 0755);
450         }
451 }
452
453 static void check_filesystem(struct blkid_struct_probe *pr)
454 {
455         pid_t pid;
456         struct stat statbuf;
457         char *e2fsck = "/usr/sbin/e2fsck";
458
459         if (strncmp(pr->id->name, "ext", 3)) {
460                 fprintf(stderr, "check_filesystem: %s is not supported\n", pr->id->name);
461                 return;
462         }
463
464         if (stat(e2fsck, &statbuf) < 0) {
465                 fprintf(stderr, "check_filesystem: %s not found\n", e2fsck);
466                 return;
467         }
468
469         pid = fork();
470         if (!pid) {
471                 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
472                 exit(-1);
473         } else if (pid > 0) {
474                 int status;
475
476                 waitpid(pid, &status, 0);
477                 if (WEXITSTATUS(status))
478                         fprintf(stderr, "check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
479         }
480 }
481
482 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
483 {
484         struct mount *m;
485         char *device;
486
487         if (!pr)
488                 return -1;
489
490         device = basename(pr->dev);
491
492         if (!strcmp(pr->id->name, "swap")) {
493                 if (hotplug && !auto_swap)
494                         return -1;
495                 m = find_swap(pr->uuid, device);
496                 if (m || anon_swap)
497                         swapon(pr->dev, (m) ? (m->prio) : (0));
498
499                 return 0;
500         }
501
502         if (hotplug && !auto_mount)
503                 return -1;
504
505         if (find_mount_point(pr->dev)) {
506                 fprintf(stderr, "%s is already mounted\n", pr->dev);
507                 return -1;
508         }
509
510         m = find_block(pr->uuid, pr->label, device, NULL);
511         if (m && m->extroot)
512                 return -1;
513
514         if (m) {
515                 char *target = m->target;
516                 char _target[32];
517                 int err = 0;
518
519                 if (!target) {
520                         snprintf(_target, sizeof(_target), "/mnt/%s", device);
521                         target = _target;
522                 }
523                 mkdir_p(target);
524
525                 if (check_fs)
526                         check_filesystem(pr);
527
528                 err = mount(pr->dev, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
529                 if (err)
530                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
531                                         pr->dev, pr->id->name, target, err, strerror(err));
532                 return err;
533         }
534
535         if (anon_mount) {
536                 char target[] = "/mnt/mmcblk123";
537                 int err = 0;
538
539                 snprintf(target, sizeof(target), "/mnt/%s", device);
540                 mkdir_p(target);
541
542                 if (check_fs)
543                         check_filesystem(pr);
544
545                 err = mount(pr->dev, target, pr->id->name, 0, "");
546                 if (err)
547                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
548                                         pr->dev, pr->id->name, target, err, strerror(err));
549                 return err;
550         }
551
552         return 0;
553 }
554
555 static int umount_device(struct blkid_struct_probe *pr)
556 {
557         struct mount *m;
558         char *device = basename(pr->dev);
559         char *mp;
560         int err;
561
562         if (!pr)
563                 return -1;
564
565         if (!strcmp(pr->id->name, "swap"))
566                 return -1;
567
568         mp = find_mount_point(pr->dev);
569         if (!mp)
570                 return -1;
571
572         m = find_block(pr->uuid, pr->label, device, NULL);
573         if (m && m->extroot)
574                 return -1;
575
576         err = umount2(mp, MNT_DETACH);
577         if (err)
578                 fprintf(stderr, "unmounting %s (%s)  failed (%d) - %s\n",
579                         pr->dev, mp, err, strerror(err));
580         else
581                 fprintf(stderr, "unmounted %s (%s)\n",
582                         pr->dev, mp);
583
584         return err;
585 }
586
587 static int main_hotplug(int argc, char **argv)
588 {
589         char path[32];
590         char *action, *device, *mount_point;
591
592         action = getenv("ACTION");
593         device = getenv("DEVNAME");
594
595         if (!action || !device)
596                 return -1;
597         snprintf(path, sizeof(path), "/dev/%s", device);
598
599         if (!strcmp(action, "remove")) {
600                 int err = 0;
601                 mount_point = find_mount_point(path);
602                 if (mount_point)
603                         err = umount2(mount_point, MNT_DETACH);
604
605                 if (err)
606                         fprintf(stderr, "umount of %s failed (%d) - %s\n",
607                                         mount_point, err, strerror(err));
608
609                 return 0;
610         } else if (strcmp(action, "add")) {
611                 fprintf(stderr, "Unkown action %s\n", action);
612
613                 return -1;
614         }
615
616         if (config_load(NULL))
617                 return -1;
618         cache_load(0);
619
620         return mount_device(find_block_info(NULL, NULL, path), 1);
621 }
622
623 static int find_block_mtd(char *name, char *part, int plen)
624 {
625         FILE *fp = fopen("/proc/mtd", "r");
626         static char line[256];
627         char *index = NULL;
628
629         if(!fp)
630                 return -1;
631
632         while (!index && fgets(line, sizeof(line), fp)) {
633                 if (strstr(line, name)) {
634                         char *eol = strstr(line, ":");
635
636                         if (!eol)
637                                 continue;
638
639                         *eol = '\0';
640                         index = &line[3];
641                 }
642         }
643
644         fclose(fp);
645
646         if (!index)
647                 return -1;
648
649         snprintf(part, plen, "/dev/mtdblock%s", index);
650
651         return 0;
652 }
653
654 static int check_extroot(char *path)
655 {
656         struct blkid_struct_probe *pr = NULL;
657         char fs[32];
658
659         if (find_block_mtd("rootfs", fs, sizeof(fs)))
660                 return -1;
661
662         list_for_each_entry(pr, &devices, list) {
663                 if (!strcmp(pr->dev, fs)) {
664                         struct stat s;
665                         FILE *fp = NULL;
666                         char tag[32];
667                         char uuid[32] = { 0 };
668
669                         snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
670                         if (stat(tag, &s)) {
671                                 fp = fopen(tag, "w+");
672                                 if (!fp) {
673                                         fprintf(stderr, "extroot: failed to write uuid tag file\n");
674                                         /* return 0 to continue boot regardless of error */
675                                         return 0;
676                                 }
677                                 fputs(pr->uuid, fp);
678                                 fclose(fp);
679                                 return 0;
680                         }
681
682                         fp = fopen(tag, "r");
683                         if (!fp) {
684                                 fprintf(stderr, "extroot: failed to open uuid tag file\n");
685                                 return -1;
686                         }
687
688                         fgets(uuid, sizeof(uuid), fp);
689                         fclose(fp);
690                         if (!strcmp(uuid, pr->uuid))
691                                 return 0;
692                         fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
693                 }
694         }
695         return -1;
696 }
697
698 static int mount_extroot(char *cfg)
699 {
700         char overlay[] = "/tmp/overlay";
701         char mnt[] = "/tmp/mnt";
702         char *path = mnt;
703         struct blkid_struct_probe *pr;
704         struct mount *m;
705         int err = -1;
706
707         if (config_load(cfg))
708                 return -2;
709
710         m = find_block(NULL, NULL, NULL, "/");
711         if (!m)
712                 m = find_block(NULL, NULL, NULL, "/overlay");
713
714         if (!m || !m->extroot)
715                 return -1;
716
717         pr = find_block_info(m->uuid, m->label, NULL);
718
719         if (!pr && delay_root){
720                 fprintf(stderr, "extroot: is not ready yet, retrying in %ui seconds\n", delay_root);
721                 sleep(delay_root);
722                 mkblkdev();
723                 cache_load(0);
724                 pr = find_block_info(m->uuid, m->label, NULL);
725         }
726         if (pr) {
727                 if (strncmp(pr->id->name, "ext", 3)) {
728                         fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
729                         return -1;
730                 }
731                 if (m->overlay)
732                         path = overlay;
733                 mkdir_p(path);
734
735                 if (check_fs)
736                         check_filesystem(pr);
737
738                 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
739
740                 if (err) {
741                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
742                                         pr->dev, pr->id->name, path, err, strerror(err));
743                 } else if (m->overlay) {
744                         err = check_extroot(path);
745                         if (err)
746                                 umount(path);
747                 }
748         }
749
750         return err;
751 }
752
753 static int main_extroot(int argc, char **argv)
754 {
755         struct blkid_struct_probe *pr;
756         char fs[32] = { 0 };
757         char fs_data[32] = { 0 };
758         int err = -1;
759
760         if (!getenv("PREINIT"))
761                 return -1;
762
763         if (argc != 2) {
764                 fprintf(stderr, "Usage: block extroot mountpoint\n");
765                 return -1;
766         }
767
768         mkblkdev();
769         cache_load(1);
770
771         find_block_mtd("rootfs", fs, sizeof(fs));
772         if (!fs[0])
773                 return -2;
774
775         pr = find_block_info(NULL, NULL, fs);
776         if (!pr)
777                 return -3;
778
779         find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
780         if (fs_data[0]) {
781                 pr = find_block_info(NULL, NULL, fs_data);
782                 if (pr && !strcmp(pr->id->name, "jffs2")) {
783                         char cfg[] = "/tmp/jffs_cfg";
784
785                         mkdir_p(cfg);
786                         if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
787                                 err = mount_extroot(cfg);
788                                 umount2(cfg, MNT_DETACH);
789                         }
790                         if (err < 0)
791                                 rmdir("/tmp/overlay");
792                         rmdir(cfg);
793                         return err;
794                 }
795         }
796
797         return mount_extroot(NULL);
798 }
799
800 static int main_mount(int argc, char **argv)
801 {
802         struct blkid_struct_probe *pr;
803
804         if (config_load(NULL))
805                 return -1;
806
807         cache_load(1);
808         list_for_each_entry(pr, &devices, list)
809                 mount_device(pr, 0);
810
811         return 0;
812 }
813
814 static int main_umount(int argc, char **argv)
815 {
816         struct blkid_struct_probe *pr;
817
818         if (config_load(NULL))
819                 return -1;
820
821         cache_load(0);
822         list_for_each_entry(pr, &devices, list)
823                 umount_device(pr);
824
825         return 0;
826 }
827
828 static int main_detect(int argc, char **argv)
829 {
830         struct blkid_struct_probe *pr;
831
832         cache_load(0);
833         printf("config 'global'\n");
834         printf("\toption\tanon_swap\t'0'\n");
835         printf("\toption\tanon_mount\t'0'\n");
836         printf("\toption\tauto_swap\t'1'\n");
837         printf("\toption\tauto_mount\t'1'\n");
838         printf("\toption\tdelay_root\t'0'\n");
839         printf("\toption\tcheck_fs\t'0'\n\n");
840         list_for_each_entry(pr, &devices, list)
841                 print_block_uci(pr);
842
843         return 0;
844 }
845
846 static int main_info(int argc, char **argv)
847 {
848         int i;
849         struct blkid_struct_probe *pr;
850
851         cache_load(1);
852         if (argc == 2) {
853                 list_for_each_entry(pr, &devices, list)
854                         print_block_info(pr);
855
856                 return 0;
857         };
858
859         for (i = 2; i < argc; i++) {
860                 struct stat s;
861
862                 if (stat(argv[i], &s)) {
863                         fprintf(stderr, "failed to stat %s\n", argv[i]);
864                         continue;
865                 }
866                 if (!S_ISBLK(s.st_mode)) {
867                         fprintf(stderr, "%s is not a block device\n", argv[i]);
868                         continue;
869                 }
870                 pr = find_block_info(NULL, NULL, argv[i]);
871                 if (pr)
872                         print_block_info(pr);
873         }
874
875         return 0;
876 }
877
878 static int main_swapon(int argc, char **argv)
879 {
880         if (argc != 2) {
881                 fprintf(stderr, "Usage: swapon <-s> <-a> [DEVICE]\n\n\tStart swapping on [DEVICE]\n -a\tStart swapping on all swap devices\n -s\tShow summary\n");
882                 return -1;
883         }
884
885         if (!strcmp(argv[1], "-s")) {
886                 FILE *fp = fopen("/proc/swaps", "r");
887                 char *lineptr = NULL;
888                 size_t s;
889
890                 if (!fp) {
891                         fprintf(stderr, "failed to open /proc/swaps\n");
892                         return -1;
893                 }
894                 while (getline(&lineptr, &s, fp) > 0)
895                         printf(lineptr);
896                 if (lineptr)
897                         free(lineptr);
898                 fclose(fp);
899         } else if (!strcmp(argv[1], "-a")) {
900                 struct blkid_struct_probe *pr;
901
902                 cache_load(0);
903                 list_for_each_entry(pr, &devices, list) {
904                         if (strcmp(pr->id->name, "swap"))
905                                 continue;
906                         if (swapon(pr->dev, 0))
907                                 fprintf(stderr, "failed to swapon %s\n", pr->dev);
908                 }
909         } else {
910                 struct stat s;
911                 int err;
912
913                 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
914                         fprintf(stderr, "%s is not a block device or file\n", argv[1]);
915                         return -1;
916                 }
917                 err = swapon(argv[1], 0);
918                 if (err) {
919                         fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
920                         return err;
921                 }
922         }
923
924         return 0;
925 }
926
927 static int main_swapoff(int argc, char **argv)
928 {
929         if (argc != 2) {
930                 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\n\tStop swapping on DEVICE\n -a\tStop swapping on all swap devices\n");
931                 return -1;
932         }
933
934         if (!strcmp(argv[1], "-a")) {
935                 FILE *fp = fopen("/proc/swaps", "r");
936                 char line[256];
937
938                 if (!fp) {
939                         fprintf(stderr, "failed to open /proc/swaps\n");
940                         return -1;
941                 }
942                 fgets(line, sizeof(line), fp);
943                 while (fgets(line, sizeof(line), fp)) {
944                         char *end = strchr(line, ' ');
945                         int err;
946
947                         if (!end)
948                                 continue;
949                         *end = '\0';
950                         err = swapoff(line);
951                         if (err)
952                                 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
953                 }
954                 fclose(fp);
955         } else {
956                 struct stat s;
957                 int err;
958
959                 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
960                         fprintf(stderr, "%s is not a block device or file\n", argv[1]);
961                         return -1;
962                 }
963                 err = swapoff(argv[1]);
964                 if (err) {
965                         fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
966                         return err;
967                 }
968         }
969
970         return 0;
971 }
972
973 int main(int argc, char **argv)
974 {
975         char *base = basename(*argv);
976
977         umask(0);
978
979         if (!strcmp(base, "swapon"))
980                 return main_swapon(argc, argv);
981
982         if (!strcmp(base, "swapoff"))
983                 return main_swapoff(argc, argv);
984
985         if ((argc > 1) && !strcmp(base, "block")) {
986                 if (!strcmp(argv[1], "info"))
987                         return main_info(argc, argv);
988
989                 if (!strcmp(argv[1], "detect"))
990                         return main_detect(argc, argv);
991
992                 if (!strcmp(argv[1], "hotplug"))
993                         return main_hotplug(argc, argv);
994
995                 if (!strcmp(argv[1], "extroot"))
996                         return main_extroot(argc, argv);
997
998                 if (!strcmp(argv[1], "mount"))
999                         return main_mount(argc, argv);
1000
1001                 if (!strcmp(argv[1], "umount"))
1002                         return main_umount(argc, argv);
1003         }
1004
1005         fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");
1006
1007         return -1;
1008 }