I installed the attached patch so that cp etc. can use the FICLONE API that appears to be what the Linux kernel has standardized on for this sort of thing.

I notice that the Linux 4.5 kernel has added a copy_file_range syscall that is said to clone in some cases, but this isn't yet declared in the /usr/include headers on Fedora 24 (4.7.2 kernel). Also, its man page says it uses a size_t (not off_t) to count the number of bytes to be copied, which is a strange choice for a file-copying API. What's up with that?

From db04172d7229c50cccc09a92075733892d3a8d86 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Fri, 9 Sep 2016 15:28:31 -0700
Subject: [PATCH] cp: use FICLONE instead of BTRFS_IOC_CLONE

This doesn't affect the generated code on my system; it's merely
a cleaner way to use the recently-introduced Linux-specific API.
* m4/jm-macros.m4 (gl_CHECK_ALL_HEADERS): Check for linux/fs.h.
* src/copy.c: Include <linux.fs.h> if available.
(FICLONE) [__linux__]: Define if not already defined.
(clone_file): Use FICLONE instead of the older BTRFS_IOC_CLONE,
as this ioctl is no longer btrfs-specific.
---
 m4/jm-macros.m4 |  3 ++-
 src/copy.c      | 16 ++++++++++------
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4
index 435c6ff..a191af1 100644
--- a/m4/jm-macros.m4
+++ b/m4/jm-macros.m4
@@ -1,4 +1,4 @@
-#serial 110   -*- autoconf -*-
+#serial 111   -*- autoconf -*-
 
 dnl Misc type-related macros for coreutils.
 
@@ -207,6 +207,7 @@ AC_DEFUN([gl_CHECK_ALL_HEADERS],
   AC_CHECK_HEADERS_ONCE([
     hurd.h
     linux/falloc.h
+    linux/fs.h
     paths.h
     priv.h
     stropts.h
diff --git a/src/copy.c b/src/copy.c
index 191ccaa..8cc5c5b 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -74,6 +74,14 @@
 # include <linux/falloc.h>
 #endif
 
+#ifdef HAVE_LINUX_FS_H
+# include <linux/fs.h>
+#endif
+
+#if !defined FICLONE && defined __linux__
+# define FICLONE _IOW (0x94, 9, int)
+#endif
+
 #ifndef HAVE_FCHOWN
 # define HAVE_FCHOWN false
 # define fchown(fd, uid, gid) (-1)
@@ -320,12 +328,8 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
 static inline int
 clone_file (int dest_fd, int src_fd)
 {
-#ifdef __linux__
-# undef BTRFS_IOCTL_MAGIC
-# define BTRFS_IOCTL_MAGIC 0x94
-# undef BTRFS_IOC_CLONE
-# define BTRFS_IOC_CLONE _IOW (BTRFS_IOCTL_MAGIC, 9, int)
-  return ioctl (dest_fd, BTRFS_IOC_CLONE, src_fd);
+#ifdef FICLONE
+  return ioctl (dest_fd, FICLONE, src_fd);
 #else
   (void) dest_fd;
   (void) src_fd;
-- 
2.7.4

Reply via email to