3 #include "bkey_methods.h"
4 #include "btree_types.h"
11 const struct bkey_ops *bch_bkey_ops[] = {
12 [BKEY_TYPE_EXTENTS] = &bch_bkey_extent_ops,
13 [BKEY_TYPE_INODES] = &bch_bkey_inode_ops,
14 [BKEY_TYPE_DIRENTS] = &bch_bkey_dirent_ops,
15 [BKEY_TYPE_XATTRS] = &bch_bkey_xattr_ops,
16 [BKEY_TYPE_BTREE] = &bch_bkey_btree_ops,
19 /* Returns string indicating reason for being invalid, or NULL if valid: */
20 const char *bkey_invalid(struct cache_set *c, enum bkey_type type,
23 const struct bkey_ops *ops = bch_bkey_ops[type];
25 if (k.k->u64s < BKEY_U64s)
26 return "u64s too small";
29 (bkey_deleted(k.k) || !ops->is_extents))
30 return "nonzero size field";
33 case KEY_TYPE_DELETED:
34 case KEY_TYPE_DISCARD:
38 return bkey_val_bytes(k.k) != 0
39 ? "value size should be zero"
43 return bkey_val_bytes(k.k) != sizeof(struct bch_cookie)
44 ? "incorrect value size"
48 if (k.k->type < KEY_TYPE_GENERIC_NR)
49 return "invalid type";
51 return ops->key_invalid(c, k);
55 const char *btree_bkey_invalid(struct cache_set *c, struct btree *b,
58 if (bkey_cmp(bkey_start_pos(k.k), b->data->min_key) < 0)
59 return "key before start of btree node";
61 if (bkey_cmp(k.k->p, b->data->max_key) > 0)
62 return "key past end of btree node";
65 return "nonzero snapshot";
67 return bkey_invalid(c, btree_node_type(b), k);
70 void bkey_debugcheck(struct cache_set *c, struct btree *b, struct bkey_s_c k)
72 enum bkey_type type = btree_node_type(b);
73 const struct bkey_ops *ops = bch_bkey_ops[type];
78 invalid = btree_bkey_invalid(c, b, k);
82 bch_bkey_val_to_text(c, type, buf, sizeof(buf), k);
83 bch_fs_bug(c, "invalid bkey %s: %s", buf, invalid);
87 if (k.k->type >= KEY_TYPE_GENERIC_NR &&
89 ops->key_debugcheck(c, b, k);
92 void bch_val_to_text(struct cache_set *c, enum bkey_type type,
93 char *buf, size_t size, struct bkey_s_c k)
95 const struct bkey_ops *ops = bch_bkey_ops[type];
97 if (k.k->type >= KEY_TYPE_GENERIC_NR &&
99 ops->val_to_text(c, buf, size, k);
102 void bch_bkey_val_to_text(struct cache_set *c, enum bkey_type type,
103 char *buf, size_t size, struct bkey_s_c k)
105 const struct bkey_ops *ops = bch_bkey_ops[type];
106 char *out = buf, *end = buf + size;
108 out += bch_bkey_to_text(out, end - out, k.k);
110 if (k.k->type >= KEY_TYPE_GENERIC_NR &&
112 out += scnprintf(out, end - out, " -> ");
113 ops->val_to_text(c, out, end - out, k);
117 void bch_bkey_swab(enum bkey_type type,
118 const struct bkey_format *f,
119 struct bkey_packed *k)
121 const struct bkey_ops *ops = bch_bkey_ops[type];
123 bch_bkey_swab_key(f, k);