add patch avoid-long-hold-times-of-j_state_lock-while-committing
[ext4-patch-queue.git] / use-ext4_sb_bread-to-disambiguate-ENOMEM
blob4320753e3c0f952c425322f1ba8e4f8cc7d0313e
1 ext4: add ext4_sb_bread() to disambiguate ENOMEM cases
3 Today, when sb_bread() returns NULL, this can either be because of an
4 I/O error or because the system failed to allocate the buffer.  Since
5 it's an old interface, changing would require changing many call
6 sites.
8 So instead we create our own ext4_sb_bread(), which also allows us to
9 set the REQ_META flag.
11 Also fixed a problem in the xattr code where a NULL return in a
12 function could also mean that the xattr was not found, which could
13 lead to the wrong error getting returned to userspace.
15 Fixes: ac27a0ec112a ("ext4: initial copy of files from ext3")
16 Cc: stable@kernel.org
17 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
18 ---
19  fs/ext4/ext4.h    |  2 ++
20  fs/ext4/migrate.c | 36 +++++++++++++++++-----------------
21  fs/ext4/resize.c  | 72 ++++++++++++++++++++++++++++++++++----------------------------------
22  fs/ext4/super.c   | 23 ++++++++++++++++++++++
23  fs/ext4/xattr.c   | 76 ++++++++++++++++++++++++++++++++++--------------------------------------
24  5 files changed, 115 insertions(+), 94 deletions(-)
26 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
27 index 3f89d0ab08fc..b4621277e259 100644
28 --- a/fs/ext4/ext4.h
29 +++ b/fs/ext4/ext4.h
30 @@ -2538,6 +2538,8 @@ extern int ext4_group_extend(struct super_block *sb,
31  extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
33  /* super.c */
34 +extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
35 +                                        sector_t block, int op_flags);
36  extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
37  extern int ext4_calculate_overhead(struct super_block *sb);
38  extern void ext4_superblock_csum_set(struct super_block *sb);
39 diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
40 index 61a9d1927817..a98bfca9c463 100644
41 --- a/fs/ext4/migrate.c
42 +++ b/fs/ext4/migrate.c
43 @@ -116,9 +116,9 @@ static int update_ind_extent_range(handle_t *handle, struct inode *inode,
44         int i, retval = 0;
45         unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
47 -       bh = sb_bread(inode->i_sb, pblock);
48 -       if (!bh)
49 -               return -EIO;
50 +       bh = ext4_sb_bread(inode->i_sb, pblock, 0);
51 +       if (IS_ERR(bh))
52 +               return PTR_ERR(bh);
54         i_data = (__le32 *)bh->b_data;
55         for (i = 0; i < max_entries; i++) {
56 @@ -145,9 +145,9 @@ static int update_dind_extent_range(handle_t *handle, struct inode *inode,
57         int i, retval = 0;
58         unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
60 -       bh = sb_bread(inode->i_sb, pblock);
61 -       if (!bh)
62 -               return -EIO;
63 +       bh = ext4_sb_bread(inode->i_sb, pblock, 0);
64 +       if (IS_ERR(bh))
65 +               return PTR_ERR(bh);
67         i_data = (__le32 *)bh->b_data;
68         for (i = 0; i < max_entries; i++) {
69 @@ -175,9 +175,9 @@ static int update_tind_extent_range(handle_t *handle, struct inode *inode,
70         int i, retval = 0;
71         unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
73 -       bh = sb_bread(inode->i_sb, pblock);
74 -       if (!bh)
75 -               return -EIO;
76 +       bh = ext4_sb_bread(inode->i_sb, pblock, 0);
77 +       if (IS_ERR(bh))
78 +               return PTR_ERR(bh);
80         i_data = (__le32 *)bh->b_data;
81         for (i = 0; i < max_entries; i++) {
82 @@ -224,9 +224,9 @@ static int free_dind_blocks(handle_t *handle,
83         struct buffer_head *bh;
84         unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
86 -       bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
87 -       if (!bh)
88 -               return -EIO;
89 +       bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
90 +       if (IS_ERR(bh))
91 +               return PTR_ERR(bh);
93         tmp_idata = (__le32 *)bh->b_data;
94         for (i = 0; i < max_entries; i++) {
95 @@ -254,9 +254,9 @@ static int free_tind_blocks(handle_t *handle,
96         struct buffer_head *bh;
97         unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
99 -       bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
100 -       if (!bh)
101 -               return -EIO;
102 +       bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
103 +       if (IS_ERR(bh))
104 +               return PTR_ERR(bh);
106         tmp_idata = (__le32 *)bh->b_data;
107         for (i = 0; i < max_entries; i++) {
108 @@ -382,9 +382,9 @@ static int free_ext_idx(handle_t *handle, struct inode *inode,
109         struct ext4_extent_header *eh;
111         block = ext4_idx_pblock(ix);
112 -       bh = sb_bread(inode->i_sb, block);
113 -       if (!bh)
114 -               return -EIO;
115 +       bh = ext4_sb_bread(inode->i_sb, block, 0);
116 +       if (IS_ERR(bh))
117 +               return PTR_ERR(bh);
119         eh = (struct ext4_extent_header *)bh->b_data;
120         if (eh->eh_depth != 0) {
121 diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c
122 index a5efee34415f..87350b642681 100644
123 --- a/fs/ext4/resize.c
124 +++ b/fs/ext4/resize.c
125 @@ -127,10 +127,12 @@ static int verify_group_input(struct super_block *sb,
126         else if (free_blocks_count < 0)
127                 ext4_warning(sb, "Bad blocks count %u",
128                              input->blocks_count);
129 -       else if (!(bh = sb_bread(sb, end - 1)))
130 +       else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) {
131 +               err = PTR_ERR(bh);
132 +               bh = NULL;
133                 ext4_warning(sb, "Cannot read last block (%llu)",
134                              end - 1);
135 -       else if (outside(input->block_bitmap, start, end))
136 +       } else if (outside(input->block_bitmap, start, end))
137                 ext4_warning(sb, "Block bitmap not in group (block %llu)",
138                              (unsigned long long)input->block_bitmap);
139         else if (outside(input->inode_bitmap, start, end))
140 @@ -781,11 +783,11 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
141         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
142         unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
143         ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
144 -       struct buffer_head **o_group_desc, **n_group_desc;
145 -       struct buffer_head *dind;
146 -       struct buffer_head *gdb_bh;
147 +       struct buffer_head **o_group_desc, **n_group_desc = NULL;
148 +       struct buffer_head *dind = NULL;
149 +       struct buffer_head *gdb_bh = NULL;
150         int gdbackups;
151 -       struct ext4_iloc iloc;
152 +       struct ext4_iloc iloc = { .bh = NULL };
153         __le32 *data;
154         int err;
156 @@ -794,21 +796,22 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
157                        "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
158                        gdb_num);
160 -       gdb_bh = sb_bread(sb, gdblock);
161 -       if (!gdb_bh)
162 -               return -EIO;
163 +       gdb_bh = ext4_sb_bread(sb, gdblock, 0);
164 +       if (IS_ERR(gdb_bh))
165 +               return PTR_ERR(gdb_bh);
167         gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
168         if (gdbackups < 0) {
169                 err = gdbackups;
170 -               goto exit_bh;
171 +               goto errout;
172         }
174         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
175 -       dind = sb_bread(sb, le32_to_cpu(*data));
176 -       if (!dind) {
177 -               err = -EIO;
178 -               goto exit_bh;
179 +       dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
180 +       if (IS_ERR(dind)) {
181 +               err = PTR_ERR(dind);
182 +               dind = NULL;
183 +               goto errout;
184         }
186         data = (__le32 *)dind->b_data;
187 @@ -816,18 +819,18 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
188                 ext4_warning(sb, "new group %u GDT block %llu not reserved",
189                              group, gdblock);
190                 err = -EINVAL;
191 -               goto exit_dind;
192 +               goto errout;
193         }
195         BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
196         err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
197         if (unlikely(err))
198 -               goto exit_dind;
199 +               goto errout;
201         BUFFER_TRACE(gdb_bh, "get_write_access");
202         err = ext4_journal_get_write_access(handle, gdb_bh);
203         if (unlikely(err))
204 -               goto exit_dind;
205 +               goto errout;
207         BUFFER_TRACE(dind, "get_write_access");
208         err = ext4_journal_get_write_access(handle, dind);
209 @@ -837,7 +840,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
210         /* ext4_reserve_inode_write() gets a reference on the iloc */
211         err = ext4_reserve_inode_write(handle, inode, &iloc);
212         if (unlikely(err))
213 -               goto exit_dind;
214 +               goto errout;
216         n_group_desc = ext4_kvmalloc((gdb_num + 1) *
217                                      sizeof(struct buffer_head *),
218 @@ -846,7 +849,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
219                 err = -ENOMEM;
220                 ext4_warning(sb, "not enough memory for %lu groups",
221                              gdb_num + 1);
222 -               goto exit_inode;
223 +               goto errout;
224         }
226         /*
227 @@ -862,7 +865,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
228         err = ext4_handle_dirty_metadata(handle, NULL, dind);
229         if (unlikely(err)) {
230                 ext4_std_error(sb, err);
231 -               goto exit_inode;
232 +               goto errout;
233         }
234         inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >>
235                            (9 - EXT4_SB(sb)->s_cluster_bits);
236 @@ -871,8 +874,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
237         err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
238         if (unlikely(err)) {
239                 ext4_std_error(sb, err);
240 -               iloc.bh = NULL;
241 -               goto exit_inode;
242 +               goto errout;
243         }
244         brelse(dind);
246 @@ -888,15 +890,11 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
247         err = ext4_handle_dirty_super(handle, sb);
248         if (err)
249                 ext4_std_error(sb, err);
251         return err;
253 -exit_inode:
254 +errout:
255         kvfree(n_group_desc);
256         brelse(iloc.bh);
257 -exit_dind:
258         brelse(dind);
259 -exit_bh:
260         brelse(gdb_bh);
262         ext4_debug("leaving with error %d\n", err);
263 @@ -916,9 +914,9 @@ static int add_new_gdb_meta_bg(struct super_block *sb,
265         gdblock = ext4_meta_bg_first_block_no(sb, group) +
266                    ext4_bg_has_super(sb, group);
267 -       gdb_bh = sb_bread(sb, gdblock);
268 -       if (!gdb_bh)
269 -               return -EIO;
270 +       gdb_bh = ext4_sb_bread(sb, gdblock, 0);
271 +       if (IS_ERR(gdb_bh))
272 +               return PTR_ERR(gdb_bh);
273         n_group_desc = ext4_kvmalloc((gdb_num + 1) *
274                                      sizeof(struct buffer_head *),
275                                      GFP_NOFS);
276 @@ -975,9 +973,10 @@ static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
277                 return -ENOMEM;
279         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
280 -       dind = sb_bread(sb, le32_to_cpu(*data));
281 -       if (!dind) {
282 -               err = -EIO;
283 +       dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
284 +       if (IS_ERR(dind)) {
285 +               err = PTR_ERR(dind);
286 +               dind = NULL;
287                 goto exit_free;
288         }
290 @@ -996,9 +995,10 @@ static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
291                         err = -EINVAL;
292                         goto exit_bh;
293                 }
294 -               primary[res] = sb_bread(sb, blk);
295 -               if (!primary[res]) {
296 -                       err = -EIO;
297 +               primary[res] = ext4_sb_bread(sb, blk, 0);
298 +               if (IS_ERR(primary[res])) {
299 +                       err = PTR_ERR(primary[res]);
300 +                       primary[res] = NULL;
301                         goto exit_bh;
302                 }
303                 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
304 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
305 index 53ff6c2a26ed..361624460431 100644
306 --- a/fs/ext4/super.c
307 +++ b/fs/ext4/super.c
308 @@ -140,6 +140,29 @@ MODULE_ALIAS_FS("ext3");
309  MODULE_ALIAS("ext3");
310  #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
313 + * This works like sb_bread() except it uses ERR_PTR for error
314 + * returns.  Currently with sb_bread it's impossible to distinguish
315 + * between ENOMEM and EIO situations (since both result in a NULL
316 + * return.
317 + */
318 +struct buffer_head *
319 +ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags)
321 +       struct buffer_head *bh = sb_getblk(sb, block);
323 +       if (bh == NULL)
324 +               return ERR_PTR(-ENOMEM);
325 +       if (buffer_uptodate(bh))
326 +               return bh;
327 +       ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh);
328 +       wait_on_buffer(bh);
329 +       if (buffer_uptodate(bh))
330 +               return bh;
331 +       put_bh(bh);
332 +       return ERR_PTR(-EIO);
335  static int ext4_verify_csum_type(struct super_block *sb,
336                                  struct ext4_super_block *es)
338 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
339 index 7643d52c776c..510c9bb7ce71 100644
340 --- a/fs/ext4/xattr.c
341 +++ b/fs/ext4/xattr.c
342 @@ -522,14 +522,13 @@ ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
343         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
344                   name_index, name, buffer, (long)buffer_size);
346 -       error = -ENODATA;
347         if (!EXT4_I(inode)->i_file_acl)
348 -               goto cleanup;
349 +               return -ENODATA;
350         ea_idebug(inode, "reading block %llu",
351                   (unsigned long long)EXT4_I(inode)->i_file_acl);
352 -       bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
353 -       if (!bh)
354 -               goto cleanup;
355 +       bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
356 +       if (IS_ERR(bh))
357 +               return PTR_ERR(bh);
358         ea_bdebug(bh, "b_count=%d, refcount=%d",
359                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
360         error = ext4_xattr_check_block(inode, bh);
361 @@ -696,26 +695,23 @@ ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
362         ea_idebug(inode, "buffer=%p, buffer_size=%ld",
363                   buffer, (long)buffer_size);
365 -       error = 0;
366         if (!EXT4_I(inode)->i_file_acl)
367 -               goto cleanup;
368 +               return 0;
369         ea_idebug(inode, "reading block %llu",
370                   (unsigned long long)EXT4_I(inode)->i_file_acl);
371 -       bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
372 -       error = -EIO;
373 -       if (!bh)
374 -               goto cleanup;
375 +       bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
376 +       if (IS_ERR(bh))
377 +               return PTR_ERR(bh);
378         ea_bdebug(bh, "b_count=%d, refcount=%d",
379                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
380         error = ext4_xattr_check_block(inode, bh);
381         if (error)
382                 goto cleanup;
383         ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
384 -       error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
386 +       error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
387 +                                       buffer_size);
388  cleanup:
389         brelse(bh);
391         return error;
394 @@ -830,9 +826,9 @@ int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
395         }
397         if (EXT4_I(inode)->i_file_acl) {
398 -               bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
399 -               if (!bh) {
400 -                       ret = -EIO;
401 +               bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
402 +               if (IS_ERR(bh)) {
403 +                       ret = PTR_ERR(bh);
404                         goto out;
405                 }
407 @@ -1821,16 +1817,15 @@ ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
409         if (EXT4_I(inode)->i_file_acl) {
410                 /* The inode already has an extended attribute block. */
411 -               bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
412 -               error = -EIO;
413 -               if (!bs->bh)
414 -                       goto cleanup;
415 +               bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
416 +               if (IS_ERR(bs->bh))
417 +                       return PTR_ERR(bs->bh);
418                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
419                         atomic_read(&(bs->bh->b_count)),
420                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
421                 error = ext4_xattr_check_block(inode, bs->bh);
422                 if (error)
423 -                       goto cleanup;
424 +                       return error;
425                 /* Find the named attribute. */
426                 bs->s.base = BHDR(bs->bh);
427                 bs->s.first = BFIRST(bs->bh);
428 @@ -1839,13 +1834,10 @@ ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
429                 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
430                                          i->name_index, i->name, 1);
431                 if (error && error != -ENODATA)
432 -                       goto cleanup;
433 +                       return error;
434                 bs->s.not_found = error;
435         }
436 -       error = 0;
438 -cleanup:
439 -       return error;
440 +       return 0;
443  static int
444 @@ -2274,9 +2266,9 @@ static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
446         if (!EXT4_I(inode)->i_file_acl)
447                 return NULL;
448 -       bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
449 -       if (!bh)
450 -               return ERR_PTR(-EIO);
451 +       bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
452 +       if (IS_ERR(bh))
453 +               return bh;
454         error = ext4_xattr_check_block(inode, bh);
455         if (error) {
456                 brelse(bh);
457 @@ -2746,10 +2738,11 @@ int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
458         if (EXT4_I(inode)->i_file_acl) {
459                 struct buffer_head *bh;
461 -               bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
462 -               error = -EIO;
463 -               if (!bh)
464 +               bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
465 +               if (IS_ERR(bh)) {
466 +                       error = PTR_ERR(bh);
467                         goto cleanup;
468 +               }
469                 error = ext4_xattr_check_block(inode, bh);
470                 if (error) {
471                         brelse(bh);
472 @@ -2903,11 +2896,12 @@ int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
473         }
475         if (EXT4_I(inode)->i_file_acl) {
476 -               bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
477 -               if (!bh) {
478 -                       EXT4_ERROR_INODE(inode, "block %llu read error",
479 -                                        EXT4_I(inode)->i_file_acl);
480 -                       error = -EIO;
481 +               bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
482 +               if (IS_ERR(bh)) {
483 +                       error = PTR_ERR(bh);
484 +                       if (error == -EIO)
485 +                               EXT4_ERROR_INODE(inode, "block %llu read error",
486 +                                                EXT4_I(inode)->i_file_acl);
487                         goto cleanup;
488                 }
489                 error = ext4_xattr_check_block(inode, bh);
490 @@ -3060,8 +3054,10 @@ ext4_xattr_block_cache_find(struct inode *inode,
491         while (ce) {
492                 struct buffer_head *bh;
494 -               bh = sb_bread(inode->i_sb, ce->e_value);
495 -               if (!bh) {
496 +               bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
497 +               if (IS_ERR(bh)) {
498 +                       if (PTR_ERR(bh) == -ENOMEM)
499 +                               return NULL;
500                         EXT4_ERROR_INODE(inode, "block %lu read error",
501                                          (unsigned long)ce->e_value);
502                 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {