Commit 2e3c2513 authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: extent_io: add proper error handling to lock_extent_buffer_for_io()

This function needs some extra checks on locked pages and eb.  For error
handling we need to unlock locked pages and the eb.

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.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 02c6db4f
...@@ -3492,19 +3492,27 @@ void wait_on_extent_buffer_writeback(struct extent_buffer *eb) ...@@ -3492,19 +3492,27 @@ void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
TASK_UNINTERRUPTIBLE); TASK_UNINTERRUPTIBLE);
} }
/*
* Lock eb pages and flush the bio if we can't the locks
*
* Return 0 if nothing went wrong
* Return >0 is same as 0, except bio is not submitted
* Return <0 if something went wrong, no page is locked
*/
static noinline_for_stack int static noinline_for_stack int
lock_extent_buffer_for_io(struct extent_buffer *eb, lock_extent_buffer_for_io(struct extent_buffer *eb,
struct btrfs_fs_info *fs_info, struct btrfs_fs_info *fs_info,
struct extent_page_data *epd) struct extent_page_data *epd)
{ {
int i, num_pages; int i, num_pages, failed_page_nr;
int flush = 0; int flush = 0;
int ret = 0; int ret = 0;
if (!btrfs_try_tree_write_lock(eb)) { if (!btrfs_try_tree_write_lock(eb)) {
flush = 1;
ret = flush_write_bio(epd); ret = flush_write_bio(epd);
BUG_ON(ret < 0); if (ret < 0)
return ret;
flush = 1;
btrfs_tree_lock(eb); btrfs_tree_lock(eb);
} }
...@@ -3514,7 +3522,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb, ...@@ -3514,7 +3522,8 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,
return 0; return 0;
if (!flush) { if (!flush) {
ret = flush_write_bio(epd); ret = flush_write_bio(epd);
BUG_ON(ret < 0); if (ret < 0)
return ret;
flush = 1; flush = 1;
} }
while (1) { while (1) {
...@@ -3556,7 +3565,10 @@ lock_extent_buffer_for_io(struct extent_buffer *eb, ...@@ -3556,7 +3565,10 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,
if (!trylock_page(p)) { if (!trylock_page(p)) {
if (!flush) { if (!flush) {
ret = flush_write_bio(epd); ret = flush_write_bio(epd);
BUG_ON(ret < 0); if (ret < 0) {
failed_page_nr = i;
goto err_unlock;
}
flush = 1; flush = 1;
} }
lock_page(p); lock_page(p);
...@@ -3564,6 +3576,11 @@ lock_extent_buffer_for_io(struct extent_buffer *eb, ...@@ -3564,6 +3576,11 @@ lock_extent_buffer_for_io(struct extent_buffer *eb,
} }
return ret; return ret;
err_unlock:
/* Unlock 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) static void end_extent_buffer_writeback(struct extent_buffer *eb)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment