Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / copy_file_range.2
blob60b5cf295698bdadff7e86a5a442ef8700138467
1 .\"This manpage is Copyright (C) 2015 Anna Schumaker <Anna.Schumaker@Netapp.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of
9 .\" this manual under the conditions for verbatim copying, provided that
10 .\" the entire resulting derived work is distributed under the terms of
11 .\" a permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume
15 .\" no responsibility for errors or omissions, or for damages resulting
16 .\" from the use of the information contained herein.  The author(s) may
17 .\" not have taken the same level of care in the production of this
18 .\" manual, which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH COPY_FILE_RANGE 2 2021-08-27 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 copy_file_range \- Copy a range of data from one file to another
28 .SH SYNOPSIS
29 .nf
30 .B #define _GNU_SOURCE
31 .B #include <unistd.h>
32 .PP
33 .BI "ssize_t copy_file_range(int " fd_in ", off64_t *" off_in ,
34 .BI "                        int " fd_out ", off64_t *" off_out ,
35 .BI "                        size_t " len ", unsigned int " flags );
36 .fi
37 .SH DESCRIPTION
38 The
39 .BR copy_file_range ()
40 system call performs an in-kernel copy between two file descriptors
41 without the additional cost of transferring data from the kernel to user space
42 and then back into the kernel.
43 It copies up to
44 .I len
45 bytes of data from the source file descriptor
46 .I fd_in
47 to the target file descriptor
48 .IR fd_out ,
49 overwriting any data that exists within the requested range of the target file.
50 .PP
51 The following semantics apply for
52 .IR off_in ,
53 and similar statements apply to
54 .IR off_out :
55 .IP * 3
57 .I off_in
58 is NULL, then bytes are read from
59 .I fd_in
60 starting from the file offset, and the file offset is
61 adjusted by the number of bytes copied.
62 .IP *
64 .I off_in
65 is not NULL, then
66 .I off_in
67 must point to a buffer that specifies the starting
68 offset where bytes from
69 .I fd_in
70 will be read.
71 The file offset of
72 .I fd_in
73 is not changed, but
74 .I off_in
75 is adjusted appropriately.
76 .PP
77 .I fd_in
78 and
79 .I fd_out
80 can refer to the same file.
81 If they refer to the same file, then the source and target ranges are not
82 allowed to overlap.
83 .PP
84 The
85 .I flags
86 argument is provided to allow for future extensions
87 and currently must be set to 0.
88 .SH RETURN VALUE
89 Upon successful completion,
90 .BR copy_file_range ()
91 will return the number of bytes copied between files.
92 This could be less than the length originally requested.
93 If the file offset of
94 .I fd_in
95 is at or past the end of file, no bytes are copied, and
96 .BR copy_file_range ()
97 returns zero.
98 .PP
99 On error,
100 .BR copy_file_range ()
101 returns \-1 and
102 .I errno
103 is set to indicate the error.
104 .SH ERRORS
106 .B EBADF
107 One or more file descriptors are not valid.
109 .B EBADF
110 .I fd_in
111 is not open for reading; or
112 .I fd_out
113 is not open for writing.
115 .B EBADF
117 .B O_APPEND
118 flag is set for the open file description (see
119 .BR open (2))
120 referred to by the file descriptor
121 .IR fd_out .
123 .B EFBIG
124 An attempt was made to write at a position past the maximum file offset the
125 kernel supports.
127 .B EFBIG
128 An attempt was made to write a range that exceeds the allowed maximum file size.
129 The maximum file size differs between filesystem implementations and can be
130 different from the maximum allowed file offset.
132 .B EFBIG
133 An attempt was made to write beyond the process's file size resource limit.
134 This may also result in the process receiving a
135 .B SIGXFSZ
136 signal.
138 .B EINVAL
140 .I flags
141 argument is not 0.
143 .B EINVAL
144 .I fd_in
146 .I fd_out
147 refer to the same file and the source and target ranges overlap.
149 .B EINVAL
150 Either
151 .I fd_in
153 .I fd_out
154 is not a regular file.
156 .B EIO
157 A low-level I/O error occurred while copying.
159 .B EISDIR
160 Either
161 .I fd_in
163 .I fd_out
164 refers to a directory.
166 .B ENOMEM
167 Out of memory.
169 .B ENOSPC
170 There is not enough space on the target filesystem to complete the copy.
172 .B EOVERFLOW
173 The requested source or destination range is too large to represent in the
174 specified data types.
176 .B EPERM
177 .I fd_out
178 refers to an immutable file.
180 .B ETXTBSY
181 Either
182 .I fd_in
184 .I fd_out
185 refers to an active swap file.
187 .B EXDEV
188 The files referred to by
189 .IR fd_in " and " fd_out
190 are not on the same mounted filesystem (pre Linux 5.3).
191 .SH VERSIONS
193 .BR copy_file_range ()
194 system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
195 emulation when it is not available.
196 .\" https://sourceware.org/git/?p=glibc.git;a=commit;f=posix/unistd.h;h=bad7a0c81f501fbbcc79af9eaa4b8254441c4a1f
198 A major rework of the kernel implementation occurred in 5.3.
199 Areas of the API that weren't clearly defined were clarified and the API bounds
200 are much more strictly checked than on earlier kernels.
201 Applications should target the behaviour and requirements of 5.3 kernels.
203 First support for cross-filesystem copies was introduced in Linux 5.3.
204 Older kernels will return -EXDEV when cross-filesystem copies are attempted.
205 .SH CONFORMING TO
207 .BR copy_file_range ()
208 system call is a nonstandard Linux and GNU extension.
209 .SH NOTES
211 .I fd_in
212 is a sparse file, then
213 .BR copy_file_range ()
214 may expand any holes existing in the requested range.
215 Users may benefit from calling
216 .BR copy_file_range ()
217 in a loop, and using the
218 .BR lseek (2)
219 .BR SEEK_DATA
221 .BR SEEK_HOLE
222 operations to find the locations of data segments.
224 .BR copy_file_range ()
225 gives filesystems an opportunity to implement "copy acceleration" techniques,
226 such as the use of reflinks (i.e., two or more inodes that share
227 pointers to the same copy-on-write disk blocks)
228 or server-side-copy (in the case of NFS).
229 .SH EXAMPLES
231 #define _GNU_SOURCE
232 #include <fcntl.h>
233 #include <stdio.h>
234 #include <stdlib.h>
235 #include <sys/stat.h>
236 #include <unistd.h>
239 main(int argc, char *argv[])
241     int fd_in, fd_out;
242     struct stat stat;
243     off64_t len, ret;
245     if (argc != 3) {
246         fprintf(stderr, "Usage: %s <source> <destination>\en", argv[0]);
247         exit(EXIT_FAILURE);
248     }
250     fd_in = open(argv[1], O_RDONLY);
251     if (fd_in == \-1) {
252         perror("open (argv[1])");
253         exit(EXIT_FAILURE);
254     }
256     if (fstat(fd_in, &stat) == \-1) {
257         perror("fstat");
258         exit(EXIT_FAILURE);
259     }
261     len = stat.st_size;
263     fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
264     if (fd_out == \-1) {
265         perror("open (argv[2])");
266         exit(EXIT_FAILURE);
267     }
269     do {
270         ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
271         if (ret == \-1) {
272             perror("copy_file_range");
273             exit(EXIT_FAILURE);
274         }
276         len \-= ret;
277     } while (len > 0 && ret > 0);
279     close(fd_in);
280     close(fd_out);
281     exit(EXIT_SUCCESS);
284 .SH SEE ALSO
285 .BR lseek (2),
286 .BR sendfile (2),
287 .BR splice (2)