1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BKEY_METHODS_H 3 #define _BCACHEFS_BKEY_METHODS_H 4 5 #include "bkey.h" 6 7 struct bch_fs; 8 struct btree; 9 struct btree_trans; 10 struct bkey; 11 enum btree_node_type; 12 13 extern const char * const bch2_bkey_types[]; 14 extern const struct bkey_ops bch2_bkey_null_ops; 15 16 /* 17 * key_invalid: checks validity of @k, returns 0 if good or -EINVAL if bad. If 18 * invalid, entire key will be deleted. 19 * 20 * When invalid, error string is returned via @err. @rw indicates whether key is 21 * being read or written; more aggressive checks can be enabled when rw == WRITE. 22 */ 23 struct bkey_ops { 24 int (*key_invalid)(struct bch_fs *c, struct bkey_s_c k, 25 enum bch_validate_flags flags, struct printbuf *err); 26 void (*val_to_text)(struct printbuf *, struct bch_fs *, 27 struct bkey_s_c); 28 void (*swab)(struct bkey_s); 29 bool (*key_normalize)(struct bch_fs *, struct bkey_s); 30 bool (*key_merge)(struct bch_fs *, struct bkey_s, struct bkey_s_c); 31 int (*trigger)(struct btree_trans *, enum btree_id, unsigned, 32 struct bkey_s_c, struct bkey_s, 33 enum btree_iter_update_trigger_flags); 34 void (*compat)(enum btree_id id, unsigned version, 35 unsigned big_endian, int write, 36 struct bkey_s); 37 38 /* Size of value type when first created: */ 39 unsigned min_val_size; 40 }; 41 42 extern const struct bkey_ops bch2_bkey_ops[]; 43 44 static inline const struct bkey_ops *bch2_bkey_type_ops(enum bch_bkey_type type) 45 { 46 return likely(type < KEY_TYPE_MAX) 47 ? &bch2_bkey_ops[type] 48 : &bch2_bkey_null_ops; 49 } 50 51 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c, 52 enum bch_validate_flags, struct printbuf *); 53 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type, 54 enum bch_validate_flags, struct printbuf *); 55 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type, 56 enum bch_validate_flags, struct printbuf *); 57 int bch2_bkey_in_btree_node(struct bch_fs *, struct btree *, 58 struct bkey_s_c, struct printbuf *); 59 60 void bch2_bpos_to_text(struct printbuf *, struct bpos); 61 void bch2_bkey_to_text(struct printbuf *, const struct bkey *); 62 void bch2_val_to_text(struct printbuf *, struct bch_fs *, 63 struct bkey_s_c); 64 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *, 65 struct bkey_s_c); 66 67 void bch2_bkey_swab_val(struct bkey_s); 68 69 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s); 70 71 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r) 72 { 73 return l->type == r->type && 74 !bversion_cmp(l->version, r->version) && 75 bpos_eq(l->p, bkey_start_pos(r)); 76 } 77 78 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c); 79 80 static inline int bch2_key_trigger(struct btree_trans *trans, 81 enum btree_id btree, unsigned level, 82 struct bkey_s_c old, struct bkey_s new, 83 enum btree_iter_update_trigger_flags flags) 84 { 85 const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type); 86 87 return ops->trigger 88 ? ops->trigger(trans, btree, level, old, new, flags) 89 : 0; 90 } 91 92 static inline int bch2_key_trigger_old(struct btree_trans *trans, 93 enum btree_id btree_id, unsigned level, 94 struct bkey_s_c old, 95 enum btree_iter_update_trigger_flags flags) 96 { 97 struct bkey_i deleted; 98 99 bkey_init(&deleted.k); 100 deleted.k.p = old.k->p; 101 102 return bch2_key_trigger(trans, btree_id, level, old, bkey_i_to_s(&deleted), 103 BTREE_TRIGGER_overwrite|flags); 104 } 105 106 static inline int bch2_key_trigger_new(struct btree_trans *trans, 107 enum btree_id btree_id, unsigned level, 108 struct bkey_s new, 109 enum btree_iter_update_trigger_flags flags) 110 { 111 struct bkey_i deleted; 112 113 bkey_init(&deleted.k); 114 deleted.k.p = new.k->p; 115 116 return bch2_key_trigger(trans, btree_id, level, bkey_i_to_s_c(&deleted), new, 117 BTREE_TRIGGER_insert|flags); 118 } 119 120 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int); 121 122 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned, 123 int, struct bkey_format *, struct bkey_packed *); 124 125 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id, 126 unsigned version, unsigned big_endian, 127 int write, 128 struct bkey_format *f, 129 struct bkey_packed *k) 130 { 131 if (version < bcachefs_metadata_version_current || 132 big_endian != CPU_BIG_ENDIAN || 133 IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) 134 __bch2_bkey_compat(level, btree_id, version, 135 big_endian, write, f, k); 136 137 } 138 139 #endif /* _BCACHEFS_BKEY_METHODS_H */ 140