<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jul 30, 2018 at 8:11 PM Nir Soffer <<a href="mailto:nirsof@gmail.com">nirsof@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Fix issues Eric found in the original patch:<br>
<a href="https://www.redhat.com/archives/libguestfs/2018-July/msg00072.html" rel="noreferrer" target="_blank">https://www.redhat.com/archives/libguestfs/2018-July/msg00072.html</a><br>
<br>
- When handling ENODEV, the caller is expecting EOPNOTSUPP to trigger<br>
  fallback.<br>
- ENODEV should be ignored in file_trim.<br>
<br>
Tested only on Fedora 28 and RHEL 7.5.<br>
---<br></blockquote><div><br></div><div>I forgot to mention that v2 only fixes a typo on v1, that was here:</div><div><a href="https://www.redhat.com/archives/libguestfs/2018-July/msg00077.html">https://www.redhat.com/archives/libguestfs/2018-July/msg00077.html</a><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 plugins/file/file.c | 33 ++++++++++++++++++++++++---------<br>
 1 file changed, 24 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/plugins/file/file.c b/plugins/file/file.c<br>
index a7c07fb..a8a6253 100644<br>
--- a/plugins/file/file.c<br>
+++ b/plugins/file/file.c<br>
@@ -50,6 +50,21 @@<br>
<br>
 static char *filename = NULL;<br>
<br>
+#if defined(FALLOC_FL_PUNCH_HOLE) || defined(FALLOC_FL_ZERO_RANGE)<br>
+static int<br>
+do_fallocate(int fd, int mode, off_t offset, off_t len)<br>
+{<br>
+  int r = -1;<br>
+  r = fallocate (fd, mode, offset, len);<br>
+  /* kernel 3.10 fails with ENODEV for block device. Kernel >= 4.9 fails<br>
+     with EOPNOTSUPP in this case. Normalize errno to simplify callers. */<br>
+  if (r == -1 && errno == ENODEV) {<br>
+    errno = EOPNOTSUPP;<br>
+  }<br>
+  return r;<br>
+}<br>
+#endif<br>
+<br>
 static void<br>
 file_unload (void)<br>
 {<br>
@@ -241,9 +256,9 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)<br>
<br>
 #ifdef FALLOC_FL_PUNCH_HOLE<br>
   if (may_trim) {<br>
-    r = fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,<br>
-                  offset, count);<br>
-    if (r == -1 && errno != EOPNOTSUPP && errno != ENODEV) {<br>
+    r = do_fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,<br>
+                      offset, count);<br>
+    if (r == -1 && errno != EOPNOTSUPP) {<br>
       nbdkit_error ("zero: %m");<br>
     }<br>
     /* PUNCH_HOLE is older; if it is not supported, it is likely that<br>
@@ -253,8 +268,8 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)<br>
 #endif<br>
<br>
 #ifdef FALLOC_FL_ZERO_RANGE<br>
-  r = fallocate (h->fd, FALLOC_FL_ZERO_RANGE, offset, count);<br>
-  if (r == -1 && errno != EOPNOTSUPP && errno != ENODEV) {<br>
+  r = do_fallocate (h->fd, FALLOC_FL_ZERO_RANGE, offset, count);<br>
+  if (r == -1 && errno != EOPNOTSUPP) {<br>
     nbdkit_error ("zero: %m");<br>
   }<br>
 #else<br>
@@ -288,11 +303,11 @@ file_trim (void *handle, uint32_t count, uint64_t offset)<br>
   struct handle *h = handle;<br>
<br>
   /* Trim is advisory; we don't care if it fails for anything other<br>
-   * than EIO, EPERM, or ENODEV (kernel 3.10) */<br>
-  r = fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,<br>
-                 offset, count);<br>
+   * than EIO or EPERM. */<br>
+  r = do_fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,<br>
+                    offset, count);<br>
   if (r < 0) {<br>
-    if (errno != EPERM && errno != EIO && errno != ENODEV) {<br>
+    if (errno != EPERM && errno != EIO) {<br>
       nbdkit_debug ("ignoring failed fallocate during trim: %m");<br>
       r = 0;<br>
     }<br>
-- <br>
2.17.1<br>
<br>
</blockquote></div></div>