1COPY_FILE_RANGE(2) Linux Programmer's Manual COPY_FILE_RANGE(2)
2
3
4
6 copy_file_range - Copy a range of data from one file to another
7
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
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 file descriptor fd_in to file descriptor
21 fd_out, overwriting any data that exists within the requested range of
22 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 The flags argument is provided to allow for future extensions and cur‐
37 rently must be to 0.
38
40 Upon successful completion, copy_file_range() will return the number of
41 bytes copied between files. This could be less than the length origi‐
42 nally requested.
43
44 On error, copy_file_range() returns -1 and errno is set to indicate the
45 error.
46
48 EBADF One or more file descriptors are not valid; or fd_in is not open
49 for reading; or fd_out is not open for writing; or the O_APPEND
50 flag is set for the open file description (see open(2)) referred
51 to by the file descriptor fd_out.
52
53 EFBIG An attempt was made to write a file that exceeds the implementa‐
54 tion-defined maximum file size or the process's file size limit,
55 or to write at a position past the maximum allowed offset.
56
57 EINVAL Requested range extends beyond the end of the source file; or
58 the flags argument is not 0.
59
60 EIO A low-level I/O error occurred while copying.
61
62 EISDIR fd_in or fd_out refers to a directory.
63
64 ENOMEM Out of memory.
65
66 ENOSPC There is not enough space on the target filesystem to complete
67 the copy.
68
69 EXDEV The files referred to by file_in and file_out are not on the
70 same mounted filesystem.
71
73 The copy_file_range() system call first appeared in Linux 4.5, but
74 glibc 2.27 provides a user-space emulation when it is not available.
75
77 The copy_file_range() system call is a nonstandard Linux and GNU exten‐
78 sion.
79
81 If file_in is a sparse file, then copy_file_range() may expand any
82 holes existing in the requested range. Users may benefit from calling
83 copy_file_range() in a loop, and using the lseek(2) SEEK_DATA and
84 SEEK_HOLE operations to find the locations of data segments.
85
86 copy_file_range() gives filesystems an opportunity to implement "copy
87 acceleration" techniques, such as the use of reflinks (i.e., two or
88 more inodes that share pointers to the same copy-on-write disk blocks)
89 or server-side-copy (in the case of NFS).
90
92 #define _GNU_SOURCE
93 #include <fcntl.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <sys/stat.h>
97 #include <sys/syscall.h>
98 #include <unistd.h>
99
100 /* On versions of glibc before 2.27, we must invoke copy_file_range()
101 using syscall(2) */
102
103 static loff_t
104 copy_file_range(int fd_in, loff_t *off_in, int fd_out,
105 loff_t *off_out, size_t len, unsigned int flags)
106 {
107 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
108 off_out, len, flags);
109 }
110
111 int
112 main(int argc, char **argv)
113 {
114 int fd_in, fd_out;
115 struct stat stat;
116 loff_t len, ret;
117
118 if (argc != 3) {
119 fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
120 exit(EXIT_FAILURE);
121 }
122
123 fd_in = open(argv[1], O_RDONLY);
124 if (fd_in == -1) {
125 perror("open (argv[1])");
126 exit(EXIT_FAILURE);
127 }
128
129 if (fstat(fd_in, &stat) == -1) {
130 perror("fstat");
131 exit(EXIT_FAILURE);
132 }
133
134 len = stat.st_size;
135
136 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
137 if (fd_out == -1) {
138 perror("open (argv[2])");
139 exit(EXIT_FAILURE);
140 }
141
142 do {
143 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
144 if (ret == -1) {
145 perror("copy_file_range");
146 exit(EXIT_FAILURE);
147 }
148
149 len -= ret;
150 } while (len > 0);
151
152 close(fd_in);
153 close(fd_out);
154 exit(EXIT_SUCCESS);
155 }
156
158 lseek(2), sendfile(2), splice(2)
159
161 This page is part of release 5.02 of the Linux man-pages project. A
162 description of the project, information about reporting bugs, and the
163 latest version of this page, can be found at
164 https://www.kernel.org/doc/man-pages/.
165
166
167
168Linux 2019-03-06 COPY_FILE_RANGE(2)