Blame io/tst-copy_file_range.c

Packit 6c4009
/* Tests for copy_file_range.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <array_length.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <inttypes.h>
Packit Service 3e830d
#include <libgen.h>
Packit Service 3e830d
#include <poll.h>
Packit Service 3e830d
#include <sched.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit Service 3e830d
#include <support/namespace.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
#include <support/xunistd.h>
Packit Service 3e830d
#ifdef CLONE_NEWNS
Packit Service 3e830d
# include <sys/mount.h>
Packit Service 3e830d
#endif
Packit 6c4009
Packit 6c4009
/* Boolean flags which indicate whether to use pointers with explicit
Packit 6c4009
   output flags.  */
Packit 6c4009
static int do_inoff;
Packit 6c4009
static int do_outoff;
Packit 6c4009
Packit 6c4009
/* Name and descriptors of the input files.  Files are truncated and
Packit 6c4009
   reopened (with O_RDWR) between tests.  */
Packit 6c4009
static char *infile;
Packit 6c4009
static int infd;
Packit 6c4009
static char *outfile;
Packit 6c4009
static int outfd;
Packit 6c4009
Packit Service 3e830d
/* Like the above, but on a different file system.  xdevfile can be
Packit Service 3e830d
   NULL if no suitable file system has been found.  */
Packit Service 3e830d
static char *xdevfile;
Packit Service 3e830d
Packit 6c4009
/* Input and output offsets.  Set according to do_inoff and do_outoff
Packit 6c4009
   before the test.  The offsets themselves are always set to
Packit 6c4009
   zero.  */
Packit 6c4009
static off64_t inoff;
Packit 6c4009
static off64_t *pinoff;
Packit 6c4009
static off64_t outoff;
Packit 6c4009
static off64_t *poutoff;
Packit 6c4009
Packit Service 3e830d
/* These are a collection of copy sizes used in tests.  The selection
Packit Service 3e830d
   takes into account that the fallback implementation uses an
Packit Service 3e830d
   internal buffer of 8192 bytes.  */
Packit 6c4009
enum { maximum_size = 99999 };
Packit 6c4009
static const int typical_sizes[] =
Packit Service 3e830d
  { 0, 1, 2, 3, 1024, 2048, 4096, 8191, 8192, 8193, 16383, 16384, 16385,
Packit Service 3e830d
    maximum_size };
Packit 6c4009
Packit 6c4009
/* The random contents of this array can be used as a pattern to check
Packit 6c4009
   for correct write operations.  */
Packit 6c4009
static unsigned char random_data[maximum_size];
Packit 6c4009
Packit 6c4009
/* The size chosen by the test harness.  */
Packit 6c4009
static int current_size;
Packit 6c4009
Packit Service 3e830d
/* Maximum writable file offset.  Updated by find_maximum_offset
Packit Service 3e830d
   below.  */
Packit Service 3e830d
static off64_t maximum_offset;
Packit Service 3e830d
Packit Service 3e830d
/* Error code when crossing the offset.  */
Packit Service 3e830d
static int maximum_offset_errno;
Packit Service 3e830d
Packit Service 3e830d
/* If true: Writes which cross the limit will fail.  If false: Writes
Packit Service 3e830d
   which cross the limit will result in a partial write.  */
Packit Service 3e830d
static bool maximum_offset_hard_limit;
Packit Service 3e830d
Packit Service 3e830d
/* Fills maximum_offset etc. above.  Truncates outfd as a side
Packit Service 3e830d
   effect.  */
Packit Service 3e830d
static void
Packit Service 3e830d
find_maximum_offset (void)
Packit Service 3e830d
{
Packit Service 3e830d
  xftruncate (outfd, 0);
Packit Service 3e830d
  if (maximum_offset != 0)
Packit Service 3e830d
    return;
Packit Service 3e830d
Packit Service 3e830d
  uint64_t upper = -1;
Packit Service 3e830d
  upper >>= 1;                  /* Maximum of off64_t.  */
Packit Service 3e830d
  TEST_VERIFY ((off64_t) upper > 0);
Packit Service 3e830d
  TEST_VERIFY ((off64_t) (upper + 1) < 0);
Packit Service 3e830d
  if (lseek64 (outfd, upper, SEEK_SET) >= 0)
Packit Service 3e830d
    {
Packit Service 3e830d
      if (write (outfd, "", 1) == 1)
Packit Service 3e830d
        FAIL_EXIT1 ("created a file larger than the off64_t range");
Packit Service 3e830d
    }
Packit Service 3e830d
Packit Service 3e830d
  uint64_t lower = 1024 * 1024; /* A reasonable minimum file size.  */
Packit Service 3e830d
  /* Loop invariant: writing at lower succeeds, writing at upper fails.  */
Packit Service 3e830d
  while (lower + 1 < upper)
Packit Service 3e830d
    {
Packit Service 3e830d
      uint64_t middle = (lower + upper) / 2;
Packit Service 3e830d
      if (test_verbose > 0)
Packit Service 3e830d
        printf ("info: %s: remaining test range %" PRIu64 " .. %" PRIu64
Packit Service 3e830d
                ", probe at %" PRIu64 "\n", __func__, lower, upper, middle);
Packit Service 3e830d
      xftruncate (outfd, 0);
Packit Service 3e830d
      if (lseek64 (outfd, middle, SEEK_SET) >= 0
Packit Service 3e830d
          && write (outfd, "", 1) == 1)
Packit Service 3e830d
        lower = middle;
Packit Service 3e830d
      else
Packit Service 3e830d
        upper = middle;
Packit Service 3e830d
    }
Packit Service 3e830d
  TEST_VERIFY (lower + 1 == upper);
Packit Service 3e830d
  maximum_offset = lower;
Packit Service 3e830d
  printf ("info: maximum writable file offset: %" PRIu64 " (%" PRIx64 ")\n",
Packit Service 3e830d
          lower, lower);
Packit Service 3e830d
Packit Service 3e830d
  /* Check that writing at the valid offset actually works.  */
Packit Service 3e830d
  xftruncate (outfd, 0);
Packit Service 3e830d
  xlseek (outfd, lower, SEEK_SET);
Packit Service 3e830d
  TEST_COMPARE (write (outfd, "", 1), 1);
Packit Service 3e830d
Packit Service 3e830d
  /* Cross the boundary with a two-byte write.  This can either result
Packit Service 3e830d
     in a short write, or a failure.  */
Packit Service 3e830d
  xlseek (outfd, lower, SEEK_SET);
Packit Service 3e830d
  ssize_t ret = write (outfd, " ", 2);
Packit Service 3e830d
  if (ret < 0)
Packit Service 3e830d
    {
Packit Service 3e830d
      maximum_offset_errno = errno;
Packit Service 3e830d
      maximum_offset_hard_limit = true;
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    maximum_offset_hard_limit = false;
Packit Service 3e830d
Packit Service 3e830d
  /* Check that writing at the next offset actually fails.  This also
Packit Service 3e830d
     obtains the expected errno value.  */
Packit Service 3e830d
  xftruncate (outfd, 0);
Packit Service 3e830d
  const char *action;
Packit Service 3e830d
  if (lseek64 (outfd, lower + 1, SEEK_SET) != 0)
Packit Service 3e830d
    {
Packit Service 3e830d
      if (write (outfd, "", 1) != -1)
Packit Service 3e830d
        FAIL_EXIT1 ("write to impossible offset %" PRIu64 " succeeded",
Packit Service 3e830d
                    lower + 1);
Packit Service 3e830d
      action = "writing";
Packit Service 3e830d
      int errno_copy = errno;
Packit Service 3e830d
      if (maximum_offset_hard_limit)
Packit Service 3e830d
        TEST_COMPARE (errno_copy, maximum_offset_errno);
Packit Service 3e830d
      else
Packit Service 3e830d
        maximum_offset_errno = errno_copy;
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    {
Packit Service 3e830d
      action = "seeking";
Packit Service 3e830d
      maximum_offset_errno = errno;
Packit Service 3e830d
    }
Packit Service 3e830d
  printf ("info: %s out of range fails with %m (%d)\n",
Packit Service 3e830d
          action, maximum_offset_errno);
Packit Service 3e830d
Packit Service 3e830d
  xftruncate (outfd, 0);
Packit Service 3e830d
  xlseek (outfd, 0, SEEK_SET);
Packit Service 3e830d
}
Packit Service 3e830d
Packit 6c4009
/* Perform a copy of a file.  */
Packit 6c4009
static void
Packit 6c4009
simple_file_copy (void)
Packit 6c4009
{
Packit 6c4009
  xwrite (infd, random_data, current_size);
Packit 6c4009
Packit 6c4009
  int length;
Packit 6c4009
  int in_skipped; /* Expected skipped bytes in input.  */
Packit 6c4009
  if (do_inoff)
Packit 6c4009
    {
Packit 6c4009
      xlseek (infd, 1, SEEK_SET);
Packit 6c4009
      inoff = 2;
Packit 6c4009
      length = current_size - 3;
Packit 6c4009
      in_skipped = 2;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      xlseek (infd, 3, SEEK_SET);
Packit 6c4009
      length = current_size - 5;
Packit 6c4009
      in_skipped = 3;
Packit 6c4009
    }
Packit 6c4009
  int out_skipped; /* Expected skipped bytes before the written data.  */
Packit 6c4009
  if (do_outoff)
Packit 6c4009
    {
Packit 6c4009
      xlseek (outfd, 4, SEEK_SET);
Packit 6c4009
      outoff = 5;
Packit 6c4009
      out_skipped = 5;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      xlseek (outfd, 6, SEEK_SET);
Packit 6c4009
      length = current_size - 6;
Packit 6c4009
      out_skipped = 6;
Packit 6c4009
    }
Packit 6c4009
  if (length < 0)
Packit 6c4009
    length = 0;
Packit 6c4009
Packit 6c4009
  TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                 length, 0), length);
Packit 6c4009
  if (do_inoff)
Packit 6c4009
    {
Packit 6c4009
      TEST_COMPARE (inoff, 2 + length);
Packit 6c4009
      TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 3 + length);
Packit 6c4009
  if (do_outoff)
Packit 6c4009
    {
Packit 6c4009
      TEST_COMPARE (outoff, 5 + length);
Packit 6c4009
      TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 4);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 6 + length);
Packit 6c4009
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  xfstat (outfd, &st);
Packit 6c4009
  if (length > 0)
Packit 6c4009
    TEST_COMPARE (st.st_size, out_skipped + length);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* If we did not write anything, we also did not add any
Packit 6c4009
         padding.  */
Packit 6c4009
      TEST_COMPARE (st.st_size, 0);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  xlseek (outfd, 0, SEEK_SET);
Packit 6c4009
  char *bytes = xmalloc (st.st_size);
Packit 6c4009
  TEST_COMPARE (read (outfd, bytes, st.st_size), st.st_size);
Packit 6c4009
  for (int i = 0; i < out_skipped; ++i)
Packit 6c4009
    TEST_COMPARE (bytes[i], 0);
Packit 6c4009
  TEST_VERIFY (memcmp (bytes + out_skipped, random_data + in_skipped,
Packit 6c4009
                       length) == 0);
Packit 6c4009
  free (bytes);
Packit 6c4009
}
Packit 6c4009
Packit Service 3e830d
/* Test that reading from a pipe willfails.  */
Packit Service 3e830d
static void
Packit Service 3e830d
pipe_as_source (void)
Packit Service 3e830d
{
Packit Service 3e830d
  int pipefds[2];
Packit Service 3e830d
  xpipe (pipefds);
Packit Service 3e830d
Packit Service 3e830d
  for (int length = 0; length < 2; ++length)
Packit Service 3e830d
    {
Packit Service 3e830d
      if (test_verbose > 0)
Packit Service 3e830d
        printf ("info: %s: length=%d\n", __func__, length);
Packit Service 3e830d
Packit Service 3e830d
      /* Make sure that there is something to copy in the pipe.  */
Packit Service 3e830d
      xwrite (pipefds[1], "@", 1);
Packit Service 3e830d
Packit Service 3e830d
      TEST_COMPARE (copy_file_range (pipefds[0], pinoff, outfd, poutoff,
Packit Service 3e830d
                                     length, 0), -1);
Packit Service 3e830d
      /* Linux 4.10 and later return EINVAL.  Older kernels return
Packit Service 3e830d
         EXDEV.  */
Packit Service 3e830d
      TEST_VERIFY (errno == EINVAL || errno == EXDEV);
Packit Service 3e830d
      TEST_COMPARE (inoff, 0);
Packit Service 3e830d
      TEST_COMPARE (outoff, 0);
Packit Service 3e830d
      TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
Packit Service 3e830d
Packit Service 3e830d
      /* Make sure that nothing was read.  */
Packit Service 3e830d
      char buf = 'A';
Packit Service 3e830d
      TEST_COMPARE (read (pipefds[0], &buf, 1), 1);
Packit Service 3e830d
      TEST_COMPARE (buf, '@');
Packit Service 3e830d
    }
Packit Service 3e830d
Packit Service 3e830d
  xclose (pipefds[0]);
Packit Service 3e830d
  xclose (pipefds[1]);
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Test that writing to a pipe fails.  */
Packit Service 3e830d
static void
Packit Service 3e830d
pipe_as_destination (void)
Packit Service 3e830d
{
Packit Service 3e830d
  /* Make sure that there is something to read in the input file.  */
Packit Service 3e830d
  xwrite (infd, "abc", 3);
Packit Service 3e830d
  xlseek (infd, 0, SEEK_SET);
Packit Service 3e830d
Packit Service 3e830d
  int pipefds[2];
Packit Service 3e830d
  xpipe (pipefds);
Packit Service 3e830d
Packit Service 3e830d
  for (int length = 0; length < 2; ++length)
Packit Service 3e830d
    {
Packit Service 3e830d
      if (test_verbose > 0)
Packit Service 3e830d
        printf ("info: %s: length=%d\n", __func__, length);
Packit Service 3e830d
Packit Service 3e830d
      TEST_COMPARE (copy_file_range (infd, pinoff, pipefds[1], poutoff,
Packit Service 3e830d
                                     length, 0), -1);
Packit Service 3e830d
      /* Linux 4.10 and later return EINVAL.  Older kernels return
Packit Service 3e830d
         EXDEV.  */
Packit Service 3e830d
      TEST_VERIFY (errno == EINVAL || errno == EXDEV);
Packit Service 3e830d
      TEST_COMPARE (inoff, 0);
Packit Service 3e830d
      TEST_COMPARE (outoff, 0);
Packit Service 3e830d
      TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
Packit Service 3e830d
Packit Service 3e830d
      /* Make sure that nothing was written.  */
Packit Service 3e830d
      struct pollfd pollfd = { .fd = pipefds[0], .events = POLLIN, };
Packit Service 3e830d
      TEST_COMPARE (poll (&pollfd, 1, 0), 0);
Packit Service 3e830d
    }
Packit Service 3e830d
Packit Service 3e830d
  xclose (pipefds[0]);
Packit Service 3e830d
  xclose (pipefds[1]);
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Test a write failure after (potentially) writing some bytes.
Packit Service 3e830d
   Failure occurs near the start of the buffer.  */
Packit Service 3e830d
static void
Packit Service 3e830d
delayed_write_failure_beginning (void)
Packit Service 3e830d
{
Packit Service 3e830d
  /* We need to write something to provoke the error.  */
Packit Service 3e830d
  if (current_size == 0)
Packit Service 3e830d
    return;
Packit Service 3e830d
  xwrite (infd, random_data, sizeof (random_data));
Packit Service 3e830d
  xlseek (infd, 0, SEEK_SET);
Packit Service 3e830d
Packit Service 3e830d
  /* Write failure near the start.  The actual error code varies among
Packit Service 3e830d
     file systems.  */
Packit Service 3e830d
  find_maximum_offset ();
Packit Service 3e830d
  off64_t where = maximum_offset;
Packit Service 3e830d
Packit Service 3e830d
  if (current_size == 1)
Packit Service 3e830d
    ++where;
Packit Service 3e830d
  outoff = where;
Packit Service 3e830d
  if (do_outoff)
Packit Service 3e830d
    xlseek (outfd, 1, SEEK_SET);
Packit Service 3e830d
  else
Packit Service 3e830d
    xlseek (outfd, where, SEEK_SET);
Packit Service 3e830d
  if (maximum_offset_hard_limit || where > maximum_offset)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit Service 3e830d
                                     sizeof (random_data), 0), -1);
Packit Service 3e830d
      TEST_COMPARE (errno, maximum_offset_errno);
Packit Service 3e830d
      TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
Packit Service 3e830d
      TEST_COMPARE (inoff, 0);
Packit Service 3e830d
      if (do_outoff)
Packit Service 3e830d
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1);
Packit Service 3e830d
      else
Packit Service 3e830d
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where);
Packit Service 3e830d
      TEST_COMPARE (outoff, where);
Packit Service 3e830d
      struct stat64 st;
Packit Service 3e830d
      xfstat (outfd, &st);
Packit Service 3e830d
      TEST_COMPARE (st.st_size, 0);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    {
Packit Service 3e830d
      /* The offset is not a hard limit.  This means we write one
Packit Service 3e830d
         byte.  */
Packit Service 3e830d
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit Service 3e830d
                                     sizeof (random_data), 0), 1);
Packit Service 3e830d
      if (do_inoff)
Packit Service 3e830d
        {
Packit Service 3e830d
          TEST_COMPARE (inoff, 1);
Packit Service 3e830d
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
Packit Service 3e830d
        }
Packit Service 3e830d
      else
Packit Service 3e830d
        {
Packit Service 3e830d
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1);
Packit Service 3e830d
          TEST_COMPARE (inoff, 0);
Packit Service 3e830d
        }
Packit Service 3e830d
      if (do_outoff)
Packit Service 3e830d
        {
Packit Service 3e830d
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 1);
Packit Service 3e830d
          TEST_COMPARE (outoff, where + 1);
Packit Service 3e830d
        }
Packit Service 3e830d
      else
Packit Service 3e830d
        {
Packit Service 3e830d
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), where + 1);
Packit Service 3e830d
          TEST_COMPARE (outoff, where);
Packit Service 3e830d
        }
Packit Service 3e830d
      struct stat64 st;
Packit Service 3e830d
      xfstat (outfd, &st);
Packit Service 3e830d
      TEST_COMPARE (st.st_size, where + 1);
Packit Service 3e830d
    }
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Test a write failure after (potentially) writing some bytes.
Packit Service 3e830d
   Failure occurs near the end of the buffer.  */
Packit Service 3e830d
static void
Packit Service 3e830d
delayed_write_failure_end (void)
Packit Service 3e830d
{
Packit Service 3e830d
  if (current_size <= 1)
Packit Service 3e830d
    /* This would be same as the first test because there is not
Packit Service 3e830d
       enough data to write to make a difference.  */
Packit Service 3e830d
    return;
Packit Service 3e830d
  xwrite (infd, random_data, sizeof (random_data));
Packit Service 3e830d
  xlseek (infd, 0, SEEK_SET);
Packit Service 3e830d
Packit Service 3e830d
  find_maximum_offset ();
Packit Service 3e830d
  off64_t where = maximum_offset - current_size + 1;
Packit Service 3e830d
  if (current_size == sizeof (random_data))
Packit Service 3e830d
    /* Otherwise we do not reach the non-writable byte.  */
Packit Service 3e830d
    ++where;
Packit Service 3e830d
  outoff = where;
Packit Service 3e830d
  if (do_outoff)
Packit Service 3e830d
    xlseek (outfd, 1, SEEK_SET);
Packit Service 3e830d
  else
Packit Service 3e830d
    xlseek (outfd, where, SEEK_SET);
Packit Service 3e830d
  ssize_t ret = copy_file_range (infd, pinoff, outfd, poutoff,
Packit Service 3e830d
                                 sizeof (random_data), 0);
Packit Service 3e830d
  if (ret < 0)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (ret, -1);
Packit Service 3e830d
      TEST_COMPARE (errno, maximum_offset_errno);
Packit Service 3e830d
      struct stat64 st;
Packit Service 3e830d
      xfstat (outfd, &st);
Packit Service 3e830d
      TEST_COMPARE (st.st_size, 0);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    {
Packit Service 3e830d
      /* The first copy succeeded.  This happens in the emulation
Packit Service 3e830d
         because the internal buffer of limited size does not
Packit Service 3e830d
         necessarily cross the off64_t boundary on the first write
Packit Service 3e830d
         operation.  */
Packit Service 3e830d
      if (test_verbose > 0)
Packit Service 3e830d
        printf ("info:   copy_file_range (%zu) returned %zd\n",
Packit Service 3e830d
                sizeof (random_data), ret);
Packit Service 3e830d
      TEST_VERIFY (ret > 0);
Packit Service 3e830d
      TEST_VERIFY (ret < maximum_size);
Packit Service 3e830d
      struct stat64 st;
Packit Service 3e830d
      xfstat (outfd, &st);
Packit Service 3e830d
      TEST_COMPARE (st.st_size, where + ret);
Packit Service 3e830d
      if (do_inoff)
Packit Service 3e830d
        {
Packit Service 3e830d
          TEST_COMPARE (inoff, ret);
Packit Service 3e830d
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
Packit Service 3e830d
        }
Packit Service 3e830d
      else
Packit Service 3e830d
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), ret);
Packit Service 3e830d
Packit Service 3e830d
      char *buffer = xmalloc (ret);
Packit Service 3e830d
      TEST_COMPARE (pread64 (outfd, buffer, ret, where), ret);
Packit Service 3e830d
      TEST_VERIFY (memcmp (buffer, random_data, ret) == 0);
Packit Service 3e830d
      free (buffer);
Packit Service 3e830d
Packit Service 3e830d
      /* The second copy fails.  */
Packit Service 3e830d
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit Service 3e830d
                                     sizeof (random_data), 0), -1);
Packit Service 3e830d
      TEST_COMPARE (errno, maximum_offset_errno);
Packit Service 3e830d
    }
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Test a write failure across devices.  */
Packit Service 3e830d
static void
Packit Service 3e830d
cross_device_failure (void)
Packit Service 3e830d
{
Packit Service 3e830d
  if (xdevfile == NULL)
Packit Service 3e830d
    /* Subtest not supported due to missing cross-device file.  */
Packit Service 3e830d
    return;
Packit Service 3e830d
Packit Service 3e830d
  /* We need something to write.  */
Packit Service 3e830d
  xwrite (infd, random_data, sizeof (random_data));
Packit Service 3e830d
  xlseek (infd, 0, SEEK_SET);
Packit Service 3e830d
Packit Service 3e830d
  int xdevfd = xopen (xdevfile, O_RDWR | O_LARGEFILE, 0);
Packit Service 3e830d
  TEST_COMPARE (copy_file_range (infd, pinoff, xdevfd, poutoff,
Packit Service 3e830d
                                 current_size, 0), -1);
Packit Service 3e830d
  TEST_COMPARE (errno, EXDEV);
Packit Service 3e830d
  TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 0);
Packit Service 3e830d
  struct stat64 st;
Packit Service 3e830d
  xfstat (xdevfd, &st);
Packit Service 3e830d
  TEST_COMPARE (st.st_size, 0);
Packit Service 3e830d
Packit Service 3e830d
  xclose (xdevfd);
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Try to exercise ENOSPC behavior with a tempfs file system (so that
Packit Service 3e830d
   we do not have to fill up a regular file system to get the error).
Packit Service 3e830d
   This function runs in a subprocess, so that we do not change the
Packit Service 3e830d
   mount namespace of the actual test process.  */
Packit Service 3e830d
static void
Packit Service 3e830d
enospc_failure_1 (void *closure)
Packit Service 3e830d
{
Packit Service 3e830d
#ifdef CLONE_NEWNS
Packit Service 3e830d
  support_become_root ();
Packit Service 3e830d
Packit Service 3e830d
  /* Make sure that we do not alter the file system mounts of the
Packit Service 3e830d
     parents.  */
Packit Service 3e830d
  if (! support_enter_mount_namespace ())
Packit Service 3e830d
    {
Packit Service 3e830d
      printf ("warning: ENOSPC test skipped\n");
Packit Service 3e830d
      return;
Packit Service 3e830d
    }
Packit Service 3e830d
Packit Service 3e830d
  char *mountpoint = closure;
Packit Service 3e830d
  if (mount ("none", mountpoint, "tmpfs", MS_NODEV | MS_NOEXEC,
Packit Service 3e830d
             "size=500k") != 0)
Packit Service 3e830d
    {
Packit Service 3e830d
      printf ("warning: could not mount tmpfs at %s: %m\n", mountpoint);
Packit Service 3e830d
      return;
Packit Service 3e830d
    }
Packit Service 3e830d
Packit Service 3e830d
  /* The source file must reside on the same file system.  */
Packit Service 3e830d
  char *intmpfsfile = xasprintf ("%s/%s", mountpoint, "in");
Packit Service 3e830d
  int intmpfsfd = xopen (intmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
Packit Service 3e830d
  xwrite (intmpfsfd, random_data, sizeof (random_data));
Packit Service 3e830d
  xlseek (intmpfsfd, 1, SEEK_SET);
Packit Service 3e830d
  inoff = 1;
Packit Service 3e830d
Packit Service 3e830d
  char *outtmpfsfile = xasprintf ("%s/%s", mountpoint, "out");
Packit Service 3e830d
  int outtmpfsfd = xopen (outtmpfsfile, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
Packit Service 3e830d
Packit Service 3e830d
  /* Fill the file with data until ENOSPC is reached.  */
Packit Service 3e830d
  while (true)
Packit Service 3e830d
    {
Packit Service 3e830d
      ssize_t ret = write (outtmpfsfd, random_data, sizeof (random_data));
Packit Service 3e830d
      if (ret < 0 && errno != ENOSPC)
Packit Service 3e830d
        FAIL_EXIT1 ("write to %s: %m", outtmpfsfile);
Packit Service 3e830d
      if (ret < sizeof (random_data))
Packit Service 3e830d
        break;
Packit Service 3e830d
    }
Packit Service 3e830d
  TEST_COMPARE (write (outtmpfsfd, "", 1), -1);
Packit Service 3e830d
  TEST_COMPARE (errno, ENOSPC);
Packit Service 3e830d
  off64_t maxsize = xlseek (outtmpfsfd, 0, SEEK_CUR);
Packit Service 3e830d
  TEST_VERIFY_EXIT (maxsize > sizeof (random_data));
Packit Service 3e830d
Packit Service 3e830d
  /* Constructed the expected file contents.  */
Packit Service 3e830d
  char *expected = xmalloc (maxsize);
Packit Service 3e830d
  TEST_COMPARE (pread64 (outtmpfsfd, expected, maxsize, 0), maxsize);
Packit Service 3e830d
  /* Go back a little, so some bytes can be written.  */
Packit Service 3e830d
  enum { offset = 20000 };
Packit Service 3e830d
  TEST_VERIFY_EXIT (offset < maxsize);
Packit Service 3e830d
  TEST_VERIFY_EXIT (offset < sizeof (random_data));
Packit Service 3e830d
  memcpy (expected + maxsize - offset, random_data + 1, offset);
Packit Service 3e830d
Packit Service 3e830d
  if (do_outoff)
Packit Service 3e830d
    {
Packit Service 3e830d
      outoff = maxsize - offset;
Packit Service 3e830d
      xlseek (outtmpfsfd, 2, SEEK_SET);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    xlseek (outtmpfsfd, -offset, SEEK_CUR);
Packit Service 3e830d
Packit Service 3e830d
  /* First call is expected to succeed because we made room for some
Packit Service 3e830d
     bytes.  */
Packit Service 3e830d
  TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff,
Packit Service 3e830d
                                 maximum_size, 0), offset);
Packit Service 3e830d
  if (do_inoff)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (inoff, 1 + offset);
Packit Service 3e830d
      TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
      TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset);
Packit Service 3e830d
  if (do_outoff)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (outoff, maxsize);
Packit Service 3e830d
      TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize);
Packit Service 3e830d
  struct stat64 st;
Packit Service 3e830d
  xfstat (outtmpfsfd, &st);
Packit Service 3e830d
  TEST_COMPARE (st.st_size, maxsize);
Packit Service 3e830d
  char *actual = xmalloc (st.st_size);
Packit Service 3e830d
  TEST_COMPARE (pread64 (outtmpfsfd, actual, st.st_size, 0), st.st_size);
Packit Service 3e830d
  TEST_VERIFY (memcmp (expected, actual, maxsize) == 0);
Packit Service 3e830d
Packit Service 3e830d
  /* Second call should fail with ENOSPC.  */
Packit Service 3e830d
  TEST_COMPARE (copy_file_range (intmpfsfd, pinoff, outtmpfsfd, poutoff,
Packit Service 3e830d
                                 maximum_size, 0), -1);
Packit Service 3e830d
  TEST_COMPARE (errno, ENOSPC);
Packit Service 3e830d
Packit Service 3e830d
  /* Offsets should be unchanged.  */
Packit Service 3e830d
  if (do_inoff)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (inoff, 1 + offset);
Packit Service 3e830d
      TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    TEST_COMPARE (xlseek (intmpfsfd, 0, SEEK_CUR), 1 + offset);
Packit Service 3e830d
  if (do_outoff)
Packit Service 3e830d
    {
Packit Service 3e830d
      TEST_COMPARE (outoff, maxsize);
Packit Service 3e830d
      TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), 2);
Packit Service 3e830d
    }
Packit Service 3e830d
  else
Packit Service 3e830d
    TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_CUR), maxsize);
Packit Service 3e830d
  TEST_COMPARE (xlseek (outtmpfsfd, 0, SEEK_END), maxsize);
Packit Service 3e830d
  TEST_COMPARE (pread64 (outtmpfsfd, actual, maxsize, 0), maxsize);
Packit Service 3e830d
  TEST_VERIFY (memcmp (expected, actual, maxsize) == 0);
Packit Service 3e830d
Packit Service 3e830d
  free (actual);
Packit Service 3e830d
  free (expected);
Packit Service 3e830d
Packit Service 3e830d
  xclose (intmpfsfd);
Packit Service 3e830d
  xclose (outtmpfsfd);
Packit Service 3e830d
  free (intmpfsfile);
Packit Service 3e830d
  free (outtmpfsfile);
Packit Service 3e830d
Packit Service 3e830d
#else /* !CLONE_NEWNS */
Packit Service 3e830d
  puts ("warning: ENOSPC test skipped (no mount namespaces)");
Packit Service 3e830d
#endif
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* Call enospc_failure_1 in a subprocess.  */
Packit Service 3e830d
static void
Packit Service 3e830d
enospc_failure (void)
Packit Service 3e830d
{
Packit Service 3e830d
  char *mountpoint
Packit Service 3e830d
    = support_create_temp_directory ("tst-copy_file_range-enospc-");
Packit Service 3e830d
  support_isolate_in_subprocess (enospc_failure_1, mountpoint);
Packit Service 3e830d
  free (mountpoint);
Packit Service 3e830d
}
Packit Service 3e830d
Packit Service 3e830d
/* The target file descriptor must have O_APPEND enabled.  */
Packit Service 3e830d
static void
Packit Service 3e830d
oappend_failure (void)
Packit Service 3e830d
{
Packit Service 3e830d
  /* Add data, to make sure we do not fail because there is
Packit Service 3e830d
     insufficient input data.  */
Packit Service 3e830d
  xwrite (infd, random_data, current_size);
Packit Service 3e830d
  xlseek (infd, 0, SEEK_SET);
Packit Service 3e830d
Packit Service 3e830d
  xclose (outfd);
Packit Service 3e830d
  outfd = xopen (outfile, O_RDWR | O_APPEND, 0);
Packit Service 3e830d
  TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit Service 3e830d
                                 current_size, 0), -1);
Packit Service 3e830d
  TEST_COMPARE (errno, EBADF);
Packit Service 3e830d
}
Packit Service 3e830d
Packit 6c4009
/* Test that a short input file results in a shortened copy.  */
Packit 6c4009
static void
Packit 6c4009
short_copy (void)
Packit 6c4009
{
Packit 6c4009
  if (current_size == 0)
Packit 6c4009
    /* Nothing to shorten.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Two subtests, one with offset 0 and current_size - 1 bytes, and
Packit 6c4009
     another one with current_size bytes, but offset 1.  */
Packit 6c4009
  for (int shift = 0; shift < 2; ++shift)
Packit 6c4009
    {
Packit 6c4009
      if (test_verbose > 0)
Packit 6c4009
        printf ("info:   shift=%d\n", shift);
Packit 6c4009
      xftruncate (infd, 0);
Packit 6c4009
      xlseek (infd, 0, SEEK_SET);
Packit 6c4009
      xwrite (infd, random_data, current_size - !shift);
Packit 6c4009
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          inoff = shift;
Packit 6c4009
          xlseek (infd, 2, SEEK_SET);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        {
Packit 6c4009
          inoff = 3;
Packit 6c4009
          xlseek (infd, shift, SEEK_SET);
Packit 6c4009
        }
Packit 6c4009
      ftruncate (outfd, 0);
Packit 6c4009
      xlseek (outfd, 0, SEEK_SET);
Packit 6c4009
      outoff = 0;
Packit 6c4009
Packit 6c4009
      /* First call copies current_size - 1 bytes.  */
Packit 6c4009
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                     current_size, 0), current_size - 1);
Packit 6c4009
      char *buffer = xmalloc (current_size);
Packit 6c4009
      TEST_COMPARE (pread64 (outfd, buffer, current_size, 0),
Packit 6c4009
                    current_size - 1);
Packit 6c4009
      TEST_VERIFY (memcmp (buffer, random_data + shift, current_size - 1)
Packit 6c4009
                   == 0);
Packit 6c4009
      free (buffer);
Packit 6c4009
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (inoff, current_size - 1 + shift);
Packit 6c4009
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift);
Packit 6c4009
      if (do_outoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (outoff, current_size - 1);
Packit 6c4009
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1);
Packit 6c4009
Packit 6c4009
      /* First call copies zero bytes.  */
Packit 6c4009
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                     current_size, 0), 0);
Packit 6c4009
      /* And the offsets are unchanged.  */
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (inoff, current_size - 1 + shift);
Packit 6c4009
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift);
Packit 6c4009
      if (do_outoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (outoff, current_size - 1);
Packit 6c4009
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* A named test function.  */
Packit 6c4009
struct test_case
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  void (*func) (void);
Packit 6c4009
  bool sizes; /* If true, call the test with different current_size values.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* The available test cases.  */
Packit 6c4009
static struct test_case tests[] =
Packit 6c4009
  {
Packit 6c4009
    { "simple_file_copy", simple_file_copy, .sizes = true },
Packit Service 3e830d
    { "pipe_as_source", pipe_as_source, },
Packit Service 3e830d
    { "pipe_as_destination", pipe_as_destination, },
Packit Service 3e830d
    { "delayed_write_failure_beginning", delayed_write_failure_beginning,
Packit Service 3e830d
      .sizes = true },
Packit Service 3e830d
    { "delayed_write_failure_end", delayed_write_failure_end, .sizes = true },
Packit Service 3e830d
    { "cross_device_failure", cross_device_failure, .sizes = true },
Packit Service 3e830d
    { "enospc_failure", enospc_failure, },
Packit Service 3e830d
    { "oappend_failure", oappend_failure, .sizes = true },
Packit 6c4009
    { "short_copy", short_copy, .sizes = true },
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  for (unsigned char *p = random_data; p < array_end (random_data); ++p)
Packit 6c4009
    *p = rand () >> 24;
Packit 6c4009
Packit 6c4009
  infd = create_temp_file ("tst-copy_file_range-in-", &infile);
Packit Service 3e830d
  xclose (create_temp_file ("tst-copy_file_range-out-", &outfile));
Packit Service 3e830d
Packit Service 3e830d
  /* Try to find a different directory from the default input/output
Packit Service 3e830d
     file.  */
Packit 6c4009
  {
Packit Service 3e830d
    struct stat64 instat;
Packit Service 3e830d
    xfstat (infd, &instat);
Packit Service 3e830d
    static const char *const candidates[] =
Packit Service 3e830d
      { NULL, "/var/tmp", "/dev/shm" };
Packit Service 3e830d
    for (const char *const *c = candidates; c < array_end (candidates); ++c)
Packit Service 3e830d
      {
Packit Service 3e830d
        const char *path = *c;
Packit Service 3e830d
        char *to_free = NULL;
Packit Service 3e830d
        if (path == NULL)
Packit Service 3e830d
          {
Packit Service 3e830d
            to_free = xreadlink ("/proc/self/exe");
Packit Service 3e830d
            path = dirname (to_free);
Packit Service 3e830d
          }
Packit Service 3e830d
Packit Service 3e830d
        struct stat64 cstat;
Packit Service 3e830d
        xstat (path, &cstat);
Packit Service 3e830d
        if (cstat.st_dev == instat.st_dev)
Packit Service 3e830d
          {
Packit Service 3e830d
            free (to_free);
Packit Service 3e830d
            continue;
Packit Service 3e830d
          }
Packit Service 3e830d
Packit Service 3e830d
        printf ("info: using alternate temporary files directory: %s\n", path);
Packit Service 3e830d
        xdevfile = xasprintf ("%s/tst-copy_file_range-xdev-XXXXXX", path);
Packit Service 3e830d
        free (to_free);
Packit Service 3e830d
        break;
Packit Service 3e830d
      }
Packit Service 3e830d
    if (xdevfile != NULL)
Packit 6c4009
      {
Packit Service 3e830d
        int xdevfd = mkstemp (xdevfile);
Packit Service 3e830d
        if (xdevfd < 0)
Packit Service 3e830d
          FAIL_EXIT1 ("mkstemp (\"%s\"): %m", xdevfile);
Packit Service 3e830d
        struct stat64 xdevst;
Packit Service 3e830d
        xfstat (xdevfd, &xdevst);
Packit Service 3e830d
        TEST_VERIFY (xdevst.st_dev != instat.st_dev);
Packit Service 3e830d
        add_temp_file (xdevfile);
Packit Service 3e830d
        xclose (xdevfd);
Packit 6c4009
      }
Packit Service 3e830d
    else
Packit Service 3e830d
      puts ("warning: no alternate directory on different file system found");
Packit 6c4009
  }
Packit 6c4009
  xclose (infd);
Packit 6c4009
Packit 6c4009
  for (do_inoff = 0; do_inoff < 2; ++do_inoff)
Packit 6c4009
    for (do_outoff = 0; do_outoff < 2; ++do_outoff)
Packit 6c4009
      for (struct test_case *test = tests; test < array_end (tests); ++test)
Packit 6c4009
        for (const int *size = typical_sizes;
Packit 6c4009
             size < array_end (typical_sizes); ++size)
Packit 6c4009
          {
Packit 6c4009
            current_size = *size;
Packit 6c4009
            if (test_verbose > 0)
Packit 6c4009
              printf ("info: %s do_inoff=%d do_outoff=%d current_size=%d\n",
Packit 6c4009
                      test->name, do_inoff, do_outoff, current_size);
Packit 6c4009
Packit 6c4009
            inoff = 0;
Packit 6c4009
            if (do_inoff)
Packit 6c4009
              pinoff = &inof;;
Packit 6c4009
            else
Packit 6c4009
              pinoff = NULL;
Packit 6c4009
            outoff = 0;
Packit 6c4009
            if (do_outoff)
Packit 6c4009
              poutoff = &outoff;
Packit 6c4009
            else
Packit 6c4009
              poutoff = NULL;
Packit 6c4009
Packit 6c4009
            infd = xopen (infile, O_RDWR | O_LARGEFILE, 0);
Packit 6c4009
            xftruncate (infd, 0);
Packit 6c4009
            outfd = xopen (outfile, O_RDWR | O_LARGEFILE, 0);
Packit 6c4009
            xftruncate (outfd, 0);
Packit 6c4009
Packit 6c4009
            test->func ();
Packit 6c4009
Packit 6c4009
            xclose (infd);
Packit 6c4009
            xclose (outfd);
Packit 6c4009
Packit 6c4009
            if (!test->sizes)
Packit 6c4009
              /* Skip the other sizes unless they have been
Packit 6c4009
                 requested.  */
Packit 6c4009
              break;
Packit 6c4009
          }
Packit 6c4009
Packit 6c4009
  free (infile);
Packit 6c4009
  free (outfile);
Packit Service 3e830d
  free (xdevfile);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>