Whamcloud - gitweb
LU-14398 llapi: add llapi_fid2path_at() 06/41406/2
authorJohn L. Hammond <jhammond@whamcloud.com>
Wed, 3 Feb 2021 19:06:16 +0000 (13:06 -0600)
committerOleg Drokin <green@whamcloud.com>
Fri, 26 Feb 2021 08:23:19 +0000 (08:23 +0000)
Add llapi_fid2path_at() which works like llapi_fid2path() takes an
open FD on the moint point instead of a 'fsname or dirirectory path'
and a const struct lu_fid * instead of a const char *.

Signed-off-by: John L. Hammond <jhammond@whamcloud.com>
Change-Id: I76234bc28de231587b65c5d866954441e0893aac
Reviewed-on: https://review.whamcloud.com/41406
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Alex Zhuravlev <bzzz@whamcloud.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/include/lustre/lustreapi.h
lustre/tests/llapi_fid_test.c
lustre/utils/liblustreapi_fid.c

index 8ff6c8c..ca2b842 100644 (file)
@@ -443,6 +443,8 @@ int llapi_get_connect_flags(const char *mnt, __u64 *flags);
 int llapi_cp(int argc, char *argv[]);
 int llapi_ls(int argc, char *argv[]);
 int llapi_fid_parse(const char *fidstr, struct lu_fid *fid, char **endptr);
+int llapi_fid2path_at(int mnt_fd, const struct lu_fid *fid, char *path,
+                     int pathlen, long long *recno, int *linkno);
 int llapi_fid2path(const char *device, const char *fidstr, char *path,
                   int pathlen, long long *recno, int *linkno);
 int llapi_path2fid(const char *path, struct lu_fid *fid);
index 8f2f946..3c17e0a 100644 (file)
@@ -80,7 +80,8 @@
 static char mainpath[PATH_MAX];
 static const char *maindir = "llapi_fid_test_name_9585766";
 
-static char fsmountdir[PATH_MAX];      /* Lustre mountpoint */
+static char mnt_dir[PATH_MAX]; /* Lustre mountpoint */
+static int mnt_fd = -1;
 static char *lustre_dir;               /* Test directory inside Lustre */
 
 /* Cleanup our test directory. */
@@ -146,6 +147,23 @@ static void helper_fid2path(const char *filename, int fd)
        ASSERTF(linkno1 == linkno2, "linknos are different: %d / %d",
                linkno1, linkno2);
 
+       /* Use llapi_fid2path_at() */
+       recno2 = -1;
+       linkno2 = 0;
+       rc = llapi_fid2path_at(mnt_fd, &fid, path2, sizeof(path2),
+                              &recno2, &linkno2);
+       ASSERTF(rc == 0, "llapi_fid2path failed for fid %s: %s",
+               fidstr, strerror(-rc));
+
+       /* Make sure both calls to llapi_fid2path returned the same
+        * data. */
+       ASSERTF(strcmp(path1, path2) == 0, "paths are different: '%s' / '%s'",
+               path1, path2);
+       ASSERTF(recno1 == recno2, "recnos are different: %lld / %lld",
+               recno1, recno2);
+       ASSERTF(linkno1 == linkno2, "linknos are different: %d / %d",
+               linkno1, linkno2);
+
        /* Try fd2fid and check that the result is still the same. */
        if (fd != -1) {
                rc = llapi_fd2fid(fd, &fid3);
@@ -158,7 +176,7 @@ static void helper_fid2path(const char *filename, int fd)
 
        /* Pass the result back to fid2path and ensure the fid stays
         * the same. */
-       rc = snprintf(path3, sizeof(path3), "%s/%s", fsmountdir, path1);
+       rc = snprintf(path3, sizeof(path3), "%s/%s", mnt_dir, path1);
        ASSERTF((rc > 0 && rc < sizeof(path3)), "invalid name");
        rc = llapi_path2fid(path3, &fid2);
        ASSERTF(rc == 0, "llapi_path2fid failed for '%s': %s",
@@ -426,7 +444,7 @@ static void test30(void)
                ASSERTF(rc == 0, "llapi_fid2path failed for fid %s: %s",
                        fidstr, strerror(-rc));
 
-               snprintf(buf2, sizeof(buf2), "%s/%s", fsmountdir, buf);
+               snprintf(buf2, sizeof(buf2), "%s/%s", mnt_dir, buf);
 
                if (past_link_limit == false) {
                        /* Find the name in the links that were created */
@@ -705,13 +723,16 @@ int main(int argc, char *argv[])
        if (lustre_dir == NULL)
                lustre_dir = "/mnt/lustre";
 
-       rc = llapi_search_mounts(lustre_dir, 0, fsmountdir, fsname);
+       rc = llapi_search_mounts(lustre_dir, 0, mnt_dir, fsname);
        if (rc != 0) {
                fprintf(stderr, "Error: %s: not a Lustre filesystem\n",
                        lustre_dir);
                return EXIT_FAILURE;
        }
 
+       mnt_fd = open(mnt_dir, O_RDONLY|O_DIRECTORY);
+       ASSERTF(!(mnt_fd < 0), "cannot open '%s': %s\n", mnt_dir, strerror(errno));
+
        /* Play nice with Lustre test scripts. Non-line buffered output
         * stream under I/O redirection may appear incorrectly. */
        setvbuf(stdout, NULL, _IOLBF, 0);
index 5c39b14..578c6e6 100644 (file)
@@ -160,11 +160,51 @@ out:
        return rc;
 }
 
+int llapi_fid2path_at(int mnt_fd, const struct lu_fid *fid,
+                     char *path_buf, int path_buf_size,
+                     long long *recno, int *linkno)
+{
+       struct getinfo_fid2path *gf = NULL;
+       int rc;
+
+       gf = calloc(1, sizeof(*gf) + path_buf_size);
+       if (gf == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       gf->gf_fid = *fid;
+       if (recno != NULL)
+               gf->gf_recno = *recno;
+
+       if (linkno != NULL)
+               gf->gf_linkno = *linkno;
+
+       gf->gf_pathlen = path_buf_size;
+
+       rc = ioctl(mnt_fd, OBD_IOC_FID2PATH, gf);
+       if (rc) {
+               rc = -errno;
+               goto out;
+       }
+
+       rc = copy_strip_dne_path(gf->gf_u.gf_path, path_buf, path_buf_size);
+
+       if (recno != NULL)
+               *recno = gf->gf_recno;
+
+       if (linkno != NULL)
+               *linkno = gf->gf_linkno;
+out:
+       free(gf);
+
+       return rc;
+}
+
 int llapi_fid2path(const char *path_or_device, const char *fidstr, char *path,
                   int pathlen, long long *recno, int *linkno)
 {
        struct lu_fid fid;
-       struct getinfo_fid2path *gf = NULL;
        int mnt_fd = -1;
        int rc;
 
@@ -192,41 +232,11 @@ int llapi_fid2path(const char *path_or_device, const char *fidstr, char *path,
                goto out;
        }
 
-       gf = calloc(1, sizeof(*gf) + pathlen);
-       if (gf == NULL) {
-               rc = -ENOMEM;
-               goto out;
-       }
-
-       gf->gf_fid = fid;
-       if (recno)
-               gf->gf_recno = *recno;
-       if (linkno)
-               gf->gf_linkno = *linkno;
-       gf->gf_pathlen = pathlen;
-
-       rc = ioctl(mnt_fd, OBD_IOC_FID2PATH, gf);
-       if (rc < 0) {
-               rc = -errno;
-               goto out;
-       }
-
-       rc = copy_strip_dne_path(gf->gf_u.gf_path, path, pathlen);
-
-       if (recno)
-               *recno = gf->gf_recno;
-
-       if (linkno)
-               *linkno = gf->gf_linkno;
-
+       rc = llapi_fid2path_at(mnt_fd, &fid, path, pathlen, recno, linkno);
 out:
        if (!(mnt_fd < 0))
                close(mnt_fd);
 
-       free(gf);
-
-       errno = -rc;
-
        return rc;
 }