4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/utils/lustre_rsync.c
36 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
37 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
41 * - lustre_rsync is a tool for replicating a lustre filesystem.
43 * - The source-fs is a live lustre filesystem. It is not a
44 * snapshot. It is mounted and undergoing changes
46 * - The target-fs is a copy of the source-fs from the past. Let's
47 * call this point, the 'sync point'.
49 * - There is a changelog of all metadata operations that happened on
50 * the filesystem since the 'sync point'.
52 * - lustre_rsync replicates all the operations saved in the changelog
53 * on to the target filesystem to make it identical to the source.
55 * To facilitate replication, the lustre filesystem provides
56 * a) a way to get the current filesystem path of a given FID
57 * b) a way to open files by specifying its FID
59 * The changelog only has a limited amount of information.
60 * tfid - The FID of the target file
61 * pfid - The FID of the parent of the target file (at the time of
63 * sfid - The FID of the source file
64 * spfid - The FID of the parent of the source file
65 * name - The name of the target file (at the time of the operation), the name
66 * of the source file is appended (delimited with '\0') if this
67 * operation involves a source
69 * With just this information, it is not alwasy possible to determine
70 * the file paths for each operation. For instance, if pfid does not
71 * exist on the source-fs (due to a subsequent deletion), its path
72 * cannot be queried. In such cases, lustre_rsync keeps the files in a
73 * special directory ("/.lustrerepl"). Once all the operations in a
74 * changelog are replayed, all the files in this special directory
75 * will get moved to the location as in the source-fs.
77 * Shorthand used: f2p(fid) = fid2path(fid)
79 * The following are the metadata operations of interest.
81 * If tfid is absent on the source-fs, ignore this operation
82 * If pfid is absent on the source-fs [or]
83 * if f2p(pfid) is not present on target-fs [or]
84 * if f2p(pfid)+name != f2p(tfid)
85 * creat .lustrerepl/tfid
86 * track [pfid,tfid,name]
91 * If .lustrerepl/[tfid] is present on the target
92 * rm .lustrerepl/[tfid]
93 * Else if pfid is present on the source-fs,
94 * if f2p(pfid)+name is present,
97 * 3. move (spfid,sname) to (pfid,name)
99 * if spfid is also present, mv (spfid,sname) to (pfid,name)
100 * else mv .lustrerepl/[sfid] to (pfid,name)
101 * Else if pfid is not present,
102 * if spfid is present, mv (spfid,sname) .lustrerepl/[sfid]
103 * If moving out of .lustrerepl
104 * move out all its children in .lustrerepl.
105 * [pfid,tfid,name] tracked from (1) is used for this.
115 #include <sys/stat.h>
116 #include <sys/types.h>
120 #include <sys/xattr.h>
122 #include <libcfs/libcfsutil.h>
123 #include <lustre/lustreapi.h>
124 #include "lustre_rsync.h"
126 #define REPLICATE_STATUS_VER 1
127 #define CLEAR_INTERVAL 100
128 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
130 #define TYPE_STR_LEN 16
132 #define DEFAULT_MDT "-MDT0000"
133 #define SPECIAL_DIR ".lustrerepl"
134 #define RSYNC "rsync"
141 /* Not used; declared for fulfilling obd.c's dependency. */
142 command_t cmdlist[0];
143 extern int obd_initialize(int argc, char **argv);
145 /* Information for processing a changelog record. This structure is
146 allocated on the heap instead of allocating large variables on the
151 unsigned int is_extended:1;
152 enum changelog_rec_type type;
153 char tfid[LR_FID_STR_LEN];
154 char pfid[LR_FID_STR_LEN];
155 char sfid[LR_FID_STR_LEN];
156 char spfid[LR_FID_STR_LEN];
157 char sname[NAME_MAX + 1];
158 char name[NAME_MAX + 1];
159 char src[PATH_MAX + 1];
160 char dest[PATH_MAX + 1];
161 char path[PATH_MAX + 1];
162 char savedpath[PATH_MAX + 1];
163 char link[PATH_MAX + 1];
164 char linktmp[PATH_MAX + 1];
169 /* Variables for querying the xattributes */
176 struct lr_parent_child_list {
177 struct lr_parent_child_log pc_log;
178 struct lr_parent_child_list *pc_next;
181 struct lustre_rsync_status *status;
182 char *statuslog; /* Name of the status log file */
184 int noxattr; /* Flag to turn off replicating xattrs */
185 int noclear; /* Flag to turn off clearing changelogs */
186 int debug; /* Flag to turn debugging information on and off */
187 int verbose; /* Verbose output */
188 long long rec_count; /* No of changelog records that were processed */
191 int use_rsync; /* Flag to turn on use of rsync to copy data */
192 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
193 int quit; /* Flag to stop processing the changelog; set on the
194 receipt of a signal */
195 int abort_on_err = 0;
197 char rsync[PATH_MAX];
198 char rsync_ver[PATH_MAX];
199 struct lr_parent_child_list *parents;
203 /* Command line options */
204 struct option long_opts[] = {
205 {"source", required_argument, 0, 's'},
206 {"target", required_argument, 0, 't'},
207 {"mdt", required_argument, 0, 'm'},
208 {"user", required_argument, 0, 'u'},
209 {"statuslog", required_argument, 0, 'l'},
210 {"verbose", no_argument, 0, 'v'},
211 {"xattr", required_argument, 0, 'x'},
212 {"dry-run", no_argument, 0, 'z'},
213 /* Undocumented options follow */
214 {"cl-clear", required_argument, 0, 'c'},
215 {"use-rsync", no_argument, 0, 'r'},
216 {"rsync-threshold", required_argument, 0, 'y'},
217 {"start-recno", required_argument, 0, 'n'},
218 {"abort-on-err",no_argument, 0, 'a'},
219 {"debug", required_argument, 0, 'd'},
220 {"debuglog", required_argument, 0, 'D'},
224 /* Command line usage */
227 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
228 "-m <mdt> -r <user id> -l <status log>\n"
229 "lustre_rsync can also pick up parameters from a "
230 "status log created earlier.\n"
231 "\tlustre_rsync -l <log_file>\n"
233 "\t--xattr <yes|no> replicate EAs\n"
234 "\t--abort-on-err abort at first err\n"
236 "\t--dry-run don't write anything\n");
239 #define DEBUG_ENTRY(info) \
240 lr_debug(D_TRACE, "***** Start %lld %s (%d) %s %s %s *****\n", \
241 (info)->recno, changelog_type2str((info)->type), \
242 (info)->type, (info)->tfid, (info)->pfid, (info)->name);
244 #define DEBUG_EXIT(info, rc) \
245 lr_debug(D_TRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n", \
246 (info)->recno, changelog_type2str((info)->type), \
247 (info)->type, (info)->tfid, (info)->pfid, (info)->name, rc);
249 /* Print debug information. This is controlled by the value of the
250 global variable 'debug' */
251 void lr_debug(int level, const char *fmt, ...)
259 if (debug_log != NULL)
260 vfprintf(debug_log, fmt, ap);
262 vfprintf(stdout, fmt, ap);
267 void * lr_grow_buf(void *buf, int size)
271 ptr = realloc(buf, size);
278 /* Use rsync to replicate file data */
279 int lr_rsync_data(struct lr_info *info)
282 struct stat st_src, st_dest;
285 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
287 rc = stat(info->src, &st_src);
289 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
290 info->src, info->name);
296 rc = stat(info->dest, &st_dest);
298 fprintf(stderr, "Error: Unable to stat dest=%s\n",
303 if (st_src.st_mtime != st_dest.st_mtime ||
304 st_src.st_size != st_dest.st_size) {
305 /* XXX spawning off an rsync for every data sync and
306 * waiting synchronously is bad for performance.
307 * librsync could possibly used here. But it does not
308 * seem to be of production grade. Multi-threaded
309 * replication is also to be considered.
312 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
314 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
315 status = system(cmd);
318 } else if (WIFEXITED(status)) {
319 status = WEXITSTATUS(status);
322 else if (status == 23 || status == 24)
323 /* Error due to vanished source files;
329 lr_debug(DINFO, "rsync %s exited with %d %d\n",
330 info->src, status, rc);
335 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
336 info->dest, info->tfid);
342 int lr_copy_data(struct lr_info *info)
352 fd_src = open(info->src, O_RDONLY);
355 if (fstat(fd_src, &st_src) == -1 ||
356 stat(info->dest, &st_dest) == -1)
359 if (st_src.st_mtime == st_dest.st_mtime &&
360 st_src.st_size == st_dest.st_size)
363 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
364 /* It is more efficient to use rsync to replicate
365 large files. Any file larger than rsync_threshold
366 is handed off to rsync. */
367 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
368 rc = lr_rsync_data(info);
372 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
377 bufsize = st_dest.st_blksize;
379 if (info->bufsize < bufsize) {
381 info->buf = lr_grow_buf(info->buf, bufsize);
382 if (info->buf == NULL) {
386 info->bufsize = bufsize;
390 rsize = read(fd_src, info->buf, bufsize);
393 } else if (rsize < 0) {
398 if (write(fd_dest, info->buf, rsize) != rsize) {
416 /* Copy data from source to destination */
417 int lr_sync_data(struct lr_info *info)
420 return lr_rsync_data(info);
422 return lr_copy_data(info);
425 /* Copy all attributes from file src to file dest */
426 int lr_copy_attr(char *src, char *dest)
431 if (stat(src, &st) == -1 ||
432 chmod(dest, st.st_mode) == -1 ||
433 chown(dest, st.st_uid, st.st_gid) == -1)
436 time.actime = st.st_atime;
437 time.modtime = st.st_mtime;
438 if (utime(dest, &time) == -1)
443 /* Copy all xattrs from file info->src to info->dest */
444 int lr_copy_xattr(struct lr_info *info)
446 size_t size = info->xsize;
455 rc = llistxattr(info->src, info->xlist, size);
456 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
457 info->src, info->xlist, rc, errno);
458 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
459 size = rc > PATH_MAX ? rc : PATH_MAX;
460 info->xlist = lr_grow_buf(info->xlist, size);
461 if (info->xlist == NULL)
464 rc = llistxattr(info->src, info->xlist, size);
465 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
466 info->src, rc, errno);
473 while (start < len) {
475 rc = lgetxattr(info->src, info->xlist + start,
477 if (info->xvalue == NULL || errno == ERANGE) {
478 size = rc > PATH_MAX ? rc : PATH_MAX;
479 info->xvalue = lr_grow_buf(info->xvalue, size);
480 if (info->xvalue == NULL)
483 rc = lgetxattr(info->src, info->xlist + start,
486 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
490 rc = lsetxattr(info->dest, info->xlist + start,
491 info->xvalue, size, 0);
492 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
495 if (errno != ENOTSUP) {
496 fprintf(stderr, "Error replicating "
497 " xattr for %s: %d\n",
504 start += strlen(info->xlist + start) + 1;
507 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
512 /* Retrieve the filesystem path for a given FID and a given
513 linkno. The path is returned in info->path */
514 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
516 long long recno = -1;
519 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
520 PATH_MAX, &recno, &linkno);
521 if (rc < 0 && rc != -ENOENT) {
522 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
523 status->ls_source, fidstr, -rc, strerror(errno = -rc));
529 /* Retrieve the filesystem path for a given FID. The path is returned
531 int lr_get_path(struct lr_info *info, char *fidstr)
533 return lr_get_path_ln(info, fidstr, 0);
536 /* Generate the path for opening by FID */
537 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
539 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
540 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
545 /* Read the symlink information into 'info->link' */
546 int lr_get_symlink(struct lr_info *info)
551 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
552 rc = readlink(info->src, info->linktmp, PATH_MAX);
554 info->linktmp[rc] = '\0';
557 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
559 if (strncmp(info->linktmp, status->ls_source,
560 strlen(status->ls_source)) == 0) {
561 /* Strip source fs path and replace with target fs path. */
562 link = info->linktmp + strlen(status->ls_source);
563 snprintf(info->src, PATH_MAX, "%s%s",
564 status->ls_targets[info->target_no], link);
567 link = info->linktmp;
569 strncpy(info->link, link, PATH_MAX);
570 info->link[PATH_MAX] = '\0';
575 /* Create file/directory/device file/symlink. */
576 int lr_mkfile(struct lr_info *info)
582 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
583 if (info->type == CL_MKDIR) {
584 rc = mkdir(info->dest, 0777);
585 } else if (info->type == CL_SOFTLINK) {
586 lr_get_symlink(info);
587 rc = symlink(info->link, info->dest);
588 } else if (info->type == CL_MKNOD) {
589 lr_get_FID_PATH(status->ls_source, info->tfid,
590 info->src, PATH_MAX);
591 rc = stat(info->src, &st);
598 rc = mknod(info->dest, st.st_mode, st.st_rdev);
600 rc = mknod(info->dest, S_IFREG | 0777, 0);
605 /* Sync data and attributes */
606 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
607 lr_debug(DTRACE, "Syncing data and attributes %s\n",
609 (void) lr_copy_xattr(info);
610 if (info->type == CL_CREATE)
611 rc = lr_sync_data(info);
613 rc = lr_copy_attr(info->src, info->dest);
616 /* Source file has disappeared. Not an error. */
619 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
626 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
628 struct lr_parent_child_list *p;
630 p = calloc(1, sizeof(*p));
633 strcpy(p->pc_log.pcl_pfid, pfid);
634 strcpy(p->pc_log.pcl_tfid, tfid);
635 strcpy(p->pc_log.pcl_name, name);
637 p->pc_next = parents;
642 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
644 struct lr_parent_child_list *curr, *prev;
648 d = calloc(1, PATH_MAX + 1);
649 prev = curr = parents;
651 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
652 snprintf(d, PATH_MAX, "%s/%s", dest,
653 curr->pc_log.pcl_name);
654 snprintf(info->src, PATH_MAX, "%s/%s/%s",
655 status->ls_targets[info->target_no],
656 SPECIAL_DIR, curr->pc_log.pcl_tfid);
657 rc = rename(info->src, d);
659 fprintf(stderr, "Error renaming file "
661 info->src, d, errno);
664 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
666 parents = curr->pc_next;
668 prev->pc_next = curr->pc_next;
670 prev = curr = parents;
674 curr = curr->pc_next;
681 /* remove [info->spfid, info->sfid] from parents */
682 int lr_remove_pc(const char *pfid, const char *tfid)
684 struct lr_parent_child_list *curr, *prev;
686 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
687 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
688 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
690 parents = curr->pc_next;
692 prev->pc_next = curr->pc_next;
700 /* Create file under SPECIAL_DIR with its tfid as its name. */
701 int lr_mk_special(struct lr_info *info)
705 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
706 status->ls_targets[info->target_no], SPECIAL_DIR,
709 rc = lr_mkfile(info);
713 rc = lr_add_pc(info->pfid, info->tfid, info->name);
717 /* Remove a file or directory */
718 int lr_rmfile(struct lr_info *info)
722 if (info->type == CL_RMDIR)
723 rc = rmdir(info->dest);
725 rc = unlink(info->dest);
731 /* Recursively remove directory and its contents */
732 int lr_rm_recursive(struct lr_info *info)
736 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
737 rc = system(info->cmd);
744 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
745 int lr_rm_special(struct lr_info *info)
749 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
750 status->ls_targets[info->target_no], SPECIAL_DIR,
752 rc = lr_rmfile(info);
755 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
756 info->dest, rc, errno);
760 /* Replicate file and directory create events */
761 int lr_create(struct lr_info *info)
768 /* Is target FID present on the source? */
769 rc = lr_get_path(info, info->tfid);
771 /* Source file has disappeared. Not an error. */
772 lr_debug(DINFO, "create: tfid %s not found on"
773 "source-fs\n", info->tfid);
778 strcpy(info->savedpath, info->path);
780 /* Is parent FID present on the source */
781 rc = lr_get_path(info, info->pfid);
783 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
790 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
791 len = strlen(info->path);
792 if (len == 1 && info->path[0] == '/')
793 snprintf(info->dest, PATH_MAX, "%s", info->name);
794 else if (len - 1 > 0 && info->path[len - 1] == '/')
795 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
797 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
799 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
801 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
802 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
803 info->tfid, info->dest, info->savedpath);
807 /* Is f2p(pfid) present on the target? If not, the parent has
810 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
812 if (access(info->dest, F_OK) != 0) {
813 lr_debug(DTRACE, "create: parent %s not found\n",
818 for (info->target_no = 0; info->target_no < status->ls_num_targets;
820 snprintf(info->dest, PATH_MAX, "%s/%s",
821 status->ls_targets[info->target_no], info->savedpath);
822 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
826 rc1 = lr_mkfile(info);
827 if (mkspecial || rc1 == -ENOENT) {
828 rc1 = lr_mk_special(info);
836 /* Replicate a file remove (rmdir/unlink) operation */
837 int lr_remove(struct lr_info *info)
842 for (info->target_no = 0; info->target_no < status->ls_num_targets;
845 rc1 = lr_rm_special(info);
849 rc1 = lr_get_path(info, info->pfid);
850 if (rc1 == -ENOENT) {
851 lr_debug(DINFO, "remove: pfid %s not found\n",
859 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
860 status->ls_targets[info->target_no], info->path,
863 rc1 = lr_rmfile(info);
864 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
865 info->dest, rc1, errno);
866 if (rc1 == -ENOTEMPTY)
867 rc1 = lr_rm_recursive(info);
877 /* Replicate a rename/move operation. */
878 int lr_move(struct lr_info *info)
884 int special_dest = 0;
885 char srcpath[PATH_MAX + 1] = "";
887 LASSERT(info->is_extended);
889 rc_src = lr_get_path(info, info->spfid);
890 if (rc_src < 0 && rc_src != -ENOENT)
892 memcpy(srcpath, info->path, strlen(info->path));
894 rc_dest = lr_get_path(info, info->pfid);
895 if (rc_dest < 0 && rc_dest != -ENOENT)
898 for (info->target_no = 0; info->target_no < status->ls_num_targets;
902 snprintf(info->dest, PATH_MAX, "%s/%s",
903 status->ls_targets[info->target_no],
905 if (access(info->dest, F_OK) != 0) {
908 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
909 status->ls_targets[info->target_no],
910 info->path, info->name);
912 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
915 if (rc_dest == -ENOENT) {
916 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
917 status->ls_targets[info->target_no],
918 SPECIAL_DIR, info->sfid);
920 lr_debug(DINFO, "special dest %s\n", info->dest);
924 snprintf(info->src, PATH_MAX, "%s/%s/%s",
925 status->ls_targets[info->target_no],
926 srcpath, info->sname);
927 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
930 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
932 snprintf(info->src, PATH_MAX, "%s/%s/%s",
933 status->ls_targets[info->target_no],
934 SPECIAL_DIR, info->sfid);
936 lr_debug(DINFO, "special src %s\n", info->src);
941 if (strcmp(info->src, info->dest) != 0) {
942 rc1 = rename(info->src, info->dest);
945 lr_debug(DINFO, "rename returns %d\n", rc1);
949 rc1 = lr_remove_pc(info->spfid, info->sfid);
951 lr_cascade_move(info->sfid, info->dest, info);
954 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
956 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
957 info->src, info->dest, rc1, errno);
964 /* Replicate a hard link */
965 int lr_link(struct lr_info *info)
973 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
974 rc = stat(info->src, &st);
978 for (info->target_no = 0; info->target_no < status->ls_num_targets;
985 /* Search through the hardlinks to get the src and dest */
986 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
987 info->dest[0] == 0); i++) {
988 rc1 = lr_get_path_ln(info, info->tfid, i);
989 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
990 info->path, info->name, i, rc1);
994 len = strlen(info->path) - strlen(info->name);
995 if (len > 0 && strcmp(info->path + len,
997 snprintf(info->dest, PATH_MAX, "%s/%s",
998 status->ls_targets[info->target_no],
1000 else if (info->src[0] == 0)
1001 snprintf(info->src, PATH_MAX, "%s/%s",
1002 status->ls_targets[info->target_no],
1011 if (info->src[0] == 0 || info->dest[0] == 0)
1012 /* Could not find the source or destination.
1013 This can happen when some links don't exist
1017 if (info->src[0] == 0)
1018 snprintf(info->src, PATH_MAX, "%s/%s/%s",
1019 status->ls_targets[info->target_no],
1020 SPECIAL_DIR, info->tfid);
1021 else if (info->dest[0] == 0)
1022 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1023 status->ls_targets[info->target_no],
1024 SPECIAL_DIR, info->tfid);
1026 rc1 = link(info->src, info->dest);
1027 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
1028 info->src, info->dest, rc1, strerror(errno));
1036 /* Replicate file attributes */
1037 int lr_setattr(struct lr_info *info)
1042 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1044 rc = lr_get_path(info, info->tfid);
1046 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1051 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1052 info->target_no++) {
1054 snprintf(info->dest, PATH_MAX, "%s/%s",
1055 status->ls_targets[info->target_no], info->path);
1056 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1059 rc1 = lr_sync_data(info);
1061 rc1 = lr_copy_attr(info->src, info->dest);
1068 /* Replicate xattrs */
1069 int lr_setxattr(struct lr_info *info)
1073 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1075 rc = lr_get_path(info, info->tfid);
1077 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1082 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1083 info->target_no++) {
1085 snprintf(info->dest, PATH_MAX, "%s/%s",
1086 status->ls_targets[info->target_no], info->path);
1087 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1090 rc1 = lr_copy_xattr(info);
1098 /* Parse a line of changelog entry */
1099 int lr_parse_line(void *priv, struct lr_info *info)
1101 struct changelog_ext_rec *rec;
1103 if (llapi_changelog_recv(priv, &rec) != 0)
1106 info->is_extended = CHANGELOG_REC_EXTENDED(rec);
1107 info->recno = rec->cr_index;
1108 info->type = rec->cr_type;
1109 sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
1110 sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
1111 strncpy(info->name, rec->cr_name, rec->cr_namelen);
1113 if (fid_is_sane(&rec->cr_sfid)) {
1114 sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
1115 sprintf(info->spfid, DFID, PFID(&rec->cr_spfid));
1116 strncpy(info->sname, changelog_rec_sname(rec),
1117 changelog_rec_snamelen(rec));
1118 info->sname[changelog_rec_snamelen(rec)] = '\0';
1121 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1122 info->name, info->sname);
1124 info->name[rec->cr_namelen] = '\0';
1127 printf("Rec %lld: %d %s\n", info->recno, info->type,
1131 llapi_changelog_free(&rec);
1137 /* Initialize the replication parameters */
1138 int lr_init_status()
1140 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1144 status = calloc(size, 1);
1147 status->ls_version = REPLICATE_STATUS_VER;
1148 status->ls_size = size;
1149 status->ls_last_recno = -1;
1153 /* Make a backup of the statuslog */
1154 void lr_backup_log()
1156 char backupfile[PATH_MAX];
1160 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1161 (void) rename(statuslog, backupfile);
1167 /* Save replication parameters to a statuslog. */
1172 size_t write_size = status->ls_size;
1173 struct lr_parent_child_list *curr;
1176 if (statuslog == NULL)
1181 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1184 fprintf(stderr, "Error opening log file for writing (%s)\n",
1189 size = write(fd, status, write_size);
1190 if (size != write_size) {
1191 fprintf(stderr, "Error writing to log file (%s) %d\n",
1197 for (curr = parents; curr; curr = curr->pc_next) {
1198 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1199 if (size != sizeof(curr->pc_log)) {
1200 fprintf(stderr, "Error writing to log file (%s) %d\n",
1210 /* Read statuslog and populate the replication parameters. Command
1211 * line parameters take precedence over parameters in the log file.*/
1214 struct lr_parent_child_list *tmp;
1215 struct lr_parent_child_log rec;
1216 struct lustre_rsync_status *s;
1219 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1222 if (statuslog == NULL)
1225 s = calloc(1, read_size);
1227 GOTO(out, rc = -ENOMEM);
1229 fd = open(statuslog, O_RDONLY);
1231 GOTO(out, rc = -errno);
1232 size = read(fd, s, read_size);
1233 if (size != read_size)
1234 GOTO(out, rc = -EINVAL);
1235 if (read_size < s->ls_size) {
1236 read_size = s->ls_size;
1237 s = lr_grow_buf(s, read_size);
1239 GOTO(out, rc = -ENOMEM);
1240 if (lseek(fd, 0, SEEK_SET) == -1)
1241 GOTO(out, rc = -errno);
1242 size = read(fd, s, read_size);
1243 if (size != read_size)
1244 GOTO(out, rc = -EINVAL);
1247 while (read(fd, &rec, sizeof(rec)) != 0) {
1248 tmp = calloc(1, sizeof(*tmp));
1250 GOTO(out, rc = -ENOMEM);
1252 tmp->pc_next = parents;
1256 /* copy uninitialized fields to status */
1257 if (status->ls_num_targets == 0) {
1258 if (status->ls_size != s->ls_size) {
1259 status = lr_grow_buf(status, s->ls_size);
1261 GOTO(out, rc = -ENOMEM);
1262 status->ls_size = s->ls_size;
1264 status->ls_num_targets = s->ls_num_targets;
1265 memcpy(status->ls_targets, s->ls_targets,
1266 PATH_MAX * s->ls_num_targets);
1268 if (status->ls_last_recno == -1)
1269 status->ls_last_recno = s->ls_last_recno;
1271 if (status->ls_registration[0] == '\0')
1272 strncpy(status->ls_registration, s->ls_registration,
1275 if (status->ls_mdt_device[0] == '\0')
1276 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1279 if (status->ls_source_fs[0] == '\0')
1280 strncpy(status->ls_source_fs, s->ls_source_fs,
1283 if (status->ls_source[0] == '\0')
1284 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1294 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1296 int lr_clear_cl(struct lr_info *info, int force)
1298 char mdt_device[LR_NAME_MAXLEN + 1];
1302 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1303 if (info->type == CL_RENAME)
1304 rec = info->recno + 1;
1307 if (!noclear && !dryrun) {
1308 /* llapi_changelog_clear modifies the mdt
1309 * device name so make a copy of it until this
1312 strncpy(mdt_device, status->ls_mdt_device,
1314 rc = llapi_changelog_clear(mdt_device,
1315 status->ls_registration,
1318 printf("Changelog clear (%s, %s, %lld) "
1319 "returned %d\n", status->ls_mdt_device,
1320 status->ls_registration, rec, rc);
1322 if (!rc && !dryrun) {
1323 status->ls_last_recno = rec;
1332 /* Locate a usable version of rsync. At this point we'll use any
1334 int lr_locate_rsync()
1340 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1341 fp = popen(rsync, "r");
1345 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1350 len = strlen(rsync);
1351 if (len > 0 && rsync[len - 1] == '\n')
1352 rsync[len - 1] = '\0';
1355 /* Determine the version of rsync */
1356 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1357 fp = popen(rsync_ver, "r");
1361 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1365 len = strlen(rsync_ver);
1366 if (len > 0 && rsync_ver[len - 1] == '\n')
1367 rsync_ver[len - 1] = '\0';
1374 /* Print the replication parameters */
1375 void lr_print_status(struct lr_info *info)
1382 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1383 printf("MDT device: %s\n", status->ls_mdt_device);
1384 printf("Source: %s\n", status->ls_source);
1385 for (i = 0; i < status->ls_num_targets; i++)
1386 printf("Target: %s\n", status->ls_targets[i]);
1387 if (statuslog != NULL)
1388 printf("Statuslog: %s\n", statuslog);
1389 printf("Changelog registration: %s\n", status->ls_registration);
1390 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1392 printf("Replicate xattrs: no\n");
1394 printf("Clear changelog after use: no\n");
1396 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1399 void lr_print_failure(struct lr_info *info, int rc)
1401 fprintf(stderr, "Replication of operation failed(%d):"
1402 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1403 changelog_type2str(info->type), info->type, info->tfid,
1404 info->pfid, info->name);
1407 /* Replicate filesystem operations from src_path to target_path */
1410 void *changelog_priv;
1411 struct lr_info *info;
1412 struct lr_info *ext = NULL;
1420 info = calloc(1, sizeof(struct lr_info));
1424 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1426 fprintf(stderr, "Source path is not a valid Lustre client "
1430 if (status->ls_mdt_device[0] == '\0')
1431 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1432 status->ls_source_fs, DEFAULT_MDT);
1434 ext = calloc(1, sizeof(struct lr_info));
1440 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1441 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1443 rc = mkdir(info->dest, 0777);
1444 if (rc == -1 && errno != EEXIST) {
1445 fprintf(stderr, "Error writing to target path %s.\n",
1446 status->ls_targets[i]);
1450 rc = llistxattr(info->src, info->xlist, info->xsize);
1451 if (rc == -1 && errno == ENOTSUP) {
1452 fprintf(stderr, "xattrs not supported on %s\n",
1453 status->ls_targets[i]);
1457 if (xattr_not_supp == status->ls_num_targets)
1458 /* None of the targets support xattrs. */
1461 lr_print_status(info);
1463 /* Open changelogs for consumption*/
1464 rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
1465 status->ls_source_fs, status->ls_last_recno);
1467 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1468 status->ls_source_fs);
1472 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1474 if (info->type == CL_RENAME && !info->is_extended) {
1475 /* Newer rename operations extends changelog to store
1476 * source file information, but old changelog has
1479 if (lr_parse_line(changelog_priv, ext) != 0)
1481 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1482 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1483 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1484 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1485 strncpy(info->sname, info->name, sizeof(info->sname));
1486 strncpy(info->name, ext->name, sizeof(info->name));
1487 info->is_extended = 1;
1495 switch(info->type) {
1500 rc = lr_create(info);
1504 rc = lr_remove(info);
1514 rc = lr_setattr(info);
1517 rc = lr_setxattr(info);
1524 /* Nothing needs to be done for these entries */
1528 DEBUG_EXIT(info, rc);
1529 if (rc && rc != -ENOENT) {
1530 lr_print_failure(info, rc);
1535 lr_clear_cl(info, 0);
1537 bzero(info, sizeof(struct lr_info));
1538 bzero(ext, sizeof(struct lr_info));
1542 llapi_changelog_fini(&changelog_priv);
1544 if (errors || verbose)
1545 printf("Errors: %d\n", errors);
1547 /* Clear changelog records used so far */
1548 lr_clear_cl(info, 1);
1551 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1552 printf("Changelog records consumed: %lld\n", rec_count);
1567 termination_handler (int signum)
1569 /* Set a flag for the replicator to gracefully shutdown */
1571 printf("lustre_rsync halting.\n");
1574 int main(int argc, char *argv[])
1580 if ((rc = lr_init_status()) != 0)
1583 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1584 long_opts, NULL)) >= 0) {
1587 /* Assume absolute paths */
1591 /* Assume absolute paths */
1592 strncpy(status->ls_source, optarg, PATH_MAX);
1595 status->ls_num_targets++;
1597 if (numtargets != status->ls_num_targets) {
1598 /* Targets were read from a log
1599 file. The ones specified on the
1600 command line take precedence. The
1601 ones from the log file will be
1603 status->ls_num_targets = numtargets;
1605 newsize = sizeof (struct lustre_rsync_status) +
1606 (status->ls_num_targets * PATH_MAX);
1607 if (status->ls_size != newsize) {
1608 status->ls_size = newsize;
1609 status = lr_grow_buf(status, newsize);
1613 strncpy(status->ls_targets[status->ls_num_targets - 1],
1618 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1621 strncpy(status->ls_registration, optarg,
1626 (void) lr_read_log();
1632 if (strcmp("no", optarg) == 0) {
1634 } else if (strcmp("yes", optarg) != 0) {
1635 printf("Invalid parameter %s. "
1636 "Specify --xattr=no or --xattr=yes\n",
1645 /* Undocumented option cl-clear */
1646 if (strcmp("no", optarg) == 0) {
1648 } else if (strcmp("yes", optarg) != 0) {
1649 printf("Invalid parameter %s. "
1650 "Specify --cl-clear=no "
1651 "or --cl-clear=yes\n",
1657 /* Undocumented option use-rsync */
1661 /* Undocumented option rsync-threshold */
1662 rsync_threshold = atol(optarg);
1665 /* Undocumented option start-recno */
1666 status->ls_last_recno = atol(optarg);
1669 /* Undocumented option debug */
1670 debug = atoi(optarg);
1671 if (debug < 0 || debug > 2)
1675 /* Undocumented option debug log file */
1676 debug_log = fopen(optarg, "a");
1677 if (debug_log == NULL) {
1678 printf("Cannot open %s for debug log\n",
1684 fprintf(stderr, "error: %s: option '%s' "
1685 "unrecognized.\n", argv[0], argv[optind - 1]);
1691 if (status->ls_last_recno == -1)
1692 status->ls_last_recno = 0;
1693 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1694 /* No registration ID was passed in. */
1695 printf("Please specify changelog consumer registration id.\n");
1699 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1700 fprintf(stderr, "Please specify the source path.\n");
1704 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1705 fprintf(stderr, "Please specify the target path.\n");
1710 /* This plumbing is needed for some of the ioctls behind
1711 llapi calls to work. */
1712 if (obd_initialize(argc, argv) < 0) {
1713 fprintf(stderr, "obd_initialize failed.\n");
1717 rc = lr_locate_rsync();
1718 if (use_rsync && rc != 0) {
1719 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1723 signal(SIGINT, termination_handler);
1724 signal(SIGHUP, termination_handler);
1725 signal(SIGTERM, termination_handler);
1727 rc = lr_replicate();
1729 if (debug_log != NULL)