btrfs: Enhancement to tree block validation
v5 cover btrfs: Enhancement to tree block validation
Patchset can be fetched from github:
https://github.com/adam900710/linux/tree/write_time_tree_checker
Which is based on v5.0-rc1 tag.
Also there is no conflict rebasing the patchset to misc-next.
This patchset has the following 3 features:
- Tree block validation output enhancement
* Output validation failure timing (write time or read time)
* Always output tree block level/key mismatch error message This part is already submitted and reviewed.
- Write time tree block validation check
To catch memory corruption either from hardware or kernel. Example output would be:
BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
BTRFS: error (device dm-3) in btrfs_commit_transaction:2220: errno=-5 IO failure (Error while writing out transaction)
BTRFS info (device dm-3): forced readonly
BTRFS warning (device dm-3): Skipping commit of aborted transaction.
BTRFS: error (device dm-3) in cleanup_transaction:1839: errno=-5 IO failure
BTRFS info (device dm-3): delayed_refs has NO entry
- Better error handling before calling flush_write_bio() One hidden reason of calling flush_write_bio() under all cases is, flush_write_bio() will trigger endio function and endio function of epd->bio will free the bio under all cases.
So we're in fact abusing flush_write_bio() as cleanup.
Since now flush_write_bio() has its own return value, we shouldn't call flush_write_bio() no-brain, here we introduce proper cleanup helper, end_write_bio(). Now we call flush_write_bio() like:
New | Old
--------------------------------------------------------------
ret = do_some_evil(&epd); | ret = do_some_evil(&epd);
if (ret < 0) { | flush_write_bio(&epd);
end_write_bio(&epd, ret); | ^^^ submitting half-backed epd->bio?
return ret; | return ret;
} | ret = flush_write_bio(&epd); | return ret; |
Above code should be more streamline for the error handling part.
Changelog:
v2:
- Unlock locked pages in lock_extent_buffer_for_io() for error handling.
- Added Reviewed-by tags.
v3:
- Remove duplicated error message.
- Use IS_ENABLED() macro to replace #ifdef.
- Added Reviewed-by tags.
v4:
- Re-organized patch split
Now each BUG_ON() cleanup has its own patch
- Dig much further into the call sites to eliminate unexpected >0 return May be a little paranoid and abuse some ASSERT(), but it should be much safer against further code change.
- Fix the false alert caused by balance and memory pressure The fix it skip owner checker for non-essential tree at write time. Since owner root can't always be reliable, either due to commit root created in current transaction or balance + memory pressure.
v5:
- Do proper error-out handling other than relying on flush_write_bio() to clean up.
This has a side effect that no Reviewed-by tags for modified patches.
- New comment for why we don't need to do anything about ebp->bio when submit_one_bio() fails.
- Add some Reviewed-by tag.
Qu Wenruo (12):
btrfs: Always output error message when key/level verification fails
btrfs: extent_io: Kill the forward declaration of flush_write_bio()
btrfs: disk-io: Show the timing of corrupted tree block explicitly
btrfs: extent_io: Move the BUG_ON() in flush_write_bio() one level up
btrfs: extent_io: Handle error better in extent_write_full_page()
btrfs: extent_io: Handle error better in btree_write_cache_pages()
btrfs: extent_io: Kill the dead branch in extent_write_cache_pages()
btrfs: extent_io: Handle error better in extent_write_locked_range()
btrfs: extent_io: Kill the BUG_ON() in lock_extent_buffer_for_io()
btrfs: extent_io: Kill the BUG_ON() in extent_write_cache_pages()
btrfs: extent_io: Handle error better in extent_writepages()
btrfs: Do mandatory tree block check before submitting bio
fs/btrfs/disk-io.c | 21 +++-- fs/btrfs/extent_io.c | 168 ++++++++++++++++++++++++++++------------ fs/btrfs/tree-checker.c | 24 +++++- fs/btrfs/tree-checker.h | 8 ++ 4 files changed, 162 insertions(+), 59 deletions(-)
Signature · 1 line
2.20.1
PATCH v5 1/12 btrfs: Always output error message when key/level verification fails
We have internal report of strange transaction abort due to EUCLEAN without any error message.
Since error message inside verify_level_key() is only enabled for CONFIG_BTRFS_DEBUG, the error message won't output for most distro.
This patch will make the error message mandatory, so when problem happens we know what's causing the problem.
--- fs/btrfs/disk-io.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +4 -6
@@ -423,12 +423,11 @@ static int verify_level_key(struct btrfs_fs_info *fs_info, found_level =btrfs_header_level (eb);if (found_level != level) { -#ifdef CONFIG_BTRFS_DEBUG -WARN_ON (1 ); +WARN (IS_ENABLED (CONFIG_BTRFS_DEBUG ), +KERN_ERR "BTRFS: tree level check failed\n" );btrfs_err (fs_info,"tree level mismatch detected, bytenr=%llu level expected=%u has=%u" , eb->start, level, found_level); -#endif return -EIO ; }
@@ -449,9 +448,9 @@ static int verify_level_key(struct btrfs_fs_info *fs_info,btrfs_item_key_to_cpu (eb, &found_key,0 ); ret =btrfs_comp_cpu_keys (first_key, &found_key); -#ifdef CONFIG_BTRFS_DEBUG if (ret) { -WARN_ON (1 ); +WARN (IS_ENABLED (CONFIG_BTRFS_DEBUG ), +KERN_ERR "BTRFS: tree first key check failed\n" ); btrfs_err(fs_info, "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", eb->start, parent_transid, first_key->objectid,
@@ -459,7 +458,6 @@ static int verify_level_key(struct btrfs_fs_info *fs_info, found_key.objectid, found_key.type, found_key.offset); } -#endif return ret; }
Signature · 1 line
2.20.1
PATCH v5 2/12 btrfs: extent_io: Kill the forward declaration of flush_write_bio()
In reply to Qu WenruoThere is no need to forward declare flush_write_bio(), as it only depends on submit_one_bio().
Both of them are pretty small, just move them to kill the forward declaration.
--- fs/btrfs/extent_io.c | 66 +++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 34 deletions(-)
fs/btrfs/extent_io.c +32 -34
@@ -147,7 +147,38 @@ static int add_extent_changeset(struct extent_state *state, unsigned bits,return ret; } -static void flush_write_bio (struct extent_page_data *epd); +static int __must_check submit_one_bio (struct bio *bio,int mirror_num, +unsigned long bio_flags) +{ +blk_status_t ret =0 ; +struct bio_vec *bvec =bio_last_bvec_all (bio); +struct page *page = bvec->bv_page; +struct extent_io_tree *tree = bio->bi_private; +u64 start; + + start =page_offset (page) + bvec->bv_offset; + + bio->bi_private =NULL ; + +if (tree->ops) + ret = tree->ops->submit_bio_hook (tree->private_data, bio, + mirror_num, bio_flags, start); +else +btrfsic_submit_bio (bio); + +return blk_status_to_errno (ret); +} + +static void flush_write_bio (struct extent_page_data *epd) +{ +if (epd->bio) { +int ret; + + ret =submit_one_bio (epd->bio,0 ,0 ); +BUG_ON (ret <0 );/* -ENOMEM */ + epd->bio =NULL ; + } +}int __init extent_io_init (void ) {
@@ -2692,28 +2723,6 @@ struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)return bio; } -static int __must_check submit_one_bio (struct bio *bio,int mirror_num, -unsigned long bio_flags) -{ -blk_status_t ret =0 ; -struct bio_vec *bvec =bio_last_bvec_all (bio); -struct page *page = bvec->bv_page; -struct extent_io_tree *tree = bio->bi_private; -u64 start; - - start =page_offset (page) + bvec->bv_offset; - - bio->bi_private =NULL ; - -if (tree->ops) - ret = tree->ops->submit_bio_hook (tree->private_data, bio, - mirror_num, bio_flags, start); -else -btrfsic_submit_bio (bio); - -return blk_status_to_errno (ret); -} -/* * @opf: bio REQ_OP_* and REQ_* flags as one value * @tree: tree so we can call our merge_bio hook
@@ -4007,17 +4016,6 @@ static int extent_write_cache_pages(struct address_space *mapping,return ret; } -static void flush_write_bio (struct extent_page_data *epd) -{ -if (epd->bio) { -int ret; - - ret =submit_one_bio (epd->bio,0 ,0 ); -BUG_ON (ret <0 );/* -ENOMEM */ - epd->bio =NULL ; - } -} -int extent_write_full_page (struct page *page,struct writeback_control *wbc) {int ret;
Signature · 1 line
2.20.1
PATCH v5 3/12 btrfs: disk-io: Show the timing of corrupted tree block explicitly
In reply to Qu WenruoJust add one extra line to show when the corruption is detected. Currently only read time detection is possible.
The planned distinguish line would be:
read time:
<detail report>
read time tree block corruption detected
write time:
<detail report>
write time tree block corruption detected
--- fs/btrfs/disk-io.c | 2 ++ 1 file changed, 2 insertions(+)
fs/btrfs/disk-io.c +2 -0
@@ -658,6 +658,8 @@ static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,if (!ret)set_extent_buffer_uptodate (eb); +else +btrfs_err (fs_info,"read time tree block corruption detected" ); err:if (reads_done &&test_and_clear_bit (EXTENT_BUFFER_READAHEAD , &eb->bflags))
Signature · 1 line
2.20.1
PATCH v5 4/12 btrfs: extent_io: Move the BUG_ON() in flush_write_bio() one level up
In reply to Qu WenruoWe have a BUG_ON() in flush_write_bio() to handle the return value of submit_one_bio().
Move the BUG_ON() one level up to all its callers.
This patch will introduce temporary variable, @flush_ret to keep code change minimal in this patch. That variable will be cleaned up when enhancing the error handling later.
--- fs/btrfs/extent_io.c | 55 +++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 14 deletions(-)
fs/btrfs/extent_io.c +41 -14
@@ -169,15 +169,28 @@ static int __must_check submit_one_bio(struct bio *bio, int mirror_num,return blk_status_to_errno (ret); } -static void flush_write_bio (struct extent_page_data *epd) +/* +* A wrapper for submit_one_bio(). +* +* Return 0 if everything is OK. +* Return <0 for error. +*/ +static int __must_check flush_write_bio (struct extent_page_data *epd) { -if (epd->bio) { -int ret; +int ret =0 ; +if (epd->bio) { ret =submit_one_bio (epd->bio,0 ,0 ); -BUG_ON (ret <0 );/* -ENOMEM */ +/* +* Clean up of epd->bio is handled by its endio function. +* And endio is either triggered by successful bio execution +* or the error handler of submit bio hook. +* So at this point, no matter what happened, we don't need +* to clean up epd->bio. +*/ epd->bio =NULL ; } +return ret; }int __init extent_io_init (void )
@@ -3510,7 +3523,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,if (!btrfs_try_tree_write_lock (eb)) { flush =1 ; -flush_write_bio (epd); + ret =flush_write_bio (epd); +BUG_ON (ret <0 );btrfs_tree_lock (eb); }
@@ -3519,7 +3533,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,if (!epd->sync_io)return 0 ;if (!flush) { -flush_write_bio (epd); + ret =flush_write_bio (epd); +BUG_ON (ret <0 ); flush =1 ; }while (1 ) {
@@ -3560,7 +3575,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,if (!trylock_page (p)) {if (!flush) { -flush_write_bio (epd); + ret =flush_write_bio (epd); +BUG_ON (ret <0 ); flush =1 ; }lock_page (p);
@@ -3751,6 +3767,7 @@ int btree_write_cache_pages(struct address_space *mapping, .sync_io = wbc->sync_mode == WB_SYNC_ALL, };int ret =0 ; +int flush_ret;int done =0 ;int nr_to_write_done =0 ;struct pagevec pvec;
@@ -3850,7 +3867,8 @@ int btree_write_cache_pages(struct address_space *mapping, index =0 ; goto retry; } -flush_write_bio (&epd); + flush_ret =flush_write_bio (&epd); +BUG_ON (flush_ret <0 );return ret; }
@@ -3947,7 +3965,8 @@ static int extent_write_cache_pages(struct address_space *mapping,* tmpfs file mapping */ if (!trylock_page (page)) { -flush_write_bio (epd); + ret =flush_write_bio (epd); +BUG_ON (ret <0 );lock_page (page); }
@@ -3957,8 +3976,10 @@ static int extent_write_cache_pages(struct address_space *mapping, }if (wbc->sync_mode !=WB_SYNC_NONE ) { -if (PageWriteback (page)) -flush_write_bio (epd); +if (PageWriteback (page)) { + ret =flush_write_bio (epd); +BUG_ON (ret <0 ); + }wait_on_page_writeback (page); }
@@ -4019,6 +4040,7 @@ static int extent_write_cache_pages(struct address_space *mapping,int extent_write_full_page (struct page *page,struct writeback_control *wbc) {int ret; +int flush_ret;struct extent_page_data epd = { .bio =NULL , .tree = &BTRFS_I (page->mapping->host)->io_tree,
@@ -4028,7 +4050,8 @@ int extent_write_full_page(struct page *page, struct writeback_control *wbc) ret =__extent_writepage (page, wbc, &epd); -flush_write_bio (&epd); + flush_ret =flush_write_bio (&epd); +BUG_ON (flush_ret <0 );return ret; }
@@ -4036,6 +4059,7 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end, int mode) {int ret =0 ; +int flush_ret;struct address_space *mapping = inode->i_mapping;struct extent_io_tree *tree = &BTRFS_I (inode)->io_tree;struct page *page;
@@ -4068,7 +4092,8 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end, start +=PAGE_SIZE ; } -flush_write_bio (&epd); + flush_ret =flush_write_bio (&epd); +BUG_ON (flush_ret <0 );return ret; }
@@ -4076,6 +4101,7 @@ int extent_writepages(struct address_space *mapping, struct writeback_control *wbc) {int ret =0 ; +int flush_ret;struct extent_page_data epd = { .bio =NULL , .tree = &BTRFS_I (mapping->host)->io_tree,
@@ -4084,7 +4110,8 @@ int extent_writepages(struct address_space *mapping, }; ret =extent_write_cache_pages (mapping, wbc, &epd); -flush_write_bio (&epd); + flush_ret =flush_write_bio (&epd); +BUG_ON (flush_ret <0 );return ret; }
Signature · 1 line
2.20.1
PATCH v5 5/12 btrfs: extent_io: Handle error better in extent_write_full_page()
In reply to Qu WenruoSince now flush_write_bio() could return error, kill the BUG_ON() first.
Then don't call flush_write_bio() unconditionally, instead we check the return value from __extent_writepage() first.
If __extent_writepage() fails, we do cleanup, and return error without submitting the possible corrupted or half-baked bio.
If __extent_writepage() successes, then we call flush_write_bio() and return the result.
--- fs/btrfs/extent_io.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +21 -3
@@ -169,6 +169,16 @@ static int __must_check submit_one_bio(struct bio *bio, int mirror_num,return blk_status_to_errno (ret); } +/* A wrapper for bio_endio() to cleanup unsubmitted bios */ +static void end_write_bio (struct extent_page_data *epd,int ret) +{ +if (epd->bio) { + epd->bio->bi_status =errno_to_blk_status (ret); +bio_endio (epd->bio); + epd->bio =NULL ; + } +} +/* * A wrapper for submit_one_bio(). *
@@ -3431,6 +3441,9 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,* records are inserted to lock ranges in the tree, and as dirty areas * are found, they are marked writeback. Then the lock bits are removed * and the end_io handler clears the writeback ranges +* +* Return 0 if everything goes well. +* Return <0 for error. */ static int __extent_writepage (struct page *page,struct writeback_control *wbc,struct extent_page_data *epd)
@@ -3500,6 +3513,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,end_extent_writepage (page, ret, start, page_end); }unlock_page (page); +ASSERT (ret <=0 );return ret; done_unlocked:
@@ -4040,7 +4054,6 @@ static int extent_write_cache_pages(struct address_space *mapping,int extent_write_full_page (struct page *page,struct writeback_control *wbc) {int ret; -int flush_ret;struct extent_page_data epd = { .bio =NULL , .tree = &BTRFS_I (page->mapping->host)->io_tree,
@@ -4049,9 +4062,14 @@ int extent_write_full_page(struct page *page, struct writeback_control *wbc) }; ret =__extent_writepage (page, wbc, &epd); +ASSERT (ret <=0 ); +if (ret <0 ) { +end_write_bio (&epd, ret); +return ret; + } - flush_ret =flush_write_bio (&epd); -BUG_ON (flush_ret <0 ); + ret =flush_write_bio (&epd); +ASSERT (ret <=0 );return ret; }
Signature · 1 line
2.20.1
PATCH v5 6/12 btrfs: extent_io: Handle error better in btree_write_cache_pages()
In reply to Qu WenruoIn btree_write_cache_pages(), we can only get @ret <= 0. Add an ASSERT() for it just in case.
Then instead of submitting the write bio even we got some error, check the return value first.
If we have already hit some error, just clean up the corrupted or half-baked bio, and return error.
If there is no error so far, then call flush_write_bio() and return the result.
--- fs/btrfs/extent_io.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +6 -3
@@ -3781,7 +3781,6 @@ int btree_write_cache_pages(struct address_space *mapping, .sync_io = wbc->sync_mode == WB_SYNC_ALL, };int ret =0 ; -int flush_ret;int done =0 ;int nr_to_write_done =0 ;struct pagevec pvec;
@@ -3881,8 +3880,12 @@ int btree_write_cache_pages(struct address_space *mapping, index =0 ; goto retry; } - flush_ret =flush_write_bio (&epd); -BUG_ON (flush_ret <0 ); +ASSERT (ret <=0 ); +if (ret <0 ) { +end_write_bio (&epd, ret); +return ret; + } + ret =flush_write_bio (&epd);return ret; }
Signature · 1 line
2.20.1
PATCH v5 7/12 btrfs: extent_io: Kill the dead branch in extent_write_cache_pages()
In reply to Qu WenruoSince __extent_writepage() will no longer return >0 value, (ret == AOP_WRITEPAGE_ACTIVATE) will never be true.
Kill that dead branch.
--- fs/btrfs/extent_io.c | 5 ----- 1 file changed, 5 deletions(-)
fs/btrfs/extent_io.c +0 -5
@@ -4007,11 +4007,6 @@ static int extent_write_cache_pages(struct address_space *mapping, } ret =__extent_writepage (page, wbc, epd); - -if (unlikely (ret ==AOP_WRITEPAGE_ACTIVATE )) { -unlock_page (page); - ret =0 ; - }if (ret <0 ) {/* * done_index is set past this page,
Signature · 1 line
2.20.1
PATCH v5 8/12 btrfs: extent_io: Handle error better in extent_write_locked_range()
In reply to Qu WenruoDo proper cleanup if we hit any error in extent_write_locked_range(), and check the return value of flush_write_bio().
--- fs/btrfs/extent_io.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +6 -3
@@ -4075,7 +4075,6 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end, int mode) {int ret =0 ; -int flush_ret;struct address_space *mapping = inode->i_mapping;struct extent_io_tree *tree = &BTRFS_I (inode)->io_tree;struct page *page;
@@ -4108,8 +4107,12 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end, start +=PAGE_SIZE ; } - flush_ret =flush_write_bio (&epd); -BUG_ON (flush_ret <0 ); +ASSERT (ret <=0 ); +if (ret <0 ) { +end_write_bio (&epd, ret); +return ret; + } + ret =flush_write_bio (&epd);return ret; }
Signature · 1 line
2.20.1
PATCH v5 9/12 btrfs: extent_io: Kill the BUG_ON() in lock_extent_buffer_for_io()
In reply to Qu WenruoThis function needs some extra check on locked pages and eb.
For error handling we need to unlock locked pages and the eb.
Also add comment for possible return values of lock_extent_buffer_for_io().
There is a rare >0 return value branch, where all pages get locked while write bio is not flushed.
Thankfully it's handled by the only caller, btree_write_cache_pages(), as later write_one_eb() call will trigger submit_one_bio(). So there shouldn't be any problem.
--- fs/btrfs/extent_io.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
fs/btrfs/extent_io.c +20 -5
@@ -3526,19 +3526,25 @@ void wait_on_extent_buffer_writeback(struct extent_buffer *eb) TASK_UNINTERRUPTIBLE); } +/* +* Return 0 if nothing went wrong, its pages get locked and submitted. +* Return >0 is mostly the same as 0, except bio is not submitted. +* Return <0 if something went wrong, no page get locked. +*/ static noinline_for_stack intlock_extent_buffer_for_io (struct extent_buffer *eb,struct btrfs_fs_info *fs_info,struct extent_page_data *epd) { -int i, num_pages; +int i, num_pages, failed_page_nr;int flush =0 ;int ret =0 ;if (!btrfs_try_tree_write_lock (eb)) { - flush =1 ; ret =flush_write_bio (epd); -BUG_ON (ret <0 ); +if (ret <0 ) +return ret; + flush =1 ;btrfs_tree_lock (eb); }
@@ -3548,7 +3554,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,return 0 ;if (!flush) { ret =flush_write_bio (epd); -BUG_ON (ret <0 ); +if (ret <0 ) +return ret; flush =1 ; }while (1 ) {
@@ -3590,7 +3597,10 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,if (!trylock_page (p)) {if (!flush) { ret =flush_write_bio (epd); -BUG_ON (ret <0 ); +if (ret <0 ) { + failed_page_nr = i; + goto err_unlock; + } flush =1 ; }lock_page (p);
@@ -3598,6 +3608,11 @@ lock_extent_buffer_for_io(struct extent_buffer *eb, }return ret; +err_unlock: +/* Unlock these already locked pages */ +for (i =0 ; i < failed_page_nr; i++) +unlock_page (eb->pages[i]); +return ret; }static void end_extent_buffer_writeback (struct extent_buffer *eb)
Signature · 1 line
2.20.1
PATCH v5 10/12 btrfs: extent_io: Kill the BUG_ON() in extent_write_cache_pages()
In reply to Qu Wenruo--- fs/btrfs/extent_io.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +8 -2
@@ -3998,7 +3998,10 @@ static int extent_write_cache_pages(struct address_space *mapping,*/ if (!trylock_page (page)) { ret =flush_write_bio (epd); -BUG_ON (ret <0 ); +if (ret <0 ) { + done =1 ; +break ; + }lock_page (page); }
@@ -4010,7 +4013,10 @@ static int extent_write_cache_pages(struct address_space *mapping,if (wbc->sync_mode !=WB_SYNC_NONE ) {if (PageWriteback (page)) { ret =flush_write_bio (epd); -BUG_ON (ret <0 ); +if (ret <0 ) { + done =1 ; +break ; + } }wait_on_page_writeback (page); }
Signature · 1 line
2.20.1
PATCH v5 11/12 btrfs: extent_io: Handle error better in extent_writepages()
In reply to Qu WenruoDo proper cleanup if we hit any error in extent_writepages(), and check the return value of flush_write_bio().
--- fs/btrfs/extent_io.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +6 -3
@@ -4141,7 +4141,6 @@ int extent_writepages(struct address_space *mapping, struct writeback_control *wbc) {int ret =0 ; -int flush_ret;struct extent_page_data epd = { .bio =NULL , .tree = &BTRFS_I (mapping->host)->io_tree,
@@ -4150,8 +4149,12 @@ int extent_writepages(struct address_space *mapping, }; ret =extent_write_cache_pages (mapping, wbc, &epd); - flush_ret =flush_write_bio (&epd); -BUG_ON (flush_ret <0 ); +ASSERT (ret <=0 ); +if (ret <0 ) { +end_write_bio (&epd, ret); +return ret; + } + ret =flush_write_bio (&epd);return ret; }
Signature · 1 line
2.20.1
PATCH v5 12/12 btrfs: Do mandatory tree block check before submitting bio
In reply to Qu WenruoThere are at least 2 reports about memory bit flip sneaking into on-disk data.
Currently we only have a relaxed check triggered at btrfs_mark_buffer_dirty() time, as it's not mandatory and only for CONFIG_BTRFS_FS_CHECK_INTEGRITY enabled build, it doesn't help user to detect such problem.
This patch will address the hole by triggering comprehensive check on tree blocks before writing it back to disk.
The design points are:
- Timing of the check: Tree block write hook
This timing is chosen to reduce the overhead.
The comprehensive check should be as expensive as csum. Doing full check at btrfs_mark_buffer_dirty() is too expensive for end user.
- Loose empty leaf check
Originally for empty leaf, tree-checker will report error if it's not a tree root.
The problem for such check at write time is:
* False alert for tree root created in current transaction In that case, the commit root still needs to be written to disk. And since current root can differ from commit root, then it will cause false alert.
This happens for log tree.
* False alert for relocated tree block
Relocated tree block can be written to disk due to memory pressure, in that case an empty csum tree root can be written to disk and cause false alert, since csum root node hasn't been updated.
Although some more reliable empty leaf check is still kept as is. Namely essential trees (e.g. extent, chunk) should never be empty.
The example error output will be something like:
BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
BTRFS: error (device dm-3) in btrfs_commit_transaction:2220: errno=-5 IO failure (Error while writing out transaction)
BTRFS info (device dm-3): forced readonly
BTRFS warning (device dm-3): Skipping commit of aborted transaction.
BTRFS: error (device dm-3) in cleanup_transaction:1839: errno=-5 IO failure
BTRFS info (device dm-3): delayed_refs has NO entry
--- fs/btrfs/disk-io.c | 9 +++++++++ fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++--- fs/btrfs/tree-checker.h | 8 ++++++++ 3 files changed, 38 insertions(+), 3 deletions(-)
fs/btrfs/disk-io.c +9 -0
@@ -313,6 +313,15 @@ static int csum_tree_block(struct btrfs_fs_info *fs_info,return -EUCLEAN ; } }else { +if (btrfs_header_level (buf)) + err =btrfs_check_node (fs_info, buf); +else + err =btrfs_check_leaf_write (fs_info, buf); +if (err <0 ) { +btrfs_err (fs_info, +"write time tree block corruption detected" ); +return err; + }write_extent_buffer (buf, result,0 , csum_size); }
fs/btrfs/tree-checker.c +21 -3
@@ -477,7 +477,7 @@ static int check_leaf_item(struct btrfs_fs_info *fs_info, }static int check_leaf (struct btrfs_fs_info *fs_info,struct extent_buffer *leaf, -bool check_item_data) +bool check_item_data,bool check_empty_leaf) {/* No valid key type is 0, so all key should be larger than this key */ struct btrfs_key prev_key = {0 ,0 ,0 };
@@ -516,6 +516,18 @@ static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, owner);return -EUCLEAN ; } + +/* +* Skip empty leaf check, mostly for write time tree block +* +* Such skip mostly happens for tree block write time, as +* we can't use @owner as accurate owner indicator. +* Case like balance and new tree block created for commit root +* can break owner check easily. +*/ +if (!check_empty_leaf) +return 0 ; + key.objectid = owner; key.type =BTRFS_ROOT_ITEM_KEY ; key.offset = (u64)-1 ;
@@ -636,13 +648,19 @@ static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,int btrfs_check_leaf_full (struct btrfs_fs_info *fs_info,struct extent_buffer *leaf) { -return check_leaf (fs_info, leaf, true); +return check_leaf (fs_info, leaf, true, true); }int btrfs_check_leaf_relaxed (struct btrfs_fs_info *fs_info,struct extent_buffer *leaf) { -return check_leaf (fs_info, leaf, false); +return check_leaf (fs_info, leaf, false, true); +} + +int btrfs_check_leaf_write (struct btrfs_fs_info *fs_info, +struct extent_buffer *leaf) +{ +return check_leaf (fs_info, leaf, false, false); }int btrfs_check_node (struct btrfs_fs_info *fs_info,struct extent_buffer *node)
fs/btrfs/tree-checker.h +8 -0
@@ -23,6 +23,14 @@ int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,*/ int btrfs_check_leaf_relaxed (struct btrfs_fs_info *fs_info,struct extent_buffer *leaf); + +/* +* Write time specific leaf checker. +* Don't check if the empty leaf belongs to a tree root. Mostly for balance +* and new tree created in current transaction. +*/ +int btrfs_check_leaf_write (struct btrfs_fs_info *fs_info, +struct extent_buffer *leaf);int btrfs_check_node (struct btrfs_fs_info *fs_info,struct extent_buffer *node);#endif
Signature · 1 line
2.20.1
Quoted 11 lines · Qu Wenruo
Patchset can be fetched from github:
https://github.com/adam900710/linux/tree/write_time_tree_checker
Which is based on v5.0-rc1 tag.
Also there is no conflict rebasing the patchset to misc-next.This patchset has the following 3 features:
- Tree block validation output enhancement
* Output validation failure timing (write time or read time)
* Always output tree block level/key mismatch error message This part is already submitted and reviewed.
Quoted 6 lines
- Write time tree block validation check
To catch memory corruption either from hardware or kernel. Example output would be:BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
This is not good. Those two error messages should be collapsed into one. Otherwise it's hard to actually match them up. Better output will be "Corrupt leaf detected during writing: root=..." and eliminate "write time tree block corruption detected" line. Is that feasible?
Quoted 76 lines
BTRFS: error (device dm-3) in btrfs_commit_transaction:2220: errno=-5 IO failure (Error while writing out transaction) BTRFS info (device dm-3): forced readonly
BTRFS warning (device dm-3): Skipping commit of aborted transaction.
BTRFS: error (device dm-3) in cleanup_transaction:1839: errno=-5 IO failure
BTRFS info (device dm-3):delayed_refshas NO entry- Better error handling before calling
flush_write_bio()One hidden reason of callingflush_write_bio()under all cases is,flush_write_bio()will trigger endio function and endio function ofepd->biowill free the bio under all cases.
So we're in fact abusingflush_write_bio()as cleanup.Since now
flush_write_bio()has its own return value, we shouldn't callflush_write_bio()no-brain, here we introduce proper cleanup helper,end_write_bio(). Now we callflush_write_bio()like:
New | Old
--------------------------------------------------------------
ret =do_some_evil(&epd); | ret =do_some_evil(&epd);
if (ret < 0) { |flush_write_bio(&epd);
end_write_bio(&epd, ret); | ^^^ submitting half-backedepd->bio?
return ret; | return ret;} | ret = flush_write_bio(&epd); | return ret; |Above code should be more streamline for the error handling part.
Changelog:
v2:
- Unlock locked pages inlock_extent_buffer_for_io()for error handling.
- Added Reviewed-by tags.v3:
- Remove duplicated error message.
- UseIS_ENABLED()macro to replace #ifdef.
- Added Reviewed-by tags.v4:
- Re-organized patch split
Now eachBUG_ON()cleanup has its own patch
- Dig much further into the call sites to eliminate unexpected >0 return May be a little paranoid and abuse someASSERT(), but it should be much safer against further code change.
- Fix the false alert caused by balance and memory pressure The fix it skip owner checker for non-essential tree at write time. Since owner root can't always be reliable, either due to commit root created in current transaction or balance + memory pressure.v5:
- Do proper error-out handling other than relying onflush_write_bio()to clean up.
This has a side effect that no Reviewed-by tags for modified patches.
- New comment for why we don't need to do anything aboutebp->biowhensubmit_one_bio()fails.
- Add some Reviewed-by tag.Qu Wenruo (12):
btrfs: Always output error message when key/level verification fails
btrfs:extent_io: Kill the forward declaration offlush_write_bio()
btrfs: disk-io: Show the timing of corrupted tree block explicitly
btrfs:extent_io: Move theBUG_ON()inflush_write_bio()one level up
btrfs:extent_io: Handle error better inextent_write_full_page()
btrfs:extent_io: Handle error better inbtree_write_cache_pages()
btrfs:extent_io: Kill the dead branch inextent_write_cache_pages()
btrfs:extent_io: Handle error better inextent_write_locked_range()
btrfs:extent_io: Kill theBUG_ON()inlock_extent_buffer_for_io()
btrfs:extent_io: Kill theBUG_ON()inextent_write_cache_pages()
btrfs:extent_io: Handle error better inextent_writepages()
btrfs: Do mandatory tree block check before submitting biofs/btrfs/disk-io.c | 21 +++-- fs/btrfs/extent_io.c | 168 ++++++++++++++++++++++++++++------------ fs/btrfs/tree-checker.c | 24 +++++- fs/btrfs/tree-checker.h | 8 ++ 4 files changed, 162 insertions(+), 59 deletions(-)
Quoted 14 lines · Nikolay Borisov
Quoted 11 lines · Qu Wenruo
Patchset can be fetched from github:
https://github.com/adam900710/linux/tree/write_time_tree_checker
Which is based on v5.0-rc1 tag.
Also there is no conflict rebasing the patchset to misc-next.This patchset has the following 3 features:
- Tree block validation output enhancement
* Output validation failure timing (write time or read time)
* Always output tree block level/key mismatch error message This part is already submitted and reviewed.
Quoted 8 lines
Quoted 6 lines
- Write time tree block validation check
To catch memory corruption either from hardware or kernel. Example output would be:BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
This is not good. Those two error messages should be collapsed into one. Otherwise it's hard to actually match them up.
That shouldn't be a problem, since the error won't happen so frequently there is no other error message that could interrupt these 2 lines.
Quoted 3 lines
Better output will
be "Corrupt leaf detected during writing: root=..." and eliminate "write time tree block corruption detected" line. Is that feasible?
Feasible, currently tree checker only get called in 3 locations:
1) read time full checker
2) mark dirty time basic checker
3) write time full checker
And they all have different internal bool to indicate the timing, so it's possible to output the timing.
But that needs to pass the internal bool down a long long way, for all the output help to accept an extra string.
I'm not a big fan for that, and prefer a timing neutral tree checker.
Thanks,
Qu
Quoted 77 lines
Quoted 77 lines
BTRFS: error (device dm-3) in btrfs_commit_transaction:2220: errno=-5 IO failure (Error while writing out transaction) BTRFS info (device dm-3): forced readonly
BTRFS warning (device dm-3): Skipping commit of aborted transaction.
BTRFS: error (device dm-3) in cleanup_transaction:1839: errno=-5 IO failure
BTRFS info (device dm-3):delayed_refshas NO entry- Better error handling before calling
flush_write_bio()One hidden reason of callingflush_write_bio()under all cases is,flush_write_bio()will trigger endio function and endio function ofepd->biowill free the bio under all cases.
So we're in fact abusingflush_write_bio()as cleanup.Since now
flush_write_bio()has its own return value, we shouldn't callflush_write_bio()no-brain, here we introduce proper cleanup helper,end_write_bio(). Now we callflush_write_bio()like:
New | Old
--------------------------------------------------------------
ret =do_some_evil(&epd); | ret =do_some_evil(&epd);
if (ret < 0) { |flush_write_bio(&epd);
end_write_bio(&epd, ret); | ^^^ submitting half-backedepd->bio?
return ret; | return ret;} | ret = flush_write_bio(&epd); | return ret; |Above code should be more streamline for the error handling part.
Changelog:
v2:
- Unlock locked pages inlock_extent_buffer_for_io()for error handling.
- Added Reviewed-by tags.v3:
- Remove duplicated error message.
- UseIS_ENABLED()macro to replace #ifdef.
- Added Reviewed-by tags.v4:
- Re-organized patch split
Now eachBUG_ON()cleanup has its own patch
- Dig much further into the call sites to eliminate unexpected >0 return May be a little paranoid and abuse someASSERT(), but it should be much safer against further code change.
- Fix the false alert caused by balance and memory pressure The fix it skip owner checker for non-essential tree at write time. Since owner root can't always be reliable, either due to commit root created in current transaction or balance + memory pressure.v5:
- Do proper error-out handling other than relying onflush_write_bio()to clean up.
This has a side effect that no Reviewed-by tags for modified patches.
- New comment for why we don't need to do anything aboutebp->biowhensubmit_one_bio()fails.
- Add some Reviewed-by tag.Qu Wenruo (12):
btrfs: Always output error message when key/level verification fails
btrfs:extent_io: Kill the forward declaration offlush_write_bio()
btrfs: disk-io: Show the timing of corrupted tree block explicitly
btrfs:extent_io: Move theBUG_ON()inflush_write_bio()one level up
btrfs:extent_io: Handle error better inextent_write_full_page()
btrfs:extent_io: Handle error better inbtree_write_cache_pages()
btrfs:extent_io: Kill the dead branch inextent_write_cache_pages()
btrfs:extent_io: Handle error better inextent_write_locked_range()
btrfs:extent_io: Kill theBUG_ON()inlock_extent_buffer_for_io()
btrfs:extent_io: Kill theBUG_ON()inextent_write_cache_pages()
btrfs:extent_io: Handle error better inextent_writepages()
btrfs: Do mandatory tree block check before submitting biofs/btrfs/disk-io.c | 21 +++-- fs/btrfs/extent_io.c | 168 ++++++++++++++++++++++++++++------------ fs/btrfs/tree-checker.c | 24 +++++- fs/btrfs/tree-checker.h | 8 ++ 4 files changed, 162 insertions(+), 59 deletions(-)
Quoted 38 lines · Qu Wenruo
Quoted 14 lines · Nikolay Borisov
Quoted 11 lines · Qu Wenruo
Patchset can be fetched from github:
https://github.com/adam900710/linux/tree/write_time_tree_checker
Which is based on v5.0-rc1 tag.
Also there is no conflict rebasing the patchset to misc-next.This patchset has the following 3 features:
- Tree block validation output enhancement
* Output validation failure timing (write time or read time)
* Always output tree block level/key mismatch error message This part is already submitted and reviewed.Quoted 8 lines
Quoted 6 lines
- Write time tree block validation check
To catch memory corruption either from hardware or kernel. Example output would be:BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
This is not good. Those two error messages should be collapsed into one. Otherwise it's hard to actually match them up.
That shouldn't be a problem, since the error won't happen so frequently there is no other error message that could interrupt these 2 lines.
Quoted 3 lines
Better output will
be "Corrupt leaf detected during writing: root=..." and eliminate "write time tree block corruption detected" line. Is that feasible?Feasible, currently tree checker only get called in 3 locations:
1) read time full checker
2) mark dirty time basic checker
3) write time full checker
Quoted 6 lines
And they all have different internal bool to indicate the timing, so it's possible to output the timing.
But that needs to pass the internal bool down a long long way, for all the output help to accept an extra string.
I'm not a big fan for that, and prefer a timing neutral tree checker.
I'd rather not merge the error messages, as we'll possibly add more sanity checks to various functions so there could be a list of problems and there's one final note about when it happened (read time/write time).
Matching the lines together is desirable though, so if the block number could be part of all messages, I hope this makes it usable for analysis.
Reading btree_readpage_end_io_hook, the message should be under the err:
label, as there are 3 other possible messages printed (bad block start, fsid and level).
Quoted 46 lines · David Sterba
Quoted 38 lines · Qu Wenruo
Quoted 14 lines · Nikolay Borisov
Quoted 11 lines · Qu Wenruo
Patchset can be fetched from github:
https://github.com/adam900710/linux/tree/write_time_tree_checker
Which is based on v5.0-rc1 tag.
Also there is no conflict rebasing the patchset to misc-next.This patchset has the following 3 features:
- Tree block validation output enhancement
* Output validation failure timing (write time or read time)
* Always output tree block level/key mismatch error message This part is already submitted and reviewed.Quoted 8 lines
Quoted 6 lines
- Write time tree block validation check
To catch memory corruption either from hardware or kernel. Example output would be:BTRFS critical (device dm-3): corrupt leaf: root=2 block=1350630375424 slot=68, bad key order, prev (10510212874240 169 0) current (1714119868416 169 0) BTRFS error (device dm-3): write time tree block corruption detected
This is not good. Those two error messages should be collapsed into one. Otherwise it's hard to actually match them up.
That shouldn't be a problem, since the error won't happen so frequently there is no other error message that could interrupt these 2 lines.
Quoted 3 lines
Better output will
be "Corrupt leaf detected during writing: root=..." and eliminate "write time tree block corruption detected" line. Is that feasible?Feasible, currently tree checker only get called in 3 locations:
1) read time full checker
2) mark dirty time basic checker
3) write time full checkerQuoted 6 lines
And they all have different internal bool to indicate the timing, so it's possible to output the timing.
But that needs to pass the internal bool down a long long way, for all the output help to accept an extra string.
I'm not a big fan for that, and prefer a timing neutral tree checker.
Quoted 7 lines
I'd rather not merge the error messages, as we'll possibly add more sanity checks to various functions so there could be a list of problems and there's one final note about when it happened (read time/write time).
Matching the lines together is desirable though, so if the block number could be part of all messages, I hope this makes it usable for analysis.
This looks much better.
I'll change the timing line to show extra info to match them.
Thanks,
Qu
Quoted 5 lines
Reading
btree_readpage_end_io_hook, the message should be under the err:
label, as there are 3 other possible messages printed (bad block start, fsid and level).