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