From 7c47145f6cdf9826f9dc24c935195b58268b1ec6 Mon Sep 17 00:00:00 2001 From: Kent Overstreet <kent.overstreet@linux.dev> Date: Fri, 28 Mar 2025 11:38:55 -0400 Subject: [PATCH] cmd_fs_usage: Fix kernel version check This needed an access() check, like the other uses, instead of exiting if it can't be read. Factor out a small common helper for this. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> --- c_src/cmd_fs.c | 5 +++-- c_src/cmd_fsck.c | 4 +--- c_src/libbcachefs.h | 14 +++++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/c_src/cmd_fs.c b/c_src/cmd_fs.c index cda9a755..06f797b9 100644 --- a/c_src/cmd_fs.c +++ b/c_src/cmd_fs.c @@ -239,9 +239,10 @@ static void accounting_sort(darray_accounting_p *sorted, static void accounting_swab_if_old(struct bch_ioctl_query_accounting *in) { - u64 kernel_version = read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version"); + unsigned kernel_version = bcachefs_kernel_version(); - if (kernel_version < bcachefs_metadata_version_disk_accounting_big_endian) + if (kernel_version && + kernel_version < bcachefs_metadata_version_disk_accounting_big_endian) for (struct bkey_i_accounting *a = in->accounting; a < (struct bkey_i_accounting *) ((u64 *) in->accounting + in->accounting_u64s); a = bkey_i_to_accounting(bkey_next(&a->k_i))) diff --git a/c_src/cmd_fsck.c b/c_src/cmd_fsck.c index 6166328c..06f131bc 100644 --- a/c_src/cmd_fsck.c +++ b/c_src/cmd_fsck.c @@ -116,9 +116,7 @@ static bool should_use_kernel_fsck(darray_str devs) { system("modprobe bcachefs"); - unsigned kernel_version = !access("/sys/module/bcachefs/parameters/version", R_OK) - ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version") - : 0; + unsigned kernel_version = bcachefs_kernel_version(); if (!kernel_version) return false; diff --git a/c_src/libbcachefs.h b/c_src/libbcachefs.h index 619bbbd5..93a86db7 100644 --- a/c_src/libbcachefs.h +++ b/c_src/libbcachefs.h @@ -45,14 +45,18 @@ struct format_opts { char *source; }; +static inline unsigned bcachefs_kernel_version(void) +{ + return !access("/sys/module/bcachefs/parameters/version", R_OK) + ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version") + : 0; +} + static inline struct format_opts format_opts_default() { - unsigned version = !access( "/sys/module/bcachefs/parameters/version", R_OK) - ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version") - : bcachefs_metadata_version_current; - return (struct format_opts) { - .version = version, + .version = bcachefs_kernel_version() ?: + bcachefs_metadata_version_current, .superblock_size = SUPERBLOCK_SIZE_DEFAULT, }; }