mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 3/4] block: Implement discard_range
Date: Fri, 14 Feb 2020 12:16:40 +0100	[thread overview]
Message-ID: <20200214111641.6350-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20200214111641.6350-1-s.hauer@pengutronix.de>

This implements the discard_range hook. When a range of data is
discarded then we do not have to read it from the device and can
pass a zeroed buffer instead.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/block.c  | 21 +++++++++++++++++++++
 include/block.h |  3 +++
 2 files changed, 24 insertions(+)

diff --git a/common/block.c b/common/block.c
index 97cf5dc4de..8b43c3c83a 100644
--- a/common/block.c
+++ b/common/block.c
@@ -161,6 +161,14 @@ static int block_cache(struct block_device *blk, int block)
 	dev_dbg(blk->dev, "%s: %d to %d\n", __func__, chunk->block_start,
 		chunk->num);
 
+	if (chunk->block_start * BLOCKSIZE(blk) >= blk->discard_start &&
+	    chunk->block_start * BLOCKSIZE(blk) + writebuffer_io_len(blk, chunk)
+	    <= blk->discard_start + blk->discard_size) {
+		memset(chunk->data, 0, writebuffer_io_len(blk, chunk));
+		list_add(&chunk->list, &blk->buffered_blocks);
+		return 0;
+	}
+
 	ret = blk->ops->read(blk, chunk->data, chunk->block_start,
 			     writebuffer_io_len(blk, chunk));
 	if (ret) {
@@ -337,11 +345,23 @@ static int block_op_flush(struct cdev *cdev)
 {
 	struct block_device *blk = cdev->priv;
 
+	blk->discard_start = blk->discard_size = 0;
+
 	return writebuffer_flush(blk);
 }
 
 static int block_op_close(struct cdev *cdev) __alias(block_op_flush);
 
+static int block_op_discard_range(struct cdev *cdev, loff_t count, loff_t offset)
+{
+	struct block_device *blk = cdev->priv;
+
+	blk->discard_start = offset;
+	blk->discard_size = count;
+
+	return 0;
+}
+
 static struct cdev_operations block_ops = {
 	.read	= block_op_read,
 #ifdef CONFIG_BLOCK_WRITE
@@ -349,6 +369,7 @@ static struct cdev_operations block_ops = {
 #endif
 	.close	= block_op_close,
 	.flush	= block_op_flush,
+	.discard_range = block_op_discard_range,
 };
 
 int blockdevice_register(struct block_device *blk)
diff --git a/include/block.h b/include/block.h
index 91377679b0..d35c4ecdf4 100644
--- a/include/block.h
+++ b/include/block.h
@@ -23,6 +23,9 @@ struct block_device {
 	int rdbufsize;
 	int blkmask;
 
+	loff_t discard_start;
+	loff_t discard_size;
+
 	struct list_head buffered_blocks;
 	struct list_head idle_blocks;
 
-- 
2.25.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2020-02-14 11:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-14 11:16 [PATCH 1/4] fs: Introduce discard_range() Sascha Hauer
2020-02-14 11:16 ` [PATCH 2/4] cdev: Add discard_range hook Sascha Hauer
2020-02-14 11:16 ` Sascha Hauer [this message]
2020-02-14 11:16 ` [PATCH 4/4] copy_file: call discard_range on destination file Sascha Hauer
  -- strict thread matches above, loose matches on Subject: below --
2019-12-13 13:20 [PATCH 0/4] add discard_range to improve write speed on block devices Sascha Hauer
2019-12-13 13:20 ` [PATCH 3/4] block: Implement discard_range Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200214111641.6350-3-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox