[Libguestfs] [nbdkit PATCH v2 05/24] plugins: Implement no-op .cache for in-memory plugins

Eric Blake eblake at redhat.com
Thu May 16 03:57:55 UTC 2019


For our plugins which have no backing file but generate everything on
the fly or store things in memory, there's really nothing to speed up,
and also no reason why we can't advertise caching to the client.
Implement .can_cache but not .cache to get nbdkit to do the work on
our behalf.

Full list of plugins changed:
data, full, memory, null, pattern, random, zero

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 plugins/data/data.c       | 11 +++++++++++
 plugins/full/full.c       | 13 ++++++++++++-
 plugins/memory/memory.c   | 11 +++++++++++
 plugins/null/null.c       | 13 ++++++++++++-
 plugins/pattern/pattern.c | 13 ++++++++++++-
 plugins/random/random.c   | 13 ++++++++++++-
 plugins/zero/zero.c       | 13 ++++++++++++-
 7 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/plugins/data/data.c b/plugins/data/data.c
index 55380c6..b0e08cb 100644
--- a/plugins/data/data.c
+++ b/plugins/data/data.c
@@ -333,6 +333,16 @@ data_can_multi_conn (void *handle)
   return 1;
 }

+/* Cache. */
+static int
+data_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 data_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
@@ -389,6 +399,7 @@ static struct nbdkit_plugin plugin = {
   .open              = data_open,
   .get_size          = data_get_size,
   .can_multi_conn    = data_can_multi_conn,
+  .can_cache         = data_can_cache,
   .pread             = data_pread,
   .pwrite            = data_pwrite,
   .zero              = data_zero,
diff --git a/plugins/full/full.c b/plugins/full/full.c
index 7661856..65b8259 100644
--- a/plugins/full/full.c
+++ b/plugins/full/full.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -94,6 +94,16 @@ full_get_size (void *handle)
   return size;
 }

+/* Cache. */
+static int
+full_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 full_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -151,6 +161,7 @@ static struct nbdkit_plugin plugin = {
   .magic_config_key  = "size",
   .open              = full_open,
   .get_size          = full_get_size,
+  .can_cache         = full_can_cache,
   .pread             = full_pread,
   .pwrite            = full_pwrite,
   .zero              = full_zero,
diff --git a/plugins/memory/memory.c b/plugins/memory/memory.c
index 90fa99e..234d414 100644
--- a/plugins/memory/memory.c
+++ b/plugins/memory/memory.c
@@ -128,6 +128,16 @@ memory_can_multi_conn (void *handle)
   return 1;
 }

+/* Cache. */
+static int
+memory_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 memory_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
@@ -184,6 +194,7 @@ static struct nbdkit_plugin plugin = {
   .open              = memory_open,
   .get_size          = memory_get_size,
   .can_multi_conn    = memory_can_multi_conn,
+  .can_cache         = memory_can_cache,
   .pread             = memory_pread,
   .pwrite            = memory_pwrite,
   .zero              = memory_zero,
diff --git a/plugins/null/null.c b/plugins/null/null.c
index 518b63b..5af7ab9 100644
--- a/plugins/null/null.c
+++ b/plugins/null/null.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -83,6 +83,16 @@ null_get_size (void *handle)
   return size;
 }

+/* Cache. */
+static int
+null_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 null_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -148,6 +158,7 @@ static struct nbdkit_plugin plugin = {
   .magic_config_key  = "size",
   .open              = null_open,
   .get_size          = null_get_size,
+  .can_cache         = null_can_cache,
   .pread             = null_pread,
   .pwrite            = null_pwrite,
   .zero              = null_zero,
diff --git a/plugins/pattern/pattern.c b/plugins/pattern/pattern.c
index 115bd96..6b9b3a0 100644
--- a/plugins/pattern/pattern.c
+++ b/plugins/pattern/pattern.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -94,6 +94,16 @@ pattern_can_multi_conn (void *handle)
   return 1;
 }

+/* Cache. */
+static int
+pattern_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 pattern_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -126,6 +136,7 @@ static struct nbdkit_plugin plugin = {
   .open              = pattern_open,
   .get_size          = pattern_get_size,
   .can_multi_conn    = pattern_can_multi_conn,
+  .can_cache         = pattern_can_cache,
   .pread             = pattern_pread,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
diff --git a/plugins/random/random.c b/plugins/random/random.c
index 7fb42c8..6377310 100644
--- a/plugins/random/random.c
+++ b/plugins/random/random.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -113,6 +113,16 @@ random_can_multi_conn (void *handle)
   return 1;
 }

+/* Cache. */
+static int
+random_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Read data. */
 static int
 random_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -156,6 +166,7 @@ static struct nbdkit_plugin plugin = {
   .open              = random_open,
   .get_size          = random_get_size,
   .can_multi_conn    = random_can_multi_conn,
+  .can_cache         = random_can_cache,
   .pread             = random_pread,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
diff --git a/plugins/zero/zero.c b/plugins/zero/zero.c
index 49ce08e..12dcd6a 100644
--- a/plugins/zero/zero.c
+++ b/plugins/zero/zero.c
@@ -1,5 +1,5 @@
 /* nbdkit
- * Copyright (C) 2017-2018 Red Hat Inc.
+ * Copyright (C) 2017-2019 Red Hat Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -67,6 +67,16 @@ zero_get_size (void *handle)
   return 0;
 }

+/* Cache. */
+static int
+zero_can_cache (void *handle)
+{
+  /* Everything is already in memory, returning this without
+   * implementing .cache lets nbdkit do the correct no-op.
+   */
+  return NBDKIT_CACHE_NATIVE;
+}
+
 /* Ideally the read plugin would be optional. */
 static int
 zero_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
@@ -82,6 +92,7 @@ static struct nbdkit_plugin plugin = {
   .config            = zero_config,
   .open              = zero_open,
   .get_size          = zero_get_size,
+  .can_cache         = zero_can_cache,
   .pread             = zero_pread,
   /* In this plugin, errno is preserved properly along error return
    * paths from failed system calls.
-- 
2.20.1




More information about the Libguestfs mailing list