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