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

Richard W.M. Jones rjones at redhat.com
Sat Jul 28 08:40:38 UTC 2018


On Sat, Jul 28, 2018 at 12:03:04AM +0300, Nir Soffer wrote:
> 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;
>      }

Thanks - I've pushed it.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/




More information about the Libguestfs mailing list