Skip to contentSkip to conversation

Don't change ctime in truncate is size does not change

PATCH In review David Chinner 3 participants 5 messages Feb 18, 2008 – Feb 21, 2008 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Don't change ctime in truncate is size does not change

XFS changes the c/mtime of an inode when truncating it to the same size. The c/mtime is only supposed to change if the size is changed. Not to be confused with ftruncate, where the c/mtime is supposed to be changed even if the size is not changed.

The Linux VFS encodes this semantic difference in the flags it sends down to ->setattr, which XFS currently ignores. We need to make XFS pay attention to the VFS flags and hence Do The Right Thing.

---
 fs/xfs/xfs_vnodeops.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/xfs/xfs_vnodeops.c +9 -4
Index: 2.6.x-xfs-new/fs/xfs/xfs_vnodeops.c
===================================================================
@@ -634,6 +634,15 @@ xfs_setattr(
 	 * Truncate file.  Must have write permission and not be a directory.
 	 */
 	if (mask & XFS_AT_SIZE) {
+		/*
+		 * Only change the c/mtime if we are changing the size
+		 * or we are explicitly asked to change it. This handles
+		 * the semantic difference between truncate() and ftruncate()
+		 * as implemented in the VFS.
+		 */
+		if (vap->va_size != ip->i_size || mask & XFS_AT_CTIME)
+			timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
+
 		if (vap->va_size > ip->i_size) {
 			xfs_igrow_finish(tp, ip, vap->va_size,
 			    !(flags & ATTR_DMI));
@@ -662,10 +671,6 @@ xfs_setattr(
 			 */
 			xfs_iflags_set(ip, XFS_ITRUNCATED);
 		}
-		/*
-		 * Have to do this even if the file's size doesn't change.
-		 */
-		timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
 	}
 
 	/*
#1 lore raw

Did we ever discover what was behind the comment:
"Have to do this even if the file's size doesn't change"

Cheers
-- Mark

Quoted 45 lines · David Chinner

XFS changes the c/mtime of an inode when truncating it to the same size. The c/mtime is only supposed to change if the size is changed. Not to be confused with ftruncate, where the c/mtime is supposed to be changed even if the size is not changed.

The Linux VFS encodes this semantic difference in the flags it sends down to ->setattr, which XFS currently ignores. We need to make XFS pay attention to the VFS flags and hence Do The Right Thing.

---
 fs/xfs/xfs_vnodeops.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Index: 2.6.x-xfs-new/fs/xfs/xfs_vnodeops.c
===================================================================

fs/xfs/xfs_vnodeops.c +9 -4
@@ -634,6 +634,15 @@ xfs_setattr(
 	 * Truncate file.  Must have write permission and not be a directory.
 	 */
 	if (mask & XFS_AT_SIZE) {
+		/*
+		 * Only change the c/mtime if we are changing the size
+		 * or we are explicitly asked to change it. This handles
+		 * the semantic difference between truncate() and ftruncate()
+		 * as implemented in the VFS.
+		 */
+		if (vap->va_size != ip->i_size || mask & XFS_AT_CTIME)
+			timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
+
 		if (vap->va_size > ip->i_size) {
 			xfs_igrow_finish(tp, ip, vap->va_size,
 			    !(flags & ATTR_DMI));
@@ -662,10 +671,6 @@ xfs_setattr(
 			 */
 			xfs_iflags_set(ip, XFS_ITRUNCATED);
 		}
-		/*
-		 * Have to do this even if the file's size doesn't change.
-		 */
-		timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
 	}
 
 	/*
Signature · 4 lines
 Mark Goodwin                                  [email protected]
 Engineering Manager for XFS and PCP    Phone: +61-3-99631937
 SGI Australian Software Group           Cell: +61-4-18969583
-------------------------------------------------------------
#2 lore raw
Quoted 2 lines · Mark Goodwin

Did we ever discover what was behind the comment:
"Have to do this even if the file's size doesn't change"

It was put there when the patch was checked in - I can only assume that it was done this way because the old test script that was being run didn't discriminate between the truncate and truncate differences.

Basically, both the code and the comment are wrong and needed to be fixed....

Cheers,

Dave.

Signature · 3 lines
Dave Chinner
Principal Engineer
SGI Australian Software Group
#3 lore raw
In reply to David Chinner

fs/xfs/xfs_vnodeops.c xfs_setattr( · line 643

Quoted 1 line
fs/xfs/xfs_vnodeops.c +1 -0
+		if (vap->va_size != ip->i_size || mask & XFS_AT_CTIME)

Normal Linux style is to always have braces around the 'foo & bar' statements to avoid operator precedance problems when re-ordering the statements. Otherwise this one looks good.

#4 lore raw
Quoted 5 lines · Christoph Hellwig

fs/xfs/xfs_vnodeops.c xfs_setattr( · line 643

Quoted 1 line
fs/xfs/xfs_vnodeops.c +1 -0
+		if (vap->va_size != ip->i_size || mask & XFS_AT_CTIME)

Normal Linux style is to always have braces around the 'foo & bar' statements to avoid operator precedance problems when re-ordering the statements. Otherwise this one looks good.

Fixed up. Thank's for the review.

Cheers,

Dave.

Signature · 3 lines
Dave Chinner
Principal Engineer
SGI Australian Software Group