summaryrefslogtreecommitdiffstats
path: root/fs/bcachefs/btree_update_leaf.c
blob: 4d1d0954efbf8bdecbf9d93b03f1a3e96a88a222 (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
// SPDX-License-Identifier: GPL-2.0

#include "bcachefs.h"
#include "btree_update.h"
#include "btree_update_interior.h"
#include "btree_io.h"
#include "btree_iter.h"
#include "btree_locking.h"
#include "debug.h"
#include "extents.h"
#include "journal.h"
#include "journal_reclaim.h"
#include "keylist.h"
#include "trace.h"

#include <linux/sort.h>

/* Inserting into a given leaf node (last stage of insert): */

/* Handle overwrites and do insert, for non extents: */
bool bch2_btree_bset_insert_key(struct btree_iter *iter,
				struct btree *b,
				struct btree_node_iter *node_iter,
				struct bkey_i *insert)
{
	const struct bkey_format *f = &b->format;
	struct bkey_packed *k;
	struct bset_tree *t;
	unsigned clobber_u64s;

	EBUG_ON(btree_node_just_written(b));
	EBUG_ON(bset_written(b, btree_bset_last(b)));
	EBUG_ON(bkey_deleted(&insert->k) && bkey_val_u64s(&insert->k));
	EBUG_ON(bkey_cmp(bkey_start_pos(&insert->k), b->data->min_key) < 0 ||
		bkey_cmp(insert->k.p, b->data->max_key) > 0);

	k = bch2_btree_node_iter_peek_all(node_iter, b);
	if (k && !bkey_cmp_packed(b, k, &insert->k)) {
		BUG_ON(bkey_whiteout(k));

		t = bch2_bkey_to_bset(b, k);

		if (bset_unwritten(b, bset(b, t)) &&
		    bkey_val_u64s(&insert->k) == bkeyp_val_u64s(f, k) &&
		    !bkey_whiteout(&insert->k)) {
			k->type = insert->k.type;
			memcpy_u64s(bkeyp_val(f, k), &insert->v,
				    bkey_val_u64s(&insert->k));
			return true;
		}

		insert->k.needs_whiteout = k->needs_whiteout;

		btree_keys_account_key_drop(&b->nr, t - b->set, k);

		if (t == bset_tree_last(b)) {
			clobber_u64s = k->u64s;

			/*
			 * If we're deleting, and the key we're deleting doesn't
			 * need a whiteout (it wasn't overwriting a key that had
			 * been written to disk) - just delete it:
			 */
			if (bkey_whiteout(&insert->k) && !k->needs_whiteout) {
				bch2_bset_delete(b, k, clobber_u64s);
				bch2_btree_node_iter_fix(iter, b, node_iter, t,
							k, clobber_u64s, 0);
				return true;
			}

			goto overwrite;
		}

		k->type = KEY_TYPE_DELETED;
		bch2_btree_node_iter_fix(iter, b, node_iter, t, k,
					k->u64s, k->u64s);

		if (bkey_whiteout(&insert->k)) {
			reserve_whiteout(b, t, k);
			return true;
		} else {
			k->needs_whiteout = false;
		}
	} else {
		/*
		 * Deleting, but the key to delete wasn't found - nothing to do:
		 */
		if (bkey_whiteout(&insert->k))
			return false;

		insert->k.needs_whiteout = false;
	}

	t = bset_tree_last(b);
	k = bch2_btree_node_iter_bset_pos(node_iter, b, t);
	clobber_u64s = 0;
overwrite:
	bch2_bset_insert(b, node_iter, k, insert, clobber_u64s);
	if (k->u64s != clobber_u64s || bkey_whiteout(&insert->k))
		bch2_btree_node_iter_fix(iter, b, node_iter, t, k,
					clobber_u64s, k->u64s);
	return true;
}

static void __btree_node_flush(struct journal *j, struct journal_entry_pin *pin,
			       unsigned i, u64 seq)
{
	struct bch_fs *c = container_of(j, struct bch_fs, journal);
	struct btree_write *w = container_of(pin, struct btree_write, journal);
	struct btree *b = container_of(w, struct btree, writes[i]);

	btree_node_lock_type(c, b, SIX_LOCK_read);
	bch2_btree_node_write_cond(c, b,
			(btree_current_write(b) == w &&
			 w->journal.pin_list == journal_seq_pin(j, seq)));
	six_unlock_read(&b->lock);
}

static void btree_node_flush0(struct journal *j, struct journal_entry_pin *pin, u64 seq)
{
	return __btree_node_flush(j, pin, 0, seq);
}

static void btree_node_flush1(struct journal *j, struct journal_entry_pin *pin, u64 seq)
{
	return __btree_node_flush(j, pin, 1, seq);
}

void bch2_btree_journal_key(struct btree_insert *trans,
			   struct btree_iter *iter,
			   struct bkey_i *insert)
{
	struct bch_fs *c = trans->c;
	struct journal *j = &c->journal;
	struct btree *b = iter->l[0].b;
	struct btree_write *w = btree_current_write(b);

	EBUG_ON(iter->level || b->level);
	EBUG_ON(trans->journal_res.ref !=
		!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY));

	if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
		u64 seq = trans->journal_res.seq;
		bool needs_whiteout = insert->k.needs_whiteout;

		/* ick */
		insert->k.needs_whiteout = false;
		bch2_journal_add_keys(j, &trans->journal_res,
				      iter->btree_id, insert);
		insert->k.needs_whiteout = needs_whiteout;

		bch2_journal_set_has_inode(j, &trans->journal_res,
					   insert->k.p.inode);

		if (trans->journal_seq)
			*trans->journal_seq = seq;
		btree_bset_last(b)->journal_seq = cpu_to_le64(seq);
	}

	if (unlikely(!journal_pin_active(&w->journal))) {
		u64 seq = likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))
			? trans->journal_res.seq
			: j->replay_journal_seq;

		bch2_journal_pin_add(j, seq, &w->journal,
				     btree_node_write_idx(b) == 0
				     ? btree_node_flush0
				     : btree_node_flush1);
	}

	if (unlikely(!btree_node_dirty(b)))
		set_btree_node_dirty(b);
}

static enum btree_insert_ret
bch2_insert_fixup_key(struct btree_insert *trans,
		     struct btree_insert_entry *insert)
{
	struct btree_iter *iter = insert->iter;
	struct btree_iter_level *l = &iter->l[0];

	EBUG_ON(iter->level);
	EBUG_ON(insert->k->k.u64s >
		bch_btree_keys_u64s_remaining(trans->c, l->b));

	if (bch2_btree_bset_insert_key(iter, l->b, &l->iter,
				       insert->k))
		bch2_btree_journal_key(trans, iter, insert->k);

	trans->did_work = true;
	return BTREE_INSERT_OK;
}

/**
 * btree_insert_key - insert a key one key into a leaf node
 */
static enum btree_insert_ret
btree_insert_key_leaf(struct btree_insert *trans,
		      struct btree_insert_entry *insert)
{
	struct bch_fs *c = trans->c;
	struct btree_iter *iter = insert->iter;
	struct btree *b = iter->l[0].b;
	enum btree_insert_ret ret;
	int old_u64s = le16_to_cpu(btree_bset_last(b)->u64s);
	int old_live_u64s = b->nr.live_u64s;
	int live_u64s_added, u64s_added;

	ret = !btree_node_is_extents(b)
		? bch2_insert_fixup_key(trans, insert)
		: bch2_insert_fixup_extent(trans, insert);

	live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
	u64s_added = (int) le16_to_cpu(btree_bset_last(b)->u64s) - old_u64s;

	if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
		b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
	if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
		b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);

	if (u64s_added > live_u64s_added &&
	    bch2_maybe_compact_whiteouts(c, b))
		bch2_btree_iter_reinit_node(iter, b);

	trace_btree_insert_key(c, b, insert->k);
	return ret;
}

#define trans_for_each_entry(trans, i)					\
	for ((i) = (trans)->entries; (i) < (trans)->entries + (trans)->nr; (i)++)

/*
 * We sort transaction entries so that if multiple iterators point to the same
 * leaf node they'll be adjacent:
 */
static bool same_leaf_as_prev(struct btree_insert *trans,
			      struct btree_insert_entry *i)
{
	return i != trans->entries &&
		i[0].iter->l[0].b == i[-1].iter->l[0].b;
}

static inline struct btree_insert_entry *trans_next_leaf(struct btree_insert *trans,
							 struct btree_insert_entry *i)
{
	struct btree *b = i->iter->l[0].b;

	do {
		i++;
	} while (i < trans->entries + trans->nr && b == i->iter->l[0].b);

	return i;
}

#define trans_for_each_leaf(trans, i)					\
	for ((i) = (trans)->entries;					\
	     (i) < (trans)->entries + (trans)->nr;			\
	     (i) = trans_next_leaf(trans, i))

inline void bch2_btree_node_lock_for_insert(struct bch_fs *c, struct btree *b,
					    struct btree_iter *iter)
{
	bch2_btree_node_lock_write(b, iter);

	if (btree_node_just_written(b) &&
	    bch2_btree_post_write_cleanup(c, b))
		bch2_btree_iter_reinit_node(iter, b);

	/*
	 * If the last bset has been written, or if it's gotten too big - start
	 * a new bset to insert into:
	 */
	if (want_new_bset(c, b))
		bch2_btree_init_next(c, b, iter);
}

static void multi_lock_write(struct bch_fs *c, struct btree_insert *trans)
{
	struct btree_insert_entry *i;

	trans_for_each_leaf(trans, i)
		bch2_btree_node_lock_for_insert(c, i->iter->l[0].b, i->iter);
}

static void multi_unlock_write(struct btree_insert *trans)
{
	struct btree_insert_entry *i;

	trans_for_each_leaf(trans, i)
		bch2_btree_node_unlock_write(i->iter->l[0].b, i->iter);
}

static inline int btree_trans_cmp(struct btree_insert_entry l,
				  struct btree_insert_entry r)
{
	return btree_iter_cmp(l.iter, r.iter);
}

/* Normal update interface: */

/*
 * Get journal reservation, take write locks, and attempt to do btree update(s):
 */
static inline int do_btree_insert_at(struct btree_insert *trans,
				     struct btree_iter **split,
				     bool *cycle_gc_lock)
{
	struct bch_fs *c = trans->c;
	struct btree_insert_entry *i;
	unsigned u64s;
	int ret;

	trans_for_each_entry(trans, i) {
		BUG_ON(i->done);
		BUG_ON(i->iter->uptodate >= BTREE_ITER_NEED_RELOCK);
	}

	u64s = 0;
	trans_for_each_entry(trans, i)
		u64s += jset_u64s(i->k->k.u64s + i->extra_res);

	memset(&trans->journal_res, 0, sizeof(trans->journal_res));

	ret = !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)
		? bch2_journal_res_get(&c->journal,
				      &trans->journal_res,
				      u64s, u64s)
		: 0;
	if (ret)
		return ret;

	multi_lock_write(c, trans);

	if (race_fault()) {
		ret = -EINTR;
		goto out;
	}

	u64s = 0;
	trans_for_each_entry(trans, i) {
		/* Multiple inserts might go to same leaf: */
		if (!same_leaf_as_prev(trans, i))
			u64s = 0;

		/*
		 * bch2_btree_node_insert_fits() must be called under write lock:
		 * with only an intent lock, another thread can still call
		 * bch2_btree_node_write(), converting an unwritten bset to a
		 * written one
		 */
		u64s += i->k->k.u64s + i->extra_res;
		if (!bch2_btree_node_insert_fits(c,
				i->iter->l[0].b, u64s)) {
			ret = -EINTR;
			*split = i->iter;
			goto out;
		}
	}

	if (!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)) {
		if (journal_seq_verify(c))
			trans_for_each_entry(trans, i)
				i->k->k.version.lo = trans->journal_res.seq;
		else if (inject_invalid_keys(c))
			trans_for_each_entry(trans, i)
				i->k->k.version = MAX_VERSION;
	}

	trans_for_each_entry(trans, i) {
		switch (btree_insert_key_leaf(trans, i)) {
		case BTREE_INSERT_OK:
			i->done = true;
			break;
		case BTREE_INSERT_JOURNAL_RES_FULL:
		case BTREE_INSERT_NEED_TRAVERSE:
		case BTREE_INSERT_NEED_RESCHED:
			ret = -EINTR;
			break;
		case BTREE_INSERT_BTREE_NODE_FULL:
			ret = -EINTR;
			*split = i->iter;
			break;
		case BTREE_INSERT_ENOSPC:
			ret = -ENOSPC;
			break;
		case BTREE_INSERT_NEED_GC_LOCK:
			ret = -EINTR;
			*cycle_gc_lock = true;
			break;
		default:
			BUG();
		}

		/*
		 * If we did some work (i.e. inserted part of an extent),
		 * we have to do all the other updates as well:
		 */
		if (!trans->did_work && (ret || *split))
			break;
	}
out:
	multi_unlock_write(trans);
	bch2_journal_res_put(&c->journal, &trans->journal_res);

	return ret;
}

static inline void btree_insert_entry_checks(struct bch_fs *c,
					     struct btree_insert_entry *i)
{
	BUG_ON(i->iter->level);
	BUG_ON(bkey_cmp(bkey_start_pos(&i->k->k), i->iter->pos));
	BUG_ON(debug_check_bkeys(c) &&
	       !bkey_deleted(&i->k->k) &&
	       bch2_bkey_invalid(c, (enum bkey_type) i->iter->btree_id,
				 bkey_i_to_s_c(i->k)));
}

/**
 * __bch_btree_insert_at - insert keys at given iterator positions
 *
 * This is main entry point for btree updates.
 *
 * Return values:
 * -EINTR: locking changed, this function should be called again. Only returned
 *  if passed BTREE_INSERT_ATOMIC.
 * -EROFS: filesystem read only
 * -EIO: journal or btree node IO error
 */
int __bch2_btree_insert_at(struct btree_insert *trans)
{
	struct bch_fs *c = trans->c;
	struct btree_insert_entry *i;
	struct btree_iter *linked, *split = NULL;
	bool cycle_gc_lock = false;
	unsigned flags;
	int ret;

	BUG_ON(!trans->nr);

	for_each_btree_iter(trans->entries[0].iter, linked)
		bch2_btree_iter_verify_locks(linked);

	/* for the sake of sanity: */
	BUG_ON(trans->nr > 1 && !(trans->flags & BTREE_INSERT_ATOMIC));

	trans_for_each_entry(trans, i)
		btree_insert_entry_checks(c, i);

	bubble_sort(trans->entries, trans->nr, btree_trans_cmp);

	if (unlikely(!percpu_ref_tryget(&c->writes)))
		return -EROFS;
retry:
	split = NULL;
	cycle_gc_lock = false;

	trans_for_each_entry(trans, i) {
		if (!bch2_btree_iter_upgrade(i->iter, 1, true)) {
			ret = -EINTR;
			goto err;
		}

		if (i->iter->flags & BTREE_ITER_ERROR) {
			ret = -EIO;
			goto err;
		}
	}

	ret = do_btree_insert_at(trans, &split, &cycle_gc_lock);
	if (unlikely(ret))
		goto err;

	trans_for_each_leaf(trans, i)
		bch2_foreground_maybe_merge(c, i->iter, 0, trans->flags);

	trans_for_each_entry(trans, i)
		bch2_btree_iter_downgrade(i->iter);
out:
	percpu_ref_put(&c->writes);

	if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
		/* make sure we didn't drop or screw up locks: */
		for_each_btree_iter(trans->entries[0].iter, linked) {
			bch2_btree_iter_verify_locks(linked);
			BUG_ON((trans->flags & BTREE_INSERT_NOUNLOCK) &&
			       trans->did_work &&
			       linked->uptodate >= BTREE_ITER_NEED_RELOCK);
		}

		/* make sure we didn't lose an error: */
		if (!ret)
			trans_for_each_entry(trans, i)
				BUG_ON(!i->done);
	}

	BUG_ON(!(trans->flags & BTREE_INSERT_ATOMIC) && ret == -EINTR);

	return ret;
err:
	flags = trans->flags;

	/*
	 * BTREE_INSERT_NOUNLOCK means don't unlock _after_ successful btree
	 * update; if we haven't done anything yet it doesn't apply
	 */
	if (!trans->did_work)
		flags &= ~BTREE_INSERT_NOUNLOCK;

	if (split) {
		ret = bch2_btree_split_leaf(c, split, flags);

		/*
		 * if the split succeeded without dropping locks the insert will
		 * still be atomic (in the BTREE_INSERT_ATOMIC sense, what the
		 * caller peeked() and is overwriting won't have changed)
		 */
#if 0
		/*
		 * XXX:
		 * split -> btree node merging (of parent node) might still drop
		 * locks when we're not passing it BTREE_INSERT_NOUNLOCK
		 */
		if (!ret && !trans->did_work)
			goto retry;
#endif

		/*
		 * don't care if we got ENOSPC because we told split it
		 * couldn't block:
		 */
		if (!ret || (flags & BTREE_INSERT_NOUNLOCK))
			ret = -EINTR;
	}

	if (cycle_gc_lock) {
		if (!down_read_trylock(&c->gc_lock)) {
			if (flags & BTREE_INSERT_NOUNLOCK)
				goto out;

			bch2_btree_iter_unlock(trans->entries[0].iter);
			down_read(&c->gc_lock);
		}
		up_read(&c->gc_lock);
	}

	if (ret == -EINTR) {
		if (flags & BTREE_INSERT_NOUNLOCK)
			goto out;

		trans_for_each_entry(trans, i) {
			int ret2 = bch2_btree_iter_traverse(i->iter);
			if (ret2) {
				ret = ret2;
				goto out;
			}

			BUG_ON(i->iter->uptodate > BTREE_ITER_NEED_PEEK);
		}

		/*
		 * BTREE_ITER_ATOMIC means we have to return -EINTR if we
		 * dropped locks:
		 */
		if (!(flags & BTREE_INSERT_ATOMIC))
			goto retry;
	}

	goto out;
}

void bch2_trans_update(struct btree_trans *trans,
		       struct btree_iter *iter,
		       struct bkey_i *k,
		       unsigned extra_journal_res)
{
	struct btree_insert_entry *i;

	BUG_ON(trans->nr_updates >= ARRAY_SIZE(trans->updates));

	i = &trans->updates[trans->nr_updates++];

	*i = (struct btree_insert_entry) {
		.iter	= iter,
		.k		= k,
		.extra_res	= extra_journal_res,
	};

	btree_insert_entry_checks(trans->c, i);
}

int bch2_trans_commit(struct btree_trans *trans,
		      struct disk_reservation *disk_res,
		      struct extent_insert_hook *hook,
		      u64 *journal_seq,
		      unsigned flags)
{
	struct btree_insert insert = {
		.c		= trans->c,
		.disk_res	= disk_res,
		.journal_seq	= journal_seq,
		.flags		= flags,
		.nr		= trans->nr_updates,
		.entries	= trans->updates,
	};

	if (!trans->nr_updates)
		return 0;

	trans->nr_updates = 0;

	return __bch2_btree_insert_at(&insert);
}

int bch2_btree_delete_at(struct btree_iter *iter, unsigned flags)
{
	struct bkey_i k;

	bkey_init(&k.k);
	k.k.p = iter->pos;

	return bch2_btree_insert_at(iter->c, NULL, NULL, NULL,
				    BTREE_INSERT_NOFAIL|
				    BTREE_INSERT_USE_RESERVE|flags,
				    BTREE_INSERT_ENTRY(iter, &k));
}

int bch2_btree_insert_list_at(struct btree_iter *iter,
			     struct keylist *keys,
			     struct disk_reservation *disk_res,
			     struct extent_insert_hook *hook,
			     u64 *journal_seq, unsigned flags)
{
	BUG_ON(flags & BTREE_INSERT_ATOMIC);
	BUG_ON(bch2_keylist_empty(keys));
	bch2_verify_keylist_sorted(keys);

	while (!bch2_keylist_empty(keys)) {
		int ret = bch2_btree_insert_at(iter->c, disk_res, hook,
				journal_seq, flags,
				BTREE_INSERT_ENTRY(iter, bch2_keylist_front(keys)));
		if (ret)
			return ret;

		bch2_keylist_pop_front(keys);
	}

	return 0;
}

/**
 * bch_btree_insert - insert keys into the extent btree
 * @c:			pointer to struct bch_fs
 * @id:			btree to insert into
 * @insert_keys:	list of keys to insert
 * @hook:		insert callback
 */
int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
		     struct bkey_i *k,
		     struct disk_reservation *disk_res,
		     struct extent_insert_hook *hook,
		     u64 *journal_seq, int flags)
{
	struct btree_iter iter;
	int ret;

	bch2_btree_iter_init(&iter, c, id, bkey_start_pos(&k->k),
			     BTREE_ITER_INTENT);
	ret = bch2_btree_insert_at(c, disk_res, hook, journal_seq, flags,
				   BTREE_INSERT_ENTRY(&iter, k));
	bch2_btree_iter_unlock(&iter);

	return ret;
}

/*
 * bch_btree_delete_range - delete everything within a given range
 *
 * Range is a half open interval - [start, end)
 */
int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
			   struct bpos start,
			   struct bpos end,
			   struct bversion version,
			   struct disk_reservation *disk_res,
			   struct extent_insert_hook *hook,
			   u64 *journal_seq)
{
	struct btree_iter iter;
	struct bkey_s_c k;
	int ret = 0;

	bch2_btree_iter_init(&iter, c, id, start,
			     BTREE_ITER_INTENT);

	while ((k = bch2_btree_iter_peek(&iter)).k &&
	       !(ret = btree_iter_err(k))) {
		unsigned max_sectors = KEY_SIZE_MAX & (~0 << c->block_bits);
		/* really shouldn't be using a bare, unpadded bkey_i */
		struct bkey_i delete;

		if (bkey_cmp(iter.pos, end) >= 0)
			break;

		bkey_init(&delete.k);

		/*
		 * For extents, iter.pos won't necessarily be the same as
		 * bkey_start_pos(k.k) (for non extents they always will be the
		 * same). It's important that we delete starting from iter.pos
		 * because the range we want to delete could start in the middle
		 * of k.
		 *
		 * (bch2_btree_iter_peek() does guarantee that iter.pos >=
		 * bkey_start_pos(k.k)).
		 */
		delete.k.p = iter.pos;
		delete.k.version = version;

		if (iter.flags & BTREE_ITER_IS_EXTENTS) {
			/* create the biggest key we can */
			bch2_key_resize(&delete.k, max_sectors);
			bch2_cut_back(end, &delete.k);
		}

		ret = bch2_btree_insert_at(c, disk_res, hook, journal_seq,
					   BTREE_INSERT_NOFAIL,
					   BTREE_INSERT_ENTRY(&iter, &delete));
		if (ret)
			break;

		bch2_btree_iter_cond_resched(&iter);
	}

	bch2_btree_iter_unlock(&iter);
	return ret;
}