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