diff mbox series

loop: disable write cache in __loop_clr_fd()

Message ID 20201203213400.84340-1-mfo@canonical.com (mailing list archive)
State New, archived
Headers show
Series loop: disable write cache in __loop_clr_fd() | expand

Commit Message

Mauricio Faria de Oliveira Dec. 3, 2020, 9:34 p.m. UTC
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 <mfo@canonical.com>
Co-developed-by: Eric Desrochers <eric.desrochers@canonical.com>
Signed-off-by: Eric Desrochers <eric.desrochers@canonical.com>
---
 drivers/block/loop.c | 3 +++
 1 file changed, 3 insertions(+)
diff mbox series

Patch

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);