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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/utils/lustre_rsync.c
36 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
37 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
41 * - lustre_rsync is a tool for replicating a lustre filesystem.
43 * - The source-fs is a live lustre filesystem. It is not a
44 * snapshot. It is mounted and undergoing changes
46 * - The target-fs is a copy of the source-fs from the past. Let's
47 * call this point, the 'sync point'.
49 * - There is a changelog of all metadata operations that happened on
50 * the filesystem since the 'sync point'.
52 * - lustre_rsync replicates all the operations saved in the changelog
53 * on to the target filesystem to make it identical to the source.
55 * To facilitate replication, the lustre filesystem provides
56 * a) a way to get the current filesystem path of a given FID
57 * b) a way to open files by specifying its FID
59 * The changelog only has a limited amount of information.
60 * tfid - The FID of the target file
61 * pfid - The FID of the parent of the target file (at the time of
63 * sfid - The FID of the source file
64 * spfid - The FID of the parent of the source file
65 * name - The name of the target file (at the time of the operation), the name
66 * of the source file is appended (delimited with '\0') if this
67 * operation involves a source
69 * With just this information, it is not alwasy possible to determine
70 * the file paths for each operation. For instance, if pfid does not
71 * exist on the source-fs (due to a subsequent deletion), its path
72 * cannot be queried. In such cases, lustre_rsync keeps the files in a
73 * special directory ("/.lustrerepl"). Once all the operations in a
74 * changelog are replayed, all the files in this special directory
75 * will get moved to the location as in the source-fs.
77 * Shorthand used: f2p(fid) = fid2path(fid)
79 * The following are the metadata operations of interest.
81 * If tfid is absent on the source-fs, ignore this operation
82 * If pfid is absent on the source-fs [or]
83 * if f2p(pfid) is not present on target-fs [or]
84 * if f2p(pfid)+name != f2p(tfid)
85 * creat .lustrerepl/tfid
86 * track [pfid,tfid,name]
91 * If .lustrerepl/[tfid] is present on the target
92 * rm .lustrerepl/[tfid]
93 * Else if pfid is present on the source-fs,
94 * if f2p(pfid)+name is present,
97 * 3. move (spfid,sname) to (pfid,name)
99 * if spfid is also present, mv (spfid,sname) to (pfid,name)
100 * else mv .lustrerepl/[sfid] to (pfid,name)
101 * Else if pfid is not present,
102 * if spfid is present, mv (spfid,sname) .lustrerepl/[sfid]
103 * If moving out of .lustrerepl
104 * move out all its children in .lustrerepl.
105 * [pfid,tfid,name] tracked from (1) is used for this.
115 #include <sys/stat.h>
116 #include <sys/types.h>
120 #include <sys/xattr.h>
122 #include <libcfs/libcfs.h>
123 #include <libcfs/libcfsutil.h>
124 #include <lustre/liblustreapi.h>
125 #include <lustre/lustre_idl.h>
126 #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"
143 /* Not used; declared for fulfilling obd.c's dependency. */
144 command_t cmdlist[0];
145 extern int obd_initialize(int argc, char **argv);
147 /* Information for processing a changelog record. This structure is
148 allocated on the heap instead of allocating large variables on the
153 unsigned int is_extended:1;
154 enum changelog_rec_type type;
155 char tfid[LR_FID_STR_LEN];
156 char pfid[LR_FID_STR_LEN];
157 char sfid[LR_FID_STR_LEN];
158 char spfid[LR_FID_STR_LEN];
159 char sname[NAME_MAX + 1];
160 char name[NAME_MAX + 1];
161 char src[PATH_MAX + 1];
162 char dest[PATH_MAX + 1];
163 char path[PATH_MAX + 1];
164 char savedpath[PATH_MAX + 1];
165 char link[PATH_MAX + 1];
166 char linktmp[PATH_MAX + 1];
171 /* Variables for querying the xattributes */
178 struct lr_parent_child_list {
179 struct lr_parent_child_log pc_log;
180 struct lr_parent_child_list *pc_next;
183 struct lustre_rsync_status *status;
184 char *statuslog; /* Name of the status log file */
186 int noxattr; /* Flag to turn off replicating xattrs */
187 int noclear; /* Flag to turn off clearing changelogs */
188 int debug; /* Flag to turn debugging information on and off */
189 int verbose; /* Verbose output */
190 long long rec_count; /* No of changelog records that were processed */
193 int use_rsync; /* Flag to turn on use of rsync to copy data */
194 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
195 int quit; /* Flag to stop processing the changelog; set on the
196 receipt of a signal */
197 int abort_on_err = 0;
199 char rsync[PATH_MAX];
200 char rsync_ver[PATH_MAX];
201 struct lr_parent_child_list *parents;
203 /* Command line options */
204 struct option long_opts[] = {
205 {"source", required_argument, 0, 's'},
206 {"target", required_argument, 0, 't'},
207 {"mdt", required_argument, 0, 'm'},
208 {"user", required_argument, 0, 'u'},
209 {"statuslog", required_argument, 0, 'l'},
210 {"verbose", no_argument, 0, 'v'},
211 {"xattr", required_argument, 0, 'x'},
212 {"dry-run", no_argument, 0, 'z'},
213 /* Undocumented options follow */
214 {"cl-clear", required_argument, 0, 'c'},
215 {"use-rsync", no_argument, 0, 'r'},
216 {"rsync-threshold", required_argument, 0, 'y'},
217 {"start-recno", required_argument, 0, 'n'},
218 {"abort-on-err",no_argument, 0, 'a'},
219 {"debug", required_argument, 0, 'd'},
223 /* Command line usage */
226 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
227 "-m <mdt> -r <user id> -l <status log>\n"
228 "lustre_rsync can also pick up parameters from a "
229 "status log created earlier.\n"
230 "\tlustre_rsync -l <log_file>\n"
232 "\t--xattr <yes|no> replicate EAs\n"
233 "\t--abort-on-err abort at first err\n"
235 "\t--dry-run don't write anything\n");
238 /* Print debug information. This is controlled by the value of the
239 global variable 'debug' */
240 void lr_debug(int level, const char *fmt, ...)
253 void * lr_grow_buf(void *buf, int size)
257 ptr = realloc(buf, size);
264 /* Use rsync to replicate file data */
265 int lr_rsync_data(struct lr_info *info)
268 struct stat st_src, st_dest;
271 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
273 rc = stat(info->src, &st_src);
275 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
276 info->src, info->name);
282 rc = stat(info->dest, &st_dest);
284 fprintf(stderr, "Error: Unable to stat dest=%s\n",
289 if (st_src.st_mtime != st_dest.st_mtime ||
290 st_src.st_size != st_dest.st_size) {
291 /* XXX spawning off an rsync for every data sync and
292 * waiting synchronously is bad for performance.
293 * librsync could possibly used here. But it does not
294 * seem to be of production grade. Multi-threaded
295 * replication is also to be considered.
298 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
300 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
301 status = system(cmd);
304 } else if (WIFEXITED(status)) {
305 status = WEXITSTATUS(status);
308 else if (status == 23 || status == 24)
309 /* Error due to vanished source files;
315 lr_debug(DINFO, "rsync %s exited with %d %d\n",
316 info->src, status, rc);
321 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
322 info->dest, info->tfid);
328 int lr_copy_data(struct lr_info *info)
338 fd_src = open(info->src, O_RDONLY);
341 if (fstat(fd_src, &st_src) == -1 ||
342 stat(info->dest, &st_dest) == -1)
345 if (st_src.st_mtime == st_dest.st_mtime &&
346 st_src.st_size == st_dest.st_size)
349 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
350 /* It is more efficient to use rsync to replicate
351 large files. Any file larger than rsync_threshold
352 is handed off to rsync. */
353 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
354 rc = lr_rsync_data(info);
358 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
363 bufsize = st_dest.st_blksize;
365 if (info->bufsize < bufsize) {
367 info->buf = lr_grow_buf(info->buf, bufsize);
368 if (info->buf == NULL) {
372 info->bufsize = bufsize;
376 rsize = read(fd_src, info->buf, bufsize);
379 } else if (rsize < 0) {
384 if (write(fd_dest, info->buf, rsize) != rsize) {
402 /* Copy data from source to destination */
403 int lr_sync_data(struct lr_info *info)
406 return lr_rsync_data(info);
408 return lr_copy_data(info);
411 /* Copy all attributes from file src to file dest */
412 int lr_copy_attr(char *src, char *dest)
417 if (stat(src, &st) == -1 ||
418 chmod(dest, st.st_mode) == -1 ||
419 chown(dest, st.st_uid, st.st_gid) == -1)
422 time.actime = st.st_atime;
423 time.modtime = st.st_mtime;
424 if (utime(dest, &time) == -1)
429 /* Copy all xattrs from file info->src to info->dest */
430 int lr_copy_xattr(struct lr_info *info)
432 size_t size = info->xsize;
441 rc = llistxattr(info->src, info->xlist, size);
442 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
443 info->src, info->xlist, rc, errno);
444 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
445 size = rc > PATH_MAX ? rc : PATH_MAX;
446 info->xlist = lr_grow_buf(info->xlist, size);
447 if (info->xlist == NULL)
450 rc = llistxattr(info->src, info->xlist, size);
451 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
452 info->src, rc, errno);
459 while (start < len) {
461 rc = lgetxattr(info->src, info->xlist + start,
463 if (info->xvalue == NULL || errno == ERANGE) {
464 size = rc > PATH_MAX ? rc : PATH_MAX;
465 info->xvalue = lr_grow_buf(info->xvalue, size);
466 if (info->xvalue == NULL)
469 rc = lgetxattr(info->src, info->xlist + start,
472 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
476 rc = lsetxattr(info->dest, info->xlist + start,
477 info->xvalue, size, 0);
478 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
481 if (errno != ENOTSUP) {
482 fprintf(stderr, "Error replicating "
483 " xattr for %s: %d\n",
490 start += strlen(info->xlist + start) + 1;
493 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
498 /* Retrieve the filesystem path for a given FID and a given
499 linkno. The path is returned in info->path */
500 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
502 long long recno = -1;
505 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
506 PATH_MAX, &recno, &linkno);
507 if (rc < 0 && rc != -ENOENT) {
508 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
509 status->ls_source, fidstr, -rc, strerror(errno = -rc));
515 /* Retrieve the filesystem path for a given FID. The path is returned
517 int lr_get_path(struct lr_info *info, char *fidstr)
519 return lr_get_path_ln(info, fidstr, 0);
522 /* Generate the path for opening by FID */
523 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
525 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
526 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
531 /* Read the symlink information into 'info->link' */
532 int lr_get_symlink(struct lr_info *info)
537 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
538 rc = readlink(info->src, info->linktmp, PATH_MAX);
540 info->linktmp[rc] = '\0';
543 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
545 if (strncmp(info->linktmp, status->ls_source,
546 strlen(status->ls_source)) == 0) {
547 /* Strip source fs path and replace with target fs path. */
548 link = info->linktmp + strlen(status->ls_source);
549 snprintf(info->src, PATH_MAX, "%s%s",
550 status->ls_targets[info->target_no], link);
553 link = info->linktmp;
555 strncpy(info->link, link, PATH_MAX);
556 info->link[PATH_MAX] = '\0';
561 /* Create file/directory/device file/symlink. */
562 int lr_mkfile(struct lr_info *info)
568 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
569 if (info->type == CL_MKDIR) {
570 rc = mkdir(info->dest, 0777);
571 } else if (info->type == CL_SOFTLINK) {
572 lr_get_symlink(info);
573 rc = symlink(info->link, info->dest);
574 } else if (info->type == CL_MKNOD) {
575 lr_get_FID_PATH(status->ls_source, info->tfid,
576 info->src, PATH_MAX);
577 rc = stat(info->src, &st);
584 rc = mknod(info->dest, st.st_mode, st.st_rdev);
586 rc = mknod(info->dest, S_IFREG | 0777, 0);
591 /* Sync data and attributes */
592 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
593 lr_debug(DTRACE, "Syncing data and attributes %s\n",
595 (void) lr_copy_xattr(info);
596 if (info->type == CL_CREATE)
597 rc = lr_sync_data(info);
599 rc = lr_copy_attr(info->src, info->dest);
602 /* Source file has disappeared. Not an error. */
605 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
612 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
614 struct lr_parent_child_list *p;
616 p = calloc(1, sizeof(*p));
619 strcpy(p->pc_log.pcl_pfid, pfid);
620 strcpy(p->pc_log.pcl_tfid, tfid);
621 strcpy(p->pc_log.pcl_name, name);
623 p->pc_next = parents;
628 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
630 struct lr_parent_child_list *curr, *prev;
634 d = calloc(1, PATH_MAX + 1);
635 prev = curr = parents;
637 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
638 snprintf(d, PATH_MAX, "%s/%s", dest,
639 curr->pc_log.pcl_name);
640 snprintf(info->src, PATH_MAX, "%s/%s/%s",
641 status->ls_targets[info->target_no],
642 SPECIAL_DIR, curr->pc_log.pcl_tfid);
643 rc = rename(info->src, d);
645 fprintf(stderr, "Error renaming file "
647 info->src, d, errno);
650 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
652 parents = curr->pc_next;
654 prev->pc_next = curr->pc_next;
656 prev = curr = parents;
660 curr = curr->pc_next;
667 /* remove [info->spfid, info->sfid] from parents */
668 int lr_remove_pc(const char *pfid, const char *tfid)
670 struct lr_parent_child_list *curr, *prev;
672 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
673 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
674 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
676 parents = curr->pc_next;
678 prev->pc_next = curr->pc_next;
686 /* Create file under SPECIAL_DIR with its tfid as its name. */
687 int lr_mk_special(struct lr_info *info)
691 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
692 status->ls_targets[info->target_no], SPECIAL_DIR,
695 rc = lr_mkfile(info);
699 rc = lr_add_pc(info->pfid, info->tfid, info->name);
703 /* Remove a file or directory */
704 int lr_rmfile(struct lr_info *info)
708 if (info->type == CL_RMDIR)
709 rc = rmdir(info->dest);
711 rc = unlink(info->dest);
717 /* Recursively remove directory and its contents */
718 int lr_rm_recursive(struct lr_info *info)
722 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
723 rc = system(info->cmd);
730 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
731 int lr_rm_special(struct lr_info *info)
735 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
736 status->ls_targets[info->target_no], SPECIAL_DIR,
738 rc = lr_rmfile(info);
741 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
742 info->dest, rc, errno);
746 /* Replicate file and directory create events */
747 int lr_create(struct lr_info *info)
754 /* Is target FID present on the source? */
755 rc = lr_get_path(info, info->tfid);
757 /* Source file has disappeared. Not an error. */
758 lr_debug(DINFO, "create: tfid %s not found on"
759 "source-fs\n", info->tfid);
764 strcpy(info->savedpath, info->path);
766 /* Is parent FID present on the source */
767 rc = lr_get_path(info, info->pfid);
769 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
776 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
777 len = strlen(info->path);
778 if (len == 1 && info->path[0] == '/')
779 snprintf(info->dest, PATH_MAX, "%s", info->name);
780 else if (len - 1 > 0 && info->path[len - 1] == '/')
781 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
783 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
785 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
787 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
788 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
789 info->tfid, info->dest, info->savedpath);
793 /* Is f2p(pfid) present on the target? If not, the parent has
796 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
798 if (access(info->dest, F_OK) != 0) {
799 lr_debug(DTRACE, "create: parent %s not found\n",
804 for (info->target_no = 0; info->target_no < status->ls_num_targets;
806 snprintf(info->dest, PATH_MAX, "%s/%s",
807 status->ls_targets[info->target_no], info->savedpath);
808 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
812 rc1 = lr_mkfile(info);
813 if (mkspecial || rc1 == -ENOENT) {
814 rc1 = lr_mk_special(info);
822 /* Replicate a file remove (rmdir/unlink) operation */
823 int lr_remove(struct lr_info *info)
828 for (info->target_no = 0; info->target_no < status->ls_num_targets;
831 rc1 = lr_rm_special(info);
835 rc1 = lr_get_path(info, info->pfid);
836 if (rc1 == -ENOENT) {
837 lr_debug(DINFO, "remove: pfid %s not found\n",
845 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
846 status->ls_targets[info->target_no], info->path,
849 rc1 = lr_rmfile(info);
850 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
851 info->dest, rc1, errno);
852 if (rc1 == -ENOTEMPTY)
853 rc1 = lr_rm_recursive(info);
863 /* Replicate a rename/move operation. */
864 int lr_move(struct lr_info *info)
870 int special_dest = 0;
871 char srcpath[PATH_MAX + 1] = "";
873 LASSERT(info->is_extended);
875 rc_src = lr_get_path(info, info->spfid);
876 if (rc_src < 0 && rc_src != -ENOENT)
878 memcpy(srcpath, info->path, strlen(info->path));
880 rc_dest = lr_get_path(info, info->pfid);
881 if (rc_dest < 0 && rc_dest != -ENOENT)
884 for (info->target_no = 0; info->target_no < status->ls_num_targets;
888 snprintf(info->dest, PATH_MAX, "%s/%s",
889 status->ls_targets[info->target_no],
891 if (access(info->dest, F_OK) != 0) {
894 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
895 status->ls_targets[info->target_no],
896 info->path, info->name);
899 if (rc_dest == -ENOENT) {
900 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
901 status->ls_targets[info->target_no],
902 SPECIAL_DIR, info->sfid);
907 snprintf(info->src, PATH_MAX, "%s/%s/%s",
908 status->ls_targets[info->target_no],
909 srcpath, info->sname);
910 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
912 snprintf(info->src, PATH_MAX, "%s/%s/%s",
913 status->ls_targets[info->target_no],
914 SPECIAL_DIR, info->sfid);
919 if (strcmp(info->src, info->dest) != 0) {
920 rc1 = rename(info->src, info->dest);
926 lr_remove_pc(info->spfid, info->sfid);
928 lr_cascade_move(info->sfid, info->dest, info);
931 lr_add_pc(info->pfid, info->sfid, info->name);
933 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
934 info->src, info->dest, rc1, errno);
941 /* Replicate a hard link */
942 int lr_link(struct lr_info *info)
950 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
951 rc = stat(info->src, &st);
955 for (info->target_no = 0; info->target_no < status->ls_num_targets;
962 /* Search through the hardlinks to get the src and dest */
963 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
964 info->dest[0] == 0); i++) {
965 rc1 = lr_get_path_ln(info, info->tfid, i);
966 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
967 info->path, info->name, i, rc1);
971 len = strlen(info->path) - strlen(info->name);
972 if (len > 0 && strcmp(info->path + len,
974 snprintf(info->dest, PATH_MAX, "%s/%s",
975 status->ls_targets[info->target_no],
977 else if (info->src[0] == 0)
978 snprintf(info->src, PATH_MAX, "%s/%s",
979 status->ls_targets[info->target_no],
988 if (info->src[0] == 0 || info->dest[0] == 0)
989 /* Could not find the source or destination.
990 This can happen when some links don't exist
994 if (info->src[0] == 0)
995 snprintf(info->src, PATH_MAX, "%s/%s/%s",
996 status->ls_targets[info->target_no],
997 SPECIAL_DIR, info->tfid);
998 else if (info->dest[0] == 0)
999 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
1000 status->ls_targets[info->target_no],
1001 SPECIAL_DIR, info->tfid);
1003 rc1 = link(info->src, info->dest);
1004 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
1005 info->src, info->dest, rc1, strerror(errno));
1013 /* Replicate file attributes */
1014 int lr_setattr(struct lr_info *info)
1019 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1021 rc = lr_get_path(info, info->tfid);
1023 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1028 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1029 info->target_no++) {
1031 snprintf(info->dest, PATH_MAX, "%s/%s",
1032 status->ls_targets[info->target_no], info->path);
1033 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1036 rc1 = lr_sync_data(info);
1038 rc1 = lr_copy_attr(info->src, info->dest);
1045 /* Replicate xattrs */
1046 int lr_setxattr(struct lr_info *info)
1050 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1052 rc = lr_get_path(info, info->tfid);
1054 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1059 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1060 info->target_no++) {
1062 snprintf(info->dest, PATH_MAX, "%s/%s",
1063 status->ls_targets[info->target_no], info->path);
1064 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1067 rc1 = lr_copy_xattr(info);
1075 /* Parse a line of changelog entry */
1076 int lr_parse_line(void *priv, struct lr_info *info)
1078 struct changelog_ext_rec *rec;
1080 if (llapi_changelog_recv(priv, &rec) != 0)
1083 info->is_extended = CHANGELOG_REC_EXTENDED(rec);
1084 info->recno = rec->cr_index;
1085 info->type = rec->cr_type;
1086 sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
1087 sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
1088 strncpy(info->name, rec->cr_name, rec->cr_namelen);
1090 if (fid_is_sane(&rec->cr_sfid)) {
1091 sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
1092 sprintf(info->spfid, DFID, PFID(&rec->cr_spfid));
1093 strncpy(info->sname, changelog_rec_sname(rec),
1094 changelog_rec_snamelen(rec));
1095 info->sname[changelog_rec_snamelen(rec)] = '\0';
1098 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1099 info->name, info->sname);
1101 info->name[rec->cr_namelen] = '\0';
1104 printf("Rec %lld: %d %s\n", info->recno, info->type,
1108 llapi_changelog_free(&rec);
1114 /* Initialize the replication parameters */
1115 int lr_init_status()
1117 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1121 status = calloc(size, 1);
1124 status->ls_version = REPLICATE_STATUS_VER;
1125 status->ls_size = size;
1126 status->ls_last_recno = -1;
1130 /* Make a backup of the statuslog */
1131 void lr_backup_log()
1133 char backupfile[PATH_MAX];
1137 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1138 (void) rename(statuslog, backupfile);
1144 /* Save replication parameters to a statuslog. */
1149 size_t write_size = status->ls_size;
1150 struct lr_parent_child_list *curr;
1153 if (statuslog == NULL)
1158 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1161 fprintf(stderr, "Error opening log file for writing (%s)\n",
1166 size = write(fd, status, write_size);
1167 if (size != write_size) {
1168 fprintf(stderr, "Error writing to log file (%s) %d\n",
1174 for (curr = parents; curr; curr = curr->pc_next) {
1175 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1176 if (size != sizeof(curr->pc_log)) {
1177 fprintf(stderr, "Error writing to log file (%s) %d\n",
1187 /* Read statuslog and populate the replication parameters. Command
1188 * line parameters take precedence over parameters in the log file.*/
1191 struct lr_parent_child_list *tmp;
1192 struct lr_parent_child_log rec;
1193 struct lustre_rsync_status *s;
1196 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1199 if (statuslog == NULL)
1202 s = calloc(1, read_size);
1204 GOTO(out, rc = -ENOMEM);
1206 fd = open(statuslog, O_RDONLY);
1208 GOTO(out, rc = -errno);
1209 size = read(fd, s, read_size);
1210 if (size != read_size)
1211 GOTO(out, rc = -EINVAL);
1212 if (read_size < s->ls_size) {
1213 read_size = s->ls_size;
1214 s = lr_grow_buf(s, read_size);
1216 GOTO(out, rc = -ENOMEM);
1217 if (lseek(fd, 0, SEEK_SET) == -1)
1218 GOTO(out, rc = -errno);
1219 size = read(fd, s, read_size);
1220 if (size != read_size)
1221 GOTO(out, rc = -EINVAL);
1224 while (read(fd, &rec, sizeof(rec)) != 0) {
1225 tmp = calloc(1, sizeof(*tmp));
1227 GOTO(out, rc = -ENOMEM);
1229 tmp->pc_next = parents;
1233 /* copy uninitialized fields to status */
1234 if (status->ls_num_targets == 0) {
1235 if (status->ls_size != s->ls_size) {
1236 status = lr_grow_buf(status, s->ls_size);
1238 GOTO(out, rc = -ENOMEM);
1239 status->ls_size = s->ls_size;
1241 status->ls_num_targets = s->ls_num_targets;
1242 memcpy(status->ls_targets, s->ls_targets,
1243 PATH_MAX * s->ls_num_targets);
1245 if (status->ls_last_recno == -1)
1246 status->ls_last_recno = s->ls_last_recno;
1248 if (status->ls_registration[0] == '\0')
1249 strncpy(status->ls_registration, s->ls_registration,
1252 if (status->ls_mdt_device[0] == '\0')
1253 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1256 if (status->ls_source_fs[0] == '\0')
1257 strncpy(status->ls_source_fs, s->ls_source_fs,
1260 if (status->ls_source[0] == '\0')
1261 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1271 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1273 int lr_clear_cl(struct lr_info *info, int force)
1275 char mdt_device[LR_NAME_MAXLEN + 1];
1279 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1280 if (info->type == CL_RENAME)
1281 rec = info->recno + 1;
1284 if (!noclear && !dryrun) {
1285 /* llapi_changelog_clear modifies the mdt
1286 * device name so make a copy of it until this
1289 strncpy(mdt_device, status->ls_mdt_device,
1291 rc = llapi_changelog_clear(mdt_device,
1292 status->ls_registration,
1295 printf("Changelog clear (%s, %s, %lld) "
1296 "returned %d\n", status->ls_mdt_device,
1297 status->ls_registration, rec, rc);
1299 if (!rc && !dryrun) {
1300 status->ls_last_recno = rec;
1309 /* Locate a usable version of rsync. At this point we'll use any
1311 int lr_locate_rsync()
1317 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1318 fp = popen(rsync, "r");
1322 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1327 len = strlen(rsync);
1328 if (len > 0 && rsync[len - 1] == '\n')
1329 rsync[len - 1] = '\0';
1332 /* Determine the version of rsync */
1333 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1334 fp = popen(rsync_ver, "r");
1338 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1342 len = strlen(rsync_ver);
1343 if (len > 0 && rsync_ver[len - 1] == '\n')
1344 rsync_ver[len - 1] = '\0';
1351 /* Print the replication parameters */
1352 void lr_print_status(struct lr_info *info)
1359 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1360 printf("MDT device: %s\n", status->ls_mdt_device);
1361 printf("Source: %s\n", status->ls_source);
1362 for (i = 0; i < status->ls_num_targets; i++)
1363 printf("Target: %s\n", status->ls_targets[i]);
1364 if (statuslog != NULL)
1365 printf("Statuslog: %s\n", statuslog);
1366 printf("Changelog registration: %s\n", status->ls_registration);
1367 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1369 printf("Replicate xattrs: no\n");
1371 printf("Clear changelog after use: no\n");
1373 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1376 void lr_print_failure(struct lr_info *info, int rc)
1378 fprintf(stderr, "Replication of operation failed(%d):"
1379 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1380 changelog_type2str(info->type), info->type, info->tfid,
1381 info->pfid, info->name);
1384 /* Replicate filesystem operations from src_path to target_path */
1387 void *changelog_priv;
1388 struct lr_info *info;
1389 struct lr_info *ext = NULL;
1397 info = calloc(1, sizeof(struct lr_info));
1401 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1403 fprintf(stderr, "Source path is not a valid Lustre client "
1407 if (status->ls_mdt_device[0] == '\0')
1408 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1409 status->ls_source_fs, DEFAULT_MDT);
1411 ext = calloc(1, sizeof(struct lr_info));
1417 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1418 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1420 rc = mkdir(info->dest, 0777);
1421 if (rc == -1 && errno != EEXIST) {
1422 fprintf(stderr, "Error writing to target path %s.\n",
1423 status->ls_targets[i]);
1427 rc = llistxattr(info->src, info->xlist, info->xsize);
1428 if (rc == -1 && errno == ENOTSUP) {
1429 fprintf(stderr, "xattrs not supported on %s\n",
1430 status->ls_targets[i]);
1434 if (xattr_not_supp == status->ls_num_targets)
1435 /* None of the targets support xattrs. */
1438 lr_print_status(info);
1440 /* Open changelogs for consumption*/
1441 rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
1442 status->ls_source_fs, status->ls_last_recno);
1444 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1445 status->ls_source_fs);
1449 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1451 if (info->type == CL_RENAME && !info->is_extended) {
1452 /* Newer rename operations extends changelog to store
1453 * source file information, but old changelog has
1456 if (lr_parse_line(changelog_priv, ext) != 0)
1458 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1459 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1460 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1461 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1462 strncpy(info->sname, info->name, sizeof(info->sname));
1463 strncpy(info->name, ext->name, sizeof(info->name));
1464 info->is_extended = 1;
1470 switch(info->type) {
1475 rc = lr_create(info);
1479 rc = lr_remove(info);
1489 rc = lr_setattr(info);
1492 rc = lr_setxattr(info);
1499 /* Nothing needs to be done for these entries */
1503 if (rc && rc != -ENOENT) {
1504 lr_print_failure(info, rc);
1509 lr_clear_cl(info, 0);
1511 bzero(info, sizeof(struct lr_info));
1512 bzero(ext, sizeof(struct lr_info));
1516 llapi_changelog_fini(&changelog_priv);
1518 if (errors || verbose)
1519 printf("Errors: %d\n", errors);
1521 /* Clear changelog records used so far */
1522 lr_clear_cl(info, 1);
1525 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1526 printf("Changelog records consumed: %lld\n", rec_count);
1541 termination_handler (int signum)
1543 /* Set a flag for the replicator to gracefully shutdown */
1545 printf("lustre_rsync halting.\n");
1548 int main(int argc, char *argv[])
1554 if ((rc = lr_init_status()) != 0)
1557 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:",
1558 long_opts, NULL)) >= 0) {
1561 /* Assume absolute paths */
1565 /* Assume absolute paths */
1566 strncpy(status->ls_source, optarg, PATH_MAX);
1569 status->ls_num_targets++;
1571 if (numtargets != status->ls_num_targets) {
1572 /* Targets were read from a log
1573 file. The ones specified on the
1574 command line take precedence. The
1575 ones from the log file will be
1577 status->ls_num_targets = numtargets;
1579 newsize = sizeof (struct lustre_rsync_status) +
1580 (status->ls_num_targets * PATH_MAX);
1581 if (status->ls_size != newsize) {
1582 status->ls_size = newsize;
1583 status = lr_grow_buf(status, newsize);
1587 strncpy(status->ls_targets[status->ls_num_targets - 1],
1592 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1595 strncpy(status->ls_registration, optarg,
1600 (void) lr_read_log();
1606 if (strcmp("no", optarg) == 0) {
1608 } else if (strcmp("yes", optarg) != 0) {
1609 printf("Invalid parameter %s. "
1610 "Specify --xattr=no or --xattr=yes\n",
1619 /* Undocumented option cl-clear */
1620 if (strcmp("no", optarg) == 0) {
1622 } else if (strcmp("yes", optarg) != 0) {
1623 printf("Invalid parameter %s. "
1624 "Specify --cl-clear=no "
1625 "or --cl-clear=yes\n",
1631 /* Undocumented option use-rsync */
1635 /* Undocumented option rsync-threshold */
1636 rsync_threshold = atol(optarg);
1639 /* Undocumented option start-recno */
1640 status->ls_last_recno = atol(optarg);
1643 /* Undocumented option debug */
1644 debug = atoi(optarg);
1645 if (debug < 0 || debug > 2)
1649 fprintf(stderr, "error: %s: option '%s' "
1650 "unrecognized.\n", argv[0], argv[optind - 1]);
1656 if (status->ls_last_recno == -1)
1657 status->ls_last_recno = 0;
1658 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1659 /* No registration ID was passed in. */
1660 printf("Please specify changelog consumer registration id.\n");
1664 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1665 fprintf(stderr, "Please specify the source path.\n");
1669 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1670 fprintf(stderr, "Please specify the target path.\n");
1675 /* This plumbing is needed for some of the ioctls behind
1676 llapi calls to work. */
1677 if (obd_initialize(argc, argv) < 0) {
1678 fprintf(stderr, "obd_initialize failed.\n");
1682 rc = lr_locate_rsync();
1683 if (use_rsync && rc != 0) {
1684 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1688 signal(SIGINT, termination_handler);
1689 signal(SIGHUP, termination_handler);
1690 signal(SIGTERM, termination_handler);
1692 rc = lr_replicate();