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 <a...@redhat.com> CC: Andrew Morton <a...@linux-foundation.org> CC: Anna Schumaker <a...@kernel.org> CC: Chao Yu <c...@kernel.org> CC: Christian Borntraeger <borntrae...@linux.ibm.com> CC: Coly Li <col...@suse.de CC: "Darrick J. Wong" <djw...@kernel.org> CC: Dave Kleikamp <sha...@kernel.org> CC: David Sterba <dste...@suse.com> CC: dm-de...@redhat.com CC: drbd-...@lists.linbit.com CC: Gao Xiang <xi...@kernel.org> CC: Jack Wang <jinpu.w...@ionos.com> CC: Jaegeuk Kim <jaeg...@kernel.org> CC: jfs-discuss...@lists.sourceforge.net CC: Joern Engel <jo...@lazybastard.org> CC: Joseph Qi <joseph...@linux.alibaba.com> CC: Kent Overstreet <kent.overstr...@gmail.com> CC: linux-bca...@vger.kernel.org CC: linux-bt...@vger.kernel.org CC: linux-er...@lists.ozlabs.org CC: <linux-e...@vger.kernel.org> CC: linux-f2fs-de...@lists.sourceforge.net CC: linux...@kvack.org CC: linux-...@lists.infradead.org CC: linux-...@vger.kernel.org CC: linux-ni...@vger.kernel.org CC: linux-n...@lists.infradead.org CC: linux...@vger.kernel.org CC: linux-r...@vger.kernel.org CC: linux-s...@vger.kernel.org CC: linux-s...@vger.kernel.org CC: linux-...@vger.kernel.org CC: "Md. Haris Iqbal" <haris.iq...@ionos.com> CC: Mike Snitzer <snit...@kernel.org> CC: Minchan Kim <minc...@kernel.org> CC: ocfs2-de...@oss.oracle.com CC: reiserfs-de...@vger.kernel.org CC: Sergey Senozhatsky <senozhat...@chromium.org> CC: Song Liu <s...@kernel.org> CC: Sven Schnelle <sv...@linux.ibm.com> CC: target-de...@vger.kernel.org CC: Ted Tso <ty...@mit.edu> CC: Trond Myklebust <trond.mykleb...@hammerspace.com> CC: xen-devel@lists.xenproject.org Signed-off-by: Jan Kara <j...@suse.cz> --- 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