]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/bkey_methods.c
Update bcachefs sources to 99175e5712 bcachefs: Fix bch2_check_discard_freespace_key()
[bcachefs-tools-debian] / libbcachefs / bkey_methods.c
index bb74e6f7d427299395fee739c399623ca7f8d960..79f3fbe925d5290235e15c6e4a44d270ba4c277b 100644 (file)
@@ -24,7 +24,7 @@ const char * const bch2_bkey_types[] = {
 };
 
 static int deleted_key_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                              int rw, struct printbuf *err)
+                              unsigned flags, struct printbuf *err)
 {
        return 0;
 }
@@ -38,7 +38,7 @@ static int deleted_key_invalid(const struct bch_fs *c, struct bkey_s_c k,
 })
 
 static int empty_val_key_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                                int rw, struct printbuf *err)
+                                unsigned flags, struct printbuf *err)
 {
        if (bkey_val_bytes(k.k)) {
                prt_printf(err, "incorrect value size (%zu != 0)",
@@ -54,19 +54,14 @@ static int empty_val_key_invalid(const struct bch_fs *c, struct bkey_s_c k,
 })
 
 static int key_type_cookie_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                                  int rw, struct printbuf *err)
+                                  unsigned flags, struct printbuf *err)
 {
-       if (bkey_val_bytes(k.k) != sizeof(struct bch_cookie)) {
-               prt_printf(err, "incorrect value size (%zu != %zu)",
-                      bkey_val_bytes(k.k), sizeof(struct bch_cookie));
-               return -BCH_ERR_invalid_bkey;
-       }
-
        return 0;
 }
 
 #define bch2_bkey_ops_cookie ((struct bkey_ops) {      \
-       .key_invalid = key_type_cookie_invalid,         \
+       .key_invalid    = key_type_cookie_invalid,      \
+       .min_val_size   = 8,                            \
 })
 
 #define bch2_bkey_ops_hash_whiteout ((struct bkey_ops) {\
@@ -74,7 +69,7 @@ static int key_type_cookie_invalid(const struct bch_fs *c, struct bkey_s_c k,
 })
 
 static int key_type_inline_data_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                                       int rw, struct printbuf *err)
+                                       unsigned flags, struct printbuf *err)
 {
        return 0;
 }
@@ -95,7 +90,7 @@ static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c,
 })
 
 static int key_type_set_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                               int rw, struct printbuf *err)
+                               unsigned flags, struct printbuf *err)
 {
        if (bkey_val_bytes(k.k)) {
                prt_printf(err, "incorrect value size (%zu != %zu)",
@@ -124,14 +119,24 @@ const struct bkey_ops bch2_bkey_ops[] = {
 };
 
 int bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k,
-                         int rw, struct printbuf *err)
+                         unsigned flags, struct printbuf *err)
 {
+       const struct bkey_ops *ops;
+
        if (k.k->type >= KEY_TYPE_MAX) {
                prt_printf(err, "invalid type (%u >= %u)", k.k->type, KEY_TYPE_MAX);
                return -BCH_ERR_invalid_bkey;
        }
 
-       return bch2_bkey_ops[k.k->type].key_invalid(c, k, rw, err);
+       ops = &bch2_bkey_ops[k.k->type];
+
+       if (bkey_val_bytes(k.k) < ops->min_val_size) {
+               prt_printf(err, "bad val size (%zu < %u)",
+                          bkey_val_bytes(k.k), ops->min_val_size);
+               return -BCH_ERR_invalid_bkey;
+       }
+
+       return ops->key_invalid(c, k, flags, err);
 }
 
 static unsigned bch2_key_types_allowed[] = {
@@ -186,7 +191,7 @@ static unsigned bch2_key_types_allowed[] = {
                (1U << KEY_TYPE_snapshot),
        [BKEY_TYPE_lru] =
                (1U << KEY_TYPE_deleted)|
-               (1U << KEY_TYPE_lru),
+               (1U << KEY_TYPE_set),
        [BKEY_TYPE_freespace] =
                (1U << KEY_TYPE_deleted)|
                (1U << KEY_TYPE_set),
@@ -199,6 +204,9 @@ static unsigned bch2_key_types_allowed[] = {
        [BKEY_TYPE_bucket_gens] =
                (1U << KEY_TYPE_deleted)|
                (1U << KEY_TYPE_bucket_gens),
+       [BKEY_TYPE_snapshot_trees] =
+               (1U << KEY_TYPE_deleted)|
+               (1U << KEY_TYPE_snapshot_tree),
        [BKEY_TYPE_btree] =
                (1U << KEY_TYPE_deleted)|
                (1U << KEY_TYPE_btree_ptr)|
@@ -207,7 +215,7 @@ static unsigned bch2_key_types_allowed[] = {
 
 int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
                        enum btree_node_type type,
-                       int rw, struct printbuf *err)
+                       unsigned flags, struct printbuf *err)
 {
        if (k.k->u64s < BKEY_U64s) {
                prt_printf(err, "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s);
@@ -216,7 +224,7 @@ int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
 
        if (!(bch2_key_types_allowed[type] & (1U << k.k->type))) {
                prt_printf(err, "invalid key type for btree %s (%s)",
-                          bch2_btree_ids[type], bch2_bkey_types[type]);
+                          bch2_btree_ids[type], bch2_bkey_types[k.k->type]);
                return -BCH_ERR_invalid_bkey;
        }
 
@@ -263,10 +271,10 @@ int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
 
 int bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
                      enum btree_node_type type,
-                     int rw, struct printbuf *err)
+                     unsigned flags, struct printbuf *err)
 {
-       return __bch2_bkey_invalid(c, k, type, rw, err) ?:
-               bch2_bkey_val_invalid(c, k, rw, err);
+       return __bch2_bkey_invalid(c, k, type, flags, err) ?:
+               bch2_bkey_val_invalid(c, k, flags, err);
 }
 
 int bch2_bkey_in_btree_node(struct btree *b, struct bkey_s_c k,
@@ -374,7 +382,11 @@ bool bch2_bkey_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
 {
        const struct bkey_ops *ops = &bch2_bkey_ops[l.k->type];
 
-       return bch2_bkey_maybe_mergable(l.k, r.k) && ops->key_merge(c, l, r);
+       return bch2_bkey_maybe_mergable(l.k, r.k) &&
+               (u64) l.k->size + r.k->size <= KEY_SIZE_MAX &&
+               bch2_bkey_ops[l.k->type].key_merge &&
+               !bch2_key_merging_disabled &&
+               ops->key_merge(c, l, r);
 }
 
 static const struct old_bkey_type {