1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_METHODS_H
3 #define _BCACHEFS_BKEY_METHODS_H
13 extern const char * const bch2_bkey_types[];
14 extern const struct bkey_ops bch2_bkey_null_ops;
16 enum bkey_invalid_flags {
17 BKEY_INVALID_WRITE = (1U << 0),
18 BKEY_INVALID_COMMIT = (1U << 1),
19 BKEY_INVALID_JOURNAL = (1U << 2),
23 * key_invalid: checks validity of @k, returns 0 if good or -EINVAL if bad. If
24 * invalid, entire key will be deleted.
26 * When invalid, error string is returned via @err. @rw indicates whether key is
27 * being read or written; more aggressive checks can be enabled when rw == WRITE.
30 int (*key_invalid)(const struct bch_fs *c, struct bkey_s_c k,
31 enum bkey_invalid_flags flags, struct printbuf *err);
32 void (*val_to_text)(struct printbuf *, struct bch_fs *,
34 void (*swab)(struct bkey_s);
35 bool (*key_normalize)(struct bch_fs *, struct bkey_s);
36 bool (*key_merge)(struct bch_fs *, struct bkey_s, struct bkey_s_c);
37 int (*trans_trigger)(struct btree_trans *, enum btree_id, unsigned,
38 struct bkey_s_c, struct bkey_i *, unsigned);
39 int (*atomic_trigger)(struct btree_trans *, enum btree_id, unsigned,
40 struct bkey_s_c, struct bkey_s_c, unsigned);
41 void (*compat)(enum btree_id id, unsigned version,
42 unsigned big_endian, int write,
45 /* Size of value type when first created: */
46 unsigned min_val_size;
49 extern const struct bkey_ops bch2_bkey_ops[];
51 static inline const struct bkey_ops *bch2_bkey_type_ops(enum bch_bkey_type type)
53 return likely(type < KEY_TYPE_MAX)
54 ? &bch2_bkey_ops[type]
55 : &bch2_bkey_null_ops;
58 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
59 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
60 enum btree_node_type, unsigned, struct printbuf *);
61 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
62 enum btree_node_type, unsigned, struct printbuf *);
63 int bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c, struct printbuf *);
65 void bch2_bpos_to_text(struct printbuf *, struct bpos);
66 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
67 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
69 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
72 void bch2_bkey_swab_val(struct bkey_s);
74 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
76 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
78 return l->type == r->type &&
79 !bversion_cmp(l->version, r->version) &&
80 bpos_eq(l->p, bkey_start_pos(r));
83 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
85 static inline int bch2_mark_key(struct btree_trans *trans,
86 enum btree_id btree, unsigned level,
87 struct bkey_s_c old, struct bkey_s_c new,
90 const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type);
92 return ops->atomic_trigger
93 ? ops->atomic_trigger(trans, btree, level, old, new, flags)
97 enum btree_update_flags {
98 __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE = __BTREE_ITER_FLAGS_END,
99 __BTREE_UPDATE_NOJOURNAL,
100 __BTREE_UPDATE_KEY_CACHE_RECLAIM,
102 __BTREE_TRIGGER_NORUN, /* Don't run triggers at all */
104 __BTREE_TRIGGER_INSERT,
105 __BTREE_TRIGGER_OVERWRITE,
108 __BTREE_TRIGGER_BUCKET_INVALIDATE,
109 __BTREE_TRIGGER_NOATOMIC,
112 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
113 #define BTREE_UPDATE_NOJOURNAL (1U << __BTREE_UPDATE_NOJOURNAL)
114 #define BTREE_UPDATE_KEY_CACHE_RECLAIM (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
116 #define BTREE_TRIGGER_NORUN (1U << __BTREE_TRIGGER_NORUN)
118 #define BTREE_TRIGGER_INSERT (1U << __BTREE_TRIGGER_INSERT)
119 #define BTREE_TRIGGER_OVERWRITE (1U << __BTREE_TRIGGER_OVERWRITE)
121 #define BTREE_TRIGGER_GC (1U << __BTREE_TRIGGER_GC)
122 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
123 #define BTREE_TRIGGER_NOATOMIC (1U << __BTREE_TRIGGER_NOATOMIC)
125 #define BTREE_TRIGGER_WANTS_OLD_AND_NEW \
126 ((1U << KEY_TYPE_alloc)| \
127 (1U << KEY_TYPE_alloc_v2)| \
128 (1U << KEY_TYPE_alloc_v3)| \
129 (1U << KEY_TYPE_alloc_v4)| \
130 (1U << KEY_TYPE_stripe)| \
131 (1U << KEY_TYPE_inode)| \
132 (1U << KEY_TYPE_inode_v2)| \
133 (1U << KEY_TYPE_snapshot))
135 static inline int bch2_trans_mark_key(struct btree_trans *trans,
136 enum btree_id btree_id, unsigned level,
137 struct bkey_s_c old, struct bkey_i *new,
140 const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new->k.type);
142 return ops->trans_trigger
143 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
147 static inline int bch2_trans_mark_old(struct btree_trans *trans,
148 enum btree_id btree_id, unsigned level,
149 struct bkey_s_c old, unsigned flags)
151 struct bkey_i deleted;
153 bkey_init(&deleted.k);
154 deleted.k.p = old.k->p;
156 return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
157 BTREE_TRIGGER_OVERWRITE|flags);
160 static inline int bch2_trans_mark_new(struct btree_trans *trans,
161 enum btree_id btree_id, unsigned level,
162 struct bkey_i *new, unsigned flags)
164 struct bkey_i deleted;
166 bkey_init(&deleted.k);
167 deleted.k.p = new->k.p;
169 return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
170 BTREE_TRIGGER_INSERT|flags);
173 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
175 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
176 int, struct bkey_format *, struct bkey_packed *);
178 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
179 unsigned version, unsigned big_endian,
181 struct bkey_format *f,
182 struct bkey_packed *k)
184 if (version < bcachefs_metadata_version_current ||
185 big_endian != CPU_BIG_ENDIAN)
186 __bch2_bkey_compat(level, btree_id, version,
187 big_endian, write, f, k);
191 #endif /* _BCACHEFS_BKEY_METHODS_H */