提交 cecd6566 编写于 作者: P Peter Krempa

storage: Refactor location of metadata for storage drive access to files

Now that we store all metadata about a storage image in a
virStorageSource struct let's use it also to store information needed by
the storage driver to access and do operations on the files.
上级 9689dfaa
...@@ -12465,7 +12465,6 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn, ...@@ -12465,7 +12465,6 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn,
bool active, bool active,
bool reuse) bool reuse)
{ {
virStorageFilePtr snapfile = NULL;
int ret = -1; int ret = -1;
struct stat st; struct stat st;
...@@ -12489,10 +12488,10 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn, ...@@ -12489,10 +12488,10 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn,
return -1; return -1;
} }
if (!(snapfile = virStorageFileInit(&snapdisk->src))) if (virStorageFileInit(&snapdisk->src) < 0)
return -1; return -1;
if (virStorageFileStat(snapfile, &st) < 0) { if (virStorageFileStat(&snapdisk->src, &st) < 0) {
if (errno != ENOENT) { if (errno != ENOENT) {
virReportSystemError(errno, virReportSystemError(errno,
_("unable to stat for disk %s: %s"), _("unable to stat for disk %s: %s"),
...@@ -12515,7 +12514,7 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn, ...@@ -12515,7 +12514,7 @@ qemuDomainSnapshotPrepareDiskExternal(virConnectPtr conn,
ret = 0; ret = 0;
cleanup: cleanup:
virStorageFileFree(snapfile); virStorageFileDeinit(&snapdisk->src);
return ret; return ret;
} }
...@@ -12738,7 +12737,6 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12738,7 +12737,6 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
int ret = -1; int ret = -1;
int fd = -1; int fd = -1;
bool need_unlink = false; bool need_unlink = false;
virStorageFilePtr snapfile = NULL;
if (snap->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) { if (snap->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
...@@ -12757,7 +12755,7 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12757,7 +12755,7 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
virStorageFileFreeMetadata(disk->backingChain); virStorageFileFreeMetadata(disk->backingChain);
disk->backingChain = NULL; disk->backingChain = NULL;
if (!(snapfile = virStorageFileInit(&snap->src))) if (virStorageFileInit(&snap->src) < 0)
goto cleanup; goto cleanup;
if (qemuDomainSnapshotDiskGetSourceString(snap, &source) < 0) if (qemuDomainSnapshotDiskGetSourceString(snap, &source) < 0)
...@@ -12886,9 +12884,9 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12886,9 +12884,9 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
} }
cleanup: cleanup:
if (need_unlink && virStorageFileUnlink(snapfile)) if (need_unlink && virStorageFileUnlink(&snap->src))
VIR_WARN("unable to unlink just-created %s", source); VIR_WARN("unable to unlink just-created %s", source);
virStorageFileFree(snapfile); virStorageFileDeinit(&snap->src);
VIR_FREE(device); VIR_FREE(device);
VIR_FREE(source); VIR_FREE(source);
VIR_FREE(newsource); VIR_FREE(newsource);
...@@ -12911,10 +12909,9 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12911,10 +12909,9 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
{ {
char *source = NULL; char *source = NULL;
char *persistSource = NULL; char *persistSource = NULL;
virStorageFilePtr diskfile = NULL;
struct stat st; struct stat st;
diskfile = virStorageFileInit(&disk->src); ignore_value(virStorageFileInit(&disk->src));
if (VIR_STRDUP(source, origdisk->src.path) < 0 || if (VIR_STRDUP(source, origdisk->src.path) < 0 ||
(persistDisk && VIR_STRDUP(persistSource, source) < 0)) (persistDisk && VIR_STRDUP(persistSource, source) < 0))
...@@ -12922,9 +12919,9 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12922,9 +12919,9 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
qemuDomainPrepareDiskChainElement(driver, vm, disk, disk->src.path, qemuDomainPrepareDiskChainElement(driver, vm, disk, disk->src.path,
VIR_DISK_CHAIN_NO_ACCESS); VIR_DISK_CHAIN_NO_ACCESS);
if (need_unlink && diskfile && if (need_unlink &&
virStorageFileStat(diskfile, &st) == 0 && S_ISREG(st.st_mode) && virStorageFileStat(&disk->src, &st) == 0 && S_ISREG(st.st_mode) &&
virStorageFileUnlink(diskfile) < 0) virStorageFileUnlink(&disk->src) < 0)
VIR_WARN("Unable to remove just-created %s", disk->src.path); VIR_WARN("Unable to remove just-created %s", disk->src.path);
/* Update vm in place to match changes. */ /* Update vm in place to match changes. */
...@@ -12953,7 +12950,7 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver, ...@@ -12953,7 +12950,7 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
} }
cleanup: cleanup:
virStorageFileFree(diskfile); virStorageFileDeinit(&disk->src);
VIR_FREE(source); VIR_FREE(source);
VIR_FREE(persistSource); VIR_FREE(persistSource);
} }
......
...@@ -160,24 +160,34 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn, ...@@ -160,24 +160,34 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
int imgformat); int imgformat);
/* ------- virStorageFile backends ------------ */ /* ------- virStorageFile backends ------------ */
typedef struct _virStorageFileBackend virStorageFileBackend;
typedef virStorageFileBackend *virStorageFileBackendPtr;
struct _virStorageDriverData {
virStorageFileBackendPtr backend;
void *priv;
};
typedef int typedef int
(*virStorageFileBackendInit)(virStorageFilePtr file); (*virStorageFileBackendInit)(virStorageSourcePtr src);
typedef void typedef void
(*virStorageFileBackendDeinit)(virStorageFilePtr file); (*virStorageFileBackendDeinit)(virStorageSourcePtr src);
typedef int typedef int
(*virStorageFileBackendCreate)(virStorageFilePtr file); (*virStorageFileBackendCreate)(virStorageSourcePtr src);
typedef int typedef int
(*virStorageFileBackendUnlink)(virStorageFilePtr file); (*virStorageFileBackendUnlink)(virStorageSourcePtr src);
typedef int typedef int
(*virStorageFileBackendStat)(virStorageFilePtr file, (*virStorageFileBackendStat)(virStorageSourcePtr src,
struct stat *st); struct stat *st);
virStorageFileBackendPtr virStorageFileBackendForType(int type, int protocol); virStorageFileBackendPtr virStorageFileBackendForType(int type, int protocol);
struct _virStorageFileBackend { struct _virStorageFileBackend {
int type; int type;
int protocol; int protocol;
......
...@@ -1335,31 +1335,31 @@ virStorageBackend virStorageBackendNetFileSystem = { ...@@ -1335,31 +1335,31 @@ virStorageBackend virStorageBackendNetFileSystem = {
static int static int
virStorageFileBackendFileUnlink(virStorageFilePtr file) virStorageFileBackendFileUnlink(virStorageSourcePtr src)
{ {
int ret; int ret;
ret = unlink(file->path); ret = unlink(src->path);
/* preserve errno */ /* preserve errno */
VIR_DEBUG("removing storage file %p(%s): ret=%d, errno=%d", VIR_DEBUG("removing storage file %p(%s): ret=%d, errno=%d",
file, file->path, ret, errno); src, src->path, ret, errno);
return ret; return ret;
} }
static int static int
virStorageFileBackendFileStat(virStorageFilePtr file, virStorageFileBackendFileStat(virStorageSourcePtr src,
struct stat *st) struct stat *st)
{ {
int ret; int ret;
ret = stat(file->path, st); ret = stat(src->path, st);
/* preserve errno */ /* preserve errno */
VIR_DEBUG("stat of storage file %p(%s): ret=%d, errno=%d", VIR_DEBUG("stat of storage file %p(%s): ret=%d, errno=%d",
file, file->path, ret, errno); src, src->path, ret, errno);
return ret; return ret;
} }
......
...@@ -546,41 +546,41 @@ struct _virStorageFileBackendGlusterPriv { ...@@ -546,41 +546,41 @@ struct _virStorageFileBackendGlusterPriv {
static void static void
virStorageFileBackendGlusterDeinit(virStorageFilePtr file) virStorageFileBackendGlusterDeinit(virStorageSourcePtr src)
{ {
VIR_DEBUG("deinitializing gluster storage file %p(%s/%s)", VIR_DEBUG("deinitializing gluster storage file %p(%s/%s)",
file, file->hosts[0].name, file->path); src, src->hosts[0].name, src->path);
virStorageFileBackendGlusterPrivPtr priv = file->priv; virStorageFileBackendGlusterPrivPtr priv = src->drv->priv;
if (priv->vol) if (priv->vol)
glfs_fini(priv->vol); glfs_fini(priv->vol);
VIR_FREE(priv->volname); VIR_FREE(priv->volname);
VIR_FREE(priv); VIR_FREE(priv);
file->priv = NULL; src->drv->priv = NULL;
} }
static int static int
virStorageFileBackendGlusterInit(virStorageFilePtr file) virStorageFileBackendGlusterInit(virStorageSourcePtr src)
{ {
virStorageFileBackendGlusterPrivPtr priv = NULL; virStorageFileBackendGlusterPrivPtr priv = NULL;
virStorageNetHostDefPtr host = &(file->hosts[0]); virStorageNetHostDefPtr host = &(src->hosts[0]);
const char *hostname = host->name; const char *hostname = host->name;
int port = 0; int port = 0;
VIR_DEBUG("initializing gluster storage file %p(%s/%s)", VIR_DEBUG("initializing gluster storage file %p(%s/%s)",
file, hostname, file->path); src, hostname, src->path);
if (VIR_ALLOC(priv) < 0) if (VIR_ALLOC(priv) < 0)
return -1; return -1;
if (VIR_STRDUP(priv->volname, file->path) < 0) if (VIR_STRDUP(priv->volname, src->path) < 0)
goto error; goto error;
if (!(priv->path = strchr(priv->volname, '/'))) { if (!(priv->path = strchr(priv->volname, '/'))) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid path of gluster volume: '%s'"), _("invalid path of gluster volume: '%s'"),
file->path); src->path);
goto error; goto error;
} }
...@@ -598,7 +598,6 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file) ...@@ -598,7 +598,6 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file)
if (host->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX) if (host->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX)
hostname = host->socket; hostname = host->socket;
if (!(priv->vol = glfs_new(priv->volname))) { if (!(priv->vol = glfs_new(priv->volname))) {
virReportOOMError(); virReportOOMError();
goto error; goto error;
...@@ -620,7 +619,7 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file) ...@@ -620,7 +619,7 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file)
goto error; goto error;
} }
file->priv = priv; src->drv->priv = priv;
return 0; return 0;
...@@ -635,32 +634,32 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file) ...@@ -635,32 +634,32 @@ virStorageFileBackendGlusterInit(virStorageFilePtr file)
static int static int
virStorageFileBackendGlusterUnlink(virStorageFilePtr file) virStorageFileBackendGlusterUnlink(virStorageSourcePtr src)
{ {
virStorageFileBackendGlusterPrivPtr priv = file->priv; virStorageFileBackendGlusterPrivPtr priv = src->drv->priv;
int ret; int ret;
ret = glfs_unlink(priv->vol, priv->path); ret = glfs_unlink(priv->vol, priv->path);
/* preserve errno */ /* preserve errno */
VIR_DEBUG("removing storage file %p(%s/%s): ret=%d, errno=%d", VIR_DEBUG("removing storage file %p(%s/%s): ret=%d, errno=%d",
file, file->hosts[0].name, file->path, ret, errno); src, src->hosts[0].name, src->path, ret, errno);
return ret; return ret;
} }
static int static int
virStorageFileBackendGlusterStat(virStorageFilePtr file, virStorageFileBackendGlusterStat(virStorageSourcePtr src,
struct stat *st) struct stat *st)
{ {
virStorageFileBackendGlusterPrivPtr priv = file->priv; virStorageFileBackendGlusterPrivPtr priv = src->drv->priv;
int ret; int ret;
ret = glfs_stat(priv->vol, priv->path, st); ret = glfs_stat(priv->vol, priv->path, st);
/* preserve errno */ /* preserve errno */
VIR_DEBUG("stat of storage file %p(%s/%s): ret=%d, errno=%d", VIR_DEBUG("stat of storage file %p(%s/%s): ret=%d, errno=%d",
file, file->hosts[0].name, file->path, ret, errno); src, src->hosts[0].name, src->path, ret, errno);
return ret; return ret;
} }
......
...@@ -2754,82 +2754,74 @@ int storageRegister(void) ...@@ -2754,82 +2754,74 @@ int storageRegister(void)
/* ----------- file handlers cooperating with storage driver --------------- */ /* ----------- file handlers cooperating with storage driver --------------- */
static bool
virStorageFileIsInitialized(virStorageSourcePtr src)
{
return !!src->drv;
}
void void
virStorageFileFree(virStorageFilePtr file) virStorageFileDeinit(virStorageSourcePtr src)
{ {
if (!file) if (!virStorageFileIsInitialized(src))
return; return;
if (file->backend && if (src->drv->backend &&
file->backend->backendDeinit) src->drv->backend->backendDeinit)
file->backend->backendDeinit(file); src->drv->backend->backendDeinit(src);
VIR_FREE(file->path); VIR_FREE(src->drv);
virStorageNetHostDefFree(file->nhosts, file->hosts);
VIR_FREE(file);
} }
virStorageFilePtr int
virStorageFileInit(virStorageSourcePtr src) virStorageFileInit(virStorageSourcePtr src)
{ {
virStorageFilePtr file = NULL; int actualType = virStorageSourceGetActualType(src);
if (VIR_ALLOC(src->drv) < 0)
if (VIR_ALLOC(file) < 0) return -1;
return NULL;
file->type = virStorageSourceGetActualType(src);
file->protocol = src->protocol;
file->nhosts = src->nhosts;
if (VIR_STRDUP(file->path, src->path) < 0)
goto error;
if (!(file->hosts = virStorageNetHostDefCopy(src->nhosts, src->hosts)))
goto error;
if (!(file->backend = virStorageFileBackendForType(file->type, if (!(src->drv->backend = virStorageFileBackendForType(actualType,
file->protocol))) src->protocol)))
goto error; goto error;
if (file->backend->backendInit && if (src->drv->backend->backendInit &&
file->backend->backendInit(file) < 0) src->drv->backend->backendInit(src) < 0)
goto error; goto error;
return file; return 0;
error: error:
VIR_FREE(file->path); VIR_FREE(src->drv);
virStorageNetHostDefFree(file->nhosts, file->hosts); return -1;
VIR_FREE(file);
return NULL;
} }
/** /**
* virStorageFileCreate: Creates an empty storage file via storage driver * virStorageFileCreate: Creates an empty storage file via storage driver
* *
* @file: file structure pointing to the file * @src: file structure pointing to the file
* *
* Returns 0 on success, -2 if the function isn't supported by the backend, * Returns 0 on success, -2 if the function isn't supported by the backend,
* -1 on other failure. Errno is set in case of failure. * -1 on other failure. Errno is set in case of failure.
*/ */
int int
virStorageFileCreate(virStorageFilePtr file) virStorageFileCreate(virStorageSourcePtr src)
{ {
if (!file->backend->storageFileCreate) { if (!virStorageFileIsInitialized(src) ||
!src->drv->backend->storageFileCreate) {
errno = ENOSYS; errno = ENOSYS;
return -2; return -2;
} }
return file->backend->storageFileCreate(file); return src->drv->backend->storageFileCreate(src);
} }
/** /**
* virStorageFileUnlink: Unlink storage file via storage driver * virStorageFileUnlink: Unlink storage file via storage driver
* *
* @file: file structure pointing to the file * @src: file structure pointing to the file
* *
* Unlinks the file described by the @file structure. * Unlinks the file described by the @file structure.
* *
...@@ -2837,34 +2829,36 @@ virStorageFileCreate(virStorageFilePtr file) ...@@ -2837,34 +2829,36 @@ virStorageFileCreate(virStorageFilePtr file)
* -1 on other failure. Errno is set in case of failure. * -1 on other failure. Errno is set in case of failure.
*/ */
int int
virStorageFileUnlink(virStorageFilePtr file) virStorageFileUnlink(virStorageSourcePtr src)
{ {
if (!file->backend->storageFileUnlink) { if (!virStorageFileIsInitialized(src) ||
!src->drv->backend->storageFileUnlink) {
errno = ENOSYS; errno = ENOSYS;
return -2; return -2;
} }
return file->backend->storageFileUnlink(file); return src->drv->backend->storageFileUnlink(src);
} }
/** /**
* virStorageFileStat: returns stat struct of a file via storage driver * virStorageFileStat: returns stat struct of a file via storage driver
* *
* @file: file structure pointing to the file * @src: file structure pointing to the file
* @stat: stat structure to return data * @stat: stat structure to return data
* *
* Returns 0 on success, -2 if the function isn't supported by the backend, * Returns 0 on success, -2 if the function isn't supported by the backend,
* -1 on other failure. Errno is set in case of failure. * -1 on other failure. Errno is set in case of failure.
*/ */
int int
virStorageFileStat(virStorageFilePtr file, virStorageFileStat(virStorageSourcePtr src,
struct stat *st) struct stat *st)
{ {
if (!(file->backend->storageFileStat)) { if (!virStorageFileIsInitialized(src) ||
!src->drv->backend->storageFileStat) {
errno = ENOSYS; errno = ENOSYS;
return -2; return -2;
} }
return file->backend->storageFileStat(file, st); return src->drv->backend->storageFileStat(src, st);
} }
...@@ -29,30 +29,13 @@ ...@@ -29,30 +29,13 @@
# include "storage_conf.h" # include "storage_conf.h"
# include "virstoragefile.h" # include "virstoragefile.h"
typedef struct _virStorageFileBackend virStorageFileBackend; int
typedef virStorageFileBackend *virStorageFileBackendPtr;
typedef struct _virStorageFile virStorageFile;
typedef virStorageFile *virStorageFilePtr;
struct _virStorageFile {
virStorageFileBackendPtr backend;
void *priv;
char *path;
int type;
int protocol;
size_t nhosts;
virStorageNetHostDefPtr hosts;
};
virStorageFilePtr
virStorageFileInit(virStorageSourcePtr src); virStorageFileInit(virStorageSourcePtr src);
void virStorageFileFree(virStorageFilePtr file); void virStorageFileDeinit(virStorageSourcePtr src);
int virStorageFileCreate(virStorageFilePtr file); int virStorageFileCreate(virStorageSourcePtr src);
int virStorageFileUnlink(virStorageFilePtr file); int virStorageFileUnlink(virStorageSourcePtr src);
int virStorageFileStat(virStorageFilePtr file, int virStorageFileStat(virStorageSourcePtr src,
struct stat *stat); struct stat *stat);
int storageRegister(void); int storageRegister(void);
......
...@@ -208,6 +208,8 @@ enum virStorageSecretType { ...@@ -208,6 +208,8 @@ enum virStorageSecretType {
VIR_STORAGE_SECRET_TYPE_LAST VIR_STORAGE_SECRET_TYPE_LAST
}; };
typedef struct _virStorageDriverData virStorageDriverData;
typedef virStorageDriverData *virStorageDriverDataPtr;
typedef struct _virStorageSource virStorageSource; typedef struct _virStorageSource virStorageSource;
typedef virStorageSource *virStorageSourcePtr; typedef virStorageSource *virStorageSourcePtr;
...@@ -243,6 +245,9 @@ struct _virStorageSource { ...@@ -243,6 +245,9 @@ struct _virStorageSource {
unsigned long long capacity; /* in bytes, 0 if unknown */ unsigned long long capacity; /* in bytes, 0 if unknown */
size_t nseclabels; size_t nseclabels;
virSecurityDeviceLabelDefPtr *seclabels; virSecurityDeviceLabelDefPtr *seclabels;
/* metadata for storage driver access to remote and local volumes */
virStorageDriverDataPtr drv;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册