Commit c98d132e authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: check_directory_structure() can now be run online

Now that we have dynamically resizable btree paths,
check_directory_structure() can check one path - inode up to the root -
in a single transaction.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent d296e7b1
...@@ -59,7 +59,7 @@ static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum, ...@@ -59,7 +59,7 @@ static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
return ret ?: subdirs; return ret ?: subdirs;
} }
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 bch_subvolume s; struct bch_subvolume s;
...@@ -72,12 +72,6 @@ static int __subvol_lookup(struct btree_trans *trans, u32 subvol, ...@@ -72,12 +72,6 @@ static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
return ret; return ret;
} }
static int subvol_lookup(struct btree_trans *trans, u32 subvol,
u32 *snapshot, u64 *inum)
{
return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
}
static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr, static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
struct bch_inode_unpacked *inode) struct bch_inode_unpacked *inode)
{ {
...@@ -105,7 +99,7 @@ static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr, ...@@ -105,7 +99,7 @@ static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
return ret; return ret;
} }
static int __lookup_inode(struct btree_trans *trans, u64 inode_nr, static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
struct bch_inode_unpacked *inode, struct bch_inode_unpacked *inode,
u32 *snapshot) u32 *snapshot)
{ {
...@@ -130,13 +124,6 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr, ...@@ -130,13 +124,6 @@ static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
return ret; return ret;
} }
static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
struct bch_inode_unpacked *inode,
u32 *snapshot)
{
return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
}
static int __lookup_dirent(struct btree_trans *trans, static int __lookup_dirent(struct btree_trans *trans,
struct bch_hash_info hash_info, struct bch_hash_info hash_info,
subvol_inum dir, struct qstr *name, subvol_inum dir, struct qstr *name,
...@@ -230,7 +217,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot, ...@@ -230,7 +217,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) }; subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) };
u32 subvol_snapshot; u32 subvol_snapshot;
ret = __subvol_lookup(trans, le32_to_cpu(st.master_subvol), ret = subvol_lookup(trans, le32_to_cpu(st.master_subvol),
&subvol_snapshot, &root_inum.inum); &subvol_snapshot, &root_inum.inum);
bch_err_msg(c, ret, "looking up root subvol"); bch_err_msg(c, ret, "looking up root subvol");
if (ret) if (ret)
...@@ -238,7 +225,8 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot, ...@@ -238,7 +225,8 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
struct bch_inode_unpacked root_inode; struct bch_inode_unpacked root_inode;
struct bch_hash_info root_hash_info; struct bch_hash_info root_hash_info;
ret = __lookup_inode(trans, root_inum.inum, &root_inode, &snapshot); ret = lookup_inode(trans, root_inum.inum, &root_inode, &snapshot);
bch_err_msg(c, ret, "looking up root inode");
if (ret) if (ret)
return ret; return ret;
...@@ -262,7 +250,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot, ...@@ -262,7 +250,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
* The bch2_check_dirents pass has already run, dangling dirents * The bch2_check_dirents pass has already run, dangling dirents
* shouldn't exist here: * shouldn't exist here:
*/ */
return __lookup_inode(trans, inum, lostfound, &snapshot); return lookup_inode(trans, inum, lostfound, &snapshot);
create_lostfound: create_lostfound:
/* /*
...@@ -305,7 +293,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot, ...@@ -305,7 +293,7 @@ static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
return ret; return ret;
} }
static int __reattach_inode(struct btree_trans *trans, static int reattach_inode(struct btree_trans *trans,
struct bch_inode_unpacked *inode, struct bch_inode_unpacked *inode,
u32 inode_snapshot) u32 inode_snapshot)
{ {
...@@ -348,16 +336,6 @@ static int __reattach_inode(struct btree_trans *trans, ...@@ -348,16 +336,6 @@ static int __reattach_inode(struct btree_trans *trans,
return __write_inode(trans, inode, inode_snapshot); return __write_inode(trans, inode, inode_snapshot);
} }
static int reattach_inode(struct btree_trans *trans,
struct bch_inode_unpacked *inode,
u32 inode_snapshot)
{
int ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
__reattach_inode(trans, inode, inode_snapshot));
bch_err_msg(trans->c, ret, "reattaching inode %llu", inode->bi_inum);
return ret;
}
static int remove_backpointer(struct btree_trans *trans, static int remove_backpointer(struct btree_trans *trans,
struct bch_inode_unpacked *inode) struct bch_inode_unpacked *inode)
{ {
...@@ -1735,7 +1713,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter, ...@@ -1735,7 +1713,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
u32 target_snapshot; u32 target_snapshot;
u64 target_inum; u64 target_inum;
ret = __subvol_lookup(trans, target_subvol, ret = subvol_lookup(trans, target_subvol,
&target_snapshot, &target_inum); &target_snapshot, &target_inum);
if (ret && !bch2_err_matches(ret, ENOENT)) if (ret && !bch2_err_matches(ret, ENOENT))
goto err; goto err;
...@@ -1747,7 +1725,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter, ...@@ -1747,7 +1725,7 @@ static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
goto err; goto err;
} }
ret = __lookup_inode(trans, target_inum, ret = lookup_inode(trans, target_inum,
&subvol_root, &target_snapshot); &subvol_root, &target_snapshot);
if (ret && !bch2_err_matches(ret, ENOENT)) if (ret && !bch2_err_matches(ret, ENOENT))
goto err; goto err;
...@@ -1907,7 +1885,7 @@ static int check_root_trans(struct btree_trans *trans) ...@@ -1907,7 +1885,7 @@ static int check_root_trans(struct btree_trans *trans)
u64 inum; u64 inum;
int ret; int ret;
ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum); ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
if (ret && !bch2_err_matches(ret, ENOENT)) if (ret && !bch2_err_matches(ret, ENOENT))
return ret; return ret;
...@@ -1929,7 +1907,7 @@ static int check_root_trans(struct btree_trans *trans) ...@@ -1929,7 +1907,7 @@ static int check_root_trans(struct btree_trans *trans)
goto err; goto err;
} }
ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot); ret = lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
if (ret && !bch2_err_matches(ret, ENOENT)) if (ret && !bch2_err_matches(ret, ENOENT))
return ret; return ret;
...@@ -2020,10 +1998,10 @@ static int check_path(struct btree_trans *trans, ...@@ -2020,10 +1998,10 @@ static int check_path(struct btree_trans *trans,
break; break;
} }
ret = lockrestart_do(trans, d = dirent_get_by_pos(trans, &dirent_iter,
PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
SPOS(inode->bi_dir, inode->bi_dir_offset, SPOS(inode->bi_dir, inode->bi_dir_offset,
parent_snapshot))).k)); parent_snapshot));
ret = bkey_err(d.s_c);
if (ret && !bch2_err_matches(ret, ENOENT)) if (ret && !bch2_err_matches(ret, ENOENT))
break; break;
...@@ -2060,6 +2038,7 @@ static int check_path(struct btree_trans *trans, ...@@ -2060,6 +2038,7 @@ static int check_path(struct btree_trans *trans,
ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot); ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
if (ret) { if (ret) {
/* Should have been caught in dirents pass */ /* Should have been caught in dirents pass */
if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
bch_err(c, "error looking up parent directory: %i", ret); bch_err(c, "error looking up parent directory: %i", ret);
break; break;
} }
...@@ -2072,19 +2051,19 @@ static int check_path(struct btree_trans *trans, ...@@ -2072,19 +2051,19 @@ static int check_path(struct btree_trans *trans,
pr_err("%llu:%u", i->inum, i->snapshot); pr_err("%llu:%u", i->inum, i->snapshot);
pr_err("%llu:%u", inode->bi_inum, snapshot); pr_err("%llu:%u", inode->bi_inum, snapshot);
if (!fsck_err(c, dir_loop, if (!fsck_err(c, dir_loop, "directory structure loop"))
"directory structure loop"))
return 0; return 0;
ret = commit_do(trans, NULL, NULL, ret = remove_backpointer(trans, inode);
BCH_TRANS_COMMIT_no_enospc, if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
remove_backpointer(trans, inode)); bch_err_msg(c, ret, "removing dirent");
if (ret) { if (ret)
bch_err(c, "error removing dirent: %i", ret);
break; break;
}
ret = reattach_inode(trans, inode, snapshot); ret = reattach_inode(trans, inode, snapshot);
if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
bch_err_msg(c, ret, "reattaching inode %llu", inode->bi_inum);
break;
} }
} }
fsck_err: fsck_err:
...@@ -2099,17 +2078,16 @@ static int check_path(struct btree_trans *trans, ...@@ -2099,17 +2078,16 @@ static int check_path(struct btree_trans *trans,
*/ */
int bch2_check_directory_structure(struct bch_fs *c) int bch2_check_directory_structure(struct bch_fs *c)
{ {
struct btree_trans *trans = bch2_trans_get(c);
struct btree_iter iter;
struct bkey_s_c k;
struct bch_inode_unpacked u; struct bch_inode_unpacked u;
pathbuf path = { 0, }; pathbuf path = { 0, };
int ret; int ret;
for_each_btree_key_old(trans, iter, BTREE_ID_inodes, POS_MIN, ret = bch2_trans_run(c,
for_each_btree_key_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
BTREE_ITER_INTENT| BTREE_ITER_INTENT|
BTREE_ITER_PREFETCH| BTREE_ITER_PREFETCH|
BTREE_ITER_ALL_SNAPSHOTS, k, ret) { BTREE_ITER_ALL_SNAPSHOTS, k,
NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
if (!bkey_is_inode(k.k)) if (!bkey_is_inode(k.k))
continue; continue;
...@@ -2118,12 +2096,8 @@ int bch2_check_directory_structure(struct bch_fs *c) ...@@ -2118,12 +2096,8 @@ int bch2_check_directory_structure(struct bch_fs *c)
if (u.bi_flags & BCH_INODE_unlinked) if (u.bi_flags & BCH_INODE_unlinked)
continue; continue;
ret = check_path(trans, &path, &u, iter.pos.snapshot); check_path(trans, &path, &u, iter.pos.snapshot);
if (ret) })));
break;
}
bch2_trans_iter_exit(trans, &iter);
bch2_trans_put(trans);
darray_exit(&path); darray_exit(&path);
bch_err_fn(c, ret); bch_err_fn(c, ret);
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
x(check_dirents, 27, PASS_FSCK) \ x(check_dirents, 27, PASS_FSCK) \
x(check_xattrs, 28, PASS_FSCK) \ x(check_xattrs, 28, PASS_FSCK) \
x(check_root, 29, PASS_ONLINE|PASS_FSCK) \ x(check_root, 29, PASS_ONLINE|PASS_FSCK) \
x(check_directory_structure, 30, PASS_FSCK) \ x(check_directory_structure, 30, PASS_ONLINE|PASS_FSCK) \
x(check_nlinks, 31, PASS_FSCK) \ x(check_nlinks, 31, PASS_FSCK) \
x(delete_dead_inodes, 32, PASS_FSCK|PASS_UNCLEAN) \ x(delete_dead_inodes, 32, PASS_FSCK|PASS_UNCLEAN) \
x(fix_reflink_p, 33, 0) \ x(fix_reflink_p, 33, 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