diff mbox series

[06/20] btrfs: handle invalid root reference found in btrfs_init_root_free_objectid()

Message ID aeb9fec3b34c662de4a9771f44e7d0b839de13f6.1706130791.git.dsterba@suse.com (mailing list archive)
State New, archived
Headers show
Series Error handling fixes | expand

Commit Message

David Sterba Jan. 24, 2024, 9:18 p.m. UTC
The btrfs_init_root_free_objectid() looks up a root by a key, allowing
to do an inexact search when key->offset is -1.  It's never expected to
find such item, as it would break the allowed range of a root id.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/disk-io.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Qu Wenruo Jan. 25, 2024, 4:06 a.m. UTC | #1
On 2024/1/25 07:48, David Sterba wrote:
> The btrfs_init_root_free_objectid() looks up a root by a key, allowing
> to do an inexact search when key->offset is -1.  It's never expected to
> find such item, as it would break the allowed range of a root id.
>
> Signed-off-by: David Sterba <dsterba@suse.com>
> ---
>   fs/btrfs/disk-io.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 274a3e1faeab..3f7291a48a4d 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -4928,7 +4928,14 @@ int btrfs_init_root_free_objectid(struct btrfs_root *root)
>   	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
>   	if (ret < 0)
>   		goto error;
> -	BUG_ON(ret == 0); /* Corruption */
> +	if (ret == 0) {
> +		/*
> +		 * Key with offset -1 found, there would have to exist a root
> +		 * with such id, but this is out of valid range.
> +		 */
> +		ret = -EUCLEAN;

Better with an error message, since we don't have a trans to abort and
one of the two callers are not aborting the transaction, making it
harder to debug.

Thanks,
Qu

> +		goto error;
> +	}
>   	if (path->slots[0] > 0) {
>   		slot = path->slots[0] - 1;
>   		l = path->nodes[0];
diff mbox series

Patch

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 274a3e1faeab..3f7291a48a4d 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -4928,7 +4928,14 @@  int btrfs_init_root_free_objectid(struct btrfs_root *root)
 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
 	if (ret < 0)
 		goto error;
-	BUG_ON(ret == 0); /* Corruption */
+	if (ret == 0) {
+		/*
+		 * Key with offset -1 found, there would have to exist a root
+		 * with such id, but this is out of valid range.
+		 */
+		ret = -EUCLEAN;
+		goto error;
+	}
 	if (path->slots[0] > 0) {
 		slot = path->slots[0] - 1;
 		l = path->nodes[0];