btrfs: Correctly free extent buffer in case btree_read_extent_buffer_pages fails
PATCH v2 1/1 btrfs: Correctly free extent buffer in case btree_read_extent_buffer_pages fails
If a an eb fails to be read for whatever reason - it's corrupted on disk and parent transid/key validations fail or IO for eb pages fail then this buffer must be removed from the buffer cache. Currently the code calls free_extent_buffer if an error occurs. Unfortunately this doesn't achieve the desired behavior since btrfs_find_create_tree_block returns with eb->refs == 2. On the other hand free_extent_buffer will only decrement the refs once leavin it added to the buffer cache radix tree. This enables later code to look up the buffer from the cache and utilize it potentially leading to a crash.
The correct way to free the buffer is call free_extent_buffer_stale. This function will correctly call atomic_dec explicitly for the buffer and subsequently call release_extent_buffer which will decrement the final reference thus correctly remove the invalid buffer from buffer cache. This change affects only newly allocated buffers since they have eb->refs == 2.
---
Changes since v1:
* Immediately remove a corrupted extent buffer from readahead_tree_block as well.
fs/btrfs/disk-io.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +9 -6
@@ -1021,9 +1021,12 @@ void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) buf =btrfs_find_create_tree_block (fs_info, bytenr);if (IS_ERR (buf))return ; -read_extent_buffer_pages (&BTRFS_I (btree_inode)->io_tree, - buf,WAIT_NONE ,0 ); -free_extent_buffer (buf); + +if (read_extent_buffer_pages (&BTRFS_I (btree_inode)->io_tree, + buf,WAIT_NONE ,0 )) +free_extent_buffer_stale (buf); +else +free_extent_buffer (buf); } int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
@@ -1043,12 +1046,12 @@ int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, ret =read_extent_buffer_pages (io_tree, buf,WAIT_PAGE_LOCK , mirror_num);if (ret) { -free_extent_buffer (buf); +free_extent_buffer_stale (buf);return ret; }if (test_bit (EXTENT_BUFFER_CORRUPT , &buf->bflags)) { -free_extent_buffer (buf); +free_extent_buffer_stale (buf);return -EIO ; }else if (extent_buffer_uptodate (buf)) { *eb = buf;
@@ -1102,7 +1105,7 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, ret =btree_read_extent_buffer_pages (fs_info, buf, parent_transid, level, first_key);if (ret) { -free_extent_buffer (buf); +free_extent_buffer_stale (buf);return ERR_PTR (ret); }return buf;
Signature · 1 line
2.17.1
Quoted 33 lines · Nikolay Borisov
If a an eb fails to be read for whatever reason - it's corrupted on disk and parent transid/key validations fail or IO for eb pages fail then this buffer must be removed from the buffer cache. Currently the code calls
free_extent_bufferif an error occurs. Unfortunately this doesn't achieve the desired behavior sincebtrfs_find_create_tree_blockreturns witheb->refs== 2. On the other handfree_extent_bufferwill only decrement the refs once leavin it added to the buffer cache radix tree. This enables later code to look up the buffer from the cache and utilize it potentially leading to a crash.The correct way to free the buffer is call
free_extent_buffer_stale. This function will correctly callatomic_decexplicitly for the buffer and subsequently callrelease_extent_bufferwhich will decrement the final reference thus correctly remove the invalid buffer from buffer cache. This change affects only newly allocated buffers since they haveeb->refs== 2.Signed-off-by Nikolay Borisov <[email protected]>---
Changes since v1:
* Immediately remove a corrupted extent buffer fromreadahead_tree_blockas well.fs/btrfs/disk-io.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)fs/btrfs/disk-io.c +0
-0
fs/btrfs/disk-io.c void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) · line 1026
Quoted 9 lines
fs/btrfs/disk-io.c +3
-3@@ -1021,9 +1021,12 @@ void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) buf =btrfs_find_create_tree_block (fs_info, bytenr);if (IS_ERR (buf))return ; -read_extent_buffer_pages (&BTRFS_I (btree_inode)->io_tree, - buf,WAIT_NONE ,0 ); -free_extent_buffer (buf); + +if (read_extent_buffer_pages (&BTRFS_I (btree_inode)->io_tree, + buf,WAIT_NONE ,0 ))
I've switched that to
ret = read_extent_buffer_pages() if (ret < 0)
as this is the common style. Function calls inside the conditions should be only predicates, the rest should store and check the return value separately.
Otherwise ok, though the dedicated helper for this would still be nice to have, but this cann follow later and this patch can go to stable.