Whamcloud - gitweb
LU-14398 llapi: add llapi_fid2path_at()
[fs/lustre-release.git] / lustre / tests / llapi_fid_test.c
index 6d08f61..3c17e0a 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 
 /*
  * Copyright 2014 Cray Inc, all rights reserved.
+ *
+ * Copyright (c) 2016, Intel Corporation.
+ *
  * Author: Frank Zago.
  *
  * A few portions are extracted from llapi_layout_test.c
 #include <fcntl.h>
 #include <unistd.h>
 #include <poll.h>
+#include <sys/ioctl.h>
 #include <time.h>
 
 #include <lustre/lustreapi.h>
-#include <lustre/lustre_idl.h>
+#include <linux/lustre/lustre_idl.h>
 
 #define ERROR(fmt, ...)                                                        \
        fprintf(stderr, "%s: %s:%d: %s: " fmt "\n",                     \
@@ -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. */
@@ -92,17 +93,20 @@ static void cleanup(void)
        rc = snprintf(cmd, sizeof(cmd), "rm -rf -- '%s'", mainpath);
        ASSERTF(rc > 0 && rc < sizeof(cmd),
                "invalid delete command for path '%s'", mainpath);
-       system(cmd);
+       rc = system(cmd);
+       ASSERTF(rc != -1, "Cannot execute rm command");
+       ASSERTF(WEXITSTATUS(rc) == 0,
+               "rm command returned %d", WEXITSTATUS(rc));
 }
 
 /* Helper - call path2fid, fd2fid and fid2path against an existing
  * file/directory */
 static void helper_fid2path(const char *filename, int fd)
 {
-       lustre_fid fid;
-       lustre_fid fid2;
-       lustre_fid fid3;
-       char fidstr[FID_LEN];
+       struct lu_fid fid;
+       struct lu_fid fid2;
+       struct lu_fid fid3;
+       char fidstr[FID_LEN + 1];
        char path1[PATH_MAX];
        char path2[PATH_MAX];
        char path3[PATH_MAX];
@@ -143,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);
@@ -155,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",
@@ -237,8 +258,8 @@ static void test11(void)
 {
        int rc;
        int fd;
-       lustre_fid fid;
-       char fidstr[FID_LEN];
+       struct lu_fid fid;
+       char fidstr[FID_LEN + 1];
        char path[PATH_MAX];
        long long recno;
        int linkno;
@@ -273,13 +294,13 @@ static void test12(void)
        int fd;
        int fd2;
        int fd3;
-       lustre_fid fid;
+       struct lu_fid fid;
 
        /* Against a volatile file */
        rc = mkdir(mainpath, 0);
        ASSERTF(rc == 0, "mkdir failed for '%s': %s",
                mainpath, strerror(errno));
-       fd = llapi_create_volatile_idx(mainpath, -1, 0600);
+       fd = llapi_create_volatile_idx(mainpath, -1, 0);
        ASSERTF(fd >= 0, "creat failed for '%s': %s",
                mainpath, strerror(errno));
 
@@ -288,7 +309,7 @@ static void test12(void)
                mainpath, strerror(-rc));
 
        /* No many ways to test, except to open by fid. */
-       fd2 = llapi_open_by_fid(mainpath, &fid, 0600);
+       fd2 = llapi_open_by_fid(mainpath, &fid, O_RDONLY);
        ASSERTF(fd2 >= 0, "llapi_open_by_fid for " DFID_NOBRACE ": %s",
                PFID(&fid), strerror(errno));
 
@@ -296,7 +317,7 @@ static void test12(void)
 
        /* Check the file can still be opened, since fd2 is not
         * closed. */
-       fd3 = llapi_open_by_fid(mainpath, &fid, 0600);
+       fd3 = llapi_open_by_fid(mainpath, &fid, O_RDONLY);
        ASSERTF(fd3 >= 0, "llapi_open_by_fid for " DFID_NOBRACE ": %s",
                PFID(&fid), strerror(errno));
 
@@ -304,7 +325,7 @@ static void test12(void)
        close(fd3);
 
        /* The volatile file is gone now. */
-       fd = llapi_open_by_fid(mainpath, &fid, 0600);
+       fd = llapi_open_by_fid(mainpath, &fid, O_RDONLY);
        ASSERTF(fd < 0, "llapi_open_by_fid for " DFID_NOBRACE ": %d",
                PFID(&fid), fd);
 }
@@ -330,7 +351,7 @@ static void test20(void)
        /* Create subdirectories as long as we can. Each new subdir is
         * "/x", so we need at least 3 characters left in testpath. */
        while (len <= sizeof(testpath) - 3) {
-               strncat(testpath, "/x", 2);
+               strncat(testpath, "/x", sizeof(testpath) - 1);
 
                len += 2;
 
@@ -366,9 +387,9 @@ static void test30(void)
                bool seen;
        } links[num_links];
        char buf[PATH_MAX];
-       char buf2[PATH_MAX];
-       lustre_fid fid;
-       char fidstr[FID_LEN];
+       char buf2[PATH_MAX * 2];
+       struct lu_fid fid;
+       char fidstr[FID_LEN + 1];
        int rc;
        int i;
        int j;
@@ -423,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 */
@@ -473,8 +494,8 @@ static void test30(void)
  * type). mainpath must exist. */
 static void help_test40(void)
 {
-       lustre_fid parent_fid;
-       lustre_fid fid2;
+       struct lu_fid parent_fid;
+       struct lu_fid fid2;
        char buf[PATH_MAX];
        int rc;
 
@@ -571,7 +592,7 @@ static void test42(void)
        const int num_links = 100;
        struct {
                char subdir[PATH_MAX];
-               lustre_fid subdir_fid;
+               struct lu_fid subdir_fid;
                char filename[PATH_MAX];
                bool seen;
        } links[num_links];
@@ -581,7 +602,7 @@ static void test42(void)
        int i;
        int fd;
        int linkno;
-       lustre_fid parent_fid;
+       struct lu_fid parent_fid;
 
        /* Create the containing directory. */
        rc = mkdir(mainpath, 0);
@@ -695,20 +716,23 @@ static void process_args(int argc, char *argv[])
 
 int main(int argc, char *argv[])
 {
-       char fsname[8];
+       char fsname[8 + 1];
        int rc;
 
        process_args(argc, 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);