From: Lennart Poettering Date: Mon, 4 Dec 2023 16:58:33 +0000 (+0100) Subject: blockdev-util: add new helper blockdev_get_device_size() X-Git-Tag: v256-rc1~1490^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=01db9c85cf928de2ab4cfa07acbe35d0f574de44;p=thirdparty%2Fsystemd.git blockdev-util: add new helper blockdev_get_device_size() This function is just a wrapper around the BLKGETSIZE64. Which is a pretty simple ioctl. The only reason to wrap it, is that the headers we need to call it are a bit messy (as "linux/fs.h" is incompatible with certain glibc headers). Hence add the simple helper that wraps it and allows us to do the header mess needed in one file only. It's also nicely symmetric to blockdev_get_sector_size(). --- diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 5bd78a03ed4..32978eb37a7 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -199,7 +199,7 @@ static int block_get_size_by_fd(int fd, uint64_t *ret) { if (!S_ISBLK(st.st_mode)) return -ENOTBLK; - return RET_NERRNO(ioctl(fd, BLKGETSIZE64, ret)); + return blockdev_get_device_size(fd, ret); } static int block_get_size_by_path(const char *path, uint64_t *ret) { @@ -2227,8 +2227,9 @@ int home_create_luks( if (flock(setup->image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */ return log_error_errno(errno, "Failed to lock block device %s: %m", ip); - if (ioctl(setup->image_fd, BLKGETSIZE64, &block_device_size) < 0) - return log_error_errno(errno, "Failed to read block device size: %m"); + r = blockdev_get_device_size(setup->image_fd, &block_device_size); + if (r < 0) + return log_error_errno(r, "Failed to read block device size: %m"); if (h->disk_size == UINT64_MAX) { @@ -3183,8 +3184,9 @@ int home_resize_luks( } else log_info("Operating on whole block device %s.", ip); - if (ioctl(image_fd, BLKGETSIZE64, &old_image_size) < 0) - return log_error_errno(errno, "Failed to determine size of original block device: %m"); + r = blockdev_get_device_size(image_fd, &old_image_size); + if (r < 0) + return log_error_errno(r, "Failed to determine size of original block device: %m"); if (flock(image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */ return log_error_errno(errno, "Failed to lock block device %s: %m", ip); diff --git a/src/partition/growfs.c b/src/partition/growfs.c index 62f3ee6744a..491040ffb08 100644 --- a/src/partition/growfs.c +++ b/src/partition/growfs.c @@ -55,8 +55,9 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_ return log_error_errno(r, "Failed to open main block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(main_devno)); - if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0) - return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m", + r = blockdev_get_device_size(main_devfd, &size); + if (r < 0) + return log_error_errno(r, "Failed to query size of \"%s\" (before resize): %m", main_devpath); log_debug("%s is %"PRIu64" bytes", main_devpath, size); @@ -83,9 +84,9 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_ if (r < 0) return log_error_errno(r, "crypt_resize() of %s failed: %m", devpath); - if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0) - log_warning_errno(errno, "Failed to query size of \"%s\" (after resize): %m", - devpath); + r = blockdev_get_device_size(main_devfd, &size); + if (r < 0) + log_warning_errno(r, "Failed to query size of \"%s\" (after resize): %m", devpath); else log_debug("%s is now %"PRIu64" bytes", main_devpath, size); @@ -250,8 +251,9 @@ static int run(int argc, char *argv[]) { return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devno)); - if (ioctl(devfd, BLKGETSIZE64, &size) != 0) - return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath); + r = blockdev_get_device_size(devfd, &size); + if (r < 0) + return log_error_errno(r, "Failed to query size of \"%s\": %m", devpath); log_debug("Resizing \"%s\" to %"PRIu64" bytes...", arg_target, size); diff --git a/src/partition/repart.c b/src/partition/repart.c index 3a92d1be1c1..1e9284e2e2e 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -6042,8 +6042,9 @@ static int context_open_copy_block_paths( if (S_ISREG(st.st_mode)) size = st.st_size; else if (S_ISBLK(st.st_mode)) { - if (ioctl(source_fd, BLKGETSIZE64, &size) != 0) - return log_error_errno(errno, "Failed to determine size of block device to copy from: %m"); + r = blockdev_get_device_size(source_fd, &size); + if (r < 0) + return log_error_errno(r, "Failed to determine size of block device to copy from: %m"); } else return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", opened); @@ -7365,8 +7366,9 @@ static int resize_backing_fd( assert(loop_device); - if (ioctl(*fd, BLKGETSIZE64, ¤t_size) < 0) - return log_error_errno(errno, "Failed to determine size of block device %s: %m", node); + r = blockdev_get_device_size(*fd, ¤t_size); + if (r < 0) + return log_error_errno(r, "Failed to determine size of block device %s: %m", node); } else { r = stat_verify_regular(&st); if (r < 0) diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index c906aec109c..347cd787c3a 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -777,6 +777,21 @@ int blockdev_get_sector_size(int fd, uint32_t *ret) { return 0; } +int blockdev_get_device_size(int fd, uint64_t *ret) { + uint64_t sz = 0; + + assert(fd >= 0); + assert(ret); + + /* This is just a type-safe wrapper around BLKGETSIZE64 that gets us around having to include messy linux/fs.h in various clients */ + + if (ioctl(fd, BLKGETSIZE64, &sz) < 0) + return -errno; + + *ret = sz; + return 0; +} + int blockdev_get_root(int level, dev_t *ret) { _cleanup_free_ char *p = NULL; dev_t devno; diff --git a/src/shared/blockdev-util.h b/src/shared/blockdev-util.h index 954a23df3e9..28aede289a0 100644 --- a/src/shared/blockdev-util.h +++ b/src/shared/blockdev-util.h @@ -57,5 +57,6 @@ int block_device_has_partitions(sd_device *dev); int blockdev_reread_partition_table(sd_device *dev); int blockdev_get_sector_size(int fd, uint32_t *ret); +int blockdev_get_device_size(int fd, uint64_t *ret); int blockdev_get_root(int level, dev_t *ret); diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index 348880cac88..b3f4c9ab766 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -13,6 +13,7 @@ #include #include "alloc-util.h" +#include "blockdev-util.h" #include "btrfs-util.h" #include "chase.h" #include "chattr-util.h" @@ -446,8 +447,9 @@ static int image_make( read_only = true; } - if (ioctl(block_fd, BLKGETSIZE64, &size) < 0) - log_debug_errno(errno, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path ?: strnull(parent), filename); + r = blockdev_get_device_size(block_fd, &size); + if (r < 0) + log_debug_errno(r, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path ?: strnull(parent), filename); block_fd = safe_close(block_fd); } diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 62aca296f61..5869843c42e 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -1759,8 +1759,9 @@ static int fs_grow(const char *node_path, int mount_fd, const char *mount_path) if (node_fd < 0) return log_debug_errno(errno, "Failed to open node device %s: %m", node_path); - if (ioctl(node_fd, BLKGETSIZE64, &size) != 0) - return log_debug_errno(errno, "Failed to get block device size of %s: %m", node_path); + r = blockdev_get_device_size(node_fd, &size); + if (r < 0) + return log_debug_errno(r, "Failed to get block device size of %s: %m", node_path); if (mount_fd < 0) { assert(mount_path);