xref: /linux/fs/bcachefs/bkey_methods.h (revision 55d0969c451159cff86949b38c39171cab962069)
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_validate: 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_validate)(struct bch_fs *c, struct bkey_s_c k,
25 					enum bch_validate_flags flags);
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_validate(struct bch_fs *, struct bkey_s_c, enum bch_validate_flags);
52 int __bch2_bkey_validate(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
53 			 enum bch_validate_flags);
54 int bch2_bkey_validate(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
55 		       enum bch_validate_flags);
56 int bch2_bkey_in_btree_node(struct bch_fs *, struct btree *, struct bkey_s_c,
57 			    enum bch_validate_flags);
58 
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 *,
62 		      struct bkey_s_c);
63 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
64 			   struct bkey_s_c);
65 
66 void bch2_bkey_swab_val(struct bkey_s);
67 
68 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
69 
70 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
71 {
72 	return l->type == r->type &&
73 		!bversion_cmp(l->bversion, r->bversion) &&
74 		bpos_eq(l->p, bkey_start_pos(r));
75 }
76 
77 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
78 
79 static inline int bch2_key_trigger(struct btree_trans *trans,
80 		enum btree_id btree, unsigned level,
81 		struct bkey_s_c old, struct bkey_s new,
82 		enum btree_iter_update_trigger_flags flags)
83 {
84 	const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type);
85 
86 	return ops->trigger
87 		? ops->trigger(trans, btree, level, old, new, flags)
88 		: 0;
89 }
90 
91 static inline int bch2_key_trigger_old(struct btree_trans *trans,
92 			enum btree_id btree_id, unsigned level,
93 			struct bkey_s_c old,
94 			enum btree_iter_update_trigger_flags flags)
95 {
96 	struct bkey_i deleted;
97 
98 	bkey_init(&deleted.k);
99 	deleted.k.p = old.k->p;
100 
101 	return bch2_key_trigger(trans, btree_id, level, old, bkey_i_to_s(&deleted),
102 				BTREE_TRIGGER_overwrite|flags);
103 }
104 
105 static inline int bch2_key_trigger_new(struct btree_trans *trans,
106 			enum btree_id btree_id, unsigned level,
107 			struct bkey_s new,
108 			enum btree_iter_update_trigger_flags flags)
109 {
110 	struct bkey_i deleted;
111 
112 	bkey_init(&deleted.k);
113 	deleted.k.p = new.k->p;
114 
115 	return bch2_key_trigger(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
116 				BTREE_TRIGGER_insert|flags);
117 }
118 
119 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
120 
121 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
122 			int, struct bkey_format *, struct bkey_packed *);
123 
124 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
125 			       unsigned version, unsigned big_endian,
126 			       int write,
127 			       struct bkey_format *f,
128 			       struct bkey_packed *k)
129 {
130 	if (version < bcachefs_metadata_version_current ||
131 	    big_endian != CPU_BIG_ENDIAN ||
132 	    IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
133 		__bch2_bkey_compat(level, btree_id, version,
134 				   big_endian, write, f, k);
135 
136 }
137 
138 #endif /* _BCACHEFS_BKEY_METHODS_H */
139