summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_gc.c
blob: 6cde4234f5e94f1c62df7c6ab7ac5d996c2ceffb (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
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
// 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 "bkey_buf.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 "reflink.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>

#define DROP_THIS_NODE		10
#define DROP_PREV_NODE		11

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);
}

/*
 * Missing: if an interior btree node is empty, we need to do something -
 * perhaps just kill it
 */
static int bch2_gc_check_topology(struct bch_fs *c,
				  struct btree *b,
				  struct bkey_buf *prev,
				  struct bkey_buf cur,
				  bool is_last)
{
	struct bpos node_start	= b->data->min_key;
	struct bpos node_end	= b->data->max_key;
	struct bpos expected_start = bkey_deleted(&prev->k->k)
		? node_start
		: bpos_successor(prev->k->k.p);
	char buf1[200], buf2[200];
	int ret = 0;

	if (cur.k->k.type == KEY_TYPE_btree_ptr_v2) {
		struct bkey_i_btree_ptr_v2 *bp = bkey_i_to_btree_ptr_v2(cur.k);

		if (bkey_deleted(&prev->k->k)) {
			struct printbuf out = PBUF(buf1);
			pr_buf(&out, "start of node: ");
			bch2_bpos_to_text(&out, node_start);
		} else {
			bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev->k));
		}

		if (bpos_cmp(expected_start, bp->v.min_key)) {
			bch2_topology_error(c);

			if (__fsck_err(c,
				  FSCK_CAN_FIX|
				  FSCK_CAN_IGNORE|
				  FSCK_NO_RATELIMIT,
				  "btree node with incorrect min_key at btree %s level %u:\n"
				  "  prev %s\n"
				  "  cur %s",
				  bch2_btree_ids[b->c.btree_id], b->c.level,
				  buf1,
				  (bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(cur.k)), buf2)) &&
			    !test_bit(BCH_FS_TOPOLOGY_REPAIR_DONE, &c->flags)) {
				bch_info(c, "Halting mark and sweep to start topology repair pass");
				return FSCK_ERR_START_TOPOLOGY_REPAIR;
			} else {
				set_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags);
			}
		}
	}

	if (is_last && bpos_cmp(cur.k->k.p, node_end)) {
		bch2_topology_error(c);

		if (__fsck_err(c,
			  FSCK_CAN_FIX|
			  FSCK_CAN_IGNORE|
			  FSCK_NO_RATELIMIT,
			  "btree node with incorrect max_key at btree %s level %u:\n"
			  "  %s\n"
			  "  expected %s",
			  bch2_btree_ids[b->c.btree_id], b->c.level,
			  (bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(cur.k)), buf1),
			  (bch2_bpos_to_text(&PBUF(buf2), node_end), buf2)) &&
		    !test_bit(BCH_FS_TOPOLOGY_REPAIR_DONE, &c->flags)) {
			bch_info(c, "Halting mark and sweep to start topology repair pass");
			return FSCK_ERR_START_TOPOLOGY_REPAIR;
		} else {
			set_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags);
		}
	}

	bch2_bkey_buf_copy(prev, c, cur.k);
fsck_err:
	return ret;
}

static void btree_ptr_to_v2(struct btree *b, struct bkey_i_btree_ptr_v2 *dst)
{
	switch (b->key.k.type) {
	case KEY_TYPE_btree_ptr: {
		struct bkey_i_btree_ptr *src = bkey_i_to_btree_ptr(&b->key);

		dst->k.p		= src->k.p;
		dst->v.mem_ptr		= 0;
		dst->v.seq		= b->data->keys.seq;
		dst->v.sectors_written	= 0;
		dst->v.flags		= 0;
		dst->v.min_key		= b->data->min_key;
		set_bkey_val_bytes(&dst->k, sizeof(dst->v) + bkey_val_bytes(&src->k));
		memcpy(dst->v.start, src->v.start, bkey_val_bytes(&src->k));
		break;
	}
	case KEY_TYPE_btree_ptr_v2:
		bkey_copy(&dst->k_i, &b->key);
		break;
	default:
		BUG();
	}
}

static int set_node_min(struct bch_fs *c, struct btree *b, struct bpos new_min)
{
	struct bkey_i_btree_ptr_v2 *new;
	int ret;

	new = kmalloc(BKEY_BTREE_PTR_U64s_MAX * sizeof(u64), GFP_KERNEL);
	if (!new)
		return -ENOMEM;

	btree_ptr_to_v2(b, new);
	b->data->min_key	= new_min;
	new->v.min_key		= new_min;
	SET_BTREE_PTR_RANGE_UPDATED(&new->v, true);

	ret = bch2_journal_key_insert(c, b->c.btree_id, b->c.level + 1, &new->k_i);
	if (ret) {
		kfree(new);
		return ret;
	}

	bch2_btree_node_drop_keys_outside_node(b);

	return 0;
}

static int set_node_max(struct bch_fs *c, struct btree *b, struct bpos new_max)
{
	struct bkey_i_btree_ptr_v2 *new;
	int ret;

	ret = bch2_journal_key_delete(c, b->c.btree_id, b->c.level + 1, b->key.k.p);
	if (ret)
		return ret;

	new = kmalloc(BKEY_BTREE_PTR_U64s_MAX * sizeof(u64), GFP_KERNEL);
	if (!new)
		return -ENOMEM;

	btree_ptr_to_v2(b, new);
	b->data->max_key	= new_max;
	new->k.p		= new_max;
	SET_BTREE_PTR_RANGE_UPDATED(&new->v, true);

	ret = bch2_journal_key_insert(c, b->c.btree_id, b->c.level + 1, &new->k_i);
	if (ret) {
		kfree(new);
		return ret;
	}

	bch2_btree_node_drop_keys_outside_node(b);

	mutex_lock(&c->btree_cache.lock);
	bch2_btree_node_hash_remove(&c->btree_cache, b);

	bkey_copy(&b->key, &new->k_i);
	ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
	BUG_ON(ret);
	mutex_unlock(&c->btree_cache.lock);
	return 0;
}

static int btree_repair_node_boundaries(struct bch_fs *c, struct btree *b,
					struct btree *prev, struct btree *cur)
{
	struct bpos expected_start = !prev
		? b->data->min_key
		: bpos_successor(prev->key.k.p);
	char buf1[200], buf2[200];
	int ret = 0;

	if (!prev) {
		struct printbuf out = PBUF(buf1);
		pr_buf(&out, "start of node: ");
		bch2_bpos_to_text(&out, b->data->min_key);
	} else {
		bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&prev->key));
	}

	bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(&cur->key));

	if (prev &&
	    bpos_cmp(expected_start, cur->data->min_key) > 0 &&
	    BTREE_NODE_SEQ(cur->data) > BTREE_NODE_SEQ(prev->data)) {
		/* cur overwrites prev: */

		if (mustfix_fsck_err_on(bpos_cmp(prev->data->min_key,
						 cur->data->min_key) >= 0, c,
				"btree node overwritten by next node at btree %s level %u:\n"
				"  node %s\n"
				"  next %s",
				bch2_btree_ids[b->c.btree_id], b->c.level,
				buf1, buf2))
			return DROP_PREV_NODE;

		if (mustfix_fsck_err_on(bpos_cmp(prev->key.k.p,
						 bpos_predecessor(cur->data->min_key)), c,
				"btree node with incorrect max_key at btree %s level %u:\n"
				"  node %s\n"
				"  next %s",
				bch2_btree_ids[b->c.btree_id], b->c.level,
				buf1, buf2))
			ret = set_node_max(c, prev,
					   bpos_predecessor(cur->data->min_key));
	} else {
		/* prev overwrites cur: */

		if (mustfix_fsck_err_on(bpos_cmp(expected_start,
						 cur->data->max_key) >= 0, c,
				"btree node overwritten by prev node at btree %s level %u:\n"
				"  prev %s\n"
				"  node %s",
				bch2_btree_ids[b->c.btree_id], b->c.level,
				buf1, buf2))
			return DROP_THIS_NODE;

		if (mustfix_fsck_err_on(bpos_cmp(expected_start, cur->data->min_key), c,
				"btree node with incorrect min_key at btree %s level %u:\n"
				"  prev %s\n"
				"  node %s",
				bch2_btree_ids[b->c.btree_id], b->c.level,
				buf1, buf2))
		    ret = set_node_min(c, cur, expected_start);
	}
fsck_err:
	return ret;
}

static int btree_repair_node_end(struct bch_fs *c, struct btree *b,
				 struct btree *child)
{
	char buf1[200], buf2[200];
	int ret = 0;

	if (mustfix_fsck_err_on(bpos_cmp(child->key.k.p, b->key.k.p), c,
			"btree node with incorrect max_key at btree %s level %u:\n"
			"  %s\n"
			"  expected %s",
			bch2_btree_ids[b->c.btree_id], b->c.level,
			(bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&child->key)), buf1),
			(bch2_bpos_to_text(&PBUF(buf2), b->key.k.p), buf2))) {
		ret = set_node_max(c, child, b->key.k.p);
		if (ret)
			return ret;
	}
fsck_err:
	return ret;
}

static int bch2_btree_repair_topology_recurse(struct bch_fs *c, struct btree *b)
{
	struct btree_and_journal_iter iter;
	struct bkey_s_c k;
	struct bkey_buf prev_k, cur_k;
	struct btree *prev = NULL, *cur = NULL;
	bool have_child, dropped_children = false;
	char buf[200];
	int ret = 0;

	if (!b->c.level)
		return 0;
again:
	prev = NULL;
	have_child = dropped_children = false;
	bch2_bkey_buf_init(&prev_k);
	bch2_bkey_buf_init(&cur_k);
	bch2_btree_and_journal_iter_init_node_iter(&iter, c, b);

	while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
		BUG_ON(bpos_cmp(k.k->p, b->data->min_key) < 0);
		BUG_ON(bpos_cmp(k.k->p, b->data->max_key) > 0);

		bch2_btree_and_journal_iter_advance(&iter);
		bch2_bkey_buf_reassemble(&cur_k, c, k);

		cur = bch2_btree_node_get_noiter(c, cur_k.k,
					b->c.btree_id, b->c.level - 1,
					false);
		ret = PTR_ERR_OR_ZERO(cur);

		if (mustfix_fsck_err_on(ret == -EIO, c,
				"Unreadable btree node at btree %s level %u:\n"
				"  %s",
				bch2_btree_ids[b->c.btree_id],
				b->c.level - 1,
				(bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(cur_k.k)), buf))) {
			bch2_btree_node_evict(c, cur_k.k);
			ret = bch2_journal_key_delete(c, b->c.btree_id,
						      b->c.level, cur_k.k->k.p);
			if (ret)
				break;
			continue;
		}

		if (ret) {
			bch_err(c, "%s: error %i getting btree node",
				__func__, ret);
			break;
		}

		ret = btree_repair_node_boundaries(c, b, prev, cur);

		if (ret == DROP_THIS_NODE) {
			six_unlock_read(&cur->c.lock);
			bch2_btree_node_evict(c, cur_k.k);
			ret = bch2_journal_key_delete(c, b->c.btree_id,
						      b->c.level, cur_k.k->k.p);
			if (ret)
				break;
			continue;
		}

		if (prev)
			six_unlock_read(&prev->c.lock);
		prev = NULL;

		if (ret == DROP_PREV_NODE) {
			bch2_btree_node_evict(c, prev_k.k);
			ret = bch2_journal_key_delete(c, b->c.btree_id,
						      b->c.level, prev_k.k->k.p);
			if (ret)
				break;

			bch2_btree_and_journal_iter_exit(&iter);
			bch2_bkey_buf_exit(&prev_k, c);
			bch2_bkey_buf_exit(&cur_k, c);
			goto again;
		} else if (ret)
			break;

		prev = cur;
		cur = NULL;
		bch2_bkey_buf_copy(&prev_k, c, cur_k.k);
	}

	if (!ret && !IS_ERR_OR_NULL(prev)) {
		BUG_ON(cur);
		ret = btree_repair_node_end(c, b, prev);
	}

	if (!IS_ERR_OR_NULL(prev))
		six_unlock_read(&prev->c.lock);
	prev = NULL;
	if (!IS_ERR_OR_NULL(cur))
		six_unlock_read(&cur->c.lock);
	cur = NULL;

	if (ret)
		goto err;

	bch2_btree_and_journal_iter_exit(&iter);
	bch2_btree_and_journal_iter_init_node_iter(&iter, c, b);

	while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
		bch2_bkey_buf_reassemble(&cur_k, c, k);
		bch2_btree_and_journal_iter_advance(&iter);

		cur = bch2_btree_node_get_noiter(c, cur_k.k,
					b->c.btree_id, b->c.level - 1,
					false);
		ret = PTR_ERR_OR_ZERO(cur);

		if (ret) {
			bch_err(c, "%s: error %i getting btree node",
				__func__, ret);
			goto err;
		}

		ret = bch2_btree_repair_topology_recurse(c, cur);
		six_unlock_read(&cur->c.lock);
		cur = NULL;

		if (ret == DROP_THIS_NODE) {
			bch2_btree_node_evict(c, cur_k.k);
			ret = bch2_journal_key_delete(c, b->c.btree_id,
						      b->c.level, cur_k.k->k.p);
			dropped_children = true;
		}

		if (ret)
			goto err;

		have_child = true;
	}

	if (mustfix_fsck_err_on(!have_child, c,
			"empty interior btree node at btree %s level %u\n"
			"  %s",
			bch2_btree_ids[b->c.btree_id],
			b->c.level,
			(bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(&b->key)), buf)))
		ret = DROP_THIS_NODE;
err:
fsck_err:
	if (!IS_ERR_OR_NULL(prev))
		six_unlock_read(&prev->c.lock);
	if (!IS_ERR_OR_NULL(cur))
		six_unlock_read(&cur->c.lock);

	bch2_btree_and_journal_iter_exit(&iter);
	bch2_bkey_buf_exit(&prev_k, c);
	bch2_bkey_buf_exit(&cur_k, c);

	if (!ret && dropped_children)
		goto again;

	return ret;
}

static int bch2_repair_topology(struct bch_fs *c)
{
	struct btree *b;
	unsigned i;
	int ret = 0;

	for (i = 0; i < BTREE_ID_NR && !ret; i++) {
		b = c->btree_roots[i].b;
		if (btree_node_fake(b))
			continue;

		six_lock_read(&b->c.lock, NULL, NULL);
		ret = bch2_btree_repair_topology_recurse(c, b);
		six_unlock_read(&b->c.lock);

		if (ret == DROP_THIS_NODE) {
			bch_err(c, "empty btree root - repair unimplemented");
			ret = FSCK_ERR_EXIT;
		}
	}

	return ret;
}

static int bch2_check_fix_ptrs(struct bch_fs *c, enum btree_id btree_id,
			       unsigned level, bool is_root,
			       struct bkey_s_c *k)
{
	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(*k);
	const union bch_extent_entry *entry;
	struct extent_ptr_decoded p = { 0 };
	bool do_update = false;
	char buf[200];
	int ret = 0;

	/*
	 * XXX
	 * use check_bucket_ref here
	 */
	bkey_for_each_ptr_decode(k->k, ptrs, p, entry) {
		struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
		struct bucket *g = PTR_BUCKET(ca, &p.ptr, true);
		struct bucket *g2 = PTR_BUCKET(ca, &p.ptr, false);
		enum bch_data_type data_type = bch2_bkey_ptr_data_type(*k, &entry->ptr);

		if (fsck_err_on(!g->gen_valid, c,
				"bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
				bch2_data_types[ptr_data_type(k->k, &p.ptr)],
				p.ptr.gen,
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf))) {
			if (!p.ptr.cached) {
				g2->_mark.gen	= g->_mark.gen		= p.ptr.gen;
				g2->gen_valid	= g->gen_valid		= true;
				set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);
			} else {
				do_update = true;
			}
		}

		if (fsck_err_on(data_type == BCH_DATA_btree &&
				g->mark.gen != p.ptr.gen, c,
				"bucket %u:%zu data type %s has metadata but wrong gen: %u != %u\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
				bch2_data_types[ptr_data_type(k->k, &p.ptr)],
				p.ptr.gen, g->mark.gen,
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf))) {
			g2->_mark.data_type	= g->_mark.data_type	= data_type;
			g2->gen_valid		= g->gen_valid		= true;
			set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);
		}

		if (fsck_err_on(gen_cmp(p.ptr.gen, g->mark.gen) > 0, c,
				"bucket %u:%zu data type %s ptr gen in the future: %u > %u\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
				bch2_data_types[ptr_data_type(k->k, &p.ptr)],
				p.ptr.gen, g->mark.gen,
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf))) {
			if (!p.ptr.cached) {
				g2->_mark.gen	= g->_mark.gen	= p.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_NEED_ANOTHER_GC, &c->flags);
				set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);
			} else {
				do_update = true;
			}
		}

		if (fsck_err_on(gen_cmp(g->mark.gen, p.ptr.gen) > BUCKET_GC_GEN_MAX, c,
				"bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->mark.gen,
				bch2_data_types[ptr_data_type(k->k, &p.ptr)],
				p.ptr.gen,
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf)))
			do_update = true;

		if (fsck_err_on(!p.ptr.cached &&
				gen_cmp(p.ptr.gen, g->mark.gen) < 0, c,
				"bucket %u:%zu data type %s stale dirty ptr: %u < %u\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
				bch2_data_types[ptr_data_type(k->k, &p.ptr)],
				p.ptr.gen, g->mark.gen,
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf)))
			do_update = true;

		if (p.ptr.gen != g->mark.gen)
			continue;

		if (fsck_err_on(g->mark.data_type &&
				g->mark.data_type != data_type, c,
				"bucket %u:%zu different types of data in same bucket: %s, %s\n"
				"while marking %s",
				p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
				bch2_data_types[g->mark.data_type],
				bch2_data_types[data_type],
				(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf))) {
			if (data_type == BCH_DATA_btree) {
				g2->_mark.data_type	= g->_mark.data_type	= data_type;
				g2->gen_valid		= g->gen_valid		= true;
				set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);
			} else {
				do_update = true;
			}
		}

		if (p.has_ec) {
			struct stripe *m = genradix_ptr(&c->stripes[true], p.ec.idx);

			if (fsck_err_on(!m || !m->alive, c,
					"pointer to nonexistent stripe %llu\n"
					"while marking %s",
					(u64) p.ec.idx,
					(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf)))
				do_update = true;

			if (fsck_err_on(!bch2_ptr_matches_stripe_m(m, p), c,
					"pointer does not match stripe %llu\n"
					"while marking %s",
					(u64) p.ec.idx,
					(bch2_bkey_val_to_text(&PBUF(buf), c, *k), buf)))
				do_update = true;
		}
	}

	if (do_update) {
		struct bkey_ptrs ptrs;
		union bch_extent_entry *entry;
		struct bch_extent_ptr *ptr;
		struct bkey_i *new;

		if (is_root) {
			bch_err(c, "cannot update btree roots yet");
			return -EINVAL;
		}

		new = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
		if (!new) {
			bch_err(c, "%s: error allocating new key", __func__);
			return -ENOMEM;
		}

		bkey_reassemble(new, *k);

		if (level) {
			/*
			 * We don't want to drop btree node pointers - if the
			 * btree node isn't there anymore, the read path will
			 * sort it out:
			 */
			ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
			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);

				ptr->gen = g->mark.gen;
			}
		} else {
			bch2_bkey_drop_ptrs(bkey_i_to_s(new), ptr, ({
				struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
				struct bucket *g = PTR_BUCKET(ca, ptr, true);
				enum bch_data_type data_type = bch2_bkey_ptr_data_type(*k, ptr);

				(ptr->cached &&
				 (!g->gen_valid || gen_cmp(ptr->gen, g->mark.gen) > 0)) ||
				(!ptr->cached &&
				 gen_cmp(ptr->gen, g->mark.gen) < 0) ||
				gen_cmp(g->mark.gen, ptr->gen) > BUCKET_GC_GEN_MAX ||
				(g->mark.data_type &&
				 g->mark.data_type != data_type);
			}));
again:
			ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
			bkey_extent_entry_for_each(ptrs, entry) {
				if (extent_entry_type(entry) == BCH_EXTENT_ENTRY_stripe_ptr) {
					struct stripe *m = genradix_ptr(&c->stripes[true],
									entry->stripe_ptr.idx);
					union bch_extent_entry *next_ptr;

					bkey_extent_entry_for_each_from(ptrs, next_ptr, entry)
						if (extent_entry_type(next_ptr) == BCH_EXTENT_ENTRY_ptr)
							goto found;
					next_ptr = NULL;
found:
					if (!next_ptr) {
						bch_err(c, "aieee, found stripe ptr with no data ptr");
						continue;
					}

					if (!m || !m->alive ||
					    !__bch2_ptr_matches_stripe(&m->ptrs[entry->stripe_ptr.block],
								       &next_ptr->ptr,
								       m->sectors)) {
						bch2_bkey_extent_entry_drop(new, entry);
						goto again;
					}
				}
			}
		}

		ret = bch2_journal_key_insert(c, btree_id, level, new);
		if (ret)
			kfree(new);
		else
			*k = bkey_i_to_s_c(new);
	}
fsck_err:
	return ret;
}

/* marking of btree keys/nodes: */

static int bch2_gc_mark_key(struct btree_trans *trans, enum btree_id btree_id,
			    unsigned level, bool is_root,
			    struct bkey_s_c *k,
			    u8 *max_stale, bool initial)
{
	struct bch_fs *c = trans->c;
	struct bkey_ptrs_c ptrs;
	const struct bch_extent_ptr *ptr;
	struct bkey deleted = KEY(0, 0, 0);
	struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
	unsigned flags =
		BTREE_TRIGGER_GC|
		(initial ? BTREE_TRIGGER_NOATOMIC : 0);
	int ret = 0;

	deleted.p = k->k->p;

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

		ret = bch2_check_fix_ptrs(c, btree_id, level, is_root, k);
		if (ret)
			goto err;

		if (fsck_err_on(k->k->version.lo > atomic64_read(&c->key_version), c,
				"key version number higher than recorded: %llu > %llu",
				k->k->version.lo,
				atomic64_read(&c->key_version)))
			atomic64_set(&c->key_version, k->k->version.lo);
	}

	ptrs = bch2_bkey_ptrs_c(*k);
	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));
	}

	ret = bch2_mark_key(trans, old, *k, flags);
fsck_err:
err:
	if (ret)
		bch_err(c, "%s: ret %i", __func__, ret);
	return ret;
}

static int btree_gc_mark_node(struct btree_trans *trans, struct btree *b, u8 *max_stale,
			      bool initial)
{
	struct bch_fs *c = trans->c;
	struct btree_node_iter iter;
	struct bkey unpacked;
	struct bkey_s_c k;
	struct bkey_buf prev, cur;
	int ret = 0;

	*max_stale = 0;

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

	bch2_btree_node_iter_init_from_start(&iter, b);
	bch2_bkey_buf_init(&prev);
	bch2_bkey_buf_init(&cur);
	bkey_init(&prev.k->k);

	while ((k = bch2_btree_node_iter_peek_unpack(&iter, b, &unpacked)).k) {
		ret = bch2_gc_mark_key(trans, b->c.btree_id, b->c.level, false,
				       &k, max_stale, initial);
		if (ret)
			break;

		bch2_btree_node_iter_advance(&iter, b);

		if (b->c.level) {
			bch2_bkey_buf_reassemble(&cur, c, k);

			ret = bch2_gc_check_topology(c, b, &prev, cur,
					bch2_btree_node_iter_end(&iter));
			if (ret)
				break;
		}
	}

	bch2_bkey_buf_exit(&cur, c);
	bch2_bkey_buf_exit(&prev, c);
	return ret;
}

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

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

	__for_each_btree_node(trans, iter, btree_id, POS_MIN,
			      0, depth, BTREE_ITER_PREFETCH, b, ret) {
		bch2_verify_btree_nr_keys(b);

		gc_pos_set(c, gc_pos_btree_node(b));

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

		if (!initial) {
			if (max_stale > 64)
				bch2_btree_node_rewrite(trans, &iter, b,
						BTREE_INSERT_NOWAIT|
						BTREE_INSERT_GC_LOCK_HELD);
			else if (!bch2_btree_gc_rewrite_disabled &&
				 (bch2_btree_gc_always_rewrite || max_stale > 16))
				bch2_btree_node_rewrite(trans, &iter,
						b, BTREE_INSERT_NOWAIT|
						BTREE_INSERT_GC_LOCK_HELD);
		}
	}
	bch2_trans_iter_exit(trans, &iter);

	if (ret)
		return ret;

	mutex_lock(&c->btree_root_lock);
	b = c->btree_roots[btree_id].b;
	if (!btree_node_fake(b)) {
		struct bkey_s_c k = bkey_i_to_s_c(&b->key);

		ret = bch2_gc_mark_key(trans, b->c.btree_id, b->c.level, true,
				       &k, &max_stale, initial);
	}
	gc_pos_set(c, gc_pos_btree_root(b->c.btree_id));
	mutex_unlock(&c->btree_root_lock);

	return ret;
}

static int bch2_gc_btree_init_recurse(struct btree_trans *trans, struct btree *b,
				      unsigned target_depth)
{
	struct bch_fs *c = trans->c;
	struct btree_and_journal_iter iter;
	struct bkey_s_c k;
	struct bkey_buf cur, prev;
	u8 max_stale = 0;
	char buf[200];
	int ret = 0;

	bch2_btree_and_journal_iter_init_node_iter(&iter, c, b);
	bch2_bkey_buf_init(&prev);
	bch2_bkey_buf_init(&cur);
	bkey_init(&prev.k->k);

	while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
		BUG_ON(bpos_cmp(k.k->p, b->data->min_key) < 0);
		BUG_ON(bpos_cmp(k.k->p, b->data->max_key) > 0);

		ret = bch2_gc_mark_key(trans, b->c.btree_id, b->c.level, false,
				       &k, &max_stale, true);
		if (ret) {
			bch_err(c, "%s: error %i from bch2_gc_mark_key", __func__, ret);
			goto fsck_err;
		}

		if (b->c.level) {
			bch2_bkey_buf_reassemble(&cur, c, k);
			k = bkey_i_to_s_c(cur.k);

			bch2_btree_and_journal_iter_advance(&iter);

			ret = bch2_gc_check_topology(c, b,
					&prev, cur,
					!bch2_btree_and_journal_iter_peek(&iter).k);
			if (ret)
				goto fsck_err;
		} else {
			bch2_btree_and_journal_iter_advance(&iter);
		}
	}

	if (b->c.level > target_depth) {
		bch2_btree_and_journal_iter_exit(&iter);
		bch2_btree_and_journal_iter_init_node_iter(&iter, c, b);

		while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
			struct btree *child;

			bch2_bkey_buf_reassemble(&cur, c, k);
			bch2_btree_and_journal_iter_advance(&iter);

			child = bch2_btree_node_get_noiter(c, cur.k,
						b->c.btree_id, b->c.level - 1,
						false);
			ret = PTR_ERR_OR_ZERO(child);

			if (ret == -EIO) {
				bch2_topology_error(c);

				if (__fsck_err(c,
					  FSCK_CAN_FIX|
					  FSCK_CAN_IGNORE|
					  FSCK_NO_RATELIMIT,
					  "Unreadable btree node at btree %s level %u:\n"
					  "  %s",
					  bch2_btree_ids[b->c.btree_id],
					  b->c.level - 1,
					  (bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(cur.k)), buf)) &&
				    !test_bit(BCH_FS_TOPOLOGY_REPAIR_DONE, &c->flags)) {
					ret = FSCK_ERR_START_TOPOLOGY_REPAIR;
					bch_info(c, "Halting mark and sweep to start topology repair pass");
					goto fsck_err;
				} else {
					/* Continue marking when opted to not
					 * fix the error: */
					ret = 0;
					set_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags);
					continue;
				}
			} else if (ret) {
				bch_err(c, "%s: error %i getting btree node",
					__func__, ret);
				break;
			}

			ret = bch2_gc_btree_init_recurse(trans, child,
							 target_depth);
			six_unlock_read(&child->c.lock);

			if (ret)
				break;
		}
	}
fsck_err:
	bch2_bkey_buf_exit(&cur, c);
	bch2_bkey_buf_exit(&prev, c);
	bch2_btree_and_journal_iter_exit(&iter);
	return ret;
}

static int bch2_gc_btree_init(struct btree_trans *trans,
			      enum btree_id btree_id,
			      bool metadata_only)
{
	struct bch_fs *c = trans->c;
	struct btree *b;
	unsigned target_depth = metadata_only		? 1
		: bch2_expensive_debug_checks		? 0
		: !btree_node_type_needs_gc(btree_id)	? 1
		: 0;
	u8 max_stale = 0;
	char buf[100];
	int ret = 0;

	b = c->btree_roots[btree_id].b;

	if (btree_node_fake(b))
		return 0;

	six_lock_read(&b->c.lock, NULL, NULL);
	if (mustfix_fsck_err_on(bpos_cmp(b->data->min_key, POS_MIN), c,
			"btree root with incorrect min_key: %s",
			(bch2_bpos_to_text(&PBUF(buf), b->data->min_key), buf))) {
		bch_err(c, "repair unimplemented");
		ret = FSCK_ERR_EXIT;
		goto fsck_err;
	}

	if (mustfix_fsck_err_on(bpos_cmp(b->data->max_key, SPOS_MAX), c,
			"btree root with incorrect max_key: %s",
			(bch2_bpos_to_text(&PBUF(buf), b->data->max_key), buf))) {
		bch_err(c, "repair unimplemented");
		ret = FSCK_ERR_EXIT;
		goto fsck_err;
	}

	if (b->c.level >= target_depth)
		ret = bch2_gc_btree_init_recurse(trans, b, target_depth);

	if (!ret) {
		struct bkey_s_c k = bkey_i_to_s_c(&b->key);

		ret = bch2_gc_mark_key(trans, b->c.btree_id, b->c.level, true,
				       &k, &max_stale, true);
	}
fsck_err:
	six_unlock_read(&b->c.lock);

	if (ret < 0)
		bch_err(c, "%s: ret %i", __func__, ret);
	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 bch2_gc_btrees(struct bch_fs *c, bool initial, bool metadata_only)
{
	struct btree_trans trans;
	enum btree_id ids[BTREE_ID_NR];
	unsigned i;
	int ret = 0;

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

	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 && !ret; i++)
		ret = initial
			? bch2_gc_btree_init(&trans, ids[i], metadata_only)
			: bch2_gc_btree(&trans, ids[i], initial, metadata_only);

	if (ret < 0)
		bch_err(c, "%s: ret %i", __func__, ret);

	bch2_trans_exit(&trans);
	return ret;
}

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);
}

#if 0
/* 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), BTREE_TRIGGER_GC);

	mutex_unlock(&c->btree_interior_update_lock);
}
#endif

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_gc);
		ca->usage_gc = 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 = NULL;
	bool verify = !metadata_only && (!initial ||
		       (c->sb.compat & (1ULL << BCH_COMPAT_alloc_info)));
	unsigned i, dev;
	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;					\
		set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);		\
	}
#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",		\
				iter.pos, ##__VA_ARGS__,		\
				dst->_f, src->_f);			\
		dst->_f = src->_f;					\
		set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);		\
	}
#define copy_bucket_field(_f)						\
	if (dst->b[b]._f != src->b[b]._f) {				\
		if (verify)						\
			fsck_err(c, "bucket %u:%zu gen %u data type %s has wrong " #_f	\
				": got %u, should be %u", dev, b,	\
				dst->b[b].mark.gen,			\
				bch2_data_types[dst->b[b].mark.data_type],\
				dst->b[b]._f, src->b[b]._f);		\
		dst->b[b]._f = src->b[b]._f;				\
		set_bit(BCH_FS_NEED_ALLOC_WRITE, &c->flags);		\
	}
#define copy_dev_field(_f, _msg, ...)					\
	copy_field(_f, "dev %u has wrong " _msg, dev, ##__VA_ARGS__)
#define copy_fs_field(_f, _msg, ...)					\
	copy_field(_f, "fs has wrong " _msg, ##__VA_ARGS__)

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

		while ((src = genradix_iter_peek(&iter, &c->stripes[1]))) {
			dst = genradix_ptr_alloc(&c->stripes[0], iter.pos, GFP_KERNEL);

			if (dst->alive		!= src->alive ||
			    dst->sectors	!= src->sectors ||
			    dst->algorithm	!= src->algorithm ||
			    dst->nr_blocks	!= src->nr_blocks ||
			    dst->nr_redundant	!= src->nr_redundant) {
				bch_err(c, "unexpected stripe inconsistency at bch2_gc_done, confused");
				ret = -EINVAL;
				goto fsck_err;
			}

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

			dst->blocks_nonempty = 0;
			for (i = 0; i < dst->nr_blocks; i++)
				dst->blocks_nonempty += dst->block_sectors[i] != 0;

			genradix_iter_advance(&iter, &c->stripes[1]);
		}
	}

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

	for_each_member_device(ca, c, dev) {
		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(_mark.gen);
			copy_bucket_field(_mark.data_type);
			copy_bucket_field(_mark.stripe);
			copy_bucket_field(_mark.dirty_sectors);
			copy_bucket_field(_mark.cached_sectors);
			copy_bucket_field(stripe_redundancy);
			copy_bucket_field(stripe);

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

		{
			struct bch_dev_usage *dst = ca->usage_base;
			struct bch_dev_usage *src = (void *)
				bch2_acc_percpu_u64s((void *) ca->usage_gc,
						     dev_usage_u64s());

			copy_dev_field(buckets_ec,		"buckets_ec");
			copy_dev_field(buckets_unavailable,	"buckets_unavailable");

			for (i = 0; i < BCH_DATA_NR; i++) {
				copy_dev_field(d[i].buckets,	"%s buckets", bch2_data_types[i]);
				copy_dev_field(d[i].sectors,	"%s sectors", bch2_data_types[i]);
				copy_dev_field(d[i].fragmented,	"%s fragmented", bch2_data_types[i]);
			}
		}
	};

	{
		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:
	if (ca)
		percpu_ref_put(&ca->ref);
	if (ret)
		bch_err(c, "%s: ret %i", __func__, ret);
	return ret;
}

static int bch2_gc_start(struct bch_fs *c,
			 bool metadata_only)
{
	struct bch_dev *ca = NULL;
	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) {
		bch_err(c, "error allocating c->usage_gc");
		return -ENOMEM;
	}

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

		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);
			bch_err(c, "error allocating ca->buckets[gc]");
			return -ENOMEM;
		}

		ca->usage_gc = alloc_percpu(struct bch_dev_usage);
		if (!ca->usage_gc) {
			bch_err(c, "error allocating ca->usage_gc");
			percpu_ref_put(&ca->ref);
			return -ENOMEM;
		}
	}

	ret = bch2_ec_mem_alloc(c, true);
	if (ret) {
		bch_err(c, "error allocating ec gc mem");
		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;
		}
	};

	percpu_up_write(&c->mark_lock);

	return 0;
}

static int bch2_gc_reflink_done_initial_fn(struct btree_trans *trans,
					   struct bkey_s_c k)
{
	struct bch_fs *c = trans->c;
	struct reflink_gc *r;
	const __le64 *refcount = bkey_refcount_c(k);
	char buf[200];
	int ret = 0;

	if (!refcount)
		return 0;

	r = genradix_ptr(&c->reflink_gc_table, c->reflink_gc_idx++);
	if (!r)
		return -ENOMEM;

	if (!r ||
	    r->offset != k.k->p.offset ||
	    r->size != k.k->size) {
		bch_err(c, "unexpected inconsistency walking reflink table at gc finish");
		return -EINVAL;
	}

	if (fsck_err_on(r->refcount != le64_to_cpu(*refcount), c,
			"reflink key has wrong refcount:\n"
			"  %s\n"
			"  should be %u",
			(bch2_bkey_val_to_text(&PBUF(buf), c, k), buf),
			r->refcount)) {
		struct bkey_i *new;

		new = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
		if (!new) {
			ret = -ENOMEM;
			goto fsck_err;
		}

		bkey_reassemble(new, k);

		if (!r->refcount) {
			new->k.type = KEY_TYPE_deleted;
			new->k.size = 0;
		} else {
			*bkey_refcount(new) = cpu_to_le64(r->refcount);
		}

		ret = bch2_journal_key_insert(c, BTREE_ID_reflink, 0, new);
		if (ret)
			kfree(new);
	}
fsck_err:
	return ret;
}

static int bch2_gc_reflink_done(struct bch_fs *c, bool initial,
				bool metadata_only)
{
	struct btree_trans trans;
	struct btree_iter iter;
	struct bkey_s_c k;
	struct reflink_gc *r;
	size_t idx = 0;
	char buf[200];
	int ret = 0;

	if (metadata_only)
		return 0;

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

	if (initial) {
		c->reflink_gc_idx = 0;

		ret = bch2_btree_and_journal_walk(&trans, BTREE_ID_reflink,
				bch2_gc_reflink_done_initial_fn);
		goto out;
	}

	for_each_btree_key(&trans, iter, BTREE_ID_reflink, POS_MIN,
			   BTREE_ITER_PREFETCH, k, ret) {
		const __le64 *refcount = bkey_refcount_c(k);

		if (!refcount)
			continue;

		r = genradix_ptr(&c->reflink_gc_table, idx);
		if (!r ||
		    r->offset != k.k->p.offset ||
		    r->size != k.k->size) {
			bch_err(c, "unexpected inconsistency walking reflink table at gc finish");
			ret = -EINVAL;
			break;
		}

		if (fsck_err_on(r->refcount != le64_to_cpu(*refcount), c,
				"reflink key has wrong refcount:\n"
				"  %s\n"
				"  should be %u",
				(bch2_bkey_val_to_text(&PBUF(buf), c, k), buf),
				r->refcount)) {
			struct bkey_i *new;

			new = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
			if (!new) {
				ret = -ENOMEM;
				break;
			}

			bkey_reassemble(new, k);

			if (!r->refcount)
				new->k.type = KEY_TYPE_deleted;
			else
				*bkey_refcount(new) = cpu_to_le64(r->refcount);

			ret = __bch2_trans_do(&trans, NULL, NULL, 0,
					__bch2_btree_insert(&trans, BTREE_ID_reflink, new));
			kfree(new);

			if (ret)
				break;
		}
	}
fsck_err:
	bch2_trans_iter_exit(&trans, &iter);
out:
	genradix_free(&c->reflink_gc_table);
	c->reflink_gc_nr = 0;
	bch2_trans_exit(&trans);
	return ret;
}

static int bch2_gc_reflink_start_initial_fn(struct btree_trans *trans,
					    struct bkey_s_c k)
{

	struct bch_fs *c = trans->c;
	struct reflink_gc *r;
	const __le64 *refcount = bkey_refcount_c(k);

	if (!refcount)
		return 0;

	r = genradix_ptr_alloc(&c->reflink_gc_table, c->reflink_gc_nr++,
			       GFP_KERNEL);
	if (!r)
		return -ENOMEM;

	r->offset	= k.k->p.offset;
	r->size		= k.k->size;
	r->refcount	= 0;
	return 0;
}

static int bch2_gc_reflink_start(struct bch_fs *c, bool initial,
				 bool metadata_only)
{
	struct btree_trans trans;
	struct btree_iter iter;
	struct bkey_s_c k;
	struct reflink_gc *r;
	int ret = 0;

	if (metadata_only)
		return 0;

	bch2_trans_init(&trans, c, 0, 0);
	genradix_free(&c->reflink_gc_table);
	c->reflink_gc_nr = 0;

	if (initial) {
		ret = bch2_btree_and_journal_walk(&trans, BTREE_ID_reflink,
						bch2_gc_reflink_start_initial_fn);
		goto out;
	}

	for_each_btree_key(&trans, iter, BTREE_ID_reflink, POS_MIN,
			   BTREE_ITER_PREFETCH, k, ret) {
		const __le64 *refcount = bkey_refcount_c(k);

		if (!refcount)
			continue;

		r = genradix_ptr_alloc(&c->reflink_gc_table, c->reflink_gc_nr++,
				       GFP_KERNEL);
		if (!r) {
			ret = -ENOMEM;
			break;
		}

		r->offset	= k.k->p.offset;
		r->size		= k.k->size;
		r->refcount	= 0;
	}
	bch2_trans_iter_exit(&trans, &iter);
out:
	bch2_trans_exit(&trans);
	return ret;
}

/**
 * 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, bool initial, bool metadata_only)
{
	struct bch_dev *ca;
	u64 start_time = local_clock();
	unsigned i, iter = 0;
	int ret;

	lockdep_assert_held(&c->state_lock);
	trace_gc_start(c);

	down_write(&c->gc_lock);

	/* flush interior btree updates: */
	closure_wait_event(&c->btree_interior_update_wait,
			   !bch2_btree_interior_updates_nr_pending(c));
again:
	ret   = bch2_gc_start(c, metadata_only) ?:
		bch2_gc_reflink_start(c, initial, metadata_only);
	if (ret)
		goto out;

	bch2_mark_superblocks(c);

	if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb) &&
	    !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags) &&
	    c->opts.fix_errors != FSCK_OPT_NO) {
		bch_info(c, "starting topology repair pass");
		ret = bch2_repair_topology(c);
		if (ret)
			goto out;
		bch_info(c, "topology repair pass done");

		set_bit(BCH_FS_TOPOLOGY_REPAIR_DONE, &c->flags);
	}

	ret = bch2_gc_btrees(c, initial, metadata_only);

	if (ret == FSCK_ERR_START_TOPOLOGY_REPAIR &&
	    !test_bit(BCH_FS_TOPOLOGY_REPAIR_DONE, &c->flags) &&
	    !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags)) {
		set_bit(BCH_FS_NEED_ANOTHER_GC, &c->flags);
		ret = 0;
	}

	if (ret == FSCK_ERR_START_TOPOLOGY_REPAIR)
		ret = FSCK_ERR_EXIT;

	if (ret)
		goto out;

#if 0
	bch2_mark_pending_btree_node_frees(c);
#endif
	c->gc_count++;

	if (test_bit(BCH_FS_NEED_ANOTHER_GC, &c->flags) ||
	    (!iter && bch2_test_restart_gc)) {
		/*
		 * XXX: make sure gens we fixed got saved
		 */
		if (iter++ <= 2) {
			bch_info(c, "Second GC pass needed, restarting:");
			clear_bit(BCH_FS_NEED_ANOTHER_GC, &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;
	}
out:
	if (!ret) {
		bch2_journal_block(&c->journal);

		percpu_down_write(&c->mark_lock);
		ret   = bch2_gc_reflink_done(c, initial, metadata_only) ?:
			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;
}

static bool gc_btree_gens_key(struct bch_fs *c, struct bkey_s_c k)
{
	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
	const struct bch_extent_ptr *ptr;

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

		if (gen_after(g->mark.gen, ptr->gen) > 16) {
			percpu_up_read(&c->mark_lock);
			return true;
		}
	}

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

		if (gen_after(g->gc_gen, ptr->gen))
			g->gc_gen = ptr->gen;
	}
	percpu_up_read(&c->mark_lock);

	return false;
}

/*
 * For recalculating oldest gen, we only need to walk keys in leaf nodes; btree
 * node pointers currently never have cached pointers that can become stale:
 */
static int bch2_gc_btree_gens(struct bch_fs *c, enum btree_id btree_id)
{
	struct btree_trans trans;
	struct btree_iter iter;
	struct bkey_s_c k;
	struct bkey_buf sk;
	int ret = 0, commit_err = 0;

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

	bch2_trans_iter_init(&trans, &iter, btree_id, POS_MIN,
			     BTREE_ITER_PREFETCH|
			     BTREE_ITER_NOT_EXTENTS|
			     BTREE_ITER_ALL_SNAPSHOTS);

	while ((bch2_trans_begin(&trans),
		k = bch2_btree_iter_peek(&iter)).k) {
		ret = bkey_err(k);

		if (ret == -EINTR)
			continue;
		if (ret)
			break;

		c->gc_gens_pos = iter.pos;

		if (gc_btree_gens_key(c, k) && !commit_err) {
			bch2_bkey_buf_reassemble(&sk, c, k);
			bch2_extent_normalize(c, bkey_i_to_s(sk.k));

			commit_err =
				bch2_trans_update(&trans, &iter, sk.k, 0) ?:
				bch2_trans_commit(&trans, NULL, NULL,
						       BTREE_INSERT_NOWAIT|
						       BTREE_INSERT_NOFAIL);
			if (commit_err == -EINTR) {
				commit_err = 0;
				continue;
			}
		}

		bch2_btree_iter_advance(&iter);
	}
	bch2_trans_iter_exit(&trans, &iter);

	bch2_trans_exit(&trans);
	bch2_bkey_buf_exit(&sk, c);

	return ret;
}

int bch2_gc_gens(struct bch_fs *c)
{
	struct bch_dev *ca;
	struct bucket_array *buckets;
	struct bucket *g;
	unsigned i;
	int ret;

	/*
	 * Ideally we would be using state_lock and not gc_lock here, but that
	 * introduces a deadlock in the RO path - we currently take the state
	 * lock at the start of going RO, thus the gc thread may get stuck:
	 */
	down_read(&c->gc_lock);

	for_each_member_device(ca, c, i) {
		down_read(&ca->bucket_lock);
		buckets = bucket_array(ca);

		for_each_bucket(g, buckets)
			g->gc_gen = g->mark.gen;
		up_read(&ca->bucket_lock);
	}

	for (i = 0; i < BTREE_ID_NR; i++)
		if ((1 << i) & BTREE_ID_HAS_PTRS) {
			c->gc_gens_btree = i;
			c->gc_gens_pos = POS_MIN;
			ret = bch2_gc_btree_gens(c, i);
			if (ret) {
				bch_err(c, "error recalculating oldest_gen: %i", ret);
				goto err;
			}
		}

	for_each_member_device(ca, c, i) {
		down_read(&ca->bucket_lock);
		buckets = bucket_array(ca);

		for_each_bucket(g, buckets)
			g->oldest_gen = g->gc_gen;
		up_read(&ca->bucket_lock);
	}

	c->gc_gens_btree	= 0;
	c->gc_gens_pos		= POS_MIN;

	c->gc_count++;
err:
	up_read(&c->gc_lock);
	return ret;
}

static int bch2_gc_thread(void *arg)
{
	struct bch_fs *c = arg;
	struct io_clock *clock = &c->io_clock[WRITE];
	unsigned long last = atomic64_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 (atomic64_read(&clock->now) >= next)
					break;

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

			try_to_freeze();
		}
		__set_current_state(TASK_RUNNING);

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

		/*
		 * Full gc is currently incompatible with btree key cache:
		 */
#if 0
		ret = bch2_gc(c, false, false);
#else
		ret = bch2_gc_gens(c);
#endif
		if (ret < 0)
			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;

	if (c->gc_thread)
		return 0;

	p = kthread_create(bch2_gc_thread, c, "bch-gc/%s", c->name);
	if (IS_ERR(p)) {
		bch_err(c, "error creating gc thread: %li", PTR_ERR(p));
		return PTR_ERR(p);
	}

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