]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_locking.c
Update bcachefs sources to 8c94740b1bf8 bcachefs: Add missing vaidation for jset_entr...
[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                                         struct get_locks_fail *f)
435 {
436         unsigned l = path->level;
437         int fail_idx = -1;
438
439         do {
440                 if (!btree_path_node(path, l))
441                         break;
442
443                 if (!(upgrade
444                       ? bch2_btree_node_upgrade(trans, path, l)
445                       : bch2_btree_node_relock(trans, path, l))) {
446                         fail_idx        = l;
447
448                         if (f) {
449                                 f->l    = l;
450                                 f->b    = path->l[l].b;
451                         }
452                 }
453
454                 l++;
455         } while (l < path->locks_want);
456
457         /*
458          * When we fail to get a lock, we have to ensure that any child nodes
459          * can't be relocked so bch2_btree_path_traverse has to walk back up to
460          * the node that we failed to relock:
461          */
462         if (fail_idx >= 0) {
463                 __bch2_btree_path_unlock(trans, path);
464                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
465
466                 do {
467                         path->l[fail_idx].b = upgrade
468                                 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
469                                 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
470                         --fail_idx;
471                 } while (fail_idx >= 0);
472         }
473
474         if (path->uptodate == BTREE_ITER_NEED_RELOCK)
475                 path->uptodate = BTREE_ITER_UPTODATE;
476
477         bch2_trans_verify_locks(trans);
478
479         return path->uptodate < BTREE_ITER_NEED_RELOCK;
480 }
481
482 bool __bch2_btree_node_relock(struct btree_trans *trans,
483                               struct btree_path *path, unsigned level,
484                               bool trace)
485 {
486         struct btree *b = btree_path_node(path, level);
487         int want = __btree_lock_want(path, level);
488
489         if (race_fault())
490                 goto fail;
491
492         if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
493             (btree_node_lock_seq_matches(path, b, level) &&
494              btree_node_lock_increment(trans, &b->c, level, want))) {
495                 mark_btree_node_locked(trans, path, level, want);
496                 return true;
497         }
498 fail:
499         if (trace && !trans->notrace_relock_fail)
500                 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
501         return false;
502 }
503
504 /* upgrade */
505
506 bool bch2_btree_node_upgrade(struct btree_trans *trans,
507                              struct btree_path *path, unsigned level)
508 {
509         struct btree *b = path->l[level].b;
510         struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
511
512         if (!is_btree_node(path, level))
513                 return false;
514
515         switch (btree_lock_want(path, level)) {
516         case BTREE_NODE_UNLOCKED:
517                 BUG_ON(btree_node_locked(path, level));
518                 return true;
519         case BTREE_NODE_READ_LOCKED:
520                 BUG_ON(btree_node_intent_locked(path, level));
521                 return bch2_btree_node_relock(trans, path, level);
522         case BTREE_NODE_INTENT_LOCKED:
523                 break;
524         case BTREE_NODE_WRITE_LOCKED:
525                 BUG();
526         }
527
528         if (btree_node_intent_locked(path, level))
529                 return true;
530
531         if (race_fault())
532                 return false;
533
534         if (btree_node_locked(path, level)) {
535                 bool ret;
536
537                 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
538                 ret = six_lock_tryupgrade(&b->c.lock);
539                 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
540
541                 if (ret)
542                         goto success;
543         } else {
544                 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
545                         goto success;
546         }
547
548         /*
549          * Do we already have an intent lock via another path? If so, just bump
550          * lock count:
551          */
552         if (btree_node_lock_seq_matches(path, b, level) &&
553             btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
554                 btree_node_unlock(trans, path, level);
555                 goto success;
556         }
557
558         trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
559         return false;
560 success:
561         mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
562         return true;
563 }
564
565 /* Btree path locking: */
566
567 /*
568  * Only for btree_cache.c - only relocks intent locks
569  */
570 int bch2_btree_path_relock_intent(struct btree_trans *trans,
571                                   struct btree_path *path)
572 {
573         unsigned l;
574
575         for (l = path->level;
576              l < path->locks_want && btree_path_node(path, l);
577              l++) {
578                 if (!bch2_btree_node_relock(trans, path, l)) {
579                         __bch2_btree_path_unlock(trans, path);
580                         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
581                         trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
582                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
583                 }
584         }
585
586         return 0;
587 }
588
589 __flatten
590 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
591                         struct btree_path *path, unsigned long trace_ip)
592 {
593         struct get_locks_fail f;
594
595         return btree_path_get_locks(trans, path, false, &f);
596 }
597
598 int __bch2_btree_path_relock(struct btree_trans *trans,
599                         struct btree_path *path, unsigned long trace_ip)
600 {
601         if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
602                 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
603                 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
604         }
605
606         return 0;
607 }
608
609 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
610                                struct btree_path *path,
611                                unsigned new_locks_want,
612                                struct get_locks_fail *f)
613 {
614         EBUG_ON(path->locks_want >= new_locks_want);
615
616         path->locks_want = new_locks_want;
617
618         return btree_path_get_locks(trans, path, true, f);
619 }
620
621 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
622                                struct btree_path *path,
623                                unsigned new_locks_want,
624                                struct get_locks_fail *f)
625 {
626         struct btree_path *linked;
627
628         if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f))
629                 return true;
630
631         /*
632          * XXX: this is ugly - we'd prefer to not be mucking with other
633          * iterators in the btree_trans here.
634          *
635          * On failure to upgrade the iterator, setting iter->locks_want and
636          * calling get_locks() is sufficient to make bch2_btree_path_traverse()
637          * get the locks we want on transaction restart.
638          *
639          * But if this iterator was a clone, on transaction restart what we did
640          * to this iterator isn't going to be preserved.
641          *
642          * Possibly we could add an iterator field for the parent iterator when
643          * an iterator is a copy - for now, we'll just upgrade any other
644          * iterators with the same btree id.
645          *
646          * The code below used to be needed to ensure ancestor nodes get locked
647          * before interior nodes - now that's handled by
648          * bch2_btree_path_traverse_all().
649          */
650         if (!path->cached && !trans->in_traverse_all)
651                 trans_for_each_path(trans, linked)
652                         if (linked != path &&
653                             linked->cached == path->cached &&
654                             linked->btree_id == path->btree_id &&
655                             linked->locks_want < new_locks_want) {
656                                 linked->locks_want = new_locks_want;
657                                 btree_path_get_locks(trans, linked, true, NULL);
658                         }
659
660         return false;
661 }
662
663 void __bch2_btree_path_downgrade(struct btree_trans *trans,
664                                  struct btree_path *path,
665                                  unsigned new_locks_want)
666 {
667         unsigned l, old_locks_want = path->locks_want;
668
669         if (trans->restarted)
670                 return;
671
672         EBUG_ON(path->locks_want < new_locks_want);
673
674         path->locks_want = new_locks_want;
675
676         while (path->nodes_locked &&
677                (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
678                 if (l > path->level) {
679                         btree_node_unlock(trans, path, l);
680                 } else {
681                         if (btree_node_intent_locked(path, l)) {
682                                 six_lock_downgrade(&path->l[l].b->c.lock);
683                                 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
684                         }
685                         break;
686                 }
687         }
688
689         bch2_btree_path_verify_locks(path);
690
691         path->downgrade_seq++;
692         trace_path_downgrade(trans, _RET_IP_, path, old_locks_want);
693 }
694
695 /* Btree transaction locking: */
696
697 void bch2_trans_downgrade(struct btree_trans *trans)
698 {
699         struct btree_path *path;
700
701         if (trans->restarted)
702                 return;
703
704         trans_for_each_path(trans, path)
705                 bch2_btree_path_downgrade(trans, path);
706 }
707
708 int bch2_trans_relock(struct btree_trans *trans)
709 {
710         struct btree_path *path;
711
712         if (unlikely(trans->restarted))
713                 return -((int) trans->restarted);
714
715         trans_for_each_path(trans, path)
716                 if (path->should_be_locked &&
717                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
718                         trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
719                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
720                 }
721         return 0;
722 }
723
724 int bch2_trans_relock_notrace(struct btree_trans *trans)
725 {
726         struct btree_path *path;
727
728         if (unlikely(trans->restarted))
729                 return -((int) trans->restarted);
730
731         trans_for_each_path(trans, path)
732                 if (path->should_be_locked &&
733                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
734                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
735                 }
736         return 0;
737 }
738
739 void bch2_trans_unlock_noassert(struct btree_trans *trans)
740 {
741         struct btree_path *path;
742
743         trans_for_each_path(trans, path)
744                 __bch2_btree_path_unlock(trans, path);
745 }
746
747 void bch2_trans_unlock(struct btree_trans *trans)
748 {
749         struct btree_path *path;
750
751         trans_for_each_path(trans, path)
752                 __bch2_btree_path_unlock(trans, path);
753
754         /*
755          * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
756          * btree nodes, it implements its own walking:
757          */
758         if (!trans->is_initial_gc)
759                 bch2_assert_btree_nodes_not_locked();
760 }
761
762 void bch2_trans_unlock_long(struct btree_trans *trans)
763 {
764         bch2_trans_unlock(trans);
765         bch2_trans_srcu_unlock(trans);
766 }
767
768 bool bch2_trans_locked(struct btree_trans *trans)
769 {
770         struct btree_path *path;
771
772         trans_for_each_path(trans, path)
773                 if (path->nodes_locked)
774                         return true;
775         return false;
776 }
777
778 int __bch2_trans_mutex_lock(struct btree_trans *trans,
779                             struct mutex *lock)
780 {
781         int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
782
783         if (ret)
784                 mutex_unlock(lock);
785         return ret;
786 }
787
788 /* Debug */
789
790 #ifdef CONFIG_BCACHEFS_DEBUG
791
792 void bch2_btree_path_verify_locks(struct btree_path *path)
793 {
794         unsigned l;
795
796         if (!path->nodes_locked) {
797                 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
798                        btree_path_node(path, path->level));
799                 return;
800         }
801
802         for (l = 0; l < BTREE_MAX_DEPTH; l++) {
803                 int want = btree_lock_want(path, l);
804                 int have = btree_node_locked_type(path, l);
805
806                 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
807
808                 BUG_ON(is_btree_node(path, l) &&
809                        (want == BTREE_NODE_UNLOCKED ||
810                         have != BTREE_NODE_WRITE_LOCKED) &&
811                        want != have);
812         }
813 }
814
815 void bch2_trans_verify_locks(struct btree_trans *trans)
816 {
817         struct btree_path *path;
818
819         trans_for_each_path(trans, path)
820                 bch2_btree_path_verify_locks(path);
821 }
822
823 #endif