summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2019-12-13 12:16:51 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2020-02-14 12:16:20 +0100
commite488952b9d04fb0fc7dddd31ec639549d71c76b3 (patch)
treee8ba5665c2a15d081d684067ba1ab5f0722fa07d /common
parenta18f5ad97df6b3d0768c8b741e85da077f990cc7 (diff)
downloadbarebox-e488952b9d04fb0fc7dddd31ec639549d71c76b3.tar.gz
barebox-e488952b9d04fb0fc7dddd31ec639549d71c76b3.tar.xz
block: Implement discard_range
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>
Diffstat (limited to 'common')
-rw-r--r--common/block.c21
1 files changed, 21 insertions, 0 deletions
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)