[PATCH RFC v2 01/14] block: factor out a helper bdev_file_alloc()
From: Yu Kuai
Date: Fri Mar 28 2025 - 02:15:11 EST
From: Yu Kuai <yukuai3@xxxxxxxxxx>
To allocate bdev_file without opening the bdev, mdraid will create hidden
disk to manage internal bitmap in following patches, while the hidden disk
can't be opened by user, and mdraid will initialize this file to manage
bitmap IO.
Signed-off-by: Yu Kuai <yukuai3@xxxxxxxxxx>
---
block/bdev.c | 21 ++++++++++++++++-----
include/linux/blkdev.h | 1 +
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index 9d73a8fbf7f9..6b4ba6cb04c9 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -989,12 +989,26 @@ static unsigned blk_to_file_flags(blk_mode_t mode)
return flags;
}
+struct file *bdev_file_alloc(struct block_device *bdev, blk_mode_t mode)
+{
+ unsigned int flags = blk_to_file_flags(mode);
+ struct file *bdev_file;
+
+ bdev_file = alloc_file_pseudo_noaccount(BD_INODE(bdev),
+ blockdev_mnt, "", flags | O_LARGEFILE, &def_blk_fops);
+
+ if (!IS_ERR(bdev_file))
+ ihold(BD_INODE(bdev));
+
+ return bdev_file;
+}
+EXPORT_SYMBOL_GPL(bdev_file_alloc);
+
struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
const struct blk_holder_ops *hops)
{
struct file *bdev_file;
struct block_device *bdev;
- unsigned int flags;
int ret;
ret = bdev_permission(dev, mode, holder);
@@ -1005,14 +1019,11 @@ struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
if (!bdev)
return ERR_PTR(-ENXIO);
- flags = blk_to_file_flags(mode);
- bdev_file = alloc_file_pseudo_noaccount(BD_INODE(bdev),
- blockdev_mnt, "", flags | O_LARGEFILE, &def_blk_fops);
+ bdev_file = bdev_file_alloc(bdev, mode);
if (IS_ERR(bdev_file)) {
blkdev_put_no_open(bdev);
return bdev_file;
}
- ihold(BD_INODE(bdev));
ret = bdev_open(bdev, mode, holder, hops, bdev_file);
if (ret) {
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 248416ecd01c..dede6721374a 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1639,6 +1639,7 @@ extern const struct blk_holder_ops fs_holder_ops;
(BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \
(((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
+struct file *bdev_file_alloc(struct block_device *bdev, blk_mode_t mode);
struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
const struct blk_holder_ops *hops);
struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
--
2.39.2