2015-11-06 21:18:21 +00:00
|
|
|
.\"This manpage is Copyright (C) 2015 Anna Schumaker <Anna.Schumaker@Netapp.com>
|
|
|
|
.\"
|
|
|
|
.\" %%%LICENSE_START(VERBATIM)
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of
|
|
|
|
.\" this manual under the conditions for verbatim copying, provided that
|
|
|
|
.\" the entire resulting derived work is distributed under the terms of
|
|
|
|
.\" a permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume
|
|
|
|
.\" no responsibility for errors or omissions, or for damages resulting
|
|
|
|
.\" from the use of the information contained herein. The author(s) may
|
|
|
|
.\" not have taken the same level of care in the production of this
|
|
|
|
.\" manual, which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" %%%LICENSE_END
|
|
|
|
.\"
|
locale.1, localedef.1, _exit.2, accept.2, access.2, acct.2, adjtimex.2, bdflush.2, bind.2, bpf.2, brk.2, chdir.2, chmod.2, chown.2, chroot.2, clock_nanosleep.2, clone.2, close.2, connect.2, copy_file_range.2, create_module.2, delete_module.2, dup.2, epoll_ctl.2, eventfd.2, execve.2, fallocate.2, fanotify_init.2, fcntl.2, flock.2, fork.2, fsync.2, futex.2, get_kernel_syms.2, getdomainname.2, getgroups.2, gethostname.2, getpagesize.2, getpeername.2, getsid.2, getsockname.2, getsockopt.2, gettimeofday.2, init_module.2, ioctl.2, ioctl_list.2, ioperm.2, iopl.2, kexec_load.2, kill.2, killpg.2, link.2, listen.2, llseek.2, lseek.2, madvise.2, memfd_create.2, mincore.2, mkdir.2, mknod.2, mmap.2, mount.2, nanosleep.2, nice.2, open.2, personality.2, pipe.2, poll.2, posix_fadvise.2, read.2, readahead.2, readlink.2, readv.2, recv.2, recvmmsg.2, rename.2, request_key.2, sched_setaffinity.2, sched_setattr.2, select.2, select_tut.2, semctl.2, semop.2, send.2, sendfile.2, sendmmsg.2, seteuid.2, setns.2, setpgid.2, setreuid.2, shutdown.2, sigaction.2, sigaltstack.2, signal.2, signalfd.2, sigpending.2, sigprocmask.2, sigsuspend.2, socketpair.2, splice.2, stat.2, statfs.2, stime.2, symlink.2, sync.2, syscall.2, syscalls.2, times.2, truncate.2, unlink.2, unshare.2, uselib.2, utimensat.2, vfork.2, vhangup.2, wait.2, wait4.2, write.2, a64l.3, abs.3, acos.3, acosh.3, addseverity.3, adjtime.3, aio_read.3, aio_write.3, asin.3, asinh.3, atan.3, atan2.3, atanh.3, atoi.3, backtrace.3, cbrt.3, ceil.3, cfree.3, clearenv.3, clock_getcpuclockid.3, clog10.3, cmsg.3, copysign.3, cos.3, cosh.3, ctermid.3, ctime.3, daemon.3, dirfd.3, div.3, dl_iterate_phdr.3, drand48.3, drand48_r.3, dysize.3, ecvt.3, ecvt_r.3, endian.3, erf.3, erfc.3, errno.3, exec.3, exp.3, exp2.3, expm1.3, fabs.3, fdim.3, ferror.3, fexecve.3, ffs.3, fgetgrent.3, fgetpwent.3, finite.3, flockfile.3, floor.3, fma.3, fmax.3, fmin.3, fmod.3, fopen.3, fpclassify.3, frexp.3, fseeko.3, fts.3, futimes.3, fwide.3, gamma.3, gcvt.3, getaddrinfo.3, getcwd.3, getdate.3, getdirentries.3, getdtablesize.3, getgrent.3, getgrent_r.3, getgrnam.3, getgrouplist.3, gethostbyname.3, gethostid.3, getline.3, getloadavg.3, getmntent.3, getnameinfo.3, getnetent_r.3, getpass.3, getprotoent_r.3, getpwent.3, getpwent_r.3, getpwnam.3, getrpcent_r.3, getservent_r.3, getspnam.3, getsubopt.3, getusershell.3, getutent.3, getw.3, gsignal.3, hypot.3, ilogb.3, inet.3, initgroups.3, insque.3, isalpha.3, isgreater.3, iswblank.3, j0.3, ldexp.3, lgamma.3, lio_listio.3, lockf.3, log.3, log10.3, log1p.3, log2.3, logb.3, lrint.3, lround.3, makedev.3, matherr.3, mbsnrtowcs.3, mkdtemp.3, mkfifo.3, mkstemp.3, mktemp.3, modf.3, mq_close.3, mq_getattr.3, mq_notify.3, mq_receive.3, mq_send.3, nan.3, nextafter.3, on_exit.3, open_memstream.3, opendir.3, perror.3, popen.3, posix_fallocate.3, posix_madvise.3, posix_memalign.3, posix_openpt.3, posix_spawn.3, pow.3, printf.3, profil.3, psignal.3, pthread_attr_setstack.3, pthread_setaffinity_np.3, putenv.3, putpwent.3, qecvt.3, rand.3, random.3, random_r.3, rcmd.3, readdir.3, realpath.3, remainder.3, remquo.3, rexec.3, rint.3, round.3, rpc.3, rpmatch.3, scalb.3, scalbln.3, scandir.3, scanf.3, seekdir.3, sem_wait.3, setbuf.3, setenv.3, setjmp.3, setnetgrent.3, siginterrupt.3, signbit.3, significand.3, sigset.3, sigsetops.3, sigvec.3, sigwait.3, sin.3, sinh.3, sleep.3, sockatmark.3, sqrt.3, statvfs.3, stpcpy.3, stpncpy.3, strdup.3, strerror.3, strftime.3, strlen.3, strnlen.3, strsep.3, strsignal.3, strtod.3, strtok.3, strtol.3, strtoul.3, syslog.3, system.3, tan.3, tanh.3, telldir.3, tempnam.3, termios.3, tgamma.3, timegm.3, timeradd.3, tmpfile.3, tmpnam.3, toascii.3, trunc.3, ttyslot.3, tzset.3, ualarm.3, unlocked_stdio.3, usleep.3, wcpcpy.3, wcpncpy.3, wcscasecmp.3, wcsdup.3, wcsncasecmp.3, wcsnlen.3, wcsnrtombs.3, wprintf.3, y0.3, pts.4, st.4, tty_ioctl.4, elf.5, gai.conf.5, group.5, locale.5, nsswitch.conf.5, proc.5, utmp.5, aio.7, capabilities.7, credentials.7, environ.7, epoll.7, fanotify.7, feature_test_macros.7, inotify.7, ip.7, mq_overview.7, namespaces.7, pipe.7, signal.7, socket.7, standards.7, svipc.7, symlink.7, time.7, unicode.7, unix.7: tstamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2016-03-15 03:46:01 +00:00
|
|
|
.TH COPY 2 2016-03-15 "Linux" "Linux Programmer's Manual"
|
2015-11-06 21:18:21 +00:00
|
|
|
.SH NAME
|
|
|
|
copy_file_range \- Copy a range of data from one file to another
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <sys/syscall.h>
|
|
|
|
.B #include <unistd.h>
|
|
|
|
|
2016-01-26 09:32:01 +00:00
|
|
|
.BI "ssize_t copy_file_range(int " fd_in ", loff_t *" off_in ,
|
|
|
|
.BI " int " fd_out ", loff_t *" off_out ,
|
|
|
|
.BI " size_t " len ", unsigned int " flags );
|
2015-11-06 21:18:21 +00:00
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
|
|
|
.BR copy_file_range ()
|
|
|
|
system call performs an in-kernel copy between two file descriptors
|
2016-01-26 09:32:01 +00:00
|
|
|
without the additional cost of transferring data from the kernel to user space
|
2015-11-06 21:18:21 +00:00
|
|
|
and then back into the kernel.
|
|
|
|
It copies up to
|
|
|
|
.I len
|
|
|
|
bytes of data from file descriptor
|
|
|
|
.I fd_in
|
|
|
|
to file descriptor
|
|
|
|
.IR fd_out ,
|
|
|
|
overwriting any data that exists within the requested range of the target file.
|
|
|
|
|
|
|
|
The following semantics apply for
|
|
|
|
.IR off_in ,
|
|
|
|
and similar statements apply to
|
|
|
|
.IR off_out :
|
|
|
|
.IP * 3
|
|
|
|
If
|
|
|
|
.I off_in
|
|
|
|
is NULL, then bytes are read from
|
|
|
|
.I fd_in
|
2016-01-26 09:32:01 +00:00
|
|
|
starting from the file offset, and the file offset is
|
2015-11-06 21:18:21 +00:00
|
|
|
adjusted by the number of bytes copied.
|
|
|
|
.IP *
|
|
|
|
If
|
|
|
|
.I off_in
|
|
|
|
is not NULL, then
|
|
|
|
.I off_in
|
|
|
|
must point to a buffer that specifies the starting
|
|
|
|
offset where bytes from
|
|
|
|
.I fd_in
|
2016-01-26 09:32:01 +00:00
|
|
|
will be read.
|
|
|
|
The file offset of
|
2015-11-06 21:18:21 +00:00
|
|
|
.I fd_in
|
|
|
|
is not changed, but
|
|
|
|
.I off_in
|
|
|
|
is adjusted appropriately.
|
|
|
|
.PP
|
|
|
|
|
|
|
|
The
|
|
|
|
.I flags
|
2016-01-26 09:32:01 +00:00
|
|
|
argument is provided to allow for future extensions
|
|
|
|
and currently must be to 0.
|
2015-11-06 21:18:21 +00:00
|
|
|
.SH RETURN VALUE
|
|
|
|
Upon successful completion,
|
|
|
|
.BR copy_file_range ()
|
|
|
|
will return the number of bytes copied between files.
|
|
|
|
This could be less than the length originally requested.
|
|
|
|
|
|
|
|
On error,
|
|
|
|
.BR copy_file_range ()
|
|
|
|
returns \-1 and
|
|
|
|
.I errno
|
|
|
|
is set to indicate the error.
|
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B EBADF
|
|
|
|
One or more file descriptors are not valid; or
|
|
|
|
.I fd_in
|
|
|
|
is not open for reading; or
|
|
|
|
.I fd_out
|
|
|
|
is not open for writing; or
|
2016-01-26 09:36:54 +00:00
|
|
|
the
|
|
|
|
.B O_APPEND
|
|
|
|
flag is set for the open file description referred to by
|
|
|
|
.IR fd_out .
|
2015-11-06 21:18:21 +00:00
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
Requested range extends beyond the end of the source file; or the
|
|
|
|
.I flags
|
|
|
|
argument is not 0.
|
|
|
|
.TP
|
|
|
|
.B EIO
|
2016-01-26 09:32:01 +00:00
|
|
|
A low-level I/O error occurred while copying.
|
2015-11-06 21:18:21 +00:00
|
|
|
.TP
|
|
|
|
.B ENOMEM
|
|
|
|
Out of memory.
|
|
|
|
.TP
|
|
|
|
.B ENOSPC
|
|
|
|
There is not enough space on the target filesystem to complete the copy.
|
|
|
|
.TP
|
|
|
|
.B EXDEV
|
2016-01-26 09:37:46 +00:00
|
|
|
The files referred to by
|
2015-11-06 21:18:21 +00:00
|
|
|
.IR file_in " and " file_out
|
|
|
|
are not on the same mounted filesystem.
|
|
|
|
.SH VERSIONS
|
|
|
|
The
|
|
|
|
.BR copy_file_range ()
|
2016-01-26 09:32:01 +00:00
|
|
|
system call first appeared in Linux 4.5.
|
2015-11-06 21:18:21 +00:00
|
|
|
.SH CONFORMING TO
|
|
|
|
The
|
|
|
|
.BR copy_file_range ()
|
|
|
|
system call is a nonstandard Linux extension.
|
|
|
|
.SH NOTES
|
|
|
|
If
|
|
|
|
.I file_in
|
|
|
|
is a sparse file, then
|
|
|
|
.BR copy_file_range ()
|
|
|
|
may expand any holes existing in the requested range.
|
|
|
|
Users may benefit from calling
|
|
|
|
.BR copy_file_range ()
|
2016-01-26 09:39:28 +00:00
|
|
|
in a loop, and using the
|
2015-11-06 21:18:21 +00:00
|
|
|
.BR lseek (2)
|
2016-01-26 09:39:28 +00:00
|
|
|
.BR SEEK_DATA
|
|
|
|
and
|
|
|
|
.BR SEEK_HOLE
|
|
|
|
operations to find the locations of data segments.
|
2016-01-27 08:10:32 +00:00
|
|
|
|
|
|
|
.BR copy_file_range ()
|
|
|
|
gives filesystems an opportunity to implement "copy acceleration" techniques,
|
|
|
|
such as the use of reflinks (i.e., two or more i-nodes that share
|
|
|
|
pointers to the same copy-on-write disk blocks)
|
|
|
|
or server-side-copy (in the case of NFS).
|
2015-11-06 21:18:21 +00:00
|
|
|
.SH EXAMPLE
|
|
|
|
.nf
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-01-26 09:32:01 +00:00
|
|
|
static loff_t
|
|
|
|
copy_file_range(int fd_in, loff_t *off_in, int fd_out,
|
|
|
|
loff_t *off_out, size_t len, unsigned int flags)
|
2015-11-06 21:18:21 +00:00
|
|
|
{
|
|
|
|
return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
|
|
|
|
off_out, len, flags);
|
|
|
|
}
|
|
|
|
|
2016-01-26 09:32:01 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2015-11-06 21:18:21 +00:00
|
|
|
{
|
|
|
|
int fd_in, fd_out;
|
|
|
|
struct stat stat;
|
|
|
|
loff_t len, ret;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s <source> <destination>\\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_in = open(argv[1], O_RDONLY);
|
|
|
|
if (fd_in == \-1) {
|
|
|
|
perror("open (argv[1])");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd_in, &stat) == \-1) {
|
|
|
|
perror("fstat");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-01-26 09:32:01 +00:00
|
|
|
|
2015-11-06 21:18:21 +00:00
|
|
|
len = stat.st_size;
|
|
|
|
|
2016-01-26 09:32:01 +00:00
|
|
|
fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
2015-11-06 21:18:21 +00:00
|
|
|
if (fd_out == \-1) {
|
|
|
|
perror("open (argv[2])");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
|
|
|
|
if (ret == \-1) {
|
|
|
|
perror("copy_file_range");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
len \-= ret;
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
close(fd_in);
|
|
|
|
close(fd_out);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH SEE ALSO
|
2016-01-26 09:32:01 +00:00
|
|
|
.BR lseek (2),
|
|
|
|
.BR sendfile (2),
|
2015-11-06 21:18:21 +00:00
|
|
|
.BR splice (2)
|