diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 4bd89d37cb..b1d07fe708 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -208,13 +208,27 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) { return d->fd; } -int loop_device_refresh_size(LoopDevice *d) { +int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) { + struct loop_info64 info; assert(d); if (d->fd < 0) return -EBADF; - if (ioctl(d->fd, LOOP_SET_CAPACITY) < 0) + if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0) + return -errno; + + if (size == UINT64_MAX && offset == UINT64_MAX) + return 0; + if (info.lo_sizelimit == size && info.lo_offset == offset) + return 0; + + if (size != UINT64_MAX) + info.lo_sizelimit = size; + if (offset != UINT64_MAX) + info.lo_offset = offset; + + if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) return -errno; return 0; diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index 50b959fe25..e5a0ae75b8 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -27,4 +27,4 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(LoopDevice*, loop_device_unref); void loop_device_relinquish(LoopDevice *d); -int loop_device_refresh_size(LoopDevice *d); +int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size);