bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

BTRFS file clone support for cp


From: Giuseppe Scrivano
Subject: BTRFS file clone support for cp
Date: Sat, 25 Jul 2009 18:15:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.96 (gnu/linux)

Hello,

the BTRFS file system, avaiable on Linux since its 2.6.29 version,
supports file cloning.  This simple patch adds the support for this
feature to the cp utility.

Is there an easy and quick way to determine which file system is used by
a file?  Probably it would be safer to add a guard around the ioctl
call.


This simple test shows the huge performance boost copying a file on an
BTRFS partition.

$ du -b foo
512000000       foo

$ env time cp foo bar
0.00user 1.75system 0:34.97elapsed 5%CPU (0avgtext+0avgdata 0maxresident)k
1000192inputs+1000024outputs (2major+273minor)pagefaults 0swaps

$ env time ./cp foo baz
0.00user 0.00system 0:00.40elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
5744inputs+32outputs (1major+253minor)pagefaults 0swaps

Cheers,
Giuseppe




>From deea0ee0c2a521aae5a89d8613f937707d8f0e7b Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <address@hidden>
Date: Sat, 25 Jul 2009 16:35:27 +0200
Subject: [PATCH] cp: support the btrfs file system clone operation.

* src/copy.c(copy_reg): Use the btrfs clone operation if it is possible.
---
 src/copy.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/src/copy.c b/src/copy.c
index 4c8c432..7779df4 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -61,6 +61,10 @@
 # include "verror.h"
 #endif
 
+#ifdef __linux__
+# include <sys/ioctl.h>
+#endif
+
 #ifndef HAVE_FCHOWN
 # define HAVE_FCHOWN false
 # define fchown(fd, uid, gid) (-1)
@@ -444,6 +448,7 @@ copy_reg (char const *src_name, char const *dst_name,
   struct stat sb;
   struct stat src_open_sb;
   bool return_val = true;
+  bool copied = false;
 
   source_desc = open (src_name,
                      (O_RDONLY | O_BINARY
@@ -589,6 +594,14 @@ copy_reg (char const *src_name, char const *dst_name,
       goto close_src_and_dst_desc;
     }
 
+#ifdef __linux__
+  /* Try a btrfs clone operation.  If the operation is not supported
+     or it fails then copy the file in the usual way.  */
+  if (!ioctl (dest_desc, 1074041865, source_desc))
+    copied = true;
+#endif
+
+  if (!copied)
   {
     typedef uintptr_t word;
     off_t n_read_total = 0;
-- 
1.6.3.3





reply via email to

[Prev in Thread] Current Thread [Next in Thread]