0a122b
From 2f7835f6ef83931dff5fb37729fd42cd7b431b17 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <2f7835f6ef83931dff5fb37729fd42cd7b431b17.1389014116.git.minovotn@redhat.com>
0a122b
In-Reply-To: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
References: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
From: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Date: Mon, 9 Dec 2013 14:09:07 +0100
0a122b
Subject: [PATCH 19/50] block: introduce bdrv_make_zero
0a122b
0a122b
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Message-id: <1386598178-11845-22-git-send-email-pbonzini@redhat.com>
0a122b
Patchwork-id: 56058
0a122b
O-Subject: [RHEL 7.0 qemu-kvm PATCH 21/52] block: introduce bdrv_make_zero
0a122b
Bugzilla: 1007815
0a122b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
From: Peter Lieven <pl@kamp.de>
0a122b
0a122b
this patch adds a call to completely zero out a block device.
0a122b
the operation is sped up by checking the block status and
0a122b
only writing zeroes to the device if they currently do not
0a122b
return zeroes. optionally the zero writing can be sped up
0a122b
by setting the flag BDRV_REQ_MAY_UNMAP to emulate the zero
0a122b
write by unmapping if the driver supports it.
0a122b
0a122b
Signed-off-by: Peter Lieven <pl@kamp.de>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit d75cbb5e68f5a7d64248ba89399a7f2073231de5)
0a122b
---
0a122b
 block.c               | 37 +++++++++++++++++++++++++++++++++++++
0a122b
 include/block/block.h |  1 +
0a122b
 2 files changed, 38 insertions(+)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 block.c               | 37 +++++++++++++++++++++++++++++++++++++
0a122b
 include/block/block.h |  1 +
0a122b
 2 files changed, 38 insertions(+)
0a122b
0a122b
diff --git a/block.c b/block.c
0a122b
index 23dbafd..2e77763 100644
0a122b
--- a/block.c
0a122b
+++ b/block.c
0a122b
@@ -2412,6 +2412,43 @@ int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
0a122b
                       BDRV_REQ_ZERO_WRITE | flags);
0a122b
 }
0a122b
 
0a122b
+/*
0a122b
+ * Completely zero out a block device with the help of bdrv_write_zeroes.
0a122b
+ * The operation is sped up by checking the block status and only writing
0a122b
+ * zeroes to the device if they currently do not return zeroes. Optional
0a122b
+ * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
0a122b
+ *
0a122b
+ * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
0a122b
+ */
0a122b
+int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)
0a122b
+{
0a122b
+    int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
0a122b
+    int64_t ret, nb_sectors, sector_num = 0;
0a122b
+    int n;
0a122b
+
0a122b
+    for (;;) {
0a122b
+        nb_sectors = target_size - sector_num;
0a122b
+        if (nb_sectors <= 0) {
0a122b
+            return 0;
0a122b
+        }
0a122b
+        if (nb_sectors > INT_MAX) {
0a122b
+            nb_sectors = INT_MAX;
0a122b
+        }
0a122b
+        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);
0a122b
+        if (ret & BDRV_BLOCK_ZERO) {
0a122b
+            sector_num += n;
0a122b
+            continue;
0a122b
+        }
0a122b
+        ret = bdrv_write_zeroes(bs, sector_num, n, flags);
0a122b
+        if (ret < 0) {
0a122b
+            error_report("error writing zeroes at sector %" PRId64 ": %s",
0a122b
+                         sector_num, strerror(-ret));
0a122b
+            return ret;
0a122b
+        }
0a122b
+        sector_num += n;
0a122b
+    }
0a122b
+}
0a122b
+
0a122b
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
0a122b
                void *buf, int count1)
0a122b
 {
0a122b
diff --git a/include/block/block.h b/include/block/block.h
0a122b
index 1958d98..b87ed3a 100644
0a122b
--- a/include/block/block.h
0a122b
+++ b/include/block/block.h
0a122b
@@ -218,6 +218,7 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
0a122b
                const uint8_t *buf, int nb_sectors);
0a122b
 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
0a122b
                int nb_sectors, BdrvRequestFlags flags);
0a122b
+int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags);
0a122b
 int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov);
0a122b
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
0a122b
                void *buf, int count);
0a122b
-- 
0a122b
1.7.11.7
0a122b