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