From cc902bb4bf7d990dfd3f9b815fcfc6311fbe0088 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Mon, 6 Nov 2023 19:01:15 -0500 Subject: [PATCH] cmd_format: Check for device options after device arguments It's a common user error to specify device specific options at the end of a format command, and then not have them apply to any devices - add a check for this. Signed-off-by: Kent Overstreet --- cmd_format.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmd_format.c b/cmd_format.c index a44205e2..f0a4b6a5 100644 --- a/cmd_format.c +++ b/cmd_format.c @@ -119,6 +119,7 @@ int cmd_format(int argc, char *argv[]) struct format_opts opts = format_opts_default(); struct dev_opts dev_opts = dev_opts_default(), *dev; bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false; + bool unconsumed_dev_option = false; unsigned v; int opt; @@ -162,6 +163,7 @@ int cmd_format(int argc, char *argv[]) case O_fs_size: if (bch2_strtoull_h(optarg, &dev_opts.size)) die("invalid filesystem size"); + unconsumed_dev_option = true; break; case O_superblock_size: if (bch2_strtouint_h(optarg, &opts.superblock_size)) @@ -172,23 +174,28 @@ int cmd_format(int argc, char *argv[]) case O_bucket_size: if (bch2_strtoull_h(optarg, &dev_opts.bucket_size)) die("bad bucket_size %s", optarg); + unconsumed_dev_option = true; break; case O_label: case 'l': dev_opts.label = optarg; + unconsumed_dev_option = true; break; case O_discard: dev_opts.discard = true; + unconsumed_dev_option = true; break; case O_data_allowed: dev_opts.data_allowed = read_flag_list_or_die(optarg, bch2_data_types, "data type"); + unconsumed_dev_option = true; break; case O_durability: if (kstrtouint(optarg, 10, &dev_opts.durability) || dev_opts.durability > BCH_REPLICAS_MAX) die("invalid durability"); + unconsumed_dev_option = true; break; case O_version: if (kstrtouint(optarg, 10, &opts.version)) @@ -202,6 +209,7 @@ int cmd_format(int argc, char *argv[]) dev_opts.path = optarg; darray_push(&devices, dev_opts); dev_opts.size = 0; + unconsumed_dev_option = false; break; case O_quiet: case 'q': @@ -219,6 +227,9 @@ int cmd_format(int argc, char *argv[]) break; } + if (unconsumed_dev_option) + die("Options for devices apply to subsequent devices; got a device option with no device"); + if (opts.version != bcachefs_metadata_version_current) initialize = false;