diff mbox series

[1/2] btrfs-progs: scan: cleanup, return errno when we have one

Message ID 1553838594-26013-1-git-send-email-anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series [1/2] btrfs-progs: scan: cleanup, return errno when we have one | expand

Commit Message

Anand Jain March 29, 2019, 5:49 a.m. UTC
Functions called in cmd_device_scan() and its return codes as below:

btrfs_forget_devices() returns -errno pass down from ioctl or -errno of
open().
btrfs_register_one_device() returns -errno pass down from the kernel
ioctl or -errno from open().
btrfs_scan_devices() (after the patch) returns error code from libblk who's
return error code matches to -errno or 0.
btrfs_register_all_devices() returns number of devices that failed to
regester (as of now) which no one uses. However if
btrfs_register_all_devices() returns error, we error_on() and return 0.

So we can safely return the error code instead of return !!ret;

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-device.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

David Sterba May 15, 2019, 2:26 p.m. UTC | #1
On Fri, Mar 29, 2019 at 01:49:53PM +0800, Anand Jain wrote:
> @@ -386,7 +386,9 @@ static int cmd_device_scan(int argc, char **argv)
>  	}
>  
>  out:
> -	return !!ret;
> +	if (ret < 0)
> +		return -ret;
> +	return ret;

No, cmd_device_scan as the command handler returns the error code that's
intepreted by shell. Most commands do just 0 and 1, in documented case
there are other values, but we can't return errno here.
Anand Jain May 28, 2019, 12:47 p.m. UTC | #2
On 15/5/19 10:26 PM, David Sterba wrote:
> On Fri, Mar 29, 2019 at 01:49:53PM +0800, Anand Jain wrote:
>> @@ -386,7 +386,9 @@ static int cmd_device_scan(int argc, char **argv)
>>   	}
>>   
>>   out:
>> -	return !!ret;
>> +	if (ret < 0)
>> +		return -ret;
>> +	return ret;
> 
> No, cmd_device_scan as the command handler returns the error code that's
> intepreted by shell. Most commands do just 0 and 1, in documented case
> there are other values, but we can't return errno here.
> 

Ok. errno is better error informative though. Interpreted by shell seems 
to be a roadblock.
diff mbox series

Patch

diff --git a/cmds-device.c b/cmds-device.c
index e3e30b6d5ded..bb3dc78149f8 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -359,13 +359,13 @@  static int cmd_device_scan(int argc, char **argv)
 
 		if (is_block_device(argv[i]) != 1) {
 			error("not a block device: %s", argv[i]);
-			ret = 1;
+			ret = EINVAL;
 			goto out;
 		}
 		path = canonicalize_path(argv[i]);
 		if (!path) {
 			error("could not canonicalize path '%s': %m", argv[i]);
-			ret = 1;
+			ret = EINVAL;
 			goto out;
 		}
 		if (forget) {
@@ -376,8 +376,8 @@  static int cmd_device_scan(int argc, char **argv)
 			}
 		} else {
 			printf("Scanning for btrfs filesystems on '%s'\n", path);
-			if (btrfs_register_one_device(path) != 0) {
-				ret = 1;
+			ret = btrfs_register_one_device(path);
+			if (ret < 0) {
 				free(path);
 				goto out;
 			}
@@ -386,7 +386,9 @@  static int cmd_device_scan(int argc, char **argv)
 	}
 
 out:
-	return !!ret;
+	if (ret < 0)
+		return -ret;
+	return ret;
 }
 
 static const char * const cmd_device_ready_usage[] = {