bug-coreutils
[Top][All Lists]
Advanced

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

Re: BTRFS file clone support for cp


From: Giuseppe Scrivano
Subject: Re: BTRFS file clone support for cp
Date: Mon, 03 Aug 2009 21:01:31 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Hi Jim,

I attached the cleaned patch, according to your advises.  If there are
other things, I'll fix them.

Regards,
Giuseppe


Jim Meyering <address@hidden> writes:

> How about this for NEWS:
>
>   cp accepts a new option, --reflink: create a lightweight copy
>   using copy-on-write (COW).  This is currently supported only on
>   btrfs file systems.
>
> Typically, with a feature like this, if a tool is unable to provide
> the functionality implied by the new option, it gives a diagnostic
> and exits nonzero.  Otherwise, it would be far more work for an
> application to determine whether the option was honored.
>
> Deciding whether we also want an option saying
> copy-via-COW-if-possible-and-ignore-any-failure can wait,
> but I'm leaning away from it for now.
>
> ------------------------
> With your change, the new reflink member may be used uninitialized
> via install and mv.  To avoid that, just initialize it to false in
> each cp_option_init.
>
> Also, please make cp give a diagnostic when --reflink is used with
> --sparse=never or --sparse=always.  Then, add this assertion in copy.c:
>
>     assert ( ! (x->reflink && x->sparse_mode != SPARSE_AUTO));



>From 75610aed5c325d14a3ef43fb2c319ace12f36c57 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <address@hidden>
Date: Sat, 1 Aug 2009 19:36:48 +0200
Subject: [PATCH] cp: accept the --reflink option

* NEWS: Mention it.
* doc/coreutils.texi: Likewise.
* src/copy.h (struct cp_options): New member reflink.
* src/copy.c (usage): Likewise.
(copy_reg): If reflink is true try to clone the file.
(main): Check for --reflink.
(cp_option_init): By default set reflink to false.
* src/install.c (cp_option_init): By default set reflink to false.
* src/mv.c (cp_option_init): By default set reflink to false.
* tests/cp/sparse: Add a new test case.
---
 NEWS               |    5 +++--
 doc/coreutils.texi |    9 +++++++++
 src/copy.c         |   16 ++++++++++------
 src/copy.h         |    3 +++
 src/cp.c           |   16 +++++++++++++++-
 src/install.c      |    1 +
 src/mv.c           |    1 +
 tests/cp/sparse    |    4 ++++
 8 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 80c60e2..94511b7 100644
--- a/NEWS
+++ b/NEWS
@@ -35,8 +35,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
   chroot now accepts the options --userspec and --groups.
 
-  cp, install, mv: take advantage of btrfs' O(1) copy-on-write feature
-  when both the source and destination are on the same btrfs partition.
+  cp accepts a new option, --reflink: create a lightweight copy
+  using copy-on-write (COW).  This is currently supported only on
+  btrfs file systems.
 
   sort accepts a new option, --human-numeric-sort (-h): sort numbers
   while honoring human readable suffixes like KiB and MB etc.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index acec76e..03c9eb7 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -7543,6 +7543,15 @@ Also, it is not portable to use @option{-R} to copy 
symbolic links
 unless you also specify @option{-P}, as @acronym{POSIX} allows
 implementations that dereference symbolic links by default.
 
address@hidden --reflink
address@hidden --reflink
+Attempt a O(1) copy-on-write (COW) when the underlying file system
+supports this operation instead of really copying the file.
+The source and destination files share the same disk data blocks until
+they are equal.  Changes done in a file are not visible in the other
+one because shared blocks will be duplicated before these changes are
+stored.
+
 @item --remove-destination
 @opindex --remove-destination
 Remove each existing destination file before attempting to open it
diff --git a/src/copy.c b/src/copy.c
index bbed336..5eae9f4 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -610,13 +610,16 @@ copy_reg (char const *src_name, char const *dst_name,
       goto close_src_and_dst_desc;
     }
 
-  /* If --sparse=auto is in effect, attempt a btrfs clone operation.
-     If the operation is not supported or it fails then copy the file
-     in the usual way.  */
-  bool copied = (x->sparse_mode == SPARSE_AUTO
-                 && clone_file (dest_desc, source_desc) == 0);
+  if (x->reflink)
+    {
+      if (clone_file (dest_desc, source_desc))
+        {
+          error (0, errno, _("cannot fstat %s"), quote (dst_name));
+          return_val = false;
+        }
+      goto close_src_and_dst_desc;
+    }
 
-  if (!copied)
   {
     typedef uintptr_t word;
     off_t n_read_total = 0;
@@ -2222,6 +2225,7 @@ valid_options (const struct cp_options *co)
   assert (VALID_BACKUP_TYPE (co->backup_type));
   assert (VALID_SPARSE_MODE (co->sparse_mode));
   assert (!(co->hard_link && co->symbolic_link));
+  assert (!(co->reflink && co->sparse_mode != SPARSE_AUTO));
   return true;
 }
 
diff --git a/src/copy.h b/src/copy.h
index 8e0b408..ddf4f4e 100644
--- a/src/copy.h
+++ b/src/copy.h
@@ -219,6 +219,9 @@ struct cp_options
      such a symlink) and returns false.  */
   bool open_dangling_dest_symlink;
 
+  /* If true, attempt to clone the file instead of copying it.  */
+  bool reflink;
+
   /* This is a set of destination name/inode/dev triples.  Each such triple
      represents a file we have created corresponding to a source file name
      that was specified on the command line.  Use it to avoid clobbering
diff --git a/src/cp.c b/src/cp.c
index 8785076..635c7c7 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -78,7 +78,8 @@ enum
   PRESERVE_ATTRIBUTES_OPTION,
   SPARSE_OPTION,
   STRIP_TRAILING_SLASHES_OPTION,
-  UNLINK_DEST_BEFORE_OPENING
+  UNLINK_DEST_BEFORE_OPENING,
+  REFLINK_OPTION
 };
 
 /* True if the kernel is SELinux enabled.  */
@@ -121,6 +122,7 @@ static struct option const long_opts[] =
   {"recursive", no_argument, NULL, 'R'},
   {"remove-destination", no_argument, NULL, UNLINK_DEST_BEFORE_OPENING},
   {"sparse", required_argument, NULL, SPARSE_OPTION},
+  {"reflink", no_argument, NULL, REFLINK_OPTION},
   {"strip-trailing-slashes", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
   {"suffix", required_argument, NULL, 'S'},
   {"symbolic-link", no_argument, NULL, 's'},
@@ -194,6 +196,7 @@ Mandatory arguments to long options are mandatory for short 
options too.\n\
                                  attempting to open it (contrast with 
--force)\n\
 "), stdout);
       fputs (_("\
+      --reflink                clone the file if it is possible\n\
       --sparse=WHEN            control creation of sparse files\n\
       --strip-trailing-slashes  remove any trailing slashes from each SOURCE\n\
                                  argument\n\
@@ -752,6 +755,7 @@ cp_option_init (struct cp_options *x)
   x->interactive = I_UNSPECIFIED;
   x->move_mode = false;
   x->one_file_system = false;
+  x->reflink = false;
 
   x->preserve_ownership = false;
   x->preserve_links = false;
@@ -916,6 +920,10 @@ main (int argc, char **argv)
                                     sparse_type_string, sparse_type);
          break;
 
+       case REFLINK_OPTION:
+         x.reflink = true;
+         break;
+
        case 'a':               /* Like -dR --preserve=all with reduced failure 
diagnostics. */
          x.dereference = DEREF_NEVER;
          x.preserve_links = true;
@@ -1076,6 +1084,12 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
+  if (x.reflink && x.sparse_mode != SPARSE_AUTO)
+    {
+      error (0, 0, _("--reflink can be used only with --sparse=auto"));
+      usage (EXIT_FAILURE);
+    }
+
   if (backup_suffix_string)
     simple_backup_suffix = xstrdup (backup_suffix_string);
 
diff --git a/src/install.c b/src/install.c
index fd8f71e..73b3981 100644
--- a/src/install.c
+++ b/src/install.c
@@ -269,6 +269,7 @@ cp_option_init (struct cp_options *x)
 {
   cp_options_default (x);
   x->copy_as_regular = true;
+  x->reflink = false;
   x->dereference = DEREF_ALWAYS;
   x->unlink_dest_before_opening = true;
   x->unlink_dest_after_failed_open = false;
diff --git a/src/mv.c b/src/mv.c
index 8b9b6a1..8d77380 100644
--- a/src/mv.c
+++ b/src/mv.c
@@ -105,6 +105,7 @@ cp_option_init (struct cp_options *x)
 
   cp_options_default (x);
   x->copy_as_regular = false;  /* FIXME: maybe make this an option */
+  x->reflink = false;
   x->dereference = DEREF_NEVER;
   x->unlink_dest_before_opening = false;
   x->unlink_dest_after_failed_open = false;
diff --git a/tests/cp/sparse b/tests/cp/sparse
index bd1a84f..d7b75f6 100755
--- a/tests/cp/sparse
+++ b/tests/cp/sparse
@@ -38,4 +38,8 @@ cp --sparse=always sparse copy || fail=1
 # Ensure that the copy has the same block count as the original.
 test `stat --printf %b copy` -le `stat --printf %b sparse` || fail=1
 
+# Ensure that --sparse={always,never} is not used together with --reflink.
+cp --sparse=always --reflink sparse copy && fail=1
+cp --sparse=never --reflink sparse copy && fail=1
+
 Exit $fail
-- 
1.6.3.3




reply via email to

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