From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 02/10] cdev: factor out range identical/overlap check
Date: Wed, 31 Jul 2024 10:05:02 +0200 [thread overview]
Message-ID: <20240731080510.364706-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240731080510.364706-1-a.fatoum@pengutronix.de>
region_overlap_size introduced here was introduced before to <common.h>
in commit 60d2fe688e ("introduce region_overlap() function").
This was removed again in commit 04e2aa516e ("common.h: move and
rename lregion_overlap()") and commit 81ca755487 ("common.h: remove
unused region_overlap()") moved lregion_overlap into its the translation
unit of its single call site.
As we now have two users and will add a third, let's create a new
range.h header and move the definition there.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
- new patch (Sascha)
---
common/partitions.c | 16 ++++---------
fs/devfs-core.c | 19 ++-------------
include/range.h | 57 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+), 29 deletions(-)
create mode 100644 include/range.h
diff --git a/common/partitions.c b/common/partitions.c
index d46ed4080597..8e6bf16e73f1 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -16,6 +16,7 @@
#include <filetype.h>
#include <linux/err.h>
#include <partitions.h>
+#include <range.h>
static LIST_HEAD(partition_parser_list);
@@ -164,15 +165,6 @@ int partition_table_write(struct partition_desc *pdesc)
return pdesc->parser->write(pdesc);
}
-static bool overlap(uint64_t s1, uint64_t e1, uint64_t s2, uint64_t e2)
-{
- if (e1 < s2)
- return false;
- if (s1 > e2)
- return false;
- return true;
-}
-
int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end)
{
@@ -192,9 +184,9 @@ int partition_create(struct partition_desc *pdesc, const char *name,
}
list_for_each_entry(part, &pdesc->partitions, list) {
- if (overlap(part->first_sec,
- part->first_sec + part->size - 1,
- lba_start, lba_end)) {
+ if (region_overlap_end(part->first_sec,
+ part->first_sec + part->size - 1,
+ lba_start, lba_end)) {
pr_err("new partition %llu-%llu overlaps with partition %s (%llu-%llu)\n",
lba_start, lba_end, part->name, part->first_sec,
part->first_sec + part->size - 1);
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 2715193c6956..0bb363d0a9ff 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -25,6 +25,7 @@
#include <linux/fs.h>
#include <linux/mtd/mtd.h>
#include <unistd.h>
+#include <range.h>
#include <fs.h>
LIST_HEAD(cdev_list);
@@ -433,22 +434,6 @@ int devfs_remove(struct cdev *cdev)
return 0;
}
-static bool region_identical(loff_t starta, loff_t lena,
- loff_t startb, loff_t lenb)
-{
- return starta == startb && lena == lenb;
-}
-
-static bool region_overlap(loff_t starta, loff_t lena,
- loff_t startb, loff_t lenb)
-{
- if (starta + lena <= startb)
- return 0;
- if (startb + lenb <= starta)
- return 0;
- return 1;
-}
-
/**
* check_overlap() - check overlap with existing partitions
* @cdev: parent cdev
@@ -482,7 +467,7 @@ static struct cdev *check_overlap(struct cdev *cdev, const char *name, loff_t of
goto identical;
}
- if (region_overlap(cpart_offset, cpart->size, offset, size)) {
+ if (region_overlap_size(cpart_offset, cpart->size, offset, size)) {
ret = -EINVAL;
goto conflict;
}
diff --git a/include/range.h b/include/range.h
new file mode 100644
index 000000000000..f72849044d0b
--- /dev/null
+++ b/include/range.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _RANGE_H__
+#define _RANGE_H__
+
+#include <linux/types.h>
+
+/**
+ * region_overlap_end - check whether a pair of [start, end] ranges overlap
+ *
+ * @starta: start of the first range
+ * @enda: end of the first range (inclusive)
+ * @startb: start of the second range
+ * @endb: end of the second range (inclusive)
+ */
+static inline bool region_overlap_end(u64 starta, u64 enda,
+ u64 startb, u64 endb)
+{
+ if (enda < startb)
+ return false;
+ if (endb < starta)
+ return false;
+ return true;
+}
+
+/**
+ * region_overlap_end - check whether a pair of [start, end] ranges overlap
+ *
+ * @starta: start of the first range
+ * @lena: length of the first range
+ * @startb: start of the second range
+ * @lenb: length of the second range
+ */
+static inline bool region_overlap_size(u64 starta, u64 lena,
+ u64 startb, u64 lenb)
+{
+ if (!lena || !lenb)
+ return false;
+
+ return region_overlap_end(starta, starta + lena - 1,
+ startb, startb + lenb - 1);
+}
+
+/**
+ * region_overlap_end - check whether a pair of [start, end] ranges overlap
+ *
+ * @starta: start of the first range
+ * @extenta: end or length of the first range
+ * @startb: start of the first range
+ * @extentb: end or length of the second range
+ */
+static inline bool region_identical(u64 starta, u64 extenta,
+ u64 startb, u64 extentb)
+{
+ return starta == startb && extenta == extentb;
+}
+
+#endif
--
2.39.2
next prev parent reply other threads:[~2024-07-31 8:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-31 8:05 [PATCH v2 00/10] mmc: add SD/eMMC erase support Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 01/10] fs: give erase() a new erase_type parameter Ahmad Fatoum
2024-08-01 10:51 ` Sascha Hauer
2024-08-01 10:54 ` Ahmad Fatoum
2024-07-31 8:05 ` Ahmad Fatoum [this message]
2024-07-31 8:05 ` [PATCH v2 03/10] block: factor out chunk_flush helper Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 04/10] block: allow block devices to implement the cdev erase operation Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 05/10] mci: turn bool members into bitfield in struct mci Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 06/10] mci: describe more command structure in mci.h Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 07/10] mci: core: use CONFIG_MCI_WRITE, not CONFIG_BLOCK_WRITE Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 08/10] mci: add support for discarding write blocks Ahmad Fatoum
2024-08-01 11:27 ` Sascha Hauer
2024-07-31 8:05 ` [PATCH v2 09/10] commands: sync: add new command to flush cached writes Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 10/10] commands: blkstats: add command to print block device statistics Ahmad Fatoum
2024-08-01 10:49 ` [PATCH v2 00/10] mmc: add SD/eMMC erase support 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=20240731080510.364706-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