]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
f4e60d2e6677278efdb81976eba1806b9f0cb08c
[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, 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 *);
64
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 *,
68                       struct bkey_s_c);
69 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
70                            struct bkey_s_c);
71
72 void bch2_bkey_swab_val(struct bkey_s);
73
74 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
75
76 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
77 {
78         return l->type == r->type &&
79                 !bversion_cmp(l->version, r->version) &&
80                 bpos_eq(l->p, bkey_start_pos(r));
81 }
82
83 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
84
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,
88                 unsigned flags)
89 {
90         const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new.k->type);
91
92         return ops->atomic_trigger
93                 ? ops->atomic_trigger(trans, btree, level, old, new, flags)
94                 : 0;
95 }
96
97 enum btree_update_flags {
98         __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE = __BTREE_ITER_FLAGS_END,
99         __BTREE_UPDATE_NOJOURNAL,
100         __BTREE_UPDATE_PREJOURNAL,
101         __BTREE_UPDATE_KEY_CACHE_RECLAIM,
102
103         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
104
105         __BTREE_TRIGGER_INSERT,
106         __BTREE_TRIGGER_OVERWRITE,
107
108         __BTREE_TRIGGER_GC,
109         __BTREE_TRIGGER_BUCKET_INVALIDATE,
110         __BTREE_TRIGGER_NOATOMIC,
111 };
112
113 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
114 #define BTREE_UPDATE_NOJOURNAL          (1U << __BTREE_UPDATE_NOJOURNAL)
115 #define BTREE_UPDATE_PREJOURNAL         (1U << __BTREE_UPDATE_PREJOURNAL)
116 #define BTREE_UPDATE_KEY_CACHE_RECLAIM  (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
117
118 #define BTREE_TRIGGER_NORUN             (1U << __BTREE_TRIGGER_NORUN)
119
120 #define BTREE_TRIGGER_INSERT            (1U << __BTREE_TRIGGER_INSERT)
121 #define BTREE_TRIGGER_OVERWRITE         (1U << __BTREE_TRIGGER_OVERWRITE)
122
123 #define BTREE_TRIGGER_GC                (1U << __BTREE_TRIGGER_GC)
124 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
125 #define BTREE_TRIGGER_NOATOMIC          (1U << __BTREE_TRIGGER_NOATOMIC)
126
127 #define BTREE_TRIGGER_WANTS_OLD_AND_NEW         \
128         ((1U << KEY_TYPE_alloc)|                \
129          (1U << KEY_TYPE_alloc_v2)|             \
130          (1U << KEY_TYPE_alloc_v3)|             \
131          (1U << KEY_TYPE_alloc_v4)|             \
132          (1U << KEY_TYPE_stripe)|               \
133          (1U << KEY_TYPE_inode)|                \
134          (1U << KEY_TYPE_inode_v2)|             \
135          (1U << KEY_TYPE_snapshot))
136
137 static inline int bch2_trans_mark_key(struct btree_trans *trans,
138                                       enum btree_id btree_id, unsigned level,
139                                       struct bkey_s_c old, struct bkey_i *new,
140                                       unsigned flags)
141 {
142         const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new->k.type);
143
144         return ops->trans_trigger
145                 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
146                 : 0;
147 }
148
149 static inline int bch2_trans_mark_old(struct btree_trans *trans,
150                                       enum btree_id btree_id, unsigned level,
151                                       struct bkey_s_c old, unsigned flags)
152 {
153         struct bkey_i deleted;
154
155         bkey_init(&deleted.k);
156         deleted.k.p = old.k->p;
157
158         return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
159                                    BTREE_TRIGGER_OVERWRITE|flags);
160 }
161
162 static inline int bch2_trans_mark_new(struct btree_trans *trans,
163                                       enum btree_id btree_id, unsigned level,
164                                       struct bkey_i *new, unsigned flags)
165 {
166         struct bkey_i deleted;
167
168         bkey_init(&deleted.k);
169         deleted.k.p = new->k.p;
170
171         return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
172                                    BTREE_TRIGGER_INSERT|flags);
173 }
174
175 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
176
177 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
178                         int, struct bkey_format *, struct bkey_packed *);
179
180 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
181                                unsigned version, unsigned big_endian,
182                                int write,
183                                struct bkey_format *f,
184                                struct bkey_packed *k)
185 {
186         if (version < bcachefs_metadata_version_current ||
187             big_endian != CPU_BIG_ENDIAN)
188                 __bch2_bkey_compat(level, btree_id, version,
189                                    big_endian, write, f, k);
190
191 }
192
193 #endif /* _BCACHEFS_BKEY_METHODS_H */