From patchwork Mon Jul 8 15:53:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13726774 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 53C3BC3271E for ; Mon, 8 Jul 2024 15:54:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BC296B0098; Mon, 8 Jul 2024 11:53:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7697C6B009A; Mon, 8 Jul 2024 11:53:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 595456B009B; Mon, 8 Jul 2024 11:53:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 39CD46B0098 for ; Mon, 8 Jul 2024 11:53:58 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E035E813F5 for ; Mon, 8 Jul 2024 15:53:57 +0000 (UTC) X-FDA: 82317031314.06.0EA27BE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id 263D0100004 for ; Mon, 8 Jul 2024 15:53:55 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=snEZEZZY; 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=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720454021; a=rsa-sha256; cv=none; b=ul4oMONIOn2omk+wJN+N9Qu6wWHpewQdsbKusjNbQKbzJ9ZqFxxCLcBJF0qmi5Z7PzrAMw MoaihbsQUqFnrk41iBAdCIYG2CEOIUBUoHsf5VDe+o3aRfFWf9aLPiJUOVXd+WN3lEK8q8 8yQMQXNl3bEQCIP/8xxrvzCMNR2GK/Y= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=snEZEZZY; 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=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720454021; 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=bJddfqUYKfPkL5IaapNTI+DPloFTuokezQWldk1boEE=; b=y5PvMoiPGIzEUwjPCE8pHnzXYguSlpgbUNxBIQ3N0M5NG+eMtAMXCw1k92S4eoajLPtxWl fFgMMeYLBV3pRDTerjZFTAVP/hlUhK2Yhfqg454d74x783a9FsDvrz6FaTKe/A/3p8h1zB yd5ARhrs2clvTCWGRL0CQNjR+pxp4Ug= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5033960EE4; Mon, 8 Jul 2024 15:53:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E048C116B1; Mon, 8 Jul 2024 15:53:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720454035; bh=/g1ddIto05q7VUdUuGmHZRulUXiGWzaifPHAIEfpWmk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=snEZEZZYYlDIZjMeR0Td2Px4FknQRH3fmM4PTBqA1d7M6RXOxjxczgJGVuJ3P4GRQ ysSySTtnx+quYylWaidML4XGNklkrRMjIqpIe8UeFAh6fHMNYp3pCuNa5yheh/2xqi +wsTLz1ey0cfjW3yrcN2MAki2ZIhVRIpvH072F1MiLO26e12C+CH8eYH7dvEyYAibA aGbLq7+A1yYlyWQNzJKsppiAyNwSDcqZ+OcaCwOF4QVX1prMyLtbRPInAiBxLEaiUo DdtmLzKbMUTY5kdzF5nSR+HLmMMhK7doBtBaiLTyGCtcwMOFVonevbVz8SqbrjWqSc evKhj7yKOb6Ag== From: Jeff Layton Date: Mon, 08 Jul 2024 11:53:37 -0400 Subject: [PATCH v4 4/9] fs: have setattr_copy handle multigrain timestamps appropriately MIME-Version: 1.0 Message-Id: <20240708-mgtime-v4-4-a0f3c6fb57f3@kernel.org> References: <20240708-mgtime-v4-0-a0f3c6fb57f3@kernel.org> In-Reply-To: <20240708-mgtime-v4-0-a0f3c6fb57f3@kernel.org> To: Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Chandan Babu R , "Darrick J. Wong" , Theodore Ts'o , Andreas Dilger , Chris Mason , Josef Bacik , David Sterba , Hugh Dickins , Andrew Morton , Jonathan Corbet Cc: Dave Chinner , Andi Kleen , Christoph Hellwig , Uros Bizjak , Kent Overstreet , kernel-team@fb.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, linux-doc@vger.kernel.org, Jeff Layton X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3403; i=jlayton@kernel.org; h=from:subject:message-id; bh=/g1ddIto05q7VUdUuGmHZRulUXiGWzaifPHAIEfpWmk=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmjAuEL4klBEgs/SfbfKR94rLthGFfhBSMCDbIG o5m14n1BsCJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZowLhAAKCRAADmhBGVaC FUjqD/45ex7zNf19dNV4LHtGLYWtZ70YPvkdUMIkEdLkrKjWeWdIIKpGP1kG8D1U+0QY/cVdZQG /9dg3txWF9kkA7P5ghcdrHIk+gP7qVfFEqlBwTxtnLOjaiWbO06hgl9ii52/I2z1ndnTd2O8Xrx n24iowjJq3gdx8wfi8LqmlBCsvZVWPUf6OmUscBEtdcwEWTPCZMEUrl15JJ8MAJGRLDLjesvoE6 snCuGvaKcDXizPA7X+YpZBYoMtL1HWE61qQfvskO4F2Nkvxj2cCKAZTcSyaGx36j5MROw3XH0y4 4Mw/YCUcJ5OoZiqAZg79fS+AicFIykbZXLERNZ58tf8y6ESylFZsI+uFV4ydKW/IXNbB3I21EG8 RNv6agbK8HNsPvQLJMr6IhghR0QPI0KTEyw4dpVCMFiYoF8671jwrq+O7SfVGJRpY8Bi7G6SOpA UZ8izyjrGP/lkbGicScFy7QkJ3AZwvePIFh5JcT6j9mjykJ90V0cTg/7VDFEwFM7b+ptXN7GBZp zBjWzxR8CGq1293ZOBFGuk0a4mCqBmjD2yZifaP01mBzmnHZFeHpeQPAZwsI3i3fLi94Ct6CyZU 9FXumDSEBy/akWO9HZWy+HQGzR/Pe9fTc2kyWC93w3fSqfRlXMsRj7DCU7viYe3L4qk0ZYzgFJV G8Tql0DRGFseNHw== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Stat-Signature: 8z4jf13rexcnximdr98orecccnz5fezy X-Rspamd-Queue-Id: 263D0100004 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1720454035-828363 X-HE-Meta: U2FsdGVkX19OMTsDdDHbDIoQvbzpJFmcATbvEw8tNj669via7jDbkPx2iLEXuDkxDzT3kUz5tmX2cEuIWRYLXNx+Ox+FW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mKOVMrAAxXoeTEf02FaN9bhyhbCj+LvBRpb3XWvVM7Wo/Fv1qfhz/Sc/QZEyrniRG0jPB3L6prwREkpJsLFYJjbwW3gYVmw4D37m7WWICSMzRIhnPPm8z+FKD6Rt4aC+3CIe0Mahh+miMm9aeJiPnfmVWSvWfgEy8ajF4XC8nUsEybqs7d47UZnY2rK7GEq58w9dwTCPFpr0d9vIgPu53jDWT7b/UhSc2VzvqAJuPORHQwf 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: The setattr codepath is still using coarse-grained timestamps, even on multigrain filesystems. To fix this, we need to fetch the timestamp for ctime updates later, at the point where the assignment occurs in setattr_copy. On a multigrain inode, ignore the ia_ctime in the attrs, and always update the ctime to the current clock value. Update the atime and mtime with the same value (if needed) unless they are being set to other specific values, a'la utimes(). Note that we don't want to do this universally however, as some filesystems (e.g. most networked fs) want to do an explicit update elsewhere before updating the local inode. Signed-off-by: Jeff Layton --- fs/attr.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index 825007d5cda4..e03ea6951864 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -271,6 +271,42 @@ int inode_newsize_ok(const struct inode *inode, loff_t offset) } EXPORT_SYMBOL(inode_newsize_ok); +/** + * setattr_copy_mgtime - update timestamps for mgtime inodes + * @inode: inode timestamps to be updated + * @attr: attrs for the update + * + * With multigrain timestamps, we need to take more care to prevent races + * when updating the ctime. Always update the ctime to the very latest + * using the standard mechanism, and use that to populate the atime and + * mtime appropriately (unless we're setting those to specific values). + */ +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; + } + + 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) + inode_set_atime_to_ts(inode, now); + + if (ia_valid & ATTR_MTIME_SET) + inode_set_mtime_to_ts(inode, attr->ia_mtime); + else if (ia_valid & ATTR_MTIME) + inode_set_mtime_to_ts(inode, now); +} + /** * setattr_copy - copy simple metadata updates into the generic inode * @idmap: idmap of the mount the inode was found from @@ -303,12 +339,6 @@ void setattr_copy(struct mnt_idmap *idmap, struct inode *inode, i_uid_update(idmap, attr, inode); i_gid_update(idmap, attr, inode); - if (ia_valid & ATTR_ATIME) - 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_MODE) { umode_t mode = attr->ia_mode; if (!in_group_or_capable(idmap, inode, @@ -316,6 +346,16 @@ void setattr_copy(struct mnt_idmap *idmap, struct inode *inode, mode &= ~S_ISGID; inode->i_mode = mode; } + + if (is_mgtime(inode)) + return setattr_copy_mgtime(inode, attr); + + if (ia_valid & ATTR_ATIME) + 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); } EXPORT_SYMBOL(setattr_copy);