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