]> xenbits.xensource.com Git - libvirt.git/commitdiff
Require format to be passed into virStorageFileGetMetadata
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 15 Jun 2010 15:15:51 +0000 (16:15 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Mon, 19 Jul 2010 17:25:04 +0000 (18:25 +0100)
Require the disk image to be passed into virStorageFileGetMetadata.
If this is set to VIR_STORAGE_FILE_AUTO, then the format will be
resolved using probing. This makes it easier to control when
probing will be used

* src/qemu/qemu_driver.c, src/qemu/qemu_security_dac.c,
  src/security/security_selinux.c, src/security/virt-aa-helper.c:
  Set VIR_STORAGE_FILE_AUTO when calling virStorageFileGetMetadata.
* src/storage/storage_backend_fs.c: Probe for disk format before
  calling virStorageFileGetMetadata.
* src/util/storage_file.h, src/util/storage_file.c: Remove format
  from virStorageFileMeta struct & require it to be passed into
  method.

src/qemu/qemu_driver.c
src/qemu/qemu_security_dac.c
src/security/security_selinux.c
src/security/virt-aa-helper.c
src/storage/storage_backend_fs.c
src/util/storage_file.c
src/util/storage_file.h

index 96277cd8f0f77af776cb94003fc4bc3ebdf3e19e..143266de5b3ba55e2068e2b5ea791cd9553178ea 100644 (file)
@@ -3073,7 +3073,9 @@ static int qemuSetupDiskCgroup(virCgroupPtr cgroup,
             }
         }
 
-        rc = virStorageFileGetMetadata(path, &meta);
+        rc = virStorageFileGetMetadata(path,
+                                       VIR_STORAGE_FILE_AUTO,
+                                       &meta);
         if (rc < 0)
             VIR_WARN("Unable to lookup parent image for %s", path);
 
@@ -3123,7 +3125,9 @@ static int qemuTeardownDiskCgroup(virCgroupPtr cgroup,
             }
         }
 
-        rc = virStorageFileGetMetadata(path, &meta);
+        rc = virStorageFileGetMetadata(path,
+                                       VIR_STORAGE_FILE_AUTO,
+                                       &meta);
         if (rc < 0)
             VIR_WARN("Unable to lookup parent image for %s", path);
 
@@ -9618,6 +9622,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     virDomainDiskDefPtr disk = NULL;
     struct stat sb;
     int i;
+    int format;
 
     virCheckFlags(0, -1);
 
@@ -9662,7 +9667,21 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     }
 
     /* Probe for magic formats */
-    if (virStorageFileGetMetadataFromFD(path, fd, &meta) < 0)
+    if (disk->driverType) {
+        if ((format = virStorageFileFormatTypeFromString(disk->driverType)) < 0) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                            _("unknown disk format %s for %s"),
+                            disk->driverType, disk->src);
+            goto cleanup;
+        }
+    } else {
+        if ((format = virStorageFileProbeFormat(disk->src)) < 0)
+            goto cleanup;
+    }
+
+    if (virStorageFileGetMetadataFromFD(path, fd,
+                                        format,
+                                        &meta) < 0)
         goto cleanup;
 
     /* Get info for normal formats */
@@ -9710,7 +9729,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
        highest allocated extent from QEMU */
     if (virDomainObjIsActive(vm) &&
         disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK &&
-        meta.format != VIR_STORAGE_FILE_RAW &&
+        format != VIR_STORAGE_FILE_RAW &&
         S_ISBLK(sb.st_mode)) {
         qemuDomainObjPrivatePtr priv = vm->privateData;
         if (qemuDomainObjBeginJob(vm) < 0)
index 95015b091b2ca8ed5977a705e8b5ba1028c0ea6e..acfe48eff889c1665a7582128dcdcd69e0a09109 100644 (file)
@@ -115,7 +115,9 @@ qemuSecurityDACSetSecurityImageLabel(virDomainObjPtr vm ATTRIBUTE_UNUSED,
         virStorageFileMetadata meta;
         int ret;
 
-        ret = virStorageFileGetMetadata(path, &meta);
+        ret = virStorageFileGetMetadata(path,
+                                        VIR_STORAGE_FILE_AUTO,
+                                        &meta);
 
         if (path != disk->src)
             VIR_FREE(path);
index e5eef196d1bc25fc4212a969034a47ef6ac14e9c..5c0f002dc647770522a952cf652595d3fc929609 100644 (file)
@@ -457,7 +457,9 @@ SELinuxSetSecurityImageLabel(virDomainObjPtr vm,
         virStorageFileMetadata meta;
         int ret;
 
-        ret = virStorageFileGetMetadata(path, &meta);
+        ret = virStorageFileGetMetadata(path,
+                                        VIR_STORAGE_FILE_AUTO,
+                                        &meta);
 
         if (path != disk->src)
             VIR_FREE(path);
index c66f1075c7e25708a0a3ea9efda1749001626171..2c045e67cd4b82b6c236d6375ead9f85b277ca5b 100644 (file)
@@ -830,7 +830,9 @@ get_files(vahControl * ctl)
             do {
                 virStorageFileMetadata meta;
 
-                ret = virStorageFileGetMetadata(path, &meta);
+                ret = virStorageFileGetMetadata(path,
+                                                VIR_STORAGE_FILE_AUTO,
+                                                &meta);
 
                 if (path != ctl->def->disks[i]->src)
                     VIR_FREE(path);
index f0cd77040ae20fac350d656909330f3a0fb61ac5..d3ac0fee3ecf16c1e37c2e7d2b96cf3d6fd252fe 100644 (file)
@@ -75,14 +75,19 @@ virStorageBackendProbeTarget(virStorageVolTargetPtr target,
 
     memset(&meta, 0, sizeof(meta));
 
-    if (virStorageFileGetMetadataFromFD(target->path, fd, &meta) < 0) {
+    if ((target->format = virStorageFileProbeFormatFromFD(target->path, fd)) < 0) {
         close(fd);
         return -1;
     }
 
-    close(fd);
+    if (virStorageFileGetMetadataFromFD(target->path, fd,
+                                        target->format,
+                                        &meta) < 0) {
+        close(fd);
+        return -1;
+    }
 
-    target->format = meta.format;
+    close(fd);
 
     if (backingStore) {
         *backingStore = meta.backingStore;
index bfc26d81d20c9fbff871f43ff861292a4bc2ea95..3cd5dbcf8de46673ae6b8b72da0d15f32d216809 100644 (file)
@@ -696,18 +696,23 @@ virStorageFileProbeFormat(const char *path)
 /**
  * virStorageFileGetMetadataFromFD:
  *
- * Probe for the format of 'fd' (which is an open file descriptor
- * for the file 'path'), filling 'meta' with the detected
- * format and other associated metadata.
+ * Extract metadata about the storage volume with the specified
+ * image format. If image format is VIR_STORAGE_FILE_AUTO, it
+ * will probe to automatically identify the format.
  *
- * Callers are advised never to trust the returned 'meta->format'
- * unless it is listed as VIR_STORAGE_FILE_RAW, since a
- * malicious guest can turn a raw file into any other non-raw
- * format at will.
+ * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a
+ * format, since a malicious guest can turn a raw file into any
+ * other non-raw format at will.
+ *
+ * If the returned meta.backingStoreFormat is VIR_STORAGE_FILE_AUTO
+ * it indicates the image didn't specify an explicit format for its
+ * backing store. Callers are advised against probing for the
+ * backing store format in this case.
  */
 int
 virStorageFileGetMetadataFromFD(const char *path,
                                 int fd,
+                                int format,
                                 virStorageFileMetadata *meta)
 {
     unsigned char *head;
@@ -731,9 +736,16 @@ virStorageFileGetMetadataFromFD(const char *path,
         goto cleanup;
     }
 
-    meta->format = virStorageFileProbeFormatFromBuf(path, head, len);
+    if (format == VIR_STORAGE_FILE_AUTO)
+        format = virStorageFileProbeFormatFromBuf(path, head, len);
+
+    if (format < 0 ||
+        format >= VIR_STORAGE_FILE_LAST) {
+        virReportSystemError(EINVAL, _("unknown storage file format %d"), format);
+        return -1;
+    }
 
-    ret = virStorageFileGetMetadataFromBuf(meta->format, path, head, len, meta);
+    ret = virStorageFileGetMetadataFromBuf(format, path, head, len, meta);
 
 cleanup:
     VIR_FREE(head);
@@ -743,16 +755,22 @@ cleanup:
 /**
  * virStorageFileGetMetadata:
  *
- * Probe for the format of 'path', filling 'meta' with the detected
- * format and other associated metadata.
+ * Extract metadata about the storage volume with the specified
+ * image format. If image format is VIR_STORAGE_FILE_AUTO, it
+ * will probe to automatically identify the format.
  *
- * Callers are advised never to trust the returned 'meta->format'
- * unless it is listed as VIR_STORAGE_FILE_RAW, since a
- * malicious guest can turn a raw file into any other non-raw
- * format at will.
+ * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a
+ * format, since a malicious guest can turn a raw file into any
+ * other non-raw format at will.
+ *
+ * If the returned meta.backingStoreFormat is VIR_STORAGE_FILE_AUTO
+ * it indicates the image didn't specify an explicit format for its
+ * backing store. Callers are advised against probing for the
+ * backing store format in this case.
  */
 int
 virStorageFileGetMetadata(const char *path,
+                          int format,
                           virStorageFileMetadata *meta)
 {
     int fd, ret;
@@ -762,7 +780,7 @@ virStorageFileGetMetadata(const char *path,
         return -1;
     }
 
-    ret = virStorageFileGetMetadataFromFD(path, fd, meta);
+    ret = virStorageFileGetMetadataFromFD(path, fd, format, meta);
 
     close(fd);
 
index 3420d4480104af5f71d5d83c23a3c3c7d22d9f8c..6853182f78b52b8f135bf4b92f7a5e05becbc8e2 100644 (file)
@@ -46,7 +46,6 @@ enum virStorageFileFormat {
 VIR_ENUM_DECL(virStorageFileFormat);
 
 typedef struct _virStorageFileMetadata {
-    int format;
     char *backingStore;
     int backingStoreFormat;
     unsigned long long capacity;
@@ -62,9 +61,11 @@ int virStorageFileProbeFormatFromFD(const char *path,
                                     int fd);
 
 int virStorageFileGetMetadata(const char *path,
+                              int format,
                               virStorageFileMetadata *meta);
 int virStorageFileGetMetadataFromFD(const char *path,
                                     int fd,
+                                    int format,
                                     virStorageFileMetadata *meta);
 
 int virStorageFileIsSharedFS(const char *path);