[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC V1 02/12] qcow2: Add code to deduplicate cluster flagg
From: |
Benoît Canet |
Subject: |
[Qemu-devel] [RFC V1 02/12] qcow2: Add code to deduplicate cluster flagged with QCOW_OFLAG_TO_DEDUP. |
Date: |
Wed, 16 Jan 2013 17:25:08 +0100 |
---
block/qcow2-dedup.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 126 insertions(+)
diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c
index 4305746..dd320bf 100644
--- a/block/qcow2-dedup.c
+++ b/block/qcow2-dedup.c
@@ -1166,6 +1166,130 @@ static int qcow2_drop_to_dedup_hashes(BlockDriverState
*bs)
return 0;
}
+static bool qcow2_try_dedup_on_disk_cluster(BlockDriverState *bs,
+ QcowPersistantHash *phash,
+ uint64_t index,
+ uint64_t physical_sect)
+{
+ BDRVQcowState *s = bs->opaque;
+ uint8_t *data;
+ int ret = 0;
+ bool result = false;
+
+ data = qemu_blockalign(bs, s->cluster_size);
+
+ /* read the cluster data from disk */
+ ret = bdrv_pread(bs->file, physical_sect << 9, data, s->cluster_size);
+
+ if (ret < 0) {
+ goto exit;
+ }
+
+ /* force computation of the hash */
+ phash->reuse = false;
+
+ ret = qcow2_try_dedup_cluster(bs,
+ phash,
+ index * s->cluster_sectors,
+ data,
+ 0);
+
+ if (ret < 0) {
+ goto exit;
+ }
+
+ /* cluster was deduplicated -> result is true */
+ if (ret) {
+ result = true;
+ }
+
+exit:
+ qemu_vfree(data);
+ return result;
+}
+
+static bool qcow2_process_undeduplicated_cluster(BlockDriverState *bs,
+ QcowPersistantHash *phash,
+ uint64_t index,
+ uint64_t physical_sect)
+{
+ BDRVQcowState *s = bs->opaque;
+ int ret = 0;
+
+ /* not duplicated */
+ ret = qcow2_store_hash(bs, &phash->hash,
+ index * s->cluster_sectors,
+ physical_sect);
+
+ if (ret < 0) {
+ error_printf("Error while storing hash");
+ return false;
+ }
+
+ /* remove the QCOW_OFLAG_TO_DEDUP flag from l2 entry
+ * note: we should take care of setting QCOW_OFLAG_COPIED if needed
+ */
+ ret = qcow2_dedup_link_l2(bs, index * s->cluster_sectors,
+ physical_sect, true);
+
+ return ret == 0 ? true : false;
+}
+
+static bool qcow2_process_to_dedup_cluster(BlockDriverState *bs,
+ uint64_t index)
+{
+ QcowPersistantHash phash;
+ bool to_dedup, deduplicated;
+ uint64_t physical_sect;
+ int ret = 0;
+
+ to_dedup = qcow2_is_cluster_to_dedup(bs, index, &physical_sect, &ret);
+
+ if (ret < 0) {
+ error_printf("Error checking if cluster must be deduplicated");
+ return false;
+ }
+
+ if (!to_dedup) {
+ return false;
+ }
+
+ deduplicated = qcow2_try_dedup_on_disk_cluster(bs,
+ &phash,
+ index,
+ physical_sect);
+
+ if (deduplicated) {
+ return true;
+ }
+
+ qcow2_process_undeduplicated_cluster(bs,
+ &phash,
+ index,
+ physical_sect);
+
+ return true;
+}
+
+/* This function try to deduplicate clusters written when dedup was not
running.
+ */
+static void qcow2_deduplicate_after_resuming(BlockDriverState *bs)
+{
+ BDRVQcowState *s = bs->opaque;
+ uint64_t i;
+ bool processed;
+
+ /* for each l2 entry */
+ for (i = 0; i < s->l2_size * s->l1_size; i++) {
+ qemu_co_mutex_lock(&s->lock);
+ processed = qcow2_process_to_dedup_cluster(bs, i);
+ qemu_co_mutex_unlock(&s->lock);
+ if (processed || !(i % s->l2_size)) {
+ co_sleep_ns(rt_clock, s->dedup_co_delay);
+ }
+ }
+}
+
/*
* This coroutine resume deduplication
*
@@ -1194,6 +1318,8 @@ static void coroutine_fn qcow2_co_dedup_resume(void
*opaque)
s->dedup_status = QCOW_DEDUP_STARTED;
qemu_co_mutex_unlock(&s->lock);
+ qcow2_deduplicate_after_resuming(bs);
+
return;
fail:
--
1.7.10.4
- [Qemu-devel] [RFC V1 00/12] QCOW2 asynchronous deduplication, Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 01/12] block: Add BlockDriver function prototype to pause and resume deduplication., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 02/12] qcow2: Add code to deduplicate cluster flagged with QCOW_OFLAG_TO_DEDUP.,
Benoît Canet <=
- [Qemu-devel] [RFC V1 07/12] qcow2: Add qcow2_pause_dedup., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 08/12] qcow2: Add qcow2_resume_dedup., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 09/12] qcow2: Make dedup status persists., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 12/12] qmp: Add block_resume_dedup., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 06/12] block: Add bdrv_pause_dedup., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 11/12] qmp: Add block-pause-dedup., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 04/12] block: Add bdrv_is_dedup_running., Benoît Canet, 2013/01/16
- [Qemu-devel] [RFC V1 05/12] block: Add bdrv_resume_dedup., Benoît Canet, 2013/01/16