]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
e67924582799404c8259a9aeb988faa7b6126268
[bcachefs-tools-debian] / cmd_fsck.c
1
2 #include "cmds.h"
3 #include "libbcachefs/error.h"
4 #include "libbcachefs.h"
5 #include "libbcachefs/super.h"
6 #include "tools-util.h"
7
8 static void usage(void)
9 {
10         puts("bcachefs fsck - filesystem check and repair\n"
11              "Usage: bcachefs fsck [OPTION]... <devices>\n"
12              "\n"
13              "Options:\n"
14              "  -p     Automatic repair (no questions)\n"
15              "  -n     Don't repair, only check for errors\n"
16              "  -y     Assume \"yes\" to all questions\n"
17              "  -f     Force checking even if filesystem is marked clean\n"
18              "  -v     Be verbose\n"
19              " --h     Display this help and exit\n"
20              "Report bugs to <linux-bcache@vger.kernel.org>");
21 }
22
23 int cmd_fsck(int argc, char *argv[])
24 {
25         struct bch_opts opts = bch2_opts_empty();
26         unsigned i;
27         int opt, ret = 0;
28
29         opt_set(opts, degraded, true);
30         opt_set(opts, fsck, true);
31         opt_set(opts, fix_errors, FSCK_OPT_ASK);
32
33         while ((opt = getopt(argc, argv, "apynfo:vh")) != -1)
34                 switch (opt) {
35                 case 'a': /* outdated alias for -p */
36                 case 'p':
37                         opt_set(opts, fix_errors, FSCK_OPT_YES);
38                         break;
39                 case 'y':
40                         opt_set(opts, fix_errors, FSCK_OPT_YES);
41                         break;
42                 case 'n':
43                         opt_set(opts, nochanges, true);
44                         opt_set(opts, fix_errors, FSCK_OPT_NO);
45                         break;
46                 case 'f':
47                         /* force check, even if filesystem marked clean: */
48                         break;
49                 case 'o':
50                         ret = bch2_parse_mount_opts(&opts, optarg);
51                         if (ret)
52                                 return ret;
53                         break;
54                 case 'v':
55                         opt_set(opts, verbose, true);
56                         break;
57                 case 'h':
58                         usage();
59                         exit(EXIT_SUCCESS);
60                 }
61         args_shift(optind);
62
63         if (!argc)
64                 die("Please supply device(s) to check");
65
66         for (i = 0; i < argc; i++)
67                 if (dev_mounted_rw(argv[i]))
68                         die("%s is mounted read-write - aborting", argv[i]);
69
70         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
71         if (IS_ERR(c))
72                 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
73
74         if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags))
75                 ret = 2;
76         if (test_bit(BCH_FS_ERROR, &c->flags))
77                 ret = 4;
78
79         bch2_fs_stop(c);
80         return ret;
81 }