Linux-Block Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] loop: fix no-unmap write-zeroes request behavior
@ 2019-10-10 17:02 Darrick J. Wong
  2019-10-11  7:51 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Darrick J. Wong @ 2019-10-10 17:02 UTC (permalink / raw)
  To: Jens Axboe, Christoph Hellwig; +Cc: linux-block, linux-fsdevel, xfs

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>
---
 drivers/block/loop.c |   32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index f6f77eaa7217..0dc981e94bf0 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -441,6 +441,35 @@ 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;
+
+	/* If we're allowed to unmap the blocks, ask the fs to punch them. */
+	if (!(rq->cmd_flags & REQ_NOUNMAP)) {
+		ret = lo_discard(lo, rq, pos);
+		if (!ret)
+			return 0;
+	}
+
+	/*
+	 * Otherwise, ask the fs to zero out the blocks, which will 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;
@@ -597,8 +626,9 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
 	case REQ_OP_FLUSH:
 		return lo_req_flush(lo, rq);
 	case REQ_OP_DISCARD:
-	case REQ_OP_WRITE_ZEROES:
 		return lo_discard(lo, rq, pos);
+	case REQ_OP_WRITE_ZEROES:
+		return lo_zeroout(lo, rq, pos);
 	case REQ_OP_WRITE:
 		if (lo->transfer)
 			return lo_write_transfer(lo, rq, pos);

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-10 17:02 [PATCH] loop: fix no-unmap write-zeroes request behavior Darrick J. Wong
2019-10-11  7:51 ` Christoph Hellwig
2019-10-11 16:05 ` [PATCH v2] " Darrick J. Wong
2019-10-14  7:28   ` Christoph Hellwig
2019-10-14 15:50 ` [PATCH v3] " Darrick J. Wong
2019-10-14 16:39   ` Eric Sandeen
2019-10-14 17:00     ` Darrick J. Wong
2019-10-15  7:58   ` Christoph Hellwig

Linux-Block Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-block/0 linux-block/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-block linux-block/ https://lore.kernel.org/linux-block \
		linux-block@vger.kernel.org
	public-inbox-index linux-block

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-block


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git