[PATCH 2/2] storage: volume: Rework lookup of volume objects

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

 



Add a helper to do all the lookup steps and remove a ton of duplicated
code.
---
 src/storage/storage_driver.c | 292 ++++++++++---------------------------------
 1 file changed, 69 insertions(+), 223 deletions(-)

diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index c9916ff..26b2601 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -1520,45 +1520,75 @@ storageVolDeleteInternal(virStorageVolPtr obj,
 }


-static int
-storageVolDelete(virStorageVolPtr obj,
-                 unsigned int flags)
+static virStorageVolDefPtr
+virStorageVolDefFromVol(virStorageVolPtr obj,
+                        virStoragePoolObjPtr *pool,
+                        virStorageBackendPtr *backend)
 {
     virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool;
-    virStorageBackendPtr backend;
     virStorageVolDefPtr vol = NULL;
-    int ret = -1;
+
+    *pool = NULL;
+    if (backend)
+        *backend = NULL;

     storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
+    *pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
     storageDriverUnlock(driver);

-    if (!pool) {
+    if (!*pool) {
         virReportError(VIR_ERR_NO_STORAGE_POOL,
                        _("no storage pool with matching name '%s'"),
                        obj->pool);
-        goto cleanup;
+        return NULL;
     }

-    if (!virStoragePoolObjIsActive(pool)) {
+    if (!virStoragePoolObjIsActive(*pool)) {
         virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
+                       _("storage pool '%s' is not active"),
+                       (*pool)->def->name);
+        goto error;
     }

-    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
-        goto cleanup;
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
+    if (backend) {
+        if (!(*backend = virStorageBackendForType((*pool)->def->type)))
+            goto error;
+    }

-    if (!vol) {
+    if (!(vol = virStorageVolDefFindByName(*pool, obj->name))) {
         virReportError(VIR_ERR_NO_STORAGE_VOL,
                        _("no storage vol with matching name '%s'"),
                        obj->name);
-        goto cleanup;
+        goto error;
     }

+    return vol;
+
+ error:
+    if (*pool) {
+        virStoragePoolObjUnlock(*pool);
+        *pool = NULL;
+    }
+
+    if (backend)
+        *backend = NULL;
+
+    return NULL;
+}
+
+
+static int
+storageVolDelete(virStorageVolPtr obj,
+                 unsigned int flags)
+{
+    virStoragePoolObjPtr pool;
+    virStorageBackendPtr backend;
+    virStorageVolDefPtr vol = NULL;
+    int ret = -1;
+
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, &backend)))
+        goto cleanup;
+
     if (virStorageVolDeleteEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;

@@ -1898,38 +1928,14 @@ storageVolDownload(virStorageVolPtr obj,
                    unsigned long long length,
                    unsigned int flags)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool = NULL;
     virStorageVolDefPtr vol = NULL;
     int ret = -1;

     virCheckFlags(0, -1);

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (vol == NULL) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, NULL)))
+        return -1;

     if (virStorageVolDownloadEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;
@@ -1950,8 +1956,7 @@ storageVolDownload(virStorageVolPtr obj,
     ret = 0;

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);

     return ret;
 }
@@ -1964,38 +1969,14 @@ storageVolUpload(virStorageVolPtr obj,
                  unsigned long long length,
                  unsigned int flags)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool = NULL;
     virStorageVolDefPtr vol = NULL;
     int ret = -1;

     virCheckFlags(0, -1);

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (vol == NULL) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, NULL)))
+        return -1;

     if (virStorageVolUploadEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;
@@ -2044,8 +2025,7 @@ storageVolUpload(virStorageVolPtr obj,
     ret = 0;

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);

     return ret;
 }
@@ -2055,7 +2035,6 @@ storageVolResize(virStorageVolPtr obj,
                  unsigned long long capacity,
                  unsigned int flags)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStorageBackendPtr backend;
     virStoragePoolObjPtr pool = NULL;
     virStorageVolDefPtr vol = NULL;
@@ -2066,34 +2045,8 @@ storageVolResize(virStorageVolPtr obj,
                   VIR_STORAGE_VOL_RESIZE_DELTA |
                   VIR_STORAGE_VOL_RESIZE_SHRINK, -1);

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
-        goto cleanup;
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (vol == NULL) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, &backend)))
+        return -1;

     if (virStorageVolResizeEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;
@@ -2161,8 +2114,7 @@ storageVolResize(virStorageVolPtr obj,
     ret = 0;

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);

     return ret;
 }
@@ -2369,7 +2321,6 @@ storageVolWipePattern(virStorageVolPtr obj,
                       unsigned int algorithm,
                       unsigned int flags)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool = NULL;
     virStorageVolDefPtr vol = NULL;
     int ret = -1;
@@ -2383,31 +2334,9 @@ storageVolWipePattern(virStorageVolPtr obj,
         return -1;
     }

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, NULL)))
+        return -1;

-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (vol == NULL) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }

     if (virStorageVolWipePatternEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;
@@ -2433,12 +2362,9 @@ storageVolWipePattern(virStorageVolPtr obj,
     ret = 0;

  cleanup:
-    if (pool) {
-        virStoragePoolObjUnlock(pool);
-    }
+    virStoragePoolObjUnlock(pool);

     return ret;
-
 }

 static int
@@ -2453,44 +2379,17 @@ static int
 storageVolGetInfo(virStorageVolPtr obj,
                   virStorageVolInfoPtr info)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool;
     virStorageBackendPtr backend;
     virStorageVolDefPtr vol;
     int ret = -1;

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (!vol) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, &backend)))
+        return -1;

     if (virStorageVolGetInfoEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;

-    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
-        goto cleanup;
-
     if (backend->refreshVol &&
         backend->refreshVol(obj->conn, pool, vol) < 0)
         goto cleanup;
@@ -2502,8 +2401,7 @@ storageVolGetInfo(virStorageVolPtr obj,
     ret = 0;

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);
     return ret;
 }

@@ -2511,7 +2409,6 @@ static char *
 storageVolGetXMLDesc(virStorageVolPtr obj,
                      unsigned int flags)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool;
     virStorageBackendPtr backend;
     virStorageVolDefPtr vol;
@@ -2519,38 +2416,12 @@ storageVolGetXMLDesc(virStorageVolPtr obj,

     virCheckFlags(0, NULL);

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (!vol) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, &backend)))
+        return NULL;

     if (virStorageVolGetXMLDescEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;

-    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
-        goto cleanup;
-
     if (backend->refreshVol &&
         backend->refreshVol(obj->conn, pool, vol) < 0)
         goto cleanup;
@@ -2558,8 +2429,7 @@ storageVolGetXMLDesc(virStorageVolPtr obj,
     ret = virStorageVolDefFormat(pool->def, vol);

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);

     return ret;
 }
@@ -2567,35 +2437,12 @@ storageVolGetXMLDesc(virStorageVolPtr obj,
 static char *
 storageVolGetPath(virStorageVolPtr obj)
 {
-    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
     virStoragePoolObjPtr pool;
     virStorageVolDefPtr vol;
     char *ret = NULL;

-    storageDriverLock(driver);
-    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock(driver);
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
-        goto cleanup;
-    }
-
-    if (!virStoragePoolObjIsActive(pool)) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("storage pool '%s' is not active"), pool->def->name);
-        goto cleanup;
-    }
-
-    vol = virStorageVolDefFindByName(pool, obj->name);
-
-    if (!vol) {
-        virReportError(VIR_ERR_NO_STORAGE_VOL,
-                       _("no storage vol with matching name '%s'"),
-                       obj->name);
-        goto cleanup;
-    }
+    if (!(vol = virStorageVolDefFromVol(obj, &pool, NULL)))
+        return NULL;

     if (virStorageVolGetPathEnsureACL(obj->conn, pool->def, vol) < 0)
         goto cleanup;
@@ -2603,8 +2450,7 @@ storageVolGetPath(virStorageVolPtr obj)
     ignore_value(VIR_STRDUP(ret, vol->target.path));

  cleanup:
-    if (pool)
-        virStoragePoolObjUnlock(pool);
+    virStoragePoolObjUnlock(pool);
     return ret;
 }

-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[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]