]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_locking.c
Update bcachefs sources to 99175e5712 bcachefs: Fix bch2_check_discard_freespace_key()
[bcachefs-tools-debian] / libbcachefs / btree_locking.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_locking.h"
5 #include "btree_types.h"
6
7 static struct lock_class_key bch2_btree_node_lock_key;
8
9 void bch2_btree_lock_init(struct btree_bkey_cached_common *b,
10                           enum six_lock_init_flags flags)
11 {
12         __six_lock_init(&b->lock, "b->c.lock", &bch2_btree_node_lock_key, flags);
13 #ifdef CONFIG_DEBUG_LOCK_ALLOC
14         lockdep_set_no_check_recursion(&b->lock.dep_map);
15 #endif
16 }
17
18 #ifdef CONFIG_LOCKDEP
19 void bch2_assert_btree_nodes_not_locked(void)
20 {
21         BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
22 }
23 #endif
24
25 /* Btree node locking: */
26
27 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
28                                                   struct btree_path *skip,
29                                                   struct btree_bkey_cached_common *b,
30                                                   unsigned level)
31 {
32         struct btree_path *path;
33         struct six_lock_count ret;
34
35         memset(&ret, 0, sizeof(ret));
36
37         if (IS_ERR_OR_NULL(b))
38                 return ret;
39
40         trans_for_each_path(trans, path)
41                 if (path != skip && &path->l[level].b->c == b) {
42                         int t = btree_node_locked_type(path, level);
43
44                         if (t != BTREE_NODE_UNLOCKED)
45                                 ret.n[t]++;
46                 }
47
48         return ret;
49 }
50
51 /* unlock */
52
53 void bch2_btree_node_unlock_write(struct btree_trans *trans,
54                         struct btree_path *path, struct btree *b)
55 {
56         bch2_btree_node_unlock_write_inlined(trans, path, b);
57 }
58
59 /* lock */
60
61 /*
62  * @trans wants to lock @b with type @type
63  */
64 struct trans_waiting_for_lock {
65         struct btree_trans              *trans;
66         struct btree_bkey_cached_common *node_want;
67         enum six_lock_type              lock_want;
68
69         /* for iterating over held locks :*/
70         u8                              path_idx;
71         u8                              level;
72         u64                             lock_start_time;
73 };
74
75 struct lock_graph {
76         struct trans_waiting_for_lock   g[8];
77         unsigned                        nr;
78 };
79
80 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
81 {
82         struct trans_waiting_for_lock *i;
83
84         prt_printf(out, "Found lock cycle (%u entries):", g->nr);
85         prt_newline(out);
86
87         for (i = g->g; i < g->g + g->nr; i++)
88                 bch2_btree_trans_to_text(out, i->trans);
89 }
90
91 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
92 {
93         struct trans_waiting_for_lock *i;
94
95         for (i = g->g; i != g->g + g->nr; i++) {
96                 if (i != g->g)
97                         prt_str(out, "<- ");
98                 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
99         }
100         prt_newline(out);
101 }
102
103 static void lock_graph_up(struct lock_graph *g)
104 {
105         closure_put(&g->g[--g->nr].trans->ref);
106 }
107
108 static noinline void lock_graph_pop_all(struct lock_graph *g)
109 {
110         while (g->nr)
111                 lock_graph_up(g);
112 }
113
114 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
115 {
116         closure_get(&trans->ref);
117
118         g->g[g->nr++] = (struct trans_waiting_for_lock) {
119                 .trans          = trans,
120                 .node_want      = trans->locking,
121                 .lock_want      = trans->locking_wait.lock_want,
122         };
123 }
124
125 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
126 {
127         struct trans_waiting_for_lock *i;
128
129         for (i = g->g + 1; i < g->g + g->nr; i++)
130                 if (i->trans->locking != i->node_want ||
131                     i->trans->locking_wait.start_time != i[-1].lock_start_time) {
132                         while (g->g + g->nr > i)
133                                 lock_graph_up(g);
134                         return true;
135                 }
136
137         return false;
138 }
139
140 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
141 {
142         if (i == g->g) {
143                 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
144                 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
145         } else {
146                 i->trans->lock_must_abort = true;
147                 wake_up_process(i->trans->locking_wait.task);
148                 return 0;
149         }
150 }
151
152 static int btree_trans_abort_preference(struct btree_trans *trans)
153 {
154         if (trans->lock_may_not_fail)
155                 return 0;
156         if (trans->locking_wait.lock_want == SIX_LOCK_write)
157                 return 1;
158         if (!trans->in_traverse_all)
159                 return 2;
160         return 3;
161 }
162
163 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
164 {
165         struct trans_waiting_for_lock *i, *abort = NULL;
166         unsigned best = 0, pref;
167         int ret;
168
169         if (lock_graph_remove_non_waiters(g))
170                 return 0;
171
172         /* Only checking, for debugfs: */
173         if (cycle) {
174                 print_cycle(cycle, g);
175                 ret = -1;
176                 goto out;
177         }
178
179         for (i = g->g; i < g->g + g->nr; i++) {
180                 pref = btree_trans_abort_preference(i->trans);
181                 if (pref > best) {
182                         abort = i;
183                         best = pref;
184                 }
185         }
186
187         if (unlikely(!best)) {
188                 struct printbuf buf = PRINTBUF;
189
190                 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
191
192                 for (i = g->g; i < g->g + g->nr; i++) {
193                         struct btree_trans *trans = i->trans;
194
195                         bch2_btree_trans_to_text(&buf, trans);
196
197                         prt_printf(&buf, "backtrace:");
198                         prt_newline(&buf);
199                         printbuf_indent_add(&buf, 2);
200                         bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
201                         printbuf_indent_sub(&buf, 2);
202                         prt_newline(&buf);
203                 }
204
205                 bch2_print_string_as_lines(KERN_ERR, buf.buf);
206                 printbuf_exit(&buf);
207                 BUG();
208         }
209
210         ret = abort_lock(g, abort);
211 out:
212         if (ret)
213                 while (g->nr)
214                         lock_graph_up(g);
215         return ret;
216 }
217
218 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
219                               struct printbuf *cycle)
220 {
221         struct btree_trans *orig_trans = g->g->trans;
222         struct trans_waiting_for_lock *i;
223
224         for (i = g->g; i < g->g + g->nr; i++)
225                 if (i->trans == trans)
226                         return break_cycle(g, cycle);
227
228         if (g->nr == ARRAY_SIZE(g->g)) {
229                 if (orig_trans->lock_may_not_fail)
230                         return 0;
231
232                 while (g->nr)
233                         lock_graph_up(g);
234
235                 if (cycle)
236                         return 0;
237
238                 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
239                 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
240         }
241
242         lock_graph_down(g, trans);
243         return 0;
244 }
245
246 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
247 {
248         return t1 + t2 > 1;
249 }
250
251 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
252 {
253         struct lock_graph g;
254         struct trans_waiting_for_lock *top;
255         struct btree_bkey_cached_common *b;
256         struct btree_path *path;
257         unsigned path_idx;
258         int ret;
259
260         if (trans->lock_must_abort) {
261                 if (cycle)
262                         return -1;
263
264                 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
265                 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
266         }
267
268         g.nr = 0;
269         lock_graph_down(&g, trans);
270 next:
271         if (!g.nr)
272                 return 0;
273
274         top = &g.g[g.nr - 1];
275
276         trans_for_each_path_safe_from(top->trans, path, path_idx, top->path_idx) {
277                 if (!path->nodes_locked)
278                         continue;
279
280                 if (path_idx != top->path_idx) {
281                         top->path_idx           = path_idx;
282                         top->level              = 0;
283                         top->lock_start_time    = 0;
284                 }
285
286                 for (;
287                      top->level < BTREE_MAX_DEPTH;
288                      top->level++, top->lock_start_time = 0) {
289                         int lock_held = btree_node_locked_type(path, top->level);
290
291                         if (lock_held == BTREE_NODE_UNLOCKED)
292                                 continue;
293
294                         b = &READ_ONCE(path->l[top->level].b)->c;
295
296                         if (IS_ERR_OR_NULL(b)) {
297                                 /*
298                                  * If we get here, it means we raced with the
299                                  * other thread updating its btree_path
300                                  * structures - which means it can't be blocked
301                                  * waiting on a lock:
302                                  */
303                                 if (!lock_graph_remove_non_waiters(&g)) {
304                                         /*
305                                          * If lock_graph_remove_non_waiters()
306                                          * didn't do anything, it must be
307                                          * because we're being called by debugfs
308                                          * checking for lock cycles, which
309                                          * invokes us on btree_transactions that
310                                          * aren't actually waiting on anything.
311                                          * Just bail out:
312                                          */
313                                         lock_graph_pop_all(&g);
314                                 }
315
316                                 goto next;
317                         }
318
319                         if (list_empty_careful(&b->lock.wait_list))
320                                 continue;
321
322                         raw_spin_lock(&b->lock.wait_lock);
323                         list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
324                                 BUG_ON(b != trans->locking);
325
326                                 if (top->lock_start_time &&
327                                     time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
328                                         continue;
329
330                                 top->lock_start_time = trans->locking_wait.start_time;
331
332                                 /* Don't check for self deadlock: */
333                                 if (trans == top->trans ||
334                                     !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
335                                         continue;
336
337                                 ret = lock_graph_descend(&g, trans, cycle);
338                                 raw_spin_unlock(&b->lock.wait_lock);
339
340                                 if (ret)
341                                         return ret;
342                                 goto next;
343
344                         }
345                         raw_spin_unlock(&b->lock.wait_lock);
346                 }
347         }
348
349         if (g.nr > 1 && cycle)
350                 print_chain(cycle, &g);
351         lock_graph_up(&g);
352         goto next;
353 }
354
355 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
356 {
357         struct btree_trans *trans = p;
358
359         return bch2_check_for_deadlock(trans, NULL);
360 }
361
362 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
363                                  struct btree_bkey_cached_common *b,
364                                  bool lock_may_not_fail)
365 {
366         int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
367         int ret;
368
369         /*
370          * Must drop our read locks before calling six_lock_write() -
371          * six_unlock() won't do wakeups until the reader count
372          * goes to 0, and it's safe because we have the node intent
373          * locked:
374          */
375         six_lock_readers_add(&b->lock, -readers);
376         ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
377                                        lock_may_not_fail, _RET_IP_);
378         six_lock_readers_add(&b->lock, readers);
379
380         if (ret)
381                 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
382
383         return ret;
384 }
385
386 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
387                                        struct btree_path *path,
388                                        struct btree_bkey_cached_common *b)
389 {
390         struct btree_path *linked;
391         unsigned i;
392         int ret;
393
394         /*
395          * XXX BIG FAT NOTICE
396          *
397          * Drop all read locks before taking a write lock:
398          *
399          * This is a hack, because bch2_btree_node_lock_write_nofail() is a
400          * hack - but by dropping read locks first, this should never fail, and
401          * we only use this in code paths where whatever read locks we've
402          * already taken are no longer needed:
403          */
404
405         trans_for_each_path(trans, linked) {
406                 if (!linked->nodes_locked)
407                         continue;
408
409                 for (i = 0; i < BTREE_MAX_DEPTH; i++)
410                         if (btree_node_read_locked(linked, i)) {
411                                 btree_node_unlock(trans, linked, i);
412                                 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
413                         }
414         }
415
416         ret = __btree_node_lock_write(trans, path, b, true);
417         BUG_ON(ret);
418 }
419
420 /* relock */
421
422 static inline bool btree_path_get_locks(struct btree_trans *trans,
423                                         struct btree_path *path,
424                                         bool upgrade)
425 {
426         unsigned l = path->level;
427         int fail_idx = -1;
428
429         do {
430                 if (!btree_path_node(path, l))
431                         break;
432
433                 if (!(upgrade
434                       ? bch2_btree_node_upgrade(trans, path, l)
435                       : bch2_btree_node_relock(trans, path, l)))
436                         fail_idx = l;
437
438                 l++;
439         } while (l < path->locks_want);
440
441         /*
442          * When we fail to get a lock, we have to ensure that any child nodes
443          * can't be relocked so bch2_btree_path_traverse has to walk back up to
444          * the node that we failed to relock:
445          */
446         if (fail_idx >= 0) {
447                 __bch2_btree_path_unlock(trans, path);
448                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
449
450                 do {
451                         path->l[fail_idx].b = upgrade
452                                 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
453                                 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
454                         --fail_idx;
455                 } while (fail_idx >= 0);
456         }
457
458         if (path->uptodate == BTREE_ITER_NEED_RELOCK)
459                 path->uptodate = BTREE_ITER_UPTODATE;
460
461         bch2_trans_verify_locks(trans);
462
463         return path->uptodate < BTREE_ITER_NEED_RELOCK;
464 }
465
466 bool __bch2_btree_node_relock(struct btree_trans *trans,
467                               struct btree_path *path, unsigned level,
468                               bool trace)
469 {
470         struct btree *b = btree_path_node(path, level);
471         int want = __btree_lock_want(path, level);
472
473         if (race_fault())
474                 goto fail;
475
476         if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
477             (btree_node_lock_seq_matches(path, b, level) &&
478              btree_node_lock_increment(trans, &b->c, level, want))) {
479                 mark_btree_node_locked(trans, path, level, want);
480                 return true;
481         }
482 fail:
483         if (trace && !trans->notrace_relock_fail)
484                 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
485         return false;
486 }
487
488 /* upgrade */
489
490 bool bch2_btree_node_upgrade(struct btree_trans *trans,
491                              struct btree_path *path, unsigned level)
492 {
493         struct btree *b = path->l[level].b;
494         struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
495
496         if (!is_btree_node(path, level))
497                 return false;
498
499         switch (btree_lock_want(path, level)) {
500         case BTREE_NODE_UNLOCKED:
501                 BUG_ON(btree_node_locked(path, level));
502                 return true;
503         case BTREE_NODE_READ_LOCKED:
504                 BUG_ON(btree_node_intent_locked(path, level));
505                 return bch2_btree_node_relock(trans, path, level);
506         case BTREE_NODE_INTENT_LOCKED:
507                 break;
508         case BTREE_NODE_WRITE_LOCKED:
509                 BUG();
510         }
511
512         if (btree_node_intent_locked(path, level))
513                 return true;
514
515         if (race_fault())
516                 return false;
517
518         if (btree_node_locked(path, level)) {
519                 bool ret;
520
521                 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
522                 ret = six_lock_tryupgrade(&b->c.lock);
523                 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
524
525                 if (ret)
526                         goto success;
527         } else {
528                 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
529                         goto success;
530         }
531
532         /*
533          * Do we already have an intent lock via another path? If so, just bump
534          * lock count:
535          */
536         if (btree_node_lock_seq_matches(path, b, level) &&
537             btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
538                 btree_node_unlock(trans, path, level);
539                 goto success;
540         }
541
542         trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
543         return false;
544 success:
545         mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
546         return true;
547 }
548
549 /* Btree path locking: */
550
551 /*
552  * Only for btree_cache.c - only relocks intent locks
553  */
554 int bch2_btree_path_relock_intent(struct btree_trans *trans,
555                                   struct btree_path *path)
556 {
557         unsigned l;
558
559         for (l = path->level;
560              l < path->locks_want && btree_path_node(path, l);
561              l++) {
562                 if (!bch2_btree_node_relock(trans, path, l)) {
563                         __bch2_btree_path_unlock(trans, path);
564                         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
565                         trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
566                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
567                 }
568         }
569
570         return 0;
571 }
572
573 __flatten
574 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
575                         struct btree_path *path, unsigned long trace_ip)
576 {
577         return btree_path_get_locks(trans, path, false);
578 }
579
580 int __bch2_btree_path_relock(struct btree_trans *trans,
581                         struct btree_path *path, unsigned long trace_ip)
582 {
583         if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
584                 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
585                 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
586         }
587
588         return 0;
589 }
590
591 __flatten
592 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
593                         struct btree_path *path, unsigned long trace_ip)
594 {
595         return btree_path_get_locks(trans, path, true);
596 }
597
598 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
599                                struct btree_path *path,
600                                unsigned new_locks_want)
601 {
602         EBUG_ON(path->locks_want >= new_locks_want);
603
604         path->locks_want = new_locks_want;
605
606         return btree_path_get_locks(trans, path, true);
607 }
608
609 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
610                                struct btree_path *path,
611                                unsigned new_locks_want)
612 {
613         struct btree_path *linked;
614
615         if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
616                 return true;
617
618         /*
619          * XXX: this is ugly - we'd prefer to not be mucking with other
620          * iterators in the btree_trans here.
621          *
622          * On failure to upgrade the iterator, setting iter->locks_want and
623          * calling get_locks() is sufficient to make bch2_btree_path_traverse()
624          * get the locks we want on transaction restart.
625          *
626          * But if this iterator was a clone, on transaction restart what we did
627          * to this iterator isn't going to be preserved.
628          *
629          * Possibly we could add an iterator field for the parent iterator when
630          * an iterator is a copy - for now, we'll just upgrade any other
631          * iterators with the same btree id.
632          *
633          * The code below used to be needed to ensure ancestor nodes get locked
634          * before interior nodes - now that's handled by
635          * bch2_btree_path_traverse_all().
636          */
637         if (!path->cached && !trans->in_traverse_all)
638                 trans_for_each_path(trans, linked)
639                         if (linked != path &&
640                             linked->cached == path->cached &&
641                             linked->btree_id == path->btree_id &&
642                             linked->locks_want < new_locks_want) {
643                                 linked->locks_want = new_locks_want;
644                                 btree_path_get_locks(trans, linked, true);
645                         }
646
647         return false;
648 }
649
650 void __bch2_btree_path_downgrade(struct btree_trans *trans,
651                                  struct btree_path *path,
652                                  unsigned new_locks_want)
653 {
654         unsigned l;
655
656         EBUG_ON(path->locks_want < new_locks_want);
657
658         path->locks_want = new_locks_want;
659
660         while (path->nodes_locked &&
661                (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
662                 if (l > path->level) {
663                         btree_node_unlock(trans, path, l);
664                 } else {
665                         if (btree_node_intent_locked(path, l)) {
666                                 six_lock_downgrade(&path->l[l].b->c.lock);
667                                 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
668                         }
669                         break;
670                 }
671         }
672
673         bch2_btree_path_verify_locks(path);
674 }
675
676 /* Btree transaction locking: */
677
678 void bch2_trans_downgrade(struct btree_trans *trans)
679 {
680         struct btree_path *path;
681
682         trans_for_each_path(trans, path)
683                 bch2_btree_path_downgrade(trans, path);
684 }
685
686 int bch2_trans_relock(struct btree_trans *trans)
687 {
688         struct btree_path *path;
689
690         if (unlikely(trans->restarted))
691                 return -((int) trans->restarted);
692
693         trans_for_each_path(trans, path)
694                 if (path->should_be_locked &&
695                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
696                         trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
697                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
698                 }
699         return 0;
700 }
701
702 int bch2_trans_relock_notrace(struct btree_trans *trans)
703 {
704         struct btree_path *path;
705
706         if (unlikely(trans->restarted))
707                 return -((int) trans->restarted);
708
709         trans_for_each_path(trans, path)
710                 if (path->should_be_locked &&
711                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
712                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
713                 }
714         return 0;
715 }
716
717 void bch2_trans_unlock_noassert(struct btree_trans *trans)
718 {
719         struct btree_path *path;
720
721         trans_for_each_path(trans, path)
722                 __bch2_btree_path_unlock(trans, path);
723 }
724
725 void bch2_trans_unlock(struct btree_trans *trans)
726 {
727         struct btree_path *path;
728
729         trans_for_each_path(trans, path)
730                 __bch2_btree_path_unlock(trans, path);
731
732         /*
733          * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
734          * btree nodes, it implements its own walking:
735          */
736         if (!trans->is_initial_gc)
737                 bch2_assert_btree_nodes_not_locked();
738 }
739
740 bool bch2_trans_locked(struct btree_trans *trans)
741 {
742         struct btree_path *path;
743
744         trans_for_each_path(trans, path)
745                 if (path->nodes_locked)
746                         return true;
747         return false;
748 }
749
750 int __bch2_trans_mutex_lock(struct btree_trans *trans,
751                             struct mutex *lock)
752 {
753         int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
754
755         if (ret)
756                 mutex_unlock(lock);
757         return ret;
758 }
759
760 /* Debug */
761
762 #ifdef CONFIG_BCACHEFS_DEBUG
763
764 void bch2_btree_path_verify_locks(struct btree_path *path)
765 {
766         unsigned l;
767
768         if (!path->nodes_locked) {
769                 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
770                        btree_path_node(path, path->level));
771                 return;
772         }
773
774         for (l = 0; l < BTREE_MAX_DEPTH; l++) {
775                 int want = btree_lock_want(path, l);
776                 int have = btree_node_locked_type(path, l);
777
778                 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
779
780                 BUG_ON(is_btree_node(path, l) &&
781                        (want == BTREE_NODE_UNLOCKED ||
782                         have != BTREE_NODE_WRITE_LOCKED) &&
783                        want != have);
784         }
785 }
786
787 void bch2_trans_verify_locks(struct btree_trans *trans)
788 {
789         struct btree_path *path;
790
791         trans_for_each_path(trans, path)
792                 bch2_btree_path_verify_locks(path);
793 }
794
795 #endif