From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 1/3] partitions: add function to find free space on partition table
Date: Thu, 22 May 2025 15:40:22 +0200 [thread overview]
Message-ID: <20250522-parted-size-v1-1-369d146aa100@pengutronix.de> (raw)
In-Reply-To: <20250522-parted-size-v1-0-369d146aa100@pengutronix.de>
We currently only support creating partitions by specifying the exact
start and end position. Add some functions to find free space in a
partition table so that we can implement creating partitions with only
specifying the size.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/partitions.c | 42 ++++++++++++++++++++++++++++++++++++++++++
include/partitions.h | 3 ++-
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/common/partitions.c b/common/partitions.c
index bc90f51f611223a59a28812b0f9854b342b30ad3..25d5f15721fc5980c927863b2745c23d7149da92 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -163,6 +163,48 @@ int partition_table_write(struct partition_desc *pdesc)
return pdesc->parser->write(pdesc);
}
+bool partition_is_free(struct partition_desc *pdesc, uint64_t start, uint64_t size)
+{
+ struct partition *p;
+
+ if (start < PARTITION_ALIGN_SECTORS)
+ return false;
+
+ if (start + size >= pdesc->blk->num_blocks)
+ return false;
+
+ list_for_each_entry(p, &pdesc->partitions, list) {
+ if (region_overlap_size(p->first_sec, p->size, start, size))
+ return false;
+ }
+
+ return true;
+}
+
+int partition_find_free_space(struct partition_desc *pdesc, uint64_t sectors, uint64_t *start)
+{
+ struct partition *p;
+ uint64_t min_sec = PARTITION_ALIGN_SECTORS;
+
+ min_sec = ALIGN(min_sec, PARTITION_ALIGN_SECTORS);
+
+ if (partition_is_free(pdesc, min_sec, sectors)) {
+ *start = min_sec;
+ return 0;
+ }
+
+ list_for_each_entry(p, &pdesc->partitions, list) {
+ uint64_t s = ALIGN(p->first_sec + p->size, PARTITION_ALIGN_SECTORS);
+
+ if (partition_is_free(pdesc, s, sectors)) {
+ *start = s;
+ return 0;
+ }
+ }
+
+ return -ENOSPC;
+}
+
int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end)
{
diff --git a/include/partitions.h b/include/partitions.h
index 785fb77ab1674d92fdbac5f5df03910b1e2196af..7fd6899bd3b88f691385c330bb7900d9ec1547ac 100644
--- a/include/partitions.h
+++ b/include/partitions.h
@@ -64,6 +64,7 @@ int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end);
int partition_remove(struct partition_desc *pdesc, int num);
void partition_table_free(struct partition_desc *pdesc);
-
+bool partition_is_free(struct partition_desc *pdesc, uint64_t start, uint64_t size);
+int partition_find_free_space(struct partition_desc *pdesc, uint64_t sectors, uint64_t *start);
#endif /* __PARTITIONS_PARSER_H__ */
--
2.39.5
next prev parent reply other threads:[~2025-05-22 13:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-22 13:40 [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
2025-05-22 13:40 ` Sascha Hauer [this message]
2025-05-22 13:40 ` [PATCH 2/3] parted: align partitions to 1MiB Sascha Hauer
2025-05-22 13:40 ` [PATCH 3/3] parted: implement mkpart_size command Sascha Hauer
2025-05-26 13:48 ` [PATCH 0/3] parted: add command to create partitions specifying the size 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=20250522-parted-size-v1-1-369d146aa100@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