summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_gc.c
blob: 532248778534e29f86e0ba65776c6a852c7737c0 (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
/*
 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
 * Copyright (C) 2014 Datera Inc.
 */

#include "bcache.h"
#include "alloc.h"
#include "bkey_methods.h"
#include "btree_locking.h"
#include "btree_update.h"
#include "btree_io.h"
#include "btree_gc.h"
#include "buckets.h"
#include "clock.h"
#include "debug.h"
#include "error.h"
#include "extents.h"
#include "journal.h"
#include "keylist.h"
#include "move.h"
#include "writeback.h"

#include <linux/slab.h>
#include <linux/bitops.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/rcupdate.h>
#include <linux/delay.h>
#include <trace/events/bcache.h>

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

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

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

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

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

	cache_set_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->level) {
		l = &r->l[b->level - 1];

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

		cache_set_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->btree_id,
						     b->data->max_key);
	}
}

u8 bch_btree_key_recalc_oldest_gen(struct cache_set *c, struct bkey_s_c k)
{
	const struct bch_extent_ptr *ptr;
	struct cache *ca;
	u8 max_stale = 0;

	if (bkey_extent_is_data(k.k)) {
		struct bkey_s_c_extent e = bkey_s_c_to_extent(k);

		rcu_read_lock();

		extent_for_each_online_device(c, e, ptr, ca) {
			struct bucket *g = PTR_BUCKET(ca, ptr);

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

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

		rcu_read_unlock();
	}

	return max_stale;
}

/*
 * For runtime mark and sweep:
 */
u8 __bch_btree_mark_key(struct cache_set *c, enum bkey_type type,
			struct bkey_s_c k)
{
	switch (type) {
	case BKEY_TYPE_BTREE:
		bch_gc_mark_key(c, k, c->sb.btree_node_size, true);
		return 0;
	case BKEY_TYPE_EXTENTS:
		bch_gc_mark_key(c, k, k.k->size, false);
		return bch_btree_key_recalc_oldest_gen(c, k);
	default:
		BUG();
	}
}

static u8 btree_mark_key(struct cache_set *c, struct btree *b,
			 struct bkey_s_c k)
{
	return __bch_btree_mark_key(c, btree_node_type(b), k);
}

static bool btree_gc_mark_node(struct cache_set *c, struct btree *b)
{
	if (btree_node_has_ptrs(b)) {
		struct btree_node_iter iter;
		struct bkey unpacked;
		struct bkey_s_c k;
		u8 stale = 0;

		for_each_btree_node_key_unpack(&b->keys, k, &iter, &unpacked) {
			bkey_debugcheck(c, b, k);
			stale = max(stale, btree_mark_key(c, b, k));
		}

		if (btree_gc_rewrite_disabled(c))
			return false;

		if (stale > 10)
			return true;
	}

	if (btree_gc_always_rewrite(c))
		return true;

	return false;
}

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

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

static int bch_gc_btree(struct cache_set *c, enum btree_id btree_id)
{
	struct btree_iter iter;
	struct btree *b;
	bool should_rewrite;
	struct range_checks r;
	int ret;

	btree_node_range_checks_init(&r);

	for_each_btree_node(&iter, c, btree_id, POS_MIN, b) {
		btree_node_range_checks(c, b, &r);

		bch_verify_btree_nr_keys(&b->keys);

		should_rewrite = btree_gc_mark_node(c, b);

		gc_pos_set(c, gc_pos_btree_node(b));

		if (should_rewrite)
			bch_btree_node_rewrite(&iter, b, NULL);

		bch_btree_iter_cond_resched(&iter);
	}
	ret = bch_btree_iter_unlock(&iter);
	if (ret)
		return ret;

	mutex_lock(&c->btree_root_lock);

	b = c->btree_roots[btree_id].b;
	__bch_btree_mark_key(c, BKEY_TYPE_BTREE, bkey_i_to_s_c(&b->key));
	gc_pos_set(c, gc_pos_btree_root(b->btree_id));

	mutex_unlock(&c->btree_root_lock);
	return 0;
}

static void bch_mark_allocator_buckets(struct cache_set *c)
{
	struct cache *ca;
	struct open_bucket *ob;
	size_t i, j, iter;
	unsigned ci;

	for_each_cache(ca, c, ci) {
		spin_lock(&ca->freelist_lock);

		fifo_for_each_entry(i, &ca->free_inc, iter)
			bch_mark_alloc_bucket(ca, &ca->buckets[i]);

		for (j = 0; j < RESERVE_NR; j++)
			fifo_for_each_entry(i, &ca->free[j], iter)
				bch_mark_alloc_bucket(ca, &ca->buckets[i]);

		spin_unlock(&ca->freelist_lock);
	}

	for (ob = c->open_buckets;
	     ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
	     ob++) {
		const struct bch_extent_ptr *ptr;

		mutex_lock(&ob->lock);
		rcu_read_lock();
		open_bucket_for_each_online_device(c, ob, ptr, ca)
			bch_mark_alloc_bucket(ca, PTR_BUCKET(ca, ptr));
		rcu_read_unlock();
		mutex_unlock(&ob->lock);
	}
}

/*
 * Mark non btree metadata - prios, journal
 */
static void bch_mark_metadata(struct cache_set *c)
{
	struct cache *ca;
	unsigned i;

	for_each_cache(ca, c, i) {
		unsigned j;
		u64 *i;

		for (j = 0; j < bch_nr_journal_buckets(ca->disk_sb.sb); j++)
			bch_mark_metadata_bucket(ca,
				&ca->buckets[journal_bucket(ca->disk_sb.sb, j)],
				true);

		spin_lock(&ca->prio_buckets_lock);

		for (i = ca->prio_buckets;
		     i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
			bch_mark_metadata_bucket(ca, &ca->buckets[*i], true);

		spin_unlock(&ca->prio_buckets_lock);
	}
}

/* Also see bch_pending_btree_node_free_insert_done() */
static void bch_mark_pending_btree_node_frees(struct cache_set *c)
{
	struct bucket_stats_cache_set stats = { 0 };
	struct btree_interior_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)
			__bch_gc_mark_key(c, bkey_i_to_s_c(&d->key),
					  c->sb.btree_node_size, true,
					  &stats);
	/*
	 * Don't apply stats - pending deletes aren't tracked in
	 * cache_set_stats:
	 */

	mutex_unlock(&c->btree_interior_update_lock);
}

static void bch_mark_scan_keylists(struct cache_set *c)
{
	struct scan_keylist *kl;

	mutex_lock(&c->gc_scan_keylist_lock);

	/* What the goddamn fuck? */
	list_for_each_entry(kl, &c->gc_scan_keylists, mark_list) {
		if (kl->owner == NULL)
			bch_keylist_recalc_oldest_gens(c, kl);
		else
			bch_queue_recalc_oldest_gens(c, kl->owner);
	}

	mutex_unlock(&c->gc_scan_keylist_lock);
}

/**
 * bch_gc - recompute bucket marks and oldest_gen, rewrite btree nodes
 */
void bch_gc(struct cache_set *c)
{
	struct cache *ca;
	struct bucket *g;
	u64 start_time = local_clock();
	unsigned i;
	int cpu;

	/*
	 * 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
	 */

	if (test_bit(CACHE_SET_GC_FAILURE, &c->flags))
		return;

	trace_bcache_gc_start(c);

	/*
	 * Do this before taking gc_lock - bch_disk_reservation_get() blocks on
	 * gc_lock if sectors_available goes to 0:
	 */
	bch_recalc_sectors_available(c);

	down_write(&c->gc_lock);

	lg_global_lock(&c->bucket_stats_lock);

	/*
	 * Indicates to buckets code that gc is now in progress - done under
	 * bucket_stats_lock to avoid racing with bch_mark_key():
	 */
	__gc_pos_set(c, GC_POS_MIN);

	/* Save a copy of the existing bucket stats while we recompute them: */
	for_each_cache(ca, c, i) {
		ca->bucket_stats_cached = __bch_bucket_stats_read_cache(ca);
		for_each_possible_cpu(cpu) {
			struct bucket_stats_cache *p =
				per_cpu_ptr(ca->bucket_stats_percpu, cpu);
			memset(p, 0, sizeof(*p));
		}
	}

	c->bucket_stats_cached = __bch_bucket_stats_read_cache_set(c);
	for_each_possible_cpu(cpu) {
		struct bucket_stats_cache_set *p =
			per_cpu_ptr(c->bucket_stats_percpu, cpu);

		memset(p->s, 0, sizeof(p->s));
		p->persistent_reserved = 0;
	}

	lg_global_unlock(&c->bucket_stats_lock);

	/* Clear bucket marks: */
	for_each_cache(ca, c, i)
		for_each_bucket(g, ca) {
			g->oldest_gen	= ca->bucket_gens[g - ca->buckets];
			atomic_set((atomic_t *) &g->mark.counter, 0);
		}

	/* Walk allocator's references: */
	bch_mark_allocator_buckets(c);

	/* Walk btree: */
	while (c->gc_pos.phase < (int) BTREE_ID_NR) {
		int ret = c->btree_roots[c->gc_pos.phase].b
			? bch_gc_btree(c, (int) c->gc_pos.phase)
			: 0;

		if (ret) {
			bch_err(c, "btree gc failed: %d", ret);
			set_bit(CACHE_SET_GC_FAILURE, &c->flags);
			up_write(&c->gc_lock);
			return;
		}

		gc_pos_set(c, gc_phase(c->gc_pos.phase + 1));
	}

	bch_mark_metadata(c);
	bch_mark_pending_btree_node_frees(c);
	bch_writeback_recalc_oldest_gens(c);
	bch_mark_scan_keylists(c);

	for_each_cache(ca, c, i)
		atomic_long_set(&ca->saturated_count, 0);

	if (1) {
		struct bucket_stats_cache_set old = c->bucket_stats_cached;
		struct bucket_stats_cache_set new =
			__bch_bucket_stats_read_cache_set(c);

		pr_info("\n"
			"before: meta %llu dirty %llu reserved %llu\n"
			"after:  meta %llu dirty %llu reserved %llu\n",
			old.sectors_meta,
			old.sectors_dirty,
			old.sectors_persistent_reserved,
			new.sectors_meta,
			new.sectors_dirty,
			new.sectors_persistent_reserved);
	}

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

	up_write(&c->gc_lock);
	trace_bcache_gc_end(c);
	bch_time_stats_update(&c->btree_gc_time, start_time);
}

/* Btree coalescing */

static void recalc_packed_keys(struct btree *b)
{
	struct btree_node_iter iter;
	struct bkey_packed *k;

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

	for_each_btree_node_key(&b->keys, k, &iter)
		btree_keys_account_key_add(&b->keys.nr, k);
}

static void bch_coalesce_nodes(struct btree *old_nodes[GC_MERGE_NODES],
			       struct btree_iter *iter)
{
	struct btree *parent = iter->nodes[old_nodes[0]->level + 1];
	struct cache_set *c = iter->c;
	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_interior_update *as;
	struct btree_reserve *res;
	struct keylist keylist;
	struct bkey_format_state format_state;
	struct bkey_format new_format;

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

	/* Count keys that are not deleted */
	for (i = 0; i < GC_MERGE_NODES && old_nodes[i]; i++)
		u64s += old_nodes[i]->keys.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 ||
	    __set_blocks(old_nodes[0]->data,
			 DIV_ROUND_UP(u64s, nr_old_nodes - 1),
			 block_bytes(c)) > blocks)
		return;

	res = bch_btree_reserve_get(c, parent, nr_old_nodes, false, NULL);
	if (IS_ERR(res))
		return;

	if (bch_keylist_realloc(&keylist,
			(BKEY_U64s + BKEY_EXTENT_U64s_MAX) * nr_old_nodes)) {
		trace_bcache_btree_gc_coalesce_fail(c);
		goto out;
	}

	trace_bcache_btree_gc_coalesce(parent, nr_old_nodes);

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

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

	new_format = bch_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 (!bch_btree_node_format_fits(old_nodes[i], &new_format)) {
			trace_bcache_btree_gc_coalesce_fail(c);
			goto out;
		}

	as = __bch_btree_interior_update_alloc(old_nodes, nr_old_nodes, iter);

	for (i = 0; i < nr_old_nodes; i++)
		bch_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] = __btree_node_alloc_replacement(c, old_nodes[i],
							      new_format, res);

	/*
	 * 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 < bset_bkey_last(s2) &&
		     __set_blocks(n1->data, le16_to_cpu(s1->u64s) + u64s + k->u64s,
				  block_bytes(c)) <= blocks;
		     k = bkey_next(k)) {
			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(bset_bkey_last(s1),
			       s2->start,
			       le16_to_cpu(s2->u64s) * sizeof(u64));
			le16_add_cpu(&s1->u64s, le16_to_cpu(s2->u64s));

			six_unlock_write(&n2->lock);
			bch_btree_node_free_never_inserted(c, n2);
			six_unlock_intent(&n2->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_key(&n1->keys.format, last).p;

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

			memcpy(bset_bkey_last(s1),
			       s2->start,
			       u64s * sizeof(u64));
			le16_add_cpu(&s1->u64s, u64s);

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

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

		recalc_packed_keys(n);
		six_unlock_write(&n->lock);
		bch_btree_node_write(n, &as->cl, NULL);
	}

	/*
	 * 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;

		bch_btree_node_free_start(c, as, old_nodes[i]);

		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;
		bch_keylist_add_in_order(&keylist, &delete);
next:
		i = i;
	}

	/*
	 * Keys for the new nodes get inserted: bch_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++)
		bch_keylist_add_in_order(&keylist, &new_nodes[i]->key);

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

	BUG_ON(!bch_keylist_empty(&keylist));

	BUG_ON(iter->nodes[old_nodes[0]->level] != old_nodes[0]);

	BUG_ON(!bch_btree_iter_node_replace(iter, new_nodes[0]));

	for (i = 0; i < nr_new_nodes; i++)
		btree_open_bucket_put(c, new_nodes[i]);

	/* Free the old nodes and update our sliding window */
	for (i = 0; i < nr_old_nodes; i++) {
		bch_btree_node_free_inmem(iter, old_nodes[i]);
		six_unlock_intent(&old_nodes[i]->lock);

		/*
		 * 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;
			if (new_nodes[i])
				six_unlock_intent(&new_nodes[i]->lock);
		}
	}
out:
	bch_keylist_free(&keylist);
	bch_btree_reserve_put(c, res);
}

static int bch_coalesce_btree(struct cache_set *c, enum btree_id btree_id)
{
	struct btree_iter iter;
	struct btree *b;
	unsigned i;

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

	/*
	 * 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(&iter, c, btree_id, POS_MIN, b, U8_MAX) {
		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]->lock, lock_seq[i]))
				break;

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

		bch_coalesce_nodes(merge, &iter);

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

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

		if (test_bit(CACHE_SET_GC_STOPPING, &c->flags)) {
			bch_btree_iter_unlock(&iter);
			return -ESHUTDOWN;
		}

		bch_btree_iter_cond_resched(&iter);

		/*
		 * 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 (iter.nodes[iter.level + 1] &&
		    !btree_node_intent_locked(&iter, iter.level + 1))
			memset(merge + 1, 0,
			       (GC_MERGE_NODES - 1) * sizeof(merge[0]));
	}
	return bch_btree_iter_unlock(&iter);
}

/**
 * bch_coalesce - coalesce adjacent nodes with low occupancy
 */
static void bch_coalesce(struct cache_set *c)
{
	u64 start_time = local_clock();
	enum btree_id id;

	if (btree_gc_coalesce_disabled(c))
		return;

	if (test_bit(CACHE_SET_GC_FAILURE, &c->flags))
		return;

	trace_bcache_gc_coalesce_start(c);

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

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

	bch_time_stats_update(&c->btree_coalesce_time, start_time);

	trace_bcache_gc_coalesce_end(c);
}

static int bch_gc_thread(void *arg)
{
	struct cache_set *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);
	struct cache *ca;
	unsigned i;

	set_freezable();

	while (1) {
		unsigned long next = last + c->exposed_capacity / 16;

		while (atomic_long_read(&clock->now) < next) {
			set_current_state(TASK_INTERRUPTIBLE);

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

			if (atomic_read(&c->kick_gc) != last_kick) {
				__set_current_state(TASK_RUNNING);
				break;
			}

			bch_io_clock_schedule_timeout(clock, next);
			try_to_freeze();
		}

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

		/*
		 * avoid racing with sysfs trigger_gc - gc gets confused if it
		 * runs concurrently with coalescing
		 */
		mutex_lock(&c->trigger_gc_lock);
		bch_gc(c);

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

		bch_coalesce(c);
		mutex_unlock(&c->trigger_gc_lock);

		debug_check_no_locks_held();
	}

	return 0;
}

void bch_gc_thread_stop(struct cache_set *c)
{
	set_bit(CACHE_SET_GC_STOPPING, &c->flags);

	if (!IS_ERR_OR_NULL(c->gc_thread))
		kthread_stop(c->gc_thread);
}

int bch_gc_thread_start(struct cache_set *c)
{
	clear_bit(CACHE_SET_GC_STOPPING, &c->flags);

	c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
	if (IS_ERR(c->gc_thread))
		return PTR_ERR(c->gc_thread);

	wake_up_process(c->gc_thread);
	return 0;
}

/* Initial GC computes bucket marks during startup */

static void bch_initial_gc_btree(struct cache_set *c, enum btree_id id)
{
	struct btree_iter iter;
	struct btree *b;
	struct range_checks r;

	btree_node_range_checks_init(&r);

	if (!c->btree_roots[id].b)
		return;

	for_each_btree_node(&iter, c, id, POS_MIN, b) {
		btree_node_range_checks(c, b, &r);

		if (btree_node_has_ptrs(b)) {
			struct btree_node_iter node_iter;
			struct bkey unpacked;
			struct bkey_s_c k;

			for_each_btree_node_key_unpack(&b->keys, k,
						       &node_iter, &unpacked)
				btree_mark_key(c, b, k);
		}

		bch_btree_iter_cond_resched(&iter);
	}

	bch_btree_iter_unlock(&iter);

	__bch_btree_mark_key(c, BKEY_TYPE_BTREE,
			     bkey_i_to_s_c(&c->btree_roots[id].b->key));
}

int bch_initial_gc(struct cache_set *c, struct list_head *journal)
{
	enum btree_id id;

	if (journal) {
		for (id = 0; id < BTREE_ID_NR; id++)
			bch_initial_gc_btree(c, id);

		bch_journal_mark(c, journal);
	}

	bch_mark_metadata(c);

	gc_pos_set(c, gc_phase(GC_PHASE_DONE));
	set_bit(CACHE_SET_INITIAL_GC_DONE, &c->flags);

	return 0;
}