[Libguestfs] [nbdkit PATCH 1/2] blocksize: Lift restriction against 0-size file

Eric Blake
2019-05-16T02:27:39Z

When the blocksize filter was first written, nbdkit claimed that
0-size files were not supported. Later, we added the zero plugin and
ensured that a 0-size file works, so the restrictions against rounding
down to zero size are no longer necessary.  The next patch will
document how to access the trailing bytes after all.

While at it, utilize the ROUND_DOWN() macro rather than open-coding
things, as that macro also post-dated the filter's initial
implementation.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 filters/blocksize/nbdkit-blocksize-filter.pod |  3 +--
 filters/blocksize/blocksize.c                 | 23 ++++---------------
 2 files changed, 6 insertions(+), 20 deletions(-)

diff --git a/filters/blocksize/nbdkit-blocksize-filter.pod
b/filters/blocksize/nbdkit-blocksize-filter.pod
index 0abed2f..3b79e28 100644
--- a/filters/blocksize/nbdkit-blocksize-filter.pod
+++ b/filters/blocksize/nbdkit-blocksize-filter.pod
@@ -38,8 +38,7 @@ read requests to alignment boundaries, performs
read-modify-write
 cycles for any unaligned head or tail of a write or zero request, and
 silently ignores any unaligned head or tail of a trim request.  The
 filter also truncates the plugin size down to an aligned value (as it
-cannot safely operate on the unaligned tail); it is an error if this
-would result in a size of 0.
+cannot safely operate on the unaligned tail).

 This parameter understands the suffix 'k' for 1024.

diff --git a/filters/blocksize/blocksize.c b/filters/blocksize/blocksize.c
index 47bea96..ba5d9e7 100644
--- a/filters/blocksize/blocksize.c
+++ b/filters/blocksize/blocksize.c
@@ -138,30 +138,18 @@ blocksize_config_complete (nbdkit_next_config_complete
*next, void *nxdata)
   "maxdata=<SIZE>       Maximum size for read/write (default
64M).\n" \
   "maxlen=<SIZE>        Maximum size for trim/zero (default
4G-minblock)."

-static int
-blocksize_prepare (struct nbdkit_next_ops *next_ops, void *nxdata,
-                   void *handle)
-{
-  /* Early call to get_size to ensure it doesn't truncate to 0. */
-  int64_t size = next_ops->get_size (nxdata);
-
-  if (size == -1)
-    return -1;
-  if (size < minblock) {
-    nbdkit_error ("disk is too small for minblock size %u",
minblock);
-    return -1;
-  }
-  /* TODO: cache per-connection FUA mode? */
-  return 0;
-}
+/* TODO: Should we have a .prepare to cache per-connection FUA mode? */

+/* Round size down to avoid issues at end of file. */
 static int64_t
 blocksize_get_size (struct nbdkit_next_ops *next_ops, void *nxdata,
                     void *handle)
 {
   int64_t size = next_ops->get_size (nxdata);

-  return size == -1 ? size : size & ~(minblock - 1);
+  if (size == -1)
+    return -1;
+  return ROUND_DOWN (size, minblock);
 }

 static int
@@ -387,7 +375,6 @@ static struct nbdkit_filter filter = {
   .config            = blocksize_config,
   .config_complete   = blocksize_config_complete,
   .config_help       = blocksize_config_help,
-  .prepare           = blocksize_prepare,
   .get_size          = blocksize_get_size,
   .can_multi_conn    = blocksize_can_multi_conn,
   .pread             = blocksize_pread,
--
2.20.1