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(const char *src, const 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) {
502 "cannot replicate xattrs from '%s' to '%s': %s\n",
503 info->src, info->dest,
510 start += strlen(info->xlist + start) + 1;
513 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
518 /* Retrieve the filesystem path for a given FID and a given
519 linkno. The path is returned in info->path */
520 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
522 long long recno = -1;
525 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
526 PATH_MAX, &recno, &linkno);
527 if (rc < 0 && rc != -ENOENT) {
528 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
529 status->ls_source, fidstr, -rc, strerror(errno = -rc));
535 /* Retrieve the filesystem path for a given FID. The path is returned
537 int lr_get_path(struct lr_info *info, char *fidstr)
539 return lr_get_path_ln(info, fidstr, 0);
542 /* Generate the path for opening by FID */
543 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
545 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
546 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
551 /* Read the symlink information into 'info->link' */
552 int lr_get_symlink(struct lr_info *info)
557 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
558 rc = readlink(info->src, info->linktmp, PATH_MAX);
560 info->linktmp[rc] = '\0';
563 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
565 if (strncmp(info->linktmp, status->ls_source,
566 strlen(status->ls_source)) == 0) {
567 /* Strip source fs path and replace with target fs path. */
568 link = info->linktmp + strlen(status->ls_source);
569 snprintf(info->src, sizeof(info->src), "%s%s",
570 status->ls_targets[info->target_no], link);
573 link = info->linktmp;
575 rc = snprintf(info->link, sizeof(info->link), "%s", link);
576 if (rc >= sizeof(info->link))
581 /* Create file/directory/device file/symlink. */
582 int lr_mkfile(struct lr_info *info)
588 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
589 if (info->type == CL_MKDIR) {
590 rc = mkdir(info->dest, 0777);
591 } else if (info->type == CL_SOFTLINK) {
592 lr_get_symlink(info);
593 rc = symlink(info->link, info->dest);
594 } else if (info->type == CL_MKNOD) {
595 lr_get_FID_PATH(status->ls_source, info->tfid,
596 info->src, PATH_MAX);
597 rc = stat(info->src, &st);
604 rc = mknod(info->dest, st.st_mode, st.st_rdev);
606 rc = mknod(info->dest, S_IFREG | 0777, 0);
616 /* Sync data and attributes */
617 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
618 lr_debug(DTRACE, "Syncing data and attributes %s\n",
620 (void) lr_copy_xattr(info);
621 if (info->type == CL_CREATE)
622 rc = lr_sync_data(info);
624 rc = lr_copy_attr(info->src, info->dest);
627 /* Source file has disappeared. Not an error. */
630 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
637 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
639 struct lr_parent_child_list *p;
642 p = calloc(1, sizeof(*p));
645 len = snprintf(p->pc_log.pcl_pfid, sizeof(p->pc_log.pcl_pfid),
647 if (len >= sizeof(p->pc_log.pcl_pfid))
649 len = snprintf(p->pc_log.pcl_tfid, sizeof(p->pc_log.pcl_tfid),
651 if (len >= sizeof(p->pc_log.pcl_tfid))
653 len = snprintf(p->pc_log.pcl_name, sizeof(p->pc_log.pcl_name),
655 if (len >= sizeof(p->pc_log.pcl_name))
658 p->pc_next = parents;
667 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
669 struct lr_parent_child_list *curr, *prev;
670 char d[4 * PATH_MAX + 1];
673 prev = curr = parents;
675 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
676 if (snprintf(d, sizeof(d), "%s/%s", dest,
677 curr->pc_log.pcl_name) >= sizeof(d)) {
678 fprintf(stderr, "Buffer truncated\n");
681 if (snprintf(info->src, sizeof(info->src), "%s/%s/%s",
682 status->ls_targets[info->target_no],
683 SPECIAL_DIR, curr->pc_log.pcl_tfid) >=
686 rc = rename(info->src, d);
688 fprintf(stderr, "Error renaming file "
690 info->src, d, errno);
694 parents = curr->pc_next;
696 prev->pc_next = curr->pc_next;
697 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
699 prev = curr = parents;
703 curr = curr->pc_next;
708 /* remove [info->spfid, info->sfid] from parents */
709 int lr_remove_pc(const char *pfid, const char *tfid)
711 struct lr_parent_child_list *curr, *prev;
713 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
714 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
715 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
717 parents = curr->pc_next;
719 prev->pc_next = curr->pc_next;
727 /* Create file under SPECIAL_DIR with its tfid as its name. */
728 int lr_mk_special(struct lr_info *info)
732 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
733 status->ls_targets[info->target_no], SPECIAL_DIR,
736 rc = lr_mkfile(info);
740 rc = lr_add_pc(info->pfid, info->tfid, info->name);
744 /* Remove a file or directory */
745 int lr_rmfile(struct lr_info *info)
749 if (info->type == CL_RMDIR)
750 rc = rmdir(info->dest);
752 rc = unlink(info->dest);
758 /* Recursively remove directory and its contents */
759 int lr_rm_recursive(struct lr_info *info)
768 extern char **environ;
772 status = callvpe("/bin/rm", args, environ);
775 else if (WIFEXITED(status))
776 rc = WEXITSTATUS(status) == 0 ? 0 : -EINVAL;
783 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
784 int lr_rm_special(struct lr_info *info)
788 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
789 status->ls_targets[info->target_no], SPECIAL_DIR,
791 rc = lr_rmfile(info);
794 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
795 info->dest, rc, errno);
799 /* Replicate file and directory create events */
800 int lr_create(struct lr_info *info)
807 /* Is target FID present on the source? */
808 rc = lr_get_path(info, info->tfid);
810 /* Source file has disappeared. Not an error. */
811 lr_debug(DINFO, "create: tfid %s not found on"
812 "source-fs\n", info->tfid);
817 strcpy(info->savedpath, info->path);
819 /* Is parent FID present on the source */
820 rc = lr_get_path(info, info->pfid);
822 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
829 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
830 len = strlen(info->path);
831 if (len == 1 && info->path[0] == '/')
832 snprintf(info->dest, sizeof(info->dest), "%s", info->name);
833 else if (len - 1 > 0 && info->path[len - 1] == '/')
834 snprintf(info->dest, sizeof(info->dest), "%s%s", info->path,
837 snprintf(info->dest, sizeof(info->dest), "%s/%s", info->path,
840 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
842 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
843 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
844 info->tfid, info->dest, info->savedpath);
848 /* Is f2p(pfid) present on the target? If not, the parent has
851 snprintf(info->dest, sizeof(info->dest), "%s/%s",
852 status->ls_targets[0], info->path);
853 if (access(info->dest, F_OK) != 0) {
854 lr_debug(DTRACE, "create: parent %s not found\n",
859 for (info->target_no = 0; info->target_no < status->ls_num_targets;
861 snprintf(info->dest, sizeof(info->dest), "%s/%s",
862 status->ls_targets[info->target_no], info->savedpath);
863 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
867 rc1 = lr_mkfile(info);
868 if (mkspecial || rc1 == -ENOENT) {
869 rc1 = lr_mk_special(info);
877 /* Replicate a file remove (rmdir/unlink) operation */
878 int lr_remove(struct lr_info *info)
883 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;
943 snprintf(info->dest, sizeof(info->dest), "%s/%s",
944 status->ls_targets[info->target_no],
946 if (access(info->dest, F_OK) != 0) {
949 snprintf(info->dest, sizeof(info->dest),
951 status->ls_targets[info->target_no],
952 info->path, info->name);
954 lr_debug(DINFO, "dest path %s rc_dest=%d\n", info->dest,
957 if (rc_dest == -ENOENT) {
958 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
959 status->ls_targets[info->target_no],
960 SPECIAL_DIR, info->sfid);
962 lr_debug(DINFO, "special dest %s\n", info->dest);
966 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
967 status->ls_targets[info->target_no],
968 srcpath, info->sname);
969 lr_debug(DINFO, "src path %s rc_src=%d\n", info->src,
972 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
974 snprintf(info->src, sizeof(info->src), "%s/%s/%s",
975 status->ls_targets[info->target_no],
976 SPECIAL_DIR, info->sfid);
978 lr_debug(DINFO, "special src %s\n", info->src);
983 if (strcmp(info->src, info->dest) != 0) {
984 rc1 = rename(info->src, info->dest);
987 lr_debug(DINFO, "rename returns %d\n", rc1);
991 rc1 = lr_remove_pc(info->spfid, info->sfid);
994 lr_cascade_move(info->sfid, info->dest, info);
996 rc1 = lr_add_pc(info->pfid, info->sfid, info->name);
998 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
999 info->src, info->dest, rc1, errno);
1006 /* Replicate a hard link */
1007 int lr_link(struct lr_info *info)
1014 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1015 rc = stat(info->src, &st);
1019 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1020 info->target_no++) {
1027 * The changelog record has the new parent directory FID and
1028 * name of the target file. So info->dest can be constructed
1029 * by getting the path of the new parent directory and
1030 * appending the target file name.
1032 rc1 = lr_get_path(info, info->pfid);
1033 lr_debug(rc1 ? 0 : DTRACE, "\tparent fid2path %s, %s, rc=%d\n",
1034 info->path, info->name, rc1);
1037 snprintf(info->dest, sizeof(info->dest), "%s/%s/%s",
1038 status->ls_targets[info->target_no],
1039 info->path, info->name);
1040 lr_debug(DINFO, "link destination is %s\n", info->dest);
1043 /* Search through the hardlinks to get the src */
1044 for (i = 0; i < st.st_nlink && info->src[0] == 0; i++) {
1047 rc1 = lr_get_path_ln(info, info->tfid, i);
1048 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
1049 info->path, 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()
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()
1256 char backupfile[PATH_MAX];
1260 snprintf(backupfile, sizeof(backupfile), "%s.old", statuslog);
1261 (void) rename(statuslog, backupfile);
1267 /* Save replication parameters to a statuslog. */
1272 size_t write_size = status->ls_size;
1273 struct lr_parent_child_list *curr;
1276 if (statuslog == NULL)
1281 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1284 fprintf(stderr, "Error opening log file for writing (%s)\n",
1289 size = write(fd, status, write_size);
1290 if (size != write_size) {
1291 fprintf(stderr, "Error writing to log file (%s) %d\n",
1297 for (curr = parents; curr; curr = curr->pc_next) {
1298 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1299 if (size != sizeof(curr->pc_log)) {
1300 fprintf(stderr, "Error writing to log file (%s) %d\n",
1310 /* Read statuslog and populate the replication parameters. Command
1311 * line parameters take precedence over parameters in the log file.*/
1314 struct lr_parent_child_list *tmp;
1315 struct lr_parent_child_log rec;
1316 struct lustre_rsync_status *s;
1319 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
1322 if (statuslog == NULL)
1325 s = calloc(1, read_size);
1331 fd = open(statuslog, O_RDONLY);
1337 size = read(fd, s, read_size);
1338 if (size != read_size) {
1343 if (read_size < s->ls_size) {
1344 read_size = s->ls_size;
1345 s = lr_grow_buf(s, read_size);
1351 if (lseek(fd, 0, SEEK_SET) == -1) {
1356 size = read(fd, s, read_size);
1357 if (size != read_size) {
1363 while (read(fd, &rec, sizeof(rec)) != 0) {
1364 tmp = calloc(1, sizeof(*tmp));
1371 tmp->pc_next = parents;
1375 /* copy uninitialized fields to status */
1376 if (status->ls_num_targets == 0) {
1377 if (status->ls_size != s->ls_size) {
1378 status = lr_grow_buf(status, s->ls_size);
1379 if (status == NULL) {
1384 status->ls_size = s->ls_size;
1386 status->ls_num_targets = s->ls_num_targets;
1387 memcpy(status->ls_targets, s->ls_targets,
1388 (PATH_MAX + 1) * s->ls_num_targets);
1390 if (status->ls_last_recno == -1)
1391 status->ls_last_recno = s->ls_last_recno;
1393 if (status->ls_registration[0] == '\0')
1394 snprintf(status->ls_registration,
1395 sizeof(status->ls_registration), "%s",
1396 s->ls_registration);
1398 if (status->ls_mdt_device[0] == '\0')
1399 snprintf(status->ls_mdt_device,
1400 sizeof(status->ls_mdt_device), "%s",
1403 if (status->ls_source_fs[0] == '\0')
1404 snprintf(status->ls_source_fs,
1405 sizeof(status->ls_source_fs), "%s",
1408 if (status->ls_source[0] == '\0')
1409 snprintf(status->ls_source,
1410 sizeof(status->ls_source), "%s",
1421 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1423 int lr_clear_cl(struct lr_info *info, int force)
1425 char mdt_device[LR_NAME_MAXLEN + 1];
1428 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1429 if (!noclear && !dryrun) {
1430 /* llapi_changelog_clear modifies the mdt
1431 * device name so make a copy of it until this
1434 snprintf(mdt_device, sizeof(mdt_device), "%s",
1435 status->ls_mdt_device);
1436 rc = llapi_changelog_clear(mdt_device,
1437 status->ls_registration,
1440 printf("Changelog clear (%s, %s, %lld) "
1441 "returned %d\n", status->ls_mdt_device,
1442 status->ls_registration, info->recno,
1446 if (!rc && !dryrun) {
1447 status->ls_last_recno = info->recno;
1455 /* Locate a usable version of rsync. At this point we'll use any
1457 int lr_locate_rsync()
1463 snprintf(rsync, sizeof(rsync), "%s -p %s", TYPE, RSYNC);
1464 fp = popen(rsync, "r");
1468 if (fgets(rsync, sizeof(rsync), fp) == NULL) {
1473 len = strlen(rsync);
1474 if (len > 0 && rsync[len - 1] == '\n')
1475 rsync[len - 1] = '\0';
1478 /* Determine the version of rsync */
1479 snprintf(rsync_ver, sizeof(rsync_ver), "%s --version", rsync);
1480 fp = popen(rsync_ver, "r");
1484 if (fgets(rsync_ver, sizeof(rsync_ver), fp) == NULL) {
1488 len = strlen(rsync_ver);
1489 if (len > 0 && rsync_ver[len - 1] == '\n')
1490 rsync_ver[len - 1] = '\0';
1497 /* Print the replication parameters */
1498 void lr_print_status(struct lr_info *info)
1505 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1506 printf("MDT device: %s\n", status->ls_mdt_device);
1507 printf("Source: %s\n", status->ls_source);
1508 for (i = 0; i < status->ls_num_targets; i++)
1509 printf("Target: %s\n", status->ls_targets[i]);
1510 if (statuslog != NULL)
1511 printf("Statuslog: %s\n", statuslog);
1512 printf("Changelog registration: %s\n", status->ls_registration);
1513 printf("Starting changelog record: %jd\n",
1514 (uintmax_t)status->ls_last_recno);
1516 printf("Replicate xattrs: no\n");
1518 printf("Clear changelog after use: no\n");
1520 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1523 void lr_print_failure(struct lr_info *info, int rc)
1525 fprintf(stderr, "Replication of operation failed(%d):"
1526 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1527 changelog_type2str(info->type), info->type, info->tfid,
1528 info->pfid, info->name);
1531 /* Replicate filesystem operations from src_path to target_path */
1534 void *changelog_priv;
1535 struct lr_info *info;
1536 struct lr_info *ext = NULL;
1544 info = calloc(1, sizeof(struct lr_info));
1548 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1550 fprintf(stderr, "Source path is not a valid Lustre client "
1555 if (status->ls_mdt_device[0] == '\0') {
1558 len = snprintf(status->ls_mdt_device,
1559 sizeof(status->ls_mdt_device), "%s%s",
1560 status->ls_source_fs, DEFAULT_MDT);
1561 if (len >= sizeof(status->ls_mdt_device)) {
1567 ext = calloc(1, sizeof(struct lr_info));
1573 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1574 snprintf(info->dest, sizeof(info->dest), "%s/%s",
1575 status->ls_targets[i], SPECIAL_DIR);
1576 rc = mkdir(info->dest, 0777);
1577 if (rc == -1 && errno != EEXIST) {
1578 fprintf(stderr, "Error writing to target path %s.\n",
1579 status->ls_targets[i]);
1583 rc = llistxattr(info->src, info->xlist, info->xsize);
1584 if (rc == -1 && errno == ENOTSUP) {
1585 fprintf(stderr, "xattrs not supported on %s\n",
1586 status->ls_targets[i]);
1590 if (xattr_not_supp == status->ls_num_targets)
1591 /* None of the targets support xattrs. */
1594 lr_print_status(info);
1596 /* Open changelogs for consumption*/
1597 rc = llapi_changelog_start(&changelog_priv,
1598 CHANGELOG_FLAG_BLOCK |
1599 CHANGELOG_FLAG_JOBID |
1600 CHANGELOG_FLAG_EXTRA_FLAGS,
1601 status->ls_mdt_device, status->ls_last_recno);
1603 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1604 status->ls_source_fs);
1608 rc = llapi_changelog_set_xflags(changelog_priv,
1609 CHANGELOG_EXTRA_FLAG_UIDGID |
1610 CHANGELOG_EXTRA_FLAG_NID |
1611 CHANGELOG_EXTRA_FLAG_OMODE |
1612 CHANGELOG_EXTRA_FLAG_XATTR);
1614 fprintf(stderr, "Error setting xflag in changelog for fs %s.\n",
1615 status->ls_source_fs);
1619 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1621 if (info->type == CL_RENAME && !info->is_extended) {
1622 /* Newer rename operations extends changelog to store
1623 * source file information, but old changelog has
1626 if (lr_parse_line(changelog_priv, ext) != 0)
1628 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1629 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1630 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1631 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1632 snprintf(info->sname, sizeof(info->sname), "%s",
1634 snprintf(info->name, sizeof(info->name), "%s",
1636 info->is_extended = 1;
1637 info->recno = ext->recno; /* For lr_clear_cl(). */
1643 lr_debug(DTRACE, "***** Start %lld %s (%d) %s %s %s *****\n",
1644 info->recno, changelog_type2str(info->type),
1645 info->type, info->tfid, info->pfid, info->name);
1647 switch(info->type) {
1652 rc = lr_create(info);
1656 rc = lr_remove(info);
1666 rc = lr_setattr(info);
1669 rc = lr_setxattr(info);
1678 /* Nothing needs to be done for these entries */
1684 lr_debug(DTRACE, "##### End %lld %s (%d) %s %s %s rc=%d #####\n",
1685 info->recno, changelog_type2str(info->type),
1686 info->type, info->tfid, info->pfid, info->name, rc);
1688 if (rc && rc != -ENOENT) {
1689 lr_print_failure(info, rc);
1694 lr_clear_cl(info, 0);
1697 llapi_changelog_fini(&changelog_priv);
1699 if (errors || verbose)
1700 printf("Errors: %d\n", errors);
1702 /* Clear changelog records used so far */
1703 lr_clear_cl(info, 1);
1706 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1707 printf("Changelog records consumed: %lld\n", rec_count);
1722 termination_handler (int signum)
1724 /* Set a flag for the replicator to gracefully shutdown */
1726 printf("lustre_rsync halting.\n");
1729 int main(int argc, char *argv[])
1735 if ((rc = lr_init_status()) != 0)
1738 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:D:",
1739 long_opts, NULL)) >= 0) {
1742 /* Assume absolute paths */
1746 /* Assume absolute paths */
1747 snprintf(status->ls_source, sizeof(status->ls_source),
1751 status->ls_num_targets++;
1753 if (numtargets != status->ls_num_targets) {
1754 /* Targets were read from a log
1755 file. The ones specified on the
1756 command line take precedence. The
1757 ones from the log file will be
1759 status->ls_num_targets = numtargets;
1761 newsize = sizeof (struct lustre_rsync_status) +
1762 (status->ls_num_targets * (PATH_MAX + 1));
1763 if (status->ls_size != newsize) {
1764 status->ls_size = newsize;
1765 status = lr_grow_buf(status, newsize);
1769 snprintf(status->ls_targets[status->ls_num_targets - 1],
1770 sizeof(status->ls_targets[0]), "%s", optarg);
1773 snprintf(status->ls_mdt_device,
1774 sizeof(status->ls_mdt_device),
1778 snprintf(status->ls_registration,
1779 sizeof(status->ls_registration),
1784 (void) lr_read_log();
1790 if (strcmp("no", optarg) == 0) {
1792 } else if (strcmp("yes", optarg) != 0) {
1793 printf("Invalid parameter %s. "
1794 "Specify --xattr=no or --xattr=yes\n",
1803 /* Undocumented option cl-clear */
1804 if (strcmp("no", optarg) == 0) {
1806 } else if (strcmp("yes", optarg) != 0) {
1807 printf("Invalid parameter %s. "
1808 "Specify --cl-clear=no "
1809 "or --cl-clear=yes\n",
1815 /* Undocumented option use-rsync */
1819 /* Undocumented option rsync-threshold */
1820 rsync_threshold = atol(optarg);
1823 /* Undocumented option start-recno */
1824 status->ls_last_recno = atol(optarg);
1827 /* Undocumented option debug */
1828 debug = atoi(optarg);
1829 if (debug < 0 || debug > 2)
1833 /* Undocumented option debug log file */
1834 if (debug_log != NULL)
1836 debug_log = fopen(optarg, "a");
1837 if (debug_log == NULL) {
1838 printf("Cannot open %s for debug log\n",
1844 fprintf(stderr, "error: %s: option '%s' "
1845 "unrecognized.\n", argv[0], argv[optind - 1]);
1851 if (status->ls_last_recno == -1)
1852 status->ls_last_recno = 0;
1853 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1854 /* No registration ID was passed in. */
1855 printf("Please specify changelog consumer registration id.\n");
1859 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1860 fprintf(stderr, "Please specify the source path.\n");
1864 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1865 fprintf(stderr, "Please specify the target path.\n");
1870 rc = lr_locate_rsync();
1871 if (use_rsync && rc != 0) {
1872 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1876 signal(SIGINT, termination_handler);
1877 signal(SIGHUP, termination_handler);
1878 signal(SIGTERM, termination_handler);
1880 rc = lr_replicate();
1882 if (debug_log != NULL)