Btrfs: fix BUG_ON in btrfs_mark_buffer_dirty
authorLiu Bo <bo.li.liu@oracle.com>
Fri, 2 Sep 2016 19:35:34 +0000 (12:35 -0700)
committerFilipe Manana <fdmanana@suse.com>
Wed, 23 Nov 2016 20:23:20 +0000 (20:23 +0000)
This can only happen with CONFIG_BTRFS_FS_CHECK_INTEGRITY=y.

Commit 1ba98d0 ("Btrfs: detect corruption when non-root leaf has zero item")
assumes that a leaf is its root when leaf->bytenr == btrfs_root_bytenr(root),
however, we should not use btrfs_root_bytenr(root) since it's mainly got
updated during committing transaction.  So the check can fail when doing
COW on this leaf while it is a root.

This changes to use "if (leaf == btrfs_root_node(root))" instead, just like
how we check whether leaf is a root in __btrfs_cow_block().

Fixes: 1ba98d086fe3 (Btrfs: detect corruption when non-root leaf has zero item)
Cc: stable@vger.kernel.org # 4.8+
Reported-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
fs/btrfs/disk-io.c

index 3a57f99d96aa7aa0af541e328af203ff73acdb64..c4e673a94426abc786822ca1c874fb2634bc66c7 100644 (file)
@@ -572,13 +572,17 @@ static noinline int check_leaf(struct btrfs_root *root,
                 * open_ctree() some roots has not yet been set up.
                 */
                if (!IS_ERR_OR_NULL(check_root)) {
+                       struct extent_buffer *eb;
+
+                       eb = btrfs_root_node(check_root);
                        /* if leaf is the root, then it's fine */
-                       if (leaf->start !=
-                           btrfs_root_bytenr(&check_root->root_item)) {
+                       if (leaf != eb) {
                                CORRUPT("non-root leaf's nritems is 0",
-                                       leaf, root, 0);
+                                       leaf, check_root, 0);
+                               free_extent_buffer(eb);
                                return -EIO;
                        }
+                       free_extent_buffer(eb);
                }
                return 0;
        }