add patch set-lazytime-on-remount
[ext4-patch-queue.git] / only-call-ext4_truncate-when-size-le-isize
bloba0b4bddae7f0040bb07d20d00bb62a7d41d54a9e
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>
16 ---
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
22 --- a/fs/ext4/inode.c
23 +++ b/fs/ext4/inode.c
24 @@ -4681,8 +4681,10 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
25                 ext4_journal_stop(handle);
26         }
28 -       if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
29 +       if (attr->ia_valid & ATTR_SIZE) {
30                 handle_t *handle;
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)
38                                 return -EFBIG;
39                 }
40 +               if (!S_ISREG(inode->i_mode))
41 +                       return -EINVAL;
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,
52                                                             attr->ia_size);
53 -                               if (error)
54 -                                       goto err_out;
55 -                       }
56 +                       if (error)
57 +                               goto err_out;
58 +               }
59 +               if (attr->ia_size != inode->i_size) {
60                         handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
61                         if (IS_ERR(handle)) {
62                                 error = PTR_ERR(handle);
63                                 goto err_out;
64                         }
65 -                       if (ext4_handle_valid(handle)) {
66 +                       if (ext4_handle_valid(handle) && shrink) {
67                                 error = ext4_orphan_add(handle, inode);
68                                 orphan = 1;
69                         }
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);
73                         if (error) {
74 -                               ext4_orphan_del(NULL, inode);
75 +                               if (orphan)
76 +                                       ext4_orphan_del(NULL, inode);
77                                 goto err_out;
78                         }
79 -               } else {
80 -                       loff_t oldsize = inode->i_size;
82 -                       i_size_write(inode, attr->ia_size);
83 -                       pagecache_isize_extended(inode, oldsize, inode->i_size);
84                 }
85 +               if (!shrink)
86 +                       pagecache_isize_extended(inode, oldsize, inode->i_size);
88                 /*
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.
92                  */
93                 truncate_pagecache(inode, inode->i_size);
94 +               if (shrink)
95 +                       ext4_truncate(inode);
96         }
97 -       /*
98 -        * We want to call ext4_truncate() even if attr->ia_size ==
99 -        * inode->i_size for cases like truncation of fallocated space
100 -        */
101 -       if (attr->ia_valid & ATTR_SIZE)
102 -               ext4_truncate(inode);
104         if (!rc) {
105                 setattr_copy(inode, attr);