btrfs-progs: lowmem: bug fixes and inode_extref repair
v2 cover btrfs-progs: lowmem: bug fixes and inode_extref repair
From: Su Yue <[email protected]>
This patchset can be fetched from my github(based on v4.17.1):
https://github.com/Damenly/btrfs-progs/tree/lowmem_extref
The patchset aims to support check and repair errors about inode_extref in lowmem mode.
patch[1-2] let btrfs_unlink() detect inode_extref. patch[3] fixes a minor bug in check_dir_item due to my careless
long ago.
patch[4] fixes a bug about inconsistent path in check_fs_roots()
under repair.
patch[5] fixes a corner case about traversal of inode items. patch[6] enable inode_extref repair support and remove unnecessary
checks.
patch[7] add a test image, it can verify above patches except
patch[3].
Changelog:
v2:
Resend with patches in right order.
Su Yue (7):
btrfs-progs: adjust arguments of btrfs_lookup_inode_extref()
btrfs-progs: make btrfs_unlink() lookup inode_extref
btrfs-progs: lowmem check: find dir_item by di_key in check_dir_item()
btrfs-progs: lowmem: search key of root again after check_fs_root()
under repair
btrfs-progs: lowmem: continue to check item in last slot while
checking inodes
btrfs-progs: lowmem: improve check_inode_extref()
btrfs-progs: fsck-tests: add test case inode_extref without dir_item
and dir_index
check/mode-lowmem.c | 148 ++++++++++++++----
ctree.h | 6 +-
inode-item.c | 6 +-
inode.c | 14 +-
.../inode_extref_without_dir_item_index.img | Bin 0 -> 10240 bytes
... inode_ref_without_dir_item_and_index.img} | Bin
6 files changed, 139 insertions(+), 35 deletions(-)
create mode 100644 tests/fsck-tests/009-no-dir-item-or-index/inode_extref_without_dir_item_index.img
rename tests/fsck-tests/009-no-dir-item-or-index/{default_case.img => inode_ref_without_dir_item_and_index.img} (100%)Signature · 1 line
2.18.0
PATCH v2 1/7 btrfs-progs: adjust arguments of btrfs_lookup_inode_extref()
From: Su Yue <[email protected]>
The argument index is not used in btrfs_lookup_inode_extref(), so remove it.
And adjust positions its arguments to make it consistent with kernel part.
No functional change.
--- ctree.h | 6 +++--- inode-item.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
ctree.h +3 -3
@@ -2708,9 +2708,9 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, struct btrfs_key *location, int mod);struct btrfs_inode_extref *btrfs_lookup_inode_extref (struct btrfs_trans_handle - *trans,struct btrfs_path *path,struct btrfs_root *root, -u64 ino,u64 parent_ino,u64 index,const char *name, -int namelen,int ins_len); + *trans,struct btrfs_root *root,struct btrfs_path *path, +const char *name,int namelen,u64 ino,u64 parent_ino, +int ins_len); int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name, int name_len,
inode-item.c +3 -3
@@ -228,9 +228,9 @@ static int btrfs_find_name_in_ext_backref(struct btrfs_path *path, }struct btrfs_inode_extref *btrfs_lookup_inode_extref (struct btrfs_trans_handle - *trans,struct btrfs_path *path,struct btrfs_root *root, -u64 ino,u64 parent_ino,u64 index,const char *name, -int namelen,int ins_len) + *trans,struct btrfs_root *root,struct btrfs_path *path, +const char *name,int namelen,u64 ino,u64 parent_ino, +int ins_len) {struct btrfs_key key;struct btrfs_inode_extref *extref;
Signature · 1 line
2.18.0
Re: [PATCH v2 1/7] btrfs-progs: adjust arguments of btrfs_lookup_inode_extref()
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
The argument index is not used in
btrfs_lookup_inode_extref(), so remove it.
And adjust positions its arguments to make it consistent with kernel part.No functional change.
Fixes 260675657767 ("btrfs-progs: Import btrfs_insert/del/lookup_extref() functions.")Signed-off-by Su Yue <[email protected]>
Looks good, just small nitpick on coding style.
After fixing that, feel free to add my tag:
Quoted 10 lines
--- ctree.h | 6 +++--- inode-item.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)ctree.h +0
-0
ctree.h int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root · line 2711
Quoted 7 lines
ctree.h +1
-3@@ -2708,9 +2708,9 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, struct btrfs_key *location, int mod); struct btrfs_inode_extref *btrfs_lookup_inode_extref(struct btrfs_trans_handle - *trans,struct btrfs_path *path,struct btrfs_root *root, -u64 ino,u64 parent_ino,u64 index,const char *name, -int namelen,int ins_len); + *trans, struct btrfs_root *root, struct btrfs_path *path,
*trans and struct btrfs_trans_handle are in different lines, looks pretty strange.
It would be much better to keep them in the same line, just what kernel does.
Quoted 10 lines
ctree.h +2
-0+ const char *name, int namelen, u64 ino, u64 parent_ino, + int ins_len); int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name, int name_len,inode-item.c +0
-0
inode-item.c static int btrfs_find_name_in_ext_backref(struct btrfs_path *path, · line 231
Quoted 7 lines
inode-item.c +1
-3@@ -228,9 +228,9 @@ static int btrfs_find_name_in_ext_backref(struct btrfs_path *path, } struct btrfs_inode_extref *btrfs_lookup_inode_extref(struct btrfs_trans_handle - *trans,struct btrfs_path *path,struct btrfs_root *root, -u64 ino,u64 parent_ino,u64 index,const char *name, -int namelen,int ins_len) + *trans, struct btrfs_root *root, struct btrfs_path *path,
Same here.
Thanks,
Qu
Quoted 6 lines
inode-item.c +2
-0+ const char *name, int namelen, u64 ino, u64 parent_ino, + int ins_len) { struct btrfs_key key;struct btrfs_inode_extref *extref;
PATCH v2 2/7 btrfs-progs: make btrfs_unlink() lookup inode_extref
In reply to [email protected]From: Su Yue <[email protected]>
btrfs_unlink() uses btrfs_lookup_inode_ref() to look up inode_ref but forget inode_extref case.
Let btrfs_unlink() call btrfs_lookup_inode_extref() if inode_ref is found and EXTENDED_IREF feature is enabled.
--- inode.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
inode.c +13 -1
@@ -277,6 +277,7 @@ int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,struct btrfs_key key;struct btrfs_inode_item *inode_item;struct btrfs_inode_ref *inode_ref; +struct btrfs_inode_extref *inode_extref =NULL ;struct btrfs_dir_item *dir_item;u64 inode_size;u32 nlinks;
@@ -296,7 +297,18 @@ int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root, ret =PTR_ERR (inode_ref); goto out; } -if (inode_ref) + +if (!inode_ref &&btrfs_fs_incompat (root->fs_info,EXTENDED_IREF )) { +btrfs_release_path (path); + inode_extref =btrfs_lookup_inode_extref (trans, root, path, + name, namelen, ino, parent_ino,0 ); +if (IS_ERR (inode_extref)) { + ret =PTR_ERR (inode_extref); + goto out; + } + } + +if (inode_ref || inode_extref) del_inode_ref =1 ;btrfs_release_path (path);
Signature · 1 line
2.18.0
Re: [PATCH v2 2/7] btrfs-progs: make btrfs_unlink() lookup inode_extref
Quoted 9 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
btrfs_unlink()usesbtrfs_lookup_inode_ref()to look upinode_refbut forgetinode_extrefcase.Let
btrfs_unlink()callbtrfs_lookup_inode_extref()ifinode_refis found andEXTENDED_IREFfeature is enabled.Fixes 0cc75eddd093 ("btrfs-progs: Add btrfs_unlink() and btrfs_add_link() functions.")
My fault.
Indeed we need to check extref especially EXTENTD_IREF is already default mkfs features.
Thanks,
Qu
Quoted 38 lines
Signed-off-by Su Yue <[email protected]>--- inode.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)inode.c +13
-1@@ -277,6 +277,7 @@ int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,struct btrfs_key key;struct btrfs_inode_item *inode_item;struct btrfs_inode_ref *inode_ref; +struct btrfs_inode_extref *inode_extref =NULL ;struct btrfs_dir_item *dir_item;u64 inode_size;u32 nlinks;@@ -296,7 +297,18 @@ int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root, ret =PTR_ERR (inode_ref); goto out; } -if (inode_ref) + +if (!inode_ref &&btrfs_fs_incompat (root->fs_info,EXTENDED_IREF )) { +btrfs_release_path (path); + inode_extref =btrfs_lookup_inode_extref (trans, root, path, + name, namelen, ino, parent_ino,0 ); +if (IS_ERR (inode_extref)) { + ret =PTR_ERR (inode_extref); + goto out; + } + } + +if (inode_ref || inode_extref) del_inode_ref =1 ;btrfs_release_path (path);
PATCH v2 3/7 btrfs-progs: lowmem check: find dir_item by di_key in check_dir_item()
In reply to [email protected]From: Su Yue <[email protected]>
In check_dir_item, we are going to search corresponding dir_item/index.
Commit 564901eac7a4 ("btrfs-progs: check: introduce print_dir_item_err()") Changed argument name from key to di_key but forgot to change the key name for dir_item search.
So @key shouldn't be used here. It should be @di_key.
--- check/mode-lowmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
check/mode-lowmem.c +1 -1
@@ -1658,7 +1658,7 @@ begin:/* check relative INDEX/ITEM */ key.objectid = di_key->objectid; -if (key.type ==BTRFS_DIR_ITEM_KEY ) { +if (di_key->type ==BTRFS_DIR_ITEM_KEY ) { key.type =BTRFS_DIR_INDEX_KEY ; key.offset = index; }else {
Signature · 1 line
2.18.0
Re: [PATCH v2 3/7] btrfs-progs: lowmem check: find dir_item by di_key in check_dir_item()
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_dir_item, we are going to search corresponding dir_item/index.Commit
564901eac7a4("btrfs-progs: check: introduceprint_dir_item_err()") Changed argument name from key todi_keybut forgot to change the key name fordir_itemsearch.
So @key shouldn't be used here. It should be @di_key.Fixes 564901eac7a4 ("btrfs-progs: check: introduce print_dir_item_err()")
Which also forgot to modify comment about parameters.
Quoted 10 lines
Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)check/mode-lowmem.c +0
-0
check/mode-lowmem.c begin: · line 1661
Quoted 5 lines
check/mode-lowmem.c +1
-1@@ -1658,7 +1658,7 @@ begin:/* check relative INDEX/ITEM */ key.objectid = di_key->objectid; -if (key.type ==BTRFS_DIR_ITEM_KEY ) { +if (di_key->type ==BTRFS_DIR_ITEM_KEY ) {
To avoid such problem, I recommend to rename @key to @search_key, and move the declaration inside the while loop.
Thanks,
Qu
Quoted 4 lines
check/mode-lowmem.c +0
-0key.type =BTRFS_DIR_INDEX_KEY ; key.offset = index; }else {
Re: [PATCH v2 3/7] btrfs-progs: lowmem check: find dir_item by di_key in check_dir_item()
Quoted 8 lines · Qu Wenruo
Quoted 5 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_dir_item, we are going to search corresponding dir_item/index.
Quoted 8 lines
Quoted 6 lines
Commit
564901eac7a4("btrfs-progs: check: introduceprint_dir_item_err()") Changed argument name from key todi_keybut forgot to change the key name fordir_itemsearch.
So @key shouldn't be used here. It should be @di_key.Fixes 564901eac7a4 ("btrfs-progs: check: introduce print_dir_item_err()")Which also forgot to modify comment about parameters.
Oh....Noticed
Quoted 12 lines
Quoted 12 lines
Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)check/mode-lowmem.c +0
-0@@ -1658,7 +1658,7 @@ begin:
Quoted 8 lines
check/mode-lowmem.c begin: · line 1661
Quoted 4 lines
check/mode-lowmem.c +1
-1/* check relative INDEX/ITEM */ key.objectid = di_key->objectid; -if (key.type ==BTRFS_DIR_ITEM_KEY ) { +if (di_key->type ==BTRFS_DIR_ITEM_KEY ) {To avoid such problem, I recommend to rename @key to @search_key, and move the declaration inside the while loop.
Nice suggestion. Will do it in next version.
Thanks,
Su
Quoted 9 lines
Thanks,
QuQuoted 4 lines
check/mode-lowmem.c +0
-0key.type =BTRFS_DIR_INDEX_KEY ; key.offset = index; }else {
PATCH v2 4/7 btrfs-progs: lowmem: search key of root again after check_fs_root() under repair
In reply to [email protected]From: Su Yue <[email protected]>
In check_fs_roots_lowmem(), we do search and follow the resulted path to call check_fs_root(), then call btrfs_next_item() to check next root.
However, if repair is enabled, the root tree can be cowed, the existed path can cause strange errors.
Solution:
If repair, save the key before calling check_fs_root, search the saved key again before checking next root.
--- check/mode-lowmem.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
check/mode-lowmem.c +15 -0
@@ -4967,9 +4967,13 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) }while (1 ) { +struct btrfs_key saved_key; + node = path.nodes[0 ]; slot = path.slots[0 ];btrfs_item_key_to_cpu (node, &key, slot); +if (repair) + saved_key = key;if (key.objectid >BTRFS_LAST_FREE_OBJECTID ) goto out; if (key.type == BTRFS_ROOT_ITEM_KEY &&
@@ -5000,6 +5004,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; } next: +/* +* Since root tree can be cowed during repair, +* here search the saved key again. +*/ +if (repair) { +btrfs_release_path (&path); + ret =btrfs_search_slot (NULL , fs_info->tree_root, + &saved_key, &path,0 ,0 ); +/* Repair never deletes trees, search must succeed. */ +BUG_ON (ret); + } ret =btrfs_next_item (tree_root, &path);if (ret >0 ) goto out;
Signature · 1 line
2.18.0
Re: [PATCH v2 4/7] btrfs-progs: lowmem: search key of root again after check_fs_root() under repair
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_fs_roots_lowmem(), we do search and follow the resulted path to callcheck_fs_root(), then callbtrfs_next_item()to check next root.
However, if repair is enabled, the root tree can be cowed, the existed path can cause strange errors.Solution:
If repair, save the key before callingcheck_fs_root, search the saved key again before checking next root.
Both reason and solution looks good.
Quoted 28 lines
Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)check/mode-lowmem.c +4
-0@@ -4967,9 +4967,13 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) }while (1 ) { +struct btrfs_key saved_key; + node = path.nodes[0 ]; slot = path.slots[0 ];btrfs_item_key_to_cpu (node, &key, slot); +if (repair) + saved_key = key;if (key.objectid >BTRFS_LAST_FREE_OBJECTID ) goto out; if (key.type == BTRFS_ROOT_ITEM_KEY &&@@ -5000,6 +5004,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; } next:
check/mode-lowmem.c int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) · line 5016
Quoted 10 lines
check/mode-lowmem.c +10
-0+/* +* Since root tree can be cowed during repair, +* here search the saved key again. +*/ +if (repair) { +btrfs_release_path (&path); + ret =btrfs_search_slot (NULL , fs_info->tree_root, + &saved_key, &path,0 ,0 ); +/* Repair never deletes trees, search must succeed. */ +BUG_ON (ret);
But this doesn't look good to me.
Your assumption here is valid (at least for now), but it's possible that some tree blocks get corrupted in a large root tree, and in that case, we could still read part of the root tree, but btrfs_search_slot() could still return -EIO for certain search key.
So I still prefer to do some error handling other than BUG_ON(ret).
Thanks,
Qu
Quoted 5 lines
check/mode-lowmem.c +1
-0+ } ret =btrfs_next_item (tree_root, &path);if (ret >0 ) goto out;
Re: [PATCH v2 4/7] btrfs-progs: lowmem: search key of root again after check_fs_root() under repair
Quoted 58 lines · Qu Wenruo
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_fs_roots_lowmem(), we do search and follow the resulted path to callcheck_fs_root(), then callbtrfs_next_item()to check next root.
However, if repair is enabled, the root tree can be cowed, the existed path can cause strange errors.Solution:
If repair, save the key before callingcheck_fs_root, search the saved key again before checking next root.Both reason and solution looks good.
Quoted 28 lines
Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)check/mode-lowmem.c +4
-0@@ -4967,9 +4967,13 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) }while (1 ) { +struct btrfs_key saved_key; + node = path.nodes[0 ]; slot = path.slots[0 ];btrfs_item_key_to_cpu (node, &key, slot); +if (repair) + saved_key = key;if (key.objectid >BTRFS_LAST_FREE_OBJECTID ) goto out; if (key.type == BTRFS_ROOT_ITEM_KEY &&@@ -5000,6 +5004,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; } next:check/mode-lowmem.c int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) · line 5016
Quoted 10 lines
check/mode-lowmem.c +10
-0+/* +* Since root tree can be cowed during repair, +* here search the saved key again. +*/ +if (repair) { +btrfs_release_path (&path); + ret =btrfs_search_slot (NULL , fs_info->tree_root, + &saved_key, &path,0 ,0 ); +/* Repair never deletes trees, search must succeed. */ +BUG_ON (ret);But this doesn't look good to me.
Quoted 7 lines
Your assumption here is valid (at least for now), but it's possible that some tree blocks get corrupted in a large root tree, and in that case, we could still read part of the root tree, but
btrfs_search_slot()could still return-EIOfor certain search key.So I still prefer to do some error handling other than
BUG_ON(ret).
Okay, will try it.
Thanks,
Su
Quoted 10 lines
Thanks,
QuQuoted 5 lines
check/mode-lowmem.c +1
-0+ } ret =btrfs_next_item (tree_root, &path);if (ret >0 ) goto out;
Re: [PATCH v2 4/7] btrfs-progs: lowmem: search key of root again after check_fs_root() under repair
Quoted 62 lines · Su Yue
Quoted 59 lines · Qu Wenruo
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_fs_roots_lowmem(), we do search and follow the resulted path to callcheck_fs_root(), then callbtrfs_next_item()to check next root.
However, if repair is enabled, the root tree can be cowed, the existed path can cause strange errors.Solution:
If repair, save the key before callingcheck_fs_root, search the saved key again before checking next root.Both reason and solution looks good.
Quoted 31 lines
Signed-off-by Su Yue <[email protected]>---
check/mode-lowmem.c| 15 +++++++++++++++1 file changed, 15 insertions(+)check/mode-lowmem.c +4
-0@@ -4967,9 +4967,13 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) } while (1) { + struct btrfs_key saved_key; + node = path.nodes[0]; slot = path.slots[0]; btrfs_item_key_to_cpu(node, &key, slot); + if (repair) + saved_key = key; if (key.objectid > BTRFS_LAST_FREE_OBJECTID) goto out;if (key.type ==
BTRFS_ROOT_ITEM_KEY&&check/mode-lowmem.c +0
-0@@ -5000,6 +5004,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; }next:
+ /*
+ * Since root tree can be cowed during repair,Quoted 8 lines
+ * here search the saved key again.
+ */
+ if (repair) {
+btrfs_release_path(&path);
+ ret =btrfs_search_slot(NULL, fs_info->tree_root, + &saved_key, &path, 0, 0);
+ /* Repair never deletes trees, search must succeed. */ +BUG_ON(ret);But this doesn't look good to me.
Quoted 8 lines
Quoted 7 lines
Your assumption here is valid (at least for now), but it's possible that some tree blocks get corrupted in a large root tree, and in that case, we could still read part of the root tree, but
btrfs_search_slot()could still return-EIOfor certain search key.So I still prefer to do some error handling other than
BUG_ON(ret).Okay, will try it.
Just to emphasize Qu's point - we should strive to remove existing BUG_ON and should never introduce new ones. btrfs-progs is already quite messy and we should be improving that.
Quoted 16 lines
Thanks,
SuQuoted 10 lines
Thanks,
QuQuoted 5 lines
+ }
ret =btrfs_next_item(tree_root, &path); if (ret > 0)
goto out;
Re: [PATCH v2 4/7] btrfs-progs: lowmem: search key of root again after check_fs_root() under repair
Quoted 70 lines · Nikolay Borisov
Quoted 67 lines · Su Yue
Quoted 64 lines · Qu Wenruo
Quoted 11 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
In
check_fs_roots_lowmem(), we do search and follow the resulted path to callcheck_fs_root(), then callbtrfs_next_item()to check next root.
However, if repair is enabled, the root tree can be cowed, the existed path can cause strange errors.Solution:
If repair, save the key before callingcheck_fs_root, search the saved key again before checking next root.Both reason and solution looks good.
Quoted 29 lines
Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)check/mode-lowmem.c +4
-0@@ -4967,9 +4967,13 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) } while (1) { + struct btrfs_key saved_key; + node = path.nodes[0]; slot = path.slots[0]; btrfs_item_key_to_cpu(node, &key, slot); + if (repair) + saved_key = key; if (key.objectid > BTRFS_LAST_FREE_OBJECTID) goto out; if (key.type == BTRFS_ROOT_ITEM_KEY &&@@ -5000,6 +5004,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) err |= ret; } next:check/mode-lowmem.c int check_fs_roots_lowmem(struct btrfs_fs_info · line 5017
Quoted 10 lines
check/mode-lowmem.c +10
-0+ /* + * Since root tree can be cowed during repair, + * here search the saved key again. + */ + if (repair) { + btrfs_release_path(&path); + ret = btrfs_search_slot(NULL, fs_info->tree_root, + &saved_key, &path, 0, 0); + /* Repair never deletes trees, search must succeed. */ + BUG_ON(ret);But this doesn't look good to me.
Your assumption here is valid (at least for now), but it's possible that some tree blocks get corrupted in a large root tree, and in that case, we could still read part of the root tree, but
btrfs_search_slot()could still return-EIOfor certain search key.
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
So I still prefer to do some error handling other than
BUG_ON(ret).Okay, will try it.
Just to emphasize Qu's point - we should strive to remove existing
BUG_ONand should never introduce new ones. btrfs-progs is already quite messy and we should be improving that.
Understood, thanks for your emphasis.
Su
Quoted 18 lines
Quoted 16 lines
Thanks,
SuQuoted 10 lines
Thanks,
QuQuoted 5 lines
check/mode-lowmem.c +1
-0+ } ret = btrfs_next_item(tree_root, &path); if (ret > 0)goto out;
PATCH v2 5/7 btrfs-progs: lowmem: continue to check item in last slot while checking inodes
In reply to [email protected]From: Su Yue <[email protected]>
After call of check_inode_item(), path may point to the last unchecked slot of the leaf. The outer walk_up_tree() always treats the position as checked item then skips to next item.
If the last item was an inode item, yes, it was unchecked. Then walk_up_tree() will think the leaf is checked and walk up to upper node, process_one_leaf() in walk_down_tree() would skip to check next inode item. Which means, the inode item won't be checked.
Solution:
After check_inode_item returns, if found path point to the last item of a leaf, decrease path slot manually, so walk_up_tree() will stay on the leaf.
--- check/mode-lowmem.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
check/mode-lowmem.c +12 -0
@@ -2612,6 +2612,18 @@ again:if (cur->start == cur_bytenr) goto again; +/* +* path may point at the last item(a inode item maybe) in a leaf. +* Without below lines, walk_up_tree() will skip the item which +* means all items related to the inode will never be checked. +* Decrease the slot manually, walk_up_tree won't skip to next node +* if it occurs. +*/ +if (path->slots[0 ] +1 >=btrfs_header_nritems (path->nodes[0 ])) { +if (path->slots[0 ]) + path->slots[0 ]--; + } +/* * we have switched to another leaf, above nodes may * have changed, here walk down the path, if a node
Signature · 1 line
2.18.0
Re: [PATCH v2 5/7] btrfs-progs: lowmem: continue to check item in last slot while checking inodes
Quoted 5 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
After call of
check_inode_item(), path may point to the last unchecked slot of the leaf. The outerwalk_up_tree()always treats the position as checked item then skips to next item.
So check_inode_item() always set its path to *unchecked* slot, while walk_up_tree() always think its path is set to *checked* slot.
Then this is indeed a problem.
Quoted 9 lines
If the last item was an inode item, yes, it was unchecked. Then
walk_up_tree()will think the leaf is checked and walk up to upper node,process_one_leaf()inwalk_down_tree()would skip to check next inode item. Which means, the inode item won't be checked.Solution:
Aftercheck_inode_itemreturns, if found path point to the last item of a leaf,
Would you please explain more about why last item makes a difference here?
Quoted 1 line
From previous statement, it looks like it's the difference in how
walk_up_tree() and check_inode_item() handles the path. Not really related to last item.
Or did I miss something?
Thanks,
Qu
Quoted 33 lines
decrease path slot manually, so
walk_up_tree()will stay on the leaf.Fixes 5e2dc770471b ("btrfs-progs: check: skip shared node or leaf check for low_memory mode")Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)check/mode-lowmem.c +12
-0@@ -2612,6 +2612,18 @@ again:if (cur->start == cur_bytenr) goto again; +/* +* path may point at the last item(a inode item maybe) in a leaf. +* Without below lines, walk_up_tree() will skip the item which +* means all items related to the inode will never be checked. +* Decrease the slot manually, walk_up_tree won't skip to next node +* if it occurs. +*/ +if (path->slots[0 ] +1 >=btrfs_header_nritems (path->nodes[0 ])) { +if (path->slots[0 ]) + path->slots[0 ]--; + } +/* * we have switched to another leaf, above nodes may * have changed, here walk down the path, if a node
Re: [PATCH v2 5/7] btrfs-progs: lowmem: continue to check item in last slot while checking inodes
Quoted 5 lines · Qu Wenruo
Quoted 2 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
Quoted 9 lines
Quoted 3 lines
After call of
check_inode_item(), path may point to the last unchecked slot of the leaf. The outerwalk_up_tree()always treats the position as checked item then skips to next item.So
check_inode_item()always set its path to *unchecked* slot, whilewalk_up_tree()always think its path is set to *checked* slot.Then this is indeed a problem.
Yep, that's the case.
Quoted 10 lines
Quoted 9 lines
If the last item was an inode item, yes, it was unchecked. Then
walk_up_tree()will think the leaf is checked and walk up to upper node,process_one_leaf()inwalk_down_tree()would skip to check next inode item. Which means, the inode item won't be checked.Solution:
Aftercheck_inode_itemreturns, if found path point to the last item of a leaf,
Quoted 6 lines
Would you please explain more about why last item makes a difference here?
From previous statement, it looks like it's the difference in how
walk_up_tree()andcheck_inode_item()handles the path. Not really related to last item.
Yes, the change is tricky. The core problem is walk_up_tree() will skip to other nodes. Decreament of slot will let path still point to the leaf after walk_up_tree() returns.
Or we must change logical of walk_up_tree() or check_inode_item().
BTW,now process_one_leaf() checks inode_item from first inode_item or second item with different ino. Change it start from last slot should be the right way.
Thanks,
Su
Thanks,
Su
Quoted 40 lines
Or did I miss something?
Thanks,
QuQuoted 33 lines
decrease path slot manually, so
walk_up_tree()will stay on the leaf.Fixes 5e2dc770471b ("btrfs-progs: check: skip shared node or leaf check for low_memory mode")Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)check/mode-lowmem.c +12
-0@@ -2612,6 +2612,18 @@ again:if (cur->start == cur_bytenr) goto again; +/* +* path may point at the last item(a inode item maybe) in a leaf. +* Without below lines, walk_up_tree() will skip the item which +* means all items related to the inode will never be checked. +* Decrease the slot manually, walk_up_tree won't skip to next node +* if it occurs. +*/ +if (path->slots[0 ] +1 >=btrfs_header_nritems (path->nodes[0 ])) { +if (path->slots[0 ]) + path->slots[0 ]--; + } +/* * we have switched to another leaf, above nodes may * have changed, here walk down the path, if a node
PATCH v2 6/7 btrfs-progs: lowmem: improve check_inode_extref()
In reply to [email protected]From: Su Yue <[email protected]>
inode_extref is much similar with inode_ref, most codes are reused in check_inode_extref().
Exception:
There is no need to check root directory, so remove it. Make check_inode_extref() verify hash value with key offset now.
And lowmem check can detect errors about inode_extref and try to repair errors.
--- check/mode-lowmem.c | 119 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 27 deletions(-)
check/mode-lowmem.c +92 -27
@@ -1182,37 +1182,79 @@ out:* * Return 0 if no error occurred. */ -static int check_inode_extref (struct btrfs_root *root, -struct btrfs_key *ref_key, -struct extent_buffer *node,int slot,u64 *refs, -int mode) +static int check_inode_extref (struct btrfs_root *root,struct btrfs_key *ref_key, +struct btrfs_path *path,char *name_ret, +u32 *namelen_ret,u64 *refs_ret,int mode) {struct btrfs_key key;struct btrfs_key location;struct btrfs_inode_extref *extref; +struct extent_buffer *node;char namebuf[BTRFS_NAME_LEN ] = {0 };u32 total; -u32 cur =0 ; +u32 cur;u32 len;u32 name_len;u64 index;u64 parent; +int err; +int tmp_err;int ret; -int err =0 ; +int slot; +u64 refs; +bool search_again = false; location.objectid = ref_key->objectid; location.type =BTRFS_INODE_ITEM_KEY ; location.offset =0 ; +begin: + cur =0 ; + err =0 ; + refs = *refs_ret; + *namelen_ret =0 ; + +/* since after repair, path and the dir item may be changed */ +if (search_again) { + search_again = false; +btrfs_release_path (path); + ret =btrfs_search_slot (NULL , root, ref_key, path,0 ,0 ); +/* +* The item was deleted, let the path point to the last checked +* item. +*/ +if (ret >0 ) { +if (path->slots[0 ] ==0 ) { + ret =btrfs_prev_leaf (root, path); +/* +* we are checking the inode item, there must +* be some items before, the case shall never +* happen. +*/ +BUG_ON (ret); + }else { + path->slots[0 ]--; + } + goto out; + }else if (ret <0 ) { + err |= ret; + goto out; + } + } + + node = path->nodes[0 ]; + slot = path->slots[0 ]; extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref); total =btrfs_item_size_nr (node, slot); -next: -/* update inode ref count */ - (*refs)++; - name_len =btrfs_inode_extref_name_len (node, extref); - index =btrfs_inode_extref_index (node, extref); +loop: +/* Update inode ref count */ + refs++; + tmp_err =0 ; parent =btrfs_inode_extref_parent (node, extref); + index =btrfs_inode_extref_index (node, extref); + name_len =btrfs_inode_extref_name_len (node, extref); +if (name_len <=BTRFS_NAME_LEN ) { len = name_len; }else {
@@ -1220,37 +1262,60 @@ next:warning ("root %llu INODE_EXTREF[%llu %llu] name too long" , root->objectid, ref_key->objectid, ref_key->offset); } +read_extent_buffer (node, namebuf, (unsigned long )(extref +1 ), len); -/* Check root dir ref name */ -if (index ==0 &&strncmp (namebuf,".." , name_len)) { -error ("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s" , +/* verify hash value */ +if (ref_key->offset !=btrfs_extref_hash (parent, namebuf, len)) { + err |= -EIO ; +error ("root %llu INODE_EXTREF[%llu %llu] name %s namelen %u mode %d mismatch with its hash, wanted %llu have %llu" , root->objectid, ref_key->objectid, ref_key->offset, - namebuf); - err |=ROOT_DIR_ERROR ; + namebuf, len, mode, ref_key->offset, +btrfs_extref_hash (parent, namebuf, len)); + goto out; } -/* find related dir_index */ +/* copy the first name found to name_ret */ +if (refs ==1 && name_ret) { +memcpy (name_ret, namebuf, len); + *namelen_ret = len; + } + +/* Find related DIR_INDEX */ key.objectid = parent; key.type =BTRFS_DIR_INDEX_KEY ; key.offset = index; - ret =find_dir_item (root, &key, &location, namebuf, len, mode); - err |= ret; + tmp_err |=find_dir_item (root, &key, &location, namebuf, len, +imode_to_type (mode)); -/* find related dir_item */ +/* Find related dir_item */ key.objectid = parent; key.type =BTRFS_DIR_ITEM_KEY ; key.offset =btrfs_name_hash (namebuf, len); - ret =find_dir_item (root, &key, &location, namebuf, len, mode); - err |= ret; + tmp_err |=find_dir_item (root, &key, &location, namebuf, len, +imode_to_type (mode)); + +if (tmp_err && repair) { + ret =repair_ternary_lowmem (root, parent, ref_key->objectid, + index, namebuf, name_len,imode_to_type (mode), + tmp_err); +if (!ret) { + search_again = true; + goto begin; + } + } +print_inode_ref_err (root, ref_key, index, namebuf, name_len, +imode_to_type (mode), tmp_err); + + err |= tmp_err; len =sizeof (*extref) + name_len; extref = (struct btrfs_inode_extref *)((char *)extref + len); cur += len; -if (cur < total) - goto next; - + goto loop; +out: + *refs_ret = refs;return err; }
@@ -2426,8 +2491,8 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path)warning ("root %llu EXTREF[%llu %llu] isn't supported" , root->objectid, key.objectid, key.offset); - ret =check_inode_extref (root, &key, node, slot, &refs, - mode); + ret =check_inode_extref (root, &key, path, namebuf, + &name_len, &refs, mode); err |= ret;break ; }
Signature · 1 line
2.18.0
Re: [PATCH v2 6/7] btrfs-progs: lowmem: improve check_inode_extref()
Quoted 68 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
inode_extrefis much similar withinode_ref, most codes are reused incheck_inode_extref().
Exception:
There is no need to check root directory, so remove it. Makecheck_inode_extref()verify hash value with key offset now.And lowmem check can detect errors about
inode_extrefand try to repair errors.Signed-off-by Su Yue <[email protected]>--- check/mode-lowmem.c | 119 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 27 deletions(-)check/mode-lowmem.c +23
-6@@ -1182,37 +1182,79 @@ out:* * Return 0 if no error occurred. */ -static int check_inode_extref (struct btrfs_root *root, -struct btrfs_key *ref_key, -struct extent_buffer *node,int slot,u64 *refs, -int mode) +static int check_inode_extref (struct btrfs_root *root,struct btrfs_key *ref_key, +struct btrfs_path *path,char *name_ret, +u32 *namelen_ret,u64 *refs_ret,int mode) {struct btrfs_key key;struct btrfs_key location;struct btrfs_inode_extref *extref; +struct extent_buffer *node;char namebuf[BTRFS_NAME_LEN ] = {0 };u32 total; -u32 cur =0 ; +u32 cur;u32 len;u32 name_len;u64 index;u64 parent; +int err; +int tmp_err;int ret; -int err =0 ; +int slot; +u64 refs; +bool search_again = false; location.objectid = ref_key->objectid; location.type =BTRFS_INODE_ITEM_KEY ; location.offset =0 ; +begin: + cur =0 ; + err =0 ; + refs = *refs_ret; + *namelen_ret =0 ; + +/* since after repair, path and the dir item may be changed */ +if (search_again) { + search_again = false; +btrfs_release_path (path); + ret =btrfs_search_slot (NULL , root, ref_key, path,0 ,0 ); +/* +* The item was deleted, let the path point to the last checked
check/mode-lowmem.c out: · line 1232
Quoted 10 lines
check/mode-lowmem.c +10
-0+* item. +*/ +if (ret >0 ) { +if (path->slots[0 ] ==0 ) { + ret =btrfs_prev_leaf (root, path); +/* +* we are checking the inode item, there must +* be some items before, the case shall never +* happen. +*/
This assumption looks valid by a quick glance, but doesn't stand in the following case:
1) It's really the first INODE_ITEM.
2) Previous leaf/node is corrupted.
So we still need some error handling for it.
Quoted 36 lines
check/mode-lowmem.c +20
-5+BUG_ON (ret); + }else { + path->slots[0 ]--; + } + goto out; + }else if (ret <0 ) { + err |= ret; + goto out; + } + } + + node = path->nodes[0 ]; + slot = path->slots[0 ]; extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref); total =btrfs_item_size_nr (node, slot); -next: -/* update inode ref count */ - (*refs)++; - name_len =btrfs_inode_extref_name_len (node, extref); - index =btrfs_inode_extref_index (node, extref); +loop: +/* Update inode ref count */ + refs++; + tmp_err =0 ; parent =btrfs_inode_extref_parent (node, extref); + index =btrfs_inode_extref_index (node, extref); + name_len =btrfs_inode_extref_name_len (node, extref); +if (name_len <=BTRFS_NAME_LEN ) { len = name_len; }else {@@ -1220,37 +1262,60 @@ next:warning ("root %llu INODE_EXTREF[%llu %llu] name too long" , root->objectid, ref_key->objectid, ref_key->offset);
check/mode-lowmem.c next: · line 1270
Quoted 10 lines
check/mode-lowmem.c +4
-3} +read_extent_buffer (node, namebuf, (unsigned long )(extref +1 ), len); -/* Check root dir ref name */ -if (index ==0 &&strncmp (namebuf,".." , name_len)) { - error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s", +/* verify hash value */ +if (ref_key->offset !=btrfs_extref_hash (parent, namebuf, len)) { + err |= -EIO ;
It's never a good idea to mix minus error number with bitmap errors.
Thanks,
Qu
Quoted 74 lines
check/mode-lowmem.c +35
-13+error ("root %llu INODE_EXTREF[%llu %llu] name %s namelen %u mode %d mismatch with its hash, wanted %llu have %llu" , root->objectid, ref_key->objectid, ref_key->offset, - namebuf); - err |=ROOT_DIR_ERROR ; + namebuf, len, mode, ref_key->offset, +btrfs_extref_hash (parent, namebuf, len)); + goto out; } -/* find related dir_index */ +/* copy the first name found to name_ret */ +if (refs ==1 && name_ret) { +memcpy (name_ret, namebuf, len); + *namelen_ret = len; + } + +/* Find related DIR_INDEX */ key.objectid = parent; key.type =BTRFS_DIR_INDEX_KEY ; key.offset = index; - ret =find_dir_item (root, &key, &location, namebuf, len, mode); - err |= ret; + tmp_err |=find_dir_item (root, &key, &location, namebuf, len, +imode_to_type (mode)); -/* find related dir_item */ +/* Find related dir_item */ key.objectid = parent; key.type =BTRFS_DIR_ITEM_KEY ; key.offset =btrfs_name_hash (namebuf, len); - ret =find_dir_item (root, &key, &location, namebuf, len, mode); - err |= ret; + tmp_err |=find_dir_item (root, &key, &location, namebuf, len, +imode_to_type (mode)); + +if (tmp_err && repair) { + ret =repair_ternary_lowmem (root, parent, ref_key->objectid, + index, namebuf, name_len,imode_to_type (mode), + tmp_err); +if (!ret) { + search_again = true; + goto begin; + } + } +print_inode_ref_err (root, ref_key, index, namebuf, name_len, +imode_to_type (mode), tmp_err); + + err |= tmp_err; len =sizeof (*extref) + name_len; extref = (struct btrfs_inode_extref *)((char *)extref + len); cur += len; -if (cur < total) - goto next; - + goto loop; +out: + *refs_ret = refs;return err; }@@ -2426,8 +2491,8 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path)warning ("root %llu EXTREF[%llu %llu] isn't supported" , root->objectid, key.objectid, key.offset); - ret =check_inode_extref (root, &key, node, slot, &refs, - mode); + ret =check_inode_extref (root, &key, path, namebuf, + &name_len, &refs, mode); err |= ret;break ; }
PATCH v2 7/7 btrfs-progs: fsck-tests: add test case inode_extref without dir_item and dir_index
In reply to [email protected]From: Su Yue <[email protected]>
This case contains an inode_extref:
==========================================
...
item 1 key (257 INODE_EXTREF 3460996356) itemoff 3947 itemsize 24
index 257 parent 256 namelen 6 name: foo255
...
==========================================
The related dir_item and dir_index are missing.
Add the case to ensure both original and lowmem mode check can handle the case of inode_extref.
Lowmem part is supported since patch named
'btrfs-progs: lowmem: improve check_inode_extref()'.
And rename default_case.img to inode_ref_without_dir_item_index.img.
---
.../inode_extref_without_dir_item_index.img | Bin 0 -> 10240 bytes
...=> inode_ref_without_dir_item_and_index.img} | Bin
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 tests/fsck-tests/009-no-dir-item-or-index/inode_extref_without_dir_item_index.img
rename tests/fsck-tests/009-no-dir-item-or-index/{default_case.img => inode_ref_without_dir_item_and_index.img} (100%)tests/fsck-tests/009-no-dir-item-or-index/inode_extref_without_dir_item_index.img new file binary +0 -0
tests/fsck-tests/009-no-dir-item-or-index/inode_ref_without_dir_item_and_index.img renamed from tests/fsck-tests/009-no-dir-item-or-index/default_case.img +0 -0
Signature · 1 line
2.18.0
Re: [PATCH v2 7/7] btrfs-progs: fsck-tests: add test case inode_extref without dir_item and dir_index
Quoted 17 lines · On 2018/9/13 上午4:49, [email protected] wrote:
From: Su Yue <[email protected]>
This case contains an
inode_extref:
==========================================
...
item 1 key (257INODE_EXTREF3460996356) itemoff 3947 itemsize 24index 257 parent 256 namelen 6 name: foo255...
==========================================
The relateddir_itemanddir_indexare missing.Add the case to ensure both original and lowmem mode check can handle the case of
inode_extref.Lowmem part is supported since patch named
'btrfs-progs: lowmem: improvecheck_inode_extref()'.
Quoted 8 lines
And rename default_case.img to inode_ref_without_dir_item_index.img.
Signed-off-by Su Yue <[email protected]>--- .../inode_extref_without_dir_item_index.img | Bin 0 -> 10240 bytes ...=> inode_ref_without_dir_item_and_index.img} | Bin 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/009-no-dir-item-or-index/inode_extref_without_dir_item_index.img
Checked the image, it's normal INODE_REF with INODE_EXTREF. Looks good by itself.
It could be even better, by providing image with INODE_EXTREF only without any INODE_REF.
Thanks,
Qu
Quoted 175 lines
rename tests/fsck-tests/009-no-dir-item-or-index/{default_case.img => inode_ref_without_dir_item_and_index.img} (100%)tests/fsck-tests/009-no-dir-item-or-index/inode_extref_without_dir_item_index.img new file binary +0
-0tests/fsck-tests/009-no-dir-item-or-index/inode_ref_without_dir_item_and_index.img renamed from tests/fsck-tests/009-no-dir-item-or-index/default_case.img +0
-0