1 .\"This manpage is Copyright (C) 2015 Anna Schumaker <Anna.Schumaker@Netapp.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH COPY_FILE_RANGE 2 2021-08-27 "Linux" "Linux Programmer's Manual"
7 copy_file_range \- Copy a range of data from one file to another
10 .RI ( libc ", " \-lc )
13 .B #define _GNU_SOURCE
14 .B #include <unistd.h>
16 .BI "ssize_t copy_file_range(int " fd_in ", off64_t *" off_in ,
17 .BI " int " fd_out ", off64_t *" off_out ,
18 .BI " size_t " len ", unsigned int " flags );
22 .BR copy_file_range ()
23 system call performs an in-kernel copy between two file descriptors
24 without the additional cost of transferring data from the kernel to user space
25 and then back into the kernel.
28 bytes of data from the source file descriptor
30 to the target file descriptor
32 overwriting any data that exists within the requested range of the target file.
34 The following semantics apply for
36 and similar statements apply to
41 is NULL, then bytes are read from
43 starting from the file offset, and the file offset is
44 adjusted by the number of bytes copied.
50 must point to a buffer that specifies the starting
51 offset where bytes from
58 is adjusted appropriately.
63 can refer to the same file.
64 If they refer to the same file, then the source and target ranges are not
69 argument is provided to allow for future extensions
70 and currently must be set to 0.
72 Upon successful completion,
73 .BR copy_file_range ()
74 will return the number of bytes copied between files.
75 This could be less than the length originally requested.
78 is at or past the end of file, no bytes are copied, and
79 .BR copy_file_range ()
83 .BR copy_file_range ()
86 is set to indicate the error.
90 One or more file descriptors are not valid.
94 is not open for reading; or
96 is not open for writing.
101 flag is set for the open file description (see
103 referred to by the file descriptor
107 An attempt was made to write at a position past the maximum file offset the
111 An attempt was made to write a range that exceeds the allowed maximum file size.
112 The maximum file size differs between filesystem implementations and can be
113 different from the maximum allowed file offset.
116 An attempt was made to write beyond the process's file size resource limit.
117 This may also result in the process receiving a
130 refer to the same file and the source and target ranges overlap.
137 is not a regular file.
140 A low-level I/O error occurred while copying.
147 refers to a directory.
153 There is not enough space on the target filesystem to complete the copy.
156 The requested source or destination range is too large to represent in the
157 specified data types.
161 refers to an immutable file.
168 refers to an active swap file.
171 The files referred to by
172 .IR fd_in " and " fd_out
173 are not on the same mounted filesystem (pre Linux 5.3).
176 .BR copy_file_range ()
177 system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
178 emulation when it is not available.
179 .\" https://sourceware.org/git/?p=glibc.git;a=commit;f=posix/unistd.h;h=bad7a0c81f501fbbcc79af9eaa4b8254441c4a1f
181 A major rework of the kernel implementation occurred in 5.3.
182 Areas of the API that weren't clearly defined were clarified and the API bounds
183 are much more strictly checked than on earlier kernels.
184 Applications should target the behaviour and requirements of 5.3 kernels.
186 First support for cross-filesystem copies was introduced in Linux 5.3.
187 Older kernels will return -EXDEV when cross-filesystem copies are attempted.
190 .BR copy_file_range ()
191 system call is a nonstandard Linux and GNU extension.
195 is a sparse file, then
196 .BR copy_file_range ()
197 may expand any holes existing in the requested range.
198 Users may benefit from calling
199 .BR copy_file_range ()
200 in a loop, and using the
205 operations to find the locations of data segments.
207 .BR copy_file_range ()
208 gives filesystems an opportunity to implement "copy acceleration" techniques,
209 such as the use of reflinks (i.e., two or more inodes that share
210 pointers to the same copy-on-write disk blocks)
211 or server-side-copy (in the case of NFS).
218 #include <sys/stat.h>
222 main(int argc, char *argv[])
229 fprintf(stderr, "Usage: %s <source> <destination>\en", argv[0]);
233 fd_in = open(argv[1], O_RDONLY);
235 perror("open (argv[1])");
239 if (fstat(fd_in, &stat) == \-1) {
246 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
248 perror("open (argv[2])");
253 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
255 perror("copy_file_range");
260 } while (len > 0 && ret > 0);