From 12b1b03974d8cb6970357675a17603e82eb82719 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 10 Aug 2023 15:01:21 +0200 Subject: [PATCH] btrfs-progs: add support for generic global parameters ./btrfs --param key=value command ... ./btrfs --param key command ... To pass various tuning data for testing and debugging, undocumented for regular users. To add support add reading of the parameter value after option parsing bconf_param_value("key") and convert to what you need. Signed-off-by: David Sterba --- btrfs.c | 4 ++++ common/help.h | 1 + common/utils.c | 42 ++++++++++++++++++++++++++++++++++++++++++ common/utils.h | 11 +++++++++++ 4 files changed, 58 insertions(+) diff --git a/btrfs.c b/btrfs.c index 751f193e..591dd45e 100644 --- a/btrfs.c +++ b/btrfs.c @@ -287,6 +287,7 @@ static int handle_global_options(int argc, char **argv) { "verbose", no_argument, NULL, 'v' }, { "quiet", no_argument, NULL, 'q' }, { "log", required_argument, NULL, OPT_LOG }, + { "param", required_argument, NULL, GETOPT_VAL_PARAM }, { NULL, 0, NULL, 0} }; int shift; @@ -312,6 +313,9 @@ static int handle_global_options(int argc, char **argv) case OPT_LOG: handle_log_level(optarg); break; + case GETOPT_VAL_PARAM: + bconf_save_param(optarg); + break; case 'v': bconf_be_verbose(); break; diff --git a/common/help.h b/common/help.h index cb84cc37..f6bf9782 100644 --- a/common/help.h +++ b/common/help.h @@ -36,6 +36,7 @@ struct cmd_group; #define GETOPT_VAL_TBYTES 519 #define GETOPT_VAL_HELP 520 +#define GETOPT_VAL_PARAM 521 #define ARGV0_BUF_SIZE PATH_MAX diff --git a/common/utils.c b/common/utils.c index ee3d5abc..b0a61126 100644 --- a/common/utils.c +++ b/common/utils.c @@ -944,6 +944,7 @@ void btrfs_config_init(void) { bconf.output_format = CMD_FORMAT_TEXT; bconf.verbose = BTRFS_BCONF_UNSET; + INIT_LIST_HEAD(&bconf.params); } void bconf_be_verbose(void) @@ -959,6 +960,47 @@ void bconf_be_quiet(void) bconf.verbose = BTRFS_BCONF_QUIET; } +void bconf_add_param(const char *key, const char *value) +{ + struct config_param *param; + + param = calloc(sizeof(*param), 1); + if (!param) + return; + param->key = strdup(key); + if (value) + param->value = strdup(value); + list_add(¶m->list, &bconf.params); +} + +const char *bconf_param_value(const char *key) +{ + struct config_param *param; + + list_for_each_entry(param, &bconf.params, list) { + if (strcmp(key, param->key) == 0) + return param->value; + } + return NULL; +} + +void bconf_save_param(const char *str) +{ + char *tmp; + + tmp = strchr(str, '='); + if (!tmp) { + bconf_add_param(str, NULL); + printf("Global param: %s\n", str); + } else { + *tmp = 0; + bconf_add_param(str, tmp + 1); + printf("Global param: %s=%s\n", str, tmp + 1); + *tmp = '='; + } +} + + /* Returns total size of main memory in bytes, -1UL if error. */ unsigned long total_memory(void) { diff --git a/common/utils.h b/common/utils.h index 0e883e2f..30899ab2 100644 --- a/common/utils.h +++ b/common/utils.h @@ -22,6 +22,7 @@ #include "kerncompat.h" #include #include +#include "kernel-lib/list.h" #include "kernel-shared/volumes.h" #include "common/fsfeatures.h" @@ -86,12 +87,22 @@ struct btrfs_config { * > 0: verbose level */ int verbose; + struct list_head params; }; extern struct btrfs_config bconf; +struct config_param { + struct list_head list; + const char *key; + const char *value; +}; + void btrfs_config_init(void); void bconf_be_verbose(void); void bconf_be_quiet(void); +void bconf_add_param(const char *key, const char *value); +void bconf_save_param(const char *str); +const char *bconf_param_value(const char *key); /* Pseudo random number generator wrappers */ int rand_int(void);