Whamcloud - gitweb
LU-4020 hsm: allow copytool event monitoring with JSON
[fs/lustre-release.git] / lustre / utils / lhsmtool_posix.c
index 0709264..c057e68 100644 (file)
@@ -78,6 +78,7 @@ struct options {
        unsigned long long       o_bandwidth;
        size_t                   o_chunk_size;
        enum ct_action           o_action;
+       char                    *o_event_fifo;
        char                    *o_mnt;
        char                    *o_hsm_root;
        char                    *o_src; /* for import, or rebind */
@@ -164,16 +165,19 @@ static void usage(const char *name, int rc)
        "       each line of <list_file> consists of <old_FID> <new_FID>\n"
        "   %s [options] --max-sequence <fsname>\n"
        "       return the max fid sequence of archived files\n"
-       "   -A, --archive <#>        Archive number (repeatable)\n"
-       "   -p, --hsm-root <path>    Target HSM mount point\n"
-       "   -q, --quiet              Produce less verbose output\n"
-       "   -v, --verbose            Produce more verbose output\n"
-       "   -c, --chunk-size <sz>    I/O size used during data copy\n"
-       "                            (unit can be used, default is MB)\n"
-       "   --abort-on-error         Abort operation on major error\n"
-       "   --dry-run                Don't run, just show what would be done\n"
-       "   --bandwidth <bw>         Limit I/O bandwidth (unit can be used\n,"
-       "                            default is MB)\n",
+       "   --abort-on-error          Abort operation on major error\n"
+       "   -A, --archive <#>         Archive number (repeatable)\n"
+       "   -b, --bandwidth <bw>      Limit I/O bandwidth (unit can be used\n,"
+       "                             default is MB)\n"
+       "   --dry-run                 Don't run, just show what would be done\n"
+       "   -c, --chunk-size <sz>     I/O size used during data copy\n"
+       "                             (unit can be used, default is MB)\n"
+       "   -f, --event-fifo <path>   Write events stream to fifo\n"
+       "   -p, --hsm-root <path>     Target HSM mount point\n"
+       "   -q, --quiet               Produce less verbose output\n"
+       "   -u, --update-interval <s> Interval between progress reports sent\n"
+       "                             to Coordinator\n"
+       "   -v, --verbose             Produce more verbose output\n",
        cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
 
        exit(rc);
@@ -189,6 +193,8 @@ static int ct_parseopts(int argc, char * const *argv)
                {"chunk-size",     required_argument, NULL,                'c'},
                {"chunk_size",     required_argument, NULL,                'c'},
                {"daemon",         no_argument,       &opt.o_daemonize,     1},
+               {"event-fifo",     required_argument, NULL,                'f'},
+               {"event_fifo",     required_argument, NULL,                'f'},
                {"dry-run",        no_argument,       &opt.o_dry_run,       1},
                {"help",           no_argument,       NULL,                'h'},
                {"hsm-root",       required_argument, NULL,                'p'},
@@ -204,7 +210,8 @@ static int ct_parseopts(int argc, char * const *argv)
                {"no_xattr",       no_argument,       &opt.o_copy_xattrs,   0},
                {"quiet",          no_argument,       NULL,                'q'},
                {"rebind",         no_argument,       NULL,                'r'},
-               {"report",         required_argument, &opt.o_report_int,    0},
+               {"update-interval", required_argument,  NULL,              'u'},
+               {"update_interval", required_argument,  NULL,              'u'},
                {"verbose",        no_argument,       NULL,                'v'},
                {0, 0, 0, 0}
        };
@@ -213,7 +220,7 @@ static int ct_parseopts(int argc, char * const *argv)
        unsigned long long       unit;
 
        optind = 0;
-       while ((c = getopt_long(argc, argv, "A:b:c:hiMp:qrv",
+       while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
                                long_opts, NULL)) != -1) {
                switch (c) {
                case 'A':
@@ -241,6 +248,9 @@ static int ct_parseopts(int argc, char * const *argv)
                        else
                                opt.o_bandwidth = value;
                        break;
+               case 'f':
+                       opt.o_event_fifo = optarg;
+                       break;
                case 'h':
                        usage(argv[0], 0);
                case 'i':
@@ -258,6 +268,15 @@ static int ct_parseopts(int argc, char * const *argv)
                case 'r':
                        opt.o_action = CA_REBIND;
                        break;
+               case 'u':
+                       opt.o_report_int = atoi(optarg);
+                       if (opt.o_report_int < 0) {
+                               rc = -EINVAL;
+                               CT_ERROR(rc, "bad value for -%c '%s'", c,
+                                        optarg);
+                               return rc;
+                       }
+                       break;
                case 'v':
                        opt.o_verbose++;
                        break;
@@ -507,7 +526,7 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        __u64                    offset = hai->hai_extent.offset;
        struct stat              src_st;
        struct stat              dst_st;
-       char                    *buf;
+       char                    *buf = NULL;
        __u64                    write_total = 0;
        __u64                    length;
        time_t                   last_print_time = time(NULL);
@@ -515,10 +534,6 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        CT_TRACE("going to copy data from '%s' to '%s'", src, dst);
 
-       buf = malloc(opt.o_chunk_size);
-       if (buf == NULL)
-               return -ENOMEM;
-
        if (fstat(src_fd, &src_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", src);
@@ -545,16 +560,19 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
        if (rc < 0) {
-               CT_ERROR(errno,
+               rc = -errno;
+               CT_ERROR(rc,
                         "cannot seek for read to "LPU64" (len %jd) in '%s'",
                         hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
-               rc = -errno;
-               goto out;
+               return rc;
        }
 
+       /* Don't read beyond a given extent */
+       length = min(hai->hai_extent.length, src_st.st_size);
+
        he.offset = offset;
        he.length = 0;
-       rc = llapi_hsm_action_progress(hcp, &he, 0);
+       rc = llapi_hsm_action_progress(hcp, &he, length, 0);
        if (rc < 0) {
                /* Action has been canceled or something wrong
                 * is happening. Stop copying data. */
@@ -564,8 +582,12 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        }
 
        errno = 0;
-       /* Don't read beyond a given extent */
-       length = min(hai->hai_extent.length, src_st.st_size);
+
+       buf = malloc(opt.o_chunk_size);
+       if (buf == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
 
        CT_DEBUG("Going to copy "LPU64" bytes %s -> %s\n", length, src, dst);
 
@@ -604,7 +626,7 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                        last_print_time = time(0);
                        CT_TRACE("%%"LPU64" ", 100 * write_total / length);
                        he.length = write_total;
-                       rc = llapi_hsm_action_progress(hcp, &he, 0);
+                       rc = llapi_hsm_action_progress(hcp, &he, length, 0);
                        if (rc < 0) {
                                /* Action has been canceled or something wrong
                                 * is happening. Stop copying data. */
@@ -631,14 +653,14 @@ out:
                rc = ftruncate(dst_fd, src_st.st_size);
                if (rc < 0) {
                        rc = -errno;
-                       CT_ERROR(rc,
-                                "cannot truncate '%s' to size %jd",
+                       CT_ERROR(rc, "cannot truncate '%s' to size %jd",
                                 dst, (intmax_t)src_st.st_size);
                        err_major++;
                }
        }
 
-       free(buf);
+       if (buf != NULL)
+               free(buf);
 
        return rc;
 }
@@ -1693,6 +1715,15 @@ static int ct_run(void)
 
        setbuf(stdout, NULL);
 
+       if (opt.o_event_fifo != NULL) {
+               rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
+               if (rc < 0) {
+                       CT_ERROR(rc, "failed to register event fifo");
+                       return rc;
+               }
+               llapi_error_callback_set(llapi_hsm_log_error);
+       }
+
        rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt, 0,
                                         opt.o_archive_cnt, opt.o_archive_id);
        if (rc < 0) {
@@ -1766,6 +1797,8 @@ static int ct_run(void)
        }
 
        llapi_hsm_copytool_unregister(&ctdata);
+       if (opt.o_event_fifo != NULL)
+               llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
 
        return rc;
 }