[RFC PATCH 3/4] blkdev: add function to add named read-only partitions

Daniel Golle
2022-11-17T00:45:57Z

Add function bdev_add_partition_ro() which can be used by drivers to
register named read-only partitions on a disk device.
Unlike the existing bdev_add_partition() function, there is also no
check for overlapping partitions.
This new function is going to be used by the uImage.FIT parser.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
block/partitions/core.c | 34 ++++++++++++++++++++++++++++++++++
include/linux/blkdev.h | 7 +++++++
2 files changed, 41 insertions(+)

diff --git a/block/partitions/core.c b/block/partitions/core.c
index 355646b0707d..060a6585a387 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -469,6 +469,40 @@ int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
return ret;
}

+int bdev_add_partition_ro(struct gendisk *disk, int partno, sector_t start,
+ sector_t length, const char *volname)
+{
+ struct block_device *part;
+ struct partition_meta_info *info;
+ int ret;
+
+ mutex_lock(&disk->open_mutex);
+ if (!disk_live(disk)) {
+ ret = -ENXIO;
+ goto out;
+ }
+
+ part = add_partition(disk, partno, start, length,
+ ADDPART_FLAG_READONLY, NULL);
+ ret = PTR_ERR_OR_ZERO(part);
+ if (ret)
+ goto out;
+
+ if (volname) {
+ info = kzalloc(sizeof(struct partition_meta_info), GFP_KERNEL);
+ if (!info) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ strscpy(info->volname, volname, sizeof(info->volname));
+ part->bd_meta_info = info;
+ }
+out:
+ mutex_unlock(&disk->open_mutex);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_add_partition_ro);
+
int bdev_del_partition(struct gendisk *disk, int partno)
{
struct block_device *part = NULL;
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index b45cdcdccc6d..6e468a2fc4ec 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1504,6 +1504,8 @@ int sync_blockdev_nowait(struct block_device *bdev);
void sync_bdevs(bool wait);
void bdev_statx_dioalign(struct inode *inode, struct kstat *stat);
void printk_all_partitions(void);
+int bdev_add_partition_ro(struct gendisk *disk, int partno, sector_t start,
+ sector_t length, const char *volname);
#else
static inline dev_t devt_from_partuuid(const char *uuid_str, int *root_wait)
{
@@ -1537,6 +1539,11 @@ static inline void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
static inline void printk_all_partitions(void)
{
}
+static inline int bdev_add_partition_ro(struct gendisk *disk, int partno, sector_t start,
+ sector_t length, const char *volname)
+{
+ return 0;
+}
#endif /* CONFIG_BLOCK */

int fsync_bdev(struct block_device *bdev);
--
2.38.1