Signed-off-by: Benoit Canet <ben...@irqsave.net>
---
 block/qcow2.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/block/qcow2.c b/block/qcow2.c
index 34b2a87..3cd1051 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -54,9 +54,19 @@ typedef struct {
     uint32_t len;
 } QCowExtension;
 
+typedef struct {
+    uint64_t offset;
+    uint32_t size;
+    uint32_t max;
+    uint32_t flags;
+    uint8_t  hash_algo;
+    char     reserved[56];
+} QCowDedupConfExtension;
+
 #define  QCOW2_EXT_MAGIC_END 0
 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
+#define  QCOW2_EXT_MAGIC_DEDUP_TABLE 0xCD8E819B
 
 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
 {
@@ -85,6 +95,7 @@ static int qcow2_read_extensions(BlockDriverState *bs, 
uint64_t start_offset,
     QCowExtension ext;
     uint64_t offset;
     int ret;
+    QCowDedupConfExtension dedup_conf_ext;
 
 #ifdef DEBUG_EXT
     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, 
end_offset);
@@ -149,6 +160,28 @@ static int qcow2_read_extensions(BlockDriverState *bs, 
uint64_t start_offset,
             }
             break;
 
+        case QCOW2_EXT_MAGIC_DEDUP_TABLE:
+            if (ext.len > sizeof(dedup_conf_ext)) {
+                fprintf(stderr, "ERROR: dedup_conf_ext: len=%u too large"
+                        " (>=%zu)\n",
+                        ext.len, sizeof(dedup_conf_ext));
+                return 2;
+            }
+            ret = bdrv_pread(bs->file, offset,
+                             &dedup_conf_ext, ext.len);
+            if (ret < 0) {
+                return ret;
+            }
+            s->dedup_conf_offset =
+                be64_to_cpu(dedup_conf_ext.offset);
+            s->dedup_conf_size =
+                be32_to_cpu(dedup_conf_ext.size);
+            s->dedup_max_incarnations =
+                be32_to_cpu(dedup_conf_ext.max);
+            s->dedup_hash_algo = dedup_conf_ext.hash_algo;
+            s->dedup_dirty = dedup_conf_ext.flags & QCOW_DEDUP_DIRTY;
+            break;
+
         default:
             /* unknown magic - save it in case we need to rewrite the header */
             {
@@ -1006,6 +1039,7 @@ int qcow2_update_header(BlockDriverState *bs)
     uint32_t refcount_table_clusters;
     size_t header_length;
     Qcow2UnknownHeaderExtension *uext;
+    QCowDedupConfExtension dedup_conf_ext;
 
     buf = qemu_blockalign(bs, buflen);
 
@@ -1109,6 +1143,25 @@ int qcow2_update_header(BlockDriverState *bs)
     buf += ret;
     buflen -= ret;
 
+    if (s->has_dedup) {
+        memset(&dedup_conf_ext, 0, sizeof(dedup_conf_ext));
+        dedup_conf_ext.offset    = cpu_to_be64(s->dedup_conf_offset);
+        dedup_conf_ext.size      = cpu_to_be32(s->dedup_conf_size);
+        dedup_conf_ext.max       = cpu_to_be32(s->dedup_max_incarnations);
+        dedup_conf_ext.hash_algo = s->dedup_hash_algo;
+        dedup_conf_ext.flags     = s->dedup_dirty ? QCOW_DEDUP_DIRTY : 0;
+        ret = header_ext_add(buf,
+                             QCOW2_EXT_MAGIC_DEDUP_TABLE,
+                             &dedup_conf_ext,
+                             sizeof(dedup_conf_ext),
+                             buflen);
+        if (ret < 0) {
+            goto fail;
+        }
+        buf += ret;
+        buflen -= ret;
+    }
+
     /* Keep unknown header extensions */
     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
-- 
1.7.10.4


Reply via email to