|
|
bdc76f |
commit 5a659ccc0ec217ab02a4c273a1f6d346a359560a
|
|
|
bdc76f |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bdc76f |
Date: Fri Jun 28 09:39:21 2019 +0200
|
|
|
bdc76f |
|
|
|
bdc76f |
io: Remove copy_file_range emulation [BZ #24744]
|
|
|
bdc76f |
|
|
|
bdc76f |
The kernel is evolving this interface (e.g., removal of the
|
|
|
bdc76f |
restriction on cross-device copies), and keeping up with that
|
|
|
bdc76f |
is difficult. Applications which need the function should
|
|
|
bdc76f |
run kernels which support the system call instead of relying on
|
|
|
bdc76f |
the imperfect glibc emulation.
|
|
|
bdc76f |
|
|
|
bdc76f |
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
|
|
bdc76f |
|
|
|
bdc76f |
# Conflicts:
|
|
|
bdc76f |
# io/copy_file_range-compat.c
|
|
|
bdc76f |
# io/copy_file_range.c
|
|
|
bdc76f |
# io/tst-copy_file_range-compat.c
|
|
|
bdc76f |
# io/tst-copy_file_range.c
|
|
|
bdc76f |
# sysdeps/unix/sysv/linux/arm/kernel-features.h
|
|
|
bdc76f |
# sysdeps/unix/sysv/linux/microblaze/kernel-features.h
|
|
|
bdc76f |
# sysdeps/unix/sysv/linux/sh/kernel-features.h
|
|
|
bdc76f |
|
|
|
bdc76f |
diff --git a/io/Makefile b/io/Makefile
|
|
|
bdc76f |
index 787a5c550ab64b17..62e71b4cbe879dbc 100644
|
|
|
bdc76f |
--- a/io/Makefile
|
|
|
bdc76f |
+++ b/io/Makefile
|
|
|
bdc76f |
@@ -75,11 +75,6 @@ tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \
|
|
|
bdc76f |
tst-fts tst-fts-lfs tst-open-tmpfile \
|
|
|
bdc76f |
tst-copy_file_range tst-getcwd-abspath \
|
|
|
bdc76f |
|
|
|
bdc76f |
-# This test includes the compat implementation of copy_file_range,
|
|
|
bdc76f |
-# which uses internal, unexported libc functions.
|
|
|
bdc76f |
-tests-static += tst-copy_file_range-compat
|
|
|
bdc76f |
-tests-internal += tst-copy_file_range-compat
|
|
|
bdc76f |
-
|
|
|
bdc76f |
# Likewise for statx, but we do not need static linking here.
|
|
|
bdc76f |
tests-internal += tst-statx
|
|
|
bdc76f |
|
|
|
bdc76f |
diff --git a/io/copy_file_range-compat.c b/io/copy_file_range-compat.c
|
|
|
bdc76f |
deleted file mode 100644
|
|
|
bdc76f |
index 4ab22cad19146ca9..0000000000000000
|
|
|
bdc76f |
--- a/io/copy_file_range-compat.c
|
|
|
bdc76f |
+++ /dev/null
|
|
|
bdc76f |
@@ -1,160 +0,0 @@
|
|
|
bdc76f |
-/* Emulation of copy_file_range.
|
|
|
bdc76f |
- Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
|
bdc76f |
- This file is part of the GNU C Library.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- The GNU C Library is free software; you can redistribute it and/or
|
|
|
bdc76f |
- modify it under the terms of the GNU Lesser General Public
|
|
|
bdc76f |
- License as published by the Free Software Foundation; either
|
|
|
bdc76f |
- version 2.1 of the License, or (at your option) any later version.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- The GNU C Library is distributed in the hope that it will be useful,
|
|
|
bdc76f |
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
bdc76f |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
bdc76f |
- Lesser General Public License for more details.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- You should have received a copy of the GNU Lesser General Public
|
|
|
bdc76f |
- License along with the GNU C Library; if not, see
|
|
|
bdc76f |
- <http://www.gnu.org/licenses/>. */
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* The following macros should be defined before including this
|
|
|
bdc76f |
- file:
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- COPY_FILE_RANGE_DECL Declaration specifiers for the function below.
|
|
|
bdc76f |
- COPY_FILE_RANGE Name of the function to define. */
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-#include <errno.h>
|
|
|
bdc76f |
-#include <fcntl.h>
|
|
|
bdc76f |
-#include <inttypes.h>
|
|
|
bdc76f |
-#include <limits.h>
|
|
|
bdc76f |
-#include <sys/stat.h>
|
|
|
bdc76f |
-#include <sys/types.h>
|
|
|
bdc76f |
-#include <unistd.h>
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-COPY_FILE_RANGE_DECL
|
|
|
bdc76f |
-ssize_t
|
|
|
bdc76f |
-COPY_FILE_RANGE (int infd, __off64_t *pinoff,
|
|
|
bdc76f |
- int outfd, __off64_t *poutoff,
|
|
|
bdc76f |
- size_t length, unsigned int flags)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- if (flags != 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- __set_errno (EINVAL);
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- struct stat64 instat;
|
|
|
bdc76f |
- struct stat64 outstat;
|
|
|
bdc76f |
- if (fstat64 (infd, &instat) != 0 || fstat64 (outfd, &outstat) != 0)
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- if (S_ISDIR (instat.st_mode) || S_ISDIR (outstat.st_mode))
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- __set_errno (EISDIR);
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- if (!S_ISREG (instat.st_mode) || !S_ISREG (outstat.st_mode))
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* We need a regular input file so that the we can seek
|
|
|
bdc76f |
- backwards in case of a write failure. */
|
|
|
bdc76f |
- __set_errno (EINVAL);
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- if (instat.st_dev != outstat.st_dev)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* Cross-device copies are not supported. */
|
|
|
bdc76f |
- __set_errno (EXDEV);
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* The output descriptor must not have O_APPEND set. */
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- int flags = __fcntl (outfd, F_GETFL);
|
|
|
bdc76f |
- if (flags & O_APPEND)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- __set_errno (EBADF);
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Avoid an overflow in the result. */
|
|
|
bdc76f |
- if (length > SSIZE_MAX)
|
|
|
bdc76f |
- length = SSIZE_MAX;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Main copying loop. The buffer size is arbitrary and is a
|
|
|
bdc76f |
- trade-off between stack size consumption, cache usage, and
|
|
|
bdc76f |
- amortization of system call overhead. */
|
|
|
bdc76f |
- size_t copied = 0;
|
|
|
bdc76f |
- char buf[8192];
|
|
|
bdc76f |
- while (length > 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- size_t to_read = length;
|
|
|
bdc76f |
- if (to_read > sizeof (buf))
|
|
|
bdc76f |
- to_read = sizeof (buf);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Fill the buffer. */
|
|
|
bdc76f |
- ssize_t read_count;
|
|
|
bdc76f |
- if (pinoff == NULL)
|
|
|
bdc76f |
- read_count = read (infd, buf, to_read);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- read_count = __libc_pread64 (infd, buf, to_read, *pinoff);
|
|
|
bdc76f |
- if (read_count == 0)
|
|
|
bdc76f |
- /* End of file reached prematurely. */
|
|
|
bdc76f |
- return copied;
|
|
|
bdc76f |
- if (read_count < 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (copied > 0)
|
|
|
bdc76f |
- /* Report the number of bytes copied so far. */
|
|
|
bdc76f |
- return copied;
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- if (pinoff != NULL)
|
|
|
bdc76f |
- *pinoff += read_count;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Write the buffer part which was read to the destination. */
|
|
|
bdc76f |
- char *end = buf + read_count;
|
|
|
bdc76f |
- for (char *p = buf; p < end; )
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- ssize_t write_count;
|
|
|
bdc76f |
- if (poutoff == NULL)
|
|
|
bdc76f |
- write_count = write (outfd, p, end - p);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- write_count = __libc_pwrite64 (outfd, p, end - p, *poutoff);
|
|
|
bdc76f |
- if (write_count < 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* Adjust the input read position to match what we have
|
|
|
bdc76f |
- written, so that the caller can pick up after the
|
|
|
bdc76f |
- error. */
|
|
|
bdc76f |
- size_t written = p - buf;
|
|
|
bdc76f |
- /* NB: This needs to be signed so that we can form the
|
|
|
bdc76f |
- negative value below. */
|
|
|
bdc76f |
- ssize_t overread = read_count - written;
|
|
|
bdc76f |
- if (pinoff == NULL)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (overread > 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* We are on an error recovery path, so we
|
|
|
bdc76f |
- cannot deal with failure here. */
|
|
|
bdc76f |
- int save_errno = errno;
|
|
|
bdc76f |
- (void) __libc_lseek64 (infd, -overread, SEEK_CUR);
|
|
|
bdc76f |
- __set_errno (save_errno);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else /* pinoff != NULL */
|
|
|
bdc76f |
- *pinoff -= overread;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- if (copied + written > 0)
|
|
|
bdc76f |
- /* Report the number of bytes copied so far. */
|
|
|
bdc76f |
- return copied + written;
|
|
|
bdc76f |
- return -1;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- p += write_count;
|
|
|
bdc76f |
- if (poutoff != NULL)
|
|
|
bdc76f |
- *poutoff += write_count;
|
|
|
bdc76f |
- } /* Write loop. */
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- copied += read_count;
|
|
|
bdc76f |
- length -= read_count;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- return copied;
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
diff --git a/io/copy_file_range.c b/io/copy_file_range.c
|
|
|
bdc76f |
index 98bff8bd2615b214..59fb979773b2b202 100644
|
|
|
bdc76f |
--- a/io/copy_file_range.c
|
|
|
bdc76f |
+++ b/io/copy_file_range.c
|
|
|
bdc76f |
@@ -1,5 +1,5 @@
|
|
|
bdc76f |
-/* Generic implementation of copy_file_range.
|
|
|
bdc76f |
- Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
|
bdc76f |
+/* Stub implementation of copy_file_range.
|
|
|
bdc76f |
+ Copyright (C) 2017-2019 Free Software Foundation, Inc.
|
|
|
bdc76f |
This file is part of the GNU C Library.
|
|
|
bdc76f |
|
|
|
bdc76f |
The GNU C Library is free software; you can redistribute it and/or
|
|
|
bdc76f |
@@ -16,7 +16,15 @@
|
|
|
bdc76f |
License along with the GNU C Library; if not, see
|
|
|
bdc76f |
<http://www.gnu.org/licenses/>. */
|
|
|
bdc76f |
|
|
|
bdc76f |
-#define COPY_FILE_RANGE_DECL
|
|
|
bdc76f |
-#define COPY_FILE_RANGE copy_file_range
|
|
|
bdc76f |
+#include <errno.h>
|
|
|
bdc76f |
+#include <unistd.h>
|
|
|
bdc76f |
|
|
|
bdc76f |
-#include <io/copy_file_range-compat.c>
|
|
|
bdc76f |
+ssize_t
|
|
|
bdc76f |
+copy_file_range (int infd, __off64_t *pinoff,
|
|
|
bdc76f |
+ int outfd, __off64_t *poutoff,
|
|
|
bdc76f |
+ size_t length, unsigned int flags)
|
|
|
bdc76f |
+{
|
|
|
bdc76f |
+ __set_errno (ENOSYS);
|
|
|
bdc76f |
+ return -1;
|
|
|
bdc76f |
+}
|
|
|
bdc76f |
+stub_warning (copy_file_range)
|
|
|
bdc76f |
diff --git a/io/tst-copy_file_range-compat.c b/io/tst-copy_file_range-compat.c
|
|
|
bdc76f |
deleted file mode 100644
|
|
|
bdc76f |
index 00c109a74d3c9d64..0000000000000000
|
|
|
bdc76f |
--- a/io/tst-copy_file_range-compat.c
|
|
|
bdc76f |
+++ /dev/null
|
|
|
bdc76f |
@@ -1,30 +0,0 @@
|
|
|
bdc76f |
-/* Test the fallback implementation of copy_file_range.
|
|
|
bdc76f |
- Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
|
bdc76f |
- This file is part of the GNU C Library.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- The GNU C Library is free software; you can redistribute it and/or
|
|
|
bdc76f |
- modify it under the terms of the GNU Lesser General Public
|
|
|
bdc76f |
- License as published by the Free Software Foundation; either
|
|
|
bdc76f |
- version 2.1 of the License, or (at your option) any later version.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- The GNU C Library is distributed in the hope that it will be useful,
|
|
|
bdc76f |
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
bdc76f |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
bdc76f |
- Lesser General Public License for more details.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- You should have received a copy of the GNU Lesser General Public
|
|
|
bdc76f |
- License along with the GNU C Library; if not, see
|
|
|
bdc76f |
- <http://www.gnu.org/licenses/>. */
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Get the declaration of the official copy_of_range function. */
|
|
|
bdc76f |
-#include <unistd.h>
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Compile a local version of copy_file_range. */
|
|
|
bdc76f |
-#define COPY_FILE_RANGE_DECL static
|
|
|
bdc76f |
-#define COPY_FILE_RANGE copy_file_range_compat
|
|
|
bdc76f |
-#include <io/copy_file_range-compat.c>
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Re-use the test, but run it against copy_file_range_compat defined
|
|
|
bdc76f |
- above. */
|
|
|
bdc76f |
-#define copy_file_range copy_file_range_compat
|
|
|
bdc76f |
-#include "tst-copy_file_range.c"
|
|
|
bdc76f |
diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
|
|
|
bdc76f |
index 3d531a19370911e5..4504020d2ee7d2ee 100644
|
|
|
bdc76f |
--- a/io/tst-copy_file_range.c
|
|
|
bdc76f |
+++ b/io/tst-copy_file_range.c
|
|
|
bdc76f |
@@ -20,22 +20,15 @@
|
|
|
bdc76f |
#include <errno.h>
|
|
|
bdc76f |
#include <fcntl.h>
|
|
|
bdc76f |
#include <inttypes.h>
|
|
|
bdc76f |
-#include <libgen.h>
|
|
|
bdc76f |
-#include <poll.h>
|
|
|
bdc76f |
-#include <sched.h>
|
|
|
bdc76f |
#include <stdbool.h>
|
|
|
bdc76f |
#include <stdio.h>
|
|
|
bdc76f |
#include <stdlib.h>
|
|
|
bdc76f |
#include <string.h>
|
|
|
bdc76f |
#include <support/check.h>
|
|
|
bdc76f |
-#include <support/namespace.h>
|
|
|
bdc76f |
#include <support/support.h>
|
|
|
bdc76f |
#include <support/temp_file.h>
|
|
|
bdc76f |
#include <support/test-driver.h>
|
|
|
bdc76f |
#include <support/xunistd.h>
|
|
|
bdc76f |
-#ifdef CLONE_NEWNS
|
|
|
bdc76f |
-# include <sys/mount.h>
|
|
|
bdc76f |
-#endif
|
|
|
bdc76f |
|
|
|
bdc76f |
/* Boolean flags which indicate whether to use pointers with explicit
|
|
|
bdc76f |
output flags. */
|
|
|
bdc76f |
@@ -49,10 +42,6 @@ static int infd;
|
|
|
bdc76f |
static char *outfile;
|
|
|
bdc76f |
static int outfd;
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* Like the above, but on a different file system. xdevfile can be
|
|
|
bdc76f |
- NULL if no suitable file system has been found. */
|
|
|
bdc76f |
-static char *xdevfile;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
/* Input and output offsets. Set according to do_inoff and do_outoff
|
|
|
bdc76f |
before the test. The offsets themselves are always set to
|
|
|
bdc76f |
zero. */
|
|
|
bdc76f |
@@ -61,13 +50,10 @@ static off64_t *pinoff;
|
|
|
bdc76f |
static off64_t outoff;
|
|
|
bdc76f |
static off64_t *poutoff;
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* These are a collection of copy sizes used in tests. The selection
|
|
|
bdc76f |
- takes into account that the fallback implementation uses an
|
|
|
bdc76f |
- internal buffer of 8192 bytes. */
|
|
|
bdc76f |
+/* These are a collection of copy sizes used in tests. */
|
|
|
bdc76f |
enum { maximum_size = 99999 };
|
|
|
bdc76f |
static const int typical_sizes[] =
|
|
|
bdc76f |
- { 0, 1, 2, 3, 1024, 2048, 4096, 8191, 8192, 8193, 16383, 16384, 16385,
|
|
|
bdc76f |
- maximum_size };
|
|
|
bdc76f |
+ { 0, 1, 2, 3, 1024, 2048, 4096, 8191, 8192, 8193, maximum_size };
|
|
|
bdc76f |
|
|
|
bdc76f |
/* The random contents of this array can be used as a pattern to check
|
|
|
bdc76f |
for correct write operations. */
|
|
|
bdc76f |
@@ -76,101 +62,6 @@ static unsigned char random_data[maximum_size];
|
|
|
bdc76f |
/* The size chosen by the test harness. */
|
|
|
bdc76f |
static int current_size;
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* Maximum writable file offset. Updated by find_maximum_offset
|
|
|
bdc76f |
- below. */
|
|
|
bdc76f |
-static off64_t maximum_offset;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Error code when crossing the offset. */
|
|
|
bdc76f |
-static int maximum_offset_errno;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* If true: Writes which cross the limit will fail. If false: Writes
|
|
|
bdc76f |
- which cross the limit will result in a partial write. */
|
|
|
bdc76f |
-static bool maximum_offset_hard_limit;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Fills maximum_offset etc. above. Truncates outfd as a side
|
|
|
bdc76f |
- effect. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-find_maximum_offset (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- xftruncate (outfd, 0);
|
|
|
bdc76f |
- if (maximum_offset != 0)
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- uint64_t upper = -1;
|
|
|
bdc76f |
- upper >>= 1; /* Maximum of off64_t. */
|
|
|
bdc76f |
- TEST_VERIFY ((off64_t) upper > 0);
|
|
|
bdc76f |
- TEST_VERIFY ((off64_t) (upper + 1) < 0);
|
|
|
bdc76f |
- if (lseek64 (outfd, upper, SEEK_SET) >= 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (write (outfd, "", 1) == 1)
|
|
|
bdc76f |
- FAIL_EXIT1 ("created a file larger than the off64_t range");
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- uint64_t lower = 1024 * 1024; /* A reasonable minimum file size. */
|
|
|
bdc76f |
- /* Loop invariant: writing at lower succeeds, writing at upper fails. */
|
|
|
bdc76f |
- while (lower + 1 < upper)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- uint64_t middle = (lower + upper) / 2;
|
|
|
bdc76f |
- if (test_verbose > 0)
|
|
|
bdc76f |
- printf ("info: %s: remaining test range %" PRIu64 " .. %" PRIu64
|
|
|
bdc76f |
- ", probe at %" PRIu64 "\n", __func__, lower, upper, middle);
|
|
|
bdc76f |
- xftruncate (outfd, 0);
|
|
|
bdc76f |
- if (lseek64 (outfd, middle, SEEK_SET) >= 0
|
|
|
bdc76f |
- && write (outfd, "", 1) == 1)
|
|
|
bdc76f |
- lower = middle;
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- upper = middle;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- TEST_VERIFY (lower + 1 == upper);
|
|
|
bdc76f |
- maximum_offset = lower;
|
|
|
bdc76f |
- printf ("info: maximum writable file offset: %" PRIu64 " (%" PRIx64 ")\n",
|
|
|
bdc76f |
- lower, lower);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Check that writing at the valid offset actually works. */
|
|
|
bdc76f |
- xftruncate (outfd, 0);
|
|
|
bdc76f |
- xlseek (outfd, lower, SEEK_SET);
|
|
|
bdc76f |
- TEST_COMPARE (write (outfd, "", 1), 1);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Cross the boundary with a two-byte write. This can either result
|
|
|
bdc76f |
- in a short write, or a failure. */
|
|
|
bdc76f |
- xlseek (outfd, lower, SEEK_SET);
|
|
|
bdc76f |
- ssize_t ret = write (outfd, " ", 2);
|
|
|
bdc76f |
- if (ret < 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- maximum_offset_errno = errno;
|
|
|
bdc76f |
- maximum_offset_hard_limit = true;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- maximum_offset_hard_limit = false;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Check that writing at the next offset actually fails. This also
|
|
|
bdc76f |
- obtains the expected errno value. */
|
|
|
bdc76f |
- xftruncate (outfd, 0);
|
|
|
bdc76f |
- const char *action;
|
|
|
bdc76f |
- if (lseek64 (outfd, lower + 1, SEEK_SET) != 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (write (outfd, "", 1) != -1)
|
|
|
bdc76f |
- FAIL_EXIT1 ("write to impossible offset %" PRIu64 " succeeded",
|
|
|
bdc76f |
- lower + 1);
|
|
|
bdc76f |
- action = "writing";
|
|
|
bdc76f |
- int errno_copy = errno;
|
|
|
bdc76f |
- if (maximum_offset_hard_limit)
|
|
|
bdc76f |
- TEST_COMPARE (errno_copy, maximum_offset_errno);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- maximum_offset_errno = errno_copy;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- action = "seeking";
|
|
|
bdc76f |
- maximum_offset_errno = errno;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- printf ("info: %s out of range fails with %m (%d)\n",
|
|
|
bdc76f |
- action, maximum_offset_errno);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xftruncate (outfd, 0);
|
|
|
bdc76f |
- xlseek (outfd, 0, SEEK_SET);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
/* Perform a copy of a file. */
|
|
|
bdc76f |
static void
|
|
|
bdc76f |
simple_file_copy (void)
|
|
|
bdc76f |
@@ -247,390 +138,6 @@ simple_file_copy (void)
|
|
|
bdc76f |
free (bytes);
|
|
|
bdc76f |
}
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* Test that reading from a pipe willfails. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-pipe_as_source (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- int pipefds[2];
|
|
|
bdc76f |
- xpipe (pipefds);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- for (int length = 0; length < 2; ++length)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (test_verbose > 0)
|
|
|
bdc76f |
- printf ("info: %s: length=%d\n", __func__, length);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Make sure that there is something to copy in the pipe. */
|
|
|
bdc76f |
- xwrite (pipefds[1], "@", 1);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (pipefds[0], pinoff, outfd, poutoff,
|
|
|
bdc76f |
- length, 0), -1);
|
|
|
bdc76f |
- /* Linux 4.10 and later return EINVAL. Older kernels return
|
|
|
bdc76f |
- EXDEV. */
|
|
|
bdc76f |
- TEST_VERIFY (errno == EINVAL || errno == EXDEV);
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 0);
|
|
|
bdc76f |
- TEST_COMPARE (outoff, 0);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Make sure that nothing was read. */
|
|
|
bdc76f |
- char buf = 'A';
|
|
|
bdc76f |
- TEST_COMPARE (read (pipefds[0], &buf, 1), 1);
|
|
|
bdc76f |
- TEST_COMPARE (buf, '@');
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xclose (pipefds[0]);
|
|
|
bdc76f |
- xclose (pipefds[1]);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Test that writing to a pipe fails. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-pipe_as_destination (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- /* Make sure that there is something to read in the input file. */
|
|
|
bdc76f |
- xwrite (infd, "abc", 3);
|
|
|
bdc76f |
- xlseek (infd, 0, SEEK_SET);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- int pipefds[2];
|
|
|
bdc76f |
- xpipe (pipefds);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- for (int length = 0; length < 2; ++length)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- if (test_verbose > 0)
|
|
|
bdc76f |
- printf ("info: %s: length=%d\n", __func__, length);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, pipefds[1], poutoff,
|
|
|
bdc76f |
- length, 0), -1);
|
|
|
bdc76f |
- /* Linux 4.10 and later return EINVAL. Older kernels return
|
|
|
bdc76f |
- EXDEV. */
|
|
|
bdc76f |
- TEST_VERIFY (errno == EINVAL || errno == EXDEV);
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 0);
|
|
|
bdc76f |
- TEST_COMPARE (outoff, 0);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Make sure that nothing was written. */
|
|
|
bdc76f |
- struct pollfd pollfd = { .fd = pipefds[0], .events = POLLIN, };
|
|
|
bdc76f |
- TEST_COMPARE (poll (&pollfd, 1, 0), 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xclose (pipefds[0]);
|
|
|
bdc76f |
- xclose (pipefds[1]);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Test a write failure after (potentially) writing some bytes.
|
|
|
bdc76f |
- Failure occurs near the start of the buffer. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-delayed_write_failure_beginning (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- /* We need to write something to provoke the error. */
|
|
|
bdc76f |
- if (current_size == 0)
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
- xwrite (infd, random_data, sizeof (random_data));
|
|
|
bdc76f |
- xlseek (infd, 0, SEEK_SET);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Write failure near the start. The actual error code varies among
|
|
|
bdc76f |
- file systems. */
|
|
|
bdc76f |
- find_maximum_offset ();
|
|
|
bdc76f |
- off64_t where = maximum_offset;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- if (current_size == 1)
|
|
|
bdc76f |
- ++where;
|
|
|
bdc76f |
- outoff = where;
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- xlseek (outfd, 1, SEEK_SET);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- xlseek (outfd, where, SEEK_SET);
|
|
|
bdc76f |
- if (maximum_offset_hard_limit || where > maximum_offset)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- sizeof (random_data), 0), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, maximum_offset_errno);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 0);
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where);
|
|
|
bdc76f |
- TEST_COMPARE (outoff, where);
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (outfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* The offset is not a hard limit. This means we write one
|
|
|
bdc76f |
- byte. */
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- sizeof (random_data), 0), 1);
|
|
|
bdc76f |
- if (do_inoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 1);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1);
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1);
|
|
|
bdc76f |
- TEST_COMPARE (outoff, where + 1);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where + 1);
|
|
|
bdc76f |
- TEST_COMPARE (outoff, where);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (outfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, where + 1);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Test a write failure after (potentially) writing some bytes.
|
|
|
bdc76f |
- Failure occurs near the end of the buffer. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-delayed_write_failure_end (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- if (current_size <= 1)
|
|
|
bdc76f |
- /* This would be same as the first test because there is not
|
|
|
bdc76f |
- enough data to write to make a difference. */
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
- xwrite (infd, random_data, sizeof (random_data));
|
|
|
bdc76f |
- xlseek (infd, 0, SEEK_SET);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- find_maximum_offset ();
|
|
|
bdc76f |
- off64_t where = maximum_offset - current_size + 1;
|
|
|
bdc76f |
- if (current_size == sizeof (random_data))
|
|
|
bdc76f |
- /* Otherwise we do not reach the non-writable byte. */
|
|
|
bdc76f |
- ++where;
|
|
|
bdc76f |
- outoff = where;
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- xlseek (outfd, 1, SEEK_SET);
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- xlseek (outfd, where, SEEK_SET);
|
|
|
bdc76f |
- ssize_t ret = copy_file_range (infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- sizeof (random_data), 0);
|
|
|
bdc76f |
- if (ret < 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (ret, -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, maximum_offset_errno);
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (outfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- /* The first copy succeeded. This happens in the emulation
|
|
|
bdc76f |
- because the internal buffer of limited size does not
|
|
|
bdc76f |
- necessarily cross the off64_t boundary on the first write
|
|
|
bdc76f |
- operation. */
|
|
|
bdc76f |
- if (test_verbose > 0)
|
|
|
bdc76f |
- printf ("info: copy_file_range (%zu) returned %zd\n",
|
|
|
bdc76f |
- sizeof (random_data), ret);
|
|
|
bdc76f |
- TEST_VERIFY (ret > 0);
|
|
|
bdc76f |
- TEST_VERIFY (ret < maximum_size);
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (outfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, where + ret);
|
|
|
bdc76f |
- if (do_inoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (inoff, ret);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), ret);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- char *buffer = xmalloc (ret);
|
|
|
bdc76f |
- TEST_COMPARE (pread64 (outfd, buffer, ret, where), ret);
|
|
|
bdc76f |
- TEST_VERIFY (memcmp (buffer, random_data, ret) == 0);
|
|
|
bdc76f |
- free (buffer);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* The second copy fails. */
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- sizeof (random_data), 0), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, maximum_offset_errno);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Test a write failure across devices. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-cross_device_failure (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- if (xdevfile == NULL)
|
|
|
bdc76f |
- /* Subtest not supported due to missing cross-device file. */
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* We need something to write. */
|
|
|
bdc76f |
- xwrite (infd, random_data, sizeof (random_data));
|
|
|
bdc76f |
- xlseek (infd, 0, SEEK_SET);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- int xdevfd = xopen (xdevfile, O_RDWR | O_LARGEFILE, 0);
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, xdevfd, poutoff,
|
|
|
bdc76f |
- current_size, 0), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, EXDEV);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (xdevfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, 0);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xclose (xdevfd);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Try to exercise ENOSPC behavior with a tempfs file system (so that
|
|
|
bdc76f |
- we do not have to fill up a regular file system to get the error).
|
|
|
bdc76f |
- This function runs in a subprocess, so that we do not change the
|
|
|
bdc76f |
- mount namespace of the actual test process. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-enospc_failure_1 (void *closure)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
-#ifdef CLONE_NEWNS
|
|
|
bdc76f |
- support_become_root ();
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Make sure that we do not alter the file system mounts of the
|
|
|
bdc76f |
- parents. */
|
|
|
bdc76f |
- if (! support_enter_mount_namespace ())
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- printf ("warning: ENOSPC test skipped\n");
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- char *mountpoint = closure;
|
|
|
bdc76f |
- if (mount ("none", mountpoint, "tmpfs", MS_NODEV | MS_NOEXEC,
|
|
|
bdc76f |
- "size=500k") != 0)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- printf ("warning: could not mount tmpfs at %s: %m\n", mountpoint);
|
|
|
bdc76f |
- return;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* The source file must reside on the same file system. */
|
|
|
bdc76f |
- char *intmpfsfile = xasprintf ("%s/%s", mountpoint, "in");
|
|
|
bdc76f |
- int intmpfsfd = xopen (intmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
|
|
|
bdc76f |
- xwrite (intmpfsfd, random_data, sizeof (random_data));
|
|
|
bdc76f |
- xlseek (intmpfsfd, 1, SEEK_SET);
|
|
|
bdc76f |
- inoff = 1;
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- char *outtmpfsfile = xasprintf ("%s/%s", mountpoint, "out");
|
|
|
bdc76f |
- int outtmpfsfd = xopen (outtmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Fill the file with data until ENOSPC is reached. */
|
|
|
bdc76f |
- while (true)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- ssize_t ret = write (outtmpfsfd, random_data, sizeof (random_data));
|
|
|
bdc76f |
- if (ret < 0 && errno != ENOSPC)
|
|
|
bdc76f |
- FAIL_EXIT1 ("write to %s: %m", outtmpfsfile);
|
|
|
bdc76f |
- if (ret < sizeof (random_data))
|
|
|
bdc76f |
- break;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- TEST_COMPARE (write (outtmpfsfd, "", 1), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, ENOSPC);
|
|
|
bdc76f |
- off64_t maxsize = xlseek (outtmpfsfd, 0, SEEK_CUR);
|
|
|
bdc76f |
- TEST_VERIFY_EXIT (maxsize > sizeof (random_data));
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Constructed the expected file contents. */
|
|
|
bdc76f |
- char *expected = xmalloc (maxsize);
|
|
|
bdc76f |
- TEST_COMPARE (pread64 (outtmpfsfd, expected, maxsize, 0), maxsize);
|
|
|
bdc76f |
- /* Go back a little, so some bytes can be written. */
|
|
|
bdc76f |
- enum { offset = 20000 };
|
|
|
bdc76f |
- TEST_VERIFY_EXIT (offset < maxsize);
|
|
|
bdc76f |
- TEST_VERIFY_EXIT (offset < sizeof (random_data));
|
|
|
bdc76f |
- memcpy (expected + maxsize - offset, random_data + 1, offset);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- outoff = maxsize - offset;
|
|
|
bdc76f |
- xlseek (outtmpfsfd, 2, SEEK_SET);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- xlseek (outtmpfsfd, -offset, SEEK_CUR);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* First call is expected to succeed because we made room for some
|
|
|
bdc76f |
- bytes. */
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff,
|
|
|
bdc76f |
- maximum_size, 0), offset);
|
|
|
bdc76f |
- if (do_inoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 1 + offset);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset);
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (outoff, maxsize);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize);
|
|
|
bdc76f |
- struct stat64 st;
|
|
|
bdc76f |
- xfstat (outtmpfsfd, &st);
|
|
|
bdc76f |
- TEST_COMPARE (st.st_size, maxsize);
|
|
|
bdc76f |
- char *actual = xmalloc (st.st_size);
|
|
|
bdc76f |
- TEST_COMPARE (pread64 (outtmpfsfd, actual, st.st_size, 0), st.st_size);
|
|
|
bdc76f |
- TEST_VERIFY (memcmp (expected, actual, maxsize) == 0);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Second call should fail with ENOSPC. */
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff,
|
|
|
bdc76f |
- maximum_size, 0), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, ENOSPC);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Offsets should be unchanged. */
|
|
|
bdc76f |
- if (do_inoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (inoff, 1 + offset);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset);
|
|
|
bdc76f |
- if (do_outoff)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- TEST_COMPARE (outoff, maxsize);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize);
|
|
|
bdc76f |
- TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_END), maxsize);
|
|
|
bdc76f |
- TEST_COMPARE (pread64 (outtmpfsfd, actual, maxsize, 0), maxsize);
|
|
|
bdc76f |
- TEST_VERIFY (memcmp (expected, actual, maxsize) == 0);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- free (actual);
|
|
|
bdc76f |
- free (expected);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xclose (intmpfsfd);
|
|
|
bdc76f |
- xclose (outtmpfsfd);
|
|
|
bdc76f |
- free (intmpfsfile);
|
|
|
bdc76f |
- free (outtmpfsfile);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-#else /* !CLONE_NEWNS */
|
|
|
bdc76f |
- puts ("warning: ENOSPC test skipped (no mount namespaces)");
|
|
|
bdc76f |
-#endif
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* Call enospc_failure_1 in a subprocess. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-enospc_failure (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- char *mountpoint
|
|
|
bdc76f |
- = support_create_temp_directory ("tst-copy_file_range-enospc-");
|
|
|
bdc76f |
- support_isolate_in_subprocess (enospc_failure_1, mountpoint);
|
|
|
bdc76f |
- free (mountpoint);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-/* The target file descriptor must have O_APPEND enabled. */
|
|
|
bdc76f |
-static void
|
|
|
bdc76f |
-oappend_failure (void)
|
|
|
bdc76f |
-{
|
|
|
bdc76f |
- /* Add data, to make sure we do not fail because there is
|
|
|
bdc76f |
- insufficient input data. */
|
|
|
bdc76f |
- xwrite (infd, random_data, current_size);
|
|
|
bdc76f |
- xlseek (infd, 0, SEEK_SET);
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- xclose (outfd);
|
|
|
bdc76f |
- outfd = xopen (outfile, O_RDWR | O_APPEND, 0);
|
|
|
bdc76f |
- TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- current_size, 0), -1);
|
|
|
bdc76f |
- TEST_COMPARE (errno, EBADF);
|
|
|
bdc76f |
-}
|
|
|
bdc76f |
-
|
|
|
bdc76f |
/* Test that a short input file results in a shortened copy. */
|
|
|
bdc76f |
static void
|
|
|
bdc76f |
short_copy (void)
|
|
|
bdc76f |
@@ -721,14 +228,6 @@ struct test_case
|
|
|
bdc76f |
static struct test_case tests[] =
|
|
|
bdc76f |
{
|
|
|
bdc76f |
{ "simple_file_copy", simple_file_copy, .sizes = true },
|
|
|
bdc76f |
- { "pipe_as_source", pipe_as_source, },
|
|
|
bdc76f |
- { "pipe_as_destination", pipe_as_destination, },
|
|
|
bdc76f |
- { "delayed_write_failure_beginning", delayed_write_failure_beginning,
|
|
|
bdc76f |
- .sizes = true },
|
|
|
bdc76f |
- { "delayed_write_failure_end", delayed_write_failure_end, .sizes = true },
|
|
|
bdc76f |
- { "cross_device_failure", cross_device_failure, .sizes = true },
|
|
|
bdc76f |
- { "enospc_failure", enospc_failure, },
|
|
|
bdc76f |
- { "oappend_failure", oappend_failure, .sizes = true },
|
|
|
bdc76f |
{ "short_copy", short_copy, .sizes = true },
|
|
|
bdc76f |
};
|
|
|
bdc76f |
|
|
|
bdc76f |
@@ -739,53 +238,18 @@ do_test (void)
|
|
|
bdc76f |
*p = rand () >> 24;
|
|
|
bdc76f |
|
|
|
bdc76f |
infd = create_temp_file ("tst-copy_file_range-in-", &infile);
|
|
|
bdc76f |
- xclose (create_temp_file ("tst-copy_file_range-out-", &outfile));
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- /* Try to find a different directory from the default input/output
|
|
|
bdc76f |
- file. */
|
|
|
bdc76f |
+ outfd = create_temp_file ("tst-copy_file_range-out-", &outfile);
|
|
|
bdc76f |
{
|
|
|
bdc76f |
- struct stat64 instat;
|
|
|
bdc76f |
- xfstat (infd, &instat);
|
|
|
bdc76f |
- static const char *const candidates[] =
|
|
|
bdc76f |
- { NULL, "/var/tmp", "/dev/shm" };
|
|
|
bdc76f |
- for (const char *const *c = candidates; c < array_end (candidates); ++c)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- const char *path = *c;
|
|
|
bdc76f |
- char *to_free = NULL;
|
|
|
bdc76f |
- if (path == NULL)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- to_free = xreadlink ("/proc/self/exe");
|
|
|
bdc76f |
- path = dirname (to_free);
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- struct stat64 cstat;
|
|
|
bdc76f |
- xstat (path, &cstat);
|
|
|
bdc76f |
- if (cstat.st_dev == instat.st_dev)
|
|
|
bdc76f |
- {
|
|
|
bdc76f |
- free (to_free);
|
|
|
bdc76f |
- continue;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
-
|
|
|
bdc76f |
- printf ("info: using alternate temporary files directory: %s\n", path);
|
|
|
bdc76f |
- xdevfile = xasprintf ("%s/tst-copy_file_range-xdev-XXXXXX", path);
|
|
|
bdc76f |
- free (to_free);
|
|
|
bdc76f |
- break;
|
|
|
bdc76f |
- }
|
|
|
bdc76f |
- if (xdevfile != NULL)
|
|
|
bdc76f |
+ ssize_t ret = copy_file_range (infd, NULL, outfd, NULL, 0, 0);
|
|
|
bdc76f |
+ if (ret != 0)
|
|
|
bdc76f |
{
|
|
|
bdc76f |
- int xdevfd = mkstemp (xdevfile);
|
|
|
bdc76f |
- if (xdevfd < 0)
|
|
|
bdc76f |
- FAIL_EXIT1 ("mkstemp (\"%s\"): %m", xdevfile);
|
|
|
bdc76f |
- struct stat64 xdevst;
|
|
|
bdc76f |
- xfstat (xdevfd, &xdevst);
|
|
|
bdc76f |
- TEST_VERIFY (xdevst.st_dev != instat.st_dev);
|
|
|
bdc76f |
- add_temp_file (xdevfile);
|
|
|
bdc76f |
- xclose (xdevfd);
|
|
|
bdc76f |
+ if (errno == ENOSYS)
|
|
|
bdc76f |
+ FAIL_UNSUPPORTED ("copy_file_range is not support on this system");
|
|
|
bdc76f |
+ FAIL_EXIT1 ("copy_file_range probing call: %m");
|
|
|
bdc76f |
}
|
|
|
bdc76f |
- else
|
|
|
bdc76f |
- puts ("warning: no alternate directory on different file system found");
|
|
|
bdc76f |
}
|
|
|
bdc76f |
xclose (infd);
|
|
|
bdc76f |
+ xclose (outfd);
|
|
|
bdc76f |
|
|
|
bdc76f |
for (do_inoff = 0; do_inoff < 2; ++do_inoff)
|
|
|
bdc76f |
for (do_outoff = 0; do_outoff < 2; ++do_outoff)
|
|
|
bdc76f |
@@ -827,7 +291,6 @@ do_test (void)
|
|
|
bdc76f |
|
|
|
bdc76f |
free (infile);
|
|
|
bdc76f |
free (outfile);
|
|
|
bdc76f |
- free (xdevfile);
|
|
|
bdc76f |
|
|
|
bdc76f |
return 0;
|
|
|
bdc76f |
}
|
|
|
bdc76f |
diff --git a/manual/llio.texi b/manual/llio.texi
|
|
|
bdc76f |
index 2733b9cb7331df07..26f7d2cb3ea220d9 100644
|
|
|
bdc76f |
--- a/manual/llio.texi
|
|
|
bdc76f |
+++ b/manual/llio.texi
|
|
|
bdc76f |
@@ -1404,10 +1404,13 @@ failure occurs. The return value is zero if the end of the input file
|
|
|
bdc76f |
is encountered immediately.
|
|
|
bdc76f |
|
|
|
bdc76f |
If no bytes can be copied, to report an error, @code{copy_file_range}
|
|
|
bdc76f |
-returns the value @math{-1} and sets @code{errno}. The following
|
|
|
bdc76f |
-@code{errno} error conditions are specific to this function:
|
|
|
bdc76f |
+returns the value @math{-1} and sets @code{errno}. The table below
|
|
|
bdc76f |
+lists some of the error conditions for this function.
|
|
|
bdc76f |
|
|
|
bdc76f |
@table @code
|
|
|
bdc76f |
+@item ENOSYS
|
|
|
bdc76f |
+The kernel does not implement the required functionality.
|
|
|
bdc76f |
+
|
|
|
bdc76f |
@item EISDIR
|
|
|
bdc76f |
At least one of the descriptors @var{inputfd} or @var{outputfd} refers
|
|
|
bdc76f |
to a directory.
|
|
|
bdc76f |
@@ -1437,9 +1440,6 @@ reading.
|
|
|
bdc76f |
|
|
|
bdc76f |
The argument @var{outputfd} is not a valid file descriptor open for
|
|
|
bdc76f |
writing, or @var{outputfd} has been opened with @code{O_APPEND}.
|
|
|
bdc76f |
-
|
|
|
bdc76f |
-@item EXDEV
|
|
|
bdc76f |
-The input and output files reside on different file systems.
|
|
|
bdc76f |
@end table
|
|
|
bdc76f |
|
|
|
bdc76f |
In addition, @code{copy_file_range} can fail with the error codes
|
|
|
bdc76f |
diff --git a/sysdeps/unix/sysv/linux/alpha/kernel-features.h b/sysdeps/unix/sysv/linux/alpha/kernel-features.h
|
|
|
bdc76f |
index 402d2573d75794d5..26344cd610a1f8e7 100644
|
|
|
bdc76f |
--- a/sysdeps/unix/sysv/linux/alpha/kernel-features.h
|
|
|
bdc76f |
+++ b/sysdeps/unix/sysv/linux/alpha/kernel-features.h
|
|
|
bdc76f |
@@ -48,7 +48,6 @@
|
|
|
bdc76f |
/* Support for copy_file_range, statx was added in kernel 4.13. */
|
|
|
bdc76f |
#if __LINUX_KERNEL_VERSION < 0x040D00
|
|
|
bdc76f |
# undef __ASSUME_MLOCK2
|
|
|
bdc76f |
-# undef __ASSUME_COPY_FILE_RANGE
|
|
|
bdc76f |
# undef __ASSUME_STATX
|
|
|
bdc76f |
#endif
|
|
|
bdc76f |
|
|
|
bdc76f |
diff --git a/sysdeps/unix/sysv/linux/copy_file_range.c b/sysdeps/unix/sysv/linux/copy_file_range.c
|
|
|
bdc76f |
index 7b1a50f7529f2a84..b88b7c9e2ecd825f 100644
|
|
|
bdc76f |
--- a/sysdeps/unix/sysv/linux/copy_file_range.c
|
|
|
bdc76f |
+++ b/sysdeps/unix/sysv/linux/copy_file_range.c
|
|
|
bdc76f |
@@ -20,27 +20,16 @@
|
|
|
bdc76f |
#include <sysdep-cancel.h>
|
|
|
bdc76f |
#include <unistd.h>
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* Include the fallback implementation. */
|
|
|
bdc76f |
-#ifndef __ASSUME_COPY_FILE_RANGE
|
|
|
bdc76f |
-#define COPY_FILE_RANGE_DECL static
|
|
|
bdc76f |
-#define COPY_FILE_RANGE copy_file_range_compat
|
|
|
bdc76f |
-#include <io/copy_file_range-compat.c>
|
|
|
bdc76f |
-#endif
|
|
|
bdc76f |
-
|
|
|
bdc76f |
ssize_t
|
|
|
bdc76f |
copy_file_range (int infd, __off64_t *pinoff,
|
|
|
bdc76f |
int outfd, __off64_t *poutoff,
|
|
|
bdc76f |
size_t length, unsigned int flags)
|
|
|
bdc76f |
{
|
|
|
bdc76f |
#ifdef __NR_copy_file_range
|
|
|
bdc76f |
- ssize_t ret = SYSCALL_CANCEL (copy_file_range, infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
- length, flags);
|
|
|
bdc76f |
-# ifndef __ASSUME_COPY_FILE_RANGE
|
|
|
bdc76f |
- if (ret == -1 && errno == ENOSYS)
|
|
|
bdc76f |
- ret = copy_file_range_compat (infd, pinoff, outfd, poutoff, length, flags);
|
|
|
bdc76f |
-# endif
|
|
|
bdc76f |
- return ret;
|
|
|
bdc76f |
-#else /* !__NR_copy_file_range */
|
|
|
bdc76f |
- return copy_file_range_compat (infd, pinoff, outfd, poutoff, length, flags);
|
|
|
bdc76f |
+ return SYSCALL_CANCEL (copy_file_range, infd, pinoff, outfd, poutoff,
|
|
|
bdc76f |
+ length, flags);
|
|
|
bdc76f |
+#else
|
|
|
bdc76f |
+ __set_errno (ENOSYS);
|
|
|
bdc76f |
+ return -1;
|
|
|
bdc76f |
#endif
|
|
|
bdc76f |
}
|
|
|
bdc76f |
diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h
|
|
|
bdc76f |
index 5543d92d7e32e501..7a74835495250268 100644
|
|
|
bdc76f |
--- a/sysdeps/unix/sysv/linux/kernel-features.h
|
|
|
bdc76f |
+++ b/sysdeps/unix/sysv/linux/kernel-features.h
|
|
|
bdc76f |
@@ -111,10 +111,6 @@
|
|
|
bdc76f |
# define __ASSUME_MLOCK2 1
|
|
|
bdc76f |
#endif
|
|
|
bdc76f |
|
|
|
bdc76f |
-#if __LINUX_KERNEL_VERSION >= 0x040500
|
|
|
bdc76f |
-# define __ASSUME_COPY_FILE_RANGE 1
|
|
|
bdc76f |
-#endif
|
|
|
bdc76f |
-
|
|
|
bdc76f |
/* Support for statx was added in kernel 4.11. */
|
|
|
bdc76f |
#if __LINUX_KERNEL_VERSION >= 0x040B00
|
|
|
bdc76f |
# define __ASSUME_STATX 1
|
|
|
bdc76f |
diff --git a/sysdeps/unix/sysv/linux/microblaze/kernel-features.h b/sysdeps/unix/sysv/linux/microblaze/kernel-features.h
|
|
|
bdc76f |
index e8e2ac6a873126ac..1c49f099b7993f1d 100644
|
|
|
bdc76f |
--- a/sysdeps/unix/sysv/linux/microblaze/kernel-features.h
|
|
|
bdc76f |
+++ b/sysdeps/unix/sysv/linux/microblaze/kernel-features.h
|
|
|
bdc76f |
@@ -58,11 +58,6 @@
|
|
|
bdc76f |
# undef __ASSUME_EXECVEAT
|
|
|
bdc76f |
#endif
|
|
|
bdc76f |
|
|
|
bdc76f |
-/* Support for the copy_file_range syscall was added in 4.10. */
|
|
|
bdc76f |
-#if __LINUX_KERNEL_VERSION < 0x040A00
|
|
|
bdc76f |
-# undef __ASSUME_COPY_FILE_RANGE
|
|
|
bdc76f |
-#endif
|
|
|
bdc76f |
-
|
|
|
bdc76f |
/* Support for statx was added in kernel 4.12. */
|
|
|
bdc76f |
#if __LINUX_KERNEL_VERSION < 0X040C00
|
|
|
bdc76f |
# undef __ASSUME_STATX
|