Re: [PATCH 5/8] qdev: improve find_device_state() to distinguish simple not found case

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



16.09.2021 13:48, Markus Armbruster wrote:
Vladimir Sementsov-Ogievskiy <vsementsov@xxxxxxxxxxxxx> writes:

We'll need this for realizing qdev_find_child() in the next commit.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@xxxxxxxxxxxxx>
---
  softmmu/qdev-monitor.c | 48 +++++++++++++++++++++++++++++-------------
  1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
index 721dec2d82..0117989009 100644
--- a/softmmu/qdev-monitor.c
+++ b/softmmu/qdev-monitor.c
@@ -819,7 +819,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
      object_unref(OBJECT(dev));
  }
-static DeviceState *find_device_state(const char *id, Error **errp)
+/*
+ * Returns: 1 when found, @dev set
+ *          0 not found, @dev and @errp untouched
+ *         <0 error, or id is ambiguous, @errp set
+ */
+static int find_device_state(const char *id, DeviceState **dev, Error **errp)
  {
      Object *obj;
@@ -835,17 +840,16 @@ static DeviceState *find_device_state(const char *id, Error **errp)
      }
if (!obj) {
-        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
-                  "Device '%s' not found", id);
-        return NULL;
+        return 0;
      }
if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
          error_setg(errp, "%s is not a hotpluggable device", id);
-        return NULL;
+        return -EINVAL;
      }
- return DEVICE(obj);
+    *dev = DEVICE(obj);
+    return 1;
  }
void qdev_unplug(DeviceState *dev, Error **errp)
@@ -894,16 +898,25 @@ void qdev_unplug(DeviceState *dev, Error **errp)
void qmp_device_del(const char *id, Error **errp)
  {
-    DeviceState *dev = find_device_state(id, errp);
-    if (dev != NULL) {
-        if (dev->pending_deleted_event) {
-            error_setg(errp, "Device %s is already in the "
-                             "process of unplug", id);
-            return;
+    int ret;
+    DeviceState *dev;
+
+    ret = find_device_state(id, &dev, errp);
+    if (ret <= 0) {
+        if (ret == 0) {
+            error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
+                      "Device '%s' not found", id);
          }
+        return;
+    }
- qdev_unplug(dev, errp);
+    if (dev->pending_deleted_event) {
+        error_setg(errp, "Device %s is already in the "
+                         "process of unplug", id);
+        return;
      }
+
+    qdev_unplug(dev, errp);
  }
void hmp_device_add(Monitor *mon, const QDict *qdict)
@@ -925,11 +938,16 @@ void hmp_device_del(Monitor *mon, const QDict *qdict)
BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
  {
+    int ret;
      DeviceState *dev;
      BlockBackend *blk;
- dev = find_device_state(id, errp);
-    if (dev == NULL) {
+    ret = find_device_state(id, &dev, errp);
+    if (ret <= 0) {
+        if (ret == 0) {
+            error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
+                      "Device '%s' not found", id);
+        }
          return NULL;
      }

Awkward.

Before, find_device_state() either finds something (and returns it) or
doesn't (and sets @errp).

Afterward, it can fail to find in two ways, and only one of it sets
@errp.  The existing callers laboriously fuse the two back together.
The next commit adds a caller that doesn't.

Failure modes that need to be handled differently are often the result
of a function doing too much.  Let's have a closer look at this one
before the patch:

     static DeviceState *find_device_state(const char *id, Error **errp)
     {
         Object *obj;

         if (id[0] == '/') {
             obj = object_resolve_path(id, NULL);

This interprets @id as a QOM path, and tries to resolve it.

On failure, @obj becomes NULL.  On success, it points to an object of
arbitrary type.

         } else {
             char *root_path = object_get_canonical_path(qdev_get_peripheral());
             char *path = g_strdup_printf("%s/%s", root_path, id);

             g_free(root_path);
             obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
             g_free(path);

This interprets @id as qdev ID, maps it to a QOM path, and tries to
resolve it to a TYPE_DEVICE.  Fails when the path doesn't resolve, and
when it resolves to something that isn't a TYPE_DEVICE.  The latter
can't happen as long as we put only devices under /machine/peripheral/.

On failure, @obj becomes NULL.  On success, it points to a TYPE_DEVICE
object.

         }

         if (!obj) {
             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                       "Device '%s' not found", id);
             return NULL;
         }

         if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
             error_setg(errp, "%s is not a hotpluggable device", id);
             return NULL;
         }

Unclean.

If we somehow ended up with a non-device /machine/peripheral/foo, then
find_device_state("foo", errp) would fail the first way, but
find_device_state("/machine/peripheral/foo", errp) would fail the second
way.  They should fail the exact same way instead.

         return DEVICE(obj);
     }

Better:

     static DeviceState *find_device_state(const char *id, Error **errp)
     {
         Object *obj;
         DeviceState *dev;

         if (id[0] == '/') {
             obj = object_resolve_path(id, NULL);
         } else {
             obj = object_resolve_path_component(qdev_get_peripheral(), id);
         }

         if (!obj) {
             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                       "Device '%s' not found", id);
             return NULL;
         }

         dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
         if (!dev) {
             error_setg(errp, "%s is not a hotpluggable device", id);
             return NULL;
         }

         return dev;
     }

Looks simpler)


I'll post this as a cleanup patch.

Note that this function does two things, one after the other, namely
1. resolve a "qdev ID or qom path" string, and 2. convert to
TYPE_DEVICE, with error checking.

Factor out the core of 1. into its own helper resolve_id_or_qom_path(),
and the next commit can do something like

     obj = resolve_id_or_qom_path(parent_id);
     if (!obj) {
         return 0;
     }

     dev = object_dynamic_cast(obj, TYPE_DEVICE);
     if (!dev) {
         error_setg(errp, ...);
         return -EINVAL;
     }


Thanks for working that out, I'll go this way in v2

--
Best regards,
Vladimir




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux