The NBD protocol is adding an extension to let servers advertise
initialization state to the client: whether the image contains holes,
and whether it is known to read as all zeroes.  With recent enough
libnbd, the nbd plugin can pass this information through.

Signed-off-by: Eric Blake <ebl...@redhat.com>
---
 plugins/nbd/nbd.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/plugins/nbd/nbd.c b/plugins/nbd/nbd.c
index d020bee..8f06420 100644
--- a/plugins/nbd/nbd.c
+++ b/plugins/nbd/nbd.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2019 Red Hat Inc.
+ * Copyright (C) 2017-2020 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -702,6 +702,42 @@ nbdplug_can_extents (void *handle)
   return i;
 }

+static int
+nbdplug_init_sparse (void *handle)
+{
+  struct handle *h = handle;
+  int i = 0;
+
+#if LIBNBD_HAVE_NBD_GET_INIT_FLAGS
+  i = nbd_get_init_flags (h->nbd);
+
+  if (i == -1) {
+    nbdkit_error ("failure to check init flags: %s", nbd_get_error ());
+    return -1;
+  }
+  i = !!(i & LIBNBD_INIT_SPARSE);
+#endif
+  return i;
+}
+
+static int
+nbdplug_init_zero (void *handle)
+{
+  struct handle *h = handle;
+  int i = 0;
+
+#if LIBNBD_HAVE_NBD_GET_INIT_FLAGS
+  i = nbd_get_init_flags (h->nbd);
+
+  if (i == -1) {
+    nbdkit_error ("failure to check init flags: %s", nbd_get_error ());
+    return -1;
+  }
+  i = !!(i & LIBNBD_INIT_ZERO);
+#endif
+  return i;
+}
+
 /* Read data from the file. */
 static int
 nbdplug_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -860,6 +896,8 @@ static struct nbdkit_plugin plugin = {
   .can_multi_conn     = nbdplug_can_multi_conn,
   .can_extents        = nbdplug_can_extents,
   .can_cache          = nbdplug_can_cache,
+  .init_sparse        = nbdplug_init_sparse,
+  .init_zero          = nbdplug_init_zero,
   .pread              = nbdplug_pread,
   .pwrite             = nbdplug_pwrite,
   .zero               = nbdplug_zero,
-- 
2.24.1

_______________________________________________
Libguestfs mailing list
Libguestfs@redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs

Reply via email to