From d2430d50970deda19ac851669e685a1cc72a8200 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 6 Mar 2023 12:07:18 +0100 Subject: [PATCH] loop-util: add call for setting the autoclear flag at arbitrary times --- src/shared/loop-util.c | 19 +++++++++++++++++++ src/shared/loop-util.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 96a42b41257..27a58952bbd 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -1111,3 +1111,22 @@ int loop_device_sync(LoopDevice *d) { return RET_NERRNO(fsync(d->fd)); } + +int loop_device_set_autoclear(LoopDevice *d, bool autoclear) { + struct loop_info64 info; + + assert(d); + + if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0) + return -errno; + + if (autoclear == FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR)) + return 0; + + SET_FLAG(info.lo_flags, LO_FLAGS_AUTOCLEAR, autoclear); + + if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) + return -errno; + + return 1; +} diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index ea461434e9c..c98b69ceee7 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -49,3 +49,5 @@ int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size); int loop_device_flock(LoopDevice *d, int operation); int loop_device_sync(LoopDevice *d); + +int loop_device_set_autoclear(LoopDevice *d, bool autoclear); -- 2.47.3