The readahead and xz filters serve as a limited cache, so we want to
advertise caching regardless of what the underlying plugin
provides. But it turns out that we are best serviced by relying on
nbdkit's ability to emulate caching by .pread requests.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
filters/readahead/readahead.c | 13 +++++++++++++
filters/xz/xz.c | 15 ++++++++++++++-
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/filters/readahead/readahead.c b/filters/readahead/readahead.c
index dc27bae..95bda0e 100644
--- a/filters/readahead/readahead.c
+++ b/filters/readahead/readahead.c
@@ -109,6 +109,18 @@ readahead_get_size (struct nbdkit_next_ops *next_ops, void *nxdata,
return r;
}
+/* Cache */
+static int
+readahead_can_cache (struct nbdkit_next_ops *next_ops, void *nxdata,
+ void *handle)
+{
+ /* We are already operating as a cache regardless of the plugin's
+ * underlying .can_cache, but it's easiest to just rely on nbdkit's
+ * behavior of calling .pread for caching.
+ */
+ return NBDKIT_CACHE_EMULATE;
+}
+
/* Read data. */
static int
@@ -241,6 +253,7 @@ static struct nbdkit_filter filter = {
.unload = readahead_unload,
.prepare = readahead_prepare,
.get_size = readahead_get_size,
+ .can_cache = readahead_can_cache,
.pread = readahead_pread,
.pwrite = readahead_pwrite,
.trim = readahead_trim,
diff --git a/filters/xz/xz.c b/filters/xz/xz.c
index 366000b..8ada294 100644
--- a/filters/xz/xz.c
+++ b/filters/xz/xz.c
@@ -1,5 +1,5 @@
/* nbdkit
- * Copyright (C) 2013-2018 Red Hat Inc.
+ * Copyright (C) 2013-2019 Red Hat Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -194,6 +194,18 @@ xz_can_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
return 0;
}
+/* Cache */
+static int
+xz_can_cache (struct nbdkit_next_ops *next_ops, void *nxdata,
+ void *handle)
+{
+ /* We are already operating as a cache regardless of the plugin's
+ * underlying .can_cache, but it's easiest to just rely on nbdkit's
+ * behavior of calling .pread for caching.
+ */
+ return NBDKIT_CACHE_EMULATE;
+}
+
/* Read data from the file. */
static int
xz_pread (struct nbdkit_next_ops *next_ops, void *nxdata,
@@ -248,6 +260,7 @@ static struct nbdkit_filter filter = {
.get_size = xz_get_size,
.can_write = xz_can_write,
.can_extents = xz_can_extents,
+ .can_cache = xz_can_cache,
.pread = xz_pread,
};
--
2.20.1