diff mbox series

[1/2] btrfs-progs: prepare helper device_is_seed

Message ID 1eb9319975967eb52107c9355d712f9eb9d96cf7.1676124188.git.anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: read device fsid from the sysfs | expand

Commit Message

Anand Jain Feb. 13, 2023, 9:37 a.m. UTC
load_device_info() checks if the device is a seed device by reading
superblock::fsid and comparing it with the mount fsid, and it fails
to work if the device is missing (a RAID1 seed fs). Move this part
of the code into a new helper function device_is_seed() in
preparation to make device_is_seed() work with the new sysfs
devinfo/<devid>/fsid interface.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
---
 cmds/filesystem-usage.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

Comments

David Sterba Feb. 21, 2023, 11:16 p.m. UTC | #1
On Mon, Feb 13, 2023 at 05:37:41PM +0800, Anand Jain wrote:
> load_device_info() checks if the device is a seed device by reading
> superblock::fsid and comparing it with the mount fsid, and it fails
> to work if the device is missing (a RAID1 seed fs). Move this part
> of the code into a new helper function device_is_seed() in
> preparation to make device_is_seed() work with the new sysfs
> devinfo/<devid>/fsid interface.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> Reviewed-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  cmds/filesystem-usage.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
> index 5810324f245e..bef9a1129a63 100644
> --- a/cmds/filesystem-usage.c
> +++ b/cmds/filesystem-usage.c
> @@ -27,6 +27,7 @@
>  #include <fcntl.h>
>  #include <dirent.h>
>  #include <limits.h>
> +#include <uuid/uuid.h>
>  #include "kernel-lib/sizes.h"
>  #include "kernel-shared/ctree.h"
>  #include "kernel-shared/disk-io.h"
> @@ -700,6 +701,21 @@ out:
>  	return ret;
>  }
>  
> +static int device_is_seed(const char *dev_path, u8 *mnt_fsid)
> +{
> +	uuid_t fsid;

I've switched this to a u8[BTFFS_UUID_SIZE] buffer, as it was in the
other funcion.

> +	int ret;
> +
> +	ret = dev_to_fsid(dev_path, fsid);
> +	if (ret)
> +		return ret;
> +
> +	if (memcmp(mnt_fsid, fsid, BTRFS_FSID_SIZE))

IMO strcmp and memcmp should use the == 0 or != 0 explicitly so it's
closer to the meaning.

> +		return 0;
> +
> +	return -1;
> +}
> +
>  /*
>   *  This function loads the device_info structure and put them in an array
>   */
> @@ -710,7 +726,6 @@ static int load_device_info(int fd, struct device_info **devinfo_ret,
>  	struct btrfs_ioctl_fs_info_args fi_args;
>  	struct btrfs_ioctl_dev_info_args dev_info;
>  	struct device_info *info;
> -	u8 fsid[BTRFS_UUID_SIZE];
>  
>  	*devcount_ret = 0;
>  	*devinfo_ret = NULL;
> @@ -754,8 +769,8 @@ static int load_device_info(int fd, struct device_info **devinfo_ret,
>  		 * Ignore any other error including -EACCES, which is seen when
>  		 * a non-root process calls dev_to_fsid(path)->open(path).
>  		 */
> -		ret = dev_to_fsid((const char *)dev_info.path, fsid);
> -		if (!ret && memcmp(fi_args.fsid, fsid, BTRFS_FSID_SIZE) != 0)
> +		ret = device_is_seed((const char *)dev_info.path, fi_args.fsid);
> +		if (!ret)
>  			continue;
>  
>  		info[ndevs].devid = dev_info.devid;
> -- 
> 2.39.1
diff mbox series

Patch

diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
index 5810324f245e..bef9a1129a63 100644
--- a/cmds/filesystem-usage.c
+++ b/cmds/filesystem-usage.c
@@ -27,6 +27,7 @@ 
 #include <fcntl.h>
 #include <dirent.h>
 #include <limits.h>
+#include <uuid/uuid.h>
 #include "kernel-lib/sizes.h"
 #include "kernel-shared/ctree.h"
 #include "kernel-shared/disk-io.h"
@@ -700,6 +701,21 @@  out:
 	return ret;
 }
 
+static int device_is_seed(const char *dev_path, u8 *mnt_fsid)
+{
+	uuid_t fsid;
+	int ret;
+
+	ret = dev_to_fsid(dev_path, fsid);
+	if (ret)
+		return ret;
+
+	if (memcmp(mnt_fsid, fsid, BTRFS_FSID_SIZE))
+		return 0;
+
+	return -1;
+}
+
 /*
  *  This function loads the device_info structure and put them in an array
  */
@@ -710,7 +726,6 @@  static int load_device_info(int fd, struct device_info **devinfo_ret,
 	struct btrfs_ioctl_fs_info_args fi_args;
 	struct btrfs_ioctl_dev_info_args dev_info;
 	struct device_info *info;
-	u8 fsid[BTRFS_UUID_SIZE];
 
 	*devcount_ret = 0;
 	*devinfo_ret = NULL;
@@ -754,8 +769,8 @@  static int load_device_info(int fd, struct device_info **devinfo_ret,
 		 * Ignore any other error including -EACCES, which is seen when
 		 * a non-root process calls dev_to_fsid(path)->open(path).
 		 */
-		ret = dev_to_fsid((const char *)dev_info.path, fsid);
-		if (!ret && memcmp(fi_args.fsid, fsid, BTRFS_FSID_SIZE) != 0)
+		ret = device_is_seed((const char *)dev_info.path, fi_args.fsid);
+		if (!ret)
 			continue;
 
 		info[ndevs].devid = dev_info.devid;