bcachefs: bch2_subvolume_get()

Factor out a little helper.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
This commit is contained in:
Kent Overstreet 2021-09-30 19:46:23 -04:00 committed by Kent Overstreet
parent 69294246b7
commit 97996ddfdb
5 changed files with 41 additions and 92 deletions

View File

@ -191,34 +191,15 @@ int __bch2_dirent_read_target(struct btree_trans *trans,
if (likely(d.v->d_type != DT_SUBVOL)) { if (likely(d.v->d_type != DT_SUBVOL)) {
*inum = le64_to_cpu(d.v->d_inum); *inum = le64_to_cpu(d.v->d_inum);
} else { } else {
struct btree_iter iter; struct bch_subvolume s;
struct bkey_s_c k;
struct bkey_s_c_subvolume s;
int ret; int ret;
*subvol = le64_to_cpu(d.v->d_inum); *subvol = le64_to_cpu(d.v->d_inum);
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
POS(0, *subvol),
BTREE_ITER_CACHED);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_subvolume) { ret = bch2_subvolume_get(trans, *subvol, !is_fsck, BTREE_ITER_CACHED, &s);
ret = -ENOENT;
goto err;
}
s = bkey_s_c_to_subvolume(k); *snapshot = le32_to_cpu(s.snapshot);
*snapshot = le32_to_cpu(s.v->snapshot); *inum = le64_to_cpu(s.inode);
*inum = le64_to_cpu(s.v->inode);
err:
if (ret == -ENOENT && !is_fsck)
bch2_fs_inconsistent(trans->c, "pointer to missing subvolume %u",
*subvol);
bch2_trans_iter_exit(trans, &iter);
} }
return ret; return ret;

View File

@ -67,26 +67,14 @@ int bch2_create_trans(struct btree_trans *trans,
if (!snapshot_src.inum) { if (!snapshot_src.inum) {
/* Inode wasn't specified, just snapshot: */ /* Inode wasn't specified, just snapshot: */
struct btree_iter subvol_iter; struct bch_subvolume s;
struct bkey_s_c k;
bch2_trans_iter_init(trans, &subvol_iter, BTREE_ID_subvolumes,
POS(0, snapshot_src.subvol), 0);
k = bch2_btree_iter_peek_slot(&subvol_iter);
ret = bkey_err(k);
if (!ret && k.k->type != KEY_TYPE_subvolume) {
bch_err(c, "subvolume %u not found",
snapshot_src.subvol);
ret = -ENOENT;
}
if (!ret)
snapshot_src.inum = le64_to_cpu(bkey_s_c_to_subvolume(k).v->inode);
bch2_trans_iter_exit(trans, &subvol_iter);
ret = bch2_subvolume_get(trans, snapshot_src.subvol, true,
BTREE_ITER_CACHED, &s);
if (ret) if (ret)
goto err; goto err;
snapshot_src.inum = le64_to_cpu(s.inode);
} }
ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src, ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src,

View File

@ -103,29 +103,14 @@ static int snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
static int __subvol_lookup(struct btree_trans *trans, u32 subvol, static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
u32 *snapshot, u64 *inum) u32 *snapshot, u64 *inum)
{ {
struct btree_iter iter; struct bch_subvolume s;
struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
POS(0, subvol), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k);
if (ret)
goto err;
if (k.k->type != KEY_TYPE_subvolume) { *snapshot = le32_to_cpu(s.snapshot);
bch_err(trans->c, "subvolume %u not fonud", subvol); *inum = le64_to_cpu(s.inode);
ret = -ENOENT;
goto err;
}
*snapshot = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
*inum = le64_to_cpu(bkey_s_c_to_subvolume(k).v->inode);
err:
bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
static int subvol_lookup(struct btree_trans *trans, u32 subvol, static int subvol_lookup(struct btree_trans *trans, u32 subvol,

View File

@ -89,23 +89,6 @@ int bch2_mark_snapshot(struct bch_fs *c,
return 0; return 0;
} }
static int subvol_lookup(struct btree_trans *trans, unsigned id, 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, id), 0);
k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
if (!ret)
*s = *bkey_s_c_to_subvolume(k).v;
bch2_trans_iter_exit(trans, &iter);
return ret;
}
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)
{ {
@ -195,7 +178,7 @@ static int bch2_snapshot_check(struct btree_trans *trans,
int ret; int ret;
id = le32_to_cpu(s.v->subvol); id = le32_to_cpu(s.v->subvol);
ret = lockrestart_do(trans, subvol_lookup(trans, id, &subvol)); ret = lockrestart_do(trans, bch2_subvolume_get(trans, id, 0, false, &subvol));
if (ret == -ENOENT) if (ret == -ENOENT)
bch_err(trans->c, "snapshot node %llu has nonexistent subvolume %u", bch_err(trans->c, "snapshot node %llu has nonexistent subvolume %u",
s.k->p.offset, id); s.k->p.offset, id);
@ -798,34 +781,44 @@ void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
le32_to_cpu(s.v->snapshot)); le32_to_cpu(s.v->snapshot));
} }
int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol, int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
u32 *snapid) bool inconsistent_if_not_found,
int iter_flags,
struct bch_subvolume *s)
{ {
struct btree_iter iter; struct btree_iter iter;
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
POS(0, subvol), iter_flags);
BTREE_ITER_CACHED|
BTREE_ITER_WITH_UPDATES);
k = bch2_btree_iter_peek_slot(&iter); k = bch2_btree_iter_peek_slot(&iter);
ret = bkey_err(k); ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
if (ret)
goto err;
if (k.k->type != KEY_TYPE_subvolume) { if (ret == -ENOENT && inconsistent_if_not_found)
bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol); bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
ret = -EIO; if (!ret)
goto err; *s = *bkey_s_c_to_subvolume(k).v;
}
*snapid = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
err:
bch2_trans_iter_exit(trans, &iter); bch2_trans_iter_exit(trans, &iter);
return ret; return ret;
} }
int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
u32 *snapid)
{
struct bch_subvolume s;
int ret;
ret = bch2_subvolume_get(trans, subvol, true,
BTREE_ITER_CACHED|
BTREE_ITER_WITH_UPDATES,
&s);
*snapid = le32_to_cpu(s.snapshot);
return ret;
}
/* XXX: mark snapshot id for deletion, walk btree and delete: */ /* XXX: mark snapshot id for deletion, walk btree and delete: */
int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid, int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid,
int deleting_snapshot) int deleting_snapshot)

View File

@ -104,6 +104,8 @@ void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c)
.val_to_text = bch2_subvolume_to_text, \ .val_to_text = bch2_subvolume_to_text, \
} }
int bch2_subvolume_get(struct btree_trans *, unsigned,
bool, int, struct bch_subvolume *);
int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *); int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
int bch2_subvolume_delete(struct btree_trans *, u32, int); int bch2_subvolume_delete(struct btree_trans *, u32, int);