Subject: bug#63850: cp fails for files > 2 GB if copy offload is unsupported

From: Pádraig Brady <[email-masked]>
Date: Fri, 2 Jun 2023 17:31:50 +0100

On 02/06/2023 16:44, Sam James wrote:
> Hello,
> 
> Forwarding a downstream report of a behaviour change between
> coreutils-9.1 and coreutils-9.3 from https://bugs.gentoo.org/907474.
> 
> The reporter bisected it to 093a8b4bfaba60005f14493ce7ef11ed665a0176
> ("copy: fix --reflink=auto to fallback in more cases", see bug#62404)
> and gave strace output showing:
> ```
> fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
> copy_file_range(3, NULL, 4, NULL, 9223372035781033984, 0) = 2147479552
> copy_file_range(3, NULL, 4, NULL, 9223372035781033984, 0) = -1 EINVAL
> (Invalid argument)
> ```
> 
> """
> When I try to copy a large file (> 2 GB) like so:
> 
> cp --debug file_a file_b
> 
> output looks like this:
> 
> 'file_a' -> 'file_b'
> cp: error copying 'file_a' to 'file_b': Invalid argument
> copy offload: unsupported, reflink: unsupported, sparse detection: no
> 
> Afterwards file_b has a size of 2147479552 bytes (= 2G - 4K).
> 
> On another system (with newer kernel version) it looks like this:
> 
> cp --debug file_a file_b
> 'file_a' -> 'file_b'
> copy offload: yes, reflink: unsupported, sparse detection: no
> """
> 
> Let me know if you need further information, although there's
> some more on the downstream Gentoo bug I linked.
> 
> Apparently this is only happening w/ the 4.19.x kernels.

I'm not sure it was working correctly before 9.3 either.
Before 9.3 we would have switched from copy_file_range() to read()/write()
upon receiving the EINVAL, which might have worked, but also I'm not sure
the file offsets would be correct in that case. Could you show the output with:

diff --git a/src/copy.c b/src/copy.c
index 0dd059d2e..35c54b905 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -363,7 +363,16 @@ sparse_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
                 edge case where the file is made immutable after creating,
                 in which case the (more accurate) error is still shown.  */
              if (*total_n_read == 0 && is_CLONENOTSUP (errno))
-              break;
+              {
+                if (*total_n_read != 0)
+                  {
+                    off_t clone_read_offset = lseek (src_fd, 0, SEEK_CUR);
+                    off_t clone_write_offset = lseek (dest_fd, 0, SEEK_CUR);
+                    printf ("switching to standard copy at :%"PRIdMAX" read=%"PRIdMAX" write=%"PRIdMAX"\n",
+                            *total_n_read, clone_read_offset, clone_write_offset);
+                  }
+                break;
+              }

              /* ENOENT was seen sometimes across CIFS shares, resulting in
                 no data being copied, but subsequent standard copies succeed.  */