xref: /linux/fs/bcachefs/btree_gc.c (revision c61fea676bcb5f14adcd882a7f7d9c5b082fe922)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
4  * Copyright (C) 2014 Datera Inc.
5  */
6 
7 #include "bcachefs.h"
8 #include "alloc_background.h"
9 #include "alloc_foreground.h"
10 #include "backpointers.h"
11 #include "bkey_methods.h"
12 #include "bkey_buf.h"
13 #include "btree_journal_iter.h"
14 #include "btree_key_cache.h"
15 #include "btree_locking.h"
16 #include "btree_node_scan.h"
17 #include "btree_update_interior.h"
18 #include "btree_io.h"
19 #include "btree_gc.h"
20 #include "buckets.h"
21 #include "clock.h"
22 #include "debug.h"
23 #include "ec.h"
24 #include "error.h"
25 #include "extents.h"
26 #include "journal.h"
27 #include "keylist.h"
28 #include "move.h"
29 #include "recovery_passes.h"
30 #include "reflink.h"
31 #include "replicas.h"
32 #include "super-io.h"
33 #include "trace.h"
34 
35 #include <linux/slab.h>
36 #include <linux/bitops.h>
37 #include <linux/freezer.h>
38 #include <linux/kthread.h>
39 #include <linux/preempt.h>
40 #include <linux/rcupdate.h>
41 #include <linux/sched/task.h>
42 
43 #define DROP_THIS_NODE		10
44 #define DROP_PREV_NODE		11
45 #define DID_FILL_FROM_SCAN	12
46 
47 static struct bkey_s unsafe_bkey_s_c_to_s(struct bkey_s_c k)
48 {
49 	return (struct bkey_s) {{{
50 		(struct bkey *) k.k,
51 		(struct bch_val *) k.v
52 	}}};
53 }
54 
55 static inline void __gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
56 {
57 	preempt_disable();
58 	write_seqcount_begin(&c->gc_pos_lock);
59 	c->gc_pos = new_pos;
60 	write_seqcount_end(&c->gc_pos_lock);
61 	preempt_enable();
62 }
63 
64 static inline void gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
65 {
66 	BUG_ON(gc_pos_cmp(new_pos, c->gc_pos) < 0);
67 	__gc_pos_set(c, new_pos);
68 }
69 
70 static void btree_ptr_to_v2(struct btree *b, struct bkey_i_btree_ptr_v2 *dst)
71 {
72 	switch (b->key.k.type) {
73 	case KEY_TYPE_btree_ptr: {
74 		struct bkey_i_btree_ptr *src = bkey_i_to_btree_ptr(&b->key);
75 
76 		dst->k.p		= src->k.p;
77 		dst->v.mem_ptr		= 0;
78 		dst->v.seq		= b->data->keys.seq;
79 		dst->v.sectors_written	= 0;
80 		dst->v.flags		= 0;
81 		dst->v.min_key		= b->data->min_key;
82 		set_bkey_val_bytes(&dst->k, sizeof(dst->v) + bkey_val_bytes(&src->k));
83 		memcpy(dst->v.start, src->v.start, bkey_val_bytes(&src->k));
84 		break;
85 	}
86 	case KEY_TYPE_btree_ptr_v2:
87 		bkey_copy(&dst->k_i, &b->key);
88 		break;
89 	default:
90 		BUG();
91 	}
92 }
93 
94 static int set_node_min(struct bch_fs *c, struct btree *b, struct bpos new_min)
95 {
96 	struct bkey_i_btree_ptr_v2 *new;
97 	int ret;
98 
99 	if (c->opts.verbose) {
100 		struct printbuf buf = PRINTBUF;
101 
102 		bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
103 		prt_str(&buf, " -> ");
104 		bch2_bpos_to_text(&buf, new_min);
105 
106 		bch_info(c, "%s(): %s", __func__, buf.buf);
107 		printbuf_exit(&buf);
108 	}
109 
110 	new = kmalloc_array(BKEY_BTREE_PTR_U64s_MAX, sizeof(u64), GFP_KERNEL);
111 	if (!new)
112 		return -BCH_ERR_ENOMEM_gc_repair_key;
113 
114 	btree_ptr_to_v2(b, new);
115 	b->data->min_key	= new_min;
116 	new->v.min_key		= new_min;
117 	SET_BTREE_PTR_RANGE_UPDATED(&new->v, true);
118 
119 	ret = bch2_journal_key_insert_take(c, b->c.btree_id, b->c.level + 1, &new->k_i);
120 	if (ret) {
121 		kfree(new);
122 		return ret;
123 	}
124 
125 	bch2_btree_node_drop_keys_outside_node(b);
126 	bkey_copy(&b->key, &new->k_i);
127 	return 0;
128 }
129 
130 static int set_node_max(struct bch_fs *c, struct btree *b, struct bpos new_max)
131 {
132 	struct bkey_i_btree_ptr_v2 *new;
133 	int ret;
134 
135 	if (c->opts.verbose) {
136 		struct printbuf buf = PRINTBUF;
137 
138 		bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
139 		prt_str(&buf, " -> ");
140 		bch2_bpos_to_text(&buf, new_max);
141 
142 		bch_info(c, "%s(): %s", __func__, buf.buf);
143 		printbuf_exit(&buf);
144 	}
145 
146 	ret = bch2_journal_key_delete(c, b->c.btree_id, b->c.level + 1, b->key.k.p);
147 	if (ret)
148 		return ret;
149 
150 	new = kmalloc_array(BKEY_BTREE_PTR_U64s_MAX, sizeof(u64), GFP_KERNEL);
151 	if (!new)
152 		return -BCH_ERR_ENOMEM_gc_repair_key;
153 
154 	btree_ptr_to_v2(b, new);
155 	b->data->max_key	= new_max;
156 	new->k.p		= new_max;
157 	SET_BTREE_PTR_RANGE_UPDATED(&new->v, true);
158 
159 	ret = bch2_journal_key_insert_take(c, b->c.btree_id, b->c.level + 1, &new->k_i);
160 	if (ret) {
161 		kfree(new);
162 		return ret;
163 	}
164 
165 	bch2_btree_node_drop_keys_outside_node(b);
166 
167 	mutex_lock(&c->btree_cache.lock);
168 	bch2_btree_node_hash_remove(&c->btree_cache, b);
169 
170 	bkey_copy(&b->key, &new->k_i);
171 	ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
172 	BUG_ON(ret);
173 	mutex_unlock(&c->btree_cache.lock);
174 	return 0;
175 }
176 
177 static int btree_check_node_boundaries(struct bch_fs *c, struct btree *b,
178 				       struct btree *prev, struct btree *cur,
179 				       struct bpos *pulled_from_scan)
180 {
181 	struct bpos expected_start = !prev
182 		? b->data->min_key
183 		: bpos_successor(prev->key.k.p);
184 	struct printbuf buf = PRINTBUF;
185 	int ret = 0;
186 
187 	BUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
188 	       !bpos_eq(bkey_i_to_btree_ptr_v2(&b->key)->v.min_key,
189 			b->data->min_key));
190 
191 	if (bpos_eq(expected_start, cur->data->min_key))
192 		return 0;
193 
194 	prt_printf(&buf, "  at btree %s level %u:\n  parent: ",
195 		   bch2_btree_id_str(b->c.btree_id), b->c.level);
196 	bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
197 
198 	if (prev) {
199 		prt_printf(&buf, "\n  prev: ");
200 		bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&prev->key));
201 	}
202 
203 	prt_str(&buf, "\n  next: ");
204 	bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&cur->key));
205 
206 	if (bpos_lt(expected_start, cur->data->min_key)) {				/* gap */
207 		if (b->c.level == 1 &&
208 		    bpos_lt(*pulled_from_scan, cur->data->min_key)) {
209 			ret = bch2_get_scanned_nodes(c, b->c.btree_id, 0,
210 						     expected_start,
211 						     bpos_predecessor(cur->data->min_key));
212 			if (ret)
213 				goto err;
214 
215 			*pulled_from_scan = cur->data->min_key;
216 			ret = DID_FILL_FROM_SCAN;
217 		} else {
218 			if (mustfix_fsck_err(c, btree_node_topology_bad_min_key,
219 					     "btree node with incorrect min_key%s", buf.buf))
220 				ret = set_node_min(c, cur, expected_start);
221 		}
222 	} else {									/* overlap */
223 		if (prev && BTREE_NODE_SEQ(cur->data) > BTREE_NODE_SEQ(prev->data)) {	/* cur overwrites prev */
224 			if (bpos_ge(prev->data->min_key, cur->data->min_key)) {		/* fully? */
225 				if (mustfix_fsck_err(c, btree_node_topology_overwritten_by_next_node,
226 						     "btree node overwritten by next node%s", buf.buf))
227 					ret = DROP_PREV_NODE;
228 			} else {
229 				if (mustfix_fsck_err(c, btree_node_topology_bad_max_key,
230 						     "btree node with incorrect max_key%s", buf.buf))
231 					ret = set_node_max(c, prev,
232 							   bpos_predecessor(cur->data->min_key));
233 			}
234 		} else {
235 			if (bpos_ge(expected_start, cur->data->max_key)) {		/* fully? */
236 				if (mustfix_fsck_err(c, btree_node_topology_overwritten_by_prev_node,
237 						     "btree node overwritten by prev node%s", buf.buf))
238 					ret = DROP_THIS_NODE;
239 			} else {
240 				if (mustfix_fsck_err(c, btree_node_topology_bad_min_key,
241 						     "btree node with incorrect min_key%s", buf.buf))
242 					ret = set_node_min(c, cur, expected_start);
243 			}
244 		}
245 	}
246 err:
247 fsck_err:
248 	printbuf_exit(&buf);
249 	return ret;
250 }
251 
252 static int btree_repair_node_end(struct bch_fs *c, struct btree *b,
253 				 struct btree *child, struct bpos *pulled_from_scan)
254 {
255 	struct printbuf buf = PRINTBUF;
256 	int ret = 0;
257 
258 	if (bpos_eq(child->key.k.p, b->key.k.p))
259 		return 0;
260 
261 	prt_printf(&buf, "at btree %s level %u:\n  parent: ",
262 		   bch2_btree_id_str(b->c.btree_id), b->c.level);
263 	bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
264 
265 	prt_str(&buf, "\n  child: ");
266 	bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&child->key));
267 
268 	if (mustfix_fsck_err(c, btree_node_topology_bad_max_key,
269 			     "btree node with incorrect max_key%s", buf.buf)) {
270 		if (b->c.level == 1 &&
271 		    bpos_lt(*pulled_from_scan, b->key.k.p)) {
272 			ret = bch2_get_scanned_nodes(c, b->c.btree_id, 0,
273 						bpos_successor(child->key.k.p), b->key.k.p);
274 			if (ret)
275 				goto err;
276 
277 			*pulled_from_scan = b->key.k.p;
278 			ret = DID_FILL_FROM_SCAN;
279 		} else {
280 			ret = set_node_max(c, child, b->key.k.p);
281 		}
282 	}
283 err:
284 fsck_err:
285 	printbuf_exit(&buf);
286 	return ret;
287 }
288 
289 static int bch2_btree_repair_topology_recurse(struct btree_trans *trans, struct btree *b,
290 					      struct bpos *pulled_from_scan)
291 {
292 	struct bch_fs *c = trans->c;
293 	struct btree_and_journal_iter iter;
294 	struct bkey_s_c k;
295 	struct bkey_buf prev_k, cur_k;
296 	struct btree *prev = NULL, *cur = NULL;
297 	bool have_child, new_pass = false;
298 	struct printbuf buf = PRINTBUF;
299 	int ret = 0;
300 
301 	if (!b->c.level)
302 		return 0;
303 
304 	bch2_bkey_buf_init(&prev_k);
305 	bch2_bkey_buf_init(&cur_k);
306 again:
307 	cur = prev = NULL;
308 	have_child = new_pass = false;
309 	bch2_btree_and_journal_iter_init_node_iter(trans, &iter, b);
310 	iter.prefetch = true;
311 
312 	while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
313 		BUG_ON(bpos_lt(k.k->p, b->data->min_key));
314 		BUG_ON(bpos_gt(k.k->p, b->data->max_key));
315 
316 		bch2_btree_and_journal_iter_advance(&iter);
317 		bch2_bkey_buf_reassemble(&cur_k, c, k);
318 
319 		cur = bch2_btree_node_get_noiter(trans, cur_k.k,
320 					b->c.btree_id, b->c.level - 1,
321 					false);
322 		ret = PTR_ERR_OR_ZERO(cur);
323 
324 		printbuf_reset(&buf);
325 		bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(cur_k.k));
326 
327 		if (mustfix_fsck_err_on(bch2_err_matches(ret, EIO), c,
328 				btree_node_unreadable,
329 				"Topology repair: unreadable btree node at btree %s level %u:\n"
330 				"  %s",
331 				bch2_btree_id_str(b->c.btree_id),
332 				b->c.level - 1,
333 				buf.buf)) {
334 			bch2_btree_node_evict(trans, cur_k.k);
335 			cur = NULL;
336 			ret = bch2_journal_key_delete(c, b->c.btree_id,
337 						      b->c.level, cur_k.k->k.p);
338 			if (ret)
339 				break;
340 
341 			if (!btree_id_is_alloc(b->c.btree_id)) {
342 				ret = bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_scan_for_btree_nodes);
343 				if (ret)
344 					break;
345 			}
346 			continue;
347 		}
348 
349 		bch_err_msg(c, ret, "getting btree node");
350 		if (ret)
351 			break;
352 
353 		if (bch2_btree_node_is_stale(c, cur)) {
354 			bch_info(c, "btree node %s older than nodes found by scanning", buf.buf);
355 			six_unlock_read(&cur->c.lock);
356 			bch2_btree_node_evict(trans, cur_k.k);
357 			ret = bch2_journal_key_delete(c, b->c.btree_id,
358 						      b->c.level, cur_k.k->k.p);
359 			cur = NULL;
360 			if (ret)
361 				break;
362 			continue;
363 		}
364 
365 		ret = btree_check_node_boundaries(c, b, prev, cur, pulled_from_scan);
366 		if (ret == DID_FILL_FROM_SCAN) {
367 			new_pass = true;
368 			ret = 0;
369 		}
370 
371 		if (ret == DROP_THIS_NODE) {
372 			six_unlock_read(&cur->c.lock);
373 			bch2_btree_node_evict(trans, cur_k.k);
374 			ret = bch2_journal_key_delete(c, b->c.btree_id,
375 						      b->c.level, cur_k.k->k.p);
376 			cur = NULL;
377 			if (ret)
378 				break;
379 			continue;
380 		}
381 
382 		if (prev)
383 			six_unlock_read(&prev->c.lock);
384 		prev = NULL;
385 
386 		if (ret == DROP_PREV_NODE) {
387 			bch_info(c, "dropped prev node");
388 			bch2_btree_node_evict(trans, prev_k.k);
389 			ret = bch2_journal_key_delete(c, b->c.btree_id,
390 						      b->c.level, prev_k.k->k.p);
391 			if (ret)
392 				break;
393 
394 			bch2_btree_and_journal_iter_exit(&iter);
395 			goto again;
396 		} else if (ret)
397 			break;
398 
399 		prev = cur;
400 		cur = NULL;
401 		bch2_bkey_buf_copy(&prev_k, c, cur_k.k);
402 	}
403 
404 	if (!ret && !IS_ERR_OR_NULL(prev)) {
405 		BUG_ON(cur);
406 		ret = btree_repair_node_end(c, b, prev, pulled_from_scan);
407 		if (ret == DID_FILL_FROM_SCAN) {
408 			new_pass = true;
409 			ret = 0;
410 		}
411 	}
412 
413 	if (!IS_ERR_OR_NULL(prev))
414 		six_unlock_read(&prev->c.lock);
415 	prev = NULL;
416 	if (!IS_ERR_OR_NULL(cur))
417 		six_unlock_read(&cur->c.lock);
418 	cur = NULL;
419 
420 	if (ret)
421 		goto err;
422 
423 	bch2_btree_and_journal_iter_exit(&iter);
424 
425 	if (new_pass)
426 		goto again;
427 
428 	bch2_btree_and_journal_iter_init_node_iter(trans, &iter, b);
429 	iter.prefetch = true;
430 
431 	while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
432 		bch2_bkey_buf_reassemble(&cur_k, c, k);
433 		bch2_btree_and_journal_iter_advance(&iter);
434 
435 		cur = bch2_btree_node_get_noiter(trans, cur_k.k,
436 					b->c.btree_id, b->c.level - 1,
437 					false);
438 		ret = PTR_ERR_OR_ZERO(cur);
439 
440 		bch_err_msg(c, ret, "getting btree node");
441 		if (ret)
442 			goto err;
443 
444 		ret = bch2_btree_repair_topology_recurse(trans, cur, pulled_from_scan);
445 		six_unlock_read(&cur->c.lock);
446 		cur = NULL;
447 
448 		if (ret == DROP_THIS_NODE) {
449 			bch2_btree_node_evict(trans, cur_k.k);
450 			ret = bch2_journal_key_delete(c, b->c.btree_id,
451 						      b->c.level, cur_k.k->k.p);
452 			new_pass = true;
453 		}
454 
455 		if (ret)
456 			goto err;
457 
458 		have_child = true;
459 	}
460 
461 	printbuf_reset(&buf);
462 	bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
463 
464 	if (mustfix_fsck_err_on(!have_child, c,
465 			btree_node_topology_interior_node_empty,
466 			"empty interior btree node at btree %s level %u\n"
467 			"  %s",
468 			bch2_btree_id_str(b->c.btree_id),
469 			b->c.level, buf.buf))
470 		ret = DROP_THIS_NODE;
471 err:
472 fsck_err:
473 	if (!IS_ERR_OR_NULL(prev))
474 		six_unlock_read(&prev->c.lock);
475 	if (!IS_ERR_OR_NULL(cur))
476 		six_unlock_read(&cur->c.lock);
477 
478 	bch2_btree_and_journal_iter_exit(&iter);
479 
480 	if (!ret && new_pass)
481 		goto again;
482 
483 	BUG_ON(!ret && bch2_btree_node_check_topology(trans, b));
484 
485 	bch2_bkey_buf_exit(&prev_k, c);
486 	bch2_bkey_buf_exit(&cur_k, c);
487 	printbuf_exit(&buf);
488 	return ret;
489 }
490 
491 int bch2_check_topology(struct bch_fs *c)
492 {
493 	struct btree_trans *trans = bch2_trans_get(c);
494 	struct bpos pulled_from_scan = POS_MIN;
495 	int ret = 0;
496 
497 	for (unsigned i = 0; i < btree_id_nr_alive(c) && !ret; i++) {
498 		struct btree_root *r = bch2_btree_id_root(c, i);
499 		bool reconstructed_root = false;
500 
501 		if (r->error) {
502 			ret = bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_scan_for_btree_nodes);
503 			if (ret)
504 				break;
505 reconstruct_root:
506 			bch_info(c, "btree root %s unreadable, must recover from scan", bch2_btree_id_str(i));
507 
508 			r->alive = false;
509 			r->error = 0;
510 
511 			if (!bch2_btree_has_scanned_nodes(c, i)) {
512 				mustfix_fsck_err(c, btree_root_unreadable_and_scan_found_nothing,
513 						 "no nodes found for btree %s, continue?", bch2_btree_id_str(i));
514 				bch2_btree_root_alloc_fake_trans(trans, i, 0);
515 			} else {
516 				bch2_btree_root_alloc_fake_trans(trans, i, 1);
517 				bch2_shoot_down_journal_keys(c, i, 1, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX);
518 				ret = bch2_get_scanned_nodes(c, i, 0, POS_MIN, SPOS_MAX);
519 				if (ret)
520 					break;
521 			}
522 
523 			reconstructed_root = true;
524 		}
525 
526 		struct btree *b = r->b;
527 
528 		btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_read);
529 		ret = bch2_btree_repair_topology_recurse(trans, b, &pulled_from_scan);
530 		six_unlock_read(&b->c.lock);
531 
532 		if (ret == DROP_THIS_NODE) {
533 			bch2_btree_node_hash_remove(&c->btree_cache, b);
534 			mutex_lock(&c->btree_cache.lock);
535 			list_move(&b->list, &c->btree_cache.freeable);
536 			mutex_unlock(&c->btree_cache.lock);
537 
538 			r->b = NULL;
539 
540 			if (!reconstructed_root)
541 				goto reconstruct_root;
542 
543 			bch_err(c, "empty btree root %s", bch2_btree_id_str(i));
544 			bch2_btree_root_alloc_fake_trans(trans, i, 0);
545 			r->alive = false;
546 			ret = 0;
547 		}
548 	}
549 fsck_err:
550 	bch2_trans_put(trans);
551 	return ret;
552 }
553 
554 /* marking of btree keys/nodes: */
555 
556 static int bch2_gc_mark_key(struct btree_trans *trans, enum btree_id btree_id,
557 			    unsigned level, struct btree **prev,
558 			    struct btree_iter *iter, struct bkey_s_c k,
559 			    bool initial)
560 {
561 	struct bch_fs *c = trans->c;
562 
563 	if (iter) {
564 		struct btree_path *path = btree_iter_path(trans, iter);
565 		struct btree *b = path_l(path)->b;
566 
567 		if (*prev != b) {
568 			int ret = bch2_btree_node_check_topology(trans, b);
569 			if (ret)
570 				return ret;
571 		}
572 		*prev = b;
573 	}
574 
575 	struct bkey deleted = KEY(0, 0, 0);
576 	struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
577 	struct printbuf buf = PRINTBUF;
578 	int ret = 0;
579 
580 	deleted.p = k.k->p;
581 
582 	if (initial) {
583 		BUG_ON(bch2_journal_seq_verify &&
584 		       k.k->version.lo > atomic64_read(&c->journal.seq));
585 
586 		if (fsck_err_on(k.k->version.lo > atomic64_read(&c->key_version), c,
587 				bkey_version_in_future,
588 				"key version number higher than recorded: %llu > %llu",
589 				k.k->version.lo,
590 				atomic64_read(&c->key_version)))
591 			atomic64_set(&c->key_version, k.k->version.lo);
592 	}
593 
594 	if (mustfix_fsck_err_on(level && !bch2_dev_btree_bitmap_marked(c, k),
595 				c, btree_bitmap_not_marked,
596 				"btree ptr not marked in member info btree allocated bitmap\n  %s",
597 				(bch2_bkey_val_to_text(&buf, c, k),
598 				 buf.buf))) {
599 		mutex_lock(&c->sb_lock);
600 		bch2_dev_btree_bitmap_mark(c, k);
601 		bch2_write_super(c);
602 		mutex_unlock(&c->sb_lock);
603 	}
604 
605 	/*
606 	 * We require a commit before key_trigger() because
607 	 * key_trigger(BTREE_TRIGGER_GC) is not idempotant; we'll calculate the
608 	 * wrong result if we run it multiple times.
609 	 */
610 	unsigned flags = !iter ? BTREE_TRIGGER_is_root : 0;
611 
612 	ret = bch2_key_trigger(trans, btree_id, level, old, unsafe_bkey_s_c_to_s(k),
613 			       BTREE_TRIGGER_check_repair|flags);
614 	if (ret)
615 		goto out;
616 
617 	if (trans->nr_updates) {
618 		ret = bch2_trans_commit(trans, NULL, NULL, 0) ?:
619 			-BCH_ERR_transaction_restart_nested;
620 		goto out;
621 	}
622 
623 	ret = bch2_key_trigger(trans, btree_id, level, old, unsafe_bkey_s_c_to_s(k),
624 			       BTREE_TRIGGER_gc|flags);
625 out:
626 fsck_err:
627 	printbuf_exit(&buf);
628 	bch_err_fn(c, ret);
629 	return ret;
630 }
631 
632 static int bch2_gc_btree(struct btree_trans *trans, enum btree_id btree, bool initial)
633 {
634 	struct bch_fs *c = trans->c;
635 	int level = 0, target_depth = btree_node_type_needs_gc(__btree_node_type(0, btree)) ? 0 : 1;
636 	int ret = 0;
637 
638 	/* We need to make sure every leaf node is readable before going RW */
639 	if (initial)
640 		target_depth = 0;
641 
642 	/* root */
643 	mutex_lock(&c->btree_root_lock);
644 	struct btree *b = bch2_btree_id_root(c, btree)->b;
645 	if (!btree_node_fake(b)) {
646 		gc_pos_set(c, gc_pos_btree(btree, b->c.level + 1, SPOS_MAX));
647 		ret = lockrestart_do(trans,
648 			bch2_gc_mark_key(trans, b->c.btree_id, b->c.level + 1,
649 					 NULL, NULL, bkey_i_to_s_c(&b->key), initial));
650 		level = b->c.level;
651 	}
652 	mutex_unlock(&c->btree_root_lock);
653 
654 	if (ret)
655 		return ret;
656 
657 	for (; level >= target_depth; --level) {
658 		struct btree *prev = NULL;
659 		struct btree_iter iter;
660 		bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, level,
661 					  BTREE_ITER_prefetch);
662 
663 		ret = for_each_btree_key_continue(trans, iter, 0, k, ({
664 			gc_pos_set(c, gc_pos_btree(btree, level, k.k->p));
665 			bch2_gc_mark_key(trans, btree, level, &prev, &iter, k, initial);
666 		}));
667 		if (ret)
668 			break;
669 	}
670 
671 	return ret;
672 }
673 
674 static inline int btree_id_gc_phase_cmp(enum btree_id l, enum btree_id r)
675 {
676 	return  (int) btree_id_to_gc_phase(l) -
677 		(int) btree_id_to_gc_phase(r);
678 }
679 
680 static int bch2_gc_btrees(struct bch_fs *c)
681 {
682 	struct btree_trans *trans = bch2_trans_get(c);
683 	enum btree_id ids[BTREE_ID_NR];
684 	unsigned i;
685 	int ret = 0;
686 
687 	for (i = 0; i < BTREE_ID_NR; i++)
688 		ids[i] = i;
689 	bubble_sort(ids, BTREE_ID_NR, btree_id_gc_phase_cmp);
690 
691 	for (i = 0; i < btree_id_nr_alive(c) && !ret; i++) {
692 		unsigned btree = i < BTREE_ID_NR ? ids[i] : i;
693 
694 		if (IS_ERR_OR_NULL(bch2_btree_id_root(c, btree)->b))
695 			continue;
696 
697 		ret = bch2_gc_btree(trans, btree, true);
698 
699 		if (mustfix_fsck_err_on(bch2_err_matches(ret, EIO),
700 					c, btree_node_read_error,
701 			       "btree node read error for %s",
702 			       bch2_btree_id_str(btree)))
703 			ret = bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology);
704 	}
705 fsck_err:
706 	bch2_trans_put(trans);
707 	bch_err_fn(c, ret);
708 	return ret;
709 }
710 
711 static int bch2_mark_superblocks(struct bch_fs *c)
712 {
713 	mutex_lock(&c->sb_lock);
714 	gc_pos_set(c, gc_phase(GC_PHASE_SB));
715 
716 	int ret = bch2_trans_mark_dev_sbs_flags(c, BTREE_TRIGGER_gc);
717 	mutex_unlock(&c->sb_lock);
718 	return ret;
719 }
720 
721 static void bch2_gc_free(struct bch_fs *c)
722 {
723 	genradix_free(&c->reflink_gc_table);
724 	genradix_free(&c->gc_stripes);
725 
726 	for_each_member_device(c, ca) {
727 		kvfree(rcu_dereference_protected(ca->buckets_gc, 1));
728 		ca->buckets_gc = NULL;
729 
730 		free_percpu(ca->usage_gc);
731 		ca->usage_gc = NULL;
732 	}
733 
734 	free_percpu(c->usage_gc);
735 	c->usage_gc = NULL;
736 }
737 
738 static int bch2_gc_done(struct bch_fs *c)
739 {
740 	struct bch_dev *ca = NULL;
741 	struct printbuf buf = PRINTBUF;
742 	unsigned i;
743 	int ret = 0;
744 
745 	percpu_down_write(&c->mark_lock);
746 
747 #define copy_field(_err, _f, _msg, ...)						\
748 	if (fsck_err_on(dst->_f != src->_f, c, _err,				\
749 			_msg ": got %llu, should be %llu" , ##__VA_ARGS__,	\
750 			dst->_f, src->_f))					\
751 		dst->_f = src->_f
752 #define copy_dev_field(_err, _f, _msg, ...)					\
753 	copy_field(_err, _f, "dev %u has wrong " _msg, ca->dev_idx, ##__VA_ARGS__)
754 #define copy_fs_field(_err, _f, _msg, ...)					\
755 	copy_field(_err, _f, "fs has wrong " _msg, ##__VA_ARGS__)
756 
757 	for (i = 0; i < ARRAY_SIZE(c->usage); i++)
758 		bch2_fs_usage_acc_to_base(c, i);
759 
760 	__for_each_member_device(c, ca) {
761 		struct bch_dev_usage *dst = ca->usage_base;
762 		struct bch_dev_usage *src = (void *)
763 			bch2_acc_percpu_u64s((u64 __percpu *) ca->usage_gc,
764 					     dev_usage_u64s());
765 
766 		for (i = 0; i < BCH_DATA_NR; i++) {
767 			copy_dev_field(dev_usage_buckets_wrong,
768 				       d[i].buckets,	"%s buckets", bch2_data_type_str(i));
769 			copy_dev_field(dev_usage_sectors_wrong,
770 				       d[i].sectors,	"%s sectors", bch2_data_type_str(i));
771 			copy_dev_field(dev_usage_fragmented_wrong,
772 				       d[i].fragmented,	"%s fragmented", bch2_data_type_str(i));
773 		}
774 	}
775 
776 	{
777 		unsigned nr = fs_usage_u64s(c);
778 		struct bch_fs_usage *dst = c->usage_base;
779 		struct bch_fs_usage *src = (void *)
780 			bch2_acc_percpu_u64s((u64 __percpu *) c->usage_gc, nr);
781 
782 		copy_fs_field(fs_usage_hidden_wrong,
783 			      b.hidden,		"hidden");
784 		copy_fs_field(fs_usage_btree_wrong,
785 			      b.btree,		"btree");
786 
787 		copy_fs_field(fs_usage_data_wrong,
788 			      b.data,	"data");
789 		copy_fs_field(fs_usage_cached_wrong,
790 			      b.cached,	"cached");
791 		copy_fs_field(fs_usage_reserved_wrong,
792 			      b.reserved,	"reserved");
793 		copy_fs_field(fs_usage_nr_inodes_wrong,
794 			      b.nr_inodes,"nr_inodes");
795 
796 		for (i = 0; i < BCH_REPLICAS_MAX; i++)
797 			copy_fs_field(fs_usage_persistent_reserved_wrong,
798 				      persistent_reserved[i],
799 				      "persistent_reserved[%i]", i);
800 
801 		for (i = 0; i < c->replicas.nr; i++) {
802 			struct bch_replicas_entry_v1 *e =
803 				cpu_replicas_entry(&c->replicas, i);
804 
805 			printbuf_reset(&buf);
806 			bch2_replicas_entry_to_text(&buf, e);
807 
808 			copy_fs_field(fs_usage_replicas_wrong,
809 				      replicas[i], "%s", buf.buf);
810 		}
811 	}
812 
813 #undef copy_fs_field
814 #undef copy_dev_field
815 #undef copy_stripe_field
816 #undef copy_field
817 fsck_err:
818 	bch2_dev_put(ca);
819 	bch_err_fn(c, ret);
820 	percpu_up_write(&c->mark_lock);
821 	printbuf_exit(&buf);
822 	return ret;
823 }
824 
825 static int bch2_gc_start(struct bch_fs *c)
826 {
827 	BUG_ON(c->usage_gc);
828 
829 	c->usage_gc = __alloc_percpu_gfp(fs_usage_u64s(c) * sizeof(u64),
830 					 sizeof(u64), GFP_KERNEL);
831 	if (!c->usage_gc) {
832 		bch_err(c, "error allocating c->usage_gc");
833 		return -BCH_ERR_ENOMEM_gc_start;
834 	}
835 
836 	for_each_member_device(c, ca) {
837 		BUG_ON(ca->usage_gc);
838 
839 		ca->usage_gc = alloc_percpu(struct bch_dev_usage);
840 		if (!ca->usage_gc) {
841 			bch_err(c, "error allocating ca->usage_gc");
842 			bch2_dev_put(ca);
843 			return -BCH_ERR_ENOMEM_gc_start;
844 		}
845 
846 		this_cpu_write(ca->usage_gc->d[BCH_DATA_free].buckets,
847 			       ca->mi.nbuckets - ca->mi.first_bucket);
848 	}
849 
850 	return 0;
851 }
852 
853 /* returns true if not equal */
854 static inline bool bch2_alloc_v4_cmp(struct bch_alloc_v4 l,
855 				     struct bch_alloc_v4 r)
856 {
857 	return  l.gen != r.gen				||
858 		l.oldest_gen != r.oldest_gen		||
859 		l.data_type != r.data_type		||
860 		l.dirty_sectors	!= r.dirty_sectors	||
861 		l.cached_sectors != r.cached_sectors	 ||
862 		l.stripe_redundancy != r.stripe_redundancy ||
863 		l.stripe != r.stripe;
864 }
865 
866 static int bch2_alloc_write_key(struct btree_trans *trans,
867 				struct btree_iter *iter,
868 				struct bch_dev *ca,
869 				struct bkey_s_c k)
870 {
871 	struct bch_fs *c = trans->c;
872 	struct bkey_i_alloc_v4 *a;
873 	struct bch_alloc_v4 old_gc, gc, old_convert, new;
874 	const struct bch_alloc_v4 *old;
875 	int ret;
876 
877 	old = bch2_alloc_to_v4(k, &old_convert);
878 	gc = new = *old;
879 
880 	percpu_down_read(&c->mark_lock);
881 	__bucket_m_to_alloc(&gc, *gc_bucket(ca, iter->pos.offset));
882 
883 	old_gc = gc;
884 
885 	if ((old->data_type == BCH_DATA_sb ||
886 	     old->data_type == BCH_DATA_journal) &&
887 	    !bch2_dev_is_online(ca)) {
888 		gc.data_type = old->data_type;
889 		gc.dirty_sectors = old->dirty_sectors;
890 	}
891 
892 	/*
893 	 * gc.data_type doesn't yet include need_discard & need_gc_gen states -
894 	 * fix that here:
895 	 */
896 	alloc_data_type_set(&gc, gc.data_type);
897 
898 	if (gc.data_type != old_gc.data_type ||
899 	    gc.dirty_sectors != old_gc.dirty_sectors)
900 		bch2_dev_usage_update(c, ca, &old_gc, &gc, 0, true);
901 	percpu_up_read(&c->mark_lock);
902 
903 	if (fsck_err_on(new.data_type != gc.data_type, c,
904 			alloc_key_data_type_wrong,
905 			"bucket %llu:%llu gen %u has wrong data_type"
906 			": got %s, should be %s",
907 			iter->pos.inode, iter->pos.offset,
908 			gc.gen,
909 			bch2_data_type_str(new.data_type),
910 			bch2_data_type_str(gc.data_type)))
911 		new.data_type = gc.data_type;
912 
913 #define copy_bucket_field(_errtype, _f)					\
914 	if (fsck_err_on(new._f != gc._f, c, _errtype,			\
915 			"bucket %llu:%llu gen %u data type %s has wrong " #_f	\
916 			": got %u, should be %u",			\
917 			iter->pos.inode, iter->pos.offset,		\
918 			gc.gen,						\
919 			bch2_data_type_str(gc.data_type),		\
920 			new._f, gc._f))					\
921 		new._f = gc._f;						\
922 
923 	copy_bucket_field(alloc_key_gen_wrong,
924 			  gen);
925 	copy_bucket_field(alloc_key_dirty_sectors_wrong,
926 			  dirty_sectors);
927 	copy_bucket_field(alloc_key_cached_sectors_wrong,
928 			  cached_sectors);
929 	copy_bucket_field(alloc_key_stripe_wrong,
930 			  stripe);
931 	copy_bucket_field(alloc_key_stripe_redundancy_wrong,
932 			  stripe_redundancy);
933 #undef copy_bucket_field
934 
935 	if (!bch2_alloc_v4_cmp(*old, new))
936 		return 0;
937 
938 	a = bch2_alloc_to_v4_mut(trans, k);
939 	ret = PTR_ERR_OR_ZERO(a);
940 	if (ret)
941 		return ret;
942 
943 	a->v = new;
944 
945 	/*
946 	 * The trigger normally makes sure this is set, but we're not running
947 	 * triggers:
948 	 */
949 	if (a->v.data_type == BCH_DATA_cached && !a->v.io_time[READ])
950 		a->v.io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
951 
952 	ret = bch2_trans_update(trans, iter, &a->k_i, BTREE_TRIGGER_norun);
953 fsck_err:
954 	return ret;
955 }
956 
957 static int bch2_gc_alloc_done(struct bch_fs *c)
958 {
959 	int ret = 0;
960 
961 	for_each_member_device(c, ca) {
962 		ret = bch2_trans_run(c,
963 			for_each_btree_key_upto_commit(trans, iter, BTREE_ID_alloc,
964 					POS(ca->dev_idx, ca->mi.first_bucket),
965 					POS(ca->dev_idx, ca->mi.nbuckets - 1),
966 					BTREE_ITER_slots|BTREE_ITER_prefetch, k,
967 					NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
968 				bch2_alloc_write_key(trans, &iter, ca, k)));
969 		if (ret) {
970 			bch2_dev_put(ca);
971 			break;
972 		}
973 	}
974 
975 	bch_err_fn(c, ret);
976 	return ret;
977 }
978 
979 static int bch2_gc_alloc_start(struct bch_fs *c)
980 {
981 	for_each_member_device(c, ca) {
982 		struct bucket_array *buckets = kvmalloc(sizeof(struct bucket_array) +
983 				ca->mi.nbuckets * sizeof(struct bucket),
984 				GFP_KERNEL|__GFP_ZERO);
985 		if (!buckets) {
986 			bch2_dev_put(ca);
987 			bch_err(c, "error allocating ca->buckets[gc]");
988 			return -BCH_ERR_ENOMEM_gc_alloc_start;
989 		}
990 
991 		buckets->first_bucket	= ca->mi.first_bucket;
992 		buckets->nbuckets	= ca->mi.nbuckets;
993 		rcu_assign_pointer(ca->buckets_gc, buckets);
994 	}
995 
996 	struct bch_dev *ca = NULL;
997 	int ret = bch2_trans_run(c,
998 		for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
999 					 BTREE_ITER_prefetch, k, ({
1000 			ca = bch2_dev_iterate(c, ca, k.k->p.inode);
1001 			if (!ca) {
1002 				bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
1003 				continue;
1004 			}
1005 
1006 			struct bch_alloc_v4 a_convert;
1007 			const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &a_convert);
1008 
1009 			struct bucket *g = gc_bucket(ca, k.k->p.offset);
1010 			g->gen_valid	= 1;
1011 			g->gen		= a->gen;
1012 			0;
1013 		})));
1014 	bch2_dev_put(ca);
1015 	bch_err_fn(c, ret);
1016 	return ret;
1017 }
1018 
1019 static int bch2_gc_write_reflink_key(struct btree_trans *trans,
1020 				     struct btree_iter *iter,
1021 				     struct bkey_s_c k,
1022 				     size_t *idx)
1023 {
1024 	struct bch_fs *c = trans->c;
1025 	const __le64 *refcount = bkey_refcount_c(k);
1026 	struct printbuf buf = PRINTBUF;
1027 	struct reflink_gc *r;
1028 	int ret = 0;
1029 
1030 	if (!refcount)
1031 		return 0;
1032 
1033 	while ((r = genradix_ptr(&c->reflink_gc_table, *idx)) &&
1034 	       r->offset < k.k->p.offset)
1035 		++*idx;
1036 
1037 	if (!r ||
1038 	    r->offset != k.k->p.offset ||
1039 	    r->size != k.k->size) {
1040 		bch_err(c, "unexpected inconsistency walking reflink table at gc finish");
1041 		return -EINVAL;
1042 	}
1043 
1044 	if (fsck_err_on(r->refcount != le64_to_cpu(*refcount), c,
1045 			reflink_v_refcount_wrong,
1046 			"reflink key has wrong refcount:\n"
1047 			"  %s\n"
1048 			"  should be %u",
1049 			(bch2_bkey_val_to_text(&buf, c, k), buf.buf),
1050 			r->refcount)) {
1051 		struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1052 		ret = PTR_ERR_OR_ZERO(new);
1053 		if (ret)
1054 			goto out;
1055 
1056 		if (!r->refcount)
1057 			new->k.type = KEY_TYPE_deleted;
1058 		else
1059 			*bkey_refcount(bkey_i_to_s(new)) = cpu_to_le64(r->refcount);
1060 		ret = bch2_trans_update(trans, iter, new, 0);
1061 	}
1062 out:
1063 fsck_err:
1064 	printbuf_exit(&buf);
1065 	return ret;
1066 }
1067 
1068 static int bch2_gc_reflink_done(struct bch_fs *c)
1069 {
1070 	size_t idx = 0;
1071 
1072 	int ret = bch2_trans_run(c,
1073 		for_each_btree_key_commit(trans, iter,
1074 				BTREE_ID_reflink, POS_MIN,
1075 				BTREE_ITER_prefetch, k,
1076 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1077 			bch2_gc_write_reflink_key(trans, &iter, k, &idx)));
1078 	c->reflink_gc_nr = 0;
1079 	return ret;
1080 }
1081 
1082 static int bch2_gc_reflink_start(struct bch_fs *c)
1083 {
1084 	c->reflink_gc_nr = 0;
1085 
1086 	int ret = bch2_trans_run(c,
1087 		for_each_btree_key(trans, iter, BTREE_ID_reflink, POS_MIN,
1088 				   BTREE_ITER_prefetch, k, ({
1089 			const __le64 *refcount = bkey_refcount_c(k);
1090 
1091 			if (!refcount)
1092 				continue;
1093 
1094 			struct reflink_gc *r = genradix_ptr_alloc(&c->reflink_gc_table,
1095 							c->reflink_gc_nr++, GFP_KERNEL);
1096 			if (!r) {
1097 				ret = -BCH_ERR_ENOMEM_gc_reflink_start;
1098 				break;
1099 			}
1100 
1101 			r->offset	= k.k->p.offset;
1102 			r->size		= k.k->size;
1103 			r->refcount	= 0;
1104 			0;
1105 		})));
1106 
1107 	bch_err_fn(c, ret);
1108 	return ret;
1109 }
1110 
1111 static int bch2_gc_write_stripes_key(struct btree_trans *trans,
1112 				     struct btree_iter *iter,
1113 				     struct bkey_s_c k)
1114 {
1115 	struct bch_fs *c = trans->c;
1116 	struct printbuf buf = PRINTBUF;
1117 	const struct bch_stripe *s;
1118 	struct gc_stripe *m;
1119 	bool bad = false;
1120 	unsigned i;
1121 	int ret = 0;
1122 
1123 	if (k.k->type != KEY_TYPE_stripe)
1124 		return 0;
1125 
1126 	s = bkey_s_c_to_stripe(k).v;
1127 	m = genradix_ptr(&c->gc_stripes, k.k->p.offset);
1128 
1129 	for (i = 0; i < s->nr_blocks; i++) {
1130 		u32 old = stripe_blockcount_get(s, i);
1131 		u32 new = (m ? m->block_sectors[i] : 0);
1132 
1133 		if (old != new) {
1134 			prt_printf(&buf, "stripe block %u has wrong sector count: got %u, should be %u\n",
1135 				   i, old, new);
1136 			bad = true;
1137 		}
1138 	}
1139 
1140 	if (bad)
1141 		bch2_bkey_val_to_text(&buf, c, k);
1142 
1143 	if (fsck_err_on(bad, c, stripe_sector_count_wrong,
1144 			"%s", buf.buf)) {
1145 		struct bkey_i_stripe *new;
1146 
1147 		new = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1148 		ret = PTR_ERR_OR_ZERO(new);
1149 		if (ret)
1150 			return ret;
1151 
1152 		bkey_reassemble(&new->k_i, k);
1153 
1154 		for (i = 0; i < new->v.nr_blocks; i++)
1155 			stripe_blockcount_set(&new->v, i, m ? m->block_sectors[i] : 0);
1156 
1157 		ret = bch2_trans_update(trans, iter, &new->k_i, 0);
1158 	}
1159 fsck_err:
1160 	printbuf_exit(&buf);
1161 	return ret;
1162 }
1163 
1164 static int bch2_gc_stripes_done(struct bch_fs *c)
1165 {
1166 	return bch2_trans_run(c,
1167 		for_each_btree_key_commit(trans, iter,
1168 				BTREE_ID_stripes, POS_MIN,
1169 				BTREE_ITER_prefetch, k,
1170 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1171 			bch2_gc_write_stripes_key(trans, &iter, k)));
1172 }
1173 
1174 /**
1175  * bch2_check_allocations - walk all references to buckets, and recompute them:
1176  *
1177  * @c:			filesystem object
1178  *
1179  * Returns: 0 on success, or standard errcode on failure
1180  *
1181  * Order matters here:
1182  *  - Concurrent GC relies on the fact that we have a total ordering for
1183  *    everything that GC walks - see  gc_will_visit_node(),
1184  *    gc_will_visit_root()
1185  *
1186  *  - also, references move around in the course of index updates and
1187  *    various other crap: everything needs to agree on the ordering
1188  *    references are allowed to move around in - e.g., we're allowed to
1189  *    start with a reference owned by an open_bucket (the allocator) and
1190  *    move it to the btree, but not the reverse.
1191  *
1192  *    This is necessary to ensure that gc doesn't miss references that
1193  *    move around - if references move backwards in the ordering GC
1194  *    uses, GC could skip past them
1195  */
1196 int bch2_check_allocations(struct bch_fs *c)
1197 {
1198 	int ret;
1199 
1200 	lockdep_assert_held(&c->state_lock);
1201 
1202 	down_write(&c->gc_lock);
1203 
1204 	bch2_btree_interior_updates_flush(c);
1205 
1206 	ret   = bch2_gc_start(c) ?:
1207 		bch2_gc_alloc_start(c) ?:
1208 		bch2_gc_reflink_start(c);
1209 	if (ret)
1210 		goto out;
1211 
1212 	gc_pos_set(c, gc_phase(GC_PHASE_START));
1213 
1214 	ret = bch2_mark_superblocks(c);
1215 	BUG_ON(ret);
1216 
1217 	ret = bch2_gc_btrees(c);
1218 	if (ret)
1219 		goto out;
1220 
1221 	c->gc_count++;
1222 
1223 	bch2_journal_block(&c->journal);
1224 out:
1225 	ret   = bch2_gc_alloc_done(c) ?:
1226 		bch2_gc_done(c) ?:
1227 		bch2_gc_stripes_done(c) ?:
1228 		bch2_gc_reflink_done(c);
1229 
1230 	bch2_journal_unblock(&c->journal);
1231 
1232 	percpu_down_write(&c->mark_lock);
1233 	/* Indicates that gc is no longer in progress: */
1234 	__gc_pos_set(c, gc_phase(GC_PHASE_NOT_RUNNING));
1235 
1236 	bch2_gc_free(c);
1237 	percpu_up_write(&c->mark_lock);
1238 
1239 	up_write(&c->gc_lock);
1240 
1241 	/*
1242 	 * At startup, allocations can happen directly instead of via the
1243 	 * allocator thread - issue wakeup in case they blocked on gc_lock:
1244 	 */
1245 	closure_wake_up(&c->freelist_wait);
1246 	bch_err_fn(c, ret);
1247 	return ret;
1248 }
1249 
1250 static int gc_btree_gens_key(struct btree_trans *trans,
1251 			     struct btree_iter *iter,
1252 			     struct bkey_s_c k)
1253 {
1254 	struct bch_fs *c = trans->c;
1255 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1256 	struct bkey_i *u;
1257 	int ret;
1258 
1259 	if (unlikely(test_bit(BCH_FS_going_ro, &c->flags)))
1260 		return -EROFS;
1261 
1262 	percpu_down_read(&c->mark_lock);
1263 	rcu_read_lock();
1264 	bkey_for_each_ptr(ptrs, ptr) {
1265 		struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev);
1266 		if (!ca)
1267 			continue;
1268 
1269 		if (dev_ptr_stale(ca, ptr) > 16) {
1270 			rcu_read_unlock();
1271 			percpu_up_read(&c->mark_lock);
1272 			goto update;
1273 		}
1274 	}
1275 
1276 	bkey_for_each_ptr(ptrs, ptr) {
1277 		struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev);
1278 		if (!ca)
1279 			continue;
1280 
1281 		u8 *gen = &ca->oldest_gen[PTR_BUCKET_NR(ca, ptr)];
1282 		if (gen_after(*gen, ptr->gen))
1283 			*gen = ptr->gen;
1284 	}
1285 	rcu_read_unlock();
1286 	percpu_up_read(&c->mark_lock);
1287 	return 0;
1288 update:
1289 	u = bch2_bkey_make_mut(trans, iter, &k, 0);
1290 	ret = PTR_ERR_OR_ZERO(u);
1291 	if (ret)
1292 		return ret;
1293 
1294 	bch2_extent_normalize(c, bkey_i_to_s(u));
1295 	return 0;
1296 }
1297 
1298 static int bch2_alloc_write_oldest_gen(struct btree_trans *trans, struct bch_dev *ca,
1299 				       struct btree_iter *iter, struct bkey_s_c k)
1300 {
1301 	struct bch_alloc_v4 a_convert;
1302 	const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &a_convert);
1303 	struct bkey_i_alloc_v4 *a_mut;
1304 	int ret;
1305 
1306 	if (a->oldest_gen == ca->oldest_gen[iter->pos.offset])
1307 		return 0;
1308 
1309 	a_mut = bch2_alloc_to_v4_mut(trans, k);
1310 	ret = PTR_ERR_OR_ZERO(a_mut);
1311 	if (ret)
1312 		return ret;
1313 
1314 	a_mut->v.oldest_gen = ca->oldest_gen[iter->pos.offset];
1315 	alloc_data_type_set(&a_mut->v, a_mut->v.data_type);
1316 
1317 	return bch2_trans_update(trans, iter, &a_mut->k_i, 0);
1318 }
1319 
1320 int bch2_gc_gens(struct bch_fs *c)
1321 {
1322 	u64 b, start_time = local_clock();
1323 	int ret;
1324 
1325 	/*
1326 	 * Ideally we would be using state_lock and not gc_lock here, but that
1327 	 * introduces a deadlock in the RO path - we currently take the state
1328 	 * lock at the start of going RO, thus the gc thread may get stuck:
1329 	 */
1330 	if (!mutex_trylock(&c->gc_gens_lock))
1331 		return 0;
1332 
1333 	trace_and_count(c, gc_gens_start, c);
1334 	down_read(&c->gc_lock);
1335 
1336 	for_each_member_device(c, ca) {
1337 		struct bucket_gens *gens = bucket_gens(ca);
1338 
1339 		BUG_ON(ca->oldest_gen);
1340 
1341 		ca->oldest_gen = kvmalloc(gens->nbuckets, GFP_KERNEL);
1342 		if (!ca->oldest_gen) {
1343 			bch2_dev_put(ca);
1344 			ret = -BCH_ERR_ENOMEM_gc_gens;
1345 			goto err;
1346 		}
1347 
1348 		for (b = gens->first_bucket;
1349 		     b < gens->nbuckets; b++)
1350 			ca->oldest_gen[b] = gens->b[b];
1351 	}
1352 
1353 	for (unsigned i = 0; i < BTREE_ID_NR; i++)
1354 		if (btree_type_has_ptrs(i)) {
1355 			c->gc_gens_btree = i;
1356 			c->gc_gens_pos = POS_MIN;
1357 
1358 			ret = bch2_trans_run(c,
1359 				for_each_btree_key_commit(trans, iter, i,
1360 						POS_MIN,
1361 						BTREE_ITER_prefetch|BTREE_ITER_all_snapshots,
1362 						k,
1363 						NULL, NULL,
1364 						BCH_TRANS_COMMIT_no_enospc,
1365 					gc_btree_gens_key(trans, &iter, k)));
1366 			if (ret)
1367 				goto err;
1368 		}
1369 
1370 	struct bch_dev *ca = NULL;
1371 	ret = bch2_trans_run(c,
1372 		for_each_btree_key_commit(trans, iter, BTREE_ID_alloc,
1373 				POS_MIN,
1374 				BTREE_ITER_prefetch,
1375 				k,
1376 				NULL, NULL,
1377 				BCH_TRANS_COMMIT_no_enospc, ({
1378 			ca = bch2_dev_iterate(c, ca, k.k->p.inode);
1379 			if (!ca) {
1380 				bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
1381 				continue;
1382 			}
1383 			bch2_alloc_write_oldest_gen(trans, ca, &iter, k);
1384 		})));
1385 	bch2_dev_put(ca);
1386 
1387 	if (ret)
1388 		goto err;
1389 
1390 	c->gc_gens_btree	= 0;
1391 	c->gc_gens_pos		= POS_MIN;
1392 
1393 	c->gc_count++;
1394 
1395 	bch2_time_stats_update(&c->times[BCH_TIME_btree_gc], start_time);
1396 	trace_and_count(c, gc_gens_end, c);
1397 err:
1398 	for_each_member_device(c, ca) {
1399 		kvfree(ca->oldest_gen);
1400 		ca->oldest_gen = NULL;
1401 	}
1402 
1403 	up_read(&c->gc_lock);
1404 	mutex_unlock(&c->gc_gens_lock);
1405 	if (!bch2_err_matches(ret, EROFS))
1406 		bch_err_fn(c, ret);
1407 	return ret;
1408 }
1409 
1410 static void bch2_gc_gens_work(struct work_struct *work)
1411 {
1412 	struct bch_fs *c = container_of(work, struct bch_fs, gc_gens_work);
1413 	bch2_gc_gens(c);
1414 	bch2_write_ref_put(c, BCH_WRITE_REF_gc_gens);
1415 }
1416 
1417 void bch2_gc_gens_async(struct bch_fs *c)
1418 {
1419 	if (bch2_write_ref_tryget(c, BCH_WRITE_REF_gc_gens) &&
1420 	    !queue_work(c->write_ref_wq, &c->gc_gens_work))
1421 		bch2_write_ref_put(c, BCH_WRITE_REF_gc_gens);
1422 }
1423 
1424 void bch2_fs_gc_init(struct bch_fs *c)
1425 {
1426 	seqcount_init(&c->gc_pos_lock);
1427 
1428 	INIT_WORK(&c->gc_gens_work, bch2_gc_gens_work);
1429 }
1430