]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to 043cfba30c fixup! bcachefs: Improve transaction restart...
[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 "subvolume_types.h"
6
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);
9
10 #define bch2_bkey_ops_snapshot (struct bkey_ops) {              \
11         .key_invalid    = bch2_snapshot_invalid,                \
12         .val_to_text    = bch2_snapshot_to_text,                \
13 }
14
15 int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
16                        struct bkey_s_c, unsigned);
17
18 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
19 {
20         return genradix_ptr(&c->snapshots, U32_MAX - id);
21 }
22
23 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
24 {
25         return snapshot_t(c, id)->parent;
26 }
27
28 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
29 {
30         struct snapshot_t *s = snapshot_t(c, id);
31
32         return s->children[0] || s->children[1];
33 }
34
35 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
36 {
37         struct snapshot_t *s;
38         u32 parent = bch2_snapshot_parent(c, id);
39
40         if (!parent)
41                 return 0;
42
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];
48         return 0;
49 }
50
51 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
52 {
53         while (id && id < ancestor)
54                 id = bch2_snapshot_parent(c, id);
55
56         return id == ancestor;
57 }
58
59 struct snapshots_seen {
60         struct bpos                     pos;
61         size_t                          nr;
62         size_t                          size;
63         u32                             *d;
64 };
65
66 static inline void snapshots_seen_exit(struct snapshots_seen *s)
67 {
68         kfree(s->d);
69         s->d = 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         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);
82
83                 if (!d) {
84                         bch_err(c, "error reallocating snapshots_seen table (new size %zu)",
85                                 new_size);
86                         return -ENOMEM;
87                 }
88
89                 s->size = new_size;
90                 s->d    = d;
91         }
92
93         s->d[s->nr++] = id;
94         return 0;
95 }
96
97 static inline bool snapshot_list_has_id(struct snapshot_id_list *s, u32 id)
98 {
99         unsigned i;
100
101         for (i = 0; i < s->nr; i++)
102                 if (id == s->d[i])
103                         return true;
104         return false;
105 }
106
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 *);
110
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);
113
114 #define bch2_bkey_ops_subvolume (struct bkey_ops) {             \
115         .key_invalid    = bch2_subvolume_invalid,               \
116         .val_to_text    = bch2_subvolume_to_text,               \
117 }
118
119 int bch2_subvolume_get(struct btree_trans *, unsigned,
120                        bool, int, struct bch_subvolume *);
121 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
122
123 int bch2_subvolume_delete(struct btree_trans *, u32);
124 int bch2_subvolume_unlink(struct btree_trans *, u32);
125 int bch2_subvolume_create(struct btree_trans *, u64, u32,
126                           u32 *, u32 *, bool);
127
128 int bch2_fs_subvolumes_init(struct bch_fs *);
129
130 #endif /* _BCACHEFS_SUBVOLUME_H */