]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/copy_file_range.2
copy_file_range.2: tfix
[thirdparty/man-pages.git] / man2 / copy_file_range.2
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 2019-10-10 "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 ", loff_t *" off_in ,
34 .BI " int " fd_out ", loff_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
56 If
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 *
63 If
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
105 .TP
106 .B EBADF
107 One or more file descriptors are not valid.
108 .TP
109 .B EBADF
110 .I fd_in
111 is not open for reading; or
112 .I fd_out
113 is not open for writing.
114 .TP
115 .B EBADF
116 The
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 .
122 .TP
123 .B EFBIG
124 An attempt was made to write at a position past the maximum file offset the
125 kernel supports.
126 .TP
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.
131 .TP
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 .I SIGXFSZ
136 signal.
137 .TP
138 .B EINVAL
139 The
140 .I flags
141 argument is not 0.
142 .TP
143 .B EINVAL
144 .I fd_in
145 and
146 .I fd_out
147 refer to the same file and the source and target ranges overlap.
148 .TP
149 .B EINVAL
150 Either
151 .I fd_in
152 or
153 .I fd_out
154 is not a regular file.
155 .TP
156 .B EIO
157 A low-level I/O error occurred while copying.
158 .TP
159 .B EISDIR
160 Either
161 .I fd_in
162 or
163 .I fd_out
164 refers to a directory.
165 .TP
166 .B ENOMEM
167 Out of memory.
168 .TP
169 .B ENOSPC
170 There is not enough space on the target filesystem to complete the copy.
171 .TP
172 .B EOVERFLOW
173 The requested source or destination range is too large to represent in the
174 specified data types.
175 .TP
176 .B EPERM
177 .I fd_out
178 refers to an immutable file.
179 .TP
180 .B ETXTBSY
181 Either
182 .I fd_in
183 or
184 .I fd_out
185 refers to an active swap file.
186 .TP
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
192 The
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
197 .PP
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.
202 .PP
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
206 The
207 .BR copy_file_range ()
208 system call is a nonstandard Linux and GNU extension.
209 .SH NOTES
210 If
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
220 and
221 .BR SEEK_HOLE
222 operations to find the locations of data segments.
223 .PP
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 EXAMPLE
230 .EX
231 #define _GNU_SOURCE
232 #include <fcntl.h>
233 #include <stdio.h>
234 #include <stdlib.h>
235 #include <sys/stat.h>
236 #include <sys/syscall.h>
237 #include <unistd.h>
238
239 /* On versions of glibc before 2.27, we must invoke copy_file_range()
240 using syscall(2) */
241
242 static loff_t
243 copy_file_range(int fd_in, loff_t *off_in, int fd_out,
244 loff_t *off_out, size_t len, unsigned int flags)
245 {
246 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
247 off_out, len, flags);
248 }
249
250 int
251 main(int argc, char **argv)
252 {
253 int fd_in, fd_out;
254 struct stat stat;
255 loff_t len, ret;
256
257 if (argc != 3) {
258 fprintf(stderr, "Usage: %s <source> <destination>\en", argv[0]);
259 exit(EXIT_FAILURE);
260 }
261
262 fd_in = open(argv[1], O_RDONLY);
263 if (fd_in == \-1) {
264 perror("open (argv[1])");
265 exit(EXIT_FAILURE);
266 }
267
268 if (fstat(fd_in, &stat) == \-1) {
269 perror("fstat");
270 exit(EXIT_FAILURE);
271 }
272
273 len = stat.st_size;
274
275 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
276 if (fd_out == \-1) {
277 perror("open (argv[2])");
278 exit(EXIT_FAILURE);
279 }
280
281 do {
282 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
283 if (ret == \-1) {
284 perror("copy_file_range");
285 exit(EXIT_FAILURE);
286 }
287
288 len \-= ret;
289 } while (len > 0 && ret > 0);
290
291 close(fd_in);
292 close(fd_out);
293 exit(EXIT_SUCCESS);
294 }
295 .EE
296 .SH SEE ALSO
297 .BR lseek (2),
298 .BR sendfile (2),
299 .BR splice (2)