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 * name - The name of the target file (at the time of the operation)
65 * With just this information, it is not alwasy possible to determine
66 * the file paths for each operation. For instance, if pfid does not
67 * exist on the source-fs (due to a subsequent deletion), its path
68 * cannot be queried. In such cases, lustre_rsync keeps the files in a
69 * special directory ("/.lustrerepl"). Once all the operations in a
70 * changelog are replayed, all the files in this special directory
71 * will get moved to the location as in the source-fs.
73 * Shorthand used: f2p(tfid) = fid2path(tfid)
75 * The following are the metadata operations of interest.
77 * If tfid is absent on the source-fs, ignore this operation
78 * If pfid is absent on the source-fs [or]
79 * if f2p(pfid) is not present on target-fs [or]
80 * if f2p(pfid)+name != f2p(tfid)
81 * creat .lustrerepl/tfid
82 * track [pfid,tfid,name]
87 * If .lustrerepl/[tfid] is present on the target
88 * rm .lustrerepl/[tfid]
89 * Else if pfid is present on the source-fs,
90 * if f2p(pfid)+name is present,
91 * rm f2p(pfid)+name(pfid,name)
93 * 3. move (pfid1,name1) to (pfid2,name2)
95 * if pfid1 is also present, mv (pfid1,name1) to (pfid2,name2)
96 * else mv .lustrerepl/[tfid] to (pfid2,name2)
97 * If pfid2 is not present,
98 * if pfid1 is present, mv (pfid1,name1) .lustrerepl/[tfid]
99 * If moving out of .lustrerepl
100 * move out all its children in .lustrerepl.
101 * [pfid,tfid,name] tracked from (1) is used for this.
111 #include <sys/stat.h>
112 #include <sys/types.h>
116 #include <sys/xattr.h>
118 #include <libcfs/libcfs.h>
119 #include <libcfs/libcfsutil.h>
120 #include <lustre/liblustreapi.h>
121 #include <lustre/lustre_idl.h>
122 #include "lustre_rsync.h"
124 #define REPLICATE_STATUS_VER 1
125 #define CLEAR_INTERVAL 100
126 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
128 #define TYPE_STR_LEN 16
130 #define DEFAULT_MDT "-MDT0000"
131 #define SPECIAL_DIR ".lustrerepl"
132 #define RSYNC "rsync"
139 /* Not used; declared for fulfilling obd.c's dependency. */
140 command_t cmdlist[0];
141 extern int obd_initialize(int argc, char **argv);
143 /* Information for processing a changelog record. This structure is
144 allocated on the heap instead of allocating large variables on the
149 enum changelog_rec_type type;
150 char pfid[LR_FID_STR_LEN];
151 char tfid[LR_FID_STR_LEN];
152 char name[PATH_MAX + 1];
153 char src[PATH_MAX + 1];
154 char dest[PATH_MAX + 1];
155 char path[PATH_MAX + 1];
156 char savedpath[PATH_MAX + 1];
157 char link[PATH_MAX + 1];
158 char linktmp[PATH_MAX + 1];
163 /* Variables for querying the xattributes */
170 struct lr_parent_child_list {
171 struct lr_parent_child_log pc_log;
172 struct lr_parent_child_list *pc_next;
175 struct lustre_rsync_status *status;
176 char *statuslog; /* Name of the status log file */
178 int noxattr; /* Flag to turn off replicating xattrs */
179 int noclear; /* Flag to turn off clearing changelogs */
180 int debug; /* Flag to turn debugging information on and off */
181 int verbose; /* Verbose output */
182 long long rec_count; /* No of changelog records that were processed */
185 int use_rsync; /* Flag to turn on use of rsync to copy data */
186 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
187 int quit; /* Flag to stop processing the changelog; set on the
188 receipt of a signal */
189 int abort_on_err = 0;
191 char rsync[PATH_MAX];
192 char rsync_ver[PATH_MAX];
193 struct lr_parent_child_list *parents;
195 /* Command line options */
196 struct option long_opts[] = {
197 {"source", required_argument, 0, 's'},
198 {"target", required_argument, 0, 't'},
199 {"mdt", required_argument, 0, 'm'},
200 {"user", required_argument, 0, 'u'},
201 {"statuslog", required_argument, 0, 'l'},
202 {"verbose", no_argument, 0, 'v'},
203 {"xattr", required_argument, 0, 'x'},
204 {"dry-run", no_argument, 0, 'z'},
205 /* Undocumented options follow */
206 {"cl-clear", required_argument, 0, 'c'},
207 {"use-rsync", no_argument, 0, 'r'},
208 {"rsync-threshold", required_argument, 0, 'y'},
209 {"start-recno", required_argument, 0, 'n'},
210 {"abort-on-err",no_argument, 0, 'a'},
211 {"debug", required_argument, 0, 'd'},
215 /* Command line usage */
218 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
219 "-m <mdt> -r <user id> -l <status log>\n"
220 "lustre_rsync can also pick up parameters from a "
221 "status log created earlier.\n"
222 "\tlustre_rsync -l <log_file>\n"
224 "\t--xattr <yes|no> replicate EAs\n"
225 "\t--abort-on-err abort at first err\n"
227 "\t--dry-run don't write anything\n");
230 /* Print debug information. This is controlled by the value of the
231 global variable 'debug' */
232 void lr_debug(int level, const char *fmt, ...)
245 void * lr_grow_buf(void *buf, int size)
249 ptr = realloc(buf, size);
256 /* Use rsync to replicate file data */
257 int lr_rsync_data(struct lr_info *info)
260 struct stat st_src, st_dest;
263 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
265 rc = stat(info->src, &st_src);
267 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
268 info->src, info->name);
274 rc = stat(info->dest, &st_dest);
276 fprintf(stderr, "Error: Unable to stat dest=%s\n",
281 if (st_src.st_mtime != st_dest.st_mtime ||
282 st_src.st_size != st_dest.st_size) {
283 /* XXX spawning off an rsync for every data sync and
284 * waiting synchronously is bad for performance.
285 * librsync could possibly used here. But it does not
286 * seem to be of production grade. Multi-threaded
287 * replication is also to be considered.
290 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
292 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
293 status = system(cmd);
296 } else if (WIFEXITED(status)) {
297 status = WEXITSTATUS(status);
300 else if (status == 23 || status == 24)
301 /* Error due to vanished source files;
307 lr_debug(DINFO, "rsync %s exited with %d %d\n",
308 info->src, status, rc);
313 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
314 info->dest, info->tfid);
320 int lr_copy_data(struct lr_info *info)
330 fd_src = open(info->src, O_RDONLY);
333 if (fstat(fd_src, &st_src) == -1 ||
334 stat(info->dest, &st_dest) == -1)
337 if (st_src.st_mtime == st_dest.st_mtime &&
338 st_src.st_size == st_dest.st_size)
341 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
342 /* It is more efficient to use rsync to replicate
343 large files. Any file larger than rsync_threshold
344 is handed off to rsync. */
345 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
346 rc = lr_rsync_data(info);
350 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
355 bufsize = st_dest.st_blksize;
357 if (info->bufsize < bufsize) {
359 info->buf = lr_grow_buf(info->buf, bufsize);
360 if (info->buf == NULL) {
364 info->bufsize = bufsize;
368 rsize = read(fd_src, info->buf, bufsize);
371 } else if (rsize < 0) {
376 if (write(fd_dest, info->buf, rsize) != rsize) {
394 /* Copy data from source to destination */
395 int lr_sync_data(struct lr_info *info)
398 return lr_rsync_data(info);
400 return lr_copy_data(info);
403 /* Copy all attributes from file src to file dest */
404 int lr_copy_attr(char *src, char *dest)
409 if (stat(src, &st) == -1 ||
410 chmod(dest, st.st_mode) == -1 ||
411 chown(dest, st.st_uid, st.st_gid) == -1)
414 time.actime = st.st_atime;
415 time.modtime = st.st_mtime;
416 if (utime(dest, &time) == -1)
421 /* Copy all xattrs from file info->src to info->dest */
422 int lr_copy_xattr(struct lr_info *info)
424 size_t size = info->xsize;
433 rc = llistxattr(info->src, info->xlist, size);
434 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
435 info->src, info->xlist, rc, errno);
436 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
437 size = rc > PATH_MAX ? rc : PATH_MAX;
438 info->xlist = lr_grow_buf(info->xlist, size);
439 if (info->xlist == NULL)
442 rc = llistxattr(info->src, info->xlist, size);
443 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
444 info->src, rc, errno);
451 while (start < len) {
453 rc = lgetxattr(info->src, info->xlist + start,
455 if (info->xvalue == NULL || errno == ERANGE) {
456 size = rc > PATH_MAX ? rc : PATH_MAX;
457 info->xvalue = lr_grow_buf(info->xvalue, size);
458 if (info->xvalue == NULL)
461 rc = lgetxattr(info->src, info->xlist + start,
464 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
468 rc = lsetxattr(info->dest, info->xlist + start,
469 info->xvalue, size, 0);
470 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
473 if (errno != ENOTSUP) {
474 fprintf(stderr, "Error replicating "
475 " xattr for %s: %d\n",
482 start += strlen(info->xlist + start) + 1;
485 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
490 /* Retrieve the filesystem path for a given FID and a given
491 linkno. The path is returned in info->path */
492 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
494 long long recno = -1;
497 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
498 PATH_MAX, &recno, &linkno);
499 if (rc < 0 && rc != -ENOENT) {
500 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
501 status->ls_source, fidstr, -rc, strerror(errno = -rc));
507 /* Retrieve the filesystem path for a given FID. The path is returned
509 int lr_get_path(struct lr_info *info, char *fidstr)
511 return lr_get_path_ln(info, fidstr, 0);
514 /* Generate the path for opening by FID */
515 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
517 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
518 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
523 /* Read the symlink information into 'info->link' */
524 int lr_get_symlink(struct lr_info *info)
529 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
530 rc = readlink(info->src, info->linktmp, PATH_MAX);
532 info->linktmp[rc] = '\0';
535 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
537 if (strncmp(info->linktmp, status->ls_source,
538 strlen(status->ls_source)) == 0) {
539 /* Strip source fs path and replace with target fs path. */
540 link = info->linktmp + strlen(status->ls_source);
541 snprintf(info->src, PATH_MAX, "%s%s",
542 status->ls_targets[info->target_no], link);
545 link = info->linktmp;
547 strncpy(info->link, link, PATH_MAX);
548 info->link[PATH_MAX] = '\0';
553 /* Create file/directory/device file/symlink. */
554 int lr_mkfile(struct lr_info *info)
560 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
561 if (info->type == CL_MKDIR) {
562 rc = mkdir(info->dest, 0777);
563 } else if (info->type == CL_SOFTLINK) {
564 lr_get_symlink(info);
565 rc = symlink(info->link, info->dest);
566 } else if (info->type == CL_MKNOD) {
567 lr_get_FID_PATH(status->ls_source, info->tfid,
568 info->src, PATH_MAX);
569 rc = stat(info->src, &st);
576 rc = mknod(info->dest, st.st_mode, st.st_rdev);
578 rc = mknod(info->dest, S_IFREG | 0777, 0);
583 /* Sync data and attributes */
584 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
585 lr_debug(DTRACE, "Syncing data and attributes %s\n",
587 (void) lr_copy_xattr(info);
588 if (info->type == CL_CREATE)
589 rc = lr_sync_data(info);
591 rc = lr_copy_attr(info->src, info->dest);
594 /* Source file has disappeared. Not an error. */
597 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
604 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
606 struct lr_parent_child_list *p;
608 p = calloc(1, sizeof(*p));
611 strcpy(p->pc_log.pcl_pfid, pfid);
612 strcpy(p->pc_log.pcl_tfid, tfid);
613 strcpy(p->pc_log.pcl_name, name);
615 p->pc_next = parents;
620 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
622 struct lr_parent_child_list *curr, *prev;
626 d = calloc(1, PATH_MAX + 1);
627 prev = curr = parents;
629 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
630 snprintf(d, PATH_MAX, "%s/%s", dest,
631 curr->pc_log.pcl_name);
632 snprintf(info->src, PATH_MAX, "%s/%s/%s",
633 status->ls_targets[info->target_no],
634 SPECIAL_DIR, curr->pc_log.pcl_tfid);
635 rc = rename(info->src, d);
637 fprintf(stderr, "Error renaming file "
639 info->src, d, errno);
642 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
644 parents = curr->pc_next;
646 prev->pc_next = curr->pc_next;
648 prev = curr = parents;
652 curr = curr->pc_next;
659 /* remove [info->pfid, ext->tfid] from parents */
660 int lr_remove_pc(const char *pfid, const char *tfid)
662 struct lr_parent_child_list *curr, *prev;
664 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
665 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
666 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
668 parents = curr->pc_next;
670 prev->pc_next = curr->pc_next;
678 /* Create file under SPECIAL_DIR with its tfid as its name. */
679 int lr_mk_special(struct lr_info *info)
683 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
684 status->ls_targets[info->target_no], SPECIAL_DIR,
687 rc = lr_mkfile(info);
691 rc = lr_add_pc(info->pfid, info->tfid, info->name);
695 /* Remove a file or directory */
696 int lr_rmfile(struct lr_info *info)
700 if (info->type == CL_RMDIR)
701 rc = rmdir(info->dest);
703 rc = unlink(info->dest);
709 /* Recursively remove directory and its contents */
710 int lr_rm_recursive(struct lr_info *info)
714 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
715 rc = system(info->cmd);
722 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
723 int lr_rm_special(struct lr_info *info)
727 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
728 status->ls_targets[info->target_no], SPECIAL_DIR,
730 rc = lr_rmfile(info);
733 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
734 info->dest, rc, errno);
738 /* Replicate file and directory create events */
739 int lr_create(struct lr_info *info)
746 /* Is target FID present on the source? */
747 rc = lr_get_path(info, info->tfid);
749 /* Source file has disappeared. Not an error. */
750 lr_debug(DINFO, "create: tfid %s not found on"
751 "source-fs\n", info->tfid);
756 strcpy(info->savedpath, info->path);
758 /* Is parent FID present on the source */
759 rc = lr_get_path(info, info->pfid);
761 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
768 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
769 len = strlen(info->path);
770 if (len - 1 >= 0 && info->path[len - 1] == '/')
771 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
773 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
775 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
777 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
778 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
779 info->tfid, info->dest, info->savedpath);
783 /* Is f2p(pfid) present on the target? If not, the parent has
786 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
788 if (access(info->dest, F_OK) != 0)
791 for (info->target_no = 0; info->target_no < status->ls_num_targets;
793 snprintf(info->dest, PATH_MAX, "%s/%s",
794 status->ls_targets[info->target_no], info->savedpath);
795 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
799 rc1 = lr_mkfile(info);
800 if (mkspecial || rc1 == -ENOENT) {
801 rc1 = lr_mk_special(info);
809 /* Replicate a file remove (rmdir/unlink) operation */
810 int lr_remove(struct lr_info *info)
815 for (info->target_no = 0; info->target_no < status->ls_num_targets;
818 rc1 = lr_rm_special(info);
822 rc1 = lr_get_path(info, info->pfid);
823 if (rc1 == -ENOENT) {
824 lr_debug(DINFO, "remove: pfid %s not found\n",
832 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
833 status->ls_targets[info->target_no], info->path,
836 rc1 = lr_rmfile(info);
837 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
838 info->dest, rc1, errno);
839 if (rc1 == -ENOTEMPTY)
840 rc1 = lr_rm_recursive(info);
850 /* Replicate a rename/move operation. This operations are tracked by
851 two changelog records. */
852 int lr_move(struct lr_info *info, struct lr_info *ext)
858 int special_dest = 0;
860 rc_dest = lr_get_path(ext, ext->pfid);
861 if (rc_dest < 0 && rc_dest != -ENOENT)
864 rc_src = lr_get_path(info, info->pfid);
865 if (rc_src < 0 && rc_src != -ENOENT)
868 for (info->target_no = 0; info->target_no < status->ls_num_targets;
872 snprintf(info->dest, PATH_MAX, "%s/%s",
873 status->ls_targets[info->target_no],
875 if (access(info->dest, F_OK) != 0) {
878 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
879 status->ls_targets[info->target_no],
880 ext->path, ext->name);
883 if (rc_dest == -ENOENT) {
884 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
885 status->ls_targets[info->target_no],
886 SPECIAL_DIR, info->tfid);
891 snprintf(info->src, PATH_MAX, "%s/%s/%s",
892 status->ls_targets[info->target_no],
893 info->path, info->name);
894 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
896 snprintf(info->src, PATH_MAX, "%s/%s/%s",
897 status->ls_targets[info->target_no],
898 SPECIAL_DIR, info->tfid);
903 if (strcmp(info->src, info->dest) != 0) {
904 rc1 = rename(info->src, info->dest);
910 lr_remove_pc(info->pfid, info->tfid);
912 lr_cascade_move(info->tfid, info->dest, info);
915 lr_add_pc(ext->pfid, info->tfid, ext->name);
917 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
918 info->src, info->dest, rc1, errno);
925 /* Replicate a hard link */
926 int lr_link(struct lr_info *info)
934 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
935 rc = stat(info->src, &st);
939 for (info->target_no = 0; info->target_no < status->ls_num_targets;
946 /* Search through the hardlinks to get the src and dest */
947 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
948 info->dest[0] == 0); i++) {
949 rc1 = lr_get_path_ln(info, info->tfid, i);
950 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
951 info->path, info->name, i, rc1);
955 len = strlen(info->path) - strlen(info->name);
956 if (len > 0 && strcmp(info->path + len,
958 snprintf(info->dest, PATH_MAX, "%s/%s",
959 status->ls_targets[info->target_no],
961 else if (info->src[0] == 0)
962 snprintf(info->src, PATH_MAX, "%s/%s",
963 status->ls_targets[info->target_no],
972 if (info->src[0] == 0 || info->dest[0] == 0)
973 /* Could not find the source or destination.
974 This can happen when some links don't exist
978 if (info->src[0] == 0)
979 snprintf(info->src, PATH_MAX, "%s/%s/%s",
980 status->ls_targets[info->target_no],
981 SPECIAL_DIR, info->tfid);
982 else if (info->dest[0] == 0)
983 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
984 status->ls_targets[info->target_no],
985 SPECIAL_DIR, info->tfid);
987 rc1 = link(info->src, info->dest);
988 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
989 info->src, info->dest, rc1, strerror(errno));
997 /* Replicate file attributes */
998 int lr_setattr(struct lr_info *info)
1003 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1005 rc = lr_get_path(info, info->tfid);
1007 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1012 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1013 info->target_no++) {
1015 snprintf(info->dest, PATH_MAX, "%s/%s",
1016 status->ls_targets[info->target_no], info->path);
1017 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1020 rc1 = lr_sync_data(info);
1022 rc1 = lr_copy_attr(info->src, info->dest);
1029 /* Replicate xattrs */
1030 int lr_setxattr(struct lr_info *info)
1034 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1036 rc = lr_get_path(info, info->tfid);
1038 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1043 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1044 info->target_no++) {
1046 snprintf(info->dest, PATH_MAX, "%s/%s",
1047 status->ls_targets[info->target_no], info->path);
1048 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1051 rc1 = lr_copy_xattr(info);
1059 /* Parse a line of changelog entry */
1060 int lr_parse_line(void *priv, struct lr_info *info)
1062 struct changelog_rec *rec;
1064 if (llapi_changelog_recv(priv, &rec) != 0)
1067 info->recno = rec->cr_index;
1068 info->type = rec->cr_type;
1069 sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
1070 sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
1071 strncpy(info->name, rec->cr_name, rec->cr_namelen);
1072 info->name[rec->cr_namelen] = '\0';
1075 printf("Rec %lld: %d %s\n", info->recno, info->type,info->name);
1077 llapi_changelog_free(&rec);
1083 /* Initialize the replication parameters */
1084 int lr_init_status()
1086 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1090 status = calloc(size, 1);
1093 status->ls_version = REPLICATE_STATUS_VER;
1094 status->ls_size = size;
1095 status->ls_last_recno = -1;
1099 /* Make a backup of the statuslog */
1100 void lr_backup_log()
1102 char backupfile[PATH_MAX];
1106 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1107 (void) rename(statuslog, backupfile);
1113 /* Save replication parameters to a statuslog. */
1118 size_t write_size = status->ls_size;
1119 struct lr_parent_child_list *curr;
1122 if (statuslog == NULL)
1127 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1130 fprintf(stderr, "Error opening log file for writing (%s)\n",
1135 size = write(fd, status, write_size);
1136 if (size != write_size) {
1137 fprintf(stderr, "Error writing to log file (%s) %d\n",
1143 for (curr = parents; curr; curr = curr->pc_next) {
1144 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1145 if (size != sizeof(curr->pc_log)) {
1146 fprintf(stderr, "Error writing to log file (%s) %d\n",
1156 /* Read statuslog and populate the replication parameters. Command
1157 * line parameters take precedence over parameters in the log file.*/
1160 struct lr_parent_child_list *tmp;
1161 struct lr_parent_child_log rec;
1162 struct lustre_rsync_status *s;
1165 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1168 if (statuslog == NULL)
1171 s = calloc(1, read_size);
1173 GOTO(out, rc = -ENOMEM);
1175 fd = open(statuslog, O_RDONLY);
1177 GOTO(out, rc = -errno);
1178 size = read(fd, s, read_size);
1179 if (size != read_size)
1180 GOTO(out, rc = -EINVAL);
1181 if (read_size < s->ls_size) {
1182 read_size = s->ls_size;
1183 s = lr_grow_buf(s, read_size);
1185 GOTO(out, rc = -ENOMEM);
1186 if (lseek(fd, 0, SEEK_SET) == -1)
1187 GOTO(out, rc = -errno);
1188 size = read(fd, s, read_size);
1189 if (size != read_size)
1190 GOTO(out, rc = -EINVAL);
1193 while (read(fd, &rec, sizeof(rec)) != 0) {
1194 tmp = calloc(1, sizeof(*tmp));
1196 GOTO(out, rc = -ENOMEM);
1198 tmp->pc_next = parents;
1202 /* copy uninitialized fields to status */
1203 if (status->ls_num_targets == 0) {
1204 if (status->ls_size != s->ls_size) {
1205 status = lr_grow_buf(status, s->ls_size);
1207 GOTO(out, rc = -ENOMEM);
1208 status->ls_size = s->ls_size;
1210 status->ls_num_targets = s->ls_num_targets;
1211 memcpy(status->ls_targets, s->ls_targets,
1212 PATH_MAX * s->ls_num_targets);
1214 if (status->ls_last_recno == -1)
1215 status->ls_last_recno = s->ls_last_recno;
1217 if (status->ls_registration[0] == '\0')
1218 strncpy(status->ls_registration, s->ls_registration,
1221 if (status->ls_mdt_device[0] == '\0')
1222 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1225 if (status->ls_source_fs[0] == '\0')
1226 strncpy(status->ls_source_fs, s->ls_source_fs,
1229 if (status->ls_source[0] == '\0')
1230 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1240 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1242 int lr_clear_cl(struct lr_info *info, int force)
1244 char mdt_device[LR_NAME_MAXLEN + 1];
1248 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1249 if (info->type == CL_RENAME)
1250 rec = info->recno + 1;
1253 if (!noclear && !dryrun) {
1254 /* llapi_changelog_clear modifies the mdt
1255 * device name so make a copy of it until this
1258 strncpy(mdt_device, status->ls_mdt_device,
1260 rc = llapi_changelog_clear(mdt_device,
1261 status->ls_registration,
1264 printf("Changelog clear (%s, %s, %lld) "
1265 "returned %d\n", status->ls_mdt_device,
1266 status->ls_registration, rec, rc);
1268 if (!rc && !dryrun) {
1269 status->ls_last_recno = rec;
1278 /* Locate a usable version of rsync. At this point we'll use any
1280 int lr_locate_rsync()
1286 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1287 fp = popen(rsync, "r");
1291 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1296 len = strlen(rsync);
1297 if (len > 0 && rsync[len - 1] == '\n')
1298 rsync[len - 1] = '\0';
1301 /* Determine the version of rsync */
1302 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1303 fp = popen(rsync_ver, "r");
1307 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1311 len = strlen(rsync_ver);
1312 if (len > 0 && rsync_ver[len - 1] == '\n')
1313 rsync_ver[len - 1] = '\0';
1320 /* Print the replication parameters */
1321 void lr_print_status(struct lr_info *info)
1328 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1329 printf("MDT device: %s\n", status->ls_mdt_device);
1330 printf("Source: %s\n", status->ls_source);
1331 for (i = 0; i < status->ls_num_targets; i++)
1332 printf("Target: %s\n", status->ls_targets[i]);
1333 if (statuslog != NULL)
1334 printf("Statuslog: %s\n", statuslog);
1335 printf("Changelog registration: %s\n", status->ls_registration);
1336 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1338 printf("Replicate xattrs: no\n");
1340 printf("Clear changelog after use: no\n");
1342 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1345 void lr_print_failure(struct lr_info *info, int rc)
1347 fprintf(stderr, "Replication of operation failed(%d):"
1348 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1349 changelog_type2str(info->type), info->type, info->tfid,
1350 info->pfid, info->name);
1353 /* Replicate filesystem operations from src_path to target_path */
1356 void *changelog_priv;
1357 struct lr_info *info;
1358 struct lr_info *ext;
1366 info = calloc(1, sizeof(struct lr_info));
1370 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1372 fprintf(stderr, "Source path is not a valid Lustre client "
1376 if (status->ls_mdt_device[0] == '\0')
1377 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1378 status->ls_source_fs, DEFAULT_MDT);
1380 ext = calloc(1, sizeof(struct lr_info));
1383 memcpy(ext, info, sizeof(struct lr_info));
1385 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1386 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1388 rc = mkdir(info->dest, 0777);
1389 if (rc == -1 && errno != EEXIST) {
1390 fprintf(stderr, "Error writing to target path %s.\n",
1391 status->ls_targets[i]);
1394 rc = llistxattr(info->src, info->xlist, info->xsize);
1395 if (rc == -1 && errno == ENOTSUP) {
1396 fprintf(stderr, "xattrs not supported on %s\n",
1397 status->ls_targets[i]);
1401 if (xattr_not_supp == status->ls_num_targets)
1402 /* None of the targets support xattrs. */
1405 lr_print_status(info);
1407 /* Open changelogs for consumption*/
1408 rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
1409 status->ls_source_fs, status->ls_last_recno);
1411 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1412 status->ls_source_fs);
1416 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1418 if (info->type == CL_RENAME)
1419 /* Rename operations have an additional changelog
1420 record of information. */
1421 lr_parse_line(changelog_priv, ext);
1426 switch(info->type) {
1431 rc = lr_create(info);
1435 rc = lr_remove(info);
1438 rc = lr_move(info, ext);
1445 rc = lr_setattr(info);
1448 rc = lr_setxattr(info);
1455 /* Nothing needs to be done for these entries */
1459 if (rc && rc != -ENOENT) {
1460 lr_print_failure(info, rc);
1465 lr_clear_cl(info, 0);
1467 bzero(info, sizeof(struct lr_info));
1468 bzero(ext, sizeof(struct lr_info));
1472 llapi_changelog_fini(&changelog_priv);
1474 if (errors || verbose)
1475 printf("Errors: %d\n", errors);
1477 /* Clear changelog records used so far */
1478 lr_clear_cl(info, 1);
1481 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1482 printf("Changelog records consumed: %lld\n", rec_count);
1489 termination_handler (int signum)
1491 /* Set a flag for the replicator to gracefully shutdown */
1493 printf("lustre_rsync halting.\n");
1496 int main(int argc, char *argv[])
1502 if ((rc = lr_init_status()) != 0)
1505 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:",
1506 long_opts, NULL)) >= 0) {
1509 /* Assume absolute paths */
1513 /* Assume absolute paths */
1514 strncpy(status->ls_source, optarg, PATH_MAX);
1517 status->ls_num_targets++;
1519 if (numtargets != status->ls_num_targets) {
1520 /* Targets were read from a log
1521 file. The ones specified on the
1522 command line take precedence. The
1523 ones from the log file will be
1525 status->ls_num_targets = numtargets;
1527 newsize = sizeof (struct lustre_rsync_status) +
1528 (status->ls_num_targets * PATH_MAX);
1529 if (status->ls_size != newsize) {
1530 status->ls_size = newsize;
1531 status = lr_grow_buf(status, newsize);
1535 strncpy(status->ls_targets[status->ls_num_targets - 1],
1540 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1543 strncpy(status->ls_registration, optarg,
1548 (void) lr_read_log();
1554 if (strcmp("no", optarg) == 0) {
1556 } else if (strcmp("yes", optarg) != 0) {
1557 printf("Invalid parameter %s. "
1558 "Specify --xattr=no or --xattr=yes\n",
1567 /* Undocumented option cl-clear */
1568 if (strcmp("no", optarg) == 0) {
1570 } else if (strcmp("yes", optarg) != 0) {
1571 printf("Invalid parameter %s. "
1572 "Specify --cl-clear=no "
1573 "or --cl-clear=yes\n",
1579 /* Undocumented option use-rsync */
1583 /* Undocumented option rsync-threshold */
1584 rsync_threshold = atol(optarg);
1587 /* Undocumented option start-recno */
1588 status->ls_last_recno = atol(optarg);
1591 /* Undocumented option debug */
1592 debug = atoi(optarg);
1593 if (debug < 0 || debug > 2)
1597 fprintf(stderr, "error: %s: option '%s' "
1598 "unrecognized.\n", argv[0], argv[optind - 1]);
1604 if (status->ls_last_recno == -1)
1605 status->ls_last_recno = 0;
1606 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1607 /* No registration ID was passed in. */
1608 printf("Please specify changelog consumer registration id.\n");
1612 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1613 fprintf(stderr, "Please specify the source path.\n");
1617 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1618 fprintf(stderr, "Please specify the target path.\n");
1623 /* This plumbing is needed for some of the ioctls behind
1624 llapi calls to work. */
1625 if (obd_initialize(argc, argv) < 0) {
1626 fprintf(stderr, "obd_initialize failed.\n");
1630 rc = lr_locate_rsync();
1631 if (use_rsync && rc != 0) {
1632 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1636 signal(SIGINT, termination_handler);
1637 signal(SIGHUP, termination_handler);
1638 signal(SIGTERM, termination_handler);
1640 rc = lr_replicate();