From: Pádraig Brady
Date: Fri, 11 Feb 2011 08:55:22 +0000 (+0000)
Subject: copy: process empty extents more efficiently
X-Git-Tag: v8.11~27
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bfdb6b585b6842362977b8c10fe7858fb96a1dd0;p=platform%2Fupstream%2Fcoreutils.git
copy: process empty extents more efficiently
* src/copy.c (extent_copy): Treat an allocated but empty extent
much like a hole. I.E. don't read data we know is going to be NUL.
Also we convert the empty extent to a hole only when SPARSE_ALWAYS
so that the source and dest have the same allocation. This will
be improved soon, when we use fallocate() to do the allocation.
* tests/cp/fiemap-empty: A new test for efficiency and correctness
of copying empty extents.
* tests/Makefile.am: Reference the new test.
* NEWS: Mention the change in behavior.
---
diff --git a/NEWS b/NEWS
index 0c1aa6b..0bfc8b7 100644
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,10 @@ GNU coreutils NEWS -*- outline -*-
The sync in only needed on Linux kernels before 2.6.38.
[The sync was introduced in coreutils-8.10]
+ cp now copies empty extents efficiently, when doing a FIEMAP copy.
+ It no longer reads the zero bytes from the input, and also can efficiently
+ create a hole in the output file when --sparse=always is specified.
+
df now aligns columns consistently, and no longer wraps entries
with longer device identifiers, over two lines.
diff --git a/src/copy.c b/src/copy.c
index e839f97..05f92b3 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -39,6 +39,7 @@
#include "extent-scan.h"
#include "error.h"
#include "fcntl--.h"
+#include "fiemap.h"
#include "file-set.h"
#include "filemode.h"
#include "filenamecat.h"
@@ -330,11 +331,28 @@ extent_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
}
unsigned int i;
- for (i = 0; i < scan.ei_count; i++)
+ bool empty_extent = false;
+ for (i = 0; i < scan.ei_count || empty_extent; i++)
{
- off_t ext_start = scan.ext_info[i].ext_logical;
- uint64_t ext_len = scan.ext_info[i].ext_length;
- uint64_t hole_size = ext_start - last_ext_start - last_ext_len;
+ off_t ext_start;
+ uint64_t ext_len;
+ uint64_t hole_size;
+
+ if (i < scan.ei_count)
+ {
+ ext_start = scan.ext_info[i].ext_logical;
+ ext_len = scan.ext_info[i].ext_length;
+ }
+ else /* empty extent at EOF. */
+ {
+ i--;
+ ext_start = last_ext_start + scan.ext_info[i].ext_length;
+ ext_len = 0;
+ }
+
+ hole_size = ext_start - last_ext_start - last_ext_len;
+
+ wrote_hole_at_eof = false;
if (hole_size)
{
@@ -346,38 +364,72 @@ extent_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
return false;
}
- if (sparse_mode != SPARSE_NEVER)
+ if ((empty_extent && sparse_mode == SPARSE_ALWAYS)
+ || (!empty_extent && sparse_mode != SPARSE_NEVER))
{
if (lseek (dest_fd, ext_start, SEEK_SET) < 0)
{
error (0, errno, _("cannot lseek %s"), quote (dst_name));
goto fail;
}
+ wrote_hole_at_eof = true;
}
else
{
/* When not inducing holes and when there is a hole between
the end of the previous extent and the beginning of the
current one, write zeros to the destination file. */
- if (! write_zeros (dest_fd, hole_size))
+ off_t nzeros = hole_size;
+ if (empty_extent)
+ nzeros = MIN (src_total_size - dest_pos, hole_size);
+
+ if (! write_zeros (dest_fd, nzeros))
{
error (0, errno, _("%s: write failed"), quote (dst_name));
goto fail;
}
+
+ dest_pos = MIN (src_total_size, ext_start);
}
}
last_ext_start = ext_start;
- last_ext_len = ext_len;
- off_t n_read;
- if ( ! sparse_copy (src_fd, dest_fd, buf, buf_size,
- sparse_mode == SPARSE_ALWAYS, src_name, dst_name,
- ext_len, &n_read,
- &wrote_hole_at_eof))
- return false;
+ /* Treat an unwritten but allocated extent much like a hole.
+ I.E. don't read, but don't convert to a hole in the destination,
+ unless SPARSE_ALWAYS. */
+ if (scan.ext_info[i].ext_flags & FIEMAP_EXTENT_UNWRITTEN)
+ {
+ empty_extent = true;
+ last_ext_len = 0;
+ if (ext_len == 0) /* The last extent is empty and processed. */
+ empty_extent = false;
+ }
+ else
+ {
+ off_t n_read;
+ empty_extent = false;
+ last_ext_len = ext_len;
+
+ if ( ! sparse_copy (src_fd, dest_fd, buf, buf_size,
+ sparse_mode == SPARSE_ALWAYS,
+ src_name, dst_name, ext_len, &n_read,
+ &wrote_hole_at_eof))
+ return false;
- dest_pos = ext_start + n_read;
+ dest_pos = ext_start + n_read;
+ }
+
+ /* If the file ends with unwritten extents not accounted for in the
+ size, then skip processing them, and the associated redundant
+ read() calls which will always return 0. We will need to
+ remove this when we add fallocate() so that we can maintain
+ extents beyond the apparent size. */
+ if (dest_pos == src_total_size)
+ {
+ scan.hit_final_extent = true;
+ break;
+ }
}
/* Release the space allocated to scan->ext_info. */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e7f3fff..685eb52 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -321,8 +321,9 @@ TESTS = \
cp/dir-vs-file \
cp/existing-perm-race \
cp/fail-perm \
- cp/fiemap-perf \
- cp/fiemap-2 \
+ cp/fiemap-empty \
+ cp/fiemap-perf \
+ cp/fiemap-2 \
cp/file-perm-race \
cp/into-self \
cp/link \
diff --git a/tests/cp/fiemap-empty b/tests/cp/fiemap-empty
new file mode 100755
index 0000000..f1ed71c
--- /dev/null
+++ b/tests/cp/fiemap-empty
@@ -0,0 +1,89 @@
+#!/bin/sh
+# Test cp reads unwritten extents efficiently
+
+# Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see