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