1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
5 #include "subvolume_types.h"
7 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
8 const char *bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c);
10 #define bch2_bkey_ops_snapshot (struct bkey_ops) { \
11 .key_invalid = bch2_snapshot_invalid, \
12 .val_to_text = bch2_snapshot_to_text, \
15 int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
16 struct bkey_s_c, unsigned);
18 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
20 return genradix_ptr(&c->snapshots, U32_MAX - id);
23 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
25 return snapshot_t(c, id)->parent;
28 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
30 struct snapshot_t *s = snapshot_t(c, id);
32 return s->children[0] || s->children[1];
35 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
38 u32 parent = bch2_snapshot_parent(c, id);
43 s = snapshot_t(c, bch2_snapshot_parent(c, id));
44 if (id == s->children[0])
45 return s->children[1];
46 if (id == s->children[1])
47 return s->children[0];
51 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
53 while (id && id < ancestor)
54 id = bch2_snapshot_parent(c, id);
56 return id == ancestor;
59 struct snapshots_seen {
66 static inline void snapshots_seen_exit(struct snapshots_seen *s)
72 static inline void snapshots_seen_init(struct snapshots_seen *s)
74 memset(s, 0, sizeof(*s));
77 static inline int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
79 if (s->nr == s->size) {
80 size_t new_size = max(s->size, (size_t) 128) * 2;
81 u32 *d = krealloc(s->d, new_size * sizeof(s->d[0]), GFP_KERNEL);
84 bch_err(c, "error reallocating snapshots_seen table (new size %zu)",
97 static inline bool snapshot_list_has_id(struct snapshot_id_list *s, u32 id)
101 for (i = 0; i < s->nr; i++)
107 int bch2_fs_snapshots_check(struct bch_fs *);
108 void bch2_fs_snapshots_exit(struct bch_fs *);
109 int bch2_fs_snapshots_start(struct bch_fs *);
111 const char *bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c);
112 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
114 #define bch2_bkey_ops_subvolume (struct bkey_ops) { \
115 .key_invalid = bch2_subvolume_invalid, \
116 .val_to_text = bch2_subvolume_to_text, \
119 int bch2_subvolume_get(struct btree_trans *, unsigned,
120 bool, int, struct bch_subvolume *);
121 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
122 struct bch_subvolume *);
123 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
125 /* only exported for tests: */
126 int bch2_snapshot_node_create(struct btree_trans *, u32,
127 u32 *, u32 *, unsigned);
129 int bch2_subvolume_delete(struct btree_trans *, u32);
130 int bch2_subvolume_unlink(struct btree_trans *, u32);
131 int bch2_subvolume_create(struct btree_trans *, u64, u32,
134 int bch2_fs_subvolumes_init(struct bch_fs *);
136 #endif /* _BCACHEFS_SUBVOLUME_H */