copy_file_range
copy_file_range is a Linux system call that copies a requested file range in the kernel and reports the number of bytes actually copied.
A successful call may copy fewer bytes than requested, and zero can indicate EOF. Implementations may accelerate copying with reflinks or server-side copy; success alone does not identify physical allocation. Sparse holes can expand. [1]
The call exists since Linux 4.5, and its flags argument must be 0. Linux 5.3 reworked the implementation, and from 5.3 to 5.18 the kernel copied across filesystems even without their support, which on some virtual filesystems reported success without copying. Since 5.19 a cross-filesystem copy needs two filesystems of the same type that implement it. [1]
In Linux v6.16 the kernel tries the filesystem's own copy method first, then a clone of the range on the same superblock, then an in-kernel splice copy. The clone and splice paths cap one call at MAX_RW_COUNT, INT_MAX rounded down to a page boundary, which is 2,147,479,552 bytes with 4 KiB pages. [3] [4]
Source and destination positions are selected independently. A NULL off_in or off_out uses and advances that file's current position; a non-NULL pointer advances the supplied value instead, leaving that file position unchanged. Distinct caller-owned offset variables can isolate range selection from shared descriptor cursors, but do not freeze source bytes or prevent overlapping destination writes. [1]
After a positive short return, continue from the updated offsets and subtract the copied count from the remaining work. Zero for a positive-count request indicates EOF: stop instead of retrying without progress. Resetting explicit offsets selects the original range again. Duplicating a descriptor alone does not provide a separate file position. [1] [5]
Relations
Advantages
- Avoids moving data through user-space buffers. [1]
Limitations
- glibc 2.27 to 2.29 supplied a user-space fallback, but since glibc 2.30 the function fails with ENOSYS on kernels without the system call. [1]
- On Linux 6.8.0-147-generic, EFAULT from a read-only explicit offset can follow copied bytes and an update to the other cursor. Blind retry can duplicate data or overwrite the previous output. [6, 7]
- O_APPEND on the destination open file description is rejected with EBADF, even when an explicit destination offset is supplied. [1]
Citations
- Linux man-pages: copy_file_range(2), independent offsets, short copies and append restriction
- Linux v6.16 fs/read_write.c: vfs_copy_file_range fallback order
- Linux v6.16 include/linux/fs.h: MAX_RW_COUNT
- Linux man-pages: dup(2), shared open file descriptions and file offsets
- Hesela: native offset-copyback failures, observed bytes and unsafe retries
- Linux v6.8: transfer and caller-offset copyback
Further reading
Sources & caveats
- linux-cfr is the Linux man-pages copy_file_range(2) page, read in full on the access date.
- read-write is fs/read_write.c at tag v6.16, read in vfs_copy_file_range and the copy_file_range system call.
- fs-header is include/linux/fs.h at tag v6.16, read for the MAX_RW_COUNT definition.
- 4.5, 5.3, 5.18, 5.19 and the glibc versions come from linux-cfr. 2,147,479,552 bytes is INT_MAX & PAGE_MASK with 4096-byte pages, from fs-header.
- The copyback supplement uses deliberately invalid caller offset buffers on one host, not a device-failure model or a guarantee about every error.