From: Josef Bacik <jbacik@fb.com>
To: <axboe@kernel.dk>, <nbd-general@lists.sourceforge.net>,
<linux-block@vger.kernel.org>, <kernel-team@fb.com>
Subject: [PATCH 1/6] nbd: handle single path failures gracefully
Date: Tue, 28 Feb 2017 11:57:06 -0500 [thread overview]
Message-ID: <1488301031-3199-2-git-send-email-jbacik@fb.com> (raw)
In-Reply-To: <1488301031-3199-1-git-send-email-jbacik@fb.com>
Currently if we have multiple connections and one of them goes down we will tear
down the whole device. However there's no reason we need to do this as we
could have other connections that are working fine. Deal with this by keeping
track of the state of the different connections, and if we lose one we mark it
as dead and send all IO destined for that socket to one of the other healthy
sockets. Any outstanding requests that were on the dead socket will timeout and
be re-submitted properly.
Signed-off-by: Josef Bacik <jbacik@fb.com>
---
drivers/block/nbd.c | 165 +++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 124 insertions(+), 41 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 0bf2b21..7a91c8f 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -47,6 +47,8 @@ static DEFINE_MUTEX(nbd_index_mutex);
struct nbd_sock {
struct socket *sock;
struct mutex tx_lock;
+ bool dead;
+ int fallback_index;
};
#define NBD_TIMEDOUT 0
@@ -80,6 +82,7 @@ struct nbd_device {
struct nbd_cmd {
struct nbd_device *nbd;
+ int index;
struct completion send_complete;
};
@@ -188,7 +191,32 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
struct nbd_device *nbd = cmd->nbd;
- dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
+ if (nbd->num_connections > 1) {
+ dev_err_ratelimited(nbd_to_dev(nbd),
+ "Connection timed out, retrying\n");
+ mutex_lock(&nbd->config_lock);
+ /*
+ * Hooray we have more connections, requeue this IO, the submit
+ * path will put it on a real connection.
+ */
+ if (nbd->socks && nbd->num_connections > 1) {
+ if (cmd->index < nbd->num_connections) {
+ struct nbd_sock *nsock =
+ nbd->socks[cmd->index];
+ mutex_lock(&nsock->tx_lock);
+ nsock->dead = true;
+ kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
+ mutex_unlock(&nsock->tx_lock);
+ }
+ mutex_unlock(&nbd->config_lock);
+ blk_mq_requeue_request(req, true);
+ return BLK_EH_RESET_TIMER;
+ }
+ mutex_unlock(&nbd->config_lock);
+ } else {
+ dev_err_ratelimited(nbd_to_dev(nbd),
+ "Connection timed out\n");
+ }
set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
req->errors++;
@@ -294,6 +322,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
return -EIO;
}
+ cmd->index = index;
memset(&request, 0, sizeof(request));
request.magic = htonl(NBD_REQUEST_MAGIC);
request.type = htonl(type);
@@ -311,7 +340,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
if (result <= 0) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Send control failed (result %d)\n", result);
- return -EIO;
+ return -EAGAIN;
}
if (type != NBD_CMD_WRITE)
@@ -334,7 +363,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
dev_err(disk_to_dev(nbd->disk),
"Send data failed (result %d)\n",
result);
- return -EIO;
+ return -EAGAIN;
}
/*
* The completion might already have come in,
@@ -361,6 +390,12 @@ static inline int sock_recv_bvec(struct nbd_device *nbd, int index,
return result;
}
+static int nbd_disconnected(struct nbd_device *nbd)
+{
+ return test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) ||
+ test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags);
+}
+
/* NULL returned = something went wrong, inform userspace */
static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
{
@@ -374,8 +409,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
reply.magic = 0;
result = sock_xmit(nbd, index, 0, &reply, sizeof(reply), MSG_WAITALL);
if (result <= 0) {
- if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
- !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
+ if (!nbd_disconnected(nbd))
dev_err(disk_to_dev(nbd->disk),
"Receive control failed (result %d)\n", result);
return ERR_PTR(result);
@@ -416,8 +450,19 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
if (result <= 0) {
dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
result);
- req->errors++;
- return cmd;
+ /*
+ * If we've disconnected or we only have 1
+ * connection then we need to make sure we
+ * complete this request, otherwise error out
+ * and let the timeout stuff handle resubmitting
+ * this request onto another connection.
+ */
+ if (nbd_disconnected(nbd) ||
+ nbd->num_connections <= 1) {
+ req->errors++;
+ return cmd;
+ }
+ return ERR_PTR(-EIO);
}
dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
cmd, bvec.bv_len);
@@ -462,19 +507,13 @@ static void recv_work(struct work_struct *work)
while (1) {
cmd = nbd_read_stat(nbd, args->index);
if (IS_ERR(cmd)) {
+ nbd->socks[args->index]->dead = true;
ret = PTR_ERR(cmd);
break;
}
nbd_end_request(cmd);
}
-
- /*
- * We got an error, shut everybody down if this wasn't the result of a
- * disconnect request.
- */
- if (ret && !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
- sock_shutdown(nbd);
atomic_dec(&nbd->recv_threads);
wake_up(&nbd->recv_wq);
}
@@ -498,50 +537,89 @@ static void nbd_clear_que(struct nbd_device *nbd)
dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
}
+static int find_fallback(struct nbd_device *nbd, int index)
+{
+ int new_index = -1;
+ struct nbd_sock *nsock = nbd->socks[index];
+ int fallback = nsock->fallback_index;
+
+ if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags))
+ return new_index;
+
+ if (nbd->num_connections <= 1) {
+ dev_err_ratelimited(disk_to_dev(nbd->disk),
+ "Attempted send on invalid socket\n");
+ return new_index;
+ }
+
+ if (fallback >= 0 && fallback < nbd->num_connections &&
+ !nbd->socks[fallback]->dead)
+ return fallback;
+
+ mutex_lock(&nsock->tx_lock);
+ if (nsock->fallback_index < 0 ||
+ nsock->fallback_index >= nbd->num_connections ||
+ nbd->socks[nsock->fallback_index]->dead) {
+ int i;
+ for (i = 0; i < nbd->num_connections; i++) {
+ if (i == index)
+ continue;
+ if (!nbd->socks[i]->dead) {
+ new_index = i;
+ break;
+ }
+ }
+ if (new_index < 0) {
+ mutex_unlock(&nsock->tx_lock);
+ dev_err_ratelimited(disk_to_dev(nbd->disk),
+ "Dead connection, failed to find a fallback\n");
+ return new_index;
+ }
+ nsock->fallback_index = new_index;
+ }
+ new_index = nsock->fallback_index;
+ mutex_unlock(&nsock->tx_lock);
+ return new_index;
+}
-static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
+static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
{
struct request *req = blk_mq_rq_from_pdu(cmd);
struct nbd_device *nbd = cmd->nbd;
struct nbd_sock *nsock;
+ int ret;
if (index >= nbd->num_connections) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
"Attempted send on invalid socket\n");
- goto error_out;
- }
-
- if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
- dev_err_ratelimited(disk_to_dev(nbd->disk),
- "Attempted send on closed socket\n");
- goto error_out;
+ return -EINVAL;
}
-
req->errors = 0;
-
+again:
nsock = nbd->socks[index];
- mutex_lock(&nsock->tx_lock);
- if (unlikely(!nsock->sock)) {
- mutex_unlock(&nsock->tx_lock);
- dev_err_ratelimited(disk_to_dev(nbd->disk),
- "Attempted send on closed socket\n");
- goto error_out;
+ if (nsock->dead) {
+ index = find_fallback(nbd, index);
+ if (index < 0)
+ return -EIO;
+ nsock = nbd->socks[index];
}
- if (nbd_send_cmd(nbd, cmd, index) != 0) {
+ /*
+ * Some failures are related to the link going down, so anything that
+ * returns EAGAIN can be retried on a different socket.
+ */
+ mutex_lock(&nsock->tx_lock);
+ ret = nbd_send_cmd(nbd, cmd, index);
+ if (ret == -EAGAIN) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
- "Request send failed\n");
- req->errors++;
- nbd_end_request(cmd);
+ "Request send failed trying another connection\n");
+ nsock->dead = true;
+ mutex_unlock(&nsock->tx_lock);
+ goto again;
}
-
mutex_unlock(&nsock->tx_lock);
- return;
-
-error_out:
- req->errors++;
- nbd_end_request(cmd);
+ return ret;
}
static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
@@ -560,7 +638,10 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
*/
init_completion(&cmd->send_complete);
blk_mq_start_request(bd->rq);
- nbd_handle_cmd(cmd, hctx->queue_num);
+ if (nbd_handle_cmd(cmd, hctx->queue_num) != 0) {
+ bd->rq->errors++;
+ nbd_end_request(cmd);
+ }
complete(&cmd->send_complete);
return BLK_MQ_RQ_QUEUE_OK;
@@ -596,6 +677,8 @@ static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
nbd->socks = socks;
+ nsock->fallback_index = -1;
+ nsock->dead = false;
mutex_init(&nsock->tx_lock);
nsock->sock = sock;
socks[nbd->num_connections++] = nsock;
--
2.7.4
next prev parent reply other threads:[~2017-02-28 16:57 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-28 16:57 [PATCH 0/6] Lots of NBD fixes and enhancements Josef Bacik
2017-02-28 16:57 ` Josef Bacik [this message]
2017-02-28 16:57 ` [PATCH 2/6] nbd: ref count the nbd device Josef Bacik
2017-02-28 16:57 ` [PATCH 3/6] nbd: stop using the bdev everywhere Josef Bacik
2017-02-28 16:57 ` [PATCH 4/6] nbd: set queue timeout properly Josef Bacik
2017-02-28 16:57 ` [PATCH 5/6] nbd: handle ERESTARTSYS properly Josef Bacik
2017-02-28 16:57 ` [PATCH 6/6] nbd: add a basic netlink interface Josef Bacik
2017-03-08 10:07 ` [Nbd] " Wouter Verhelst
2017-03-08 14:56 ` Josef Bacik
2017-03-08 20:44 ` Wouter Verhelst
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=1488301031-3199-2-git-send-email-jbacik@fb.com \
--to=jbacik@fb.com \
--cc=axboe@kernel.dk \
--cc=kernel-team@fb.com \
--cc=linux-block@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