The same as qemu's copyonread flag, this caches read requests.
---
filters/cache/nbdkit-cache-filter.pod | 11 +++++
filters/cache/cache.c | 37 +++++++++++++--
tests/Makefile.am | 4 +-
tests/test-cache-on-read.sh | 66 +++++++++++++++++++++++++++
4 files changed, 114 insertions(+), 4 deletions(-)
diff --git a/filters/cache/nbdkit-cache-filter.pod
b/filters/cache/nbdkit-cache-filter.pod
index 31e533d..bfbada0 100644
--- a/filters/cache/nbdkit-cache-filter.pod
+++ b/filters/cache/nbdkit-cache-filter.pod
@@ -5,6 +5,7 @@ nbdkit-cache-filter - nbdkit caching filter
=head1 SYNOPSIS
nbdkit --filter=cache plugin [cache=writeback|writethrough|unsafe]
+ [cache-on-read=true|false]
[plugin-args...]
=head1 DESCRIPTION
@@ -50,6 +51,16 @@ This is dangerous and can cause data loss, but this may be acceptable
if you only use it for testing or with data that you don't care about
or can cheaply reconstruct.
+=item B<cache-on-read=true>
+
+Cache read requests as well as write requests. Any time a block is
+read from the plugin, it is saved in the cache (if there is sufficient
+space) so the same data can be served more quickly later.
+
+=item B<cache-on-read=false>
+
+Do not cache read requests (this is the default).
+
=back
=head1 ENVIRONMENT VARIABLES
diff --git a/filters/cache/cache.c b/filters/cache/cache.c
index 1dc17fd..b1c3d53 100644
--- a/filters/cache/cache.c
+++ b/filters/cache/cache.c
@@ -90,6 +90,9 @@ static enum cache_mode {
CACHE_MODE_UNSAFE,
} cache_mode = CACHE_MODE_WRITEBACK;
+/* Cache read requests. */
+static bool cache_on_read = false;
+
static int cache_flush (struct nbdkit_next_ops *next_ops, void *nxdata, void *handle,
uint32_t flags, int *err);
static void
@@ -156,6 +159,15 @@ cache_config (nbdkit_next_config *next, void *nxdata,
return -1;
}
}
+ else if (strcmp (key, "cache-on-read") == 0) {
+ int r;
+
+ r = nbdkit_parse_bool (value);
+ if (r == -1)
+ return -1;
+ cache_on_read = r;
+ return 0;
+ }
else {
return next (nxdata, key, value);
}
@@ -242,9 +254,28 @@ blk_read (struct nbdkit_next_ops *next_ops, void *nxdata,
state == BLOCK_DIRTY ? "dirty" :
"unknown");
- if (state == BLOCK_NOT_CACHED) /* Read underlying plugin. */
- return next_ops->pread (nxdata, block, BLKSIZE, offset, 0, err);
- else { /* Read cache. */
+ if (state == BLOCK_NOT_CACHED) { /* Read underlying plugin. */
+ if (next_ops->pread (nxdata, block, BLKSIZE, offset, 0, err) == -1)
+ return -1;
+
+ /* If cache-on-read, copy the block to the cache. */
+ if (cache_on_read) {
+ off_t offset = blknum * BLKSIZE;
+
+ nbdkit_debug ("cache: cache-on-read block %" PRIu64
+ " (offset %" PRIu64 ")",
+ blknum, (uint64_t) offset);
+
+ if (pwrite (fd, block, BLKSIZE, offset) == -1) {
+ *err = errno;
+ nbdkit_error ("pwrite: %m");
+ return -1;
+ }
+ bitmap_set_blk (&bm, blknum, BLOCK_CLEAN);
+ }
+ return 0;
+ }
+ else { /* Read cache. */
if (pread (fd, block, BLKSIZE, offset) == -1) {
*err = errno;
nbdkit_error ("pread: %m");
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 79b723a..ccd5ff5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -702,7 +702,9 @@ TESTS += test-blocksize.sh
# cache filter test.
if HAVE_GUESTFISH
-TESTS += test-cache.sh
+TESTS += \
+ test-cache.sh \
+ test-cache-on-read.sh
endif HAVE_GUESTFISH
# cow filter test.
diff --git a/tests/test-cache-on-read.sh b/tests/test-cache-on-read.sh
new file mode 100755
index 0000000..51764a2
--- /dev/null
+++ b/tests/test-cache-on-read.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+# nbdkit
+# Copyright (C) 2018 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+source ./functions.sh
+set -e
+set -x
+
+files="cache-on-read.img cache-on-read.sock cache-on-read.pid"
+rm -f $files
+cleanup_fn rm -f $files
+
+# Create an empty base image.
+truncate -s 1G cache-on-read.img
+
+# Run nbdkit with the caching filter and cache-on-read set.
+start_nbdkit -P cache-on-read.pid -U cache-on-read.sock \
+ --filter=cache \
+ file cache-on-read.img \
+ cache-on-read=true
+
+# Open the overlay and perform some operations.
+guestfish --format=raw -a "nbd://?socket=$PWD/cache-on-read.sock"
<<'EOF'
+ run
+ part-disk /dev/sda gpt
+ mkfs ext4 /dev/sda1
+ mount /dev/sda1 /
+ fill-dir / 10000
+ fill-pattern "abcde" 5M /large
+ write /hello "hello, world"
+EOF
+
+# Check the last files we created exist.
+guestfish --ro -a cache-on-read.img -m /dev/sda1 <<'EOF'
+ cat /hello
+ cat /large | cat >/dev/null
+EOF
--
2.19.2