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