From patchwork Sat Sep 14 17:07:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13804444 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51D94C021A7 for ; Sat, 14 Sep 2024 17:07:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D74326B0092; Sat, 14 Sep 2024 13:07:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C810E6B0093; Sat, 14 Sep 2024 13:07:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFFA46B0095; Sat, 14 Sep 2024 13:07:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8A6FE6B0092 for ; Sat, 14 Sep 2024 13:07:36 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3F7E4A9B78 for ; Sat, 14 Sep 2024 17:07:36 +0000 (UTC) X-FDA: 82563975312.23.69D2D09 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id 64600100004 for ; Sat, 14 Sep 2024 17:07:34 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Pl5qlkt/"; spf=pass (imf05.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726333600; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Uu/3uANHfluwJOT0sOVK+YFAiKxotPPufi8sl8a6/Aw=; b=rk4mo/P97dmPctBz/Rf8uCgjNZVr1h7Qv5ze+/2VhtoYVgCfHrQhKM0Nxu6Ul1+kWaFC6k JXCeoWY5yYZwxMo3r1acoF7ULh5tVgmeAkcWDI0h6rYjSWTUx8GhsXBvK+dz10bq0d2DaW Fn7kFGG0y31PXAMi15VH8r+VPib1GIc= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Pl5qlkt/"; spf=pass (imf05.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726333600; a=rsa-sha256; cv=none; b=SMnKWP5OsAOtuysTlOOC8CmVgRWMa8aklb1l3gC434t+fVKsMRh5gmPBqw1tMixC1s0wPg /GIb8ij9ktYXrybaOxdWh3EN5cpNTyHQpwS7Fez0ZxDfpPQ1WA1jZDkuvw+aHWnREae4Sl XkiabjmL+hZwcbIO9Fg7pQriOA+sMZg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B4CA25C5AC9; Sat, 14 Sep 2024 17:07:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78032C4CEC0; Sat, 14 Sep 2024 17:07:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726333653; bh=aSqbB0gVujzisU/y6FV03b+ti8yD2UZ7U4fMZl1YgTo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Pl5qlkt/VRPxlEcECODI0Xew6wnc+fSPdtKQp8YUJed8MYy0e5w2xaPwRcdMBgUEX EpB1GwVipSkYtzT4yrDLNRGOmaESeRR8m1tNAMSKO5hR4QqT1zGET+2Eou0MilSySI /62SgfKM/z80LwjQbkooyLyYx8/YcM4NEjijduCtIUi1nxOwqRJAvxDk0IGFVy489A IizzP7V2GujnZLllrWkxp4cKoqQkdRkijuNJJB3pofH9F6G2JmhIMFY7uTji5J0rxl osd1Y1rOrPAsGiKanTdIrtULRfkQUyOW9EY0W2vuNxYLQ3adHa0xSm1IFwWGFcOOoT 1Y1NG/JInOehw== From: Jeff Layton Date: Sat, 14 Sep 2024 13:07:17 -0400 Subject: [PATCH v8 04/11] fs: handle delegated timestamps in setattr_copy_mgtime MIME-Version: 1.0 Message-Id: <20240914-mgtime-v8-4-5bd872330bed@kernel.org> References: <20240914-mgtime-v8-0-5bd872330bed@kernel.org> In-Reply-To: <20240914-mgtime-v8-0-5bd872330bed@kernel.org> To: John Stultz , Thomas Gleixner , Stephen Boyd , Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Chandan Babu R , "Darrick J. Wong" , Theodore Ts'o , Andreas Dilger , Chris Mason , Josef Bacik , David Sterba , Hugh Dickins , Andrew Morton , Chuck Lever , Vadim Fedorenko Cc: Randy Dunlap , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org, Jeff Layton X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=6206; i=jlayton@kernel.org; h=from:subject:message-id; bh=aSqbB0gVujzisU/y6FV03b+ti8yD2UZ7U4fMZl1YgTo=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm5cLFQ8HK1/W2OQTOc42uOSa7MYW8Wq+Whom2H gQ2acWJQ+SJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZuXCxQAKCRAADmhBGVaC Fc5vEADJP9NxRrVNDWeeviXY5a0IMlU18SPHOde4ugVGgDVCrzzVS05AkoXjAn2YVxLbwxN2ls2 Xt0ndOnQIzwg5/eJrvpZdUlgKhEzMwtV2nsiT6qdBMSK0yeQHxgbbeNb1CUF66GiuTafnxCFO9c sccqTqX75ub/SOPK/89OsWUfBPIJtKcBqOzMeOsgJE17qSOBkXRr0ItxwxbxWc0m4ryqmIvth3z NPeW+VHgfvWPnx/eEK0FKiYh/Bkyeb6GdebifCbkRUEtCIYww4s3QkE+jWy1rb8akDFPD94vooX 5sZ77ScCJaYnb0Jas54ze20L1jQI+XoYIo0MLqjUAIY2cKMIRrzCDXCpoEPPnucksVjxFRJTaj2 mGXAC1vlu/7x+xu9rS8qu3xSUaShw/WCDKAyWkL1WSrd9pNgWenfVdu8FSJBc/3cSxXT93VT5Fk 1LgzBtyh74/qerEJ6ie4twa+D+eVUGmNsLVEg2EnSX63Gk5BXeQTn1sdnf56tpqTz8K5QUhjPQV XPkOopyKtxqzlYrpF7CJyJr8AetoNK9xLXniUW5qwbuDR1Hjqr1vm1EBnXDGk8khK5T27zBmxUb LwkTcemBpaEbcbX/PGbRS3tr6NFaZdf4Koe6U0Mu+rN/k0srUBYIxZlJj/WWHmBlKMrRspT/g+j VIHjLzOS8yubbvg== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 64600100004 X-Stat-Signature: wzj6pnzz1dnsfastwgojsn5m1ybspn65 X-HE-Tag: 1726333654-320783 X-HE-Meta: U2FsdGVkX1/2FJoKZj1LmIuR0fEMqljaJoGhnj9EsZCltkbv11eA3Ktl43pFCI0FbWseS1KO1Q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qntHZp0oXJ9yPKvHZ6miIZVp3CZQSldTHbWc9cob/l3zXjh3F4j/0oqLORv6YfkGHAJ05dj3tDgOiNZN67dc3Evl57lOsNATMp+qKhfnaVe8SosApFVmYfIIezyFrHZUBa3kv0We5nBg676GFP/sazOlgtc50nAKoK8W17rTSorEzQ7IBPxQtd0fijMK59cTwCJoRcPv7DHl5Vfz+dm+1h0vU/jrbPnEDrBDA0dXFdtVXG8bgj70aH1iHgdUkKaNbOiV7MRQtnqdEFBTSobDfQ192J0QnBD7N5 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When updating the ctime on an inode for a SETATTR with a multigrain filesystem, we usually want to take the latest time we can get for the ctime. The exception to this rule is when there is a nfsd write delegation and the server is proxying timestamps from the client. When nfsd gets a CB_GETATTR response, we want to update the timestamp value in the inode to the values that the client is tracking. The client doesn't send a ctime value (since that's always determined by the exported filesystem), but it can send a mtime value. In the case where it does, then we may need to update the ctime to a value commensurate with that instead of the current time. If ATTR_DELEG is set, then use ia_ctime value instead of setting the timestamp to the current time. With the addition of delegated timestamps we can also receive a request to update only the atime, but we may not need to set the ctime. Trust the ATTR_CTIME flag in the update and only update the ctime when it's set. Signed-off-by: Jeff Layton --- fs/attr.c | 28 +++++++++++++-------- fs/inode.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index 3bcbc45708a3..392eb62aa609 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -286,16 +286,20 @@ static void setattr_copy_mgtime(struct inode *inode, const struct iattr *attr) unsigned int ia_valid = attr->ia_valid; struct timespec64 now; - /* - * If the ctime isn't being updated then nothing else should be - * either. - */ - if (!(ia_valid & ATTR_CTIME)) { - WARN_ON_ONCE(ia_valid & (ATTR_ATIME|ATTR_MTIME)); - return; + if (ia_valid & ATTR_CTIME) { + /* + * In the case of an update for a write delegation, we must respect + * the value in ia_ctime and not use the current time. + */ + if (ia_valid & ATTR_DELEG) + now = inode_set_ctime_deleg(inode, attr->ia_ctime); + else + now = inode_set_ctime_current(inode); + } else { + /* If ATTR_CTIME isn't set, then ATTR_MTIME shouldn't be either. */ + WARN_ON_ONCE(ia_valid & ATTR_MTIME); } - now = inode_set_ctime_current(inode); if (ia_valid & ATTR_ATIME_SET) inode_set_atime_to_ts(inode, attr->ia_atime); else if (ia_valid & ATTR_ATIME) @@ -354,8 +358,12 @@ void setattr_copy(struct mnt_idmap *idmap, struct inode *inode, inode_set_atime_to_ts(inode, attr->ia_atime); if (ia_valid & ATTR_MTIME) inode_set_mtime_to_ts(inode, attr->ia_mtime); - if (ia_valid & ATTR_CTIME) - inode_set_ctime_to_ts(inode, attr->ia_ctime); + if (ia_valid & ATTR_CTIME) { + if (ia_valid & ATTR_DELEG) + inode_set_ctime_deleg(inode, attr->ia_ctime); + else + inode_set_ctime_to_ts(inode, attr->ia_ctime); + } } EXPORT_SYMBOL(setattr_copy); diff --git a/fs/inode.c b/fs/inode.c index 232b474218e6..614d0402e9ad 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2715,6 +2715,78 @@ struct timespec64 inode_set_ctime_current(struct inode *inode) } EXPORT_SYMBOL(inode_set_ctime_current); +/** + * inode_set_ctime_deleg - try to update the ctime on a delegated inode + * @inode: inode to update + * @update: timespec64 to set the ctime + * + * Attempt to atomically update the ctime on behalf of a delegation holder. + * + * The nfs server can call back the holder of a delegation to get updated + * inode attributes, including the mtime. When updating the mtime we may + * need to update the ctime to a value at least equal to that. + * + * This can race with concurrent updates to the inode, in which + * case we just don't do the update. + * + * Note that this works even when multigrain timestamps are not enabled, + * so use it in either case. + */ +struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 update) +{ + struct timespec64 now, cur_ts; + u32 cur, old; + + /* pairs with try_cmpxchg below */ + cur = smp_load_acquire(&inode->i_ctime_nsec); + cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED; + cur_ts.tv_sec = inode->i_ctime_sec; + + /* If the update is older than the existing value, skip it. */ + if (timespec64_compare(&update, &cur_ts) <= 0) + return cur_ts; + + ktime_get_coarse_real_ts64_mg(&now); + + /* Clamp the update to "now" if it's in the future */ + if (timespec64_compare(&update, &now) > 0) + update = now; + + update = timestamp_truncate(update, inode); + + /* No need to update if the values are already the same */ + if (timespec64_equal(&update, &cur_ts)) + return cur_ts; + + /* + * Try to swap the nsec value into place. If it fails, that means + * we raced with an update due to a write or similar activity. That + * stamp takes precedence, so just skip the update. + */ +retry: + old = cur; + if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) { + inode->i_ctime_sec = update.tv_sec; + mgtime_counter_inc(mg_ctime_swaps); + return update; + } + + /* + * Was the change due to someone marking the old ctime QUERIED? + * If so then retry the swap. This can only happen once since + * the only way to clear I_CTIME_QUERIED is to stamp the inode + * with a new ctime. + */ + if (!(old & I_CTIME_QUERIED) && (cur == (old | I_CTIME_QUERIED))) + goto retry; + + /* Otherwise, it was a new timestamp. */ + cur_ts.tv_sec = inode->i_ctime_sec; + cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED; + return cur_ts; +} +EXPORT_SYMBOL(inode_set_ctime_deleg); + /** * in_group_or_capable - check whether caller is CAP_FSETID privileged * @idmap: idmap of the mount @inode was found from diff --git a/include/linux/fs.h b/include/linux/fs.h index eff688e75f2f..ea7ed437d2b1 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1544,6 +1544,8 @@ static inline bool fsuidgid_has_mapping(struct super_block *sb, struct timespec64 current_time(struct inode *inode); struct timespec64 inode_set_ctime_current(struct inode *inode); +struct timespec64 inode_set_ctime_deleg(struct inode *inode, + struct timespec64 update); static inline time64_t inode_get_atime_sec(const struct inode *inode) {