958e1b
From d78a26ac572e339d115c3485d3a19ead0e9d4dcf Mon Sep 17 00:00:00 2001
958e1b
Message-Id: <d78a26ac572e339d115c3485d3a19ead0e9d4dcf.1418766606.git.jen@redhat.com>
958e1b
In-Reply-To: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
958e1b
References: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
958e1b
From: Fam Zheng <famz@redhat.com>
958e1b
Date: Thu, 4 Dec 2014 00:05:02 -0600
958e1b
Subject: [CHANGE 08/31] block/vmdk: add basic .bdrv_check support
958e1b
To: rhvirt-patches@redhat.com,
958e1b
    jen@redhat.com
958e1b
958e1b
RH-Author: Fam Zheng <famz@redhat.com>
958e1b
Message-id: <1417651524-18041-9-git-send-email-famz@redhat.com>
958e1b
Patchwork-id: 62681
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH v5 08/30] block/vmdk: add basic .bdrv_check support
958e1b
Bugzilla: 1134251
958e1b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
958e1b
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
From: Peter Lieven <pl@kamp.de>
958e1b
958e1b
this adds a basic vmdk corruption check. it should detect severe
958e1b
table corruptions and file truncation.
958e1b
958e1b
Signed-off-by: Peter Lieven <pl@kamp.de>
958e1b
Reviewed-by: Fam Zheng <famz@redhat.com>
958e1b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
(cherry picked from commit f43aa8e18ad83508c2786403e7230a584d357c8e)
958e1b
Signed-off-by: Fam Zheng <famz@redhat.com>
958e1b
Signed-off-by: Jeff E. Nelson <jen@redhat.com>
958e1b
---
958e1b
 block/vmdk.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 1 file changed, 48 insertions(+)
958e1b
958e1b
diff --git a/block/vmdk.c b/block/vmdk.c
958e1b
index 7ea3aef..eda2962 100644
958e1b
--- a/block/vmdk.c
958e1b
+++ b/block/vmdk.c
958e1b
@@ -1944,6 +1944,53 @@ static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
958e1b
     return info;
958e1b
 }
958e1b
 
958e1b
+static int vmdk_check(BlockDriverState *bs, BdrvCheckResult *result,
958e1b
+                      BdrvCheckMode fix)
958e1b
+{
958e1b
+    BDRVVmdkState *s = bs->opaque;
958e1b
+    VmdkExtent *extent = NULL;
958e1b
+    int64_t sector_num = 0;
958e1b
+    int64_t total_sectors = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
958e1b
+    int ret;
958e1b
+    uint64_t cluster_offset;
958e1b
+
958e1b
+    if (fix) {
958e1b
+        return -ENOTSUP;
958e1b
+    }
958e1b
+
958e1b
+    for (;;) {
958e1b
+        if (sector_num >= total_sectors) {
958e1b
+            return 0;
958e1b
+        }
958e1b
+        extent = find_extent(s, sector_num, extent);
958e1b
+        if (!extent) {
958e1b
+            fprintf(stderr,
958e1b
+                    "ERROR: could not find extent for sector %" PRId64 "\n",
958e1b
+                    sector_num);
958e1b
+            break;
958e1b
+        }
958e1b
+        ret = get_cluster_offset(bs, extent, NULL,
958e1b
+                                 sector_num << BDRV_SECTOR_BITS,
958e1b
+                                 0, &cluster_offset);
958e1b
+        if (ret == VMDK_ERROR) {
958e1b
+            fprintf(stderr,
958e1b
+                    "ERROR: could not get cluster_offset for sector %"
958e1b
+                    PRId64 "\n", sector_num);
958e1b
+            break;
958e1b
+        }
958e1b
+        if (ret == VMDK_OK && cluster_offset >= bdrv_getlength(extent->file)) {
958e1b
+            fprintf(stderr,
958e1b
+                    "ERROR: cluster offset for sector %"
958e1b
+                    PRId64 " points after EOF\n", sector_num);
958e1b
+            break;
958e1b
+        }
958e1b
+        sector_num += extent->cluster_sectors;
958e1b
+    }
958e1b
+
958e1b
+    result->corruptions++;
958e1b
+    return 0;
958e1b
+}
958e1b
+
958e1b
 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
958e1b
 {
958e1b
     int i;
958e1b
@@ -2017,6 +2064,7 @@ static BlockDriver bdrv_vmdk = {
958e1b
     .instance_size                = sizeof(BDRVVmdkState),
958e1b
     .bdrv_probe                   = vmdk_probe,
958e1b
     .bdrv_open                    = vmdk_open,
958e1b
+    .bdrv_check                   = vmdk_check,
958e1b
     .bdrv_reopen_prepare          = vmdk_reopen_prepare,
958e1b
     .bdrv_read                    = vmdk_co_read,
958e1b
     .bdrv_write                   = vmdk_co_write,
958e1b
-- 
958e1b
2.1.0
958e1b