summaryrefslogtreecommitdiff
path: root/fs/bcachefs/snapshot.h
blob: f09a22f4423969024ea29224340f6a1a528d2821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_SNAPSHOT_H
#define _BCACHEFS_SNAPSHOT_H

enum bkey_invalid_flags;

void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
int bch2_snapshot_tree_invalid(struct bch_fs *, struct bkey_s_c,
			       enum bkey_invalid_flags, struct printbuf *);

#define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) {	\
	.key_invalid	= bch2_snapshot_tree_invalid,		\
	.val_to_text	= bch2_snapshot_tree_to_text,		\
	.min_val_size	= 8,					\
})

struct bkey_i_snapshot_tree *__bch2_snapshot_tree_create(struct btree_trans *);

int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);

void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
int bch2_snapshot_invalid(struct bch_fs *, struct bkey_s_c,
			  enum bkey_invalid_flags, struct printbuf *);
int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
		       struct bkey_s_c, struct bkey_s_c, unsigned);

#define bch2_bkey_ops_snapshot ((struct bkey_ops) {		\
	.key_invalid	= bch2_snapshot_invalid,		\
	.val_to_text	= bch2_snapshot_to_text,		\
	.atomic_trigger	= bch2_mark_snapshot,			\
	.min_val_size	= 24,					\
})

static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
{
	return &t->s[U32_MAX - id];
}

static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
{
	return __snapshot_t(rcu_dereference(c->snapshots), id);
}

static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
{
	rcu_read_lock();
	id = snapshot_t(c, id)->tree;
	rcu_read_unlock();

	return id;
}

static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
{
	return snapshot_t(c, id)->parent;
}

static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
{
	rcu_read_lock();
	id = __bch2_snapshot_parent_early(c, id);
	rcu_read_unlock();

	return id;
}

static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
{
#ifdef CONFIG_BCACHEFS_DEBUG
	u32 parent = snapshot_t(c, id)->parent;

	if (parent &&
	    snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
		panic("id %u depth=%u parent %u depth=%u\n",
		      id, snapshot_t(c, id)->depth,
		      parent, snapshot_t(c, parent)->depth);

	return parent;
#else
	return snapshot_t(c, id)->parent;
#endif
}

static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
{
	rcu_read_lock();
	id = __bch2_snapshot_parent(c, id);
	rcu_read_unlock();

	return id;
}

static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
{
	rcu_read_lock();
	while (n--)
		id = __bch2_snapshot_parent(c, id);
	rcu_read_unlock();

	return id;
}

u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);

static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
{
	u32 parent;

	rcu_read_lock();
	while ((parent = __bch2_snapshot_parent(c, id)))
		id = parent;
	rcu_read_unlock();

	return id;
}

static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
{
	return snapshot_t(c, id)->equiv;
}

static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
{
	rcu_read_lock();
	id = __bch2_snapshot_equiv(c, id);
	rcu_read_unlock();

	return id;
}

static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
{
	return id == bch2_snapshot_equiv(c, id);
}

static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
{
	const struct snapshot_t *s;
	bool ret;

	rcu_read_lock();
	s = snapshot_t(c, id);
	ret = s->children[0];
	rcu_read_unlock();

	return ret;
}

static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
{
	return !bch2_snapshot_is_internal_node(c, id);
}

static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
{
	const struct snapshot_t *s;
	u32 parent = __bch2_snapshot_parent(c, id);

	if (!parent)
		return 0;

	s = snapshot_t(c, __bch2_snapshot_parent(c, id));
	if (id == s->children[0])
		return s->children[1];
	if (id == s->children[1])
		return s->children[0];
	return 0;
}

static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
{
	u32 depth;

	rcu_read_lock();
	depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
	rcu_read_unlock();

	return depth;
}

bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);

static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
{
	return id == ancestor
		? true
		: __bch2_snapshot_is_ancestor(c, id, ancestor);
}

static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
{
	const struct snapshot_t *t;
	bool ret;

	rcu_read_lock();
	t = snapshot_t(c, id);
	ret = (t->children[0]|t->children[1]) != 0;
	rcu_read_unlock();

	return ret;
}

static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
{
	u32 *i;

	darray_for_each(*s, i)
		if (*i == id)
			return true;
	return false;
}

static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
{
	u32 *i;

	darray_for_each(*s, i)
		if (bch2_snapshot_is_ancestor(c, id, *i))
			return true;
	return false;
}

static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
{
	int ret;

	BUG_ON(snapshot_list_has_id(s, id));
	ret = darray_push(s, id);
	if (ret)
		bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
	return ret;
}

int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
			 struct bch_snapshot *s);
int bch2_snapshot_get_subvol(struct btree_trans *, u32,
			     struct bch_subvolume *);

/* only exported for tests: */
int bch2_snapshot_node_create(struct btree_trans *, u32,
			      u32 *, u32 *, unsigned);

int bch2_check_snapshot_trees(struct bch_fs *);
int bch2_check_snapshots(struct bch_fs *);

int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
void bch2_delete_dead_snapshots_work(struct work_struct *);

int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);

static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
					  enum btree_id id,
					  struct bpos pos)
{
	if (!btree_type_has_snapshots(id) ||
	    bch2_snapshot_is_leaf(trans->c, pos.snapshot))
		return 0;

	return __bch2_key_has_snapshot_overwrites(trans, id, pos);
}

int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
					  struct bkey_s_c, struct bpos *);

int bch2_snapshots_read(struct bch_fs *);
void bch2_fs_snapshots_exit(struct bch_fs *);

#endif /* _BCACHEFS_SNAPSHOT_H */