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