diff options
author | Qu Wenruo <wqu@suse.com> | 2017-11-07 19:54:24 -0500 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2017-11-28 08:59:09 -0500 |
commit | 69fc6cbbac542c349b3d350d10f6e394c253c81d (patch) | |
tree | 4c0fef143573a65892fea4f0e77974904f4ec3b6 | |
parent | ebb70442cdd4872260c2415929c456be3562da82 (diff) |
btrfs: tree-checker: Fix false panic for sanity test
[BUG]
If we run btrfs with CONFIG_BTRFS_FS_RUN_SANITY_TESTS=y, it will
instantly cause kernel panic like:
------
...
assertion failed: 0, file: fs/btrfs/disk-io.c, line: 3853
...
Call Trace:
btrfs_mark_buffer_dirty+0x187/0x1f0 [btrfs]
setup_items_for_insert+0x385/0x650 [btrfs]
__btrfs_drop_extents+0x129a/0x1870 [btrfs]
...
-----
[Cause]
Btrfs will call btrfs_check_leaf() in btrfs_mark_buffer_dirty() to check
if the leaf is valid with CONFIG_BTRFS_FS_RUN_SANITY_TESTS=y.
However quite some btrfs_mark_buffer_dirty() callers(*) don't really
initialize its item data but only initialize its item pointers, leaving
item data uninitialized.
This makes tree-checker catch uninitialized data as error, causing
such panic.
*: These callers include but not limited to
setup_items_for_insert()
btrfs_split_item()
btrfs_expand_item()
[Fix]
Add a new parameter @check_item_data to btrfs_check_leaf().
With @check_item_data set to false, item data check will be skipped and
fallback to old btrfs_check_leaf() behavior.
So we can still get early warning if we screw up item pointers, and
avoid false panic.
Cc: Filipe Manana <fdmanana@gmail.com>
Reported-by: Lakshmipathi.G <lakshmipathi.g@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r-- | fs/btrfs/disk-io.c | 10 | ||||
-rw-r--r-- | fs/btrfs/tree-checker.c | 27 | ||||
-rw-r--r-- | fs/btrfs/tree-checker.h | 14 |
3 files changed, 43 insertions, 8 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index efce9a2fa9be..10a2a579cc7f 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c | |||
@@ -610,7 +610,7 @@ static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, | |||
610 | * that we don't try and read the other copies of this block, just | 610 | * that we don't try and read the other copies of this block, just |
611 | * return -EIO. | 611 | * return -EIO. |
612 | */ | 612 | */ |
613 | if (found_level == 0 && btrfs_check_leaf(root, eb)) { | 613 | if (found_level == 0 && btrfs_check_leaf_full(root, eb)) { |
614 | set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); | 614 | set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); |
615 | ret = -EIO; | 615 | ret = -EIO; |
616 | } | 616 | } |
@@ -3848,7 +3848,13 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf) | |||
3848 | buf->len, | 3848 | buf->len, |
3849 | fs_info->dirty_metadata_batch); | 3849 | fs_info->dirty_metadata_batch); |
3850 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY | 3850 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
3851 | if (btrfs_header_level(buf) == 0 && btrfs_check_leaf(root, buf)) { | 3851 | /* |
3852 | * Since btrfs_mark_buffer_dirty() can be called with item pointer set | ||
3853 | * but item data not updated. | ||
3854 | * So here we should only check item pointers, not item data. | ||
3855 | */ | ||
3856 | if (btrfs_header_level(buf) == 0 && | ||
3857 | btrfs_check_leaf_relaxed(root, buf)) { | ||
3852 | btrfs_print_leaf(buf); | 3858 | btrfs_print_leaf(buf); |
3853 | ASSERT(0); | 3859 | ASSERT(0); |
3854 | } | 3860 | } |
diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 114fc5f0ecc5..ce4ed6ec8f39 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c | |||
@@ -242,7 +242,8 @@ static int check_leaf_item(struct btrfs_root *root, | |||
242 | return ret; | 242 | return ret; |
243 | } | 243 | } |
244 | 244 | ||
245 | int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf) | 245 | static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, |
246 | bool check_item_data) | ||
246 | { | 247 | { |
247 | struct btrfs_fs_info *fs_info = root->fs_info; | 248 | struct btrfs_fs_info *fs_info = root->fs_info; |
248 | /* No valid key type is 0, so all key should be larger than this key */ | 249 | /* No valid key type is 0, so all key should be larger than this key */ |
@@ -361,10 +362,15 @@ int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf) | |||
361 | return -EUCLEAN; | 362 | return -EUCLEAN; |
362 | } | 363 | } |
363 | 364 | ||
364 | /* Check if the item size and content meet other criteria */ | 365 | if (check_item_data) { |
365 | ret = check_leaf_item(root, leaf, &key, slot); | 366 | /* |
366 | if (ret < 0) | 367 | * Check if the item size and content meet other |
367 | return ret; | 368 | * criteria |
369 | */ | ||
370 | ret = check_leaf_item(root, leaf, &key, slot); | ||
371 | if (ret < 0) | ||
372 | return ret; | ||
373 | } | ||
368 | 374 | ||
369 | prev_key.objectid = key.objectid; | 375 | prev_key.objectid = key.objectid; |
370 | prev_key.type = key.type; | 376 | prev_key.type = key.type; |
@@ -374,6 +380,17 @@ int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf) | |||
374 | return 0; | 380 | return 0; |
375 | } | 381 | } |
376 | 382 | ||
383 | int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf) | ||
384 | { | ||
385 | return check_leaf(root, leaf, true); | ||
386 | } | ||
387 | |||
388 | int btrfs_check_leaf_relaxed(struct btrfs_root *root, | ||
389 | struct extent_buffer *leaf) | ||
390 | { | ||
391 | return check_leaf(root, leaf, false); | ||
392 | } | ||
393 | |||
377 | int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) | 394 | int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) |
378 | { | 395 | { |
379 | unsigned long nr = btrfs_header_nritems(node); | 396 | unsigned long nr = btrfs_header_nritems(node); |
diff --git a/fs/btrfs/tree-checker.h b/fs/btrfs/tree-checker.h index 96c486e95d70..3d53e8d6fda0 100644 --- a/fs/btrfs/tree-checker.h +++ b/fs/btrfs/tree-checker.h | |||
@@ -20,7 +20,19 @@ | |||
20 | #include "ctree.h" | 20 | #include "ctree.h" |
21 | #include "extent_io.h" | 21 | #include "extent_io.h" |
22 | 22 | ||
23 | int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf); | 23 | /* |
24 | * Comprehensive leaf checker. | ||
25 | * Will check not only the item pointers, but also every possible member | ||
26 | * in item data. | ||
27 | */ | ||
28 | int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf); | ||
29 | |||
30 | /* | ||
31 | * Less strict leaf checker. | ||
32 | * Will only check item pointers, not reading item data. | ||
33 | */ | ||
34 | int btrfs_check_leaf_relaxed(struct btrfs_root *root, | ||
35 | struct extent_buffer *leaf); | ||
24 | int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node); | 36 | int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node); |
25 | 37 | ||
26 | #endif | 38 | #endif |