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