]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to f638850417 bcachefs: bch2_trans_log_msg()
[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 const char *bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c);
10
11 #define bch2_bkey_ops_snapshot (struct bkey_ops) {              \
12         .key_invalid    = bch2_snapshot_invalid,                \
13         .val_to_text    = bch2_snapshot_to_text,                \
14 }
15
16 int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
17                        struct bkey_s_c, unsigned);
18
19 static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
20 {
21         return genradix_ptr(&c->snapshots, U32_MAX - id);
22 }
23
24 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
25 {
26         return snapshot_t(c, id)->parent;
27 }
28
29 static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
30 {
31         struct snapshot_t *s = snapshot_t(c, id);
32
33         return s->children[0] || s->children[1];
34 }
35
36 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
37 {
38         struct snapshot_t *s;
39         u32 parent = bch2_snapshot_parent(c, id);
40
41         if (!parent)
42                 return 0;
43
44         s = snapshot_t(c, bch2_snapshot_parent(c, id));
45         if (id == s->children[0])
46                 return s->children[1];
47         if (id == s->children[1])
48                 return s->children[0];
49         return 0;
50 }
51
52 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
53 {
54         while (id && id < ancestor)
55                 id = bch2_snapshot_parent(c, id);
56
57         return id == ancestor;
58 }
59
60 struct snapshots_seen {
61         struct bpos                     pos;
62         DARRAY(u32)                     ids;
63 };
64
65 static inline void snapshots_seen_exit(struct snapshots_seen *s)
66 {
67         kfree(s->ids.data);
68         s->ids.data = NULL;
69 }
70
71 static inline void snapshots_seen_init(struct snapshots_seen *s)
72 {
73         memset(s, 0, sizeof(*s));
74 }
75
76 static inline int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
77 {
78         int ret = darray_push(s->ids, id);
79         if (ret)
80                 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
81                         s->ids.size);
82         return ret;
83 }
84
85 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
86 {
87         u32 *i;
88
89         darray_for_each(*s, i)
90                 if (*i == id)
91                         return true;
92         return false;
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_snapshot_get_subvol(struct btree_trans *, u32,
110                              struct bch_subvolume *);
111 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
112
113 /* only exported for tests: */
114 int bch2_snapshot_node_create(struct btree_trans *, u32,
115                               u32 *, u32 *, unsigned);
116
117 int bch2_subvolume_delete(struct btree_trans *, u32);
118 int bch2_subvolume_unlink(struct btree_trans *, u32);
119 int bch2_subvolume_create(struct btree_trans *, u64, u32,
120                           u32 *, u32 *, bool);
121
122 int bch2_fs_subvolumes_init(struct bch_fs *);
123
124 #endif /* _BCACHEFS_SUBVOLUME_H */