]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_update_interior.c
Update bcachefs sources to 50ac18afbb bcachefs: Fix an uninitialized variable
[bcachefs-tools-debian] / libbcachefs / btree_update_interior.c
index dfff972551ee6e84bf73554597c8dbd676e22b59..47568a0bc5f18c456612b5cdd04ded6a3d06b632 100644 (file)
@@ -16,6 +16,7 @@
 #include "journal.h"
 #include "journal_reclaim.h"
 #include "keylist.h"
+#include "recovery.h"
 #include "replicas.h"
 #include "super-io.h"
 
@@ -44,7 +45,7 @@ static void btree_node_interior_verify(struct bch_fs *c, struct btree *b)
 
        BUG_ON(!b->c.level);
 
-       if (!test_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags))
+       if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
                return;
 
        bch2_btree_node_iter_init_from_start(&iter, b);
@@ -223,12 +224,12 @@ retry:
        if (IS_ERR(wp))
                return ERR_CAST(wp);
 
-       if (wp->sectors_free < c->opts.btree_node_size) {
+       if (wp->sectors_free < btree_sectors(c)) {
                struct open_bucket *ob;
                unsigned i;
 
                open_bucket_for_each(c, &wp->ptrs, ob, i)
-                       if (ob->sectors_free < c->opts.btree_node_size)
+                       if (ob->sectors_free < btree_sectors(c))
                                ob->sectors_free = 0;
 
                bch2_alloc_sectors_done(c, wp);
@@ -236,7 +237,7 @@ retry:
        }
 
        bkey_btree_ptr_v2_init(&tmp.k);
-       bch2_alloc_sectors_append_ptrs(c, wp, &tmp.k, c->opts.btree_node_size);
+       bch2_alloc_sectors_append_ptrs(c, wp, &tmp.k, btree_sectors(c), false);
 
        bch2_open_bucket_get(c, wp, &ob);
        bch2_alloc_sectors_done(c, wp);
@@ -455,15 +456,23 @@ static void bch2_btree_update_free(struct btree_update *as)
        bch2_disk_reservation_put(c, &as->disk_res);
        bch2_btree_reserve_put(as);
 
+       bch2_time_stats_update(&c->times[BCH_TIME_btree_interior_update_total],
+                              as->start_time);
+
        mutex_lock(&c->btree_interior_update_lock);
        list_del(&as->unwritten_list);
        list_del(&as->list);
-       mutex_unlock(&c->btree_interior_update_lock);
 
        closure_debug_destroy(&as->cl);
        mempool_free(as, &c->btree_interior_update_pool);
 
+       /*
+        * Have to do the wakeup with btree_interior_update_lock still held,
+        * since being on btree_interior_update_list is our ref on @c:
+        */
        closure_wake_up(&c->btree_interior_update_wait);
+
+       mutex_unlock(&c->btree_interior_update_lock);
 }
 
 static void btree_update_will_delete_key(struct btree_update *as,
@@ -902,6 +911,9 @@ static void bch2_btree_interior_update_will_free_node(struct btree_update *as,
 
 static void bch2_btree_update_done(struct btree_update *as)
 {
+       struct bch_fs *c = as->c;
+       u64 start_time = as->start_time;
+
        BUG_ON(as->mode == BTREE_INTERIOR_NO_UPDATE);
 
        if (as->took_gc_lock)
@@ -912,6 +924,9 @@ static void bch2_btree_update_done(struct btree_update *as)
 
        continue_at(&as->cl, btree_update_set_nodes_written,
                    as->c->btree_interior_update_worker);
+
+       bch2_time_stats_update(&c->times[BCH_TIME_btree_interior_update_foreground],
+                              start_time);
 }
 
 static struct btree_update *
@@ -921,6 +936,7 @@ bch2_btree_update_start(struct btree_trans *trans, struct btree_path *path,
        struct bch_fs *c = trans->c;
        struct btree_update *as;
        struct closure cl;
+       u64 start_time = local_clock();
        int disk_res_flags = (flags & BTREE_INSERT_NOFAIL)
                ? BCH_DISK_RESERVATION_NOFAIL : 0;
        int journal_flags = 0;
@@ -939,7 +955,7 @@ retry:
         * instead of locking/reserving all the way to the root:
         */
        if (!bch2_btree_path_upgrade(trans, path, U8_MAX)) {
-               trace_trans_restart_iter_upgrade(trans->ip, _RET_IP_,
+               trace_trans_restart_iter_upgrade(trans->fn, _RET_IP_,
                                                 path->btree_id, &path->pos);
                ret = btree_trans_restart(trans);
                return ERR_PTR(ret);
@@ -960,6 +976,7 @@ retry:
        memset(as, 0, sizeof(*as));
        closure_init(&as->cl, NULL);
        as->c           = c;
+       as->start_time  = start_time;
        as->mode        = BTREE_INTERIOR_NO_UPDATE;
        as->took_gc_lock = !(flags & BTREE_INSERT_GC_LOCK_HELD);
        as->btree_id    = path->btree_id;
@@ -1002,7 +1019,7 @@ retry:
                                BTREE_UPDATE_JOURNAL_RES,
                                journal_flags);
                if (ret) {
-                       trace_trans_restart_journal_preres_get(trans->ip, _RET_IP_);
+                       trace_trans_restart_journal_preres_get(trans->fn, _RET_IP_);
                        goto err;
                }
 
@@ -1013,7 +1030,7 @@ retry:
        }
 
        ret = bch2_disk_reservation_get(c, &as->disk_res,
-                       nr_nodes * c->opts.btree_node_size,
+                       nr_nodes * btree_sectors(c),
                        c->opts.metadata_replicas,
                        disk_res_flags);
        if (ret)
@@ -1130,6 +1147,9 @@ static void bch2_insert_fixup_btree_ptr(struct btree_update *as,
        BUG_ON(insert->k.type == KEY_TYPE_btree_ptr_v2 &&
               !btree_ptr_sectors_written(insert));
 
+       if (unlikely(!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags)))
+               bch2_journal_key_overwritten(c, b->c.btree_id, b->c.level, insert->k.p);
+
        invalid = bch2_bkey_invalid(c, bkey_i_to_s_c(insert), btree_node_type(b)) ?:
                bch2_bkey_in_btree_node(b, bkey_i_to_s_c(insert));
        if (invalid) {
@@ -1452,7 +1472,9 @@ static void btree_split(struct btree_update *as, struct btree_trans *trans,
 
        bch2_trans_verify_locks(trans);
 
-       bch2_time_stats_update(&c->times[BCH_TIME_btree_node_split],
+       bch2_time_stats_update(&c->times[n2
+                              ? BCH_TIME_btree_node_split
+                              : BCH_TIME_btree_node_compact],
                               start_time);
 }
 
@@ -1573,6 +1595,7 @@ int __bch2_foreground_maybe_merge(struct btree_trans *trans,
        struct btree *b, *m, *n, *prev, *next, *parent;
        struct bpos sib_pos;
        size_t sib_u64s;
+       u64 start_time = local_clock();
        int ret = 0;
 
        BUG_ON(!path->should_be_locked);
@@ -1590,8 +1613,8 @@ int __bch2_foreground_maybe_merge(struct btree_trans *trans,
                ? bpos_predecessor(b->data->min_key)
                : bpos_successor(b->data->max_key);
 
-       sib_path = bch2_path_get(trans, false, path->btree_id, sib_pos,
-                                U8_MAX, level, true, _THIS_IP_);
+       sib_path = bch2_path_get(trans, path->btree_id, sib_pos,
+                                U8_MAX, level, BTREE_ITER_INTENT, _THIS_IP_);
        ret = bch2_btree_path_traverse(trans, sib_path, false);
        if (ret)
                goto err;
@@ -1710,6 +1733,8 @@ int __bch2_foreground_maybe_merge(struct btree_trans *trans,
        six_unlock_intent(&n->c.lock);
 
        bch2_btree_update_done(as);
+
+       bch2_time_stats_update(&c->times[BCH_TIME_btree_node_merge], start_time);
 out:
 err:
        bch2_path_put(trans, sib_path, true);
@@ -1826,9 +1851,6 @@ void bch2_btree_node_rewrite_async(struct bch_fs *c, struct btree *b)
 {
        struct async_btree_rewrite *a;
 
-       if (!test_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags))
-               return;
-
        if (!percpu_ref_tryget(&c->writes))
                return;