Actually put gfapi to use, by allowing the creation of a gluster
pool. Right now, all volumes are treated as raw; further patches
will allow peering into files to allow for qcow2 files and backing
chains, and reporting proper volume allocation.
I've reported a couple of glusterfs bugs; if we were to require a
minimum of (not-yet-released) glusterfs 3.5, we could use the new
glfs_readdir [1] and not worry about the bogus return value of
glfs_fini [2], but for now I'm testing with Fedora 19's glusterfs
3.4.1.
[1]
http://lists.gnu.org/archive/html/gluster-devel/2013-10/msg00085.html
[2]
http://lists.gnu.org/archive/html/gluster-devel/2013-10/msg00086.html
* src/storage/storage_backend_gluster.c
(virStorageBackendGlusterRefreshPool): Initial implementation.
(virStorageBackendGlusterOpen, virStorageBackendGlusterClose): New
helper functions.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/storage/storage_backend_gluster.c | 192 +++++++++++++++++++++++++++++++++-
1 file changed, 187 insertions(+), 5 deletions(-)
diff --git a/src/storage/storage_backend_gluster.c
b/src/storage/storage_backend_gluster.c
index 2863c73..a9b00c7 100644
--- a/src/storage/storage_backend_gluster.c
+++ b/src/storage/storage_backend_gluster.c
@@ -23,20 +23,202 @@
#include <glusterfs/api/glfs.h>
-#include "virerror.h"
#include "storage_backend_gluster.h"
#include "storage_conf.h"
+#include "viralloc.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virstoragefile.h"
+#include "virstring.h"
+#include "viruri.h"
#define VIR_FROM_THIS VIR_FROM_STORAGE
+struct _virStorageBackendGlusterState {
+ glfs_t *vol;
+
+ /* Accept the same URIs as qemu's block/gluster.c:
+ * gluster[+transport]://[server[:port]]/vol/[dir/]image[?socket=...] */
+ virURI *uri;
+
+ char *volname; /* vol from URI */
+ char *dir; /* dir from URI, or "." */
+};
+
+typedef struct _virStorageBackendGlusterState virStorageBackendGlusterState;
+typedef virStorageBackendGlusterState *virStorageBackendGlusterStatePtr;
+
+static void
+virStorageBackendGlusterClose(virStorageBackendGlusterStatePtr state)
+{
+ if (!state)
+ return;
+
+ /* Yuck - glusterfs-api-3.4.1 appears to always return -1 for
+ * glfs_fini, with errno containing random data, so there's no way
+ * to tell if it succeeded. 3.4.2 is supposed to fix this.*/
+ if (state->vol && glfs_fini(state->vol) < 0)
+ VIR_DEBUG("shutdown of gluster volume %s failed with errno %d",
+ state->volname, errno);
+
+ virURIFree(state->uri);
+ VIR_FREE(state->volname);
+ VIR_FREE(state->dir);
+ VIR_FREE(state);
+}
+
+static virStorageBackendGlusterStatePtr
+virStorageBackendGlusterOpen(virStoragePoolObjPtr pool)
+{
+ virStorageBackendGlusterStatePtr ret = NULL;
+ char *sub;
+ const char *name = pool->def->source.name;
+
+ if (VIR_ALLOC(ret) < 0)
+ return NULL;
+
+ if (*name == '/')
+ name++;
+ if ((sub = strchr(name, '/'))) {
+ if (VIR_STRNDUP(ret->volname, name, sub - name) < 0 ||
+ VIR_STRDUP(ret->dir, sub) < 0)
+ goto error;
+ } else {
+ if (VIR_STRDUP(ret->volname, pool->def->source.name) < 0 ||
+ VIR_STRDUP(ret->dir, ".") < 0)
+ goto error;
+ }
+
+ /* FIXME: Currently hard-coded to tcp transport; XML needs to be
+ * extended to allow alternate transport */
+ if (VIR_ALLOC(ret->uri) < 0 ||
+ VIR_STRDUP(ret->uri->scheme, "gluster") < 0 ||
+ VIR_STRDUP(ret->uri->server, pool->def->source.hosts[0].name) < 0
||
+ virAsprintf(&ret->uri->path, "%s%s",
+ *pool->def->source.name == '/' ? "" :
"/",
+ pool->def->source.name) < 0)
+ goto error;
+ ret->uri->port = pool->def->source.hosts[0].port;
+
+ if (!(ret->vol = glfs_new(ret->volname))) {
+ virReportOOMError();
+ goto error;
+ }
+
+ if (glfs_set_volfile_server(ret->vol, "tcp",
+ ret->uri->server, ret->uri->port) < 0 ||
+ glfs_init(ret->vol) < 0) {
+ char *uri = virURIFormat(ret->uri);
+
+ virReportSystemError(errno, _("failed to connect to %s"),
+ NULLSTR(uri));
+ VIR_FREE(uri);
+ goto error;
+ }
+
+ return ret;
+
+error:
+ virStorageBackendGlusterClose(ret);
+ return NULL;
+}
static int
virStorageBackendGlusterRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
- virStoragePoolObjPtr pool ATTRIBUTE_UNUSED)
+ virStoragePoolObjPtr pool)
{
- virReportError(VIR_ERR_NO_SUPPORT, "%s",
- _("gluster pool type not fully supported yet"));
- return -1;
+ int ret = -1;
+ virStorageBackendGlusterStatePtr state = NULL;
+ struct {
+ struct dirent ent;
+ /* See comment below about readdir_r needing padding */
+ char padding[MAX(1, 256 - (int) (sizeof(struct dirent)
+ - offsetof(struct dirent, d_name)))];
+ } de;
+ struct dirent *ent;
+ glfs_fd_t *dir = NULL;
+ struct stat st;
+ virStorageVolDefPtr vol = NULL;
+ struct statvfs sb;
+
+ if (!(state = virStorageBackendGlusterOpen(pool)))
+ goto cleanup;
+
+ /* Why oh why did glfs 3.4 decide to expose only readdir_r rather
+ * than readdir? POSIX admits that readdir_r is inherently a
+ * flawed design, because systems are not required to define
+ * NAME_MAX:
http://austingroupbugs.net/view.php?id=696
+ *
http://womble.decadent.org.uk/readdir_r-advisory.html
+ *
+ * Fortunately, gluster uses _only_ XFS file systems, and XFS has
+ * a known NAME_MAX of 255; so we are guaranteed that if we
+ * provide 256 bytes of tail padding, then we have enough space to
+ * avoid buffer overflow no matter whether the OS used d_name[],
+ * d_name[1], or d_name[256] in its 'struct dirent'.
+ *
http://lists.gnu.org/archive/html/gluster-devel/2013-10/msg00083.html
+ */
+
+ if (!(dir = glfs_opendir(state->vol, state->dir))) {
+ virReportSystemError(errno, _("cannot open path '%s'"),
+ pool->def->name);
+ goto cleanup;
+ }
+ while (!(errno = glfs_readdirplus_r(dir, &st, &de.ent, &ent)) &&
ent) {
+ char *tmp;
+
+ /* Silently skip directories, including '.' and '..'. FIXME:
+ * should non-'.' subdirectories be listed as type dir? */
+ if (S_ISDIR(st.st_mode))
+ continue;
+
+ if (VIR_ALLOC(vol) < 0 ||
+ VIR_STRDUP(vol->name, ent->d_name) < 0 ||
+ virAsprintf(&vol->key, "%s%s/%s",
+ *pool->def->source.name == '/' ? "" :
"/",
+ pool->def->source.name, vol->name) < 0)
+ goto cleanup;
+
+ /* FIXME - must open files to determine if they are non-raw */
+ vol->type = VIR_STORAGE_VOL_NETWORK;
+ vol->target.format = VIR_STORAGE_FILE_RAW;
+ vol->capacity = vol->allocation = st.st_size;
+ tmp = state->uri->path;
+ state->uri->path = vol->key;
+ if (!(vol->target.path = virURIFormat(state->uri))) {
+ state->uri->path = tmp;
+ goto cleanup;
+ }
+ state->uri->path = tmp;
+ if (VIR_APPEND_ELEMENT(pool->volumes.objs, pool->volumes.count,
+ vol) < 0)
+ goto cleanup;
+ }
+ if (errno) {
+ virReportSystemError(errno, _("failed to read directory
'%s'"),
+ pool->def->source.name);
+ goto cleanup;
+ }
+
+ if (glfs_statvfs(state->vol, state->dir, &sb) < 0) {
+ virReportSystemError(errno, _("cannot statvfs path '%s'"),
pool->def->source.name);
+ goto cleanup;
+ }
+
+ pool->def->capacity = ((unsigned long long)sb.f_frsize *
+ (unsigned long long)sb.f_blocks);
+ pool->def->available = ((unsigned long long)sb.f_bfree *
+ (unsigned long long)sb.f_frsize);
+ pool->def->allocation = pool->def->capacity -
pool->def->available;
+
+ ret = 0;
+cleanup:
+ if (dir)
+ glfs_closedir(dir);
+ virStorageVolDefFree(vol);
+ virStorageBackendGlusterClose(state);
+ if (ret < 0)
+ virStoragePoolObjClearVols(pool);
+ return ret;
}
virStorageBackend virStorageBackendGluster = {
--
1.8.3.1