[Libguestfs] [PATCH] file: Fix zero/trim with block device

Nir Soffer nirsof at gmail.com
Fri Jul 27 21:03:04 UTC 2018


When using block device on RHEL 7.5, file plugin fails to zero with this
error (copied from strace):

[pid 39551] fallocate(8, FALLOC_FL_ZERO_RANGE, 1536, 64000) = -1 ENODEV (No such device)

This is expected error according to the manual:

ENODEV fd does not refer to a regular file or a directory.  (If fd is a
pipe or FIFO, a different error results.)

Treat this error as EOPNOSUPP.

Tested only on Fedora 28; I don't know how to build nbdkit on RHEL, but
the change is pretty trivial.
---
 plugins/file/file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/plugins/file/file.c b/plugins/file/file.c
index b6e33de..a7c07fb 100644
--- a/plugins/file/file.c
+++ b/plugins/file/file.c
@@ -243,7 +243,7 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
   if (may_trim) {
     r = fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 		   offset, count);
-    if (r == -1 && errno != EOPNOTSUPP) {
+    if (r == -1 && errno != EOPNOTSUPP && errno != ENODEV) {
       nbdkit_error ("zero: %m");
     }
     /* PUNCH_HOLE is older; if it is not supported, it is likely that
@@ -254,7 +254,7 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
 
 #ifdef FALLOC_FL_ZERO_RANGE
   r = fallocate (h->fd, FALLOC_FL_ZERO_RANGE, offset, count);
-  if (r == -1 && errno != EOPNOTSUPP) {
+  if (r == -1 && errno != EOPNOTSUPP && errno != ENODEV) {
     nbdkit_error ("zero: %m");
   }
 #else
@@ -288,11 +288,11 @@ file_trim (void *handle, uint32_t count, uint64_t offset)
   struct handle *h = handle;
 
   /* Trim is advisory; we don't care if it fails for anything other
-   * than EIO or EPERM. */
+   * than EIO, EPERM, or ENODEV (kernel 3.10) */
   r = fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
                  offset, count);
   if (r < 0) {
-    if (errno != EPERM && errno != EIO) {
+    if (errno != EPERM && errno != EIO && errno != ENODEV) {
       nbdkit_debug ("ignoring failed fallocate during trim: %m");
       r = 0;
     }
-- 
2.17.1




More information about the Libguestfs mailing list