218e99
From 68c09ecc5cb4154d5d7ef839b3d20e7a4f45bc32 Mon Sep 17 00:00:00 2001
218e99
From: Paolo Bonzini <pbonzini@redhat.com>
218e99
Date: Fri, 18 Oct 2013 08:14:38 +0200
218e99
Subject: [PATCH 13/81] block: define get_block_status return value
218e99
218e99
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
218e99
Message-id: <1382084091-16636-14-git-send-email-pbonzini@redhat.com>
218e99
Patchwork-id: 54997
218e99
O-Subject: [RHEL 7.0 qemu-kvm PATCH 13/26] block: define get_block_status return value
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
Define the return value of get_block_status.  Bits 0, 1, 2 and 9-62
218e99
are valid; bit 63 (the sign bit) is reserved for errors.  Bits 3-8
218e99
are left for future extensions.
218e99
218e99
The return code is compatible with the old is_allocated API: if a driver
218e99
only returns 0 or 1 (aka BDRV_BLOCK_DATA) like is_allocated used to,
218e99
clients of is_allocated will not have any change in behavior.  Still,
218e99
we will return more precise information in the next patches and the
218e99
new definition of bdrv_is_allocated is already prepared for this.
218e99
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
(cherry picked from commit 4333bb71405f58a8dc8d3255feb3ca5960b0daf8)
218e99
---
218e99
 block.c               | 10 ++++++++--
218e99
 include/block/block.h | 26 ++++++++++++++++++++++++++
218e99
 2 files changed, 34 insertions(+), 2 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c               |   10 ++++++++--
218e99
 include/block/block.h |   26 ++++++++++++++++++++++++++
218e99
 2 files changed, 34 insertions(+), 2 deletions(-)
218e99
218e99
diff --git a/block.c b/block.c
218e99
index 772f5b7..8c583bf 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -3059,7 +3059,7 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
218e99
 
218e99
     if (!bs->drv->bdrv_co_get_block_status) {
218e99
         *pnum = nb_sectors;
218e99
-        return 1;
218e99
+        return BDRV_BLOCK_DATA;
218e99
     }
218e99
 
218e99
     return bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
218e99
@@ -3109,7 +3109,13 @@ int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
218e99
 int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
218e99
                                    int nb_sectors, int *pnum)
218e99
 {
218e99
-    return bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
218e99
+    int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
218e99
+    if (ret < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+    return
218e99
+        (ret & BDRV_BLOCK_DATA) ||
218e99
+        ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
218e99
 }
218e99
 
218e99
 /*
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index a733f5f..01f5c65 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -92,6 +92,32 @@ typedef struct BlockDevOps {
218e99
 #define BDRV_SECTOR_SIZE   (1ULL << BDRV_SECTOR_BITS)
218e99
 #define BDRV_SECTOR_MASK   ~(BDRV_SECTOR_SIZE - 1)
218e99
 
218e99
+/* BDRV_BLOCK_DATA: data is read from bs->file or another file
218e99
+ * BDRV_BLOCK_ZERO: sectors read as zero
218e99
+ * BDRV_BLOCK_OFFSET_VALID: sector stored in bs->file as raw data
218e99
+ *
218e99
+ * If BDRV_BLOCK_OFFSET_VALID is set, bits 9-62 represent the offset in
218e99
+ * bs->file where sector data can be read from as raw data.
218e99
+ *
218e99
+ * DATA == 0 && ZERO == 0 means that data is read from backing_hd if present.
218e99
+ *
218e99
+ * DATA ZERO OFFSET_VALID
218e99
+ *  t    t        t       sectors read as zero, bs->file is zero at offset
218e99
+ *  t    f        t       sectors read as valid from bs->file at offset
218e99
+ *  f    t        t       sectors preallocated, read as zero, bs->file not
218e99
+ *                        necessarily zero at offset
218e99
+ *  f    f        t       sectors preallocated but read from backing_hd,
218e99
+ *                        bs->file contains garbage at offset
218e99
+ *  t    t        f       sectors preallocated, read as zero, unknown offset
218e99
+ *  t    f        f       sectors read from unknown file or offset
218e99
+ *  f    t        f       not allocated or unknown offset, read as zero
218e99
+ *  f    f        f       not allocated or unknown offset, read from backing_hd
218e99
+ */
218e99
+#define BDRV_BLOCK_DATA         1
218e99
+#define BDRV_BLOCK_ZERO         2
218e99
+#define BDRV_BLOCK_OFFSET_VALID 4
218e99
+#define BDRV_BLOCK_OFFSET_MASK  BDRV_SECTOR_MASK
218e99
+
218e99
 typedef enum {
218e99
     BDRV_ACTION_REPORT, BDRV_ACTION_IGNORE, BDRV_ACTION_STOP
218e99
 } BlockErrorAction;
218e99
-- 
218e99
1.7.1
218e99