Switch storage objects to array instead of linked list

This commit is contained in:
Daniel P. Berrange 2008-10-10 15:13:28 +00:00
parent dd48e929c7
commit 2e53a9912e
8 changed files with 291 additions and 264 deletions

View File

@ -1,3 +1,10 @@
Fri Oct 10 15:39:00 BST 2008 Daniel P. Berrange <berrange@redhat.com>
* src/storage_conf.c, src/storage_conf.h, src/storage_driver.c,
src/storage_backend_disk.c, src/storage_backend_fs.c,
src/storage_backend_iscsi.c, src/storage_backend_logical.c:
Switch storage objects to array instead of linked list
Fri Oct 10 15:39:00 BST 2008 Daniel P. Berrange <berrange@redhat.com> Fri Oct 10 15:39:00 BST 2008 Daniel P. Berrange <berrange@redhat.com>
* src/network_conf.c, src/network_conf.h, src/network_driver.c, * src/network_conf.c, src/network_conf.h, src/network_driver.c,

View File

@ -182,9 +182,13 @@ virStorageBackendDiskMakeDataVol(virConnectPtr conn,
return -1; return -1;
} }
vol->next = pool->volumes; if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes = vol; pool->volumes.count+1) < 0) {
pool->nvolumes++; virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("volume"));
virStorageVolDefFree(vol);
return -1;
}
pool->volumes.objs[pool->volumes.count++] = vol;
/* Prepended path will be same for all partitions, so we can /* Prepended path will be same for all partitions, so we can
* strip the path to form a reasonable pool-unique name * strip the path to form a reasonable pool-unique name

View File

@ -822,6 +822,7 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn,
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
struct statvfs sb; struct statvfs sb;
virStorageVolDefPtr vol = NULL;
if (!(dir = opendir(pool->def->target.path))) { if (!(dir = opendir(pool->def->target.path))) {
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
@ -831,61 +832,42 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn,
} }
while ((ent = readdir(dir)) != NULL) { while ((ent = readdir(dir)) != NULL) {
virStorageVolDefPtr vol;
int ret; int ret;
if (VIR_ALLOC(vol) < 0) { if (VIR_ALLOC(vol) < 0)
virStorageReportError(conn, VIR_ERR_NO_MEMORY, goto no_memory;
"%s", _("volume"));
goto cleanup;
}
vol->name = strdup(ent->d_name); if ((vol->name = strdup(ent->d_name)) == NULL)
if (vol->name == NULL) { goto no_memory;
VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume name"));
goto cleanup;
}
vol->target.format = VIR_STORAGE_VOL_RAW; /* Real value is filled in during probe */ vol->target.format = VIR_STORAGE_VOL_RAW; /* Real value is filled in during probe */
if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) + if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
1 + strlen(vol->name) + 1) < 0) { 1 + strlen(vol->name) + 1) < 0)
VIR_FREE(vol->target.path); goto no_memory;
VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume name"));
goto cleanup;
}
strcpy(vol->target.path, pool->def->target.path); strcpy(vol->target.path, pool->def->target.path);
strcat(vol->target.path, "/"); strcat(vol->target.path, "/");
strcat(vol->target.path, vol->name); strcat(vol->target.path, vol->name);
if ((vol->key = strdup(vol->target.path)) == NULL) { if ((vol->key = strdup(vol->target.path)) == NULL)
VIR_FREE(vol->name); goto no_memory;
VIR_FREE(vol->target.path);
VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume key"));
goto cleanup;
}
if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) { if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) {
VIR_FREE(vol->key);
VIR_FREE(vol->name);
VIR_FREE(vol->target.path);
VIR_FREE(vol);
if (ret == -1) if (ret == -1)
goto cleanup; goto no_memory;
else else {
/* Silently ignore non-regular files, /* Silently ignore non-regular files,
* eg '.' '..', 'lost+found' */ * eg '.' '..', 'lost+found' */
virStorageVolDefFree(vol);
vol = NULL;
continue; continue;
} }
}
vol->next = pool->volumes; if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes = vol; pool->volumes.count+1) < 0)
pool->nvolumes++; goto no_memory;
continue; pool->volumes.objs[pool->volumes.count++] = vol;
vol = NULL;
} }
closedir(dir); closedir(dir);
@ -904,8 +886,13 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn,
return 0; return 0;
no_memory:
virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
/* fallthrough */
cleanup: cleanup:
closedir(dir); closedir(dir);
virStorageVolDefFree(vol);
virStoragePoolObjClearVols(pool); virStoragePoolObjClearVols(pool);
return -1; return -1;
} }

View File

@ -236,9 +236,12 @@ virStorageBackendISCSINewLun(virConnectPtr conn, virStoragePoolObjPtr pool,
pool->def->capacity += vol->capacity; pool->def->capacity += vol->capacity;
pool->def->allocation += vol->allocation; pool->def->allocation += vol->allocation;
vol->next = pool->volumes; if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes = vol; pool->volumes.count+1) < 0) {
pool->nvolumes++; virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
goto cleanup;
}
pool->volumes.objs[pool->volumes.count++] = vol;
close(fd); close(fd);

View File

@ -119,12 +119,17 @@ virStorageBackendLogicalMakeVol(virConnectPtr conn,
if ((vol->name = strdup(groups[0])) == NULL) { if ((vol->name = strdup(groups[0])) == NULL) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume")); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
virStorageVolDefFree(vol);
return -1; return -1;
} }
vol->next = pool->volumes; if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes = vol; pool->volumes.count + 1)) {
pool->nvolumes++; virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
virStorageVolDefFree(vol);
return -1;
}
pool->volumes.objs[pool->volumes.count++] = vol;
} }
if (vol->target.path == NULL) { if (vol->target.path == NULL) {

View File

@ -52,6 +52,10 @@
void void
virStorageVolDefFree(virStorageVolDefPtr def) { virStorageVolDefFree(virStorageVolDefPtr def) {
int i; int i;
if (!def)
return;
VIR_FREE(def->name); VIR_FREE(def->name);
VIR_FREE(def->key); VIR_FREE(def->key);
@ -69,6 +73,9 @@ void
virStoragePoolDefFree(virStoragePoolDefPtr def) { virStoragePoolDefFree(virStoragePoolDefPtr def) {
int i; int i;
if (!def)
return;
VIR_FREE(def->name); VIR_FREE(def->name);
VIR_FREE(def->source.host.name); VIR_FREE(def->source.host.name);
for (i = 0 ; i < def->source.ndevice ; i++) { for (i = 0 ; i < def->source.ndevice ; i++) {
@ -92,9 +99,10 @@ virStoragePoolDefFree(virStoragePoolDefPtr def) {
void void
virStoragePoolObjFree(virStoragePoolObjPtr obj) { virStoragePoolObjFree(virStoragePoolObjPtr obj) {
if (obj->def) if (!obj)
return;
virStoragePoolDefFree(obj->def); virStoragePoolDefFree(obj->def);
if (obj->newDef)
virStoragePoolDefFree(obj->newDef); virStoragePoolDefFree(obj->newDef);
VIR_FREE(obj->configFile); VIR_FREE(obj->configFile);
@ -102,28 +110,37 @@ virStoragePoolObjFree(virStoragePoolObjPtr obj) {
VIR_FREE(obj); VIR_FREE(obj);
} }
void virStoragePoolObjListFree(virStoragePoolObjListPtr pools)
{
unsigned int i;
for (i = 0 ; i < pools->count ; i++)
virStoragePoolObjFree(pools->objs[i]);
VIR_FREE(pools->objs);
pools->count = 0;
}
void void
virStoragePoolObjRemove(virStorageDriverStatePtr driver, virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
virStoragePoolObjPtr pool) virStoragePoolObjPtr pool)
{ {
virStoragePoolObjPtr prev = NULL, curr; unsigned int i;
curr = driver->pools; for (i = 0 ; i < pools->count ; i++) {
while (curr != pool) { if (pools->objs[i] == pool) {
prev = curr; virStoragePoolObjFree(pools->objs[i]);
curr = curr->next;
if (i < (pools->count - 1))
memmove(pools->objs + i, pools->objs + i + 1,
sizeof(*(pools->objs)) * (pools->count - (i + 1)));
if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
; /* Failure to reduce memory allocation isn't fatal */
} }
pools->count--;
if (curr) { break;
if (prev) }
prev->next = curr->next;
else
driver->pools = curr->next;
driver->ninactivePools--;
} }
virStoragePoolObjFree(pool);
} }
@ -906,29 +923,25 @@ virStorageVolDefFormat(virConnectPtr conn,
virStoragePoolObjPtr virStoragePoolObjPtr
virStoragePoolObjFindByUUID(virStorageDriverStatePtr driver, virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
const unsigned char *uuid) { const unsigned char *uuid) {
virStoragePoolObjPtr pool = driver->pools; unsigned int i;
while (pool) { for (i = 0 ; i < pools->count ; i++)
if (!memcmp(pool->def->uuid, uuid, VIR_UUID_BUFLEN)) if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
return pool; return pools->objs[i];
pool = pool->next;
}
return NULL; return NULL;
} }
virStoragePoolObjPtr virStoragePoolObjPtr
virStoragePoolObjFindByName(virStorageDriverStatePtr driver, virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
const char *name) { const char *name) {
virStoragePoolObjPtr pool = driver->pools; unsigned int i;
while (pool) { for (i = 0 ; i < pools->count ; i++)
if (STREQ(pool->def->name, name)) if (STREQ(pools->objs[i]->def->name, name))
return pool; return pools->objs[i];
pool = pool->next;
}
return NULL; return NULL;
} }
@ -936,26 +949,22 @@ virStoragePoolObjFindByName(virStorageDriverStatePtr driver,
void void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool) virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{ {
virStorageVolDefPtr vol = pool->volumes; unsigned int i;
while (vol) { for (i = 0 ; i < pool->volumes.count ; i++)
virStorageVolDefPtr next = vol->next; virStorageVolDefFree(pool->volumes.objs[i]);
virStorageVolDefFree(vol);
vol = next; VIR_FREE(pool->volumes.objs);
} pool->volumes.count = 0;
pool->volumes = NULL;
pool->nvolumes = 0;
} }
virStorageVolDefPtr virStorageVolDefPtr
virStorageVolDefFindByKey(virStoragePoolObjPtr pool, virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
const char *key) { const char *key) {
virStorageVolDefPtr vol = pool->volumes; unsigned int i;
while (vol) { for (i = 0 ; i < pool->volumes.count ; i++)
if (STREQ(vol->key, key)) if (STREQ(pool->volumes.objs[i]->key, key))
return vol; return pool->volumes.objs[i];
vol = vol->next;
}
return NULL; return NULL;
} }
@ -963,13 +972,11 @@ virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
virStorageVolDefPtr virStorageVolDefPtr
virStorageVolDefFindByPath(virStoragePoolObjPtr pool, virStorageVolDefFindByPath(virStoragePoolObjPtr pool,
const char *path) { const char *path) {
virStorageVolDefPtr vol = pool->volumes; unsigned int i;
while (vol) { for (i = 0 ; i < pool->volumes.count ; i++)
if (STREQ(vol->target.path, path)) if (STREQ(pool->volumes.objs[i]->target.path, path))
return vol; return pool->volumes.objs[i];
vol = vol->next;
}
return NULL; return NULL;
} }
@ -977,24 +984,22 @@ virStorageVolDefFindByPath(virStoragePoolObjPtr pool,
virStorageVolDefPtr virStorageVolDefPtr
virStorageVolDefFindByName(virStoragePoolObjPtr pool, virStorageVolDefFindByName(virStoragePoolObjPtr pool,
const char *name) { const char *name) {
virStorageVolDefPtr vol = pool->volumes; unsigned int i;
while (vol) { for (i = 0 ; i < pool->volumes.count ; i++)
if (STREQ(vol->name, name)) if (STREQ(pool->volumes.objs[i]->name, name))
return vol; return pool->volumes.objs[i];
vol = vol->next;
}
return NULL; return NULL;
} }
virStoragePoolObjPtr virStoragePoolObjPtr
virStoragePoolObjAssignDef(virConnectPtr conn, virStoragePoolObjAssignDef(virConnectPtr conn,
virStorageDriverStatePtr driver, virStoragePoolObjListPtr pools,
virStoragePoolDefPtr def) { virStoragePoolDefPtr def) {
virStoragePoolObjPtr pool; virStoragePoolObjPtr pool;
if ((pool = virStoragePoolObjFindByName(driver, def->name))) { if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
if (!virStoragePoolObjIsActive(pool)) { if (!virStoragePoolObjIsActive(pool)) {
virStoragePoolDefFree(pool->def); virStoragePoolDefFree(pool->def);
pool->def = def; pool->def = def;
@ -1014,16 +1019,21 @@ virStoragePoolObjAssignDef(virConnectPtr conn,
pool->active = 0; pool->active = 0;
pool->def = def; pool->def = def;
pool->next = driver->pools;
driver->pools = pool; if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
driver->ninactivePools++; pool->def = NULL;
virStoragePoolObjFree(pool);
virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
return NULL;
}
pools->objs[pools->count++] = pool;
return pool; return pool;
} }
static virStoragePoolObjPtr static virStoragePoolObjPtr
virStoragePoolObjLoad(virStorageDriverStatePtr driver, virStoragePoolObjLoad(virConnectPtr conn,
virStoragePoolObjListPtr pools,
const char *file, const char *file,
const char *path, const char *path,
const char *xml, const char *xml,
@ -1045,7 +1055,7 @@ virStoragePoolObjLoad(virStorageDriverStatePtr driver,
return NULL; return NULL;
} }
if (!(pool = virStoragePoolObjAssignDef(NULL, driver, def))) { if (!(pool = virStoragePoolObjAssignDef(conn, pools, def))) {
virStorageLog("Failed to load storage pool config '%s': out of memory", path); virStorageLog("Failed to load storage pool config '%s': out of memory", path);
virStoragePoolDefFree(def); virStoragePoolDefFree(def);
return NULL; return NULL;
@ -1072,15 +1082,18 @@ virStoragePoolObjLoad(virStorageDriverStatePtr driver,
int int
virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver) { virStoragePoolLoadAllConfigs(virConnectPtr conn,
virStoragePoolObjListPtr pools,
const char *configDir,
const char *autostartDir) {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
if (!(dir = opendir(driver->configDir))) { if (!(dir = opendir(configDir))) {
if (errno == ENOENT) if (errno == ENOENT)
return 0; return 0;
virStorageLog("Failed to open dir '%s': %s", virStorageLog("Failed to open dir '%s': %s",
driver->configDir, strerror(errno)); configDir, strerror(errno));
return -1; return -1;
} }
@ -1095,24 +1108,24 @@ virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver) {
if (!virFileHasSuffix(entry->d_name, ".xml")) if (!virFileHasSuffix(entry->d_name, ".xml"))
continue; continue;
if (virFileBuildPath(driver->configDir, entry->d_name, if (virFileBuildPath(configDir, entry->d_name,
NULL, path, PATH_MAX) < 0) { NULL, path, PATH_MAX) < 0) {
virStorageLog("Config filename '%s/%s' is too long", virStorageLog("Config filename '%s/%s' is too long",
driver->configDir, entry->d_name); configDir, entry->d_name);
continue; continue;
} }
if (virFileBuildPath(driver->autostartDir, entry->d_name, if (virFileBuildPath(autostartDir, entry->d_name,
NULL, autostartLink, PATH_MAX) < 0) { NULL, autostartLink, PATH_MAX) < 0) {
virStorageLog("Autostart link path '%s/%s' is too long", virStorageLog("Autostart link path '%s/%s' is too long",
driver->autostartDir, entry->d_name); autostartDir, entry->d_name);
continue; continue;
} }
if (virFileReadAll(path, 8192, &xml) < 0) if (virFileReadAll(path, 8192, &xml) < 0)
continue; continue;
virStoragePoolObjLoad(driver, entry->d_name, path, xml, autostartLink); virStoragePoolObjLoad(conn, pools, entry->d_name, path, xml, autostartLink);
VIR_FREE(xml); VIR_FREE(xml);
} }

View File

@ -87,10 +87,14 @@ struct _virStorageVolDef {
virStorageVolSource source; virStorageVolSource source;
virStorageVolTarget target; virStorageVolTarget target;
virStorageVolDefPtr next;
}; };
typedef struct _virStorageVolDefList virStorageVolDefList;
typedef virStorageVolDefList *virStorageVolDefListPtr;
struct _virStorageVolDefList {
unsigned int count;
virStorageVolDefPtr *objs;
};
@ -222,10 +226,14 @@ struct _virStoragePoolObj {
virStoragePoolDefPtr def; virStoragePoolDefPtr def;
virStoragePoolDefPtr newDef; virStoragePoolDefPtr newDef;
int nvolumes; virStorageVolDefList volumes;
virStorageVolDefPtr volumes; };
virStoragePoolObjPtr next; typedef struct _virStoragePoolObjList virStoragePoolObjList;
typedef virStoragePoolObjList *virStoragePoolObjListPtr;
struct _virStoragePoolObjList {
unsigned int count;
virStoragePoolObjPtr *objs;
}; };
@ -235,9 +243,8 @@ typedef struct _virStorageDriverState virStorageDriverState;
typedef virStorageDriverState *virStorageDriverStatePtr; typedef virStorageDriverState *virStorageDriverStatePtr;
struct _virStorageDriverState { struct _virStorageDriverState {
int nactivePools; virStoragePoolObjList pools;
int ninactivePools;
virStoragePoolObjPtr pools;
char *configDir; char *configDir;
char *autostartDir; char *autostartDir;
}; };
@ -251,11 +258,14 @@ static inline int virStoragePoolObjIsActive(virStoragePoolObjPtr pool) {
__virReportErrorHelper(conn, VIR_FROM_STORAGE, code, __FILE__, \ __virReportErrorHelper(conn, VIR_FROM_STORAGE, code, __FILE__, \
__FUNCTION__, __LINE__, fmt) __FUNCTION__, __LINE__, fmt)
int virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver); int virStoragePoolLoadAllConfigs(virConnectPtr conn,
virStoragePoolObjListPtr pools,
const char *configDir,
const char *autostartDir);
virStoragePoolObjPtr virStoragePoolObjFindByUUID(virStorageDriverStatePtr driver, virStoragePoolObjPtr virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
const unsigned char *uuid); const unsigned char *uuid);
virStoragePoolObjPtr virStoragePoolObjFindByName(virStorageDriverStatePtr driver, virStoragePoolObjPtr virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
const char *name); const char *name);
virStorageVolDefPtr virStorageVolDefFindByKey(virStoragePoolObjPtr pool, virStorageVolDefPtr virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
@ -282,7 +292,7 @@ char *virStorageVolDefFormat(virConnectPtr conn,
virStorageVolDefPtr def); virStorageVolDefPtr def);
virStoragePoolObjPtr virStoragePoolObjAssignDef(virConnectPtr conn, virStoragePoolObjPtr virStoragePoolObjAssignDef(virConnectPtr conn,
virStorageDriverStatePtr driver, virStoragePoolObjListPtr pools,
virStoragePoolDefPtr def); virStoragePoolDefPtr def);
int virStoragePoolObjSaveDef(virConnectPtr conn, int virStoragePoolObjSaveDef(virConnectPtr conn,
@ -295,7 +305,8 @@ int virStoragePoolObjDeleteDef(virConnectPtr conn,
void virStorageVolDefFree(virStorageVolDefPtr def); void virStorageVolDefFree(virStorageVolDefPtr def);
void virStoragePoolDefFree(virStoragePoolDefPtr def); void virStoragePoolDefFree(virStoragePoolDefPtr def);
void virStoragePoolObjFree(virStoragePoolObjPtr pool); void virStoragePoolObjFree(virStoragePoolObjPtr pool);
void virStoragePoolObjRemove(virStorageDriverStatePtr driver, void virStoragePoolObjListFree(virStoragePoolObjListPtr pools);
void virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
virStoragePoolObjPtr pool); virStoragePoolObjPtr pool);
#endif /* __VIR_STORAGE_DRIVER_H__ */ #endif /* __VIR_STORAGE_DRIVER_H__ */

View File

@ -49,11 +49,10 @@ static int storageDriverShutdown(void);
static void static void
storageDriverAutostart(virStorageDriverStatePtr driver) { storageDriverAutostart(virStorageDriverStatePtr driver) {
virStoragePoolObjPtr pool; unsigned int i;
pool = driver->pools; for (i = 0 ; i < driver->pools.count ; i++) {
while (pool != NULL) { virStoragePoolObjPtr pool = driver->pools.objs[i];
virStoragePoolObjPtr next = pool->next;
if (pool->autostart && if (pool->autostart &&
!virStoragePoolObjIsActive(pool)) { !virStoragePoolObjIsActive(pool)) {
@ -61,7 +60,6 @@ storageDriverAutostart(virStorageDriverStatePtr driver) {
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) { if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
storageLog("Missing backend %d", storageLog("Missing backend %d",
pool->def->type); pool->def->type);
pool = next;
continue; continue;
} }
@ -70,7 +68,6 @@ storageDriverAutostart(virStorageDriverStatePtr driver) {
virErrorPtr err = virGetLastError(); virErrorPtr err = virGetLastError();
storageLog("Failed to autostart storage pool '%s': %s", storageLog("Failed to autostart storage pool '%s': %s",
pool->def->name, err ? err->message : NULL); pool->def->name, err ? err->message : NULL);
pool = next;
continue; continue;
} }
@ -80,15 +77,10 @@ storageDriverAutostart(virStorageDriverStatePtr driver) {
backend->stopPool(NULL, pool); backend->stopPool(NULL, pool);
storageLog("Failed to autostart storage pool '%s': %s", storageLog("Failed to autostart storage pool '%s': %s",
pool->def->name, err ? err->message : NULL); pool->def->name, err ? err->message : NULL);
pool = next;
continue; continue;
} }
pool->active = 1; pool->active = 1;
driver->nactivePools++;
driver->ninactivePools--;
} }
pool = next;
} }
} }
@ -149,7 +141,10 @@ storageDriverStartup(void) {
} }
*/ */
if (virStoragePoolObjScanConfigs(driverState) < 0) { if (virStoragePoolLoadAllConfigs(NULL,
&driverState->pools,
driverState->configDir,
driverState->autostartDir) < 0) {
storageDriverShutdown(); storageDriverShutdown();
return -1; return -1;
} }
@ -173,7 +168,13 @@ storageDriverStartup(void) {
*/ */
static int static int
storageDriverReload(void) { storageDriverReload(void) {
virStoragePoolObjScanConfigs(driverState); if (!driverState)
return -1;
virStoragePoolLoadAllConfigs(NULL,
&driverState->pools,
driverState->configDir,
driverState->autostartDir);
storageDriverAutostart(driverState); storageDriverAutostart(driverState);
return 0; return 0;
@ -188,10 +189,16 @@ storageDriverReload(void) {
*/ */
static int static int
storageDriverActive(void) { storageDriverActive(void) {
unsigned int i;
if (!driverState)
return 0;
/* If we've any active networks or guests, then we /* If we've any active networks or guests, then we
* mark this driver as active * mark this driver as active
*/ */
if (driverState->nactivePools) for (i = 0 ; i < driverState->pools.count ; i++)
if (virStoragePoolObjIsActive(driverState->pools.objs[i]))
return 1; return 1;
/* Otherwise we're happy to deal with a shutdown */ /* Otherwise we're happy to deal with a shutdown */
@ -205,15 +212,15 @@ storageDriverActive(void) {
*/ */
static int static int
storageDriverShutdown(void) { storageDriverShutdown(void) {
virStoragePoolObjPtr pool; unsigned int i;
if (!driverState) if (!driverState)
return -1; return -1;
/* shutdown active pools */ /* shutdown active pools */
pool = driverState->pools; for (i = 0 ; i < driverState->pools.count ; i++) {
while (pool) { virStoragePoolObjPtr pool = driverState->pools.objs[i];
virStoragePoolObjPtr next = pool->next;
if (virStoragePoolObjIsActive(pool)) { if (virStoragePoolObjIsActive(pool)) {
virStorageBackendPtr backend; virStorageBackendPtr backend;
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) { if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
@ -229,24 +236,14 @@ storageDriverShutdown(void) {
} }
virStoragePoolObjClearVols(pool); virStoragePoolObjClearVols(pool);
} }
pool = next;
} }
/* free inactive pools */ /* free inactive pools */
pool = driverState->pools; virStoragePoolObjListFree(&driverState->pools);
while (pool) {
virStoragePoolObjPtr next = pool->next;
virStoragePoolObjFree(pool);
pool = next;
}
driverState->pools = NULL;
driverState->nactivePools = 0;
driverState->ninactivePools = 0;
free(driverState->configDir); VIR_FREE(driverState->configDir);
free(driverState->autostartDir); VIR_FREE(driverState->autostartDir);
free(driverState); VIR_FREE(driverState);
driverState = NULL;
return 0; return 0;
} }
@ -258,7 +255,7 @@ storagePoolLookupByUUID(virConnectPtr conn,
const unsigned char *uuid) { const unsigned char *uuid) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)conn->storagePrivateData; (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
virStoragePoolPtr ret; virStoragePoolPtr ret;
if (!pool) { if (!pool) {
@ -276,7 +273,7 @@ storagePoolLookupByName(virConnectPtr conn,
const char *name) { const char *name) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)conn->storagePrivateData; (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, name); virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, name);
virStoragePoolPtr ret; virStoragePoolPtr ret;
if (!pool) { if (!pool) {
@ -316,7 +313,13 @@ static int
storageNumPools(virConnectPtr conn) { storageNumPools(virConnectPtr conn) {
virStorageDriverStatePtr driver virStorageDriverStatePtr driver
= (virStorageDriverStatePtr)conn->storagePrivateData; = (virStorageDriverStatePtr)conn->storagePrivateData;
return driver->nactivePools; unsigned int i, nactive = 0;
for (i = 0 ; i < driver->pools.count ; i++)
if (virStoragePoolObjIsActive(driver->pools.objs[i]))
nactive++;
return nactive;
} }
static int static int
@ -325,18 +328,17 @@ storageListPools(virConnectPtr conn,
int nnames) { int nnames) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)conn->storagePrivateData; (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = driver->pools;
int got = 0, i; int got = 0, i;
while (pool && got < nnames) {
if (virStoragePoolObjIsActive(pool)) { for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
if (!(names[got] = strdup(pool->def->name))) { if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("names")); "%s", _("names"));
goto cleanup; goto cleanup;
} }
got++; got++;
} }
pool = pool->next;
} }
return got; return got;
@ -353,7 +355,13 @@ static int
storageNumDefinedPools(virConnectPtr conn) { storageNumDefinedPools(virConnectPtr conn) {
virStorageDriverStatePtr driver virStorageDriverStatePtr driver
= (virStorageDriverStatePtr)conn->storagePrivateData; = (virStorageDriverStatePtr)conn->storagePrivateData;
return driver->ninactivePools; unsigned int i, nactive = 0;
for (i = 0 ; i < driver->pools.count ; i++)
if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
nactive++;
return nactive;
} }
static int static int
@ -362,18 +370,17 @@ storageListDefinedPools(virConnectPtr conn,
int nnames) { int nnames) {
virStorageDriverStatePtr driver virStorageDriverStatePtr driver
= (virStorageDriverStatePtr)conn->storagePrivateData; = (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = driver->pools;
int got = 0, i; int got = 0, i;
while (pool && got < nnames) {
if (!virStoragePoolObjIsActive(pool)) { for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
if (!(names[got] = strdup(pool->def->name))) { if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("names")); "%s", _("names"));
goto cleanup; goto cleanup;
} }
got++; got++;
} }
pool = pool->next;
} }
return got; return got;
@ -424,8 +431,8 @@ storagePoolCreate(virConnectPtr conn,
if (!(def = virStoragePoolDefParse(conn, xml, NULL))) if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
return NULL; return NULL;
if (virStoragePoolObjFindByUUID(driver, def->uuid) || if (virStoragePoolObjFindByUUID(&driver->pools, def->uuid) ||
virStoragePoolObjFindByName(driver, def->name)) { virStoragePoolObjFindByName(&driver->pools, def->name)) {
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
"%s", _("storage pool already exists")); "%s", _("storage pool already exists"));
virStoragePoolDefFree(def); virStoragePoolDefFree(def);
@ -437,7 +444,7 @@ storagePoolCreate(virConnectPtr conn,
return NULL; return NULL;
} }
if (!(pool = virStoragePoolObjAssignDef(conn, driver, def))) { if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
virStoragePoolDefFree(def); virStoragePoolDefFree(def);
return NULL; return NULL;
} }
@ -451,8 +458,6 @@ storagePoolCreate(virConnectPtr conn,
return NULL; return NULL;
} }
pool->active = 1; pool->active = 1;
driver->nactivePools++;
driver->ninactivePools--;
ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid); ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
@ -478,13 +483,13 @@ storagePoolDefine(virConnectPtr conn,
return NULL; return NULL;
} }
if (!(pool = virStoragePoolObjAssignDef(conn, driver, def))) { if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
virStoragePoolDefFree(def); virStoragePoolDefFree(def);
return NULL; return NULL;
} }
if (virStoragePoolObjSaveDef(conn, driver, pool, def) < 0) { if (virStoragePoolObjSaveDef(conn, driver, pool, def) < 0) {
virStoragePoolObjRemove(driver, pool); virStoragePoolObjRemove(&driver->pools, pool);
return NULL; return NULL;
} }
@ -496,7 +501,7 @@ static int
storagePoolUndefine(virStoragePoolPtr obj) { storagePoolUndefine(virStoragePoolPtr obj) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -517,12 +522,10 @@ storagePoolUndefine(virStoragePoolPtr obj) {
storageLog("Failed to delete autostart link '%s': %s", storageLog("Failed to delete autostart link '%s': %s",
pool->autostartLink, strerror(errno)); pool->autostartLink, strerror(errno));
free(pool->configFile); VIR_FREE(pool->configFile);
pool->configFile = NULL; VIR_FREE(pool->autostartLink);
free(pool->autostartLink);
pool->autostartLink = NULL;
virStoragePoolObjRemove(driver, pool); virStoragePoolObjRemove(&driver->pools, pool);
return 0; return 0;
} }
@ -532,7 +535,7 @@ storagePoolStart(virStoragePoolPtr obj,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
if (!pool) { if (!pool) {
@ -560,8 +563,6 @@ storagePoolStart(virStoragePoolPtr obj,
} }
pool->active = 1; pool->active = 1;
driver->nactivePools++;
driver->ninactivePools--;
return 0; return 0;
} }
@ -571,7 +572,7 @@ storagePoolBuild(virStoragePoolPtr obj,
unsigned int flags) { unsigned int flags) {
virStorageDriverStatePtr driver virStorageDriverStatePtr driver
= (virStorageDriverStatePtr)obj->conn->storagePrivateData; = (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
if (!pool) { if (!pool) {
@ -602,7 +603,7 @@ static int
storagePoolDestroy(virStoragePoolPtr obj) { storagePoolDestroy(virStoragePoolPtr obj) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
if (!pool) { if (!pool) {
@ -628,11 +629,9 @@ storagePoolDestroy(virStoragePoolPtr obj) {
virStoragePoolObjClearVols(pool); virStoragePoolObjClearVols(pool);
pool->active = 0; pool->active = 0;
driver->nactivePools--;
driver->ninactivePools++;
if (pool->configFile == NULL) if (pool->configFile == NULL)
virStoragePoolObjRemove(driver, pool); virStoragePoolObjRemove(&driver->pools, pool);
return 0; return 0;
} }
@ -643,7 +642,7 @@ storagePoolDelete(virStoragePoolPtr obj,
unsigned int flags) { unsigned int flags) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
if (!pool) { if (!pool) {
@ -679,7 +678,7 @@ storagePoolRefresh(virStoragePoolPtr obj,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
int ret = 0; int ret = 0;
@ -705,11 +704,9 @@ storagePoolRefresh(virStoragePoolPtr obj,
backend->stopPool(obj->conn, pool); backend->stopPool(obj->conn, pool);
pool->active = 0; pool->active = 0;
driver->nactivePools--;
driver->ninactivePools++;
if (pool->configFile == NULL) if (pool->configFile == NULL)
virStoragePoolObjRemove(driver, pool); virStoragePoolObjRemove(&driver->pools, pool);
} }
return ret; return ret;
@ -721,7 +718,7 @@ storagePoolGetInfo(virStoragePoolPtr obj,
virStoragePoolInfoPtr info) { virStoragePoolInfoPtr info) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
if (!pool) { if (!pool) {
@ -751,7 +748,7 @@ storagePoolDumpXML(virStoragePoolPtr obj,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -767,7 +764,7 @@ storagePoolGetAutostart(virStoragePoolPtr obj,
int *autostart) { int *autostart) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -789,7 +786,7 @@ storagePoolSetAutostart(virStoragePoolPtr obj,
int autostart) { int autostart) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -845,7 +842,7 @@ static int
storagePoolNumVolumes(virStoragePoolPtr obj) { storagePoolNumVolumes(virStoragePoolPtr obj) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -859,7 +856,7 @@ storagePoolNumVolumes(virStoragePoolPtr obj) {
return -1; return -1;
} }
return pool->nvolumes; return pool->volumes.count;
} }
static int static int
@ -868,9 +865,8 @@ storagePoolListVolumes(virStoragePoolPtr obj,
int maxnames) { int maxnames) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
int i = 0; int i, n = 0;
virStorageVolDefPtr vol;
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -885,25 +881,20 @@ storagePoolListVolumes(virStoragePoolPtr obj,
} }
memset(names, 0, maxnames); memset(names, 0, maxnames);
vol = pool->volumes; for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
while (vol && i < maxnames) { if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
names[i] = strdup(vol->name);
if (names[i] == NULL) {
virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY,
"%s", _("name")); "%s", _("name"));
goto cleanup; goto cleanup;
} }
vol = vol->next;
i++;
} }
return i; return n;
cleanup: cleanup:
for (i = 0 ; i < maxnames ; i++) { for (n = 0 ; n < maxnames ; n++)
free(names[i]); VIR_FREE(names[i]);
names[i] = NULL;
}
memset(names, 0, maxnames); memset(names, 0, maxnames);
return -1; return -1;
} }
@ -914,7 +905,7 @@ storageVolumeLookupByName(virStoragePoolPtr obj,
const char *name) { const char *name) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageVolDefPtr vol; virStorageVolDefPtr vol;
if (!pool) { if (!pool) {
@ -946,19 +937,19 @@ storageVolumeLookupByKey(virConnectPtr conn,
const char *key) { const char *key) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)conn->storagePrivateData; (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = driver->pools; unsigned int i;
while (pool) { for (i = 0 ; i < driver->pools.count ; i++) {
if (virStoragePoolObjIsActive(pool)) { if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
virStorageVolDefPtr vol = virStorageVolDefFindByKey(pool, key); virStorageVolDefPtr vol =
virStorageVolDefFindByKey(driver->pools.objs[i], key);
if (vol) if (vol)
return virGetStorageVol(conn, return virGetStorageVol(conn,
pool->def->name, driver->pools.objs[i]->def->name,
vol->name, vol->name,
vol->key); vol->key);
} }
pool = pool->next;
} }
virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL, virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
@ -971,19 +962,19 @@ storageVolumeLookupByPath(virConnectPtr conn,
const char *path) { const char *path) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)conn->storagePrivateData; (virStorageDriverStatePtr)conn->storagePrivateData;
virStoragePoolObjPtr pool = driver->pools; unsigned int i;
while (pool) { for (i = 0 ; i < driver->pools.count ; i++) {
if (virStoragePoolObjIsActive(pool)) { if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
virStorageVolDefPtr vol = virStorageVolDefFindByPath(pool, path); virStorageVolDefPtr vol =
virStorageVolDefFindByPath(driver->pools.objs[i], path);
if (vol) if (vol)
return virGetStorageVol(conn, return virGetStorageVol(conn,
pool->def->name, driver->pools.objs[i]->def->name,
vol->name, vol->name,
vol->key); vol->key);
} }
pool = pool->next;
} }
virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL, virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
@ -997,7 +988,7 @@ storageVolumeCreateXML(virStoragePoolPtr obj,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid); virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
virStorageBackendPtr backend; virStorageBackendPtr backend;
virStorageVolDefPtr vol; virStorageVolDefPtr vol;
@ -1027,6 +1018,13 @@ storageVolumeCreateXML(virStoragePoolPtr obj,
return NULL; return NULL;
} }
if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes.count+1) < 0) {
virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, NULL);
virStorageVolDefFree(vol);
return NULL;
}
if (!backend->createVol) { if (!backend->createVol) {
virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT, virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT,
"%s", _("storage pool does not support volume creation")); "%s", _("storage pool does not support volume creation"));
@ -1039,9 +1037,7 @@ storageVolumeCreateXML(virStoragePoolPtr obj,
return NULL; return NULL;
} }
vol->next = pool->volumes; pool->volumes.objs[pool->volumes.count++] = vol;
pool->volumes = vol;
pool->nvolumes++;
return virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key); return virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);
} }
@ -1051,9 +1047,10 @@ storageVolumeDelete(virStorageVolPtr obj,
unsigned int flags) { unsigned int flags) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool); virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
virStorageBackendPtr backend; virStorageBackendPtr backend;
virStorageVolDefPtr vol, tmp, prev; virStorageVolDefPtr vol;
unsigned int i;
if (!pool) { if (!pool) {
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL, virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@ -1089,22 +1086,22 @@ storageVolumeDelete(virStorageVolPtr obj,
return -1; return -1;
} }
prev = NULL; for (i = 0 ; i < pool->volumes.count ; i++) {
tmp = pool->volumes; if (pool->volumes.objs[i] == vol) {
while (tmp) { virStorageVolDefFree(vol);
if (tmp == vol) {
if (i < (pool->volumes.count - 1))
memmove(pool->volumes.objs + i, pool->volumes.objs + i + 1,
sizeof(*(pool->volumes.objs)) * (pool->volumes.count - (i + 1)));
if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count - 1) < 0) {
; /* Failure to reduce memory allocation isn't fatal */
}
pool->volumes.count--;
break; break;
} }
prev = tmp;
tmp = tmp->next;
} }
if (prev) {
prev->next = vol->next;
} else {
pool->volumes = vol->next;
}
pool->nvolumes--;
virStorageVolDefFree(vol);
return 0; return 0;
} }
@ -1114,7 +1111,7 @@ storageVolumeGetInfo(virStorageVolPtr obj,
virStorageVolInfoPtr info) { virStorageVolInfoPtr info) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool); virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
virStorageBackendPtr backend; virStorageBackendPtr backend;
virStorageVolDefPtr vol; virStorageVolDefPtr vol;
@ -1158,7 +1155,7 @@ storageVolumeGetXMLDesc(virStorageVolPtr obj,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool); virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
virStorageBackendPtr backend; virStorageBackendPtr backend;
virStorageVolDefPtr vol; virStorageVolDefPtr vol;
@ -1192,7 +1189,7 @@ static char *
storageVolumeGetPath(virStorageVolPtr obj) { storageVolumeGetPath(virStorageVolPtr obj) {
virStorageDriverStatePtr driver = virStorageDriverStatePtr driver =
(virStorageDriverStatePtr)obj->conn->storagePrivateData; (virStorageDriverStatePtr)obj->conn->storagePrivateData;
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool); virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
virStorageVolDefPtr vol; virStorageVolDefPtr vol;
char *ret; char *ret;