1 ext4: only call ext4_truncate when size <= isize
3 From: Josef Bacik <jbacik@fb.com>
5 At LSF we decided that if we truncate up from isize we shouldn't trim
6 fallocated blocks that were fallocated with KEEP_SIZE and are past the
7 new i_size. This patch fixes ext4 to do this.
9 [ Completely reworked patch so that i_disksize would actually get set
10 when truncating up. Also reworked the code for handling truncate so
11 that it's easier to handle. -- tytso ]
13 Signed-off-by: Josef Bacik <jbacik@fb.com>
14 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 Reviewed-by: Lukas Czerner <lczerner@redhat.com>
17 fs/ext4/inode.c | 38 ++++++++++++++++++--------------------
18 1 file changed, 18 insertions(+), 20 deletions(-)
20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
21 index ae93f0b..e057c6f 100644
24 @@ -4681,8 +4681,10 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
25 ext4_journal_stop(handle);
28 - if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
29 + if (attr->ia_valid & ATTR_SIZE) {
31 + loff_t oldsize = inode->i_size;
32 + int shrink = (attr->ia_size <= inode->i_size);
34 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
35 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
36 @@ -4690,24 +4692,26 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
37 if (attr->ia_size > sbi->s_bitmap_maxbytes)
40 + if (!S_ISREG(inode->i_mode))
43 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
44 inode_inc_iversion(inode);
46 - if (S_ISREG(inode->i_mode) &&
47 + if (ext4_should_order_data(inode) &&
48 (attr->ia_size < inode->i_size)) {
49 - if (ext4_should_order_data(inode)) {
50 - error = ext4_begin_ordered_truncate(inode,
51 + error = ext4_begin_ordered_truncate(inode,
59 + if (attr->ia_size != inode->i_size) {
60 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
62 error = PTR_ERR(handle);
65 - if (ext4_handle_valid(handle)) {
66 + if (ext4_handle_valid(handle) && shrink) {
67 error = ext4_orphan_add(handle, inode);
70 @@ -4726,15 +4730,13 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
71 up_write(&EXT4_I(inode)->i_data_sem);
72 ext4_journal_stop(handle);
74 - ext4_orphan_del(NULL, inode);
76 + ext4_orphan_del(NULL, inode);
80 - loff_t oldsize = inode->i_size;
82 - i_size_write(inode, attr->ia_size);
83 - pagecache_isize_extended(inode, oldsize, inode->i_size);
86 + pagecache_isize_extended(inode, oldsize, inode->i_size);
89 * Blocks are going to be removed from the inode. Wait
90 @@ -4754,13 +4756,9 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
91 * in data=journal mode to make pages freeable.
93 truncate_pagecache(inode, inode->i_size);
95 + ext4_truncate(inode);
98 - * We want to call ext4_truncate() even if attr->ia_size ==
99 - * inode->i_size for cases like truncation of fallocated space
101 - if (attr->ia_valid & ATTR_SIZE)
102 - ext4_truncate(inode);
105 setattr_copy(inode, attr);