summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2025-03-28 11:38:55 -0400
committerKent Overstreet <kent.overstreet@linux.dev>2025-03-28 11:38:55 -0400
commit7c47145f6cdf9826f9dc24c935195b58268b1ec6 (patch)
treef63e7951cbddcde3db8e0058e70dc0604abc4a74
parent396545c2ea073144d5cd905bc8bcfb0080549f6d (diff)
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>
-rw-r--r--c_src/cmd_fs.c5
-rw-r--r--c_src/cmd_fsck.c4
-rw-r--r--c_src/libbcachefs.h12
3 files changed, 12 insertions, 9 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 struct format_opts format_opts_default()
+static inline unsigned bcachefs_kernel_version(void)
{
- unsigned version = !access( "/sys/module/bcachefs/parameters/version", R_OK)
+ return !access("/sys/module/bcachefs/parameters/version", R_OK)
? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
- : bcachefs_metadata_version_current;
+ : 0;
+}
+static inline struct format_opts format_opts_default()
+{
return (struct format_opts) {
- .version = version,
+ .version = bcachefs_kernel_version() ?:
+ bcachefs_metadata_version_current,
.superblock_size = SUPERBLOCK_SIZE_DEFAULT,
};
}