ioctl_userfaultfd.2, madvise.2, memfd_create.2, migrate_pages.2, mmap.2, shmget.2...
[man-pages.git] / man2 / copy_file_range.2
blob20374abb21f0c50fb061505cc49b9f3bb31ef5bf
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 2018-02-02 "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 file descriptor
46 .I fd_in
47 to 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 .PP
78 The
79 .I flags
80 argument is provided to allow for future extensions
81 and currently must be to 0.
82 .SH RETURN VALUE
83 Upon successful completion,
84 .BR copy_file_range ()
85 will return the number of bytes copied between files.
86 This could be less than the length originally requested.
87 .PP
88 On error,
89 .BR copy_file_range ()
90 returns \-1 and
91 .I errno
92 is set to indicate the error.
93 .SH ERRORS
94 .TP
95 .B EBADF
96 One or more file descriptors are not valid; or
97 .I fd_in
98 is not open for reading; or
99 .I fd_out
100 is not open for writing; or
102 .B O_APPEND
103 flag is set for the open file description referred to by
104 .IR fd_out .
106 .B EFBIG
107 An attempt was made to write a file that exceeds the implementation-defined
108 maximum file size or the process's file size limit,
109 or to write at a position past the maximum allowed offset.
111 .B EINVAL
112 Requested range extends beyond the end of the source file; or the
113 .I flags
114 argument is not 0.
116 .B EIO
117 A low-level I/O error occurred while copying.
119 .B EISDIR
120 .I fd_in
122 .I fd_out
123 refers to a directory.
125 .B ENOMEM
126 Out of memory.
128 .B ENOSPC
129 There is not enough space on the target filesystem to complete the copy.
131 .B EXDEV
132 The files referred to by
133 .IR file_in " and " file_out
134 are not on the same mounted filesystem.
135 .SH VERSIONS
137 .BR copy_file_range ()
138 system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
139 emulation when it is not available.
140 .\" https://sourceware.org/git/?p=glibc.git;a=commit;f=posix/unistd.h;h=bad7a0c81f501fbbcc79af9eaa4b8254441c4a1f
141 .SH CONFORMING TO
143 .BR copy_file_range ()
144 system call is a nonstandard Linux and GNU extension.
145 .SH NOTES
147 .I file_in
148 is a sparse file, then
149 .BR copy_file_range ()
150 may expand any holes existing in the requested range.
151 Users may benefit from calling
152 .BR copy_file_range ()
153 in a loop, and using the
154 .BR lseek (2)
155 .BR SEEK_DATA
157 .BR SEEK_HOLE
158 operations to find the locations of data segments.
160 .BR copy_file_range ()
161 gives filesystems an opportunity to implement "copy acceleration" techniques,
162 such as the use of reflinks (i.e., two or more inodes that share
163 pointers to the same copy-on-write disk blocks)
164 or server-side-copy (in the case of NFS).
165 .SH EXAMPLE
167 #define _GNU_SOURCE
168 #include <fcntl.h>
169 #include <stdio.h>
170 #include <stdlib.h>
171 #include <sys/stat.h>
172 #include <sys/syscall.h>
173 #include <unistd.h>
175 /* On versions of glibc before 2.27, we must invoke copy_file_range()
176    using syscall(2) */
178 static loff_t
179 copy_file_range(int fd_in, loff_t *off_in, int fd_out,
180                 loff_t *off_out, size_t len, unsigned int flags)
182     return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
183                    off_out, len, flags);
187 main(int argc, char **argv)
189     int fd_in, fd_out;
190     struct stat stat;
191     loff_t len, ret;
193     if (argc != 3) {
194         fprintf(stderr, "Usage: %s <source> <destination>\\n", argv[0]);
195         exit(EXIT_FAILURE);
196     }
198     fd_in = open(argv[1], O_RDONLY);
199     if (fd_in == \-1) {
200         perror("open (argv[1])");
201         exit(EXIT_FAILURE);
202     }
204     if (fstat(fd_in, &stat) == \-1) {
205         perror("fstat");
206         exit(EXIT_FAILURE);
207     }
209     len = stat.st_size;
211     fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
212     if (fd_out == \-1) {
213         perror("open (argv[2])");
214         exit(EXIT_FAILURE);
215     }
217     do {
218         ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
219         if (ret == \-1) {
220             perror("copy_file_range");
221             exit(EXIT_FAILURE);
222         }
224         len \-= ret;
225     } while (len > 0);
227     close(fd_in);
228     close(fd_out);
229     exit(EXIT_SUCCESS);
232 .SH SEE ALSO
233 .BR lseek (2),
234 .BR sendfile (2),
235 .BR splice (2)