From 8dcdb6a4731f2953c4c4506b010da307cc99fa0a Mon Sep 17 00:00:00 2001 From: Qian Yingjin Date: Fri, 19 Mar 2021 16:45:26 +0800 Subject: [PATCH] LU-10499 pcc: don't reopen mountpoint for each cache file 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. EX-bug-id: EX-2861 Signed-off-by: Qian Yingjin Change-Id: Iad92c216262296096e30ca4a4c6b2765dfd3afaa Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/54388 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: James Simmons Reviewed-by: Li Xi Reviewed-by: Oleg Drokin --- lustre/include/lustre/lustreapi.h | 2 + lustre/llite/vvp_io.c | 2 +- lustre/utils/lfs.c | 27 ++++++++++-- lustre/utils/libhsm_scanner.h | 1 + lustre/utils/liblustreapi_pcc.c | 89 ++++++++++++++++++++++----------------- 5 files changed, 77 insertions(+), 44 deletions(-) diff --git a/lustre/include/lustre/lustreapi.h b/lustre/include/lustre/lustreapi.h index ed7b8d4..5be8c33 100644 --- a/lustre/include/lustre/lustreapi.h +++ b/lustre/include/lustre/lustreapi.h @@ -717,6 +717,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); diff --git a/lustre/llite/vvp_io.c b/lustre/llite/vvp_io.c index 78ca667..82eb1ba 100644 --- a/lustre/llite/vvp_io.c +++ b/lustre/llite/vvp_io.c @@ -1034,7 +1034,7 @@ static void vvp_set_batch_dirty(struct folio_batch *fbatch) struct page *page = fbatch_at_pg(fbatch, 0, 0); int count = folio_batch_count(fbatch); int i; -#if !defined(HAVE_FOLIO_BATCH) || !defined(HAVE_FILEMAP_GET_FOLIOS) || \ +#if !defined(HAVE_FOLIO_BATCH) || !defined(HAVE_FILEMAP_GET_FOLIOS) || \ defined(HAVE_KALLSYMS_LOOKUP_NAME) int pg, npgs; #endif diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 002acdf..24a50ec 100755 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -13825,9 +13825,10 @@ static int lfs_pcc_detach_fid(int argc, char **argv) char short_opts[] = "hk"; int c; int rc = 0; - const char *fid; + 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, @@ -13846,21 +13847,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; } diff --git a/lustre/utils/libhsm_scanner.h b/lustre/utils/libhsm_scanner.h index 6bc2dfe..4dea572 100644 --- a/lustre/utils/libhsm_scanner.h +++ b/lustre/utils/libhsm_scanner.h @@ -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); diff --git a/lustre/utils/liblustreapi_pcc.c b/lustre/utils/liblustreapi_pcc.c index 2e6a2ec..9e6710b 100644 --- a/lustre/utils/liblustreapi_pcc.c +++ b/lustre/utils/liblustreapi_pcc.c @@ -234,16 +234,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; } @@ -276,6 +271,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. @@ -326,16 +343,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; } @@ -550,7 +562,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. */ @@ -576,27 +587,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 = llapi_root_path_open(hsc->hsc_mntpath, &fd); + 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) { rc = -errno; llapi_printf(LLAPI_MSG_DEBUG, @@ -635,7 +635,14 @@ static int llapi_pcc_del_internal(const char *mntpath, const char *pccpath, enum hsmtool_type type, enum lu_pcc_cleanup_flags 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; @@ -650,12 +657,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; } -- 1.8.3.1