qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Lieven <pl@kamp.de>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, pbonzini@redhat.com, Peter Lieven <pl@kamp.de>,
	ronniesahlberg@gmail.com, stefanha@redhat.com
Subject: [Qemu-devel] [PATCH 4/4] qemu-img: conditionally discard target on convert
Date: Mon, 15 Jul 2013 12:49:35 +0200	[thread overview]
Message-ID: <1373885375-13601-5-git-send-email-pl@kamp.de> (raw)
In-Reply-To: <1373885375-13601-1-git-send-email-pl@kamp.de>

if a destination has has_zero_init = 0, but it supports
discard zeroes use discard to convert the target
into an all zero device.

Signed-off-by: Peter Lieven <pl@kamp.de>
---
 qemu-img.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/qemu-img.c b/qemu-img.c
index f8c97d3..cbde02f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1342,7 +1342,8 @@ static int img_convert(int argc, char **argv)
         goto out;
     }
 
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;
+
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
@@ -1442,8 +1443,61 @@ static int img_convert(int argc, char **argv)
         /* signal EOF to align */
         bdrv_write_compressed(out_bs, 0, NULL, 0);
     } else {
+        BlockDriverInfo bdi;
+
         int has_zero_init = bdrv_has_zero_init(out_bs);
 
+        /* if the target has no zero init by default check if we
+         * can discard blocks to zeroize the device */
+        if (!has_zero_init && !out_baseimg &&
+            bdrv_get_info(out_bs, &bdi) == 0 &&
+            bdi.discard_zeroes && bdi.max_unmap > 0) {
+            int64_t target_size = bdrv_getlength(out_bs) / BDRV_SECTOR_SIZE;
+            int64_t sector_num2 = -1;
+            int n;
+            sector_num = 0;
+            for (;;) {
+                nb_sectors = target_size - sector_num;
+                if (nb_sectors <= 0) {
+                    has_zero_init = 1;
+                    break;
+                }
+                if (nb_sectors > INT_MAX) {
+                    nb_sectors = INT_MAX;
+                }
+                if (!bdrv_is_allocated(out_bs, sector_num, nb_sectors, &n)) {
+                    if (!n) {
+                        /* an error occured, continue with has_zero_init = 0 */
+                        break;
+                    }
+                    sector_num += n;
+                    continue;
+                }
+                if (sector_num == sector_num2) {
+                    /* some drivers allow a discard to silently fail.
+                     * double-check that we do not try to discard the
+                     * same sectors twice. if thats the case
+                     * continue with has_zero_init = 0 */
+                    break;
+                }
+                sector_num2 = sector_num;
+                while (n > 0) {
+                    nb_sectors = n;
+                    if (nb_sectors > bdi.max_unmap) {
+                        nb_sectors = bdi.max_unmap;
+                    }
+                    ret = bdrv_discard(out_bs, sector_num2, nb_sectors);
+                    if (ret < 0) {
+                        error_report("error while discarding at sector %" PRId64 ": %s",
+                                     sector_num, strerror(-ret));
+                        goto out;
+                    }
+                    sector_num2 += nb_sectors;
+                    n -= nb_sectors;;
+                }
+            }
+        }
+
         sector_num = 0; // total number of sectors converted so far
         nb_sectors = total_sectors - sector_num;
         if (nb_sectors != 0) {
-- 
1.7.9.5

  parent reply	other threads:[~2013-07-15 10:50 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-15 10:49 [Qemu-devel] [PATCH 0/4] qemu-img: conditionally discard target on convert Peter Lieven
2013-07-15 10:49 ` [Qemu-devel] [PATCH 1/4] block: add discard_zeroes and max_unmap to BlockDriverInfo Peter Lieven
2013-07-15 10:49 ` [Qemu-devel] [PATCH 2/4] iscsi: add .bdrv_get_info Peter Lieven
2013-07-15 10:49 ` [Qemu-devel] [PATCH 3/4] block/raw: " Peter Lieven
2013-07-19  5:12   ` Stefan Hajnoczi
2013-07-15 10:49 ` Peter Lieven [this message]
2013-07-17  8:46   ` [Qemu-devel] [PATCH 4/4] qemu-img: conditionally discard target on convert Kevin Wolf
2013-07-17  9:58     ` Paolo Bonzini
2013-07-17 10:21       ` Peter Lieven
2013-07-17 10:27         ` Kevin Wolf
2013-07-17 10:31           ` Peter Lieven
2013-07-17 10:47           ` Paolo Bonzini
2013-07-17 14:19             ` Peter Lieven
2013-07-17 10:45         ` Paolo Bonzini
2013-07-17 14:21           ` Peter Lieven
2013-07-17 14:26             ` Paolo Bonzini
2013-07-17 10:25       ` Kevin Wolf
2013-07-17 15:54         ` ronnie sahlberg
2013-07-17 16:27           ` Paolo Bonzini
2013-07-17 16:31             ` ronnie sahlberg
2013-07-17 17:02               ` Peter Lieven
2013-07-17 17:04                 ` Paolo Bonzini
2013-07-17 17:48                   ` Peter Lieven
2013-07-17 20:00                     ` Paolo Bonzini
2013-07-18  9:23                     ` Kevin Wolf
2013-07-18 10:24                       ` Paolo Bonzini
2013-07-18 10:42                         ` Kevin Wolf
2013-07-18 10:44                         ` Peter Lieven
2013-07-18 10:56                           ` Paolo Bonzini
2013-07-18 11:04                             ` Peter Lieven
2013-07-18 12:31                               ` Paolo Bonzini
2013-07-18 13:29                                 ` Peter Lieven
2013-07-18 13:52                                   ` Paolo Bonzini
2013-07-18 14:09                                     ` Peter Lieven
2013-07-18 14:20                                       ` Paolo Bonzini
2013-07-18 14:32                                         ` Peter Lieven
2013-07-18 14:35                                           ` Paolo Bonzini
2013-07-18 18:43                                             ` Peter Lieven
2013-07-18 18:54                                               ` ronnie sahlberg
2013-07-18 19:28                                                 ` Peter Lieven
2013-07-19  5:58                                                   ` Paolo Bonzini
2013-07-19  6:08                                                     ` Peter Lieven
2013-07-19 13:25                                                       ` ronnie sahlberg
2013-07-19 13:49                                                         ` Peter Lieven
2013-07-19 14:00                                                           ` ronnie sahlberg
2013-07-19 14:02                                                             ` Peter Lieven
2013-07-19 13:58                                               ` ronnie sahlberg
2013-07-18 13:55                                   ` ronnie sahlberg
2013-07-18 14:14                                     ` Paolo Bonzini
2013-09-12  8:52                                       ` Peter Lieven
2013-09-12  8:55                                         ` Paolo Bonzini
2013-09-12  9:03                                           ` Peter Lieven
2013-07-17 17:09                 ` ronnie sahlberg
2013-07-18  9:21           ` Kevin Wolf
2013-07-17 10:22     ` Peter Lieven
2013-07-16 10:55 ` [Qemu-devel] [PATCH 0/4] " Paolo Bonzini
2013-07-16 11:18   ` Peter Lieven
2013-07-16 11:27     ` Paolo Bonzini
2013-07-16 11:40       ` Peter Lieven
2013-07-16 11:55         ` Paolo Bonzini
2013-07-17 10:23           ` Peter Lieven
2013-07-17 10:28             ` Paolo Bonzini
2013-07-17 10:40               ` Peter Lieven
2013-07-17 10:50                 ` Paolo Bonzini
2013-07-17 14:18                   ` Peter Lieven
2013-07-17 14:26                     ` Paolo Bonzini
2013-07-17 14:46                       ` Peter Lieven
2013-07-17 14:53                         ` Paolo Bonzini
2013-07-17 15:12                           ` Kevin Wolf
2013-07-17 16:53                             ` Peter Lieven
2013-07-17 17:01                           ` Peter Lieven
2013-07-19  5:13 ` Stefan Hajnoczi

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=1373885375-13601-5-git-send-email-pl@kamp.de \
    --to=pl@kamp.de \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=stefanha@redhat.com \
    /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).