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, 2016, 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.
115 #include <sys/stat.h>
116 #include <sys/types.h>
121 #include <sys/xattr.h>
122 #include <linux/types.h>
124 #include <libcfs/libcfs_debug.h>
125 #include <libcfs/util/string.h>
126 #include <libcfs/util/parser.h>
127 #include <lustre/lustreapi.h>
128 #include "lustre_rsync.h"
130 #define REPLICATE_STATUS_VER 1
131 #define CLEAR_INTERVAL 100
132 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
134 #define TYPE_STR_LEN 16
136 #define DEFAULT_MDT "-MDT0000"
137 #define SPECIAL_DIR ".lustrerepl"
138 #define RSYNC "rsync"
145 /* Not used; declared for fulfilling obd.c's dependency. */
146 command_t cmdlist[0];
147 extern int obd_initialize(int argc, char **argv);
149 /* Information for processing a changelog record. This structure is
150 allocated on the heap instead of allocating large variables on the
155 unsigned int is_extended:1;
156 enum changelog_rec_type type;
157 char tfid[LR_FID_STR_LEN];
158 char pfid[LR_FID_STR_LEN];
159 char sfid[LR_FID_STR_LEN];
160 char spfid[LR_FID_STR_LEN];
161 char sname[NAME_MAX + 1];
162 char name[NAME_MAX + 1];
163 char src[PATH_MAX + 1];
164 char dest[PATH_MAX + 1];
165 char path[PATH_MAX + 1];
166 char savedpath[PATH_MAX + 1];
167 char link[PATH_MAX + 1];
168 char linktmp[PATH_MAX + 1];
173 /* Variables for querying the xattributes */
180 struct lr_parent_child_list {
181 struct lr_parent_child_log pc_log;
182 struct lr_parent_child_list *pc_next;
185 struct lustre_rsync_status *status;
186 char *statuslog; /* Name of the status log file */
188 int noxattr; /* Flag to turn off replicating xattrs */
189 int noclear; /* Flag to turn off clearing changelogs */
190 int debug; /* Flag to turn debugging information on and off */
191 int verbose; /* Verbose output */
192 long long rec_count; /* No of changelog records that were processed */
195 int use_rsync; /* Flag to turn on use of rsync to copy data */
196 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
197 int quit; /* Flag to stop processing the changelog; set on the
198 receipt of a signal */
199 int abort_on_err = 0;
201 char rsync[PATH_MAX];
202 char rsync_ver[PATH_MAX];
203 struct lr_parent_child_list *parents;
207 /* Command line options */
208 struct option long_opts[] = {
209 { .val = 'l', .name = "statuslog", .has_arg = required_argument },
210 { .val = 'm', .name = "mdt", .has_arg = required_argument },
211 { .val = 's', .name = "source", .has_arg = required_argument },
212 { .val = 't', .name = "target", .has_arg = required_argument },
213 { .val = 'u', .name = "user", .has_arg = required_argument },
214 { .val = 'v', .name = "verbose", .has_arg = no_argument },
215 { .val = 'x', .name = "xattr", .has_arg = required_argument },
216 { .val = 'z', .name = "dry-run", .has_arg = no_argument },
217 /* Undocumented options follow */
218 { .val = 'a', .name = "abort-on-err", .has_arg = no_argument },
219 { .val = 'c', .name = "cl-clear", .has_arg = required_argument },
220 { .val = 'd', .name = "debug", .has_arg = required_argument },
221 { .val = 'D', .name = "debuglog", .has_arg = required_argument },
222 { .val = 'n', .name = "start-recno", .has_arg = required_argument },
223 { .val = 'r', .name = "use-rsync", .has_arg = no_argument },
224 { .val = 'y', .name = "rsync-threshold",
225 .has_arg = required_argument },
228 /* Command line usage */
231 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
232 "-m <mdt> -r <user id> -l <status log>\n"
233 "lustre_rsync can also pick up parameters from a "
234 "status log created earlier.\n"
235 "\tlustre_rsync -l <log_file>\n"
237 "\t--xattr <yes|no> replicate EAs\n"
238 "\t--abort-on-err abort at first err\n"
240 "\t--dry-run don't write anything\n");
243 #define DEBUG_ENTRY(info) \
244 lr_debug(D_TRACE, "***** Start %lld %s (%d) %s %s %s *****\n", \
245 (info)->recno, changelog_type2str((info)->type), \
246 (info)->type, (info)->tfid, (info)->pfid, (info)->name);
248 #define DEBUG_EXIT(info, rc) \
249 lr_debug(D_TRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n", \
250 (info)->recno, changelog_type2str((info)->type), \
251 (info)->type, (info)->tfid, (info)->pfid, (info)->name, rc);
253 /* Print debug information. This is controlled by the value of the
254 global variable 'debug' */
255 void lr_debug(int level, const char *fmt, ...)
263 if (debug_log != NULL)
264 vfprintf(debug_log, fmt, ap);
266 vfprintf(stdout, fmt, ap);
271 void * lr_grow_buf(void *buf, int size)
275 ptr = realloc(buf, size);
282 /* Use rsync to replicate file data */
283 int lr_rsync_data(struct lr_info *info)
286 struct stat st_src, st_dest;
289 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
291 rc = stat(info->src, &st_src);
293 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
294 info->src, info->name);
300 rc = stat(info->dest, &st_dest);
302 fprintf(stderr, "Error: Unable to stat dest=%s\n",
307 if (st_src.st_mtime != st_dest.st_mtime ||
308 st_src.st_size != st_dest.st_size) {
309 /* XXX spawning off an rsync for every data sync and
310 * waiting synchronously is bad for performance.
311 * librsync could possibly used here. But it does not
312 * seem to be of production grade. Multi-threaded
313 * replication is also to be considered.
316 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
318 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
319 status = system(cmd);
322 } else if (WIFEXITED(status)) {
323 status = WEXITSTATUS(status);
326 else if (status == 23 || status == 24)
327 /* Error due to vanished source files;
333 lr_debug(DINFO, "rsync %s exited with %d %d\n",
334 info->src, status, rc);
339 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
340 info->dest, info->tfid);
346 int lr_copy_data(struct lr_info *info)
356 fd_src = open(info->src, O_RDONLY);
359 if (fstat(fd_src, &st_src) == -1 ||
360 stat(info->dest, &st_dest) == -1)
363 if (st_src.st_mtime == st_dest.st_mtime &&
364 st_src.st_size == st_dest.st_size)
367 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
368 /* It is more efficient to use rsync to replicate
369 large files. Any file larger than rsync_threshold
370 is handed off to rsync. */
371 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
372 rc = lr_rsync_data(info);
376 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
381 bufsize = st_dest.st_blksize;
383 if (info->bufsize < bufsize) {
385 info->buf = lr_grow_buf(info->buf, bufsize);
386 if (info->buf == NULL) {
390 info->bufsize = bufsize;
398 rsize = read(fd_src, buf, bufsize);
408 wsize = write(fd_dest, buf, rsize);
428 /* Copy data from source to destination */
429 int lr_sync_data(struct lr_info *info)
432 return lr_rsync_data(info);
434 return lr_copy_data(info);
437 /* Copy all attributes from file src to file dest */
438 int lr_copy_attr(char *src, char *dest)
443 if (stat(src, &st) == -1 ||
444 chmod(dest, st.st_mode) == -1 ||
445 chown(dest, st.st_uid, st.st_gid) == -1)
448 time.actime = st.st_atime;
449 time.modtime = st.st_mtime;
450 if (utime(dest, &time) == -1)
455 /* Copy all xattrs from file info->src to info->dest */
456 int lr_copy_xattr(struct lr_info *info)
458 size_t size = info->xsize;
467 rc = llistxattr(info->src, info->xlist, size);
468 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
469 info->src, info->xlist, rc, errno);
470 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
471 size = rc > PATH_MAX ? rc : PATH_MAX;
472 info->xlist = lr_grow_buf(info->xlist, size);
473 if (info->xlist == NULL)
476 rc = llistxattr(info->src, info->xlist, size);
477 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
478 info->src, rc, errno);
485 while (start < len) {
487 rc = lgetxattr(info->src, info->xlist + start,
489 if (info->xvalue == NULL || errno == ERANGE) {
490 size = rc > PATH_MAX ? rc : PATH_MAX;
491 info->xvalue = lr_grow_buf(info->xvalue, size);
492 if (info->xvalue == NULL)
495 rc = lgetxattr(info->src, info->xlist + start,
498 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
502 rc = lsetxattr(info->dest, info->xlist + start,
503 info->xvalue, size, 0);
504 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
507 if (errno != ENOTSUP) {
508 fprintf(stderr, "Error replicating "
509 " xattr for %s: %d\n",
516 start += strlen(info->xlist + start) + 1;
519 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
524 /* Retrieve the filesystem path for a given FID and a given
525 linkno. The path is returned in info->path */
526 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
528 long long recno = -1;
531 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
532 PATH_MAX, &recno, &linkno);
533 if (rc < 0 && rc != -ENOENT) {
534 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
535 status->ls_source, fidstr, -rc, strerror(errno = -rc));
541 /* Retrieve the filesystem path for a given FID. The path is returned
543 int lr_get_path(struct lr_info *info, char *fidstr)
545 return lr_get_path_ln(info, fidstr, 0);
548 /* Generate the path for opening by FID */
549 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
551 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
552 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
557 /* Read the symlink information into 'info->link' */
558 int lr_get_symlink(struct lr_info *info)
563 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
564 rc = readlink(info->src, info->linktmp, PATH_MAX);
566 info->linktmp[rc] = '\0';
569 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
571 if (strncmp(info->linktmp, status->ls_source,
572 strlen(status->ls_source)) == 0) {
573 /* Strip source fs path and replace with target fs path. */
574 link = info->linktmp + strlen(status->ls_source);
575 snprintf(info->src, PATH_MAX, "%s%s",
576 status->ls_targets[info->target_no], link);
579 link = info->linktmp;
581 strlcpy(info->link, link, sizeof(info->link));
586 /* Create file/directory/device file/symlink. */
587 int lr_mkfile(struct lr_info *info)
593 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
594 if (info->type == CL_MKDIR) {
595 rc = mkdir(info->dest, 0777);
596 } else if (info->type == CL_SOFTLINK) {
597 lr_get_symlink(info);
598 rc = symlink(info->link, info->dest);
599 } else if (info->type == CL_MKNOD) {
600 lr_get_FID_PATH(status->ls_source, info->tfid,
601 info->src, PATH_MAX);
602 rc = stat(info->src, &st);
609 rc = mknod(info->dest, st.st_mode, st.st_rdev);
611 rc = mknod(info->dest, S_IFREG | 0777, 0);
621 /* Sync data and attributes */
622 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
623 lr_debug(DTRACE, "Syncing data and attributes %s\n",
625 (void) lr_copy_xattr(info);
626 if (info->type == CL_CREATE)
627 rc = lr_sync_data(info);
629 rc = lr_copy_attr(info->src, info->dest);
632 /* Source file has disappeared. Not an error. */
635 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
642 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
644 struct lr_parent_child_list *p;
647 p = calloc(1, sizeof(*p));
650 len = strlcpy(p->pc_log.pcl_pfid, pfid, sizeof(p->pc_log.pcl_pfid));
651 if (len >= sizeof(p->pc_log.pcl_pfid))
653 len = strlcpy(p->pc_log.pcl_tfid, tfid, sizeof(p->pc_log.pcl_tfid));
654 if (len >= sizeof(p->pc_log.pcl_tfid))
656 len = strlcpy(p->pc_log.pcl_name, name, sizeof(p->pc_log.pcl_name));
657 if (len >= sizeof(p->pc_log.pcl_name))
660 p->pc_next = parents;
669 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
671 struct lr_parent_child_list *curr, *prev;
675 d = calloc(1, PATH_MAX + 1);
676 prev = curr = parents;
678 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
679 snprintf(d, PATH_MAX, "%s/%s", dest,
680 curr->pc_log.pcl_name);
681 snprintf(info->src, PATH_MAX, "%s/%s/%s",
682 status->ls_targets[info->target_no],
683 SPECIAL_DIR, curr->pc_log.pcl_tfid);
684 rc = rename(info->src, d);
686 fprintf(stderr, "Error renaming file "
688 info->src, d, errno);
692 parents = curr->pc_next;
694 prev->pc_next = curr->pc_next;
695 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
697 prev = curr = parents;
701 curr = curr->pc_next;
708 /* remove [info->spfid, info->sfid] from parents */
709 int lr_remove_pc(const char *pfid, const char *tfid)
711 struct lr_parent_child_list *curr, *prev;
713 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
714 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
715 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
717 parents = curr->pc_next;
719 prev->pc_next = curr->pc_next;
727 /* Create file under SPECIAL_DIR with its tfid as its name. */
728 int lr_mk_special(struct lr_info *info)
732 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
733 status->ls_targets[info->target_no], SPECIAL_DIR,
736 rc = lr_mkfile(info);
740 rc = lr_add_pc(info->pfid, info->tfid, info->name);
744 /* Remove a file or directory */
745 int lr_rmfile(struct lr_info *info)
749 if (info->type == CL_RMDIR)
750 rc = rmdir(info->dest);
752 rc = unlink(info->dest);
758 /* Recursively remove directory and its contents */
759 int lr_rm_recursive(struct lr_info *info)
763 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
764 rc = system(info->cmd);
771 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
772 int lr_rm_special(struct lr_info *info)
776 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
777 status->ls_targets[info->target_no], SPECIAL_DIR,
779 rc = lr_rmfile(info);
782 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
783 info->dest, rc, errno);
787 /* Replicate file and directory create events */
788 int lr_create(struct lr_info *info)
795 /* Is target FID present on the source? */
796 rc = lr_get_path(info, info->tfid);
798 /* Source file has disappeared. Not an error. */
799 lr_debug(DINFO, "create: tfid %s not found on"
800 "source-fs\n", info->tfid);
805 strcpy(info->savedpath, info->path);
807 /* Is parent FID present on the source */
808 rc = lr_get_path(info, info->pfid);
810 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
817 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
818 len = strlen(info->path);
819 if (len == 1 && info->path[0] == '/')
820 snprintf(info->dest, PATH_MAX, "%s", info->name);
821 else if (len - 1 > 0 && info->path[len - 1] == '/')
822 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
824 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
826 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
828 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
829 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
830 info->tfid, info->dest, info->savedpath);
834 /* Is f2p(pfid) present on the target? If not, the parent has
837 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
839 if (access(info->dest, F_OK) != 0) {
840 lr_debug(DTRACE, "create: parent %s not found\n",
845 for (info->target_no = 0; info->target_no < status->ls_num_targets;
847 snprintf(info->dest, PATH_MAX, "%s/%s",
848 status->ls_targets[info->target_no], info->savedpath);
849 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
853 rc1 = lr_mkfile(info);
854 if (mkspecial || rc1 == -ENOENT) {
855 rc1 = lr_mk_special(info);
863 /* Replicate a file remove (rmdir/unlink) operation */
864 int lr_remove(struct lr_info *info)
869 for (info->target_no = 0; info->target_no < status->ls_num_targets;
872 rc1 = lr_rm_special(info);
876 rc1 = lr_get_path(info, info->pfid);
877 if (rc1 == -ENOENT) {
878 lr_debug(DINFO, "remove: pfid %s not found\n",
886 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
887 status->ls_targets[info->target_no], info->path,
890 rc1 = lr_rmfile(info);
891 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
892 info->dest, rc1, errno);
893 if (rc1 == -ENOTEMPTY)
894 rc1 = lr_rm_recursive(info);
904 /* Replicate a rename/move operation. */
905 int lr_move(struct lr_info *info)
911 int special_dest = 0;
912 char srcpath[PATH_MAX + 1] = "";
914 assert(info->is_extended);
916 rc_src = lr_get_path(info, info->spfid);
917 if (rc_src < 0 && rc_src != -ENOENT)
919 memcpy(srcpath, info->path, strlen(info->path));
921 rc_dest = lr_get_path(info, info->pfid);
922 if (rc_dest < 0 && rc_dest != -ENOENT)
925 for (info->target_no = 0; info->target_no < status->ls_num_targets;
929 snprintf(info->dest, PATH_MAX, "%s/%s",
930 status->ls_targets[info->target_no],
932 if (access(info->dest, F_OK) != 0) {
935 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
936 status->ls_targets[info->target_no],
937 info->path, info->name);
939 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
942 if (rc_dest == -ENOENT) {
943 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
944 status->ls_targets[info->target_no],
945 SPECIAL_DIR, info->sfid);
947 lr_debug(DINFO, "special dest %s\n", info->dest);
951 snprintf(info->src, PATH_MAX, "%s/%s/%s",
952 status->ls_targets[info->target_no],
953 srcpath, info->sname);
954 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
957 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
959 snprintf(info->src, PATH_MAX, "%s/%s/%s",
960 status->ls_targets[info->target_no],
961 SPECIAL_DIR, info->sfid);
963 lr_debug(DINFO, "special src %s\n", info->src);
968 if (strcmp(info->src, info->dest) != 0) {
969 rc1 = rename(info->src, info->dest);
972 lr_debug(DINFO, "rename returns %d\n", rc1);
976 rc1 = lr_remove_pc(info->spfid, info->sfid);
979 lr_cascade_move(info->sfid, info->dest, info);
981 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
983 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
984 info->src, info->dest, rc1, errno);
991 /* Replicate a hard link */
992 int lr_link(struct lr_info *info)
999 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1000 rc = stat(info->src, &st);
1004 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1005 info->target_no++) {
1012 * The changelog record has the new parent directory FID and
1013 * name of the target file. So info->dest can be constructed
1014 * by getting the path of the new parent directory and
1015 * appending the target file name.
1017 rc1 = lr_get_path(info, info->pfid);
1018 lr_debug(rc1 ? 0 : DTRACE, "\tparent fid2path %s, %s, rc=%d\n",
1019 info->path, info->name, rc1);
1022 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1023 status->ls_targets[info->target_no],
1024 info->path, info->name);
1025 lr_debug(DINFO, "link destination is %s\n", info->dest);
1028 /* Search through the hardlinks to get the src */
1029 for (i = 0; i < st.st_nlink && info->src[0] == 0; i++) {
1030 rc1 = lr_get_path_ln(info, info->tfid, i);
1031 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
1032 info->path, info->name, i, rc1);
1037 * Compare the path of target FID with info->dest
1038 * to find out info->src.
1040 char srcpath[PATH_MAX];
1042 snprintf(srcpath, sizeof(srcpath), "%s/%s",
1043 status->ls_targets[info->target_no],
1046 if (strcmp(srcpath, info->dest) != 0) {
1047 strlcpy(info->src, srcpath, sizeof(info->src));
1048 lr_debug(DINFO, "link source is %s\n",
1058 if (info->src[0] == 0)
1059 snprintf(info->src, PATH_MAX, "%s/%s/%s",
1060 status->ls_targets[info->target_no],
1061 SPECIAL_DIR, info->tfid);
1062 else if (info->dest[0] == 0)
1063 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1064 status->ls_targets[info->target_no],
1065 SPECIAL_DIR, info->tfid);
1067 rc1 = link(info->src, info->dest);
1068 lr_debug(DINFO, "link: %s [to] %s; rc1=%d %s\n",
1069 info->src, info->dest, rc1,
1070 strerror(rc1 ? errno : 0));
1078 /* Replicate file attributes */
1079 int lr_setattr(struct lr_info *info)
1084 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1086 rc = lr_get_path(info, info->tfid);
1088 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1093 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1094 info->target_no++) {
1096 snprintf(info->dest, PATH_MAX, "%s/%s",
1097 status->ls_targets[info->target_no], info->path);
1098 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1101 rc1 = lr_sync_data(info);
1103 rc1 = lr_copy_attr(info->src, info->dest);
1110 /* Replicate xattrs */
1111 int lr_setxattr(struct lr_info *info)
1115 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1117 rc = lr_get_path(info, info->tfid);
1119 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1124 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1125 info->target_no++) {
1127 snprintf(info->dest, PATH_MAX, "%s/%s",
1128 status->ls_targets[info->target_no], info->path);
1129 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1132 rc1 = lr_copy_xattr(info);
1140 /* Parse a line of changelog entry */
1141 int lr_parse_line(void *priv, struct lr_info *info)
1143 struct changelog_rec *rec;
1144 struct changelog_ext_rename *rnm;
1146 size_t copylen = sizeof(info->name);
1148 if (llapi_changelog_recv(priv, &rec) != 0)
1151 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1152 info->recno = rec->cr_index;
1153 info->type = rec->cr_type;
1154 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1155 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1157 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1158 if (copylen > namelen + 1)
1159 copylen = namelen + 1;
1160 strlcpy(info->name, changelog_rec_name(rec), copylen);
1162 /* Don't use rnm if CLF_RENAME isn't set */
1163 rnm = changelog_rec_rename(rec);
1164 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1165 copylen = sizeof(info->sname);
1167 snprintf(info->sfid, sizeof(info->sfid), DFID,
1168 PFID(&rnm->cr_sfid));
1169 snprintf(info->spfid, sizeof(info->spfid), DFID,
1170 PFID(&rnm->cr_spfid));
1171 namelen = changelog_rec_snamelen(rec);
1172 if (copylen > namelen + 1)
1173 copylen = namelen + 1;
1174 strlcpy(info->sname, changelog_rec_sname(rec), copylen);
1177 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1178 info->name, info->sname);
1181 printf("Rec %lld: %d %s\n", info->recno, info->type,
1185 llapi_changelog_free(&rec);
1191 /* Initialize the replication parameters */
1192 int lr_init_status()
1194 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1198 status = calloc(size, 1);
1201 status->ls_version = REPLICATE_STATUS_VER;
1202 status->ls_size = size;
1203 status->ls_last_recno = -1;
1207 /* Make a backup of the statuslog */
1208 void lr_backup_log()
1210 char backupfile[PATH_MAX];
1214 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1215 (void) rename(statuslog, backupfile);
1221 /* Save replication parameters to a statuslog. */
1226 size_t write_size = status->ls_size;
1227 struct lr_parent_child_list *curr;
1230 if (statuslog == NULL)
1235 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1238 fprintf(stderr, "Error opening log file for writing (%s)\n",
1243 size = write(fd, status, write_size);
1244 if (size != write_size) {
1245 fprintf(stderr, "Error writing to log file (%s) %d\n",
1251 for (curr = parents; curr; curr = curr->pc_next) {
1252 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1253 if (size != sizeof(curr->pc_log)) {
1254 fprintf(stderr, "Error writing to log file (%s) %d\n",
1264 /* Read statuslog and populate the replication parameters. Command
1265 * line parameters take precedence over parameters in the log file.*/
1268 struct lr_parent_child_list *tmp;
1269 struct lr_parent_child_log rec;
1270 struct lustre_rsync_status *s;
1273 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1276 if (statuslog == NULL)
1279 s = calloc(1, read_size);
1285 fd = open(statuslog, O_RDONLY);
1291 size = read(fd, s, read_size);
1292 if (size != read_size) {
1297 if (read_size < s->ls_size) {
1298 read_size = s->ls_size;
1299 s = lr_grow_buf(s, read_size);
1305 if (lseek(fd, 0, SEEK_SET) == -1) {
1310 size = read(fd, s, read_size);
1311 if (size != read_size) {
1317 while (read(fd, &rec, sizeof(rec)) != 0) {
1318 tmp = calloc(1, sizeof(*tmp));
1325 tmp->pc_next = parents;
1329 /* copy uninitialized fields to status */
1330 if (status->ls_num_targets == 0) {
1331 if (status->ls_size != s->ls_size) {
1332 status = lr_grow_buf(status, s->ls_size);
1333 if (status == NULL) {
1338 status->ls_size = s->ls_size;
1340 status->ls_num_targets = s->ls_num_targets;
1341 memcpy(status->ls_targets, s->ls_targets,
1342 (PATH_MAX + 1) * s->ls_num_targets);
1344 if (status->ls_last_recno == -1)
1345 status->ls_last_recno = s->ls_last_recno;
1347 if (status->ls_registration[0] == '\0')
1348 strlcpy(status->ls_registration, s->ls_registration,
1349 sizeof(status->ls_registration));
1351 if (status->ls_mdt_device[0] == '\0')
1352 strlcpy(status->ls_mdt_device, s->ls_mdt_device,
1353 sizeof(status->ls_mdt_device));
1355 if (status->ls_source_fs[0] == '\0')
1356 strlcpy(status->ls_source_fs, s->ls_source_fs,
1357 sizeof(status->ls_source_fs));
1359 if (status->ls_source[0] == '\0')
1360 strlcpy(status->ls_source, s->ls_source,
1361 sizeof(status->ls_source));
1371 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1373 int lr_clear_cl(struct lr_info *info, int force)
1375 char mdt_device[LR_NAME_MAXLEN + 1];
1379 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1380 if (info->type == CL_RENAME)
1381 rec = info->recno + 1;
1384 if (!noclear && !dryrun) {
1385 /* llapi_changelog_clear modifies the mdt
1386 * device name so make a copy of it until this
1389 strlcpy(mdt_device, status->ls_mdt_device,
1390 sizeof(mdt_device));
1391 rc = llapi_changelog_clear(mdt_device,
1392 status->ls_registration,
1395 printf("Changelog clear (%s, %s, %lld) "
1396 "returned %d\n", status->ls_mdt_device,
1397 status->ls_registration, rec, rc);
1399 if (!rc && !dryrun) {
1400 status->ls_last_recno = rec;
1409 /* Locate a usable version of rsync. At this point we'll use any
1411 int lr_locate_rsync()
1417 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1418 fp = popen(rsync, "r");
1422 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1427 len = strlen(rsync);
1428 if (len > 0 && rsync[len - 1] == '\n')
1429 rsync[len - 1] = '\0';
1432 /* Determine the version of rsync */
1433 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1434 fp = popen(rsync_ver, "r");
1438 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1442 len = strlen(rsync_ver);
1443 if (len > 0 && rsync_ver[len - 1] == '\n')
1444 rsync_ver[len - 1] = '\0';
1451 /* Print the replication parameters */
1452 void lr_print_status(struct lr_info *info)
1459 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1460 printf("MDT device: %s\n", status->ls_mdt_device);
1461 printf("Source: %s\n", status->ls_source);
1462 for (i = 0; i < status->ls_num_targets; i++)
1463 printf("Target: %s\n", status->ls_targets[i]);
1464 if (statuslog != NULL)
1465 printf("Statuslog: %s\n", statuslog);
1466 printf("Changelog registration: %s\n", status->ls_registration);
1467 printf("Starting changelog record: %jd\n",
1468 (uintmax_t)status->ls_last_recno);
1470 printf("Replicate xattrs: no\n");
1472 printf("Clear changelog after use: no\n");
1474 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1477 void lr_print_failure(struct lr_info *info, int rc)
1479 fprintf(stderr, "Replication of operation failed(%d):"
1480 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1481 changelog_type2str(info->type), info->type, info->tfid,
1482 info->pfid, info->name);
1485 /* Replicate filesystem operations from src_path to target_path */
1488 void *changelog_priv;
1489 struct lr_info *info;
1490 struct lr_info *ext = NULL;
1498 info = calloc(1, sizeof(struct lr_info));
1502 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1504 fprintf(stderr, "Source path is not a valid Lustre client "
1508 if (status->ls_mdt_device[0] == '\0')
1509 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1510 status->ls_source_fs, DEFAULT_MDT);
1512 ext = calloc(1, sizeof(struct lr_info));
1518 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1519 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1521 rc = mkdir(info->dest, 0777);
1522 if (rc == -1 && errno != EEXIST) {
1523 fprintf(stderr, "Error writing to target path %s.\n",
1524 status->ls_targets[i]);
1528 rc = llistxattr(info->src, info->xlist, info->xsize);
1529 if (rc == -1 && errno == ENOTSUP) {
1530 fprintf(stderr, "xattrs not supported on %s\n",
1531 status->ls_targets[i]);
1535 if (xattr_not_supp == status->ls_num_targets)
1536 /* None of the targets support xattrs. */
1539 lr_print_status(info);
1541 /* Open changelogs for consumption*/
1542 rc = llapi_changelog_start(&changelog_priv,
1543 CHANGELOG_FLAG_BLOCK | CHANGELOG_FLAG_JOBID,
1544 status->ls_mdt_device, status->ls_last_recno);
1546 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1547 status->ls_source_fs);
1551 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1553 if (info->type == CL_RENAME && !info->is_extended) {
1554 /* Newer rename operations extends changelog to store
1555 * source file information, but old changelog has
1558 if (lr_parse_line(changelog_priv, ext) != 0)
1560 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1561 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1562 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1563 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1564 strlcpy(info->sname, info->name, sizeof(info->sname));
1565 strlcpy(info->name, ext->name, sizeof(info->name));
1566 info->is_extended = 1;
1574 switch(info->type) {
1579 rc = lr_create(info);
1583 rc = lr_remove(info);
1593 rc = lr_setattr(info);
1596 rc = lr_setxattr(info);
1603 /* Nothing needs to be done for these entries */
1608 DEBUG_EXIT(info, rc);
1609 if (rc && rc != -ENOENT) {
1610 lr_print_failure(info, rc);
1615 lr_clear_cl(info, 0);
1617 bzero(info, sizeof(struct lr_info));
1618 bzero(ext, sizeof(struct lr_info));
1622 llapi_changelog_fini(&changelog_priv);
1624 if (errors || verbose)
1625 printf("Errors: %d\n", errors);
1627 /* Clear changelog records used so far */
1628 lr_clear_cl(info, 1);
1631 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1632 printf("Changelog records consumed: %lld\n", rec_count);
1647 termination_handler (int signum)
1649 /* Set a flag for the replicator to gracefully shutdown */
1651 printf("lustre_rsync halting.\n");
1654 int main(int argc, char *argv[])
1660 if ((rc = lr_init_status()) != 0)
1663 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1664 long_opts, NULL)) >= 0) {
1667 /* Assume absolute paths */
1671 /* Assume absolute paths */
1672 strlcpy(status->ls_source, optarg,
1673 sizeof(status->ls_source));
1676 status->ls_num_targets++;
1678 if (numtargets != status->ls_num_targets) {
1679 /* Targets were read from a log
1680 file. The ones specified on the
1681 command line take precedence. The
1682 ones from the log file will be
1684 status->ls_num_targets = numtargets;
1686 newsize = sizeof (struct lustre_rsync_status) +
1687 (status->ls_num_targets * (PATH_MAX + 1));
1688 if (status->ls_size != newsize) {
1689 status->ls_size = newsize;
1690 status = lr_grow_buf(status, newsize);
1694 strlcpy(status->ls_targets[status->ls_num_targets - 1],
1695 optarg, sizeof(status->ls_targets[0]));
1698 strlcpy(status->ls_mdt_device, optarg,
1699 sizeof(status->ls_mdt_device));
1702 strlcpy(status->ls_registration, optarg,
1703 sizeof(status->ls_registration));
1707 (void) lr_read_log();
1713 if (strcmp("no", optarg) == 0) {
1715 } else if (strcmp("yes", optarg) != 0) {
1716 printf("Invalid parameter %s. "
1717 "Specify --xattr=no or --xattr=yes\n",
1726 /* Undocumented option cl-clear */
1727 if (strcmp("no", optarg) == 0) {
1729 } else if (strcmp("yes", optarg) != 0) {
1730 printf("Invalid parameter %s. "
1731 "Specify --cl-clear=no "
1732 "or --cl-clear=yes\n",
1738 /* Undocumented option use-rsync */
1742 /* Undocumented option rsync-threshold */
1743 rsync_threshold = atol(optarg);
1746 /* Undocumented option start-recno */
1747 status->ls_last_recno = atol(optarg);
1750 /* Undocumented option debug */
1751 debug = atoi(optarg);
1752 if (debug < 0 || debug > 2)
1756 /* Undocumented option debug log file */
1757 if (debug_log != NULL)
1759 debug_log = fopen(optarg, "a");
1760 if (debug_log == NULL) {
1761 printf("Cannot open %s for debug log\n",
1767 fprintf(stderr, "error: %s: option '%s' "
1768 "unrecognized.\n", argv[0], argv[optind - 1]);
1774 if (status->ls_last_recno == -1)
1775 status->ls_last_recno = 0;
1776 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1777 /* No registration ID was passed in. */
1778 printf("Please specify changelog consumer registration id.\n");
1782 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1783 fprintf(stderr, "Please specify the source path.\n");
1787 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1788 fprintf(stderr, "Please specify the target path.\n");
1793 /* This plumbing is needed for some of the ioctls behind
1794 llapi calls to work. */
1795 if (obd_initialize(argc, argv) < 0) {
1796 fprintf(stderr, "obd_initialize failed.\n");
1800 rc = lr_locate_rsync();
1801 if (use_rsync && rc != 0) {
1802 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1806 signal(SIGINT, termination_handler);
1807 signal(SIGHUP, termination_handler);
1808 signal(SIGTERM, termination_handler);
1810 rc = lr_replicate();
1812 if (debug_log != NULL)