replace QEMUOptionParameter with QemuOpts parser
v2 cover replace QEMUOptionParameter with QemuOpts parser
This is still a RFC, I cut some block image formats to simplify the exploring.
patch 1-3 are from Luiz, added Markus's comments, discussion could be found here:
http://lists.nongnu.org/archive/html/qemu-devel/2012-07/msg02716.html
patch 4-5: because qemu_opts_create can not fail while id is null, so create function qemu_opts_create_nofail and use it.
patch 6: create function qemu_opt_set_number, like qemu_opt_set_bool.
patch 7: remove QEMUOptionParameter and use QemuOpts parser.
v1->v2:
1) add Luiz's patches.
2) create qemu_opt_set_number() and qemu_opts_create_nofail() functions.
3) add QemuOptsList map to drivers.
4) use original opts parser, not creating new ones.
5) fix other bugs.
Dong Xu Wang (7):
qemu-option: qemu_opt_set_bool(): fix code duplication
qemu-option: opt_set(): split it up into more functions
qemu-option: qemu_opts_validate(): fix duplicated code introduce qemu_opts_create_nofail function
use qemu_opts_create_nofail
create new function: qemu_opt_set_number remove QEMUOptionParameter
block.c | 88 +++++----- block.h | 5 +- block/Makefile.objs | 9 +- block/qcow2.c | 175 ++++++++++--------- block/raw-posix.c | 68 ++++---- block/raw.c | 31 ++-- block_int.h | 6 +- blockdev.c | 2 +- hw/watchdog.c | 2 +- qemu-config.c | 7 +- qemu-img.c | 60 +++---- qemu-option.c | 501 ++++++++++++++++----------------------------------- qemu-option.h | 47 +---- qemu-sockets.c | 8 +- vl.c | 6 +- 15 files changed, 401 insertions(+), 614 deletions(-)
RFC v2 1/7 qemu-option: qemu_opt_set_bool(): fix code duplication
Call qemu_opt_set() instead of duplicating opt_set().
It will set opt->str in qemu_opt_set_bool, without opt->str, there will be some potential bugs.
These are uses of opt->str, and what happens when it isn't set:
* qemu_opt_get(): returns NULL, which means "not set". Bug can bite when value isn't the default value.
* qemu_opt_parse(): passes NULL to parse_option_bool(), which treats it like "on". Wrong if the value is actually false. Bug can bite when qemu_opts_validate() runs after qemu_opt_set_bool().
* qemu_opt_del(): passes NULL to g_free(), which is just fine.
* qemu_opt_foreach(): passes NULL to the callback, which is unlikely to be prepared for it.
* qemu_opts_print(): prints NULL, which crashes on some systems.
* qemu_opts_to_qdict(): passes NULL to qstring_from_str(), which crashes.
--- qemu-option.c | 28 +--------------------------- 1 files changed, 1 insertions(+), 27 deletions(-)
qemu-option.c +1 -27
@@ -667,33 +667,7 @@ void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val) { -QemuOpt *opt; -const QemuOptDesc *desc = opts->list->desc; -int i; - -for (i =0 ; desc[i].name !=NULL ; i++) { -if (strcmp (desc[i].name, name) ==0 ) { -break ; - } - } -if (desc[i].name ==NULL ) { -if (i ==0 ) { -/* empty list -> allow any */ ; - }else { -qerror_report (QERR_INVALID_PARAMETER , name); -return -1 ; - } - } - - opt =g_malloc0 (sizeof (*opt)); - opt->name =g_strdup (name); - opt->opts = opts; -QTAILQ_INSERT_TAIL (&opts->head, opt, next); -if (desc[i].name !=NULL ) { - opt->desc = desc+i; - } - opt->value.boolean = !!val; -return 0 ; +return qemu_opt_set (opts, name, val ?"on" :"off" ); } int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
Signature · 1 line
1.7.1
Re: [Qemu-devel] [RFC v2 1/7] qemu-option: qemu_opt_set_bool(): fix code duplication
Il 27/09/2012 07:14, Dong Xu Wang ha scritto:
Quoted 56 lines
Call
qemu_opt_set()instead of duplicatingopt_set().It will set
opt->strinqemu_opt_set_bool, withoutopt->str, there will be some potential bugs.These are uses of
opt->str, and what happens when it isn't set:*
qemu_opt_get(): returns NULL, which means "not set". Bug can bite when value isn't the default value.*
qemu_opt_parse(): passes NULL toparse_option_bool(), which treats it like "on". Wrong if the value is actually false. Bug can bite whenqemu_opts_validate()runs afterqemu_opt_set_bool().*
qemu_opt_del(): passes NULL tog_free(), which is just fine.*
qemu_opt_foreach(): passes NULL to the callback, which is unlikely to be prepared for it.*
qemu_opts_print(): prints NULL, which crashes on some systems.*
qemu_opts_to_qdict(): passes NULL toqstring_from_str(), which crashes.Signed-off-by Luiz Capitulino <[email protected]>Signed-off-by Dong Xu Wang <[email protected]>--- qemu-option.c | 28 +--------------------------- 1 files changed, 1 insertions(+), 27 deletions(-)qemu-option.c +0
-18@@ -667,33 +667,7 @@ void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,int qemu_opt_set_bool (QemuOpts *opts,const char *name,bool val) { -QemuOpt *opt; -const QemuOptDesc *desc = opts->list->desc; -int i; - -for (i =0 ; desc[i].name !=NULL ; i++) { -if (strcmp (desc[i].name, name) ==0 ) { -break ; - } - } -if (desc[i].name ==NULL ) { -if (i ==0 ) { -/* empty list -> allow any */ ; - }else { -qerror_report (QERR_INVALID_PARAMETER , name); -return -1 ; - } - } -
qemu-option.c void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value, · line 670
Quoted 10 lines
qemu-option.c +1
-9- opt =g_malloc0 (sizeof (*opt)); - opt->name =g_strdup (name); - opt->opts = opts; -QTAILQ_INSERT_TAIL (&opts->head, opt, next); -if (desc[i].name !=NULL ) { - opt->desc = desc+i; - } - opt->value.boolean = !!val; -return 0 ; +return qemu_opt_set (opts, name, val ?"on" :"off" );
This now calls qemu_opt_parse, which won't work if you have opt->desc = NULL.
Instead of this patch, using the new functions you create in patch 2 should be enough to limit the code duplication in qemu_opt_set_bool.
Paolo
Quoted 4 lines
qemu-option.c +0
-0} int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
RFC v2 2/7 qemu-option: opt_set(): split it up into more functions
In reply to Dong Xu WangThe new functions are opts_accepts_any() and find_desc_by_name(), which are also going to be used by qemu_opts_validate() (see next commit).
This also makes opt_set() slightly more readable.
--- qemu-option.c | 40 ++++++++++++++++++++++++---------------- 1 files changed, 24 insertions(+), 16 deletions(-)
qemu-option.c +24 -16
@@ -602,26 +602,36 @@ static void qemu_opt_del(QemuOpt *opt)g_free (opt); } -static void opt_set (QemuOpts *opts,const char *name,const char *value, -bool prepend,Error **errp) +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) { -QemuOpt *opt; -const QemuOptDesc *desc = opts->list->desc; -Error *local_err =NULL ;int i;for (i =0 ; desc[i].name !=NULL ; i++) {if (strcmp (desc[i].name, name) ==0 ) { -break ; +return &desc[i]; } } -if (desc[i].name ==NULL ) { -if (i ==0 ) { -/* empty list -> allow any */ ; - }else { -error_set (errp,QERR_INVALID_PARAMETER , name); -return ; - } + +return NULL ; +} + +static void opt_set (QemuOpts *opts,const char *name,const char *value, +bool prepend,Error **errp) +{ +QemuOpt *opt; +const QemuOptDesc *desc; +Error *local_err =NULL ; + + desc =find_desc_by_name (opts->list->desc, name); +if (!desc && !opts_accepts_any (opts)) { +error_set (errp,QERR_INVALID_PARAMETER , name); +return ; } opt =g_malloc0 (sizeof (*opt));
@@ -632,9 +642,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value, }else {QTAILQ_INSERT_TAIL (&opts->head, opt, next); } -if (desc[i].name !=NULL ) { - opt->desc = desc+i; - } + opt->desc = desc;if (value) { opt->str =g_strdup (value); }
Signature · 1 line
1.7.1
RFC v2 3/7 qemu-option: qemu_opts_validate(): fix duplicated code
In reply to Dong Xu WangUse opts_accepts_any() and find_desc_by_name().
--- qemu-option.c | 14 +++----------- 1 files changed, 3 insertions(+), 11 deletions(-)
qemu-option.c +3 -11
@@ -1050,23 +1050,15 @@ void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)QemuOpt *opt;Error *local_err =NULL ; -assert (opts->list->desc[0 ].name ==NULL ); +assert (opts_accepts_any (opts)); QTAILQ_FOREACH(opt, &opts->head, next) { -int i; - -for (i =0 ; desc[i].name !=NULL ; i++) { -if (strcmp (desc[i].name, opt->name) ==0 ) { -break ; - } - } -if (desc[i].name ==NULL ) { + opt->desc =find_desc_by_name (desc, opt->name); +if (!opt->desc) {error_set (errp,QERR_INVALID_PARAMETER , opt->name);return ; } - opt->desc = &desc[i]; -qemu_opt_parse (opt, &local_err);if (error_is_set (&local_err)) {error_propagate (errp, local_err);
Signature · 1 line
1.7.1
RFC v2 4/7 introduce qemu_opts_create_nofail function
In reply to Dong Xu WangWhile id is NULL, qemu_opts_create can not fail, so ignore errors is fine.
--- qemu-option.c | 5 +++++ qemu-option.h | 1 + 2 files changed, 6 insertions(+), 0 deletions(-)
qemu-option.c +5 -0
@@ -763,6 +763,11 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,return opts; } +QemuOpts *qemu_opts_create_nofail (QemuOptsList *list) +{ +return qemu_opts_create (list,NULL ,0 ,NULL ); +} +void qemu_opts_reset (QemuOptsList *list) {QemuOpts *opts, *next_opts;
qemu-option.h +1 -0
@@ -133,6 +133,7 @@ int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,QemuOpts *qemu_opts_find (QemuOptsList *list,const char *id);QemuOpts *qemu_opts_create (QemuOptsList *list,const char *id,int fail_if_exists,Error **errp); +QemuOpts *qemu_opts_create_nofail (QemuOptsList *list);void qemu_opts_reset (QemuOptsList *list);void qemu_opts_loc_restore (QemuOpts *opts); int qemu_opts_set(QemuOptsList *list, const char *id,
Signature · 1 line
1.7.1
RFC v2 5/7 use qemu_opts_create_nofail
In reply to Dong Xu Wanguse qemu_opts_create_nofail while id is NULL.
--- blockdev.c | 2 +- hw/watchdog.c | 2 +- qemu-config.c | 4 ++-- qemu-img.c | 2 +- qemu-sockets.c | 8 ++++---- vl.c | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-)
blockdev.c +1 -1
@@ -559,7 +559,7 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)break ;case IF_VIRTIO :/* add virtio block device */ - opts =qemu_opts_create (qemu_find_opts ("device" ),NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (qemu_find_opts ("device" ));if (arch_type ==QEMU_ARCH_S390X ) {qemu_opt_set (opts,"driver" ,"virtio-blk-s390" ); }else {
hw/watchdog.c +1 -1
@@ -66,7 +66,7 @@ int select_watchdog(const char *p) QLIST_FOREACH(model, &watchdog_list, entry) {if (strcasecmp (model->wdt_name, p) ==0 ) {/* add the device */ - opts =qemu_opts_create (qemu_find_opts ("device" ),NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (qemu_find_opts ("device" ));qemu_opt_set (opts,"driver" , p);return 0 ; }
qemu-config.c +2 -2
@@ -762,7 +762,7 @@ int qemu_global_option(const char *str)return -1 ; } - opts =qemu_opts_create (&qemu_global_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (&qemu_global_opts);qemu_opt_set (opts,"driver" , driver);qemu_opt_set (opts,"property" , property);qemu_opt_set (opts,"value" , str+offset+1 );
@@ -849,7 +849,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)error_free (local_err); goto out; } - opts =qemu_opts_create (list,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (list);continue ; }if (sscanf (line," %63s = \"%1023[^\"]\"" , arg, value) ==2 ) {
qemu-img.c +1 -1
@@ -1778,7 +1778,7 @@ static int img_resize(int argc, char **argv) }/* Parse size */ - param =qemu_opts_create (&resize_options,NULL ,0 ,NULL ); + param =qemu_opts_create_nofail (&resize_options);if (qemu_opt_set (param,BLOCK_OPT_SIZE , size)) {/* Error message already printed when size parsing fails */ ret =-1 ;
qemu-sockets.c +4 -4
@@ -469,7 +469,7 @@ int inet_listen(const char *str, char *ostr, int olen,char *optstr;int sock =-1 ; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (&dummy_opts);if (inet_parse (opts, str) ==0 ) { sock =inet_listen_opts (opts, port_offset, errp);if (sock !=-1 && ostr) {
@@ -498,7 +498,7 @@ int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)QemuOpts *opts;int sock =-1 ; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (&dummy_opts);if (inet_parse (opts, str) ==0 ) {if (block) {qemu_opt_set (opts,"block" ,"on" );
@@ -597,7 +597,7 @@ int unix_listen(const char *str, char *ostr, int olen)char *path, *optstr;int sock, len; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (&dummy_opts); optstr =strchr (str,',' );if (optstr) {
@@ -625,7 +625,7 @@ int unix_connect(const char *path)QemuOpts *opts;int sock; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (&dummy_opts);qemu_opt_set (opts,"path" , path); sock =unix_connect_opts (opts);qemu_opts_del (opts);
vl.c +3 -3
@@ -1863,7 +1863,7 @@ static int balloon_parse(const char *arg)return -1 ; }else {/* create empty opts */ - opts =qemu_opts_create (qemu_find_opts ("device" ),NULL ,0 ,NULL ); + opts =qemu_opts_create_nofail (qemu_find_opts ("device" )); }qemu_opt_set (opts,"driver" ,"virtio-balloon" );return 0 ;
@@ -2111,14 +2111,14 @@ static int virtcon_parse(const char *devname)exit (1 ); } - bus_opts =qemu_opts_create (device,NULL ,0 ,NULL ); + bus_opts =qemu_opts_create_nofail (device);if (arch_type ==QEMU_ARCH_S390X ) {qemu_opt_set (bus_opts,"driver" ,"virtio-serial-s390" ); }else {qemu_opt_set (bus_opts,"driver" ,"virtio-serial-pci" ); } - dev_opts =qemu_opts_create (device,NULL ,0 ,NULL ); + dev_opts =qemu_opts_create_nofail (device);qemu_opt_set (dev_opts,"driver" ,"virtconsole" );snprintf (label,sizeof (label),"virtcon%d" , index);
Signature · 1 line
1.7.1
RFC v2 6/7 create new function: qemu_opt_set_number
In reply to Dong Xu Wang--- qemu-option.c | 8 ++++++++ qemu-option.h | 1 + 2 files changed, 9 insertions(+), 0 deletions(-)
qemu-option.c +8 -0
@@ -678,6 +678,14 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)return qemu_opt_set (opts, name, val ?"on" :"off" ); } +int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +char buffer[1024 ]; +snprintf (buffer,sizeof (buffer),"%" PRId64, val); +return qemu_opt_set (opts, name, buffer); +} + +int qemu_opt_foreach (QemuOpts *opts,qemu_opt_loopfunc func,void *opaque,int abort_on_failure) {
qemu-option.h +1 -0
@@ -126,6 +126,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);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);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);
Signature · 1 line
1.7.1
Re: [Qemu-devel] [RFC v2 6/7] create new function: qemu_opt_set_number
Il 27/09/2012 07:14, Dong Xu Wang ha scritto:
qemu-option.c int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) · line 686
Quoted 6 lines
qemu-option.c +6
-0+int qemu_opt_set_number (QemuOpts *opts,const char *name,int64_t val) +{ +char buffer[1024 ]; +snprintf (buffer,sizeof (buffer),"%" PRId64, val); +return qemu_opt_set (opts, name, buffer); +}
This has the same problem as qemu_opt_set_bool after patch 1.
Paolo
RFC v2 7/7 remove QEMUOptionParameter
In reply to Dong Xu Wangremove QEMUOptionParameter, and use QemuOpts and QemuOptsList.
--- block.c | 88 ++++++------ block.h | 5 +- block/Makefile.objs | 9 +- block/qcow2.c | 175 ++++++++++++----------- block/raw-posix.c | 68 ++++----- block/raw.c | 31 +++-- block_int.h | 6 +- qemu-config.c | 3 + qemu-img.c | 58 ++++---- qemu-option.c | 408 +++++++++++++++------------------------------------ qemu-option.h | 45 +----- 11 files changed, 347 insertions(+), 549 deletions(-)
block.c +43 -45
@@ -351,7 +351,7 @@ BlockDriver *bdrv_find_whitelisted_format(const char *format_name)typedef struct CreateCo {BlockDriver *drv;char *filename; -QEMUOptionParameter *options; +QemuOpts *opts;int ret; }CreateCo ;
@@ -360,11 +360,11 @@ 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) +QemuOpts *opts) {int ret;
@@ -372,7 +372,7 @@ int bdrv_create(BlockDriver *drv, const char* filename,CreateCo cco = { .drv = drv, .filename =g_strdup (filename), - .options = options, + .opts = opts, .ret =NOT_DONE , };
@@ -397,7 +397,7 @@ int bdrv_create(BlockDriver *drv, const char* filename,return ret; } -int bdrv_create_file (const char * filename,QEMUOptionParameter *options) +int bdrv_create_file (const char *filename,QemuOpts *opts) {BlockDriver *drv;
@@ -406,7 +406,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options)return -ENOENT ; } -return bdrv_create (drv, filename, options); +return bdrv_create (drv, filename, opts); }/*
@@ -746,7 +746,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,int64_t total_size;int is_protocol =0 ;BlockDriver *bdrv_qcow2; -QEMUOptionParameter *options; +QemuOpts *opts;char backing_filename[PATH_MAX ];/* if snapshot, we create a temporary backing file and open it
@@ -779,17 +779,18 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,return -errno; bdrv_qcow2 =bdrv_find_format ("qcow2" ); - options =parse_option_parameters ("" , bdrv_qcow2->create_options,NULL ); + opts =qemu_opts_create (qemu_find_opts ("qcow2-create-opts" ), +NULL ,0 ,NULL ); -set_option_parameter_int (options,BLOCK_OPT_SIZE , total_size); -set_option_parameter (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 (options,BLOCK_OPT_BACKING_FMT , +qemu_opt_set (opts,BLOCK_OPT_BACKING_FMT , drv->format_name); } - ret =bdrv_create (bdrv_qcow2, tmp_filename, options); -free_option_parameters (options); + ret =bdrv_create (bdrv_qcow2, tmp_filename, opts); +qemu_opts_del (opts);if (ret <0 ) {return ret; }
@@ -3905,8 +3906,10 @@ int bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt, char *options, uint64_t img_size, int flags) { - QEMUOptionParameter *param = NULL, *create_options = NULL; -QEMUOptionParameter *backing_fmt, *backing_file, *size; + QemuOpts *param = NULL; +QemuOptsList *create_options =NULL ; +const char *backing_fmt, *backing_file; +int64_t size;BlockDriverState *bs =NULL ;BlockDriver *drv, *proto_drv;BlockDriver *backing_drv =NULL ;
@@ -3926,21 +3929,16 @@ int bdrv_img_create(const char *filename, const char *fmt, ret = -EINVAL ; goto out; } - - create_options =append_option_parameters (create_options, - drv->create_options); - create_options =append_option_parameters (create_options, - proto_drv->create_options); - + create_options =append_opts_list (drv->bdrv_create_options (), + proto_drv->bdrv_create_options ());/* Create parameter list with default values */ - param =parse_option_parameters ("" , create_options, param); + param =qemu_opts_create (create_options,NULL ,0 ,NULL ); -set_option_parameter_int (param,BLOCK_OPT_SIZE , img_size); +qemu_opt_set_number (param,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 (param, options,NULL ) !=0 ) {error_report ("Invalid options for file format '%s'." , fmt); ret = -EINVAL ; goto out;
@@ -3948,7 +3946,7 @@ int bdrv_img_create(const char *filename, const char *fmt, }if (base_filename) { -if (set_option_parameter (param,BLOCK_OPT_BACKING_FILE , +if (qemu_opt_set (param,BLOCK_OPT_BACKING_FILE , base_filename)) {error_report ("Backing file not supported for file format '%s'" , fmt);
@@ -3958,7 +3956,7 @@ int 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_set (param,BLOCK_OPT_BACKING_FMT , base_fmt)) {error_report ("Backing file format not supported for file " "format '%s'" , fmt); ret = -EINVAL ;
@@ -3966,9 +3964,9 @@ int bdrv_img_create(const char *filename, const char *fmt, } } - 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 (param,BLOCK_OPT_BACKING_FILE ); +if (backing_file) { +if (!strcmp (filename, backing_file)) {error_report ("Error: Trying to create an image with the " "same filename as the backing file" ); ret = -EINVAL ;
@@ -3976,12 +3974,12 @@ int bdrv_img_create(const char *filename, const char *fmt, } } - 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 (param,BLOCK_OPT_BACKING_FMT ); +if (backing_fmt) { + backing_drv =bdrv_find_format (backing_fmt);if (!backing_drv) {error_report ("Unknown backing file format '%s'" , - backing_fmt->value.s); + backing_fmt); ret = -EINVAL ; goto out; }
@@ -3989,11 +3987,10 @@ int bdrv_img_create(const char *filename, const char *fmt,// 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_number (param,BLOCK_OPT_SIZE ,-1 ); +if (size ==-1 ) { +if (backing_file) {uint64_t size; -char buf[32 ];int back_flags;/* backing files always opened read-only */
@@ -4002,16 +3999,15 @@ int bdrv_img_create(const char *filename, const char *fmt, bs =bdrv_new ("" ); - ret =bdrv_open (bs, backing_file->value.s, back_flags, backing_drv); + ret =bdrv_open (bs, backing_file, back_flags, backing_drv);if (ret <0 ) { -error_report ("Could not open '%s'" , backing_file->value.s); +error_report ("Could not open '%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_set_number (param,BLOCK_OPT_SIZE , size); }else {error_report ("Image creation needs a size parameter" ); ret = -EINVAL ;
@@ -4020,7 +4016,7 @@ int bdrv_img_create(const char *filename, const char *fmt, }printf ("Formatting '%s', fmt=%s " , filename, fmt); -print_option_parameters (param); +qemu_opts_print (param,NULL );puts ("" ); ret =bdrv_create (drv, filename, param);
@@ -4039,8 +4035,10 @@ int bdrv_img_create(const char *filename, const char *fmt, } out: -free_option_parameters (create_options); -free_option_parameters (param); +free_opts_list (create_options); +if (param) { +qemu_opts_del (param); + }if (bs) {bdrv_delete (bs);
block.h +2 -3
@@ -119,8 +119,8 @@ 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); +QemuOpts *options); +int bdrv_create_file (const char *filename,QemuOpts *options);BlockDriverState *bdrv_new (const char *device_name);void bdrv_make_anon (BlockDriverState *bs);void bdrv_swap (BlockDriverState *bs_new,BlockDriverState *bs_old);
@@ -404,5 +404,4 @@ typedef enum {#define BLKDBG_EVENT (bs, evt) bdrv_debug_event(bs, evt)void bdrv_debug_event (BlockDriverState *bs,BlkDebugEvent event); -#endif
block/Makefile.objs +5 -4
@@ -1,8 +1,9 @@ -block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o +#block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o -block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o -block-obj-y += qed-check.o -block-obj-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o +#block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o +#block-obj-y += qed-check.o +#block-obj-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o +block-obj-y += raw.o block-obj-y += stream.o block-obj-$(CONFIG_WIN32) += raw-win32.o block-obj-$(CONFIG_POSIX) += raw-posix.o
block/qcow2.c +91 -84
222 lines not on this page · raw message
block/raw-posix.c +33 -35
156 lines not on this page · raw message
block/raw.c +20 -11
@@ -87,25 +87,34 @@ 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) +static int raw_create (const char *filename,QemuOpts *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_has_zero_init (BlockDriverState *bs) {return bdrv_has_zero_init (bs->file); } +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_NUMBER , + .help ="Virtual disk size" + }, + {/* end of list */ } + } +}; + +static QemuOptsList *raw_create_options (void ) +{ +return &raw_create_opts; +} +static BlockDriver bdrv_raw = { .format_name ="raw" ,
@@ -133,8 +142,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_options = raw_create_options, };static void bdrv_raw_init (void )
block_int.h +2 -4
@@ -147,7 +147,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 *options);int (*bdrv_set_key)(BlockDriverState *bs,const char *key);int (*bdrv_make_empty)(BlockDriverState *bs);/* aio */
@@ -236,9 +236,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_options)(void);/* * Returns 0 for completed check, -errno for internal errors.
qemu-config.c +3 -0
@@ -5,6 +5,8 @@#include "hw/qdev.h" #include "error.h" +/*******************************************************************/ +static QemuOptsList qemu_drive_opts = { .name ="drive" , .head =QTAILQ_HEAD_INITIALIZER (qemu_drive_opts.head),
@@ -666,6 +668,7 @@ static QemuOptsList *vm_config_groups[32] = { &qemu_boot_opts, &qemu_iscsi_opts, &qemu_sandbox_opts, + NULL, };
qemu-img.c +26 -32
135 lines not on this page · raw message
qemu-option.c +117 -291
431 lines not on this page · raw message
qemu-option.h +5 -40
67 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [Qemu-devel] [RFC v2 7/7] remove QEMUOptionParameter
Il 27/09/2012 07:14, Dong Xu Wang ha scritto:
Quoted 16 lines
remove QEMUOptionParameter, and use QemuOpts and QemuOptsList.
Signed-off-by Dong Xu Wang <[email protected]>--- block.c | 88 ++++++------ block.h | 5 +- block/Makefile.objs | 9 +- block/qcow2.c | 175 ++++++++++++----------- block/raw-posix.c | 68 ++++----- block/raw.c | 31 +++-- block_int.h | 6 +- qemu-config.c | 3 + qemu-img.c | 58 ++++---- qemu-option.c | 408 +++++++++++++++------------------------------------ qemu-option.h | 45 +----- 11 files changed, 347 insertions(+), 549 deletions(-)
The patch is already quite big, so please move the qemu-option.c changes to separate patches.
For example, patch 7 could add def_value and use it in qemu_opts_print.
Patch 8 should add append_opts_list, free_opts_list, print_opts_list.
Patch 9 should touch only the block layer. Patch 10 should remove the now-unuse QEMUOptionParameter code.
(Regarding def_value, it is quite unintuitive that you need to specify the value again when calling qemu_opt_get_*. Perhaps, qemu_opts_validate could instead walk through descriptors that are not present but have a default value, and add new options with the default value to the QemuOpts object).
Paolo
Re: [Qemu-devel] [RFC v2 7/7] remove QEMUOptionParameter
Quoted 26 lines · Paolo Bonzini
Il 27/09/2012 07:14, Dong Xu Wang ha scritto:
Quoted 16 lines
remove QEMUOptionParameter, and use QemuOpts and QemuOptsList.
Signed-off-by Dong Xu Wang <[email protected]>--- block.c | 88 ++++++------ block.h | 5 +- block/Makefile.objs | 9 +- block/qcow2.c | 175 ++++++++++++----------- block/raw-posix.c | 68 ++++----- block/raw.c | 31 +++-- block_int.h | 6 +- qemu-config.c | 3 + qemu-img.c | 58 ++++---- qemu-option.c | 408 +++++++++++++++------------------------------------ qemu-option.h | 45 +----- 11 files changed, 347 insertions(+), 549 deletions(-)The patch is already quite big, so please move the
qemu-option.cchanges to separate patches.For example, patch 7 could add
def_valueand use it inqemu_opts_print.
Patch 8 should addappend_opts_list,free_opts_list,print_opts_list.
Patch 9 should touch only the block layer. Patch 10 should remove the now-unuse QEMUOptionParameter code.
Quoted 9 lines
(Regarding
def_value, it is quite unintuitive that you need to specify the value again when calling qemu_opt_get_*. Perhaps,qemu_opts_validatecould instead walk through descriptors that are not present but have a default value, and add new options with the default value to the QemuOpts object).Paolo
Thank you Paolo, I will take a week's vocation, so I will fix it once I am back.