]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
Update bcachefs sources to 84f132d569 bcachefs: fsck: Break walk_inode() up into...
[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         /* Size of value type when first created: */
39         unsigned        min_val_size;
40 };
41
42 extern const struct bkey_ops bch2_bkey_ops[];
43
44 #define BKEY_INVALID_FROM_JOURNAL               (1 << 1)
45
46 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c, unsigned, struct printbuf *);
47 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
48                         enum btree_node_type, unsigned, struct printbuf *);
49 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
50                       enum btree_node_type, unsigned, struct printbuf *);
51 int bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c, struct printbuf *);
52
53 void bch2_bpos_to_text(struct printbuf *, struct bpos);
54 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
55 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
56                       struct bkey_s_c);
57 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
58                            struct bkey_s_c);
59
60 void bch2_bkey_swab_val(struct bkey_s);
61
62 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
63
64 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
65 {
66         return l->type == r->type &&
67                 !bversion_cmp(l->version, r->version) &&
68                 bpos_eq(l->p, bkey_start_pos(r));
69 }
70
71 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
72
73 static inline int bch2_mark_key(struct btree_trans *trans,
74                 enum btree_id btree, unsigned level,
75                 struct bkey_s_c old, struct bkey_s_c new,
76                 unsigned flags)
77 {
78         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new.k->type];
79
80         return ops->atomic_trigger
81                 ? ops->atomic_trigger(trans, btree, level, old, new, flags)
82                 : 0;
83 }
84
85 enum btree_update_flags {
86         __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE = __BTREE_ITER_FLAGS_END,
87         __BTREE_UPDATE_NOJOURNAL,
88         __BTREE_UPDATE_KEY_CACHE_RECLAIM,
89
90         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
91
92         __BTREE_TRIGGER_INSERT,
93         __BTREE_TRIGGER_OVERWRITE,
94
95         __BTREE_TRIGGER_GC,
96         __BTREE_TRIGGER_BUCKET_INVALIDATE,
97         __BTREE_TRIGGER_NOATOMIC,
98 };
99
100 #define BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE (1U << __BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE)
101 #define BTREE_UPDATE_NOJOURNAL          (1U << __BTREE_UPDATE_NOJOURNAL)
102 #define BTREE_UPDATE_KEY_CACHE_RECLAIM  (1U << __BTREE_UPDATE_KEY_CACHE_RECLAIM)
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 static inline int bch2_trans_mark_key(struct btree_trans *trans,
114                                       enum btree_id btree_id, unsigned level,
115                                       struct bkey_s_c old, struct bkey_i *new,
116                                       unsigned flags)
117 {
118         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new->k.type];
119
120         return ops->trans_trigger
121                 ? ops->trans_trigger(trans, btree_id, level, old, new, flags)
122                 : 0;
123 }
124
125 static inline int bch2_trans_mark_old(struct btree_trans *trans,
126                                       enum btree_id btree_id, unsigned level,
127                                       struct bkey_s_c old, unsigned flags)
128 {
129         struct bkey_i deleted;
130
131         bkey_init(&deleted.k);
132         deleted.k.p = old.k->p;
133
134         return bch2_trans_mark_key(trans, btree_id, level, old, &deleted,
135                                    BTREE_TRIGGER_OVERWRITE|flags);
136 }
137
138 static inline int bch2_trans_mark_new(struct btree_trans *trans,
139                                       enum btree_id btree_id, unsigned level,
140                                       struct bkey_i *new, unsigned flags)
141 {
142         struct bkey_i deleted;
143
144         bkey_init(&deleted.k);
145         deleted.k.p = new->k.p;
146
147         return bch2_trans_mark_key(trans, btree_id, level, bkey_i_to_s_c(&deleted), new,
148                                    BTREE_TRIGGER_INSERT|flags);
149 }
150
151 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
152
153 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
154                         int, struct bkey_format *, struct bkey_packed *);
155
156 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
157                                unsigned version, unsigned big_endian,
158                                int write,
159                                struct bkey_format *f,
160                                struct bkey_packed *k)
161 {
162         if (version < bcachefs_metadata_version_current ||
163             big_endian != CPU_BIG_ENDIAN)
164                 __bch2_bkey_compat(level, btree_id, version,
165                                    big_endian, write, f, k);
166
167 }
168
169 #endif /* _BCACHEFS_BKEY_METHODS_H */