add patch show-more-binary-mount-options
[ext4-patch-queue.git] / fix-buffer-double-free-in-ext4_alloc_branch
blob98315b4032e8190d3aef568739b6d915c96931e9
1 ext4: Fix buffer double free in ext4_alloc_branch()
3 From: Jan Kara <jack@suse.cz>
5 Error recovery in ext4_alloc_branch() calls ext4_forget() even for
6 buffer corresponding to indirect block it did not allocate. This leads
7 to brelse() being called twice for that buffer (once from ext4_forget()
8 and once from cleanup in ext4_ind_map_blocks()) leading to buffer use
9 count misaccounting. Eventually (but often much later because there
10 are other users of the buffer) we will see messages like:
11 VFS: brelse: Trying to free free buffer
13 Another manifestation of this problem is an error:
14 JBD2 unexpected failure: jbd2_journal_revoke: !buffer_revoked(bh);
15 inconsistent data on disk
17 The fix is easy - don't forget buffer we did not allocate. Also add an
18 explanatory comment because the indexing at ext4_alloc_branch() is
19 somewhat subtle.
21 Signed-off-by: Jan Kara <jack@suse.cz>
22 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
23 Cc: stable@vger.kernel.org
24 ---
25  fs/ext4/indirect.c | 8 +++++++-
26  1 file changed, 7 insertions(+), 1 deletion(-)
28 diff --git a/fs/ext4/indirect.c b/fs/ext4/indirect.c
29 index 594009f5f523..3b91d240da4d 100644
30 --- a/fs/ext4/indirect.c
31 +++ b/fs/ext4/indirect.c
32 @@ -389,7 +389,13 @@ static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
33         return 0;
34  failed:
35         for (; i >= 0; i--) {
36 -               if (i != indirect_blks && branch[i].bh)
37 +               /*
38 +                * We want to ext4_forget() only freshly allocated indirect
39 +                * blocks.  Buffer for new_blocks[i-1] is at branch[i].bh and
40 +                * buffer at branch[0].bh is indirect block / inode already
41 +                * existing before ext4_alloc_branch() was called.
42 +                */
43 +               if (i > 0 && i != indirect_blks && branch[i].bh)
44                         ext4_forget(handle, 1, inode, branch[i].bh,
45                                     branch[i].bh->b_blocknr);
46                 ext4_free_blocks(handle, inode, NULL, new_blocks[i],
47 -- 
48 1.8.1.4