bcachefs: Add an .invalid method for bch2_btree_ptr_v2
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 22 Mar 2021 21:23:30 +0000 (17:23 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:57 +0000 (17:08 -0400)
It was using the method for btree_ptr_v1, but that wasn't checking all
the fields.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/extents.c
fs/bcachefs/extents.h

index 2be49f443eb01345a1f0fc572e9f6ebf7cf43bc0..7ac3d75876557089b6b8a5736414683f3ffb6645 100644 (file)
@@ -157,7 +157,7 @@ int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
 
 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
 {
-       if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
+       if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX)
                return "value too big";
 
        return bch2_bkey_ptrs_invalid(c, k);
@@ -169,6 +169,22 @@ void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
        bch2_bkey_ptrs_to_text(out, c, k);
 }
 
+const char *bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
+{
+       struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
+
+       if (bkey_val_bytes(k.k) <= sizeof(*bp.v))
+               return "value too small";
+
+       if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
+               return "value too big";
+
+       if (bp.v->min_key.snapshot)
+               return "invalid min_key.snapshot";
+
+       return bch2_bkey_ptrs_invalid(c, k);
+}
+
 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
                            struct bkey_s_c k)
 {
index 2ee50a24501e8b0437dd2c943791802f5f7ebdab..c8069dfb90ffceae47f4b019f9b3d9a29ada12ee 100644 (file)
@@ -371,6 +371,7 @@ const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
 void bch2_btree_ptr_to_text(struct printbuf *, struct bch_fs *,
                            struct bkey_s_c);
 
+const char *bch2_btree_ptr_v2_invalid(const struct bch_fs *, struct bkey_s_c);
 void bch2_btree_ptr_v2_to_text(struct printbuf *, struct bch_fs *,
                            struct bkey_s_c);
 void bch2_btree_ptr_v2_compat(enum btree_id, unsigned, unsigned,
@@ -383,7 +384,7 @@ void bch2_btree_ptr_v2_compat(enum btree_id, unsigned, unsigned,
 }
 
 #define bch2_bkey_ops_btree_ptr_v2 (struct bkey_ops) {         \
-       .key_invalid    = bch2_btree_ptr_invalid,               \
+       .key_invalid    = bch2_btree_ptr_v2_invalid,            \
        .val_to_text    = bch2_btree_ptr_v2_to_text,            \
        .swab           = bch2_ptr_swab,                        \
        .compat         = bch2_btree_ptr_v2_compat,             \