Whamcloud - gitweb
LU-14506 hsm: correct default stripe offset in import
[fs/lustre-release.git] / lustre / utils / lhsmtool_posix.c
index 14aa197..707cbab 100644 (file)
@@ -59,7 +59,7 @@
 #include <libcfs/util/string.h>
 #include <linux/lustre/lustre_fid.h>
 #include <lustre/lustreapi.h>
-#include "lstddef.h"
+#include "pid_file.h"
 
 /* Progress reporting period */
 #define REPORT_INTERVAL_DEFAULT 30
@@ -101,6 +101,7 @@ struct options {
        char                    *o_hsm_root;
        char                    *o_src; /* for import, or rebind */
        char                    *o_dst; /* for import, or rebind */
+       char                    *o_pid_file;
 };
 
 /* everything else is zeroed */
@@ -125,6 +126,7 @@ static int err_minor;
 
 static char cmd_name[PATH_MAX];
 static char fs_name[MAX_OBD_NAME + 1];
+static int pid_file_fd = -1;
 
 static struct hsm_copytool_private *ctdata;
 
@@ -199,6 +201,7 @@ static void usage(const char *name, int rc)
        "   -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, --pid-file=PATH       Lock and write PID to PATH\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"
@@ -242,6 +245,7 @@ static int ct_parseopts(int argc, char * const *argv)
          .flag = &opt.o_copy_xattrs },
        { .val = 0,     .name = "no_xattr",     .has_arg = no_argument,
          .flag = &opt.o_copy_xattrs },
+       { .val = 'P',   .name = "pid-file",     .has_arg = required_argument },
        { .val = 'p',   .name = "hsm-root",     .has_arg = required_argument },
        { .val = 'p',   .name = "hsm_root",     .has_arg = required_argument },
        { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
@@ -252,6 +256,7 @@ static int ct_parseopts(int argc, char * const *argv)
                                                .has_arg = required_argument },
        { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
        { .name = NULL } };
+
        unsigned long long value;
        unsigned long long unit;
        bool all_id = false;
@@ -266,7 +271,7 @@ static int ct_parseopts(int argc, char * const *argv)
        if (opt.o_archive_id == NULL)
                return -ENOMEM;
 repeat:
-       while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
+       while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:P:qru:v",
                                long_opts, NULL)) != -1) {
                switch (c) {
                case 'A': {
@@ -340,6 +345,9 @@ repeat:
                case 'M':
                        opt.o_action = CA_MAXSEQ;
                        break;
+               case 'P':
+                       opt.o_pid_file = optarg;
+                       break;
                case 'p':
                        opt.o_hsm_root = optarg;
                        break;
@@ -657,27 +665,6 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                int     chunk = (length - write_total > opt.o_chunk_size) ?
                                 opt.o_chunk_size : length - write_total;
 
-               /* Try the accelerated copy path first. Once LU-10180 lands
-                * we should deal with holes.
-                */
-               wsize = copy_file_range(src_fd, NULL, dst_fd, NULL,
-                                       chunk, 0);
-               if (wsize != -1)
-                       goto fini_fastcopy;
-               rc = -errno;
-               /* Before Linux kernel 5.3 copy_file_range only supported
-                * file copies between filesystems of the same type. In
-                * that case copy_file_range() will return -EXDEV.
-                */
-               if (rc != -EXDEV && rc != -ENOSYS) {
-                       CT_ERROR(rc, "copy_file_range failed for '%s' to '%s'",
-                                src, dst);
-                       break;
-               }
-
-               /* copy_file_range is not avalible or failed
-                * so use what works.
-                */
                rsize = pread(src_fd, buf, chunk, offset);
                if (rsize == 0)
                        /* EOF */
@@ -695,7 +682,7 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                        CT_ERROR(rc, "cannot write to '%s'", dst);
                        break;
                }
-fini_fastcopy:
+
                write_total += wsize;
                offset += wsize;
 
@@ -864,22 +851,22 @@ static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
 static int ct_path_lustre(char *buf, int sz, const char *mnt,
                          const struct lu_fid *fid)
 {
-       return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
-                       dot_lustre_name, PFID(fid));
+       return scnprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
+                        dot_lustre_name, PFID(fid));
 }
 
 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
                           const struct lu_fid *fid)
 {
-       return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
-                       DFID_NOBRACE, archive_dir,
-                       (fid)->f_oid       & 0xFFFF,
-                       (fid)->f_oid >> 16 & 0xFFFF,
-                       (unsigned int)((fid)->f_seq       & 0xFFFF),
-                       (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
-                       (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
-                       (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
-                       PFID(fid));
+       return scnprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
+                        DFID_NOBRACE, archive_dir,
+                        (fid)->f_oid       & 0xFFFF,
+                        (fid)->f_oid >> 16 & 0xFFFF,
+                        (unsigned int)((fid)->f_seq       & 0xFFFF),
+                        (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
+                        (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
+                        (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
+                        PFID(fid));
 }
 
 static bool ct_is_retryable(int err)
@@ -1099,17 +1086,19 @@ static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
                int              depth = 0;
                ssize_t          sz;
 
-               sprintf(buf, DFID, PFID(&hai->hai_fid));
                sprintf(src, "%s/shadow/", opt.o_hsm_root);
 
                ptr = opt.o_hsm_root;
                while (*ptr)
                        (*ptr++ == '/') ? depth-- : 0;
 
-               rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
-                                   sizeof(src) - strlen(src), &recno, &linkno);
+               rc = llapi_fid2path_at(opt.o_mnt_fd, &hai->hai_fid,
+                                      src + strlen(src),
+                                      sizeof(src) - strlen(src),
+                                      &recno, &linkno);
                if (rc < 0) {
-                       CT_ERROR(rc, "cannot get FID of '%s'", buf);
+                       CT_ERROR(rc, "cannot get FID of "DFID,
+                                PFID(&hai->hai_fid));
                        rcf = rcf ? rcf : rc;
                        goto fini_minor;
                }
@@ -1363,19 +1352,19 @@ static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
 
        if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
                /* Print the original path */
-               char            fid[128];
                char            path[PATH_MAX];
                long long       recno = -1;
                int             linkno = 0;
 
-               sprintf(fid, DFID, PFID(&hai->hai_fid));
-               CT_TRACE("'%s' action %s reclen %d, cookie=%#jx",
-                        fid, hsm_copytool_action2name(hai->hai_action),
+               CT_TRACE(DFID" action %s reclen %d, cookie=%#jx",
+                        PFID(&hai->hai_fid),
+                        hsm_copytool_action2name(hai->hai_action),
                         hai->hai_len, (uintmax_t)hai->hai_cookie);
-               rc = llapi_fid2path(opt.o_mnt, fid, path,
-                                   sizeof(path), &recno, &linkno);
+               rc = llapi_fid2path_at(opt.o_mnt_fd, &hai->hai_fid, path,
+                                      sizeof(path), &recno, &linkno);
                if (rc < 0)
-                       CT_ERROR(rc, "cannot get path of FID %s", fid);
+                       CT_ERROR(rc, "cannot get path of "DFID,
+                                PFID(&hai->hai_fid));
                else
                        CT_TRACE("processing file '%s'", path);
        }
@@ -1488,9 +1477,14 @@ static int ct_import_one(const char *src, const char *dst)
                return 0;
 
        rc = llapi_hsm_import(dst,
-                             opt.o_archive_id_used ?
-                             opt.o_archive_id[0] : 0,
-                             &st, 0, 0, 0, 0, NULL, &fid);
+                             opt.o_archive_id_used ? opt.o_archive_id[0] : 0,
+                             &st,
+                             0 /* default stripe_size */,
+                             -1 /* default stripe offset */,
+                             0 /* default stripe count */,
+                             0 /* stripe pattern (will be RAID0+RELEASED) */,
+                             NULL /* pool_name */,
+                             &fid);
        if (rc < 0) {
                CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
                return rc;
@@ -1894,6 +1888,15 @@ static int ct_run(void)
                }
        }
 
+       if (opt.o_pid_file != NULL) {
+               pid_file_fd = create_pid_file(opt.o_pid_file);
+               if (pid_file_fd < 0) {
+                       rc = -errno;
+                       CT_ERROR(rc, "cannot create PID file");
+                       return rc;
+               }
+       }
+
        setbuf(stdout, NULL);
 
        if (opt.o_event_fifo != NULL) {