Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / lreplicate.c
index 28b025e..c4ee8f8 100644 (file)
 #include <libcfs/libcfsutil.h>
 #include <lustre/liblustreapi.h>
 #include <lustre/lustre_idl.h>
-#include <lustre/lreplicate.h>
+#include "lreplicate.h"
 
 #define REPLICATE_STATUS_VER 1
 #define CLEAR_INTERVAL 100
@@ -188,6 +188,8 @@ int use_rsync;  /* Flag to turn on use of rsync to copy data */
 long long rsync_threshold = DEFAULT_RSYNC_THRESHOLD;
 int quit;       /* Flag to stop processing the changelog; set on the
                    receipt of a signal */
+int abort_on_err = 0;
+
 char rsync[PATH_MAX];
 char rsync_ver[PATH_MAX];
 struct lr_parent_child_list *parents;
@@ -199,16 +201,17 @@ struct option long_opts[] = {
         {"mdt",         required_argument, 0, 'm'},
         {"user",        required_argument, 0, 'u'},
         {"statuslog",   required_argument, 0, 'l'},
-        {"verbose",     no_argument, 0, 'v'},
+        {"verbose",     no_argument,       0, 'v'},
         {"xattr",       required_argument, 0, 'x'},
-        {"dry-run",     no_argument, 0, 'z'},
+        {"dry-run",     no_argument,       0, 'z'},
         /* Undocumented options follow */
         {"cl-clear",    required_argument, 0, 'c'},
-        {"use-rsync",   no_argument, 0, 'r'},
+        {"use-rsync",   no_argument,       0, 'r'},
         {"rsync-threshold", required_argument, 0, 'y'},
         {"start-recno", required_argument, 0, 'n'},
+        {"abort-on-err",no_argument,       0, 'a'},
         {"debug",       required_argument, 0, 'd'},
-        {0, 0, 0, 0}    
+        {0, 0, 0, 0}
 };
 
 /* Command line usage */
@@ -218,7 +221,12 @@ void lr_usage()
                 "-m <mdt> -r <user id> -l <status log>\n"
                 "lreplicate can also pick up parameters from a "
                 "status log created earlier.\n"
-                "\tlreplicate -l <log_file>\n");
+                "\tlreplicate -l <log_file>\n"
+                "options:\n"
+                "\t--xattr <yes|no> replicate EAs\n"
+                "\t--abort-on-err   abort at first err\n"
+                "\t--verbose\n"
+                "\t--dry-run        don't write anything\n");
 }
 
 /* Print debug information. This is controlled by the value of the
@@ -488,12 +496,11 @@ int lr_get_path_ln(struct lr_info *info, char *fidstr, int linkno)
         long long recno = -1;
         int rc;
 
-        rc = llapi_fid2path(status->ls_mdt_device, fidstr, info->path,
+        rc = llapi_fid2path(status->ls_source, fidstr, info->path,
                             PATH_MAX, &recno, &linkno);
         if (rc < 0 && rc != -ENOENT) {
                 fprintf(stderr, "fid2path error: (%s, %s) %d %s\n",
-                        status->ls_mdt_device, fidstr,
-                        -rc, strerror(errno = -rc));
+                        status->ls_source, fidstr, -rc, strerror(errno = -rc));
         }
 
         return rc;
@@ -510,8 +517,8 @@ int lr_get_path(struct lr_info *info, char *fidstr)
 void lr_get_FID_PATH(char *mntpt, char *fidstr, char *buf, int bufsize)
 {
         /* Open-by-FID path is <mntpt>/.lustre/fid/[SEQ:OID:VER] */
-        snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, mdd_dot_lustre_name,
-                 fidstr + 2);
+        snprintf(buf, bufsize, "%s/%s/fid/%s", mntpt, dot_lustre_name,
+                 fidstr);
         return;
 }
 
@@ -603,8 +610,8 @@ int lr_add_pc(const char *pfid, const char *tfid, const char *name)
         p = calloc(1, sizeof(*p));
         if (!p)
                 return -ENOMEM;
-        strcpy(p->pc_log.pcl_pfid, pfid + 2);
-        strcpy(p->pc_log.pcl_tfid, tfid + 2);
+        strcpy(p->pc_log.pcl_pfid, pfid);
+        strcpy(p->pc_log.pcl_tfid, tfid);
         strcpy(p->pc_log.pcl_name, name);
 
         p->pc_next = parents;
@@ -657,8 +664,8 @@ int lr_remove_pc(const char *pfid, const char *tfid)
         struct lr_parent_child_list *curr, *prev;
 
         for (prev = curr = parents; curr; prev = curr, curr = curr->pc_next) {
-                if (strcmp(curr->pc_log.pcl_pfid, pfid + 2) == 0 &&
-                    strcmp(curr->pc_log.pcl_tfid, tfid + 2) == 0) {
+                if (strcmp(curr->pc_log.pcl_pfid, pfid) == 0 &&
+                    strcmp(curr->pc_log.pcl_tfid, tfid) == 0) {
                         if (curr == parents)
                                 parents = curr->pc_next;
                         else
@@ -677,7 +684,7 @@ int lr_mk_special(struct lr_info *info)
 
         snprintf(info->dest, PATH_MAX, "%s/%s/%s",
                 status->ls_targets[info->target_no], SPECIAL_DIR,
-                info->tfid + 2);
+                info->tfid);
 
         rc = lr_mkfile(info);
         if (rc)
@@ -708,7 +715,7 @@ int lr_rm_special(struct lr_info *info)
 
         snprintf(info->dest, PATH_MAX, "%s/%s/%s",
                  status->ls_targets[info->target_no], SPECIAL_DIR,
-                 info->tfid + 2);
+                 info->tfid);
         rc = lr_rmfile(info);
 
         if (rc)
@@ -726,7 +733,7 @@ int lr_create(struct lr_info *info)
         int mkspecial = 0;
 
         /* Is target FID present on the source? */
-        rc = lr_get_path(info, info->tfid + 3);
+        rc = lr_get_path(info, info->tfid);
         if (rc == -ENOENT) {
                 /* Source file has disappeared. Not an error. */
                 lr_debug(DINFO, "create: tfid %s not found on"
@@ -738,7 +745,7 @@ int lr_create(struct lr_info *info)
         strcpy(info->savedpath, info->path);
 
         /* Is parent FID present on the source */
-        rc = lr_get_path(info, info->pfid + 3);
+        rc = lr_get_path(info, info->pfid);
         if (rc == -ENOENT) {
                 lr_debug(DINFO, "create: pfid %s not found on source-fs\n",
                          info->tfid);
@@ -801,7 +808,7 @@ int lr_remove(struct lr_info *info)
                 if (!rc1)
                         continue;
 
-                rc1 = lr_get_path(info, info->pfid + 3);
+                rc1 = lr_get_path(info, info->pfid);
                 if (rc1 == -ENOENT) {
                         lr_debug(DINFO, "remove: pfid %s not found\n",
                                  info->pfid);
@@ -836,11 +843,11 @@ int lr_move(struct lr_info *info, struct lr_info *ext)
         int special_src = 0;
         int special_dest = 0;
 
-        rc_dest = lr_get_path(ext, ext->pfid + 3);
+        rc_dest = lr_get_path(ext, ext->pfid);
         if (rc_dest < 0 && rc_dest != -ENOENT)
                 return rc_dest;
 
-        rc_src = lr_get_path(info, info->pfid + 3);
+        rc_src = lr_get_path(info, info->pfid);
         if (rc_src < 0 && rc_src != -ENOENT)
                 return rc_src;
 
@@ -862,7 +869,7 @@ int lr_move(struct lr_info *info, struct lr_info *ext)
                 if (rc_dest == -ENOENT) {
                         snprintf(info->dest, PATH_MAX, "%s/%s/%s",
                                 status->ls_targets[info->target_no],
-                                SPECIAL_DIR, info->tfid + 2);
+                                SPECIAL_DIR, info->tfid);
                         special_dest = 1;
                 }
 
@@ -874,7 +881,7 @@ int lr_move(struct lr_info *info, struct lr_info *ext)
                                           errno == ENOENT)) {
                         snprintf(info->src, PATH_MAX, "%s/%s/%s",
                                 status->ls_targets[info->target_no],
-                                SPECIAL_DIR, info->tfid + 2);
+                                SPECIAL_DIR, info->tfid);
                         special_src = 1;
                 }
 
@@ -888,7 +895,7 @@ int lr_move(struct lr_info *info, struct lr_info *ext)
                 if (special_src) {
                         lr_remove_pc(info->pfid, info->tfid);
                         if (!special_dest)
-                                lr_cascade_move(info->tfid + 2, info->dest, info);
+                                lr_cascade_move(info->tfid, info->dest, info);
                 }
                 if (special_dest)
                         lr_add_pc(ext->pfid, info->tfid, ext->name);
@@ -925,12 +932,11 @@ int lr_link(struct lr_info *info)
                 /* Search through the hardlinks to get the src and dest */
                 for (i = 0; i < st.st_nlink && (info->src[0] == 0 ||
                                                 info->dest[0] == 0); i++) {
-                        rc1 = lr_get_path_ln(info, info->tfid + 3, i);
+                        rc1 = lr_get_path_ln(info, info->tfid, i);
+                        lr_debug(rc1 ? 0:DTRACE, "\tfid2path %s, %s, %d rc=%d\n",
+                                 info->path, info->name, i, rc1);
                         if (rc1)
                                 break;
-                        else
-                                lr_debug(DTRACE, "\tfid2path %s, %s, %d\n",
-                                         info->path, info->name, i);
 
                         len = strlen(info->path) - strlen(info->name);
                         if (len > 0 && strcmp(info->path + len,
@@ -958,15 +964,15 @@ int lr_link(struct lr_info *info)
                 if (info->src[0] == 0)
                         snprintf(info->src, PATH_MAX, "%s/%s/%s",
                                 status->ls_targets[info->target_no],
-                                SPECIAL_DIR, info->tfid + 2);
+                                SPECIAL_DIR, info->tfid);
                 else if (info->dest[0] == 0)
                         snprintf(info->dest, PATH_MAX, "%s/%s/%s",
                                 status->ls_targets[info->target_no],
-                                SPECIAL_DIR, info->tfid + 2);
+                                SPECIAL_DIR, info->tfid);
 
                 rc1 = link(info->src, info->dest);
-                lr_debug(DINFO, "link: %s [to] %s; rc1=%d,errno=%d\n",
-                         info->src, info->dest, rc1, errno);
+                lr_debug(rc1?0:DINFO, "link: %s [to] %s; rc1=%d %s\n",
+                         info->src, info->dest, rc1, strerror(errno));
 
                 if (rc1)
                         rc = rc1;
@@ -982,7 +988,7 @@ int lr_setattr(struct lr_info *info)
 
         lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
 
-        rc = lr_get_path(info, info->tfid + 3);
+        rc = lr_get_path(info, info->tfid);
         if (rc == -ENOENT)
                 lr_debug(DINFO, "setattr: %s not present on source-fs\n",
                          info->src);
@@ -1013,7 +1019,7 @@ int lr_setxattr(struct lr_info *info)
 
         lr_get_FID_PATH(status->ls_source, info->tfid, info->src, PATH_MAX);
 
-        rc = lr_get_path(info, info->tfid + 3);
+        rc = lr_get_path(info, info->tfid);
         if (rc == -ENOENT)
                 lr_debug(DINFO, "setxattr: %s not present on source-fs\n",
                          info->src);
@@ -1037,51 +1043,23 @@ int lr_setxattr(struct lr_info *info)
 }
 
 /* Parse a line of changelog entry */
-int lr_parse_line(struct lr_info *info, FILE *fp)
+int lr_parse_line(void *priv, struct lr_info *info)
 {
-        unsigned long long time;
-        unsigned int flags;
-        char typestr[TYPE_STR_LEN];
-        char line[PATH_MAX];
-        char *str;
-        int i;
+        struct changelog_rec *rec;
 
-        if (fgets(line, sizeof(line), fp) != NULL) {
-                if (sscanf(line, "%llu %s %llu %x %s %s",
-                           &info->recno, typestr, &time,
-                           &flags, info->tfid, info->pfid) < 4) {
-                        fprintf(stderr, "error: unexpected changelog record "
-                                "format - %s\n", line);
-                        return -1;
-                }
-                typestr[2] = '\0';
-                info->type = atoi(typestr);
-
-                /* The filename could have spaces in it. scanf would
-                   have ignored it. Parse for the complete
-                   filename. */
-                if (info->type != CL_SETATTR &&
-                    info->type != CL_XATTR &&
-                    info->type != CL_MARK) {
-                        for (i = 0, str = line; str != NULL && i <= 5;
-                             i++, str++){
-                                str = strchr(str, ' ');
-                        }
-                        if (str) {
-                                strncpy(info->name, str, PATH_MAX);
-                                str = strchr(info->name, '\n');
-                                if (str)
-                                        str[0] = '\0';
-                        } else {
-                                fprintf(stderr, "error: unexpected changelog "
-                                        "record format - %s\n", line);
-                                return -1;
-                        }
-                }
-                rec_count++;
-        } else {
+        if (llapi_changelog_recv(priv, &rec) != 0)
                 return -1;
-        }
+
+        info->recno = rec->cr_index;
+        info->type = rec->cr_type;
+        sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
+        sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
+        strncpy(info->name, rec->cr_name, rec->cr_namelen);
+        info->name[rec->cr_namelen] = '\0';
+
+        llapi_changelog_free(&rec);
+
+        rec_count++;
         return 0;
 }
 
@@ -1346,12 +1324,18 @@ void lr_print_status(struct lr_info *info)
                 printf("Using rsync: %s (%s)\n", rsync, rsync_ver);
 }
 
+void lr_print_failure(struct lr_info *info, int rc)
+{
+        fprintf(stderr, "Replication of operation failed(%d):"
+                " %lld %s (%d) %s %s %s\n", rc, info->recno,
+                changelog_type2str(info->type), info->type, info->tfid,
+                info->pfid, info->name);
+}
+
 /* Replicate filesystem operations from src_path to target_path */
 int lr_replicate()
 {
-        int fd;
-        FILE *fp;
-        long long startrec;
+        void *changelog_priv;
         struct lr_info *info;
         struct lr_info *ext;
         time_t start;
@@ -1364,7 +1348,7 @@ int lr_replicate()
         info = calloc(1, sizeof(struct lr_info));
         if (info == NULL)
                 return -ENOMEM;
-        
+
         rc = llapi_search_fsname(status->ls_source, status->ls_source_fs);
         if (rc) {
                 fprintf(stderr, "Source path is not a valid Lustre client "
@@ -1403,25 +1387,20 @@ int lr_replicate()
         lr_print_status(info);
 
         /* Open changelogs for consumption*/
-        startrec = status->ls_last_recno;
-        fd = llapi_changelog_open(status->ls_source_fs, startrec);
-        if (fd < 0) {
+        rc = llapi_changelog_start(&changelog_priv, 0, status->ls_source_fs,
+                                   status->ls_last_recno);
+        if (rc < 0) {
                 fprintf(stderr, "Error opening changelog file for fs %s.\n",
                         status->ls_source_fs);
-                return fd;
-        }
-        if ((fp = fdopen(fd, "r")) == NULL) {
-                fprintf(stderr, "Error: fdopen failed.");
-                close(fd);
-                return -errno;
+                return rc;
         }
 
-        while (!quit && lr_parse_line(info, fp) == 0) {
+        while (!quit && lr_parse_line(changelog_priv, info) == 0) {
                 rc = 0;
                 if (info->type == CL_RENAME)
                         /* Rename operations have an additional changelog
                            record of information. */
-                        lr_parse_line(ext, fp);
+                        lr_parse_line(changelog_priv, ext);
 
                 if (dryrun)
                         continue;
@@ -1460,10 +1439,10 @@ int lr_replicate()
                         break;
                 }
                 if (rc && rc != -ENOENT) {
-                        fprintf(stderr, "Replication of operation %d, "
-                                "index %lld failed: %d\n",
-                                info->type, info->recno, rc);
+                        lr_print_failure(info, rc);
                         errors++;
+                        if (abort_on_err)
+                                break;
                 }
                 lr_clear_cl(info, 0);
                 if (debug) {
@@ -1472,6 +1451,8 @@ int lr_replicate()
                 }
         }
 
+        llapi_changelog_fini(&changelog_priv);
+
         if (errors || verbose)
                 printf("Errors: %d\n", errors);
 
@@ -1483,9 +1464,6 @@ int lr_replicate()
                 printf("Changelog records consumed: %lld\n", rec_count);
         }
 
-        close(fd);
-        fclose(fp);
-
         return 0;
 }
 
@@ -1507,9 +1485,13 @@ int main(int argc, char *argv[])
         if ((rc = lr_init_status()) != 0)
                 return rc;
 
-        while ((c = getopt_long(argc, argv, "s:t:m:u:l:vx:zc:ry:n:d:",
+        while ((c = getopt_long(argc, argv, "as:t:m:u:l:vx:zc:ry:n:d:",
                                 long_opts, NULL)) >= 0) {
                 switch (c) {
+                case 'a':
+                        /* Assume absolute paths */
+                        abort_on_err++;
+                        break;
                 case 's':
                         /* Assume absolute paths */
                         strncpy(status->ls_source, optarg, PATH_MAX);
@@ -1549,7 +1531,7 @@ int main(int argc, char *argv[])
                         (void) lr_read_log();
                         break;
                 case 'v':
-                        verbose = 1;
+                        verbose++;
                         break;
                 case 'x':
                         if (strcmp("no", optarg) == 0) {