6 #include "libbcachefs/error.h"
7 #include "libbcachefs.h"
8 #include "libbcachefs/super.h"
9 #include "tools-util.h"
11 static void 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"
24 " -h, --help Display this help and exit\n"
25 "Report bugs to <linux-bcachefs@vger.kernel.org>");
28 static void setnonblocking(int fd)
30 int flags = fcntl(fd, F_GETFL);
31 if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
32 die("fcntl error: %m");
35 static int do_splice(int rfd, int wfd)
39 int r = read(rfd, buf, sizeof(buf));
40 if (r < 0 && errno == EAGAIN)
46 if (write(wfd, buf, r) != r)
51 static int fsck_online(const char *dev_path)
54 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
56 struct bch_ioctl_fsck_online fsck = { 0 };
58 int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
60 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
62 setnonblocking(STDIN_FILENO);
63 setnonblocking(fsck_fd);
69 FD_SET(STDIN_FILENO, &fds);
70 FD_SET(fsck_fd, &fds);
72 select(fsck_fd + 1, &fds, NULL, NULL, NULL);
74 int r = do_splice(fsck_fd, STDOUT_FILENO) ?:
75 do_splice(STDIN_FILENO, fsck_fd);
84 int cmd_fsck(int argc, char *argv[])
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' },
92 struct bch_opts opts = bch2_opts_empty();
96 opt_set(opts, degraded, true);
97 opt_set(opts, fsck, true);
98 opt_set(opts, fix_errors, FSCK_FIX_ask);
100 while ((opt = getopt_long(argc, argv,
102 longopts, NULL)) != -1)
104 case 'a': /* outdated alias for -p */
107 opt_set(opts, fix_errors, FSCK_FIX_yes);
110 opt_set(opts, nochanges, true);
111 opt_set(opts, fix_errors, FSCK_FIX_no);
114 /* force check, even if filesystem marked clean: */
117 ret = bch2_parse_mount_opts(NULL, &opts, optarg);
122 opt_set(opts, ratelimit_errors, true);
125 opt_set(opts, reconstruct_alloc, true);
128 opt_set(opts, verbose, true);
137 fprintf(stderr, "Please supply device(s) to check\n");
141 for (i = 0; i < argc; i++)
142 if (dev_mounted(argv[i]))
143 return fsck_online(argv[i]);
145 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
147 fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
151 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
152 fprintf(stderr, "%s: errors fixed\n", c->name);
155 if (test_bit(BCH_FS_error, &c->flags)) {
156 fprintf(stderr, "%s: still has errors\n", c->name);