From patchwork Thu Jan 26 03:33:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Demi Marie Obenour X-Patchwork-Id: 48483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp76293wrn; Wed, 25 Jan 2023 19:39:56 -0800 (PST) X-Google-Smtp-Source: AMrXdXuaVEbz+OAuqEQUXauR6FLBeBaR7mb0luYqGiSqD+Zd3mdPPgQdBeDO8lWiFMCEuYBBWwnZ X-Received: by 2002:a17:906:b215:b0:871:2060:c4c4 with SMTP id p21-20020a170906b21500b008712060c4c4mr36649179ejz.68.1674704396755; Wed, 25 Jan 2023 19:39:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674704396; cv=none; d=google.com; s=arc-20160816; b=Ccuvp0CLPtOR3dzvDHVSSeTurBYajjwxF5ouo9/IZ1DUB4pNN/ykj8443cM1qmnEXV DUMlon5DMI8qQJe/PYTMG5+BB4UImHi8BV+whTg2XiUYSijwmmWLavldzNBnPcD7yxZQ AFudMM6qee2ZeFu/oM7JrCu/1mSt7PxuUyyux1sDMvDdkovX8nqGx2Bt4wRZx+z+UxYU HC2LaWBIFvMtH/OSi9RM3bY9Fchu5Gx3WQWJyGFHxWvqNiX96WvxRPCWptNPMyLhdNc+ v7XmY7SaZVc/xUn0opb5gq09giR31TUxC9eOQCdFu5hpN+AYgCtLnsUSuXt6NpypH/FS iLkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=daYdDSxsQizEcNPbMbpsOlpi5qeRBNOt+BqQaw5kD0w=; b=wcFR1RTQAQsIfAglNBte1TSJaopVP1D+6kU7V8ZqZ5RTfsbuKgesOQRRxqtb3bTRHn UzJCUSCT52YPkN9MWpCBZmjLocv0BdmrNm89izoApWf0es1R/D8NcIMUE09w30qd+okH aNlan05jIYy81hUP6C9iZ0ss9VtAp19u54IqxUYGkYvlYFNTpG34FnRq041jF8ZaA3QQ Pg7+s+FdC0a4NXYzUYMByqAc2AvuH+zbG+bycttVmhe334EH3Ip9pYWeE7HCtmbK5DFA R9AG8cyQVUZydzXKQvqO/0Q+Fsuo4mF0H/nED5Up8a/u2zCLB0tRXuGkXPrp8U4qQ8R+ bfFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@invisiblethingslab.com header.s=fm3 header.b="Gz2/2Xbq"; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=NutHK5vz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mf19-20020a1709071a5300b007c11812c621si8179385ejc.459.2023.01.25.19.39.33; Wed, 25 Jan 2023 19:39:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@invisiblethingslab.com header.s=fm3 header.b="Gz2/2Xbq"; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=NutHK5vz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236453AbjAZDeZ (ORCPT + 99 others); Wed, 25 Jan 2023 22:34:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236339AbjAZDeR (ORCPT ); Wed, 25 Jan 2023 22:34:17 -0500 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05A91577CB for ; Wed, 25 Jan 2023 19:34:12 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 443E25C05B6; Wed, 25 Jan 2023 22:34:12 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 25 Jan 2023 22:34:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-transfer-encoding:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm3; t= 1674704052; x=1674790452; bh=daYdDSxsQizEcNPbMbpsOlpi5qeRBNOt+Bq Qaw5kD0w=; b=Gz2/2XbquxSEsRBwW3i8K6QksQx0hV7tWzYjhpzeocxqrFIM6wh zdx8cHua8jpdXCnntgZk9zP1FpKjVVuxcTDVAAaIPY7uX1HET1rnYyBE/OUveR/a yGltG4jXyOnqQ3XVG9O0oOamWRMfH3jkWh9CTsX8tZPb+D00NjEubF8/Tt1oKf0T JZ5GQO0SLUiYtvCIPUFuvUIvYreccKY7X9UApl1p+dTZHOXZX79hcLfihRLQvAnU L2LTLLdKL1eSL+Y3znx8Cu2IMujQ9I10DgzMdzuqLJO7T/MDOGnT80u2FU12KGEv qRtd/wPKUG+HAcJ8mr/1iDlcnqC4Fitaq+g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1674704052; x=1674790452; bh=daYdDSxsQizEc NPbMbpsOlpi5qeRBNOt+BqQaw5kD0w=; b=NutHK5vzuBp6vHCz5b862NQHzoAk2 QPkv41LPJqzDVYP/LpUDEPi9xtlw73q09xZRZiPm8QzFFgdDD69UZ0AQIvD6Iyin 85LXriSm4s570MdKkyAUDRygNHx6uNmuyWaozpY90SnPKK/peQWaQr71aWbms5PT ag17AqsLTL9bUI5y9LRTfpZfB8o9LGxhaklz2PXkKYLw5hhUv7mhxObZ64ZayGhw XtUcsfqkhf+tG8itv9TK6QGxQPMDn4XfaCF3jRe5rvChK1DlDDmx+VOtO7lTXCnD L81I4VkrBnt7l1Rd6zkgQuOxrWiivMc9yxG8JFCUu2gqLnxg5gvv01PTw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedruddvfedgheelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgvmhhi ucforghrihgvucfqsggvnhhouhhruceouggvmhhisehinhhvihhsihgslhgvthhhihhngh hslhgrsgdrtghomheqnecuggftrfgrthhtvghrnhepjeffjefggfeugeduvedvjeekgfeh gffhhfffjeetkeelueefffetfffhtdduheetnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepuggvmhhisehinhhvihhsihgslhgvthhhihhnghhs lhgrsgdrtghomh X-ME-Proxy: Feedback-ID: iac594737:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 25 Jan 2023 22:34:11 -0500 (EST) From: Demi Marie Obenour To: Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com Cc: Demi Marie Obenour , =?utf-8?q?Marek_Marczy?= =?utf-8?q?kowski-G=C3=B3recki?= , linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/7] Allow userspace to get an FD to a newly-created DM device Date: Wed, 25 Jan 2023 22:33:54 -0500 Message-Id: <20230126033358.1880-3-demi@invisiblethingslab.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230126033358.1880-1-demi@invisiblethingslab.com> References: <20230126033358.1880-1-demi@invisiblethingslab.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756054837079087735?= X-GMAIL-MSGID: =?utf-8?q?1756054837079087735?= This allows creating a device-mapper device, opening it, and setting it to be deleted when unused in a single atomic operation. Signed-off-by: Demi Marie Obenour --- drivers/md/dm-ioctl.c | 67 +++++++++++++++++++++++++++++------ include/uapi/linux/dm-ioctl.h | 16 ++++++++- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c index 36fc6ae4737a05ab53ab67a8ccee525cb5fda082..05438dedcd17b7cac470fcc5a9721d67daad4bfb 100644 --- a/drivers/md/dm-ioctl.c +++ b/drivers/md/dm-ioctl.c @@ -853,9 +853,21 @@ static void __dev_status(struct mapped_device *md, struct dm_ioctl *param) static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size) { - int r, m = DM_ANY_MINOR; + int r, m = DM_ANY_MINOR, fd; struct mapped_device *md; + /* Do not allow unknown flags */ + if (param->flags > (2 * DM_FILE_DESCRIPTOR_FLAG - 1)) + return -EINVAL; + + /* + * Do not allow creating a device that would just be destroyed + * before the ioctl returns. + */ + if ((param->flags & DM_DEFERRED_REMOVE) && + !(param->flags & DM_FILE_DESCRIPTOR_FLAG)) + return -EINVAL; + r = check_name(param->name); if (r) return r; @@ -867,20 +879,55 @@ static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_si if (r) return r; - r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); - if (r) { - dm_put(md); - dm_destroy(md); - return r; - } - param->flags &= ~DM_INACTIVE_PRESENT_FLAG; + r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); + if (r) + goto out_put; + + if (param->flags & DM_FILE_DESCRIPTOR_FLAG) { + struct block_device *bdev = dm_disk(md)->part0; + struct file *file; + + fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); + if (fd < 0) { + r = fd; + goto out_put; + } + + file = blkdev_get_file(bdev, O_RDWR|O_CLOEXEC, NULL); + if (IS_ERR(file)) { + r = PTR_ERR(file); + goto out_put_fd; + } + + /* + * Simulate opening the device. The other checks in + * dm_blk_open() are not necessary becuase we have a reference + * to the `struct md`. + */ + atomic_inc(&md->open_count); + fd_install(fd, file); + param->file_descriptor = fd; + } + + /* + * If userspace requests it, automatically delete the device + * when it is no longer used + */ + if (param->flags & DM_DEFERRED_REMOVE) + set_bit(DMF_DEFERRED_REMOVE, &md->flags); + __dev_status(md, param); - dm_put(md); - return 0; + +out_put_fd: + put_unused_fd(fd); +out_put: + dm_put(md); + dm_destroy(md); + return r; } /* diff --git a/include/uapi/linux/dm-ioctl.h b/include/uapi/linux/dm-ioctl.h index 7edf335778bae1cb206f6dd4d44e9cf7fb9da35c..30a6260ed7e06ff71fad1675dd4e7f9325d752a6 100644 --- a/include/uapi/linux/dm-ioctl.h +++ b/include/uapi/linux/dm-ioctl.h @@ -136,7 +136,13 @@ struct dm_ioctl { * For output, the ioctls return the event number, not the cookie. */ __u32 event_nr; /* in/out */ - __u32 padding; + + union { + /* Padding for named devices */ + __u32 padding; + /* For anonymous devices, this is a file descriptor. */ + __u32 file_descriptor; + }; __u64 dev; /* in/out */ @@ -382,4 +388,12 @@ enum { */ #define DM_IMA_MEASUREMENT_FLAG (1 << 19) /* In */ +/* + * If set in a DM_DEV_CREATE ioctl(), sets the file_descriptor field + * to a valid file descriptor. This can be combined with DM_DEFERRED_REMOVE + * to cause the device to be destroyed when the file descriptor is closed + * and is otherwise unused. + */ +#define DM_FILE_DESCRIPTOR_FLAG (1 << 20) /* In */ + #endif /* _LINUX_DM_IOCTL_H */