1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
6 #include "btree_cache.h"
7 #include "btree_iter.h"
8 #include "btree_key_cache.h"
9 #include "btree_locking.h"
10 #include "btree_update.h"
17 #include "subvolume.h"
19 #include <linux/prefetch.h>
20 #include <trace/events/bcachefs.h>
22 static void btree_trans_verify_sorted(struct btree_trans *);
23 inline void bch2_btree_path_check_sort(struct btree_trans *, struct btree_path *, int);
25 static inline void btree_path_list_remove(struct btree_trans *, struct btree_path *);
26 static inline void btree_path_list_add(struct btree_trans *, struct btree_path *,
29 static inline unsigned long btree_iter_ip_allocated(struct btree_iter *iter)
31 #ifdef CONFIG_BCACHEFS_DEBUG
32 return iter->ip_allocated;
38 static struct btree_path *btree_path_alloc(struct btree_trans *, struct btree_path *);
41 * Unlocks before scheduling
42 * Note: does not revalidate iterator
44 static inline int bch2_trans_cond_resched(struct btree_trans *trans)
46 if (need_resched() || race_fault()) {
47 bch2_trans_unlock(trans);
49 return bch2_trans_relock(trans) ? 0 : -EINTR;
55 static inline int __btree_path_cmp(const struct btree_path *l,
56 enum btree_id r_btree_id,
62 * Must match lock ordering as defined by __bch2_btree_node_lock:
64 return cmp_int(l->btree_id, r_btree_id) ?:
65 cmp_int((int) l->cached, (int) r_cached) ?:
66 bpos_cmp(l->pos, r_pos) ?:
67 -cmp_int(l->level, r_level);
70 static inline int btree_path_cmp(const struct btree_path *l,
71 const struct btree_path *r)
73 return __btree_path_cmp(l, r->btree_id, r->cached, r->pos, r->level);
76 static inline struct bpos bkey_successor(struct btree_iter *iter, struct bpos p)
78 /* Are we iterating over keys in all snapshots? */
79 if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) {
80 p = bpos_successor(p);
82 p = bpos_nosnap_successor(p);
83 p.snapshot = iter->snapshot;
89 static inline struct bpos bkey_predecessor(struct btree_iter *iter, struct bpos p)
91 /* Are we iterating over keys in all snapshots? */
92 if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) {
93 p = bpos_predecessor(p);
95 p = bpos_nosnap_predecessor(p);
96 p.snapshot = iter->snapshot;
102 static inline bool is_btree_node(struct btree_path *path, unsigned l)
104 return l < BTREE_MAX_DEPTH &&
105 (unsigned long) path->l[l].b >= 128;
108 static inline struct bpos btree_iter_search_key(struct btree_iter *iter)
110 struct bpos pos = iter->pos;
112 if ((iter->flags & BTREE_ITER_IS_EXTENTS) &&
113 bkey_cmp(pos, POS_MAX))
114 pos = bkey_successor(iter, pos);
118 static inline bool btree_path_pos_before_node(struct btree_path *path,
121 return bpos_cmp(path->pos, b->data->min_key) < 0;
124 static inline bool btree_path_pos_after_node(struct btree_path *path,
127 return bpos_cmp(b->key.k.p, path->pos) < 0;
130 static inline bool btree_path_pos_in_node(struct btree_path *path,
133 return path->btree_id == b->c.btree_id &&
134 !btree_path_pos_before_node(path, b) &&
135 !btree_path_pos_after_node(path, b);
138 /* Btree node locking: */
140 void bch2_btree_node_unlock_write(struct btree_trans *trans,
141 struct btree_path *path, struct btree *b)
143 bch2_btree_node_unlock_write_inlined(trans, path, b);
146 void __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree *b)
148 struct btree_path *linked;
149 unsigned readers = 0;
151 trans_for_each_path(trans, linked)
152 if (linked->l[b->c.level].b == b &&
153 btree_node_read_locked(linked, b->c.level))
157 * Must drop our read locks before calling six_lock_write() -
158 * six_unlock() won't do wakeups until the reader count
159 * goes to 0, and it's safe because we have the node intent
162 if (!b->c.lock.readers)
163 atomic64_sub(__SIX_VAL(read_lock, readers),
164 &b->c.lock.state.counter);
166 this_cpu_sub(*b->c.lock.readers, readers);
168 six_lock_write(&b->c.lock, NULL, NULL);
170 if (!b->c.lock.readers)
171 atomic64_add(__SIX_VAL(read_lock, readers),
172 &b->c.lock.state.counter);
174 this_cpu_add(*b->c.lock.readers, readers);
177 bool __bch2_btree_node_relock(struct btree_trans *trans,
178 struct btree_path *path, unsigned level)
180 struct btree *b = btree_path_node(path, level);
181 int want = __btree_lock_want(path, level);
183 if (!is_btree_node(path, level))
189 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
190 (btree_node_lock_seq_matches(path, b, level) &&
191 btree_node_lock_increment(trans, b, level, want))) {
192 mark_btree_node_locked(trans, path, level, want);
196 trace_btree_node_relock_fail(trans->fn, _RET_IP_,
200 path->l[level].lock_seq,
201 is_btree_node(path, level) ? b->c.lock.state.seq : 0);
205 bool bch2_btree_node_upgrade(struct btree_trans *trans,
206 struct btree_path *path, unsigned level)
208 struct btree *b = path->l[level].b;
210 if (!is_btree_node(path, level))
213 switch (btree_lock_want(path, level)) {
214 case BTREE_NODE_UNLOCKED:
215 BUG_ON(btree_node_locked(path, level));
217 case BTREE_NODE_READ_LOCKED:
218 BUG_ON(btree_node_intent_locked(path, level));
219 return bch2_btree_node_relock(trans, path, level);
220 case BTREE_NODE_INTENT_LOCKED:
224 if (btree_node_intent_locked(path, level))
230 if (btree_node_locked(path, level)
231 ? six_lock_tryupgrade(&b->c.lock)
232 : six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
235 if (btree_node_lock_seq_matches(path, b, level) &&
236 btree_node_lock_increment(trans, b, level, BTREE_NODE_INTENT_LOCKED)) {
237 btree_node_unlock(path, level);
243 mark_btree_node_intent_locked(trans, path, level);
247 static inline bool btree_path_get_locks(struct btree_trans *trans,
248 struct btree_path *path,
251 unsigned l = path->level;
255 if (!btree_path_node(path, l))
259 ? bch2_btree_node_upgrade(trans, path, l)
260 : bch2_btree_node_relock(trans, path, l)))
264 } while (l < path->locks_want);
267 * When we fail to get a lock, we have to ensure that any child nodes
268 * can't be relocked so bch2_btree_path_traverse has to walk back up to
269 * the node that we failed to relock:
272 __bch2_btree_path_unlock(path);
273 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
276 path->l[fail_idx].b = BTREE_ITER_NO_NODE_GET_LOCKS;
278 } while (fail_idx >= 0);
281 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
282 path->uptodate = BTREE_ITER_UPTODATE;
284 bch2_trans_verify_locks(trans);
286 return path->uptodate < BTREE_ITER_NEED_RELOCK;
289 static struct bpos btree_node_pos(struct btree_bkey_cached_common *_b,
293 ? container_of(_b, struct btree, c)->key.k.p
294 : container_of(_b, struct bkey_cached, c)->key.pos;
298 bool __bch2_btree_node_lock(struct btree_trans *trans,
299 struct btree_path *path,
301 struct bpos pos, unsigned level,
302 enum six_lock_type type,
303 six_lock_should_sleep_fn should_sleep_fn, void *p,
306 struct btree_path *linked;
309 /* Check if it's safe to block: */
310 trans_for_each_path(trans, linked) {
311 if (!linked->nodes_locked)
315 * Can't block taking an intent lock if we have _any_ nodes read
318 * - Our read lock blocks another thread with an intent lock on
319 * the same node from getting a write lock, and thus from
320 * dropping its intent lock
322 * - And the other thread may have multiple nodes intent locked:
323 * both the node we want to intent lock, and the node we
324 * already have read locked - deadlock:
326 if (type == SIX_LOCK_intent &&
327 linked->nodes_locked != linked->nodes_intent_locked) {
332 if (linked->btree_id != path->btree_id) {
333 if (linked->btree_id < path->btree_id)
341 * Within the same btree, non-cached paths come before cached
344 if (linked->cached != path->cached) {
353 * Interior nodes must be locked before their descendants: if
354 * another path has possible descendants locked of the node
355 * we're about to lock, it must have the ancestors locked too:
357 if (level > __fls(linked->nodes_locked)) {
362 /* Must lock btree nodes in key order: */
363 if (btree_node_locked(linked, level) &&
364 bpos_cmp(pos, btree_node_pos((void *) linked->l[level].b,
365 linked->cached)) <= 0) {
366 BUG_ON(trans->in_traverse_all);
372 return btree_node_lock_type(trans, path, b, pos, level,
373 type, should_sleep_fn, p);
375 trace_trans_restart_would_deadlock(trans->fn, ip,
376 trans->in_traverse_all, reason,
383 btree_trans_restart(trans);
387 /* Btree iterator locking: */
389 #ifdef CONFIG_BCACHEFS_DEBUG
391 static void bch2_btree_path_verify_locks(struct btree_path *path)
395 if (!path->nodes_locked) {
396 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
397 btree_path_node(path, path->level));
401 for (l = 0; btree_path_node(path, l); l++)
402 BUG_ON(btree_lock_want(path, l) !=
403 btree_node_locked_type(path, l));
406 void bch2_trans_verify_locks(struct btree_trans *trans)
408 struct btree_path *path;
410 trans_for_each_path(trans, path)
411 bch2_btree_path_verify_locks(path);
414 static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
417 /* Btree path locking: */
420 * Only for btree_cache.c - only relocks intent locks
422 bool bch2_btree_path_relock_intent(struct btree_trans *trans,
423 struct btree_path *path)
427 for (l = path->level;
428 l < path->locks_want && btree_path_node(path, l);
430 if (!bch2_btree_node_relock(trans, path, l)) {
431 __bch2_btree_path_unlock(path);
432 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
433 trace_trans_restart_relock_path_intent(trans->fn, _RET_IP_,
434 path->btree_id, &path->pos);
435 btree_trans_restart(trans);
444 static bool bch2_btree_path_relock(struct btree_trans *trans,
445 struct btree_path *path, unsigned long trace_ip)
447 bool ret = btree_path_get_locks(trans, path, false);
450 trace_trans_restart_relock_path(trans->fn, trace_ip,
451 path->btree_id, &path->pos);
452 btree_trans_restart(trans);
457 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
458 struct btree_path *path,
459 unsigned new_locks_want)
461 struct btree_path *linked;
463 EBUG_ON(path->locks_want >= new_locks_want);
465 path->locks_want = new_locks_want;
467 if (btree_path_get_locks(trans, path, true))
471 * XXX: this is ugly - we'd prefer to not be mucking with other
472 * iterators in the btree_trans here.
474 * On failure to upgrade the iterator, setting iter->locks_want and
475 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
476 * get the locks we want on transaction restart.
478 * But if this iterator was a clone, on transaction restart what we did
479 * to this iterator isn't going to be preserved.
481 * Possibly we could add an iterator field for the parent iterator when
482 * an iterator is a copy - for now, we'll just upgrade any other
483 * iterators with the same btree id.
485 * The code below used to be needed to ensure ancestor nodes get locked
486 * before interior nodes - now that's handled by
487 * bch2_btree_path_traverse_all().
489 if (!path->cached && !trans->in_traverse_all)
490 trans_for_each_path(trans, linked)
491 if (linked != path &&
492 linked->cached == path->cached &&
493 linked->btree_id == path->btree_id &&
494 linked->locks_want < new_locks_want) {
495 linked->locks_want = new_locks_want;
496 btree_path_get_locks(trans, linked, true);
502 void __bch2_btree_path_downgrade(struct btree_path *path,
503 unsigned new_locks_want)
507 EBUG_ON(path->locks_want < new_locks_want);
509 path->locks_want = new_locks_want;
511 while (path->nodes_locked &&
512 (l = __fls(path->nodes_locked)) >= path->locks_want) {
513 if (l > path->level) {
514 btree_node_unlock(path, l);
516 if (btree_node_intent_locked(path, l)) {
517 six_lock_downgrade(&path->l[l].b->c.lock);
518 path->nodes_intent_locked ^= 1 << l;
524 bch2_btree_path_verify_locks(path);
527 void bch2_trans_downgrade(struct btree_trans *trans)
529 struct btree_path *path;
531 trans_for_each_path(trans, path)
532 bch2_btree_path_downgrade(path);
535 /* Btree transaction locking: */
537 bool bch2_trans_relock(struct btree_trans *trans)
539 struct btree_path *path;
541 if (unlikely(trans->restarted))
544 trans_for_each_path(trans, path)
545 if (path->should_be_locked &&
546 !bch2_btree_path_relock(trans, path, _RET_IP_)) {
547 trace_trans_restart_relock(trans->fn, _RET_IP_,
548 path->btree_id, &path->pos);
549 BUG_ON(!trans->restarted);
555 void bch2_trans_unlock(struct btree_trans *trans)
557 struct btree_path *path;
559 trans_for_each_path(trans, path)
560 __bch2_btree_path_unlock(path);
563 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
564 * btree nodes, it implements its own walking:
566 BUG_ON(!trans->is_initial_gc &&
567 lock_class_is_held(&bch2_btree_node_lock_key));
570 /* Btree iterator: */
572 #ifdef CONFIG_BCACHEFS_DEBUG
574 static void bch2_btree_path_verify_cached(struct btree_trans *trans,
575 struct btree_path *path)
577 struct bkey_cached *ck;
578 bool locked = btree_node_locked(path, 0);
580 if (!bch2_btree_node_relock(trans, path, 0))
583 ck = (void *) path->l[0].b;
584 BUG_ON(ck->key.btree_id != path->btree_id ||
585 bkey_cmp(ck->key.pos, path->pos));
588 btree_node_unlock(path, 0);
591 static void bch2_btree_path_verify_level(struct btree_trans *trans,
592 struct btree_path *path, unsigned level)
594 struct btree_path_level *l;
595 struct btree_node_iter tmp;
597 struct bkey_packed *p, *k;
598 struct printbuf buf1 = PRINTBUF;
599 struct printbuf buf2 = PRINTBUF;
600 struct printbuf buf3 = PRINTBUF;
603 if (!bch2_debug_check_iterators)
608 locked = btree_node_locked(path, level);
612 bch2_btree_path_verify_cached(trans, path);
616 if (!btree_path_node(path, level))
619 if (!bch2_btree_node_relock(trans, path, level))
622 BUG_ON(!btree_path_pos_in_node(path, l->b));
624 bch2_btree_node_iter_verify(&l->iter, l->b);
627 * For interior nodes, the iterator will have skipped past deleted keys:
630 ? bch2_btree_node_iter_prev(&tmp, l->b)
631 : bch2_btree_node_iter_prev_all(&tmp, l->b);
632 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
634 if (p && bkey_iter_pos_cmp(l->b, p, &path->pos) >= 0) {
639 if (k && bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
645 btree_node_unlock(path, level);
648 bch2_bpos_to_text(&buf1, path->pos);
651 struct bkey uk = bkey_unpack_key(l->b, p);
652 bch2_bkey_to_text(&buf2, &uk);
654 prt_printf(&buf2, "(none)");
658 struct bkey uk = bkey_unpack_key(l->b, k);
659 bch2_bkey_to_text(&buf3, &uk);
661 prt_printf(&buf3, "(none)");
664 panic("path should be %s key at level %u:\n"
668 msg, level, buf1.buf, buf2.buf, buf3.buf);
671 static void bch2_btree_path_verify(struct btree_trans *trans,
672 struct btree_path *path)
674 struct bch_fs *c = trans->c;
677 EBUG_ON(path->btree_id >= BTREE_ID_NR);
679 for (i = 0; i < (!path->cached ? BTREE_MAX_DEPTH : 1); i++) {
681 BUG_ON(!path->cached &&
682 c->btree_roots[path->btree_id].b->c.level > i);
686 bch2_btree_path_verify_level(trans, path, i);
689 bch2_btree_path_verify_locks(path);
692 void bch2_trans_verify_paths(struct btree_trans *trans)
694 struct btree_path *path;
696 trans_for_each_path(trans, path)
697 bch2_btree_path_verify(trans, path);
700 static void bch2_btree_iter_verify(struct btree_iter *iter)
702 struct btree_trans *trans = iter->trans;
704 BUG_ON(iter->btree_id >= BTREE_ID_NR);
706 BUG_ON(!!(iter->flags & BTREE_ITER_CACHED) != iter->path->cached);
708 BUG_ON((iter->flags & BTREE_ITER_IS_EXTENTS) &&
709 (iter->flags & BTREE_ITER_ALL_SNAPSHOTS));
711 BUG_ON(!(iter->flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
712 (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
713 !btree_type_has_snapshots(iter->btree_id));
715 if (iter->update_path)
716 bch2_btree_path_verify(trans, iter->update_path);
717 bch2_btree_path_verify(trans, iter->path);
720 static void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter)
722 BUG_ON((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
723 !iter->pos.snapshot);
725 BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
726 iter->pos.snapshot != iter->snapshot);
728 BUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&iter->k)) < 0 ||
729 bkey_cmp(iter->pos, iter->k.p) > 0);
732 static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k)
734 struct btree_trans *trans = iter->trans;
735 struct btree_iter copy;
736 struct bkey_s_c prev;
739 if (!bch2_debug_check_iterators)
742 if (!(iter->flags & BTREE_ITER_FILTER_SNAPSHOTS))
745 if (bkey_err(k) || !k.k)
748 BUG_ON(!bch2_snapshot_is_ancestor(trans->c,
752 bch2_trans_iter_init(trans, ©, iter->btree_id, iter->pos,
753 BTREE_ITER_NOPRESERVE|
754 BTREE_ITER_ALL_SNAPSHOTS);
755 prev = bch2_btree_iter_prev(©);
759 ret = bkey_err(prev);
763 if (!bkey_cmp(prev.k->p, k.k->p) &&
764 bch2_snapshot_is_ancestor(trans->c, iter->snapshot,
765 prev.k->p.snapshot) > 0) {
766 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
768 bch2_bkey_to_text(&buf1, k.k);
769 bch2_bkey_to_text(&buf2, prev.k);
771 panic("iter snap %u\n"
778 bch2_trans_iter_exit(trans, ©);
782 void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
783 struct bpos pos, bool key_cache)
785 struct btree_path *path;
787 struct printbuf buf = PRINTBUF;
789 trans_for_each_path_inorder(trans, path, idx) {
790 int cmp = cmp_int(path->btree_id, id) ?:
791 cmp_int(path->cached, key_cache);
798 if (!(path->nodes_locked & 1) ||
799 !path->should_be_locked)
803 if (bkey_cmp(pos, path->l[0].b->data->min_key) >= 0 &&
804 bkey_cmp(pos, path->l[0].b->key.k.p) <= 0)
807 if (!bkey_cmp(pos, path->pos))
812 bch2_dump_trans_paths_updates(trans);
813 bch2_bpos_to_text(&buf, pos);
815 panic("not locked: %s %s%s\n",
816 bch2_btree_ids[id], buf.buf,
817 key_cache ? " cached" : "");
822 static inline void bch2_btree_path_verify_level(struct btree_trans *trans,
823 struct btree_path *path, unsigned l) {}
824 static inline void bch2_btree_path_verify(struct btree_trans *trans,
825 struct btree_path *path) {}
826 static inline void bch2_btree_iter_verify(struct btree_iter *iter) {}
827 static inline void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter) {}
828 static inline int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k) { return 0; }
832 /* Btree path: fixups after btree updates */
834 static void btree_node_iter_set_set_pos(struct btree_node_iter *iter,
837 struct bkey_packed *k)
839 struct btree_node_iter_set *set;
841 btree_node_iter_for_each(iter, set)
842 if (set->end == t->end_offset) {
843 set->k = __btree_node_key_to_offset(b, k);
844 bch2_btree_node_iter_sort(iter, b);
848 bch2_btree_node_iter_push(iter, b, k, btree_bkey_last(b, t));
851 static void __bch2_btree_path_fix_key_modified(struct btree_path *path,
853 struct bkey_packed *where)
855 struct btree_path_level *l = &path->l[b->c.level];
857 if (where != bch2_btree_node_iter_peek_all(&l->iter, l->b))
860 if (bkey_iter_pos_cmp(l->b, where, &path->pos) < 0)
861 bch2_btree_node_iter_advance(&l->iter, l->b);
864 void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
866 struct bkey_packed *where)
868 struct btree_path *path;
870 trans_for_each_path_with_node(trans, b, path) {
871 __bch2_btree_path_fix_key_modified(path, b, where);
872 bch2_btree_path_verify_level(trans, path, b->c.level);
876 static void __bch2_btree_node_iter_fix(struct btree_path *path,
878 struct btree_node_iter *node_iter,
880 struct bkey_packed *where,
881 unsigned clobber_u64s,
884 const struct bkey_packed *end = btree_bkey_last(b, t);
885 struct btree_node_iter_set *set;
886 unsigned offset = __btree_node_key_to_offset(b, where);
887 int shift = new_u64s - clobber_u64s;
888 unsigned old_end = t->end_offset - shift;
889 unsigned orig_iter_pos = node_iter->data[0].k;
890 bool iter_current_key_modified =
891 orig_iter_pos >= offset &&
892 orig_iter_pos <= offset + clobber_u64s;
894 btree_node_iter_for_each(node_iter, set)
895 if (set->end == old_end)
898 /* didn't find the bset in the iterator - might have to readd it: */
900 bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
901 bch2_btree_node_iter_push(node_iter, b, where, end);
904 /* Iterator is after key that changed */
908 set->end = t->end_offset;
910 /* Iterator hasn't gotten to the key that changed yet: */
915 bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
917 } else if (set->k < offset + clobber_u64s) {
918 set->k = offset + new_u64s;
919 if (set->k == set->end)
920 bch2_btree_node_iter_set_drop(node_iter, set);
922 /* Iterator is after key that changed */
923 set->k = (int) set->k + shift;
927 bch2_btree_node_iter_sort(node_iter, b);
929 if (node_iter->data[0].k != orig_iter_pos)
930 iter_current_key_modified = true;
933 * When a new key is added, and the node iterator now points to that
934 * key, the iterator might have skipped past deleted keys that should
935 * come after the key the iterator now points to. We have to rewind to
936 * before those deleted keys - otherwise
937 * bch2_btree_node_iter_prev_all() breaks:
939 if (!bch2_btree_node_iter_end(node_iter) &&
940 iter_current_key_modified &&
943 struct bkey_packed *k, *k2, *p;
945 k = bch2_btree_node_iter_peek_all(node_iter, b);
947 for_each_bset(b, t) {
948 bool set_pos = false;
950 if (node_iter->data[0].end == t->end_offset)
953 k2 = bch2_btree_node_iter_bset_pos(node_iter, b, t);
955 while ((p = bch2_bkey_prev_all(b, t, k2)) &&
956 bkey_iter_cmp(b, k, p) < 0) {
962 btree_node_iter_set_set_pos(node_iter,
968 void bch2_btree_node_iter_fix(struct btree_trans *trans,
969 struct btree_path *path,
971 struct btree_node_iter *node_iter,
972 struct bkey_packed *where,
973 unsigned clobber_u64s,
976 struct bset_tree *t = bch2_bkey_to_bset(b, where);
977 struct btree_path *linked;
979 if (node_iter != &path->l[b->c.level].iter) {
980 __bch2_btree_node_iter_fix(path, b, node_iter, t,
981 where, clobber_u64s, new_u64s);
983 if (bch2_debug_check_iterators)
984 bch2_btree_node_iter_verify(node_iter, b);
987 trans_for_each_path_with_node(trans, b, linked) {
988 __bch2_btree_node_iter_fix(linked, b,
989 &linked->l[b->c.level].iter, t,
990 where, clobber_u64s, new_u64s);
991 bch2_btree_path_verify_level(trans, linked, b->c.level);
995 /* Btree path level: pointer to a particular btree node and node iter */
997 static inline struct bkey_s_c __btree_iter_unpack(struct bch_fs *c,
998 struct btree_path_level *l,
1000 struct bkey_packed *k)
1004 * signal to bch2_btree_iter_peek_slot() that we're currently at
1007 u->type = KEY_TYPE_deleted;
1008 return bkey_s_c_null;
1011 return bkey_disassemble(l->b, k, u);
1014 static inline struct bkey_s_c btree_path_level_peek_all(struct bch_fs *c,
1015 struct btree_path_level *l,
1018 return __btree_iter_unpack(c, l, u,
1019 bch2_btree_node_iter_peek_all(&l->iter, l->b));
1022 static inline struct bkey_s_c btree_path_level_peek(struct bch_fs *c,
1023 struct btree_path *path,
1024 struct btree_path_level *l,
1027 struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1028 bch2_btree_node_iter_peek(&l->iter, l->b));
1030 path->pos = k.k ? k.k->p : l->b->key.k.p;
1034 static inline struct bkey_s_c btree_path_level_prev(struct bch_fs *c,
1035 struct btree_path *path,
1036 struct btree_path_level *l,
1039 struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1040 bch2_btree_node_iter_prev(&l->iter, l->b));
1042 path->pos = k.k ? k.k->p : l->b->data->min_key;
1046 static inline bool btree_path_advance_to_pos(struct btree_path *path,
1047 struct btree_path_level *l,
1050 struct bkey_packed *k;
1051 int nr_advanced = 0;
1053 while ((k = bch2_btree_node_iter_peek_all(&l->iter, l->b)) &&
1054 bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
1055 if (max_advance > 0 && nr_advanced >= max_advance)
1058 bch2_btree_node_iter_advance(&l->iter, l->b);
1066 * Verify that iterator for parent node points to child node:
1068 static void btree_path_verify_new_node(struct btree_trans *trans,
1069 struct btree_path *path, struct btree *b)
1071 struct bch_fs *c = trans->c;
1072 struct btree_path_level *l;
1075 struct bkey_packed *k;
1077 if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
1080 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1083 plevel = b->c.level + 1;
1084 if (!btree_path_node(path, plevel))
1087 parent_locked = btree_node_locked(path, plevel);
1089 if (!bch2_btree_node_relock(trans, path, plevel))
1092 l = &path->l[plevel];
1093 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1096 bkey_cmp_left_packed(l->b, k, &b->key.k.p)) {
1097 struct printbuf buf1 = PRINTBUF;
1098 struct printbuf buf2 = PRINTBUF;
1099 struct printbuf buf3 = PRINTBUF;
1100 struct printbuf buf4 = PRINTBUF;
1101 struct bkey uk = bkey_unpack_key(b, k);
1103 bch2_dump_btree_node(c, l->b);
1104 bch2_bpos_to_text(&buf1, path->pos);
1105 bch2_bkey_to_text(&buf2, &uk);
1106 bch2_bpos_to_text(&buf3, b->data->min_key);
1107 bch2_bpos_to_text(&buf3, b->data->max_key);
1108 panic("parent iter doesn't point to new node:\n"
1112 bch2_btree_ids[path->btree_id],
1113 buf1.buf, buf2.buf, buf3.buf, buf4.buf);
1117 btree_node_unlock(path, plevel);
1120 static inline void __btree_path_level_init(struct btree_path *path,
1123 struct btree_path_level *l = &path->l[level];
1125 bch2_btree_node_iter_init(&l->iter, l->b, &path->pos);
1128 * Iterators to interior nodes should always be pointed at the first non
1132 bch2_btree_node_iter_peek(&l->iter, l->b);
1135 static inline void btree_path_level_init(struct btree_trans *trans,
1136 struct btree_path *path,
1139 BUG_ON(path->cached);
1141 btree_path_verify_new_node(trans, path, b);
1143 EBUG_ON(!btree_path_pos_in_node(path, b));
1144 EBUG_ON(b->c.lock.state.seq & 1);
1146 path->l[b->c.level].lock_seq = b->c.lock.state.seq;
1147 path->l[b->c.level].b = b;
1148 __btree_path_level_init(path, b->c.level);
1151 /* Btree path: fixups after btree node updates: */
1154 * A btree node is being replaced - update the iterator to point to the new
1157 void bch2_trans_node_add(struct btree_trans *trans, struct btree *b)
1159 struct btree_path *path;
1161 trans_for_each_path(trans, path)
1162 if (!path->cached &&
1163 btree_path_pos_in_node(path, b)) {
1164 enum btree_node_locked_type t =
1165 btree_lock_want(path, b->c.level);
1167 if (path->nodes_locked &&
1168 t != BTREE_NODE_UNLOCKED) {
1169 btree_node_unlock(path, b->c.level);
1170 six_lock_increment(&b->c.lock, t);
1171 mark_btree_node_locked(trans, path, b->c.level, t);
1174 btree_path_level_init(trans, path, b);
1179 * A btree node has been modified in such a way as to invalidate iterators - fix
1182 void bch2_trans_node_reinit_iter(struct btree_trans *trans, struct btree *b)
1184 struct btree_path *path;
1186 trans_for_each_path_with_node(trans, b, path)
1187 __btree_path_level_init(path, b->c.level);
1190 /* Btree path: traverse, set_pos: */
1192 static int lock_root_check_fn(struct six_lock *lock, void *p)
1194 struct btree *b = container_of(lock, struct btree, c.lock);
1195 struct btree **rootp = p;
1197 return b == *rootp ? 0 : -1;
1200 static inline int btree_path_lock_root(struct btree_trans *trans,
1201 struct btree_path *path,
1202 unsigned depth_want,
1203 unsigned long trace_ip)
1205 struct bch_fs *c = trans->c;
1206 struct btree *b, **rootp = &c->btree_roots[path->btree_id].b;
1207 enum six_lock_type lock_type;
1210 EBUG_ON(path->nodes_locked);
1213 b = READ_ONCE(*rootp);
1214 path->level = READ_ONCE(b->c.level);
1216 if (unlikely(path->level < depth_want)) {
1218 * the root is at a lower depth than the depth we want:
1219 * got to the end of the btree, or we're walking nodes
1220 * greater than some depth and there are no nodes >=
1223 path->level = depth_want;
1224 for (i = path->level; i < BTREE_MAX_DEPTH; i++)
1225 path->l[i].b = NULL;
1229 lock_type = __btree_lock_want(path, path->level);
1230 if (unlikely(!btree_node_lock(trans, path, b, SPOS_MAX,
1231 path->level, lock_type,
1232 lock_root_check_fn, rootp,
1234 if (trans->restarted)
1239 if (likely(b == READ_ONCE(*rootp) &&
1240 b->c.level == path->level &&
1242 for (i = 0; i < path->level; i++)
1243 path->l[i].b = BTREE_ITER_NO_NODE_LOCK_ROOT;
1244 path->l[path->level].b = b;
1245 for (i = path->level + 1; i < BTREE_MAX_DEPTH; i++)
1246 path->l[i].b = NULL;
1248 mark_btree_node_locked(trans, path, path->level, lock_type);
1249 btree_path_level_init(trans, path, b);
1253 six_unlock_type(&b->c.lock, lock_type);
1258 static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *path)
1260 struct bch_fs *c = trans->c;
1261 struct btree_path_level *l = path_l(path);
1262 struct btree_node_iter node_iter = l->iter;
1263 struct bkey_packed *k;
1264 struct bkey_buf tmp;
1265 unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
1266 ? (path->level > 1 ? 0 : 2)
1267 : (path->level > 1 ? 1 : 16);
1268 bool was_locked = btree_node_locked(path, path->level);
1271 bch2_bkey_buf_init(&tmp);
1273 while (nr && !ret) {
1274 if (!bch2_btree_node_relock(trans, path, path->level))
1277 bch2_btree_node_iter_advance(&node_iter, l->b);
1278 k = bch2_btree_node_iter_peek(&node_iter, l->b);
1282 bch2_bkey_buf_unpack(&tmp, c, l->b, k);
1283 ret = bch2_btree_node_prefetch(c, trans, path, tmp.k, path->btree_id,
1288 btree_node_unlock(path, path->level);
1290 bch2_bkey_buf_exit(&tmp, c);
1294 static int btree_path_prefetch_j(struct btree_trans *trans, struct btree_path *path,
1295 struct btree_and_journal_iter *jiter)
1297 struct bch_fs *c = trans->c;
1299 struct bkey_buf tmp;
1300 unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
1301 ? (path->level > 1 ? 0 : 2)
1302 : (path->level > 1 ? 1 : 16);
1303 bool was_locked = btree_node_locked(path, path->level);
1306 bch2_bkey_buf_init(&tmp);
1308 while (nr && !ret) {
1309 if (!bch2_btree_node_relock(trans, path, path->level))
1312 bch2_btree_and_journal_iter_advance(jiter);
1313 k = bch2_btree_and_journal_iter_peek(jiter);
1317 bch2_bkey_buf_reassemble(&tmp, c, k);
1318 ret = bch2_btree_node_prefetch(c, trans, path, tmp.k, path->btree_id,
1323 btree_node_unlock(path, path->level);
1325 bch2_bkey_buf_exit(&tmp, c);
1329 static noinline void btree_node_mem_ptr_set(struct btree_trans *trans,
1330 struct btree_path *path,
1331 unsigned plevel, struct btree *b)
1333 struct btree_path_level *l = &path->l[plevel];
1334 bool locked = btree_node_locked(path, plevel);
1335 struct bkey_packed *k;
1336 struct bch_btree_ptr_v2 *bp;
1338 if (!bch2_btree_node_relock(trans, path, plevel))
1341 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1342 BUG_ON(k->type != KEY_TYPE_btree_ptr_v2);
1344 bp = (void *) bkeyp_val(&l->b->format, k);
1345 bp->mem_ptr = (unsigned long)b;
1348 btree_node_unlock(path, plevel);
1351 static noinline int btree_node_iter_and_journal_peek(struct btree_trans *trans,
1352 struct btree_path *path,
1354 struct bkey_buf *out)
1356 struct bch_fs *c = trans->c;
1357 struct btree_path_level *l = path_l(path);
1358 struct btree_and_journal_iter jiter;
1362 __bch2_btree_and_journal_iter_init_node_iter(&jiter, c, l->b, l->iter, path->pos);
1364 k = bch2_btree_and_journal_iter_peek(&jiter);
1366 bch2_bkey_buf_reassemble(out, c, k);
1368 if (flags & BTREE_ITER_PREFETCH)
1369 ret = btree_path_prefetch_j(trans, path, &jiter);
1371 bch2_btree_and_journal_iter_exit(&jiter);
1375 static __always_inline int btree_path_down(struct btree_trans *trans,
1376 struct btree_path *path,
1378 unsigned long trace_ip)
1380 struct bch_fs *c = trans->c;
1381 struct btree_path_level *l = path_l(path);
1383 unsigned level = path->level - 1;
1384 enum six_lock_type lock_type = __btree_lock_want(path, level);
1385 bool replay_done = test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags);
1386 struct bkey_buf tmp;
1389 EBUG_ON(!btree_node_locked(path, path->level));
1391 bch2_bkey_buf_init(&tmp);
1393 if (unlikely(!replay_done)) {
1394 ret = btree_node_iter_and_journal_peek(trans, path, flags, &tmp);
1398 bch2_bkey_buf_unpack(&tmp, c, l->b,
1399 bch2_btree_node_iter_peek(&l->iter, l->b));
1401 if (flags & BTREE_ITER_PREFETCH) {
1402 ret = btree_path_prefetch(trans, path);
1408 b = bch2_btree_node_get(trans, path, tmp.k, level, lock_type, trace_ip);
1409 ret = PTR_ERR_OR_ZERO(b);
1413 mark_btree_node_locked(trans, path, level, lock_type);
1414 btree_path_level_init(trans, path, b);
1416 if (likely(replay_done && tmp.k->k.type == KEY_TYPE_btree_ptr_v2) &&
1417 unlikely(b != btree_node_mem_ptr(tmp.k)))
1418 btree_node_mem_ptr_set(trans, path, level + 1, b);
1420 if (btree_node_read_locked(path, level + 1))
1421 btree_node_unlock(path, level + 1);
1422 path->level = level;
1424 bch2_btree_path_verify_locks(path);
1426 bch2_bkey_buf_exit(&tmp, c);
1430 static int btree_path_traverse_one(struct btree_trans *, struct btree_path *,
1431 unsigned, unsigned long);
1433 static int bch2_btree_path_traverse_all(struct btree_trans *trans)
1435 struct bch_fs *c = trans->c;
1436 struct btree_path *path;
1437 unsigned long trace_ip = _RET_IP_;
1440 if (trans->in_traverse_all)
1443 trans->in_traverse_all = true;
1445 trans->restarted = false;
1446 trans->traverse_all_idx = U8_MAX;
1448 trans_for_each_path(trans, path)
1449 path->should_be_locked = false;
1451 btree_trans_verify_sorted(trans);
1453 for (i = trans->nr_sorted - 2; i >= 0; --i) {
1454 struct btree_path *path1 = trans->paths + trans->sorted[i];
1455 struct btree_path *path2 = trans->paths + trans->sorted[i + 1];
1457 if (path1->btree_id == path2->btree_id &&
1458 path1->locks_want < path2->locks_want)
1459 __bch2_btree_path_upgrade(trans, path1, path2->locks_want);
1460 else if (!path1->locks_want && path2->locks_want)
1461 __bch2_btree_path_upgrade(trans, path1, 1);
1464 bch2_trans_unlock(trans);
1467 if (unlikely(trans->memory_allocation_failure)) {
1470 closure_init_stack(&cl);
1473 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
1478 /* Now, redo traversals in correct order: */
1479 trans->traverse_all_idx = 0;
1480 while (trans->traverse_all_idx < trans->nr_sorted) {
1481 path = trans->paths + trans->sorted[trans->traverse_all_idx];
1484 * Traversing a path can cause another path to be added at about
1485 * the same position:
1487 if (path->uptodate) {
1488 ret = btree_path_traverse_one(trans, path, 0, _THIS_IP_);
1489 if (ret == -EINTR || ret == -ENOMEM)
1493 BUG_ON(path->uptodate);
1495 trans->traverse_all_idx++;
1500 * BTREE_ITER_NEED_RELOCK is ok here - if we called bch2_trans_unlock()
1501 * and relock(), relock() won't relock since path->should_be_locked
1502 * isn't set yet, which is all fine
1504 trans_for_each_path(trans, path)
1505 BUG_ON(path->uptodate >= BTREE_ITER_NEED_TRAVERSE);
1507 bch2_btree_cache_cannibalize_unlock(c);
1509 trans->in_traverse_all = false;
1511 trace_trans_traverse_all(trans->fn, trace_ip);
1515 static inline bool btree_path_good_node(struct btree_trans *trans,
1516 struct btree_path *path,
1517 unsigned l, int check_pos)
1519 if (!is_btree_node(path, l) ||
1520 !bch2_btree_node_relock(trans, path, l))
1523 if (check_pos < 0 && btree_path_pos_before_node(path, path->l[l].b))
1525 if (check_pos > 0 && btree_path_pos_after_node(path, path->l[l].b))
1530 static void btree_path_set_level_up(struct btree_path *path)
1532 btree_node_unlock(path, path->level);
1533 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
1535 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1538 static void btree_path_set_level_down(struct btree_trans *trans,
1539 struct btree_path *path,
1544 path->level = new_level;
1546 for (l = path->level + 1; l < BTREE_MAX_DEPTH; l++)
1547 if (btree_lock_want(path, l) == BTREE_NODE_UNLOCKED)
1548 btree_node_unlock(path, l);
1550 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1551 bch2_btree_path_verify(trans, path);
1554 static inline unsigned btree_path_up_until_good_node(struct btree_trans *trans,
1555 struct btree_path *path,
1558 unsigned i, l = path->level;
1560 while (btree_path_node(path, l) &&
1561 !btree_path_good_node(trans, path, l, check_pos)) {
1562 btree_node_unlock(path, l);
1563 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1567 /* If we need intent locks, take them too: */
1569 i < path->locks_want && btree_path_node(path, i);
1571 if (!bch2_btree_node_relock(trans, path, i))
1573 btree_node_unlock(path, l);
1574 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1582 * This is the main state machine for walking down the btree - walks down to a
1585 * Returns 0 on success, -EIO on error (error reading in a btree node).
1587 * On error, caller (peek_node()/peek_key()) must return NULL; the error is
1588 * stashed in the iterator and returned from bch2_trans_exit().
1590 static int btree_path_traverse_one(struct btree_trans *trans,
1591 struct btree_path *path,
1593 unsigned long trace_ip)
1595 unsigned depth_want = path->level;
1598 if (unlikely(trans->restarted)) {
1604 * Ensure we obey path->should_be_locked: if it's set, we can't unlock
1605 * and re-traverse the path without a transaction restart:
1607 if (path->should_be_locked) {
1608 ret = bch2_btree_path_relock(trans, path, trace_ip) ? 0 : -EINTR;
1613 ret = bch2_btree_path_traverse_cached(trans, path, flags);
1617 if (unlikely(path->level >= BTREE_MAX_DEPTH))
1620 path->level = btree_path_up_until_good_node(trans, path, 0);
1623 * Note: path->nodes[path->level] may be temporarily NULL here - that
1624 * would indicate to other code that we got to the end of the btree,
1625 * here it indicates that relocking the root failed - it's critical that
1626 * btree_path_lock_root() comes next and that it can't fail
1628 while (path->level > depth_want) {
1629 ret = btree_path_node(path, path->level)
1630 ? btree_path_down(trans, path, flags, trace_ip)
1631 : btree_path_lock_root(trans, path, depth_want, trace_ip);
1632 if (unlikely(ret)) {
1635 * No nodes at this level - got to the end of
1642 __bch2_btree_path_unlock(path);
1643 path->level = depth_want;
1646 path->l[path->level].b =
1647 BTREE_ITER_NO_NODE_ERROR;
1649 path->l[path->level].b =
1650 BTREE_ITER_NO_NODE_DOWN;
1655 path->uptodate = BTREE_ITER_UPTODATE;
1657 BUG_ON((ret == -EINTR) != !!trans->restarted);
1658 bch2_btree_path_verify(trans, path);
1662 int __must_check bch2_btree_path_traverse(struct btree_trans *trans,
1663 struct btree_path *path, unsigned flags)
1665 if (path->uptodate < BTREE_ITER_NEED_RELOCK)
1668 return bch2_trans_cond_resched(trans) ?:
1669 btree_path_traverse_one(trans, path, flags, _RET_IP_);
1672 static void btree_path_copy(struct btree_trans *trans, struct btree_path *dst,
1673 struct btree_path *src)
1675 unsigned i, offset = offsetof(struct btree_path, pos);
1677 memcpy((void *) dst + offset,
1678 (void *) src + offset,
1679 sizeof(struct btree_path) - offset);
1681 for (i = 0; i < BTREE_MAX_DEPTH; i++)
1682 if (btree_node_locked(dst, i))
1683 six_lock_increment(&dst->l[i].b->c.lock,
1684 __btree_lock_want(dst, i));
1686 bch2_btree_path_check_sort(trans, dst, 0);
1689 static struct btree_path *btree_path_clone(struct btree_trans *trans, struct btree_path *src,
1692 struct btree_path *new = btree_path_alloc(trans, src);
1694 btree_path_copy(trans, new, src);
1695 __btree_path_get(new, intent);
1699 inline struct btree_path * __must_check
1700 bch2_btree_path_make_mut(struct btree_trans *trans,
1701 struct btree_path *path, bool intent,
1704 if (path->ref > 1 || path->preserve) {
1705 __btree_path_put(path, intent);
1706 path = btree_path_clone(trans, path, intent);
1707 path->preserve = false;
1708 #ifdef CONFIG_BCACHEFS_DEBUG
1709 path->ip_allocated = ip;
1711 btree_trans_verify_sorted(trans);
1714 path->should_be_locked = false;
1718 struct btree_path * __must_check
1719 bch2_btree_path_set_pos(struct btree_trans *trans,
1720 struct btree_path *path, struct bpos new_pos,
1721 bool intent, unsigned long ip)
1723 int cmp = bpos_cmp(new_pos, path->pos);
1724 unsigned l = path->level;
1726 EBUG_ON(trans->restarted);
1727 EBUG_ON(!path->ref);
1732 path = bch2_btree_path_make_mut(trans, path, intent, ip);
1734 path->pos = new_pos;
1736 bch2_btree_path_check_sort(trans, path, cmp);
1738 if (unlikely(path->cached)) {
1739 btree_node_unlock(path, 0);
1740 path->l[0].b = BTREE_ITER_NO_NODE_CACHED;
1741 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1745 l = btree_path_up_until_good_node(trans, path, cmp);
1747 if (btree_path_node(path, l)) {
1748 BUG_ON(!btree_node_locked(path, l));
1750 * We might have to skip over many keys, or just a few: try
1751 * advancing the node iterator, and if we have to skip over too
1752 * many keys just reinit it (or if we're rewinding, since that
1756 !btree_path_advance_to_pos(path, &path->l[l], 8))
1757 __btree_path_level_init(path, l);
1760 if (l != path->level) {
1761 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1762 __bch2_btree_path_unlock(path);
1765 bch2_btree_path_verify(trans, path);
1769 /* Btree path: main interface: */
1771 static struct btree_path *have_path_at_pos(struct btree_trans *trans, struct btree_path *path)
1773 struct btree_path *next;
1775 next = prev_btree_path(trans, path);
1776 if (next && !btree_path_cmp(next, path))
1779 next = next_btree_path(trans, path);
1780 if (next && !btree_path_cmp(next, path))
1786 static struct btree_path *have_node_at_pos(struct btree_trans *trans, struct btree_path *path)
1788 struct btree_path *next;
1790 next = prev_btree_path(trans, path);
1791 if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1794 next = next_btree_path(trans, path);
1795 if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1801 static inline void __bch2_path_free(struct btree_trans *trans, struct btree_path *path)
1803 __bch2_btree_path_unlock(path);
1804 btree_path_list_remove(trans, path);
1805 trans->paths_allocated &= ~(1ULL << path->idx);
1808 void bch2_path_put(struct btree_trans *trans, struct btree_path *path, bool intent)
1810 struct btree_path *dup;
1812 EBUG_ON(trans->paths + path->idx != path);
1813 EBUG_ON(!path->ref);
1815 if (!__btree_path_put(path, intent))
1819 * Perhaps instead we should check for duplicate paths in traverse_all:
1821 if (path->preserve &&
1822 (dup = have_path_at_pos(trans, path))) {
1823 dup->preserve = true;
1824 path->preserve = false;
1828 if (!path->preserve &&
1829 (dup = have_node_at_pos(trans, path)))
1833 if (path->should_be_locked &&
1834 !btree_node_locked(dup, path->level))
1837 dup->should_be_locked |= path->should_be_locked;
1838 __bch2_path_free(trans, path);
1841 void bch2_trans_updates_to_text(struct printbuf *buf, struct btree_trans *trans)
1843 struct btree_insert_entry *i;
1845 prt_printf(buf, "transaction updates for %s journal seq %llu",
1846 trans->fn, trans->journal_res.seq);
1848 printbuf_indent_add(buf, 2);
1850 trans_for_each_update(trans, i) {
1851 struct bkey_s_c old = { &i->old_k, i->old_v };
1853 prt_printf(buf, "update: btree=%s cached=%u %pS",
1854 bch2_btree_ids[i->btree_id],
1856 (void *) i->ip_allocated);
1859 prt_printf(buf, " old ");
1860 bch2_bkey_val_to_text(buf, trans->c, old);
1863 prt_printf(buf, " new ");
1864 bch2_bkey_val_to_text(buf, trans->c, bkey_i_to_s_c(i->k));
1868 printbuf_indent_sub(buf, 2);
1872 void bch2_dump_trans_updates(struct btree_trans *trans)
1874 struct printbuf buf = PRINTBUF;
1876 bch2_trans_updates_to_text(&buf, trans);
1877 bch_err(trans->c, "%s", buf.buf);
1878 printbuf_exit(&buf);
1882 void bch2_dump_trans_paths_updates(struct btree_trans *trans)
1884 struct btree_path *path;
1885 struct printbuf buf = PRINTBUF;
1888 trans_for_each_path_inorder(trans, path, idx) {
1889 printbuf_reset(&buf);
1891 bch2_bpos_to_text(&buf, path->pos);
1893 printk(KERN_ERR "path: idx %u ref %u:%u%s%s btree=%s l=%u pos %s locks %u %pS\n",
1894 path->idx, path->ref, path->intent_ref,
1895 path->should_be_locked ? " S" : "",
1896 path->preserve ? " P" : "",
1897 bch2_btree_ids[path->btree_id],
1901 #ifdef CONFIG_BCACHEFS_DEBUG
1902 (void *) path->ip_allocated
1909 printbuf_exit(&buf);
1911 bch2_dump_trans_updates(trans);
1914 static struct btree_path *btree_path_alloc(struct btree_trans *trans,
1915 struct btree_path *pos)
1917 struct btree_path *path;
1920 if (unlikely(trans->paths_allocated ==
1921 ~((~0ULL << 1) << (BTREE_ITER_MAX - 1)))) {
1922 bch2_dump_trans_paths_updates(trans);
1923 panic("trans path oveflow\n");
1926 idx = __ffs64(~trans->paths_allocated);
1927 trans->paths_allocated |= 1ULL << idx;
1929 path = &trans->paths[idx];
1933 path->intent_ref = 0;
1934 path->nodes_locked = 0;
1935 path->nodes_intent_locked = 0;
1937 btree_path_list_add(trans, pos, path);
1941 struct btree_path *bch2_path_get(struct btree_trans *trans,
1942 enum btree_id btree_id, struct bpos pos,
1943 unsigned locks_want, unsigned level,
1944 unsigned flags, unsigned long ip)
1946 struct btree_path *path, *path_pos = NULL;
1947 bool cached = flags & BTREE_ITER_CACHED;
1948 bool intent = flags & BTREE_ITER_INTENT;
1951 BUG_ON(trans->restarted);
1952 btree_trans_verify_sorted(trans);
1953 bch2_trans_verify_locks(trans);
1955 trans_for_each_path_inorder(trans, path, i) {
1956 if (__btree_path_cmp(path,
1967 path_pos->cached == cached &&
1968 path_pos->btree_id == btree_id &&
1969 path_pos->level == level) {
1970 __btree_path_get(path_pos, intent);
1971 path = bch2_btree_path_set_pos(trans, path_pos, pos, intent, ip);
1973 path = btree_path_alloc(trans, path_pos);
1976 __btree_path_get(path, intent);
1978 path->btree_id = btree_id;
1979 path->cached = cached;
1980 path->uptodate = BTREE_ITER_NEED_TRAVERSE;
1981 path->should_be_locked = false;
1982 path->level = level;
1983 path->locks_want = locks_want;
1984 path->nodes_locked = 0;
1985 path->nodes_intent_locked = 0;
1986 for (i = 0; i < ARRAY_SIZE(path->l); i++)
1987 path->l[i].b = BTREE_ITER_NO_NODE_INIT;
1988 #ifdef CONFIG_BCACHEFS_DEBUG
1989 path->ip_allocated = ip;
1991 btree_trans_verify_sorted(trans);
1994 if (!(flags & BTREE_ITER_NOPRESERVE))
1995 path->preserve = true;
1997 if (path->intent_ref)
1998 locks_want = max(locks_want, level + 1);
2001 * If the path has locks_want greater than requested, we don't downgrade
2002 * it here - on transaction restart because btree node split needs to
2003 * upgrade locks, we might be putting/getting the iterator again.
2004 * Downgrading iterators only happens via bch2_trans_downgrade(), after
2005 * a successful transaction commit.
2008 locks_want = min(locks_want, BTREE_MAX_DEPTH);
2009 if (locks_want > path->locks_want) {
2010 path->locks_want = locks_want;
2011 btree_path_get_locks(trans, path, true);
2017 inline struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *path, struct bkey *u)
2022 if (!path->cached) {
2023 struct btree_path_level *l = path_l(path);
2024 struct bkey_packed *_k;
2026 EBUG_ON(path->uptodate != BTREE_ITER_UPTODATE);
2028 _k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
2029 k = _k ? bkey_disassemble(l->b, _k, u) : bkey_s_c_null;
2031 EBUG_ON(k.k && bkey_deleted(k.k) && bpos_cmp(k.k->p, path->pos) == 0);
2033 if (!k.k || bpos_cmp(path->pos, k.k->p))
2036 struct bkey_cached *ck = (void *) path->l[0].b;
2039 (path->btree_id != ck->key.btree_id ||
2040 bkey_cmp(path->pos, ck->key.pos)));
2042 /* BTREE_ITER_CACHED_NOFILL|BTREE_ITER_CACHED_NOCREATE? */
2043 if (unlikely(!ck || !ck->valid))
2044 return bkey_s_c_null;
2046 EBUG_ON(path->uptodate != BTREE_ITER_UPTODATE);
2049 k = bkey_i_to_s_c(ck->k);
2056 return (struct bkey_s_c) { u, NULL };
2059 /* Btree iterators: */
2062 __bch2_btree_iter_traverse(struct btree_iter *iter)
2064 return bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
2068 bch2_btree_iter_traverse(struct btree_iter *iter)
2072 iter->path = bch2_btree_path_set_pos(iter->trans, iter->path,
2073 btree_iter_search_key(iter),
2074 iter->flags & BTREE_ITER_INTENT,
2075 btree_iter_ip_allocated(iter));
2077 ret = bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
2081 iter->path->should_be_locked = true;
2085 /* Iterate across nodes (leaf and interior nodes) */
2087 struct btree *bch2_btree_iter_peek_node(struct btree_iter *iter)
2089 struct btree_trans *trans = iter->trans;
2090 struct btree *b = NULL;
2093 EBUG_ON(iter->path->cached);
2094 bch2_btree_iter_verify(iter);
2096 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2100 b = btree_path_node(iter->path, iter->path->level);
2104 BUG_ON(bpos_cmp(b->key.k.p, iter->pos) < 0);
2106 bkey_init(&iter->k);
2107 iter->k.p = iter->pos = b->key.k.p;
2109 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p,
2110 iter->flags & BTREE_ITER_INTENT,
2111 btree_iter_ip_allocated(iter));
2112 iter->path->should_be_locked = true;
2113 BUG_ON(iter->path->uptodate);
2115 bch2_btree_iter_verify_entry_exit(iter);
2116 bch2_btree_iter_verify(iter);
2124 struct btree *bch2_btree_iter_next_node(struct btree_iter *iter)
2126 struct btree_trans *trans = iter->trans;
2127 struct btree_path *path = iter->path;
2128 struct btree *b = NULL;
2131 BUG_ON(trans->restarted);
2132 EBUG_ON(iter->path->cached);
2133 bch2_btree_iter_verify(iter);
2135 /* already at end? */
2136 if (!btree_path_node(path, path->level))
2140 if (!btree_path_node(path, path->level + 1)) {
2141 btree_path_set_level_up(path);
2145 if (!bch2_btree_node_relock(trans, path, path->level + 1)) {
2146 __bch2_btree_path_unlock(path);
2147 path->l[path->level].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2148 path->l[path->level + 1].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2149 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
2150 trace_trans_restart_relock_next_node(trans->fn, _THIS_IP_,
2151 path->btree_id, &path->pos);
2152 btree_trans_restart(trans);
2157 b = btree_path_node(path, path->level + 1);
2159 if (!bpos_cmp(iter->pos, b->key.k.p)) {
2160 btree_node_unlock(path, path->level);
2161 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
2165 * Haven't gotten to the end of the parent node: go back down to
2166 * the next child node
2169 bch2_btree_path_set_pos(trans, path, bpos_successor(iter->pos),
2170 iter->flags & BTREE_ITER_INTENT,
2171 btree_iter_ip_allocated(iter));
2173 btree_path_set_level_down(trans, path, iter->min_depth);
2175 ret = bch2_btree_path_traverse(trans, path, iter->flags);
2179 b = path->l[path->level].b;
2182 bkey_init(&iter->k);
2183 iter->k.p = iter->pos = b->key.k.p;
2185 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p,
2186 iter->flags & BTREE_ITER_INTENT,
2187 btree_iter_ip_allocated(iter));
2188 iter->path->should_be_locked = true;
2189 BUG_ON(iter->path->uptodate);
2191 bch2_btree_iter_verify_entry_exit(iter);
2192 bch2_btree_iter_verify(iter);
2200 /* Iterate across keys (in leaf nodes only) */
2202 inline bool bch2_btree_iter_advance(struct btree_iter *iter)
2204 if (likely(!(iter->flags & BTREE_ITER_ALL_LEVELS))) {
2205 struct bpos pos = iter->k.p;
2206 bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2207 ? bpos_cmp(pos, SPOS_MAX)
2208 : bkey_cmp(pos, SPOS_MAX)) != 0;
2210 if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2211 pos = bkey_successor(iter, pos);
2212 bch2_btree_iter_set_pos(iter, pos);
2215 if (!btree_path_node(iter->path, iter->path->level))
2218 iter->advanced = true;
2223 inline bool bch2_btree_iter_rewind(struct btree_iter *iter)
2225 struct bpos pos = bkey_start_pos(&iter->k);
2226 bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2227 ? bpos_cmp(pos, POS_MIN)
2228 : bkey_cmp(pos, POS_MIN)) != 0;
2230 if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2231 pos = bkey_predecessor(iter, pos);
2232 bch2_btree_iter_set_pos(iter, pos);
2236 static inline struct bkey_i *btree_trans_peek_updates(struct btree_trans *trans,
2237 enum btree_id btree_id,
2240 struct btree_insert_entry *i;
2241 struct bkey_i *ret = NULL;
2243 trans_for_each_update(trans, i) {
2244 if (i->btree_id < btree_id)
2246 if (i->btree_id > btree_id)
2248 if (bpos_cmp(i->k->k.p, pos) < 0)
2250 if (i->key_cache_already_flushed)
2252 if (!ret || bpos_cmp(i->k->k.p, ret->k.p) < 0)
2259 struct bkey_i *bch2_btree_journal_peek(struct btree_trans *trans,
2260 struct btree_iter *iter,
2261 struct bpos start_pos,
2262 struct bpos end_pos)
2266 if (bpos_cmp(start_pos, iter->journal_pos) < 0)
2267 iter->journal_idx = 0;
2269 k = bch2_journal_keys_peek_upto(trans->c, iter->btree_id, 0,
2271 &iter->journal_idx);
2273 iter->journal_pos = k ? k->k.p : end_pos;
2277 struct bkey_i *bch2_btree_journal_peek_slot(struct btree_trans *trans,
2278 struct btree_iter *iter,
2281 return bch2_btree_journal_peek(trans, iter, pos, pos);
2285 struct bkey_s_c btree_trans_peek_journal(struct btree_trans *trans,
2286 struct btree_iter *iter,
2289 struct bkey_i *next_journal =
2290 bch2_btree_journal_peek(trans, iter, iter->path->pos,
2291 k.k ? k.k->p : iter->path->l[0].b->key.k.p);
2294 iter->k = next_journal->k;
2295 k = bkey_i_to_s_c(next_journal);
2302 * Checks btree key cache for key at iter->pos and returns it if present, or
2306 struct bkey_s_c btree_trans_peek_key_cache(struct btree_iter *iter, struct bpos pos)
2308 struct btree_trans *trans = iter->trans;
2309 struct bch_fs *c = trans->c;
2313 if (!bch2_btree_key_cache_find(c, iter->btree_id, pos))
2314 return bkey_s_c_null;
2316 if (!iter->key_cache_path)
2317 iter->key_cache_path = bch2_path_get(trans, iter->btree_id, pos,
2318 iter->flags & BTREE_ITER_INTENT, 0,
2319 iter->flags|BTREE_ITER_CACHED,
2322 iter->key_cache_path = bch2_btree_path_set_pos(trans, iter->key_cache_path, pos,
2323 iter->flags & BTREE_ITER_INTENT,
2324 btree_iter_ip_allocated(iter));
2326 ret = bch2_btree_path_traverse(trans, iter->key_cache_path, iter->flags|BTREE_ITER_CACHED);
2328 return bkey_s_c_err(ret);
2330 iter->key_cache_path->should_be_locked = true;
2332 return bch2_btree_path_peek_slot(iter->key_cache_path, &u);
2335 static struct bkey_s_c __bch2_btree_iter_peek(struct btree_iter *iter, struct bpos search_key)
2337 struct btree_trans *trans = iter->trans;
2338 struct bkey_i *next_update;
2339 struct bkey_s_c k, k2;
2342 EBUG_ON(iter->path->cached || iter->path->level);
2343 bch2_btree_iter_verify(iter);
2346 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2347 iter->flags & BTREE_ITER_INTENT,
2348 btree_iter_ip_allocated(iter));
2350 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2351 if (unlikely(ret)) {
2352 /* ensure that iter->k is consistent with iter->pos: */
2353 bch2_btree_iter_set_pos(iter, iter->pos);
2354 k = bkey_s_c_err(ret);
2358 iter->path->should_be_locked = true;
2360 k = btree_path_level_peek_all(trans->c, &iter->path->l[0], &iter->k);
2362 if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) &&
2364 (k2 = btree_trans_peek_key_cache(iter, k.k->p)).k) {
2368 bch2_btree_iter_set_pos(iter, iter->pos);
2376 if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL))
2377 k = btree_trans_peek_journal(trans, iter, k);
2379 next_update = iter->flags & BTREE_ITER_WITH_UPDATES
2380 ? btree_trans_peek_updates(trans, iter->btree_id, search_key)
2383 bpos_cmp(next_update->k.p,
2384 k.k ? k.k->p : iter->path->l[0].b->key.k.p) <= 0) {
2385 iter->k = next_update->k;
2386 k = bkey_i_to_s_c(next_update);
2389 if (k.k && bkey_deleted(k.k)) {
2391 * If we've got a whiteout, and it's after the search
2392 * key, advance the search key to the whiteout instead
2393 * of just after the whiteout - it might be a btree
2394 * whiteout, with a real key at the same position, since
2395 * in the btree deleted keys sort before non deleted.
2397 search_key = bpos_cmp(search_key, k.k->p)
2399 : bpos_successor(k.k->p);
2405 } else if (likely(bpos_cmp(iter->path->l[0].b->key.k.p, SPOS_MAX))) {
2406 /* Advance to next leaf node: */
2407 search_key = bpos_successor(iter->path->l[0].b->key.k.p);
2410 bch2_btree_iter_set_pos(iter, SPOS_MAX);
2416 bch2_btree_iter_verify(iter);
2422 * bch2_btree_iter_peek: returns first key greater than or equal to iterator's
2425 struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *iter, struct bpos end)
2427 struct btree_trans *trans = iter->trans;
2428 struct bpos search_key = btree_iter_search_key(iter);
2430 struct bpos iter_pos;
2433 EBUG_ON(iter->flags & BTREE_ITER_ALL_LEVELS);
2435 if (iter->update_path) {
2436 bch2_path_put(trans, iter->update_path,
2437 iter->flags & BTREE_ITER_INTENT);
2438 iter->update_path = NULL;
2441 bch2_btree_iter_verify_entry_exit(iter);
2444 k = __bch2_btree_iter_peek(iter, search_key);
2445 if (!k.k || bkey_err(k))
2449 * iter->pos should be mononotically increasing, and always be
2450 * equal to the key we just returned - except extents can
2451 * straddle iter->pos:
2453 if (!(iter->flags & BTREE_ITER_IS_EXTENTS))
2455 else if (bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0)
2456 iter_pos = bkey_start_pos(k.k);
2458 iter_pos = iter->pos;
2460 if (bkey_cmp(iter_pos, end) > 0) {
2461 bch2_btree_iter_set_pos(iter, end);
2466 if (iter->update_path &&
2467 bkey_cmp(iter->update_path->pos, k.k->p)) {
2468 bch2_path_put(trans, iter->update_path,
2469 iter->flags & BTREE_ITER_INTENT);
2470 iter->update_path = NULL;
2473 if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
2474 (iter->flags & BTREE_ITER_INTENT) &&
2475 !(iter->flags & BTREE_ITER_IS_EXTENTS) &&
2476 !iter->update_path) {
2477 struct bpos pos = k.k->p;
2479 if (pos.snapshot < iter->snapshot) {
2480 search_key = bpos_successor(k.k->p);
2484 pos.snapshot = iter->snapshot;
2487 * advance, same as on exit for iter->path, but only up
2490 __btree_path_get(iter->path, iter->flags & BTREE_ITER_INTENT);
2491 iter->update_path = iter->path;
2493 iter->update_path = bch2_btree_path_set_pos(trans,
2494 iter->update_path, pos,
2495 iter->flags & BTREE_ITER_INTENT,
2500 * We can never have a key in a leaf node at POS_MAX, so
2501 * we don't have to check these successor() calls:
2503 if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
2504 !bch2_snapshot_is_ancestor(trans->c,
2507 search_key = bpos_successor(k.k->p);
2511 if (bkey_whiteout(k.k) &&
2512 !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2513 search_key = bkey_successor(iter, k.k->p);
2520 iter->pos = iter_pos;
2522 iter->path = bch2_btree_path_set_pos(trans, iter->path, k.k->p,
2523 iter->flags & BTREE_ITER_INTENT,
2524 btree_iter_ip_allocated(iter));
2525 BUG_ON(!iter->path->nodes_locked);
2527 if (iter->update_path) {
2528 if (iter->update_path->uptodate &&
2529 !bch2_btree_path_relock(trans, iter->update_path, _THIS_IP_)) {
2530 k = bkey_s_c_err(-EINTR);
2532 BUG_ON(!(iter->update_path->nodes_locked & 1));
2533 iter->update_path->should_be_locked = true;
2536 iter->path->should_be_locked = true;
2538 if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
2539 iter->pos.snapshot = iter->snapshot;
2541 ret = bch2_btree_iter_verify_ret(iter, k);
2542 if (unlikely(ret)) {
2543 bch2_btree_iter_set_pos(iter, iter->pos);
2544 k = bkey_s_c_err(ret);
2547 bch2_btree_iter_verify_entry_exit(iter);
2553 * bch2_btree_iter_peek_all_levels: returns the first key greater than or equal
2554 * to iterator's current position, returning keys from every level of the btree.
2555 * For keys at different levels of the btree that compare equal, the key from
2556 * the lower level (leaf) is returned first.
2558 struct bkey_s_c bch2_btree_iter_peek_all_levels(struct btree_iter *iter)
2560 struct btree_trans *trans = iter->trans;
2564 EBUG_ON(iter->path->cached);
2565 bch2_btree_iter_verify(iter);
2566 BUG_ON(iter->path->level < iter->min_depth);
2567 BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS));
2568 EBUG_ON(!(iter->flags & BTREE_ITER_ALL_LEVELS));
2571 iter->path = bch2_btree_path_set_pos(trans, iter->path, iter->pos,
2572 iter->flags & BTREE_ITER_INTENT,
2573 btree_iter_ip_allocated(iter));
2575 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2576 if (unlikely(ret)) {
2577 /* ensure that iter->k is consistent with iter->pos: */
2578 bch2_btree_iter_set_pos(iter, iter->pos);
2579 k = bkey_s_c_err(ret);
2583 /* Already at end? */
2584 if (!btree_path_node(iter->path, iter->path->level)) {
2589 k = btree_path_level_peek_all(trans->c,
2590 &iter->path->l[iter->path->level], &iter->k);
2592 /* Check if we should go up to the parent node: */
2595 !bpos_cmp(path_l(iter->path)->b->key.k.p, iter->pos))) {
2596 iter->pos = path_l(iter->path)->b->key.k.p;
2597 btree_path_set_level_up(iter->path);
2598 iter->advanced = false;
2603 * Check if we should go back down to a leaf:
2604 * If we're not in a leaf node, we only return the current key
2605 * if it exactly matches iter->pos - otherwise we first have to
2606 * go back to the leaf:
2608 if (iter->path->level != iter->min_depth &&
2611 bpos_cmp(iter->pos, k.k->p))) {
2612 btree_path_set_level_down(trans, iter->path, iter->min_depth);
2613 iter->pos = bpos_successor(iter->pos);
2614 iter->advanced = false;
2618 /* Check if we should go to the next key: */
2619 if (iter->path->level == iter->min_depth &&
2622 !bpos_cmp(iter->pos, k.k->p)) {
2623 iter->pos = bpos_successor(iter->pos);
2624 iter->advanced = false;
2628 if (iter->advanced &&
2629 iter->path->level == iter->min_depth &&
2630 bpos_cmp(k.k->p, iter->pos))
2631 iter->advanced = false;
2633 BUG_ON(iter->advanced);
2640 iter->path->should_be_locked = true;
2641 bch2_btree_iter_verify(iter);
2647 * bch2_btree_iter_next: returns first key greater than iterator's current
2650 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *iter)
2652 if (!bch2_btree_iter_advance(iter))
2653 return bkey_s_c_null;
2655 return bch2_btree_iter_peek(iter);
2659 * bch2_btree_iter_peek_prev: returns first key less than or equal to
2660 * iterator's current position
2662 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter)
2664 struct btree_trans *trans = iter->trans;
2665 struct bpos search_key = iter->pos;
2666 struct btree_path *saved_path = NULL;
2668 struct bkey saved_k;
2669 const struct bch_val *saved_v;
2672 EBUG_ON(iter->path->cached || iter->path->level);
2673 EBUG_ON(iter->flags & BTREE_ITER_WITH_UPDATES);
2675 if (iter->flags & BTREE_ITER_WITH_JOURNAL)
2676 return bkey_s_c_err(-EIO);
2678 bch2_btree_iter_verify(iter);
2679 bch2_btree_iter_verify_entry_exit(iter);
2681 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2682 search_key.snapshot = U32_MAX;
2685 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2686 iter->flags & BTREE_ITER_INTENT,
2687 btree_iter_ip_allocated(iter));
2689 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2690 if (unlikely(ret)) {
2691 /* ensure that iter->k is consistent with iter->pos: */
2692 bch2_btree_iter_set_pos(iter, iter->pos);
2693 k = bkey_s_c_err(ret);
2697 k = btree_path_level_peek(trans->c, iter->path,
2698 &iter->path->l[0], &iter->k);
2700 ((iter->flags & BTREE_ITER_IS_EXTENTS)
2701 ? bpos_cmp(bkey_start_pos(k.k), search_key) >= 0
2702 : bpos_cmp(k.k->p, search_key) > 0))
2703 k = btree_path_level_prev(trans->c, iter->path,
2704 &iter->path->l[0], &iter->k);
2706 bch2_btree_path_check_sort(trans, iter->path, 0);
2709 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) {
2710 if (k.k->p.snapshot == iter->snapshot)
2714 * If we have a saved candidate, and we're no
2715 * longer at the same _key_ (not pos), return
2718 if (saved_path && bkey_cmp(k.k->p, saved_k.p)) {
2719 bch2_path_put(trans, iter->path,
2720 iter->flags & BTREE_ITER_INTENT);
2721 iter->path = saved_path;
2728 if (bch2_snapshot_is_ancestor(iter->trans->c,
2732 bch2_path_put(trans, saved_path,
2733 iter->flags & BTREE_ITER_INTENT);
2734 saved_path = btree_path_clone(trans, iter->path,
2735 iter->flags & BTREE_ITER_INTENT);
2740 search_key = bpos_predecessor(k.k->p);
2744 if (bkey_whiteout(k.k) &&
2745 !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2746 search_key = bkey_predecessor(iter, k.k->p);
2747 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2748 search_key.snapshot = U32_MAX;
2753 } else if (likely(bpos_cmp(iter->path->l[0].b->data->min_key, POS_MIN))) {
2754 /* Advance to previous leaf node: */
2755 search_key = bpos_predecessor(iter->path->l[0].b->data->min_key);
2757 /* Start of btree: */
2758 bch2_btree_iter_set_pos(iter, POS_MIN);
2764 EBUG_ON(bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0);
2766 /* Extents can straddle iter->pos: */
2767 if (bkey_cmp(k.k->p, iter->pos) < 0)
2770 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2771 iter->pos.snapshot = iter->snapshot;
2774 bch2_path_put(trans, saved_path, iter->flags & BTREE_ITER_INTENT);
2775 iter->path->should_be_locked = true;
2777 bch2_btree_iter_verify_entry_exit(iter);
2778 bch2_btree_iter_verify(iter);
2784 * bch2_btree_iter_prev: returns first key less than iterator's current
2787 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *iter)
2789 if (!bch2_btree_iter_rewind(iter))
2790 return bkey_s_c_null;
2792 return bch2_btree_iter_peek_prev(iter);
2795 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
2797 struct btree_trans *trans = iter->trans;
2798 struct bpos search_key;
2802 bch2_btree_iter_verify(iter);
2803 bch2_btree_iter_verify_entry_exit(iter);
2804 EBUG_ON(iter->flags & BTREE_ITER_ALL_LEVELS);
2805 EBUG_ON(iter->path->level && (iter->flags & BTREE_ITER_WITH_KEY_CACHE));
2807 /* extents can't span inode numbers: */
2808 if ((iter->flags & BTREE_ITER_IS_EXTENTS) &&
2809 unlikely(iter->pos.offset == KEY_OFFSET_MAX)) {
2810 if (iter->pos.inode == KEY_INODE_MAX)
2811 return bkey_s_c_null;
2813 bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
2816 search_key = btree_iter_search_key(iter);
2817 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2818 iter->flags & BTREE_ITER_INTENT,
2819 btree_iter_ip_allocated(iter));
2821 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2823 return bkey_s_c_err(ret);
2825 if ((iter->flags & BTREE_ITER_CACHED) ||
2826 !(iter->flags & (BTREE_ITER_IS_EXTENTS|BTREE_ITER_FILTER_SNAPSHOTS))) {
2827 struct bkey_i *next_update;
2829 if ((iter->flags & BTREE_ITER_WITH_UPDATES) &&
2830 (next_update = btree_trans_peek_updates(trans,
2831 iter->btree_id, search_key)) &&
2832 !bpos_cmp(next_update->k.p, iter->pos)) {
2833 iter->k = next_update->k;
2834 k = bkey_i_to_s_c(next_update);
2838 if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL) &&
2839 (next_update = bch2_btree_journal_peek_slot(trans,
2840 iter, iter->pos))) {
2841 iter->k = next_update->k;
2842 k = bkey_i_to_s_c(next_update);
2846 if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) &&
2847 (k = btree_trans_peek_key_cache(iter, iter->pos)).k) {
2853 k = bch2_btree_path_peek_slot(iter->path, &iter->k);
2857 EBUG_ON(iter->path->level);
2859 if (iter->flags & BTREE_ITER_INTENT) {
2860 struct btree_iter iter2;
2861 struct bpos end = iter->pos;
2863 if (iter->flags & BTREE_ITER_IS_EXTENTS)
2864 end.offset = U64_MAX;
2866 bch2_trans_copy_iter(&iter2, iter);
2867 k = bch2_btree_iter_peek_upto(&iter2, end);
2869 if (k.k && !bkey_err(k)) {
2873 bch2_trans_iter_exit(trans, &iter2);
2875 struct bpos pos = iter->pos;
2877 k = bch2_btree_iter_peek(iter);
2881 if (unlikely(bkey_err(k)))
2884 next = k.k ? bkey_start_pos(k.k) : POS_MAX;
2886 if (bkey_cmp(iter->pos, next) < 0) {
2887 bkey_init(&iter->k);
2888 iter->k.p = iter->pos;
2890 if (iter->flags & BTREE_ITER_IS_EXTENTS) {
2891 bch2_key_resize(&iter->k,
2892 min_t(u64, KEY_SIZE_MAX,
2893 (next.inode == iter->pos.inode
2897 EBUG_ON(!iter->k.size);
2900 k = (struct bkey_s_c) { &iter->k, NULL };
2904 iter->path->should_be_locked = true;
2906 bch2_btree_iter_verify_entry_exit(iter);
2907 bch2_btree_iter_verify(iter);
2908 ret = bch2_btree_iter_verify_ret(iter, k);
2910 return bkey_s_c_err(ret);
2915 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *iter)
2917 if (!bch2_btree_iter_advance(iter))
2918 return bkey_s_c_null;
2920 return bch2_btree_iter_peek_slot(iter);
2923 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *iter)
2925 if (!bch2_btree_iter_rewind(iter))
2926 return bkey_s_c_null;
2928 return bch2_btree_iter_peek_slot(iter);
2931 /* new transactional stuff: */
2933 static inline void btree_path_verify_sorted_ref(struct btree_trans *trans,
2934 struct btree_path *path)
2936 EBUG_ON(path->sorted_idx >= trans->nr_sorted);
2937 EBUG_ON(trans->sorted[path->sorted_idx] != path->idx);
2938 EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
2941 static inline void btree_trans_verify_sorted_refs(struct btree_trans *trans)
2943 #ifdef CONFIG_BCACHEFS_DEBUG
2946 for (i = 0; i < trans->nr_sorted; i++)
2947 btree_path_verify_sorted_ref(trans, trans->paths + trans->sorted[i]);
2951 static void btree_trans_verify_sorted(struct btree_trans *trans)
2953 #ifdef CONFIG_BCACHEFS_DEBUG
2954 struct btree_path *path, *prev = NULL;
2957 if (!bch2_debug_check_iterators)
2960 trans_for_each_path_inorder(trans, path, i) {
2961 if (prev && btree_path_cmp(prev, path) > 0) {
2962 bch2_dump_trans_paths_updates(trans);
2963 panic("trans paths out of order!\n");
2970 static inline void btree_path_swap(struct btree_trans *trans,
2971 struct btree_path *l, struct btree_path *r)
2973 swap(l->sorted_idx, r->sorted_idx);
2974 swap(trans->sorted[l->sorted_idx],
2975 trans->sorted[r->sorted_idx]);
2977 btree_path_verify_sorted_ref(trans, l);
2978 btree_path_verify_sorted_ref(trans, r);
2981 inline void bch2_btree_path_check_sort(struct btree_trans *trans, struct btree_path *path,
2984 struct btree_path *n;
2987 n = prev_btree_path(trans, path);
2988 if (n && btree_path_cmp(n, path) > 0) {
2990 btree_path_swap(trans, n, path);
2991 n = prev_btree_path(trans, path);
2992 } while (n && btree_path_cmp(n, path) > 0);
2999 n = next_btree_path(trans, path);
3000 if (n && btree_path_cmp(path, n) > 0) {
3002 btree_path_swap(trans, path, n);
3003 n = next_btree_path(trans, path);
3004 } while (n && btree_path_cmp(path, n) > 0);
3008 btree_trans_verify_sorted(trans);
3011 static inline void btree_path_list_remove(struct btree_trans *trans,
3012 struct btree_path *path)
3016 EBUG_ON(path->sorted_idx >= trans->nr_sorted);
3018 array_remove_item(trans->sorted, trans->nr_sorted, path->sorted_idx);
3020 for (i = path->sorted_idx; i < trans->nr_sorted; i++)
3021 trans->paths[trans->sorted[i]].sorted_idx = i;
3023 path->sorted_idx = U8_MAX;
3025 btree_trans_verify_sorted_refs(trans);
3028 static inline void btree_path_list_add(struct btree_trans *trans,
3029 struct btree_path *pos,
3030 struct btree_path *path)
3034 btree_trans_verify_sorted_refs(trans);
3036 path->sorted_idx = pos ? pos->sorted_idx + 1 : 0;
3038 if (trans->in_traverse_all &&
3039 trans->traverse_all_idx != U8_MAX &&
3040 trans->traverse_all_idx >= path->sorted_idx)
3041 trans->traverse_all_idx++;
3043 array_insert_item(trans->sorted, trans->nr_sorted, path->sorted_idx, path->idx);
3045 for (i = path->sorted_idx; i < trans->nr_sorted; i++)
3046 trans->paths[trans->sorted[i]].sorted_idx = i;
3048 btree_trans_verify_sorted_refs(trans);
3051 void bch2_trans_iter_exit(struct btree_trans *trans, struct btree_iter *iter)
3054 bch2_path_put(trans, iter->path,
3055 iter->flags & BTREE_ITER_INTENT);
3056 if (iter->update_path)
3057 bch2_path_put(trans, iter->update_path,
3058 iter->flags & BTREE_ITER_INTENT);
3059 if (iter->key_cache_path)
3060 bch2_path_put(trans, iter->key_cache_path,
3061 iter->flags & BTREE_ITER_INTENT);
3063 iter->update_path = NULL;
3064 iter->key_cache_path = NULL;
3067 static void __bch2_trans_iter_init(struct btree_trans *trans,
3068 struct btree_iter *iter,
3069 unsigned btree_id, struct bpos pos,
3070 unsigned locks_want,
3075 EBUG_ON(trans->restarted);
3077 if (flags & BTREE_ITER_ALL_LEVELS)
3078 flags |= BTREE_ITER_ALL_SNAPSHOTS|__BTREE_ITER_ALL_SNAPSHOTS;
3080 if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) &&
3081 btree_node_type_is_extents(btree_id))
3082 flags |= BTREE_ITER_IS_EXTENTS;
3084 if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
3085 !btree_type_has_snapshots(btree_id))
3086 flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
3088 if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) &&
3089 btree_type_has_snapshots(btree_id))
3090 flags |= BTREE_ITER_FILTER_SNAPSHOTS;
3092 if (!test_bit(JOURNAL_REPLAY_DONE, &trans->c->journal.flags))
3093 flags |= BTREE_ITER_WITH_JOURNAL;
3095 iter->trans = trans;
3097 iter->update_path = NULL;
3098 iter->key_cache_path = NULL;
3099 iter->btree_id = btree_id;
3100 iter->min_depth = depth;
3101 iter->flags = flags;
3102 iter->snapshot = pos.snapshot;
3104 iter->k.type = KEY_TYPE_deleted;
3107 iter->journal_idx = 0;
3108 iter->journal_pos = POS_MIN;
3109 #ifdef CONFIG_BCACHEFS_DEBUG
3110 iter->ip_allocated = ip;
3113 iter->path = bch2_path_get(trans, btree_id, iter->pos,
3114 locks_want, depth, flags, ip);
3117 void bch2_trans_iter_init(struct btree_trans *trans,
3118 struct btree_iter *iter,
3119 unsigned btree_id, struct bpos pos,
3122 if (!btree_id_cached(trans->c, btree_id)) {
3123 flags &= ~BTREE_ITER_CACHED;
3124 flags &= ~BTREE_ITER_WITH_KEY_CACHE;
3125 } else if (!(flags & BTREE_ITER_CACHED))
3126 flags |= BTREE_ITER_WITH_KEY_CACHE;
3128 __bch2_trans_iter_init(trans, iter, btree_id, pos,
3129 0, 0, flags, _RET_IP_);
3132 void bch2_trans_node_iter_init(struct btree_trans *trans,
3133 struct btree_iter *iter,
3134 enum btree_id btree_id,
3136 unsigned locks_want,
3140 __bch2_trans_iter_init(trans, iter, btree_id, pos, locks_want, depth,
3141 BTREE_ITER_NOT_EXTENTS|
3142 __BTREE_ITER_ALL_SNAPSHOTS|
3143 BTREE_ITER_ALL_SNAPSHOTS|
3145 BUG_ON(iter->path->locks_want < min(locks_want, BTREE_MAX_DEPTH));
3146 BUG_ON(iter->path->level != depth);
3147 BUG_ON(iter->min_depth != depth);
3150 void bch2_trans_copy_iter(struct btree_iter *dst, struct btree_iter *src)
3154 __btree_path_get(src->path, src->flags & BTREE_ITER_INTENT);
3155 if (src->update_path)
3156 __btree_path_get(src->update_path, src->flags & BTREE_ITER_INTENT);
3157 dst->key_cache_path = NULL;
3160 void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
3162 size_t new_top = trans->mem_top + size;
3165 if (new_top > trans->mem_bytes) {
3166 size_t old_bytes = trans->mem_bytes;
3167 size_t new_bytes = roundup_pow_of_two(new_top);
3170 WARN_ON_ONCE(new_bytes > BTREE_TRANS_MEM_MAX);
3172 new_mem = krealloc(trans->mem, new_bytes, GFP_NOFS);
3173 if (!new_mem && new_bytes <= BTREE_TRANS_MEM_MAX) {
3174 new_mem = mempool_alloc(&trans->c->btree_trans_mem_pool, GFP_KERNEL);
3175 new_bytes = BTREE_TRANS_MEM_MAX;
3180 return ERR_PTR(-ENOMEM);
3182 trans->mem = new_mem;
3183 trans->mem_bytes = new_bytes;
3186 trace_trans_restart_mem_realloced(trans->fn, _RET_IP_, new_bytes);
3187 btree_trans_restart(trans);
3188 return ERR_PTR(-EINTR);
3192 p = trans->mem + trans->mem_top;
3193 trans->mem_top += size;
3199 * bch2_trans_begin() - reset a transaction after a interrupted attempt
3200 * @trans: transaction to reset
3202 * While iterating over nodes or updating nodes a attempt to lock a btree
3203 * node may return EINTR when the trylock fails. When this occurs
3204 * bch2_trans_begin() should be called and the transaction retried.
3206 void bch2_trans_begin(struct btree_trans *trans)
3208 struct btree_path *path;
3210 bch2_trans_reset_updates(trans);
3214 if (trans->fs_usage_deltas) {
3215 trans->fs_usage_deltas->used = 0;
3216 memset((void *) trans->fs_usage_deltas +
3217 offsetof(struct replicas_delta_list, memset_start), 0,
3218 (void *) &trans->fs_usage_deltas->memset_end -
3219 (void *) &trans->fs_usage_deltas->memset_start);
3222 trans_for_each_path(trans, path) {
3223 path->should_be_locked = false;
3226 * If the transaction wasn't restarted, we're presuming to be
3227 * doing something new: dont keep iterators excpt the ones that
3228 * are in use - except for the subvolumes btree:
3230 if (!trans->restarted && path->btree_id != BTREE_ID_subvolumes)
3231 path->preserve = false;
3234 * XXX: we probably shouldn't be doing this if the transaction
3235 * was restarted, but currently we still overflow transaction
3236 * iterators if we do that
3238 if (!path->ref && !path->preserve)
3239 __bch2_path_free(trans, path);
3241 path->preserve = false;
3244 bch2_trans_cond_resched(trans);
3246 if (trans->restarted)
3247 bch2_btree_path_traverse_all(trans);
3249 trans->restarted = false;
3252 static void bch2_trans_alloc_paths(struct btree_trans *trans, struct bch_fs *c)
3254 size_t paths_bytes = sizeof(struct btree_path) * BTREE_ITER_MAX;
3255 size_t updates_bytes = sizeof(struct btree_insert_entry) * BTREE_ITER_MAX;
3258 BUG_ON(trans->used_mempool);
3261 p = this_cpu_xchg(c->btree_paths_bufs->path , NULL);
3264 p = mempool_alloc(&trans->c->btree_paths_pool, GFP_NOFS);
3266 trans->paths = p; p += paths_bytes;
3267 trans->updates = p; p += updates_bytes;
3270 void __bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
3271 unsigned expected_nr_iters,
3272 size_t expected_mem_bytes,
3274 __acquires(&c->btree_trans_barrier)
3276 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
3278 memset(trans, 0, sizeof(*trans));
3282 bch2_trans_alloc_paths(trans, c);
3284 if (expected_mem_bytes) {
3285 trans->mem_bytes = roundup_pow_of_two(expected_mem_bytes);
3286 trans->mem = kmalloc(trans->mem_bytes, GFP_KERNEL|__GFP_NOFAIL);
3288 if (!unlikely(trans->mem)) {
3289 trans->mem = mempool_alloc(&c->btree_trans_mem_pool, GFP_KERNEL);
3290 trans->mem_bytes = BTREE_TRANS_MEM_MAX;
3294 trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
3296 trans->pid = current->pid;
3297 mutex_lock(&c->btree_trans_lock);
3298 list_add(&trans->list, &c->btree_trans_list);
3299 mutex_unlock(&c->btree_trans_lock);
3302 static void check_btree_paths_leaked(struct btree_trans *trans)
3304 #ifdef CONFIG_BCACHEFS_DEBUG
3305 struct bch_fs *c = trans->c;
3306 struct btree_path *path;
3308 trans_for_each_path(trans, path)
3313 bch_err(c, "btree paths leaked from %s!", trans->fn);
3314 trans_for_each_path(trans, path)
3316 printk(KERN_ERR " btree %s %pS\n",
3317 bch2_btree_ids[path->btree_id],
3318 (void *) path->ip_allocated);
3319 /* Be noisy about this: */
3320 bch2_fatal_error(c);
3324 void bch2_trans_exit(struct btree_trans *trans)
3325 __releases(&c->btree_trans_barrier)
3327 struct btree_insert_entry *i;
3328 struct bch_fs *c = trans->c;
3330 bch2_trans_unlock(trans);
3332 trans_for_each_update(trans, i)
3333 __btree_path_put(i->path, true);
3334 trans->nr_updates = 0;
3336 check_btree_paths_leaked(trans);
3338 mutex_lock(&c->btree_trans_lock);
3339 list_del(&trans->list);
3340 mutex_unlock(&c->btree_trans_lock);
3342 srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx);
3344 bch2_journal_preres_put(&c->journal, &trans->journal_preres);
3346 kfree(trans->extra_journal_entries.data);
3348 if (trans->fs_usage_deltas) {
3349 if (trans->fs_usage_deltas->size + sizeof(trans->fs_usage_deltas) ==
3350 REPLICAS_DELTA_LIST_MAX)
3351 mempool_free(trans->fs_usage_deltas,
3352 &c->replicas_delta_pool);
3354 kfree(trans->fs_usage_deltas);
3357 if (trans->mem_bytes == BTREE_TRANS_MEM_MAX)
3358 mempool_free(trans->mem, &c->btree_trans_mem_pool);
3364 * Userspace doesn't have a real percpu implementation:
3366 trans->paths = this_cpu_xchg(c->btree_paths_bufs->path, trans->paths);
3370 mempool_free(trans->paths, &c->btree_paths_pool);
3372 trans->mem = (void *) 0x1;
3373 trans->paths = (void *) 0x1;
3376 static void __maybe_unused
3377 bch2_btree_path_node_to_text(struct printbuf *out,
3378 struct btree_bkey_cached_common *_b,
3381 prt_printf(out, " l=%u %s:",
3382 _b->level, bch2_btree_ids[_b->btree_id]);
3383 bch2_bpos_to_text(out, btree_node_pos(_b, cached));
3386 static bool trans_has_locks(struct btree_trans *trans)
3388 struct btree_path *path;
3390 trans_for_each_path(trans, path)
3391 if (path->nodes_locked)
3396 void bch2_btree_trans_to_text(struct printbuf *out, struct bch_fs *c)
3398 struct btree_trans *trans;
3399 struct btree_path *path;
3401 static char lock_types[] = { 'r', 'i', 'w' };
3404 mutex_lock(&c->btree_trans_lock);
3405 list_for_each_entry(trans, &c->btree_trans_list, list) {
3406 if (!trans_has_locks(trans))
3409 prt_printf(out, "%i %s\n", trans->pid, trans->fn);
3411 trans_for_each_path(trans, path) {
3412 if (!path->nodes_locked)
3415 prt_printf(out, " path %u %c l=%u %s:",
3417 path->cached ? 'c' : 'b',
3419 bch2_btree_ids[path->btree_id]);
3420 bch2_bpos_to_text(out, path->pos);
3421 prt_printf(out, "\n");
3423 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
3424 if (btree_node_locked(path, l)) {
3425 prt_printf(out, " %s l=%u ",
3426 btree_node_intent_locked(path, l) ? "i" : "r", l);
3427 bch2_btree_path_node_to_text(out,
3428 (void *) path->l[l].b,
3430 prt_printf(out, "\n");
3435 b = READ_ONCE(trans->locking);
3437 path = &trans->paths[trans->locking_path_idx];
3438 prt_printf(out, " locking path %u %c l=%u %c %s:",
3439 trans->locking_path_idx,
3440 path->cached ? 'c' : 'b',
3441 trans->locking_level,
3442 lock_types[trans->locking_lock_type],
3443 bch2_btree_ids[trans->locking_btree_id]);
3444 bch2_bpos_to_text(out, trans->locking_pos);
3446 prt_printf(out, " node ");
3447 bch2_btree_path_node_to_text(out,
3448 (void *) b, path->cached);
3449 prt_printf(out, "\n");
3452 mutex_unlock(&c->btree_trans_lock);
3455 void bch2_fs_btree_iter_exit(struct bch_fs *c)
3457 if (c->btree_trans_barrier_initialized)
3458 cleanup_srcu_struct(&c->btree_trans_barrier);
3459 mempool_exit(&c->btree_trans_mem_pool);
3460 mempool_exit(&c->btree_paths_pool);
3463 int bch2_fs_btree_iter_init(struct bch_fs *c)
3465 unsigned nr = BTREE_ITER_MAX;
3468 INIT_LIST_HEAD(&c->btree_trans_list);
3469 mutex_init(&c->btree_trans_lock);
3471 ret = mempool_init_kmalloc_pool(&c->btree_paths_pool, 1,
3472 sizeof(struct btree_path) * nr +
3473 sizeof(struct btree_insert_entry) * nr) ?:
3474 mempool_init_kmalloc_pool(&c->btree_trans_mem_pool, 1,
3475 BTREE_TRANS_MEM_MAX) ?:
3476 init_srcu_struct(&c->btree_trans_barrier);
3478 c->btree_trans_barrier_initialized = true;