]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/cmd_fsck.c
79f520076bb204703e4bfa1e7908bad98709fbe4
[bcachefs-tools-debian] / c_src / 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 "libbcachefs/super-io.h"
10 #include "tools-util.h"
11
12 static void fsck_usage(void)
13 {
14         puts("bcachefs fsck - filesystem check and repair\n"
15              "Usage: bcachefs fsck [OPTION]... <devices>\n"
16              "\n"
17              "Options:\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"
25              "  -v                      Be verbose\n"
26              "  -h, --help              Display this help and exit\n"
27              "Report bugs to <linux-bcachefs@vger.kernel.org>");
28 }
29
30 static void setnonblocking(int fd)
31 {
32         int flags = fcntl(fd, F_GETFL);
33         if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
34                 die("fcntl error: %m");
35 }
36
37 static int do_splice(int rfd, int wfd)
38 {
39         char buf[4096], *b = buf;
40
41         int r = read(rfd, buf, sizeof(buf));
42         if (r < 0 && errno == EAGAIN)
43                 return 0;
44         if (r < 0)
45                 return r;
46         if (!r)
47                 return 1;
48         do {
49                 ssize_t w = write(wfd, b, r);
50                 if (w < 0)
51                         die("%s: write error: %m", __func__);
52                 r -= w;
53                 b += w;
54         } while (r);
55         return 0;
56 }
57
58 static int splice_fd_to_stdinout(int fd)
59 {
60         setnonblocking(STDIN_FILENO);
61         setnonblocking(fd);
62
63         while (true) {
64                 fd_set fds;
65
66                 FD_ZERO(&fds);
67                 FD_SET(STDIN_FILENO, &fds);
68                 FD_SET(fd, &fds);
69
70                 select(fd + 1, &fds, NULL, NULL, NULL);
71
72                 int r = do_splice(fd, STDOUT_FILENO);
73                 if (r)
74                         return r < 0 ? r : 0;
75
76                 r = do_splice(STDIN_FILENO, fd);
77                 if (r < 0)
78                         return r;
79         }
80
81         return 0;
82 }
83
84 static int fsck_online(const char *dev_path)
85 {
86         int dev_idx;
87         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
88
89         struct bch_ioctl_fsck_online fsck = { 0 };
90
91         int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
92         if (fsck_fd < 0)
93                 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
94
95         return splice_fd_to_stdinout(fsck_fd);
96 }
97
98 static void append_opt(struct printbuf *out, const char *opt)
99 {
100         if (out->pos)
101                 prt_char(out, ',');
102         prt_str(out, opt);
103 }
104
105 static bool should_use_kernel_fsck(darray_str devs)
106 {
107         unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK)
108             ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
109             : 0;
110
111         if (!kernel_version)
112                 return false;
113
114         if (kernel_version == bcachefs_metadata_version_current)
115                 return false;
116
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);
122
123         struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
124         if (IS_ERR(c))
125                 return false;
126
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));
131
132         if (ret) {
133                 struct printbuf buf = PRINTBUF;
134
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");
142
143                 printf("%s", buf.buf);
144                 printbuf_exit(&buf);
145         }
146
147         bch2_fs_stop(c);
148
149         return ret;
150 }
151
152 int cmd_fsck(int argc, char *argv[])
153 {
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' },
160                 { NULL }
161         };
162         int kernel = -1; /* unset */
163         int opt, ret = 0;
164         struct printbuf opts_str = PRINTBUF;
165
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");
170
171         while ((opt = getopt_long(argc, argv,
172                                   "apynfo:rRkvh",
173                                   longopts, NULL)) != -1)
174                 switch (opt) {
175                 case 'a': /* outdated alias for -p */
176                 case 'p':
177                 case 'y':
178                         append_opt(&opts_str, "fix_errors=yes");
179                         break;
180                 case 'n':
181                         append_opt(&opts_str, "nochanges");
182                         append_opt(&opts_str, "fix_errors=no");
183                         break;
184                 case 'f':
185                         /* force check, even if filesystem marked clean: */
186                         break;
187                 case 'o':
188                         append_opt(&opts_str, optarg);
189                         break;
190                 case 'r':
191                         append_opt(&opts_str, "ratelimit_errors");
192                         break;
193                 case 'R':
194                         append_opt(&opts_str, "reconstruct_alloc");
195                         break;
196                 case 'k':
197                         kernel = true;
198                         break;
199                 case 'K':
200                         kernel = false;
201                         break;
202                 case 'v':
203                         append_opt(&opts_str, "verbose");
204                         break;
205                 case 'h':
206                         fsck_usage();
207                         exit(16);
208                 }
209         args_shift(optind);
210
211         if (!argc) {
212                 fprintf(stderr, "Please supply device(s) to check\n");
213                 exit(8);
214         }
215
216         darray_str devs = get_or_split_cmdline_devs(argc, argv);
217
218         if (kernel < 0)
219                 kernel = should_use_kernel_fsck(devs);
220
221         if (!kernel) {
222                 struct bch_opts opts = bch2_opts_empty();
223                 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
224                 if (ret)
225                         return ret;
226
227                 darray_for_each(devs, i)
228                         if (dev_mounted(*i))
229                                 return fsck_online(*i);
230
231                 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
232                 if (IS_ERR(c))
233                         exit(8);
234
235                 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
236                         fprintf(stderr, "%s: errors fixed\n", c->name);
237                         ret |= 1;
238                 }
239                 if (test_bit(BCH_FS_error, &c->flags)) {
240                         fprintf(stderr, "%s: still has errors\n", c->name);
241                         ret |= 4;
242                 }
243
244                 bch2_fs_stop(c);
245         } else {
246                 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
247                                                              sizeof(u64) * devs.nr, 1);
248
249                 fsck->opts = (unsigned long)opts_str.buf;
250                 darray_for_each(devs, i)
251                         fsck->devs[i - devs.data] = (unsigned long) *i;
252                 fsck->nr_devs = devs.nr;
253
254                 int ctl_fd = bcachectl_open();
255
256                 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
257                 if (fsck_fd < 0)
258                         die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
259
260                 ret = splice_fd_to_stdinout(fsck_fd);
261                 free(fsck);
262         }
263
264         printbuf_exit(&opts_str);
265         return ret;
266 }