]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nodedev: add helper functions to remove node devices
authorJonathon Jongsma <jjongsma@redhat.com>
Thu, 15 Oct 2020 16:30:35 +0000 (11:30 -0500)
committerJonathon Jongsma <jjongsma@redhat.com>
Wed, 7 Apr 2021 20:08:59 +0000 (15:08 -0500)
When a mediated device is stopped or undefined by an application outside
of libvirt, we need to remove it from our list of node devices within
libvirt. This patch introduces virNodeDeviceObjListRemoveLocked() and
virNodeDeviceObjListForEachRemove() (which are analogous to other types
of object lists in libvirt) to facilitate that. They will be used in
coming commits.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/conf/virnodedeviceobj.c
src/conf/virnodedeviceobj.h
src/libvirt_private.syms

index ce84e4d8c17240e31e857827fd32bca62c66dcc7..23fbaac8896d9b8343726decd07751c99b81947c 100644 (file)
@@ -507,23 +507,29 @@ void
 virNodeDeviceObjListRemove(virNodeDeviceObjListPtr devs,
                            virNodeDeviceObjPtr obj)
 {
-    virNodeDeviceDefPtr def;
-
     if (!obj)
         return;
-    def = obj->def;
 
     virObjectRef(obj);
     virObjectUnlock(obj);
     virObjectRWLockWrite(devs);
     virObjectLock(obj);
-    virHashRemoveEntry(devs->objs, def->name);
+    virNodeDeviceObjListRemoveLocked(devs, obj);
     virObjectUnlock(obj);
     virObjectUnref(obj);
     virObjectRWUnlock(devs);
 }
 
 
+/* The caller must hold lock on 'devs' */
+void
+virNodeDeviceObjListRemoveLocked(virNodeDeviceObjList *devs,
+                                 virNodeDeviceObj *dev)
+{
+    virHashRemoveEntry(devs->objs, dev->def->name);
+}
+
+
 /*
  * Return the NPIV dev's parent device name
  */
@@ -1019,3 +1025,47 @@ virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
 {
     obj->persistent = persistent;
 }
+
+
+struct virNodeDeviceObjListRemoveHelperData
+{
+    virNodeDeviceObjListRemoveIterator callback;
+    void *opaque;
+};
+
+static int virNodeDeviceObjListRemoveHelper(void *key G_GNUC_UNUSED,
+                                            void *value,
+                                            void *opaque)
+{
+    struct virNodeDeviceObjListRemoveHelperData *data = opaque;
+
+    return data->callback(value, data->opaque);
+}
+
+
+/**
+ * virNodeDeviceObjListForEachRemove
+ * @devs: Pointer to object list
+ * @callback: function to call for each device object
+ * @opaque: Opaque data to use as argument to helper
+ *
+ * For each object in @devs, call the @callback helper using @opaque as
+ * an argument. If @callback returns true, that item will be removed from the
+ * object list.
+ */
+void
+virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs,
+                                  virNodeDeviceObjListRemoveIterator callback,
+                                  void *opaque)
+{
+    struct virNodeDeviceObjListRemoveHelperData data = {
+        .callback = callback,
+        .opaque = opaque
+    };
+
+    virObjectRWLockWrite(devs);
+    g_hash_table_foreach_remove(devs->objs,
+                                virNodeDeviceObjListRemoveHelper,
+                                &data);
+    virObjectRWUnlock(devs);
+}
index 7f682b9dcaa9b56ae4ee5fbe5da1d7fbbf0988f7..a41742c257b42bf64b0488054eab1b67826f367c 100644 (file)
@@ -80,6 +80,10 @@ void
 virNodeDeviceObjListRemove(virNodeDeviceObjListPtr devs,
                            virNodeDeviceObjPtr dev);
 
+void
+virNodeDeviceObjListRemoveLocked(virNodeDeviceObjList *devs,
+                                 virNodeDeviceObj *dev);
+
 int
 virNodeDeviceObjListGetParentHost(virNodeDeviceObjListPtr devs,
                                   virNodeDeviceDefPtr def);
@@ -134,3 +138,10 @@ virNodeDeviceObjIsPersistent(virNodeDeviceObj *obj);
 void
 virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
                               bool persistent);
+
+typedef bool (*virNodeDeviceObjListRemoveIterator)(virNodeDeviceObj *obj,
+                                                   const void *opaque);
+
+void virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs,
+                                       virNodeDeviceObjListRemoveIterator callback,
+                                       void *opaque);
index 047314ec19554fd0b9b0ddb18d77f47101de3b3a..f36400b5f632e45f641a0b7c740868315521fe64 100644 (file)
@@ -1280,12 +1280,14 @@ virNodeDeviceObjListFindByName;
 virNodeDeviceObjListFindBySysfsPath;
 virNodeDeviceObjListFindMediatedDeviceByUUID;
 virNodeDeviceObjListFindSCSIHostByWWNs;
+virNodeDeviceObjListForEachRemove;
 virNodeDeviceObjListFree;
 virNodeDeviceObjListGetNames;
 virNodeDeviceObjListGetParentHost;
 virNodeDeviceObjListNew;
 virNodeDeviceObjListNumOfDevices;
 virNodeDeviceObjListRemove;
+virNodeDeviceObjListRemoveLocked;
 virNodeDeviceObjSetActive;
 virNodeDeviceObjSetPersistent;