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);
76 r = do_splice(STDIN_FILENO, fd);
84 static int fsck_online(const char *dev_path)
87 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
89 struct bch_ioctl_fsck_online fsck = { 0 };
91 int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
93 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
95 return splice_fd_to_stdinout(fsck_fd);
98 static void append_opt(struct printbuf *out, const char *opt)
105 static bool should_use_kernel_fsck(darray_str devs)
107 unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK)
108 ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
114 if (kernel_version == bcachefs_metadata_version_current)
117 struct bch_opts opts = bch2_opts_empty();
118 opt_set(opts, nostart, true);
119 opt_set(opts, noexcl, true);
120 opt_set(opts, nochanges, true);
121 opt_set(opts, read_only, true);
123 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
127 bool ret = ((bcachefs_metadata_version_current < kernel_version &&
128 kernel_version <= c->sb.version) ||
129 (c->sb.version <= kernel_version &&
130 kernel_version < bcachefs_metadata_version_current));
133 struct printbuf buf = PRINTBUF;
135 prt_str(&buf, "fsck binary is version ");
136 bch2_version_to_text(&buf, bcachefs_metadata_version_current);
137 prt_str(&buf, " but filesystem is ");
138 bch2_version_to_text(&buf, c->sb.version);
139 prt_str(&buf, " and kernel is ");
140 bch2_version_to_text(&buf, kernel_version);
141 prt_str(&buf, ", using kernel fsck\n");
143 printf("%s", buf.buf);
152 int cmd_fsck(int argc, char *argv[])
154 static const struct option longopts[] = {
155 { "ratelimit_errors", no_argument, NULL, 'r' },
156 { "reconstruct_alloc", no_argument, NULL, 'R' },
157 { "kernel", no_argument, NULL, 'k' },
158 { "no-kernel", no_argument, NULL, 'K' },
159 { "help", no_argument, NULL, 'h' },
162 int kernel = -1; /* unset */
164 struct printbuf opts_str = PRINTBUF;
166 append_opt(&opts_str, "degraded");
167 append_opt(&opts_str, "fsck");
168 append_opt(&opts_str, "fix_errors=ask");
169 append_opt(&opts_str, "read_only");
171 while ((opt = getopt_long(argc, argv,
173 longopts, NULL)) != -1)
175 case 'a': /* outdated alias for -p */
178 append_opt(&opts_str, "fix_errors=yes");
181 append_opt(&opts_str, "nochanges");
182 append_opt(&opts_str, "fix_errors=no");
185 /* force check, even if filesystem marked clean: */
188 append_opt(&opts_str, optarg);
191 append_opt(&opts_str, "ratelimit_errors");
194 append_opt(&opts_str, "reconstruct_alloc");
203 append_opt(&opts_str, "verbose");
212 fprintf(stderr, "Please supply device(s) to check\n");
216 darray_str devs = get_or_split_cmdline_devs(argc, argv);
218 int kernel_probed = kernel;
219 if (kernel_probed < 0)
220 kernel_probed = should_use_kernel_fsck(devs);
223 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
224 sizeof(u64) * devs.nr, 1);
226 fsck->opts = (unsigned long)opts_str.buf;
227 darray_for_each(devs, i)
228 fsck->devs[i - devs.data] = (unsigned long) *i;
229 fsck->nr_devs = devs.nr;
231 int ctl_fd = bcachectl_open();
232 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
235 if (fsck_fd < 0 && kernel < 0)
239 die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
241 ret = splice_fd_to_stdinout(fsck_fd);
244 struct bch_opts opts = bch2_opts_empty();
245 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
249 darray_for_each(devs, i)
251 return fsck_online(*i);
253 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
257 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
258 fprintf(stderr, "%s: errors fixed\n", c->name);
261 if (test_bit(BCH_FS_error, &c->flags)) {
262 fprintf(stderr, "%s: still has errors\n", c->name);
269 printbuf_exit(&opts_str);