diff mbox series

[v2] loop: fix no-unmap write-zeroes request behavior

Message ID 20191011160545.GD13098@magnolia (mailing list archive)
State New, archived
Headers show
Series [v2] loop: fix no-unmap write-zeroes request behavior | expand

Commit Message

Darrick J. Wong Oct. 11, 2019, 4:05 p.m. UTC
From: Darrick J. Wong <darrick.wong@oracle.com>

Currently, if the loop device receives a WRITE_ZEROES request, it asks
the underlying filesystem to punch out the range.  This behavior is
correct if unmapping is allowed.  However, a NOUNMAP request means that
the caller forbids us from freeing the storage backing the range, so
punching out the range is incorrect behavior.

To satisfy a NOUNMAP | WRITE_ZEROES request, loop should ask the
underlying filesystem to FALLOC_FL_ZERO_RANGE, which is (according to
the fallocate documentation) required to ensure that the entire range is
backed by real storage, which suffices for our purposes.

Fixes: 19372e2769179dd ("loop: implement REQ_OP_WRITE_ZEROES")
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
v2: reorganize a little according to hch feedback
---
 drivers/block/loop.c |   31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

Comments

Christoph Hellwig Oct. 14, 2019, 7:28 a.m. UTC | #1
While this looks generally good to me, I have another nitpick to avoid
code duplication.  What about just renaming lo_discard to lo_fallocate
and pass the mode (possibly minus the FALLOC_FL_KEEP_SIZE flag) to it?

The in the do_req_filebacked we could further simplify it down to:

  	case REQ_OP_WRITE_ZEROES:
		/*
		 * If the caller doesn't want deallocation, call zeroout to
		 * write zeroes the range.  Otherwise, punch them out.
		 */
		return lo_fallocate(lo, rq, pos,
			(rq->cmd_flags & REQ_NOUNMAP) ?
				FALLOC_FL_ZERO_RANGE : FALLOC_FL_PUNCH_HOLE);
		break;
	case REQ_OP_DISCARD:
		return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
diff mbox series

Patch

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index f6f77eaa7217..4943d0c5c61c 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -441,6 +441,28 @@  static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
 	return ret;
 }
 
+static int lo_zeroout(struct loop_device *lo, struct request *rq, loff_t pos)
+{
+	struct file *file = lo->lo_backing_file;
+	int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE;
+	int ret;
+
+	/*
+	 * Ask the fs to zero out the blocks, which is supposed to result in
+	 * space being allocated to the file.
+	 */
+	if (!file->f_op->fallocate) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+
+	ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
+	if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
+		ret = -EIO;
+ out:
+	return ret;
+}
+
 static int lo_req_flush(struct loop_device *lo, struct request *rq)
 {
 	struct file *file = lo->lo_backing_file;
@@ -596,8 +618,15 @@  static int do_req_filebacked(struct loop_device *lo, struct request *rq)
 	switch (req_op(rq)) {
 	case REQ_OP_FLUSH:
 		return lo_req_flush(lo, rq);
-	case REQ_OP_DISCARD:
 	case REQ_OP_WRITE_ZEROES:
+		/*
+		 * If the caller doesn't want deallocation, call zeroout to
+		 * write zeroes the range.  Otherwise, punch them out.
+		 */
+		if (rq->cmd_flags & REQ_NOUNMAP)
+			return lo_zeroout(lo, rq, pos);
+		/* fall through */
+	case REQ_OP_DISCARD:
 		return lo_discard(lo, rq, pos);
 	case REQ_OP_WRITE:
 		if (lo->transfer)