Skip to content

Commit 18eb53a

Browse files
fdmananagregkh
authored andcommitted
btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info()
commit 28cb13f29faf6290597b24b728dc3100c019356f 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 <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]> [Minor conflict resolved due to code context change.] Signed-off-by: Jianqi Ren <[email protected]> Signed-off-by: He Zhe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 386507c commit 18eb53a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

fs/btrfs/extent-tree.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
179179
ei = btrfs_item_ptr(leaf, path->slots[0],
180180
struct btrfs_extent_item);
181181
num_refs = btrfs_extent_refs(leaf, ei);
182+
if (unlikely(num_refs == 0)) {
183+
ret = -EUCLEAN;
184+
btrfs_err(fs_info,
185+
"unexpected zero reference count for extent item (%llu %u %llu)",
186+
key.objectid, key.type, key.offset);
187+
btrfs_abort_transaction(trans, ret);
188+
goto out_free;
189+
}
182190
extent_flags = btrfs_extent_flags(leaf, ei);
183191
} else {
184192
ret = -EINVAL;
@@ -190,8 +198,6 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
190198

191199
goto out_free;
192200
}
193-
194-
BUG_ON(num_refs == 0);
195201
} else {
196202
num_refs = 0;
197203
extent_flags = 0;
@@ -221,10 +227,19 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
221227
goto search_again;
222228
}
223229
spin_lock(&head->lock);
224-
if (head->extent_op && head->extent_op->update_flags)
230+
if (head->extent_op && head->extent_op->update_flags) {
225231
extent_flags |= head->extent_op->flags_to_set;
226-
else
227-
BUG_ON(num_refs == 0);
232+
} else if (unlikely(num_refs == 0)) {
233+
spin_unlock(&head->lock);
234+
mutex_unlock(&head->mutex);
235+
spin_unlock(&delayed_refs->lock);
236+
ret = -EUCLEAN;
237+
btrfs_err(fs_info,
238+
"unexpected zero reference count for extent %llu (%s)",
239+
bytenr, metadata ? "metadata" : "data");
240+
btrfs_abort_transaction(trans, ret);
241+
goto out_free;
242+
}
228243

229244
num_refs += head->ref_mod;
230245
spin_unlock(&head->lock);

0 commit comments

Comments
 (0)