1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SNAPSHOT_H
3 #define _BCACHEFS_SNAPSHOT_H
5 enum bkey_invalid_flags;
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 *);
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, \
17 struct bkey_i_snapshot_tree *__bch2_snapshot_tree_create(struct btree_trans *);
19 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
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);
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, \
34 static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
36 u32 idx = U32_MAX - id;
38 return likely(t && idx < t->nr)
43 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
45 return __snapshot_t(rcu_dereference(c->snapshots), id);
48 static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
51 const struct snapshot_t *s = snapshot_t(c, id);
58 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
60 const struct snapshot_t *s = snapshot_t(c, id);
61 return s ? s->parent : 0;
64 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
67 id = __bch2_snapshot_parent_early(c, id);
73 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
75 const struct snapshot_t *s = snapshot_t(c, id);
79 u32 parent = s->parent;
80 if (IS_ENABLED(CONFIG_BCACHEFS_DEBU) &&
82 s->depth != snapshot_t(c, parent)->depth + 1)
83 panic("id %u depth=%u parent %u depth=%u\n",
84 id, snapshot_t(c, id)->depth,
85 parent, snapshot_t(c, parent)->depth);
90 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
93 id = __bch2_snapshot_parent(c, id);
99 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
103 id = __bch2_snapshot_parent(c, id);
109 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
111 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
116 while ((parent = __bch2_snapshot_parent(c, id)))
123 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
125 const struct snapshot_t *s = snapshot_t(c, id);
126 return s ? s->equiv : 0;
129 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
132 id = __bch2_snapshot_equiv(c, id);
138 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
140 return id == bch2_snapshot_equiv(c, id);
143 static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
146 const struct snapshot_t *s = snapshot_t(c, id);
147 int ret = s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
153 static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
155 int ret = bch2_snapshot_is_internal_node(c, id);
161 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
166 depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
172 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
174 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
176 return id == ancestor
178 : __bch2_snapshot_is_ancestor(c, id, ancestor);
181 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
183 const struct snapshot_t *t;
187 t = snapshot_t(c, id);
188 ret = (t->children[0]|t->children[1]) != 0;
194 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
196 darray_for_each(*s, i)
202 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
204 darray_for_each(*s, i)
205 if (bch2_snapshot_is_ancestor(c, id, *i))
210 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
212 BUG_ON(snapshot_list_has_id(s, id));
213 int ret = darray_push(s, id);
215 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
219 static inline int snapshot_list_add_nodup(struct bch_fs *c, snapshot_id_list *s, u32 id)
221 int ret = snapshot_list_has_id(s, id)
223 : darray_push(s, id);
225 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
229 static inline int snapshot_list_merge(struct bch_fs *c, snapshot_id_list *dst, snapshot_id_list *src)
231 darray_for_each(*src, i) {
232 int ret = snapshot_list_add_nodup(c, dst, *i);
240 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
241 struct bch_snapshot *s);
242 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
243 struct bch_subvolume *);
245 /* only exported for tests: */
246 int bch2_snapshot_node_create(struct btree_trans *, u32,
247 u32 *, u32 *, unsigned);
249 int bch2_check_snapshot_trees(struct bch_fs *);
250 int bch2_check_snapshots(struct bch_fs *);
251 int bch2_reconstruct_snapshots(struct bch_fs *);
253 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
254 void bch2_delete_dead_snapshots_work(struct work_struct *);
256 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
258 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
262 if (!btree_type_has_snapshots(id) ||
263 bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
266 return __bch2_key_has_snapshot_overwrites(trans, id, pos);
269 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
270 struct bkey_s_c, struct bpos *);
272 int bch2_snapshots_read(struct bch_fs *);
273 void bch2_fs_snapshots_exit(struct bch_fs *);
275 #endif /* _BCACHEFS_SNAPSHOT_H */