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.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/lustre_rsync.c
38 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
39 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
43 * - lustre_rsync is a tool for replicating a lustre filesystem.
45 * - The source-fs is a live lustre filesystem. It is not a
46 * snapshot. It is mounted and undergoing changes
48 * - The target-fs is a copy of the source-fs from the past. Let's
49 * call this point, the 'sync point'.
51 * - There is a changelog of all metadata operations that happened on
52 * the filesystem since the 'sync point'.
54 * - lustre_rsync replicates all the operations saved in the changelog
55 * on to the target filesystem to make it identical to the source.
57 * To facilitate replication, the lustre filesystem provides
58 * a) a way to get the current filesystem path of a given FID
59 * b) a way to open files by specifying its FID
61 * The changelog only has a limited amount of information.
62 * tfid - The FID of the target file
63 * pfid - The FID of the parent of the target file (at the time of
65 * sfid - The FID of the source file
66 * spfid - The FID of the parent of the source file
67 * name - The name of the target file (at the time of the operation), the name
68 * of the source file is appended (delimited with '\0') if this
69 * operation involves a source
71 * With just this information, it is not alwasy possible to determine
72 * the file paths for each operation. For instance, if pfid does not
73 * exist on the source-fs (due to a subsequent deletion), its path
74 * cannot be queried. In such cases, lustre_rsync keeps the files in a
75 * special directory ("/.lustrerepl"). Once all the operations in a
76 * changelog are replayed, all the files in this special directory
77 * will get moved to the location as in the source-fs.
79 * Shorthand used: f2p(fid) = fid2path(fid)
81 * The following are the metadata operations of interest.
83 * If tfid is absent on the source-fs, ignore this operation
84 * If pfid is absent on the source-fs [or]
85 * if f2p(pfid) is not present on target-fs [or]
86 * if f2p(pfid)+name != f2p(tfid)
87 * creat .lustrerepl/tfid
88 * track [pfid,tfid,name]
93 * If .lustrerepl/[tfid] is present on the target
94 * rm .lustrerepl/[tfid]
95 * Else if pfid is present on the source-fs,
96 * if f2p(pfid)+name is present,
99 * 3. move (spfid,sname) to (pfid,name)
101 * if spfid is also present, mv (spfid,sname) to (pfid,name)
102 * else mv .lustrerepl/[sfid] to (pfid,name)
103 * Else if pfid is not present,
104 * if spfid is present, mv (spfid,sname) .lustrerepl/[sfid]
105 * If moving out of .lustrerepl
106 * move out all its children in .lustrerepl.
107 * [pfid,tfid,name] tracked from (1) is used for this.
118 #include <sys/stat.h>
119 #include <sys/types.h>
123 #include <sys/xattr.h>
125 #include <libcfs/util/string.h>
126 #include <libcfs/util/parser.h>
127 #include <lustre/lustreapi.h>
128 #include <lustre/lustre_idl.h>
129 #include "lustre_rsync.h"
131 #define REPLICATE_STATUS_VER 1
132 #define CLEAR_INTERVAL 100
133 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
135 #define TYPE_STR_LEN 16
137 #define DEFAULT_MDT "-MDT0000"
138 #define SPECIAL_DIR ".lustrerepl"
139 #define RSYNC "rsync"
146 /* Not used; declared for fulfilling obd.c's dependency. */
147 command_t cmdlist[0];
148 extern int obd_initialize(int argc, char **argv);
150 /* Information for processing a changelog record. This structure is
151 allocated on the heap instead of allocating large variables on the
156 unsigned int is_extended:1;
157 enum changelog_rec_type type;
158 char tfid[LR_FID_STR_LEN];
159 char pfid[LR_FID_STR_LEN];
160 char sfid[LR_FID_STR_LEN];
161 char spfid[LR_FID_STR_LEN];
162 char sname[NAME_MAX + 1];
163 char name[NAME_MAX + 1];
164 char src[PATH_MAX + 1];
165 char dest[PATH_MAX + 1];
166 char path[PATH_MAX + 1];
167 char savedpath[PATH_MAX + 1];
168 char link[PATH_MAX + 1];
169 char linktmp[PATH_MAX + 1];
174 /* Variables for querying the xattributes */
181 struct lr_parent_child_list {
182 struct lr_parent_child_log pc_log;
183 struct lr_parent_child_list *pc_next;
186 struct lustre_rsync_status *status;
187 char *statuslog; /* Name of the status log file */
189 int noxattr; /* Flag to turn off replicating xattrs */
190 int noclear; /* Flag to turn off clearing changelogs */
191 int debug; /* Flag to turn debugging information on and off */
192 int verbose; /* Verbose output */
193 long long rec_count; /* No of changelog records that were processed */
196 int use_rsync; /* Flag to turn on use of rsync to copy data */
197 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
198 int quit; /* Flag to stop processing the changelog; set on the
199 receipt of a signal */
200 int abort_on_err = 0;
202 char rsync[PATH_MAX];
203 char rsync_ver[PATH_MAX];
204 struct lr_parent_child_list *parents;
208 /* Command line options */
209 struct option long_opts[] = {
210 {"source", required_argument, 0, 's'},
211 {"target", required_argument, 0, 't'},
212 {"mdt", required_argument, 0, 'm'},
213 {"user", required_argument, 0, 'u'},
214 {"statuslog", required_argument, 0, 'l'},
215 {"verbose", no_argument, 0, 'v'},
216 {"xattr", required_argument, 0, 'x'},
217 {"dry-run", no_argument, 0, 'z'},
218 /* Undocumented options follow */
219 {"cl-clear", required_argument, 0, 'c'},
220 {"use-rsync", no_argument, 0, 'r'},
221 {"rsync-threshold", required_argument, 0, 'y'},
222 {"start-recno", required_argument, 0, 'n'},
223 {"abort-on-err",no_argument, 0, 'a'},
224 {"debug", required_argument, 0, 'd'},
225 {"debuglog", required_argument, 0, 'D'},
229 /* Command line usage */
232 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
233 "-m <mdt> -r <user id> -l <status log>\n"
234 "lustre_rsync can also pick up parameters from a "
235 "status log created earlier.\n"
236 "\tlustre_rsync -l <log_file>\n"
238 "\t--xattr <yes|no> replicate EAs\n"
239 "\t--abort-on-err abort at first err\n"
241 "\t--dry-run don't write anything\n");
244 #define DEBUG_ENTRY(info) \
245 lr_debug(D_TRACE, "***** Start %lld %s (%d) %s %s %s *****\n", \
246 (info)->recno, changelog_type2str((info)->type), \
247 (info)->type, (info)->tfid, (info)->pfid, (info)->name);
249 #define DEBUG_EXIT(info, rc) \
250 lr_debug(D_TRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n", \
251 (info)->recno, changelog_type2str((info)->type), \
252 (info)->type, (info)->tfid, (info)->pfid, (info)->name, rc);
254 /* Print debug information. This is controlled by the value of the
255 global variable 'debug' */
256 void lr_debug(int level, const char *fmt, ...)
264 if (debug_log != NULL)
265 vfprintf(debug_log, fmt, ap);
267 vfprintf(stdout, fmt, ap);
272 void * lr_grow_buf(void *buf, int size)
276 ptr = realloc(buf, size);
283 /* Use rsync to replicate file data */
284 int lr_rsync_data(struct lr_info *info)
287 struct stat st_src, st_dest;
290 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
292 rc = stat(info->src, &st_src);
294 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
295 info->src, info->name);
301 rc = stat(info->dest, &st_dest);
303 fprintf(stderr, "Error: Unable to stat dest=%s\n",
308 if (st_src.st_mtime != st_dest.st_mtime ||
309 st_src.st_size != st_dest.st_size) {
310 /* XXX spawning off an rsync for every data sync and
311 * waiting synchronously is bad for performance.
312 * librsync could possibly used here. But it does not
313 * seem to be of production grade. Multi-threaded
314 * replication is also to be considered.
317 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
319 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
320 status = system(cmd);
323 } else if (WIFEXITED(status)) {
324 status = WEXITSTATUS(status);
327 else if (status == 23 || status == 24)
328 /* Error due to vanished source files;
334 lr_debug(DINFO, "rsync %s exited with %d %d\n",
335 info->src, status, rc);
340 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
341 info->dest, info->tfid);
347 int lr_copy_data(struct lr_info *info)
357 fd_src = open(info->src, O_RDONLY);
360 if (fstat(fd_src, &st_src) == -1 ||
361 stat(info->dest, &st_dest) == -1)
364 if (st_src.st_mtime == st_dest.st_mtime &&
365 st_src.st_size == st_dest.st_size)
368 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
369 /* It is more efficient to use rsync to replicate
370 large files. Any file larger than rsync_threshold
371 is handed off to rsync. */
372 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
373 rc = lr_rsync_data(info);
377 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
382 bufsize = st_dest.st_blksize;
384 if (info->bufsize < bufsize) {
386 info->buf = lr_grow_buf(info->buf, bufsize);
387 if (info->buf == NULL) {
391 info->bufsize = bufsize;
395 rsize = read(fd_src, info->buf, bufsize);
398 } else if (rsize < 0) {
403 if (write(fd_dest, info->buf, rsize) != rsize) {
421 /* Copy data from source to destination */
422 int lr_sync_data(struct lr_info *info)
425 return lr_rsync_data(info);
427 return lr_copy_data(info);
430 /* Copy all attributes from file src to file dest */
431 int lr_copy_attr(char *src, char *dest)
436 if (stat(src, &st) == -1 ||
437 chmod(dest, st.st_mode) == -1 ||
438 chown(dest, st.st_uid, st.st_gid) == -1)
441 time.actime = st.st_atime;
442 time.modtime = st.st_mtime;
443 if (utime(dest, &time) == -1)
448 /* Copy all xattrs from file info->src to info->dest */
449 int lr_copy_xattr(struct lr_info *info)
451 size_t size = info->xsize;
460 rc = llistxattr(info->src, info->xlist, size);
461 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
462 info->src, info->xlist, rc, errno);
463 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
464 size = rc > PATH_MAX ? rc : PATH_MAX;
465 info->xlist = lr_grow_buf(info->xlist, size);
466 if (info->xlist == NULL)
469 rc = llistxattr(info->src, info->xlist, size);
470 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
471 info->src, rc, errno);
478 while (start < len) {
480 rc = lgetxattr(info->src, info->xlist + start,
482 if (info->xvalue == NULL || errno == ERANGE) {
483 size = rc > PATH_MAX ? rc : PATH_MAX;
484 info->xvalue = lr_grow_buf(info->xvalue, size);
485 if (info->xvalue == NULL)
488 rc = lgetxattr(info->src, info->xlist + start,
491 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
495 rc = lsetxattr(info->dest, info->xlist + start,
496 info->xvalue, size, 0);
497 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
500 if (errno != ENOTSUP) {
501 fprintf(stderr, "Error replicating "
502 " xattr for %s: %d\n",
509 start += strlen(info->xlist + start) + 1;
512 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
517 /* Retrieve the filesystem path for a given FID and a given
518 linkno. The path is returned in info->path */
519 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
521 long long recno = -1;
524 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
525 PATH_MAX, &recno, &linkno);
526 if (rc < 0 && rc != -ENOENT) {
527 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
528 status->ls_source, fidstr, -rc, strerror(errno = -rc));
534 /* Retrieve the filesystem path for a given FID. The path is returned
536 int lr_get_path(struct lr_info *info, char *fidstr)
538 return lr_get_path_ln(info, fidstr, 0);
541 /* Generate the path for opening by FID */
542 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
544 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
545 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
550 /* Read the symlink information into 'info->link' */
551 int lr_get_symlink(struct lr_info *info)
556 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
557 rc = readlink(info->src, info->linktmp, PATH_MAX);
559 info->linktmp[rc] = '\0';
562 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
564 if (strncmp(info->linktmp, status->ls_source,
565 strlen(status->ls_source)) == 0) {
566 /* Strip source fs path and replace with target fs path. */
567 link = info->linktmp + strlen(status->ls_source);
568 snprintf(info->src, PATH_MAX, "%s%s",
569 status->ls_targets[info->target_no], link);
572 link = info->linktmp;
574 strlcpy(info->link, link, sizeof(info->link));
579 /* Create file/directory/device file/symlink. */
580 int lr_mkfile(struct lr_info *info)
586 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
587 if (info->type == CL_MKDIR) {
588 rc = mkdir(info->dest, 0777);
589 } else if (info->type == CL_SOFTLINK) {
590 lr_get_symlink(info);
591 rc = symlink(info->link, info->dest);
592 } else if (info->type == CL_MKNOD) {
593 lr_get_FID_PATH(status->ls_source, info->tfid,
594 info->src, PATH_MAX);
595 rc = stat(info->src, &st);
602 rc = mknod(info->dest, st.st_mode, st.st_rdev);
604 rc = mknod(info->dest, S_IFREG | 0777, 0);
609 /* Sync data and attributes */
610 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
611 lr_debug(DTRACE, "Syncing data and attributes %s\n",
613 (void) lr_copy_xattr(info);
614 if (info->type == CL_CREATE)
615 rc = lr_sync_data(info);
617 rc = lr_copy_attr(info->src, info->dest);
620 /* Source file has disappeared. Not an error. */
623 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
630 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
632 struct lr_parent_child_list *p;
635 p = calloc(1, sizeof(*p));
638 len = strlcpy(p->pc_log.pcl_pfid, pfid, sizeof(p->pc_log.pcl_pfid));
639 if (len >= sizeof(p->pc_log.pcl_pfid))
641 len = strlcpy(p->pc_log.pcl_tfid, tfid, sizeof(p->pc_log.pcl_tfid));
642 if (len >= sizeof(p->pc_log.pcl_tfid))
644 len = strlcpy(p->pc_log.pcl_name, name, sizeof(p->pc_log.pcl_name));
645 if (len >= sizeof(p->pc_log.pcl_name))
648 p->pc_next = parents;
657 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
659 struct lr_parent_child_list *curr, *prev;
663 d = calloc(1, PATH_MAX + 1);
664 prev = curr = parents;
666 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
667 snprintf(d, PATH_MAX, "%s/%s", dest,
668 curr->pc_log.pcl_name);
669 snprintf(info->src, PATH_MAX, "%s/%s/%s",
670 status->ls_targets[info->target_no],
671 SPECIAL_DIR, curr->pc_log.pcl_tfid);
672 rc = rename(info->src, d);
674 fprintf(stderr, "Error renaming file "
676 info->src, d, errno);
680 parents = curr->pc_next;
682 prev->pc_next = curr->pc_next;
683 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
685 prev = curr = parents;
689 curr = curr->pc_next;
696 /* remove [info->spfid, info->sfid] from parents */
697 int lr_remove_pc(const char *pfid, const char *tfid)
699 struct lr_parent_child_list *curr, *prev;
701 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
702 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
703 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
705 parents = curr->pc_next;
707 prev->pc_next = curr->pc_next;
715 /* Create file under SPECIAL_DIR with its tfid as its name. */
716 int lr_mk_special(struct lr_info *info)
720 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
721 status->ls_targets[info->target_no], SPECIAL_DIR,
724 rc = lr_mkfile(info);
728 rc = lr_add_pc(info->pfid, info->tfid, info->name);
732 /* Remove a file or directory */
733 int lr_rmfile(struct lr_info *info)
737 if (info->type == CL_RMDIR)
738 rc = rmdir(info->dest);
740 rc = unlink(info->dest);
746 /* Recursively remove directory and its contents */
747 int lr_rm_recursive(struct lr_info *info)
751 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
752 rc = system(info->cmd);
759 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
760 int lr_rm_special(struct lr_info *info)
764 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
765 status->ls_targets[info->target_no], SPECIAL_DIR,
767 rc = lr_rmfile(info);
770 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
771 info->dest, rc, errno);
775 /* Replicate file and directory create events */
776 int lr_create(struct lr_info *info)
783 /* Is target FID present on the source? */
784 rc = lr_get_path(info, info->tfid);
786 /* Source file has disappeared. Not an error. */
787 lr_debug(DINFO, "create: tfid %s not found on"
788 "source-fs\n", info->tfid);
793 strcpy(info->savedpath, info->path);
795 /* Is parent FID present on the source */
796 rc = lr_get_path(info, info->pfid);
798 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
805 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
806 len = strlen(info->path);
807 if (len == 1 && info->path[0] == '/')
808 snprintf(info->dest, PATH_MAX, "%s", info->name);
809 else if (len - 1 > 0 && info->path[len - 1] == '/')
810 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
812 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
814 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
816 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
817 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
818 info->tfid, info->dest, info->savedpath);
822 /* Is f2p(pfid) present on the target? If not, the parent has
825 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
827 if (access(info->dest, F_OK) != 0) {
828 lr_debug(DTRACE, "create: parent %s not found\n",
833 for (info->target_no = 0; info->target_no < status->ls_num_targets;
835 snprintf(info->dest, PATH_MAX, "%s/%s",
836 status->ls_targets[info->target_no], info->savedpath);
837 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
841 rc1 = lr_mkfile(info);
842 if (mkspecial || rc1 == -ENOENT) {
843 rc1 = lr_mk_special(info);
851 /* Replicate a file remove (rmdir/unlink) operation */
852 int lr_remove(struct lr_info *info)
857 for (info->target_no = 0; info->target_no < status->ls_num_targets;
860 rc1 = lr_rm_special(info);
864 rc1 = lr_get_path(info, info->pfid);
865 if (rc1 == -ENOENT) {
866 lr_debug(DINFO, "remove: pfid %s not found\n",
874 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
875 status->ls_targets[info->target_no], info->path,
878 rc1 = lr_rmfile(info);
879 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
880 info->dest, rc1, errno);
881 if (rc1 == -ENOTEMPTY)
882 rc1 = lr_rm_recursive(info);
892 /* Replicate a rename/move operation. */
893 int lr_move(struct lr_info *info)
899 int special_dest = 0;
900 char srcpath[PATH_MAX + 1] = "";
902 LASSERT(info->is_extended);
904 rc_src = lr_get_path(info, info->spfid);
905 if (rc_src < 0 && rc_src != -ENOENT)
907 memcpy(srcpath, info->path, strlen(info->path));
909 rc_dest = lr_get_path(info, info->pfid);
910 if (rc_dest < 0 && rc_dest != -ENOENT)
913 for (info->target_no = 0; info->target_no < status->ls_num_targets;
917 snprintf(info->dest, PATH_MAX, "%s/%s",
918 status->ls_targets[info->target_no],
920 if (access(info->dest, F_OK) != 0) {
923 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
924 status->ls_targets[info->target_no],
925 info->path, info->name);
927 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
930 if (rc_dest == -ENOENT) {
931 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
932 status->ls_targets[info->target_no],
933 SPECIAL_DIR, info->sfid);
935 lr_debug(DINFO, "special dest %s\n", info->dest);
939 snprintf(info->src, PATH_MAX, "%s/%s/%s",
940 status->ls_targets[info->target_no],
941 srcpath, info->sname);
942 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
945 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
947 snprintf(info->src, PATH_MAX, "%s/%s/%s",
948 status->ls_targets[info->target_no],
949 SPECIAL_DIR, info->sfid);
951 lr_debug(DINFO, "special src %s\n", info->src);
956 if (strcmp(info->src, info->dest) != 0) {
957 rc1 = rename(info->src, info->dest);
960 lr_debug(DINFO, "rename returns %d\n", rc1);
964 rc1 = lr_remove_pc(info->spfid, info->sfid);
967 lr_cascade_move(info->sfid, info->dest, info);
969 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
971 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
972 info->src, info->dest, rc1, errno);
979 /* Replicate a hard link */
980 int lr_link(struct lr_info *info)
988 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
989 rc = stat(info->src, &st);
993 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1000 /* Search through the hardlinks to get the src and dest */
1001 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
1002 info->dest[0] == 0); i++) {
1003 rc1 = lr_get_path_ln(info, info->tfid, i);
1004 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
1005 info->path, info->name, i, rc1);
1009 len = strlen(info->path) - strlen(info->name);
1010 if (len >= 0 && strcmp(info->path + len,
1012 snprintf(info->dest, PATH_MAX, "%s/%s",
1013 status->ls_targets[info->target_no],
1015 else if (info->src[0] == 0)
1016 snprintf(info->src, PATH_MAX, "%s/%s",
1017 status->ls_targets[info->target_no],
1026 if (info->src[0] == 0 || info->dest[0] == 0)
1027 /* Could not find the source or destination.
1028 This can happen when some links don't exist
1032 if (info->src[0] == 0)
1033 snprintf(info->src, PATH_MAX, "%s/%s/%s",
1034 status->ls_targets[info->target_no],
1035 SPECIAL_DIR, info->tfid);
1036 else if (info->dest[0] == 0)
1037 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1038 status->ls_targets[info->target_no],
1039 SPECIAL_DIR, info->tfid);
1041 rc1 = link(info->src, info->dest);
1042 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
1043 info->src, info->dest, rc1, strerror(errno));
1051 /* Replicate file attributes */
1052 int lr_setattr(struct lr_info *info)
1057 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1059 rc = lr_get_path(info, info->tfid);
1061 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1066 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1067 info->target_no++) {
1069 snprintf(info->dest, PATH_MAX, "%s/%s",
1070 status->ls_targets[info->target_no], info->path);
1071 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1074 rc1 = lr_sync_data(info);
1076 rc1 = lr_copy_attr(info->src, info->dest);
1083 /* Replicate xattrs */
1084 int lr_setxattr(struct lr_info *info)
1088 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1090 rc = lr_get_path(info, info->tfid);
1092 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1097 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1098 info->target_no++) {
1100 snprintf(info->dest, PATH_MAX, "%s/%s",
1101 status->ls_targets[info->target_no], info->path);
1102 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1105 rc1 = lr_copy_xattr(info);
1113 /* Parse a line of changelog entry */
1114 int lr_parse_line(void *priv, struct lr_info *info)
1116 struct changelog_rec *rec;
1117 struct changelog_ext_rename *rnm;
1119 size_t copylen = sizeof(info->name);
1121 if (llapi_changelog_recv(priv, &rec) != 0)
1124 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1125 info->recno = rec->cr_index;
1126 info->type = rec->cr_type;
1127 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1128 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1130 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1131 if (copylen > namelen + 1)
1132 copylen = namelen + 1;
1133 strlcpy(info->name, changelog_rec_name(rec), copylen);
1135 /* Don't use rnm if CLF_RENAME isn't set */
1136 rnm = changelog_rec_rename(rec);
1137 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1138 copylen = sizeof(info->sname);
1140 snprintf(info->sfid, sizeof(info->sfid), DFID,
1141 PFID(&rnm->cr_sfid));
1142 snprintf(info->spfid, sizeof(info->spfid), DFID,
1143 PFID(&rnm->cr_spfid));
1144 namelen = changelog_rec_snamelen(rec);
1145 if (copylen > namelen + 1)
1146 copylen = namelen + 1;
1147 strlcpy(info->sname, changelog_rec_sname(rec), copylen);
1150 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1151 info->name, info->sname);
1154 printf("Rec %lld: %d %s\n", info->recno, info->type,
1158 llapi_changelog_free(&rec);
1164 /* Initialize the replication parameters */
1165 int lr_init_status()
1167 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1171 status = calloc(size, 1);
1174 status->ls_version = REPLICATE_STATUS_VER;
1175 status->ls_size = size;
1176 status->ls_last_recno = -1;
1180 /* Make a backup of the statuslog */
1181 void lr_backup_log()
1183 char backupfile[PATH_MAX];
1187 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1188 (void) rename(statuslog, backupfile);
1194 /* Save replication parameters to a statuslog. */
1199 size_t write_size = status->ls_size;
1200 struct lr_parent_child_list *curr;
1203 if (statuslog == NULL)
1208 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1211 fprintf(stderr, "Error opening log file for writing (%s)\n",
1216 size = write(fd, status, write_size);
1217 if (size != write_size) {
1218 fprintf(stderr, "Error writing to log file (%s) %d\n",
1224 for (curr = parents; curr; curr = curr->pc_next) {
1225 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1226 if (size != sizeof(curr->pc_log)) {
1227 fprintf(stderr, "Error writing to log file (%s) %d\n",
1237 /* Read statuslog and populate the replication parameters. Command
1238 * line parameters take precedence over parameters in the log file.*/
1241 struct lr_parent_child_list *tmp;
1242 struct lr_parent_child_log rec;
1243 struct lustre_rsync_status *s;
1246 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1249 if (statuslog == NULL)
1252 s = calloc(1, read_size);
1258 fd = open(statuslog, O_RDONLY);
1264 size = read(fd, s, read_size);
1265 if (size != read_size) {
1270 if (read_size < s->ls_size) {
1271 read_size = s->ls_size;
1272 s = lr_grow_buf(s, read_size);
1278 if (lseek(fd, 0, SEEK_SET) == -1) {
1283 size = read(fd, s, read_size);
1284 if (size != read_size) {
1290 while (read(fd, &rec, sizeof(rec)) != 0) {
1291 tmp = calloc(1, sizeof(*tmp));
1298 tmp->pc_next = parents;
1302 /* copy uninitialized fields to status */
1303 if (status->ls_num_targets == 0) {
1304 if (status->ls_size != s->ls_size) {
1305 status = lr_grow_buf(status, s->ls_size);
1306 if (status == NULL) {
1311 status->ls_size = s->ls_size;
1313 status->ls_num_targets = s->ls_num_targets;
1314 memcpy(status->ls_targets, s->ls_targets,
1315 (PATH_MAX + 1) * s->ls_num_targets);
1317 if (status->ls_last_recno == -1)
1318 status->ls_last_recno = s->ls_last_recno;
1320 if (status->ls_registration[0] == '\0')
1321 strlcpy(status->ls_registration, s->ls_registration,
1322 sizeof(status->ls_registration));
1324 if (status->ls_mdt_device[0] == '\0')
1325 strlcpy(status->ls_mdt_device, s->ls_mdt_device,
1326 sizeof(status->ls_mdt_device));
1328 if (status->ls_source_fs[0] == '\0')
1329 strlcpy(status->ls_source_fs, s->ls_source_fs,
1330 sizeof(status->ls_source_fs));
1332 if (status->ls_source[0] == '\0')
1333 strlcpy(status->ls_source, s->ls_source,
1334 sizeof(status->ls_source));
1344 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1346 int lr_clear_cl(struct lr_info *info, int force)
1348 char mdt_device[LR_NAME_MAXLEN + 1];
1352 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1353 if (info->type == CL_RENAME)
1354 rec = info->recno + 1;
1357 if (!noclear && !dryrun) {
1358 /* llapi_changelog_clear modifies the mdt
1359 * device name so make a copy of it until this
1362 strlcpy(mdt_device, status->ls_mdt_device,
1363 sizeof(mdt_device));
1364 rc = llapi_changelog_clear(mdt_device,
1365 status->ls_registration,
1368 printf("Changelog clear (%s, %s, %lld) "
1369 "returned %d\n", status->ls_mdt_device,
1370 status->ls_registration, rec, rc);
1372 if (!rc && !dryrun) {
1373 status->ls_last_recno = rec;
1382 /* Locate a usable version of rsync. At this point we'll use any
1384 int lr_locate_rsync()
1390 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1391 fp = popen(rsync, "r");
1395 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1400 len = strlen(rsync);
1401 if (len > 0 && rsync[len - 1] == '\n')
1402 rsync[len - 1] = '\0';
1405 /* Determine the version of rsync */
1406 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1407 fp = popen(rsync_ver, "r");
1411 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1415 len = strlen(rsync_ver);
1416 if (len > 0 && rsync_ver[len - 1] == '\n')
1417 rsync_ver[len - 1] = '\0';
1424 /* Print the replication parameters */
1425 void lr_print_status(struct lr_info *info)
1432 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1433 printf("MDT device: %s\n", status->ls_mdt_device);
1434 printf("Source: %s\n", status->ls_source);
1435 for (i = 0; i < status->ls_num_targets; i++)
1436 printf("Target: %s\n", status->ls_targets[i]);
1437 if (statuslog != NULL)
1438 printf("Statuslog: %s\n", statuslog);
1439 printf("Changelog registration: %s\n", status->ls_registration);
1440 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1442 printf("Replicate xattrs: no\n");
1444 printf("Clear changelog after use: no\n");
1446 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1449 void lr_print_failure(struct lr_info *info, int rc)
1451 fprintf(stderr, "Replication of operation failed(%d):"
1452 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1453 changelog_type2str(info->type), info->type, info->tfid,
1454 info->pfid, info->name);
1457 /* Replicate filesystem operations from src_path to target_path */
1460 void *changelog_priv;
1461 struct lr_info *info;
1462 struct lr_info *ext = NULL;
1470 info = calloc(1, sizeof(struct lr_info));
1474 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1476 fprintf(stderr, "Source path is not a valid Lustre client "
1480 if (status->ls_mdt_device[0] == '\0')
1481 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1482 status->ls_source_fs, DEFAULT_MDT);
1484 ext = calloc(1, sizeof(struct lr_info));
1490 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1491 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1493 rc = mkdir(info->dest, 0777);
1494 if (rc == -1 && errno != EEXIST) {
1495 fprintf(stderr, "Error writing to target path %s.\n",
1496 status->ls_targets[i]);
1500 rc = llistxattr(info->src, info->xlist, info->xsize);
1501 if (rc == -1 && errno == ENOTSUP) {
1502 fprintf(stderr, "xattrs not supported on %s\n",
1503 status->ls_targets[i]);
1507 if (xattr_not_supp == status->ls_num_targets)
1508 /* None of the targets support xattrs. */
1511 lr_print_status(info);
1513 /* Open changelogs for consumption*/
1514 rc = llapi_changelog_start(&changelog_priv,
1515 CHANGELOG_FLAG_BLOCK | CHANGELOG_FLAG_JOBID,
1516 status->ls_source_fs, status->ls_last_recno);
1518 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1519 status->ls_source_fs);
1523 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1525 if (info->type == CL_RENAME && !info->is_extended) {
1526 /* Newer rename operations extends changelog to store
1527 * source file information, but old changelog has
1530 if (lr_parse_line(changelog_priv, ext) != 0)
1532 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1533 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1534 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1535 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1536 strlcpy(info->sname, info->name, sizeof(info->sname));
1537 strlcpy(info->name, ext->name, sizeof(info->name));
1538 info->is_extended = 1;
1546 switch(info->type) {
1551 rc = lr_create(info);
1555 rc = lr_remove(info);
1565 rc = lr_setattr(info);
1568 rc = lr_setxattr(info);
1575 /* Nothing needs to be done for these entries */
1580 DEBUG_EXIT(info, rc);
1581 if (rc && rc != -ENOENT) {
1582 lr_print_failure(info, rc);
1587 lr_clear_cl(info, 0);
1589 bzero(info, sizeof(struct lr_info));
1590 bzero(ext, sizeof(struct lr_info));
1594 llapi_changelog_fini(&changelog_priv);
1596 if (errors || verbose)
1597 printf("Errors: %d\n", errors);
1599 /* Clear changelog records used so far */
1600 lr_clear_cl(info, 1);
1603 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1604 printf("Changelog records consumed: %lld\n", rec_count);
1619 termination_handler (int signum)
1621 /* Set a flag for the replicator to gracefully shutdown */
1623 printf("lustre_rsync halting.\n");
1626 int main(int argc, char *argv[])
1632 if ((rc = lr_init_status()) != 0)
1635 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1636 long_opts, NULL)) >= 0) {
1639 /* Assume absolute paths */
1643 /* Assume absolute paths */
1644 strlcpy(status->ls_source, optarg,
1645 sizeof(status->ls_source));
1648 status->ls_num_targets++;
1650 if (numtargets != status->ls_num_targets) {
1651 /* Targets were read from a log
1652 file. The ones specified on the
1653 command line take precedence. The
1654 ones from the log file will be
1656 status->ls_num_targets = numtargets;
1658 newsize = sizeof (struct lustre_rsync_status) +
1659 (status->ls_num_targets * (PATH_MAX + 1));
1660 if (status->ls_size != newsize) {
1661 status->ls_size = newsize;
1662 status = lr_grow_buf(status, newsize);
1666 strlcpy(status->ls_targets[status->ls_num_targets - 1],
1667 optarg, sizeof(status->ls_targets[0]));
1670 strlcpy(status->ls_mdt_device, optarg,
1671 sizeof(status->ls_mdt_device));
1674 strlcpy(status->ls_registration, optarg,
1675 sizeof(status->ls_registration));
1679 (void) lr_read_log();
1685 if (strcmp("no", optarg) == 0) {
1687 } else if (strcmp("yes", optarg) != 0) {
1688 printf("Invalid parameter %s. "
1689 "Specify --xattr=no or --xattr=yes\n",
1698 /* Undocumented option cl-clear */
1699 if (strcmp("no", optarg) == 0) {
1701 } else if (strcmp("yes", optarg) != 0) {
1702 printf("Invalid parameter %s. "
1703 "Specify --cl-clear=no "
1704 "or --cl-clear=yes\n",
1710 /* Undocumented option use-rsync */
1714 /* Undocumented option rsync-threshold */
1715 rsync_threshold = atol(optarg);
1718 /* Undocumented option start-recno */
1719 status->ls_last_recno = atol(optarg);
1722 /* Undocumented option debug */
1723 debug = atoi(optarg);
1724 if (debug < 0 || debug > 2)
1728 /* Undocumented option debug log file */
1729 if (debug_log != NULL)
1731 debug_log = fopen(optarg, "a");
1732 if (debug_log == NULL) {
1733 printf("Cannot open %s for debug log\n",
1739 fprintf(stderr, "error: %s: option '%s' "
1740 "unrecognized.\n", argv[0], argv[optind - 1]);
1746 if (status->ls_last_recno == -1)
1747 status->ls_last_recno = 0;
1748 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1749 /* No registration ID was passed in. */
1750 printf("Please specify changelog consumer registration id.\n");
1754 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1755 fprintf(stderr, "Please specify the source path.\n");
1759 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1760 fprintf(stderr, "Please specify the target path.\n");
1765 /* This plumbing is needed for some of the ioctls behind
1766 llapi calls to work. */
1767 if (obd_initialize(argc, argv) < 0) {
1768 fprintf(stderr, "obd_initialize failed.\n");
1772 rc = lr_locate_rsync();
1773 if (use_rsync && rc != 0) {
1774 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1778 signal(SIGINT, termination_handler);
1779 signal(SIGHUP, termination_handler);
1780 signal(SIGTERM, termination_handler);
1782 rc = lr_replicate();
1784 if (debug_log != NULL)