Skip to contentSkip to conversation

QCOW2 deduplication core functionality

RFC In review Benoît Canet 4 participants 41 messages Jun 20, 2013 – Jul 17, 2013 Block layer

lore
Share & tools
lore
#0 lore raw

v8 cover QCOW2 deduplication core functionality

Hello,

This is the new patchset of the QCOW2 deduplication feature using an on disk key value store modelled after the SILT one.

This is more a millestone reached post than a for review post.

Performance is slow because the code is issuing a lot of read and write to the SSD in a sequential ways.
The next revision will focus on parallelising the IOs.

Kevin: the first patch add the description of the QCOW2 journal structure.
The code make it easy to have multiple journal in QCOW2. Do you think it could fit others QCOW2 usages.
Patches describing the others data structure are comming.

Best regards

Benoît

Benoît Canet (24):
qcow2: Add journal specification.
qcow2: Add deduplication structures and fields.
qcow2: Add journal.
qcow2: Create the log store.
qcow2: Add the hash store.
qcow2: Add the deduplication store.
qcow2: Add qcow2_dedup_read_missing_and_concatenate
qcow2: Create a way to link to l2 tables when deduplicating.
qcow2: Make qcow2_update_cluster_refcount public.
qcow2: Add qcow2_dedup and related functions
qcow2: Add qcow2_dedup_store_new_hashes.
qcow2: Do allocate on rewrite on the dedup case.
qcow2: Implement qcow2_compute_cluster_hash.
qcow2: Load and save deduplication table header extension.
qcow2: Extract qcow2_set_incompat_feature and
qcow2_clear_incompat_feature.
block: Add qcow2_dedup format and image creation code.
qcow2: Drop hash for a given cluster when dedup makes refcount >

    2^16/2.

qcow2: Remove hash when cluster is deleted.
qcow2: Integrate deduplication in qcow2_co_writev loop.
qcow2: Serialize write requests when deduplication is activated.
qcow2: Integrate SKEIN hash algorithm in deduplication.
qcow2: Add qcow2_dedup_init and qcow2_dedup_close.
qcow2: Enable the deduplication feature.
qcow2: Enable deduplication tests

 block/Makefile.objs          |    1 +
 block/qcow2-cluster.c        |   13 +-
 block/qcow2-dedup.c          |  778 +++++++++++++++++++++++++
 block/qcow2-hash-store.c     |  802 ++++++++++++++++++++++++++
 block/qcow2-journal.c        |  587 +++++++++++++++++++
 block/qcow2-log-store.c      | 1281 ++++++++++++++++++++++++++++++++++++++++++
 block/qcow2-refcount.c       |   20 +-
 block/qcow2-store.c          |  771 +++++++++++++++++++++++++
 block/qcow2.c                |  431 +++++++++++++-
 block/qcow2.h                |  357 +++++++++++-
 configure                    |  121 +++-
 docs/specs/qcow2.txt         |   42 ++
 include/block/block_int.h    |    1 +
 tests/qemu-iotests/common    |    6 +
 tests/qemu-iotests/common.rc |    3 +-
 15 files changed, 5144 insertions(+), 70 deletions(-)
 create mode 100644 block/qcow2-dedup.c
 create mode 100644 block/qcow2-hash-store.c
 create mode 100644 block/qcow2-journal.c
 create mode 100644 block/qcow2-log-store.c
 create mode 100644 block/qcow2-store.c
Signature · 1 line
1.7.10.4
#1 lore raw

RFC v8 1/24 qcow2: Add journal specification.

---
 docs/specs/qcow2.txt |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
docs/specs/qcow2.txt +42 -0
index 36a559d..a4ffc85 100644
@@ -350,3 +350,45 @@ Snapshot table entry:
         variable:   Unique ID string for the snapshot (not null terminated)
 
         variable:   Name of the snapshot (not null terminated)
+
+== Journal ==
+
+QCOW2 can use one or more instance of a metadata journal.
+
+A journal is a sequential log of journal entries appended on a previously
+allocated and reseted area.
+A journal is designed like a linked list with each entry pointing to the next
+so it's easy to iterate over entries.
+
+A journal uses the following constants to denote the type of each entry
+
+TYPE_NONE = 0xFF      default value of any bytes in a reseted journal
+TYPE_END  = 1         the entry ends a journal cluster and point to the next
+                      cluster
+TYPE_HASH = 2         the entry contains a deduplication hash
+
+QCOW2 journal entry:
+
+    Byte 0         :    Size of the entry: size = 2 + n with size <= 254
+
+         1         :    Type of the entry
+
+         2 - size  :    The optional n bytes structure carried by entry
+
+A journal is divided into clusters and no journal entry can be spilled on two
+clusters. This avoid having to read more than one cluster to get a single entry.
+
+For this purpose an entry with the end type is added at the end of a journal
+cluster before starting to write in the next cluster.
+The size of such an entry is set so the entry points to the next cluster.
+
+As any journal cluster must be ended with an end entry the size of regular
+journal entries is limited to 254 bytes in order to always left room for an end
+entry which mimimal size is two bytes.
+
+The only cases where size > 254 are none entries where size = 255.
+
+The replay of a journal stop when the first end none entry is reached.
+
+The journal cluster size is 4096 bytes.
+
Signature · 1 line
1.7.10.4
#18 lore raw

RFC v8 2/24 qcow2: Add deduplication structures and fields.

In reply to Benoît Canet
---
 block/qcow2.h |  203 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 201 insertions(+), 2 deletions(-)
block/qcow2.h +201 -2
index 9421843..953edfe 100644
@@ -57,7 +57,182 @@
 #define REFCOUNT_CACHE_SIZE 4
 
 #define DEFAULT_CLUSTER_SIZE 65536
-
+#define DEFAULT_DEDUP_CLUSTER_SIZE 4096
+
+#define HASH_LENGTH 32
+
+/* indicate that this cluster hash has been deleted from the key value store */
+#define QCOW_DEDUP_DELETED (1LL << 61)
+/* indicate that the hash structure is empty and miss offset */
+#define QCOW_DEDUP_FLAG_EMPTY   (1LL << 62)
+
+#define SSD_ERASE_BLOCK_SIZE (512 * 1024) /* match SSD erase block size */
+#define JOURNAL_CLUSTER_SIZE 4096       /* used to read entries */
+#define HASH_STORE_CLUSTER_SIZE 4096
+
+#define QCOW_LOG_END_SIZE 2            /* size of a end block journal entry */
+#define QCOW_LOG_STORE_ENTRY_USED (1LL << 60) /* mark used entry in table */
+#define QCOW_LOG_STORE_BUCKET_SIZE 4   /* size of a cuckoo hash bucket */
+#define QCOW_LOG_STORE_MAX_KICKS 128   /* max numbers of cuckoo hash kicks */
+#define QCOW_LOG_STORE_JOURNAL_RATIO 2 /* the ratio to compute the extra
+                                        * room the journal will take based
+                                        * on the log store size
+                                        */
+#define QCOW2_NB_INCARNATION_GOAL  128 /* targeted number of incarnation */
+
+#define QCOW_DEDUP_DIRTY 1 /* dirty flag in the qcow2 header extension */
+
+typedef enum {
+    QCOW_LOG_NONE = 0xFF,     /* on SSD erased clusters will mark none */
+    QCOW_LOG_END = 1,         /* end a block and point to the next */
+    QCOW_LOG_HASH = 2,        /* used to journalize a QCowHashInfo */
+} QCowLogEntryType;
+
+typedef enum {
+    QCOW_HASH_SHA256 = 0,
+    QCOW_HASH_SHA3   = 1,
+    QCOW_HASH_SKEIN  = 2,
+} QCowHashAlgo;
+
+typedef struct {
+    uint8_t data[HASH_LENGTH]; /* 32 bytes hash of a given cluster */
+} __attribute__((packed)) QCowHash;
+
+/* deduplication info */
+typedef struct {
+    QCowHash hash;
+    uint64_t physical_sect;       /* where the cluster is stored on disk */
+    uint64_t first_logical_sect;  /* logical sector of the first occurrence of
+                                   * this cluster
+                                   */
+} __attribute__((packed)) QCowHashInfo;
+
+/* Used to keep a single precomputed hash between the calls of the dedup
+ * function
+ */
+typedef struct {
+    QCowHashInfo hash_info;
+    bool reuse;     /* The main deduplication function can set this field to
+                     * true before exiting to avoid computing the same hash
+                     * twice. It's a speed optimization.
+                     */
+} QcowPersistentHash;
+
+/* Undedupable hashes that must be written later to disk */
+typedef struct QCowHashElement {
+    QCowHashInfo hash_info;
+    QTAILQ_ENTRY(QCowHashElement) next;
+} QCowHashElement;
+
+typedef struct {
+    QcowPersistentHash phash;  /* contains a hash persisting between calls of
+                                * qcow2_dedup()
+                                */
+    QTAILQ_HEAD(, QCowHashElement) undedupables;
+    uint64_t nb_clusters_processed;
+    uint64_t nb_undedupable_sectors;
+} QCowDedupState;
+
+/* The code must take care that the maximum size field of a QCowJournalEntry
+ * will be no more than 254 bytes.
+ * It's required to save the 2 bytes of room for QCOW_LOG_END entries
+ * in every cases
+ */
+typedef union {
+    QCowHashInfo hash_info;
+    uint8_t      padding[254]; /* note the extra two bytes of padding to avoid
+                                * read overflow.
+                                */
+} QCowJournalEntryUnion;
+
+typedef struct {
+    uint8_t size;            /* maximum size of a journal entry is 254 bytes */
+    uint8_t type;            /* contains a QCowLogEntryType for future usage */
+    QCowJournalEntryUnion u;
+} __attribute__((packed)) QCowJournalEntry;
+
+typedef struct {
+    uint64_t sector;                  /* the journal physical on disk sector */
+    uint64_t size;                    /* the size of the journal in bytes */
+    uint64_t index;                   /* index of next buf cluster to write */
+    uint8_t  *write_buf;              /* used to buffer written data */
+    uint64_t offset_in_buf;           /* the offset in the write buffer */
+    bool     flushed;                 /* true if the buffer reached disk*/
+    uint8_t  *read_cache;             /* used to cache read data */
+    int64_t read_index;               /* index the cached read cluster */
+    bool started;                     /* has the journal been resumed */
+} QCowJournal;
+
+typedef struct {
+    QCowJournal journal;          /* the journal this log store will use */
+    uint32_t order;               /* the number of bits used for the sub hashes
+                                   * as sub hashes will be used as an index for
+                                   * locating each bucket nb_bucket = 2^order
+                                   */
+    uint16_t nb_kicks;            /* the number of cuckoo hash kicks done */
+    bool *kick_map;               /* take care of not doing kick path loops */
+    QCowHashInfo *hash_table;     /* nb_buckets * QCOW_LOG_STORE_BUCKET_SIZE */
+    QCowHashInfo *hash_table_copy; /* copy of the hash table for packing */
+
+    /* members required to freeze a log store into a hash store consumable
+     * hash table (incarnation)
+     */
+    uint8_t *write_buf;           /* the buffer used to write */
+    uint64_t write_buf_offset;    /* the on disk offset of the buffer */
+    uint32_t in_buf_offset;       /* the current offset in the write buffer */
+} QCowLogStore;
+
+/* a QCowIncarnation is a frozen QCowLogStore
+ * Freezes are read only and their in ram filters is queried from the youngest
+ * to the oldest in order to know if their hash table contains a given
+ * QCowHashInfo.
+ * If so a read is issued to retrieve the QCowHashInfo.
+ */
+typedef struct QCowIncarnation {
+    uint64_t filter_offset;      /* the on disk offset of the ram filter */
+    uint64_t hash_table_offset;  /* the on disk offset of the hash table
+                                  * (should be just after the end of the filter)
+                                  */
+    uint64_t size;               /* the on disk size of the incarnation */
+    uint8_t  *filter;            /* an in ram filter */
+    QTAILQ_ENTRY(QCowIncarnation) next;
+} QCowIncarnation;
+
+typedef struct QCowLimbo {
+    uint64_t offset;             /* the on disk offset of the to reincarnate
+                                  * disk space
+                                  */
+    QTAILQ_ENTRY(QCowLimbo) next;
+} QCowLimbo;
+
+typedef struct {
+    uint32_t order;               /* the number of bits used for the sub hashes
+                                   * as sub hashes will be used as an index for
+                                   * locating each bucket nb_bucket = 2^order
+                                   */
+    uint32_t nb_incarnations;     /* the current number of incarnations */
+    uint32_t nb_in_limbo;         /* the number of dead incarnations */
+    QTAILQ_HEAD(incarnations_head, QCowIncarnation) incarnations;
+                                               /* a list of frozen hash table
+                                                * ordered from the youngest
+                                                * to the oldest
+                                                */
+    QTAILQ_HEAD(in_limbo_head, QCowLimbo) in_limbo;
+                                       /* a list of dead incarnation which disk
+                                        * space can be recycled
+                                        */
+} QCowHashStore;
+
+typedef struct {
+    uint32_t      order;
+    QCowLogStore  log_store;         /* the current log store */
+    QCowLogStore  frozen_log_store;  /* the log store to incarnate */
+    bool          freezing;          /* are we incarnating a log store */
+    QCowHashStore hash_store;
+    CoMutex       insert_lock;       /* used to prevent multiple freeze attempts
+                                      * at the same time
+                                      */
+} QCowStore;
 
 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy_refcounts"
 
@@ -117,8 +292,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 */
@@ -163,6 +340,28 @@ typedef struct BDRVQcowState {
     int64_t free_cluster_index;
     int64_t free_byte_offset;
 
+    bool has_dedup;                     /* indicate if this image has dedup */
+
+    /* the following fields are saved in QCOW2 dedup header extension */
+    bool dedup_dirty;                   /* mapped to the header dirty flag */
+    uint64_t dedup_conf_offset;         /* disk offset of the dedup config */
+    size_t dedup_conf_size;             /* disk size of the dedup config */
+    QCowHashAlgo dedup_hash_algo;       /* the cryptographic hash algo used */
+    uint32_t dedup_max_incarnations;    /* the maximum number of incarnations in
+                                         * hash store -> oldest incarnation will
+                                         * be dropped. It's harmless for the
+                                         * dedup and allow to scale.
+                                         * The whole thing act as a FIFO or LRU
+                                         * if value is 0 there is no limits
+                                         */
+
+    QCowStore key_value_store;          /* the key value store used to store
+                                         * dedup hashes
+                                         */
+    int freeze_errno;                   /* catch errors when incarnating */
+    Coroutine *freeze_co;               /* coroutine used when freezing */
+    Coroutine *load_filter_co;          /* used to load incarnations filters */
+
     CoMutex lock;
 
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
Signature · 1 line
1.7.10.4
#19 lore raw

RFC v8 3/24 qcow2: Add journal.

In reply to Benoît Canet

This commit add the code required to manage one or more journals in a qcow2 file. The primary user of this journal will be the qcow2-log-store.c.
The journal is asynchronous and will require it's users to issue flushs in order to make sure that entries had reached stable storage.

---
 block/Makefile.objs   |    1 +
 block/qcow2-journal.c |  587 +++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h         |   29 +++
 3 files changed, 617 insertions(+)
 create mode 100644 block/qcow2-journal.c
block/Makefile.objs +1 -0
index 5f0358a..ee894b5 100644
@@ -1,5 +1,6 @@
 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-journal.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 += vhdx.o
block/qcow2-journal.c new file +587 -0

587 lines not on this page · raw message

block/qcow2.h +29 -0
index 953edfe..adde631 100644
@@ -606,4 +606,33 @@ 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-journal.c functions */
+int qcow2_journal_resume(BlockDriverState *bs,
+                         QCowJournal *journal,
+                         uint64_t offset);
+uint64_t qcow2_journal_round_to_erase_blocks(uint64_t size);
+int qcow2_journal_recycle(BlockDriverState *bs, QCowJournal *journal);
+void qcow2_journal_init(BlockDriverState *bs,
+                        QCowJournal *journal);
+void qcow2_journal_cleanup(QCowJournal *journal);
+bool qcow2_journal_is_allocated(QCowJournal *journal);
+int qcow2_journal_disk_allocate(BlockDriverState *bs,
+                                QCowJournal *journal,
+                                uint64_t size);
+void qcow2_journal_disk_deallocate(BlockDriverState *bs,
+                                   QCowJournal *journal);
+int64_t qcow2_journal_append(BlockDriverState *bs, QCowJournal *journal,
+                             QCowJournalEntry *entry);
+int qcow2_journal_read(BlockDriverState *bs, QCowJournal *journal,
+                       uint64_t offset, QCowJournalEntry *entry);
+int qcow2_journal_flush(BlockDriverState *bs, QCowJournal *journal);
+int qcow2_journal_stop(BlockDriverState *bs, QCowJournal *journal);
+void qcow2_journal_entry_from_hash_info(QCowJournalEntry *entry,
+                                        QCowHashInfo *hash_info);
+int qcow2_hash_info_from_journal_entry(QCowHashInfo *hash_info,
+                                       QCowJournalEntry *entry);
+size_t qcow2_journal_dump_size(void);
+size_t qcow2_journal_dump(uint8_t *buf, QCowJournal *journal);
+void qcow2_journal_parse(QCowJournal *journal, uint8_t *buf);
+
 #endif
Signature · 1 line
1.7.10.4
#20 lore raw

RFC v8 4/24 qcow2: Create the log store.

In reply to Benoît Canet

The log store is an in RAM hash table used to index QCowJournalEntries. Combining the log store with a journal allows to get the benefits of the journal (fewer random write and minimization of SSD wear out) with the advantages of a hash table (being good at random access).

Once the journal is full and not packable or if the log store hash table is full the journal is frozen and converted into an on disk hash table used by the hash store.

---
 block/Makefile.objs     |    2 +-
 block/qcow2-log-store.c | 1039 +++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h           |   26 ++
 3 files changed, 1066 insertions(+), 1 deletion(-)
 create mode 100644 block/qcow2-log-store.c
block/Makefile.objs +1 -1
index ee894b5..8c0b646 100644
@@ -1,6 +1,6 @@
 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-journal.o
+block-obj-y += qcow2-journal.o qcow2-log-store.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 += vhdx.o
block/qcow2-log-store.c new file +1039 -0

1,039 lines not on this page · raw message

block/qcow2.h +26 -0
index adde631..d347471 100644
@@ -635,4 +635,30 @@ size_t qcow2_journal_dump_size(void);
 size_t qcow2_journal_dump(uint8_t *buf, QCowJournal *journal);
 void qcow2_journal_parse(QCowJournal *journal, uint8_t *buf);
 
+/* qcow2-log-store.c functions */
+
+uint64_t qcow2_dedup_h1(QCowHashInfo *hash_info, uint32_t order);
+uint64_t qcow2_dedup_h2(QCowHashInfo *hash_info, uint32_t order);
+uint64_t qcow2_log_store_nb_entries(uint32_t order);
+int qcow2_log_store_create(BlockDriverState *bs,
+                           QCowLogStore *store,
+                           uint32_t order);
+int qcow2_log_store_recycle(BlockDriverState *bs,
+                            QCowLogStore *store);
+void qcow2_log_store_init(BlockDriverState *bs,
+                          QCowLogStore *store,
+                          uint32_t order);
+void qcow2_log_store_cleanup(QCowLogStore *store);
+int qcow2_log_store_compute_order(uint64_t min_nb_entries_required);
+bool qcow2_log_store_get(QCowLogStore *store, QCowHashInfo *hash_info);
+int qcow2_log_store_insert(BlockDriverState *bs, QCowLogStore *store,
+                           QCowHashInfo *hash_info, bool *need_save);
+int qcow2_log_store_flush(BlockDriverState *bs, QCowLogStore *store);
+int qcow2_log_store_stop(BlockDriverState *bs, QCowLogStore *store);
+int qcow2_log_store_rebuild_from_journal(BlockDriverState *bs,
+                                         QCowLogStore *store);
+size_t qcow2_log_store_dump_size(void);
+size_t qcow2_log_store_dump(uint8_t *buf, QCowLogStore *store);
+size_t qcow2_log_store_parse(QCowLogStore *store, uint8_t *buf);
+
 #endif
Signature · 1 line
1.7.10.4
#23 lore raw

RFC v8 7/24 qcow2: Add qcow2_dedup_read_missing_and_concatenate

In reply to Benoît Canet

This function is used to read missing data when unaligned writes are done. This function also concatenate missing data with the given qiov data in order to prepare a buffer used to look for duplicated clusters.

---
 block/Makefile.objs |    2 +-
 block/qcow2-dedup.c |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c       |   35 +++++++++++++++
 block/qcow2.h       |   13 ++++++
 4 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100644 block/qcow2-dedup.c
block/Makefile.objs +1 -1
index 2d1a269..088407a 100644
@@ -1,6 +1,6 @@
 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-journal.o qcow2-log-store.o qcow2-hash-store.o qcow2-store.o
+block-obj-y += qcow2-journal.o qcow2-log-store.o qcow2-hash-store.o qcow2-store.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 += vhdx.o
block/qcow2-dedup.c new file +121 -0

121 lines not on this page · raw message

block/qcow2.c +35 -0
index 2e346d8..34b2a87 100644
@@ -1157,6 +1157,41 @@ fail:
     return ret;
 }
 
+/**
+ * Read some data from the QCOW2 file
+ *
+ * Important: s->lock is dropped. Things can change before the function returns
+ *            to the caller.
+ *
+ * @data:       the buffer where the data must be stored
+ * @sector_num: the sector number to read in the QCOW2 file
+ * @nb_sectors: the number of sectors to read
+ * @ret:        negative on error
+ */
+coroutine_fn int qcow2_read_cluster_data(BlockDriverState *bs,
+                            uint8_t *data,
+                            uint64_t sector_num,
+                            int nb_sectors)
+{
+    BDRVQcowState *s = bs->opaque;
+    QEMUIOVector qiov;
+    struct iovec iov;
+    int ret;
+
+    iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
+    iov.iov_base = data;
+    qemu_iovec_init_external(&qiov, &iov, 1);
+    qemu_co_mutex_unlock(&s->lock);
+    ret = qcow2_co_readv(bs, sector_num, nb_sectors, &qiov);
+    qemu_co_mutex_lock(&s->lock);
+    if (ret < 0) {
+        error_report("failed to read %d sectors at offset %" PRIu64 "\n",
+                     nb_sectors, sector_num);
+    }
+
+    return ret;
+}
+
 static int qcow2_change_backing_file(BlockDriverState *bs,
     const char *backing_file, const char *backing_fmt)
 {
block/qcow2.h +13 -0
index c0dbfe3..9a9abd3 100644
@@ -539,6 +539,10 @@ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
 
 int qcow2_mark_dirty(BlockDriverState *bs);
 int qcow2_update_header(BlockDriverState *bs);
+int qcow2_read_cluster_data(BlockDriverState *bs,
+                            uint8_t *data,
+                            uint64_t sector_num,
+                            int nb_sectors);
 
 /* qcow2-refcount.c functions */
 int qcow2_refcount_init(BlockDriverState *bs);
@@ -717,4 +721,13 @@ int qcow2_store_load(BlockDriverState *bs, QCowStore *store);
 int qcow2_store_start(BlockDriverState *bs, QCowStore *store);
 int qcow2_store_forget(BlockDriverState *bs, QCowStore *store);
 
+/* qcow2-dedup.c functions */
+int qcow2_dedup_read_missing_and_concatenate(BlockDriverState *bs,
+                                             QEMUIOVector *qiov,
+                                             uint64_t sector,
+                                             int sectors_nr,
+                                             uint8_t **dedup_cluster_data,
+                                             int *dedup_cluster_data_nr);
+
+
 #endif
Signature · 1 line
1.7.10.4
#24 lore raw

RFC v8 8/24 qcow2: Create a way to link to l2 tables when deduplicating.

In reply to Benoît Canet
---
 block/qcow2-cluster.c |    6 ++++--
 block/qcow2.h         |    6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +4 -2
index c71470a..d6db0b9 100644
@@ -696,7 +696,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
             old_cluster[j++] = l2_table[l2_index + i];
 
         l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
-                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
+                    (i << s->cluster_bits)) | m->l2_entry_flags);
      }
 
 
@@ -709,7 +709,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
      * If this was a COW, we need to decrease the refcount of the old cluster.
      * Also flush bs->file to get the right order for L2 and refcount update.
      */
-    if (j != 0) {
+    if (!m->overwrite && j != 0) {
         for (i = 0; i < j; i++) {
             qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1);
         }
@@ -1098,6 +1098,8 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
             .offset     = nb_sectors * BDRV_SECTOR_SIZE,
             .nb_sectors = avail_sectors - nb_sectors,
         },
+        .l2_entry_flags = QCOW_OFLAG_COPIED,
+        .overwrite      = false,
     };
     qemu_co_queue_init(&(*m)->dependent_requests);
     QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
block/qcow2.h +6 -0
index 9a9abd3..7754065 100644
@@ -438,6 +438,12 @@ typedef struct QCowL2Meta
      */
     CoQueue dependent_requests;
 
+    /* contains the flags to apply to the l2 entry */
+    uint64_t l2_entry_flags;
+
+    /* set to true if we are overwriting an L2 table entry */
+    bool overwrite;
+
     /**
      * The COW Region between the start of the first allocated cluster and the
      * area the guest actually writes to.
Signature · 1 line
1.7.10.4
#22 lore raw

RFC v8 6/24 qcow2: Add the deduplication store.

In reply to Benoît Canet

The key value store is an SSD optimised store used to store deduplication's QCowHashInfo.
It binds together the QCowJournal, the QCowLogStore and the QCowHashStore and must be called be the deduplication code.

---
 block/Makefile.objs |    2 +-
 block/qcow2-store.c |  771 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h       |   20 +-
 3 files changed, 791 insertions(+), 2 deletions(-)
 create mode 100644 block/qcow2-store.c
block/Makefile.objs +1 -1
index cafd4f8..2d1a269 100644
@@ -1,6 +1,6 @@
 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-journal.o qcow2-log-store.o qcow2-hash-store.o
+block-obj-y += qcow2-journal.o qcow2-log-store.o qcow2-hash-store.o qcow2-store.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 += vhdx.o
block/qcow2-store.c new file +771 -0

771 lines not on this page · raw message

block/qcow2.h +19 -1
index d0037bf..c0dbfe3 100644
@@ -226,9 +226,9 @@ typedef struct {
 
 typedef struct {
     uint32_t      order;
+    bool          freezing;          /* is a log store freeze in progress */
     QCowLogStore  log_store;         /* the current log store */
     QCowLogStore  frozen_log_store;  /* the log store to incarnate */
-    bool          freezing;          /* are we incarnating a log store */
     QCowHashStore hash_store;
     CoMutex       insert_lock;       /* used to prevent multiple freeze attempts
                                       * at the same time
@@ -699,4 +699,22 @@ size_t qcow2_hash_store_parse(QCowHashStore *store,
                                 uint8_t *buf,
                                 uint8_t *buf_end);
 
+/* qcow2-store.c functions (Should be called by dedup.c) */
+
+void qcow2_store_cleanup(QCowStore *store);
+int qcow2_store_get(BlockDriverState *bs, QCowStore *store,
+                    QCowHashInfo *hash_info);
+int qcow2_store_insert(BlockDriverState *bs, QCowStore *store,
+                       QCowHashInfo *hash_info);
+int qcow2_store_flush(BlockDriverState *bs, QCowStore *store);
+int qcow2_store_delete(BlockDriverState *bs, QCowStore *store,
+                       QCowHashInfo *hash_info);
+int64_t qcow2_store_save(BlockDriverState *bs, QCowStore *store);
+int64_t qcow2_store_create(BlockDriverState *bs,
+                           QCowStore *store,
+                           uint64_t total_size);
+int qcow2_store_load(BlockDriverState *bs, QCowStore *store);
+int qcow2_store_start(BlockDriverState *bs, QCowStore *store);
+int qcow2_store_forget(BlockDriverState *bs, QCowStore *store);
+
 #endif
Signature · 1 line
1.7.10.4
#21 lore raw

RFC v8 5/24 qcow2: Add the hash store.

In reply to Benoît Canet

The hash store allow to do lookup in the set of previously frozen log stores. Each frozen log store is called an incarnation. When the number of incarnation reach a user defined setting oldest incarnations are dropped to avoid that the dedup metadata grow without limit.

---
 block/Makefile.objs      |    2 +-
 block/qcow2-hash-store.c |  802 ++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2-log-store.c  |  242 ++++++++++++++
 block/qcow2.h            |   38 +++
 4 files changed, 1083 insertions(+), 1 deletion(-)
 create mode 100644 block/qcow2-hash-store.c
block/Makefile.objs +1 -1
index 8c0b646..cafd4f8 100644
@@ -1,6 +1,6 @@
 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-journal.o qcow2-log-store.o
+block-obj-y += qcow2-journal.o qcow2-log-store.o qcow2-hash-store.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 += vhdx.o
block/qcow2-hash-store.c new file +802 -0

802 lines not on this page · raw message

block/qcow2-log-store.c +242 -0

254 lines not on this page · raw message

block/qcow2.h +38 -0
index d347471..d0037bf 100644
@@ -79,6 +79,7 @@
                                         * on the log store size
                                         */
 #define QCOW2_NB_INCARNATION_GOAL  128 /* targeted number of incarnation */
+#define QCOW2_STORE_CO_SLEEP_NS    (100 * 1000 * 1000) /* 0.1 s in ns */
 
 #define QCOW_DEDUP_DIRTY 1 /* dirty flag in the qcow2 header extension */
 
@@ -657,8 +658,45 @@ int qcow2_log_store_flush(BlockDriverState *bs, QCowLogStore *store);
 int qcow2_log_store_stop(BlockDriverState *bs, QCowLogStore *store);
 int qcow2_log_store_rebuild_from_journal(BlockDriverState *bs,
                                          QCowLogStore *store);
+int qcow2_log_store_freeze_filter(BlockDriverState *bs,
+                                  QCowLogStore *store,
+                                  uint64_t offset,
+                                  uint8_t **filter);
+int qcow2_log_store_freeze_hash_table(BlockDriverState *bs,
+                                      QCowLogStore *store,
+                                      uint64_t offset);
 size_t qcow2_log_store_dump_size(void);
 size_t qcow2_log_store_dump(uint8_t *buf, QCowLogStore *store);
 size_t qcow2_log_store_parse(QCowLogStore *store, uint8_t *buf);
 
+/* qcow2-hash-store.c functions */
+
+int qcow2_hash_store_get_tag_size(uint32_t order);
+uint64_t qcow2_hash_store_nb_hash_info_per_cluster(void);
+uint64_t qcow2_hash_store_incarnation_size(uint32_t order);
+void qcow2_hash_store_reset(QCowHashStore *store,
+                            uint32_t order,
+                            uint32_t nb_incarnations,
+                            uint32_t nb_in_limbo);
+void qcow2_hash_store_init(QCowHashStore *store, uint32_t order);
+void qcow2_hash_store_cleanup(QCowHashStore *store);
+uint64_t qcow2_hash_store_filter_size(uint32_t order);
+void qcow2_hash_store_incarnation_free(QCowIncarnation *incarnation);
+int64_t qcow2_hash_store_get_incarnation_offset(BlockDriverState *bs,
+                                                QCowHashStore *store);
+void qcow2_hash_store_forget_all_incarnations(QCowHashStore *store);
+void qcow2_hash_store_add_incarnation(BlockDriverState *bs,
+                                      QCowHashStore *hash_store,
+                                      uint8_t *filter,
+                                      uint64_t offset);
+int qcow2_hash_store_get(BlockDriverState *bs, QCowHashStore *store,
+                         QCowHashInfo *hash_info);
+void qcow2_hash_store_load_filters(BlockDriverState *bs,
+                                   QCowHashStore *store);
+size_t qcow2_hash_store_dump_size(QCowHashStore *store);
+size_t qcow2_hash_store_dump(uint8_t *buf, QCowHashStore *store);
+size_t qcow2_hash_store_parse(QCowHashStore *store,
+                                uint8_t *buf,
+                                uint8_t *buf_end);
+
 #endif
Signature · 1 line
1.7.10.4
#25 lore raw

RFC v8 9/24 qcow2: Make qcow2_update_cluster_refcount public.

In reply to Benoît Canet
---
 block/qcow2-refcount.c |   17 ++++++++++-------
 block/qcow2.h          |    3 +++
 2 files changed, 13 insertions(+), 7 deletions(-)
block/qcow2-refcount.c +10 -7
index b32738f..3bd8f37 100644
@@ -520,9 +520,9 @@ fail:
  * If the return value is non-negative, it is the new refcount of the cluster.
  * If it is negative, it is -errno and indicates an error.
  */
-static int update_cluster_refcount(BlockDriverState *bs,
-                                   int64_t cluster_index,
-                                   int addend)
+int qcow2_update_cluster_refcount(BlockDriverState *bs,
+                                  int64_t cluster_index,
+                                  int addend)
 {
     BDRVQcowState *s = bs->opaque;
     int ret;
@@ -649,7 +649,7 @@ int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
         if (free_in_cluster == 0)
             s->free_byte_offset = 0;
         if ((offset & (s->cluster_size - 1)) != 0)
-            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
+            qcow2_update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
     } else {
         offset = qcow2_alloc_clusters(bs, s->cluster_size);
         if (offset < 0) {
@@ -659,7 +659,7 @@ int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
         if ((cluster_offset + s->cluster_size) == offset) {
             /* we are lucky: contiguous data */
             offset = s->free_byte_offset;
-            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
+            qcow2_update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
             s->free_byte_offset += size;
         } else {
             s->free_byte_offset = offset;
@@ -795,7 +795,9 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                     } else {
                         uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                         if (addend != 0) {
-                            refcount = update_cluster_refcount(bs, cluster_index, addend);
+                            refcount = qcow2_update_cluster_refcount(bs,
+                                           cluster_index,
+                                           addend);
                         } else {
                             refcount = get_refcount(bs, cluster_index);
                         }
@@ -827,7 +829,8 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
 
 
             if (addend != 0) {
-                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
+                refcount = qcow2_update_cluster_refcount(bs,
+                               l2_offset >> s->cluster_bits, addend);
             } else {
                 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
             }
block/qcow2.h +3 -0
index 7754065..3238083 100644
@@ -568,6 +568,9 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
 
 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                           BdrvCheckMode fix);
+int qcow2_update_cluster_refcount(BlockDriverState *bs,
+                                  int64_t cluster_index,
+                                  int addend);
 
 /* qcow2-cluster.c functions */
 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
Signature · 1 line
1.7.10.4
#28 lore raw

RFC v8 12/24 qcow2: Do allocate on rewrite on the dedup case.

In reply to Benoît Canet

This patch does allocate on rewrite when deduplication is on. This get rid of the need of removing the old hash of the lookup structure when a cluster get rewritten.
The old data is left in place and will be collected/deleted when it's cluster will reach 0.

---
 block/qcow2-cluster.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +3 -2
index d6db0b9..41c4bc2 100644
@@ -878,7 +878,8 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
     cluster_offset = be64_to_cpu(l2_table[l2_index]);
 
     /* Check how many clusters are already allocated and don't need COW */
-    if (qcow2_get_cluster_type(cluster_offset) == QCOW2_CLUSTER_NORMAL
+    if (!s->has_dedup &&
+        qcow2_get_cluster_type(cluster_offset) == QCOW2_CLUSTER_NORMAL
         && (cluster_offset & QCOW_OFLAG_COPIED))
     {
         /* If a specific host_offset is required, check it */
@@ -1028,7 +1029,7 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
     /* For the moment, overwrite compressed clusters one by one */
     if (entry & QCOW_OFLAG_COMPRESSED) {
         nb_clusters = 1;
-    } else {
+    } else if (!s->has_dedup) {
         nb_clusters = count_cow_clusters(s, nb_clusters, l2_table, l2_index);
     }
 
Signature · 1 line
1.7.10.4
#26 lore raw

RFC v8 10/24 qcow2: Add qcow2_dedup and related functions

In reply to Benoît Canet
---
 block/qcow2-dedup.c |  415 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h       |    5 +
 2 files changed, 420 insertions(+)
block/qcow2-dedup.c +415 -0
index bc6e2c2..0daf77e 100644
@@ -119,3 +119,418 @@ fail:
     *data = NULL;
     return ret;
 }
+
+/*
+ * Set a QCowHashInfo into the new state
+ *
+ * @hash_info: the hash_info to set to new
+ */
+static void qcow2_set_hash_info_new(QCowHashInfo *hash_info)
+{
+    hash_info->physical_sect = QCOW_DEDUP_FLAG_EMPTY;
+    hash_info->first_logical_sect = QCOW_DEDUP_FLAG_EMPTY;
+}
+
+/*
+ * Compute the hash of a given cluster
+ *
+ * @data: a buffer containing the cluster data
+ * @hash: a QCowHash where to store the computed hash
+ * @ret:  0 on success, negative on error
+ */
+static int qcow2_compute_cluster_hash(BlockDriverState *bs,
+                                       QCowHash *hash,
+                                       uint8_t *data)
+{
+    return 0;
+}
+
+/*
+ * Get a QCowHashInfo corresponding to a cluster data
+ *
+ * @phash:           if phash can be used no hash is computed
+ * @data:            a buffer containing the cluster
+ * @ret:             1 if if hash found, 0 if not found, -errno on error
+ */
+static int qcow2_get_persistent_hash_for_cluster(BlockDriverState *bs,
+                                                 QcowPersistentHash *phash,
+                                                 uint8_t *data)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+
+    /* no hash has been provided compute it and store it for later usage */
+    if (!phash->reuse) {
+        ret = qcow2_compute_cluster_hash(bs,
+                                         &phash->hash_info.hash,
+                                         data);
+    }
+
+    /* do not reuse the hash anymore if it was precomputed */
+    phash->reuse = false;
+
+    /* error */
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* ask the store if it knows this QCowHashInfo */
+    return qcow2_store_get(bs, &s->key_value_store, &phash->hash_info);
+}
+
+/*
+ * Insert a QCowHashInfo into the store as new
+ *
+ * @hash_info: the QCowHashInfo to set to new and insert
+ * @ret:       0 on succes, -errno on error
+ */
+static int qcow2_add_new_hash_info_to_store(BlockDriverState *bs,
+                                            QCowHashInfo *hash_info)
+{
+    BDRVQcowState *s = bs->opaque;
+
+    /* set the QCowHashInfo to the new state so we will remember to fill these
+     * field later with real values
+     */
+    qcow2_set_hash_info_new(hash_info);
+    return qcow2_store_insert(bs, &s->key_value_store, hash_info);
+}
+
+/*
+ * Helper used to build a QCowHashElement
+ *
+ * @hash: the QCowHash to use
+ * @ret:  a newly allocated QCowHashElement containing the given hash
+ */
+static QCowHashElement *qcow2_dedup_hash_new(QCowHash *hash)
+{
+    QCowHashElement *dedup_hash;
+    dedup_hash = g_new0(QCowHashElement, 1);
+    memcpy(dedup_hash->hash_info.hash.data, hash->data, HASH_LENGTH);
+    return dedup_hash;
+}
+
+/*
+ * Helper used to link a deduplicated cluster in the l2
+ *
+ * @logical_sect:  the cluster sector seen by the guest
+ * @physical_sect: the cluster sector in the QCOW2 file
+ * @overwrite:     true if we must overwrite the L2 table entry
+ * @ret:
+ */
+static int qcow2_dedup_link_l2(BlockDriverState *bs,
+                               uint64_t logical_sect,
+                               uint64_t physical_sect,
+                               bool overwrite)
+{
+    QCowL2Meta m = {
+        .alloc_offset   = physical_sect << 9,
+        .offset         = logical_sect << 9,
+        .nb_clusters    = 1,
+        .nb_available   = 0,
+        .cow_start = {
+            .offset     = 0,
+            .nb_sectors = 0,
+        },
+        .cow_end = {
+            .offset     = 0,
+            .nb_sectors = 0,
+        },
+        .l2_entry_flags = 0,
+        .overwrite      = overwrite,
+    };
+    return qcow2_alloc_cluster_link_l2(bs, &m);
+}
+
+/* Clear the QCOW_OFLAG_COPIED from the first L2 entry written for a physical
+ * cluster.
+ *
+ * @hash_info: the duplicated hash info
+ * @ret:       0 on success, negative on error
+ */
+static int qcow2_clear_l2_copied_flag_if_needed(BlockDriverState *bs,
+                                                QCowHashInfo *hash_info)
+{
+    int ret = 0;
+    uint64_t first_logical_sect = hash_info->first_logical_sect;
+
+    /* QCOW_OFLAG_COPIED already cleared -> do nothing */
+    if (!(first_logical_sect & QCOW_OFLAG_COPIED)) {
+        return 0;
+    }
+
+    first_logical_sect &= ~QCOW_OFLAG_COPIED;
+
+    /* overwrite first L2 entry to clear QCOW_FLAG_COPIED */
+    ret = qcow2_dedup_link_l2(bs, first_logical_sect,
+                              hash_info->physical_sect,
+                              true);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* remember that we don't need to clear QCOW_OFLAG_COPIED again */
+    hash_info->first_logical_sect = first_logical_sect;
+
+    return 0;
+}
+
+/* This function deduplicate a cluster
+ *
+ * @logical_sect: The logical sector of the write
+ * @hash_info:    The duplicated cluster hash info
+ * @ret:          0 on success, negative on error
+ */
+static int qcow2_deduplicate_cluster(BlockDriverState *bs,
+                                     uint64_t logical_sect,
+                                     QCowHashInfo *hash_info)
+{
+    BDRVQcowState *s = bs->opaque;
+    uint64_t cluster_index = hash_info->physical_sect / s->cluster_sectors;
+    int ret = 0;
+
+    /* Increment the refcount of the cluster */
+    ret = qcow2_update_cluster_refcount(bs,
+                                        cluster_index,
+                                        1);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* create new L2 entry */
+    return qcow2_dedup_link_l2(bs, logical_sect,
+                               hash_info->physical_sect,
+                               false);
+}
+
+/* This function tries to deduplicate a given cluster.
+ *
+ * @sector_num:           the logical sector number we are trying to deduplicate
+ * @phash:                Used instead of computing the hash if provided
+ * @data:                 the buffer in which to look for a duplicated cluster
+ * @ret:                  ret < 0 on error, 1 on deduplication else 0
+ */
+static int qcow2_try_dedup_cluster(BlockDriverState *bs,
+                                   QcowPersistentHash *phash,
+                                   uint64_t sector_num,
+                                   uint8_t *data)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+    uint64_t logical_sect;
+    uint64_t existing_physical_offset;
+    int pnum = s->cluster_sectors;
+
+    /* search the tree for duplicated cluster */
+    ret = qcow2_get_persistent_hash_for_cluster(bs,
+                                                phash,
+                                                data);
+
+    /* we won't reuse the hash on error */
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* if cluster is not duplicated store hash for later usage */
+    if (!ret) {
+        return qcow2_add_new_hash_info_to_store(bs, &phash->hash_info);
+    }
+
+    logical_sect = sector_num & ~(s->cluster_sectors - 1);
+    ret = qcow2_get_cluster_offset(bs, logical_sect << 9,
+                                   &pnum, &existing_physical_offset);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* if we are rewriting the same cluster at the same place do nothing */
+    if (existing_physical_offset == phash->hash_info.physical_sect << 9) {
+        return 1;
+    }
+
+    /* take care of not having refcount > 1 and QCOW_OFLAG_COPIED at once */
+    ret = qcow2_clear_l2_copied_flag_if_needed(bs, &phash->hash_info);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* do the deduplication */
+    ret = qcow2_deduplicate_cluster(bs, logical_sect, &phash->hash_info);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    return 1;
+}
+
+
+static void add_hash_to_undedupable_list(BlockDriverState *bs,
+                                         QCowDedupState *ds)
+{
+    /* memorize hash for later storage in key value store and and disk */
+    QCowHashElement *dedup_hash =
+        qcow2_dedup_hash_new(&ds->phash.hash_info.hash);
+    QTAILQ_INSERT_TAIL(&ds->undedupables, dedup_hash, next);
+}
+
+static int qcow2_dedup_starting_from_begining(BlockDriverState *bs,
+                                              QCowDedupState *ds,
+                                              uint64_t sector_num,
+                                              uint8_t *data,
+                                              int left_to_process)
+{
+    BDRVQcowState *s = bs->opaque;
+    int i;
+    int ret = 0;
+
+    for (i = 0; i < left_to_process; i++) {
+        ret = qcow2_try_dedup_cluster(bs,
+                                      &ds->phash,
+                                      sector_num + i * s->cluster_sectors,
+                                      data + i * s->cluster_size);
+
+        if (ret < 0) {
+            return ret;
+        }
+
+        /* stop if a cluster has not been deduplicated */
+        if (ret != 1) {
+            break;
+        }
+    }
+
+    return i;
+}
+
+static int qcow2_count_next_non_dedupable_clusters(BlockDriverState *bs,
+                                                   QCowDedupState *ds,
+                                                   uint8_t *data,
+                                                   int left_to_process)
+{
+    BDRVQcowState *s = bs->opaque;
+    int i;
+    int ret = 0;
+
+    for (i = 0; i < left_to_process; i++) {
+        ret = qcow2_get_persistent_hash_for_cluster(bs,
+                                                    &ds->phash,
+                                                    data + i * s->cluster_size);
+
+        if (ret < 0) {
+            return ret;
+        }
+
+        /* found a duplicated cluster : stop here */
+        if (ret) {
+            break;
+        }
+
+        ret = qcow2_add_new_hash_info_to_store(bs, &ds->phash.hash_info);
+
+        if (ret < 0) {
+            return ret;
+        }
+        add_hash_to_undedupable_list(bs, ds);
+    }
+
+    return i;
+}
+
+
+/* Deduplicate all the cluster that can be deduplicated.
+ *
+ * Next it computes the number of non deduplicable sectors to come while storing
+ * the hashes of these sectors in a linked list for later usage.
+ * Then it computes the first duplicated cluster hash that comes after non
+ * deduplicable cluster, this hash will be used at next call of the function
+ *
+ * @ds:              a structure containing the state of the deduplication
+ *                   for this write request
+ * @sector_num:      The logical sector
+ * @data:            the buffer containing the data to deduplicate
+ * @data_nr:         the size of the buffer in sectors
+ *
+ */
+int qcow2_dedup(BlockDriverState *bs,
+                QCowDedupState *ds,
+                uint64_t sector_num,
+                uint8_t *data,
+                int data_nr)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+    int deduped_clusters_nr = 0;
+    int left_to_process;
+    int start_index;
+
+    start_index = sector_num & (s->cluster_sectors - 1);
+
+    left_to_process = (data_nr / s->cluster_sectors) -
+                      ds->nb_clusters_processed;
+
+    data += ds->nb_clusters_processed * s->cluster_size;
+
+    /* start deduplicating all that can be cluster after cluster */
+    ret = qcow2_dedup_starting_from_begining(bs,
+                                             ds,
+                                             sector_num,
+                                             data,
+                                             left_to_process);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    deduped_clusters_nr = ret;
+
+    left_to_process -= ret;
+    ds->nb_clusters_processed += ret;
+    data += ret * s->cluster_size;
+
+    /* We deduped everything till the end */
+    if (!left_to_process) {
+        ds->nb_undedupable_sectors = 0;
+        goto exit;
+    }
+
+    /* skip and account the first undedupable cluster found */
+    left_to_process--;
+    ds->nb_clusters_processed++;
+    data += s->cluster_size;
+    ds->nb_undedupable_sectors += s->cluster_sectors;
+
+    add_hash_to_undedupable_list(bs, ds);
+
+    /* Count how many non duplicated sector can be written and memorize hashes
+     * to write them after data has reached disk.
+     */
+    ret = qcow2_count_next_non_dedupable_clusters(bs,
+                                                  ds,
+                                                  data,
+                                                  left_to_process);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    left_to_process -= ret;
+    ds->nb_clusters_processed += ret;
+    ds->nb_undedupable_sectors += ret * s->cluster_sectors;
+
+    /* remember to reuse the last hash computed at new qcow2_dedup call */
+    if (left_to_process) {
+        ds->phash.reuse = true;
+    }
+
+exit:
+    if (!deduped_clusters_nr) {
+        return 0;
+    }
+
+    return deduped_clusters_nr * s->cluster_sectors - start_index;
+}
block/qcow2.h +5 -0
index 3238083..3346842 100644
@@ -737,6 +737,11 @@ int qcow2_dedup_read_missing_and_concatenate(BlockDriverState *bs,
                                              int sectors_nr,
                                              uint8_t **dedup_cluster_data,
                                              int *dedup_cluster_data_nr);
+int qcow2_dedup(BlockDriverState *bs,
+                QCowDedupState *ds,
+                uint64_t sector_num,
+                uint8_t *data,
+                int data_nr);
 
 
 #endif
Signature · 1 line
1.7.10.4
#27 lore raw

RFC v8 11/24 qcow2: Add qcow2_dedup_store_new_hashes.

In reply to Benoît Canet
---
 block/qcow2-dedup.c |   97 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 block/qcow2.h       |    6 +++-
 2 files changed, 101 insertions(+), 2 deletions(-)
block/qcow2-dedup.c +96 -1
index 0daf77e..ffbf866 100644
@@ -251,6 +251,7 @@ static int qcow2_dedup_link_l2(BlockDriverState *bs,
 static int qcow2_clear_l2_copied_flag_if_needed(BlockDriverState *bs,
                                                 QCowHashInfo *hash_info)
 {
+    BDRVQcowState *s = bs->opaque;
     int ret = 0;
     uint64_t first_logical_sect = hash_info->first_logical_sect;
 
@@ -273,7 +274,8 @@ static int qcow2_clear_l2_copied_flag_if_needed(BlockDriverState *bs,
     /* remember that we don't need to clear QCOW_OFLAG_COPIED again */
     hash_info->first_logical_sect = first_logical_sect;
 
-    return 0;
+    /* clear the QCOW_OFLAG_COPIED flag from disk */
+    return qcow2_store_insert(bs, &s->key_value_store, hash_info);
 }
 
 /* This function deduplicate a cluster
@@ -534,3 +536,96 @@ exit:
 
     return deduped_clusters_nr * s->cluster_sectors - start_index;
 }
+
+static inline bool is_hash_info_empty(QCowHashInfo *hash_info)
+{
+    return hash_info->physical_sect & QCOW_DEDUP_FLAG_EMPTY;
+}
+
+/* This function store a hash information to disk and RAM
+ *
+ * @hash_info:      the QCowHashInfo to process
+ * @logical_sect:   the logical sector of the cluster seen by the guest
+ * @physical_sect:  the physical sector of the stored cluster
+ * @ret:            0 on success, negative on error
+ */
+static int qcow2_store_hash(BlockDriverState *bs,
+                            QCowHashInfo *hash_info,
+                            uint64_t logical_sect,
+                            uint64_t physical_sect)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+
+    ret = qcow2_store_get(bs, &s->key_value_store, hash_info);
+
+    /* no hash info found in store or error */
+    if (ret <= 0) {
+        return ret;
+    }
+
+    /* the hash info information are already completed */
+    if (!is_hash_info_empty(hash_info)) {
+        return 0;
+    }
+
+    /* Remember that this QCowHashInfo represents the first occurrence of the
+     * cluster so we will be able to clear QCOW_OFLAG_COPIED from the L2 table
+     * entry when refcount will go > 1.
+     */
+    logical_sect = logical_sect | QCOW_OFLAG_COPIED;
+
+    /* fill the missing fields of the hash info */
+    hash_info->physical_sect = physical_sect;
+    hash_info->first_logical_sect = logical_sect;
+
+    /* write the hash into the key value store */
+    return qcow2_store_insert(bs, &s->key_value_store, hash_info);
+}
+
+/* This function store the hashes of the clusters which are not duplicated
+ *
+ * @ds:            The deduplication state
+ * @count:         the number of dedup hash to process
+ * @logical_sect:  logical offset of the first cluster (in sectors)
+ * @physical_sect: offset of the first cluster (in sectors)
+ * @ret:           0 on succes, errno on error
+ */
+int qcow2_dedup_store_new_hashes(BlockDriverState *bs,
+                                 QCowDedupState *ds,
+                                 int count,
+                                 uint64_t logical_sect,
+                                 uint64_t physical_sect)
+{
+    int ret = 0;
+    int i = 0;
+    BDRVQcowState *s = bs->opaque;
+    QCowHashElement *dedup_hash, *next_dedup_hash;
+
+    /* round values on cluster boundaries for easier cluster deletion */
+    logical_sect = logical_sect & ~(s->cluster_sectors - 1);
+    physical_sect = physical_sect & ~(s->cluster_sectors - 1);
+
+    QTAILQ_FOREACH_SAFE(dedup_hash, &ds->undedupables, next, next_dedup_hash) {
+
+        ret = qcow2_store_hash(bs,
+                               &dedup_hash->hash_info,
+                               logical_sect + i * s->cluster_sectors,
+                               physical_sect + i * s->cluster_sectors);
+
+        QTAILQ_REMOVE(&ds->undedupables, dedup_hash, next);
+        g_free(dedup_hash);
+
+        if (ret < 0) {
+            break;
+        }
+
+        i++;
+
+        if (i == count) {
+            break;
+        }
+    }
+
+    return ret;
+}
block/qcow2.h +5 -1
index 3346842..ff26e81 100644
@@ -742,6 +742,10 @@ int qcow2_dedup(BlockDriverState *bs,
                 uint64_t sector_num,
                 uint8_t *data,
                 int data_nr);
-
+int qcow2_dedup_store_new_hashes(BlockDriverState *bs,
+                                 QCowDedupState *ds,
+                                 int count,
+                                 uint64_t logical_sect,
+                                 uint64_t physical_sect);
 
 #endif
Signature · 1 line
1.7.10.4
#30 lore raw

RFC v8 14/24 qcow2: Load and save deduplication table header extension.

In reply to Benoît Canet
---
 block/qcow2.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
block/qcow2.c +53 -0
index 34b2a87..3cd1051 100644
@@ -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);
Signature · 1 line
1.7.10.4
#29 lore raw

RFC v8 13/24 qcow2: Implement qcow2_compute_cluster_hash.

In reply to Benoît Canet

Also factorize detection of libgnutls with vnc tls.

---
 block/qcow2-dedup.c |   17 +++++++++-
 configure           |   86 +++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 79 insertions(+), 24 deletions(-)
block/qcow2-dedup.c +16 -1
index ffbf866..2d2c15c 100644
@@ -28,6 +28,10 @@
 #include "block/block_int.h"
 #include "qemu-common.h"
 #include "qcow2.h"
+#ifdef CONFIG_SHA256_DEDUP
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
+#endif
 
 /*
  * Prepare a buffer containing everything required to compute cluster
@@ -142,7 +146,18 @@ static int qcow2_compute_cluster_hash(BlockDriverState *bs,
                                        QCowHash *hash,
                                        uint8_t *data)
 {
-    return 0;
+    BDRVQcowState *s = bs->opaque;
+    switch (s->dedup_hash_algo) {
+#ifdef CONFIG_SHA256_DEDUP
+    case QCOW_HASH_SHA256:
+        return gnutls_hash_fast(GNUTLS_DIG_SHA256, data,
+                                s->cluster_size, hash->data);
+#endif
+    default:
+        error_report("Invalid deduplication hash algorithm %i",
+                     s->dedup_hash_algo);
+        abort();
+    }
 }
 
 /*
configure +63 -23

125 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#33 lore raw

RFC v8 17/24 qcow2: Drop hash for a given cluster when dedup makes refcount > 2^16/2.

In reply to Benoît Canet

A new physical cluster with the same hash value will be used for further occurrence of this hash.

---
 block/qcow2-dedup.c |   20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
block/qcow2-dedup.c +16 -4
index 2d2c15c..da4ad5c 100644
@@ -305,12 +305,24 @@ static int qcow2_deduplicate_cluster(BlockDriverState *bs,
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t cluster_index = hash_info->physical_sect / s->cluster_sectors;
-    int ret = 0;
+    int refcount, ret = 0;
 
     /* Increment the refcount of the cluster */
-    ret = qcow2_update_cluster_refcount(bs,
-                                        cluster_index,
-                                        1);
+    refcount = qcow2_update_cluster_refcount(bs,
+                                             cluster_index,
+                                             1);
+
+    if (refcount < 0) {
+        return refcount;
+    }
+
+    /* if we reached half the max refcount delete the QCowHashInfo from the
+     * store so the next time this cluster will be seen it will be handled as
+     * new
+     */
+    if (refcount >= 0xFFFF/2) {
+        ret = qcow2_store_delete(bs, &s->key_value_store, hash_info);
+    }
 
     if (ret < 0) {
         return ret;
Signature · 1 line
1.7.10.4
#31 lore raw

RFC v8 15/24 qcow2: Extract qcow2_set_incompat_feature and qcow2_clear_incompat_feature.

In reply to Benoît Canet

Also change callers.

---
 block/qcow2-cluster.c |    2 +-
 block/qcow2.c         |   43 ++++++++++++++++++++++---------------------
 block/qcow2.h         |    7 ++++---
 3 files changed, 27 insertions(+), 25 deletions(-)
block/qcow2-cluster.c +1 -1
index 41c4bc2..7cd6e75 100644
@@ -672,7 +672,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
 
     /* Update L2 table. */
     if (s->use_lazy_refcounts) {
-        qcow2_mark_dirty(bs);
+        qcow2_set_incompat_feature(bs, QCOW2_INCOMPAT_DIRTY);
     }
     if (qcow2_need_accurate_refcounts(s)) {
         qcow2_cache_set_dependency(bs, s->l2_table_cache,
block/qcow2.c +22 -21

92 lines not on this page · raw message

block/qcow2.h +4 -3
index ff26e81..720131d 100644
@@ -290,14 +290,14 @@ enum {
 };
 
 /* Incompatible feature bits */
-enum {
+typedef 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_DEDUP,
-};
+} QCow2IncompatibleFeature;
 
 /* Compatible feature bits */
 enum {
@@ -543,7 +543,8 @@ static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
                   int64_t sector_num, int nb_sectors);
 
-int qcow2_mark_dirty(BlockDriverState *bs);
+int qcow2_set_incompat_feature(BlockDriverState *bs,
+                               QCow2IncompatibleFeature feature);
 int qcow2_update_header(BlockDriverState *bs);
 int qcow2_read_cluster_data(BlockDriverState *bs,
                             uint8_t *data,
Signature · 1 line
1.7.10.4
#34 lore raw

RFC v8 18/24 qcow2: Remove hash when cluster is deleted.

In reply to Benoît Canet
---
 block/qcow2-dedup.c    |   45 +++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2-refcount.c |    3 +++
 block/qcow2.h          |    2 ++
 3 files changed, 50 insertions(+)
block/qcow2-dedup.c +45 -0
index da4ad5c..599cb2e 100644
@@ -656,3 +656,48 @@ int qcow2_dedup_store_new_hashes(BlockDriverState *bs,
 
     return ret;
 }
+
+/* Clean the last reference to a given cluster when its refcount is zero
+ *
+ * @cluster_index: the index of the physical cluster
+ */
+void qcow2_dedup_destroy_hash(BlockDriverState *bs,
+                              uint64_t cluster_index)
+{
+    BDRVQcowState *s = bs->opaque;
+    uint64_t offset = cluster_index * s->cluster_size;
+    QCowHashInfo hash_info;
+    uint8_t *buf;
+    int ret = 0;
+
+    /* allocate buffer */
+    buf = qemu_blockalign(bs, s->cluster_size);
+
+    /* read cluster from disk */
+    ret = bdrv_pread(bs->file, offset, buf, s->cluster_size);
+
+    /* error */
+    if (ret < 0) {
+        goto free_exit;
+    }
+
+    /* clear hash info */
+    memset(&hash_info, 0, sizeof(QCowHashInfo));
+
+    /* compute hash for the cluster */
+    ret = qcow2_compute_cluster_hash(bs,
+                                     &hash_info.hash,
+                                     buf);
+
+
+    /* error */
+    if (ret < 0) {
+        goto free_exit;
+    }
+
+    /* delete hash from key value store. It will not be deduplicated anymore */
+    qcow2_store_delete(bs, &s->key_value_store, &hash_info);
+
+free_exit:
+   qemu_vfree(buf);
+}
block/qcow2-refcount.c +3 -0
index 3bd8f37..2734cd9 100644
@@ -482,6 +482,9 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
             ret = -EINVAL;
             goto fail;
         }
+        if (s->has_dedup && refcount == 0) {
+            qcow2_dedup_destroy_hash(bs, cluster_index);
+        }
         if (refcount == 0 && cluster_index < s->free_cluster_index) {
             s->free_cluster_index = cluster_index;
         }
block/qcow2.h +2 -0
index 720131d..6f85e03 100644
@@ -748,5 +748,7 @@ int qcow2_dedup_store_new_hashes(BlockDriverState *bs,
                                  int count,
                                  uint64_t logical_sect,
                                  uint64_t physical_sect);
+void qcow2_dedup_destroy_hash(BlockDriverState *bs,
+                              uint64_t cluster_index);
 
 #endif
Signature · 1 line
1.7.10.4
#36 lore raw

RFC v8 20/24 qcow2: Serialize write requests when deduplication is activated.

In reply to Benoît Canet

This fixes the sub cluster sized writes race conditions while waiting for a faster solution.

---
 block/qcow2.c |   14 ++++++++++++++
 block/qcow2.h |    1 +
 2 files changed, 15 insertions(+)
block/qcow2.c +14 -0
index 8eb63f1..11c115f 100644
@@ -534,6 +534,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
 
     /* Initialise locks */
     qemu_co_mutex_init(&s->lock);
+    qemu_co_mutex_init(&s->dedup_lock);
 
     /* Repair image if dirty */
     if (!(flags & BDRV_O_CHECK) && !bs->read_only &&
@@ -841,6 +842,15 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
 
     s->cluster_cache_offset = -1; /* disable compressed cache */
 
+    if (s->has_dedup) {
+        /* This mutex is used to serialize the write requests in the dedup case.
+         * The goal is to avoid that the dedup process concurrents requests to
+         * the same clusters and corrupt data.
+         * With qcow2_dedup_read_missing_and_concatenate that would not work.
+         */
+        qemu_co_mutex_lock(&s->dedup_lock);
+    }
+
     qemu_co_mutex_lock(&s->lock);
 
     if (s->has_dedup) {
@@ -1018,6 +1028,10 @@ fail:
         l2meta = next;
     }
 
+    if (s->has_dedup) {
+        qemu_co_mutex_unlock(&s->dedup_lock);
+    }
+
     qemu_iovec_destroy(&hd_qiov);
     qemu_vfree(cluster_data);
     qemu_vfree(dedup_cluster_data);
block/qcow2.h +1 -0
index 6f85e03..3c6e685 100644
@@ -364,6 +364,7 @@ typedef struct BDRVQcowState {
     Coroutine *load_filter_co;          /* used to load incarnations filters */
 
     CoMutex lock;
+    CoMutex dedup_lock;
 
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
     uint32_t crypt_method_header;
Signature · 1 line
1.7.10.4
#32 lore raw

RFC v8 16/24 block: Add qcow2_dedup format and image creation code.

In reply to Benoît Canet

Also modify qemu-io-test.

---
 block/qcow2.c                |  176 ++++++++++++++++++++++++++++++++++++++++--
 include/block/block_int.h    |    1 +
 tests/qemu-iotests/common.rc |    3 +-
 3 files changed, 173 insertions(+), 7 deletions(-)
block/qcow2.c +170 -6
index 58e6236..e1265a2 100644
@@ -1313,7 +1313,8 @@ static int preallocate(BlockDriverState *bs)
 static int qcow2_create2(const char *filename, int64_t total_size,
                          const char *backing_file, const char *backing_format,
                          int flags, size_t cluster_size, int prealloc,
-                         QEMUOptionParameter *options, int version)
+                         QEMUOptionParameter *options, int version,
+                         bool dedup, uint8_t hash_algo)
 {
     /* Calculate cluster_bits */
     int cluster_bits;
@@ -1417,11 +1418,39 @@ static int qcow2_create2(const char *filename, int64_t total_size,
     }
 
     /* Okay, now that we have a valid image, let's give it the right size */
+    BDRVQcowState *s = bs->opaque;
     ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
     if (ret < 0) {
         goto out;
     }
 
+    if (dedup) {
+        s->has_dedup = true;
+        /* fill cluster size for qcow2_store_create */
+        s->cluster_size = cluster_size;
+        /* unlimited incarnation number */
+        s->dedup_max_incarnations = 0;
+        s->dedup_hash_algo = hash_algo;
+
+        ret = qcow2_set_incompat_feature(bs, QCOW2_INCOMPAT_DEDUP);
+        if (ret < 0) {
+            goto out;
+        }
+
+        /* this will create the journal and save the conf to disk then
+         * save header extension
+         */
+        ret = qcow2_store_create(bs,
+                                 &s->key_value_store,
+                                 total_size * BDRV_SECTOR_SIZE);
+
+        if (ret < 0) {
+            goto out;
+        }
+
+        qcow2_store_cleanup(bs, &s->key_value_store);
+    }
+
     /* Want a backing file? There you go.*/
     if (backing_file) {
         ret = bdrv_change_backing_file(bs, backing_file, backing_format);
@@ -1447,15 +1476,41 @@ out:
     return ret;
 }
 
+static int qcow2_warn_if_version_3_is_needed(int version,
+                                             bool has_feature,
+                                             const char *feature)
+{
+    if (version < 3 && has_feature) {
+        fprintf(stderr, "%s only supported with compatibility "
+                "level 1.1 and above (use compat=1.1 or greater)\n",
+                feature);
+        return -EINVAL;
+    }
+    return 0;
+}
+
+static int8_t qcow2_get_dedup_hash_algo(char *value)
+{
+    if (!value || !strcmp(value, "sha256")) {
+        return QCOW_HASH_SHA256;
+    }
+
+    error_printf("Unsupported deduplication hash algorithm.\n");
+    return -EINVAL;
+}
+
 static int qcow2_create(const char *filename, QEMUOptionParameter *options)
 {
     const char *backing_file = NULL;
     const char *backing_fmt = NULL;
     uint64_t sectors = 0;
     int flags = 0;
+    int ret;
     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
     int prealloc = 0;
     int version = 2;
+    bool dedup = false;
+    int8_t hash_algo = 0;
 
     /* Read out options */
     while (options && options->name) {
@@ -1493,6 +1548,13 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options)
             }
         } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
             flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
+        } else if (!strcmp(options->name, BLOCK_OPT_DEDUP)) {
+            hash_algo = qcow2_get_dedup_hash_algo(options->value.s);
+            if (hash_algo < 0) {
+                return hash_algo;
+            }
+            dedup = true;
+            version = 3;
         }
         options++;
     }
@@ -1503,14 +1565,22 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options)
         return -EINVAL;
     }
 
-    if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
-        fprintf(stderr, "Lazy refcounts only supported with compatibility "
-                "level 1.1 and above (use compat=1.1 or greater)\n");
-        return -EINVAL;
+    ret = qcow2_warn_if_version_3_is_needed(version,
+                                            flags & BLOCK_FLAG_LAZY_REFCOUNTS,
+                                            "Lazy refcounts");
+    if (ret < 0) {
+        return ret;
+    }
+    ret = qcow2_warn_if_version_3_is_needed(version,
+                                            dedup,
+                                            "Deduplication");
+    if (ret < 0) {
+        return ret;
     }
 
     return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
-                         cluster_size, prealloc, options, version);
+                         cluster_size, prealloc, options, version,
+                         dedup, hash_algo);
 }
 
 static int qcow2_make_empty(BlockDriverState *bs)
@@ -1829,6 +1899,51 @@ static QEMUOptionParameter qcow2_create_options[] = {
     { NULL }
 };
 
+static QEMUOptionParameter qcow2_dedup_create_options[] = {
+    {
+        .name = BLOCK_OPT_SIZE,
+        .type = OPT_SIZE,
+        .help = "Virtual disk size"
+    },
+    {
+        .name = BLOCK_OPT_BACKING_FILE,
+        .type = OPT_STRING,
+        .help = "File name of a base image"
+    },
+    {
+        .name = BLOCK_OPT_BACKING_FMT,
+        .type = OPT_STRING,
+        .help = "Image format of the base image"
+    },
+    {
+        .name = BLOCK_OPT_ENCRYPT,
+        .type = OPT_FLAG,
+        .help = "Encrypt the image"
+    },
+    {
+        .name = BLOCK_OPT_CLUSTER_SIZE,
+        .type = OPT_SIZE,
+        .help = "qcow2 cluster size",
+        .value = { .n = DEFAULT_DEDUP_CLUSTER_SIZE },
+    },
+    {
+        .name = BLOCK_OPT_PREALLOC,
+        .type = OPT_STRING,
+        .help = "Preallocation mode (allowed values: off, metadata)"
+    },
+    {
+        .name = BLOCK_OPT_LAZY_REFCOUNTS,
+        .type = OPT_FLAG,
+        .help = "Postpone refcount updates",
+    },
+    {
+        .name = BLOCK_OPT_DEDUP,
+        .type = OPT_STRING,
+        .help = "Deduplication",
+    },
+    { NULL }
+};
+
 static BlockDriver bdrv_qcow2 = {
     .format_name        = "qcow2",
     .instance_size      = sizeof(BDRVQcowState),
@@ -1868,9 +1983,58 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_check = qcow2_check,
 };
 
+/* As all the defined .create_options are passed to qcow2_create() even if
+ * the user does not specify them it's not possible to have a default 4KB
+ * cluster size for deduplication.
+ * For example it's impossible to make the difference between the 64KB cluster
+ * size default create option of qcow2 or a 64KB user specified cluster size.
+ * So we declare the qcow2_dedup format in order to be able to define
+ * deduplication specific create options.
+ * It will also help for qemu-io-test integration.
+ */
+static BlockDriver bdrv_qcow2_dedup = {
+    .format_name        = "qcow2_dedup",
+    .instance_size      = sizeof(BDRVQcowState),
+    .bdrv_probe         = qcow2_probe,
+    .bdrv_open          = qcow2_open,
+    .bdrv_close         = qcow2_close,
+    .bdrv_reopen_prepare  = qcow2_reopen_prepare,
+    .bdrv_create        = qcow2_create,
+    .bdrv_co_is_allocated = qcow2_co_is_allocated,
+    .bdrv_set_key       = qcow2_set_key,
+    .bdrv_make_empty    = qcow2_make_empty,
+
+    .bdrv_co_readv          = qcow2_co_readv,
+    .bdrv_co_writev         = qcow2_co_writev,
+    .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
+
+    .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
+    .bdrv_co_discard        = qcow2_co_discard,
+    .bdrv_truncate          = qcow2_truncate,
+    .bdrv_write_compressed  = qcow2_write_compressed,
+
+    .bdrv_snapshot_create   = qcow2_snapshot_create,
+    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
+    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
+    .bdrv_snapshot_list     = qcow2_snapshot_list,
+    .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
+    .bdrv_get_info      = qcow2_get_info,
+
+    .bdrv_save_vmstate    = qcow2_save_vmstate,
+    .bdrv_load_vmstate    = qcow2_load_vmstate,
+
+    .bdrv_change_backing_file   = qcow2_change_backing_file,
+
+    .bdrv_invalidate_cache      = qcow2_invalidate_cache,
+
+    .create_options = qcow2_dedup_create_options,
+    .bdrv_check = qcow2_check,
+};
+
 static void bdrv_qcow2_init(void)
 {
     bdrv_register(&bdrv_qcow2);
+    bdrv_register(&bdrv_qcow2_dedup);
 }
 
 block_init(bdrv_qcow2_init);
include/block/block_int.h +1 -0
index 6078dd3..ef4d5d5 100644
@@ -57,6 +57,7 @@
 #define BLOCK_OPT_COMPAT_LEVEL      "compat"
 #define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
 #define BLOCK_OPT_ADAPTER_TYPE      "adapter_type"
+#define BLOCK_OPT_DEDUP             "dedup"
 
 typedef struct BdrvTrackedRequest BdrvTrackedRequest;
 
tests/qemu-iotests/common.rc +2 -1
index 31eb62b..71b67f7 100644
@@ -130,7 +130,8 @@ _make_test_img()
             -e "s# zeroed_grain=\\(on\\|off\\)##g" \
             -e "s# subformat='[^']*'##g" \
             -e "s# adapter_type='[^']*'##g" \
-            -e "s# lazy_refcounts=\\(on\\|off\\)##g"
+            -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
+            -e "s# dedup=\\('sha256'\\|'skein'\\|'sha3'\\)##g"
 
     # Start an NBD server on the image file, which is what we'll be talking to
     if [ $IMGPROTO = "nbd" ]; then
Signature · 1 line
1.7.10.4
#35 lore raw

RFC v8 19/24 qcow2: Integrate deduplication in qcow2_co_writev loop.

In reply to Benoît Canet
---
 block/qcow2.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 86 insertions(+), 2 deletions(-)
block/qcow2.c +86 -2
index e1265a2..8eb63f1 100644
@@ -342,6 +342,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
     Error *local_err = NULL;
     uint64_t ext_end;
 
+    s->has_dedup = false;
     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
     if (ret < 0) {
         goto fail;
@@ -821,13 +822,17 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
     BDRVQcowState *s = bs->opaque;
     int index_in_cluster;
     int n_end;
-    int ret;
+    int ret = 0;
     int cur_nr_sectors; /* number of sectors in current iteration */
     uint64_t cluster_offset;
     QEMUIOVector hd_qiov;
     uint64_t bytes_done = 0;
     uint8_t *cluster_data = NULL;
     QCowL2Meta *l2meta = NULL;
+    uint8_t *dedup_cluster_data = NULL;
+    int dedup_cluster_data_nr;
+    int deduped_sectors_nr;
+    QCowDedupState ds;
 
     trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
                                  remaining_sectors);
@@ -838,13 +843,69 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
 
     qemu_co_mutex_lock(&s->lock);
 
+    if (s->has_dedup) {
+        QTAILQ_INIT(&ds.undedupables);
+        ds.phash.reuse = false;
+        ds.nb_undedupable_sectors = 0;
+        ds.nb_clusters_processed = 0;
+
+        /* if deduplication is on we make sure dedup_cluster_data
+         * contains a multiple of cluster size of data in order
+         * to compute the hashes
+         */
+        ret = qcow2_dedup_read_missing_and_concatenate(bs,
+                                                       qiov,
+                                                       sector_num,
+                                                       remaining_sectors,
+                                                       &dedup_cluster_data,
+                                                       &dedup_cluster_data_nr);
+
+        if (ret < 0) {
+            goto fail;
+        }
+    }
+
     while (remaining_sectors != 0) {
 
         l2meta = NULL;
 
         trace_qcow2_writev_start_part(qemu_coroutine_self());
+
+        if (s->has_dedup && ds.nb_undedupable_sectors == 0) {
+            /* Try to deduplicate as much clusters as possible */
+            deduped_sectors_nr = qcow2_dedup(bs,
+                                             &ds,
+                                             sector_num,
+                                             dedup_cluster_data,
+                                             dedup_cluster_data_nr);
+
+            if (deduped_sectors_nr < 0) {
+                goto fail;
+            }
+
+            remaining_sectors -= deduped_sectors_nr;
+            sector_num += deduped_sectors_nr;
+            bytes_done += deduped_sectors_nr * 512;
+
+            /* no more data to write -> exit */
+            if (remaining_sectors <= 0) {
+                break;
+            }
+
+            /* if we deduped something trace it */
+            if (deduped_sectors_nr) {
+                trace_qcow2_writev_done_part(qemu_coroutine_self(),
+                                             deduped_sectors_nr);
+                trace_qcow2_writev_start_part(qemu_coroutine_self());
+            }
+        }
+
         index_in_cluster = sector_num & (s->cluster_sectors - 1);
-        n_end = index_in_cluster + remaining_sectors;
+        n_end = s->has_dedup &&
+                ds.nb_undedupable_sectors < remaining_sectors ?
+                index_in_cluster + ds.nb_undedupable_sectors :
+                index_in_cluster + remaining_sectors;
+
         if (s->crypt_method &&
             n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
             n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
@@ -912,6 +973,28 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
             l2meta = next;
         }
 
+        /* Write the non duplicated clusters hashes to disk */
+        if (s->has_dedup) {
+            int count = cur_nr_sectors / s->cluster_sectors;
+            int has_ending = ((cluster_offset >> 9) + index_in_cluster +
+                             cur_nr_sectors) & (s->cluster_sectors - 1);
+            if (index_in_cluster) {
+                count++;
+            }
+            if (has_ending) {
+                count++;
+            }
+            ret = qcow2_dedup_store_new_hashes(bs,
+                                               &ds,
+                                               count,
+                                               sector_num,
+                                               (cluster_offset >> 9));
+            if (ret < 0) {
+                goto fail;
+            }
+        }
+
+        ds.nb_undedupable_sectors -= cur_nr_sectors;
         remaining_sectors -= cur_nr_sectors;
         sector_num += cur_nr_sectors;
         bytes_done += cur_nr_sectors * 512;
@@ -937,6 +1020,7 @@ fail:
 
     qemu_iovec_destroy(&hd_qiov);
     qemu_vfree(cluster_data);
+    qemu_vfree(dedup_cluster_data);
     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
 
     return ret;
Signature · 1 line
1.7.10.4
#38 lore raw

RFC v8 22/24 qcow2: Add qcow2_dedup_init and qcow2_dedup_close.

In reply to Benoît Canet
---
 block/qcow2-dedup.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c       |    2 +-
 block/qcow2.h       |    2 ++
 3 files changed, 63 insertions(+), 1 deletion(-)
block/qcow2-dedup.c +60 -0
index 606459f..5eeea38 100644
@@ -716,3 +716,63 @@ void qcow2_dedup_destroy_hash(BlockDriverState *bs,
 free_exit:
    qemu_vfree(buf);
 }
+
+int qcow2_dedup_init(BlockDriverState *bs)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+
+    s->has_dedup = true;
+
+    /* if we are read-only we don't init anything */
+    if (bs->read_only) {
+        return 0;
+    }
+
+    /* no need to allocate the various store's buffers since qcow2_store_load
+     * will do it
+     */
+
+    /* load and parse the configuration from disk */
+    ret = qcow2_store_load(bs, &s->key_value_store);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* if QEMU crashed forget everyting that was stored in the store */
+    if(s->dedup_dirty) {
+        qcow2_store_forget(bs, &s->key_value_store);
+    }
+
+    /* set the dirty bit */
+    s->dedup_dirty = true;
+    qcow2_update_header(bs);
+
+    /* load the journal and the incarnations in a coroutine */
+    return qcow2_store_start(bs, &s->key_value_store);
+}
+
+void qcow2_dedup_close(BlockDriverState *bs)
+{
+    BDRVQcowState *s = bs->opaque;
+    int ret = 0;
+
+    /* if we are read-only we don't need to cleanup */
+    if (bs->read_only) {
+        return;
+    }
+
+    ret = qcow2_store_flush(bs, &s->key_value_store);
+
+    qcow2_store_cleanup(&s->key_value_store);
+
+    /* flush failed -> leave the store dirty so it will be discard at restart */
+    if (ret < 0) {
+        return;
+    }
+
+    /* clear the dirty bit */
+    s->dedup_dirty = false;
+    qcow2_update_header(bs);
+}
block/qcow2.c +1 -1
index ea2f0f2..f7b94dd 100644
@@ -1546,7 +1546,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
             goto out;
         }
 
-        qcow2_store_cleanup(bs, &s->key_value_store);
+        qcow2_store_cleanup(&s->key_value_store);
     }
 
     /* Want a backing file? There you go.*/
block/qcow2.h +2 -0
index 3c6e685..b293be7 100644
@@ -751,5 +751,7 @@ int qcow2_dedup_store_new_hashes(BlockDriverState *bs,
                                  uint64_t physical_sect);
 void qcow2_dedup_destroy_hash(BlockDriverState *bs,
                               uint64_t cluster_index);
+int qcow2_dedup_init(BlockDriverState *bs);
+void qcow2_dedup_close(BlockDriverState *bs);
 
 #endif
Signature · 1 line
1.7.10.4
#37 lore raw

RFC v8 21/24 qcow2: Integrate SKEIN hash algorithm in deduplication.

In reply to Benoît Canet
---
 block/qcow2-dedup.c |   15 +++++++++++++++
 block/qcow2.c       |    5 +++++
 configure           |   35 +++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
block/qcow2-dedup.c +15 -0
index 599cb2e..606459f 100644
@@ -33,6 +33,10 @@
 #include <gnutls/crypto.h>
 #endif
 
+#ifdef CONFIG_SKEIN_DEDUP
+#include <skeinApi.h>
+#endif
+
 /*
  * Prepare a buffer containing everything required to compute cluster
  * sized deduplication hashes.
@@ -153,6 +157,17 @@ static int qcow2_compute_cluster_hash(BlockDriverState *bs,
         return gnutls_hash_fast(GNUTLS_DIG_SHA256, data,
                                 s->cluster_size, hash->data);
 #endif
+#if defined(CONFIG_SKEIN_DEDUP)
+    case QCOW_HASH_SKEIN:
+        {
+        SkeinCtx_t ctx;
+        skeinCtxPrepare(&ctx, Skein256);
+        skeinInit(&ctx, Skein256);
+        skeinUpdate(&ctx, data, s->cluster_size);
+        skeinFinal(&ctx, hash->data);
+        }
+        return 0;
+#endif
     default:
         error_report("Invalid deduplication hash algorithm %i",
                      s->dedup_hash_algo);
block/qcow2.c +5 -0
index 11c115f..ea2f0f2 100644
@@ -1592,6 +1592,11 @@ static int8_t qcow2_get_dedup_hash_algo(char *value)
     if (!value || !strcmp(value, "sha256")) {
         return QCOW_HASH_SHA256;
     }
+#if defined(CONFIG_SKEIN_DEDUP)
+    if (!strcmp(value, "skein")) {
+        return QCOW_HASH_SKEIN;
+    }
+#endif
 
     error_printf("Unsupported deduplication hash algorithm.\n");
     return -EINVAL;
configure +35 -0
index 34b26b2..902c3c1 100755
@@ -241,6 +241,7 @@ gtkabi="2.0"
 tpm="no"
 libssh2=""
 sha256_dedup="yes"
+skein_dedup="no"
 
 # parse CC options first
 for opt do
@@ -935,6 +936,10 @@ for opt do
   ;;
   --enable-sha256-dedup) sha256_dedup="yes"
   ;;
+  --disable-skein-dedup) skein_dedup="no"
+  ;;
+  --enable-skein-dedup) skein_dedup="yes"
+  ;;
   *) echo "ERROR: unknown option $opt"; show_help="yes"
   ;;
   esac
@@ -1202,6 +1207,7 @@ echo "  --gcov=GCOV              use specified gcov [$gcov_tool]"
 echo "  --enable-tpm             enable TPM support"
 echo "  --disable-libssh2        disable ssh block device support"
 echo "  --enable-libssh2         enable ssh block device support"
+echo "  --enable-skein-dedup     enable computing dedup hashes with SKEIN"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2613,6 +2619,30 @@ EOF
   fi
 fi
 
+##########################################
+# SKEIN dedup hash function probe
+if test "$skein_dedup" != "no" ; then
+  cat > $TMPC <<EOF
+#include <skeinApi.h>
+int main(void) {
+    SkeinCtx_t ctx;
+    skeinCtxPrepare(&ctx, 512);
+    return 0;
+}
+EOF
+  skein_libs="-lskein3fish"
+  if compile_prog "" "$skein_libs" ; then
+    skein_dedup=yes
+    libs_tools="$skein_libs $libs_tools"
+    libs_softmmu="$skein_libs $libs_softmmu"
+  else
+    if test "$skein_dedup" = "yes" ; then
+      feature_not_found "libskein3fish not found"
+    fi
+    skein_dedup=no
+  fi
+fi
+
 #
 # Check for xxxat() functions when we are building linux-user
 # emulator.  This is done because older glibc versions don't
@@ -3603,6 +3633,7 @@ echo "gcov enabled      $gcov"
 echo "TPM support       $tpm"
 echo "libssh2 support   $libssh2"
 echo "TPM passthrough   $tpm_passthrough"
+echo "SKEIN support     $skein_dedup"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3991,6 +4022,10 @@ if test "$sha256_dedup" = "yes" ; then
   echo "CONFIG_SHA256_DEDUP=y" >> $config_host_mak
 fi
 
+if test "$skein_dedup" = "yes" ; then
+  echo "CONFIG_SKEIN_DEDUP=y" >> $config_host_mak
+fi
+
 # USB host support
 case "$usb" in
 linux)
Signature · 1 line
1.7.10.4
#39 lore raw

RFC v8 23/24 qcow2: Enable the deduplication feature.

In reply to Benoît Canet
---
 block/qcow2.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
block/qcow2.c +17 -0
index f7b94dd..bb7bf74 100644
@@ -569,6 +569,13 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
         goto fail;
     }
 
+    if (s->incompatible_features & QCOW2_INCOMPAT_DEDUP) {
+        ret = qcow2_dedup_init(bs);
+        if (ret < 0) {
+            goto fail;
+        }
+    }
+
 #ifdef DEBUG_ALLOC
     {
         BdrvCheckResult result = {0};
@@ -1043,8 +1050,13 @@ fail:
 static void qcow2_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
+
     g_free(s->l1_table);
 
+    if (s->has_dedup) {
+        qcow2_dedup_close(bs);
+    }
+
     qcow2_cache_flush(bs, s->l2_table_cache);
     qcow2_cache_flush(bs, s->refcount_block_cache);
 
@@ -1547,6 +1559,11 @@ static int qcow2_create2(const char *filename, int64_t total_size,
         }
 
         qcow2_store_cleanup(&s->key_value_store);
+
+        ret = qcow2_dedup_init(bs);
+        if (ret < 0) {
+            goto out;
+        }
     }
 
     /* Want a backing file? There you go.*/
Signature · 1 line
1.7.10.4
#40 lore raw

RFC v8 24/24 qcow2: Enable deduplication tests

In reply to Benoît Canet
---
 tests/qemu-iotests/common |    6 ++++++
 1 file changed, 6 insertions(+)
tests/qemu-iotests/common +6 -0
index 6826ea7..2483742 100644
@@ -130,6 +130,7 @@ check options
     -cow                test cow
     -qcow               test qcow
     -qcow2              test qcow2
+    -qcow2_dedup        test qcow2 deduplication
     -qed                test qed
     -vdi                test vdi
     -vpc                test vpc
@@ -175,6 +176,11 @@ testlist options
 	    xpand=false
 	    ;;
 
+	-qcow2_dedup)
+	    IMGFMT=qcow2_dedup
+	    xpand=false
+	    ;;
+
 	-qed)
 	    IMGFMT=qed
 	    xpand=false
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Benoît Canet
Quoted 9 lines · Benoît Canet
---
 docs/specs/qcow2.txt |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
docs/specs/qcow2.txt +0 -0
index 36a559d..a4ffc85 100644

docs/specs/qcow2.txt Snapshot table entry: · line 356

Quoted 7 lines
docs/specs/qcow2.txt +4 -0
@@ -350,3 +350,45 @@ Snapshot table entry:
         variable:   Unique ID string for the snapshot (not null terminated)
 
         variable:   Name of the snapshot (not null terminated)
+
+== Journal ==
+
+QCOW2 can use one or more instance of a metadata journal.

s/instance/instances/

Is there a reason to use multiple journals rather than a single journal for all entry types? The single journal area avoids seeks.

docs/specs/qcow2.txt Snapshot table entry: · line 359

Quoted 3 lines
docs/specs/qcow2.txt +3 -0
+
+A journal is a sequential log of journal entries appended on a previously
+allocated and reseted area.

I think you say "previously reset area" instead of "reseted". Another option is "initialized area".

docs/specs/qcow2.txt Snapshot table entry: · line 372

Quoted 13 lines
docs/specs/qcow2.txt +13 -0
+A journal is designed like a linked list with each entry pointing to the next
+so it's easy to iterate over entries.
+
+A journal uses the following constants to denote the type of each entry
+
+TYPE_NONE = 0xFF      default value of any bytes in a reseted journal
+TYPE_END  = 1         the entry ends a journal cluster and point to the next
+                      cluster
+TYPE_HASH = 2         the entry contains a deduplication hash
+
+QCOW2 journal entry:
+
+    Byte 0         :    Size of the entry: size = 2 + n with size <= 254

This is not clear. I'm wondering if the +2 is included in the byte value or not. I'm also wondering what a byte value of zero means and what a byte value of 255 means.

Please include an example to illustrate how this field works.

Quoted 9 lines
docs/specs/qcow2.txt +9 -0
+
+         1         :    Type of the entry
+
+         2 - size  :    The optional n bytes structure carried by entry
+
+A journal is divided into clusters and no journal entry can be spilled on two
+clusters. This avoid having to read more than one cluster to get a single entry.
+
+For this purpose an entry with the end type is added at the end of a journal

docs/specs/qcow2.txt Snapshot table entry: · line 391

Quoted 10 lines
docs/specs/qcow2.txt +10 -0
+cluster before starting to write in the next cluster.
+The size of such an entry is set so the entry points to the next cluster.
+
+As any journal cluster must be ended with an end entry the size of regular
+journal entries is limited to 254 bytes in order to always left room for an end
+entry which mimimal size is two bytes.
+
+The only cases where size > 254 are none entries where size = 255.
+
+The replay of a journal stop when the first end none entry is reached.

s/stop/stops/

docs/specs/qcow2.txt Snapshot table entry: · line 393

Quoted 1 line
docs/specs/qcow2.txt +1 -0
+The journal cluster size is 4096 bytes.

Questions about this layout:

1. Journal entries have no integrity mechanism, which is especially important if they span physical sectors where cheap disks may perform a partial write. This would leave a corrupt journal. If the last bytes are a checksum then you can get some confidence that the entry was fully written and is valid.

Did I miss something?

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

Any ideas how existing journals handle this?

#3 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

Am 02.07.2013 um 16:42 hat Stefan Hajnoczi geschrieben:

Quoted 76 lines · Benoît Canet
Quoted 9 lines
---
 docs/specs/qcow2.txt |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
docs/specs/qcow2.txt +0 -0
index 36a559d..a4ffc85 100644

docs/specs/qcow2.txt Snapshot table entry: · line 356

Quoted 7 lines
docs/specs/qcow2.txt +4 -0
@@ -350,3 +350,45 @@ Snapshot table entry:
         variable:   Unique ID string for the snapshot (not null terminated)
 
         variable:   Name of the snapshot (not null terminated)
+
+== Journal ==
+
+QCOW2 can use one or more instance of a metadata journal.

s/instance/instances/

Is there a reason to use multiple journals rather than a single journal for all entry types? The single journal area avoids seeks.

docs/specs/qcow2.txt Snapshot table entry: · line 359

Quoted 3 lines
docs/specs/qcow2.txt +3 -0
+
+A journal is a sequential log of journal entries appended on a previously
+allocated and reseted area.

I think you say "previously reset area" instead of "reseted". Another option is "initialized area".

docs/specs/qcow2.txt Snapshot table entry: · line 372

Quoted 13 lines
docs/specs/qcow2.txt +13 -0
+A journal is designed like a linked list with each entry pointing to the next
+so it's easy to iterate over entries.
+
+A journal uses the following constants to denote the type of each entry
+
+TYPE_NONE = 0xFF      default value of any bytes in a reseted journal
+TYPE_END  = 1         the entry ends a journal cluster and point to the next
+                      cluster
+TYPE_HASH = 2         the entry contains a deduplication hash
+
+QCOW2 journal entry:
+
+    Byte 0         :    Size of the entry: size = 2 + n with size <= 254

This is not clear. I'm wondering if the +2 is included in the byte value or not. I'm also wondering what a byte value of zero means and what a byte value of 255 means.

Please include an example to illustrate how this field works.

Quoted 9 lines
docs/specs/qcow2.txt +9 -0
+
+         1         :    Type of the entry
+
+         2 - size  :    The optional n bytes structure carried by entry
+
+A journal is divided into clusters and no journal entry can be spilled on two
+clusters. This avoid having to read more than one cluster to get a single entry.
+
+For this purpose an entry with the end type is added at the end of a journal

docs/specs/qcow2.txt Snapshot table entry: · line 391

Quoted 10 lines
docs/specs/qcow2.txt +10 -0
+cluster before starting to write in the next cluster.
+The size of such an entry is set so the entry points to the next cluster.
+
+As any journal cluster must be ended with an end entry the size of regular
+journal entries is limited to 254 bytes in order to always left room for an end
+entry which mimimal size is two bytes.
+
+The only cases where size > 254 are none entries where size = 255.
+
+The replay of a journal stop when the first end none entry is reached.

s/stop/stops/

docs/specs/qcow2.txt Snapshot table entry: · line 393

Quoted 1 line
docs/specs/qcow2.txt +1 -0
+The journal cluster size is 4096 bytes.

Questions about this layout:

Quoted 7 lines

1. Journal entries have no integrity mechanism, which is especially important if they span physical sectors where cheap disks may perform a partial write. This would leave a corrupt journal. If the last bytes are a checksum then you can get some confidence that the entry was fully written and is valid.

Did I miss something?

Adding a checksum sounds like a good idea.

Quoted 5 lines

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

Any ideas how existing journals handle this?

You commit only whole blocks. So in this case we can consider a block only committed as soon as a TYPE_END entry has been written (and after that we won't touch it any more until the journalled changes have been flushed to disk).

There's one "interesting" case: cache=writethrough. I'm not entirely sure yet what to do with it, but it's slow anyway, so using one block per entry and therefore flushing the journal very often might actually be not totally unreasonable.

Another thing I'm not sure about is whether a fixed 4k block is good or if we should leave it configurable. I don't think making it an option would hurt (not necessarily modifyable with qemu-img, but as a field in the file format).

Kevin

#7 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Stefan Hajnoczi
Quoted 6 lines
Quoted 1 line

+QCOW2 can use one or more instance of a metadata journal.

s/instance/instances/

Is there a reason to use multiple journals rather than a single journal for all entry types? The single journal area avoids seeks.

Here are the main reason for this:

For the deduplication some patterns like cycles of insertion/deletion could leave the hash table almost empty while filling the journal.

If the journal is full and the hash table is empty a packing operation is started.

Basically a new journal is created and only the entry presents in the hash table are reinserted.

This is why I want to keep the deduplication journal appart from regular qcow2
journal: to avoid interferences between a pack operation and regular qcow2 journal entries.

The other thing is that freezing the log store would need a replay of regular qcow2 entries as it trigger a reset of the journal.

Also since deduplication will not work on spinning disk I discarded the seek time factor.

Maybe commiting the dedupe journal by erase block sized chunk would be a good idea to reduce random writes to the SSD.

The additional reason for having multiple journals is that the SILT paper propose a mode where prefix of the hash is used to dispatch insertions in multiples store and it easier to do with multiple journals.

Quoted 17 lines

docs/specs/qcow2.txt Snapshot table entry: · line 359

Quoted 4 lines
docs/specs/qcow2.txt +3 -0
+
+A journal is a sequential log of journal entries appended on a previously
+allocated and reseted area.

I think you say "previously reset area" instead of "reseted". Another option is "initialized area".

Quoted 9 lines
docs/specs/qcow2.txt +9 -0
+A journal is designed like a linked list with each entry pointing to the next
+so it's easy to iterate over entries.
+
+A journal uses the following constants to denote the type of each entry
+
+TYPE_NONE = 0xFF      default value of any bytes in a reseted journal
+TYPE_END  = 1         the entry ends a journal cluster and point to the next
+                      cluster
+TYPE_HASH = 2         the entry contains a deduplication hash
Quoted 8 lines

docs/specs/qcow2.txt Snapshot table entry: · line 372

Quoted 4 lines
docs/specs/qcow2.txt +4 -0
+
+QCOW2 journal entry:
+
+    Byte 0         :    Size of the entry: size = 2 + n with size <= 254

This is not clear. I'm wondering if the +2 is included in the byte value or not. I'm also wondering what a byte value of zero means and what a byte value of 255 means.

I am counting the journal entry header in the size. So yes the +2 is in the byte value.
A byte value of zero, 1 or 255 is an error.

Maybe this design is bogus and I should only count the payload size in the size field. It would make less tricky cases.

Quoted 27 lines

Please include an example to illustrate how this field works.

Quoted 9 lines
docs/specs/qcow2.txt +9 -0
+
+         1         :    Type of the entry
+
+         2 - size  :    The optional n bytes structure carried by entry
+
+A journal is divided into clusters and no journal entry can be spilled on two
+clusters. This avoid having to read more than one cluster to get a single entry.
+
+For this purpose an entry with the end type is added at the end of a journal

docs/specs/qcow2.txt Snapshot table entry: · line 391

Quoted 10 lines
docs/specs/qcow2.txt +10 -0
+cluster before starting to write in the next cluster.
+The size of such an entry is set so the entry points to the next cluster.
+
+As any journal cluster must be ended with an end entry the size of regular
+journal entries is limited to 254 bytes in order to always left room for an end
+entry which mimimal size is two bytes.
+
+The only cases where size > 254 are none entries where size = 255.
+
+The replay of a journal stop when the first end none entry is reached.

s/stop/stops/

Quoted 1 line

+The journal cluster size is 4096 bytes.

Quoted 7 lines

Questions about this layout:

1. Journal entries have no integrity mechanism, which is especially important if they span physical sectors where cheap disks may perform a partial write. This would leave a corrupt journal. If the last bytes are a checksum then you can get some confidence that the entry was fully written and is valid.

I will add a checksum mecanism.

Do you have any preferences regarding the checksum function ?

Quoted 6 lines

Did I miss something?

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

It's designed to be committed by 4KB blocks.

Quoted 3 lines

Any ideas how existing journals handle this?

#4 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Kevin Wolf
Quoted 11 lines
Quoted 5 lines

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

Any ideas how existing journals handle this?

You commit only whole blocks. So in this case we can consider a block only committed as soon as a TYPE_END entry has been written (and after that we won't touch it any more until the journalled changes have been flushed to disk).

Quoted 4 lines

There's one "interesting" case: cache=writethrough. I'm not entirely sure yet what to do with it, but it's slow anyway, so using one block per entry and therefore flushing the journal very often might actually be not totally unreasonable.

This sure would finish to kill the performance because this would be an io per metadata written to disk.

Quoted 5 lines

Another thing I'm not sure about is whether a fixed 4k block is good or if we should leave it configurable. I don't think making it an option would hurt (not necessarily modifyable with qemu-img, but as a field in the file format).

I agree.
I also think about make the number of block to be flushed at once configurable.

Benoît

#6 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Kevin Wolf
Quoted 103 lines · Kevin Wolf

Am 02.07.2013 um 16:42 hat Stefan Hajnoczi geschrieben:

Quoted 76 lines · Benoît Canet
Quoted 9 lines
---
 docs/specs/qcow2.txt |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
docs/specs/qcow2.txt +0 -0
index 36a559d..a4ffc85 100644

docs/specs/qcow2.txt Snapshot table entry: · line 356

Quoted 7 lines
docs/specs/qcow2.txt +4 -0
@@ -350,3 +350,45 @@ Snapshot table entry:
         variable:   Unique ID string for the snapshot (not null terminated)
 
         variable:   Name of the snapshot (not null terminated)
+
+== Journal ==
+
+QCOW2 can use one or more instance of a metadata journal.

s/instance/instances/

Is there a reason to use multiple journals rather than a single journal for all entry types? The single journal area avoids seeks.

docs/specs/qcow2.txt Snapshot table entry: · line 359

Quoted 3 lines
docs/specs/qcow2.txt +3 -0
+
+A journal is a sequential log of journal entries appended on a previously
+allocated and reseted area.

I think you say "previously reset area" instead of "reseted". Another option is "initialized area".

docs/specs/qcow2.txt Snapshot table entry: · line 372

Quoted 13 lines
docs/specs/qcow2.txt +13 -0
+A journal is designed like a linked list with each entry pointing to the next
+so it's easy to iterate over entries.
+
+A journal uses the following constants to denote the type of each entry
+
+TYPE_NONE = 0xFF      default value of any bytes in a reseted journal
+TYPE_END  = 1         the entry ends a journal cluster and point to the next
+                      cluster
+TYPE_HASH = 2         the entry contains a deduplication hash
+
+QCOW2 journal entry:
+
+    Byte 0         :    Size of the entry: size = 2 + n with size <= 254

This is not clear. I'm wondering if the +2 is included in the byte value or not. I'm also wondering what a byte value of zero means and what a byte value of 255 means.

Please include an example to illustrate how this field works.

Quoted 9 lines
docs/specs/qcow2.txt +9 -0
+
+         1         :    Type of the entry
+
+         2 - size  :    The optional n bytes structure carried by entry
+
+A journal is divided into clusters and no journal entry can be spilled on two
+clusters. This avoid having to read more than one cluster to get a single entry.
+
+For this purpose an entry with the end type is added at the end of a journal

docs/specs/qcow2.txt Snapshot table entry: · line 391

Quoted 10 lines
docs/specs/qcow2.txt +10 -0
+cluster before starting to write in the next cluster.
+The size of such an entry is set so the entry points to the next cluster.
+
+As any journal cluster must be ended with an end entry the size of regular
+journal entries is limited to 254 bytes in order to always left room for an end
+entry which mimimal size is two bytes.
+
+The only cases where size > 254 are none entries where size = 255.
+
+The replay of a journal stop when the first end none entry is reached.

s/stop/stops/

docs/specs/qcow2.txt Snapshot table entry: · line 393

Quoted 1 line
docs/specs/qcow2.txt +1 -0
+The journal cluster size is 4096 bytes.

Questions about this layout:

Quoted 7 lines

1. Journal entries have no integrity mechanism, which is especially important if they span physical sectors where cheap disks may perform a partial write. This would leave a corrupt journal. If the last bytes are a checksum then you can get some confidence that the entry was fully written and is valid.

Did I miss something?

Adding a checksum sounds like a good idea.

Quoted 5 lines

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

Any ideas how existing journals handle this?

You commit only whole blocks. So in this case we can consider a block only committed as soon as a TYPE_END entry has been written (and after that we won't touch it any more until the journalled changes have been flushed to disk).

There's one "interesting" case: cache=writethrough. I'm not entirely sure yet what to do with it, but it's slow anyway, so using one block per entry and therefore flushing the journal very often might actually be not totally unreasonable.

Quoted 4 lines

Another thing I'm not sure about is whether a fixed 4k block is good or if we should leave it configurable. I don't think making it an option would hurt (not necessarily modifyable with qemu-img, but as a field in the file format).

Making block size configurable seems like a good idea so we can adapt to disk performance and data integrity characteristics.

Stefan

#8 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Benoît Canet
Quoted 28 lines · Benoît Canet
Quoted 6 lines
Quoted 1 line

+QCOW2 can use one or more instance of a metadata journal.

s/instance/instances/

Is there a reason to use multiple journals rather than a single journal for all entry types? The single journal area avoids seeks.

Here are the main reason for this:

For the deduplication some patterns like cycles of insertion/deletion could leave the hash table almost empty while filling the journal.

If the journal is full and the hash table is empty a packing operation is started.

Basically a new journal is created and only the entry presents in the hash table are reinserted.

This is why I want to keep the deduplication journal appart from regular qcow2
journal: to avoid interferences between a pack operation and regular qcow2 journal entries.

The other thing is that freezing the log store would need a replay of regular qcow2 entries as it trigger a reset of the journal.

Also since deduplication will not work on spinning disk I discarded the seek time factor.

Quoted 6 lines

Maybe commiting the dedupe journal by erase block sized chunk would be a good idea to reduce random writes to the SSD.

The additional reason for having multiple journals is that the SILT paper propose a mode where prefix of the hash is used to dispatch insertions in multiples store and it easier to do with multiple journals.

It sounds like the journal is more than just a data integrity mechanism. It's an integral part of your dedup algorithm and you plan to carefully manage it while rebuilding some of the other dedup data structures.

Does this mean the journal forms the first-stage data structure for deduplication? Dedup records will accumulate in the journal until it becomes time to convert them in bulk into a more compact representation?

When I read this specification I was thinking of a journal purely for logging operations. You could use a commit record to mark previous records applied. Upon startup, qcow2 would inspect uncommitted records and deal with them.

We just need to figure out how to define a good interface so that the journal can be used in a general way but also for dedup's specific needs.

Stefan

#10 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Benoît Canet

Am 02.07.2013 um 23:23 hat Benoît Canet geschrieben:

Quoted 2 lines

Also since deduplication will not work on spinning disk I discarded the seek time factor.

Care to explain that in more detail? Why shouldn't it work on spinning disks?

Kevin

#5 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Benoît Canet

Am 02.07.2013 um 23:26 hat Benoît Canet geschrieben:

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

2. Byte-granularity means that read-modify-write is necessary to append entries to the journal. Therefore a failure could destroy previously committed entries.

Any ideas how existing journals handle this?

You commit only whole blocks. So in this case we can consider a block only committed as soon as a TYPE_END entry has been written (and after that we won't touch it any more until the journalled changes have been flushed to disk).

Quoted 7 lines
Quoted 4 lines

There's one "interesting" case: cache=writethrough. I'm not entirely sure yet what to do with it, but it's slow anyway, so using one block per entry and therefore flushing the journal very often might actually be not totally unreasonable.

This sure would finish to kill the performance because this would be an io per metadata written to disk.

cache=writethrough already pretty much kills performance because it's not only an I/O per metadata write, but also a flush.

The question is, do we have any option to avoid it?

Quoted 7 lines
Quoted 4 lines

Another thing I'm not sure about is whether a fixed 4k block is good or if we should leave it configurable. I don't think making it an option would hurt (not necessarily modifyable with qemu-img, but as a field in the file format).

I agree.
I also think about make the number of block to be flushed at once configurable.

This is more of a runtime option. We can store a default in the image, though.

Kevin

#12 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Benoît Canet
Quoted 1 line · Benoît Canet
Quoted 1 line

Any ideas how existing journals handle this?

By the way, I don't know much about journalling techniques. So I'm asking you these questions so that either you can answer them straight away or because they might warrant a look at existing journal implementations like:

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/jbd2
http://www.sqlite.org/cgi/src/dir?name=src
http://blitiri.com.ar/p/libjio/

Stefan

#11 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Kevin Wolf
Quoted 2 lines

Care to explain that in more detail? Why shouldn't it work on spinning disks?

Hash are random they introduce random read access.

With a QCOW2 cluster size of 4KB the deduplication code when writting duplicated data will do one random read per 4KB block to deduplicate.

A server grade hardisk is rated for 250 iops. This traduce in 1MB/s of deduplicated data. Not very usable.

On the contrary a samsung 840 pro SSD is rated for 80k iops of random read. That should traduce in 320MB/s of potentially deduplicated data.

Havind dedup metadata on SSD and actual data on disk would solve the problem but it would need block backend.

Benoît

#9 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Stefan Hajnoczi
Quoted 3 lines

Does this mean the journal forms the first-stage data structure for deduplication? Dedup records will accumulate in the journal until it becomes time to convert them in bulk into a more compact representation?

The journal is mainly used to persist the last inserted dedup metadata across QEMU stop and restart. I replay it at startup to rebuild the hash table. So yes it's the first stage even it's never used for regular queries.

Quoted 5 lines

When I read this specification I was thinking of a journal purely for logging operations. You could use a commit record to mark previous records applied. Upon startup, qcow2 would inspect uncommitted records and deal with them.

Maybe that could help regular QCOW2 usage. I don't know.

#13 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

In reply to Stefan Hajnoczi
Quoted 4 lines

By the way, I don't know much about journalling techniques. So I'm asking you these questions so that either you can answer them straight away or because they might warrant a look at existing journal implementations like:

I tried to so something simple and performing for the deduplication usage.

That explain that there is no concept of transaction and that the journal's block are flushed asynchronously in order to have an high insertion rate.

I agree with your previous comment is more a log than a journal.

Quoted 4 lines

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/jbd2
http://www.sqlite.org/cgi/src/dir?name=src
http://blitiri.com.ar/p/libjio/

I will try to find a paper on journal design.

Benoît

#14 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

Quoted 11 lines · Benoît Canet
Quoted 4 lines

By the way, I don't know much about journalling techniques. So I'm asking you these questions so that either you can answer them straight away or because they might warrant a look at existing journal implementations like:

I tried to so something simple and performing for the deduplication usage.

That explain that there is no concept of transaction and that the journal's block are flushed asynchronously in order to have an high insertion rate.

I agree with your previous comment is more a log than a journal.

Simple is good. Even for deduplication alone, I think data integrity is critical - otherwise we risk stale dedup metadata pointing to clusters that are unallocated or do not contain the right data. So the journal will probably need to follow techniques for commits/checksums.

Stefan

#15 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

Quoted 4 lines

Simple is good. Even for deduplication alone, I think data integrity is critical - otherwise we risk stale dedup metadata pointing to clusters that are unallocated or do not contain the right data. So the journal will probably need to follow techniques for commits/checksums.

I agree that checksums are missing for the dedup.
Maybe we could even use some kind of error correcting code instead of a checksum.

Concerning data integrity the events that the deduplication code cannot loose are hash deletions because they mark a previously inserted hash as obsolete.

The problem with a commit/flush mechanism on hash deletion is that it will slow down the store insertion speed and also create some extra SSD wear out.

To solve this I considered the fact that the dedup metadata as a whole is disposable.

So I implemented a "dedup dirty" bit.

When QEMU stop the journal is flushed and the dirty bit is cleared. When QEMU start and the dirty bit is set a crash is detected and _all_ the deduplication metadata is dropped.
The QCOW2 data integrity won't suffer only the dedup ratio will be lower.

As you said once on irc crashes don't happen often.

Benoît

#16 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

Quoted 5 lines
Quoted 4 lines

Simple is good. Even for deduplication alone, I think data integrity is critical - otherwise we risk stale dedup metadata pointing to clusters that are unallocated or do not contain the right data. So the journal will probably need to follow techniques for commits/checksums.

I'll add checksums to the journal and clean the journal entry size mess soon.

For the transactional/commits aspect of the journal I think that we need Kevin's point of view on the subject.

Best regards

Benoît

#17 lore raw

Re: [Qemu-devel] [RFC V8 01/24] qcow2: Add journal specification.

Am 17.07.2013 um 00:45 hat Benoît Canet geschrieben:

Quoted 10 lines
Quoted 5 lines
Quoted 4 lines

Simple is good. Even for deduplication alone, I think data integrity is critical - otherwise we risk stale dedup metadata pointing to clusters that are unallocated or do not contain the right data. So the journal will probably need to follow techniques for commits/checksums.

I'll add checksums to the journal and clean the journal entry size mess soon.

For the transactional/commits aspect of the journal I think that we need Kevin's point of view on the subject.

Sorry, I was going to prepare a patch that does journalling for the existing metadata, but once again other things stole my time. I'm still planning to do that, though, and then we can compare whether your requirements are fulfilled with it as well.

Kevin