1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SNAPSHOT_H
3 #define _BCACHEFS_SNAPSHOT_H
5 enum bch_validate_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 bch_validate_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 bch_validate_flags, struct printbuf *);
24 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
25 struct bkey_s_c, struct bkey_s,
26 enum btree_iter_update_trigger_flags);
28 #define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
29 .key_invalid = bch2_snapshot_invalid, \
30 .val_to_text = bch2_snapshot_to_text, \
31 .trigger = bch2_mark_snapshot, \
35 static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
37 u32 idx = U32_MAX - id;
39 return likely(t && idx < t->nr)
44 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
46 return __snapshot_t(rcu_dereference(c->snapshots), id);
49 static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
52 const struct snapshot_t *s = snapshot_t(c, id);
59 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
61 const struct snapshot_t *s = snapshot_t(c, id);
62 return s ? s->parent : 0;
65 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
68 id = __bch2_snapshot_parent_early(c, id);
74 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
76 const struct snapshot_t *s = snapshot_t(c, id);
80 u32 parent = s->parent;
81 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
83 s->depth != snapshot_t(c, parent)->depth + 1)
84 panic("id %u depth=%u parent %u depth=%u\n",
85 id, snapshot_t(c, id)->depth,
86 parent, snapshot_t(c, parent)->depth);
91 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
94 id = __bch2_snapshot_parent(c, id);
100 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
104 id = __bch2_snapshot_parent(c, id);
110 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
112 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
117 while ((parent = __bch2_snapshot_parent(c, id)))
124 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
126 const struct snapshot_t *s = snapshot_t(c, id);
127 return s ? s->equiv : 0;
130 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
133 id = __bch2_snapshot_equiv(c, id);
139 static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
142 const struct snapshot_t *s = snapshot_t(c, id);
143 int ret = s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
149 static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
151 int ret = bch2_snapshot_is_internal_node(c, id);
157 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
162 depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
168 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
170 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
172 return id == ancestor
174 : __bch2_snapshot_is_ancestor(c, id, ancestor);
177 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
179 const struct snapshot_t *t;
183 t = snapshot_t(c, id);
184 ret = (t->children[0]|t->children[1]) != 0;
190 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
192 darray_for_each(*s, i)
198 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
200 darray_for_each(*s, i)
201 if (bch2_snapshot_is_ancestor(c, id, *i))
206 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
208 BUG_ON(snapshot_list_has_id(s, id));
209 int ret = darray_push(s, id);
211 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
215 static inline int snapshot_list_add_nodup(struct bch_fs *c, snapshot_id_list *s, u32 id)
217 int ret = snapshot_list_has_id(s, id)
219 : darray_push(s, id);
221 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
225 static inline int snapshot_list_merge(struct bch_fs *c, snapshot_id_list *dst, snapshot_id_list *src)
227 darray_for_each(*src, i) {
228 int ret = snapshot_list_add_nodup(c, dst, *i);
236 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
237 struct bch_snapshot *s);
238 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
239 struct bch_subvolume *);
241 /* only exported for tests: */
242 int bch2_snapshot_node_create(struct btree_trans *, u32,
243 u32 *, u32 *, unsigned);
245 int bch2_check_snapshot_trees(struct bch_fs *);
246 int bch2_check_snapshots(struct bch_fs *);
247 int bch2_reconstruct_snapshots(struct bch_fs *);
249 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
250 void bch2_delete_dead_snapshots_work(struct work_struct *);
252 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
254 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
258 if (!btree_type_has_snapshots(id) ||
259 bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
262 return __bch2_key_has_snapshot_overwrites(trans, id, pos);
265 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
266 struct bkey_s_c, struct bpos *);
268 int bch2_snapshots_read(struct bch_fs *);
269 void bch2_fs_snapshots_exit(struct bch_fs *);
271 #endif /* _BCACHEFS_SNAPSHOT_H */