From patchwork Thu Feb 3 13:14:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734107 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02AACC433EF for ; Thu, 3 Feb 2022 13:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350715AbiBCNOo (ORCPT ); Thu, 3 Feb 2022 08:14:44 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:55338 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347799AbiBCNOm (ORCPT ); Thu, 3 Feb 2022 08:14:42 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 00817617FE for ; Thu, 3 Feb 2022 13:14:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07CE3C340EF; Thu, 3 Feb 2022 13:14:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894081; bh=HBVtq9mPXrIsA+URtlvbHEEm8yANYFgBIPPy0Zbl3qE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VfcjDE8fRXhTs2M5bksSU4I0Du8FgmVGzq96+PMYEJWVk8Oxrtvdn22fs8phsqHMC /r9/WHeRRPQUvXU+xcnMSLuHkD1XY7LZAtgzk4luJ15jNzLQLUtYItRH1UfjaTKMxi o1KIB4ZZyz8dD824qTDcxJL51U91jFo/tw8gZNiLZlJ3FCCmDEDxinkFgbAtg7TOuD 03DGIHVAsRqxjdgHZPHoALpVMIAvYzw8hbIM9abDyAri4RCjOc722ZkTOb8Mz9BGF4 shGgAMM4hejURsjErH+fdrNYz2XNdzCE4KV5GOiWkskcvL9xItrhAMcKSKkjQEPNuX 50aVL/MBDewOw== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 1/7] tests: fix idmapped mount_setattr test Date: Thu, 3 Feb 2022 14:14:05 +0100 Message-Id: <20220203131411.3093040-2-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1705; h=from:subject; bh=HBVtq9mPXrIsA+URtlvbHEEm8yANYFgBIPPy0Zbl3qE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsq4SivTcKfw8u0HpLYkx8Wdc9QS0jvrkqDoJVW09qjM 0/tPO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACYS+4zhr4SInIJQP+PC1Yd2HOOtZ2 Y/+zjDheODS/j2OVxZ5vyfWxn+R6nnrNl81X1OHafav71PWe3VAxt6Tu/h4tuwKlTW2mA2FwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The test treated zero as a successful run when it really should treat non-zero as a successful run. A mount's idmapping can't change once it has been attached to the filesystem. Fixes: 01eadc8dd96d ("tests: add mount_setattr() selftests") Cc: Seth Forshee Cc: Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- tools/testing/selftests/mount_setattr/mount_setattr_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mount_setattr/mount_setattr_test.c b/tools/testing/selftests/mount_setattr/mount_setattr_test.c index f31205f04ee0..8c5fea68ae67 100644 --- a/tools/testing/selftests/mount_setattr/mount_setattr_test.c +++ b/tools/testing/selftests/mount_setattr/mount_setattr_test.c @@ -1236,7 +1236,7 @@ static int get_userns_fd(unsigned long nsid, unsigned long hostid, unsigned long } /** - * Validate that an attached mount in our mount namespace can be idmapped. + * Validate that an attached mount in our mount namespace cannot be idmapped. * (The kernel enforces that the mount's mount namespace and the caller's mount * namespace match.) */ @@ -1259,7 +1259,7 @@ TEST_F(mount_setattr_idmapped, attached_mount_inside_current_mount_namespace) attr.userns_fd = get_userns_fd(0, 10000, 10000); ASSERT_GE(attr.userns_fd, 0); - ASSERT_EQ(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0); + ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0); ASSERT_EQ(close(attr.userns_fd), 0); ASSERT_EQ(close(open_tree_fd), 0); } From patchwork Thu Feb 3 13:14:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734108 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1FBFC433F5 for ; Thu, 3 Feb 2022 13:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350716AbiBCNOo (ORCPT ); Thu, 3 Feb 2022 08:14:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350575AbiBCNOo (ORCPT ); Thu, 3 Feb 2022 08:14:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44035C061714 for ; Thu, 3 Feb 2022 05:14:44 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D4EE8617E9 for ; Thu, 3 Feb 2022 13:14:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE40AC340ED; Thu, 3 Feb 2022 13:14:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894083; bh=FWhmJ0HWjsMoFYkIW2ebdBklYwq0CMJXlb3zKBXrK4I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wz/laMYYyKqIQvgKrwURjr0PlVfagZXPFiUvvUltw3raYPhC3KkZyRVJ2yy2r764/ D+EQi/zzaI0UhemRJmqXDIJ3esqCjv0XlDmJaJMxJdO1b4bS3nKISl1YEbNMaFfsrQ jHMiqDOaiXiTQyTj01ZwZVk3ZUZ1xZOeHShD3W0W0KUNtk8mlMsev0tqen5FoJoTXx /HvWW92ocd8+Wb+ARkDqJ4gT7HCsJKDs1Uk7dcM8IH69hpENjSE++AlSGbqTYqLxV6 pYIARptYhE6QFYS6r5YOEI6TlsaqozhRMyNx0N1+WGkZQfE68lhG/Pv9H3CVbfcfRh EMM0lTG0p6eBw== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 2/7] MAINTAINERS: add entry for idmapped mounts Date: Thu, 3 Feb 2022 14:14:06 +0100 Message-Id: <20220203131411.3093040-3-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1113; h=from:subject; bh=FWhmJ0HWjsMoFYkIW2ebdBklYwq0CMJXlb3zKBXrK4I=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vspoNm9u06Tot9Xz8y153i8+uc+kfOeh4BuyVh/vnmxf LbR/ZkcpC4MYF4OsmCKLQ7tJuNxynorNRpkaMHNYmUCGMHBxCsBE/OQZGa48/bv9tQJ7qkhQqsm0nN Q/8xpNwrkmz+72v/7V0ufDJn5GhiPLLbujuriau+UmzQ8IPf3Ozk/mkHpMhcGrvz2W0kq3GAE= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org I'd like to continue maintaining the work that was done around idmapped, make sure that I'm Cced on new patches and work that impacts the infrastructure. Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index f41088418aae..0496b973bb87 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9253,6 +9253,15 @@ S: Maintained W: https://github.com/o2genum/ideapad-slidebar F: drivers/input/misc/ideapad_slidebar.c +IDMAPPED MOUNTS +M: Christian Brauner +L: linux-fsdevel@vger.kernel.org +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git +F: Documentation/filesystems/idmappings.rst +F: tools/testing/selftests/mount_setattr/ +F: include/linux/mnt_idmapping.h + IDT VersaClock 5 CLOCK DRIVER M: Luca Ceresoli S: Maintained From patchwork Thu Feb 3 13:14:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C70CC433F5 for ; Thu, 3 Feb 2022 13:14:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350717AbiBCNOs (ORCPT ); Thu, 3 Feb 2022 08:14:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241853AbiBCNOq (ORCPT ); Thu, 3 Feb 2022 08:14:46 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 609C8C061714 for ; Thu, 3 Feb 2022 05:14:46 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F132D617FE for ; Thu, 3 Feb 2022 13:14:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6A1FC340F1; Thu, 3 Feb 2022 13:14:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894085; bh=U78bXyJT6cq6yQMpEOnCqW0QOW6BGlhwk2lyDA/GncI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lB2xlXVMPUxMHGpDtAW93QS5OKMP/FAbdBAY8lDV0OgYCJYDnOfs8tuvG1pcZNczv BnVwZvpYidy7j4h18k4OOb11mAb0r+ZSaaGUTeLe6drlP9ocjrzpVFOyxwGfm3dMdY MqkbqW7tjpnP4CcVLqHkBXYV0kRY3sK36GJ3T7r9reZruqobxh7RO8uchoMacm+bLz tjC/rnxVu3Qejms0JBFcZP4PImJqA7vgH8JCbOcLK9ceh8QHA12UazNKSAXx/a67z3 IUcQXB/N5TTxxcQ7xWrM6ZdVrWqSXOJ2401ZZSTsDXXyQ9/tn3JCG9KqwjtLE6QwlR iVP77Kx74f0wQ== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 3/7] fs: add kernel doc for mnt_{hold,unhold}_writers() Date: Thu, 3 Feb 2022 14:14:07 +0100 Message-Id: <20220203131411.3093040-4-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2254; h=from:subject; bh=U78bXyJT6cq6yQMpEOnCqW0QOW6BGlhwk2lyDA/GncI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsq0TSll5Y8te4M1M8xuOajPZuTu9jDNCclpf+IavKZb 7F9TRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwESOWTL8FX3x6ovWvcRvcmHCrDGSy6 4dejTnd22wcVVi55mS88eezGRkuOXQnWV6tNDw1P6ybJlXimYJX09vrn5wN+Xgox9T7Ixl+AA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When I introduced mnt_{hold,unhold}_writers() in commit fbdc2f6c40f6 ("fs: split out functions to hold writers") I did not add kernel doc for them. Fix this and introduce proper documentation. Fixes: commit fbdc2f6c40f6 ("fs: split out functions to hold writers") Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- fs/namespace.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/fs/namespace.c b/fs/namespace.c index 40b994a29e90..de6fae84f1a1 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -469,6 +469,24 @@ void mnt_drop_write_file(struct file *file) } EXPORT_SYMBOL(mnt_drop_write_file); +/** + * mnt_hold_writers - prevent write access to the given mount + * @mnt: mnt to prevent write access to + * + * Prevents write access to @mnt if there are no active writers for @mnt. + * This function needs to be called and return successfully before changing + * properties of @mnt that need to remain stable for callers with write access + * to @mnt. + * + * After this functions has been called successfully callers must pair it with + * a call to mnt_unhold_writers() in order to stop preventing write access to + * @mnt. + * + * Context: This function expects lock_mount_hash() to be held serializing + * setting MNT_WRITE_HOLD. + * Return: On success 0 is returned. + * On error, -EBUSY is returned. + */ static inline int mnt_hold_writers(struct mount *mnt) { mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; @@ -500,6 +518,18 @@ static inline int mnt_hold_writers(struct mount *mnt) return 0; } +/** + * mnt_unhold_writers - stop preventing write access to the given mount + * @mnt: mnt to stop preventing write access to + * + * Stop preventing write access to @mnt allowing callers to gain write access + * to @mnt again. + * + * This function can only be called after a successful call to + * mnt_hold_writers(). + * + * Context: This function expects lock_mount_hash() to be held. + */ static inline void mnt_unhold_writers(struct mount *mnt) { /* From patchwork Thu Feb 3 13:14:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734110 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C575C433EF for ; Thu, 3 Feb 2022 13:14:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350723AbiBCNOx (ORCPT ); Thu, 3 Feb 2022 08:14:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350721AbiBCNOu (ORCPT ); Thu, 3 Feb 2022 08:14:50 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5F4EC061714 for ; Thu, 3 Feb 2022 05:14:49 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 86E63B83402 for ; Thu, 3 Feb 2022 13:14:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAE70C340F0; Thu, 3 Feb 2022 13:14:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894087; bh=PZFEvSeWqtjZgQHKPH3SNY8goi8KJHMMMUNAANt6D/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lGrjdSy8DfJaPMYYPh29irTJDyUHPM9ywqbiD7RAIn5PMwJD0sq400vIRkPpi76gs Zs5wB0kzBeAqk6iqKc2986GcyTNQJ+lvvGD1REFu2zp8+OxmDtLDek6cF+0K7D0JPt FbY95wi4as7a7Cnd8Cd2hcTon729yURGt9xxRfZEPV+X+GdnrTKU+QmC8bitU6ZjeK oStlIEp3GAY48ugtq1ul7/PZ1/uYJFZ6vvoJR32s+g1yhRtDKixT+fVOzhNPXAqvy8 zPa0Rt9k6ckOP1dyE7CFSx6yZqzrMTmi7jXp/fcLAcaaE+Mhvygt+/WNq9HgAg4Hde iNg4bZYw4j/Pg== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 4/7] fs: add mnt_allow_writers() and simplify mount_setattr_prepare() Date: Thu, 3 Feb 2022 14:14:08 +0100 Message-Id: <20220203131411.3093040-5-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1919; h=from:subject; bh=PZFEvSeWqtjZgQHKPH3SNY8goi8KJHMMMUNAANt6D/s=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsqUkiTe8V/ohMi0v6l7U+OlhIoFL82676jX0bPoz6rA TDnpjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgInkujMyfDH52Xtg04r5bkKs/Dt978 m4sf24tv3XU+t57/799f84S4eRYbMpT9xP1oU9aZsLWNXfXdZOOy0Wa/PGejlP7CxxlS/C3AA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add a tiny helper that lets us simplify the control-flow and can be used in the next patch to avoid adding another condition open-coded into mount_setattr_prepare(). Instead we can add it into the new helper. Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- fs/namespace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index de6fae84f1a1..7e5535ed155d 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -3998,6 +3998,22 @@ static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) return 0; } +/** + * mnt_allow_writers() - check whether the attribute change allows writers + * @kattr: the new mount attributes + * @mnt: the mount to which @kattr will be applied + * + * Check whether thew new mount attributes in @kattr allow concurrent writers. + * + * Return: true if writers need to be held, false if not + */ +static inline bool mnt_allow_writers(const struct mount_kattr *kattr, + const struct mount *mnt) +{ + return !(kattr->attr_set & MNT_READONLY) || + (mnt->mnt.mnt_flags & MNT_READONLY); +} + static struct mount *mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt, int *err) { @@ -4028,12 +4044,12 @@ static struct mount *mount_setattr_prepare(struct mount_kattr *kattr, last = m; - if ((kattr->attr_set & MNT_READONLY) && - !(m->mnt.mnt_flags & MNT_READONLY)) { - *err = mnt_hold_writers(m); - if (*err) - goto out; - } + if (mnt_allow_writers(kattr, m)) + continue; + + *err = mnt_hold_writers(m); + if (*err) + goto out; } while (kattr->recurse && (m = next_mnt(m, mnt))); out: From patchwork Thu Feb 3 13:14:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2FDF4C433FE for ; Thu, 3 Feb 2022 13:14:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350721AbiBCNOy (ORCPT ); Thu, 3 Feb 2022 08:14:54 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:55424 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350720AbiBCNOu (ORCPT ); Thu, 3 Feb 2022 08:14:50 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DA53761812 for ; Thu, 3 Feb 2022 13:14:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE369C340EB; Thu, 3 Feb 2022 13:14:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894089; bh=OUaSff9cDZ+6ABcme6URZJBJhurYNAABavUam3S21C4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QrcTIOHbnbcWnpAL8TP7CE3RoEGkrXTgTvMlnf5AfneQtBw1MhUQOXTGEc9CmQpzS aNi8ZcKWpVZZXdnHzn7TTPe7E6oISrTG6nVe/gCwYAQdNYKMx+t0A/Qc8acqgPvZLk X6GwMsZp0UXRKy8f5t75b+WygQByb98xSntJep8R3YQsxaHT7GAZ6UknFxYWyPCmqm Ni6mZFPNTBIE1wn2cDD/5gd5ihxRHgE9zCmyo3A6e1J/m9OZ2afHMP2Q4+mwgMUqvH QJeq9aXYUoHv3t+5kYUgcDGrux5Ne+OBdiDBNIi8QQ9z0In/KHMe4SbUZiQkh15mbY KcxmvsHdYtlXA== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 5/7] fs: simplify check in mount_setattr_commit() Date: Thu, 3 Feb 2022 14:14:09 +0100 Message-Id: <20220203131411.3093040-6-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1742; h=from:subject; bh=OUaSff9cDZ+6ABcme6URZJBJhurYNAABavUam3S21C4=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsq0sGv5oW2us7yLDGeWSh2cXujR/GHl5ojr1uwO0RtK /y326ihlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZjIdmdGhomHZesTJgma3otfo5erls C4M9NiUdPeL1+arqlrWoYU1DEyrG7Z5ilvlfbqw/pY7dsHFvh3n3j0SkNFqnP2oY9ziytf8QMA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org In order to determine whether we need to call mnt_unhold_writers() in mount_setattr_commit() we currently do not just check whether MNT_WRITE_HOLD is set but also if a read-only mount was requested. However, checking whether MNT_WRITE_HOLD is set is enough. Setting MNT_WRITE_HOLD requires lock_mount_hash() to be held and it must be unset before calling unlock_mount_hash(). This guarantees that if we see MNT_WRITE_HOLD we know that we were the ones who set it earlier. We don't need to care about why we set it. Plus, leaving this additional read-only check in makes the code more confusing because it implies that MNT_WRITE_HOLD could've been set by another thread when it really can't. Remove it and update the associated comment. Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- fs/namespace.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 7e5535ed155d..ddae5c08ea8c 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -4096,13 +4096,8 @@ static void mount_setattr_commit(struct mount_kattr *kattr, WRITE_ONCE(m->mnt.mnt_flags, flags); } - /* - * We either set MNT_READONLY above so make it visible - * before ~MNT_WRITE_HOLD or we failed to recursively - * apply mount options. - */ - if ((kattr->attr_set & MNT_READONLY) && - (m->mnt.mnt_flags & MNT_WRITE_HOLD)) + /* If we had to hold writers unblock them. */ + if (m->mnt.mnt_flags & MNT_WRITE_HOLD) mnt_unhold_writers(m); if (!err && kattr->propagation) From patchwork Thu Feb 3 13:14:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734112 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C00CDC433F5 for ; Thu, 3 Feb 2022 13:14:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350724AbiBCNOz (ORCPT ); Thu, 3 Feb 2022 08:14:55 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:55446 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350726AbiBCNOw (ORCPT ); Thu, 3 Feb 2022 08:14:52 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CFFB16181B for ; Thu, 3 Feb 2022 13:14:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8F1DC340F0; Thu, 3 Feb 2022 13:14:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894091; bh=GNplQJzg1tMapSIoXSa1/+OR/ErS/FSxWWQdn2CUOps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TqpPwQj965I+jzbOa4V3GJVO6MAR7kDfZ5FUkYlHHfMZSdea78SltVy0u8TOh5vLV J1T0vE5ge42nXW9V61Zyx1TTnnU4NRODLqYqaG37TdEoaFyC2gJd6Sp22jV76UalWL 9dP45tuI0pAkZ365iC1yjoafw9XlOvCB2V8XJmH+FbbZDZBHLJOmhjLvB4AMPm/ysR 4nTq1AXswqtztsnf2w2CxRjiPTsAlfa18FR/mmZNitHk9cpNpNJ4vGqabo3sBPU+yG tWhrOCNtGkR/kL4EJjnAjwyBnWh9tYQpEaC9dbVnURUZoOTc1ao8HnZzPqNr/0mLpY Hq8NXn5cXQ8xg== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 6/7] fs: don't open-code mnt_hold_writers() Date: Thu, 3 Feb 2022 14:14:10 +0100 Message-Id: <20220203131411.3093040-7-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1092; h=from:subject; bh=GNplQJzg1tMapSIoXSa1/+OR/ErS/FSxWWQdn2CUOps=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsrUv6fQ+8Uv9ZyvUaICnocmz73y0dTXcrPalN38xd/2 pqof7ShlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZhI13+G/85Prxznz8jkrhWJdYxMb4 n50atmscMtdCf7+0dnEi0On2BkeHzt6u3NhlcfOb6YH3X/ktLNBcUxnHr5BoWH9tleeFY5kw8A X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Remove sb_prepare_remount_readonly()'s open-coded mnt_hold_writers() implementation with the real helper we introduced in commit fbdc2f6c40f6 ("fs: split out functions to hold writers"). Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- fs/namespace.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index ddae5c08ea8c..00762f9a736a 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -563,12 +563,9 @@ int sb_prepare_remount_readonly(struct super_block *sb) lock_mount_hash(); list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { - mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; - smp_mb(); - if (mnt_get_writers(mnt) > 0) { - err = -EBUSY; + err = mnt_hold_writers(mnt); + if (err) break; - } } } if (!err && atomic_long_read(&sb->s_remove_count)) From patchwork Thu Feb 3 13:14:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12734113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC5A1C4332F for ; Thu, 3 Feb 2022 13:14:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350730AbiBCNOz (ORCPT ); Thu, 3 Feb 2022 08:14:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241853AbiBCNOy (ORCPT ); Thu, 3 Feb 2022 08:14:54 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C07BC061714 for ; Thu, 3 Feb 2022 05:14:54 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BE4C26181B for ; Thu, 3 Feb 2022 13:14:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3BA1C340EB; Thu, 3 Feb 2022 13:14:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643894093; bh=ZYDZdJq9E6TdeKXjWJmi2UIy460h1uKegq7T5Lp78H0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VsthdNfdd+S71cKA4lOGT0eoqvqmGr+IoRAcWOLu+7CRggD34qEuLd0PenjoGhzTn uafH045jkx8Up4unH6OuCksfbWjJmttkFykMQ2AiIOToI+ekxusZ+y2CvFJDE4CNic FktuLnUt9KbUom5VXbXAschKqj67G18g1mz4Fi9h/QCcsDL8IVtgctW7mmmw5M+jxi NOwP96Moed23KTmGcuxTtjwoLzOjjzdDJKsTAvI/vcBGj5dgYt56ePzbBJA+N/GZO5 pZSDhgOTgzUQiKfz7iiuATvh1IBGnvdWMGi8U1GXYv9+SkLGrtdt/oI3lrHAjJePSr psuapZRYiRxHw== From: Christian Brauner To: linux-fsdevel@vger.kernel.org Cc: Seth Forshee , Christoph Hellwig , Al Viro , Christian Brauner Subject: [PATCH 7/7] fs: clean up mount_setattr control flow Date: Thu, 3 Feb 2022 14:14:11 +0100 Message-Id: <20220203131411.3093040-8-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203131411.3093040-1-brauner@kernel.org> References: <20220203131411.3093040-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5232; h=from:subject; bh=ZYDZdJq9E6TdeKXjWJmi2UIy460h1uKegq7T5Lp78H0=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST+vsr8OXcuw+2cP0aBOXcTwnZ8Zc/eq546w/7+7+m6QWHz mPROdZSyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWQIAxenAEyk8SQjw/Styszf1kU8ftm4N+W70H RxT5ft7Wu2Z6x/tJCB60vNsgWMDN+/txaJRzzhmNuisSI52XPuPKaHpht+2X+7vuPZG8fP7awA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Simplify the control flow of mount_setattr_{prepare,commit} so they became easiert to follow. We kept using both an integer error variable that was passed by pointer as well as a pointer as an indicator for whether or not we need to revert or commit the prepared changes. Simplify this and just use the pointer. If we successfully changed properties the revert pointer will be NULL and if we failed to change properties it will indicate where we failed and thus need to stop reverting. Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Christoph Hellwig --- fs/namespace.c | 84 ++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 00762f9a736a..0e342e2ade83 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -82,6 +82,7 @@ struct mount_kattr { unsigned int lookup_flags; bool recurse; struct user_namespace *mnt_userns; + struct mount *revert; }; /* /sys/fs */ @@ -4011,46 +4012,34 @@ static inline bool mnt_allow_writers(const struct mount_kattr *kattr, (mnt->mnt.mnt_flags & MNT_READONLY); } -static struct mount *mount_setattr_prepare(struct mount_kattr *kattr, - struct mount *mnt, int *err) +static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt) { - struct mount *m = mnt, *last = NULL; - - if (!is_mounted(&m->mnt)) { - *err = -EINVAL; - goto out; - } - - if (!(mnt_has_parent(m) ? check_mnt(m) : is_anon_ns(m->mnt_ns))) { - *err = -EINVAL; - goto out; - } + struct mount *m = mnt; do { + int err = -EPERM; unsigned int flags; - flags = recalc_flags(kattr, m); - if (!can_change_locked_flags(m, flags)) { - *err = -EPERM; - goto out; - } + kattr->revert = m; - *err = can_idmap_mount(kattr, m); - if (*err) - goto out; + flags = recalc_flags(kattr, m); + if (!can_change_locked_flags(m, flags)) + return err; - last = m; + err = can_idmap_mount(kattr, m); + if (err) + return err; if (mnt_allow_writers(kattr, m)) continue; - *err = mnt_hold_writers(m); - if (*err) - goto out; + err = mnt_hold_writers(m); + if (err) + return err; } while (kattr->recurse && (m = next_mnt(m, mnt))); -out: - return last; + kattr->revert = NULL; + return 0; } static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) @@ -4078,14 +4067,12 @@ static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) put_user_ns(old_mnt_userns); } -static void mount_setattr_commit(struct mount_kattr *kattr, - struct mount *mnt, struct mount *last, - int err) +static void mount_setattr_finish(struct mount_kattr *kattr, struct mount *mnt) { struct mount *m = mnt; do { - if (!err) { + if (!kattr->revert) { unsigned int flags; do_idmap_mount(kattr, m); @@ -4097,24 +4084,24 @@ static void mount_setattr_commit(struct mount_kattr *kattr, if (m->mnt.mnt_flags & MNT_WRITE_HOLD) mnt_unhold_writers(m); - if (!err && kattr->propagation) + if (!kattr->revert && kattr->propagation) change_mnt_propagation(m, kattr->propagation); /* * On failure, only cleanup until we found the first mount * we failed to handle. */ - if (err && m == last) - break; + if (kattr->revert == m) + return; } while (kattr->recurse && (m = next_mnt(m, mnt))); - if (!err) + if (!kattr->revert) touch_mnt_namespace(mnt->mnt_ns); } static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) { - struct mount *mnt = real_mount(path->mnt), *last = NULL; + struct mount *mnt = real_mount(path->mnt); int err = 0; if (path->dentry != mnt->mnt.mnt_root) @@ -4135,16 +4122,31 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) } } + err = -EINVAL; lock_mount_hash(); + /* Ensure that this isn't anything purely vfs internal. */ + if (!is_mounted(&mnt->mnt)) + goto out; + /* - * Get the mount tree in a shape where we can change mount - * properties without failure. + * If this is an attached mount make sure it's located in the callers + * mount namespace. If it's not don't let the caller interact with it. + * If this is a detached mount make sure it has an anonymous mount + * namespace attached to it, i.e. we've created it via OPEN_TREE_CLONE. */ - last = mount_setattr_prepare(kattr, mnt, &err); - if (last) /* Commit all changes or revert to the old state. */ - mount_setattr_commit(kattr, mnt, last, err); + if (!(mnt_has_parent(mnt) ? check_mnt(mnt) : is_anon_ns(mnt->mnt_ns))) + goto out; + /* + * First, we get the mount tree in a shape where we can change mount + * properties without failure. If we succeeded to do so we commit all + * changes and if we failed we clean up. + */ + err = mount_setattr_prepare(kattr, mnt); + mount_setattr_finish(kattr, mnt); + +out: unlock_mount_hash(); if (kattr->propagation) {