diff mbox

[v2,2/4] block/null-{co, aio}: Allow reading zeroes

Message ID 1458858840-3859-3-git-send-email-mreitz@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Max Reitz March 24, 2016, 10:33 p.m. UTC
This is optional so that it does not impede the null block driver's
performance unless this behavior is desired.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/null.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Comments

Eric Blake March 24, 2016, 10:55 p.m. UTC | #1
On 03/24/2016 04:33 PM, Max Reitz wrote:
> This is optional so that it does not impede the null block driver's
> performance unless this behavior is desired.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/null.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>
Fam Zheng March 25, 2016, 2:01 a.m. UTC | #2
On Thu, 03/24 23:33, Max Reitz wrote:
> This is optional so that it does not impede the null block driver's
> performance unless this behavior is desired.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/null.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/block/null.c b/block/null.c
> index d90165d..a7df386 100644
> --- a/block/null.c
> +++ b/block/null.c
> @@ -14,10 +14,12 @@
>  #include "block/block_int.h"
>  
>  #define NULL_OPT_LATENCY "latency-ns"
> +#define NULL_OPT_ZEROES  "read-zeroes"
>  
>  typedef struct {
>      int64_t length;
>      int64_t latency_ns;
> +    bool read_zeroes;
>  } BDRVNullState;
>  
>  static QemuOptsList runtime_opts = {
> @@ -40,6 +42,11 @@ static QemuOptsList runtime_opts = {
>              .help = "nanoseconds (approximated) to wait "
>                      "before completing request",
>          },
> +        {
> +            .name = NULL_OPT_ZEROES,
> +            .type = QEMU_OPT_BOOL,
> +            .help = "return zeroes when read",
> +        },
>          { /* end of list */ }
>      },
>  };
> @@ -61,6 +68,7 @@ static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
>          error_setg(errp, "latency-ns is invalid");
>          ret = -EINVAL;
>      }
> +    s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
>      qemu_opts_del(opts);
>      return ret;
>  }
> @@ -90,6 +98,12 @@ static coroutine_fn int null_co_readv(BlockDriverState *bs,
>                                        int64_t sector_num, int nb_sectors,
>                                        QEMUIOVector *qiov)
>  {
> +    BDRVNullState *s = bs->opaque;
> +
> +    if (s->read_zeroes) {
> +        qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
> +    }
> +
>      return null_co_common(bs);
>  }
>  
> @@ -159,6 +173,12 @@ static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
>                                    BlockCompletionFunc *cb,
>                                    void *opaque)
>  {
> +    BDRVNullState *s = bs->opaque;
> +
> +    if (s->read_zeroes) {
> +        qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
> +    }
> +
>      return null_aio_common(bs, cb, opaque);
>  }
>  
> -- 
> 2.7.4
> 

Acked-by: Fam Zheng <famz@redhat.com>
diff mbox

Patch

diff --git a/block/null.c b/block/null.c
index d90165d..a7df386 100644
--- a/block/null.c
+++ b/block/null.c
@@ -14,10 +14,12 @@ 
 #include "block/block_int.h"
 
 #define NULL_OPT_LATENCY "latency-ns"
+#define NULL_OPT_ZEROES  "read-zeroes"
 
 typedef struct {
     int64_t length;
     int64_t latency_ns;
+    bool read_zeroes;
 } BDRVNullState;
 
 static QemuOptsList runtime_opts = {
@@ -40,6 +42,11 @@  static QemuOptsList runtime_opts = {
             .help = "nanoseconds (approximated) to wait "
                     "before completing request",
         },
+        {
+            .name = NULL_OPT_ZEROES,
+            .type = QEMU_OPT_BOOL,
+            .help = "return zeroes when read",
+        },
         { /* end of list */ }
     },
 };
@@ -61,6 +68,7 @@  static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
         error_setg(errp, "latency-ns is invalid");
         ret = -EINVAL;
     }
+    s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
     qemu_opts_del(opts);
     return ret;
 }
@@ -90,6 +98,12 @@  static coroutine_fn int null_co_readv(BlockDriverState *bs,
                                       int64_t sector_num, int nb_sectors,
                                       QEMUIOVector *qiov)
 {
+    BDRVNullState *s = bs->opaque;
+
+    if (s->read_zeroes) {
+        qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
+    }
+
     return null_co_common(bs);
 }
 
@@ -159,6 +173,12 @@  static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
                                   BlockCompletionFunc *cb,
                                   void *opaque)
 {
+    BDRVNullState *s = bs->opaque;
+
+    if (s->read_zeroes) {
+        qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
+    }
+
     return null_aio_common(bs, cb, opaque);
 }