4 #include "libbcachefs/error.h"
5 #include "libbcachefs.h"
6 #include "libbcachefs/super.h"
7 #include "tools-util.h"
9 static void usage(void)
11 puts("bcachefs fsck - filesystem check and repair\n"
12 "Usage: bcachefs fsck [OPTION]... <devices>\n"
15 " -p Automatic repair (no questions)\n"
16 " -n Don't repair, only check for errors\n"
17 " -y Assume \"yes\" to all questions\n"
18 " -f Force checking even if filesystem is marked clean\n"
19 " --reconstruct_alloc Reconstruct the alloc btree\n"
21 " --h Display this help and exit\n"
22 "Report bugs to <linux-bcachefs@vger.kernel.org>");
25 int cmd_fsck(int argc, char *argv[])
27 static const struct option longopts[] = {
28 { "reconstruct_alloc", no_argument, NULL, 'R' },
31 struct bch_opts opts = bch2_opts_empty();
35 opt_set(opts, degraded, true);
36 opt_set(opts, fsck, true);
37 opt_set(opts, fix_errors, FSCK_OPT_ASK);
39 while ((opt = getopt_long(argc, argv,
41 longopts, NULL)) != -1)
43 case 'a': /* outdated alias for -p */
45 opt_set(opts, fix_errors, FSCK_OPT_YES);
48 opt_set(opts, fix_errors, FSCK_OPT_YES);
51 opt_set(opts, nochanges, true);
52 opt_set(opts, fix_errors, FSCK_OPT_NO);
55 /* force check, even if filesystem marked clean: */
58 ret = bch2_parse_mount_opts(&opts, optarg);
63 opt_set(opts, reconstruct_alloc, true);
66 opt_set(opts, verbose, true);
75 die("Please supply device(s) to check");
77 for (i = 0; i < argc; i++)
78 if (dev_mounted_rw(argv[i]))
79 die("%s is mounted read-write - aborting", argv[i]);
81 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
83 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
85 if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags))
87 if (test_bit(BCH_FS_ERROR, &c->flags))