linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2] lightnvm: prevent bd removal if busy
@ 2017-09-10 19:07 Rakesh Pandit
  2017-09-12 13:22 ` Javier González
  0 siblings, 1 reply; 3+ messages in thread
From: Rakesh Pandit @ 2017-09-10 19:07 UTC (permalink / raw)
  To: Matias Bjørling, linux-block, linux-kernel; +Cc: Javier González

When a virtual block device is formatted and mounted after creating
with "nvme lnvm create... -t pblk", a removal from "nvm lnvm remove"
would result in this:

446416.309757] bdi-block not registered
[446416.309773] ------------[ cut here ]------------
[446416.309780] WARNING: CPU: 3 PID: 4319 at fs/fs-writeback.c:2159 __mark_inode_dirty+0x268/0x340

Ideally removal should return -EBUSY as block device is mounted after
formatting.  This patch tries to address this checking if whole device
or any partition of it already mounted or not before removal.

Whole device is checked using "bd_super" member of block device.  This
member is always set once block device has been mounted using a
filesystem.  Another member "bd_part_count" takes care of checking any
if any partitions are under use.  "bd_part_count" is only updated
under locks when partitions are opened or closed (first open and last
release).  This at least does take care sending -EBUSY if removal is
being attempted while whole block device or any partition is mounted.

Signed-off-by: Rakesh Pandit <rakesh@tuxera.com>
---

V2: Take a different approach. Instead of checking bd_openers use
bd_super and bd_part_count.  This should address the removal of bdevs
which are mounted from removal.

 drivers/lightnvm/core.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
index c39f87d..9f9a137 100644
--- a/drivers/lightnvm/core.c
+++ b/drivers/lightnvm/core.c
@@ -373,6 +373,7 @@ static void __nvm_remove_target(struct nvm_target *t)
 static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
 {
 	struct nvm_target *t;
+	struct block_device *bdev;
 
 	mutex_lock(&dev->mlock);
 	t = nvm_find_target(dev, remove->tgtname);
@@ -380,6 +381,19 @@ static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
 		mutex_unlock(&dev->mlock);
 		return 1;
 	}
+	bdev = bdget_disk(t->disk, 0);
+	if (!bdev) {
+		pr_err("nvm: removal failed, allocating bd failed\n");
+		mutex_unlock(&dev->mlock);
+		return -ENOMEM;
+	}
+	if (bdev->bd_super || bdev->bd_part_count) {
+		pr_err("nvm: removal failed, block device busy\n");
+		bdput(bdev);
+		mutex_unlock(&dev->mlock);
+		return -EBUSY;
+	}
+	bdput(bdev);
 	__nvm_remove_target(t);
 	mutex_unlock(&dev->mlock);
 
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] lightnvm: prevent bd removal if busy
  2017-09-10 19:07 [PATCH V2] lightnvm: prevent bd removal if busy Rakesh Pandit
@ 2017-09-12 13:22 ` Javier González
  2017-09-21 11:13   ` Matias Bjørling
  0 siblings, 1 reply; 3+ messages in thread
From: Javier González @ 2017-09-12 13:22 UTC (permalink / raw)
  To: Rakesh Pandit; +Cc: Matias Bjørling, linux-block, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2662 bytes --]

> On 10 Sep 2017, at 21.07, Rakesh Pandit <rakesh@tuxera.com> wrote:
> 
> When a virtual block device is formatted and mounted after creating
> with "nvme lnvm create... -t pblk", a removal from "nvm lnvm remove"
> would result in this:
> 
> 446416.309757] bdi-block not registered
> [446416.309773] ------------[ cut here ]------------
> [446416.309780] WARNING: CPU: 3 PID: 4319 at fs/fs-writeback.c:2159 __mark_inode_dirty+0x268/0x340
> 
> Ideally removal should return -EBUSY as block device is mounted after
> formatting.  This patch tries to address this checking if whole device
> or any partition of it already mounted or not before removal.
> 
> Whole device is checked using "bd_super" member of block device.  This
> member is always set once block device has been mounted using a
> filesystem.  Another member "bd_part_count" takes care of checking any
> if any partitions are under use.  "bd_part_count" is only updated
> under locks when partitions are opened or closed (first open and last
> release).  This at least does take care sending -EBUSY if removal is
> being attempted while whole block device or any partition is mounted.
> 
> Signed-off-by: Rakesh Pandit <rakesh@tuxera.com>
> ---
> 
> V2: Take a different approach. Instead of checking bd_openers use
> bd_super and bd_part_count.  This should address the removal of bdevs
> which are mounted from removal.
> 
> drivers/lightnvm/core.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
> 
> diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
> index c39f87d..9f9a137 100644
> --- a/drivers/lightnvm/core.c
> +++ b/drivers/lightnvm/core.c
> @@ -373,6 +373,7 @@ static void __nvm_remove_target(struct nvm_target *t)
> static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
> {
> 	struct nvm_target *t;
> +	struct block_device *bdev;
> 
> 	mutex_lock(&dev->mlock);
> 	t = nvm_find_target(dev, remove->tgtname);
> @@ -380,6 +381,19 @@ static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
> 		mutex_unlock(&dev->mlock);
> 		return 1;
> 	}
> +	bdev = bdget_disk(t->disk, 0);
> +	if (!bdev) {
> +		pr_err("nvm: removal failed, allocating bd failed\n");
> +		mutex_unlock(&dev->mlock);
> +		return -ENOMEM;
> +	}
> +	if (bdev->bd_super || bdev->bd_part_count) {
> +		pr_err("nvm: removal failed, block device busy\n");
> +		bdput(bdev);
> +		mutex_unlock(&dev->mlock);
> +		return -EBUSY;
> +	}
> +	bdput(bdev);
> 	__nvm_remove_target(t);
> 	mutex_unlock(&dev->mlock);
> 
> --
> 2.7.4

Looks good.

Reviewed-by: Javier González <javier@cnexlabs.com>


[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] lightnvm: prevent bd removal if busy
  2017-09-12 13:22 ` Javier González
@ 2017-09-21 11:13   ` Matias Bjørling
  0 siblings, 0 replies; 3+ messages in thread
From: Matias Bjørling @ 2017-09-21 11:13 UTC (permalink / raw)
  To: Javier González, Rakesh Pandit; +Cc: linux-block, linux-kernel

On 09/12/2017 03:22 PM, Javier González wrote:
>> On 10 Sep 2017, at 21.07, Rakesh Pandit <rakesh@tuxera.com> wrote:
>>
>> When a virtual block device is formatted and mounted after creating
>> with "nvme lnvm create... -t pblk", a removal from "nvm lnvm remove"
>> would result in this:
>>
>> 446416.309757] bdi-block not registered
>> [446416.309773] ------------[ cut here ]------------
>> [446416.309780] WARNING: CPU: 3 PID: 4319 at fs/fs-writeback.c:2159 __mark_inode_dirty+0x268/0x340
>>
>> Ideally removal should return -EBUSY as block device is mounted after
>> formatting.  This patch tries to address this checking if whole device
>> or any partition of it already mounted or not before removal.
>>
>> Whole device is checked using "bd_super" member of block device.  This
>> member is always set once block device has been mounted using a
>> filesystem.  Another member "bd_part_count" takes care of checking any
>> if any partitions are under use.  "bd_part_count" is only updated
>> under locks when partitions are opened or closed (first open and last
>> release).  This at least does take care sending -EBUSY if removal is
>> being attempted while whole block device or any partition is mounted.
>>
>> Signed-off-by: Rakesh Pandit <rakesh@tuxera.com>
>> ---
>>
>> V2: Take a different approach. Instead of checking bd_openers use
>> bd_super and bd_part_count.  This should address the removal of bdevs
>> which are mounted from removal.
>>
>> drivers/lightnvm/core.c | 14 ++++++++++++++
>> 1 file changed, 14 insertions(+)
>>
>> diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
>> index c39f87d..9f9a137 100644
>> --- a/drivers/lightnvm/core.c
>> +++ b/drivers/lightnvm/core.c
>> @@ -373,6 +373,7 @@ static void __nvm_remove_target(struct nvm_target *t)
>> static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
>> {
>> 	struct nvm_target *t;
>> +	struct block_device *bdev;
>>
>> 	mutex_lock(&dev->mlock);
>> 	t = nvm_find_target(dev, remove->tgtname);
>> @@ -380,6 +381,19 @@ static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
>> 		mutex_unlock(&dev->mlock);
>> 		return 1;
>> 	}
>> +	bdev = bdget_disk(t->disk, 0);
>> +	if (!bdev) {
>> +		pr_err("nvm: removal failed, allocating bd failed\n");
>> +		mutex_unlock(&dev->mlock);
>> +		return -ENOMEM;
>> +	}
>> +	if (bdev->bd_super || bdev->bd_part_count) {
>> +		pr_err("nvm: removal failed, block device busy\n");
>> +		bdput(bdev);
>> +		mutex_unlock(&dev->mlock);
>> +		return -EBUSY;
>> +	}
>> +	bdput(bdev);
>> 	__nvm_remove_target(t);
>> 	mutex_unlock(&dev->mlock);
>>
>> --
>> 2.7.4
> 
> Looks good.
> 
> Reviewed-by: Javier González <javier@cnexlabs.com>
> 

Thanks Rakesh. I pulled it in for 4.15.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-09-21 11:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-10 19:07 [PATCH V2] lightnvm: prevent bd removal if busy Rakesh Pandit
2017-09-12 13:22 ` Javier González
2017-09-21 11:13   ` Matias Bjørling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).