copy_file_range(2)
๐ copy_file_range โ the man page, but make it make sense ๐
okay bestie, so this is the copy_file_range(2) page from the System Calls Manual, and it's literally about copying a range of data from one file to another. that's the whole vibe. let's get into it ๐ฆ
โจ NAME โจ
copy_file_range โ Copy a range of data from one file to another
๐ธ LIBRARY ๐ธ
Standard C library (libc, -lc) โ yeah she lives in libc, no surprise there ๐
๐ SYNOPSIS ๐
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
ssize_t copy_file_range(int fd_in, off_t *_Nullable off_in,
int fd_out, off_t *_Nullable off_out,
size_t size, unsigned int flags);
๐ฆ DESCRIPTION ๐ฆ
so the copy_file_range() system call does an in-kernel copy between two file descriptors. and that's the slay part โ it skips the whole extra cost of shuffling data out of the kernel into user space and then back into the kernel again. no cap, it's giving efficiency ๐
it copies up to size bytes of data from the source file descriptor fd_in to the target file descriptor fd_out, and it overwrites any data that already exists inside the requested range of the target file. periodt.
here's how the semantics go for off_in โ and the same statements apply to off_out:
- if
off_inisNULL, then bytes get read fromfd_instarting from the file offset, and the file offset gets adjusted by however many bytes were copied. - if
off_inis notNULL, thenoff_inmust point to a buffer that says the starting offset where bytes fromfd_inwill be read. the file offset offd_indoes not change, butoff_ingets adjusted appropriately.
fd_in and fd_out can refer to the same file โ same girl, same file ๐ โ but if they do, the source and target ranges are not allowed to overlap. that's the rule, don't break it.
and the flags argument? it exists purely to allow for future extensions. currently it must be set to 0. that's it, that's the whole job ๐
lowkey a whole syscall whose entireflagsvalue is0. iconic.
๐ฅ RETURN VALUE ๐ฅ
on a successful completion, copy_file_range() returns the number of bytes copied between the files. and heads up: that can totally be less than the size you originally requested. if the file offset of fd_in is at or past the end of file, no bytes are copied at all, and copy_file_range() returns zero. zero bytes, zero problems ๐ซถ
on error, copy_file_range() returns -1 and errno gets set to indicate the error.
๐ ERRORS ๐
EBADFโ one or more file descriptors are not valid.EBADFโfd_inis not open for reading; orfd_outis not open for writing.EBADFโ theO_APPENDflag is set for the open file description (seeopen(2)) referred to by the file descriptorfd_out.EFBIGโ an attempt was made to write at a position past the maximum file offset the kernel supports.EFBIGโ an attempt was made to write a range that exceeds the allowed maximum file size. and btw, the maximum file size differs between filesystem implementations and can be different from the maximum allowed file offset.EFBIGโ an attempt was made to write beyond the process's file size resource limit. this may also result in the process receiving aSIGXFSZsignal. ๐ญEINVALโ theflagsargument is not0.EINVALโfd_inandfd_outrefer to the same file and the source and target ranges overlap.EINVALโ eitherfd_inorfd_outis not a regular file.EIOโ a low-level I/O error occurred while copying.EISDIRโ eitherfd_inorfd_outrefers to a directory.ENOMEMโ out of memory.ENOSPCโ there is not enough space on the target filesystem to complete the copy.EOPNOTSUPPโ (since Linux 5.19) the filesystem does not support this operation.EOVERFLOWโ the requested source or destination range is too large to represent in the specified data types.EPERMโfd_outrefers to an immutable file.ETXTBSYโ eitherfd_inorfd_outrefers to an active swap file.EXDEVโ (before Linux 5.3) the files referred to byfd_inandfd_outare not on the same filesystem.EXDEVโ (since Linux 5.19) the files referred to byfd_inandfd_outare not on the same filesystem, and the source and target filesystems are not of the same type, or do not support cross-filesystem copy.
not all of those are your fault, some of them are just the filesystem not being supportive. we've all been there ๐
๐ VERSIONS ๐
a major rework of the kernel implementation happened in Linux 5.3. the parts of the API that weren't clearly defined got clarified, and the API bounds are checked much more strictly than they were on earlier kernels. glow-up fr fr โจ
since Linux 5.19, cross-filesystem copies can be achieved when both filesystems are of the same type, and that filesystem implements support for it. see BUGS for the behavior prior to Linux 5.19.
and listen: applications should target the behavior and requirements of Linux 5.19, which was also backported to earlier stable kernels. so that's the version to write against, bestie ๐
๐ STANDARDS ๐
Linux, GNU.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
Linux 4.5.
before glibc 2.30 โ starting with glibc 2.27 โ the glibc wrapper function provided a user-space fallback implementation for when the kernel didn't implement this system call. since glibc 2.30, that fallback has been removed; the function now fails with ENOSYS if the kernel lacks support for copy_file_range(). rip fallback, you did your best ๐ซก
๐ NOTES ๐
if fd_in is a sparse file, then copy_file_range() may expand any holes existing in the requested range. so users may benefit from calling copy_file_range() in a loop, and using the lseek(2) SEEK_DATA and SEEK_HOLE operations to find the locations of data segments. that's the move ๐ง
copy_file_range() gives filesystems an opportunity to implement "copy acceleration" techniques, such as the use of reflinks (i.e., two or more inodes that share pointers to the same copy-on-write disk blocks) or server-side-copy (in the case of NFS). it's giving cloned inodes, shared blocks, we love to see it ๐ฅ
_FILE_OFFSET_BITS should be defined to be 64 in code that uses non-null off_in or off_out or that takes the address of copy_file_range, if that code is intended to be portable to traditional 32-bit x86 and ARM platforms where off_t's width defaults to 32 bits. write it down, you'll forget iykyk ๐
๐ BUGS ๐
in Linux 5.3 to Linux 5.18, cross-filesystem copies were implemented by the kernel if the operation wasn't supported by individual filesystems. however, on some virtual filesystems, the call failed to copy while still reporting success. ๐๐๐ it lied. it said yes and did nothing. the audacity.
so if you're on those kernels and something looks suspiciously fine โ double check it, bestie. not me trusting a successful return on a virtual filesystem ๐
๐ EXAMPLES ๐
here's the whole thing, copy-paste ready:
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int fd_in, fd_out;
off_t size, ret;
struct stat stat;
if (argc != 3) {
fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
exit(EXIT_FAILURE);
}
fd_in = open(argv[1], O_RDONLY);
if (fd_in == -1) {
perror("open (argv[1])");
exit(EXIT_FAILURE);
}
if (fstat(fd_in, &stat) == -1) {
perror("fstat");
exit(EXIT_FAILURE);
}
size = stat.st_size;
fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd_out == -1) {
perror("open (argv[2])");
exit(EXIT_FAILURE);
}
do {
ret = copy_file_range(fd_in, NULL, fd_out, NULL, size, 0);
if (ret == -1) {
perror("copy_file_range");
exit(EXIT_FAILURE);
}
size -= ret;
} while (size > 0 && ret > 0);
close(fd_in);
close(fd_out);
exit(EXIT_SUCCESS);
}
see the do { } while (size > 0 && ret > 0); loop? that's the little dance you gotta do, because a single call can return fewer bytes than you asked for. keep going till it's done, main character energy only ๐
๐ธ SEE ALSO ๐ธ
and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-05-14, and it's the copy_file_range(2) girlies' moment to shine. go copy some ranges (in-kernel, overlapping-free, flags set to 0) and slay ๐๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2015, Anna Schumaker <Anna.Schumaker@Netapp.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms