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"
144 /* Information for processing a changelog record. This structure is
145 allocated on the heap instead of allocating large variables on the
150 unsigned int is_extended:1;
151 enum changelog_rec_type type;
152 char tfid[LR_FID_STR_LEN];
153 char pfid[LR_FID_STR_LEN];
154 char sfid[LR_FID_STR_LEN];
155 char spfid[LR_FID_STR_LEN];
156 char sname[NAME_MAX + 1];
157 char name[NAME_MAX + 1];
158 char src[3 * PATH_MAX + 1];
159 char dest[3 * PATH_MAX + 1];
160 char path[PATH_MAX + 1];
161 char savedpath[PATH_MAX + 1];
162 char link[PATH_MAX + 1];
163 char linktmp[PATH_MAX + 1];
167 /* Variables for querying the xattributes */
174 struct lr_parent_child_list {
175 struct lr_parent_child_log pc_log;
176 struct lr_parent_child_list *pc_next;
179 struct lustre_rsync_status *status;
180 char *statuslog; /* Name of the status log file */
182 int noxattr; /* Flag to turn off replicating xattrs */
183 int noclear; /* Flag to turn off clearing changelogs */
184 int debug; /* Flag to turn debugging information on and off */
185 int verbose; /* Verbose output */
186 long long rec_count; /* No of changelog records that were processed */
189 int use_rsync; /* Flag to turn on use of rsync to copy data */
190 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
191 int quit; /* Flag to stop processing the changelog; set on the
192 receipt of a signal */
193 int abort_on_err = 0;
195 char rsync[PATH_MAX];
196 char rsync_ver[PATH_MAX];
197 struct lr_parent_child_list *parents;
201 /* Command line options */
202 struct option long_opts[] = {
203 { .val = 'l', .name = "statuslog", .has_arg = required_argument },
204 { .val = 'm', .name = "mdt", .has_arg = required_argument },
205 { .val = 's', .name = "source", .has_arg = required_argument },
206 { .val = 't', .name = "target", .has_arg = required_argument },
207 { .val = 'u', .name = "user", .has_arg = required_argument },
208 { .val = 'v', .name = "verbose", .has_arg = no_argument },
209 { .val = 'x', .name = "xattr", .has_arg = required_argument },
210 { .val = 'z', .name = "dry-run", .has_arg = no_argument },
211 /* Undocumented options follow */
212 { .val = 'a', .name = "abort-on-err", .has_arg = no_argument },
213 { .val = 'c', .name = "cl-clear", .has_arg = required_argument },
214 { .val = 'd', .name = "debug", .has_arg = required_argument },
215 { .val = 'D', .name = "debuglog", .has_arg = required_argument },
216 { .val = 'n', .name = "start-recno", .has_arg = required_argument },
217 { .val = 'r', .name = "use-rsync", .has_arg = no_argument },
218 { .val = 'y', .name = "rsync-threshold",
219 .has_arg = required_argument },
222 /* Command line usage */
225 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
226 "-m <mdt> -r <user id> -l <status log>\n"
227 "lustre_rsync can also pick up parameters from a "
228 "status log created earlier.\n"
229 "\tlustre_rsync -l <log_file>\n"
231 "\t--xattr <yes|no> replicate EAs\n"
232 "\t--abort-on-err abort at first err\n"
234 "\t--dry-run don't write anything\n");
237 /* Print debug information. This is controlled by the value of the
238 global variable 'debug' */
239 void lr_debug(int level, const char *fmt, ...)
247 if (debug_log != NULL)
248 vfprintf(debug_log, fmt, ap);
250 vfprintf(stdout, fmt, ap);
255 void * lr_grow_buf(void *buf, int size)
259 ptr = realloc(buf, size);
266 /* Use rsync to replicate file data */
267 int lr_rsync_data(struct lr_info *info)
269 struct stat st_src, st_dest;
272 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
274 rc = stat(info->src, &st_src);
276 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
277 info->src, info->name);
283 rc = stat(info->dest, &st_dest);
285 fprintf(stderr, "Error: Unable to stat dest=%s\n",
290 if (st_src.st_mtime != st_dest.st_mtime ||
291 st_src.st_size != st_dest.st_size) {
292 /* XXX spawning off an rsync for every data sync and
293 * waiting synchronously is bad for performance.
294 * librsync could possibly used here. But it does not
295 * seem to be of production grade. Multi-threaded
296 * replication is also to be considered.
306 extern char **environ;
309 lr_debug(DTRACE, "\t%s %s %s %s %s %s\n", args[0], args[1],
310 args[2], args[3], args[4], info->tfid);
312 status = callvpe(rsync, args, environ);
315 } else if (WIFEXITED(status)) {
316 status = WEXITSTATUS(status);
319 else if (status == 23 || status == 24)
320 /* Error due to vanished source files;
326 lr_debug(DINFO, "rsync %s exited with %d %d\n",
327 info->src, status, rc);
332 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
333 info->dest, info->tfid);
339 int lr_copy_data(struct lr_info *info)
349 fd_src = open(info->src, O_RDONLY);
352 if (fstat(fd_src, &st_src) == -1 ||
353 stat(info->dest, &st_dest) == -1)
356 if (st_src.st_mtime == st_dest.st_mtime &&
357 st_src.st_size == st_dest.st_size)
360 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
361 /* It is more efficient to use rsync to replicate
362 large files. Any file larger than rsync_threshold
363 is handed off to rsync. */
364 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
365 rc = lr_rsync_data(info);
369 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
374 bufsize = st_dest.st_blksize;
376 if (info->bufsize < bufsize) {
378 info->buf = lr_grow_buf(info->buf, bufsize);
379 if (info->buf == NULL) {
383 info->bufsize = bufsize;
391 rsize = read(fd_src, buf, bufsize);
401 wsize = write(fd_dest, buf, rsize);
421 /* Copy data from source to destination */
422 int lr_sync_data(struct lr_info *info)
425 return lr_rsync_data(info);
427 return lr_copy_data(info);
430 /* Copy all attributes from file src to file dest */
431 int lr_copy_attr(char *src, char *dest)
436 if (stat(src, &st) == -1 ||
437 chmod(dest, st.st_mode) == -1 ||
438 chown(dest, st.st_uid, st.st_gid) == -1)
441 time.actime = st.st_atime;
442 time.modtime = st.st_mtime;
443 if (utime(dest, &time) == -1)
448 /* Copy all xattrs from file info->src to info->dest */
449 int lr_copy_xattr(struct lr_info *info)
451 size_t size = info->xsize;
460 rc = llistxattr(info->src, info->xlist, size);
461 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
462 info->src, info->xlist, rc, errno);
463 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
464 size = rc > PATH_MAX ? rc : PATH_MAX;
465 info->xlist = lr_grow_buf(info->xlist, size);
466 if (info->xlist == NULL)
469 rc = llistxattr(info->src, info->xlist, size);
470 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
471 info->src, rc, errno);
478 while (start < len) {
480 rc = lgetxattr(info->src, info->xlist + start,
482 if (info->xvalue == NULL || errno == ERANGE) {
483 size = rc > PATH_MAX ? rc : PATH_MAX;
484 info->xvalue = lr_grow_buf(info->xvalue, size);
485 if (info->xvalue == NULL)
488 rc = lgetxattr(info->src, info->xlist + start,
491 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
495 rc = lsetxattr(info->dest, info->xlist + start,
496 info->xvalue, size, 0);
497 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
500 if (errno != ENOTSUP) {
501 fprintf(stderr, "Error replicating "
502 " xattr for %s: %d\n",
509 start += strlen(info->xlist + start) + 1;
512 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
517 /* Retrieve the filesystem path for a given FID and a given
518 linkno. The path is returned in info->path */
519 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
521 long long recno = -1;
524 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
525 PATH_MAX, &recno, &linkno);
526 if (rc < 0 && rc != -ENOENT) {
527 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
528 status->ls_source, fidstr, -rc, strerror(errno = -rc));
534 /* Retrieve the filesystem path for a given FID. The path is returned
536 int lr_get_path(struct lr_info *info, char *fidstr)
538 return lr_get_path_ln(info, fidstr, 0);
541 /* Generate the path for opening by FID */
542 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
544 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
545 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
550 /* Read the symlink information into 'info->link' */
551 int lr_get_symlink(struct lr_info *info)
556 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
557 rc = readlink(info->src, info->linktmp, PATH_MAX);
559 info->linktmp[rc] = '\0';
562 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
564 if (strncmp(info->linktmp, status->ls_source,
565 strlen(status->ls_source)) == 0) {
566 /* Strip source fs path and replace with target fs path. */
567 link = info->linktmp + strlen(status->ls_source);
568 snprintf(info->src, sizeof(info->src), "%s%s",
569 status->ls_targets[info->target_no], link);
572 link = info->linktmp;
574 rc = snprintf(info->link, sizeof(info->link), "%s", link);
575 if (rc >= sizeof(info->link))
580 /* Create file/directory/device file/symlink. */
581 int lr_mkfile(struct lr_info *info)
587 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
588 if (info->type == CL_MKDIR) {
589 rc = mkdir(info->dest, 0777);
590 } else if (info->type == CL_SOFTLINK) {
591 lr_get_symlink(info);
592 rc = symlink(info->link, info->dest);
593 } else if (info->type == CL_MKNOD) {
594 lr_get_FID_PATH(status->ls_source, info->tfid,
595 info->src, PATH_MAX);
596 rc = stat(info->src, &st);
603 rc = mknod(info->dest, st.st_mode, st.st_rdev);
605 rc = mknod(info->dest, S_IFREG | 0777, 0);
615 /* Sync data and attributes */
616 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
617 lr_debug(DTRACE, "Syncing data and attributes %s\n",
619 (void) lr_copy_xattr(info);
620 if (info->type == CL_CREATE)
621 rc = lr_sync_data(info);
623 rc = lr_copy_attr(info->src, info->dest);
626 /* Source file has disappeared. Not an error. */
629 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
636 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
638 struct lr_parent_child_list *p;
641 p = calloc(1, sizeof(*p));
644 len = snprintf(p->pc_log.pcl_pfid, sizeof(p->pc_log.pcl_pfid),
646 if (len >= sizeof(p->pc_log.pcl_pfid))
648 len = snprintf(p->pc_log.pcl_tfid, sizeof(p->pc_log.pcl_tfid),
650 if (len >= sizeof(p->pc_log.pcl_tfid))
652 len = snprintf(p->pc_log.pcl_name, sizeof(p->pc_log.pcl_name),
654 if (len >= sizeof(p->pc_log.pcl_name))
657 p->pc_next = parents;
666 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
668 struct lr_parent_child_list *curr, *prev;
669 char d[4 * PATH_MAX + 1];
672 prev = curr = parents;
674 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
675 if (snprintf(d, sizeof(d), "%s/%s", dest,
676 curr->pc_log.pcl_name) >= sizeof(d)) {
677 fprintf(stderr, "Buffer truncated\n");
680 if (snprintf(info->src, sizeof(info->src), "%s/%s/%s",
681 status->ls_targets[info->target_no],
682 SPECIAL_DIR, curr->pc_log.pcl_tfid) >=
685 rc = rename(info->src, d);
687 fprintf(stderr, "Error renaming file "
689 info->src, d, errno);
693 parents = curr->pc_next;
695 prev->pc_next = curr->pc_next;
696 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
698 prev = curr = parents;
702 curr = curr->pc_next;
707 /* remove [info->spfid, info->sfid] from parents */
708 int lr_remove_pc(const char *pfid, const char *tfid)
710 struct lr_parent_child_list *curr, *prev;
712 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
713 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
714 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
716 parents = curr->pc_next;
718 prev->pc_next = curr->pc_next;
726 /* Create file under SPECIAL_DIR with its tfid as its name. */
727 int lr_mk_special(struct lr_info *info)
731 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
732 status->ls_targets[info->target_no], SPECIAL_DIR,
735 rc = lr_mkfile(info);
739 rc = lr_add_pc(info->pfid, info->tfid, info->name);
743 /* Remove a file or directory */
744 int lr_rmfile(struct lr_info *info)
748 if (info->type == CL_RMDIR)
749 rc = rmdir(info->dest);
751 rc = unlink(info->dest);
757 /* Recursively remove directory and its contents */
758 int lr_rm_recursive(struct lr_info *info)
767 extern char **environ;
771 status = callvpe("/bin/rm", args, environ);
774 else if (WIFEXITED(status))
775 rc = WEXITSTATUS(status) == 0 ? 0 : -EINVAL;
782 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
783 int lr_rm_special(struct lr_info *info)
787 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
788 status->ls_targets[info->target_no], SPECIAL_DIR,
790 rc = lr_rmfile(info);
793 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
794 info->dest, rc, errno);
798 /* Replicate file and directory create events */
799 int lr_create(struct lr_info *info)
806 /* Is target FID present on the source? */
807 rc = lr_get_path(info, info->tfid);
809 /* Source file has disappeared. Not an error. */
810 lr_debug(DINFO, "create: tfid %s not found on"
811 "source-fs\n", info->tfid);
816 strcpy(info->savedpath, info->path);
818 /* Is parent FID present on the source */
819 rc = lr_get_path(info, info->pfid);
821 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
828 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
829 len = strlen(info->path);
830 if (len == 1 && info->path[0] == '/')
831 snprintf(info->dest, sizeof(info->dest), "%s", info->name);
832 else if (len - 1 > 0 && info->path[len - 1] == '/')
833 snprintf(info->dest, sizeof(info->dest), "%s%s", info->path,
836 snprintf(info->dest, sizeof(info->dest), "%s/%s", info->path,
839 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
841 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
842 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
843 info->tfid, info->dest, info->savedpath);
847 /* Is f2p(pfid) present on the target? If not, the parent has
850 snprintf(info->dest, sizeof(info->dest), "%s/%s",
851 status->ls_targets[0], info->path);
852 if (access(info->dest, F_OK) != 0) {
853 lr_debug(DTRACE, "create: parent %s not found\n",
858 for (info->target_no = 0; info->target_no < status->ls_num_targets;
860 snprintf(info->dest, sizeof(info->dest), "%s/%s",
861 status->ls_targets[info->target_no], info->savedpath);
862 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
866 rc1 = lr_mkfile(info);
867 if (mkspecial || rc1 == -ENOENT) {
868 rc1 = lr_mk_special(info);
876 /* Replicate a file remove (rmdir/unlink) operation */
877 int lr_remove(struct lr_info *info)
882 for (info->target_no = 0; info->target_no < status->ls_num_targets;
885 rc1 = lr_rm_special(info);
889 rc1 = lr_get_path(info, info->pfid);
890 if (rc1 == -ENOENT) {
891 lr_debug(DINFO, "remove: pfid %s not found\n",
899 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
900 status->ls_targets[info->target_no], info->path,
903 rc1 = lr_rmfile(info);
904 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
905 info->dest, rc1, errno);
906 if (rc1 == -ENOTEMPTY)
907 rc1 = lr_rm_recursive(info);
917 /* Replicate a rename/move operation. */
918 int lr_move(struct lr_info *info)
924 int special_dest = 0;
925 char srcpath[PATH_MAX + 1] = "";
927 assert(info->is_extended);
929 rc_src = lr_get_path(info, info->spfid);
930 if (rc_src < 0 && rc_src != -ENOENT)
932 memcpy(srcpath, info->path, strlen(info->path));
934 rc_dest = lr_get_path(info, info->pfid);
935 if (rc_dest < 0 && rc_dest != -ENOENT)
938 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 || (access(info->src, F_OK) != 0 &&
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, "\tfid2path %s, %s, %d rc=%d\n",
1048 info->path, info->name, i, rc1);
1053 * Compare the path of target FID with info->dest
1054 * to find out info->src.
1056 len = sizeof(status->ls_targets[info->target_no]) +
1058 char srcpath[len + 1];
1060 snprintf(srcpath, sizeof(srcpath), "%s/%s",
1061 status->ls_targets[info->target_no],
1064 if (strcmp(srcpath, info->dest) != 0) {
1065 snprintf(info->src, sizeof(info->src), "%s",
1067 lr_debug(DINFO, "link source is %s\n",
1077 if (info->src[0] == 0)
1078 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
1079 status->ls_targets[info->target_no],
1080 SPECIAL_DIR, info->tfid);
1081 else if (info->dest[0] == 0)
1082 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1083 status->ls_targets[info->target_no],
1084 SPECIAL_DIR, info->tfid);
1086 rc1 = link(info->src, info->dest);
1087 lr_debug(DINFO, "link: %s [to] %s; rc1=%d %s\n",
1088 info->src, info->dest, rc1,
1089 strerror(rc1 ? errno : 0));
1097 /* Replicate file attributes */
1098 int lr_setattr(struct lr_info *info)
1103 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1105 rc = lr_get_path(info, info->tfid);
1107 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1112 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1113 info->target_no++) {
1115 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1116 status->ls_targets[info->target_no], info->path);
1117 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1120 rc1 = lr_sync_data(info);
1122 rc1 = lr_copy_attr(info->src, info->dest);
1129 /* Replicate xattrs */
1130 int lr_setxattr(struct lr_info *info)
1134 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1136 rc = lr_get_path(info, info->tfid);
1138 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1143 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1144 info->target_no++) {
1146 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1147 status->ls_targets[info->target_no], info->path);
1148 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1151 rc1 = lr_copy_xattr(info);
1159 /* Parse a line of changelog entry */
1160 int lr_parse_line(void *priv, struct lr_info *info)
1162 struct changelog_rec *rec;
1163 struct changelog_ext_rename *rnm;
1165 size_t copylen = sizeof(info->name);
1167 if (llapi_changelog_recv(priv, &rec) != 0)
1170 info->is_extended = !!(rec->cr_flags & CLF_RENAME);
1171 info->recno = rec->cr_index;
1172 info->type = rec->cr_type;
1173 snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
1174 snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
1176 namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
1177 if (copylen > namelen + 1)
1178 copylen = namelen + 1;
1179 snprintf(info->name, copylen, "%s", changelog_rec_name(rec));
1181 /* Don't use rnm if CLF_RENAME isn't set */
1182 rnm = changelog_rec_rename(rec);
1183 if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
1184 copylen = sizeof(info->sname);
1186 snprintf(info->sfid, sizeof(info->sfid), DFID,
1187 PFID(&rnm->cr_sfid));
1188 snprintf(info->spfid, sizeof(info->spfid), DFID,
1189 PFID(&rnm->cr_spfid));
1190 namelen = changelog_rec_snamelen(rec);
1191 if (copylen > namelen + 1)
1192 copylen = namelen + 1;
1193 snprintf(info->sname, copylen, "%s", changelog_rec_sname(rec));
1196 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1197 info->name, info->sname);
1200 printf("Rec %lld: %d %s\n", info->recno, info->type,
1204 llapi_changelog_free(&rec);
1210 /* Initialize the replication parameters */
1211 int lr_init_status()
1213 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1217 status = calloc(size, 1);
1220 status->ls_version = REPLICATE_STATUS_VER;
1221 status->ls_size = size;
1222 status->ls_last_recno = -1;
1226 /* Make a backup of the statuslog */
1227 void lr_backup_log()
1229 char backupfile[PATH_MAX];
1233 snprintf(backupfile, sizeof(backupfile), "%s.old", statuslog);
1234 (void) rename(statuslog, backupfile);
1240 /* Save replication parameters to a statuslog. */
1245 size_t write_size = status->ls_size;
1246 struct lr_parent_child_list *curr;
1249 if (statuslog == NULL)
1254 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1257 fprintf(stderr, "Error opening log file for writing (%s)\n",
1262 size = write(fd, status, write_size);
1263 if (size != write_size) {
1264 fprintf(stderr, "Error writing to log file (%s) %d\n",
1270 for (curr = parents; curr; curr = curr->pc_next) {
1271 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1272 if (size != sizeof(curr->pc_log)) {
1273 fprintf(stderr, "Error writing to log file (%s) %d\n",
1283 /* Read statuslog and populate the replication parameters. Command
1284 * line parameters take precedence over parameters in the log file.*/
1287 struct lr_parent_child_list *tmp;
1288 struct lr_parent_child_log rec;
1289 struct lustre_rsync_status *s;
1292 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1295 if (statuslog == NULL)
1298 s = calloc(1, read_size);
1304 fd = open(statuslog, O_RDONLY);
1310 size = read(fd, s, read_size);
1311 if (size != read_size) {
1316 if (read_size < s->ls_size) {
1317 read_size = s->ls_size;
1318 s = lr_grow_buf(s, read_size);
1324 if (lseek(fd, 0, SEEK_SET) == -1) {
1329 size = read(fd, s, read_size);
1330 if (size != read_size) {
1336 while (read(fd, &rec, sizeof(rec)) != 0) {
1337 tmp = calloc(1, sizeof(*tmp));
1344 tmp->pc_next = parents;
1348 /* copy uninitialized fields to status */
1349 if (status->ls_num_targets == 0) {
1350 if (status->ls_size != s->ls_size) {
1351 status = lr_grow_buf(status, s->ls_size);
1352 if (status == NULL) {
1357 status->ls_size = s->ls_size;
1359 status->ls_num_targets = s->ls_num_targets;
1360 memcpy(status->ls_targets, s->ls_targets,
1361 (PATH_MAX + 1) * s->ls_num_targets);
1363 if (status->ls_last_recno == -1)
1364 status->ls_last_recno = s->ls_last_recno;
1366 if (status->ls_registration[0] == '\0')
1367 snprintf(status->ls_registration,
1368 sizeof(status->ls_registration), "%s",
1369 s->ls_registration);
1371 if (status->ls_mdt_device[0] == '\0')
1372 snprintf(status->ls_mdt_device,
1373 sizeof(status->ls_mdt_device), "%s",
1376 if (status->ls_source_fs[0] == '\0')
1377 snprintf(status->ls_source_fs,
1378 sizeof(status->ls_source_fs), "%s",
1381 if (status->ls_source[0] == '\0')
1382 snprintf(status->ls_source,
1383 sizeof(status->ls_source), "%s",
1394 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1396 int lr_clear_cl(struct lr_info *info, int force)
1398 char mdt_device[LR_NAME_MAXLEN + 1];
1401 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1402 if (!noclear && !dryrun) {
1403 /* llapi_changelog_clear modifies the mdt
1404 * device name so make a copy of it until this
1407 snprintf(mdt_device, sizeof(mdt_device), "%s",
1408 status->ls_mdt_device);
1409 rc = llapi_changelog_clear(mdt_device,
1410 status->ls_registration,
1413 printf("Changelog clear (%s, %s, %lld) "
1414 "returned %d\n", status->ls_mdt_device,
1415 status->ls_registration, info->recno,
1419 if (!rc && !dryrun) {
1420 status->ls_last_recno = info->recno;
1428 /* Locate a usable version of rsync. At this point we'll use any
1430 int lr_locate_rsync()
1436 snprintf(rsync, sizeof(rsync), "%s -p %s", TYPE, RSYNC);
1437 fp = popen(rsync, "r");
1441 if (fgets(rsync, sizeof(rsync), fp) == NULL) {
1446 len = strlen(rsync);
1447 if (len > 0 && rsync[len - 1] == '\n')
1448 rsync[len - 1] = '\0';
1451 /* Determine the version of rsync */
1452 snprintf(rsync_ver, sizeof(rsync_ver), "%s --version", rsync);
1453 fp = popen(rsync_ver, "r");
1457 if (fgets(rsync_ver, sizeof(rsync_ver), fp) == NULL) {
1461 len = strlen(rsync_ver);
1462 if (len > 0 && rsync_ver[len - 1] == '\n')
1463 rsync_ver[len - 1] = '\0';
1470 /* Print the replication parameters */
1471 void lr_print_status(struct lr_info *info)
1478 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1479 printf("MDT device: %s\n", status->ls_mdt_device);
1480 printf("Source: %s\n", status->ls_source);
1481 for (i = 0; i < status->ls_num_targets; i++)
1482 printf("Target: %s\n", status->ls_targets[i]);
1483 if (statuslog != NULL)
1484 printf("Statuslog: %s\n", statuslog);
1485 printf("Changelog registration: %s\n", status->ls_registration);
1486 printf("Starting changelog record: %jd\n",
1487 (uintmax_t)status->ls_last_recno);
1489 printf("Replicate xattrs: no\n");
1491 printf("Clear changelog after use: no\n");
1493 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1496 void lr_print_failure(struct lr_info *info, int rc)
1498 fprintf(stderr, "Replication of operation failed(%d):"
1499 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1500 changelog_type2str(info->type), info->type, info->tfid,
1501 info->pfid, info->name);
1504 /* Replicate filesystem operations from src_path to target_path */
1507 void *changelog_priv;
1508 struct lr_info *info;
1509 struct lr_info *ext = NULL;
1517 info = calloc(1, sizeof(struct lr_info));
1521 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1523 fprintf(stderr, "Source path is not a valid Lustre client "
1528 if (status->ls_mdt_device[0] == '\0') {
1531 len = snprintf(status->ls_mdt_device,
1532 sizeof(status->ls_mdt_device), "%s%s",
1533 status->ls_source_fs, DEFAULT_MDT);
1534 if (len >= sizeof(status->ls_mdt_device)) {
1540 ext = calloc(1, sizeof(struct lr_info));
1546 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1547 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1548 status->ls_targets[i], SPECIAL_DIR);
1549 rc = mkdir(info->dest, 0777);
1550 if (rc == -1 && errno != EEXIST) {
1551 fprintf(stderr, "Error writing to target path %s.\n",
1552 status->ls_targets[i]);
1556 rc = llistxattr(info->src, info->xlist, info->xsize);
1557 if (rc == -1 && errno == ENOTSUP) {
1558 fprintf(stderr, "xattrs not supported on %s\n",
1559 status->ls_targets[i]);
1563 if (xattr_not_supp == status->ls_num_targets)
1564 /* None of the targets support xattrs. */
1567 lr_print_status(info);
1569 /* Open changelogs for consumption*/
1570 rc = llapi_changelog_start(&changelog_priv,
1571 CHANGELOG_FLAG_BLOCK |
1572 CHANGELOG_FLAG_JOBID |
1573 CHANGELOG_FLAG_EXTRA_FLAGS,
1574 status->ls_mdt_device, status->ls_last_recno);
1576 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1577 status->ls_source_fs);
1581 rc = llapi_changelog_set_xflags(changelog_priv,
1582 CHANGELOG_EXTRA_FLAG_UIDGID |
1583 CHANGELOG_EXTRA_FLAG_NID |
1584 CHANGELOG_EXTRA_FLAG_OMODE |
1585 CHANGELOG_EXTRA_FLAG_XATTR);
1587 fprintf(stderr, "Error setting xflag in changelog for fs %s.\n",
1588 status->ls_source_fs);
1592 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1594 if (info->type == CL_RENAME && !info->is_extended) {
1595 /* Newer rename operations extends changelog to store
1596 * source file information, but old changelog has
1599 if (lr_parse_line(changelog_priv, ext) != 0)
1601 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1602 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1603 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1604 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1605 snprintf(info->sname, sizeof(info->sname), "%s",
1607 snprintf(info->name, sizeof(info->name), "%s",
1609 info->is_extended = 1;
1610 info->recno = ext->recno; /* For lr_clear_cl(). */
1616 lr_debug(DTRACE, "***** Start %lld %s (%d) %s %s %s *****\n",
1617 info->recno, changelog_type2str(info->type),
1618 info->type, info->tfid, info->pfid, info->name);
1620 switch(info->type) {
1625 rc = lr_create(info);
1629 rc = lr_remove(info);
1639 rc = lr_setattr(info);
1642 rc = lr_setxattr(info);
1651 /* Nothing needs to be done for these entries */
1657 lr_debug(DTRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n",
1658 info->recno, changelog_type2str(info->type),
1659 info->type, info->tfid, info->pfid, info->name, rc);
1661 if (rc && rc != -ENOENT) {
1662 lr_print_failure(info, rc);
1667 lr_clear_cl(info, 0);
1670 llapi_changelog_fini(&changelog_priv);
1672 if (errors || verbose)
1673 printf("Errors: %d\n", errors);
1675 /* Clear changelog records used so far */
1676 lr_clear_cl(info, 1);
1679 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1680 printf("Changelog records consumed: %lld\n", rec_count);
1695 termination_handler (int signum)
1697 /* Set a flag for the replicator to gracefully shutdown */
1699 printf("lustre_rsync halting.\n");
1702 int main(int argc, char *argv[])
1708 if ((rc = lr_init_status()) != 0)
1711 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1712 long_opts, NULL)) >= 0) {
1715 /* Assume absolute paths */
1719 /* Assume absolute paths */
1720 snprintf(status->ls_source, sizeof(status->ls_source),
1724 status->ls_num_targets++;
1726 if (numtargets != status->ls_num_targets) {
1727 /* Targets were read from a log
1728 file. The ones specified on the
1729 command line take precedence. The
1730 ones from the log file will be
1732 status->ls_num_targets = numtargets;
1734 newsize = sizeof (struct lustre_rsync_status) +
1735 (status->ls_num_targets * (PATH_MAX + 1));
1736 if (status->ls_size != newsize) {
1737 status->ls_size = newsize;
1738 status = lr_grow_buf(status, newsize);
1742 snprintf(status->ls_targets[status->ls_num_targets - 1],
1743 sizeof(status->ls_targets[0]), "%s", optarg);
1746 snprintf(status->ls_mdt_device,
1747 sizeof(status->ls_mdt_device),
1751 snprintf(status->ls_registration,
1752 sizeof(status->ls_registration),
1757 (void) lr_read_log();
1763 if (strcmp("no", optarg) == 0) {
1765 } else if (strcmp("yes", optarg) != 0) {
1766 printf("Invalid parameter %s. "
1767 "Specify --xattr=no or --xattr=yes\n",
1776 /* Undocumented option cl-clear */
1777 if (strcmp("no", optarg) == 0) {
1779 } else if (strcmp("yes", optarg) != 0) {
1780 printf("Invalid parameter %s. "
1781 "Specify --cl-clear=no "
1782 "or --cl-clear=yes\n",
1788 /* Undocumented option use-rsync */
1792 /* Undocumented option rsync-threshold */
1793 rsync_threshold = atol(optarg);
1796 /* Undocumented option start-recno */
1797 status->ls_last_recno = atol(optarg);
1800 /* Undocumented option debug */
1801 debug = atoi(optarg);
1802 if (debug < 0 || debug > 2)
1806 /* Undocumented option debug log file */
1807 if (debug_log != NULL)
1809 debug_log = fopen(optarg, "a");
1810 if (debug_log == NULL) {
1811 printf("Cannot open %s for debug log\n",
1817 fprintf(stderr, "error: %s: option '%s' "
1818 "unrecognized.\n", argv[0], argv[optind - 1]);
1824 if (status->ls_last_recno == -1)
1825 status->ls_last_recno = 0;
1826 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1827 /* No registration ID was passed in. */
1828 printf("Please specify changelog consumer registration id.\n");
1832 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1833 fprintf(stderr, "Please specify the source path.\n");
1837 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1838 fprintf(stderr, "Please specify the target path.\n");
1843 rc = lr_locate_rsync();
1844 if (use_rsync && rc != 0) {
1845 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1849 signal(SIGINT, termination_handler);
1850 signal(SIGHUP, termination_handler);
1851 signal(SIGTERM, termination_handler);
1853 rc = lr_replicate();
1855 if (debug_log != NULL)