From patchwork Tue Oct 1 10:58:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13817777 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 4C706CE7D14 for ; Tue, 1 Oct 2024 10:59:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC08A280074; Tue, 1 Oct 2024 06:59:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4847280068; Tue, 1 Oct 2024 06:59:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A26B2280074; Tue, 1 Oct 2024 06:59:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 79C39280068 for ; Tue, 1 Oct 2024 06:59:24 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 364FF120FCD for ; Tue, 1 Oct 2024 10:59:24 +0000 (UTC) X-FDA: 82624737048.22.8E89765 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf08.hostedemail.com (Postfix) with ESMTP id 79745160019 for ; Tue, 1 Oct 2024 10:59:22 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iouMPU0k; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727780342; a=rsa-sha256; cv=none; b=EeLuu9gftcYoJ0gXXlJA8GB76Q8zqKqxqZJPO4GX9EP9nAts6iXQdgBTkgleU8+FB84+6b vLOhw0V0BADIiWL/xFmUm8iovmTc5nkg2kAduohmbAsahwK4ONmMNupja07/Ubz5UO2Kr+ 3GY/Wv/n2/sBByBG5LKZHS1UL0rc3GQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iouMPU0k; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727780342; 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=M9B4Ipm5j8bxXr4of9Ewzaksl/6V7W9qA18FLGIis8o=; b=qwgwko+WiX41q2lvdNv74cPUIaKfLpHraFtc1oNNQ4GTn8+7sRYRb/WoHnVTBHFm6NkTdW Tha92mgT0vAdA0ku2ikixBCGKGLbJH6hyzG6WA3OWMHV5SsqIyHIlb9LLNxWPp9rPC8yYK GjnUmnTWPpc8A/S94ax+t6cUrMFXOSw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 8E060A434DC; Tue, 1 Oct 2024 10:59:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B8A4C4CED3; Tue, 1 Oct 2024 10:59:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727780361; bh=otxFDOKr6FG8LsCMnriOfpK/JOFC4LtwAGxd1Ko/vgM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iouMPU0kTWC6M8npPfU0q2wFskriO4LNOr8BhfkrxeQrTmfuwtAU2qX8aV59H/EWu yb1y5W49N4SFLIMhwFa+ZA7jQ26HCsz5y1Nc1/f9BCH1gRee7b8ynledplAnFSI57V 263wZ+4IpzwtwIGp8fEeauEOLFnV256F8asITtggzLheTwjSCtfKX1pg8YVm5qukWP lR3lBRAAc91/iOB/suVkMLg2fT2MetTV1GW+6qmAbhHvlpZ3z36l5wb97Wsas8nXyn NGwYXVDOZogH8ePpV0Ue1A/e52jiyh0d2BqZycje35KAPLWN2/Qs/mw30lkiZBn5rF NPJcQXpRtFaMQ== From: Jeff Layton Date: Tue, 01 Oct 2024 06:58:58 -0400 Subject: [PATCH v8 04/12] fs: handle delegated timestamps in setattr_copy_mgtime MIME-Version: 1.0 Message-Id: <20241001-mgtime-v8-4-903343d91bc3@kernel.org> References: <20241001-mgtime-v8-0-903343d91bc3@kernel.org> In-Reply-To: <20241001-mgtime-v8-0-903343d91bc3@kernel.org> To: John Stultz , Thomas Gleixner , Stephen Boyd , Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Randy Dunlap , 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: 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=6276; i=jlayton@kernel.org; h=from:subject:message-id; bh=otxFDOKr6FG8LsCMnriOfpK/JOFC4LtwAGxd1Ko/vgM=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm+9X64D3EHKo2ySeez80WVHd3oCsvm+pNbsgmR 0aOttENCLGJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZvvV+gAKCRAADmhBGVaC FYm7D/4qzJJQm9TmX5xrE7NpsLG94CdIeCac47fLnlVxmf3t3Mjkt5I1L2v0j5u5Af3l2qg3p8i is8M/pFHGfva0Tvm6zCYQyajc1TFOSk1CnHSGbN44zHC3XftW2w+aT7wEleS46FU22+/HmaaQ4d icXVwKy09Hvr8Nmz/pmbP8ADLuWjSnoga/0bhJdOanUvf1WzsV7RgH0lI+I/Mf5r25YH9QmQe18 +ZDRGPs4SBuwzQ6L0CR6pQmSuGHCvo11mEDnulJuEW0MnmkS7L+Q0MJJq8izDNCZAlVF3NqwN0C f3u+OygAKquCyKT8dugS9lqkEOXJBhi5OZhDZdSBGxOkXQiwb0gy5tLMkPhZ8K2g8pLzKeA6KjA AtoR4crhk1WNXcU4nzjhBDgS1wB+idQe8CHHyoTGvy5j97RCr6d+hNVFhEDtAK1HWd7bRJG6dzE b9PhO56VnY1bDWMLHOJCSWf1qgCyVVcHv+Ma+7Z21qnNI/YKo6VVjfj5fuisKZX4QxhHGVgDZv1 pXXPdbO8sttx+ccUD9hwj0etd8WW5IyqRpvbtJFus50FIxV7DVjvMUPPUJ8fjeTSAhnhh8M550A RotKZi9jt8ytEzPXSq/+XyCry209lsk44xqqR5YNZb6WMGC30+UeZrUPJBsNoRdJyuQ1fGjDzl9 CAsXrWV3wpcbqhA== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspam-User: X-Stat-Signature: 5n5s4a4yjr431tqqmd3zq74hax76ka6q X-Rspamd-Queue-Id: 79745160019 X-Rspamd-Server: rspam02 X-HE-Tag: 1727780362-291897 X-HE-Meta: U2FsdGVkX1/0ItLobAj10+/ocWUZ51BL5/R74z0T7e1WTaWu0zOA01urfLTjjVtHeGtOi9Yeuq68nggaXaqd0S6UY0/WHXm/Atd6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1SmrhYUxtQk8cq87kyyRIitEA697W1/H1MFxPRfH9M1ihnEZMtRm+3Y9CJvlYSjIv8aFZmeUvGjDoV6hdM2gTk9rD6lRy71qkVwMzSyglF4fYw8KPv65J9UHkwj33fgtWyllniKBzZ17tqlY09/XDPI8eCc7QI1ofNETCMwRu+vY 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. Tested-by: Randy Dunlap # documentation bits Signed-off-by: Jeff Layton Reviewed-by: Jan Kara --- 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 4ec1e71e9a9d..7a324d999816 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2751,6 +2751,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 23908bad166c..b1a3bd07711b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1584,6 +1584,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) {