]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to 70fa0c1ff4 fixup! bcachefs: Btree key cache improvements
[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_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 *);
11
12 #define bch2_bkey_ops_snapshot ((struct bkey_ops) {             \
13         .key_invalid    = bch2_snapshot_invalid,                \
14         .val_to_text    = bch2_snapshot_to_text,                \
15 })
16
17 int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
18                        struct bkey_s_c, unsigned);
19
20 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
21 {
22         return genradix_ptr(&c->snapshots, U32_MAX - id);
23 }
24
25 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
26 {
27         return snapshot_t(c, id)->parent;
28 }
29
30 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
31 {
32         return snapshot_t(c, id)->equiv;
33 }
34
35 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
36 {
37         return id == snapshot_t(c, id)->equiv;
38 }
39
40 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
41 {
42         struct snapshot_t *s = snapshot_t(c, id);
43
44         return s->children[0] || s->children[1];
45 }
46
47 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
48 {
49         struct snapshot_t *s;
50         u32 parent = bch2_snapshot_parent(c, id);
51
52         if (!parent)
53                 return 0;
54
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];
60         return 0;
61 }
62
63 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
64 {
65         while (id && id < ancestor)
66                 id = bch2_snapshot_parent(c, id);
67
68         return id == ancestor;
69 }
70
71 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
72 {
73         u32 *i;
74
75         darray_for_each(*s, i)
76                 if (*i == id)
77                         return true;
78         return false;
79 }
80
81 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
82 {
83         u32 *i;
84
85         darray_for_each(*s, i)
86                 if (bch2_snapshot_is_ancestor(c, id, *i))
87                         return true;
88         return false;
89 }
90
91 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
92 {
93         int ret;
94
95         BUG_ON(snapshot_list_has_id(s, id));
96         ret = darray_push(s, id);
97         if (ret)
98                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
99         return ret;
100 }
101
102 int bch2_fs_check_snapshots(struct bch_fs *);
103 int bch2_fs_check_subvols(struct bch_fs *);
104
105 void bch2_fs_snapshots_exit(struct bch_fs *);
106 int bch2_fs_snapshots_start(struct bch_fs *);
107
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);
111
112 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {            \
113         .key_invalid    = bch2_subvolume_invalid,               \
114         .val_to_text    = bch2_subvolume_to_text,               \
115 })
116
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 *);
122
123 /* only exported for tests: */
124 int bch2_snapshot_node_create(struct btree_trans *, u32,
125                               u32 *, u32 *, unsigned);
126
127 int bch2_delete_dead_snapshots(struct bch_fs *);
128 void bch2_delete_dead_snapshots_async(struct bch_fs *);
129
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,
133                           u32 *, u32 *, bool);
134
135 int bch2_fs_subvolumes_init(struct bch_fs *);
136
137 #endif /* _BCACHEFS_SUBVOLUME_H */