[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250424192956.GO3659@twin.jikos.cz>
Date: Thu, 24 Apr 2025 21:29:56 +0200
From: David Sterba <dsterba@...e.cz>
To: Daniel Vacek <neelx@...e.com>
Cc: Chris Mason <clm@...com>, Josef Bacik <josef@...icpanda.com>,
David Sterba <dsterba@...e.com>, Nick Terrell <terrelln@...com>,
linux-btrfs@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2] btrfs: harden parsing of compress mount option
On Wed, Apr 23, 2025 at 03:22:19PM +0200, Daniel Vacek wrote:
> Btrfs happily but incorrectly accepts the `-o compress=zlib+foo` and similar
> options with any random suffix. Let's handle that correctly.
Please split the patch. Moving code and adding a fix obscures the fix.
As we'll want to backport more than just the validation of ':' it
makes more sense to do the full move first and then add the individual
fixes on top of that. Thanks.
> Signed-off-by: Daniel Vacek <neelx@...e.com>
> ---
> v2: Drop useless check for comma and split compress options
> into a separate helper function
>
> fs/btrfs/super.c | 108 +++++++++++++++++++++++++++--------------------
> 1 file changed, 62 insertions(+), 46 deletions(-)
>
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 40709e2a44fce..422fb82279877 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -270,6 +270,67 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
> return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
> }
>
> +static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
> + struct fs_parameter *param, int opt)
> +{
> + /*
> + * Provide the same semantics as older kernels that don't use fs
> + * context, specifying the "compress" option clears
> + * "force-compress" without the need to pass
> + * "compress-force=[no|none]" before specifying "compress".
> + */
> + if (opt != Opt_compress_force && opt != Opt_compress_force_type)
> + btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
> +
> + if (opt == Opt_compress || opt == Opt_compress_force) {
> + ctx->compress_type = BTRFS_COMPRESS_ZLIB;
> + ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
> + btrfs_set_opt(ctx->mount_opt, COMPRESS);
> + btrfs_clear_opt(ctx->mount_opt, NODATACOW);
> + btrfs_clear_opt(ctx->mount_opt, NODATASUM);
Additional cleanups can reorganize the checks so the option clearing
is done once (and not repeated for each compression algorithm).
> + } else if (strncmp(param->string, "zlib", 4) == 0 &&
> + (param->string[4] == ':' ||
> + param->string[4] == '\0')) {
Matching the name also looks like it can be done by a helper like
match_compresssion(param, "zlib")
and implemented like
int len = strlen(compression);
if (strncmp(param->string, compression, len) == 0 &&
(param->string[len] ... etc
> + ctx->compress_type = BTRFS_COMPRESS_ZLIB;
> + ctx->compress_level =
> + btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
> + param->string + 4);
> + btrfs_set_opt(ctx->mount_opt, COMPRESS);
> + btrfs_clear_opt(ctx->mount_opt, NODATACOW);
> + btrfs_clear_opt(ctx->mount_opt, NODATASUM);
> + } else if (strncmp(param->string, "lzo", 3) == 0 &&
> + param->string[3] == '\0') {
> + ctx->compress_type = BTRFS_COMPRESS_LZO;
> + ctx->compress_level = 0;
> + btrfs_set_opt(ctx->mount_opt, COMPRESS);
> + btrfs_clear_opt(ctx->mount_opt, NODATACOW);
> + btrfs_clear_opt(ctx->mount_opt, NODATASUM);
> + } else if (strncmp(param->string, "zstd", 4) == 0 &&
> + (param->string[4] == ':' ||
> + param->string[4] == '\0')) {
> + ctx->compress_type = BTRFS_COMPRESS_ZSTD;
> + ctx->compress_level =
> + btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
> + param->string + 4);
> + btrfs_set_opt(ctx->mount_opt, COMPRESS);
> + btrfs_clear_opt(ctx->mount_opt, NODATACOW);
> + btrfs_clear_opt(ctx->mount_opt, NODATASUM);
> + } else if ((strncmp(param->string, "no", 2) == 0 &&
> + param->string[2] == '\0') ||
> + (strncmp(param->string, "none", 4) == 0 &&
> + param->string[4] == '\0')) {
> + ctx->compress_level = 0;
> + ctx->compress_type = 0;
> + btrfs_clear_opt(ctx->mount_opt, COMPRESS);
> + btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
> + } else {
> + btrfs_err(NULL, "unrecognized compression value %s",
> + param->string);
> + return -EINVAL;
> + }
> + return 0;
> +}
Powered by blists - more mailing lists