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