From patchwork Tue Feb 7 22:10:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9561367 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 A2A456047A for ; Tue, 7 Feb 2017 23:02:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90F352848D for ; Tue, 7 Feb 2017 23:02:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 852A428490; Tue, 7 Feb 2017 23:02:21 +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 822D62848D for ; Tue, 7 Feb 2017 23:02:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751906AbdBGXCS (ORCPT ); Tue, 7 Feb 2017 18:02:18 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:59963 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751743AbdBGXCR (ORCPT ); Tue, 7 Feb 2017 18:02:17 -0500 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v17M4pJ0030986; Tue, 7 Feb 2017 14:10:30 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : mime-version : content-type; s=facebook; bh=oL3Ep2WQTyCLEgxQ3Jm6eQMssDgcRmjU+4MSma5Smsc=; b=G6hFgmP5Y4qHLoazn08DrDOE5MXyrLSAE1zNVpjVFMRYfpTSKq5p8LJPyMccLmudypmE xcDmJa8aDPDIyftavWxMUy1vUbLdhxDvW6Puy+NEQLUU3CmLzWtsvSbye+yJnmMwduEY a9ehKtd2uzDip0t00l7xskTwMPGBrG0RSr8= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 28fh4a1gmm-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 07 Feb 2017 14:10:30 -0800 Received: from NAM01-BY2-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.11) with Microsoft SMTP Server (TLS) id 14.3.294.0; Tue, 7 Feb 2017 14:10:29 -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=oL3Ep2WQTyCLEgxQ3Jm6eQMssDgcRmjU+4MSma5Smsc=; b=Jeyf6EWQmfgxRyq9O/OHmNJ8ZhnorHeljspOO1mtUxh+G4llZv2rF7jFip4RApojKNQcC7G3VTDb0is4e1dmqPqqsrIzJnR/wvt7yaUAMKx0fwTOXU2I08odHVsN0OHwkMT3giwKRJB4l6jhWo8sE5mJrY0sPwZbt/xB0KWDXyk= Received: from localhost (2606:a000:4381:1201:225:22ff:feb3:e51a) by CY4PR15MB1912.namprd15.prod.outlook.com (10.174.54.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.888.16; Tue, 7 Feb 2017 22:10:26 +0000 From: Josef Bacik To: , , , Subject: [PATCH] nbd: cleanup ioctl handling Date: Tue, 7 Feb 2017 17:10:22 -0500 Message-ID: <1486505422-2850-1-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Originating-IP: [2606:a000:4381:1201:225:22ff:feb3:e51a] X-ClientProxiedBy: MWHPR15CA0027.namprd15.prod.outlook.com (10.173.226.141) To CY4PR15MB1912.namprd15.prod.outlook.com (10.174.54.145) X-MS-Office365-Filtering-Correlation-Id: cac9fc68-1cc4-4551-0db2-08d44fa61f39 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:CY4PR15MB1912; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1912; 3:Bxx22Vd/1JbC3+Cn3T1xK7/Kl4vDZfp4eJfHyRuBd+imvYnTeVH7SGdbgAdiLerMTgUILm26ZIdyA99CvvCO2/2bBdUx/4cmpW/CoyUpiWiBx8vf6Zjybxm1ek0qBCQr54GQOeuy/DufLR1FAgMgoCjiEN1LDfNvYFCiJ5rRttqovL1JhleLhs1BI8Osz4ZuBXtn2FrtRdnCPP5Rc80Md01z8YeCXNDn+YhsDRB2ld80Xf4+bMNHvZFgyFgU/e00lvbYcxqElwxK4BBOR7440g==; 25:Mk3yoitty9pjxE+Cy7SMZdUstVZbS34D2xFHTTVIN7rRw4l0jXUPE6puw8XRJwN8ZLjm7T9QdwjTQOXIoId9c2abdgPRU9LXezvA/NOsBBsIbQBywaufQ3uUHx1qF4urf+avvkByjKm3W8tXd4BFPy/U/eQ4w2XAR4UizGSLffewnIqyElmjaN9WoUg95TzeqJi51MALgwwIc3Du3Ok23tOk2I7An+30ARCJzfxICx7m2qQzrAjZEJDG3C9vd675NcvgvuvPBb6+6zzXIIV6Erh/00Xb7v0amRKUPsCAXgvS4pk8Q1371/0qsa3sgYOd8FHWd77iSVKWoI3KPfV4AsN+AC+qKBY+CMc3Wa0/s26UDhh5V1gONjPzZ4sVK4ntNd7jOmH2ZS89/4EUR3EOsVsP8LzlYeeZfOZdLW49360NgWwPyVhB4DMaHSCOb1P7VRD056StIV8HQq9FyWZmAA== X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1912; 31:qJZh2+cph6LbiSj9P/y4tHhZoBjGz8FSMcOrOvPHwhFqO5FrlyhrVoVJEOO4CbgItHnhJq/4B7F4ht3zaBlt91y+ZXhdpRuj+r9IKIA384NOw7Yv5FJxkv9AG+yXST7CzL6J+HeZPP2QtosWnBMGfgyKnNL2vW31UtBMH/a4piRu9RanOtpEcA7xT0+UTadxB3ht0J3twreKXKM3b73w6K7RdlXq1nP3oehAHuUkipsfT+RZJF02IbrcaS63kS7K1N0iU5kUm4JbvjnvM9lwPQ==; 20:DJGjqGQ9xsn89lhK6LDuaovBzBXniDo6Pj0qIfF34H3ePqT8NPHqVAWgWRNMm8ODl2EY3OtqGHjsjV2p9kJlVLX7ZCDiqmBCqR78s98mlSawzzisgMxYBl84m6YwiPKxX3uKwJ5AwS/W7HF2NfdVX2duDKMxym0EcNgsHkNw5LjqBuwvP97Ty1Qhbtwyi54423BKeKp+AaKyOOWGbOEaQOqgDIEJ0Z0/05RFDO/vKDnRHuMlM+U1jLZ/EXSvf9sPdjaYJ51qA15cpkiM3gJB+WEU+pALCoUR6OKyRX8E09FP+uIiGdxVVeoyiAi4j8VKh3FC3ZekE54warwajXVAuUOTy09SowZJ4B7J5nJa3+4FVIQhP8ZjQt5Uk//GAy2jtAr/ReVpLeImhN98Jh75XYI7SzYU0eCd7t2Hj4ySbDI2aSyt/HG60p/mXijei0maIS4g2tq6ay4BnOPssZBIqFKcDyEntN7h1I6IBzuGDzKEZy0NMDQNyo+SovS7UGla X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(158342451672863)(67672495146484); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(20170203043)(2017020603029)(8121501046)(5005006)(3002001)(10201501046)(6041248)(20161123560025)(20161123562025)(20161123564025)(20161123558025)(20161123555025)(6072148); SRVR:CY4PR15MB1912; BCL:0; PCL:0; RULEID:; SRVR:CY4PR15MB1912; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1912; 4:/lR/zN79IYrHzmbSaRuQmWoSwx++FnhnDNQh6BL7Od9nVwDxhvEk3ZVGShgS9S7OXb7h8R10ng6m8eRkOH6P1yuTRr64IYBr5A3CWpnt36MH+yOOBSk5RvBsT9qwhE0hj/jIMNQ9Un2q7q7sUuwckQe3Ds1lpqK4TZYgyjIagM10Q6yZG2WrG0y+cb9kG4BhjbdcnJz1H7rcTKmr5jBvSP0dy60Fm4KNYyfMgoqdUApstux4hBu22jSDQUKNlYbPnpFi5a1Se+Tk/Y+ezvj3es54THUcbVQOAJ1I8R7sh0Ws3iebZvUJTOvY/DMcIkMa0Vd02WX1jgVR4umafkjHbYnhhx5jF5XO4/Z9kXwfDbwbiy7v1ZiXrRtDtvNzBAvehIcW2m6iCopAsJncnUjX7e42wHv5aQPzYv7kpcKkVLHA20DeXgPBRQ9UH7I8CBQzTMCUg1/pW7LTYO2/DyTSC/50svL38tOVfHsIGqsdgtm+uWRIMNaHNSaWJXGvR+xlReE46P25ijTD0QGHP9J0+2HQGw5CwQQORbnimmcjl89uERQNbKU8eChy+7LsekS5bL3jyQsua4TA+PtbGu+aoVzF+ON6pXuUkHN6LBCay5AmAbWW7OjSL82l5zf1V0fHrmG9FXp2xnnem+YFLI4MADQfXa4doMPvivo6mzWXx5Mysc7p7TWkhh31Dc1icnnbZ+DddyPDwNSbLS272s8JgS3GEX8H1/7+n0stQfvAkXJDGQpnWXsLgx/O8MeQHahC X-Forefront-PRVS: 0211965D06 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(6069001)(7916002)(39450400003)(189002)(199003)(6486002)(50226002)(47776003)(25786008)(189998001)(68736007)(2201001)(86362001)(50986999)(97736004)(5003940100001)(81166006)(81156014)(8676002)(33646002)(76506005)(42186005)(6666003)(50466002)(48376002)(101416001)(106356001)(105586002)(305945005)(7736002)(36756003)(6116002)(5660300001)(2906002)(92566002)(6496005)(53936002)(38730400002); DIR:OUT; SFP:1102; SCL:1; SRVR:CY4PR15MB1912; 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; CY4PR15MB1912; 23:ziNV9JWT+PjB1nll9U7xNMfu9ct1Bu+5sWDSNK71S?= =?us-ascii?Q?DOZPmZP1GLTUbhsY5JVnlBVnRfggj/+yj4Nx8hc0/JpqanPhArDuGBZQ0hDu?= =?us-ascii?Q?N54K9Uko3HztcB4Or5hxRr0Xh/xCIcgsQlkW3IPHNW6fbsTwHDqlfIzAY0nI?= =?us-ascii?Q?rfF25pJZg4A30tDwG44/5PGa+JQVbmtsXnRiub0rYuVUbYjXDqMyGzGktTjK?= =?us-ascii?Q?B8oS1qipS8iYRgN2ADOtvUaVM4gElPo9oWy7sud+9ez1O6mvYoNQC+JnOU3k?= =?us-ascii?Q?yHbUUoPnuE9KpvUgEjXE4NSEmfSio56wdUst2igfaT3l8G0QX6Dn7t0UpuS4?= =?us-ascii?Q?Vyn0eewdYBIGy2yILd/MPvfzaAU7sZcznIw1msrsoK7qDcEpyCltiAgOBXdc?= =?us-ascii?Q?s6vvtbE0cNgjVBeV5oJUqJUICsM7XFgEWkE0yKGn7BwEQhbu/TODvbUkkz7I?= =?us-ascii?Q?ipVphZTh8IHYr1JYG1zewMamb2fEK7EcVFP9lZjg/764T7QvY9VCvgoPQIiL?= =?us-ascii?Q?Vr8JFIoTiPp9AVMNAAsh1Rt3d4ZVdF3C7y/OjY3lh+JR5MjuhPx2cFyQAAdv?= =?us-ascii?Q?VOAFm4po1K9puyCgJFM8qiEXIra/MTL75pr8BfPc2UAZmHTHwi6RJDCUZlSw?= =?us-ascii?Q?742nbUPRZMyCS86APGQ62wOsUaW+gRnA6WCAJkR+Urc2RhPVd3Y+NikgGe5u?= =?us-ascii?Q?fIg8GIEh2jnOLOvj2iSlW+Ac/WIJGpz+mbVirMC3LoIpbDwSBZCfp+haPRTM?= =?us-ascii?Q?DJgKVpwJevvKHdDNuEpvkjsc6pa4AWneM2PBzpleypz1ojmQFn7LquFg/2UJ?= =?us-ascii?Q?SRDpPTvlnMDFiHxM2TyR/Xg+XuyeNcFah/3IZnEj2Q+t4GxVPbSjrlMrnQqn?= =?us-ascii?Q?Tzr+HQPbMO6OIIq9soAwmh5sZMCasxT0x7K6j3xPaxFuyext9uYvN/COFPJv?= =?us-ascii?Q?NoEQAmBv6MPw/DQ0fDv4jb9Bwg6DcJcH5V3wOFB+yQOWS1UvOJfljOJ2j6Vh?= =?us-ascii?Q?7o=3D?= X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1912; 6:7NOBr8rTsmx4ez7Q3QJOhZY55apWQDn4dwd2DwUaVEPBy8gMbpIo5/8VADROkt+B+cMmpCJ1B3fEcbttLCoUaRWuUjoXySn2AToG0V4tba+b+6h/gwjow8gaDYGgn4Mzz1zbDVDbSxqIgyXD/X/H6RWOviRWQIMMCKIK2gZFtIcc1siwr53EgB51lPnhKFYwMnxxUkeoqpmgo9cOCzICY1ILA0BWmEW3deuojVLt4vhG7IbsmW21R4Y8KgmWWH+b3gd6mELn4XMM1+oxL90bVEUT9WMcmpl89bwtLryHmh3j4s9I/D0kUOrB7uqWvO/Ut9k5sHpJjEPF3dSTW0xc/3zwOTfOgfabci3VhhOfbpGEFd+DxhzbbMj2jaT3H1gETpPCR53G2SkvZNVmy4+Zqw==; 5:jP0nPn3LYD882qnpudjBj8nxrmGX9VSlULr7fT3C8+ZTryzaIWNHRYkdXc9fAz/mQDI93jyZ8yMWK6Y14v6k9yldRuv/Oa9x8ZAlqFEFlyUaitRdUkjB87Yazb5+fKckcctrK2YqXbNbCLuMhT6t0Q==; 24:raCAjQJoT/UXB9PKZxBq1p/1qTCDxRSnWx1n/AgtP2zc+FoITfA1pao+HfJlzWMaOatFLBwKsNab3dxepYQ3K73jvNDye4pB1POnIxWLv70= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1912; 7:XpFMCbLRjg81YwPLGjNGbDu8TvzQx8xu7wT/mVBfIQ8BzOCW8OqtIddfvpB4RK3IZ8HGCLLHWm+2BK/+3uNuuve2RaXRM2xXKVtF61frj6OAgt/pOIa0I1qSxKz+Hcbfn6Aowk7i4IFfQbSJ6+pAQhDI++zkcKJjIFpIgHNWIIDcs8E/mRew2pWB6Ww9/2jdVh33BdPkPhXlLQiTYVTX72JujLgVZyQT7XOaKpZk9hpBdoywK9jNjzLCnZbIVsuGdqR0T2g8pgO9OtXTPkxZijE3fMwPQQScUgxAN4ZwmatlZyFq70tV69mqI+5spybDjCi46qi1Zasy+FobShT0zF2hRKVv6DhvCHWPBAMeY1l29DlbfcPAiMHE683hp/5h+7GR4ysTZmz59dk/tCviE+zpi5k0JOch5ZMEMnyHBDO+IX4UH328BE4idwGf7xAQhAT1nbYXLhPjjZ3Tqj9reQYooh9moYQ58UjpWgpIu3iyjxi7MExbAi11u58H98JkKZFj+t9ddRlta3gJDsru0g==; 20:UPdwfirWz4gzkj9bQ9GOr9P7KsGEJupfSA/dT+evn8+nscCtV6CAQAzPCNoYB1Jtt/oQpnHX4+vav2LydYooSyyJ7pBZSnI1YMZmt7tKW7d6fxx3bclbVqB3T2BS5G6jkm6kMmsKl698x20OWcwKvYhHuiwyJeZSn6Nmb0Xvk0Q= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2017 22:10:26.9497 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1912 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-07_12:, , 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 Break the ioctl handling out into helper functions, some of these things are getting pretty big and unwieldy. Signed-off-by: Josef Bacik --- drivers/block/nbd.c | 265 ++++++++++++++++++++++++++-------------------------- 1 file changed, 130 insertions(+), 135 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 164a548..1914ba2 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -99,6 +99,10 @@ static int max_part; static struct workqueue_struct *recv_workqueue; static int part_shift; +static int nbd_dev_dbg_init(struct nbd_device *nbd); +static void nbd_dev_dbg_close(struct nbd_device *nbd); + + static inline struct device *nbd_to_dev(struct nbd_device *nbd) { return disk_to_dev(nbd->disk); @@ -652,10 +656,17 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, return BLK_MQ_RQ_QUEUE_OK; } -static int nbd_add_socket(struct nbd_device *nbd, struct socket *sock) +static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev, + unsigned long arg) { + struct socket *sock; struct nbd_sock **socks; struct nbd_sock *nsock; + int err; + + sock = sockfd_lookup(arg, &err); + if (!sock) + return err; if (!nbd->task_setup) nbd->task_setup = current; @@ -681,26 +692,20 @@ static int nbd_add_socket(struct nbd_device *nbd, struct socket *sock) nsock->sock = sock; socks[nbd->num_connections++] = nsock; + if (max_part) + bdev->bd_invalidated = 1; return 0; } /* Reset all properties of an NBD device */ static void nbd_reset(struct nbd_device *nbd) { - int i; - - for (i = 0; i < nbd->num_connections; i++) - kfree(nbd->socks[i]); - kfree(nbd->socks); - nbd->socks = NULL; nbd->runtime_flags = 0; nbd->blksize = 1024; nbd->bytesize = 0; set_capacity(nbd->disk, 0); nbd->flags = 0; nbd->tag_set.timeout = 0; - nbd->num_connections = 0; - nbd->task_setup = NULL; queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); } @@ -742,74 +747,135 @@ static void send_disconnects(struct nbd_device *nbd) } } -static int nbd_dev_dbg_init(struct nbd_device *nbd); -static void nbd_dev_dbg_close(struct nbd_device *nbd); - -/* Must be called with config_lock held */ -static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, - unsigned int cmd, unsigned long arg) +static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev) { - switch (cmd) { - case NBD_DISCONNECT: { - dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); - if (!nbd->socks) - return -EINVAL; + dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); + if (!nbd->socks) + return -EINVAL; - mutex_unlock(&nbd->config_lock); - fsync_bdev(bdev); - mutex_lock(&nbd->config_lock); + mutex_unlock(&nbd->config_lock); + fsync_bdev(bdev); + mutex_lock(&nbd->config_lock); - /* Check again after getting mutex back. */ - if (!nbd->socks) - return -EINVAL; + /* Check again after getting mutex back. */ + if (!nbd->socks) + return -EINVAL; - if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED, - &nbd->runtime_flags)) - send_disconnects(nbd); - return 0; + if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED, + &nbd->runtime_flags)) + send_disconnects(nbd); + return 0; +} + +static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev) +{ + sock_shutdown(nbd); + nbd_clear_que(nbd); + kill_bdev(bdev); + nbd_bdev_reset(bdev); + /* + * We want to give the run thread a chance to wait for everybody + * to clean up and then do it's own cleanup. + */ + if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) && + nbd->num_connections) { + int i; + + for (i = 0; i < nbd->num_connections; i++) + kfree(nbd->socks[i]); + kfree(nbd->socks); + nbd->socks = NULL; + nbd->num_connections = 0; } + nbd->task_setup = NULL; - case NBD_CLEAR_SOCK: - sock_shutdown(nbd); - nbd_clear_que(nbd); - kill_bdev(bdev); - nbd_bdev_reset(bdev); - /* - * We want to give the run thread a chance to wait for everybody - * to clean up and then do it's own cleanup. - */ - if (!test_bit(NBD_RUNNING, &nbd->runtime_flags)) { - int i; - - for (i = 0; i < nbd->num_connections; i++) - kfree(nbd->socks[i]); - kfree(nbd->socks); - nbd->socks = NULL; - nbd->num_connections = 0; - nbd->task_setup = NULL; - } - return 0; + return 0; +} - case NBD_SET_SOCK: { - int err; - struct socket *sock = sockfd_lookup(arg, &err); +static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev) +{ + struct recv_thread_args *args; + int num_connections = nbd->num_connections; + int error = 0, i; - if (!sock) - return err; + if (nbd->task_recv) + return -EBUSY; + if (!nbd->socks) + return -EINVAL; + if (num_connections > 1 && + !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) { + dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); + error = -EINVAL; + goto out_err; + } - err = nbd_add_socket(nbd, sock); - if (!err && max_part) - bdev->bd_invalidated = 1; + set_bit(NBD_RUNNING, &nbd->runtime_flags); + blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections); + args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL); + if (!args) { + error = -ENOMEM; + goto out_err; + } + nbd->task_recv = current; + mutex_unlock(&nbd->config_lock); - return err; + nbd_parse_flags(nbd, bdev); + + error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); + if (error) { + dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); + goto out_recv; } - case NBD_SET_BLKSIZE: { - loff_t bsize = div_s64(nbd->bytesize, arg); + nbd_size_update(nbd, bdev); - return nbd_size_set(nbd, bdev, arg, bsize); + nbd_dev_dbg_init(nbd); + for (i = 0; i < num_connections; i++) { + sk_set_memalloc(nbd->socks[i]->sock->sk); + atomic_inc(&nbd->recv_threads); + INIT_WORK(&args[i].work, recv_work); + args[i].nbd = nbd; + args[i].index = i; + queue_work(recv_workqueue, &args[i].work); } + wait_event_interruptible(nbd->recv_wq, + atomic_read(&nbd->recv_threads) == 0); + for (i = 0; i < num_connections; i++) + flush_work(&args[i].work); + nbd_dev_dbg_close(nbd); + nbd_size_clear(nbd, bdev); + device_remove_file(disk_to_dev(nbd->disk), &pid_attr); +out_recv: + mutex_lock(&nbd->config_lock); + nbd->task_recv = NULL; +out_err: + clear_bit(NBD_RUNNING, &nbd->runtime_flags); + nbd_clear_sock(nbd, bdev); + + /* user requested, ignore socket errors */ + if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) + error = 0; + if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags)) + error = -ETIMEDOUT; + + nbd_reset(nbd); + return error; +} +/* Must be called with config_lock held */ +static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, + unsigned int cmd, unsigned long arg) +{ + switch (cmd) { + case NBD_DISCONNECT: + return nbd_disconnect(nbd, bdev); + case NBD_CLEAR_SOCK: + return nbd_clear_sock(nbd, bdev); + case NBD_SET_SOCK: + return nbd_add_socket(nbd, bdev, arg); + case NBD_SET_BLKSIZE: + return nbd_size_set(nbd, bdev, arg, + div_s64(nbd->bytesize, arg)); case NBD_SET_SIZE: return nbd_size_set(nbd, bdev, nbd->blksize, div_s64(arg, nbd->blksize)); @@ -824,85 +890,14 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, case NBD_SET_FLAGS: nbd->flags = arg; return 0; - - case NBD_DO_IT: { - struct recv_thread_args *args; - int num_connections = nbd->num_connections; - int error = 0, i; - - if (nbd->task_recv) - return -EBUSY; - if (!nbd->socks) - return -EINVAL; - if (num_connections > 1 && - !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) { - dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); - error = -EINVAL; - goto out_err; - } - - set_bit(NBD_RUNNING, &nbd->runtime_flags); - blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections); - args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL); - if (!args) { - error = -ENOMEM; - goto out_err; - } - nbd->task_recv = current; - mutex_unlock(&nbd->config_lock); - - nbd_parse_flags(nbd, bdev); - - error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); - if (error) { - dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); - goto out_recv; - } - - nbd_size_update(nbd, bdev); - - nbd_dev_dbg_init(nbd); - for (i = 0; i < num_connections; i++) { - sk_set_memalloc(nbd->socks[i]->sock->sk); - atomic_inc(&nbd->recv_threads); - INIT_WORK(&args[i].work, recv_work); - args[i].nbd = nbd; - args[i].index = i; - queue_work(recv_workqueue, &args[i].work); - } - wait_event_interruptible(nbd->recv_wq, - atomic_read(&nbd->recv_threads) == 0); - for (i = 0; i < num_connections; i++) - flush_work(&args[i].work); - nbd_dev_dbg_close(nbd); - nbd_size_clear(nbd, bdev); - device_remove_file(disk_to_dev(nbd->disk), &pid_attr); -out_recv: - mutex_lock(&nbd->config_lock); - nbd->task_recv = NULL; -out_err: - sock_shutdown(nbd); - nbd_clear_que(nbd); - kill_bdev(bdev); - nbd_bdev_reset(bdev); - - /* user requested, ignore socket errors */ - if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) - error = 0; - if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags)) - error = -ETIMEDOUT; - - nbd_reset(nbd); - return error; - } - + case NBD_DO_IT: + return nbd_start_device(nbd, bdev); case NBD_CLEAR_QUE: /* * This is for compatibility only. The queue is always cleared * by NBD_DO_IT or NBD_CLEAR_SOCK. */ return 0; - case NBD_PRINT_DEBUG: /* * For compatibility only, we no longer keep a list of