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
8 So instead we create our own ext4_sb_bread(), which also allows us to
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")
17 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
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
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);
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,
45 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
47 - bh = sb_bread(inode->i_sb, pblock);
50 + bh = ext4_sb_bread(inode->i_sb, pblock, 0);
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,
58 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
60 - bh = sb_bread(inode->i_sb, pblock);
63 + bh = ext4_sb_bread(inode->i_sb, pblock, 0);
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,
71 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
73 - bh = sb_bread(inode->i_sb, pblock);
76 + bh = ext4_sb_bread(inode->i_sb, pblock, 0);
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));
89 + bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
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));
102 + bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
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);
115 + bh = ext4_sb_bread(inode->i_sb, block, 0);
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))) {
133 ext4_warning(sb, "Cannot read last block (%llu)",
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;
151 - struct ext4_iloc iloc;
152 + struct ext4_iloc iloc = { .bh = NULL };
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",
160 - gdb_bh = sb_bread(sb, gdblock);
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);
174 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
175 - dind = sb_bread(sb, le32_to_cpu(*data));
179 + dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
180 + if (IS_ERR(dind)) {
181 + err = PTR_ERR(dind);
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",
195 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
196 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
201 BUFFER_TRACE(gdb_bh, "get_write_access");
202 err = ext4_journal_get_write_access(handle, gdb_bh);
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);
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,
220 ext4_warning(sb, "not enough memory for %lu groups",
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);
230 ext4_std_error(sb, err);
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);
239 ext4_std_error(sb, err);
246 @@ -888,15 +890,11 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
247 err = ext4_handle_dirty_super(handle, sb);
249 ext4_std_error(sb, err);
255 kvfree(n_group_desc);
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);
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 *),
276 @@ -975,9 +973,10 @@ static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
279 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
280 - dind = sb_bread(sb, le32_to_cpu(*data));
283 + dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
284 + if (IS_ERR(dind)) {
285 + err = PTR_ERR(dind);
290 @@ -996,9 +995,10 @@ static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
294 - primary[res] = sb_bread(sb, blk);
295 - if (!primary[res]) {
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;
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
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);
324 + return ERR_PTR(-ENOMEM);
325 + if (buffer_uptodate(bh))
327 + ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh);
328 + wait_on_buffer(bh);
329 + if (buffer_uptodate(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);
347 if (!EXT4_I(inode)->i_file_acl)
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);
355 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
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);
366 if (!EXT4_I(inode)->i_file_acl)
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);
375 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
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);
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,
394 @@ -830,9 +826,9 @@ int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
397 if (EXT4_I(inode)->i_file_acl) {
398 - bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
401 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
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);
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);
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)
434 bs->s.not_found = error;
444 @@ -2274,9 +2266,9 @@ static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
446 if (!EXT4_I(inode)->i_file_acl)
448 - bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
450 - return ERR_PTR(-EIO);
451 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
454 error = ext4_xattr_check_block(inode, 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);
464 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
466 + error = PTR_ERR(bh);
469 error = ext4_xattr_check_block(inode, bh);
472 @@ -2903,11 +2896,12 @@ int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
475 if (EXT4_I(inode)->i_file_acl) {
476 - bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
478 - EXT4_ERROR_INODE(inode, "block %llu read error",
479 - EXT4_I(inode)->i_file_acl);
481 + bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
483 + error = PTR_ERR(bh);
485 + EXT4_ERROR_INODE(inode, "block %llu read error",
486 + EXT4_I(inode)->i_file_acl);
489 error = ext4_xattr_check_block(inode, bh);
490 @@ -3060,8 +3054,10 @@ ext4_xattr_block_cache_find(struct inode *inode,
492 struct buffer_head *bh;
494 - bh = sb_bread(inode->i_sb, ce->e_value);
496 + bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
498 + if (PTR_ERR(bh) == -ENOMEM)
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) {