]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fsck.c
Update bcachefs sources to 44ac32df8e0c bcachefs: Split brain detection
[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 fsck_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              "  -k, --kernel            Use the in-kernel fsck implementation\n"
24              "  -v                      Be verbose\n"
25              "  -h, --help              Display this help and exit\n"
26              "Report bugs to <linux-bcachefs@vger.kernel.org>");
27 }
28
29 static void setnonblocking(int fd)
30 {
31         int flags = fcntl(fd, F_GETFL);
32         if (fcntl(fd, F_SETFL, flags|O_NONBLOCK))
33                 die("fcntl error: %m");
34 }
35
36 static int do_splice(int rfd, int wfd)
37 {
38         char buf[4096];
39
40         int r = read(rfd, buf, sizeof(buf));
41         if (r < 0 && errno == EAGAIN)
42                 return 0;
43         if (r < 0)
44                 return r;
45         if (!r)
46                 return 1;
47         if (write(wfd, buf, r) != r)
48                 die("write error");
49         return 0;
50 }
51
52 static int splice_fd_to_stdinout(int fd)
53 {
54         setnonblocking(STDIN_FILENO);
55         setnonblocking(fd);
56
57         while (true) {
58                 fd_set fds;
59
60                 FD_ZERO(&fds);
61                 FD_SET(STDIN_FILENO, &fds);
62                 FD_SET(fd, &fds);
63
64                 select(fd + 1, &fds, NULL, NULL, NULL);
65
66                 int r = do_splice(fd, STDOUT_FILENO) ?:
67                         do_splice(STDIN_FILENO, fd);
68                 if (r)
69                         return r < 0 ? r : 0;
70         }
71
72         return 0;
73 }
74
75 static int fsck_online(const char *dev_path)
76 {
77         int dev_idx;
78         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
79
80         struct bch_ioctl_fsck_online fsck = { 0 };
81
82         int fsck_fd = ioctl(fs.ioctl_fd, BCH_IOCTL_FSCK_ONLINE, &fsck);
83         if (fsck_fd < 0)
84                 die("BCH_IOCTL_FSCK_ONLINE error: %s", bch2_err_str(fsck_fd));
85
86         return splice_fd_to_stdinout(fsck_fd);
87 }
88
89 static void append_opt(struct printbuf *out, const char *opt)
90 {
91         if (out->pos)
92                 prt_char(out, ',');
93         prt_str(out, opt);
94 }
95
96 int cmd_fsck(int argc, char *argv[])
97 {
98         static const struct option longopts[] = {
99                 { "ratelimit_errors",   no_argument,            NULL, 'r' },
100                 { "reconstruct_alloc",  no_argument,            NULL, 'R' },
101                 { "kernel",             no_argument,            NULL, 'k' },
102                 { "help",               no_argument,            NULL, 'h' },
103                 { NULL }
104         };
105         bool kernel = false;
106         int opt, ret = 0;
107         struct printbuf opts_str = PRINTBUF;
108
109         append_opt(&opts_str, "degraded");
110         append_opt(&opts_str, "fsck");
111         append_opt(&opts_str, "fix_errors=ask");
112         append_opt(&opts_str, "read_only");
113
114         while ((opt = getopt_long(argc, argv,
115                                   "apynfo:rRkvh",
116                                   longopts, NULL)) != -1)
117                 switch (opt) {
118                 case 'a': /* outdated alias for -p */
119                 case 'p':
120                 case 'y':
121                         append_opt(&opts_str, "fix_errors=yes");
122                         break;
123                 case 'n':
124                         append_opt(&opts_str, "nochanges");
125                         append_opt(&opts_str, "fix_errors=no");
126                         break;
127                 case 'f':
128                         /* force check, even if filesystem marked clean: */
129                         break;
130                 case 'o':
131                         append_opt(&opts_str, optarg);
132                         break;
133                 case 'r':
134                         append_opt(&opts_str, "ratelimit_errors");
135                         break;
136                 case 'R':
137                         append_opt(&opts_str, "reconstruct_alloc");
138                         break;
139                 case 'k':
140                         kernel = true;
141                         break;
142                 case 'v':
143                         append_opt(&opts_str, "verbose");
144                         break;
145                 case 'h':
146                         fsck_usage();
147                         exit(16);
148                 }
149         args_shift(optind);
150
151         if (!argc) {
152                 fprintf(stderr, "Please supply device(s) to check\n");
153                 exit(8);
154         }
155
156         darray_str devs = get_or_split_cmdline_devs(argc, argv);
157
158         if (!kernel) {
159                 struct bch_opts opts = bch2_opts_empty();
160                 ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
161                 if (ret)
162                         return ret;
163
164                 darray_for_each(devs, i)
165                         if (dev_mounted(*i))
166                                 return fsck_online(*i);
167
168                 struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
169                 if (IS_ERR(c))
170                         exit(8);
171
172                 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
173                         fprintf(stderr, "%s: errors fixed\n", c->name);
174                         ret |= 1;
175                 }
176                 if (test_bit(BCH_FS_error, &c->flags)) {
177                         fprintf(stderr, "%s: still has errors\n", c->name);
178                         ret |= 4;
179                 }
180
181                 bch2_fs_stop(c);
182         } else {
183                 struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
184                                                              sizeof(u64) * devs.nr, 1);
185
186                 fsck->opts = (unsigned long)opts_str.buf;
187                 darray_for_each(devs, i)
188                         fsck->devs[i - devs.data] = (unsigned long) *i;
189                 fsck->nr_devs = devs.nr;
190
191                 int ctl_fd = bcachectl_open();
192
193                 int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
194                 if (fsck_fd < 0)
195                         die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
196
197                 ret = splice_fd_to_stdinout(fsck_fd);
198                 free(fsck);
199         }
200
201         printbuf_exit(&opts_str);
202         return ret;
203 }