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