diff mbox series

[3/9] block: factor out a bd_end_claim helper from blkdev_put

Message ID 20230505175132.2236632-4-hch@lst.de (mailing list archive)
State Superseded
Headers show
Series [1/9] block: consolidate the shutdown logic in blk_mark_disk_dead and del_gendisk | expand

Commit Message

Christoph Hellwig May 5, 2023, 5:51 p.m. UTC
Move all the logic to release an exclusive claim into a helper.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/bdev.c | 63 +++++++++++++++++++++++++++-------------------------
 1 file changed, 33 insertions(+), 30 deletions(-)

Comments

Jan Kara May 7, 2023, 7:08 p.m. UTC | #1
On Fri 05-05-23 13:51:26, Christoph Hellwig wrote:
> Move all the logic to release an exclusive claim into a helper.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Nice. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  block/bdev.c | 63 +++++++++++++++++++++++++++-------------------------
>  1 file changed, 33 insertions(+), 30 deletions(-)
> 
> diff --git a/block/bdev.c b/block/bdev.c
> index 850852fe4b78e1..f2c7181b0bba7d 100644
> --- a/block/bdev.c
> +++ b/block/bdev.c
> @@ -578,6 +578,37 @@ void bd_abort_claiming(struct block_device *bdev, void *holder)
>  }
>  EXPORT_SYMBOL(bd_abort_claiming);
>  
> +static void bd_end_claim(struct block_device *bdev)
> +{
> +	struct block_device *whole = bdev_whole(bdev);
> +	bool unblock = false;
> +
> +	/*
> +	 * Release a claim on the device.  The holder fields are protected with
> +	 * bdev_lock.  open_mutex is used to synchronize disk_holder unlinking.
> +	 */
> +	spin_lock(&bdev_lock);
> +	WARN_ON_ONCE(--bdev->bd_holders < 0);
> +	WARN_ON_ONCE(--whole->bd_holders < 0);
> +	if (!bdev->bd_holders) {
> +		bdev->bd_holder = NULL;
> +		if (bdev->bd_write_holder)
> +			unblock = true;
> +	}
> +	if (!whole->bd_holders)
> +		whole->bd_holder = NULL;
> +	spin_unlock(&bdev_lock);
> +
> +	/*
> +	 * If this was the last claim, remove holder link and unblock evpoll if
> +	 * it was a write holder.
> +	 */
> +	if (unblock) {
> +		disk_unblock_events(bdev->bd_disk);
> +		bdev->bd_write_holder = false;
> +	}
> +}
> +
>  static void blkdev_flush_mapping(struct block_device *bdev)
>  {
>  	WARN_ON_ONCE(bdev->bd_holders);
> @@ -832,36 +863,8 @@ void blkdev_put(struct block_device *bdev, fmode_t mode)
>  		sync_blockdev(bdev);
>  
>  	mutex_lock(&disk->open_mutex);
> -	if (mode & FMODE_EXCL) {
> -		struct block_device *whole = bdev_whole(bdev);
> -		bool bdev_free;
> -
> -		/*
> -		 * Release a claim on the device.  The holder fields
> -		 * are protected with bdev_lock.  open_mutex is to
> -		 * synchronize disk_holder unlinking.
> -		 */
> -		spin_lock(&bdev_lock);
> -
> -		WARN_ON_ONCE(--bdev->bd_holders < 0);
> -		WARN_ON_ONCE(--whole->bd_holders < 0);
> -
> -		if ((bdev_free = !bdev->bd_holders))
> -			bdev->bd_holder = NULL;
> -		if (!whole->bd_holders)
> -			whole->bd_holder = NULL;
> -
> -		spin_unlock(&bdev_lock);
> -
> -		/*
> -		 * If this was the last claim, remove holder link and
> -		 * unblock evpoll if it was a write holder.
> -		 */
> -		if (bdev_free && bdev->bd_write_holder) {
> -			disk_unblock_events(disk);
> -			bdev->bd_write_holder = false;
> -		}
> -	}
> +	if (mode & FMODE_EXCL)
> +		bd_end_claim(bdev);
>  
>  	/*
>  	 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
> -- 
> 2.39.2
>
Christian Brauner May 16, 2023, 4:29 p.m. UTC | #2
On Fri, May 05, 2023 at 01:51:26PM -0400, Christoph Hellwig wrote:
> Move all the logic to release an exclusive claim into a helper.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---

Acked-by: Christian Brauner <brauner@kernel.org>
diff mbox series

Patch

diff --git a/block/bdev.c b/block/bdev.c
index 850852fe4b78e1..f2c7181b0bba7d 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -578,6 +578,37 @@  void bd_abort_claiming(struct block_device *bdev, void *holder)
 }
 EXPORT_SYMBOL(bd_abort_claiming);
 
+static void bd_end_claim(struct block_device *bdev)
+{
+	struct block_device *whole = bdev_whole(bdev);
+	bool unblock = false;
+
+	/*
+	 * Release a claim on the device.  The holder fields are protected with
+	 * bdev_lock.  open_mutex is used to synchronize disk_holder unlinking.
+	 */
+	spin_lock(&bdev_lock);
+	WARN_ON_ONCE(--bdev->bd_holders < 0);
+	WARN_ON_ONCE(--whole->bd_holders < 0);
+	if (!bdev->bd_holders) {
+		bdev->bd_holder = NULL;
+		if (bdev->bd_write_holder)
+			unblock = true;
+	}
+	if (!whole->bd_holders)
+		whole->bd_holder = NULL;
+	spin_unlock(&bdev_lock);
+
+	/*
+	 * If this was the last claim, remove holder link and unblock evpoll if
+	 * it was a write holder.
+	 */
+	if (unblock) {
+		disk_unblock_events(bdev->bd_disk);
+		bdev->bd_write_holder = false;
+	}
+}
+
 static void blkdev_flush_mapping(struct block_device *bdev)
 {
 	WARN_ON_ONCE(bdev->bd_holders);
@@ -832,36 +863,8 @@  void blkdev_put(struct block_device *bdev, fmode_t mode)
 		sync_blockdev(bdev);
 
 	mutex_lock(&disk->open_mutex);
-	if (mode & FMODE_EXCL) {
-		struct block_device *whole = bdev_whole(bdev);
-		bool bdev_free;
-
-		/*
-		 * Release a claim on the device.  The holder fields
-		 * are protected with bdev_lock.  open_mutex is to
-		 * synchronize disk_holder unlinking.
-		 */
-		spin_lock(&bdev_lock);
-
-		WARN_ON_ONCE(--bdev->bd_holders < 0);
-		WARN_ON_ONCE(--whole->bd_holders < 0);
-
-		if ((bdev_free = !bdev->bd_holders))
-			bdev->bd_holder = NULL;
-		if (!whole->bd_holders)
-			whole->bd_holder = NULL;
-
-		spin_unlock(&bdev_lock);
-
-		/*
-		 * If this was the last claim, remove holder link and
-		 * unblock evpoll if it was a write holder.
-		 */
-		if (bdev_free && bdev->bd_write_holder) {
-			disk_unblock_events(disk);
-			bdev->bd_write_holder = false;
-		}
-	}
+	if (mode & FMODE_EXCL)
+		bd_end_claim(bdev);
 
 	/*
 	 * Trigger event checking and tell drivers to flush MEDIA_CHANGE