The blk_writeback function was misnamed, since depending on the cache
mode and flags it could either do a writethrough or a writeback [write
to the cache only].
Since it's really the general "write a block" function, rename it
simply as blk_write. Note there exists a blk_read function already.
There are several related changes in this commit, but it's entirely
refactoring and makes no functional difference.
---
filters/cache/cache.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/filters/cache/cache.c b/filters/cache/cache.c
index e9a5c38..1dc17fd 100644
--- a/filters/cache/cache.c
+++ b/filters/cache/cache.c
@@ -262,8 +262,7 @@ blk_writethrough (struct nbdkit_next_ops *next_ops, void *nxdata,
{
off_t offset = blknum * BLKSIZE;
- nbdkit_debug ("cache: blk_writethrough block %" PRIu64
- " (offset %" PRIu64 ")",
+ nbdkit_debug ("cache: writethrough block %" PRIu64 " (offset %"
PRIu64 ")",
blknum, (uint64_t) offset);
if (pwrite (fd, block, BLKSIZE, offset) == -1) {
@@ -280,11 +279,18 @@ blk_writethrough (struct nbdkit_next_ops *next_ops, void *nxdata,
return 0;
}
-/* Write to the cache only. */
+/* Write a whole block.
+ *
+ * If the cache is in writethrough mode, or the FUA flag is set, then
+ * this calls blk_writethrough above which will write both to the
+ * cache and through to the underlying device.
+ *
+ * Otherwise it will only write to the cache.
+ */
static int
-blk_writeback (struct nbdkit_next_ops *next_ops, void *nxdata,
- uint64_t blknum, const uint8_t *block, uint32_t flags,
- int *err)
+blk_write (struct nbdkit_next_ops *next_ops, void *nxdata,
+ uint64_t blknum, const uint8_t *block, uint32_t flags,
+ int *err)
{
off_t offset;
@@ -294,8 +300,7 @@ blk_writeback (struct nbdkit_next_ops *next_ops, void *nxdata,
offset = blknum * BLKSIZE;
- nbdkit_debug ("cache: blk_writeback block %" PRIu64
- " (offset %" PRIu64 ")",
+ nbdkit_debug ("cache: writeback block %" PRIu64 " (offset %" PRIu64
")",
blknum, (uint64_t) offset);
if (pwrite (fd, block, BLKSIZE, offset) == -1) {
@@ -385,7 +390,7 @@ cache_pwrite (struct nbdkit_next_ops *next_ops, void *nxdata,
return -1;
}
memcpy (&block[blkoffs], buf, n);
- if (blk_writeback (next_ops, nxdata, blknum, block, flags, err) == -1) {
+ if (blk_write (next_ops, nxdata, blknum, block, flags, err) == -1) {
free (block);
return -1;
}
@@ -437,7 +442,7 @@ cache_zero (struct nbdkit_next_ops *next_ops, void *nxdata,
return -1;
}
memset (&block[blkoffs], 0, n);
- if (blk_writeback (next_ops, nxdata, blknum, block, flags, err) == -1) {
+ if (blk_write (next_ops, nxdata, blknum, block, flags, err) == -1) {
free (block);
return -1;
}
--
2.19.2