Skip to contentSkip to conversation

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

PATCH Superseded Zoltan Racz 2 participants 2 messages Jul 30, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

This patch fixes bug #979 - "wrong and misleading" device usage output as normal user

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 | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
common/device-utils.c +41 -1
index 783d7955..68aaa538 100644
@@ -342,6 +342,42 @@ u64 device_get_partition_size_fd(int fd)
 	return result;
 }
 
+static u64 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)) 
+		return 0;
+
+	dev_name = basename(real_path);
+
+	if (!dev_name)
+		return 0;
+
+	snprintf(sysfs, PATH_MAX, "/sys/class/block/%s/queue/hw_sector_size", dev_name);
+	
+	sysfd = open(sysfs, O_RDONLY);
+	if (sysfd < 0)
+		return 0;
+
+	ret = sysfs_read_file(sysfd, sizebuf, sizeof(sizebuf));
+	close(sysfd);
+
+	if (ret < 0) 
+		return 0;
+
+	return atoi(sizebuf);
+}
+
+
 static u64 device_get_partition_size_sysfs(const char *dev)
 {
 	int ret;
@@ -351,6 +387,7 @@ static u64 device_get_partition_size_sysfs(const char *dev)
 	const char *name = NULL;
 	int sysfd;
 	unsigned long long size = 0;
+	unsigned long long sector_size = 0;
 
 	name = realpath(dev, path);
 	if (!name)
@@ -375,7 +412,10 @@ static u64 device_get_partition_size_sysfs(const char *dev)
 		return 0;
 	}
 	close(sysfd);
-	return size;
+
+	sector_size = get_partition_sector_size_sysfs(name);
+
+	return size * sector_size;
 }
 
 u64 device_get_partition_size(const char *dev)
Signature · 1 line
2.50.1
#1 lore raw
Quoted 1 line · Zoltan Racz

This patch fixes bug #979 - "wrong and misleading" device usage output as normal user

This can be replaced by "Issue: #979" tag.

And github will automatically handle the reference.

Quoted 40 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.

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 | 42 +++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 41 insertions(+), 1 deletion(-)
common/device-utils.c +9 -0
index 783d7955..68aaa538 100644
@@ -342,6 +342,42 @@ u64 device_get_partition_size_fd(int fd)
  	return result;
  }
 
+static u64 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];

common/device-utils.c u64 device_get_partition_size_fd(int fd) · line 363

Quoted 10 lines
common/device-utils.c +10 -0
+
+	snprintf(link_path, PATH_MAX, "/sys/class/block/%s/..", name);
+
+	if (!realpath(link_path, real_path))
+		return 0;
+
+	dev_name = basename(real_path);
+
+	if (!dev_name)
+		return 0;

No error handling.

common/device-utils.c u64 device_get_partition_size_fd(int fd) · line 369

Quoted 6 lines
common/device-utils.c +6 -0
+
+	snprintf(sysfs, PATH_MAX, "/sys/class/block/%s/queue/hw_sector_size", dev_name);
+	
+	sysfd = open(sysfs, O_RDONLY);
+	if (sysfd < 0)
+		return 0;

The same missing error handling.

common/device-utils.c u64 device_get_partition_size_fd(int fd) · line 377

Quoted 8 lines
common/device-utils.c +8 -0
+
+	ret = sysfs_read_file(sysfd, sizebuf, sizeof(sizebuf));
+	close(sysfd);
+
+	if (ret < 0)
+		return 0;
+
+	return atoi(sizebuf);

Again returning 0 on error.

Quoted 15 lines
common/device-utils.c +4 -0
+}
+
+
  static u64 device_get_partition_size_sysfs(const char *dev)
  {
  	int ret;
@@ -351,6 +387,7 @@ static u64 device_get_partition_size_sysfs(const char *dev)
  	const char *name = NULL;
  	int sysfd;
  	unsigned long long size = 0;
+	unsigned long long sector_size = 0;
 
  	name = realpath(dev, path);
  	if (!name)

common/device-utils.c static u64 device_get_partition_size_sysfs(const char *dev) · line 416

Quoted 6 lines
common/device-utils.c +2 -1
@@ -375,7 +412,10 @@ static u64 device_get_partition_size_sysfs(const char *dev)
  		return 0;
  	}
  	close(sysfd);
-	return size;
+
+	sector_size = get_partition_sector_size_sysfs(name);

So when on error, sector_size is 0 and device_get_partition_size() just return 0, causing even more confusing result.

I'd prefer get_parition_sector_size_sysfs() to return minus number for error, and output a warning when we failed to get the correct value.

Thanks,
Qu

Quoted 5 lines
common/device-utils.c +2 -0
+
+	return size * sector_size;
  }
 
  u64 device_get_partition_size(const char *dev)