]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_locking.c
Update bcachefs sources to 5d0a6c2b32f1 bcachefs: check_directory_structure() can...
[bcachefs-tools-debian] / libbcachefs / btree_locking.c
index 1cdf7d4f9cc764c7138eb117607e664d4e73d2ae..1ed8327a9fa2cc409cba3eb8c2ac1848999c7508 100644 (file)
@@ -4,19 +4,26 @@
 #include "btree_locking.h"
 #include "btree_types.h"
 
-struct lock_class_key bch2_btree_node_lock_key;
+static struct lock_class_key bch2_btree_node_lock_key;
 
-/* Btree node locking: */
+void bch2_btree_lock_init(struct btree_bkey_cached_common *b,
+                         enum six_lock_init_flags flags)
+{
+       __six_lock_init(&b->lock, "b->c.lock", &bch2_btree_node_lock_key, flags);
+       lockdep_set_novalidate_class(&b->lock);
+}
 
-static inline void six_lock_readers_add(struct six_lock *lock, int nr)
+#ifdef CONFIG_LOCKDEP
+void bch2_assert_btree_nodes_not_locked(void)
 {
-       if (lock->readers)
-               this_cpu_add(*lock->readers, nr);
-       else if (nr > 0)
-               atomic64_add(__SIX_VAL(read_lock, nr), &lock->state.counter);
-       else
-               atomic64_sub(__SIX_VAL(read_lock, -nr), &lock->state.counter);
+#if 0
+       //Re-enable when lock_class_is_held() is merged:
+       BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
+#endif
 }
+#endif
+
+/* Btree node locking: */
 
 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
                                                  struct btree_path *skip,
@@ -25,13 +32,14 @@ struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
 {
        struct btree_path *path;
        struct six_lock_count ret;
+       unsigned i;
 
        memset(&ret, 0, sizeof(ret));
 
        if (IS_ERR_OR_NULL(b))
                return ret;
 
-       trans_for_each_path(trans, path)
+       trans_for_each_path(trans, path, i)
                if (path != skip && &path->l[level].b->c == b) {
                        int t = btree_node_locked_type(path, level);
 
@@ -52,117 +60,417 @@ void bch2_btree_node_unlock_write(struct btree_trans *trans,
 
 /* lock */
 
-void __bch2_btree_node_lock_write(struct btree_trans *trans,
-                                 struct btree_bkey_cached_common *b)
+/*
+ * @trans wants to lock @b with type @type
+ */
+struct trans_waiting_for_lock {
+       struct btree_trans              *trans;
+       struct btree_bkey_cached_common *node_want;
+       enum six_lock_type              lock_want;
+
+       /* for iterating over held locks :*/
+       u8                              path_idx;
+       u8                              level;
+       u64                             lock_start_time;
+};
+
+struct lock_graph {
+       struct trans_waiting_for_lock   g[8];
+       unsigned                        nr;
+};
+
+static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
 {
-       int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
+       struct trans_waiting_for_lock *i;
 
-       /*
-        * Must drop our read locks before calling six_lock_write() -
-        * six_unlock() won't do wakeups until the reader count
-        * goes to 0, and it's safe because we have the node intent
-        * locked:
-        */
-       six_lock_readers_add(&b->lock, -readers);
-       btree_node_lock_nopath_nofail(trans, b, SIX_LOCK_write);
-       six_lock_readers_add(&b->lock, readers);
+       prt_printf(out, "Found lock cycle (%u entries):", g->nr);
+       prt_newline(out);
+
+       for (i = g->g; i < g->g + g->nr; i++)
+               bch2_btree_trans_to_text(out, i->trans);
 }
 
-static inline bool path_has_read_locks(struct btree_path *path)
+static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
 {
-       unsigned l;
+       struct trans_waiting_for_lock *i;
+
+       for (i = g->g; i != g->g + g->nr; i++) {
+               struct task_struct *task = i->trans->locking_wait.task;
+               if (i != g->g)
+                       prt_str(out, "<- ");
+               prt_printf(out, "%u ", task ?task->pid : 0);
+       }
+       prt_newline(out);
+}
+
+static void lock_graph_up(struct lock_graph *g)
+{
+       closure_put(&g->g[--g->nr].trans->ref);
+}
 
-       for (l = 0; l < BTREE_MAX_DEPTH; l++)
-               if (btree_node_read_locked(path, l))
+static noinline void lock_graph_pop_all(struct lock_graph *g)
+{
+       while (g->nr)
+               lock_graph_up(g);
+}
+
+static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
+{
+       g->g[g->nr++] = (struct trans_waiting_for_lock) {
+               .trans          = trans,
+               .node_want      = trans->locking,
+               .lock_want      = trans->locking_wait.lock_want,
+       };
+}
+
+static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
+{
+       closure_get(&trans->ref);
+       __lock_graph_down(g, trans);
+}
+
+static bool lock_graph_remove_non_waiters(struct lock_graph *g)
+{
+       struct trans_waiting_for_lock *i;
+
+       for (i = g->g + 1; i < g->g + g->nr; i++)
+               if (i->trans->locking != i->node_want ||
+                   i->trans->locking_wait.start_time != i[-1].lock_start_time) {
+                       while (g->g + g->nr > i)
+                               lock_graph_up(g);
                        return true;
+               }
+
        return false;
 }
 
-/* Slowpath: */
-int __bch2_btree_node_lock(struct btree_trans *trans,
-                          struct btree_path *path,
-                          struct btree_bkey_cached_common *b,
-                          struct bpos pos, unsigned level,
-                          enum six_lock_type type,
-                          six_lock_should_sleep_fn should_sleep_fn, void *p,
-                          unsigned long ip)
+static void trace_would_deadlock(struct lock_graph *g, struct btree_trans *trans,
+                                unsigned long ip)
 {
-       struct btree_path *linked;
-       unsigned reason;
+       struct bch_fs *c = trans->c;
 
-       /* Check if it's safe to block: */
-       trans_for_each_path(trans, linked) {
-               if (!linked->nodes_locked)
-                       continue;
+       count_event(c, trans_restart_would_deadlock);
+
+       if (trace_trans_restart_would_deadlock_enabled()) {
+               struct printbuf buf = PRINTBUF;
+
+               buf.atomic++;
+               print_cycle(&buf, g);
+
+               trace_trans_restart_would_deadlock(trans, ip, buf.buf);
+               printbuf_exit(&buf);
+       }
+}
+
+static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
+{
+       if (i == g->g) {
+               trace_would_deadlock(g, i->trans, _RET_IP_);
+               return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
+       } else {
+               i->trans->lock_must_abort = true;
+               wake_up_process(i->trans->locking_wait.task);
+               return 0;
+       }
+}
 
-               /*
-                * Can't block taking an intent lock if we have _any_ nodes read
-                * locked:
-                *
-                * - Our read lock blocks another thread with an intent lock on
-                *   the same node from getting a write lock, and thus from
-                *   dropping its intent lock
-                *
-                * - And the other thread may have multiple nodes intent locked:
-                *   both the node we want to intent lock, and the node we
-                *   already have read locked - deadlock:
-                */
-               if (type == SIX_LOCK_intent &&
-                   path_has_read_locks(linked)) {
-                       reason = 1;
-                       goto deadlock;
+static int btree_trans_abort_preference(struct btree_trans *trans)
+{
+       if (trans->lock_may_not_fail)
+               return 0;
+       if (trans->locking_wait.lock_want == SIX_LOCK_write)
+               return 1;
+       if (!trans->in_traverse_all)
+               return 2;
+       return 3;
+}
+
+static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
+{
+       struct trans_waiting_for_lock *i, *abort = NULL;
+       unsigned best = 0, pref;
+       int ret;
+
+       if (lock_graph_remove_non_waiters(g))
+               return 0;
+
+       /* Only checking, for debugfs: */
+       if (cycle) {
+               print_cycle(cycle, g);
+               ret = -1;
+               goto out;
+       }
+
+       for (i = g->g; i < g->g + g->nr; i++) {
+               pref = btree_trans_abort_preference(i->trans);
+               if (pref > best) {
+                       abort = i;
+                       best = pref;
                }
+       }
 
-               if (linked->btree_id != path->btree_id) {
-                       if (linked->btree_id < path->btree_id)
-                               continue;
+       if (unlikely(!best)) {
+               struct printbuf buf = PRINTBUF;
+
+               prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
+
+               for (i = g->g; i < g->g + g->nr; i++) {
+                       struct btree_trans *trans = i->trans;
 
-                       reason = 3;
-                       goto deadlock;
+                       bch2_btree_trans_to_text(&buf, trans);
+
+                       prt_printf(&buf, "backtrace:");
+                       prt_newline(&buf);
+                       printbuf_indent_add(&buf, 2);
+                       bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
+                       printbuf_indent_sub(&buf, 2);
+                       prt_newline(&buf);
                }
 
-               /*
-                * Within the same btree, non-cached paths come before cached
-                * paths:
-                */
-               if (linked->cached != path->cached) {
-                       if (!linked->cached)
-                               continue;
+               bch2_print_string_as_lines(KERN_ERR, buf.buf);
+               printbuf_exit(&buf);
+               BUG();
+       }
+
+       ret = abort_lock(g, abort);
+out:
+       if (ret)
+               while (g->nr)
+                       lock_graph_up(g);
+       return ret;
+}
 
-                       reason = 4;
-                       goto deadlock;
+static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
+                             struct printbuf *cycle)
+{
+       struct btree_trans *orig_trans = g->g->trans;
+       struct trans_waiting_for_lock *i;
+
+       for (i = g->g; i < g->g + g->nr; i++)
+               if (i->trans == trans) {
+                       closure_put(&trans->ref);
+                       return break_cycle(g, cycle);
                }
 
-               /*
-                * Interior nodes must be locked before their descendants: if
-                * another path has possible descendants locked of the node
-                * we're about to lock, it must have the ancestors locked too:
-                */
-               if (level > btree_path_highest_level_locked(linked)) {
-                       reason = 5;
-                       goto deadlock;
+       if (g->nr == ARRAY_SIZE(g->g)) {
+               closure_put(&trans->ref);
+
+               if (orig_trans->lock_may_not_fail)
+                       return 0;
+
+               while (g->nr)
+                       lock_graph_up(g);
+
+               if (cycle)
+                       return 0;
+
+               trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
+               return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
+       }
+
+       __lock_graph_down(g, trans);
+       return 0;
+}
+
+static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
+{
+       return t1 + t2 > 1;
+}
+
+int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
+{
+       struct lock_graph g;
+       struct trans_waiting_for_lock *top;
+       struct btree_bkey_cached_common *b;
+       btree_path_idx_t path_idx;
+       int ret = 0;
+
+       g.nr = 0;
+
+       if (trans->lock_must_abort) {
+               if (cycle)
+                       return -1;
+
+               trace_would_deadlock(&g, trans, _RET_IP_);
+               return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
+       }
+
+       lock_graph_down(&g, trans);
+
+       /* trans->paths is rcu protected vs. freeing */
+       rcu_read_lock();
+       if (cycle)
+               cycle->atomic++;
+next:
+       if (!g.nr)
+               goto out;
+
+       top = &g.g[g.nr - 1];
+
+       struct btree_path *paths = rcu_dereference(top->trans->paths);
+       if (!paths)
+               goto up;
+
+       unsigned long *paths_allocated = trans_paths_allocated(paths);
+
+       trans_for_each_path_idx_from(paths_allocated, *trans_paths_nr(paths),
+                                    path_idx, top->path_idx) {
+               struct btree_path *path = paths + path_idx;
+               if (!path->nodes_locked)
+                       continue;
+
+               if (path_idx != top->path_idx) {
+                       top->path_idx           = path_idx;
+                       top->level              = 0;
+                       top->lock_start_time    = 0;
                }
 
-               /* Must lock btree nodes in key order: */
-               if (btree_node_locked(linked, level) &&
-                   bpos_cmp(pos, btree_node_pos(&linked->l[level].b->c)) <= 0) {
-                       reason = 7;
-                       goto deadlock;
+               for (;
+                    top->level < BTREE_MAX_DEPTH;
+                    top->level++, top->lock_start_time = 0) {
+                       int lock_held = btree_node_locked_type(path, top->level);
+
+                       if (lock_held == BTREE_NODE_UNLOCKED)
+                               continue;
+
+                       b = &READ_ONCE(path->l[top->level].b)->c;
+
+                       if (IS_ERR_OR_NULL(b)) {
+                               /*
+                                * If we get here, it means we raced with the
+                                * other thread updating its btree_path
+                                * structures - which means it can't be blocked
+                                * waiting on a lock:
+                                */
+                               if (!lock_graph_remove_non_waiters(&g)) {
+                                       /*
+                                        * If lock_graph_remove_non_waiters()
+                                        * didn't do anything, it must be
+                                        * because we're being called by debugfs
+                                        * checking for lock cycles, which
+                                        * invokes us on btree_transactions that
+                                        * aren't actually waiting on anything.
+                                        * Just bail out:
+                                        */
+                                       lock_graph_pop_all(&g);
+                               }
+
+                               goto next;
+                       }
+
+                       if (list_empty_careful(&b->lock.wait_list))
+                               continue;
+
+                       raw_spin_lock(&b->lock.wait_lock);
+                       list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
+                               BUG_ON(b != trans->locking);
+
+                               if (top->lock_start_time &&
+                                   time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
+                                       continue;
+
+                               top->lock_start_time = trans->locking_wait.start_time;
+
+                               /* Don't check for self deadlock: */
+                               if (trans == top->trans ||
+                                   !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
+                                       continue;
+
+                               closure_get(&trans->ref);
+                               raw_spin_unlock(&b->lock.wait_lock);
+
+                               ret = lock_graph_descend(&g, trans, cycle);
+                               if (ret)
+                                       goto out;
+                               goto next;
+
+                       }
+                       raw_spin_unlock(&b->lock.wait_lock);
                }
        }
+up:
+       if (g.nr > 1 && cycle)
+               print_chain(cycle, &g);
+       lock_graph_up(&g);
+       goto next;
+out:
+       if (cycle)
+               --cycle->atomic;
+       rcu_read_unlock();
+       return ret;
+}
+
+int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
+{
+       struct btree_trans *trans = p;
 
-       return btree_node_lock_type(trans, path, b, pos, level,
-                                   type, should_sleep_fn, p);
-deadlock:
-       trace_and_count(trans->c, trans_restart_would_deadlock, trans, ip, reason, linked, path, &pos);
-       return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
+       return bch2_check_for_deadlock(trans, NULL);
+}
+
+int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
+                                struct btree_bkey_cached_common *b,
+                                bool lock_may_not_fail)
+{
+       int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
+       int ret;
+
+       /*
+        * Must drop our read locks before calling six_lock_write() -
+        * six_unlock() won't do wakeups until the reader count
+        * goes to 0, and it's safe because we have the node intent
+        * locked:
+        */
+       six_lock_readers_add(&b->lock, -readers);
+       ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
+                                      lock_may_not_fail, _RET_IP_);
+       six_lock_readers_add(&b->lock, readers);
+
+       if (ret)
+               mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_INTENT_LOCKED);
+
+       return ret;
+}
+
+void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
+                                      struct btree_path *path,
+                                      struct btree_bkey_cached_common *b)
+{
+       struct btree_path *linked;
+       unsigned i, iter;
+       int ret;
+
+       /*
+        * XXX BIG FAT NOTICE
+        *
+        * Drop all read locks before taking a write lock:
+        *
+        * This is a hack, because bch2_btree_node_lock_write_nofail() is a
+        * hack - but by dropping read locks first, this should never fail, and
+        * we only use this in code paths where whatever read locks we've
+        * already taken are no longer needed:
+        */
+
+       trans_for_each_path(trans, linked, iter) {
+               if (!linked->nodes_locked)
+                       continue;
+
+               for (i = 0; i < BTREE_MAX_DEPTH; i++)
+                       if (btree_node_read_locked(linked, i)) {
+                               btree_node_unlock(trans, linked, i);
+                               btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
+                       }
+       }
+
+       ret = __btree_node_lock_write(trans, path, b, true);
+       BUG_ON(ret);
 }
 
 /* relock */
 
 static inline bool btree_path_get_locks(struct btree_trans *trans,
                                        struct btree_path *path,
-                                       bool upgrade)
+                                       bool upgrade,
+                                       struct get_locks_fail *f)
 {
        unsigned l = path->level;
        int fail_idx = -1;
@@ -173,8 +481,14 @@ static inline bool btree_path_get_locks(struct btree_trans *trans,
 
                if (!(upgrade
                      ? bch2_btree_node_upgrade(trans, path, l)
-                     : bch2_btree_node_relock(trans, path, l)))
-                       fail_idx = l;
+                     : bch2_btree_node_relock(trans, path, l))) {
+                       fail_idx        = l;
+
+                       if (f) {
+                               f->l    = l;
+                               f->b    = path->l[l].b;
+                       }
+               }
 
                l++;
        } while (l < path->locks_want);
@@ -205,7 +519,8 @@ static inline bool btree_path_get_locks(struct btree_trans *trans,
 }
 
 bool __bch2_btree_node_relock(struct btree_trans *trans,
-                             struct btree_path *path, unsigned level)
+                             struct btree_path *path, unsigned level,
+                             bool trace)
 {
        struct btree *b = btree_path_node(path, level);
        int want = __btree_lock_want(path, level);
@@ -220,7 +535,8 @@ bool __bch2_btree_node_relock(struct btree_trans *trans,
                return true;
        }
 fail:
-       trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
+       if (trace && !trans->notrace_relock_fail)
+               trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
        return false;
 }
 
@@ -230,6 +546,7 @@ bool bch2_btree_node_upgrade(struct btree_trans *trans,
                             struct btree_path *path, unsigned level)
 {
        struct btree *b = path->l[level].b;
+       struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
 
        if (!is_btree_node(path, level))
                return false;
@@ -253,11 +570,24 @@ bool bch2_btree_node_upgrade(struct btree_trans *trans,
        if (race_fault())
                return false;
 
-       if (btree_node_locked(path, level)
-           ? six_lock_tryupgrade(&b->c.lock)
-           : six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
-               goto success;
+       if (btree_node_locked(path, level)) {
+               bool ret;
+
+               six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
+               ret = six_lock_tryupgrade(&b->c.lock);
+               six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
 
+               if (ret)
+                       goto success;
+       } else {
+               if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
+                       goto success;
+       }
+
+       /*
+        * Do we already have an intent lock via another path? If so, just bump
+        * lock count:
+        */
        if (btree_node_lock_seq_matches(path, b, level) &&
            btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
                btree_node_unlock(trans, path, level);
@@ -267,7 +597,7 @@ bool bch2_btree_node_upgrade(struct btree_trans *trans,
        trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
        return false;
 success:
-       mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
+       mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
        return true;
 }
 
@@ -299,34 +629,40 @@ __flatten
 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
                        struct btree_path *path, unsigned long trace_ip)
 {
-       return btree_path_get_locks(trans, path, false);
+       struct get_locks_fail f;
+
+       return btree_path_get_locks(trans, path, false, &f);
 }
 
-__flatten
-bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
+int __bch2_btree_path_relock(struct btree_trans *trans,
                        struct btree_path *path, unsigned long trace_ip)
 {
-       return btree_path_get_locks(trans, path, true);
+       if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
+               trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
+               return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
+       }
+
+       return 0;
 }
 
 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
                               struct btree_path *path,
-                              unsigned new_locks_want)
+                              unsigned new_locks_want,
+                              struct get_locks_fail *f)
 {
        EBUG_ON(path->locks_want >= new_locks_want);
 
        path->locks_want = new_locks_want;
 
-       return btree_path_get_locks(trans, path, true);
+       return btree_path_get_locks(trans, path, true, f);
 }
 
 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
                               struct btree_path *path,
-                              unsigned new_locks_want)
+                              unsigned new_locks_want,
+                              struct get_locks_fail *f)
 {
-       struct btree_path *linked;
-
-       if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
+       if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f))
                return true;
 
        /*
@@ -348,15 +684,19 @@ bool __bch2_btree_path_upgrade(struct btree_trans *trans,
         * before interior nodes - now that's handled by
         * bch2_btree_path_traverse_all().
         */
-       if (!path->cached && !trans->in_traverse_all)
-               trans_for_each_path(trans, linked)
+       if (!path->cached && !trans->in_traverse_all) {
+               struct btree_path *linked;
+               unsigned i;
+
+               trans_for_each_path(trans, linked, i)
                        if (linked != path &&
                            linked->cached == path->cached &&
                            linked->btree_id == path->btree_id &&
                            linked->locks_want < new_locks_want) {
                                linked->locks_want = new_locks_want;
-                               btree_path_get_locks(trans, linked, true);
+                               btree_path_get_locks(trans, linked, true, NULL);
                        }
+       }
 
        return false;
 }
@@ -365,7 +705,10 @@ void __bch2_btree_path_downgrade(struct btree_trans *trans,
                                 struct btree_path *path,
                                 unsigned new_locks_want)
 {
-       unsigned l;
+       unsigned l, old_locks_want = path->locks_want;
+
+       if (trans->restarted)
+               return;
 
        EBUG_ON(path->locks_want < new_locks_want);
 
@@ -378,13 +721,15 @@ void __bch2_btree_path_downgrade(struct btree_trans *trans,
                } else {
                        if (btree_node_intent_locked(path, l)) {
                                six_lock_downgrade(&path->l[l].b->c.lock);
-                               mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
+                               mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
                        }
                        break;
                }
        }
 
        bch2_btree_path_verify_locks(path);
+
+       trace_path_downgrade(trans, _RET_IP_, path, old_locks_want);
 }
 
 /* Btree transaction locking: */
@@ -392,19 +737,24 @@ void __bch2_btree_path_downgrade(struct btree_trans *trans,
 void bch2_trans_downgrade(struct btree_trans *trans)
 {
        struct btree_path *path;
+       unsigned i;
+
+       if (trans->restarted)
+               return;
 
-       trans_for_each_path(trans, path)
+       trans_for_each_path(trans, path, i)
                bch2_btree_path_downgrade(trans, path);
 }
 
 int bch2_trans_relock(struct btree_trans *trans)
 {
        struct btree_path *path;
+       unsigned i;
 
        if (unlikely(trans->restarted))
-               return - ((int) trans->restarted);
+               return -((int) trans->restarted);
 
-       trans_for_each_path(trans, path)
+       trans_for_each_path(trans, path, i)
                if (path->should_be_locked &&
                    !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
                        trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
@@ -413,19 +763,65 @@ int bch2_trans_relock(struct btree_trans *trans)
        return 0;
 }
 
+int bch2_trans_relock_notrace(struct btree_trans *trans)
+{
+       struct btree_path *path;
+       unsigned i;
+
+       if (unlikely(trans->restarted))
+               return -((int) trans->restarted);
+
+       trans_for_each_path(trans, path, i)
+               if (path->should_be_locked &&
+                   !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
+                       return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
+               }
+       return 0;
+}
+
+void bch2_trans_unlock_noassert(struct btree_trans *trans)
+{
+       struct btree_path *path;
+       unsigned i;
+
+       trans_for_each_path(trans, path, i)
+               __bch2_btree_path_unlock(trans, path);
+}
+
 void bch2_trans_unlock(struct btree_trans *trans)
 {
        struct btree_path *path;
+       unsigned i;
 
-       trans_for_each_path(trans, path)
+       trans_for_each_path(trans, path, i)
                __bch2_btree_path_unlock(trans, path);
+}
 
-       /*
-        * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
-        * btree nodes, it implements its own walking:
-        */
-       BUG_ON(!trans->is_initial_gc &&
-              lock_class_is_held(&bch2_btree_node_lock_key));
+void bch2_trans_unlock_long(struct btree_trans *trans)
+{
+       bch2_trans_unlock(trans);
+       bch2_trans_srcu_unlock(trans);
+}
+
+bool bch2_trans_locked(struct btree_trans *trans)
+{
+       struct btree_path *path;
+       unsigned i;
+
+       trans_for_each_path(trans, path, i)
+               if (path->nodes_locked)
+                       return true;
+       return false;
+}
+
+int __bch2_trans_mutex_lock(struct btree_trans *trans,
+                           struct mutex *lock)
+{
+       int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
+
+       if (ret)
+               mutex_unlock(lock);
+       return ret;
 }
 
 /* Debug */
@@ -458,8 +854,9 @@ void bch2_btree_path_verify_locks(struct btree_path *path)
 void bch2_trans_verify_locks(struct btree_trans *trans)
 {
        struct btree_path *path;
+       unsigned i;
 
-       trans_for_each_path(trans, path)
+       trans_for_each_path(trans, path, i)
                bch2_btree_path_verify_locks(path);
 }