Fix alignment issue on 64-bit. Solution from Steve Ortiz.
[rsync/rsync-patches.git] / preallocate.diff
CommitLineData
5e3c6c93
WD
1This patch adds the --preallocate option that asks rsync to preallocate the
2copied files. This slows down the copy, but should reduce fragmentation on
3systems that need that.
4
5To use this patch, run these commands for a successful build:
6
7 patch -p1 <patches/preallocate.diff
8 ./prepare-source
9 ./configure
10 make
11
c1ff70aa 12based-on: a01e3b490eb36ccf9e704840e1b6683dab867550
f9df736a
WD
13diff --git a/compat.c b/compat.c
14--- a/compat.c
15+++ b/compat.c
08e9fae5 16@@ -33,6 +33,7 @@ extern int inplace;
f9df736a
WD
17 extern int recurse;
18 extern int use_qsort;
19 extern int allow_inc_recurse;
20+extern int preallocate_files;
21 extern int append_mode;
22 extern int fuzzy_basis;
23 extern int read_batch;
08e9fae5 24@@ -188,6 +189,14 @@ void setup_protocol(int f_out,int f_in)
f9df736a
WD
25 if (read_batch)
26 check_batch_flags();
27
28+#ifndef SUPPORT_PREALLOCATION
29+ if (preallocate_files && !am_sender) {
08e9fae5
WD
30+ rprintf(FERROR, "preallocation is not supported on this %s\n",
31+ am_server ? "Server" : "Client");
f9df736a
WD
32+ exit_cleanup(RERR_SYNTAX);
33+ }
34+#endif
35+
36 if (protocol_version < 30) {
37 if (append_mode == 1)
38 append_mode = 2;
cc3e685d
WD
39diff --git a/configure.in b/configure.in
40--- a/configure.in
41+++ b/configure.in
c1ff70aa 42@@ -589,13 +589,49 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
5e3c6c93 43 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
5214a41b 44 seteuid strerror putenv iconv_open locale_charset nl_langinfo getxattr \
fc557362 45 extattr_get_link sigaction sigprocmask setattrlist getgrouplist \
72e5645e 46- initgroups utimensat)
08e9fae5 47+ initgroups utimensat posix_fallocate)
5e3c6c93 48
4c15e800
WD
49 dnl cygwin iconv.h defines iconv_open as libiconv_open
50 if test x"$ac_cv_func_iconv_open" != x"yes"; then
f9df736a
WD
51 AC_CHECK_FUNC(libiconv_open, [ac_cv_func_iconv_open=yes; AC_DEFINE(HAVE_ICONV_OPEN, 1)])
52 fi
53
54+dnl Preallocation stuff (also fallocate, posix_fallocate function tests above):
55+
08e9fae5 56+AC_CACHE_CHECK([for useable fallocate],rsync_cv_have_fallocate,[
8a946eb0 57+AC_TRY_LINK([#include <fcntl.h>
08e9fae5 58+#include <sys/types.h>],
8a946eb0 59+[fallocate(0, 0, 0, 0);],
08e9fae5
WD
60+rsync_cv_have_fallocate=yes,rsync_cv_have_fallocate=no)])
61+if test x"$rsync_cv_have_fallocate" = x"yes"; then
62+ AC_DEFINE(HAVE_FALLOCATE, 1, [Define to 1 if you have the fallocate function and it compiles and links without error])
63+fi
64+
f9df736a
WD
65+AC_CACHE_CHECK([for SYS_fallocate],rsync_cv_have_sys_fallocate,[
66+AC_TRY_COMPILE([#include <sys/syscall.h>
67+#include <sys/types.h>],
8a946eb0 68+[syscall(SYS_fallocate, 0, 0, (loff_t)0, (loff_t)0);],
f9df736a
WD
69+rsync_cv_have_sys_fallocate=yes,rsync_cv_have_sys_fallocate=no)])
70+if test x"$rsync_cv_have_sys_fallocate" = x"yes"; then
71+ AC_DEFINE(HAVE_SYS_FALLOCATE, 1, [Define to 1 if you have the SYS_fallocate syscall number])
72+fi
73+
74+if test x"$ac_cv_func_posix_fallocate" = x"yes"; then
75+ AC_MSG_CHECKING([whether posix_fallocate is efficient])
76+ case $host_os in
77+ *cygwin*)
78+ AC_MSG_RESULT(yes)
79+ AC_DEFINE(HAVE_EFFICIENT_POSIX_FALLOCATE, 1,
80+ [Define if posix_fallocate is efficient (Cygwin)])
81+ ;;
82+ *)
83+ AC_MSG_RESULT(no)
84+ ;;
85+ esac
86+fi
87+
88+dnl End of preallocation stuff
89+
90 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
91 if test $ac_cv_func_getpgrp = yes; then
92 AC_FUNC_GETPGRP
cc3e685d
WD
93diff --git a/options.c b/options.c
94--- a/options.c
95+++ b/options.c
c0c7984e 96@@ -73,6 +73,7 @@ int remove_source_files = 0;
5e3c6c93
WD
97 int one_file_system = 0;
98 int protocol_version = PROTOCOL_VERSION;
99 int sparse_files = 0;
100+int preallocate_files = 0;
101 int do_compression = 0;
102 int def_compress_level = Z_DEFAULT_COMPRESSION;
58b399b9 103 int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
72e5645e 104@@ -567,6 +568,7 @@ static void print_rsync_version(enum logcode f)
5e3c6c93 105 char const *links = "no ";
58b399b9 106 char const *iconv = "no ";
5e3c6c93
WD
107 char const *ipv6 = "no ";
108+ char const *preallocation = "no ";
109 STRUCT_STAT *dumstat;
110
ac2da598 111 #if SUBPROTOCOL_VERSION != 0
72e5645e
WD
112@@ -600,6 +602,9 @@ static void print_rsync_version(enum logcode f)
113 #ifdef CAN_SET_SYMLINK_TIMES
85096e5e 114 symtimes = "";
5e3c6c93 115 #endif
5e3c6c93
WD
116+#ifdef SUPPORT_PREALLOCATION
117+ preallocation = "";
118+#endif
ac2da598
WD
119
120 rprintf(f, "%s version %s protocol version %d%s\n",
121 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
72e5645e 122@@ -613,8 +618,8 @@ static void print_rsync_version(enum logcode f)
5e3c6c93
WD
123 (int)(sizeof (int64) * 8));
124 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
125 got_socketpair, hardlinks, links, ipv6, have_inplace);
85096e5e
WD
126- rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
127- have_inplace, acls, xattrs, iconv, symtimes);
128+ rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %spreallocation\n",
129+ have_inplace, acls, xattrs, iconv, symtimes, preallocation);
5e3c6c93
WD
130
131 #ifdef MAINTAINER_MODE
132 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
08e9fae5 133@@ -704,6 +709,11 @@ void usage(enum logcode F)
58b399b9
WD
134 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
135 #endif
5e3c6c93
WD
136 rprintf(F," -S, --sparse handle sparse files efficiently\n");
137+#ifdef SUPPORT_PREALLOCATION
f9df736a 138+ rprintf(F," --preallocate allocate dest files before writing them\n");
08e9fae5
WD
139+#else
140+ rprintf(F," --preallocate pre-allocate dest files on remote receiver\n");
5e3c6c93 141+#endif
e2b0842a 142 rprintf(F," -n, --dry-run perform a trial run with no changes made\n");
f2863bc0 143 rprintf(F," -W, --whole-file copy files whole (without delta-xfer algorithm)\n");
5e3c6c93 144 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
08e9fae5 145@@ -900,6 +910,7 @@ static struct poptOption long_options[] = {
c0c7984e
WD
146 {"sparse", 'S', POPT_ARG_VAL, &sparse_files, 1, 0, 0 },
147 {"no-sparse", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
148 {"no-S", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
5e3c6c93 149+ {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0},
c0c7984e
WD
150 {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 },
151 {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 },
790ba11a 152 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
c1ff70aa 153@@ -2661,6 +2672,9 @@ void server_options(char **args, int *argc_p)
5e3c6c93
WD
154 else if (remove_source_files)
155 args[ac++] = "--remove-sent-files";
156
157+ if (preallocate_files && am_sender)
158+ args[ac++] = "--preallocate";
159+
ae306a29
WD
160 if (ac > MAX_SERVER_ARGS) { /* Not possible... */
161 rprintf(FERROR, "argc overflow in server_options().\n");
162 exit_cleanup(RERR_MALLOC);
cc3e685d
WD
163diff --git a/receiver.c b/receiver.c
164--- a/receiver.c
165+++ b/receiver.c
fc557362 166@@ -44,6 +44,7 @@ extern int cleanup_got_literal;
5e3c6c93
WD
167 extern int remove_source_files;
168 extern int append_mode;
169 extern int sparse_files;
170+extern int preallocate_files;
171 extern int keep_partial;
fc557362 172 extern int checksum_len;
5e3c6c93 173 extern int checksum_seed;
08e9fae5 174@@ -207,6 +208,22 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
f9df736a 175 char *data;
5e3c6c93
WD
176 int32 i;
177 char *map = NULL;
5e3c6c93 178+#ifdef SUPPORT_PREALLOCATION
08e9fae5 179+#ifdef PREALLOCATE_NEEDS_TRUNCATE
cc3e685d 180+ OFF_T preallocated_len = 0;
08e9fae5 181+#endif
5e3c6c93
WD
182+
183+ if (preallocate_files && fd != -1 && total_size > 0) {
08e9fae5
WD
184+ /* Try to preallocate enough space for file's eventual length. Can
185+ * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
186+ if (do_fallocate(fd, 0, total_size) == 0) {
187+#ifdef PREALLOCATE_NEEDS_TRUNCATE
5e3c6c93 188+ preallocated_len = total_size;
08e9fae5
WD
189+#endif
190+ } else
f9df736a 191+ rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
5e3c6c93
WD
192+ }
193+#endif
f9df736a 194
5e3c6c93
WD
195 read_sum_head(f_in, &sum);
196
08e9fae5 197@@ -317,8 +334,14 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
5e3c6c93
WD
198 goto report_write_error;
199
200 #ifdef HAVE_FTRUNCATE
abd3adb8 201- if (inplace && fd != -1
fc557362 202- && ftruncate(fd, offset) < 0) {
5e3c6c93
WD
203+ /* inplace: New data could be shorter than old data.
204+ * preallocate_files: total_size could have been an overestimate.
205+ * Cut off any extra preallocated zeros from dest file. */
fc557362 206+ if ((inplace
08e9fae5 207+#ifdef PREALLOCATE_NEEDS_TRUNCATE
fc557362 208+ || preallocated_len > offset
5e3c6c93 209+#endif
fc557362 210+ ) && fd != -1 && ftruncate(fd, offset) < 0) {
abd3adb8
WD
211 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
212 full_fname(fname));
fc557362 213 }
cc3e685d
WD
214diff --git a/rsync.h b/rsync.h
215--- a/rsync.h
216+++ b/rsync.h
08e9fae5 217@@ -646,6 +646,21 @@ struct ht_int64_node {
ffc18846 218 #define ACLS_NEED_MASK 1
5e3c6c93
WD
219 #endif
220
08e9fae5
WD
221+#if defined HAVE_FALLOCATE || HAVE_SYS_FALLOCATE
222+#include <linux/falloc.h>
223+#ifdef FALLOC_FL_KEEP_SIZE
5e3c6c93 224+#define SUPPORT_PREALLOCATION 1
08e9fae5
WD
225+#elif defined HAVE_FTRUNCATE
226+#define SUPPORT_PREALLOCATION 1
227+#define PREALLOCATE_NEEDS_TRUNCATE 1
228+#endif
229+#else /* !fallocate */
230+#if defined HAVE_EFFICIENT_POSIX_FALLOCATE && defined HAVE_FTRUNCATE
231+#define SUPPORT_PREALLOCATION 1
232+#define PREALLOCATE_NEEDS_TRUNCATE 1
233+#endif
5e3c6c93
WD
234+#endif
235+
612d3765 236 union file_extras {
c4bd76ea
WD
237 int32 num;
238 uint32 unum;
cc3e685d
WD
239diff --git a/rsync.yo b/rsync.yo
240--- a/rsync.yo
241+++ b/rsync.yo
fc557362 242@@ -359,6 +359,7 @@ to the detailed description below for a complete description. verb(
612d3765 243 --super receiver attempts super-user activities
58b399b9 244 --fake-super store/recover privileged attrs using xattrs
612d3765 245 -S, --sparse handle sparse files efficiently
f9df736a 246+ --preallocate allocate dest files before writing
e2b0842a 247 -n, --dry-run perform a trial run with no changes made
f2863bc0 248 -W, --whole-file copy files whole (w/o delta-xfer algorithm)
612d3765 249 -x, --one-file-system don't cross filesystem boundaries
08e9fae5 250@@ -1127,6 +1128,17 @@ NOTE: Don't use this option when the destination is a Solaris "tmpfs"
72e5645e
WD
251 filesystem. It seems to have problems seeking over null regions,
252 and ends up corrupting the files.
612d3765
WD
253
254+dit(bf(--preallocate)) This tells the receiver to allocate each destination
f9df736a 255+file to its eventual size before writing data to the file. Rsync will only use
08e9fae5
WD
256+the real filesystem-level preallocation support provided by Linux's
257+bf(fallocate)(2) system call or Cygwin's bf(posix_fallocate)(3), not the slow
258+glibc implementation that writes a zero byte into each block.
612d3765 259+
08e9fae5
WD
260+Without this option, larger files may not be entirely contiguous on the
261+filesystem, but with this option rsync will probably copy more slowly. If the
262+destination is not an extent-supporting filesystem (such as ext4, xfs, NTFS,
263+etc.), this option may have no positive effect at all.
612d3765 264+
e2b0842a
WD
265 dit(bf(-n, --dry-run)) This makes rsync perform a trial run that doesn't
266 make any changes (and produces mostly the same output as a real run). It
267 is most commonly used in combination with the bf(-v, --verbose) and/or
f9df736a
WD
268diff --git a/syscall.c b/syscall.c
269--- a/syscall.c
270+++ b/syscall.c
271@@ -29,6 +29,10 @@
272 #include <sys/attr.h>
273 #endif
274
275+#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
276+#include <sys/syscall.h>
277+#endif
278+
279 extern int dry_run;
280 extern int am_root;
72e5645e 281 extern int am_sender;
08e9fae5 282@@ -325,3 +329,25 @@ OFF_T do_lseek(int fd, OFF_T offset, int whence)
c0c7984e 283 return lseek(fd, offset, whence);
f9df736a
WD
284 #endif
285 }
286+
287+#ifdef SUPPORT_PREALLOCATION
288+int do_fallocate(int fd, OFF_T offset, OFF_T length)
289+{
08e9fae5
WD
290+#ifdef FALLOC_FL_KEEP_SIZE
291+#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
292+#else
293+#define DO_FALLOC_OPTIONS 0
294+#endif
f9df736a
WD
295+ RETURN_ERROR_IF(dry_run, 0);
296+ RETURN_ERROR_IF_RO_OR_LO;
f9df736a 297+#if defined HAVE_FALLOCATE
08e9fae5 298+ return fallocate(fd, DO_FALLOC_OPTIONS, offset, length);
f9df736a 299+#elif defined HAVE_SYS_FALLOCATE
08e9fae5 300+ return syscall(SYS_fallocate, fd, DO_FALLOC_OPTIONS, (loff_t)offset, (loff_t)length);
f9df736a
WD
301+#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
302+ return posix_fallocate(fd, offset, length);
303+#else
08e9fae5 304+#error Coding error in SUPPORT_PREALLOCATION logic.
f9df736a
WD
305+#endif
306+}
307+#endif
cc3e685d
WD
308diff --git a/t_stub.c b/t_stub.c
309--- a/t_stub.c
310+++ b/t_stub.c
ffc18846 311@@ -22,6 +22,7 @@
5e3c6c93
WD
312 #include "rsync.h"
313
314 int modify_window = 0;
315+int preallocate_files = 0;
316 int module_id = -1;
317 int relative_paths = 0;
fc557362 318 int module_dirlen = 0;
cc3e685d
WD
319diff --git a/util.c b/util.c
320--- a/util.c
321+++ b/util.c
fc557362
WD
322@@ -26,6 +26,7 @@
323 #include "inums.h"
5e3c6c93 324
5e3c6c93
WD
325 extern int dry_run;
326+extern int preallocate_files;
327 extern int module_id;
328 extern int modify_window;
329 extern int relative_paths;
08e9fae5 330@@ -332,6 +333,9 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
e2b0842a 331 int ifd;
5e3c6c93
WD
332 char buf[1024 * 8];
333 int len; /* Number of bytes read into `buf'. */
08e9fae5
WD
334+#ifdef PREALLOCATE_NEEDS_TRUNCATE
335+ OFF_T preallocated_len = 0, offset = 0;
5e3c6c93
WD
336+#endif
337
c8a8b4a7 338 if ((ifd = do_open(source, O_RDONLY, 0)) < 0) {
91270139 339 int save_errno = errno;
08e9fae5 340@@ -357,6 +361,25 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
e2b0842a 341 }
5e3c6c93
WD
342 }
343
344+#ifdef SUPPORT_PREALLOCATION
345+ if (preallocate_files) {
5e3c6c93 346+ STRUCT_STAT srcst;
08e9fae5
WD
347+
348+ /* Try to preallocate enough space for file's eventual length. Can
349+ * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
350+ if (do_fstat(ifd, &srcst) < 0)
f9df736a 351+ rsyserr(FWARNING, errno, "fstat %s", full_fname(source));
08e9fae5
WD
352+ else if (srcst.st_size > 0) {
353+ if (do_fallocate(ofd, 0, srcst.st_size) == 0) {
354+#ifdef PREALLOCATE_NEEDS_TRUNCATE
355+ preallocated_len = srcst.st_size;
356+#endif
357+ } else
358+ rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(dest));
359+ }
5e3c6c93
WD
360+ }
361+#endif
362+
363 while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
5e3c6c93 364 if (full_write(ofd, buf, len) < 0) {
91270139 365 int save_errno = errno;
08e9fae5
WD
366@@ -366,6 +389,9 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
367 errno = save_errno;
368 return -1;
369 }
370+#ifdef PREALLOCATE_NEEDS_TRUNCATE
371+ offset += len;
372+#endif
373 }
374
375 if (len < 0) {
376@@ -382,6 +408,16 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
5e3c6c93
WD
377 full_fname(source));
378 }
379
08e9fae5 380+#ifdef PREALLOCATE_NEEDS_TRUNCATE
5e3c6c93
WD
381+ /* Source file might have shrunk since we fstatted it.
382+ * Cut off any extra preallocated zeros from dest file. */
08e9fae5
WD
383+ if (offset < preallocated_len && ftruncate(ofd, offset) < 0) {
384+ /* If we fail to truncate, the dest file may be wrong, so we
385+ * must trigger the "partial transfer" error. */
386+ rsyserr(FERROR_XFER, errno, "ftruncate %s", full_fname(dest));
387+ }
5e3c6c93
WD
388+#endif
389+
390 if (close(ofd) < 0) {
91270139 391 int save_errno = errno;
cc3e685d 392 rsyserr(FERROR_XFER, errno, "close failed on %s",