mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Markus Pargmann <mpa@pengutronix.de>
To: Paul Clements <Paul.Clements@steeleye.com>
Cc: nbd-general@lists.sourceforge.net, linux-kernel@vger.kernel.org,
	kernel@pengutronix.de, Markus Pargmann <mpa@pengutronix.de>
Subject: [RFC 2/4] nbd: Split 'DO_IT' into three functions
Date: Tue, 13 Jan 2015 14:44:23 +0100	[thread overview]
Message-ID: <1421156665-27318-3-git-send-email-mpa@pengutronix.de> (raw)
In-Reply-To: <1421156665-27318-1-git-send-email-mpa@pengutronix.de>

The DO_IT ioctl does a lot of things to handle the connection. This
patch splits the DO_IT ioctl routine into three functions
nbd_connection_start()/run()/stop(). This makes it easier to read and
allows other functions to call the same code. This patch prepares the
DO_IT code for nbd-root device.

Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
---
 drivers/block/nbd.c | 146 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 91 insertions(+), 55 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 6bd7df2ff648..0ff6ebbec252 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -596,6 +596,95 @@ static void do_nbd_request(struct request_queue *q)
 	}
 }
 
+static int nbd_connection_start(struct block_device *bdev,
+				struct nbd_device *nbd)
+{
+	if (nbd->flags & NBD_FLAG_READ_ONLY)
+		set_device_ro(bdev, true);
+	if (nbd->flags & NBD_FLAG_SEND_TRIM)
+		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
+	if (nbd->flags & NBD_FLAG_SEND_FLUSH)
+		blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
+	else
+		blk_queue_flush(nbd->disk->queue, 0);
+	return 0;
+}
+
+static int nbd_connection_run(struct nbd_device *nbd)
+{
+	struct task_struct *thread;
+	int error;
+
+	thread = kthread_run(nbd_thread, nbd, "%s", nbd->disk->disk_name);
+	if (IS_ERR(thread)) {
+		mutex_lock(&nbd->tx_lock);
+		return PTR_ERR(thread);
+	}
+
+	error = nbd_do_it(nbd);
+
+	kthread_stop(thread);
+
+	return error;
+}
+
+/*
+ * Call with tx_lock held
+ */
+static int nbd_connection_stop(struct block_device *bdev,
+			       struct nbd_device *nbd)
+{
+	struct socket *sock;
+
+	sock_shutdown(nbd, 0);
+	sock = nbd->sock;
+	nbd->sock = NULL;
+	nbd_clear_que(nbd);
+	dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
+	kill_bdev(bdev);
+	queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
+	set_device_ro(bdev, false);
+	if (sock)
+		sockfd_put(sock);
+
+	nbd->flags = 0;
+	nbd->bytesize = 0;
+	bdev->bd_inode->i_size = 0;
+	set_capacity(nbd->disk, 0);
+
+	if (max_part > 0)
+		ioctl_by_bdev(bdev, BLKRRPART, 0);
+	if (nbd->disconnect) /* user requested, ignore socket errors */
+		return 0;
+
+	return nbd->harderror;
+}
+
+/*
+ * Call with tx_lock held
+ */
+static int nbd_connection_handler(struct block_device *bdev,
+				  struct nbd_device *nbd)
+{
+	int error;
+
+	mutex_unlock(&nbd->tx_lock);
+
+	error = nbd_connection_start(bdev, nbd);
+	if (error)
+		return error;
+
+	error = nbd_connection_run(nbd);
+
+	mutex_lock(&nbd->tx_lock);
+	if (error)
+		return error;
+
+	error = nbd_connection_stop(bdev, nbd);
+
+	return error;
+}
+
 /* Must be called with tx_lock held */
 
 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
@@ -684,61 +773,8 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		set_capacity(nbd->disk, nbd->bytesize >> 9);
 		return 0;
 
-	case NBD_DO_IT: {
-		struct task_struct *thread;
-		struct socket *sock;
-		int error;
-
-		if (nbd->pid)
-			return -EBUSY;
-		if (!nbd->sock)
-			return -EINVAL;
-
-		mutex_unlock(&nbd->tx_lock);
-
-		if (nbd->flags & NBD_FLAG_READ_ONLY)
-			set_device_ro(bdev, true);
-		if (nbd->flags & NBD_FLAG_SEND_TRIM)
-			queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
-				nbd->disk->queue);
-		if (nbd->flags & NBD_FLAG_SEND_FLUSH)
-			blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
-		else
-			blk_queue_flush(nbd->disk->queue, 0);
-
-		thread = kthread_run(nbd_thread, nbd, "%s",
-				     nbd->disk->disk_name);
-		if (IS_ERR(thread)) {
-			mutex_lock(&nbd->tx_lock);
-			return PTR_ERR(thread);
-		}
-
-		error = nbd_do_it(nbd);
-		kthread_stop(thread);
-
-		mutex_lock(&nbd->tx_lock);
-		if (error)
-			return error;
-		sock_shutdown(nbd, 0);
-		sock = nbd->sock;
-		nbd->sock = NULL;
-		nbd_clear_que(nbd);
-		dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
-		kill_bdev(bdev);
-		queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
-		set_device_ro(bdev, false);
-		if (sock)
-			sockfd_put(sock);
-		nbd->flags = 0;
-		nbd->bytesize = 0;
-		bdev->bd_inode->i_size = 0;
-		set_capacity(nbd->disk, 0);
-		if (max_part > 0)
-			ioctl_by_bdev(bdev, BLKRRPART, 0);
-		if (nbd->disconnect) /* user requested, ignore socket errors */
-			return 0;
-		return nbd->harderror;
-	}
+	case NBD_DO_IT:
+		return nbd_connection_handler(bdev, nbd);
 
 	case NBD_CLEAR_QUE:
 		/*
-- 
2.1.4


  parent reply	other threads:[~2015-01-13 13:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 13:44 [RFC 0/4] nbd: root device support Markus Pargmann
2015-01-13 13:44 ` [RFC 1/4] nbd: Replace kthread_create with kthread_run Markus Pargmann
2015-01-13 13:44 ` Markus Pargmann [this message]
2015-01-26 16:16   ` [RFC 2/4] nbd: Split 'DO_IT' into three functions Paul Clements
2015-01-26 16:23     ` Markus Pargmann
2015-01-13 13:44 ` [RFC 3/4] nbd: Create helper functions for ioctls Markus Pargmann
2015-01-28 21:04   ` Paul Clements
2015-01-30  7:54     ` Markus Pargmann
2015-01-13 13:44 ` [RFC 4/4] nbd: Add support for nbd as root device Markus Pargmann
2015-01-20 11:51   ` Markus Pargmann
2015-01-29 23:42   ` [Nbd] " Wouter Verhelst
2015-01-30  8:04     ` Markus Pargmann
2015-01-30 17:30       ` Wouter Verhelst
2015-01-31 12:38         ` Markus Pargmann
     [not found]           ` <CAECXXi5+VhBeZwZ5aosc+Oc+0bCZbJZTjzYA-QTRrJCjs2NFOw@mail.gmail.com>
2015-01-31 14:43             ` Andreas Klauer
2015-01-31 15:45               ` Markus Pargmann
2015-01-31 15:00             ` Markus Pargmann
2015-01-30 18:10       ` [Nbd] " H. Peter Anvin
2015-01-31 12:08         ` Markus Pargmann

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=1421156665-27318-3-git-send-email-mpa@pengutronix.de \
    --to=mpa@pengutronix.de \
    --cc=Paul.Clements@steeleye.com \
    --cc=kernel@pengutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®