]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
cmd_fsck can now take colon separated devices
[bcachefs-tools-debian] / cmd_fsck.c
1
2 #include <getopt.h>
3 #include <sys/uio.h>
4 #include <unistd.h>
5 #include "cmds.h"
6 #include "libbcachefs/error.h"
7 #include "libbcachefs.h"
8 #include "libbcachefs/super.h"
9 #include "tools-util.h"
10
11 static void usage(void)
12 {
13         puts("bcachefs fsck - filesystem check and repair\n"
14              "Usage: bcachefs fsck [OPTION]... <devices>\n"
15              "\n"
16              "Options:\n"
17              "  -p                      Automatic repair (no questions)\n"
18              "  -n                      Don't repair, only check for errors\n"
19              "  -y                      Assume \"yes\" to all questions\n"
20              "  -f                      Force checking even if filesystem is marked clean\n"
21              "  -r, --ratelimit_errors  Don't display more than 10 errors of a given type\n"
22              "  -R, --reconstruct_alloc Reconstruct the alloc btree\n"
23              "  -v                      Be verbose\n"
24              "  -h, --help              Display this help and exit\n"
25              "Report bugs to <linux-bcachefs@vger.kernel.org>");
26 }
27
28 static void setnonblocking(int fd)
29 {
30         int flags = fcntl(fd, F_GETFL);
31         if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
32                 die("fcntl error: %m");
33 }
34
35 static int do_splice(int rfd, int wfd)
36 {
37         char buf[4096];
38
39         int r = read(rfd, buf, sizeof(buf));
40         if (r < 0 && errno == EAGAIN)
41                 return 0;
42         if (r < 0)
43                 return r;
44         if (!r)
45                 return 1;
46         if (write(wfd, buf, r) != r)
47                 die("write error");
48         return 0;
49 }
50
51 static int fsck_online(const char *dev_path)
52 {
53         int dev_idx;
54         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
55
56         struct bch_ioctl_fsck_online fsck = { 0 };
57
58         int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
59         if (fsck_fd < 0)
60                 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
61
62         setnonblocking(STDIN_FILENO);
63         setnonblocking(fsck_fd);
64
65         while (true) {
66                 fd_set fds;
67
68                 FD_ZERO(&fds);
69                 FD_SET(STDIN_FILENO, &fds);
70                 FD_SET(fsck_fd, &fds);
71
72                 select(fsck_fd + 1, &fds, NULL, NULL, NULL);
73
74                 int r = do_splice(fsck_fd, STDOUT_FILENO) ?:
75                         do_splice(STDIN_FILENO, fsck_fd);
76                 if (r)
77                         return r < 0 ? r : 0;
78         }
79
80         pr_info("done");
81         return 0;
82 }
83
84 int cmd_fsck(int argc, char *argv[])
85 {
86         static const struct option longopts[] = {
87                 { "ratelimit_errors",   no_argument,            NULL, 'r' },
88                 { "reconstruct_alloc",  no_argument,            NULL, 'R' },
89                 { "help",               no_argument,            NULL, 'h' },
90                 { NULL }
91         };
92         struct bch_opts opts = bch2_opts_empty();
93         int opt, ret = 0;
94
95         opt_set(opts, degraded, true);
96         opt_set(opts, fsck, true);
97         opt_set(opts, fix_errors, FSCK_FIX_ask);
98
99         while ((opt = getopt_long(argc, argv,
100                                   "apynfo:rvh",
101                                   longopts, NULL)) != -1)
102                 switch (opt) {
103                 case 'a': /* outdated alias for -p */
104                 case 'p':
105                 case 'y':
106                         opt_set(opts, fix_errors, FSCK_FIX_yes);
107                         break;
108                 case 'n':
109                         opt_set(opts, nochanges, true);
110                         opt_set(opts, fix_errors, FSCK_FIX_no);
111                         break;
112                 case 'f':
113                         /* force check, even if filesystem marked clean: */
114                         break;
115                 case 'o':
116                         ret = bch2_parse_mount_opts(NULL, &opts, optarg);
117                         if (ret)
118                                 return ret;
119                         break;
120                 case 'r':
121                         opt_set(opts, ratelimit_errors, true);
122                         break;
123                 case 'R':
124                         opt_set(opts, reconstruct_alloc, true);
125                         break;
126                 case 'v':
127                         opt_set(opts, verbose, true);
128                         break;
129                 case 'h':
130                         usage();
131                         exit(16);
132                 }
133         args_shift(optind);
134
135         if (!argc) {
136                 fprintf(stderr, "Please supply device(s) to check\n");
137                 exit(8);
138         }
139
140         darray_str devs = get_or_split_cmdline_devs(argc, argv);
141
142         darray_for_each(devs, i)
143                 if (dev_mounted(*i))
144                         return fsck_online(*i);
145
146         struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
147         if (IS_ERR(c))
148                 exit(8);
149
150         if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
151                 fprintf(stderr, "%s: errors fixed\n", c->name);
152                 ret |= 1;
153         }
154         if (test_bit(BCH_FS_error, &c->flags)) {
155                 fprintf(stderr, "%s: still has errors\n", c->name);
156                 ret |= 4;
157         }
158
159         bch2_fs_stop(c);
160         return ret;
161 }