Skip to content

Commit dd0bd42

Browse files
jankaraSasha Levin
authored and
Sasha Levin
committed
block: Provide bdev_open_* functions
[ Upstream commit e719b4d ] Create struct bdev_handle that contains all parameters that need to be passed to blkdev_put() and provide bdev_open_* functions that return this structure instead of plain bdev pointer. This will eventually allow us to pass one more argument to blkdev_put() (renamed to bdev_release()) without too much hassle. Acked-by: Christoph Hellwig <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]> Stable-dep-of: 0f28be6 ("erofs: fix lockdep false positives on initializing erofs_pseudo_mnt") Signed-off-by: Sasha Levin <[email protected]>
1 parent 3721c63 commit dd0bd42

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

block/bdev.c

+48
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,25 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
831831
}
832832
EXPORT_SYMBOL(blkdev_get_by_dev);
833833

834+
struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
835+
const struct blk_holder_ops *hops)
836+
{
837+
struct bdev_handle *handle = kmalloc(sizeof(*handle), GFP_KERNEL);
838+
struct block_device *bdev;
839+
840+
if (!handle)
841+
return ERR_PTR(-ENOMEM);
842+
bdev = blkdev_get_by_dev(dev, mode, holder, hops);
843+
if (IS_ERR(bdev)) {
844+
kfree(handle);
845+
return ERR_CAST(bdev);
846+
}
847+
handle->bdev = bdev;
848+
handle->holder = holder;
849+
return handle;
850+
}
851+
EXPORT_SYMBOL(bdev_open_by_dev);
852+
834853
/**
835854
* blkdev_get_by_path - open a block device by name
836855
* @path: path to the block device to open
@@ -869,6 +888,28 @@ struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
869888
}
870889
EXPORT_SYMBOL(blkdev_get_by_path);
871890

891+
struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,
892+
void *holder, const struct blk_holder_ops *hops)
893+
{
894+
struct bdev_handle *handle;
895+
dev_t dev;
896+
int error;
897+
898+
error = lookup_bdev(path, &dev);
899+
if (error)
900+
return ERR_PTR(error);
901+
902+
handle = bdev_open_by_dev(dev, mode, holder, hops);
903+
if (!IS_ERR(handle) && (mode & BLK_OPEN_WRITE) &&
904+
bdev_read_only(handle->bdev)) {
905+
bdev_release(handle);
906+
return ERR_PTR(-EACCES);
907+
}
908+
909+
return handle;
910+
}
911+
EXPORT_SYMBOL(bdev_open_by_path);
912+
872913
void blkdev_put(struct block_device *bdev, void *holder)
873914
{
874915
struct gendisk *disk = bdev->bd_disk;
@@ -905,6 +946,13 @@ void blkdev_put(struct block_device *bdev, void *holder)
905946
}
906947
EXPORT_SYMBOL(blkdev_put);
907948

949+
void bdev_release(struct bdev_handle *handle)
950+
{
951+
blkdev_put(handle->bdev, handle->holder);
952+
kfree(handle);
953+
}
954+
EXPORT_SYMBOL(bdev_release);
955+
908956
/**
909957
* lookup_bdev() - Look up a struct block_device by name.
910958
* @pathname: Name of the block device in the filesystem.

include/linux/blkdev.h

+10
Original file line numberDiff line numberDiff line change
@@ -1479,14 +1479,24 @@ extern const struct blk_holder_ops fs_holder_ops;
14791479
#define sb_open_mode(flags) \
14801480
(BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
14811481

1482+
struct bdev_handle {
1483+
struct block_device *bdev;
1484+
void *holder;
1485+
};
1486+
14821487
struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
14831488
const struct blk_holder_ops *hops);
14841489
struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
14851490
void *holder, const struct blk_holder_ops *hops);
1491+
struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1492+
const struct blk_holder_ops *hops);
1493+
struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,
1494+
void *holder, const struct blk_holder_ops *hops);
14861495
int bd_prepare_to_claim(struct block_device *bdev, void *holder,
14871496
const struct blk_holder_ops *hops);
14881497
void bd_abort_claiming(struct block_device *bdev, void *holder);
14891498
void blkdev_put(struct block_device *bdev, void *holder);
1499+
void bdev_release(struct bdev_handle *handle);
14901500

14911501
/* just for blk-cgroup, don't use elsewhere */
14921502
struct block_device *blkdev_get_no_open(dev_t dev);

0 commit comments

Comments
 (0)