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, 2017, 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/util/string.h>
125 #include <lustre/lustreapi.h>
126 #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"
145 * Information for processing a changelog record. This structure is
146 * allocated on the heap instead of allocating large variables on the
152 unsigned int is_extended:1;
153 enum changelog_rec_type type;
154 char tfid[LR_FID_STR_LEN];
155 char pfid[LR_FID_STR_LEN];
156 char sfid[LR_FID_STR_LEN];
157 char spfid[LR_FID_STR_LEN];
158 char sname[NAME_MAX + 1];
159 char name[NAME_MAX + 1];
160 char src[3 * PATH_MAX + 1];
161 char dest[3 * PATH_MAX + 1];
162 char path[PATH_MAX + 1];
163 char savedpath[PATH_MAX + 1];
164 char link[PATH_MAX + 1];
165 char linktmp[PATH_MAX + 1];
169 /* Variables for querying the xattributes */
176 struct lr_parent_child_list {
177 struct lr_parent_child_log pc_log;
178 struct lr_parent_child_list *pc_next;
181 struct lustre_rsync_status *status;
182 char *statuslog; /* Name of the status log file */
184 int noxattr; /* Flag to turn off replicating xattrs */
185 int noclear; /* Flag to turn off clearing changelogs */
186 int debug; /* Flag to turn debugging information on and off */
187 int verbose; /* Verbose output */
188 long long rec_count; /* No of changelog records that were processed */
191 int use_rsync; /* Flag to turn on use of rsync to copy data */
192 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
193 int quit; /* Flag to stop processing the changelog; set on the
194 * receipt of a signal
198 char rsync[PATH_MAX + 128];
199 char rsync_ver[PATH_MAX * 2];
200 struct lr_parent_child_list *parents;
204 /* Command line options */
205 struct option long_opts[] = {
206 { .val = 'l', .name = "statuslog", .has_arg = required_argument },
207 { .val = 'm', .name = "mdt", .has_arg = required_argument },
208 { .val = 's', .name = "source", .has_arg = required_argument },
209 { .val = 't', .name = "target", .has_arg = required_argument },
210 { .val = 'u', .name = "user", .has_arg = required_argument },
211 { .val = 'v', .name = "verbose", .has_arg = no_argument },
212 { .val = 'x', .name = "xattr", .has_arg = required_argument },
213 { .val = 'z', .name = "dry-run", .has_arg = no_argument },
214 /* Undocumented options follow */
215 { .val = 'a', .name = "abort-on-err", .has_arg = no_argument },
216 { .val = 'c', .name = "cl-clear", .has_arg = required_argument },
217 { .val = 'd', .name = "debug", .has_arg = required_argument },
218 { .val = 'D', .name = "debuglog", .has_arg = required_argument },
219 { .val = 'n', .name = "start-recno", .has_arg = required_argument },
220 { .val = 'r', .name = "use-rsync", .has_arg = no_argument },
221 { .val = 'y', .name = "rsync-threshold",
222 .has_arg = required_argument },
225 /* Command line usage */
228 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
229 "-m <mdt> -r <user id> -l <status log>\n"
230 "lustre_rsync can also pick up parameters from a "
231 "status log created earlier.\n"
232 "\tlustre_rsync -l <log_file>\n"
234 "\t--xattr <yes|no> replicate EAs\n"
235 "\t--abort-on-err abort at first err\n"
237 "\t--dry-run don't write anything\n");
241 * Print debug information. This is controlled by the value of the
242 * global variable 'debug'
244 void lr_debug(int level, const char *fmt, ...)
253 vfprintf(debug_log, fmt, ap);
255 vfprintf(stdout, fmt, ap);
259 void *lr_grow_buf(void *buf, int size)
263 ptr = realloc(buf, size);
269 /* Use rsync to replicate file data */
270 int lr_rsync_data(struct lr_info *info)
272 struct stat st_src, st_dest;
275 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
277 rc = stat(info->src, &st_src);
279 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
280 info->src, info->name);
286 rc = stat(info->dest, &st_dest);
288 fprintf(stderr, "Error: Unable to stat dest=%s\n",
293 if (st_src.st_mtime != st_dest.st_mtime ||
294 st_src.st_size != st_dest.st_size) {
296 * XXX spawning off an rsync for every data sync and
297 * waiting synchronously is bad for performance.
298 * librsync could possibly used here. But it does not
299 * seem to be of production grade. Multi-threaded
300 * replication is also to be considered.
310 extern char **environ;
313 lr_debug(DTRACE, "\t%s %s %s %s %s %s\n", args[0], args[1],
314 args[2], args[3], args[4], info->tfid);
316 status = callvpe(rsync, args, environ);
319 } else if (WIFEXITED(status)) {
320 status = WEXITSTATUS(status);
323 else if (status == 23 || status == 24)
324 /* Error due to vanished source files;
331 lr_debug(DINFO, "rsync %s exited with %d %d\n",
332 info->src, status, rc);
337 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
338 info->dest, info->tfid);
344 int lr_copy_data(struct lr_info *info)
354 fd_src = open(info->src, O_RDONLY);
357 if (fstat(fd_src, &st_src) == -1 || stat(info->dest, &st_dest) == -1)
360 if (st_src.st_mtime == st_dest.st_mtime &&
361 st_src.st_size == st_dest.st_size)
364 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
366 * It is more efficient to use rsync to replicate
367 * large files. Any file larger than rsync_threshold
368 * is handed off to rsync.
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);
389 info->bufsize = bufsize;
397 rsize = read(fd_src, buf, bufsize);
407 wsize = write(fd_dest, buf, rsize);
427 /* Copy data from source to destination */
428 int lr_sync_data(struct lr_info *info)
431 return lr_rsync_data(info);
433 return lr_copy_data(info);
436 /* Copy all attributes from file src to file dest */
437 int lr_copy_attr(const char *src, const char *dest)
442 if (stat(src, &st) == -1 || chmod(dest, st.st_mode) == -1 ||
443 chown(dest, st.st_uid, st.st_gid) == -1)
446 time.actime = st.st_atime;
447 time.modtime = st.st_mtime;
448 if (utime(dest, &time) == -1)
453 /* Copy all xattrs from file info->src to info->dest */
454 int lr_copy_xattr(struct lr_info *info)
456 size_t size = info->xsize;
465 rc = llistxattr(info->src, info->xlist, size);
466 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
467 info->src, info->xlist, rc, errno);
468 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
469 size = rc > PATH_MAX ? rc : PATH_MAX;
470 info->xlist = lr_grow_buf(info->xlist, size);
474 rc = llistxattr(info->src, info->xlist, size);
475 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
476 info->src, rc, errno);
483 while (start < len) {
485 rc = lgetxattr(info->src, info->xlist + start,
487 if (!info->xvalue || errno == ERANGE) {
488 size = rc > PATH_MAX ? rc : PATH_MAX;
489 info->xvalue = lr_grow_buf(info->xvalue, size);
493 rc = lgetxattr(info->src, info->xlist + start,
496 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
500 rc = lsetxattr(info->dest, info->xlist + start,
501 info->xvalue, size, 0);
502 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
505 if (errno != ENOTSUP) {
506 fprintf(stderr, "cannot replicate xattrs from '%s' to '%s': %s\n",
507 info->src, info->dest,
514 start += strlen(info->xlist + start) + 1;
517 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
523 * Retrieve the filesystem path for a given FID and a given
524 * 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));
542 * Retrieve the filesystem path for a given FID. The path is returned
545 int lr_get_path(struct lr_info *info, char *fidstr)
547 return lr_get_path_ln(info, fidstr, 0);
550 /* Generate the path for opening by FID */
551 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
553 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
554 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
558 /* Read the symlink information into 'info->link' */
559 int lr_get_symlink(struct lr_info *info)
564 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
565 rc = readlink(info->src, info->linktmp, PATH_MAX);
567 info->linktmp[rc] = '\0';
570 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
572 if (strncmp(info->linktmp, status->ls_source,
573 strlen(status->ls_source)) == 0) {
574 /* Strip source fs path and replace with target fs path. */
575 link = info->linktmp + strlen(status->ls_source);
576 snprintf(info->src, sizeof(info->src), "%s%s",
577 status->ls_targets[info->target_no], link);
580 link = info->linktmp;
582 rc = snprintf(info->link, sizeof(info->link), "%s", link);
583 if (rc >= sizeof(info->link))
588 /* Create file/directory/device file/symlink. */
589 int lr_mkfile(struct lr_info *info)
595 lr_debug(DINFO, "mkfile(%d) %s\n", info->type, info->dest);
596 if (info->type == CL_MKDIR) {
597 rc = mkdir(info->dest, 0777);
598 } else if (info->type == CL_SOFTLINK) {
599 lr_get_symlink(info);
600 rc = symlink(info->link, info->dest);
601 } else if (info->type == CL_MKNOD) {
602 lr_get_FID_PATH(status->ls_source, info->tfid,
603 info->src, PATH_MAX);
604 rc = stat(info->src, &st);
611 rc = mknod(info->dest, st.st_mode, st.st_rdev);
613 rc = mknod(info->dest, S_IFREG | 0777, 0);
623 /* Sync data and attributes */
624 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
625 lr_debug(DTRACE, "Syncing data and attributes %s\n",
627 (void)lr_copy_xattr(info);
628 if (info->type == CL_CREATE)
629 rc = lr_sync_data(info);
631 rc = lr_copy_attr(info->src, info->dest);
634 /* Source file has disappeared. Not an error. */
637 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
644 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
646 struct lr_parent_child_list *p;
649 p = calloc(1, sizeof(*p));
652 len = snprintf(p->pc_log.pcl_pfid, sizeof(p->pc_log.pcl_pfid),
654 if (len >= sizeof(p->pc_log.pcl_pfid))
656 len = snprintf(p->pc_log.pcl_tfid, sizeof(p->pc_log.pcl_tfid),
658 if (len >= sizeof(p->pc_log.pcl_tfid))
660 len = snprintf(p->pc_log.pcl_name, sizeof(p->pc_log.pcl_name),
662 if (len >= sizeof(p->pc_log.pcl_name))
665 p->pc_next = parents;
674 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
676 struct lr_parent_child_list *curr, *prev;
677 char d[4 * PATH_MAX + 1];
680 prev = curr = parents;
682 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
683 if (snprintf(d, sizeof(d), "%s/%s", dest,
684 curr->pc_log.pcl_name) >= sizeof(d)) {
685 fprintf(stderr, "Buffer truncated\n");
688 if (snprintf(info->src, sizeof(info->src), "%s/%s/%s",
689 status->ls_targets[info->target_no],
690 SPECIAL_DIR, curr->pc_log.pcl_tfid) >=
693 rc = rename(info->src, d);
695 fprintf(stderr, "Error renaming file %s to %s: %d\n",
696 info->src, d, errno);
700 parents = curr->pc_next;
702 prev->pc_next = curr->pc_next;
703 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
705 prev = curr = parents;
708 curr = curr->pc_next;
713 /* remove [info->spfid, info->sfid] from parents */
714 int lr_remove_pc(const char *pfid, const char *tfid)
716 struct lr_parent_child_list *curr, *prev;
718 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
719 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
720 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
722 parents = curr->pc_next;
724 prev->pc_next = curr->pc_next;
732 /* Create file under SPECIAL_DIR with its tfid as its name. */
733 int lr_mk_special(struct lr_info *info)
737 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
738 status->ls_targets[info->target_no], SPECIAL_DIR, info->tfid);
740 rc = lr_mkfile(info);
744 rc = lr_add_pc(info->pfid, info->tfid, info->name);
748 /* Remove a file or directory */
749 int lr_rmfile(struct lr_info *info)
753 if (info->type == CL_RMDIR)
754 rc = rmdir(info->dest);
756 rc = unlink(info->dest);
762 /* Recursively remove directory and its contents */
763 int lr_rm_recursive(struct lr_info *info)
772 extern char **environ;
776 status = callvpe("/bin/rm", args, environ);
779 else if (WIFEXITED(status))
780 rc = WEXITSTATUS(status) == 0 ? 0 : -EINVAL;
787 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
788 int lr_rm_special(struct lr_info *info)
792 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
793 status->ls_targets[info->target_no], SPECIAL_DIR, info->tfid);
794 rc = lr_rmfile(info);
797 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
798 info->dest, rc, errno);
802 /* Replicate file and directory create events */
803 int lr_create(struct lr_info *info)
810 /* Is target FID present on the source? */
811 rc = lr_get_path(info, info->tfid);
813 /* Source file has disappeared. Not an error. */
814 lr_debug(DINFO, "create: tfid %s not found on source-fs\n",
820 strcpy(info->savedpath, info->path);
822 /* Is parent FID present on the source */
823 rc = lr_get_path(info, info->pfid);
825 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
832 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
833 len = strlen(info->path);
834 if (len == 1 && info->path[0] == '/')
835 snprintf(info->dest, sizeof(info->dest), "%s", info->name);
836 else if (len - 1 > 0 && info->path[len - 1] == '/')
837 snprintf(info->dest, sizeof(info->dest), "%s%s", info->path,
840 snprintf(info->dest, sizeof(info->dest), "%s/%s", info->path,
843 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
845 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
846 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
847 info->tfid, info->dest, info->savedpath);
851 /* Is f2p(pfid) present on the target? If not, the parent has moved */
853 snprintf(info->dest, sizeof(info->dest), "%s/%s",
854 status->ls_targets[0], info->path);
855 if (access(info->dest, F_OK) != 0) {
856 lr_debug(DTRACE, "create: parent %s not found\n",
861 for (info->target_no = 0; info->target_no < status->ls_num_targets;
863 snprintf(info->dest, sizeof(info->dest), "%s/%s",
864 status->ls_targets[info->target_no], info->savedpath);
865 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
869 rc1 = lr_mkfile(info);
870 if (mkspecial || rc1 == -ENOENT)
871 rc1 = lr_mk_special(info);
878 /* Replicate a file remove (rmdir/unlink) operation */
879 int lr_remove(struct lr_info *info)
884 for (info->target_no = 0; info->target_no < status->ls_num_targets;
886 rc1 = lr_rm_special(info);
890 rc1 = lr_get_path(info, info->pfid);
891 if (rc1 == -ENOENT) {
892 lr_debug(DINFO, "remove: pfid %s not found\n",
900 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
901 status->ls_targets[info->target_no], info->path,
904 rc1 = lr_rmfile(info);
905 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
906 info->dest, rc1, errno);
907 if (rc1 == -ENOTEMPTY)
908 rc1 = lr_rm_recursive(info);
918 /* Replicate a rename/move operation. */
919 int lr_move(struct lr_info *info)
925 int special_dest = 0;
926 char srcpath[PATH_MAX + 1] = "";
928 assert(info->is_extended);
930 rc_src = lr_get_path(info, info->spfid);
931 if (rc_src < 0 && rc_src != -ENOENT)
933 memcpy(srcpath, info->path, strlen(info->path));
935 rc_dest = lr_get_path(info, info->pfid);
936 if (rc_dest < 0 && rc_dest != -ENOENT)
939 for (info->target_no = 0; info->target_no < status->ls_num_targets;
942 snprintf(info->dest, sizeof(info->dest), "%s/%s",
943 status->ls_targets[info->target_no],
945 if (access(info->dest, F_OK) != 0) {
948 snprintf(info->dest, sizeof(info->dest),
950 status->ls_targets[info->target_no],
951 info->path, info->name);
953 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
956 if (rc_dest == -ENOENT) {
957 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
958 status->ls_targets[info->target_no],
959 SPECIAL_DIR, info->sfid);
961 lr_debug(DINFO, "special dest %s\n", info->dest);
965 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
966 status->ls_targets[info->target_no],
967 srcpath, info->sname);
968 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
971 if (rc_src == -ENOENT ||
972 (access(info->src, F_OK) != 0 && errno == ENOENT)) {
973 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
974 status->ls_targets[info->target_no],
975 SPECIAL_DIR, info->sfid);
977 lr_debug(DINFO, "special src %s\n", info->src);
982 if (strcmp(info->src, info->dest) != 0) {
983 rc1 = rename(info->src, info->dest);
986 lr_debug(DINFO, "rename returns %d\n", rc1);
990 rc1 = lr_remove_pc(info->spfid, info->sfid);
993 lr_cascade_move(info->sfid, info->dest, info);
995 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
997 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
998 info->src, info->dest, rc1, errno);
1005 /* Replicate a hard link */
1006 int lr_link(struct lr_info *info)
1013 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1014 rc = stat(info->src, &st);
1018 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1019 info->target_no++) {
1026 * The changelog record has the new parent directory FID and
1027 * name of the target file. So info->dest can be constructed
1028 * by getting the path of the new parent directory and
1029 * appending the target file name.
1031 rc1 = lr_get_path(info, info->pfid);
1032 lr_debug(rc1 ? 0 : DTRACE, "\tparent fid2path %s, %s, rc=%d\n",
1033 info->path, info->name, rc1);
1036 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1037 status->ls_targets[info->target_no],
1038 info->path, info->name);
1039 lr_debug(DINFO, "link destination is %s\n", info->dest);
1042 /* Search through the hardlinks to get the src */
1043 for (i = 0; i < st.st_nlink && info->src[0] == 0; i++) {
1046 rc1 = lr_get_path_ln(info, info->tfid, i);
1047 lr_debug(rc1 ? 0 : DTRACE,
1048 "\tfid2path %s, %s, %d rc=%d\n", info->path,
1049 info->name, i, rc1);
1054 * Compare the path of target FID with info->dest
1055 * to find out info->src.
1057 len = sizeof(status->ls_targets[info->target_no]) +
1059 char srcpath[len + 1];
1061 snprintf(srcpath, sizeof(srcpath), "%s/%s",
1062 status->ls_targets[info->target_no],
1065 if (strcmp(srcpath, info->dest) != 0) {
1066 snprintf(info->src, sizeof(info->src), "%s",
1068 lr_debug(DINFO, "link source is %s\n",
1078 if (info->src[0] == 0)
1079 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
1080 status->ls_targets[info->target_no],
1081 SPECIAL_DIR, info->tfid);
1082 else if (info->dest[0] == 0)
1083 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1084 status->ls_targets[info->target_no],
1085 SPECIAL_DIR, info->tfid);
1087 rc1 = link(info->src, info->dest);
1088 lr_debug(DINFO, "link: %s [to] %s; rc1=%d %s\n",
1089 info->src, info->dest, rc1,
1090 strerror(rc1 ? errno : 0));
1098 int lr_set_dest_for_attr(struct lr_info *info)
1102 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1103 status->ls_targets[info->target_no], info->path);
1104 rc = access(info->dest, F_OK);
1111 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1112 status->ls_targets[info->target_no], SPECIAL_DIR,
1115 rc = access(info->dest, F_OK);
1122 /* Replicate file attributes */
1123 int lr_setattr(struct lr_info *info)
1128 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1130 rc = lr_get_path(info, info->tfid);
1132 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1137 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1138 info->target_no++) {
1139 rc = lr_set_dest_for_attr(info);
1143 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1146 rc1 = lr_sync_data(info);
1148 rc1 = lr_copy_attr(info->src, info->dest);
1155 /* Replicate xattrs */
1156 int lr_setxattr(struct lr_info *info)
1160 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1162 rc = lr_get_path(info, info->tfid);
1164 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1169 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1170 info->target_no++) {
1171 rc = lr_set_dest_for_attr(info);
1175 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1178 rc1 = lr_copy_xattr(info);
1186 /* Parse a line of changelog entry */
1187 int lr_parse_line(void *priv, struct lr_info *info)
1189 struct changelog_rec *rec;
1190 struct changelog_ext_rename *rnm;
1192 size_t copylen = sizeof(info->name);
1194 if (llapi_changelog_recv(priv, &rec) != 0)
1197 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1198 info->recno = rec->cr_index;
1199 info->type = rec->cr_type;
1200 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1201 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1203 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1204 if (copylen > namelen + 1)
1205 copylen = namelen + 1;
1206 snprintf(info->name, copylen, "%s", changelog_rec_name(rec));
1208 /* Don't use rnm if CLF_RENAME isn't set */
1209 rnm = changelog_rec_rename(rec);
1210 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1211 copylen = sizeof(info->sname);
1213 snprintf(info->sfid, sizeof(info->sfid), DFID,
1214 PFID(&rnm->cr_sfid));
1215 snprintf(info->spfid, sizeof(info->spfid), DFID,
1216 PFID(&rnm->cr_spfid));
1217 namelen = changelog_rec_snamelen(rec);
1218 if (copylen > namelen + 1)
1219 copylen = namelen + 1;
1220 snprintf(info->sname, copylen, "%s", changelog_rec_sname(rec));
1223 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1224 info->name, info->sname);
1227 printf("Rec %lld: %d %s\n", info->recno, info->type,
1231 llapi_changelog_free(&rec);
1237 /* Initialize the replication parameters */
1238 int lr_init_status(void)
1240 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1244 status = calloc(size, 1);
1247 status->ls_version = REPLICATE_STATUS_VER;
1248 status->ls_size = size;
1249 status->ls_last_recno = -1;
1253 /* Make a backup of the statuslog */
1254 void lr_backup_log(void)
1256 char backupfile[PATH_MAX];
1260 snprintf(backupfile, sizeof(backupfile), "%s.old", statuslog);
1261 (void) rename(statuslog, backupfile);
1265 /* Save replication parameters to a statuslog. */
1266 int lr_write_log(void)
1270 size_t write_size = status->ls_size;
1271 struct lr_parent_child_list *curr;
1279 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
1281 fprintf(stderr, "Error opening log file for writing (%s)\n",
1286 size = write(fd, status, write_size);
1287 if (size != write_size) {
1288 fprintf(stderr, "Error writing to log file (%s) %d\n",
1294 for (curr = parents; curr; curr = curr->pc_next) {
1295 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1296 if (size != sizeof(curr->pc_log)) {
1297 fprintf(stderr, "Error writing to log file (%s) %d\n",
1308 * Read statuslog and populate the replication parameters. Command
1309 * line parameters take precedence over parameters in the log file.
1311 int lr_read_log(void)
1313 struct lr_parent_child_list *tmp;
1314 struct lr_parent_child_log rec;
1315 struct lustre_rsync_status *s;
1318 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1324 s = calloc(1, read_size);
1330 fd = open(statuslog, O_RDONLY);
1336 size = read(fd, s, read_size);
1337 if (size != read_size) {
1342 if (read_size < s->ls_size) {
1343 read_size = s->ls_size;
1344 s = lr_grow_buf(s, read_size);
1350 if (lseek(fd, 0, SEEK_SET) == -1) {
1355 size = read(fd, s, read_size);
1356 if (size != read_size) {
1362 while (read(fd, &rec, sizeof(rec)) != 0) {
1363 tmp = calloc(1, sizeof(*tmp));
1370 tmp->pc_next = parents;
1374 /* copy uninitialized fields to status */
1375 if (status->ls_num_targets == 0) {
1376 if (status->ls_size != s->ls_size) {
1377 status = lr_grow_buf(status, s->ls_size);
1383 status->ls_size = s->ls_size;
1385 status->ls_num_targets = s->ls_num_targets;
1386 memcpy(status->ls_targets, s->ls_targets,
1387 (PATH_MAX + 1) * s->ls_num_targets);
1389 if (status->ls_last_recno == -1)
1390 status->ls_last_recno = s->ls_last_recno;
1392 if (status->ls_registration[0] == '\0')
1393 snprintf(status->ls_registration,
1394 sizeof(status->ls_registration), "%s",
1395 s->ls_registration);
1397 if (status->ls_mdt_device[0] == '\0')
1398 snprintf(status->ls_mdt_device,
1399 sizeof(status->ls_mdt_device), "%s",
1402 if (status->ls_source_fs[0] == '\0')
1403 snprintf(status->ls_source_fs,
1404 sizeof(status->ls_source_fs), "%s",
1407 if (status->ls_source[0] == '\0')
1408 snprintf(status->ls_source,
1409 sizeof(status->ls_source), "%s",
1421 * Clear changelogs every CLEAR_INTERVAL records or at the end of
1424 int lr_clear_cl(struct lr_info *info, int force)
1426 char mdt_device[LR_NAME_MAXLEN + 1];
1429 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1430 if (!noclear && !dryrun) {
1432 * llapi_changelog_clear modifies the mdt
1433 * device name so make a copy of it until this
1436 snprintf(mdt_device, sizeof(mdt_device), "%s",
1437 status->ls_mdt_device);
1438 rc = llapi_changelog_clear(mdt_device,
1439 status->ls_registration,
1442 printf("Changelog clear (%s, %s, %lld) returned %d\n",
1443 status->ls_mdt_device,
1444 status->ls_registration, info->recno,
1448 if (!rc && !dryrun) {
1449 status->ls_last_recno = info->recno;
1458 * Locate a usable version of rsync. At this point we'll use any
1461 int lr_locate_rsync(void)
1467 snprintf(rsync, sizeof(rsync), "%s -p %s", TYPE, RSYNC);
1468 fp = popen(rsync, "r");
1472 if (fgets(rsync, sizeof(rsync), fp) == NULL) {
1477 len = strlen(rsync);
1478 if (len > 0 && rsync[len - 1] == '\n')
1479 rsync[len - 1] = '\0';
1482 /* Determine the version of rsync */
1483 snprintf(rsync_ver, sizeof(rsync_ver), "%s --version", rsync);
1484 fp = popen(rsync_ver, "r");
1488 if (fgets(rsync_ver, sizeof(rsync_ver), fp) == NULL) {
1492 len = strlen(rsync_ver);
1493 if (len > 0 && rsync_ver[len - 1] == '\n')
1494 rsync_ver[len - 1] = '\0';
1500 /* Print the replication parameters */
1501 void lr_print_status(struct lr_info *info)
1508 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1509 printf("MDT device: %s\n", status->ls_mdt_device);
1510 printf("Source: %s\n", status->ls_source);
1511 for (i = 0; i < status->ls_num_targets; i++)
1512 printf("Target: %s\n", status->ls_targets[i]);
1514 printf("Statuslog: %s\n", statuslog);
1515 printf("Changelog registration: %s\n", status->ls_registration);
1516 printf("Starting changelog record: %jd\n",
1517 (uintmax_t)status->ls_last_recno);
1519 printf("Replicate xattrs: no\n");
1521 printf("Clear changelog after use: no\n");
1523 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1526 void lr_print_failure(struct lr_info *info, int rc)
1529 "Replication of operation failed(%d): %lld %s (%d) %s %s %s\n",
1530 rc, info->recno, changelog_type2str(info->type), info->type,
1531 info->tfid, info->pfid, info->name);
1534 /* Replicate filesystem operations from src_path to target_path */
1535 int lr_replicate(void)
1537 void *changelog_priv;
1538 struct lr_info *info;
1539 struct lr_info *ext = NULL;
1547 info = calloc(1, sizeof(struct lr_info));
1551 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1553 fprintf(stderr, "Source path is not a valid Lustre client mountpoint.\n");
1557 if (status->ls_mdt_device[0] == '\0') {
1560 len = snprintf(status->ls_mdt_device,
1561 sizeof(status->ls_mdt_device), "%s%s",
1562 status->ls_source_fs, DEFAULT_MDT);
1563 if (len >= sizeof(status->ls_mdt_device)) {
1569 ext = calloc(1, sizeof(struct lr_info));
1575 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1576 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1577 status->ls_targets[i], SPECIAL_DIR);
1578 rc = mkdir(info->dest, 0777);
1579 if (rc == -1 && errno != EEXIST) {
1580 fprintf(stderr, "Error writing to target path %s.\n",
1581 status->ls_targets[i]);
1585 rc = llistxattr(info->src, info->xlist, info->xsize);
1586 if (rc == -1 && errno == ENOTSUP) {
1587 fprintf(stderr, "xattrs not supported on %s\n",
1588 status->ls_targets[i]);
1592 if (xattr_not_supp == status->ls_num_targets)
1593 /* None of the targets support xattrs. */
1596 lr_print_status(info);
1598 /* Open changelogs for consumption*/
1599 rc = llapi_changelog_start(&changelog_priv,
1600 CHANGELOG_FLAG_BLOCK |
1601 CHANGELOG_FLAG_JOBID |
1602 CHANGELOG_FLAG_EXTRA_FLAGS,
1603 status->ls_mdt_device,
1604 status->ls_last_recno);
1606 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1607 status->ls_source_fs);
1611 rc = llapi_changelog_set_xflags(changelog_priv,
1612 CHANGELOG_EXTRA_FLAG_UIDGID |
1613 CHANGELOG_EXTRA_FLAG_NID |
1614 CHANGELOG_EXTRA_FLAG_OMODE |
1615 CHANGELOG_EXTRA_FLAG_XATTR);
1617 fprintf(stderr, "Error setting xflag in changelog for fs %s.\n",
1618 status->ls_source_fs);
1622 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1625 if (info->type == CL_RENAME && !info->is_extended) {
1627 * Newer rename operations extends changelog to store
1628 * source file information, but old changelog has
1631 if (lr_parse_line(changelog_priv, ext) != 0)
1633 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1634 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1635 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1636 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1637 snprintf(info->sname, sizeof(info->sname), "%s",
1639 snprintf(info->name, sizeof(info->name), "%s",
1641 info->is_extended = 1;
1642 info->recno = ext->recno; /* For lr_clear_cl(). */
1648 lr_debug(DTRACE, "***** Start %lld %s (%d) %s %s %s *****\n",
1649 info->recno, changelog_type2str(info->type),
1650 info->type, info->tfid, info->pfid, info->name);
1652 switch (info->type) {
1657 rc = lr_create(info);
1661 rc = lr_remove(info);
1671 rc = lr_setattr(info);
1674 rc = lr_setxattr(info);
1684 * Nothing needs to be done for these entries
1691 lr_debug(DTRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n",
1692 info->recno, changelog_type2str(info->type),
1693 info->type, info->tfid, info->pfid, info->name, rc);
1695 if (rc && rc != -ENOENT) {
1696 lr_print_failure(info, rc);
1701 lr_clear_cl(info, 0);
1704 llapi_changelog_fini(&changelog_priv);
1706 if (errors || verbose)
1707 printf("Errors: %d\n", errors);
1709 /* Clear changelog records used so far */
1710 lr_clear_cl(info, 1);
1713 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1714 printf("Changelog records consumed: %lld\n", rec_count);
1729 termination_handler (int signum)
1731 /* Set a flag for the replicator to gracefully shutdown */
1733 printf("lustre_rsync halting.\n");
1736 int main(int argc, char *argv[])
1742 if ((rc = lr_init_status()) != 0)
1745 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1746 long_opts, NULL)) >= 0) {
1749 /* Assume absolute paths */
1753 /* Assume absolute paths */
1754 snprintf(status->ls_source, sizeof(status->ls_source),
1758 status->ls_num_targets++;
1760 if (numtargets != status->ls_num_targets) {
1762 * Targets were read from a log file.
1763 * The ones specified on the command line
1764 * take precedence. The ones from the log
1765 * file will be ignored.
1767 status->ls_num_targets = numtargets;
1769 newsize = sizeof(struct lustre_rsync_status) +
1770 (status->ls_num_targets * (PATH_MAX + 1));
1771 if (status->ls_size != newsize) {
1772 status->ls_size = newsize;
1773 status = lr_grow_buf(status, newsize);
1777 snprintf(status->ls_targets[status->ls_num_targets - 1],
1778 sizeof(status->ls_targets[0]), "%s", optarg);
1781 snprintf(status->ls_mdt_device,
1782 sizeof(status->ls_mdt_device),
1786 snprintf(status->ls_registration,
1787 sizeof(status->ls_registration),
1792 (void)lr_read_log();
1798 if (strcmp("no", optarg) == 0) {
1800 } else if (strcmp("yes", optarg) != 0) {
1801 printf("Invalid parameter %s. Specify --xattr=no or --xattr=yes\n",
1810 /* Undocumented option cl-clear */
1811 if (strcmp("no", optarg) == 0) {
1813 } else if (strcmp("yes", optarg) != 0) {
1814 printf("Invalid parameter %s. Specify --cl-clear=no or --cl-clear=yes\n",
1820 /* Undocumented option use-rsync */
1824 /* Undocumented option rsync-threshold */
1825 rsync_threshold = atol(optarg);
1828 /* Undocumented option start-recno */
1829 status->ls_last_recno = atol(optarg);
1832 /* Undocumented option debug */
1833 debug = atoi(optarg);
1834 if (debug < 0 || debug > 2)
1838 /* Undocumented option debug log file */
1841 debug_log = fopen(optarg, "a");
1843 printf("Cannot open %s for debug log\n",
1849 fprintf(stderr, "error: %s: option '%s' unrecognized.\n",
1850 argv[0], argv[optind - 1]);
1856 if (status->ls_last_recno == -1)
1857 status->ls_last_recno = 0;
1858 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1859 /* No registration ID was passed in. */
1860 printf("Please specify changelog consumer registration id.\n");
1864 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1865 fprintf(stderr, "Please specify the source path.\n");
1869 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1870 fprintf(stderr, "Please specify the target path.\n");
1875 rc = lr_locate_rsync();
1876 if (use_rsync && rc != 0) {
1877 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1881 signal(SIGINT, termination_handler);
1882 signal(SIGHUP, termination_handler);
1883 signal(SIGTERM, termination_handler);
1885 rc = lr_replicate();