From: Lennart Poettering Date: Thu, 30 Jul 2020 16:42:13 +0000 (+0200) Subject: loop-device: implicitly sync device on detach X-Git-Tag: v246~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cae1e8fb88c5a6b0960a2d0be3df8755f0c78462;p=thirdparty%2Fsystemd.git loop-device: implicitly sync device on detach Apparently, if IO is still in flight at the moment we invoke LOOP_CLR_FD it is likely simply dropped (probably because yanking physical storage, such as a USB stick would drop it too). Let's protect ourselves against that and always sync explicitly before we invoke it. --- diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 7aee239e33a..4a593b05f3c 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -191,6 +191,10 @@ LoopDevice* loop_device_unref(LoopDevice *d) { return NULL; if (d->fd >= 0) { + /* Implicitly sync the device, since otherwise in-flight blocks might not get written */ + if (fsync(d->fd) < 0) + log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m"); + if (d->nr >= 0 && !d->relinquished) { if (ioctl(d->fd, LOOP_CLR_FD) < 0) log_debug_errno(errno, "Failed to clear loop device: %m"); @@ -216,7 +220,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { log_warning_errno(errno, "Failed to remove device %s: %m", strna(d->node)); break; } - usleep(50 * USEC_PER_MSEC); + (void) usleep(50 * USEC_PER_MSEC); } }