file_unix_copy: copy with sendfile() between two filesystems
On Linux, the file worker copies with copy_file_range(). The kernel refuses it with EXDEV when the
source and the destination are on different filesystems, unless both are on the same NFS or CIFS
client. That sendfile path now covers many everyday copies: from the home folder to a USB stick or an external disk,
from one partition to another, and from /tmp. In other cases, the worker then falls back to read() and write()
through a 256 KiB buffer, so the data goes into user space and back.
What this does
When copy_file_range() fails with EXDEV, ENOSYS, EOPNOTSUPP or EINVAL, the copy continues with sendfile(). On Linux, sendfile() copies from a file to a file inside the kernel. It uses the same chunks, writeback window, progress and cancel checks as copy_file_range(). read() and write() remain the last fallback when sendfile() fails too. Any other error, such as EIO or ENOSPC, goes to read() and write() as before.
Numbers
A 1 GiB file copied with KIO::file_copy(), master against this branch, release builds, 5 rounds alternating. The source was in the page cache, and the time is until the job reports success.
| 1 GiB | master, median (range) | this branch, median (range) |
|---|---|---|
| tmpfs to ext4 | 443 ms (363 to 583) | 272 ms (242 to 312) |
| ext4 to tmpfs | 531 ms (303 to 698) | 278 ms (272 to 289) |
The branch was faster in every round in both directions. A copy limited by a slow device, such as a USB 2 stick, only uses less CPU and finishes no sooner.