]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to 07c2895cb3 bcachefs: Add a valgrind memcheck hint
[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 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
6 const char *bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c);
7
8 #define bch2_bkey_ops_snapshot (struct bkey_ops) {              \
9         .key_invalid    = bch2_snapshot_invalid,                \
10         .val_to_text    = bch2_snapshot_to_text,                \
11 }
12
13 int bch2_mark_snapshot(struct bch_fs *, struct bkey_s_c,
14                        struct bkey_s_c, u64, unsigned);
15
16 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
17 {
18         return genradix_ptr(&c->snapshots, U32_MAX - id);
19 }
20
21 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
22 {
23         return snapshot_t(c, id)->parent;
24 }
25
26 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
27 {
28         struct snapshot_t *s = snapshot_t(c, id);
29
30         return s->children[0] || s->children[1];
31 }
32
33 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
34 {
35         struct snapshot_t *s;
36         u32 parent = bch2_snapshot_parent(c, id);
37
38         if (!parent)
39                 return 0;
40
41         s = snapshot_t(c, bch2_snapshot_parent(c, id));
42         if (id == s->children[0])
43                 return s->children[1];
44         if (id == s->children[1])
45                 return s->children[0];
46         return 0;
47 }
48
49 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
50 {
51         while (id && id < ancestor)
52                 id = bch2_snapshot_parent(c, id);
53
54         return id == ancestor;
55 }
56
57 struct snapshots_seen {
58         struct bpos                     pos;
59         size_t                          nr;
60         size_t                          size;
61         u32                             *d;
62 };
63
64 static inline void snapshots_seen_exit(struct snapshots_seen *s)
65 {
66         kfree(s->d);
67         s->d = NULL;
68 }
69
70 static inline void snapshots_seen_init(struct snapshots_seen *s)
71 {
72         memset(s, 0, sizeof(*s));
73 }
74
75 static inline int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
76 {
77         if (s->nr == s->size) {
78                 size_t new_size = max(s->size, 128UL) * 2;
79                 u32 *d = krealloc(s->d, new_size * sizeof(s->d[0]), GFP_KERNEL);
80
81                 if (!d) {
82                         bch_err(c, "error reallocating snapshots_seen table (new size %zu)",
83                                 new_size);
84                         return -ENOMEM;
85                 }
86
87                 s->size = new_size;
88                 s->d    = d;
89         }
90
91         s->d[s->nr++] = id;
92         return 0;
93 }
94
95 int bch2_fs_snapshots_check(struct bch_fs *);
96 void bch2_fs_snapshots_exit(struct bch_fs *);
97 int bch2_fs_snapshots_start(struct bch_fs *);
98
99 const char *bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c);
100 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
101
102 #define bch2_bkey_ops_subvolume (struct bkey_ops) {             \
103         .key_invalid    = bch2_subvolume_invalid,               \
104         .val_to_text    = bch2_subvolume_to_text,               \
105 }
106
107 int bch2_subvolume_get(struct btree_trans *, unsigned,
108                        bool, int, struct bch_subvolume *);
109 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
110
111 int bch2_subvolume_delete(struct btree_trans *, u32, int);
112 int bch2_subvolume_create(struct btree_trans *, u64, u32,
113                           u32 *, u32 *, bool);
114
115 int bch2_fs_subvolumes_init(struct bch_fs *);
116
117 #endif /* _BCACHEFS_SUBVOLUME_H */