diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2023-08-03 20:42:37 +0200 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-22 23:10:09 +0200 |
commit | 6c6439650ec913c83d48055da63b8f204075afb7 (patch) | |
tree | b5aaaab5c386ffbb6a029890601638ed4ee2512d /fs/bcachefs/bkey.c | |
parent | bcachefs: bcachefs_metadata_version_deleted_inodes (diff) | |
download | linux-6c6439650ec913c83d48055da63b8f204075afb7.tar.xz linux-6c6439650ec913c83d48055da63b8f204075afb7.zip |
bcachefs: bkey_format helper improvements
- add a to_text() method for bkey_format
- convert bch2_bkey_format_validate() to modern error message style,
where we pass a printbuf for the error string instead of returning a
static string
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/bkey.c')
-rw-r--r-- | fs/bcachefs/bkey.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/fs/bcachefs/bkey.c b/fs/bcachefs/bkey.c index ee7ba700e75f..b7b77f459724 100644 --- a/fs/bcachefs/bkey.c +++ b/fs/bcachefs/bkey.c @@ -608,12 +608,15 @@ struct bkey_format bch2_bkey_format_done(struct bkey_format_state *s) return ret; } -const char *bch2_bkey_format_validate(struct bkey_format *f) +int bch2_bkey_format_validate(struct bkey_format *f, struct printbuf *err) { unsigned i, bits = KEY_PACKED_BITS_START; - if (f->nr_fields != BKEY_NR_FIELDS) - return "incorrect number of fields"; + if (f->nr_fields != BKEY_NR_FIELDS) { + prt_printf(err, "incorrect number of fields: got %u, should be %u", + f->nr_fields, BKEY_NR_FIELDS); + return -BCH_ERR_invalid; + } /* * Verify that the packed format can't represent fields larger than the @@ -628,16 +631,35 @@ const char *bch2_bkey_format_validate(struct bkey_format *f) u64 field_offset = le64_to_cpu(f->field_offset[i]); if (packed_max + field_offset < packed_max || - packed_max + field_offset > unpacked_max) - return "field too large"; + packed_max + field_offset > unpacked_max) { + prt_printf(err, "field %u too large: %llu + %llu > %llu", + i, packed_max, field_offset, unpacked_max); + return -BCH_ERR_invalid; + } bits += f->bits_per_field[i]; } - if (f->key_u64s != DIV_ROUND_UP(bits, 64)) - return "incorrect key_u64s"; + if (f->key_u64s != DIV_ROUND_UP(bits, 64)) { + prt_printf(err, "incorrect key_u64s: got %u, should be %u", + f->key_u64s, DIV_ROUND_UP(bits, 64)); + return -BCH_ERR_invalid; + } + + return 0; +} - return NULL; +void bch2_bkey_format_to_text(struct printbuf *out, const struct bkey_format *f) +{ + prt_printf(out, "u64s %u fields ", f->key_u64s); + + for (unsigned i = 0; i < ARRAY_SIZE(f->bits_per_field); i++) { + if (i) + prt_str(out, ", "); + prt_printf(out, "%u:%llu", + f->bits_per_field[i], + le64_to_cpu(f->field_offset[i])); + } } /* |