linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: use vmtruncate() instead of ext4_truncate() in ext4_setattr()
@ 2011-05-17 22:59 Jiaying Zhang
  2011-05-18  2:46 ` Yongqiang Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 22+ messages in thread
From: Jiaying Zhang @ 2011-05-17 22:59 UTC (permalink / raw)
  To: tytso; +Cc: linux-ext4

There is a bug in commit c8d46e41 "ext4: Add flag to files with blocks
intentionally past EOF" that if we fallocate a file with FALLOC_FL_KEEP_SIZE
flag and then ftruncate the file to a size larger than the file's i_size,
any allocated but unwritten blocks will be freed but the file size is set
to the size that ftruncate specifies.

Here is a simple test to reproduce the problem:
  1. fallocate a 12k size file with KEEP_SIZE flag
  2. write the first 4k
  3. ftruncate the file to 8k
Then 'ls -l' shows that the i_size of the file becomes 8k but debugfs
shows the file has only the first written block left.

Below is the proposed patch to fix the bug:

ext4: use vmtruncate() instead of ext4_truncate() in ext4_setattr().

Change ext4_setattr() to use vmtruncate(inode, attr->ia_size) instead
of ext4_truncate(inode) when it needs to truncate an inode so that
if the inode has EXT4_EOFBLOCKS_FL flag set and we are trying to truncate
to a size larger than the inode's i_size, we will only truncate the blocks
beyond the specified truncate size instead of all of blocks beyond i_size.

Signed-off-by: Jiaying Zhang <jiayingz@google.com>

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 3424e82..3bfad57 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5347,8 +5347,11 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
 			}
 		}
 		/* ext4_truncate will clear the flag */
-		if ((ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))
-			ext4_truncate(inode);
+		if ((ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) {
+			rc = vmtruncate(inode, attr->ia_size);
+			if (rc)
+				goto err_out;
+		}
 	}
 
 	if ((attr->ia_valid & ATTR_SIZE) &&

^ permalink raw reply related	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2011-05-24 22:31 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-17 22:59 [PATCH] ext4: use vmtruncate() instead of ext4_truncate() in ext4_setattr() Jiaying Zhang
2011-05-18  2:46 ` Yongqiang Yang
2011-05-18  2:56 ` Yongqiang Yang
2011-05-18  3:27   ` Yongqiang Yang
2011-05-18  3:19 ` Eric Sandeen
2011-05-18  5:35   ` Andreas Dilger
2011-05-18 20:32     ` Jiaying Zhang
2011-05-18 20:45       ` Andreas Dilger
2011-05-18 20:57         ` Jiaying Zhang
2011-05-18  6:13   ` Dave Chinner
2011-05-18 14:05     ` Eric Sandeen
2011-05-18 20:42     ` Jiaying Zhang
2011-05-18 20:29   ` Jiaying Zhang
     [not found]   ` <BANLkTi=Yv_q820aHFa2wkCL-PnYNcZdWCQ@mail.gmail.com>
2011-05-18 20:31     ` Eric Sandeen
2011-05-18 20:38       ` Jiaying Zhang
2011-05-22 23:56 ` Ted Ts'o
2011-05-23 18:30   ` Jiaying Zhang
2011-05-23 19:19     ` [PATCH -v2] ext4: use truncate_setsize() unconditionally Theodore Ts'o
2011-05-23 20:22       ` Jiaying Zhang
2011-05-24 14:30       ` Eric Sandeen
2011-05-24 22:06         ` Jiaying Zhang
2011-05-24 22:31           ` Eric Sandeen

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).