xref: /freebsd/lib/libsys/copy_file_range.2 (revision cb2887746f8b9dd4ad6b1e757cdc053a08b25a2e)
1.\" SPDX-License-Identifier: BSD-2-Clause
2.\"
3.\" Copyright (c) 2019 Rick Macklem
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.Dd August 16, 2025
27.Dt COPY_FILE_RANGE 2
28.Os
29.Sh NAME
30.Nm copy_file_range
31.Nd kernel copy of a byte range from one regular file to another
32or within one regular file
33.Sh LIBRARY
34.Lb libc
35.Sh SYNOPSIS
36.In sys/types.h
37.In unistd.h
38.Ft ssize_t
39.Fo copy_file_range
40.Fa "int infd"
41.Fa "off_t *inoffp"
42.Fa "int outfd"
43.Fa "off_t *outoffp"
44.Fa "size_t len"
45.Fa "unsigned int flags"
46.Fc
47.Sh DESCRIPTION
48The
49.Fn copy_file_range
50system call
51copies up to
52.Fa len
53bytes from
54.Fa infd
55to
56.Fa outfd
57in the kernel.
58It may do this using a file system specific technique if
59.Fa infd
60and
61.Fa outfd
62are on the same file system.
63If
64.Fa infd
65and
66.Fa outfd
67refer to the same file, the byte ranges defined by
68the input file offset, output file offset and
69.Fa len
70cannot overlap.
71The
72.Fa infd
73argument must be opened for reading and the
74.Fa outfd
75argument must be opened for writing, but not
76.Dv O_APPEND .
77.Pp
78If
79.Fa inoffp
80or
81.Fa outoffp
82is
83.Dv NULL ,
84the file offset for
85.Fa infd
86or
87.Fa outfd
88respectively will be used and updated by
89the number of bytes copied.
90If
91.Fa inoffp
92or
93.Fa outoffp
94is not
95.Dv NULL ,
96the byte offset pointed to by
97.Fa inoffp
98or
99.Fa outoffp
100respectively will be used/updated and the file offset for
101.Fa infd
102or
103.Fa outfd
104respectively will not be affected.
105.Pp
106The only
107.Fa flags
108argument currently defined is
109.Dv COPY_FILE_RANGE_CLONE .
110When this flag is set,
111.Fn copy_file_range
112will return
113.Er EOPNOTSUPP
114if the copy cannot be done via
115block cloning.
116When
117.Fa flags
118is 0, a file system may do the copy via block cloning
119or by data copying.
120Block cloning is only possible when the offsets (plus
121.Fa len
122if not to EOF on the input file) are block
123aligned.
124The correct block alignment can normally be acquired via the
125.Dv _PC_CLONE_BLKSIZE
126query for
127.Xr pathconf 2 .
128.Pp
129This system call attempts to maintain holes in the output file for
130the byte range being copied.
131However, this does not always work well.
132It is recommended that sparse files be copied in a loop using
133.Xr lseek 2
134with
135.Dv SEEK_HOLE ,
136.Dv SEEK_DATA
137arguments and this system call for the
138data ranges found.
139.Pp
140For best performance, call
141.Fn copy_file_range
142with the largest
143.Fa len
144value possible.
145It is interruptible on most file systems,
146so there is no penalty for using very large len values, even SSIZE_MAX.
147.Pp
148.Sh RETURN VALUES
149If it succeeds, the call returns the number of bytes copied, which can be fewer
150than
151.Fa len .
152Returning fewer bytes than
153.Fa len
154does not necessarily indicate that EOF was reached.
155However, a return of zero for a non-zero
156.Fa len
157argument indicates that the offset for
158.Fa infd
159is at or beyond EOF.
160.Fn copy_file_range
161should be used in a loop until copying of the desired byte range has been
162completed.
163If an error has occurred, a \-1 is returned and the error code is placed in
164the global variable
165.Va errno .
166.Sh ERRORS
167The
168.Fn copy_file_range
169system call
170will fail if:
171.Bl -tag -width Er
172.It Bq Er EBADF
173If
174.Fa infd
175is not open for reading or
176.Fa outfd
177is not open for writing, or opened for writing with
178.Dv O_APPEND ,
179or if
180.Fa infd
181and
182.Fa outfd
183refer to the same file.
184.It Bq Er EFBIG
185If the copy exceeds the process's file size limit or the maximum file size
186for the file system
187.Fa outfd
188resides on.
189.It Bq Er EINTR
190A signal interrupted the system call
191before it could be completed.
192This may happen for files on some NFS mounts.
193When this happens, the values pointed to by
194.Fa inoffp
195and
196.Fa outoffp
197are reset to the initial values for the system call.
198.It Bq Er EINVAL
199.Fa infd
200and
201.Fa outfd
202refer to the same file and the byte ranges overlap.
203.It Bq Er EINVAL
204The
205.Fa flags
206argument is not zero.
207.It Bq Er EINVAL
208Either
209.Fa infd
210or
211.Fa outfd
212refers to a file object that is not a regular file.
213.It Bq Er EIO
214An I/O error occurred while reading/writing the files.
215.It Bq Er EINTEGRITY
216Corrupted data was detected while reading from a file system.
217.It Bq Er EISDIR
218If either
219.Fa infd
220or
221.Fa outfd
222refers to a directory.
223.It Bq Er ENOSPC
224File system that stores
225.Fa outfd
226is full.
227.It Bq Er EOPNOTSUPP
228Cannot do the copy via block cloning and the
229.Dv COPY_FILE_RANGE_CLONE
230.Fa flags
231argument is specified.
232.El
233.Sh SEE ALSO
234.Xr lseek 2 ,
235.Xr pathconf 2
236.Sh STANDARDS
237The
238.Fn copy_file_range
239system call is expected to be compatible with the Linux system call of
240the same name.
241.Sh HISTORY
242The
243.Fn copy_file_range
244function appeared in
245.Fx 13.0 .
246