Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp228381ybi; Fri, 24 May 2019 02:57:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqwBJHdrPyh4KFDZ54UeWJHDW7FoEcASO4AgcT9GEtwXRPa19ey7U6e3aYI1tCBRyTdpOdK5 X-Received: by 2002:a17:902:2947:: with SMTP id g65mr84699333plb.115.1558691841093; Fri, 24 May 2019 02:57:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558691841; cv=none; d=google.com; s=arc-20160816; b=exGJxnMQUXl7vIDiDzsomLo57GjW1RVDVR/ropTvPXhrIBumpLN7kTnw4wUZ58AoU/ zAIEyJ7HZsK+5egXT0Ykqxky6bQ/wSBmbdcvaVYBC3zSmoeRmJQ8ql/vdXjRoxuSP4QA m6yzxw67FCJYIrMTu7JkCOsRgayI86srWglvXnADctxN7ILWoVzs3li0144YORYISSF7 n/W7eEuyJtsi3ADxKclt2Ov8+tlEDEFxaChp/F0IwKBjHoS6gDQLvZVN5C+sYFD4VCv9 6EijJRrS5lm7riTeMW3w0l+c8HX88XgV1rqQax9I64a58MsCAEUg3HQ4euRM06eld6DQ yKdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=2QLOt4/Lcbdp0AlB3ggj0wmmW/3V6L1TH0FIVWNHroc=; b=ptbxB6+Dl2zm/B9t5mvsyT9IA8mZKJ9VGmocc4/SCpKQVP8zPXk4w8AzISOcHTNqU+ HKiqNE1msAo75wHOh7o4FTlbia8vizDZTem2ffaf9FE68qt3AaoZxjko7+ZBoUrGR7Sx 7qmaOROvimMve5DiMQIoh+RGzDGZLNemujDo8va19p4KGWQ/LevxFdmaxjZyt6l1nmrX S46KDx076fs1F6t7Q+4yXMJsjMY/FjfJVoI8UtglXTOKlrk10L3ThZk5/EusC22TuTB+ AKFypcLUeRpKVom3fYtlFmDZUHp0KMaXSg7Doi6Z2f3iDyBLqYMv52PkSYpbtGBna8L2 6R2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ucloud.cn Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t34si3326999pgm.396.2019.05.24.02.57.03; Fri, 24 May 2019 02:57:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ucloud.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390314AbfEXJzq (ORCPT + 99 others); Fri, 24 May 2019 05:55:46 -0400 Received: from m9784.mail.qiye.163.com ([220.181.97.84]:30670 "EHLO m9784.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389913AbfEXJzp (ORCPT ); Fri, 24 May 2019 05:55:45 -0400 Received: from kernel_test2.localdomain (unknown [120.132.1.243]) by m9784.mail.qiye.163.com (Hmail) with ESMTPA id B6CFB415E9; Fri, 24 May 2019 17:48:02 +0800 (CST) From: Yao Liu To: Josef Bacik , Jens Axboe Cc: linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] nbd: fix connection timed out error after reconnecting to server Date: Fri, 24 May 2019 17:43:54 +0800 Message-Id: <1558691036-16281-1-git-send-email-yotta.liu@ucloud.cn> X-Mailer: git-send-email 1.8.3.1 X-HM-Spam-Status: e1kIGBQJHllBWUlVT0tMQkJCQkJJSExLTUpZV1koWUFJQjdXWS1ZQUlXWQ kOFx4IWUFZNTQpNjo3JCkuNz5ZBg++ X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6PS46Dio*Ezg1TSxWMTA*CEwe TSMwFA9VSlVKTk5DTUJKSUNIS0xLVTMWGhIXVQIUDw8aVRcSDjsOGBcUDh9VGBVFWVdZEgtZQVlK SUtVSkhJVUpVSU9IWVdZCAFZQU9ISEg3Bg++ X-HM-Tid: 0a6ae93d7ff12086kuqyb6cfb415e9 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some I/O requests that have been sent succussfully but have not yet been replied won't be resubmitted after reconnecting because of server restart, so we add a list to track them. Signed-off-by: Yao Liu --- drivers/block/nbd.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 053958a..ca69d6e 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -113,6 +113,8 @@ struct nbd_device { struct list_head list; struct task_struct *task_recv; struct task_struct *task_setup; + struct mutex outstanding_lock; + struct list_head outstanding_queue; }; #define NBD_CMD_REQUEUED 1 @@ -125,6 +127,7 @@ struct nbd_cmd { blk_status_t status; unsigned long flags; u32 cmd_cookie; + struct list_head outstanding_entry; }; #if IS_ENABLED(CONFIG_DEBUG_FS) @@ -619,6 +622,24 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) return 0; } +static struct nbd_cmd *nbd_get_cmd(struct nbd_device *nbd, + struct nbd_cmd *xcmd) +{ + struct nbd_cmd *cmd, *tmp; + + mutex_lock(&nbd->outstanding_lock); + list_for_each_entry_safe(cmd, tmp, &nbd->outstanding_queue, outstanding_entry) { + if (cmd != xcmd) + continue; + list_del_init(&cmd->outstanding_entry); + mutex_unlock(&nbd->outstanding_lock); + return cmd; + } + mutex_unlock(&nbd->outstanding_lock); + + return ERR_PTR(-ENOENT); +} + /* NULL returned = something went wrong, inform userspace */ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) { @@ -714,12 +735,30 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) req, bvec.bv_len); } } + cmd = nbd_get_cmd(nbd, cmd); + if (IS_ERR(cmd)) { + dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p which not in outstanding queue\n", + tag, req); + ret = -ENOENT; + } out: trace_nbd_payload_received(req, handle); mutex_unlock(&cmd->lock); return ret ? ERR_PTR(ret) : cmd; } +static void nbd_requeue_outstanding(struct nbd_device *nbd) +{ + struct nbd_cmd *cmd, *tmp; + + mutex_lock(&nbd->outstanding_lock); + list_for_each_entry_safe(cmd, tmp, &nbd->outstanding_queue, outstanding_entry) { + nbd_requeue_cmd(cmd); + list_del_init(&cmd->outstanding_entry); + } + mutex_unlock(&nbd->outstanding_lock); +} + static void recv_work(struct work_struct *work) { struct recv_thread_args *args = container_of(work, @@ -742,6 +781,7 @@ static void recv_work(struct work_struct *work) blk_mq_complete_request(blk_mq_rq_from_pdu(cmd)); } + nbd_requeue_outstanding(nbd); atomic_dec(&config->recv_threads); wake_up(&config->recv_wq); nbd_config_put(nbd); @@ -892,6 +932,10 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) nbd_mark_nsock_dead(nbd, nsock, 1); nbd_requeue_cmd(cmd); ret = 0; + } else if (ret == 0) { + mutex_lock(&nbd->outstanding_lock); + list_add_tail(&cmd->outstanding_entry, &nbd->outstanding_queue); + mutex_unlock(&nbd->outstanding_lock); } out: mutex_unlock(&nsock->tx_lock); @@ -1615,6 +1659,8 @@ static int nbd_dev_add(int index) refcount_set(&nbd->config_refs, 0); refcount_set(&nbd->refs, 1); INIT_LIST_HEAD(&nbd->list); + mutex_init(&nbd->outstanding_lock); + INIT_LIST_HEAD(&nbd->outstanding_queue); disk->major = NBD_MAJOR; disk->first_minor = index << part_shift; disk->fops = &nbd_fops; -- 1.8.3.1