Skip to contentSkip to conversation

btrfs-progs: lowmem: bug fixes and inode_extref repair

PATCH Superseded [email protected] 2 participants 11 messages Sep 12, 2018 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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].

Su Yue (6):
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: optimization and repair for check_inode_extref()

 check/mode-lowmem.c | 148 +++++++++++++++++++++++++++++++++++---------
 ctree.h             |   6 +-
 inode-item.c        |   6 +-
 inode.c             |  14 ++++-
 4 files changed, 139 insertions(+), 35 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/6 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.

Fixes 260675657767 ("btrfs-progs: Import btrfs_insert/del/lookup_extref() functions.")
---
 ctree.h      | 6 +++---
 inode-item.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
ctree.h +3 -3
index 4719962df67d..e7f6c5df95f1 100644
@@ -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
index 1cc106670cd4..461557cb83d6 100644
@@ -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
#2 lore raw

PATCH 2/6 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.

Fixes 0cc75eddd093 ("btrfs-progs: Add btrfs_unlink() and btrfs_add_link() functions.")
---
 inode.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
inode.c +13 -1
index 2398bca4a109..598ad0ab6b4c 100644
@@ -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
#3 lore raw

PATCH 3/6 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.

Fixes 564901eac7a4 ("btrfs-progs: check: introduce print_dir_item_err()")
---
 check/mode-lowmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
check/mode-lowmem.c +1 -1
index 1bce44f5658a..89a304bbdd69 100644
@@ -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
#4 lore raw

PATCH 4/6 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
index 89a304bbdd69..8fc9edab1d66 100644
@@ -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
#5 lore raw

PATCH 5/6 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.

Fixes 5e2dc770471b ("btrfs-progs: check: skip shared node or leaf check for low_memory mode")
---
 check/mode-lowmem.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
check/mode-lowmem.c +12 -0
index 8fc9edab1d66..b6b33786d02b 100644
@@ -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
#6 lore raw

PATCH 5/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
index 8fc9edab1d66..b665c700328c 100644
@@ -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
#7 lore raw

PATCH 6/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.

Fixes 5e2dc770471b ("btrfs-progs: check: skip shared node or leaf check for low_memory mode")
---
 check/mode-lowmem.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
check/mode-lowmem.c +12 -0
index b665c700328c..bec2ee185cc8 100644
@@ -2677,6 +2677,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
#8 lore raw

PATCH 6/6 btrfs-progs: lowmem: optimization and repair for 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
index b6b33786d02b..bec2ee185cc8 100644
@@ -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
#9 lore raw

PATCH 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
new file mode 100644
index 0000000000000000000000000000000000000000..85f7a8211fe66dfa9b2b1f3a98e72f54cd402616
GIT binary patch
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
similarity index 100%
rename from tests/fsck-tests/009-no-dir-item-or-index/default_case.img
rename to tests/fsck-tests/009-no-dir-item-or-index/inode_ref_without_dir_item_and_index.img
Signature · 1 line
2.18.0
#10 lore raw
In reply to [email protected]

Sorry for the noise, Please ignore this patchset.
Will send v2.

Quoted 36 lines · On 2018/9/13 3:20 AM, [email protected] wrote:

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].

Su Yue (6):
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: optimization and repair for check_inode_extref()

  check/mode-lowmem.c | 148 +++++++++++++++++++++++++++++++++++---------
  ctree.h             |   6 +-
  inode-item.c        |   6 +-
  inode.c             |  14 ++++-
  4 files changed, 139 insertions(+), 35 deletions(-)