[PATCH 01/32] block: Provide blkdev_get_handle_* functions

Jan Kara jack at suse.cz
Tue Jul 4 05:21:28 PDT 2023


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 at redhat.com>
CC: Andrew Morton <akpm at linux-foundation.org>
CC: Anna Schumaker <anna at kernel.org>
CC: Chao Yu <chao at kernel.org>
CC: Christian Borntraeger <borntraeger at linux.ibm.com>
CC: Coly Li <colyli at suse.de
CC: "Darrick J. Wong" <djwong at kernel.org>
CC: Dave Kleikamp <shaggy at kernel.org>
CC: David Sterba <dsterba at suse.com>
CC: dm-devel at redhat.com
CC: drbd-dev at lists.linbit.com
CC: Gao Xiang <xiang at kernel.org>
CC: Jack Wang <jinpu.wang at ionos.com>
CC: Jaegeuk Kim <jaegeuk at kernel.org>
CC: jfs-discussion at lists.sourceforge.net
CC: Joern Engel <joern at lazybastard.org>
CC: Joseph Qi <joseph.qi at linux.alibaba.com>
CC: Kent Overstreet <kent.overstreet at gmail.com>
CC: linux-bcache at vger.kernel.org
CC: linux-btrfs at vger.kernel.org
CC: linux-erofs at lists.ozlabs.org
CC: <linux-ext4 at vger.kernel.org>
CC: linux-f2fs-devel at lists.sourceforge.net
CC: linux-mm at kvack.org
CC: linux-mtd at lists.infradead.org
CC: linux-nfs at vger.kernel.org
CC: linux-nilfs at vger.kernel.org
CC: linux-nvme at lists.infradead.org
CC: linux-pm at vger.kernel.org
CC: linux-raid at vger.kernel.org
CC: linux-s390 at vger.kernel.org
CC: linux-scsi at vger.kernel.org
CC: linux-xfs at vger.kernel.org
CC: "Md. Haris Iqbal" <haris.iqbal at ionos.com>
CC: Mike Snitzer <snitzer at kernel.org>
CC: Minchan Kim <minchan at kernel.org>
CC: ocfs2-devel at oss.oracle.com
CC: reiserfs-devel at vger.kernel.org
CC: Sergey Senozhatsky <senozhatsky at chromium.org>
CC: Song Liu <song at kernel.org>
CC: Sven Schnelle <svens at linux.ibm.com>
CC: target-devel at vger.kernel.org
CC: Ted Tso <tytso at mit.edu>
CC: Trond Myklebust <trond.myklebust at hammerspace.com>
CC: xen-devel at lists.xenproject.org
Signed-off-by: Jan Kara <jack at 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




More information about the linux-mtd mailing list