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 <lustre/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 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 {"debug", required_argument, 0, 'd'},
214 /* Command line usage */
217 fprintf(stderr, "\tlreplicate -s <lustre_root_path> -t <target_path> "
218 "-m <mdt> -r <user id> -l <status log>\n"
219 "lreplicate can also pick up parameters from a "
220 "status log created earlier.\n"
221 "\tlreplicate -l <log_file>\n");
224 /* Print debug information. This is controlled by the value of the
225 global variable 'debug' */
226 void lr_debug(int level, const char *fmt, ...)
239 void * lr_grow_buf(void *buf, int size)
243 ptr = realloc(buf, size);
250 /* Use rsync to replicate file data */
251 int lr_rsync_data(struct lr_info *info)
254 struct stat st_src, st_dest;
257 lr_debug(DTRACE, "Syncing data%s\n", info->tfid);
259 rc = stat(info->src, &st_src);
261 fprintf(stderr, "Error: Unable to stat src=%s %s\n",
262 info->src, info->name);
268 rc = stat(info->dest, &st_dest);
270 fprintf(stderr, "Error: Unable to stat dest=%s\n",
275 if (st_src.st_mtime != st_dest.st_mtime ||
276 st_src.st_size != st_dest.st_size) {
277 /* XXX spawning off an rsync for every data sync and
278 * waiting synchronously is bad for performance.
279 * librsync could possibly used here. But it does not
280 * seem to be of production grade. Multi-threaded
281 * replication is also to be considered.
284 snprintf(cmd, PATH_MAX, "%s --inplace %s %s", rsync, info->src,
286 lr_debug(DTRACE, "\t%s %s\n", cmd, info->tfid);
287 status = system(cmd);
290 } else if (WIFEXITED(status)) {
291 status = WEXITSTATUS(status);
294 else if (status == 23 || status == 24)
295 /* Error due to vanished source files;
301 lr_debug(DINFO, "rsync %s exited with %d %d\n",
302 info->src, status, rc);
307 lr_debug(DTRACE, "Not syncing %s and %s %s\n", info->src,
308 info->dest, info->tfid);
314 int lr_copy_data(struct lr_info *info)
324 fd_src = open(info->src, O_RDONLY);
327 if (fstat(fd_src, &st_src) == -1 ||
328 stat(info->dest, &st_dest) == -1)
331 if (st_src.st_mtime == st_dest.st_mtime &&
332 st_src.st_size == st_dest.st_size)
335 if (st_src.st_size > rsync_threshold && rsync[0] != '\0') {
336 /* It is more efficient to use rsync to replicate
337 large files. Any file larger than rsync_threshold
338 is handed off to rsync. */
339 lr_debug(DTRACE, "Using rsync to replicate %s\n", info->tfid);
340 rc = lr_rsync_data(info);
344 fd_dest = open(info->dest, O_WRONLY | O_TRUNC, st_src.st_mode);
349 bufsize = st_dest.st_blksize;
351 if (info->bufsize < bufsize) {
353 info->buf = lr_grow_buf(info->buf, bufsize);
354 if (info->buf == NULL) {
358 info->bufsize = bufsize;
362 rsize = read(fd_src, info->buf, bufsize);
365 } else if (rsize < 0) {
370 if (write(fd_dest, info->buf, rsize) != rsize) {
388 /* Copy data from source to destination */
389 int lr_sync_data(struct lr_info *info)
392 return lr_rsync_data(info);
394 return lr_copy_data(info);
397 /* Copy all attributes from file src to file dest */
398 int lr_copy_attr(char *src, char *dest)
403 if (stat(src, &st) == -1 ||
404 chmod(dest, st.st_mode) == -1 ||
405 chown(dest, st.st_uid, st.st_gid) == -1)
408 time.actime = st.st_atime;
409 time.modtime = st.st_mtime;
410 if (utime(dest, &time) == -1)
415 /* Copy all xattrs from file info->src to info->dest */
416 int lr_copy_xattr(struct lr_info *info)
418 size_t size = info->xsize;
427 rc = llistxattr(info->src, info->xlist, size);
428 lr_debug(DTRACE, "llistxattr(%s,%p) returned %d, errno=%d\n",
429 info->src, info->xlist, rc, errno);
430 if ((rc > 0 && info->xlist == NULL) || errno == ERANGE) {
431 size = rc > PATH_MAX ? rc : PATH_MAX;
432 info->xlist = lr_grow_buf(info->xlist, size);
433 if (info->xlist == NULL)
436 rc = llistxattr(info->src, info->xlist, size);
437 lr_debug(DTRACE, "llistxattr %s returned %d, errno=%d\n",
438 info->src, rc, errno);
445 while (start < len) {
447 rc = lgetxattr(info->src, info->xlist + start,
449 if (info->xvalue == NULL || errno == ERANGE) {
450 size = rc > PATH_MAX ? rc : PATH_MAX;
451 info->xvalue = lr_grow_buf(info->xvalue, size);
452 if (info->xvalue == NULL)
455 rc = lgetxattr(info->src, info->xlist + start,
458 lr_debug(DTRACE, "\t(%s,%d) rc=%p\n", info->xlist + start,
462 rc = lsetxattr(info->dest, info->xlist + start,
463 info->xvalue, size, 0);
464 lr_debug(DTRACE, "\tlsetxattr(), rc=%d, errno=%d\n",
467 if (errno != ENOTSUP) {
468 fprintf(stderr, "Error replicating "
469 " xattr for %s: %d\n",
476 start += strlen(info->xlist + start) + 1;
479 lr_debug(DINFO, "setxattr: %s %s\n", info->src, info->dest);
484 /* Retrieve the filesystem path for a given FID and a given
485 linkno. The path is returned in info->path */
486 int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
488 long long recno = -1;
491 rc = llapi_fid2path(status->ls_mdt_device, fidstr, info->path,
492 PATH_MAX, &recno, &linkno);
493 if (rc < 0 && rc != -ENOENT) {
494 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
495 status->ls_mdt_device, fidstr,
496 -rc, strerror(errno = -rc));
502 /* Retrieve the filesystem path for a given FID. The path is returned
504 int lr_get_path(struct lr_info *info, char *fidstr)
506 return lr_get_path_ln(info, fidstr, 0);
509 /* Generate the path for opening by FID */
510 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
512 /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
513 snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, mdd_dot_lustre_name,
518 /* Read the symlink information into 'info->link' */
519 int lr_get_symlink(struct lr_info *info)
524 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
525 rc = readlink(info->src, info->linktmp, PATH_MAX);
527 info->linktmp[rc] = '\0';
530 lr_debug(DTRACE, "symlink: readlink returned %s\n", info->linktmp);
532 if (strncmp(info->linktmp, status->ls_source,
533 strlen(status->ls_source)) == 0) {
534 /* Strip source fs path and replace with target fs path. */
535 link = info->linktmp + strlen(status->ls_source);
536 snprintf(info->src, PATH_MAX, "%s%s",
537 status->ls_targets[info->target_no], link);
540 link = info->linktmp;
542 strncpy(info->link, link, PATH_MAX);
543 info->link[PATH_MAX] = '\0';
548 /* Create file/directory/device file/symlink. */
549 int lr_mkfile(struct lr_info *info)
555 lr_debug(DINFO, "mkfile(%d) %s \n", info->type, info->dest);
556 if (info->type == CL_MKDIR) {
557 rc = mkdir(info->dest, 0777);
558 } else if (info->type == CL_SOFTLINK) {
559 lr_get_symlink(info);
560 rc = symlink(info->link, info->dest);
561 } else if (info->type == CL_MKNOD) {
562 lr_get_FID_PATH(status->ls_source, info->tfid,
563 info->src, PATH_MAX);
564 rc = stat(info->src, &st);
571 rc = mknod(info->dest, st.st_mode, st.st_rdev);
573 rc = mknod(info->dest, S_IFREG | 0777, 0);
578 /* Sync data and attributes */
579 if (info->type == CL_CREATE || info->type == CL_MKDIR) {
580 lr_debug(DTRACE, "Syncing data and attributes %s\n",
582 (void) lr_copy_xattr(info);
583 if (info->type == CL_CREATE)
584 rc = lr_sync_data(info);
586 rc = lr_copy_attr(info->src, info->dest);
589 /* Source file has disappeared. Not an error. */
592 lr_debug(DTRACE, "Not syncing data and attributes %s\n",
599 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
601 struct lr_parent_child_list *p;
603 p = calloc(1, sizeof(*p));
606 strcpy(p->pc_log.pcl_pfid, pfid + 2);
607 strcpy(p->pc_log.pcl_tfid, tfid + 2);
608 strcpy(p->pc_log.pcl_name, name);
610 p->pc_next = parents;
615 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
617 struct lr_parent_child_list *curr, *prev;
621 d = calloc(1, PATH_MAX + 1);
622 prev = curr = parents;
624 if (strcmp(curr->pc_log.pcl_pfid, fid) == 0) {
625 snprintf(d, PATH_MAX, "%s/%s", dest,
626 curr->pc_log.pcl_name);
627 snprintf(info->src, PATH_MAX, "%s/%s/%s",
628 status->ls_targets[info->target_no],
629 SPECIAL_DIR, curr->pc_log.pcl_tfid);
630 rc = rename(info->src, d);
632 fprintf(stderr, "Error renaming file "
634 info->src, d, errno);
637 lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
639 parents = curr->pc_next;
641 prev->pc_next = curr->pc_next;
643 prev = curr = parents;
647 curr = curr->pc_next;
654 /* remove [info->pfid, ext->tfid] from parents */
655 int lr_remove_pc(const char *pfid, const char *tfid)
657 struct lr_parent_child_list *curr, *prev;
659 for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
660 if (strcmp(curr->pc_log.pcl_pfid, pfid + 2) == 0 &&
661 strcmp(curr->pc_log.pcl_tfid, tfid + 2) == 0) {
663 parents = curr->pc_next;
665 prev->pc_next = curr->pc_next;
673 /* Create file under SPECIAL_DIR with its tfid as its name. */
674 int lr_mk_special(struct lr_info *info)
678 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
679 status->ls_targets[info->target_no], SPECIAL_DIR,
682 rc = lr_mkfile(info);
686 rc = lr_add_pc(info->pfid, info->tfid, info->name);
690 /* Remove a file or directory */
691 int lr_rmfile(struct lr_info *info)
695 if (info->type == CL_RMDIR)
696 rc = rmdir(info->dest);
698 rc = unlink(info->dest);
704 /* Remove a file under SPECIAL_DIR with its tfid as its name. */
705 int lr_rm_special(struct lr_info *info)
709 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
710 status->ls_targets[info->target_no], SPECIAL_DIR,
712 rc = lr_rmfile(info);
715 lr_debug(DINFO, "remove: %s; rc=%d, errno=%d\n",
716 info->dest, rc, errno);
720 /* Replicate file and directory create events */
721 int lr_create(struct lr_info *info)
728 /* Is target FID present on the source? */
729 rc = lr_get_path(info, info->tfid + 3);
731 /* Source file has disappeared. Not an error. */
732 lr_debug(DINFO, "create: tfid %s not found on"
733 "source-fs\n", info->tfid);
738 strcpy(info->savedpath, info->path);
740 /* Is parent FID present on the source */
741 rc = lr_get_path(info, info->pfid + 3);
743 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
750 /* Is f2p(pfid)+name != f2p(tfid)? If not the file has moved. */
751 len = strlen(info->path);
752 if (len - 1 >= 0 && info->path[len - 1] == '/')
753 snprintf(info->dest, PATH_MAX, "%s%s", info->path, info->name);
755 snprintf(info->dest, PATH_MAX, "%s/%s", info->path, info->name);
757 lr_debug(DTRACE, "dest = %s; savedpath = %s\n", info->dest,
759 if (strncmp(info->dest, info->savedpath, PATH_MAX) != 0) {
760 lr_debug(DTRACE, "create: file moved (%s). %s != %s\n",
761 info->tfid, info->dest, info->savedpath);
765 /* Is f2p(pfid) present on the target? If not, the parent has
768 snprintf(info->dest, PATH_MAX, "%s%s", status->ls_targets[0],
770 if (access(info->dest, F_OK) != 0)
773 for (info->target_no = 0; info->target_no < status->ls_num_targets;
775 snprintf(info->dest, PATH_MAX, "%s%s",
776 status->ls_targets[info->target_no], info->savedpath);
777 lr_get_FID_PATH(status->ls_source, info->tfid, info->src,
781 rc1 = lr_mkfile(info);
782 if (mkspecial || rc1 == -ENOENT) {
783 rc1 = lr_mk_special(info);
791 /* Replicate a file remove (rmdir/unlink) operation */
792 int lr_remove(struct lr_info *info)
797 for (info->target_no = 0; info->target_no < status->ls_num_targets;
800 rc1 = lr_rm_special(info);
804 rc1 = lr_get_path(info, info->pfid + 3);
805 if (rc1 == -ENOENT) {
806 lr_debug(DINFO, "remove: pfid %s not found\n",
814 snprintf(info->dest, PATH_MAX, "%s%s/%s",
815 status->ls_targets[info->target_no], info->path,
818 rc1 = lr_rmfile(info);
819 lr_debug(DINFO, "remove: %s; rc1=%d, errno=%d\n",
820 info->dest, rc1, errno);
829 /* Replicate a rename/move operation. This operations are tracked by
830 two changelog records. */
831 int lr_move(struct lr_info *info, struct lr_info *ext)
837 int special_dest = 0;
839 rc_dest = lr_get_path(ext, ext->pfid + 3);
840 if (rc_dest < 0 && rc_dest != -ENOENT)
843 rc_src = lr_get_path(info, info->pfid + 3);
844 if (rc_src < 0 && rc_src != -ENOENT)
847 for (info->target_no = 0; info->target_no < status->ls_num_targets;
851 snprintf(info->dest, PATH_MAX, "%s%s",
852 status->ls_targets[info->target_no],
854 if (access(info->dest, F_OK) != 0) {
857 snprintf(info->dest, PATH_MAX, "%s%s/%s",
858 status->ls_targets[info->target_no],
859 ext->path, ext->name);
862 if (rc_dest == -ENOENT) {
863 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
864 status->ls_targets[info->target_no],
865 SPECIAL_DIR, info->tfid + 2);
870 snprintf(info->src, PATH_MAX, "%s%s/%s",
871 status->ls_targets[info->target_no],
872 info->path, info->name);
873 if (rc_src == -ENOENT || (access(info->src, F_OK) != 0 &&
875 snprintf(info->src, PATH_MAX, "%s/%s/%s",
876 status->ls_targets[info->target_no],
877 SPECIAL_DIR, info->tfid + 2);
882 if (strcmp(info->src, info->dest) != 0) {
883 rc1 = rename(info->src, info->dest);
889 lr_remove_pc(info->pfid, info->tfid);
891 lr_cascade_move(info->tfid + 2, info->dest, info);
894 lr_add_pc(ext->pfid, info->tfid, ext->name);
896 lr_debug(DINFO, "move: %s [to] %s rc1=%d, errno=%d\n",
897 info->src, info->dest, rc1, errno);
904 /* Replicate a hard link */
905 int lr_link(struct lr_info *info)
913 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
914 rc = stat(info->src, &st);
918 for (info->target_no = 0; info->target_no < status->ls_num_targets;
925 /* Search through the hardlinks to get the src and dest */
926 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
927 info->dest[0] == 0); i++) {
928 rc1 = lr_get_path_ln(info, info->tfid + 3, i);
932 lr_debug(DTRACE, "\tfid2path %s, %s, %d\n",
933 info->path, info->name, i);
935 len = strlen(info->path) - strlen(info->name);
936 if (len > 0 && strcmp(info->path + len,
938 snprintf(info->dest, PATH_MAX, "%s%s",
939 status->ls_targets[info->target_no],
941 else if (info->src[0] == 0)
942 snprintf(info->src, PATH_MAX, "%s%s",
943 status->ls_targets[info->target_no],
952 if (info->src[0] == 0 || info->dest[0] == 0)
953 /* Could not find the source or destination.
954 This can happen when some links don't exist
958 if (info->src[0] == 0)
959 snprintf(info->src, PATH_MAX, "%s/%s/%s",
960 status->ls_targets[info->target_no],
961 SPECIAL_DIR, info->tfid + 2);
962 else if (info->dest[0] == 0)
963 snprintf(info->dest, PATH_MAX, "%s/%s/%s",
964 status->ls_targets[info->target_no],
965 SPECIAL_DIR, info->tfid + 2);
967 rc1 = link(info->src, info->dest);
968 lr_debug(DINFO, "link: %s [to] %s; rc1=%d,errno=%d\n",
969 info->src, info->dest, rc1, errno);
977 /* Replicate file attributes */
978 int lr_setattr(struct lr_info *info)
983 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
985 rc = lr_get_path(info, info->tfid + 3);
987 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
992 for (info->target_no = 0; info->target_no < status->ls_num_targets;
995 snprintf(info->dest, PATH_MAX, "%s%s",
996 status->ls_targets[info->target_no], info->path);
997 lr_debug(DINFO, "setattr: %s %s %s", info->src, info->dest,
1000 rc1 = lr_sync_data(info);
1002 rc1 = lr_copy_attr(info->src, info->dest);
1009 /* Replicate xattrs */
1010 int lr_setxattr(struct lr_info *info)
1014 lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
1016 rc = lr_get_path(info, info->tfid + 3);
1018 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
1023 for (info->target_no = 0; info->target_no < status->ls_num_targets;
1024 info->target_no++) {
1026 snprintf(info->dest, PATH_MAX, "%s%s",
1027 status->ls_targets[info->target_no], info->path);
1028 lr_debug(DINFO, "setxattr: %s %s %s\n", info->src, info->dest,
1031 rc1 = lr_copy_xattr(info);
1039 /* Parse a line of changelog entry */
1040 int lr_parse_line(struct lr_info *info, FILE *fp)
1042 unsigned long long time;
1044 char typestr[TYPE_STR_LEN];
1045 char line[PATH_MAX];
1049 if (fgets(line, sizeof(line), fp) != NULL) {
1050 if (sscanf(line, "%llu %s %llu %x %s %s",
1051 &info->recno, typestr, &time,
1052 &flags, info->tfid, info->pfid) < 4) {
1053 fprintf(stderr, "error: unexpected changelog record "
1054 "format - %s\n", line);
1058 info->type = atoi(typestr);
1060 /* The filename could have spaces in it. scanf would
1061 have ignored it. Parse for the complete
1063 if (info->type != CL_SETATTR &&
1064 info->type != CL_XATTR &&
1065 info->type != CL_MARK) {
1066 for (i = 0, str = line; str != NULL && i <= 5;
1068 str = strchr(str, ' ');
1071 strncpy(info->name, str, PATH_MAX);
1072 str = strchr(info->name, '\n');
1076 fprintf(stderr, "error: unexpected changelog "
1077 "record format - %s\n", line);
1088 /* Initialize the replication parameters */
1089 int lr_init_status()
1091 size_t size = sizeof(struct lreplicate_status) + PATH_MAX;
1095 status = calloc(size, 1);
1098 status->ls_version = REPLICATE_STATUS_VER;
1099 status->ls_size = size;
1100 status->ls_last_recno = -1;
1104 /* Make a backup of the statuslog */
1105 void lr_backup_log()
1107 char backupfile[PATH_MAX];
1111 snprintf(backupfile, PATH_MAX, "%s.old", statuslog);
1112 (void) rename(statuslog, backupfile);
1118 /* Save replication parameters to a statuslog. */
1123 size_t write_size = status->ls_size;
1124 struct lr_parent_child_list *curr;
1127 if (statuslog == NULL)
1132 fd = open(statuslog, O_WRONLY | O_CREAT | O_SYNC);
1134 fprintf(stderr, "Error opening log file for writing (%s)\n",
1139 size = write(fd, status, write_size);
1140 if (size != write_size) {
1141 fprintf(stderr, "Error writing to log file (%s) %d\n",
1147 for (curr = parents; curr; curr = curr->pc_next) {
1148 size = write(fd, &curr->pc_log, sizeof(curr->pc_log));
1149 if (size != sizeof(curr->pc_log)) {
1150 fprintf(stderr, "Error writing to log file (%s) %d\n",
1160 /* Read statuslog and populate the replication parameters. Command
1161 * line parameters take precedence over parameters in the log file.*/
1164 struct lr_parent_child_list *tmp;
1165 struct lr_parent_child_log rec;
1166 struct lreplicate_status *s;
1169 size_t read_size = sizeof(struct lreplicate_status) + PATH_MAX;
1172 if (statuslog == NULL)
1175 s = calloc(1, read_size);
1177 GOTO(out, rc = -ENOMEM);
1179 fd = open(statuslog, O_RDONLY);
1181 GOTO(out, rc = -errno);
1182 size = read(fd, s, read_size);
1183 if (size != read_size)
1184 GOTO(out, rc = -EINVAL);
1185 if (read_size < s->ls_size) {
1186 read_size = s->ls_size;
1187 s = lr_grow_buf(s, read_size);
1189 GOTO(out, rc = -ENOMEM);
1190 if (lseek(fd, 0, SEEK_SET) == -1)
1191 GOTO(out, rc = -errno);
1192 size = read(fd, s, read_size);
1193 if (size != read_size)
1194 GOTO(out, rc = -EINVAL);
1197 while (read(fd, &rec, sizeof(rec)) != 0) {
1198 tmp = calloc(1, sizeof(*tmp));
1200 GOTO(out, rc = -ENOMEM);
1202 tmp->pc_next = parents;
1206 /* copy uninitialized fields to status */
1207 if (status->ls_num_targets == 0) {
1208 if (status->ls_size != s->ls_size) {
1209 status = lr_grow_buf(status, s->ls_size);
1211 GOTO(out, rc = -ENOMEM);
1212 status->ls_size = s->ls_size;
1214 status->ls_num_targets = s->ls_num_targets;
1215 memcpy(status->ls_targets, s->ls_targets,
1216 PATH_MAX * s->ls_num_targets);
1218 if (status->ls_last_recno == -1)
1219 status->ls_last_recno = s->ls_last_recno;
1221 if (status->ls_registration[0] == '\0')
1222 strncpy(status->ls_registration, s->ls_registration,
1225 if (status->ls_mdt_device[0] == '\0')
1226 strncpy(status->ls_mdt_device, s->ls_mdt_device,
1229 if (status->ls_source_fs[0] == '\0')
1230 strncpy(status->ls_source_fs, s->ls_source_fs,
1233 if (status->ls_source[0] == '\0')
1234 strncpy(status->ls_source, s->ls_source, PATH_MAX);
1244 /* Clear changelogs every CLEAR_INTERVAL records or at the end of
1246 int lr_clear_cl(struct lr_info *info, int force)
1248 char mdt_device[LR_NAME_MAXLEN + 1];
1252 if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
1253 if (info->type == CL_RENAME)
1254 rec = info->recno + 1;
1257 if (!noclear && !dryrun) {
1258 /* llapi_changelog_clear modifies the mdt
1259 * device name so make a copy of it until this
1262 strncpy(mdt_device, status->ls_mdt_device,
1264 rc = llapi_changelog_clear(mdt_device,
1265 status->ls_registration,
1268 printf("Changelog clear (%s, %s, %lld) "
1269 "returned %d\n", status->ls_mdt_device,
1270 status->ls_registration, rec, rc);
1272 if (!rc && !dryrun) {
1273 status->ls_last_recno = rec;
1282 /* Locate a usable version of rsync. At this point we'll use any
1284 int lr_locate_rsync()
1290 snprintf(rsync, PATH_MAX, "%s -p %s", TYPE, RSYNC);
1291 fp = popen(rsync, "r");
1295 if (fgets(rsync, PATH_MAX, fp) == NULL) {
1300 len = strlen(rsync);
1301 if (len > 0 && rsync[len - 1] == '\n')
1302 rsync[len - 1] = '\0';
1305 /* Determine the version of rsync */
1306 snprintf(rsync_ver, PATH_MAX, "%s --version", rsync);
1307 fp = popen(rsync_ver, "r");
1311 if (fgets(rsync_ver, PATH_MAX, fp) == NULL) {
1315 len = strlen(rsync_ver);
1316 if (len > 0 && rsync_ver[len - 1] == '\n')
1317 rsync_ver[len - 1] = '\0';
1324 /* Print the replication parameters */
1325 void lr_print_status(struct lr_info *info)
1332 printf("Lustre filesystem: %s\n", status->ls_source_fs);
1333 printf("MDT device: %s\n", status->ls_mdt_device);
1334 printf("Source: %s\n", status->ls_source);
1335 for (i = 0; i < status->ls_num_targets; i++)
1336 printf("Target: %s\n", status->ls_targets[i]);
1337 if (statuslog != NULL)
1338 printf("Statuslog: %s\n", statuslog);
1339 printf("Changelog registration: %s\n", status->ls_registration);
1340 printf("Starting changelog record: %lld\n", status->ls_last_recno);
1342 printf("Replicate xattrs: no\n");
1344 printf("Clear changelog after use: no\n");
1346 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
1349 /* Replicate filesystem operations from src_path to target_path */
1355 struct lr_info *info;
1356 struct lr_info *ext;
1364 info = calloc(1, sizeof(struct lr_info));
1368 rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
1370 fprintf(stderr, "Source path is not a valid Lustre client "
1374 if (status->ls_mdt_device[0] == '\0')
1375 snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s",
1376 status->ls_source_fs, DEFAULT_MDT);
1378 ext = calloc(1, sizeof(struct lr_info));
1381 memcpy(ext, info, sizeof(struct lr_info));
1383 for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) {
1384 snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i],
1386 rc = mkdir(info->dest, 0777);
1387 if (rc == -1 && errno != EEXIST) {
1388 fprintf(stderr, "Error writing to target path %s.\n",
1389 status->ls_targets[i]);
1392 rc = llistxattr(info->src, info->xlist, info->xsize);
1393 if (rc == -1 && errno == ENOTSUP) {
1394 fprintf(stderr, "xattrs not supported on %s\n",
1395 status->ls_targets[i]);
1399 if (xattr_not_supp == status->ls_num_targets)
1400 /* None of the targets support xattrs. */
1403 lr_print_status(info);
1405 /* Open changelogs for consumption*/
1406 startrec = status->ls_last_recno;
1407 fd = llapi_changelog_open(status->ls_source_fs, startrec);
1409 fprintf(stderr, "Error opening changelog file for fs %s.\n",
1410 status->ls_source_fs);
1413 if ((fp = fdopen(fd, "r")) == NULL) {
1414 fprintf(stderr, "Error: fdopen failed.");
1419 while (!quit && lr_parse_line(info, fp) == 0) {
1421 if (info->type == CL_RENAME)
1422 /* Rename operations have an additional changelog
1423 record of information. */
1424 lr_parse_line(ext, fp);
1429 switch(info->type) {
1434 rc = lr_create(info);
1438 rc = lr_remove(info);
1441 rc = lr_move(info, ext);
1448 rc = lr_setattr(info);
1451 rc = lr_setxattr(info);
1458 /* Nothing needs to be done for these entries */
1462 if (rc && rc != -ENOENT) {
1463 fprintf(stderr, "Replication of operation %d, "
1464 "index %lld failed: %d\n",
1465 info->type, info->recno, rc);
1468 lr_clear_cl(info, 0);
1470 bzero(info, sizeof(struct lr_info));
1471 bzero(ext, sizeof(struct lr_info));
1475 if (errors || verbose)
1476 printf("Errors: %d\n", errors);
1478 /* Clear changelog records used so far */
1479 lr_clear_cl(info, 1);
1482 printf("lreplicate took %ld seconds\n", time(NULL) - start);
1483 printf("Changelog records consumed: %lld\n", rec_count);
1493 termination_handler (int signum)
1495 /* Set a flag for the replicator to gracefully shutdown */
1497 printf("lreplicate halting.\n");
1500 int main(int argc, char *argv[])
1507 if ((rc = lr_init_status()) != 0)
1510 while ((c = getopt_long(argc, argv, "s:t:m:u:l:vx:zc:ry:n:d:",
1511 long_opts, NULL)) >= 0) {
1514 /* Assume absolute paths */
1515 strncpy(status->ls_source, optarg, PATH_MAX);
1518 status->ls_num_targets++;
1520 if (numtargets != status->ls_num_targets) {
1521 /* Targets were read from a log
1522 file. The ones specified on the
1523 command line take precedence. The
1524 ones from the log file will be
1526 status->ls_num_targets = numtargets;
1528 newsize = sizeof (struct lreplicate_status) +
1529 (status->ls_num_targets * PATH_MAX);
1530 if (status->ls_size != newsize) {
1531 status->ls_size = newsize;
1532 status = lr_grow_buf(status, newsize);
1536 strncpy(status->ls_targets[status->ls_num_targets - 1],
1541 strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
1544 strncpy(status->ls_registration, optarg,
1549 (void) lr_read_log();
1555 if (strcmp("no", optarg) == 0) {
1557 } else if (strcmp("yes", optarg) != 0) {
1558 printf("Invalid parameter %s. "
1559 "Specify --xattr=no or --xattr=yes\n",
1568 /* Undocumented option cl-clear */
1569 if (strcmp("no", optarg) == 0) {
1571 } else if (strcmp("yes", optarg) != 0) {
1572 printf("Invalid parameter %s. "
1573 "Specify --cl-clear=no "
1574 "or --cl-clear=yes\n",
1580 /* Undocumented option use-rsync */
1584 /* Undocumented option rsync-threshold */
1585 rsync_threshold = atol(optarg);
1588 /* Undocumented option start-recno */
1589 status->ls_last_recno = atol(optarg);
1592 /* Undocumented option debug */
1593 debug = atoi(optarg);
1594 if (debug < 0 || debug > 2)
1598 fprintf(stderr, "error: %s: option '%s' "
1599 "unrecognized.\n", argv[0], argv[optind - 1]);
1605 if (status->ls_last_recno == -1)
1606 status->ls_last_recno = 0;
1607 if (strnlen(status->ls_registration, LR_NAME_MAXLEN) == 0) {
1608 /* No registration ID was passed in. */
1609 printf("Please specify changelog consumer registration id.\n");
1613 if (strnlen(status->ls_source, PATH_MAX) == 0) {
1614 fprintf(stderr, "Please specify the source path.\n");
1618 if (strnlen(status->ls_targets[0], PATH_MAX) == 0) {
1619 fprintf(stderr, "Please specify the target path.\n");
1624 /* This plumbing is needed for some of the ioctls behind
1625 llapi calls to work. */
1626 if (obd_initialize(argc, argv) < 0) {
1627 fprintf(stderr, "obd_initialize failed.\n");
1631 rc = lr_locate_rsync();
1632 if (use_rsync && rc != 0) {
1633 fprintf(stderr, "Error: unable to locate %s.\n", RSYNC);
1637 signal(SIGINT, termination_handler);
1638 signal(SIGHUP, termination_handler);
1639 signal(SIGTERM, termination_handler);
1641 rc = lr_replicate();