From a2e2e5c8a0e3e4ffb25d8177b4af115e06d02925 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 20 Sep 2020 20:49:53 +0300 Subject: [PATCH] mdbx: allow sendfile() and copy_file_range() to fail for Valgrind/QEMU cases. Change-Id: I37ee8d652d91a8d2106c782beceaacb13e9f667f --- src/core.c | 32 +++++++++++++++++++------------- src/options.h | 8 ++++---- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/core.c b/src/core.c index f20b3091..e6fbd52e 100644 --- a/src/core.c +++ b/src/core.c @@ -16315,30 +16315,36 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, MDBX_txn *read_txn, buffer + ceil_powerof2(meta_bytes, env->me_os_psize); for (size_t offset = meta_bytes; rc == MDBX_SUCCESS && offset < used_size;) { #if MDBX_USE_SENDFILE - if (dest_is_pipe) { + static bool sendfile_unavailable; + if (dest_is_pipe && likely(!sendfile_unavailable)) { off_t in_offset = offset; - const intptr_t written = + const ssize_t written = sendfile(fd, env->me_lazy_fd, &in_offset, used_size - offset); - if (unlikely(written <= 0)) { - rc = written ? errno : MDBX_ENODATA; - break; + if (likely(written > 0)) { + offset = in_offset; + continue; } - offset = in_offset; - continue; + rc = MDBX_ENODATA; + if (written == 0 || ignore_enosys(rc = errno) != MDBX_RESULT_TRUE) + break; + sendfile_unavailable = true; } #endif /* MDBX_USE_SENDFILE */ #if MDBX_USE_COPYFILERANGE - if (!dest_is_pipe) { + static bool copyfilerange_unavailable; + if (!dest_is_pipe && likely(!copyfilerange_unavailable)) { off_t in_offset = offset, out_offset = offset; ssize_t bytes_copied = copy_file_range( env->me_lazy_fd, &in_offset, fd, &out_offset, used_size - offset, 0); - if (unlikely(bytes_copied <= 0)) { - rc = bytes_copied ? errno : MDBX_ENODATA; - break; + if (likely(bytes_copied > 0)) { + offset = in_offset; + continue; } - offset = in_offset; - continue; + rc = MDBX_ENODATA; + if (bytes_copied == 0 || ignore_enosys(rc = errno) != MDBX_RESULT_TRUE) + break; + copyfilerange_unavailable = true; } #endif /* MDBX_USE_COPYFILERANGE */ diff --git a/src/options.h b/src/options.h index 2f0d2f65..0191ecc6 100644 --- a/src/options.h +++ b/src/options.h @@ -164,9 +164,9 @@ /** Advanced: Using sendfile() syscall (autodetection by default). */ #ifndef MDBX_USE_SENDFILE -#if (defined(__linux__) || defined(__gnu_linux__)) && \ - (!defined(__ANDROID_API__) || __ANDROID_API__ >= 21) && \ - !defined(MDBX_SAFE4QEMU) +#if ((defined(__linux__) || defined(__gnu_linux__)) && \ + __GLIBC_PREREQ(2, 3)) || \ + (defined(__ANDROID_API__) && __ANDROID_API__ >= 21) #define MDBX_USE_SENDFILE 1 #else #define MDBX_USE_SENDFILE 0 @@ -175,7 +175,7 @@ /** Advanced: Using copy_file_range() syscall (autodetection by default). */ #ifndef MDBX_USE_COPYFILERANGE -#if __GLIBC_PREREQ(2, 27) && defined(_GNU_SOURCE) && !defined(MDBX_SAFE4QEMU) +#if __GLIBC_PREREQ(2, 27) && defined(_GNU_SOURCE) #define MDBX_USE_COPYFILERANGE 1 #else #define MDBX_USE_COPYFILERANGE 0