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: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2023-04-29 19:33:09 -04:00
parent 174f930b8e
commit bcb79a51cb
15 changed files with 162 additions and 218 deletions

View File

@ -540,14 +540,13 @@ bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter
struct bkey_i_alloc_v4 *a;
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_CACHED|
BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
if (unlikely(ret))
goto err;
return ERR_PTR(ret);
a = bch2_alloc_to_v4_mut_inlined(trans, k);
ret = PTR_ERR_OR_ZERO(a);
@ -789,13 +788,12 @@ static int bch2_bucket_do_index(struct btree_trans *trans,
return 0;
}
bch2_trans_iter_init(trans, &iter, btree,
old = bch2_bkey_get_iter(trans, &iter, btree,
bkey_start_pos(&k->k),
BTREE_ITER_INTENT);
old = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(old);
if (ret)
goto err;
return ret;
if (ca->mi.freespace_initialized &&
test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags) &&
@ -833,13 +831,12 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
if (ret)
return ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_bucket_gens, pos,
BTREE_ITER_INTENT|
BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_bucket_gens, pos,
BTREE_ITER_INTENT|
BTREE_ITER_WITH_UPDATES);
ret = bkey_err(k);
if (ret)
goto err;
return ret;
if (k.k->type != KEY_TYPE_bucket_gens) {
bkey_bucket_gens_init(&g->k_i);
@ -851,7 +848,6 @@ static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
g->v.gens[offset] = gen;
ret = bch2_trans_update(trans, &iter, &g->k_i, 0);
err:
bch2_trans_iter_exit(trans, &iter);
return ret;
}
@ -1312,18 +1308,16 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans,
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,
"entry in %s btree for nonexistant dev:bucket %llu:%llu",
bch2_btree_ids[iter->btree_id], pos.inode, pos.offset))
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);
if (fsck_err_on(a->data_type != state ||
@ -1336,7 +1330,6 @@ static int bch2_check_discard_freespace_key(struct btree_trans *trans,
genbits >> 56, alloc_freespace_genbits(*a) >> 56))
goto delete;
out:
err:
fsck_err:
bch2_trans_iter_exit(trans, &alloc_iter);
printbuf_exit(&buf);
@ -1525,7 +1518,7 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
struct btree_iter lru_iter;
struct bch_alloc_v4 a_convert;
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;
int ret;
@ -1542,21 +1535,20 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
if (a->data_type != BCH_DATA_cached)
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,
bucket_to_u64(alloc_k.k->p),
a->io_time[READ]), 0);
k = bch2_btree_iter_peek_slot(&lru_iter);
ret = bkey_err(k);
ret = bkey_err(lru_k);
if (ret)
goto err;
return ret;
if (fsck_err_on(!a->io_time[READ], c,
"cached bucket with read_time 0\n"
" %s",
(printbuf_reset(&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"
" %s",
(printbuf_reset(&buf),
@ -1645,10 +1637,9 @@ static int bch2_discard_one_bucket(struct btree_trans *trans,
goto out;
}
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
need_discard_iter->pos,
BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
need_discard_iter->pos,
BTREE_ITER_CACHED);
ret = bkey_err(k);
if (ret)
goto out;

View File

@ -303,8 +303,9 @@ static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bc
goto err;
}
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter,
BTREE_ID_alloc, POS(ca->dev_idx, b),
BTREE_ITER_CACHED);
ret = bkey_err(k);
if (ret) {
ob = ERR_PTR(ret);

View File

@ -158,12 +158,11 @@ int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *trans,
set_bkey_val_u64s(&bp_k->k, 0);
}
bch2_trans_iter_init(trans, &bp_iter, BTREE_ID_backpointers,
bp_k->k.p,
BTREE_ITER_INTENT|
BTREE_ITER_SLOTS|
BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&bp_iter);
k = bch2_bkey_get_iter(trans, &bp_iter, BTREE_ID_backpointers,
bp_k->k.p,
BTREE_ITER_INTENT|
BTREE_ITER_SLOTS|
BTREE_ITER_WITH_UPDATES);
ret = bkey_err(k);
if (ret)
goto err;
@ -202,9 +201,8 @@ int bch2_get_next_backpointer(struct btree_trans *trans,
goto done;
if (gen >= 0) {
bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc,
bucket, BTREE_ITER_CACHED|iter_flags);
k = bch2_btree_iter_peek_slot(&alloc_iter);
k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc,
bucket, BTREE_ITER_CACHED|iter_flags);
ret = bkey_err(k);
if (ret)
goto out;
@ -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);
bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc,
bp_pos_to_bucket(c, k.k->p), 0);
alloc_k = bch2_btree_iter_peek_slot(&alloc_iter);
alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc,
bp_pos_to_bucket(c, k.k->p), 0);
ret = bkey_err(alloc_k);
if (ret)
goto out;
@ -442,10 +438,9 @@ static int check_bp_exists(struct btree_trans *trans,
if (!bch2_dev_bucket_exists(c, bucket))
goto missing;
bch2_trans_iter_init(trans, &bp_iter, BTREE_ID_backpointers,
bucket_pos_to_bp(c, bucket, bp.bucket_offset),
0);
bp_k = bch2_btree_iter_peek_slot(&bp_iter);
bp_k = bch2_bkey_get_iter(trans, &bp_iter, BTREE_ID_backpointers,
bucket_pos_to_bp(c, bucket, bp.bucket_offset),
0);
ret = bkey_err(bp_k);
if (ret)
goto err;

View File

@ -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)
{
struct bkey_i *mut = bch2_trans_kmalloc_nomemzero(trans, bkey_bytes(k.k));

View File

@ -386,10 +386,9 @@ static int btree_key_cache_fill(struct btree_trans *trans,
struct bkey_i *new_k = NULL;
int ret;
bch2_trans_iter_init(trans, &iter, ck->key.btree_id, ck->key.pos,
BTREE_ITER_KEY_CACHE_FILL|
BTREE_ITER_CACHED_NOFILL);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, ck->key.btree_id, ck->key.pos,
BTREE_ITER_KEY_CACHE_FILL|
BTREE_ITER_CACHED_NOFILL);
ret = bkey_err(k);
if (ret)
goto err;

View File

@ -57,10 +57,9 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
whiteout_pos.snapshot = k.k->p.snapshot;
bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
BTREE_ITER_NOT_EXTENTS|
BTREE_ITER_INTENT);
k2 = bch2_btree_iter_peek_slot(&iter2);
k2 = bch2_bkey_get_iter(trans, &iter2, id, whiteout_pos,
BTREE_ITER_NOT_EXTENTS|
BTREE_ITER_INTENT);
ret = bkey_err(k2);
if (!ret && k2.k->type == KEY_TYPE_deleted) {

View File

@ -452,9 +452,8 @@ static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
struct bkey_s_c k;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
POS(0, idx), BTREE_ITER_SLOTS);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
POS(0, idx), BTREE_ITER_SLOTS);
ret = bkey_err(k);
if (ret)
goto err;
@ -755,9 +754,8 @@ static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
struct bkey_s_c_stripe s;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, idx),
BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
BTREE_ITER_INTENT);
ret = bkey_err(k);
if (ret)
goto err;
@ -835,9 +833,8 @@ static int ec_stripe_key_update(struct btree_trans *trans,
struct bkey_s_c k;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes,
new->k.p, BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
new->k.p, BTREE_ITER_INTENT);
ret = bkey_err(k);
if (ret)
goto err;

View File

@ -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,
u32 *subvol)
{
struct btree_iter iter;
struct bkey_s_c k;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
POS(0, snapshot), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_snapshot) {
struct bch_snapshot s;
int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
POS(0, snapshot), 0,
snapshot, &s);
if (!ret)
*subvol = le32_to_cpu(s.subvol);
else if (ret == -ENOENT)
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;
}
@ -152,9 +140,8 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
struct bkey_s_c k;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
SPOS(0, inode_nr, *snapshot), 0);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, inode_nr, *snapshot), 0);
ret = bkey_err(k);
if (ret)
goto err;
@ -259,10 +246,8 @@ static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
retry:
bch2_trans_begin(trans);
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
ret = bkey_err(k);
if (ret)
goto err;
@ -453,22 +438,14 @@ static int remove_backpointer(struct btree_trans *trans,
struct bch_inode_unpacked *inode)
{
struct btree_iter iter;
struct bkey_s_c k;
struct bkey_s_c_dirent d;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
POS(inode->bi_dir, inode->bi_dir_offset), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
if (ret)
goto out;
if (k.k->type != KEY_TYPE_dirent) {
ret = -ENOENT;
goto out;
}
ret = __remove_dirent(trans, k.k->p);
out:
d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
POS(inode->bi_dir, inode->bi_dir_offset), 0,
dirent);
ret = bkey_err(d) ?:
__remove_dirent(trans, d.k->p);
bch2_trans_iter_exit(trans, &iter);
return ret;
}
@ -1081,20 +1058,7 @@ static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
struct btree_iter *iter,
struct bpos pos)
{
struct bkey_s_c k;
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);
return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
}
static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,

View File

@ -329,13 +329,12 @@ int bch2_inode_peek(struct btree_trans *trans,
if (ret)
return ret;
bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot),
flags|BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(iter);
k = bch2_bkey_get_iter(trans, iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot),
flags|BTREE_ITER_CACHED);
ret = bkey_err(k);
if (ret)
goto err;
return ret;
ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
if (ret)
@ -760,11 +759,9 @@ int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
if (ret)
goto err;
bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot),
BTREE_ITER_INTENT|BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(&trans, &iter, BTREE_ID_inodes,
SPOS(0, inum.inum, snapshot),
BTREE_ITER_INTENT|BTREE_ITER_CACHED);
ret = bkey_err(k);
if (ret)
goto err;

View File

@ -2312,9 +2312,8 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
if (crc_is_compressed(rbio->pick.crc))
return 0;
bch2_trans_iter_init(trans, &iter, rbio->data_btree, rbio->data_pos,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, rbio->data_btree, rbio->data_pos,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
if ((ret = bkey_err(k)))
goto out;
@ -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) +
*offset_into_extent;
bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink,
POS(0, reflink_offset),
BTREE_ITER_SLOTS);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
POS(0, reflink_offset), 0);
ret = bkey_err(k);
if (ret)
goto err;

View File

@ -114,8 +114,7 @@ static int bch2_check_lru_key(struct btree_trans *trans,
alloc_pos.inode, alloc_pos.offset))
return bch2_btree_delete_at(trans, lru_iter, 0);
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
ret = bkey_err(k);
if (ret)
goto err;

View File

@ -91,12 +91,9 @@ static int bch2_bucket_is_movable(struct btree_trans *trans,
b->k.bucket.offset))
return 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
b->k.bucket, BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
b->k.bucket, BTREE_ITER_CACHED);
ret = bkey_err(k);
bch2_trans_iter_exit(trans, &iter);
if (ret)
return ret;
@ -108,14 +105,7 @@ static int bch2_bucket_is_movable(struct btree_trans *trans,
a->fragmentation_lru &&
a->fragmentation_lru <= time;
if (!ret) {
struct printbuf buf = PRINTBUF;
bch2_bkey_val_to_text(&buf, trans->c, k);
pr_debug("%s", buf.buf);
printbuf_exit(&buf);
}
bch2_trans_iter_exit(trans, &iter);
return ret;
}

View File

@ -901,10 +901,8 @@ static int bch2_set_quota_trans(struct btree_trans *trans,
struct bkey_s_c k;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
ret = bkey_err(k);
if (unlikely(ret))
return ret;

View File

@ -1065,12 +1065,11 @@ static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
struct bch_inode_unpacked inode;
int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
k = bch2_btree_iter_peek_slot(&iter);
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
ret = bkey_err(k);
if (ret)
goto err;
return ret;
if (!bkey_is_inode(k.k)) {
bch_err(trans->c, "root inode not found");

View File

@ -103,20 +103,8 @@ int bch2_mark_snapshot(struct btree_trans *trans,
static int snapshot_lookup(struct btree_trans *trans, u32 id,
struct bch_snapshot *s)
{
struct btree_iter iter;
struct bkey_s_c k;
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;
return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
BTREE_ITER_WITH_UPDATES, snapshot, s);
}
static int snapshot_live(struct btree_trans *trans, u32 id)
@ -402,28 +390,21 @@ 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)
{
struct bch_fs *c = trans->c;
struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
struct bkey_s_c k;
struct bkey_s_c_snapshot s;
u32 parent_id;
unsigned i;
int ret = 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
BTREE_ITER_INTENT, snapshot);
ret = bkey_err(s);
bch2_fs_inconsistent_on(ret == -ENOENT, c, "missing snapshot %u", id);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_snapshot) {
bch2_fs_inconsistent(trans->c, "missing snapshot %u", id);
ret = -ENOENT;
goto err;
}
s = bkey_s_c_to_snapshot(k);
BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
parent_id = le32_to_cpu(s.v->parent);
@ -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);
ret = PTR_ERR_OR_ZERO(parent);
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;
}
@ -445,7 +426,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
break;
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);
else
parent->v.children[i] = 0;
@ -756,21 +737,10 @@ bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
int iter_flags,
struct bch_subvolume *s)
{
struct btree_iter iter;
struct bkey_s_c k;
int ret;
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);
int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
iter_flags, subvolume, s);
bch2_fs_inconsistent_on(ret == -ENOENT && inconsistent_if_not_found,
trans->c, "missing subvolume %u", subvol);
return ret;
}
@ -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)
{
struct btree_iter iter;
struct bkey_s_c k;
struct bkey_s_c_subvolume subvol;
struct btree_trans_commit_hook *h;
u32 snapid;
int ret = 0;
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
POS(0, subvolid),
BTREE_ITER_CACHED|
BTREE_ITER_INTENT);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
subvol = bch2_bkey_get_iter_typed(trans, &iter,
BTREE_ID_subvolumes, POS(0, subvolid),
BTREE_ITER_CACHED|BTREE_ITER_INTENT,
subvolume);
ret = bkey_err(subvol);
bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
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);
ret = bch2_btree_delete_at(trans, &iter, 0);