]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to bdf6d7c135 fixup! bcachefs: Kill journal buf bloom filter
[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_internal_node(struct bch_fs *c, u32 id)
31 {
32         struct snapshot_t *s = snapshot_t(c, id);
33
34         return s->children[0] || s->children[1];
35 }
36
37 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
38 {
39         struct snapshot_t *s;
40         u32 parent = bch2_snapshot_parent(c, id);
41
42         if (!parent)
43                 return 0;
44
45         s = snapshot_t(c, bch2_snapshot_parent(c, id));
46         if (id == s->children[0])
47                 return s->children[1];
48         if (id == s->children[1])
49                 return s->children[0];
50         return 0;
51 }
52
53 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
54 {
55         while (id && id < ancestor)
56                 id = bch2_snapshot_parent(c, id);
57
58         return id == ancestor;
59 }
60
61 struct snapshots_seen {
62         struct bpos                     pos;
63         DARRAY(u32)                     ids;
64 };
65
66 static inline void snapshots_seen_exit(struct snapshots_seen *s)
67 {
68         kfree(s->ids.data);
69         s->ids.data = NULL;
70 }
71
72 static inline void snapshots_seen_init(struct snapshots_seen *s)
73 {
74         memset(s, 0, sizeof(*s));
75 }
76
77 static inline int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
78 {
79         int ret = darray_push(&s->ids, id);
80         if (ret)
81                 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
82                         s->ids.size);
83         return ret;
84 }
85
86 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
87 {
88         u32 *i;
89
90         darray_for_each(*s, i)
91                 if (*i == id)
92                         return true;
93         return false;
94 }
95
96 int bch2_fs_snapshots_check(struct bch_fs *);
97 void bch2_fs_snapshots_exit(struct bch_fs *);
98 int bch2_fs_snapshots_start(struct bch_fs *);
99
100 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
101                            int rw, struct printbuf *);
102 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
103
104 #define bch2_bkey_ops_subvolume (struct bkey_ops) {             \
105         .key_invalid    = bch2_subvolume_invalid,               \
106         .val_to_text    = bch2_subvolume_to_text,               \
107 }
108
109 int bch2_subvolume_get(struct btree_trans *, unsigned,
110                        bool, int, struct bch_subvolume *);
111 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
112                              struct bch_subvolume *);
113 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
114
115 /* only exported for tests: */
116 int bch2_snapshot_node_create(struct btree_trans *, u32,
117                               u32 *, u32 *, unsigned);
118
119 int bch2_subvolume_delete(struct btree_trans *, u32);
120 int bch2_subvolume_unlink(struct btree_trans *, u32);
121 int bch2_subvolume_create(struct btree_trans *, u64, u32,
122                           u32 *, u32 *, bool);
123
124 int bch2_fs_subvolumes_init(struct bch_fs *);
125
126 #endif /* _BCACHEFS_SUBVOLUME_H */