From: Lennart Poettering Date: Mon, 4 Dec 2023 17:01:39 +0000 (+0100) Subject: loop-util: also store the device size in LoopDevice X-Git-Tag: v256-rc1~1490^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c961a8c605a36bb8035380db3385be9ba2e45460;p=thirdparty%2Fsystemd.git loop-util: also store the device size in LoopDevice That makes the field easily accessible, just as the sector size. --- diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 32978eb37a7..57016fb14b0 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -1295,9 +1295,6 @@ int home_setup_luks( if (!IN_SET(errno, ENOTTY, EINVAL)) return log_error_errno(errno, "Failed to get block device metrics of %s: %m", n); - if (ioctl(setup->loop->fd, BLKGETSIZE64, &size) < 0) - return log_error_errno(r, "Failed to read block device size of %s: %m", n); - if (fstat(setup->loop->fd, &st) < 0) return log_error_errno(r, "Failed to stat block device %s: %m", n); assert(S_ISBLK(st.st_mode)); @@ -1329,6 +1326,8 @@ int home_setup_luks( offset *= 512U; } + + size = setup->loop->device_size; } else { #if HAVE_VALGRIND_MEMCHECK_H VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index b11e9268aff..c337e881fe2 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -149,8 +149,9 @@ static int loop_configure_verify(int fd, const struct loop_config *c) { * effect hence. And if not use classic LOOP_SET_STATUS64. */ uint64_t z; - if (ioctl(fd, BLKGETSIZE64, &z) < 0) - return -errno; + r = blockdev_get_device_size(fd, &z); + if (r < 0) + return r; if (z != c->info.lo_sizelimit) { log_debug("LOOP_CONFIGURE is broken, doesn't honour .info.lo_sizelimit. Falling back to LOOP_SET_STATUS64."); @@ -404,6 +405,11 @@ static int loop_configure( assert_not_reached(); } + uint64_t device_size; + r = blockdev_get_device_size(loop_with_fd, &device_size); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get loopback device size: %m"); + LoopDevice *d = new(LoopDevice, 1); if (!d) return log_oom_debug(); @@ -420,6 +426,7 @@ static int loop_configure( .uevent_seqnum_not_before = seqnum, .timestamp_not_before = timestamp, .sector_size = c->block_size, + .device_size = device_size, }; *ret = TAKE_PTR(d); @@ -944,6 +951,11 @@ int loop_device_open( if (r < 0) return r; + uint64_t device_size; + r = blockdev_get_device_size(fd, &device_size); + if (r < 0) + return r; + r = sd_device_get_devnum(dev, &devnum); if (r < 0) return r; @@ -976,6 +988,7 @@ int loop_device_open( .uevent_seqnum_not_before = UINT64_MAX, .timestamp_not_before = USEC_INFINITY, .sector_size = sector_size, + .device_size = device_size, }; *ret = d; @@ -1057,8 +1070,9 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) { return -EINVAL; current_offset *= 512U; - if (ioctl(partition_fd, BLKGETSIZE64, ¤t_size) < 0) - return -EINVAL; + r = blockdev_get_device_size(partition_fd, ¤t_size); + if (r < 0) + return r; if (size == UINT64_MAX && offset == UINT64_MAX) return 0; diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index d77c314af83..194e7f78d4e 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -28,6 +28,7 @@ struct LoopDevice { uint64_t uevent_seqnum_not_before; /* uevent sequm right before we attached the loopback device, or UINT64_MAX if we don't know */ usec_t timestamp_not_before; /* CLOCK_MONOTONIC timestamp taken immediately before attaching the loopback device, or USEC_INFINITY if we don't know */ uint32_t sector_size; + uint64_t device_size; }; /* Returns true if LoopDevice object is not actually a loopback device but some other block device we just wrap */