btrfs-progs: Fixing invalid device size output when "btrfs device usage /" called as normal user
PATCH v2 1/1 btrfs-progs: Fixing invalid device size output when "btrfs device usage /" called as normal user
Issue: #979
Changes since v1:
- Added Issue: #979 tag instead of bug description
- Added error handling to get_partition_sector_size_sysfs()
- Changed return value of get_partition_sector_size_sysfs() to negative on failure
When "btrfs device usage /" is being called as a normal user, the function device_get_partition_size in device-utils.c calls device_get_partition_size_sysfs() as a fallback, which reads the size of the partition from "/sys/class/block/[partition]/size".
The problem here is that the size read is not actually the size of the partition but rather a number of how many 512B (or whatever the devices sector size is) sectors the partition contains.
Ex: if read value is 104857600 the size is not 100MB but 104857600 * 512B = 50GB
This patch adds a function named get_partition_sector_size_sysfs which based on the partition name returns the sector size of the device, and replaces "return size" with "return size * sector_size" in device_get_partition_size_sysfs.
--- common/device-utils.c | 53 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-)
common/device-utils.c +52 -1
@@ -342,6 +342,50 @@ u64 device_get_partition_size_fd(int fd)return result; } +static ssize_t get_partition_sector_size_sysfs (const char *name) +{ +int ret =0 ; +char real_path[PATH_MAX ] = {}; +char link_path[PATH_MAX ] = {}; +char *dev_name =NULL ; +int sysfd; +char sysfs[PATH_MAX ] = {}; +char sizebuf[128 ]; + +snprintf (link_path,PATH_MAX ,"/sys/class/block/%s/.." , name); + +if (!realpath (link_path, real_path)) { +error ("Failed to resolve realpath of %s: %s\n" , link_path,strerror (errno)); +return -1 ; + } + + dev_name =basename (real_path); + +if (!dev_name) { +error ("Failed to determine basename for path %s\n" , real_path); +return -1 ; + } + +snprintf (sysfs,PATH_MAX ,"/sys/class/block/%s/queue/hw_sector_size" , dev_name); + + sysfd =open (sysfs,O_RDONLY ); +if (sysfd <0 ) { +error ("Error opening %s to determine dev sector size: %s\n" , real_path,strerror (errno)); +return -1 ; + } + + ret =sysfs_read_file (sysfd, sizebuf,sizeof (sizebuf)); +close (sysfd); + +if (ret <0 ) { +error ("Error reading hw_sector_size from sysfs for device %s!\n" , dev_name); +return -1 ; + } + +return atoi (sizebuf); +} + +static u64 device_get_partition_size_sysfs (const char *dev) {int ret;
@@ -351,6 +395,7 @@ static u64 device_get_partition_size_sysfs(const char *dev)const char *name =NULL ;int sysfd;unsigned long long size =0 ; +ssize_t sector_size =0 ; name =realpath (dev, path);if (!name)
@@ -375,7 +420,13 @@ static u64 device_get_partition_size_sysfs(const char *dev)return 0 ; }close (sysfd); -return size; + + sector_size =get_partition_sector_size_sysfs (name); + +if (sector_size <0 ) +return 0 ; + +return size * sector_size; }u64 device_get_partition_size (const char *dev)
Signature · 1 line
2.50.1
Quoted 7 lines · Zoltan Racz
Issue: #979
Changes since v1:
- Added Issue: #979 tag instead of bug description
- Added error handling toget_partition_sector_size_sysfs()
- Changed return value ofget_partition_sector_size_sysfs()to negative on failure
Please put change log behind "---" line so it can be dropped by git-am.
Furthermore, the "Issue:" tag should be put at the end of the commit message, normally before an SoB line.
Although SoB is not mandatory for one-time contribution, but if you want to keep contributing, an SoB will still be preferred.
You can follow the following example on how to put involved tags and changelogs into a single patch:
Quoted 10 lines
When "btrfs device usage /" is being called as a normal user, the function
device_get_partition_sizeindevice-utils.ccallsdevice_get_partition_size_sysfs()as a fallback, which reads the size of the partition from "/sys/class/block/[partition]/size".The problem here is that the size read is not actually the size of the partition but rather a number of how many 512B (or whatever the devices sector size is) sectors the partition contains.
Quoted 7 lines
Ex: if read value is 104857600 the size is not 100MB but 104857600 * 512B = 50GB
This patch adds a function named
get_partition_sector_size_sysfswhich based on the partition name returns the sector size of the device, and replaces "return size" with "return size *sector_size" indevice_get_partition_size_sysfs.
Otherwise looks good to me.
You don't need to resend the patch, I'll do the modification during merge.
Thanks,
Qu
Quoted 83 lines
--- common/device-utils.c | 53 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-)common/device-utils.c +52
-1@@ -342,6 +342,50 @@ u64 device_get_partition_size_fd(int fd)return result; } +static ssize_t get_partition_sector_size_sysfs (const char *name) +{ +int ret =0 ; +char real_path[PATH_MAX ] = {}; +char link_path[PATH_MAX ] = {}; +char *dev_name =NULL ; +int sysfd; +char sysfs[PATH_MAX ] = {}; +char sizebuf[128 ]; + +snprintf (link_path,PATH_MAX ,"/sys/class/block/%s/.." , name); + +if (!realpath (link_path, real_path)) { +error ("Failed to resolve realpath of %s: %s\n" , link_path,strerror (errno)); +return -1 ; + } + + dev_name =basename (real_path); + +if (!dev_name) { +error ("Failed to determine basename for path %s\n" , real_path); +return -1 ; + } + +snprintf (sysfs,PATH_MAX ,"/sys/class/block/%s/queue/hw_sector_size" , dev_name); + + sysfd =open (sysfs,O_RDONLY ); +if (sysfd <0 ) { +error ("Error opening %s to determine dev sector size: %s\n" , real_path,strerror (errno)); +return -1 ; + } + + ret =sysfs_read_file (sysfd, sizebuf,sizeof (sizebuf)); +close (sysfd); + +if (ret <0 ) { +error ("Error reading hw_sector_size from sysfs for device %s!\n" , dev_name); +return -1 ; + } + +return atoi (sizebuf); +} + +static u64 device_get_partition_size_sysfs (const char *dev) {int ret;@@ -351,6 +395,7 @@ static u64 device_get_partition_size_sysfs(const char *dev)const char *name =NULL ;int sysfd;unsigned long long size =0 ; +ssize_t sector_size =0 ; name =realpath (dev, path);if (!name)@@ -375,7 +420,13 @@ static u64 device_get_partition_size_sysfs(const char *dev)return 0 ; }close (sysfd); -return size; + + sector_size =get_partition_sector_size_sysfs (name); + +if (sector_size <0 ) +return 0 ; + +return size * sector_size; }u64 device_get_partition_size (const char *dev)