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 static void 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(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(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 trans_for_each_path(trans, linked)
490 if (linked != path &&
491 linked->cached == path->cached &&
492 linked->btree_id == path->btree_id &&
493 linked->locks_want < new_locks_want) {
494 linked->locks_want = new_locks_want;
495 btree_path_get_locks(trans, linked, true);
501 void __bch2_btree_path_downgrade(struct btree_path *path,
502 unsigned new_locks_want)
506 EBUG_ON(path->locks_want < new_locks_want);
508 path->locks_want = new_locks_want;
510 while (path->nodes_locked &&
511 (l = __fls(path->nodes_locked)) >= path->locks_want) {
512 if (l > path->level) {
513 btree_node_unlock(path, l);
515 if (btree_node_intent_locked(path, l)) {
516 six_lock_downgrade(&path->l[l].b->c.lock);
517 path->nodes_intent_locked ^= 1 << l;
523 bch2_btree_path_verify_locks(path);
526 void bch2_trans_downgrade(struct btree_trans *trans)
528 struct btree_path *path;
530 trans_for_each_path(trans, path)
531 bch2_btree_path_downgrade(path);
534 /* Btree transaction locking: */
536 bool bch2_trans_relock(struct btree_trans *trans)
538 struct btree_path *path;
540 if (unlikely(trans->restarted))
543 trans_for_each_path(trans, path)
544 if (path->should_be_locked &&
545 !bch2_btree_path_relock(trans, path, _RET_IP_)) {
546 trace_trans_restart_relock(trans->fn, _RET_IP_,
547 path->btree_id, &path->pos);
548 BUG_ON(!trans->restarted);
554 void bch2_trans_unlock(struct btree_trans *trans)
556 struct btree_path *path;
558 trans_for_each_path(trans, path)
559 __bch2_btree_path_unlock(path);
561 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
564 /* Btree iterator: */
566 #ifdef CONFIG_BCACHEFS_DEBUG
568 static void bch2_btree_path_verify_cached(struct btree_trans *trans,
569 struct btree_path *path)
571 struct bkey_cached *ck;
572 bool locked = btree_node_locked(path, 0);
574 if (!bch2_btree_node_relock(trans, path, 0))
577 ck = (void *) path->l[0].b;
578 BUG_ON(ck->key.btree_id != path->btree_id ||
579 bkey_cmp(ck->key.pos, path->pos));
582 btree_node_unlock(path, 0);
585 static void bch2_btree_path_verify_level(struct btree_trans *trans,
586 struct btree_path *path, unsigned level)
588 struct btree_path_level *l;
589 struct btree_node_iter tmp;
591 struct bkey_packed *p, *k;
592 char buf1[100], buf2[100], buf3[100];
595 if (!bch2_debug_check_iterators)
600 locked = btree_node_locked(path, level);
604 bch2_btree_path_verify_cached(trans, path);
608 if (!btree_path_node(path, level))
611 if (!bch2_btree_node_relock(trans, path, level))
614 BUG_ON(!btree_path_pos_in_node(path, l->b));
616 bch2_btree_node_iter_verify(&l->iter, l->b);
619 * For interior nodes, the iterator will have skipped past deleted keys:
622 ? bch2_btree_node_iter_prev(&tmp, l->b)
623 : bch2_btree_node_iter_prev_all(&tmp, l->b);
624 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
626 if (p && bkey_iter_pos_cmp(l->b, p, &path->pos) >= 0) {
631 if (k && bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
637 btree_node_unlock(path, level);
640 strcpy(buf2, "(none)");
641 strcpy(buf3, "(none)");
643 bch2_bpos_to_text(&PBUF(buf1), path->pos);
646 struct bkey uk = bkey_unpack_key(l->b, p);
647 bch2_bkey_to_text(&PBUF(buf2), &uk);
651 struct bkey uk = bkey_unpack_key(l->b, k);
652 bch2_bkey_to_text(&PBUF(buf3), &uk);
655 panic("path should be %s key at level %u:\n"
659 msg, level, buf1, buf2, buf3);
662 static void bch2_btree_path_verify(struct btree_trans *trans,
663 struct btree_path *path)
665 struct bch_fs *c = trans->c;
668 EBUG_ON(path->btree_id >= BTREE_ID_NR);
670 for (i = 0; i < (!path->cached ? BTREE_MAX_DEPTH : 1); i++) {
672 BUG_ON(!path->cached &&
673 c->btree_roots[path->btree_id].b->c.level > i);
677 bch2_btree_path_verify_level(trans, path, i);
680 bch2_btree_path_verify_locks(path);
683 void bch2_trans_verify_paths(struct btree_trans *trans)
685 struct btree_path *path;
687 trans_for_each_path(trans, path)
688 bch2_btree_path_verify(trans, path);
691 static void bch2_btree_iter_verify(struct btree_iter *iter)
693 struct btree_trans *trans = iter->trans;
695 BUG_ON(iter->btree_id >= BTREE_ID_NR);
697 BUG_ON(!!(iter->flags & BTREE_ITER_CACHED) != iter->path->cached);
699 BUG_ON((iter->flags & BTREE_ITER_IS_EXTENTS) &&
700 (iter->flags & BTREE_ITER_ALL_SNAPSHOTS));
702 BUG_ON(!(iter->flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
703 (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
704 !btree_type_has_snapshots(iter->btree_id));
706 if (iter->update_path)
707 bch2_btree_path_verify(trans, iter->update_path);
708 bch2_btree_path_verify(trans, iter->path);
711 static void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter)
713 BUG_ON((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
714 !iter->pos.snapshot);
716 BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
717 iter->pos.snapshot != iter->snapshot);
719 BUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&iter->k)) < 0 ||
720 bkey_cmp(iter->pos, iter->k.p) > 0);
723 static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k)
725 struct btree_trans *trans = iter->trans;
726 struct btree_iter copy;
727 struct bkey_s_c prev;
730 if (!bch2_debug_check_iterators)
733 if (!(iter->flags & BTREE_ITER_FILTER_SNAPSHOTS))
736 if (bkey_err(k) || !k.k)
739 BUG_ON(!bch2_snapshot_is_ancestor(trans->c,
743 bch2_trans_iter_init(trans, ©, iter->btree_id, iter->pos,
744 BTREE_ITER_NOPRESERVE|
745 BTREE_ITER_ALL_SNAPSHOTS);
746 prev = bch2_btree_iter_prev(©);
750 ret = bkey_err(prev);
754 if (!bkey_cmp(prev.k->p, k.k->p) &&
755 bch2_snapshot_is_ancestor(trans->c, iter->snapshot,
756 prev.k->p.snapshot) > 0) {
757 char buf1[100], buf2[200];
759 bch2_bkey_to_text(&PBUF(buf1), k.k);
760 bch2_bkey_to_text(&PBUF(buf2), prev.k);
762 panic("iter snap %u\n"
769 bch2_trans_iter_exit(trans, ©);
773 void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
774 struct bpos pos, bool key_cache)
776 struct btree_path *path;
780 trans_for_each_path_inorder(trans, path, idx) {
781 int cmp = cmp_int(path->btree_id, id) ?:
782 cmp_int(path->cached, key_cache);
789 if (!(path->nodes_locked & 1) ||
790 !path->should_be_locked)
794 if (bkey_cmp(pos, path->l[0].b->data->min_key) >= 0 &&
795 bkey_cmp(pos, path->l[0].b->key.k.p) <= 0)
798 if (!bkey_cmp(pos, path->pos))
803 bch2_dump_trans_paths_updates(trans);
804 panic("not locked: %s %s%s\n",
806 (bch2_bpos_to_text(&PBUF(buf), pos), buf),
807 key_cache ? " cached" : "");
812 static inline void bch2_btree_path_verify_level(struct btree_trans *trans,
813 struct btree_path *path, unsigned l) {}
814 static inline void bch2_btree_path_verify(struct btree_trans *trans,
815 struct btree_path *path) {}
816 static inline void bch2_btree_iter_verify(struct btree_iter *iter) {}
817 static inline void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter) {}
818 static inline int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k) { return 0; }
822 /* Btree path: fixups after btree updates */
824 static void btree_node_iter_set_set_pos(struct btree_node_iter *iter,
827 struct bkey_packed *k)
829 struct btree_node_iter_set *set;
831 btree_node_iter_for_each(iter, set)
832 if (set->end == t->end_offset) {
833 set->k = __btree_node_key_to_offset(b, k);
834 bch2_btree_node_iter_sort(iter, b);
838 bch2_btree_node_iter_push(iter, b, k, btree_bkey_last(b, t));
841 static void __bch2_btree_path_fix_key_modified(struct btree_path *path,
843 struct bkey_packed *where)
845 struct btree_path_level *l = &path->l[b->c.level];
847 if (where != bch2_btree_node_iter_peek_all(&l->iter, l->b))
850 if (bkey_iter_pos_cmp(l->b, where, &path->pos) < 0)
851 bch2_btree_node_iter_advance(&l->iter, l->b);
854 void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
856 struct bkey_packed *where)
858 struct btree_path *path;
860 trans_for_each_path_with_node(trans, b, path) {
861 __bch2_btree_path_fix_key_modified(path, b, where);
862 bch2_btree_path_verify_level(trans, path, b->c.level);
866 static void __bch2_btree_node_iter_fix(struct btree_path *path,
868 struct btree_node_iter *node_iter,
870 struct bkey_packed *where,
871 unsigned clobber_u64s,
874 const struct bkey_packed *end = btree_bkey_last(b, t);
875 struct btree_node_iter_set *set;
876 unsigned offset = __btree_node_key_to_offset(b, where);
877 int shift = new_u64s - clobber_u64s;
878 unsigned old_end = t->end_offset - shift;
879 unsigned orig_iter_pos = node_iter->data[0].k;
880 bool iter_current_key_modified =
881 orig_iter_pos >= offset &&
882 orig_iter_pos <= offset + clobber_u64s;
884 btree_node_iter_for_each(node_iter, set)
885 if (set->end == old_end)
888 /* didn't find the bset in the iterator - might have to readd it: */
890 bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
891 bch2_btree_node_iter_push(node_iter, b, where, end);
894 /* Iterator is after key that changed */
898 set->end = t->end_offset;
900 /* Iterator hasn't gotten to the key that changed yet: */
905 bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
907 } else if (set->k < offset + clobber_u64s) {
908 set->k = offset + new_u64s;
909 if (set->k == set->end)
910 bch2_btree_node_iter_set_drop(node_iter, set);
912 /* Iterator is after key that changed */
913 set->k = (int) set->k + shift;
917 bch2_btree_node_iter_sort(node_iter, b);
919 if (node_iter->data[0].k != orig_iter_pos)
920 iter_current_key_modified = true;
923 * When a new key is added, and the node iterator now points to that
924 * key, the iterator might have skipped past deleted keys that should
925 * come after the key the iterator now points to. We have to rewind to
926 * before those deleted keys - otherwise
927 * bch2_btree_node_iter_prev_all() breaks:
929 if (!bch2_btree_node_iter_end(node_iter) &&
930 iter_current_key_modified &&
933 struct bkey_packed *k, *k2, *p;
935 k = bch2_btree_node_iter_peek_all(node_iter, b);
937 for_each_bset(b, t) {
938 bool set_pos = false;
940 if (node_iter->data[0].end == t->end_offset)
943 k2 = bch2_btree_node_iter_bset_pos(node_iter, b, t);
945 while ((p = bch2_bkey_prev_all(b, t, k2)) &&
946 bkey_iter_cmp(b, k, p) < 0) {
952 btree_node_iter_set_set_pos(node_iter,
958 void bch2_btree_node_iter_fix(struct btree_trans *trans,
959 struct btree_path *path,
961 struct btree_node_iter *node_iter,
962 struct bkey_packed *where,
963 unsigned clobber_u64s,
966 struct bset_tree *t = bch2_bkey_to_bset(b, where);
967 struct btree_path *linked;
969 if (node_iter != &path->l[b->c.level].iter) {
970 __bch2_btree_node_iter_fix(path, b, node_iter, t,
971 where, clobber_u64s, new_u64s);
973 if (bch2_debug_check_iterators)
974 bch2_btree_node_iter_verify(node_iter, b);
977 trans_for_each_path_with_node(trans, b, linked) {
978 __bch2_btree_node_iter_fix(linked, b,
979 &linked->l[b->c.level].iter, t,
980 where, clobber_u64s, new_u64s);
981 bch2_btree_path_verify_level(trans, linked, b->c.level);
985 /* Btree path level: pointer to a particular btree node and node iter */
987 static inline struct bkey_s_c __btree_iter_unpack(struct bch_fs *c,
988 struct btree_path_level *l,
990 struct bkey_packed *k)
994 * signal to bch2_btree_iter_peek_slot() that we're currently at
997 u->type = KEY_TYPE_deleted;
998 return bkey_s_c_null;
1001 return bkey_disassemble(l->b, k, u);
1004 static inline struct bkey_s_c btree_path_level_peek_all(struct bch_fs *c,
1005 struct btree_path_level *l,
1008 return __btree_iter_unpack(c, l, u,
1009 bch2_btree_node_iter_peek_all(&l->iter, l->b));
1012 static inline struct bkey_s_c btree_path_level_peek(struct bch_fs *c,
1013 struct btree_path *path,
1014 struct btree_path_level *l,
1017 struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1018 bch2_btree_node_iter_peek(&l->iter, l->b));
1020 path->pos = k.k ? k.k->p : l->b->key.k.p;
1024 static inline struct bkey_s_c btree_path_level_prev(struct bch_fs *c,
1025 struct btree_path *path,
1026 struct btree_path_level *l,
1029 struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1030 bch2_btree_node_iter_prev(&l->iter, l->b));
1032 path->pos = k.k ? k.k->p : l->b->data->min_key;
1036 static inline bool btree_path_advance_to_pos(struct btree_path *path,
1037 struct btree_path_level *l,
1040 struct bkey_packed *k;
1041 int nr_advanced = 0;
1043 while ((k = bch2_btree_node_iter_peek_all(&l->iter, l->b)) &&
1044 bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
1045 if (max_advance > 0 && nr_advanced >= max_advance)
1048 bch2_btree_node_iter_advance(&l->iter, l->b);
1056 * Verify that iterator for parent node points to child node:
1058 static void btree_path_verify_new_node(struct btree_trans *trans,
1059 struct btree_path *path, struct btree *b)
1061 struct bch_fs *c = trans->c;
1062 struct btree_path_level *l;
1065 struct bkey_packed *k;
1067 if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
1070 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1073 plevel = b->c.level + 1;
1074 if (!btree_path_node(path, plevel))
1077 parent_locked = btree_node_locked(path, plevel);
1079 if (!bch2_btree_node_relock(trans, path, plevel))
1082 l = &path->l[plevel];
1083 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1086 bkey_cmp_left_packed(l->b, k, &b->key.k.p)) {
1091 struct bkey uk = bkey_unpack_key(b, k);
1093 bch2_dump_btree_node(c, l->b);
1094 bch2_bpos_to_text(&PBUF(buf1), path->pos);
1095 bch2_bkey_to_text(&PBUF(buf2), &uk);
1096 bch2_bpos_to_text(&PBUF(buf3), b->data->min_key);
1097 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
1098 panic("parent iter doesn't point to new node:\n"
1102 bch2_btree_ids[path->btree_id], buf1,
1107 btree_node_unlock(path, plevel);
1110 static inline void __btree_path_level_init(struct btree_path *path,
1113 struct btree_path_level *l = &path->l[level];
1115 bch2_btree_node_iter_init(&l->iter, l->b, &path->pos);
1118 * Iterators to interior nodes should always be pointed at the first non
1122 bch2_btree_node_iter_peek(&l->iter, l->b);
1125 static inline void btree_path_level_init(struct btree_trans *trans,
1126 struct btree_path *path,
1129 BUG_ON(path->cached);
1131 btree_path_verify_new_node(trans, path, b);
1133 EBUG_ON(!btree_path_pos_in_node(path, b));
1134 EBUG_ON(b->c.lock.state.seq & 1);
1136 path->l[b->c.level].lock_seq = b->c.lock.state.seq;
1137 path->l[b->c.level].b = b;
1138 __btree_path_level_init(path, b->c.level);
1141 /* Btree path: fixups after btree node updates: */
1144 * A btree node is being replaced - update the iterator to point to the new
1147 void bch2_trans_node_add(struct btree_trans *trans, struct btree *b)
1149 struct btree_path *path;
1151 trans_for_each_path(trans, path)
1152 if (!path->cached &&
1153 btree_path_pos_in_node(path, b)) {
1154 enum btree_node_locked_type t =
1155 btree_lock_want(path, b->c.level);
1157 if (path->nodes_locked &&
1158 t != BTREE_NODE_UNLOCKED) {
1159 btree_node_unlock(path, b->c.level);
1160 six_lock_increment(&b->c.lock, t);
1161 mark_btree_node_locked(path, b->c.level, t);
1164 btree_path_level_init(trans, path, b);
1169 * A btree node has been modified in such a way as to invalidate iterators - fix
1172 void bch2_trans_node_reinit_iter(struct btree_trans *trans, struct btree *b)
1174 struct btree_path *path;
1176 trans_for_each_path_with_node(trans, b, path)
1177 __btree_path_level_init(path, b->c.level);
1180 /* Btree path: traverse, set_pos: */
1182 static int lock_root_check_fn(struct six_lock *lock, void *p)
1184 struct btree *b = container_of(lock, struct btree, c.lock);
1185 struct btree **rootp = p;
1187 return b == *rootp ? 0 : -1;
1190 static inline int btree_path_lock_root(struct btree_trans *trans,
1191 struct btree_path *path,
1192 unsigned depth_want,
1193 unsigned long trace_ip)
1195 struct bch_fs *c = trans->c;
1196 struct btree *b, **rootp = &c->btree_roots[path->btree_id].b;
1197 enum six_lock_type lock_type;
1200 EBUG_ON(path->nodes_locked);
1203 b = READ_ONCE(*rootp);
1204 path->level = READ_ONCE(b->c.level);
1206 if (unlikely(path->level < depth_want)) {
1208 * the root is at a lower depth than the depth we want:
1209 * got to the end of the btree, or we're walking nodes
1210 * greater than some depth and there are no nodes >=
1213 path->level = depth_want;
1214 for (i = path->level; i < BTREE_MAX_DEPTH; i++)
1215 path->l[i].b = NULL;
1219 lock_type = __btree_lock_want(path, path->level);
1220 if (unlikely(!btree_node_lock(trans, path, b, SPOS_MAX,
1221 path->level, lock_type,
1222 lock_root_check_fn, rootp,
1224 if (trans->restarted)
1229 if (likely(b == READ_ONCE(*rootp) &&
1230 b->c.level == path->level &&
1232 for (i = 0; i < path->level; i++)
1233 path->l[i].b = BTREE_ITER_NO_NODE_LOCK_ROOT;
1234 path->l[path->level].b = b;
1235 for (i = path->level + 1; i < BTREE_MAX_DEPTH; i++)
1236 path->l[i].b = NULL;
1238 mark_btree_node_locked(path, path->level, lock_type);
1239 btree_path_level_init(trans, path, b);
1243 six_unlock_type(&b->c.lock, lock_type);
1248 static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *path)
1250 struct bch_fs *c = trans->c;
1251 struct btree_path_level *l = path_l(path);
1252 struct btree_node_iter node_iter = l->iter;
1253 struct bkey_packed *k;
1254 struct bkey_buf tmp;
1255 unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
1256 ? (path->level > 1 ? 0 : 2)
1257 : (path->level > 1 ? 1 : 16);
1258 bool was_locked = btree_node_locked(path, path->level);
1261 bch2_bkey_buf_init(&tmp);
1263 while (nr && !ret) {
1264 if (!bch2_btree_node_relock(trans, path, path->level))
1267 bch2_btree_node_iter_advance(&node_iter, l->b);
1268 k = bch2_btree_node_iter_peek(&node_iter, l->b);
1272 bch2_bkey_buf_unpack(&tmp, c, l->b, k);
1273 ret = bch2_btree_node_prefetch(c, trans, path, tmp.k, path->btree_id,
1278 btree_node_unlock(path, path->level);
1280 bch2_bkey_buf_exit(&tmp, c);
1284 static int btree_path_prefetch_j(struct btree_trans *trans, struct btree_path *path,
1285 struct btree_and_journal_iter *jiter)
1287 struct bch_fs *c = trans->c;
1289 struct bkey_buf tmp;
1290 unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
1291 ? (path->level > 1 ? 0 : 2)
1292 : (path->level > 1 ? 1 : 16);
1293 bool was_locked = btree_node_locked(path, path->level);
1296 bch2_bkey_buf_init(&tmp);
1298 while (nr && !ret) {
1299 if (!bch2_btree_node_relock(trans, path, path->level))
1302 bch2_btree_and_journal_iter_advance(jiter);
1303 k = bch2_btree_and_journal_iter_peek(jiter);
1307 bch2_bkey_buf_reassemble(&tmp, c, k);
1308 ret = bch2_btree_node_prefetch(c, trans, path, tmp.k, path->btree_id,
1313 btree_node_unlock(path, path->level);
1315 bch2_bkey_buf_exit(&tmp, c);
1319 static noinline void btree_node_mem_ptr_set(struct btree_trans *trans,
1320 struct btree_path *path,
1321 unsigned plevel, struct btree *b)
1323 struct btree_path_level *l = &path->l[plevel];
1324 bool locked = btree_node_locked(path, plevel);
1325 struct bkey_packed *k;
1326 struct bch_btree_ptr_v2 *bp;
1328 if (!bch2_btree_node_relock(trans, path, plevel))
1331 k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1332 BUG_ON(k->type != KEY_TYPE_btree_ptr_v2);
1334 bp = (void *) bkeyp_val(&l->b->format, k);
1335 bp->mem_ptr = (unsigned long)b;
1338 btree_node_unlock(path, plevel);
1341 static noinline int btree_node_iter_and_journal_peek(struct btree_trans *trans,
1342 struct btree_path *path,
1344 struct bkey_buf *out)
1346 struct bch_fs *c = trans->c;
1347 struct btree_path_level *l = path_l(path);
1348 struct btree_and_journal_iter jiter;
1352 __bch2_btree_and_journal_iter_init_node_iter(&jiter, c, l->b, l->iter, path->pos);
1354 k = bch2_btree_and_journal_iter_peek(&jiter);
1356 bch2_bkey_buf_reassemble(out, c, k);
1358 if (flags & BTREE_ITER_PREFETCH)
1359 ret = btree_path_prefetch_j(trans, path, &jiter);
1361 bch2_btree_and_journal_iter_exit(&jiter);
1365 static __always_inline int btree_path_down(struct btree_trans *trans,
1366 struct btree_path *path,
1368 unsigned long trace_ip)
1370 struct bch_fs *c = trans->c;
1371 struct btree_path_level *l = path_l(path);
1373 unsigned level = path->level - 1;
1374 enum six_lock_type lock_type = __btree_lock_want(path, level);
1375 bool replay_done = test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags);
1376 struct bkey_buf tmp;
1379 EBUG_ON(!btree_node_locked(path, path->level));
1381 bch2_bkey_buf_init(&tmp);
1383 if (unlikely(!replay_done)) {
1384 ret = btree_node_iter_and_journal_peek(trans, path, flags, &tmp);
1388 bch2_bkey_buf_unpack(&tmp, c, l->b,
1389 bch2_btree_node_iter_peek(&l->iter, l->b));
1391 if (flags & BTREE_ITER_PREFETCH) {
1392 ret = btree_path_prefetch(trans, path);
1398 b = bch2_btree_node_get(trans, path, tmp.k, level, lock_type, trace_ip);
1399 ret = PTR_ERR_OR_ZERO(b);
1403 mark_btree_node_locked(path, level, lock_type);
1404 btree_path_level_init(trans, path, b);
1406 if (likely(replay_done && tmp.k->k.type == KEY_TYPE_btree_ptr_v2) &&
1407 unlikely(b != btree_node_mem_ptr(tmp.k)))
1408 btree_node_mem_ptr_set(trans, path, level + 1, b);
1410 if (btree_node_read_locked(path, level + 1))
1411 btree_node_unlock(path, level + 1);
1412 path->level = level;
1414 bch2_btree_path_verify_locks(path);
1416 bch2_bkey_buf_exit(&tmp, c);
1420 static int btree_path_traverse_one(struct btree_trans *, struct btree_path *,
1421 unsigned, unsigned long);
1423 static int __btree_path_traverse_all(struct btree_trans *trans, int ret,
1424 unsigned long trace_ip)
1426 struct bch_fs *c = trans->c;
1427 struct btree_path *path;
1430 if (trans->in_traverse_all)
1433 trans->in_traverse_all = true;
1435 trans->restarted = false;
1437 trans_for_each_path(trans, path)
1438 path->should_be_locked = false;
1440 btree_trans_verify_sorted(trans);
1442 for (i = trans->nr_sorted - 2; i >= 0; --i) {
1443 struct btree_path *path1 = trans->paths + trans->sorted[i];
1444 struct btree_path *path2 = trans->paths + trans->sorted[i + 1];
1446 if (path1->btree_id == path2->btree_id &&
1447 path1->locks_want < path2->locks_want)
1448 __bch2_btree_path_upgrade(trans, path1, path2->locks_want);
1449 else if (!path1->locks_want && path2->locks_want)
1450 __bch2_btree_path_upgrade(trans, path1, 1);
1453 bch2_trans_unlock(trans);
1456 if (unlikely(ret == -ENOMEM)) {
1459 closure_init_stack(&cl);
1462 ret = bch2_btree_cache_cannibalize_lock(c, &cl);
1467 if (unlikely(ret == -EIO))
1470 BUG_ON(ret && ret != -EINTR);
1472 /* Now, redo traversals in correct order: */
1474 while (i < trans->nr_sorted) {
1475 path = trans->paths + trans->sorted[i];
1478 * Traversing a path can cause another path to be added at about
1479 * the same position:
1481 if (path->uptodate) {
1482 ret = btree_path_traverse_one(trans, path, 0, _THIS_IP_);
1491 * BTREE_ITER_NEED_RELOCK is ok here - if we called bch2_trans_unlock()
1492 * and relock(), relock() won't relock since path->should_be_locked
1493 * isn't set yet, which is all fine
1495 trans_for_each_path(trans, path)
1496 BUG_ON(path->uptodate >= BTREE_ITER_NEED_TRAVERSE);
1498 bch2_btree_cache_cannibalize_unlock(c);
1500 trans->in_traverse_all = false;
1502 trace_trans_traverse_all(trans->fn, trace_ip);
1506 static int bch2_btree_path_traverse_all(struct btree_trans *trans)
1508 return __btree_path_traverse_all(trans, 0, _RET_IP_);
1511 static inline bool btree_path_good_node(struct btree_trans *trans,
1512 struct btree_path *path,
1513 unsigned l, int check_pos)
1515 if (!is_btree_node(path, l) ||
1516 !bch2_btree_node_relock(trans, path, l))
1519 if (check_pos < 0 && btree_path_pos_before_node(path, path->l[l].b))
1521 if (check_pos > 0 && btree_path_pos_after_node(path, path->l[l].b))
1526 static inline unsigned btree_path_up_until_good_node(struct btree_trans *trans,
1527 struct btree_path *path,
1530 unsigned i, l = path->level;
1532 while (btree_path_node(path, l) &&
1533 !btree_path_good_node(trans, path, l, check_pos)) {
1534 btree_node_unlock(path, l);
1535 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1539 /* If we need intent locks, take them too: */
1541 i < path->locks_want && btree_path_node(path, i);
1543 if (!bch2_btree_node_relock(trans, path, i))
1545 btree_node_unlock(path, l);
1546 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1554 * This is the main state machine for walking down the btree - walks down to a
1557 * Returns 0 on success, -EIO on error (error reading in a btree node).
1559 * On error, caller (peek_node()/peek_key()) must return NULL; the error is
1560 * stashed in the iterator and returned from bch2_trans_exit().
1562 static int btree_path_traverse_one(struct btree_trans *trans,
1563 struct btree_path *path,
1565 unsigned long trace_ip)
1567 unsigned depth_want = path->level;
1570 if (unlikely(trans->restarted)) {
1576 * Ensure we obey path->should_be_locked: if it's set, we can't unlock
1577 * and re-traverse the path without a transaction restart:
1579 if (path->should_be_locked) {
1580 ret = bch2_btree_path_relock(trans, path, trace_ip) ? 0 : -EINTR;
1585 ret = bch2_btree_path_traverse_cached(trans, path, flags);
1589 if (unlikely(path->level >= BTREE_MAX_DEPTH))
1592 path->level = btree_path_up_until_good_node(trans, path, 0);
1595 * Note: path->nodes[path->level] may be temporarily NULL here - that
1596 * would indicate to other code that we got to the end of the btree,
1597 * here it indicates that relocking the root failed - it's critical that
1598 * btree_path_lock_root() comes next and that it can't fail
1600 while (path->level > depth_want) {
1601 ret = btree_path_node(path, path->level)
1602 ? btree_path_down(trans, path, flags, trace_ip)
1603 : btree_path_lock_root(trans, path, depth_want, trace_ip);
1604 if (unlikely(ret)) {
1607 * No nodes at this level - got to the end of
1614 __bch2_btree_path_unlock(path);
1615 path->level = depth_want;
1618 path->l[path->level].b =
1619 BTREE_ITER_NO_NODE_ERROR;
1621 path->l[path->level].b =
1622 BTREE_ITER_NO_NODE_DOWN;
1627 path->uptodate = BTREE_ITER_UPTODATE;
1629 BUG_ON((ret == -EINTR) != !!trans->restarted);
1630 bch2_btree_path_verify(trans, path);
1634 static int __btree_path_traverse_all(struct btree_trans *, int, unsigned long);
1636 int __must_check bch2_btree_path_traverse(struct btree_trans *trans,
1637 struct btree_path *path, unsigned flags)
1639 if (path->uptodate < BTREE_ITER_NEED_RELOCK)
1642 return bch2_trans_cond_resched(trans) ?:
1643 btree_path_traverse_one(trans, path, flags, _RET_IP_);
1646 static void btree_path_copy(struct btree_trans *trans, struct btree_path *dst,
1647 struct btree_path *src)
1651 memcpy(&dst->pos, &src->pos,
1652 sizeof(struct btree_path) - offsetof(struct btree_path, pos));
1654 for (i = 0; i < BTREE_MAX_DEPTH; i++)
1655 if (btree_node_locked(dst, i))
1656 six_lock_increment(&dst->l[i].b->c.lock,
1657 __btree_lock_want(dst, i));
1659 btree_path_check_sort(trans, dst, 0);
1662 static struct btree_path *btree_path_clone(struct btree_trans *trans, struct btree_path *src,
1665 struct btree_path *new = btree_path_alloc(trans, src);
1667 btree_path_copy(trans, new, src);
1668 __btree_path_get(new, intent);
1672 inline struct btree_path * __must_check
1673 bch2_btree_path_make_mut(struct btree_trans *trans,
1674 struct btree_path *path, bool intent,
1677 if (path->ref > 1 || path->preserve) {
1678 __btree_path_put(path, intent);
1679 path = btree_path_clone(trans, path, intent);
1680 path->preserve = false;
1681 #ifdef CONFIG_BCACHEFS_DEBUG
1682 path->ip_allocated = ip;
1684 btree_trans_verify_sorted(trans);
1690 struct btree_path * __must_check
1691 bch2_btree_path_set_pos(struct btree_trans *trans,
1692 struct btree_path *path, struct bpos new_pos,
1693 bool intent, unsigned long ip)
1695 int cmp = bpos_cmp(new_pos, path->pos);
1696 unsigned l = path->level;
1698 EBUG_ON(trans->restarted);
1699 EBUG_ON(!path->ref);
1704 path = bch2_btree_path_make_mut(trans, path, intent, ip);
1706 path->pos = new_pos;
1707 path->should_be_locked = false;
1709 btree_path_check_sort(trans, path, cmp);
1711 if (unlikely(path->cached)) {
1712 btree_node_unlock(path, 0);
1713 path->l[0].b = BTREE_ITER_NO_NODE_CACHED;
1714 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1718 l = btree_path_up_until_good_node(trans, path, cmp);
1720 if (btree_path_node(path, l)) {
1722 * We might have to skip over many keys, or just a few: try
1723 * advancing the node iterator, and if we have to skip over too
1724 * many keys just reinit it (or if we're rewinding, since that
1728 !btree_path_advance_to_pos(path, &path->l[l], 8))
1729 __btree_path_level_init(path, l);
1732 if (l != path->level) {
1733 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1734 __bch2_btree_path_unlock(path);
1737 bch2_btree_path_verify(trans, path);
1741 /* Btree path: main interface: */
1743 static struct btree_path *have_path_at_pos(struct btree_trans *trans, struct btree_path *path)
1745 struct btree_path *next;
1747 next = prev_btree_path(trans, path);
1748 if (next && !btree_path_cmp(next, path))
1751 next = next_btree_path(trans, path);
1752 if (next && !btree_path_cmp(next, path))
1758 static struct btree_path *have_node_at_pos(struct btree_trans *trans, struct btree_path *path)
1760 struct btree_path *next;
1762 next = prev_btree_path(trans, path);
1763 if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1766 next = next_btree_path(trans, path);
1767 if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1773 static inline void __bch2_path_free(struct btree_trans *trans, struct btree_path *path)
1775 __bch2_btree_path_unlock(path);
1776 btree_path_list_remove(trans, path);
1777 trans->paths_allocated &= ~(1ULL << path->idx);
1780 void bch2_path_put(struct btree_trans *trans, struct btree_path *path, bool intent)
1782 struct btree_path *dup;
1784 EBUG_ON(trans->paths + path->idx != path);
1785 EBUG_ON(!path->ref);
1787 if (!__btree_path_put(path, intent))
1791 * Perhaps instead we should check for duplicate paths in traverse_all:
1793 if (path->preserve &&
1794 (dup = have_path_at_pos(trans, path))) {
1795 dup->preserve = true;
1796 path->preserve = false;
1800 if (!path->preserve &&
1801 (dup = have_node_at_pos(trans, path)))
1805 if (path->should_be_locked &&
1806 !btree_node_locked(dup, path->level))
1809 dup->should_be_locked |= path->should_be_locked;
1810 __bch2_path_free(trans, path);
1814 void bch2_dump_trans_paths_updates(struct btree_trans *trans)
1816 struct btree_path *path;
1817 struct btree_insert_entry *i;
1819 char buf1[300], buf2[300];
1821 btree_trans_verify_sorted(trans);
1823 trans_for_each_path_inorder(trans, path, idx)
1824 printk(KERN_ERR "path: idx %u ref %u:%u%s%s btree %s pos %s locks %u %pS\n",
1825 path->idx, path->ref, path->intent_ref,
1826 path->should_be_locked ? " S" : "",
1827 path->preserve ? " P" : "",
1828 bch2_btree_ids[path->btree_id],
1829 (bch2_bpos_to_text(&PBUF(buf1), path->pos), buf1),
1831 #ifdef CONFIG_BCACHEFS_DEBUG
1832 (void *) path->ip_allocated
1838 trans_for_each_update(trans, i) {
1840 struct bkey_s_c old = bch2_btree_path_peek_slot(i->path, &u);
1842 printk(KERN_ERR "update: btree %s %pS\n old %s\n new %s",
1843 bch2_btree_ids[i->btree_id],
1844 (void *) i->ip_allocated,
1845 (bch2_bkey_val_to_text(&PBUF(buf1), trans->c, old), buf1),
1846 (bch2_bkey_val_to_text(&PBUF(buf2), trans->c, bkey_i_to_s_c(i->k)), buf2));
1850 static struct btree_path *btree_path_alloc(struct btree_trans *trans,
1851 struct btree_path *pos)
1853 struct btree_path *path;
1856 if (unlikely(trans->paths_allocated ==
1857 ~((~0ULL << 1) << (BTREE_ITER_MAX - 1)))) {
1858 bch2_dump_trans_paths_updates(trans);
1859 panic("trans path oveflow\n");
1862 idx = __ffs64(~trans->paths_allocated);
1863 trans->paths_allocated |= 1ULL << idx;
1865 path = &trans->paths[idx];
1869 path->intent_ref = 0;
1870 path->nodes_locked = 0;
1871 path->nodes_intent_locked = 0;
1873 btree_path_list_add(trans, pos, path);
1877 struct btree_path *bch2_path_get(struct btree_trans *trans,
1878 enum btree_id btree_id, struct bpos pos,
1879 unsigned locks_want, unsigned level,
1880 unsigned flags, unsigned long ip)
1882 struct btree_path *path, *path_pos = NULL;
1883 bool cached = flags & BTREE_ITER_CACHED;
1884 bool intent = flags & BTREE_ITER_INTENT;
1887 BUG_ON(trans->restarted);
1889 trans_for_each_path_inorder(trans, path, i) {
1890 if (__btree_path_cmp(path,
1901 path_pos->cached == cached &&
1902 path_pos->btree_id == btree_id &&
1903 path_pos->level == level) {
1904 __btree_path_get(path_pos, intent);
1905 path = bch2_btree_path_set_pos(trans, path_pos, pos, intent, ip);
1907 path = btree_path_alloc(trans, path_pos);
1910 __btree_path_get(path, intent);
1912 path->btree_id = btree_id;
1913 path->cached = cached;
1914 path->uptodate = BTREE_ITER_NEED_TRAVERSE;
1915 path->should_be_locked = false;
1916 path->level = level;
1917 path->locks_want = locks_want;
1918 path->nodes_locked = 0;
1919 path->nodes_intent_locked = 0;
1920 for (i = 0; i < ARRAY_SIZE(path->l); i++)
1921 path->l[i].b = BTREE_ITER_NO_NODE_INIT;
1922 #ifdef CONFIG_BCACHEFS_DEBUG
1923 path->ip_allocated = ip;
1925 btree_trans_verify_sorted(trans);
1928 if (!(flags & BTREE_ITER_NOPRESERVE))
1929 path->preserve = true;
1931 if (path->intent_ref)
1932 locks_want = max(locks_want, level + 1);
1935 * If the path has locks_want greater than requested, we don't downgrade
1936 * it here - on transaction restart because btree node split needs to
1937 * upgrade locks, we might be putting/getting the iterator again.
1938 * Downgrading iterators only happens via bch2_trans_downgrade(), after
1939 * a successful transaction commit.
1942 locks_want = min(locks_want, BTREE_MAX_DEPTH);
1943 if (locks_want > path->locks_want) {
1944 path->locks_want = locks_want;
1945 btree_path_get_locks(trans, path, true);
1951 inline struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *path, struct bkey *u)
1956 if (!path->cached) {
1957 struct btree_path_level *l = path_l(path);
1958 struct bkey_packed *_k;
1960 EBUG_ON(path->uptodate != BTREE_ITER_UPTODATE);
1962 _k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1963 k = _k ? bkey_disassemble(l->b, _k, u) : bkey_s_c_null;
1965 EBUG_ON(k.k && bkey_deleted(k.k) && bpos_cmp(k.k->p, path->pos) == 0);
1967 if (!k.k || bpos_cmp(path->pos, k.k->p))
1970 struct bkey_cached *ck = (void *) path->l[0].b;
1973 (path->btree_id != ck->key.btree_id ||
1974 bkey_cmp(path->pos, ck->key.pos)));
1976 /* BTREE_ITER_CACHED_NOFILL|BTREE_ITER_CACHED_NOCREATE? */
1977 if (unlikely(!ck || !ck->valid))
1978 return bkey_s_c_null;
1980 EBUG_ON(path->uptodate != BTREE_ITER_UPTODATE);
1982 k = bkey_i_to_s_c(ck->k);
1989 return (struct bkey_s_c) { u, NULL };
1992 /* Btree iterators: */
1995 __bch2_btree_iter_traverse(struct btree_iter *iter)
1997 return bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
2001 bch2_btree_iter_traverse(struct btree_iter *iter)
2005 iter->path = bch2_btree_path_set_pos(iter->trans, iter->path,
2006 btree_iter_search_key(iter),
2007 iter->flags & BTREE_ITER_INTENT,
2008 btree_iter_ip_allocated(iter));
2010 ret = bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
2014 iter->path->should_be_locked = true;
2018 /* Iterate across nodes (leaf and interior nodes) */
2020 struct btree *bch2_btree_iter_peek_node(struct btree_iter *iter)
2022 struct btree_trans *trans = iter->trans;
2023 struct btree *b = NULL;
2026 EBUG_ON(iter->path->cached);
2027 bch2_btree_iter_verify(iter);
2029 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2033 b = btree_path_node(iter->path, iter->path->level);
2037 BUG_ON(bpos_cmp(b->key.k.p, iter->pos) < 0);
2039 bkey_init(&iter->k);
2040 iter->k.p = iter->pos = b->key.k.p;
2042 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p,
2043 iter->flags & BTREE_ITER_INTENT,
2044 btree_iter_ip_allocated(iter));
2045 iter->path->should_be_locked = true;
2046 BUG_ON(iter->path->uptodate);
2048 bch2_btree_iter_verify_entry_exit(iter);
2049 bch2_btree_iter_verify(iter);
2057 struct btree *bch2_btree_iter_next_node(struct btree_iter *iter)
2059 struct btree_trans *trans = iter->trans;
2060 struct btree_path *path = iter->path;
2061 struct btree *b = NULL;
2065 BUG_ON(trans->restarted);
2066 EBUG_ON(iter->path->cached);
2067 bch2_btree_iter_verify(iter);
2069 /* already at end? */
2070 if (!btree_path_node(path, path->level))
2074 if (!btree_path_node(path, path->level + 1)) {
2075 btree_node_unlock(path, path->level);
2076 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
2081 if (!bch2_btree_node_relock(trans, path, path->level + 1)) {
2082 __bch2_btree_path_unlock(path);
2083 path->l[path->level].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2084 path->l[path->level + 1].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2085 trace_trans_restart_relock_next_node(trans->fn, _THIS_IP_,
2086 path->btree_id, &path->pos);
2087 btree_trans_restart(trans);
2092 b = btree_path_node(path, path->level + 1);
2094 if (!bpos_cmp(iter->pos, b->key.k.p)) {
2095 btree_node_unlock(path, path->level);
2096 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
2100 * Haven't gotten to the end of the parent node: go back down to
2101 * the next child node
2104 bch2_btree_path_set_pos(trans, path, bpos_successor(iter->pos),
2105 iter->flags & BTREE_ITER_INTENT,
2106 btree_iter_ip_allocated(iter));
2108 path->level = iter->min_depth;
2110 for (l = path->level + 1; l < BTREE_MAX_DEPTH; l++)
2111 if (btree_lock_want(path, l) == BTREE_NODE_UNLOCKED)
2112 btree_node_unlock(path, l);
2114 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
2115 bch2_btree_iter_verify(iter);
2117 ret = bch2_btree_path_traverse(trans, path, iter->flags);
2121 b = path->l[path->level].b;
2124 bkey_init(&iter->k);
2125 iter->k.p = iter->pos = b->key.k.p;
2127 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p,
2128 iter->flags & BTREE_ITER_INTENT,
2129 btree_iter_ip_allocated(iter));
2130 iter->path->should_be_locked = true;
2131 BUG_ON(iter->path->uptodate);
2133 bch2_btree_iter_verify_entry_exit(iter);
2134 bch2_btree_iter_verify(iter);
2142 /* Iterate across keys (in leaf nodes only) */
2144 inline bool bch2_btree_iter_advance(struct btree_iter *iter)
2146 struct bpos pos = iter->k.p;
2147 bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2148 ? bpos_cmp(pos, SPOS_MAX)
2149 : bkey_cmp(pos, SPOS_MAX)) != 0;
2151 if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2152 pos = bkey_successor(iter, pos);
2153 bch2_btree_iter_set_pos(iter, pos);
2157 inline bool bch2_btree_iter_rewind(struct btree_iter *iter)
2159 struct bpos pos = bkey_start_pos(&iter->k);
2160 bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2161 ? bpos_cmp(pos, POS_MIN)
2162 : bkey_cmp(pos, POS_MIN)) != 0;
2164 if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2165 pos = bkey_predecessor(iter, pos);
2166 bch2_btree_iter_set_pos(iter, pos);
2170 static inline struct bkey_i *btree_trans_peek_updates(struct btree_trans *trans,
2171 enum btree_id btree_id,
2174 struct btree_insert_entry *i;
2176 trans_for_each_update(trans, i)
2177 if ((cmp_int(btree_id, i->btree_id) ?:
2178 bpos_cmp(pos, i->k->k.p)) <= 0) {
2179 if (btree_id == i->btree_id)
2188 struct bkey_i *__btree_trans_peek_journal(struct btree_trans *trans,
2189 struct btree_path *path)
2191 struct journal_keys *keys = &trans->c->journal_keys;
2192 size_t idx = bch2_journal_key_search(keys, path->btree_id,
2193 path->level, path->pos);
2195 while (idx < keys->nr && keys->d[idx].overwritten)
2198 return (idx < keys->nr &&
2199 keys->d[idx].btree_id == path->btree_id &&
2200 keys->d[idx].level == path->level)
2206 struct bkey_s_c btree_trans_peek_journal(struct btree_trans *trans,
2207 struct btree_iter *iter,
2210 struct bkey_i *next_journal =
2211 __btree_trans_peek_journal(trans, iter->path);
2214 bpos_cmp(next_journal->k.p,
2215 k.k ? k.k->p : iter->path->l[0].b->key.k.p) <= 0) {
2216 iter->k = next_journal->k;
2217 k = bkey_i_to_s_c(next_journal);
2224 * Checks btree key cache for key at iter->pos and returns it if present, or
2228 struct bkey_s_c btree_trans_peek_key_cache(struct btree_iter *iter, struct bpos pos)
2230 struct btree_trans *trans = iter->trans;
2231 struct bch_fs *c = trans->c;
2235 if (!bch2_btree_key_cache_find(c, iter->btree_id, pos))
2236 return bkey_s_c_null;
2238 if (!iter->key_cache_path)
2239 iter->key_cache_path = bch2_path_get(trans, iter->btree_id, pos,
2240 iter->flags & BTREE_ITER_INTENT, 0,
2241 iter->flags|BTREE_ITER_CACHED,
2244 iter->key_cache_path = bch2_btree_path_set_pos(trans, iter->key_cache_path, pos,
2245 iter->flags & BTREE_ITER_INTENT,
2246 btree_iter_ip_allocated(iter));
2248 ret = bch2_btree_path_traverse(trans, iter->key_cache_path, iter->flags|BTREE_ITER_CACHED);
2250 return bkey_s_c_err(ret);
2252 iter->key_cache_path->should_be_locked = true;
2254 return bch2_btree_path_peek_slot(iter->key_cache_path, &u);
2257 static struct bkey_s_c __bch2_btree_iter_peek(struct btree_iter *iter, struct bpos search_key)
2259 struct btree_trans *trans = iter->trans;
2260 struct bkey_i *next_update;
2261 struct bkey_s_c k, k2;
2264 EBUG_ON(iter->path->cached || iter->path->level);
2265 bch2_btree_iter_verify(iter);
2268 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2269 iter->flags & BTREE_ITER_INTENT,
2270 btree_iter_ip_allocated(iter));
2272 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2273 if (unlikely(ret)) {
2274 /* ensure that iter->k is consistent with iter->pos: */
2275 bch2_btree_iter_set_pos(iter, iter->pos);
2276 k = bkey_s_c_err(ret);
2280 iter->path->should_be_locked = true;
2282 k = btree_path_level_peek_all(trans->c, &iter->path->l[0], &iter->k);
2284 if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) &&
2286 (k2 = btree_trans_peek_key_cache(iter, k.k->p)).k) {
2290 bch2_btree_iter_set_pos(iter, iter->pos);
2298 if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL))
2299 k = btree_trans_peek_journal(trans, iter, k);
2301 next_update = iter->flags & BTREE_ITER_WITH_UPDATES
2302 ? btree_trans_peek_updates(trans, iter->btree_id, search_key)
2305 bpos_cmp(next_update->k.p,
2306 k.k ? k.k->p : iter->path->l[0].b->key.k.p) <= 0) {
2307 iter->k = next_update->k;
2308 k = bkey_i_to_s_c(next_update);
2311 if (k.k && bkey_deleted(k.k)) {
2313 * If we've got a whiteout, and it's after the search
2314 * key, advance the search key to the whiteout instead
2315 * of just after the whiteout - it might be a btree
2316 * whiteout, with a real key at the same position, since
2317 * in the btree deleted keys sort before non deleted.
2319 search_key = bpos_cmp(search_key, k.k->p)
2321 : bpos_successor(k.k->p);
2327 } else if (likely(bpos_cmp(iter->path->l[0].b->key.k.p, SPOS_MAX))) {
2328 /* Advance to next leaf node: */
2329 search_key = bpos_successor(iter->path->l[0].b->key.k.p);
2332 bch2_btree_iter_set_pos(iter, SPOS_MAX);
2338 bch2_btree_iter_verify(iter);
2344 * bch2_btree_iter_peek: returns first key greater than or equal to iterator's
2347 struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter)
2349 struct btree_trans *trans = iter->trans;
2350 struct bpos search_key = btree_iter_search_key(iter);
2354 if (iter->update_path) {
2355 bch2_path_put(trans, iter->update_path,
2356 iter->flags & BTREE_ITER_INTENT);
2357 iter->update_path = NULL;
2360 bch2_btree_iter_verify_entry_exit(iter);
2363 k = __bch2_btree_iter_peek(iter, search_key);
2364 if (!k.k || bkey_err(k))
2367 if (iter->update_path &&
2368 bkey_cmp(iter->update_path->pos, k.k->p)) {
2369 bch2_path_put(trans, iter->update_path,
2370 iter->flags & BTREE_ITER_INTENT);
2371 iter->update_path = NULL;
2374 if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
2375 (iter->flags & BTREE_ITER_INTENT) &&
2376 !(iter->flags & BTREE_ITER_IS_EXTENTS) &&
2377 !iter->update_path) {
2378 struct bpos pos = k.k->p;
2380 if (pos.snapshot < iter->snapshot) {
2381 search_key = bpos_successor(k.k->p);
2385 pos.snapshot = iter->snapshot;
2388 * advance, same as on exit for iter->path, but only up
2391 __btree_path_get(iter->path, iter->flags & BTREE_ITER_INTENT);
2392 iter->update_path = iter->path;
2394 iter->update_path = bch2_btree_path_set_pos(trans,
2395 iter->update_path, pos,
2396 iter->flags & BTREE_ITER_INTENT,
2397 btree_iter_ip_allocated(iter));
2399 BUG_ON(!(iter->update_path->nodes_locked & 1));
2400 iter->update_path->should_be_locked = true;
2404 * We can never have a key in a leaf node at POS_MAX, so
2405 * we don't have to check these successor() calls:
2407 if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
2408 !bch2_snapshot_is_ancestor(trans->c,
2411 search_key = bpos_successor(k.k->p);
2415 if (bkey_whiteout(k.k) &&
2416 !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2417 search_key = bkey_successor(iter, k.k->p);
2425 * iter->pos should be mononotically increasing, and always be equal to
2426 * the key we just returned - except extents can straddle iter->pos:
2428 if (!(iter->flags & BTREE_ITER_IS_EXTENTS))
2430 else if (bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0)
2431 iter->pos = bkey_start_pos(k.k);
2433 iter->path = bch2_btree_path_set_pos(trans, iter->path, k.k->p,
2434 iter->flags & BTREE_ITER_INTENT,
2435 btree_iter_ip_allocated(iter));
2436 BUG_ON(!iter->path->nodes_locked);
2438 if (iter->update_path) {
2439 BUG_ON(!(iter->update_path->nodes_locked & 1));
2440 iter->update_path->should_be_locked = true;
2442 iter->path->should_be_locked = true;
2444 if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
2445 iter->pos.snapshot = iter->snapshot;
2447 ret = bch2_btree_iter_verify_ret(iter, k);
2448 if (unlikely(ret)) {
2449 bch2_btree_iter_set_pos(iter, iter->pos);
2450 k = bkey_s_c_err(ret);
2453 bch2_btree_iter_verify_entry_exit(iter);
2459 * bch2_btree_iter_next: returns first key greater than iterator's current
2462 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *iter)
2464 if (!bch2_btree_iter_advance(iter))
2465 return bkey_s_c_null;
2467 return bch2_btree_iter_peek(iter);
2471 * bch2_btree_iter_peek_prev: returns first key less than or equal to
2472 * iterator's current position
2474 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter)
2476 struct btree_trans *trans = iter->trans;
2477 struct bpos search_key = iter->pos;
2478 struct btree_path *saved_path = NULL;
2480 struct bkey saved_k;
2481 const struct bch_val *saved_v;
2484 EBUG_ON(iter->path->cached || iter->path->level);
2485 EBUG_ON(iter->flags & BTREE_ITER_WITH_UPDATES);
2487 if (iter->flags & BTREE_ITER_WITH_JOURNAL)
2488 return bkey_s_c_err(-EIO);
2490 bch2_btree_iter_verify(iter);
2491 bch2_btree_iter_verify_entry_exit(iter);
2493 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2494 search_key.snapshot = U32_MAX;
2497 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2498 iter->flags & BTREE_ITER_INTENT,
2499 btree_iter_ip_allocated(iter));
2501 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2502 if (unlikely(ret)) {
2503 /* ensure that iter->k is consistent with iter->pos: */
2504 bch2_btree_iter_set_pos(iter, iter->pos);
2505 k = bkey_s_c_err(ret);
2509 k = btree_path_level_peek(trans->c, iter->path,
2510 &iter->path->l[0], &iter->k);
2512 ((iter->flags & BTREE_ITER_IS_EXTENTS)
2513 ? bpos_cmp(bkey_start_pos(k.k), search_key) >= 0
2514 : bpos_cmp(k.k->p, search_key) > 0))
2515 k = btree_path_level_prev(trans->c, iter->path,
2516 &iter->path->l[0], &iter->k);
2518 btree_path_check_sort(trans, iter->path, 0);
2521 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) {
2522 if (k.k->p.snapshot == iter->snapshot)
2526 * If we have a saved candidate, and we're no
2527 * longer at the same _key_ (not pos), return
2530 if (saved_path && bkey_cmp(k.k->p, saved_k.p)) {
2531 bch2_path_put(trans, iter->path,
2532 iter->flags & BTREE_ITER_INTENT);
2533 iter->path = saved_path;
2540 if (bch2_snapshot_is_ancestor(iter->trans->c,
2544 bch2_path_put(trans, saved_path,
2545 iter->flags & BTREE_ITER_INTENT);
2546 saved_path = btree_path_clone(trans, iter->path,
2547 iter->flags & BTREE_ITER_INTENT);
2552 search_key = bpos_predecessor(k.k->p);
2556 if (bkey_whiteout(k.k) &&
2557 !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2558 search_key = bkey_predecessor(iter, k.k->p);
2559 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2560 search_key.snapshot = U32_MAX;
2565 } else if (likely(bpos_cmp(iter->path->l[0].b->data->min_key, POS_MIN))) {
2566 /* Advance to previous leaf node: */
2567 search_key = bpos_predecessor(iter->path->l[0].b->data->min_key);
2569 /* Start of btree: */
2570 bch2_btree_iter_set_pos(iter, POS_MIN);
2576 EBUG_ON(bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0);
2578 /* Extents can straddle iter->pos: */
2579 if (bkey_cmp(k.k->p, iter->pos) < 0)
2582 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2583 iter->pos.snapshot = iter->snapshot;
2586 bch2_path_put(trans, saved_path, iter->flags & BTREE_ITER_INTENT);
2587 iter->path->should_be_locked = true;
2589 bch2_btree_iter_verify_entry_exit(iter);
2590 bch2_btree_iter_verify(iter);
2596 * bch2_btree_iter_prev: returns first key less than iterator's current
2599 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *iter)
2601 if (!bch2_btree_iter_rewind(iter))
2602 return bkey_s_c_null;
2604 return bch2_btree_iter_peek_prev(iter);
2607 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
2609 struct btree_trans *trans = iter->trans;
2610 struct bpos search_key;
2614 EBUG_ON(iter->path->level);
2615 bch2_btree_iter_verify(iter);
2616 bch2_btree_iter_verify_entry_exit(iter);
2618 /* extents can't span inode numbers: */
2619 if ((iter->flags & BTREE_ITER_IS_EXTENTS) &&
2620 unlikely(iter->pos.offset == KEY_OFFSET_MAX)) {
2621 if (iter->pos.inode == KEY_INODE_MAX)
2622 return bkey_s_c_null;
2624 bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
2627 search_key = btree_iter_search_key(iter);
2628 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key,
2629 iter->flags & BTREE_ITER_INTENT,
2630 btree_iter_ip_allocated(iter));
2632 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2634 return bkey_s_c_err(ret);
2636 if ((iter->flags & BTREE_ITER_CACHED) ||
2637 !(iter->flags & (BTREE_ITER_IS_EXTENTS|BTREE_ITER_FILTER_SNAPSHOTS))) {
2638 struct bkey_i *next_update;
2640 if ((iter->flags & BTREE_ITER_WITH_UPDATES) &&
2641 (next_update = btree_trans_peek_updates(trans,
2642 iter->btree_id, search_key)) &&
2643 !bpos_cmp(next_update->k.p, iter->pos)) {
2644 iter->k = next_update->k;
2645 k = bkey_i_to_s_c(next_update);
2649 if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL) &&
2650 (next_update = __btree_trans_peek_journal(trans, iter->path)) &&
2651 !bpos_cmp(next_update->k.p, iter->pos)) {
2652 iter->k = next_update->k;
2653 k = bkey_i_to_s_c(next_update);
2657 if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) &&
2658 (k = btree_trans_peek_key_cache(iter, iter->pos)).k) {
2664 k = bch2_btree_path_peek_slot(iter->path, &iter->k);
2668 if (iter->flags & BTREE_ITER_INTENT) {
2669 struct btree_iter iter2;
2671 bch2_trans_copy_iter(&iter2, iter);
2672 k = bch2_btree_iter_peek(&iter2);
2674 if (k.k && !bkey_err(k)) {
2678 bch2_trans_iter_exit(trans, &iter2);
2680 struct bpos pos = iter->pos;
2682 k = bch2_btree_iter_peek(iter);
2686 if (unlikely(bkey_err(k)))
2689 next = k.k ? bkey_start_pos(k.k) : POS_MAX;
2691 if (bkey_cmp(iter->pos, next) < 0) {
2692 bkey_init(&iter->k);
2693 iter->k.p = iter->pos;
2695 if (iter->flags & BTREE_ITER_IS_EXTENTS) {
2696 bch2_key_resize(&iter->k,
2697 min_t(u64, KEY_SIZE_MAX,
2698 (next.inode == iter->pos.inode
2702 EBUG_ON(!iter->k.size);
2705 k = (struct bkey_s_c) { &iter->k, NULL };
2709 iter->path->should_be_locked = true;
2711 bch2_btree_iter_verify_entry_exit(iter);
2712 bch2_btree_iter_verify(iter);
2713 ret = bch2_btree_iter_verify_ret(iter, k);
2715 return bkey_s_c_err(ret);
2720 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *iter)
2722 if (!bch2_btree_iter_advance(iter))
2723 return bkey_s_c_null;
2725 return bch2_btree_iter_peek_slot(iter);
2728 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *iter)
2730 if (!bch2_btree_iter_rewind(iter))
2731 return bkey_s_c_null;
2733 return bch2_btree_iter_peek_slot(iter);
2736 /* new transactional stuff: */
2738 static inline void btree_path_verify_sorted_ref(struct btree_trans *trans,
2739 struct btree_path *path)
2741 EBUG_ON(path->sorted_idx >= trans->nr_sorted);
2742 EBUG_ON(trans->sorted[path->sorted_idx] != path->idx);
2743 EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
2746 static inline void btree_trans_verify_sorted_refs(struct btree_trans *trans)
2748 #ifdef CONFIG_BCACHEFS_DEBUG
2751 for (i = 0; i < trans->nr_sorted; i++)
2752 btree_path_verify_sorted_ref(trans, trans->paths + trans->sorted[i]);
2756 static void btree_trans_verify_sorted(struct btree_trans *trans)
2758 #ifdef CONFIG_BCACHEFS_DEBUG
2759 struct btree_path *path, *prev = NULL;
2762 trans_for_each_path_inorder(trans, path, i) {
2763 BUG_ON(prev && btree_path_cmp(prev, path) > 0);
2769 static inline void btree_path_swap(struct btree_trans *trans,
2770 struct btree_path *l, struct btree_path *r)
2772 swap(l->sorted_idx, r->sorted_idx);
2773 swap(trans->sorted[l->sorted_idx],
2774 trans->sorted[r->sorted_idx]);
2776 btree_path_verify_sorted_ref(trans, l);
2777 btree_path_verify_sorted_ref(trans, r);
2780 static void btree_path_check_sort(struct btree_trans *trans, struct btree_path *path,
2783 struct btree_path *n;
2786 n = prev_btree_path(trans, path);
2787 if (n && btree_path_cmp(n, path) > 0) {
2789 btree_path_swap(trans, n, path);
2790 n = prev_btree_path(trans, path);
2791 } while (n && btree_path_cmp(n, path) > 0);
2798 n = next_btree_path(trans, path);
2799 if (n && btree_path_cmp(path, n) > 0) {
2801 btree_path_swap(trans, path, n);
2802 n = next_btree_path(trans, path);
2803 } while (n && btree_path_cmp(path, n) > 0);
2807 btree_trans_verify_sorted(trans);
2810 static inline void btree_path_list_remove(struct btree_trans *trans,
2811 struct btree_path *path)
2815 EBUG_ON(path->sorted_idx >= trans->nr_sorted);
2817 array_remove_item(trans->sorted, trans->nr_sorted, path->sorted_idx);
2819 for (i = path->sorted_idx; i < trans->nr_sorted; i++)
2820 trans->paths[trans->sorted[i]].sorted_idx = i;
2822 path->sorted_idx = U8_MAX;
2824 btree_trans_verify_sorted_refs(trans);
2827 static inline void btree_path_list_add(struct btree_trans *trans,
2828 struct btree_path *pos,
2829 struct btree_path *path)
2833 btree_trans_verify_sorted_refs(trans);
2835 path->sorted_idx = pos ? pos->sorted_idx + 1 : 0;
2837 array_insert_item(trans->sorted, trans->nr_sorted, path->sorted_idx, path->idx);
2839 for (i = path->sorted_idx; i < trans->nr_sorted; i++)
2840 trans->paths[trans->sorted[i]].sorted_idx = i;
2842 btree_trans_verify_sorted_refs(trans);
2845 void bch2_trans_iter_exit(struct btree_trans *trans, struct btree_iter *iter)
2848 bch2_path_put(trans, iter->path,
2849 iter->flags & BTREE_ITER_INTENT);
2850 if (iter->update_path)
2851 bch2_path_put(trans, iter->update_path,
2852 iter->flags & BTREE_ITER_INTENT);
2853 if (iter->key_cache_path)
2854 bch2_path_put(trans, iter->key_cache_path,
2855 iter->flags & BTREE_ITER_INTENT);
2857 iter->update_path = NULL;
2858 iter->key_cache_path = NULL;
2861 static void __bch2_trans_iter_init(struct btree_trans *trans,
2862 struct btree_iter *iter,
2863 unsigned btree_id, struct bpos pos,
2864 unsigned locks_want,
2869 EBUG_ON(trans->restarted);
2871 if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) &&
2872 btree_node_type_is_extents(btree_id))
2873 flags |= BTREE_ITER_IS_EXTENTS;
2875 if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
2876 !btree_type_has_snapshots(btree_id))
2877 flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
2879 if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) &&
2880 btree_type_has_snapshots(btree_id))
2881 flags |= BTREE_ITER_FILTER_SNAPSHOTS;
2883 if (!test_bit(JOURNAL_REPLAY_DONE, &trans->c->journal.flags))
2884 flags |= BTREE_ITER_WITH_JOURNAL;
2886 if (!btree_id_cached(trans->c, btree_id)) {
2887 flags &= ~BTREE_ITER_CACHED;
2888 flags &= ~BTREE_ITER_WITH_KEY_CACHE;
2889 } else if (!(flags & BTREE_ITER_CACHED))
2890 flags |= BTREE_ITER_WITH_KEY_CACHE;
2892 iter->trans = trans;
2894 iter->update_path = NULL;
2895 iter->key_cache_path = NULL;
2896 iter->btree_id = btree_id;
2897 iter->min_depth = depth;
2898 iter->flags = flags;
2899 iter->snapshot = pos.snapshot;
2901 iter->k.type = KEY_TYPE_deleted;
2904 #ifdef CONFIG_BCACHEFS_DEBUG
2905 iter->ip_allocated = ip;
2908 iter->path = bch2_path_get(trans, btree_id, iter->pos,
2909 locks_want, depth, flags, ip);
2912 void bch2_trans_iter_init(struct btree_trans *trans,
2913 struct btree_iter *iter,
2914 unsigned btree_id, struct bpos pos,
2917 __bch2_trans_iter_init(trans, iter, btree_id, pos,
2918 0, 0, flags, _RET_IP_);
2921 void bch2_trans_node_iter_init(struct btree_trans *trans,
2922 struct btree_iter *iter,
2923 enum btree_id btree_id,
2925 unsigned locks_want,
2929 __bch2_trans_iter_init(trans, iter, btree_id, pos, locks_want, depth,
2930 BTREE_ITER_NOT_EXTENTS|
2931 __BTREE_ITER_ALL_SNAPSHOTS|
2932 BTREE_ITER_ALL_SNAPSHOTS|
2934 BUG_ON(iter->path->locks_want < min(locks_want, BTREE_MAX_DEPTH));
2935 BUG_ON(iter->path->level != depth);
2936 BUG_ON(iter->min_depth != depth);
2939 void bch2_trans_copy_iter(struct btree_iter *dst, struct btree_iter *src)
2943 __btree_path_get(src->path, src->flags & BTREE_ITER_INTENT);
2944 if (src->update_path)
2945 __btree_path_get(src->update_path, src->flags & BTREE_ITER_INTENT);
2946 dst->key_cache_path = NULL;
2949 void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
2951 size_t new_top = trans->mem_top + size;
2954 if (new_top > trans->mem_bytes) {
2955 size_t old_bytes = trans->mem_bytes;
2956 size_t new_bytes = roundup_pow_of_two(new_top);
2959 WARN_ON_ONCE(new_bytes > BTREE_TRANS_MEM_MAX);
2961 new_mem = krealloc(trans->mem, new_bytes, GFP_NOFS);
2962 if (!new_mem && new_bytes <= BTREE_TRANS_MEM_MAX) {
2963 new_mem = mempool_alloc(&trans->c->btree_trans_mem_pool, GFP_KERNEL);
2964 new_bytes = BTREE_TRANS_MEM_MAX;
2969 return ERR_PTR(-ENOMEM);
2971 trans->mem = new_mem;
2972 trans->mem_bytes = new_bytes;
2975 trace_trans_restart_mem_realloced(trans->fn, _RET_IP_, new_bytes);
2976 btree_trans_restart(trans);
2977 return ERR_PTR(-EINTR);
2981 p = trans->mem + trans->mem_top;
2982 trans->mem_top += size;
2988 * bch2_trans_begin() - reset a transaction after a interrupted attempt
2989 * @trans: transaction to reset
2991 * While iterating over nodes or updating nodes a attempt to lock a btree
2992 * node may return EINTR when the trylock fails. When this occurs
2993 * bch2_trans_begin() should be called and the transaction retried.
2995 void bch2_trans_begin(struct btree_trans *trans)
2997 struct btree_insert_entry *i;
2998 struct btree_path *path;
3000 trans_for_each_update(trans, i)
3001 __btree_path_put(i->path, true);
3003 memset(&trans->journal_res, 0, sizeof(trans->journal_res));
3004 trans->extra_journal_res = 0;
3005 trans->nr_updates = 0;
3008 trans->hooks = NULL;
3009 trans->extra_journal_entries = NULL;
3010 trans->extra_journal_entry_u64s = 0;
3012 if (trans->fs_usage_deltas) {
3013 trans->fs_usage_deltas->used = 0;
3014 memset(&trans->fs_usage_deltas->memset_start, 0,
3015 (void *) &trans->fs_usage_deltas->memset_end -
3016 (void *) &trans->fs_usage_deltas->memset_start);
3019 trans_for_each_path(trans, path) {
3020 path->should_be_locked = false;
3023 * XXX: we probably shouldn't be doing this if the transaction
3024 * was restarted, but currently we still overflow transaction
3025 * iterators if we do that
3027 if (!path->ref && !path->preserve)
3028 __bch2_path_free(trans, path);
3029 else if (!path->ref)
3030 path->preserve = false;
3033 bch2_trans_cond_resched(trans);
3035 if (trans->restarted)
3036 bch2_btree_path_traverse_all(trans);
3038 trans->restarted = false;
3041 static void bch2_trans_alloc_paths(struct btree_trans *trans, struct bch_fs *c)
3043 size_t paths_bytes = sizeof(struct btree_path) * BTREE_ITER_MAX;
3044 size_t updates_bytes = sizeof(struct btree_insert_entry) * BTREE_ITER_MAX;
3047 BUG_ON(trans->used_mempool);
3050 p = this_cpu_xchg(c->btree_paths_bufs->path , NULL);
3053 p = mempool_alloc(&trans->c->btree_paths_pool, GFP_NOFS);
3055 trans->paths = p; p += paths_bytes;
3056 trans->updates = p; p += updates_bytes;
3059 void __bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
3060 unsigned expected_nr_iters,
3061 size_t expected_mem_bytes,
3063 __acquires(&c->btree_trans_barrier)
3065 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
3067 memset(trans, 0, sizeof(*trans));
3071 bch2_trans_alloc_paths(trans, c);
3073 if (expected_mem_bytes) {
3074 trans->mem_bytes = roundup_pow_of_two(expected_mem_bytes);
3075 trans->mem = kmalloc(trans->mem_bytes, GFP_KERNEL|__GFP_NOFAIL);
3077 if (!unlikely(trans->mem)) {
3078 trans->mem = mempool_alloc(&c->btree_trans_mem_pool, GFP_KERNEL);
3079 trans->mem_bytes = BTREE_TRANS_MEM_MAX;
3083 trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
3085 trans->pid = current->pid;
3086 mutex_lock(&c->btree_trans_lock);
3087 list_add(&trans->list, &c->btree_trans_list);
3088 mutex_unlock(&c->btree_trans_lock);
3091 static void check_btree_paths_leaked(struct btree_trans *trans)
3093 #ifdef CONFIG_BCACHEFS_DEBUG
3094 struct bch_fs *c = trans->c;
3095 struct btree_path *path;
3097 trans_for_each_path(trans, path)
3102 bch_err(c, "btree paths leaked from %s!", trans->fn);
3103 trans_for_each_path(trans, path)
3105 printk(KERN_ERR " btree %s %pS\n",
3106 bch2_btree_ids[path->btree_id],
3107 (void *) path->ip_allocated);
3108 /* Be noisy about this: */
3109 bch2_fatal_error(c);
3113 void bch2_trans_exit(struct btree_trans *trans)
3114 __releases(&c->btree_trans_barrier)
3116 struct btree_insert_entry *i;
3117 struct bch_fs *c = trans->c;
3119 bch2_trans_unlock(trans);
3121 trans_for_each_update(trans, i)
3122 __btree_path_put(i->path, true);
3123 trans->nr_updates = 0;
3125 check_btree_paths_leaked(trans);
3127 mutex_lock(&c->btree_trans_lock);
3128 list_del(&trans->list);
3129 mutex_unlock(&c->btree_trans_lock);
3131 srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx);
3133 bch2_journal_preres_put(&c->journal, &trans->journal_preres);
3135 if (trans->fs_usage_deltas) {
3136 if (trans->fs_usage_deltas->size + sizeof(trans->fs_usage_deltas) ==
3137 REPLICAS_DELTA_LIST_MAX)
3138 mempool_free(trans->fs_usage_deltas,
3139 &c->replicas_delta_pool);
3141 kfree(trans->fs_usage_deltas);
3144 if (trans->mem_bytes == BTREE_TRANS_MEM_MAX)
3145 mempool_free(trans->mem, &c->btree_trans_mem_pool);
3151 * Userspace doesn't have a real percpu implementation:
3153 trans->paths = this_cpu_xchg(c->btree_paths_bufs->path, trans->paths);
3157 mempool_free(trans->paths, &c->btree_paths_pool);
3159 trans->mem = (void *) 0x1;
3160 trans->paths = (void *) 0x1;
3163 static void __maybe_unused
3164 bch2_btree_path_node_to_text(struct printbuf *out,
3165 struct btree_bkey_cached_common *_b,
3168 pr_buf(out, " l=%u %s:",
3169 _b->level, bch2_btree_ids[_b->btree_id]);
3170 bch2_bpos_to_text(out, btree_node_pos(_b, cached));
3173 static bool trans_has_locks(struct btree_trans *trans)
3175 struct btree_path *path;
3177 trans_for_each_path(trans, path)
3178 if (path->nodes_locked)
3183 void bch2_btree_trans_to_text(struct printbuf *out, struct bch_fs *c)
3185 struct btree_trans *trans;
3186 struct btree_path *path;
3188 static char lock_types[] = { 'r', 'i', 'w' };
3191 mutex_lock(&c->btree_trans_lock);
3192 list_for_each_entry(trans, &c->btree_trans_list, list) {
3193 if (!trans_has_locks(trans))
3196 pr_buf(out, "%i %s\n", trans->pid, trans->fn);
3198 trans_for_each_path(trans, path) {
3199 if (!path->nodes_locked)
3202 pr_buf(out, " path %u %c l=%u %s:",
3204 path->cached ? 'c' : 'b',
3206 bch2_btree_ids[path->btree_id]);
3207 bch2_bpos_to_text(out, path->pos);
3210 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
3211 if (btree_node_locked(path, l)) {
3212 pr_buf(out, " %s l=%u ",
3213 btree_node_intent_locked(path, l) ? "i" : "r", l);
3214 bch2_btree_path_node_to_text(out,
3215 (void *) path->l[l].b,
3222 b = READ_ONCE(trans->locking);
3224 path = &trans->paths[trans->locking_path_idx];
3225 pr_buf(out, " locking path %u %c l=%u %c %s:",
3226 trans->locking_path_idx,
3227 path->cached ? 'c' : 'b',
3228 trans->locking_level,
3229 lock_types[trans->locking_lock_type],
3230 bch2_btree_ids[trans->locking_btree_id]);
3231 bch2_bpos_to_text(out, trans->locking_pos);
3233 pr_buf(out, " node ");
3234 bch2_btree_path_node_to_text(out,
3235 (void *) b, path->cached);
3239 mutex_unlock(&c->btree_trans_lock);
3242 void bch2_fs_btree_iter_exit(struct bch_fs *c)
3244 if (c->btree_trans_barrier_initialized)
3245 cleanup_srcu_struct(&c->btree_trans_barrier);
3246 mempool_exit(&c->btree_trans_mem_pool);
3247 mempool_exit(&c->btree_paths_pool);
3250 int bch2_fs_btree_iter_init(struct bch_fs *c)
3252 unsigned nr = BTREE_ITER_MAX;
3255 INIT_LIST_HEAD(&c->btree_trans_list);
3256 mutex_init(&c->btree_trans_lock);
3258 ret = mempool_init_kmalloc_pool(&c->btree_paths_pool, 1,
3259 sizeof(struct btree_path) * nr +
3260 sizeof(struct btree_insert_entry) * nr) ?:
3261 mempool_init_kmalloc_pool(&c->btree_trans_mem_pool, 1,
3262 BTREE_TRANS_MEM_MAX) ?:
3263 init_srcu_struct(&c->btree_trans_barrier);
3265 c->btree_trans_barrier_initialized = true;