1. 06 7月, 2016 10 次提交
    • E
      qapi: Add new clone visitor · a15fcc3c
      Eric Blake 提交于
      We have a couple places in the code base that want to deep-clone
      one QAPI object into another, and they were resorting to serializing
      the struct out to QObject then reparsing it.  A much more efficient
      version can be done by adding a new clone visitor.
      
      Since cloning is still relatively uncommon, expose the use of the
      new visitor via a QAPI_CLONE() macro that takes care of type-punning
      the underlying function pointer, rather than generating lots of
      unused functions for types that won't be cloned.  And yes, we're
      relying on the compiler treating all pointers equally, even though
      a strict C program cannot portably do so - but we're not the first
      one in the qemu code base to expect it to work (hello, glib!).
      
      The choice of adding a fourth visitor type deserves some explanation.
      On the surface, the clone visitor is mostly an input visitor (it
      takes arbitrary input - in this case, another QAPI object - and
      creates a new QAPI object during the course of the visit).  But
      ever since commit da72ab0 consolidated enum visits based on the
      visitor type, using VISITOR_INPUT would cause us to run
      visit_type_str(), even though for cloning there is nothing to do
      (we just copy the enum value across, without regards to its mapping
      to strings).   Also, since our input happens to be a QAPI object,
      we can also satisfy the internal checks for VISITOR_OUTPUT.  So in
      the end, I settled with a new VISITOR_CLONE, and chose its value
      such that many internal checks can use 'v->type & mask', sticking
      to 'v->type == value' where the difference matters.
      
      Note that we can only clone objects (including alternates) and lists,
      not built-ins or enums.  The visitor core hides integer width from
      the actual visitor (since commit 04e070d2), and as long as that's the
      case, we can't clone top-level integers.  Then again, those can
      always be cloned by direct copy, since they are not objects with
      deep pointers, so it's no real loss.  And restricting cloning to
      just objects and lists is cleaner than restricting it to non-integers.
      As such, I documented that the clone visitor is for direct use only
      by code internal to QAPI, and should not be used on incomplete objects
      (other than a hack to work around the fact that we allow NULL in place
      of "" in visit_type_str() in other output visitors).  Note that as
      written, the clone visitor will never fail on a complete object.
      
      Scalars (including enums) not at the root of the clone copy just fine
      with no additional effort while visiting the scalar, by virtue of a
      g_memdup() each time we push another struct onto the stack.  Cloning
      a string requires deduplication of a pointer, which means it can also
      provide the guarantee of an input visitor of never producing NULL
      even when still accepting NULL in place of "" the way the QMP output
      visitor does.
      
      Cloning an 'any' type could be possible by incrementing the QObject
      refcnt, but it's not obvious whether that is better than implementing
      a QObject deep clone.  So for now, we document it as unsupported,
      and intentionally omit the .type_any() callback to let a developer
      know their usage needs implementation.
      
      Add testsuite coverage for several different clone situations, to
      ensure that the code is working.  I also tested that valgrind was
      happy with the test.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-14-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      a15fcc3c
    • E
      qapi: Add new visit_complete() function · 3b098d56
      Eric Blake 提交于
      Making each output visitor provide its own output collection
      function was the only remaining reason for exposing visitor
      sub-types to the rest of the code base.  Add a polymorphic
      visit_complete() function which is a no-op for input visitors,
      and which populates an opaque pointer for output visitors.  For
      maximum type-safety, also add a parameter to the output visitor
      constructors with a type-correct version of the output pointer,
      and assert that the two uses match.
      
      This approach was considered superior to either passing the
      output parameter only during construction (action at a distance
      during visit_free() feels awkward) or only during visit_complete()
      (defeating type safety makes it easier to use incorrectly).
      
      Most callers were function-local, and therefore a mechanical
      conversion; the testsuite was a bit trickier, but the previous
      cleanup patch minimized the churn here.
      
      The visit_complete() function may be called at most once; doing
      so lets us use transfer semantics rather than duplication or
      ref-count semantics to get the just-built output back to the
      caller, even though it means our behavior is not idempotent.
      
      Generated code is simplified as follows for events:
      
      |@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP
      |     QDict *qmp;
      |     Error *err = NULL;
      |     QMPEventFuncEmit emit;
      |-    QmpOutputVisitor *qov;
      |+    QObject *obj;
      |     Visitor *v;
      |     q_obj_ACPI_DEVICE_OST_arg param = {
      |         info
      |@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP
      |
      |     qmp = qmp_event_build_dict("ACPI_DEVICE_OST");
      |
      |-    qov = qmp_output_visitor_new();
      |-    v = qmp_output_get_visitor(qov);
      |+    v = qmp_output_visitor_new(&obj);
      |
      |     visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err);
      |     if (err) {
      |@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP
      |         goto out;
      |     }
      |
      |-    qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
      |+    visit_complete(v, &obj);
      |+    qdict_put_obj(qmp, "data", obj);
      |     emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);
      
      and for commands:
      
      | {
      |     Error *err = NULL;
      |-    QmpOutputVisitor *qov = qmp_output_visitor_new();
      |     Visitor *v;
      |
      |-    v = qmp_output_get_visitor(qov);
      |+    v = qmp_output_visitor_new(ret_out);
      |     visit_type_AddfdInfo(v, "unused", &ret_in, &err);
      |-    if (err) {
      |-        goto out;
      |+    if (!err) {
      |+        visit_complete(v, ret_out);
      |     }
      |-    *ret_out = qmp_output_get_qobject(qov);
      |-
      |-out:
      |     error_propagate(errp, err);
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-13-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      3b098d56
    • E
      qmp-output-visitor: Favor new visit_free() function · 1830f22a
      Eric Blake 提交于
      Now that we have a polymorphic visit_free(), we no longer need
      qmp_output_visitor_cleanup(); however, we still need to
      expose the subtype for qmp_output_get_qobject().
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-10-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      1830f22a
    • E
      string-output-visitor: Favor new visit_free() function · e7ca5656
      Eric Blake 提交于
      Now that we have a polymorphic visit_free(), we no longer need
      string_output_visitor_cleanup(); however, we still need to
      expose the subtype for string_output_get_string().
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-9-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      e7ca5656
    • E
      qmp-input-visitor: Favor new visit_free() function · b70ce101
      Eric Blake 提交于
      Now that we have a polymorphic visit_free(), we no longer need
      qmp_input_visitor_cleanup(); which in turn means we no longer
      need to return a subtype from qmp_input_visitor_new() nor a
      public upcast function.
      
      Generated code changes to qmp-marshal.c look like:
      
      |@@ -52,11 +52,10 @@ void qmp_marshal_add_fd(QDict *args, QOb
      | {
      |     Error *err = NULL;
      |     AddfdInfo *retval;
      |-    QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
      |     Visitor *v;
      |     q_obj_add_fd_arg arg = {0};
      |
      |-    v = qmp_input_get_visitor(qiv);
      |+    v = qmp_input_visitor_new(QOBJECT(args), true);
      |     visit_start_struct(v, NULL, NULL, 0, &err);
      |     if (err) {
      |         goto out;
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-8-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      b70ce101
    • E
      string-input-visitor: Favor new visit_free() function · 7a0525c7
      Eric Blake 提交于
      Now that we have a polymorphic visit_free(), we no longer need
      string_input_visitor_cleanup(); which in turn means we no longer
      need to return a subtype from string_input_visitor_new() nor a
      public upcast function.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-7-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      7a0525c7
    • E
      opts-visitor: Favor new visit_free() function · 09204eac
      Eric Blake 提交于
      Now that we have a polymorphic visit_free(), we no longer need
      opts_visitor_cleanup(); which in turn means we no longer need
      to return a subtype from opts_visitor_new() nor a public upcast
      function.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-6-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      09204eac
    • E
      qapi: Add new visit_free() function · 2c0ef9f4
      Eric Blake 提交于
      Making each visitor provide its own (awkwardly-named) FOO_cleanup()
      is unusual, when we can instead have a polymorphic visit_free()
      interface.  Over the next few patches, we can use the polymorphic
      functions to eliminate the need for a FOO_get_visitor() function
      for accessing specific visitor functionality, once everything can
      be accessed directly through the Visitor* interfaces.
      
      The dealloc visitor is the first one converted to completely use
      the new entry point, since qapi_dealloc_visitor_cleanup() was the
      only reason that qapi_dealloc_get_visitor() existed, and only
      generated and testsuite code was even using it.  With the new
      visit_free() entry point in place, we no longer need to expose
      the QapiDeallocVisitor subtype through qapi_dealloc_visitor_new(),
      and can get by with less generated code, with diffs that look like:
      
      | void qapi_free_ACPIOSTInfo(ACPIOSTInfo *obj)
      | {
      |-    QapiDeallocVisitor *qdv;
      |     Visitor *v;
      |
      |     if (!obj) {
      |         return;
      |     }
      |
      |-    qdv = qapi_dealloc_visitor_new();
      |-    v = qapi_dealloc_get_visitor(qdv);
      |+    v = qapi_dealloc_visitor_new();
      |     visit_type_ACPIOSTInfo(v, NULL, &obj, NULL);
      |-    qapi_dealloc_visitor_cleanup(qdv);
      |+    visit_free(v);
      |}
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-5-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      2c0ef9f4
    • E
      qapi: Add parameter to visit_end_* · 1158bb2a
      Eric Blake 提交于
      Rather than making the dealloc visitor track of stack of pointers
      remembered during visit_start_* in order to free them during
      visit_end_*, it's a lot easier to just make all callers pass the
      same pointer to visit_end_*.  The generated code has access to the
      same pointer, while all other users are doing virtual walks and
      can pass NULL.  The dealloc visitor is then greatly simplified.
      
      All three visit_end_*() functions intentionally take a void**,
      even though the visit_start_*() functions differ between void**,
      GenericList**, and GenericAlternate**.  This is done for several
      reasons: when doing a virtual walk, passing NULL doesn't care
      what the type is, but when doing a generated walk, we already
      have to cast the caller's specific FOO* to call visit_start,
      while using void** lets us use visit_end without a cast. Also,
      an upcoming patch will add a clone visitor that wants to use
      the same implementation for all three visit_end callbacks,
      which is made easier if all three share the same signature.
      
      For visitors with already track per-object state (the QMP visitors
      via a stack, and the string visitors which do not allow nesting),
      add an assertion that the caller is indeed passing the same
      pointer to paired calls.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-4-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      1158bb2a
    • E
      qapi: Improve use of qmp/types.h · c7eb39cb
      Eric Blake 提交于
      'qjson.h' is not a QObject subtype; include this file directly in
      .c files that are using it, rather than abusing qmp/types.h for
      that purpose.
      
      Meanwhile, for files that include a list of individual QObject
      subtypes, it's easier to just use qmp/types.h for that purpose.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1465490926-28625-2-git-send-email-eblake@redhat.com>
      Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      c7eb39cb
  2. 05 7月, 2016 1 次提交
    • E
      blkdebug: Set request_alignment during .bdrv_refresh_limits() · 835db3ee
      Eric Blake 提交于
      We want to eventually stick request_alignment alongside other
      BlockLimits, but first, we must ensure it is populated at the
      same time as all other limits, rather than being a special case
      that is set only when a block is first opened.
      
      Note that when the user does not provide "align", then we were
      defaulting to bs->request_alignment - but at this stage in the
      initialization, that was always 512.  We were also rejecting an
      explicit "align":0 from the user; this patch now allows that,
      as an explicit request for the default alignment (which may not
      always be 512 in the future).
      
      qemu-iotests 77 is particularly sensitive to the fact that we
      can specify an artificial alignment override in blkdebug, and
      that override must continue to work even when limits are
      refreshed on an already open device.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Reviewed-by: NFam Zheng <famz@redhat.com>
      Signed-off-by: NKevin Wolf <kwolf@redhat.com>
      835db3ee
  3. 04 7月, 2016 2 次提交
  4. 30 6月, 2016 1 次提交
    • E
      qapi: Simplify use of range.h · 7c47959d
      Eric Blake 提交于
      Calling our function g_list_insert_sorted_merged is a misnomer,
      since we are NOT writing a glib function.  Furthermore, we are
      making every caller pass the same comparator function of
      range_merge(): any caller that would try otherwise would break
      in weird ways since our internal call to ranges_can_merge() is
      hard-coded to operate only on ranges, rather than paying
      attention to the caller's comparator.
      
      Better is to fix things so that callers don't have to care about
      our internal comparator, by picking a function name and updating
      the parameter type away from a gratuitous use of void*, to make
      it obvious that we are operating specifically on a list of ranges
      and not a generic list.  Plus, refactoring the code here will
      make it easier to plug a memory leak in the next patch.
      
      range_compare() is now internal only, and moves to the .c file.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1464712890-14262-3-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      7c47959d
  5. 07 6月, 2016 1 次提交
  6. 12 5月, 2016 17 次提交
    • W
      qmp: add monitor command to add/remove a child · 7f821597
      Wen Congyang 提交于
      The new QMP command name is x-blockdev-change. It's just for adding/removing
      quorum's child now, and doesn't support all kinds of children, all kinds of
      operations, nor all block drivers. So it is experimental now.
      Signed-off-by: NWen Congyang <wency@cn.fujitsu.com>
      Signed-off-by: Nzhanghailiang <zhang.zhanghailiang@huawei.com>
      Signed-off-by: NGonglei <arei.gonglei@huawei.com>
      Signed-off-by: NChanglong Xie <xiecl.fnst@cn.fujitsu.com>
      Reviewed-by: NMax Reitz <mreitz@redhat.com>
      Reviewed-by: NAlberto Garcia <berto@igalia.com>
      Message-id: 1462865799-19402-4-git-send-email-xiecl.fnst@cn.fujitsu.com
      Signed-off-by: NMax Reitz <mreitz@redhat.com>
      7f821597
    • E
      qapi: Change visit_type_FOO() to no longer return partial objects · 68ab47e4
      Eric Blake 提交于
      Returning a partial object on error is an invitation for a careless
      caller to leak memory.  We already fixed things in an earlier
      patch to guarantee NULL if visit_start fails ("qapi: Guarantee
      NULL obj on input visitor callback error"), but that does not
      help the case where visit_start succeeds but some other failure
      happens before visit_end, such that we leak a partially constructed
      object outside visit_type_FOO(). As no one outside the testsuite
      was actually relying on these semantics, it is cleaner to just
      document and guarantee that ALL pointer-based visit_type_FOO()
      functions always leave a safe value in *obj during an input visitor
      (either the new object on success, or NULL if an error is
      encountered), so callers can now unconditionally use
      qapi_free_FOO() to clean up regardless of whether an error occurred.
      
      The decision is done by adding visit_is_input(), then updating the
      generated code to check if additional cleanup is needed based on
      the type of visitor in use.
      
      Note that we still leave *obj unchanged after a scalar-based
      visit_type_FOO(); I did not feel like auditing all uses of
      visit_type_Enum() to see if the callers would tolerate a specific
      sentinel value (not to mention having to decide whether it would
      be better to use 0 or ENUM__MAX as that sentinel).
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-25-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      68ab47e4
    • E
      qapi: Simplify semantics of visit_next_list() · d9f62dde
      Eric Blake 提交于
      The semantics of the list visit are somewhat baroque, with the
      following pseudocode when FooList is used:
      
      start()
      for (prev = head; cur = next(prev); prev = &cur) {
          visit(&cur->value)
      }
      
      Note that these semantics (advance before visit) requires that
      the first call to next() return the list head, while all other
      calls return the next element of the list; that is, every visitor
      implementation is required to track extra state to decide whether
      to return the input as-is, or to advance.  It also requires an
      argument of 'GenericList **' to next(), solely because the first
      iteration might need to modify the caller's GenericList head, so
      that all other calls have to do a layer of dereferencing.
      
      Thankfully, we only have two uses of list visits in the entire
      code base: one in spapr_drc (which completely avoids
      visit_next_list(), feeding in integers from a different source
      than uint8List), and one in qapi-visit.py.  That is, all other
      list visitors are generated in qapi-visit.c, and share the same
      paradigm based on a qapi FooList type, so we can refactor how
      lists are laid out with minimal churn among clients.
      
      We can greatly simplify things by hoisting the special case
      into the start() routine, and flipping the order in the loop
      to visit before advance:
      
      start(head)
      for (tail = *head; tail; tail = next(tail)) {
          visit(&tail->value)
      }
      
      With the simpler semantics, visitors have less state to track,
      the argument to next() is reduced to 'GenericList *', and it
      also becomes obvious whether an input visitor is allocating a
      FooList during visit_start_list() (rather than the old way of
      not knowing if an allocation happened until the first
      visit_next_list()).  As a minor drawback, we now allocate in
      two functions instead of one, and have to pass the size to
      both functions (unless we were to tweak the input visitors to
      cache the size to start_list for reuse during next_list, but
      that defeats the goal of less visitor state).
      
      The signature of visit_start_list() is chosen to match
      visit_start_struct(), with the new parameters after 'name'.
      
      The spapr_drc case is a virtual visit, done by passing NULL for
      list, similarly to how NULL is passed to visit_start_struct()
      when a qapi type is not used in those visits.  It was easy to
      provide these semantics for qmp-output and dealloc visitors,
      and a bit harder for qmp-input (several prerequisite patches
      refactored things to make this patch straightforward).  But it
      turned out that the string and opts visitors munge enough other
      state during visit_next_list() to make it easier to just
      document and require a GenericList visit for now; an assertion
      will remind us to adjust things if we need the semantics in the
      future.
      
      Several pre-requisite cleanup patches made the reshuffling of
      the various visitors easier; particularly the qmp input visitor.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-24-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      d9f62dde
    • E
      qapi: Fix string input visitor handling of invalid list · 74f24cb6
      Eric Blake 提交于
      As shown in the previous commit, the string input visitor was
      treating bogus input as an empty list rather than an error.
      Fix parse_str() to set errp, then the callers to exit early if
      an error was reported.
      
      Meanwhile, fix the testsuite to use the generated
      qapi_free_int16List() instead of rolling our own, and to
      validate the fixed behavior, while at the same time documenting
      one more change that we'd like to make in a later patch (a
      failed visit_start_list should guarantee a NULL pointer,
      regardless of what things were on input).
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-23-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      74f24cb6
    • E
      qapi: Split visit_end_struct() into pieces · 15c2f669
      Eric Blake 提交于
      As mentioned in previous patches, we want to call visit_end_struct()
      functions unconditionally, so that visitors can release resources
      tied up since the matching visit_start_struct() without also having
      to worry about error priority if more than one error occurs.
      
      Even though error_propagate() can be safely used to ignore a second
      error during cleanup caused by a first error, it is simpler if the
      cleanup cannot set an error.  So, split out the error checking
      portion (basically, input visitors checking for unvisited keys) into
      a new function visit_check_struct(), which can be safely skipped if
      any earlier errors are encountered, and leave the cleanup portion
      (which never fails, but must be called unconditionally if
      visit_start_struct() succeeded) in visit_end_struct().
      
      Generated code in qapi-visit.c has diffs resembling:
      
      |@@ -59,10 +59,12 @@ void visit_type_ACPIOSTInfo(Visitor *v,
      |         goto out_obj;
      |     }
      |     visit_type_ACPIOSTInfo_members(v, obj, &err);
      |-    error_propagate(errp, err);
      |-    err = NULL;
      |+    if (err) {
      |+        goto out_obj;
      |+    }
      |+    visit_check_struct(v, &err);
      | out_obj:
      |-    visit_end_struct(v, &err);
      |+    visit_end_struct(v);
      | out:
      
      and in qapi-event.c:
      
      @@ -47,7 +47,10 @@ void qapi_event_send_acpi_device_ost(ACP
      |         goto out;
      |     }
      |     visit_type_q_obj_ACPI_DEVICE_OST_arg_members(v, &param, &err);
      |-    visit_end_struct(v, err ? NULL : &err);
      |+    if (!err) {
      |+        visit_check_struct(v, &err);
      |+    }
      |+    visit_end_struct(v);
      |     if (err) {
      |         goto out;
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-20-git-send-email-eblake@redhat.com>
      [Conflict with a doc fixup resolved]
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      15c2f669
    • E
      qmp: Tighten output visitor rules · 56a6f02b
      Eric Blake 提交于
      Tighten assertions in the QMP output visitor, so that:
      
      - qmp_output_get_qobject() can only be called after pairing a
      visit_end_* for every visit_start_* (rather than allowing it on
      a partially built object)
      
      - qmp_output_get_qobject() cannot be called unless at least one
      visit_type_* or visit_start/visit_end pair has occurred since
      creation/reset (the accidental return of NULL fixed by commit
      ab8bf1d7 would have been much easier to diagnose)
      
      - ensure that we are encountering the expected object or list
      type, to provide protection against mismatched push(struct)/
      pop(list) or push(list)/pop(struct), similar to the qmp-input
      protection added in commit bdd8e6b5.
      
      - ensure that except for the root, 'name' is non-null inside a
      dict, and NULL inside a list (this may need changing later if
      we add "name.0" support for better error messages for a list,
      but for now it makes sure all users are at least consistent)
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-19-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      56a6f02b
    • E
      qmp: Support explicit null during visits · 3df016f1
      Eric Blake 提交于
      Implement the new type_null() callback for the qmp input and
      output visitors. While we don't yet have a use for this in QAPI
      input (the generator will need some tweaks first), some
      potential usages have already been discussed on the list.
      Meanwhile, the output visitor could already output explicit null
      via type_any, but this gives us finer control.
      
      At any rate, it's easy to test that we can round-trip an explicit
      null through manual use of visit_type_null() wrapped by a virtual
      visit_start_struct() walk, even if we can't do the visit in a
      QAPI type.  Repurpose the test_visitor_out_empty test,
      particularly since a future patch will tighten semantics to
      forbid use of qmp_output_get_qobject() without at least one
      intervening visit_type_*.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-16-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      3df016f1
    • E
      qapi: Add visit_type_null() visitor · 3bc97fd5
      Eric Blake 提交于
      Right now, qmp-output-visitor happens to produce a QNull result
      if nothing is actually visited between the creation of the visitor
      and the request for the resulting QObject.  A stronger protocol
      would require that a QMP output visit MUST visit something.  But
      to still be able to produce a JSON 'null' output, we need a new
      visitor function that states our intentions.  Yes, we could say
      that such a visit must go through visit_type_any(), but that
      feels clunky.
      
      So this patch introduces the new visit_type_null() interface and
      its no-op interface in the dealloc visitor, and stubs in the
      qmp visitors (the next patch will finish the implementation).
      For the visitors that will not implement the callback, document
      the situation. The code in qapi-visit-core unconditionally
      dereferences the callback pointer, so that a segfault will inform
      a developer if they need to implement the callback for their
      choice of visitor.
      
      Note that JSON has a primitive null type, with the single value
      null; likewise with the QNull type for QObject; but for QAPI,
      we just have the 'null' value without a null type.  We may
      eventually want to add more support in QAPI for null (most likely,
      we'd use it via an alternate type that permits 'null' or an
      object); but we'll create that usage when we need it.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-15-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      3bc97fd5
    • E
      qapi: Document visitor interfaces, add assertions · adfb264c
      Eric Blake 提交于
      The visitor interface for mapping between QObject/QemuOpts/string
      and QAPI is scandalously under-documented, making changes to visitor
      core, individual visitors, and users of visitors difficult to
      coordinate.  Among other questions: when is it safe to pass NULL,
      vs. when a string must be provided; which visitors implement which
      callbacks; the difference between concrete and virtual visits.
      
      Correct this by retrofitting proper contracts, and document where some
      of the interface warts remain (for example, we may want to modify
      visit_end_* to require the same 'obj' as the visit_start counterpart,
      so the dealloc visitor can be simplified).  Later patches in this
      series will tackle some, but not all, of these warts.
      
      Add assertions to (partially) enforce the contract.  Some of these
      were only made possible by recent cleanup commits.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-13-git-send-email-eblake@redhat.com>
      [Doc fix from Eric squashed in]
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      adfb264c
    • E
      qmp-input: Refactor when list is advanced · fcf3cb21
      Eric Blake 提交于
      In the QMP input visitor, visiting a list traverses two objects:
      the QAPI GenericList of the caller (which gets advanced in
      visit_next_list() regardless of this patch), and the QList input
      that we are converting to QAPI.  For consistency with QDict
      visits, we want to consume elements from the input QList during
      the visit_type_FOO() for the list element; that is, we want ALL
      the code for consuming an input to live in qmp_input_get_object(),
      rather than having it split according to whether we are visiting
      a dict or a list.  Making qmp_input_get_object() the common point
      of consumption will make it easier for a later patch to refactor
      visit_start_list() to cover the GenericList * head of a QAPI list,
      and in turn will get rid of the 'first' flag (which lived in
      qmp_input_next_list() pre-patch, and is hoisted to StackObject
      by this patch).
      
      This patch is therefore altering the post-condition use of 'entry',
      while keeping what gets visited unchanged, from:
      
              start_list next_list type_ELT ... next_list type_ELT next_list end_list
       visits                      1st elt                last elt
       entry  NULL       1st elt   1st elt      last elt  last elt NULL      gone
      
      where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps
      entry
      
      to this usage:
      
              start_list next_list type_ELT ... next_list type_ELT next_list end_list
       visits                      1st elt                last elt
       entry  1st elt    1nd elt   2nd elt      last elt  NULL     NULL      gone
      
      where type_ELT() steps entry and returns the old entry, and next_list()
      leaves entry alone.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-12-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      fcf3cb21
    • E
      qmp-input: Require struct push to visit members of top dict · ce140b17
      Eric Blake 提交于
      Don't embed the root of the visit into the stack of current
      containers being visited.  That way, we no longer get confused
      on whether the first visit of a dictionary is to the dictionary
      itself or to one of the members of the dictionary, based on
      whether the caller passed name=NULL; and makes the QMP Input
      visitor like other visitors where the value of 'name' is now
      ignored on the root visit.  (We may someday want to revisit
      the rules on what 'name' should be on a top-level visit,
      rather than just ignoring it; but that would be the topic of
      another patch).
      
      An audit of all qmp_input_visitor_new() call sites shows that
      there were only two places where callers had previously been
      visiting to a QDict with a non-NULL name to bypass a call to
      visit_start_struct(), and those were fixed in prior patches.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-11-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      ce140b17
    • E
      qmp-input: Don't consume input when checking has_member · e5826a2f
      Eric Blake 提交于
      Commit e8316d7e mistakenly passed consume=true within
      qmp_input_optional() when checking if an optional member was
      present, but the mistake was silently ignored since the code
      happily let us extract a member more than once.  Fix
      qmp_input_optional() to not consume anything, then tighten up
      the input visitor to ensure that a member is consumed exactly
      once (all generated code follows this pattern; and the new
      assert will catch any hand-written code that tries to visit
      the same key more than once).
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-8-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      e5826a2f
    • E
      qapi: Consolidate QMP input visitor creation · fc471c18
      Eric Blake 提交于
      Rather than having two separate ways to create a QMP input
      visitor, where the safer approach has the more verbose name,
      it is better to consolidate things into a single function
      where the caller must explicitly choose whether to be strict
      or to ignore excess input.  This patch is the strictly
      mechanical conversion; the next patch will then audit which
      uses can be made stricter.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-6-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      fc471c18
    • E
      qmp-input: Clean up stack handling · b471d012
      Eric Blake 提交于
      Management of the top of stack was a bit verbose; creating a
      temporary variable and adding some comments makes the existing
      code more legible before the next few patches improve things.
      No semantic changes other than asserting that we are always
      visiting a QObject, and not a NULL value.  In particular, the
      check for 'name && qobject_type(qobj) == QTYPE_QDICT)' is a
      bit overkill (a dict visit should always have a name); a later
      patch revisits that, while this patch is only changing one
      layer of indentation due to dropping 'if (qobj)'.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-5-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      b471d012
    • E
      qmp: Drop dead command->type · 42a502a7
      Eric Blake 提交于
      Ever since QMP was first added back in commit 43c20a43, we have
      never had any QmpCommandType other than QCT_NORMAL.  It's
      pointless to carry around the cruft.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-4-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      42a502a7
    • E
      qapi: Guarantee NULL obj on input visitor callback error · e58d695e
      Eric Blake 提交于
      Our existing input visitors were not very consistent on errors in a
      function taking 'TYPE **obj'.  These are start_struct(),
      start_alternate(), type_str(), and type_any().  next_list() is
      similar, but can't fail (see commit 08f9541d).  While all of them set
      '*obj' to allocated storage on success, it was not obvious whether
      '*obj' was guaranteed safe on failure, or whether it was left
      uninitialized.  But a future patch wants to guarantee that
      visit_type_FOO() does not leak a partially-constructed obj back to
      the caller; it is easier to implement this if we can reliably state
      that input visitors assign '*obj' regardless of success or failure,
      and that on failure *obj is NULL.  Add assertions to enforce
      consistency in the final setting of err vs. *obj.
      
      The opts-visitor start_struct() doesn't set an error, but it
      also was doing a weird check for 0 size; all callers pass in
      non-zero size if obj is non-NULL.
      
      The testsuite has at least one spot where we no longer need
      to pre-initialize a variable prior to a visit; valgrind confirms
      that the test is still fine with the cleanup.
      
      A later patch will document the design constraint implemented
      here.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-3-git-send-email-eblake@redhat.com>
      [visit_start_alternate()'s assertion tightened, commit message tweaked]
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      e58d695e
    • E
      qapi-visit: Add visitor.type classification · 983f52d4
      Eric Blake 提交于
      We have three classes of QAPI visitors: input, output, and dealloc.
      Currently, all implementations of these visitors have one thing in
      common based on their visitor type: the implementation used for the
      visit_type_enum() callback.  But since we plan to add more such
      common behavior, in relation to documenting and further refining
      the semantics, it makes more sense to have the visitor
      implementations advertise which class they belong to, so the common
      qapi-visit-core code can use that information in multiple places.
      
      A later patch will better document the types of visitors directly
      in visitor.h.
      
      For this patch, knowing the class of a visitor implementation lets
      us make input_type_enum() and output_type_enum() become static
      functions, by replacing the callback function Visitor.type_enum()
      with the simpler enum member Visitor.type.  Share a common
      assertion in qapi-visit-core as part of the refactoring.
      
      Move comments in opts-visitor.c to match the refactored layout.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1461879932-9020-2-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      983f52d4
  7. 29 4月, 2016 1 次提交
  8. 30 3月, 2016 2 次提交
    • D
      block: add generic full disk encryption driver · 78368575
      Daniel P. Berrange 提交于
      Add a block driver that is capable of supporting any full disk
      encryption format. This utilizes the previously added block
      encryption code, and at this time supports the LUKS format.
      
      The driver code is capable of supporting any format supported
      by the QCryptoBlock module, so it registers one block driver
      for each format. This patch only registers the "luks" driver
      since the "qcow" driver is there only for back-compatibility
      with existing qcow built-in encryption.
      
      New LUKS compatible volumes can be formatted using qemu-img
      with defaults for all settings.
      
      $ qemu-img create --object secret,data=123456,id=sec0 \
            -f luks -o key-secret=sec0 demo.luks 10G
      
      Alternatively the cryptographic settings can be explicitly
      set
      
      $ qemu-img create --object secret,data=123456,id=sec0 \
            -f luks -o key-secret=sec0,cipher-alg=aes-256,\
                       cipher-mode=cbc,ivgen-alg=plain64,hash-alg=sha256 \
            demo.luks 10G
      
      And query its size
      
      $ qemu-img info demo.img
      image: demo.img
      file format: luks
      virtual size: 10G (10737418240 bytes)
      disk size: 132K
      encrypted: yes
      
      Note that it was not necessary to provide the password
      when querying info for the volume. The password is only
      required when performing I/O on the volume
      
      All volumes created by this new 'luks' driver should be
      capable of being opened by the kernel dm-crypt driver.
      
      The only algorithms listed in the LUKS spec that are
      not currently supported by this impl are sha512 and
      ripemd160 hashes and cast6 cipher.
      Reviewed-by: NEric Blake <eblake@redhat.com>
      Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
      [ kwolf - Added #include to resolve conflict with da34e65c ]
      Signed-off-by: NKevin Wolf <kwolf@redhat.com>
      78368575
    • K
      block: Remove cache.writeback from blockdev-add · aaa436f9
      Kevin Wolf 提交于
      The WCE bit is a frontend property and should not be part of the backend
      configuration. This is especially important because the same BDS can be
      used by different users with different WCE requirements.
      Signed-off-by: NKevin Wolf <kwolf@redhat.com>
      aaa436f9
  9. 23 3月, 2016 2 次提交
    • V
      util: move declarations out of qemu-common.h · f348b6d1
      Veronia Bahaa 提交于
      Move declarations out of qemu-common.h for functions declared in
      utils/ files: e.g. include/qemu/path.h for utils/path.c.
      Move inline functions out of qemu-common.h and into new files (e.g.
      include/qemu/bcd.h)
      Signed-off-by: NVeronia Bahaa <veroniabahaa@gmail.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      f348b6d1
    • M
      include/qemu/osdep.h: Don't include qapi/error.h · da34e65c
      Markus Armbruster 提交于
      Commit 57cb38b3 included qapi/error.h into qemu/osdep.h to get the
      Error typedef.  Since then, we've moved to include qemu/osdep.h
      everywhere.  Its file comment explains: "To avoid getting into
      possible circular include dependencies, this file should not include
      any other QEMU headers, with the exceptions of config-host.h,
      compiler.h, os-posix.h and os-win32.h, all of which are doing a
      similar job to this file and are under similar constraints."
      qapi/error.h doesn't do a similar job, and it doesn't adhere to
      similar constraints: it includes qapi-types.h.  That's in excess of
      100KiB of crap most .c files don't actually need.
      
      Add the typedef to qemu/typedefs.h, and include that instead of
      qapi/error.h.  Include qapi/error.h in .c files that need it and don't
      get it now.  Include qapi-types.h in qom/object.h for uint16List.
      
      Update scripts/clean-includes accordingly.  Update it further to match
      reality: replace config.h by config-target.h, add sysemu/os-posix.h,
      sysemu/os-win32.h.  Update the list of includes in the qemu/osdep.h
      comment quoted above similarly.
      
      This reduces the number of objects depending on qapi/error.h from "all
      of them" to less than a third.  Unfortunately, the number depending on
      qapi-types.h shrinks only a little.  More work is needed for that one.
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      [Fix compilation without the spice devel packages. - Paolo]
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      da34e65c
  10. 18 3月, 2016 2 次提交
    • E
      qapi: Use anonymous bases in QMP flat unions · 3666a97f
      Eric Blake 提交于
      Now that the generator supports it, we might as well use an
      anonymous base rather than breaking out a single-use Base
      structure, for all three of our current QMP flat unions.
      
      Oddly enough, this change does not affect the resulting
      introspection output (because we already inline the members of
      a base type into an object, and had no independent use of the
      base type reachable from a command).
      
      The case_whitelist now has to list the name of an implicit
      type; which is not too bad (consider it a feature if it makes
      it harder for developers to make the whitelist grow :)
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Message-Id: <1458254921-17042-16-git-send-email-eblake@redhat.com>
      Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
      3666a97f
    • D
      crypto: implement the LUKS block encryption format · 3e308f20
      Daniel P. Berrange 提交于
      Provide a block encryption implementation that follows the
      LUKS/dm-crypt specification.
      
      This supports all combinations of hash, cipher algorithm,
      cipher mode and iv generator that are implemented by the
      current crypto layer.
      
      There is support for opening existing volumes formatted
      by dm-crypt, and for formatting new volumes. In the latter
      case it will only use key slot 0.
      Reviewed-by: NEric Blake <eblake@redhat.com>
      Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
      3e308f20
  11. 17 3月, 2016 1 次提交
    • D
      crypto: add block encryption framework · 7d969014
      Daniel P. Berrange 提交于
      Add a generic framework for supporting different block encryption
      formats. Upon instantiating a QCryptoBlock object, it will read
      the encryption header and extract the encryption keys. It is
      then possible to call methods to encrypt/decrypt data buffers.
      
      There is also a mode whereby it will create/initialize a new
      encryption header on a previously unformatted volume.
      
      The initial framework comes with support for the legacy QCow
      AES based encryption. This enables code in the QCow driver to
      be consolidated later.
      Reviewed-by: NEric Blake <eblake@redhat.com>
      Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
      7d969014