Skip to content

Commit

Permalink
btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_i…
Browse files Browse the repository at this point in the history
…nfo()

commit 28cb13f upstream.

Instead of doing a BUG_ON() handle the error by returning -EUCLEAN,
aborting the transaction and logging an error message.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
[Minor conflict resolved due to code context change.]
Signed-off-by: Jianqi Ren <jianqi.ren.cn@windriver.com>
Signed-off-by: He Zhe <zhe.he@windriver.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Filipe Manana authored and Greg Kroah-Hartman committed May 22, 2025
1 parent 386507c commit 18eb53a
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item);
num_refs = btrfs_extent_refs(leaf, ei);
if (unlikely(num_refs == 0)) {
ret = -EUCLEAN;
btrfs_err(fs_info,
"unexpected zero reference count for extent item (%llu %u %llu)",
key.objectid, key.type, key.offset);
btrfs_abort_transaction(trans, ret);
goto out_free;
}
extent_flags = btrfs_extent_flags(leaf, ei);
} else {
ret = -EINVAL;
Expand All @@ -190,8 +198,6 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,

goto out_free;
}

BUG_ON(num_refs == 0);
} else {
num_refs = 0;
extent_flags = 0;
Expand Down Expand Up @@ -221,10 +227,19 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
goto search_again;
}
spin_lock(&head->lock);
if (head->extent_op && head->extent_op->update_flags)
if (head->extent_op && head->extent_op->update_flags) {
extent_flags |= head->extent_op->flags_to_set;
else
BUG_ON(num_refs == 0);
} else if (unlikely(num_refs == 0)) {
spin_unlock(&head->lock);
mutex_unlock(&head->mutex);
spin_unlock(&delayed_refs->lock);
ret = -EUCLEAN;
btrfs_err(fs_info,
"unexpected zero reference count for extent %llu (%s)",
bytenr, metadata ? "metadata" : "data");
btrfs_abort_transaction(trans, ret);
goto out_free;
}

num_refs += head->ref_mod;
spin_unlock(&head->lock);
Expand Down

0 comments on commit 18eb53a

Please sign in to comment.