mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 02/10] block: factor out chunk_flush helper
Date: Tue, 30 Jul 2024 09:19:21 +0200	[thread overview]
Message-ID: <20240730071929.2016537-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240730071929.2016537-1-a.fatoum@pengutronix.de>

We have two places where we flush chunks: when explicitly flushing
all buffered blocks and when there are no idle blocks.

A third place will be added soon for the new erasure callback, so
prepare for that by factoring out the helper.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/block.c | 42 ++++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/common/block.c b/common/block.c
index c7ca4fb4034a..f55da775a797 100644
--- a/common/block.c
+++ b/common/block.c
@@ -31,6 +31,24 @@ static int writebuffer_io_len(struct block_device *blk, struct chunk *chunk)
 	return min_t(blkcnt_t, blk->rdbufsize, blk->num_blocks - chunk->block_start);
 }
 
+static int chunk_flush(struct block_device *blk, struct chunk *chunk)
+{
+	int ret;
+
+	if (!chunk->dirty)
+		return 0;
+
+	ret = blk->ops->write(blk, chunk->data,
+			      chunk->block_start,
+			      writebuffer_io_len(blk, chunk));
+	if (ret < 0)
+		return ret;
+
+	chunk->dirty = 0;
+
+	return 0;
+}
+
 /*
  * Write all dirty chunks back to the device
  */
@@ -43,15 +61,9 @@ static int writebuffer_flush(struct block_device *blk)
 		return 0;
 
 	list_for_each_entry(chunk, &blk->buffered_blocks, list) {
-		if (chunk->dirty) {
-			ret = blk->ops->write(blk, chunk->data,
-					      chunk->block_start,
-					      writebuffer_io_len(blk, chunk));
-			if (ret < 0)
-				return ret;
-
-			chunk->dirty = 0;
-		}
+		ret = chunk_flush(blk, chunk);
+		if (ret < 0)
+			return ret;
 	}
 
 	if (blk->ops->flush)
@@ -112,15 +124,9 @@ static struct chunk *get_chunk(struct block_device *blk)
 	if (list_empty(&blk->idle_blocks)) {
 		/* use last entry which is the most unused */
 		chunk = list_last_entry(&blk->buffered_blocks, struct chunk, list);
-		if (chunk->dirty) {
-			ret = blk->ops->write(blk, chunk->data,
-					      chunk->block_start,
-					      writebuffer_io_len(blk, chunk));
-			if (ret < 0)
-				return ERR_PTR(ret);
-
-			chunk->dirty = 0;
-		}
+		ret = chunk_flush(blk, chunk);
+		if (ret < 0)
+			return ERR_PTR(ret);
 	} else {
 		chunk = list_first_entry(&blk->idle_blocks, struct chunk, list);
 	}
-- 
2.39.2




  parent reply	other threads:[~2024-07-30  7:20 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30  7:19 [PATCH 00/10] mmc: add SD/eMMC erase support Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 01/10] fs: give erase() a new erase_type parameter Ahmad Fatoum
2024-07-30  8:31   ` Sascha Hauer
2024-07-30  8:32     ` Ahmad Fatoum
2024-07-30  7:19 ` Ahmad Fatoum [this message]
2024-07-30  7:19 ` [PATCH 03/10] block: allow block devices to implement the cdev erase operation Ahmad Fatoum
2024-07-30  8:55   ` Sascha Hauer
2024-07-30 11:10     ` Ahmad Fatoum
2024-07-30 11:21       ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 04/10] mci: turn bool members into bitfield in struct mci Ahmad Fatoum
2024-07-30  9:06   ` Sascha Hauer
2024-07-30 11:10     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 05/10] mci: describe more command structure in mci.h Ahmad Fatoum
2024-07-30  9:25   ` Yann Sionneau
2024-07-30 11:07     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 06/10] mci: core: use CONFIG_MCI_WRITE, not CONFIG_BLOCK_WRITE Ahmad Fatoum
2024-07-30  9:18   ` Sascha Hauer
2024-07-30 11:08     ` Ahmad Fatoum
2024-07-31  7:19       ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 07/10] mci: add support for discarding write blocks Ahmad Fatoum
2024-07-30  9:23   ` Yann Sionneau
2024-07-30 11:14     ` Ahmad Fatoum
2024-07-30 10:05   ` Sascha Hauer
2024-07-30 11:17     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 08/10] commands: sync: add new command to flush cached writes Ahmad Fatoum
2024-07-30 10:08   ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 09/10] mci: core: remove reference to SD Card from common mci_card_probe Ahmad Fatoum
2024-07-30 10:09   ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 10/10] commands: blkstats: add command to print block device statistics Ahmad Fatoum

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=20240730071929.2016537-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@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