]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
New upstream snapshot
[bcachefs-tools-debian] / cmd_fsck.c
1
2 #include <getopt.h>
3 #include "cmds.h"
4 #include "libbcachefs/error.h"
5 #include "libbcachefs.h"
6 #include "libbcachefs/super.h"
7 #include "tools-util.h"
8
9 static void usage(void)
10 {
11         puts("bcachefs fsck - filesystem check and repair\n"
12              "Usage: bcachefs fsck [OPTION]... <devices>\n"
13              "\n"
14              "Options:\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"
20              "  -v                     Be verbose\n"
21              "  -h                     Display this help and exit\n"
22              "Report bugs to <linux-bcachefs@vger.kernel.org>");
23 }
24
25 int cmd_fsck(int argc, char *argv[])
26 {
27         static const struct option longopts[] = {
28                 { "reconstruct_alloc",  no_argument,            NULL, 'R' },
29                 { NULL }
30         };
31         struct bch_opts opts = bch2_opts_empty();
32         unsigned i;
33         int opt, ret = 0;
34
35         opt_set(opts, degraded, true);
36         opt_set(opts, fsck, true);
37         opt_set(opts, fix_errors, FSCK_OPT_ASK);
38
39         while ((opt = getopt_long(argc, argv,
40                                   "apynfo:vh",
41                                   longopts, NULL)) != -1)
42                 switch (opt) {
43                 case 'a': /* outdated alias for -p */
44                 case 'p':
45                         opt_set(opts, fix_errors, FSCK_OPT_YES);
46                         break;
47                 case 'y':
48                         opt_set(opts, fix_errors, FSCK_OPT_YES);
49                         break;
50                 case 'n':
51                         opt_set(opts, nochanges, true);
52                         opt_set(opts, fix_errors, FSCK_OPT_NO);
53                         break;
54                 case 'f':
55                         /* force check, even if filesystem marked clean: */
56                         break;
57                 case 'o':
58                         ret = bch2_parse_mount_opts(NULL, &opts, optarg);
59                         if (ret)
60                                 return ret;
61                         break;
62                 case 'R':
63                         opt_set(opts, reconstruct_alloc, true);
64                         break;
65                 case 'v':
66                         opt_set(opts, verbose, true);
67                         break;
68                 case 'h':
69                         usage();
70                         exit(16);
71                 }
72         args_shift(optind);
73
74         if (!argc) {
75                 fprintf(stderr, "Please supply device(s) to check\n");
76                 exit(8);
77         }
78
79         for (i = 0; i < argc; i++) {
80                 switch (dev_mounted(argv[i])) {
81                 case 1:
82                         ret |= 2;
83                         break;
84                 case 2:
85                         fprintf(stderr, "%s is mounted read-write - aborting\n", argv[i]);
86                         exit(8);
87                 }
88         }
89
90         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
91         if (IS_ERR(c)) {
92                 fprintf(stderr, "error opening %s: %s\n", argv[0], strerror(-PTR_ERR(c)));
93                 exit(8);
94         }
95
96         if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags))
97                 ret |= 1;
98         if (test_bit(BCH_FS_ERROR, &c->flags))
99                 ret |= 4;
100
101         bch2_fs_stop(c);
102         return ret;
103 }