From: Ralph Boehme Date: Sun, 24 Mar 2024 16:02:21 +0000 (+0100) Subject: libreplace: add rep_copy_reflink() X-Git-Tag: tdb-1.4.13~1379 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9ffcfb8c4361bf1f6a364aa56a6057ed9b7bd247;p=thirdparty%2Fsamba.git libreplace: add rep_copy_reflink() FICLONERANGE was introduced in kernel version 4.5, so I guess we can just assume it is present if HAVE_LINUX_IOCTL is defined. Signed-off-by: Ralph Boehme Reviewed-by: David Disseldorp --- diff --git a/lib/replace/replace.c b/lib/replace/replace.c index 68829f2a3c9..0b4e06d8bd5 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -1083,6 +1083,34 @@ ssize_t rep_copy_file_range(int fd_in, } #endif /* HAVE_COPY_FILE_RANGE */ +#ifdef HAVE_LINUX_IOCTL +# include +# include +#endif + +ssize_t rep_copy_reflink(int src_fd, + off_t src_off, + int dst_fd, + off_t dst_off, + off_t to_copy) +{ +#ifdef HAVE_LINUX_IOCTL + struct file_clone_range cr; + + cr = (struct file_clone_range) { + .src_fd = src_fd, + .src_offset = (uint64_t)src_off, + .dest_offset = (uint64_t)dst_off, + .src_length = (uint64_t)to_copy, + }; + + return ioctl(dst_fd, FICLONERANGE, &cr); +#else + errno = ENOSYS; + return -1; +#endif +} + #ifndef HAVE_OPENAT2 /* fallback known wellknown __NR_openat2 values */ diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 537e61e1e48..586f4e0a575 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -1014,6 +1014,15 @@ ssize_t rep_copy_file_range(int fd_in, unsigned int flags); #endif /* HAVE_COPY_FILE_RANGE */ + +#define copy_reflink rep_copy_reflink + +ssize_t rep_copy_reflink(int src_fd, + off_t src_off, + int dst_fd, + off_t dst_off, + off_t to_copy); + #ifndef FALL_THROUGH # ifdef HAVE_FALLTHROUGH_ATTRIBUTE # define FALL_THROUGH __attribute__ ((fallthrough))