1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
6 #include "subvolume_types.h"
8 enum bkey_invalid_flags;
10 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
11 int bch2_snapshot_tree_invalid(const struct bch_fs *, struct bkey_s_c,
12 enum bkey_invalid_flags, struct printbuf *);
14 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) { \
15 .key_invalid = bch2_snapshot_tree_invalid, \
16 .val_to_text = bch2_snapshot_tree_to_text, \
20 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
22 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
23 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
24 enum bkey_invalid_flags, struct printbuf *);
25 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
26 struct bkey_s_c, struct bkey_s_c, unsigned);
28 #define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
29 .key_invalid = bch2_snapshot_invalid, \
30 .val_to_text = bch2_snapshot_to_text, \
31 .atomic_trigger = bch2_mark_snapshot, \
35 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
37 return genradix_ptr(&c->snapshots, U32_MAX - id);
40 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
42 return snapshot_t(c, id)->parent;
45 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
49 while ((parent = bch2_snapshot_parent(c, id)))
54 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
56 return snapshot_t(c, id)->equiv;
59 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
61 return id == snapshot_t(c, id)->equiv;
64 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
66 struct snapshot_t *s = snapshot_t(c, id);
68 return s->children[0] || s->children[1];
71 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
74 u32 parent = bch2_snapshot_parent(c, id);
79 s = snapshot_t(c, bch2_snapshot_parent(c, id));
80 if (id == s->children[0])
81 return s->children[1];
82 if (id == s->children[1])
83 return s->children[0];
87 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
89 while (id && id < ancestor)
90 id = bch2_snapshot_parent(c, id);
92 return id == ancestor;
95 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
97 struct snapshot_t *t = snapshot_t(c, id);
99 return (t->children[0]|t->children[1]) != 0;
102 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
106 darray_for_each(*s, i)
112 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
116 darray_for_each(*s, i)
117 if (bch2_snapshot_is_ancestor(c, id, *i))
122 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
126 BUG_ON(snapshot_list_has_id(s, id));
127 ret = darray_push(s, id);
129 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
133 int bch2_check_snapshot_trees(struct bch_fs *);
134 int bch2_check_snapshots(struct bch_fs *);
135 int bch2_check_subvols(struct bch_fs *);
137 void bch2_fs_snapshots_exit(struct bch_fs *);
138 int bch2_snapshots_read(struct bch_fs *);
140 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
141 unsigned, struct printbuf *);
142 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
144 #define bch2_bkey_ops_subvolume ((struct bkey_ops) { \
145 .key_invalid = bch2_subvolume_invalid, \
146 .val_to_text = bch2_subvolume_to_text, \
147 .min_val_size = 16, \
150 int bch2_subvolume_get(struct btree_trans *, unsigned,
151 bool, int, struct bch_subvolume *);
152 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
153 struct bch_subvolume *);
154 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
156 /* only exported for tests: */
157 int bch2_snapshot_node_create(struct btree_trans *, u32,
158 u32 *, u32 *, unsigned);
160 int bch2_delete_dead_snapshots(struct bch_fs *);
161 void bch2_delete_dead_snapshots_async(struct bch_fs *);
163 int bch2_subvolume_unlink(struct btree_trans *, u32);
164 int bch2_subvolume_create(struct btree_trans *, u64, u32,
167 int bch2_fs_subvolumes_init(struct bch_fs *);
169 #endif /* _BCACHEFS_SUBVOLUME_H */