From patchwork Wed Aug 30 18:15:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13370811 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 6AD11C83F14 for ; Wed, 30 Aug 2023 22:48:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243822AbjH3WsX (ORCPT ); Wed, 30 Aug 2023 18:48:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243834AbjH3WsW (ORCPT ); Wed, 30 Aug 2023 18:48:22 -0400 Received: from outbound-ip7a.ess.barracuda.com (outbound-ip7a.ess.barracuda.com [209.222.82.174]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 677E3E63 for ; Wed, 30 Aug 2023 15:48:05 -0700 (PDT) Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2173.outbound.protection.outlook.com [104.47.57.173]) by mx-outbound11-128.us-east-2a.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 30 Aug 2023 22:48:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YAFVlAm60K5C09gOx4UmY3JhZZcKl8nZFSkVLV44QVqp8pYi8Bq47bVJSRbD5K3zxBgt6phdpOtw6FMBcn4UrrPnTrWj9ZMDCheBRNGsohDdfZLmIhzcZ2w9kIBBJNCg8YGXnZtIQXSLtPJhVxGCiqN9XV6qlErIY4z5sWenOwHnSQ3omHac+iRUyNNPPM8nuLoQHjCxeFs1SGSg+Elg9FQhtTZ3xg4tSirnVSIhJ4T+kV4nfSeLMU4bUGX0X+aRCKgD4p31a7eMATkvi1UiV/voNDymj0LioKmc4zIsAz8qOaOK9u4tgHV/u+r0JIweyrAdnbjSi1gNmAX3UT8LIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=tF1eSqoL5JhboBKNpfZY3n9Bv/tC68B4oK8Zk/vMHK4=; b=jFJ4u8VYOT2r2KLiFIIxRA4cB/TNgRFKcUESFkZzgt/D7TyKNWXl3ZRy8pY7aTer+Rz9UZrAsFZDonmI0UVoX84lOZhQWztL1oi5duzftMWg89ix6TcAxxkSF4lNMvBYF0XIGDhg5KUO/IyuD8kTzHvTn1gVcfYwPE4mDED0GoR5xBxdSu8QvWrms+LVD3EDO3bJf55wztsdNOPqoBJ28RYutATJzBBi+qM5LWrTa/u3B5GmG2P++klbvpB9cN4rnvsr8JCyqEMvjm96mQsNjzUTEX+xK2J7Gd04RZeYR6h8ZxizGyarMjdVQlgryd8Nwu6+njwu/Qms7VHTLvou9w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tF1eSqoL5JhboBKNpfZY3n9Bv/tC68B4oK8Zk/vMHK4=; b=xRJvUdcZa38aE8R7O8bbvnNut42Ai8VegZOpbOyDL2iRysgcuPspASbiAzumFRHV70gIeqet4Q3tZ/AnHB5f2AGh44hEl3oAqrzpFUi3TG9Ume6cOlN7GNPPIC8fSSSClc1mbLFr5l3a8orS5yQ860+lE+JG43Wu99pwY0I6HTE= Received: from BN0PR04CA0039.namprd04.prod.outlook.com (2603:10b6:408:e8::14) by DS7PR19MB6422.namprd19.prod.outlook.com (2603:10b6:8:95::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.35; Wed, 30 Aug 2023 18:15:25 +0000 Received: from BN8NAM04FT019.eop-NAM04.prod.protection.outlook.com (2603:10b6:408:e8:cafe::92) by BN0PR04CA0039.outlook.office365.com (2603:10b6:408:e8::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6745.20 via Frontend Transport; Wed, 30 Aug 2023 18:15:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by BN8NAM04FT019.mail.protection.outlook.com (10.13.160.221) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6745.20 via Frontend Transport; Wed, 30 Aug 2023 18:15:24 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 6CA1420C684C; Wed, 30 Aug 2023 12:16:30 -0600 (MDT) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, Bernd Schubert , Josef Bacik , linux-btrfs@vger.kernel.org, Alexander Viro , Christian Brauner Subject: [PATCH 1/2] fs: Add and export file_needs_remove_privs Date: Wed, 30 Aug 2023 20:15:18 +0200 Message-Id: <20230830181519.2964941-2-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230830181519.2964941-1-bschubert@ddn.com> References: <20230830181519.2964941-1-bschubert@ddn.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM04FT019:EE_|DS7PR19MB6422:EE_ X-MS-Office365-Filtering-Correlation-Id: 003ba8fd-34f6-48a2-1c5b-08dba98514f1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RVacYzyqnXuXurDUMIXi0ebCJvBi6Sv9OfXk2rqnV5i73K1rUOa9qOOS/zLxB9oBIny2wcjfBiNogy4ZMCF0EEHT4p01i/QpbfoIueqPQ8uCMulo3PvbUGTtB1wyGSfq/86H1w/lJcFhl5UrEDRzm8dRjVYOuumwu5NCrxi8+cbROV/OA99Tz6RSmdd7fCAIu82FWCHUV9JXIkYeabhUgpLIacg37cc+yjKREXJnFF2qYoI7nVY7rBDKkktmTb0JR/lRTTofXAQmLXxY5j64kHnHpWqa2VuUJbl/S0bzVZVcOyf0kkyF/9SzA1L3rAPYN1CxzTV7CRxbCV0NYyeNdPeJQUlWPodkKqdO3ZE9p6kBSdDl6jbGfcwBr78rP4255CmpgvWVMSugfq6+zwrktTuhMdedTjHdtAt0OC42T1IQHM96ussmdIWt2Sfgsm5t6JeWBxUbq4tqPKKjYelQH/729gPx6NLKFQ7xIkEx4vvhSeS6/BTKzCQbMvDXXRaWGezvXatqKDb9fsenYPbjTNpTrli2A2nyRRifQVk29jpLXjumQu9iDy9/xgBEbN29/4SV3grOR22YGTT93cD9HydsbjjBMmrVSYAS13xe3Lm2sqkMl/LFR3ZECRz3Z2CCvOQNlC6ifsv4rDlgywPdIceTye9Gt/wD9jR56fnBdIR2cNW4Qq95UWvvbL7yJA3n/zPQ3Y0ZsWDtJPiGptWtU/Sf/7xIHosWN9nOOl+AuHD8iPfHFyCPV5CldLdo+n1K X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(396003)(376002)(39850400004)(136003)(346002)(82310400011)(451199024)(1800799009)(186009)(46966006)(36840700001)(316002)(41300700001)(336012)(4326008)(6266002)(5660300002)(6916009)(2906002)(47076005)(83380400001)(2616005)(26005)(8676002)(86362001)(36756003)(1076003)(36860700001)(40480700001)(8936002)(6666004)(82740400003)(356005)(81166007)(54906003)(70206006)(70586007)(478600001)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: B7f73LEUFFBR/m2lnX/nR+OKHU0OAId54qEZNYTJJuRu2amY0EYbT4aZClDJ3bGAKixbkls6BuHfDTvL36VewOyTvZWHL24avfYHSG+JdcdHpekFOI0rMiD8PMQP/Dr6gVOEIn+wcbu8r2Ztinp8vugmbjZjFB4V/+beOBlU7p35AF3GALIDYp3S0VM3KEyjopPc7MuIPHaEnbrJ8CbM9jy0bl/sGsOSePIhmkaWFto3KxI00BdXkupDjcyAQ6/E3kGq15I8iTHuJLt3wbwQyuzoZ3LUbkdrR7bWsGmVD6co8EN0Ep0Va3YSPknxd+ykUnQM9vg7RDYFJXL/45pwHVN00kKKAldjsiVXG8PC7MEITPIufoQa/MOZvYO8v/qZwbtgrhU32w+BldSVchc+mQfgjAEQxkAUc0DmhOQMYADrXSfXVRUQMdqLHJHmcqiRDS5mtawOPBMuzrmQngqOnkJHwY9YarKV9DD22QCBy2LZJlgyjEHEpNZrd2fKFA0XpEjyh8XrRHxkCCkONg7x8tChibFmn3s7Hoao8Sumnns427GzpnQNKsvTwNWaKpkOzk8QgIa8YYrgiTlrpu9IIqKIBnVsZIj8iO3U706hmuUOUE8hGbdTQ9s5OaUnhu+JgoZdYyEg9hQBwS+bK5pLsx+jVL7D+G/cDv0TzQnULTOihinXwJDDDLBCq/drR5wU6PKHYUjoST/9COPJ4/nHzjSpxSzkO6eQFiPzVbex9u7dru40NQB2+8uHLpd3s7AfrC7ohd5NhceKWcUU0dQ+V1YH3akb3FHg9IVaAzzXG7i+fhiy8+0rgOz/AiU0QZyip79mnc63RuxxEDU0Za/A0UKeBf1oWzm+i3+thja9/N0jjPnYCOAMZ8b9HKXdOFGrE+X2RWsxx4vJt7qBTGu5JcyEC8eQgBjSp9QtoDy/7c6orlidzJ91NBr99mfPGBxACqKL6Rltd4HFiMUaXBOHKpvTEVnZ8V26wsA/KTuIpdM= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Aug 2023 18:15:24.8741 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 003ba8fd-34f6-48a2-1c5b-08dba98514f1 X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM04FT019.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR19MB6422 X-OriginatorOrg: ddn.com X-BESS-ID: 1693435684-102944-23009-14193-1 X-BESS-VER: 2019.1_20230830.2058 X-BESS-Apparent-Source-IP: 104.47.57.173 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVobm5hZAVgZQ0CTZONnUIMnIJM 0szTzN2CQ5Lc3AxCTVJDXRyCDFICVJqTYWAHLaTghBAAAA X-BESS-Outbound-Spam-Score: 0.50 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.250500 [from cloudscan14-178.us-east-2a.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.50 BSF_RULE7568M META: Custom Rule 7568M 0.00 BSF_BESS_OUTBOUND META: BESS Outbound 0.00 BSF_SC0_MISMATCH_TO META: Envelope rcpt doesn't match header X-BESS-Outbound-Spam-Status: SCORE=0.50 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_RULE7568M, BSF_BESS_OUTBOUND, BSF_SC0_MISMATCH_TO X-BESS-BRTS-Status: 1 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an exclusive lock. The new export function file_needs_remove_privs() is added in order to first check if that is needed. Cc: Miklos Szeredi Cc: Dharmendra Singh Cc: Josef Bacik Cc: linux-btrfs@vger.kernel.org Cc: Alexander Viro Cc: Christian Brauner Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Bernd Schubert --- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+) diff --git a/fs/inode.c b/fs/inode.c index 67611a360031..9b05db602e41 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap, return mask; } +int file_needs_remove_privs(struct file *file) +{ + struct dentry *dentry = file_dentry(file); + + return dentry_needs_remove_privs(file_mnt_idmap(file), dentry); +} +EXPORT_SYMBOL_GPL(file_needs_remove_privs); + static int __remove_privs(struct mnt_idmap *idmap, struct dentry *dentry, int kill) { diff --git a/include/linux/fs.h b/include/linux/fs.h index 562f2623c9c9..9245f0de00bc 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2721,6 +2721,7 @@ extern struct inode *new_inode_pseudo(struct super_block *sb); extern struct inode *new_inode(struct super_block *sb); extern void free_inode_nonrcu(struct inode *inode); extern int setattr_should_drop_suidgid(struct mnt_idmap *, struct inode *); +int file_needs_remove_privs(struct file *); extern int file_remove_privs(struct file *); int setattr_should_drop_sgid(struct mnt_idmap *idmap, const struct inode *inode); From patchwork Wed Aug 30 18:15:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13370770 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 5F4A8C83F01 for ; Wed, 30 Aug 2023 22:46:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344177AbjH3Wqr (ORCPT ); Wed, 30 Aug 2023 18:46:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344110AbjH3Wqq (ORCPT ); Wed, 30 Aug 2023 18:46:46 -0400 X-Greylist: delayed 16254 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 30 Aug 2023 15:46:30 PDT Received: from outbound-ip7a.ess.barracuda.com (outbound-ip7a.ess.barracuda.com [209.222.82.174]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 262B0C5 for ; Wed, 30 Aug 2023 15:46:29 -0700 (PDT) Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2043.outbound.protection.outlook.com [104.47.73.43]) by mx-outbound45-208.us-east-2c.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 30 Aug 2023 22:46:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AkaXen7/n8vAWbOMDuhtCSxXfhPRChUSC2vMgapd8d4o5QXQB4GRyEVbCzLCGg2uwfN2iKu2vkiPdqsuO98VcI2KJfgs/CcIADWDVGsO7/beTs/NbfpskH3CFTOWk1LeNTpHNtHc6fuwmJDeye0cArMti8vBoiOoOor3naLAYTG+//jSrGq1NGx+wedAUlqS5qHoKVvxYStwZPzqnB4tErT5pyi6/gpm803vihT8IplEZNNYEa0QcGxB+Exhy94i5mzE2PeftZOXte7BaoT78EklirwRh8pkw/aZJMTF0jRWf4ta2C8wXFz5BAzXcMiPnquCVhkCyYDd0zH9bX8oDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JH1IRVqwi5yaAlGJWt4Jx4JOmTtWYWQE7IEL5yxEOSw=; b=jxf2OlfMKQiXhiNKalq9Qc0292G1h1OLpCRTdPwjjGWS4L6yV2gJ4ExcOBt25q7qQoqaXuI1UZI3/wgkeo1tdHnzxke/ELc5gbqjemxxYQ3rROp6C93JcQLVvuRVnR55USJnvZLhI58e6fB7jbO0f8PRyIft6FwAP0xyfZ6FcASaFVeTF/pZqeKmj4u5IHvQCYhRqhGkdCoL9LpGh+OZ5QrO+wLwf0WLOevIHBg5uIeBgi0lrrAVeqsDnRwFkq7+P25rVjnVnL2mXap9CKfljUgwZouGIZdyZ0Z+pwd2JX9wBIPBKgS4uplh5R9kygOD75mscGqMlyBAyLSjTtq7Hw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JH1IRVqwi5yaAlGJWt4Jx4JOmTtWYWQE7IEL5yxEOSw=; b=VMvvXNbrPlP1xgZPeznqXxF2z58MT6sqaLqOLmaH11mO9RLC3qs4ttWcFFRb++LsZNambfeR8XYTBsbNgHI7IPGYScvrarKvP8mhZrawe5KRTqjI/nhsxlBvaS6oP2MG81ClnQ7ciJs6o2PdrDeUciYdau5ODui4VJJJCTx8gVs= Received: from SJ0PR05CA0017.namprd05.prod.outlook.com (2603:10b6:a03:33b::22) by SN7PR19MB7733.namprd19.prod.outlook.com (2603:10b6:806:2eb::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.34; Wed, 30 Aug 2023 18:15:26 +0000 Received: from MW2NAM04FT067.eop-NAM04.prod.protection.outlook.com (2603:10b6:a03:33b:cafe::9c) by SJ0PR05CA0017.outlook.office365.com (2603:10b6:a03:33b::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6745.18 via Frontend Transport; Wed, 30 Aug 2023 18:15:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by MW2NAM04FT067.mail.protection.outlook.com (10.13.31.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6745.21 via Frontend Transport; Wed, 30 Aug 2023 18:15:25 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 691F020C684D; Wed, 30 Aug 2023 12:16:31 -0600 (MDT) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, Bernd Schubert , Goldwyn Rodrigues , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs: file_remove_privs needs an exclusive lock Date: Wed, 30 Aug 2023 20:15:19 +0200 Message-Id: <20230830181519.2964941-3-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230830181519.2964941-1-bschubert@ddn.com> References: <20230830181519.2964941-1-bschubert@ddn.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MW2NAM04FT067:EE_|SN7PR19MB7733:EE_ X-MS-Office365-Filtering-Correlation-Id: c490965c-38c2-402d-9eb4-08dba9851571 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9XojB2xQSsmwN2dWbmsdfCwcp7f3IRFK5dTrcUIeZyh6Z7rAN8K5Re3kmKOk2Sf6q2Ht3YEiCYvLIXReaAvAv6rkMfJr2qNwsSh20Z6k0VU3QNFMeEi4QFsJik4oV7Rzwf29yC5n3IFE11O0wZJE+d6FbalhLjacMn5uFv+ni2E/rtQTy+g1Up8LSkoXkI2Q+JpLAclbgw6VDSa56CSQjHkVkL/7V6zR8FvP2NM5DckSFU8omOcsEgayaLq+aBeJ8ZJJU9alM7yJFoSktPkpsm0kRWNHjYxV2Yy9P82WFDsnWqkKpGRgTstHrobMdL0TdAz25jzPHg0RJfZySJmOCf+8Tz7/yNi1STzMgWGF/s7+nHsXQITZeNSh7M2LXMZXAMiQEynhVOhxtfrNAz7GQjSUZHwcm7PHMYxUqQUGXaWmNNxOMyxOENgX33jElRwJzA945ZFN92aFNTmk3sJ6k2I7rnJuuTpssWgvZHMI36aHTQKzCWbtPXImgwiYMOet++QJS1wTxpCnx86uVipmGqAbCXsuPQKUjxv623RzyCS4gwVWqI4bER4kjVlTLb05alNt4dD8P+IzeiFD9Eyo+SitbYIhNNmWsab2jWKuhX/fXDdRVZGzDXHFobkgB3WmgxX7fmSwtVmVjPrmTomprDE8VcFbX7T/cMT1uEPIDd3WFe5E6+rUR8eSm7mWJQ8MyuKEM0lqiS/yKelgiXkEUrIrZ83k8mwi9r86fZlOFmNd90ig5B4u3GIrachRSi+e X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(376002)(136003)(346002)(396003)(39850400004)(186009)(1800799009)(451199024)(82310400011)(46966006)(36840700001)(36860700001)(41300700001)(26005)(356005)(81166007)(82740400003)(6666004)(86362001)(83380400001)(478600001)(47076005)(336012)(1076003)(6266002)(2616005)(40480700001)(70206006)(70586007)(6916009)(2906002)(54906003)(36756003)(316002)(5660300002)(4326008)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 4eKDDvpAC7dG0DgGBo6jE5/RluR9IXqniC9Fr96kJQj1sAtugIlhOuaR8V7eKCJb1GGnDVGmgTz/noPkQdYleh76GkB93YaNvZkLoJ3yF4j5Yi11Nr/ElOU5Tz3r+Xcdh/4mPndF8OhFDplBoADf+XK5Xxt+gMUP6duRz++kIu5scajZxKGKi7y3EnjiAocEFOn0JNZWNP0B83oAc9U4brdscin/Yyy3AmfQpZK59NxzcB1H6IYaqp0jvyKTrYa+nCwRMRru5kTyQ/F8k8MbTbS0XbWJrcXLv6TPsxlARTigfcGWzWvWWkgGHxFEH7jgLYoxybEnm6m14tVJjIrhdNLHffN4GI4htjIWg6e8IGgqTlAEX8mLj1RfQ5PT31v9wEKSi5mT7JeaWoFt0zRY8J9OlQ8UT/EXZ4GOopAAthk0E5kCzfXqW18csKMbLIP6o98sAbVAfeb0wckisNyy9RLLX8k0A6e9k4lfiRrrXrhK+QiC4QCst0e4HPwUJndCRGnfUYdCurgRskUvISey36KOpB9ntUzg17d/EaRUKL2zChoKS/QddOR2wTejPxr1Vr+uO2cLkdSZVylaZueK0DChl4rNN+B8Y6FJAR0anYbfUD+yfeDpSj4cSW0of+5heW37sLuODLpVkv4NdDAcQV0iZLJi4mzadj4uWCavsKokfGnJkWYiSqitttO41LWBrkNB2RzPS8N5ub2Q9BJ54fL27Ju9CPfB2CP3Kil3Se2WSA4JcE+Y4TYyG+738+jObX1LSbd+8/PtKrZ1rdb8FFRlt7rTMbe3Oe55kZPghPF9Ancd1jP72zjULeZLYtzdOfLJwbnFCs+ARS19UB9ZwYiDzZeznwZhvERztRE+jCcCL6/+tHVT5QGzqBpoJOiHfYaNaka1AoA0muZQ+KZyAn4HSdqURcOyYXP7MfsxdskdEiEYRO/mrrvMmv5yi9cB X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Aug 2023 18:15:25.7631 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c490965c-38c2-402d-9eb4-08dba9851571 X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: MW2NAM04FT067.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR19MB7733 X-OriginatorOrg: ddn.com X-BESS-ID: 1693435588-111728-27806-5975-1 X-BESS-VER: 2019.1_20230830.2058 X-BESS-Apparent-Source-IP: 104.47.73.43 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKViYG5kZAVgZQ0DIt2TLNzMgo2T A5KS3NIC3RzDTVwsQwLTU5NdHMOM1AqTYWAOYOC11BAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.250500 [from cloudscan11-241.us-east-2a.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound 0.00 BSF_SC0_MISMATCH_TO META: Envelope rcpt doesn't match header X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND, BSF_SC0_MISMATCH_TO X-BESS-BRTS-Status: 1 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org file_remove_privs might call into notify_change(), which requires to hold an exclusive lock. In order to keep the shared lock for most IOs it now first checks if privilege changes are needed, then switches to the exclusive lock, rechecks and only then calls file_remove_privs. This makes usage of the new exported function file_needs_remove_privs(). The file_remove_privs code path is not optimized, under the assumption that it would be a rare call (file_remove_privs calls file_needs_remove_privs a 2nd time). Fixes: e9adabb9712e ("btrfs: use shared lock for direct writes within EOF") Cc: Goldwyn Rodrigues Cc: Miklos Szeredi Cc: Dharmendra Singh Cc: Chris Mason Cc: Josef Bacik Cc: David Sterba Cc: linux-btrfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Bernd Schubert --- fs/btrfs/file.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index fd03e689a6be..89c869ab131d 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1125,7 +1125,7 @@ static void update_time_for_write(struct inode *inode) } static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from, - size_t count) + size_t count, unsigned int *ilock_flags) { struct file *file = iocb->ki_filp; struct inode *inode = file_inode(file); @@ -1145,9 +1145,17 @@ static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from, !(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) return -EAGAIN; - ret = file_remove_privs(file); - if (ret) - return ret; + ret = file_needs_remove_privs(file); + if (ret) { + if (ilock_flags && *ilock_flags & BTRFS_ILOCK_SHARED) { + *ilock_flags &= ~BTRFS_ILOCK_SHARED; + return -EAGAIN; + } + + ret = file_remove_privs(file); + if (ret) + return ret; + } /* * We reserve space for updating the inode when we reserve space for the @@ -1204,7 +1212,7 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, if (ret <= 0) goto out; - ret = btrfs_write_check(iocb, i, ret); + ret = btrfs_write_check(iocb, i, ret, NULL); if (ret < 0) goto out; @@ -1462,13 +1470,16 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) ssize_t err; unsigned int ilock_flags = 0; struct iomap_dio *dio; + bool has_shared_lock; if (iocb->ki_flags & IOCB_NOWAIT) ilock_flags |= BTRFS_ILOCK_TRY; /* If the write DIO is within EOF, use a shared lock */ - if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode)) + if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode)) { ilock_flags |= BTRFS_ILOCK_SHARED; + has_shared_lock = true; + } relock: err = btrfs_inode_lock(BTRFS_I(inode), ilock_flags); @@ -1481,8 +1492,17 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) return err; } - err = btrfs_write_check(iocb, from, err); + /* might uset BTRFS_ILOCK_SHARED */ + err = btrfs_write_check(iocb, from, err, &ilock_flags); if (err < 0) { + if (err == -EAGAIN && has_shared_lock && + !(ilock_flags & BTRFS_ILOCK_SHARED)) { + btrfs_inode_unlock(BTRFS_I(inode), + ilock_flags | BTRFS_ILOCK_SHARED); + has_shared_lock = false; + goto relock; + } + btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); goto out; } @@ -1496,6 +1516,7 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) pos + iov_iter_count(from) > i_size_read(inode)) { btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); ilock_flags &= ~BTRFS_ILOCK_SHARED; + has_shared_lock = false; goto relock; } @@ -1632,7 +1653,7 @@ static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from, if (ret || encoded->len == 0) goto out; - ret = btrfs_write_check(iocb, from, encoded->len); + ret = btrfs_write_check(iocb, from, encoded->len, NULL); if (ret < 0) goto out;