From ead624f0815c7b82d45b92322e9fb0abef027e49 Mon Sep 17 00:00:00 2001
From: wangchao <wangchao>
Date: Fri, 12 Dec 2003 06:43:41 +0000
Subject: [PATCH] b=1792 r=Chris

add sanity test for "iopen_connect_dentry() on already-connected dentry"
---
 lustre/tests/Makefile.am  |   4 +-
 lustre/tests/iopentest1.c |  82 +++++++++++++++++++++
 lustre/tests/iopentest2.c | 145 ++++++++++++++++++++++++++++++++++++++
 lustre/tests/sanity.sh    |  20 ++++++
 4 files changed, 250 insertions(+), 1 deletion(-)
 create mode 100644 lustre/tests/iopentest1.c
 create mode 100644 lustre/tests/iopentest2.c

diff --git a/lustre/tests/Makefile.am b/lustre/tests/Makefile.am
index 3a91b5a358..ecc1908a80 100644
--- a/lustre/tests/Makefile.am
+++ b/lustre/tests/Makefile.am
@@ -21,13 +21,15 @@ noinst_PROGRAMS += wantedi statone runas openfile getdents mkdirdeep o_directory
 noinst_PROGRAMS += small_write multiop sleeptest ll_sparseness_verify
 noinst_PROGRAMS += ll_sparseness_write mrename
 # noinst_PROGRAMS += ldaptest
-bin_PROGRAMS = mcreate munlink mkdirmany
+bin_PROGRAMS = mcreate munlink mkdirmany iopentest1 iopentest2
 
 # ldaptest_SOURCES = ldaptest.c
 tchmod_SOURCES = tchmod.c
 toexcl_SOURCES = toexcl.c
 testreq_SOURCES = testreq.c
 mcreate_SOURCES = mcreate.c
+iopentest1_SOURCES = iopentest1.c
+iopentest2_SOURCES = iopentest2.c
 munlink_SOURCES = munlink.c
 mlink_SOURCES = mlink.c
 truncate_SOURCES = truncate.c
diff --git a/lustre/tests/iopentest1.c b/lustre/tests/iopentest1.c
new file mode 100644
index 0000000000..25ad4019e4
--- /dev/null
+++ b/lustre/tests/iopentest1.c
@@ -0,0 +1,82 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+#include <errno.h>
+
+const char *progname;
+const char usage_fmt[] = "Usage: %s <file> <mountpoint>\n";
+#define INAME_LEN (PATH_MAX + 1)
+
+#define CHECK_IT(exp, pstr) \
+if (!(exp)) { \
+    fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
+    perror((pstr)); \
+    exit(1); \
+}
+
+#define CHECK_SNPRINTF(rc, len) \
+    CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
+
+static char *get_iname(char *fname, const char *mtpt)
+{
+        char *iname;
+        int fd, rc;
+        struct stat buf;
+
+        iname = malloc(INAME_LEN);
+        CHECK_IT(iname, "malloc() failed");
+
+        fd = open(fname, O_CREAT, 0644);
+        CHECK_IT(fd >= 0 || errno == EISDIR, "open(fname) failed");
+
+        if (fd >= 0)
+                close(fd);
+
+        rc = stat(fname, &buf);
+        CHECK_IT(rc == 0, "stat(fname) failed");
+
+        rc = snprintf(iname, INAME_LEN,
+                      "%s/__iopen__/%lu", mtpt, buf.st_ino);
+        CHECK_SNPRINTF(rc, INAME_LEN);
+
+        return iname;
+}
+
+int main(int argc, char *argv[])
+{
+        char *fname, *mtpt, *iname, *pname;
+        struct stat buf;
+        int rc;
+        int i;
+
+        pname = strdup(argv[0]);
+        progname = basename(pname);
+	
+        if (argc != 3) {
+                fprintf(stderr, usage_fmt, progname);
+                return 1;
+        }
+
+        fname = argv[1];
+        mtpt  = argv[2];
+
+        iname = get_iname(fname, mtpt);
+        i=10000;
+        printf("%s:started...\n",argv[0]);
+        do {
+                rc = stat(fname, &buf);
+                CHECK_IT(rc == 0, "stat(fname) failed");
+		
+                rc = stat(iname, &buf);
+                CHECK_IT(rc == 0, "stat(iname) failed");
+                i--;
+        } while (i>=1);
+
+        return 0;
+}
diff --git a/lustre/tests/iopentest2.c b/lustre/tests/iopentest2.c
new file mode 100644
index 0000000000..dfa0b828cc
--- /dev/null
+++ b/lustre/tests/iopentest2.c
@@ -0,0 +1,145 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+#include <errno.h>
+
+const char usage_fmt[] = "Usage: %s <mountpoint>\n";
+#define INAME_LEN (PATH_MAX + 1)
+const char *progname;
+
+#define CHECK_IT(exp, pstr) \
+if (!(exp)) { \
+    fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
+    perror((pstr)); \
+    exit(1); \
+}
+
+#define CHECK_SNPRINTF(rc, len) \
+    CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
+
+static char *get_iname(char *fname, const char *mtpt)
+{
+        char *iname;
+        int fd, rc;
+        struct stat buf;
+
+        iname = malloc(INAME_LEN);
+        CHECK_IT(iname, "malloc() failed");
+
+        fd = open(fname, O_CREAT, 0644);
+        CHECK_IT(fd >= 0 || errno == EISDIR, "open(fname) failed");
+
+        if (fd >= 0)
+                close(fd);
+
+        rc = stat(fname, &buf);
+        CHECK_IT(rc == 0, "stat(fname) failed");
+
+        rc = snprintf(iname, INAME_LEN,
+                      "%s/__iopen__/%lu", mtpt, buf.st_ino);
+        CHECK_SNPRINTF(rc, INAME_LEN);
+
+        return iname;
+}
+
+int main(int argc, char *argv[])
+{
+        char *fname, *mtpt, *pname;
+        int rc, fd;
+        char *fname_iname, *dir;
+        char *dir_iname = NULL, *foo = NULL, *bar = NULL;
+
+        int i,j;
+        int thread=0;
+
+        pname = strdup(argv[0]);
+        progname = basename(argv[0]);
+                                                                                                                             
+        if (argc != 2) {
+                fprintf(stderr, usage_fmt, progname);
+                return 1;
+        }
+ 
+        for(i=1;i<=10;i++) {
+                rc=fork();
+                if (rc < 0) {
+                        fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
+                                strerror(rc = errno));
+                        break;
+                } else if (rc == 0) {
+                        thread = i;
+                        break;
+                } else {
+                        printf("%s: thread #%ld (PID %d) started\n",
+                               argv[0], i, rc);
+                }
+                rc = 0;
+        }
+
+        if (thread!=0){
+                j=1000;
+
+                mtpt  = argv[1];
+                fname = malloc(INAME_LEN);
+                CHECK_IT(fname, "malloc() failed");
+
+                rc = snprintf(fname, INAME_LEN,
+                              "%s/%d", mtpt, getpid());
+                CHECK_SNPRINTF(rc, INAME_LEN);
+
+                rc = mkdir(fname, 0644);
+                CHECK_IT(rc == 0, "mkdir(fname) failed");
+
+                fname_iname = get_iname(fname, mtpt);
+
+                dir = malloc(INAME_LEN);
+                CHECK_IT(dir, "malloc() failed");
+
+                rc = snprintf(dir, INAME_LEN,
+                              "%s/dir", fname_iname);
+                CHECK_SNPRINTF(rc, INAME_LEN);
+
+                foo = malloc(INAME_LEN);
+                CHECK_IT(foo, "malloc() failed");
+
+                bar = malloc(INAME_LEN);
+                CHECK_IT(bar, "malloc() failed");
+
+                do {
+                        rc = mkdir(dir, 0644);
+                        CHECK_IT(rc == 0, "mkdir() failed");
+
+                        dir_iname = get_iname(dir, mtpt);
+		
+                        rc = snprintf(foo, INAME_LEN,
+                                      "%s/bar", dir_iname);
+                        CHECK_SNPRINTF(rc, INAME_LEN);
+		
+                        rc = snprintf(bar, INAME_LEN,
+                                      "%s/bar", dir_iname);
+                        CHECK_SNPRINTF(rc, INAME_LEN);
+
+                        fd = open(foo, O_CREAT, 0644);
+                        CHECK_IT(fd >= 0, "open(foo) failed");
+                        close(fd);
+
+                        rc = rename(foo, bar);
+                        CHECK_IT(rc == 0, "rename(foo, bar) failed");
+
+                        rc = unlink(bar);
+                        CHECK_IT(rc == 0, "unlink(bar) failed");
+                        rc = rmdir(dir);
+                        CHECK_IT(rc == 0, "rmdir(dir) failed");
+
+                        free(dir_iname);
+                        j--;
+                } while (j>=1);
+        }
+        return 0;
+}
diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh
index 41b20e509a..87e13405aa 100644
--- a/lustre/tests/sanity.sh
+++ b/lustre/tests/sanity.sh
@@ -14,6 +14,8 @@ ALWAYS_EXCEPT=${ALWAYS_EXCEPT:-"42b"}
 SRCDIR=`dirname $0`
 PATH=$PWD/$SRCDIR:$SRCDIR:$SRCDIR/../utils:$PATH
 
+TMP=${TMP:-/tmp}
+
 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
 CREATETEST=${CREATETEST:-createtest}
 LFIND=${LFIND:-lfind}
@@ -27,6 +29,8 @@ TRUNCATE=${TRUNCATE:-truncate}
 MUNLINK=${MUNLINK:-munlink}
 SOCKETSERVER=${SOCKETSERVER:-socketserver}
 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
+IOPENTEST1=${IOPENTEST1:-iopentest1}
+IOPENTEST2=${IOPENTEST2:-iopentest2}
 
 if [ $UID -ne 0 ]; then
 	RUNAS_ID="$UID"
@@ -1542,6 +1546,22 @@ test_54d() {
 }
 run_test 54d "fifo device works in lustre"
 
+test_55() {
+        for i in `ls $TMP|grep -E 'mds|ost'` ; do
+                rm -rf $DIR/d55
+                mkdir $DIR/d55
+                mount -o loop,iopen $TMP/$i $DIR/d55
+                touch $DIR/d55/foo
+                $IOPENTEST1 $DIR/d55/foo $DIR/d55
+                $IOPENTEST2 $DIR/d55
+                echo "check for $TMP/$i. Please wait..."
+                sleep 6
+                rm -rf $DIR/d55/*
+                umount $DIR/d55
+        done
+}
+run_test 55 "check iopen_connect_dentry()======================="
+
 test_59() {
 	echo "touch 130 files"
 	for i in `seq 1 130` ; do
-- 
GitLab