summaryrefslogtreecommitdiff
path: root/drivers/md/bcache/btree_iter.c
blob: 343b44439f59135b65040a3f8aa9aa3d37d3482b (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

#include "bcache.h"
#include "bkey_methods.h"
#include "btree_cache.h"
#include "btree_iter.h"
#include "btree_locking.h"
#include "debug.h"
#include "extents.h"
#include "snapshot.h"

#include <trace/events/bcache.h>

#define BTREE_ITER_NOT_END	((struct btree *) 1)

static inline bool is_btree_node(struct btree_iter_state *_iter, unsigned l)
{
	struct btree *b = _iter->l[l].node;

	return b && b != BTREE_ITER_NOT_END;
}

/* Btree node locking: */

/*
 * Updates the saved lock sequence number, so that bch_btree_node_relock() will
 * succeed:
 */
void btree_node_unlock_write(struct btree *b, struct btree_iter *iter)
{
	struct btree_iter_state *_iter = iter_s(iter);
	struct btree_iter *linked;

	EBUG_ON(!btree_iter_has_node(_iter, b));
	EBUG_ON(_iter->l[b->level].lock_seq + 1 != b->lock.state.seq);

	for_each_linked_btree_node(iter, b, linked)
		iter_s(linked)->l[b->level].lock_seq += 2;

	_iter->l[b->level].lock_seq += 2;

	six_unlock_write(&b->lock);
}

void btree_node_lock_write(struct btree *b, struct btree_iter *iter)
{
	struct btree_iter_state *_iter = iter_s(iter);
	struct btree_iter *linked;
	unsigned readers = 0;

	EBUG_ON(!btree_iter_has_node(_iter, b));
	EBUG_ON(_iter->l[b->level].lock_seq != b->lock.state.seq);

	if (six_trylock_write(&b->lock))
		return;

	for_each_linked_btree_iter(iter, linked) {
		struct btree_iter_state *_linked = iter_s(linked);

		if (btree_iter_has_node(_linked, b) &&
		    btree_node_read_locked(_linked, b->level))
			readers++;
	}

	if (likely(!readers)) {
		six_lock_write(&b->lock);
	} else {
		/*
		 * Must drop our read locks before calling six_lock_write() -
		 * six_unlock() won't do wakeups until the reader count
		 * goes to 0, and it's safe because we have the node intent
		 * locked:
		 */
		atomic64_sub(__SIX_VAL(read_lock, readers),
			     &b->lock.state.counter);
		six_lock_write(&b->lock);
		atomic64_add(__SIX_VAL(read_lock, readers),
			     &b->lock.state.counter);
	}
}

/* versions that allow iter to be null: */
void __btree_node_unlock_write(struct btree *b, struct btree_iter *iter)
{
	if (likely(iter))
		btree_node_unlock_write(b, iter);
	else
		six_unlock_write(&b->lock);
}

void __btree_node_lock_write(struct btree *b, struct btree_iter *iter)
{
	if (likely(iter))
		btree_node_lock_write(b, iter);
	else
		six_lock_write(&b->lock);
}

static bool btree_lock_upgrade(struct btree_iter *iter,
			       struct btree_iter_state *_iter,
			       unsigned level)
{
	struct btree_iter *linked;
	struct btree *b = _iter->l[level].node;

	if (btree_node_intent_locked(_iter, level))
		return true;

	if (!is_btree_node(_iter, level))
		return false;

	if (btree_node_locked(_iter, level)
	    ? six_trylock_convert(&b->lock, SIX_LOCK_read, SIX_LOCK_intent)
	    : six_relock_intent(&b->lock, _iter->l[level].lock_seq))
		goto success;

	for_each_linked_btree_iter(iter, linked)
		if (iter_s(linked)->l[level].node == b &&
		    btree_node_intent_locked(iter_s(linked), level) &&
		    _iter->l[level].lock_seq == b->lock.state.seq) {
			btree_node_unlock(_iter, level);
			six_lock_increment(&b->lock, SIX_LOCK_intent);
			goto success;
		}


	trace_bcache_btree_upgrade_lock_fail(b, iter);
	return false;
success:
	mark_btree_node_intent_locked(_iter, level);
	trace_bcache_btree_upgrade_lock(b, iter);
	return true;
}

/* Btree iterator locking: */

static bool __btree_iter_upgrade(struct btree_iter *iter,
				 struct btree_iter_state *_iter)
{
	int i;

	for (i = iter->level;
	     i < min_t(int, iter->locks_want, BTREE_MAX_DEPTH);
	     i++)
		if (_iter->l[i].node && !btree_lock_upgrade(iter, _iter, i)) {
			do {
				btree_node_unlock(_iter, i);

				/*
				 * Make sure bch_btree_node_relock() in
				 * btree_iter_traverse() fails, so that we keep
				 * going up and get all the intent locks we need
				 */
				_iter->l[i].lock_seq--;
			} while (--i >= 0);

			return false;
		}

	return true;
}

bool bch_btree_iter_upgrade(struct btree_iter *iter)
{
	return __btree_iter_upgrade(iter, iter_s(iter));
}

void __btree_iter_unlock(struct btree_iter_state *_iter)
{
	while (_iter->nodes_locked)
		btree_node_unlock(_iter, __ffs(_iter->nodes_locked));
}

int bch_btree_iter_unlock(struct btree_iter *iter)
{
	__btree_iter_unlock(iter_s(iter));

	return iter->error;
}

bool bch_btree_node_relock(struct btree_iter *iter,
			   struct btree_iter_state *_iter,
			   unsigned level)
{
	struct btree_iter *linked;
	struct btree *b = _iter->l[level].node;
	enum six_lock_type type = btree_lock_want(iter, level);

	if (btree_node_locked(_iter, level))
		return true;

	if (race_fault())
		return false;

	if (is_btree_node(_iter, level) &&
	    six_relock_type(&b->lock, _iter->l[level].lock_seq, type)) {
		mark_btree_node_locked(_iter, level, type);
		return true;
	}

	for_each_linked_btree_iter(iter, linked)
		if (iter_s(linked)->l[level].node == b &&
		    btree_node_locked_type(iter_s(linked), level) == type &&
		    _iter->l[level].lock_seq == b->lock.state.seq) {
			six_lock_increment(&b->lock, type);
			mark_btree_node_locked(_iter, level, type);
			return true;
		}

	return false;
}

/* Btree iterator: */

#ifdef CONFIG_BCACHE_DEBUG

static void __bch_btree_iter_verify(struct btree_node_iter *iter,
				    struct btree *b,
				    struct bpos pos,
				    bool strictly_greater)
{
	const struct bkey_format *f = &b->keys.format;
	struct bset_tree *t;
	struct bkey_packed *k;

	bch_btree_node_iter_verify(iter, &b->keys);

	for (t = b->keys.set; t <= b->keys.set + b->keys.nsets; t++) {
		k = bkey_prev(t,
			bch_btree_node_iter_bset_pos(iter, &b->keys, t->data) ?:
			bset_bkey_last(t->data));

		/*
		 * For interior nodes, the iterator will have skipped past
		 * deleted keys:
		 */
		if (b->level)
			while (k && bkey_deleted(k))
				k = bkey_prev(t, k);

		BUG_ON(k && btree_iter_pos_cmp_packed(f, pos, k,
						      strictly_greater));
	}

	k = bch_btree_node_iter_peek_all(iter, &b->keys);
	BUG_ON(k && !btree_iter_pos_cmp_packed(f, pos, k, strictly_greater));
}

void bch_btree_iter_verify(struct btree_iter *iter, struct btree *b)
{
	struct btree_iter *linked;

	if (iter->l[b->level].node == b)
		__bch_btree_iter_verify(&iter->l[b->level].node_iter,
					b, iter->pos,
					iter->is_extents);

	for_each_linked_btree_node(iter, b, linked)
		__bch_btree_iter_verify(&linked->l[b->level].node_iter,
					b, linked->pos,
					linked->is_extents);
}

#endif

static void __bch_btree_node_iter_fix(struct btree_iter *iter,
				      struct btree_keys *b,
				      struct btree_node_iter *node_iter,
				      struct bkey_packed *where,
				      bool overwrote)
{
	struct bkey_format *f = &b->format;
	struct bset *i = bch_bkey_to_bset(b, where)->data;
	const struct bkey_packed *end = bset_bkey_last(i);
	struct btree_node_iter_set *set;
	unsigned shift = overwrote ? 0 : where->u64s;
	unsigned offset = __btree_node_key_to_offset(b, where);
	unsigned old_end = __btree_node_key_to_offset(b, end) - shift;
	bool iter_pos_before_new = btree_iter_pos_cmp_packed(f,
				iter->pos, where, iter->is_extents);

	btree_node_iter_for_each(node_iter, set)
		if (set->end == old_end) {
			set->end += shift;

			/*
			 * When we inserted at @where, the key we inserted - the
			 * new key at @where - compared strictly less than the
			 * key previously at @where (which is now the next key
			 * after the key we inserted).
			 *
			 * However, it is not necessarily true that if the
			 * iterator's position is less than the key we inserted
			 * it was <= the key previously at where - transitivity
			 * doesn't hold here - because the key previously at
			 * @where might have been a deleted key that the
			 * iterator had skipped past.
			 */
			if (set->k >= offset) {
				if (iter_pos_before_new)
					set->k = offset;
				else
					set->k += shift;
			}

			/*
			 * Resort iterator if we changed a key it points to:
			 *
			 * Do this before checking if we're removing a key from
			 * the iterator:
			 */
			if (set->k == offset)
				bch_btree_node_iter_sort(node_iter, b);
			goto check_remove;
		}

	/* didn't find the bset in the iterator - might have to readd it: */
	if (iter_pos_before_new)
		bch_btree_node_iter_push(node_iter, b, where, end);
check_remove:
	if (!iter_pos_before_new &&
	    bch_btree_node_iter_peek_all(node_iter, b) == where)
		bch_btree_node_iter_advance(node_iter, b);
}

void bch_btree_node_iter_fix(struct btree_iter *iter,
			     struct btree *b,
			     struct btree_node_iter *node_iter,
			     struct bkey_packed *k,
			     bool overwrote)
{
	struct btree_iter *linked;

	if (node_iter != &iter->l[b->level].node_iter)
		__bch_btree_node_iter_fix(iter, &b->keys, node_iter,
					  k, overwrote);

	if (iter->l[b->level].node == b)
		__bch_btree_node_iter_fix(iter, &b->keys,
					  &iter->l[b->level].node_iter,
					  k, overwrote);

	for_each_linked_btree_node(iter, b, linked)
		__bch_btree_node_iter_fix(linked, &b->keys,
					  &linked->l[b->level].node_iter,
					  k, overwrote);
	bch_btree_iter_verify(iter, b);
}

/* peek_all() doesn't skip deleted keys */
static inline struct bkey_s_c __btree_iter_peek_all(struct btree_iter *iter,
						    struct btree_iter_level *l)
{
	const struct bkey_format *f = &l->node->keys.format;
	struct bkey_packed *k =
		bch_btree_node_iter_peek_all(&l->node_iter, &l->node->keys);
	struct bkey_s_c ret;

	if (!k)
		return bkey_s_c_null;

	ret = bkey_disassemble(f, k, &iter->k);

	if (debug_check_bkeys(iter->c))
		bkey_debugcheck(iter->c, l->node, ret);

	return ret;
}

static inline struct bkey_s_c __btree_iter_peek(struct btree_iter *iter,
						struct btree_iter_level *l)
{
	const struct bkey_format *f = &l->node->keys.format;
	struct bkey_packed *k =
		bch_btree_node_iter_peek(&l->node_iter, &l->node->keys);
	struct bkey_s_c ret;

	if (!k)
		return bkey_s_c_null;

	ret = bkey_disassemble(f, k, &iter->k);

	if (debug_check_bkeys(iter->c))
		bkey_debugcheck(iter->c, l->node, ret);

	return ret;
}

static inline void __btree_iter_advance(struct btree_iter_level *l)
{
	bch_btree_node_iter_advance(&l->node_iter, &l->node->keys);
}

static inline void btree_iter_node_iter_init(struct btree_iter *iter,
					     struct btree_iter_level *l,
					     struct bpos pos)
{
	bch_btree_node_iter_init(&l->node_iter, &l->node->keys,
				 pos, iter->is_extents);
}

static inline void btree_iter_node_set(struct btree_iter *iter,
				       struct btree_iter_state *_iter,
				       struct btree *b,
				       struct bpos pos)
{
	struct btree_iter_level *l = &_iter->l[b->level];

	BUG_ON(b->lock.state.seq & 1);

	l->lock_seq = b->lock.state.seq;
	l->node = b;

	btree_iter_node_iter_init(iter, l, pos);
}

static bool btree_iter_pos_in_node(struct btree_iter *iter, struct btree *b)
{
	return iter->btree_id == b->btree_id &&
		bkey_cmp(iter->pos, b->data->min_key) >= 0 &&
		btree_iter_pos_cmp(iter->pos, &b->key.k, iter->is_extents);
}

/*
 * A btree node is being replaced - update the iterator to point to the new
 * node:
 */
bool bch_btree_iter_node_replace(struct btree_iter *iter, struct btree *b)
{
	struct btree_iter *linked;

	for_each_linked_btree_iter(iter, linked)
		if (btree_iter_pos_in_node(linked, b)) {
			/*
			 * bch_btree_iter_node_drop() has already been called -
			 * the old node we're replacing has already been
			 * unlocked and the pointer invalidated
			 */
			EBUG_ON(btree_node_locked(iter_s(linked), b->level));

			/*
			 * If @linked wants this node read locked, we don't want
			 * to actually take the read lock now because it's not
			 * legal to hold read locks on other nodes while we take
			 * write locks, so the journal can make forward
			 * progress...
			 *
			 * Instead, btree_iter_node_set() sets things up so
			 * bch_btree_node_relock() will succeed:
			 */

			if (btree_want_intent(linked, b->level)) {
				six_lock_increment(&b->lock, SIX_LOCK_intent);
				mark_btree_node_intent_locked(iter_s(linked), b->level);
			}

			btree_iter_node_set(linked, iter_s(linked),
					    b, linked->pos);
		}

	if (!btree_iter_pos_in_node(iter, b)) {
		six_unlock_intent(&b->lock);
		return false;
	}

	mark_btree_node_intent_locked(iter_s(iter), b->level);
	btree_iter_node_set(iter, iter_s(iter), b, iter->pos);
	return true;
}

void bch_btree_iter_node_drop_linked(struct btree_iter *iter, struct btree *b)
{
	struct btree_iter *linked;
	unsigned level = b->level;

	for_each_linked_btree_iter(iter, linked) {
		struct btree_iter_state *_linked = iter_s(linked);

		if (_linked->l[level].node == b) {
			btree_node_unlock(_linked, level);
			_linked->l[level].node = BTREE_ITER_NOT_END;
		}
	}
}

void bch_btree_iter_node_drop(struct btree_iter *iter, struct btree *b)
{
	struct btree_iter_state *_iter = iter_s(iter);
	unsigned level = b->level;

	if (_iter->l[level].node == b) {
		BUG_ON(b->lock.state.intent_lock != 1);
		btree_node_unlock(_iter, level);
		_iter->l[level].node = BTREE_ITER_NOT_END;
	}
}

/*
 * A btree node has been modified in such a way as to invalidate iterators - fix
 * them:
 */
void bch_btree_iter_reinit_node(struct btree_iter *iter, struct btree *b)
{
	struct btree_iter *linked;

	for_each_linked_btree_node(iter, b, linked)
		btree_iter_node_iter_init(linked,
					  &iter_s(linked)->l[b->level],
					  linked->pos);

	btree_iter_node_iter_init(iter,
				  &iter_s(iter)->l[b->level],
				  iter->pos);
}

static void btree_iter_verify_locking(struct btree_iter *iter,
				      struct btree_iter_state *_iter,
				      unsigned level)
{
#ifdef CONFIG_BCACHE_DEBUG
	struct btree_iter *linked;

	if (!btree_want_intent(iter, level))
		return;

	/*
	 * Can't hold _any_ read locks (including in linked iterators) when
	 * taking intent locks, that leads to a fun deadlock involving write
	 * locks and journal reservations
	 *
	 * We could conceivably drop read locks, then retake them and if
	 * retaking fails then return -EINTR... but, let's keep things simple
	 * for now:
	 */
	BUG_ON(_iter->nodes_locked != _iter->nodes_intent_locked);

	for_each_linked_btree_iter(iter, linked)
		BUG_ON(iter_s(linked)->nodes_locked !=
		       iter_s(linked)->nodes_intent_locked);

	/* Lock ordering: */
	for_each_linked_btree_iter(iter, linked) {
		BUG_ON(iter_s(linked)->nodes_locked &&
		       btree_iter_cmp(linked, iter) > 0);

		BUG_ON(iter_s(linked)->nodes_locked &&
		       linked->btree_id == iter->btree_id &&
		       level > __fls(iter_s(linked)->nodes_locked));
	}
#endif
}

static inline void btree_iter_lock_root(struct btree_iter *iter,
					struct btree_iter_state *_iter,
					struct bpos pos)
{
	struct cache_set *c = iter->c;

	while (1) {
		struct btree *b = c->btree_roots[iter->btree_id].b;
		unsigned level = READ_ONCE(b->level);

		btree_iter_verify_locking(iter, _iter, level);

		if (btree_node_lock(b, iter, level,
				(b != c->btree_roots[iter->btree_id].b))) {
			iter->level = level;
			if (level + 1 < BTREE_MAX_DEPTH)
				_iter->l[level + 1].node = NULL;
			btree_iter_node_set(iter, _iter, b, pos);
			break;
		}
	}
}

static inline int btree_iter_down(struct btree_iter *iter,
				  struct btree_iter_state *_iter,
				  struct bpos pos, struct closure *cl)
{
	struct btree *b;
	struct bkey_s_c k = __btree_iter_peek(iter,
				&_iter->l[iter->level]);
	BKEY_PADDED(k) tmp;

	bkey_reassemble(&tmp.k, k);

	b = bch_btree_node_get(iter, _iter, &tmp.k, iter->level - 1, cl);
	if (unlikely(IS_ERR(b)))
		return PTR_ERR(b);

	btree_iter_verify_locking(iter, _iter, iter->level - 1);

	--iter->level;
	btree_iter_node_set(iter, _iter, b, pos);
	return 0;
}

static void btree_iter_up(struct btree_iter *iter,
			  struct btree_iter_state *_iter)
{
	btree_node_unlock(_iter, iter->level++);
}

/*
 * This is the main state machine for walking down the btree - walks down to a
 * specified depth
 *
 * Returns 0 on success, -EIO on error (error reading in a btree node).
 *
 * On error, caller (peek_node()/peek_key()) must return NULL; the error is
 * stashed in the iterator and returned from bch_btree_iter_unlock().
 */
static int __must_check __bch_btree_iter_traverse(struct btree_iter *iter,
					struct btree_iter_state *_iter,
					unsigned traverse_to,
					struct bpos pos)
{
	struct btree_iter_level *l;

	if (!_iter->l[iter->level].node)
		return 0;

	iter->at_end_of_leaf = false;
retry:
	/*
	 * If the current node isn't locked, go up until we have a locked node
	 * or run out of nodes:
	 */
	while (_iter->l[iter->level].node &&
	       !(is_btree_node(_iter, iter->level) &&
		 bch_btree_node_relock(iter, _iter, iter->level) &&
		 btree_iter_pos_cmp(pos,
				    &_iter->l[iter->level].node->key.k),
				    iter->is_extents))
		btree_iter_up(iter, _iter);

	/*
	 * If we've got a btree node locked (i.e. we aren't about to relock the
	 * root) - advance its node iterator if necessary:
	 */
	if (iter->level < BTREE_MAX_DEPTH &&
	    (l = &_iter->l[iter->level])->node) {
		struct bkey_s_c k;

		while ((k = __btree_iter_peek_all(iter, l)).k &&
		       !btree_iter_pos_cmp(pos, k.k, iter->is_extents))
			__btree_iter_advance(l);
	}

	/*
	 * Note: iter->nodes[iter->level] may be temporarily NULL here - that
	 * would indicate to other code that we got to the end of the btree,
	 * here it indicates that relocking the root failed - it's critical that
	 * btree_iter_lock_root() comes next and that it can't fail
	 */
	while (iter->level > traverse_to)
		if (iter->level < BTREE_MAX_DEPTH &&
		    _iter->l[iter->level].node) {
			struct closure cl;
			int ret;

			closure_init_stack(&cl);

			ret = btree_iter_down(iter, _iter, pos, &cl);
			if (unlikely(ret)) {
				__btree_iter_unlock(_iter);
				closure_sync(&cl);

				/*
				 * We just dropped all our locks - so if we need
				 * intent locks, make sure to get them again:
				 */
				if (ret == -EAGAIN || ret == -EINTR) {
					__btree_iter_upgrade(iter, _iter);
					goto retry;
				}

				iter->error = ret;
				iter->level = BTREE_MAX_DEPTH;
				return ret;
			}
		} else {
			btree_iter_lock_root(iter, _iter, pos);
		}

	return 0;
}

int __must_check bch_btree_iter_traverse(struct btree_iter *iter)
{
	return __bch_btree_iter_traverse(iter, iter_s(iter),
					 iter->level, iter->pos);
}

/* Iterate across nodes (leaf and interior nodes) */

struct btree *bch_btree_iter_peek_node(struct btree_iter *iter)
{
	struct btree_iter_state *_iter = iter_s(iter);
	struct btree *b;
	int ret;

	EBUG_ON(iter->is_extents);

	ret = bch_btree_iter_traverse(iter);
	if (ret)
		return NULL;

	b = _iter->l[iter->level].node;

	EBUG_ON(bkey_cmp(b->key.k.p, iter->pos) < 0);
	iter->pos = b->key.k.p;

	return b;
}

struct btree *bch_btree_iter_next_node(struct btree_iter *iter)
{
	struct btree_iter_state *_iter = iter_s(iter);
	struct btree *b;
	int ret;

	EBUG_ON(iter->is_extents);

	btree_iter_up(iter, _iter);

	if (iter->level >= BTREE_MAX_DEPTH ||
	    !_iter->l[iter->level].node)
		return NULL;

	/* parent node usually won't be locked: redo traversal if necessary */
	ret = bch_btree_iter_traverse(iter);
	if (ret)
		return NULL;

	b = _iter->l[iter->level].node;

	if (bkey_cmp(iter->pos, b->key.k.p) < 0) {
		struct bpos pos = bkey_successor(iter->pos);

		ret = __bch_btree_iter_traverse(iter, iter_s(iter), 0, pos);
		if (ret)
			return NULL;

		b = _iter->l[iter->level].node;
	}

	iter->pos = b->key.k.p;

	return b;
}

/* Iterate across keys (in leaf nodes only) */

void bch_btree_iter_set_pos_same_leaf(struct btree_iter *iter, struct bpos new_pos)
{
	struct btree_keys *b = &iter->nodes[0]->keys;
	struct btree_node_iter *node_iter = &iter->node_iters[0];
	struct bkey_packed *k;

	EBUG_ON(bkey_cmp(new_pos, iter->pos) < 0);
	EBUG_ON(!btree_node_locked(iter, 0));
	EBUG_ON(bkey_cmp(new_pos, iter->nodes[0]->key.k.p) > 0);

	while ((k = bch_btree_node_iter_peek_all(node_iter, b)) &&
	       !btree_iter_pos_cmp_packed(&b->format, new_pos, k,
					  iter->is_extents))
		bch_btree_node_iter_advance(node_iter, b);

	if (!k &&
	    !btree_iter_pos_cmp(new_pos, &iter->nodes[0]->key.k,
				iter->is_extents))
		iter->at_end_of_leaf = true;

	iter->pos = new_pos;
}

void bch_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
{
	EBUG_ON(bkey_cmp(new_pos, iter->pos) < 0);
	iter->pos = new_pos;
}

void bch_btree_iter_advance_pos(struct btree_iter *iter)
{
	bch_btree_iter_set_pos(iter,
		btree_type_successor(iter->btree_id, iter->k.p));
}

/* XXX: expensive */
void bch_btree_iter_rewind(struct btree_iter *iter, struct bpos pos)
{
	struct btree_iter_state *_iter = iter_s(iter);
	struct btree_iter_level *l = &_iter->l[iter->level];

	/* incapable of rewinding across nodes: */
	BUG_ON(bkey_cmp(pos, l->node->data->min_key) < 0);

	iter->pos = pos;

	btree_iter_node_iter_init(iter, l, pos);
}

struct bkey_s_c __bch_btree_iter_peek(struct btree_iter *iter,
				      struct btree_iter_state *_iter)
{
	struct bpos pos = iter->pos;
	struct bkey_s_c k;
	int ret;

	while (1) {
		ret = __bch_btree_iter_traverse(iter, _iter, 0, pos);
		if (unlikely(ret))
			return bkey_s_c_null;

		k = __btree_iter_peek(iter, &_iter->l[0]);
		if (likely(k.k)) {
			/*
			 * iter->pos should always be equal to the key we just
			 * returned - except extents can straddle iter->pos:
			 */
			if (!iter->is_extents ||
			    bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0)
				bch_btree_iter_set_pos(iter, bkey_start_pos(k.k));
			return k;
		}

		pos = btree_iter_leaf(iter)->key.k.p;

		if (!bkey_cmp(pos, POS_MAX))
			return (struct bkey_s_c) { NULL, NULL };

		pos = btree_type_successor(iter->btree_id, pos);
	}

}

struct bkey_s_c bch_btree_iter_peek(struct btree_iter *iter)
{
	return __bch_btree_iter_peek(iter, iter_s(iter));
}

/*
 * iter_s(iter) is the cursor for iter->pos - the key we return
 *
 * iter->pos.snapshot may be different than snapshot->id (but must be an
 * ancestor)
 *
 * if iter->have_alternate != 0, then iter_a(iter) is the cursor for inserting a
 * key at iter.pos, except with iter.pos.snapshot = snapshot->id
 */

struct bkey_s_c bch_btree_iter_peek_snapshot(struct btree_iter *iter,
					     struct snapshot *snapshot)
{
	struct btree_iter_state *_iter		= iter_s(iter);
	struct btree_iter_state *_alternate	= iter_a(iter);
	struct bkey_s_c k;

	if (iter->have_alternate) {
		__btree_iter_unlock(_alternate);
		iter->have_alternate = 0;
	}

	k = bch_btree_iter_peek(iter);
	if (unlikely(!k.k))
		return k;

	if (likely(bch_is_snapshot_ancestor(iter->c,
				snapshot, k.k->p.snapshot)))
		return k;

	/*
	 * keep current position locked, advance until we find a key that is an
	 * ancestor:
	 */
	*_alternate = *_iter;
	_alternate->nodes_locked	= 0;
	_alternate->nodes_intent_locked	= 0;
	iter->have_alternate = 1;

	iter->s_idx ^= 1;
	swap(_iter, _alternate);

	while (1) {
		bch_btree_iter_advance_pos(iter);

		k = bch_btree_iter_peek(iter);
		if (unlikely(!k.k))
			return k;

		if (likely(bch_is_snapshot_ancestor(iter->c,
					snapshot, k.k->p.snapshot)))
			return k;
	}

	return k;
#if 0
	struct bpos pos = iter->pos;
	struct bkey_s_c k;
	int ret;

	while (1) {
		ret = __bch_btree_iter_traverse(iter, 0, pos);
		if (unlikely(ret))
			return bkey_s_c_null;

		k = __btree_iter_peek(iter);
		if (likely(k.k)) {
			/*
			 * iter->pos should always be equal to the key we just
			 * returned - except extents can straddle iter->pos:
			 */
			if (!iter->is_extents ||
			    bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0)
				bch_btree_iter_set_pos(iter, bkey_start_pos(k.k));
			return k;
		}

		pos = btree_iter_leaf(iter)->key.k.p;

		if (!bkey_cmp(pos, POS_MAX))
			return (struct bkey_s_c) { NULL, NULL };

		pos = btree_type_successor(iter->btree_id, pos);
	}
#endif
}

struct bkey_s_c bch_btree_iter_peek_with_holes(struct btree_iter *iter)
{
	struct bkey_s_c k;
	struct bkey n;
	int ret;

	while (1) {
		ret = __bch_btree_iter_traverse(iter, iter_s(iter),
						0, iter->pos);
		if (unlikely(ret))
			return bkey_s_c_null;

		k = iter->is_extents
			? __btree_iter_peek(iter, &iter_s(iter)->l[0])
			: __btree_iter_peek_all(iter, &iter_s(iter)->l[0]);
recheck:
		if (!k.k || bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0) {
			/* hole */
			bkey_init(&n);
			n.p = iter->pos;

			if (iter->is_extents) {
				if (n.p.offset == KEY_OFFSET_MAX) {
					iter->pos = bkey_successor(iter->pos);
					goto recheck;
				}

				if (!k.k)
					k.k = &btree_iter_leaf(iter)->key.k;

				bch_key_resize(&n,
				       min_t(u64, KEY_SIZE_MAX,
					     (k.k->p.inode == n.p.inode
					      ? bkey_start_offset(k.k)
					      : KEY_OFFSET_MAX) -
					     n.p.offset));

				EBUG_ON(!n.size);
			}

			iter->k = n;
			return (struct bkey_s_c) { &iter->k, NULL };
		} else if (!bkey_deleted(k.k)) {
			return k;
		} else {
			__btree_iter_advance(&iter_s(iter)->l[0]);
		}
	}

	EBUG_ON(!iter->error &&
		(iter->btree_id != BTREE_ID_INODES
		 ? bkey_cmp(iter->pos, POS_MAX)
		 : iter->pos.inode != KEY_INODE_MAX));

	return bkey_s_c_null;
}

struct bkey_s_c bch_btree_iter_peek_snapshot_with_holes(struct btree_iter *iter,
							struct snapshot *snapshot)
{
	struct bkey_s_c k;
	struct bkey n;
	int ret;

	while (1) {
		ret = __bch_btree_iter_traverse(iter, iter_s(iter),
						0, iter->pos);
		if (unlikely(ret))
			return bkey_s_c_null;

		k = __btree_iter_peek_all(iter, &iter_s(iter)->l[0]);

		/* hrm */

recheck:
		if (!k.k || bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0) {
			/* hole */
			bkey_init(&n);
			n.p = iter->pos;

			if (iter->is_extents) {
				if (n.p.offset == KEY_OFFSET_MAX) {
					iter->pos = bkey_successor(iter->pos);
					goto recheck;
				}

				if (!k.k)
					k.k = &btree_iter_leaf(iter)->key.k;

				bch_key_resize(&n,
				       min_t(u64, KEY_SIZE_MAX,
					     (k.k->p.inode == n.p.inode
					      ? bkey_start_offset(k.k)
					      : KEY_OFFSET_MAX) -
					     n.p.offset));

				EBUG_ON(!n.size);
			}

			iter->k = n;
			return (struct bkey_s_c) { &iter->k, NULL };
		} else if (!bkey_deleted(k.k)) {
			return k;
		} else {
			__btree_iter_advance(&iter_s(iter)->l[0]);
		}
	}

	EBUG_ON(!iter->error &&
		(iter->btree_id != BTREE_ID_INODES
		 ? bkey_cmp(iter->pos, POS_MAX)
		 : iter->pos.inode != KEY_INODE_MAX));

	return bkey_s_c_null;
}

void __bch_btree_iter_init(struct btree_iter *iter, struct cache_set *c,
			   enum btree_id btree_id, struct bpos pos,
			   int locks_want)
{
	struct btree_iter_state *_iter;

	iter->c				= c;
	iter->next			= iter;
	iter->pos			= pos;

	iter->error			= 0;
	iter->btree_id			= btree_id;
	iter->at_end_of_leaf		= 0;
	iter->is_extents		= btree_id == BTREE_ID_EXTENTS;
	iter->locks_want		= locks_want;
	iter->level			= 0;
	iter->s_idx			= 0;

	_iter				= iter_s(iter);
	_iter->nodes_locked		= 0;
	_iter->nodes_intent_locked	= 0;
	_iter->l[iter->level].node	= BTREE_ITER_NOT_END;
	_iter->l[iter->level + 1].node	= NULL;
}

int bch_btree_iter_unlink(struct btree_iter *iter)
{
	struct btree_iter *linked;
	int ret = bch_btree_iter_unlock(iter);

	for_each_linked_btree_iter(iter, linked)
		if (linked->next == iter) {
			linked->next = iter->next;
			iter->next = iter;
			return ret;
		}

	BUG();
}

void bch_btree_iter_link(struct btree_iter *iter, struct btree_iter *linked)
{
	BUG_ON(linked->next != linked);

	linked->next = iter->next;
	iter->next = linked;
}

void bch_btree_iter_copy(struct btree_iter *dst, struct btree_iter *src)
{
	bch_btree_iter_unlock(dst);

	dst->level	= src->level;
	dst->is_extents	= src->is_extents;
	dst->btree_id	= src->btree_id;
	dst->pos	= src->pos;
	memcpy(dst->l, src->l, sizeof(src->l));

	bch_btree_iter_upgrade(dst);
}

void bch_btree_iter_init_copy(struct btree_iter *dst, struct btree_iter *src)
{
	*dst = *src;
	dst->next = dst;
	dst->s[0].nodes_locked = 0;
	dst->s[0].nodes_intent_locked = 0;
	dst->s[1].nodes_locked = 0;
	dst->s[1].nodes_intent_locked = 0;
	bch_btree_iter_link(src, dst);

	bch_btree_iter_upgrade(dst);
}