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/
31 * lustre/utils/lustre_rsync.c
33 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
34 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
38 * - lustre_rsync is a tool for replicating a lustre filesystem.
40 * - The source-fs is a live lustre filesystem. It is not a
41 * snapshot. It is mounted and undergoing changes
43 * - The target-fs is a copy of the source-fs from the past. Let's
44 * call this point, the 'sync point'.
46 * - There is a changelog of all metadata operations that happened on
47 * the filesystem since the 'sync point'.
49 * - lustre_rsync replicates all the operations saved in the changelog
50 * on to the target filesystem to make it identical to the source.
52 * To facilitate replication, the lustre filesystem provides
53 * a) a way to get the current filesystem path of a given FID
54 * b) a way to open files by specifying its FID
56 * The changelog only has a limited amount of information.
57 * tfid - The FID of the target file
58 * pfid - The FID of the parent of the target file (at the time of
60 * sfid - The FID of the source file
61 * spfid - The FID of the parent of the source file
62 * name - The name of the target file (at the time of the operation), the name
63 * of the source file is appended (delimited with '\0') if this
64 * operation involves a source
66 * With just this information, it is not alwasy possible to determine
67 * the file paths for each operation. For instance, if pfid does not
68 * exist on the source-fs (due to a subsequent deletion), its path
69 * cannot be queried. In such cases, lustre_rsync keeps the files in a
70 * special directory ("/.lustrerepl"). Once all the operations in a
71 * changelog are replayed, all the files in this special directory
72 * will get moved to the location as in the source-fs.
74 * Shorthand used: f2p(fid) = fid2path(fid)
76 * The following are the metadata operations of interest.
78 * If tfid is absent on the source-fs, ignore this operation
79 * If pfid is absent on the source-fs [or]
80 * if f2p(pfid) is not present on target-fs [or]
81 * if f2p(pfid)+name != f2p(tfid)
82 * creat .lustrerepl/tfid
83 * track [pfid,tfid,name]
88 * If .lustrerepl/[tfid] is present on the target
89 * rm .lustrerepl/[tfid]
90 * Else if pfid is present on the source-fs,
91 * if f2p(pfid)+name is present,
94 * 3. move (spfid,sname) to (pfid,name)
96 * if spfid is also present, mv (spfid,sname) to (pfid,name)
97 * else mv .lustrerepl/[sfid] to (pfid,name)
98 * Else if pfid is not present,
99 * if spfid is present, mv (spfid,sname) .lustrerepl/[sfid]
100 * If moving out of .lustrerepl
101 * move out all its children in .lustrerepl.
102 * [pfid,tfid,name] tracked from (1) is used for this.
114 #include <sys/stat.h>
115 #include <sys/types.h>
120 #include <sys/xattr.h>
121 #include <linux/types.h>
123 #include <libcfs/util/string.h>
124 #include <lustre/lustreapi.h>
125 #include "lustre_rsync.h"
128 #define REPLICATE_STATUS_VER 1
129 #define CLEAR_INTERVAL 100
130 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
132 #define TYPE_STR_LEN 16
134 #define DEFAULT_MDT "-MDT0000"
135 #define SPECIAL_DIR ".lustrerepl"
136 #define RSYNC "rsync"
144 * Information for processing a changelog record. This structure is
145 * allocated on the heap instead of allocating large variables on the
151 unsigned int is_extended:1;
152 enum changelog_rec_type type;
153 char tfid[LR_FID_STR_LEN];
154 char pfid[LR_FID_STR_LEN];
155 char sfid[LR_FID_STR_LEN];
156 char spfid[LR_FID_STR_LEN];
157 char sname[NAME_MAX + 1];
158 char name[NAME_MAX + 1];
159 char src[3 * PATH_MAX + 1];
160 char dest[3 * PATH_MAX + 1];
161 char path[PATH_MAX + 1];
162 char savedpath[PATH_MAX + 1];
163 char link[PATH_MAX + 1];
164 char linktmp[PATH_MAX + 1];
168 /* Variables for querying the xattributes */
175 struct lr_parent_child_list {
176 struct lr_parent_child_log pc_log;
177 struct lr_parent_child_list *pc_next;
180 struct lustre_rsync_status *status;
181 char *statuslog; /* Name of the status log file */
183 int noxattr; /* Flag to turn off replicating xattrs */
184 int noclear; /* Flag to turn off clearing changelogs */
185 int debug; /* Flag to turn debugging information on and off */
186 int verbose; /* Verbose output */
187 long long rec_count; /* No of changelog records that were processed */
190 int use_rsync; /* Flag to turn on use of rsync to copy data */
191 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
192 int quit; /* Flag to stop processing the changelog; set on the
193 * receipt of a signal
197 char rsync[PATH_MAX + 128];
198 char rsync_ver[PATH_MAX * 2];
199 struct lr_parent_child_list *parents;
203 /* Command line options */
204 struct option long_opts[] = {
205 { .val = 'l', .name = "statuslog", .has_arg = required_argument },
206 { .val = 'm', .name = "mdt", .has_arg = required_argument },
207 { .val = 's', .name = "source", .has_arg = required_argument },
208 { .val = 't', .name = "target", .has_arg = required_argument },
209 { .val = 'u', .name = "user", .has_arg = required_argument },
210 { .val = 'v', .name = "verbose", .has_arg = no_argument },
211 { .val = 'x', .name = "xattr", .has_arg = required_argument },
212 { .val = 'z', .name = "dry-run", .has_arg = no_argument },
213 /* Undocumented options follow */
214 { .val = 'a', .name = "abort-on-err", .has_arg = no_argument },
215 { .val = 'c', .name = "cl-clear", .has_arg = required_argument },
216 { .val = 'd', .name = "debug", .has_arg = required_argument },
217 { .val = 'D', .name = "debuglog", .has_arg = required_argument },
218 { .val = 'n', .name = "start-recno", .has_arg = required_argument },
219 { .val = 'r', .name = "use-rsync", .has_arg = no_argument },
220 { .val = 'y', .name = "rsync-threshold",
221 .has_arg = required_argument },
224 /* Command line usage */
227 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
228 "-m <mdt> -r <user id> -l <status log>\n"
229 "lustre_rsync can also pick up parameters from a "
230 "status log created earlier.\n"
231 "\tlustre_rsync -l <log_file>\n"
233 "\t--xattr <yes|no> replicate EAs\n"
234 "\t--abort-on-err abort at first err\n"
236 "\t--dry-run don't write anything\n");
240 * Print debug information. This is controlled by the value of the
241 * global variable 'debug'
243 void lr_debug(int level, const char *fmt, ...)
252 vfprintf(debug_log, fmt, ap);
254 vfprintf(stdout, fmt, ap);
258 void *lr_grow_buf(void *buf, int size)
262 ptr = realloc(buf, size);
268 /* Use rsync to replicate file data */
269 int lr_rsync_data(struct lr_info *info)
271 struct stat st_src, st_dest;
274 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
276 rc = stat(info->src, &st_src);
278 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
279 info->src, info->name);
285 rc = stat(info->dest, &st_dest);
287 fprintf(stderr, "Error: Unable to stat dest=%s\n",
292 if (st_src.st_mtime != st_dest.st_mtime ||
293 st_src.st_size != st_dest.st_size) {
295 * XXX spawning off an rsync for every data sync and
296 * waiting synchronously is bad for performance.
297 * librsync could possibly used here. But it does not
298 * seem to be of production grade. Multi-threaded
299 * replication is also to be considered.
309 extern char **environ;
312 lr_debug(DTRACE, "\t%s %s %s %s %s %s\n", args[0], args[1],
313 args[2], args[3], args[4], info->tfid);
315 status = callvpe(rsync, args, environ);
318 } else if (WIFEXITED(status)) {
319 status = WEXITSTATUS(status);
322 else if (status == 23 || status == 24)
323 /* Error due to vanished source files;
330 lr_debug(DINFO, "rsync %s exited with %d %d\n",
331 info->src, status, rc);
336 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
337 info->dest, info->tfid);
343 int lr_copy_data(struct lr_info *info)
353 fd_src = open(info->src, O_RDONLY);
356 if (fstat(fd_src, &st_src) == -1 || stat(info->dest, &st_dest) == -1)
359 if (st_src.st_mtime == st_dest.st_mtime &&
360 st_src.st_size == st_dest.st_size)
363 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
365 * It is more efficient to use rsync to replicate
366 * large files. Any file larger than rsync_threshold
367 * is handed off to rsync.
369 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
370 rc = lr_rsync_data(info);
374 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
379 bufsize = st_dest.st_blksize;
381 if (info->bufsize < bufsize) {
383 info->buf = lr_grow_buf(info->buf, bufsize);
388 info->bufsize = bufsize;
396 rsize = read(fd_src, buf, bufsize);
406 wsize = write(fd_dest, buf, rsize);
426 /* Copy data from source to destination */
427 int lr_sync_data(struct lr_info *info)
430 return lr_rsync_data(info);
432 return lr_copy_data(info);
435 /* Copy all attributes from file src to file dest */
436 int lr_copy_attr(const char *src, const char *dest)
441 if (stat(src, &st) == -1 || chmod(dest, st.st_mode) == -1 ||
442 chown(dest, st.st_uid, st.st_gid) == -1)
445 time.actime = st.st_atime;
446 time.modtime = st.st_mtime;
447 if (utime(dest, &time) == -1)
452 /* Copy all xattrs from file info->src to info->dest */
453 int lr_copy_xattr(struct lr_info *info)
455 size_t size = info->xsize;
464 rc = llistxattr(info->src, info->xlist, size);
465 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
466 info->src, info->xlist, rc, errno);
467 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
468 size = rc > PATH_MAX ? rc : PATH_MAX;
469 info->xlist = lr_grow_buf(info->xlist, size);
473 rc = llistxattr(info->src, info->xlist, size);
474 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
475 info->src, rc, errno);
482 while (start < len) {
484 rc = lgetxattr(info->src, info->xlist + start,
487 lr_debug(DTRACE, "\t(%s,0) rc=%d, errno=%d\n",
488 info->xlist + start, rc, errno);
489 start += strlen(info->xlist + start) + 1;
494 * XATTR_SIZE_MAX should be the upper limit, but
497 size = rc > XATTR_SIZE_MAX ? rc : XATTR_SIZE_MAX;
498 info->xvalue = lr_grow_buf(info->xvalue, size);
504 rc = lgetxattr(info->src, info->xlist + start,
506 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
510 rc = lsetxattr(info->dest, info->xlist + start,
511 info->xvalue, size, 0);
512 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
515 if (errno != ENOTSUP) {
516 fprintf(stderr, "cannot replicate xattrs from '%s' to '%s': %s\n",
517 info->src, info->dest,
524 start += strlen(info->xlist + start) + 1;
527 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
533 * Retrieve the filesystem path for a given FID and a given
534 * linkno. The path is returned in info->path
536 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
538 long long recno = -1;
541 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
542 PATH_MAX, &recno, &linkno);
543 if (rc < 0 && rc != -ENOENT) {
544 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
545 status->ls_source, fidstr, -rc, strerror(errno = -rc));
552 * Retrieve the filesystem path for a given FID. The path is returned
555 int lr_get_path(struct lr_info *info, char *fidstr)
557 return lr_get_path_ln(info, fidstr, 0);
560 /* Generate the path for opening by FID */
561 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
563 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
564 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
568 /* Read the symlink information into 'info->link' */
569 int lr_get_symlink(struct lr_info *info)
574 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
575 rc = readlink(info->src, info->linktmp, PATH_MAX);
577 info->linktmp[rc] = '\0';
580 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
582 if (strncmp(info->linktmp, status->ls_source,
583 strlen(status->ls_source)) == 0) {
584 /* Strip source fs path and replace with target fs path. */
585 link = info->linktmp + strlen(status->ls_source);
586 snprintf(info->src, sizeof(info->src), "%s%s",
587 status->ls_targets[info->target_no], link);
590 link = info->linktmp;
592 rc = snprintf(info->link, sizeof(info->link), "%s", link);
593 if (rc >= sizeof(info->link))
598 /* Create file/directory/device file/symlink. */
599 int lr_mkfile(struct lr_info *info)
605 lr_debug(DINFO, "mkfile(%d) %s\n", info->type, info->dest);
606 if (info->type == CL_MKDIR) {
607 rc = mkdir(info->dest, 0777);
608 } else if (info->type == CL_SOFTLINK) {
609 lr_get_symlink(info);
610 rc = symlink(info->link, info->dest);
611 } else if (info->type == CL_MKNOD) {
612 lr_get_FID_PATH(status->ls_source, info->tfid,
613 info->src, PATH_MAX);
614 rc = stat(info->src, &st);
621 rc = mknod(info->dest, st.st_mode, st.st_rdev);
623 rc = mknod(info->dest, S_IFREG | 0777, 0);
633 /* Sync data and attributes */
634 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
635 lr_debug(DTRACE, "Syncing data and attributes %s\n",
637 (void)lr_copy_xattr(info);
638 if (info->type == CL_CREATE)
639 rc = lr_sync_data(info);
641 rc = lr_copy_attr(info->src, info->dest);
644 /* Source file has disappeared. Not an error. */
647 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
654 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
656 struct lr_parent_child_list *p;
659 p = calloc(1, sizeof(*p));
662 len = snprintf(p->pc_log.pcl_pfid, sizeof(p->pc_log.pcl_pfid),
664 if (len >= sizeof(p->pc_log.pcl_pfid))
666 len = snprintf(p->pc_log.pcl_tfid, sizeof(p->pc_log.pcl_tfid),
668 if (len >= sizeof(p->pc_log.pcl_tfid))
670 len = snprintf(p->pc_log.pcl_name, sizeof(p->pc_log.pcl_name),
672 if (len >= sizeof(p->pc_log.pcl_name))
675 p->pc_next = parents;
684 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
686 struct lr_parent_child_list *curr, *prev;
687 char d[4 * PATH_MAX + 1];
690 prev = curr = parents;
692 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
693 if (snprintf(d, sizeof(d), "%s/%s", dest,
694 curr->pc_log.pcl_name) >= sizeof(d)) {
695 fprintf(stderr, "Buffer truncated\n");
698 if (snprintf(info->src, sizeof(info->src), "%s/%s/%s",
699 status->ls_targets[info->target_no],
700 SPECIAL_DIR, curr->pc_log.pcl_tfid) >=
703 rc = rename(info->src, d);
705 fprintf(stderr, "Error renaming file %s to %s: %d\n",
706 info->src, d, errno);
710 parents = curr->pc_next;
712 prev->pc_next = curr->pc_next;
713 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
715 prev = curr = parents;
718 curr = curr->pc_next;
723 /* remove [info->spfid, info->sfid] from parents */
724 int lr_remove_pc(const char *pfid, const char *tfid)
726 struct lr_parent_child_list *curr, *prev;
728 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
729 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
730 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
732 parents = curr->pc_next;
734 prev->pc_next = curr->pc_next;
742 /* Create file under SPECIAL_DIR with its tfid as its name. */
743 int lr_mk_special(struct lr_info *info)
747 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
748 status->ls_targets[info->target_no], SPECIAL_DIR, info->tfid);
750 rc = lr_mkfile(info);
754 rc = lr_add_pc(info->pfid, info->tfid, info->name);
758 /* Remove a file or directory */
759 int lr_rmfile(struct lr_info *info)
763 if (info->type == CL_RMDIR)
764 rc = rmdir(info->dest);
766 rc = unlink(info->dest);
772 /* Recursively remove directory and its contents */
773 int lr_rm_recursive(struct lr_info *info)
782 extern char **environ;
786 status = callvpe("/bin/rm", args, environ);
789 else if (WIFEXITED(status))
790 rc = WEXITSTATUS(status) == 0 ? 0 : -EINVAL;
797 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
798 int lr_rm_special(struct lr_info *info)
802 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
803 status->ls_targets[info->target_no], SPECIAL_DIR, info->tfid);
804 rc = lr_rmfile(info);
807 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
808 info->dest, rc, errno);
812 /* Replicate file and directory create events */
813 int lr_create(struct lr_info *info)
820 /* Is target FID present on the source? */
821 rc = lr_get_path(info, info->tfid);
823 /* Source file has disappeared. Not an error. */
824 lr_debug(DINFO, "create: tfid %s not found on source-fs\n",
830 strcpy(info->savedpath, info->path);
832 /* Is parent FID present on the source */
833 rc = lr_get_path(info, info->pfid);
835 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
842 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
843 len = strlen(info->path);
844 if (len == 1 && info->path[0] == '/')
845 snprintf(info->dest, sizeof(info->dest), "%s", info->name);
846 else if (len - 1 > 0 && info->path[len - 1] == '/')
847 snprintf(info->dest, sizeof(info->dest), "%s%s", info->path,
850 snprintf(info->dest, sizeof(info->dest), "%s/%s", info->path,
853 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
855 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
856 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
857 info->tfid, info->dest, info->savedpath);
861 /* Is f2p(pfid) present on the target? If not, the parent has moved */
863 snprintf(info->dest, sizeof(info->dest), "%s/%s",
864 status->ls_targets[0], info->path);
865 if (access(info->dest, F_OK) != 0) {
866 lr_debug(DTRACE, "create: parent %s not found\n",
871 for (info->target_no = 0; info->target_no < status->ls_num_targets;
873 snprintf(info->dest, sizeof(info->dest), "%s/%s",
874 status->ls_targets[info->target_no], info->savedpath);
875 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
879 rc1 = lr_mkfile(info);
880 if (mkspecial || rc1 == -ENOENT)
881 rc1 = lr_mk_special(info);
888 /* Replicate a file remove (rmdir/unlink) operation */
889 int lr_remove(struct lr_info *info)
894 for (info->target_no = 0; info->target_no < status->ls_num_targets;
896 rc1 = lr_rm_special(info);
900 rc1 = lr_get_path(info, info->pfid);
901 if (rc1 == -ENOENT) {
902 lr_debug(DINFO, "remove: pfid %s not found\n",
910 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
911 status->ls_targets[info->target_no], info->path,
914 rc1 = lr_rmfile(info);
915 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
916 info->dest, rc1, errno);
917 if (rc1 == -ENOTEMPTY)
918 rc1 = lr_rm_recursive(info);
928 /* Replicate a rename/move operation. */
929 int lr_move(struct lr_info *info)
935 int special_dest = 0;
936 char srcpath[PATH_MAX + 1] = "";
938 assert(info->is_extended);
940 rc_src = lr_get_path(info, info->spfid);
941 if (rc_src < 0 && rc_src != -ENOENT)
943 memcpy(srcpath, info->path, strlen(info->path));
945 rc_dest = lr_get_path(info, info->pfid);
946 if (rc_dest < 0 && rc_dest != -ENOENT)
949 for (info->target_no = 0; info->target_no < status->ls_num_targets;
952 snprintf(info->dest, sizeof(info->dest), "%s/%s",
953 status->ls_targets[info->target_no],
955 if (access(info->dest, F_OK) != 0) {
958 snprintf(info->dest, sizeof(info->dest),
960 status->ls_targets[info->target_no],
961 info->path, info->name);
963 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
966 if (rc_dest == -ENOENT) {
967 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
968 status->ls_targets[info->target_no],
969 SPECIAL_DIR, info->sfid);
971 lr_debug(DINFO, "special dest %s\n", info->dest);
975 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
976 status->ls_targets[info->target_no],
977 srcpath, info->sname);
978 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
981 if (rc_src == -ENOENT ||
982 (access(info->src, F_OK) != 0 && errno == ENOENT)) {
983 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
984 status->ls_targets[info->target_no],
985 SPECIAL_DIR, info->sfid);
987 lr_debug(DINFO, "special src %s\n", info->src);
992 if (strcmp(info->src, info->dest) != 0) {
993 rc1 = rename(info->src, info->dest);
996 lr_debug(DINFO, "rename returns %d\n", rc1);
1000 rc1 = lr_remove_pc(info->spfid, info->sfid);
1003 lr_cascade_move(info->sfid, info->dest, info);
1005 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
1007 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
1008 info->src, info->dest, rc1, errno);
1015 /* Replicate a hard link */
1016 int lr_link(struct lr_info *info)
1023 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1024 rc = stat(info->src, &st);
1028 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1029 info->target_no++) {
1036 * The changelog record has the new parent directory FID and
1037 * name of the target file. So info->dest can be constructed
1038 * by getting the path of the new parent directory and
1039 * appending the target file name.
1041 rc1 = lr_get_path(info, info->pfid);
1042 lr_debug(rc1 ? 0 : DTRACE, "\tparent fid2path %s, %s, rc=%d\n",
1043 info->path, info->name, rc1);
1046 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1047 status->ls_targets[info->target_no],
1048 info->path, info->name);
1049 lr_debug(DINFO, "link destination is %s\n", info->dest);
1052 /* Search through the hardlinks to get the src */
1053 for (i = 0; i < st.st_nlink && info->src[0] == 0; i++) {
1056 rc1 = lr_get_path_ln(info, info->tfid, i);
1057 lr_debug(rc1 ? 0 : DTRACE,
1058 "\tfid2path %s, %s, %d rc=%d\n", info->path,
1059 info->name, i, rc1);
1064 * Compare the path of target FID with info->dest
1065 * to find out info->src.
1067 len = sizeof(status->ls_targets[info->target_no]) +
1069 char srcpath[len + 1];
1071 snprintf(srcpath, sizeof(srcpath), "%s/%s",
1072 status->ls_targets[info->target_no],
1075 if (strcmp(srcpath, info->dest) != 0) {
1076 snprintf(info->src, sizeof(info->src), "%s",
1078 lr_debug(DINFO, "link source is %s\n",
1088 if (info->src[0] == 0)
1089 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
1090 status->ls_targets[info->target_no],
1091 SPECIAL_DIR, info->tfid);
1092 else if (info->dest[0] == 0)
1093 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1094 status->ls_targets[info->target_no],
1095 SPECIAL_DIR, info->tfid);
1097 rc1 = link(info->src, info->dest);
1098 lr_debug(DINFO, "link: %s [to] %s; rc1=%d %s\n",
1099 info->src, info->dest, rc1,
1100 strerror(rc1 ? errno : 0));
1108 int lr_set_dest_for_attr(struct lr_info *info)
1112 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1113 status->ls_targets[info->target_no], info->path);
1114 rc = access(info->dest, F_OK);
1121 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1122 status->ls_targets[info->target_no], SPECIAL_DIR,
1125 rc = access(info->dest, F_OK);
1132 /* Replicate file attributes */
1133 int lr_setattr(struct lr_info *info)
1138 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1140 rc = lr_get_path(info, info->tfid);
1142 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1147 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1148 info->target_no++) {
1149 rc = lr_set_dest_for_attr(info);
1153 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1156 rc1 = lr_sync_data(info);
1158 rc1 = lr_copy_attr(info->src, info->dest);
1165 /* Replicate xattrs */
1166 int lr_setxattr(struct lr_info *info)
1170 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1172 rc = lr_get_path(info, info->tfid);
1174 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1179 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1180 info->target_no++) {
1181 rc = lr_set_dest_for_attr(info);
1185 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1188 rc1 = lr_copy_xattr(info);
1196 /* Parse a line of changelog entry */
1197 int lr_parse_line(void *priv, struct lr_info *info)
1199 struct changelog_rec *rec;
1200 struct changelog_ext_rename *rnm;
1202 size_t copylen = sizeof(info->name);
1204 if (llapi_changelog_recv(priv, &rec) != 0)
1207 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1208 info->recno = rec->cr_index;
1209 info->type = rec->cr_type;
1210 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1211 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1213 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1214 if (copylen > namelen + 1)
1215 copylen = namelen + 1;
1216 snprintf(info->name, copylen, "%s", changelog_rec_name(rec));
1218 /* Don't use rnm if CLF_RENAME isn't set */
1219 rnm = changelog_rec_rename(rec);
1220 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1221 copylen = sizeof(info->sname);
1223 snprintf(info->sfid, sizeof(info->sfid), DFID,
1224 PFID(&rnm->cr_sfid));
1225 snprintf(info->spfid, sizeof(info->spfid), DFID,
1226 PFID(&rnm->cr_spfid));
1227 namelen = changelog_rec_snamelen(rec);
1228 if (copylen > namelen + 1)
1229 copylen = namelen + 1;
1230 snprintf(info->sname, copylen, "%s", changelog_rec_sname(rec));
1233 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1234 info->name, info->sname);
1237 printf("Rec %lld: %d %s\n", info->recno, info->type,
1241 llapi_changelog_free(&rec);
1247 /* Initialize the replication parameters */
1248 int lr_init_status(void)
1250 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1254 status = calloc(size, 1);
1257 status->ls_version = REPLICATE_STATUS_VER;
1258 status->ls_size = size;
1259 status->ls_last_recno = -1;
1263 /* Make a backup of the statuslog */
1264 void lr_backup_log(void)
1266 char backupfile[PATH_MAX];
1270 snprintf(backupfile, sizeof(backupfile), "%s.old", statuslog);
1271 (void) rename(statuslog, backupfile);
1275 /* Save replication parameters to a statuslog. */
1276 int lr_write_log(void)
1280 size_t write_size = status->ls_size;
1281 struct lr_parent_child_list *curr;
1289 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
1291 fprintf(stderr, "Error opening log file for writing (%s)\n",
1296 size = write(fd, status, write_size);
1297 if (size != write_size) {
1298 fprintf(stderr, "Error writing to log file (%s) %d\n",
1304 for (curr = parents; curr; curr = curr->pc_next) {
1305 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1306 if (size != sizeof(curr->pc_log)) {
1307 fprintf(stderr, "Error writing to log file (%s) %d\n",
1318 * Read statuslog and populate the replication parameters. Command
1319 * line parameters take precedence over parameters in the log file.
1321 int lr_read_log(void)
1323 struct lr_parent_child_list *tmp;
1324 struct lr_parent_child_log rec;
1325 struct lustre_rsync_status *s;
1328 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1334 s = calloc(1, read_size);
1340 fd = open(statuslog, O_RDONLY);
1346 size = read(fd, s, read_size);
1347 if (size != read_size) {
1352 if (read_size < s->ls_size) {
1353 read_size = s->ls_size;
1354 s = lr_grow_buf(s, read_size);
1360 if (lseek(fd, 0, SEEK_SET) == -1) {
1365 size = read(fd, s, read_size);
1366 if (size != read_size) {
1372 while (read(fd, &rec, sizeof(rec)) != 0) {
1373 tmp = calloc(1, sizeof(*tmp));
1380 tmp->pc_next = parents;
1384 /* copy uninitialized fields to status */
1385 if (status->ls_num_targets == 0) {
1386 if (status->ls_size != s->ls_size) {
1387 status = lr_grow_buf(status, s->ls_size);
1393 status->ls_size = s->ls_size;
1395 status->ls_num_targets = s->ls_num_targets;
1396 memcpy(status->ls_targets, s->ls_targets,
1397 (PATH_MAX + 1) * s->ls_num_targets);
1399 if (status->ls_last_recno == -1)
1400 status->ls_last_recno = s->ls_last_recno;
1402 if (status->ls_registration[0] == '\0')
1403 snprintf(status->ls_registration,
1404 sizeof(status->ls_registration), "%s",
1405 s->ls_registration);
1407 if (status->ls_mdt_device[0] == '\0')
1408 snprintf(status->ls_mdt_device,
1409 sizeof(status->ls_mdt_device), "%s",
1412 if (status->ls_source_fs[0] == '\0')
1413 snprintf(status->ls_source_fs,
1414 sizeof(status->ls_source_fs), "%s",
1417 if (status->ls_source[0] == '\0')
1418 snprintf(status->ls_source,
1419 sizeof(status->ls_source), "%s",
1431 * Clear changelogs every CLEAR_INTERVAL records or at the end of
1434 int lr_clear_cl(struct lr_info *info, int force)
1436 char mdt_device[LR_NAME_MAXLEN + 1];
1439 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1440 if (!noclear && !dryrun) {
1442 * llapi_changelog_clear modifies the mdt
1443 * device name so make a copy of it until this
1446 snprintf(mdt_device, sizeof(mdt_device), "%s",
1447 status->ls_mdt_device);
1448 rc = llapi_changelog_clear(mdt_device,
1449 status->ls_registration,
1452 printf("Changelog clear (%s, %s, %lld) returned %d\n",
1453 status->ls_mdt_device,
1454 status->ls_registration, info->recno,
1458 if (!rc && !dryrun) {
1459 status->ls_last_recno = info->recno;
1468 * Locate a usable version of rsync. At this point we'll use any
1471 int lr_locate_rsync(void)
1477 snprintf(rsync, sizeof(rsync), "%s -p %s", TYPE, RSYNC);
1478 fp = popen(rsync, "r");
1482 if (fgets(rsync, sizeof(rsync), fp) == NULL) {
1487 len = strlen(rsync);
1488 if (len > 0 && rsync[len - 1] == '\n')
1489 rsync[len - 1] = '\0';
1492 /* Determine the version of rsync */
1493 snprintf(rsync_ver, sizeof(rsync_ver), "%s --version", rsync);
1494 fp = popen(rsync_ver, "r");
1498 if (fgets(rsync_ver, sizeof(rsync_ver), fp) == NULL) {
1502 len = strlen(rsync_ver);
1503 if (len > 0 && rsync_ver[len - 1] == '\n')
1504 rsync_ver[len - 1] = '\0';
1510 /* Print the replication parameters */
1511 void lr_print_status(struct lr_info *info)
1518 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1519 printf("MDT device: %s\n", status->ls_mdt_device);
1520 printf("Source: %s\n", status->ls_source);
1521 for (i = 0; i < status->ls_num_targets; i++)
1522 printf("Target: %s\n", status->ls_targets[i]);
1524 printf("Statuslog: %s\n", statuslog);
1525 printf("Changelog registration: %s\n", status->ls_registration);
1526 printf("Starting changelog record: %jd\n",
1527 (uintmax_t)status->ls_last_recno);
1529 printf("Replicate xattrs: no\n");
1531 printf("Clear changelog after use: no\n");
1533 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1536 void lr_print_failure(struct lr_info *info, int rc)
1539 "Replication of operation failed(%d): %lld %s (%d) %s %s %s\n",
1540 rc, info->recno, changelog_type2str(info->type), info->type,
1541 info->tfid, info->pfid, info->name);
1544 /* Replicate filesystem operations from src_path to target_path */
1545 int lr_replicate(void)
1547 void *changelog_priv = NULL;
1548 struct lr_info *info;
1549 struct lr_info *ext = NULL;
1557 info = calloc(1, sizeof(struct lr_info));
1561 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1563 fprintf(stderr, "Source path is not a valid Lustre client mountpoint.\n");
1567 if (status->ls_mdt_device[0] == '\0') {
1570 len = snprintf(status->ls_mdt_device,
1571 sizeof(status->ls_mdt_device), "%s%s",
1572 status->ls_source_fs, DEFAULT_MDT);
1573 if (len >= sizeof(status->ls_mdt_device)) {
1579 ext = calloc(1, sizeof(struct lr_info));
1585 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1586 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1587 status->ls_targets[i], SPECIAL_DIR);
1588 rc = mkdir(info->dest, 0777);
1589 if (rc == -1 && errno != EEXIST) {
1590 fprintf(stderr, "Error writing to target path %s.\n",
1591 status->ls_targets[i]);
1595 rc = llistxattr(info->src, info->xlist, info->xsize);
1596 if (rc == -1 && errno == ENOTSUP) {
1597 fprintf(stderr, "xattrs not supported on %s\n",
1598 status->ls_targets[i]);
1602 if (xattr_not_supp == status->ls_num_targets)
1603 /* None of the targets support xattrs. */
1606 lr_print_status(info);
1608 /* Open changelogs for consumption*/
1609 rc = llapi_changelog_start(&changelog_priv,
1610 CHANGELOG_FLAG_BLOCK |
1611 CHANGELOG_FLAG_JOBID |
1612 CHANGELOG_FLAG_EXTRA_FLAGS,
1613 status->ls_mdt_device,
1614 status->ls_last_recno);
1616 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1617 status->ls_source_fs);
1621 rc = llapi_changelog_set_xflags(changelog_priv,
1622 CHANGELOG_EXTRA_FLAG_UIDGID |
1623 CHANGELOG_EXTRA_FLAG_NID |
1624 CHANGELOG_EXTRA_FLAG_OMODE |
1625 CHANGELOG_EXTRA_FLAG_XATTR);
1627 fprintf(stderr, "Error setting xflag in changelog for fs %s.\n",
1628 status->ls_source_fs);
1632 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1635 if (info->type == CL_RENAME && !info->is_extended) {
1637 * Newer rename operations extends changelog to store
1638 * source file information, but old changelog has
1641 if (lr_parse_line(changelog_priv, ext) != 0)
1643 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1644 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1645 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1646 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1647 snprintf(info->sname, sizeof(info->sname), "%s",
1649 snprintf(info->name, sizeof(info->name), "%s",
1651 info->is_extended = 1;
1652 info->recno = ext->recno; /* For lr_clear_cl(). */
1658 lr_debug(DTRACE, "***** Start %lld %s (%d) %s %s %s *****\n",
1659 info->recno, changelog_type2str(info->type),
1660 info->type, info->tfid, info->pfid, info->name);
1662 switch (info->type) {
1667 rc = lr_create(info);
1671 rc = lr_remove(info);
1681 rc = lr_setattr(info);
1684 rc = lr_setxattr(info);
1694 * Nothing needs to be done for these entries
1702 lr_debug(DTRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n",
1703 info->recno, changelog_type2str(info->type),
1704 info->type, info->tfid, info->pfid, info->name, rc);
1706 if (rc && rc != -ENOENT) {
1707 lr_print_failure(info, rc);
1712 lr_clear_cl(info, 0);
1715 llapi_changelog_fini(&changelog_priv);
1717 if (errors || verbose)
1718 printf("Errors: %d\n", errors);
1720 /* Clear changelog records used so far */
1721 lr_clear_cl(info, 1);
1724 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1725 printf("Changelog records consumed: %lld\n", rec_count);
1732 free(changelog_priv);
1742 termination_handler (int signum)
1744 /* Set a flag for the replicator to gracefully shutdown */
1746 printf("lustre_rsync halting.\n");
1749 int main(int argc, char *argv[])
1755 if ((rc = lr_init_status()) != 0)
1758 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1759 long_opts, NULL)) >= 0) {
1762 /* Assume absolute paths */
1766 /* Assume absolute paths */
1767 snprintf(status->ls_source, sizeof(status->ls_source),
1771 status->ls_num_targets++;
1773 if (numtargets != status->ls_num_targets) {
1775 * Targets were read from a log file.
1776 * The ones specified on the command line
1777 * take precedence. The ones from the log
1778 * file will be ignored.
1780 status->ls_num_targets = numtargets;
1782 newsize = sizeof(struct lustre_rsync_status) +
1783 (status->ls_num_targets * (PATH_MAX + 1));
1784 if (status->ls_size != newsize) {
1785 status->ls_size = newsize;
1786 status = lr_grow_buf(status, newsize);
1790 snprintf(status->ls_targets[status->ls_num_targets - 1],
1791 sizeof(status->ls_targets[0]), "%s", optarg);
1794 snprintf(status->ls_mdt_device,
1795 sizeof(status->ls_mdt_device),
1799 snprintf(status->ls_registration,
1800 sizeof(status->ls_registration),
1805 (void)lr_read_log();
1811 if (strcmp("no", optarg) == 0) {
1813 } else if (strcmp("yes", optarg) != 0) {
1814 printf("Invalid parameter %s. Specify --xattr=no or --xattr=yes\n",
1823 /* Undocumented option cl-clear */
1824 if (strcmp("no", optarg) == 0) {
1826 } else if (strcmp("yes", optarg) != 0) {
1827 printf("Invalid parameter %s. Specify --cl-clear=no or --cl-clear=yes\n",
1833 /* Undocumented option use-rsync */
1837 /* Undocumented option rsync-threshold */
1838 rsync_threshold = atol(optarg);
1841 /* Undocumented option start-recno */
1842 status->ls_last_recno = atol(optarg);
1845 /* Undocumented option debug */
1846 debug = atoi(optarg);
1847 if (debug < 0 || debug > 2)
1851 /* Undocumented option debug log file */
1854 debug_log = fopen(optarg, "a");
1856 printf("Cannot open %s for debug log\n",
1862 fprintf(stderr, "error: %s: option '%s' unrecognized.\n",
1863 argv[0], argv[optind - 1]);
1869 if (status->ls_last_recno == -1)
1870 status->ls_last_recno = 0;
1871 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1872 /* No registration ID was passed in. */
1873 printf("Please specify changelog consumer registration id.\n");
1877 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1878 fprintf(stderr, "Please specify the source path.\n");
1882 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1883 fprintf(stderr, "Please specify the target path.\n");
1888 rc = lr_locate_rsync();
1889 if (use_rsync && rc != 0) {
1890 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1894 signal(SIGINT, termination_handler);
1895 signal(SIGHUP, termination_handler);
1896 signal(SIGTERM, termination_handler);
1898 rc = lr_replicate();