From 9102efae6ab7d8ee2607b842f2cdf6cbffaadb86 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 11 Jan 2019 21:58:14 -0800 Subject: block: Alias block_op_close() to block_op_flush() The two functions are identical, so there's no need to keep two copies of the same code around. Alias block_op_close() to block_op_flush() and drop standalone definition for the former. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/block.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'common/block.c') diff --git a/common/block.c b/common/block.c index 8d0de42d90..549df71a9d 100644 --- a/common/block.c +++ b/common/block.c @@ -329,13 +329,6 @@ static ssize_t block_op_write(struct cdev *cdev, const void *buf, size_t count, } #endif -static int block_op_close(struct cdev *cdev) -{ - struct block_device *blk = cdev->priv; - - return writebuffer_flush(blk); -} - static int block_op_flush(struct cdev *cdev) { struct block_device *blk = cdev->priv; @@ -343,6 +336,8 @@ static int block_op_flush(struct cdev *cdev) return writebuffer_flush(blk); } +static int block_op_close(struct cdev *cdev) __alias(block_op_flush); + static struct cdev_operations block_ops = { .read = block_op_read, #ifdef CONFIG_BLOCK_WRITE -- cgit v1.2.3 From c19b36aeb723493489ed84930824b65712906dae Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 11 Jan 2019 21:58:15 -0800 Subject: block: Replace debug() with dev_dbg() All of the functions using debug() in that file have enough info to use dev_dbg instead. Convert all of the uses of debug() to dev_dbg() in order to get more informative debug output. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/block.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'common/block.c') diff --git a/common/block.c b/common/block.c index 549df71a9d..35173c65f1 100644 --- a/common/block.c +++ b/common/block.c @@ -76,7 +76,8 @@ static struct chunk *chunk_get_cached(struct block_device *blk, int block) list_for_each_entry(chunk, &blk->buffered_blocks, list) { if (block >= chunk->block_start && block < chunk->block_start + blk->rdbufsize) { - debug("%s: found %d in %d\n", __func__, block, chunk->num); + dev_dbg(blk->dev, "%s: found %d in %d\n", __func__, + block, chunk->num); /* * move most recently used entry to the head of the list */ @@ -153,8 +154,8 @@ static int block_cache(struct block_device *blk, int block) chunk->block_start = block & ~blk->blkmask; - debug("%s: %d to %d\n", __func__, chunk->block_start, - chunk->num); + dev_dbg(blk->dev, "%s: %d to %d\n", __func__, chunk->block_start, + chunk->num); num_blocks = min(blk->rdbufsize, blk->num_blocks - chunk->block_start); @@ -364,8 +365,8 @@ int blockdevice_register(struct block_device *blk) INIT_LIST_HEAD(&blk->idle_blocks); blk->blkmask = blk->rdbufsize - 1; - debug("%s: rdbufsize: %d blockbits: %d blkmask: 0x%08x\n", __func__, blk->rdbufsize, blk->blockbits, - blk->blkmask); + dev_dbg(blk->dev, "rdbufsize: %d blockbits: %d blkmask: 0x%08x\n", + blk->rdbufsize, blk->blockbits, blk->blkmask); for (i = 0; i < 32; i++) { struct chunk *chunk = xzalloc(sizeof(*chunk)); -- cgit v1.2.3 From 17c4b989a5d00fe5fc84c528bbd59583c45d80d0 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 21 Jan 2019 22:06:24 -0800 Subject: block: Do not write past block device boundary during a flush When calling I/O functions of underlying block device driver we always need to make sure that its size is small enough to not go past device's boundary. Not only in get_chunk() and block_cache(), but in writebuffer_flush() as well. Since the same code is used in three different places, move it into a subroutine and adjust all of the calls to ->write()/->read() accordingly. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/block.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'common/block.c') diff --git a/common/block.c b/common/block.c index 35173c65f1..3a031a4fc7 100644 --- a/common/block.c +++ b/common/block.c @@ -38,6 +38,11 @@ struct chunk { #define BUFSIZE (PAGE_SIZE * 4) +static int writebuffer_io_len(struct block_device *blk, struct chunk *chunk) +{ + return min(blk->rdbufsize, blk->num_blocks - chunk->block_start); +} + /* * Write all dirty chunks back to the device */ @@ -51,7 +56,9 @@ static int writebuffer_flush(struct block_device *blk) list_for_each_entry(chunk, &blk->buffered_blocks, list) { if (chunk->dirty) { - ret = blk->ops->write(blk, chunk->data, chunk->block_start, blk->rdbufsize); + ret = blk->ops->write(blk, chunk->data, + chunk->block_start, + writebuffer_io_len(blk, chunk)); if (ret < 0) return ret; @@ -118,10 +125,9 @@ static struct chunk *get_chunk(struct block_device *blk) /* use last entry which is the most unused */ chunk = list_last_entry(&blk->buffered_blocks, struct chunk, list); if (chunk->dirty) { - size_t num_blocks = min(blk->rdbufsize, - blk->num_blocks - chunk->block_start); - ret = blk->ops->write(blk, chunk->data, chunk->block_start, - num_blocks); + ret = blk->ops->write(blk, chunk->data, + chunk->block_start, + writebuffer_io_len(blk, chunk)); if (ret < 0) return ERR_PTR(ret); @@ -145,7 +151,6 @@ static struct chunk *get_chunk(struct block_device *blk) static int block_cache(struct block_device *blk, int block) { struct chunk *chunk; - size_t num_blocks; int ret; chunk = get_chunk(blk); @@ -157,9 +162,8 @@ 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); - num_blocks = min(blk->rdbufsize, blk->num_blocks - chunk->block_start); - - ret = blk->ops->read(blk, chunk->data, chunk->block_start, num_blocks); + ret = blk->ops->read(blk, chunk->data, chunk->block_start, + writebuffer_io_len(blk, chunk)); if (ret) { list_add_tail(&chunk->list, &blk->idle_blocks); return ret; -- cgit v1.2.3 From f24245742636ef9996aae66e92244aed659b7d72 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 21 Jan 2019 22:06:25 -0800 Subject: block: Move shared code in get_chunk() out of if statement Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/block.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'common/block.c') diff --git a/common/block.c b/common/block.c index 3a031a4fc7..2917218762 100644 --- a/common/block.c +++ b/common/block.c @@ -133,13 +133,12 @@ static struct chunk *get_chunk(struct block_device *blk) chunk->dirty = 0; } - - list_del(&chunk->list); } else { chunk = list_first_entry(&blk->idle_blocks, struct chunk, list); - list_del(&chunk->list); } + list_del(&chunk->list); + return chunk; } -- cgit v1.2.3