xref: /linux/fs/bcachefs/snapshot.h (revision 72bea132f3680ee51e7ed2cee62892b6f5121909)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SNAPSHOT_H
3 #define _BCACHEFS_SNAPSHOT_H
4 
5 enum bkey_invalid_flags;
6 
7 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
8 int bch2_snapshot_tree_invalid(struct bch_fs *, struct bkey_s_c,
9 			       enum bkey_invalid_flags, struct printbuf *);
10 
11 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) {	\
12 	.key_invalid	= bch2_snapshot_tree_invalid,		\
13 	.val_to_text	= bch2_snapshot_tree_to_text,		\
14 	.min_val_size	= 8,					\
15 })
16 
17 struct bkey_i_snapshot_tree *__bch2_snapshot_tree_create(struct btree_trans *);
18 
19 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
20 
21 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
22 int bch2_snapshot_invalid(struct bch_fs *, struct bkey_s_c,
23 			  enum bkey_invalid_flags, struct printbuf *);
24 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
25 		       struct bkey_s_c, struct bkey_s, unsigned);
26 
27 #define bch2_bkey_ops_snapshot ((struct bkey_ops) {		\
28 	.key_invalid	= bch2_snapshot_invalid,		\
29 	.val_to_text	= bch2_snapshot_to_text,		\
30 	.trigger	= bch2_mark_snapshot,			\
31 	.min_val_size	= 24,					\
32 })
33 
34 static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
35 {
36 	return &t->s[U32_MAX - id];
37 }
38 
39 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
40 {
41 	return __snapshot_t(rcu_dereference(c->snapshots), id);
42 }
43 
44 static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
45 {
46 	rcu_read_lock();
47 	id = snapshot_t(c, id)->tree;
48 	rcu_read_unlock();
49 
50 	return id;
51 }
52 
53 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
54 {
55 	return snapshot_t(c, id)->parent;
56 }
57 
58 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
59 {
60 	rcu_read_lock();
61 	id = __bch2_snapshot_parent_early(c, id);
62 	rcu_read_unlock();
63 
64 	return id;
65 }
66 
67 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
68 {
69 #ifdef CONFIG_BCACHEFS_DEBUG
70 	u32 parent = snapshot_t(c, id)->parent;
71 
72 	if (parent &&
73 	    snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
74 		panic("id %u depth=%u parent %u depth=%u\n",
75 		      id, snapshot_t(c, id)->depth,
76 		      parent, snapshot_t(c, parent)->depth);
77 
78 	return parent;
79 #else
80 	return snapshot_t(c, id)->parent;
81 #endif
82 }
83 
84 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
85 {
86 	rcu_read_lock();
87 	id = __bch2_snapshot_parent(c, id);
88 	rcu_read_unlock();
89 
90 	return id;
91 }
92 
93 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
94 {
95 	rcu_read_lock();
96 	while (n--)
97 		id = __bch2_snapshot_parent(c, id);
98 	rcu_read_unlock();
99 
100 	return id;
101 }
102 
103 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
104 
105 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
106 {
107 	u32 parent;
108 
109 	rcu_read_lock();
110 	while ((parent = __bch2_snapshot_parent(c, id)))
111 		id = parent;
112 	rcu_read_unlock();
113 
114 	return id;
115 }
116 
117 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
118 {
119 	return snapshot_t(c, id)->equiv;
120 }
121 
122 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
123 {
124 	rcu_read_lock();
125 	id = __bch2_snapshot_equiv(c, id);
126 	rcu_read_unlock();
127 
128 	return id;
129 }
130 
131 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
132 {
133 	return id == bch2_snapshot_equiv(c, id);
134 }
135 
136 static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
137 {
138 	const struct snapshot_t *s;
139 	bool ret;
140 
141 	rcu_read_lock();
142 	s = snapshot_t(c, id);
143 	ret = s->children[0];
144 	rcu_read_unlock();
145 
146 	return ret;
147 }
148 
149 static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
150 {
151 	return !bch2_snapshot_is_internal_node(c, id);
152 }
153 
154 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
155 {
156 	const struct snapshot_t *s;
157 	u32 parent = __bch2_snapshot_parent(c, id);
158 
159 	if (!parent)
160 		return 0;
161 
162 	s = snapshot_t(c, __bch2_snapshot_parent(c, id));
163 	if (id == s->children[0])
164 		return s->children[1];
165 	if (id == s->children[1])
166 		return s->children[0];
167 	return 0;
168 }
169 
170 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
171 {
172 	u32 depth;
173 
174 	rcu_read_lock();
175 	depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
176 	rcu_read_unlock();
177 
178 	return depth;
179 }
180 
181 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
182 
183 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
184 {
185 	return id == ancestor
186 		? true
187 		: __bch2_snapshot_is_ancestor(c, id, ancestor);
188 }
189 
190 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
191 {
192 	const struct snapshot_t *t;
193 	bool ret;
194 
195 	rcu_read_lock();
196 	t = snapshot_t(c, id);
197 	ret = (t->children[0]|t->children[1]) != 0;
198 	rcu_read_unlock();
199 
200 	return ret;
201 }
202 
203 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
204 {
205 	darray_for_each(*s, i)
206 		if (*i == id)
207 			return true;
208 	return false;
209 }
210 
211 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
212 {
213 	darray_for_each(*s, i)
214 		if (bch2_snapshot_is_ancestor(c, id, *i))
215 			return true;
216 	return false;
217 }
218 
219 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
220 {
221 	int ret;
222 
223 	BUG_ON(snapshot_list_has_id(s, id));
224 	ret = darray_push(s, id);
225 	if (ret)
226 		bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
227 	return ret;
228 }
229 
230 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
231 			 struct bch_snapshot *s);
232 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
233 			     struct bch_subvolume *);
234 
235 /* only exported for tests: */
236 int bch2_snapshot_node_create(struct btree_trans *, u32,
237 			      u32 *, u32 *, unsigned);
238 
239 int bch2_check_snapshot_trees(struct bch_fs *);
240 int bch2_check_snapshots(struct bch_fs *);
241 
242 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
243 void bch2_delete_dead_snapshots_work(struct work_struct *);
244 
245 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
246 
247 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
248 					  enum btree_id id,
249 					  struct bpos pos)
250 {
251 	if (!btree_type_has_snapshots(id) ||
252 	    bch2_snapshot_is_leaf(trans->c, pos.snapshot))
253 		return 0;
254 
255 	return __bch2_key_has_snapshot_overwrites(trans, id, pos);
256 }
257 
258 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
259 					  struct bkey_s_c, struct bpos *);
260 
261 int bch2_snapshots_read(struct bch_fs *);
262 void bch2_fs_snapshots_exit(struct bch_fs *);
263 
264 #endif /* _BCACHEFS_SNAPSHOT_H */
265