]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to 5074caad6a fixup! bcachefs: BTREE_ID_snapshot_tree
[bcachefs-tools-debian] / libbcachefs / subvolume.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
4
5 #include "darray.h"
6 #include "subvolume_types.h"
7
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 *);
11
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,           \
15         .min_val_size   = 8,                                    \
16 })
17
18 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
19
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);
25
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,                   \
30         .min_val_size   = 24,                                   \
31 })
32
33 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
34 {
35         return genradix_ptr(&c->snapshots, U32_MAX - id);
36 }
37
38 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
39 {
40         return snapshot_t(c, id)->parent;
41 }
42
43 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
44 {
45         u32 parent;
46
47         while ((parent = bch2_snapshot_parent(c, id)))
48                 id = parent;
49         return id;
50 }
51
52 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
53 {
54         return snapshot_t(c, id)->equiv;
55 }
56
57 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
58 {
59         return id == snapshot_t(c, id)->equiv;
60 }
61
62 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
63 {
64         struct snapshot_t *s = snapshot_t(c, id);
65
66         return s->children[0] || s->children[1];
67 }
68
69 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
70 {
71         struct snapshot_t *s;
72         u32 parent = bch2_snapshot_parent(c, id);
73
74         if (!parent)
75                 return 0;
76
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];
82         return 0;
83 }
84
85 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
86 {
87         while (id && id < ancestor)
88                 id = bch2_snapshot_parent(c, id);
89
90         return id == ancestor;
91 }
92
93 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
94 {
95         struct snapshot_t *t = snapshot_t(c, id);
96
97         return (t->children[0]|t->children[1]) != 0;
98 }
99
100 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
101 {
102         u32 *i;
103
104         darray_for_each(*s, i)
105                 if (*i == id)
106                         return true;
107         return false;
108 }
109
110 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
111 {
112         u32 *i;
113
114         darray_for_each(*s, i)
115                 if (bch2_snapshot_is_ancestor(c, id, *i))
116                         return true;
117         return false;
118 }
119
120 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
121 {
122         int ret;
123
124         BUG_ON(snapshot_list_has_id(s, id));
125         ret = darray_push(s, id);
126         if (ret)
127                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
128         return ret;
129 }
130
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 *);
134
135 void bch2_fs_snapshots_exit(struct bch_fs *);
136 int bch2_fs_snapshots_start(struct bch_fs *);
137
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);
141
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,                                   \
146 })
147
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 *);
153
154 /* only exported for tests: */
155 int bch2_snapshot_node_create(struct btree_trans *, u32,
156                               u32 *, u32 *, unsigned);
157
158 int bch2_delete_dead_snapshots(struct bch_fs *);
159 void bch2_delete_dead_snapshots_async(struct bch_fs *);
160
161 int bch2_subvolume_unlink(struct btree_trans *, u32);
162 int bch2_subvolume_create(struct btree_trans *, u64, u32,
163                           u32 *, u32 *, bool);
164
165 int bch2_fs_subvolumes_init(struct bch_fs *);
166
167 #endif /* _BCACHEFS_SUBVOLUME_H */