Commit bcb79a51 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: bch2_bkey_get_iter() helpers

Introduce new helpers for a common pattern:

  bch2_trans_iter_init();
  bch2_btree_iter_peek_slot();

 - bch2_bkey_get_iter_type() returns -ENOENT if it doesn't find a key of
   the correct type
 - bch2_bkey_get_val_typed() copies the val out of the btree to a
   (typically stack allocated) variable; it handles the case where the
   value in the btree is smaller than the current version of the type,
   zeroing out the remainder.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 174f930b
...@@ -540,14 +540,13 @@ bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter ...@@ -540,14 +540,13 @@ bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter
struct bkey_i_alloc_v4 *a; struct bkey_i_alloc_v4 *a;
int ret; int ret;
bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos, k = bch2_bkey_get_iter(trans, iter, BTREE_ID_alloc, pos,
BTREE_ITER_WITH_UPDATES| BTREE_ITER_WITH_UPDATES|
BTREE_ITER_CACHED| BTREE_ITER_CACHED|
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k); ret = bkey_err(k);
if (unlikely(ret)) if (unlikely(ret))
goto err; return ERR_PTR(ret);
a = bch2_alloc_to_v4_mut_inlined(trans, k); a = bch2_alloc_to_v4_mut_inlined(trans, k);
ret = PTR_ERR_OR_ZERO(a); ret = PTR_ERR_OR_ZERO(a);
...@@ -789,13 +788,12 @@ static int bch2_bucket_do_index(struct btree_trans *trans, ...@@ -789,13 +788,12 @@ static int bch2_bucket_do_index(struct btree_trans *trans,
return 0; return 0;
} }
bch2_trans_iter_init(trans, &iter, btree, old = bch2_bkey_get_iter(trans, &iter, btree,
bkey_start_pos(&k->k), bkey_start_pos(&k->k),
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
old = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(old); ret = bkey_err(old);
if (ret) if (ret)
goto err; return ret;
if (ca->mi.freespace_initialized && if (ca->mi.freespace_initialized &&
test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags) && test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags) &&
...@@ -833,13 +831,12 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans, ...@@ -833,13 +831,12 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
if (ret) if (ret)
return ret; return ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_bucket_gens, pos, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_bucket_gens, pos,
BTREE_ITER_INTENT| BTREE_ITER_INTENT|
BTREE_ITER_WITH_UPDATES); BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; return ret;
if (k.k->type != KEY_TYPE_bucket_gens) { if (k.k->type != KEY_TYPE_bucket_gens) {
bkey_bucket_gens_init(&g->k_i); bkey_bucket_gens_init(&g->k_i);
...@@ -851,7 +848,6 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans, ...@@ -851,7 +848,6 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
g->v.gens[offset] = gen; g->v.gens[offset] = gen;
ret = bch2_trans_update(trans, &iter, &g->k_i, 0); ret = bch2_trans_update(trans, &iter, &g->k_i, 0);
err:
bch2_trans_iter_exit(trans, &iter); bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
...@@ -1312,18 +1308,16 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans, ...@@ -1312,18 +1308,16 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans,
pos.offset &= ~(~0ULL << 56); pos.offset &= ~(~0ULL << 56);
genbits = iter->pos.offset & (~0ULL << 56); genbits = iter->pos.offset & (~0ULL << 56);
bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0); alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
ret = bkey_err(alloc_k);
if (ret)
return ret;
if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c, if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
"entry in %s btree for nonexistant dev:bucket %llu:%llu", "entry in %s btree for nonexistant dev:bucket %llu:%llu",
bch2_btree_ids[iter->btree_id], pos.inode, pos.offset)) bch2_btree_ids[iter->btree_id], pos.inode, pos.offset))
goto delete; goto delete;
alloc_k = bch2_btree_iter_peek_slot(&alloc_iter);
ret = bkey_err(alloc_k);
if (ret)
goto err;
a = bch2_alloc_to_v4(alloc_k, &a_convert); a = bch2_alloc_to_v4(alloc_k, &a_convert);
if (fsck_err_on(a->data_type != state || if (fsck_err_on(a->data_type != state ||
...@@ -1336,7 +1330,6 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans, ...@@ -1336,7 +1330,6 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans,
genbits >> 56, alloc_freespace_genbits(*a) >> 56)) genbits >> 56, alloc_freespace_genbits(*a) >> 56))
goto delete; goto delete;
out: out:
err:
fsck_err: fsck_err:
bch2_trans_iter_exit(trans, &alloc_iter); bch2_trans_iter_exit(trans, &alloc_iter);
printbuf_exit(&buf); printbuf_exit(&buf);
...@@ -1525,7 +1518,7 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans, ...@@ -1525,7 +1518,7 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
struct btree_iter lru_iter; struct btree_iter lru_iter;
struct bch_alloc_v4 a_convert; struct bch_alloc_v4 a_convert;
const struct bch_alloc_v4 *a; const struct bch_alloc_v4 *a;
struct bkey_s_c alloc_k, k; struct bkey_s_c alloc_k, lru_k;
struct printbuf buf = PRINTBUF; struct printbuf buf = PRINTBUF;
int ret; int ret;
...@@ -1542,21 +1535,20 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans, ...@@ -1542,21 +1535,20 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
if (a->data_type != BCH_DATA_cached) if (a->data_type != BCH_DATA_cached)
return 0; return 0;
bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru, lru_k = bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru,
lru_pos(alloc_k.k->p.inode, lru_pos(alloc_k.k->p.inode,
bucket_to_u64(alloc_k.k->p), bucket_to_u64(alloc_k.k->p),
a->io_time[READ]), 0); a->io_time[READ]), 0);
k = bch2_btree_iter_peek_slot(&lru_iter); ret = bkey_err(lru_k);
ret = bkey_err(k);
if (ret) if (ret)
goto err; return ret;
if (fsck_err_on(!a->io_time[READ], c, if (fsck_err_on(!a->io_time[READ], c,
"cached bucket with read_time 0\n" "cached bucket with read_time 0\n"
" %s", " %s",
(printbuf_reset(&buf), (printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) || bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
fsck_err_on(k.k->type != KEY_TYPE_set, c, fsck_err_on(lru_k.k->type != KEY_TYPE_set, c,
"missing lru entry\n" "missing lru entry\n"
" %s", " %s",
(printbuf_reset(&buf), (printbuf_reset(&buf),
...@@ -1645,10 +1637,9 @@ static int bch2_discard_one_bucket(struct btree_trans *trans, ...@@ -1645,10 +1637,9 @@ static int bch2_discard_one_bucket(struct btree_trans *trans,
goto out; goto out;
} }
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
need_discard_iter->pos, need_discard_iter->pos,
BTREE_ITER_CACHED); BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto out; goto out;
......
...@@ -303,8 +303,9 @@ static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bc ...@@ -303,8 +303,9 @@ static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bc
goto err; goto err;
} }
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED); k = bch2_bkey_get_iter(trans, &iter,
k = bch2_btree_iter_peek_slot(&iter); BTREE_ID_alloc, POS(ca->dev_idx, b),
BTREE_ITER_CACHED);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) { if (ret) {
ob = ERR_PTR(ret); ob = ERR_PTR(ret);
......
...@@ -158,12 +158,11 @@ int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *trans, ...@@ -158,12 +158,11 @@ int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *trans,
set_bkey_val_u64s(&bp_k->k, 0); set_bkey_val_u64s(&bp_k->k, 0);
} }
bch2_trans_iter_init(trans, &bp_iter, BTREE_ID_backpointers, k = bch2_bkey_get_iter(trans, &bp_iter, BTREE_ID_backpointers,
bp_k->k.p, bp_k->k.p,
BTREE_ITER_INTENT| BTREE_ITER_INTENT|
BTREE_ITER_SLOTS| BTREE_ITER_SLOTS|
BTREE_ITER_WITH_UPDATES); BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&bp_iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
...@@ -202,9 +201,8 @@ int bch2_get_next_backpointer(struct btree_trans *trans, ...@@ -202,9 +201,8 @@ int bch2_get_next_backpointer(struct btree_trans *trans,
goto done; goto done;
if (gen >= 0) { if (gen >= 0) {
bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc,
bucket, BTREE_ITER_CACHED|iter_flags); bucket, BTREE_ITER_CACHED|iter_flags);
k = bch2_btree_iter_peek_slot(&alloc_iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto out; goto out;
...@@ -381,10 +379,8 @@ static int bch2_check_btree_backpointer(struct btree_trans *trans, struct btree_ ...@@ -381,10 +379,8 @@ static int bch2_check_btree_backpointer(struct btree_trans *trans, struct btree_
ca = bch_dev_bkey_exists(c, k.k->p.inode); ca = bch_dev_bkey_exists(c, k.k->p.inode);
bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc,
bp_pos_to_bucket(c, k.k->p), 0); bp_pos_to_bucket(c, k.k->p), 0);
alloc_k = bch2_btree_iter_peek_slot(&alloc_iter);
ret = bkey_err(alloc_k); ret = bkey_err(alloc_k);
if (ret) if (ret)
goto out; goto out;
...@@ -442,10 +438,9 @@ static int check_bp_exists(struct btree_trans *trans, ...@@ -442,10 +438,9 @@ static int check_bp_exists(struct btree_trans *trans,
if (!bch2_dev_bucket_exists(c, bucket)) if (!bch2_dev_bucket_exists(c, bucket))
goto missing; goto missing;
bch2_trans_iter_init(trans, &bp_iter, BTREE_ID_backpointers, bp_k = bch2_bkey_get_iter(trans, &bp_iter, BTREE_ID_backpointers,
bucket_pos_to_bp(c, bucket, bp.bucket_offset), bucket_pos_to_bp(c, bucket, bp.bucket_offset),
0); 0);
bp_k = bch2_btree_iter_peek_slot(&bp_iter);
ret = bkey_err(bp_k); ret = bkey_err(bp_k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -483,6 +483,62 @@ static inline void *bch2_trans_kmalloc_nomemzero(struct btree_trans *trans, size ...@@ -483,6 +483,62 @@ static inline void *bch2_trans_kmalloc_nomemzero(struct btree_trans *trans, size
} }
} }
static inline struct bkey_s_c __bch2_bkey_get_iter(struct btree_trans *trans,
struct btree_iter *iter,
unsigned btree_id, struct bpos pos,
unsigned flags, unsigned type)
{
struct bkey_s_c k;
bch2_trans_iter_init(trans, iter, btree_id, pos, flags);
k = bch2_btree_iter_peek_slot(iter);
if (!bkey_err(k) && type && k.k->type != type)
k = bkey_s_c_err(-ENOENT);
if (unlikely(bkey_err(k)))
bch2_trans_iter_exit(trans, iter);
return k;
}
static inline struct bkey_s_c bch2_bkey_get_iter(struct btree_trans *trans,
struct btree_iter *iter,
unsigned btree_id, struct bpos pos,
unsigned flags)
{
return __bch2_bkey_get_iter(trans, iter, btree_id, pos, flags, 0);
}
#define bch2_bkey_get_iter_typed(_trans, _iter, _btree_id, _pos, _flags, _type)\
bkey_s_c_to_##_type(__bch2_bkey_get_iter(_trans, _iter, \
_btree_id, _pos, _flags, KEY_TYPE_##_type))
static inline int __bch2_bkey_get_val_typed(struct btree_trans *trans,
unsigned btree_id, struct bpos pos,
unsigned flags, unsigned type,
unsigned val_size, void *val)
{
struct btree_iter iter;
struct bkey_s_c k;
int ret;
k = __bch2_bkey_get_iter(trans, &iter, btree_id, pos, flags, type);
ret = bkey_err(k);
if (!ret) {
unsigned b = min_t(unsigned, bkey_val_bytes(k.k), val_size);
memcpy(val, k.v, b);
if (unlikely(b < sizeof(*val)))
memset((void *) val + b, 0, sizeof(*val) - b);
bch2_trans_iter_exit(trans, &iter);
}
return ret;
}
#define bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags, _type, _val)\
__bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags, \
KEY_TYPE_##_type, sizeof(*_val), _val)
static inline struct bkey_i *bch2_bkey_make_mut(struct btree_trans *trans, struct bkey_s_c k) 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)); struct bkey_i *mut = bch2_trans_kmalloc_nomemzero(trans, bkey_bytes(k.k));
......
...@@ -386,10 +386,9 @@ static int btree_key_cache_fill(struct btree_trans *trans, ...@@ -386,10 +386,9 @@ static int btree_key_cache_fill(struct btree_trans *trans,
struct bkey_i *new_k = NULL; struct bkey_i *new_k = NULL;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, ck->key.btree_id, ck->key.pos, k = bch2_bkey_get_iter(trans, &iter, ck->key.btree_id, ck->key.pos,
BTREE_ITER_KEY_CACHE_FILL| BTREE_ITER_KEY_CACHE_FILL|
BTREE_ITER_CACHED_NOFILL); BTREE_ITER_CACHED_NOFILL);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -57,10 +57,9 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans, ...@@ -57,10 +57,9 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
whiteout_pos.snapshot = k.k->p.snapshot; whiteout_pos.snapshot = k.k->p.snapshot;
bch2_trans_iter_init(trans, &iter2, id, whiteout_pos, k2 = bch2_bkey_get_iter(trans, &iter2, id, whiteout_pos,
BTREE_ITER_NOT_EXTENTS| BTREE_ITER_NOT_EXTENTS|
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
k2 = bch2_btree_iter_peek_slot(&iter2);
ret = bkey_err(k2); ret = bkey_err(k2);
if (!ret && k2.k->type == KEY_TYPE_deleted) { if (!ret && k2.k->type == KEY_TYPE_deleted) {
......
...@@ -452,9 +452,8 @@ static int get_stripe_key_trans(struct btree_trans *trans, u64 idx, ...@@ -452,9 +452,8 @@ static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
POS(0, idx), BTREE_ITER_SLOTS); POS(0, idx), BTREE_ITER_SLOTS);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
...@@ -755,9 +754,8 @@ static int ec_stripe_delete(struct btree_trans *trans, u64 idx) ...@@ -755,9 +754,8 @@ static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
struct bkey_s_c_stripe s; struct bkey_s_c_stripe s;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, idx), k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
...@@ -835,9 +833,8 @@ static int ec_stripe_key_update(struct btree_trans *trans, ...@@ -835,9 +833,8 @@ static int ec_stripe_key_update(struct btree_trans *trans,
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
new->k.p, BTREE_ITER_INTENT); new->k.p, BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -72,26 +72,14 @@ static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum, ...@@ -72,26 +72,14 @@ static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot, static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
u32 *subvol) u32 *subvol)
{ {
struct btree_iter iter; struct bch_snapshot s;
struct bkey_s_c k; int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
int ret; POS(0, snapshot), 0,
snapshot, &s);
bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, if (!ret)
POS(0, snapshot), 0); *subvol = le32_to_cpu(s.subvol);
k = bch2_btree_iter_peek_slot(&iter); else if (ret == -ENOENT)
ret = bkey_err(k);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_snapshot) {
bch_err(trans->c, "snapshot %u not fonud", snapshot); bch_err(trans->c, "snapshot %u not fonud", snapshot);
ret = -ENOENT;
goto err;
}
*subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
err:
bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
...@@ -152,9 +140,8 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr, ...@@ -152,9 +140,8 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, inode_nr, *snapshot), 0); SPOS(0, inode_nr, *snapshot), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
...@@ -259,10 +246,8 @@ static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot) ...@@ -259,10 +246,8 @@ static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
retry: retry:
bch2_trans_begin(trans); bch2_trans_begin(trans);
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, inum, snapshot), BTREE_ITER_INTENT); SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
...@@ -453,22 +438,14 @@ static int remove_backpointer(struct btree_trans *trans, ...@@ -453,22 +438,14 @@ static int remove_backpointer(struct btree_trans *trans,
struct bch_inode_unpacked *inode) struct bch_inode_unpacked *inode)
{ {
struct btree_iter iter; struct btree_iter iter;
struct bkey_s_c k; struct bkey_s_c_dirent d;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
POS(inode->bi_dir, inode->bi_dir_offset), 0); POS(inode->bi_dir, inode->bi_dir_offset), 0,
k = bch2_btree_iter_peek_slot(&iter); dirent);
ret = bkey_err(k); ret = bkey_err(d) ?:
if (ret) __remove_dirent(trans, d.k->p);
goto out;
if (k.k->type != KEY_TYPE_dirent) {
ret = -ENOENT;
goto out;
}
ret = __remove_dirent(trans, k.k->p);
out:
bch2_trans_iter_exit(trans, &iter); bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
...@@ -1081,20 +1058,7 @@ static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans, ...@@ -1081,20 +1058,7 @@ static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
struct btree_iter *iter, struct btree_iter *iter,
struct bpos pos) struct bpos pos)
{ {
struct bkey_s_c k; return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
int ret;
bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
if (!ret && k.k->type != KEY_TYPE_dirent)
ret = -ENOENT;
if (ret) {
bch2_trans_iter_exit(trans, iter);
return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
}
return bkey_s_c_to_dirent(k);
} }
static bool inode_points_to_dirent(struct bch_inode_unpacked *inode, static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
......
...@@ -329,13 +329,12 @@ int bch2_inode_peek(struct btree_trans *trans, ...@@ -329,13 +329,12 @@ int bch2_inode_peek(struct btree_trans *trans,
if (ret) if (ret)
return ret; return ret;
bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, k = bch2_bkey_get_iter(trans, iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot), SPOS(0, inum.inum, snapshot),
flags|BTREE_ITER_CACHED); flags|BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; return ret;
ret = bkey_is_inode(k.k) ? 0 : -ENOENT; ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
if (ret) if (ret)
...@@ -760,11 +759,9 @@ int bch2_inode_rm(struct bch_fs *c, subvol_inum inum) ...@@ -760,11 +759,9 @@ int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
if (ret) if (ret)
goto err; goto err;
bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, k = bch2_bkey_get_iter(&trans, &iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot), SPOS(0, inum.inum, snapshot),
BTREE_ITER_INTENT|BTREE_ITER_CACHED); BTREE_ITER_INTENT|BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -2312,9 +2312,8 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans, ...@@ -2312,9 +2312,8 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
if (crc_is_compressed(rbio->pick.crc)) if (crc_is_compressed(rbio->pick.crc))
return 0; return 0;
bch2_trans_iter_init(trans, &iter, rbio->data_btree, rbio->data_pos, k = bch2_bkey_get_iter(trans, &iter, rbio->data_btree, rbio->data_pos,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT); BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
if ((ret = bkey_err(k))) if ((ret = bkey_err(k)))
goto out; goto out;
...@@ -2550,10 +2549,8 @@ int __bch2_read_indirect_extent(struct btree_trans *trans, ...@@ -2550,10 +2549,8 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) + reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
*offset_into_extent; *offset_into_extent;
bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
POS(0, reflink_offset), POS(0, reflink_offset), 0);
BTREE_ITER_SLOTS);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -114,8 +114,7 @@ static int bch2_check_lru_key(struct btree_trans *trans, ...@@ -114,8 +114,7 @@ static int bch2_check_lru_key(struct btree_trans *trans,
alloc_pos.inode, alloc_pos.offset)) alloc_pos.inode, alloc_pos.offset))
return bch2_btree_delete_at(trans, lru_iter, 0); return bch2_btree_delete_at(trans, lru_iter, 0);
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, alloc_pos, 0); k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; goto err;
......
...@@ -91,12 +91,9 @@ static int bch2_bucket_is_movable(struct btree_trans *trans, ...@@ -91,12 +91,9 @@ static int bch2_bucket_is_movable(struct btree_trans *trans,
b->k.bucket.offset)) b->k.bucket.offset))
return 0; return 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
b->k.bucket, BTREE_ITER_CACHED); b->k.bucket, BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
bch2_trans_iter_exit(trans, &iter);
if (ret) if (ret)
return ret; return ret;
...@@ -108,14 +105,7 @@ static int bch2_bucket_is_movable(struct btree_trans *trans, ...@@ -108,14 +105,7 @@ static int bch2_bucket_is_movable(struct btree_trans *trans,
a->fragmentation_lru && a->fragmentation_lru &&
a->fragmentation_lru <= time; a->fragmentation_lru <= time;
if (!ret) { bch2_trans_iter_exit(trans, &iter);
struct printbuf buf = PRINTBUF;
bch2_bkey_val_to_text(&buf, trans->c, k);
pr_debug("%s", buf.buf);
printbuf_exit(&buf);
}
return ret; return ret;
} }
......
...@@ -901,10 +901,8 @@ static int bch2_set_quota_trans(struct btree_trans *trans, ...@@ -901,10 +901,8 @@ static int bch2_set_quota_trans(struct btree_trans *trans,
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_quotas, new_quota->k.p, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT); BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (unlikely(ret)) if (unlikely(ret))
return ret; return ret;
......
...@@ -1065,12 +1065,11 @@ static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans) ...@@ -1065,12 +1065,11 @@ static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
struct bch_inode_unpacked inode; struct bch_inode_unpacked inode;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0); SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k);
if (ret) if (ret)
goto err; return ret;
if (!bkey_is_inode(k.k)) { if (!bkey_is_inode(k.k)) {
bch_err(trans->c, "root inode not found"); bch_err(trans->c, "root inode not found");
......
...@@ -103,20 +103,8 @@ int bch2_mark_snapshot(struct btree_trans *trans, ...@@ -103,20 +103,8 @@ int bch2_mark_snapshot(struct btree_trans *trans,
static int snapshot_lookup(struct btree_trans *trans, u32 id, static int snapshot_lookup(struct btree_trans *trans, u32 id,
struct bch_snapshot *s) struct bch_snapshot *s)
{ {
struct btree_iter iter; return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
struct bkey_s_c k; BTREE_ITER_WITH_UPDATES, snapshot, s);
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k) ?: k.k->type == KEY_TYPE_snapshot ? 0 : -ENOENT;
if (!ret)
*s = *bkey_s_c_to_snapshot(k).v;
bch2_trans_iter_exit(trans, &iter);
return ret;
} }
static int snapshot_live(struct btree_trans *trans, u32 id) static int snapshot_live(struct btree_trans *trans, u32 id)
...@@ -402,27 +390,20 @@ static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id) ...@@ -402,27 +390,20 @@ static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id) static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
{ {
struct bch_fs *c = trans->c;
struct btree_iter iter, p_iter = (struct btree_iter) { NULL }; struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
struct bkey_s_c k;
struct bkey_s_c_snapshot s; struct bkey_s_c_snapshot s;
u32 parent_id; u32 parent_id;
unsigned i; unsigned i;
int ret = 0; int ret = 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id), s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
BTREE_ITER_INTENT); BTREE_ITER_INTENT, snapshot);
k = bch2_btree_iter_peek_slot(&iter); ret = bkey_err(s);
ret = bkey_err(k); bch2_fs_inconsistent_on(ret == -ENOENT, c, "missing snapshot %u", id);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_snapshot) { if (ret)
bch2_fs_inconsistent(trans->c, "missing snapshot %u", id);
ret = -ENOENT;
goto err; goto err;
}
s = bkey_s_c_to_snapshot(k);
BUG_ON(!BCH_SNAPSHOT_DELETED(s.v)); BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
parent_id = le32_to_cpu(s.v->parent); parent_id = le32_to_cpu(s.v->parent);
...@@ -436,7 +417,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id) ...@@ -436,7 +417,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
parent = bch2_bkey_get_mut_typed(trans, &p_iter, snapshot); parent = bch2_bkey_get_mut_typed(trans, &p_iter, snapshot);
ret = PTR_ERR_OR_ZERO(parent); ret = PTR_ERR_OR_ZERO(parent);
if (unlikely(ret)) { if (unlikely(ret)) {
bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", parent_id); bch2_fs_inconsistent_on(ret == -ENOENT, c, "missing snapshot %u", parent_id);
goto err; goto err;
} }
...@@ -445,7 +426,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id) ...@@ -445,7 +426,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
break; break;
if (i == 2) if (i == 2)
bch_err(trans->c, "snapshot %u missing child pointer to %u", bch_err(c, "snapshot %u missing child pointer to %u",
parent_id, id); parent_id, id);
else else
parent->v.children[i] = 0; parent->v.children[i] = 0;
...@@ -756,21 +737,10 @@ bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol, ...@@ -756,21 +737,10 @@ bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
int iter_flags, int iter_flags,
struct bch_subvolume *s) struct bch_subvolume *s)
{ {
struct btree_iter iter; int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
struct bkey_s_c k; iter_flags, subvolume, s);
int ret; bch2_fs_inconsistent_on(ret == -ENOENT && inconsistent_if_not_found,
trans->c, "missing subvolume %u", subvol);
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
iter_flags);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
if (ret == -ENOENT && inconsistent_if_not_found)
bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
if (!ret)
*s = *bkey_s_c_to_subvolume(k).v;
bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
...@@ -813,28 +783,20 @@ int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol, ...@@ -813,28 +783,20 @@ int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid) int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
{ {
struct btree_iter iter; struct btree_iter iter;
struct bkey_s_c k;
struct bkey_s_c_subvolume subvol; struct bkey_s_c_subvolume subvol;
struct btree_trans_commit_hook *h; struct btree_trans_commit_hook *h;
u32 snapid; u32 snapid;
int ret = 0; int ret = 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, subvol = bch2_bkey_get_iter_typed(trans, &iter,
POS(0, subvolid), BTREE_ID_subvolumes, POS(0, subvolid),
BTREE_ITER_CACHED| BTREE_ITER_CACHED|BTREE_ITER_INTENT,
BTREE_ITER_INTENT); subvolume);
k = bch2_btree_iter_peek_slot(&iter); ret = bkey_err(subvol);
ret = bkey_err(k); bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
if (ret) if (ret)
goto err; return ret;
if (k.k->type != KEY_TYPE_subvolume) {
bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvolid);
ret = -EIO;
goto err;
}
subvol = bkey_s_c_to_subvolume(k);
snapid = le32_to_cpu(subvol.v->snapshot); snapid = le32_to_cpu(subvol.v->snapshot);
ret = bch2_btree_delete_at(trans, &iter, 0); ret = bch2_btree_delete_at(trans, &iter, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment