Skip to content

Commit 0af4fed

Browse files
konissmb49
authored andcommitted
nilfs2: handle inconsistent state in nilfs_btnode_create_block()
BugLink: https://bugs.launchpad.net/bugs/2078428 commit 4811f7af6090e8f5a398fbdd766f903ef6c0d787 upstream. Syzbot reported that a buffer state inconsistency was detected in nilfs_btnode_create_block(), triggering a kernel bug. It is not appropriate to treat this inconsistency as a bug; it can occur if the argument block address (the buffer index of the newly created block) is a virtual block number and has been reallocated due to corruption of the bitmap used to manage its allocation state. So, modify nilfs_btnode_create_block() and its callers to treat it as a possible filesystem error, rather than triggering a kernel bug. Link: https://lkml.kernel.org/r/[email protected] Fixes: a60be98 ("nilfs2: B-tree node cache") Signed-off-by: Ryusuke Konishi <[email protected]> Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=89cc4f2324ed37988b60 Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Koichiro Den <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 5d7c36f commit 0af4fed

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

fs/nilfs2/btnode.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@ nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
5151

5252
bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
5353
if (unlikely(!bh))
54-
return NULL;
54+
return ERR_PTR(-ENOMEM);
5555

5656
if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
5757
buffer_dirty(bh))) {
58-
brelse(bh);
59-
BUG();
58+
/*
59+
* The block buffer at the specified new address was already
60+
* in use. This can happen if it is a virtual block number
61+
* and has been reallocated due to corruption of the bitmap
62+
* used to manage its allocation state (if not, the buffer
63+
* clearing of an abandoned b-tree node is missing somewhere).
64+
*/
65+
nilfs_error(inode->i_sb,
66+
"state inconsistency probably due to duplicate use of b-tree node block address %llu (ino=%lu)",
67+
(unsigned long long)blocknr, inode->i_ino);
68+
goto failed;
6069
}
6170
memset(bh->b_data, 0, i_blocksize(inode));
6271
bh->b_bdev = inode->i_sb->s_bdev;
@@ -67,6 +76,12 @@ nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
6776
unlock_page(bh->b_page);
6877
put_page(bh->b_page);
6978
return bh;
79+
80+
failed:
81+
unlock_page(bh->b_page);
82+
put_page(bh->b_page);
83+
brelse(bh);
84+
return ERR_PTR(-EIO);
7085
}
7186

7287
int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
@@ -217,8 +232,8 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc,
217232
}
218233

219234
nbh = nilfs_btnode_create_block(btnc, newkey);
220-
if (!nbh)
221-
return -ENOMEM;
235+
if (IS_ERR(nbh))
236+
return PTR_ERR(nbh);
222237

223238
BUG_ON(nbh == obh);
224239
ctxt->newbh = nbh;

fs/nilfs2/btree.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
6363
struct buffer_head *bh;
6464

6565
bh = nilfs_btnode_create_block(btnc, ptr);
66-
if (!bh)
67-
return -ENOMEM;
66+
if (IS_ERR(bh))
67+
return PTR_ERR(bh);
6868

6969
set_buffer_nilfs_volatile(bh);
7070
*bhp = bh;

0 commit comments

Comments
 (0)