]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
Update bcachefs sources to 33a60d9b05 bcachefs: Assorted fixes for clang
[bcachefs-tools-debian] / libbcachefs / bkey_methods.h
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 enum bkey_invalid_flags {
17         BKEY_INVALID_WRITE              = (1U << 0),
18         BKEY_INVALID_COMMIT             = (1U << 1),
19         BKEY_INVALID_JOURNAL            = (1U << 2),
20 };
21
22 /*
23  * key_invalid: checks validity of @k, returns 0 if good or -EINVAL if bad. If
24  * invalid, entire key will be deleted.
25  *
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.
28  */
29 struct bkey_ops {
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 *,
33                                        struct bkey_s_c);
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,
43                                   struct bkey_s);
44
45         /* Size of value type when first created: */
46         unsigned        min_val_size;
47 };
48
49 extern const struct bkey_ops bch2_bkey_ops[];
50
51 static inline const struct bkey_ops *bch2_bkey_type_ops(enum bch_bkey_type type)
52 {
53         return likely(type < KEY_TYPE_MAX)
54                 ? &bch2_bkey_ops[type]
55                 : &bch2_bkey_null_ops;
56 }
57
58 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c,
59                           enum bkey_invalid_flags, struct printbuf *);
60 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
61                         enum bkey_invalid_flags, struct printbuf *);
62 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c, enum btree_node_type,
63                       enum bkey_invalid_flags, struct printbuf *);
64 int bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c, struct printbuf *);
65
66 void bch2_bpos_to_text(struct printbuf *, struct bpos);
67 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
68 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
69                       struct bkey_s_c);
70 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
71                            struct bkey_s_c);
72
73 void bch2_bkey_swab_val(struct bkey_s);
74
75 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
76
77 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
78 {
79         return l->type == r->type &&
80                 !bversion_cmp(l->version, r->version) &&
81                 bpos_eq(l->p, bkey_start_pos(r));
82 }
83
84 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
85
86 static inline int bch2_mark_key(struct btree_trans *trans,
87                 enum btree_id btree, unsigned level,
88                 struct bkey_s_c old, struct bkey_s_c new,
89                 unsigned flags)
90 {
91         const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type);
92
93         return ops->atomic_trigger
94                 ? ops->atomic_trigger(trans, btree, level, old, new, flags)
95                 : 0;
96 }
97
98 enum btree_update_flags {
99         __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE = __BTREE_ITER_FLAGS_END,
100         __BTREE_UPDATE_NOJOURNAL,
101         __BTREE_UPDATE_PREJOURNAL,
102         __BTREE_UPDATE_KEY_CACHE_RECLAIM,
103
104         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
105
106         __BTREE_TRIGGER_INSERT,
107         __BTREE_TRIGGER_OVERWRITE,
108
109         __BTREE_TRIGGER_GC,
110         __BTREE_TRIGGER_BUCKET_INVALIDATE,
111         __BTREE_TRIGGER_NOATOMIC,
112 };
113
114 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
115 #define BTREE_UPDATE_NOJOURNAL          (1U << __BTREE_UPDATE_NOJOURNAL)
116 #define BTREE_UPDATE_PREJOURNAL         (1U << __BTREE_UPDATE_PREJOURNAL)
117 #define BTREE_UPDATE_KEY_CACHE_RECLAIM  (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
118
119 #define BTREE_TRIGGER_NORUN             (1U << __BTREE_TRIGGER_NORUN)
120
121 #define BTREE_TRIGGER_INSERT            (1U << __BTREE_TRIGGER_INSERT)
122 #define BTREE_TRIGGER_OVERWRITE         (1U << __BTREE_TRIGGER_OVERWRITE)
123
124 #define BTREE_TRIGGER_GC                (1U << __BTREE_TRIGGER_GC)
125 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
126 #define BTREE_TRIGGER_NOATOMIC          (1U << __BTREE_TRIGGER_NOATOMIC)
127
128 #define BTREE_TRIGGER_WANTS_OLD_AND_NEW         \
129         ((1U << KEY_TYPE_alloc)|                \
130          (1U << KEY_TYPE_alloc_v2)|             \
131          (1U << KEY_TYPE_alloc_v3)|             \
132          (1U << KEY_TYPE_alloc_v4)|             \
133          (1U << KEY_TYPE_stripe)|               \
134          (1U << KEY_TYPE_inode)|                \
135          (1U << KEY_TYPE_inode_v2)|             \
136          (1U << KEY_TYPE_snapshot))
137
138 static inline int bch2_trans_mark_key(struct btree_trans *trans,
139                                       enum btree_id btree_id, unsigned level,
140                                       struct bkey_s_c old, struct bkey_i *new,
141                                       unsigned flags)
142 {
143         const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new->k.type);
144
145         return ops->trans_trigger
146                 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
147                 : 0;
148 }
149
150 static inline int bch2_trans_mark_old(struct btree_trans *trans,
151                                       enum btree_id btree_id, unsigned level,
152                                       struct bkey_s_c old, unsigned flags)
153 {
154         struct bkey_i deleted;
155
156         bkey_init(&deleted.k);
157         deleted.k.p = old.k->p;
158
159         return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
160                                    BTREE_TRIGGER_OVERWRITE|flags);
161 }
162
163 static inline int bch2_trans_mark_new(struct btree_trans *trans,
164                                       enum btree_id btree_id, unsigned level,
165                                       struct bkey_i *new, unsigned flags)
166 {
167         struct bkey_i deleted;
168
169         bkey_init(&deleted.k);
170         deleted.k.p = new->k.p;
171
172         return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
173                                    BTREE_TRIGGER_INSERT|flags);
174 }
175
176 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
177
178 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
179                         int, struct bkey_format *, struct bkey_packed *);
180
181 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
182                                unsigned version, unsigned big_endian,
183                                int write,
184                                struct bkey_format *f,
185                                struct bkey_packed *k)
186 {
187         if (version < bcachefs_metadata_version_current ||
188             big_endian != CPU_BIG_ENDIAN)
189                 __bch2_bkey_compat(level, btree_id, version,
190                                    big_endian, write, f, k);
191
192 }
193
194 #endif /* _BCACHEFS_BKEY_METHODS_H */