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