6 #include "libbcachefs/error.h"
7 #include "libbcachefs.h"
8 #include "libbcachefs/super.h"
9 #include "libbcachefs/super-io.h"
10 #include "tools-util.h"
12 static void fsck_usage(void)
14 puts("bcachefs fsck - filesystem check and repair\n"
15 "Usage: bcachefs fsck [OPTION]... <devices>\n"
18 " -p Automatic repair (no questions)\n"
19 " -n Don't repair, only check for errors\n"
20 " -y Assume \"yes\" to all questions\n"
21 " -f Force checking even if filesystem is marked clean\n"
22 " -r, --ratelimit_errors Don't display more than 10 errors of a given type\n"
23 " -R, --reconstruct_alloc Reconstruct the alloc btree\n"
24 " -k, --kernel Use the in-kernel fsck implementation\n"
26 " -h, --help Display this help and exit\n"
27 "Report bugs to <linux-bcachefs@vger.kernel.org>");
30 static void setnonblocking(int fd)
32 int flags = fcntl(fd, F_GETFL);
33 if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
34 die("fcntl error: %m");
37 static int do_splice(int rfd, int wfd)
39 char buf[4096], *b = buf;
41 int r = read(rfd, buf, sizeof(buf));
42 if (r < 0 && errno == EAGAIN)
49 ssize_t w = write(wfd, b, r);
51 die("%s: write error: %m", __func__);
58 static int splice_fd_to_stdinout(int fd)
60 setnonblocking(STDIN_FILENO);
67 FD_SET(STDIN_FILENO, &fds);
70 select(fd + 1, &fds, NULL, NULL, NULL);
72 int r = do_splice(fd, STDOUT_FILENO);
78 r = do_splice(STDIN_FILENO, fd);
86 static int fsck_online(const char *dev_path)
89 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
91 struct bch_ioctl_fsck_online fsck = { 0 };
93 int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
95 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
97 return splice_fd_to_stdinout(fsck_fd);
100 static void append_opt(struct printbuf *out, const char *opt)
107 static bool should_use_kernel_fsck(darray_str devs)
109 unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK)
110 ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
116 if (kernel_version == bcachefs_metadata_version_current)
119 struct bch_opts opts = bch2_opts_empty();
120 opt_set(opts, nostart, true);
121 opt_set(opts, noexcl, true);
122 opt_set(opts, nochanges, true);
123 opt_set(opts, read_only, true);
125 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
129 bool ret = ((bcachefs_metadata_version_current < kernel_version &&
130 kernel_version <= c->sb.version) ||
131 (c->sb.version <= kernel_version &&
132 kernel_version < bcachefs_metadata_version_current));
135 struct printbuf buf = PRINTBUF;
137 prt_str(&buf, "fsck binary is version ");
138 bch2_version_to_text(&buf, bcachefs_metadata_version_current);
139 prt_str(&buf, " but filesystem is ");
140 bch2_version_to_text(&buf, c->sb.version);
141 prt_str(&buf, " and kernel is ");
142 bch2_version_to_text(&buf, kernel_version);
143 prt_str(&buf, ", using kernel fsck\n");
145 printf("%s", buf.buf);
154 int cmd_fsck(int argc, char *argv[])
156 static const struct option longopts[] = {
157 { "ratelimit_errors", no_argument, NULL, 'r' },
158 { "reconstruct_alloc", no_argument, NULL, 'R' },
159 { "kernel", no_argument, NULL, 'k' },
160 { "no-kernel", no_argument, NULL, 'K' },
161 { "help", no_argument, NULL, 'h' },
164 int kernel = -1; /* unset */
166 struct printbuf opts_str = PRINTBUF;
168 append_opt(&opts_str, "degraded");
169 append_opt(&opts_str, "fsck");
170 append_opt(&opts_str, "fix_errors=ask");
171 append_opt(&opts_str, "read_only");
173 while ((opt = getopt_long(argc, argv,
175 longopts, NULL)) != -1)
177 case 'a': /* outdated alias for -p */
180 append_opt(&opts_str, "fix_errors=yes");
183 append_opt(&opts_str, "nochanges");
184 append_opt(&opts_str, "fix_errors=no");
187 /* force check, even if filesystem marked clean: */
190 append_opt(&opts_str, optarg);
193 append_opt(&opts_str, "ratelimit_errors");
196 append_opt(&opts_str, "reconstruct_alloc");
205 append_opt(&opts_str, "verbose");
214 fprintf(stderr, "Please supply device(s) to check\n");
218 darray_str devs = get_or_split_cmdline_devs(argc, argv);
220 int kernel_probed = kernel;
221 if (kernel_probed < 0)
222 kernel_probed = should_use_kernel_fsck(devs);
224 struct bch_opts opts = bch2_opts_empty();
227 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
228 sizeof(u64) * devs.nr, 1);
230 fsck->opts = (unsigned long)opts_str.buf;
231 darray_for_each(devs, i)
232 fsck->devs[i - devs.data] = (unsigned long) *i;
233 fsck->nr_devs = devs.nr;
235 int ctl_fd = bcachectl_open();
236 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
239 if (fsck_fd < 0 && kernel < 0)
243 die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
245 ret = splice_fd_to_stdinout(fsck_fd);
248 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
252 darray_for_each(devs, i)
254 return fsck_online(*i);
256 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
260 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
261 fprintf(stderr, "%s: errors fixed\n", c->name);
264 if (test_bit(BCH_FS_error, &c->flags)) {
265 fprintf(stderr, "%s: still has errors\n", c->name);
272 printbuf_exit(&opts_str);