From patchwork Fri Jan 20 21:56:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9529567 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 811DD60459 for ; Fri, 20 Jan 2017 21:57:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 818AE286D6 for ; Fri, 20 Jan 2017 21:57:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7687B286DA; Fri, 20 Jan 2017 21:57:49 +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 C4FBD286D9 for ; Fri, 20 Jan 2017 21:57:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751146AbdATV5o (ORCPT ); Fri, 20 Jan 2017 16:57:44 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:46916 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752239AbdATV5U (ORCPT ); Fri, 20 Jan 2017 16:57:20 -0500 Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v0KLqYDH015416; Fri, 20 Jan 2017 13:57:10 -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=j1MnjsHfrHmM5RA/i2npbGIsPTY4/st5C1O1cpPecz0=; b=HvKUwXPXOcMNZWmXautzsobzwCdwlYdVyT4N1UAU8yUExqmd4V6XbSJF9wRWaWdl+yRw vKbsFxbgHYqMAMHftVWbotXpI/6Yz9eqxc26q1OhjIyHYXOcwHIT2ksrjebgDGhLK5Ho 8qX2j5NINNLSguowEEvL9mCtOjkWq9aOQn4= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 283m2gh9an-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 20 Jan 2017 13:57:10 -0800 Received: from NAM02-SN1-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, 20 Jan 2017 13:57:09 -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=j1MnjsHfrHmM5RA/i2npbGIsPTY4/st5C1O1cpPecz0=; b=OE1u16jjejMNLet6Xgo4OHT4r3+HVhdijnbxjy9g8Dj+xVYxIfsyG13IvxHtMQXTvo6m42pSQVmqoXG6ZRGdlA4SWavzGDdR4EdI02x92LY0xu9Ap5LBVSXjlxDYfZDsr9eCB/4EsbER7NJXrGVFpwmmlBq8dNtK+HopxZ6kkhk= Received: from localhost (2606:a000:4381:1201:225:22ff:feb3:e51a) by MWHPR15MB1327.namprd15.prod.outlook.com (10.175.3.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.845.12; Fri, 20 Jan 2017 21:57:07 +0000 From: Josef Bacik To: , , , , , Subject: [PATCH 3/4] nbd: use an idr to keep track of nbd devices Date: Fri, 20 Jan 2017 16:56:51 -0500 Message-ID: <1484949412-6903-3-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484949412-6903-1-git-send-email-jbacik@fb.com> References: <1484949412-6903-1-git-send-email-jbacik@fb.com> MIME-Version: 1.0 X-Originating-IP: [2606:a000:4381:1201:225:22ff:feb3:e51a] X-ClientProxiedBy: MWHPR11CA0040.namprd11.prod.outlook.com (10.175.56.154) To MWHPR15MB1327.namprd15.prod.outlook.com (10.175.3.141) X-MS-Office365-Filtering-Correlation-Id: 568ecb72-9275-477e-3796-08d4417f471b X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:MWHPR15MB1327; X-Microsoft-Exchange-Diagnostics: 1; MWHPR15MB1327; 3:7WqWsK3pdnO4L88MTbGCImYk20jfrLZb/NgZJWi86HsAE1SgJquNmnXhKyDdccZbY0UmJJsQ4byGcSex4JvUCehr7bh3qJiXzzymLnODC+wOQTny7CQ5eNlG474RrRWFqm/j4Gz3agUZ7aoN6oowAM+u7zjH+wRFtu1l7YMTc1TrYFhPdX0V42mJPwtq2+BrEbnWfEIR75bOZzbzeya0GBkksdeZLRYDRrSyH1iA9+/bkqSVlrX/WILILLNDX6HXRNRacSymqmoVaqlZGZ/QQg== X-Microsoft-Exchange-Diagnostics: 1; MWHPR15MB1327; 25:zrBpjLolM6PNTJz8HP3Lwxp/4E+6AqKSiODvhS21lnc6XPz7DHCwa2mhgB6EzLgrhLNgDvRmogND1FALmIMVcqCEx5nHuhXOT81Jnp9D7yCGbkqFTHTX3GsLnt95NA7sUEuKe26TQFLamzn0AwVZCBON6ganwUxL9mEhA1OBfHh+nOPWTsUy22VG88z+PjrTvLdstJAtrDuGiWENObDlR1tA4evdqIJCu9BfWbrNWqf4TIWZnDk7toYarHr3GD2kUrszSaKl8HAafE8YIMdNtC61hGpJdgQ93SE1W1OHWBuUhb/4Y1wtKX2GpHGm0TO3AHdzFio63qeIpy8rQqcxLxB7XLeoQLq4kuY235ovsd6lhD45l+ns/YGMI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ytsovSu9to8Y40lApMSDEbKi3fJfoyZuwGuSxDeDuQ9tIzqu+zPu06vBq0+rvycJT8JyVsZs9nG1u4wVimmai0cVAasckzTrno+HUAJpue2LkHCyyVWwRm7IsgqQzNLXJEOk8M9O3aWMPXof117BVGUHVifymI1TTHpsD9/er296jo 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)(5005006)(8121501046)(3002001)(10201501046)(6041248)(20161123560025)(20161123564025)(20161123555025)(20161123558021)(20161123562025)(6072148); SRVR:MWHPR15MB1327; BCL:0; PCL:0; RULEID:; SRVR:MWHPR15MB1327; X-Microsoft-Exchange-Diagnostics: 1; MWHPR15MB1327; 4:R78AfXX1nsEfeVexiUE+28lXo1t0cJX7SoHluoFcFqthwdcr0VKe53fsPUeysiKuMixyjkrAcQK2Pp3c9mGowg7QlGR15Gfpupj2ARNG8g1EYfknMbpMthu6hEzHM+Um+v6a4Ag/wPvzPBuSuTSOAYQ81wRJ6NU5y1VaX3yJVSL8XOedv5BRJugE5BU6knZviwvdbPMjZwjFoSvWVe/3E6wbgjLhwLSQmuBCq3SbLiX8NIaRBlIHmE1SKxWbodIGyhRdoFWoe/0bkCEfOUcfeRTpeyg+mSm4ow/L9gJgdFEB0xQu3H+MWXDcDcfH7ZO1NM1Cym/KrIkdMWMko1XBk1qroDF5StEblHvzuwm7F37H/DifMwnANw2h77O4weiweAMhGLlJQltS3fuWnOujc+J33jtqmdCxI1S66sCBEYIcDzZwtCFQs24cqRbMEQ4umrihi+Dz+Dd1QLnL1SUJIXbdIjLYoZOdotlB3pHKZ8/GBDNQwBhTZZusFdkjbbBO1q/n95gCldHYSNzXRudMQt681Et9wruGcV4N2HMfXsXGruRL0AimJcV8ps6I95M87hY+Hs2fAX4JuPHG+O+ZLU8+vqN483fn0zObgAvG9kPk7r06pPhnuJEiJGtJkj1ODXOSsPzGQGUWMRJ6osPKaA== X-Forefront-PRVS: 01930B2BA8 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(6069001)(7916002)(39450400003)(39410400002)(39830400002)(199003)(189002)(6116002)(106356001)(25786008)(97736004)(53936002)(5001770100001)(107886002)(6666003)(50226002)(68736007)(8676002)(92566002)(81166006)(305945005)(81156014)(189998001)(36756003)(5003940100001)(6486002)(6496003)(2201001)(50986999)(76506005)(42186005)(101416001)(7736002)(33646002)(2906002)(76176999)(105586002)(2950100002)(6636002)(38730400001)(48376002)(50466002)(86362001)(5660300001)(47776003); DIR:OUT; SFP:1102; SCL:1; SRVR:MWHPR15MB1327; H:localhost; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX: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; MWHPR15MB1327; 23:JdS+lR15alXhs47O1iJA38ahlpqm2iStbtKPJqjAY?= =?us-ascii?Q?zcu5mgA5carnUzNqkYrxeMmilMSkL6aiuYTB6LKnB+rBNNTnC2LNLFYtn/Jj?= =?us-ascii?Q?Ep4gPKNCgiY/vAG8Hgjkt5hcwu2MBTZCR+VnHEGhGdxRMCh1a1W1tVKQR7nM?= =?us-ascii?Q?EShiZfgtXFEz+zEP0gCrCfx6FRfHSwja2fjwJ1PwZzkgwqlEZ0HYBBBEYw1Q?= =?us-ascii?Q?a5hpYvbkLJx8XQifa5ymXhvl1jgDkU+03tcTZ8hZzliX474h2d2YZ6rDeZ2i?= =?us-ascii?Q?u6tkCavPiG6eQuqRndq+QaqK9o8BRhUH9zm5ot9VMCg+xr+WMELMbxW2zEkQ?= =?us-ascii?Q?s64Mo+CC3bb6F5smirbGqnmRjgEHEtgyl6jaa1z0jnY+kU9r67/JU5ZgWOD7?= =?us-ascii?Q?TM5nhToYO1sUXKB4vjMbYUtIiEgjgAsbqGSvokNvHEGo5HXPPH55djp7nCvF?= =?us-ascii?Q?wS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 20:IjYGYNkgMmeQxjG+qwOrJRzyEvnmkzm8E58905YpSKezREHy7O0rg6gaeoQuXVx6OYBT5G1E+T+Lq3O5LFJ83g8Mi8JWEcgpFkXGLklpxODukEFJ4a/E7zBZLUD8klyWkudYeXDTouCloSbIG3CJe9Hzh7nGkFkL8EtMv5tapZ8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2017 21:57:07.2738 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR15MB1327 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-20_14:, , 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);