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 4/7] block: reparse partition table when necessary
Date: Thu, 15 Feb 2024 08:47:54 +0100	[thread overview]
Message-ID: <20240215074757.960200-5-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20240215074757.960200-1-s.hauer@pengutronix.de>

Call reparse_partition_table() when the partition table may have
changed. We detect this by recording if the block device has been
written to in the area where the partition table is.

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

diff --git a/common/block.c b/common/block.c
index 3a4a9fb731..007c8bed12 100644
--- a/common/block.c
+++ b/common/block.c
@@ -284,6 +284,17 @@ static ssize_t block_op_write(struct cdev *cdev, const void *buf, size_t count,
 	blkcnt_t blocks;
 	int ret;
 
+	/*
+	 * When the offset that is written to is within the first two
+	 * LBAs then the partition table has changed, reparse the partition
+	 * table at close time in this case. A GPT covers more space than
+	 * only the first two LBAs, but a CRC of the remaining pieces is
+	 * written to LBA1, so LBA1 must change as well when the partioning
+	 * is changed.
+	 */
+	if (offset < 2 * SECTOR_SIZE)
+		blk->need_reparse = true;
+
 	if (offset & mask) {
 		size_t now = BLOCKSIZE(blk) - (offset & mask);
 		void *iobuf = block_get(blk, block);
@@ -335,13 +346,28 @@ static ssize_t block_op_write(struct cdev *cdev, const void *buf, size_t count,
 static int block_op_flush(struct cdev *cdev)
 {
 	struct block_device *blk = cdev->priv;
+	int ret;
 
 	blk->discard_start = blk->discard_size = 0;
 
-	return writebuffer_flush(blk);
+	ret = writebuffer_flush(blk);
+
+	return ret;
 }
 
-static int block_op_close(struct cdev *cdev) __alias(block_op_flush);
+static int block_op_close(struct cdev *cdev)
+{
+	struct block_device *blk = cdev->priv;
+
+	block_op_flush(cdev);
+
+	if (blk->need_reparse) {
+		reparse_partition_table(blk);
+		blk->need_reparse = false;
+	}
+
+	return 0;
+}
 
 static int block_op_discard_range(struct cdev *cdev, loff_t count, loff_t offset)
 {
diff --git a/include/block.h b/include/block.h
index 44037bd74c..aaaa97400f 100644
--- a/include/block.h
+++ b/include/block.h
@@ -33,6 +33,8 @@ struct block_device {
 	struct list_head idle_blocks;
 
 	struct cdev cdev;
+
+	bool need_reparse;
 };
 
 extern struct list_head block_device_list;
-- 
2.39.2




  parent reply	other threads:[~2024-02-15  7:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-15  7:47 [PATCH 0/7] Detect partition changes at runtime Sascha Hauer
2024-02-15  7:47 ` [PATCH 1/7] fs: move cdev open count to cdev_open()/cdev_close() Sascha Hauer
2024-02-15  7:47 ` [PATCH 2/7] common: partitions: efi: fix memory leak Sascha Hauer
2024-02-15  7:47 ` [PATCH 3/7] partition: allow to reparse a partition table Sascha Hauer
2024-02-15 11:07   ` Marco Felsch
2024-02-16 11:14     ` Sascha Hauer
2024-02-15  7:47 ` Sascha Hauer [this message]
2024-02-15 12:52   ` [PATCH 4/7] block: reparse partition table when necessary Marco Felsch
2024-02-16 11:16     ` Sascha Hauer
2024-02-15  7:47 ` [PATCH 5/7] fastboot: pass list to fb_addvar() Sascha Hauer
2024-02-15  7:47 ` [PATCH 6/7] fastboot: add function to free a list of fastboot variables Sascha Hauer
2024-02-15  7:47 ` [PATCH 7/7] fastboot: evaluate fastboot partitions when used Sascha Hauer
2024-02-15 12:51   ` Marco Felsch
2024-02-16 11:15     ` Sascha Hauer
2024-02-15  8:31 ` [PATCH 0/7] Detect partition changes at runtime Ahmad Fatoum
2024-02-15 12:54 ` Marco Felsch
2024-02-16 11:13 ` 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=20240215074757.960200-5-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