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