linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andries.Brouwer@cwi.nl
To: linux-fs@cwi.nl, linux-kernel@vger.kernel.org
Cc: torvalds@transmeta.com
Subject: [patch?] truncate and timestamps
Date: Thu, 22 May 2003 21:09:43 +0200 (MEST)	[thread overview]
Message-ID: <UTC200305221909.h4MJ9h903738.aeb@smtp.cwi.nl> (raw)

Investigating why some SuSE init script no longer works, I find:
The shell command
    > file
does not update the time stamp of file in case it existed and had size 0.
The corresponding system call is
    open(file, O_WRONLY | O_TRUNC);

Time stamp here is st_mtime, and the behaviour is not unreasonable:
after all, the contents do not change.

And indeed, in do_truncate() we have
    newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
that is, ATTR_MTIME is not mentioned.

Older Linux systems did update st_mtime, old FreeBSD systems did not,
SunOS 5.7 does, SunOS 5.8 does not. There is no uniform Unix behaviour.

Remains the question what POSIX says.
For opening with O_TRUNC, and for the ftruncate system call
POSIX has always said and still says: st_ctime and st_mtime
are updated.
For the truncate call POSIX.1-2001 says: st_ctime and st_mtime
are updated if the size changed.

If we want to follow this, then do_truncate() needs an additional
parameter indicating what kind of call we have.
A little bit ugly. A possible patch follows.

Andries

------------------
diff -u --recursive --new-file -X /linux/dontdiff a/fs/exec.c b/fs/exec.c
--- a/fs/exec.c	Thu May 22 13:17:02 2003
+++ b/fs/exec.c	Thu May 22 20:14:02 2003
@@ -1352,7 +1352,7 @@
 		goto close_fail;
 	if (!file->f_op->write)
 		goto close_fail;
-	if (do_truncate(file->f_dentry, 0) != 0)
+	if (do_truncate(file->f_dentry, 0, 1) != 0)
 		goto close_fail;
 
 	retval = binfmt->core_dump(signr, regs, file);
diff -u --recursive --new-file -X /linux/dontdiff a/fs/namei.c b/fs/namei.c
--- a/fs/namei.c	Thu May 22 13:17:02 2003
+++ b/fs/namei.c	Thu May 22 20:13:41 2003
@@ -1178,7 +1178,7 @@
 		if (!error) {
 			DQUOT_INIT(inode);
 			
-			error = do_truncate(dentry, 0);
+			error = do_truncate(dentry, 0, 1);
 		}
 		put_write_access(inode);
 		if (error)
diff -u --recursive --new-file -X /linux/dontdiff a/fs/open.c b/fs/open.c
--- a/fs/open.c	Thu May 22 13:17:02 2003
+++ b/fs/open.c	Thu May 22 20:15:25 2003
@@ -75,7 +75,7 @@
 	return error;
 }
 
-int do_truncate(struct dentry *dentry, loff_t length)
+int do_truncate(struct dentry *dentry, loff_t length, int update_timestamp)
 {
 	int err;
 	struct iattr newattrs;
@@ -85,7 +85,9 @@
 		return -EINVAL;
 
 	newattrs.ia_size = length;
-	newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
+	newattrs.ia_valid = ATTR_SIZE;
+	if (update_timestamp || length != dentry->d_inode->i_size)
+		newattrs.ia_valid |= ATTR_CTIME | ATTR_MTIME;
 	down(&dentry->d_inode->i_sem);
 	err = notify_change(dentry, &newattrs);
 	up(&dentry->d_inode->i_sem);
@@ -142,7 +144,7 @@
 	error = locks_verify_truncate(inode, NULL, length);
 	if (!error) {
 		DQUOT_INIT(inode);
-		error = do_truncate(nd.dentry, length);
+		error = do_truncate(nd.dentry, length, 0);
 	}
 	put_write_access(inode);
 
@@ -194,7 +196,7 @@
 
 	error = locks_verify_truncate(inode, file, length);
 	if (!error)
-		error = do_truncate(dentry, length);
+		error = do_truncate(dentry, length, 1);
 out_putf:
 	fput(file);
 out:
diff -u --recursive --new-file -X /linux/dontdiff a/include/linux/fs.h b/include/linux/fs.h
--- a/include/linux/fs.h	Thu May 22 13:17:05 2003
+++ b/include/linux/fs.h	Thu May 22 20:14:39 2003
@@ -1019,7 +1019,7 @@
 
 asmlinkage long sys_open(const char __user *, int, int);
 asmlinkage long sys_close(unsigned int);	/* yes, it's really unsigned */
-extern int do_truncate(struct dentry *, loff_t start);
+extern int do_truncate(struct dentry *, loff_t start, int update_timestamp);
 
 extern struct file *filp_open(const char *, int, int);
 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int);

             reply	other threads:[~2003-05-22 18:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-22 19:09 Andries.Brouwer [this message]
2003-05-22 23:20 ` [patch?] truncate and timestamps Andrew Morton
2003-05-23  0:17 Andries.Brouwer
2003-05-23  0:30 ` Linus Torvalds
2003-05-23  1:17   ` viro
2003-05-23  2:42     ` Andrew Morton
2003-05-23  5:11       ` Trond Myklebust
2003-05-23  5:25         ` Trond Myklebust
2003-05-23  3:14     ` David Schwartz
2003-05-26 23:42     ` Alan Cox
2003-05-27  1:17       ` Andrew Morton
2003-05-23 18:02   ` Kai Henningsen
2003-05-23 19:07     ` Andries Brouwer

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=UTC200305221909.h4MJ9h903738.aeb@smtp.cwi.nl \
    --to=andries.brouwer@cwi.nl \
    --cc=linux-fs@cwi.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@transmeta.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).