btrfs-progs: image: introduce symbolic names for the sanitization modes
[platform/upstream/btrfs-progs.git] / cmds-scrub.c
index ea6ffc9..5388fdc 100644 (file)
@@ -44,6 +44,7 @@
 #include "disk-io.h"
 
 #include "commands.h"
+#include "help.h"
 
 static const char * const scrub_cmd_group_usage[] = {
        "btrfs scrub <command> [options] <path>|<device>",
@@ -430,7 +431,7 @@ static int scrub_rename_file(const char *fn_base, const char *fn_local,
 /*
  * returns 0 if the key did not match (nothing was read)
  *         1 if the key did match (success)
- *        -1 if the key did match and an error occured
+ *        -1 if the key did match and an error occurred
  */
 static int scrub_kvread(int *i, int len, int avail, const char *buf,
                        const char *key, u64 *dest)
@@ -467,7 +468,7 @@ static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
 {
        int avail = 0;
        int old_avail = 0;
-       char l[16 * 1024];
+       char l[SZ_16K];
        int state = 0;
        int curr = -1;
        int i = 0;
@@ -481,7 +482,10 @@ static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
 
 again:
        old_avail = avail - i;
-       BUG_ON(old_avail < 0);
+       if (old_avail < 0) {
+               error("scrub record file corrupted near byte %d", i);
+               return ERR_PTR(-EINVAL);
+       }
        if (old_avail)
                memmove(l, l + i, old_avail);
        avail = read(fd, l + old_avail, sizeof(l) - old_avail);
@@ -650,7 +654,9 @@ skip:
                        } while (i < avail);
                        continue;
                }
-               BUG();
+               error("internal error: unknown parser state %d near byte %d",
+                               state, i);
+               return ERR_PTR(-EINVAL);
        }
        goto again;
 }
@@ -1141,7 +1147,6 @@ static int scrub_start(int argc, char **argv, int resume)
        int force = 0;
        int nothing_to_resume = 0;
 
-       optind = 1;
        while ((c = getopt(argc, argv, "BdqrRc:n:f")) != -1) {
                switch (c) {
                case 'B':
@@ -1198,17 +1203,9 @@ static int scrub_start(int argc, char **argv, int resume)
 
        path = argv[optind];
 
-       fdmnt = open_path_or_dev_mnt(path, &dirstream);
-
-       if (fdmnt < 0) {
-               if (errno == EINVAL)
-                       error_on(!do_quiet, "'%s' is not a mounted btrfs device",
-                               path);
-               else
-                       error_on(!do_quiet, "can't access '%s': %s",
-                               path, strerror(errno));
+       fdmnt = open_path_or_dev_mnt(path, &dirstream, !do_quiet);
+       if (fdmnt < 0)
                return 1;
-       }
 
        ret = get_fs_info(path, &fi_args, &di_args);
        if (ret) {
@@ -1599,17 +1596,15 @@ static int cmd_scrub_cancel(int argc, char **argv)
        int fdmnt = -1;
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 2))
+       clean_args_no_options(argc, argv, cmd_scrub_cancel_usage);
+
+       if (check_argc_exact(argc - optind, 1))
                usage(cmd_scrub_cancel_usage);
 
-       path = argv[1];
+       path = argv[optind];
 
-       fdmnt = open_path_or_dev_mnt(path, &dirstream);
+       fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
        if (fdmnt < 0) {
-               if (errno == EINVAL)
-                       error("'%s' is not a mounted btrfs device", path);
-               else
-                       error("can't access '%s': %s", path, strerror(errno));
                ret = 1;
                goto out;
        }
@@ -1685,7 +1680,6 @@ static int cmd_scrub_status(int argc, char **argv)
        int err = 0;
        DIR *dirstream = NULL;
 
-       optind = 1;
        while ((c = getopt(argc, argv, "dR")) != -1) {
                switch (c) {
                case 'd':
@@ -1705,15 +1699,9 @@ static int cmd_scrub_status(int argc, char **argv)
 
        path = argv[optind];
 
-       fdmnt = open_path_or_dev_mnt(path, &dirstream);
-
-       if (fdmnt < 0) {
-               if (errno == EINVAL)
-                       error("'%s' is not a mounted btrfs device", path);
-               else
-                       error("can't access '%s': %s", path, strerror(errno));
+       fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
+       if (fdmnt < 0)
                return 1;
-       }
 
        ret = get_fs_info(path, &fi_args, &di_args);
        if (ret) {