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_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
9 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
10 unsigned, struct printbuf *);
11 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
12 struct bkey_s_c, struct bkey_s_c, unsigned);
14 #define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
15 .key_invalid = bch2_snapshot_invalid, \
16 .val_to_text = bch2_snapshot_to_text, \
17 .atomic_trigger = bch2_mark_snapshot, \
20 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
22 return genradix_ptr(&c->snapshots, U32_MAX - id);
25 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
27 return snapshot_t(c, id)->parent;
30 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
32 return snapshot_t(c, id)->equiv;
35 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
37 return id == snapshot_t(c, id)->equiv;
40 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
42 struct snapshot_t *s = snapshot_t(c, id);
44 return s->children[0] || s->children[1];
47 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
50 u32 parent = bch2_snapshot_parent(c, id);
55 s = snapshot_t(c, bch2_snapshot_parent(c, id));
56 if (id == s->children[0])
57 return s->children[1];
58 if (id == s->children[1])
59 return s->children[0];
63 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
65 while (id && id < ancestor)
66 id = bch2_snapshot_parent(c, id);
68 return id == ancestor;
71 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
73 struct snapshot_t *t = snapshot_t(c, id);
75 return (t->children[0]|t->children[1]) != 0;
78 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
82 darray_for_each(*s, i)
88 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
92 darray_for_each(*s, i)
93 if (bch2_snapshot_is_ancestor(c, id, *i))
98 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
102 BUG_ON(snapshot_list_has_id(s, id));
103 ret = darray_push(s, id);
105 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
109 int bch2_fs_check_snapshots(struct bch_fs *);
110 int bch2_fs_check_subvols(struct bch_fs *);
112 void bch2_fs_snapshots_exit(struct bch_fs *);
113 int bch2_fs_snapshots_start(struct bch_fs *);
115 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
116 unsigned, struct printbuf *);
117 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
119 #define bch2_bkey_ops_subvolume ((struct bkey_ops) { \
120 .key_invalid = bch2_subvolume_invalid, \
121 .val_to_text = bch2_subvolume_to_text, \
124 int bch2_subvolume_get(struct btree_trans *, unsigned,
125 bool, int, struct bch_subvolume *);
126 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
127 struct bch_subvolume *);
128 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
130 /* only exported for tests: */
131 int bch2_snapshot_node_create(struct btree_trans *, u32,
132 u32 *, u32 *, unsigned);
134 int bch2_delete_dead_snapshots(struct bch_fs *);
135 void bch2_delete_dead_snapshots_async(struct bch_fs *);
137 int bch2_subvolume_delete(struct btree_trans *, u32);
138 int bch2_subvolume_unlink(struct btree_trans *, u32);
139 int bch2_subvolume_create(struct btree_trans *, u64, u32,
142 int bch2_fs_subvolumes_init(struct bch_fs *);
144 #endif /* _BCACHEFS_SUBVOLUME_H */