]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
Update bcachefs sources to 070ec8d07b bcachefs: Snapshot depth, skiplist fields
[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_KEY_CACHE_RECLAIM,
101
102         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
103
104         __BTREE_TRIGGER_INSERT,
105         __BTREE_TRIGGER_OVERWRITE,
106
107         __BTREE_TRIGGER_GC,
108         __BTREE_TRIGGER_BUCKET_INVALIDATE,
109         __BTREE_TRIGGER_NOATOMIC,
110 };
111
112 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
113 #define BTREE_UPDATE_NOJOURNAL          (1U << __BTREE_UPDATE_NOJOURNAL)
114 #define BTREE_UPDATE_KEY_CACHE_RECLAIM  (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
115
116 #define BTREE_TRIGGER_NORUN             (1U << __BTREE_TRIGGER_NORUN)
117
118 #define BTREE_TRIGGER_INSERT            (1U << __BTREE_TRIGGER_INSERT)
119 #define BTREE_TRIGGER_OVERWRITE         (1U << __BTREE_TRIGGER_OVERWRITE)
120
121 #define BTREE_TRIGGER_GC                (1U << __BTREE_TRIGGER_GC)
122 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
123 #define BTREE_TRIGGER_NOATOMIC          (1U << __BTREE_TRIGGER_NOATOMIC)
124
125 #define BTREE_TRIGGER_WANTS_OLD_AND_NEW         \
126         ((1U << KEY_TYPE_alloc)|                \
127          (1U << KEY_TYPE_alloc_v2)|             \
128          (1U << KEY_TYPE_alloc_v3)|             \
129          (1U << KEY_TYPE_alloc_v4)|             \
130          (1U << KEY_TYPE_stripe)|               \
131          (1U << KEY_TYPE_inode)|                \
132          (1U << KEY_TYPE_inode_v2)|             \
133          (1U << KEY_TYPE_snapshot))
134
135 static inline int bch2_trans_mark_key(struct btree_trans *trans,
136                                       enum btree_id btree_id, unsigned level,
137                                       struct bkey_s_c old, struct bkey_i *new,
138                                       unsigned flags)
139 {
140         const struct bkey_ops *ops = bch2_bkey_type_ops(old.k->type ?: new->k.type);
141
142         return ops->trans_trigger
143                 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
144                 : 0;
145 }
146
147 static inline int bch2_trans_mark_old(struct btree_trans *trans,
148                                       enum btree_id btree_id, unsigned level,
149                                       struct bkey_s_c old, unsigned flags)
150 {
151         struct bkey_i deleted;
152
153         bkey_init(&deleted.k);
154         deleted.k.p = old.k->p;
155
156         return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
157                                    BTREE_TRIGGER_OVERWRITE|flags);
158 }
159
160 static inline int bch2_trans_mark_new(struct btree_trans *trans,
161                                       enum btree_id btree_id, unsigned level,
162                                       struct bkey_i *new, unsigned flags)
163 {
164         struct bkey_i deleted;
165
166         bkey_init(&deleted.k);
167         deleted.k.p = new->k.p;
168
169         return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
170                                    BTREE_TRIGGER_INSERT|flags);
171 }
172
173 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
174
175 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
176                         int, struct bkey_format *, struct bkey_packed *);
177
178 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
179                                unsigned version, unsigned big_endian,
180                                int write,
181                                struct bkey_format *f,
182                                struct bkey_packed *k)
183 {
184         if (version < bcachefs_metadata_version_current ||
185             big_endian != CPU_BIG_ENDIAN)
186                 __bch2_bkey_compat(level, btree_id, version,
187                                    big_endian, write, f, k);
188
189 }
190
191 #endif /* _BCACHEFS_BKEY_METHODS_H */