btrfs-progs: fsck-test: Add new image with shared block ref only metadata backref
[platform/upstream/btrfs-progs.git] / cmds-scrub.c
index 4a1d475..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>",
@@ -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':
@@ -1675,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':