replace QEMUOptionParameter with QemuOpts parser
v13 cover replace QEMUOptionParameter with QemuOpts parser
These patches will replace QEMUOptionParameter with QemuOpts. Change logs please go to each patch's commit message.
Dong Xu Wang (6):
add def_value_str in QemuOptDesc struct and rewrite qemu_opts_print avoid duplication of default value in QemuOpts
Create four QemuOptsList related functions
create some QemuOpts functons
Use QemuOpts support in block layer
remove QEMUOptionParameter related functions and struct
block.c | 98 ++++---- block/cow.c | 46 ++-- block/gluster.c | 37 ++-- block/iscsi.c | 35 ++- block/qcow.c | 61 +++-- block/qcow2.c | 173 ++++++++------- block/qed.c | 86 ++++--- block/qed.h | 2 +- block/raw-posix.c | 59 +++-- block/raw-win32.c | 31 +-- block/raw.c | 30 +-- block/rbd.c | 62 +++--- block/sheepdog.c | 79 ++++--- block/vdi.c | 70 +++--- block/vmdk.c | 90 ++++---- block/vpc.c | 57 ++--- block/vvfat.c | 11 +- include/block/block.h | 5 +- include/block/block_int.h | 6 +- include/qemu/option.h | 49 ++-- qemu-img.c | 61 +++-- util/qemu-option.c | 555 ++++++++++++++++++++-------------------------- 22 files changed, 791 insertions(+), 912 deletions(-)
Signature · 1 line
1.7.11.7
PATCH v13 1/6 add def_value_str in QemuOptDesc struct and rewrite qemu_opts_print
qemu_opts_print has no user now, so can re-write the function safely.
qemu_opts_print will be used while using "qemu-img create", it will produce the same output as previous code.
The behavior of this function has changed:
1. Print every possible option, whether a value has been set or not.
2. Option descriptors may provide a default value.
3. Print to stdout instead of stderr.
Previously the behavior was to print every option that has been set. Options that have not been set would be skipped.
---v12->v13
1) re-write commit message.
v11->v12
1) make def_value_str become the real default value string in opt_set function.
v10->v11:
1) print all values that have actually been assigned while accept-any cases.
v7->v8:
1) print "elements => accept any params" while opts_accepts_any() == true.
2) since def_print_str is the default value if an option isn't set, so rename it to def_value_str.
include/qemu/option.h | 3 ++- util/qemu-option.c | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-)
include/qemu/option.h +2 -1
@@ -96,6 +96,7 @@ typedef struct QemuOptDesc {const char *name;enum QemuOptType type;const char *help; +const char *def_value_str; } QemuOptDesc;struct QemuOptsList {
@@ -152,7 +153,7 @@ QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);void qemu_opts_absorb_qdict (QemuOpts *opts,QDict *qdict,Error **errp);typedef int (*qemu_opts_loopfunc )(QemuOpts *opts,void *opaque); -int qemu_opts_print (QemuOpts *opts,void *dummy); +int qemu_opts_print (QemuOpts *opts);int qemu_opts_foreach (QemuOptsList *list,qemu_opts_loopfunc func,void *opaque,int abort_on_failure);
util/qemu-option.c +27 -6
@@ -646,6 +646,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value, } opt->desc = desc; opt->str =g_strdup (value); + opt->str =g_strdup (value ?: desc->def_value_str);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {error_propagate (errp, local_err);
@@ -860,16 +861,36 @@ void qemu_opts_del(QemuOpts *opts)g_free (opts); } -int qemu_opts_print (QemuOpts *opts,void *dummy) +int qemu_opts_print (QemuOpts *opts) {QemuOpt *opt; +QemuOptDesc *desc = opts->list->desc; -fprintf (stderr,"%s: %s:" , opts->list->name, - opts->id ? opts->id :"<noid>" ); - QTAILQ_FOREACH(opt, &opts->head, next) { -fprintf (stderr," %s=\"%s\"" , opt->name, opt->str); +if (desc[0 ].name ==NULL ) { + QTAILQ_FOREACH(opt, &opts->head, next) { +printf ("%s=\"%s\" " , opt->name, opt->str); + } +return 0 ; + } +for (; desc && desc->name; desc++) { +const char *value = desc->def_value_str; +QemuOpt *opt; + + opt =qemu_opt_find (opts, desc->name); +if (opt) { + value = opt->str; + } + +if (!value) { +continue ; + } + +if (desc->type ==QEMU_OPT_STRING ) { +printf ("%s='%s' " , desc->name, value); + }else { +printf ("%s=%s " , desc->name, value); + } } -fprintf (stderr,"\n" );return 0 ; }
Signature · 1 line
1.7.11.7
PATCH v13 2/6 avoid duplication of default value in QemuOpts
In reply to Dong Xu WangAccording Markus's comments, his patch will move the default value entirely to QemuOptDesc.
When getting the value of an option that hasn't been set, and QemuOptDesc has a default value, return that. Else, behave as before.
Example: qemu_opt_get_number(opts, "foo", 42)
If "foo" has been set in opts, return its value.
Else, if opt's QemuOptDesc has a default value for "foo", return that.
Else, return 42.
Note that the last argument is useless when QemuOptDesc has a default value. Ugly. If it bothers us, assert that the argument equals the default from QemuOptDesc.
Example: qemu_opt_get(opts, "bar")
If "bar" has been set in opts, return its value.
Else, if opt's QemuOptDesc has a default value for "bar", return that.
Else, return NULL.
--- util/qemu-option.c | 58 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-)
util/qemu-option.c +40 -18
@@ -525,10 +525,28 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)return NULL ; } +static const QemuOptDesc *find_desc_by_name (const QemuOptDesc *desc, +const char *name) +{ +int i; + +for (i =0 ; desc[i].name !=NULL ; i++) { +if (strcmp (desc[i].name, name) ==0 ) { +return &desc[i]; + } + } + +return NULL ; +} +const char *qemu_opt_get (QemuOpts *opts,const char *name) {QemuOpt *opt =qemu_opt_find (opts, name); -return opt ? opt->str :NULL ; +const QemuOptDesc *desc; + desc =find_desc_by_name (opts->list->desc, name); + +return opt ? opt->str : + (desc && desc->def_value_str ? desc->def_value_str :NULL ); }bool qemu_opt_has_help_opt (QemuOpts *opts)
@@ -546,9 +564,15 @@ bool qemu_opt_has_help_opt(QemuOpts *opts)bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_bool (name, desc->def_value_str, &defval,NULL ); + }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL );return opt->value.boolean; }
@@ -556,9 +580,15 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_number (name, desc->def_value_str, &defval,NULL ); + }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_NUMBER );return opt->value.uint; }
@@ -566,9 +596,15 @@ uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_size (name, desc->def_value_str, &defval,NULL ); + }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE );return opt->value.uint; }
@@ -609,20 +645,6 @@ static bool opts_accepts_any(const QemuOpts *opts)return opts->list->desc[0 ].name ==NULL ; } -static const QemuOptDesc *find_desc_by_name (const QemuOptDesc *desc, -const char *name) -{ -int i; - -for (i =0 ; desc[i].name !=NULL ; i++) { -if (strcmp (desc[i].name, name) ==0 ) { -return &desc[i]; - } - } - -return NULL ; -} -static void opt_set (QemuOpts *opts,const char *name,const char *value,bool prepend,Error **errp) {
Signature · 1 line
1.7.11.7
PATCH v13 3/6 Create four QemuOptsList related functions
In reply to Dong Xu WangThis patch will create 4 functions, count_opts_list, qemu_opts_append, qemu_opts_free and qemu_opts_print_help, they will be used in following commits.
---v12->v13:
1) simply assert that neither argument has merge_lists set.
2) drop superfluous paranthesesis around p == first.
v11->v12:
1) renmae functions.
2) fix loop styles and code styles.
3) qemu_opts_apend will not return NULL now.
4) merge_lists value is from arguments in qemu_opts_append.
v6->v7:
1) Fix typo.
v5->v6:
1) allocate enough space in append_opts_list function.
include/qemu/option.h | 3 ++ util/qemu-option.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+)
include/qemu/option.h +3 -0
@@ -157,4 +157,7 @@ int qemu_opts_print(QemuOpts *opts);int qemu_opts_foreach (QemuOptsList *list,qemu_opts_loopfunc func,void *opaque,int abort_on_failure); +QemuOptsList *qemu_opts_append (QemuOptsList *first,QemuOptsList *second); +void qemu_opts_free (QemuOptsList *list); +void qemu_opts_print_help (QemuOptsList *list);#endif
util/qemu-option.c +82 -0
85 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v13 4/6 create some QemuOpts functons
In reply to Dong Xu WangThese functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: after get an option value, options++.
--- include/qemu/option.h | 11 +++++- util/qemu-option.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 9 deletions(-)
include/qemu/option.h +10 -1
@@ -108,6 +108,7 @@ struct QemuOptsList { };const char *qemu_opt_get (QemuOpts *opts,const char *name); +const char *qemu_opt_get_del (QemuOpts *opts,const char *name);/** * qemu_opt_has_help_opt: * @opts: options to search for a help request
@@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);*/ bool qemu_opt_has_help_opt (QemuOpts *opts);bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval); +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval);uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval);uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval); +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval);int qemu_opt_set (QemuOpts *opts,const char *name,const char *value); +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value);void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp);int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val);int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val); +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val);typedef int (*qemu_opt_loopfunc )(const char *name,const char *value,void *opaque);int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure);
@@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts);void qemu_opts_del (QemuOpts *opts);void qemu_opts_validate (QemuOpts *opts,const QemuOptDesc *desc,Error **errp);int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname); -QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,int permit_abbrev); +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname); +QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params, +int permit_abbrev);void qemu_opts_set_defaults (QemuOptsList *list,const char *params,int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
util/qemu-option.c +95 -8
187 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v13 5/6 Use QemuOpts support in block layer
In reply to Dong Xu WangThis patch will use QemuOpts related functions in block layer, add a member bdrv_create_opts to BlockDriver struct, it will return a QemuOptsList pointer, which includes the image format's create options.
And create options's primary consumer is block creating related functions, so modify them together.
---v12->v13:
1) split into 2 patches. one for block, one for QemuOpts funcitons.
2) fix leaking cco->opts.
3) other small fix.
v11->v12:
1) create functions, such as qemu_opt_get_del and qemu_opt_replace_set. These functions works like origin code.
2) use QEMU_OPT_SIZE, not QEMU_OPT_NUMBER.
3) in bdrv_create, if opts is NULL, will create an empty one, so can discard if(opts) code safely.
v10->v11:
1) qed.h move QED_DEFAULT_CLUSTER_SIZE from enum to macro, or qemu_opts_print produce un-expanded cluster_size.
2) In qcow2.c and qcow.c, bdrv_create_file(filename, NULL), NULL -> opts,
or while using protocol, there will be an error.
v8->v9:
1) add qemu_ prefix to gluster_create_opts.
2) fix bug: bdrv_gluster_unix and bdrv_gluster_rdma should also be converted.
v7->v8:
1) rebase to upstream source tree.
2) add gluster.c, raw-win32.c, and rbd.c.
v6->v7:
1) use osdep.h:stringify(), not redefining new macro.
2) preserve TODO comment.
3) fix typo. BLOCK_OPT_ENCRYPT->BLOCK_OPT_STATIC.
4) initialize disk_type even when opts is NULL.
v5->v6:
1) judge if opts == NULL in block layer create functions.
2) use bdrv_create_file(filename, NULL) in qcow_create and cow_create funtion.
3) made more readable while using qemu_opt_get_number.
block.c | 98 ++++++++++++-------------- block/cow.c | 46 ++++++------ block/gluster.c | 37 +++++----- block/iscsi.c | 35 +++++----- block/qcow.c | 61 ++++++++-------- block/qcow2.c | 173 +++++++++++++++++++++++----------------------- block/qed.c | 86 +++++++++++------------ block/qed.h | 2 +- block/raw-posix.c | 59 +++++++--------- block/raw-win32.c | 31 +++++---- block/raw.c | 30 ++++---- block/rbd.c | 62 ++++++++--------- block/sheepdog.c | 79 ++++++++++----------- block/vdi.c | 70 +++++++++---------- block/vmdk.c | 90 ++++++++++++------------ block/vpc.c | 57 +++++++-------- block/vvfat.c | 11 +-- include/block/block.h | 5 +- include/block/block_int.h | 6 +- qemu-img.c | 61 ++++++++-------- 20 files changed, 535 insertions(+), 564 deletions(-)
block.c +46 -52
@@ -355,7 +355,7 @@ BlockDriver *bdrv_find_whitelisted_format(const char *format_name)typedef struct CreateCo {BlockDriver *drv;char *filename; -QEMUOptionParameter *options; +QemuOpts *opts;int ret; }CreateCo ;
@@ -364,11 +364,10 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque)CreateCo *cco = opaque;assert (cco->drv); - cco->ret = cco->drv->bdrv_create (cco->filename, cco->options); + cco->ret = cco->drv->bdrv_create (cco->filename, cco->opts); } -int bdrv_create (BlockDriver *drv,const char * filename, -QEMUOptionParameter *options) +int bdrv_create (BlockDriver *drv,const char * filename,QemuOpts *opts) {int ret;
@@ -376,7 +375,7 @@ int bdrv_create(BlockDriver *drv, const char* filename,CreateCo cco = { .drv = drv, .filename =g_strdup (filename), - .options = options, + .opts = opts ?:qemu_opts_create_nofail (drv->bdrv_create_opts), .ret =NOT_DONE , };
@@ -399,11 +398,14 @@ int bdrv_create(BlockDriver *drv, const char* filename, ret = cco.ret; out: +if (!opts) { +qemu_opts_del (cco.opts); + }g_free (cco.filename);return ret; } -int bdrv_create_file (const char * filename,QEMUOptionParameter *options) +int bdrv_create_file (const char *filename,QemuOpts *opts) {BlockDriver *drv;
@@ -412,7 +414,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options)return -ENOENT ; } -return bdrv_create (drv, filename, options); +return bdrv_create (drv, filename, opts); }/*
@@ -924,7 +926,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,BlockDriverState *bs1;int64_t total_size;BlockDriver *bdrv_qcow2; -QEMUOptionParameter *create_options; +QemuOpts *opts;char backing_filename[PATH_MAX ];if (qdict_size (options) !=0 ) {
@@ -963,19 +965,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, } bdrv_qcow2 =bdrv_find_format ("qcow2" ); - create_options =parse_option_parameters ("" , bdrv_qcow2->create_options, -NULL ); + opts =qemu_opts_create_nofail (bdrv_qcow2->bdrv_create_opts); -set_option_parameter_int (create_options,BLOCK_OPT_SIZE , total_size); -set_option_parameter (create_options,BLOCK_OPT_BACKING_FILE , - backing_filename); +qemu_opt_set_number (opts,BLOCK_OPT_SIZE , total_size); +qemu_opt_set (opts,BLOCK_OPT_BACKING_FILE , backing_filename);if (drv) { -set_option_parameter (create_options,BLOCK_OPT_BACKING_FMT , - drv->format_name); +qemu_opt_set (opts,BLOCK_OPT_BACKING_FMT , drv->format_name); } - ret =bdrv_create (bdrv_qcow2, tmp_filename, create_options); -free_option_parameters (create_options); + ret =bdrv_create (bdrv_qcow2, tmp_filename, opts); +qemu_opts_del (opts);if (ret <0 ) { goto fail; }
@@ -4640,8 +4639,10 @@ void bdrv_img_create(const char *filename, const char *fmt, char *options, uint64_t img_size, int flags, Error **errp, bool quiet) { - QEMUOptionParameter *param = NULL, *create_options = NULL; -QEMUOptionParameter *backing_fmt, *backing_file, *size; + QemuOpts *opts = NULL; +QemuOptsList *create_opts =NULL ; +const char *backing_fmt, *backing_file; +int64_t size;BlockDriverState *bs =NULL ;BlockDriver *drv, *proto_drv;BlockDriver *backing_drv =NULL ;
@@ -4659,28 +4660,23 @@ void bdrv_img_create(const char *filename, const char *fmt,error_setg (errp,"Unknown protocol '%s'" , filename);return ; } - - create_options =append_option_parameters (create_options, - drv->create_options); - create_options =append_option_parameters (create_options, - proto_drv->create_options); - + create_opts =qemu_opts_append (drv->bdrv_create_opts, + proto_drv->bdrv_create_opts);/* Create parameter list with default values */ - param =parse_option_parameters ("" , create_options, param); + opts =qemu_opts_create_nofail (create_opts); -set_option_parameter_int (param,BLOCK_OPT_SIZE , img_size); +qemu_opt_set_number (opts,BLOCK_OPT_SIZE , img_size);/* Parse -o options */ if (options) { - param =parse_option_parameters (options, create_options, param); -if (param ==NULL ) { +if (qemu_opts_do_parse_replace (opts, options,NULL ) !=0 ) {error_setg (errp,"Invalid options for file format '%s'." , fmt); goto out; } }if (base_filename) { -if (set_option_parameter (param,BLOCK_OPT_BACKING_FILE , +if (qemu_opt_replace_set (opts,BLOCK_OPT_BACKING_FILE , base_filename)) {error_setg (errp,"Backing file not supported for file format '%s'" , fmt);
@@ -4689,39 +4685,37 @@ void bdrv_img_create(const char *filename, const char *fmt, }if (base_fmt) { -if (set_option_parameter (param,BLOCK_OPT_BACKING_FMT , base_fmt)) { +if (qemu_opt_replace_set (opts,BLOCK_OPT_BACKING_FMT , base_fmt)) {error_setg (errp,"Backing file format not supported for file " "format '%s'" , fmt); goto out; } } - backing_file =get_option_parameter (param,BLOCK_OPT_BACKING_FILE ); -if (backing_file && backing_file->value.s) { -if (!strcmp (filename, backing_file->value.s)) { + backing_file =qemu_opt_get (opts,BLOCK_OPT_BACKING_FILE ); +if (backing_file) { +if (!strcmp (filename, backing_file)) {error_setg (errp,"Error: Trying to create an image with the " "same filename as the backing file" ); goto out; } } - backing_fmt =get_option_parameter (param,BLOCK_OPT_BACKING_FMT ); -if (backing_fmt && backing_fmt->value.s) { - backing_drv =bdrv_find_format (backing_fmt->value.s); + backing_fmt =qemu_opt_get (opts,BLOCK_OPT_BACKING_FMT ); +if (backing_fmt) { + backing_drv =bdrv_find_format (backing_fmt);if (!backing_drv) { -error_setg (errp,"Unknown backing file format '%s'" , - backing_fmt->value.s); +error_setg (errp,"Unknown backing file format '%s'" , backing_fmt); goto out; } }// The size for the image must always be specified, with one exception: // If we are using a backing file, we can obtain the size from there - size =get_option_parameter (param,BLOCK_OPT_SIZE ); -if (size && size->value.n ==-1 ) { -if (backing_file && backing_file->value.s) { + size =qemu_opt_get_size (opts,BLOCK_OPT_SIZE ,0 ); +if (size ==-1 ) { +if (backing_file) {uint64_t size; -char buf[32 ];int back_flags;/* backing files always opened read-only */
@@ -4730,18 +4724,16 @@ void bdrv_img_create(const char *filename, const char *fmt, bs =bdrv_new ("" ); - ret =bdrv_open (bs, backing_file->value.s,NULL , back_flags, - backing_drv); + ret =bdrv_open (bs, backing_file,NULL , back_flags, backing_drv);if (ret <0 ) {error_setg_errno (errp, -ret,"Could not open '%s'" , - backing_file->value.s); + backing_file); goto out; }bdrv_get_geometry (bs, &size); size *=512 ; -snprintf (buf,sizeof (buf),"%" PRId64, size); -set_option_parameter (param,BLOCK_OPT_SIZE , buf); +qemu_opt_replace_set_number (opts,BLOCK_OPT_SIZE , size); }else {error_setg (errp,"Image creation needs a size parameter" ); goto out;
@@ -4750,10 +4742,10 @@ void bdrv_img_create(const char *filename, const char *fmt,if (!quiet) {printf ("Formatting '%s', fmt=%s " , filename, fmt); -print_option_parameters (param); +qemu_opts_print (opts);puts ("" ); } - ret =bdrv_create (drv, filename, param); + ret =bdrv_create (drv, filename, opts);if (ret <0 ) {if (ret == -ENOTSUP ) { error_setg(errp,"Formatting or formatting option not supported for "
@@ -4768,8 +4760,10 @@ void bdrv_img_create(const char *filename, const char *fmt, } out: -free_option_parameters (create_options); -free_option_parameters (param); +qemu_opts_free (create_opts); +if (opts) { +qemu_opts_del (opts); + }if (bs) {bdrv_delete (bs);
block/cow.c +22 -24
71 lines not on this page · raw message
block/gluster.c +18 -19
@@ -335,8 +335,7 @@ out:return ret; } -static int qemu_gluster_create (const char *filename, -QEMUOptionParameter *options) +static int qemu_gluster_create (const char *filename,QemuOpts *opts) {struct glfs *glfs;struct glfs_fd *fd;
@@ -350,12 +349,8 @@ static int qemu_gluster_create(const char *filename, goto out; } -while (options && options->name) { -if (!strcmp (options->name,BLOCK_OPT_SIZE )) { - total_size = options->value.n /BDRV_SECTOR_SIZE ; - } - options++; - } + total_size = +qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ) /BDRV_SECTOR_SIZE ; fd =glfs_creat (glfs, gconf->image,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,S_IRUSR |S_IWUSR );
@@ -544,13 +539,17 @@ static void qemu_gluster_close(BlockDriverState *bs)glfs_fini (s->glfs); } -static QEMUOptionParameter qemu_gluster_create_options[] = { - { - .name =BLOCK_OPT_SIZE , - .type =OPT_SIZE , - .help ="Virtual disk size" - }, - {NULL } +static QemuOptsList qemu_gluster_create_opts = { + .name ="qemu-gluster-create-opts" , + .head =QTAILQ_HEAD_INITIALIZER (qemu_gluster_create_opts.head), + .desc = { + { + .name =BLOCK_OPT_SIZE , + .type =QEMU_OPT_SIZE , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static BlockDriver bdrv_gluster = {
@@ -565,7 +564,7 @@ static BlockDriver bdrv_gluster = { .bdrv_aio_readv = qemu_gluster_aio_readv, .bdrv_aio_writev = qemu_gluster_aio_writev, .bdrv_aio_flush = qemu_gluster_aio_flush, - .create_options = qemu_gluster_create_options, + .bdrv_create_opts = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_tcp = {
@@ -580,7 +579,7 @@ static BlockDriver bdrv_gluster_tcp = { .bdrv_aio_readv = qemu_gluster_aio_readv, .bdrv_aio_writev = qemu_gluster_aio_writev, .bdrv_aio_flush = qemu_gluster_aio_flush, - .create_options = qemu_gluster_create_options, + .bdrv_create_opts = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_unix = {
@@ -595,7 +594,7 @@ static BlockDriver bdrv_gluster_unix = { .bdrv_aio_readv = qemu_gluster_aio_readv, .bdrv_aio_writev = qemu_gluster_aio_writev, .bdrv_aio_flush = qemu_gluster_aio_flush, - .create_options = qemu_gluster_create_options, + .bdrv_create_opts = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_rdma = {
@@ -610,7 +609,7 @@ static BlockDriver bdrv_gluster_rdma = { .bdrv_aio_readv = qemu_gluster_aio_readv, .bdrv_aio_writev = qemu_gluster_aio_writev, .bdrv_aio_flush = qemu_gluster_aio_flush, - .create_options = qemu_gluster_create_options, + .bdrv_create_opts = &qemu_gluster_create_opts, };static void bdrv_gluster_init (void )
block/iscsi.c +16 -19
@@ -904,9 +904,7 @@ static char *parse_initiator_name(const char *target)if (!opts) { opts =QTAILQ_FIRST (&list->head); } -if (opts) { - name =qemu_opt_get (opts,"initiator-name" ); - } + name =qemu_opt_get (opts,"initiator-name" ); }if (name) {
@@ -1184,7 +1182,7 @@ static int iscsi_has_zero_init(BlockDriverState *bs)return 0 ; } -static int iscsi_create (const char *filename,QEMUOptionParameter *options) +static int iscsi_create (const char *filename,QemuOpts *opts) {int ret =0 ;int64_t total_size =0 ;
@@ -1194,13 +1192,8 @@ static int iscsi_create(const char *filename, QEMUOptionParameter *options)memset (&bs,0 ,sizeof (BlockDriverState));/* Read out options */ -while (options && options->name) { -if (!strcmp (options->name,"size" )) { - total_size = options->value.n /BDRV_SECTOR_SIZE ; - } - options++; - } - + total_size = +qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ) /BDRV_SECTOR_SIZE ; bs.opaque =g_malloc0 (sizeof (struct IscsiLun )); iscsilun = bs.opaque;
@@ -1229,13 +1222,17 @@ out:return ret; } -static QEMUOptionParameter iscsi_create_options[] = { - { - .name =BLOCK_OPT_SIZE , - .type =OPT_SIZE , - .help ="Virtual disk size" - }, - {NULL } +static QemuOptsList iscsi_create_opts = { + .name ="iscsi-create-opts" , + .head =QTAILQ_HEAD_INITIALIZER (iscsi_create_opts.head), + .desc = { + { + .name =BLOCK_OPT_SIZE , + .type =QEMU_OPT_SIZE , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static BlockDriver bdrv_iscsi = {
@@ -1246,7 +1243,7 @@ static BlockDriver bdrv_iscsi = { .bdrv_file_open = iscsi_open, .bdrv_close = iscsi_close, .bdrv_create = iscsi_create, - .create_options = iscsi_create_options, + .bdrv_create_opts = &iscsi_create_opts, .bdrv_getlength = iscsi_getlength, .bdrv_truncate = iscsi_truncate,
block/qcow.c +30 -31
87 lines not on this page · raw message
block/qcow2.c +88 -85
218 lines not on this page · raw message
block/qed.c +42 -44
109 lines not on this page · raw message
block/qed.h +1 -1
@@ -43,6 +43,7 @@* * All fields are little-endian on disk. */ +#define QED_DEFAULT_CLUSTER_SIZE 65536enum {QED_MAGIC ='Q' |'E' <<8 |'D' <<16 |'\0' <<24 ,
@@ -69,7 +70,6 @@ enum {*/ QED_MIN_CLUSTER_SIZE =4 *1024 ,/* in bytes */ QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024, -QED_DEFAULT_CLUSTER_SIZE =64 *1024 ,/* Allocated clusters are tracked using a 2-level pagetable. Table size is * a multiple of clusters so large maximum image sizes can be supported
block/raw-posix.c +26 -33
150 lines not on this page · raw message
block/raw-win32.c +16 -15
@@ -385,18 +385,15 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs)return st.st_size; } -static int raw_create (const char *filename,QEMUOptionParameter *options) +static int raw_create (const char *filename,QemuOpts *opts) {int fd;int64_t total_size =0 ;/* Read out options */ -while (options && options->name) { -if (!strcmp (options->name,BLOCK_OPT_SIZE )) { - total_size = options->value.n /512 ; - } - options++; - } + + total_size = +qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ) /512 ; fd =qemu_open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,0644 );
@@ -408,13 +405,17 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)return 0 ; } -static QEMUOptionParameter raw_create_options[] = { - { - .name =BLOCK_OPT_SIZE , - .type =OPT_SIZE , - .help ="Virtual disk size" - }, - {NULL } +static QemuOptsList raw_create_opts = { + .name ="raw-create-opts" , + .head =QTAILQ_HEAD_INITIALIZER (raw_create_opts.head), + .desc = { + { + .name =BLOCK_OPT_SIZE , + .type =QEMU_OPT_SIZE , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static BlockDriver bdrv_file = {
@@ -434,7 +435,7 @@ static BlockDriver bdrv_file = { .bdrv_get_allocated_file_size = raw_get_allocated_file_size, - .create_options = raw_create_options, + .bdrv_create_opts = &raw_create_opts, };/***********************************************/
block/raw.c +17 -13
@@ -95,18 +95,22 @@ static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,return bdrv_aio_ioctl (bs->file, req, buf, cb, opaque); } -static int raw_create (const char *filename,QEMUOptionParameter *options) -{ -return bdrv_create_file (filename, options); -} - -static QEMUOptionParameter raw_create_options[] = { - { - .name =BLOCK_OPT_SIZE , - .type =OPT_SIZE , - .help ="Virtual disk size" - }, - {NULL } +static int raw_create (const char *filename,QemuOpts *opts) +{ +return bdrv_create_file (filename, opts); +} + +static QemuOptsList raw_create_opts = { + .name ="raw-create-opts" , + .head =QTAILQ_HEAD_INITIALIZER (raw_create_opts.head), + .desc = { + { + .name =BLOCK_OPT_SIZE , + .type =QEMU_OPT_SIZE , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static int raw_has_zero_init (BlockDriverState *bs)
@@ -143,8 +147,8 @@ static BlockDriver bdrv_raw = { .bdrv_aio_ioctl = raw_aio_ioctl, .bdrv_create = raw_create, - .create_options = raw_create_options, .bdrv_has_zero_init = raw_has_zero_init, + .bdrv_create_opts = &raw_create_opts, };static void bdrv_raw_init (void )
block/rbd.c +30 -32
88 lines not on this page · raw message
block/sheepdog.c +37 -42
120 lines not on this page · raw message
block/vdi.c +34 -36
102 lines not on this page · raw message
block/vmdk.c +44 -46
115 lines not on this page · raw message
block/vpc.c +29 -28
90 lines not on this page · raw message
block/vvfat.c +6 -5
@@ -2803,7 +2803,7 @@ static BlockDriver vvfat_write_target = {static int enable_write_target (BDRVVVFATState *s) {BlockDriver *bdrv_qcow; -QEMUOptionParameter *options; +QemuOpts *opts;int ret;int size =sector2cluster (s, s->sector_count); s->used_clusters =calloc (size,1 );
@@ -2819,12 +2819,13 @@ static int enable_write_target(BDRVVVFATState *s) } bdrv_qcow =bdrv_find_format ("qcow" ); - options =parse_option_parameters ("" , bdrv_qcow->create_options,NULL ); -set_option_parameter_int (options,BLOCK_OPT_SIZE , s->sector_count *512 ); -set_option_parameter (options,BLOCK_OPT_BACKING_FILE ,"fat:" ); + opts =qemu_opts_create_nofail (bdrv_qcow->bdrv_create_opts); +qemu_opt_set_number (opts,BLOCK_OPT_SIZE , s->sector_count *512 ); +qemu_opt_set (opts,BLOCK_OPT_BACKING_FILE ,"fat:" ); -if (bdrv_create (bdrv_qcow, s->qcow_filename, options) <0 ) +if (bdrv_create (bdrv_qcow, s->qcow_filename, opts) <0 ) {return -1 ; + } s->qcow =bdrv_new ("" );if (s->qcow ==NULL ) {
include/block/block.h +2 -3
@@ -125,9 +125,8 @@ void bdrv_init_with_whitelist(void);BlockDriver *bdrv_find_protocol (const char *filename);BlockDriver *bdrv_find_format (const char *format_name);BlockDriver *bdrv_find_whitelisted_format (const char *format_name); -int bdrv_create (BlockDriver *drv,const char * filename, -QEMUOptionParameter *options); -int bdrv_create_file (const char * filename,QEMUOptionParameter *options); +int bdrv_create (BlockDriver *drv,const char * filename,QemuOpts *opts); +int bdrv_create_file (const char *filename,QemuOpts *opts);BlockDriverState *bdrv_new (const char *device_name);void bdrv_make_anon (BlockDriverState *bs);void bdrv_swap (BlockDriverState *bs_new,BlockDriverState *bs_old);
include/block/block_int.h +2 -4
@@ -95,7 +95,7 @@ struct BlockDriver { const uint8_t *buf, int nb_sectors);void (*bdrv_close)(BlockDriverState *bs);void (*bdrv_rebind)(BlockDriverState *bs); -int (*bdrv_create)(const char *filename,QEMUOptionParameter *options); +int (*bdrv_create)(const char *filename,QemuOpts *opts);int (*bdrv_set_key)(BlockDriverState *bs,const char *key);int (*bdrv_make_empty)(BlockDriverState *bs);/* aio */
@@ -184,9 +184,7 @@ struct BlockDriver { unsigned long int req, void *buf, BlockDriverCompletionFunc *cb, void *opaque); -/* List of options for creating images, terminated by name == NULL */ -QEMUOptionParameter *create_options; - +QemuOptsList *bdrv_create_opts;/* * Returns 0 for completed check, -errno for internal errors.
qemu-img.c +29 -32
138 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v13 6/6 remove QEMUOptionParameter related functions and struct
In reply to Dong Xu Wang--- include/qemu/option.h | 32 ------ util/qemu-option.c | 285 -------------------------------------------------- 2 files changed, 317 deletions(-)
include/qemu/option.h +0 -32
@@ -38,17 +38,6 @@ enum QEMUOptionParType { OPT_STRING, }; -typedef struct QEMUOptionParameter { -const char *name; -enum QEMUOptionParType type; -union { -uint64_t n; -char * s; - } value; -const char *help; -}QEMUOptionParameter ; - -const char *get_opt_name (char *buf,int buf_size,const char *p,char delim);const char *get_opt_value (char *buf,int buf_size,const char *p); int get_next_param_value(char *buf, int buf_size,
@@ -58,27 +47,6 @@ int get_param_value(char *buf, int buf_size,int check_params (char *buf,int buf_size,const char *const *params,const char *str); - -/* -* The following functions take a parameter list as input. This is a pointer to -* the first element of a QEMUOptionParameter array which is terminated by an -* entry with entry->name == NULL. -*/ - -QEMUOptionParameter *get_option_parameter (QEMUOptionParameter *list, -const char *name); -int set_option_parameter (QEMUOptionParameter *list,const char *name, -const char *value); -int set_option_parameter_int (QEMUOptionParameter *list,const char *name, -uint64_t value); -QEMUOptionParameter *append_option_parameters (QEMUOptionParameter *dest, -QEMUOptionParameter *list); -QEMUOptionParameter *parse_option_parameters (const char *param, -QEMUOptionParameter *list,QEMUOptionParameter *dest); -void free_option_parameters (QEMUOptionParameter *list); -void print_option_parameters (QEMUOptionParameter *list); -void print_option_help (QEMUOptionParameter *list); -/* ------------------------------------------------------------------ */ typedef struct QemuOpt QemuOpt ;
util/qemu-option.c +0 -285
297 lines not on this page · raw message
Signature · 1 line
1.7.11.7
Quoted 26 lines · Dong Xu Wang
These patches will replace QEMUOptionParameter with QemuOpts. Change logs please go to each patch's commit message.
Dong Xu Wang (6):
adddef_value_strin QemuOptDesc struct and rewriteqemu_opts_printavoid duplication of default value in QemuOpts
Create four QemuOptsList related functions
create some QemuOpts functons
Use QemuOpts support in block layer
remove QEMUOptionParameter related functions and structblock.c | 98 ++++---- block/cow.c | 46 ++-- block/gluster.c | 37 ++-- block/iscsi.c | 35 ++- block/qcow.c | 61 +++-- block/qcow2.c | 173 ++++++++------- block/qed.c | 86 ++++--- block/qed.h | 2 +- block/raw-posix.c | 59 +++-- block/raw-win32.c | 31 +-- block/raw.c | 30 +-- block/rbd.c | 62 +++--- block/sheepdog.c | 79 ++++--- block/vdi.c | 70 +++--- block/vmdk.c | 90 ++++----
Quoted 9 lines
block/vpc.c | 57 ++--- block/vvfat.c | 11 +- include/block/block.h | 5 +- include/block/block_int.h | 6 +- include/qemu/option.h | 49 ++-- qemu-img.c | 61 +++-- util/qemu-option.c | 555 ++++++++++++++++++++-------------------------- 22 files changed, 791 insertions(+), 912 deletions(-)
Ping..
Can anyone give some comments? That will be very grateful..
Quoted 36 lines · Dong Xu Wang
Quoted 26 lines · Dong Xu Wang
These patches will replace QEMUOptionParameter with QemuOpts. Change logs please go to each patch's commit message.
Dong Xu Wang (6):
adddef_value_strin QemuOptDesc struct and rewriteqemu_opts_printavoid duplication of default value in QemuOpts
Create four QemuOptsList related functions
create some QemuOpts functons
Use QemuOpts support in block layer
remove QEMUOptionParameter related functions and structblock.c | 98 ++++---- block/cow.c | 46 ++-- block/gluster.c | 37 ++-- block/iscsi.c | 35 ++- block/qcow.c | 61 +++-- block/qcow2.c | 173 ++++++++------- block/qed.c | 86 ++++--- block/qed.h | 2 +- block/raw-posix.c | 59 +++-- block/raw-win32.c | 31 +-- block/raw.c | 30 +-- block/rbd.c | 62 +++--- block/sheepdog.c | 79 ++++--- block/vdi.c | 70 +++--- block/vmdk.c | 90 ++++----Quoted 9 lines
block/vpc.c | 57 ++--- block/vvfat.c | 11 +- include/block/block.h | 5 +- include/block/block_int.h | 6 +- include/qemu/option.h | 49 ++-- qemu-img.c | 61 +++-- util/qemu-option.c | 555 ++++++++++++++++++++-------------------------- 22 files changed, 791 insertions(+), 912 deletions(-)
Quoted 2 lines
Ping..
Can anyone give some comments? That will be very grateful..
It's in my queue. Sorry for the delay, I know how frustrating it must be, but I'm doing what I can.
Re: [Qemu-devel] [PATCH V13 2/6] avoid duplication of default value in QemuOpts
In reply to Dong Xu WangQuoted 11 lines · Dong Xu Wang
According Markus's comments, his patch will move the default value entirely to QemuOptDesc.
When getting the value of an option that hasn't been set, and QemuOptDesc has a default value, return that. Else, behave as before.
Example:
qemu_opt_get_number(opts, "foo", 42)If "foo" has been set in opts, return its value.
Quoted 8 lines
Else, if opt's QemuOptDesc has a default value for "foo", return that.
Else, return 42.
Note that the last argument is useless when QemuOptDesc has a default value. Ugly. If it bothers us, assert that the argument equals the default from QemuOptDesc.
Last sentence is not 100% clear. Either change it to "If it bothers us, we could assert", or implement the assert. Considering we're at v12, I recommend the former.
Quoted 38 lines
Example:
qemu_opt_get(opts, "bar")If "bar" has been set in opts, return its value.
Else, if opt's QemuOptDesc has a default value for "bar", return that.
Else, return NULL.
Signed-off-by Dong Xu Wang <[email protected]>--- util/qemu-option.c | 58 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-)util/qemu-option.c +14
-0@@ -525,10 +525,28 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)return NULL ; } +static const QemuOptDesc *find_desc_by_name (const QemuOptDesc *desc, +const char *name) +{ +int i; + +for (i =0 ; desc[i].name !=NULL ; i++) { +if (strcmp (desc[i].name, name) ==0 ) { +return &desc[i]; + } + } + +return NULL ; +} +const char *qemu_opt_get (QemuOpts *opts,const char *name)
util/qemu-option.c static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) · line 551
Quoted 10 lines
util/qemu-option.c +5
-1{QemuOpt *opt =qemu_opt_find (opts, name); -return opt ? opt->str :NULL ; +const QemuOptDesc *desc; + desc =find_desc_by_name (opts->list->desc, name); + +return opt ? opt->str : + (desc && desc->def_value_str ? desc->def_value_str :NULL ); }
I'd make this function work exactly like the ones that follow:
if (!opt) {
desc = find_desc_by_name(opts->list->desc, name);
if (desc && desc->def_value_str) {
return desc->def_value_str;
}
}
return opt ? opt->str : NULL;Matter of taste; no need to respin just for that.
util/qemu-option.c bool qemu_opt_has_help_opt(QemuOpts *opts) · line 572
Quoted 12 lines
util/qemu-option.c +5
-1bool qemu_opt_has_help_opt (QemuOpts *opts)@@ -546,9 +564,15 @@ bool qemu_opt_has_help_opt(QemuOpts *opts)bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_bool (name, desc->def_value_str, &defval,NULL );
Ignores errors. Should only happen when somebody sets a bad desc->def_value_str. Because those are fixed at compile time, that's a programming error. Recommend to assert like this:
parse_option_bool(name, desc->def_value_str, &defval, &local_err);
assert(!local_err);Quoted 7 lines
util/qemu-option.c +2
-0+ }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL );return opt->value.boolean; }
util/qemu-option.c bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) · line 588
Quoted 10 lines
util/qemu-option.c +5
-1@@ -556,9 +580,15 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_number (name, desc->def_value_str, &defval,NULL );
Likewise.
Quoted 7 lines
util/qemu-option.c +2
-0+ }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_NUMBER );return opt->value.uint; }
util/qemu-option.c uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) · line 604
Quoted 10 lines
util/qemu-option.c +5
-1@@ -566,9 +596,15 @@ uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name); +const QemuOptDesc *desc; -if (opt ==NULL ) +if (opt ==NULL ) { + desc =find_desc_by_name (opts->list->desc, name); +if (desc && desc->def_value_str) { +parse_option_size (name, desc->def_value_str, &defval,NULL );
Likewise.
Quoted 27 lines
util/qemu-option.c +2
-14+ }return defval; + }assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE );return opt->value.uint; }@@ -609,20 +645,6 @@ static bool opts_accepts_any(const QemuOpts *opts)return opts->list->desc[0 ].name ==NULL ; } -static const QemuOptDesc *find_desc_by_name (const QemuOptDesc *desc, -const char *name) -{ -int i; - -for (i =0 ; desc[i].name !=NULL ; i++) { -if (strcmp (desc[i].name, name) ==0 ) { -return &desc[i]; - } - } - -return NULL ; -} -static void opt_set (QemuOpts *opts,const char *name,const char *value,bool prepend,Error **errp) {
Re: [Qemu-devel] [PATCH V13 1/6] add def_value_str in QemuOptDesc struct and rewrite qemu_opts_print
In reply to Dong Xu WangQuoted 64 lines · Dong Xu Wang
qemu_opts_printhas no user now, so can re-write the function safely.
qemu_opts_printwill be used while using "qemu-img create", it will produce the same output as previous code.The behavior of this function has changed:
1. Print every possible option, whether a value has been set or not.
2. Option descriptors may provide a default value.
3. Print to stdout instead of stderr.Previously the behavior was to print every option that has been set. Options that have not been set would be skipped.
Signed-off-by Dong Xu Wang <[email protected]>---
v12->v13
1) re-write commit message.
v11->v12
1) makedef_value_strbecome the real default value string inopt_setfunction.
v10->v11:
1) print all values that have actually been assigned while accept-any cases.
v7->v8:
1) print "elements => accept any params" whileopts_accepts_any()== true.
2) sincedef_print_stris the default value if an option isn't set, so rename it todef_value_str.include/qemu/option.h | 3 ++- util/qemu-option.c | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-)include/qemu/option.h +2
-1@@ -96,6 +96,7 @@ typedef struct QemuOptDesc {const char *name;enum QemuOptType type;const char *help; +const char *def_value_str; } QemuOptDesc;struct QemuOptsList {@@ -152,7 +153,7 @@ QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);void qemu_opts_absorb_qdict (QemuOpts *opts,QDict *qdict,Error **errp);typedef int (*qemu_opts_loopfunc )(QemuOpts *opts,void *opaque); -int qemu_opts_print (QemuOpts *opts,void *dummy); +int qemu_opts_print (QemuOpts *opts);int qemu_opts_foreach (QemuOptsList *list,qemu_opts_loopfunc func,void *opaque,int abort_on_failure);util/qemu-option.c +0
-0
util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 649
Quoted 4 lines
util/qemu-option.c +1
-0@@ -646,6 +646,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value, } opt->desc = desc; opt->str =g_strdup (value); + opt->str =g_strdup (value ?: desc->def_value_str);
Memory leak. Did you forget to delete the previous line? You do it in PATCH 4/6, plugging the leak...
util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 652
Quoted 3 lines
util/qemu-option.c +0
-0qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {error_propagate (errp, local_err);
opt_set() now accepts a null value argument, and so do its callers qemu_opt_set(), qemu_opt_set_err(), qemu_opts_set(). Why?
Quoted 42 lines
util/qemu-option.c +26
-6@@ -860,16 +861,36 @@ void qemu_opts_del(QemuOpts *opts)g_free (opts); } -int qemu_opts_print (QemuOpts *opts,void *dummy) +int qemu_opts_print (QemuOpts *opts) {QemuOpt *opt; +QemuOptDesc *desc = opts->list->desc; -fprintf (stderr,"%s: %s:" , opts->list->name, - opts->id ? opts->id :"<noid>" ); - QTAILQ_FOREACH(opt, &opts->head, next) { -fprintf (stderr," %s=\"%s\"" , opt->name, opt->str); +if (desc[0 ].name ==NULL ) { + QTAILQ_FOREACH(opt, &opts->head, next) { +printf ("%s=\"%s\" " , opt->name, opt->str); + } +return 0 ; + } +for (; desc && desc->name; desc++) { +const char *value = desc->def_value_str; +QemuOpt *opt; + + opt =qemu_opt_find (opts, desc->name); +if (opt) { + value = opt->str; + } + +if (!value) { +continue ; + } + +if (desc->type ==QEMU_OPT_STRING ) { +printf ("%s='%s' " , desc->name, value); + }else { +printf ("%s=%s " , desc->name, value); + } } -fprintf (stderr,"\n" );return 0 ; }
Re: [Qemu-devel] [PATCH V13 4/6] create some QemuOpts functons
In reply to Dong Xu WangQuoted 3 lines · Dong Xu Wang
These functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: after get an option value, options++.
I don't understand the last sentence.
util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name)
Quoted 63 lines
Signed-off-by Dong Xu Wang <[email protected]>--- include/qemu/option.h | 11 +++++- util/qemu-option.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 9 deletions(-)include/qemu/option.h +10
-1@@ -108,6 +108,7 @@ struct QemuOptsList { };const char *qemu_opt_get (QemuOpts *opts,const char *name); +const char *qemu_opt_get_del (QemuOpts *opts,const char *name);/** * qemu_opt_has_help_opt: * @opts: options to search for a help request @@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);*/ bool qemu_opt_has_help_opt (QemuOpts *opts);bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval); +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval);uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval);uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval); +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval);int qemu_opt_set (QemuOpts *opts,const char *name,const char *value); +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value);void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp);int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val);int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val); +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val);typedef int (*qemu_opt_loopfunc )(const char *name,const char *value,void *opaque);int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure);@@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts);void qemu_opts_del (QemuOpts *opts);void qemu_opts_validate (QemuOpts *opts,const QemuOptDesc *desc,Error **errp);int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname); -QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,int permit_abbrev); +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname); +QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params, +int permit_abbrev);void qemu_opts_set_defaults (QemuOptsList *list,const char *params,int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,util/qemu-option.c +2
-0@@ -33,6 +33,8 @@#include "qapi/qmp/qerror.h" #include "qemu/option_int.h" +static void qemu_opt_del (QemuOpt *opt); +/* * Extracts the name of an option from the parameter string (p points at the * first byte of the option name) @@ -549,6 +551,16 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name)
const char *qemu_opt_get(QemuOpts *opts, const char *name)
{
QemuOpt *opt = qemu_opt_find(opts, name);
const QemuOptDesc *desc;
desc = find_desc_by_name(opts->list->desc, name);
return opt ? opt->str :util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name) · line 563
Quoted 13 lines
util/qemu-option.c +10
-0(desc && desc->def_value_str ? desc->def_value_str :NULL ); } +const char *qemu_opt_get_del (QemuOpts *opts,const char *name) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +const char *str = opt ?g_strdup (opt->str) :NULL ; +if (opt) { +qemu_opt_del (opt); + } +return str; +} +
Unlike qemu_opt_del(), this one doesn't use def_value_str. Why? Isn't that a trap for users of this function?
Same question for the qemu_opt_get_FOO_del() that follow.
Quoted 72 lines
util/qemu-option.c +39
-1bool qemu_opt_has_help_opt (QemuOpts *opts) {QemuOpt *opt;@@ -577,6 +589,22 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)return opt->value.boolean; } +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +bool ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.boolean; +assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name);@@ -609,6 +637,23 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)return opt->value.uint; } +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +uint64_t ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.uint; +assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +static void qemu_opt_parse (QemuOpt *opt,Error **errp) {if (opt->desc ==NULL )@@ -646,7 +691,7 @@ static bool opts_accepts_any(const QemuOpts *opts) }static void opt_set (QemuOpts *opts,const char *name,const char *value, -bool prepend,Error **errp) +bool prepend,bool replace,Error **errp) {QemuOpt *opt;const QemuOptDesc *desc;@@ -658,6 +703,11 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,return ; } + opt =qemu_opt_find (opts, name); +if (replace && opt) { +qemu_opt_del (opt); + } + opt =g_malloc0 (sizeof (*opt)); opt->name =g_strdup (name); opt->opts = opts;
util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 722
Quoted 7 lines
util/qemu-option.c +0
-1@@ -667,7 +717,6 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,QTAILQ_INSERT_TAIL (&opts->head, opt, next); } opt->desc = desc; - opt->str =g_strdup (value); opt->str =g_strdup (value ?: desc->def_value_str);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {
Here you plug the leak you created in PATCH 1/6.
Quoted 17 lines
util/qemu-option.c +12
-1@@ -680,7 +729,29 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) {Error *local_err =NULL ; -opt_set (opts, name, value, false, &local_err); +opt_set (opts, name, value, false, false, &local_err); +if (error_is_set (&local_err)) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +return 0 ; +} + +/* +* If name exists, the function will delete the opt first and then add a new
util/qemu-option.c int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) · line 753
Quoted 10 lines
util/qemu-option.c +10
-0+* one. +*/ +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value) +{ +Error *local_err =NULL ; +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + }
Why? Won't opt_set() delete it already?
Same question for the qemu_opt_replace_set_FOO() that follow.
Quoted 72 lines
util/qemu-option.c +22
-5+opt_set (opts, name, value, false, true, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -693,7 +764,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp) { -opt_set (opts, name, value, false, errp); +opt_set (opts, name, value, false, false, errp); }int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val)@@ -740,6 +811,16 @@ int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)return 0 ; } +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + } +return qemu_opt_set_number (opts, name, val); +} +int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure) {@@ -917,7 +998,7 @@ int qemu_opts_print(QemuOpts *opts) }static int opts_do_parse (QemuOpts *opts,const char *params, -const char *firstname,bool prepend) +const char *firstname,bool prepend,bool replace) {char option[128 ], value[1024 ];const char *p,*pe,*pc;@@ -953,7 +1034,7 @@ static int opts_do_parse(QemuOpts *opts, const char *params, }if (strcmp (option,"id" ) !=0 ) {/* store and parse */ -opt_set (opts, option, value, prepend, &local_err); +opt_set (opts, option, value, prepend, replace, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -969,7 +1050,13 @@ static int opts_do_parse(QemuOpts *opts, const char *params,int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname) { -return opts_do_parse (opts, params, firstname, false); +return opts_do_parse (opts, params, firstname, false, false); +} + +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname) +{ +return opts_do_parse (opts, params, firstname, false, true); } static QemuOpts *opts_parse(QemuOptsList *list, const char *params,@@ -1008,7 +1095,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,return NULL ; } -if (opts_do_parse (opts, params, firstname, defaults) !=0 ) { +if (opts_do_parse (opts, params, firstname, defaults, false) !=0 ) {qemu_opts_del (opts);return NULL ; }
Quoted 2 lines · Dong Xu Wang
These patches will replace QEMUOptionParameter with QemuOpts. Change logs please go to each patch's commit message.
I reviewed 1-4/6 for now. I'm sorry it has taken me so long. Let's discuss my findings before I continue with 5/6, because that one's *big*.
Re: [Qemu-devel] [PATCH V13 4/6] create some QemuOpts functons
In reply to Markus ArmbrusterQuoted 200 lines · Markus Armbruster
Quoted 3 lines · Dong Xu Wang
These functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: after get an option value, options++.
I don't understand the last sentence.
util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name)
Quoted 63 lines
Signed-off-by Dong Xu Wang <[email protected]>--- include/qemu/option.h | 11 +++++- util/qemu-option.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 9 deletions(-)include/qemu/option.h +10
-1@@ -108,6 +108,7 @@ struct QemuOptsList { };const char *qemu_opt_get (QemuOpts *opts,const char *name); +const char *qemu_opt_get_del (QemuOpts *opts,const char *name);/** * qemu_opt_has_help_opt: * @opts: options to search for a help request @@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);*/ bool qemu_opt_has_help_opt (QemuOpts *opts);bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval); +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval);uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval);uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval); +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval);int qemu_opt_set (QemuOpts *opts,const char *name,const char *value); +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value);void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp);int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val);int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val); +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val);typedef int (*qemu_opt_loopfunc )(const char *name,const char *value,void *opaque);int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure);@@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts);void qemu_opts_del (QemuOpts *opts);void qemu_opts_validate (QemuOpts *opts,const QemuOptDesc *desc,Error **errp);int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname); -QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,int permit_abbrev); +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname); +QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params, +int permit_abbrev);void qemu_opts_set_defaults (QemuOptsList *list,const char *params,int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,util/qemu-option.c +2
-0@@ -33,6 +33,8 @@#include "qapi/qmp/qerror.h" #include "qemu/option_int.h" +static void qemu_opt_del (QemuOpt *opt); +/* * Extracts the name of an option from the parameter string (p points at the * first byte of the option name) @@ -549,6 +551,16 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name)const char *qemu_opt_get(QemuOpts *opts, const char *name) { QemuOpt *opt = qemu_opt_find(opts, name); const QemuOptDesc *desc; desc = find_desc_by_name(opts->list->desc, name); return opt ? opt->str :util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name) · line 563
Quoted 13 lines
util/qemu-option.c +10
-0(desc && desc->def_value_str ? desc->def_value_str :NULL ); } +const char *qemu_opt_get_del (QemuOpts *opts,const char *name) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +const char *str = opt ?g_strdup (opt->str) :NULL ; +if (opt) { +qemu_opt_del (opt); + } +return str; +} +Unlike
qemu_opt_del(), this one doesn't usedef_value_str. Why? Isn't that a trap for users of this function?Same question for the
qemu_opt_get_FOO_del()that follow.Quoted 72 lines
util/qemu-option.c +39
-1bool qemu_opt_has_help_opt (QemuOpts *opts) {QemuOpt *opt;@@ -577,6 +589,22 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)return opt->value.boolean; } +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +bool ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.boolean; +assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name);@@ -609,6 +637,23 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)return opt->value.uint; } +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +uint64_t ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.uint; +assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +static void qemu_opt_parse (QemuOpt *opt,Error **errp) {if (opt->desc ==NULL )@@ -646,7 +691,7 @@ static bool opts_accepts_any(const QemuOpts *opts) }static void opt_set (QemuOpts *opts,const char *name,const char *value, -bool prepend,Error **errp) +bool prepend,bool replace,Error **errp) {QemuOpt *opt;const QemuOptDesc *desc;@@ -658,6 +703,11 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,return ; } + opt =qemu_opt_find (opts, name); +if (replace && opt) { +qemu_opt_del (opt); + } + opt =g_malloc0 (sizeof (*opt)); opt->name =g_strdup (name); opt->opts = opts;util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 722
Quoted 7 lines
util/qemu-option.c +0
-1@@ -667,7 +717,6 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,QTAILQ_INSERT_TAIL (&opts->head, opt, next); } opt->desc = desc; - opt->str =g_strdup (value); opt->str =g_strdup (value ?: desc->def_value_str);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {Here you plug the leak you created in PATCH 1/6.
Quoted 21 lines
util/qemu-option.c +16
-1@@ -680,7 +729,29 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) {Error *local_err =NULL ; -opt_set (opts, name, value, false, &local_err); +opt_set (opts, name, value, false, false, &local_err); +if (error_is_set (&local_err)) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +return 0 ; +} + +/* +* If name exists, the function will delete the opt first and then add a new +* one. +*/ +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value) +{
Quoted 9 lines
util/qemu-option.c int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) · line 753
Quoted 6 lines
util/qemu-option.c +6
-0+Error *local_err =NULL ; +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + }Why? Won't
opt_set()delete it already?
No, opt_set will not delete it, but add a new opt. In block layer, while parsing options, if the parameter is parsed, it should be deleted and won't be used again, so I delete it manually.
Quoted 76 lines
Same question for the
qemu_opt_replace_set_FOO()that follow.Quoted 72 lines
util/qemu-option.c +22
-5+opt_set (opts, name, value, false, true, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -693,7 +764,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp) { -opt_set (opts, name, value, false, errp); +opt_set (opts, name, value, false, false, errp); }int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val)@@ -740,6 +811,16 @@ int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)return 0 ; } +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + } +return qemu_opt_set_number (opts, name, val); +} +int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure) {@@ -917,7 +998,7 @@ int qemu_opts_print(QemuOpts *opts) }static int opts_do_parse (QemuOpts *opts,const char *params, -const char *firstname,bool prepend) +const char *firstname,bool prepend,bool replace) {char option[128 ], value[1024 ];const char *p,*pe,*pc;@@ -953,7 +1034,7 @@ static int opts_do_parse(QemuOpts *opts, const char *params, }if (strcmp (option,"id" ) !=0 ) {/* store and parse */ -opt_set (opts, option, value, prepend, &local_err); +opt_set (opts, option, value, prepend, replace, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -969,7 +1050,13 @@ static int opts_do_parse(QemuOpts *opts, const char *params,int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname) { -return opts_do_parse (opts, params, firstname, false); +return opts_do_parse (opts, params, firstname, false, false); +} + +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname) +{ +return opts_do_parse (opts, params, firstname, false, true); } static QemuOpts *opts_parse(QemuOptsList *list, const char *params,@@ -1008,7 +1095,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,return NULL ; } -if (opts_do_parse (opts, params, firstname, defaults) !=0 ) { +if (opts_do_parse (opts, params, firstname, defaults, false) !=0 ) {qemu_opts_del (opts);return NULL ; }
Re: [Qemu-devel] [PATCH V13 4/6] create some QemuOpts functons
Quoted 96 lines · Dong Xu Wang
Quoted 95 lines · Markus Armbruster
Quoted 3 lines · Dong Xu Wang
These functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: after get an option value, options++.
I don't understand the last sentence.
util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name)
Quoted 63 lines
Signed-off-by Dong Xu Wang <[email protected]>--- include/qemu/option.h | 11 +++++- util/qemu-option.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 9 deletions(-)include/qemu/option.h +10
-1@@ -108,6 +108,7 @@ struct QemuOptsList { };const char *qemu_opt_get (QemuOpts *opts,const char *name); +const char *qemu_opt_get_del (QemuOpts *opts,const char *name);/** * qemu_opt_has_help_opt: * @opts: options to search for a help request @@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);*/ bool qemu_opt_has_help_opt (QemuOpts *opts);bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval); +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval);uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval);uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval); +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval);int qemu_opt_set (QemuOpts *opts,const char *name,const char *value); +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value);void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp);int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val);int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val); +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val);typedef int (*qemu_opt_loopfunc )(const char *name,const char *value,void *opaque);int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure);@@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts);void qemu_opts_del (QemuOpts *opts);void qemu_opts_validate (QemuOpts *opts,const QemuOptDesc *desc,Error **errp);int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname); -QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,int permit_abbrev); +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname); +QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params, +int permit_abbrev);void qemu_opts_set_defaults (QemuOptsList *list,const char *params,int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,util/qemu-option.c +2
-0@@ -33,6 +33,8 @@#include "qapi/qmp/qerror.h" #include "qemu/option_int.h" +static void qemu_opt_del (QemuOpt *opt); +/* * Extracts the name of an option from the parameter string (p points at the * first byte of the option name) @@ -549,6 +551,16 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name)const char *qemu_opt_get(QemuOpts *opts, const char *name) { QemuOpt *opt = qemu_opt_find(opts, name); const QemuOptDesc *desc; desc = find_desc_by_name(opts->list->desc, name); return opt ? opt->str :util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name) · line 563
Quoted 13 lines
util/qemu-option.c +10
-0(desc && desc->def_value_str ? desc->def_value_str :NULL ); } +const char *qemu_opt_get_del (QemuOpts *opts,const char *name) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +const char *str = opt ?g_strdup (opt->str) :NULL ; +if (opt) { +qemu_opt_del (opt); + } +return str; +} +Unlike
qemu_opt_del(), this one doesn't usedef_value_str. Why? Isn't that a trap for users of this function?
Quoted 1 line
Quoted 1 line
Same question for the
qemu_opt_get_FOO_del()that follow.
I'm still interested in answers :)
Quoted 112 lines
Quoted 103 lines
Quoted 72 lines
util/qemu-option.c +39
-1bool qemu_opt_has_help_opt (QemuOpts *opts) {QemuOpt *opt;@@ -577,6 +589,22 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)return opt->value.boolean; } +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +bool ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.boolean; +assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name);@@ -609,6 +637,23 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)return opt->value.uint; } +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +uint64_t ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.uint; +assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +static void qemu_opt_parse (QemuOpt *opt,Error **errp) {if (opt->desc ==NULL )@@ -646,7 +691,7 @@ static bool opts_accepts_any(const QemuOpts *opts) }static void opt_set (QemuOpts *opts,const char *name,const char *value, -bool prepend,Error **errp) +bool prepend,bool replace,Error **errp) {QemuOpt *opt;const QemuOptDesc *desc;@@ -658,6 +703,11 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,return ; } + opt =qemu_opt_find (opts, name); +if (replace && opt) { +qemu_opt_del (opt); + } + opt =g_malloc0 (sizeof (*opt)); opt->name =g_strdup (name); opt->opts = opts;util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 722
Quoted 7 lines
util/qemu-option.c +0
-1@@ -667,7 +717,6 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,QTAILQ_INSERT_TAIL (&opts->head, opt, next); } opt->desc = desc; - opt->str =g_strdup (value); opt->str =g_strdup (value ?: desc->def_value_str);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {Here you plug the leak you created in PATCH 1/6.
Quoted 21 lines
util/qemu-option.c +16
-1@@ -680,7 +729,29 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) {Error *local_err =NULL ; -opt_set (opts, name, value, false, &local_err); +opt_set (opts, name, value, false, false, &local_err); +if (error_is_set (&local_err)) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +return 0 ; +} + +/* +* If name exists, the function will delete the opt first and then add a new +* one. +*/ +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value) +{Quoted 9 lines
util/qemu-option.c int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) · line 753
Quoted 6 lines
util/qemu-option.c +6
-0+Error *local_err =NULL ; +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + }Why? Won't
opt_set()delete it already?
Quoted 1 line
No,
opt_setwill not delete it, but add a new opt. In block layer,
Then I'm confused...
Quoted 6 lines
while parsing options, if the parameter is parsed, it should be deleted and won't be used again, so I delete it manually.
Quoted 3 lines
Same question for the
qemu_opt_replace_set_FOO()that follow.Quoted 1 line
util/qemu-option.c +1
-0+opt_set (opts, name, value, false, true, &local_err);
... because here you pass true for parameter replace, which I (naively?) expect to delete the option. Can you unconfuse me?
Quoted 73 lines
Quoted 73 lines
Quoted 71 lines
util/qemu-option.c +21
-5if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -693,7 +764,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp) { -opt_set (opts, name, value, false, errp); +opt_set (opts, name, value, false, false, errp); }int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val)@@ -740,6 +811,16 @@ int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)return 0 ; } +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + } +return qemu_opt_set_number (opts, name, val); +} +int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure) {@@ -917,7 +998,7 @@ int qemu_opts_print(QemuOpts *opts) }static int opts_do_parse (QemuOpts *opts,const char *params, -const char *firstname,bool prepend) +const char *firstname,bool prepend,bool replace) {char option[128 ], value[1024 ];const char *p,*pe,*pc;@@ -953,7 +1034,7 @@ static int opts_do_parse(QemuOpts *opts, const char *params, }if (strcmp (option,"id" ) !=0 ) {/* store and parse */ -opt_set (opts, option, value, prepend, &local_err); +opt_set (opts, option, value, prepend, replace, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -969,7 +1050,13 @@ static int opts_do_parse(QemuOpts *opts, const char *params,int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname) { -return opts_do_parse (opts, params, firstname, false); +return opts_do_parse (opts, params, firstname, false, false); +} + +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname) +{ +return opts_do_parse (opts, params, firstname, false, true); } static QemuOpts *opts_parse(QemuOptsList *list, const char *params,@@ -1008,7 +1095,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,return NULL ; } -if (opts_do_parse (opts, params, firstname, defaults) !=0 ) { +if (opts_do_parse (opts, params, firstname, defaults, false) !=0 ) {qemu_opts_del (opts);return NULL ; }
Re: [Qemu-devel] [PATCH V13 4/6] create some QemuOpts functons
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Dong Xu Wang
Quoted 8 lines · Markus Armbruster
Quoted 3 lines · Dong Xu Wang
These functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: after get an option value, options++.
I don't understand the last sentence.
I will make the sentence clear next version.
In block layer, I created 2 series of functions:
1) one is qemu_opt_get_del series, it is used to parse create options, after parsing one parameter, will delete it.
2) the other is qemu_opt_replace_set series, it will delete related opt and then add a new one.
Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name)
Quoted 63 lines
Signed-off-by Dong Xu Wang <[email protected]>--- include/qemu/option.h | 11 +++++- util/qemu-option.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 9 deletions(-)include/qemu/option.h +10
-1@@ -108,6 +108,7 @@ struct QemuOptsList { };const char *qemu_opt_get (QemuOpts *opts,const char *name); +const char *qemu_opt_get_del (QemuOpts *opts,const char *name);/** * qemu_opt_has_help_opt: * @opts: options to search for a help request @@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);*/ bool qemu_opt_has_help_opt (QemuOpts *opts);bool qemu_opt_get_bool (QemuOpts *opts,const char *name,bool defval); +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval);uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval);uint64_t qemu_opt_get_size (QemuOpts *opts,const char *name,uint64_t defval); +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval);int qemu_opt_set (QemuOpts *opts,const char *name,const char *value); +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value);void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp);int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val);int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val); +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val);typedef int (*qemu_opt_loopfunc )(const char *name,const char *value,void *opaque);int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure);@@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts);void qemu_opts_del (QemuOpts *opts);void qemu_opts_validate (QemuOpts *opts,const QemuOptDesc *desc,Error **errp);int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname); -QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,int permit_abbrev); +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname); +QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params, +int permit_abbrev);void qemu_opts_set_defaults (QemuOptsList *list,const char *params,int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,util/qemu-option.c +2
-0@@ -33,6 +33,8 @@#include "qapi/qmp/qerror.h" #include "qemu/option_int.h" +static void qemu_opt_del (QemuOpt *opt); +/* * Extracts the name of an option from the parameter string (p points at the * first byte of the option name) @@ -549,6 +551,16 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name)const char *qemu_opt_get(QemuOpts *opts, const char *name) { QemuOpt *opt = qemu_opt_find(opts, name); const QemuOptDesc *desc; desc = find_desc_by_name(opts->list->desc, name); return opt ? opt->str :util/qemu-option.c const char *qemu_opt_get(QemuOpts *opts, const char *name) · line 563
Quoted 13 lines
util/qemu-option.c +10
-0(desc && desc->def_value_str ? desc->def_value_str :NULL ); } +const char *qemu_opt_get_del (QemuOpts *opts,const char *name) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +const char *str = opt ?g_strdup (opt->str) :NULL ; +if (opt) { +qemu_opt_del (opt); + } +return str; +} +Unlike
qemu_opt_del(), this one doesn't usedef_value_str. Why? Isn't that a trap for users of this function?
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Same question for the
qemu_opt_get_FOO_del()that follow.I'm still interested in answers :)
Here I made a mistake, it should use def_value_str, will fix in next version.
Quoted 119 lines
Quoted 112 lines
Quoted 103 lines
Quoted 72 lines
util/qemu-option.c +39
-1bool qemu_opt_has_help_opt (QemuOpts *opts) {QemuOpt *opt;@@ -577,6 +589,22 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)return opt->value.boolean; } +bool qemu_opt_get_bool_del (QemuOpts *opts,const char *name,bool defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +bool ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.boolean; +assert (opt->desc && opt->desc->type ==QEMU_OPT_BOOL ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +uint64_t qemu_opt_get_number (QemuOpts *opts,const char *name,uint64_t defval) {QemuOpt *opt =qemu_opt_find (opts, name);@@ -609,6 +637,23 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)return opt->value.uint; } +uint64_t qemu_opt_get_size_del (QemuOpts *opts,const char *name, +uint64_t defval) +{ +QemuOpt *opt =qemu_opt_find (opts, name); +uint64_t ret; + +if (opt ==NULL ) { +return defval; + } + ret = opt->value.uint; +assert (opt->desc && opt->desc->type ==QEMU_OPT_SIZE ); +if (opt) { +qemu_opt_del (opt); + } +return ret; +} +static void qemu_opt_parse (QemuOpt *opt,Error **errp) {if (opt->desc ==NULL )@@ -646,7 +691,7 @@ static bool opts_accepts_any(const QemuOpts *opts) }static void opt_set (QemuOpts *opts,const char *name,const char *value, -bool prepend,Error **errp) +bool prepend,bool replace,Error **errp) {QemuOpt *opt;const QemuOptDesc *desc;@@ -658,6 +703,11 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,return ; } + opt =qemu_opt_find (opts, name); +if (replace && opt) { +qemu_opt_del (opt); + } + opt =g_malloc0 (sizeof (*opt)); opt->name =g_strdup (name); opt->opts = opts;util/qemu-option.c static void opt_set(QemuOpts *opts, const char *name, const char *value, · line 722
Quoted 7 lines
util/qemu-option.c +0
-1@@ -667,7 +717,6 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,QTAILQ_INSERT_TAIL (&opts->head, opt, next); } opt->desc = desc; - opt->str =g_strdup (value); opt->str =g_strdup (value ?: desc->def_value_str);qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {Here you plug the leak you created in PATCH 1/6.
Quoted 21 lines
util/qemu-option.c +16
-1@@ -680,7 +729,29 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) {Error *local_err =NULL ; -opt_set (opts, name, value, false, &local_err); +opt_set (opts, name, value, false, false, &local_err); +if (error_is_set (&local_err)) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +return 0 ; +} + +/* +* If name exists, the function will delete the opt first and then add a new +* one. +*/ +int qemu_opt_replace_set (QemuOpts *opts,const char *name,const char *value) +{Quoted 9 lines
util/qemu-option.c int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) · line 753
Quoted 6 lines
util/qemu-option.c +6
-0+Error *local_err =NULL ; +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + }Why? Won't
opt_set()delete it already?Quoted 1 line
No,
opt_setwill not delete it, but add a new opt. In block layer,Then I'm confused...
Quoted 3 lines
while parsing options, if the parameter is parsed, it should be deleted and won't be used again, so I delete it manually.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Same question for the
qemu_opt_replace_set_FOO()that follow.Quoted 1 line
util/qemu-option.c +1
-0+opt_set (opts, name, value, false, true, &local_err);... because here you pass true for parameter replace, which I (naively?) expect to delete the option. Can you unconfuse me?
Sorry, I used qemu_opt_del twice, will clean.
Quoted 76 lines
Quoted 73 lines
Quoted 73 lines
Quoted 71 lines
util/qemu-option.c +21
-5if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -693,7 +764,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)void qemu_opt_set_err (QemuOpts *opts,const char *name,const char *value,Error **errp) { -opt_set (opts, name, value, false, errp); +opt_set (opts, name, value, false, false, errp); }int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val)@@ -740,6 +811,16 @@ int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)return 0 ; } +int qemu_opt_replace_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +QemuOpt *opt =qemu_opt_find (opts, name); + +if (opt) { +qemu_opt_del (opt); + } +return qemu_opt_set_number (opts, name, val); +} +int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure) {@@ -917,7 +998,7 @@ int qemu_opts_print(QemuOpts *opts) }static int opts_do_parse (QemuOpts *opts,const char *params, -const char *firstname,bool prepend) +const char *firstname,bool prepend,bool replace) {char option[128 ], value[1024 ];const char *p,*pe,*pc;@@ -953,7 +1034,7 @@ static int opts_do_parse(QemuOpts *opts, const char *params, }if (strcmp (option,"id" ) !=0 ) {/* store and parse */ -opt_set (opts, option, value, prepend, &local_err); +opt_set (opts, option, value, prepend, replace, &local_err);if (error_is_set (&local_err)) {qerror_report_err (local_err);error_free (local_err);@@ -969,7 +1050,13 @@ static int opts_do_parse(QemuOpts *opts, const char *params,int qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname) { -return opts_do_parse (opts, params, firstname, false); +return opts_do_parse (opts, params, firstname, false, false); +} + +int qemu_opts_do_parse_replace (QemuOpts *opts,const char *params, +const char *firstname) +{ +return opts_do_parse (opts, params, firstname, false, true); } static QemuOpts *opts_parse(QemuOptsList *list, const char *params,@@ -1008,7 +1095,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,return NULL ; } -if (opts_do_parse (opts, params, firstname, defaults) !=0 ) { +if (opts_do_parse (opts, params, firstname, defaults, false) !=0 ) {qemu_opts_del (opts);return NULL ; }