replace QEMUOptionParameter with QemuOpts parser
v9 cover replace QEMUOptionParameter with QemuOpts parser
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 3 was changed according Paolo's comments.
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: add def_value and use it in qemu_opts_print.
Patch 8: Create functions to pair with QEMUOptionParameter parser.
Patch 9: Use QemuOpts parser in Block.
Patch 10: Remove QEMUOptionParameter parser related code.
Patches 1 - 6 have been merged into block branch, so only patches 8 to 10 are included.
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) 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.
3) rebase to upstream source tree.
4) add gluster.c, raw-win32.c, and rbd.c.
v6->v7:
1) Fix typo: enouth->enough.
2) use osdep.h:stringify(), not redefining new macro.
3) preserve TODO comment.
4) fix typo: BLOCK_OPT_ENCRYPT->BLOCK_OPT_STATIC.
5) initialize disk_type even when opts is NULL.
v5->v6:
1) allocate enough space in append_opts_list function.
2) judge if opts == NULL in block layer create functions.
3) use bdrv_create_file(filename, NULL) in qcow_create funtion.
4) made more readable while using qemu_opt_get_number funtion.
v4->v5:
1) Rewrite qemu_opts_create_nofail function based on Peter Maydell's comments.
2) Use g_strdup_printf in qemu_opt_set_number.
3) Rewrite qemu_opts_print.
4) .bdrv_create_options returns pointer directly. Fix a bug about "encryption".
5) Check qemu_opt_get_number in raw-posix.c.
v3->v4:
1) Rebased to the newest source tree.
2) Remove redundant "#include "block-cache.h"
3) Other small changes.
v2->v3:
1) rewrite qemu_opt_set_bool and qemu_opt_set_number according Paolo's coments.
2) split patches to make review easier.
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 (4):
add def_print_str and use it in qemu_opts_print. Create four opts list related functions
Use QemuOpts support in block layer
remove QEMUOptionParameter related functions and struct
block.c | 91 ++++++------- block.h | 4 +- block/cow.c | 46 +++--- block/gluster.c | 37 +++--- block/qcow.c | 60 ++++---- block/qcow2.c | 171 ++++++++++++----------- block/qed.c | 86 ++++++------ block/raw-posix.c | 65 ++++----- block/raw-win32.c | 30 ++-- block/raw.c | 30 +++-- block/rbd.c | 63 ++++---- block/sheepdog.c | 75 +++++----- block/vdi.c | 69 +++++----- block/vmdk.c | 74 +++++----- block/vpc.c | 67 +++++---- block/vvfat.c | 11 +- block_int.h | 6 +- qemu-img.c | 61 ++++---- qemu-option.c | 406 +++++++++++++++-------------------------------------- qemu-option.h | 37 +---- 20 files changed, 641 insertions(+), 848 deletions(-)
PATCH v9 1/4 add def_print_str and use it in 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.
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.
--- qemu-option.c | 31 ++++++++++++++++++++++++------- qemu-option.h | 1 + 2 files changed, 25 insertions(+), 7 deletions(-)
qemu-option.c +24 -7
@@ -862,15 +862,32 @@ void qemu_opts_del(QemuOpts *opts)int qemu_opts_print (QemuOpts *opts,void *dummy) { -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 ) { +printf ("no elements => accept any params" ); +return 0 ; } -fprintf (stderr,"\n" ); -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); + } + } +return 0 ; } static int opts_do_parse(QemuOpts *opts, const char *params,
qemu-option.h +1 -0
@@ -96,6 +96,7 @@ typedef struct QemuOptDesc {const char *name;enum QemuOptType type;const char *help; +const char *def_value_str; } QemuOptDesc;struct QemuOptsList {
Signature · 1 line
1.7.1
PATCH v9 2/4 Create four opts list related functions
In reply to Dong Xu WangThis patch will create 4 functions, count_opts_list, append_opts_list, free_opts_list and print_opts_list, they will used in following commits.
v6->v7):
1) Fix typo.
v5->v6):
1) allocate enough space in append_opts_list function.
--- qemu-option.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-option.h | 4 ++ 2 files changed, 94 insertions(+), 0 deletions(-)
qemu-option.c +90 -0
@@ -1149,3 +1149,93 @@ int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,loc_pop (&loc);return rc; } + +static size_t count_opts_list (QemuOptsList *list) +{ +size_t i =0 ; + +while (list && list->desc[i].name) { + i++; + } + +return i; +} + +/* Create a new QemuOptsList and make its desc to the merge of first and second. +* It will allocate space for one new QemuOptsList plus enough space for +* QemuOptDesc in first and second QemuOptsList. First argument's QemuOptDesc +* members take precedence over second's. +*/ +QemuOptsList *append_opts_list (QemuOptsList *first, +QemuOptsList *second) +{ +size_t num_first_options, num_second_options; +QemuOptsList *dest =NULL ; +int i =0 ; +int index =0 ; + + num_first_options =count_opts_list (first); + num_second_options =count_opts_list (second); +if (num_first_options + num_second_options ==0 ) { +return NULL ; + } + + dest =g_malloc0 (sizeof (QemuOptsList) + + (num_first_options + num_second_options +1 ) *sizeof (QemuOptDesc)); + + dest->name ="append_opts_list" ; + dest->implied_opt_name =NULL ; + dest->merge_lists = false; +QTAILQ_INIT (&dest->head); +while (first && (first->desc[i].name)) { +if (!find_desc_by_name (dest->desc, first->desc[i].name)) { + dest->desc[index].name =g_strdup (first->desc[i].name); + dest->desc[index].help =g_strdup (first->desc[i].help); + dest->desc[index].type = first->desc[i].type; + dest->desc[index].def_value_str = +g_strdup (first->desc[i].def_value_str); + ++index; + } + i++; + } + i =0 ; +while (second && (second->desc[i].name)) { +if (!find_desc_by_name (dest->desc, second->desc[i].name)) { + dest->desc[index].name =g_strdup (first->desc[i].name); + dest->desc[index].help =g_strdup (first->desc[i].help); + dest->desc[index].type = second->desc[i].type; + dest->desc[index].def_value_str = +g_strdup (second->desc[i].def_value_str); + ++index; + } + i++; + } + dest->desc[index].name =NULL ; +return dest; +} + +void free_opts_list (QemuOptsList *list) +{ +int i =0 ; + +while (list && list->desc[i].name) { +g_free ((char *)list->desc[i].name); +g_free ((char *)list->desc[i].help); +g_free ((char *)list->desc[i].def_value_str); + i++; + } + +g_free (list); +} + +void print_opts_list (QemuOptsList *list) +{ +int i =0 ; +printf ("Supported options:\n" ); +while (list && list->desc[i].name) { +printf ("%-16s %s\n" , list->desc[i].name, + list->desc[i].help ? + list->desc[i].help :"No description available" ); + i++; + } +}
qemu-option.h +4 -0
@@ -156,4 +156,8 @@ int qemu_opts_print(QemuOpts *opts, void *dummy);int qemu_opts_foreach (QemuOptsList *list,qemu_opts_loopfunc func,void *opaque,int abort_on_failure); +QemuOptsList *append_opts_list (QemuOptsList *dest, +QemuOptsList *list); +void free_opts_list (QemuOptsList *list); +void print_opts_list (QemuOptsList *list);#endif
Signature · 1 line
1.7.1
PATCH v9 3/4 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_options 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.
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 | 91 +++++++++++++--------------- block.h | 4 +- block/cow.c | 46 +++++++------- block/gluster.c | 37 ++++++------ block/qcow.c | 60 +++++++++--------- block/qcow2.c | 171 +++++++++++++++++++++++++++-------------------------- block/qed.c | 86 +++++++++++++------------- block/raw-posix.c | 65 ++++++++++----------- block/raw-win32.c | 30 +++++---- block/raw.c | 30 +++++---- block/rbd.c | 63 ++++++++++---------- block/sheepdog.c | 75 +++++++++++------------ block/vdi.c | 69 +++++++++++----------- block/vmdk.c | 74 ++++++++++++------------ block/vpc.c | 67 +++++++++++---------- block/vvfat.c | 11 ++-- block_int.h | 6 +- qemu-img.c | 61 +++++++++---------- 18 files changed, 522 insertions(+), 524 deletions(-)
block.c +43 -48
@@ -361,7 +361,7 @@ BlockDriver *bdrv_find_whitelisted_format(const char *format_name)typedef struct CreateCo {BlockDriver *drv;char *filename; -QEMUOptionParameter *options; +QemuOpts *opts;int ret; }CreateCo ;
@@ -370,11 +370,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;
@@ -382,7 +382,7 @@ int bdrv_create(BlockDriver *drv, const char* filename,CreateCo cco = { .drv = drv, .filename =g_strdup (filename), - .options = options, + .opts = opts, .ret =NOT_DONE , };
@@ -409,7 +409,7 @@ out:return ret; } -int bdrv_create_file (const char * filename,QEMUOptionParameter *options) +int bdrv_create_file (const char *filename,QemuOpts *opts) {BlockDriver *drv;
@@ -418,7 +418,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options)return -ENOENT ; } -return bdrv_create (drv, filename, options); +return bdrv_create (drv, filename, opts); }/*
@@ -798,7 +798,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
@@ -831,17 +831,16 @@ 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_nofail (bdrv_qcow2->bdrv_create_options); -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 , - drv->format_name); +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; }
@@ -4485,8 +4484,10 @@ void 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, Error **errp) { - QEMUOptionParameter *param = NULL, *create_options = NULL; -QEMUOptionParameter *backing_fmt, *backing_file, *size; + QemuOpts *opts = 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 ;
@@ -4504,28 +4505,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_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); + opts =qemu_opts_create_nofail (create_options); -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 (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_set (opts,BLOCK_OPT_BACKING_FILE , base_filename)) {error_setg (errp,"Backing file not supported for file format '%s'" , fmt);
@@ -4534,39 +4530,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_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_number (opts,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 */
@@ -4575,17 +4569,16 @@ void 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_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_set_number (opts,BLOCK_OPT_SIZE , size); }else {error_setg (errp,"Image creation needs a size parameter" ); goto out;
@@ -4593,10 +4586,10 @@ void bdrv_img_create(const char *filename, const char *fmt, }printf ("Formatting '%s', fmt=%s " , filename, fmt); -print_option_parameters (param); +qemu_opts_print (opts,NULL );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 "
@@ -4611,8 +4604,10 @@ void 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 (opts) { +qemu_opts_del (opts); + }if (bs) {bdrv_delete (bs);
block.h +2 -2
@@ -124,8 +124,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);
block/cow.c +23 -23
72 lines not on this page · raw message
block/gluster.c +19 -18
@@ -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,11 +349,9 @@ 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++; +if (opts) { + total_size = +qemu_opt_get_number (opts,BLOCK_OPT_SIZE ,0 ) /BDRV_SECTOR_SIZE ; } fd = glfs_creat(glfs, gconf->image,
@@ -544,13 +541,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 ="gluster-create-opts" , + .head =QTAILQ_HEAD_INITIALIZER (gluster_create_opts.head), + .desc = { + { + .name =BLOCK_OPT_SIZE , + .type =QEMU_OPT_NUMBER , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static BlockDriver bdrv_gluster = {
@@ -565,7 +566,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, + .create_options = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_tcp = {
@@ -580,7 +581,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, + .create_options = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_unix = {
@@ -595,7 +596,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, + .create_options = &qemu_gluster_create_opts, };static BlockDriver bdrv_gluster_rdma = {
@@ -610,7 +611,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, + .create_options = &qemu_gluster_create_opts, };static void bdrv_gluster_init (void )
block/qcow.c +30 -30
87 lines not on this page · raw message
block/qcow2.c +87 -84
217 lines not on this page · raw message
block/qed.c +43 -43
109 lines not on this page · raw message
block/raw-posix.c +31 -34
154 lines not on this page · raw message
block/raw-win32.c +16 -14
@@ -382,17 +382,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++; +if (opts) { + total_size = +qemu_opt_get_number (opts,BLOCK_OPT_SIZE ,0 ) /512 ; } fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
@@ -405,13 +403,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_NUMBER , + .help ="Virtual disk size" + }, + {/* end of list */ } + } };static BlockDriver bdrv_file = {
@@ -431,7 +433,7 @@ static BlockDriver bdrv_file = { .bdrv_get_allocated_file_size = raw_get_allocated_file_size, - .create_options = raw_create_options, + .create_options = &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_NUMBER , + .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_options = &raw_create_opts, };static void bdrv_raw_init (void )
block/rbd.c +32 -31
90 lines not on this page · raw message
block/sheepdog.c +36 -39
105 lines not on this page · raw message
block/vdi.c +34 -35
101 lines not on this page · raw message
block/vmdk.c +37 -37
99 lines not on this page · raw message
block/vpc.c +35 -32
96 lines not on this page · raw message
block/vvfat.c +6 -5
@@ -2806,7 +2806,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 );
@@ -2822,12 +2822,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_options); +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 ) {
block_int.h +2 -4
@@ -88,7 +88,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 */
@@ -177,9 +177,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;/* * 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.1
PATCH v9 4/4 remove QEMUOptionParameter related functions and struct
In reply to Dong Xu Wang--- qemu-option.c | 285 --------------------------------------------------------- qemu-option.h | 32 ------- 2 files changed, 0 insertions(+), 317 deletions(-)
qemu-option.c +0 -285
@@ -153,22 +153,6 @@ int check_params(char *buf, int buf_size,return 0 ; } -/* -* Searches an option list for an option with the given name -*/ -QEMUOptionParameter *get_option_parameter (QEMUOptionParameter *list, -const char *name) -{ -while (list && list->name) { -if (!strcmp (list->name, name)) { -return list; - } - list++; - } - -return NULL ; -} -static void parse_option_bool (const char *name,const char *value,bool *ret,Error **errp) {
@@ -240,275 +224,6 @@ static void parse_option_size(const char *name, const char *value, } } -/* -* Sets the value of a parameter in a given option list. The parsing of the -* value depends on the type of option: -* -* OPT_FLAG (uses value.n): -* If no value is given, the flag is set to 1. -* Otherwise the value must be "on" (set to 1) or "off" (set to 0) -* -* OPT_STRING (uses value.s): -* value is strdup()ed and assigned as option value -* -* OPT_SIZE (uses value.n): -* The value is converted to an integer. Suffixes for kilobytes etc. are -* allowed (powers of 1024). -* -* Returns 0 on succes, -1 in error cases -*/ -int set_option_parameter (QEMUOptionParameter *list,const char *name, -const char *value) -{ -bool flag; -Error *local_err =NULL ; - -// Find a matching parameter - list =get_option_parameter (list, name); -if (list ==NULL ) { -fprintf (stderr,"Unknown option '%s'\n" , name); -return -1 ; - } - -// Process parameter -switch (list->type) { -case OPT_FLAG : -parse_option_bool (name, value, &flag, &local_err); -if (!error_is_set (&local_err)) { - list->value.n = flag; - } -break ; - -case OPT_STRING : -if (value !=NULL ) { - list->value.s =g_strdup (value); - }else { -fprintf (stderr,"Option '%s' needs a parameter\n" , name); -return -1 ; - } -break ; - -case OPT_SIZE : -parse_option_size (name, value, &list->value.n, &local_err); -break ; - -default : -fprintf (stderr,"Bug: Option '%s' has an unknown type\n" , name); -return -1 ; - } - -if (error_is_set (&local_err)) { -qerror_report_err (local_err); -error_free (local_err); -return -1 ; - } - -return 0 ; -} - -/* -* Sets the given parameter to an integer instead of a string. -* This function cannot be used to set string options. -* -* Returns 0 on success, -1 in error cases -*/ -int set_option_parameter_int (QEMUOptionParameter *list,const char *name, -uint64_t value) -{ -// Find a matching parameter - list =get_option_parameter (list, name); -if (list ==NULL ) { -fprintf (stderr,"Unknown option '%s'\n" , name); -return -1 ; - } - -// Process parameter -switch (list->type) { -case OPT_FLAG : -case OPT_NUMBER : -case OPT_SIZE : - list->value.n = value; -break ; - -default : -return -1 ; - } - -return 0 ; -} - -/* -* Frees a option list. If it contains strings, the strings are freed as well. -*/ -void free_option_parameters (QEMUOptionParameter *list) -{ -QEMUOptionParameter *cur = list; - -while (cur && cur->name) { -if (cur->type ==OPT_STRING ) { -g_free (cur->value.s); - } - cur++; - } - -g_free (list); -} - -/* -* Count valid options in list -*/ -static size_t count_option_parameters (QEMUOptionParameter *list) -{ -size_t num_options =0 ; - -while (list && list->name) { - num_options++; - list++; - } - -return num_options; -} - -/* -* Append an option list (list) to an option list (dest). -* -* If dest is NULL, a new copy of list is created. -* -* Returns a pointer to the first element of dest (or the newly allocated copy) -*/ -QEMUOptionParameter *append_option_parameters (QEMUOptionParameter *dest, -QEMUOptionParameter *list) -{ -size_t num_options, num_dest_options; - - num_options =count_option_parameters (dest); - num_dest_options = num_options; - - num_options +=count_option_parameters (list); - - dest =g_realloc (dest, (num_options +1 ) *sizeof (QEMUOptionParameter)); - dest[num_dest_options].name =NULL ; - -while (list && list->name) { -if (get_option_parameter (dest, list->name) ==NULL ) { - dest[num_dest_options++] = *list; - dest[num_dest_options].name =NULL ; - } - list++; - } - -return dest; -} - -/* -* Parses a parameter string (param) into an option list (dest). -* -* list is the template option list. If dest is NULL, a new copy of list is -* created. If list is NULL, this function fails. -* -* A parameter string consists of one or more parameters, separated by commas. -* Each parameter consists of its name and possibly of a value. In the latter -* case, the value is delimited by an = character. To specify a value which -* contains commas, double each comma so it won't be recognized as the end of -* the parameter. -* -* For more details of the parsing see above. -* -* Returns a pointer to the first element of dest (or the newly allocated copy) -* or NULL in error cases -*/ -QEMUOptionParameter *parse_option_parameters (const char *param, -QEMUOptionParameter *list,QEMUOptionParameter *dest) -{ -QEMUOptionParameter *allocated =NULL ; -char name[256 ]; -char value[256 ]; -char *param_delim, *value_delim; -char next_delim; - -if (list ==NULL ) { -return NULL ; - } - -if (dest ==NULL ) { - dest = allocated =append_option_parameters (NULL , list); - } - -while (*param) { - -// Find parameter name and value in the string - param_delim =strchr (param,',' ); - value_delim =strchr (param,'=' ); - -if (value_delim && (value_delim < param_delim || !param_delim)) { - next_delim ='=' ; - }else { - next_delim =',' ; - value_delim =NULL ; - } - - param =get_opt_name (name,sizeof (name), param, next_delim); -if (value_delim) { - param =get_opt_value (value,sizeof (value), param +1 ); - } -if (*param !='\0' ) { - param++; - } - -// Set the parameter -if (set_option_parameter (dest, name, value_delim ? value :NULL )) { - goto fail; - } - } - -return dest; - -fail: -// Only free the list if it was newly allocated -free_option_parameters (allocated); -return NULL ; -} - -/* -* Prints all options of a list that have a value to stdout -*/ -void print_option_parameters (QEMUOptionParameter *list) -{ -while (list && list->name) { -switch (list->type) { -case OPT_STRING : -if (list->value.s !=NULL ) { -printf ("%s='%s' " , list->name, list->value.s); - } -break ; -case OPT_FLAG : -printf ("%s=%s " , list->name, list->value.n ?"on" :"off" ); -break ; -case OPT_SIZE : -case OPT_NUMBER : -printf ("%s=%" PRId64" " , list->name, list->value.n); -break ; -default : -printf ("%s=(unknown type) " , list->name); -break ; - } - list++; - } -} - -/* -* Prints an overview of all available options -*/ -void print_option_help (QEMUOptionParameter *list) -{ -printf ("Supported options:\n" ); -while (list && list->name) { -printf ("%-16s %s\n" , list->name, - list->help ? list->help :"No description available" ); - list++; - } -} -/* ------------------------------------------------------------------ */ static QemuOpt *qemu_opt_find (QemuOpts *opts,const char *name)
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 ;
Signature · 1 line
1.7.1
Quoted 89 lines · Dong Xu Wang
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 3 was changed according Paolo's comments.Patch 4-5: because
qemu_opts_createcan not fail while id is null, so create functionqemu_opts_create_nofailand use it.Patch 6: create function
qemu_opt_set_number, likeqemu_opt_set_bool.Patch 7: add
def_valueand use it inqemu_opts_print.Patch 8: Create functions to pair with QEMUOptionParameter parser.
Patch 9: Use QemuOpts parser in Block.
Patch 10: Remove QEMUOptionParameter parser related code.
Patches 1 - 6 have been merged into block branch, so only patches 8 to 10 are included.
v8->v9)
1) addqemu_prefix togluster_create_opts.
2) fix bug:bdrv_gluster_unixandbdrv_gluster_rdmashould also be converted.
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.
3) rebase to upstream source tree.
4) addgluster.c,raw-win32.c, andrbd.c.
v6->v7:
1) Fix typo:enouth->enough.
2) use osdep.h:stringify(), not redefining new macro.
3) preserve TODO comment.
4) fix typo:BLOCK_OPT_ENCRYPT->BLOCK_OPT_STATIC.
5) initializedisk_typeeven when opts is NULL.
v5->v6:
1) allocate enough space inappend_opts_listfunction.
2) judge if opts == NULL in block layer create functions.
3) usebdrv_create_file(filename, NULL)inqcow_createfuntion.
4) made more readable while usingqemu_opt_get_numberfuntion.
v4->v5:
1) Rewriteqemu_opts_create_nofailfunction based on Peter Maydell's comments.
2) Useg_strdup_printfinqemu_opt_set_number.
3) Rewriteqemu_opts_print.
4) .bdrv_create_options returns pointer directly. Fix a bug about "encryption".
5) Checkqemu_opt_get_numberinraw-posix.c.
v3->v4:
1) Rebased to the newest source tree.
2) Remove redundant "#include "block-cache.h"
3) Other small changes.
v2->v3:
1) rewriteqemu_opt_set_boolandqemu_opt_set_numberaccording Paolo's coments.
2) split patches to make review easier.
v1->v2:
1) add Luiz's patches.
2) createqemu_opt_set_number()andqemu_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 (4):
adddef_print_strand use it inqemu_opts_print. Create four opts list related functions
Use QemuOpts support in block layer
remove QEMUOptionParameter related functions and structblock.c | 91 ++++++------- block.h | 4 +- block/cow.c | 46 +++--- block/gluster.c | 37 +++--- block/qcow.c | 60 ++++---- block/qcow2.c | 171 ++++++++++++----------- block/qed.c | 86 ++++++------ block/raw-posix.c | 65 ++++----- block/raw-win32.c | 30 ++-- block/raw.c | 30 +++-- block/rbd.c | 63 ++++---- block/sheepdog.c | 75 +++++----- block/vdi.c | 69 +++++-----
Quoted 8 lines
block/vmdk.c | 74 +++++----- block/vpc.c | 67 +++++---- block/vvfat.c | 11 +- block_int.h | 6 +- qemu-img.c | 61 ++++---- qemu-option.c | 406 +++++++++++++++-------------------------------------- qemu-option.h | 37 +---- 20 files changed, 641 insertions(+), 848 deletions(-)
block/rbd.c: In function ‘qemu_rbd_create’:block/rbd.c:315:9: error: ‘cluster_size’ undeclared (first use in this function) block/rbd.c:315:9: note: each undeclared identifier is reported only once for each function it appears in block/rbd.c: At top level:block/rbd.c:945:5: error: unknown field ‘create_options’ specified in initializer block/rbd.c:945:5: error: initialization from incompatible pointer type [-Werror] block/rbd.c:945:5: error: (near initialization for ‘bdrv_rbd.bdrv_save_vmstate’) [-Werror]
Stefan