qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 10/12] VMDK: change get_cluster_offset return type t


From: Fam Zheng
Subject: [Qemu-devel] [PATCH 10/12] VMDK: change get_cluster_offset return type to success flag
Date: Sat, 4 Jun 2011 08:43:24 +0800

As mono flat format being introduced, cluster offset of 0 becomes a
valid value. get_cluster_offset now return non-zero on fail, and 0 on
success.

Signed-off-by: Fam Zheng <address@hidden>
---
 block/vmdk.c |   86 ++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/block/vmdk.c b/block/vmdk.c
index dd92377..85e868e 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -637,11 +637,12 @@ static int vmdk_open(BlockDriverState *bs, int flags)
     }
 }

-static uint64_t get_cluster_offset(BlockDriverState *bs,
+static int get_cluster_offset(BlockDriverState *bs,
                     VmdkExtent *extent,
                     VmdkMetaData *m_data,
                     uint64_t offset,
-                    int allocate);
+                    int allocate,
+                    uint64_t *cluster_offset);

 static int get_whole_cluster(BlockDriverState *bs,
                 VmdkExtent *extent,
@@ -702,25 +703,33 @@ static int vmdk_L2update(VmdkExtent *extent,
VmdkMetaData *m_data)
     return 0;
 }

-static uint64_t get_cluster_offset(BlockDriverState *bs,
+static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
-                                    uint64_t offset, int allocate)
+                                    uint64_t offset,
+                                    int allocate,
+                                    uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
     int min_index, i, j;
     uint32_t min_count, *l2_table, tmp = 0;
-    uint64_t cluster_offset;

+    if (extent->flat) {
+               if (m_data) {
+                       m_data->valid = 0;
+               }
+        *cluster_offset = 0;
+        return 0;
+    }
     if (m_data)
         m_data->valid = 0;

     l1_index = (offset >> 9) / extent->l1_entry_sectors;
     if (l1_index >= extent->l1_size)
-        return 0;
+        return -1;
     l2_offset = extent->l1_table[l1_index];
     if (!l2_offset)
-        return 0;
+        return -1;
     for(i = 0; i < L2_CACHE_SIZE; i++) {
         if (l2_offset == extent->l2_cache_offsets[i]) {
             /* increment the hit count */
@@ -748,28 +757,29 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
             (int64_t)l2_offset * 512,
             l2_table,
             extent->l2_size * sizeof(uint32_t)
-        ) != extent->l2_size * sizeof(uint32_t))
-        return 0;
+        ) != extent->l2_size * sizeof(uint32_t)) {
+        return -1;
+    }

     extent->l2_cache_offsets[min_index] = l2_offset;
     extent->l2_cache_counts[min_index] = 1;
  found:
     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
-    cluster_offset = le32_to_cpu(l2_table[l2_index]);
+    *cluster_offset = le32_to_cpu(l2_table[l2_index]);

-    if (!cluster_offset) {
+    if (!*cluster_offset) {
         if (!allocate)
-            return 0;
+            return -1;

         // Avoid the L2 tables update for the images that have snapshots.
-        cluster_offset = bdrv_getlength(extent->file);
+        *cluster_offset = bdrv_getlength(extent->file);
         bdrv_truncate(
             extent->file,
-            cluster_offset + (extent->cluster_sectors << 9)
+            *cluster_offset + (extent->cluster_sectors << 9)
         );

-        cluster_offset >>= 9;
-        tmp = cpu_to_le32(cluster_offset);
+        *cluster_offset >>= 9;
+        tmp = cpu_to_le32(*cluster_offset);
         l2_table[l2_index] = tmp;

         /* First of all we write grain itself, to avoid race condition
@@ -778,8 +788,8 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
          * or inappropriate VM shutdown.
          */
         if (get_whole_cluster(
-                bs, extent, cluster_offset, offset, allocate) == -1)
-            return 0;
+                bs, extent, *cluster_offset, offset, allocate) == -1)
+            return -1;

         if (m_data) {
             m_data->offset = tmp;
@@ -789,8 +799,8 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
             m_data->valid = 1;
         }
     }
-    cluster_offset <<= 9;
-    return cluster_offset;
+    *cluster_offset <<= 9;
+    return 0;
 }

 static int find_extent(BDRVVmdkState *s, int64_t sector_num, int start_idx)
@@ -822,15 +832,17 @@ static int vmdk_is_allocated(BlockDriverState
*bs, int64_t sector_num,
         n = extent->sectors - sector_num;
         ret = 1;
     } else {
-        offset = get_cluster_offset(bs, extent, NULL, sector_num * 512, 0);
+        ret = get_cluster_offset(bs, extent, NULL, sector_num << 9,
0, &offset);
+        if (ret) {
+            return 0;
+               }
         index_in_cluster = sector_num % extent->cluster_sectors;
         n = extent->cluster_sectors - index_in_cluster;
-        ret = offset ? 1 : 0;
     }
     if (n > nb_sectors)
         n = nb_sectors;
     *pnum = n;
-    return ret;
+    return 1;
 }

 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
@@ -849,13 +861,14 @@ static int vmdk_read(BlockDriverState *bs,
int64_t sector_num,
             return -1;
         }
         extent = &s->extents[ext_idx];
-        cluster_offset = get_cluster_offset(
-                            bs, extent, NULL, sector_num << 9, 0);
+        ret = get_cluster_offset(
+                            bs, extent, NULL, sector_num << 9, 0,
+                            &cluster_offset);
         index_in_cluster = sector_num % extent->cluster_sectors;
         n = extent->cluster_sectors - index_in_cluster;
         if (n > nb_sectors)
             n = nb_sectors;
-        if (!cluster_offset) {
+        if (ret) {
             // try to read from parent image, if exist
             if (bs->backing_hd) {
                 if (!vmdk_is_cid_valid(bs, extent))
@@ -867,8 +880,12 @@ static int vmdk_read(BlockDriverState *bs,
int64_t sector_num,
                 memset(buf, 0, 512 * n);
             }
         } else {
-            if(bdrv_pread(bs->file, cluster_offset + index_in_cluster
* 512, buf, n * 512) != n * 512)
+            if(bdrv_pread(extent->file,
+                                   cluster_offset + index_in_cluster * 512,
+                                   buf, n * 512)
+                               != n * 512) {
                 return -1;
+                       }
         }
         nb_sectors -= n;
         sector_num += n;
@@ -883,6 +900,7 @@ static int vmdk_write(BlockDriverState *bs,
int64_t sector_num,
     BDRVVmdkState *s = bs->opaque;
     VmdkExtent *extent;
     int ext_idx = 0;
+    int ret;
     int n, index_in_cluster;
     uint64_t cluster_offset;
     VmdkMetaData m_data;
@@ -901,20 +919,24 @@ static int vmdk_write(BlockDriverState *bs,
int64_t sector_num,
             return -1;
         }
         extent = &s->extents[ext_idx];
-        cluster_offset = get_cluster_offset(
+        ret = get_cluster_offset(
                                 bs,
                                 extent,
                                 &m_data,
-                                sector_num << 9, 1);
-        if (!cluster_offset)
+                                sector_num << 9, 1,
+                                &cluster_offset);
+        if (ret)
             return -1;
         index_in_cluster = sector_num & (extent->cluster_sectors - 1);
         n = extent->cluster_sectors - index_in_cluster;
         if (n > nb_sectors)
             n = nb_sectors;
-
-        if (bdrv_pwrite(bs->file, cluster_offset + index_in_cluster *
512, buf, n * 512) != n * 512)
+        if (bdrv_pwrite(extent->file,
+                        cluster_offset + index_in_cluster * 512,
+                        buf, n * 512)
+                    != n * 512) {
             return -1;
+               }
         if (m_data.valid) {
             /* update L2 tables */
             if (vmdk_L2update(extent, &m_data) == -1)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]