Skip to contentSkip to conversation

btrfs-progs: Fixing invalid device size output when "btrfs device usage /" called as normal user

PATCH In review Zoltan Racz 2 participants 2 messages Jul 31, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

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
index 783d7955..9b02a0e8 100644
@@ -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
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Zoltan Racz

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

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:

https://lore.kernel.org/linux-btrfs/aa5a3d849cb093a767e08616258c03c7eec8fe26.1753806780.git.boris@bur.io/

Quoted 10 lines

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.

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_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.

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
index 783d7955..9b02a0e8 100644
@@ -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)