[RFC,3/4] blkdev: add function to add named read-only partitions
Commit Message
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(+)
Comments
On Thu, Nov 17, 2022 at 12:44:40AM +0000, Daniel Golle wrote:
> 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.
Err, no. No on has any business adding partitions to the block device
except for the partition parser.
On Wed, Nov 16, 2022 at 09:56:37PM -0800, Christoph Hellwig wrote:
> On Thu, Nov 17, 2022 at 12:44:40AM +0000, Daniel Golle wrote:
> > 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.
>
> Err, no. No on has any business adding partitions to the block device
> except for the partition parser.
Well, there is a user-space ioctl for this as well[1], just that won't
set the partition to read-only and also doesn't allow naming it...
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/block/ioctl.c#n40
@@ -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;
@@ -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);