1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
6 #include "subvolume_types.h"
8 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
9 int bch2_snapshot_tree_invalid(const struct bch_fs *, struct bkey_s_c,
10 unsigned, struct printbuf *);
12 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) { \
13 .key_invalid = bch2_snapshot_tree_invalid, \
14 .val_to_text = bch2_snapshot_tree_to_text, \
18 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
20 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
21 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
22 unsigned, struct printbuf *);
23 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
24 struct bkey_s_c, struct bkey_s_c, unsigned);
26 #define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
27 .key_invalid = bch2_snapshot_invalid, \
28 .val_to_text = bch2_snapshot_to_text, \
29 .atomic_trigger = bch2_mark_snapshot, \
33 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
35 return genradix_ptr(&c->snapshots, U32_MAX - id);
38 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
40 return snapshot_t(c, id)->parent;
43 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
47 while ((parent = bch2_snapshot_parent(c, id)))
52 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
54 return snapshot_t(c, id)->equiv;
57 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
59 return id == snapshot_t(c, id)->equiv;
62 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
64 struct snapshot_t *s = snapshot_t(c, id);
66 return s->children[0] || s->children[1];
69 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
72 u32 parent = bch2_snapshot_parent(c, id);
77 s = snapshot_t(c, bch2_snapshot_parent(c, id));
78 if (id == s->children[0])
79 return s->children[1];
80 if (id == s->children[1])
81 return s->children[0];
85 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
87 while (id && id < ancestor)
88 id = bch2_snapshot_parent(c, id);
90 return id == ancestor;
93 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
95 struct snapshot_t *t = snapshot_t(c, id);
97 return (t->children[0]|t->children[1]) != 0;
100 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
104 darray_for_each(*s, i)
110 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
114 darray_for_each(*s, i)
115 if (bch2_snapshot_is_ancestor(c, id, *i))
120 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
124 BUG_ON(snapshot_list_has_id(s, id));
125 ret = darray_push(s, id);
127 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
131 int bch2_fs_check_snapshot_trees(struct bch_fs *);
132 int bch2_fs_check_snapshots(struct bch_fs *);
133 int bch2_fs_check_subvols(struct bch_fs *);
135 void bch2_fs_snapshots_exit(struct bch_fs *);
136 int bch2_fs_snapshots_start(struct bch_fs *);
138 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
139 unsigned, struct printbuf *);
140 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
142 #define bch2_bkey_ops_subvolume ((struct bkey_ops) { \
143 .key_invalid = bch2_subvolume_invalid, \
144 .val_to_text = bch2_subvolume_to_text, \
145 .min_val_size = 16, \
148 int bch2_subvolume_get(struct btree_trans *, unsigned,
149 bool, int, struct bch_subvolume *);
150 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
151 struct bch_subvolume *);
152 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
154 /* only exported for tests: */
155 int bch2_snapshot_node_create(struct btree_trans *, u32,
156 u32 *, u32 *, unsigned);
158 int bch2_delete_dead_snapshots(struct bch_fs *);
159 void bch2_delete_dead_snapshots_async(struct bch_fs *);
161 int bch2_subvolume_unlink(struct btree_trans *, u32);
162 int bch2_subvolume_create(struct btree_trans *, u64, u32,
165 int bch2_fs_subvolumes_init(struct bch_fs *);
167 #endif /* _BCACHEFS_SUBVOLUME_H */