Commit 841dd789 authored by Jan Kara's avatar Jan Kara Committed by Christian Brauner

block: Use bdev_open_by_dev() in blkdev_open()

Convert blkdev_open() to use bdev_open_by_dev(). To be able to propagate
handle from blkdev_open() to blkdev_release() we need to stop using
existence of file->private_data to determine exclusive block device
opens. Use bdev_handle->mode for this purpose since file->f_flags
isn't usable for this (O_EXCL is cleared from the flags during open).
Acked-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarChristian Brauner <brauner@kernel.org>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20230927093442.25915-2-jack@suse.czSigned-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent e719b4d1
...@@ -844,6 +844,9 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, ...@@ -844,6 +844,9 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
} }
handle->bdev = bdev; handle->bdev = bdev;
handle->holder = holder; handle->holder = holder;
if (holder)
mode |= BLK_OPEN_EXCL;
handle->mode = mode;
return handle; return handle;
} }
EXPORT_SYMBOL(bdev_open_by_dev); EXPORT_SYMBOL(bdev_open_by_dev);
......
...@@ -542,15 +542,31 @@ static int blkdev_fsync(struct file *filp, loff_t start, loff_t end, ...@@ -542,15 +542,31 @@ static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
return error; return error;
} }
/**
* file_to_blk_mode - get block open flags from file flags
* @file: file whose open flags should be converted
*
* Look at file open flags and generate corresponding block open flags from
* them. The function works both for file just being open (e.g. during ->open
* callback) and for file that is already open. This is actually non-trivial
* (see comment in the function).
*/
blk_mode_t file_to_blk_mode(struct file *file) blk_mode_t file_to_blk_mode(struct file *file)
{ {
blk_mode_t mode = 0; blk_mode_t mode = 0;
struct bdev_handle *handle = file->private_data;
if (file->f_mode & FMODE_READ) if (file->f_mode & FMODE_READ)
mode |= BLK_OPEN_READ; mode |= BLK_OPEN_READ;
if (file->f_mode & FMODE_WRITE) if (file->f_mode & FMODE_WRITE)
mode |= BLK_OPEN_WRITE; mode |= BLK_OPEN_WRITE;
if (file->private_data) /*
* do_dentry_open() clears O_EXCL from f_flags, use handle->mode to
* determine whether the open was exclusive for already open files.
*/
if (handle)
mode |= handle->mode & BLK_OPEN_EXCL;
else if (file->f_flags & O_EXCL)
mode |= BLK_OPEN_EXCL; mode |= BLK_OPEN_EXCL;
if (file->f_flags & O_NDELAY) if (file->f_flags & O_NDELAY)
mode |= BLK_OPEN_NDELAY; mode |= BLK_OPEN_NDELAY;
...@@ -568,7 +584,8 @@ blk_mode_t file_to_blk_mode(struct file *file) ...@@ -568,7 +584,8 @@ blk_mode_t file_to_blk_mode(struct file *file)
static int blkdev_open(struct inode *inode, struct file *filp) static int blkdev_open(struct inode *inode, struct file *filp)
{ {
struct block_device *bdev; struct bdev_handle *handle;
blk_mode_t mode;
/* /*
* Preserve backwards compatibility and allow large file access * Preserve backwards compatibility and allow large file access
...@@ -579,29 +596,24 @@ static int blkdev_open(struct inode *inode, struct file *filp) ...@@ -579,29 +596,24 @@ static int blkdev_open(struct inode *inode, struct file *filp)
filp->f_flags |= O_LARGEFILE; filp->f_flags |= O_LARGEFILE;
filp->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT; filp->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT;
/* mode = file_to_blk_mode(filp);
* Use the file private data to store the holder for exclusive openes. handle = bdev_open_by_dev(inode->i_rdev, mode,
* file_to_blk_mode relies on it being present to set BLK_OPEN_EXCL. mode & BLK_OPEN_EXCL ? filp : NULL, NULL);
*/ if (IS_ERR(handle))
if (filp->f_flags & O_EXCL) return PTR_ERR(handle);
filp->private_data = filp;
bdev = blkdev_get_by_dev(inode->i_rdev, file_to_blk_mode(filp),
filp->private_data, NULL);
if (IS_ERR(bdev))
return PTR_ERR(bdev);
if (bdev_nowait(bdev)) if (bdev_nowait(handle->bdev))
filp->f_mode |= FMODE_NOWAIT; filp->f_mode |= FMODE_NOWAIT;
filp->f_mapping = bdev->bd_inode->i_mapping; filp->f_mapping = handle->bdev->bd_inode->i_mapping;
filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
filp->private_data = handle;
return 0; return 0;
} }
static int blkdev_release(struct inode *inode, struct file *filp) static int blkdev_release(struct inode *inode, struct file *filp)
{ {
blkdev_put(I_BDEV(filp->f_mapping->host), filp->private_data); bdev_release(filp->private_data);
return 0; return 0;
} }
......
...@@ -1482,6 +1482,7 @@ extern const struct blk_holder_ops fs_holder_ops; ...@@ -1482,6 +1482,7 @@ extern const struct blk_holder_ops fs_holder_ops;
struct bdev_handle { struct bdev_handle {
struct block_device *bdev; struct block_device *bdev;
void *holder; void *holder;
blk_mode_t mode;
}; };
struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment