Whamcloud - gitweb
EX-5102 utils: Add --pin option for 'lfs pcc attach' command
authorLei Feng <flei@whamcloud.com>
Thu, 14 Apr 2022 05:07:07 +0000 (01:07 -0400)
committerAndreas Dilger <adilger@whamcloud.com>
Mon, 25 Apr 2022 23:42:11 +0000 (23:42 +0000)
If --pin option is provided, 'lfs pcc attach' command will
first pin then attach a file. So that user does not need
to run them separately.

Change-Id: I8e06af09abe7922399fd5e8e60132ff24ca31d1a
Signed-off-by: Lei Feng <flei@whamcloud.com>
Test-Parameters: trivial testlist=sanity-pcc
Reviewed-on: https://review.whamcloud.com/47067
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Yingjin Qian <qian@ddn.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
lustre/doc/lfs-pcc-attach.1
lustre/include/lustre/lustreapi.h
lustre/tests/sanity-pcc.sh
lustre/utils/lfs.c
lustre/utils/liblustreapi_pcc.c

index 73eb7f3..c4c0c63 100644 (file)
@@ -32,6 +32,9 @@ Attach the file in read-only mode.  This is the default.
 .TP
 .BR --write | -w
 Attach the file in read-write mode.
+.TP
+.BR --pin | -p
+Pin and attach the file so that it will not be detached automatically.
 .SH EXAMPLES
 .TP
 .B $ lfs pcc attach -i 1 /mnt/lustre/file
index d476490..13a4b6d 100644 (file)
@@ -660,9 +660,11 @@ int llapi_pccdev_get(const char *mntpath);
 int llapi_pcc_del(const char *mntpath, const char *pccpath, __u32 flags);
 int llapi_pcc_clear(const char *mntpath, __u32 flags);
 int llapi_pcc_pin_file(const char *path, __u32 id);
+int llapi_pcc_pin_fid(const char *lustre_dir, const struct lu_fid *fid, __u32 id);
 int llapi_pcc_unpin_file(const char *path, __u32 id);
 int llapi_pcc_backend_id_get(const char *path, enum lu_pcc_type type,
                             __u32 *id);
+
 /** @} llapi */
 
 /* llapi_layout user interface */
index 1d16c74..0e81ca7 100644 (file)
@@ -4519,8 +4519,7 @@ test_204a() {
        do_facet $SINGLEAGT $LFS pcc pin -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to pcc pin $file"
        do_facet $SINGLEAGT getfattr $file -d -m $xattrname
-       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname \
-                    --only-values)
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname --only-values)
        [[ "$xattrvalue" =~ "hsm: $HSM_ARCHIVE_NUMBER" ]] ||
                error "incorrect xattr $xattrname=$xattrvalue"
 
@@ -4528,8 +4527,7 @@ test_204a() {
        do_facet $SINGLEAGT $LFS pcc pin -i 100 $file ||
                error "failed to pcc pin $file"
        do_facet $SINGLEAGT getfattr $file -d -m $xattrname
-       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname \
-                    --only-values)
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname --only-values)
        [[ "$xattrvalue" =~ "hsm: $HSM_ARCHIVE_NUMBER" &&
           "$xattrvalue" =~ "hsm: 100" ]] ||
                error "incorrect xattr $xattrname=$xattrvalue"
@@ -4543,8 +4541,7 @@ test_204a() {
        do_facet $SINGLEAGT $LFS pcc unpin -i 100 $file ||
                error "failed to pcc unpin $file"
        do_facet $SINGLEAGT getfattr $file -d -m $xattrname
-       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname \
-                    --only-values)
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname --only-values)
        [[ "$xattrvalue" =~ "hsm: $HSM_ARCHIVE_NUMBER" ]] ||
                error "incorrect xattr $xattrname=$xattrvalue"
 
@@ -4552,8 +4549,7 @@ test_204a() {
        do_facet $SINGLEAGT $LFS pcc unpin -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to pcc unpin $file"
        do_facet $SINGLEAGT getfattr $file -d -m $xattrname
-       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname \
-                    --only-values)
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -d -m $xattrname --only-values)
        [[ -z "$xattrvalue" ]] ||
                error "incorrect xattr $xattrname=$xattrvalue"
 
@@ -4618,7 +4614,6 @@ test_204b() {
        local file=$DIR/$tfile
        local xattrname="trusted.pin"
        local xattrvalue
-       local fid
 
        (( $MDS1_VERSION >= $(version_code 2.14.0.38) )) ||
                skip "Need server version at least 2.14.0.38"
@@ -4646,6 +4641,50 @@ test_204b() {
 }
 run_test 204b "PCC pin/unpin without attach ID specified"
 
+test_204c() {
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file
+       local fid
+       local xattrname="trusted.pin"
+
+       (( $MDS1_VERSION >= $(version_code 2.14.0.38) )) ||
+               skip "Need server version at least 2.14.0.38"
+
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 14
+       do_facet $SINGLEAGT mkdir $hsm_root || error "mkdir $hsm_root failed"
+       setup_pcc_mapping $SINGLEAGT \
+               "fname={*.dat}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
+
+       # attach --pin with archive id
+       file=$DIR/$tfile.1
+       do_facet $SINGLEAGT touch $file || error "failed to create $file"
+
+       do_facet $SINGLEAGT \
+               $LFS pcc attach -i $HSM_ARCHIVE_NUMBER --pin $file ||
+               error "failed to attach $file with --pin option"
+       check_lpcc_state $file "readonly"
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -n $xattrname --only-values)
+       [[ "$xattrvalue" =~ "hsm: $HSM_ARCHIVE_NUMBER" ]] ||
+               error "incorrect xattr $xattrname=$xattrvalue"
+
+       # attach_fid --pin without archive id
+       file=$DIR/$tfile.2
+       do_facet $SINGLEAGT touch $file || error "failed to create $file"
+       # remove [] for it causes chaos later
+       fid=$(path2fid $file)
+
+       do_facet $SINGLEAGT \
+               $LFS pcc attach_fid -m $MOUNT --pin $fid ||
+               error "failed to attach fid $fid with --pin option"
+       check_lpcc_state $file "readonly"
+       xattrvalue=$(do_facet $SINGLEAGT getfattr $file -n $xattrname --only-values)
+       [[ "$xattrvalue" =~ "hsm: $HSM_ARCHIVE_NUMBER" ]] ||
+               error "incorrect xattr $xattrname=$xattrvalue"
+}
+run_test 204c "attach/attach_fid with --pin option"
+
 complete $SECONDS
 check_and_cleanup_lustre
 exit_status
index 69b8ba1..1cf5a7d 100644 (file)
@@ -374,18 +374,20 @@ command_t mirror_cmdlist[] = {
  */
 command_t pcc_cmdlist[] = {
        { .pc_name = "attach", .pc_func = lfs_pcc_attach,
-         .pc_help = "Attach given files to the Persistent Client Cache.\n"
-               "usage: lfs pcc attach [--id|-i ID] [--readonly|-r] [--write|-w] <file> ...\n"
+         .pc_help = "Copy given files to PCC, the local cache storage.\n"
+               "usage: lfs pcc attach [--id|-i ID] [--readonly|-r] [--write|-w] [-p|--pin] <file> ...\n"
                "\t-i: archive ID for PCC\n"
                "\t-r: readonly attach\n"
-               "\t-w: writeable attach\n" },
+               "\t-w: writeable attach\n"
+               "\t-p: pin and attach the file\n" },
        { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
-         .pc_help = "Attach given files into PCC by FID(s).\n"
-               "usage: lfs pcc attach_fid [--id|-i ID] {--mnt|-m MOUNTPATH} [--readonly|-r] [--write|-w] <FID> ...\n"
+         .pc_help = "Copy given files to PCC by FID(s), the local cache storage.\n"
+               "usage: lfs pcc attach_fid [--id|-i ID] {--mnt|-m MOUNTPATH} [--readonly|-r] [--write|-w] [-p|--pin] <FID> ...\n"
                "\t-i: archive ID for PCC\n"
                "\t-m: Lustre mount point\n"
                "\t-r: readonly attach\n"
-               "\t-w: writeable attach\n" },
+               "\t-w: writeable attach\n"
+               "\t-p: pin and attach the file\n" },
        { .pc_name = "state", .pc_func = lfs_pcc_state,
          .pc_help = "Display the PCC state for given files.\n"
                "usage: lfs pcc state <FILE> ...\n" },
@@ -11920,6 +11922,7 @@ static int lfs_pcc_attach(int argc, char **argv)
        { .val = 'i',   .name = "id",   .has_arg = required_argument },
        { .val = 'r',   .name = "readonly",     .has_arg = no_argument },
        { .val = 'w',   .name = "write",        .has_arg = no_argument },
+       { .val = 'p',   .name = "pin",          .has_arg = no_argument },
        { .name = NULL } };
        int c;
        int rc = 0;
@@ -11928,9 +11931,10 @@ static int lfs_pcc_attach(int argc, char **argv)
        char *end;
        char fullpath[PATH_MAX];
        enum lu_pcc_type type = LU_PCC_READONLY;
+       bool pin = false;
 
        optind = 0;
-       while ((c = getopt_long(argc, argv, "i:rw",
+       while ((c = getopt_long(argc, argv, "i:prw",
                                long_opts, NULL)) != -1) {
                switch (c) {
                case 'i':
@@ -11950,6 +11954,9 @@ static int lfs_pcc_attach(int argc, char **argv)
                case 'w':
                        type = LU_PCC_READWRITE;
                        break;
+               case 'p':
+                       pin = true;
+                       break;
                case '?':
                        return CMD_HELP;
                default:
@@ -11977,6 +11984,28 @@ static int lfs_pcc_attach(int argc, char **argv)
                        continue;
                }
 
+               if (pin) {
+                       if (attach_id == 0) {
+                               rc2 = llapi_pcc_backend_id_get(fullpath, type,
+                                                              &attach_id);
+                               if (rc2 < 0) {
+                                       fprintf(stderr,
+                                               "%s: failed to get archive id for '%s': %s\n",
+                                               argv[0], path, strerror(-rc2));
+                                       if (rc == 0)
+                                               rc = rc2;
+                                       continue;
+                               }
+                       }
+                       rc2 = llapi_pcc_pin_file(fullpath, attach_id);
+                       if (rc2 < 0) {
+                               fprintf(stderr, "%s: cannot pin '%s': %s\n",
+                                       argv[0], path, strerror(-rc2));
+                               if (rc == 0)
+                                       rc = rc2;
+                       }
+               }
+
                rc2 = llapi_pcc_attach(fullpath, attach_id, type);
                if (rc2 < 0) {
                        fprintf(stderr,
@@ -11996,15 +12025,18 @@ static int lfs_pcc_attach_fid(int argc, char **argv)
        { .val = 'r',   .name = "readonly",     .has_arg = no_argument },
        { .val = 'w',   .name = "write",        .has_arg = no_argument },
        { .val = 'm',   .name = "mnt",          .has_arg = required_argument },
+       { .val = 'p',   .name = "pin",          .has_arg = no_argument },
        { .name = NULL } };
-       char                     short_opts[] = "i:m:rw";
-       int                      c;
-       int                      rc = 0;
-       __u32                    attach_id = 0;
-       char                    *end;
-       const char              *mntpath = NULL;
-       const char              *fidstr;
-       enum lu_pcc_type         type = LU_PCC_READONLY;
+       char short_opts[] = "i:m:prw";
+       int c;
+       int rc = 0;
+       __u32 attach_id = 0;
+       char *end;
+       const char *mntpath = NULL;
+       const char *fidstr;
+       enum lu_pcc_type type = LU_PCC_READONLY;
+       bool pin = false;
+       struct lu_fid fid;
 
        optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
@@ -12030,6 +12062,9 @@ static int lfs_pcc_attach_fid(int argc, char **argv)
                case 'm':
                        mntpath = optarg;
                        break;
+               case 'p':
+                       pin = true;
+                       break;
                case '?':
                        return CMD_HELP;
                default:
@@ -12053,13 +12088,43 @@ static int lfs_pcc_attach_fid(int argc, char **argv)
                return CMD_HELP;
        }
 
+       if (pin && attach_id == 0) {
+               rc = llapi_pcc_backend_id_get(mntpath, type,
+                                             &attach_id);
+               if (rc < 0) {
+                       fprintf(stderr,
+                               "%s: failed to get archive id for "DFID" on '%s': %s\n",
+                               argv[0], PFID(&fid), mntpath,
+                               strerror(-rc));
+                       return rc;
+                }
+       }
+
        while (optind < argc) {
                int rc2;
 
                fidstr = argv[optind++];
 
-               rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
-                                              attach_id, type);
+               rc2 = llapi_fid_parse(fidstr, &fid, NULL);
+               if (rc2) {
+                       fprintf(stderr, "%s: invalid fid string: %s\n",
+                               argv[0], strerror(-rc2));
+                       rc = rc ? rc : rc2;
+                       continue;
+               }
+
+               if (pin) {
+                       rc2 = llapi_pcc_pin_fid(mntpath, &fid, attach_id);
+                       if (rc2) {
+                               fprintf(stderr, "%s: cannot pin fid '%s' on '%s': %s\n",
+                                       argv[0], fidstr, mntpath,
+                                       strerror(-rc2));
+                               rc = rc ? rc : rc2;
+                               continue;
+                       }
+               }
+
+               rc2 = llapi_pcc_attach_fid(mntpath, &fid, attach_id, type);
                if (rc2 < 0) {
                        fprintf(stderr,
                                "%s: cannot attach '%s' on '%s' to PCC with attach ID '%u': %s\n",
index 2fac644..3ebe237 100644 (file)
@@ -177,7 +177,7 @@ static int llapi_readwrite_pcc_attach_fid(const char *mntpath,
        if (fd < 0) {
                rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc,
-                           "llapi_open_by_fid for " DFID "failed",
+                           "llapi_open_by_fid for " DFID " failed",
                            PFID(fid));
                return rc;
        }
@@ -199,7 +199,7 @@ static int llapi_readonly_pcc_attach_fid(const char *mntpath,
        if (fd < 0) {
                rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc,
-                           "llapi_open_by_fid for " DFID "failed",
+                           "llapi_open_by_fid for " DFID " failed",
                            PFID(fid));
                return rc;
        }
@@ -1079,3 +1079,24 @@ int llapi_pcc_unpin_file(const char *path, __u32 id)
 out:
        return rc;
 }
+
+/**
+* Pin a given file with lustre mount point and fid for a PCC backend.
+*
+* \param[in] lustre_dir        lustre mount point
+* \param[in] fid       fid
+* \param[in] id                archive id
+* \return              0 or -errno
+*/
+int llapi_pcc_pin_fid(const char *lustre_dir, const struct lu_fid *fid,
+                     __u32 id)
+{
+       char path[PATH_MAX];
+       int rc;
+
+       snprintf(path, PATH_MAX, "%s/.lustre/fid/"DFID, lustre_dir,
+                PFID(fid));
+       rc = llapi_pcc_pin_file(path, id);
+
+       return rc;
+}