From patchwork Fri Sep 13 13:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13803490 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 AF6B8FA3755 for ; Fri, 13 Sep 2024 13:54:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A502B6B00D5; Fri, 13 Sep 2024 09:54:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FDAE6B00D6; Fri, 13 Sep 2024 09:54:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 878CA6B00D7; Fri, 13 Sep 2024 09:54:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 64FF46B00D5 for ; Fri, 13 Sep 2024 09:54:35 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CFEEA1C6D68 for ; Fri, 13 Sep 2024 13:54:34 +0000 (UTC) X-FDA: 82559860068.05.2300E66 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id E92DA1C000B for ; Fri, 13 Sep 2024 13:54:32 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fl8X3cLS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726235644; a=rsa-sha256; cv=none; b=RyIBzLZ0Bd1mwfZLggE9xA9rmg4j6PMNGZ5cTAZygQXdXmreYgygrbwb7E9eWweDOz2Je1 7z20vN/QMS0JXiWTNe0U+L0ZVtM4KXDajqZMs1hzRMSaNIzVCdAJ5dq5RYWf8KqW6P6YFZ 4pTIwHrEC4pZcT8Nf8oxRdw/jUDAs4c= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fl8X3cLS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 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=1726235644; 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=EX5z9CJFuWf8sDJeP6pHGIUJQozuPbguJJdN2ywUYus=; b=QTTyb8s/pChQSyU8KyVYBkSX5ihf90fS9wueguil9YFbTURT9ZacNaR5tB1KeL3e7Ch/r4 LzpwYX6lARcCGliCg/XPtj/gBz/nw66bJu473BVDIDwuMAH1zXCFAlF/q5GsDh7KpdsS3i 9j6h2aVpKaN2bUnjmMkJ/gbkmW4qAm4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 64D5A5C1129; Fri, 13 Sep 2024 13:54:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44623C4CED0; Fri, 13 Sep 2024 13:54:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726235671; bh=hdc3HuXN78tnJke+lsjFRc8MIpdoXqfI1aIZo2Ph9do=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fl8X3cLSHojZIjmkmSYV4n4Y0MHgS2lGCRsEJknPf7WD1GMgEqaV67ufmNO5d2WUe 49XX5BJCMJJPdQdd/+/K0lkypJS229K2gE/55/J3DaoogPv0is7FMexHVQSMGR2W1j Vdrwmh0kYrFZOeyTFYJcVT8R6/XN9q1qODVb3SvTGDjDpJ/8ojpwL/u61Tu5NvSy95 nV7FUWW+5YI3dMDjIIfBOrSAamp42O2lT/fv34w1kXigWtrcLpRO1fi3xh6WXCZhC5 kJQ9wRKjcqXz1hReqjy9TiDj+w46N8W4b8wGlVxcsoETWKwF4lopHIbb6SPYyxEAWI WRhZXKraQkpsw== From: Jeff Layton Date: Fri, 13 Sep 2024 09:54:13 -0400 Subject: [PATCH v7 04/11] fs: handle delegated timestamps in setattr_copy_mgtime MIME-Version: 1.0 Message-Id: <20240913-mgtime-v7-4-92d4020e3b00@kernel.org> References: <20240913-mgtime-v7-0-92d4020e3b00@kernel.org> In-Reply-To: <20240913-mgtime-v7-0-92d4020e3b00@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: 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=hdc3HuXN78tnJke+lsjFRc8MIpdoXqfI1aIZo2Ph9do=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm5EQI05miWpOV+IKasjIrj/f2h92GkcTZGaHOX i7zmXV5tiCJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZuRECAAKCRAADmhBGVaC FawzD/0QM/rrrncZr9goBf5Fodvc0nyXf5LKp7ptAeNyS9j27/HdizuJeqJN85pMsI6Z+d7O0ZB yJtdOR5rB/LlzyQO2tyXlaGp1w9niFn45ega3Y3e3a5jBVJVSCnQfvrBEeuzNILZjt7LLBwUS86 JB40F60UHoHTsiglLUa0/NPxitwDK/RJSDNLwySzK5dfrO3xO0zOyESEvAIx43QMJw3BwkvbOPh QTDhu6GVvb0HfFXv9EbRGoiDK9h2oCyk+FVKf3DJNQezu+QIBHGTH7TJ3VaO6rdESdIXTq6S8eU eJS7QDt/VRB46z+xHYc8HozzWOrSg9rI5b65RU9GraAhX76JKl8KdUt6J2rt/M/zHuuWh5gVMi5 yAktGJy//n2pDhUgslejA0WBSRfeydc+LET6il1YWu4dBUMzn8L2RdtnvU171pbHggj2eInPkY4 EoBw4Ii07JxOm3v7sMAdGeNDXQL855ydZjyfaMLmIqbes+U/3PQp5fehiCA3v/9Qez7BJmRsID6 OdSTBy0gE61fj0imPRmKgZBTrepnfUSwY+O8G4HOGGmD0YwPgDfOJb2fuSwwqgxxh4SWeEib5JY udf6qVdd0u+unM4YE3oGKxf4qwHSGO7R6emJKLlLQ6qgVHCReeXNOtpC6Y5OULwrMsJe+TbzRHh npI0J3yTRNm5ndw== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspam-User: X-Rspamd-Queue-Id: E92DA1C000B X-Rspamd-Server: rspam01 X-Stat-Signature: 1jf5to8yhp7r9cob1s5im6aao6eh3teq X-HE-Tag: 1726235672-9269 X-HE-Meta: U2FsdGVkX1+qC/jMW6pQ5d+RqBxwkPny/YkdtecmaAqEZErUjCdzXyLxEXFFdajF6D7sicfU2oTjTb6y3N0IAtY4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cFoToc5Mp9Pmzfsu4E5cqyFTJ0+o0ambYGkjKkiEOkN9uYFaoC3C1yzefAKxgV7GqXwV9UMwRXYnaNRemcRZAZgCyKK4Qiq2TDfrxnIEWx7Zr71HIkXSkSHZvD0N/UZYXFYV+rH6yN4gJh8V4Q== 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 8ab36779066e..260a8a1c1096 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2710,6 +2710,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) {