1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/lreplicate.c
38 * Author: Kalpak Shah <Kalpak.Shah@Sun.COM>
39 * Author: Manoj Joseph <Manoj.Joseph@Sun.COM>
43 * - lreplicate is a tool for replicating a lustre filesystem.
45 * - The source-fs is a live lustre filesystem. It is not a
46 * snapshot. It is mounted and undergoing changes
48 * - The target-fs is a copy of the source-fs from the past. Let's
49 * call this point, the 'sync point'.
51 * - There is a changelog of all metadata operations that happened on
52 * the filesystem since the 'sync point'.
54 * - lreplicate replicates all the operations saved in the changelog
55 * on to the target filesystem to make it identical to the source.
57 * To facilitate replication, the lustre filesystem provides
58 * a) a way to get the current filesystem path of a given FID
59 * b) a way to open files by specifying its FID
61 * The changelog only has a limited amount of information.
62 * tfid - The FID of the target file
63 * pfid - The FID of the parent of the target file (at the time of
65 * name - The name of the target file (at the time of the operation)
67 * With just this information, it is not alwasy possible to determine
68 * the file paths for each operation. For instance, if pfid does not
69 * exist on the source-fs (due to a subsequent deletion), its path
70 * cannot be queried. In such cases, lreplicate keeps the files in a
71 * special directory ("/.lustrerepl"). Once all the operations in a
72 * changelog are replayed, all the files in this special directory
73 * will get moved to the location as in the source-fs.
75 * Shorthand used: f2p(tfid) = fid2path(tfid)
77 * The following are the metadata operations of interest.
79 * If tfid is absent on the source-fs, ignore this operation
80 * If pfid is absent on the source-fs [or]
81 * if f2p(pfid) is not present on target-fs [or]
82 * if f2p(pfid)+name != f2p(tfid)
83 * creat .lustrerepl/tfid
84 * track [pfid,tfid,name]
89 * If .lustrerepl/[tfid] is present on the target
90 * rm .lustrerepl/[tfid]
91 * Else if pfid is present on the source-fs,
92 * if f2p(pfid)+name is present,
93 * rm f2p(pfid)+name(pfid,name)
95 * 3. move (pfid1,name1) to (pfid2,name2)
97 * if pfid1 is also present, mv (pfid1,name1) to (pfid2,name2)
98 * else mv .lustrerepl/[tfid] to (pfid2,name2)
99 * If pfid2 is not present,
100 * if pfid1 is present, mv (pfid1,name1) .lustrerepl/[tfid]
101 * If moving out of .lustrerepl
102 * move out all its children in .lustrerepl.
103 * [pfid,tfid,name] tracked from (1) is used for this.
113 #include <sys/stat.h>
114 #include <sys/types.h>
118 #include <sys/xattr.h>
120 #include <libcfs/libcfs.h>
121 #include <libcfs/libcfsutil.h>
122 #include <lustre/liblustreapi.h>
123 #include <lustre/lustre_idl.h>
124 #include "lreplicate.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 enum changelog_rec_type type;
152 char pfid[LR_FID_STR_LEN];
153 char tfid[LR_FID_STR_LEN];
154 char name[PATH_MAX + 1];
155 char src[PATH_MAX + 1];
156 char dest[PATH_MAX + 1];
157 char path[PATH_MAX + 1];
158 char savedpath[PATH_MAX + 1];
159 char link[PATH_MAX + 1];
160 char linktmp[PATH_MAX + 1];
165 /* Variables for querying the xattributes */
172 struct lr_parent_child_list {
173 struct lr_parent_child_log pc_log;
174 struct lr_parent_child_list *pc_next;
177 struct lreplicate_status *status;
178 char *statuslog; /* Name of the status log file */
180 int noxattr; /* Flag to turn off replicating xattrs */
181 int noclear; /* Flag to turn off clearing changelogs */
182 int debug; /* Flag to turn debugging information on and off */
183 int verbose; /* Verbose output */
184 long long rec_count; /* No of changelog records that were processed */
187 int use_rsync; /* Flag to turn on use of rsync to copy data */
188 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
189 int quit; /* Flag to stop processing the changelog; set on the
190 receipt of a signal */
191 int abort_on_err = 0;
193 char rsync[PATH_MAX];
194 char rsync_ver[PATH_MAX];
195 struct lr_parent_child_list *parents;
197 /* Command line options */
198 struct option long_opts[] = {
199 {"source", required_argument, 0, 's'},
200 {"target", required_argument, 0, 't'},
201 {"mdt", required_argument, 0, 'm'},
202 {"user", required_argument, 0, 'u'},
203 {"statuslog", required_argument, 0, 'l'},
204 {"verbose", no_argument, 0, 'v'},
205 {"xattr", required_argument, 0, 'x'},
206 {"dry-run", no_argument, 0, 'z'},
207 /* Undocumented options follow */
208 {"cl-clear", required_argument, 0, 'c'},
209 {"use-rsync", no_argument, 0, 'r'},
210 {"rsync-threshold", required_argument, 0, 'y'},
211 {"start-recno", required_argument, 0, 'n'},
212 {"abort-on-err",no_argument, 0, 'a'},
213 {"debug", required_argument, 0, 'd'},
217 /* Command line usage */
220 fprintf(stderr, "\tlreplicate -s <lustre_root_path> -t <target_path> "
221 "-m <mdt> -r <user id> -l <status log>\n"
222 "lreplicate can also pick up parameters from a "
223 "status log created earlier.\n"
224 "\tlreplicate -l <log_file>\n"
226 "\t--xattr <yes|no> replicate EAs\n"
227 "\t--abort-on-err abort at first err\n"
229 "\t--dry-run don't write anything\n");
232 /* Print debug information. This is controlled by the value of the
233 global variable 'debug' */
234 void lr_debug(int level, const char *fmt, ...)
247 void * lr_grow_buf(void *buf, int size)
251 ptr = realloc(buf, size);
258 /* Use rsync to replicate file data */
259 int lr_rsync_data(struct lr_info *info)
262 struct stat st_src, st_dest;
265 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
267 rc = stat(info->src, &st_src);
269 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
270 info->src, info->name);
276 rc = stat(info->dest, &st_dest);
278 fprintf(stderr, "Error: Unable to stat dest=%s\n",
283 if (st_src.st_mtime != st_dest.st_mtime ||
284 st_src.st_size != st_dest.st_size) {
285 /* XXX spawning off an rsync for every data sync and
286 * waiting synchronously is bad for performance.
287 * librsync could possibly used here. But it does not
288 * seem to be of production grade. Multi-threaded
289 * replication is also to be considered.
292 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
294 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
295 status = system(cmd);
298 } else if (WIFEXITED(status)) {
299 status = WEXITSTATUS(status);
302 else if (status == 23 || status == 24)
303 /* Error due to vanished source files;
309 lr_debug(DINFO, "rsync %s exited with %d %d\n",
310 info->src, status, rc);
315 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
316 info->dest, info->tfid);
322 int lr_copy_data(struct lr_info *info)
332 fd_src = open(info->src, O_RDONLY);
335 if (fstat(fd_src, &st_src) == -1 ||
336 stat(info->dest, &st_dest) == -1)
339 if (st_src.st_mtime == st_dest.st_mtime &&
340 st_src.st_size == st_dest.st_size)
343 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
344 /* It is more efficient to use rsync to replicate
345 large files. Any file larger than rsync_threshold
346 is handed off to rsync. */
347 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
348 rc = lr_rsync_data(info);
352 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
357 bufsize = st_dest.st_blksize;
359 if (info->bufsize < bufsize) {
361 info->buf = lr_grow_buf(info->buf, bufsize);
362 if (info->buf == NULL) {
366 info->bufsize = bufsize;
370 rsize = read(fd_src, info->buf, bufsize);
373 } else if (rsize < 0) {
378 if (write(fd_dest, info->buf, rsize) != rsize) {
396 /* Copy data from source to destination */
397 int lr_sync_data(struct lr_info *info)
400 return lr_rsync_data(info);
402 return lr_copy_data(info);
405 /* Copy all attributes from file src to file dest */
406 int lr_copy_attr(char *src, char *dest)
411 if (stat(src, &st) == -1 ||
412 chmod(dest, st.st_mode) == -1 ||
413 chown(dest, st.st_uid, st.st_gid) == -1)
416 time.actime = st.st_atime;
417 time.modtime = st.st_mtime;
418 if (utime(dest, &time) == -1)
423 /* Copy all xattrs from file info->src to info->dest */
424 int lr_copy_xattr(struct lr_info *info)
426 size_t size = info->xsize;
435 rc = llistxattr(info->src, info->xlist, size);
436 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
437 info->src, info->xlist, rc, errno);
438 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
439 size = rc > PATH_MAX ? rc : PATH_MAX;
440 info->xlist = lr_grow_buf(info->xlist, size);
441 if (info->xlist == NULL)
444 rc = llistxattr(info->src, info->xlist, size);
445 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
446 info->src, rc, errno);
453 while (start < len) {
455 rc = lgetxattr(info->src, info->xlist + start,
457 if (info->xvalue == NULL || errno == ERANGE) {
458 size = rc > PATH_MAX ? rc : PATH_MAX;
459 info->xvalue = lr_grow_buf(info->xvalue, size);
460 if (info->xvalue == NULL)
463 rc = lgetxattr(info->src, info->xlist + start,
466 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
470 rc = lsetxattr(info->dest, info->xlist + start,
471 info->xvalue, size, 0);
472 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
475 if (errno != ENOTSUP) {
476 fprintf(stderr, "Error replicating "
477 " xattr for %s: %d\n",
484 start += strlen(info->xlist + start) + 1;
487 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
492 /* Retrieve the filesystem path for a given FID and a given
493 linkno. The path is returned in info->path */
494 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
496 long long recno = -1;
499 rc = llapi_fid2path(status->ls_source, fidstr, info->path,
500 PATH_MAX, &recno, &linkno);
501 if (rc < 0 && rc != -ENOENT) {
502 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
503 status->ls_source, fidstr, -rc, strerror(errno = -rc));
509 /* Retrieve the filesystem path for a given FID. The path is returned
511 int lr_get_path(struct lr_info *info, char *fidstr)
513 return lr_get_path_ln(info, fidstr, 0);
516 /* Generate the path for opening by FID */
517 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
519 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
520 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
525 /* Read the symlink information into 'info->link' */
526 int lr_get_symlink(struct lr_info *info)
531 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
532 rc = readlink(info->src, info->linktmp, PATH_MAX);
534 info->linktmp[rc] = '\0';
537 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
539 if (strncmp(info->linktmp, status->ls_source,
540 strlen(status->ls_source)) == 0) {
541 /* Strip source fs path and replace with target fs path. */
542 link = info->linktmp + strlen(status->ls_source);
543 snprintf(info->src, PATH_MAX, "%s%s",
544 status->ls_targets[info->target_no], link);
547 link = info->linktmp;
549 strncpy(info->link, link, PATH_MAX);
550 info->link[PATH_MAX] = '\0';
555 /* Create file/directory/device file/symlink. */
556 int lr_mkfile(struct lr_info *info)
562 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
563 if (info->type == CL_MKDIR) {
564 rc = mkdir(info->dest, 0777);
565 } else if (info->type == CL_SOFTLINK) {
566 lr_get_symlink(info);
567 rc = symlink(info->link, info->dest);
568 } else if (info->type == CL_MKNOD) {
569 lr_get_FID_PATH(status->ls_source, info->tfid,
570 info->src, PATH_MAX);
571 rc = stat(info->src, &st);
578 rc = mknod(info->dest, st.st_mode, st.st_rdev);
580 rc = mknod(info->dest, S_IFREG | 0777, 0);
585 /* Sync data and attributes */
586 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
587 lr_debug(DTRACE, "Syncing data and attributes %s\n",
589 (void) lr_copy_xattr(info);
590 if (info->type == CL_CREATE)
591 rc = lr_sync_data(info);
593 rc = lr_copy_attr(info->src, info->dest);
596 /* Source file has disappeared. Not an error. */
599 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
606 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
608 struct lr_parent_child_list *p;
610 p = calloc(1, sizeof(*p));
613 strcpy(p->pc_log.pcl_pfid, pfid + 2);
614 strcpy(p->pc_log.pcl_tfid, tfid + 2);
615 strcpy(p->pc_log.pcl_name, name);
617 p->pc_next = parents;
622 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
624 struct lr_parent_child_list *curr, *prev;
628 d = calloc(1, PATH_MAX + 1);
629 prev = curr = parents;
631 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
632 snprintf(d, PATH_MAX, "%s/%s", dest,
633 curr->pc_log.pcl_name);
634 snprintf(info->src, PATH_MAX, "%s/%s/%s",
635 status->ls_targets[info->target_no],
636 SPECIAL_DIR, curr->pc_log.pcl_tfid);
637 rc = rename(info->src, d);
639 fprintf(stderr, "Error renaming file "
641 info->src, d, errno);
644 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
646 parents = curr->pc_next;
648 prev->pc_next = curr->pc_next;
650 prev = curr = parents;
654 curr = curr->pc_next;
661 /* remove [info->pfid, ext->tfid] from parents */
662 int lr_remove_pc(const char *pfid, const char *tfid)
664 struct lr_parent_child_list *curr, *prev;
666 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
667 if (strcmp(curr->pc_log.pcl_pfid, pfid + 2) == 0 &&
668 strcmp(curr->pc_log.pcl_tfid, tfid + 2) == 0) {
670 parents = curr->pc_next;
672 prev->pc_next = curr->pc_next;
680 /* Create file under SPECIAL_DIR with its tfid as its name. */
681 int lr_mk_special(struct lr_info *info)
685 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
686 status->ls_targets[info->target_no], SPECIAL_DIR,
689 rc = lr_mkfile(info);
693 rc = lr_add_pc(info->pfid, info->tfid, info->name);
697 /* Remove a file or directory */
698 int lr_rmfile(struct lr_info *info)
702 if (info->type == CL_RMDIR)
703 rc = rmdir(info->dest);
705 rc = unlink(info->dest);
711 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
712 int lr_rm_special(struct lr_info *info)
716 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
717 status->ls_targets[info->target_no], SPECIAL_DIR,
719 rc = lr_rmfile(info);
722 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
723 info->dest, rc, errno);
727 /* Replicate file and directory create events */
728 int lr_create(struct lr_info *info)
735 /* Is target FID present on the source? */
736 rc = lr_get_path(info, info->tfid + 3);
738 /* Source file has disappeared. Not an error. */
739 lr_debug(DINFO, "create: tfid %s not found on"
740 "source-fs\n", info->tfid);
745 strcpy(info->savedpath, info->path);
747 /* Is parent FID present on the source */
748 rc = lr_get_path(info, info->pfid + 3);
750 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
757 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
758 len = strlen(info->path);
759 if (len - 1 >= 0 && info->path[len - 1] == '/')
760 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
762 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
764 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
766 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
767 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
768 info->tfid, info->dest, info->savedpath);
772 /* Is f2p(pfid) present on the target? If not, the parent has
775 snprintf(info->dest, PATH_MAX, "%s%s", status->ls_targets[0],
777 if (access(info->dest, F_OK) != 0)
780 for (info->target_no = 0; info->target_no < status->ls_num_targets;
782 snprintf(info->dest, PATH_MAX, "%s%s",
783 status->ls_targets[info->target_no], info->savedpath);
784 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
788 rc1 = lr_mkfile(info);
789 if (mkspecial || rc1 == -ENOENT) {
790 rc1 = lr_mk_special(info);
798 /* Replicate a file remove (rmdir/unlink) operation */
799 int lr_remove(struct lr_info *info)
804 for (info->target_no = 0; info->target_no < status->ls_num_targets;
807 rc1 = lr_rm_special(info);
811 rc1 = lr_get_path(info, info->pfid + 3);
812 if (rc1 == -ENOENT) {
813 lr_debug(DINFO, "remove: pfid %s not found\n",
821 snprintf(info->dest, PATH_MAX, "%s%s/%s",
822 status->ls_targets[info->target_no], info->path,
825 rc1 = lr_rmfile(info);
826 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
827 info->dest, rc1, errno);
836 /* Replicate a rename/move operation. This operations are tracked by
837 two changelog records. */
838 int lr_move(struct lr_info *info, struct lr_info *ext)
844 int special_dest = 0;
846 rc_dest = lr_get_path(ext, ext->pfid + 3);
847 if (rc_dest < 0 && rc_dest != -ENOENT)
850 rc_src = lr_get_path(info, info->pfid + 3);
851 if (rc_src < 0 && rc_src != -ENOENT)
854 for (info->target_no = 0; info->target_no < status->ls_num_targets;
858 snprintf(info->dest, PATH_MAX, "%s%s",
859 status->ls_targets[info->target_no],
861 if (access(info->dest, F_OK) != 0) {
864 snprintf(info->dest, PATH_MAX, "%s%s/%s",
865 status->ls_targets[info->target_no],
866 ext->path, ext->name);
869 if (rc_dest == -ENOENT) {
870 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
871 status->ls_targets[info->target_no],
872 SPECIAL_DIR, info->tfid + 2);
877 snprintf(info->src, PATH_MAX, "%s%s/%s",
878 status->ls_targets[info->target_no],
879 info->path, info->name);
880 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
882 snprintf(info->src, PATH_MAX, "%s/%s/%s",
883 status->ls_targets[info->target_no],
884 SPECIAL_DIR, info->tfid + 2);
889 if (strcmp(info->src, info->dest) != 0) {
890 rc1 = rename(info->src, info->dest);
896 lr_remove_pc(info->pfid, info->tfid);
898 lr_cascade_move(info->tfid + 2, info->dest, info);
901 lr_add_pc(ext->pfid, info->tfid, ext->name);
903 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
904 info->src, info->dest, rc1, errno);
911 /* Replicate a hard link */
912 int lr_link(struct lr_info *info)
920 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
921 rc = stat(info->src, &st);
925 for (info->target_no = 0; info->target_no < status->ls_num_targets;
932 /* Search through the hardlinks to get the src and dest */
933 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
934 info->dest[0] == 0); i++) {
935 rc1 = lr_get_path_ln(info, info->tfid + 3, i);
936 lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
937 info->path, info->name, i, rc1);
941 len = strlen(info->path) - strlen(info->name);
942 if (len > 0 && strcmp(info->path + len,
944 snprintf(info->dest, PATH_MAX, "%s%s",
945 status->ls_targets[info->target_no],
947 else if (info->src[0] == 0)
948 snprintf(info->src, PATH_MAX, "%s%s",
949 status->ls_targets[info->target_no],
958 if (info->src[0] == 0 || info->dest[0] == 0)
959 /* Could not find the source or destination.
960 This can happen when some links don't exist
964 if (info->src[0] == 0)
965 snprintf(info->src, PATH_MAX, "%s/%s/%s",
966 status->ls_targets[info->target_no],
967 SPECIAL_DIR, info->tfid + 2);
968 else if (info->dest[0] == 0)
969 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
970 status->ls_targets[info->target_no],
971 SPECIAL_DIR, info->tfid + 2);
973 rc1 = link(info->src, info->dest);
974 lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
975 info->src, info->dest, rc1, strerror(errno));
983 /* Replicate file attributes */
984 int lr_setattr(struct lr_info *info)
989 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
991 rc = lr_get_path(info, info->tfid + 3);
993 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
998 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1001 snprintf(info->dest, PATH_MAX, "%s%s",
1002 status->ls_targets[info->target_no], info->path);
1003 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1006 rc1 = lr_sync_data(info);
1008 rc1 = lr_copy_attr(info->src, info->dest);
1015 /* Replicate xattrs */
1016 int lr_setxattr(struct lr_info *info)
1020 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1022 rc = lr_get_path(info, info->tfid + 3);
1024 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1029 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1030 info->target_no++) {
1032 snprintf(info->dest, PATH_MAX, "%s%s",
1033 status->ls_targets[info->target_no], info->path);
1034 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1037 rc1 = lr_copy_xattr(info);
1045 /* Parse a line of changelog entry */
1046 int lr_parse_line(struct lr_info *info, FILE *fp)
1048 unsigned long long time;
1050 char typestr[TYPE_STR_LEN];
1051 char line[PATH_MAX];
1055 if (fgets(line, sizeof(line), fp) != NULL) {
1056 if (sscanf(line, "%llu %s %llu %x %s %s",
1057 &info->recno, typestr, &time,
1058 &flags, info->tfid, info->pfid) < 4) {
1059 fprintf(stderr, "error: unexpected changelog record "
1060 "format - %s\n", line);
1064 info->type = atoi(typestr);
1066 /* The filename could have spaces in it. scanf would
1067 have ignored it. Parse for the complete
1069 if (info->type != CL_SETATTR &&
1070 info->type != CL_XATTR &&
1071 info->type != CL_MARK) {
1072 for (i = 0, str = line; str != NULL && i <= 5;
1074 str = strchr(str, ' ');
1077 strncpy(info->name, str, PATH_MAX);
1078 str = strchr(info->name, '\n');
1082 fprintf(stderr, "error: unexpected changelog "
1083 "record format - %s\n", line);
1094 /* Initialize the replication parameters */
1095 int lr_init_status()
1097 size_t size = sizeof(struct lreplicate_status) + PATH_MAX;
1101 status = calloc(size, 1);
1104 status->ls_version = REPLICATE_STATUS_VER;
1105 status->ls_size = size;
1106 status->ls_last_recno = -1;
1110 /* Make a backup of the statuslog */
1111 void lr_backup_log()
1113 char backupfile[PATH_MAX];
1117 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1118 (void) rename(statuslog, backupfile);
1124 /* Save replication parameters to a statuslog. */
1129 size_t write_size = status->ls_size;
1130 struct lr_parent_child_list *curr;
1133 if (statuslog == NULL)
1138 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC);
1140 fprintf(stderr, "Error opening log file for writing (%s)\n",
1145 size = write(fd, status, write_size);
1146 if (size != write_size) {
1147 fprintf(stderr, "Error writing to log file (%s) %d\n",
1153 for (curr = parents; curr; curr = curr->pc_next) {
1154 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1155 if (size != sizeof(curr->pc_log)) {
1156 fprintf(stderr, "Error writing to log file (%s) %d\n",
1166 /* Read statuslog and populate the replication parameters. Command
1167 * line parameters take precedence over parameters in the log file.*/
1170 struct lr_parent_child_list *tmp;
1171 struct lr_parent_child_log rec;
1172 struct lreplicate_status *s;
1175 size_t read_size = sizeof(struct lreplicate_status) + PATH_MAX;
1178 if (statuslog == NULL)
1181 s = calloc(1, read_size);
1183 GOTO(out, rc = -ENOMEM);
1185 fd = open(statuslog, O_RDONLY);
1187 GOTO(out, rc = -errno);
1188 size = read(fd, s, read_size);
1189 if (size != read_size)
1190 GOTO(out, rc = -EINVAL);
1191 if (read_size < s->ls_size) {
1192 read_size = s->ls_size;
1193 s = lr_grow_buf(s, read_size);
1195 GOTO(out, rc = -ENOMEM);
1196 if (lseek(fd, 0, SEEK_SET) == -1)
1197 GOTO(out, rc = -errno);
1198 size = read(fd, s, read_size);
1199 if (size != read_size)
1200 GOTO(out, rc = -EINVAL);
1203 while (read(fd, &rec, sizeof(rec)) != 0) {
1204 tmp = calloc(1, sizeof(*tmp));
1206 GOTO(out, rc = -ENOMEM);
1208 tmp->pc_next = parents;
1212 /* copy uninitialized fields to status */
1213 if (status->ls_num_targets == 0) {
1214 if (status->ls_size != s->ls_size) {
1215 status = lr_grow_buf(status, s->ls_size);
1217 GOTO(out, rc = -ENOMEM);
1218 status->ls_size = s->ls_size;
1220 status->ls_num_targets = s->ls_num_targets;
1221 memcpy(status->ls_targets, s->ls_targets,
1222 PATH_MAX * s->ls_num_targets);
1224 if (status->ls_last_recno == -1)
1225 status->ls_last_recno = s->ls_last_recno;
1227 if (status->ls_registration[0] == '\0')
1228 strncpy(status->ls_registration, s->ls_registration,
1231 if (status->ls_mdt_device[0] == '\0')
1232 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1235 if (status->ls_source_fs[0] == '\0')
1236 strncpy(status->ls_source_fs, s->ls_source_fs,
1239 if (status->ls_source[0] == '\0')
1240 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1250 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1252 int lr_clear_cl(struct lr_info *info, int force)
1254 char mdt_device[LR_NAME_MAXLEN + 1];
1258 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1259 if (info->type == CL_RENAME)
1260 rec = info->recno + 1;
1263 if (!noclear && !dryrun) {
1264 /* llapi_changelog_clear modifies the mdt
1265 * device name so make a copy of it until this
1268 strncpy(mdt_device, status->ls_mdt_device,
1270 rc = llapi_changelog_clear(mdt_device,
1271 status->ls_registration,
1274 printf("Changelog clear (%s, %s, %lld) "
1275 "returned %d\n", status->ls_mdt_device,
1276 status->ls_registration, rec, rc);
1278 if (!rc && !dryrun) {
1279 status->ls_last_recno = rec;
1288 /* Locate a usable version of rsync. At this point we'll use any
1290 int lr_locate_rsync()
1296 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1297 fp = popen(rsync, "r");
1301 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1306 len = strlen(rsync);
1307 if (len > 0 && rsync[len - 1] == '\n')
1308 rsync[len - 1] = '\0';
1311 /* Determine the version of rsync */
1312 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1313 fp = popen(rsync_ver, "r");
1317 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1321 len = strlen(rsync_ver);
1322 if (len > 0 && rsync_ver[len - 1] == '\n')
1323 rsync_ver[len - 1] = '\0';
1330 /* Print the replication parameters */
1331 void lr_print_status(struct lr_info *info)
1338 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1339 printf("MDT device: %s\n", status->ls_mdt_device);
1340 printf("Source: %s\n", status->ls_source);
1341 for (i = 0; i < status->ls_num_targets; i++)
1342 printf("Target: %s\n", status->ls_targets[i]);
1343 if (statuslog != NULL)
1344 printf("Statuslog: %s\n", statuslog);
1345 printf("Changelog registration: %s\n", status->ls_registration);
1346 printf("Starting changelog record: %lld\n", status->ls_last_recno);
1348 printf("Replicate xattrs: no\n");
1350 printf("Clear changelog after use: no\n");
1352 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1355 DECLARE_CHANGELOG_NAMES;
1357 void lr_print_failure(struct lr_info *info, int rc)
1359 fprintf(stderr, "Replication of operation failed(%d):"
1360 " %lld %s (%d) %s %s %s\n", rc, info->recno,
1361 changelog_str[info->type], info->type, info->tfid,
1362 info->pfid, info->name);
1365 /* Replicate filesystem operations from src_path to target_path */
1371 struct lr_info *info;
1372 struct lr_info *ext;
1380 info = calloc(1, sizeof(struct lr_info));
1384 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1386 fprintf(stderr, "Source path is not a valid Lustre client "
1390 if (status->ls_mdt_device[0] == '\0')
1391 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1392 status->ls_source_fs, DEFAULT_MDT);
1394 ext = calloc(1, sizeof(struct lr_info));
1397 memcpy(ext, info, sizeof(struct lr_info));
1399 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1400 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1402 rc = mkdir(info->dest, 0777);
1403 if (rc == -1 && errno != EEXIST) {
1404 fprintf(stderr, "Error writing to target path %s.\n",
1405 status->ls_targets[i]);
1408 rc = llistxattr(info->src, info->xlist, info->xsize);
1409 if (rc == -1 && errno == ENOTSUP) {
1410 fprintf(stderr, "xattrs not supported on %s\n",
1411 status->ls_targets[i]);
1415 if (xattr_not_supp == status->ls_num_targets)
1416 /* None of the targets support xattrs. */
1419 lr_print_status(info);
1421 /* Open changelogs for consumption*/
1422 startrec = status->ls_last_recno;
1423 fd = llapi_changelog_open(status->ls_source_fs, startrec);
1425 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1426 status->ls_source_fs);
1429 if ((fp = fdopen(fd, "r")) == NULL) {
1430 fprintf(stderr, "Error: fdopen failed.");
1435 while (!quit && lr_parse_line(info, fp) == 0) {
1437 if (info->type == CL_RENAME)
1438 /* Rename operations have an additional changelog
1439 record of information. */
1440 lr_parse_line(ext, fp);
1445 switch(info->type) {
1450 rc = lr_create(info);
1454 rc = lr_remove(info);
1457 rc = lr_move(info, ext);
1464 rc = lr_setattr(info);
1467 rc = lr_setxattr(info);
1474 /* Nothing needs to be done for these entries */
1478 if (rc && rc != -ENOENT) {
1479 lr_print_failure(info, rc);
1484 lr_clear_cl(info, 0);
1486 bzero(info, sizeof(struct lr_info));
1487 bzero(ext, sizeof(struct lr_info));
1491 if (errors || verbose)
1492 printf("Errors: %d\n", errors);
1494 /* Clear changelog records used so far */
1495 lr_clear_cl(info, 1);
1498 printf("lreplicate took %ld seconds\n", time(NULL) - start);
1499 printf("Changelog records consumed: %lld\n", rec_count);
1509 termination_handler (int signum)
1511 /* Set a flag for the replicator to gracefully shutdown */
1513 printf("lreplicate halting.\n");
1516 int main(int argc, char *argv[])
1523 if ((rc = lr_init_status()) != 0)
1526 while ((c = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:",
1527 long_opts, NULL)) >= 0) {
1530 /* Assume absolute paths */
1534 /* Assume absolute paths */
1535 strncpy(status->ls_source, optarg, PATH_MAX);
1538 status->ls_num_targets++;
1540 if (numtargets != status->ls_num_targets) {
1541 /* Targets were read from a log
1542 file. The ones specified on the
1543 command line take precedence. The
1544 ones from the log file will be
1546 status->ls_num_targets = numtargets;
1548 newsize = sizeof (struct lreplicate_status) +
1549 (status->ls_num_targets * PATH_MAX);
1550 if (status->ls_size != newsize) {
1551 status->ls_size = newsize;
1552 status = lr_grow_buf(status, newsize);
1556 strncpy(status->ls_targets[status->ls_num_targets - 1],
1561 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1564 strncpy(status->ls_registration, optarg,
1569 (void) lr_read_log();
1575 if (strcmp("no", optarg) == 0) {
1577 } else if (strcmp("yes", optarg) != 0) {
1578 printf("Invalid parameter %s. "
1579 "Specify --xattr=no or --xattr=yes\n",
1588 /* Undocumented option cl-clear */
1589 if (strcmp("no", optarg) == 0) {
1591 } else if (strcmp("yes", optarg) != 0) {
1592 printf("Invalid parameter %s. "
1593 "Specify --cl-clear=no "
1594 "or --cl-clear=yes\n",
1600 /* Undocumented option use-rsync */
1604 /* Undocumented option rsync-threshold */
1605 rsync_threshold = atol(optarg);
1608 /* Undocumented option start-recno */
1609 status->ls_last_recno = atol(optarg);
1612 /* Undocumented option debug */
1613 debug = atoi(optarg);
1614 if (debug < 0 || debug > 2)
1618 fprintf(stderr, "error: %s: option '%s' "
1619 "unrecognized.\n", argv[0], argv[optind - 1]);
1625 if (status->ls_last_recno == -1)
1626 status->ls_last_recno = 0;
1627 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1628 /* No registration ID was passed in. */
1629 printf("Please specify changelog consumer registration id.\n");
1633 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1634 fprintf(stderr, "Please specify the source path.\n");
1638 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1639 fprintf(stderr, "Please specify the target path.\n");
1644 /* This plumbing is needed for some of the ioctls behind
1645 llapi calls to work. */
1646 if (obd_initialize(argc, argv) < 0) {
1647 fprintf(stderr, "obd_initialize failed.\n");
1651 rc = lr_locate_rsync();
1652 if (use_rsync && rc != 0) {
1653 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1657 signal(SIGINT, termination_handler);
1658 signal(SIGHUP, termination_handler);
1659 signal(SIGTERM, termination_handler);
1661 rc = lr_replicate();