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, off64_t *off_in,
13                               int fd_out, off64_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 set 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  re‐
69              source  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 fd_in and fd_out are not on the same
98              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 at‐
111       tempted.
112

CONFORMING TO

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

NOTES

118       If  fd_in is a sparse file, then copy_file_range() may expand any holes
119       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

EXAMPLES

129       #define _GNU_SOURCE
130       #include <fcntl.h>
131       #include <stdio.h>
132       #include <stdlib.h>
133       #include <sys/stat.h>
134       #include <unistd.h>
135
136       int
137       main(int argc, char *argv[])
138       {
139           int fd_in, fd_out;
140           struct stat stat;
141           off64_t len, ret;
142
143           if (argc != 3) {
144               fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
145               exit(EXIT_FAILURE);
146           }
147
148           fd_in = open(argv[1], O_RDONLY);
149           if (fd_in == -1) {
150               perror("open (argv[1])");
151               exit(EXIT_FAILURE);
152           }
153
154           if (fstat(fd_in, &stat) == -1) {
155               perror("fstat");
156               exit(EXIT_FAILURE);
157           }
158
159           len = stat.st_size;
160
161           fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
162           if (fd_out == -1) {
163               perror("open (argv[2])");
164               exit(EXIT_FAILURE);
165           }
166
167           do {
168               ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
169               if (ret == -1) {
170                   perror("copy_file_range");
171                   exit(EXIT_FAILURE);
172               }
173
174               len -= ret;
175           } while (len > 0 && ret > 0);
176
177           close(fd_in);
178           close(fd_out);
179           exit(EXIT_SUCCESS);
180       }
181

SEE ALSO

183       lseek(2), sendfile(2), splice(2)
184

COLOPHON

186       This  page  is  part of release 5.13 of the Linux man-pages project.  A
187       description of the project, information about reporting bugs,  and  the
188       latest     version     of     this    page,    can    be    found    at
189       https://www.kernel.org/doc/man-pages/.
190
191
192
193Linux                             2021-08-27                COPY_FILE_RANGE(2)
Impressum