linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jubin Zhong <zhongjubin@huawei.com>
To: <viro@zeniv.linux.org.uk>
Cc: <wangfangpeng1@huawei.com>, <kechengsong@huawei.com>,
	<linux-fsdevel@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: [PATCH] fs: Fix truncate never updates m/ctime
Date: Mon, 15 Nov 2021 19:00:18 +0800	[thread overview]
Message-ID: <1636974018-31285-1-git-send-email-zhongjubin@huawei.com> (raw)

From: zhongjubin <zhongjubin@huawei.com>

Syscall truncate() never updates m/ctime even if the file size is
changed. However, this is incorrect according to man file:

  truncate (2):
  If  the  size  changed, then the st_ctime and st_mtime fields
  (respectively, time of last status change and time of last modification;
  see stat(2)) for the file are updated, and the set-user-ID and
  set-group-ID mode bits may be cleared.

Check file size before do_truncate() to fix this.

Signed-off-by: zhongjubin <zhongjubin@huawei.com>
---
 fs/open.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/open.c b/fs/open.c
index f732fb94600c..02404b759c2e 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -106,8 +106,11 @@ long vfs_truncate(const struct path *path, loff_t length)
 		goto put_write_and_out;
 
 	error = security_path_truncate(path);
-	if (!error)
-		error = do_truncate(mnt_userns, path->dentry, length, 0, NULL);
+	if (error)
+		goto put_write_and_out;
+
+	if (i_size_read(inode) != length)
+		error = do_truncate(mnt_userns, path->dentry, length, ATTR_MTIME | ATTR_CTIME, NULL);
 
 put_write_and_out:
 	put_write_access(inode);
-- 
2.12.3


             reply	other threads:[~2021-11-15 11:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-15 11:00 Jubin Zhong [this message]
2021-11-15 17:58 ` [PATCH] fs: Fix truncate never updates m/ctime Christoph Hellwig
2021-11-16  3:58   ` Jubin Zhong
2021-11-16  5:22     ` Christoph Hellwig
2021-11-16  6:57       ` Jubin Zhong
2021-11-16  7:02         ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1636974018-31285-1-git-send-email-zhongjubin@huawei.com \
    --to=zhongjubin@huawei.com \
    --cc=kechengsong@huawei.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wangfangpeng1@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).