]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to 070ec8d07b bcachefs: Snapshot depth, skiplist fields
[bcachefs-tools-debian] / libbcachefs / subvolume.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
4
5 #include "darray.h"
6 #include "subvolume_types.h"
7
8 enum bkey_invalid_flags;
9
10 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
11 int bch2_snapshot_tree_invalid(const struct bch_fs *, struct bkey_s_c,
12                                enum bkey_invalid_flags, struct printbuf *);
13
14 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) {        \
15         .key_invalid    = bch2_snapshot_tree_invalid,           \
16         .val_to_text    = bch2_snapshot_tree_to_text,           \
17         .min_val_size   = 8,                                    \
18 })
19
20 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
21
22 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
23 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
24                           enum bkey_invalid_flags, struct printbuf *);
25 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
26                        struct bkey_s_c, struct bkey_s_c, unsigned);
27
28 #define bch2_bkey_ops_snapshot ((struct bkey_ops) {             \
29         .key_invalid    = bch2_snapshot_invalid,                \
30         .val_to_text    = bch2_snapshot_to_text,                \
31         .atomic_trigger = bch2_mark_snapshot,                   \
32         .min_val_size   = 24,                                   \
33 })
34
35 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
36 {
37         return genradix_ptr(&c->snapshots, U32_MAX - id);
38 }
39
40 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
41 {
42         return snapshot_t(c, id)->parent;
43 }
44
45 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
46 {
47 #ifdef CONFIG_BCACHEFS_DEBUG
48         u32 parent = snapshot_t(c, id)->parent;
49
50         if (parent &&
51             snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
52                 panic("id %u depth=%u parent %u depth=%u\n",
53                       id, snapshot_t(c, id)->depth,
54                       parent, snapshot_t(c, parent)->depth);
55
56         return parent;
57 #else
58         return snapshot_t(c, id)->parent;
59 #endif
60 }
61
62 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
63 {
64         while (n--)
65                 id = bch2_snapshot_parent(c, id);
66
67         return id;
68 }
69
70 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
71 {
72         u32 parent;
73
74         while ((parent = bch2_snapshot_parent(c, id)))
75                 id = parent;
76         return id;
77 }
78
79 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
80 {
81         return snapshot_t(c, id)->equiv;
82 }
83
84 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
85 {
86         return id == snapshot_t(c, id)->equiv;
87 }
88
89 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
90 {
91         struct snapshot_t *s = snapshot_t(c, id);
92
93         return s->children[0] || s->children[1];
94 }
95
96 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
97 {
98         struct snapshot_t *s;
99         u32 parent = bch2_snapshot_parent(c, id);
100
101         if (!parent)
102                 return 0;
103
104         s = snapshot_t(c, bch2_snapshot_parent(c, id));
105         if (id == s->children[0])
106                 return s->children[1];
107         if (id == s->children[1])
108                 return s->children[0];
109         return 0;
110 }
111
112 bool bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
113
114 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
115 {
116         struct snapshot_t *t = snapshot_t(c, id);
117
118         return (t->children[0]|t->children[1]) != 0;
119 }
120
121 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
122 {
123         u32 *i;
124
125         darray_for_each(*s, i)
126                 if (*i == id)
127                         return true;
128         return false;
129 }
130
131 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
132 {
133         u32 *i;
134
135         darray_for_each(*s, i)
136                 if (bch2_snapshot_is_ancestor(c, id, *i))
137                         return true;
138         return false;
139 }
140
141 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
142 {
143         int ret;
144
145         BUG_ON(snapshot_list_has_id(s, id));
146         ret = darray_push(s, id);
147         if (ret)
148                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
149         return ret;
150 }
151
152 int bch2_check_snapshot_trees(struct bch_fs *);
153 int bch2_check_snapshots(struct bch_fs *);
154 int bch2_check_subvols(struct bch_fs *);
155
156 void bch2_fs_snapshots_exit(struct bch_fs *);
157 int bch2_snapshots_read(struct bch_fs *);
158
159 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
160                            unsigned, struct printbuf *);
161 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
162
163 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {            \
164         .key_invalid    = bch2_subvolume_invalid,               \
165         .val_to_text    = bch2_subvolume_to_text,               \
166         .min_val_size   = 16,                                   \
167 })
168
169 int bch2_subvolume_get(struct btree_trans *, unsigned,
170                        bool, int, struct bch_subvolume *);
171 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
172                              struct bch_subvolume *);
173 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
174
175 /* only exported for tests: */
176 int bch2_snapshot_node_create(struct btree_trans *, u32,
177                               u32 *, u32 *, unsigned);
178
179 int bch2_delete_dead_snapshots(struct bch_fs *);
180 void bch2_delete_dead_snapshots_async(struct bch_fs *);
181
182 int bch2_subvolume_unlink(struct btree_trans *, u32);
183 int bch2_subvolume_create(struct btree_trans *, u64, u32,
184                           u32 *, u32 *, bool);
185
186 int bch2_fs_subvolumes_init(struct bch_fs *);
187
188 #endif /* _BCACHEFS_SUBVOLUME_H */