Skip to content

Commit 46f870d

Browse files
committed
ext4: simulate various I/O and checksum errors when reading metadata
This allows us to test various error handling code paths Link: https://lore.kernel.org/r/20191209012317.59398-1-tytso@mit.edu Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 878520a commit 46f870d

6 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎fs/ext4/balloc.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
371371
if (buffer_verified(bh))
372372
goto verified;
373373
if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
374-
desc, bh))) {
374+
desc, bh) ||
375+
ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_CRC))) {
375376
ext4_unlock_group(sb, block_group);
376377
ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
377378
ext4_mark_group_bitmap_corrupted(sb, block_group,
@@ -505,6 +506,7 @@ int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
505506
if (!desc)
506507
return -EFSCORRUPTED;
507508
wait_on_buffer(bh);
509+
ext4_simulate_fail_bh(sb, bh, EXT4_SIM_BBITMAP_EIO);
508510
if (!buffer_uptodate(bh)) {
509511
ext4_set_errno(sb, EIO);
510512
ext4_error(sb, "Cannot read block bitmap - "

‎fs/ext4/ext4.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,9 @@ struct ext4_sb_info {
15571557
/* Barrier between changing inodes' journal flags and writepages ops. */
15581558
struct percpu_rw_semaphore s_journal_flag_rwsem;
15591559
struct dax_device *s_daxdev;
1560+
#ifdef CONFIG_EXT4_DEBUG
1561+
unsigned long s_simulate_fail;
1562+
#endif
15601563
};
15611564

15621565
static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1575,6 +1578,40 @@ static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
15751578
ino <= le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count));
15761579
}
15771580

1581+
/*
1582+
* Simulate_fail codes
1583+
*/
1584+
#define EXT4_SIM_BBITMAP_EIO 1
1585+
#define EXT4_SIM_BBITMAP_CRC 2
1586+
#define EXT4_SIM_IBITMAP_EIO 3
1587+
#define EXT4_SIM_IBITMAP_CRC 4
1588+
#define EXT4_SIM_INODE_EIO 5
1589+
#define EXT4_SIM_INODE_CRC 6
1590+
#define EXT4_SIM_DIRBLOCK_EIO 7
1591+
#define EXT4_SIM_DIRBLOCK_CRC 8
1592+
1593+
static inline bool ext4_simulate_fail(struct super_block *sb,
1594+
unsigned long code)
1595+
{
1596+
#ifdef CONFIG_EXT4_DEBUG
1597+
struct ext4_sb_info *sbi = EXT4_SB(sb);
1598+
1599+
if (unlikely(sbi->s_simulate_fail == code)) {
1600+
sbi->s_simulate_fail = 0;
1601+
return true;
1602+
}
1603+
#endif
1604+
return false;
1605+
}
1606+
1607+
static inline void ext4_simulate_fail_bh(struct super_block *sb,
1608+
struct buffer_head *bh,
1609+
unsigned long code)
1610+
{
1611+
if (!IS_ERR(bh) && ext4_simulate_fail(sb, code))
1612+
clear_buffer_uptodate(bh);
1613+
}
1614+
15781615
/*
15791616
* Error number codes for s_{first,last}_error_errno
15801617
*

‎fs/ext4/ialloc.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ static int ext4_validate_inode_bitmap(struct super_block *sb,
9494
goto verified;
9595
blk = ext4_inode_bitmap(sb, desc);
9696
if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
97-
EXT4_INODES_PER_GROUP(sb) / 8)) {
97+
EXT4_INODES_PER_GROUP(sb) / 8) ||
98+
ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) {
9899
ext4_unlock_group(sb, block_group);
99100
ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
100101
"inode_bitmap = %llu", block_group, blk);
@@ -192,6 +193,7 @@ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
192193
get_bh(bh);
193194
submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
194195
wait_on_buffer(bh);
196+
ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO);
195197
if (!buffer_uptodate(bh)) {
196198
put_bh(bh);
197199
ext4_set_errno(sb, EIO);

‎fs/ext4/inode.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,8 @@ static int __ext4_get_inode_loc(struct inode *inode,
42434243
bh = sb_getblk(sb, block);
42444244
if (unlikely(!bh))
42454245
return -ENOMEM;
4246+
if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4247+
goto simulate_eio;
42464248
if (!buffer_uptodate(bh)) {
42474249
lock_buffer(bh);
42484250

@@ -4341,6 +4343,7 @@ static int __ext4_get_inode_loc(struct inode *inode,
43414343
blk_finish_plug(&plug);
43424344
wait_on_buffer(bh);
43434345
if (!buffer_uptodate(bh)) {
4346+
simulate_eio:
43444347
ext4_set_errno(inode->i_sb, EIO);
43454348
EXT4_ERROR_INODE_BLOCK(inode, block,
43464349
"unable to read itable block");
@@ -4555,7 +4558,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
45554558
sizeof(gen));
45564559
}
45574560

4558-
if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4561+
if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4562+
ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
45594563
ext4_set_errno(inode->i_sb, EFSBADCRC);
45604564
ext4_error_inode(inode, function, line, 0,
45614565
"iget: checksum invalid");

‎fs/ext4/namei.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
109109
struct ext4_dir_entry *dirent;
110110
int is_dx_block = 0;
111111

112-
bh = ext4_bread(NULL, inode, block, 0);
112+
if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
113+
bh = ERR_PTR(-EIO);
114+
else
115+
bh = ext4_bread(NULL, inode, block, 0);
113116
if (IS_ERR(bh)) {
114117
__ext4_warning(inode->i_sb, func, line,
115118
"inode #%lu: lblock %lu: comm %s: "
@@ -153,7 +156,8 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
153156
* caller is sure it should be an index block.
154157
*/
155158
if (is_dx_block && type == INDEX) {
156-
if (ext4_dx_csum_verify(inode, dirent))
159+
if (ext4_dx_csum_verify(inode, dirent) &&
160+
!ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
157161
set_buffer_verified(bh);
158162
else {
159163
ext4_set_errno(inode->i_sb, EFSBADCRC);
@@ -164,7 +168,8 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
164168
}
165169
}
166170
if (!is_dx_block) {
167-
if (ext4_dirblock_csum_verify(inode, bh))
171+
if (ext4_dirblock_csum_verify(inode, bh) &&
172+
!ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
168173
set_buffer_verified(bh);
169174
else {
170175
ext4_set_errno(inode->i_sb, EFSBADCRC);

‎fs/ext4/sysfs.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef enum {
2929
attr_last_error_time,
3030
attr_feature,
3131
attr_pointer_ui,
32+
attr_pointer_ul,
3233
attr_pointer_atomic,
3334
attr_journal_task,
3435
} attr_id_t;
@@ -160,6 +161,9 @@ static struct ext4_attr ext4_attr_##_name = { \
160161
#define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
161162
EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
162163

164+
#define EXT4_RW_ATTR_SBI_UL(_name,_elname) \
165+
EXT4_ATTR_OFFSET(_name, 0644, pointer_ul, ext4_sb_info, _elname)
166+
163167
#define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
164168
static struct ext4_attr ext4_attr_##_name = { \
165169
.attr = {.name = __stringify(_name), .mode = _mode }, \
@@ -194,6 +198,9 @@ EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.int
194198
EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
195199
EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
196200
EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
201+
#ifdef CONFIG_EXT4_DEBUG
202+
EXT4_RW_ATTR_SBI_UL(simulate_fail, s_simulate_fail);
203+
#endif
197204
EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
198205
EXT4_ATTR(first_error_time, 0444, first_error_time);
199206
EXT4_ATTR(last_error_time, 0444, last_error_time);
@@ -228,6 +235,9 @@ static struct attribute *ext4_attrs[] = {
228235
ATTR_LIST(first_error_time),
229236
ATTR_LIST(last_error_time),
230237
ATTR_LIST(journal_task),
238+
#ifdef CONFIG_EXT4_DEBUG
239+
ATTR_LIST(simulate_fail),
240+
#endif
231241
NULL,
232242
};
233243
ATTRIBUTE_GROUPS(ext4);
@@ -318,6 +328,11 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
318328
else
319329
return snprintf(buf, PAGE_SIZE, "%u\n",
320330
*((unsigned int *) ptr));
331+
case attr_pointer_ul:
332+
if (!ptr)
333+
return 0;
334+
return snprintf(buf, PAGE_SIZE, "%lu\n",
335+
*((unsigned long *) ptr));
321336
case attr_pointer_atomic:
322337
if (!ptr)
323338
return 0;
@@ -361,6 +376,14 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
361376
else
362377
*((unsigned int *) ptr) = t;
363378
return len;
379+
case attr_pointer_ul:
380+
if (!ptr)
381+
return 0;
382+
ret = kstrtoul(skip_spaces(buf), 0, &t);
383+
if (ret)
384+
return ret;
385+
*((unsigned long *) ptr) = t;
386+
return len;
364387
case attr_inode_readahead:
365388
return inode_readahead_blks_store(sbi, buf, len);
366389
case attr_trigger_test_error:

0 commit comments

Comments
 (0)