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;
17 * key_invalid: checks validity of @k, returns 0 if good or -EINVAL if bad. If
18 * invalid, entire key will be deleted.
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.
24 int (*key_invalid)(struct bch_fs *c, struct bkey_s_c k,
25 enum bkey_invalid_flags flags, struct printbuf *err);
26 void (*val_to_text)(struct printbuf *, struct bch_fs *,
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, unsigned);
33 void (*compat)(enum btree_id id, unsigned version,
34 unsigned big_endian, int write,
37 /* Size of value type when first created: */
38 unsigned min_val_size;
41 extern const struct bkey_ops bch2_bkey_ops[];
43 static inline const struct bkey_ops *bch2_bkey_type_ops(enum bch_bkey_type type)
45 return likely(type < KEY_TYPE_MAX)
46 ? &bch2_bkey_ops[type]
47 : &bch2_bkey_null_ops;
50 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c,
51 enum bkey_invalid_flags, struct printbuf *);
52 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
53 enum bkey_invalid_flags, struct printbuf *);
54 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
55 enum bkey_invalid_flags, struct printbuf *);
56 int bch2_bkey_in_btree_node(struct bch_fs *, struct btree *,
57 struct bkey_s_c, struct printbuf *);
59 void bch2_bpos_to_text(struct printbuf *, struct bpos);
60 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
61 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
63 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
66 void bch2_bkey_swab_val(struct bkey_s);
68 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
70 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
72 return l->type == r->type &&
73 !bversion_cmp(l->version, r->version) &&
74 bpos_eq(l->p, bkey_start_pos(r));
77 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
79 enum btree_update_flags {
80 __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE = __BTREE_ITER_FLAGS_END,
81 __BTREE_UPDATE_SNAPSHOT_WHITEOUT_CHECKS_DONE,
82 __BTREE_UPDATE_NOJOURNAL,
83 __BTREE_UPDATE_KEY_CACHE_RECLAIM,
85 __BTREE_TRIGGER_NORUN,
86 __BTREE_TRIGGER_TRANSACTIONAL,
87 __BTREE_TRIGGER_ATOMIC,
89 __BTREE_TRIGGER_INSERT,
90 __BTREE_TRIGGER_OVERWRITE,
91 __BTREE_TRIGGER_BUCKET_INVALIDATE,
94 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
95 #define BTREE_UPDATE_SNAPSHOT_WHITEOUT_CHECKS_DONE \
96 (1U << __BTREE_UPDATE_SNAPSHOT_WHITEOUT_CHECKS_DONE)
97 #define BTREE_UPDATE_NOJOURNAL (1U << __BTREE_UPDATE_NOJOURNAL)
98 #define BTREE_UPDATE_KEY_CACHE_RECLAIM (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
100 /* Don't run triggers at all */
101 #define BTREE_TRIGGER_NORUN (1U << __BTREE_TRIGGER_NORUN)
104 * If set, we're running transactional triggers as part of a transaction commit:
105 * triggers may generate new updates
107 * If cleared, and either BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE are set,
108 * we're running atomic triggers during a transaction commit: we have our
109 * journal reservation, we're holding btree node write locks, and we know the
110 * transaction is going to commit (returning an error here is a fatal error,
111 * causing us to go emergency read-only)
113 #define BTREE_TRIGGER_TRANSACTIONAL (1U << __BTREE_TRIGGER_TRANSACTIONAL)
114 #define BTREE_TRIGGER_ATOMIC (1U << __BTREE_TRIGGER_ATOMIC)
116 /* We're in gc/fsck: running triggers to recalculate e.g. disk usage */
117 #define BTREE_TRIGGER_GC (1U << __BTREE_TRIGGER_GC)
119 /* @new is entering the btree */
120 #define BTREE_TRIGGER_INSERT (1U << __BTREE_TRIGGER_INSERT)
122 /* @old is leaving the btree */
123 #define BTREE_TRIGGER_OVERWRITE (1U << __BTREE_TRIGGER_OVERWRITE)
125 /* signal from bucket invalidate path to alloc trigger */
126 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
128 static inline int bch2_key_trigger(struct btree_trans *trans,
129 enum btree_id btree, unsigned level,
130 struct bkey_s_c old, struct bkey_s new,
133 const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type);
136 ? ops->trigger(trans, btree, level, old, new, flags)
140 static inline int bch2_key_trigger_old(struct btree_trans *trans,
141 enum btree_id btree_id, unsigned level,
142 struct bkey_s_c old, unsigned flags)
144 struct bkey_i deleted;
146 bkey_init(&deleted.k);
147 deleted.k.p = old.k->p;
149 return bch2_key_trigger(trans, btree_id, level, old, bkey_i_to_s(&deleted),
150 BTREE_TRIGGER_OVERWRITE|flags);
153 static inline int bch2_key_trigger_new(struct btree_trans *trans,
154 enum btree_id btree_id, unsigned level,
155 struct bkey_s new, unsigned flags)
157 struct bkey_i deleted;
159 bkey_init(&deleted.k);
160 deleted.k.p = new.k->p;
162 return bch2_key_trigger(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
163 BTREE_TRIGGER_INSERT|flags);
166 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
168 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
169 int, struct bkey_format *, struct bkey_packed *);
171 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
172 unsigned version, unsigned big_endian,
174 struct bkey_format *f,
175 struct bkey_packed *k)
177 if (version < bcachefs_metadata_version_current ||
178 big_endian != CPU_BIG_ENDIAN)
179 __bch2_bkey_compat(level, btree_id, version,
180 big_endian, write, f, k);
184 #endif /* _BCACHEFS_BKEY_METHODS_H */