]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/cmd_fsck.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[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 < 0)
74                         return r;
75                 if (r)
76                         break;
77
78                 r = do_splice(STDIN_FILENO, fd);
79                 if (r < 0)
80                         return r;
81         }
82
83         return close(fd);
84 }
85
86 static int fsck_online(const char *dev_path)
87 {
88         int dev_idx;
89         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
90
91         struct bch_ioctl_fsck_online fsck = { 0 };
92
93         int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
94         if (fsck_fd < 0)
95                 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
96
97         return splice_fd_to_stdinout(fsck_fd);
98 }
99
100 static void append_opt(struct printbuf *out, const char *opt)
101 {
102         if (out->pos)
103                 prt_char(out, ',');
104         prt_str(out, opt);
105 }
106
107 static bool should_use_kernel_fsck(darray_str devs)
108 {
109         unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK)
110             ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
111             : 0;
112
113         if (!kernel_version)
114                 return false;
115
116         if (kernel_version == bcachefs_metadata_version_current)
117                 return false;
118
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);
124
125         struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
126         if (IS_ERR(c))
127                 return false;
128
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));
133
134         if (ret) {
135                 struct printbuf buf = PRINTBUF;
136
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");
144
145                 printf("%s", buf.buf);
146                 printbuf_exit(&buf);
147         }
148
149         bch2_fs_stop(c);
150
151         return ret;
152 }
153
154 int cmd_fsck(int argc, char *argv[])
155 {
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' },
162                 { NULL }
163         };
164         int kernel = -1; /* unset */
165         int opt, ret = 0;
166         struct printbuf opts_str = PRINTBUF;
167
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");
172
173         while ((opt = getopt_long(argc, argv,
174                                   "apynfo:rRkvh",
175                                   longopts, NULL)) != -1)
176                 switch (opt) {
177                 case 'a': /* outdated alias for -p */
178                 case 'p':
179                 case 'y':
180                         append_opt(&opts_str, "fix_errors=yes");
181                         break;
182                 case 'n':
183                         append_opt(&opts_str, "nochanges");
184                         append_opt(&opts_str, "fix_errors=no");
185                         break;
186                 case 'f':
187                         /* force check, even if filesystem marked clean: */
188                         break;
189                 case 'o':
190                         append_opt(&opts_str, optarg);
191                         break;
192                 case 'r':
193                         append_opt(&opts_str, "ratelimit_errors");
194                         break;
195                 case 'R':
196                         append_opt(&opts_str, "reconstruct_alloc");
197                         break;
198                 case 'k':
199                         kernel = true;
200                         break;
201                 case 'K':
202                         kernel = false;
203                         break;
204                 case 'v':
205                         append_opt(&opts_str, "verbose");
206                         break;
207                 case 'h':
208                         fsck_usage();
209                         exit(16);
210                 }
211         args_shift(optind);
212
213         if (!argc) {
214                 fprintf(stderr, "Please supply device(s) to check\n");
215                 exit(8);
216         }
217
218         darray_str devs = get_or_split_cmdline_devs(argc, argv);
219
220         int kernel_probed = kernel;
221         if (kernel_probed < 0)
222                 kernel_probed = should_use_kernel_fsck(devs);
223
224         struct bch_opts opts = bch2_opts_empty();
225
226         if (kernel_probed) {
227                 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
228                                                              sizeof(u64) * devs.nr, 1);
229
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;
234
235                 int ctl_fd = bcachectl_open();
236                 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
237                 free(fsck);
238
239                 if (fsck_fd < 0 && kernel < 0)
240                         goto userland_fsck;
241
242                 if (fsck_fd < 0)
243                         die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
244
245                 ret = splice_fd_to_stdinout(fsck_fd);
246         } else {
247 userland_fsck:
248                 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
249                 if (ret)
250                         return ret;
251
252                 darray_for_each(devs, i)
253                         if (dev_mounted(*i))
254                                 return fsck_online(*i);
255
256                 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
257                 if (IS_ERR(c))
258                         exit(8);
259
260                 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
261                         fprintf(stderr, "%s: errors fixed\n", c->name);
262                         ret |= 1;
263                 }
264                 if (test_bit(BCH_FS_error, &c->flags)) {
265                         fprintf(stderr, "%s: still has errors\n", c->name);
266                         ret |= 4;
267                 }
268
269                 bch2_fs_stop(c);
270         }
271
272         printbuf_exit(&opts_str);
273         return ret;
274 }