From patchwork Wed Feb 1 21:11:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9550683 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 B5C4760236 for ; Wed, 1 Feb 2017 21:11:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A7E5D28497 for ; Wed, 1 Feb 2017 21:11:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9B141284B4; Wed, 1 Feb 2017 21:11:56 +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 B7AFD28497 for ; Wed, 1 Feb 2017 21:11:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751263AbdBAVLz (ORCPT ); Wed, 1 Feb 2017 16:11:55 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:58977 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751240AbdBAVLx (ORCPT ); Wed, 1 Feb 2017 16:11:53 -0500 Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v11L8gq5010881; Wed, 1 Feb 2017 13:11:52 -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=j1MnjsHfrHmM5RA/i2npbGIsPTY4/st5C1O1cpPecz0=; b=Y5OmEkt73E6LldhL+cT9KARIkv4mPnYzmPi1JPcFZ805u1bujVEwJLQ3gcRiG9n/uz3X aIIIpPXO/Jh1B4J6Nx13QjQJiHHoYxShIYjxA0+uQMT2w/CF0njZT453lvCKguoOE8kp hj+nWHyehlJpr4YORNNd/aKCemOoP7CYV4o= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 28bj4n9259-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 01 Feb 2017 13:11:51 -0800 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.25) with Microsoft SMTP Server (TLS) id 14.3.294.0; Wed, 1 Feb 2017 16:11:51 -0500 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=j1MnjsHfrHmM5RA/i2npbGIsPTY4/st5C1O1cpPecz0=; b=VTQwRsEX0uqI9ErwgGFQtixc2aRc0H++jMCFN6vvHf1q2XwkwltwJcpyJUh97taPv2pKli3kst2MAQBb/wf/EhORiLkH6BFl6I97Uq492a0Pf6E1Q0qTd7NxuxNRCsCdYm75v4wD7LmLrQceo/owVer3LKyDYV4DtAe5WzAsrjo= Received: from localhost (2606:a000:4381:1201:225:22ff:feb3:e51a) by DM5PR15MB1321.namprd15.prod.outlook.com (10.173.210.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.874.12; Wed, 1 Feb 2017 21:11:49 +0000 From: Josef Bacik To: , , Subject: [PATCH] nbd: use an idr to keep track of nbd devices Date: Wed, 1 Feb 2017 16:11:40 -0500 Message-ID: <1485983500-3830-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: DM5PR13CA0006.namprd13.prod.outlook.com (10.168.112.16) To DM5PR15MB1321.namprd15.prod.outlook.com (10.173.210.11) X-MS-Office365-Filtering-Correlation-Id: 0814dfa4-7b4f-4750-54c3-08d44ae6efd3 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:DM5PR15MB1321; X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1321; 3:kObnfTP1yqGoQW1oFUnXdw64AwXNk/c3kJHLPvaQhfSCw8FNWVAJ22zwVd1HAUNBpWdcHEDlZRbMYov37sFvTuwcPrfBjOvK+/thfoZQ6cwhzmVKdS9ycWA8tWzqQ7jf6BB+474ePpbRmDmDyVzOUkdpIe6K6ky2PzOfbgxZNwbjbX6S3XVq1VCq+kybklvLmEu+Ill8EdvysmyigurXDIwT2zT/vo/kViWS0xL9OAFj2EVp58z0e1rNa6vz8eM4s5byTumuUfRth0eH3OeBog== X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1321; 25:b/QxIWa2Fx/EgMEJ0Z6SWgTPrTOAVPfLSiFQyf0KIQNjVI617zTCV015Kzd9VIiBF7BstTWL6V1a1U5faqV+G5whspMkFhNBgQrsWBvqKKUfXQ6QdMbh92wkGAkIg/hny35o3g/pObgo2JfcCRp5ooMbre3wDJkLOxs6QTwmFdU7KYmAashbM8zD44KpQxkTTcHFgpzxtMU1ZXZMSduzMbA3j3iwL3LF5zYX0urei6DPuaeQDPc9x4m3ZyTLyktgx1fKnsaD5QaojrbHxvh7PCoc4TBhEwwA6ty5S3yvJT+SkjlFoQFLHGuAjFiFU5poHP57++SG9fUjeWetRCxqshXSgi64aVStdzBXnjFuYgIXRYn6nW3UBSToijy+NR8FjB7fjbNoz/X+v5A6BLQx05vaNuNTBpY/3AWbWRuRUkonn1YVkdvsaOuJdHHUgN9hdwvFy+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Wj 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)(20161123564025)(20161123560025)(20161123555025)(20161123562025)(20161123558025)(6072148); SRVR:DM5PR15MB1321; BCL:0; PCL:0; RULEID:; SRVR:DM5PR15MB1321; X-Microsoft-Exchange-Diagnostics: 1; DM5PR15MB1321; 4:bIq1RQXJbPPNvqBuPd90gc0hREEKyXXeYRX9URa/rAfep1EBOzLN1XraOaTMT2nuWkyFeWMTozmjUxIKRVhRaK3nV3ebfjCjyfe11eFSCkDOn3NzkSTQRhXQZeFskZ4oH4E4cHEcgvsUmkmHUKyVKOq0ymT/uO8m5QhusOsOTixQ7rDC5o7PstA+KA4PNNVdeamhN/xKFpcALUIml+cWkbTxiNFaNtYIJ+UE4GXHSHKwJLwWvEhvTh2OM8Mdt06jjAzkmeVcAoATqWRnqKen8FdJ/XiGxmocLoprF0jKs5nl7MdoXYTm99+F8SDHPNmHKsUmwlnahwmAg0BVNimbz+mIo5XyWdQQIoUT2rljNLq/z+J1nsx6f5gdkV8vOtXU+jgJN8hvQKOYhikReN10Zc5HtjbFwaTRzxHUpi3Y/QpKB7ZSAjQGQvf7Xbzx/KW4MZpPF1KDKojQdjuynOyBzAYfxdft3HcOcIKLhC3KNpwixhx2aF01KahunlqG/LsnhO8JGYnIXLqWhP6et2WLfHFKddBvMewzURQ7XpmSa4rQ/6UMy7RjDted6DpyTVcjPHlwn5Tf4w2/BynB+wbui1R4d5trRA3KgpizXQ5twOD011tAgBQOqdqMWP4sCMBUNm8NxQ92/qFYgEcumAZR9Q== X-Forefront-PRVS: 0205EDCD76 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6069001)(6009001)(7916002)(39410400002)(39830400002)(39450400003)(189002)(199003)(101416001)(6116002)(5660300001)(105586002)(76506005)(42186005)(106356001)(2906002)(86362001)(68736007)(53936002)(7736002)(25786008)(50986999)(6666003)(36756003)(92566002)(2201001)(6636002)(5001770100001)(81166006)(305945005)(189998001)(38730400001)(6486002)(33646002)(5003940100001)(50226002)(107886002)(81156014)(97736004)(48376002)(8676002)(50466002)(47776003)(6496005); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR15MB1321; 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; DM5PR15MB1321; 23:tQtPEMcCv28ZdULbqgceDiCDmuRafuCMIWyvcTAB/?= =?us-ascii?Q?FtgCARdtt26N8yvM+l88zQp0zqbnD7rG6YA04vfEPrk1xjDDqKy1iYXa2ruD?= =?us-ascii?Q?G+EVQHxFmi9k1N+NbxIokqt2rmDUJPqLYe0VzbF47YzfXWWY78wQP3YNPUmX?= =?us-ascii?Q?fLknNlZ5OZdXM7bXk+cS6WbtZMpoJow3goa1d0+s7KOrBDmTEyGrmGDhZUGA?= =?us-ascii?Q?XKZd6txtuWofsEX+AvqWbwqZ+NefDFXy3wOmcysxYbcFT6GFQQmNR4BijsTS?= =?us-ascii?Q?NyLXRJjknzUMBY3SXQWiwjW3pKoDiAEYOt8m3ELktd2MWGAdt2QD3ZQu/urV?= =?us-ascii?Q?w5uGmKIPN9kf8Acqc8DrRvFQ2w9MTswOn7Y4kh6oWtreMGW9/GpDktztONNg?= =?us-ascii?Q?4NXmvqSx+iYlrfEeLyhGPNkiP+XSgWj8tHkbGz3+tCODZdQlIXxBnb8s48ex?= =?us-ascii?Q?3AwtAmOkpWCV0CNR7uVLB6f5vJJohSj48DmhW/0z5CFGGUd+ag7dY8Gl7w7P?= =?us-ascii?Q?R+aXKghC8cZO/HtNNfecq4IaZ9jVZldEFRvD0mqDevndYbumg7cF2d+3/DnJ?= =?us-ascii?Q?BM58XlTF1OV4xea5Vy0yoIOinul815fB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dP27qOOwX70kN7SPKpUn/9j4tBamxZ1iXd1zuIj6V0c95Y3sJxX+bHAXGut0aB0OdoKTasaLBmPg5pebZLpk= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Feb 2017 21:11:49.1539 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR15MB1321 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-01_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 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 | 213 ++++++++++++++++++++++++++++------------------------ 1 file changed, 115 insertions(+), 98 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 9fe9763..96f7681 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; @@ -89,9 +92,9 @@ 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 struct workqueue_struct *recv_workqueue; +static int part_shift; static inline struct device *nbd_to_dev(struct nbd_device *nbd) { @@ -997,6 +1000,103 @@ 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); + } + kfree(nbd); +} + +static int nbd_dev_add(int index) +{ + struct nbd_device *nbd; + struct gendisk *disk; + struct request_queue *q; + 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; + + q = blk_mq_init_queue(&nbd->tag_set); + if (IS_ERR(q)) { + err = PTR_ERR(q); + goto out_free_tags; + } + disk->queue = q; + + /* + * 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_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 :-) @@ -1004,9 +1104,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); @@ -1040,114 +1138,33 @@ static int __init nbd_init(void) if (!recv_workqueue) return -ENOMEM; - nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL); - if (!nbd_dev) { - destroy_workqueue(recv_workqueue); - return -ENOMEM; - } - - for (i = 0; i < nbds_max; i++) { - struct request_queue *q; - 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. - */ - q = blk_mq_init_queue(&nbd_dev[i].tag_set); - if (IS_ERR(q)) { - blk_mq_free_tag_set(&nbd_dev[i].tag_set); - put_disk(disk); - goto out; - } - disk->queue = q; - - /* - * 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); - } - kfree(nbd_dev); - destroy_workqueue(recv_workqueue); - 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); - } - } + idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL); + idr_destroy(&nbd_index_idr); destroy_workqueue(recv_workqueue); unregister_blkdev(NBD_MAJOR, "nbd"); - kfree(nbd_dev); - printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR); } module_init(nbd_init);