]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
Update bcachefs sources to fd381c355c bcachefs: Fix a null ptr deref in fsck check_ex...
[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
15 /*
16  * key_invalid: checks validity of @k, returns 0 if good or -EINVAL if bad. If
17  * invalid, entire key will be deleted.
18  *
19  * When invalid, error string is returned via @err. @rw indicates whether key is
20  * being read or written; more aggressive checks can be enabled when rw == WRITE.
21  */
22 struct bkey_ops {
23         int             (*key_invalid)(const struct bch_fs *c, struct bkey_s_c k,
24                                        unsigned flags, struct printbuf *err);
25         void            (*val_to_text)(struct printbuf *, struct bch_fs *,
26                                        struct bkey_s_c);
27         void            (*swab)(struct bkey_s);
28         bool            (*key_normalize)(struct bch_fs *, struct bkey_s);
29         bool            (*key_merge)(struct bch_fs *, struct bkey_s, struct bkey_s_c);
30         int             (*trans_trigger)(struct btree_trans *, enum btree_id, unsigned,
31                                          struct bkey_s_c, struct bkey_i *, unsigned);
32         int             (*atomic_trigger)(struct btree_trans *, enum btree_id, unsigned,
33                                           struct bkey_s_c, struct bkey_s_c, unsigned);
34         void            (*compat)(enum btree_id id, unsigned version,
35                                   unsigned big_endian, int write,
36                                   struct bkey_s);
37 };
38
39 extern const struct bkey_ops bch2_bkey_ops[];
40
41 #define BKEY_INVALID_FROM_JOURNAL               (1 << 1)
42
43 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
44 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
45                         enum btree_node_type, unsigned, struct printbuf *);
46 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
47                       enum btree_node_type, unsigned, struct printbuf *);
48 int bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c, struct printbuf *);
49
50 void bch2_bpos_to_text(struct printbuf *, struct bpos);
51 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
52 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
53                       struct bkey_s_c);
54 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
55                            struct bkey_s_c);
56
57 void bch2_bkey_swab_val(struct bkey_s);
58
59 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
60
61 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
62 {
63         return l->type == r->type &&
64                 !bversion_cmp(l->version, r->version) &&
65                 bpos_eq(l->p, bkey_start_pos(r));
66 }
67
68 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
69
70 static inline int bch2_mark_key(struct btree_trans *trans,
71                 enum btree_id btree, unsigned level,
72                 struct bkey_s_c old, struct bkey_s_c new,
73                 unsigned flags)
74 {
75         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new.k->type];
76
77         return ops->atomic_trigger
78                 ? ops->atomic_trigger(trans, btree, level, old, new, flags)
79                 : 0;
80 }
81
82 enum btree_update_flags {
83         __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
84         __BTREE_UPDATE_NOJOURNAL,
85         __BTREE_UPDATE_KEY_CACHE_RECLAIM,
86         __BTREE_UPDATE_NO_KEY_CACHE_COHERENCY,
87
88         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
89
90         __BTREE_TRIGGER_INSERT,
91         __BTREE_TRIGGER_OVERWRITE,
92
93         __BTREE_TRIGGER_GC,
94         __BTREE_TRIGGER_BUCKET_INVALIDATE,
95         __BTREE_TRIGGER_NOATOMIC,
96 };
97
98 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
99 #define BTREE_UPDATE_NOJOURNAL          (1U << __BTREE_UPDATE_NOJOURNAL)
100 #define BTREE_UPDATE_KEY_CACHE_RECLAIM  (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
101 #define BTREE_UPDATE_NO_KEY_CACHE_COHERENCY     \
102         (1U << __BTREE_UPDATE_NO_KEY_CACHE_COHERENCY)
103
104 #define BTREE_TRIGGER_NORUN             (1U << __BTREE_TRIGGER_NORUN)
105
106 #define BTREE_TRIGGER_INSERT            (1U << __BTREE_TRIGGER_INSERT)
107 #define BTREE_TRIGGER_OVERWRITE         (1U << __BTREE_TRIGGER_OVERWRITE)
108
109 #define BTREE_TRIGGER_GC                (1U << __BTREE_TRIGGER_GC)
110 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
111 #define BTREE_TRIGGER_NOATOMIC          (1U << __BTREE_TRIGGER_NOATOMIC)
112
113 #define BTREE_TRIGGER_WANTS_OLD_AND_NEW         \
114         ((1U << KEY_TYPE_alloc)|                \
115          (1U << KEY_TYPE_alloc_v2)|             \
116          (1U << KEY_TYPE_alloc_v3)|             \
117          (1U << KEY_TYPE_alloc_v4)|             \
118          (1U << KEY_TYPE_stripe)|               \
119          (1U << KEY_TYPE_inode)|                \
120          (1U << KEY_TYPE_inode_v2)|             \
121          (1U << KEY_TYPE_snapshot))
122
123 static inline int bch2_trans_mark_key(struct btree_trans *trans,
124                                       enum btree_id btree_id, unsigned level,
125                                       struct bkey_s_c old, struct bkey_i *new,
126                                       unsigned flags)
127 {
128         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new->k.type];
129
130         return ops->trans_trigger
131                 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
132                 : 0;
133 }
134
135 static inline int bch2_trans_mark_old(struct btree_trans *trans,
136                                       enum btree_id btree_id, unsigned level,
137                                       struct bkey_s_c old, unsigned flags)
138 {
139         struct bkey_i deleted;
140
141         bkey_init(&deleted.k);
142         deleted.k.p = old.k->p;
143
144         return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
145                                    BTREE_TRIGGER_OVERWRITE|flags);
146 }
147
148 static inline int bch2_trans_mark_new(struct btree_trans *trans,
149                                       enum btree_id btree_id, unsigned level,
150                                       struct bkey_i *new, unsigned flags)
151 {
152         struct bkey_i deleted;
153
154         bkey_init(&deleted.k);
155         deleted.k.p = new->k.p;
156
157         return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
158                                    BTREE_TRIGGER_INSERT|flags);
159 }
160
161 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
162
163 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
164                         int, struct bkey_format *, struct bkey_packed *);
165
166 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
167                                unsigned version, unsigned big_endian,
168                                int write,
169                                struct bkey_format *f,
170                                struct bkey_packed *k)
171 {
172         if (version < bcachefs_metadata_version_current ||
173             big_endian != CPU_BIG_ENDIAN)
174                 __bch2_bkey_compat(level, btree_id, version,
175                                    big_endian, write, f, k);
176
177 }
178
179 #endif /* _BCACHEFS_BKEY_METHODS_H */