From patchwork Fri Jan 20 21:56: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: 9529561 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 955B36020B for ; Fri, 20 Jan 2017 21:57:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 955D9286D6 for ; Fri, 20 Jan 2017 21:57:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 875E3286DA; Fri, 20 Jan 2017 21:57: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 0B29C286D6 for ; Fri, 20 Jan 2017 21:57:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752386AbdATV5l (ORCPT ); Fri, 20 Jan 2017 16:57:41 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:59094 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752243AbdATV5Y (ORCPT ); Fri, 20 Jan 2017 16:57:24 -0500 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v0KLsdIc019739; Fri, 20 Jan 2017 13:57:14 -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=+DYjNnwrZ/2jc+7/jko+QygtceoJhCpm9z/m+tS37mA=; b=Fv3gHMpszrp247bmSxKFjvbECnORZNontrda1RI45V/5CSN4lrrbaGAkYzeTq+22eb2G fur/NKEe15Heve48QhOcwRiC6mBAM2mO3To+BXCpNnH2s0PxQ8PwIpOS6+GZtNmTg/IO G0vxnHPCxEQerICSZI7EvInx919/rjemtRA= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 283qmark69-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 20 Jan 2017 13:57:14 -0800 Received: from NAM02-CY1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.15) with Microsoft SMTP Server (TLS) id 14.3.294.0; Fri, 20 Jan 2017 13:57:13 -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=+DYjNnwrZ/2jc+7/jko+QygtceoJhCpm9z/m+tS37mA=; b=efet93w6+LuEpjsM4S+PuK2HuifsFuIKj8zEexW1Ho2cx6Rkr6rXfZDiRnU9veS4IDiU/lwysfRs755bE5zsXB1botHYflKeVkBGquCqPTYUbDYpd9uLLuFvwpvTLsyrH1byQvMM7fEKjG02MLgKySrtf8PrcmrLpP4rdFwn8DA= Received: from localhost (2606:a000:4381:1201:225:22ff:feb3:e51a) by CY4PR15MB1319.namprd15.prod.outlook.com (10.172.182.7) 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:11 +0000 From: Josef Bacik To: , , , , , Subject: [PATCH 4/4] nbd: add a nbd-control interface Date: Fri, 20 Jan 2017 16:56:52 -0500 Message-ID: <1484949412-6903-4-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: SN2PR18CA0019.namprd18.prod.outlook.com (10.169.189.29) To CY4PR15MB1319.namprd15.prod.outlook.com (10.172.182.7) X-MS-Office365-Filtering-Correlation-Id: 557d79e1-7ade-4fc1-bf03-08d4417f498f X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:CY4PR15MB1319; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1319; 3:ELZQ2YkV6VxRheCRD4EIojZRnClTPSaEE3kiB3fKTUJkpuKkmth6bYBFQyiByuKGyQUclwHmkiavBmg33xuBsUo+mtpo5U2rSAI9quTWrAFtfWeF4EaG0KxieII7irOfozeHl/QNBeVj9pommLC1VPQWPTveRJq6VDO8b8GDtddgOmMbLsMPt/QZF2mKt8vXf/JqWEr9nEwnkKjQEFZDUsST9gyHdUlrvZbORmMU1mVawqBFUsekqo/9bhjJCxejvYiOUpEnI/duKn8B/SWtoQ== X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1319; 25:PciLCYb0Dxenxp0tUzJEAyXV5V6ICRYAs6dpw4Iz8BoidOE3PG7iERNZR1+MNHy22Epj5lsWSjXXXHrZwBQ7QdFLQaTmuKXMmEDyU08Dqi6BbehfoGpCoEr51YnrOLv8EKt72aUl6m0D9RbfSTQFUWwQefN/oewYR7alt29oV8TPmy27WrQz/TQkocoop3osLnYVgJkGOfrOS5pTd0YN/SVGPzuZTJseroHg5CPb0sUqWqUtKABsWM4rGFEEajNAsjJDXitbmd61iCb5UddYfKJmQEpBdgpG8e9Pb4KzPvLrgjTJxNNAKB7xAJrDaFu7MO8GaOIgQsiCRNkRW++pBPXfVIWn+/XsOU7kSrlsTANvXu7ZihSVLt92wl+boq+hd47Mdpb0+k6i1zm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icf0hVeMpEyQK4bHUm86CMXV+Htg4ro5YrvT45xVfhIC3m28LTIH3nwALQBaeDszkJP2BhqGloLxE4QaYhRgreuiFvlP/piBwGdjtcjvoFF8Lv5bFaHRAvz1ZC 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)(10201501046)(3002001)(6041248)(20161123555025)(20161123558021)(20161123560025)(20161123562025)(20161123564025)(6072148); SRVR:CY4PR15MB1319; BCL:0; PCL:0; RULEID:; SRVR:CY4PR15MB1319; X-Microsoft-Exchange-Diagnostics: 1; CY4PR15MB1319; 4:0ad7WtfbYVtKmR3StElP2kGIvBT3/5Rs+KJV8GSU5eVCZHYi5Eh23M9OlmaY7QZekbesCUXtZZZxx3QWeC7cACH8Rr7j7CojBMxw04g5TbJLt7V/dGuMPCRISdOQ/k22rDBwQvmwlrzeu2GcnmTZxdWAOTBXgf025qexRlRABuQ/9cPRSAzysriTJJv883I21tADaxZ0+r7SoFmVviZR1WqkZYKH+u9eXOtgbwwDAwRmtl/c39KbKtlaSz2dqVmAvy99w5dIsDFb4zVHKfYkDOUYe6H+K8dwy2WFb0G913bUsh6wQacPZX+5TjTe+s+JxXkmVojZ3IkUWq98gl5QzD0gja/w8+aGq3YdmxrWejGwzizKnTSsAfU3xM4vaHMdaSVP6W81Fob4Zi5l81XMhUdjsXvn3XG8TY6373vTvONeN6GXun+wbc4QLPxPUU31lhMDL5Hcl8DXTiyvEpx6Yd75JLq83XFgpmTjNoo6f4F2hZ0D9elIs+cf8Chrvn8QhuCTv2SQEErzNmaK6/ucehff1vxsOBhW+HRE2nSeehAVwKEWjiVLB4dYdgl48YD9efhbwxcvNZMdyJ1H6rDv751GQ/1pgdJOrkfjqcEqqYXYXAlS042qSvzKuVQDFJd4Pd7wIsgeQZUDfDppCrOIpw== X-Forefront-PRVS: 01930B2BA8 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6069001)(6009001)(7916002)(39410400002)(39450400003)(39830400002)(189002)(51234002)(199003)(105586002)(47776003)(92566002)(5660300001)(106356001)(48376002)(86362001)(2201001)(101416001)(50226002)(2906002)(5003940100001)(42186005)(5001770100001)(7736002)(305945005)(97736004)(68736007)(50466002)(25786008)(81166006)(81156014)(6116002)(6486002)(53936002)(8676002)(6666003)(6496003)(6636002)(2950100002)(33646002)(76506005)(107886002)(50986999)(189998001)(36756003)(76176999)(38730400001); DIR:OUT; SFP:1102; SCL:1; SRVR:CY4PR15MB1319; 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; CY4PR15MB1319; 23:VuW2OkpYK1DBvooI7djnMAEXY9JO/RbwXhOm5s9xL?= =?us-ascii?Q?NmrIvEkrmE3srazNJKZa6gWMsDKgrs2DXZGqMocxsunBsOb3BMj16ykKgOfm?= =?us-ascii?Q?+XwPzxR8l1uxqTlw+eP64xpKnyaDV5LY2AZUiq/FOYfNQngoRjDZckoo0IQ4?= =?us-ascii?Q?YPolB0RTiuHRcelhUMd3cSl5gX/SY/jZlzLS9L36uDMaGijEpxZqWPCinxs8?= =?us-ascii?Q?9ZgmdeiQtRLGRG8bqACrASq6HdXtvZY2yMNseRxphNKKCrEIYPa16njfQ+Gu?= =?us-ascii?Q?LJySe0o7fp52rxo/EdyM/CzEXoH6JtaJl0fc7vpm5hPhvpvEdq0dsiJ2IaQ7?= =?us-ascii?Q?b3StIVZEwuaTixtXsq8pupOlsjg8Zv1kVw+mErSe0WKlAfpSNx0YCyaTPKna?= =?us-ascii?Q?fh+HX+doPgORDPBuqXncEclNNTNFYq1xyPgNnenmZut87w8BUwK6o2F5x+5i?= =?us-ascii?Q?Yg+KImsgzyd4wlBX7TlCDylephnBSdfucbMROzrl4qMdweKAYRW8r6me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XEZSTpRbsyqFv+S130aYa3BdQlq8pwDe4+/y6bp2SCyqfKXCXfThkj2CV4oyZf8n5u0bBIMeQK1vhlGrliGsewd/9rv9XgjbXmQ1UMMjrmZJ2bUxK9LZKTCvJ0v21O9spoPXr2thzk/GVC2cHgItVsHkJIs7CUX2cku0= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2017 21:57:11.4780 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1319 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 This patch mirrors the loop back device behavior with a few changes. First there is no DEL operation as NBD doesn't get as much churn as loop devices do. Secondly the GET_NEXT operation can optionally create a new NBD device or not. Our infrastructure people want to not allow NBD to create new devices as it causes problems for them in containers. However allow this to be optional as things like the OSS NBD client probably doesn't care and would like to just be given a device to use. Signed-off-by: Josef Bacik --- drivers/block/nbd.c | 129 ++++++++++++++++++++++++++++++++++++++++++----- include/uapi/linux/nbd.h | 10 ++++ 2 files changed, 125 insertions(+), 14 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 96f7681..c06ed36 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -59,6 +60,7 @@ struct nbd_device { unsigned long runtime_flags; struct nbd_sock **socks; int magic; + int index; struct blk_mq_tag_set tag_set; @@ -1041,6 +1043,7 @@ static int nbd_dev_add(int index) if (err < 0) goto out_free_disk; + nbd->index = index; nbd->disk = disk; nbd->tag_set.ops = &nbd_mq_ops; nbd->tag_set.nr_hw_queues = 1; @@ -1097,20 +1100,109 @@ static int nbd_dev_add(int index) return err; } -/* - * And here should be modules and kernel interface - * (Just smiley confuses emacs :-) - */ +static int find_free_cb(int id, void *ptr, void *data) +{ + struct nbd_device *nbd = ptr; + struct nbd_device **ret = data; + + if (nbd->num_connections == 0) { + *ret = nbd; + return 1; + } + if (*ret == NULL || (*ret)->index < nbd->index) + *ret = nbd; + return 0; +} + +static int nbd_dev_lookup(struct nbd_device **ret, int index) +{ + struct nbd_device *nbd = NULL; + + if (index < 0) { + int err = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd); + if (err != 1) { + if (nbd != NULL) { + *ret = NULL; + return nbd->index + 1; + } else { + return 0; + } + } + } else + nbd = idr_find(&nbd_index_idr, index); + if (nbd) { + *ret = nbd; + return nbd->index; + } + return -ENODEV; +} + +static long nbd_control_ioctl(struct file *file, unsigned int cmd, + unsigned long parm) +{ + struct nbd_device *nbd = NULL; + int ret = -ENOSYS; + + mutex_lock(&nbd_index_mutex); + switch (cmd) { + case NBD_CTL_ADD: + ret = nbd_dev_lookup(&nbd, parm); + if (ret >= 0) { + ret = -EEXIST; + break; + } + ret = nbd_dev_add(parm); + break; + case NBD_CTL_GET_FREE: + ret = nbd_dev_lookup(&nbd, -1); + if (ret < 0) + break; + if (parm && !nbd) { + int index = ret; + + ret = nbd_dev_add(ret); + if (ret < 0) + break; + ret = index; + } + break; + default: + break; + } + mutex_unlock(&nbd_index_mutex); + return ret; +} + +static const struct file_operations nbd_ctl_fops = { + .open = nonseekable_open, + .unlocked_ioctl = nbd_control_ioctl, + .compat_ioctl = nbd_control_ioctl, + .owner = THIS_MODULE, + .llseek = noop_llseek, +}; + +static struct miscdevice nbd_misc = { + .minor = NBD_CTRL_MINOR, + .name = "nbd-control", + .fops = &nbd_ctl_fops, +}; + +MODULE_ALIAS_MISCDEV(NBD_CTRL_MINOR); +MODULE_ALIAS("devname:nbd-control"); static int __init nbd_init(void) { - int i; + int i, ret; BUILD_BUG_ON(sizeof(struct nbd_request) != 28); + ret = misc_register(&nbd_misc); + if (ret < 0) + return ret; + ret = -EINVAL; if (max_part < 0) { printk(KERN_ERR "nbd: max_part must be >= 0\n"); - return -EINVAL; + goto out_misc; } part_shift = 0; @@ -1129,17 +1221,20 @@ static int __init nbd_init(void) } if ((1UL << part_shift) > DISK_MAX_PARTS) - return -EINVAL; - + goto out_misc; if (nbds_max > 1UL << (MINORBITS - part_shift)) - return -EINVAL; + goto out_misc; + recv_workqueue = alloc_workqueue("knbd-recv", WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); - if (!recv_workqueue) - return -ENOMEM; - - if (register_blkdev(NBD_MAJOR, "nbd")) - return -EIO; + if (!recv_workqueue) { + ret = -ENOMEM; + goto out_misc; + } + if (register_blkdev(NBD_MAJOR, "nbd")) { + ret = -EIO; + goto out_workqueue; + } nbd_dbg_init(); @@ -1148,6 +1243,11 @@ static int __init nbd_init(void) nbd_dev_add(i); mutex_unlock(&nbd_index_mutex); return 0; +out_workqueue: + destroy_workqueue(recv_workqueue); +out_misc: + misc_deregister(&nbd_misc); + return ret; } static int nbd_exit_cb(int id, void *ptr, void *data) @@ -1164,6 +1264,7 @@ static void __exit nbd_cleanup(void) idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL); idr_destroy(&nbd_index_idr); destroy_workqueue(recv_workqueue); + misc_deregister(&nbd_misc); unregister_blkdev(NBD_MAJOR, "nbd"); } diff --git a/include/uapi/linux/nbd.h b/include/uapi/linux/nbd.h index c91c642..a76924b 100644 --- a/include/uapi/linux/nbd.h +++ b/include/uapi/linux/nbd.h @@ -29,6 +29,16 @@ #define NBD_SET_TIMEOUT _IO( 0xab, 9 ) #define NBD_SET_FLAGS _IO( 0xab, 10) +/* Use the last 8 values of our allocation for CTRL ioctls. */ +#define NBD_CTL_ADD _IO( 0xab, 23) +#define NBD_CTL_GET_FREE _IO( 0xab, 24) + +enum { + NBD_CTL_GET_FREE_SCAN = 0, /* Get the next free available device. */ + NBD_CTL_GET_FREE_ADD = 1, /* Get the next free available device, but + create one if one does not exist. */ +}; + enum { NBD_CMD_READ = 0, NBD_CMD_WRITE = 1,