man-pages/man2/copy_file_range.2.html
2021-03-31 01:06:50 +01:00

418 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of COPY_FILE_RANGE</TITLE>
</HEAD><BODY>
<H1>COPY_FILE_RANGE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
copy_file_range - Copy a range of data from one file to another
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>ssize_t copy_file_range(int </B><I>fd_in</I><B>, loff_t *</B><I>off_in</I><B>,</B>
<B> int </B><I>fd_out</I><B>, loff_t *</B><I>off_out</I><B>,</B>
<B> size_t </B><I>len</I><B>, unsigned int </B><I>flags</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>copy_file_range</B>()
system call performs an in-kernel copy between two file descriptors
without the additional cost of transferring data from the kernel to user space
and then back into the kernel.
It copies up to
<I>len</I>
bytes of data from the source file descriptor
<I>fd_in</I>
to the target file descriptor
<I>fd_out</I>,
overwriting any data that exists within the requested range of the target file.
<P>
The following semantics apply for
<I>off_in</I>,
and similar statements apply to
<I>off_out</I>:
<DL COMPACT>
<DT id="1">*<DD>
If
<I>off_in</I>
is NULL, then bytes are read from
<I>fd_in</I>
starting from the file offset, and the file offset is
adjusted by the number of bytes copied.
<DT id="2">*<DD>
If
<I>off_in</I>
is not NULL, then
<I>off_in</I>
must point to a buffer that specifies the starting
offset where bytes from
<I>fd_in</I>
will be read.
The file offset of
<I>fd_in</I>
is not changed, but
<I>off_in</I>
is adjusted appropriately.
</DL>
<P>
<I>fd_in</I>
and
<I>fd_out</I>
can refer to the same file.
If they refer to the same file, then the source and target ranges are not
allowed to overlap.
<P>
The
<I>flags</I>
argument is provided to allow for future extensions
and currently must be set to 0.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
Upon successful completion,
<B>copy_file_range</B>()
will return the number of bytes copied between files.
This could be less than the length originally requested.
If the file offset of
<I>fd_in</I>
is at or past the end of file, no bytes are copied, and
<B>copy_file_range</B>()
returns zero.
<P>
On error,
<B>copy_file_range</B>()
returns -1 and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="3"><B>EBADF</B>
<DD>
One or more file descriptors are not valid.
<DT id="4"><B>EBADF</B>
<DD>
<I>fd_in</I>
is not open for reading; or
<I>fd_out</I>
is not open for writing.
<DT id="5"><B>EBADF</B>
<DD>
The
<B>O_APPEND</B>
flag is set for the open file description (see
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
referred to by the file descriptor
<I>fd_out</I>.
<DT id="6"><B>EFBIG</B>
<DD>
An attempt was made to write at a position past the maximum file offset the
kernel supports.
<DT id="7"><B>EFBIG</B>
<DD>
An attempt was made to write a range that exceeds the allowed maximum file size.
The maximum file size differs between filesystem implementations and can be
different from the maximum allowed file offset.
<DT id="8"><B>EFBIG</B>
<DD>
An attempt was made to write beyond the process's file size resource limit.
This may also result in the process receiving a
<I>SIGXFSZ</I>
signal.
<DT id="9"><B>EINVAL</B>
<DD>
The
<I>flags</I>
argument is not 0.
<DT id="10"><B>EINVAL</B>
<DD>
<I>fd_in</I>
and
<I>fd_out</I>
refer to the same file and the source and target ranges overlap.
<DT id="11"><B>EINVAL</B>
<DD>
Either
<I>fd_in</I>
or
<I>fd_out</I>
is not a regular file.
<DT id="12"><B>EIO</B>
<DD>
A low-level I/O error occurred while copying.
<DT id="13"><B>EISDIR</B>
<DD>
Either
<I>fd_in</I>
or
<I>fd_out</I>
refers to a directory.
<DT id="14"><B>ENOMEM</B>
<DD>
Out of memory.
<DT id="15"><B>ENOSPC</B>
<DD>
There is not enough space on the target filesystem to complete the copy.
<DT id="16"><B>EOVERFLOW</B>
<DD>
The requested source or destination range is too large to represent in the
specified data types.
<DT id="17"><B>EPERM</B>
<DD>
<I>fd_out</I>
refers to an immutable file.
<DT id="18"><B>ETXTBSY</B>
<DD>
Either
<I>fd_in</I>
or
<I>fd_out</I>
refers to an active swap file.
<DT id="19"><B>EXDEV</B>
<DD>
The files referred to by
<I>file_in</I> and <I>file_out</I>
are not on the same mounted filesystem (pre Linux 5.3).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>copy_file_range</B>()
system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
emulation when it is not available.
<P>
A major rework of the kernel implementation occurred in 5.3.
Areas of the API that weren't clearly defined were clarified and the API bounds
are much more strictly checked than on earlier kernels.
Applications should target the behaviour and requirements of 5.3 kernels.
<P>
First support for cross-filesystem copies was introduced in Linux 5.3.
Older kernels will return -EXDEV when cross-filesystem copies are attempted.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
The
<B>copy_file_range</B>()
system call is a nonstandard Linux and GNU extension.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
If
<I>file_in</I>
is a sparse file, then
<B>copy_file_range</B>()
may expand any holes existing in the requested range.
Users may benefit from calling
<B>copy_file_range</B>()
in a loop, and using the
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2)
<B>SEEK_DATA</B>
and
<B>SEEK_HOLE</B>
operations to find the locations of data segments.
<P>
<B>copy_file_range</B>()
gives filesystems an opportunity to implement &quot;copy acceleration&quot; techniques,
such as the use of reflinks (i.e., two or more inodes that share
pointers to the same copy-on-write disk blocks)
or server-side-copy (in the case of NFS).
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
/* On versions of glibc before 2.27, we must invoke copy_file_range()
<BR>&nbsp;&nbsp;&nbsp;using&nbsp;<A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A>(2)&nbsp;*/
<P>
static loff_t
copy_file_range(int fd_in, loff_t *off_in, int fd_out,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loff_t&nbsp;*off_out,&nbsp;size_t&nbsp;len,&nbsp;unsigned&nbsp;int&nbsp;flags)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;syscall(__NR_copy_file_range,&nbsp;fd_in,&nbsp;off_in,&nbsp;fd_out,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;off_out,&nbsp;len,&nbsp;flags);
}
<P>
int
main(int argc, char **argv)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fd_in,&nbsp;fd_out;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;stat&nbsp;stat;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;loff_t&nbsp;len,&nbsp;ret;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;source&gt;&nbsp;&lt;destination&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fd_in&nbsp;=&nbsp;open(argv[1],&nbsp;O_RDONLY);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd_in&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;open&nbsp;(argv[1])&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fstat(fd_in,&nbsp;&amp;stat)&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;fstat&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;=&nbsp;stat.st_size;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fd_out&nbsp;=&nbsp;open(argv[2],&nbsp;O_CREAT&nbsp;|&nbsp;O_WRONLY&nbsp;|&nbsp;O_TRUNC,&nbsp;0644);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd_out&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;open&nbsp;(argv[2])&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;=&nbsp;copy_file_range(fd_in,&nbsp;NULL,&nbsp;fd_out,&nbsp;NULL,&nbsp;len,&nbsp;0);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(ret&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;copy_file_range&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;-=&nbsp;ret;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(len&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;ret&nbsp;&gt;&nbsp;0);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;close(fd_in);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;close(fd_out);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendfile">sendfile</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="20"><A HREF="#lbAB">NAME</A><DD>
<DT id="21"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="22"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="23"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="24"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="25"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="26"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="27"><A HREF="#lbAI">NOTES</A><DD>
<DT id="28"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="29"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="30"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>