1 This patch adds the --preallocate option that asks rsync to preallocate the
2 copied files. This slows down the copy, but should reduce fragmentation on
3 systems that need that.
5 To use this patch, run these commands for a successful build:
7 patch -p1 <patches/preallocate.diff
12 based-on: 3b8f8192227b14e708bf535072485e50f4362270
13 diff --git a/compat.c b/compat.c
16 @@ -34,6 +34,7 @@ extern int inplace;
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;
24 @@ -189,6 +190,15 @@ void setup_protocol(int f_out,int f_in)
28 +#ifndef SUPPORT_PREALLOCATION
29 + if (preallocate_files && !am_sender) {
31 + "preallocation is not supported on this %s\n",
32 + am_server ? "server" : "client");
33 + exit_cleanup(RERR_SYNTAX);
37 if (protocol_version < 30) {
40 diff --git a/configure.in b/configure.in
43 @@ -574,13 +574,40 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
44 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
45 strerror putenv iconv_open locale_charset nl_langinfo getxattr \
46 extattr_get_link sigaction sigprocmask setattrlist getgrouplist \
47 - initgroups utimensat)
48 + initgroups utimensat fallocate posix_fallocate)
50 dnl cygwin iconv.h defines iconv_open as libiconv_open
51 if test x"$ac_cv_func_iconv_open" != x"yes"; then
52 AC_CHECK_FUNC(libiconv_open, [ac_cv_func_iconv_open=yes; AC_DEFINE(HAVE_ICONV_OPEN, 1)])
55 +dnl Preallocation stuff (also fallocate, posix_fallocate function tests above):
57 +AC_CACHE_CHECK([for SYS_fallocate],rsync_cv_have_sys_fallocate,[
58 +AC_TRY_COMPILE([#include <sys/syscall.h>
59 +#include <sys/types.h>],
60 +[syscall(SYS_fallocate, 0, 0, (loff_t) 0, (loff_t) 0);],
61 +rsync_cv_have_sys_fallocate=yes,rsync_cv_have_sys_fallocate=no)])
62 +if test x"$rsync_cv_have_sys_fallocate" = x"yes"; then
63 + AC_DEFINE(HAVE_SYS_FALLOCATE, 1, [Define to 1 if you have the SYS_fallocate syscall number])
66 +if test x"$ac_cv_func_posix_fallocate" = x"yes"; then
67 + AC_MSG_CHECKING([whether posix_fallocate is efficient])
71 + AC_DEFINE(HAVE_EFFICIENT_POSIX_FALLOCATE, 1,
72 + [Define if posix_fallocate is efficient (Cygwin)])
80 +dnl End of preallocation stuff
82 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
83 if test $ac_cv_func_getpgrp = yes; then
85 diff --git a/options.c b/options.c
88 @@ -73,6 +73,7 @@ int remove_source_files = 0;
89 int one_file_system = 0;
90 int protocol_version = PROTOCOL_VERSION;
92 +int preallocate_files = 0;
93 int do_compression = 0;
94 int def_compress_level = Z_DEFAULT_COMPRESSION;
95 int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
96 @@ -567,6 +568,7 @@ static void print_rsync_version(enum logcode f)
97 char const *links = "no ";
98 char const *iconv = "no ";
99 char const *ipv6 = "no ";
100 + char const *preallocation = "no ";
101 STRUCT_STAT *dumstat;
103 #if SUBPROTOCOL_VERSION != 0
104 @@ -600,6 +602,9 @@ static void print_rsync_version(enum logcode f)
105 #ifdef CAN_SET_SYMLINK_TIMES
108 +#ifdef SUPPORT_PREALLOCATION
109 + preallocation = "";
112 rprintf(f, "%s version %s protocol version %d%s\n",
113 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
114 @@ -613,8 +618,8 @@ static void print_rsync_version(enum logcode f)
115 (int)(sizeof (int64) * 8));
116 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
117 got_socketpair, hardlinks, links, ipv6, have_inplace);
118 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
119 - have_inplace, acls, xattrs, iconv, symtimes);
120 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %spreallocation\n",
121 + have_inplace, acls, xattrs, iconv, symtimes, preallocation);
123 #ifdef MAINTAINER_MODE
124 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
125 @@ -704,6 +709,9 @@ void usage(enum logcode F)
126 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
128 rprintf(F," -S, --sparse handle sparse files efficiently\n");
129 +#ifdef SUPPORT_PREALLOCATION
130 + rprintf(F," --preallocate allocate dest files before writing them\n");
132 rprintf(F," -n, --dry-run perform a trial run with no changes made\n");
133 rprintf(F," -W, --whole-file copy files whole (without delta-xfer algorithm)\n");
134 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
135 @@ -900,6 +908,7 @@ static struct poptOption long_options[] = {
136 {"sparse", 'S', POPT_ARG_VAL, &sparse_files, 1, 0, 0 },
137 {"no-sparse", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
138 {"no-S", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
139 + {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0},
140 {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 },
141 {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 },
142 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
143 @@ -2627,6 +2636,9 @@ void server_options(char **args, int *argc_p)
144 else if (remove_source_files)
145 args[ac++] = "--remove-sent-files";
147 + if (preallocate_files && am_sender)
148 + args[ac++] = "--preallocate";
150 if (ac > MAX_SERVER_ARGS) { /* Not possible... */
151 rprintf(FERROR, "argc overflow in server_options().\n");
152 exit_cleanup(RERR_MALLOC);
153 diff --git a/receiver.c b/receiver.c
156 @@ -44,6 +44,7 @@ extern int cleanup_got_literal;
157 extern int remove_source_files;
158 extern int append_mode;
159 extern int sparse_files;
160 +extern int preallocate_files;
161 extern int keep_partial;
162 extern int checksum_len;
163 extern int checksum_seed;
164 @@ -207,6 +208,18 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
168 +#ifdef SUPPORT_PREALLOCATION
169 + OFF_T preallocated_len = 0;
171 + if (preallocate_files && fd != -1 && total_size > 0) {
172 + /* Preallocate enough space for file's eventual length if
173 + * possible; seems to reduce fragmentation on Windows. */
174 + if (do_fallocate(fd, 0, total_size) == 0)
175 + preallocated_len = total_size;
177 + rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
181 read_sum_head(f_in, &sum);
183 @@ -317,8 +330,14 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
184 goto report_write_error;
186 #ifdef HAVE_FTRUNCATE
187 - if (inplace && fd != -1
188 - && ftruncate(fd, offset) < 0) {
189 + /* inplace: New data could be shorter than old data.
190 + * preallocate_files: total_size could have been an overestimate.
191 + * Cut off any extra preallocated zeros from dest file. */
193 +#ifdef SUPPORT_PREALLOCATION
194 + || preallocated_len > offset
196 + ) && fd != -1 && ftruncate(fd, offset) < 0) {
197 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
200 diff --git a/rsync.h b/rsync.h
203 @@ -642,6 +642,13 @@ struct ht_int64_node {
204 #define ACLS_NEED_MASK 1
207 +#if defined HAVE_FTRUNCATE \
208 + && (defined HAVE_FALLOCATE \
209 + || defined HAVE_SYS_FALLOCATE \
210 + || defined HAVE_EFFICIENT_POSIX_FALLOCATE)
211 +#define SUPPORT_PREALLOCATION 1
217 diff --git a/rsync.yo b/rsync.yo
220 @@ -359,6 +359,7 @@ to the detailed description below for a complete description. verb(
221 --super receiver attempts super-user activities
222 --fake-super store/recover privileged attrs using xattrs
223 -S, --sparse handle sparse files efficiently
224 + --preallocate allocate dest files before writing
225 -n, --dry-run perform a trial run with no changes made
226 -W, --whole-file copy files whole (w/o delta-xfer algorithm)
227 -x, --one-file-system don't cross filesystem boundaries
228 @@ -1127,6 +1128,18 @@ NOTE: Don't use this option when the destination is a Solaris "tmpfs"
229 filesystem. It seems to have problems seeking over null regions,
230 and ends up corrupting the files.
232 +dit(bf(--preallocate)) This tells the receiver to allocate each destination
233 +file to its eventual size before writing data to the file. Rsync will only use
234 +the real filesystem-level preallocation support provided by bf(fallocate)(2) or
235 +Cygwin's bf(posix_fallocate)(3), not the slow glibc implementation that writes
236 +a zero byte into each block. If the receiver is remote, this nonstandard
237 +option only works if the receiver also has the preallocation patch.
239 +Without this option on MS Windows, very large destination files tend to be
240 +broken into thousands of fragments; advising Windows ahead of time of the
241 +eventual file size using this option usually reduces the number of
242 +fragments to one. The usefulness of this option on Linux is yet to be tested.
244 dit(bf(-n, --dry-run)) This makes rsync perform a trial run that doesn't
245 make any changes (and produces mostly the same output as a real run). It
246 is most commonly used in combination with the bf(-v, --verbose) and/or
247 diff --git a/syscall.c b/syscall.c
251 #include <sys/attr.h>
254 +#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
255 +#include <sys/syscall.h>
260 extern int am_sender;
261 @@ -325,3 +329,21 @@ OFF_T do_lseek(int fd, OFF_T offset, int whence)
262 return lseek(fd, offset, whence);
266 +#ifdef SUPPORT_PREALLOCATION
267 +int do_fallocate(int fd, OFF_T offset, OFF_T length)
269 + RETURN_ERROR_IF(dry_run, 0);
270 + RETURN_ERROR_IF_RO_OR_LO;
271 + /* TODO: Use FALLOC_FL_KEEP_SIZE to avoid the need to truncate. */
272 +#if defined HAVE_FALLOCATE
273 + return fallocate(fd, 0, offset, length);
274 +#elif defined HAVE_SYS_FALLOCATE
275 + return syscall(SYS_fallocate, fd, 0, (loff_t) offset, (loff_t) length);
276 +#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
277 + return posix_fallocate(fd, offset, length);
279 +#error coding error in SUPPORT_PREALLOCATION
283 diff --git a/t_stub.c b/t_stub.c
289 int modify_window = 0;
290 +int preallocate_files = 0;
292 int relative_paths = 0;
293 int module_dirlen = 0;
294 diff --git a/util.c b/util.c
301 +extern int preallocate_files;
302 extern int module_id;
303 extern int modify_window;
304 extern int relative_paths;
305 @@ -332,6 +333,10 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
308 int len; /* Number of bytes read into `buf'. */
309 +#ifdef SUPPORT_PREALLOCATION
310 + OFF_T preallocated_len = 0;
314 if ((ifd = do_open(source, O_RDONLY, 0)) < 0) {
315 int save_errno = errno;
316 @@ -357,7 +362,27 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
320 +#ifdef SUPPORT_PREALLOCATION
321 + if (preallocate_files) {
322 + /* Preallocate enough space for file's eventual length if
323 + * possible; seems to reduce fragmentation on Windows. */
325 + if (do_fstat(ifd, &srcst) == 0) {
326 + if (srcst.st_size > 0) {
327 + if (do_fallocate(ofd, 0, srcst.st_size) == 0)
328 + preallocated_len = srcst.st_size;
330 + rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(dest));
333 + rsyserr(FWARNING, errno, "fstat %s", full_fname(source));
337 while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
338 +#ifdef SUPPORT_PREALLOCATION
341 if (full_write(ofd, buf, len) < 0) {
342 int save_errno = errno;
343 rsyserr(FERROR_XFER, errno, "write %s", full_fname(dest));
344 @@ -382,6 +407,16 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
348 +#ifdef SUPPORT_PREALLOCATION
349 + /* Source file might have shrunk since we fstatted it.
350 + * Cut off any extra preallocated zeros from dest file. */
351 + if (preallocated_len > offset)
352 + if (ftruncate(ofd, offset) < 0)
353 + /* If we fail to truncate, the dest file may be wrong, so we
354 + * must trigger the "partial transfer" error. */
355 + rsyserr(FERROR_XFER, errno, "ftruncate %s", full_fname(dest));
358 if (close(ofd) < 0) {
359 int save_errno = errno;
360 rsyserr(FERROR_XFER, errno, "close failed on %s",