]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
storage: Add storage file backends for gluster
authorPeter Krempa <pkrempa@redhat.com>
Mon, 3 Feb 2014 16:18:24 +0000 (17:18 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 14 Feb 2014 10:07:23 +0000 (11:07 +0100)
Implement storage backend functions to deal with gluster volumes and
implement the "stat" and "unlink" backend APIs.

src/storage/storage_backend.c
src/storage/storage_backend_gluster.c
src/storage/storage_backend_gluster.h

index aa12c5aecea1d1e45f36cf5820d7c774edb60398..f7b91acb7a8e3156147dd328aafb206ac22e038c 100644 (file)
@@ -126,6 +126,9 @@ static virStorageFileBackendPtr fileBackends[] = {
 #if WITH_STORAGE_FS
     &virStorageFileBackendFile,
     &virStorageFileBackendBlock,
+#endif
+#if WITH_STORAGE_GLUSTER
+    &virStorageFileBackendGluster,
 #endif
     NULL
 };
index d7a15536c080378dcb252c2494120a1d65e4d3b9..a9c33c2b48cb22b9d47d827a5e3e570acc706b57 100644 (file)
@@ -477,3 +477,142 @@ virStorageBackend virStorageBackendGluster = {
 
     .deleteVol = virStorageBackendGlusterVolDelete,
 };
+
+
+typedef struct _virStorageFileBackendGlusterPriv virStorageFileBackendGlusterPriv;
+typedef virStorageFileBackendGlusterPriv *virStorageFileBackendGlusterPrivPtr;
+
+struct _virStorageFileBackendGlusterPriv {
+    glfs_t *vol;
+    char *volname;
+    char *path;
+};
+
+
+static void
+virStorageFileBackendGlusterDeinit(virStorageFilePtr file)
+{
+    VIR_DEBUG("deinitializing gluster storage file %p(%s/%s)",
+              file, file->hosts[0].name, file->path);
+    virStorageFileBackendGlusterPrivPtr priv = file->priv;
+
+    glfs_fini(priv->vol);
+    VIR_FREE(priv->volname);
+
+    VIR_FREE(priv);
+    file->priv = NULL;
+}
+
+static int
+virStorageFileBackendGlusterInit(virStorageFilePtr file)
+{
+    virStorageFileBackendGlusterPrivPtr priv = NULL;
+    virDomainDiskHostDefPtr host = &(file->hosts[0]);
+    const char *hostname = host->name;
+    int port = 0;
+
+    VIR_DEBUG("initializing gluster storage file %p(%s/%s)",
+              file, hostname, file->path);
+
+    if (VIR_ALLOC(priv) < 0)
+        return -1;
+
+    if (VIR_STRDUP(priv->volname, file->path) < 0)
+        goto error;
+
+    if (!(priv->path = strchr(priv->volname, '/'))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("invalid path of gluster volume: '%s'"),
+                       file->path);
+        goto error;
+    }
+
+    *priv->path = '\0';
+    priv->path++;
+
+    if (host->port &&
+        virStrToLong_i(host->port, NULL, 10, &port) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("failed to parse port number '%s'"),
+                       host->port);
+        goto error;
+    }
+
+    if (host->transport == VIR_DOMAIN_DISK_PROTO_TRANS_UNIX)
+        hostname = host->socket;
+
+
+    if (!(priv->vol = glfs_new(priv->volname))) {
+        virReportOOMError();
+        goto error;
+    }
+
+    if (glfs_set_volfile_server(priv->vol,
+                                virDomainDiskProtocolTransportTypeToString(host->transport),
+                                hostname, port) < 0) {
+        virReportSystemError(errno,
+                             _("failed to set gluster volfile server '%s'"),
+                             hostname);
+        goto error;
+    }
+
+    if (glfs_init(priv->vol) < 0) {
+        virReportSystemError(errno,
+                             _("failed to initialize gluster connection to "
+                               "server: '%s'"), hostname);
+        goto error;
+    }
+
+    file->priv = priv;
+
+    return 0;
+
+error:
+    VIR_FREE(priv->volname);
+    glfs_fini(priv->vol);
+
+    return -1;
+}
+
+
+static int
+virStorageFileBackendGlusterUnlink(virStorageFilePtr file)
+{
+    virStorageFileBackendGlusterPrivPtr priv = file->priv;
+    int ret;
+
+    ret = glfs_unlink(priv->vol, priv->path);
+    /* preserve errno */
+
+    VIR_DEBUG("removing storage file %p(%s/%s): ret=%d, errno=%d",
+              file, file->hosts[0].name, file->path, ret, errno);
+    return ret;
+}
+
+
+static int
+virStorageFileBackendGlusterStat(virStorageFilePtr file,
+                                 struct stat *st)
+{
+    virStorageFileBackendGlusterPrivPtr priv = file->priv;
+    int ret;
+
+    ret = glfs_stat(priv->vol, priv->path, st);
+    /* preserve errno */
+
+    VIR_DEBUG("stat of storage file %p(%s/%s): ret=%d, errno=%d",
+              file, file->hosts[0].name, file->path, ret, errno);
+    return ret;
+}
+
+
+virStorageFileBackend virStorageFileBackendGluster = {
+    .type = VIR_DOMAIN_DISK_TYPE_NETWORK,
+    .protocol = VIR_DOMAIN_DISK_PROTOCOL_GLUSTER,
+
+    .backendInit = virStorageFileBackendGlusterInit,
+    .backendDeinit = virStorageFileBackendGlusterDeinit,
+
+    .storageFileUnlink = virStorageFileBackendGlusterUnlink,
+    .storageFileStat = virStorageFileBackendGlusterStat,
+};
index b21bda7337a788a1a205f2b5e59adfbb9eebb618..679601624ce3ea62dd256201077a58d3743f06f9 100644 (file)
@@ -25,5 +25,6 @@
 # include "storage_backend.h"
 
 extern virStorageBackend virStorageBackendGluster;
+extern virStorageFileBackend virStorageFileBackendGluster;
 
 #endif /* __VIR_STORAGE_BACKEND_GLUSTER_H__ */