diff mbox series

[1/2] btrfs-progs: use btrfs_device_size() instead of device_get_partition_size_fd()

Message ID 20210922075343.1160788-2-naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: fixes for mkfs.btrfs on file with emulated zones | expand

Commit Message

Naohiro Aota Sept. 22, 2021, 7:53 a.m. UTC
device_get_partition_size_fd() fails if we pass a regular file. This can
happen when trying to create an emualted zoned btrfs on a regular file.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 kernel-shared/zoned.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
index 75c6c53393ac..891a2c0aeef2 100644
--- a/kernel-shared/zoned.c
+++ b/kernel-shared/zoned.c
@@ -267,6 +267,7 @@  static int report_zones(int fd, const char *file,
 	u64 zone_bytes = zone_size(file);
 	size_t rep_size;
 	u64 sector = 0;
+	struct stat st;
 	struct blk_zone_report *rep;
 	struct blk_zone *zone;
 	unsigned int i, n = 0;
@@ -291,11 +292,13 @@  static int report_zones(int fd, const char *file,
 		exit(1);
 	}
 
-	/*
-	 * No need to use btrfs_device_size() here, since it is ensured
-	 * that the file is block device.
-	 */
-	device_size = device_get_partition_size_fd(fd);
+	ret = fstat(fd, &st);
+	if (ret < 0) {
+		error("unable to stat %s: %m", file);
+		return -EIO;
+	}
+
+	device_size = btrfs_device_size(fd, &st);
 	if (device_size == 0) {
 		error("zoned: failed to read size of %s: %m", file);
 		exit(1);