From patchwork Fri Jan 13 19:04:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9516285 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 E07E06077E for ; Fri, 13 Jan 2017 19:21:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CA7E728766 for ; Fri, 13 Jan 2017 19:21:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BF75B2876D; Fri, 13 Jan 2017 19:21:45 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 EEA7828766 for ; Fri, 13 Jan 2017 19:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751028AbdAMTVo (ORCPT ); Fri, 13 Jan 2017 14:21:44 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:38214 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750978AbdAMTVm (ORCPT ); Fri, 13 Jan 2017 14:21:42 -0500 Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id v0DJ2M59002766 for ; Fri, 13 Jan 2017 11:04:57 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : subject : date : message-id : mime-version : content-type; s=facebook; bh=CO7NHHxyP8grgFWz3Ty+VsTZNHr3BuCse4VBX+ihqxI=; b=KCkAh5Bg37JLRdfI8e5x1zYhTlbgpjLnThbwCuevFbdE0dPTbXF6KVe9YoRqFPSzmLZy 2EZP2XSVqEZ/TlcJHofuGrugSEuI/3M25bLGJ7iPo8RXd+IDrNVhwMziE5RdCu511jti nRJc7O6CIa+X6M/0mDj8g6IDE2PzT8p1H24= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 27y39fga4c-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Fri, 13 Jan 2017 11:04:57 -0800 Received: from NAM01-BY2-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.23) with Microsoft SMTP Server (TLS) id 14.3.294.0; Fri, 13 Jan 2017 11:04:57 -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=2zJuQcr5JTa8uespU9593lBq91XVomN5I6mAqEVC8qI=; b=HJpyS0v2DTyxdj9MYMprNi1q90m0ISuA1FuTU1LgK0EzPsTH/NIP569Z1zdGygLCQflNOSNrlsCXShPh0869CrBjhypj5EzG7IobkmAf4bdCGYh2OW8RnnlBLcuNkdVDf8MJ9aGallPDlv3SOQ7uvqXpqqiE1KoFz+ZOjs2G6Qk= Received: from localhost (2606:a000:4381:1201:6e88:14ff:fe1b:6164) by DM5PR15MB1322.namprd15.prod.outlook.com (10.173.210.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.771.8; Fri, 13 Jan 2017 19:04:55 +0000 From: Josef Bacik To: , Subject: [PATCH] nbd: use an idr to keep track of nbd devices Date: Fri, 13 Jan 2017 14:04:52 -0500 Message-ID: <1484334292-29933-1-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 2.5.5 MIME-Version: 1.0 X-Originating-IP: [2606:a000:4381:1201:6e88:14ff:fe1b:6164] X-ClientProxiedBy: DM5PR17CA0009.namprd17.prod.outlook.com (10.168.112.147) To DM5PR15MB1322.namprd15.prod.outlook.com (10.173.210.12) X-MS-Office365-Filtering-Correlation-Id: b5031fea-5469-4a8f-bf67-08d43be70fc4 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:DM5PR15MB1322; X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1322; 3:AhKsK1TWoyTN/6l4+9h9NFds7j9KtpR8cOGGVQhGqZVniOS9S1bflBuUIIiD+nLmyb9Nbdj3yLKVXxDMoIr2hxd4IlaTBxDyQ1/hBRqyEc+By9218iEnFXesrp+YU0Tc2XZghD+qeqR+caabry8AZuWcwXQ5V6g6mR5WzNIMcRXToESp+oYqExR9ANUqHF89ga+Db+PZXOjB6JpZEeIkueV08futusTY2VjA19x4d9XYVchkYuLtE9tvMdkbuTKe0Fy2W/pIrc4EStMMlRtMMw==; 25:5JumMZGwKWHTxAcg1EAWazn+uf7ridYCTMCKc0u6nw3WM39d9zyLnLL3OYyGBDQdtgAvtk3fKskqLI66UlKgYSJfEdPIDwKkhyqL37entvgF4fX3L11FHNwY4su3PmLYihY+2ghWgdjFbsDR1RfDEB8m6c0dcf+Ew4+QQHkLTfeoUKxws0oYRR9uWSv8temB6II3csGDp3LXr41ia3p+5cP03iV0MxubB5VfgzrY4Psl4zU3oyMvVP0yy/OVxFMCM4I7pGA3z+rFaSpYeuWZpu15uvQcDFmWShAg4j7Cabin02XVfZ62Cc7dfMOQ80b0gRkRt6eqb8Zj7zc2QStih1cpl9WsIuYB0hDoN1FhdQ4MD9keX7DhgFHJxUxirZfTHKXGXaFUrk3AmcA/pMPOC9DwfQddUTh4nOcIjJnHMtaveIbhu09PMIld+FlY6qQazCvFESFTKFzwrk5jnl2CbQ== X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1322; 31:1w3AhYevMYdgxmCwvAOnt0RrAtsgmMEFN3YTGeqkzpG8nuq2K+Cf38Z+YtqRcFLxf8p9dtZKYbsex69l3gNiEyCD56zHlIOKtDlEVceCq7doIZHVo76iQavu4l3fG7neByatXC5DRqpuOnno4tnXle9sMXJ7MDC30yZCGp7/BjAnRueJE8cX8n0TbLZdVRzBkpQUyM3jARaXJKNo0COKBgvtb4lotSclo1mKjL62CTjMUfnaRd++3uD7u8yMKkeq; 20:kuX5bEqRBS5SqZrwQyJiaUju4DJuZpPoyER6pMo067/hhAspX7AMdBWJUFDJ4p60+CQA5ozwdw3+37joLqc5+1F4T0jo1cV1S5fzOcVMiIbqb/yok3DI/8rbGfuEpKy6nuOlAzCRy8bq0VVo2McToOIr97FfsFyEU6VTTxjbuEYnZC0Uq3WsMOa+dqKb/OzK8lR3LZAHcelXHH/nXlQpl5UXk/c7HwgUjf/gQCYgRP9xgCYx9SowUY/YYDwzdsvHtutgfSfQ6Y11KCB4I7anEbuIYnDmQBZ8nw87UvwRXcsPqhi63Drjv9byVB+wHdp10/hg2vpwo8Es7elEcBHDibR7UFhX5pM5TJjKdlHBvxhS1byTtOOTrixwb1BpC0CngprOf3CW2JDPBZuOLtoYWW685j0tgP5K/qpmo5CimprsWBtrpXni/IXqKTCLdOdgYv+CeRdlLs7JOh8qLn5IUWyIG8rRe/Lw0aVIYSPMCPQPRTochFASxLTVUcN/CNd3 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)(20161123555025)(20161123560025)(20161123562025)(20161123564025)(6072148); SRVR:DM5PR15MB1322; BCL:0; PCL:0; RULEID:; SRVR:DM5PR15MB1322; X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1322; 4:AL1GmHBmjmNBc90YGnqCnzcdqhwLrm66JJZUUbGuzC2FjVelG3UOJmCv+ndp4fZagxeTJBTNC2UBUMBJjvw0NxbYN2ox7FaD16cOFEmSI6LaqfAPgX2XJnr4XUNUkFsEPTRPntApqbnFZWaE+dY6oGGpvJUiW4IzYaDOzBl0JI/96W8axKdroxgyys9yhyhhqGCxtMXSWRvaq39lpAarYC5ey3F9omM2n9rJyXYASbP35AI/8K+8UB4RIlTkeUrQ+BOl+BWhTlpKyeRUAQkMTGDRJmcJ1QSK5YQ2GYCvuMdFImTB/F4wsu9YF52Dshjr/dBDjMCvXIjrVb0Z0Op70YniTF6v00Aelb697Uy+bG3clxmAm0gtJTvzE+mR/QwhdG7WbOBOw+nl0Kaps4g7cFaHwlP2BTWSJapn49Uhgj08gtBi0UFpt5N8L3NKYPRrwVaN+aj9kIqspnwtK//oWCrIc+cencORHjUiN5MqJjzguLfUXHZxEc9/JnsxmDUe4X5/WVfEttbmbHRx9mM6E94zLYy626su1+ywMIXNN3BXmRRCLNMk0Kd3TVfSjUapcJpe220wrMhI6wXAFCOiNcKeHgt/FffM0s4/DyM1+nLkVCVVAl7MAMsU97mp+PgQ X-Forefront-PRVS: 018632C080 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(6069001)(7916002)(39830400002)(39410400002)(39450400003)(189002)(51234002)(199003)(86362001)(575784001)(33646002)(7736002)(106356001)(50226002)(105586002)(42186005)(101416001)(47776003)(189998001)(68736007)(81156014)(36756003)(81166006)(8676002)(27001)(23001)(16001)(26001)(25001)(13001)(14001)(24001)(12001)(22001)(15001)(50986999)(6116002)(2906002)(305945005)(25786008)(50466002)(6666003)(38730400001)(6486002)(450100001)(5001770100001)(97736004)(92566002)(6496003)(76506005)(6636002)(5003940100001)(5660300001)(107886002)(48376002); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR15MB1322; 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 hos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qp9WL3iZyrWr/OBoz9EBxXhdrNENPgz81IjaSZ32yacRjlzUYiIYvqp4NcXi48Bjzjt+Sm9cUg3RpJDwB4EVao7g7K/+TsqSn7a48u90GM6KYzXvKhl+j/2/Ctz5CY2KM8lEJS259SB6+fMlcdk=; 24:4y4jEEwGhI4z0lhXWajeBuji5xD/41Y2Jo35TjrD8BFsS62p2Whyu/7kaYkiTWAXdZi/5+0XqSlt3P2Gb8/7rMq/tI/K4fhsAwxuO3BXPUM= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1322; 7:rGP4g8MQuSEQOaYhvQaTX+taEl7XPidHnbj+vA3QrpdloMi6bubxzisLaARAe2t0jl7aUPK2aQ4OivGYpRjM95b92STgZRsQg6wGXyS3XzZOc79u+oQ9W9LE25sYs3Cqt6kP7acbgOLZ1KqnhxRIiqWy6wy6q6XutD2cewZ0Rp3vHPOuBXW2s8lmU37kRtjIP3MhSWIkDGSoEvja5Js2N1lYtslu/qAfbBPNXZfck3wXD8r8ufGQp8wiOgERfn+AR8gouy8ccCPa4xgmKVhKBIRWvSNEES6Vs3dN4StvgKWAjWEi84E0dGogQtaK/esgSBwnZ+QU+Shvh5SRR1joaRFW9QzCR6ioCQxXXcQ9RflUGzcIYK2dskLY26CcMLz821OaTBh3D93AI1stUHGBwp5oE/u8e+uSv3aalWHjGkCaaXEehxw6GRI1GHTz1ZcKB9ibXj/ytXD+ALhA+keXLQ==; 20:v7LvWUY3YOLQZ/GFjw7Fms6qt3uEYt+SLJ+AyqXLQa4WvyLuF+AvH6Qb5BBzwVn2AfDLCMqGUBkEE5ImbHQegIHpGMuo+rrWv/tYC8oFAKOADHpLPMD6DeHol9rfMmKDS3RHwqghSStD8QVmWBIqPAcI9qDurkSFZq+wnMR4X+o= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jan 2017 19:04:55.2087 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR15MB1322 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-01-13_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 To prepare for dynamically adding new nbd devices to the system switch from using an array for the nbd devices and instead use an idr. This copies what loop does for keeping track of its devices. Signed-off-by: Josef Bacik --- drivers/block/nbd.c | 226 ++++++++++++++++++++++++++++------------------------ 1 file changed, 121 insertions(+), 105 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index e0a8d51..454f770 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -41,6 +41,9 @@ #include +static DEFINE_IDR(nbd_index_idr); +static DEFINE_MUTEX(nbd_index_mutex); + struct nbd_sock { struct socket *sock; struct mutex tx_lock; @@ -90,8 +93,8 @@ static struct dentry *nbd_dbg_dir; #define NBD_MAGIC 0x68797548 static unsigned int nbds_max = 16; -static struct nbd_device *nbd_dev; static int max_part; +static int part_shift; static inline struct device *nbd_to_dev(struct nbd_device *nbd) { @@ -999,6 +1002,108 @@ static struct blk_mq_ops nbd_mq_ops = { .timeout = nbd_xmit_timeout, }; +static void nbd_dev_remove(struct nbd_device *nbd) +{ + struct gendisk *disk = nbd->disk; + nbd->magic = 0; + if (disk) { + del_gendisk(disk); + blk_cleanup_queue(disk->queue); + blk_mq_free_tag_set(&nbd->tag_set); + put_disk(disk); + destroy_workqueue(nbd->recv_workqueue); + } + kfree(nbd); +} + +static int nbd_dev_add(int index) +{ + struct nbd_device *nbd; + struct gendisk *disk; + int err = -ENOMEM; + + nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL); + if (!nbd) + goto out; + + disk = alloc_disk(1 << part_shift); + if (!disk) + goto out_free_nbd; + + if (index >= 0) { + err = idr_alloc(&nbd_index_idr, nbd, index, index + 1, + GFP_KERNEL); + if (err == -ENOSPC) + err = -EEXIST; + } else { + err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL); + if (err >= 0) + index = err; + } + if (err < 0) + goto out_free_disk; + + nbd->disk = disk; + nbd->tag_set.ops = &nbd_mq_ops; + nbd->tag_set.nr_hw_queues = 1; + nbd->tag_set.queue_depth = 128; + nbd->tag_set.numa_node = NUMA_NO_NODE; + nbd->tag_set.cmd_size = sizeof(struct nbd_cmd); + nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | + BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING; + nbd->tag_set.driver_data = nbd; + + err = blk_mq_alloc_tag_set(&nbd->tag_set); + if (err) + goto out_free_idr; + + err = -ENOMEM; + disk->queue = blk_mq_init_queue(&nbd->tag_set); + if (!disk->queue) + goto out_free_tags; + nbd->recv_workqueue = + alloc_workqueue("knbd-recv", + WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); + if (!nbd->recv_workqueue) + goto out_free_queue; + + /* + * Tell the block layer that we are not a rotational device + */ + queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); + queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); + disk->queue->limits.discard_granularity = 512; + blk_queue_max_discard_sectors(disk->queue, UINT_MAX); + disk->queue->limits.discard_zeroes_data = 0; + blk_queue_max_hw_sectors(disk->queue, 65536); + disk->queue->limits.max_sectors = 256; + + nbd->magic = NBD_MAGIC; + mutex_init(&nbd->config_lock); + disk->major = NBD_MAJOR; + disk->first_minor = index << part_shift; + disk->fops = &nbd_fops; + disk->private_data = nbd; + sprintf(disk->disk_name, "nbd%d", index); + init_waitqueue_head(&nbd->recv_wq); + nbd_reset(nbd); + add_disk(disk); + return index; + +out_free_queue: + blk_cleanup_queue(disk->queue); +out_free_tags: + blk_mq_free_tag_set(&nbd->tag_set); +out_free_idr: + idr_remove(&nbd_index_idr, index); +out_free_disk: + put_disk(disk); +out_free_nbd: + kfree(nbd); +out: + return err; +} + /* * And here should be modules and kernel interface * (Just smiley confuses emacs :-) @@ -1006,9 +1111,7 @@ static struct blk_mq_ops nbd_mq_ops = { static int __init nbd_init(void) { - int err = -ENOMEM; int i; - int part_shift; BUILD_BUG_ON(sizeof(struct nbd_request) != 28); @@ -1038,120 +1141,33 @@ static int __init nbd_init(void) if (nbds_max > 1UL << (MINORBITS - part_shift)) return -EINVAL; - nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL); - if (!nbd_dev) - return -ENOMEM; - - for (i = 0; i < nbds_max; i++) { - struct gendisk *disk = alloc_disk(1 << part_shift); - if (!disk) - goto out; - nbd_dev[i].disk = disk; - - nbd_dev[i].tag_set.ops = &nbd_mq_ops; - nbd_dev[i].tag_set.nr_hw_queues = 1; - nbd_dev[i].tag_set.queue_depth = 128; - nbd_dev[i].tag_set.numa_node = NUMA_NO_NODE; - nbd_dev[i].tag_set.cmd_size = sizeof(struct nbd_cmd); - nbd_dev[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE | - BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING; - nbd_dev[i].tag_set.driver_data = &nbd_dev[i]; - - err = blk_mq_alloc_tag_set(&nbd_dev[i].tag_set); - if (err) { - put_disk(disk); - goto out; - } - - /* - * The new linux 2.5 block layer implementation requires - * every gendisk to have its very own request_queue struct. - * These structs are big so we dynamically allocate them. - */ - disk->queue = blk_mq_init_queue(&nbd_dev[i].tag_set); - if (!disk->queue) { - blk_mq_free_tag_set(&nbd_dev[i].tag_set); - put_disk(disk); - goto out; - } - - nbd_dev[i].recv_workqueue = - alloc_workqueue("knbd-recv", - WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); - if (!nbd_dev[i].recv_workqueue) { - blk_mq_free_tag_set(&nbd_dev[i].tag_set); - blk_cleanup_queue(disk->queue); - put_disk(disk); - goto out; - } - - /* - * Tell the block layer that we are not a rotational device - */ - queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); - queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); - disk->queue->limits.discard_granularity = 512; - blk_queue_max_discard_sectors(disk->queue, UINT_MAX); - disk->queue->limits.discard_zeroes_data = 0; - blk_queue_max_hw_sectors(disk->queue, 65536); - disk->queue->limits.max_sectors = 256; - } - - if (register_blkdev(NBD_MAJOR, "nbd")) { - err = -EIO; - goto out; - } - - printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); + if (register_blkdev(NBD_MAJOR, "nbd")) + return -EIO; nbd_dbg_init(); - for (i = 0; i < nbds_max; i++) { - struct gendisk *disk = nbd_dev[i].disk; - nbd_dev[i].magic = NBD_MAGIC; - mutex_init(&nbd_dev[i].config_lock); - disk->major = NBD_MAJOR; - disk->first_minor = i << part_shift; - disk->fops = &nbd_fops; - disk->private_data = &nbd_dev[i]; - sprintf(disk->disk_name, "nbd%d", i); - init_waitqueue_head(&nbd_dev[i].recv_wq); - nbd_reset(&nbd_dev[i]); - add_disk(disk); - } + mutex_lock(&nbd_index_mutex); + for (i = 0; i < nbds_max; i++) + nbd_dev_add(i); + mutex_unlock(&nbd_index_mutex); + return 0; +} +static int nbd_exit_cb(int id, void *ptr, void *data) +{ + struct nbd_device *nbd = ptr; + nbd_dev_remove(nbd); return 0; -out: - while (i--) { - blk_mq_free_tag_set(&nbd_dev[i].tag_set); - blk_cleanup_queue(nbd_dev[i].disk->queue); - put_disk(nbd_dev[i].disk); - destroy_workqueue(nbd_dev[i].recv_workqueue); - } - kfree(nbd_dev); - return err; } static void __exit nbd_cleanup(void) { - int i; - nbd_dbg_close(); - for (i = 0; i < nbds_max; i++) { - struct gendisk *disk = nbd_dev[i].disk; - nbd_dev[i].magic = 0; - if (disk) { - del_gendisk(disk); - blk_cleanup_queue(disk->queue); - blk_mq_free_tag_set(&nbd_dev[i].tag_set); - put_disk(disk); - destroy_workqueue(nbd_dev[i].recv_workqueue); - } - } + idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL); + idr_destroy(&nbd_index_idr); + unregister_blkdev(NBD_MAJOR, "nbd"); - kfree(nbd_dev); - printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR); } module_init(nbd_init);