]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/bkey_methods.c
Update bcachefs sources to 50847e296b34 bcachefs: Check subvol <-> inode pointers...
[bcachefs-tools-debian] / libbcachefs / bkey_methods.c
index 36e0c5152b4793b390d37847f8e84dba0d05b88d..5e52684764eb14de4d8433abd5954a829648440b 100644 (file)
@@ -1,7 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
+#include "backpointers.h"
 #include "bkey_methods.h"
+#include "btree_cache.h"
 #include "btree_types.h"
 #include "alloc_background.h"
 #include "dirent.h"
@@ -9,8 +11,12 @@
 #include "error.h"
 #include "extents.h"
 #include "inode.h"
+#include "io_misc.h"
+#include "lru.h"
 #include "quota.h"
 #include "reflink.h"
+#include "snapshot.h"
+#include "subvolume.h"
 #include "xattr.h"
 
 const char * const bch2_bkey_types[] = {
@@ -20,179 +26,271 @@ const char * const bch2_bkey_types[] = {
        NULL
 };
 
-static const char *deleted_key_invalid(const struct bch_fs *c,
-                                       struct bkey_s_c k)
+static int deleted_key_invalid(struct bch_fs *c, struct bkey_s_c k,
+                              enum bkey_invalid_flags flags, struct printbuf *err)
 {
-       return NULL;
+       return 0;
 }
 
-#define bch2_bkey_ops_deleted (struct bkey_ops) {      \
+#define bch2_bkey_ops_deleted ((struct bkey_ops) {     \
        .key_invalid = deleted_key_invalid,             \
-}
+})
 
-#define bch2_bkey_ops_discard (struct bkey_ops) {      \
+#define bch2_bkey_ops_whiteout ((struct bkey_ops) {    \
        .key_invalid = deleted_key_invalid,             \
-}
+})
 
-static const char *empty_val_key_invalid(const struct bch_fs *c, struct bkey_s_c k)
+static int empty_val_key_invalid(struct bch_fs *c, struct bkey_s_c k,
+                                enum bkey_invalid_flags flags, struct printbuf *err)
 {
-       if (bkey_val_bytes(k.k))
-               return "value size should be zero";
+       int ret = 0;
 
-       return NULL;
+       bkey_fsck_err_on(bkey_val_bytes(k.k), c, err,
+                        bkey_val_size_nonzero,
+                        "incorrect value size (%zu != 0)",
+                        bkey_val_bytes(k.k));
+fsck_err:
+       return ret;
 }
 
-#define bch2_bkey_ops_error (struct bkey_ops) {                \
+#define bch2_bkey_ops_error ((struct bkey_ops) {       \
        .key_invalid = empty_val_key_invalid,           \
+})
+
+static int key_type_cookie_invalid(struct bch_fs *c, struct bkey_s_c k,
+                                  enum bkey_invalid_flags flags, struct printbuf *err)
+{
+       return 0;
 }
 
-static const char *key_type_cookie_invalid(const struct bch_fs *c,
-                                          struct bkey_s_c k)
+static void key_type_cookie_to_text(struct printbuf *out, struct bch_fs *c,
+                                   struct bkey_s_c k)
 {
-       if (bkey_val_bytes(k.k) != sizeof(struct bch_cookie))
-               return "incorrect value size";
+       struct bkey_s_c_cookie ck = bkey_s_c_to_cookie(k);
 
-       return NULL;
+       prt_printf(out, "%llu", le64_to_cpu(ck.v->cookie));
 }
 
-#define bch2_bkey_ops_cookie (struct bkey_ops) {       \
-       .key_invalid = key_type_cookie_invalid,         \
-}
+#define bch2_bkey_ops_cookie ((struct bkey_ops) {      \
+       .key_invalid    = key_type_cookie_invalid,      \
+       .val_to_text    = key_type_cookie_to_text,      \
+       .min_val_size   = 8,                            \
+})
 
-#define bch2_bkey_ops_whiteout (struct bkey_ops) {     \
+#define bch2_bkey_ops_hash_whiteout ((struct bkey_ops) {\
        .key_invalid = empty_val_key_invalid,           \
-}
+})
 
-static const char *key_type_inline_data_invalid(const struct bch_fs *c,
-                                          struct bkey_s_c k)
+static int key_type_inline_data_invalid(struct bch_fs *c, struct bkey_s_c k,
+                                       enum bkey_invalid_flags flags, struct printbuf *err)
 {
-       return NULL;
+       return 0;
 }
 
 static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c,
                                         struct bkey_s_c k)
 {
-       pr_buf(out, "(%zu bytes)", bkey_val_bytes(k.k));
+       struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
+       unsigned datalen = bkey_inline_data_bytes(k.k);
+
+       prt_printf(out, "datalen %u: %*phN",
+              datalen, min(datalen, 32U), d.v->data);
 }
 
-#define bch2_bkey_ops_inline_data (struct bkey_ops) {  \
+#define bch2_bkey_ops_inline_data ((struct bkey_ops) { \
        .key_invalid    = key_type_inline_data_invalid, \
        .val_to_text    = key_type_inline_data_to_text, \
+})
+
+static bool key_type_set_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
+{
+       bch2_key_resize(l.k, l.k->size + r.k->size);
+       return true;
 }
 
-static const struct bkey_ops bch2_bkey_ops[] = {
+#define bch2_bkey_ops_set ((struct bkey_ops) {         \
+       .key_invalid    = empty_val_key_invalid,        \
+       .key_merge      = key_type_set_merge,           \
+})
+
+const struct bkey_ops bch2_bkey_ops[] = {
 #define x(name, nr) [KEY_TYPE_##name]  = bch2_bkey_ops_##name,
        BCH_BKEY_TYPES()
 #undef x
 };
 
-const char *bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k)
-{
-       if (k.k->type >= KEY_TYPE_MAX)
-               return "invalid type";
-
-       return bch2_bkey_ops[k.k->type].key_invalid(c, k);
-}
+const struct bkey_ops bch2_bkey_null_ops = {
+};
 
-const char *__bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
-                               enum btree_node_type type)
+int bch2_bkey_val_invalid(struct bch_fs *c, struct bkey_s_c k,
+                         enum bkey_invalid_flags flags,
+                         struct printbuf *err)
 {
-       if (k.k->u64s < BKEY_U64s)
-               return "u64s too small";
-
-       if (type == BKEY_TYPE_BTREE &&
-           bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
-               return "value too big";
-
-       if (btree_node_type_is_extents(type)) {
-               if ((k.k->size == 0) != bkey_deleted(k.k))
-                       return "bad size field";
-
-               if (k.k->size > k.k->p.offset)
-                       return "size greater than offset";
-       } else {
-               if (k.k->size)
-                       return "nonzero size field";
-       }
+       const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
+       int ret = 0;
 
-       if (k.k->p.snapshot)
-               return "nonzero snapshot";
+       bkey_fsck_err_on(bkey_val_bytes(k.k) < ops->min_val_size, c, err,
+                        bkey_val_size_too_small,
+                        "bad val size (%zu < %u)",
+                        bkey_val_bytes(k.k), ops->min_val_size);
 
-       if (type != BKEY_TYPE_BTREE &&
-           !bkey_cmp(k.k->p, POS_MAX))
-               return "POS_MAX key";
+       if (!ops->key_invalid)
+               return 0;
 
-       return NULL;
+       ret = ops->key_invalid(c, k, flags, err);
+fsck_err:
+       return ret;
 }
 
-const char *bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
-                             enum btree_node_type type)
+static u64 bch2_key_types_allowed[] = {
+       [BKEY_TYPE_btree] =
+               BIT_ULL(KEY_TYPE_deleted)|
+               BIT_ULL(KEY_TYPE_btree_ptr)|
+               BIT_ULL(KEY_TYPE_btree_ptr_v2),
+#define x(name, nr, flags, keys)       [BKEY_TYPE_##name] = BIT_ULL(KEY_TYPE_deleted)|keys,
+       BCH_BTREE_IDS()
+#undef x
+};
+
+const char *bch2_btree_node_type_str(enum btree_node_type type)
 {
-       return __bch2_bkey_invalid(c, k, type) ?:
-               bch2_bkey_val_invalid(c, k);
+       return type == BKEY_TYPE_btree ? "internal btree node" : bch2_btree_id_str(type - 1);
 }
 
-const char *bch2_bkey_in_btree_node(struct btree *b, struct bkey_s_c k)
+int __bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
+                       enum btree_node_type type,
+                       enum bkey_invalid_flags flags,
+                       struct printbuf *err)
 {
-       if (bkey_cmp(k.k->p, b->data->min_key) < 0)
-               return "key before start of btree node";
+       int ret = 0;
+
+       bkey_fsck_err_on(k.k->u64s < BKEY_U64s, c, err,
+                        bkey_u64s_too_small,
+                        "u64s too small (%u < %zu)", k.k->u64s, BKEY_U64s);
+
+       if (type >= BKEY_TYPE_NR)
+               return 0;
+
+       bkey_fsck_err_on((flags & BKEY_INVALID_COMMIT) &&
+                        !(bch2_key_types_allowed[type] & BIT_ULL(k.k->type)), c, err,
+                        bkey_invalid_type_for_btree,
+                        "invalid key type for btree %s (%s)",
+                        bch2_btree_node_type_str(type), bch2_bkey_types[k.k->type]);
+
+       if (btree_node_type_is_extents(type) && !bkey_whiteout(k.k)) {
+               bkey_fsck_err_on(k.k->size == 0, c, err,
+                                bkey_extent_size_zero,
+                                "size == 0");
+
+               bkey_fsck_err_on(k.k->size > k.k->p.offset, c, err,
+                                bkey_extent_size_greater_than_offset,
+                                "size greater than offset (%u > %llu)",
+                                k.k->size, k.k->p.offset);
+       } else {
+               bkey_fsck_err_on(k.k->size, c, err,
+                                bkey_size_nonzero,
+                                "size != 0");
+       }
 
-       if (bkey_cmp(k.k->p, b->data->max_key) > 0)
-               return "key past end of btree node";
+       if (type != BKEY_TYPE_btree) {
+               enum btree_id btree = type - 1;
+
+               if (btree_type_has_snapshots(btree)) {
+                       bkey_fsck_err_on(!k.k->p.snapshot, c, err,
+                                        bkey_snapshot_zero,
+                                        "snapshot == 0");
+               } else if (!btree_type_has_snapshot_field(btree)) {
+                       bkey_fsck_err_on(k.k->p.snapshot, c, err,
+                                        bkey_snapshot_nonzero,
+                                        "nonzero snapshot");
+               } else {
+                       /*
+                        * btree uses snapshot field but it's not required to be
+                        * nonzero
+                        */
+               }
 
-       return NULL;
+               bkey_fsck_err_on(bkey_eq(k.k->p, POS_MAX), c, err,
+                                bkey_at_pos_max,
+                                "key at POS_MAX");
+       }
+fsck_err:
+       return ret;
 }
 
-void bch2_bkey_debugcheck(struct bch_fs *c, struct btree *b, struct bkey_s_c k)
+int bch2_bkey_invalid(struct bch_fs *c, struct bkey_s_c k,
+                     enum btree_node_type type,
+                     enum bkey_invalid_flags flags,
+                     struct printbuf *err)
 {
-       const struct bkey_ops *ops = &bch2_bkey_ops[k.k->type];
-       const char *invalid;
-
-       BUG_ON(!k.k->u64s);
+       return __bch2_bkey_invalid(c, k, type, flags, err) ?:
+               bch2_bkey_val_invalid(c, k, flags, err);
+}
 
-       invalid = bch2_bkey_invalid(c, k, btree_node_type(b)) ?:
-               bch2_bkey_in_btree_node(b, k);
-       if (invalid) {
-               char buf[160];
+int bch2_bkey_in_btree_node(struct bch_fs *c, struct btree *b,
+                           struct bkey_s_c k, struct printbuf *err)
+{
+       int ret = 0;
 
-               bch2_bkey_val_to_text(&PBUF(buf), c, k);
-               bch2_fs_inconsistent(c, "invalid bkey %s: %s", buf, invalid);
-               return;
-       }
+       bkey_fsck_err_on(bpos_lt(k.k->p, b->data->min_key), c, err,
+                        bkey_before_start_of_btree_node,
+                        "key before start of btree node");
 
-       if (ops->key_debugcheck)
-               ops->key_debugcheck(c, k);
+       bkey_fsck_err_on(bpos_gt(k.k->p, b->data->max_key), c, err,
+                        bkey_after_end_of_btree_node,
+                        "key past end of btree node");
+fsck_err:
+       return ret;
 }
 
 void bch2_bpos_to_text(struct printbuf *out, struct bpos pos)
 {
-       if (!bkey_cmp(pos, POS_MIN))
-               pr_buf(out, "POS_MIN");
-       else if (!bkey_cmp(pos, POS_MAX))
-               pr_buf(out, "POS_MAX");
-       else
-               pr_buf(out, "%llu:%llu", pos.inode, pos.offset);
+       if (bpos_eq(pos, POS_MIN))
+               prt_printf(out, "POS_MIN");
+       else if (bpos_eq(pos, POS_MAX))
+               prt_printf(out, "POS_MAX");
+       else if (bpos_eq(pos, SPOS_MAX))
+               prt_printf(out, "SPOS_MAX");
+       else {
+               if (pos.inode == U64_MAX)
+                       prt_printf(out, "U64_MAX");
+               else
+                       prt_printf(out, "%llu", pos.inode);
+               prt_printf(out, ":");
+               if (pos.offset == U64_MAX)
+                       prt_printf(out, "U64_MAX");
+               else
+                       prt_printf(out, "%llu", pos.offset);
+               prt_printf(out, ":");
+               if (pos.snapshot == U32_MAX)
+                       prt_printf(out, "U32_MAX");
+               else
+                       prt_printf(out, "%u", pos.snapshot);
+       }
 }
 
 void bch2_bkey_to_text(struct printbuf *out, const struct bkey *k)
 {
        if (k) {
-               pr_buf(out, "u64s %u type %s ", k->u64s,
-                      bch2_bkey_types[k->type]);
+               prt_printf(out, "u64s %u type ", k->u64s);
+
+               if (k->type < KEY_TYPE_MAX)
+                       prt_printf(out, "%s ", bch2_bkey_types[k->type]);
+               else
+                       prt_printf(out, "%u ", k->type);
 
                bch2_bpos_to_text(out, k->p);
 
-               pr_buf(out, " snap %u len %u ver %llu",
-                      k->p.snapshot, k->size, k->version.lo);
+               prt_printf(out, " len %u ver %llu", k->size, k->version.lo);
        } else {
-               pr_buf(out, "(null)");
+               prt_printf(out, "(null)");
        }
 }
 
 void bch2_val_to_text(struct printbuf *out, struct bch_fs *c,
                      struct bkey_s_c k)
 {
-       const struct bkey_ops *ops = &bch2_bkey_ops[k.k->type];
+       const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
 
        if (likely(ops->val_to_text))
                ops->val_to_text(out, c, k);
@@ -203,15 +301,15 @@ void bch2_bkey_val_to_text(struct printbuf *out, struct bch_fs *c,
 {
        bch2_bkey_to_text(out, k.k);
 
-       if (k.k) {
-               pr_buf(out, ": ");
+       if (bkey_val_bytes(k.k)) {
+               prt_printf(out, ": ");
                bch2_val_to_text(out, c, k);
        }
 }
 
 void bch2_bkey_swab_val(struct bkey_s k)
 {
-       const struct bkey_ops *ops = &bch2_bkey_ops[k.k->type];
+       const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
 
        if (ops->swab)
                ops->swab(k);
@@ -219,31 +317,22 @@ void bch2_bkey_swab_val(struct bkey_s k)
 
 bool bch2_bkey_normalize(struct bch_fs *c, struct bkey_s k)
 {
-       const struct bkey_ops *ops = &bch2_bkey_ops[k.k->type];
+       const struct bkey_ops *ops = bch2_bkey_type_ops(k.k->type);
 
        return ops->key_normalize
                ? ops->key_normalize(c, k)
                : false;
 }
 
-enum merge_result bch2_bkey_merge(struct bch_fs *c,
-                                 struct bkey_s l, struct bkey_s r)
+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];
-       enum merge_result ret;
-
-       if (key_merging_disabled(c) ||
-           !ops->key_merge ||
-           l.k->type != r.k->type ||
-           bversion_cmp(l.k->version, r.k->version) ||
-           bkey_cmp(l.k->p, bkey_start_pos(r.k)))
-               return BCH_MERGE_NOMERGE;
-
-       ret = ops->key_merge(c, l, r);
+       const struct bkey_ops *ops = bch2_bkey_type_ops(l.k->type);
 
-       if (ret != BCH_MERGE_NOMERGE)
-               l.k->needs_whiteout |= r.k->needs_whiteout;
-       return ret;
+       return ops->key_merge &&
+               bch2_bkey_maybe_mergable(l.k, r.k) &&
+               (u64) l.k->size + r.k->size <= KEY_SIZE_MAX &&
+               !bch2_key_merging_disabled &&
+               ops->key_merge(c, l, r);
 }
 
 static const struct old_bkey_type {
@@ -251,18 +340,18 @@ static const struct old_bkey_type {
        u8              old;
        u8              new;
 } bkey_renumber_table[] = {
-       {BKEY_TYPE_BTREE,       128, KEY_TYPE_btree_ptr         },
-       {BKEY_TYPE_EXTENTS,     128, KEY_TYPE_extent            },
-       {BKEY_TYPE_EXTENTS,     129, KEY_TYPE_extent            },
-       {BKEY_TYPE_EXTENTS,     130, KEY_TYPE_reservation       },
-       {BKEY_TYPE_INODES,      128, KEY_TYPE_inode             },
-       {BKEY_TYPE_INODES,      130, KEY_TYPE_inode_generation  },
-       {BKEY_TYPE_DIRENTS,     128, KEY_TYPE_dirent            },
-       {BKEY_TYPE_DIRENTS,     129, KEY_TYPE_whiteout          },
-       {BKEY_TYPE_XATTRS,      128, KEY_TYPE_xattr             },
-       {BKEY_TYPE_XATTRS,      129, KEY_TYPE_whiteout          },
-       {BKEY_TYPE_ALLOC,       128, KEY_TYPE_alloc             },
-       {BKEY_TYPE_QUOTAS,      128, KEY_TYPE_quota             },
+       {BKEY_TYPE_btree,       128, KEY_TYPE_btree_ptr         },
+       {BKEY_TYPE_extents,     128, KEY_TYPE_extent            },
+       {BKEY_TYPE_extents,     129, KEY_TYPE_extent            },
+       {BKEY_TYPE_extents,     130, KEY_TYPE_reservation       },
+       {BKEY_TYPE_inodes,      128, KEY_TYPE_inode             },
+       {BKEY_TYPE_inodes,      130, KEY_TYPE_inode_generation  },
+       {BKEY_TYPE_dirents,     128, KEY_TYPE_dirent            },
+       {BKEY_TYPE_dirents,     129, KEY_TYPE_hash_whiteout     },
+       {BKEY_TYPE_xattrs,      128, KEY_TYPE_xattr             },
+       {BKEY_TYPE_xattrs,      129, KEY_TYPE_hash_whiteout     },
+       {BKEY_TYPE_alloc,       128, KEY_TYPE_alloc             },
+       {BKEY_TYPE_quotas,      128, KEY_TYPE_quota             },
 };
 
 void bch2_bkey_renumber(enum btree_node_type btree_node_type,
@@ -289,15 +378,15 @@ void __bch2_bkey_compat(unsigned level, enum btree_id btree_id,
 {
        const struct bkey_ops *ops;
        struct bkey uk;
-       struct bkey_s u;
+       unsigned nr_compat = 5;
        int i;
 
        /*
         * Do these operations in reverse order in the write path:
         */
 
-       for (i = 0; i < 4; i++)
-       switch (!write ? i : 3 - i) {
+       for (i = 0; i < nr_compat; i++)
+       switch (!write ? i : nr_compat - 1 - i) {
        case 0:
                if (big_endian != CPU_BIG_ENDIAN)
                        bch2_bkey_swab_key(f, k);
@@ -308,9 +397,10 @@ void __bch2_bkey_compat(unsigned level, enum btree_id btree_id,
                break;
        case 2:
                if (version < bcachefs_metadata_version_inode_btree_change &&
-                   btree_id == BTREE_ID_INODES) {
+                   btree_id == BTREE_ID_inodes) {
                        if (!bkey_packed(k)) {
                                struct bkey_i *u = packed_to_bkey(k);
+
                                swap(u->k.p.inode, u->k.p.offset);
                        } else if (f->bits_per_field[BKEY_FIELD_INODE] &&
                                   f->bits_per_field[BKEY_FIELD_OFFSET]) {
@@ -331,6 +421,30 @@ void __bch2_bkey_compat(unsigned level, enum btree_id btree_id,
                }
                break;
        case 3:
+               if (version < bcachefs_metadata_version_snapshot &&
+                   (level || btree_type_has_snapshots(btree_id))) {
+                       struct bkey_i *u = packed_to_bkey(k);
+
+                       if (u) {
+                               u->k.p.snapshot = write
+                                       ? 0 : U32_MAX;
+                       } else {
+                               u64 min_packed = le64_to_cpu(f->field_offset[BKEY_FIELD_SNAPSHOT]);
+                               u64 max_packed = min_packed +
+                                       ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]);
+
+                               uk = __bch2_bkey_unpack_key(f, k);
+                               uk.p.snapshot = write
+                                       ? min_packed : min_t(u64, U32_MAX, max_packed);
+
+                               BUG_ON(!bch2_bkey_pack_key(k, &uk, f));
+                       }
+               }
+
+               break;
+       case 4: {
+               struct bkey_s u;
+
                if (!bkey_packed(k)) {
                        u = bkey_i_to_s(packed_to_bkey(k));
                } else {
@@ -342,11 +456,12 @@ void __bch2_bkey_compat(unsigned level, enum btree_id btree_id,
                if (big_endian != CPU_BIG_ENDIAN)
                        bch2_bkey_swab_val(u);
 
-               ops = &bch2_bkey_ops[k->type];
+               ops = bch2_bkey_type_ops(k->type);
 
                if (ops->compat)
                        ops->compat(btree_id, version, big_endian, write, u);
                break;
+       }
        default:
                BUG();
        }