[PATCH v2 02/10] cdev: factor out range identical/overlap check
Ahmad Fatoum
a.fatoum at pengutronix.de
Wed Jul 31 01:05:02 PDT 2024
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 at 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
More information about the barebox
mailing list