]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
cmd_migrate: check for write errors
[bcachefs-tools-debian] / cmd_fsck.c
1
2 #include <getopt.h>
3 #include <sys/uio.h>
4 #include <unistd.h>
5 #include "cmds.h"
6 #include "libbcachefs/error.h"
7 #include "libbcachefs.h"
8 #include "libbcachefs/super.h"
9 #include "tools-util.h"
10
11 static void usage(void)
12 {
13         puts("bcachefs fsck - filesystem check and repair\n"
14              "Usage: bcachefs fsck [OPTION]... <devices>\n"
15              "\n"
16              "Options:\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              "  -v                      Be verbose\n"
24              "  -h, --help              Display this help and exit\n"
25              "Report bugs to <linux-bcachefs@vger.kernel.org>");
26 }
27
28 static void setnonblocking(int fd)
29 {
30         int flags = fcntl(fd, F_GETFL);
31         if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
32                 die("fcntl error: %m");
33 }
34
35 static int do_splice(int rfd, int wfd)
36 {
37         char buf[4096];
38
39         int r = read(rfd, buf, sizeof(buf));
40         if (r < 0 && errno == EAGAIN)
41                 return 0;
42         if (r < 0)
43                 return r;
44         if (!r)
45                 return 1;
46         if (write(wfd, buf, r) != r)
47                 die("write error");
48         return 0;
49 }
50
51 static int fsck_online(const char *dev_path)
52 {
53         int dev_idx;
54         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
55
56         struct bch_ioctl_fsck_online fsck = { 0 };
57
58         int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
59         if (fsck_fd < 0)
60                 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
61
62         setnonblocking(STDIN_FILENO);
63         setnonblocking(fsck_fd);
64
65         while (true) {
66                 fd_set fds;
67
68                 FD_ZERO(&fds);
69                 FD_SET(STDIN_FILENO, &fds);
70                 FD_SET(fsck_fd, &fds);
71
72                 select(fsck_fd + 1, &fds, NULL, NULL, NULL);
73
74                 int r = do_splice(fsck_fd, STDOUT_FILENO) ?:
75                         do_splice(STDIN_FILENO, fsck_fd);
76                 if (r)
77                         return r < 0 ? r : 0;
78         }
79
80         pr_info("done");
81         return 0;
82 }
83
84 int cmd_fsck(int argc, char *argv[])
85 {
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' },
90                 { NULL }
91         };
92         struct bch_opts opts = bch2_opts_empty();
93         unsigned i;
94         int opt, ret = 0;
95
96         opt_set(opts, degraded, true);
97         opt_set(opts, fsck, true);
98         opt_set(opts, fix_errors, FSCK_FIX_ask);
99
100         while ((opt = getopt_long(argc, argv,
101                                   "apynfo:rvh",
102                                   longopts, NULL)) != -1)
103                 switch (opt) {
104                 case 'a': /* outdated alias for -p */
105                 case 'p':
106                 case 'y':
107                         opt_set(opts, fix_errors, FSCK_FIX_yes);
108                         break;
109                 case 'n':
110                         opt_set(opts, nochanges, true);
111                         opt_set(opts, fix_errors, FSCK_FIX_no);
112                         break;
113                 case 'f':
114                         /* force check, even if filesystem marked clean: */
115                         break;
116                 case 'o':
117                         ret = bch2_parse_mount_opts(NULL, &opts, optarg);
118                         if (ret)
119                                 return ret;
120                         break;
121                 case 'r':
122                         opt_set(opts, ratelimit_errors, true);
123                         break;
124                 case 'R':
125                         opt_set(opts, reconstruct_alloc, true);
126                         break;
127                 case 'v':
128                         opt_set(opts, verbose, true);
129                         break;
130                 case 'h':
131                         usage();
132                         exit(16);
133                 }
134         args_shift(optind);
135
136         if (!argc) {
137                 fprintf(stderr, "Please supply device(s) to check\n");
138                 exit(8);
139         }
140
141         for (i = 0; i < argc; i++)
142                 if (dev_mounted(argv[i]))
143                         return fsck_online(argv[i]);
144
145         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
146         if (IS_ERR(c)) {
147                 fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
148                 exit(8);
149         }
150
151         if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
152                 fprintf(stderr, "%s: errors fixed\n", c->name);
153                 ret |= 1;
154         }
155         if (test_bit(BCH_FS_error, &c->flags)) {
156                 fprintf(stderr, "%s: still has errors\n", c->name);
157                 ret |= 4;
158         }
159
160         bch2_fs_stop(c);
161         return ret;
162 }