]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
Add packaging workflow
[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              "  -r, --ratelimit_errors  Don't display more than 10 errors of a given type\n"
20              "  -R, --reconstruct_alloc Reconstruct the alloc btree\n"
21              "  -v                      Be verbose\n"
22              "  -h, --help              Display this help and exit\n"
23              "Report bugs to <linux-bcachefs@vger.kernel.org>");
24 }
25
26 int cmd_fsck(int argc, char *argv[])
27 {
28         static const struct option longopts[] = {
29                 { "ratelimit_errors",   no_argument,            NULL, 'r' },
30                 { "reconstruct_alloc",  no_argument,            NULL, 'R' },
31                 { "help",               no_argument,            NULL, 'h' },
32                 { NULL }
33         };
34         struct bch_opts opts = bch2_opts_empty();
35         unsigned i;
36         int opt, ret = 0;
37
38         opt_set(opts, degraded, true);
39         opt_set(opts, fsck, true);
40         opt_set(opts, fix_errors, FSCK_OPT_ASK);
41
42         while ((opt = getopt_long(argc, argv,
43                                   "apynfo:rvh",
44                                   longopts, NULL)) != -1)
45                 switch (opt) {
46                 case 'a': /* outdated alias for -p */
47                 case 'p':
48                         opt_set(opts, fix_errors, FSCK_OPT_YES);
49                         break;
50                 case 'y':
51                         opt_set(opts, fix_errors, FSCK_OPT_YES);
52                         break;
53                 case 'n':
54                         opt_set(opts, nochanges, true);
55                         opt_set(opts, fix_errors, FSCK_OPT_NO);
56                         break;
57                 case 'f':
58                         /* force check, even if filesystem marked clean: */
59                         break;
60                 case 'o':
61                         ret = bch2_parse_mount_opts(NULL, &opts, optarg);
62                         if (ret)
63                                 return ret;
64                         break;
65                 case 'r':
66                         opt_set(opts, ratelimit_errors, true);
67                         break;
68                 case 'R':
69                         opt_set(opts, reconstruct_alloc, true);
70                         break;
71                 case 'v':
72                         opt_set(opts, verbose, true);
73                         break;
74                 case 'h':
75                         usage();
76                         exit(16);
77                 }
78         args_shift(optind);
79
80         if (!argc) {
81                 fprintf(stderr, "Please supply device(s) to check\n");
82                 exit(8);
83         }
84
85         for (i = 0; i < argc; i++) {
86                 switch (dev_mounted(argv[i])) {
87                 case 1:
88                         ret |= 2;
89                         break;
90                 case 2:
91                         fprintf(stderr, "%s is mounted read-write - aborting\n", argv[i]);
92                         exit(8);
93                 }
94         }
95
96         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
97         if (IS_ERR(c)) {
98                 fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
99                 exit(8);
100         }
101
102         if (test_bit(BCH_FS_ERRORS_FIXED, &c->flags)) {
103                 fprintf(stderr, "%s: errors fixed\n", c->name);
104                 ret |= 1;
105         }
106         if (test_bit(BCH_FS_ERROR, &c->flags)) {
107                 fprintf(stderr, "%s: still has errors\n", c->name);
108                 ret |= 4;
109         }
110
111         bch2_fs_stop(c);
112         return ret;
113 }