Skip to contentSkip to conversation

QCow2 deduplication write mechanism

RFC In review Benoît Canet 4 participants 6 messages Sep 27, 2012 Block layer

lore
Share & tools
lore
#0 lore raw

cover QCow2 deduplication write mechanism

This patchset introduce a partial implementation of a deduplication mechanism for QCow2.
This patchset layout the disk format changes in the qcow2 spec and provide a partial implementation of the write mechanisms.

Albeit being partial this is posted to get an early feedback and see if things are on track.

Benoît Canet (2):
qcow2: modify spec for deduplication
qcow2: Deduplication write mechanism.

 block/Makefile.objs  |    2 +-
 block/qcow2-dedup.c  |  368 ++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c        |   74 +++++++++-
 block/qcow2.h        |   40 +++++-
 docs/specs/qcow2.txt |    6 +-
 5 files changed, 486 insertions(+), 4 deletions(-)
 create mode 100644 block/qcow2-dedup.c
Signature · 1 line
1.7.10.4
#1 lore raw

RFC 1/2 qcow2: modify spec for deduplication

---
 docs/specs/qcow2.txt |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
docs/specs/qcow2.txt +5 -1
index 36a559d..e8bc189 100644
@@ -80,7 +80,10 @@ in the description of a field.
                                 tables to repair refcounts before accessing the
                                 image.
 
-                    Bits 1-63:  Reserved (set to 0)
+                    Bit 1:      Deduplication bit.  If this bit is set then
+                                deduplication is used on this image.
+
+                    Bits 2-63:  Reserved (set to 0)
 
          80 -  87:  compatible_features
                     Bitmask of compatible features. An implementation can
@@ -116,6 +119,7 @@ be stored. Each extension has a structure like the following:
                         0x00000000 - End of the header extension area
                         0xE2792ACA - Backing file format name
                         0x6803f857 - Feature name table
+                        0xCD8E819B - Deduplication table offset and length
                         other      - Unknown header extension, can be safely
                                      ignored
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [partial RFC 1/2] qcow2: modify spec for deduplication

Quoted 21 lines · Benoît Canet
---
 docs/specs/qcow2.txt |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
docs/specs/qcow2.txt +4 -1
index 36a559d..e8bc189 100644
@@ -80,7 +80,10 @@ in the description of a field.
                                 tables to repair refcounts before accessing the
                                 image.
 
-                    Bits 1-63:  Reserved (set to 0)
+                    Bit 1:      Deduplication bit.  If this bit is set then
+                                deduplication is used on this image.
+
+                    Bits 2-63:  Reserved (set to 0)
 
          80 -  87:  compatible_features
                     Bitmask of compatible features. An implementation can

docs/specs/qcow2.txt be stored. Each extension has a structure like the following: · line 122

Quoted 4 lines
docs/specs/qcow2.txt +1 -0
@@ -116,6 +119,7 @@ be stored. Each extension has a structure like the following:
                         0x00000000 - End of the header extension area
                         0xE2792ACA - Backing file format name
                         0x6803f857 - Feature name table
+                        0xCD8E819B - Deduplication table offset and length

Missing a description of what this looks like (see the section on '== Feature name table ==' for comparison).

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

RFC 2/2 qcow2: Deduplication write mechanism.

In reply to Benoît Canet
---
 block/Makefile.objs |    2 +-
 block/qcow2-dedup.c |  368 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c       |   74 ++++++++++-
 block/qcow2.h       |   40 +++++-
 4 files changed, 481 insertions(+), 3 deletions(-)
 create mode 100644 block/qcow2-dedup.c
block/Makefile.objs +1 -1
index b5754d3..37a8aab 100644
@@ -1,5 +1,5 @@
 block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
-block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
+block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-dedup.o
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
block/qcow2-dedup.c new file +368 -0

368 lines not on this page · raw message

block/qcow2.c +73 -1

108 lines not on this page · raw message

block/qcow2.h +39 -1
index b4eb654..fd2657b 100644
@@ -114,8 +114,10 @@ enum {
 enum {
     QCOW2_INCOMPAT_DIRTY_BITNR   = 0,
     QCOW2_INCOMPAT_DIRTY         = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
+    QCOW2_INCOMPAT_DEDUP_BITNR   = 1,
+    QCOW2_INCOMPAT_DEDUP         = 1 << QCOW2_INCOMPAT_DEDUP_BITNR,
 
-    QCOW2_INCOMPAT_MASK          = QCOW2_INCOMPAT_DIRTY,
+    QCOW2_INCOMPAT_MASK          = QCOW2_INCOMPAT_DIRTY | QCOW2_INCOMPAT_DEDUP,
 };
 
 /* Compatible feature bits */
@@ -132,6 +134,11 @@ typedef struct Qcow2Feature {
     char    name[46];
 } QEMU_PACKED Qcow2Feature;
 
+typedef struct QCowDedupHash {
+    uint8_t *hash;
+    QTAILQ_ENTRY(QCowDedupHash) next;
+} QCowDedupHash;
+
 typedef struct BDRVQcowState {
     int cluster_bits;
     int cluster_size;
@@ -148,6 +155,7 @@ typedef struct BDRVQcowState {
 
     Qcow2Cache* l2_table_cache;
     Qcow2Cache* refcount_block_cache;
+    Qcow2Cache *dedup_block_cache;
 
     uint8_t *cluster_cache;
     uint8_t *cluster_data;
@@ -160,6 +168,11 @@ typedef struct BDRVQcowState {
     int64_t free_cluster_index;
     int64_t free_byte_offset;
 
+    bool has_dedup;
+    uint64_t *dedup_table;
+    uint64_t dedup_table_offset;
+    uint32_t dedup_table_size;
+
     CoMutex lock;
 
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
@@ -181,6 +194,7 @@ typedef struct BDRVQcowState {
     size_t unknown_header_fields_size;
     void* unknown_header_fields;
     QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
+    QTAILQ_HEAD(, QCowDedupHash) undedupable_hashes;
 } BDRVQcowState;
 
 /* XXX: use std qcow open function ? */
@@ -333,4 +347,28 @@ int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
     void **table);
 int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
 
+/* qcow2-dedup functions */
+int qcow2_dedup_read_missing_and_concatenate(BlockDriverState *bs,
+                                             QEMUIOVector *qiov,
+                                             uint64_t sector,
+                                             int remaining_sectors,
+                                             uint8_t *dedup_cluster_data,
+                                             int *dedup_cluster_data_nr);
+
+int qcow2_dedup(BlockDriverState *bs,
+                uint8_t *dedup_cluster_data,
+                int dedup_cluster_data_nr,
+                int64_t sector_num,
+                int remaining_sectors,
+                int *next_non_dedupable_sectors_nr,
+                int *skip_before_dedup_cluster_nr,
+                uint8_t **next_call_first_hash);
+
+void qcow2_dedup_write_new_hashes(BlockDriverState *bs,
+                                  uint64_t cluster_offset,
+                                  int count);
+
+void qcow2_dedup_free_cluster(BlockDriverState *bs,
+                              uint64_t cluster_offset);
+
 #endif
Signature · 1 line
1.7.10.4
#4 lore raw
In reply to Benoît Canet
Quoted 7 lines · Benoît Canet

This patchset introduce a partial implementation of a deduplication mechanism for QCow2.
This patchset layout the disk format changes in the qcow2 spec and provide a partial implementation of the write mechanisms.

Albeit being partial this is posted to get an early feedback and see if things are on track.

If I understood correctly, this does cluster-level dedup within a qcow2 image.

What is the motivation here? Reduce space usage if a guest copies files internally?

Why use cluster granularity? If the guest uses smaller granularity, it will misalign the data wrt cluster boundaries, and deduplication will fail.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 2 lines

If I understood correctly, this does cluster-level dedup within a qcow2 image.

yes

Quoted 2 lines

What is the motivation here? Reduce space usage if a guest copies files internally?

First use case is people using LXC, OpenVZ or chroot inside vms they rent to their "cloud" provider.

Second use case is applications generating duplicates blocks. A well known CAD software does this and on a filer doing deduplication on 4KB blocks the dedup ratio is around five.

Quoted 3 lines

Why use cluster granularity? If the guest uses smaller granularity, it will misalign the data wrt cluster boundaries, and deduplication will fail.

Using cluster granularity would allow to use the qcow2 refcount and l1/l2 tables to track duplicated blocks.
The patch embryo does read the missing data if given data is misaligned. To avoid hurting performance using a smaller cluster size with the adequate changes in qcow2 may help.

Regards

Benoît