Debian Codegraph @

upstream/debian-trixie/man2/copy_file_range.2

raw
1 .\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH copy_file_range 2 2024-06-15 "Linux man-pages 6.9.1"
6 .SH NAME
7 copy_file_range \- Copy a range of data from one file to another
8 .SH LIBRARY
9 Standard C library
10 .RI ( libc ", " \-lc )
11 .SH SYNOPSIS
12 .nf
13 .B #define _GNU_SOURCE
14 .B #define _FILE_OFFSET_BITS 64
15 .B #include <unistd.h>
16 .P
17 .BI "ssize_t copy_file_range(int " fd_in ", off_t *_Nullable " off_in ,
18 .BI " int " fd_out ", off_t *_Nullable " off_out ,
19 .BI " size_t " len ", unsigned int " flags );
20 .fi
21 .SH DESCRIPTION
22 The
23 .BR copy_file_range ()
24 system call performs an in-kernel copy between two file descriptors
25 without the additional cost of transferring data from the kernel to user space
26 and then back into the kernel.
27 It copies up to
28 .I len
29 bytes of data from the source file descriptor
30 .I fd_in
31 to the target file descriptor
32 .IR fd_out ,
33 overwriting any data that exists within the requested range of the target file.
34 .P
35 The following semantics apply for
36 .IR off_in ,
37 and similar statements apply to
38 .IR off_out :
39 .IP \[bu] 3
40 If
41 .I off_in
42 is NULL, then bytes are read from
43 .I fd_in
44 starting from the file offset, and the file offset is
45 adjusted by the number of bytes copied.
46 .IP \[bu]
47 If
48 .I off_in
49 is not NULL, then
50 .I off_in
51 must point to a buffer that specifies the starting
52 offset where bytes from
53 .I fd_in
54 will be read.
55 The file offset of
56 .I fd_in
57 is not changed, but
58 .I off_in
59 is adjusted appropriately.
60 .P
61 .I fd_in
62 and
63 .I fd_out
64 can refer to the same file.
65 If they refer to the same file, then the source and target ranges are not
66 allowed to overlap.
67 .P
68 The
69 .I flags
70 argument is provided to allow for future extensions
71 and currently must be set to 0.
72 .SH RETURN VALUE
73 Upon successful completion,
74 .BR copy_file_range ()
75 will return the number of bytes copied between files.
76 This could be less than the length originally requested.
77 If the file offset of
78 .I fd_in
79 is at or past the end of file, no bytes are copied, and
80 .BR copy_file_range ()
81 returns zero.
82 .P
83 On error,
84 .BR copy_file_range ()
85 returns \-1 and
86 .I errno
87 is set to indicate the error.
88 .SH ERRORS
89 .TP
90 .B EBADF
91 One or more file descriptors are not valid.
92 .TP
93 .B EBADF
94 .I fd_in
95 is not open for reading; or
96 .I fd_out
97 is not open for writing.
98 .TP
99 .B EBADF
100 The
101 .B O_APPEND
102 flag is set for the open file description (see
103 .BR open (2))
104 referred to by the file descriptor
105 .IR fd_out .
106 .TP
107 .B EFBIG
108 An attempt was made to write at a position past the maximum file offset the
109 kernel supports.
110 .TP
111 .B EFBIG
112 An attempt was made to write a range that exceeds the allowed maximum file size.
113 The maximum file size differs between filesystem implementations and can be
114 different from the maximum allowed file offset.
115 .TP
116 .B EFBIG
117 An attempt was made to write beyond the process's file size resource limit.
118 This may also result in the process receiving a
119 .B SIGXFSZ
120 signal.
121 .TP
122 .B EINVAL
123 The
124 .I flags
125 argument is not 0.
126 .TP
127 .B EINVAL
128 .I fd_in
129 and
130 .I fd_out
131 refer to the same file and the source and target ranges overlap.
132 .TP
133 .B EINVAL
134 Either
135 .I fd_in
136 or
137 .I fd_out
138 is not a regular file.
139 .TP
140 .B EIO
141 A low-level I/O error occurred while copying.
142 .TP
143 .B EISDIR
144 Either
145 .I fd_in
146 or
147 .I fd_out
148 refers to a directory.
149 .TP
150 .B ENOMEM
151 Out of memory.
152 .TP
153 .B ENOSPC
154 There is not enough space on the target filesystem to complete the copy.
155 .TP
156 .BR EOPNOTSUPP " (since Linux 5.19)"
157 .\" commit 868f9f2f8e004bfe0d3935b1976f625b2924893b
158 The filesystem does not support this operation.
159 .TP
160 .B EOVERFLOW
161 The requested source or destination range is too large to represent in the
162 specified data types.
163 .TP
164 .B EPERM
165 .I fd_out
166 refers to an immutable file.
167 .TP
168 .B ETXTBSY
169 Either
170 .I fd_in
171 or
172 .I fd_out
173 refers to an active swap file.
174 .TP
175 .BR EXDEV " (before Linux 5.3)"
176 .\" commit 5dae222a5ff0c269730393018a5539cc970a4726
177 The files referred to by
178 .IR fd_in " and " fd_out
179 are not on the same filesystem.
180 .TP
181 .BR EXDEV " (since Linux 5.19)"
182 .\" commit 868f9f2f8e004bfe0d3935b1976f625b2924893b
183 The files referred to by
184 .IR fd_in " and " fd_out
185 are not on the same filesystem,
186 and the source and target filesystems are not of the same type,
187 or do not support cross-filesystem copy.
188 .SH VERSIONS
189 A major rework of the kernel implementation occurred in Linux 5.3.
190 Areas of the API that weren't clearly defined were clarified and the API bounds
191 are much more strictly checked than on earlier kernels.
192 .P
193 Since Linux 5.19,
194 cross-filesystem copies can be achieved
195 when both filesystems are of the same type,
196 and that filesystem implements support for it.
197 See BUGS for behavior prior to Linux 5.19.
198 .P
199 Applications should target the behaviour and requirements of Linux 5.19,
200 that was also backported to earlier stable kernels.
201 .SH STANDARDS
202 Linux, GNU.
203 .SH HISTORY
204 Linux 4.5,
205 but glibc 2.27 provides a user-space
206 emulation when it is not available.
207 .\" https://sourceware.org/git/?p=glibc.git;a=commit;f=posix/unistd.h;h=bad7a0c81f501fbbcc79af9eaa4b8254441c4a1f
208 .SH NOTES
209 If
210 .I fd_in
211 is a sparse file, then
212 .BR copy_file_range ()
213 may expand any holes existing in the requested range.
214 Users may benefit from calling
215 .BR copy_file_range ()
216 in a loop, and using the
217 .BR lseek (2)
218 .B SEEK_DATA
219 and
220 .B SEEK_HOLE
221 operations to find the locations of data segments.
222 .P
223 .BR copy_file_range ()
224 gives filesystems an opportunity to implement "copy acceleration" techniques,
225 such as the use of reflinks (i.e., two or more inodes that share
226 pointers to the same copy-on-write disk blocks)
227 or server-side-copy (in the case of NFS).
228 .P
229 .B _FILE_OFFSET_BITS
230 should be defined to be 64 in code that uses non-null
231 .I off_in
232 or
233 .I off_out
234 or that takes the address of
235 .BR copy_file_range ,
236 if the code is intended to be portable
237 to traditional 32-bit x86 and ARM platforms where
238 .BR off_t 's
239 width defaults to 32 bits.
240 .SH BUGS
241 In Linux 5.3 to Linux 5.18,
242 cross-filesystem copies were implemented by the kernel,
243 if the operation was not supported by individual filesystems.
244 However, on some virtual filesystems,
245 the call failed to copy, while still reporting success.
246 .SH EXAMPLES
247 .\" SRC BEGIN (copy_file_range.c)
248 .EX
249 #define _GNU_SOURCE
250 #define _FILE_OFFSET_BITS 64
251 #include <fcntl.h>
252 #include <stdio.h>
253 #include <stdlib.h>
254 #include <sys/stat.h>
255 #include <sys/types.h>
256 #include <unistd.h>
257 \&
258 int
259 main(int argc, char *argv[])
260 {
261 int fd_in, fd_out;
262 off_t len, ret;
263 struct stat stat;
264 \&
265 if (argc != 3) {
266 fprintf(stderr, "Usage: %s <source> <destination>\[rs]n", argv[0]);
267 exit(EXIT_FAILURE);
268 }
269 \&
270 fd_in = open(argv[1], O_RDONLY);
271 if (fd_in == \-1) {
272 perror("open (argv[1])");
273 exit(EXIT_FAILURE);
274 }
275 \&
276 if (fstat(fd_in, &stat) == \-1) {
277 perror("fstat");
278 exit(EXIT_FAILURE);
279 }
280 \&
281 len = stat.st_size;
282 \&
283 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
284 if (fd_out == \-1) {
285 perror("open (argv[2])");
286 exit(EXIT_FAILURE);
287 }
288 \&
289 do {
290 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
291 if (ret == \-1) {
292 perror("copy_file_range");
293 exit(EXIT_FAILURE);
294 }
295 \&
296 len \-= ret;
297 } while (len > 0 && ret > 0);
298 \&
299 close(fd_in);
300 close(fd_out);
301 exit(EXIT_SUCCESS);
302 }
303 .EE
304 .\" SRC END
305 .SH SEE ALSO
306 .BR lseek (2),
307 .BR sendfile (2),
308 .BR splice (2)