A detached loop device can hit an I/O error on fsync() if it was
previously associated/attached.
This happens because the write cache is enabled in loop_configure()
in the attach path, but not disabled anywhere in the detach path.
So, disable write cache in the detach path.
Before:
# losetup /dev/loop7 disk.img
# losetup -d /dev/loop7
# strace -e fsync parted -s /dev/loop7 unit s print 2>&1 | grep fsync
fsync(3) = -1 EIO (Input/output error)
Warning: Error fsyncing/closing /dev/loop7: Input/output error
[ 34.696133] blk_update_request: I/O error, dev loop7, sector 0 op
0x1:(WRITE) flags 0x800 phys_seg 0 prio class 0
After:
# losetup /dev/loop7 disk.img
# losetup -d /dev/loop7
# strace -e fsync parted -s /dev/loop7 unit s print 2>&1 | grep fsync
fsync(3) = 0
Signed-off-by: Mauricio Faria de Oliveira <redacted>
Co-developed-by: Eric Desrochers <redacted>
Signed-off-by: Eric Desrochers <redacted>
---
drivers/block/loop.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a58084c2ed7c..0e23ab151667 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1223,6 +1223,9 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
goto out_unlock;
}
+ if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && filp->f_op->fsync)
+ blk_queue_write_cache(lo->lo_queue, false, false);
+
/* freeze request queue during the transition */
blk_mq_freeze_queue(lo->lo_queue);
--
2.25.1