Skip to contentSkip to conversation

replace QEMUOptionParameter with QemuOpts

PATCH Superseded Chunyan Liu 1 participant 19 messages May 7, 2014 – May 14, 2014 Block layer

v27 of 28 Superseded Latest is v28 →

lore
Share & tools
lore
#0 lore raw

v27 cover replace QEMUOptionParameter with QemuOpts

This patch series is to replace QEMUOptionParameter with QemuOpts, so that only one Qemu Option structure is kept in QEMU code.

---
Changes to v26:
* Following Eric's comment, backward split 2/33, 3/33. (repurpose qemu_opts_print first, add def_value_str to QemuOptDesc later).
* Fix memory free in qemu_opts_append to solve iotest issue. 10/33
* Following Eric's comment, remove the end '.' in error message. And update

    qemu-iotests .out file. 12/33

* Following Eric's comment, fix memory free in vvfat.c 13/33
* Following Eric's comment, split qcow2 patch into two. 19/33, 20/33:
export qemu_opt_find first, add qcow2 driver patch later.
* rebase to git master

All patches are also available from:
https://github.com/chunyanliu/qemu/commits/QemuOpts

Chunyan Liu (33):
QemuOpts: move find_desc_by_name ahead for later calling
QemuOpts: repurpose qemu_opts_print to replace
print_option_parameters
QemuOpts: add def_value_str to QemuOptDesc
qapi: output def_value_str when query command line options
QemuOpts: change opt->name|str from (const char *) to (char *)
QemuOpts: move qemu_opt_del ahead for later calling
QemuOpts: add qemu_opt_get_*_del functions for replace work
QemuOpts: add qemu_opts_print_help to replace print_option_help
QemuOpts: add conversion between QEMUOptionParameter to QemuOpts
QemuOpts: add qemu_opts_append to replace append_option_parameters
QemuOpts: check NULL input for qemu_opts_del
change block layer to support both QemuOpts and QEMUOptionParamter vvfat.c: handle cross_driver's create_options and create_opts cow.c: replace QEMUOptionParameter with QemuOpts gluster.c: replace QEMUOptionParameter with QemuOpts iscsi.c: replace QEMUOptionParameter with QemuOpts nfs.c: replace QEMUOptionParameter with QemuOpts qcow.c: replace QEMUOptionParameter with QemuOpts
QemuOpts: export qemu_opt_find
qcow2.c: replace QEMUOptionParameter with QemuOpts qed.c: replace QEMUOptionParameter with QemuOpts raw-posix.c: replace QEMUOptionParameter with QemuOpts raw-win32.c: replace QEMUOptionParameter with QemuOpts raw_bsd.c: replace QEMUOptionParameter with QemuOpts rbd.c: replace QEMUOptionParameter with QemuOpts sheepdog.c: replace QEMUOptionParameter with QemuOpts ssh.c: replace QEMUOptionParameter with QemuOpts
vdi.c: replace QEMUOptionParameter with QemuOpts vhdx.c: replace QEMUOptionParameter with QemuOpts vmdk.c: replace QEMUOptionParameter with QemuOpts vpc.c: replace QEMUOptionParameter with QemuOpts cleanup QEMUOptionParameter
QemuOpts: cleanup tmp 'allocated' member from QemuOptsList

 block.c                    |  99 ++++----
 block/cow.c                |  52 ++--
 block/gluster.c            |  73 +++---
 block/iscsi.c              |  32 ++-
 block/nfs.c                |  10 +-
 block/qcow.c               |  72 +++---
 block/qcow2.c              | 259 ++++++++++----------
 block/qed.c                | 112 +++++----
 block/qed.h                |   3 +-
 block/raw-posix.c          |  55 ++---
 block/raw-win32.c          |  38 +--
 block/raw_bsd.c            |  25 +-
 block/rbd.c                |  61 +++--
 block/sheepdog.c           | 102 ++++----
 block/ssh.c                |  30 ++-
 block/vdi.c                |  71 +++---
 block/vhdx.c               |  97 ++++----
 block/vhdx.h               |   1 +
 block/vmdk.c               | 121 +++++-----
 block/vpc.c                |  60 ++---
 block/vvfat.c              |  13 +-
 include/block/block.h      |   7 +-
 include/block/block_int.h  |   9 +-
 include/qemu/option.h      |  53 +---
 include/qemu/option_int.h  |   4 +-
 qapi-schema.json           |   5 +-
 qapi/opts-visitor.c        |  10 +-
 qemu-img.c                 |  89 +++----
 qmp-commands.hx            |   2 +
 tests/qemu-iotests/049.out |   2 +-
 tests/qemu-iotests/061.out |   2 +-
 util/qemu-config.c         |   4 +
 util/qemu-option.c         | 588 ++++++++++++++++++++-------------------------
 33 files changed, 1033 insertions(+), 1128 deletions(-)
Signature · 1 line
1.7.12.4
#1 lore raw

PATCH v27 1/33 QemuOpts: move find_desc_by_name ahead for later calling

---
 util/qemu-option.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
util/qemu-option.c +14 -14
index 8bbc3ad..808aef4 100644
@@ -173,6 +173,20 @@ static void parse_option_number(const char *name, const char *value,
     }
 }
 
+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;
+}
+
 void parse_option_size(const char *name, const char *value,
                        uint64_t *ret, Error **errp)
 {
@@ -637,20 +651,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;
-}
-
 int qemu_opt_unset(QemuOpts *opts, const char *name)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
Signature · 1 line
1.7.12.4
#2 lore raw

PATCH v27 2/33 QemuOpts: repurpose qemu_opts_print to replace print_option_parameters

In reply to Chunyan Liu

Currently this function is not used anywhere. In later patches, it will replace print_option_parameters. To avoid print info changes, change qemu_opts_print from fprintf stderr to printf, and remove last printf.

---
Changes:
* backward split than v26 (2/33, 3/33).

 include/qemu/option.h |  2 +-
 util/qemu-option.c    | 10 ++++------
 2 files changed, 5 insertions(+), 7 deletions(-)
include/qemu/option.h +1 -1
index 8c0ac34..1077b69 100644
@@ -156,7 +156,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);
+void 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 +4 -6
index 808aef4..290ed54 100644
@@ -895,17 +895,15 @@ void qemu_opts_del(QemuOpts *opts)
     g_free(opts);
 }
 
-int qemu_opts_print(QemuOpts *opts, void *dummy)
+void qemu_opts_print(QemuOpts *opts)
 {
     QemuOpt *opt;
 
-    fprintf(stderr, "%s: %s:", opts->list->name,
-            opts->id ? opts->id : "<noid>");
+    printf("%s: %s:", opts->list->name,
+           opts->id ? opts->id : "<noid>");
     QTAILQ_FOREACH(opt, &opts->head, next) {
-        fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
+        printf(" %s=\"%s\"", opt->name, opt->str);
     }
-    fprintf(stderr, "\n");
-    return 0;
 }
 
 static int opts_do_parse(QemuOpts *opts, const char *params,
Signature · 1 line
1.7.12.4
#3 lore raw

PATCH v27 3/33 QemuOpts: add def_value_str to QemuOptDesc

In reply to Chunyan Liu

Add def_value_str (default value) to QemuOptDesc, to replace function of the default value in QEMUOptionParameter.

Improve qemu_opts_get_* functions: if find opt, return opt->str; otherwise, if desc->def_value_str is set, return desc->def_value_str; otherwise, return input defval.

Improve qemu_opts_print: if option is set, print opt->str; otherwise, if desc->def_value_str is set, print it.

---
Changes:
* backward split than v26. (2/33, 3/33)

 include/qemu/option.h |  1 +
 util/qemu-option.c    | 56 ++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 50 insertions(+), 7 deletions(-)
include/qemu/option.h +1 -0
index 1077b69..c3b0a91 100644
@@ -99,6 +99,7 @@ typedef struct QemuOptDesc {
     const char *name;
     enum QemuOptType type;
     const char *help;
+    const char *def_value_str;
 } QemuOptDesc;
 
 struct QemuOptsList {
util/qemu-option.c +49 -7

90 lines not on this page · raw message

Signature · 1 line
1.7.12.4
#4 lore raw

PATCH v27 4/33 qapi: output def_value_str when query command line options

In reply to Chunyan Liu

Change qapi interfaces to output the newly added def_value_str when querying command line options.

---
 qapi-schema.json   | 5 ++++-
 qmp-commands.hx    | 2 ++
 util/qemu-config.c | 4 ++++
 3 files changed, 10 insertions(+), 1 deletion(-)
qapi-schema.json +4 -1
index 0b00427..880829d 100644
@@ -4088,12 +4088,15 @@
 #
 # @help: #optional human readable text string, not suitable for parsing.
 #
+# @default: #optional default value string (since 2.1)
+#
 # Since 1.5
 ##
 { 'type': 'CommandLineParameterInfo',
   'data': { 'name': 'str',
             'type': 'CommandLineParameterType',
-            '*help': 'str' } }
+            '*help': 'str',
+            '*default': 'str' } }
 
 ##
 # @CommandLineOptionInfo:
qmp-commands.hx +2 -0
index ed3ab92..1271332 100644
@@ -2895,6 +2895,8 @@ Each array entry contains the following:
               or 'size')
     - "help": human readable description of the parameter
               (json-string, optional)
+    - "default": default value string for the parameter
+                 (json-string, optional)
 
 Example:
 
util/qemu-config.c +4 -0
index f4e4f38..ba375c0 100644
@@ -82,6 +82,10 @@ static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
             info->has_help = true;
             info->help = g_strdup(desc[i].help);
         }
+        if (desc[i].def_value_str) {
+            info->has_q_default = true;
+            info->q_default = g_strdup(desc[i].def_value_str);
+        }
 
         entry = g_malloc0(sizeof(*entry));
         entry->value = info;
Signature · 1 line
1.7.12.4
#5 lore raw

PATCH v27 5/33 QemuOpts: change opt->name|str from (const char *) to (char *)

In reply to Chunyan Liu

qemu_opt_del() already assumes that all QemuOpt instances contain malloc'd name and value; but it had to cast away const because opts_start_struct() was doing its own thing and using static storage instead. By using the correct type and malloced strings everywhere, the usage of this struct becomes clearer.

---
 include/qemu/option_int.h |  4 ++--
 qapi/opts-visitor.c       | 10 +++++++---
 util/qemu-option.c        |  4 ++--
 3 files changed, 11 insertions(+), 7 deletions(-)
include/qemu/option_int.h +2 -2
index 8212fa4..6432c1a 100644
@@ -30,8 +30,8 @@
 #include "qemu/error-report.h"
 
 struct QemuOpt {
-    const char   *name;
-    const char   *str;
+    char *name;
+    char *str;
 
     const QemuOptDesc *desc;
     union {
qapi/opts-visitor.c +7 -3
index 5d830a2..7a64f4e 100644
@@ -143,8 +143,8 @@ opts_start_struct(Visitor *v, void **obj, const char *kind,
     if (ov->opts_root->id != NULL) {
         ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt);
 
-        ov->fake_id_opt->name = "id";
-        ov->fake_id_opt->str = ov->opts_root->id;
+        ov->fake_id_opt->name = g_strdup("id");
+        ov->fake_id_opt->str = g_strdup(ov->opts_root->id);
         opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
     }
 }
@@ -177,7 +177,11 @@ opts_end_struct(Visitor *v, Error **errp)
     }
     g_hash_table_destroy(ov->unprocessed_opts);
     ov->unprocessed_opts = NULL;
-    g_free(ov->fake_id_opt);
+    if (ov->fake_id_opt) {
+        g_free(ov->fake_id_opt->name);
+        g_free(ov->fake_id_opt->str);
+        g_free(ov->fake_id_opt);
+    }
     ov->fake_id_opt = NULL;
 }
 
util/qemu-option.c +2 -2
index 2be6995..69cdf3f 100644
@@ -664,8 +664,8 @@ static void qemu_opt_parse(QemuOpt *opt, Error **errp)
 static void qemu_opt_del(QemuOpt *opt)
 {
     QTAILQ_REMOVE(&opt->opts->head, opt, next);
-    g_free((/* !const */ char*)opt->name);
-    g_free((/* !const */ char*)opt->str);
+    g_free(opt->name);
+    g_free(opt->str);
     g_free(opt);
 }
 
Signature · 1 line
1.7.12.4
#6 lore raw

PATCH v27 6/33 QemuOpts: move qemu_opt_del ahead for later calling

In reply to Chunyan Liu

In later patch, qemu_opt_get_del functions will be added, they will first get the option value, then call qemu_opt_del to remove the option from opt list. To prepare for that purpose, move qemu_opt_del ahead first.

---
 util/qemu-option.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
util/qemu-option.c +8 -8
index 69cdf3f..4d2d4d1 100644
@@ -567,6 +567,14 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
     return NULL;
 }
 
+static void qemu_opt_del(QemuOpt *opt)
+{
+    QTAILQ_REMOVE(&opt->opts->head, opt, next);
+    g_free(opt->name);
+    g_free(opt->str);
+    g_free(opt);
+}
+
 const char *qemu_opt_get(QemuOpts *opts, const char *name)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
@@ -661,14 +669,6 @@ static void qemu_opt_parse(QemuOpt *opt, Error **errp)
     }
 }
 
-static void qemu_opt_del(QemuOpt *opt)
-{
-    QTAILQ_REMOVE(&opt->opts->head, opt, next);
-    g_free(opt->name);
-    g_free(opt->str);
-    g_free(opt);
-}
-
 static bool opts_accepts_any(const QemuOpts *opts)
 {
     return opts->list->desc[0].name == NULL;
Signature · 1 line
1.7.12.4
#7 lore raw

PATCH v27 7/33 QemuOpts: add qemu_opt_get_*_del functions for replace work

In reply to Chunyan Liu

Add qemu_opt_get_del, qemu_opt_get_bool_del, qemu_opt_get_number_del and qemu_opt_get_size_del to replace the same handling of QEMUOptionParameter (get and delete).

Several drivers are coded to parse a known subset of options, then remove them from the list before handing all remaining options to a second driver for further option processing. get_*_del makes it easier to retrieve a known option (or its default) and remove it from the list all in one action.

Share common helper function:

For qemu_opt_get_bool/size/number, they and their get_*_del counterpart could share most of the code except whether or not deleting the opt from option list, so generate common helper functions.

For qemu_opt_get and qemu_opt_get_del, keep code duplication, since
1. qemu_opt_get_del returns malloc'd memory while qemu_opt_get returns in-place memory
2. qemu_opt_get_del returns (char *), qemu_opt_get returns (const char *), and could not change to (char *), since in one case, it will return desc->def_value_str, which is (const char *).

---
 include/qemu/option.h |   6 +++
 util/qemu-option.c    | 116 ++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 109 insertions(+), 13 deletions(-)
include/qemu/option.h +6 -0
index c3b0a91..6653e43 100644
@@ -111,6 +111,7 @@ struct QemuOptsList {
 };
 
 const char *qemu_opt_get(QemuOpts *opts, const char *name);
+char *qemu_opt_get_del(QemuOpts *opts, const char *name);
 /**
  * qemu_opt_has_help_opt:
  * @opts: options to search for a help request
@@ -126,6 +127,11 @@ bool qemu_opt_has_help_opt(QemuOpts *opts);
 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);
 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval);
+bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval);
+uint64_t qemu_opt_get_number_del(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_unset(QemuOpts *opts, const char *name);
 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,
util/qemu-option.c +103 -13

165 lines not on this page · raw message

Signature · 1 line
1.7.12.4
#8 lore raw

PATCH v27 8/33 QemuOpts: add qemu_opts_print_help to replace print_option_help

In reply to Chunyan Liu

print_option_help takes QEMUOptionParameter as parameter, add qemu_opts_print_help to take QemuOptsList as parameter for later replace work.

---
 include/qemu/option.h |  1 +
 util/qemu-option.c    | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/qemu/option.h +1 -0
index 6653e43..fbf5dc2 100644
@@ -166,5 +166,6 @@ typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
 void qemu_opts_print(QemuOpts *opts);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure);
+void qemu_opts_print_help(QemuOptsList *list);
 
 #endif
util/qemu-option.c +13 -0
index 32e1d50..adb7c3c 100644
@@ -553,6 +553,19 @@ void print_option_help(QEMUOptionParameter *list)
     }
 }
 
+void qemu_opts_print_help(QemuOptsList *list)
+{
+    QemuOptDesc *desc;
+
+    assert(list);
+    desc = list->desc;
+    printf("Supported options:\n");
+    while (desc && desc->name) {
+        printf("%-16s %s\n", desc->name,
+               desc->help ? desc->help : "No description available");
+        desc++;
+    }
+}
 /* ------------------------------------------------------------------ */
 
 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
Signature · 1 line
1.7.12.4
#9 lore raw

PATCH v27 9/33 QemuOpts: add conversion between QEMUOptionParameter to QemuOpts

In reply to Chunyan Liu

Add two temp conversion functions between QEMUOptionParameter to QemuOpts, so that next patch can use it. It will simplify later patch for easier review. And will be finally removed after all backend drivers switch to QemuOpts.

---
 include/qemu/option.h |   9 +++
 util/qemu-option.c    | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 162 insertions(+)
include/qemu/option.h +9 -0
index fbf5dc2..e98e8ef 100644
@@ -103,6 +103,12 @@ typedef struct QemuOptDesc {
 } QemuOptDesc;
 
 struct QemuOptsList {
+    /* FIXME: Temp used for QEMUOptionParamter->QemuOpts conversion to
+     * indicate the need to free memory. Will remove after all drivers
+     * switch to QemuOpts.
+     */
+    bool allocated;
+
     const char *name;
     const char *implied_opt_name;
     bool merge_lists;  /* Merge multiple uses of option into a single list? */
@@ -167,5 +173,8 @@ void qemu_opts_print(QemuOpts *opts);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure);
 void qemu_opts_print_help(QemuOptsList *list);
+void qemu_opts_free(QemuOptsList *list);
+QEMUOptionParameter *opts_to_params(QemuOpts *opts);
+QemuOptsList *params_to_opts(QEMUOptionParameter *list);
 
 #endif
util/qemu-option.c +153 -0

156 lines not on this page · raw message

Signature · 1 line
1.7.12.4
#10 lore raw

PATCH v27 10/33 QemuOpts: add qemu_opts_append to replace append_option_parameters

In reply to Chunyan Liu

For later merge .create_opts of drv and proto_drv in qemu-img commands.

---
Changes:
* fix memory free:

     if (param) {
   -     g_free(list);
   +     qemu_opts_free(list); //free allocated memory too
     }
 include/qemu/option.h |  5 ++++
 util/qemu-option.c    | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
include/qemu/option.h +5 -0
index e98e8ef..44d9961 100644
@@ -176,5 +176,10 @@ void qemu_opts_print_help(QemuOptsList *list);
 void qemu_opts_free(QemuOptsList *list);
 QEMUOptionParameter *opts_to_params(QemuOpts *opts);
 QemuOptsList *params_to_opts(QEMUOptionParameter *list);
+/* FIXME: will remove QEMUOptionParameter after all drivers switch to QemuOpts.
+ */
+QemuOptsList *qemu_opts_append(QemuOptsList *dst,
+                               QemuOptsList *list,
+                               QEMUOptionParameter *param);
 
 #endif
util/qemu-option.c +67 -0

70 lines not on this page · raw message

Signature · 1 line
1.7.12.4
#11 lore raw

PATCH v27 11/33 QemuOpts: check NULL input for qemu_opts_del

In reply to Chunyan Liu

To simplify later using of qemu_opts_del, accept NULL input.

---
 util/qemu-option.c | 4 ++++
 1 file changed, 4 insertions(+)
util/qemu-option.c +4 -0
index e15723a..71415ee 100644
@@ -1010,6 +1010,10 @@ void qemu_opts_del(QemuOpts *opts)
 {
     QemuOpt *opt;
 
+    if (opts == NULL) {
+        return;
+    }
+
     for (;;) {
         opt = QTAILQ_FIRST(&opts->head);
         if (opt == NULL)
Signature · 1 line
1.7.12.4
#12 lore raw

PATCH v27 12/33 change block layer to support both QemuOpts and QEMUOptionParamter

In reply to Chunyan Liu

Change block layer to support both QemuOpts and QEMUOptionParameter. After this patch, it will change backend drivers one by one. At the end, QEMUOptionParameter will be removed and only QemuOpts is kept.

---
Changes:
* Fix Eric's comments:
- remove the end '.' in two error messages.
- update related iotests .out file.

 block.c                    | 160 +++++++++++++++++++++++++++++++--------------
 block/cow.c                |   2 +-
 block/qcow.c               |   2 +-
 block/qcow2.c              |   2 +-
 block/qed.c                |   2 +-
 block/raw_bsd.c            |   2 +-
 block/vhdx.c               |   2 +-
 block/vmdk.c               |   4 +-
 block/vvfat.c              |   2 +-
 include/block/block.h      |   7 +-
 include/block/block_int.h  |  13 +++-
 qemu-img.c                 |  94 +++++++++++++-------------
 tests/qemu-iotests/049.out |   2 +-
 tests/qemu-iotests/061.out |   2 +-
 14 files changed, 183 insertions(+), 113 deletions(-)
block.c +111 -49
index b749d31..d417190 100644
@@ -328,6 +328,13 @@ void bdrv_register(BlockDriver *bdrv)
         }
     }
 
+    if (bdrv->bdrv_create) {
+        assert(!bdrv->bdrv_create2 && !bdrv->create_opts);
+        assert(!bdrv->bdrv_amend_options2);
+    } else if (bdrv->bdrv_create2) {
+        assert(!bdrv->bdrv_create && !bdrv->create_options);
+        assert(!bdrv->bdrv_amend_options);
+    }
     QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
 }
 
@@ -419,6 +426,7 @@ typedef struct CreateCo {
     BlockDriver *drv;
     char *filename;
     QEMUOptionParameter *options;
+    QemuOpts *opts;
     int ret;
     Error *err;
 } CreateCo;
@@ -430,8 +438,28 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque)
 
     CreateCo *cco = opaque;
     assert(cco->drv);
+    assert(!(cco->options && cco->opts));
 
-    ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err);
+    if (cco->drv->bdrv_create2) {
+        QemuOptsList *opts_list = NULL;
+        if (cco->options) {
+            opts_list = params_to_opts(cco->options);
+            cco->opts = qemu_opts_create(opts_list, NULL, 0, &error_abort);
+        }
+        ret = cco->drv->bdrv_create2(cco->filename, cco->opts, &local_err);
+        if (cco->options) {
+            qemu_opts_del(cco->opts);
+            qemu_opts_free(opts_list);
+        }
+    } else {
+        if (cco->opts) {
+            cco->options = opts_to_params(cco->opts);
+        }
+        ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err);
+        if (cco->opts) {
+            free_option_parameters(cco->options);
+        }
+    }
     if (local_err) {
         error_propagate(&cco->err, local_err);
     }
@@ -439,7 +467,8 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque)
 }
 
 int bdrv_create(BlockDriver *drv, const char* filename,
-    QEMUOptionParameter *options, Error **errp)
+                QEMUOptionParameter *options,
+                QemuOpts *opts, Error **errp)
 {
     int ret;
 
@@ -448,11 +477,12 @@ int bdrv_create(BlockDriver *drv, const char* filename,
         .drv = drv,
         .filename = g_strdup(filename),
         .options = options,
+        .opts = opts,
         .ret = NOT_DONE,
         .err = NULL,
     };
 
-    if (!drv->bdrv_create) {
+    if (!drv->bdrv_create && !drv->bdrv_create2) {
         error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
         ret = -ENOTSUP;
         goto out;
@@ -484,7 +514,7 @@ out:
 }
 
 int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
-                     Error **errp)
+                     QemuOpts *opts, Error **errp)
 {
     BlockDriver *drv;
     Error *local_err = NULL;
@@ -496,7 +526,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
         return -ENOENT;
     }
 
-    ret = bdrv_create(drv, filename, options, &local_err);
+    ret = bdrv_create(drv, filename, options, opts, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
     }
@@ -1212,7 +1242,8 @@ void bdrv_append_temp_snapshot(BlockDriverState *bs, Error **errp)
     char *tmp_filename = g_malloc0(PATH_MAX + 1);
     int64_t total_size;
     BlockDriver *bdrv_qcow2;
-    QEMUOptionParameter *create_options;
+    QemuOptsList *create_opts = NULL;
+    QemuOpts *opts = NULL;
     QDict *snapshot_options;
     BlockDriverState *bs_snapshot;
     Error *local_err;
@@ -1237,13 +1268,20 @@ void bdrv_append_temp_snapshot(BlockDriverState *bs, Error **errp)
     }
 
     bdrv_qcow2 = bdrv_find_format("qcow2");
-    create_options = parse_option_parameters("", bdrv_qcow2->create_options,
-                                             NULL);
-
-    set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size);
 
-    ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err);
-    free_option_parameters(create_options);
+    assert(!(bdrv_qcow2->create_options && bdrv_qcow2->create_opts));
+    if (bdrv_qcow2->create_options) {
+        create_opts = params_to_opts(bdrv_qcow2->create_options);
+    } else {
+        create_opts = bdrv_qcow2->create_opts;
+    }
+    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+    qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size);
+    ret = bdrv_create(bdrv_qcow2, tmp_filename, NULL, opts, &local_err);
+    qemu_opts_del(opts);
+    if (bdrv_qcow2->create_options) {
+        qemu_opts_free(create_opts);
+    }
     if (ret < 0) {
         error_setg_errno(errp, -ret, "Could not create temporary overlay "
                          "'%s': %s", tmp_filename,
@@ -5346,8 +5384,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;
+    QemuOptsList *create_opts = NULL;
+    QemuOpts *opts = NULL;
+    const char *backing_fmt, *backing_file;
+    int64_t size;
     BlockDriver *drv, *proto_drv;
     BlockDriver *backing_drv = NULL;
     Error *local_err = NULL;
@@ -5366,28 +5406,25 @@ void bdrv_img_create(const char *filename, const char *fmt,
         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(create_opts, drv->create_opts,
+                                   drv->create_options);
+    create_opts = qemu_opts_append(create_opts, proto_drv->create_opts,
+                                   proto_drv->create_options);
 
     /* Create parameter list with default values */
-    param = parse_option_parameters("", create_options, param);
-
-    set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
+    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+    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) {
-            error_setg(errp, "Invalid options for file format '%s'.", fmt);
+        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,
-                                 base_filename)) {
+        if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) {
             error_setg(errp, "Backing file not supported for file format '%s'",
                        fmt);
             goto out;
@@ -5395,37 +5432,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);
+                       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) {
             BlockDriverState *bs;
             uint64_t size;
             char buf[32];
@@ -5436,11 +5473,11 @@ void bdrv_img_create(const char *filename, const char *fmt,
                 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
 
             bs = NULL;
-            ret = bdrv_open(&bs, backing_file->value.s, NULL, NULL, back_flags,
+            ret = bdrv_open(&bs, backing_file, NULL, NULL, back_flags,
                             backing_drv, &local_err);
             if (ret < 0) {
                 error_setg_errno(errp, -ret, "Could not open '%s': %s",
-                                 backing_file->value.s,
+                                 backing_file,
                                  error_get_pretty(local_err));
                 error_free(local_err);
                 local_err = NULL;
@@ -5450,7 +5487,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
             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);
 
             bdrv_unref(bs);
         } else {
@@ -5461,16 +5498,18 @@ 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, &local_err);
+
+    ret = bdrv_create(drv, filename, NULL, opts, &local_err);
+
     if (ret == -EFBIG) {
         /* This is generally a better message than whatever the driver would
          * deliver (especially because of the cluster_size_hint), since that
          * is most probably not much different from "image too large". */
         const char *cluster_size_hint = "";
-        if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
+        if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
             cluster_size_hint = " (try using a larger cluster size)";
         }
         error_setg(errp, "The image size is too large for file format '%s'"
@@ -5480,9 +5519,8 @@ void bdrv_img_create(const char *filename, const char *fmt,
     }
 
 out:
-    free_option_parameters(create_options);
-    free_option_parameters(param);
-
+    qemu_opts_del(opts);
+    qemu_opts_free(create_opts);
     if (local_err) {
         error_propagate(errp, local_err);
     }
@@ -5500,12 +5538,36 @@ void bdrv_add_before_write_notifier(BlockDriverState *bs,
     notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
 }
 
-int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options)
+int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options,
+                       QemuOpts *opts)
 {
-    if (bs->drv->bdrv_amend_options == NULL) {
+    int ret;
+    assert(!(options && opts));
+
+    if (!bs->drv->bdrv_amend_options && !bs->drv->bdrv_amend_options2) {
         return -ENOTSUP;
     }
-    return bs->drv->bdrv_amend_options(bs, options);
+    if (bs->drv->bdrv_amend_options2) {
+        QemuOptsList *opts_list = NULL;
+        if (options) {
+            opts_list = params_to_opts(options);
+            opts = qemu_opts_create(opts_list, NULL, 0, &error_abort);
+        }
+        ret = bs->drv->bdrv_amend_options2(bs, opts);
+        if (options) {
+            qemu_opts_del(opts);
+            qemu_opts_free(opts_list);
+        }
+    } else {
+        if (opts) {
+            options = opts_to_params(opts);
+        }
+        ret = bs->drv->bdrv_amend_options(bs, options);
+        if (opts) {
+            free_option_parameters(options);
+        }
+    }
+    return ret;
 }
 
 /* This function will be called by the bdrv_recurse_is_first_non_filter method
block/cow.c +1 -1
index 164759f..7e61024 100644
@@ -345,7 +345,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
         options++;
     }
 
-    ret = bdrv_create_file(filename, options, &local_err);
+    ret = bdrv_create_file(filename, options, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         return ret;
block/qcow.c +1 -1
index 937dd6d..3bac591 100644
@@ -688,7 +688,7 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
         options++;
     }
 
-    ret = bdrv_create_file(filename, options, &local_err);
+    ret = bdrv_create_file(filename, options, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         return ret;
block/qcow2.c +1 -1
index a4b97e8..fb267cc 100644
@@ -1627,7 +1627,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
     Error *local_err = NULL;
     int ret;
 
-    ret = bdrv_create_file(filename, options, &local_err);
+    ret = bdrv_create_file(filename, options, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         return ret;
block/qed.c +1 -1
index c130e42..2982640 100644
@@ -566,7 +566,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,
     int ret = 0;
     BlockDriverState *bs;
 
-    ret = bdrv_create_file(filename, NULL, &local_err);
+    ret = bdrv_create_file(filename, NULL, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         return ret;
block/raw_bsd.c +1 -1
index 01ea692..9ae5fc2 100644
@@ -145,7 +145,7 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
     Error *local_err = NULL;
     int ret;
 
-    ret = bdrv_create_file(filename, options, &local_err);
+    ret = bdrv_create_file(filename, options, NULL, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
     }
block/vhdx.c +1 -1
index 509baaf..a9fcf6b 100644
@@ -1796,7 +1796,7 @@ static int vhdx_create(const char *filename, QEMUOptionParameter *options,
     block_size = block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX :
                                                     block_size;
 
-    ret = bdrv_create_file(filename, options, &local_err);
+    ret = bdrv_create_file(filename, options, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         goto exit;
block/vmdk.c +2 -2
index 06a1f9f..3802863 100644
@@ -1526,7 +1526,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
 
-    ret = bdrv_create_file(filename, NULL, &local_err);
+    ret = bdrv_create_file(filename, NULL, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         goto exit;
@@ -1866,7 +1866,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
     if (!split && !flat) {
         desc_offset = 0x200;
     } else {
-        ret = bdrv_create_file(filename, options, &local_err);
+        ret = bdrv_create_file(filename, options, NULL, &local_err);
         if (ret < 0) {
             error_setg_errno(errp, -ret, "Could not create image file");
             goto exit;
block/vvfat.c +1 -1
index c3af7ff..155fc9b 100644
@@ -2926,7 +2926,7 @@ static int enable_write_target(BDRVVVFATState *s)
     set_option_parameter_int(options, BLOCK_OPT_SIZE, s->sector_count * 512);
     set_option_parameter(options, BLOCK_OPT_BACKING_FILE, "fat:");
 
-    ret = bdrv_create(bdrv_qcow, s->qcow_filename, options, &local_err);
+    ret = bdrv_create(bdrv_qcow, s->qcow_filename, options, NULL, &local_err);
     if (ret < 0) {
         qerror_report_err(local_err);
         error_free(local_err);
include/block/block.h +4 -3
index 467fb2b..faf15e0 100644
@@ -178,9 +178,9 @@ BlockDriver *bdrv_find_format(const char *format_name);
 BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
                                           bool readonly);
 int bdrv_create(BlockDriver *drv, const char* filename,
-    QEMUOptionParameter *options, Error **errp);
+    QEMUOptionParameter *options, QemuOpts *opts, Error **errp);
 int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
-                     Error **errp);
+                     QemuOpts *opts, Error **errp);
 BlockDriverState *bdrv_new(const char *device_name, Error **errp);
 void bdrv_make_anon(BlockDriverState *bs);
 void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old);
@@ -285,7 +285,8 @@ typedef enum {
 
 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
 
-int bdrv_amend_options(BlockDriverState *bs_new, QEMUOptionParameter *options);
+int bdrv_amend_options(BlockDriverState *bs_new, QEMUOptionParameter *options,
+                       QemuOpts *opts);
 
 /* external snapshots */
 bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
include/block/block_int.h +12 -1
index 9ffcb69..3312b02 100644
@@ -118,6 +118,8 @@ struct BlockDriver {
     void (*bdrv_rebind)(BlockDriverState *bs);
     int (*bdrv_create)(const char *filename, QEMUOptionParameter *options,
                        Error **errp);
+    /* FIXME: will remove the duplicate and rename back to bdrv_create later */
+    int (*bdrv_create2)(const char *filename, QemuOpts *opts, Error **errp);
     int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
     int (*bdrv_make_empty)(BlockDriverState *bs);
     /* aio */
@@ -217,7 +219,12 @@ struct BlockDriver {
 
     /* List of options for creating images, terminated by name == NULL */
     QEMUOptionParameter *create_options;
-
+    /* FIXME: will replace create_options.
+     * These two fields are mutually exclusive. At most one is non-NULL.
+     * create_options should only be set with bdrv_create, and create_opts
+     * should only be set with bdrv_create2.
+     */
+    QemuOptsList *create_opts;
 
     /*
      * Returns 0 for completed check, -errno for internal errors.
@@ -228,6 +235,10 @@ struct BlockDriver {
 
     int (*bdrv_amend_options)(BlockDriverState *bs,
         QEMUOptionParameter *options);
+    /* FIXME: will remove the duplicate and rename back to
+     * bdrv_amend_options later
+     */
+    int (*bdrv_amend_options2)(BlockDriverState *bs, QemuOpts *opts);
 
     void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
 
qemu-img.c +45 -49

202 lines not on this page · raw message

tests/qemu-iotests/049.out +1 -1
index ceb2328..71ca44d 100644
@@ -120,7 +120,7 @@ qemu-img: kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes.
 
 qemu-img create -f qcow2 -o size=foobar TEST_DIR/t.qcow2
 qemu-img: Parameter 'size' expects a size
-qemu-img: TEST_DIR/t.qcow2: Invalid options for file format 'qcow2'.
+qemu-img: TEST_DIR/t.qcow2: Invalid options for file format 'qcow2'
 
 == Check correct interpretation of suffixes for cluster size ==
 
tests/qemu-iotests/061.out +1 -1
index 4027e00..e372470 100644
@@ -281,7 +281,7 @@ Lazy refcounts only supported with compatibility level 1.1 and above (use compat
 qemu-img: Error while amending options: Invalid argument
 Unknown compatibility level 0.42.
 qemu-img: Error while amending options: Invalid argument
-Unknown option 'foo'
+qemu-img: Invalid parameter 'foo'
 qemu-img: Invalid options for file format 'qcow2'
 Changing the cluster size is not supported.
 qemu-img: Error while amending options: Operation not supported
Signature · 1 line
1.7.12.4
#13 lore raw

PATCH v27 13/33 vvfat.c: handle cross_driver's create_options and create_opts

In reply to Chunyan Liu

vvfat shares create options of qcow driver. To avoid vvfat breaking when qcow driver changes from QEMUOptionParameter to QemuOpts, let it able to handle both cases.

---
Change:
* Fix memory free:
- qemu_opts_free(create_opts);
+ if (bdrv_qcow->create_options) {
+ qemu_opts_free(create_opts);
+ }

 block/vvfat.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
block/vvfat.c +17 -6
index 155fc9b..41fe623 100644
@@ -2906,8 +2906,9 @@ static BlockDriver vvfat_write_target = {
 
 static int enable_write_target(BDRVVVFATState *s)
 {
-    BlockDriver *bdrv_qcow;
-    QEMUOptionParameter *options;
+    BlockDriver *bdrv_qcow = NULL;
+    QemuOptsList *create_opts = NULL;
+    QemuOpts *opts = NULL;
     Error *local_err = NULL;
     int ret;
     int size = sector2cluster(s, s->sector_count);
@@ -2922,11 +2923,17 @@ 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:");
+    assert(!(bdrv_qcow->create_opts && bdrv_qcow->create_options));
+    if (bdrv_qcow->create_options) {
+        create_opts = params_to_opts(bdrv_qcow->create_options);
+    } else {
+        create_opts = bdrv_qcow->create_opts;
+    }
+    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+    qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512);
+    qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:");
 
-    ret = bdrv_create(bdrv_qcow, s->qcow_filename, options, NULL, &local_err);
+    ret = bdrv_create(bdrv_qcow, s->qcow_filename, NULL, opts, &local_err);
     if (ret < 0) {
         qerror_report_err(local_err);
         error_free(local_err);
@@ -2955,6 +2962,10 @@ static int enable_write_target(BDRVVVFATState *s)
     return 0;
 
 err:
+    qemu_opts_del(opts);
+    if (bdrv_qcow->create_options) {
+        qemu_opts_free(create_opts);
+    }
     g_free(s->qcow_filename);
     s->qcow_filename = NULL;
     return ret;
Signature · 1 line
1.7.12.4
#14 lore raw

PATCH v27 14/33 cow.c: replace QEMUOptionParameter with QemuOpts

In reply to Chunyan Liu
---
 block/cow.c | 54 ++++++++++++++++++++++++++----------------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
block/cow.c +26 -28
index 7e61024..af85753 100644
@@ -324,31 +324,24 @@ static void cow_close(BlockDriverState *bs)
 {
 }
 
-static int cow_create(const char *filename, QEMUOptionParameter *options,
-                      Error **errp)
+static int cow_create(const char *filename, QemuOpts *opts, Error **errp)
 {
     struct cow_header_v2 cow_header;
     struct stat st;
     int64_t image_sectors = 0;
-    const char *image_filename = NULL;
+    char *image_filename = NULL;
     Error *local_err = NULL;
     int ret;
     BlockDriverState *cow_bs;
 
     /* Read out options */
-    while (options && options->name) {
-        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
-            image_sectors = options->value.n / 512;
-        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
-            image_filename = options->value.s;
-        }
-        options++;
-    }
+    image_sectors = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512;
+    image_filename = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
 
-    ret = bdrv_create_file(filename, options, NULL, &local_err);
+    ret = bdrv_create_file(filename, NULL, opts, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
-        return ret;
+        goto exit;
     }
 
     cow_bs = NULL;
@@ -356,7 +349,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
                     BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
-        return ret;
+        goto exit;
     }
 
     memset(&cow_header, 0, sizeof(cow_header));
@@ -389,22 +382,27 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
     }
 
 exit:
+    g_free(image_filename);
     bdrv_unref(cow_bs);
     return ret;
 }
 
-static QEMUOptionParameter cow_create_options[] = {
-    {
-        .name = BLOCK_OPT_SIZE,
-        .type = OPT_SIZE,
-        .help = "Virtual disk size"
-    },
-    {
-        .name = BLOCK_OPT_BACKING_FILE,
-        .type = OPT_STRING,
-        .help = "File name of a base image"
-    },
-    { NULL }
+static QemuOptsList cow_create_opts = {
+    .name = "cow-create-opts",
+    .head = QTAILQ_HEAD_INITIALIZER(cow_create_opts.head),
+    .desc = {
+        {
+            .name = BLOCK_OPT_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
+        {
+            .name = BLOCK_OPT_BACKING_FILE,
+            .type = QEMU_OPT_STRING,
+            .help = "File name of a base image"
+        },
+        { /* end of list */ }
+    }
 };
 
 static BlockDriver bdrv_cow = {
@@ -414,14 +412,14 @@ static BlockDriver bdrv_cow = {
     .bdrv_probe     = cow_probe,
     .bdrv_open      = cow_open,
     .bdrv_close     = cow_close,
-    .bdrv_create    = cow_create,
+    .bdrv_create2   = cow_create,
     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
 
     .bdrv_read              = cow_co_read,
     .bdrv_write             = cow_co_write,
     .bdrv_co_get_block_status   = cow_co_get_block_status,
 
-    .create_options = cow_create_options,
+    .create_opts    = &cow_create_opts,
 };
 
 static void bdrv_cow_init(void)
Signature · 1 line
1.7.12.4
#15 lore raw

PATCH v27 15/33 gluster.c: replace QEMUOptionParameter with QemuOpts

In reply to Chunyan Liu
---
 block/gluster.c | 81 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 42 insertions(+), 39 deletions(-)
block/gluster.c +42 -39
index 8836085..c72ca77 100644
@@ -471,13 +471,14 @@ static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
 #endif
 
 static int qemu_gluster_create(const char *filename,
-        QEMUOptionParameter *options, Error **errp)
+                               QemuOpts *opts, Error **errp)
 {
     struct glfs *glfs;
     struct glfs_fd *fd;
     int ret = 0;
     int prealloc = 0;
     int64_t total_size = 0;
+    char *tmp = NULL;
     GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));
 
     glfs = qemu_gluster_init(gconf, filename, errp);
@@ -486,24 +487,21 @@ 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;
-        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
-            if (!options->value.s || !strcmp(options->value.s, "off")) {
-                prealloc = 0;
-            } else if (!strcmp(options->value.s, "full") &&
-                    gluster_supports_zerofill()) {
-                prealloc = 1;
-            } else {
-                error_setg(errp, "Invalid preallocation mode: '%s'"
-                    " or GlusterFS doesn't support zerofill API",
-                           options->value.s);
-                ret = -EINVAL;
-                goto out;
-            }
-        }
-        options++;
+    total_size =
+        qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE;
+
+    tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
+    if (!tmp || !strcmp(tmp, "off")) {
+        prealloc = 0;
+    } else if (!strcmp(tmp, "full") &&
+               gluster_supports_zerofill()) {
+        prealloc = 1;
+    } else {
+        error_setg(errp, "Invalid preallocation mode: '%s'"
+            " or GlusterFS doesn't support zerofill API",
+            tmp);
+        ret = -EINVAL;
+        goto out;
     }
 
     fd = glfs_creat(glfs, gconf->image,
@@ -525,6 +523,7 @@ static int qemu_gluster_create(const char *filename,
         }
     }
 out:
+    g_free(tmp);
     qemu_gluster_gconf_free(gconf);
     if (glfs) {
         glfs_fini(glfs);
@@ -688,18 +687,22 @@ static int qemu_gluster_has_zero_init(BlockDriverState *bs)
     return 0;
 }
 
-static QEMUOptionParameter qemu_gluster_create_options[] = {
-    {
-        .name = BLOCK_OPT_SIZE,
-        .type = OPT_SIZE,
-        .help = "Virtual disk size"
-    },
-    {
-        .name = BLOCK_OPT_PREALLOC,
-        .type = OPT_STRING,
-        .help = "Preallocation mode (allowed values: off, full)"
-    },
-    { 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"
+        },
+        {
+            .name = BLOCK_OPT_PREALLOC,
+            .type = QEMU_OPT_STRING,
+            .help = "Preallocation mode (allowed values: off, full)"
+        },
+        { /* end of list */ }
+    }
 };
 
 static BlockDriver bdrv_gluster = {
@@ -712,7 +715,7 @@ static BlockDriver bdrv_gluster = {
     .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
     .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
     .bdrv_close                   = qemu_gluster_close,
-    .bdrv_create                  = qemu_gluster_create,
+    .bdrv_create2                 = qemu_gluster_create,
     .bdrv_getlength               = qemu_gluster_getlength,
     .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
     .bdrv_truncate                = qemu_gluster_truncate,
@@ -726,7 +729,7 @@ static BlockDriver bdrv_gluster = {
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
-    .create_options               = qemu_gluster_create_options,
+    .create_opts                  = &qemu_gluster_create_opts,
 };
 
 static BlockDriver bdrv_gluster_tcp = {
@@ -739,7 +742,7 @@ static BlockDriver bdrv_gluster_tcp = {
     .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
     .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
     .bdrv_close                   = qemu_gluster_close,
-    .bdrv_create                  = qemu_gluster_create,
+    .bdrv_create2                 = qemu_gluster_create,
     .bdrv_getlength               = qemu_gluster_getlength,
     .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
     .bdrv_truncate                = qemu_gluster_truncate,
@@ -753,7 +756,7 @@ static BlockDriver bdrv_gluster_tcp = {
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
-    .create_options               = qemu_gluster_create_options,
+    .create_opts                  = &qemu_gluster_create_opts,
 };
 
 static BlockDriver bdrv_gluster_unix = {
@@ -766,7 +769,7 @@ static BlockDriver bdrv_gluster_unix = {
     .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
     .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
     .bdrv_close                   = qemu_gluster_close,
-    .bdrv_create                  = qemu_gluster_create,
+    .bdrv_create2                 = qemu_gluster_create,
     .bdrv_getlength               = qemu_gluster_getlength,
     .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
     .bdrv_truncate                = qemu_gluster_truncate,
@@ -780,7 +783,7 @@ static BlockDriver bdrv_gluster_unix = {
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
-    .create_options               = qemu_gluster_create_options,
+    .create_opts                  = &qemu_gluster_create_opts,
 };
 
 static BlockDriver bdrv_gluster_rdma = {
@@ -793,7 +796,7 @@ static BlockDriver bdrv_gluster_rdma = {
     .bdrv_reopen_commit           = qemu_gluster_reopen_commit,
     .bdrv_reopen_abort            = qemu_gluster_reopen_abort,
     .bdrv_close                   = qemu_gluster_close,
-    .bdrv_create                  = qemu_gluster_create,
+    .bdrv_create2                 = qemu_gluster_create,
     .bdrv_getlength               = qemu_gluster_getlength,
     .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
     .bdrv_truncate                = qemu_gluster_truncate,
@@ -807,7 +810,7 @@ static BlockDriver bdrv_gluster_rdma = {
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
-    .create_options               = qemu_gluster_create_options,
+    .create_opts                  = &qemu_gluster_create_opts,
 };
 
 static void bdrv_gluster_init(void)
Signature · 1 line
1.7.12.4
#16 lore raw

PATCH v27 16/33 iscsi.c: replace QEMUOptionParameter with QemuOpts

In reply to Chunyan Liu
---
 block/iscsi.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
block/iscsi.c +16 -18
index a30202b..e4a127b 100644
@@ -1391,8 +1391,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
     return 0;
 }
 
-static int iscsi_create(const char *filename, QEMUOptionParameter *options,
-                        Error **errp)
+static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
 {
     int ret = 0;
     int64_t total_size = 0;
@@ -1403,13 +1402,8 @@ static int iscsi_create(const char *filename, QEMUOptionParameter *options,
     bs = bdrv_new("", &error_abort);
 
     /* 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;
 
@@ -1460,13 +1454,17 @@ static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
     return 0;
 }
 
-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 = {
@@ -1477,8 +1475,8 @@ static BlockDriver bdrv_iscsi = {
     .bdrv_needs_filename = true,
     .bdrv_file_open  = iscsi_open,
     .bdrv_close      = iscsi_close,
-    .bdrv_create     = iscsi_create,
-    .create_options  = iscsi_create_options,
+    .bdrv_create2    = iscsi_create,
+    .create_opts     = &iscsi_create_opts,
     .bdrv_reopen_prepare  = iscsi_reopen_prepare,
 
     .bdrv_getlength  = iscsi_getlength,
Signature · 1 line
1.7.12.4
#17 lore raw
In reply to Chunyan Liu

My git-send-email died after sending 15 of 33. Resend again. 1st 15 patches might appear twice. Sorry for that.

Quoted 1 line
Quoted 1 line
Quoted 1 line

On 5/7/2014 at 05:54 PM, in message

<[email protected]>, Chunyan Liu

Quoted 92 lines · <[email protected]> wrote:

This patch series is to replace QEMUOptionParameter with QemuOpts, so that only
one Qemu Option structure is kept in QEMU code.

---
Changes to v26:
* Following Eric's comment, backward split 2/33, 3/33. (repurpose qemu_opts_print first, add def_value_str to QemuOptDesc
later).
* Fix memory free in qemu_opts_append to solve iotest issue. 10/33
* Following Eric's comment, remove the end '.' in error message. And
update

    qemu-iotests .out file. 12/33 

* Following Eric's comment, fix memory free in vvfat.c 13/33
* Following Eric's comment, split qcow2 patch into two. 19/33, 20/33:
export qemu_opt_find first, add qcow2 driver patch later.
* rebase to git master

All patches are also available from:
https://github.com/chunyanliu/qemu/commits/QemuOpts

Chunyan Liu (33):
QemuOpts: move find_desc_by_name ahead for later calling
QemuOpts: repurpose qemu_opts_print to replace
print_option_parameters
QemuOpts: add def_value_str to QemuOptDesc
qapi: output def_value_str when query command line options
QemuOpts: change opt->name|str from (const char *) to (char *)
QemuOpts: move qemu_opt_del ahead for later calling
QemuOpts: add qemu_opt_get_*_del functions for replace work
QemuOpts: add qemu_opts_print_help to replace print_option_help
QemuOpts: add conversion between QEMUOptionParameter to QemuOpts
QemuOpts: add qemu_opts_append to replace append_option_parameters
QemuOpts: check NULL input for qemu_opts_del
change block layer to support both QemuOpts and QEMUOptionParamter vvfat.c: handle cross_driver's create_options and create_opts cow.c: replace QEMUOptionParameter with QemuOpts gluster.c: replace QEMUOptionParameter with QemuOpts iscsi.c: replace QEMUOptionParameter with QemuOpts nfs.c: replace QEMUOptionParameter with QemuOpts qcow.c: replace QEMUOptionParameter with QemuOpts
QemuOpts: export qemu_opt_find
qcow2.c: replace QEMUOptionParameter with QemuOpts qed.c: replace QEMUOptionParameter with QemuOpts raw-posix.c: replace QEMUOptionParameter with QemuOpts raw-win32.c: replace QEMUOptionParameter with QemuOpts raw_bsd.c: replace QEMUOptionParameter with QemuOpts rbd.c: replace QEMUOptionParameter with QemuOpts sheepdog.c: replace QEMUOptionParameter with QemuOpts ssh.c: replace QEMUOptionParameter with QemuOpts
vdi.c: replace QEMUOptionParameter with QemuOpts vhdx.c: replace QEMUOptionParameter with QemuOpts vmdk.c: replace QEMUOptionParameter with QemuOpts vpc.c: replace QEMUOptionParameter with QemuOpts cleanup QEMUOptionParameter
QemuOpts: cleanup tmp 'allocated' member from QemuOptsList

 block.c                    |  99 ++++---- 
 block/cow.c                |  52 ++-- 
 block/gluster.c            |  73 +++--- 
 block/iscsi.c              |  32 ++- 
 block/nfs.c                |  10 +- 
 block/qcow.c               |  72 +++--- 
 block/qcow2.c              | 259 ++++++++++---------- 
 block/qed.c                | 112 +++++---- 
 block/qed.h                |   3 +- 
 block/raw-posix.c          |  55 ++--- 
 block/raw-win32.c          |  38 +-- 
 block/raw_bsd.c            |  25 +- 
 block/rbd.c                |  61 +++-- 
 block/sheepdog.c           | 102 ++++---- 
 block/ssh.c                |  30 ++- 
 block/vdi.c                |  71 +++--- 
 block/vhdx.c               |  97 ++++---- 
 block/vhdx.h               |   1 + 
 block/vmdk.c               | 121 +++++----- 
 block/vpc.c                |  60 ++--- 
 block/vvfat.c              |  13 +- 
 include/block/block.h      |   7 +- 
 include/block/block_int.h  |   9 +- 
 include/qemu/option.h      |  53 +--- 
 include/qemu/option_int.h  |   4 +- 
 qapi-schema.json           |   5 +- 
 qapi/opts-visitor.c        |  10 +- 
 qemu-img.c                 |  89 +++---- 
 qmp-commands.hx            |   2 + 
 tests/qemu-iotests/049.out |   2 +- 
 tests/qemu-iotests/061.out |   2 +- 
 util/qemu-config.c         |   4 + 
 util/qemu-option.c         | 588 ++++++++++++++++++++------------------------- 
 33 files changed, 1033 insertions(+), 1128 deletions(-) 
#18 lore raw
In reply to Chunyan Liu

Hi, any feedback about this version?

http://lists.gnu.org/archive/html/qemu-devel/2014-05/msg01098.html
http://lists.gnu.org/archive/html/qemu-devel/2014-05/msg01135.html

- Chunyan

Quoted 1 line
Quoted 1 line
Quoted 1 line

On 5/7/2014 at 06:17 PM, in message <536A0846.4C2 : 102 : 21807>, Chun Yan Liu

Quoted 101 lines · wrote:

My git-send-email died after sending 15 of 33. Resend again. 1st 15 patches might appear twice. Sorry for that.

Quoted 1 line
Quoted 1 line
Quoted 1 line

On 5/7/2014 at 05:54 PM, in message

<[email protected]>, Chunyan Liu

Quoted 83 lines · <[email protected]> wrote:

This patch series is to replace QEMUOptionParameter with QemuOpts, so that only
one Qemu Option structure is kept in QEMU code.

---
Changes to v26:
* Following Eric's comment, backward split 2/33, 3/33. (repurpose qemu_opts_print first, add def_value_str to QemuOptDesc
later).
* Fix memory free in qemu_opts_append to solve iotest issue. 10/33
* Following Eric's comment, remove the end '.' in error message. And
update

    qemu-iotests .out file. 12/33  

* Following Eric's comment, fix memory free in vvfat.c 13/33
* Following Eric's comment, split qcow2 patch into two. 19/33, 20/33:
export qemu_opt_find first, add qcow2 driver patch later.
* rebase to git master

All patches are also available from:
https://github.com/chunyanliu/qemu/commits/QemuOpts

Chunyan Liu (33):
QemuOpts: move find_desc_by_name ahead for later calling
QemuOpts: repurpose qemu_opts_print to replace
print_option_parameters
QemuOpts: add def_value_str to QemuOptDesc
qapi: output def_value_str when query command line options
QemuOpts: change opt->name|str from (const char *) to (char *)
QemuOpts: move qemu_opt_del ahead for later calling
QemuOpts: add qemu_opt_get_*_del functions for replace work
QemuOpts: add qemu_opts_print_help to replace print_option_help
QemuOpts: add conversion between QEMUOptionParameter to QemuOpts
QemuOpts: add qemu_opts_append to replace append_option_parameters
QemuOpts: check NULL input for qemu_opts_del
change block layer to support both QemuOpts and QEMUOptionParamter vvfat.c: handle cross_driver's create_options and create_opts cow.c: replace QEMUOptionParameter with QemuOpts gluster.c: replace QEMUOptionParameter with QemuOpts iscsi.c: replace QEMUOptionParameter with QemuOpts nfs.c: replace QEMUOptionParameter with QemuOpts qcow.c: replace QEMUOptionParameter with QemuOpts
QemuOpts: export qemu_opt_find
qcow2.c: replace QEMUOptionParameter with QemuOpts qed.c: replace QEMUOptionParameter with QemuOpts raw-posix.c: replace QEMUOptionParameter with QemuOpts raw-win32.c: replace QEMUOptionParameter with QemuOpts raw_bsd.c: replace QEMUOptionParameter with QemuOpts rbd.c: replace QEMUOptionParameter with QemuOpts sheepdog.c: replace QEMUOptionParameter with QemuOpts ssh.c: replace QEMUOptionParameter with QemuOpts
vdi.c: replace QEMUOptionParameter with QemuOpts vhdx.c: replace QEMUOptionParameter with QemuOpts vmdk.c: replace QEMUOptionParameter with QemuOpts vpc.c: replace QEMUOptionParameter with QemuOpts cleanup QEMUOptionParameter
QemuOpts: cleanup tmp 'allocated' member from QemuOptsList

 block.c                    |  99 ++++----  
 block/cow.c                |  52 ++--  
 block/gluster.c            |  73 +++---  
 block/iscsi.c              |  32 ++-  
 block/nfs.c                |  10 +-  
 block/qcow.c               |  72 +++---  
 block/qcow2.c              | 259 ++++++++++----------  
 block/qed.c                | 112 +++++----  
 block/qed.h                |   3 +-  
 block/raw-posix.c          |  55 ++---  
 block/raw-win32.c          |  38 +--  
 block/raw_bsd.c            |  25 +-  
 block/rbd.c                |  61 +++--  
 block/sheepdog.c           | 102 ++++----  
 block/ssh.c                |  30 ++-  
 block/vdi.c                |  71 +++---  
 block/vhdx.c               |  97 ++++----  
 block/vhdx.h               |   1 +  
 block/vmdk.c               | 121 +++++-----  
 block/vpc.c                |  60 ++---  
 block/vvfat.c              |  13 +-  
 include/block/block.h      |   7 +-  
 include/block/block_int.h  |   9 +-  
 include/qemu/option.h      |  53 +---  
 include/qemu/option_int.h  |   4 +-  
Quoted 8 lines
 qapi-schema.json           |   5 +-  
 qapi/opts-visitor.c        |  10 +-  
 qemu-img.c                 |  89 +++----  
 qmp-commands.hx            |   2 +  
 tests/qemu-iotests/049.out |   2 +-  
 tests/qemu-iotests/061.out |   2 +-  
 util/qemu-config.c         |   4 +  
 util/qemu-option.c         | 588  

++++++++++++++++++++-------------------------

Quoted 1 line
 33 files changed, 1033 insertions(+), 1128 deletions(-)