2017-03-17 06:18:50 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_BTREE_ITER_H
|
|
|
|
#define _BCACHEFS_BTREE_ITER_H
|
|
|
|
|
2019-01-13 21:02:22 +00:00
|
|
|
#include "bset.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "btree_types.h"
|
2022-07-05 21:27:44 +00:00
|
|
|
#include "trace.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline void __btree_path_get(struct btree_path *path, bool intent)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
path->ref++;
|
|
|
|
path->intent_ref += intent;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline bool __btree_path_put(struct btree_path *path, bool intent)
|
|
|
|
{
|
|
|
|
EBUG_ON(!path->ref);
|
|
|
|
EBUG_ON(!path->intent_ref && intent);
|
|
|
|
path->intent_ref -= intent;
|
|
|
|
return --path->ref == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void btree_path_set_dirty(struct btree_path *path,
|
|
|
|
enum btree_path_uptodate u)
|
|
|
|
{
|
|
|
|
path->uptodate = max_t(unsigned, path->uptodate, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btree *btree_path_node(struct btree_path *path,
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned level)
|
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
return level < BTREE_MAX_DEPTH ? path->l[level].b : NULL;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline bool btree_node_lock_seq_matches(const struct btree_path *path,
|
2020-06-06 16:28:01 +00:00
|
|
|
const struct btree *b, unsigned level)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We don't compare the low bits of the lock sequence numbers because
|
2021-08-30 19:18:31 +00:00
|
|
|
* @path might have taken a write lock on @b, and we don't want to skip
|
|
|
|
* the linked path if the sequence numbers were equal before taking that
|
|
|
|
* write lock. The lock sequence number is incremented by taking and
|
|
|
|
* releasing write locks and is even when unlocked:
|
2020-06-06 16:28:01 +00:00
|
|
|
*/
|
2021-08-30 19:18:31 +00:00
|
|
|
return path->l[level].lock_seq >> 1 == b->c.lock.state.seq >> 1;
|
2020-06-06 16:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree *btree_node_parent(struct btree_path *path,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree *b)
|
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
return btree_path_node(path, b->c.level + 1);
|
2019-03-28 02:03:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
/* Iterate over paths within a transaction: */
|
2019-03-28 02:03:30 +00:00
|
|
|
|
2021-09-03 21:18:57 +00:00
|
|
|
void __bch2_btree_trans_sort_paths(struct btree_trans *);
|
|
|
|
|
|
|
|
static inline void btree_trans_sort_paths(struct btree_trans *trans)
|
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
|
|
|
|
trans->paths_sorted)
|
|
|
|
return;
|
|
|
|
__bch2_btree_trans_sort_paths(trans);
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path *
|
|
|
|
__trans_next_path(struct btree_trans *trans, unsigned idx)
|
2019-03-28 02:03:30 +00:00
|
|
|
{
|
2020-12-09 18:34:42 +00:00
|
|
|
u64 l;
|
|
|
|
|
|
|
|
if (idx == BTREE_ITER_MAX)
|
|
|
|
return NULL;
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
l = trans->paths_allocated >> idx;
|
2020-12-02 04:11:53 +00:00
|
|
|
if (!l)
|
|
|
|
return NULL;
|
2019-03-28 02:03:30 +00:00
|
|
|
|
2020-12-02 04:11:53 +00:00
|
|
|
idx += __ffs64(l);
|
2020-12-09 18:34:42 +00:00
|
|
|
EBUG_ON(idx >= BTREE_ITER_MAX);
|
2021-08-30 19:18:31 +00:00
|
|
|
EBUG_ON(trans->paths[idx].idx != idx);
|
|
|
|
return &trans->paths[idx];
|
2019-03-28 02:03:30 +00:00
|
|
|
}
|
|
|
|
|
bcachefs: Deadlock cycle detector
We've outgrown our own deadlock avoidance strategy.
The btree iterator API provides an interface where the user doesn't need
to concern themselves with lock ordering - different btree iterators can
be traversed in any order. Without special care, this will lead to
deadlocks.
Our previous strategy was to define a lock ordering internally, and
whenever we attempt to take a lock and trylock() fails, we'd check if
the current btree transaction is holding any locks that cause a lock
ordering violation. If so, we'd issue a transaction restart, and then
bch2_trans_begin() would re-traverse all previously used iterators, but
in the correct order.
That approach had some issues, though.
- Sometimes we'd issue transaction restarts unnecessarily, when no
deadlock would have actually occured. Lock ordering restarts have
become our primary cause of transaction restarts, on some workloads
totally 20% of actual transaction commits.
- To avoid deadlock or livelock, we'd often have to take intent locks
when we only wanted a read lock: with the lock ordering approach, it
is actually illegal to hold _any_ read lock while blocking on an intent
lock, and this has been causing us unnecessary lock contention.
- It was getting fragile - the various lock ordering rules are not
trivial, and we'd been seeing occasional livelock issues related to
this machinery.
So, since bcachefs is already a relational database masquerading as a
filesystem, we're stealing the next traditional database technique and
switching to a cycle detector for avoiding deadlocks.
When we block taking a btree lock, after adding ourself to the waitlist
but before sleeping, we do a DFS of btree transactions waiting on other
btree transactions, starting with the current transaction and walking
our held locks, and transactions blocking on our held locks.
If we find a cycle, we emit a transaction restart. Occasionally (e.g.
the btree split path) we can not allow the lock() operation to fail, so
if necessary we'll tell another transaction that it has to fail.
Result: trans_restart_would_deadlock events are reduced by a factor of
10 to 100, and we'll be able to delete a whole bunch of grotty, fragile
code.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-08-22 17:23:47 +00:00
|
|
|
#define trans_for_each_path_from(_trans, _path, _start) \
|
|
|
|
for (_path = __trans_next_path((_trans), _start); \
|
2021-08-30 19:18:31 +00:00
|
|
|
(_path); \
|
|
|
|
_path = __trans_next_path((_trans), (_path)->idx + 1))
|
2019-03-28 02:03:30 +00:00
|
|
|
|
bcachefs: Deadlock cycle detector
We've outgrown our own deadlock avoidance strategy.
The btree iterator API provides an interface where the user doesn't need
to concern themselves with lock ordering - different btree iterators can
be traversed in any order. Without special care, this will lead to
deadlocks.
Our previous strategy was to define a lock ordering internally, and
whenever we attempt to take a lock and trylock() fails, we'd check if
the current btree transaction is holding any locks that cause a lock
ordering violation. If so, we'd issue a transaction restart, and then
bch2_trans_begin() would re-traverse all previously used iterators, but
in the correct order.
That approach had some issues, though.
- Sometimes we'd issue transaction restarts unnecessarily, when no
deadlock would have actually occured. Lock ordering restarts have
become our primary cause of transaction restarts, on some workloads
totally 20% of actual transaction commits.
- To avoid deadlock or livelock, we'd often have to take intent locks
when we only wanted a read lock: with the lock ordering approach, it
is actually illegal to hold _any_ read lock while blocking on an intent
lock, and this has been causing us unnecessary lock contention.
- It was getting fragile - the various lock ordering rules are not
trivial, and we'd been seeing occasional livelock issues related to
this machinery.
So, since bcachefs is already a relational database masquerading as a
filesystem, we're stealing the next traditional database technique and
switching to a cycle detector for avoiding deadlocks.
When we block taking a btree lock, after adding ourself to the waitlist
but before sleeping, we do a DFS of btree transactions waiting on other
btree transactions, starting with the current transaction and walking
our held locks, and transactions blocking on our held locks.
If we find a cycle, we emit a transaction restart. Occasionally (e.g.
the btree split path) we can not allow the lock() operation to fail, so
if necessary we'll tell another transaction that it has to fail.
Result: trans_restart_would_deadlock events are reduced by a factor of
10 to 100, and we'll be able to delete a whole bunch of grotty, fragile
code.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-08-22 17:23:47 +00:00
|
|
|
#define trans_for_each_path(_trans, _path) \
|
|
|
|
trans_for_each_path_from(_trans, _path, 0)
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path *next_btree_path(struct btree_trans *trans, struct btree_path *path)
|
2021-06-12 19:45:45 +00:00
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
unsigned idx = path ? path->sorted_idx + 1 : 0;
|
2021-06-12 19:45:45 +00:00
|
|
|
|
|
|
|
EBUG_ON(idx > trans->nr_sorted);
|
|
|
|
|
|
|
|
return idx < trans->nr_sorted
|
2021-08-30 19:18:31 +00:00
|
|
|
? trans->paths + trans->sorted[idx]
|
2021-06-12 19:45:45 +00:00
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path *prev_btree_path(struct btree_trans *trans, struct btree_path *path)
|
2021-06-12 19:45:45 +00:00
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
unsigned idx = path ? path->sorted_idx : trans->nr_sorted;
|
2021-06-12 19:45:45 +00:00
|
|
|
|
|
|
|
return idx
|
2021-08-30 19:18:31 +00:00
|
|
|
? trans->paths + trans->sorted[idx - 1]
|
2021-06-12 19:45:45 +00:00
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
#define trans_for_each_path_inorder(_trans, _path, _i) \
|
2021-06-12 19:45:45 +00:00
|
|
|
for (_i = 0; \
|
2021-08-30 19:18:31 +00:00
|
|
|
((_path) = (_trans)->paths + trans->sorted[_i]), (_i) < (_trans)->nr_sorted;\
|
2021-06-12 19:45:45 +00:00
|
|
|
_i++)
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
#define trans_for_each_path_inorder_reverse(_trans, _path, _i) \
|
2021-06-12 19:45:45 +00:00
|
|
|
for (_i = trans->nr_sorted - 1; \
|
2021-08-30 19:18:31 +00:00
|
|
|
((_path) = (_trans)->paths + trans->sorted[_i]), (_i) >= 0;\
|
2021-06-12 19:45:45 +00:00
|
|
|
--_i)
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline bool __path_has_node(const struct btree_path *path,
|
2017-03-17 06:18:50 +00:00
|
|
|
const struct btree *b)
|
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
return path->l[b->c.level].b == b &&
|
|
|
|
btree_node_lock_seq_matches(path, b, b->c.level);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path *
|
|
|
|
__trans_next_path_with_node(struct btree_trans *trans, struct btree *b,
|
2019-03-28 03:14:38 +00:00
|
|
|
unsigned idx)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_path *path = __trans_next_path(trans, idx);
|
|
|
|
|
|
|
|
while (path && !__path_has_node(path, b))
|
|
|
|
path = __trans_next_path(trans, path->idx + 1);
|
2019-03-28 02:03:30 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define trans_for_each_path_with_node(_trans, _b, _path) \
|
|
|
|
for (_path = __trans_next_path_with_node((_trans), (_b), 0); \
|
|
|
|
(_path); \
|
|
|
|
_path = __trans_next_path_with_node((_trans), (_b), \
|
|
|
|
(_path)->idx + 1))
|
|
|
|
|
|
|
|
struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *,
|
|
|
|
struct btree_path *, bool);
|
2019-03-28 02:03:30 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path * __must_check
|
|
|
|
bch2_btree_path_make_mut(struct btree_trans *trans,
|
|
|
|
struct btree_path *path, bool intent)
|
|
|
|
{
|
|
|
|
if (path->ref > 1 || path->preserve)
|
|
|
|
path = __bch2_btree_path_make_mut(trans, path, intent);
|
2022-03-30 17:47:07 +00:00
|
|
|
path->should_be_locked = false;
|
2021-08-30 19:18:31 +00:00
|
|
|
return path;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 03:21:44 +00:00
|
|
|
struct btree_path * __must_check
|
|
|
|
__bch2_btree_path_set_pos(struct btree_trans *, struct btree_path *,
|
|
|
|
struct bpos, bool, int);
|
|
|
|
|
|
|
|
static inline struct btree_path * __must_check
|
|
|
|
bch2_btree_path_set_pos(struct btree_trans *trans,
|
|
|
|
struct btree_path *path, struct bpos new_pos,
|
|
|
|
bool intent)
|
|
|
|
{
|
|
|
|
int cmp = bpos_cmp(new_pos, path->pos);
|
|
|
|
|
|
|
|
return cmp
|
|
|
|
? __bch2_btree_path_set_pos(trans, path, new_pos, intent, cmp)
|
|
|
|
: path;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
int __must_check bch2_btree_path_traverse(struct btree_trans *,
|
|
|
|
struct btree_path *, unsigned);
|
2021-12-22 01:48:26 +00:00
|
|
|
struct btree_path *bch2_path_get(struct btree_trans *, enum btree_id, struct bpos,
|
|
|
|
unsigned, unsigned, unsigned);
|
2022-11-14 01:01:42 +00:00
|
|
|
struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *, struct bkey *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-05-21 17:10:39 +00:00
|
|
|
struct bkey_i *bch2_btree_journal_peek_slot(struct btree_trans *,
|
|
|
|
struct btree_iter *, struct bpos);
|
|
|
|
|
2022-11-14 01:01:42 +00:00
|
|
|
void bch2_btree_path_level_init(struct btree_trans *, struct btree_path *, struct btree *);
|
2022-09-16 18:42:38 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
#ifdef CONFIG_BCACHEFS_DEBUG
|
2021-08-30 19:18:31 +00:00
|
|
|
void bch2_trans_verify_paths(struct btree_trans *);
|
2021-11-06 04:03:40 +00:00
|
|
|
void bch2_assert_pos_locked(struct btree_trans *, enum btree_id,
|
|
|
|
struct bpos, bool);
|
2017-03-17 06:18:50 +00:00
|
|
|
#else
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline void bch2_trans_verify_paths(struct btree_trans *trans) {}
|
2021-11-06 04:03:40 +00:00
|
|
|
static inline void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
|
|
|
|
struct bpos pos, bool key_cache) {}
|
2017-03-17 06:18:50 +00:00
|
|
|
#endif
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
|
2021-08-25 01:30:06 +00:00
|
|
|
struct btree *, struct bkey_packed *);
|
2021-08-30 19:18:31 +00:00
|
|
|
void bch2_btree_node_iter_fix(struct btree_trans *trans, struct btree_path *,
|
2021-08-25 01:30:06 +00:00
|
|
|
struct btree *, struct btree_node_iter *,
|
|
|
|
struct bkey_packed *, unsigned, unsigned);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-07-18 03:06:38 +00:00
|
|
|
int bch2_btree_path_relock_intent(struct btree_trans *, struct btree_path *);
|
2021-08-30 19:18:31 +00:00
|
|
|
|
|
|
|
void bch2_path_put(struct btree_trans *, struct btree_path *, bool);
|
2021-07-14 19:13:27 +00:00
|
|
|
|
2022-07-18 03:06:38 +00:00
|
|
|
int bch2_trans_relock(struct btree_trans *);
|
2019-05-15 13:47:40 +00:00
|
|
|
void bch2_trans_unlock(struct btree_trans *);
|
2022-10-03 20:39:49 +00:00
|
|
|
bool bch2_trans_locked(struct btree_trans *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-07-18 03:06:38 +00:00
|
|
|
static inline bool trans_was_restarted(struct btree_trans *trans, u32 restart_count)
|
2022-07-17 23:35:38 +00:00
|
|
|
{
|
2022-07-18 03:06:38 +00:00
|
|
|
return restart_count != trans->restart_count;
|
2022-07-17 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_trans_verify_not_restarted(struct btree_trans *, u32);
|
|
|
|
|
2021-07-25 21:19:52 +00:00
|
|
|
__always_inline
|
2022-07-18 03:06:38 +00:00
|
|
|
static inline int btree_trans_restart_nounlock(struct btree_trans *trans, int err)
|
2021-07-25 21:19:52 +00:00
|
|
|
{
|
2022-07-18 03:06:38 +00:00
|
|
|
BUG_ON(err <= 0);
|
|
|
|
BUG_ON(!bch2_err_matches(err, BCH_ERR_transaction_restart));
|
|
|
|
|
|
|
|
trans->restarted = err;
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
__always_inline
|
|
|
|
static inline int btree_trans_restart(struct btree_trans *trans, int err)
|
|
|
|
{
|
|
|
|
btree_trans_restart_nounlock(trans, err);
|
|
|
|
return -err;
|
2021-07-25 21:19:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 16:08:02 +00:00
|
|
|
bool bch2_btree_node_upgrade(struct btree_trans *,
|
|
|
|
struct btree_path *, unsigned);
|
|
|
|
|
2022-07-14 06:58:23 +00:00
|
|
|
void __bch2_btree_path_downgrade(struct btree_trans *, struct btree_path *, unsigned);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-07-14 06:58:23 +00:00
|
|
|
static inline void bch2_btree_path_downgrade(struct btree_trans *trans,
|
|
|
|
struct btree_path *path)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2021-08-30 19:18:31 +00:00
|
|
|
unsigned new_locks_want = path->level + !!path->intent_ref;
|
2021-04-03 01:29:05 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
if (path->locks_want > new_locks_want)
|
2022-07-14 06:58:23 +00:00
|
|
|
__bch2_btree_path_downgrade(trans, path, new_locks_want);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:26:48 +00:00
|
|
|
void bch2_trans_downgrade(struct btree_trans *);
|
|
|
|
|
2021-08-30 18:36:03 +00:00
|
|
|
void bch2_trans_node_add(struct btree_trans *trans, struct btree *);
|
|
|
|
void bch2_trans_node_reinit_iter(struct btree_trans *, struct btree *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-09-07 19:34:16 +00:00
|
|
|
int __must_check __bch2_btree_iter_traverse(struct btree_iter *iter);
|
2021-03-24 01:22:50 +00:00
|
|
|
int __must_check bch2_btree_iter_traverse(struct btree_iter *);
|
2019-09-08 18:00:12 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree *bch2_btree_iter_peek_node(struct btree_iter *);
|
2020-02-18 21:17:55 +00:00
|
|
|
struct btree *bch2_btree_iter_next_node(struct btree_iter *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-03-11 17:31:52 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *, struct bpos);
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_next(struct btree_iter *);
|
2019-09-07 21:17:21 +00:00
|
|
|
|
2022-04-12 22:04:08 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_peek_all_levels(struct btree_iter *);
|
|
|
|
|
2022-03-11 17:31:52 +00:00
|
|
|
static inline struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter)
|
|
|
|
{
|
|
|
|
return bch2_btree_iter_peek_upto(iter, SPOS_MAX);
|
|
|
|
}
|
|
|
|
|
2019-09-07 21:17:21 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *);
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *);
|
|
|
|
|
|
|
|
struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *);
|
|
|
|
struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *);
|
2021-03-03 03:45:28 +00:00
|
|
|
struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-03-21 20:55:25 +00:00
|
|
|
bool bch2_btree_iter_advance(struct btree_iter *);
|
|
|
|
bool bch2_btree_iter_rewind(struct btree_iter *);
|
2021-03-22 01:16:52 +00:00
|
|
|
|
2022-01-09 06:07:29 +00:00
|
|
|
static inline void __bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
|
2021-03-22 01:16:52 +00:00
|
|
|
{
|
2021-04-04 01:09:13 +00:00
|
|
|
iter->k.type = KEY_TYPE_deleted;
|
|
|
|
iter->k.p.inode = iter->pos.inode = new_pos.inode;
|
|
|
|
iter->k.p.offset = iter->pos.offset = new_pos.offset;
|
|
|
|
iter->k.p.snapshot = iter->pos.snapshot = new_pos.snapshot;
|
|
|
|
iter->k.size = 0;
|
2021-03-22 01:16:52 +00:00
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-01-09 06:07:29 +00:00
|
|
|
static inline void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
|
|
|
|
{
|
2022-01-09 02:22:31 +00:00
|
|
|
if (unlikely(iter->update_path))
|
|
|
|
bch2_path_put(iter->trans, iter->update_path,
|
|
|
|
iter->flags & BTREE_ITER_INTENT);
|
|
|
|
iter->update_path = NULL;
|
|
|
|
|
2022-01-09 06:07:29 +00:00
|
|
|
if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
|
|
|
|
new_pos.snapshot = iter->snapshot;
|
|
|
|
|
|
|
|
__bch2_btree_iter_set_pos(iter, new_pos);
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:16:10 +00:00
|
|
|
static inline void bch2_btree_iter_set_pos_to_extent_start(struct btree_iter *iter)
|
|
|
|
{
|
|
|
|
BUG_ON(!(iter->flags & BTREE_ITER_IS_EXTENTS));
|
|
|
|
iter->pos = bkey_start_pos(&iter->k);
|
|
|
|
}
|
|
|
|
|
2021-03-05 03:29:25 +00:00
|
|
|
static inline void bch2_btree_iter_set_snapshot(struct btree_iter *iter, u32 snapshot)
|
|
|
|
{
|
|
|
|
struct bpos pos = iter->pos;
|
|
|
|
|
|
|
|
iter->snapshot = snapshot;
|
|
|
|
pos.snapshot = snapshot;
|
|
|
|
bch2_btree_iter_set_pos(iter, pos);
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
void bch2_trans_iter_exit(struct btree_trans *, struct btree_iter *);
|
2022-11-25 05:40:27 +00:00
|
|
|
|
|
|
|
static inline unsigned __bch2_btree_iter_flags(struct btree_trans *trans,
|
|
|
|
unsigned btree_id,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
if (flags & BTREE_ITER_ALL_LEVELS)
|
|
|
|
flags |= BTREE_ITER_ALL_SNAPSHOTS|__BTREE_ITER_ALL_SNAPSHOTS;
|
|
|
|
|
|
|
|
if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) &&
|
|
|
|
btree_node_type_is_extents(btree_id))
|
|
|
|
flags |= BTREE_ITER_IS_EXTENTS;
|
|
|
|
|
|
|
|
if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
|
|
|
|
!btree_type_has_snapshots(btree_id))
|
|
|
|
flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
|
|
|
|
|
|
|
|
if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) &&
|
|
|
|
btree_type_has_snapshots(btree_id))
|
|
|
|
flags |= BTREE_ITER_FILTER_SNAPSHOTS;
|
|
|
|
|
|
|
|
if (trans->journal_replay_not_finished)
|
|
|
|
flags |= BTREE_ITER_WITH_JOURNAL;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned bch2_btree_iter_flags(struct btree_trans *trans,
|
|
|
|
unsigned btree_id,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
if (!btree_id_cached(trans->c, btree_id)) {
|
|
|
|
flags &= ~BTREE_ITER_CACHED;
|
|
|
|
flags &= ~BTREE_ITER_WITH_KEY_CACHE;
|
|
|
|
} else if (!(flags & BTREE_ITER_CACHED))
|
|
|
|
flags |= BTREE_ITER_WITH_KEY_CACHE;
|
|
|
|
|
|
|
|
return __bch2_btree_iter_flags(trans, btree_id, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bch2_trans_iter_init_common(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
unsigned btree_id, struct bpos pos,
|
|
|
|
unsigned locks_want,
|
|
|
|
unsigned depth,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
memset(iter, 0, sizeof(*iter));
|
|
|
|
iter->trans = trans;
|
|
|
|
iter->btree_id = btree_id;
|
|
|
|
iter->flags = flags;
|
|
|
|
iter->snapshot = pos.snapshot;
|
|
|
|
iter->pos = pos;
|
|
|
|
iter->k.p = pos;
|
|
|
|
|
|
|
|
iter->path = bch2_path_get(trans, btree_id, iter->pos,
|
|
|
|
locks_want, depth, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_trans_iter_init_outlined(struct btree_trans *, struct btree_iter *,
|
|
|
|
enum btree_id, struct bpos, unsigned);
|
|
|
|
|
|
|
|
static inline void bch2_trans_iter_init(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
unsigned btree_id, struct bpos pos,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
if (__builtin_constant_p(btree_id) &&
|
|
|
|
__builtin_constant_p(flags))
|
|
|
|
bch2_trans_iter_init_common(trans, iter, btree_id, pos, 0, 0,
|
|
|
|
bch2_btree_iter_flags(trans, btree_id, flags));
|
|
|
|
else
|
|
|
|
bch2_trans_iter_init_outlined(trans, iter, btree_id, pos, flags);
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
void bch2_trans_node_iter_init(struct btree_trans *, struct btree_iter *,
|
|
|
|
enum btree_id, struct bpos,
|
|
|
|
unsigned, unsigned, unsigned);
|
|
|
|
void bch2_trans_copy_iter(struct btree_iter *, struct btree_iter *);
|
|
|
|
|
|
|
|
static inline void set_btree_iter_dontneed(struct btree_iter *iter)
|
|
|
|
{
|
2022-08-05 21:08:35 +00:00
|
|
|
if (!iter->trans->restarted)
|
|
|
|
iter->path->preserve = false;
|
2021-10-21 16:05:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:15:17 +00:00
|
|
|
void *__bch2_trans_kmalloc(struct btree_trans *, size_t);
|
|
|
|
|
|
|
|
static inline void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
|
|
|
|
{
|
2022-11-24 03:13:19 +00:00
|
|
|
size = roundup(size, 8);
|
|
|
|
|
|
|
|
if (likely(trans->mem_top + size <= trans->mem_bytes)) {
|
|
|
|
void *p = trans->mem + trans->mem_top;
|
2022-09-26 20:15:17 +00:00
|
|
|
|
|
|
|
trans->mem_top += size;
|
|
|
|
memset(p, 0, size);
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return __bch2_trans_kmalloc(trans, size);
|
2022-11-24 03:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *bch2_trans_kmalloc_nomemzero(struct btree_trans *trans, size_t size)
|
|
|
|
{
|
|
|
|
size = roundup(size, 8);
|
2022-09-26 20:15:17 +00:00
|
|
|
|
2022-11-24 03:13:19 +00:00
|
|
|
if (likely(trans->mem_top + size <= trans->mem_bytes)) {
|
|
|
|
void *p = trans->mem + trans->mem_top;
|
|
|
|
|
|
|
|
trans->mem_top += size;
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return __bch2_trans_kmalloc(trans, size);
|
2022-09-26 20:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 03:13:19 +00:00
|
|
|
static inline struct bkey_i *bch2_bkey_make_mut(struct btree_trans *trans, struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
struct bkey_i *mut = bch2_trans_kmalloc_nomemzero(trans, bkey_bytes(k.k));
|
|
|
|
|
|
|
|
if (!IS_ERR(mut))
|
|
|
|
bkey_reassemble(mut, k);
|
|
|
|
return mut;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bkey_i *bch2_bkey_get_mut(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter)
|
|
|
|
{
|
|
|
|
struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
|
|
|
|
|
|
|
|
return unlikely(IS_ERR(k.k))
|
|
|
|
? ERR_CAST(k.k)
|
|
|
|
: bch2_bkey_make_mut(trans, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define bch2_bkey_get_mut_typed(_trans, _iter, _type) \
|
|
|
|
({ \
|
|
|
|
struct bkey_i *_k = bch2_bkey_get_mut(_trans, _iter); \
|
|
|
|
struct bkey_i_##_type *_ret; \
|
|
|
|
\
|
|
|
|
if (IS_ERR(_k)) \
|
|
|
|
_ret = ERR_CAST(_k); \
|
|
|
|
else if (unlikely(_k->k.type != KEY_TYPE_##_type)) \
|
|
|
|
_ret = ERR_PTR(-ENOENT); \
|
|
|
|
else \
|
|
|
|
_ret = bkey_i_to_##_type(_k); \
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define bch2_bkey_alloc(_trans, _iter, _type) \
|
|
|
|
({ \
|
|
|
|
struct bkey_i_##_type *_k = bch2_trans_kmalloc_nomemzero(_trans, sizeof(*_k));\
|
|
|
|
if (!IS_ERR(_k)) { \
|
|
|
|
bkey_##_type##_init(&_k->k_i); \
|
|
|
|
_k->k.p = (_iter)->pos; \
|
|
|
|
} \
|
|
|
|
_k; \
|
|
|
|
})
|
|
|
|
|
2022-07-17 23:35:38 +00:00
|
|
|
u32 bch2_trans_begin(struct btree_trans *);
|
2021-10-21 16:05:21 +00:00
|
|
|
|
|
|
|
static inline struct btree *
|
|
|
|
__btree_iter_peek_node_and_restart(struct btree_trans *trans, struct btree_iter *iter)
|
|
|
|
{
|
|
|
|
struct btree *b;
|
|
|
|
|
|
|
|
while (b = bch2_btree_iter_peek_node(iter),
|
2022-07-18 03:06:38 +00:00
|
|
|
bch2_err_matches(PTR_ERR_OR_ZERO(b), BCH_ERR_transaction_restart))
|
2021-10-21 16:05:21 +00:00
|
|
|
bch2_trans_begin(trans);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2022-11-16 03:48:03 +00:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* this does not handle transaction restarts from bch2_btree_iter_next_node()
|
|
|
|
* correctly
|
|
|
|
*/
|
2021-10-19 18:20:50 +00:00
|
|
|
#define __for_each_btree_node(_trans, _iter, _btree_id, _start, \
|
|
|
|
_locks_want, _depth, _flags, _b, _ret) \
|
2021-08-30 19:18:31 +00:00
|
|
|
for (bch2_trans_node_iter_init((_trans), &(_iter), (_btree_id), \
|
2021-10-21 16:05:21 +00:00
|
|
|
_start, _locks_want, _depth, _flags); \
|
|
|
|
(_b) = __btree_iter_peek_node_and_restart((_trans), &(_iter)),\
|
2021-10-19 18:20:50 +00:00
|
|
|
!((_ret) = PTR_ERR_OR_ZERO(_b)) && (_b); \
|
2021-08-30 19:18:31 +00:00
|
|
|
(_b) = bch2_btree_iter_next_node(&(_iter)))
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-03-25 19:10:15 +00:00
|
|
|
#define for_each_btree_node(_trans, _iter, _btree_id, _start, \
|
2021-10-19 18:20:50 +00:00
|
|
|
_flags, _b, _ret) \
|
2019-03-25 19:10:15 +00:00
|
|
|
__for_each_btree_node(_trans, _iter, _btree_id, _start, \
|
2021-10-19 18:20:50 +00:00
|
|
|
0, 0, _flags, _b, _ret)
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
static inline int bkey_err(struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
return PTR_ERR_OR_ZERO(k.k);
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:13:27 +00:00
|
|
|
static inline struct bkey_s_c bch2_btree_iter_peek_prev_type(struct btree_iter *iter,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
BUG_ON(flags & BTREE_ITER_ALL_LEVELS);
|
|
|
|
|
|
|
|
return flags & BTREE_ITER_SLOTS ? bch2_btree_iter_peek_slot(iter) :
|
|
|
|
bch2_btree_iter_peek_prev(iter);
|
|
|
|
}
|
|
|
|
|
2022-01-09 06:07:29 +00:00
|
|
|
static inline struct bkey_s_c bch2_btree_iter_peek_type(struct btree_iter *iter,
|
2022-03-11 17:31:52 +00:00
|
|
|
unsigned flags)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2022-04-12 22:04:08 +00:00
|
|
|
return flags & BTREE_ITER_ALL_LEVELS ? bch2_btree_iter_peek_all_levels(iter) :
|
|
|
|
flags & BTREE_ITER_SLOTS ? bch2_btree_iter_peek_slot(iter) :
|
|
|
|
bch2_btree_iter_peek(iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 17:31:52 +00:00
|
|
|
static inline struct bkey_s_c bch2_btree_iter_peek_upto_type(struct btree_iter *iter,
|
|
|
|
struct bpos end,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
if (!(flags & BTREE_ITER_SLOTS))
|
|
|
|
return bch2_btree_iter_peek_upto(iter, end);
|
|
|
|
|
2022-11-24 08:12:22 +00:00
|
|
|
if (bkey_gt(iter->pos, end))
|
2022-03-11 17:31:52 +00:00
|
|
|
return bkey_s_c_null;
|
|
|
|
|
|
|
|
return bch2_btree_iter_peek_slot(iter);
|
|
|
|
}
|
|
|
|
|
2021-11-24 00:00:23 +00:00
|
|
|
static inline int btree_trans_too_many_iters(struct btree_trans *trans)
|
|
|
|
{
|
2022-08-27 14:30:36 +00:00
|
|
|
if (hweight64(trans->paths_allocated) > BTREE_ITER_MAX - 8) {
|
2022-08-27 16:48:36 +00:00
|
|
|
trace_and_count(trans->c, trans_restart_too_many_iters, trans, _THIS_IP_);
|
2022-07-18 03:06:38 +00:00
|
|
|
return btree_trans_restart(trans, BCH_ERR_transaction_restart_too_many_iters);
|
2022-07-05 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-11-24 00:00:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
static inline struct bkey_s_c
|
|
|
|
__bch2_btree_iter_peek_and_restart(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter, unsigned flags)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2021-10-21 16:05:21 +00:00
|
|
|
struct bkey_s_c k;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-11-24 00:00:23 +00:00
|
|
|
while (btree_trans_too_many_iters(trans) ||
|
2022-01-09 06:07:29 +00:00
|
|
|
(k = bch2_btree_iter_peek_type(iter, flags),
|
2022-07-18 03:06:38 +00:00
|
|
|
bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart)))
|
2021-10-21 16:05:21 +00:00
|
|
|
bch2_trans_begin(trans);
|
|
|
|
|
|
|
|
return k;
|
2019-09-25 19:57:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 23:35:38 +00:00
|
|
|
#define lockrestart_do(_trans, _do) \
|
|
|
|
({ \
|
2022-07-07 04:37:46 +00:00
|
|
|
u32 _restart_count; \
|
2022-07-17 23:35:38 +00:00
|
|
|
int _ret; \
|
|
|
|
\
|
|
|
|
do { \
|
2022-07-07 04:37:46 +00:00
|
|
|
_restart_count = bch2_trans_begin(_trans); \
|
2022-07-17 23:35:38 +00:00
|
|
|
_ret = (_do); \
|
2022-07-18 03:06:38 +00:00
|
|
|
} while (bch2_err_matches(_ret, BCH_ERR_transaction_restart)); \
|
2022-07-17 23:35:38 +00:00
|
|
|
\
|
2022-07-07 04:37:46 +00:00
|
|
|
if (!_ret) \
|
|
|
|
bch2_trans_verify_not_restarted(_trans, _restart_count);\
|
|
|
|
\
|
2022-07-17 23:35:38 +00:00
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nested_lockrestart_do(), nested_commit_do():
|
|
|
|
*
|
|
|
|
* These are like lockrestart_do() and commit_do(), with two differences:
|
|
|
|
*
|
|
|
|
* - We don't call bch2_trans_begin() unless we had a transaction restart
|
2022-07-18 03:06:38 +00:00
|
|
|
* - We return -BCH_ERR_transaction_restart_nested if we succeeded after a
|
|
|
|
* transaction restart
|
2022-07-17 23:35:38 +00:00
|
|
|
*/
|
|
|
|
#define nested_lockrestart_do(_trans, _do) \
|
|
|
|
({ \
|
|
|
|
u32 _restart_count, _orig_restart_count; \
|
|
|
|
int _ret; \
|
|
|
|
\
|
|
|
|
_restart_count = _orig_restart_count = (_trans)->restart_count; \
|
|
|
|
\
|
2022-07-18 03:06:38 +00:00
|
|
|
while (bch2_err_matches(_ret = (_do), BCH_ERR_transaction_restart))\
|
2022-07-17 23:35:38 +00:00
|
|
|
_restart_count = bch2_trans_begin(_trans); \
|
|
|
|
\
|
|
|
|
if (!_ret) \
|
|
|
|
bch2_trans_verify_not_restarted(_trans, _restart_count);\
|
|
|
|
\
|
2022-07-18 03:06:38 +00:00
|
|
|
if (!_ret && trans_was_restarted(_trans, _orig_restart_count)) \
|
|
|
|
_ret = -BCH_ERR_transaction_restart_nested; \
|
|
|
|
\
|
|
|
|
_ret; \
|
2022-07-17 23:35:38 +00:00
|
|
|
})
|
|
|
|
|
2022-07-16 00:51:09 +00:00
|
|
|
#define for_each_btree_key2(_trans, _iter, _btree_id, \
|
|
|
|
_start, _flags, _k, _do) \
|
|
|
|
({ \
|
|
|
|
int _ret = 0; \
|
|
|
|
\
|
|
|
|
bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \
|
|
|
|
(_start), (_flags)); \
|
|
|
|
\
|
2022-07-18 03:06:38 +00:00
|
|
|
while (1) { \
|
2022-07-07 04:37:46 +00:00
|
|
|
u32 _restart_count = bch2_trans_begin(_trans); \
|
2022-11-16 04:17:55 +00:00
|
|
|
\
|
|
|
|
_ret = 0; \
|
2022-07-16 00:51:09 +00:00
|
|
|
(_k) = bch2_btree_iter_peek_type(&(_iter), (_flags)); \
|
2022-11-16 04:17:55 +00:00
|
|
|
if (!(_k).k) \
|
2022-07-16 00:51:09 +00:00
|
|
|
break; \
|
|
|
|
\
|
|
|
|
_ret = bkey_err(_k) ?: (_do); \
|
2022-07-18 03:06:38 +00:00
|
|
|
if (bch2_err_matches(_ret, BCH_ERR_transaction_restart))\
|
|
|
|
continue; \
|
|
|
|
if (_ret) \
|
|
|
|
break; \
|
2022-07-07 04:37:46 +00:00
|
|
|
bch2_trans_verify_not_restarted(_trans, _restart_count);\
|
2022-07-20 20:13:27 +00:00
|
|
|
if (!bch2_btree_iter_advance(&(_iter))) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
bch2_trans_iter_exit((_trans), &(_iter)); \
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define for_each_btree_key_reverse(_trans, _iter, _btree_id, \
|
|
|
|
_start, _flags, _k, _do) \
|
|
|
|
({ \
|
|
|
|
int _ret = 0; \
|
|
|
|
\
|
|
|
|
bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \
|
|
|
|
(_start), (_flags)); \
|
|
|
|
\
|
|
|
|
while (1) { \
|
2022-07-07 04:37:46 +00:00
|
|
|
u32 _restart_count = bch2_trans_begin(_trans); \
|
2022-07-20 20:13:27 +00:00
|
|
|
(_k) = bch2_btree_iter_peek_prev_type(&(_iter), (_flags));\
|
|
|
|
if (!(_k).k) { \
|
|
|
|
_ret = 0; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
_ret = bkey_err(_k) ?: (_do); \
|
|
|
|
if (bch2_err_matches(_ret, BCH_ERR_transaction_restart))\
|
|
|
|
continue; \
|
|
|
|
if (_ret) \
|
|
|
|
break; \
|
2022-07-07 04:37:46 +00:00
|
|
|
bch2_trans_verify_not_restarted(_trans, _restart_count);\
|
2022-07-20 20:13:27 +00:00
|
|
|
if (!bch2_btree_iter_rewind(&(_iter))) \
|
|
|
|
break; \
|
2022-07-18 03:06:38 +00:00
|
|
|
} \
|
2022-07-16 00:51:09 +00:00
|
|
|
\
|
|
|
|
bch2_trans_iter_exit((_trans), &(_iter)); \
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define for_each_btree_key_commit(_trans, _iter, _btree_id, \
|
|
|
|
_start, _iter_flags, _k, \
|
|
|
|
_disk_res, _journal_seq, _commit_flags,\
|
|
|
|
_do) \
|
|
|
|
for_each_btree_key2(_trans, _iter, _btree_id, _start, _iter_flags, _k,\
|
|
|
|
(_do) ?: bch2_trans_commit(_trans, (_disk_res),\
|
|
|
|
(_journal_seq), (_commit_flags)))
|
|
|
|
|
2019-04-17 19:49:28 +00:00
|
|
|
#define for_each_btree_key(_trans, _iter, _btree_id, \
|
|
|
|
_start, _flags, _k, _ret) \
|
2021-08-30 19:18:31 +00:00
|
|
|
for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \
|
2021-10-21 16:05:21 +00:00
|
|
|
(_start), (_flags)); \
|
|
|
|
(_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\
|
2021-03-05 03:11:28 +00:00
|
|
|
!((_ret) = bkey_err(_k)) && (_k).k; \
|
2021-10-21 16:05:21 +00:00
|
|
|
bch2_btree_iter_advance(&(_iter)))
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
#define for_each_btree_key_norestart(_trans, _iter, _btree_id, \
|
|
|
|
_start, _flags, _k, _ret) \
|
|
|
|
for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \
|
|
|
|
(_start), (_flags)); \
|
2022-01-09 06:07:29 +00:00
|
|
|
(_k) = bch2_btree_iter_peek_type(&(_iter), _flags), \
|
2019-09-25 19:57:56 +00:00
|
|
|
!((_ret) = bkey_err(_k)) && (_k).k; \
|
2021-10-21 16:05:21 +00:00
|
|
|
bch2_btree_iter_advance(&(_iter)))
|
2022-03-11 17:31:52 +00:00
|
|
|
|
|
|
|
#define for_each_btree_key_upto_norestart(_trans, _iter, _btree_id, \
|
|
|
|
_start, _end, _flags, _k, _ret) \
|
|
|
|
for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id), \
|
|
|
|
(_start), (_flags)); \
|
|
|
|
(_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, _flags),\
|
|
|
|
!((_ret) = bkey_err(_k)) && (_k).k; \
|
|
|
|
bch2_btree_iter_advance(&(_iter)))
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
#define for_each_btree_key_continue(_trans, _iter, _flags, _k, _ret) \
|
|
|
|
for (; \
|
|
|
|
(_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\
|
|
|
|
!((_ret) = bkey_err(_k)) && (_k).k; \
|
|
|
|
bch2_btree_iter_advance(&(_iter)))
|
2021-02-21 03:19:34 +00:00
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
#define for_each_btree_key_continue_norestart(_iter, _flags, _k, _ret) \
|
|
|
|
for (; \
|
2022-01-09 06:07:29 +00:00
|
|
|
(_k) = bch2_btree_iter_peek_type(&(_iter), _flags), \
|
2021-10-21 16:05:21 +00:00
|
|
|
!((_ret) = bkey_err(_k)) && (_k).k; \
|
|
|
|
bch2_btree_iter_advance(&(_iter)))
|
2021-02-21 03:19:34 +00:00
|
|
|
|
2021-10-21 16:05:21 +00:00
|
|
|
/* new multiple iterator interface: */
|
2021-03-20 02:54:18 +00:00
|
|
|
|
2022-03-11 23:38:24 +00:00
|
|
|
void bch2_trans_updates_to_text(struct printbuf *, struct btree_trans *);
|
2022-08-12 00:14:54 +00:00
|
|
|
void bch2_btree_path_to_text(struct printbuf *, struct btree_path *);
|
|
|
|
void bch2_trans_paths_to_text(struct printbuf *, struct btree_trans *);
|
2022-03-03 03:18:56 +00:00
|
|
|
void bch2_dump_trans_updates(struct btree_trans *);
|
2021-10-21 16:05:21 +00:00
|
|
|
void bch2_dump_trans_paths_updates(struct btree_trans *);
|
2022-10-17 11:03:11 +00:00
|
|
|
void __bch2_trans_init(struct btree_trans *, struct bch_fs *, unsigned);
|
2021-10-19 19:08:00 +00:00
|
|
|
void bch2_trans_exit(struct btree_trans *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-10-17 11:03:11 +00:00
|
|
|
extern const char *bch2_btree_transaction_fns[BCH_TRANSACTIONS_NR];
|
|
|
|
unsigned bch2_trans_get_fn_idx(const char *);
|
|
|
|
|
|
|
|
#define bch2_trans_init(_trans, _c, _nr_iters, _mem) \
|
|
|
|
do { \
|
|
|
|
static unsigned trans_fn_idx; \
|
|
|
|
\
|
|
|
|
if (unlikely(!trans_fn_idx)) \
|
|
|
|
trans_fn_idx = bch2_trans_get_fn_idx(__func__); \
|
|
|
|
\
|
|
|
|
__bch2_trans_init(_trans, _c, trans_fn_idx); \
|
|
|
|
} while (0)
|
2022-01-04 05:33:52 +00:00
|
|
|
|
2022-06-18 00:12:02 +00:00
|
|
|
void bch2_btree_trans_to_text(struct printbuf *, struct btree_trans *);
|
2020-06-02 20:36:11 +00:00
|
|
|
|
2019-09-07 18:16:00 +00:00
|
|
|
void bch2_fs_btree_iter_exit(struct bch_fs *);
|
|
|
|
int bch2_fs_btree_iter_init(struct bch_fs *);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
#endif /* _BCACHEFS_BTREE_ITER_H */
|