From patchwork Wed Oct 2 21:27:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13820397 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 A8721CF8542 for ; Wed, 2 Oct 2024 21:27:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C24B36B050A; Wed, 2 Oct 2024 17:27:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BD3B36B050C; Wed, 2 Oct 2024 17:27:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4E2A6B050B; Wed, 2 Oct 2024 17:27:48 -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 7A7886B0503 for ; Wed, 2 Oct 2024 17:27:48 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E3B4740FDD for ; Wed, 2 Oct 2024 21:27:47 +0000 (UTC) X-FDA: 82629949374.20.3EC60D0 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id 28149100003 for ; Wed, 2 Oct 2024 21:27:45 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pErORCLF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.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=1727904371; a=rsa-sha256; cv=none; b=MpqTRtdnFm55r8FCGOUdLqx3JIFDcXX4f8ERKw5viR2NDCTXPVmCp7x3uuTVnhszOPI0hM 1MOm9Aj9u+NTfHsq0kgoEm8njbTDH3p47uYx+nzFNWr9AZbMdMAqInnytgj72tytHMLFhB iZDcL0N5cEN6ObtB+wFzWjue2foJGe0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pErORCLF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.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=1727904371; 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=RthaHd78aIzX6l6fHtlc28hdsCzk1km0BH6z0mUUcWs=; b=mGjfF+fhQkxSVvl8GiU0lfWx39WHvryMi9cF5TW1jsrVD3ZSi9H3MZ6ohZa/kkJ+18DrIF eqIZnAD7E5P64QMUYaJRhTNle2VOa1Z4I+3nXPEEclUId6NVCXJZqaWED72pS3cNUJ/t3L GaA+XrpXjvCmej9jaK8OYWch9aMcZRE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id E3A62A4401F; Wed, 2 Oct 2024 21:27:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90C01C4CED8; Wed, 2 Oct 2024 21:27:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727904465; bh=GgXDPuj4VTCO+KYXw1W6wA5jmJ8SPd2bLeg/94OOtzA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pErORCLFbUMtYV6Xg0Q8FSs6oa64pfK8QQ0Y2/53GcZECSBeypOEHAXcefZC1c2Tq nK/M2DGCQG1niK1yXv0WLJLWOr27L/5eLGvZfj6wegF9JGGH3AhxnV5eb9iH3Na9Ep CczJA9ddfxZ72S9kSmFSXcrlzuLes8ZZfp6aov8cqZu1YCkvACsPRLk5LFhVmIPDOa zNbRpMPaHvIm8fNV7gqc0l+mYtzLf4NLo2qaQvjXfMFsIDN4CyZ3MijISmLPCPNomW yrhuvZmXB7WYmqsl/t401/RJz34nyupB8rsbUKUjfJ6QidHDhDlmG0Wh1f1S+Wr0kT wqNcdegxY9Wlw== From: Jeff Layton Date: Wed, 02 Oct 2024 17:27:20 -0400 Subject: [PATCH v10 05/12] fs: handle delegated timestamps in setattr_copy_mgtime MIME-Version: 1.0 Message-Id: <20241002-mgtime-v10-5-d1c4717f5284@kernel.org> References: <20241002-mgtime-v10-0-d1c4717f5284@kernel.org> In-Reply-To: <20241002-mgtime-v10-0-d1c4717f5284@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.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6385; i=jlayton@kernel.org; h=from:subject:message-id; bh=GgXDPuj4VTCO+KYXw1W6wA5jmJ8SPd2bLeg/94OOtzA=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm/bq/8ejyIg0PRylRvcQV6hmgwmnKDZHwiP42g ghVwS8PaLKJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZv26vwAKCRAADmhBGVaC FUs6EACFaoAMIiWmAxm2JDOYSGBGqYx7XgULFJuHkZorvtP2QGTa3sFuMu0UtasSpV7DnjmXAp/ 58p6rdY498tiod4Ll7n9AwjwsBnmb2P2aPP3L1wpOZV+tK1hk+BnJ7XfM4/P3Yi4Bbut6GpQl8U x8jnTYyJFrknCniKjxi+mI91aXA67q5KEqqI8W+ZAr4E8C2Ai3/cD+24VbybY41A52VgelzA5EG Ap3liaASdiktai/bt5qbndIJJ2qvjFyn3QcW4Tz0LTSoHZFEY2MRJSO/6Unnl3bsZByuCsgNzCW hgi8TX/79pDgk+WdlH6cN7JN1pNFCNC6u8hyp8H+pbBYmHQ2sgy2vTR+2fKcKKtCls6CuTBkCE8 Lw3Ok0r66ptJdcW4waC3luYXt1KfZc1JpJ/Uw0iMc+jhYjhM8G9GDqFxgudGT60UrBhGkF6juES hrhkkqG/rpPnqv6AmU4Z5DtKHp8wR0nrZeiQUu5H0RqZ/MdrgEH+qhEEM0OKOfkbmNrXYtwuTmo 54WkyE0sLh/y23R41nuZPVgLSJul2Mzx1B3o4zRyc4pX0QiWdPI9CWYp+0ZPwp8fmXkMb+8aVKO qIysDBYjA+SkV0rOsDDA7fXhNsMH40A1UN0npUBje6zvkZFqTM4n/myilmcSEbsnFyU46LlFfRC 8/zxtdFjBqxLvrg== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Queue-Id: 28149100003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: fcc8tnfnknupet81rxmpjqzi6wdx9kxb X-HE-Tag: 1727904465-239255 X-HE-Meta: U2FsdGVkX1+T9jotRoOTm8yYn+tbeAGx17Dh+QNjcCJk0p1isGDY/oqZ8jcmm26t2wqANqunWU24r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+0XUt52d7CPem3u15PKVekroFvtz3/3iuoF9uOoF/AmOtHd8RSGXUb1iW9slGmuJgqKicgwCkYcIe8XsxrDllHeBfcwf/w8DMwTn8emXcUNuSpo2wvwCmLfzsiPNIMrgWbuw+fmAk1s2fPT4zhD0PZMqBi3AtUF9uUvrHstC3OmrCeI/gCUqJZLYP1jz1+JuancKUW2zwv6Qz6FRY77ZfbACB66DuIArgYdKa+CJEYVkdqfSPG/Eb8rpIIdastbTBRLgf7ITgYww/SXNwzi6yp109n+re4K35A== 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: An update to the inode ctime typically requires the latest clock value possible. 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, 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, 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, the server may receive a request to update only the atime, which doesn't involve a ctime update. Trust the ATTR_CTIME flag in the update and only update the ctime when it's set. Tested-by: Randy Dunlap # documentation bits Reviewed-by: Jan Kara Signed-off-by: Jeff Layton --- fs/attr.c | 28 +++++++++++++-------- fs/inode.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ 3 files changed, 93 insertions(+), 10 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index 0309c2bd8afa04bc43db6ff207f8a58d9f6a617d..c614b954bda5244cc20ee82a98a8e68845f23bd7 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 53f56f6e1ff26e718080211880924f37cf0e5b3c..7d1ede60e549683502911f3bb3a3a079768e449b 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2717,6 +2717,79 @@ 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, update + * the ctime to a value at least equal to that. + * + * This can race with concurrent updates to the inode, in which + * case the update is skipped. + * + * Note that this works even when multigrain timestamps are not enabled, + * so it is used 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 + * it 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 another task 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 eff688e75f2f29f1c44dca96370ee230f8c21db4..ea7ed437d2b165debf680507aa450b2662fd5839 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) {