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)
40 int r = read(rfd, buf, sizeof(buf));
41 if (r < 0 && errno == EAGAIN)
47 if (write(wfd, buf, r) != r)
52 static int splice_fd_to_stdinout(int fd)
54 setnonblocking(STDIN_FILENO);
61 FD_SET(STDIN_FILENO, &fds);
64 select(fd + 1, &fds, NULL, NULL, NULL);
66 int r = do_splice(fd, STDOUT_FILENO) ?:
67 do_splice(STDIN_FILENO, fd);
75 static int fsck_online(const char *dev_path)
78 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
80 struct bch_ioctl_fsck_online fsck = { 0 };
82 int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
84 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
86 return splice_fd_to_stdinout(fsck_fd);
89 static void append_opt(struct printbuf *out, const char *opt)
96 int cmd_fsck(int argc, char *argv[])
98 static const struct option longopts[] = {
99 { "ratelimit_errors", no_argument, NULL, 'r' },
100 { "reconstruct_alloc", no_argument, NULL, 'R' },
101 { "kernel", no_argument, NULL, 'k' },
102 { "help", no_argument, NULL, 'h' },
107 struct printbuf opts_str = PRINTBUF;
109 append_opt(&opts_str, "degraded");
110 append_opt(&opts_str, "fsck");
111 append_opt(&opts_str, "fix_errors=ask");
112 append_opt(&opts_str, "read_only");
114 while ((opt = getopt_long(argc, argv,
116 longopts, NULL)) != -1)
118 case 'a': /* outdated alias for -p */
121 append_opt(&opts_str, "fix_errors=yes");
124 append_opt(&opts_str, "nochanges");
125 append_opt(&opts_str, "fix_errors=no");
128 /* force check, even if filesystem marked clean: */
131 append_opt(&opts_str, optarg);
134 append_opt(&opts_str, "ratelimit_errors");
137 append_opt(&opts_str, "reconstruct_alloc");
143 append_opt(&opts_str, "verbose");
152 fprintf(stderr, "Please supply device(s) to check\n");
156 darray_str devs = get_or_split_cmdline_devs(argc, argv);
159 struct bch_opts opts = bch2_opts_empty();
160 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
164 darray_for_each(devs, i)
166 return fsck_online(*i);
168 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
172 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
173 fprintf(stderr, "%s: errors fixed\n", c->name);
176 if (test_bit(BCH_FS_error, &c->flags)) {
177 fprintf(stderr, "%s: still has errors\n", c->name);
183 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
184 sizeof(u64) * devs.nr, 1);
186 fsck->opts = (unsigned long)opts_str.buf;
187 darray_for_each(devs, i)
188 fsck->devs[i - devs.data] = (unsigned long) *i;
189 fsck->nr_devs = devs.nr;
191 int ctl_fd = bcachectl_open();
193 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
195 die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
197 ret = splice_fd_to_stdinout(fsck_fd);
201 printbuf_exit(&opts_str);