1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BTREE_ITER_H 3 #define _BCACHEFS_BTREE_ITER_H 4 5 #include "bset.h" 6 #include "btree_types.h" 7 #include "trace.h" 8 9 static inline int __bkey_err(const struct bkey *k) 10 { 11 return PTR_ERR_OR_ZERO(k); 12 } 13 14 #define bkey_err(_k) __bkey_err((_k).k) 15 16 static inline void __btree_path_get(struct btree_path *path, bool intent) 17 { 18 path->ref++; 19 path->intent_ref += intent; 20 } 21 22 static inline bool __btree_path_put(struct btree_path *path, bool intent) 23 { 24 EBUG_ON(!path->ref); 25 EBUG_ON(!path->intent_ref && intent); 26 path->intent_ref -= intent; 27 return --path->ref == 0; 28 } 29 30 static inline void btree_path_set_dirty(struct btree_path *path, 31 enum btree_path_uptodate u) 32 { 33 path->uptodate = max_t(unsigned, path->uptodate, u); 34 } 35 36 static inline struct btree *btree_path_node(struct btree_path *path, 37 unsigned level) 38 { 39 return level < BTREE_MAX_DEPTH ? path->l[level].b : NULL; 40 } 41 42 static inline bool btree_node_lock_seq_matches(const struct btree_path *path, 43 const struct btree *b, unsigned level) 44 { 45 return path->l[level].lock_seq == six_lock_seq(&b->c.lock); 46 } 47 48 static inline struct btree *btree_node_parent(struct btree_path *path, 49 struct btree *b) 50 { 51 return btree_path_node(path, b->c.level + 1); 52 } 53 54 /* Iterate over paths within a transaction: */ 55 56 void __bch2_btree_trans_sort_paths(struct btree_trans *); 57 58 static inline void btree_trans_sort_paths(struct btree_trans *trans) 59 { 60 if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && 61 trans->paths_sorted) 62 return; 63 __bch2_btree_trans_sort_paths(trans); 64 } 65 66 static inline struct btree_path * 67 __trans_next_path(struct btree_trans *trans, unsigned idx) 68 { 69 u64 l; 70 71 if (idx == BTREE_ITER_MAX) 72 return NULL; 73 74 l = trans->paths_allocated >> idx; 75 if (!l) 76 return NULL; 77 78 idx += __ffs64(l); 79 EBUG_ON(idx >= BTREE_ITER_MAX); 80 EBUG_ON(trans->paths[idx].idx != idx); 81 return &trans->paths[idx]; 82 } 83 84 #define trans_for_each_path_from(_trans, _path, _start) \ 85 for (_path = __trans_next_path((_trans), _start); \ 86 (_path); \ 87 _path = __trans_next_path((_trans), (_path)->idx + 1)) 88 89 #define trans_for_each_path(_trans, _path) \ 90 trans_for_each_path_from(_trans, _path, 0) 91 92 static inline struct btree_path * 93 __trans_next_path_safe(struct btree_trans *trans, unsigned *idx) 94 { 95 u64 l; 96 97 if (*idx == BTREE_ITER_MAX) 98 return NULL; 99 100 l = trans->paths_allocated >> *idx; 101 if (!l) 102 return NULL; 103 104 *idx += __ffs64(l); 105 EBUG_ON(*idx >= BTREE_ITER_MAX); 106 return &trans->paths[*idx]; 107 } 108 109 /* 110 * This version is intended to be safe for use on a btree_trans that is owned by 111 * another thread, for bch2_btree_trans_to_text(); 112 */ 113 #define trans_for_each_path_safe_from(_trans, _path, _idx, _start) \ 114 for (_idx = _start; \ 115 (_path = __trans_next_path_safe((_trans), &_idx)); \ 116 _idx++) 117 118 #define trans_for_each_path_safe(_trans, _path, _idx) \ 119 trans_for_each_path_safe_from(_trans, _path, _idx, 0) 120 121 static inline struct btree_path *next_btree_path(struct btree_trans *trans, struct btree_path *path) 122 { 123 unsigned idx = path ? path->sorted_idx + 1 : 0; 124 125 EBUG_ON(idx > trans->nr_sorted); 126 127 return idx < trans->nr_sorted 128 ? trans->paths + trans->sorted[idx] 129 : NULL; 130 } 131 132 static inline struct btree_path *prev_btree_path(struct btree_trans *trans, struct btree_path *path) 133 { 134 unsigned idx = path ? path->sorted_idx : trans->nr_sorted; 135 136 return idx 137 ? trans->paths + trans->sorted[idx - 1] 138 : NULL; 139 } 140 141 #define trans_for_each_path_inorder(_trans, _path, _i) \ 142 for (_i = 0; \ 143 ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) < (_trans)->nr_sorted;\ 144 _i++) 145 146 #define trans_for_each_path_inorder_reverse(_trans, _path, _i) \ 147 for (_i = trans->nr_sorted - 1; \ 148 ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) >= 0;\ 149 --_i) 150 151 static inline bool __path_has_node(const struct btree_path *path, 152 const struct btree *b) 153 { 154 return path->l[b->c.level].b == b && 155 btree_node_lock_seq_matches(path, b, b->c.level); 156 } 157 158 static inline struct btree_path * 159 __trans_next_path_with_node(struct btree_trans *trans, struct btree *b, 160 unsigned idx) 161 { 162 struct btree_path *path = __trans_next_path(trans, idx); 163 164 while (path && !__path_has_node(path, b)) 165 path = __trans_next_path(trans, path->idx + 1); 166 167 return path; 168 } 169 170 #define trans_for_each_path_with_node(_trans, _b, _path) \ 171 for (_path = __trans_next_path_with_node((_trans), (_b), 0); \ 172 (_path); \ 173 _path = __trans_next_path_with_node((_trans), (_b), \ 174 (_path)->idx + 1)) 175 176 struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *, struct btree_path *, 177 bool, unsigned long); 178 179 static inline struct btree_path * __must_check 180 bch2_btree_path_make_mut(struct btree_trans *trans, 181 struct btree_path *path, bool intent, 182 unsigned long ip) 183 { 184 if (path->ref > 1 || path->preserve) 185 path = __bch2_btree_path_make_mut(trans, path, intent, ip); 186 path->should_be_locked = false; 187 return path; 188 } 189 190 struct btree_path * __must_check 191 __bch2_btree_path_set_pos(struct btree_trans *, struct btree_path *, 192 struct bpos, bool, unsigned long, int); 193 194 static inline struct btree_path * __must_check 195 bch2_btree_path_set_pos(struct btree_trans *trans, 196 struct btree_path *path, struct bpos new_pos, 197 bool intent, unsigned long ip) 198 { 199 int cmp = bpos_cmp(new_pos, path->pos); 200 201 return cmp 202 ? __bch2_btree_path_set_pos(trans, path, new_pos, intent, ip, cmp) 203 : path; 204 } 205 206 int __must_check bch2_btree_path_traverse_one(struct btree_trans *, struct btree_path *, 207 unsigned, unsigned long); 208 209 static inline int __must_check bch2_btree_path_traverse(struct btree_trans *trans, 210 struct btree_path *path, unsigned flags) 211 { 212 if (path->uptodate < BTREE_ITER_NEED_RELOCK) 213 return 0; 214 215 return bch2_btree_path_traverse_one(trans, path, flags, _RET_IP_); 216 } 217 218 int __must_check bch2_btree_path_traverse(struct btree_trans *, 219 struct btree_path *, unsigned); 220 struct btree_path *bch2_path_get(struct btree_trans *, enum btree_id, struct bpos, 221 unsigned, unsigned, unsigned, unsigned long); 222 struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *, struct bkey *); 223 224 /* 225 * bch2_btree_path_peek_slot() for a cached iterator might return a key in a 226 * different snapshot: 227 */ 228 static inline struct bkey_s_c bch2_btree_path_peek_slot_exact(struct btree_path *path, struct bkey *u) 229 { 230 struct bkey_s_c k = bch2_btree_path_peek_slot(path, u); 231 232 if (k.k && bpos_eq(path->pos, k.k->p)) 233 return k; 234 235 bkey_init(u); 236 u->p = path->pos; 237 return (struct bkey_s_c) { u, NULL }; 238 } 239 240 struct bkey_i *bch2_btree_journal_peek_slot(struct btree_trans *, 241 struct btree_iter *, struct bpos); 242 243 void bch2_btree_path_level_init(struct btree_trans *, struct btree_path *, struct btree *); 244 245 int __bch2_trans_mutex_lock(struct btree_trans *, struct mutex *); 246 247 static inline int bch2_trans_mutex_lock(struct btree_trans *trans, struct mutex *lock) 248 { 249 return mutex_trylock(lock) 250 ? 0 251 : __bch2_trans_mutex_lock(trans, lock); 252 } 253 254 #ifdef CONFIG_BCACHEFS_DEBUG 255 void bch2_trans_verify_paths(struct btree_trans *); 256 void bch2_assert_pos_locked(struct btree_trans *, enum btree_id, 257 struct bpos, bool); 258 #else 259 static inline void bch2_trans_verify_paths(struct btree_trans *trans) {} 260 static inline void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id, 261 struct bpos pos, bool key_cache) {} 262 #endif 263 264 void bch2_btree_path_fix_key_modified(struct btree_trans *trans, 265 struct btree *, struct bkey_packed *); 266 void bch2_btree_node_iter_fix(struct btree_trans *trans, struct btree_path *, 267 struct btree *, struct btree_node_iter *, 268 struct bkey_packed *, unsigned, unsigned); 269 270 int bch2_btree_path_relock_intent(struct btree_trans *, struct btree_path *); 271 272 void bch2_path_put(struct btree_trans *, struct btree_path *, bool); 273 274 int bch2_trans_relock(struct btree_trans *); 275 int bch2_trans_relock_notrace(struct btree_trans *); 276 void bch2_trans_unlock(struct btree_trans *); 277 void bch2_trans_unlock_long(struct btree_trans *); 278 bool bch2_trans_locked(struct btree_trans *); 279 280 static inline int trans_was_restarted(struct btree_trans *trans, u32 restart_count) 281 { 282 return restart_count != trans->restart_count 283 ? -BCH_ERR_transaction_restart_nested 284 : 0; 285 } 286 287 void __noreturn bch2_trans_restart_error(struct btree_trans *, u32); 288 289 static inline void bch2_trans_verify_not_restarted(struct btree_trans *trans, 290 u32 restart_count) 291 { 292 if (trans_was_restarted(trans, restart_count)) 293 bch2_trans_restart_error(trans, restart_count); 294 } 295 296 void __noreturn bch2_trans_in_restart_error(struct btree_trans *); 297 298 static inline void bch2_trans_verify_not_in_restart(struct btree_trans *trans) 299 { 300 if (trans->restarted) 301 bch2_trans_in_restart_error(trans); 302 } 303 304 __always_inline 305 static int btree_trans_restart_nounlock(struct btree_trans *trans, int err) 306 { 307 BUG_ON(err <= 0); 308 BUG_ON(!bch2_err_matches(-err, BCH_ERR_transaction_restart)); 309 310 trans->restarted = err; 311 trans->last_restarted_ip = _THIS_IP_; 312 return -err; 313 } 314 315 __always_inline 316 static int btree_trans_restart(struct btree_trans *trans, int err) 317 { 318 btree_trans_restart_nounlock(trans, err); 319 return -err; 320 } 321 322 bool bch2_btree_node_upgrade(struct btree_trans *, 323 struct btree_path *, unsigned); 324 325 void __bch2_btree_path_downgrade(struct btree_trans *, struct btree_path *, unsigned); 326 327 static inline void bch2_btree_path_downgrade(struct btree_trans *trans, 328 struct btree_path *path) 329 { 330 unsigned new_locks_want = path->level + !!path->intent_ref; 331 332 if (path->locks_want > new_locks_want) 333 __bch2_btree_path_downgrade(trans, path, new_locks_want); 334 } 335 336 void bch2_trans_downgrade(struct btree_trans *); 337 338 void bch2_trans_node_add(struct btree_trans *trans, struct btree *); 339 void bch2_trans_node_reinit_iter(struct btree_trans *, struct btree *); 340 341 int __must_check __bch2_btree_iter_traverse(struct btree_iter *iter); 342 int __must_check bch2_btree_iter_traverse(struct btree_iter *); 343 344 struct btree *bch2_btree_iter_peek_node(struct btree_iter *); 345 struct btree *bch2_btree_iter_peek_node_and_restart(struct btree_iter *); 346 struct btree *bch2_btree_iter_next_node(struct btree_iter *); 347 348 struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *, struct bpos); 349 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *); 350 351 struct bkey_s_c bch2_btree_iter_peek_all_levels(struct btree_iter *); 352 353 static inline struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter) 354 { 355 return bch2_btree_iter_peek_upto(iter, SPOS_MAX); 356 } 357 358 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *); 359 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *); 360 361 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *); 362 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *); 363 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *); 364 365 bool bch2_btree_iter_advance(struct btree_iter *); 366 bool bch2_btree_iter_rewind(struct btree_iter *); 367 368 static inline void __bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos) 369 { 370 iter->k.type = KEY_TYPE_deleted; 371 iter->k.p.inode = iter->pos.inode = new_pos.inode; 372 iter->k.p.offset = iter->pos.offset = new_pos.offset; 373 iter->k.p.snapshot = iter->pos.snapshot = new_pos.snapshot; 374 iter->k.size = 0; 375 } 376 377 static inline void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos) 378 { 379 if (unlikely(iter->update_path)) 380 bch2_path_put(iter->trans, iter->update_path, 381 iter->flags & BTREE_ITER_INTENT); 382 iter->update_path = NULL; 383 384 if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) 385 new_pos.snapshot = iter->snapshot; 386 387 __bch2_btree_iter_set_pos(iter, new_pos); 388 } 389 390 static inline void bch2_btree_iter_set_pos_to_extent_start(struct btree_iter *iter) 391 { 392 BUG_ON(!(iter->flags & BTREE_ITER_IS_EXTENTS)); 393 iter->pos = bkey_start_pos(&iter->k); 394 } 395 396 static inline void bch2_btree_iter_set_snapshot(struct btree_iter *iter, u32 snapshot) 397 { 398 struct bpos pos = iter->pos; 399 400 iter->snapshot = snapshot; 401 pos.snapshot = snapshot; 402 bch2_btree_iter_set_pos(iter, pos); 403 } 404 405 void bch2_trans_iter_exit(struct btree_trans *, struct btree_iter *); 406 407 static inline unsigned __bch2_btree_iter_flags(struct btree_trans *trans, 408 unsigned btree_id, 409 unsigned flags) 410 { 411 if (flags & BTREE_ITER_ALL_LEVELS) 412 flags |= BTREE_ITER_ALL_SNAPSHOTS|__BTREE_ITER_ALL_SNAPSHOTS; 413 414 if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) && 415 btree_id_is_extents(btree_id)) 416 flags |= BTREE_ITER_IS_EXTENTS; 417 418 if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) && 419 !btree_type_has_snapshot_field(btree_id)) 420 flags &= ~BTREE_ITER_ALL_SNAPSHOTS; 421 422 if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) && 423 btree_type_has_snapshots(btree_id)) 424 flags |= BTREE_ITER_FILTER_SNAPSHOTS; 425 426 if (trans->journal_replay_not_finished) 427 flags |= BTREE_ITER_WITH_JOURNAL; 428 429 return flags; 430 } 431 432 static inline unsigned bch2_btree_iter_flags(struct btree_trans *trans, 433 unsigned btree_id, 434 unsigned flags) 435 { 436 if (!btree_id_cached(trans->c, btree_id)) { 437 flags &= ~BTREE_ITER_CACHED; 438 flags &= ~BTREE_ITER_WITH_KEY_CACHE; 439 } else if (!(flags & BTREE_ITER_CACHED)) 440 flags |= BTREE_ITER_WITH_KEY_CACHE; 441 442 return __bch2_btree_iter_flags(trans, btree_id, flags); 443 } 444 445 static inline void bch2_trans_iter_init_common(struct btree_trans *trans, 446 struct btree_iter *iter, 447 unsigned btree_id, struct bpos pos, 448 unsigned locks_want, 449 unsigned depth, 450 unsigned flags, 451 unsigned long ip) 452 { 453 memset(iter, 0, sizeof(*iter)); 454 iter->trans = trans; 455 iter->btree_id = btree_id; 456 iter->flags = flags; 457 iter->snapshot = pos.snapshot; 458 iter->pos = pos; 459 iter->k.p = pos; 460 461 #ifdef CONFIG_BCACHEFS_DEBUG 462 iter->ip_allocated = ip; 463 #endif 464 iter->path = bch2_path_get(trans, btree_id, iter->pos, 465 locks_want, depth, flags, ip); 466 } 467 468 void bch2_trans_iter_init_outlined(struct btree_trans *, struct btree_iter *, 469 enum btree_id, struct bpos, unsigned); 470 471 static inline void bch2_trans_iter_init(struct btree_trans *trans, 472 struct btree_iter *iter, 473 unsigned btree_id, struct bpos pos, 474 unsigned flags) 475 { 476 if (__builtin_constant_p(btree_id) && 477 __builtin_constant_p(flags)) 478 bch2_trans_iter_init_common(trans, iter, btree_id, pos, 0, 0, 479 bch2_btree_iter_flags(trans, btree_id, flags), 480 _THIS_IP_); 481 else 482 bch2_trans_iter_init_outlined(trans, iter, btree_id, pos, flags); 483 } 484 485 void bch2_trans_node_iter_init(struct btree_trans *, struct btree_iter *, 486 enum btree_id, struct bpos, 487 unsigned, unsigned, unsigned); 488 void bch2_trans_copy_iter(struct btree_iter *, struct btree_iter *); 489 490 static inline void set_btree_iter_dontneed(struct btree_iter *iter) 491 { 492 if (!iter->trans->restarted) 493 iter->path->preserve = false; 494 } 495 496 void *__bch2_trans_kmalloc(struct btree_trans *, size_t); 497 498 static inline void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size) 499 { 500 size = roundup(size, 8); 501 502 if (likely(trans->mem_top + size <= trans->mem_bytes)) { 503 void *p = trans->mem + trans->mem_top; 504 505 trans->mem_top += size; 506 memset(p, 0, size); 507 return p; 508 } else { 509 return __bch2_trans_kmalloc(trans, size); 510 } 511 } 512 513 static inline void *bch2_trans_kmalloc_nomemzero(struct btree_trans *trans, size_t size) 514 { 515 size = roundup(size, 8); 516 517 if (likely(trans->mem_top + size <= trans->mem_bytes)) { 518 void *p = trans->mem + trans->mem_top; 519 520 trans->mem_top += size; 521 return p; 522 } else { 523 return __bch2_trans_kmalloc(trans, size); 524 } 525 } 526 527 static inline struct bkey_s_c __bch2_bkey_get_iter(struct btree_trans *trans, 528 struct btree_iter *iter, 529 unsigned btree_id, struct bpos pos, 530 unsigned flags, unsigned type) 531 { 532 struct bkey_s_c k; 533 534 bch2_trans_iter_init(trans, iter, btree_id, pos, flags); 535 k = bch2_btree_iter_peek_slot(iter); 536 537 if (!bkey_err(k) && type && k.k->type != type) 538 k = bkey_s_c_err(-BCH_ERR_ENOENT_bkey_type_mismatch); 539 if (unlikely(bkey_err(k))) 540 bch2_trans_iter_exit(trans, iter); 541 return k; 542 } 543 544 static inline struct bkey_s_c bch2_bkey_get_iter(struct btree_trans *trans, 545 struct btree_iter *iter, 546 unsigned btree_id, struct bpos pos, 547 unsigned flags) 548 { 549 return __bch2_bkey_get_iter(trans, iter, btree_id, pos, flags, 0); 550 } 551 552 #define bch2_bkey_get_iter_typed(_trans, _iter, _btree_id, _pos, _flags, _type)\ 553 bkey_s_c_to_##_type(__bch2_bkey_get_iter(_trans, _iter, \ 554 _btree_id, _pos, _flags, KEY_TYPE_##_type)) 555 556 static inline int __bch2_bkey_get_val_typed(struct btree_trans *trans, 557 unsigned btree_id, struct bpos pos, 558 unsigned flags, unsigned type, 559 unsigned val_size, void *val) 560 { 561 struct btree_iter iter; 562 struct bkey_s_c k; 563 int ret; 564 565 k = __bch2_bkey_get_iter(trans, &iter, btree_id, pos, flags, type); 566 ret = bkey_err(k); 567 if (!ret) { 568 unsigned b = min_t(unsigned, bkey_val_bytes(k.k), val_size); 569 570 memcpy(val, k.v, b); 571 if (unlikely(b < sizeof(*val))) 572 memset((void *) val + b, 0, sizeof(*val) - b); 573 bch2_trans_iter_exit(trans, &iter); 574 } 575 576 return ret; 577 } 578 579 #define bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags, _type, _val)\ 580 __bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags, \ 581 KEY_TYPE_##_type, sizeof(*_val), _val) 582 583 void bch2_trans_srcu_unlock(struct btree_trans *); 584 void bch2_trans_srcu_lock(struct btree_trans *); 585 586 u32 bch2_trans_begin(struct btree_trans *); 587 588 /* 589 * XXX 590 * this does not handle transaction restarts from bch2_btree_iter_next_node() 591 * correctly 592 */ 593 #define __for_each_btree_node(_trans, _iter, _btree_id, _start, \ 594 _locks_want, _depth, _flags, _b, _ret) \ 595 for (bch2_trans_node_iter_init((_trans), &(_iter), (_btree_id), \ 596 _start, _locks_want, _depth, _flags); \ 597 (_b) = bch2_btree_iter_peek_node_and_restart(&(_iter)), \ 598 !((_ret) = PTR_ERR_OR_ZERO(_b)) && (_b); \ 599 (_b) = bch2_btree_iter_next_node(&(_iter))) 600 601 #define for_each_btree_node(_trans, _iter, _btree_id, _start, \ 602 _flags, _b, _ret) \ 603 __for_each_btree_node(_trans, _iter, _btree_id, _start, \ 604 0, 0, _flags, _b, _ret) 605 606 static inline struct bkey_s_c bch2_btree_iter_peek_prev_type(struct btree_iter *iter, 607 unsigned flags) 608 { 609 BUG_ON(flags & BTREE_ITER_ALL_LEVELS); 610 611 return flags & BTREE_ITER_SLOTS ? bch2_btree_iter_peek_slot(iter) : 612 bch2_btree_iter_peek_prev(iter); 613 } 614 615 static inline struct bkey_s_c bch2_btree_iter_peek_type(struct btree_iter *iter, 616 unsigned flags) 617 { 618 return flags & BTREE_ITER_ALL_LEVELS ? bch2_btree_iter_peek_all_levels(iter) : 619 flags & BTREE_ITER_SLOTS ? bch2_btree_iter_peek_slot(iter) : 620 bch2_btree_iter_peek(iter); 621 } 622 623 static inline struct bkey_s_c bch2_btree_iter_peek_upto_type(struct btree_iter *iter, 624 struct bpos end, 625 unsigned flags) 626 { 627 if (!(flags & BTREE_ITER_SLOTS)) 628 return bch2_btree_iter_peek_upto(iter, end); 629 630 if (bkey_gt(iter->pos, end)) 631 return bkey_s_c_null; 632 633 return bch2_btree_iter_peek_slot(iter); 634 } 635 636 static inline int btree_trans_too_many_iters(struct btree_trans *trans) 637 { 638 if (hweight64(trans->paths_allocated) > BTREE_ITER_MAX - 8) { 639 trace_and_count(trans->c, trans_restart_too_many_iters, trans, _THIS_IP_); 640 return btree_trans_restart(trans, BCH_ERR_transaction_restart_too_many_iters); 641 } 642 643 return 0; 644 } 645 646 struct bkey_s_c bch2_btree_iter_peek_and_restart_outlined(struct btree_iter *); 647 648 static inline struct bkey_s_c 649 __bch2_btree_iter_peek_and_restart(struct btree_trans *trans, 650 struct btree_iter *iter, unsigned flags) 651 { 652 struct bkey_s_c k; 653 654 while (btree_trans_too_many_iters(trans) || 655 (k = bch2_btree_iter_peek_type(iter, flags), 656 bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart))) 657 bch2_trans_begin(trans); 658 659 return k; 660 } 661 662 static inline struct bkey_s_c 663 __bch2_btree_iter_peek_upto_and_restart(struct btree_trans *trans, 664 struct btree_iter *iter, 665 struct bpos end, 666 unsigned flags) 667 { 668 struct bkey_s_c k; 669 670 while (btree_trans_too_many_iters(trans) || 671 (k = bch2_btree_iter_peek_upto_type(iter, end, flags), 672 bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart))) 673 bch2_trans_begin(trans); 674 675 return k; 676 } 677 678 #define lockrestart_do(_trans, _do) \ 679 ({ \ 680 u32 _restart_count; \ 681 int _ret2; \ 682 \ 683 do { \ 684 _restart_count = bch2_trans_begin(_trans); \ 685 _ret2 = (_do); \ 686 } while (bch2_err_matches(_ret2, BCH_ERR_transaction_restart)); \ 687 \ 688 if (!_ret2) \ 689 bch2_trans_verify_not_restarted(_trans, _restart_count);\ 690 \ 691 _ret2; \ 692 }) 693 694 /* 695 * nested_lockrestart_do(), nested_commit_do(): 696 * 697 * These are like lockrestart_do() and commit_do(), with two differences: 698 * 699 * - We don't call bch2_trans_begin() unless we had a transaction restart 700 * - We return -BCH_ERR_transaction_restart_nested if we succeeded after a 701 * transaction restart 702 */ 703 #define nested_lockrestart_do(_trans, _do) \ 704 ({ \ 705 u32 _restart_count, _orig_restart_count; \ 706 int _ret2; \ 707 \ 708 _restart_count = _orig_restart_count = (_trans)->restart_count; \ 709 \ 710 while (bch2_err_matches(_ret2 = (_do), BCH_ERR_transaction_restart))\ 711 _restart_count = bch2_trans_begin(_trans); \ 712 \ 713 if (!_ret2) \ 714 bch2_trans_verify_not_restarted(_trans, _restart_count);\ 715 \ 716 _ret2 ?: trans_was_restarted(_trans, _restart_count); \ 717 }) 718 719 #define for_each_btree_key2(_trans, _iter, _btree_id, \ 720 _start, _flags, _k, _do) \ 721 ({ \ 722 int _ret3 = 0; \ 723 \ 724 bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 725 (_start), (_flags)); \ 726 \ 727 while (1) { \ 728 u32 _restart_count = bch2_trans_begin(_trans); \ 729 \ 730 _ret3 = 0; \ 731 (_k) = bch2_btree_iter_peek_type(&(_iter), (_flags)); \ 732 if (!(_k).k) \ 733 break; \ 734 \ 735 _ret3 = bkey_err(_k) ?: (_do); \ 736 if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\ 737 continue; \ 738 if (_ret3) \ 739 break; \ 740 bch2_trans_verify_not_restarted(_trans, _restart_count);\ 741 if (!bch2_btree_iter_advance(&(_iter))) \ 742 break; \ 743 } \ 744 \ 745 bch2_trans_iter_exit((_trans), &(_iter)); \ 746 _ret3; \ 747 }) 748 749 #define for_each_btree_key2_upto(_trans, _iter, _btree_id, \ 750 _start, _end, _flags, _k, _do) \ 751 ({ \ 752 int _ret3 = 0; \ 753 \ 754 bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 755 (_start), (_flags)); \ 756 \ 757 while (1) { \ 758 u32 _restart_count = bch2_trans_begin(_trans); \ 759 \ 760 _ret3 = 0; \ 761 (_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, (_flags));\ 762 if (!(_k).k) \ 763 break; \ 764 \ 765 _ret3 = bkey_err(_k) ?: (_do); \ 766 if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\ 767 continue; \ 768 if (_ret3) \ 769 break; \ 770 bch2_trans_verify_not_restarted(_trans, _restart_count);\ 771 if (!bch2_btree_iter_advance(&(_iter))) \ 772 break; \ 773 } \ 774 \ 775 bch2_trans_iter_exit((_trans), &(_iter)); \ 776 _ret3; \ 777 }) 778 779 #define for_each_btree_key_reverse(_trans, _iter, _btree_id, \ 780 _start, _flags, _k, _do) \ 781 ({ \ 782 int _ret3 = 0; \ 783 \ 784 bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 785 (_start), (_flags)); \ 786 \ 787 while (1) { \ 788 u32 _restart_count = bch2_trans_begin(_trans); \ 789 (_k) = bch2_btree_iter_peek_prev_type(&(_iter), (_flags));\ 790 if (!(_k).k) { \ 791 _ret3 = 0; \ 792 break; \ 793 } \ 794 \ 795 _ret3 = bkey_err(_k) ?: (_do); \ 796 if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\ 797 continue; \ 798 if (_ret3) \ 799 break; \ 800 bch2_trans_verify_not_restarted(_trans, _restart_count);\ 801 if (!bch2_btree_iter_rewind(&(_iter))) \ 802 break; \ 803 } \ 804 \ 805 bch2_trans_iter_exit((_trans), &(_iter)); \ 806 _ret3; \ 807 }) 808 809 #define for_each_btree_key_commit(_trans, _iter, _btree_id, \ 810 _start, _iter_flags, _k, \ 811 _disk_res, _journal_seq, _commit_flags,\ 812 _do) \ 813 for_each_btree_key2(_trans, _iter, _btree_id, _start, _iter_flags, _k,\ 814 (_do) ?: bch2_trans_commit(_trans, (_disk_res),\ 815 (_journal_seq), (_commit_flags))) 816 817 #define for_each_btree_key_reverse_commit(_trans, _iter, _btree_id, \ 818 _start, _iter_flags, _k, \ 819 _disk_res, _journal_seq, _commit_flags,\ 820 _do) \ 821 for_each_btree_key_reverse(_trans, _iter, _btree_id, _start, _iter_flags, _k,\ 822 (_do) ?: bch2_trans_commit(_trans, (_disk_res),\ 823 (_journal_seq), (_commit_flags))) 824 825 #define for_each_btree_key_upto_commit(_trans, _iter, _btree_id, \ 826 _start, _end, _iter_flags, _k, \ 827 _disk_res, _journal_seq, _commit_flags,\ 828 _do) \ 829 for_each_btree_key2_upto(_trans, _iter, _btree_id, _start, _end, _iter_flags, _k,\ 830 (_do) ?: bch2_trans_commit(_trans, (_disk_res),\ 831 (_journal_seq), (_commit_flags))) 832 833 #define for_each_btree_key(_trans, _iter, _btree_id, \ 834 _start, _flags, _k, _ret) \ 835 for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 836 (_start), (_flags)); \ 837 (_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\ 838 !((_ret) = bkey_err(_k)) && (_k).k; \ 839 bch2_btree_iter_advance(&(_iter))) 840 841 #define for_each_btree_key_upto(_trans, _iter, _btree_id, \ 842 _start, _end, _flags, _k, _ret) \ 843 for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 844 (_start), (_flags)); \ 845 (_k) = __bch2_btree_iter_peek_upto_and_restart((_trans), \ 846 &(_iter), _end, _flags),\ 847 !((_ret) = bkey_err(_k)) && (_k).k; \ 848 bch2_btree_iter_advance(&(_iter))) 849 850 #define for_each_btree_key_norestart(_trans, _iter, _btree_id, \ 851 _start, _flags, _k, _ret) \ 852 for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 853 (_start), (_flags)); \ 854 (_k) = bch2_btree_iter_peek_type(&(_iter), _flags), \ 855 !((_ret) = bkey_err(_k)) && (_k).k; \ 856 bch2_btree_iter_advance(&(_iter))) 857 858 #define for_each_btree_key_upto_norestart(_trans, _iter, _btree_id, \ 859 _start, _end, _flags, _k, _ret) \ 860 for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \ 861 (_start), (_flags)); \ 862 (_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, _flags),\ 863 !((_ret) = bkey_err(_k)) && (_k).k; \ 864 bch2_btree_iter_advance(&(_iter))) 865 866 #define for_each_btree_key_continue(_trans, _iter, _flags, _k, _ret) \ 867 for (; \ 868 (_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\ 869 !((_ret) = bkey_err(_k)) && (_k).k; \ 870 bch2_btree_iter_advance(&(_iter))) 871 872 #define for_each_btree_key_continue_norestart(_iter, _flags, _k, _ret) \ 873 for (; \ 874 (_k) = bch2_btree_iter_peek_type(&(_iter), _flags), \ 875 !((_ret) = bkey_err(_k)) && (_k).k; \ 876 bch2_btree_iter_advance(&(_iter))) 877 878 #define for_each_btree_key_upto_continue_norestart(_iter, _end, _flags, _k, _ret)\ 879 for (; \ 880 (_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, _flags), \ 881 !((_ret) = bkey_err(_k)) && (_k).k; \ 882 bch2_btree_iter_advance(&(_iter))) 883 884 #define drop_locks_do(_trans, _do) \ 885 ({ \ 886 bch2_trans_unlock(_trans); \ 887 _do ?: bch2_trans_relock(_trans); \ 888 }) 889 890 #define allocate_dropping_locks_errcode(_trans, _do) \ 891 ({ \ 892 gfp_t _gfp = GFP_NOWAIT|__GFP_NOWARN; \ 893 int _ret = _do; \ 894 \ 895 if (bch2_err_matches(_ret, ENOMEM)) { \ 896 _gfp = GFP_KERNEL; \ 897 _ret = drop_locks_do(trans, _do); \ 898 } \ 899 _ret; \ 900 }) 901 902 #define allocate_dropping_locks(_trans, _ret, _do) \ 903 ({ \ 904 gfp_t _gfp = GFP_NOWAIT|__GFP_NOWARN; \ 905 typeof(_do) _p = _do; \ 906 \ 907 _ret = 0; \ 908 if (unlikely(!_p)) { \ 909 _gfp = GFP_KERNEL; \ 910 _ret = drop_locks_do(trans, ((_p = _do), 0)); \ 911 } \ 912 _p; \ 913 }) 914 915 /* new multiple iterator interface: */ 916 917 void bch2_trans_updates_to_text(struct printbuf *, struct btree_trans *); 918 void bch2_btree_path_to_text(struct printbuf *, struct btree_path *); 919 void bch2_trans_paths_to_text(struct printbuf *, struct btree_trans *); 920 void bch2_dump_trans_updates(struct btree_trans *); 921 void bch2_dump_trans_paths_updates(struct btree_trans *); 922 923 struct btree_trans *__bch2_trans_get(struct bch_fs *, unsigned); 924 void bch2_trans_put(struct btree_trans *); 925 926 extern const char *bch2_btree_transaction_fns[BCH_TRANSACTIONS_NR]; 927 unsigned bch2_trans_get_fn_idx(const char *); 928 929 #define bch2_trans_get(_c) \ 930 ({ \ 931 static unsigned trans_fn_idx; \ 932 \ 933 if (unlikely(!trans_fn_idx)) \ 934 trans_fn_idx = bch2_trans_get_fn_idx(__func__); \ 935 __bch2_trans_get(_c, trans_fn_idx); \ 936 }) 937 938 void bch2_btree_trans_to_text(struct printbuf *, struct btree_trans *); 939 940 void bch2_fs_btree_iter_exit(struct bch_fs *); 941 int bch2_fs_btree_iter_init(struct bch_fs *); 942 943 #endif /* _BCACHEFS_BTREE_ITER_H */ 944