fstools: allow the mounting with full access time accounting
[project/fstools.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 <getopt.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <libgen.h>
21 #include <glob.h>
22 #include <dirent.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/swap.h>
32 #include <sys/mount.h>
33 #include <sys/wait.h>
34 #include <sys/sysmacros.h>
35
36 #include <linux/fs.h>
37
38 #include <uci.h>
39 #include <uci_blob.h>
40
41 #include <libubox/ulog.h>
42 #include <libubox/list.h>
43 #include <libubox/vlist.h>
44 #include <libubox/blobmsg_json.h>
45 #include <libubox/avl-cmp.h>
46 #include <libubus.h>
47
48 #include "probe.h"
49
50 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
51
52 #ifdef UBIFS_EXTROOT
53 #include "libubi/libubi.h"
54 #endif
55
56 enum {
57 TYPE_MOUNT,
58 TYPE_SWAP,
59 };
60
61 enum {
62 TYPE_DEV,
63 TYPE_HOTPLUG,
64 TYPE_AUTOFS,
65 };
66
67 struct mount {
68 struct vlist_node node;
69 int type;
70
71 char *target;
72 char *path;
73 char *options;
74 uint32_t flags;
75 char *uuid;
76 char *label;
77 char *device;
78 int extroot;
79 int autofs;
80 int overlay;
81 int disabled_fsck;
82 unsigned int prio;
83 };
84
85 static struct vlist_tree mounts;
86 static struct blob_buf b;
87 static LIST_HEAD(devices);
88 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
89 static unsigned int delay_root;
90
91 enum {
92 CFG_ANON_MOUNT,
93 CFG_ANON_SWAP,
94 CFG_AUTO_MOUNT,
95 CFG_AUTO_SWAP,
96 CFG_DELAY_ROOT,
97 CFG_CHECK_FS,
98 __CFG_MAX
99 };
100
101 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
102 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
103 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
104 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
105 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
106 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
107 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
108 };
109
110 enum {
111 MOUNT_UUID,
112 MOUNT_LABEL,
113 MOUNT_ENABLE,
114 MOUNT_TARGET,
115 MOUNT_DEVICE,
116 MOUNT_OPTIONS,
117 MOUNT_AUTOFS,
118 __MOUNT_MAX
119 };
120
121 static const struct uci_blob_param_list config_attr_list = {
122 .n_params = __CFG_MAX,
123 .params = config_policy,
124 };
125
126 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
127 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
128 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
129 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
130 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
131 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
132 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
133 [MOUNT_AUTOFS] = { .name = "autofs", .type = BLOBMSG_TYPE_INT32 },
134 };
135
136 static const struct uci_blob_param_list mount_attr_list = {
137 .n_params = __MOUNT_MAX,
138 .params = mount_policy,
139 };
140
141 enum {
142 SWAP_ENABLE,
143 SWAP_UUID,
144 SWAP_LABEL,
145 SWAP_DEVICE,
146 SWAP_PRIO,
147 __SWAP_MAX
148 };
149
150 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
151 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
152 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
153 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
154 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
155 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
156 };
157
158 static const struct uci_blob_param_list swap_attr_list = {
159 .n_params = __SWAP_MAX,
160 .params = swap_policy,
161 };
162
163 struct mount_flag {
164 const char *name;
165 int32_t flag;
166 };
167
168 static const struct mount_flag mount_flags[] = {
169 { "sync", MS_SYNCHRONOUS },
170 { "async", ~MS_SYNCHRONOUS },
171 { "dirsync", MS_DIRSYNC },
172 { "mand", MS_MANDLOCK },
173 { "nomand", ~MS_MANDLOCK },
174 { "atime", ~MS_NOATIME },
175 { "noatime", MS_NOATIME },
176 { "dev", ~MS_NODEV },
177 { "nodev", MS_NODEV },
178 { "diratime", ~MS_NODIRATIME },
179 { "nodiratime", MS_NODIRATIME },
180 { "exec", ~MS_NOEXEC },
181 { "noexec", MS_NOEXEC },
182 { "suid", ~MS_NOSUID },
183 { "nosuid", MS_NOSUID },
184 { "rw", ~MS_RDONLY },
185 { "ro", MS_RDONLY },
186 { "relatime", MS_RELATIME },
187 { "norelatime", ~MS_RELATIME },
188 { "strictatime", MS_STRICTATIME },
189 { "acl", MS_POSIXACL },
190 { "noacl", ~MS_POSIXACL },
191 { "nouser_xattr", MS_NOUSER },
192 { "user_xattr", ~MS_NOUSER },
193 };
194
195 static char *blobmsg_get_strdup(struct blob_attr *attr)
196 {
197 if (!attr)
198 return NULL;
199
200 return strdup(blobmsg_get_string(attr));
201 }
202
203 static char *blobmsg_get_basename(struct blob_attr *attr)
204 {
205 if (!attr)
206 return NULL;
207
208 return strdup(basename(blobmsg_get_string(attr)));
209 }
210
211 static void parse_mount_options(struct mount *m, char *optstr)
212 {
213 int i;
214 bool is_flag;
215 char *p, *opts, *last;
216
217 m->flags = 0;
218 m->options = NULL;
219
220 if (!optstr || !*optstr)
221 return;
222
223 m->options = opts = calloc(1, strlen(optstr) + 1);
224
225 if (!m->options)
226 return;
227
228 p = last = optstr;
229
230 do {
231 p = strchr(p, ',');
232
233 if (p)
234 *p++ = 0;
235
236 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
237 if (!strcmp(last, mount_flags[i].name)) {
238 if (mount_flags[i].flag < 0)
239 m->flags &= (uint32_t)mount_flags[i].flag;
240 else
241 m->flags |= (uint32_t)mount_flags[i].flag;
242 is_flag = true;
243 break;
244 }
245 }
246
247 if (!is_flag)
248 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
249
250 last = p;
251
252 } while (p);
253
254 free(optstr);
255 }
256
257 static int mount_add(struct uci_section *s)
258 {
259 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
260 struct mount *m;
261
262 blob_buf_init(&b, 0);
263 uci_to_blob(&b, s, &mount_attr_list);
264 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
265
266 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
267 return -1;
268
269 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
270 return -1;
271
272 m = malloc(sizeof(struct mount));
273 m->type = TYPE_MOUNT;
274 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
275 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
276 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
277 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
278 if (tb[MOUNT_AUTOFS])
279 m->autofs = blobmsg_get_u32(tb[MOUNT_AUTOFS]);
280 else
281 m->autofs = 0;
282 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
283
284 m->overlay = m->extroot = 0;
285 if (m->target && !strcmp(m->target, "/"))
286 m->extroot = 1;
287 if (m->target && !strcmp(m->target, "/overlay"))
288 m->extroot = m->overlay = 1;
289
290 if (m->target && *m->target != '/') {
291 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
292 s->e.name, m->target);
293 free(m);
294 return -1;
295 }
296
297 if (m->uuid)
298 vlist_add(&mounts, &m->node, m->uuid);
299 else if (m->label)
300 vlist_add(&mounts, &m->node, m->label);
301 else if (m->device)
302 vlist_add(&mounts, &m->node, m->device);
303
304 return 0;
305 }
306
307 static int swap_add(struct uci_section *s)
308 {
309 struct blob_attr *tb[__SWAP_MAX] = { 0 };
310 struct mount *m;
311
312 blob_buf_init(&b, 0);
313 uci_to_blob(&b, s, &swap_attr_list);
314 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
315
316 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
317 return -1;
318
319 m = malloc(sizeof(struct mount));
320 memset(m, 0, sizeof(struct mount));
321 m->type = TYPE_SWAP;
322 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
323 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
324 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
325 if (tb[SWAP_PRIO])
326 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
327 if (m->prio)
328 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
329
330 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
331 /* store complete swap path */
332 if (tb[SWAP_DEVICE])
333 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
334
335 if (m->uuid)
336 vlist_add(&mounts, &m->node, m->uuid);
337 else if (m->label)
338 vlist_add(&mounts, &m->node, m->label);
339 else if (m->device)
340 vlist_add(&mounts, &m->node, m->device);
341 }
342
343 return 0;
344 }
345
346 static int global_add(struct uci_section *s)
347 {
348 struct blob_attr *tb[__CFG_MAX] = { 0 };
349
350 blob_buf_init(&b, 0);
351 uci_to_blob(&b, s, &config_attr_list);
352 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
353
354 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
355 anon_mount = 1;
356 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
357 anon_swap = 1;
358
359 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
360 auto_mount = 1;
361 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
362 auto_swap = 1;
363
364 if (tb[CFG_DELAY_ROOT])
365 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
366
367 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
368 check_fs = 1;
369
370 return 0;
371 }
372
373 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
374 {
375 struct mount *m;
376
377 vlist_for_each_element(&mounts, m, node) {
378 if (m->type != TYPE_SWAP)
379 continue;
380 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
381 return m;
382 if (label && m->label && !strcmp(m->label, label))
383 return m;
384 if (device && m->device && !strcmp(m->device, device))
385 return m;
386 }
387
388 return NULL;
389 }
390
391 static struct mount* find_block(const char *uuid, const char *label, const char *device,
392 const char *target)
393 {
394 struct mount *m;
395
396 vlist_for_each_element(&mounts, m, node) {
397 if (m->type != TYPE_MOUNT)
398 continue;
399 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
400 return m;
401 if (m->label && label && !strcmp(m->label, label))
402 return m;
403 if (m->target && target && !strcmp(m->target, target))
404 return m;
405 if (m->device && device && !strcmp(m->device, device))
406 return m;
407 }
408
409 return NULL;
410 }
411
412 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
413 struct vlist_node *node_old)
414 {
415 }
416
417 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
418 {
419 char *file = basename(path);
420 char *dir = dirname(path);
421 char *err;
422 struct uci_package *pkg;
423
424 uci_set_confdir(ctx, dir);
425 ULOG_INFO("attempting to load %s/%s\n", dir, file);
426
427 if (uci_load(ctx, file, &pkg)) {
428 uci_get_errorstr(ctx, &err, file);
429 ULOG_ERR("unable to load configuration (%s)\n", err);
430
431 free(err);
432 return NULL;
433 }
434
435 return pkg;
436 }
437
438 static int config_load(char *cfg)
439 {
440 struct uci_context *ctx = uci_alloc_context();
441 struct uci_package *pkg = NULL;
442 struct uci_element *e;
443 char path[64];
444
445 vlist_init(&mounts, avl_strcmp, mounts_update);
446
447 if (cfg) {
448 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
449 pkg = config_try_load(ctx, path);
450
451 if (!pkg) {
452 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
453 pkg = config_try_load(ctx, path);
454 }
455 }
456
457 if (!pkg) {
458 snprintf(path, sizeof(path), "/etc/config/fstab");
459 pkg = config_try_load(ctx, path);
460 }
461
462 if (!pkg) {
463 ULOG_ERR("no usable configuration\n");
464 return -1;
465 }
466
467 vlist_update(&mounts);
468 uci_foreach_element(&pkg->sections, e) {
469 struct uci_section *s = uci_to_section(e);
470
471 if (!strcmp(s->type, "mount"))
472 mount_add(s);
473 if (!strcmp(s->type, "swap"))
474 swap_add(s);
475 if (!strcmp(s->type, "global"))
476 global_add(s);
477 }
478 vlist_flush(&mounts);
479
480 return 0;
481 }
482
483 static struct probe_info* _probe_path(char *path)
484 {
485 struct probe_info *pr;
486 char tmppath[64];
487
488 /* skip ubi device if ubiblock device is present */
489 if (path[5] == 'u' && path[6] == 'b' && path[7] == 'i' &&
490 path[8] >= '0' && path[8] <= '9' ) {
491 snprintf(tmppath, sizeof(tmppath), "/dev/ubiblock%s", path + 8);
492 list_for_each_entry(pr, &devices, list)
493 if (!strcasecmp(pr->dev, tmppath))
494 return NULL;
495 }
496
497 return probe_path(path);
498 }
499
500 static int _cache_load(const char *path)
501 {
502 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
503 int j;
504 glob_t gl;
505
506 if (glob(path, gl_flags, NULL, &gl) < 0)
507 return -1;
508
509 for (j = 0; j < gl.gl_pathc; j++) {
510 struct probe_info *pr = _probe_path(gl.gl_pathv[j]);
511 if (pr)
512 list_add_tail(&pr->list, &devices);
513 }
514
515 globfree(&gl);
516
517 return 0;
518 }
519
520 static void cache_load(int mtd)
521 {
522 if (mtd) {
523 _cache_load("/dev/mtdblock*");
524 _cache_load("/dev/ubiblock*");
525 _cache_load("/dev/ubi[0-9]*");
526 }
527 _cache_load("/dev/loop*");
528 _cache_load("/dev/mmcblk*");
529 _cache_load("/dev/sd*");
530 _cache_load("/dev/hd*");
531 _cache_load("/dev/md*");
532 _cache_load("/dev/nvme*");
533 _cache_load("/dev/vd*");
534 _cache_load("/dev/xvd*");
535 _cache_load("/dev/mapper/*");
536 }
537
538
539 static int print_block_uci(struct probe_info *pr)
540 {
541 if (!strcmp(pr->type, "swap")) {
542 printf("config 'swap'\n");
543 } else {
544 printf("config 'mount'\n");
545 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
546 }
547 if (pr->uuid)
548 printf("\toption\tuuid\t'%s'\n", pr->uuid);
549 else
550 printf("\toption\tdevice\t'%s'\n", pr->dev);
551 printf("\toption\tenabled\t'0'\n\n");
552
553 return 0;
554 }
555
556 static struct probe_info* find_block_info(char *uuid, char *label, char *path)
557 {
558 struct probe_info *pr = NULL;
559
560 if (uuid)
561 list_for_each_entry(pr, &devices, list)
562 if (pr->uuid && !strcasecmp(pr->uuid, uuid))
563 return pr;
564
565 if (label)
566 list_for_each_entry(pr, &devices, list)
567 if (pr->label && !strcmp(pr->label, label))
568 return pr;
569
570 if (path)
571 list_for_each_entry(pr, &devices, list)
572 if (pr->dev && !strcmp(basename(pr->dev), basename(path)))
573 return pr;
574
575 return NULL;
576 }
577
578 static char* find_mount_point(char *block)
579 {
580 FILE *fp = fopen("/proc/self/mountinfo", "r");
581 static char line[256];
582 int len = strlen(block);
583 char *point = NULL, *pos, *tmp, *cpoint, *devname;
584 struct stat s;
585 int rstat;
586 unsigned int minor, major;
587
588 if (!fp)
589 return NULL;
590
591 rstat = stat(block, &s);
592
593 while (fgets(line, sizeof(line), fp)) {
594 pos = strchr(line, ' ');
595 if (!pos)
596 continue;
597
598 pos = strchr(pos + 1, ' ');
599 if (!pos)
600 continue;
601
602 tmp = ++pos;
603 pos = strchr(pos, ':');
604 if (!pos)
605 continue;
606
607 *pos = '\0';
608 major = atoi(tmp);
609 tmp = ++pos;
610 pos = strchr(pos, ' ');
611 if (!pos)
612 continue;
613
614 *pos = '\0';
615 minor = atoi(tmp);
616 pos = strchr(pos + 1, ' ');
617 if (!pos)
618 continue;
619 tmp = ++pos;
620
621 pos = strchr(pos, ' ');
622 if (!pos)
623 continue;
624 *pos = '\0';
625 cpoint = tmp;
626
627 pos = strchr(pos + 1, ' ');
628 if (!pos)
629 continue;
630
631 pos = strchr(pos + 1, ' ');
632 if (!pos)
633 continue;
634
635 pos = strchr(pos + 1, ' ');
636 if (!pos)
637 continue;
638
639 tmp = ++pos;
640 pos = strchr(pos, ' ');
641 if (!pos)
642 continue;
643
644 *pos = '\0';
645 devname = tmp;
646 if (!strncmp(block, devname, len)) {
647 point = strdup(cpoint);
648 break;
649 }
650
651 if (rstat)
652 continue;
653
654 if (!S_ISBLK(s.st_mode))
655 continue;
656
657 if (major == major(s.st_rdev) &&
658 minor == minor(s.st_rdev)) {
659 point = strdup(cpoint);
660 break;
661 }
662 }
663
664 fclose(fp);
665
666 return point;
667 }
668
669 static int print_block_info(struct probe_info *pr)
670 {
671 static char *mp;
672
673 mp = find_mount_point(pr->dev);
674 printf("%s:", pr->dev);
675 if (pr->uuid)
676 printf(" UUID=\"%s\"", pr->uuid);
677
678 if (pr->label)
679 printf(" LABEL=\"%s\"", pr->label);
680
681 if (pr->version)
682 printf(" VERSION=\"%s\"", pr->version);
683
684 if (mp) {
685 printf(" MOUNT=\"%s\"", mp);
686 free(mp);
687 }
688
689 printf(" TYPE=\"%s\"\n", pr->type);
690
691 return 0;
692 }
693
694 static void mkdir_p(char *dir)
695 {
696 char *l = strrchr(dir, '/');
697
698 if (l) {
699 *l = '\0';
700 mkdir_p(dir);
701 *l = '/';
702 mkdir(dir, 0755);
703 }
704 }
705
706 static void check_filesystem(struct probe_info *pr)
707 {
708 pid_t pid;
709 struct stat statbuf;
710 const char *e2fsck = "/usr/sbin/e2fsck";
711 const char *f2fsck = "/usr/sbin/fsck.f2fs";
712 const char *dosfsck = "/usr/sbin/dosfsck";
713 const char *btrfsck = "/usr/bin/btrfsck";
714 const char *ckfs;
715
716 /* UBIFS does not need stuff like fsck */
717 if (!strncmp(pr->type, "ubifs", 5))
718 return;
719
720 if (!strncmp(pr->type, "vfat", 4)) {
721 ckfs = dosfsck;
722 } else if (!strncmp(pr->type, "f2fs", 4)) {
723 ckfs = f2fsck;
724 } else if (!strncmp(pr->type, "ext", 3)) {
725 ckfs = e2fsck;
726 } else if (!strncmp(pr->type, "btrfs", 5)) {
727 ckfs = btrfsck;
728 } else {
729 ULOG_ERR("check_filesystem: %s is not supported\n", pr->type);
730 return;
731 }
732
733 if (stat(ckfs, &statbuf) < 0) {
734 ULOG_ERR("check_filesystem: %s not found\n", ckfs);
735 return;
736 }
737
738 pid = fork();
739 if (!pid) {
740 if(!strncmp(pr->type, "f2fs", 4)) {
741 execl(ckfs, ckfs, "-f", pr->dev, NULL);
742 exit(-1);
743 } else if(!strncmp(pr->type, "btrfs", 5)) {
744 execl(ckfs, ckfs, "--repair", pr->dev, NULL);
745 exit(-1);
746 } else {
747 execl(ckfs, ckfs, "-p", pr->dev, NULL);
748 exit(-1);
749 }
750 } else if (pid > 0) {
751 int status;
752
753 waitpid(pid, &status, 0);
754 if (WEXITSTATUS(status))
755 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
756 }
757 }
758
759 static void handle_swapfiles(bool on)
760 {
761 struct stat s;
762 struct mount *m;
763 struct probe_info *pr;
764
765 vlist_for_each_element(&mounts, m, node)
766 {
767 if (m->type != TYPE_SWAP || !m->target)
768 continue;
769
770 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
771 continue;
772
773 pr = _probe_path(m->target);
774
775 if (!pr)
776 continue;
777
778 if (!strcmp(pr->type, "swap")) {
779 if (on)
780 swapon(pr->dev, m->prio);
781 else
782 swapoff(pr->dev);
783 }
784
785 free(pr);
786 }
787 }
788
789 static void to_devnull(int fd)
790 {
791 int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY);
792
793 if (devnull >= 0)
794 dup2(devnull, fd);
795
796 if (devnull > STDERR_FILENO)
797 close(devnull);
798 }
799
800 static int exec_mount(const char *source, const char *target,
801 const char *fstype, const char *options)
802 {
803 pid_t pid;
804 struct stat s;
805 FILE *mount_fd;
806 int err, status, pfds[2];
807 char errmsg[128], cmd[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
808
809 snprintf(cmd, sizeof(cmd), "/sbin/mount.%s", fstype);
810
811 if (stat(cmd, &s) < 0 || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR)) {
812 ULOG_ERR("No \"mount.%s\" utility available\n", fstype);
813 return -1;
814 }
815
816 if (pipe(pfds) < 0)
817 return -1;
818
819 fcntl(pfds[0], F_SETFD, fcntl(pfds[0], F_GETFD) | FD_CLOEXEC);
820 fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
821
822 pid = vfork();
823
824 switch (pid) {
825 case -1:
826 close(pfds[0]);
827 close(pfds[1]);
828
829 return -1;
830
831 case 0:
832 to_devnull(STDIN_FILENO);
833 to_devnull(STDOUT_FILENO);
834
835 dup2(pfds[1], STDERR_FILENO);
836 close(pfds[0]);
837 close(pfds[1]);
838
839 if (options && *options)
840 execl(cmd, cmd, "-o", options, source, target, NULL);
841 else
842 execl(cmd, cmd, source, target, NULL);
843
844 return -1;
845
846 default:
847 close(pfds[1]);
848
849 mount_fd = fdopen(pfds[0], "r");
850
851 while (fgets(errmsg, sizeof(errmsg), mount_fd))
852 ULOG_ERR("mount.%s: %s", fstype, errmsg);
853
854 fclose(mount_fd);
855
856 err = waitpid(pid, &status, 0);
857
858 if (err != -1) {
859 if (status != 0) {
860 ULOG_ERR("mount.%s: failed with status %d\n", fstype, status);
861 errno = EINVAL;
862 err = -1;
863 } else {
864 errno = 0;
865 err = 0;
866 }
867 }
868
869 break;
870 }
871
872 return err;
873 }
874
875 static int handle_mount(const char *source, const char *target,
876 const char *fstype, struct mount *m)
877 {
878 int i, err;
879 size_t mount_opts_len;
880 char *mount_opts = NULL, *ptr;
881
882 err = mount(source, target, fstype, m ? m->flags : 0,
883 (m && m->options) ? m->options : "");
884
885 /* Requested file system type is not available in kernel,
886 attempt to call mount helper. */
887 if (err == -1 && errno == ENODEV) {
888 if (m) {
889 /* Convert mount flags back into string representation,
890 first calculate needed length of string buffer... */
891 mount_opts_len = 1 + (m->options ? strlen(m->options) : 0);
892
893 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
894 if ((mount_flags[i].flag > 0) &&
895 (mount_flags[i].flag < INT_MAX) &&
896 (m->flags & (uint32_t)mount_flags[i].flag))
897 mount_opts_len += strlen(mount_flags[i].name) + 1;
898
899 /* ... then now allocate and fill it ... */
900 ptr = mount_opts = calloc(1, mount_opts_len);
901
902 if (!ptr) {
903 errno = ENOMEM;
904 return -1;
905 }
906
907 if (m->options)
908 ptr += sprintf(ptr, "%s,", m->options);
909
910 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
911 if ((mount_flags[i].flag > 0) &&
912 (mount_flags[i].flag < INT_MAX) &&
913 (m->flags & (uint32_t)mount_flags[i].flag))
914 ptr += sprintf(ptr, "%s,", mount_flags[i].name);
915
916 mount_opts[mount_opts_len - 1] = 0;
917 }
918
919 /* ... and now finally invoke the external mount program */
920 err = exec_mount(source, target, fstype, mount_opts);
921 }
922
923 return err;
924 }
925
926 static void blockd_notify(char *device, struct mount *m, struct probe_info *pr)
927 {
928 struct ubus_context *ctx = ubus_connect(NULL);
929 uint32_t id;
930
931 if (!ctx)
932 return;
933
934 if (!ubus_lookup_id(ctx, "block", &id)) {
935 struct blob_buf buf = { 0 };
936 char *d = strrchr(device, '/');
937
938 if (d)
939 d++;
940 else
941 d = device;
942
943 blob_buf_init(&buf, 0);
944
945 if (m) {
946
947 blobmsg_add_string(&buf, "device", d);
948 if (m->uuid)
949 blobmsg_add_string(&buf, "uuid", m->uuid);
950 if (m->label)
951 blobmsg_add_string(&buf, "label", m->label);
952 if (m->target)
953 blobmsg_add_string(&buf, "target", m->target);
954 if (m->options)
955 blobmsg_add_string(&buf, "options", m->options);
956 if (m->autofs)
957 blobmsg_add_u32(&buf, "autofs", m->autofs);
958 if (pr->type)
959 blobmsg_add_string(&buf, "type", pr->type);
960 if (pr->version)
961 blobmsg_add_string(&buf, "version", pr->version);
962 } else if (pr) {
963 blobmsg_add_string(&buf, "device", d);
964 if (pr->uuid)
965 blobmsg_add_string(&buf, "uuid", pr->uuid);
966 if (pr->label)
967 blobmsg_add_string(&buf, "label", pr->label);
968 if (pr->type)
969 blobmsg_add_string(&buf, "type", pr->type);
970 if (pr->version)
971 blobmsg_add_string(&buf, "version", pr->version);
972 blobmsg_add_u32(&buf, "anon", 1);
973 } else {
974 blobmsg_add_string(&buf, "device", d);
975 blobmsg_add_u32(&buf, "remove", 1);
976 }
977
978 ubus_invoke(ctx, id, "hotplug", buf.head, NULL, NULL, 3000);
979 }
980
981 ubus_free(ctx);
982 }
983
984 static int mount_device(struct probe_info *pr, int type)
985 {
986 struct mount *m;
987 char *device;
988 char *mp;
989
990 if (!pr)
991 return -1;
992
993 device = basename(pr->dev);
994
995 if (!strcmp(pr->type, "swap")) {
996 if ((type == TYPE_HOTPLUG) && !auto_swap)
997 return -1;
998 m = find_swap(pr->uuid, pr->label, device);
999 if (m || anon_swap)
1000 swapon(pr->dev, (m) ? (m->prio) : (0));
1001
1002 return 0;
1003 }
1004
1005 mp = find_mount_point(pr->dev);
1006 if (mp && (type != TYPE_HOTPLUG)) {
1007 ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
1008 free(mp);
1009 return -1;
1010 }
1011
1012 m = find_block(pr->uuid, pr->label, device, NULL);
1013 if (m && m->extroot)
1014 return -1;
1015
1016 if (m) switch (type) {
1017 case TYPE_HOTPLUG:
1018 blockd_notify(device, m, pr);
1019 if (m->autofs)
1020 return 0;
1021 if (!auto_mount)
1022 return -1;
1023 break;
1024 case TYPE_AUTOFS:
1025 if (!m->autofs)
1026 return -1;
1027 break;
1028 case TYPE_DEV:
1029 if (m->autofs)
1030 return -1;
1031 break;
1032 } else if (type == TYPE_HOTPLUG) {
1033 blockd_notify(device, NULL, pr);
1034 }
1035
1036 if (m) {
1037 char *target = m->target;
1038 char _target[32];
1039 int err = 0;
1040
1041 if (m->autofs) {
1042 snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
1043 target = _target;
1044 }
1045 if (!target) {
1046 snprintf(_target, sizeof(_target), "/mnt/%s", device);
1047 target = _target;
1048 }
1049 mkdir_p(target);
1050
1051 if (check_fs)
1052 check_filesystem(pr);
1053
1054 err = handle_mount(pr->dev, target, pr->type, m);
1055 if (err)
1056 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1057 pr->dev, pr->type, target, errno);
1058 else
1059 handle_swapfiles(true);
1060 return err;
1061 }
1062
1063 if (anon_mount) {
1064 char target[32];
1065 int err = 0;
1066
1067 snprintf(target, sizeof(target), "/mnt/%s", device);
1068 mkdir_p(target);
1069
1070 if (check_fs)
1071 check_filesystem(pr);
1072
1073 err = handle_mount(pr->dev, target, pr->type, NULL);
1074 if (err)
1075 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1076 pr->dev, pr->type, target, errno);
1077 else
1078 handle_swapfiles(true);
1079 return err;
1080 }
1081
1082 return 0;
1083 }
1084
1085 static int umount_device(struct probe_info *pr)
1086 {
1087 struct mount *m;
1088 char *device = basename(pr->dev);
1089 char *mp;
1090 int err;
1091
1092 if (!pr)
1093 return -1;
1094
1095 if (!strcmp(pr->type, "swap"))
1096 return -1;
1097
1098 mp = find_mount_point(pr->dev);
1099 if (!mp)
1100 return -1;
1101
1102 m = find_block(pr->uuid, pr->label, device, NULL);
1103 if (m && m->extroot)
1104 return -1;
1105
1106 err = umount2(mp, MNT_DETACH);
1107 if (err)
1108 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n",
1109 pr->dev, mp, errno);
1110 else
1111 ULOG_INFO("unmounted %s (%s)\n",
1112 pr->dev, mp);
1113
1114 free(mp);
1115 return err;
1116 }
1117
1118 static int mount_action(char *action, char *device, int type)
1119 {
1120 char path[32];
1121 char *mount_point;
1122
1123 if (!action || !device)
1124 return -1;
1125 snprintf(path, sizeof(path), "/dev/%s", device);
1126
1127 if (!strcmp(action, "remove")) {
1128 int err = 0;
1129
1130 if (type == TYPE_HOTPLUG)
1131 blockd_notify(device, NULL, NULL);
1132
1133 mount_point = find_mount_point(path);
1134 if (mount_point)
1135 err = umount2(mount_point, MNT_DETACH);
1136
1137 if (err)
1138 ULOG_ERR("umount of %s failed (%d) - %m\n",
1139 mount_point, errno);
1140
1141 free(mount_point);
1142 return 0;
1143 } else if (strcmp(action, "add")) {
1144 ULOG_ERR("Unkown action %s\n", action);
1145
1146 return -1;
1147 }
1148
1149 if (config_load(NULL))
1150 return -1;
1151 cache_load(0);
1152
1153 return mount_device(find_block_info(NULL, NULL, path), type);
1154 }
1155
1156 static int main_hotplug(int argc, char **argv)
1157 {
1158 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG);
1159 }
1160
1161 static int main_autofs(int argc, char **argv)
1162 {
1163 if (argc < 3)
1164 return -1;
1165
1166 if (!strcmp(argv[2], "start")) {
1167 struct probe_info *pr;
1168
1169 if (config_load(NULL))
1170 return -1;
1171
1172 cache_load(0);
1173 list_for_each_entry(pr, &devices, list) {
1174 struct mount *m = find_block(pr->uuid, pr->label, NULL, NULL);
1175
1176 if (m && m->autofs)
1177 mount_device(pr, TYPE_HOTPLUG);
1178 else
1179 blockd_notify(pr->dev, m, pr);
1180 }
1181 return 0;
1182 }
1183 return mount_action(argv[2], argv[3], TYPE_AUTOFS);
1184 }
1185
1186 static int find_block_mtd(char *name, char *part, int plen)
1187 {
1188 FILE *fp = fopen("/proc/mtd", "r");
1189 static char line[256];
1190 char *index = NULL;
1191
1192 if(!fp)
1193 return -1;
1194
1195 while (!index && fgets(line, sizeof(line), fp)) {
1196 if (strstr(line, name)) {
1197 char *eol = strstr(line, ":");
1198
1199 if (!eol)
1200 continue;
1201
1202 *eol = '\0';
1203 index = &line[3];
1204 }
1205 }
1206
1207 fclose(fp);
1208
1209 if (!index)
1210 return -1;
1211
1212 snprintf(part, plen, "/dev/mtdblock%s", index);
1213
1214 return 0;
1215 }
1216
1217 #ifdef UBIFS_EXTROOT
1218 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
1219 {
1220 int dev = 0;
1221
1222 while (ubi_dev_present(libubi, dev))
1223 {
1224 struct ubi_dev_info dev_info;
1225 struct ubi_vol_info vol_info;
1226
1227 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
1228 continue;
1229 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
1230 continue;
1231
1232 *dev_num = dev_info.dev_num;
1233 *vol_id = vol_info.vol_id;
1234
1235 return 0;
1236 }
1237
1238 return -1;
1239 }
1240
1241 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
1242 {
1243 int dev_num;
1244 int vol_id;
1245 int err = -1;
1246
1247 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1248 if (!err)
1249 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
1250
1251 return err;
1252 }
1253
1254 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
1255 {
1256 int dev_num;
1257 int vol_id;
1258 int err = -1;
1259
1260 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1261 if (!err)
1262 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
1263
1264 return err;
1265 }
1266
1267 #else
1268
1269 static int find_root_dev(char *buf, int len)
1270 {
1271 DIR *d;
1272 dev_t root;
1273 struct stat s;
1274 struct dirent *e;
1275
1276 if (stat("/", &s))
1277 return -1;
1278
1279 if (!(d = opendir("/dev")))
1280 return -1;
1281
1282 root = s.st_dev;
1283
1284 while ((e = readdir(d)) != NULL) {
1285 snprintf(buf, len, "/dev/%s", e->d_name);
1286
1287 if (stat(buf, &s) || s.st_rdev != root)
1288 continue;
1289
1290 closedir(d);
1291 return 0;
1292 }
1293
1294 closedir(d);
1295 return -1;
1296 }
1297
1298 #endif
1299
1300 static int test_fs_support(const char *name)
1301 {
1302 char line[128], *p;
1303 int rv = -1;
1304 FILE *f;
1305
1306 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
1307 while (fgets(line, sizeof(line), f)) {
1308 p = strtok(line, "\t\n");
1309
1310 if (p && !strcmp(p, "nodev"))
1311 p = strtok(NULL, "\t\n");
1312
1313 if (p && !strcmp(p, name)) {
1314 rv = 0;
1315 break;
1316 }
1317 }
1318 fclose(f);
1319 }
1320
1321 return rv;
1322 }
1323
1324 static int check_extroot(char *path)
1325 {
1326 struct probe_info *pr = NULL;
1327 char devpath[32];
1328
1329 #ifdef UBIFS_EXTROOT
1330 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1331 int err = -1;
1332 libubi_t libubi;
1333
1334 libubi = libubi_open();
1335 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1336 libubi_close(libubi);
1337 if (err)
1338 return -1;
1339 }
1340 #else
1341 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1342 if (find_root_dev(devpath, sizeof(devpath))) {
1343 ULOG_ERR("extroot: unable to determine root device\n");
1344 return -1;
1345 }
1346 }
1347 #endif
1348
1349 list_for_each_entry(pr, &devices, list) {
1350 if (!strcmp(pr->dev, devpath)) {
1351 struct stat s;
1352 FILE *fp = NULL;
1353 char tag[64];
1354 char uuid[64] = { 0 };
1355
1356 snprintf(tag, sizeof(tag), "%s/etc", path);
1357 if (stat(tag, &s))
1358 mkdir_p(tag);
1359
1360 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1361 if (stat(tag, &s)) {
1362 fp = fopen(tag, "w+");
1363 if (!fp) {
1364 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1365 tag, errno);
1366 /* return 0 to continue boot regardless of error */
1367 return 0;
1368 }
1369 fputs(pr->uuid, fp);
1370 fclose(fp);
1371 return 0;
1372 }
1373
1374 fp = fopen(tag, "r");
1375 if (!fp) {
1376 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1377 tag, errno);
1378 return -1;
1379 }
1380
1381 if (!fgets(uuid, sizeof(uuid), fp))
1382 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1383 tag, errno);
1384 fclose(fp);
1385
1386 if (*uuid && !strcasecmp(uuid, pr->uuid))
1387 return 0;
1388
1389 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1390 pr->uuid, basename(path), uuid);
1391 return -1;
1392 }
1393 }
1394
1395 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1396 return -1;
1397 }
1398
1399 /*
1400 * Read info about extroot from UCI (using prefix) and mount it.
1401 */
1402 static int mount_extroot(char *cfg)
1403 {
1404 char overlay[] = "/tmp/extroot/overlay";
1405 char mnt[] = "/tmp/extroot/mnt";
1406 char *path = mnt;
1407 struct probe_info *pr;
1408 struct mount *m;
1409 int err = -1;
1410
1411 /* Load @cfg/etc/config/fstab */
1412 if (config_load(cfg))
1413 return -2;
1414
1415 /* See if there is extroot-specific mount config */
1416 m = find_block(NULL, NULL, NULL, "/");
1417 if (!m)
1418 m = find_block(NULL, NULL, NULL, "/overlay");
1419
1420 if (!m || !m->extroot)
1421 {
1422 ULOG_INFO("extroot: not configured\n");
1423 return -1;
1424 }
1425
1426 /* Find block device pointed by the mount config */
1427 pr = find_block_info(m->uuid, m->label, m->device);
1428
1429 if (!pr && delay_root){
1430 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1431 sleep(delay_root);
1432 make_devs();
1433 cache_load(0);
1434 pr = find_block_info(m->uuid, m->label, m->device);
1435 }
1436 if (pr) {
1437 if (strncmp(pr->type, "ext", 3) &&
1438 strncmp(pr->type, "f2fs", 4) &&
1439 strncmp(pr->type, "btrfs", 5) &&
1440 strncmp(pr->type, "ubifs", 5)) {
1441 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs or ubifs\n", pr->type);
1442 return -1;
1443 }
1444
1445 if (test_fs_support(pr->type)) {
1446 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->type);
1447 return -1;
1448 }
1449
1450 if (m->overlay)
1451 path = overlay;
1452 mkdir_p(path);
1453
1454 if (check_fs)
1455 check_filesystem(pr);
1456
1457 err = mount(pr->dev, path, pr->type, m->flags,
1458 (m->options) ? (m->options) : (""));
1459
1460 if (err) {
1461 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1462 pr->dev, pr->type, path, errno);
1463 } else if (m->overlay) {
1464 err = check_extroot(path);
1465 if (err)
1466 umount(path);
1467 }
1468 } else {
1469 ULOG_ERR("extroot: cannot find device %s%s\n",
1470 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1471 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1472 }
1473
1474 return err;
1475 }
1476
1477 static int main_extroot(int argc, char **argv)
1478 {
1479 struct probe_info *pr;
1480 char blkdev_path[32] = { 0 };
1481 int err = -1;
1482 #ifdef UBIFS_EXTROOT
1483 libubi_t libubi;
1484 #endif
1485
1486 if (!getenv("PREINIT"))
1487 return -1;
1488
1489 if (argc != 2) {
1490 ULOG_ERR("Usage: block extroot\n");
1491 return -1;
1492 }
1493
1494 make_devs();
1495 cache_load(1);
1496
1497 /* enable LOG_INFO messages */
1498 ulog_threshold(LOG_INFO);
1499
1500 /*
1501 * Look for "rootfs_data". We will want to mount it and check for
1502 * extroot configuration.
1503 */
1504
1505 /* Start with looking for MTD partition */
1506 find_block_mtd("\"rootfs_data\"", blkdev_path, sizeof(blkdev_path));
1507 if (blkdev_path[0]) {
1508 pr = find_block_info(NULL, NULL, blkdev_path);
1509 if (pr && !strcmp(pr->type, "jffs2")) {
1510 char cfg[] = "/tmp/jffs_cfg";
1511
1512 /*
1513 * Mount MTD part and try extroot (using
1514 * /etc/config/fstab from that partition)
1515 */
1516 mkdir_p(cfg);
1517 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1518 err = mount_extroot(cfg);
1519 umount2(cfg, MNT_DETACH);
1520 }
1521 if (err < 0)
1522 rmdir("/tmp/overlay");
1523 rmdir(cfg);
1524 return err;
1525 }
1526 }
1527
1528 #ifdef UBIFS_EXTROOT
1529 /* ... but it also could be an UBI volume */
1530 memset(blkdev_path, 0, sizeof(blkdev_path));
1531 libubi = libubi_open();
1532 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1533 libubi_close(libubi);
1534 if (blkdev_path[0]) {
1535 char cfg[] = "/tmp/ubifs_cfg";
1536
1537 /* Mount volume and try extroot (using fstab from that vol) */
1538 mkdir_p(cfg);
1539 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1540 err = mount_extroot(cfg);
1541 umount2(cfg, MNT_DETACH);
1542 }
1543 if (err < 0)
1544 rmdir("/tmp/overlay");
1545 rmdir(cfg);
1546 return err;
1547 }
1548 #endif
1549
1550 return mount_extroot(NULL);
1551 }
1552
1553 static int main_mount(int argc, char **argv)
1554 {
1555 struct probe_info *pr;
1556
1557 if (config_load(NULL))
1558 return -1;
1559
1560 cache_load(1);
1561 list_for_each_entry(pr, &devices, list)
1562 mount_device(pr, TYPE_DEV);
1563
1564 handle_swapfiles(true);
1565
1566 return 0;
1567 }
1568
1569 static int main_umount(int argc, char **argv)
1570 {
1571 struct probe_info *pr;
1572
1573 if (config_load(NULL))
1574 return -1;
1575
1576 handle_swapfiles(false);
1577
1578 cache_load(0);
1579 list_for_each_entry(pr, &devices, list)
1580 umount_device(pr);
1581
1582 return 0;
1583 }
1584
1585 static int main_detect(int argc, char **argv)
1586 {
1587 struct probe_info *pr;
1588
1589 cache_load(0);
1590 printf("config 'global'\n");
1591 printf("\toption\tanon_swap\t'0'\n");
1592 printf("\toption\tanon_mount\t'0'\n");
1593 printf("\toption\tauto_swap\t'1'\n");
1594 printf("\toption\tauto_mount\t'1'\n");
1595 printf("\toption\tdelay_root\t'5'\n");
1596 printf("\toption\tcheck_fs\t'0'\n\n");
1597 list_for_each_entry(pr, &devices, list)
1598 print_block_uci(pr);
1599
1600 return 0;
1601 }
1602
1603 static int main_info(int argc, char **argv)
1604 {
1605 int i;
1606 struct probe_info *pr;
1607
1608 cache_load(1);
1609 if (argc == 2) {
1610 list_for_each_entry(pr, &devices, list)
1611 print_block_info(pr);
1612
1613 return 0;
1614 };
1615
1616 for (i = 2; i < argc; i++) {
1617 struct stat s;
1618
1619 if (stat(argv[i], &s)) {
1620 ULOG_ERR("failed to stat %s\n", argv[i]);
1621 continue;
1622 }
1623 if (!S_ISBLK(s.st_mode) && !(S_ISCHR(s.st_mode) && major(s.st_rdev) == 250)) {
1624 ULOG_ERR("%s is not a block device\n", argv[i]);
1625 continue;
1626 }
1627 pr = find_block_info(NULL, NULL, argv[i]);
1628 if (pr)
1629 print_block_info(pr);
1630 }
1631
1632 return 0;
1633 }
1634
1635 static int swapon_usage(void)
1636 {
1637 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1638 "\tStart swapping on [DEVICE]\n"
1639 " -a\tStart swapping on all swap devices\n"
1640 " -p pri\tSet priority of swap device\n"
1641 " -s\tShow summary\n");
1642 return -1;
1643 }
1644
1645 static int main_swapon(int argc, char **argv)
1646 {
1647 int ch;
1648 FILE *fp;
1649 char *lineptr;
1650 size_t s;
1651 struct probe_info *pr;
1652 int flags = 0;
1653 int pri;
1654 struct stat st;
1655 int err;
1656
1657 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1658 switch(ch) {
1659 case 's':
1660 fp = fopen("/proc/swaps", "r");
1661 lineptr = NULL;
1662
1663 if (!fp) {
1664 ULOG_ERR("failed to open /proc/swaps\n");
1665 return -1;
1666 }
1667 while (getline(&lineptr, &s, fp) > 0)
1668 printf("%s", lineptr);
1669 if (lineptr)
1670 free(lineptr);
1671 fclose(fp);
1672 return 0;
1673 case 'a':
1674 cache_load(0);
1675 list_for_each_entry(pr, &devices, list) {
1676 if (strcmp(pr->type, "swap"))
1677 continue;
1678 if (swapon(pr->dev, 0))
1679 ULOG_ERR("failed to swapon %s\n", pr->dev);
1680 }
1681 return 0;
1682 case 'p':
1683 pri = atoi(optarg);
1684 if (pri >= 0)
1685 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1686 break;
1687 default:
1688 return swapon_usage();
1689 }
1690
1691 }
1692
1693 if (optind != (argc - 1))
1694 return swapon_usage();
1695
1696 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1697 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1698 return -1;
1699 }
1700 err = swapon(argv[optind], flags);
1701 if (err) {
1702 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1703 return err;
1704 }
1705
1706 return 0;
1707 }
1708
1709 static int main_swapoff(int argc, char **argv)
1710 {
1711 if (argc != 2) {
1712 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1713 "\tStop swapping on DEVICE\n"
1714 " -a\tStop swapping on all swap devices\n");
1715 return -1;
1716 }
1717
1718 if (!strcmp(argv[1], "-a")) {
1719 FILE *fp = fopen("/proc/swaps", "r");
1720 char line[256];
1721
1722 if (!fp) {
1723 ULOG_ERR("failed to open /proc/swaps\n");
1724 return -1;
1725 }
1726 if (fgets(line, sizeof(line), fp))
1727 while (fgets(line, sizeof(line), fp)) {
1728 char *end = strchr(line, ' ');
1729 int err;
1730
1731 if (!end)
1732 continue;
1733 *end = '\0';
1734 err = swapoff(line);
1735 if (err)
1736 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1737 }
1738 fclose(fp);
1739 } else {
1740 struct stat s;
1741 int err;
1742
1743 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1744 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1745 return -1;
1746 }
1747 err = swapoff(argv[1]);
1748 if (err) {
1749 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1750 return err;
1751 }
1752 }
1753
1754 return 0;
1755 }
1756
1757 int main(int argc, char **argv)
1758 {
1759 char *base = basename(*argv);
1760
1761 umask(0);
1762
1763 ulog_open(-1, -1, "block");
1764 ulog_threshold(LOG_NOTICE);
1765
1766 if (!strcmp(base, "swapon"))
1767 return main_swapon(argc, argv);
1768
1769 if (!strcmp(base, "swapoff"))
1770 return main_swapoff(argc, argv);
1771
1772 if ((argc > 1) && !strcmp(base, "block")) {
1773 if (!strcmp(argv[1], "info"))
1774 return main_info(argc, argv);
1775
1776 if (!strcmp(argv[1], "detect"))
1777 return main_detect(argc, argv);
1778
1779 if (!strcmp(argv[1], "hotplug"))
1780 return main_hotplug(argc, argv);
1781
1782 if (!strcmp(argv[1], "autofs"))
1783 return main_autofs(argc, argv);
1784
1785 if (!strcmp(argv[1], "extroot"))
1786 return main_extroot(argc, argv);
1787
1788 if (!strcmp(argv[1], "mount"))
1789 return main_mount(argc, argv);
1790
1791 if (!strcmp(argv[1], "umount"))
1792 return main_umount(argc, argv);
1793
1794 if (!strcmp(argv[1], "remount")) {
1795 int ret = main_umount(argc, argv);
1796
1797 if (!ret)
1798 ret = main_mount(argc, argv);
1799 return ret;
1800 }
1801 }
1802
1803 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");
1804
1805 return -1;
1806 }