Lines Matching refs:trans

121 static void bch2_btree_path_verify_cached(struct btree_trans *trans,  in bch2_btree_path_verify_cached()  argument
127 if (!bch2_btree_node_relock(trans, path, 0)) in bch2_btree_path_verify_cached()
135 btree_node_unlock(trans, path, 0); in bch2_btree_path_verify_cached()
138 static void bch2_btree_path_verify_level(struct btree_trans *trans, in bch2_btree_path_verify_level() argument
159 bch2_btree_path_verify_cached(trans, path); in bch2_btree_path_verify_level()
166 if (!bch2_btree_node_relock_notrace(trans, path, level)) in bch2_btree_path_verify_level()
192 btree_node_unlock(trans, path, level); in bch2_btree_path_verify_level()
220 static void bch2_btree_path_verify(struct btree_trans *trans, in bch2_btree_path_verify() argument
223 struct bch_fs *c = trans->c; in bch2_btree_path_verify()
232 bch2_btree_path_verify_level(trans, path, i); in bch2_btree_path_verify()
238 void bch2_trans_verify_paths(struct btree_trans *trans) in bch2_trans_verify_paths() argument
243 trans_for_each_path(trans, path, iter) in bch2_trans_verify_paths()
244 bch2_btree_path_verify(trans, path); in bch2_trans_verify_paths()
249 struct btree_trans *trans = iter->trans; in bch2_btree_iter_verify() local
251 BUG_ON(!!(iter->flags & BTREE_ITER_cached) != btree_iter_path(trans, iter)->cached); in bch2_btree_iter_verify()
261 bch2_btree_path_verify(trans, &trans->paths[iter->update_path]); in bch2_btree_iter_verify()
262 bch2_btree_path_verify(trans, btree_iter_path(trans, iter)); in bch2_btree_iter_verify()
279 struct btree_trans *trans = iter->trans; in bch2_btree_iter_verify_ret() local
293 BUG_ON(!bch2_snapshot_is_ancestor(trans->c, in bch2_btree_iter_verify_ret()
297 bch2_trans_iter_init(trans, &copy, iter->btree_id, iter->pos, in bch2_btree_iter_verify_ret()
309 bch2_snapshot_is_ancestor(trans->c, iter->snapshot, in bch2_btree_iter_verify_ret()
323 bch2_trans_iter_exit(trans, &copy); in bch2_btree_iter_verify_ret()
327 void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id, in bch2_assert_pos_locked() argument
330 bch2_trans_verify_not_unlocked(trans); in bch2_assert_pos_locked()
336 btree_trans_sort_paths(trans); in bch2_assert_pos_locked()
338 trans_for_each_path_inorder(trans, path, iter) { in bch2_assert_pos_locked()
354 bch2_dump_trans_paths_updates(trans); in bch2_assert_pos_locked()
362 static inline void bch2_btree_path_verify_level(struct btree_trans *trans, in bch2_btree_path_verify_level() argument
364 static inline void bch2_btree_path_verify(struct btree_trans *trans, in bch2_btree_path_verify() argument
404 void bch2_btree_path_fix_key_modified(struct btree_trans *trans, in bch2_btree_path_fix_key_modified() argument
411 trans_for_each_path_with_node(trans, b, path, i) { in bch2_btree_path_fix_key_modified()
413 bch2_btree_path_verify_level(trans, path, b->c.level); in bch2_btree_path_fix_key_modified()
508 void bch2_btree_node_iter_fix(struct btree_trans *trans, in bch2_btree_node_iter_fix() argument
528 trans_for_each_path_with_node(trans, b, linked, i) { in bch2_btree_node_iter_fix()
532 bch2_btree_path_verify_level(trans, linked, b->c.level); in bch2_btree_node_iter_fix()
563 static inline struct bkey_s_c btree_path_level_peek(struct btree_trans *trans, in btree_path_level_peek() argument
568 struct bkey_s_c k = __btree_iter_unpack(trans->c, l, u, in btree_path_level_peek()
572 trans->paths_sorted = false; in btree_path_level_peek()
573 bch2_btree_path_verify_level(trans, path, l - path->l); in btree_path_level_peek()
577 static inline struct bkey_s_c btree_path_level_prev(struct btree_trans *trans, in btree_path_level_prev() argument
582 struct bkey_s_c k = __btree_iter_unpack(trans->c, l, u, in btree_path_level_prev()
586 trans->paths_sorted = false; in btree_path_level_prev()
587 bch2_btree_path_verify_level(trans, path, l - path->l); in btree_path_level_prev()
625 void bch2_btree_path_level_init(struct btree_trans *trans, in bch2_btree_path_level_init() argument
640 static void bch2_trans_revalidate_updates_in_node(struct btree_trans *trans, struct btree *b) in bch2_trans_revalidate_updates_in_node() argument
642 struct bch_fs *c = trans->c; in bch2_trans_revalidate_updates_in_node()
644 trans_for_each_update(trans, i) in bch2_trans_revalidate_updates_in_node()
650 i->old_v = bch2_btree_path_peek_slot(trans->paths + i->path, &i->old_k).v; in bch2_trans_revalidate_updates_in_node()
652 if (unlikely(trans->journal_replay_not_finished)) { in bch2_trans_revalidate_updates_in_node()
669 void bch2_trans_node_add(struct btree_trans *trans, in bch2_trans_node_add() argument
677 while ((prev = prev_btree_path(trans, path)) && in bch2_trans_node_add()
683 path = next_btree_path(trans, path)) in bch2_trans_node_add()
689 btree_node_unlock(trans, path, b->c.level); in bch2_trans_node_add()
691 mark_btree_node_locked(trans, path, b->c.level, t); in bch2_trans_node_add()
694 bch2_btree_path_level_init(trans, path, b); in bch2_trans_node_add()
697 bch2_trans_revalidate_updates_in_node(trans, b); in bch2_trans_node_add()
704 void bch2_trans_node_reinit_iter(struct btree_trans *trans, struct btree *b) in bch2_trans_node_reinit_iter() argument
709 trans_for_each_path_with_node(trans, b, path, i) in bch2_trans_node_reinit_iter()
712 bch2_trans_revalidate_updates_in_node(trans, b); in bch2_trans_node_reinit_iter()
717 static inline int btree_path_lock_root(struct btree_trans *trans, in btree_path_lock_root() argument
722 struct bch_fs *c = trans->c; in btree_path_lock_root()
748 ret = btree_node_lock(trans, path, &b->c, in btree_path_lock_root()
767 mark_btree_node_locked(trans, path, path->level, in btree_path_lock_root()
769 bch2_btree_path_level_init(trans, path, b); in btree_path_lock_root()
778 static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *path) in btree_path_prefetch() argument
780 struct bch_fs *c = trans->c; in btree_path_prefetch()
794 if (!bch2_btree_node_relock(trans, path, path->level)) in btree_path_prefetch()
803 ret = bch2_btree_node_prefetch(trans, path, tmp.k, path->btree_id, in btree_path_prefetch()
808 btree_node_unlock(trans, path, path->level); in btree_path_prefetch()
814 static int btree_path_prefetch_j(struct btree_trans *trans, struct btree_path *path, in btree_path_prefetch_j() argument
817 struct bch_fs *c = trans->c; in btree_path_prefetch_j()
829 if (!bch2_btree_node_relock(trans, path, path->level)) in btree_path_prefetch_j()
838 ret = bch2_btree_node_prefetch(trans, path, tmp.k, path->btree_id, in btree_path_prefetch_j()
843 btree_node_unlock(trans, path, path->level); in btree_path_prefetch_j()
849 static noinline void btree_node_mem_ptr_set(struct btree_trans *trans, in btree_node_mem_ptr_set() argument
858 if (!bch2_btree_node_relock(trans, path, plevel)) in btree_node_mem_ptr_set()
868 btree_node_unlock(trans, path, plevel); in btree_node_mem_ptr_set()
871 static noinline int btree_node_iter_and_journal_peek(struct btree_trans *trans, in btree_node_iter_and_journal_peek() argument
876 struct bch_fs *c = trans->c; in btree_node_iter_and_journal_peek()
882 __bch2_btree_and_journal_iter_init_node_iter(trans, &jiter, l->b, l->iter, path->pos); in btree_node_iter_and_journal_peek()
902 ret = btree_path_prefetch_j(trans, path, &jiter); in btree_node_iter_and_journal_peek()
909 static __always_inline int btree_path_down(struct btree_trans *trans, in btree_path_down() argument
914 struct bch_fs *c = trans->c; in btree_path_down()
926 if (unlikely(trans->journal_replay_not_finished)) { in btree_path_down()
927 ret = btree_node_iter_and_journal_peek(trans, path, flags, &tmp); in btree_path_down()
950 ret = btree_path_prefetch(trans, path); in btree_path_down()
956 b = bch2_btree_node_get(trans, path, tmp.k, level, lock_type, trace_ip); in btree_path_down()
961 if (likely(!trans->journal_replay_not_finished && in btree_path_down()
964 btree_node_mem_ptr_set(trans, path, level + 1, b); in btree_path_down()
967 btree_node_unlock(trans, path, level + 1); in btree_path_down()
969 mark_btree_node_locked(trans, path, level, in btree_path_down()
972 bch2_btree_path_level_init(trans, path, b); in btree_path_down()
980 static int bch2_btree_path_traverse_all(struct btree_trans *trans) in bch2_btree_path_traverse_all() argument
982 struct bch_fs *c = trans->c; in bch2_btree_path_traverse_all()
988 if (trans->in_traverse_all) in bch2_btree_path_traverse_all()
991 trans->in_traverse_all = true; in bch2_btree_path_traverse_all()
993 trans->restarted = 0; in bch2_btree_path_traverse_all()
994 trans->last_restarted_ip = 0; in bch2_btree_path_traverse_all()
996 trans_for_each_path(trans, path, i) in bch2_btree_path_traverse_all()
999 btree_trans_sort_paths(trans); in bch2_btree_path_traverse_all()
1001 bch2_trans_unlock(trans); in bch2_btree_path_traverse_all()
1003 trans_set_locked(trans); in bch2_btree_path_traverse_all()
1005 if (unlikely(trans->memory_allocation_failure)) { in bch2_btree_path_traverse_all()
1011 ret = bch2_btree_cache_cannibalize_lock(trans, &cl); in bch2_btree_path_traverse_all()
1018 while (i < trans->nr_sorted) { in bch2_btree_path_traverse_all()
1019 btree_path_idx_t idx = trans->sorted[i]; in bch2_btree_path_traverse_all()
1025 if (trans->paths[idx].uptodate) { in bch2_btree_path_traverse_all()
1026 __btree_path_get(trans, &trans->paths[idx], false); in bch2_btree_path_traverse_all()
1027 ret = bch2_btree_path_traverse_one(trans, idx, 0, _THIS_IP_); in bch2_btree_path_traverse_all()
1028 __btree_path_put(trans, &trans->paths[idx], false); in bch2_btree_path_traverse_all()
1047 bch2_btree_cache_cannibalize_unlock(trans); in bch2_btree_path_traverse_all()
1049 trans->in_traverse_all = false; in bch2_btree_path_traverse_all()
1051 trace_and_count(c, trans_traverse_all, trans, trace_ip); in bch2_btree_path_traverse_all()
1065 static inline bool btree_path_good_node(struct btree_trans *trans, in btree_path_good_node() argument
1070 bch2_btree_node_relock(trans, path, l) && in btree_path_good_node()
1074 static void btree_path_set_level_down(struct btree_trans *trans, in btree_path_set_level_down() argument
1084 btree_node_unlock(trans, path, l); in btree_path_set_level_down()
1087 bch2_btree_path_verify(trans, path); in btree_path_set_level_down()
1090 static noinline unsigned __btree_path_up_until_good_node(struct btree_trans *trans, in __btree_path_up_until_good_node() argument
1097 !btree_path_good_node(trans, path, l, check_pos)) in __btree_path_up_until_good_node()
1098 __btree_path_set_level_up(trans, path, l++); in __btree_path_up_until_good_node()
1104 if (!bch2_btree_node_relock(trans, path, i)) { in __btree_path_up_until_good_node()
1106 __btree_path_set_level_up(trans, path, l++); in __btree_path_up_until_good_node()
1113 static inline unsigned btree_path_up_until_good_node(struct btree_trans *trans, in btree_path_up_until_good_node() argument
1120 : __btree_path_up_until_good_node(trans, path, check_pos); in btree_path_up_until_good_node()
1132 int bch2_btree_path_traverse_one(struct btree_trans *trans, in bch2_btree_path_traverse_one() argument
1137 struct btree_path *path = &trans->paths[path_idx]; in bch2_btree_path_traverse_one()
1139 int ret = -((int) trans->restarted); in bch2_btree_path_traverse_one()
1144 if (unlikely(!trans->srcu_held)) in bch2_btree_path_traverse_one()
1145 bch2_trans_srcu_lock(trans); in bch2_btree_path_traverse_one()
1147 trace_btree_path_traverse_start(trans, path); in bch2_btree_path_traverse_one()
1154 ret = bch2_btree_path_relock(trans, path, trace_ip); in bch2_btree_path_traverse_one()
1159 ret = bch2_btree_path_traverse_cached(trans, path, flags); in bch2_btree_path_traverse_one()
1163 path = &trans->paths[path_idx]; in bch2_btree_path_traverse_one()
1168 path->level = btree_path_up_until_good_node(trans, path, 0); in bch2_btree_path_traverse_one()
1182 ? btree_path_down(trans, path, flags, trace_ip) in bch2_btree_path_traverse_one()
1183 : btree_path_lock_root(trans, path, depth_want, trace_ip); in bch2_btree_path_traverse_one()
1194 __bch2_btree_path_unlock(trans, path); in bch2_btree_path_traverse_one()
1205 trans_for_each_path_with_node(trans, path_l(path)->b, linked, iter) in bch2_btree_path_traverse_one()
1212 trace_btree_path_traverse_end(trans, path); in bch2_btree_path_traverse_one()
1214 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) != !!trans->restarted) in bch2_btree_path_traverse_one()
1217 bch2_err_str(trans->restarted), trans->restarted); in bch2_btree_path_traverse_one()
1218 bch2_btree_path_verify(trans, path); in bch2_btree_path_traverse_one()
1222 static inline void btree_path_copy(struct btree_trans *trans, struct btree_path *dst, in btree_path_copy() argument
1239 static btree_path_idx_t btree_path_clone(struct btree_trans *trans, btree_path_idx_t src, in btree_path_clone() argument
1242 btree_path_idx_t new = btree_path_alloc(trans, src); in btree_path_clone()
1243 btree_path_copy(trans, trans->paths + new, trans->paths + src); in btree_path_clone()
1244 __btree_path_get(trans, trans->paths + new, intent); in btree_path_clone()
1246 trans->paths[new].ip_allocated = ip; in btree_path_clone()
1252 btree_path_idx_t __bch2_btree_path_make_mut(struct btree_trans *trans, in __bch2_btree_path_make_mut() argument
1255 struct btree_path *old = trans->paths + path; in __bch2_btree_path_make_mut()
1256 __btree_path_put(trans, trans->paths + path, intent); in __bch2_btree_path_make_mut()
1257 path = btree_path_clone(trans, path, intent, ip); in __bch2_btree_path_make_mut()
1258 trace_btree_path_clone(trans, old, trans->paths + path); in __bch2_btree_path_make_mut()
1259 trans->paths[path].preserve = false; in __bch2_btree_path_make_mut()
1264 __bch2_btree_path_set_pos(struct btree_trans *trans, in __bch2_btree_path_set_pos() argument
1268 int cmp = bpos_cmp(new_pos, trans->paths[path_idx].pos); in __bch2_btree_path_set_pos()
1270 bch2_trans_verify_not_in_restart(trans); in __bch2_btree_path_set_pos()
1271 EBUG_ON(!trans->paths[path_idx].ref); in __bch2_btree_path_set_pos()
1273 trace_btree_path_set_pos(trans, trans->paths + path_idx, &new_pos); in __bch2_btree_path_set_pos()
1275 path_idx = bch2_btree_path_make_mut(trans, path_idx, intent, ip); in __bch2_btree_path_set_pos()
1277 struct btree_path *path = trans->paths + path_idx; in __bch2_btree_path_set_pos()
1279 trans->paths_sorted = false; in __bch2_btree_path_set_pos()
1282 btree_node_unlock(trans, path, 0); in __bch2_btree_path_set_pos()
1288 unsigned level = btree_path_up_until_good_node(trans, path, cmp); in __bch2_btree_path_set_pos()
1314 __bch2_btree_path_unlock(trans, path); in __bch2_btree_path_set_pos()
1317 bch2_btree_path_verify(trans, path); in __bch2_btree_path_set_pos()
1323 static struct btree_path *have_path_at_pos(struct btree_trans *trans, struct btree_path *path) in have_path_at_pos() argument
1327 sib = prev_btree_path(trans, path); in have_path_at_pos()
1331 sib = next_btree_path(trans, path); in have_path_at_pos()
1338 static struct btree_path *have_node_at_pos(struct btree_trans *trans, struct btree_path *path) in have_node_at_pos() argument
1342 sib = prev_btree_path(trans, path); in have_node_at_pos()
1346 sib = next_btree_path(trans, path); in have_node_at_pos()
1353 static inline void __bch2_path_free(struct btree_trans *trans, btree_path_idx_t path) in __bch2_path_free() argument
1355 __bch2_btree_path_unlock(trans, trans->paths + path); in __bch2_path_free()
1356 btree_path_list_remove(trans, trans->paths + path); in __bch2_path_free()
1357 __clear_bit(path, trans->paths_allocated); in __bch2_path_free()
1360 static bool bch2_btree_path_can_relock(struct btree_trans *trans, struct btree_path *path) in bch2_btree_path_can_relock() argument
1380 void bch2_path_put(struct btree_trans *trans, btree_path_idx_t path_idx, bool intent) in bch2_path_put() argument
1382 struct btree_path *path = trans->paths + path_idx, *dup; in bch2_path_put()
1384 if (!__btree_path_put(trans, path, intent)) in bch2_path_put()
1388 ? have_path_at_pos(trans, path) in bch2_path_put()
1389 : have_node_at_pos(trans, path); in bch2_path_put()
1391 trace_btree_path_free(trans, path_idx, dup); in bch2_path_put()
1396 if (path->should_be_locked && !trans->restarted) { in bch2_path_put()
1400 if (!(trans->locked in bch2_path_put()
1401 ? bch2_btree_path_relock_norestart(trans, dup) in bch2_path_put()
1402 : bch2_btree_path_can_relock(trans, dup))) in bch2_path_put()
1411 __bch2_path_free(trans, path_idx); in bch2_path_put()
1414 static void bch2_path_put_nokeep(struct btree_trans *trans, btree_path_idx_t path, in bch2_path_put_nokeep() argument
1417 if (!__btree_path_put(trans, trans->paths + path, intent)) in bch2_path_put_nokeep()
1420 __bch2_path_free(trans, path); in bch2_path_put_nokeep()
1423 void __noreturn bch2_trans_restart_error(struct btree_trans *trans, u32 restart_count) in bch2_trans_restart_error() argument
1426 trans->restart_count, restart_count, in bch2_trans_restart_error()
1427 (void *) trans->last_begin_ip); in bch2_trans_restart_error()
1430 void __noreturn bch2_trans_in_restart_error(struct btree_trans *trans) in bch2_trans_in_restart_error() argument
1433 bch2_err_str(trans->restarted), in bch2_trans_in_restart_error()
1434 (void *) trans->last_restarted_ip); in bch2_trans_in_restart_error()
1437 void __noreturn bch2_trans_unlocked_error(struct btree_trans *trans) in bch2_trans_unlocked_error() argument
1440 (void *) trans->last_unlock_ip); in bch2_trans_unlocked_error()
1444 void bch2_trans_updates_to_text(struct printbuf *buf, struct btree_trans *trans) in bch2_trans_updates_to_text() argument
1447 trans->nr_updates, trans->fn, trans->journal_res.seq); in bch2_trans_updates_to_text()
1450 trans_for_each_update(trans, i) { in bch2_trans_updates_to_text()
1459 bch2_bkey_val_to_text(buf, trans->c, old); in bch2_trans_updates_to_text()
1463 bch2_bkey_val_to_text(buf, trans->c, bkey_i_to_s_c(i->k)); in bch2_trans_updates_to_text()
1467 for (struct jset_entry *e = trans->journal_entries; in bch2_trans_updates_to_text()
1468 e != btree_trans_journal_entries_top(trans); in bch2_trans_updates_to_text()
1470 bch2_journal_entry_to_text(buf, trans->c, e); in bch2_trans_updates_to_text()
1476 void bch2_dump_trans_updates(struct btree_trans *trans) in bch2_dump_trans_updates() argument
1480 bch2_trans_updates_to_text(&buf, trans); in bch2_dump_trans_updates()
1481 bch2_print_str(trans->c, buf.buf); in bch2_dump_trans_updates()
1485 static void bch2_btree_path_to_text_short(struct printbuf *out, struct btree_trans *trans, btree_pa… in bch2_btree_path_to_text_short() argument
1487 struct btree_path *path = trans->paths + path_idx; in bch2_btree_path_to_text_short()
1527 void bch2_btree_path_to_text(struct printbuf *out, struct btree_trans *trans, btree_path_idx_t path… in bch2_btree_path_to_text() argument
1529 bch2_btree_path_to_text_short(out, trans, path_idx); in bch2_btree_path_to_text()
1531 struct btree_path *path = trans->paths + path_idx; in bch2_btree_path_to_text()
1553 void __bch2_trans_paths_to_text(struct printbuf *out, struct btree_trans *trans, in __bch2_trans_paths_to_text() argument
1559 btree_trans_sort_paths(trans); in __bch2_trans_paths_to_text()
1561 trans_for_each_path_idx_inorder(trans, iter) { in __bch2_trans_paths_to_text()
1562 bch2_btree_path_to_text_short(out, trans, iter.path_idx); in __bch2_trans_paths_to_text()
1568 void bch2_trans_paths_to_text(struct printbuf *out, struct btree_trans *trans) in bch2_trans_paths_to_text() argument
1570 __bch2_trans_paths_to_text(out, trans, false); in bch2_trans_paths_to_text()
1574 void __bch2_dump_trans_paths_updates(struct btree_trans *trans, bool nosort) in __bch2_dump_trans_paths_updates() argument
1578 __bch2_trans_paths_to_text(&buf, trans, nosort); in __bch2_dump_trans_paths_updates()
1579 bch2_trans_updates_to_text(&buf, trans); in __bch2_dump_trans_paths_updates()
1581 bch2_print_str(trans->c, buf.buf); in __bch2_dump_trans_paths_updates()
1586 void bch2_dump_trans_paths_updates(struct btree_trans *trans) in bch2_dump_trans_paths_updates() argument
1588 __bch2_dump_trans_paths_updates(trans, false); in bch2_dump_trans_paths_updates()
1592 static void bch2_trans_update_max_paths(struct btree_trans *trans) in bch2_trans_update_max_paths() argument
1594 struct btree_transaction_stats *s = btree_trans_stats(trans); in bch2_trans_update_max_paths()
1596 size_t nr = bitmap_weight(trans->paths_allocated, trans->nr_paths); in bch2_trans_update_max_paths()
1598 bch2_trans_paths_to_text(&buf, trans); in bch2_trans_update_max_paths()
1611 trans->nr_paths_max = nr; in bch2_trans_update_max_paths()
1615 int __bch2_btree_trans_too_many_iters(struct btree_trans *trans) in __bch2_btree_trans_too_many_iters() argument
1620 bch2_trans_paths_to_text(&buf, trans); in __bch2_btree_trans_too_many_iters()
1621 trace_trans_restart_too_many_iters(trans, _THIS_IP_, buf.buf); in __bch2_btree_trans_too_many_iters()
1625 count_event(trans->c, trans_restart_too_many_iters); in __bch2_btree_trans_too_many_iters()
1627 return btree_trans_restart(trans, BCH_ERR_transaction_restart_too_many_iters); in __bch2_btree_trans_too_many_iters()
1630 static noinline void btree_path_overflow(struct btree_trans *trans) in btree_path_overflow() argument
1632 bch2_dump_trans_paths_updates(trans); in btree_path_overflow()
1633 bch_err(trans->c, "trans path overflow"); in btree_path_overflow()
1636 static noinline void btree_paths_realloc(struct btree_trans *trans) in btree_paths_realloc() argument
1638 unsigned nr = trans->nr_paths * 2; in btree_paths_realloc()
1647 …memcpy(paths_allocated, trans->paths_allocated, BITS_TO_LONGS(trans->nr_paths) * sizeof(unsigned l… in btree_paths_realloc()
1653 memcpy(paths, trans->paths, trans->nr_paths * sizeof(struct btree_path)); in btree_paths_realloc()
1657 memcpy(sorted, trans->sorted, trans->nr_sorted * sizeof(btree_path_idx_t)); in btree_paths_realloc()
1661 memcpy(updates, trans->updates, trans->nr_paths * sizeof(struct btree_insert_entry)); in btree_paths_realloc()
1663 unsigned long *old = trans->paths_allocated; in btree_paths_realloc()
1665 rcu_assign_pointer(trans->paths_allocated, paths_allocated); in btree_paths_realloc()
1666 rcu_assign_pointer(trans->paths, paths); in btree_paths_realloc()
1667 rcu_assign_pointer(trans->sorted, sorted); in btree_paths_realloc()
1668 rcu_assign_pointer(trans->updates, updates); in btree_paths_realloc()
1670 trans->nr_paths = nr; in btree_paths_realloc()
1672 if (old != trans->_paths_allocated) in btree_paths_realloc()
1676 static inline btree_path_idx_t btree_path_alloc(struct btree_trans *trans, in btree_path_alloc() argument
1679 btree_path_idx_t idx = find_first_zero_bit(trans->paths_allocated, trans->nr_paths); in btree_path_alloc()
1681 if (unlikely(idx == trans->nr_paths)) { in btree_path_alloc()
1682 if (trans->nr_paths == BTREE_ITER_MAX) { in btree_path_alloc()
1683 btree_path_overflow(trans); in btree_path_alloc()
1687 btree_paths_realloc(trans); in btree_path_alloc()
1694 if (unlikely(idx > trans->nr_paths_max)) in btree_path_alloc()
1695 bch2_trans_update_max_paths(trans); in btree_path_alloc()
1697 __set_bit(idx, trans->paths_allocated); in btree_path_alloc()
1699 struct btree_path *path = &trans->paths[idx]; in btree_path_alloc()
1704 btree_path_list_add(trans, pos, idx); in btree_path_alloc()
1705 trans->paths_sorted = false; in btree_path_alloc()
1709 btree_path_idx_t bch2_path_get(struct btree_trans *trans, in bch2_path_get() argument
1720 bch2_trans_verify_not_unlocked(trans); in bch2_path_get()
1721 bch2_trans_verify_not_in_restart(trans); in bch2_path_get()
1722 bch2_trans_verify_locks(trans); in bch2_path_get()
1724 btree_trans_sort_paths(trans); in bch2_path_get()
1726 trans_for_each_path_inorder(trans, path, iter) { in bch2_path_get()
1738 trans->paths[path_pos].cached == cached && in bch2_path_get()
1739 trans->paths[path_pos].btree_id == btree_id && in bch2_path_get()
1740 trans->paths[path_pos].level == level) { in bch2_path_get()
1741 trace_btree_path_get(trans, trans->paths + path_pos, &pos); in bch2_path_get()
1743 __btree_path_get(trans, trans->paths + path_pos, intent); in bch2_path_get()
1744 path_idx = bch2_btree_path_set_pos(trans, path_pos, pos, intent, ip); in bch2_path_get()
1745 path = trans->paths + path_idx; in bch2_path_get()
1747 path_idx = btree_path_alloc(trans, path_pos); in bch2_path_get()
1748 path = trans->paths + path_idx; in bch2_path_get()
1750 __btree_path_get(trans, path, intent); in bch2_path_get()
1764 trans->paths_sorted = false; in bch2_path_get()
1766 trace_btree_path_alloc(trans, path); in bch2_path_get()
1785 bch2_btree_path_upgrade_noupgrade_sibs(trans, path, locks_want, NULL); in bch2_path_get()
1790 btree_path_idx_t bch2_path_get_unlocked_mut(struct btree_trans *trans, in bch2_path_get_unlocked_mut() argument
1795 btree_path_idx_t path_idx = bch2_path_get(trans, btree_id, pos, level + 1, level, in bch2_path_get_unlocked_mut()
1798 path_idx = bch2_btree_path_make_mut(trans, path_idx, true, _RET_IP_); in bch2_path_get_unlocked_mut()
1800 struct btree_path *path = trans->paths + path_idx; in bch2_path_get_unlocked_mut()
1801 bch2_btree_path_downgrade(trans, path); in bch2_path_get_unlocked_mut()
1802 __bch2_btree_path_unlock(trans, path); in bch2_path_get_unlocked_mut()
1849 struct btree_trans *trans = iter->trans; in bch2_set_btree_iter_dontneed() local
1851 if (!iter->path || trans->restarted) in bch2_set_btree_iter_dontneed()
1854 struct btree_path *path = btree_iter_path(trans, iter); in bch2_set_btree_iter_dontneed()
1864 return bch2_btree_path_traverse(iter->trans, iter->path, iter->flags); in __bch2_btree_iter_traverse()
1870 struct btree_trans *trans = iter->trans; in bch2_btree_iter_traverse() local
1873 bch2_trans_verify_not_unlocked(trans); in bch2_btree_iter_traverse()
1875 iter->path = bch2_btree_path_set_pos(trans, iter->path, in bch2_btree_iter_traverse()
1880 ret = bch2_btree_path_traverse(iter->trans, iter->path, iter->flags); in bch2_btree_iter_traverse()
1884 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_iter_traverse()
1886 btree_path_set_should_be_locked(trans, path); in bch2_btree_iter_traverse()
1894 struct btree_trans *trans = iter->trans; in bch2_btree_iter_peek_node() local
1898 EBUG_ON(trans->paths[iter->path].cached); in bch2_btree_iter_peek_node()
1901 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in bch2_btree_iter_peek_node()
1905 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_iter_peek_node()
1915 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p, in bch2_btree_iter_peek_node()
1918 btree_path_set_should_be_locked(trans, btree_iter_path(trans, iter)); in bch2_btree_iter_peek_node()
1936 bch2_trans_begin(iter->trans); in bch2_btree_iter_peek_node_and_restart()
1943 struct btree_trans *trans = iter->trans; in bch2_btree_iter_next_node() local
1947 EBUG_ON(trans->paths[iter->path].cached); in bch2_btree_iter_next_node()
1948 bch2_trans_verify_not_in_restart(trans); in bch2_btree_iter_next_node()
1951 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in bch2_btree_iter_next_node()
1956 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_iter_next_node()
1964 btree_path_set_level_up(trans, path); in bch2_btree_iter_next_node()
1968 if (!bch2_btree_node_relock(trans, path, path->level + 1)) { in bch2_btree_iter_next_node()
1969 __bch2_btree_path_unlock(trans, path); in bch2_btree_iter_next_node()
1973 trace_and_count(trans->c, trans_restart_relock_next_node, trans, _THIS_IP_, path); in bch2_btree_iter_next_node()
1974 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_relock); in bch2_btree_iter_next_node()
1981 __btree_path_set_level_up(trans, path, path->level++); in bch2_btree_iter_next_node()
1984 btree_node_unlock(trans, path, path->level + 1); in bch2_btree_iter_next_node()
1990 iter->path = bch2_btree_path_set_pos(trans, iter->path, in bch2_btree_iter_next_node()
1995 path = btree_iter_path(trans, iter); in bch2_btree_iter_next_node()
1996 btree_path_set_level_down(trans, path, iter->min_depth); in bch2_btree_iter_next_node()
1998 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in bch2_btree_iter_next_node()
2002 path = btree_iter_path(trans, iter); in bch2_btree_iter_next_node()
2009 iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p, in bch2_btree_iter_next_node()
2012 btree_path_set_should_be_locked(trans, btree_iter_path(trans, iter)); in bch2_btree_iter_next_node()
2013 EBUG_ON(btree_iter_path(trans, iter)->uptodate); in bch2_btree_iter_next_node()
2053 void bch2_btree_trans_peek_prev_updates(struct btree_trans *trans, struct btree_iter *iter, in bch2_btree_trans_peek_prev_updates() argument
2056 struct bpos end = path_l(btree_iter_path(trans, iter))->b->data->min_key; in bch2_btree_trans_peek_prev_updates()
2058 trans_for_each_update(trans, i) in bch2_btree_trans_peek_prev_updates()
2069 void bch2_btree_trans_peek_updates(struct btree_trans *trans, struct btree_iter *iter, in bch2_btree_trans_peek_updates() argument
2072 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_trans_peek_updates()
2075 trans_for_each_update(trans, i) in bch2_btree_trans_peek_updates()
2086 void bch2_btree_trans_peek_slot_updates(struct btree_trans *trans, struct btree_iter *iter, in bch2_btree_trans_peek_slot_updates() argument
2089 trans_for_each_update(trans, i) in bch2_btree_trans_peek_slot_updates()
2098 static struct bkey_i *bch2_btree_journal_peek(struct btree_trans *trans, in bch2_btree_journal_peek() argument
2102 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_journal_peek()
2104 return bch2_journal_keys_peek_upto(trans->c, iter->btree_id, in bch2_btree_journal_peek()
2112 struct bkey_s_c btree_trans_peek_slot_journal(struct btree_trans *trans, in btree_trans_peek_slot_journal() argument
2115 struct btree_path *path = btree_iter_path(trans, iter); in btree_trans_peek_slot_journal()
2116 struct bkey_i *k = bch2_btree_journal_peek(trans, iter, path->pos); in btree_trans_peek_slot_journal()
2127 struct bkey_s_c btree_trans_peek_journal(struct btree_trans *trans, in btree_trans_peek_journal() argument
2131 struct btree_path *path = btree_iter_path(trans, iter); in btree_trans_peek_journal()
2133 bch2_btree_journal_peek(trans, iter, in btree_trans_peek_journal()
2151 struct btree_trans *trans = iter->trans; in btree_trans_peek_key_cache() local
2152 struct bch_fs *c = trans->c; in btree_trans_peek_key_cache()
2157 bch2_trans_verify_not_in_restart(trans); in btree_trans_peek_key_cache()
2158 bch2_trans_verify_not_unlocked(trans); in btree_trans_peek_key_cache()
2168 iter->key_cache_path = bch2_path_get(trans, iter->btree_id, pos, in btree_trans_peek_key_cache()
2174 iter->key_cache_path = bch2_btree_path_set_pos(trans, iter->key_cache_path, pos, in btree_trans_peek_key_cache()
2178 ret = bch2_btree_path_traverse(trans, iter->key_cache_path, in btree_trans_peek_key_cache()
2180 bch2_btree_path_relock(trans, btree_iter_path(trans, iter), _THIS_IP_); in btree_trans_peek_key_cache()
2184 btree_path_set_should_be_locked(trans, trans->paths + iter->key_cache_path); in btree_trans_peek_key_cache()
2186 k = bch2_btree_path_peek_slot(trans->paths + iter->key_cache_path, &u); in btree_trans_peek_key_cache()
2196 struct btree_trans *trans = iter->trans; in __bch2_btree_iter_peek() local
2200 EBUG_ON(btree_iter_path(trans, iter)->cached); in __bch2_btree_iter_peek()
2206 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, in __bch2_btree_iter_peek()
2210 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in __bch2_btree_iter_peek()
2218 struct btree_path *path = btree_iter_path(trans, iter); in __bch2_btree_iter_peek()
2228 btree_path_set_should_be_locked(trans, path); in __bch2_btree_iter_peek()
2230 k = btree_path_level_peek_all(trans->c, l, &iter->k); in __bch2_btree_iter_peek()
2244 k = btree_trans_peek_journal(trans, iter, k); in __bch2_btree_iter_peek()
2247 trans->nr_updates)) in __bch2_btree_iter_peek()
2248 bch2_btree_trans_peek_updates(trans, iter, &k); in __bch2_btree_iter_peek()
2292 struct btree_trans *trans = iter->trans; in bch2_btree_iter_peek_upto() local
2298 bch2_trans_verify_not_unlocked(trans); in bch2_btree_iter_peek_upto()
2302 bch2_path_put_nokeep(trans, iter->update_path, in bch2_btree_iter_peek_upto()
2332 !bkey_eq(trans->paths[iter->update_path].pos, k.k->p)) { in bch2_btree_iter_peek_upto()
2333 bch2_path_put_nokeep(trans, iter->update_path, in bch2_btree_iter_peek_upto()
2355 __btree_path_get(trans, trans->paths + iter->path, iter->flags & BTREE_ITER_intent); in bch2_btree_iter_peek_upto()
2358 iter->update_path = bch2_btree_path_set_pos(trans, in bch2_btree_iter_peek_upto()
2362 ret = bch2_btree_path_traverse(trans, iter->update_path, iter->flags); in bch2_btree_iter_peek_upto()
2374 !bch2_snapshot_is_ancestor(trans->c, in bch2_btree_iter_peek_upto()
2407 iter->path = bch2_btree_path_set_pos(trans, iter->path, k.k->p, in bch2_btree_iter_peek_upto()
2411 btree_path_set_should_be_locked(trans, btree_iter_path(trans, iter)); in bch2_btree_iter_peek_upto()
2414 ret = bch2_btree_path_relock(trans, trans->paths + iter->update_path, _THIS_IP_); in bch2_btree_iter_peek_upto()
2418 btree_path_set_should_be_locked(trans, trans->paths + iter->update_path); in bch2_btree_iter_peek_upto()
2463 struct btree_trans *trans = iter->trans; in bch2_btree_iter_peek_prev() local
2471 bch2_trans_verify_not_unlocked(trans); in bch2_btree_iter_peek_prev()
2472 EBUG_ON(btree_iter_path(trans, iter)->cached || in bch2_btree_iter_peek_prev()
2473 btree_iter_path(trans, iter)->level); in bch2_btree_iter_peek_prev()
2485 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, in bch2_btree_iter_peek_prev()
2489 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in bch2_btree_iter_peek_prev()
2497 struct btree_path *path = btree_iter_path(trans, iter); in bch2_btree_iter_peek_prev()
2499 k = btree_path_level_peek(trans, path, &path->l[0], &iter->k); in bch2_btree_iter_peek_prev()
2504 k = btree_path_level_prev(trans, path, &path->l[0], &iter->k); in bch2_btree_iter_peek_prev()
2507 trans->nr_updates)) in bch2_btree_iter_peek_prev()
2508 bch2_btree_trans_peek_prev_updates(trans, iter, &k); in bch2_btree_iter_peek_prev()
2521 bch2_path_put_nokeep(trans, iter->path, in bch2_btree_iter_peek_prev()
2530 if (bch2_snapshot_is_ancestor(trans->c, in bch2_btree_iter_peek_prev()
2534 bch2_path_put_nokeep(trans, saved_path, in bch2_btree_iter_peek_prev()
2536 saved_path = btree_path_clone(trans, iter->path, in bch2_btree_iter_peek_prev()
2539 path = btree_iter_path(trans, iter); in bch2_btree_iter_peek_prev()
2540 trace_btree_path_save_pos(trans, path, trans->paths + saved_path); in bch2_btree_iter_peek_prev()
2557 btree_path_set_should_be_locked(trans, path); in bch2_btree_iter_peek_prev()
2580 bch2_path_put_nokeep(trans, saved_path, iter->flags & BTREE_ITER_intent); in bch2_btree_iter_peek_prev()
2605 struct btree_trans *trans = iter->trans; in bch2_btree_iter_peek_slot() local
2610 bch2_trans_verify_not_unlocked(trans); in bch2_btree_iter_peek_slot()
2613 EBUG_ON(btree_iter_path(trans, iter)->level && (iter->flags & BTREE_ITER_with_key_cache)); in bch2_btree_iter_peek_slot()
2625 iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, in bch2_btree_iter_peek_slot()
2629 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); in bch2_btree_iter_peek_slot()
2640 trans->nr_updates)) { in bch2_btree_iter_peek_slot()
2641 bch2_btree_trans_peek_slot_updates(trans, iter, &k); in bch2_btree_iter_peek_slot()
2647 (k = btree_trans_peek_slot_journal(trans, iter)).k) in bch2_btree_iter_peek_slot()
2658 k = bch2_btree_path_peek_slot(trans->paths + iter->path, &iter->k); in bch2_btree_iter_peek_slot()
2668 EBUG_ON(btree_iter_path(trans, iter)->level); in bch2_btree_iter_peek_slot()
2681 bch2_trans_iter_exit(trans, &iter2); in bch2_btree_iter_peek_slot()
2715 btree_path_set_should_be_locked(trans, btree_iter_path(trans, iter)); in bch2_btree_iter_peek_slot()
2747 while (btree_trans_too_many_iters(iter->trans) || in bch2_btree_iter_peek_and_restart_outlined()
2750 bch2_trans_begin(iter->trans); in bch2_btree_iter_peek_and_restart_outlined()
2758 static void btree_trans_verify_sorted_refs(struct btree_trans *trans) in btree_trans_verify_sorted_refs() argument
2763 BUG_ON(trans->nr_sorted != bitmap_weight(trans->paths_allocated, trans->nr_paths) - 1); in btree_trans_verify_sorted_refs()
2765 trans_for_each_path(trans, path, i) { in btree_trans_verify_sorted_refs()
2766 BUG_ON(path->sorted_idx >= trans->nr_sorted); in btree_trans_verify_sorted_refs()
2767 BUG_ON(trans->sorted[path->sorted_idx] != i); in btree_trans_verify_sorted_refs()
2770 for (i = 0; i < trans->nr_sorted; i++) { in btree_trans_verify_sorted_refs()
2771 unsigned idx = trans->sorted[i]; in btree_trans_verify_sorted_refs()
2773 BUG_ON(!test_bit(idx, trans->paths_allocated)); in btree_trans_verify_sorted_refs()
2774 BUG_ON(trans->paths[idx].sorted_idx != i); in btree_trans_verify_sorted_refs()
2778 static void btree_trans_verify_sorted(struct btree_trans *trans) in btree_trans_verify_sorted() argument
2786 trans_for_each_path_inorder(trans, path, iter) { in btree_trans_verify_sorted()
2788 __bch2_dump_trans_paths_updates(trans, true); in btree_trans_verify_sorted()
2795 static inline void btree_trans_verify_sorted_refs(struct btree_trans *trans) {} in btree_trans_verify_sorted_refs() argument
2796 static inline void btree_trans_verify_sorted(struct btree_trans *trans) {} in btree_trans_verify_sorted() argument
2799 void __bch2_btree_trans_sort_paths(struct btree_trans *trans) in __bch2_btree_trans_sort_paths() argument
2801 int i, l = 0, r = trans->nr_sorted, inc = 1; in __bch2_btree_trans_sort_paths()
2804 btree_trans_verify_sorted_refs(trans); in __bch2_btree_trans_sort_paths()
2806 if (trans->paths_sorted) in __bch2_btree_trans_sort_paths()
2819 if (btree_path_cmp(trans->paths + trans->sorted[i], in __bch2_btree_trans_sort_paths()
2820 trans->paths + trans->sorted[i + 1]) > 0) { in __bch2_btree_trans_sort_paths()
2821 swap(trans->sorted[i], trans->sorted[i + 1]); in __bch2_btree_trans_sort_paths()
2822 trans->paths[trans->sorted[i]].sorted_idx = i; in __bch2_btree_trans_sort_paths()
2823 trans->paths[trans->sorted[i + 1]].sorted_idx = i + 1; in __bch2_btree_trans_sort_paths()
2835 trans->paths_sorted = true; in __bch2_btree_trans_sort_paths()
2837 btree_trans_verify_sorted(trans); in __bch2_btree_trans_sort_paths()
2840 static inline void btree_path_list_remove(struct btree_trans *trans, in btree_path_list_remove() argument
2843 EBUG_ON(path->sorted_idx >= trans->nr_sorted); in btree_path_list_remove()
2845 trans->nr_sorted--; in btree_path_list_remove()
2846 memmove_u64s_down_small(trans->sorted + path->sorted_idx, in btree_path_list_remove()
2847 trans->sorted + path->sorted_idx + 1, in btree_path_list_remove()
2848 DIV_ROUND_UP(trans->nr_sorted - path->sorted_idx, in btree_path_list_remove()
2851 array_remove_item(trans->sorted, trans->nr_sorted, path->sorted_idx); in btree_path_list_remove()
2853 for (unsigned i = path->sorted_idx; i < trans->nr_sorted; i++) in btree_path_list_remove()
2854 trans->paths[trans->sorted[i]].sorted_idx = i; in btree_path_list_remove()
2857 static inline void btree_path_list_add(struct btree_trans *trans, in btree_path_list_add() argument
2861 struct btree_path *path = trans->paths + path_idx; in btree_path_list_add()
2863 path->sorted_idx = pos ? trans->paths[pos].sorted_idx + 1 : trans->nr_sorted; in btree_path_list_add()
2866 memmove_u64s_up_small(trans->sorted + path->sorted_idx + 1, in btree_path_list_add()
2867 trans->sorted + path->sorted_idx, in btree_path_list_add()
2868 DIV_ROUND_UP(trans->nr_sorted - path->sorted_idx, in btree_path_list_add()
2870 trans->nr_sorted++; in btree_path_list_add()
2871 trans->sorted[path->sorted_idx] = path_idx; in btree_path_list_add()
2873 array_insert_item(trans->sorted, trans->nr_sorted, path->sorted_idx, path_idx); in btree_path_list_add()
2876 for (unsigned i = path->sorted_idx; i < trans->nr_sorted; i++) in btree_path_list_add()
2877 trans->paths[trans->sorted[i]].sorted_idx = i; in btree_path_list_add()
2879 btree_trans_verify_sorted_refs(trans); in btree_path_list_add()
2882 void bch2_trans_iter_exit(struct btree_trans *trans, struct btree_iter *iter) in bch2_trans_iter_exit() argument
2885 bch2_path_put_nokeep(trans, iter->update_path, in bch2_trans_iter_exit()
2888 bch2_path_put(trans, iter->path, in bch2_trans_iter_exit()
2891 bch2_path_put(trans, iter->key_cache_path, in bch2_trans_iter_exit()
2896 iter->trans = NULL; in bch2_trans_iter_exit()
2899 void bch2_trans_iter_init_outlined(struct btree_trans *trans, in bch2_trans_iter_init_outlined() argument
2904 bch2_trans_iter_init_common(trans, iter, btree_id, pos, 0, 0, in bch2_trans_iter_init_outlined()
2905 bch2_btree_iter_flags(trans, btree_id, flags), in bch2_trans_iter_init_outlined()
2909 void bch2_trans_node_iter_init(struct btree_trans *trans, in bch2_trans_node_iter_init() argument
2921 bch2_trans_iter_init_common(trans, iter, btree_id, pos, locks_want, depth, in bch2_trans_node_iter_init()
2922 __bch2_btree_iter_flags(trans, btree_id, flags), in bch2_trans_node_iter_init()
2927 struct btree_path *path = btree_iter_path(trans, iter); in bch2_trans_node_iter_init()
2935 struct btree_trans *trans = src->trans; in bch2_trans_copy_iter() local
2942 __btree_path_get(trans, trans->paths + src->path, src->flags & BTREE_ITER_intent); in bch2_trans_copy_iter()
2944 __btree_path_get(trans, trans->paths + src->update_path, src->flags & BTREE_ITER_intent); in bch2_trans_copy_iter()
2948 void *__bch2_trans_kmalloc(struct btree_trans *trans, size_t size) in __bch2_trans_kmalloc() argument
2950 struct bch_fs *c = trans->c; in __bch2_trans_kmalloc()
2951 unsigned new_top = trans->mem_top + size; in __bch2_trans_kmalloc()
2952 unsigned old_bytes = trans->mem_bytes; in __bch2_trans_kmalloc()
2960 struct btree_transaction_stats *s = btree_trans_stats(trans); in __bch2_trans_kmalloc()
2963 if (trans->used_mempool) { in __bch2_trans_kmalloc()
2964 if (trans->mem_bytes >= new_bytes) in __bch2_trans_kmalloc()
2970 bch2_trans_unlock(trans); in __bch2_trans_kmalloc()
2976 ret = bch2_trans_relock(trans); in __bch2_trans_kmalloc()
2982 memcpy(new_mem, trans->mem, trans->mem_top); in __bch2_trans_kmalloc()
2983 trans->used_mempool = false; in __bch2_trans_kmalloc()
2984 mempool_free(trans->mem, &c->btree_trans_mem_pool); in __bch2_trans_kmalloc()
2988 new_mem = krealloc(trans->mem, new_bytes, GFP_NOWAIT|__GFP_NOWARN); in __bch2_trans_kmalloc()
2990 bch2_trans_unlock(trans); in __bch2_trans_kmalloc()
2992 new_mem = krealloc(trans->mem, new_bytes, GFP_KERNEL); in __bch2_trans_kmalloc()
2996 memcpy(new_mem, trans->mem, trans->mem_top); in __bch2_trans_kmalloc()
2997 trans->used_mempool = true; in __bch2_trans_kmalloc()
2998 kfree(trans->mem); in __bch2_trans_kmalloc()
3004 trans->mem = new_mem; in __bch2_trans_kmalloc()
3005 trans->mem_bytes = new_bytes; in __bch2_trans_kmalloc()
3007 ret = bch2_trans_relock(trans); in __bch2_trans_kmalloc()
3012 trans->mem = new_mem; in __bch2_trans_kmalloc()
3013 trans->mem_bytes = new_bytes; in __bch2_trans_kmalloc()
3016 trace_and_count(c, trans_restart_mem_realloced, trans, _RET_IP_, new_bytes); in __bch2_trans_kmalloc()
3017 return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_mem_realloced)); in __bch2_trans_kmalloc()
3020 p = trans->mem + trans->mem_top; in __bch2_trans_kmalloc()
3021 trans->mem_top += size; in __bch2_trans_kmalloc()
3026 static inline void check_srcu_held_too_long(struct btree_trans *trans) in check_srcu_held_too_long() argument
3028 WARN(trans->srcu_held && time_after(jiffies, trans->srcu_lock_time + HZ * 10), in check_srcu_held_too_long()
3030 (jiffies - trans->srcu_lock_time) / HZ); in check_srcu_held_too_long()
3033 void bch2_trans_srcu_unlock(struct btree_trans *trans) in bch2_trans_srcu_unlock() argument
3035 if (trans->srcu_held) { in bch2_trans_srcu_unlock()
3036 struct bch_fs *c = trans->c; in bch2_trans_srcu_unlock()
3040 trans_for_each_path(trans, path, i) in bch2_trans_srcu_unlock()
3044 check_srcu_held_too_long(trans); in bch2_trans_srcu_unlock()
3045 srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx); in bch2_trans_srcu_unlock()
3046 trans->srcu_held = false; in bch2_trans_srcu_unlock()
3050 static void bch2_trans_srcu_lock(struct btree_trans *trans) in bch2_trans_srcu_lock() argument
3052 if (!trans->srcu_held) { in bch2_trans_srcu_lock()
3053 trans->srcu_idx = srcu_read_lock(&trans->c->btree_trans_barrier); in bch2_trans_srcu_lock()
3054 trans->srcu_lock_time = jiffies; in bch2_trans_srcu_lock()
3055 trans->srcu_held = true; in bch2_trans_srcu_lock()
3069 u32 bch2_trans_begin(struct btree_trans *trans) in bch2_trans_begin() argument
3075 bch2_trans_reset_updates(trans); in bch2_trans_begin()
3077 trans->restart_count++; in bch2_trans_begin()
3078 trans->mem_top = 0; in bch2_trans_begin()
3079 trans->journal_entries = NULL; in bch2_trans_begin()
3081 trans_for_each_path(trans, path, i) { in bch2_trans_begin()
3089 if (!trans->restarted && path->btree_id != BTREE_ID_subvolumes) in bch2_trans_begin()
3098 __bch2_path_free(trans, i); in bch2_trans_begin()
3106 time_after64(now, trans->last_begin_time + 10)) in bch2_trans_begin()
3107 __bch2_time_stats_update(&btree_trans_stats(trans)->duration, in bch2_trans_begin()
3108 trans->last_begin_time, now); in bch2_trans_begin()
3110 if (!trans->restarted && in bch2_trans_begin()
3112 time_after64(now, trans->last_begin_time + BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS))) { in bch2_trans_begin()
3113 bch2_trans_unlock(trans); in bch2_trans_begin()
3117 trans->last_begin_time = now; in bch2_trans_begin()
3119 if (unlikely(trans->srcu_held && in bch2_trans_begin()
3120 time_after(jiffies, trans->srcu_lock_time + msecs_to_jiffies(10)))) in bch2_trans_begin()
3121 bch2_trans_srcu_unlock(trans); in bch2_trans_begin()
3123 trans->last_begin_ip = _RET_IP_; in bch2_trans_begin()
3125 trans_set_locked(trans); in bch2_trans_begin()
3127 if (trans->restarted) { in bch2_trans_begin()
3128 bch2_btree_path_traverse_all(trans); in bch2_trans_begin()
3129 trans->notrace_relock_fail = false; in bch2_trans_begin()
3132 bch2_trans_verify_not_unlocked(trans); in bch2_trans_begin()
3133 return trans->restart_count; in bch2_trans_begin()
3154 struct btree_trans *trans; in __bch2_trans_get() local
3157 trans = this_cpu_xchg(c->btree_trans_bufs->trans, NULL); in __bch2_trans_get()
3158 if (trans) { in __bch2_trans_get()
3159 memset(trans, 0, offsetof(struct btree_trans, list)); in __bch2_trans_get()
3164 trans = mempool_alloc(&c->btree_trans_pool, GFP_NOFS); in __bch2_trans_get()
3165 memset(trans, 0, sizeof(*trans)); in __bch2_trans_get()
3172 trans->locking_wait.task = current; in __bch2_trans_get()
3188 list_add(&trans->list, &c->btree_trans_list); in __bch2_trans_get()
3191 trans->c = c; in __bch2_trans_get()
3192 trans->last_begin_time = local_clock(); in __bch2_trans_get()
3193 trans->fn_idx = fn_idx; in __bch2_trans_get()
3194 trans->locking_wait.task = current; in __bch2_trans_get()
3195 trans->journal_replay_not_finished = in __bch2_trans_get()
3198 trans->nr_paths = ARRAY_SIZE(trans->_paths); in __bch2_trans_get()
3199 trans->paths_allocated = trans->_paths_allocated; in __bch2_trans_get()
3200 trans->sorted = trans->_sorted; in __bch2_trans_get()
3201 trans->paths = trans->_paths; in __bch2_trans_get()
3202 trans->updates = trans->_updates; in __bch2_trans_get()
3204 *trans_paths_nr(trans->paths) = BTREE_ITER_INITIAL; in __bch2_trans_get()
3206 trans->paths_allocated[0] = 1; in __bch2_trans_get()
3209 lockdep_init_map(&trans->dep_map, "bcachefs_btree", &lockdep_key, 0); in __bch2_trans_get()
3212 trans->fn = bch2_btree_transaction_fns[fn_idx]; in __bch2_trans_get()
3219 trans->mem = kmalloc(expected_mem_bytes, GFP_KERNEL); in __bch2_trans_get()
3220 if (likely(trans->mem)) in __bch2_trans_get()
3221 trans->mem_bytes = expected_mem_bytes; in __bch2_trans_get()
3224 trans->nr_paths_max = s->nr_max_paths; in __bch2_trans_get()
3225 trans->journal_entries_size = s->journal_entries_size; in __bch2_trans_get()
3228 trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier); in __bch2_trans_get()
3229 trans->srcu_lock_time = jiffies; in __bch2_trans_get()
3230 trans->srcu_held = true; in __bch2_trans_get()
3231 trans_set_locked(trans); in __bch2_trans_get()
3233 closure_init_stack_release(&trans->ref); in __bch2_trans_get()
3234 return trans; in __bch2_trans_get()
3237 static void check_btree_paths_leaked(struct btree_trans *trans) in check_btree_paths_leaked() argument
3240 struct bch_fs *c = trans->c; in check_btree_paths_leaked()
3244 trans_for_each_path(trans, path, i) in check_btree_paths_leaked()
3249 bch_err(c, "btree paths leaked from %s!", trans->fn); in check_btree_paths_leaked()
3250 trans_for_each_path(trans, path, i) in check_btree_paths_leaked()
3260 void bch2_trans_put(struct btree_trans *trans) in bch2_trans_put() argument
3263 struct bch_fs *c = trans->c; in bch2_trans_put()
3265 bch2_trans_unlock(trans); in bch2_trans_put()
3267 trans_for_each_update(trans, i) in bch2_trans_put()
3268 __btree_path_put(trans, trans->paths + i->path, true); in bch2_trans_put()
3269 trans->nr_updates = 0; in bch2_trans_put()
3271 check_btree_paths_leaked(trans); in bch2_trans_put()
3273 if (trans->srcu_held) { in bch2_trans_put()
3274 check_srcu_held_too_long(trans); in bch2_trans_put()
3275 srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx); in bch2_trans_put()
3278 if (unlikely(trans->journal_replay_not_finished)) in bch2_trans_put()
3285 closure_return_sync(&trans->ref); in bch2_trans_put()
3286 trans->locking_wait.task = NULL; in bch2_trans_put()
3288 unsigned long *paths_allocated = trans->paths_allocated; in bch2_trans_put()
3289 trans->paths_allocated = NULL; in bch2_trans_put()
3290 trans->paths = NULL; in bch2_trans_put()
3292 if (paths_allocated != trans->_paths_allocated) in bch2_trans_put()
3295 if (trans->used_mempool) in bch2_trans_put()
3296 mempool_free(trans->mem, &c->btree_trans_mem_pool); in bch2_trans_put()
3298 kfree(trans->mem); in bch2_trans_put()
3302 trans = this_cpu_xchg(c->btree_trans_bufs->trans, trans); in bch2_trans_put()
3304 if (trans) { in bch2_trans_put()
3306 list_del(&trans->list); in bch2_trans_put()
3309 mempool_free(trans, &c->btree_trans_pool); in bch2_trans_put()
3316 struct btree_trans *trans; in bch2_current_has_btree_trans() local
3318 list_for_each_entry(trans, &c->btree_trans_list, list) in bch2_current_has_btree_trans()
3319 if (trans->locking_wait.task == current && in bch2_current_has_btree_trans()
3320 trans->locked) { in bch2_current_has_btree_trans()
3349 void bch2_btree_trans_to_text(struct printbuf *out, struct btree_trans *trans) in bch2_btree_trans_to_text() argument
3353 struct task_struct *task = READ_ONCE(trans->locking_wait.task); in bch2_btree_trans_to_text()
3364 prt_printf(out, "%i %s\n", task ? task->pid : 0, trans->fn); in bch2_btree_trans_to_text()
3370 struct btree_path *paths = rcu_dereference(trans->paths); in bch2_btree_trans_to_text()
3400 b = READ_ONCE(trans->locking); in bch2_btree_trans_to_text()
3403 div_u64(local_clock() - trans->locking_wait.start_time, 1000)); in bch2_btree_trans_to_text()
3404 prt_printf(out, " %c", lock_types[trans->locking_wait.lock_want]); in bch2_btree_trans_to_text()
3416 struct btree_trans *trans; in bch2_fs_btree_iter_exit() local
3421 struct btree_trans *trans = in bch2_fs_btree_iter_exit() local
3422 per_cpu_ptr(c->btree_trans_bufs, cpu)->trans; in bch2_fs_btree_iter_exit()
3424 if (trans) { in bch2_fs_btree_iter_exit()
3426 list_del(&trans->list); in bch2_fs_btree_iter_exit()
3429 kfree(trans); in bch2_fs_btree_iter_exit()
3433 trans = list_first_entry_or_null(&c->btree_trans_list, struct btree_trans, list); in bch2_fs_btree_iter_exit()
3434 if (trans) in bch2_fs_btree_iter_exit()
3435 panic("%s leaked btree_trans\n", trans->fn); in bch2_fs_btree_iter_exit()
3490 struct btree_trans *trans = bch2_trans_get(c); in bch2_fs_btree_iter_init() local
3491 trans_set_locked(trans); in bch2_fs_btree_iter_init()
3492 bch2_trans_put(trans); in bch2_fs_btree_iter_init()