1COPY_FILE_RANGE(2)         Linux Programmer's Manual        COPY_FILE_RANGE(2)
2
3
4

NAME

6       copy_file_range - Copy a range of data from one file to another
7

SYNOPSIS

9       #define _GNU_SOURCE
10       #include <unistd.h>
11
12       ssize_t copy_file_range(int fd_in, loff_t *off_in,
13                               int fd_out, loff_t *off_out,
14                               size_t len, unsigned int flags);
15

DESCRIPTION

17       The  copy_file_range()  system  call performs an in-kernel copy between
18       two file descriptors without the additional cost of  transferring  data
19       from the kernel to user space and then back into the kernel.  It copies
20       up to len bytes of data from the source file descriptor  fd_in  to  the
21       target  file descriptor fd_out, overwriting any data that exists within
22       the requested range of the target file.
23
24       The following semantics apply for off_in, and similar statements  apply
25       to off_out:
26
27       *  If  off_in is NULL, then bytes are read from fd_in starting from the
28          file offset, and the file offset is adjusted by the number of  bytes
29          copied.
30
31       *  If off_in is not NULL, then off_in must point to a buffer that spec‐
32          ifies the starting offset where bytes from fd_in will be read.   The
33          file  offset  of fd_in is not changed, but off_in is adjusted appro‐
34          priately.
35
36       fd_in and fd_out can refer to the same file.  If they refer to the same
37       file, then the source and target ranges are not allowed to overlap.
38
39       The  flags argument is provided to allow for future extensions and cur‐
40       rently must be to 0.
41

RETURN VALUE

43       Upon successful completion, copy_file_range() will return the number of
44       bytes  copied between files.  This could be less than the length origi‐
45       nally requested.  If the file offset of fd_in is at or past the end  of
46       file, no bytes are copied, and copy_file_range() returns zero.
47
48       On error, copy_file_range() returns -1 and errno is set to indicate the
49       error.
50

ERRORS

52       EBADF  One or more file descriptors are not valid.
53
54       EBADF  fd_in is not open for reading; or fd_out is not open  for  writ‐
55              ing.
56
57       EBADF  The  O_APPEND  flag  is  set  for the open file description (see
58              open(2)) referred to by the file descriptor fd_out.
59
60       EFBIG  An attempt was made to write at a position past the maximum file
61              offset the kernel supports.
62
63       EFBIG  An  attempt  was  made to write a range that exceeds the allowed
64              maximum file  size.   The  maximum  file  size  differs  between
65              filesystem implementations and can be different from the maximum
66              allowed file offset.
67
68       EFBIG  An attempt was made to write  beyond  the  process's  file  size
69              resource limit.  This may also result in the process receiving a
70              SIGXFSZ signal.
71
72       EINVAL The flags argument is not 0.
73
74       EINVAL fd_in and fd_out refer to the same file and the source and  tar‐
75              get ranges overlap.
76
77       EINVAL Either fd_in or fd_out is not a regular file.
78
79       EIO    A low-level I/O error occurred while copying.
80
81       EISDIR Either fd_in or fd_out refers to a directory.
82
83       ENOMEM Out of memory.
84
85       ENOSPC There  is  not enough space on the target filesystem to complete
86              the copy.
87
88       EOVERFLOW
89              The requested source or destination range is too large to repre‐
90              sent in the specified data types.
91
92       EPERM  fd_out refers to an immutable file.
93
94       ETXTBSY
95              Either fd_in or fd_out refers to an active swap file.
96
97       EXDEV  The  files  referred  to  by file_in and file_out are not on the
98              same mounted filesystem (pre Linux 5.3).
99

VERSIONS

101       The copy_file_range() system call first  appeared  in  Linux  4.5,  but
102       glibc 2.27 provides a user-space emulation when it is not available.
103
104       A  major rework of the kernel implementation occurred in 5.3.  Areas of
105       the API that weren't clearly defined were clarified and the API  bounds
106       are  much  more strictly checked than on earlier kernels.  Applications
107       should target the behaviour and requirements of 5.3 kernels.
108
109       First support for cross-filesystem copies was introduced in Linux  5.3.
110       Older  kernels  will  return  -EXDEV  when  cross-filesystem copies are
111       attempted.
112

CONFORMING TO

114       The copy_file_range() system call is a nonstandard Linux and GNU exten‐
115       sion.
116

NOTES

118       If  file_in  is  a  sparse  file, then copy_file_range() may expand any
119       holes existing in the requested range.  Users may benefit from  calling
120       copy_file_range()  in  a  loop,  and  using  the lseek(2) SEEK_DATA and
121       SEEK_HOLE operations to find the locations of data segments.
122
123       copy_file_range() gives filesystems an opportunity to  implement  "copy
124       acceleration"  techniques,  such  as  the use of reflinks (i.e., two or
125       more inodes that share pointers to the same copy-on-write disk  blocks)
126       or server-side-copy (in the case of NFS).
127

EXAMPLE

129       #define _GNU_SOURCE
130       #include <fcntl.h>
131       #include <stdio.h>
132       #include <stdlib.h>
133       #include <sys/stat.h>
134       #include <sys/syscall.h>
135       #include <unistd.h>
136
137       /* On versions of glibc before 2.27, we must invoke copy_file_range()
138          using syscall(2) */
139
140       static loff_t
141       copy_file_range(int fd_in, loff_t *off_in, int fd_out,
142                       loff_t *off_out, size_t len, unsigned int flags)
143       {
144           return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
145                          off_out, len, flags);
146       }
147
148       int
149       main(int argc, char **argv)
150       {
151           int fd_in, fd_out;
152           struct stat stat;
153           loff_t len, ret;
154
155           if (argc != 3) {
156               fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
157               exit(EXIT_FAILURE);
158           }
159
160           fd_in = open(argv[1], O_RDONLY);
161           if (fd_in == -1) {
162               perror("open (argv[1])");
163               exit(EXIT_FAILURE);
164           }
165
166           if (fstat(fd_in, &stat) == -1) {
167               perror("fstat");
168               exit(EXIT_FAILURE);
169           }
170
171           len = stat.st_size;
172
173           fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
174           if (fd_out == -1) {
175               perror("open (argv[2])");
176               exit(EXIT_FAILURE);
177           }
178
179           do {
180               ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
181               if (ret == -1) {
182                   perror("copy_file_range");
183                   exit(EXIT_FAILURE);
184               }
185
186               len -= ret;
187           } while (len > 0 && ret > 0);
188
189           close(fd_in);
190           close(fd_out);
191           exit(EXIT_SUCCESS);
192       }
193

SEE ALSO

195       lseek(2), sendfile(2), splice(2)
196

COLOPHON

198       This  page  is  part of release 5.04 of the Linux man-pages project.  A
199       description of the project, information about reporting bugs,  and  the
200       latest     version     of     this    page,    can    be    found    at
201       https://www.kernel.org/doc/man-pages/.
202
203
204
205Linux                             2019-10-10                COPY_FILE_RANGE(2)
Impressum