Check in Jan Kara's v3 DAX iomap patches
[ext4-patch-queue.git] / allow-ext4_truncate-to-return-an-error
blob8ecf2637f6a8d7269456bc189b2fbc7c664a7251
1 ext4: allow ext4_truncate() to return an error
3 This allows us to properly propagate errors back up to
4 ext4_truncate()'s callers.  This also means we no longer have to
5 silently ignore some errors (e.g., when trying to add the inode to the
6 orphan inode list).
8 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
9 ---
10  fs/ext4/ext4.h  |  2 +-
11  fs/ext4/inode.c | 41 ++++++++++++++++++++++++++---------------
12  fs/ext4/ioctl.c |  7 +++++--
13  fs/ext4/super.c |  6 ++++--
14  4 files changed, 36 insertions(+), 20 deletions(-)
16 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
17 index 282a51b07c57..be2282dcde7d 100644
18 --- a/fs/ext4/ext4.h
19 +++ b/fs/ext4/ext4.h
20 @@ -2491,7 +2491,7 @@ extern int ext4_change_inode_journal_flag(struct inode *, int);
21  extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
22  extern int ext4_inode_attach_jinode(struct inode *inode);
23  extern int ext4_can_truncate(struct inode *inode);
24 -extern void ext4_truncate(struct inode *);
25 +extern int ext4_truncate(struct inode *);
26  extern int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length);
27  extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
28  extern void ext4_set_inode_flags(struct inode *);
29 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
30 index 9c064727ed62..40ea090d2e0e 100644
31 --- a/fs/ext4/inode.c
32 +++ b/fs/ext4/inode.c
33 @@ -261,8 +261,15 @@ void ext4_evict_inode(struct inode *inode)
34                              "couldn't mark inode dirty (err %d)", err);
35                 goto stop_handle;
36         }
37 -       if (inode->i_blocks)
38 -               ext4_truncate(inode);
39 +       if (inode->i_blocks) {
40 +               err = ext4_truncate(inode);
41 +               if (err) {
42 +                       ext4_error(inode->i_sb,
43 +                                  "couldn't truncate inode %lu (err %d)",
44 +                                  inode->i_ino, err);
45 +                       goto stop_handle;
46 +               }
47 +       }
49         /*
50          * ext4_ext_truncate() doesn't reserve any slop when it
51 @@ -4091,10 +4098,11 @@ int ext4_inode_attach_jinode(struct inode *inode)
52   * that's fine - as long as they are linked from the inode, the post-crash
53   * ext4_truncate() run will find them and release them.
54   */
55 -void ext4_truncate(struct inode *inode)
56 +int ext4_truncate(struct inode *inode)
57  {
58         struct ext4_inode_info *ei = EXT4_I(inode);
59         unsigned int credits;
60 +       int err = 0;
61         handle_t *handle;
62         struct address_space *mapping = inode->i_mapping;
64 @@ -4108,7 +4116,7 @@ void ext4_truncate(struct inode *inode)
65         trace_ext4_truncate_enter(inode);
67         if (!ext4_can_truncate(inode))
68 -               return;
69 +               return 0;
71         ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
73 @@ -4120,13 +4128,13 @@ void ext4_truncate(struct inode *inode)
75                 ext4_inline_data_truncate(inode, &has_inline);
76                 if (has_inline)
77 -                       return;
78 +                       return 0;
79         }
81         /* If we zero-out tail of the page, we have to create jinode for jbd2 */
82         if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
83                 if (ext4_inode_attach_jinode(inode) < 0)
84 -                       return;
85 +                       return 0;
86         }
88         if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
89 @@ -4135,10 +4143,8 @@ void ext4_truncate(struct inode *inode)
90                 credits = ext4_blocks_for_truncate(inode);
92         handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
93 -       if (IS_ERR(handle)) {
94 -               ext4_std_error(inode->i_sb, PTR_ERR(handle));
95 -               return;
96 -       }
97 +       if (IS_ERR(handle))
98 +               return PTR_ERR(handle);
100         if (inode->i_size & (inode->i_sb->s_blocksize - 1))
101                 ext4_block_truncate_page(handle, mapping, inode->i_size);
102 @@ -4152,7 +4158,8 @@ void ext4_truncate(struct inode *inode)
103          * Implication: the file must always be in a sane, consistent
104          * truncatable state while each transaction commits.
105          */
106 -       if (ext4_orphan_add(handle, inode))
107 +       err = ext4_orphan_add(handle, inode);
108 +       if (err)
109                 goto out_stop;
111         down_write(&EXT4_I(inode)->i_data_sem);
112 @@ -4185,6 +4192,7 @@ void ext4_truncate(struct inode *inode)
113         ext4_journal_stop(handle);
115         trace_ext4_truncate_exit(inode);
116 +       return err;
119  /*
120 @@ -5199,12 +5207,15 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
121                  * in data=journal mode to make pages freeable.
122                  */
123                 truncate_pagecache(inode, inode->i_size);
124 -               if (shrink)
125 -                       ext4_truncate(inode);
126 +               if (shrink) {
127 +                       rc = ext4_truncate(inode);
128 +                       if (rc)
129 +                               error = rc;
130 +               }
131                 up_write(&EXT4_I(inode)->i_mmap_sem);
132         }
134 -       if (!rc) {
135 +       if (!error) {
136                 setattr_copy(inode, attr);
137                 mark_inode_dirty(inode);
138         }
139 @@ -5216,7 +5227,7 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
140         if (orphan && inode->i_nlink)
141                 ext4_orphan_del(NULL, inode);
143 -       if (!rc && (ia_valid & ATTR_MODE))
144 +       if (!error && (ia_valid & ATTR_MODE))
145                 rc = posix_acl_chmod(inode, inode->i_mode);
147  err_out:
148 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
149 index bf5ae8ebbc97..99862a3726fc 100644
150 --- a/fs/ext4/ioctl.c
151 +++ b/fs/ext4/ioctl.c
152 @@ -248,8 +248,11 @@ static int ext4_ioctl_setflags(struct inode *inode,
153                         err = -EOPNOTSUPP;
154                         goto flags_out;
155                 }
156 -       } else if (oldflags & EXT4_EOFBLOCKS_FL)
157 -               ext4_truncate(inode);
158 +       } else if (oldflags & EXT4_EOFBLOCKS_FL) {
159 +               err = ext4_truncate(inode);
160 +               if (err)
161 +                       goto flags_out;
162 +       }
164         handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
165         if (IS_ERR(handle)) {
166 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
167 index 20da99da0a34..e4f61c39328a 100644
168 --- a/fs/ext4/super.c
169 +++ b/fs/ext4/super.c
170 @@ -2330,7 +2330,7 @@ static void ext4_orphan_cleanup(struct super_block *sb,
171                                 struct ext4_super_block *es)
173         unsigned int s_flags = sb->s_flags;
174 -       int nr_orphans = 0, nr_truncates = 0;
175 +       int ret, nr_orphans = 0, nr_truncates = 0;
176  #ifdef CONFIG_QUOTA
177         int i;
178  #endif
179 @@ -2412,7 +2412,9 @@ static void ext4_orphan_cleanup(struct super_block *sb,
180                                   inode->i_ino, inode->i_size);
181                         inode_lock(inode);
182                         truncate_inode_pages(inode->i_mapping, inode->i_size);
183 -                       ext4_truncate(inode);
184 +                       ret = ext4_truncate(inode);
185 +                       if (ret)
186 +                               ext4_std_error(inode->i_sb, ret);
187                         inode_unlock(inode);
188                         nr_truncates++;
189                 } else {