[PATCH] virfile: workaround for when posix_fallocate() is not supported by FS

Roman Bogorodskiy posted 1 patch 3 years, 2 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20210131112521.89512-1-bogorodskiy@gmail.com
src/util/virfile.c | 10 ++++++++++
1 file changed, 10 insertions(+)
[PATCH] virfile: workaround for when posix_fallocate() is not supported by FS
Posted by Roman Bogorodskiy 3 years, 2 months ago
posix_fallocate() might be not supported by a filesystem, for example,
it's not supported by ZFS. In that case it fails with
return code 22 (EINVAL), and thus safezero_posix_fallocate() returns -1.

As safezero_posix_fallocate() is the first function tried by safezero()
and it tries other functions only when it returns -2, it fails
immediately without falling back to other methods, such as
safezero_slow().

Fix that by returning -2 if posix_fallocate() returns EINVAL, to give
safezero() a chance to try other functions.

Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
---
 src/util/virfile.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 609cafdd34..67a350deb3 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1119,6 +1119,16 @@ safezero_posix_fallocate(int fd, off_t offset, off_t len)
     int ret = posix_fallocate(fd, offset, len);
     if (ret == 0)
         return 0;
+    else if (ret == EINVAL)
+        /* EINVAL is returned when either:
+           - Operation is not supported by the underlying filesystem,
+           - offset or len argument values are invalid.
+           Assuming that offset and len are valid, this error means
+           the operation is not supported, and we need to fall back
+           to other methods.
+        */
+        return -2;
+
     errno = ret;
     return -1;
 }
-- 
2.30.0

Re: [PATCH] virfile: workaround for when posix_fallocate() is not supported by FS
Posted by Michal Privoznik 3 years, 2 months ago
On 1/31/21 12:25 PM, Roman Bogorodskiy wrote:
> posix_fallocate() might be not supported by a filesystem, for example,
> it's not supported by ZFS. In that case it fails with
> return code 22 (EINVAL), and thus safezero_posix_fallocate() returns -1.
> 
> As safezero_posix_fallocate() is the first function tried by safezero()
> and it tries other functions only when it returns -2, it fails
> immediately without falling back to other methods, such as
> safezero_slow().
> 
> Fix that by returning -2 if posix_fallocate() returns EINVAL, to give
> safezero() a chance to try other functions.
> 
> Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
> ---
>   src/util/virfile.c | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 609cafdd34..67a350deb3 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -1119,6 +1119,16 @@ safezero_posix_fallocate(int fd, off_t offset, off_t len)
>       int ret = posix_fallocate(fd, offset, len);
>       if (ret == 0)
>           return 0;
> +    else if (ret == EINVAL)

Please append { at EOL

> +        /* EINVAL is returned when either:
> +           - Operation is not supported by the underlying filesystem,
> +           - offset or len argument values are invalid.
> +           Assuming that offset and len are valid, this error means
> +           the operation is not supported, and we need to fall back
> +           to other methods.
> +        */
> +        return -2;
> +

and close it here. It's really a multiline body and should be wrapped in 
curly braces.

>       errno = ret;
>       return -1;
>   }
> 

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal