diff mbox series

btrfs-progs: Fix seed device bug for btrfs249

Message ID 20220810073347.4998-1-hmsjwzb@zoho.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: Fix seed device bug for btrfs249 | expand

Commit Message

hmsjwzb Aug. 10, 2022, 7:33 a.m. UTC
Signed-off-by: Flint.Wang <hmsjwzb@zoho.com>
---
 cmds/device.c           |  2 +-
 cmds/filesystem-usage.c | 10 +++++-----
 cmds/filesystem-usage.h |  2 +-
 common/utils.c          |  9 +++++----
 common/utils.h          |  2 +-
 ioctl.h                 |  2 ++
 6 files changed, 15 insertions(+), 12 deletions(-)

Comments

Qu Wenruo Aug. 10, 2022, 7:56 a.m. UTC | #1
Commit message please.

On 2022/8/10 15:33, Flint.Wang wrote:
> Signed-off-by: Flint.Wang <hmsjwzb@zoho.com>
> ---
>   cmds/device.c           |  2 +-
>   cmds/filesystem-usage.c | 10 +++++-----
>   cmds/filesystem-usage.h |  2 +-
>   common/utils.c          |  9 +++++----
>   common/utils.h          |  2 +-
>   ioctl.h                 |  2 ++
>   6 files changed, 15 insertions(+), 12 deletions(-)
>
> diff --git a/cmds/device.c b/cmds/device.c
> index 7d3febff..81559110 100644
> --- a/cmds/device.c
> +++ b/cmds/device.c
> @@ -752,7 +752,7 @@ static int _cmd_device_usage(int fd, const char *path, unsigned unit_mode)
>   	int devcount = 0;
>
>   	ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
> -			&devcount);
> +			&devcount, false);
>   	if (ret)
>   		goto out;
>
> diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
> index 01729e18..b2ed3212 100644
> --- a/cmds/filesystem-usage.c
> +++ b/cmds/filesystem-usage.c
> @@ -693,7 +693,7 @@ out:
>    *  This function loads the device_info structure and put them in an array
>    */
>   static int load_device_info(int fd, struct device_info **device_info_ptr,
> -			   int *device_info_count)
> +			   int *device_info_count, bool noseed)
>   {
>   	int ret, i, ndevs;
>   	struct btrfs_ioctl_fs_info_args fi_args;
> @@ -727,7 +727,7 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
>   			goto out;
>   		}
>   		memset(&dev_info, 0, sizeof(dev_info));
> -		ret = get_device_info(fd, i, &dev_info);
> +		ret = get_device_info(fd, i, &dev_info, noseed);
>
>   		if (ret == -ENODEV)
>   			continue;
> @@ -779,7 +779,7 @@ out:
>   }
>
>   int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
> -		int *chunkcount, struct device_info **devinfo, int *devcount)
> +		int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed)
>   {
>   	int ret;
>
> @@ -791,7 +791,7 @@ int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>   		return ret;
>   	}
>
> -	ret = load_device_info(fd, devinfo, devcount);
> +	ret = load_device_info(fd, devinfo, devcount, noseed);
>   	if (ret == -EPERM) {
>   		warning(
>   		"cannot get filesystem info from ioctl(FS_INFO), run as root");
> @@ -1172,7 +1172,7 @@ static int cmd_filesystem_usage(const struct cmd_struct *cmd,
>   			printf("\n");
>
>   		ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
> -				&devinfo, &devcount);
> +				&devinfo, &devcount, true);
>   		if (ret)
>   			goto cleanup;
>
> diff --git a/cmds/filesystem-usage.h b/cmds/filesystem-usage.h
> index cab38462..6fd04172 100644
> --- a/cmds/filesystem-usage.h
> +++ b/cmds/filesystem-usage.h
> @@ -45,7 +45,7 @@ struct chunk_info {
>   };
>
>   int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
> -		int *chunkcount, struct device_info **devinfo, int *devcount);
> +		int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed);
>   void print_device_chunks(struct device_info *devinfo,
>   		struct chunk_info *chunks_info_ptr,
>   		int chunks_info_count, unsigned unit_mode);
> diff --git a/common/utils.c b/common/utils.c
> index 1ed5571f..72d50885 100644
> --- a/common/utils.c
> +++ b/common/utils.c
> @@ -285,14 +285,15 @@ void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
>   }
>
>   int get_device_info(int fd, u64 devid,
> -		struct btrfs_ioctl_dev_info_args *di_args)
> +		struct btrfs_ioctl_dev_info_args *di_args, bool noseed)
>   {
>   	int ret;
> +	unsigned long req = noseed ? BTRFS_IOC_DEV_INFO_NOSEED : BTRFS_IOC_DEV_INFO;
>
>   	di_args->devid = devid;
>   	memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
>
> -	ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
> +	ret = ioctl(fd, req, di_args);
>   	return ret < 0 ? -errno : 0;
>   }
>
> @@ -498,7 +499,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>   		 * search_chunk_tree_for_fs_info() will lacks the devid 0
>   		 * so manual probe for it here.
>   		 */
> -		ret = get_device_info(fd, 0, &tmp);
> +		ret = get_device_info(fd, 0, &tmp, false);

No, I don't think we should go that complex.

The root cause is pretty simple, btrfs_ioctl_fs_info() just return RW
devices for its fi_args->num_devices.

This is fine for most cases, but for seed devices cases, we can not rely
on that.


Knowing that, it's much easier to fix in user space, do a tree-search
ioctl to grab the device items from chunk tree, then we can easily check
the total number of devices (including RW and seed devices).

With that info, we even have the FSID of each devices, then we can call
btrfs_ioctl_dev_info() to get each device.


I'd like to have a better optimization, to skip above chunk tree search,
but I don't have a quick idea on how to determine if a fs has seed devices.


Another more elegant solution is to make btrfs_ioctl_fs_info_args to
include one new member, total_devs, so that we can get rid of all the
problems.

But for compatibility reasons, above tree-search based solution is still
needed as a fallback.

Thanks,
Qu

>   		if (!ret) {
>   			fi_args->num_devices++;
>   			ndevs++;
> @@ -521,7 +522,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>   		memcpy(di_args, &tmp, sizeof(tmp));
>   	for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
>   	     last_devid++) {
> -		ret = get_device_info(fd, last_devid, &di_args[ndevs]);
> +		ret = get_device_info(fd, last_devid, &di_args[ndevs], false);
>   		if (ret == -ENODEV)
>   			continue;
>   		if (ret)
> diff --git a/common/utils.h b/common/utils.h
> index ea05fe5b..de4f93ca 100644
> --- a/common/utils.h
> +++ b/common/utils.h
> @@ -68,7 +68,7 @@ int lookup_path_rootid(int fd, u64 *rootid);
>   int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
>   int find_mount_root(const char *path, char **mount_root);
>   int get_device_info(int fd, u64 devid,
> -		struct btrfs_ioctl_dev_info_args *di_args);
> +		struct btrfs_ioctl_dev_info_args *di_args, bool noseed);
>   int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
>
>   const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
> diff --git a/ioctl.h b/ioctl.h
> index 368a87b2..e68fe58d 100644
> --- a/ioctl.h
> +++ b/ioctl.h
> @@ -883,6 +883,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
>   					struct btrfs_ioctl_scrub_args)
>   #define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
>   					struct btrfs_ioctl_dev_info_args)
> +#define BTRFS_IOC_DEV_INFO_NOSEED _IOR(BTRFS_IOCTL_MAGIC, 30, \
> +				       struct btrfs_ioctl_dev_info_args)
>   #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
>                                    struct btrfs_ioctl_fs_info_args)
>   #define BTRFS_IOC_BALANCE_V2 _IOWR(BTRFS_IOCTL_MAGIC, 32, \
hmsjwzb Aug. 11, 2022, 6:07 a.m. UTC | #2
Hi Qu,

Thanks for you reply. I am working on the tree-search based solution.
I have got the btrfs_dev_item from the chunk tree.
But I don't know how to tell whether it is a seed device.
Do you have some solution to tell whether it is a seed device or not?

Thanks,
Flint

On 8/10/22 03:56, Qu Wenruo wrote:
> Commit message please.
> 
> On 2022/8/10 15:33, Flint.Wang wrote:
>> Signed-off-by: Flint.Wang <hmsjwzb@zoho.com>
>> ---
>>   cmds/device.c           |  2 +-
>>   cmds/filesystem-usage.c | 10 +++++-----
>>   cmds/filesystem-usage.h |  2 +-
>>   common/utils.c          |  9 +++++----
>>   common/utils.h          |  2 +-
>>   ioctl.h                 |  2 ++
>>   6 files changed, 15 insertions(+), 12 deletions(-)
>>
>> diff --git a/cmds/device.c b/cmds/device.c
>> index 7d3febff..81559110 100644
>> --- a/cmds/device.c
>> +++ b/cmds/device.c
>> @@ -752,7 +752,7 @@ static int _cmd_device_usage(int fd, const char *path, unsigned unit_mode)
>>       int devcount = 0;
>>
>>       ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
>> -            &devcount);
>> +            &devcount, false);
>>       if (ret)
>>           goto out;
>>
>> diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
>> index 01729e18..b2ed3212 100644
>> --- a/cmds/filesystem-usage.c
>> +++ b/cmds/filesystem-usage.c
>> @@ -693,7 +693,7 @@ out:
>>    *  This function loads the device_info structure and put them in an array
>>    */
>>   static int load_device_info(int fd, struct device_info **device_info_ptr,
>> -               int *device_info_count)
>> +               int *device_info_count, bool noseed)
>>   {
>>       int ret, i, ndevs;
>>       struct btrfs_ioctl_fs_info_args fi_args;
>> @@ -727,7 +727,7 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
>>               goto out;
>>           }
>>           memset(&dev_info, 0, sizeof(dev_info));
>> -        ret = get_device_info(fd, i, &dev_info);
>> +        ret = get_device_info(fd, i, &dev_info, noseed);
>>
>>           if (ret == -ENODEV)
>>               continue;
>> @@ -779,7 +779,7 @@ out:
>>   }
>>
>>   int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>> -        int *chunkcount, struct device_info **devinfo, int *devcount)
>> +        int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed)
>>   {
>>       int ret;
>>
>> @@ -791,7 +791,7 @@ int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>>           return ret;
>>       }
>>
>> -    ret = load_device_info(fd, devinfo, devcount);
>> +    ret = load_device_info(fd, devinfo, devcount, noseed);
>>       if (ret == -EPERM) {
>>           warning(
>>           "cannot get filesystem info from ioctl(FS_INFO), run as root");
>> @@ -1172,7 +1172,7 @@ static int cmd_filesystem_usage(const struct cmd_struct *cmd,
>>               printf("\n");
>>
>>           ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
>> -                &devinfo, &devcount);
>> +                &devinfo, &devcount, true);
>>           if (ret)
>>               goto cleanup;
>>
>> diff --git a/cmds/filesystem-usage.h b/cmds/filesystem-usage.h
>> index cab38462..6fd04172 100644
>> --- a/cmds/filesystem-usage.h
>> +++ b/cmds/filesystem-usage.h
>> @@ -45,7 +45,7 @@ struct chunk_info {
>>   };
>>
>>   int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>> -        int *chunkcount, struct device_info **devinfo, int *devcount);
>> +        int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed);
>>   void print_device_chunks(struct device_info *devinfo,
>>           struct chunk_info *chunks_info_ptr,
>>           int chunks_info_count, unsigned unit_mode);
>> diff --git a/common/utils.c b/common/utils.c
>> index 1ed5571f..72d50885 100644
>> --- a/common/utils.c
>> +++ b/common/utils.c
>> @@ -285,14 +285,15 @@ void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
>>   }
>>
>>   int get_device_info(int fd, u64 devid,
>> -        struct btrfs_ioctl_dev_info_args *di_args)
>> +        struct btrfs_ioctl_dev_info_args *di_args, bool noseed)
>>   {
>>       int ret;
>> +    unsigned long req = noseed ? BTRFS_IOC_DEV_INFO_NOSEED : BTRFS_IOC_DEV_INFO;
>>
>>       di_args->devid = devid;
>>       memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
>>
>> -    ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
>> +    ret = ioctl(fd, req, di_args);
>>       return ret < 0 ? -errno : 0;
>>   }
>>
>> @@ -498,7 +499,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>>            * search_chunk_tree_for_fs_info() will lacks the devid 0
>>            * so manual probe for it here.
>>            */
>> -        ret = get_device_info(fd, 0, &tmp);
>> +        ret = get_device_info(fd, 0, &tmp, false);
> 
> No, I don't think we should go that complex.
> 
> The root cause is pretty simple, btrfs_ioctl_fs_info() just return RW
> devices for its fi_args->num_devices.
> 
> This is fine for most cases, but for seed devices cases, we can not rely
> on that.
> 
> 
> Knowing that, it's much easier to fix in user space, do a tree-search
> ioctl to grab the device items from chunk tree, then we can easily check
> the total number of devices (including RW and seed devices).
> 
> With that info, we even have the FSID of each devices, then we can call
> btrfs_ioctl_dev_info() to get each device.
> 
> 
> I'd like to have a better optimization, to skip above chunk tree search,
> but I don't have a quick idea on how to determine if a fs has seed devices.
> 
> 
> Another more elegant solution is to make btrfs_ioctl_fs_info_args to
> include one new member, total_devs, so that we can get rid of all the
> problems.
> 
> But for compatibility reasons, above tree-search based solution is still
> needed as a fallback.
> 
> Thanks,
> Qu
> 
>>           if (!ret) {
>>               fi_args->num_devices++;
>>               ndevs++;
>> @@ -521,7 +522,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>>           memcpy(di_args, &tmp, sizeof(tmp));
>>       for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
>>            last_devid++) {
>> -        ret = get_device_info(fd, last_devid, &di_args[ndevs]);
>> +        ret = get_device_info(fd, last_devid, &di_args[ndevs], false);
>>           if (ret == -ENODEV)
>>               continue;
>>           if (ret)
>> diff --git a/common/utils.h b/common/utils.h
>> index ea05fe5b..de4f93ca 100644
>> --- a/common/utils.h
>> +++ b/common/utils.h
>> @@ -68,7 +68,7 @@ int lookup_path_rootid(int fd, u64 *rootid);
>>   int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
>>   int find_mount_root(const char *path, char **mount_root);
>>   int get_device_info(int fd, u64 devid,
>> -        struct btrfs_ioctl_dev_info_args *di_args);
>> +        struct btrfs_ioctl_dev_info_args *di_args, bool noseed);
>>   int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
>>
>>   const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
>> diff --git a/ioctl.h b/ioctl.h
>> index 368a87b2..e68fe58d 100644
>> --- a/ioctl.h
>> +++ b/ioctl.h
>> @@ -883,6 +883,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
>>                       struct btrfs_ioctl_scrub_args)
>>   #define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
>>                       struct btrfs_ioctl_dev_info_args)
>> +#define BTRFS_IOC_DEV_INFO_NOSEED _IOR(BTRFS_IOCTL_MAGIC, 30, \
>> +                       struct btrfs_ioctl_dev_info_args)
>>   #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
>>                                    struct btrfs_ioctl_fs_info_args)
>>   #define BTRFS_IOC_BALANCE_V2 _IOWR(BTRFS_IOCTL_MAGIC, 32, \
Qu Wenruo Aug. 11, 2022, 6:21 a.m. UTC | #3
On 2022/8/11 14:07, hmsjwzb wrote:
> Hi Qu,
>
> Thanks for you reply. I am working on the tree-search based solution.
> I have got the btrfs_dev_item from the chunk tree.
> But I don't know how to tell whether it is a seed device.

You can check the fsid.

Seed device has a different fsid than the one returned by BTRFS_IOC_FS_INFO.

Thanks,
Qu

> Do you have some solution to tell whether it is a seed device or not?
>
> Thanks,
> Flint
>
> On 8/10/22 03:56, Qu Wenruo wrote:
>> Commit message please.
>>
>> On 2022/8/10 15:33, Flint.Wang wrote:
>>> Signed-off-by: Flint.Wang <hmsjwzb@zoho.com>
>>> ---
>>>    cmds/device.c           |  2 +-
>>>    cmds/filesystem-usage.c | 10 +++++-----
>>>    cmds/filesystem-usage.h |  2 +-
>>>    common/utils.c          |  9 +++++----
>>>    common/utils.h          |  2 +-
>>>    ioctl.h                 |  2 ++
>>>    6 files changed, 15 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/cmds/device.c b/cmds/device.c
>>> index 7d3febff..81559110 100644
>>> --- a/cmds/device.c
>>> +++ b/cmds/device.c
>>> @@ -752,7 +752,7 @@ static int _cmd_device_usage(int fd, const char *path, unsigned unit_mode)
>>>        int devcount = 0;
>>>
>>>        ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
>>> -            &devcount);
>>> +            &devcount, false);
>>>        if (ret)
>>>            goto out;
>>>
>>> diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
>>> index 01729e18..b2ed3212 100644
>>> --- a/cmds/filesystem-usage.c
>>> +++ b/cmds/filesystem-usage.c
>>> @@ -693,7 +693,7 @@ out:
>>>     *  This function loads the device_info structure and put them in an array
>>>     */
>>>    static int load_device_info(int fd, struct device_info **device_info_ptr,
>>> -               int *device_info_count)
>>> +               int *device_info_count, bool noseed)
>>>    {
>>>        int ret, i, ndevs;
>>>        struct btrfs_ioctl_fs_info_args fi_args;
>>> @@ -727,7 +727,7 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
>>>                goto out;
>>>            }
>>>            memset(&dev_info, 0, sizeof(dev_info));
>>> -        ret = get_device_info(fd, i, &dev_info);
>>> +        ret = get_device_info(fd, i, &dev_info, noseed);
>>>
>>>            if (ret == -ENODEV)
>>>                continue;
>>> @@ -779,7 +779,7 @@ out:
>>>    }
>>>
>>>    int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>>> -        int *chunkcount, struct device_info **devinfo, int *devcount)
>>> +        int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed)
>>>    {
>>>        int ret;
>>>
>>> @@ -791,7 +791,7 @@ int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>>>            return ret;
>>>        }
>>>
>>> -    ret = load_device_info(fd, devinfo, devcount);
>>> +    ret = load_device_info(fd, devinfo, devcount, noseed);
>>>        if (ret == -EPERM) {
>>>            warning(
>>>            "cannot get filesystem info from ioctl(FS_INFO), run as root");
>>> @@ -1172,7 +1172,7 @@ static int cmd_filesystem_usage(const struct cmd_struct *cmd,
>>>                printf("\n");
>>>
>>>            ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
>>> -                &devinfo, &devcount);
>>> +                &devinfo, &devcount, true);
>>>            if (ret)
>>>                goto cleanup;
>>>
>>> diff --git a/cmds/filesystem-usage.h b/cmds/filesystem-usage.h
>>> index cab38462..6fd04172 100644
>>> --- a/cmds/filesystem-usage.h
>>> +++ b/cmds/filesystem-usage.h
>>> @@ -45,7 +45,7 @@ struct chunk_info {
>>>    };
>>>
>>>    int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
>>> -        int *chunkcount, struct device_info **devinfo, int *devcount);
>>> +        int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed);
>>>    void print_device_chunks(struct device_info *devinfo,
>>>            struct chunk_info *chunks_info_ptr,
>>>            int chunks_info_count, unsigned unit_mode);
>>> diff --git a/common/utils.c b/common/utils.c
>>> index 1ed5571f..72d50885 100644
>>> --- a/common/utils.c
>>> +++ b/common/utils.c
>>> @@ -285,14 +285,15 @@ void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
>>>    }
>>>
>>>    int get_device_info(int fd, u64 devid,
>>> -        struct btrfs_ioctl_dev_info_args *di_args)
>>> +        struct btrfs_ioctl_dev_info_args *di_args, bool noseed)
>>>    {
>>>        int ret;
>>> +    unsigned long req = noseed ? BTRFS_IOC_DEV_INFO_NOSEED : BTRFS_IOC_DEV_INFO;
>>>
>>>        di_args->devid = devid;
>>>        memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
>>>
>>> -    ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
>>> +    ret = ioctl(fd, req, di_args);
>>>        return ret < 0 ? -errno : 0;
>>>    }
>>>
>>> @@ -498,7 +499,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>>>             * search_chunk_tree_for_fs_info() will lacks the devid 0
>>>             * so manual probe for it here.
>>>             */
>>> -        ret = get_device_info(fd, 0, &tmp);
>>> +        ret = get_device_info(fd, 0, &tmp, false);
>>
>> No, I don't think we should go that complex.
>>
>> The root cause is pretty simple, btrfs_ioctl_fs_info() just return RW
>> devices for its fi_args->num_devices.
>>
>> This is fine for most cases, but for seed devices cases, we can not rely
>> on that.
>>
>>
>> Knowing that, it's much easier to fix in user space, do a tree-search
>> ioctl to grab the device items from chunk tree, then we can easily check
>> the total number of devices (including RW and seed devices).
>>
>> With that info, we even have the FSID of each devices, then we can call
>> btrfs_ioctl_dev_info() to get each device.
>>
>>
>> I'd like to have a better optimization, to skip above chunk tree search,
>> but I don't have a quick idea on how to determine if a fs has seed devices.
>>
>>
>> Another more elegant solution is to make btrfs_ioctl_fs_info_args to
>> include one new member, total_devs, so that we can get rid of all the
>> problems.
>>
>> But for compatibility reasons, above tree-search based solution is still
>> needed as a fallback.
>>
>> Thanks,
>> Qu
>>
>>>            if (!ret) {
>>>                fi_args->num_devices++;
>>>                ndevs++;
>>> @@ -521,7 +522,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
>>>            memcpy(di_args, &tmp, sizeof(tmp));
>>>        for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
>>>             last_devid++) {
>>> -        ret = get_device_info(fd, last_devid, &di_args[ndevs]);
>>> +        ret = get_device_info(fd, last_devid, &di_args[ndevs], false);
>>>            if (ret == -ENODEV)
>>>                continue;
>>>            if (ret)
>>> diff --git a/common/utils.h b/common/utils.h
>>> index ea05fe5b..de4f93ca 100644
>>> --- a/common/utils.h
>>> +++ b/common/utils.h
>>> @@ -68,7 +68,7 @@ int lookup_path_rootid(int fd, u64 *rootid);
>>>    int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
>>>    int find_mount_root(const char *path, char **mount_root);
>>>    int get_device_info(int fd, u64 devid,
>>> -        struct btrfs_ioctl_dev_info_args *di_args);
>>> +        struct btrfs_ioctl_dev_info_args *di_args, bool noseed);
>>>    int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
>>>
>>>    const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
>>> diff --git a/ioctl.h b/ioctl.h
>>> index 368a87b2..e68fe58d 100644
>>> --- a/ioctl.h
>>> +++ b/ioctl.h
>>> @@ -883,6 +883,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
>>>                        struct btrfs_ioctl_scrub_args)
>>>    #define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
>>>                        struct btrfs_ioctl_dev_info_args)
>>> +#define BTRFS_IOC_DEV_INFO_NOSEED _IOR(BTRFS_IOCTL_MAGIC, 30, \
>>> +                       struct btrfs_ioctl_dev_info_args)
>>>    #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
>>>                                     struct btrfs_ioctl_fs_info_args)
>>>    #define BTRFS_IOC_BALANCE_V2 _IOWR(BTRFS_IOCTL_MAGIC, 32, \
diff mbox series

Patch

diff --git a/cmds/device.c b/cmds/device.c
index 7d3febff..81559110 100644
--- a/cmds/device.c
+++ b/cmds/device.c
@@ -752,7 +752,7 @@  static int _cmd_device_usage(int fd, const char *path, unsigned unit_mode)
 	int devcount = 0;
 
 	ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
-			&devcount);
+			&devcount, false);
 	if (ret)
 		goto out;
 
diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
index 01729e18..b2ed3212 100644
--- a/cmds/filesystem-usage.c
+++ b/cmds/filesystem-usage.c
@@ -693,7 +693,7 @@  out:
  *  This function loads the device_info structure and put them in an array
  */
 static int load_device_info(int fd, struct device_info **device_info_ptr,
-			   int *device_info_count)
+			   int *device_info_count, bool noseed)
 {
 	int ret, i, ndevs;
 	struct btrfs_ioctl_fs_info_args fi_args;
@@ -727,7 +727,7 @@  static int load_device_info(int fd, struct device_info **device_info_ptr,
 			goto out;
 		}
 		memset(&dev_info, 0, sizeof(dev_info));
-		ret = get_device_info(fd, i, &dev_info);
+		ret = get_device_info(fd, i, &dev_info, noseed);
 
 		if (ret == -ENODEV)
 			continue;
@@ -779,7 +779,7 @@  out:
 }
 
 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
-		int *chunkcount, struct device_info **devinfo, int *devcount)
+		int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed)
 {
 	int ret;
 
@@ -791,7 +791,7 @@  int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
 		return ret;
 	}
 
-	ret = load_device_info(fd, devinfo, devcount);
+	ret = load_device_info(fd, devinfo, devcount, noseed);
 	if (ret == -EPERM) {
 		warning(
 		"cannot get filesystem info from ioctl(FS_INFO), run as root");
@@ -1172,7 +1172,7 @@  static int cmd_filesystem_usage(const struct cmd_struct *cmd,
 			printf("\n");
 
 		ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
-				&devinfo, &devcount);
+				&devinfo, &devcount, true);
 		if (ret)
 			goto cleanup;
 
diff --git a/cmds/filesystem-usage.h b/cmds/filesystem-usage.h
index cab38462..6fd04172 100644
--- a/cmds/filesystem-usage.h
+++ b/cmds/filesystem-usage.h
@@ -45,7 +45,7 @@  struct chunk_info {
 };
 
 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
-		int *chunkcount, struct device_info **devinfo, int *devcount);
+		int *chunkcount, struct device_info **devinfo, int *devcount, bool noseed);
 void print_device_chunks(struct device_info *devinfo,
 		struct chunk_info *chunks_info_ptr,
 		int chunks_info_count, unsigned unit_mode);
diff --git a/common/utils.c b/common/utils.c
index 1ed5571f..72d50885 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -285,14 +285,15 @@  void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
 }
 
 int get_device_info(int fd, u64 devid,
-		struct btrfs_ioctl_dev_info_args *di_args)
+		struct btrfs_ioctl_dev_info_args *di_args, bool noseed)
 {
 	int ret;
+	unsigned long req = noseed ? BTRFS_IOC_DEV_INFO_NOSEED : BTRFS_IOC_DEV_INFO;
 
 	di_args->devid = devid;
 	memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
 
-	ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
+	ret = ioctl(fd, req, di_args);
 	return ret < 0 ? -errno : 0;
 }
 
@@ -498,7 +499,7 @@  int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		 * search_chunk_tree_for_fs_info() will lacks the devid 0
 		 * so manual probe for it here.
 		 */
-		ret = get_device_info(fd, 0, &tmp);
+		ret = get_device_info(fd, 0, &tmp, false);
 		if (!ret) {
 			fi_args->num_devices++;
 			ndevs++;
@@ -521,7 +522,7 @@  int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		memcpy(di_args, &tmp, sizeof(tmp));
 	for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
 	     last_devid++) {
-		ret = get_device_info(fd, last_devid, &di_args[ndevs]);
+		ret = get_device_info(fd, last_devid, &di_args[ndevs], false);
 		if (ret == -ENODEV)
 			continue;
 		if (ret)
diff --git a/common/utils.h b/common/utils.h
index ea05fe5b..de4f93ca 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -68,7 +68,7 @@  int lookup_path_rootid(int fd, u64 *rootid);
 int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
 int find_mount_root(const char *path, char **mount_root);
 int get_device_info(int fd, u64 devid,
-		struct btrfs_ioctl_dev_info_args *di_args);
+		struct btrfs_ioctl_dev_info_args *di_args, bool noseed);
 int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
 
 const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
diff --git a/ioctl.h b/ioctl.h
index 368a87b2..e68fe58d 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -883,6 +883,8 @@  static inline char *btrfs_err_str(enum btrfs_err_code err_code)
 					struct btrfs_ioctl_scrub_args)
 #define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
 					struct btrfs_ioctl_dev_info_args)
+#define BTRFS_IOC_DEV_INFO_NOSEED _IOR(BTRFS_IOCTL_MAGIC, 30, \
+				       struct btrfs_ioctl_dev_info_args)
 #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
                                  struct btrfs_ioctl_fs_info_args)
 #define BTRFS_IOC_BALANCE_V2 _IOWR(BTRFS_IOCTL_MAGIC, 32, \