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 return &t->s[U32_MAX - id];
39 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
41 return __snapshot_t(rcu_dereference(c->snapshots), id);
44 static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
47 id = snapshot_t(c, id)->tree;
53 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
55 return snapshot_t(c, id)->parent;
58 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
61 id = __bch2_snapshot_parent_early(c, id);
67 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
69 #ifdef CONFIG_BCACHEFS_DEBUG
70 u32 parent = snapshot_t(c, id)->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);
80 return snapshot_t(c, id)->parent;
84 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
87 id = __bch2_snapshot_parent(c, id);
93 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
97 id = __bch2_snapshot_parent(c, id);
103 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
105 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
110 while ((parent = __bch2_snapshot_parent(c, id)))
117 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
119 return snapshot_t(c, id)->equiv;
122 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
125 id = __bch2_snapshot_equiv(c, id);
131 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
133 return id == bch2_snapshot_equiv(c, id);
136 static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
138 const struct snapshot_t *s;
142 s = snapshot_t(c, id);
143 ret = s->children[0];
149 static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
151 return !bch2_snapshot_is_internal_node(c, id);
154 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
156 const struct snapshot_t *s;
157 u32 parent = __bch2_snapshot_parent(c, id);
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];
170 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
175 depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
181 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
183 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
185 return id == ancestor
187 : __bch2_snapshot_is_ancestor(c, id, ancestor);
190 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
192 const struct snapshot_t *t;
196 t = snapshot_t(c, id);
197 ret = (t->children[0]|t->children[1]) != 0;
203 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
205 darray_for_each(*s, i)
211 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
213 darray_for_each(*s, i)
214 if (bch2_snapshot_is_ancestor(c, id, *i))
219 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
223 BUG_ON(snapshot_list_has_id(s, id));
224 ret = darray_push(s, id);
226 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
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 *);
235 /* only exported for tests: */
236 int bch2_snapshot_node_create(struct btree_trans *, u32,
237 u32 *, u32 *, unsigned);
239 int bch2_check_snapshot_trees(struct bch_fs *);
240 int bch2_check_snapshots(struct bch_fs *);
242 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
243 void bch2_delete_dead_snapshots_work(struct work_struct *);
245 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
247 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
251 if (!btree_type_has_snapshots(id) ||
252 bch2_snapshot_is_leaf(trans->c, pos.snapshot))
255 return __bch2_key_has_snapshot_overwrites(trans, id, pos);
258 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
259 struct bkey_s_c, struct bpos *);
261 int bch2_snapshots_read(struct bch_fs *);
262 void bch2_fs_snapshots_exit(struct bch_fs *);
264 #endif /* _BCACHEFS_SNAPSHOT_H */