[PATCH 1/4] btrfs: replace BUG() with error handling in compression.c

From: Adarsh Das

Date: Fri Feb 27 2026 - 13:33:00 EST


Replace BUG() calls with proper error handling. Where fs_info is
available, use btrfs_err() and return -EUCLEAN. Where fs_info is
not available, use WARN_ONCE() with the invalid type value so the
stack trace carries enough context for debugging.

Signed-off-by: Adarsh Das <adarshdas950@xxxxxxxxx>
---
fs/btrfs/compression.c | 42 ++++++++++++------------------------------
1 file changed, 12 insertions(+), 30 deletions(-)

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 790518a8c803..29281aba925e 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -95,11 +95,9 @@ static int compression_decompress_bio(struct list_head *ws,
case BTRFS_COMPRESS_ZSTD: return zstd_decompress_bio(ws, cb);
case BTRFS_COMPRESS_NONE:
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ btrfs_err(cb_to_fs_info(cb), "invalid compression type %d",
+ cb->compress_type);
+ return -EUCLEAN;
}
}

@@ -116,11 +114,7 @@ static int compression_decompress(int type, struct list_head *ws,
dest_pgoff, srclen, destlen);
case BTRFS_COMPRESS_NONE:
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ return -EUCLEAN;
}
}

@@ -703,11 +697,8 @@ static struct list_head *alloc_workspace(struct btrfs_fs_info *fs_info, int type
case BTRFS_COMPRESS_LZO: return lzo_alloc_workspace(fs_info);
case BTRFS_COMPRESS_ZSTD: return zstd_alloc_workspace(fs_info, level);
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ btrfs_err(fs_info, "invalid compression type %d", type);
+ return ERR_PTR(-EUCLEAN);
}
}

@@ -719,11 +710,8 @@ static void free_workspace(int type, struct list_head *ws)
case BTRFS_COMPRESS_LZO: return lzo_free_workspace(ws);
case BTRFS_COMPRESS_ZSTD: return zstd_free_workspace(ws);
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ WARN_ONCE(1, "invalid compression type %d", type);
+ return;
}
}

@@ -874,11 +862,8 @@ static struct list_head *get_workspace(struct btrfs_fs_info *fs_info, int type,
case BTRFS_COMPRESS_LZO: return btrfs_get_workspace(fs_info, type, level);
case BTRFS_COMPRESS_ZSTD: return zstd_get_workspace(fs_info, level);
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ btrfs_err(fs_info, "invalid compression type %d", type);
+ return ERR_PTR(-EUCLEAN);
}
}

@@ -925,11 +910,8 @@ static void put_workspace(struct btrfs_fs_info *fs_info, int type, struct list_h
case BTRFS_COMPRESS_LZO: return btrfs_put_workspace(fs_info, type, ws);
case BTRFS_COMPRESS_ZSTD: return zstd_put_workspace(fs_info, ws);
default:
- /*
- * This can't happen, the type is validated several times
- * before we get here.
- */
- BUG();
+ btrfs_err(fs_info, "invalid compression type %d", type);
+ return;
}
}

--
2.53.0