]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bkey_methods.h
Update bcachefs sources to e027cf9aa0 fixup! bcachefs: Defer checking of alloc -...
[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                                        int rw, 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 *, struct bkey_s_c,
31                                          struct bkey_i *, unsigned);
32         int             (*atomic_trigger)(struct btree_trans *, struct bkey_s_c,
33                                           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 int bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
42 int __bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
43                         enum btree_node_type, int, struct printbuf *);
44 int bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
45                       enum btree_node_type, int, struct printbuf *);
46 int bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c, struct printbuf *);
47
48 void bch2_bpos_to_text(struct printbuf *, struct bpos);
49 void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
50 void bch2_val_to_text(struct printbuf *, struct bch_fs *,
51                       struct bkey_s_c);
52 void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
53                            struct bkey_s_c);
54
55 void bch2_bkey_swab_val(struct bkey_s);
56
57 bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
58
59 static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
60 {
61         return l->type == r->type &&
62                 !bversion_cmp(l->version, r->version) &&
63                 !bpos_cmp(l->p, bkey_start_pos(r)) &&
64                 (u64) l->size + r->size <= KEY_SIZE_MAX &&
65                 bch2_bkey_ops[l->type].key_merge &&
66                 !bch2_key_merging_disabled;
67 }
68
69 bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
70
71 static inline int bch2_mark_key(struct btree_trans *trans,
72                   struct bkey_s_c old,
73                   struct bkey_s_c new,
74                   unsigned flags)
75 {
76         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new.k->type];
77
78         return ops->atomic_trigger
79                 ? ops->atomic_trigger(trans, old, new, flags)
80                 : 0;
81 }
82
83 static inline int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c old,
84                         struct bkey_i *new, unsigned flags)
85 {
86         const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new->k.type];
87
88         return ops->trans_trigger
89                 ? ops->trans_trigger(trans, old, new, flags)
90                 : 0;
91 }
92
93 void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
94
95 void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
96                         int, struct bkey_format *, struct bkey_packed *);
97
98 static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
99                                unsigned version, unsigned big_endian,
100                                int write,
101                                struct bkey_format *f,
102                                struct bkey_packed *k)
103 {
104         if (version < bcachefs_metadata_version_current ||
105             big_endian != CPU_BIG_ENDIAN)
106                 __bch2_bkey_compat(level, btree_id, version,
107                                    big_endian, write, f, k);
108
109 }
110
111 #endif /* _BCACHEFS_BKEY_METHODS_H */