From 08892438473f3188de3dc7f76b0ce433eaef4367 Mon Sep 17 00:00:00 2001 From: Emoly Liu Date: Wed, 1 Jul 2020 18:07:00 +0800 Subject: [PATCH] LU-13732 lfs: fid2path should match the root path correctly This patch is to match the root path in function get_root_path() correctly. For example, if the mount point is /mnt/lustre, the following root path formats are acceptable: - /mnt/lustre - /mnt/lustre/* sanity.sh test_154A/247d are modified to verify this patch. Signed-off-by: Emoly Liu Change-Id: If705dd341b273d462aeba280fa27d5608b5f3b7c Reviewed-on: https://review.whamcloud.com/39225 Tested-by: jenkins Reviewed-by: Andreas Dilger Tested-by: Maloo Reviewed-by: Jian Yu Reviewed-by: Oleg Drokin --- lustre/tests/sanity.sh | 42 ++++++++++++++++++++++++++++++++++-------- lustre/utils/lfs.c | 28 +++++++++++++++++++++------- lustre/utils/liblustreapi.c | 13 ++++++++----- 3 files changed, 63 insertions(+), 20 deletions(-) diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh index 4b43cd0..3cb0d6b 100755 --- a/lustre/tests/sanity.sh +++ b/lustre/tests/sanity.sh @@ -13642,10 +13642,19 @@ test_154A() { [ -z "$fid" ] && error "path2fid unable to get $tf FID" # check that we get the same pathname back - local found=$($LFS fid2path $MOUNT "$fid") - [ -z "$found" ] && error "fid2path unable to get '$fid' path" - [ "$found" == "$tf" ] || - error "fid2path($fid=path2fid($tf)) = $found != $tf" + local rootpath + local found + for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do + echo "$rootpath $fid" + found=$($LFS fid2path $rootpath "$fid") + [ -z "$found" ] && error "fid2path unable to get '$fid' path" + [ "$found" == "$tf" ] || error "fid2path $found != $tf" + done + + # check wrong root path format + rootpath=$MOUNT"_wrong" + found=$($LFS fid2path $rootpath "$fid") + [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)" } run_test 154A "lfs path2fid and fid2path basic checks" @@ -18655,7 +18664,7 @@ test_247c() { $LFS fid2path $submount $fid && error "fid2path should fail" cleanup_247 $submount } -run_test 247c "running fid2path outside root" +run_test 247c "running fid2path outside subdirectory root" test_247d() { lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree || @@ -18668,11 +18677,28 @@ test_247d() { FILESET="$FILESET/$tdir" mount_client $submount || error "mount $submount failed" trap "cleanup_247 $submount" EXIT - local fid=$($LFS path2fid $submount/dir1) - $LFS fid2path $submount $fid || error "fid2path should succeed" + + local td=$submount/dir1 + local fid=$($LFS path2fid $td) + [ -z "$fid" ] && error "path2fid unable to get $td FID" + + # check that we get the same pathname back + local rootpath + local found + for rootpath in "$submount" "$submount///" "$submount/dir1"; do + echo "$rootpath $fid" + found=$($LFS fid2path $rootpath "$fid") + [ -n "found" ] || error "fid2path should succeed" + [ "$found" == "$td" ] || error "fid2path $found != $td" + done + # check wrong root path format + rootpath=$submount"_wrong" + found=$($LFS fid2path $rootpath "$fid") + [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)" + cleanup_247 $submount } -run_test 247d "running fid2path inside root" +run_test 247d "running fid2path inside subdirectory root" # LU-8037 test_247e() { diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 3f7e129..82fa2c5 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -8113,8 +8113,9 @@ static int lfs_fid2path(int argc, char **argv) { .val = 'l', .name = "link", .has_arg = required_argument }, { .val = 'r', .name = "rec", .has_arg = required_argument }, { .name = NULL } }; - char short_opts[] = "cl:r:"; - char *device, *fid, *path; + char short_opts[] = "cl:r:"; + char mntdir[PATH_MAX]; + char *device, *fid, *path, *rootpath; long long recno = -1; int linkno = -1; int lnktmp; @@ -8172,6 +8173,18 @@ static int lfs_fid2path(int argc, char **argv) } rc = 0; + /* in case that device is not the mountpoint */ + if (device[0] == '/') { + rc = llapi_search_mounts(device, 0, mntdir, NULL); + if (rc == 0) { + rootpath = mntdir; + } else { + fprintf(stderr, + "%s fid2path: %s has no mountpoint: %s\n", + progname, device, strerror(-rc)); + goto out; + } + } while (optind < argc) { fid = argv[optind++]; @@ -8185,8 +8198,9 @@ static int lfs_fid2path(int argc, char **argv) &rectmp, &lnktmp); if (rc2 < 0) { fprintf(stderr, - "%s fid2path: cannot find '%s': %s\n", - progname, fid, strerror(errno = -rc2)); + "%s fid2path: cannot find %s %s: %s\n", + progname, device, fid, + strerror(errno = -rc2)); if (rc == 0) rc = rc2; break; @@ -8195,8 +8209,8 @@ static int lfs_fid2path(int argc, char **argv) if (printcur) fprintf(stdout, "%lld ", rectmp); if (device[0] == '/') { - fprintf(stdout, "%s", device); - if (device[strlen(device) - 1] != '/') + fprintf(stdout, "%s", rootpath); + if (rootpath[strlen(rootpath) - 1] != '/') fprintf(stdout, "/"); } else if (path[0] == '\0') { fprintf(stdout, "/"); @@ -8211,7 +8225,7 @@ static int lfs_fid2path(int argc, char **argv) break; } } - +out: free(path); return rc; } diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 5575b78..44ac26c 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -1289,7 +1289,7 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) char buf[PATH_MAX], mntdir[PATH_MAX]; char *ptr, *ptr_end; FILE *fp; - int idx = 0, len = 0, mntlen, fd; + int idx = 0, mntlen = 0, fd; int rc = -ENODEV; int fsnamelen, mountlen; @@ -1345,16 +1345,19 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) rc = 0; break; /* Otherwise find the longest matching path */ - } else if ((strlen(path) >= mntlen) && (mntlen >= len) && + } else if ((strlen(path) >= mntlen) && (strncmp(mnt.mnt_dir, path, mntlen) == 0)) { + /* check the path format */ + if (strlen(path) > mntlen && path[mntlen] != '/') + continue; strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1); mntdir[sizeof(mntdir) - 1] = '\0'; - len = mntlen; if ((want & WANT_FSNAME) && fsname != NULL) { strncpy(fsname, ptr, mountlen); fsname[mountlen] = '\0'; } rc = 0; + break; } } endmntent(fp); @@ -1362,8 +1365,8 @@ int get_root_path(int want, char *fsname, int *outfd, char *path, int index) /* Found it */ if (rc == 0) { if ((want & WANT_PATH) && path != NULL) { - strncpy(path, mntdir, PATH_MAX); - path[strlen(mntdir)] = '\0'; + strncpy(path, mntdir, mntlen); + path[mntlen] = '\0'; } if (want & WANT_FD) { fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK); -- 1.8.3.1