Create struct bdev_handle that contains all parameters that need to be passed to blkdev_put() and provide blkdev_get_handle_* functions that return this structure instead of plain bdev pointer. This will eventually allow us to pass one more argument to blkdev_put() without too much hassle. CC: Alasdair Kergon <agk@xxxxxxxxxx> CC: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> CC: Anna Schumaker <anna@xxxxxxxxxx> CC: Chao Yu <chao@xxxxxxxxxx> CC: Christian Borntraeger <borntraeger@xxxxxxxxxxxxx> CC: Coly Li <colyli@xxxxxxx CC: "Darrick J. Wong" <djwong@xxxxxxxxxx> CC: Dave Kleikamp <shaggy@xxxxxxxxxx> CC: David Sterba <dsterba@xxxxxxxx> CC: dm-devel@xxxxxxxxxx CC: drbd-dev@xxxxxxxxxxxxxxxx CC: Gao Xiang <xiang@xxxxxxxxxx> CC: Jack Wang <jinpu.wang@xxxxxxxxx> CC: Jaegeuk Kim <jaegeuk@xxxxxxxxxx> CC: jfs-discussion@xxxxxxxxxxxxxxxxxxxxx CC: Joern Engel <joern@xxxxxxxxxxxxxxx> CC: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx> CC: Kent Overstreet <kent.overstreet@xxxxxxxxx> CC: linux-bcache@xxxxxxxxxxxxxxx CC: linux-btrfs@xxxxxxxxxxxxxxx CC: linux-erofs@xxxxxxxxxxxxxxxx CC: <linux-ext4@xxxxxxxxxxxxxxx> CC: linux-f2fs-devel@xxxxxxxxxxxxxxxxxxxxx CC: linux-mm@xxxxxxxxx CC: linux-mtd@xxxxxxxxxxxxxxxxxxx CC: linux-nfs@xxxxxxxxxxxxxxx CC: linux-nilfs@xxxxxxxxxxxxxxx CC: linux-nvme@xxxxxxxxxxxxxxxxxxx CC: linux-pm@xxxxxxxxxxxxxxx CC: linux-raid@xxxxxxxxxxxxxxx CC: linux-s390@xxxxxxxxxxxxxxx CC: linux-scsi@xxxxxxxxxxxxxxx CC: linux-xfs@xxxxxxxxxxxxxxx CC: "Md. Haris Iqbal" <haris.iqbal@xxxxxxxxx> CC: Mike Snitzer <snitzer@xxxxxxxxxx> CC: Minchan Kim <minchan@xxxxxxxxxx> CC: ocfs2-devel@xxxxxxxxxxxxxx CC: reiserfs-devel@xxxxxxxxxxxxxxx CC: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx> CC: Song Liu <song@xxxxxxxxxx> CC: Sven Schnelle <svens@xxxxxxxxxxxxx> CC: target-devel@xxxxxxxxxxxxxxx CC: Ted Tso <tytso@xxxxxxx> CC: Trond Myklebust <trond.myklebust@xxxxxxxxxxxxxxx> CC: xen-devel@xxxxxxxxxxxxxxxxxxxx Signed-off-by: Jan Kara <jack@xxxxxxx> --- block/bdev.c | 47 ++++++++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 10 +++++++++ 2 files changed, 57 insertions(+) diff --git a/block/bdev.c b/block/bdev.c index 979e28a46b98..c75de5cac2bc 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -846,6 +846,24 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, } EXPORT_SYMBOL(blkdev_get_by_dev); +struct bdev_handle *blkdev_get_handle_by_dev(dev_t dev, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops) +{ + struct bdev_handle *handle = kmalloc(sizeof(struct bdev_handle), + GFP_KERNEL); + struct block_device *bdev; + + if (!handle) + return ERR_PTR(-ENOMEM); + bdev = blkdev_get_by_dev(dev, mode, holder, hops); + if (IS_ERR(bdev)) + return ERR_CAST(bdev); + handle->bdev = bdev; + handle->holder = holder; + return handle; +} +EXPORT_SYMBOL(blkdev_get_handle_by_dev); + /** * blkdev_get_by_path - open a block device by name * @path: path to the block device to open @@ -884,6 +902,28 @@ struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, } EXPORT_SYMBOL(blkdev_get_by_path); +struct bdev_handle *blkdev_get_handle_by_path(const char *path, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops) +{ + struct bdev_handle *handle; + dev_t dev; + int error; + + error = lookup_bdev(path, &dev); + if (error) + return ERR_PTR(error); + + handle = blkdev_get_handle_by_dev(dev, mode, holder, hops); + if (!IS_ERR(handle) && (mode & BLK_OPEN_WRITE) && + bdev_read_only(handle->bdev)) { + blkdev_handle_put(handle); + return ERR_PTR(-EACCES); + } + + return handle; +} +EXPORT_SYMBOL(blkdev_get_handle_by_path); + void blkdev_put(struct block_device *bdev, void *holder) { struct gendisk *disk = bdev->bd_disk; @@ -920,6 +960,13 @@ void blkdev_put(struct block_device *bdev, void *holder) } EXPORT_SYMBOL(blkdev_put); +void blkdev_handle_put(struct bdev_handle *handle) +{ + blkdev_put(handle->bdev, handle->holder); + kfree(handle); +} +EXPORT_SYMBOL(blkdev_handle_put); + /** * lookup_bdev() - Look up a struct block_device by name. * @pathname: Name of the block device in the filesystem. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index ed44a997f629..a910e9997ddd 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1471,14 +1471,24 @@ struct blk_holder_ops { #define sb_open_mode(flags) \ (BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) +struct bdev_handle { + struct block_device *bdev; + void *holder; +}; + struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops); struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops); +struct bdev_handle *blkdev_get_handle_by_dev(dev_t dev, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops); +struct bdev_handle *blkdev_get_handle_by_path(const char *path, blk_mode_t mode, + void *holder, const struct blk_holder_ops *hops); int bd_prepare_to_claim(struct block_device *bdev, void *holder, const struct blk_holder_ops *hops); void bd_abort_claiming(struct block_device *bdev, void *holder); void blkdev_put(struct block_device *bdev, void *holder); +void blkdev_handle_put(struct bdev_handle *handle); /* just for blk-cgroup, don't use elsewhere */ struct block_device *blkdev_get_no_open(dev_t dev); -- 2.35.3