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_journal_iter.h"
9 #include "btree_key_cache.h"
10 #include "btree_locking.h"
11 #include "btree_update.h"
16 #include "journal_io.h"
21 #include <linux/random.h>
22 #include <linux/prefetch.h>
24 static inline void btree_path_list_remove(struct btree_trans
*, struct btree_path
*);
25 static inline void btree_path_list_add(struct btree_trans
*,
26 btree_path_idx_t
, btree_path_idx_t
);
28 static inline unsigned long btree_iter_ip_allocated(struct btree_iter
*iter
)
30 #ifdef TRACK_PATH_ALLOCATED
31 return iter
->ip_allocated
;
37 static btree_path_idx_t
btree_path_alloc(struct btree_trans
*, btree_path_idx_t
);
38 static void bch2_trans_srcu_lock(struct btree_trans
*);
40 static inline int __btree_path_cmp(const struct btree_path
*l
,
41 enum btree_id r_btree_id
,
47 * Must match lock ordering as defined by __bch2_btree_node_lock:
49 return cmp_int(l
->btree_id
, r_btree_id
) ?:
50 cmp_int((int) l
->cached
, (int) r_cached
) ?:
51 bpos_cmp(l
->pos
, r_pos
) ?:
52 -cmp_int(l
->level
, r_level
);
55 static inline int btree_path_cmp(const struct btree_path
*l
,
56 const struct btree_path
*r
)
58 return __btree_path_cmp(l
, r
->btree_id
, r
->cached
, r
->pos
, r
->level
);
61 static inline struct bpos
bkey_successor(struct btree_iter
*iter
, struct bpos p
)
63 /* Are we iterating over keys in all snapshots? */
64 if (iter
->flags
& BTREE_ITER_all_snapshots
) {
65 p
= bpos_successor(p
);
67 p
= bpos_nosnap_successor(p
);
68 p
.snapshot
= iter
->snapshot
;
74 static inline struct bpos
bkey_predecessor(struct btree_iter
*iter
, struct bpos p
)
76 /* Are we iterating over keys in all snapshots? */
77 if (iter
->flags
& BTREE_ITER_all_snapshots
) {
78 p
= bpos_predecessor(p
);
80 p
= bpos_nosnap_predecessor(p
);
81 p
.snapshot
= iter
->snapshot
;
87 static inline struct bpos
btree_iter_search_key(struct btree_iter
*iter
)
89 struct bpos pos
= iter
->pos
;
91 if ((iter
->flags
& BTREE_ITER_is_extents
) &&
92 !bkey_eq(pos
, POS_MAX
))
93 pos
= bkey_successor(iter
, pos
);
97 static inline bool btree_path_pos_before_node(struct btree_path
*path
,
100 return bpos_lt(path
->pos
, b
->data
->min_key
);
103 static inline bool btree_path_pos_after_node(struct btree_path
*path
,
106 return bpos_gt(path
->pos
, b
->key
.k
.p
);
109 static inline bool btree_path_pos_in_node(struct btree_path
*path
,
112 return path
->btree_id
== b
->c
.btree_id
&&
113 !btree_path_pos_before_node(path
, b
) &&
114 !btree_path_pos_after_node(path
, b
);
117 /* Btree iterator: */
119 #ifdef CONFIG_BCACHEFS_DEBUG
121 static void bch2_btree_path_verify_cached(struct btree_trans
*trans
,
122 struct btree_path
*path
)
124 struct bkey_cached
*ck
;
125 bool locked
= btree_node_locked(path
, 0);
127 if (!bch2_btree_node_relock(trans
, path
, 0))
130 ck
= (void *) path
->l
[0].b
;
131 BUG_ON(ck
->key
.btree_id
!= path
->btree_id
||
132 !bkey_eq(ck
->key
.pos
, path
->pos
));
135 btree_node_unlock(trans
, path
, 0);
138 static void bch2_btree_path_verify_level(struct btree_trans
*trans
,
139 struct btree_path
*path
, unsigned level
)
141 struct btree_path_level
*l
;
142 struct btree_node_iter tmp
;
144 struct bkey_packed
*p
, *k
;
145 struct printbuf buf1
= PRINTBUF
;
146 struct printbuf buf2
= PRINTBUF
;
147 struct printbuf buf3
= PRINTBUF
;
150 if (!bch2_debug_check_iterators
)
155 locked
= btree_node_locked(path
, level
);
159 bch2_btree_path_verify_cached(trans
, path
);
163 if (!btree_path_node(path
, level
))
166 if (!bch2_btree_node_relock_notrace(trans
, path
, level
))
169 BUG_ON(!btree_path_pos_in_node(path
, l
->b
));
171 bch2_btree_node_iter_verify(&l
->iter
, l
->b
);
174 * For interior nodes, the iterator will have skipped past deleted keys:
177 ? bch2_btree_node_iter_prev(&tmp
, l
->b
)
178 : bch2_btree_node_iter_prev_all(&tmp
, l
->b
);
179 k
= bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
);
181 if (p
&& bkey_iter_pos_cmp(l
->b
, p
, &path
->pos
) >= 0) {
186 if (k
&& bkey_iter_pos_cmp(l
->b
, k
, &path
->pos
) < 0) {
192 btree_node_unlock(trans
, path
, level
);
195 bch2_bpos_to_text(&buf1
, path
->pos
);
198 struct bkey uk
= bkey_unpack_key(l
->b
, p
);
200 bch2_bkey_to_text(&buf2
, &uk
);
202 prt_printf(&buf2
, "(none)");
206 struct bkey uk
= bkey_unpack_key(l
->b
, k
);
208 bch2_bkey_to_text(&buf3
, &uk
);
210 prt_printf(&buf3
, "(none)");
213 panic("path should be %s key at level %u:\n"
217 msg
, level
, buf1
.buf
, buf2
.buf
, buf3
.buf
);
220 static void bch2_btree_path_verify(struct btree_trans
*trans
,
221 struct btree_path
*path
)
223 struct bch_fs
*c
= trans
->c
;
225 for (unsigned i
= 0; i
< (!path
->cached
? BTREE_MAX_DEPTH
: 1); i
++) {
227 BUG_ON(!path
->cached
&&
228 bch2_btree_id_root(c
, path
->btree_id
)->b
->c
.level
> i
);
232 bch2_btree_path_verify_level(trans
, path
, i
);
235 bch2_btree_path_verify_locks(path
);
238 void bch2_trans_verify_paths(struct btree_trans
*trans
)
240 struct btree_path
*path
;
243 trans_for_each_path(trans
, path
, iter
)
244 bch2_btree_path_verify(trans
, path
);
247 static void bch2_btree_iter_verify(struct btree_iter
*iter
)
249 struct btree_trans
*trans
= iter
->trans
;
251 BUG_ON(!!(iter
->flags
& BTREE_ITER_cached
) != btree_iter_path(trans
, iter
)->cached
);
253 BUG_ON((iter
->flags
& BTREE_ITER_is_extents
) &&
254 (iter
->flags
& BTREE_ITER_all_snapshots
));
256 BUG_ON(!(iter
->flags
& BTREE_ITER_snapshot_field
) &&
257 (iter
->flags
& BTREE_ITER_all_snapshots
) &&
258 !btree_type_has_snapshot_field(iter
->btree_id
));
260 if (iter
->update_path
)
261 bch2_btree_path_verify(trans
, &trans
->paths
[iter
->update_path
]);
262 bch2_btree_path_verify(trans
, btree_iter_path(trans
, iter
));
265 static void bch2_btree_iter_verify_entry_exit(struct btree_iter
*iter
)
267 BUG_ON((iter
->flags
& BTREE_ITER_filter_snapshots
) &&
268 !iter
->pos
.snapshot
);
270 BUG_ON(!(iter
->flags
& BTREE_ITER_all_snapshots
) &&
271 iter
->pos
.snapshot
!= iter
->snapshot
);
273 BUG_ON(iter
->flags
& BTREE_ITER_all_snapshots
? !bpos_eq(iter
->pos
, iter
->k
.p
) :
274 !(iter
->flags
& BTREE_ITER_is_extents
) ? !bkey_eq(iter
->pos
, iter
->k
.p
) :
275 (bkey_lt(iter
->pos
, bkey_start_pos(&iter
->k
)) ||
276 bkey_gt(iter
->pos
, iter
->k
.p
)));
279 static int bch2_btree_iter_verify_ret(struct btree_iter
*iter
, struct bkey_s_c k
)
281 struct btree_trans
*trans
= iter
->trans
;
282 struct btree_iter copy
;
283 struct bkey_s_c prev
;
286 if (!bch2_debug_check_iterators
)
289 if (!(iter
->flags
& BTREE_ITER_filter_snapshots
))
292 if (bkey_err(k
) || !k
.k
)
295 BUG_ON(!bch2_snapshot_is_ancestor(trans
->c
,
299 bch2_trans_iter_init(trans
, ©
, iter
->btree_id
, iter
->pos
,
300 BTREE_ITER_nopreserve
|
301 BTREE_ITER_all_snapshots
);
302 prev
= bch2_btree_iter_prev(©
);
306 ret
= bkey_err(prev
);
310 if (bkey_eq(prev
.k
->p
, k
.k
->p
) &&
311 bch2_snapshot_is_ancestor(trans
->c
, iter
->snapshot
,
312 prev
.k
->p
.snapshot
) > 0) {
313 struct printbuf buf1
= PRINTBUF
, buf2
= PRINTBUF
;
315 bch2_bkey_to_text(&buf1
, k
.k
);
316 bch2_bkey_to_text(&buf2
, prev
.k
);
318 panic("iter snap %u\n"
325 bch2_trans_iter_exit(trans
, ©
);
329 void bch2_assert_pos_locked(struct btree_trans
*trans
, enum btree_id id
,
332 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
334 struct btree_path
*path
;
335 struct trans_for_each_path_inorder_iter iter
;
336 struct printbuf buf
= PRINTBUF
;
338 btree_trans_sort_paths(trans
);
340 trans_for_each_path_inorder(trans
, path
, iter
) {
341 if (path
->btree_id
!= id
||
342 !btree_node_locked(path
, 0) ||
343 !path
->should_be_locked
)
347 if (bkey_ge(pos
, path
->l
[0].b
->data
->min_key
) &&
348 bkey_le(pos
, path
->l
[0].b
->key
.k
.p
))
351 if (bkey_eq(pos
, path
->pos
))
356 bch2_dump_trans_paths_updates(trans
);
357 bch2_bpos_to_text(&buf
, pos
);
359 panic("not locked: %s %s\n", bch2_btree_id_str(id
), buf
.buf
);
364 static inline void bch2_btree_path_verify_level(struct btree_trans
*trans
,
365 struct btree_path
*path
, unsigned l
) {}
366 static inline void bch2_btree_path_verify(struct btree_trans
*trans
,
367 struct btree_path
*path
) {}
368 static inline void bch2_btree_iter_verify(struct btree_iter
*iter
) {}
369 static inline void bch2_btree_iter_verify_entry_exit(struct btree_iter
*iter
) {}
370 static inline int bch2_btree_iter_verify_ret(struct btree_iter
*iter
, struct bkey_s_c k
) { return 0; }
374 /* Btree path: fixups after btree updates */
376 static void btree_node_iter_set_set_pos(struct btree_node_iter
*iter
,
379 struct bkey_packed
*k
)
381 struct btree_node_iter_set
*set
;
383 btree_node_iter_for_each(iter
, set
)
384 if (set
->end
== t
->end_offset
) {
385 set
->k
= __btree_node_key_to_offset(b
, k
);
386 bch2_btree_node_iter_sort(iter
, b
);
390 bch2_btree_node_iter_push(iter
, b
, k
, btree_bkey_last(b
, t
));
393 static void __bch2_btree_path_fix_key_modified(struct btree_path
*path
,
395 struct bkey_packed
*where
)
397 struct btree_path_level
*l
= &path
->l
[b
->c
.level
];
399 if (where
!= bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
))
402 if (bkey_iter_pos_cmp(l
->b
, where
, &path
->pos
) < 0)
403 bch2_btree_node_iter_advance(&l
->iter
, l
->b
);
406 void bch2_btree_path_fix_key_modified(struct btree_trans
*trans
,
408 struct bkey_packed
*where
)
410 struct btree_path
*path
;
413 trans_for_each_path_with_node(trans
, b
, path
, i
) {
414 __bch2_btree_path_fix_key_modified(path
, b
, where
);
415 bch2_btree_path_verify_level(trans
, path
, b
->c
.level
);
419 static void __bch2_btree_node_iter_fix(struct btree_path
*path
,
421 struct btree_node_iter
*node_iter
,
423 struct bkey_packed
*where
,
424 unsigned clobber_u64s
,
427 const struct bkey_packed
*end
= btree_bkey_last(b
, t
);
428 struct btree_node_iter_set
*set
;
429 unsigned offset
= __btree_node_key_to_offset(b
, where
);
430 int shift
= new_u64s
- clobber_u64s
;
431 unsigned old_end
= t
->end_offset
- shift
;
432 unsigned orig_iter_pos
= node_iter
->data
[0].k
;
433 bool iter_current_key_modified
=
434 orig_iter_pos
>= offset
&&
435 orig_iter_pos
<= offset
+ clobber_u64s
;
437 btree_node_iter_for_each(node_iter
, set
)
438 if (set
->end
== old_end
)
441 /* didn't find the bset in the iterator - might have to readd it: */
443 bkey_iter_pos_cmp(b
, where
, &path
->pos
) >= 0) {
444 bch2_btree_node_iter_push(node_iter
, b
, where
, end
);
447 /* Iterator is after key that changed */
451 set
->end
= t
->end_offset
;
453 /* Iterator hasn't gotten to the key that changed yet: */
458 bkey_iter_pos_cmp(b
, where
, &path
->pos
) >= 0) {
460 } else if (set
->k
< offset
+ clobber_u64s
) {
461 set
->k
= offset
+ new_u64s
;
462 if (set
->k
== set
->end
)
463 bch2_btree_node_iter_set_drop(node_iter
, set
);
465 /* Iterator is after key that changed */
466 set
->k
= (int) set
->k
+ shift
;
470 bch2_btree_node_iter_sort(node_iter
, b
);
472 if (node_iter
->data
[0].k
!= orig_iter_pos
)
473 iter_current_key_modified
= true;
476 * When a new key is added, and the node iterator now points to that
477 * key, the iterator might have skipped past deleted keys that should
478 * come after the key the iterator now points to. We have to rewind to
479 * before those deleted keys - otherwise
480 * bch2_btree_node_iter_prev_all() breaks:
482 if (!bch2_btree_node_iter_end(node_iter
) &&
483 iter_current_key_modified
&&
485 struct bkey_packed
*k
, *k2
, *p
;
487 k
= bch2_btree_node_iter_peek_all(node_iter
, b
);
489 for_each_bset(b
, t
) {
490 bool set_pos
= false;
492 if (node_iter
->data
[0].end
== t
->end_offset
)
495 k2
= bch2_btree_node_iter_bset_pos(node_iter
, b
, t
);
497 while ((p
= bch2_bkey_prev_all(b
, t
, k2
)) &&
498 bkey_iter_cmp(b
, k
, p
) < 0) {
504 btree_node_iter_set_set_pos(node_iter
,
510 void bch2_btree_node_iter_fix(struct btree_trans
*trans
,
511 struct btree_path
*path
,
513 struct btree_node_iter
*node_iter
,
514 struct bkey_packed
*where
,
515 unsigned clobber_u64s
,
518 struct bset_tree
*t
= bch2_bkey_to_bset_inlined(b
, where
);
519 struct btree_path
*linked
;
522 if (node_iter
!= &path
->l
[b
->c
.level
].iter
) {
523 __bch2_btree_node_iter_fix(path
, b
, node_iter
, t
,
524 where
, clobber_u64s
, new_u64s
);
526 if (bch2_debug_check_iterators
)
527 bch2_btree_node_iter_verify(node_iter
, b
);
530 trans_for_each_path_with_node(trans
, b
, linked
, i
) {
531 __bch2_btree_node_iter_fix(linked
, b
,
532 &linked
->l
[b
->c
.level
].iter
, t
,
533 where
, clobber_u64s
, new_u64s
);
534 bch2_btree_path_verify_level(trans
, linked
, b
->c
.level
);
538 /* Btree path level: pointer to a particular btree node and node iter */
540 static inline struct bkey_s_c
__btree_iter_unpack(struct bch_fs
*c
,
541 struct btree_path_level
*l
,
543 struct bkey_packed
*k
)
547 * signal to bch2_btree_iter_peek_slot() that we're currently at
550 u
->type
= KEY_TYPE_deleted
;
551 return bkey_s_c_null
;
554 return bkey_disassemble(l
->b
, k
, u
);
557 static inline struct bkey_s_c
btree_path_level_peek_all(struct bch_fs
*c
,
558 struct btree_path_level
*l
,
561 return __btree_iter_unpack(c
, l
, u
,
562 bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
));
565 static inline struct bkey_s_c
btree_path_level_peek(struct btree_trans
*trans
,
566 struct btree_path
*path
,
567 struct btree_path_level
*l
,
570 struct bkey_s_c k
= __btree_iter_unpack(trans
->c
, l
, u
,
571 bch2_btree_node_iter_peek(&l
->iter
, l
->b
));
573 path
->pos
= k
.k
? k
.k
->p
: l
->b
->key
.k
.p
;
574 trans
->paths_sorted
= false;
575 bch2_btree_path_verify_level(trans
, path
, l
- path
->l
);
579 static inline struct bkey_s_c
btree_path_level_prev(struct btree_trans
*trans
,
580 struct btree_path
*path
,
581 struct btree_path_level
*l
,
584 struct bkey_s_c k
= __btree_iter_unpack(trans
->c
, l
, u
,
585 bch2_btree_node_iter_prev(&l
->iter
, l
->b
));
587 path
->pos
= k
.k
? k
.k
->p
: l
->b
->data
->min_key
;
588 trans
->paths_sorted
= false;
589 bch2_btree_path_verify_level(trans
, path
, l
- path
->l
);
593 static inline bool btree_path_advance_to_pos(struct btree_path
*path
,
594 struct btree_path_level
*l
,
597 struct bkey_packed
*k
;
600 while ((k
= bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
)) &&
601 bkey_iter_pos_cmp(l
->b
, k
, &path
->pos
) < 0) {
602 if (max_advance
> 0 && nr_advanced
>= max_advance
)
605 bch2_btree_node_iter_advance(&l
->iter
, l
->b
);
612 static inline void __btree_path_level_init(struct btree_path
*path
,
615 struct btree_path_level
*l
= &path
->l
[level
];
617 bch2_btree_node_iter_init(&l
->iter
, l
->b
, &path
->pos
);
620 * Iterators to interior nodes should always be pointed at the first non
624 bch2_btree_node_iter_peek(&l
->iter
, l
->b
);
627 void bch2_btree_path_level_init(struct btree_trans
*trans
,
628 struct btree_path
*path
,
631 BUG_ON(path
->cached
);
633 EBUG_ON(!btree_path_pos_in_node(path
, b
));
635 path
->l
[b
->c
.level
].lock_seq
= six_lock_seq(&b
->c
.lock
);
636 path
->l
[b
->c
.level
].b
= b
;
637 __btree_path_level_init(path
, b
->c
.level
);
640 /* Btree path: fixups after btree node updates: */
642 static void bch2_trans_revalidate_updates_in_node(struct btree_trans
*trans
, struct btree
*b
)
644 struct bch_fs
*c
= trans
->c
;
646 trans_for_each_update(trans
, i
)
648 i
->level
== b
->c
.level
&&
649 i
->btree_id
== b
->c
.btree_id
&&
650 bpos_cmp(i
->k
->k
.p
, b
->data
->min_key
) >= 0 &&
651 bpos_cmp(i
->k
->k
.p
, b
->data
->max_key
) <= 0) {
652 i
->old_v
= bch2_btree_path_peek_slot(trans
->paths
+ i
->path
, &i
->old_k
).v
;
654 if (unlikely(trans
->journal_replay_not_finished
)) {
656 bch2_journal_keys_peek_slot(c
, i
->btree_id
, i
->level
,
668 * A btree node is being replaced - update the iterator to point to the new
671 void bch2_trans_node_add(struct btree_trans
*trans
,
672 struct btree_path
*path
,
675 struct btree_path
*prev
;
677 BUG_ON(!btree_path_pos_in_node(path
, b
));
679 while ((prev
= prev_btree_path(trans
, path
)) &&
680 btree_path_pos_in_node(prev
, b
))
684 path
&& btree_path_pos_in_node(path
, b
);
685 path
= next_btree_path(trans
, path
))
686 if (path
->uptodate
== BTREE_ITER_UPTODATE
&& !path
->cached
) {
687 enum btree_node_locked_type t
=
688 btree_lock_want(path
, b
->c
.level
);
690 if (t
!= BTREE_NODE_UNLOCKED
) {
691 btree_node_unlock(trans
, path
, b
->c
.level
);
692 six_lock_increment(&b
->c
.lock
, (enum six_lock_type
) t
);
693 mark_btree_node_locked(trans
, path
, b
->c
.level
, t
);
696 bch2_btree_path_level_init(trans
, path
, b
);
699 bch2_trans_revalidate_updates_in_node(trans
, b
);
702 void bch2_trans_node_drop(struct btree_trans
*trans
,
705 struct btree_path
*path
;
706 unsigned i
, level
= b
->c
.level
;
708 trans_for_each_path(trans
, path
, i
)
709 if (path
->l
[level
].b
== b
) {
710 btree_node_unlock(trans
, path
, level
);
711 path
->l
[level
].b
= ERR_PTR(-BCH_ERR_no_btree_node_init
);
716 * A btree node has been modified in such a way as to invalidate iterators - fix
719 void bch2_trans_node_reinit_iter(struct btree_trans
*trans
, struct btree
*b
)
721 struct btree_path
*path
;
724 trans_for_each_path_with_node(trans
, b
, path
, i
)
725 __btree_path_level_init(path
, b
->c
.level
);
727 bch2_trans_revalidate_updates_in_node(trans
, b
);
730 /* Btree path: traverse, set_pos: */
732 static inline int btree_path_lock_root(struct btree_trans
*trans
,
733 struct btree_path
*path
,
735 unsigned long trace_ip
)
737 struct bch_fs
*c
= trans
->c
;
738 struct btree_root
*r
= bch2_btree_id_root(c
, path
->btree_id
);
739 enum six_lock_type lock_type
;
743 EBUG_ON(path
->nodes_locked
);
746 struct btree
*b
= READ_ONCE(r
->b
);
752 path
->level
= READ_ONCE(b
->c
.level
);
754 if (unlikely(path
->level
< depth_want
)) {
756 * the root is at a lower depth than the depth we want:
757 * got to the end of the btree, or we're walking nodes
758 * greater than some depth and there are no nodes >=
761 path
->level
= depth_want
;
762 for (i
= path
->level
; i
< BTREE_MAX_DEPTH
; i
++)
767 lock_type
= __btree_lock_want(path
, path
->level
);
768 ret
= btree_node_lock(trans
, path
, &b
->c
,
769 path
->level
, lock_type
, trace_ip
);
771 if (bch2_err_matches(ret
, BCH_ERR_transaction_restart
))
776 if (likely(b
== READ_ONCE(r
->b
) &&
777 b
->c
.level
== path
->level
&&
779 for (i
= 0; i
< path
->level
; i
++)
780 path
->l
[i
].b
= ERR_PTR(-BCH_ERR_no_btree_node_lock_root
);
781 path
->l
[path
->level
].b
= b
;
782 for (i
= path
->level
+ 1; i
< BTREE_MAX_DEPTH
; i
++)
785 mark_btree_node_locked(trans
, path
, path
->level
,
786 (enum btree_node_locked_type
) lock_type
);
787 bch2_btree_path_level_init(trans
, path
, b
);
791 six_unlock_type(&b
->c
.lock
, lock_type
);
796 static int btree_path_prefetch(struct btree_trans
*trans
, struct btree_path
*path
)
798 struct bch_fs
*c
= trans
->c
;
799 struct btree_path_level
*l
= path_l(path
);
800 struct btree_node_iter node_iter
= l
->iter
;
801 struct bkey_packed
*k
;
803 unsigned nr
= test_bit(BCH_FS_started
, &c
->flags
)
804 ? (path
->level
> 1 ? 0 : 2)
805 : (path
->level
> 1 ? 1 : 16);
806 bool was_locked
= btree_node_locked(path
, path
->level
);
809 bch2_bkey_buf_init(&tmp
);
811 while (nr
-- && !ret
) {
812 if (!bch2_btree_node_relock(trans
, path
, path
->level
))
815 bch2_btree_node_iter_advance(&node_iter
, l
->b
);
816 k
= bch2_btree_node_iter_peek(&node_iter
, l
->b
);
820 bch2_bkey_buf_unpack(&tmp
, c
, l
->b
, k
);
821 ret
= bch2_btree_node_prefetch(trans
, path
, tmp
.k
, path
->btree_id
,
826 btree_node_unlock(trans
, path
, path
->level
);
828 bch2_bkey_buf_exit(&tmp
, c
);
832 static int btree_path_prefetch_j(struct btree_trans
*trans
, struct btree_path
*path
,
833 struct btree_and_journal_iter
*jiter
)
835 struct bch_fs
*c
= trans
->c
;
838 unsigned nr
= test_bit(BCH_FS_started
, &c
->flags
)
839 ? (path
->level
> 1 ? 0 : 2)
840 : (path
->level
> 1 ? 1 : 16);
841 bool was_locked
= btree_node_locked(path
, path
->level
);
844 bch2_bkey_buf_init(&tmp
);
846 jiter
->fail_if_too_many_whiteouts
= true;
848 while (nr
-- && !ret
) {
849 if (!bch2_btree_node_relock(trans
, path
, path
->level
))
852 bch2_btree_and_journal_iter_advance(jiter
);
853 k
= bch2_btree_and_journal_iter_peek(jiter
);
857 bch2_bkey_buf_reassemble(&tmp
, c
, k
);
858 ret
= bch2_btree_node_prefetch(trans
, path
, tmp
.k
, path
->btree_id
,
863 btree_node_unlock(trans
, path
, path
->level
);
865 bch2_bkey_buf_exit(&tmp
, c
);
869 static noinline
void btree_node_mem_ptr_set(struct btree_trans
*trans
,
870 struct btree_path
*path
,
871 unsigned plevel
, struct btree
*b
)
873 struct btree_path_level
*l
= &path
->l
[plevel
];
874 bool locked
= btree_node_locked(path
, plevel
);
875 struct bkey_packed
*k
;
876 struct bch_btree_ptr_v2
*bp
;
878 if (!bch2_btree_node_relock(trans
, path
, plevel
))
881 k
= bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
);
882 BUG_ON(k
->type
!= KEY_TYPE_btree_ptr_v2
);
884 bp
= (void *) bkeyp_val(&l
->b
->format
, k
);
885 bp
->mem_ptr
= (unsigned long)b
;
888 btree_node_unlock(trans
, path
, plevel
);
891 static noinline
int btree_node_iter_and_journal_peek(struct btree_trans
*trans
,
892 struct btree_path
*path
,
894 struct bkey_buf
*out
)
896 struct bch_fs
*c
= trans
->c
;
897 struct btree_path_level
*l
= path_l(path
);
898 struct btree_and_journal_iter jiter
;
902 __bch2_btree_and_journal_iter_init_node_iter(trans
, &jiter
, l
->b
, l
->iter
, path
->pos
);
904 k
= bch2_btree_and_journal_iter_peek(&jiter
);
906 struct printbuf buf
= PRINTBUF
;
908 prt_str(&buf
, "node not found at pos ");
909 bch2_bpos_to_text(&buf
, path
->pos
);
910 prt_str(&buf
, " at btree ");
911 bch2_btree_pos_to_text(&buf
, c
, l
->b
);
913 ret
= bch2_fs_topology_error(c
, "%s", buf
.buf
);
918 bch2_bkey_buf_reassemble(out
, c
, k
);
920 if ((flags
& BTREE_ITER_prefetch
) &&
921 c
->opts
.btree_node_prefetch
)
922 ret
= btree_path_prefetch_j(trans
, path
, &jiter
);
925 bch2_btree_and_journal_iter_exit(&jiter
);
929 static __always_inline
int btree_path_down(struct btree_trans
*trans
,
930 struct btree_path
*path
,
932 unsigned long trace_ip
)
934 struct bch_fs
*c
= trans
->c
;
935 struct btree_path_level
*l
= path_l(path
);
937 unsigned level
= path
->level
- 1;
938 enum six_lock_type lock_type
= __btree_lock_want(path
, level
);
942 EBUG_ON(!btree_node_locked(path
, path
->level
));
944 bch2_bkey_buf_init(&tmp
);
946 if (unlikely(trans
->journal_replay_not_finished
)) {
947 ret
= btree_node_iter_and_journal_peek(trans
, path
, flags
, &tmp
);
951 struct bkey_packed
*k
= bch2_btree_node_iter_peek(&l
->iter
, l
->b
);
953 struct printbuf buf
= PRINTBUF
;
955 prt_str(&buf
, "node not found at pos ");
956 bch2_bpos_to_text(&buf
, path
->pos
);
957 prt_str(&buf
, " within parent node ");
958 bch2_bkey_val_to_text(&buf
, c
, bkey_i_to_s_c(&l
->b
->key
));
960 bch2_fs_fatal_error(c
, "%s", buf
.buf
);
962 ret
= -BCH_ERR_btree_need_topology_repair
;
966 bch2_bkey_buf_unpack(&tmp
, c
, l
->b
, k
);
968 if ((flags
& BTREE_ITER_prefetch
) &&
969 c
->opts
.btree_node_prefetch
) {
970 ret
= btree_path_prefetch(trans
, path
);
976 b
= bch2_btree_node_get(trans
, path
, tmp
.k
, level
, lock_type
, trace_ip
);
977 ret
= PTR_ERR_OR_ZERO(b
);
981 if (likely(!trans
->journal_replay_not_finished
&&
982 tmp
.k
->k
.type
== KEY_TYPE_btree_ptr_v2
) &&
983 unlikely(b
!= btree_node_mem_ptr(tmp
.k
)))
984 btree_node_mem_ptr_set(trans
, path
, level
+ 1, b
);
986 if (btree_node_read_locked(path
, level
+ 1))
987 btree_node_unlock(trans
, path
, level
+ 1);
989 mark_btree_node_locked(trans
, path
, level
,
990 (enum btree_node_locked_type
) lock_type
);
992 bch2_btree_path_level_init(trans
, path
, b
);
994 bch2_btree_path_verify_locks(path
);
996 bch2_bkey_buf_exit(&tmp
, c
);
1000 static int bch2_btree_path_traverse_all(struct btree_trans
*trans
)
1002 struct bch_fs
*c
= trans
->c
;
1003 struct btree_path
*path
;
1004 unsigned long trace_ip
= _RET_IP_
;
1008 if (trans
->in_traverse_all
)
1009 return -BCH_ERR_transaction_restart_in_traverse_all
;
1011 trans
->in_traverse_all
= true;
1013 trans
->restarted
= 0;
1014 trans
->last_restarted_ip
= 0;
1016 trans_for_each_path(trans
, path
, i
)
1017 path
->should_be_locked
= false;
1019 btree_trans_sort_paths(trans
);
1021 bch2_trans_unlock(trans
);
1023 trans_set_locked(trans
, false);
1025 if (unlikely(trans
->memory_allocation_failure
)) {
1028 closure_init_stack(&cl
);
1031 ret
= bch2_btree_cache_cannibalize_lock(trans
, &cl
);
1036 /* Now, redo traversals in correct order: */
1038 while (i
< trans
->nr_sorted
) {
1039 btree_path_idx_t idx
= trans
->sorted
[i
];
1042 * Traversing a path can cause another path to be added at about
1043 * the same position:
1045 if (trans
->paths
[idx
].uptodate
) {
1046 __btree_path_get(trans
, &trans
->paths
[idx
], false);
1047 ret
= bch2_btree_path_traverse_one(trans
, idx
, 0, _THIS_IP_
);
1048 __btree_path_put(trans
, &trans
->paths
[idx
], false);
1050 if (bch2_err_matches(ret
, BCH_ERR_transaction_restart
) ||
1051 bch2_err_matches(ret
, ENOMEM
))
1061 * We used to assert that all paths had been traversed here
1062 * (path->uptodate < BTREE_ITER_NEED_TRAVERSE); however, since
1063 * path->should_be_locked is not set yet, we might have unlocked and
1064 * then failed to relock a path - that's fine.
1067 bch2_btree_cache_cannibalize_unlock(trans
);
1069 trans
->in_traverse_all
= false;
1071 trace_and_count(c
, trans_traverse_all
, trans
, trace_ip
);
1075 static inline bool btree_path_check_pos_in_node(struct btree_path
*path
,
1076 unsigned l
, int check_pos
)
1078 if (check_pos
< 0 && btree_path_pos_before_node(path
, path
->l
[l
].b
))
1080 if (check_pos
> 0 && btree_path_pos_after_node(path
, path
->l
[l
].b
))
1085 static inline bool btree_path_good_node(struct btree_trans
*trans
,
1086 struct btree_path
*path
,
1087 unsigned l
, int check_pos
)
1089 return is_btree_node(path
, l
) &&
1090 bch2_btree_node_relock(trans
, path
, l
) &&
1091 btree_path_check_pos_in_node(path
, l
, check_pos
);
1094 static void btree_path_set_level_down(struct btree_trans
*trans
,
1095 struct btree_path
*path
,
1100 path
->level
= new_level
;
1102 for (l
= path
->level
+ 1; l
< BTREE_MAX_DEPTH
; l
++)
1103 if (btree_lock_want(path
, l
) == BTREE_NODE_UNLOCKED
)
1104 btree_node_unlock(trans
, path
, l
);
1106 btree_path_set_dirty(path
, BTREE_ITER_NEED_TRAVERSE
);
1107 bch2_btree_path_verify(trans
, path
);
1110 static noinline
unsigned __btree_path_up_until_good_node(struct btree_trans
*trans
,
1111 struct btree_path
*path
,
1114 unsigned i
, l
= path
->level
;
1116 while (btree_path_node(path
, l
) &&
1117 !btree_path_good_node(trans
, path
, l
, check_pos
))
1118 __btree_path_set_level_up(trans
, path
, l
++);
1120 /* If we need intent locks, take them too: */
1122 i
< path
->locks_want
&& btree_path_node(path
, i
);
1124 if (!bch2_btree_node_relock(trans
, path
, i
)) {
1126 __btree_path_set_level_up(trans
, path
, l
++);
1133 static inline unsigned btree_path_up_until_good_node(struct btree_trans
*trans
,
1134 struct btree_path
*path
,
1137 return likely(btree_node_locked(path
, path
->level
) &&
1138 btree_path_check_pos_in_node(path
, path
->level
, check_pos
))
1140 : __btree_path_up_until_good_node(trans
, path
, check_pos
);
1144 * This is the main state machine for walking down the btree - walks down to a
1147 * Returns 0 on success, -EIO on error (error reading in a btree node).
1149 * On error, caller (peek_node()/peek_key()) must return NULL; the error is
1150 * stashed in the iterator and returned from bch2_trans_exit().
1152 int bch2_btree_path_traverse_one(struct btree_trans
*trans
,
1153 btree_path_idx_t path_idx
,
1155 unsigned long trace_ip
)
1157 struct btree_path
*path
= &trans
->paths
[path_idx
];
1158 unsigned depth_want
= path
->level
;
1159 int ret
= -((int) trans
->restarted
);
1164 if (unlikely(!trans
->srcu_held
))
1165 bch2_trans_srcu_lock(trans
);
1167 trace_btree_path_traverse_start(trans
, path
);
1170 * Ensure we obey path->should_be_locked: if it's set, we can't unlock
1171 * and re-traverse the path without a transaction restart:
1173 if (path
->should_be_locked
) {
1174 ret
= bch2_btree_path_relock(trans
, path
, trace_ip
);
1179 ret
= bch2_btree_path_traverse_cached(trans
, path
, flags
);
1183 path
= &trans
->paths
[path_idx
];
1185 if (unlikely(path
->level
>= BTREE_MAX_DEPTH
))
1188 path
->level
= btree_path_up_until_good_node(trans
, path
, 0);
1189 unsigned max_level
= path
->level
;
1191 EBUG_ON(btree_path_node(path
, path
->level
) &&
1192 !btree_node_locked(path
, path
->level
));
1195 * Note: path->nodes[path->level] may be temporarily NULL here - that
1196 * would indicate to other code that we got to the end of the btree,
1197 * here it indicates that relocking the root failed - it's critical that
1198 * btree_path_lock_root() comes next and that it can't fail
1200 while (path
->level
> depth_want
) {
1201 ret
= btree_path_node(path
, path
->level
)
1202 ? btree_path_down(trans
, path
, flags
, trace_ip
)
1203 : btree_path_lock_root(trans
, path
, depth_want
, trace_ip
);
1204 if (unlikely(ret
)) {
1207 * No nodes at this level - got to the end of
1214 __bch2_btree_path_unlock(trans
, path
);
1215 path
->level
= depth_want
;
1216 path
->l
[path
->level
].b
= ERR_PTR(ret
);
1221 if (unlikely(max_level
> path
->level
)) {
1222 struct btree_path
*linked
;
1225 trans_for_each_path_with_node(trans
, path_l(path
)->b
, linked
, iter
)
1226 for (unsigned j
= path
->level
+ 1; j
< max_level
; j
++)
1227 linked
->l
[j
] = path
->l
[j
];
1231 path
->uptodate
= BTREE_ITER_UPTODATE
;
1232 trace_btree_path_traverse_end(trans
, path
);
1234 if (bch2_err_matches(ret
, BCH_ERR_transaction_restart
) != !!trans
->restarted
)
1235 panic("ret %s (%i) trans->restarted %s (%i)\n",
1236 bch2_err_str(ret
), ret
,
1237 bch2_err_str(trans
->restarted
), trans
->restarted
);
1238 bch2_btree_path_verify(trans
, path
);
1242 static inline void btree_path_copy(struct btree_trans
*trans
, struct btree_path
*dst
,
1243 struct btree_path
*src
)
1245 unsigned i
, offset
= offsetof(struct btree_path
, pos
);
1247 memcpy((void *) dst
+ offset
,
1248 (void *) src
+ offset
,
1249 sizeof(struct btree_path
) - offset
);
1251 for (i
= 0; i
< BTREE_MAX_DEPTH
; i
++) {
1252 unsigned t
= btree_node_locked_type(dst
, i
);
1254 if (t
!= BTREE_NODE_UNLOCKED
)
1255 six_lock_increment(&dst
->l
[i
].b
->c
.lock
, t
);
1259 static btree_path_idx_t
btree_path_clone(struct btree_trans
*trans
, btree_path_idx_t src
,
1260 bool intent
, unsigned long ip
)
1262 btree_path_idx_t
new = btree_path_alloc(trans
, src
);
1263 btree_path_copy(trans
, trans
->paths
+ new, trans
->paths
+ src
);
1264 __btree_path_get(trans
, trans
->paths
+ new, intent
);
1265 #ifdef TRACK_PATH_ALLOCATED
1266 trans
->paths
[new].ip_allocated
= ip
;
1272 btree_path_idx_t
__bch2_btree_path_make_mut(struct btree_trans
*trans
,
1273 btree_path_idx_t path
, bool intent
, unsigned long ip
)
1275 struct btree_path
*old
= trans
->paths
+ path
;
1276 __btree_path_put(trans
, trans
->paths
+ path
, intent
);
1277 path
= btree_path_clone(trans
, path
, intent
, ip
);
1278 trace_btree_path_clone(trans
, old
, trans
->paths
+ path
);
1279 trans
->paths
[path
].preserve
= false;
1283 btree_path_idx_t __must_check
1284 __bch2_btree_path_set_pos(struct btree_trans
*trans
,
1285 btree_path_idx_t path_idx
, struct bpos new_pos
,
1286 bool intent
, unsigned long ip
)
1288 int cmp
= bpos_cmp(new_pos
, trans
->paths
[path_idx
].pos
);
1290 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
1291 EBUG_ON(!trans
->paths
[path_idx
].ref
);
1293 trace_btree_path_set_pos(trans
, trans
->paths
+ path_idx
, &new_pos
);
1295 path_idx
= bch2_btree_path_make_mut(trans
, path_idx
, intent
, ip
);
1297 struct btree_path
*path
= trans
->paths
+ path_idx
;
1298 path
->pos
= new_pos
;
1299 trans
->paths_sorted
= false;
1301 if (unlikely(path
->cached
)) {
1302 btree_node_unlock(trans
, path
, 0);
1303 path
->l
[0].b
= ERR_PTR(-BCH_ERR_no_btree_node_up
);
1304 btree_path_set_dirty(path
, BTREE_ITER_NEED_TRAVERSE
);
1308 unsigned level
= btree_path_up_until_good_node(trans
, path
, cmp
);
1310 if (btree_path_node(path
, level
)) {
1311 struct btree_path_level
*l
= &path
->l
[level
];
1313 BUG_ON(!btree_node_locked(path
, level
));
1315 * We might have to skip over many keys, or just a few: try
1316 * advancing the node iterator, and if we have to skip over too
1317 * many keys just reinit it (or if we're rewinding, since that
1321 !btree_path_advance_to_pos(path
, l
, 8))
1322 bch2_btree_node_iter_init(&l
->iter
, l
->b
, &path
->pos
);
1325 * Iterators to interior nodes should always be pointed at the first non
1328 if (unlikely(level
))
1329 bch2_btree_node_iter_peek(&l
->iter
, l
->b
);
1332 if (unlikely(level
!= path
->level
)) {
1333 btree_path_set_dirty(path
, BTREE_ITER_NEED_TRAVERSE
);
1334 __bch2_btree_path_unlock(trans
, path
);
1337 bch2_btree_path_verify(trans
, path
);
1341 /* Btree path: main interface: */
1343 static struct btree_path
*have_path_at_pos(struct btree_trans
*trans
, struct btree_path
*path
)
1345 struct btree_path
*sib
;
1347 sib
= prev_btree_path(trans
, path
);
1348 if (sib
&& !btree_path_cmp(sib
, path
))
1351 sib
= next_btree_path(trans
, path
);
1352 if (sib
&& !btree_path_cmp(sib
, path
))
1358 static struct btree_path
*have_node_at_pos(struct btree_trans
*trans
, struct btree_path
*path
)
1360 struct btree_path
*sib
;
1362 sib
= prev_btree_path(trans
, path
);
1363 if (sib
&& sib
->level
== path
->level
&& path_l(sib
)->b
== path_l(path
)->b
)
1366 sib
= next_btree_path(trans
, path
);
1367 if (sib
&& sib
->level
== path
->level
&& path_l(sib
)->b
== path_l(path
)->b
)
1373 static inline void __bch2_path_free(struct btree_trans
*trans
, btree_path_idx_t path
)
1375 __bch2_btree_path_unlock(trans
, trans
->paths
+ path
);
1376 btree_path_list_remove(trans
, trans
->paths
+ path
);
1377 __clear_bit(path
, trans
->paths_allocated
);
1380 static bool bch2_btree_path_can_relock(struct btree_trans
*trans
, struct btree_path
*path
)
1382 unsigned l
= path
->level
;
1385 if (!btree_path_node(path
, l
))
1388 if (!is_btree_node(path
, l
))
1391 if (path
->l
[l
].lock_seq
!= path
->l
[l
].b
->c
.lock
.seq
)
1395 } while (l
< path
->locks_want
);
1400 void bch2_path_put(struct btree_trans
*trans
, btree_path_idx_t path_idx
, bool intent
)
1402 struct btree_path
*path
= trans
->paths
+ path_idx
, *dup
;
1404 if (!__btree_path_put(trans
, path
, intent
))
1407 dup
= path
->preserve
1408 ? have_path_at_pos(trans
, path
)
1409 : have_node_at_pos(trans
, path
);
1411 trace_btree_path_free(trans
, path_idx
, dup
);
1413 if (!dup
&& !(!path
->preserve
&& !is_btree_node(path
, path
->level
)))
1416 if (path
->should_be_locked
&& !trans
->restarted
) {
1421 ? bch2_btree_path_relock_norestart(trans
, dup
)
1422 : bch2_btree_path_can_relock(trans
, dup
)))
1427 dup
->preserve
|= path
->preserve
;
1428 dup
->should_be_locked
|= path
->should_be_locked
;
1431 __bch2_path_free(trans
, path_idx
);
1434 static void bch2_path_put_nokeep(struct btree_trans
*trans
, btree_path_idx_t path
,
1437 if (!__btree_path_put(trans
, trans
->paths
+ path
, intent
))
1440 __bch2_path_free(trans
, path
);
1443 void __noreturn
bch2_trans_restart_error(struct btree_trans
*trans
, u32 restart_count
)
1445 panic("trans->restart_count %u, should be %u, last restarted by %pS\n",
1446 trans
->restart_count
, restart_count
,
1447 (void *) trans
->last_begin_ip
);
1450 static void __noreturn
bch2_trans_in_restart_error(struct btree_trans
*trans
)
1452 #ifdef CONFIG_BCACHEFS_DEBUG
1453 struct printbuf buf
= PRINTBUF
;
1454 bch2_prt_backtrace(&buf
, &trans
->last_restarted_trace
);
1455 panic("in transaction restart: %s, last restarted by\n%s",
1456 bch2_err_str(trans
->restarted
),
1459 panic("in transaction restart: %s, last restarted by %pS\n",
1460 bch2_err_str(trans
->restarted
),
1461 (void *) trans
->last_restarted_ip
);
1465 void __noreturn
bch2_trans_unlocked_or_in_restart_error(struct btree_trans
*trans
)
1467 if (trans
->restarted
)
1468 bch2_trans_in_restart_error(trans
);
1471 panic("trans should be locked, unlocked by %pS\n",
1472 (void *) trans
->last_unlock_ip
);
1478 void bch2_trans_updates_to_text(struct printbuf
*buf
, struct btree_trans
*trans
)
1480 prt_printf(buf
, "%u transaction updates for %s journal seq %llu\n",
1481 trans
->nr_updates
, trans
->fn
, trans
->journal_res
.seq
);
1482 printbuf_indent_add(buf
, 2);
1484 trans_for_each_update(trans
, i
) {
1485 struct bkey_s_c old
= { &i
->old_k
, i
->old_v
};
1487 prt_str(buf
, "update: btree=");
1488 bch2_btree_id_to_text(buf
, i
->btree_id
);
1489 prt_printf(buf
, " cached=%u %pS\n",
1491 (void *) i
->ip_allocated
);
1493 prt_printf(buf
, " old ");
1494 bch2_bkey_val_to_text(buf
, trans
->c
, old
);
1497 prt_printf(buf
, " new ");
1498 bch2_bkey_val_to_text(buf
, trans
->c
, bkey_i_to_s_c(i
->k
));
1502 for (struct jset_entry
*e
= trans
->journal_entries
;
1503 e
!= btree_trans_journal_entries_top(trans
);
1504 e
= vstruct_next(e
))
1505 bch2_journal_entry_to_text(buf
, trans
->c
, e
);
1507 printbuf_indent_sub(buf
, 2);
1511 void bch2_dump_trans_updates(struct btree_trans
*trans
)
1513 struct printbuf buf
= PRINTBUF
;
1515 bch2_trans_updates_to_text(&buf
, trans
);
1516 bch2_print_str(trans
->c
, buf
.buf
);
1517 printbuf_exit(&buf
);
1520 static void bch2_btree_path_to_text_short(struct printbuf
*out
, struct btree_trans
*trans
, btree_path_idx_t path_idx
)
1522 struct btree_path
*path
= trans
->paths
+ path_idx
;
1524 prt_printf(out
, "path: idx %3u ref %u:%u %c %c %c ",
1525 path_idx
, path
->ref
, path
->intent_ref
,
1526 path
->preserve
? 'P' : ' ',
1527 path
->should_be_locked
? 'S' : ' ',
1528 path
->cached
? 'C' : 'B');
1529 bch2_btree_id_level_to_text(out
, path
->btree_id
, path
->level
);
1530 prt_str(out
, " pos ");
1531 bch2_bpos_to_text(out
, path
->pos
);
1533 if (!path
->cached
&& btree_node_locked(path
, path
->level
)) {
1535 struct btree
*b
= path_l(path
)->b
;
1536 bch2_bpos_to_text(out
, b
->data
->min_key
);
1538 bch2_bpos_to_text(out
, b
->key
.k
.p
);
1541 #ifdef TRACK_PATH_ALLOCATED
1542 prt_printf(out
, " %pS", (void *) path
->ip_allocated
);
1546 static const char *btree_node_locked_str(enum btree_node_locked_type t
)
1549 case BTREE_NODE_UNLOCKED
:
1551 case BTREE_NODE_READ_LOCKED
:
1553 case BTREE_NODE_INTENT_LOCKED
:
1555 case BTREE_NODE_WRITE_LOCKED
:
1562 void bch2_btree_path_to_text(struct printbuf
*out
, struct btree_trans
*trans
, btree_path_idx_t path_idx
)
1564 bch2_btree_path_to_text_short(out
, trans
, path_idx
);
1566 struct btree_path
*path
= trans
->paths
+ path_idx
;
1568 prt_printf(out
, " uptodate %u locks_want %u", path
->uptodate
, path
->locks_want
);
1571 printbuf_indent_add(out
, 2);
1572 for (unsigned l
= 0; l
< BTREE_MAX_DEPTH
; l
++) {
1573 prt_printf(out
, "l=%u locks %s seq %u node ", l
,
1574 btree_node_locked_str(btree_node_locked_type(path
, l
)),
1575 path
->l
[l
].lock_seq
);
1577 int ret
= PTR_ERR_OR_ZERO(path
->l
[l
].b
);
1579 prt_str(out
, bch2_err_str(ret
));
1581 prt_printf(out
, "%px", path
->l
[l
].b
);
1584 printbuf_indent_sub(out
, 2);
1587 static noinline __cold
1588 void __bch2_trans_paths_to_text(struct printbuf
*out
, struct btree_trans
*trans
,
1591 struct trans_for_each_path_inorder_iter iter
;
1594 btree_trans_sort_paths(trans
);
1596 trans_for_each_path_idx_inorder(trans
, iter
) {
1597 bch2_btree_path_to_text_short(out
, trans
, iter
.path_idx
);
1603 void bch2_trans_paths_to_text(struct printbuf
*out
, struct btree_trans
*trans
)
1605 __bch2_trans_paths_to_text(out
, trans
, false);
1608 static noinline __cold
1609 void __bch2_dump_trans_paths_updates(struct btree_trans
*trans
, bool nosort
)
1611 struct printbuf buf
= PRINTBUF
;
1613 __bch2_trans_paths_to_text(&buf
, trans
, nosort
);
1614 bch2_trans_updates_to_text(&buf
, trans
);
1616 bch2_print_str(trans
->c
, buf
.buf
);
1617 printbuf_exit(&buf
);
1621 void bch2_dump_trans_paths_updates(struct btree_trans
*trans
)
1623 __bch2_dump_trans_paths_updates(trans
, false);
1627 static void bch2_trans_update_max_paths(struct btree_trans
*trans
)
1629 struct btree_transaction_stats
*s
= btree_trans_stats(trans
);
1630 struct printbuf buf
= PRINTBUF
;
1631 size_t nr
= bitmap_weight(trans
->paths_allocated
, trans
->nr_paths
);
1633 bch2_trans_paths_to_text(&buf
, trans
);
1635 if (!buf
.allocation_failure
) {
1636 mutex_lock(&s
->lock
);
1637 if (nr
> s
->nr_max_paths
) {
1638 s
->nr_max_paths
= nr
;
1639 swap(s
->max_paths_text
, buf
.buf
);
1641 mutex_unlock(&s
->lock
);
1644 printbuf_exit(&buf
);
1646 trans
->nr_paths_max
= nr
;
1650 int __bch2_btree_trans_too_many_iters(struct btree_trans
*trans
)
1652 if (trace_trans_restart_too_many_iters_enabled()) {
1653 struct printbuf buf
= PRINTBUF
;
1655 bch2_trans_paths_to_text(&buf
, trans
);
1656 trace_trans_restart_too_many_iters(trans
, _THIS_IP_
, buf
.buf
);
1657 printbuf_exit(&buf
);
1660 count_event(trans
->c
, trans_restart_too_many_iters
);
1662 return btree_trans_restart(trans
, BCH_ERR_transaction_restart_too_many_iters
);
1665 static noinline
void btree_path_overflow(struct btree_trans
*trans
)
1667 bch2_dump_trans_paths_updates(trans
);
1668 bch_err(trans
->c
, "trans path overflow");
1671 static noinline
void btree_paths_realloc(struct btree_trans
*trans
)
1673 unsigned nr
= trans
->nr_paths
* 2;
1675 void *p
= kvzalloc(BITS_TO_LONGS(nr
) * sizeof(unsigned long) +
1676 sizeof(struct btree_trans_paths
) +
1677 nr
* sizeof(struct btree_path
) +
1678 nr
* sizeof(btree_path_idx_t
) + 8 +
1679 nr
* sizeof(struct btree_insert_entry
), GFP_KERNEL
|__GFP_NOFAIL
);
1681 unsigned long *paths_allocated
= p
;
1682 memcpy(paths_allocated
, trans
->paths_allocated
, BITS_TO_LONGS(trans
->nr_paths
) * sizeof(unsigned long));
1683 p
+= BITS_TO_LONGS(nr
) * sizeof(unsigned long);
1685 p
+= sizeof(struct btree_trans_paths
);
1686 struct btree_path
*paths
= p
;
1687 *trans_paths_nr(paths
) = nr
;
1688 memcpy(paths
, trans
->paths
, trans
->nr_paths
* sizeof(struct btree_path
));
1689 p
+= nr
* sizeof(struct btree_path
);
1691 btree_path_idx_t
*sorted
= p
;
1692 memcpy(sorted
, trans
->sorted
, trans
->nr_sorted
* sizeof(btree_path_idx_t
));
1693 p
+= nr
* sizeof(btree_path_idx_t
) + 8;
1695 struct btree_insert_entry
*updates
= p
;
1696 memcpy(updates
, trans
->updates
, trans
->nr_paths
* sizeof(struct btree_insert_entry
));
1698 unsigned long *old
= trans
->paths_allocated
;
1700 rcu_assign_pointer(trans
->paths_allocated
, paths_allocated
);
1701 rcu_assign_pointer(trans
->paths
, paths
);
1702 rcu_assign_pointer(trans
->sorted
, sorted
);
1703 rcu_assign_pointer(trans
->updates
, updates
);
1705 trans
->nr_paths
= nr
;
1707 if (old
!= trans
->_paths_allocated
)
1708 kfree_rcu_mightsleep(old
);
1711 static inline btree_path_idx_t
btree_path_alloc(struct btree_trans
*trans
,
1712 btree_path_idx_t pos
)
1714 btree_path_idx_t idx
= find_first_zero_bit(trans
->paths_allocated
, trans
->nr_paths
);
1716 if (unlikely(idx
== trans
->nr_paths
)) {
1717 if (trans
->nr_paths
== BTREE_ITER_MAX
) {
1718 btree_path_overflow(trans
);
1722 btree_paths_realloc(trans
);
1726 * Do this before marking the new path as allocated, since it won't be
1729 if (unlikely(idx
> trans
->nr_paths_max
))
1730 bch2_trans_update_max_paths(trans
);
1732 __set_bit(idx
, trans
->paths_allocated
);
1734 struct btree_path
*path
= &trans
->paths
[idx
];
1736 path
->intent_ref
= 0;
1737 path
->nodes_locked
= 0;
1739 btree_path_list_add(trans
, pos
, idx
);
1740 trans
->paths_sorted
= false;
1744 btree_path_idx_t
bch2_path_get(struct btree_trans
*trans
,
1745 enum btree_id btree_id
, struct bpos pos
,
1746 unsigned locks_want
, unsigned level
,
1747 unsigned flags
, unsigned long ip
)
1749 struct btree_path
*path
;
1750 bool cached
= flags
& BTREE_ITER_cached
;
1751 bool intent
= flags
& BTREE_ITER_intent
;
1752 struct trans_for_each_path_inorder_iter iter
;
1753 btree_path_idx_t path_pos
= 0, path_idx
;
1755 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
1756 bch2_trans_verify_locks(trans
);
1758 btree_trans_sort_paths(trans
);
1760 trans_for_each_path_inorder(trans
, path
, iter
) {
1761 if (__btree_path_cmp(path
,
1768 path_pos
= iter
.path_idx
;
1772 trans
->paths
[path_pos
].cached
== cached
&&
1773 trans
->paths
[path_pos
].btree_id
== btree_id
&&
1774 trans
->paths
[path_pos
].level
== level
) {
1775 trace_btree_path_get(trans
, trans
->paths
+ path_pos
, &pos
);
1777 __btree_path_get(trans
, trans
->paths
+ path_pos
, intent
);
1778 path_idx
= bch2_btree_path_set_pos(trans
, path_pos
, pos
, intent
, ip
);
1779 path
= trans
->paths
+ path_idx
;
1781 path_idx
= btree_path_alloc(trans
, path_pos
);
1782 path
= trans
->paths
+ path_idx
;
1784 __btree_path_get(trans
, path
, intent
);
1786 path
->btree_id
= btree_id
;
1787 path
->cached
= cached
;
1788 path
->uptodate
= BTREE_ITER_NEED_TRAVERSE
;
1789 path
->should_be_locked
= false;
1790 path
->level
= level
;
1791 path
->locks_want
= locks_want
;
1792 path
->nodes_locked
= 0;
1793 for (unsigned i
= 0; i
< ARRAY_SIZE(path
->l
); i
++)
1794 path
->l
[i
].b
= ERR_PTR(-BCH_ERR_no_btree_node_init
);
1795 #ifdef TRACK_PATH_ALLOCATED
1796 path
->ip_allocated
= ip
;
1798 trans
->paths_sorted
= false;
1800 trace_btree_path_alloc(trans
, path
);
1803 if (!(flags
& BTREE_ITER_nopreserve
))
1804 path
->preserve
= true;
1806 if (path
->intent_ref
)
1807 locks_want
= max(locks_want
, level
+ 1);
1810 * If the path has locks_want greater than requested, we don't downgrade
1811 * it here - on transaction restart because btree node split needs to
1812 * upgrade locks, we might be putting/getting the iterator again.
1813 * Downgrading iterators only happens via bch2_trans_downgrade(), after
1814 * a successful transaction commit.
1817 locks_want
= min(locks_want
, BTREE_MAX_DEPTH
);
1818 if (locks_want
> path
->locks_want
)
1819 bch2_btree_path_upgrade_noupgrade_sibs(trans
, path
, locks_want
, NULL
);
1824 btree_path_idx_t
bch2_path_get_unlocked_mut(struct btree_trans
*trans
,
1825 enum btree_id btree_id
,
1829 btree_path_idx_t path_idx
= bch2_path_get(trans
, btree_id
, pos
, level
+ 1, level
,
1830 BTREE_ITER_nopreserve
|
1831 BTREE_ITER_intent
, _RET_IP_
);
1832 path_idx
= bch2_btree_path_make_mut(trans
, path_idx
, true, _RET_IP_
);
1834 struct btree_path
*path
= trans
->paths
+ path_idx
;
1835 bch2_btree_path_downgrade(trans
, path
);
1836 __bch2_btree_path_unlock(trans
, path
);
1840 struct bkey_s_c
bch2_btree_path_peek_slot(struct btree_path
*path
, struct bkey
*u
)
1843 struct btree_path_level
*l
= path_l(path
);
1844 struct bkey_packed
*_k
;
1847 if (unlikely(!l
->b
))
1848 return bkey_s_c_null
;
1850 EBUG_ON(path
->uptodate
!= BTREE_ITER_UPTODATE
);
1851 EBUG_ON(!btree_node_locked(path
, path
->level
));
1853 if (!path
->cached
) {
1854 _k
= bch2_btree_node_iter_peek_all(&l
->iter
, l
->b
);
1855 k
= _k
? bkey_disassemble(l
->b
, _k
, u
) : bkey_s_c_null
;
1857 EBUG_ON(k
.k
&& bkey_deleted(k
.k
) && bpos_eq(k
.k
->p
, path
->pos
));
1859 if (!k
.k
|| !bpos_eq(path
->pos
, k
.k
->p
))
1862 struct bkey_cached
*ck
= (void *) path
->l
[0].b
;
1864 return bkey_s_c_null
;
1866 EBUG_ON(path
->btree_id
!= ck
->key
.btree_id
||
1867 !bkey_eq(path
->pos
, ck
->key
.pos
));
1870 k
= (struct bkey_s_c
) { u
, &ck
->k
->v
};
1877 return (struct bkey_s_c
) { u
, NULL
};
1880 void bch2_set_btree_iter_dontneed(struct btree_iter
*iter
)
1882 struct btree_trans
*trans
= iter
->trans
;
1884 if (!iter
->path
|| trans
->restarted
)
1887 struct btree_path
*path
= btree_iter_path(trans
, iter
);
1888 path
->preserve
= false;
1890 path
->should_be_locked
= false;
1892 /* Btree iterators: */
1895 __bch2_btree_iter_traverse(struct btree_iter
*iter
)
1897 return bch2_btree_path_traverse(iter
->trans
, iter
->path
, iter
->flags
);
1901 bch2_btree_iter_traverse(struct btree_iter
*iter
)
1903 struct btree_trans
*trans
= iter
->trans
;
1906 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
1908 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
,
1909 btree_iter_search_key(iter
),
1910 iter
->flags
& BTREE_ITER_intent
,
1911 btree_iter_ip_allocated(iter
));
1913 ret
= bch2_btree_path_traverse(iter
->trans
, iter
->path
, iter
->flags
);
1917 struct btree_path
*path
= btree_iter_path(trans
, iter
);
1918 if (btree_path_node(path
, path
->level
))
1919 btree_path_set_should_be_locked(trans
, path
);
1923 /* Iterate across nodes (leaf and interior nodes) */
1925 struct btree
*bch2_btree_iter_peek_node(struct btree_iter
*iter
)
1927 struct btree_trans
*trans
= iter
->trans
;
1928 struct btree
*b
= NULL
;
1931 EBUG_ON(trans
->paths
[iter
->path
].cached
);
1932 bch2_btree_iter_verify(iter
);
1934 ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
1938 struct btree_path
*path
= btree_iter_path(trans
, iter
);
1939 b
= btree_path_node(path
, path
->level
);
1943 BUG_ON(bpos_lt(b
->key
.k
.p
, iter
->pos
));
1945 bkey_init(&iter
->k
);
1946 iter
->k
.p
= iter
->pos
= b
->key
.k
.p
;
1948 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
, b
->key
.k
.p
,
1949 iter
->flags
& BTREE_ITER_intent
,
1950 btree_iter_ip_allocated(iter
));
1951 btree_path_set_should_be_locked(trans
, btree_iter_path(trans
, iter
));
1953 bch2_btree_iter_verify_entry_exit(iter
);
1954 bch2_btree_iter_verify(iter
);
1962 /* Only kept for -tools */
1963 struct btree
*bch2_btree_iter_peek_node_and_restart(struct btree_iter
*iter
)
1967 while (b
= bch2_btree_iter_peek_node(iter
),
1968 bch2_err_matches(PTR_ERR_OR_ZERO(b
), BCH_ERR_transaction_restart
))
1969 bch2_trans_begin(iter
->trans
);
1974 struct btree
*bch2_btree_iter_next_node(struct btree_iter
*iter
)
1976 struct btree_trans
*trans
= iter
->trans
;
1977 struct btree
*b
= NULL
;
1980 EBUG_ON(trans
->paths
[iter
->path
].cached
);
1981 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
1982 bch2_btree_iter_verify(iter
);
1984 ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
1989 struct btree_path
*path
= btree_iter_path(trans
, iter
);
1991 /* already at end? */
1992 if (!btree_path_node(path
, path
->level
))
1996 if (!btree_path_node(path
, path
->level
+ 1)) {
1997 btree_path_set_level_up(trans
, path
);
2001 if (!bch2_btree_node_relock(trans
, path
, path
->level
+ 1)) {
2002 __bch2_btree_path_unlock(trans
, path
);
2003 path
->l
[path
->level
].b
= ERR_PTR(-BCH_ERR_no_btree_node_relock
);
2004 path
->l
[path
->level
+ 1].b
= ERR_PTR(-BCH_ERR_no_btree_node_relock
);
2005 btree_path_set_dirty(path
, BTREE_ITER_NEED_TRAVERSE
);
2006 trace_and_count(trans
->c
, trans_restart_relock_next_node
, trans
, _THIS_IP_
, path
);
2007 ret
= btree_trans_restart(trans
, BCH_ERR_transaction_restart_relock
);
2011 b
= btree_path_node(path
, path
->level
+ 1);
2013 if (bpos_eq(iter
->pos
, b
->key
.k
.p
)) {
2014 __btree_path_set_level_up(trans
, path
, path
->level
++);
2016 if (btree_lock_want(path
, path
->level
+ 1) == BTREE_NODE_UNLOCKED
)
2017 btree_node_unlock(trans
, path
, path
->level
+ 1);
2020 * Haven't gotten to the end of the parent node: go back down to
2021 * the next child node
2023 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
,
2024 bpos_successor(iter
->pos
),
2025 iter
->flags
& BTREE_ITER_intent
,
2026 btree_iter_ip_allocated(iter
));
2028 path
= btree_iter_path(trans
, iter
);
2029 btree_path_set_level_down(trans
, path
, iter
->min_depth
);
2031 ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
2035 path
= btree_iter_path(trans
, iter
);
2036 b
= path
->l
[path
->level
].b
;
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 btree_path_set_should_be_locked(trans
, btree_iter_path(trans
, iter
));
2046 EBUG_ON(btree_iter_path(trans
, iter
)->uptodate
);
2048 bch2_btree_iter_verify_entry_exit(iter
);
2049 bch2_btree_iter_verify(iter
);
2057 /* Iterate across keys (in leaf nodes only) */
2059 inline bool bch2_btree_iter_advance(struct btree_iter
*iter
)
2061 struct bpos pos
= iter
->k
.p
;
2062 bool ret
= !(iter
->flags
& BTREE_ITER_all_snapshots
2063 ? bpos_eq(pos
, SPOS_MAX
)
2064 : bkey_eq(pos
, SPOS_MAX
));
2066 if (ret
&& !(iter
->flags
& BTREE_ITER_is_extents
))
2067 pos
= bkey_successor(iter
, pos
);
2068 bch2_btree_iter_set_pos(iter
, pos
);
2072 inline bool bch2_btree_iter_rewind(struct btree_iter
*iter
)
2074 struct bpos pos
= bkey_start_pos(&iter
->k
);
2075 bool ret
= !(iter
->flags
& BTREE_ITER_all_snapshots
2076 ? bpos_eq(pos
, POS_MIN
)
2077 : bkey_eq(pos
, POS_MIN
));
2079 if (ret
&& !(iter
->flags
& BTREE_ITER_is_extents
))
2080 pos
= bkey_predecessor(iter
, pos
);
2081 bch2_btree_iter_set_pos(iter
, pos
);
2086 void bch2_btree_trans_peek_prev_updates(struct btree_trans
*trans
, struct btree_iter
*iter
,
2089 struct bpos end
= path_l(btree_iter_path(trans
, iter
))->b
->data
->min_key
;
2091 trans_for_each_update(trans
, i
)
2092 if (!i
->key_cache_already_flushed
&&
2093 i
->btree_id
== iter
->btree_id
&&
2094 bpos_le(i
->k
->k
.p
, iter
->pos
) &&
2095 bpos_ge(i
->k
->k
.p
, k
->k
? k
->k
->p
: end
)) {
2097 *k
= bkey_i_to_s_c(i
->k
);
2102 void bch2_btree_trans_peek_updates(struct btree_trans
*trans
, struct btree_iter
*iter
,
2105 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2106 struct bpos end
= path_l(path
)->b
->key
.k
.p
;
2108 trans_for_each_update(trans
, i
)
2109 if (!i
->key_cache_already_flushed
&&
2110 i
->btree_id
== iter
->btree_id
&&
2111 bpos_ge(i
->k
->k
.p
, path
->pos
) &&
2112 bpos_le(i
->k
->k
.p
, k
->k
? k
->k
->p
: end
)) {
2114 *k
= bkey_i_to_s_c(i
->k
);
2119 void bch2_btree_trans_peek_slot_updates(struct btree_trans
*trans
, struct btree_iter
*iter
,
2122 trans_for_each_update(trans
, i
)
2123 if (!i
->key_cache_already_flushed
&&
2124 i
->btree_id
== iter
->btree_id
&&
2125 bpos_eq(i
->k
->k
.p
, iter
->pos
)) {
2127 *k
= bkey_i_to_s_c(i
->k
);
2131 static struct bkey_i
*bch2_btree_journal_peek(struct btree_trans
*trans
,
2132 struct btree_iter
*iter
,
2133 struct bpos end_pos
)
2135 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2137 return bch2_journal_keys_peek_max(trans
->c
, iter
->btree_id
,
2141 &iter
->journal_idx
);
2145 struct bkey_s_c
btree_trans_peek_slot_journal(struct btree_trans
*trans
,
2146 struct btree_iter
*iter
)
2148 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2149 struct bkey_i
*k
= bch2_btree_journal_peek(trans
, iter
, path
->pos
);
2153 return bkey_i_to_s_c(k
);
2155 return bkey_s_c_null
;
2160 void btree_trans_peek_journal(struct btree_trans
*trans
,
2161 struct btree_iter
*iter
,
2164 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2165 struct bkey_i
*next_journal
=
2166 bch2_btree_journal_peek(trans
, iter
,
2167 k
->k
? k
->k
->p
: path_l(path
)->b
->key
.k
.p
);
2169 iter
->k
= next_journal
->k
;
2170 *k
= bkey_i_to_s_c(next_journal
);
2174 static struct bkey_i
*bch2_btree_journal_peek_prev(struct btree_trans
*trans
,
2175 struct btree_iter
*iter
,
2176 struct bpos end_pos
)
2178 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2180 return bch2_journal_keys_peek_prev_min(trans
->c
, iter
->btree_id
,
2184 &iter
->journal_idx
);
2188 void btree_trans_peek_prev_journal(struct btree_trans
*trans
,
2189 struct btree_iter
*iter
,
2192 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2193 struct bkey_i
*next_journal
=
2194 bch2_btree_journal_peek_prev(trans
, iter
,
2195 k
->k
? k
->k
->p
: path_l(path
)->b
->key
.k
.p
);
2198 iter
->k
= next_journal
->k
;
2199 *k
= bkey_i_to_s_c(next_journal
);
2204 * Checks btree key cache for key at iter->pos and returns it if present, or
2208 struct bkey_s_c
btree_trans_peek_key_cache(struct btree_iter
*iter
, struct bpos pos
)
2210 struct btree_trans
*trans
= iter
->trans
;
2211 struct bch_fs
*c
= trans
->c
;
2216 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
2218 if ((iter
->flags
& BTREE_ITER_key_cache_fill
) &&
2219 bpos_eq(iter
->pos
, pos
))
2220 return bkey_s_c_null
;
2222 if (!bch2_btree_key_cache_find(c
, iter
->btree_id
, pos
))
2223 return bkey_s_c_null
;
2225 if (!iter
->key_cache_path
)
2226 iter
->key_cache_path
= bch2_path_get(trans
, iter
->btree_id
, pos
,
2227 iter
->flags
& BTREE_ITER_intent
, 0,
2228 iter
->flags
|BTREE_ITER_cached
|
2229 BTREE_ITER_cached_nofill
,
2232 iter
->key_cache_path
= bch2_btree_path_set_pos(trans
, iter
->key_cache_path
, pos
,
2233 iter
->flags
& BTREE_ITER_intent
,
2234 btree_iter_ip_allocated(iter
));
2236 ret
= bch2_btree_path_traverse(trans
, iter
->key_cache_path
,
2237 iter
->flags
|BTREE_ITER_cached
) ?:
2238 bch2_btree_path_relock(trans
, btree_iter_path(trans
, iter
), _THIS_IP_
);
2240 return bkey_s_c_err(ret
);
2242 k
= bch2_btree_path_peek_slot(trans
->paths
+ iter
->key_cache_path
, &u
);
2246 if ((iter
->flags
& BTREE_ITER_all_snapshots
) &&
2247 !bpos_eq(pos
, k
.k
->p
))
2248 return bkey_s_c_null
;
2252 btree_path_set_should_be_locked(trans
, trans
->paths
+ iter
->key_cache_path
);
2256 static struct bkey_s_c
__bch2_btree_iter_peek(struct btree_iter
*iter
, struct bpos search_key
)
2258 struct btree_trans
*trans
= iter
->trans
;
2259 struct bkey_s_c k
, k2
;
2262 EBUG_ON(btree_iter_path(trans
, iter
)->cached
);
2263 bch2_btree_iter_verify(iter
);
2266 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
, search_key
,
2267 iter
->flags
& BTREE_ITER_intent
,
2268 btree_iter_ip_allocated(iter
));
2270 ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
2271 if (unlikely(ret
)) {
2272 /* ensure that iter->k is consistent with iter->pos: */
2273 bch2_btree_iter_set_pos(iter
, iter
->pos
);
2274 k
= bkey_s_c_err(ret
);
2278 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2279 struct btree_path_level
*l
= path_l(path
);
2281 if (unlikely(!l
->b
)) {
2282 /* No btree nodes at requested level: */
2283 bch2_btree_iter_set_pos(iter
, SPOS_MAX
);
2288 btree_path_set_should_be_locked(trans
, path
);
2290 k
= btree_path_level_peek_all(trans
->c
, l
, &iter
->k
);
2292 if (unlikely(iter
->flags
& BTREE_ITER_with_key_cache
) &&
2294 (k2
= btree_trans_peek_key_cache(iter
, k
.k
->p
)).k
) {
2297 bch2_btree_iter_set_pos(iter
, iter
->pos
);
2302 if (unlikely(iter
->flags
& BTREE_ITER_with_journal
))
2303 btree_trans_peek_journal(trans
, iter
, &k
);
2305 if (unlikely((iter
->flags
& BTREE_ITER_with_updates
) &&
2307 bch2_btree_trans_peek_updates(trans
, iter
, &k
);
2309 if (k
.k
&& bkey_deleted(k
.k
)) {
2311 * If we've got a whiteout, and it's after the search
2312 * key, advance the search key to the whiteout instead
2313 * of just after the whiteout - it might be a btree
2314 * whiteout, with a real key at the same position, since
2315 * in the btree deleted keys sort before non deleted.
2317 search_key
= !bpos_eq(search_key
, k
.k
->p
)
2319 : bpos_successor(k
.k
->p
);
2325 } else if (likely(!bpos_eq(l
->b
->key
.k
.p
, SPOS_MAX
))) {
2326 /* Advance to next leaf node: */
2327 search_key
= bpos_successor(l
->b
->key
.k
.p
);
2330 bch2_btree_iter_set_pos(iter
, SPOS_MAX
);
2336 bch2_btree_iter_verify(iter
);
2341 * bch2_btree_iter_peek_max() - returns first key greater than or equal to
2342 * iterator's current position
2343 * @iter: iterator to peek from
2344 * @end: search limit: returns keys less than or equal to @end
2346 * Returns: key if found, or an error extractable with bkey_err().
2348 struct bkey_s_c
bch2_btree_iter_peek_max(struct btree_iter
*iter
, struct bpos end
)
2350 struct btree_trans
*trans
= iter
->trans
;
2351 struct bpos search_key
= btree_iter_search_key(iter
);
2353 struct bpos iter_pos
= iter
->pos
;
2356 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
2357 bch2_btree_iter_verify_entry_exit(iter
);
2358 EBUG_ON((iter
->flags
& BTREE_ITER_filter_snapshots
) && bkey_eq(end
, POS_MAX
));
2360 if (iter
->update_path
) {
2361 bch2_path_put_nokeep(trans
, iter
->update_path
,
2362 iter
->flags
& BTREE_ITER_intent
);
2363 iter
->update_path
= 0;
2367 k
= __bch2_btree_iter_peek(iter
, search_key
);
2370 if (unlikely(bkey_err(k
)))
2373 if (iter
->flags
& BTREE_ITER_filter_snapshots
) {
2375 * We need to check against @end before FILTER_SNAPSHOTS because
2376 * if we get to a different inode that requested we might be
2377 * seeing keys for a different snapshot tree that will all be
2380 * But we can't do the full check here, because bkey_start_pos()
2381 * isn't monotonically increasing before FILTER_SNAPSHOTS, and
2382 * that's what we check against in extents mode:
2384 if (unlikely(!(iter
->flags
& BTREE_ITER_is_extents
)
2385 ? bkey_gt(k
.k
->p
, end
)
2386 : k
.k
->p
.inode
> end
.inode
))
2389 if (iter
->update_path
&&
2390 !bkey_eq(trans
->paths
[iter
->update_path
].pos
, k
.k
->p
)) {
2391 bch2_path_put_nokeep(trans
, iter
->update_path
,
2392 iter
->flags
& BTREE_ITER_intent
);
2393 iter
->update_path
= 0;
2396 if ((iter
->flags
& BTREE_ITER_intent
) &&
2397 !(iter
->flags
& BTREE_ITER_is_extents
) &&
2398 !iter
->update_path
) {
2399 struct bpos pos
= k
.k
->p
;
2401 if (pos
.snapshot
< iter
->snapshot
) {
2402 search_key
= bpos_successor(k
.k
->p
);
2406 pos
.snapshot
= iter
->snapshot
;
2409 * advance, same as on exit for iter->path, but only up
2412 __btree_path_get(trans
, trans
->paths
+ iter
->path
, iter
->flags
& BTREE_ITER_intent
);
2413 iter
->update_path
= iter
->path
;
2415 iter
->update_path
= bch2_btree_path_set_pos(trans
,
2416 iter
->update_path
, pos
,
2417 iter
->flags
& BTREE_ITER_intent
,
2419 ret
= bch2_btree_path_traverse(trans
, iter
->update_path
, iter
->flags
);
2420 if (unlikely(ret
)) {
2421 k
= bkey_s_c_err(ret
);
2427 * We can never have a key in a leaf node at POS_MAX, so
2428 * we don't have to check these successor() calls:
2430 if (!bch2_snapshot_is_ancestor(trans
->c
,
2433 search_key
= bpos_successor(k
.k
->p
);
2437 if (bkey_whiteout(k
.k
) &&
2438 !(iter
->flags
& BTREE_ITER_key_cache_fill
)) {
2439 search_key
= bkey_successor(iter
, k
.k
->p
);
2445 * iter->pos should be mononotically increasing, and always be
2446 * equal to the key we just returned - except extents can
2447 * straddle iter->pos:
2449 if (!(iter
->flags
& BTREE_ITER_is_extents
))
2452 iter_pos
= bkey_max(iter
->pos
, bkey_start_pos(k
.k
));
2454 if (unlikely(iter
->flags
& BTREE_ITER_all_snapshots
? bpos_gt(iter_pos
, end
) :
2455 iter
->flags
& BTREE_ITER_is_extents
? bkey_ge(iter_pos
, end
) :
2456 bkey_gt(iter_pos
, end
)))
2462 iter
->pos
= iter_pos
;
2464 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
, k
.k
->p
,
2465 iter
->flags
& BTREE_ITER_intent
,
2466 btree_iter_ip_allocated(iter
));
2468 btree_path_set_should_be_locked(trans
, btree_iter_path(trans
, iter
));
2470 if (iter
->update_path
) {
2471 ret
= bch2_btree_path_relock(trans
, trans
->paths
+ iter
->update_path
, _THIS_IP_
);
2473 k
= bkey_s_c_err(ret
);
2475 btree_path_set_should_be_locked(trans
, trans
->paths
+ iter
->update_path
);
2478 if (!(iter
->flags
& BTREE_ITER_all_snapshots
))
2479 iter
->pos
.snapshot
= iter
->snapshot
;
2481 ret
= bch2_btree_iter_verify_ret(iter
, k
);
2482 if (unlikely(ret
)) {
2483 bch2_btree_iter_set_pos(iter
, iter
->pos
);
2484 k
= bkey_s_c_err(ret
);
2487 bch2_btree_iter_verify_entry_exit(iter
);
2491 bch2_btree_iter_set_pos(iter
, end
);
2497 * bch2_btree_iter_next() - returns first key greater than iterator's current
2499 * @iter: iterator to peek from
2501 * Returns: key if found, or an error extractable with bkey_err().
2503 struct bkey_s_c
bch2_btree_iter_next(struct btree_iter
*iter
)
2505 if (!bch2_btree_iter_advance(iter
))
2506 return bkey_s_c_null
;
2508 return bch2_btree_iter_peek(iter
);
2511 static struct bkey_s_c
__bch2_btree_iter_peek_prev(struct btree_iter
*iter
, struct bpos search_key
)
2513 struct btree_trans
*trans
= iter
->trans
;
2514 struct bkey_s_c k
, k2
;
2516 bch2_btree_iter_verify(iter
);
2519 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
, search_key
,
2520 iter
->flags
& BTREE_ITER_intent
,
2521 btree_iter_ip_allocated(iter
));
2523 int ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
2524 if (unlikely(ret
)) {
2525 /* ensure that iter->k is consistent with iter->pos: */
2526 bch2_btree_iter_set_pos(iter
, iter
->pos
);
2527 k
= bkey_s_c_err(ret
);
2531 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2532 struct btree_path_level
*l
= path_l(path
);
2534 if (unlikely(!l
->b
)) {
2535 /* No btree nodes at requested level: */
2536 bch2_btree_iter_set_pos(iter
, SPOS_MAX
);
2541 btree_path_set_should_be_locked(trans
, path
);
2543 k
= btree_path_level_peek_all(trans
->c
, l
, &iter
->k
);
2544 if (!k
.k
|| bpos_gt(k
.k
->p
, search_key
)) {
2545 k
= btree_path_level_prev(trans
, path
, l
, &iter
->k
);
2547 BUG_ON(k
.k
&& bpos_gt(k
.k
->p
, search_key
));
2550 if (unlikely(iter
->flags
& BTREE_ITER_with_key_cache
) &&
2552 (k2
= btree_trans_peek_key_cache(iter
, k
.k
->p
)).k
) {
2555 bch2_btree_iter_set_pos(iter
, iter
->pos
);
2560 if (unlikely(iter
->flags
& BTREE_ITER_with_journal
))
2561 btree_trans_peek_prev_journal(trans
, iter
, &k
);
2563 if (unlikely((iter
->flags
& BTREE_ITER_with_updates
) &&
2565 bch2_btree_trans_peek_prev_updates(trans
, iter
, &k
);
2567 if (likely(k
.k
&& !bkey_deleted(k
.k
))) {
2570 search_key
= bpos_predecessor(k
.k
->p
);
2571 } else if (likely(!bpos_eq(path
->l
[0].b
->data
->min_key
, POS_MIN
))) {
2572 /* Advance to previous leaf node: */
2573 search_key
= bpos_predecessor(path
->l
[0].b
->data
->min_key
);
2575 /* Start of btree: */
2576 bch2_btree_iter_set_pos(iter
, POS_MIN
);
2582 bch2_btree_iter_verify(iter
);
2587 * bch2_btree_iter_peek_prev_min() - returns first key less than or equal to
2588 * iterator's current position
2589 * @iter: iterator to peek from
2590 * @end: search limit: returns keys greater than or equal to @end
2592 * Returns: key if found, or an error extractable with bkey_err().
2594 struct bkey_s_c
bch2_btree_iter_peek_prev_min(struct btree_iter
*iter
, struct bpos end
)
2596 if ((iter
->flags
& (BTREE_ITER_is_extents
|BTREE_ITER_filter_snapshots
)) &&
2597 !bkey_eq(iter
->pos
, POS_MAX
)) {
2599 * bkey_start_pos(), for extents, is not monotonically
2600 * increasing until after filtering for snapshots:
2602 * Thus, for extents we need to search forward until we find a
2603 * real visible extents - easiest to just use peek_slot() (which
2604 * internally uses peek() for extents)
2606 struct bkey_s_c k
= bch2_btree_iter_peek_slot(iter
);
2610 if (!bkey_deleted(k
.k
) &&
2611 (!(iter
->flags
& BTREE_ITER_is_extents
) ||
2612 bkey_lt(bkey_start_pos(k
.k
), iter
->pos
)))
2616 struct btree_trans
*trans
= iter
->trans
;
2617 struct bpos search_key
= iter
->pos
;
2619 btree_path_idx_t saved_path
= 0;
2621 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
2622 bch2_btree_iter_verify_entry_exit(iter
);
2623 EBUG_ON((iter
->flags
& BTREE_ITER_filter_snapshots
) && bpos_eq(end
, POS_MIN
));
2626 k
= __bch2_btree_iter_peek_prev(iter
, search_key
);
2629 if (unlikely(bkey_err(k
)))
2632 if (iter
->flags
& BTREE_ITER_filter_snapshots
) {
2633 struct btree_path
*s
= saved_path
? trans
->paths
+ saved_path
: NULL
;
2634 if (s
&& bpos_lt(k
.k
->p
, SPOS(s
->pos
.inode
, s
->pos
.offset
, iter
->snapshot
))) {
2636 * If we have a saved candidate, and we're past
2637 * the last possible snapshot overwrite, return
2640 bch2_path_put_nokeep(trans
, iter
->path
,
2641 iter
->flags
& BTREE_ITER_intent
);
2642 iter
->path
= saved_path
;
2644 k
= bch2_btree_path_peek_slot(btree_iter_path(trans
, iter
), &iter
->k
);
2649 * We need to check against @end before FILTER_SNAPSHOTS because
2650 * if we get to a different inode that requested we might be
2651 * seeing keys for a different snapshot tree that will all be
2654 if (unlikely(bkey_lt(k
.k
->p
, end
)))
2657 if (!bch2_snapshot_is_ancestor(trans
->c
, iter
->snapshot
, k
.k
->p
.snapshot
)) {
2658 search_key
= bpos_predecessor(k
.k
->p
);
2662 if (k
.k
->p
.snapshot
!= iter
->snapshot
) {
2664 * Have a key visible in iter->snapshot, but
2665 * might have overwrites: - save it and keep
2666 * searching. Unless it's a whiteout - then drop
2667 * our previous saved candidate:
2670 bch2_path_put_nokeep(trans
, saved_path
,
2671 iter
->flags
& BTREE_ITER_intent
);
2675 if (!bkey_whiteout(k
.k
)) {
2676 saved_path
= btree_path_clone(trans
, iter
->path
,
2677 iter
->flags
& BTREE_ITER_intent
,
2679 trace_btree_path_save_pos(trans
,
2680 trans
->paths
+ iter
->path
,
2681 trans
->paths
+ saved_path
);
2684 search_key
= bpos_predecessor(k
.k
->p
);
2688 if (bkey_whiteout(k
.k
)) {
2689 search_key
= bkey_predecessor(iter
, k
.k
->p
);
2690 search_key
.snapshot
= U32_MAX
;
2695 EBUG_ON(iter
->flags
& BTREE_ITER_all_snapshots
? bpos_gt(k
.k
->p
, iter
->pos
) :
2696 iter
->flags
& BTREE_ITER_is_extents
? bkey_ge(bkey_start_pos(k
.k
), iter
->pos
) :
2697 bkey_gt(k
.k
->p
, iter
->pos
));
2699 if (unlikely(iter
->flags
& BTREE_ITER_all_snapshots
? bpos_lt(k
.k
->p
, end
) :
2700 iter
->flags
& BTREE_ITER_is_extents
? bkey_le(k
.k
->p
, end
) :
2701 bkey_lt(k
.k
->p
, end
)))
2707 /* Extents can straddle iter->pos: */
2708 iter
->pos
= bpos_min(iter
->pos
, k
.k
->p
);;
2710 if (iter
->flags
& BTREE_ITER_filter_snapshots
)
2711 iter
->pos
.snapshot
= iter
->snapshot
;
2714 bch2_path_put_nokeep(trans
, saved_path
, iter
->flags
& BTREE_ITER_intent
);
2716 bch2_btree_iter_verify_entry_exit(iter
);
2717 bch2_btree_iter_verify(iter
);
2720 bch2_btree_iter_set_pos(iter
, end
);
2726 * bch2_btree_iter_prev() - returns first key less than iterator's current
2728 * @iter: iterator to peek from
2730 * Returns: key if found, or an error extractable with bkey_err().
2732 struct bkey_s_c
bch2_btree_iter_prev(struct btree_iter
*iter
)
2734 if (!bch2_btree_iter_rewind(iter
))
2735 return bkey_s_c_null
;
2737 return bch2_btree_iter_peek_prev(iter
);
2740 struct bkey_s_c
bch2_btree_iter_peek_slot(struct btree_iter
*iter
)
2742 struct btree_trans
*trans
= iter
->trans
;
2743 struct bpos search_key
;
2747 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
2748 bch2_btree_iter_verify(iter
);
2749 bch2_btree_iter_verify_entry_exit(iter
);
2750 EBUG_ON(btree_iter_path(trans
, iter
)->level
&& (iter
->flags
& BTREE_ITER_with_key_cache
));
2752 /* extents can't span inode numbers: */
2753 if ((iter
->flags
& BTREE_ITER_is_extents
) &&
2754 unlikely(iter
->pos
.offset
== KEY_OFFSET_MAX
)) {
2755 if (iter
->pos
.inode
== KEY_INODE_MAX
)
2756 return bkey_s_c_null
;
2758 bch2_btree_iter_set_pos(iter
, bpos_nosnap_successor(iter
->pos
));
2761 search_key
= btree_iter_search_key(iter
);
2762 iter
->path
= bch2_btree_path_set_pos(trans
, iter
->path
, search_key
,
2763 iter
->flags
& BTREE_ITER_intent
,
2764 btree_iter_ip_allocated(iter
));
2766 ret
= bch2_btree_path_traverse(trans
, iter
->path
, iter
->flags
);
2767 if (unlikely(ret
)) {
2768 k
= bkey_s_c_err(ret
);
2772 struct btree_path
*path
= btree_iter_path(trans
, iter
);
2773 if (unlikely(!btree_path_node(path
, path
->level
)))
2774 return bkey_s_c_null
;
2776 if ((iter
->flags
& BTREE_ITER_cached
) ||
2777 !(iter
->flags
& (BTREE_ITER_is_extents
|BTREE_ITER_filter_snapshots
))) {
2780 if (unlikely((iter
->flags
& BTREE_ITER_with_updates
) &&
2781 trans
->nr_updates
)) {
2782 bch2_btree_trans_peek_slot_updates(trans
, iter
, &k
);
2787 if (unlikely(iter
->flags
& BTREE_ITER_with_journal
) &&
2788 (k
= btree_trans_peek_slot_journal(trans
, iter
)).k
)
2791 if (unlikely(iter
->flags
& BTREE_ITER_with_key_cache
) &&
2792 (k
= btree_trans_peek_key_cache(iter
, iter
->pos
)).k
) {
2795 /* We're not returning a key from iter->path: */
2799 k
= bch2_btree_path_peek_slot(trans
->paths
+ iter
->path
, &iter
->k
);
2803 if (unlikely(k
.k
->type
== KEY_TYPE_whiteout
&&
2804 (iter
->flags
& BTREE_ITER_filter_snapshots
) &&
2805 !(iter
->flags
& BTREE_ITER_key_cache_fill
)))
2806 iter
->k
.type
= KEY_TYPE_deleted
;
2809 struct bpos end
= iter
->pos
;
2811 if (iter
->flags
& BTREE_ITER_is_extents
)
2812 end
.offset
= U64_MAX
;
2814 EBUG_ON(btree_iter_path(trans
, iter
)->level
);
2816 if (iter
->flags
& BTREE_ITER_intent
) {
2817 struct btree_iter iter2
;
2819 bch2_trans_copy_iter(&iter2
, iter
);
2820 k
= bch2_btree_iter_peek_max(&iter2
, end
);
2822 if (k
.k
&& !bkey_err(k
)) {
2823 swap(iter
->key_cache_path
, iter2
.key_cache_path
);
2827 bch2_trans_iter_exit(trans
, &iter2
);
2829 struct bpos pos
= iter
->pos
;
2831 k
= bch2_btree_iter_peek_max(iter
, end
);
2832 if (unlikely(bkey_err(k
)))
2833 bch2_btree_iter_set_pos(iter
, pos
);
2838 if (unlikely(bkey_err(k
)))
2841 next
= k
.k
? bkey_start_pos(k
.k
) : POS_MAX
;
2843 if (bkey_lt(iter
->pos
, next
)) {
2844 bkey_init(&iter
->k
);
2845 iter
->k
.p
= iter
->pos
;
2847 if (iter
->flags
& BTREE_ITER_is_extents
) {
2848 bch2_key_resize(&iter
->k
,
2849 min_t(u64
, KEY_SIZE_MAX
,
2850 (next
.inode
== iter
->pos
.inode
2854 EBUG_ON(!iter
->k
.size
);
2857 k
= (struct bkey_s_c
) { &iter
->k
, NULL
};
2861 btree_path_set_should_be_locked(trans
, btree_iter_path(trans
, iter
));
2863 bch2_btree_iter_verify_entry_exit(iter
);
2864 bch2_btree_iter_verify(iter
);
2865 ret
= bch2_btree_iter_verify_ret(iter
, k
);
2867 return bkey_s_c_err(ret
);
2872 struct bkey_s_c
bch2_btree_iter_next_slot(struct btree_iter
*iter
)
2874 if (!bch2_btree_iter_advance(iter
))
2875 return bkey_s_c_null
;
2877 return bch2_btree_iter_peek_slot(iter
);
2880 struct bkey_s_c
bch2_btree_iter_prev_slot(struct btree_iter
*iter
)
2882 if (!bch2_btree_iter_rewind(iter
))
2883 return bkey_s_c_null
;
2885 return bch2_btree_iter_peek_slot(iter
);
2888 /* Obsolete, but still used by rust wrapper in -tools */
2889 struct bkey_s_c
bch2_btree_iter_peek_and_restart_outlined(struct btree_iter
*iter
)
2893 while (btree_trans_too_many_iters(iter
->trans
) ||
2894 (k
= bch2_btree_iter_peek_type(iter
, iter
->flags
),
2895 bch2_err_matches(bkey_err(k
), BCH_ERR_transaction_restart
)))
2896 bch2_trans_begin(iter
->trans
);
2901 /* new transactional stuff: */
2903 #ifdef CONFIG_BCACHEFS_DEBUG
2904 static void btree_trans_verify_sorted_refs(struct btree_trans
*trans
)
2906 struct btree_path
*path
;
2909 BUG_ON(trans
->nr_sorted
!= bitmap_weight(trans
->paths_allocated
, trans
->nr_paths
) - 1);
2911 trans_for_each_path(trans
, path
, i
) {
2912 BUG_ON(path
->sorted_idx
>= trans
->nr_sorted
);
2913 BUG_ON(trans
->sorted
[path
->sorted_idx
] != i
);
2916 for (i
= 0; i
< trans
->nr_sorted
; i
++) {
2917 unsigned idx
= trans
->sorted
[i
];
2919 BUG_ON(!test_bit(idx
, trans
->paths_allocated
));
2920 BUG_ON(trans
->paths
[idx
].sorted_idx
!= i
);
2924 static void btree_trans_verify_sorted(struct btree_trans
*trans
)
2926 struct btree_path
*path
, *prev
= NULL
;
2927 struct trans_for_each_path_inorder_iter iter
;
2929 if (!bch2_debug_check_iterators
)
2932 trans_for_each_path_inorder(trans
, path
, iter
) {
2933 if (prev
&& btree_path_cmp(prev
, path
) > 0) {
2934 __bch2_dump_trans_paths_updates(trans
, true);
2935 panic("trans paths out of order!\n");
2941 static inline void btree_trans_verify_sorted_refs(struct btree_trans
*trans
) {}
2942 static inline void btree_trans_verify_sorted(struct btree_trans
*trans
) {}
2945 void __bch2_btree_trans_sort_paths(struct btree_trans
*trans
)
2947 int i
, l
= 0, r
= trans
->nr_sorted
, inc
= 1;
2950 btree_trans_verify_sorted_refs(trans
);
2952 if (trans
->paths_sorted
)
2956 * Cocktail shaker sort: this is efficient because iterators will be
2962 for (i
= inc
> 0 ? l
: r
- 2;
2963 i
+ 1 < r
&& i
>= l
;
2965 if (btree_path_cmp(trans
->paths
+ trans
->sorted
[i
],
2966 trans
->paths
+ trans
->sorted
[i
+ 1]) > 0) {
2967 swap(trans
->sorted
[i
], trans
->sorted
[i
+ 1]);
2968 trans
->paths
[trans
->sorted
[i
]].sorted_idx
= i
;
2969 trans
->paths
[trans
->sorted
[i
+ 1]].sorted_idx
= i
+ 1;
2981 trans
->paths_sorted
= true;
2983 btree_trans_verify_sorted(trans
);
2986 static inline void btree_path_list_remove(struct btree_trans
*trans
,
2987 struct btree_path
*path
)
2989 EBUG_ON(path
->sorted_idx
>= trans
->nr_sorted
);
2990 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2992 memmove_u64s_down_small(trans
->sorted
+ path
->sorted_idx
,
2993 trans
->sorted
+ path
->sorted_idx
+ 1,
2994 DIV_ROUND_UP(trans
->nr_sorted
- path
->sorted_idx
,
2995 sizeof(u64
) / sizeof(btree_path_idx_t
)));
2997 array_remove_item(trans
->sorted
, trans
->nr_sorted
, path
->sorted_idx
);
2999 for (unsigned i
= path
->sorted_idx
; i
< trans
->nr_sorted
; i
++)
3000 trans
->paths
[trans
->sorted
[i
]].sorted_idx
= i
;
3003 static inline void btree_path_list_add(struct btree_trans
*trans
,
3004 btree_path_idx_t pos
,
3005 btree_path_idx_t path_idx
)
3007 struct btree_path
*path
= trans
->paths
+ path_idx
;
3009 path
->sorted_idx
= pos
? trans
->paths
[pos
].sorted_idx
+ 1 : trans
->nr_sorted
;
3011 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
3012 memmove_u64s_up_small(trans
->sorted
+ path
->sorted_idx
+ 1,
3013 trans
->sorted
+ path
->sorted_idx
,
3014 DIV_ROUND_UP(trans
->nr_sorted
- path
->sorted_idx
,
3015 sizeof(u64
) / sizeof(btree_path_idx_t
)));
3017 trans
->sorted
[path
->sorted_idx
] = path_idx
;
3019 array_insert_item(trans
->sorted
, trans
->nr_sorted
, path
->sorted_idx
, path_idx
);
3022 for (unsigned i
= path
->sorted_idx
; i
< trans
->nr_sorted
; i
++)
3023 trans
->paths
[trans
->sorted
[i
]].sorted_idx
= i
;
3025 btree_trans_verify_sorted_refs(trans
);
3028 void bch2_trans_iter_exit(struct btree_trans
*trans
, struct btree_iter
*iter
)
3030 if (iter
->update_path
)
3031 bch2_path_put_nokeep(trans
, iter
->update_path
,
3032 iter
->flags
& BTREE_ITER_intent
);
3034 bch2_path_put(trans
, iter
->path
,
3035 iter
->flags
& BTREE_ITER_intent
);
3036 if (iter
->key_cache_path
)
3037 bch2_path_put(trans
, iter
->key_cache_path
,
3038 iter
->flags
& BTREE_ITER_intent
);
3040 iter
->update_path
= 0;
3041 iter
->key_cache_path
= 0;
3045 void bch2_trans_iter_init_outlined(struct btree_trans
*trans
,
3046 struct btree_iter
*iter
,
3047 enum btree_id btree_id
, struct bpos pos
,
3050 bch2_trans_iter_init_common(trans
, iter
, btree_id
, pos
, 0, 0,
3051 bch2_btree_iter_flags(trans
, btree_id
, 0, flags
),
3055 void bch2_trans_node_iter_init(struct btree_trans
*trans
,
3056 struct btree_iter
*iter
,
3057 enum btree_id btree_id
,
3059 unsigned locks_want
,
3063 flags
|= BTREE_ITER_not_extents
;
3064 flags
|= BTREE_ITER_snapshot_field
;
3065 flags
|= BTREE_ITER_all_snapshots
;
3067 if (!depth
&& btree_id_cached(trans
->c
, btree_id
))
3068 flags
|= BTREE_ITER_with_key_cache
;
3070 bch2_trans_iter_init_common(trans
, iter
, btree_id
, pos
, locks_want
, depth
,
3071 bch2_btree_iter_flags(trans
, btree_id
, depth
, flags
),
3074 iter
->min_depth
= depth
;
3076 struct btree_path
*path
= btree_iter_path(trans
, iter
);
3077 BUG_ON(path
->locks_want
< min(locks_want
, BTREE_MAX_DEPTH
));
3078 BUG_ON(path
->level
!= depth
);
3079 BUG_ON(iter
->min_depth
!= depth
);
3082 void bch2_trans_copy_iter(struct btree_iter
*dst
, struct btree_iter
*src
)
3084 struct btree_trans
*trans
= src
->trans
;
3087 #ifdef TRACK_PATH_ALLOCATED
3088 dst
->ip_allocated
= _RET_IP_
;
3091 __btree_path_get(trans
, trans
->paths
+ src
->path
, src
->flags
& BTREE_ITER_intent
);
3092 if (src
->update_path
)
3093 __btree_path_get(trans
, trans
->paths
+ src
->update_path
, src
->flags
& BTREE_ITER_intent
);
3094 dst
->key_cache_path
= 0;
3097 void *__bch2_trans_kmalloc(struct btree_trans
*trans
, size_t size
)
3099 struct bch_fs
*c
= trans
->c
;
3100 unsigned new_top
= trans
->mem_top
+ size
;
3101 unsigned old_bytes
= trans
->mem_bytes
;
3102 unsigned new_bytes
= roundup_pow_of_two(new_top
);
3107 WARN_ON_ONCE(new_bytes
> BTREE_TRANS_MEM_MAX
);
3109 struct btree_transaction_stats
*s
= btree_trans_stats(trans
);
3110 s
->max_mem
= max(s
->max_mem
, new_bytes
);
3112 if (trans
->used_mempool
) {
3113 if (trans
->mem_bytes
>= new_bytes
)
3114 goto out_change_top
;
3116 /* No more space from mempool item, need malloc new one */
3117 new_mem
= kmalloc(new_bytes
, GFP_NOWAIT
|__GFP_NOWARN
);
3118 if (unlikely(!new_mem
)) {
3119 bch2_trans_unlock(trans
);
3121 new_mem
= kmalloc(new_bytes
, GFP_KERNEL
);
3123 return ERR_PTR(-BCH_ERR_ENOMEM_trans_kmalloc
);
3125 ret
= bch2_trans_relock(trans
);
3128 return ERR_PTR(ret
);
3131 memcpy(new_mem
, trans
->mem
, trans
->mem_top
);
3132 trans
->used_mempool
= false;
3133 mempool_free(trans
->mem
, &c
->btree_trans_mem_pool
);
3137 new_mem
= krealloc(trans
->mem
, new_bytes
, GFP_NOWAIT
|__GFP_NOWARN
);
3138 if (unlikely(!new_mem
)) {
3139 bch2_trans_unlock(trans
);
3141 new_mem
= krealloc(trans
->mem
, new_bytes
, GFP_KERNEL
);
3142 if (!new_mem
&& new_bytes
<= BTREE_TRANS_MEM_MAX
) {
3143 new_mem
= mempool_alloc(&c
->btree_trans_mem_pool
, GFP_KERNEL
);
3144 new_bytes
= BTREE_TRANS_MEM_MAX
;
3145 memcpy(new_mem
, trans
->mem
, trans
->mem_top
);
3146 trans
->used_mempool
= true;
3151 return ERR_PTR(-BCH_ERR_ENOMEM_trans_kmalloc
);
3153 trans
->mem
= new_mem
;
3154 trans
->mem_bytes
= new_bytes
;
3156 ret
= bch2_trans_relock(trans
);
3158 return ERR_PTR(ret
);
3161 trans
->mem
= new_mem
;
3162 trans
->mem_bytes
= new_bytes
;
3165 trace_and_count(c
, trans_restart_mem_realloced
, trans
, _RET_IP_
, new_bytes
);
3166 return ERR_PTR(btree_trans_restart(trans
, BCH_ERR_transaction_restart_mem_realloced
));
3169 p
= trans
->mem
+ trans
->mem_top
;
3170 trans
->mem_top
+= size
;
3175 static inline void check_srcu_held_too_long(struct btree_trans
*trans
)
3177 WARN(trans
->srcu_held
&& time_after(jiffies
, trans
->srcu_lock_time
+ HZ
* 10),
3178 "btree trans held srcu lock (delaying memory reclaim) for %lu seconds",
3179 (jiffies
- trans
->srcu_lock_time
) / HZ
);
3182 void bch2_trans_srcu_unlock(struct btree_trans
*trans
)
3184 if (trans
->srcu_held
) {
3185 struct bch_fs
*c
= trans
->c
;
3186 struct btree_path
*path
;
3189 trans_for_each_path(trans
, path
, i
)
3190 if (path
->cached
&& !btree_node_locked(path
, 0))
3191 path
->l
[0].b
= ERR_PTR(-BCH_ERR_no_btree_node_srcu_reset
);
3193 check_srcu_held_too_long(trans
);
3194 srcu_read_unlock(&c
->btree_trans_barrier
, trans
->srcu_idx
);
3195 trans
->srcu_held
= false;
3199 static void bch2_trans_srcu_lock(struct btree_trans
*trans
)
3201 if (!trans
->srcu_held
) {
3202 trans
->srcu_idx
= srcu_read_lock(&trans
->c
->btree_trans_barrier
);
3203 trans
->srcu_lock_time
= jiffies
;
3204 trans
->srcu_held
= true;
3209 * bch2_trans_begin() - reset a transaction after a interrupted attempt
3210 * @trans: transaction to reset
3212 * Returns: current restart counter, to be used with trans_was_restarted()
3214 * While iterating over nodes or updating nodes a attempt to lock a btree node
3215 * may return BCH_ERR_transaction_restart when the trylock fails. When this
3216 * occurs bch2_trans_begin() should be called and the transaction retried.
3218 u32
bch2_trans_begin(struct btree_trans
*trans
)
3220 struct btree_path
*path
;
3224 bch2_trans_reset_updates(trans
);
3226 trans
->restart_count
++;
3228 trans
->journal_entries
= NULL
;
3230 trans_for_each_path(trans
, path
, i
) {
3231 path
->should_be_locked
= false;
3234 * If the transaction wasn't restarted, we're presuming to be
3235 * doing something new: dont keep iterators excpt the ones that
3236 * are in use - except for the subvolumes btree:
3238 if (!trans
->restarted
&& path
->btree_id
!= BTREE_ID_subvolumes
)
3239 path
->preserve
= false;
3242 * XXX: we probably shouldn't be doing this if the transaction
3243 * was restarted, but currently we still overflow transaction
3244 * iterators if we do that
3246 if (!path
->ref
&& !path
->preserve
)
3247 __bch2_path_free(trans
, i
);
3249 path
->preserve
= false;
3252 now
= local_clock();
3254 if (!IS_ENABLED(CONFIG_BCACHEFS_NO_LATENCY_ACCT
) &&
3255 time_after64(now
, trans
->last_begin_time
+ 10))
3256 __bch2_time_stats_update(&btree_trans_stats(trans
)->duration
,
3257 trans
->last_begin_time
, now
);
3259 if (!trans
->restarted
&&
3261 time_after64(now
, trans
->last_begin_time
+ BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS
))) {
3262 bch2_trans_unlock(trans
);
3264 now
= local_clock();
3266 trans
->last_begin_time
= now
;
3268 if (unlikely(trans
->srcu_held
&&
3269 time_after(jiffies
, trans
->srcu_lock_time
+ msecs_to_jiffies(10))))
3270 bch2_trans_srcu_unlock(trans
);
3272 trans
->last_begin_ip
= _RET_IP_
;
3274 trans_set_locked(trans
, false);
3276 if (trans
->restarted
) {
3277 bch2_btree_path_traverse_all(trans
);
3278 trans
->notrace_relock_fail
= false;
3281 bch2_trans_verify_not_unlocked_or_in_restart(trans
);
3282 return trans
->restart_count
;
3285 const char *bch2_btree_transaction_fns
[BCH_TRANSACTIONS_NR
] = { "(unknown)" };
3287 unsigned bch2_trans_get_fn_idx(const char *fn
)
3289 for (unsigned i
= 0; i
< ARRAY_SIZE(bch2_btree_transaction_fns
); i
++)
3290 if (!bch2_btree_transaction_fns
[i
] ||
3291 bch2_btree_transaction_fns
[i
] == fn
) {
3292 bch2_btree_transaction_fns
[i
] = fn
;
3296 pr_warn_once("BCH_TRANSACTIONS_NR not big enough!");
3300 struct btree_trans
*__bch2_trans_get(struct bch_fs
*c
, unsigned fn_idx
)
3301 __acquires(&c
->btree_trans_barrier
)
3303 struct btree_trans
*trans
;
3305 if (IS_ENABLED(__KERNEL__
)) {
3306 trans
= this_cpu_xchg(c
->btree_trans_bufs
->trans
, NULL
);
3308 memset(trans
, 0, offsetof(struct btree_trans
, list
));
3313 trans
= mempool_alloc(&c
->btree_trans_pool
, GFP_NOFS
);
3314 memset(trans
, 0, sizeof(*trans
));
3316 seqmutex_lock(&c
->btree_trans_lock
);
3317 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG
)) {
3318 struct btree_trans
*pos
;
3319 pid_t pid
= current
->pid
;
3321 trans
->locking_wait
.task
= current
;
3323 list_for_each_entry(pos
, &c
->btree_trans_list
, list
) {
3324 struct task_struct
*pos_task
= READ_ONCE(pos
->locking_wait
.task
);
3326 * We'd much prefer to be stricter here and completely
3327 * disallow multiple btree_trans in the same thread -
3328 * but the data move path calls bch2_write when we
3329 * already have a btree_trans initialized.
3332 pid
== pos_task
->pid
&&
3337 list_add(&trans
->list
, &c
->btree_trans_list
);
3338 seqmutex_unlock(&c
->btree_trans_lock
);
3341 trans
->last_begin_time
= local_clock();
3342 trans
->fn_idx
= fn_idx
;
3343 trans
->locking_wait
.task
= current
;
3344 trans
->journal_replay_not_finished
=
3345 unlikely(!test_bit(JOURNAL_replay_done
, &c
->journal
.flags
)) &&
3346 atomic_inc_not_zero(&c
->journal_keys
.ref
);
3347 trans
->nr_paths
= ARRAY_SIZE(trans
->_paths
);
3348 trans
->paths_allocated
= trans
->_paths_allocated
;
3349 trans
->sorted
= trans
->_sorted
;
3350 trans
->paths
= trans
->_paths
;
3351 trans
->updates
= trans
->_updates
;
3353 *trans_paths_nr(trans
->paths
) = BTREE_ITER_INITIAL
;
3355 trans
->paths_allocated
[0] = 1;
3357 static struct lock_class_key lockdep_key
;
3358 lockdep_init_map(&trans
->dep_map
, "bcachefs_btree", &lockdep_key
, 0);
3360 if (fn_idx
< BCH_TRANSACTIONS_NR
) {
3361 trans
->fn
= bch2_btree_transaction_fns
[fn_idx
];
3363 struct btree_transaction_stats
*s
= &c
->btree_transaction_stats
[fn_idx
];
3366 unsigned expected_mem_bytes
= roundup_pow_of_two(s
->max_mem
);
3368 trans
->mem
= kmalloc(expected_mem_bytes
, GFP_KERNEL
);
3369 if (likely(trans
->mem
))
3370 trans
->mem_bytes
= expected_mem_bytes
;
3373 trans
->nr_paths_max
= s
->nr_max_paths
;
3374 trans
->journal_entries_size
= s
->journal_entries_size
;
3377 trans
->srcu_idx
= srcu_read_lock(&c
->btree_trans_barrier
);
3378 trans
->srcu_lock_time
= jiffies
;
3379 trans
->srcu_held
= true;
3380 trans_set_locked(trans
, false);
3382 closure_init_stack_release(&trans
->ref
);
3386 static void check_btree_paths_leaked(struct btree_trans
*trans
)
3388 #ifdef CONFIG_BCACHEFS_DEBUG
3389 struct bch_fs
*c
= trans
->c
;
3390 struct btree_path
*path
;
3393 trans_for_each_path(trans
, path
, i
)
3398 bch_err(c
, "btree paths leaked from %s!", trans
->fn
);
3399 trans_for_each_path(trans
, path
, i
)
3401 printk(KERN_ERR
" btree %s %pS\n",
3402 bch2_btree_id_str(path
->btree_id
),
3403 (void *) path
->ip_allocated
);
3404 /* Be noisy about this: */
3405 bch2_fatal_error(c
);
3409 void bch2_trans_put(struct btree_trans
*trans
)
3410 __releases(&c
->btree_trans_barrier
)
3412 struct bch_fs
*c
= trans
->c
;
3414 if (trans
->restarted
)
3415 bch2_trans_in_restart_error(trans
);
3417 bch2_trans_unlock(trans
);
3419 trans_for_each_update(trans
, i
)
3420 __btree_path_put(trans
, trans
->paths
+ i
->path
, true);
3421 trans
->nr_updates
= 0;
3423 check_btree_paths_leaked(trans
);
3425 if (trans
->srcu_held
) {
3426 check_srcu_held_too_long(trans
);
3427 srcu_read_unlock(&c
->btree_trans_barrier
, trans
->srcu_idx
);
3430 if (unlikely(trans
->journal_replay_not_finished
))
3431 bch2_journal_keys_put(c
);
3434 * trans->ref protects trans->locking_wait.task, btree_paths array; used
3437 closure_return_sync(&trans
->ref
);
3438 trans
->locking_wait
.task
= NULL
;
3440 #ifdef CONFIG_BCACHEFS_DEBUG
3441 darray_exit(&trans
->last_restarted_trace
);
3444 unsigned long *paths_allocated
= trans
->paths_allocated
;
3445 trans
->paths_allocated
= NULL
;
3446 trans
->paths
= NULL
;
3448 if (paths_allocated
!= trans
->_paths_allocated
)
3449 kvfree_rcu_mightsleep(paths_allocated
);
3451 if (trans
->used_mempool
)
3452 mempool_free(trans
->mem
, &c
->btree_trans_mem_pool
);
3456 /* Userspace doesn't have a real percpu implementation: */
3457 if (IS_ENABLED(__KERNEL__
))
3458 trans
= this_cpu_xchg(c
->btree_trans_bufs
->trans
, trans
);
3461 seqmutex_lock(&c
->btree_trans_lock
);
3462 list_del(&trans
->list
);
3463 seqmutex_unlock(&c
->btree_trans_lock
);
3465 mempool_free(trans
, &c
->btree_trans_pool
);
3469 bool bch2_current_has_btree_trans(struct bch_fs
*c
)
3471 seqmutex_lock(&c
->btree_trans_lock
);
3472 struct btree_trans
*trans
;
3474 list_for_each_entry(trans
, &c
->btree_trans_list
, list
)
3475 if (trans
->locking_wait
.task
== current
&&
3480 seqmutex_unlock(&c
->btree_trans_lock
);
3484 static void __maybe_unused
3485 bch2_btree_bkey_cached_common_to_text(struct printbuf
*out
,
3486 struct btree_bkey_cached_common
*b
)
3488 struct six_lock_count c
= six_lock_counts(&b
->lock
);
3489 struct task_struct
*owner
;
3493 owner
= READ_ONCE(b
->lock
.owner
);
3494 pid
= owner
? owner
->pid
: 0;
3497 prt_printf(out
, "\t%px %c ", b
, b
->cached
? 'c' : 'b');
3498 bch2_btree_id_to_text(out
, b
->btree_id
);
3499 prt_printf(out
, " l=%u:", b
->level
);
3500 bch2_bpos_to_text(out
, btree_node_pos(b
));
3502 prt_printf(out
, "\t locks %u:%u:%u held by pid %u",
3503 c
.n
[0], c
.n
[1], c
.n
[2], pid
);
3506 void bch2_btree_trans_to_text(struct printbuf
*out
, struct btree_trans
*trans
)
3508 struct btree_bkey_cached_common
*b
;
3509 static char lock_types
[] = { 'r', 'i', 'w' };
3510 struct task_struct
*task
= READ_ONCE(trans
->locking_wait
.task
);
3513 /* before rcu_read_lock(): */
3514 bch2_printbuf_make_room(out
, 4096);
3516 if (!out
->nr_tabstops
) {
3517 printbuf_tabstop_push(out
, 16);
3518 printbuf_tabstop_push(out
, 32);
3521 prt_printf(out
, "%i %s\n", task
? task
->pid
: 0, trans
->fn
);
3523 /* trans->paths is rcu protected vs. freeing */
3527 struct btree_path
*paths
= rcu_dereference(trans
->paths
);
3531 unsigned long *paths_allocated
= trans_paths_allocated(paths
);
3533 trans_for_each_path_idx_from(paths_allocated
, *trans_paths_nr(paths
), idx
, 1) {
3534 struct btree_path
*path
= paths
+ idx
;
3535 if (!path
->nodes_locked
)
3538 prt_printf(out
, " path %u %c ",
3540 path
->cached
? 'c' : 'b');
3541 bch2_btree_id_to_text(out
, path
->btree_id
);
3542 prt_printf(out
, " l=%u:", path
->level
);
3543 bch2_bpos_to_text(out
, path
->pos
);
3546 for (l
= 0; l
< BTREE_MAX_DEPTH
; l
++) {
3547 if (btree_node_locked(path
, l
) &&
3548 !IS_ERR_OR_NULL(b
= (void *) READ_ONCE(path
->l
[l
].b
))) {
3549 prt_printf(out
, " %c l=%u ",
3550 lock_types
[btree_node_locked_type(path
, l
)], l
);
3551 bch2_btree_bkey_cached_common_to_text(out
, b
);
3557 b
= READ_ONCE(trans
->locking
);
3559 prt_printf(out
, " blocked for %lluus on\n",
3560 div_u64(local_clock() - trans
->locking_wait
.start_time
, 1000));
3561 prt_printf(out
, " %c", lock_types
[trans
->locking_wait
.lock_want
]);
3562 bch2_btree_bkey_cached_common_to_text(out
, b
);
3570 void bch2_fs_btree_iter_exit(struct bch_fs
*c
)
3572 struct btree_transaction_stats
*s
;
3573 struct btree_trans
*trans
;
3576 if (c
->btree_trans_bufs
)
3577 for_each_possible_cpu(cpu
) {
3578 struct btree_trans
*trans
=
3579 per_cpu_ptr(c
->btree_trans_bufs
, cpu
)->trans
;
3582 seqmutex_lock(&c
->btree_trans_lock
);
3583 list_del(&trans
->list
);
3584 seqmutex_unlock(&c
->btree_trans_lock
);
3588 free_percpu(c
->btree_trans_bufs
);
3590 trans
= list_first_entry_or_null(&c
->btree_trans_list
, struct btree_trans
, list
);
3592 panic("%s leaked btree_trans\n", trans
->fn
);
3594 for (s
= c
->btree_transaction_stats
;
3595 s
< c
->btree_transaction_stats
+ ARRAY_SIZE(c
->btree_transaction_stats
);
3597 kfree(s
->max_paths_text
);
3598 bch2_time_stats_exit(&s
->lock_hold_times
);
3601 if (c
->btree_trans_barrier_initialized
) {
3602 synchronize_srcu_expedited(&c
->btree_trans_barrier
);
3603 cleanup_srcu_struct(&c
->btree_trans_barrier
);
3605 mempool_exit(&c
->btree_trans_mem_pool
);
3606 mempool_exit(&c
->btree_trans_pool
);
3609 void bch2_fs_btree_iter_init_early(struct bch_fs
*c
)
3611 struct btree_transaction_stats
*s
;
3613 for (s
= c
->btree_transaction_stats
;
3614 s
< c
->btree_transaction_stats
+ ARRAY_SIZE(c
->btree_transaction_stats
);
3616 bch2_time_stats_init(&s
->duration
);
3617 bch2_time_stats_init(&s
->lock_hold_times
);
3618 mutex_init(&s
->lock
);
3621 INIT_LIST_HEAD(&c
->btree_trans_list
);
3622 seqmutex_init(&c
->btree_trans_lock
);
3625 int bch2_fs_btree_iter_init(struct bch_fs
*c
)
3629 c
->btree_trans_bufs
= alloc_percpu(struct btree_trans_buf
);
3630 if (!c
->btree_trans_bufs
)
3633 ret
= mempool_init_kmalloc_pool(&c
->btree_trans_pool
, 1,
3634 sizeof(struct btree_trans
)) ?:
3635 mempool_init_kmalloc_pool(&c
->btree_trans_mem_pool
, 1,
3636 BTREE_TRANS_MEM_MAX
) ?:
3637 init_srcu_struct(&c
->btree_trans_barrier
);
3642 * static annotation (hackily done) for lock ordering of reclaim vs.
3645 #ifdef CONFIG_LOCKDEP
3646 fs_reclaim_acquire(GFP_KERNEL
);
3647 struct btree_trans
*trans
= bch2_trans_get(c
);
3648 trans_set_locked(trans
, false);
3649 bch2_trans_put(trans
);
3650 fs_reclaim_release(GFP_KERNEL
);
3653 c
->btree_trans_barrier_initialized
= true;