diff mbox series

[3/3] nbd: Add .bdrv_known_zeroes() server support

Message ID 20200210214109.751734-4-eblake@redhat.com (mailing list archive)
State New, archived
Headers show
Series NBD_INFO_INIT_STATE extension | expand

Commit Message

Eric Blake Feb. 10, 2020, 9:41 p.m. UTC
Using the new NBD extension of NBD_INFO_INIT_STATE, we can advertise
at least the NBD_INIT_ZERO bit based on what the block layer already
knows.  Advertising NBD_INIT_SPARSE might also be possible by
inspecting blk_probe_blocksizes, but as the block layer does not
consume that information at present, the effort of advertising it for
a third party is less important.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block/block-backend.c          |  9 +++++++++
 include/sysemu/block-backend.h |  1 +
 nbd/server.c                   | 11 +++++++++++
 3 files changed, 21 insertions(+)
diff mbox series

Patch

diff --git a/block/block-backend.c b/block/block-backend.c
index 8b8f2a80a0d5..d7e01f2e67de 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -2127,6 +2127,15 @@  int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
     return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
 }

+int blk_known_zeroes(BlockBackend *blk)
+{
+    if (!blk_is_available(blk)) {
+        return 0;
+    }
+
+    return bdrv_known_zeroes(blk_bs(blk));
+}
+
 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
 {
     if (!blk_is_available(blk)) {
diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h
index b198deca0b24..2a9b750bb775 100644
--- a/include/sysemu/block-backend.h
+++ b/include/sysemu/block-backend.h
@@ -245,6 +245,7 @@  int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size);
 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz);
 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo);
+int blk_known_zeroes(BlockBackend *blk);
 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
                                   BlockCompletionFunc *cb,
                                   void *opaque, int ret);
diff --git a/nbd/server.c b/nbd/server.c
index 11a31094ff83..f6bb7d944daa 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -661,6 +661,17 @@  static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
         return rc;
     }

+    /* Send NBD_INFO_INIT_STATE always */
+    trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
+    /* Is it worth using blk_probe_blocksizes for setting NBD_INIT_SPARSE? */
+    stw_be_p(buf, ((blk_known_zeroes(exp->blk) & BDRV_ZERO_OPEN)
+                   ? NBD_INIT_ZERO : 0));
+    rc = nbd_negotiate_send_info(client, NBD_INFO_INIT_STATE,
+                                 sizeof(uint16_t), buf, errp);
+    if (rc < 0) {
+        return rc;
+    }
+
     /*
      * If the client is just asking for NBD_OPT_INFO, but forgot to
      * request block sizes in a situation that would impact