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 int rw, struct printbuf *);
12 #define bch2_bkey_ops_snapshot (struct bkey_ops) { \
13 .key_invalid = bch2_snapshot_invalid, \
14 .val_to_text = bch2_snapshot_to_text, \
17 int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
18 struct bkey_s_c, unsigned);
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 snapshot_list_has_id(snapshot_id_list *s, u32 id)
75 darray_for_each(*s, i)
81 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
85 darray_for_each(*s, i)
86 if (bch2_snapshot_is_ancestor(c, id, *i))
91 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
95 BUG_ON(snapshot_list_has_id(s, id));
96 ret = darray_push(s, id);
98 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
102 int bch2_fs_check_snapshots(struct bch_fs *);
103 int bch2_fs_check_subvols(struct bch_fs *);
105 void bch2_fs_snapshots_exit(struct bch_fs *);
106 int bch2_fs_snapshots_start(struct bch_fs *);
108 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
109 int rw, struct printbuf *);
110 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
112 #define bch2_bkey_ops_subvolume (struct bkey_ops) { \
113 .key_invalid = bch2_subvolume_invalid, \
114 .val_to_text = bch2_subvolume_to_text, \
117 int bch2_subvolume_get(struct btree_trans *, unsigned,
118 bool, int, struct bch_subvolume *);
119 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
120 struct bch_subvolume *);
121 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
123 /* only exported for tests: */
124 int bch2_snapshot_node_create(struct btree_trans *, u32,
125 u32 *, u32 *, unsigned);
127 int bch2_delete_dead_snapshots(struct bch_fs *);
128 void bch2_delete_dead_snapshots_async(struct bch_fs *);
130 int bch2_subvolume_delete(struct btree_trans *, u32);
131 int bch2_subvolume_unlink(struct btree_trans *, u32);
132 int bch2_subvolume_create(struct btree_trans *, u64, u32,
135 int bch2_fs_subvolumes_init(struct bch_fs *);
137 #endif /* _BCACHEFS_SUBVOLUME_H */