linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pranay Kr Srivastava <pranjas@gmail.com>
To: mpa@pengutronix.de, nbd-general@lists.sourceforge.net,
	linux-kernel@vger.kernel.org
Cc: Pranay Kr Srivastava <pranjas@gmail.com>
Subject: [PATCH v5 3/4] make nbd device wait for its users
Date: Sat, 16 Jul 2016 16:06:36 +0530	[thread overview]
Message-ID: <1468665396-12149-1-git-send-email-pranjas@gmail.com> (raw)
In-Reply-To: <CA+aCy1Ey8tvDTO88o-FNGOcRrJxnmTcsZ0=CCrxZas8GcCBe4g@mail.gmail.com>

	When a timeout occurs or a recv fails, then
	instead of abruptly killing nbd block device
	wait for its users to finish.

	This is more required when filesystem(s) like
	ext2 or ext3 don't expect their buffer heads to
	disappear while the filesystem is mounted.

	Each open is now refcounted with the device being released
	for re-use only when there are no "other users".

	A timedout or a disconnected device, if in use, can't
	be used until it has been resetted. The reset happens
	when all tasks having this bdev open closes this bdev.

Behavioral Change:

1)	NBD_DO_IT will not wait for the device to be reset. Hence
	the nbd-client "may exit" while some other process is using
	this device without actually doing the reset on this device
	, hence thus making it unusable until all such user space
	processes have stopped using this device.

2)	There's a window where the nbd-client will not be able to
	change / issue ioctls to the nbd device. This is when there's
	been a disconnect issued or a timeout has occured, however
	there are "other" user processes which currently have this
	nbd device opened.

Signed-off-by: Pranay Kr Srivastava <pranjas@gmail.com>
---
 drivers/block/nbd.c | 37 ++++++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 4919760..fe36280 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -74,6 +74,7 @@ struct nbd_device {
 	 *This is specifically for calling sock_shutdown, for now.
 	 */
 	struct work_struct ws_shutdown;
+	atomic_t users; /* Users that opened the block device */
 };
 
 #if IS_ENABLED(CONFIG_DEBUG_FS)
@@ -699,6 +700,9 @@ static void nbd_dev_dbg_close(struct nbd_device *nbd);
 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		       unsigned int cmd, unsigned long arg)
 {
+	if (nbd->disconnect || nbd->timedout)
+		return -EBUSY;
+
 	switch (cmd) {
 	case NBD_DISCONNECT: {
 		struct request sreq;
@@ -728,7 +732,6 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		nbd_clear_que(nbd);
 		BUG_ON(!list_empty(&nbd->queue_head));
 		BUG_ON(!list_empty(&nbd->waiting_queue));
-		kill_bdev(bdev);
 		return 0;
 
 	case NBD_SET_SOCK: {
@@ -804,16 +807,12 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		mutex_lock(&nbd->tx_lock);
 		nbd->task_recv = NULL;
 		nbd_clear_que(nbd);
-		kill_bdev(bdev);
-		nbd_bdev_reset(bdev);
 
 		if (nbd->disconnect) /* user requested, ignore socket errors */
 			error = 0;
 		if (nbd->timedout)
 			error = -ETIMEDOUT;
 
-		nbd_reset(nbd);
-
 		return error;
 	}
 
@@ -852,10 +851,38 @@ static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
 	return error;
 }
 
+static int nbd_open(struct block_device *bdev, fmode_t mode)
+{
+	struct nbd_device *nbd = bdev->bd_disk->private_data;
+
+	atomic_inc(&nbd->users);
+
+	return 0;
+}
+
+static void nbd_release(struct gendisk *disk, fmode_t mode)
+{
+	struct nbd_device *nbd = disk->private_data;
+	struct block_device *bdev = bdget (part_devt(
+						dev_to_part(nbd_to_dev(nbd))));
+
+	WARN_ON(!bdev);
+	if (atomic_dec_and_test(&nbd->users)) {
+		if (bdev) {
+			nbd_bdev_reset(bdev);
+			kill_bdev(bdev);
+			bdput(bdev);
+		}
+		nbd_reset(nbd);
+	}
+}
+
 static const struct block_device_operations nbd_fops = {
 	.owner =	THIS_MODULE,
 	.ioctl =	nbd_ioctl,
 	.compat_ioctl =	nbd_ioctl,
+	.open =         nbd_open,
+	.release =      nbd_release,
 };
 
 static void nbd_ws_func_shutdown(struct work_struct *ws_nbd)
-- 
2.6.2

  reply	other threads:[~2016-07-16 10:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-30 11:02 [PATCH v4 0/4] nbd: nbd fixes Pranay Kr. Srivastava
2016-06-30 11:02 ` [PATCH v4 1/5]nbd: cleanup nbd_set_socket Pranay Kr. Srivastava
2016-07-07 14:56   ` Pranay Srivastava
2016-07-09  7:36     ` Pranay Srivastava
2016-06-30 11:02 ` [PATCH v4 2/5]nbd: fix might_sleep warning on socket shutdown Pranay Kr. Srivastava
2016-07-04  7:06   ` Pranay Srivastava
2016-07-10 12:25   ` Markus Pargmann
     [not found]     ` <CA+aCy1GZo6Vk9Yy1KXWgyVhcGmVETyuPuhQT=pSVDVxi5qr8ww@mail.gmail.com>
2016-07-13  7:13       ` Markus Pargmann
2016-07-14  5:59         ` Pranay Srivastava
2016-07-16  9:22           ` Pranay Kr Srivastava
2016-07-16  9:22             ` [PATCH v5 2/4] nbd: fix might_sleep warning on socket shutdown Pranay Kr Srivastava
2016-07-16 10:14               ` Pranay Srivastava
2016-06-30 11:02 ` [PATCH v4 3/5]nbd: make nbd device wait for its users Pranay Kr. Srivastava
2016-07-10 13:02   ` Markus Pargmann
2016-07-10 16:02     ` Pranay Srivastava
2016-07-13  7:54       ` Markus Pargmann
2016-07-14  5:47         ` Pranay Srivastava
2016-07-16 10:36           ` Pranay Kr Srivastava [this message]
2016-07-16 10:42             ` [PATCH v5 3/4] " Pranay Srivastava
2016-07-20  7:47             ` Markus Pargmann
2016-06-30 11:02 ` [PATCH v4 4/5]nbd: use i_size_write to assign nbd device size Pranay Kr. Srivastava

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468665396-12149-1-git-send-email-pranjas@gmail.com \
    --to=pranjas@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpa@pengutronix.de \
    --cc=nbd-general@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).