9ae3a8
From 789d0334d894eaea6a5ca35538926bbb63a9cea3 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Mon, 9 Sep 2013 14:28:01 +0200
9ae3a8
Subject: [PATCH 10/38] qcow2: Options to enable discard for freed clusters
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1378736903-18489-11-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 54195
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 10/32] qcow2: Options to enable discard for freed clusters
9ae3a8
Bugzilla: 1005818
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Bugzilla: 1005818
9ae3a8
9ae3a8
Deleted snapshots are discarded in the image file by default, discard
9ae3a8
requests take their default from the -drive discard=... option and other
9ae3a8
places that free clusters must always be enabled explicitly.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 67af674e478054086f972811dd0a11289afa39a9)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c |  5 +++++
9ae3a8
 block/qcow2.c          | 26 ++++++++++++++++++++++++++
9ae3a8
 block/qcow2.h          |  5 +++++
9ae3a8
 3 files changed, 36 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c |    5 +++++
9ae3a8
 block/qcow2.c          |   26 ++++++++++++++++++++++++++
9ae3a8
 block/qcow2.h          |    5 +++++
9ae3a8
 3 files changed, 36 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 6d35e49..7488988 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -488,6 +488,11 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
9ae3a8
             s->free_cluster_index = cluster_index;
9ae3a8
         }
9ae3a8
         refcount_block[block_index] = cpu_to_be16(refcount);
9ae3a8
+        if (refcount == 0 && s->discard_passthrough[type]) {
9ae3a8
+            /* Try discarding, ignore errors */
9ae3a8
+            /* FIXME Doing this cluster by cluster will be painfully slow */
9ae3a8
+            bdrv_discard(bs->file, cluster_offset, 1);
9ae3a8
+        }
9ae3a8
     }
9ae3a8
 
9ae3a8
     ret = 0;
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index e28ea47..ef8a2ca 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -295,6 +295,22 @@ static QemuOptsList qcow2_runtime_opts = {
9ae3a8
             .type = QEMU_OPT_BOOL,
9ae3a8
             .help = "Postpone refcount updates",
9ae3a8
         },
9ae3a8
+        {
9ae3a8
+            .name = QCOW2_OPT_DISCARD_REQUEST,
9ae3a8
+            .type = QEMU_OPT_BOOL,
9ae3a8
+            .help = "Pass guest discard requests to the layer below",
9ae3a8
+        },
9ae3a8
+        {
9ae3a8
+            .name = QCOW2_OPT_DISCARD_SNAPSHOT,
9ae3a8
+            .type = QEMU_OPT_BOOL,
9ae3a8
+            .help = "Generate discard requests when snapshot related space "
9ae3a8
+                    "is freed",
9ae3a8
+        },
9ae3a8
+        {
9ae3a8
+            .name = QCOW2_OPT_DISCARD_OTHER,
9ae3a8
+            .type = QEMU_OPT_BOOL,
9ae3a8
+            .help = "Generate discard requests when other clusters are freed",
9ae3a8
+        },
9ae3a8
         { /* end of list */ }
9ae3a8
     },
9ae3a8
 };
9ae3a8
@@ -532,6 +548,16 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
9ae3a8
     s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
9ae3a8
         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
9ae3a8
 
9ae3a8
+    s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
9ae3a8
+    s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
9ae3a8
+    s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
9ae3a8
+        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
9ae3a8
+                          flags & BDRV_O_UNMAP);
9ae3a8
+    s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
9ae3a8
+        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
9ae3a8
+    s->discard_passthrough[QCOW2_DISCARD_OTHER] =
9ae3a8
+        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
9ae3a8
+
9ae3a8
     qemu_opts_del(opts);
9ae3a8
 
9ae3a8
     if (s->use_lazy_refcounts && s->qcow_version < 3) {
9ae3a8
diff --git a/block/qcow2.h b/block/qcow2.h
9ae3a8
index 64a6479..6f91b9a 100644
9ae3a8
--- a/block/qcow2.h
9ae3a8
+++ b/block/qcow2.h
9ae3a8
@@ -60,6 +60,9 @@
9ae3a8
 
9ae3a8
 
9ae3a8
 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy_refcounts"
9ae3a8
+#define QCOW2_OPT_DISCARD_REQUEST "pass_discard_request"
9ae3a8
+#define QCOW2_OPT_DISCARD_SNAPSHOT "pass_discard_snapshot"
9ae3a8
+#define QCOW2_OPT_DISCARD_OTHER "pass_discard_other"
9ae3a8
 
9ae3a8
 typedef struct QCowHeader {
9ae3a8
     uint32_t magic;
9ae3a8
@@ -187,6 +190,8 @@ typedef struct BDRVQcowState {
9ae3a8
     int qcow_version;
9ae3a8
     bool use_lazy_refcounts;
9ae3a8
 
9ae3a8
+    bool discard_passthrough[QCOW2_DISCARD_MAX];
9ae3a8
+
9ae3a8
     uint64_t incompatible_features;
9ae3a8
     uint64_t compatible_features;
9ae3a8
     uint64_t autoclear_features;
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8