From: Lennart Poettering Date: Mon, 6 Mar 2023 11:07:18 +0000 (+0100) Subject: loop-util: add call for setting the autoclear flag at arbitrary times X-Git-Tag: v254-rc1~1072^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d2430d50970deda19ac851669e685a1cc72a8200;p=thirdparty%2Fsystemd.git loop-util: add call for setting the autoclear flag at arbitrary times --- 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);