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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/lustre_rsync.c
34 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
35 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
39 * - lustre_rsync is a tool for replicating a lustre filesystem.
41 * - The source-fs is a live lustre filesystem. It is not a
42 * snapshot. It is mounted and undergoing changes
44 * - The target-fs is a copy of the source-fs from the past. Let's
45 * call this point, the 'sync point'.
47 * - There is a changelog of all metadata operations that happened on
48 * the filesystem since the 'sync point'.
50 * - lustre_rsync replicates all the operations saved in the changelog
51 * on to the target filesystem to make it identical to the source.
53 * To facilitate replication, the lustre filesystem provides
54 * a) a way to get the current filesystem path of a given FID
55 * b) a way to open files by specifying its FID
57 * The changelog only has a limited amount of information.
58 * tfid - The FID of the target file
59 * pfid - The FID of the parent of the target file (at the time of
61 * sfid - The FID of the source file
62 * spfid - The FID of the parent of the source file
63 * name - The name of the target file (at the time of the operation), the name
64 * of the source file is appended (delimited with '\0') if this
65 * operation involves a source
67 * With just this information, it is not alwasy possible to determine
68 * the file paths for each operation. For instance, if pfid does not
69 * exist on the source-fs (due to a subsequent deletion), its path
70 * cannot be queried. In such cases, lustre_rsync keeps the files in a
71 * special directory ("/.lustrerepl"). Once all the operations in a
72 * changelog are replayed, all the files in this special directory
73 * will get moved to the location as in the source-fs.
75 * Shorthand used: f2p(fid) = fid2path(fid)
77 * The following are the metadata operations of interest.
79 * If tfid is absent on the source-fs, ignore this operation
80 * If pfid is absent on the source-fs [or]
81 * if f2p(pfid) is not present on target-fs [or]
82 * if f2p(pfid)+name != f2p(tfid)
83 * creat .lustrerepl/tfid
84 * track [pfid,tfid,name]
89 * If .lustrerepl/[tfid] is present on the target
90 * rm .lustrerepl/[tfid]
91 * Else if pfid is present on the source-fs,
92 * if f2p(pfid)+name is present,
95 * 3. move (spfid,sname) to (pfid,name)
97 * if spfid is also present, mv (spfid,sname) to (pfid,name)
98 * else mv .lustrerepl/[sfid] to (pfid,name)
99 * Else if pfid is not present,
100 * if spfid is present, mv (spfid,sname) .lustrerepl/[sfid]
101 * If moving out of .lustrerepl
102 * move out all its children in .lustrerepl.
103 * [pfid,tfid,name] tracked from (1) is used for this.
114 #include <sys/stat.h>
115 #include <sys/types.h>
120 #include <sys/xattr.h>
122 #include <libcfs/util/string.h>
123 #include <libcfs/util/parser.h>
124 #include <lustre/lustreapi.h>
125 #include <lustre/lustre_idl.h>
126 #include "lustre_rsync.h"
128 #define REPLICATE_STATUS_VER 1
129 #define CLEAR_INTERVAL 100
130 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
132 #define TYPE_STR_LEN 16
134 #define DEFAULT_MDT "-MDT0000"
135 #define SPECIAL_DIR ".lustrerepl"
136 #define RSYNC "rsync"
143 /* Not used; declared for fulfilling obd.c's dependency. */
144 command_t cmdlist[0];
145 extern int obd_initialize(int argc, char **argv);
147 /* Information for processing a changelog record. This structure is
148 allocated on the heap instead of allocating large variables on the
153 unsigned int is_extended:1;
154 enum changelog_rec_type type;
155 char tfid[LR_FID_STR_LEN];
156 char pfid[LR_FID_STR_LEN];
157 char sfid[LR_FID_STR_LEN];
158 char spfid[LR_FID_STR_LEN];
159 char sname[NAME_MAX + 1];
160 char name[NAME_MAX + 1];
161 char src[PATH_MAX + 1];
162 char dest[PATH_MAX + 1];
163 char path[PATH_MAX + 1];
164 char savedpath[PATH_MAX + 1];
165 char link[PATH_MAX + 1];
166 char linktmp[PATH_MAX + 1];
171 /* Variables for querying the xattributes */
178 struct lr_parent_child_list {
179 struct lr_parent_child_log pc_log;
180 struct lr_parent_child_list *pc_next;
183 struct lustre_rsync_status *status;
184 char *statuslog; /* Name of the status log file */
186 int noxattr; /* Flag to turn off replicating xattrs */
187 int noclear; /* Flag to turn off clearing changelogs */
188 int debug; /* Flag to turn debugging information on and off */
189 int verbose; /* Verbose output */
190 long long rec_count; /* No of changelog records that were processed */
193 int use_rsync; /* Flag to turn on use of rsync to copy data */
194 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
195 int quit; /* Flag to stop processing the changelog; set on the
196 receipt of a signal */
197 int abort_on_err = 0;
199 char rsync[PATH_MAX];
200 char rsync_ver[PATH_MAX];
201 struct lr_parent_child_list *parents;
205 /* Command line options */
206 struct option long_opts[] = {
207 {"source", required_argument, 0, 's'},
208 {"target", required_argument, 0, 't'},
209 {"mdt", required_argument, 0, 'm'},
210 {"user", required_argument, 0, 'u'},
211 {"statuslog", required_argument, 0, 'l'},
212 {"verbose", no_argument, 0, 'v'},
213 {"xattr", required_argument, 0, 'x'},
214 {"dry-run", no_argument, 0, 'z'},
215 /* Undocumented options follow */
216 {"cl-clear", required_argument, 0, 'c'},
217 {"use-rsync", no_argument, 0, 'r'},
218 {"rsync-threshold", required_argument, 0, 'y'},
219 {"start-recno", required_argument, 0, 'n'},
220 {"abort-on-err",no_argument, 0, 'a'},
221 {"debug", required_argument, 0, 'd'},
222 {"debuglog", required_argument, 0, 'D'},
226 /* Command line usage */
229 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
230 "-m <mdt> -r <user id> -l <status log>\n"
231 "lustre_rsync can also pick up parameters from a "
232 "status log created earlier.\n"
233 "\tlustre_rsync -l <log_file>\n"
235 "\t--xattr <yes|no> replicate EAs\n"
236 "\t--abort-on-err abort at first err\n"
238 "\t--dry-run don't write anything\n");
241 #define DEBUG_ENTRY(info) \
242 lr_debug(D_TRACE, "***** Start %lld %s (%d) %s %s %s *****\n", \
243 (info)->recno, changelog_type2str((info)->type), \
244 (info)->type, (info)->tfid, (info)->pfid, (info)->name);
246 #define DEBUG_EXIT(info, rc) \
247 lr_debug(D_TRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n", \
248 (info)->recno, changelog_type2str((info)->type), \
249 (info)->type, (info)->tfid, (info)->pfid, (info)->name, rc);
251 /* Print debug information. This is controlled by the value of the
252 global variable 'debug' */
253 void lr_debug(int level, const char *fmt, ...)
261 if (debug_log != NULL)
262 vfprintf(debug_log, fmt, ap);
264 vfprintf(stdout, fmt, ap);
269 void * lr_grow_buf(void *buf, int size)
273 ptr = realloc(buf, size);
280 /* Use rsync to replicate file data */
281 int lr_rsync_data(struct lr_info *info)
284 struct stat st_src, st_dest;
287 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
289 rc = stat(info->src, &st_src);
291 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
292 info->src, info->name);
298 rc = stat(info->dest, &st_dest);
300 fprintf(stderr, "Error: Unable to stat dest=%s\n",
305 if (st_src.st_mtime != st_dest.st_mtime ||
306 st_src.st_size != st_dest.st_size) {
307 /* XXX spawning off an rsync for every data sync and
308 * waiting synchronously is bad for performance.
309 * librsync could possibly used here. But it does not
310 * seem to be of production grade. Multi-threaded
311 * replication is also to be considered.
314 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
316 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
317 status = system(cmd);
320 } else if (WIFEXITED(status)) {
321 status = WEXITSTATUS(status);
324 else if (status == 23 || status == 24)
325 /* Error due to vanished source files;
331 lr_debug(DINFO, "rsync %s exited with %d %d\n",
332 info->src, status, rc);
337 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
338 info->dest, info->tfid);
344 int lr_copy_data(struct lr_info *info)
354 fd_src = open(info->src, O_RDONLY);
357 if (fstat(fd_src, &st_src) == -1 ||
358 stat(info->dest, &st_dest) == -1)
361 if (st_src.st_mtime == st_dest.st_mtime &&
362 st_src.st_size == st_dest.st_size)
365 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
366 /* It is more efficient to use rsync to replicate
367 large files. Any file larger than rsync_threshold
368 is handed off to rsync. */
369 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
370 rc = lr_rsync_data(info);
374 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
379 bufsize = st_dest.st_blksize;
381 if (info->bufsize < bufsize) {
383 info->buf = lr_grow_buf(info->buf, bufsize);
384 if (info->buf == NULL) {
388 info->bufsize = bufsize;
396 rsize = read(fd_src, buf, bufsize);
406 wsize = write(fd_dest, buf, rsize);
426 /* Copy data from source to destination */
427 int lr_sync_data(struct lr_info *info)
430 return lr_rsync_data(info);
432 return lr_copy_data(info);
435 /* Copy all attributes from file src to file dest */
436 int lr_copy_attr(char *src, char *dest)
441 if (stat(src, &st) == -1 ||
442 chmod(dest, st.st_mode) == -1 ||
443 chown(dest, st.st_uid, st.st_gid) == -1)
446 time.actime = st.st_atime;
447 time.modtime = st.st_mtime;
448 if (utime(dest, &time) == -1)
453 /* Copy all xattrs from file info->src to info->dest */
454 int lr_copy_xattr(struct lr_info *info)
456 size_t size = info->xsize;
465 rc = llistxattr(info->src, info->xlist, size);
466 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
467 info->src, info->xlist, rc, errno);
468 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
469 size = rc > PATH_MAX ? rc : PATH_MAX;
470 info->xlist = lr_grow_buf(info->xlist, size);
471 if (info->xlist == NULL)
474 rc = llistxattr(info->src, info->xlist, size);
475 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
476 info->src, rc, errno);
483 while (start < len) {
485 rc = lgetxattr(info->src, info->xlist + start,
487 if (info->xvalue == NULL || errno == ERANGE) {
488 size = rc > PATH_MAX ? rc : PATH_MAX;
489 info->xvalue = lr_grow_buf(info->xvalue, size);
490 if (info->xvalue == NULL)
493 rc = lgetxattr(info->src, info->xlist + start,
496 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
500 rc = lsetxattr(info->dest, info->xlist + start,
501 info->xvalue, size, 0);
502 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
505 if (errno != ENOTSUP) {
506 fprintf(stderr, "Error replicating "
507 " xattr for %s: %d\n",
514 start += strlen(info->xlist + start) + 1;
517 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
522 /* Retrieve the filesystem path for a given FID and a given
523 linkno. The path is returned in info->path */
524 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
526 long long recno = -1;
529 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
530 PATH_MAX, &recno, &linkno);
531 if (rc < 0 && rc != -ENOENT) {
532 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
533 status->ls_source, fidstr, -rc, strerror(errno = -rc));
539 /* Retrieve the filesystem path for a given FID. The path is returned
541 int lr_get_path(struct lr_info *info, char *fidstr)
543 return lr_get_path_ln(info, fidstr, 0);
546 /* Generate the path for opening by FID */
547 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
549 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
550 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
555 /* Read the symlink information into 'info->link' */
556 int lr_get_symlink(struct lr_info *info)
561 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
562 rc = readlink(info->src, info->linktmp, PATH_MAX);
564 info->linktmp[rc] = '\0';
567 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
569 if (strncmp(info->linktmp, status->ls_source,
570 strlen(status->ls_source)) == 0) {
571 /* Strip source fs path and replace with target fs path. */
572 link = info->linktmp + strlen(status->ls_source);
573 snprintf(info->src, PATH_MAX, "%s%s",
574 status->ls_targets[info->target_no], link);
577 link = info->linktmp;
579 strlcpy(info->link, link, sizeof(info->link));
584 /* Create file/directory/device file/symlink. */
585 int lr_mkfile(struct lr_info *info)
591 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
592 if (info->type == CL_MKDIR) {
593 rc = mkdir(info->dest, 0777);
594 } else if (info->type == CL_SOFTLINK) {
595 lr_get_symlink(info);
596 rc = symlink(info->link, info->dest);
597 } else if (info->type == CL_MKNOD) {
598 lr_get_FID_PATH(status->ls_source, info->tfid,
599 info->src, PATH_MAX);
600 rc = stat(info->src, &st);
607 rc = mknod(info->dest, st.st_mode, st.st_rdev);
609 rc = mknod(info->dest, S_IFREG | 0777, 0);
619 /* Sync data and attributes */
620 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
621 lr_debug(DTRACE, "Syncing data and attributes %s\n",
623 (void) lr_copy_xattr(info);
624 if (info->type == CL_CREATE)
625 rc = lr_sync_data(info);
627 rc = lr_copy_attr(info->src, info->dest);
630 /* Source file has disappeared. Not an error. */
633 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
640 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
642 struct lr_parent_child_list *p;
645 p = calloc(1, sizeof(*p));
648 len = strlcpy(p->pc_log.pcl_pfid, pfid, sizeof(p->pc_log.pcl_pfid));
649 if (len >= sizeof(p->pc_log.pcl_pfid))
651 len = strlcpy(p->pc_log.pcl_tfid, tfid, sizeof(p->pc_log.pcl_tfid));
652 if (len >= sizeof(p->pc_log.pcl_tfid))
654 len = strlcpy(p->pc_log.pcl_name, name, sizeof(p->pc_log.pcl_name));
655 if (len >= sizeof(p->pc_log.pcl_name))
658 p->pc_next = parents;
667 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
669 struct lr_parent_child_list *curr, *prev;
673 d = calloc(1, PATH_MAX + 1);
674 prev = curr = parents;
676 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
677 snprintf(d, PATH_MAX, "%s/%s", dest,
678 curr->pc_log.pcl_name);
679 snprintf(info->src, PATH_MAX, "%s/%s/%s",
680 status->ls_targets[info->target_no],
681 SPECIAL_DIR, curr->pc_log.pcl_tfid);
682 rc = rename(info->src, d);
684 fprintf(stderr, "Error renaming file "
686 info->src, d, errno);
690 parents = curr->pc_next;
692 prev->pc_next = curr->pc_next;
693 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
695 prev = curr = parents;
699 curr = curr->pc_next;
706 /* remove [info->spfid, info->sfid] from parents */
707 int lr_remove_pc(const char *pfid, const char *tfid)
709 struct lr_parent_child_list *curr, *prev;
711 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
712 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
713 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
715 parents = curr->pc_next;
717 prev->pc_next = curr->pc_next;
725 /* Create file under SPECIAL_DIR with its tfid as its name. */
726 int lr_mk_special(struct lr_info *info)
730 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
731 status->ls_targets[info->target_no], SPECIAL_DIR,
734 rc = lr_mkfile(info);
738 rc = lr_add_pc(info->pfid, info->tfid, info->name);
742 /* Remove a file or directory */
743 int lr_rmfile(struct lr_info *info)
747 if (info->type == CL_RMDIR)
748 rc = rmdir(info->dest);
750 rc = unlink(info->dest);
756 /* Recursively remove directory and its contents */
757 int lr_rm_recursive(struct lr_info *info)
761 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
762 rc = system(info->cmd);
769 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
770 int lr_rm_special(struct lr_info *info)
774 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
775 status->ls_targets[info->target_no], SPECIAL_DIR,
777 rc = lr_rmfile(info);
780 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
781 info->dest, rc, errno);
785 /* Replicate file and directory create events */
786 int lr_create(struct lr_info *info)
793 /* Is target FID present on the source? */
794 rc = lr_get_path(info, info->tfid);
796 /* Source file has disappeared. Not an error. */
797 lr_debug(DINFO, "create: tfid %s not found on"
798 "source-fs\n", info->tfid);
803 strcpy(info->savedpath, info->path);
805 /* Is parent FID present on the source */
806 rc = lr_get_path(info, info->pfid);
808 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
815 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
816 len = strlen(info->path);
817 if (len == 1 && info->path[0] == '/')
818 snprintf(info->dest, PATH_MAX, "%s", info->name);
819 else if (len - 1 > 0 && info->path[len - 1] == '/')
820 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
822 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
824 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
826 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
827 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
828 info->tfid, info->dest, info->savedpath);
832 /* Is f2p(pfid) present on the target? If not, the parent has
835 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
837 if (access(info->dest, F_OK) != 0) {
838 lr_debug(DTRACE, "create: parent %s not found\n",
843 for (info->target_no = 0; info->target_no < status->ls_num_targets;
845 snprintf(info->dest, PATH_MAX, "%s/%s",
846 status->ls_targets[info->target_no], info->savedpath);
847 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
851 rc1 = lr_mkfile(info);
852 if (mkspecial || rc1 == -ENOENT) {
853 rc1 = lr_mk_special(info);
861 /* Replicate a file remove (rmdir/unlink) operation */
862 int lr_remove(struct lr_info *info)
867 for (info->target_no = 0; info->target_no < status->ls_num_targets;
870 rc1 = lr_rm_special(info);
874 rc1 = lr_get_path(info, info->pfid);
875 if (rc1 == -ENOENT) {
876 lr_debug(DINFO, "remove: pfid %s not found\n",
884 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
885 status->ls_targets[info->target_no], info->path,
888 rc1 = lr_rmfile(info);
889 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
890 info->dest, rc1, errno);
891 if (rc1 == -ENOTEMPTY)
892 rc1 = lr_rm_recursive(info);
902 /* Replicate a rename/move operation. */
903 int lr_move(struct lr_info *info)
909 int special_dest = 0;
910 char srcpath[PATH_MAX + 1] = "";
912 LASSERT(info->is_extended);
914 rc_src = lr_get_path(info, info->spfid);
915 if (rc_src < 0 && rc_src != -ENOENT)
917 memcpy(srcpath, info->path, strlen(info->path));
919 rc_dest = lr_get_path(info, info->pfid);
920 if (rc_dest < 0 && rc_dest != -ENOENT)
923 for (info->target_no = 0; info->target_no < status->ls_num_targets;
927 snprintf(info->dest, PATH_MAX, "%s/%s",
928 status->ls_targets[info->target_no],
930 if (access(info->dest, F_OK) != 0) {
933 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
934 status->ls_targets[info->target_no],
935 info->path, info->name);
937 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
940 if (rc_dest == -ENOENT) {
941 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
942 status->ls_targets[info->target_no],
943 SPECIAL_DIR, info->sfid);
945 lr_debug(DINFO, "special dest %s\n", info->dest);
949 snprintf(info->src, PATH_MAX, "%s/%s/%s",
950 status->ls_targets[info->target_no],
951 srcpath, info->sname);
952 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
955 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
957 snprintf(info->src, PATH_MAX, "%s/%s/%s",
958 status->ls_targets[info->target_no],
959 SPECIAL_DIR, info->sfid);
961 lr_debug(DINFO, "special src %s\n", info->src);
966 if (strcmp(info->src, info->dest) != 0) {
967 rc1 = rename(info->src, info->dest);
970 lr_debug(DINFO, "rename returns %d\n", rc1);
974 rc1 = lr_remove_pc(info->spfid, info->sfid);
977 lr_cascade_move(info->sfid, info->dest, info);
979 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
981 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
982 info->src, info->dest, rc1, errno);
989 /* Replicate a hard link */
990 int lr_link(struct lr_info *info)
997 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
998 rc = stat(info->src, &st);
1002 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1003 info->target_no++) {
1010 * The changelog record has the new parent directory FID and
1011 * name of the target file. So info->dest can be constructed
1012 * by getting the path of the new parent directory and
1013 * appending the target file name.
1015 rc1 = lr_get_path(info, info->pfid);
1016 lr_debug(rc1 ? 0 : DTRACE, "\tparent fid2path %s, %s, rc=%d\n",
1017 info->path, info->name, rc1);
1020 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1021 status->ls_targets[info->target_no],
1022 info->path, info->name);
1023 lr_debug(DINFO, "link destination is %s\n", info->dest);
1026 /* Search through the hardlinks to get the src */
1027 for (i = 0; i < st.st_nlink && info->src[0] == 0; i++) {
1028 rc1 = lr_get_path_ln(info, info->tfid, i);
1029 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
1030 info->path, info->name, i, rc1);
1035 * Compare the path of target FID with info->dest
1036 * to find out info->src.
1038 char srcpath[PATH_MAX];
1040 snprintf(srcpath, sizeof(srcpath), "%s/%s",
1041 status->ls_targets[info->target_no],
1044 if (strcmp(srcpath, info->dest) != 0) {
1045 strlcpy(info->src, srcpath, sizeof(info->src));
1046 lr_debug(DINFO, "link source is %s\n",
1056 if (info->src[0] == 0)
1057 snprintf(info->src, PATH_MAX, "%s/%s/%s",
1058 status->ls_targets[info->target_no],
1059 SPECIAL_DIR, info->tfid);
1060 else if (info->dest[0] == 0)
1061 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1062 status->ls_targets[info->target_no],
1063 SPECIAL_DIR, info->tfid);
1065 rc1 = link(info->src, info->dest);
1066 lr_debug(DINFO, "link: %s [to] %s; rc1=%d %s\n",
1067 info->src, info->dest, rc1,
1068 strerror(rc1 ? errno : 0));
1076 /* Replicate file attributes */
1077 int lr_setattr(struct lr_info *info)
1082 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1084 rc = lr_get_path(info, info->tfid);
1086 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1091 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1092 info->target_no++) {
1094 snprintf(info->dest, PATH_MAX, "%s/%s",
1095 status->ls_targets[info->target_no], info->path);
1096 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1099 rc1 = lr_sync_data(info);
1101 rc1 = lr_copy_attr(info->src, info->dest);
1108 /* Replicate xattrs */
1109 int lr_setxattr(struct lr_info *info)
1113 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1115 rc = lr_get_path(info, info->tfid);
1117 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1122 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1123 info->target_no++) {
1125 snprintf(info->dest, PATH_MAX, "%s/%s",
1126 status->ls_targets[info->target_no], info->path);
1127 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1130 rc1 = lr_copy_xattr(info);
1138 /* Parse a line of changelog entry */
1139 int lr_parse_line(void *priv, struct lr_info *info)
1141 struct changelog_rec *rec;
1142 struct changelog_ext_rename *rnm;
1144 size_t copylen = sizeof(info->name);
1146 if (llapi_changelog_recv(priv, &rec) != 0)
1149 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1150 info->recno = rec->cr_index;
1151 info->type = rec->cr_type;
1152 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1153 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1155 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1156 if (copylen > namelen + 1)
1157 copylen = namelen + 1;
1158 strlcpy(info->name, changelog_rec_name(rec), copylen);
1160 /* Don't use rnm if CLF_RENAME isn't set */
1161 rnm = changelog_rec_rename(rec);
1162 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1163 copylen = sizeof(info->sname);
1165 snprintf(info->sfid, sizeof(info->sfid), DFID,
1166 PFID(&rnm->cr_sfid));
1167 snprintf(info->spfid, sizeof(info->spfid), DFID,
1168 PFID(&rnm->cr_spfid));
1169 namelen = changelog_rec_snamelen(rec);
1170 if (copylen > namelen + 1)
1171 copylen = namelen + 1;
1172 strlcpy(info->sname, changelog_rec_sname(rec), copylen);
1175 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1176 info->name, info->sname);
1179 printf("Rec %lld: %d %s\n", info->recno, info->type,
1183 llapi_changelog_free(&rec);
1189 /* Initialize the replication parameters */
1190 int lr_init_status()
1192 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1196 status = calloc(size, 1);
1199 status->ls_version = REPLICATE_STATUS_VER;
1200 status->ls_size = size;
1201 status->ls_last_recno = -1;
1205 /* Make a backup of the statuslog */
1206 void lr_backup_log()
1208 char backupfile[PATH_MAX];
1212 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1213 (void) rename(statuslog, backupfile);
1219 /* Save replication parameters to a statuslog. */
1224 size_t write_size = status->ls_size;
1225 struct lr_parent_child_list *curr;
1228 if (statuslog == NULL)
1233 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1236 fprintf(stderr, "Error opening log file for writing (%s)\n",
1241 size = write(fd, status, write_size);
1242 if (size != write_size) {
1243 fprintf(stderr, "Error writing to log file (%s) %d\n",
1249 for (curr = parents; curr; curr = curr->pc_next) {
1250 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1251 if (size != sizeof(curr->pc_log)) {
1252 fprintf(stderr, "Error writing to log file (%s) %d\n",
1262 /* Read statuslog and populate the replication parameters. Command
1263 * line parameters take precedence over parameters in the log file.*/
1266 struct lr_parent_child_list *tmp;
1267 struct lr_parent_child_log rec;
1268 struct lustre_rsync_status *s;
1271 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1274 if (statuslog == NULL)
1277 s = calloc(1, read_size);
1283 fd = open(statuslog, O_RDONLY);
1289 size = read(fd, s, read_size);
1290 if (size != read_size) {
1295 if (read_size < s->ls_size) {
1296 read_size = s->ls_size;
1297 s = lr_grow_buf(s, read_size);
1303 if (lseek(fd, 0, SEEK_SET) == -1) {
1308 size = read(fd, s, read_size);
1309 if (size != read_size) {
1315 while (read(fd, &rec, sizeof(rec)) != 0) {
1316 tmp = calloc(1, sizeof(*tmp));
1323 tmp->pc_next = parents;
1327 /* copy uninitialized fields to status */
1328 if (status->ls_num_targets == 0) {
1329 if (status->ls_size != s->ls_size) {
1330 status = lr_grow_buf(status, s->ls_size);
1331 if (status == NULL) {
1336 status->ls_size = s->ls_size;
1338 status->ls_num_targets = s->ls_num_targets;
1339 memcpy(status->ls_targets, s->ls_targets,
1340 (PATH_MAX + 1) * s->ls_num_targets);
1342 if (status->ls_last_recno == -1)
1343 status->ls_last_recno = s->ls_last_recno;
1345 if (status->ls_registration[0] == '\0')
1346 strlcpy(status->ls_registration, s->ls_registration,
1347 sizeof(status->ls_registration));
1349 if (status->ls_mdt_device[0] == '\0')
1350 strlcpy(status->ls_mdt_device, s->ls_mdt_device,
1351 sizeof(status->ls_mdt_device));
1353 if (status->ls_source_fs[0] == '\0')
1354 strlcpy(status->ls_source_fs, s->ls_source_fs,
1355 sizeof(status->ls_source_fs));
1357 if (status->ls_source[0] == '\0')
1358 strlcpy(status->ls_source, s->ls_source,
1359 sizeof(status->ls_source));
1369 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1371 int lr_clear_cl(struct lr_info *info, int force)
1373 char mdt_device[LR_NAME_MAXLEN + 1];
1377 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1378 if (info->type == CL_RENAME)
1379 rec = info->recno + 1;
1382 if (!noclear && !dryrun) {
1383 /* llapi_changelog_clear modifies the mdt
1384 * device name so make a copy of it until this
1387 strlcpy(mdt_device, status->ls_mdt_device,
1388 sizeof(mdt_device));
1389 rc = llapi_changelog_clear(mdt_device,
1390 status->ls_registration,
1393 printf("Changelog clear (%s, %s, %lld) "
1394 "returned %d\n", status->ls_mdt_device,
1395 status->ls_registration, rec, rc);
1397 if (!rc && !dryrun) {
1398 status->ls_last_recno = rec;
1407 /* Locate a usable version of rsync. At this point we'll use any
1409 int lr_locate_rsync()
1415 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1416 fp = popen(rsync, "r");
1420 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1425 len = strlen(rsync);
1426 if (len > 0 && rsync[len - 1] == '\n')
1427 rsync[len - 1] = '\0';
1430 /* Determine the version of rsync */
1431 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1432 fp = popen(rsync_ver, "r");
1436 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1440 len = strlen(rsync_ver);
1441 if (len > 0 && rsync_ver[len - 1] == '\n')
1442 rsync_ver[len - 1] = '\0';
1449 /* Print the replication parameters */
1450 void lr_print_status(struct lr_info *info)
1457 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1458 printf("MDT device: %s\n", status->ls_mdt_device);
1459 printf("Source: %s\n", status->ls_source);
1460 for (i = 0; i < status->ls_num_targets; i++)
1461 printf("Target: %s\n", status->ls_targets[i]);
1462 if (statuslog != NULL)
1463 printf("Statuslog: %s\n", statuslog);
1464 printf("Changelog registration: %s\n", status->ls_registration);
1465 printf("Starting changelog record: %jd\n",
1466 (uintmax_t)status->ls_last_recno);
1468 printf("Replicate xattrs: no\n");
1470 printf("Clear changelog after use: no\n");
1472 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1475 void lr_print_failure(struct lr_info *info, int rc)
1477 fprintf(stderr, "Replication of operation failed(%d):"
1478 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1479 changelog_type2str(info->type), info->type, info->tfid,
1480 info->pfid, info->name);
1483 /* Replicate filesystem operations from src_path to target_path */
1486 void *changelog_priv;
1487 struct lr_info *info;
1488 struct lr_info *ext = NULL;
1496 info = calloc(1, sizeof(struct lr_info));
1500 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1502 fprintf(stderr, "Source path is not a valid Lustre client "
1506 if (status->ls_mdt_device[0] == '\0')
1507 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1508 status->ls_source_fs, DEFAULT_MDT);
1510 ext = calloc(1, sizeof(struct lr_info));
1516 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1517 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1519 rc = mkdir(info->dest, 0777);
1520 if (rc == -1 && errno != EEXIST) {
1521 fprintf(stderr, "Error writing to target path %s.\n",
1522 status->ls_targets[i]);
1526 rc = llistxattr(info->src, info->xlist, info->xsize);
1527 if (rc == -1 && errno == ENOTSUP) {
1528 fprintf(stderr, "xattrs not supported on %s\n",
1529 status->ls_targets[i]);
1533 if (xattr_not_supp == status->ls_num_targets)
1534 /* None of the targets support xattrs. */
1537 lr_print_status(info);
1539 /* Open changelogs for consumption*/
1540 rc = llapi_changelog_start(&changelog_priv,
1541 CHANGELOG_FLAG_BLOCK | CHANGELOG_FLAG_JOBID,
1542 status->ls_mdt_device, status->ls_last_recno);
1544 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1545 status->ls_source_fs);
1549 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1551 if (info->type == CL_RENAME && !info->is_extended) {
1552 /* Newer rename operations extends changelog to store
1553 * source file information, but old changelog has
1556 if (lr_parse_line(changelog_priv, ext) != 0)
1558 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1559 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1560 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1561 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1562 strlcpy(info->sname, info->name, sizeof(info->sname));
1563 strlcpy(info->name, ext->name, sizeof(info->name));
1564 info->is_extended = 1;
1572 switch(info->type) {
1577 rc = lr_create(info);
1581 rc = lr_remove(info);
1591 rc = lr_setattr(info);
1594 rc = lr_setxattr(info);
1601 /* Nothing needs to be done for these entries */
1606 DEBUG_EXIT(info, rc);
1607 if (rc && rc != -ENOENT) {
1608 lr_print_failure(info, rc);
1613 lr_clear_cl(info, 0);
1615 bzero(info, sizeof(struct lr_info));
1616 bzero(ext, sizeof(struct lr_info));
1620 llapi_changelog_fini(&changelog_priv);
1622 if (errors || verbose)
1623 printf("Errors: %d\n", errors);
1625 /* Clear changelog records used so far */
1626 lr_clear_cl(info, 1);
1629 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1630 printf("Changelog records consumed: %lld\n", rec_count);
1645 termination_handler (int signum)
1647 /* Set a flag for the replicator to gracefully shutdown */
1649 printf("lustre_rsync halting.\n");
1652 int main(int argc, char *argv[])
1658 if ((rc = lr_init_status()) != 0)
1661 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1662 long_opts, NULL)) >= 0) {
1665 /* Assume absolute paths */
1669 /* Assume absolute paths */
1670 strlcpy(status->ls_source, optarg,
1671 sizeof(status->ls_source));
1674 status->ls_num_targets++;
1676 if (numtargets != status->ls_num_targets) {
1677 /* Targets were read from a log
1678 file. The ones specified on the
1679 command line take precedence. The
1680 ones from the log file will be
1682 status->ls_num_targets = numtargets;
1684 newsize = sizeof (struct lustre_rsync_status) +
1685 (status->ls_num_targets * (PATH_MAX + 1));
1686 if (status->ls_size != newsize) {
1687 status->ls_size = newsize;
1688 status = lr_grow_buf(status, newsize);
1692 strlcpy(status->ls_targets[status->ls_num_targets - 1],
1693 optarg, sizeof(status->ls_targets[0]));
1696 strlcpy(status->ls_mdt_device, optarg,
1697 sizeof(status->ls_mdt_device));
1700 strlcpy(status->ls_registration, optarg,
1701 sizeof(status->ls_registration));
1705 (void) lr_read_log();
1711 if (strcmp("no", optarg) == 0) {
1713 } else if (strcmp("yes", optarg) != 0) {
1714 printf("Invalid parameter %s. "
1715 "Specify --xattr=no or --xattr=yes\n",
1724 /* Undocumented option cl-clear */
1725 if (strcmp("no", optarg) == 0) {
1727 } else if (strcmp("yes", optarg) != 0) {
1728 printf("Invalid parameter %s. "
1729 "Specify --cl-clear=no "
1730 "or --cl-clear=yes\n",
1736 /* Undocumented option use-rsync */
1740 /* Undocumented option rsync-threshold */
1741 rsync_threshold = atol(optarg);
1744 /* Undocumented option start-recno */
1745 status->ls_last_recno = atol(optarg);
1748 /* Undocumented option debug */
1749 debug = atoi(optarg);
1750 if (debug < 0 || debug > 2)
1754 /* Undocumented option debug log file */
1755 if (debug_log != NULL)
1757 debug_log = fopen(optarg, "a");
1758 if (debug_log == NULL) {
1759 printf("Cannot open %s for debug log\n",
1765 fprintf(stderr, "error: %s: option '%s' "
1766 "unrecognized.\n", argv[0], argv[optind - 1]);
1772 if (status->ls_last_recno == -1)
1773 status->ls_last_recno = 0;
1774 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1775 /* No registration ID was passed in. */
1776 printf("Please specify changelog consumer registration id.\n");
1780 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1781 fprintf(stderr, "Please specify the source path.\n");
1785 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1786 fprintf(stderr, "Please specify the target path.\n");
1791 /* This plumbing is needed for some of the ioctls behind
1792 llapi calls to work. */
1793 if (obd_initialize(argc, argv) < 0) {
1794 fprintf(stderr, "obd_initialize failed.\n");
1798 rc = lr_locate_rsync();
1799 if (use_rsync && rc != 0) {
1800 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1804 signal(SIGINT, termination_handler);
1805 signal(SIGHUP, termination_handler);
1806 signal(SIGTERM, termination_handler);
1808 rc = lr_replicate();
1810 if (debug_log != NULL)