]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/copy_file_range.2
getent.1, iconv.1, ldd.1, locale.1, localedef.1, memusage.1, memusagestat.1, pldd...
[thirdparty/man-pages.git] / man2 / copy_file_range.2
CommitLineData
903b4807
AS
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.\"
9ba01802 25.TH COPY_FILE_RANGE 2 2019-03-06 "Linux" "Linux Programmer's Manual"
903b4807
AS
26.SH NAME
27copy_file_range \- Copy a range of data from one file to another
28.SH SYNOPSIS
29.nf
78ab0c7b 30.B #define _GNU_SOURCE
903b4807 31.B #include <unistd.h>
dbfe9c70 32.PP
ee43ffde
MK
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 );
903b4807
AS
36.fi
37.SH DESCRIPTION
38The
39.BR copy_file_range ()
40system call performs an in-kernel copy between two file descriptors
ee43ffde 41without the additional cost of transferring data from the kernel to user space
903b4807
AS
42and then back into the kernel.
43It copies up to
44.I len
45bytes of data from file descriptor
46.I fd_in
47to file descriptor
48.IR fd_out ,
49overwriting any data that exists within the requested range of the target file.
efeece04 50.PP
903b4807
AS
51The following semantics apply for
52.IR off_in ,
53and similar statements apply to
54.IR off_out :
55.IP * 3
56If
57.I off_in
58is NULL, then bytes are read from
59.I fd_in
ee43ffde 60starting from the file offset, and the file offset is
903b4807
AS
61adjusted by the number of bytes copied.
62.IP *
63If
64.I off_in
65is not NULL, then
66.I off_in
67must point to a buffer that specifies the starting
68offset where bytes from
69.I fd_in
ee43ffde
MK
70will be read.
71The file offset of
903b4807
AS
72.I fd_in
73is not changed, but
74.I off_in
75is adjusted appropriately.
76.PP
efeece04 77.PP
903b4807
AS
78The
79.I flags
ee43ffde
MK
80argument is provided to allow for future extensions
81and currently must be to 0.
903b4807
AS
82.SH RETURN VALUE
83Upon successful completion,
84.BR copy_file_range ()
85will return the number of bytes copied between files.
86This could be less than the length originally requested.
efeece04 87.PP
903b4807
AS
88On error,
89.BR copy_file_range ()
90returns \-1 and
91.I errno
92is set to indicate the error.
93.SH ERRORS
94.TP
95.B EBADF
96One or more file descriptors are not valid; or
97.I fd_in
98is not open for reading; or
99.I fd_out
100is not open for writing; or
f0558db8
MK
101the
102.B O_APPEND
7f11e32c
MK
103flag is set for the open file description (see
104.BR open (2))
105referred to by the file descriptor
f0558db8 106.IR fd_out .
903b4807 107.TP
8253adf0
FW
108.B EFBIG
109An attempt was made to write a file that exceeds the implementation-defined
110maximum file size or the process's file size limit,
111or to write at a position past the maximum allowed offset.
112.TP
43d8d5ed
MK
113.B EINVAL
114Requested range extends beyond the end of the source file; or the
115.I flags
116argument is not 0.
117.TP
903b4807 118.B EIO
ee43ffde 119A low-level I/O error occurred while copying.
903b4807 120.TP
36f69b24
MK
121.B EISDIR
122.I fd_in
123or
124.I fd_out
125refers to a directory.
126.TP
903b4807
AS
127.B ENOMEM
128Out of memory.
129.TP
130.B ENOSPC
131There is not enough space on the target filesystem to complete the copy.
132.TP
133.B EXDEV
4cfafd79 134The files referred to by
903b4807
AS
135.IR file_in " and " file_out
136are not on the same mounted filesystem.
137.SH VERSIONS
138The
139.BR copy_file_range ()
78ab0c7b
SL
140system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
141emulation when it is not available.
142.\" https://sourceware.org/git/?p=glibc.git;a=commit;f=posix/unistd.h;h=bad7a0c81f501fbbcc79af9eaa4b8254441c4a1f
903b4807
AS
143.SH CONFORMING TO
144The
145.BR copy_file_range ()
78ab0c7b 146system call is a nonstandard Linux and GNU extension.
903b4807
AS
147.SH NOTES
148If
149.I file_in
150is a sparse file, then
151.BR copy_file_range ()
152may expand any holes existing in the requested range.
153Users may benefit from calling
154.BR copy_file_range ()
b4fe696c 155in a loop, and using the
903b4807 156.BR lseek (2)
b4fe696c
MK
157.BR SEEK_DATA
158and
159.BR SEEK_HOLE
160operations to find the locations of data segments.
efeece04 161.PP
2bea5d44
MK
162.BR copy_file_range ()
163gives filesystems an opportunity to implement "copy acceleration" techniques,
fbc8ab9a 164such as the use of reflinks (i.e., two or more inodes that share
2bea5d44
MK
165pointers to the same copy-on-write disk blocks)
166or server-side-copy (in the case of NFS).
903b4807 167.SH EXAMPLE
b76974c1 168.EX
903b4807
AS
169#define _GNU_SOURCE
170#include <fcntl.h>
171#include <stdio.h>
172#include <stdlib.h>
173#include <sys/stat.h>
174#include <sys/syscall.h>
175#include <unistd.h>
176
0e124f35
MK
177/* On versions of glibc before 2.27, we must invoke copy_file_range()
178 using syscall(2) */
179
ee43ffde
MK
180static loff_t
181copy_file_range(int fd_in, loff_t *off_in, int fd_out,
182 loff_t *off_out, size_t len, unsigned int flags)
903b4807
AS
183{
184 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
185 off_out, len, flags);
186}
187
ee43ffde
MK
188int
189main(int argc, char **argv)
903b4807
AS
190{
191 int fd_in, fd_out;
192 struct stat stat;
193 loff_t len, ret;
903b4807
AS
194
195 if (argc != 3) {
d1a71985 196 fprintf(stderr, "Usage: %s <source> <destination>\en", argv[0]);
903b4807
AS
197 exit(EXIT_FAILURE);
198 }
199
200 fd_in = open(argv[1], O_RDONLY);
201 if (fd_in == \-1) {
202 perror("open (argv[1])");
203 exit(EXIT_FAILURE);
204 }
205
206 if (fstat(fd_in, &stat) == \-1) {
207 perror("fstat");
208 exit(EXIT_FAILURE);
209 }
ee43ffde 210
903b4807
AS
211 len = stat.st_size;
212
ee43ffde 213 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
903b4807
AS
214 if (fd_out == \-1) {
215 perror("open (argv[2])");
216 exit(EXIT_FAILURE);
217 }
218
219 do {
220 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
221 if (ret == \-1) {
222 perror("copy_file_range");
223 exit(EXIT_FAILURE);
224 }
225
226 len \-= ret;
227 } while (len > 0);
228
229 close(fd_in);
230 close(fd_out);
231 exit(EXIT_SUCCESS);
232}
b76974c1 233.EE
903b4807 234.SH SEE ALSO
ee43ffde
MK
235.BR lseek (2),
236.BR sendfile (2),
903b4807 237.BR splice (2)