1 .\"This manpage is Copyright (C) 2015 Anna Schumaker <Anna.Schumaker@Netapp.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH COPY_FILE_RANGE 2 2018-02-02 "Linux" "Linux Programmer's Manual"
27 copy_file_range \- Copy a range of data from one file to another
30 .B #define _GNU_SOURCE
31 .B #include <unistd.h>
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 );
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.
45 bytes of data from file descriptor
49 overwriting any data that exists within the requested range of the target file.
51 The following semantics apply for
53 and similar statements apply to
58 is NULL, then bytes are read from
60 starting from the file offset, and the file offset is
61 adjusted by the number of bytes copied.
67 must point to a buffer that specifies the starting
68 offset where bytes from
75 is adjusted appropriately.
80 argument is provided to allow for future extensions
81 and currently must be to 0.
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.
89 .BR copy_file_range ()
92 is set to indicate the error.
96 One or more file descriptors are not valid; or
98 is not open for reading; or
100 is not open for writing; or
103 flag is set for the open file description referred to by
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.
112 Requested range extends beyond the end of the source file; or the
117 A low-level I/O error occurred while copying.
123 refers to a directory.
129 There is not enough space on the target filesystem to complete the copy.
132 The files referred to by
133 .IR file_in " and " file_out
134 are not on the same mounted filesystem.
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
143 .BR copy_file_range ()
144 system call is a nonstandard Linux and GNU extension.
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
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).
171 #include <sys/stat.h>
172 #include <sys/syscall.h>
175 /* On versions of glibc before 2.27, we must invoke copy_file_range()
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)
194 fprintf(stderr, "Usage: %s <source> <destination>\\n", argv[0]);
198 fd_in = open(argv[1], O_RDONLY);
200 perror("open (argv[1])");
204 if (fstat(fd_in, &stat) == \-1) {
211 fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
213 perror("open (argv[2])");
218 ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
220 perror("copy_file_range");