diff mbox series

[02/29] block: Use bdev_open_by_dev() in blkdev_open()

Message ID 20230823104857.11437-2-jack@suse.cz (mailing list archive)
State New, archived
Headers show
Series block: Make blkdev_get_by_*() return handle | expand

Commit Message

Jan Kara Aug. 23, 2023, 10:48 a.m. UTC
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: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jan Kara <jack@suse.cz>
---
 block/bdev.c           |  3 +++
 block/blk.h            |  1 -
 block/fops.c           | 34 ++++++++++++++++------------------
 block/ioctl.c          |  6 ++++--
 include/linux/blkdev.h |  1 +
 5 files changed, 24 insertions(+), 21 deletions(-)

Comments

Al Viro Aug. 25, 2023, 2:28 a.m. UTC | #1
On Wed, Aug 23, 2023 at 12:48:13PM +0200, Jan Kara wrote:
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 648670ddb164..54c1e2f71031 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -582,7 +582,8 @@ long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
>  {
>  	struct block_device *bdev = I_BDEV(file->f_mapping->host);
>  	void __user *argp = (void __user *)arg;
> -	blk_mode_t mode = file_to_blk_mode(file);
> +	struct bdev_handle *bdev_handle = file->private_data;
> +	blk_mode_t mode = bdev_handle->mode;
>  	int ret;
>  
>  	switch (cmd) {

	Still the same bug as in v2 - you are missing the effects of
fcntl(2) setting/clearing O_NDELAY and sd_ioctl() is sensitive to that.
Jan Kara Aug. 25, 2023, 9:45 a.m. UTC | #2
On Fri 25-08-23 03:28:26, Al Viro wrote:
> On Wed, Aug 23, 2023 at 12:48:13PM +0200, Jan Kara wrote:
> > diff --git a/block/ioctl.c b/block/ioctl.c
> > index 648670ddb164..54c1e2f71031 100644
> > --- a/block/ioctl.c
> > +++ b/block/ioctl.c
> > @@ -582,7 +582,8 @@ long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
> >  {
> >  	struct block_device *bdev = I_BDEV(file->f_mapping->host);
> >  	void __user *argp = (void __user *)arg;
> > -	blk_mode_t mode = file_to_blk_mode(file);
> > +	struct bdev_handle *bdev_handle = file->private_data;
> > +	blk_mode_t mode = bdev_handle->mode;
> >  	int ret;
> >  
> >  	switch (cmd) {
> 
> 	Still the same bug as in v2 - you are missing the effects of
> fcntl(2) setting/clearing O_NDELAY and sd_ioctl() is sensitive to that.

Argh, indeed you are correct. I forgot about fcntl(2) modifying
file->f_flags. Attached is a version of the patch that I'm currently
testing.

								Honza
Christian Brauner Aug. 25, 2023, 1:29 p.m. UTC | #3
> file->f_flags. Attached is a version of the patch that I'm currently
> testing.

Appended patch looks good to me,
Reviewed-by: Christian Brauner <brauner@kernel.org>

The patch also has another fix for O_EXCL. In earlier versions of this
patch series it relied of f_flags. Thanks for that comment you added in
there about this now. This really helps given that O_EXCL has special
meaning for block devices. Ideally we'd have kernel doc for
file_to_blk_mode().
Jan Kara Aug. 28, 2023, 4:46 p.m. UTC | #4
On Fri 25-08-23 15:29:11, Christian Brauner wrote:
> > file->f_flags. Attached is a version of the patch that I'm currently
> > testing.
> 
> Appended patch looks good to me,
> Reviewed-by: Christian Brauner <brauner@kernel.org>
> 
> The patch also has another fix for O_EXCL. In earlier versions of this
> patch series it relied of f_flags. Thanks for that comment you added in
> there about this now. This really helps given that O_EXCL has special
> meaning for block devices. Ideally we'd have kernel doc for
> file_to_blk_mode().

Thanks for review! I've added the kerneldoc comment:

/**
 * 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).
 */

								Honza
Christian Brauner Aug. 29, 2023, 11:03 a.m. UTC | #5
On Mon, Aug 28, 2023 at 06:46:13PM +0200, Jan Kara wrote:
> On Fri 25-08-23 15:29:11, Christian Brauner wrote:
> > > file->f_flags. Attached is a version of the patch that I'm currently
> > > testing.
> > 
> > Appended patch looks good to me,
> > Reviewed-by: Christian Brauner <brauner@kernel.org>
> > 
> > The patch also has another fix for O_EXCL. In earlier versions of this
> > patch series it relied of f_flags. Thanks for that comment you added in
> > there about this now. This really helps given that O_EXCL has special
> > meaning for block devices. Ideally we'd have kernel doc for
> > file_to_blk_mode().
> 
> Thanks for review! I've added the kerneldoc comment:
> 
> /**
>  * 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).
>  */

Perfect, thanks!
diff mbox series

Patch

diff --git a/block/bdev.c b/block/bdev.c
index fce150f9e66d..f1de1e65517b 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -844,6 +844,9 @@  struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
 	}
 	handle->bdev = bdev;
 	handle->holder = holder;
+	if (holder)
+		mode |= BLK_OPEN_EXCL;
+	handle->mode = mode;
 	return handle;
 }
 EXPORT_SYMBOL(bdev_open_by_dev);
diff --git a/block/blk.h b/block/blk.h
index 608c5dcc516b..43b80dc78918 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -464,7 +464,6 @@  extern struct device_attribute dev_attr_events_poll_msecs;
 
 extern struct attribute_group blk_trace_attr_group;
 
-blk_mode_t file_to_blk_mode(struct file *file);
 int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
 		loff_t lstart, loff_t lend);
 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
diff --git a/block/fops.c b/block/fops.c
index a286bf3325c5..7261c27c7901 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -470,7 +470,7 @@  static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
 	return error;
 }
 
-blk_mode_t file_to_blk_mode(struct file *file)
+static blk_mode_t file_to_blk_mode(struct file *file)
 {
 	blk_mode_t mode = 0;
 
@@ -478,7 +478,7 @@  blk_mode_t file_to_blk_mode(struct file *file)
 		mode |= BLK_OPEN_READ;
 	if (file->f_mode & FMODE_WRITE)
 		mode |= BLK_OPEN_WRITE;
-	if (file->private_data)
+	if (file->f_flags & O_EXCL)
 		mode |= BLK_OPEN_EXCL;
 	if (file->f_flags & O_NDELAY)
 		mode |= BLK_OPEN_NDELAY;
@@ -496,7 +496,8 @@  blk_mode_t file_to_blk_mode(struct file *file)
 
 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
@@ -507,29 +508,24 @@  static int blkdev_open(struct inode *inode, struct file *filp)
 	filp->f_flags |= O_LARGEFILE;
 	filp->f_mode |= FMODE_BUF_RASYNC;
 
-	/*
-	 * Use the file private data to store the holder for exclusive openes.
-	 * file_to_blk_mode relies on it being present to set BLK_OPEN_EXCL.
-	 */
-	if (filp->f_flags & O_EXCL)
-		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);
+	mode = file_to_blk_mode(filp);
+	handle = bdev_open_by_dev(inode->i_rdev, mode,
+			mode & BLK_OPEN_EXCL ? filp : NULL, NULL);
+	if (IS_ERR(handle))
+		return PTR_ERR(handle);
 
-	if (bdev_nowait(bdev))
+	if (bdev_nowait(handle->bdev))
 		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->private_data = handle;
 	return 0;
 }
 
 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;
 }
 
@@ -630,6 +626,8 @@  static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 {
 	struct inode *inode = bdev_file_inode(file);
 	struct block_device *bdev = I_BDEV(inode);
+	struct bdev_handle *bdev_handle = file->private_data;
+	blk_mode_t open_mode = bdev_handle->mode;
 	loff_t end = start + len - 1;
 	loff_t isize;
 	int error;
@@ -659,7 +657,7 @@  static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 	filemap_invalidate_lock(inode->i_mapping);
 
 	/* Invalidate the page cache, including dirty pages. */
-	error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end);
+	error = truncate_bdev_range(bdev, open_mode, start, end);
 	if (error)
 		goto fail;
 
diff --git a/block/ioctl.c b/block/ioctl.c
index 648670ddb164..54c1e2f71031 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -582,7 +582,8 @@  long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 {
 	struct block_device *bdev = I_BDEV(file->f_mapping->host);
 	void __user *argp = (void __user *)arg;
-	blk_mode_t mode = file_to_blk_mode(file);
+	struct bdev_handle *bdev_handle = file->private_data;
+	blk_mode_t mode = bdev_handle->mode;
 	int ret;
 
 	switch (cmd) {
@@ -643,7 +644,8 @@  long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 	void __user *argp = compat_ptr(arg);
 	struct block_device *bdev = I_BDEV(file->f_mapping->host);
 	struct gendisk *disk = bdev->bd_disk;
-	blk_mode_t mode = file_to_blk_mode(file);
+	struct bdev_handle *bdev_handle = file->private_data;
+	blk_mode_t mode = bdev_handle->mode;
 
 	switch (cmd) {
 	/* These need separate implementations for the data structure */
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 6a942ec773b6..ae741dec184b 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1481,6 +1481,7 @@  extern const struct blk_holder_ops fs_holder_ops;
 struct bdev_handle {
 	struct block_device *bdev;
 	void *holder;
+	blk_mode_t mode;
 };
 
 struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,