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/libcfsutil.h>
123 #include <lustre/lustreapi.h>
124 #include "lustre_rsync.h"
126 #define REPLICATE_STATUS_VER 1
127 #define CLEAR_INTERVAL 100
128 #define DEFAULT_RSYNC_THRESHOLD 0xA00000 /* 10 MB */
130 #define TYPE_STR_LEN 16
132 #define DEFAULT_MDT "-MDT0000"
133 #define SPECIAL_DIR ".lustrerepl"
134 #define RSYNC "rsync"
141 /* Not used; declared for fulfilling obd.c's dependency. */
142 command_t cmdlist[0];
143 extern int obd_initialize(int argc, char **argv);
145 /* Information for processing a changelog record. This structure is
146 allocated on the heap instead of allocating large variables on the
151 unsigned int is_extended:1;
152 enum changelog_rec_type type;
153 char tfid[LR_FID_STR_LEN];
154 char pfid[LR_FID_STR_LEN];
155 char sfid[LR_FID_STR_LEN];
156 char spfid[LR_FID_STR_LEN];
157 char sname[NAME_MAX + 1];
158 char name[NAME_MAX + 1];
159 char src[PATH_MAX + 1];
160 char dest[PATH_MAX + 1];
161 char path[PATH_MAX + 1];
162 char savedpath[PATH_MAX + 1];
163 char link[PATH_MAX + 1];
164 char linktmp[PATH_MAX + 1];
169 /* Variables for querying the xattributes */
176 struct lr_parent_child_list {
177 struct lr_parent_child_log pc_log;
178 struct lr_parent_child_list *pc_next;
181 struct lustre_rsync_status *status;
182 char *statuslog; /* Name of the status log file */
184 int noxattr; /* Flag to turn off replicating xattrs */
185 int noclear; /* Flag to turn off clearing changelogs */
186 int debug; /* Flag to turn debugging information on and off */
187 int verbose; /* Verbose output */
188 long long rec_count; /* No of changelog records that were processed */
191 int use_rsync; /* Flag to turn on use of rsync to copy data */
192 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
193 int quit; /* Flag to stop processing the changelog; set on the
194 receipt of a signal */
195 int abort_on_err = 0;
197 char rsync[PATH_MAX];
198 char rsync_ver[PATH_MAX];
199 struct lr_parent_child_list *parents;
201 /* Command line options */
202 struct option long_opts[] = {
203 {"source", required_argument, 0, 's'},
204 {"target", required_argument, 0, 't'},
205 {"mdt", required_argument, 0, 'm'},
206 {"user", required_argument, 0, 'u'},
207 {"statuslog", required_argument, 0, 'l'},
208 {"verbose", no_argument, 0, 'v'},
209 {"xattr", required_argument, 0, 'x'},
210 {"dry-run", no_argument, 0, 'z'},
211 /* Undocumented options follow */
212 {"cl-clear", required_argument, 0, 'c'},
213 {"use-rsync", no_argument, 0, 'r'},
214 {"rsync-threshold", required_argument, 0, 'y'},
215 {"start-recno", required_argument, 0, 'n'},
216 {"abort-on-err",no_argument, 0, 'a'},
217 {"debug", required_argument, 0, 'd'},
221 /* Command line usage */
224 fprintf(stderr, "\tlustre_rsync -s <lustre_root_path> -t <target_path> "
225 "-m <mdt> -r <user id> -l <status log>\n"
226 "lustre_rsync can also pick up parameters from a "
227 "status log created earlier.\n"
228 "\tlustre_rsync -l <log_file>\n"
230 "\t--xattr <yes|no> replicate EAs\n"
231 "\t--abort-on-err abort at first err\n"
233 "\t--dry-run don't write anything\n");
236 /* Print debug information. This is controlled by the value of the
237 global variable 'debug' */
238 void lr_debug(int level, const char *fmt, ...)
251 void * lr_grow_buf(void *buf, int size)
255 ptr = realloc(buf, size);
262 /* Use rsync to replicate file data */
263 int lr_rsync_data(struct lr_info *info)
266 struct stat st_src, st_dest;
269 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
271 rc = stat(info->src, &st_src);
273 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
274 info->src, info->name);
280 rc = stat(info->dest, &st_dest);
282 fprintf(stderr, "Error: Unable to stat dest=%s\n",
287 if (st_src.st_mtime != st_dest.st_mtime ||
288 st_src.st_size != st_dest.st_size) {
289 /* XXX spawning off an rsync for every data sync and
290 * waiting synchronously is bad for performance.
291 * librsync could possibly used here. But it does not
292 * seem to be of production grade. Multi-threaded
293 * replication is also to be considered.
296 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
298 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
299 status = system(cmd);
302 } else if (WIFEXITED(status)) {
303 status = WEXITSTATUS(status);
306 else if (status == 23 || status == 24)
307 /* Error due to vanished source files;
313 lr_debug(DINFO, "rsync %s exited with %d %d\n",
314 info->src, status, rc);
319 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
320 info->dest, info->tfid);
326 int lr_copy_data(struct lr_info *info)
336 fd_src = open(info->src, O_RDONLY);
339 if (fstat(fd_src, &st_src) == -1 ||
340 stat(info->dest, &st_dest) == -1)
343 if (st_src.st_mtime == st_dest.st_mtime &&
344 st_src.st_size == st_dest.st_size)
347 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
348 /* It is more efficient to use rsync to replicate
349 large files. Any file larger than rsync_threshold
350 is handed off to rsync. */
351 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
352 rc = lr_rsync_data(info);
356 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
361 bufsize = st_dest.st_blksize;
363 if (info->bufsize < bufsize) {
365 info->buf = lr_grow_buf(info->buf, bufsize);
366 if (info->buf == NULL) {
370 info->bufsize = bufsize;
374 rsize = read(fd_src, info->buf, bufsize);
377 } else if (rsize < 0) {
382 if (write(fd_dest, info->buf, rsize) != rsize) {
400 /* Copy data from source to destination */
401 int lr_sync_data(struct lr_info *info)
404 return lr_rsync_data(info);
406 return lr_copy_data(info);
409 /* Copy all attributes from file src to file dest */
410 int lr_copy_attr(char *src, char *dest)
415 if (stat(src, &st) == -1 ||
416 chmod(dest, st.st_mode) == -1 ||
417 chown(dest, st.st_uid, st.st_gid) == -1)
420 time.actime = st.st_atime;
421 time.modtime = st.st_mtime;
422 if (utime(dest, &time) == -1)
427 /* Copy all xattrs from file info->src to info->dest */
428 int lr_copy_xattr(struct lr_info *info)
430 size_t size = info->xsize;
439 rc = llistxattr(info->src, info->xlist, size);
440 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
441 info->src, info->xlist, rc, errno);
442 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
443 size = rc > PATH_MAX ? rc : PATH_MAX;
444 info->xlist = lr_grow_buf(info->xlist, size);
445 if (info->xlist == NULL)
448 rc = llistxattr(info->src, info->xlist, size);
449 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
450 info->src, rc, errno);
457 while (start < len) {
459 rc = lgetxattr(info->src, info->xlist + start,
461 if (info->xvalue == NULL || errno == ERANGE) {
462 size = rc > PATH_MAX ? rc : PATH_MAX;
463 info->xvalue = lr_grow_buf(info->xvalue, size);
464 if (info->xvalue == NULL)
467 rc = lgetxattr(info->src, info->xlist + start,
470 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
474 rc = lsetxattr(info->dest, info->xlist + start,
475 info->xvalue, size, 0);
476 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
479 if (errno != ENOTSUP) {
480 fprintf(stderr, "Error replicating "
481 " xattr for %s: %d\n",
488 start += strlen(info->xlist + start) + 1;
491 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
496 /* Retrieve the filesystem path for a given FID and a given
497 linkno. The path is returned in info->path */
498 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
500 long long recno = -1;
503 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
504 PATH_MAX, &recno, &linkno);
505 if (rc < 0 && rc != -ENOENT) {
506 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
507 status->ls_source, fidstr, -rc, strerror(errno = -rc));
513 /* Retrieve the filesystem path for a given FID. The path is returned
515 int lr_get_path(struct lr_info *info, char *fidstr)
517 return lr_get_path_ln(info, fidstr, 0);
520 /* Generate the path for opening by FID */
521 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
523 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
524 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
529 /* Read the symlink information into 'info->link' */
530 int lr_get_symlink(struct lr_info *info)
535 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
536 rc = readlink(info->src, info->linktmp, PATH_MAX);
538 info->linktmp[rc] = '\0';
541 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
543 if (strncmp(info->linktmp, status->ls_source,
544 strlen(status->ls_source)) == 0) {
545 /* Strip source fs path and replace with target fs path. */
546 link = info->linktmp + strlen(status->ls_source);
547 snprintf(info->src, PATH_MAX, "%s%s",
548 status->ls_targets[info->target_no], link);
551 link = info->linktmp;
553 strncpy(info->link, link, PATH_MAX);
554 info->link[PATH_MAX] = '\0';
559 /* Create file/directory/device file/symlink. */
560 int lr_mkfile(struct lr_info *info)
566 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
567 if (info->type == CL_MKDIR) {
568 rc = mkdir(info->dest, 0777);
569 } else if (info->type == CL_SOFTLINK) {
570 lr_get_symlink(info);
571 rc = symlink(info->link, info->dest);
572 } else if (info->type == CL_MKNOD) {
573 lr_get_FID_PATH(status->ls_source, info->tfid,
574 info->src, PATH_MAX);
575 rc = stat(info->src, &st);
582 rc = mknod(info->dest, st.st_mode, st.st_rdev);
584 rc = mknod(info->dest, S_IFREG | 0777, 0);
589 /* Sync data and attributes */
590 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
591 lr_debug(DTRACE, "Syncing data and attributes %s\n",
593 (void) lr_copy_xattr(info);
594 if (info->type == CL_CREATE)
595 rc = lr_sync_data(info);
597 rc = lr_copy_attr(info->src, info->dest);
600 /* Source file has disappeared. Not an error. */
603 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
610 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
612 struct lr_parent_child_list *p;
614 p = calloc(1, sizeof(*p));
617 strcpy(p->pc_log.pcl_pfid, pfid);
618 strcpy(p->pc_log.pcl_tfid, tfid);
619 strcpy(p->pc_log.pcl_name, name);
621 p->pc_next = parents;
626 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
628 struct lr_parent_child_list *curr, *prev;
632 d = calloc(1, PATH_MAX + 1);
633 prev = curr = parents;
635 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
636 snprintf(d, PATH_MAX, "%s/%s", dest,
637 curr->pc_log.pcl_name);
638 snprintf(info->src, PATH_MAX, "%s/%s/%s",
639 status->ls_targets[info->target_no],
640 SPECIAL_DIR, curr->pc_log.pcl_tfid);
641 rc = rename(info->src, d);
643 fprintf(stderr, "Error renaming file "
645 info->src, d, errno);
648 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
650 parents = curr->pc_next;
652 prev->pc_next = curr->pc_next;
654 prev = curr = parents;
658 curr = curr->pc_next;
665 /* remove [info->spfid, info->sfid] from parents */
666 int lr_remove_pc(const char *pfid, const char *tfid)
668 struct lr_parent_child_list *curr, *prev;
670 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
671 if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
672 strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
674 parents = curr->pc_next;
676 prev->pc_next = curr->pc_next;
684 /* Create file under SPECIAL_DIR with its tfid as its name. */
685 int lr_mk_special(struct lr_info *info)
689 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
690 status->ls_targets[info->target_no], SPECIAL_DIR,
693 rc = lr_mkfile(info);
697 rc = lr_add_pc(info->pfid, info->tfid, info->name);
701 /* Remove a file or directory */
702 int lr_rmfile(struct lr_info *info)
706 if (info->type == CL_RMDIR)
707 rc = rmdir(info->dest);
709 rc = unlink(info->dest);
715 /* Recursively remove directory and its contents */
716 int lr_rm_recursive(struct lr_info *info)
720 snprintf(info->cmd, PATH_MAX, "rm -rf %s", info->dest);
721 rc = system(info->cmd);
728 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
729 int lr_rm_special(struct lr_info *info)
733 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
734 status->ls_targets[info->target_no], SPECIAL_DIR,
736 rc = lr_rmfile(info);
739 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
740 info->dest, rc, errno);
744 /* Replicate file and directory create events */
745 int lr_create(struct lr_info *info)
752 /* Is target FID present on the source? */
753 rc = lr_get_path(info, info->tfid);
755 /* Source file has disappeared. Not an error. */
756 lr_debug(DINFO, "create: tfid %s not found on"
757 "source-fs\n", info->tfid);
762 strcpy(info->savedpath, info->path);
764 /* Is parent FID present on the source */
765 rc = lr_get_path(info, info->pfid);
767 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
774 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
775 len = strlen(info->path);
776 if (len == 1 && info->path[0] == '/')
777 snprintf(info->dest, PATH_MAX, "%s", info->name);
778 else if (len - 1 > 0 && info->path[len - 1] == '/')
779 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
781 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
783 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
785 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
786 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
787 info->tfid, info->dest, info->savedpath);
791 /* Is f2p(pfid) present on the target? If not, the parent has
794 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[0],
796 if (access(info->dest, F_OK) != 0) {
797 lr_debug(DTRACE, "create: parent %s not found\n",
802 for (info->target_no = 0; info->target_no < status->ls_num_targets;
804 snprintf(info->dest, PATH_MAX, "%s/%s",
805 status->ls_targets[info->target_no], info->savedpath);
806 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
810 rc1 = lr_mkfile(info);
811 if (mkspecial || rc1 == -ENOENT) {
812 rc1 = lr_mk_special(info);
820 /* Replicate a file remove (rmdir/unlink) operation */
821 int lr_remove(struct lr_info *info)
826 for (info->target_no = 0; info->target_no < status->ls_num_targets;
829 rc1 = lr_rm_special(info);
833 rc1 = lr_get_path(info, info->pfid);
834 if (rc1 == -ENOENT) {
835 lr_debug(DINFO, "remove: pfid %s not found\n",
843 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
844 status->ls_targets[info->target_no], info->path,
847 rc1 = lr_rmfile(info);
848 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
849 info->dest, rc1, errno);
850 if (rc1 == -ENOTEMPTY)
851 rc1 = lr_rm_recursive(info);
861 /* Replicate a rename/move operation. */
862 int lr_move(struct lr_info *info)
868 int special_dest = 0;
869 char srcpath[PATH_MAX + 1] = "";
871 LASSERT(info->is_extended);
873 rc_src = lr_get_path(info, info->spfid);
874 if (rc_src < 0 && rc_src != -ENOENT)
876 memcpy(srcpath, info->path, strlen(info->path));
878 rc_dest = lr_get_path(info, info->pfid);
879 if (rc_dest < 0 && rc_dest != -ENOENT)
882 for (info->target_no = 0; info->target_no < status->ls_num_targets;
886 snprintf(info->dest, PATH_MAX, "%s/%s",
887 status->ls_targets[info->target_no],
889 if (access(info->dest, F_OK) != 0) {
892 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
893 status->ls_targets[info->target_no],
894 info->path, info->name);
897 if (rc_dest == -ENOENT) {
898 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
899 status->ls_targets[info->target_no],
900 SPECIAL_DIR, info->sfid);
905 snprintf(info->src, PATH_MAX, "%s/%s/%s",
906 status->ls_targets[info->target_no],
907 srcpath, info->sname);
908 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
910 snprintf(info->src, PATH_MAX, "%s/%s/%s",
911 status->ls_targets[info->target_no],
912 SPECIAL_DIR, info->sfid);
917 if (strcmp(info->src, info->dest) != 0) {
918 rc1 = rename(info->src, info->dest);
924 lr_remove_pc(info->spfid, info->sfid);
926 lr_cascade_move(info->sfid, info->dest, info);
929 lr_add_pc(info->pfid, info->sfid, info->name);
931 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
932 info->src, info->dest, rc1, errno);
939 /* Replicate a hard link */
940 int lr_link(struct lr_info *info)
948 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
949 rc = stat(info->src, &st);
953 for (info->target_no = 0; info->target_no < status->ls_num_targets;
960 /* Search through the hardlinks to get the src and dest */
961 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
962 info->dest[0] == 0); i++) {
963 rc1 = lr_get_path_ln(info, info->tfid, i);
964 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
965 info->path, info->name, i, rc1);
969 len = strlen(info->path) - strlen(info->name);
970 if (len > 0 && strcmp(info->path + len,
972 snprintf(info->dest, PATH_MAX, "%s/%s",
973 status->ls_targets[info->target_no],
975 else if (info->src[0] == 0)
976 snprintf(info->src, PATH_MAX, "%s/%s",
977 status->ls_targets[info->target_no],
986 if (info->src[0] == 0 || info->dest[0] == 0)
987 /* Could not find the source or destination.
988 This can happen when some links don't exist
992 if (info->src[0] == 0)
993 snprintf(info->src, PATH_MAX, "%s/%s/%s",
994 status->ls_targets[info->target_no],
995 SPECIAL_DIR, info->tfid);
996 else if (info->dest[0] == 0)
997 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
998 status->ls_targets[info->target_no],
999 SPECIAL_DIR, info->tfid);
1001 rc1 = link(info->src, info->dest);
1002 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
1003 info->src, info->dest, rc1, strerror(errno));
1011 /* Replicate file attributes */
1012 int lr_setattr(struct lr_info *info)
1017 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1019 rc = lr_get_path(info, info->tfid);
1021 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
1026 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1027 info->target_no++) {
1029 snprintf(info->dest, PATH_MAX, "%s/%s",
1030 status->ls_targets[info->target_no], info->path);
1031 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1034 rc1 = lr_sync_data(info);
1036 rc1 = lr_copy_attr(info->src, info->dest);
1043 /* Replicate xattrs */
1044 int lr_setxattr(struct lr_info *info)
1048 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1050 rc = lr_get_path(info, info->tfid);
1052 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1057 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1058 info->target_no++) {
1060 snprintf(info->dest, PATH_MAX, "%s/%s",
1061 status->ls_targets[info->target_no], info->path);
1062 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1065 rc1 = lr_copy_xattr(info);
1073 /* Parse a line of changelog entry */
1074 int lr_parse_line(void *priv, struct lr_info *info)
1076 struct changelog_ext_rec *rec;
1078 if (llapi_changelog_recv(priv, &rec) != 0)
1081 info->is_extended = CHANGELOG_REC_EXTENDED(rec);
1082 info->recno = rec->cr_index;
1083 info->type = rec->cr_type;
1084 sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
1085 sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
1086 strncpy(info->name, rec->cr_name, rec->cr_namelen);
1088 if (fid_is_sane(&rec->cr_sfid)) {
1089 sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
1090 sprintf(info->spfid, DFID, PFID(&rec->cr_spfid));
1091 strncpy(info->sname, changelog_rec_sname(rec),
1092 changelog_rec_snamelen(rec));
1093 info->sname[changelog_rec_snamelen(rec)] = '\0';
1096 printf("Rec %lld: %d %s %s\n", info->recno, info->type,
1097 info->name, info->sname);
1099 info->name[rec->cr_namelen] = '\0';
1102 printf("Rec %lld: %d %s\n", info->recno, info->type,
1106 llapi_changelog_free(&rec);
1112 /* Initialize the replication parameters */
1113 int lr_init_status()
1115 size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1119 status = calloc(size, 1);
1122 status->ls_version = REPLICATE_STATUS_VER;
1123 status->ls_size = size;
1124 status->ls_last_recno = -1;
1128 /* Make a backup of the statuslog */
1129 void lr_backup_log()
1131 char backupfile[PATH_MAX];
1135 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1136 (void) rename(statuslog, backupfile);
1142 /* Save replication parameters to a statuslog. */
1147 size_t write_size = status->ls_size;
1148 struct lr_parent_child_list *curr;
1151 if (statuslog == NULL)
1156 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC,
1159 fprintf(stderr, "Error opening log file for writing (%s)\n",
1164 size = write(fd, status, write_size);
1165 if (size != write_size) {
1166 fprintf(stderr, "Error writing to log file (%s) %d\n",
1172 for (curr = parents; curr; curr = curr->pc_next) {
1173 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1174 if (size != sizeof(curr->pc_log)) {
1175 fprintf(stderr, "Error writing to log file (%s) %d\n",
1185 /* Read statuslog and populate the replication parameters. Command
1186 * line parameters take precedence over parameters in the log file.*/
1189 struct lr_parent_child_list *tmp;
1190 struct lr_parent_child_log rec;
1191 struct lustre_rsync_status *s;
1194 size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX;
1197 if (statuslog == NULL)
1200 s = calloc(1, read_size);
1202 GOTO(out, rc = -ENOMEM);
1204 fd = open(statuslog, O_RDONLY);
1206 GOTO(out, rc = -errno);
1207 size = read(fd, s, read_size);
1208 if (size != read_size)
1209 GOTO(out, rc = -EINVAL);
1210 if (read_size < s->ls_size) {
1211 read_size = s->ls_size;
1212 s = lr_grow_buf(s, read_size);
1214 GOTO(out, rc = -ENOMEM);
1215 if (lseek(fd, 0, SEEK_SET) == -1)
1216 GOTO(out, rc = -errno);
1217 size = read(fd, s, read_size);
1218 if (size != read_size)
1219 GOTO(out, rc = -EINVAL);
1222 while (read(fd, &rec, sizeof(rec)) != 0) {
1223 tmp = calloc(1, sizeof(*tmp));
1225 GOTO(out, rc = -ENOMEM);
1227 tmp->pc_next = parents;
1231 /* copy uninitialized fields to status */
1232 if (status->ls_num_targets == 0) {
1233 if (status->ls_size != s->ls_size) {
1234 status = lr_grow_buf(status, s->ls_size);
1236 GOTO(out, rc = -ENOMEM);
1237 status->ls_size = s->ls_size;
1239 status->ls_num_targets = s->ls_num_targets;
1240 memcpy(status->ls_targets, s->ls_targets,
1241 PATH_MAX * s->ls_num_targets);
1243 if (status->ls_last_recno == -1)
1244 status->ls_last_recno = s->ls_last_recno;
1246 if (status->ls_registration[0] == '\0')
1247 strncpy(status->ls_registration, s->ls_registration,
1250 if (status->ls_mdt_device[0] == '\0')
1251 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1254 if (status->ls_source_fs[0] == '\0')
1255 strncpy(status->ls_source_fs, s->ls_source_fs,
1258 if (status->ls_source[0] == '\0')
1259 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1269 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1271 int lr_clear_cl(struct lr_info *info, int force)
1273 char mdt_device[LR_NAME_MAXLEN + 1];
1277 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1278 if (info->type == CL_RENAME)
1279 rec = info->recno + 1;
1282 if (!noclear && !dryrun) {
1283 /* llapi_changelog_clear modifies the mdt
1284 * device name so make a copy of it until this
1287 strncpy(mdt_device, status->ls_mdt_device,
1289 rc = llapi_changelog_clear(mdt_device,
1290 status->ls_registration,
1293 printf("Changelog clear (%s, %s, %lld) "
1294 "returned %d\n", status->ls_mdt_device,
1295 status->ls_registration, rec, rc);
1297 if (!rc && !dryrun) {
1298 status->ls_last_recno = rec;
1307 /* Locate a usable version of rsync. At this point we'll use any
1309 int lr_locate_rsync()
1315 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1316 fp = popen(rsync, "r");
1320 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1325 len = strlen(rsync);
1326 if (len > 0 && rsync[len - 1] == '\n')
1327 rsync[len - 1] = '\0';
1330 /* Determine the version of rsync */
1331 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1332 fp = popen(rsync_ver, "r");
1336 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1340 len = strlen(rsync_ver);
1341 if (len > 0 && rsync_ver[len - 1] == '\n')
1342 rsync_ver[len - 1] = '\0';
1349 /* Print the replication parameters */
1350 void lr_print_status(struct lr_info *info)
1357 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1358 printf("MDT device: %s\n", status->ls_mdt_device);
1359 printf("Source: %s\n", status->ls_source);
1360 for (i = 0; i < status->ls_num_targets; i++)
1361 printf("Target: %s\n", status->ls_targets[i]);
1362 if (statuslog != NULL)
1363 printf("Statuslog: %s\n", statuslog);
1364 printf("Changelog registration: %s\n", status->ls_registration);
1365 printf("Starting changelog record: "LPD64"\n", status->ls_last_recno);
1367 printf("Replicate xattrs: no\n");
1369 printf("Clear changelog after use: no\n");
1371 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1374 void lr_print_failure(struct lr_info *info, int rc)
1376 fprintf(stderr, "Replication of operation failed(%d):"
1377 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1378 changelog_type2str(info->type), info->type, info->tfid,
1379 info->pfid, info->name);
1382 /* Replicate filesystem operations from src_path to target_path */
1385 void *changelog_priv;
1386 struct lr_info *info;
1387 struct lr_info *ext = NULL;
1395 info = calloc(1, sizeof(struct lr_info));
1399 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1401 fprintf(stderr, "Source path is not a valid Lustre client "
1405 if (status->ls_mdt_device[0] == '\0')
1406 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1407 status->ls_source_fs, DEFAULT_MDT);
1409 ext = calloc(1, sizeof(struct lr_info));
1415 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1416 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1418 rc = mkdir(info->dest, 0777);
1419 if (rc == -1 && errno != EEXIST) {
1420 fprintf(stderr, "Error writing to target path %s.\n",
1421 status->ls_targets[i]);
1425 rc = llistxattr(info->src, info->xlist, info->xsize);
1426 if (rc == -1 && errno == ENOTSUP) {
1427 fprintf(stderr, "xattrs not supported on %s\n",
1428 status->ls_targets[i]);
1432 if (xattr_not_supp == status->ls_num_targets)
1433 /* None of the targets support xattrs. */
1436 lr_print_status(info);
1438 /* Open changelogs for consumption*/
1439 rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
1440 status->ls_source_fs, status->ls_last_recno);
1442 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1443 status->ls_source_fs);
1447 while (!quit && lr_parse_line(changelog_priv, info) == 0) {
1449 if (info->type == CL_RENAME && !info->is_extended) {
1450 /* Newer rename operations extends changelog to store
1451 * source file information, but old changelog has
1454 if (lr_parse_line(changelog_priv, ext) != 0)
1456 memcpy(info->sfid, info->tfid, sizeof(info->sfid));
1457 memcpy(info->spfid, info->pfid, sizeof(info->spfid));
1458 memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
1459 memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
1460 strncpy(info->sname, info->name, sizeof(info->sname));
1461 strncpy(info->name, ext->name, sizeof(info->name));
1462 info->is_extended = 1;
1468 switch(info->type) {
1473 rc = lr_create(info);
1477 rc = lr_remove(info);
1487 rc = lr_setattr(info);
1490 rc = lr_setxattr(info);
1497 /* Nothing needs to be done for these entries */
1501 if (rc && rc != -ENOENT) {
1502 lr_print_failure(info, rc);
1507 lr_clear_cl(info, 0);
1509 bzero(info, sizeof(struct lr_info));
1510 bzero(ext, sizeof(struct lr_info));
1514 llapi_changelog_fini(&changelog_priv);
1516 if (errors || verbose)
1517 printf("Errors: %d\n", errors);
1519 /* Clear changelog records used so far */
1520 lr_clear_cl(info, 1);
1523 printf("lustre_rsync took %ld seconds\n", time(NULL) - start);
1524 printf("Changelog records consumed: %lld\n", rec_count);
1539 termination_handler (int signum)
1541 /* Set a flag for the replicator to gracefully shutdown */
1543 printf("lustre_rsync halting.\n");
1546 int main(int argc, char *argv[])
1552 if ((rc = lr_init_status()) != 0)
1555 while ((rc = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:",
1556 long_opts, NULL)) >= 0) {
1559 /* Assume absolute paths */
1563 /* Assume absolute paths */
1564 strncpy(status->ls_source, optarg, PATH_MAX);
1567 status->ls_num_targets++;
1569 if (numtargets != status->ls_num_targets) {
1570 /* Targets were read from a log
1571 file. The ones specified on the
1572 command line take precedence. The
1573 ones from the log file will be
1575 status->ls_num_targets = numtargets;
1577 newsize = sizeof (struct lustre_rsync_status) +
1578 (status->ls_num_targets * PATH_MAX);
1579 if (status->ls_size != newsize) {
1580 status->ls_size = newsize;
1581 status = lr_grow_buf(status, newsize);
1585 strncpy(status->ls_targets[status->ls_num_targets - 1],
1590 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1593 strncpy(status->ls_registration, optarg,
1598 (void) lr_read_log();
1604 if (strcmp("no", optarg) == 0) {
1606 } else if (strcmp("yes", optarg) != 0) {
1607 printf("Invalid parameter %s. "
1608 "Specify --xattr=no or --xattr=yes\n",
1617 /* Undocumented option cl-clear */
1618 if (strcmp("no", optarg) == 0) {
1620 } else if (strcmp("yes", optarg) != 0) {
1621 printf("Invalid parameter %s. "
1622 "Specify --cl-clear=no "
1623 "or --cl-clear=yes\n",
1629 /* Undocumented option use-rsync */
1633 /* Undocumented option rsync-threshold */
1634 rsync_threshold = atol(optarg);
1637 /* Undocumented option start-recno */
1638 status->ls_last_recno = atol(optarg);
1641 /* Undocumented option debug */
1642 debug = atoi(optarg);
1643 if (debug < 0 || debug > 2)
1647 fprintf(stderr, "error: %s: option '%s' "
1648 "unrecognized.\n", argv[0], argv[optind - 1]);
1654 if (status->ls_last_recno == -1)
1655 status->ls_last_recno = 0;
1656 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1657 /* No registration ID was passed in. */
1658 printf("Please specify changelog consumer registration id.\n");
1662 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1663 fprintf(stderr, "Please specify the source path.\n");
1667 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1668 fprintf(stderr, "Please specify the target path.\n");
1673 /* This plumbing is needed for some of the ioctls behind
1674 llapi calls to work. */
1675 if (obd_initialize(argc, argv) < 0) {
1676 fprintf(stderr, "obd_initialize failed.\n");
1680 rc = lr_locate_rsync();
1681 if (use_rsync && rc != 0) {
1682 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1686 signal(SIGINT, termination_handler);
1687 signal(SIGHUP, termination_handler);
1688 signal(SIGTERM, termination_handler);
1690 rc = lr_replicate();