]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
rust: Fix ptr casting in Fs::open()
[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                           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);
13
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,                   \
18 })
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 bch2_snapshot_has_children(struct bch_fs *c, u32 id)
72 {
73         struct snapshot_t *t = snapshot_t(c, id);
74
75         return (t->children[0]|t->children[1]) != 0;
76 }
77
78 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
79 {
80         u32 *i;
81
82         darray_for_each(*s, i)
83                 if (*i == id)
84                         return true;
85         return false;
86 }
87
88 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
89 {
90         u32 *i;
91
92         darray_for_each(*s, i)
93                 if (bch2_snapshot_is_ancestor(c, id, *i))
94                         return true;
95         return false;
96 }
97
98 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
99 {
100         int ret;
101
102         BUG_ON(snapshot_list_has_id(s, id));
103         ret = darray_push(s, id);
104         if (ret)
105                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
106         return ret;
107 }
108
109 int bch2_fs_check_snapshots(struct bch_fs *);
110 int bch2_fs_check_subvols(struct bch_fs *);
111
112 void bch2_fs_snapshots_exit(struct bch_fs *);
113 int bch2_fs_snapshots_start(struct bch_fs *);
114
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);
118
119 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {            \
120         .key_invalid    = bch2_subvolume_invalid,               \
121         .val_to_text    = bch2_subvolume_to_text,               \
122 })
123
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 *);
129
130 /* only exported for tests: */
131 int bch2_snapshot_node_create(struct btree_trans *, u32,
132                               u32 *, u32 *, unsigned);
133
134 int bch2_delete_dead_snapshots(struct bch_fs *);
135 void bch2_delete_dead_snapshots_async(struct bch_fs *);
136
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,
140                           u32 *, u32 *, bool);
141
142 int bch2_fs_subvolumes_init(struct bch_fs *);
143
144 #endif /* _BCACHEFS_SUBVOLUME_H */