From: Fam Zheng <famcool@gmail.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, Fam Zheng <famcool@gmail.com>,
hch@lst.de, stefanha@gmail.com
Subject: [Qemu-devel] [PATCH v7 08/12] VMDK: change get_cluster_offset return type
Date: Sun, 3 Jul 2011 23:06:48 +0800 [thread overview]
Message-ID: <1309705612-27079-9-git-send-email-famcool@gmail.com> (raw)
In-Reply-To: <1309705612-27079-1-git-send-email-famcool@gmail.com>
The return type of get_cluster_offset was an offset that use 0 to denote
'not allocated', this will be no longer true for flat extents, as we see
flat extent file as a single huge cluster whose offset is 0 and length
is the whole file length.
So now we use int return value, 0 means success and otherwise offset
invalid.
Signed-off-by: Fam Zheng <famcool@gmail.com>
---
block/vmdk.c | 79 ++++++++++++++++++++++++++++++---------------------------
1 files changed, 42 insertions(+), 37 deletions(-)
diff --git a/block/vmdk.c b/block/vmdk.c
index 196419b..5bb6392 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -664,26 +664,31 @@ 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 (m_data)
m_data->valid = 0;
+ if (extent->flat) {
+ *cluster_offset = 0;
+ return 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]) {
@@ -713,28 +718,29 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
l2_table,
extent->l2_size * sizeof(uint32_t)
) != extent->l2_size * sizeof(uint32_t)) {
- return 0;
+ 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 (!allocate)
- return 0;
+ if (!*cluster_offset) {
+ if (!allocate) {
+ 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
@@ -743,8 +749,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;
@@ -754,8 +760,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 VmdkExtent *find_extent(BDRVVmdkState *s,
@@ -779,7 +785,6 @@ static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
int nb_sectors, int *pnum)
{
BDRVVmdkState *s = bs->opaque;
-
int64_t index_in_cluster, n, ret;
uint64_t offset;
VmdkExtent *extent;
@@ -788,15 +793,13 @@ static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
if (!extent) {
return 0;
}
- if (extent->flat) {
- n = extent->end_sector - sector_num;
- ret = 1;
- } else {
- offset = get_cluster_offset(bs, extent, NULL, sector_num * 512, 0);
- index_in_cluster = sector_num % extent->cluster_sectors;
- n = extent->cluster_sectors - index_in_cluster;
- ret = offset ? 1 : 0;
- }
+ ret = get_cluster_offset(bs, extent, NULL,
+ sector_num * 512, 0, &offset);
+ /* get_cluster_offset returning 0 means success */
+ ret = !ret;
+
+ index_in_cluster = sector_num % extent->cluster_sectors;
+ n = extent->cluster_sectors - index_in_cluster;
if (n > nb_sectors)
n = nb_sectors;
*pnum = n;
@@ -817,14 +820,15 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
if (!extent) {
return -EIO;
}
- 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) {
- // try to read from parent image, if exist
+ if (ret) {
+ /* if not allocated, try to read from parent image, if exist */
if (bs->backing_hd) {
if (!vmdk_is_cid_valid(bs))
return -1;
@@ -850,7 +854,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
{
BDRVVmdkState *s = bs->opaque;
VmdkExtent *extent = NULL;
- int n;
+ int n, ret;
int64_t index_in_cluster;
uint64_t cluster_offset;
VmdkMetaData m_data;
@@ -868,13 +872,14 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
if (!extent) {
return -EIO;
}
- cluster_offset = get_cluster_offset(
+ ret = get_cluster_offset(
bs,
extent,
&m_data,
- sector_num << 9, 1);
- if (!cluster_offset) {
- return -1;
+ sector_num << 9, 1,
+ &cluster_offset);
+ if (ret) {
+ return -EINVAL;
}
index_in_cluster = sector_num % extent->cluster_sectors;
n = extent->cluster_sectors - index_in_cluster;
next prev parent reply other threads:[~2011-07-03 15:08 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-03 15:06 [Qemu-devel] [PATCH v7 00/12] Adding VMDK monolithic flat support Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 01/12] VMDK: introduce VmdkExtent Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 02/12] VMDK: bugfix, align offset to cluster in get_whole_cluster Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 03/12] VMDK: probe for monolithicFlat images Fam Zheng
2011-07-04 10:38 ` Kevin Wolf
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 04/12] VMDK: separate vmdk_open by format version Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 05/12] VMDK: add field BDRVVmdkState.desc_offset Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 06/12] VMDK: flush multiple extents Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 07/12] VMDK: move 'static' cid_update flag to bs field Fam Zheng
2011-07-03 15:06 ` Fam Zheng [this message]
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 09/12] VMDK: open/read/write for monolithicFlat image Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 10/12] VMDK: create different subformats Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 11/12] VMDK: fix coding style Fam Zheng
2011-07-03 15:06 ` [Qemu-devel] [PATCH v7 12/12] block: add bdrv_get_allocated_file_size() operation Fam Zheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1309705612-27079-9-git-send-email-famcool@gmail.com \
--to=famcool@gmail.com \
--cc=hch@lst.de \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).