xref: /linux/fs/bcachefs/btree_update_interior.h (revision 6f2a71a99ebd5dfaa7948a2e9c59eae94b741bd8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_INTERIOR_H
3 #define _BCACHEFS_BTREE_UPDATE_INTERIOR_H
4 
5 #include "btree_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
8 
9 #define BTREE_UPDATE_NODES_MAX		((BTREE_MAX_DEPTH - 2) * 2 + GC_MERGE_NODES)
10 
11 #define BTREE_UPDATE_JOURNAL_RES	(BTREE_UPDATE_NODES_MAX * (BKEY_BTREE_PTR_U64s_MAX + 1))
12 
13 int bch2_btree_node_check_topology(struct btree_trans *, struct btree *);
14 
15 #define BTREE_UPDATE_MODES()	\
16 	x(none)			\
17 	x(node)			\
18 	x(root)			\
19 	x(update)
20 
21 enum btree_update_mode {
22 #define x(n)	BTREE_UPDATE_##n,
23 	BTREE_UPDATE_MODES()
24 #undef x
25 };
26 
27 /*
28  * Tracks an in progress split/rewrite of a btree node and the update to the
29  * parent node:
30  *
31  * When we split/rewrite a node, we do all the updates in memory without
32  * waiting for any writes to complete - we allocate the new node(s) and update
33  * the parent node, possibly recursively up to the root.
34  *
35  * The end result is that we have one or more new nodes being written -
36  * possibly several, if there were multiple splits - and then a write (updating
37  * an interior node) which will make all these new nodes visible.
38  *
39  * Additionally, as we split/rewrite nodes we free the old nodes - but the old
40  * nodes can't be freed (their space on disk can't be reclaimed) until the
41  * update to the interior node that makes the new node visible completes -
42  * until then, the old nodes are still reachable on disk.
43  *
44  */
45 struct btree_update {
46 	struct closure			cl;
47 	struct bch_fs			*c;
48 	u64				start_time;
49 	unsigned long			ip_started;
50 
51 	struct list_head		list;
52 	struct list_head		unwritten_list;
53 
54 	enum btree_update_mode		mode;
55 	enum bch_trans_commit_flags	flags;
56 	unsigned			nodes_written:1;
57 	unsigned			took_gc_lock:1;
58 
59 	enum btree_id			btree_id;
60 	struct bpos			node_start;
61 	struct bpos			node_end;
62 	enum btree_node_rewrite_reason	node_needed_rewrite;
63 	u16				node_written;
64 	u16				node_sectors;
65 	u16				node_remaining;
66 
67 	unsigned			update_level_start;
68 	unsigned			update_level_end;
69 
70 	struct disk_reservation		disk_res;
71 
72 	/*
73 	 * BTREE_UPDATE_node:
74 	 * The update that made the new nodes visible was a regular update to an
75 	 * existing interior node - @b. We can't write out the update to @b
76 	 * until the new nodes we created are finished writing, so we block @b
77 	 * from writing by putting this btree_interior update on the
78 	 * @b->write_blocked list with @write_blocked_list:
79 	 */
80 	struct btree			*b;
81 	struct list_head		write_blocked_list;
82 
83 	/*
84 	 * We may be freeing nodes that were dirty, and thus had journal entries
85 	 * pinned: we need to transfer the oldest of those pins to the
86 	 * btree_update operation, and release it when the new node(s)
87 	 * are all persistent and reachable:
88 	 */
89 	struct journal_entry_pin	journal;
90 
91 	/* Preallocated nodes we reserve when we start the update: */
92 	struct prealloc_nodes {
93 		struct btree		*b[BTREE_UPDATE_NODES_MAX];
94 		unsigned		nr;
95 	}				prealloc_nodes[2];
96 
97 	/* Nodes being freed: */
98 	struct keylist			old_keys;
99 	u64				_old_keys[BTREE_UPDATE_NODES_MAX *
100 						  BKEY_BTREE_PTR_U64s_MAX];
101 
102 	/* Nodes being added: */
103 	struct keylist			new_keys;
104 	u64				_new_keys[BTREE_UPDATE_NODES_MAX *
105 						  BKEY_BTREE_PTR_U64s_MAX];
106 
107 	/* New nodes, that will be made reachable by this update: */
108 	struct btree			*new_nodes[BTREE_UPDATE_NODES_MAX];
109 	unsigned			nr_new_nodes;
110 
111 	struct btree			*old_nodes[BTREE_UPDATE_NODES_MAX];
112 	__le64				old_nodes_seq[BTREE_UPDATE_NODES_MAX];
113 	unsigned			nr_old_nodes;
114 
115 	open_bucket_idx_t		open_buckets[BTREE_UPDATE_NODES_MAX *
116 						     BCH_REPLICAS_MAX];
117 	open_bucket_idx_t		nr_open_buckets;
118 
119 	unsigned			journal_u64s;
120 	u64				journal_entries[BTREE_UPDATE_JOURNAL_RES];
121 
122 	/* Only here to reduce stack usage on recursive splits: */
123 	struct keylist			parent_keys;
124 	/*
125 	 * Enough room for btree_split's keys without realloc - btree node
126 	 * pointers never have crc/compression info, so we only need to acount
127 	 * for the pointers for three keys
128 	 */
129 	u64				inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3];
130 };
131 
132 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *,
133 						  struct btree_trans *,
134 						  struct btree *,
135 						  struct bkey_format);
136 
137 int bch2_btree_split_leaf(struct btree_trans *, btree_path_idx_t, unsigned);
138 
139 int bch2_btree_increase_depth(struct btree_trans *, btree_path_idx_t, unsigned);
140 
141 int __bch2_foreground_maybe_merge(struct btree_trans *, btree_path_idx_t,
142 				  unsigned, unsigned, enum btree_node_sibling);
143 
bch2_foreground_maybe_merge_sibling(struct btree_trans * trans,btree_path_idx_t path_idx,unsigned level,unsigned flags,enum btree_node_sibling sib)144 static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans,
145 					btree_path_idx_t path_idx,
146 					unsigned level, unsigned flags,
147 					enum btree_node_sibling sib)
148 {
149 	struct btree_path *path = trans->paths + path_idx;
150 	struct btree *b;
151 
152 	EBUG_ON(!btree_node_locked(path, level));
153 
154 	if (static_branch_unlikely(&bch2_btree_node_merging_disabled))
155 		return 0;
156 
157 	b = path->l[level].b;
158 	if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold)
159 		return 0;
160 
161 	return __bch2_foreground_maybe_merge(trans, path_idx, level, flags, sib);
162 }
163 
bch2_foreground_maybe_merge(struct btree_trans * trans,btree_path_idx_t path,unsigned level,unsigned flags)164 static inline int bch2_foreground_maybe_merge(struct btree_trans *trans,
165 					      btree_path_idx_t path,
166 					      unsigned level,
167 					      unsigned flags)
168 {
169 	bch2_trans_verify_not_unlocked_or_in_restart(trans);
170 
171 	return  bch2_foreground_maybe_merge_sibling(trans, path, level, flags,
172 						    btree_prev_sib) ?:
173 		bch2_foreground_maybe_merge_sibling(trans, path, level, flags,
174 						    btree_next_sib);
175 }
176 
177 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
178 			    struct btree *, unsigned, unsigned);
179 int bch2_btree_node_rewrite_key(struct btree_trans *,
180 				enum btree_id, unsigned,
181 				struct bkey_i *, unsigned);
182 int bch2_btree_node_rewrite_pos(struct btree_trans *,
183 				enum btree_id, unsigned,
184 				struct bpos, unsigned, unsigned);
185 int bch2_btree_node_rewrite_key_get_iter(struct btree_trans *,
186 					 struct btree *, unsigned);
187 
188 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
189 
190 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
191 			       struct btree *, struct bkey_i *,
192 			       unsigned, bool);
193 int bch2_btree_node_update_key_get_iter(struct btree_trans *, struct btree *,
194 					struct bkey_i *, unsigned, bool);
195 
196 void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *);
197 
198 int bch2_btree_root_alloc_fake_trans(struct btree_trans *, enum btree_id, unsigned);
199 void bch2_btree_root_alloc_fake(struct bch_fs *, enum btree_id, unsigned);
200 
btree_update_reserve_required(struct bch_fs * c,struct btree * b)201 static inline unsigned btree_update_reserve_required(struct bch_fs *c,
202 						     struct btree *b)
203 {
204 	unsigned depth = btree_node_root(c, b)->c.level + 1;
205 
206 	/*
207 	 * Number of nodes we might have to allocate in a worst case btree
208 	 * split operation - we split all the way up to the root, then allocate
209 	 * a new root, unless we're already at max depth:
210 	 */
211 	if (depth < BTREE_MAX_DEPTH)
212 		return (depth - b->c.level) * 2 + 1;
213 	else
214 		return (depth - b->c.level) * 2 - 1;
215 }
216 
btree_node_reset_sib_u64s(struct btree * b)217 static inline void btree_node_reset_sib_u64s(struct btree *b)
218 {
219 	b->sib_u64s[0] = b->nr.live_u64s;
220 	b->sib_u64s[1] = b->nr.live_u64s;
221 }
222 
btree_data_end(struct btree * b)223 static inline void *btree_data_end(struct btree *b)
224 {
225 	return (void *) b->data + btree_buf_bytes(b);
226 }
227 
unwritten_whiteouts_start(struct btree * b)228 static inline struct bkey_packed *unwritten_whiteouts_start(struct btree *b)
229 {
230 	return (void *) ((u64 *) btree_data_end(b) - b->whiteout_u64s);
231 }
232 
unwritten_whiteouts_end(struct btree * b)233 static inline struct bkey_packed *unwritten_whiteouts_end(struct btree *b)
234 {
235 	return btree_data_end(b);
236 }
237 
write_block(struct btree * b)238 static inline void *write_block(struct btree *b)
239 {
240 	return (void *) b->data + (b->written << 9);
241 }
242 
__btree_addr_written(struct btree * b,void * p)243 static inline bool __btree_addr_written(struct btree *b, void *p)
244 {
245 	return p < write_block(b);
246 }
247 
bset_written(struct btree * b,struct bset * i)248 static inline bool bset_written(struct btree *b, struct bset *i)
249 {
250 	return __btree_addr_written(b, i);
251 }
252 
bkey_written(struct btree * b,struct bkey_packed * k)253 static inline bool bkey_written(struct btree *b, struct bkey_packed *k)
254 {
255 	return __btree_addr_written(b, k);
256 }
257 
__bch2_btree_u64s_remaining(struct btree * b,void * end)258 static inline ssize_t __bch2_btree_u64s_remaining(struct btree *b, void *end)
259 {
260 	ssize_t used = bset_byte_offset(b, end) / sizeof(u64) +
261 		b->whiteout_u64s;
262 	ssize_t total = btree_buf_bytes(b) >> 3;
263 
264 	/* Always leave one extra u64 for bch2_varint_decode: */
265 	used++;
266 
267 	return total - used;
268 }
269 
bch2_btree_keys_u64s_remaining(struct btree * b)270 static inline size_t bch2_btree_keys_u64s_remaining(struct btree *b)
271 {
272 	ssize_t remaining = __bch2_btree_u64s_remaining(b,
273 				btree_bkey_last(b, bset_tree_last(b)));
274 
275 	BUG_ON(remaining < 0);
276 
277 	if (bset_written(b, btree_bset_last(b)))
278 		return 0;
279 
280 	return remaining;
281 }
282 
283 #define BTREE_WRITE_SET_U64s_BITS	9
284 
btree_write_set_buffer(struct btree * b)285 static inline unsigned btree_write_set_buffer(struct btree *b)
286 {
287 	/*
288 	 * Could buffer up larger amounts of keys for btrees with larger keys,
289 	 * pending benchmarking:
290 	 */
291 	return 8 << BTREE_WRITE_SET_U64s_BITS;
292 }
293 
want_new_bset(struct bch_fs * c,struct btree * b)294 static inline struct btree_node_entry *want_new_bset(struct bch_fs *c, struct btree *b)
295 {
296 	struct bset_tree *t = bset_tree_last(b);
297 	struct btree_node_entry *bne = max(write_block(b),
298 			(void *) btree_bkey_last(b, t));
299 	ssize_t remaining_space =
300 		__bch2_btree_u64s_remaining(b, bne->keys.start);
301 
302 	if (unlikely(bset_written(b, bset(b, t)))) {
303 		if (b->written + block_sectors(c) <= btree_sectors(c))
304 			return bne;
305 	} else {
306 		if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) &&
307 		    remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3))
308 			return bne;
309 	}
310 
311 	return NULL;
312 }
313 
push_whiteout(struct btree * b,struct bpos pos)314 static inline void push_whiteout(struct btree *b, struct bpos pos)
315 {
316 	struct bkey_packed k;
317 
318 	BUG_ON(bch2_btree_keys_u64s_remaining(b) < BKEY_U64s);
319 	EBUG_ON(btree_node_just_written(b));
320 
321 	if (!bkey_pack_pos(&k, pos, b)) {
322 		struct bkey *u = (void *) &k;
323 
324 		bkey_init(u);
325 		u->p = pos;
326 	}
327 
328 	k.needs_whiteout = true;
329 
330 	b->whiteout_u64s += k.u64s;
331 	bkey_p_copy(unwritten_whiteouts_start(b), &k);
332 }
333 
334 /*
335  * write lock must be held on @b (else the dirty bset that we were going to
336  * insert into could be written out from under us)
337  */
bch2_btree_node_insert_fits(struct btree * b,unsigned u64s)338 static inline bool bch2_btree_node_insert_fits(struct btree *b, unsigned u64s)
339 {
340 	if (unlikely(btree_node_need_rewrite(b)))
341 		return false;
342 
343 	return u64s <= bch2_btree_keys_u64s_remaining(b);
344 }
345 
346 void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *);
347 
348 bool bch2_btree_interior_updates_flush(struct bch_fs *);
349 
350 void bch2_journal_entry_to_btree_root(struct bch_fs *, struct jset_entry *);
351 struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *,
352 					struct jset_entry *, unsigned long);
353 
354 void bch2_async_btree_node_rewrites_flush(struct bch_fs *);
355 void bch2_do_pending_node_rewrites(struct bch_fs *);
356 void bch2_free_pending_node_rewrites(struct bch_fs *);
357 
358 void bch2_btree_reserve_cache_to_text(struct printbuf *, struct bch_fs *);
359 
360 void bch2_fs_btree_interior_update_exit(struct bch_fs *);
361 void bch2_fs_btree_interior_update_init_early(struct bch_fs *);
362 int bch2_fs_btree_interior_update_init(struct bch_fs *);
363 
364 #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */
365