6 #include "libbcachefs/error.h"
7 #include "libbcachefs.h"
8 #include "libbcachefs/super.h"
9 #include "tools-util.h"
11 static void fsck_usage(void)
13 puts("bcachefs fsck - filesystem check and repair\n"
14 "Usage: bcachefs fsck [OPTION]... <devices>\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 " -k, --kernel Use the in-kernel fsck implementation\n"
25 " -h, --help Display this help and exit\n"
26 "Report bugs to <linux-bcachefs@vger.kernel.org>");
29 static void setnonblocking(int fd)
31 int flags = fcntl(fd, F_GETFL);
32 if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
33 die("fcntl error: %m");
36 static int do_splice(int rfd, int wfd)
38 char buf[4096], *b = buf;
40 int r = read(rfd, buf, sizeof(buf));
41 if (r < 0 && errno == EAGAIN)
48 ssize_t w = write(wfd, b, r);
50 die("%s: write error: %m", __func__);
57 static int splice_fd_to_stdinout(int fd)
59 setnonblocking(STDIN_FILENO);
66 FD_SET(STDIN_FILENO, &fds);
69 select(fd + 1, &fds, NULL, NULL, NULL);
71 int r = do_splice(fd, STDOUT_FILENO) ?:
72 do_splice(STDIN_FILENO, fd);
80 static int fsck_online(const char *dev_path)
83 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
85 struct bch_ioctl_fsck_online fsck = { 0 };
87 int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
89 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
91 return splice_fd_to_stdinout(fsck_fd);
94 static void append_opt(struct printbuf *out, const char *opt)
101 int cmd_fsck(int argc, char *argv[])
103 static const struct option longopts[] = {
104 { "ratelimit_errors", no_argument, NULL, 'r' },
105 { "reconstruct_alloc", no_argument, NULL, 'R' },
106 { "kernel", no_argument, NULL, 'k' },
107 { "help", no_argument, NULL, 'h' },
112 struct printbuf opts_str = PRINTBUF;
114 append_opt(&opts_str, "degraded");
115 append_opt(&opts_str, "fsck");
116 append_opt(&opts_str, "fix_errors=ask");
117 append_opt(&opts_str, "read_only");
119 while ((opt = getopt_long(argc, argv,
121 longopts, NULL)) != -1)
123 case 'a': /* outdated alias for -p */
126 append_opt(&opts_str, "fix_errors=yes");
129 append_opt(&opts_str, "nochanges");
130 append_opt(&opts_str, "fix_errors=no");
133 /* force check, even if filesystem marked clean: */
136 append_opt(&opts_str, optarg);
139 append_opt(&opts_str, "ratelimit_errors");
142 append_opt(&opts_str, "reconstruct_alloc");
148 append_opt(&opts_str, "verbose");
157 fprintf(stderr, "Please supply device(s) to check\n");
161 darray_str devs = get_or_split_cmdline_devs(argc, argv);
164 struct bch_opts opts = bch2_opts_empty();
165 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
169 darray_for_each(devs, i)
171 return fsck_online(*i);
173 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
177 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
178 fprintf(stderr, "%s: errors fixed\n", c->name);
181 if (test_bit(BCH_FS_error, &c->flags)) {
182 fprintf(stderr, "%s: still has errors\n", c->name);
188 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
189 sizeof(u64) * devs.nr, 1);
191 fsck->opts = (unsigned long)opts_str.buf;
192 darray_for_each(devs, i)
193 fsck->devs[i - devs.data] = (unsigned long) *i;
194 fsck->nr_devs = devs.nr;
196 int ctl_fd = bcachectl_open();
198 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
200 die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
202 ret = splice_fd_to_stdinout(fsck_fd);
206 printbuf_exit(&opts_str);