Whamcloud - gitweb
EX-2861 pcc: don't reopen mountpoint for each cache file
authorQian Yingjin <qian@ddn.com>
Fri, 19 Mar 2021 08:45:26 +0000 (16:45 +0800)
committerLi Xi <lixi@ddn.com>
Fri, 14 May 2021 08:00:40 +0000 (08:00 +0000)
When scanning and processing files in the PCC cache filesystem
(e.g. "llapi_pcc_scan_detach()" is looking for the Lustre
mountpoint and reopening it for every file processed.

This patch changed it to open the Lustre mountpoint only once,
then reuse the file handle for all of the later calls. The file
handle will be closed when finished the processing.

This patch also repaces to use llapi_fid_parse to get FID from
an given string.

Signed-off-by: Qian Yingjin <qian@ddn.com>
Change-Id: Iad92c216262296096e30ca4a4c6b2765dfd3afaa
Reviewed-on: https://review.whamcloud.com/42107
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: John L. Hammond <jhammond@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
lustre/include/lustre/lustreapi.h
lustre/utils/lfs.c
lustre/utils/libhsm_scanner.h
lustre/utils/liblustreapi_pcc.c

index 05c30a0..dcf7967 100644 (file)
@@ -633,6 +633,8 @@ int llapi_pcc_detach_fid(const char *mntpath, const struct lu_fid *fid,
 int llapi_pcc_detach_fid_str(const char *mntpath, const char *fidstr,
                             __u32 flags);
 int llapi_pcc_detach_file(const char *path, __u32 flags);
+int llapi_pcc_detach_at(int dirfd, const struct lu_fid *fid,
+                       enum lu_pcc_detach_flags flags);
 int llapi_pcc_state_get_fd(int fd, struct lu_pcc_state *state);
 int llapi_pcc_state_get(const char *path, struct lu_pcc_state *state);
 int llapi_pccdev_set(const char *mntpath, const char *cmd);
index daa06d8..1a49939 100644 (file)
@@ -11686,12 +11686,13 @@ static int lfs_pcc_detach_fid(int argc, char **argv)
        struct option long_opts[] = {
        { .val = 'k',   .name = "keep", .has_arg = no_argument },
        { .name = NULL } };
-       char             short_opts[] = "k";
-       int              c;
-       int              rc = 0;
-       const char      *fid;
-       const char      *mntpath;
-       __u32            detach_flags = PCC_DETACH_FL_UNCACHE;
+       char short_opts[] = "k";
+       int c;
+       int rc = 0;
+       const char *fidstr;
+       const char *mntpath;
+       __u32 detach_flags = PCC_DETACH_FL_UNCACHE;
+       int dirfd;
 
        optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
@@ -11710,21 +11711,39 @@ static int lfs_pcc_detach_fid(int argc, char **argv)
        }
 
        mntpath = argv[optind++];
+       dirfd = open(mntpath, O_RDONLY);
+       if (dirfd < 0) {
+               rc = -errno;
+               fprintf(stderr, "%s: cannot open '%s': %s",
+                       argv[0], mntpath, strerror(errno));
+               return rc;
+       }
 
        while (optind < argc) {
+               struct lu_fid fid;
                int rc2;
 
-               fid = argv[optind++];
+               fidstr = argv[optind++];
+               rc2 = llapi_fid_parse(fidstr, &fid, NULL);
+               if (rc2) {
+                       fprintf(stderr, "%s: '%s' is not a valid FID\n",
+                               argv[0], fidstr);
+                       if (rc == 0)
+                               rc = rc2;
+                       continue;
+               }
 
-               rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_flags);
+               rc2 = llapi_pcc_detach_at(dirfd, &fid, detach_flags);
                if (rc2 < 0) {
                        fprintf(stderr,
                                "%s: cannot detach '%s' on '%s' from PCC: %s\n",
-                               argv[0], fid, mntpath, strerror(-rc2));
+                               argv[0], fidstr, mntpath, strerror(-rc2));
                        if (rc == 0)
                                rc = rc2;
                }
        }
+
+       close(dirfd);
        return rc;
 }
 
index 6bc2dfe..4dea572 100644 (file)
@@ -43,6 +43,7 @@ struct hsm_scan_control {
        const char              *hsc_hsmpath;
        hsm_scan_func_t          hsc_func;
        int                      hsc_errnum;
+       int                      hsc_mntfd;
 };
 
 int hsm_scan_process(struct hsm_scan_control *hsc);
index 8cb44fb..885a4cc 100644 (file)
@@ -232,16 +232,11 @@ int llapi_pcc_attach_fid_str(const char *mntpath, const char *fidstr,
 {
        int rc;
        struct lu_fid fid;
-       const char *fidstr_orig = fidstr;
 
-       while (*fidstr == '[')
-               fidstr++;
-       rc = sscanf(fidstr, SFID, RFID(&fid));
-       if (rc != 3) {
+       rc = llapi_fid_parse(fidstr, &fid, NULL);
+       if (rc) {
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "bad FID format '%s', should be [seq:oid:ver]"
-                                 " (e.g. "DFID")\n", fidstr_orig,
-                                 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
+                                 "PCC: '%s' is not a valid FID", fidstr);
                return -EINVAL;
        }
 
@@ -271,6 +266,28 @@ int llapi_pcc_detach_fd(int fd, __u32 flags)
 /**
  * detach PCC cache of a file via FID.
  *
+ * \param dirfd                Dir file handle.
+ * \param fid          FID of the file.
+ * \param flags                Detach flags.
+ *
+ * \return 0 on success, an error code otherwise.
+ */
+int llapi_pcc_detach_at(int dirfd, const struct lu_fid *fid,
+                       enum lu_pcc_detach_flags flags)
+{
+       int rc;
+       struct lu_pcc_detach_fid detach = {
+               .pccd_fid = *fid,
+               .pccd_flags = flags,
+       };
+
+       rc = ioctl(dirfd, LL_IOC_PCC_DETACH_BY_FID, &detach);
+       return rc;
+}
+
+/**
+ * detach PCC cache of a file via FID.
+ *
  * \param mntpath      Fullpath to the client mount point.
  * \param fid          FID of the file.
  * \param flags                Detach flags.
@@ -319,16 +336,11 @@ int llapi_pcc_detach_fid_str(const char *mntpath, const char *fidstr,
 {
        int rc;
        struct lu_fid fid;
-       const char *fidstr_orig = fidstr;
 
-       while (*fidstr == '[')
-               fidstr++;
-       rc = sscanf(fidstr, SFID, RFID(&fid));
-       if (rc != 3 || !fid_is_sane(&fid)) {
+       rc = llapi_fid_parse(fidstr, &fid, NULL);
+       if (rc) {
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "bad FID format '%s', should be [seq:oid:ver]"
-                                 " (e.g. "DFID")\n", fidstr_orig,
-                                 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
+                                 "PCC: '%s' is not a valid FID", fidstr);
                return -EINVAL;
        }
 
@@ -554,7 +566,6 @@ static int llapi_pcc_scan_detach(const char *pname, const char *fname,
        char fidstr[FID_LEN];
        const char *fidname;
        bool lov_file;
-       int fd;
        int rc;
 
        /* It is the saved lov file when archive on HSM backend. */
@@ -580,27 +591,16 @@ static int llapi_pcc_scan_detach(const char *pname, const char *fname,
                fidname = fname;
        }
 
-       rc = sscanf(fidname, SFID, RFID(&detach.pccd_fid));
-       if (rc != 3 || !fid_is_sane(&detach.pccd_fid)) {
-               llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "bad FID format '%s', should be [seq:oid:ver] (e.g. "DFID")\n",
-                                 fidname, (unsigned long long)FID_SEQ_NORMAL,
-                                 2, 0);
-               return -EINVAL;
-       }
-
-       llapi_printf(LLAPI_MSG_DEBUG, "Handle the file: %s\n", fidname);
-
-       rc = get_root_path(WANT_FD, NULL, &fd, (char *)hsc->hsc_mntpath, -1);
+       rc = llapi_fid_parse(fidname, &detach.pccd_fid, NULL);
        if (rc) {
-               llapi_error(LLAPI_MSG_ERROR, rc, "cannot get root path: %s",
-                           hsc->hsc_mntpath);
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "PCC: '%s' is not a valid FID", fidname);
                return rc;
        }
 
-       rc = ioctl(fd, LL_IOC_PCC_DETACH_BY_FID, &detach);
-       close(fd);
+       llapi_printf(LLAPI_MSG_DEBUG, "Handle the file: %s\n", fidname);
 
+       rc = ioctl(hsc->hsc_mntfd, LL_IOC_PCC_DETACH_BY_FID, &detach);
        if (rc) {
                llapi_printf(LLAPI_MSG_DEBUG,
                             "failed to detach file: %s\n", fidname);
@@ -636,7 +636,14 @@ static int llapi_pcc_scan_detach(const char *pname, const char *fname,
 static int llapi_pcc_del_internal(const char *mntpath, const char *pccpath,
                                  enum hsmtool_type type, __u32 flags)
 {
-       struct hsm_scan_control hsc;
+       struct hsm_scan_control hsc = {
+               .hsc_type = type,
+               .hsc_mntpath = mntpath,
+               .hsc_hsmpath = pccpath,
+               .hsc_mntfd = -1,
+               .hsc_func = llapi_pcc_scan_detach,
+               .hsc_errnum = 0,
+       };
        char cmd[PATH_MAX];
        int rc;
 
@@ -651,12 +658,16 @@ static int llapi_pcc_del_internal(const char *mntpath, const char *pccpath,
        if (flags & PCC_CLEANUP_FL_KEEP_DATA)
                return 0;
 
-       hsc.hsc_type = type;
-       hsc.hsc_mntpath = mntpath;
-       hsc.hsc_hsmpath = pccpath;
-       hsc.hsc_func = llapi_pcc_scan_detach;
-       hsc.hsc_errnum = 0;
+       hsc.hsc_mntfd = open(mntpath, O_RDONLY);
+       if (hsc.hsc_mntfd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "cannot open '%s'", mntpath);
+               return rc;
+       }
+
        rc = hsm_scan_process(&hsc);
+       close(hsc.hsc_mntfd);
 
        return rc;
 }