Skip to contentSkip to conversation

fix BUG_ON and retun real error in find_next_devid() and clone_fs_devices()

PATCH In review Anand Jain 5 participants 13 messages Aug 27, 2019 – Sep 10, 2019 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

cover fix BUG_ON and retun real error in find_next_devid() and clone_fs_devices()

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Anand Jain (2):
btrfs: fix BUG_ON with proper error handle in find_next_devid
btrfs: fix error return on alloc fail in clone_fs_devices

 fs/btrfs/volumes.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0 (Apple Git-120)
author
#1 lore raw

PATCH 1/2 btrfs: fix BUG_ON with proper error handle in find_next_devid

In a corrupted tree if search for next devid finds the device with devid = -1, then report the error -EUCLEAN back to the parent function to fail gracefully.

---
 fs/btrfs/volumes.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +6 -1
index 4db4a100c05b..36aa5f79fb6c 100644
@@ -1849,7 +1849,12 @@ static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
 	if (ret < 0)
 		goto error;
 
-	BUG_ON(ret == 0); /* Corruption */
+	if (ret == 0) {
+		/* Corruption */
+		btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
+		ret = -EUCLEAN;
+		goto error;
+	}
 
 	ret = btrfs_previous_item(fs_info->chunk_root, path,
 				  BTRFS_DEV_ITEMS_OBJECTID,
Signature · 1 line
2.21.0 (Apple Git-120)
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] btrfs: fix BUG_ON with proper error handle in find_next_devid

Signature · 8 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5
90409 Nürnberg
Germany
(HRB 247165, AG München)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#3 lore raw

Re: [PATCH 1/2] btrfs: fix BUG_ON with proper error handle in find_next_devid

In reply to Anand Jain
Quoted 14 lines · Anand Jain

In a corrupted tree if search for next devid finds the device with devid = -1, then report the error -EUCLEAN back to the parent function to fail gracefully.

---
 fs/btrfs/volumes.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +0 -0
index 4db4a100c05b..36aa5f79fb6c 100644

fs/btrfs/volumes.c static noinline int find_next_devid(struct btrfs_fs_info *fs_info, · line 1854

Quoted 7 lines
fs/btrfs/volumes.c +3 -1
@@ -1849,7 +1849,12 @@ static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
 	if (ret < 0)
 		goto error;
 
-	BUG_ON(ret == 0); /* Corruption */
+	if (ret == 0) {
+		/* Corruption */
+		btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");

It will never hit this branch.

As in tree checker, we have checked if the devid is so large that a chunk item or system chunk array can't contain one.

That limit is way smaller than (u64)-1.
Thus if we really have a key (DEV_ITEMS DEV_ITEM -1), it will be rejected by tree-checker in the first place, thus you will get a ret == -EUCLEAN from previous btrfs_search_slot() call.

Thanks,
Qu

Quoted 7 lines
fs/btrfs/volumes.c +3 -0
+		ret = -EUCLEAN;
+		goto error;
+	}
 
 	ret = btrfs_previous_item(fs_info->chunk_root, path,
 				  BTRFS_DEV_ITEMS_OBJECTID,
author
#4 lore raw

Re: [PATCH 1/2] btrfs: fix BUG_ON with proper error handle in find_next_devid

Quoted 27 lines · Qu Wenruo
Quoted 14 lines · Anand Jain

In a corrupted tree if search for next devid finds the device with devid = -1, then report the error -EUCLEAN back to the parent function to fail gracefully.

---
  fs/btrfs/volumes.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +0 -0
index 4db4a100c05b..36aa5f79fb6c 100644

fs/btrfs/volumes.c static noinline int find_next_devid(struct btrfs_fs_info *fs_info, · line 1854

Quoted 7 lines
fs/btrfs/volumes.c +3 -1
@@ -1849,7 +1849,12 @@ static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
  	if (ret < 0)
  		goto error;
 
-	BUG_ON(ret == 0); /* Corruption */
+	if (ret == 0) {
+		/* Corruption */
+		btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");

It will never hit this branch.

Quoted 2 lines

As in tree checker, we have checked if the devid is so large that a chunk item or system chunk array can't contain one.

That check is buggy. It assumes devid represents the num_devices, it does not account for the possible devid hole as created in the below script.

$ cat t

umount /btrfs
dev1=/dev/sdb
dev2=/dev/sdc
mkfs.btrfs -fq -dsingle -msingle $dev1
mount $dev1 /btrfs

_fail()

{
	echo $1
	exit 1
}

while true; do

	btrfs dev add -f $dev2 /btrfs || _fail "add failed"
	btrfs dev del $dev1 /btrfs || _fail "del failed"
	dev_tmp=$dev1
	dev1=$dev2
	dev2=$dev_tmp

done

-----------------------

[  185.446441] BTRFS critical (device sdb): corrupt leaf: root=3 block=313739198464 slot=1 devid=1 invalid devid: has=507 expect=[0, 506]
[  185.446446] BTRFS error (device sdb): block=313739198464 write time tree block corruption detected
[  185.446556] BTRFS: error (device sdb) in btrfs_commit_transaction:2268: errno=-5 IO failure (Error while writing out transaction)
[  185.446559] BTRFS warning (device sdb): Skipping commit of aborted transaction.
[  185.446561] BTRFS: error (device sdb) in cleanup_transaction:1827: errno=-5 IO failure

-----------------------

Thanks, Anand

Quoted 15 lines

That limit is way smaller than (u64)-1.
Thus if we really have a key (DEV_ITEMS DEV_ITEM -1), it will be rejected by tree-checker in the first place, thus you will get a ret == -EUCLEAN from previous btrfs_search_slot() call.

Thanks,
Qu

Quoted 7 lines
fs/btrfs/volumes.c +3 -0
+		ret = -EUCLEAN;
+		goto error;
+	}
 
  	ret = btrfs_previous_item(fs_info->chunk_root, path,
  				  BTRFS_DEV_ITEMS_OBJECTID,
Reviewed-by
#5 lore raw

Re: [PATCH 1/2] btrfs: fix BUG_ON with proper error handle in find_next_devid

Quoted 57 lines · Anand Jain
Quoted 27 lines · Qu Wenruo
Quoted 17 lines · Anand Jain

In a corrupted tree if search for next devid finds the device with devid = -1, then report the error -EUCLEAN back to the parent function to fail gracefully.

---
  fs/btrfs/volumes.c | 7 ++++++-

  1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +0 -0
index 4db4a100c05b..36aa5f79fb6c 100644
@@ -1849,7 +1849,12 @@ static noinline int find_next_devid(struct
 btrfs_fs_info *fs_info,
       if (ret < 0)
           goto error;
Quoted 4 lines

  -    BUG_ON(ret == 0); /* Corruption */
+    if (ret == 0) {
+        /* Corruption */
+        btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");

It will never hit this branch.

Quoted 2 lines

As in tree checker, we have checked if the devid is so large that a chunk item or system chunk array can't contain one.

 That check is buggy. It assumes devid represents the num_devices, it does not account for the possible devid hole as created in the below script.

$ cat t

umount /btrfs
dev1=/dev/sdb
dev2=/dev/sdc
mkfs.btrfs -fq -dsingle -msingle $dev1
mount $dev1 /btrfs

_fail()
{
    echo $1
    exit 1
}

while true; do
    btrfs dev add -f $dev2 /btrfs || _fail "add failed" btrfs dev del $dev1 /btrfs || _fail "del failed" dev_tmp=$dev1
    dev1=$dev2
    dev2=$dev_tmp
done

Quoted 13 lines

-----------------------
[  185.446441] BTRFS critical (device sdb): corrupt leaf: root=3 block=313739198464 slot=1 devid=1 invalid devid: has=507 expect=[0, 506] [  185.446446] BTRFS error (device sdb): block=313739198464 write time tree block corruption detected
[  185.446556] BTRFS: error (device sdb) in btrfs_commit_transaction:2268: errno=-5 IO failure (Error while writing out transaction)
[  185.446559] BTRFS warning (device sdb): Skipping commit of aborted transaction.
[  185.446561] BTRFS: error (device sdb) in cleanup_transaction:1827:
errno=-5 IO failure
-----------------------

Oh, that's a case I haven't considered.

Great we can find a bug in a seemingly unrelated patch.

So the patch itself is OK.

Thanks,
Qu

Quoted 20 lines

Thanks, Anand

Quoted 14 lines

That limit is way smaller than (u64)-1.
Thus if we really have a key (DEV_ITEMS DEV_ITEM -1), it will be rejected by tree-checker in the first place, thus you will get a ret == -EUCLEAN from previous btrfs_search_slot() call.

Thanks,
Qu

Quoted 6 lines

+        ret = -EUCLEAN;
+        goto error;
+    }
        ret = btrfs_previous_item(fs_info->chunk_root, path, BTRFS_DEV_ITEMS_OBJECTID,

author
#6 lore raw

PATCH 2/2 btrfs: fix error return on alloc fail in clone_fs_devices

In reply to Anand Jain

Fix the fake ENOMEM return error code to the actual error in clone_fs_devices().

---
 fs/btrfs/volumes.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/btrfs/volumes.c +6 -2
index 36aa5f79fb6c..8d72098ccb4b 100644
@@ -1115,6 +1115,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
 	struct btrfs_fs_devices *fs_devices;
 	struct btrfs_device *device;
 	struct btrfs_device *orig_dev;
+	int ret = 0;
 
 	fs_devices = alloc_fs_devices(orig->fsid, NULL);
 	if (IS_ERR(fs_devices))
@@ -1128,8 +1129,10 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
 
 		device = btrfs_alloc_device(NULL, &orig_dev->devid,
 					    orig_dev->uuid);
-		if (IS_ERR(device))
+		if (IS_ERR(device)) {
+			ret = PTR_ERR(device);
 			goto error;
+		}
 
 		/*
 		 * This is ok to do without rcu read locked because we hold the
@@ -1140,6 +1143,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
 					GFP_KERNEL);
 			if (!name) {
 				btrfs_free_device(device);
+				ret = -ENOMEM;
 				goto error;
 			}
 			rcu_assign_pointer(device->name, name);
@@ -1154,7 +1158,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
 error:
 	mutex_unlock(&orig->device_list_mutex);
 	free_fs_devices(fs_devices);
-	return ERR_PTR(-ENOMEM);
+	return ERR_PTR(ret);
 }
 
 /*
Signature · 1 line
2.21.0 (Apple Git-120)
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] btrfs: fix error return on alloc fail in clone_fs_devices

Signature · 8 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5
90409 Nürnberg
Germany
(HRB 247165, AG München)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on the v1 series
#8 lore raw
In reply to Anand Jain
Quoted 9 lines · Anand Jain

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Anand Jain (2):
btrfs: fix BUG_ON with proper error handle in find_next_devid
btrfs: fix error return on alloc fail in clone_fs_devices

 fs/btrfs/volumes.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Quoted 1 line
#9 lore raw
In reply to Anand Jain
Quoted 6 lines · Anand Jain

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Anand Jain (2):
btrfs: fix BUG_ON with proper error handle in find_next_devid
btrfs: fix error return on alloc fail in clone_fs_devices

Added to misc-next, thanks. If you have script that can reproduce the problem, please add them to the changelog. I've added more from the discussion and questions from Qu.

#10 lore raw
Quoted 4 lines · David Sterba
Quoted 3 lines · Anand Jain

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Quoted 8 lines
Quoted 3 lines

Anand Jain (2):
btrfs: fix BUG_ON with proper error handle in find_next_devid
btrfs: fix error return on alloc fail in clone_fs_devices

Added to misc-next, thanks. If you have script that can reproduce the problem, please add them to the changelog. I've added more from the discussion and questions from Qu.

Actually such a script should ideally be turned into an fstest testcase

author
#11 lore raw
In reply to David Sterba
Quoted 8 lines · David Sterba
Quoted 6 lines · Anand Jain

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Anand Jain (2):
btrfs: fix BUG_ON with proper error handle in find_next_devid
btrfs: fix error return on alloc fail in clone_fs_devices

Quoted 8 lines

Added to misc-next, thanks.

If you have script that can reproduce the
problem, please add them to the changelog. I've added more from the discussion and questions from Qu.

The script and output rather makes senses in the patch [1] it doesn't
here.
[1] [PATCH v2] btrfs: tree-checker: Fix wrong check on max devid

Thanks, Anand

author
#12 lore raw
Quoted 13 lines · Anand Jain
Quoted 9 lines · David Sterba
Quoted 6 lines · Anand Jain

Fixes BUG_ON in find_next_devid() and fixes to return real error in clone_fs_devices(). These two patches can be send to be independent.

Anand Jain (2):
   btrfs: fix BUG_ON with proper error handle in find_next_devid
   btrfs: fix error return on alloc fail in clone_fs_devices

Added to misc-next, thanks.

Quoted 8 lines
Quoted 4 lines

If you have script that can reproduce the
problem, please add them to the changelog. I've added more from the discussion and questions from Qu.

 The script and output rather makes senses in the patch [1] it doesn't
here.
  [1] [PATCH v2] btrfs: tree-checker: Fix wrong check on max devid

David,

The script does not make sense in this patch. I still see it in misc-next.

Thanks, Anand