From patchwork Tue Feb 28 16:57:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9596481 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9DA92601D7 for ; Tue, 28 Feb 2017 17:58:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95BF228536 for ; Tue, 28 Feb 2017 17:58:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8970328549; Tue, 28 Feb 2017 17:58:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 80C0228547 for ; Tue, 28 Feb 2017 17:58:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751122AbdB1R6c (ORCPT ); Tue, 28 Feb 2017 12:58:32 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:43473 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751037AbdB1R6a (ORCPT ); Tue, 28 Feb 2017 12:58:30 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1SGvUwp025344; Tue, 28 Feb 2017 08:58:04 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=gLPmwKSAnsbq86JyTpQQtxTYc5xY0k+NJUYUvPVTv+g=; b=UPEOvupWgrFbEeD+J2qnc5HZRZN2DugQ4Lge349NkxK6QyxkfWJ9juNVjECACoz8HQrP 3AdV7Lj09hpA7dH2C59pZ65wOTdUdy7Ck3oU3OVCTUoSk5HI6dHmPtGyO/VyRZKxhqkS Cic7b7WCxk9KJdlRJaSi1enAbj0KpAfxVJE= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 28w838h4mw-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 28 Feb 2017 08:58:04 -0800 Received: from NAM03-CO1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.17) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 28 Feb 2017 08:58:03 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=gLPmwKSAnsbq86JyTpQQtxTYc5xY0k+NJUYUvPVTv+g=; b=h86kE6LcHMnYmlmIuAydZTY+Up2VjUXCFWm9CtD9E95wpoHQl8ac1KOESt3eqjmCtnCuHX9XMwSrQFXkHqgCR/SCfzxMCkKHEqyrSvEcEb3Q8KD5F9eWM5yRTaap9ldr5VnJODHs/FwLSgeUnah0QFuVRPkGEAbaXYKdXXNJJfk= Received: from localhost (2606:a000:4381:1201:225:22ff:feb3:e51a) by CY4PR15MB1909.namprd15.prod.outlook.com (10.174.54.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.933.12; Tue, 28 Feb 2017 16:58:01 +0000 From: Josef Bacik To: , , , Subject: [PATCH 5/6] nbd: handle ERESTARTSYS properly Date: Tue, 28 Feb 2017 11:57:10 -0500 Message-ID: <1488301031-3199-6-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488301031-3199-1-git-send-email-jbacik@fb.com> References: <1488301031-3199-1-git-send-email-jbacik@fb.com> MIME-Version: 1.0 X-Originating-IP: [2606:a000:4381:1201:225:22ff:feb3:e51a] X-ClientProxiedBy: BN6PR11CA0032.namprd11.prod.outlook.com (10.173.25.18) To CY4PR15MB1909.namprd15.prod.outlook.com (10.174.54.142) X-MS-Office365-Filtering-Correlation-Id: 80642bf0-54fa-4a75-3671-08d45ffaf4e7 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:CY4PR15MB1909; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1909; 3:S8TyGmgeEW5GcpAKtALRL/WAcGy6BNgXwW62TmRYQ6W235tP6oqkYKI+pjuk8poVO+JTcA/LYlxHbmGgKG30iWc+sl4cAiltxdOqkqFBtAoYdyYPYNiXNJRsjKcYwtl4OCcpO30Vw04Tk2uHdIBTa1wE+6zUIqGTVVzYC/aGDhMswsSa2MgOjM0eCTN7AXEqmHTdWMxeS5pq8A0SqF1aziZS1l1Y/NV+Ks00hCTnjO1hTyQ/588W3qOTzZCE4doyHE91y01Jn5mbQ2hVYrsXzQ==; 25:ux34/rSGlx6MjwjELCOY5+VBaXnQSNVezsXviwiTYuGYcs5eo+i1ff8RBrI1xU+GzSNjh5Y7aGPTZwcoglwZMruS87hPV1lK9aiU7OHLZe4i+zDfy2L3/83OAzXAOX7cVcC0XllD0U5kQznWdO7SEmeAQvO02ELH8wY12d3bhda/QMB7SUgzqLVbeNm76wP0yhUeHxX4acOcpXQIDlJ9zVPZnmjOn3L9cLGTM8WOLePGU6zoxmkp5DkT4DxJ6qSPyiacNzUxTMVyRItx3j65BswswuzAr8TxNgEzv1dDNRLezPXSsLy6g813lPZtZCkC0DcYiptjp65+5pvumrb+ghdg2fcUsFHxlCE/28mA6hClMXTx6yHuQ2IY8CTwyJM6wiSnWX49u07c9Wz9bMv/zSED6A4wv3RriVHlSB0DIw/hivotX4O+sw+PcpMhVE6YBR2yEZCP/96C+rE7Jt+Iwg== X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1909; 31:slsIDoyV472Xeur1H9mdUjLWbK4noq+mrew5yjmbMG1mGvrpqdzZkKcSNZWQwX8M6ynTmdADdAUx7Fkjpm1WjaT3nyNaiTE8/K3y8MXQSAxJkkoRmDxdsiopozQYbGIQdm4+7G8XsQnIOXaKeqh6TKMKeBM2pE+Qizc2t1zztnGP5Z6+BQB2DeA3/oCvpPCYC8OBGVcPkD1OcrYlQbUU7YV+6twU9mQliiYxM+kUQfDm6mG0JGup2DCQJJZ6mi2q; 20:3VlnRUHcMpGakwc9KFghIGpGZR0OhvnLqvYCURW7qLsjukQhu+USAz3f4OVVoAJyuQ3aOPK9PDz0ANvywW3SVZ3ujGY9PjB2YXPEjxAiODEJPc+8RHdkxrFHfz6au40g0LqKSoJdXdJiqQ4NlPI6qUgOnmVYt9rKia4Y9p/G7c34DCK3Aq/3CuosviItY0Zi9dNW0WfYh9WrKwiu2zHSu8ZNi+CH+smnmKTpb75SUaO4r88cjOmZ0RbITCZ9fjc2EenItiZqeWggaQssVzX0X7F64GJeaAaZIgStnwyIElXy8xGkV8pgdM1w7TaPlgGKQYQSIl4WwjlECW8KMYIqrgIef8aRqR4pUL0o6CCD0bDYBAVY4yJ6odXyJfdS08aX8IpDbErSVk1AWxjwBZJFSOjfzrYTCfx2E73sVA02V1TyAZ/6ZtJ1qmjEb70XhblyH6DD9b2v7HIeQEZ3gKlvHfnrAm/mEJTxeeloPXKG6H0Ba3hIj00+CnoeZMNvaA56 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(67672495146484); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6041248)(20161123558025)(20161123564025)(20161123562025)(20161123555025)(20161123560025)(6072148); SRVR:CY4PR15MB1909; BCL:0; PCL:0; RULEID:; SRVR:CY4PR15MB1909; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1909; 4:iZSKzVouS5czB98VPpnFFx1t7zoZ4+iNq7u5MjzaM0woykV88zgTYiDPoRNT1sECQKElgMZ+5XT1lMmPf4KfqNdv4IgYBW6ftDXFM789gP1/Qr/AOanKjae5oHhetDRfG7jnZM9FzE2A4R47PZURHSoyWB7m+2jI9LOGtUgcnMMPY0XYHxouNsLVmkKiA1GhWviJMWJewSrPX3t/Bvy+jWdmXe0AkCkNObWA/QHNpBTadmrAjXFBNzxz0+5rKbNQqdSdntZb0LkHJVecsD00Ux3PaITZtkoNcOcQS46L3edwR2N61djCqg7ag5pUaRtkPIAadUfnzmoDHkY10uboYwP0wU/3ii4Y0vttVRoZs075p7VLGhac+0Qxgt/buQifmh8cyFjAhWCOgZzwQWH9yy/oeNYr7YoBiBPw97iE/oPaljOUwXMyPYGh74szRjeK1I2mDnfwDdS2O4kMVe4ZrW5iAMow5GqUDJlYnNakfwXll1Py2MCUoV1AOnMly9Oz/nE+LrJvTLZCygm6uI8GzWZKmQPi/ly1POtlPvnJtKkl4OLDkM9PqrIpxZdGJVA84mKbWJp3So739a9dMGaMT17lwmUyCqMOuPQIYnM2iyX74F/FM/Hs6d0EUnjIimud+DC1cIX91pWKcgmxePfRPw== X-Forefront-PRVS: 0232B30BBC X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6069001)(6009001)(7916002)(39410400002)(39830400002)(39450400003)(189002)(199003)(76176999)(5660300001)(106356001)(105586002)(25786008)(97736004)(5003940100001)(2906002)(50986999)(189998001)(6666003)(101416001)(2950100002)(50226002)(6486002)(81156014)(8676002)(81166006)(36756003)(6636002)(305945005)(7736002)(68736007)(53936002)(42186005)(48376002)(50466002)(86362001)(92566002)(47776003)(76506005)(6116002)(6496005)(2201001)(38730400002)(33646002)(473944003); DIR:OUT; SFP:1102; SCL:1; SRVR:CY4PR15MB1909; H:localhost; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY4PR15MB1909; 23:+jhdnLY1pRMRol4qOEt395UFfkd6o1TiWzX5ywW7L?= =?us-ascii?Q?yQ6/Iy1Uw8loLFu2/iJp+4jndRrtJ6Whv5Gf/h66Yhkf+rtTgGgxIlwiwj2q?= =?us-ascii?Q?Emi3HJkzi0cDOmKQAMtv4446qLCSu0Yuu/HVTLbvr8JnUhO37/Z7hMbx0NKj?= =?us-ascii?Q?VXc5uz1lbo+j4z3UnmWx21SCoZsd4uUEaAFgjGbVPueqGf03YqEtnba1Vyc6?= =?us-ascii?Q?TTe9k6c0I8dNV3yX8DW2rlVGsqyzXpf+zzLCxDtVQ2UQ5KEyjBmLljUlzJn1?= =?us-ascii?Q?yJ1NMHGLASJHUOKpayQJt+BeenywblyJj+tASFax2s2VvO13LgAdx2vN3YTU?= =?us-ascii?Q?r3Qa6ePWtZMuQ4WvZQyU//SIJVbNjnMQr9ooYeWUi3VN+kJ0ixObxFDekuCN?= =?us-ascii?Q?Yk+9AL+TlU1rUODmrv1Jtppr6zXnH+2shKeAIFX++Lqias8cHoQif4Iv8s5Z?= =?us-ascii?Q?01z1vPtcqc+X4ELKZTGNVRnMPx6+o9xl/HCssmRQp4Imcy9MbsOFeTGtacrI?= =?us-ascii?Q?HGSNumsjPEdhPzVmaVliGUssuoq0VsbzvDNcP6pSWQ4fGBX1/HxNeRc9JMXH?= =?us-ascii?Q?CzBTEe3k+3oaF9lIQEa1J4pTEC2OBjGqn9sLp/U3SnrS0uAe0grLZCctcdqN?= =?us-ascii?Q?Knfpa+UorDnbZ8VJ5WCqGVgVsZUbYY2uh62SlG9//82ND3i4vQ7CRO+6oqeH?= =?us-ascii?Q?TA9jwPmTLAfxVyYxb22zOfCMivr27XCYJhKMv49NoqGm39xRmnOljH3sW+0u?= =?us-ascii?Q?LGDVhqWKs1yAKk7y2hoSiWu0mz4Y5xEKwrsewlBPJNGud3wj7Id7FVPuT94c?= =?us-ascii?Q?uvHc44PxEdbH77lMtM3OJhsQvYcgtKfayWJp8Reue1lH/PRy7AGHRuTBxMVr?= =?us-ascii?Q?Nb2nBkt26cWHJaSf2sZB6fFdlydRD3+HYWlE0PQgISdFamCohH8kAJAUykYB?= =?us-ascii?Q?6dwv4QouldfF7/KR4RUFIrdpxS2iyP8KF8MLLdwGPEv5IwTcAqNtdeOW5hK3?= =?us-ascii?Q?Zb+8b8ofguhZRofo1i9kixzMdKumi1A0sUu1xEjMehabo51D97RIkY4mx/Sl?= =?us-ascii?Q?pGlCMLWMlzemw5b/gPLNXUa2Sk62PsQ3GxPFToSI+kgfhY5M0/oBX6jwlbRf?= =?us-ascii?Q?3mCM8wPWo/rvFW4cyEDCs47m4Mf8BU4?= X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1909; 6:Hu/HxzdLrRt+yC64KVq/VpKZ1DgWbkEa+YTJWRusM1o4LbmBEznupp6hqDWQojfvLAgahD8FgwwVQzNdFWW8q0fn0+7DsiNDXlkU4O15rTOz1UH58GSL2wlKU6pdmQml6psc9nhXOqaTSHfbn4fvUkjnW8v3nwMLR+hznoNLnF12yUUP5U1f2DgG7TY7vGimW9sMD/fFuTm9WLBnxX4xgwCbdJP/ut8YGh7UzcHU4MU36snbPpS0cNc4Gi25q7zZHu3S5K0AcTG9xNrq7xemPGK86NmWrOMgYa072CBQug5ZFlqU15EcMBigjZPhdJpxVcQ3DVzSLr3TSF9ubYADvlYlv8Kw0YZAFnHCnkGEoITqCpUaKpjsUNDlYm1Wmx2tSFiqZbl5B6hB4PXHahBVjA==; 5:XSFsaxDC3uPc+RLp2kGRJ6OWIBo/oFg2b9h8IDmbmXm1XnZOMWUNKraYXI8qJf4FQolVK5TOFHduhv/wMOKoPDqfWZvgiV50safMlYfCgAM8uwZN4XrHgXnqbEBodQVpdoXBAvzGLcTMMfxb/a2Is0XyRPWrH12s/mNu0VLcYTk=; 24:F4SrXthLLxlUmESoYDtAVceVuoP6flEXo1Mw4geO5oO1p3lOJ5Z6ITrr6yIEXOPVH6DX0YSIxG3o45caPNCeZyk+CeKcV8np3h4nlAPx/Ss= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1909; 7:mKSRIIxY0ElChkzgecvgqnIoLbvhHi+FJ/mRKucrTvHqNU48wrwJyGTKc9w/G5pDo/pHFyVnjqpTXv8t3dUwLV4zJ4ZLddMzRkHCL0dr/cBTbL3lVcH0DsOMYWgMVBzIzvyEd3D1EEMzF2UW8WNlUKjln9uUhSyJe818AX2nb19Xthd2buLznqjaR/QLBtCHEDi9BzXH367Dee2Ve2aHa92sfFkEOeLA1t/9MtKXQJ8IiabE1/ygqNGIB2yyvik3FtOjQ4EtOVtWg4+9El7nh2+XKOrwpTifzwBwlb3I0VE56e9mGJoed0J5g2ef//YqVpL+GBrrlXZxPH+6si2PXw==; 20:chGfJICAqLd0QC1Ew+MmOXm1oCi0ujEJNes5zrPt9P3XqSCxkZV63e65qpy606Hv3F4+2Lf8JdYFOi3dfPutWcLYSBB2R5mWv+Y2kGGHZZOHFfpKsASwhRoCvUSeikBsy1/aS4BfhOCQ/2zKa5mRUk7kANte0a3JIouS9CGdJW4= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Feb 2017 16:58:01.9619 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1909 X-OriginatorOrg: fb.com X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-28_15:, , signatures=0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We can submit IO in a processes context, which means there can be pending signals. This isn't a fatal error for NBD, but it does require some finesse. If the signal happens before we transmit anything then we are ok, just requeue the request and carry on. However if we've done a partial transmit we can't allow anything else to be transmitted on this socket until we transmit the remaining part of the request. Deal with this by keeping track of how much we've sent for the current request, and if we get an ERESTARTSYS during any part of our transmission save the state of that request and requeue the IO. If anybody tries to submit a request that isn't our pending request then requeue that request until we are able to service the one that is pending. Signed-off-by: Josef Bacik --- drivers/block/nbd.c | 138 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 116 insertions(+), 22 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index ac5a03a..aba1aba 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -49,6 +49,8 @@ struct nbd_sock { struct mutex tx_lock; bool dead; int fallback_index; + struct request *pending; + int sent; }; struct recv_thread_args { @@ -145,6 +147,13 @@ static struct device_attribute pid_attr = { .show = pid_show, }; +static void nbd_mark_nsock_dead(struct nbd_sock *nsock) +{ + nsock->dead = true; + nsock->pending = NULL; + nsock->sent = 0; +} + static int nbd_get_unless_zero(struct nbd_device *nbd) { return atomic_add_unless(&nbd->refs, 1, 0); @@ -200,8 +209,8 @@ static void sock_shutdown(struct nbd_device *nbd) struct nbd_sock *nsock = nbd->socks[i]; mutex_lock(&nsock->tx_lock); kernel_sock_shutdown(nsock->sock, SHUT_RDWR); + nbd_mark_nsock_dead(nsock); mutex_unlock(&nsock->tx_lock); - nsock->dead = true; } dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n"); } @@ -229,8 +238,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, struct nbd_sock *nsock = nbd->socks[cmd->index]; mutex_lock(&nsock->tx_lock); - nsock->dead = true; kernel_sock_shutdown(nsock->sock, SHUT_RDWR); + nbd_mark_nsock_dead(nsock); mutex_unlock(&nsock->tx_lock); } blk_mq_requeue_request(req, true); @@ -253,7 +262,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, * Send or receive packet. */ static int sock_xmit(struct nbd_device *nbd, int index, int send, void *buf, - int size, int msg_flags) + int size, int msg_flags, int *sent) { struct socket *sock = nbd->socks[index]->sock; int result; @@ -292,6 +301,8 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send, void *buf, } size -= result; buf += result; + if (sent) + *sent += result; } while (size > 0); tsk_restore_flags(current, pflags, PF_MEMALLOC); @@ -300,12 +311,28 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send, void *buf, } static inline int sock_send_bvec(struct nbd_device *nbd, int index, - struct bio_vec *bvec, int flags) + struct bio_vec *bvec, int flags, int *skip, + int *sent) { + void *kaddr; + unsigned int offset = bvec->bv_offset; + unsigned int len = bvec->bv_len; int result; - void *kaddr = kmap(bvec->bv_page); - result = sock_xmit(nbd, index, 1, kaddr + bvec->bv_offset, - bvec->bv_len, flags); + + /* If we had partially sent this request previously we need to skip over + * what we've already sent. + */ + if (*skip) { + if (len <= *skip) { + *skip -= len; + return len; + } + offset += *skip; + len -= *skip; + *skip = 0; + } + kaddr = kmap(bvec->bv_page); + result = sock_xmit(nbd, index, 1, kaddr + offset, len, flags, sent); kunmap(bvec->bv_page); return result; } @@ -314,12 +341,14 @@ static inline int sock_send_bvec(struct nbd_device *nbd, int index, static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) { struct request *req = blk_mq_rq_from_pdu(cmd); + struct nbd_sock *nsock = nbd->socks[index]; int result; struct nbd_request request; unsigned long size = blk_rq_bytes(req); struct bio *bio; u32 type; u32 tag = blk_mq_unique_tag(req); + int sent = nsock->sent, skip = 0; switch (req_op(req)) { case REQ_OP_DISCARD: @@ -345,6 +374,15 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) return -EIO; } + /* We did a partial send previously, and we at least sent the whole + * request struct, so just go and send the rest of the pages in the + * request. + */ + if (sent && sent >= sizeof(request)) { + skip = sent - sizeof(request); + goto send_pages; + } + cmd->index = index; memset(&request, 0, sizeof(request)); request.magic = htonl(NBD_REQUEST_MAGIC); @@ -358,16 +396,33 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", cmd, nbdcmd_to_ascii(type), (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); - result = sock_xmit(nbd, index, 1, &request, sizeof(request), - (type == NBD_CMD_WRITE) ? MSG_MORE : 0); + + /* We may have only partially sent the request previously, so handle + * this horrible, horrible, terrifying possiblity here. + */ + result = sock_xmit(nbd, index, 1, (void *)(&request) + sent, + sizeof(request) - sent, + (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); if (result <= 0) { + if (result == -ERESTARTSYS) { + /* If we havne't sent anything we can just return BUSY, + * however if we have sent something we need to make + * sure we only allow this req to be sent until we are + * completely done. + */ + if (sent) { + nsock->pending = req; + nsock->sent = sent; + } + return BLK_MQ_RQ_QUEUE_BUSY; + } dev_err_ratelimited(disk_to_dev(nbd->disk), "Send control failed (result %d)\n", result); return -EAGAIN; } - +send_pages: if (type != NBD_CMD_WRITE) - return 0; + goto out; bio = req->bio; while (bio) { @@ -381,8 +436,18 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", cmd, bvec.bv_len); - result = sock_send_bvec(nbd, index, &bvec, flags); + result = sock_send_bvec(nbd, index, &bvec, flags, + &skip, &sent); if (result <= 0) { + if (result == -ERESTARTSYS) { + /* We've already sent the header, we + * have no choice but to set pending and + * return BUSY. + */ + nsock->pending = req; + nsock->sent = sent; + return BLK_MQ_RQ_QUEUE_BUSY; + } dev_err(disk_to_dev(nbd->disk), "Send data failed (result %d)\n", result); @@ -399,6 +464,9 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) } bio = next; } +out: + nsock->pending = NULL; + nsock->sent = 0; return 0; } @@ -408,7 +476,7 @@ static inline int sock_recv_bvec(struct nbd_device *nbd, int index, int result; void *kaddr = kmap(bvec->bv_page); result = sock_xmit(nbd, index, 0, kaddr + bvec->bv_offset, - bvec->bv_len, MSG_WAITALL); + bvec->bv_len, MSG_WAITALL, NULL); kunmap(bvec->bv_page); return result; } @@ -430,7 +498,8 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) u32 tag; reply.magic = 0; - result = sock_xmit(nbd, index, 0, &reply, sizeof(reply), MSG_WAITALL); + result = sock_xmit(nbd, index, 0, &reply, sizeof(reply), MSG_WAITALL, + NULL); if (result <= 0) { if (!nbd_disconnected(nbd)) dev_err(disk_to_dev(nbd->disk), @@ -510,7 +579,10 @@ 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; + struct nbd_sock *nsock = nbd->socks[args->index]; + mutex_lock(&nsock->tx_lock); + nbd_mark_nsock_dead(nsock); + mutex_unlock(&nsock->tx_lock); ret = PTR_ERR(cmd); break; } @@ -622,14 +694,26 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) * returns EAGAIN can be retried on a different socket. */ mutex_lock(&nsock->tx_lock); + + /* Handle the case that we have a pending request that was partially + * transmitted that _has_ to be serviced first. We need to call requeue + * here so that it gets put _after_ the request that is already on the + * dispatch list. + */ + if (unlikely(nsock->pending && nsock->pending != req)) { + blk_mq_requeue_request(req, true); + ret = 0; + goto out; + } ret = nbd_send_cmd(nbd, cmd, index); if (ret == -EAGAIN) { dev_err_ratelimited(disk_to_dev(nbd->disk), "Request send failed trying another connection\n"); - nsock->dead = true; + nbd_mark_nsock_dead(nsock); mutex_unlock(&nsock->tx_lock); goto again; } +out: mutex_unlock(&nsock->tx_lock); nbd_put(nbd); return ret; @@ -639,6 +723,7 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, const struct blk_mq_queue_data *bd) { struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); + int ret; /* * Since we look at the bio's to send the request over the network we @@ -651,13 +736,20 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, */ init_completion(&cmd->send_complete); blk_mq_start_request(bd->rq); - if (nbd_handle_cmd(cmd, hctx->queue_num) != 0) { - bd->rq->errors++; - nbd_end_request(cmd); - } + + /* We can be called directly from the user space process, which means we + * could possibly have signals pending so our sendmsg will fail. In + * this case we need to return that we are busy, otherwise error out as + * appropriate. + */ + ret = nbd_handle_cmd(cmd, hctx->queue_num); + if (ret < 0) + ret = BLK_MQ_RQ_QUEUE_ERROR; + if (!ret) + ret = BLK_MQ_RQ_QUEUE_OK; complete(&cmd->send_complete); - return BLK_MQ_RQ_QUEUE_OK; + return ret; } static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg) @@ -699,6 +791,8 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg) nsock->dead = false; mutex_init(&nsock->tx_lock); nsock->sock = sock; + nsock->pending = NULL; + nsock->sent = 0; socks[nbd->num_connections++] = nsock; err = 0; @@ -751,7 +845,7 @@ static void send_disconnects(struct nbd_device *nbd) request.type = htonl(NBD_CMD_DISC); for (i = 0; i < nbd->num_connections; i++) { - ret = sock_xmit(nbd, i, 1, &request, sizeof(request), 0); + ret = sock_xmit(nbd, i, 1, &request, sizeof(request), 0, NULL); if (ret <= 0) dev_err(disk_to_dev(nbd->disk), "Send disconnect failed %d\n", ret);