summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2024-02-11 21:42:14 -0500
committerKent Overstreet <kent.overstreet@linux.dev>2024-02-12 15:52:26 -0500
commitcd4cbfc6273b930125f9f56a7367ec8610dd9a49 (patch)
tree4ff25ce91fbe7bbab6906f4bf8e2d3b051581230
parentb03f7b606fb11c717d64389f15a010a74d7b9481 (diff)
fsck: Automatically use kernel fsck when better version match
To avoid expensive version upgrades and downgrades - use the kernel version of fsck when it's availale and a better match. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r--c_src/cmd_fsck.c64
1 files changed, 61 insertions, 3 deletions
diff --git a/c_src/cmd_fsck.c b/c_src/cmd_fsck.c
index 262ac1bc..79f52007 100644
--- a/c_src/cmd_fsck.c
+++ b/c_src/cmd_fsck.c
@@ -6,6 +6,7 @@
#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)
@@ -68,10 +69,13 @@ static int splice_fd_to_stdinout(int fd)
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)
return r < 0 ? r : 0;
+
+ r = do_splice(STDIN_FILENO, fd);
+ if (r < 0)
+ return r;
}
return 0;
@@ -98,16 +102,64 @@ static void append_opt(struct printbuf *out, const char *opt)
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;
@@ -144,6 +196,9 @@ int cmd_fsck(int argc, char *argv[])
case 'k':
kernel = true;
break;
+ case 'K':
+ kernel = false;
+ break;
case 'v':
append_opt(&opts_str, "verbose");
break;
@@ -160,6 +215,9 @@ int cmd_fsck(int argc, char *argv[])
darray_str devs = get_or_split_cmdline_devs(argc, argv);
+ if (kernel < 0)
+ kernel = should_use_kernel_fsck(devs);
+
if (!kernel) {
struct bch_opts opts = bch2_opts_empty();
ret = bch2_parse_mount_opts(NULL, &opts, opts_str.buf);