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, 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.
117 #include <sys/stat.h>
118 #include <sys/types.h>
122 #include <sys/xattr.h>
124 #include <libcfs/libcfsutil.h>
125 #include <lustre/lustreapi.h>
126 #include <lustre/lustre_idl.h>
127 #include "lustre_rsync.h"
129 #define REPLICATE_STATUS_VER 1
130 #define CLEAR_INTERVAL 100
131 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
133 #define TYPE_STR_LEN 16
135 #define DEFAULT_MDT "-MDT0000"
136 #define SPECIAL_DIR ".lustrerepl"
137 #define RSYNC "rsync"
144 /* Not used; declared for fulfilling obd.c's dependency. */
145 command_t cmdlist[0];
146 extern int obd_initialize(int argc, char **argv);
148 /* Information for processing a changelog record. This structure is
149 allocated on the heap instead of allocating large variables on the
154 unsigned int is_extended:1;
155 enum changelog_rec_type type;
156 char tfid[LR_FID_STR_LEN];
157 char pfid[LR_FID_STR_LEN];
158 char sfid[LR_FID_STR_LEN];
159 char spfid[LR_FID_STR_LEN];
160 char sname[NAME_MAX + 1];
161 char name[NAME_MAX + 1];
162 char src[PATH_MAX + 1];
163 char dest[PATH_MAX + 1];
164 char path[PATH_MAX + 1];
165 char savedpath[PATH_MAX + 1];
166 char link[PATH_MAX + 1];
167 char linktmp[PATH_MAX + 1];
172 /* Variables for querying the xattributes */
179 struct lr_parent_child_list {
180 struct lr_parent_child_log pc_log;
181 struct lr_parent_child_list *pc_next;
184 struct lustre_rsync_status *status;
185 char *statuslog; /* Name of the status log file */
187 int noxattr; /* Flag to turn off replicating xattrs */
188 int noclear; /* Flag to turn off clearing changelogs */
189 int debug; /* Flag to turn debugging information on and off */
190 int verbose; /* Verbose output */
191 long long rec_count; /* No of changelog records that were processed */
194 int use_rsync; /* Flag to turn on use of rsync to copy data */
195 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
196 int quit; /* Flag to stop processing the changelog; set on the
197 receipt of a signal */
198 int abort_on_err = 0;
200 char rsync[PATH_MAX];
201 char rsync_ver[PATH_MAX];
202 struct lr_parent_child_list *parents;
206 /* Command line options */
207 struct option long_opts[] = {
208 {"source", required_argument, 0, 's'},
209 {"target", required_argument, 0, 't'},
210 {"mdt", required_argument, 0, 'm'},
211 {"user", required_argument, 0, 'u'},
212 {"statuslog", required_argument, 0, 'l'},
213 {"verbose", no_argument, 0, 'v'},
214 {"xattr", required_argument, 0, 'x'},
215 {"dry-run", no_argument, 0, 'z'},
216 /* Undocumented options follow */
217 {"cl-clear", required_argument, 0, 'c'},
218 {"use-rsync", no_argument, 0, 'r'},
219 {"rsync-threshold", required_argument, 0, 'y'},
220 {"start-recno", required_argument, 0, 'n'},
221 {"abort-on-err",no_argument, 0, 'a'},
222 {"debug", required_argument, 0, 'd'},
223 {"debuglog", required_argument, 0, 'D'},
227 /* Command line usage */
230 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
231 "-m <mdt> -r <user id> -l <status log>\n"
232 "lustre_rsync can also pick up parameters from a "
233 "status log created earlier.\n"
234 "\tlustre_rsync -l <log_file>\n"
236 "\t--xattr <yes|no> replicate EAs\n"
237 "\t--abort-on-err abort at first err\n"
239 "\t--dry-run don't write anything\n");
242 #define DEBUG_ENTRY(info) \
243 lr_debug(D_TRACE, "***** Start %lld %s (%d) %s %s %s *****\n", \
244 (info)->recno, changelog_type2str((info)->type), \
245 (info)->type, (info)->tfid, (info)->pfid, (info)->name);
247 #define DEBUG_EXIT(info, rc) \
248 lr_debug(D_TRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n", \
249 (info)->recno, changelog_type2str((info)->type), \
250 (info)->type, (info)->tfid, (info)->pfid, (info)->name, rc);
252 /* Print debug information. This is controlled by the value of the
253 global variable 'debug' */
254 void lr_debug(int level, const char *fmt, ...)
262 if (debug_log != NULL)
263 vfprintf(debug_log, fmt, ap);
265 vfprintf(stdout, fmt, ap);
270 void * lr_grow_buf(void *buf, int size)
274 ptr = realloc(buf, size);
281 /* Use rsync to replicate file data */
282 int lr_rsync_data(struct lr_info *info)
285 struct stat st_src, st_dest;
288 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
290 rc = stat(info->src, &st_src);
292 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
293 info->src, info->name);
299 rc = stat(info->dest, &st_dest);
301 fprintf(stderr, "Error: Unable to stat dest=%s\n",
306 if (st_src.st_mtime != st_dest.st_mtime ||
307 st_src.st_size != st_dest.st_size) {
308 /* XXX spawning off an rsync for every data sync and
309 * waiting synchronously is bad for performance.
310 * librsync could possibly used here. But it does not
311 * seem to be of production grade. Multi-threaded
312 * replication is also to be considered.
315 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
317 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
318 status = system(cmd);
321 } else if (WIFEXITED(status)) {
322 status = WEXITSTATUS(status);
325 else if (status == 23 || status == 24)
326 /* Error due to vanished source files;
332 lr_debug(DINFO, "rsync %s exited with %d %d\n",
333 info->src, status, rc);
338 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
339 info->dest, info->tfid);
345 int lr_copy_data(struct lr_info *info)
355 fd_src = open(info->src, O_RDONLY);
358 if (fstat(fd_src, &st_src) == -1 ||
359 stat(info->dest, &st_dest) == -1)
362 if (st_src.st_mtime == st_dest.st_mtime &&
363 st_src.st_size == st_dest.st_size)
366 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
367 /* It is more efficient to use rsync to replicate
368 large files. Any file larger than rsync_threshold
369 is handed off to rsync. */
370 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
371 rc = lr_rsync_data(info);
375 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
380 bufsize = st_dest.st_blksize;
382 if (info->bufsize < bufsize) {
384 info->buf = lr_grow_buf(info->buf, bufsize);
385 if (info->buf == NULL) {
389 info->bufsize = bufsize;
393 rsize = read(fd_src, info->buf, bufsize);
396 } else if (rsize < 0) {
401 if (write(fd_dest, info->buf, rsize) != rsize) {
419 /* Copy data from source to destination */
420 int lr_sync_data(struct lr_info *info)
423 return lr_rsync_data(info);
425 return lr_copy_data(info);
428 /* Copy all attributes from file src to file dest */
429 int lr_copy_attr(char *src, char *dest)
434 if (stat(src, &st) == -1 ||
435 chmod(dest, st.st_mode) == -1 ||
436 chown(dest, st.st_uid, st.st_gid) == -1)
439 time.actime = st.st_atime;
440 time.modtime = st.st_mtime;
441 if (utime(dest, &time) == -1)
446 /* Copy all xattrs from file info->src to info->dest */
447 int lr_copy_xattr(struct lr_info *info)
449 size_t size = info->xsize;
458 rc = llistxattr(info->src, info->xlist, size);
459 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
460 info->src, info->xlist, rc, errno);
461 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
462 size = rc > PATH_MAX ? rc : PATH_MAX;
463 info->xlist = lr_grow_buf(info->xlist, size);
464 if (info->xlist == NULL)
467 rc = llistxattr(info->src, info->xlist, size);
468 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
469 info->src, rc, errno);
476 while (start < len) {
478 rc = lgetxattr(info->src, info->xlist + start,
480 if (info->xvalue == NULL || errno == ERANGE) {
481 size = rc > PATH_MAX ? rc : PATH_MAX;
482 info->xvalue = lr_grow_buf(info->xvalue, size);
483 if (info->xvalue == NULL)
486 rc = lgetxattr(info->src, info->xlist + start,
489 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
493 rc = lsetxattr(info->dest, info->xlist + start,
494 info->xvalue, size, 0);
495 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
498 if (errno != ENOTSUP) {
499 fprintf(stderr, "Error replicating "
500 " xattr for %s: %d\n",
507 start += strlen(info->xlist + start) + 1;
510 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
515 /* Retrieve the filesystem path for a given FID and a given
516 linkno. The path is returned in info->path */
517 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
519 long long recno = -1;
522 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
523 PATH_MAX, &recno, &linkno);
524 if (rc < 0 && rc != -ENOENT) {
525 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
526 status->ls_source, fidstr, -rc, strerror(errno = -rc));
532 /* Retrieve the filesystem path for a given FID. The path is returned
534 int lr_get_path(struct lr_info *info, char *fidstr)
536 return lr_get_path_ln(info, fidstr, 0);
539 /* Generate the path for opening by FID */
540 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
542 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
543 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
548 /* Read the symlink information into 'info->link' */
549 int lr_get_symlink(struct lr_info *info)
554 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
555 rc = readlink(info->src, info->linktmp, PATH_MAX);
557 info->linktmp[rc] = '\0';
560 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
562 if (strncmp(info->linktmp, status->ls_source,
563 strlen(status->ls_source)) == 0) {
564 /* Strip source fs path and replace with target fs path. */
565 link = info->linktmp + strlen(status->ls_source);
566 snprintf(info->src, PATH_MAX, "%s%s",
567 status->ls_targets[info->target_no], link);
570 link = info->linktmp;
572 strncpy(info->link, link, PATH_MAX);
573 info->link[PATH_MAX] = '\0';
578 /* Create file/directory/device file/symlink. */
579 int lr_mkfile(struct lr_info *info)
585 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
586 if (info->type == CL_MKDIR) {
587 rc = mkdir(info->dest, 0777);
588 } else if (info->type == CL_SOFTLINK) {
589 lr_get_symlink(info);
590 rc = symlink(info->link, info->dest);
591 } else if (info->type == CL_MKNOD) {
592 lr_get_FID_PATH(status->ls_source, info->tfid,
593 info->src, PATH_MAX);
594 rc = stat(info->src, &st);
601 rc = mknod(info->dest, st.st_mode, st.st_rdev);
603 rc = mknod(info->dest, S_IFREG | 0777, 0);
608 /* Sync data and attributes */
609 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
610 lr_debug(DTRACE, "Syncing data and attributes %s\n",
612 (void) lr_copy_xattr(info);
613 if (info->type == CL_CREATE)
614 rc = lr_sync_data(info);
616 rc = lr_copy_attr(info->src, info->dest);
619 /* Source file has disappeared. Not an error. */
622 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
629 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
631 struct lr_parent_child_list *p;
633 p = calloc(1, sizeof(*p));
636 if (strlen(pfid) > sizeof(p->pc_log.pcl_pfid)-1)
638 strncpy(p->pc_log.pcl_pfid, pfid, sizeof(p->pc_log.pcl_pfid));
639 if (strlen(tfid) > sizeof(p->pc_log.pcl_tfid)-1)
641 strncpy(p->pc_log.pcl_tfid, tfid, sizeof(p->pc_log.pcl_tfid));
642 if (strlen(name) > sizeof(p->pc_log.pcl_name)-1)
644 strncpy(p->pc_log.pcl_name, name, sizeof(p->pc_log.pcl_name));
646 p->pc_next = parents;
655 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
657 struct lr_parent_child_list *curr, *prev;
661 d = calloc(1, PATH_MAX + 1);
662 prev = curr = parents;
664 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
665 snprintf(d, PATH_MAX, "%s/%s", dest,
666 curr->pc_log.pcl_name);
667 snprintf(info->src, PATH_MAX, "%s/%s/%s",
668 status->ls_targets[info->target_no],
669 SPECIAL_DIR, curr->pc_log.pcl_tfid);
670 rc = rename(info->src, d);
672 fprintf(stderr, "Error renaming file "
674 info->src, d, errno);
677 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
679 parents = curr->pc_next;
681 prev->pc_next = curr->pc_next;
683 prev = curr = parents;
687 curr = curr->pc_next;
694 /* remove [info->spfid, info->sfid] from parents */
695 int lr_remove_pc(const char *pfid, const char *tfid)
697 struct lr_parent_child_list *curr, *prev;
699 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
700 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
701 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
703 parents = curr->pc_next;
705 prev->pc_next = curr->pc_next;
713 /* Create file under SPECIAL_DIR with its tfid as its name. */
714 int lr_mk_special(struct lr_info *info)
718 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
719 status->ls_targets[info->target_no], SPECIAL_DIR,
722 rc = lr_mkfile(info);
726 rc = lr_add_pc(info->pfid, info->tfid, info->name);
730 /* Remove a file or directory */
731 int lr_rmfile(struct lr_info *info)
735 if (info->type == CL_RMDIR)
736 rc = rmdir(info->dest);
738 rc = unlink(info->dest);
744 /* Recursively remove directory and its contents */
745 int lr_rm_recursive(struct lr_info *info)
749 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
750 rc = system(info->cmd);
757 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
758 int lr_rm_special(struct lr_info *info)
762 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
763 status->ls_targets[info->target_no], SPECIAL_DIR,
765 rc = lr_rmfile(info);
768 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
769 info->dest, rc, errno);
773 /* Replicate file and directory create events */
774 int lr_create(struct lr_info *info)
781 /* Is target FID present on the source? */
782 rc = lr_get_path(info, info->tfid);
784 /* Source file has disappeared. Not an error. */
785 lr_debug(DINFO, "create: tfid %s not found on"
786 "source-fs\n", info->tfid);
791 strcpy(info->savedpath, info->path);
793 /* Is parent FID present on the source */
794 rc = lr_get_path(info, info->pfid);
796 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
803 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
804 len = strlen(info->path);
805 if (len == 1 && info->path[0] == '/')
806 snprintf(info->dest, PATH_MAX, "%s", info->name);
807 else if (len - 1 > 0 && info->path[len - 1] == '/')
808 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
810 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
812 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
814 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
815 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
816 info->tfid, info->dest, info->savedpath);
820 /* Is f2p(pfid) present on the target? If not, the parent has
823 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
825 if (access(info->dest, F_OK) != 0) {
826 lr_debug(DTRACE, "create: parent %s not found\n",
831 for (info->target_no = 0; info->target_no < status->ls_num_targets;
833 snprintf(info->dest, PATH_MAX, "%s/%s",
834 status->ls_targets[info->target_no], info->savedpath);
835 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
839 rc1 = lr_mkfile(info);
840 if (mkspecial || rc1 == -ENOENT) {
841 rc1 = lr_mk_special(info);
849 /* Replicate a file remove (rmdir/unlink) operation */
850 int lr_remove(struct lr_info *info)
855 for (info->target_no = 0; info->target_no < status->ls_num_targets;
858 rc1 = lr_rm_special(info);
862 rc1 = lr_get_path(info, info->pfid);
863 if (rc1 == -ENOENT) {
864 lr_debug(DINFO, "remove: pfid %s not found\n",
872 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
873 status->ls_targets[info->target_no], info->path,
876 rc1 = lr_rmfile(info);
877 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
878 info->dest, rc1, errno);
879 if (rc1 == -ENOTEMPTY)
880 rc1 = lr_rm_recursive(info);
890 /* Replicate a rename/move operation. */
891 int lr_move(struct lr_info *info)
897 int special_dest = 0;
898 char srcpath[PATH_MAX + 1] = "";
900 LASSERT(info->is_extended);
902 rc_src = lr_get_path(info, info->spfid);
903 if (rc_src < 0 && rc_src != -ENOENT)
905 memcpy(srcpath, info->path, strlen(info->path));
907 rc_dest = lr_get_path(info, info->pfid);
908 if (rc_dest < 0 && rc_dest != -ENOENT)
911 for (info->target_no = 0; info->target_no < status->ls_num_targets;
915 snprintf(info->dest, PATH_MAX, "%s/%s",
916 status->ls_targets[info->target_no],
918 if (access(info->dest, F_OK) != 0) {
921 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
922 status->ls_targets[info->target_no],
923 info->path, info->name);
925 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
928 if (rc_dest == -ENOENT) {
929 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
930 status->ls_targets[info->target_no],
931 SPECIAL_DIR, info->sfid);
933 lr_debug(DINFO, "special dest %s\n", info->dest);
937 snprintf(info->src, PATH_MAX, "%s/%s/%s",
938 status->ls_targets[info->target_no],
939 srcpath, info->sname);
940 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
943 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
945 snprintf(info->src, PATH_MAX, "%s/%s/%s",
946 status->ls_targets[info->target_no],
947 SPECIAL_DIR, info->sfid);
949 lr_debug(DINFO, "special src %s\n", info->src);
954 if (strcmp(info->src, info->dest) != 0) {
955 rc1 = rename(info->src, info->dest);
958 lr_debug(DINFO, "rename returns %d\n", rc1);
962 rc1 = lr_remove_pc(info->spfid, info->sfid);
964 lr_cascade_move(info->sfid, info->dest, info);
967 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
969 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
970 info->src, info->dest, rc1, errno);
977 /* Replicate a hard link */
978 int lr_link(struct lr_info *info)
986 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
987 rc = stat(info->src, &st);
991 for (info->target_no = 0; info->target_no < status->ls_num_targets;
998 /* Search through the hardlinks to get the src and dest */
999 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
1000 info->dest[0] == 0); i++) {
1001 rc1 = lr_get_path_ln(info, info->tfid, i);
1002 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
1003 info->path, info->name, i, rc1);
1007 len = strlen(info->path) - strlen(info->name);
1008 if (len > 0 && strcmp(info->path + len,
1010 snprintf(info->dest, PATH_MAX, "%s/%s",
1011 status->ls_targets[info->target_no],
1013 else if (info->src[0] == 0)
1014 snprintf(info->src, PATH_MAX, "%s/%s",
1015 status->ls_targets[info->target_no],
1024 if (info->src[0] == 0 || info->dest[0] == 0)
1025 /* Could not find the source or destination.
1026 This can happen when some links don't exist
1030 if (info->src[0] == 0)
1031 snprintf(info->src, PATH_MAX, "%s/%s/%s",
1032 status->ls_targets[info->target_no],
1033 SPECIAL_DIR, info->tfid);
1034 else if (info->dest[0] == 0)
1035 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1036 status->ls_targets[info->target_no],
1037 SPECIAL_DIR, info->tfid);
1039 rc1 = link(info->src, info->dest);
1040 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
1041 info->src, info->dest, rc1, strerror(errno));
1049 /* Replicate file attributes */
1050 int lr_setattr(struct lr_info *info)
1055 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1057 rc = lr_get_path(info, info->tfid);
1059 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1064 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1065 info->target_no++) {
1067 snprintf(info->dest, PATH_MAX, "%s/%s",
1068 status->ls_targets[info->target_no], info->path);
1069 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1072 rc1 = lr_sync_data(info);
1074 rc1 = lr_copy_attr(info->src, info->dest);
1081 /* Replicate xattrs */
1082 int lr_setxattr(struct lr_info *info)
1086 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1088 rc = lr_get_path(info, info->tfid);
1090 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1095 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1096 info->target_no++) {
1098 snprintf(info->dest, PATH_MAX, "%s/%s",
1099 status->ls_targets[info->target_no], info->path);
1100 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1103 rc1 = lr_copy_xattr(info);
1111 /* Parse a line of changelog entry */
1112 int lr_parse_line(void *priv, struct lr_info *info)
1114 struct changelog_ext_rec *rec;
1116 if (llapi_changelog_recv(priv, &rec) != 0)
1119 info->is_extended = CHANGELOG_REC_EXTENDED(rec);
1120 info->recno = rec->cr_index;
1121 info->type = rec->cr_type;
1122 sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
1123 sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
1124 strncpy(info->name, rec->cr_name, rec->cr_namelen);
1126 if (fid_is_sane(&rec->cr_sfid)) {
1127 sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
1128 sprintf(info->spfid, DFID, PFID(&rec->cr_spfid));
1129 strncpy(info->sname, changelog_rec_sname(rec),
1130 changelog_rec_snamelen(rec));
1131 info->sname[changelog_rec_snamelen(rec)] = '\0';
1134 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1135 info->name, info->sname);
1137 info->name[rec->cr_namelen] = '\0';
1140 printf("Rec %lld: %d %s\n", info->recno, info->type,
1144 llapi_changelog_free(&rec);
1150 /* Initialize the replication parameters */
1151 int lr_init_status()
1153 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1157 status = calloc(size, 1);
1160 status->ls_version = REPLICATE_STATUS_VER;
1161 status->ls_size = size;
1162 status->ls_last_recno = -1;
1166 /* Make a backup of the statuslog */
1167 void lr_backup_log()
1169 char backupfile[PATH_MAX];
1173 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1174 (void) rename(statuslog, backupfile);
1180 /* Save replication parameters to a statuslog. */
1185 size_t write_size = status->ls_size;
1186 struct lr_parent_child_list *curr;
1189 if (statuslog == NULL)
1194 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1197 fprintf(stderr, "Error opening log file for writing (%s)\n",
1202 size = write(fd, status, write_size);
1203 if (size != write_size) {
1204 fprintf(stderr, "Error writing to log file (%s) %d\n",
1210 for (curr = parents; curr; curr = curr->pc_next) {
1211 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1212 if (size != sizeof(curr->pc_log)) {
1213 fprintf(stderr, "Error writing to log file (%s) %d\n",
1223 /* Read statuslog and populate the replication parameters. Command
1224 * line parameters take precedence over parameters in the log file.*/
1227 struct lr_parent_child_list *tmp;
1228 struct lr_parent_child_log rec;
1229 struct lustre_rsync_status *s;
1232 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1235 if (statuslog == NULL)
1238 s = calloc(1, read_size);
1240 GOTO(out, rc = -ENOMEM);
1242 fd = open(statuslog, O_RDONLY);
1244 GOTO(out, rc = -errno);
1245 size = read(fd, s, read_size);
1246 if (size != read_size)
1247 GOTO(out, rc = -EINVAL);
1248 if (read_size < s->ls_size) {
1249 read_size = s->ls_size;
1250 s = lr_grow_buf(s, read_size);
1252 GOTO(out, rc = -ENOMEM);
1253 if (lseek(fd, 0, SEEK_SET) == -1)
1254 GOTO(out, rc = -errno);
1255 size = read(fd, s, read_size);
1256 if (size != read_size)
1257 GOTO(out, rc = -EINVAL);
1260 while (read(fd, &rec, sizeof(rec)) != 0) {
1261 tmp = calloc(1, sizeof(*tmp));
1263 GOTO(out, rc = -ENOMEM);
1265 tmp->pc_next = parents;
1269 /* copy uninitialized fields to status */
1270 if (status->ls_num_targets == 0) {
1271 if (status->ls_size != s->ls_size) {
1272 status = lr_grow_buf(status, s->ls_size);
1274 GOTO(out, rc = -ENOMEM);
1275 status->ls_size = s->ls_size;
1277 status->ls_num_targets = s->ls_num_targets;
1278 memcpy(status->ls_targets, s->ls_targets,
1279 (PATH_MAX + 1) * s->ls_num_targets);
1281 if (status->ls_last_recno == -1)
1282 status->ls_last_recno = s->ls_last_recno;
1284 if (status->ls_registration[0] == '\0')
1285 strncpy(status->ls_registration, s->ls_registration,
1288 if (status->ls_mdt_device[0] == '\0')
1289 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1292 if (status->ls_source_fs[0] == '\0')
1293 strncpy(status->ls_source_fs, s->ls_source_fs,
1296 if (status->ls_source[0] == '\0')
1297 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1307 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1309 int lr_clear_cl(struct lr_info *info, int force)
1311 char mdt_device[LR_NAME_MAXLEN + 1];
1315 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1316 if (info->type == CL_RENAME)
1317 rec = info->recno + 1;
1320 if (!noclear && !dryrun) {
1321 /* llapi_changelog_clear modifies the mdt
1322 * device name so make a copy of it until this
1325 strncpy(mdt_device, status->ls_mdt_device,
1327 rc = llapi_changelog_clear(mdt_device,
1328 status->ls_registration,
1331 printf("Changelog clear (%s, %s, %lld) "
1332 "returned %d\n", status->ls_mdt_device,
1333 status->ls_registration, rec, rc);
1335 if (!rc && !dryrun) {
1336 status->ls_last_recno = rec;
1345 /* Locate a usable version of rsync. At this point we'll use any
1347 int lr_locate_rsync()
1353 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1354 fp = popen(rsync, "r");
1358 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1363 len = strlen(rsync);
1364 if (len > 0 && rsync[len - 1] == '\n')
1365 rsync[len - 1] = '\0';
1368 /* Determine the version of rsync */
1369 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1370 fp = popen(rsync_ver, "r");
1374 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1378 len = strlen(rsync_ver);
1379 if (len > 0 && rsync_ver[len - 1] == '\n')
1380 rsync_ver[len - 1] = '\0';
1387 /* Print the replication parameters */
1388 void lr_print_status(struct lr_info *info)
1395 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1396 printf("MDT device: %s\n", status->ls_mdt_device);
1397 printf("Source: %s\n", status->ls_source);
1398 for (i = 0; i < status->ls_num_targets; i++)
1399 printf("Target: %s\n", status->ls_targets[i]);
1400 if (statuslog != NULL)
1401 printf("Statuslog: %s\n", statuslog);
1402 printf("Changelog registration: %s\n", status->ls_registration);
1403 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1405 printf("Replicate xattrs: no\n");
1407 printf("Clear changelog after use: no\n");
1409 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1412 void lr_print_failure(struct lr_info *info, int rc)
1414 fprintf(stderr, "Replication of operation failed(%d):"
1415 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1416 changelog_type2str(info->type), info->type, info->tfid,
1417 info->pfid, info->name);
1420 /* Replicate filesystem operations from src_path to target_path */
1423 void *changelog_priv;
1424 struct lr_info *info;
1425 struct lr_info *ext = NULL;
1433 info = calloc(1, sizeof(struct lr_info));
1437 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1439 fprintf(stderr, "Source path is not a valid Lustre client "
1443 if (status->ls_mdt_device[0] == '\0')
1444 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1445 status->ls_source_fs, DEFAULT_MDT);
1447 ext = calloc(1, sizeof(struct lr_info));
1453 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1454 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1456 rc = mkdir(info->dest, 0777);
1457 if (rc == -1 && errno != EEXIST) {
1458 fprintf(stderr, "Error writing to target path %s.\n",
1459 status->ls_targets[i]);
1463 rc = llistxattr(info->src, info->xlist, info->xsize);
1464 if (rc == -1 && errno == ENOTSUP) {
1465 fprintf(stderr, "xattrs not supported on %s\n",
1466 status->ls_targets[i]);
1470 if (xattr_not_supp == status->ls_num_targets)
1471 /* None of the targets support xattrs. */
1474 lr_print_status(info);
1476 /* Open changelogs for consumption*/
1477 rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
1478 status->ls_source_fs, status->ls_last_recno);
1480 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1481 status->ls_source_fs);
1485 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1487 if (info->type == CL_RENAME && !info->is_extended) {
1488 /* Newer rename operations extends changelog to store
1489 * source file information, but old changelog has
1492 if (lr_parse_line(changelog_priv, ext) != 0)
1494 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1495 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1496 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1497 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1498 strncpy(info->sname, info->name, sizeof(info->sname));
1499 strncpy(info->name, ext->name, sizeof(info->name));
1500 info->is_extended = 1;
1508 switch(info->type) {
1513 rc = lr_create(info);
1517 rc = lr_remove(info);
1527 rc = lr_setattr(info);
1530 rc = lr_setxattr(info);
1537 /* Nothing needs to be done for these entries */
1542 DEBUG_EXIT(info, rc);
1543 if (rc && rc != -ENOENT) {
1544 lr_print_failure(info, rc);
1549 lr_clear_cl(info, 0);
1551 bzero(info, sizeof(struct lr_info));
1552 bzero(ext, sizeof(struct lr_info));
1556 llapi_changelog_fini(&changelog_priv);
1558 if (errors || verbose)
1559 printf("Errors: %d\n", errors);
1561 /* Clear changelog records used so far */
1562 lr_clear_cl(info, 1);
1565 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1566 printf("Changelog records consumed: %lld\n", rec_count);
1581 termination_handler (int signum)
1583 /* Set a flag for the replicator to gracefully shutdown */
1585 printf("lustre_rsync halting.\n");
1588 int main(int argc, char *argv[])
1594 if ((rc = lr_init_status()) != 0)
1597 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1598 long_opts, NULL)) >= 0) {
1601 /* Assume absolute paths */
1605 /* Assume absolute paths */
1606 strncpy(status->ls_source, optarg, PATH_MAX);
1609 status->ls_num_targets++;
1611 if (numtargets != status->ls_num_targets) {
1612 /* Targets were read from a log
1613 file. The ones specified on the
1614 command line take precedence. The
1615 ones from the log file will be
1617 status->ls_num_targets = numtargets;
1619 newsize = sizeof (struct lustre_rsync_status) +
1620 (status->ls_num_targets * (PATH_MAX + 1));
1621 if (status->ls_size != newsize) {
1622 status->ls_size = newsize;
1623 status = lr_grow_buf(status, newsize);
1627 strncpy(status->ls_targets[status->ls_num_targets - 1],
1632 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1635 strncpy(status->ls_registration, optarg,
1640 (void) lr_read_log();
1646 if (strcmp("no", optarg) == 0) {
1648 } else if (strcmp("yes", optarg) != 0) {
1649 printf("Invalid parameter %s. "
1650 "Specify --xattr=no or --xattr=yes\n",
1659 /* Undocumented option cl-clear */
1660 if (strcmp("no", optarg) == 0) {
1662 } else if (strcmp("yes", optarg) != 0) {
1663 printf("Invalid parameter %s. "
1664 "Specify --cl-clear=no "
1665 "or --cl-clear=yes\n",
1671 /* Undocumented option use-rsync */
1675 /* Undocumented option rsync-threshold */
1676 rsync_threshold = atol(optarg);
1679 /* Undocumented option start-recno */
1680 status->ls_last_recno = atol(optarg);
1683 /* Undocumented option debug */
1684 debug = atoi(optarg);
1685 if (debug < 0 || debug > 2)
1689 /* Undocumented option debug log file */
1690 if (debug_log != NULL)
1692 debug_log = fopen(optarg, "a");
1693 if (debug_log == NULL) {
1694 printf("Cannot open %s for debug log\n",
1700 fprintf(stderr, "error: %s: option '%s' "
1701 "unrecognized.\n", argv[0], argv[optind - 1]);
1707 if (status->ls_last_recno == -1)
1708 status->ls_last_recno = 0;
1709 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1710 /* No registration ID was passed in. */
1711 printf("Please specify changelog consumer registration id.\n");
1715 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1716 fprintf(stderr, "Please specify the source path.\n");
1720 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1721 fprintf(stderr, "Please specify the target path.\n");
1726 /* This plumbing is needed for some of the ioctls behind
1727 llapi calls to work. */
1728 if (obd_initialize(argc, argv) < 0) {
1729 fprintf(stderr, "obd_initialize failed.\n");
1733 rc = lr_locate_rsync();
1734 if (use_rsync && rc != 0) {
1735 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1739 signal(SIGINT, termination_handler);
1740 signal(SIGHUP, termination_handler);
1741 signal(SIGTERM, termination_handler);
1743 rc = lr_replicate();
1745 if (debug_log != NULL)