summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_gc.c
blob: a0b65267cf760fa6db09263f4ea964ec18d7cfc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
 * Copyright (C) 2014 Datera Inc.
 */

#include "bcachefs.h"
#include "alloc_background.h"
#include "alloc_foreground.h"
#include "bkey_methods.h"
#include "btree_locking.h"
#include "btree_update_interior.h"
#include "btree_io.h"
#include "btree_gc.h"
#include "buckets.h"
#include "clock.h"
#include "debug.h"
#include "ec.h"
#include "error.h"
#include "extents.h"
#include "journal.h"
#include "keylist.h"
#include "move.h"
#include "recovery.h"
#include "replicas.h"
#include "super-io.h"
#include "trace.h"

#include <linux/slab.h>
#include <linux/bitops.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/preempt.h>
#include <linux/rcupdate.h>
#include <linux/sched/task.h>

static inline void __gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
{
	preempt_disable();
	write_seqcount_begin(&c->gc_pos_lock);
	c->gc_pos = new_pos;
	write_seqcount_end(&c->gc_pos_lock);
	preempt_enable();
}

static inline void gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
{
	BUG_ON(gc_pos_cmp(new_pos, c->gc_pos) <= 0);
	__gc_pos_set(c, new_pos);
}

/* range_checks - for validating min/max pos of each btree node: */

struct range_checks {
	struct range_level {
		struct bpos	min;
		struct bpos	max;
	}			l[BTREE_MAX_DEPTH];
	unsigned		depth;
};

static void btree_node_range_checks_init(struct range_checks *r, unsigned depth)
{
	unsigned i;

	for (i = 0; i < BTREE_MAX_DEPTH; i++)
		r->l[i].min = r->l[i].max = POS_MIN;
	r->depth = depth;
}

static void btree_node_range_checks(struct bch_fs *c, struct btree *b,
				    struct range_checks *r)
{
	struct range_level *l = &r->l[b->c.level];

	struct bpos expected_min = bkey_cmp(l->min, l->max)
		? btree_type_successor(b->c.btree_id, l->max)
		: l->max;

	bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, expected_min), c,
		"btree node has incorrect min key: %llu:%llu != %llu:%llu",
		b->data->min_key.inode,
		b->data->min_key.offset,
		expected_min.inode,
		expected_min.offset);

	l->max = b->data->max_key;

	if (b->c.level > r->depth) {
		l = &r->l[b->c.level - 1];

		bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, l->min), c,
			"btree node min doesn't match min of child nodes: %llu:%llu != %llu:%llu",
			b->data->min_key.inode,
			b->data->min_key.offset,
			l->min.inode,
			l->min.offset);

		bch2_fs_inconsistent_on(bkey_cmp(b->data->max_key, l->max), c,
			"btree node max doesn't match max of child nodes: %llu:%llu != %llu:%llu",
			b->data->max_key.inode,
			b->data->max_key.offset,
			l->max.inode,
			l->max.offset);

		if (bkey_cmp(b->data->max_key, POS_MAX))
			l->min = l->max =
				btree_type_successor(b->c.btree_id,
						     b->data->max_key);
	}
}

/* marking of btree keys/nodes: */

static int bch2_gc_mark_key(struct bch_fs *c, struct bkey_s_c k,
			    u8 *max_stale, bool initial)
{
	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
	const struct bch_extent_ptr *ptr;
	unsigned flags =
		BTREE_TRIGGER_GC|
		(initial ? BTREE_TRIGGER_NOATOMIC : 0);
	int ret = 0;

	if (initial) {
		BUG_ON(journal_seq_verify(c) &&
		       k.k->version.lo > journal_cur_seq(&c->journal));

		if (k.k->version.lo > atomic64_read(&c->key_version))
			atomic64_set(&c->key_version, k.k->version.lo);

		if (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
		    fsck_err_on(!bch2_bkey_replicas_marked(c, k, false), c,
				"superblock not marked as containing replicas (type %u)",
				k.k->type)) {
			ret = bch2_mark_bkey_replicas(c, k);
			if (ret)
				return ret;
		}

		bkey_for_each_ptr(ptrs, ptr) {
			struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
			struct bucket *g = PTR_BUCKET(ca, ptr, true);
			struct bucket *g2 = PTR_BUCKET(ca, ptr, false);

			if (mustfix_fsck_err_on(!g->gen_valid, c,
					"bucket %u:%zu data type %s ptr gen %u missing in alloc btree",
					ptr->dev, PTR_BUCKET_NR(ca, ptr),
					bch2_data_types[ptr_data_type(k.k, ptr)],
					ptr->gen)) {
				g2->_mark.gen	= g->_mark.gen		= ptr->gen;
				g2->gen_valid	= g->gen_valid		= true;
			}

			if (mustfix_fsck_err_on(gen_cmp(ptr->gen, g->mark.gen) > 0, c,
					"bucket %u:%zu data type %s ptr gen in the future: %u > %u",
					ptr->dev, PTR_BUCKET_NR(ca, ptr),
					bch2_data_types[ptr_data_type(k.k, ptr)],
					ptr->gen, g->mark.gen)) {
				g2->_mark.gen	= g->_mark.gen		= ptr->gen;
				g2->gen_valid	= g->gen_valid		= true;
				g2->_mark.data_type		= 0;
				g2->_mark.dirty_sectors		= 0;
				g2->_mark.cached_sectors	= 0;
				set_bit(BCH_FS_FIXED_GENS, &c->flags);
			}
		}
	}

	bkey_for_each_ptr(ptrs, ptr) {
		struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
		struct bucket *g = PTR_BUCKET(ca, ptr, true);

		if (gen_after(g->oldest_gen, ptr->gen))
			g->oldest_gen = ptr->gen;

		*max_stale = max(*max_stale, ptr_stale(ca, ptr));
	}

	bch2_mark_key(c, k, 0, k.k->size, NULL, 0, flags);
fsck_err:
	return ret;
}

static int btree_gc_mark_node(struct bch_fs *c, struct btree *b,
			      u8 *max_stale, bool initial)
{
	struct btree_node_iter iter;
	struct bkey unpacked;
	struct bkey_s_c k;
	int ret = 0;

	*max_stale = 0;

	if (!btree_node_type_needs_gc(btree_node_type(b)))
		return 0;

	for_each_btree_node_key_unpack(b, k, &iter,
				       &unpacked) {
		bch2_bkey_debugcheck(c, b, k);

		ret = bch2_gc_mark_key(c, k, max_stale, initial);
		if (ret)
			break;
	}

	return ret;
}

static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id,
			 bool initial, bool metadata_only)
{
	struct btree_trans trans;
	struct btree_iter *iter;
	struct btree *b;
	struct range_checks r;
	unsigned depth = metadata_only			? 1
		: expensive_debug_checks(c)		? 0
		: !btree_node_type_needs_gc(btree_id)	? 1
		: 0;
	u8 max_stale = 0;
	int ret = 0;

	bch2_trans_init(&trans, c, 0, 0);

	gc_pos_set(c, gc_pos_btree(btree_id, POS_MIN, 0));

	btree_node_range_checks_init(&r, depth);

	__for_each_btree_node(&trans, iter, btree_id, POS_MIN,
			      0, depth, BTREE_ITER_PREFETCH, b) {
		btree_node_range_checks(c, b, &r);

		bch2_verify_btree_nr_keys(b);

		gc_pos_set(c, gc_pos_btree_node(b));

		ret = btree_gc_mark_node(c, b, &max_stale, initial);
		if (ret)
			break;

		if (!initial) {
			if (max_stale > 64)
				bch2_btree_node_rewrite(c, iter,
						b->data->keys.seq,
						BTREE_INSERT_USE_RESERVE|
						BTREE_INSERT_NOWAIT|
						BTREE_INSERT_GC_LOCK_HELD);
			else if (!btree_gc_rewrite_disabled(c) &&
				 (btree_gc_always_rewrite(c) || max_stale > 16))
				bch2_btree_node_rewrite(c, iter,
						b->data->keys.seq,
						BTREE_INSERT_NOWAIT|
						BTREE_INSERT_GC_LOCK_HELD);
		}

		bch2_trans_cond_resched(&trans);
	}
	ret = bch2_trans_exit(&trans) ?: ret;
	if (ret)
		return ret;

	mutex_lock(&c->btree_root_lock);
	b = c->btree_roots[btree_id].b;
	if (!btree_node_fake(b))
		ret = bch2_gc_mark_key(c, bkey_i_to_s_c(&b->key),
				       &max_stale, initial);
	gc_pos_set(c, gc_pos_btree_root(b->c.btree_id));
	mutex_unlock(&c->btree_root_lock);

	return ret;
}

static inline int btree_id_gc_phase_cmp(enum btree_id l, enum btree_id r)
{
	return  (int) btree_id_to_gc_phase(l) -
		(int) btree_id_to_gc_phase(r);
}

static int mark_journal_key(struct bch_fs *c, enum btree_id id,
			    struct bkey_i *insert)
{
	struct btree_trans trans;
	struct btree_iter *iter;
	struct bkey_s_c k;
	u8 max_stale;
	int ret = 0;

	ret = bch2_gc_mark_key(c, bkey_i_to_s_c(insert), &max_stale, true);
	if (ret)
		return ret;

	bch2_trans_init(&trans, c, 0, 0);

	for_each_btree_key(&trans, iter, id, bkey_start_pos(&insert->k),
			   BTREE_ITER_SLOTS, k, ret) {
		percpu_down_read(&c->mark_lock);
		ret = bch2_mark_overwrite(&trans, iter, k, insert, NULL,
					 BTREE_TRIGGER_GC|
					 BTREE_TRIGGER_NOATOMIC);
		percpu_up_read(&c->mark_lock);

		if (!ret)
			break;
	}

	return bch2_trans_exit(&trans) ?: ret;
}

static int bch2_gc_btrees(struct bch_fs *c, struct journal_keys *journal_keys,
			  bool initial, bool metadata_only)
{
	enum btree_id ids[BTREE_ID_NR];
	unsigned i;

	for (i = 0; i < BTREE_ID_NR; i++)
		ids[i] = i;
	bubble_sort(ids, BTREE_ID_NR, btree_id_gc_phase_cmp);

	for (i = 0; i < BTREE_ID_NR; i++) {
		enum btree_id id = ids[i];
		enum btree_node_type type = __btree_node_type(0, id);

		int ret = bch2_gc_btree(c, id, initial, metadata_only);
		if (ret)
			return ret;

		if (journal_keys && !metadata_only &&
		    btree_node_type_needs_gc(type)) {
			struct journal_key *j;
			int ret;

			for_each_journal_key(*journal_keys, j)
				if (j->btree_id == id) {
					ret = mark_journal_key(c, id, j->k);
					if (ret)
						return ret;
				}
		}
	}

	return 0;
}

static void mark_metadata_sectors(struct bch_fs *c, struct bch_dev *ca,
				  u64 start, u64 end,
				  enum bch_data_type type,
				  unsigned flags)
{
	u64 b = sector_to_bucket(ca, start);

	do {
		unsigned sectors =
			min_t(u64, bucket_to_sector(ca, b + 1), end) - start;

		bch2_mark_metadata_bucket(c, ca, b, type, sectors,
					  gc_phase(GC_PHASE_SB), flags);
		b++;
		start += sectors;
	} while (start < end);
}

void bch2_mark_dev_superblock(struct bch_fs *c, struct bch_dev *ca,
			      unsigned flags)
{
	struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
	unsigned i;
	u64 b;

	/*
	 * This conditional is kind of gross, but we may be called from the
	 * device add path, before the new device has actually been added to the
	 * running filesystem:
	 */
	if (c) {
		lockdep_assert_held(&c->sb_lock);
		percpu_down_read(&c->mark_lock);
	}

	for (i = 0; i < layout->nr_superblocks; i++) {
		u64 offset = le64_to_cpu(layout->sb_offset[i]);

		if (offset == BCH_SB_SECTOR)
			mark_metadata_sectors(c, ca, 0, BCH_SB_SECTOR,
					      BCH_DATA_SB, flags);

		mark_metadata_sectors(c, ca, offset,
				      offset + (1 << layout->sb_max_size_bits),
				      BCH_DATA_SB, flags);
	}

	for (i = 0; i < ca->journal.nr; i++) {
		b = ca->journal.buckets[i];
		bch2_mark_metadata_bucket(c, ca, b, BCH_DATA_JOURNAL,
					  ca->mi.bucket_size,
					  gc_phase(GC_PHASE_SB), flags);
	}

	if (c)
		percpu_up_read(&c->mark_lock);
}

static void bch2_mark_superblocks(struct bch_fs *c)
{
	struct bch_dev *ca;
	unsigned i;

	mutex_lock(&c->sb_lock);
	gc_pos_set(c, gc_phase(GC_PHASE_SB));

	for_each_online_member(ca, c, i)
		bch2_mark_dev_superblock(c, ca, BTREE_TRIGGER_GC);
	mutex_unlock(&c->sb_lock);
}

/* Also see bch2_pending_btree_node_free_insert_done() */
static void bch2_mark_pending_btree_node_frees(struct bch_fs *c)
{
	struct btree_update *as;
	struct pending_btree_node_free *d;

	mutex_lock(&c->btree_interior_update_lock);
	gc_pos_set(c, gc_phase(GC_PHASE_PENDING_DELETE));

	for_each_pending_btree_node_free(c, as, d)
		if (d->index_update_done)
			bch2_mark_key(c, bkey_i_to_s_c(&d->key),
				      0, 0, NULL, 0,
				      BTREE_TRIGGER_GC);

	mutex_unlock(&c->btree_interior_update_lock);
}

static void bch2_mark_allocator_buckets(struct bch_fs *c)
{
	struct bch_dev *ca;
	struct open_bucket *ob;
	size_t i, j, iter;
	unsigned ci;

	percpu_down_read(&c->mark_lock);

	spin_lock(&c->freelist_lock);
	gc_pos_set(c, gc_pos_alloc(c, NULL));

	for_each_member_device(ca, c, ci) {
		fifo_for_each_entry(i, &ca->free_inc, iter)
			bch2_mark_alloc_bucket(c, ca, i, true,
					       gc_pos_alloc(c, NULL),
					       BTREE_TRIGGER_GC);



		for (j = 0; j < RESERVE_NR; j++)
			fifo_for_each_entry(i, &ca->free[j], iter)
				bch2_mark_alloc_bucket(c, ca, i, true,
						       gc_pos_alloc(c, NULL),
						       BTREE_TRIGGER_GC);
	}

	spin_unlock(&c->freelist_lock);

	for (ob = c->open_buckets;
	     ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
	     ob++) {
		spin_lock(&ob->lock);
		if (ob->valid) {
			gc_pos_set(c, gc_pos_alloc(c, ob));
			ca = bch_dev_bkey_exists(c, ob->ptr.dev);
			bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr), true,
					       gc_pos_alloc(c, ob),
					       BTREE_TRIGGER_GC);
		}
		spin_unlock(&ob->lock);
	}

	percpu_up_read(&c->mark_lock);
}

static void bch2_gc_free(struct bch_fs *c)
{
	struct bch_dev *ca;
	unsigned i;

	genradix_free(&c->stripes[1]);

	for_each_member_device(ca, c, i) {
		kvpfree(rcu_dereference_protected(ca->buckets[1], 1),
			sizeof(struct bucket_array) +
			ca->mi.nbuckets * sizeof(struct bucket));
		ca->buckets[1] = NULL;

		free_percpu(ca->usage[1]);
		ca->usage[1] = NULL;
	}

	free_percpu(c->usage_gc);
	c->usage_gc = NULL;
}

static int bch2_gc_done(struct bch_fs *c,
			bool initial, bool metadata_only)
{
	struct bch_dev *ca;
	bool verify = !metadata_only &&
		(!initial ||
		 (c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)));
	unsigned i;
	int ret = 0;

#define copy_field(_f, _msg, ...)					\
	if (dst->_f != src->_f) {					\
		if (verify)						\
			fsck_err(c, _msg ": got %llu, should be %llu"	\
				, ##__VA_ARGS__, dst->_f, src->_f);	\
		dst->_f = src->_f;					\
	}
#define copy_stripe_field(_f, _msg, ...)				\
	if (dst->_f != src->_f) {					\
		if (verify)						\
			fsck_err(c, "stripe %zu has wrong "_msg		\
				": got %u, should be %u",		\
				dst_iter.pos, ##__VA_ARGS__,		\
				dst->_f, src->_f);			\
		dst->_f = src->_f;					\
		dst->dirty = true;					\
	}
#define copy_bucket_field(_f)						\
	if (dst->b[b].mark._f != src->b[b].mark._f) {			\
		if (verify)						\
			fsck_err(c, "dev %u bucket %zu has wrong " #_f	\
				": got %u, should be %u", i, b,		\
				dst->b[b].mark._f, src->b[b].mark._f);	\
		dst->b[b]._mark._f = src->b[b].mark._f;			\
	}
#define copy_dev_field(_f, _msg, ...)					\
	copy_field(_f, "dev %u has wrong " _msg, i, ##__VA_ARGS__)
#define copy_fs_field(_f, _msg, ...)					\
	copy_field(_f, "fs has wrong " _msg, ##__VA_ARGS__)

	if (!metadata_only) {
		struct genradix_iter dst_iter = genradix_iter_init(&c->stripes[0], 0);
		struct genradix_iter src_iter = genradix_iter_init(&c->stripes[1], 0);
		struct stripe *dst, *src;
		unsigned i;

		c->ec_stripes_heap.used = 0;

		while ((dst = genradix_iter_peek(&dst_iter, &c->stripes[0])) &&
		       (src = genradix_iter_peek(&src_iter, &c->stripes[1]))) {
			BUG_ON(src_iter.pos != dst_iter.pos);

			copy_stripe_field(alive,	"alive");
			copy_stripe_field(sectors,	"sectors");
			copy_stripe_field(algorithm,	"algorithm");
			copy_stripe_field(nr_blocks,	"nr_blocks");
			copy_stripe_field(nr_redundant,	"nr_redundant");
			copy_stripe_field(blocks_nonempty,
					  "blocks_nonempty");

			for (i = 0; i < ARRAY_SIZE(dst->block_sectors); i++)
				copy_stripe_field(block_sectors[i],
						  "block_sectors[%u]", i);

			if (dst->alive)
				bch2_stripes_heap_insert(c, dst, dst_iter.pos);

			genradix_iter_advance(&dst_iter, &c->stripes[0]);
			genradix_iter_advance(&src_iter, &c->stripes[1]);
		}
	}

	for_each_member_device(ca, c, i) {
		struct bucket_array *dst = __bucket_array(ca, 0);
		struct bucket_array *src = __bucket_array(ca, 1);
		size_t b;

		for (b = 0; b < src->nbuckets; b++) {
			copy_bucket_field(gen);
			copy_bucket_field(data_type);
			copy_bucket_field(owned_by_allocator);
			copy_bucket_field(stripe);
			copy_bucket_field(dirty_sectors);
			copy_bucket_field(cached_sectors);

			dst->b[b].oldest_gen = src->b[b].oldest_gen;
		}
	};

	for (i = 0; i < ARRAY_SIZE(c->usage); i++)
		bch2_fs_usage_acc_to_base(c, i);

	bch2_dev_usage_from_buckets(c);

	{
		unsigned nr = fs_usage_u64s(c);
		struct bch_fs_usage *dst = c->usage_base;
		struct bch_fs_usage *src = (void *)
			bch2_acc_percpu_u64s((void *) c->usage_gc, nr);

		copy_fs_field(hidden,		"hidden");
		copy_fs_field(btree,		"btree");

		if (!metadata_only) {
			copy_fs_field(data,	"data");
			copy_fs_field(cached,	"cached");
			copy_fs_field(reserved,	"reserved");
			copy_fs_field(nr_inodes,"nr_inodes");

			for (i = 0; i < BCH_REPLICAS_MAX; i++)
				copy_fs_field(persistent_reserved[i],
					      "persistent_reserved[%i]", i);
		}

		for (i = 0; i < c->replicas.nr; i++) {
			struct bch_replicas_entry *e =
				cpu_replicas_entry(&c->replicas, i);
			char buf[80];

			if (metadata_only &&
			    (e->data_type == BCH_DATA_USER ||
			     e->data_type == BCH_DATA_CACHED))
				continue;

			bch2_replicas_entry_to_text(&PBUF(buf), e);

			copy_fs_field(replicas[i], "%s", buf);
		}
	}

#undef copy_fs_field
#undef copy_dev_field
#undef copy_bucket_field
#undef copy_stripe_field
#undef copy_field
fsck_err:
	return ret;
}

static int bch2_gc_start(struct bch_fs *c,
			 bool metadata_only)
{
	struct bch_dev *ca;
	unsigned i;
	int ret;

	BUG_ON(c->usage_gc);

	c->usage_gc = __alloc_percpu_gfp(fs_usage_u64s(c) * sizeof(u64),
					 sizeof(u64), GFP_KERNEL);
	if (!c->usage_gc)
		return -ENOMEM;

	for_each_member_device(ca, c, i) {
		BUG_ON(ca->buckets[1]);
		BUG_ON(ca->usage[1]);

		ca->buckets[1] = kvpmalloc(sizeof(struct bucket_array) +
				ca->mi.nbuckets * sizeof(struct bucket),
				GFP_KERNEL|__GFP_ZERO);
		if (!ca->buckets[1]) {
			percpu_ref_put(&ca->ref);
			return -ENOMEM;
		}

		ca->usage[1] = alloc_percpu(struct bch_dev_usage);
		if (!ca->usage[1]) {
			percpu_ref_put(&ca->ref);
			return -ENOMEM;
		}
	}

	ret = bch2_ec_mem_alloc(c, true);
	if (ret)
		return ret;

	percpu_down_write(&c->mark_lock);

	/*
	 * indicate to stripe code that we need to allocate for the gc stripes
	 * radix tree, too
	 */
	gc_pos_set(c, gc_phase(GC_PHASE_START));

	for_each_member_device(ca, c, i) {
		struct bucket_array *dst = __bucket_array(ca, 1);
		struct bucket_array *src = __bucket_array(ca, 0);
		size_t b;

		dst->first_bucket	= src->first_bucket;
		dst->nbuckets		= src->nbuckets;

		for (b = 0; b < src->nbuckets; b++) {
			struct bucket *d = &dst->b[b];
			struct bucket *s = &src->b[b];

			d->_mark.gen = dst->b[b].oldest_gen = s->mark.gen;
			d->gen_valid = s->gen_valid;

			if (metadata_only &&
			    (s->mark.data_type == BCH_DATA_USER ||
			     s->mark.data_type == BCH_DATA_CACHED)) {
				d->_mark = s->mark;
				d->_mark.owned_by_allocator = 0;
			}
		}
	};

	percpu_up_write(&c->mark_lock);

	return 0;
}

/**
 * bch2_gc - walk _all_ references to buckets, and recompute them:
 *
 * Order matters here:
 *  - Concurrent GC relies on the fact that we have a total ordering for
 *    everything that GC walks - see  gc_will_visit_node(),
 *    gc_will_visit_root()
 *
 *  - also, references move around in the course of index updates and
 *    various other crap: everything needs to agree on the ordering
 *    references are allowed to move around in - e.g., we're allowed to
 *    start with a reference owned by an open_bucket (the allocator) and
 *    move it to the btree, but not the reverse.
 *
 *    This is necessary to ensure that gc doesn't miss references that
 *    move around - if references move backwards in the ordering GC
 *    uses, GC could skip past them
 */
int bch2_gc(struct bch_fs *c, struct journal_keys *journal_keys,
	    bool initial, bool metadata_only)
{
	struct bch_dev *ca;
	u64 start_time = local_clock();
	unsigned i, iter = 0;
	int ret;

	trace_gc_start(c);

	down_write(&c->gc_lock);
again:
	ret = bch2_gc_start(c, metadata_only);
	if (ret)
		goto out;

	bch2_mark_superblocks(c);

	ret = bch2_gc_btrees(c, journal_keys, initial, metadata_only);
	if (ret)
		goto out;

	bch2_mark_pending_btree_node_frees(c);
	bch2_mark_allocator_buckets(c);

	c->gc_count++;
out:
	if (!ret &&
	    (test_bit(BCH_FS_FIXED_GENS, &c->flags) ||
	     (!iter && test_restart_gc(c)))) {
		/*
		 * XXX: make sure gens we fixed got saved
		 */
		if (iter++ <= 2) {
			bch_info(c, "Fixed gens, restarting mark and sweep:");
			clear_bit(BCH_FS_FIXED_GENS, &c->flags);
			__gc_pos_set(c, gc_phase(GC_PHASE_NOT_RUNNING));

			percpu_down_write(&c->mark_lock);
			bch2_gc_free(c);
			percpu_up_write(&c->mark_lock);
			/* flush fsck errors, reset counters */
			bch2_flush_fsck_errs(c);

			goto again;
		}

		bch_info(c, "Unable to fix bucket gens, looping");
		ret = -EINVAL;
	}

	if (!ret) {
		bch2_journal_block(&c->journal);

		percpu_down_write(&c->mark_lock);
		ret = bch2_gc_done(c, initial, metadata_only);

		bch2_journal_unblock(&c->journal);
	} else {
		percpu_down_write(&c->mark_lock);
	}

	/* Indicates that gc is no longer in progress: */
	__gc_pos_set(c, gc_phase(GC_PHASE_NOT_RUNNING));

	bch2_gc_free(c);
	percpu_up_write(&c->mark_lock);

	up_write(&c->gc_lock);

	trace_gc_end(c);
	bch2_time_stats_update(&c->times[BCH_TIME_btree_gc], start_time);

	/*
	 * Wake up allocator in case it was waiting for buckets
	 * because of not being able to inc gens
	 */
	for_each_member_device(ca, c, i)
		bch2_wake_allocator(ca);

	/*
	 * At startup, allocations can happen directly instead of via the
	 * allocator thread - issue wakeup in case they blocked on gc_lock:
	 */
	closure_wake_up(&c->freelist_wait);
	return ret;
}

/* Btree coalescing */

static void recalc_packed_keys(struct btree *b)
{
	struct bset *i = btree_bset_first(b);
	struct bkey_packed *k;

	memset(&b->nr, 0, sizeof(b->nr));

	BUG_ON(b->nsets != 1);

	vstruct_for_each(i, k)
		btree_keys_account_key_add(&b->nr, 0, k);
}

static void bch2_coalesce_nodes(struct bch_fs *c, struct btree_iter *iter,
				struct btree *old_nodes[GC_MERGE_NODES])
{
	struct btree *parent = btree_node_parent(iter, old_nodes[0]);
	unsigned i, nr_old_nodes, nr_new_nodes, u64s = 0;
	unsigned blocks = btree_blocks(c) * 2 / 3;
	struct btree *new_nodes[GC_MERGE_NODES];
	struct btree_update *as;
	struct keylist keylist;
	struct bkey_format_state format_state;
	struct bkey_format new_format;

	memset(new_nodes, 0, sizeof(new_nodes));
	bch2_keylist_init(&keylist, NULL);

	/* Count keys that are not deleted */
	for (i = 0; i < GC_MERGE_NODES && old_nodes[i]; i++)
		u64s += old_nodes[i]->nr.live_u64s;

	nr_old_nodes = nr_new_nodes = i;

	/* Check if all keys in @old_nodes could fit in one fewer node */
	if (nr_old_nodes <= 1 ||
	    __vstruct_blocks(struct btree_node, c->block_bits,
			     DIV_ROUND_UP(u64s, nr_old_nodes - 1)) > blocks)
		return;

	/* Find a format that all keys in @old_nodes can pack into */
	bch2_bkey_format_init(&format_state);

	for (i = 0; i < nr_old_nodes; i++)
		__bch2_btree_calc_format(&format_state, old_nodes[i]);

	new_format = bch2_bkey_format_done(&format_state);

	/* Check if repacking would make any nodes too big to fit */
	for (i = 0; i < nr_old_nodes; i++)
		if (!bch2_btree_node_format_fits(c, old_nodes[i], &new_format)) {
			trace_btree_gc_coalesce_fail(c,
					BTREE_GC_COALESCE_FAIL_FORMAT_FITS);
			return;
		}

	if (bch2_keylist_realloc(&keylist, NULL, 0,
			(BKEY_U64s + BKEY_EXTENT_U64s_MAX) * nr_old_nodes)) {
		trace_btree_gc_coalesce_fail(c,
				BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC);
		return;
	}

	as = bch2_btree_update_start(c, iter->btree_id,
			btree_update_reserve_required(c, parent) + nr_old_nodes,
			BTREE_INSERT_NOFAIL|
			BTREE_INSERT_USE_RESERVE,
			NULL);
	if (IS_ERR(as)) {
		trace_btree_gc_coalesce_fail(c,
				BTREE_GC_COALESCE_FAIL_RESERVE_GET);
		bch2_keylist_free(&keylist, NULL);
		return;
	}

	trace_btree_gc_coalesce(c, old_nodes[0]);

	for (i = 0; i < nr_old_nodes; i++)
		bch2_btree_interior_update_will_free_node(as, old_nodes[i]);

	/* Repack everything with @new_format and sort down to one bset */
	for (i = 0; i < nr_old_nodes; i++)
		new_nodes[i] =
			__bch2_btree_node_alloc_replacement(as, old_nodes[i],
							    new_format);

	/*
	 * Conceptually we concatenate the nodes together and slice them
	 * up at different boundaries.
	 */
	for (i = nr_new_nodes - 1; i > 0; --i) {
		struct btree *n1 = new_nodes[i];
		struct btree *n2 = new_nodes[i - 1];

		struct bset *s1 = btree_bset_first(n1);
		struct bset *s2 = btree_bset_first(n2);
		struct bkey_packed *k, *last = NULL;

		/* Calculate how many keys from @n2 we could fit inside @n1 */
		u64s = 0;

		for (k = s2->start;
		     k < vstruct_last(s2) &&
		     vstruct_blocks_plus(n1->data, c->block_bits,
					 u64s + k->u64s) <= blocks;
		     k = bkey_next_skip_noops(k, vstruct_last(s2))) {
			last = k;
			u64s += k->u64s;
		}

		if (u64s == le16_to_cpu(s2->u64s)) {
			/* n2 fits entirely in n1 */
			n1->key.k.p = n1->data->max_key = n2->data->max_key;

			memcpy_u64s(vstruct_last(s1),
				    s2->start,
				    le16_to_cpu(s2->u64s));
			le16_add_cpu(&s1->u64s, le16_to_cpu(s2->u64s));

			set_btree_bset_end(n1, n1->set);

			six_unlock_write(&n2->c.lock);
			bch2_btree_node_free_never_inserted(c, n2);
			six_unlock_intent(&n2->c.lock);

			memmove(new_nodes + i - 1,
				new_nodes + i,
				sizeof(new_nodes[0]) * (nr_new_nodes - i));
			new_nodes[--nr_new_nodes] = NULL;
		} else if (u64s) {
			/* move part of n2 into n1 */
			n1->key.k.p = n1->data->max_key =
				bkey_unpack_pos(n1, last);

			n2->data->min_key =
				btree_type_successor(iter->btree_id,
						     n1->data->max_key);

			memcpy_u64s(vstruct_last(s1),
				    s2->start, u64s);
			le16_add_cpu(&s1->u64s, u64s);

			memmove(s2->start,
				vstruct_idx(s2, u64s),
				(le16_to_cpu(s2->u64s) - u64s) * sizeof(u64));
			s2->u64s = cpu_to_le16(le16_to_cpu(s2->u64s) - u64s);

			set_btree_bset_end(n1, n1->set);
			set_btree_bset_end(n2, n2->set);
		}
	}

	for (i = 0; i < nr_new_nodes; i++) {
		struct btree *n = new_nodes[i];

		recalc_packed_keys(n);
		btree_node_reset_sib_u64s(n);

		bch2_btree_build_aux_trees(n);
		six_unlock_write(&n->c.lock);

		bch2_btree_node_write(c, n, SIX_LOCK_intent);
	}

	/*
	 * The keys for the old nodes get deleted. We don't want to insert keys
	 * that compare equal to the keys for the new nodes we'll also be
	 * inserting - we can't because keys on a keylist must be strictly
	 * greater than the previous keys, and we also don't need to since the
	 * key for the new node will serve the same purpose (overwriting the key
	 * for the old node).
	 */
	for (i = 0; i < nr_old_nodes; i++) {
		struct bkey_i delete;
		unsigned j;

		for (j = 0; j < nr_new_nodes; j++)
			if (!bkey_cmp(old_nodes[i]->key.k.p,
				      new_nodes[j]->key.k.p))
				goto next;

		bkey_init(&delete.k);
		delete.k.p = old_nodes[i]->key.k.p;
		bch2_keylist_add_in_order(&keylist, &delete);
next:
		i = i;
	}

	/*
	 * Keys for the new nodes get inserted: bch2_btree_insert_keys() only
	 * does the lookup once and thus expects the keys to be in sorted order
	 * so we have to make sure the new keys are correctly ordered with
	 * respect to the deleted keys added in the previous loop
	 */
	for (i = 0; i < nr_new_nodes; i++)
		bch2_keylist_add_in_order(&keylist, &new_nodes[i]->key);

	/* Insert the newly coalesced nodes */
	bch2_btree_insert_node(as, parent, iter, &keylist, 0);

	BUG_ON(!bch2_keylist_empty(&keylist));

	BUG_ON(iter->l[old_nodes[0]->c.level].b != old_nodes[0]);

	bch2_btree_iter_node_replace(iter, new_nodes[0]);

	for (i = 0; i < nr_new_nodes; i++)
		bch2_open_buckets_put(c, &new_nodes[i]->ob);

	/* Free the old nodes and update our sliding window */
	for (i = 0; i < nr_old_nodes; i++) {
		bch2_btree_node_free_inmem(c, old_nodes[i], iter);

		/*
		 * the index update might have triggered a split, in which case
		 * the nodes we coalesced - the new nodes we just created -
		 * might not be sibling nodes anymore - don't add them to the
		 * sliding window (except the first):
		 */
		if (!i) {
			old_nodes[i] = new_nodes[i];
		} else {
			old_nodes[i] = NULL;
		}
	}

	for (i = 0; i < nr_new_nodes; i++)
		six_unlock_intent(&new_nodes[i]->c.lock);

	bch2_btree_update_done(as);
	bch2_keylist_free(&keylist, NULL);
}

static int bch2_coalesce_btree(struct bch_fs *c, enum btree_id btree_id)
{
	struct btree_trans trans;
	struct btree_iter *iter;
	struct btree *b;
	bool kthread = (current->flags & PF_KTHREAD) != 0;
	unsigned i;

	/* Sliding window of adjacent btree nodes */
	struct btree *merge[GC_MERGE_NODES];
	u32 lock_seq[GC_MERGE_NODES];

	bch2_trans_init(&trans, c, 0, 0);

	/*
	 * XXX: We don't have a good way of positively matching on sibling nodes
	 * that have the same parent - this code works by handling the cases
	 * where they might not have the same parent, and is thus fragile. Ugh.
	 *
	 * Perhaps redo this to use multiple linked iterators?
	 */
	memset(merge, 0, sizeof(merge));

	__for_each_btree_node(&trans, iter, btree_id, POS_MIN,
			      BTREE_MAX_DEPTH, 0,
			      BTREE_ITER_PREFETCH, b) {
		memmove(merge + 1, merge,
			sizeof(merge) - sizeof(merge[0]));
		memmove(lock_seq + 1, lock_seq,
			sizeof(lock_seq) - sizeof(lock_seq[0]));

		merge[0] = b;

		for (i = 1; i < GC_MERGE_NODES; i++) {
			if (!merge[i] ||
			    !six_relock_intent(&merge[i]->c.lock, lock_seq[i]))
				break;

			if (merge[i]->c.level != merge[0]->c.level) {
				six_unlock_intent(&merge[i]->c.lock);
				break;
			}
		}
		memset(merge + i, 0, (GC_MERGE_NODES - i) * sizeof(merge[0]));

		bch2_coalesce_nodes(c, iter, merge);

		for (i = 1; i < GC_MERGE_NODES && merge[i]; i++) {
			lock_seq[i] = merge[i]->c.lock.state.seq;
			six_unlock_intent(&merge[i]->c.lock);
		}

		lock_seq[0] = merge[0]->c.lock.state.seq;

		if (kthread && kthread_should_stop()) {
			bch2_trans_exit(&trans);
			return -ESHUTDOWN;
		}

		bch2_trans_cond_resched(&trans);

		/*
		 * If the parent node wasn't relocked, it might have been split
		 * and the nodes in our sliding window might not have the same
		 * parent anymore - blow away the sliding window:
		 */
		if (btree_iter_node(iter, iter->level + 1) &&
		    !btree_node_intent_locked(iter, iter->level + 1))
			memset(merge + 1, 0,
			       (GC_MERGE_NODES - 1) * sizeof(merge[0]));
	}
	return bch2_trans_exit(&trans);
}

/**
 * bch_coalesce - coalesce adjacent nodes with low occupancy
 */
void bch2_coalesce(struct bch_fs *c)
{
	enum btree_id id;

	down_read(&c->gc_lock);
	trace_gc_coalesce_start(c);

	for (id = 0; id < BTREE_ID_NR; id++) {
		int ret = c->btree_roots[id].b
			? bch2_coalesce_btree(c, id)
			: 0;

		if (ret) {
			if (ret != -ESHUTDOWN)
				bch_err(c, "btree coalescing failed: %d", ret);
			return;
		}
	}

	trace_gc_coalesce_end(c);
	up_read(&c->gc_lock);
}

static int bch2_gc_thread(void *arg)
{
	struct bch_fs *c = arg;
	struct io_clock *clock = &c->io_clock[WRITE];
	unsigned long last = atomic_long_read(&clock->now);
	unsigned last_kick = atomic_read(&c->kick_gc);
	int ret;

	set_freezable();

	while (1) {
		while (1) {
			set_current_state(TASK_INTERRUPTIBLE);

			if (kthread_should_stop()) {
				__set_current_state(TASK_RUNNING);
				return 0;
			}

			if (atomic_read(&c->kick_gc) != last_kick)
				break;

			if (c->btree_gc_periodic) {
				unsigned long next = last + c->capacity / 16;

				if (atomic_long_read(&clock->now) >= next)
					break;

				bch2_io_clock_schedule_timeout(clock, next);
			} else {
				schedule();
			}

			try_to_freeze();
		}
		__set_current_state(TASK_RUNNING);

		last = atomic_long_read(&clock->now);
		last_kick = atomic_read(&c->kick_gc);

		ret = bch2_gc(c, NULL, false, false);
		if (ret)
			bch_err(c, "btree gc failed: %i", ret);

		debug_check_no_locks_held();
	}

	return 0;
}

void bch2_gc_thread_stop(struct bch_fs *c)
{
	struct task_struct *p;

	p = c->gc_thread;
	c->gc_thread = NULL;

	if (p) {
		kthread_stop(p);
		put_task_struct(p);
	}
}

int bch2_gc_thread_start(struct bch_fs *c)
{
	struct task_struct *p;

	BUG_ON(c->gc_thread);

	p = kthread_create(bch2_gc_thread, c, "bch_gc");
	if (IS_ERR(p))
		return PTR_ERR(p);

	get_task_struct(p);
	c->gc_thread = p;
	wake_up_process(p);
	return 0;
}