Skip to content

Commit cae1e8f

Browse files
poetteringkeszybz
authored andcommitted
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.
1 parent 3660da1 commit cae1e8f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/shared/loop-util.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
191191
return NULL;
192192

193193
if (d->fd >= 0) {
194+
/* Implicitly sync the device, since otherwise in-flight blocks might not get written */
195+
if (fsync(d->fd) < 0)
196+
log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m");
197+
194198
if (d->nr >= 0 && !d->relinquished) {
195199
if (ioctl(d->fd, LOOP_CLR_FD) < 0)
196200
log_debug_errno(errno, "Failed to clear loop device: %m");
@@ -216,7 +220,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
216220
log_warning_errno(errno, "Failed to remove device %s: %m", strna(d->node));
217221
break;
218222
}
219-
usleep(50 * USEC_PER_MSEC);
223+
(void) usleep(50 * USEC_PER_MSEC);
220224
}
221225
}
222226

0 commit comments

Comments
 (0)