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"
18 #include <linux/random.h>
19 #include <trace/events/bcachefs.h>
21 static void btree_node_will_make_reachable(struct btree_update *,
23 static void btree_update_drop_new_node(struct bch_fs *, struct btree *);
24 static void bch2_btree_set_root_ondisk(struct bch_fs *, struct btree *, int);
28 static void btree_node_interior_verify(struct btree *b)
30 struct btree_node_iter iter;
31 struct bkey_packed *k;
35 bch2_btree_node_iter_init(&iter, b, b->key.k.p, false, false);
37 BUG_ON(!(k = bch2_btree_node_iter_peek(&iter, b)) ||
38 bkey_cmp_left_packed(b, k, &b->key.k.p));
40 BUG_ON((bch2_btree_node_iter_advance(&iter, b),
41 !bch2_btree_node_iter_end(&iter)));
46 k = bch2_btree_node_iter_peek(&iter, b);
50 msg = "isn't what it should be";
51 if (bkey_cmp_left_packed(b, k, &b->key.k.p))
54 bch2_btree_node_iter_advance(&iter, b);
56 msg = "isn't last key";
57 if (!bch2_btree_node_iter_end(&iter))
61 bch2_dump_btree_node(b);
62 printk(KERN_ERR "last key %llu:%llu %s\n", b->key.k.p.inode,
63 b->key.k.p.offset, msg);
68 /* Calculate ideal packed bkey format for new btree nodes: */
70 void __bch2_btree_calc_format(struct bkey_format_state *s, struct btree *b)
72 struct bkey_packed *k;
76 bch2_bkey_format_add_pos(s, b->data->min_key);
79 for (k = btree_bkey_first(b, t);
80 k != btree_bkey_last(b, t);
82 if (!bkey_whiteout(k)) {
83 uk = bkey_unpack_key(b, k);
84 bch2_bkey_format_add_key(s, &uk);
88 static struct bkey_format bch2_btree_calc_format(struct btree *b)
90 struct bkey_format_state s;
92 bch2_bkey_format_init(&s);
93 __bch2_btree_calc_format(&s, b);
95 return bch2_bkey_format_done(&s);
98 static size_t btree_node_u64s_with_format(struct btree *b,
99 struct bkey_format *new_f)
101 struct bkey_format *old_f = &b->format;
103 /* stupid integer promotion rules */
105 (((int) new_f->key_u64s - old_f->key_u64s) *
106 (int) b->nr.packed_keys) +
107 (((int) new_f->key_u64s - BKEY_U64s) *
108 (int) b->nr.unpacked_keys);
110 BUG_ON(delta + b->nr.live_u64s < 0);
112 return b->nr.live_u64s + delta;
116 * btree_node_format_fits - check if we could rewrite node with a new format
118 * This assumes all keys can pack with the new format -- it just checks if
119 * the re-packed keys would fit inside the node itself.
121 bool bch2_btree_node_format_fits(struct bch_fs *c, struct btree *b,
122 struct bkey_format *new_f)
124 size_t u64s = btree_node_u64s_with_format(b, new_f);
126 return __vstruct_bytes(struct btree_node, u64s) < btree_bytes(c);
129 /* Btree node freeing/allocation: */
131 static bool btree_key_matches(struct bch_fs *c,
132 struct bkey_s_c_extent l,
133 struct bkey_s_c_extent r)
135 const struct bch_extent_ptr *ptr1, *ptr2;
137 extent_for_each_ptr(l, ptr1)
138 extent_for_each_ptr(r, ptr2)
139 if (ptr1->dev == ptr2->dev &&
140 ptr1->gen == ptr2->gen &&
141 ptr1->offset == ptr2->offset)
148 * We're doing the index update that makes @b unreachable, update stuff to
151 * Must be called _before_ btree_update_updated_root() or
152 * btree_update_updated_node:
154 static void bch2_btree_node_free_index(struct btree_update *as, struct btree *b,
156 struct bch_fs_usage *stats)
158 struct bch_fs *c = as->c;
159 struct pending_btree_node_free *d;
163 * btree_update lock is only needed here to avoid racing with
166 mutex_lock(&c->btree_interior_update_lock);
168 for (d = as->pending; d < as->pending + as->nr_pending; d++)
169 if (!bkey_cmp(k.k->p, d->key.k.p) &&
170 btree_key_matches(c, bkey_s_c_to_extent(k),
171 bkey_i_to_s_c_extent(&d->key)))
175 BUG_ON(d->index_update_done);
176 d->index_update_done = true;
179 * Btree nodes are accounted as freed in bch_alloc_stats when they're
180 * freed from the index:
182 replicas = bch2_extent_nr_dirty_ptrs(k);
184 stats->s[replicas - 1].data[S_META] -= c->opts.btree_node_size;
187 * We're dropping @k from the btree, but it's still live until the
188 * index update is persistent so we need to keep a reference around for
189 * mark and sweep to find - that's primarily what the
190 * btree_node_pending_free list is for.
192 * So here (when we set index_update_done = true), we're moving an
193 * existing reference to a different part of the larger "gc keyspace" -
194 * and the new position comes after the old position, since GC marks
195 * the pending free list after it walks the btree.
197 * If we move the reference while mark and sweep is _between_ the old
198 * and the new position, mark and sweep will see the reference twice
199 * and it'll get double accounted - so check for that here and subtract
200 * to cancel out one of mark and sweep's markings if necessary:
204 * bch2_mark_key() compares the current gc pos to the pos we're
205 * moving this reference from, hence one comparison here:
207 if (gc_pos_cmp(c->gc_pos, gc_phase(GC_PHASE_PENDING_DELETE)) < 0) {
208 struct bch_fs_usage tmp = { 0 };
210 bch2_mark_key(c, bkey_i_to_s_c(&d->key),
211 -c->opts.btree_node_size, true, b
212 ? gc_pos_btree_node(b)
213 : gc_pos_btree_root(as->btree_id),
216 * Don't apply tmp - pending deletes aren't tracked in
221 mutex_unlock(&c->btree_interior_update_lock);
224 static void __btree_node_free(struct bch_fs *c, struct btree *b,
225 struct btree_iter *iter)
227 trace_btree_node_free(c, b);
229 BUG_ON(btree_node_dirty(b));
230 BUG_ON(btree_node_need_write(b));
231 BUG_ON(b == btree_node_root(c, b));
233 BUG_ON(!list_empty(&b->write_blocked));
234 BUG_ON(b->will_make_reachable);
236 clear_btree_node_noevict(b);
238 six_lock_write(&b->lock);
240 bch2_btree_node_hash_remove(&c->btree_cache, b);
242 mutex_lock(&c->btree_cache.lock);
243 list_move(&b->list, &c->btree_cache.freeable);
244 mutex_unlock(&c->btree_cache.lock);
247 * By using six_unlock_write() directly instead of
248 * bch2_btree_node_unlock_write(), we don't update the iterator's
249 * sequence numbers and cause future bch2_btree_node_relock() calls to
252 six_unlock_write(&b->lock);
255 void bch2_btree_node_free_never_inserted(struct bch_fs *c, struct btree *b)
257 struct btree_ob_ref ob = b->ob;
259 btree_update_drop_new_node(c, b);
263 clear_btree_node_dirty(b);
265 __btree_node_free(c, b, NULL);
267 bch2_open_bucket_put_refs(c, &ob.nr, ob.refs);
270 void bch2_btree_node_free_inmem(struct bch_fs *c, struct btree *b,
271 struct btree_iter *iter)
274 * Is this a node that isn't reachable on disk yet?
276 * Nodes that aren't reachable yet have writes blocked until they're
277 * reachable - now that we've cancelled any pending writes and moved
278 * things waiting on that write to wait on this update, we can drop this
279 * node from the list of nodes that the other update is making
280 * reachable, prior to freeing it:
282 btree_update_drop_new_node(c, b);
284 bch2_btree_iter_node_drop_linked(iter, b);
286 __btree_node_free(c, b, iter);
288 bch2_btree_iter_node_drop(iter, b);
291 static void bch2_btree_node_free_ondisk(struct bch_fs *c,
292 struct pending_btree_node_free *pending)
294 struct bch_fs_usage stats = { 0 };
296 BUG_ON(!pending->index_update_done);
298 bch2_mark_key(c, bkey_i_to_s_c(&pending->key),
299 -c->opts.btree_node_size, true,
300 gc_phase(GC_PHASE_PENDING_DELETE),
303 * Don't apply stats - pending deletes aren't tracked in
308 void bch2_btree_open_bucket_put(struct bch_fs *c, struct btree *b)
310 bch2_open_bucket_put_refs(c, &b->ob.nr, b->ob.refs);
313 static struct btree *__bch2_btree_node_alloc(struct bch_fs *c,
314 struct disk_reservation *res,
318 struct write_point *wp;
321 struct bkey_i_extent *e;
322 struct btree_ob_ref ob;
323 struct bch_devs_list devs_have = (struct bch_devs_list) { 0 };
325 enum alloc_reserve alloc_reserve;
327 if (flags & BTREE_INSERT_USE_ALLOC_RESERVE) {
329 alloc_reserve = RESERVE_ALLOC;
330 } else if (flags & BTREE_INSERT_USE_RESERVE) {
331 nr_reserve = BTREE_NODE_RESERVE / 2;
332 alloc_reserve = RESERVE_BTREE;
334 nr_reserve = BTREE_NODE_RESERVE;
335 alloc_reserve = RESERVE_NONE;
338 mutex_lock(&c->btree_reserve_cache_lock);
339 if (c->btree_reserve_cache_nr > nr_reserve) {
340 struct btree_alloc *a =
341 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
344 bkey_copy(&tmp.k, &a->k);
345 mutex_unlock(&c->btree_reserve_cache_lock);
348 mutex_unlock(&c->btree_reserve_cache_lock);
351 wp = bch2_alloc_sectors_start(c, NULL,
352 writepoint_ptr(&c->btree_write_point),
355 c->opts.metadata_replicas_required,
356 alloc_reserve, 0, cl);
360 if (wp->sectors_free < c->opts.btree_node_size) {
361 struct open_bucket *ob;
364 writepoint_for_each_ptr(wp, ob, i)
365 if (ob->sectors_free < c->opts.btree_node_size)
366 ob->sectors_free = 0;
368 bch2_alloc_sectors_done(c, wp);
372 e = bkey_extent_init(&tmp.k);
373 bch2_alloc_sectors_append_ptrs(c, wp, e, c->opts.btree_node_size);
376 bch2_open_bucket_get(c, wp, &ob.nr, ob.refs);
377 bch2_alloc_sectors_done(c, wp);
379 b = bch2_btree_node_mem_alloc(c);
381 /* we hold cannibalize_lock: */
385 bkey_copy(&b->key, &tmp.k);
391 static struct btree *bch2_btree_node_alloc(struct btree_update *as, unsigned level)
393 struct bch_fs *c = as->c;
396 BUG_ON(level >= BTREE_MAX_DEPTH);
397 BUG_ON(!as->reserve->nr);
399 b = as->reserve->b[--as->reserve->nr];
401 BUG_ON(bch2_btree_node_hash_insert(&c->btree_cache, b, level, as->btree_id));
403 set_btree_node_accessed(b);
404 set_btree_node_dirty(b);
406 bch2_bset_init_first(b, &b->data->keys);
407 memset(&b->nr, 0, sizeof(b->nr));
408 b->data->magic = cpu_to_le64(bset_magic(c));
410 SET_BTREE_NODE_ID(b->data, as->btree_id);
411 SET_BTREE_NODE_LEVEL(b->data, level);
412 b->data->ptr = bkey_i_to_extent(&b->key)->v.start->ptr;
414 bch2_btree_build_aux_trees(b);
416 btree_node_will_make_reachable(as, b);
418 trace_btree_node_alloc(c, b);
422 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *as,
424 struct bkey_format format)
428 n = bch2_btree_node_alloc(as, b->level);
430 n->data->min_key = b->data->min_key;
431 n->data->max_key = b->data->max_key;
432 n->data->format = format;
433 SET_BTREE_NODE_SEQ(n->data, BTREE_NODE_SEQ(b->data) + 1);
435 btree_node_set_format(n, format);
437 bch2_btree_sort_into(as->c, n, b);
439 btree_node_reset_sib_u64s(n);
441 n->key.k.p = b->key.k.p;
445 static struct btree *bch2_btree_node_alloc_replacement(struct btree_update *as,
448 struct bkey_format new_f = bch2_btree_calc_format(b);
451 * The keys might expand with the new format - if they wouldn't fit in
452 * the btree node anymore, use the old format for now:
454 if (!bch2_btree_node_format_fits(as->c, b, &new_f))
457 return __bch2_btree_node_alloc_replacement(as, b, new_f);
460 static struct btree *__btree_root_alloc(struct btree_update *as, unsigned level)
462 struct btree *b = bch2_btree_node_alloc(as, level);
464 b->data->min_key = POS_MIN;
465 b->data->max_key = POS_MAX;
466 b->data->format = bch2_btree_calc_format(b);
467 b->key.k.p = POS_MAX;
469 btree_node_set_format(b, b->data->format);
470 bch2_btree_build_aux_trees(b);
472 six_unlock_write(&b->lock);
477 static void bch2_btree_reserve_put(struct bch_fs *c, struct btree_reserve *reserve)
479 bch2_disk_reservation_put(c, &reserve->disk_res);
481 mutex_lock(&c->btree_reserve_cache_lock);
483 while (reserve->nr) {
484 struct btree *b = reserve->b[--reserve->nr];
486 six_unlock_write(&b->lock);
488 if (c->btree_reserve_cache_nr <
489 ARRAY_SIZE(c->btree_reserve_cache)) {
490 struct btree_alloc *a =
491 &c->btree_reserve_cache[c->btree_reserve_cache_nr++];
495 bkey_copy(&a->k, &b->key);
497 bch2_btree_open_bucket_put(c, b);
500 __btree_node_free(c, b, NULL);
502 six_unlock_intent(&b->lock);
505 mutex_unlock(&c->btree_reserve_cache_lock);
507 mempool_free(reserve, &c->btree_reserve_pool);
510 static struct btree_reserve *bch2_btree_reserve_get(struct bch_fs *c,
515 struct btree_reserve *reserve;
517 struct disk_reservation disk_res = { 0, 0 };
518 unsigned sectors = nr_nodes * c->opts.btree_node_size;
519 int ret, disk_res_flags = BCH_DISK_RESERVATION_GC_LOCK_HELD;
521 if (flags & BTREE_INSERT_NOFAIL)
522 disk_res_flags |= BCH_DISK_RESERVATION_NOFAIL;
525 * This check isn't necessary for correctness - it's just to potentially
526 * prevent us from doing a lot of work that'll end up being wasted:
528 ret = bch2_journal_error(&c->journal);
532 if (bch2_disk_reservation_get(c, &disk_res, sectors,
533 c->opts.metadata_replicas,
535 return ERR_PTR(-ENOSPC);
537 BUG_ON(nr_nodes > BTREE_RESERVE_MAX);
540 * Protects reaping from the btree node cache and using the btree node
541 * open bucket reserve:
543 ret = bch2_btree_cache_cannibalize_lock(c, cl);
545 bch2_disk_reservation_put(c, &disk_res);
549 reserve = mempool_alloc(&c->btree_reserve_pool, GFP_NOIO);
551 reserve->disk_res = disk_res;
554 while (reserve->nr < nr_nodes) {
555 b = __bch2_btree_node_alloc(c, &disk_res,
556 flags & BTREE_INSERT_NOWAIT
563 ret = bch2_mark_bkey_replicas(c, BCH_DATA_BTREE,
564 bkey_i_to_s_c(&b->key));
568 reserve->b[reserve->nr++] = b;
571 bch2_btree_cache_cannibalize_unlock(c);
574 bch2_btree_reserve_put(c, reserve);
575 bch2_btree_cache_cannibalize_unlock(c);
576 trace_btree_reserve_get_fail(c, nr_nodes, cl);
580 /* Asynchronous interior node update machinery */
582 static void bch2_btree_update_free(struct btree_update *as)
584 struct bch_fs *c = as->c;
586 BUG_ON(as->nr_new_nodes);
587 BUG_ON(as->nr_pending);
590 bch2_btree_reserve_put(c, as->reserve);
592 mutex_lock(&c->btree_interior_update_lock);
595 closure_debug_destroy(&as->cl);
596 mempool_free(as, &c->btree_interior_update_pool);
597 percpu_ref_put(&c->writes);
599 closure_wake_up(&c->btree_interior_update_wait);
600 mutex_unlock(&c->btree_interior_update_lock);
603 static void btree_update_nodes_reachable(struct closure *cl)
605 struct btree_update *as = container_of(cl, struct btree_update, cl);
606 struct bch_fs *c = as->c;
608 bch2_journal_pin_drop(&c->journal, &as->journal);
610 mutex_lock(&c->btree_interior_update_lock);
612 while (as->nr_new_nodes) {
613 struct btree *b = as->new_nodes[--as->nr_new_nodes];
615 BUG_ON(b->will_make_reachable != (unsigned long) as);
616 b->will_make_reachable = 0;
617 mutex_unlock(&c->btree_interior_update_lock);
620 * b->will_make_reachable prevented it from being written, so
621 * write it now if it needs to be written:
623 six_lock_read(&b->lock);
624 bch2_btree_node_write_cond(c, b, btree_node_need_write(b));
625 six_unlock_read(&b->lock);
626 mutex_lock(&c->btree_interior_update_lock);
629 while (as->nr_pending)
630 bch2_btree_node_free_ondisk(c, &as->pending[--as->nr_pending]);
632 mutex_unlock(&c->btree_interior_update_lock);
634 closure_wake_up(&as->wait);
636 bch2_btree_update_free(as);
639 static void btree_update_wait_on_journal(struct closure *cl)
641 struct btree_update *as = container_of(cl, struct btree_update, cl);
642 struct bch_fs *c = as->c;
645 ret = bch2_journal_open_seq_async(&c->journal, as->journal_seq, cl);
649 continue_at(cl, btree_update_wait_on_journal, system_wq);
651 bch2_journal_flush_seq_async(&c->journal, as->journal_seq, cl);
653 continue_at(cl, btree_update_nodes_reachable, system_wq);
656 static void btree_update_nodes_written(struct closure *cl)
658 struct btree_update *as = container_of(cl, struct btree_update, cl);
659 struct bch_fs *c = as->c;
663 * We did an update to a parent node where the pointers we added pointed
664 * to child nodes that weren't written yet: now, the child nodes have
665 * been written so we can write out the update to the interior node.
668 mutex_lock(&c->btree_interior_update_lock);
669 as->nodes_written = true;
672 case BTREE_INTERIOR_NO_UPDATE:
674 case BTREE_INTERIOR_UPDATING_NODE:
675 /* The usual case: */
676 b = READ_ONCE(as->b);
678 if (!six_trylock_read(&b->lock)) {
679 mutex_unlock(&c->btree_interior_update_lock);
680 six_lock_read(&b->lock);
681 six_unlock_read(&b->lock);
685 BUG_ON(!btree_node_dirty(b));
686 closure_wait(&btree_current_write(b)->wait, cl);
688 list_del(&as->write_blocked_list);
689 mutex_unlock(&c->btree_interior_update_lock);
692 * b->write_blocked prevented it from being written, so
693 * write it now if it needs to be written:
695 bch2_btree_node_write_cond(c, b, true);
696 six_unlock_read(&b->lock);
699 case BTREE_INTERIOR_UPDATING_AS:
701 * The btree node we originally updated has been freed and is
702 * being rewritten - so we need to write anything here, we just
703 * need to signal to that btree_update that it's ok to make the
704 * new replacement node visible:
706 closure_put(&as->parent_as->cl);
709 * and then we have to wait on that btree_update to finish:
711 closure_wait(&as->parent_as->wait, cl);
712 mutex_unlock(&c->btree_interior_update_lock);
715 case BTREE_INTERIOR_UPDATING_ROOT:
716 /* b is the new btree root: */
717 b = READ_ONCE(as->b);
719 if (!six_trylock_read(&b->lock)) {
720 mutex_unlock(&c->btree_interior_update_lock);
721 six_lock_read(&b->lock);
722 six_unlock_read(&b->lock);
726 BUG_ON(c->btree_roots[b->btree_id].as != as);
727 c->btree_roots[b->btree_id].as = NULL;
729 bch2_btree_set_root_ondisk(c, b, WRITE);
732 * We don't have to wait anything anything here (before
733 * btree_update_nodes_reachable frees the old nodes
734 * ondisk) - we've ensured that the very next journal write will
735 * have the pointer to the new root, and before the allocator
736 * can reuse the old nodes it'll have to do a journal commit:
738 six_unlock_read(&b->lock);
739 mutex_unlock(&c->btree_interior_update_lock);
742 * Bit of funny circularity going on here we have to break:
744 * We have to drop our journal pin before writing the journal
745 * entry that points to the new btree root: else, we could
746 * deadlock if the journal currently happens to be full.
748 * This mean we're dropping the journal pin _before_ the new
749 * nodes are technically reachable - but this is safe, because
750 * after the bch2_btree_set_root_ondisk() call above they will
751 * be reachable as of the very next journal write:
753 bch2_journal_pin_drop(&c->journal, &as->journal);
755 as->journal_seq = bch2_journal_last_unwritten_seq(&c->journal);
757 btree_update_wait_on_journal(cl);
761 continue_at(cl, btree_update_nodes_reachable, system_wq);
765 * We're updating @b with pointers to nodes that haven't finished writing yet:
766 * block @b from being written until @as completes
768 static void btree_update_updated_node(struct btree_update *as, struct btree *b)
770 struct bch_fs *c = as->c;
772 mutex_lock(&c->btree_interior_update_lock);
774 BUG_ON(as->mode != BTREE_INTERIOR_NO_UPDATE);
775 BUG_ON(!btree_node_dirty(b));
777 as->mode = BTREE_INTERIOR_UPDATING_NODE;
779 list_add(&as->write_blocked_list, &b->write_blocked);
781 mutex_unlock(&c->btree_interior_update_lock);
784 * In general, when you're staging things in a journal that will later
785 * be written elsewhere, and you also want to guarantee ordering: that
786 * is, if you have updates a, b, c, after a crash you should never see c
787 * and not a or b - there's a problem:
789 * If the final destination of the update(s) (i.e. btree node) can be
790 * written/flushed _before_ the relevant journal entry - oops, that
791 * breaks ordering, since the various leaf nodes can be written in any
794 * Normally we use bset->journal_seq to deal with this - if during
795 * recovery we find a btree node write that's newer than the newest
796 * journal entry, we just ignore it - we don't need it, anything we're
797 * supposed to have (that we reported as completed via fsync()) will
798 * still be in the journal, and as far as the state of the journal is
799 * concerned that btree node write never happened.
801 * That breaks when we're rewriting/splitting/merging nodes, since we're
802 * mixing btree node writes that haven't happened yet with previously
803 * written data that has been reported as completed to the journal.
805 * Thus, before making the new nodes reachable, we have to wait the
806 * newest journal sequence number we have data for to be written (if it
809 bch2_journal_wait_on_seq(&c->journal, as->journal_seq, &as->cl);
812 static void interior_update_flush(struct journal *j,
813 struct journal_entry_pin *pin, u64 seq)
815 struct btree_update *as =
816 container_of(pin, struct btree_update, journal);
818 bch2_journal_flush_seq_async(j, as->journal_seq, NULL);
821 static void btree_update_reparent(struct btree_update *as,
822 struct btree_update *child)
824 struct bch_fs *c = as->c;
827 child->mode = BTREE_INTERIOR_UPDATING_AS;
828 child->parent_as = as;
829 closure_get(&as->cl);
832 * When we write a new btree root, we have to drop our journal pin
833 * _before_ the new nodes are technically reachable; see
834 * btree_update_nodes_written().
836 * This goes for journal pins that are recursively blocked on us - so,
837 * just transfer the journal pin to the new interior update so
838 * btree_update_nodes_written() can drop it.
840 bch2_journal_pin_add_if_older(&c->journal, &child->journal,
841 &as->journal, interior_update_flush);
842 bch2_journal_pin_drop(&c->journal, &child->journal);
844 as->journal_seq = max(as->journal_seq, child->journal_seq);
847 static void btree_update_updated_root(struct btree_update *as)
849 struct bch_fs *c = as->c;
850 struct btree_root *r = &c->btree_roots[as->btree_id];
852 mutex_lock(&c->btree_interior_update_lock);
854 BUG_ON(as->mode != BTREE_INTERIOR_NO_UPDATE);
857 * Old root might not be persistent yet - if so, redirect its
858 * btree_update operation to point to us:
861 btree_update_reparent(as, r->as);
863 as->mode = BTREE_INTERIOR_UPDATING_ROOT;
867 mutex_unlock(&c->btree_interior_update_lock);
870 * When we're rewriting nodes and updating interior nodes, there's an
871 * issue with updates that haven't been written in the journal getting
872 * mixed together with older data - see btree_update_updated_node()
873 * for the explanation.
875 * However, this doesn't affect us when we're writing a new btree root -
876 * because to make that new root reachable we have to write out a new
877 * journal entry, which must necessarily be newer than as->journal_seq.
881 static void btree_node_will_make_reachable(struct btree_update *as,
884 struct bch_fs *c = as->c;
886 mutex_lock(&c->btree_interior_update_lock);
887 BUG_ON(as->nr_new_nodes >= ARRAY_SIZE(as->new_nodes));
888 BUG_ON(b->will_make_reachable);
890 as->new_nodes[as->nr_new_nodes++] = b;
891 b->will_make_reachable = 1UL|(unsigned long) as;
893 closure_get(&as->cl);
894 mutex_unlock(&c->btree_interior_update_lock);
897 static void btree_update_drop_new_node(struct bch_fs *c, struct btree *b)
899 struct btree_update *as;
903 mutex_lock(&c->btree_interior_update_lock);
904 v = xchg(&b->will_make_reachable, 0);
905 as = (struct btree_update *) (v & ~1UL);
908 mutex_unlock(&c->btree_interior_update_lock);
912 for (i = 0; i < as->nr_new_nodes; i++)
913 if (as->new_nodes[i] == b)
918 array_remove_item(as->new_nodes, as->nr_new_nodes, i);
919 mutex_unlock(&c->btree_interior_update_lock);
922 closure_put(&as->cl);
925 static void btree_interior_update_add_node_reference(struct btree_update *as,
928 struct bch_fs *c = as->c;
929 struct pending_btree_node_free *d;
931 mutex_lock(&c->btree_interior_update_lock);
933 /* Add this node to the list of nodes being freed: */
934 BUG_ON(as->nr_pending >= ARRAY_SIZE(as->pending));
936 d = &as->pending[as->nr_pending++];
937 d->index_update_done = false;
938 d->seq = b->data->keys.seq;
939 d->btree_id = b->btree_id;
941 bkey_copy(&d->key, &b->key);
943 mutex_unlock(&c->btree_interior_update_lock);
947 * @b is being split/rewritten: it may have pointers to not-yet-written btree
948 * nodes and thus outstanding btree_updates - redirect @b's
949 * btree_updates to point to this btree_update:
951 void bch2_btree_interior_update_will_free_node(struct btree_update *as,
954 struct bch_fs *c = as->c;
955 struct closure *cl, *cl_n;
956 struct btree_update *p, *n;
957 struct btree_write *w;
960 set_btree_node_dying(b);
962 if (btree_node_fake(b))
965 btree_interior_update_add_node_reference(as, b);
968 * Does this node have data that hasn't been written in the journal?
970 * If so, we have to wait for the corresponding journal entry to be
971 * written before making the new nodes reachable - we can't just carry
972 * over the bset->journal_seq tracking, since we'll be mixing those keys
973 * in with keys that aren't in the journal anymore:
976 as->journal_seq = max(as->journal_seq,
977 le64_to_cpu(bset(b, t)->journal_seq));
979 mutex_lock(&c->btree_interior_update_lock);
982 * Does this node have any btree_update operations preventing
983 * it from being written?
985 * If so, redirect them to point to this btree_update: we can
986 * write out our new nodes, but we won't make them visible until those
987 * operations complete
989 list_for_each_entry_safe(p, n, &b->write_blocked, write_blocked_list) {
990 list_del(&p->write_blocked_list);
991 btree_update_reparent(as, p);
994 clear_btree_node_dirty(b);
995 clear_btree_node_need_write(b);
996 w = btree_current_write(b);
999 * Does this node have any btree_update operations waiting on this node
1002 * If so, wake them up when this btree_update operation is reachable:
1004 llist_for_each_entry_safe(cl, cl_n, llist_del_all(&w->wait.list), list)
1005 llist_add(&cl->list, &as->wait.list);
1008 * Does this node have unwritten data that has a pin on the journal?
1010 * If so, transfer that pin to the btree_update operation -
1011 * note that if we're freeing multiple nodes, we only need to keep the
1012 * oldest pin of any of the nodes we're freeing. We'll release the pin
1013 * when the new nodes are persistent and reachable on disk:
1015 bch2_journal_pin_add_if_older(&c->journal, &w->journal,
1016 &as->journal, interior_update_flush);
1017 bch2_journal_pin_drop(&c->journal, &w->journal);
1019 w = btree_prev_write(b);
1020 bch2_journal_pin_add_if_older(&c->journal, &w->journal,
1021 &as->journal, interior_update_flush);
1022 bch2_journal_pin_drop(&c->journal, &w->journal);
1024 mutex_unlock(&c->btree_interior_update_lock);
1027 void bch2_btree_update_done(struct btree_update *as)
1029 BUG_ON(as->mode == BTREE_INTERIOR_NO_UPDATE);
1031 bch2_btree_reserve_put(as->c, as->reserve);
1034 continue_at(&as->cl, btree_update_nodes_written, system_freezable_wq);
1037 struct btree_update *
1038 bch2_btree_update_start(struct bch_fs *c, enum btree_id id,
1039 unsigned nr_nodes, unsigned flags,
1042 struct btree_reserve *reserve;
1043 struct btree_update *as;
1045 if (unlikely(!percpu_ref_tryget(&c->writes)))
1046 return ERR_PTR(-EROFS);
1048 reserve = bch2_btree_reserve_get(c, nr_nodes, flags, cl);
1049 if (IS_ERR(reserve)) {
1050 percpu_ref_put(&c->writes);
1051 return ERR_CAST(reserve);
1054 as = mempool_alloc(&c->btree_interior_update_pool, GFP_NOIO);
1055 memset(as, 0, sizeof(*as));
1056 closure_init(&as->cl, NULL);
1058 as->mode = BTREE_INTERIOR_NO_UPDATE;
1060 as->reserve = reserve;
1061 INIT_LIST_HEAD(&as->write_blocked_list);
1063 bch2_keylist_init(&as->parent_keys, as->inline_keys);
1065 mutex_lock(&c->btree_interior_update_lock);
1066 list_add_tail(&as->list, &c->btree_interior_update_list);
1067 mutex_unlock(&c->btree_interior_update_lock);
1072 /* Btree root updates: */
1074 static void __bch2_btree_set_root_inmem(struct bch_fs *c, struct btree *b)
1076 /* Root nodes cannot be reaped */
1077 mutex_lock(&c->btree_cache.lock);
1078 list_del_init(&b->list);
1079 mutex_unlock(&c->btree_cache.lock);
1081 mutex_lock(&c->btree_root_lock);
1082 BUG_ON(btree_node_root(c, b) &&
1083 (b->level < btree_node_root(c, b)->level ||
1084 !btree_node_dying(btree_node_root(c, b))));
1086 btree_node_root(c, b) = b;
1087 mutex_unlock(&c->btree_root_lock);
1089 bch2_recalc_btree_reserve(c);
1092 static void bch2_btree_set_root_inmem(struct btree_update *as, struct btree *b)
1094 struct bch_fs *c = as->c;
1095 struct btree *old = btree_node_root(c, b);
1096 struct bch_fs_usage stats = { 0 };
1098 __bch2_btree_set_root_inmem(c, b);
1100 bch2_mark_key(c, bkey_i_to_s_c(&b->key),
1101 c->opts.btree_node_size, true,
1102 gc_pos_btree_root(b->btree_id),
1105 if (old && !btree_node_fake(old))
1106 bch2_btree_node_free_index(as, NULL,
1107 bkey_i_to_s_c(&old->key),
1109 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1110 gc_pos_btree_root(b->btree_id));
1113 static void bch2_btree_set_root_ondisk(struct bch_fs *c, struct btree *b, int rw)
1115 struct btree_root *r = &c->btree_roots[b->btree_id];
1117 mutex_lock(&c->btree_root_lock);
1120 bkey_copy(&r->key, &b->key);
1121 r->level = b->level;
1124 c->btree_roots_dirty = true;
1126 mutex_unlock(&c->btree_root_lock);
1130 * bch_btree_set_root - update the root in memory and on disk
1132 * To ensure forward progress, the current task must not be holding any
1133 * btree node write locks. However, you must hold an intent lock on the
1136 * Note: This allocates a journal entry but doesn't add any keys to
1137 * it. All the btree roots are part of every journal write, so there
1138 * is nothing new to be done. This just guarantees that there is a
1141 static void bch2_btree_set_root(struct btree_update *as, struct btree *b,
1142 struct btree_iter *iter)
1144 struct bch_fs *c = as->c;
1147 trace_btree_set_root(c, b);
1148 BUG_ON(!b->written);
1150 old = btree_node_root(c, b);
1153 * Ensure no one is using the old root while we switch to the
1156 bch2_btree_node_lock_write(old, iter);
1158 bch2_btree_set_root_inmem(as, b);
1160 btree_update_updated_root(as);
1163 * Unlock old root after new root is visible:
1165 * The new root isn't persistent, but that's ok: we still have
1166 * an intent lock on the new root, and any updates that would
1167 * depend on the new root would have to update the new root.
1169 bch2_btree_node_unlock_write(old, iter);
1172 /* Interior node updates: */
1174 static void bch2_insert_fixup_btree_ptr(struct btree_update *as, struct btree *b,
1175 struct btree_iter *iter,
1176 struct bkey_i *insert,
1177 struct btree_node_iter *node_iter)
1179 struct bch_fs *c = as->c;
1180 struct bch_fs_usage stats = { 0 };
1181 struct bkey_packed *k;
1184 BUG_ON(insert->k.u64s > bch_btree_keys_u64s_remaining(c, b));
1186 if (bkey_extent_is_data(&insert->k))
1187 bch2_mark_key(c, bkey_i_to_s_c(insert),
1188 c->opts.btree_node_size, true,
1189 gc_pos_btree_node(b), &stats, 0, 0);
1191 while ((k = bch2_btree_node_iter_peek_all(node_iter, b)) &&
1192 !btree_iter_pos_cmp_packed(b, &insert->k.p, k, false))
1193 bch2_btree_node_iter_advance(node_iter, b);
1196 * If we're overwriting, look up pending delete and mark so that gc
1197 * marks it on the pending delete list:
1199 if (k && !bkey_cmp_packed(b, k, &insert->k))
1200 bch2_btree_node_free_index(as, b,
1201 bkey_disassemble(b, k, &tmp),
1204 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1205 gc_pos_btree_node(b));
1207 bch2_btree_bset_insert_key(iter, b, node_iter, insert);
1208 set_btree_node_dirty(b);
1209 set_btree_node_need_write(b);
1213 * Move keys from n1 (original replacement node, now lower node) to n2 (higher
1216 static struct btree *__btree_split_node(struct btree_update *as,
1218 struct btree_iter *iter)
1220 size_t nr_packed = 0, nr_unpacked = 0;
1222 struct bset *set1, *set2;
1223 struct bkey_packed *k, *prev = NULL;
1225 n2 = bch2_btree_node_alloc(as, n1->level);
1227 n2->data->max_key = n1->data->max_key;
1228 n2->data->format = n1->format;
1229 SET_BTREE_NODE_SEQ(n2->data, BTREE_NODE_SEQ(n1->data));
1230 n2->key.k.p = n1->key.k.p;
1232 btree_node_set_format(n2, n2->data->format);
1234 set1 = btree_bset_first(n1);
1235 set2 = btree_bset_first(n2);
1238 * Has to be a linear search because we don't have an auxiliary
1243 if (bkey_next(k) == vstruct_last(set1))
1245 if (k->_data - set1->_data >= (le16_to_cpu(set1->u64s) * 3) / 5)
1259 n1->key.k.p = bkey_unpack_pos(n1, prev);
1260 n1->data->max_key = n1->key.k.p;
1262 btree_type_successor(n1->btree_id, n1->key.k.p);
1264 set2->u64s = cpu_to_le16((u64 *) vstruct_end(set1) - (u64 *) k);
1265 set1->u64s = cpu_to_le16(le16_to_cpu(set1->u64s) - le16_to_cpu(set2->u64s));
1267 set_btree_bset_end(n1, n1->set);
1268 set_btree_bset_end(n2, n2->set);
1270 n2->nr.live_u64s = le16_to_cpu(set2->u64s);
1271 n2->nr.bset_u64s[0] = le16_to_cpu(set2->u64s);
1272 n2->nr.packed_keys = n1->nr.packed_keys - nr_packed;
1273 n2->nr.unpacked_keys = n1->nr.unpacked_keys - nr_unpacked;
1275 n1->nr.live_u64s = le16_to_cpu(set1->u64s);
1276 n1->nr.bset_u64s[0] = le16_to_cpu(set1->u64s);
1277 n1->nr.packed_keys = nr_packed;
1278 n1->nr.unpacked_keys = nr_unpacked;
1280 BUG_ON(!set1->u64s);
1281 BUG_ON(!set2->u64s);
1283 memcpy_u64s(set2->start,
1285 le16_to_cpu(set2->u64s));
1287 btree_node_reset_sib_u64s(n1);
1288 btree_node_reset_sib_u64s(n2);
1290 bch2_verify_btree_nr_keys(n1);
1291 bch2_verify_btree_nr_keys(n2);
1294 btree_node_interior_verify(n1);
1295 btree_node_interior_verify(n2);
1302 * For updates to interior nodes, we've got to do the insert before we split
1303 * because the stuff we're inserting has to be inserted atomically. Post split,
1304 * the keys might have to go in different nodes and the split would no longer be
1307 * Worse, if the insert is from btree node coalescing, if we do the insert after
1308 * we do the split (and pick the pivot) - the pivot we pick might be between
1309 * nodes that were coalesced, and thus in the middle of a child node post
1312 static void btree_split_insert_keys(struct btree_update *as, struct btree *b,
1313 struct btree_iter *iter,
1314 struct keylist *keys)
1316 struct btree_node_iter node_iter;
1317 struct bkey_i *k = bch2_keylist_front(keys);
1318 struct bkey_packed *p;
1321 BUG_ON(btree_node_type(b) != BKEY_TYPE_BTREE);
1323 bch2_btree_node_iter_init(&node_iter, b, k->k.p, false, false);
1325 while (!bch2_keylist_empty(keys)) {
1326 k = bch2_keylist_front(keys);
1328 BUG_ON(bch_keylist_u64s(keys) >
1329 bch_btree_keys_u64s_remaining(as->c, b));
1330 BUG_ON(bkey_cmp(k->k.p, b->data->min_key) < 0);
1331 BUG_ON(bkey_cmp(k->k.p, b->data->max_key) > 0);
1333 bch2_insert_fixup_btree_ptr(as, b, iter, k, &node_iter);
1334 bch2_keylist_pop_front(keys);
1338 * We can't tolerate whiteouts here - with whiteouts there can be
1339 * duplicate keys, and it would be rather bad if we picked a duplicate
1342 i = btree_bset_first(b);
1344 while (p != vstruct_last(i))
1345 if (bkey_deleted(p)) {
1346 le16_add_cpu(&i->u64s, -p->u64s);
1347 set_btree_bset_end(b, b->set);
1348 memmove_u64s_down(p, bkey_next(p),
1349 (u64 *) vstruct_last(i) -
1354 BUG_ON(b->nsets != 1 ||
1355 b->nr.live_u64s != le16_to_cpu(btree_bset_first(b)->u64s));
1357 btree_node_interior_verify(b);
1360 static void btree_split(struct btree_update *as, struct btree *b,
1361 struct btree_iter *iter, struct keylist *keys)
1363 struct bch_fs *c = as->c;
1364 struct btree *parent = btree_node_parent(iter, b);
1365 struct btree *n1, *n2 = NULL, *n3 = NULL;
1366 u64 start_time = local_clock();
1368 BUG_ON(!parent && (b != btree_node_root(c, b)));
1369 BUG_ON(!btree_node_intent_locked(iter, btree_node_root(c, b)->level));
1371 bch2_btree_interior_update_will_free_node(as, b);
1373 n1 = bch2_btree_node_alloc_replacement(as, b);
1376 btree_split_insert_keys(as, n1, iter, keys);
1378 if (vstruct_blocks(n1->data, c->block_bits) > BTREE_SPLIT_THRESHOLD(c)) {
1379 trace_btree_split(c, b);
1381 n2 = __btree_split_node(as, n1, iter);
1383 bch2_btree_build_aux_trees(n2);
1384 bch2_btree_build_aux_trees(n1);
1385 six_unlock_write(&n2->lock);
1386 six_unlock_write(&n1->lock);
1388 bch2_btree_node_write(c, n2, SIX_LOCK_intent);
1391 * Note that on recursive parent_keys == keys, so we
1392 * can't start adding new keys to parent_keys before emptying it
1393 * out (which we did with btree_split_insert_keys() above)
1395 bch2_keylist_add(&as->parent_keys, &n1->key);
1396 bch2_keylist_add(&as->parent_keys, &n2->key);
1399 /* Depth increases, make a new root */
1400 n3 = __btree_root_alloc(as, b->level + 1);
1402 n3->sib_u64s[0] = U16_MAX;
1403 n3->sib_u64s[1] = U16_MAX;
1405 btree_split_insert_keys(as, n3, iter, &as->parent_keys);
1407 bch2_btree_node_write(c, n3, SIX_LOCK_intent);
1410 trace_btree_compact(c, b);
1412 bch2_btree_build_aux_trees(n1);
1413 six_unlock_write(&n1->lock);
1415 bch2_keylist_add(&as->parent_keys, &n1->key);
1418 bch2_btree_node_write(c, n1, SIX_LOCK_intent);
1420 /* New nodes all written, now make them visible: */
1423 /* Split a non root node */
1424 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1426 bch2_btree_set_root(as, n3, iter);
1428 /* Root filled up but didn't need to be split */
1429 bch2_btree_set_root(as, n1, iter);
1432 bch2_btree_open_bucket_put(c, n1);
1434 bch2_btree_open_bucket_put(c, n2);
1436 bch2_btree_open_bucket_put(c, n3);
1439 * Note - at this point other linked iterators could still have @b read
1440 * locked; we're depending on the bch2_btree_iter_node_replace() calls
1441 * below removing all references to @b so we don't return with other
1442 * iterators pointing to a node they have locked that's been freed.
1444 * We have to free the node first because the bch2_iter_node_replace()
1445 * calls will drop _our_ iterator's reference - and intent lock - to @b.
1447 bch2_btree_node_free_inmem(c, b, iter);
1449 /* Successful split, update the iterator to point to the new nodes: */
1452 bch2_btree_iter_node_replace(iter, n3);
1454 bch2_btree_iter_node_replace(iter, n2);
1455 bch2_btree_iter_node_replace(iter, n1);
1457 bch2_time_stats_update(&c->btree_split_time, start_time);
1461 bch2_btree_insert_keys_interior(struct btree_update *as, struct btree *b,
1462 struct btree_iter *iter, struct keylist *keys)
1464 struct btree_iter *linked;
1465 struct btree_node_iter node_iter;
1466 struct bkey_i *insert = bch2_keylist_front(keys);
1467 struct bkey_packed *k;
1469 /* Don't screw up @iter's position: */
1470 node_iter = iter->l[b->level].iter;
1473 * btree_split(), btree_gc_coalesce() will insert keys before
1474 * the iterator's current position - they know the keys go in
1475 * the node the iterator points to:
1477 while ((k = bch2_btree_node_iter_prev_all(&node_iter, b)) &&
1478 (bkey_cmp_packed(b, k, &insert->k) >= 0))
1481 while (!bch2_keylist_empty(keys)) {
1482 insert = bch2_keylist_front(keys);
1484 bch2_insert_fixup_btree_ptr(as, b, iter, insert, &node_iter);
1485 bch2_keylist_pop_front(keys);
1488 btree_update_updated_node(as, b);
1490 for_each_linked_btree_node(iter, b, linked)
1491 bch2_btree_node_iter_peek(&linked->l[b->level].iter, b);
1492 bch2_btree_node_iter_peek(&iter->l[b->level].iter, b);
1494 bch2_btree_iter_verify(iter, b);
1498 * bch_btree_insert_node - insert bkeys into a given btree node
1500 * @iter: btree iterator
1501 * @keys: list of keys to insert
1502 * @hook: insert callback
1503 * @persistent: if not null, @persistent will wait on journal write
1505 * Inserts as many keys as it can into a given btree node, splitting it if full.
1506 * If a split occurred, this function will return early. This can only happen
1507 * for leaf nodes -- inserts into interior nodes have to be atomic.
1509 void bch2_btree_insert_node(struct btree_update *as, struct btree *b,
1510 struct btree_iter *iter, struct keylist *keys)
1512 struct bch_fs *c = as->c;
1513 int old_u64s = le16_to_cpu(btree_bset_last(b)->u64s);
1514 int old_live_u64s = b->nr.live_u64s;
1515 int live_u64s_added, u64s_added;
1517 BUG_ON(!btree_node_intent_locked(iter, btree_node_root(c, b)->level));
1519 BUG_ON(!as || as->b);
1520 bch2_verify_keylist_sorted(keys);
1522 if (as->must_rewrite)
1525 bch2_btree_node_lock_for_insert(c, b, iter);
1527 if (!bch2_btree_node_insert_fits(c, b, bch_keylist_u64s(keys))) {
1528 bch2_btree_node_unlock_write(b, iter);
1532 bch2_btree_insert_keys_interior(as, b, iter, keys);
1534 live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
1535 u64s_added = (int) le16_to_cpu(btree_bset_last(b)->u64s) - old_u64s;
1537 if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
1538 b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
1539 if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
1540 b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);
1542 if (u64s_added > live_u64s_added &&
1543 bch2_maybe_compact_whiteouts(c, b))
1544 bch2_btree_iter_reinit_node(iter, b);
1546 bch2_btree_node_unlock_write(b, iter);
1548 btree_node_interior_verify(b);
1550 bch2_foreground_maybe_merge(c, iter, b->level);
1553 btree_split(as, b, iter, keys);
1556 int bch2_btree_split_leaf(struct bch_fs *c, struct btree_iter *iter,
1557 unsigned btree_reserve_flags)
1559 struct btree *b = iter->l[0].b;
1560 struct btree_update *as;
1565 * We already have a disk reservation and open buckets pinned; this
1566 * allocation must not block:
1568 if (iter->btree_id == BTREE_ID_EXTENTS)
1569 btree_reserve_flags |= BTREE_INSERT_USE_RESERVE;
1571 closure_init_stack(&cl);
1573 /* Hack, because gc and splitting nodes doesn't mix yet: */
1574 if (!down_read_trylock(&c->gc_lock)) {
1575 bch2_btree_iter_unlock(iter);
1576 down_read(&c->gc_lock);
1578 if (btree_iter_linked(iter))
1583 * XXX: figure out how far we might need to split,
1584 * instead of locking/reserving all the way to the root:
1586 if (!bch2_btree_iter_set_locks_want(iter, U8_MAX)) {
1591 as = bch2_btree_update_start(c, iter->btree_id,
1592 btree_update_reserve_required(c, b),
1593 btree_reserve_flags, &cl);
1596 if (ret == -EAGAIN) {
1597 bch2_btree_iter_unlock(iter);
1598 up_read(&c->gc_lock);
1605 btree_split(as, b, iter, NULL);
1606 bch2_btree_update_done(as);
1608 bch2_btree_iter_set_locks_want(iter, 1);
1610 up_read(&c->gc_lock);
1615 int __bch2_foreground_maybe_merge(struct bch_fs *c,
1616 struct btree_iter *iter,
1618 enum btree_node_sibling sib)
1620 struct btree_update *as;
1621 struct bkey_format_state new_s;
1622 struct bkey_format new_f;
1623 struct bkey_i delete;
1624 struct btree *b, *m, *n, *prev, *next, *parent;
1629 closure_init_stack(&cl);
1631 if (!bch2_btree_node_relock(iter, level))
1634 b = iter->l[level].b;
1636 parent = btree_node_parent(iter, b);
1640 if (b->sib_u64s[sib] > BTREE_FOREGROUND_MERGE_THRESHOLD(c))
1643 /* XXX: can't be holding read locks */
1644 m = bch2_btree_node_get_sibling(c, iter, b, sib);
1650 /* NULL means no sibling: */
1652 b->sib_u64s[sib] = U16_MAX;
1656 if (sib == btree_prev_sib) {
1664 bch2_bkey_format_init(&new_s);
1665 __bch2_btree_calc_format(&new_s, b);
1666 __bch2_btree_calc_format(&new_s, m);
1667 new_f = bch2_bkey_format_done(&new_s);
1669 sib_u64s = btree_node_u64s_with_format(b, &new_f) +
1670 btree_node_u64s_with_format(m, &new_f);
1672 if (sib_u64s > BTREE_FOREGROUND_MERGE_HYSTERESIS(c)) {
1673 sib_u64s -= BTREE_FOREGROUND_MERGE_HYSTERESIS(c);
1675 sib_u64s += BTREE_FOREGROUND_MERGE_HYSTERESIS(c);
1678 sib_u64s = min(sib_u64s, btree_max_u64s(c));
1679 b->sib_u64s[sib] = sib_u64s;
1681 if (b->sib_u64s[sib] > BTREE_FOREGROUND_MERGE_THRESHOLD(c)) {
1682 six_unlock_intent(&m->lock);
1686 /* We're changing btree topology, doesn't mix with gc: */
1687 if (!down_read_trylock(&c->gc_lock)) {
1688 six_unlock_intent(&m->lock);
1689 bch2_btree_iter_unlock(iter);
1691 down_read(&c->gc_lock);
1692 up_read(&c->gc_lock);
1697 if (!bch2_btree_iter_set_locks_want(iter, U8_MAX)) {
1702 as = bch2_btree_update_start(c, iter->btree_id,
1703 btree_update_reserve_required(c, b),
1704 BTREE_INSERT_NOFAIL|
1705 BTREE_INSERT_USE_RESERVE,
1712 bch2_btree_interior_update_will_free_node(as, b);
1713 bch2_btree_interior_update_will_free_node(as, m);
1715 n = bch2_btree_node_alloc(as, b->level);
1717 n->data->min_key = prev->data->min_key;
1718 n->data->max_key = next->data->max_key;
1719 n->data->format = new_f;
1720 n->key.k.p = next->key.k.p;
1722 btree_node_set_format(n, new_f);
1724 bch2_btree_sort_into(c, n, prev);
1725 bch2_btree_sort_into(c, n, next);
1727 bch2_btree_build_aux_trees(n);
1728 six_unlock_write(&n->lock);
1730 bkey_init(&delete.k);
1731 delete.k.p = prev->key.k.p;
1732 bch2_keylist_add(&as->parent_keys, &delete);
1733 bch2_keylist_add(&as->parent_keys, &n->key);
1735 bch2_btree_node_write(c, n, SIX_LOCK_intent);
1737 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1739 bch2_btree_open_bucket_put(c, n);
1740 bch2_btree_node_free_inmem(c, b, iter);
1741 bch2_btree_node_free_inmem(c, m, iter);
1742 bch2_btree_iter_node_replace(iter, n);
1744 bch2_btree_iter_verify(iter, n);
1746 bch2_btree_update_done(as);
1748 if (ret != -EINTR && ret != -EAGAIN)
1749 bch2_btree_iter_set_locks_want(iter, 1);
1750 six_unlock_intent(&m->lock);
1751 up_read(&c->gc_lock);
1753 if (ret == -EAGAIN || ret == -EINTR) {
1754 bch2_btree_iter_unlock(iter);
1760 if (ret == -EINTR) {
1761 ret = bch2_btree_iter_traverse(iter);
1769 static int __btree_node_rewrite(struct bch_fs *c, struct btree_iter *iter,
1770 struct btree *b, unsigned flags,
1773 struct btree *n, *parent = btree_node_parent(iter, b);
1774 struct btree_update *as;
1776 as = bch2_btree_update_start(c, iter->btree_id,
1777 btree_update_reserve_required(c, b),
1780 trace_btree_gc_rewrite_node_fail(c, b);
1784 bch2_btree_interior_update_will_free_node(as, b);
1786 n = bch2_btree_node_alloc_replacement(as, b);
1788 bch2_btree_build_aux_trees(n);
1789 six_unlock_write(&n->lock);
1791 trace_btree_gc_rewrite_node(c, b);
1793 bch2_btree_node_write(c, n, SIX_LOCK_intent);
1796 bch2_btree_insert_node(as, parent, iter,
1797 &keylist_single(&n->key));
1799 bch2_btree_set_root(as, n, iter);
1802 bch2_btree_open_bucket_put(c, n);
1804 bch2_btree_node_free_inmem(c, b, iter);
1806 BUG_ON(!bch2_btree_iter_node_replace(iter, n));
1808 bch2_btree_update_done(as);
1813 * bch_btree_node_rewrite - Rewrite/move a btree node
1815 * Returns 0 on success, -EINTR or -EAGAIN on failure (i.e.
1816 * btree_check_reserve() has to wait)
1818 int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *iter,
1819 __le64 seq, unsigned flags)
1821 unsigned locks_want = iter->locks_want;
1826 flags |= BTREE_INSERT_NOFAIL;
1828 closure_init_stack(&cl);
1830 bch2_btree_iter_set_locks_want(iter, U8_MAX);
1832 if (!(flags & BTREE_INSERT_GC_LOCK_HELD)) {
1833 if (!down_read_trylock(&c->gc_lock)) {
1834 bch2_btree_iter_unlock(iter);
1835 down_read(&c->gc_lock);
1840 ret = bch2_btree_iter_traverse(iter);
1844 b = bch2_btree_iter_peek_node(iter);
1845 if (!b || b->data->keys.seq != seq)
1848 ret = __btree_node_rewrite(c, iter, b, flags, &cl);
1849 if (ret != -EAGAIN &&
1853 bch2_btree_iter_unlock(iter);
1857 bch2_btree_iter_set_locks_want(iter, locks_want);
1859 if (!(flags & BTREE_INSERT_GC_LOCK_HELD))
1860 up_read(&c->gc_lock);
1866 static void __bch2_btree_node_update_key(struct bch_fs *c,
1867 struct btree_update *as,
1868 struct btree_iter *iter,
1869 struct btree *b, struct btree *new_hash,
1870 struct bkey_i_extent *new_key)
1872 struct btree *parent;
1876 * Two corner cases that need to be thought about here:
1878 * @b may not be reachable yet - there might be another interior update
1879 * operation waiting on @b to be written, and we're gonna deliver the
1880 * write completion to that interior update operation _before_
1881 * persisting the new_key update
1883 * That ends up working without us having to do anything special here:
1884 * the reason is, we do kick off (and do the in memory updates) for the
1885 * update for @new_key before we return, creating a new interior_update
1888 * The new interior update operation here will in effect override the
1889 * previous one. The previous one was going to terminate - make @b
1890 * reachable - in one of two ways:
1891 * - updating the btree root pointer
1893 * no, this doesn't work. argh.
1896 if (b->will_make_reachable)
1897 as->must_rewrite = true;
1899 btree_interior_update_add_node_reference(as, b);
1901 parent = btree_node_parent(iter, b);
1904 bkey_copy(&new_hash->key, &new_key->k_i);
1905 ret = bch2_btree_node_hash_insert(&c->btree_cache,
1906 new_hash, b->level, b->btree_id);
1910 bch2_keylist_add(&as->parent_keys, &new_key->k_i);
1911 bch2_btree_insert_node(as, parent, iter, &as->parent_keys);
1914 mutex_lock(&c->btree_cache.lock);
1915 bch2_btree_node_hash_remove(&c->btree_cache, new_hash);
1917 bch2_btree_node_hash_remove(&c->btree_cache, b);
1919 bkey_copy(&b->key, &new_key->k_i);
1920 ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
1922 mutex_unlock(&c->btree_cache.lock);
1924 bkey_copy(&b->key, &new_key->k_i);
1927 struct bch_fs_usage stats = { 0 };
1929 BUG_ON(btree_node_root(c, b) != b);
1931 bch2_btree_node_lock_write(b, iter);
1933 bch2_mark_key(c, bkey_i_to_s_c(&new_key->k_i),
1934 c->opts.btree_node_size, true,
1935 gc_pos_btree_root(b->btree_id),
1937 bch2_btree_node_free_index(as, NULL,
1938 bkey_i_to_s_c(&b->key),
1940 bch2_fs_usage_apply(c, &stats, &as->reserve->disk_res,
1941 gc_pos_btree_root(b->btree_id));
1943 if (PTR_HASH(&new_key->k_i) != PTR_HASH(&b->key)) {
1944 mutex_lock(&c->btree_cache.lock);
1945 bch2_btree_node_hash_remove(&c->btree_cache, b);
1947 bkey_copy(&b->key, &new_key->k_i);
1948 ret = __bch2_btree_node_hash_insert(&c->btree_cache, b);
1950 mutex_unlock(&c->btree_cache.lock);
1952 bkey_copy(&b->key, &new_key->k_i);
1955 btree_update_updated_root(as);
1956 bch2_btree_node_unlock_write(b, iter);
1959 bch2_btree_update_done(as);
1962 int bch2_btree_node_update_key(struct bch_fs *c, struct btree_iter *iter,
1963 struct btree *b, struct bkey_i_extent *new_key)
1965 struct btree_update *as = NULL;
1966 struct btree *new_hash = NULL;
1970 closure_init_stack(&cl);
1972 if (!down_read_trylock(&c->gc_lock)) {
1973 bch2_btree_iter_unlock(iter);
1974 down_read(&c->gc_lock);
1976 if (!bch2_btree_iter_relock(iter)) {
1982 /* check PTR_HASH() after @b is locked by btree_iter_traverse(): */
1983 if (PTR_HASH(&new_key->k_i) != PTR_HASH(&b->key)) {
1984 /* bch2_btree_reserve_get will unlock */
1985 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
1989 bch2_btree_iter_unlock(iter);
1990 up_read(&c->gc_lock);
1992 down_read(&c->gc_lock);
1994 if (!bch2_btree_iter_relock(iter))
1998 new_hash = bch2_btree_node_mem_alloc(c);
2001 as = bch2_btree_update_start(c, iter->btree_id,
2002 btree_update_reserve_required(c, b),
2003 BTREE_INSERT_NOFAIL|
2004 BTREE_INSERT_USE_RESERVE|
2005 BTREE_INSERT_USE_ALLOC_RESERVE,
2015 bch2_btree_iter_unlock(iter);
2016 up_read(&c->gc_lock);
2018 down_read(&c->gc_lock);
2020 if (!bch2_btree_iter_relock(iter))
2024 ret = bch2_mark_bkey_replicas(c, BCH_DATA_BTREE,
2025 extent_i_to_s_c(new_key).s_c);
2027 goto err_free_update;
2029 __bch2_btree_node_update_key(c, as, iter, b, new_hash, new_key);
2032 mutex_lock(&c->btree_cache.lock);
2033 list_move(&new_hash->list, &c->btree_cache.freeable);
2034 mutex_unlock(&c->btree_cache.lock);
2036 six_unlock_write(&new_hash->lock);
2037 six_unlock_intent(&new_hash->lock);
2039 up_read(&c->gc_lock);
2043 bch2_btree_update_free(as);
2050 * Only for filesystem bringup, when first reading the btree roots or allocating
2051 * btree roots when initializing a new filesystem:
2053 void bch2_btree_set_root_for_read(struct bch_fs *c, struct btree *b)
2055 BUG_ON(btree_node_root(c, b));
2057 __bch2_btree_set_root_inmem(c, b);
2058 bch2_btree_set_root_ondisk(c, b, READ);
2061 void bch2_btree_root_alloc(struct bch_fs *c, enum btree_id id)
2067 closure_init_stack(&cl);
2070 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
2074 b = bch2_btree_node_mem_alloc(c);
2075 bch2_btree_cache_cannibalize_unlock(c);
2077 set_btree_node_fake(b);
2081 bkey_extent_init(&b->key);
2082 b->key.k.p = POS_MAX;
2083 bkey_i_to_extent(&b->key)->v._data[0] = U64_MAX - id;
2085 bch2_bset_init_first(b, &b->data->keys);
2086 bch2_btree_build_aux_trees(b);
2088 b->data->min_key = POS_MIN;
2089 b->data->max_key = POS_MAX;
2090 b->data->format = bch2_btree_calc_format(b);
2091 btree_node_set_format(b, b->data->format);
2093 ret = bch2_btree_node_hash_insert(&c->btree_cache, b, b->level, b->btree_id);
2096 __bch2_btree_set_root_inmem(c, b);
2098 six_unlock_write(&b->lock);
2099 six_unlock_intent(&b->lock);
2102 ssize_t bch2_btree_updates_print(struct bch_fs *c, char *buf)
2104 char *out = buf, *end = buf + PAGE_SIZE;
2105 struct btree_update *as;
2107 mutex_lock(&c->btree_interior_update_lock);
2108 list_for_each_entry(as, &c->btree_interior_update_list, list)
2109 out += scnprintf(out, end - out, "%p m %u w %u r %u j %llu\n",
2113 atomic_read(&as->cl.remaining) & CLOSURE_REMAINING_MASK,
2114 bch2_journal_pin_seq(&c->journal, &as->journal));
2115 mutex_unlock(&c->btree_interior_update_lock);