| 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. |
| 4 | |
| 5 | To 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 | |
| 12 | diff --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; |
| 23 | @@ -187,6 +188,15 @@ void setup_protocol(int f_out,int f_in) |
| 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; |
| 39 | diff --git a/configure.in b/configure.in |
| 40 | --- a/configure.in |
| 41 | +++ b/configure.in |
| 42 | @@ -554,13 +554,40 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \ |
| 43 | strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \ |
| 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) |
| 47 | + extattr_get_link sigaction sigprocmask setattrlist fallocate posix_fallocate) |
| 48 | |
| 49 | dnl cygwin iconv.h defines iconv_open as libiconv_open |
| 50 | if test x"$ac_cv_func_iconv_open" != x"yes"; then |
| 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 |
| 84 | diff --git a/options.c b/options.c |
| 85 | --- a/options.c |
| 86 | +++ b/options.c |
| 87 | @@ -73,6 +73,7 @@ int remove_source_files = 0; |
| 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; |
| 94 | int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */ |
| 95 | @@ -225,6 +226,7 @@ static void print_rsync_version(enum logcode f) |
| 96 | char const *links = "no "; |
| 97 | char const *iconv = "no "; |
| 98 | char const *ipv6 = "no "; |
| 99 | + char const *preallocation = "no "; |
| 100 | STRUCT_STAT *dumstat; |
| 101 | |
| 102 | #if SUBPROTOCOL_VERSION != 0 |
| 103 | @@ -258,6 +260,9 @@ static void print_rsync_version(enum logcode f) |
| 104 | #if defined HAVE_LUTIMES && defined HAVE_UTIMES |
| 105 | symtimes = ""; |
| 106 | #endif |
| 107 | +#ifdef SUPPORT_PREALLOCATION |
| 108 | + preallocation = ""; |
| 109 | +#endif |
| 110 | |
| 111 | rprintf(f, "%s version %s protocol version %d%s\n", |
| 112 | RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol); |
| 113 | @@ -271,8 +276,8 @@ static void print_rsync_version(enum logcode f) |
| 114 | (int)(sizeof (int64) * 8)); |
| 115 | rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n", |
| 116 | got_socketpair, hardlinks, links, ipv6, have_inplace); |
| 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); |
| 121 | |
| 122 | #ifdef MAINTAINER_MODE |
| 123 | rprintf(f, "Panic Action: \"%s\"\n", get_panic_action()); |
| 124 | @@ -359,6 +364,9 @@ void usage(enum logcode F) |
| 125 | rprintf(F," --fake-super store/recover privileged attrs using xattrs\n"); |
| 126 | #endif |
| 127 | rprintf(F," -S, --sparse handle sparse files efficiently\n"); |
| 128 | +#ifdef SUPPORT_PREALLOCATION |
| 129 | + rprintf(F," --preallocate allocate dest files before writing them\n"); |
| 130 | +#endif |
| 131 | rprintf(F," -n, --dry-run perform a trial run with no changes made\n"); |
| 132 | rprintf(F," -W, --whole-file copy files whole (without delta-xfer algorithm)\n"); |
| 133 | rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n"); |
| 134 | @@ -543,6 +551,7 @@ static struct poptOption long_options[] = { |
| 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 }, |
| 138 | + {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0}, |
| 139 | {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 }, |
| 140 | {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 }, |
| 141 | {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 }, |
| 142 | @@ -2063,6 +2072,9 @@ void server_options(char **args, int *argc_p) |
| 143 | else if (remove_source_files) |
| 144 | args[ac++] = "--remove-sent-files"; |
| 145 | |
| 146 | + if (preallocate_files && am_sender) |
| 147 | + args[ac++] = "--preallocate"; |
| 148 | + |
| 149 | if (ac > MAX_SERVER_ARGS) { /* Not possible... */ |
| 150 | rprintf(FERROR, "argc overflow in server_options().\n"); |
| 151 | exit_cleanup(RERR_MALLOC); |
| 152 | diff --git a/receiver.c b/receiver.c |
| 153 | --- a/receiver.c |
| 154 | +++ b/receiver.c |
| 155 | @@ -45,6 +45,7 @@ extern int cleanup_got_literal; |
| 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; |
| 163 | @@ -175,6 +176,18 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
| 164 | char *data; |
| 165 | int32 i; |
| 166 | char *map = NULL; |
| 167 | +#ifdef SUPPORT_PREALLOCATION |
| 168 | + OFF_T preallocated_len = 0; |
| 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. */ |
| 173 | + if (do_fallocate(fd, 0, total_size) == 0) |
| 174 | + preallocated_len = total_size; |
| 175 | + else |
| 176 | + rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname)); |
| 177 | + } |
| 178 | +#endif |
| 179 | |
| 180 | read_sum_head(f_in, &sum); |
| 181 | |
| 182 | @@ -285,7 +298,15 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, |
| 183 | goto report_write_error; |
| 184 | |
| 185 | #ifdef HAVE_FTRUNCATE |
| 186 | - if (inplace && fd != -1 |
| 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. */ |
| 190 | + if (fd != -1 |
| 191 | +#ifdef SUPPORT_PREALLOCATION |
| 192 | + && (inplace || preallocated_len > offset) |
| 193 | +#else |
| 194 | + && inplace |
| 195 | +#endif |
| 196 | && ftruncate(fd, offset) < 0) { |
| 197 | rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", |
| 198 | full_fname(fname)); |
| 199 | diff --git a/rsync.h b/rsync.h |
| 200 | --- a/rsync.h |
| 201 | +++ b/rsync.h |
| 202 | @@ -614,6 +614,13 @@ struct ht_int64_node { |
| 203 | #define ACLS_NEED_MASK 1 |
| 204 | #endif |
| 205 | |
| 206 | +#if defined HAVE_FTRUNCATE \ |
| 207 | + && (defined HAVE_FALLOCATE \ |
| 208 | + || defined HAVE_SYS_FALLOCATE \ |
| 209 | + || defined HAVE_EFFICIENT_POSIX_FALLOCATE) |
| 210 | +#define SUPPORT_PREALLOCATION 1 |
| 211 | +#endif |
| 212 | + |
| 213 | union file_extras { |
| 214 | int32 num; |
| 215 | uint32 unum; |
| 216 | diff --git a/rsync.yo b/rsync.yo |
| 217 | --- a/rsync.yo |
| 218 | +++ b/rsync.yo |
| 219 | @@ -356,6 +356,7 @@ to the detailed description below for a complete description. verb( |
| 220 | --super receiver attempts super-user activities |
| 221 | --fake-super store/recover privileged attrs using xattrs |
| 222 | -S, --sparse handle sparse files efficiently |
| 223 | + --preallocate allocate dest files before writing |
| 224 | -n, --dry-run perform a trial run with no changes made |
| 225 | -W, --whole-file copy files whole (w/o delta-xfer algorithm) |
| 226 | -x, --one-file-system don't cross filesystem boundaries |
| 227 | @@ -1053,6 +1054,18 @@ NOTE: Don't use this option when the destination is a Solaris "tmpfs" |
| 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 |
| 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. |
| 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 |
| 241 | +fragments to one. The usefulness of this option on Linux is yet to be tested. |
| 242 | + |
| 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 |
| 246 | diff --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; |
| 260 | @@ -282,3 +286,21 @@ OFF_T do_lseek(int fd, OFF_T offset, int whence) |
| 261 | return lseek(fd, offset, whence); |
| 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 |
| 282 | diff --git a/t_stub.c b/t_stub.c |
| 283 | --- a/t_stub.c |
| 284 | +++ b/t_stub.c |
| 285 | @@ -22,6 +22,7 @@ |
| 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; |
| 293 | diff --git a/util.c b/util.c |
| 294 | --- a/util.c |
| 295 | +++ b/util.c |
| 296 | @@ -25,6 +25,7 @@ |
| 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; |
| 304 | @@ -276,6 +277,10 @@ int copy_file(const char *source, const char *dest, int ofd, |
| 305 | int ifd; |
| 306 | char buf[1024 * 8]; |
| 307 | int len; /* Number of bytes read into `buf'. */ |
| 308 | +#ifdef SUPPORT_PREALLOCATION |
| 309 | + OFF_T preallocated_len = 0; |
| 310 | + OFF_T offset = 0; |
| 311 | +#endif |
| 312 | |
| 313 | if ((ifd = do_open(source, O_RDONLY, 0)) < 0) { |
| 314 | int save_errno = errno; |
| 315 | @@ -309,7 +314,27 @@ int copy_file(const char *source, const char *dest, int ofd, |
| 316 | } |
| 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) { |
| 326 | + if (do_fallocate(ofd, 0, srcst.st_size) == 0) |
| 327 | + preallocated_len = srcst.st_size; |
| 328 | + else |
| 329 | + rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(dest)); |
| 330 | + } |
| 331 | + } else |
| 332 | + rsyserr(FWARNING, errno, "fstat %s", full_fname(source)); |
| 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) { |
| 341 | int save_errno = errno; |
| 342 | rsyserr(FERROR_XFER, errno, "write %s", full_fname(dest)); |
| 343 | @@ -334,6 +359,16 @@ int copy_file(const char *source, const char *dest, int ofd, |
| 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. */ |
| 354 | + rsyserr(FERROR_XFER, errno, "ftruncate %s", full_fname(dest)); |
| 355 | +#endif |
| 356 | + |
| 357 | if (close(ofd) < 0) { |
| 358 | int save_errno = errno; |
| 359 | rsyserr(FERROR_XFER, errno, "close failed on %s", |