From f6c30640bc64e470a49a2721c7466273d67212e8 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 29 Apr 2021 20:13:49 +0200 Subject: [PATCH] btrfs-progs: add fd version of device_get_partition_size The helper wraps a raw ioctl but some users may already have the fd and not necessarily the path. Add a suitable helper for convenience. Signed-off-by: David Sterba --- common/device-utils.c | 10 ++++++++++ common/device-utils.h | 1 + 2 files changed, 11 insertions(+) diff --git a/common/device-utils.c b/common/device-utils.c index 0e63d94f..4c7cfd4a 100644 --- a/common/device-utils.c +++ b/common/device-utils.c @@ -288,6 +288,16 @@ u64 btrfs_device_size(int fd, struct stat *st) /* * Read partition size using the low-level ioctl */ +u64 device_get_partition_size_fd(int fd) +{ + u64 result; + + if (ioctl(fd, BLKGETSIZE64, &result) < 0) + return 0; + + return result; +} + u64 device_get_partition_size(const char *dev) { u64 result; diff --git a/common/device-utils.h b/common/device-utils.h index 75277932..4113664d 100644 --- a/common/device-utils.h +++ b/common/device-utils.h @@ -34,6 +34,7 @@ int device_discard_blocks(int fd, u64 start, u64 len); int device_zero_blocks(int fd, off_t start, size_t len); u64 device_get_partition_size(const char *dev); +u64 device_get_partition_size_fd(int fd); int device_get_queue_param(const char *file, const char *param, char *buf, size_t len); /*