qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 14/28] qcow2: Add bdrv_discard support
Date: Mon, 31 Jan 2011 16:29:02 +0100	[thread overview]
Message-ID: <1296487756-12553-15-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1296487756-12553-1-git-send-email-kwolf@redhat.com>

This adds a bdrv_discard function to qcow2 that frees the discarded clusters.
It does not yet pass the discard on to the underlying file system driver, but
the space can be reused by future writes to the image.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 block/qcow2-cluster.c |   82 +++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c         |    8 +++++
 block/qcow2.h         |    2 +
 3 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 1c2003a..5fb8c66 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -888,3 +888,85 @@ int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
     }
     return 0;
 }
+
+/*
+ * This discards as many clusters of nb_clusters as possible at once (i.e.
+ * all clusters in the same L2 table) and returns the number of discarded
+ * clusters.
+ */
+static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
+    unsigned int nb_clusters)
+{
+    BDRVQcowState *s = bs->opaque;
+    uint64_t l2_offset, *l2_table;
+    int l2_index;
+    int ret;
+    int i;
+
+    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* Limit nb_clusters to one L2 table */
+    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
+
+    for (i = 0; i < nb_clusters; i++) {
+        uint64_t old_offset;
+
+        old_offset = be64_to_cpu(l2_table[l2_index + i]);
+        old_offset &= ~QCOW_OFLAG_COPIED;
+
+        if (old_offset == 0) {
+            continue;
+        }
+
+        /* First remove L2 entries */
+        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
+        l2_table[l2_index + i] = cpu_to_be64(0);
+
+        /* Then decrease the refcount */
+        qcow2_free_any_clusters(bs, old_offset, 1);
+    }
+
+    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return nb_clusters;
+}
+
+int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
+    int nb_sectors)
+{
+    BDRVQcowState *s = bs->opaque;
+    uint64_t end_offset;
+    unsigned int nb_clusters;
+    int ret;
+
+    end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS);
+
+    /* Round start up and end down */
+    offset = align_offset(offset, s->cluster_size);
+    end_offset &= ~(s->cluster_size - 1);
+
+    if (offset > end_offset) {
+        return 0;
+    }
+
+    nb_clusters = size_to_clusters(s, end_offset - offset);
+
+    /* Each L2 table is handled by its own loop iteration */
+    while (nb_clusters > 0) {
+        ret = discard_single_l2(bs, offset, nb_clusters);
+        if (ret < 0) {
+            return ret;
+        }
+
+        nb_clusters -= ret;
+        offset += (ret * s->cluster_size);
+    }
+
+    return 0;
+}
diff --git a/block/qcow2.c b/block/qcow2.c
index 49bf7b9..dbe4fdd 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1084,6 +1084,13 @@ static int qcow2_make_empty(BlockDriverState *bs)
     return 0;
 }
 
+static int qcow2_discard(BlockDriverState *bs, int64_t sector_num,
+    int nb_sectors)
+{
+    return qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
+        nb_sectors);
+}
+
 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
 {
     BDRVQcowState *s = bs->opaque;
@@ -1349,6 +1356,7 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_aio_writev    = qcow2_aio_writev,
     .bdrv_aio_flush     = qcow2_aio_flush,
 
+    .bdrv_discard           = qcow2_discard,
     .bdrv_truncate          = qcow2_truncate,
     .bdrv_write_compressed  = qcow2_write_compressed,
 
diff --git a/block/qcow2.h b/block/qcow2.h
index 6d80120..a019831 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -209,6 +209,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
                                          int compressed_size);
 
 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
+int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
+    int nb_sectors);
 
 /* qcow2-snapshot.c functions */
 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
-- 
1.7.2.3

  parent reply	other threads:[~2011-01-31 15:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-31 15:28 [Qemu-devel] [PULL 00/28] Block patches Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 01/28] strtosz(): use unsigned char and switch to qemu_isspace() Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 02/28] strtosz() use qemu_toupper() to simplify switch statement Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 03/28] strtosz(): Fix name confusion in use of modf() Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 04/28] strtosz(): Use suffix macros in switch() statement Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 05/28] block: add block_resize monitor command Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 06/28] block: tell drivers about an image resize Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 07/28] virtio-blk: tell the guest about size changes Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 08/28] virtio-pci: Disable virtio-ioeventfd when !CONFIG_IOTHREAD Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 09/28] Add documentation for STRTOSZ_DEFSUFFIX_ macros Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 10/28] ahci: Fix cpu_physical_memory_unmap() argument ordering Kevin Wolf
2011-01-31 15:28 ` [Qemu-devel] [PATCH 11/28] Reorganize struct Qcow2Cache for better struct packing Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 12/28] sheepdog: support creating images on remote hosts Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 13/28] qemu-io: Fix discard command Kevin Wolf
2011-01-31 15:29 ` Kevin Wolf [this message]
2011-01-31 15:29 ` [Qemu-devel] [PATCH 15/28] qed: Images with backing file do not require QED_F_NEED_CHECK Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 16/28] raw-win32: Fix bdrv_flush return value Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 17/28] scsi hotplug: Set DriveInfo member bus correctly Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 18/28] qcow2-refcount: remove write-only variables Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 19/28] blockdev: New drive_get_next(), replacing qdev_init_bdrv() Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 20/28] blockdev: Move BlockInterfaceType from qemu-common.h to blockdev.h Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 21/28] blockdev: Put BlockInterfaceType names and max_devs in tables Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 22/28] blockdev: Fix regression in -drive if=scsi, index=N Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 23/28] blockdev: Make drive_add() take explicit type, index parameters Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 24/28] blockdev: Factor drive_index_to_{bus, unit}_id out of drive_init() Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 25/28] blockdev: New drive_get_by_index() Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 26/28] blockdev: Reject multiple definitions for the same drive Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 27/28] blockdev: Replace drive_add()'s fmt, ... by optstr parameter Kevin Wolf
2011-01-31 15:29 ` [Qemu-devel] [PATCH 28/28] blockdev: Fix drive_add for drives without media Kevin Wolf
2011-01-31 18:01 ` [Qemu-devel] Re: [PULL 00/28] Block patches Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1296487756-12553-15-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).