#include "libbcachefs/error.h"
#include "libbcachefs.h"
#include "libbcachefs/super.h"
+#include "libbcachefs/super-io.h"
#include "tools-util.h"
static void fsck_usage(void)
select(fd + 1, &fds, NULL, NULL, NULL);
- int r = do_splice(fd, STDOUT_FILENO) ?:
- do_splice(STDIN_FILENO, fd);
+ int r = do_splice(fd, STDOUT_FILENO);
+ if (r < 0)
+ return r;
if (r)
- return r < 0 ? r : 0;
+ break;
+
+ r = do_splice(STDIN_FILENO, fd);
+ if (r < 0)
+ return r;
}
- return 0;
+ return close(fd);
}
static int fsck_online(const char *dev_path)
prt_str(out, opt);
}
+static bool should_use_kernel_fsck(darray_str devs)
+{
+ unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK)
+ ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
+ : 0;
+
+ if (!kernel_version)
+ return false;
+
+ if (kernel_version == bcachefs_metadata_version_current)
+ return false;
+
+ struct bch_opts opts = bch2_opts_empty();
+ opt_set(opts, nostart, true);
+ opt_set(opts, noexcl, true);
+ opt_set(opts, nochanges, true);
+ opt_set(opts, read_only, true);
+
+ struct bch_fs *c = bch2_fs_open(devs.data, devs.nr, opts);
+ if (IS_ERR(c))
+ return false;
+
+ bool ret = ((bcachefs_metadata_version_current < kernel_version &&
+ kernel_version <= c->sb.version) ||
+ (c->sb.version <= kernel_version &&
+ kernel_version < bcachefs_metadata_version_current));
+
+ if (ret) {
+ struct printbuf buf = PRINTBUF;
+
+ prt_str(&buf, "fsck binary is version ");
+ bch2_version_to_text(&buf, bcachefs_metadata_version_current);
+ prt_str(&buf, " but filesystem is ");
+ bch2_version_to_text(&buf, c->sb.version);
+ prt_str(&buf, " and kernel is ");
+ bch2_version_to_text(&buf, kernel_version);
+ prt_str(&buf, ", using kernel fsck\n");
+
+ printf("%s", buf.buf);
+ printbuf_exit(&buf);
+ }
+
+ bch2_fs_stop(c);
+
+ return ret;
+}
+
int cmd_fsck(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "ratelimit_errors", no_argument, NULL, 'r' },
{ "reconstruct_alloc", no_argument, NULL, 'R' },
{ "kernel", no_argument, NULL, 'k' },
+ { "no-kernel", no_argument, NULL, 'K' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
- bool kernel = false;
+ int kernel = -1; /* unset */
int opt, ret = 0;
struct printbuf opts_str = PRINTBUF;
case 'k':
kernel = true;
break;
+ case 'K':
+ kernel = false;
+ break;
case 'v':
append_opt(&opts_str, "verbose");
break;
darray_str devs = get_or_split_cmdline_devs(argc, argv);
- if (!kernel) {
- struct bch_opts opts = bch2_opts_empty();
+ int kernel_probed = kernel;
+ if (kernel_probed < 0)
+ kernel_probed = should_use_kernel_fsck(devs);
+
+ struct bch_opts opts = bch2_opts_empty();
+
+ if (kernel_probed) {
+ struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
+ sizeof(u64) * devs.nr, 1);
+
+ fsck->opts = (unsigned long)opts_str.buf;
+ darray_for_each(devs, i)
+ fsck->devs[i - devs.data] = (unsigned long) *i;
+ fsck->nr_devs = devs.nr;
+
+ int ctl_fd = bcachectl_open();
+ int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
+ free(fsck);
+
+ if (fsck_fd < 0 && kernel < 0)
+ goto userland_fsck;
+
+ if (fsck_fd < 0)
+ die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
+
+ ret = splice_fd_to_stdinout(fsck_fd);
+ } else {
+userland_fsck:
ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);
if (ret)
return ret;
}
bch2_fs_stop(c);
- } else {
- struct bch_ioctl_fsck_offline *fsck = calloc(sizeof(*fsck) +
- sizeof(u64) * devs.nr, 1);
-
- fsck->opts = (unsigned long)opts_str.buf;
- darray_for_each(devs, i)
- fsck->devs[i - devs.data] = (unsigned long) *i;
- fsck->nr_devs = devs.nr;
-
- int ctl_fd = bcachectl_open();
-
- int fsck_fd = ioctl(ctl_fd, BCH_IOCTL_FSCK_OFFLINE, fsck);
- if (fsck_fd < 0)
- die("BCH_IOCTL_FSCK_OFFLINE error: %s", bch2_err_str(fsck_fd));
-
- ret = splice_fd_to_stdinout(fsck_fd);
- free(fsck);
}
printbuf_exit(&opts_str);