218e99
From 90d033044f22ff96e5fc7c409934f7318d03f103 Mon Sep 17 00:00:00 2001
218e99
From: Paolo Bonzini <pbonzini@redhat.com>
218e99
Date: Fri, 18 Oct 2013 08:14:39 +0200
218e99
Subject: [PATCH 14/81] block: return get_block_status data and flags for formats
218e99
218e99
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
218e99
Message-id: <1382084091-16636-15-git-send-email-pbonzini@redhat.com>
218e99
Patchwork-id: 54996
218e99
O-Subject: [RHEL 7.0 qemu-kvm PATCH 14/26] block: return get_block_status data and flags for formats
218e99
Bugzilla: 989646
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
(cherry picked from commit 4bc74be997a72922170f4f272fd5e8074a3ecc27)
218e99
---
218e99
 block/cow.c      |  8 +++++++-
218e99
 block/qcow.c     |  9 ++++++++-
218e99
 block/qcow2.c    | 16 ++++++++++++++--
218e99
 block/qed.c      | 35 ++++++++++++++++++++++++++++-------
218e99
 block/sheepdog.c |  2 +-
218e99
 block/vdi.c      | 13 ++++++++++++-
218e99
 block/vmdk.c     | 19 ++++++++++++++++++-
218e99
 block/vvfat.c    | 11 ++++++-----
218e99
 8 files changed, 94 insertions(+), 19 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block/cow.c      |    8 +++++++-
218e99
 block/qcow.c     |    9 ++++++++-
218e99
 block/qcow2.c    |   16 ++++++++++++++--
218e99
 block/qed.c      |   35 ++++++++++++++++++++++++++++-------
218e99
 block/sheepdog.c |    2 +-
218e99
 block/vdi.c      |   13 ++++++++++++-
218e99
 block/vmdk.c     |   19 ++++++++++++++++++-
218e99
 block/vvfat.c    |   11 ++++++-----
218e99
 8 files changed, 94 insertions(+), 19 deletions(-)
218e99
218e99
diff --git a/block/cow.c b/block/cow.c
218e99
index 5e71c76..6958808 100644
218e99
--- a/block/cow.c
218e99
+++ b/block/cow.c
218e99
@@ -191,7 +191,13 @@ static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
218e99
 static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
218e99
         int64_t sector_num, int nb_sectors, int *num_same)
218e99
 {
218e99
-    return cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
218e99
+    BDRVCowState *s = bs->opaque;
218e99
+    int ret = cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
218e99
+    int64_t offset = s->cow_sectors_offset + (sector_num << BDRV_SECTOR_BITS);
218e99
+    if (ret < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+    return (ret ? BDRV_BLOCK_DATA : 0) | offset | BDRV_BLOCK_OFFSET_VALID;
218e99
 }
218e99
 
218e99
 static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
218e99
diff --git a/block/qcow.c b/block/qcow.c
218e99
index 05af25c..4ab552e 100644
218e99
--- a/block/qcow.c
218e99
+++ b/block/qcow.c
218e99
@@ -410,7 +410,14 @@ static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
218e99
     if (n > nb_sectors)
218e99
         n = nb_sectors;
218e99
     *pnum = n;
218e99
-    return (cluster_offset != 0);
218e99
+    if (!cluster_offset) {
218e99
+        return 0;
218e99
+    }
218e99
+    if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypt_method) {
218e99
+        return BDRV_BLOCK_DATA;
218e99
+    }
218e99
+    cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
218e99
+    return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset;
218e99
 }
218e99
 
218e99
 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
218e99
diff --git a/block/qcow2.c b/block/qcow2.c
218e99
index 8d3bf5d..8e894b9 100644
218e99
--- a/block/qcow2.c
218e99
+++ b/block/qcow2.c
218e99
@@ -645,7 +645,8 @@ static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
218e99
 {
218e99
     BDRVQcowState *s = bs->opaque;
218e99
     uint64_t cluster_offset;
218e99
-    int ret;
218e99
+    int index_in_cluster, ret;
218e99
+    int64_t status = 0;
218e99
 
218e99
     *pnum = nb_sectors;
218e99
     qemu_co_mutex_lock(&s->lock);
218e99
@@ -655,7 +656,18 @@ static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
218e99
         return ret;
218e99
     }
218e99
 
218e99
-    return (cluster_offset != 0) || (ret == QCOW2_CLUSTER_ZERO);
218e99
+    if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
218e99
+        !s->crypt_method) {
218e99
+        index_in_cluster = sector_num & (s->cluster_sectors - 1);
218e99
+        cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
218e99
+        status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
218e99
+    }
218e99
+    if (ret == QCOW2_CLUSTER_ZERO) {
218e99
+        status |= BDRV_BLOCK_ZERO;
218e99
+    } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
218e99
+        status |= BDRV_BLOCK_DATA;
218e99
+    }
218e99
+    return status;
218e99
 }
218e99
 
218e99
 /* handle reading after the end of the backing file */
218e99
diff --git a/block/qed.c b/block/qed.c
218e99
index a573039..815ee1c 100644
218e99
--- a/block/qed.c
218e99
+++ b/block/qed.c
218e99
@@ -652,16 +652,36 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
218e99
 }
218e99
 
218e99
 typedef struct {
218e99
+    BlockDriverState *bs;
218e99
     Coroutine *co;
218e99
-    int is_allocated;
218e99
+    uint64_t pos;
218e99
+    int64_t status;
218e99
     int *pnum;
218e99
 } QEDIsAllocatedCB;
218e99
 
218e99
 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
218e99
 {
218e99
     QEDIsAllocatedCB *cb = opaque;
218e99
+    BDRVQEDState *s = cb->bs->opaque;
218e99
     *cb->pnum = len / BDRV_SECTOR_SIZE;
218e99
-    cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO);
218e99
+    switch (ret) {
218e99
+    case QED_CLUSTER_FOUND:
218e99
+        offset |= qed_offset_into_cluster(s, cb->pos);
218e99
+        cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
218e99
+        break;
218e99
+    case QED_CLUSTER_ZERO:
218e99
+        cb->status = BDRV_BLOCK_ZERO;
218e99
+        break;
218e99
+    case QED_CLUSTER_L2:
218e99
+    case QED_CLUSTER_L1:
218e99
+        cb->status = 0;
218e99
+        break;
218e99
+    default:
218e99
+        assert(ret < 0);
218e99
+        cb->status = ret;
218e99
+        break;
218e99
+    }
218e99
+
218e99
     if (cb->co) {
218e99
         qemu_coroutine_enter(cb->co, NULL);
218e99
     }
218e99
@@ -672,25 +692,26 @@ static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
218e99
                                                  int nb_sectors, int *pnum)
218e99
 {
218e99
     BDRVQEDState *s = bs->opaque;
218e99
-    uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
218e99
     size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
218e99
     QEDIsAllocatedCB cb = {
218e99
-        .is_allocated = -1,
218e99
+        .bs = bs,
218e99
+        .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
218e99
+        .status = BDRV_BLOCK_OFFSET_MASK,
218e99
         .pnum = pnum,
218e99
     };
218e99
     QEDRequest request = { .l2_table = NULL };
218e99
 
218e99
-    qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb;;
218e99
+    qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb;;
218e99
 
218e99
     /* Now sleep if the callback wasn't invoked immediately */
218e99
-    while (cb.is_allocated == -1) {
218e99
+    while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
218e99
         cb.co = qemu_coroutine_self();
218e99
         qemu_coroutine_yield();
218e99
     }
218e99
 
218e99
     qed_unref_l2_cache_entry(request.l2_table);
218e99
 
218e99
-    return cb.is_allocated;
218e99
+    return cb.status;
218e99
 }
218e99
 
218e99
 static int bdrv_qed_make_empty(BlockDriverState *bs)
218e99
diff --git a/block/sheepdog.c b/block/sheepdog.c
218e99
index e5398bb..f7cc76e 100644
218e99
--- a/block/sheepdog.c
218e99
+++ b/block/sheepdog.c
218e99
@@ -2299,7 +2299,7 @@ sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
218e99
                   end = DIV_ROUND_UP((sector_num + nb_sectors) *
218e99
                                      BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE);
218e99
     unsigned long idx;
218e99
-    int ret = 1;
218e99
+    int64_t ret = BDRV_BLOCK_DATA;
218e99
 
218e99
     for (idx = start; idx < end; idx++) {
218e99
         if (inode->data_vdi_id[idx] == 0) {
218e99
diff --git a/block/vdi.c b/block/vdi.c
218e99
index 1252ad4..bc97c39 100644
218e99
--- a/block/vdi.c
218e99
+++ b/block/vdi.c
218e99
@@ -479,12 +479,23 @@ static int64_t coroutine_fn vdi_co_get_block_status(BlockDriverState *bs,
218e99
     size_t sector_in_block = sector_num % s->block_sectors;
218e99
     int n_sectors = s->block_sectors - sector_in_block;
218e99
     uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
218e99
+    uint64_t offset;
218e99
+    int result;
218e99
+
218e99
     logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
218e99
     if (n_sectors > nb_sectors) {
218e99
         n_sectors = nb_sectors;
218e99
     }
218e99
     *pnum = n_sectors;
218e99
-    return VDI_IS_ALLOCATED(bmap_entry);
218e99
+    result = VDI_IS_ALLOCATED(bmap_entry);
218e99
+    if (!result) {
218e99
+        return 0;
218e99
+    }
218e99
+
218e99
+    offset = s->header.offset_data +
218e99
+                              (uint64_t)bmap_entry * s->block_size +
218e99
+                              sector_in_block * SECTOR_SIZE;
218e99
+    return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
218e99
 }
218e99
 
218e99
 static int vdi_co_read(BlockDriverState *bs,
218e99
diff --git a/block/vmdk.c b/block/vmdk.c
218e99
index a30c3b9..7456f9b 100644
218e99
--- a/block/vmdk.c
218e99
+++ b/block/vmdk.c
218e99
@@ -1059,7 +1059,24 @@ static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
218e99
                             sector_num * 512, 0, &offset);
218e99
     qemu_co_mutex_unlock(&s->lock);
218e99
 
218e99
-    ret = (ret == VMDK_OK || ret == VMDK_ZEROED);
218e99
+    switch (ret) {
218e99
+    case VMDK_ERROR:
218e99
+        ret = -EIO;
218e99
+        break;
218e99
+    case VMDK_UNALLOC:
218e99
+        ret = 0;
218e99
+        break;
218e99
+    case VMDK_ZEROED:
218e99
+        ret = BDRV_BLOCK_ZERO;
218e99
+        break;
218e99
+    case VMDK_OK:
218e99
+        ret = BDRV_BLOCK_DATA;
218e99
+        if (extent->file == bs->file) {
218e99
+            ret |= BDRV_BLOCK_OFFSET_VALID | offset;
218e99
+        }
218e99
+
218e99
+        break;
218e99
+    }
218e99
 
218e99
     index_in_cluster = sector_num % extent->cluster_sectors;
218e99
     n = extent->cluster_sectors - index_in_cluster;
218e99
diff --git a/block/vvfat.c b/block/vvfat.c
218e99
index 510a559..415fba3 100644
218e99
--- a/block/vvfat.c
218e99
+++ b/block/vvfat.c
218e99
@@ -2879,11 +2879,12 @@ static int64_t coroutine_fn vvfat_co_get_block_status(BlockDriverState *bs,
218e99
 {
218e99
     BDRVVVFATState* s = bs->opaque;
218e99
     *n = s->sector_count - sector_num;
218e99
-    if (*n > nb_sectors)
218e99
-	*n = nb_sectors;
218e99
-    else if (*n < 0)
218e99
-	return 0;
218e99
-    return 1;
218e99
+    if (*n > nb_sectors) {
218e99
+        *n = nb_sectors;
218e99
+    } else if (*n < 0) {
218e99
+        return 0;
218e99
+    }
218e99
+    return BDRV_BLOCK_DATA;
218e99
 }
218e99
 
218e99
 static int write_target_commit(BlockDriverState *bs, int64_t sector_num,
218e99
-- 
218e99
1.7.1
218e99