Use "use warnings" rather than -w on the #! line.
[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
f9df736a
WD
12diff --git a/compat.c b/compat.c
13--- a/compat.c
14+++ b/compat.c
15@@ -33,6 +33,7 @@ extern int inplace;
16 extern int recurse;
17 extern int use_qsort;
18 extern int allow_inc_recurse;
19+extern int preallocate_files;
20 extern int append_mode;
21 extern int fuzzy_basis;
22 extern int read_batch;
ae306a29 23@@ -187,6 +188,15 @@ void setup_protocol(int f_out,int f_in)
f9df736a
WD
24 if (read_batch)
25 check_batch_flags();
26
27+#ifndef SUPPORT_PREALLOCATION
28+ if (preallocate_files && !am_sender) {
29+ rprintf(FERROR,
30+ "preallocation is not supported on this %s\n",
31+ am_server ? "server" : "client");
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
c0c7984e 42@@ -554,13 +554,40 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
5e3c6c93
WD
43 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
44 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
80c89075 45 strerror putenv iconv_open locale_charset nl_langinfo getxattr \
7c4c2959 46- extattr_get_link sigaction sigprocmask setattrlist)
f9df736a 47+ extattr_get_link sigaction sigprocmask setattrlist fallocate 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+
56+AC_CACHE_CHECK([for SYS_fallocate],rsync_cv_have_sys_fallocate,[
57+AC_TRY_COMPILE([#include <sys/syscall.h>
58+#include <sys/types.h>],
59+[syscall(SYS_fallocate, 0, 0, (loff_t) 0, (loff_t) 0);],
60+rsync_cv_have_sys_fallocate=yes,rsync_cv_have_sys_fallocate=no)])
61+if test x"$rsync_cv_have_sys_fallocate" = x"yes"; then
62+ AC_DEFINE(HAVE_SYS_FALLOCATE, 1, [Define to 1 if you have the SYS_fallocate syscall number])
63+fi
64+
65+if test x"$ac_cv_func_posix_fallocate" = x"yes"; then
66+ AC_MSG_CHECKING([whether posix_fallocate is efficient])
67+ case $host_os in
68+ *cygwin*)
69+ AC_MSG_RESULT(yes)
70+ AC_DEFINE(HAVE_EFFICIENT_POSIX_FALLOCATE, 1,
71+ [Define if posix_fallocate is efficient (Cygwin)])
72+ ;;
73+ *)
74+ AC_MSG_RESULT(no)
75+ ;;
76+ esac
77+fi
78+
79+dnl End of preallocation stuff
80+
81 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
82 if test $ac_cv_func_getpgrp = yes; then
83 AC_FUNC_GETPGRP
cc3e685d
WD
84diff --git a/options.c b/options.c
85--- a/options.c
86+++ b/options.c
c0c7984e 87@@ -73,6 +73,7 @@ int remove_source_files = 0;
5e3c6c93
WD
88 int one_file_system = 0;
89 int protocol_version = PROTOCOL_VERSION;
90 int sparse_files = 0;
91+int preallocate_files = 0;
92 int do_compression = 0;
93 int def_compress_level = Z_DEFAULT_COMPRESSION;
58b399b9 94 int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
c0c7984e 95@@ -225,6 +226,7 @@ static void print_rsync_version(enum logcode f)
5e3c6c93 96 char const *links = "no ";
58b399b9 97 char const *iconv = "no ";
5e3c6c93
WD
98 char const *ipv6 = "no ";
99+ char const *preallocation = "no ";
100 STRUCT_STAT *dumstat;
101
ac2da598 102 #if SUBPROTOCOL_VERSION != 0
abd3adb8 103@@ -258,6 +260,9 @@ static void print_rsync_version(enum logcode f)
85096e5e
WD
104 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
105 symtimes = "";
5e3c6c93 106 #endif
5e3c6c93
WD
107+#ifdef SUPPORT_PREALLOCATION
108+ preallocation = "";
109+#endif
ac2da598
WD
110
111 rprintf(f, "%s version %s protocol version %d%s\n",
112 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
abd3adb8 113@@ -271,8 +276,8 @@ static void print_rsync_version(enum logcode f)
5e3c6c93
WD
114 (int)(sizeof (int64) * 8));
115 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
116 got_socketpair, hardlinks, links, ipv6, have_inplace);
85096e5e
WD
117- rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
118- have_inplace, acls, xattrs, iconv, symtimes);
119+ rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %spreallocation\n",
120+ have_inplace, acls, xattrs, iconv, symtimes, preallocation);
5e3c6c93
WD
121
122 #ifdef MAINTAINER_MODE
123 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
abd3adb8 124@@ -359,6 +364,9 @@ void usage(enum logcode F)
58b399b9
WD
125 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
126 #endif
5e3c6c93
WD
127 rprintf(F," -S, --sparse handle sparse files efficiently\n");
128+#ifdef SUPPORT_PREALLOCATION
f9df736a 129+ rprintf(F," --preallocate allocate dest files before writing them\n");
5e3c6c93 130+#endif
e2b0842a 131 rprintf(F," -n, --dry-run perform a trial run with no changes made\n");
f2863bc0 132 rprintf(F," -W, --whole-file copy files whole (without delta-xfer algorithm)\n");
5e3c6c93 133 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
abd3adb8 134@@ -543,6 +551,7 @@ static struct poptOption long_options[] = {
c0c7984e
WD
135 {"sparse", 'S', POPT_ARG_VAL, &sparse_files, 1, 0, 0 },
136 {"no-sparse", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
137 {"no-S", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
5e3c6c93 138+ {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0},
c0c7984e
WD
139 {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 },
140 {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 },
790ba11a 141 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
abd3adb8 142@@ -2063,6 +2072,9 @@ void server_options(char **args, int *argc_p)
5e3c6c93
WD
143 else if (remove_source_files)
144 args[ac++] = "--remove-sent-files";
145
146+ if (preallocate_files && am_sender)
147+ args[ac++] = "--preallocate";
148+
ae306a29
WD
149 if (ac > MAX_SERVER_ARGS) { /* Not possible... */
150 rprintf(FERROR, "argc overflow in server_options().\n");
151 exit_cleanup(RERR_MALLOC);
cc3e685d
WD
152diff --git a/receiver.c b/receiver.c
153--- a/receiver.c
154+++ b/receiver.c
5795bf59 155@@ -45,6 +45,7 @@ extern int cleanup_got_literal;
5e3c6c93
WD
156 extern int remove_source_files;
157 extern int append_mode;
158 extern int sparse_files;
159+extern int preallocate_files;
160 extern int keep_partial;
161 extern int checksum_seed;
162 extern int inplace;
963ca808 163@@ -175,6 +176,18 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
f9df736a 164 char *data;
5e3c6c93
WD
165 int32 i;
166 char *map = NULL;
5e3c6c93 167+#ifdef SUPPORT_PREALLOCATION
cc3e685d 168+ OFF_T preallocated_len = 0;
5e3c6c93
WD
169+
170+ if (preallocate_files && fd != -1 && total_size > 0) {
171+ /* Preallocate enough space for file's eventual length if
172+ * possible; seems to reduce fragmentation on Windows. */
f9df736a 173+ if (do_fallocate(fd, 0, total_size) == 0)
5e3c6c93
WD
174+ preallocated_len = total_size;
175+ else
f9df736a 176+ rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
5e3c6c93
WD
177+ }
178+#endif
f9df736a 179
5e3c6c93
WD
180 read_sum_head(f_in, &sum);
181
abd3adb8 182@@ -285,7 +298,15 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
5e3c6c93
WD
183 goto report_write_error;
184
185 #ifdef HAVE_FTRUNCATE
abd3adb8 186- if (inplace && fd != -1
5e3c6c93
WD
187+ /* inplace: New data could be shorter than old data.
188+ * preallocate_files: total_size could have been an overestimate.
189+ * Cut off any extra preallocated zeros from dest file. */
abd3adb8 190+ if (fd != -1
5e3c6c93 191+#ifdef SUPPORT_PREALLOCATION
abd3adb8
WD
192+ && (inplace || preallocated_len > offset)
193+#else
194+ && inplace
5e3c6c93 195+#endif
abd3adb8
WD
196 && ftruncate(fd, offset) < 0) {
197 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
198 full_fname(fname));
cc3e685d
WD
199diff --git a/rsync.h b/rsync.h
200--- a/rsync.h
201+++ b/rsync.h
ae306a29 202@@ -614,6 +614,13 @@ struct ht_int64_node {
ffc18846 203 #define ACLS_NEED_MASK 1
5e3c6c93
WD
204 #endif
205
f9df736a
WD
206+#if defined HAVE_FTRUNCATE \
207+ && (defined HAVE_FALLOCATE \
208+ || defined HAVE_SYS_FALLOCATE \
209+ || defined HAVE_EFFICIENT_POSIX_FALLOCATE)
5e3c6c93
WD
210+#define SUPPORT_PREALLOCATION 1
211+#endif
212+
612d3765 213 union file_extras {
c4bd76ea
WD
214 int32 num;
215 uint32 unum;
cc3e685d
WD
216diff --git a/rsync.yo b/rsync.yo
217--- a/rsync.yo
218+++ b/rsync.yo
abd3adb8 219@@ -356,6 +356,7 @@ to the detailed description below for a complete description. verb(
612d3765 220 --super receiver attempts super-user activities
58b399b9 221 --fake-super store/recover privileged attrs using xattrs
612d3765 222 -S, --sparse handle sparse files efficiently
f9df736a 223+ --preallocate allocate dest files before writing
e2b0842a 224 -n, --dry-run perform a trial run with no changes made
f2863bc0 225 -W, --whole-file copy files whole (w/o delta-xfer algorithm)
612d3765 226 -x, --one-file-system don't cross filesystem boundaries
abd3adb8 227@@ -1053,6 +1054,18 @@ NOTE: Don't use this option when the destination is a Solaris "tmpfs"
612d3765
WD
228 filesystem. It doesn't seem to handle seeks over null regions
229 correctly and ends up corrupting the files.
230
231+dit(bf(--preallocate)) This tells the receiver to allocate each destination
f9df736a
WD
232+file to its eventual size before writing data to the file. Rsync will only use
233+the real filesystem-level preallocation support provided by bf(fallocate)(2) or
234+Cygwin's bf(posix_fallocate)(3), not the slow glibc implementation that writes
235+a zero byte into each block. If the receiver is remote, this nonstandard
236+option only works if the receiver also has the preallocation patch.
612d3765
WD
237+
238+Without this option on MS Windows, very large destination files tend to be
239+broken into thousands of fragments; advising Windows ahead of time of the
240+eventual file size using this option usually reduces the number of
f9df736a 241+fragments to one. The usefulness of this option on Linux is yet to be tested.
612d3765 242+
e2b0842a
WD
243 dit(bf(-n, --dry-run)) This makes rsync perform a trial run that doesn't
244 make any changes (and produces mostly the same output as a real run). It
245 is most commonly used in combination with the bf(-v, --verbose) and/or
f9df736a
WD
246diff --git a/syscall.c b/syscall.c
247--- a/syscall.c
248+++ b/syscall.c
249@@ -29,6 +29,10 @@
250 #include <sys/attr.h>
251 #endif
252
253+#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
254+#include <sys/syscall.h>
255+#endif
256+
257 extern int dry_run;
258 extern int am_root;
259 extern int read_only;
c0c7984e
WD
260@@ -282,3 +286,21 @@ OFF_T do_lseek(int fd, OFF_T offset, int whence)
261 return lseek(fd, offset, whence);
f9df736a
WD
262 #endif
263 }
264+
265+#ifdef SUPPORT_PREALLOCATION
266+int do_fallocate(int fd, OFF_T offset, OFF_T length)
267+{
268+ RETURN_ERROR_IF(dry_run, 0);
269+ RETURN_ERROR_IF_RO_OR_LO;
270+ /* TODO: Use FALLOC_FL_KEEP_SIZE to avoid the need to truncate. */
271+#if defined HAVE_FALLOCATE
272+ return fallocate(fd, 0, offset, length);
273+#elif defined HAVE_SYS_FALLOCATE
274+ return syscall(SYS_fallocate, fd, 0, (loff_t) offset, (loff_t) length);
275+#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
276+ return posix_fallocate(fd, offset, length);
277+#else
278+#error coding error in SUPPORT_PREALLOCATION
279+#endif
280+}
281+#endif
cc3e685d
WD
282diff --git a/t_stub.c b/t_stub.c
283--- a/t_stub.c
284+++ b/t_stub.c
ffc18846 285@@ -22,6 +22,7 @@
5e3c6c93
WD
286 #include "rsync.h"
287
288 int modify_window = 0;
289+int preallocate_files = 0;
290 int module_id = -1;
291 int relative_paths = 0;
292 int human_readable = 0;
cc3e685d
WD
293diff --git a/util.c b/util.c
294--- a/util.c
295+++ b/util.c
c8a8b4a7 296@@ -25,6 +25,7 @@
5e3c6c93
WD
297
298 extern int verbose;
299 extern int dry_run;
300+extern int preallocate_files;
301 extern int module_id;
302 extern int modify_window;
303 extern int relative_paths;
91270139 304@@ -276,6 +277,10 @@ int copy_file(const char *source, const char *dest, int ofd,
e2b0842a 305 int ifd;
5e3c6c93
WD
306 char buf[1024 * 8];
307 int len; /* Number of bytes read into `buf'. */
308+#ifdef SUPPORT_PREALLOCATION
cc3e685d
WD
309+ OFF_T preallocated_len = 0;
310+ OFF_T offset = 0;
5e3c6c93
WD
311+#endif
312
c8a8b4a7 313 if ((ifd = do_open(source, O_RDONLY, 0)) < 0) {
91270139
WD
314 int save_errno = errno;
315@@ -309,7 +314,27 @@ int copy_file(const char *source, const char *dest, int ofd,
e2b0842a 316 }
5e3c6c93
WD
317 }
318
319+#ifdef SUPPORT_PREALLOCATION
320+ if (preallocate_files) {
321+ /* Preallocate enough space for file's eventual length if
322+ * possible; seems to reduce fragmentation on Windows. */
323+ STRUCT_STAT srcst;
324+ if (do_fstat(ifd, &srcst) == 0) {
325+ if (srcst.st_size > 0) {
f9df736a 326+ if (do_fallocate(ofd, 0, srcst.st_size) == 0)
5e3c6c93
WD
327+ preallocated_len = srcst.st_size;
328+ else
f9df736a 329+ rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(dest));
5e3c6c93
WD
330+ }
331+ } else
f9df736a 332+ rsyserr(FWARNING, errno, "fstat %s", full_fname(source));
5e3c6c93
WD
333+ }
334+#endif
335+
336 while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
337+#ifdef SUPPORT_PREALLOCATION
338+ offset += len;
339+#endif
340 if (full_write(ofd, buf, len) < 0) {
91270139 341 int save_errno = errno;
cc3e685d 342 rsyserr(FERROR_XFER, errno, "write %s", full_fname(dest));
91270139 343@@ -334,6 +359,16 @@ int copy_file(const char *source, const char *dest, int ofd,
5e3c6c93
WD
344 full_fname(source));
345 }
346
347+#ifdef SUPPORT_PREALLOCATION
348+ /* Source file might have shrunk since we fstatted it.
349+ * Cut off any extra preallocated zeros from dest file. */
350+ if (preallocated_len > offset)
351+ if (ftruncate(ofd, offset) < 0)
352+ /* If we fail to truncate, the dest file may be wrong, so we
353+ * must trigger the "partial transfer" error. */
cc3e685d 354+ rsyserr(FERROR_XFER, errno, "ftruncate %s", full_fname(dest));
5e3c6c93
WD
355+#endif
356+
357 if (close(ofd) < 0) {
91270139 358 int save_errno = errno;
cc3e685d 359 rsyserr(FERROR_XFER, errno, "close failed on %s",