4 #include "bkey_methods.h"
5 #include "btree_cache.h"
7 #include "btree_update.h"
8 #include "btree_update_interior.h"
10 #include "btree_iter.h"
11 #include "btree_locking.h"
15 #include "journal_reclaim.h"
20 #include <linux/random.h>
21 #include <trace/events/bcachefs.h>
23 static void btree_node_will_make_reachable(struct btree_update *,
25 static void btree_update_drop_new_node(struct bch_fs *, struct btree *);
26 static void bch2_btree_set_root_ondisk(struct bch_fs *, struct btree *, int);
30 static void btree_node_interior_verify(struct btree *b)
32 struct btree_node_iter iter;
33 struct bkey_packed *k;
37 bch2_btree_node_iter_init(&iter, b, b->key.k.p, false, false);
39 BUG_ON(!(k = bch2_btree_node_iter_peek(&iter, b)) ||
40 bkey_cmp_left_packed(b, k, &b->key.k.p));
42 BUG_ON((bch2_btree_node_iter_advance(&iter, b),
43 !bch2_btree_node_iter_end(&iter)));
48 k = bch2_btree_node_iter_peek(&iter, b);
52 msg = "isn't what it should be";
53 if (bkey_cmp_left_packed(b, k, &b->key.k.p))
56 bch2_btree_node_iter_advance(&iter, b);
58 msg = "isn't last key";
59 if (!bch2_btree_node_iter_end(&iter))
63 bch2_dump_btree_node(b);
64 printk(KERN_ERR "last key %llu:%llu %s\n", b->key.k.p.inode,
65 b->key.k.p.offset, msg);
70 /* Calculate ideal packed bkey format for new btree nodes: */
72 void __bch2_btree_calc_format(struct bkey_format_state *s, struct btree *b)
74 struct bkey_packed *k;
78 bch2_bkey_format_add_pos(s, b->data->min_key);
81 for (k = btree_bkey_first(b, t);
82 k != btree_bkey_last(b, t);
84 if (!bkey_whiteout(k)) {
85 uk = bkey_unpack_key(b, k);
86 bch2_bkey_format_add_key(s, &uk);
90 static struct bkey_format bch2_btree_calc_format(struct btree *b)
92 struct bkey_format_state s;
94 bch2_bkey_format_init(&s);
95 __bch2_btree_calc_format(&s, b);
97 return bch2_bkey_format_done(&s);
100 static size_t btree_node_u64s_with_format(struct btree *b,
101 struct bkey_format *new_f)
103 struct bkey_format *old_f = &b->format;
105 /* stupid integer promotion rules */
107 (((int) new_f->key_u64s - old_f->key_u64s) *
108 (int) b->nr.packed_keys) +
109 (((int) new_f->key_u64s - BKEY_U64s) *
110 (int) b->nr.unpacked_keys);
112 BUG_ON(delta + b->nr.live_u64s < 0);
114 return b->nr.live_u64s + delta;
118 * btree_node_format_fits - check if we could rewrite node with a new format
120 * This assumes all keys can pack with the new format -- it just checks if
121 * the re-packed keys would fit inside the node itself.
123 bool bch2_btree_node_format_fits(struct bch_fs *c, struct btree *b,
124 struct bkey_format *new_f)
126 size_t u64s = btree_node_u64s_with_format(b, new_f);
128 return __vstruct_bytes(struct btree_node, u64s) < btree_bytes(c);
131 /* Btree node freeing/allocation: */
133 static bool btree_key_matches(struct bch_fs *c,
134 struct bkey_s_c_extent l,
135 struct bkey_s_c_extent r)
137 const struct bch_extent_ptr *ptr1, *ptr2;
139 extent_for_each_ptr(l, ptr1)
140 extent_for_each_ptr(r, ptr2)
141 if (ptr1->dev == ptr2->dev &&
142 ptr1->gen == ptr2->gen &&
143 ptr1->offset == ptr2->offset)
150 * We're doing the index update that makes @b unreachable, update stuff to
153 * Must be called _before_ btree_update_updated_root() or
154 * btree_update_updated_node:
156 static void bch2_btree_node_free_index(struct btree_update *as, struct btree *b,
158 struct bch_fs_usage *stats)
160 struct bch_fs *c = as->c;
161 struct pending_btree_node_free *d;
165 * btree_update lock is only needed here to avoid racing with
168 mutex_lock(&c->btree_interior_update_lock);
170 for (d = as->pending; d < as->pending + as->nr_pending; d++)
171 if (!bkey_cmp(k.k->p, d->key.k.p) &&
172 btree_key_matches(c, bkey_s_c_to_extent(k),
173 bkey_i_to_s_c_extent(&d->key)))
177 BUG_ON(d->index_update_done);
178 d->index_update_done = true;
181 * Btree nodes are accounted as freed in bch_alloc_stats when they're
182 * freed from the index:
184 replicas = bch2_extent_nr_dirty_ptrs(k);
186 stats->s[replicas - 1].data[S_META] -= c->opts.btree_node_size;
189 * We're dropping @k from the btree, but it's still live until the
190 * index update is persistent so we need to keep a reference around for
191 * mark and sweep to find - that's primarily what the
192 * btree_node_pending_free list is for.
194 * So here (when we set index_update_done = true), we're moving an
195 * existing reference to a different part of the larger "gc keyspace" -
196 * and the new position comes after the old position, since GC marks
197 * the pending free list after it walks the btree.
199 * If we move the reference while mark and sweep is _between_ the old
200 * and the new position, mark and sweep will see the reference twice
201 * and it'll get double accounted - so check for that here and subtract
202 * to cancel out one of mark and sweep's markings if necessary:
206 * bch2_mark_key() compares the current gc pos to the pos we're
207 * moving this reference from, hence one comparison here:
209 if (gc_pos_cmp(c->gc_pos, gc_phase(GC_PHASE_PENDING_DELETE)) < 0) {
210 struct bch_fs_usage tmp = { 0 };
212 bch2_mark_key(c, bkey_i_to_s_c(&d->key),
213 -c->opts.btree_node_size, true, b
214 ? gc_pos_btree_node(b)
215 : gc_pos_btree_root(as->btree_id),
218 * Don't apply tmp - pending deletes aren't tracked in
223 mutex_unlock(&c->btree_interior_update_lock);
226 static void __btree_node_free(struct bch_fs *c, struct btree *b,
227 struct btree_iter *iter)
229 trace_btree_node_free(c, b);
231 BUG_ON(btree_node_dirty(b));
232 BUG_ON(btree_node_need_write(b));
233 BUG_ON(b == btree_node_root(c, b));
235 BUG_ON(!list_empty(&b->write_blocked));
236 BUG_ON(b->will_make_reachable);
238 clear_btree_node_noevict(b);
240 six_lock_write(&b->lock);
242 bch2_btree_node_hash_remove(&c->btree_cache, b);
244 mutex_lock(&c->btree_cache.lock);
245 list_move(&b->list, &c->btree_cache.freeable);
246 mutex_unlock(&c->btree_cache.lock);
249 * By using six_unlock_write() directly instead of
250 * bch2_btree_node_unlock_write(), we don't update the iterator's
251 * sequence numbers and cause future bch2_btree_node_relock() calls to
254 six_unlock_write(&b->lock);
257 void bch2_btree_node_free_never_inserted(struct bch_fs *c, struct btree *b)
259 struct btree_ob_ref ob = b->ob;
261 btree_update_drop_new_node(c, b);
265 clear_btree_node_dirty(b);
267 __btree_node_free(c, b, NULL);
269 bch2_open_bucket_put_refs(c, &ob.nr, ob.refs);
272 void bch2_btree_node_free_inmem(struct bch_fs *c, struct btree *b,
273 struct btree_iter *iter)
276 * Is this a node that isn't reachable on disk yet?
278 * Nodes that aren't reachable yet have writes blocked until they're
279 * reachable - now that we've cancelled any pending writes and moved
280 * things waiting on that write to wait on this update, we can drop this
281 * node from the list of nodes that the other update is making
282 * reachable, prior to freeing it:
284 btree_update_drop_new_node(c, b);
286 bch2_btree_iter_node_drop_linked(iter, b);
288 __btree_node_free(c, b, iter);
290 bch2_btree_iter_node_drop(iter, b);
293 static void bch2_btree_node_free_ondisk(struct bch_fs *c,
294 struct pending_btree_node_free *pending)
296 struct bch_fs_usage stats = { 0 };
298 BUG_ON(!pending->index_update_done);
300 bch2_mark_key(c, bkey_i_to_s_c(&pending->key),
301 -c->opts.btree_node_size, true,
302 gc_phase(GC_PHASE_PENDING_DELETE),
305 * Don't apply stats - pending deletes aren't tracked in
310 void bch2_btree_open_bucket_put(struct bch_fs *c, struct btree *b)
312 bch2_open_bucket_put_refs(c, &b->ob.nr, b->ob.refs);
315 static struct btree *__bch2_btree_node_alloc(struct bch_fs *c,
316 struct disk_reservation *res,
320 struct write_point *wp;
323 struct bkey_i_extent *e;
324 struct btree_ob_ref ob;
325 struct bch_devs_list devs_have = (struct bch_devs_list) { 0 };
327 enum alloc_reserve alloc_reserve;
329 if (flags & BTREE_INSERT_USE_ALLOC_RESERVE) {
331 alloc_reserve = RESERVE_ALLOC;
332 } else if (flags & BTREE_INSERT_USE_RESERVE) {
333 nr_reserve = BTREE_NODE_RESERVE / 2;
334 alloc_reserve = RESERVE_BTREE;
336 nr_reserve = BTREE_NODE_RESERVE;
337 alloc_reserve = RESERVE_NONE;
340 mutex_lock(&c->btree_reserve_cache_lock);
341 if (c->btree_reserve_cache_nr > nr_reserve) {
342 struct btree_alloc *a =
343 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
346 bkey_copy(&tmp.k, &a->k);
347 mutex_unlock(&c->btree_reserve_cache_lock);
350 mutex_unlock(&c->btree_reserve_cache_lock);
353 wp = bch2_alloc_sectors_start(c, c->opts.foreground_target,
354 writepoint_ptr(&c->btree_write_point),
357 c->opts.metadata_replicas_required,
358 alloc_reserve, 0, cl);
362 if (wp->sectors_free < c->opts.btree_node_size) {
363 struct open_bucket *ob;
366 writepoint_for_each_ptr(wp, ob, i)
367 if (ob->sectors_free < c->opts.btree_node_size)
368 ob->sectors_free = 0;
370 bch2_alloc_sectors_done(c, wp);
374 e = bkey_extent_init(&tmp.k);
375 bch2_alloc_sectors_append_ptrs(c, wp, e, c->opts.btree_node_size);
378 bch2_open_bucket_get(c, wp, &ob.nr, ob.refs);
379 bch2_alloc_sectors_done(c, wp);
381 b = bch2_btree_node_mem_alloc(c);
383 /* we hold cannibalize_lock: */
387 bkey_copy(&b->key, &tmp.k);
393 static struct btree *bch2_btree_node_alloc(struct btree_update *as, unsigned level)
395 struct bch_fs *c = as->c;
398 BUG_ON(level >= BTREE_MAX_DEPTH);
399 BUG_ON(!as->reserve->nr);
401 b = as->reserve->b[--as->reserve->nr];
403 BUG_ON(bch2_btree_node_hash_insert(&c->btree_cache, b, level, as->btree_id));
405 set_btree_node_accessed(b);
406 set_btree_node_dirty(b);
408 bch2_bset_init_first(b, &b->data->keys);
409 memset(&b->nr, 0, sizeof(b->nr));
410 b->data->magic = cpu_to_le64(bset_magic(c));
412 SET_BTREE_NODE_ID(b->data, as->btree_id);
413 SET_BTREE_NODE_LEVEL(b->data, level);
414 b->data->ptr = bkey_i_to_extent(&b->key)->v.start->ptr;
416 bch2_btree_build_aux_trees(b);
418 btree_node_will_make_reachable(as, b);
420 trace_btree_node_alloc(c, b);
424 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *as,
426 struct bkey_format format)
430 n = bch2_btree_node_alloc(as, b->level);
432 n->data->min_key = b->data->min_key;
433 n->data->max_key = b->data->max_key;
434 n->data->format = format;
435 SET_BTREE_NODE_SEQ(n->data, BTREE_NODE_SEQ(b->data) + 1);
437 btree_node_set_format(n, format);
439 bch2_btree_sort_into(as->c, n, b);
441 btree_node_reset_sib_u64s(n);
443 n->key.k.p = b->key.k.p;
447 static struct btree *bch2_btree_node_alloc_replacement(struct btree_update *as,
450 struct bkey_format new_f = bch2_btree_calc_format(b);
453 * The keys might expand with the new format - if they wouldn't fit in
454 * the btree node anymore, use the old format for now:
456 if (!bch2_btree_node_format_fits(as->c, b, &new_f))
459 return __bch2_btree_node_alloc_replacement(as, b, new_f);
462 static struct btree *__btree_root_alloc(struct btree_update *as, unsigned level)
464 struct btree *b = bch2_btree_node_alloc(as, level);
466 b->data->min_key = POS_MIN;
467 b->data->max_key = POS_MAX;
468 b->data->format = bch2_btree_calc_format(b);
469 b->key.k.p = POS_MAX;
471 btree_node_set_format(b, b->data->format);
472 bch2_btree_build_aux_trees(b);
474 six_unlock_write(&b->lock);
479 static void bch2_btree_reserve_put(struct bch_fs *c, struct btree_reserve *reserve)
481 bch2_disk_reservation_put(c, &reserve->disk_res);
483 mutex_lock(&c->btree_reserve_cache_lock);
485 while (reserve->nr) {
486 struct btree *b = reserve->b[--reserve->nr];
488 six_unlock_write(&b->lock);
490 if (c->btree_reserve_cache_nr <
491 ARRAY_SIZE(c->btree_reserve_cache)) {
492 struct btree_alloc *a =
493 &c->btree_reserve_cache[c->btree_reserve_cache_nr++];
497 bkey_copy(&a->k, &b->key);
499 bch2_btree_open_bucket_put(c, b);
502 __btree_node_free(c, b, NULL);
504 six_unlock_intent(&b->lock);
507 mutex_unlock(&c->btree_reserve_cache_lock);
509 mempool_free(reserve, &c->btree_reserve_pool);
512 static struct btree_reserve *bch2_btree_reserve_get(struct bch_fs *c,
517 struct btree_reserve *reserve;
519 struct disk_reservation disk_res = { 0, 0 };
520 unsigned sectors = nr_nodes * c->opts.btree_node_size;
521 int ret, disk_res_flags = BCH_DISK_RESERVATION_GC_LOCK_HELD;
523 if (flags & BTREE_INSERT_NOFAIL)
524 disk_res_flags |= BCH_DISK_RESERVATION_NOFAIL;
527 * This check isn't necessary for correctness - it's just to potentially
528 * prevent us from doing a lot of work that'll end up being wasted:
530 ret = bch2_journal_error(&c->journal);
534 if (bch2_disk_reservation_get(c, &disk_res, sectors,
535 c->opts.metadata_replicas,
537 return ERR_PTR(-ENOSPC);
539 BUG_ON(nr_nodes > BTREE_RESERVE_MAX);
542 * Protects reaping from the btree node cache and using the btree node
543 * open bucket reserve:
545 ret = bch2_btree_cache_cannibalize_lock(c, cl);
547 bch2_disk_reservation_put(c, &disk_res);
551 reserve = mempool_alloc(&c->btree_reserve_pool, GFP_NOIO);
553 reserve->disk_res = disk_res;
556 while (reserve->nr < nr_nodes) {
557 b = __bch2_btree_node_alloc(c, &disk_res,
558 flags & BTREE_INSERT_NOWAIT
565 ret = bch2_mark_bkey_replicas(c, BCH_DATA_BTREE,
566 bkey_i_to_s_c(&b->key));
570 reserve->b[reserve->nr++] = b;
573 bch2_btree_cache_cannibalize_unlock(c);
576 bch2_btree_reserve_put(c, reserve);
577 bch2_btree_cache_cannibalize_unlock(c);
578 trace_btree_reserve_get_fail(c, nr_nodes, cl);
582 /* Asynchronous interior node update machinery */
584 static void bch2_btree_update_free(struct btree_update *as)
586 struct bch_fs *c = as->c;
588 BUG_ON(as->nr_new_nodes);
589 BUG_ON(as->nr_pending);
592 bch2_btree_reserve_put(c, as->reserve);
594 mutex_lock(&c->btree_interior_update_lock);
597 closure_debug_destroy(&as->cl);
598 mempool_free(as, &c->btree_interior_update_pool);
599 percpu_ref_put(&c->writes);
601 closure_wake_up(&c->btree_interior_update_wait);
602 mutex_unlock(&c->btree_interior_update_lock);
605 static void btree_update_nodes_reachable(struct closure *cl)
607 struct btree_update *as = container_of(cl, struct btree_update, cl);
608 struct bch_fs *c = as->c;
610 bch2_journal_pin_drop(&c->journal, &as->journal);
612 mutex_lock(&c->btree_interior_update_lock);
614 while (as->nr_new_nodes) {
615 struct btree *b = as->new_nodes[--as->nr_new_nodes];
617 BUG_ON(b->will_make_reachable != (unsigned long) as);
618 b->will_make_reachable = 0;
619 mutex_unlock(&c->btree_interior_update_lock);
622 * b->will_make_reachable prevented it from being written, so
623 * write it now if it needs to be written:
625 six_lock_read(&b->lock);
626 bch2_btree_node_write_cond(c, b, btree_node_need_write(b));
627 six_unlock_read(&b->lock);
628 mutex_lock(&c->btree_interior_update_lock);
631 while (as->nr_pending)
632 bch2_btree_node_free_ondisk(c, &as->pending[--as->nr_pending]);
634 mutex_unlock(&c->btree_interior_update_lock);
636 closure_wake_up(&as->wait);
638 bch2_btree_update_free(as);
641 static void btree_update_wait_on_journal(struct closure *cl)
643 struct btree_update *as = container_of(cl, struct btree_update, cl);
644 struct bch_fs *c = as->c;
647 ret = bch2_journal_open_seq_async(&c->journal, as->journal_seq, cl);
651 continue_at(cl, btree_update_wait_on_journal, system_wq);
653 bch2_journal_flush_seq_async(&c->journal, as->journal_seq, cl);
655 continue_at(cl, btree_update_nodes_reachable, system_wq);
658 static void btree_update_nodes_written(struct closure *cl)
660 struct btree_update *as = container_of(cl, struct btree_update, cl);
661 struct bch_fs *c = as->c;
665 * We did an update to a parent node where the pointers we added pointed
666 * to child nodes that weren't written yet: now, the child nodes have
667 * been written so we can write out the update to the interior node.
670 mutex_lock(&c->btree_interior_update_lock);
671 as->nodes_written = true;
674 case BTREE_INTERIOR_NO_UPDATE:
676 case BTREE_INTERIOR_UPDATING_NODE:
677 /* The usual case: */
678 b = READ_ONCE(as->b);
680 if (!six_trylock_read(&b->lock)) {
681 mutex_unlock(&c->btree_interior_update_lock);
682 six_lock_read(&b->lock);
683 six_unlock_read(&b->lock);
687 BUG_ON(!btree_node_dirty(b));
688 closure_wait(&btree_current_write(b)->wait, cl);
690 list_del(&as->write_blocked_list);
691 mutex_unlock(&c->btree_interior_update_lock);
694 * b->write_blocked prevented it from being written, so
695 * write it now if it needs to be written:
697 bch2_btree_node_write_cond(c, b, true);
698 six_unlock_read(&b->lock);
701 case BTREE_INTERIOR_UPDATING_AS:
703 * The btree node we originally updated has been freed and is
704 * being rewritten - so we need to write anything here, we just
705 * need to signal to that btree_update that it's ok to make the
706 * new replacement node visible:
708 closure_put(&as->parent_as->cl);
711 * and then we have to wait on that btree_update to finish:
713 closure_wait(&as->parent_as->wait, cl);
714 mutex_unlock(&c->btree_interior_update_lock);
717 case BTREE_INTERIOR_UPDATING_ROOT:
718 /* b is the new btree root: */
719 b = READ_ONCE(as->b);
721 if (!six_trylock_read(&b->lock)) {
722 mutex_unlock(&c->btree_interior_update_lock);
723 six_lock_read(&b->lock);
724 six_unlock_read(&b->lock);
728 BUG_ON(c->btree_roots[b->btree_id].as != as);
729 c->btree_roots[b->btree_id].as = NULL;
731 bch2_btree_set_root_ondisk(c, b, WRITE);
734 * We don't have to wait anything anything here (before
735 * btree_update_nodes_reachable frees the old nodes
736 * ondisk) - we've ensured that the very next journal write will
737 * have the pointer to the new root, and before the allocator
738 * can reuse the old nodes it'll have to do a journal commit:
740 six_unlock_read(&b->lock);
741 mutex_unlock(&c->btree_interior_update_lock);
744 * Bit of funny circularity going on here we have to break:
746 * We have to drop our journal pin before writing the journal
747 * entry that points to the new btree root: else, we could
748 * deadlock if the journal currently happens to be full.
750 * This mean we're dropping the journal pin _before_ the new
751 * nodes are technically reachable - but this is safe, because
752 * after the bch2_btree_set_root_ondisk() call above they will
753 * be reachable as of the very next journal write:
755 bch2_journal_pin_drop(&c->journal, &as->journal);
757 as->journal_seq = bch2_journal_last_unwritten_seq(&c->journal);
759 btree_update_wait_on_journal(cl);
763 continue_at(cl, btree_update_nodes_reachable, system_wq);
767 * We're updating @b with pointers to nodes that haven't finished writing yet:
768 * block @b from being written until @as completes
770 static void btree_update_updated_node(struct btree_update *as, struct btree *b)
772 struct bch_fs *c = as->c;
774 mutex_lock(&c->btree_interior_update_lock);
776 BUG_ON(as->mode != BTREE_INTERIOR_NO_UPDATE);
777 BUG_ON(!btree_node_dirty(b));
779 as->mode = BTREE_INTERIOR_UPDATING_NODE;
781 list_add(&as->write_blocked_list, &b->write_blocked);
783 mutex_unlock(&c->btree_interior_update_lock);
786 * In general, when you're staging things in a journal that will later
787 * be written elsewhere, and you also want to guarantee ordering: that
788 * is, if you have updates a, b, c, after a crash you should never see c
789 * and not a or b - there's a problem:
791 * If the final destination of the update(s) (i.e. btree node) can be
792 * written/flushed _before_ the relevant journal entry - oops, that
793 * breaks ordering, since the various leaf nodes can be written in any
796 * Normally we use bset->journal_seq to deal with this - if during
797 * recovery we find a btree node write that's newer than the newest
798 * journal entry, we just ignore it - we don't need it, anything we're
799 * supposed to have (that we reported as completed via fsync()) will
800 * still be in the journal, and as far as the state of the journal is
801 * concerned that btree node write never happened.
803 * That breaks when we're rewriting/splitting/merging nodes, since we're
804 * mixing btree node writes that haven't happened yet with previously
805 * written data that has been reported as completed to the journal.
807 * Thus, before making the new nodes reachable, we have to wait the
808 * newest journal sequence number we have data for to be written (if it
811 bch2_journal_wait_on_seq(&c->journal, as->journal_seq, &as->cl);
814 static void interior_update_flush(struct journal *j,
815 struct journal_entry_pin *pin, u64 seq)
817 struct btree_update *as =
818 container_of(pin, struct btree_update, journal);
820 bch2_journal_flush_seq_async(j, as->journal_seq, NULL);
823 static void btree_update_reparent(struct btree_update *as,
824 struct btree_update *child)
826 struct bch_fs *c = as->c;
829 child->mode = BTREE_INTERIOR_UPDATING_AS;
830 child->parent_as = as;
831 closure_get(&as->cl);
834 * When we write a new btree root, we have to drop our journal pin
835 * _before_ the new nodes are technically reachable; see
836 * btree_update_nodes_written().
838 * This goes for journal pins that are recursively blocked on us - so,
839 * just transfer the journal pin to the new interior update so
840 * btree_update_nodes_written() can drop it.
842 bch2_journal_pin_add_if_older(&c->journal, &child->journal,
843 &as->journal, interior_update_flush);
844 bch2_journal_pin_drop(&c->journal, &child->journal);
846 as->journal_seq = max(as->journal_seq, child->journal_seq);
849 static void btree_update_updated_root(struct btree_update *as)
851 struct bch_fs *c = as->c;
852 struct btree_root *r = &c->btree_roots[as->btree_id];
854 mutex_lock(&c->btree_interior_update_lock);
856 BUG_ON(as->mode != BTREE_INTERIOR_NO_UPDATE);
859 * Old root might not be persistent yet - if so, redirect its
860 * btree_update operation to point to us:
863 btree_update_reparent(as, r->as);
865 as->mode = BTREE_INTERIOR_UPDATING_ROOT;
869 mutex_unlock(&c->btree_interior_update_lock);
872 * When we're rewriting nodes and updating interior nodes, there's an
873 * issue with updates that haven't been written in the journal getting
874 * mixed together with older data - see btree_update_updated_node()
875 * for the explanation.
877 * However, this doesn't affect us when we're writing a new btree root -
878 * because to make that new root reachable we have to write out a new
879 * journal entry, which must necessarily be newer than as->journal_seq.
883 static void btree_node_will_make_reachable(struct btree_update *as,
886 struct bch_fs *c = as->c;
888 mutex_lock(&c->btree_interior_update_lock);
889 BUG_ON(as->nr_new_nodes >= ARRAY_SIZE(as->new_nodes));
890 BUG_ON(b->will_make_reachable);
892 as->new_nodes[as->nr_new_nodes++] = b;
893 b->will_make_reachable = 1UL|(unsigned long) as;
895 closure_get(&as->cl);
896 mutex_unlock(&c->btree_interior_update_lock);
899 static void btree_update_drop_new_node(struct bch_fs *c, struct btree *b)
901 struct btree_update *as;
905 mutex_lock(&c->btree_interior_update_lock);
906 v = xchg(&b->will_make_reachable, 0);
907 as = (struct btree_update *) (v & ~1UL);
910 mutex_unlock(&c->btree_interior_update_lock);
914 for (i = 0; i < as->nr_new_nodes; i++)
915 if (as->new_nodes[i] == b)
920 array_remove_item(as->new_nodes, as->nr_new_nodes, i);
921 mutex_unlock(&c->btree_interior_update_lock);
924 closure_put(&as->cl);
927 static void btree_interior_update_add_node_reference(struct btree_update *as,
930 struct bch_fs *c = as->c;
931 struct pending_btree_node_free *d;
933 mutex_lock(&c->btree_interior_update_lock);
935 /* Add this node to the list of nodes being freed: */
936 BUG_ON(as->nr_pending >= ARRAY_SIZE(as->pending));
938 d = &as->pending[as->nr_pending++];
939 d->index_update_done = false;
940 d->seq = b->data->keys.seq;
941 d->btree_id = b->btree_id;
943 bkey_copy(&d->key, &b->key);
945 mutex_unlock(&c->btree_interior_update_lock);
949 * @b is being split/rewritten: it may have pointers to not-yet-written btree
950 * nodes and thus outstanding btree_updates - redirect @b's
951 * btree_updates to point to this btree_update:
953 void bch2_btree_interior_update_will_free_node(struct btree_update *as,
956 struct bch_fs *c = as->c;
957 struct closure *cl, *cl_n;
958 struct btree_update *p, *n;
959 struct btree_write *w;
962 set_btree_node_dying(b);
964 if (btree_node_fake(b))
967 btree_interior_update_add_node_reference(as, b);
970 * Does this node have data that hasn't been written in the journal?
972 * If so, we have to wait for the corresponding journal entry to be
973 * written before making the new nodes reachable - we can't just carry
974 * over the bset->journal_seq tracking, since we'll be mixing those keys
975 * in with keys that aren't in the journal anymore:
978 as->journal_seq = max(as->journal_seq,
979 le64_to_cpu(bset(b, t)->journal_seq));
981 mutex_lock(&c->btree_interior_update_lock);
984 * Does this node have any btree_update operations preventing
985 * it from being written?
987 * If so, redirect them to point to this btree_update: we can
988 * write out our new nodes, but we won't make them visible until those
989 * operations complete
991 list_for_each_entry_safe(p, n, &b->write_blocked, write_blocked_list) {
992 list_del(&p->write_blocked_list);
993 btree_update_reparent(as, p);
996 clear_btree_node_dirty(b);
997 clear_btree_node_need_write(b);
998 w = btree_current_write(b);
1001 * Does this node have any btree_update operations waiting on this node
1004 * If so, wake them up when this btree_update operation is reachable:
1006 llist_for_each_entry_safe(cl, cl_n, llist_del_all(&w->wait.list), list)
1007 llist_add(&cl->list, &as->wait.list);
1010 * Does this node have unwritten data that has a pin on the journal?
1012 * If so, transfer that pin to the btree_update operation -
1013 * note that if we're freeing multiple nodes, we only need to keep the
1014 * oldest pin of any of the nodes we're freeing. We'll release the pin
1015 * when the new nodes are persistent and reachable on disk:
1017 bch2_journal_pin_add_if_older(&c->journal, &w->journal,
1018 &as->journal, interior_update_flush);
1019 bch2_journal_pin_drop(&c->journal, &w->journal);
1021 w = btree_prev_write(b);
1022 bch2_journal_pin_add_if_older(&c->journal, &w->journal,
1023 &as->journal, interior_update_flush);
1024 bch2_journal_pin_drop(&c->journal, &w->journal);
1026 mutex_unlock(&c->btree_interior_update_lock);
1029 void bch2_btree_update_done(struct btree_update *as)
1031 BUG_ON(as->mode == BTREE_INTERIOR_NO_UPDATE);
1033 bch2_btree_reserve_put(as->c, as->reserve);
1036 continue_at(&as->cl, btree_update_nodes_written, system_freezable_wq);
1039 struct btree_update *
1040 bch2_btree_update_start(struct bch_fs *c, enum btree_id id,
1041 unsigned nr_nodes, unsigned flags,
1044 struct btree_reserve *reserve;
1045 struct btree_update *as;
1047 if (unlikely(!percpu_ref_tryget(&c->writes)))
1048 return ERR_PTR(-EROFS);
1050 reserve = bch2_btree_reserve_get(c, nr_nodes, flags, cl);
1051 if (IS_ERR(reserve)) {
1052 percpu_ref_put(&c->writes);
1053 return ERR_CAST(reserve);
1056 as = mempool_alloc(&c->btree_interior_update_pool, GFP_NOIO);
1057 memset(as, 0, sizeof(*as));
1058 closure_init(&as->cl, NULL);
1060 as->mode = BTREE_INTERIOR_NO_UPDATE;
1062 as->reserve = reserve;
1063 INIT_LIST_HEAD(&as->write_blocked_list);
1065 bch2_keylist_init(&as->parent_keys, as->inline_keys);
1067 mutex_lock(&c->btree_interior_update_lock);
1068 list_add_tail(&as->list, &c->btree_interior_update_list);
1069 mutex_unlock(&c->btree_interior_update_lock);
1074 /* Btree root updates: */
1076 static void __bch2_btree_set_root_inmem(struct bch_fs *c, struct btree *b)
1078 /* Root nodes cannot be reaped */
1079 mutex_lock(&c->btree_cache.lock);
1080 list_del_init(&b->list);
1081 mutex_unlock(&c->btree_cache.lock);
1083 mutex_lock(&c->btree_root_lock);
1084 BUG_ON(btree_node_root(c, b) &&
1085 (b->level < btree_node_root(c, b)->level ||
1086 !btree_node_dying(btree_node_root(c, b))));
1088 btree_node_root(c, b) = b;
1089 mutex_unlock(&c->btree_root_lock);
1091 bch2_recalc_btree_reserve(c);
1094 static void bch2_btree_set_root_inmem(struct btree_update *as, struct btree *b)
1096 struct bch_fs *c = as->c;
1097 struct btree *old = btree_node_root(c, b);
1098 struct bch_fs_usage stats = { 0 };
1100 __bch2_btree_set_root_inmem(c, b);
1102 bch2_mark_key(c, bkey_i_to_s_c(&b->key),
1103 c->opts.btree_node_size, true,
1104 gc_pos_btree_root(b->btree_id),
1107 if (old && !btree_node_fake(old))
1108 bch2_btree_node_free_index(as, NULL,
1109 bkey_i_to_s_c(&old->key),
1111 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1112 gc_pos_btree_root(b->btree_id));
1115 static void bch2_btree_set_root_ondisk(struct bch_fs *c, struct btree *b, int rw)
1117 struct btree_root *r = &c->btree_roots[b->btree_id];
1119 mutex_lock(&c->btree_root_lock);
1122 bkey_copy(&r->key, &b->key);
1123 r->level = b->level;
1126 c->btree_roots_dirty = true;
1128 mutex_unlock(&c->btree_root_lock);
1132 * bch_btree_set_root - update the root in memory and on disk
1134 * To ensure forward progress, the current task must not be holding any
1135 * btree node write locks. However, you must hold an intent lock on the
1138 * Note: This allocates a journal entry but doesn't add any keys to
1139 * it. All the btree roots are part of every journal write, so there
1140 * is nothing new to be done. This just guarantees that there is a
1143 static void bch2_btree_set_root(struct btree_update *as, struct btree *b,
1144 struct btree_iter *iter)
1146 struct bch_fs *c = as->c;
1149 trace_btree_set_root(c, b);
1150 BUG_ON(!b->written);
1152 old = btree_node_root(c, b);
1155 * Ensure no one is using the old root while we switch to the
1158 bch2_btree_node_lock_write(old, iter);
1160 bch2_btree_set_root_inmem(as, b);
1162 btree_update_updated_root(as);
1165 * Unlock old root after new root is visible:
1167 * The new root isn't persistent, but that's ok: we still have
1168 * an intent lock on the new root, and any updates that would
1169 * depend on the new root would have to update the new root.
1171 bch2_btree_node_unlock_write(old, iter);
1174 /* Interior node updates: */
1176 static void bch2_insert_fixup_btree_ptr(struct btree_update *as, struct btree *b,
1177 struct btree_iter *iter,
1178 struct bkey_i *insert,
1179 struct btree_node_iter *node_iter)
1181 struct bch_fs *c = as->c;
1182 struct bch_fs_usage stats = { 0 };
1183 struct bkey_packed *k;
1186 BUG_ON(insert->k.u64s > bch_btree_keys_u64s_remaining(c, b));
1188 if (bkey_extent_is_data(&insert->k))
1189 bch2_mark_key(c, bkey_i_to_s_c(insert),
1190 c->opts.btree_node_size, true,
1191 gc_pos_btree_node(b), &stats, 0, 0);
1193 while ((k = bch2_btree_node_iter_peek_all(node_iter, b)) &&
1194 !btree_iter_pos_cmp_packed(b, &insert->k.p, k, false))
1195 bch2_btree_node_iter_advance(node_iter, b);
1198 * If we're overwriting, look up pending delete and mark so that gc
1199 * marks it on the pending delete list:
1201 if (k && !bkey_cmp_packed(b, k, &insert->k))
1202 bch2_btree_node_free_index(as, b,
1203 bkey_disassemble(b, k, &tmp),
1206 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1207 gc_pos_btree_node(b));
1209 bch2_btree_bset_insert_key(iter, b, node_iter, insert);
1210 set_btree_node_dirty(b);
1211 set_btree_node_need_write(b);
1215 * Move keys from n1 (original replacement node, now lower node) to n2 (higher
1218 static struct btree *__btree_split_node(struct btree_update *as,
1220 struct btree_iter *iter)
1222 size_t nr_packed = 0, nr_unpacked = 0;
1224 struct bset *set1, *set2;
1225 struct bkey_packed *k, *prev = NULL;
1227 n2 = bch2_btree_node_alloc(as, n1->level);
1229 n2->data->max_key = n1->data->max_key;
1230 n2->data->format = n1->format;
1231 SET_BTREE_NODE_SEQ(n2->data, BTREE_NODE_SEQ(n1->data));
1232 n2->key.k.p = n1->key.k.p;
1234 btree_node_set_format(n2, n2->data->format);
1236 set1 = btree_bset_first(n1);
1237 set2 = btree_bset_first(n2);
1240 * Has to be a linear search because we don't have an auxiliary
1245 if (bkey_next(k) == vstruct_last(set1))
1247 if (k->_data - set1->_data >= (le16_to_cpu(set1->u64s) * 3) / 5)
1261 n1->key.k.p = bkey_unpack_pos(n1, prev);
1262 n1->data->max_key = n1->key.k.p;
1264 btree_type_successor(n1->btree_id, n1->key.k.p);
1266 set2->u64s = cpu_to_le16((u64 *) vstruct_end(set1) - (u64 *) k);
1267 set1->u64s = cpu_to_le16(le16_to_cpu(set1->u64s) - le16_to_cpu(set2->u64s));
1269 set_btree_bset_end(n1, n1->set);
1270 set_btree_bset_end(n2, n2->set);
1272 n2->nr.live_u64s = le16_to_cpu(set2->u64s);
1273 n2->nr.bset_u64s[0] = le16_to_cpu(set2->u64s);
1274 n2->nr.packed_keys = n1->nr.packed_keys - nr_packed;
1275 n2->nr.unpacked_keys = n1->nr.unpacked_keys - nr_unpacked;
1277 n1->nr.live_u64s = le16_to_cpu(set1->u64s);
1278 n1->nr.bset_u64s[0] = le16_to_cpu(set1->u64s);
1279 n1->nr.packed_keys = nr_packed;
1280 n1->nr.unpacked_keys = nr_unpacked;
1282 BUG_ON(!set1->u64s);
1283 BUG_ON(!set2->u64s);
1285 memcpy_u64s(set2->start,
1287 le16_to_cpu(set2->u64s));
1289 btree_node_reset_sib_u64s(n1);
1290 btree_node_reset_sib_u64s(n2);
1292 bch2_verify_btree_nr_keys(n1);
1293 bch2_verify_btree_nr_keys(n2);
1296 btree_node_interior_verify(n1);
1297 btree_node_interior_verify(n2);
1304 * For updates to interior nodes, we've got to do the insert before we split
1305 * because the stuff we're inserting has to be inserted atomically. Post split,
1306 * the keys might have to go in different nodes and the split would no longer be
1309 * Worse, if the insert is from btree node coalescing, if we do the insert after
1310 * we do the split (and pick the pivot) - the pivot we pick might be between
1311 * nodes that were coalesced, and thus in the middle of a child node post
1314 static void btree_split_insert_keys(struct btree_update *as, struct btree *b,
1315 struct btree_iter *iter,
1316 struct keylist *keys)
1318 struct btree_node_iter node_iter;
1319 struct bkey_i *k = bch2_keylist_front(keys);
1320 struct bkey_packed *p;
1323 BUG_ON(btree_node_type(b) != BKEY_TYPE_BTREE);
1325 bch2_btree_node_iter_init(&node_iter, b, k->k.p, false, false);
1327 while (!bch2_keylist_empty(keys)) {
1328 k = bch2_keylist_front(keys);
1330 BUG_ON(bch_keylist_u64s(keys) >
1331 bch_btree_keys_u64s_remaining(as->c, b));
1332 BUG_ON(bkey_cmp(k->k.p, b->data->min_key) < 0);
1333 BUG_ON(bkey_cmp(k->k.p, b->data->max_key) > 0);
1335 bch2_insert_fixup_btree_ptr(as, b, iter, k, &node_iter);
1336 bch2_keylist_pop_front(keys);
1340 * We can't tolerate whiteouts here - with whiteouts there can be
1341 * duplicate keys, and it would be rather bad if we picked a duplicate
1344 i = btree_bset_first(b);
1346 while (p != vstruct_last(i))
1347 if (bkey_deleted(p)) {
1348 le16_add_cpu(&i->u64s, -p->u64s);
1349 set_btree_bset_end(b, b->set);
1350 memmove_u64s_down(p, bkey_next(p),
1351 (u64 *) vstruct_last(i) -
1356 BUG_ON(b->nsets != 1 ||
1357 b->nr.live_u64s != le16_to_cpu(btree_bset_first(b)->u64s));
1359 btree_node_interior_verify(b);
1362 static void btree_split(struct btree_update *as, struct btree *b,
1363 struct btree_iter *iter, struct keylist *keys)
1365 struct bch_fs *c = as->c;
1366 struct btree *parent = btree_node_parent(iter, b);
1367 struct btree *n1, *n2 = NULL, *n3 = NULL;
1368 u64 start_time = local_clock();
1370 BUG_ON(!parent && (b != btree_node_root(c, b)));
1371 BUG_ON(!btree_node_intent_locked(iter, btree_node_root(c, b)->level));
1373 bch2_btree_interior_update_will_free_node(as, b);
1375 n1 = bch2_btree_node_alloc_replacement(as, b);
1378 btree_split_insert_keys(as, n1, iter, keys);
1380 if (vstruct_blocks(n1->data, c->block_bits) > BTREE_SPLIT_THRESHOLD(c)) {
1381 trace_btree_split(c, b);
1383 n2 = __btree_split_node(as, n1, iter);
1385 bch2_btree_build_aux_trees(n2);
1386 bch2_btree_build_aux_trees(n1);
1387 six_unlock_write(&n2->lock);
1388 six_unlock_write(&n1->lock);
1390 bch2_btree_node_write(c, n2, SIX_LOCK_intent);
1393 * Note that on recursive parent_keys == keys, so we
1394 * can't start adding new keys to parent_keys before emptying it
1395 * out (which we did with btree_split_insert_keys() above)
1397 bch2_keylist_add(&as->parent_keys, &n1->key);
1398 bch2_keylist_add(&as->parent_keys, &n2->key);
1401 /* Depth increases, make a new root */
1402 n3 = __btree_root_alloc(as, b->level + 1);
1404 n3->sib_u64s[0] = U16_MAX;
1405 n3->sib_u64s[1] = U16_MAX;
1407 btree_split_insert_keys(as, n3, iter, &as->parent_keys);
1409 bch2_btree_node_write(c, n3, SIX_LOCK_intent);
1412 trace_btree_compact(c, b);
1414 bch2_btree_build_aux_trees(n1);
1415 six_unlock_write(&n1->lock);
1417 bch2_keylist_add(&as->parent_keys, &n1->key);
1420 bch2_btree_node_write(c, n1, SIX_LOCK_intent);
1422 /* New nodes all written, now make them visible: */
1425 /* Split a non root node */
1426 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1428 bch2_btree_set_root(as, n3, iter);
1430 /* Root filled up but didn't need to be split */
1431 bch2_btree_set_root(as, n1, iter);
1434 bch2_btree_open_bucket_put(c, n1);
1436 bch2_btree_open_bucket_put(c, n2);
1438 bch2_btree_open_bucket_put(c, n3);
1441 * Note - at this point other linked iterators could still have @b read
1442 * locked; we're depending on the bch2_btree_iter_node_replace() calls
1443 * below removing all references to @b so we don't return with other
1444 * iterators pointing to a node they have locked that's been freed.
1446 * We have to free the node first because the bch2_iter_node_replace()
1447 * calls will drop _our_ iterator's reference - and intent lock - to @b.
1449 bch2_btree_node_free_inmem(c, b, iter);
1451 /* Successful split, update the iterator to point to the new nodes: */
1454 bch2_btree_iter_node_replace(iter, n3);
1456 bch2_btree_iter_node_replace(iter, n2);
1457 bch2_btree_iter_node_replace(iter, n1);
1459 bch2_time_stats_update(&c->btree_split_time, start_time);
1463 bch2_btree_insert_keys_interior(struct btree_update *as, struct btree *b,
1464 struct btree_iter *iter, struct keylist *keys)
1466 struct btree_iter *linked;
1467 struct btree_node_iter node_iter;
1468 struct bkey_i *insert = bch2_keylist_front(keys);
1469 struct bkey_packed *k;
1471 /* Don't screw up @iter's position: */
1472 node_iter = iter->l[b->level].iter;
1475 * btree_split(), btree_gc_coalesce() will insert keys before
1476 * the iterator's current position - they know the keys go in
1477 * the node the iterator points to:
1479 while ((k = bch2_btree_node_iter_prev_all(&node_iter, b)) &&
1480 (bkey_cmp_packed(b, k, &insert->k) >= 0))
1483 while (!bch2_keylist_empty(keys)) {
1484 insert = bch2_keylist_front(keys);
1486 bch2_insert_fixup_btree_ptr(as, b, iter, insert, &node_iter);
1487 bch2_keylist_pop_front(keys);
1490 btree_update_updated_node(as, b);
1492 for_each_linked_btree_node(iter, b, linked)
1493 bch2_btree_node_iter_peek(&linked->l[b->level].iter, b);
1494 bch2_btree_node_iter_peek(&iter->l[b->level].iter, b);
1496 bch2_btree_iter_verify(iter, b);
1500 * bch_btree_insert_node - insert bkeys into a given btree node
1502 * @iter: btree iterator
1503 * @keys: list of keys to insert
1504 * @hook: insert callback
1505 * @persistent: if not null, @persistent will wait on journal write
1507 * Inserts as many keys as it can into a given btree node, splitting it if full.
1508 * If a split occurred, this function will return early. This can only happen
1509 * for leaf nodes -- inserts into interior nodes have to be atomic.
1511 void bch2_btree_insert_node(struct btree_update *as, struct btree *b,
1512 struct btree_iter *iter, struct keylist *keys)
1514 struct bch_fs *c = as->c;
1515 int old_u64s = le16_to_cpu(btree_bset_last(b)->u64s);
1516 int old_live_u64s = b->nr.live_u64s;
1517 int live_u64s_added, u64s_added;
1519 BUG_ON(!btree_node_intent_locked(iter, btree_node_root(c, b)->level));
1521 BUG_ON(!as || as->b);
1522 bch2_verify_keylist_sorted(keys);
1524 if (as->must_rewrite)
1527 bch2_btree_node_lock_for_insert(c, b, iter);
1529 if (!bch2_btree_node_insert_fits(c, b, bch_keylist_u64s(keys))) {
1530 bch2_btree_node_unlock_write(b, iter);
1534 bch2_btree_insert_keys_interior(as, b, iter, keys);
1536 live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
1537 u64s_added = (int) le16_to_cpu(btree_bset_last(b)->u64s) - old_u64s;
1539 if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
1540 b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
1541 if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
1542 b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);
1544 if (u64s_added > live_u64s_added &&
1545 bch2_maybe_compact_whiteouts(c, b))
1546 bch2_btree_iter_reinit_node(iter, b);
1548 bch2_btree_node_unlock_write(b, iter);
1550 btree_node_interior_verify(b);
1552 bch2_foreground_maybe_merge(c, iter, b->level);
1555 btree_split(as, b, iter, keys);
1558 int bch2_btree_split_leaf(struct bch_fs *c, struct btree_iter *iter,
1559 unsigned btree_reserve_flags)
1561 struct btree *b = iter->l[0].b;
1562 struct btree_update *as;
1567 * We already have a disk reservation and open buckets pinned; this
1568 * allocation must not block:
1570 if (iter->btree_id == BTREE_ID_EXTENTS)
1571 btree_reserve_flags |= BTREE_INSERT_USE_RESERVE;
1573 closure_init_stack(&cl);
1575 /* Hack, because gc and splitting nodes doesn't mix yet: */
1576 if (!down_read_trylock(&c->gc_lock)) {
1577 bch2_btree_iter_unlock(iter);
1578 down_read(&c->gc_lock);
1580 if (btree_iter_linked(iter))
1585 * XXX: figure out how far we might need to split,
1586 * instead of locking/reserving all the way to the root:
1588 if (!bch2_btree_iter_set_locks_want(iter, U8_MAX)) {
1593 as = bch2_btree_update_start(c, iter->btree_id,
1594 btree_update_reserve_required(c, b),
1595 btree_reserve_flags, &cl);
1598 if (ret == -EAGAIN) {
1599 bch2_btree_iter_unlock(iter);
1600 up_read(&c->gc_lock);
1607 btree_split(as, b, iter, NULL);
1608 bch2_btree_update_done(as);
1610 bch2_btree_iter_set_locks_want(iter, 1);
1612 up_read(&c->gc_lock);
1617 int __bch2_foreground_maybe_merge(struct bch_fs *c,
1618 struct btree_iter *iter,
1620 enum btree_node_sibling sib)
1622 struct btree_update *as;
1623 struct bkey_format_state new_s;
1624 struct bkey_format new_f;
1625 struct bkey_i delete;
1626 struct btree *b, *m, *n, *prev, *next, *parent;
1631 closure_init_stack(&cl);
1633 if (!bch2_btree_node_relock(iter, level))
1636 b = iter->l[level].b;
1638 parent = btree_node_parent(iter, b);
1642 if (b->sib_u64s[sib] > BTREE_FOREGROUND_MERGE_THRESHOLD(c))
1645 /* XXX: can't be holding read locks */
1646 m = bch2_btree_node_get_sibling(c, iter, b, sib);
1652 /* NULL means no sibling: */
1654 b->sib_u64s[sib] = U16_MAX;
1658 if (sib == btree_prev_sib) {
1666 bch2_bkey_format_init(&new_s);
1667 __bch2_btree_calc_format(&new_s, b);
1668 __bch2_btree_calc_format(&new_s, m);
1669 new_f = bch2_bkey_format_done(&new_s);
1671 sib_u64s = btree_node_u64s_with_format(b, &new_f) +
1672 btree_node_u64s_with_format(m, &new_f);
1674 if (sib_u64s > BTREE_FOREGROUND_MERGE_HYSTERESIS(c)) {
1675 sib_u64s -= BTREE_FOREGROUND_MERGE_HYSTERESIS(c);
1677 sib_u64s += BTREE_FOREGROUND_MERGE_HYSTERESIS(c);
1680 sib_u64s = min(sib_u64s, btree_max_u64s(c));
1681 b->sib_u64s[sib] = sib_u64s;
1683 if (b->sib_u64s[sib] > BTREE_FOREGROUND_MERGE_THRESHOLD(c)) {
1684 six_unlock_intent(&m->lock);
1688 /* We're changing btree topology, doesn't mix with gc: */
1689 if (!down_read_trylock(&c->gc_lock)) {
1690 six_unlock_intent(&m->lock);
1691 bch2_btree_iter_unlock(iter);
1693 down_read(&c->gc_lock);
1694 up_read(&c->gc_lock);
1699 if (!bch2_btree_iter_set_locks_want(iter, U8_MAX)) {
1704 as = bch2_btree_update_start(c, iter->btree_id,
1705 btree_update_reserve_required(c, b),
1706 BTREE_INSERT_NOFAIL|
1707 BTREE_INSERT_USE_RESERVE,
1714 bch2_btree_interior_update_will_free_node(as, b);
1715 bch2_btree_interior_update_will_free_node(as, m);
1717 n = bch2_btree_node_alloc(as, b->level);
1719 n->data->min_key = prev->data->min_key;
1720 n->data->max_key = next->data->max_key;
1721 n->data->format = new_f;
1722 n->key.k.p = next->key.k.p;
1724 btree_node_set_format(n, new_f);
1726 bch2_btree_sort_into(c, n, prev);
1727 bch2_btree_sort_into(c, n, next);
1729 bch2_btree_build_aux_trees(n);
1730 six_unlock_write(&n->lock);
1732 bkey_init(&delete.k);
1733 delete.k.p = prev->key.k.p;
1734 bch2_keylist_add(&as->parent_keys, &delete);
1735 bch2_keylist_add(&as->parent_keys, &n->key);
1737 bch2_btree_node_write(c, n, SIX_LOCK_intent);
1739 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1741 bch2_btree_open_bucket_put(c, n);
1742 bch2_btree_node_free_inmem(c, b, iter);
1743 bch2_btree_node_free_inmem(c, m, iter);
1744 bch2_btree_iter_node_replace(iter, n);
1746 bch2_btree_iter_verify(iter, n);
1748 bch2_btree_update_done(as);
1750 if (ret != -EINTR && ret != -EAGAIN)
1751 bch2_btree_iter_set_locks_want(iter, 1);
1752 six_unlock_intent(&m->lock);
1753 up_read(&c->gc_lock);
1755 if (ret == -EAGAIN || ret == -EINTR) {
1756 bch2_btree_iter_unlock(iter);
1762 if (ret == -EINTR) {
1763 ret = bch2_btree_iter_traverse(iter);
1771 static int __btree_node_rewrite(struct bch_fs *c, struct btree_iter *iter,
1772 struct btree *b, unsigned flags,
1775 struct btree *n, *parent = btree_node_parent(iter, b);
1776 struct btree_update *as;
1778 as = bch2_btree_update_start(c, iter->btree_id,
1779 btree_update_reserve_required(c, b),
1782 trace_btree_gc_rewrite_node_fail(c, b);
1786 bch2_btree_interior_update_will_free_node(as, b);
1788 n = bch2_btree_node_alloc_replacement(as, b);
1790 bch2_btree_build_aux_trees(n);
1791 six_unlock_write(&n->lock);
1793 trace_btree_gc_rewrite_node(c, b);
1795 bch2_btree_node_write(c, n, SIX_LOCK_intent);
1798 bch2_btree_insert_node(as, parent, iter,
1799 &keylist_single(&n->key));
1801 bch2_btree_set_root(as, n, iter);
1804 bch2_btree_open_bucket_put(c, n);
1806 bch2_btree_node_free_inmem(c, b, iter);
1808 BUG_ON(!bch2_btree_iter_node_replace(iter, n));
1810 bch2_btree_update_done(as);
1815 * bch_btree_node_rewrite - Rewrite/move a btree node
1817 * Returns 0 on success, -EINTR or -EAGAIN on failure (i.e.
1818 * btree_check_reserve() has to wait)
1820 int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *iter,
1821 __le64 seq, unsigned flags)
1823 unsigned locks_want = iter->locks_want;
1828 flags |= BTREE_INSERT_NOFAIL;
1830 closure_init_stack(&cl);
1832 bch2_btree_iter_set_locks_want(iter, U8_MAX);
1834 if (!(flags & BTREE_INSERT_GC_LOCK_HELD)) {
1835 if (!down_read_trylock(&c->gc_lock)) {
1836 bch2_btree_iter_unlock(iter);
1837 down_read(&c->gc_lock);
1842 ret = bch2_btree_iter_traverse(iter);
1846 b = bch2_btree_iter_peek_node(iter);
1847 if (!b || b->data->keys.seq != seq)
1850 ret = __btree_node_rewrite(c, iter, b, flags, &cl);
1851 if (ret != -EAGAIN &&
1855 bch2_btree_iter_unlock(iter);
1859 bch2_btree_iter_set_locks_want(iter, locks_want);
1861 if (!(flags & BTREE_INSERT_GC_LOCK_HELD))
1862 up_read(&c->gc_lock);
1868 static void __bch2_btree_node_update_key(struct bch_fs *c,
1869 struct btree_update *as,
1870 struct btree_iter *iter,
1871 struct btree *b, struct btree *new_hash,
1872 struct bkey_i_extent *new_key)
1874 struct btree *parent;
1878 * Two corner cases that need to be thought about here:
1880 * @b may not be reachable yet - there might be another interior update
1881 * operation waiting on @b to be written, and we're gonna deliver the
1882 * write completion to that interior update operation _before_
1883 * persisting the new_key update
1885 * That ends up working without us having to do anything special here:
1886 * the reason is, we do kick off (and do the in memory updates) for the
1887 * update for @new_key before we return, creating a new interior_update
1890 * The new interior update operation here will in effect override the
1891 * previous one. The previous one was going to terminate - make @b
1892 * reachable - in one of two ways:
1893 * - updating the btree root pointer
1895 * no, this doesn't work. argh.
1898 if (b->will_make_reachable)
1899 as->must_rewrite = true;
1901 btree_interior_update_add_node_reference(as, b);
1903 parent = btree_node_parent(iter, b);
1906 bkey_copy(&new_hash->key, &new_key->k_i);
1907 ret = bch2_btree_node_hash_insert(&c->btree_cache,
1908 new_hash, b->level, b->btree_id);
1912 bch2_keylist_add(&as->parent_keys, &new_key->k_i);
1913 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1916 mutex_lock(&c->btree_cache.lock);
1917 bch2_btree_node_hash_remove(&c->btree_cache, new_hash);
1919 bch2_btree_node_hash_remove(&c->btree_cache, b);
1921 bkey_copy(&b->key, &new_key->k_i);
1922 ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
1924 mutex_unlock(&c->btree_cache.lock);
1926 bkey_copy(&b->key, &new_key->k_i);
1929 struct bch_fs_usage stats = { 0 };
1931 BUG_ON(btree_node_root(c, b) != b);
1933 bch2_btree_node_lock_write(b, iter);
1935 bch2_mark_key(c, bkey_i_to_s_c(&new_key->k_i),
1936 c->opts.btree_node_size, true,
1937 gc_pos_btree_root(b->btree_id),
1939 bch2_btree_node_free_index(as, NULL,
1940 bkey_i_to_s_c(&b->key),
1942 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1943 gc_pos_btree_root(b->btree_id));
1945 if (PTR_HASH(&new_key->k_i) != PTR_HASH(&b->key)) {
1946 mutex_lock(&c->btree_cache.lock);
1947 bch2_btree_node_hash_remove(&c->btree_cache, b);
1949 bkey_copy(&b->key, &new_key->k_i);
1950 ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
1952 mutex_unlock(&c->btree_cache.lock);
1954 bkey_copy(&b->key, &new_key->k_i);
1957 btree_update_updated_root(as);
1958 bch2_btree_node_unlock_write(b, iter);
1961 bch2_btree_update_done(as);
1964 int bch2_btree_node_update_key(struct bch_fs *c, struct btree_iter *iter,
1965 struct btree *b, struct bkey_i_extent *new_key)
1967 struct btree_update *as = NULL;
1968 struct btree *new_hash = NULL;
1972 closure_init_stack(&cl);
1974 if (!down_read_trylock(&c->gc_lock)) {
1975 bch2_btree_iter_unlock(iter);
1976 down_read(&c->gc_lock);
1978 if (!bch2_btree_iter_relock(iter)) {
1984 /* check PTR_HASH() after @b is locked by btree_iter_traverse(): */
1985 if (PTR_HASH(&new_key->k_i) != PTR_HASH(&b->key)) {
1986 /* bch2_btree_reserve_get will unlock */
1987 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
1991 bch2_btree_iter_unlock(iter);
1992 up_read(&c->gc_lock);
1994 down_read(&c->gc_lock);
1996 if (!bch2_btree_iter_relock(iter))
2000 new_hash = bch2_btree_node_mem_alloc(c);
2003 as = bch2_btree_update_start(c, iter->btree_id,
2004 btree_update_reserve_required(c, b),
2005 BTREE_INSERT_NOFAIL|
2006 BTREE_INSERT_USE_RESERVE|
2007 BTREE_INSERT_USE_ALLOC_RESERVE,
2017 bch2_btree_iter_unlock(iter);
2018 up_read(&c->gc_lock);
2020 down_read(&c->gc_lock);
2022 if (!bch2_btree_iter_relock(iter))
2026 ret = bch2_mark_bkey_replicas(c, BCH_DATA_BTREE,
2027 extent_i_to_s_c(new_key).s_c);
2029 goto err_free_update;
2031 __bch2_btree_node_update_key(c, as, iter, b, new_hash, new_key);
2034 mutex_lock(&c->btree_cache.lock);
2035 list_move(&new_hash->list, &c->btree_cache.freeable);
2036 mutex_unlock(&c->btree_cache.lock);
2038 six_unlock_write(&new_hash->lock);
2039 six_unlock_intent(&new_hash->lock);
2041 up_read(&c->gc_lock);
2045 bch2_btree_update_free(as);
2052 * Only for filesystem bringup, when first reading the btree roots or allocating
2053 * btree roots when initializing a new filesystem:
2055 void bch2_btree_set_root_for_read(struct bch_fs *c, struct btree *b)
2057 BUG_ON(btree_node_root(c, b));
2059 __bch2_btree_set_root_inmem(c, b);
2060 bch2_btree_set_root_ondisk(c, b, READ);
2063 void bch2_btree_root_alloc(struct bch_fs *c, enum btree_id id)
2069 closure_init_stack(&cl);
2072 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
2076 b = bch2_btree_node_mem_alloc(c);
2077 bch2_btree_cache_cannibalize_unlock(c);
2079 set_btree_node_fake(b);
2083 bkey_extent_init(&b->key);
2084 b->key.k.p = POS_MAX;
2085 bkey_i_to_extent(&b->key)->v._data[0] = U64_MAX - id;
2087 bch2_bset_init_first(b, &b->data->keys);
2088 bch2_btree_build_aux_trees(b);
2090 b->data->min_key = POS_MIN;
2091 b->data->max_key = POS_MAX;
2092 b->data->format = bch2_btree_calc_format(b);
2093 btree_node_set_format(b, b->data->format);
2095 ret = bch2_btree_node_hash_insert(&c->btree_cache, b, b->level, b->btree_id);
2098 __bch2_btree_set_root_inmem(c, b);
2100 six_unlock_write(&b->lock);
2101 six_unlock_intent(&b->lock);
2104 ssize_t bch2_btree_updates_print(struct bch_fs *c, char *buf)
2106 char *out = buf, *end = buf + PAGE_SIZE;
2107 struct btree_update *as;
2109 mutex_lock(&c->btree_interior_update_lock);
2110 list_for_each_entry(as, &c->btree_interior_update_list, list)
2111 out += scnprintf(out, end - out, "%p m %u w %u r %u j %llu\n",
2115 atomic_read(&as->cl.remaining) & CLOSURE_REMAINING_MASK,
2116 bch2_journal_pin_seq(&c->journal, &as->journal));
2117 mutex_unlock(&c->btree_interior_update_lock);
2122 size_t bch2_btree_interior_updates_nr_pending(struct bch_fs *c)
2125 struct list_head *i;
2127 mutex_lock(&c->btree_interior_update_lock);
2128 list_for_each(i, &c->btree_interior_update_list)
2130 mutex_unlock(&c->btree_interior_update_lock);