[Ocfs2-tools-devel] [Ocfs2-test-devel] [PATCH 3/6] New set of inline-data testing tools:Add single-node testing binary for directory
tristan.ye
tristan.ye at oracle.com
Sat Aug 30 03:22:37 PDT 2008
Still have sent this patch out before,
just enhance the orignal inline-dirs.c become work-load tunable,to
perform a variety of test,such as stress test. also add concurrent r/w
and multiple directories cases.
Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
Index: ocfs2-test/programs/inline-data/single-inline-dirs.c
===================================================================
--- ocfs2-test/programs/inline-data/single-inline-dirs.c (revision 0)
+++ ocfs2-test/programs/inline-data/single-inline-dirs.c (revision 0)
@@ -0,0 +1,1227 @@
+/*
+ * Verify inline directory data.
+ *
+ * All tests read back the entire directory to verify correctness.
+ *
+ * XXX: This could easily be turned into an mpi program, where a
+ * second node does the verification step.
+ */
+
+#define _XOPEN_SOURCE 600
+#define _GNU_SOURCE
+
+#include <asm/types.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <sys/shm.h>
+#include <sys/sem.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include <ocfs2/ocfs2.h>
+
+#include <dirent.h>
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#include <signal.h>
+#include <sys/wait.h>
+#include <inttypes.h>
+#include <linux/types.h>
+
+
+#define OCFS2_MAX_FILENAME_LEN 255
+
+/*
+ * OCFS2_DIR_PAD defines the directory entries boundaries
+ *
+ * NOTE: It must be a multiple of 4
+ */
+#define OCFS2_DIR_PAD 4
+#define OCFS2_DIR_ROUND (OCFS2_DIR_PAD - 1)
+#define OCFS2_DIR_MEMBER_LEN offsetof(struct ocfs2_dir_entry, name)
+#define OCFS2_DIR_REC_LEN(name_len) (((name_len) + OCFS2_DIR_MEMBER_LEN
+ \
+ OCFS2_DIR_ROUND) & \
+ ~OCFS2_DIR_ROUND)
+
+#define FILE_BUFFERED_RW_FLAGS (O_CREAT|O_RDWR|O_TRUNC)
+#define FILE_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IROTH|\
+ S_IWOTH|S_IXOTH|S_IRGRP|S_IWGRP|
S_IXGRP)
+
+/*
+ * Quick reference table of namelen boundaries and their respective
reclens.
+ *
+ * (name: 1 rec: 16) (name: 5 rec: 20) (name: 9 rec: 24)
+ * (name: 13 rec: 28) (name: 17 rec: 32) (name: 21 rec: 36)
+ * (name: 25 rec: 40) (name: 29 rec: 44) (name: 33 rec: 48)
+ * (name: 37 rec: 52) (name: 41 rec: 56) (name: 45 rec: 60)
+ * (name: 49 rec: 64) (name: 53 rec: 68) (name: 57 rec: 72)
+ * (name: 61 rec: 76) (name: 65 rec: 80) (name: 69 rec: 84)
+ * (name: 73 rec: 88) (name: 77 rec: 92) (name: 81 rec: 96)
+ * (name: 85 rec: 100) (name: 89 rec: 104) (name: 93 rec: 108)
+ * (name: 97 rec: 112) (name: 101 rec: 116) (name: 105 rec:
120)
+ * (name: 109 rec: 124) (name: 113 rec: 128) (name: 117 rec:
132)
+ * (name: 121 rec: 136) (name: 125 rec: 140) (name: 129 rec:
144)
+ * (name: 133 rec: 148) (name: 137 rec: 152) (name: 141 rec:
156)
+ * (name: 145 rec: 160) (name: 149 rec: 164) (name: 153 rec:
168)
+ * (name: 157 rec: 172) (name: 161 rec: 176) (name: 165 rec:
180)
+ * (name: 169 rec: 184) (name: 173 rec: 188) (name: 177 rec:
192)
+ * (name: 181 rec: 196) (name: 185 rec: 200) (name: 189 rec:
204)
+ * (name: 193 rec: 208) (name: 197 rec: 212) (name: 201 rec:
216)
+ * (name: 205 rec: 220) (name: 209 rec: 224) (name: 213 rec:
228)
+ * (name: 217 rec: 232) (name: 221 rec: 236) (name: 225 rec:
240)
+ * (name: 229 rec: 244) (name: 233 rec: 248) (name: 237 rec:
252)
+ * (name: 241 rec: 256) (name: 245 rec: 260) (name: 249 rec:
264)
+ * (name: 253 rec: 268)
+ */
+
+/*
+ * OCFS2 directory file types. Only the low 3 bits are used. The
+ * other bits are reserved for now.
+ */
+#define OCFS2_FT_UNKNOWN 0
+#define OCFS2_FT_REG_FILE 1
+#define OCFS2_FT_DIR 2
+#define OCFS2_FT_CHRDEV 3
+#define OCFS2_FT_BLKDEV 4
+#define OCFS2_FT_FIFO 5
+#define OCFS2_FT_SOCK 6
+#define OCFS2_FT_SYMLINK 7
+
+#define OCFS2_FT_MAX 8
+
+
+#define S_SHIFT 12
+
+struct my_dirent {
+ unsigned int type;
+ unsigned int name_len;
+ unsigned int seen;
+ char name[OCFS2_MAX_FILENAME_LEN];
+};
+
+union semun {
+ int val; /* value for SETVAL */
+ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
+ unsigned short int *array; /* array for GETALL, SETALL */
+ struct seminfo *__buf; /* buffer for IPC_INFO */
+};
+
+#define WORK_PLACE "single-inline-data-test"
+static char *prog;
+static char device[100];
+
+static ocfs2_filesys *fs = NULL;
+static struct ocfs2_super_block *ocfs2_sb = NULL;
+
+static unsigned long page_size;
+static unsigned int blocksize = 4096;
+static unsigned long clustersize;
+
+unsigned int id_count;
+unsigned long i_size;
+
+static char mount_point[255];
+static char work_place[255];
+static char dirent_name[255];
+static char dir_name[255];
+
+static int iteration = 1;
+static int do_multi_process_test = 0;
+static int do_multi_file_test = 0;
+static unsigned long child_nums = 2;
+static unsigned long file_nums = 2;
+static unsigned int operated_entries = 20;
+
+pid_t *child_pid_list = NULL;
+
+int shm_id;
+int sem_id;
+
+#define MAX_DIRENTS 1024
+struct my_dirent *dirents = NULL;
+static unsigned int num_dirents = 0;
+
+static unsigned int max_inline_size;
+unsigned int usable_space;
+
+static char path[PATH_MAX];
+static char path1[PATH_MAX];
+
+static int testno = 1;
+
+static unsigned long get_rand(unsigned long min, unsigned long max)
+{
+ if (min == 0 && max == 0)
+ return 0;
+
+ return min + (rand() % (max - min + 1));
+}
+
+static inline char rand_char(void)
+{
+ return 'A' + (char) get_rand(0, 25);
+}
+
+static void usage(void)
+{
+ printf("Usage: inline-dirs [-i <iteration>] [-s operated_entries] "
+ "[-c <concurrent_process_num>] [-m <multi_file_num>] "
+ "<-d <device>> <mount_point>\n"
+ "Run a series of tests intended to verify I/O to and
from\n"
+ "dirs with inline data.\n\n"
+ "iteration specify the running times.\n"
+ "operated_dir_entries specify the entires number to be "
+ "operated,such as random create/unlink/rename.\n"
+ "concurrent_process_num specify the number of concurrent
"
+ "multi_file_num specify the number of multiple dirs"
+ "processes to perform inline-data read/rename.\n"
+ "device and mount_point are mandatory.\n");
+ exit(1);
+
+}
+
+static int parse_opts(int argc, char **argv)
+{
+ int c;
+ while (1) {
+ c = getopt(argc, argv, "D:d:I:i:C:c:M:m:S:s:");
+ if (c == -1)
+ break;
+ switch (c) {
+ case 'i':
+ case 'I':
+ iteration = atol(optarg);
+ break;
+ case 'd':
+ case 'D':
+ strcpy(device, optarg);
+ break;
+ case 'c':
+ case 'C':
+ do_multi_process_test = 1;
+ child_nums = atol(optarg);
+ break;
+ case 'm':
+ case 'M':
+ do_multi_file_test = 1;
+ file_nums = atol(optarg);
+ break;
+ case 's':
+ case 'S':
+ operated_entries = atol(optarg);
+ default:
+ break;
+ }
+ }
+
+ if (strcmp(device, "") == 0)
+ return EINVAL;
+
+ if (argc - optind != 1)
+ return EINVAL;
+
+ strcpy(mount_point, argv[optind]);
+ if (mount_point[strlen(mount_point) - 1] == '/')
+ mount_point[strlen(mount_point) - 1] = '\0';
+
+ return 0;
+}
+
+static int is_dot_entry(struct my_dirent *dirent)
+{
+ if (dirent->name_len == 1 && dirent->name[0] == '.')
+ return 1;
+ if (dirent->name_len == 2 && dirent->name[0] == '.'
+ && dirent->name[1] == '.')
+ return 1;
+ return 0;
+}
+
+static int unlink_dirent(struct my_dirent *dirent)
+{
+ sprintf(path1, "%s/%s", dir_name, dirent->name);
+
+ return unlink(path1);
+}
+
+static void destroy_dir(void)
+{
+ int ret, i;
+ struct my_dirent *dirent;
+
+ for(i = 0; i < num_dirents; i++) {
+ dirent = &dirents[i];
+
+ if (dirent->name_len == 0)
+ continue;
+
+ if (!is_dot_entry(dirent)) {
+ ret = unlink_dirent(dirent);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "unlink failure %d: %s\n", ret,
+ strerror(ret));
+ exit(ret);
+ }
+ }
+
+ dirent->name_len = 0;
+ }
+
+ ret = rmdir(dir_name);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "rmdir failure %d: %s\n", ret,
+ strerror(ret));
+ exit(ret);
+ }
+}
+
+static struct my_dirent *find_my_dirent(char *name)
+{
+ int i, len;
+ struct my_dirent *my_dirent;
+
+ len = strlen(name);
+
+ for(i = 0; i < num_dirents; i++) {
+ my_dirent = &dirents[i];
+
+ if (my_dirent->name_len == 0)
+ continue;
+
+ if (my_dirent->name_len == len &&
+ strcmp(my_dirent->name, name) == 0)
+ return my_dirent;
+ }
+
+ return NULL;
+}
+
+static void create_and_prep_dir(void)
+{
+ int ret;
+ struct my_dirent *dirent;
+
+ memset(dirents, 0, sizeof(struct my_dirent) * MAX_DIRENTS);
+
+ dirent = &dirents[0];
+ dirent->type = S_IFDIR >> S_SHIFT;
+ dirent->name_len = 1;
+ strcpy(dirent->name, ".");
+
+ dirent = &dirents[1];
+ dirent->type = S_IFDIR >> S_SHIFT;
+ dirent->name_len = 2;
+ strcpy(dirent->name, "..");
+
+ num_dirents = 2;
+ snprintf(dirent_name, 255, "inline-data-dir-test-%d", getpid());
+ snprintf(dir_name, 255, "%s/%s", work_place, dirent_name);
+
+ ret = mkdir(dir_name, FILE_MODE);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "mkdir failure %d: %s\n", ret, strerror(ret));
+ exit(ret);
+ }
+}
+
+static void create_file(char *filename)
+{
+ int ret, fd;
+ struct my_dirent *dirent;
+
+ dirent = &dirents[num_dirents];
+ num_dirents++;
+
+ dirent->type = S_IFREG >> S_SHIFT;
+ dirent->name_len = strlen(filename);
+ dirent->seen = 0;
+ strcpy(dirent->name, filename);
+
+ sprintf(path, "%s/%s", dir_name, dirent->name);
+
+ fd = open(path, FILE_BUFFERED_RW_FLAGS, FILE_MODE);
+ if (fd == -1) {
+ ret = errno;
+ fprintf(stderr, "open failure %d: %s\n", ret, strerror(ret));
+ exit(ret);
+ }
+
+ close(fd);
+}
+
+static void create_files(char *prefix, int num)
+{
+ int i;
+
+ for (i = 0; i < num; i++) {
+ sprintf(path1, "%s%011d", prefix, i);
+ create_file(path1);
+ }
+}
+static int get_max_inlined_entries(int max_inline_size)
+{
+ unsigned int almost_full_entries;
+
+ /*
+ * This will create enough entries to leave only 280 free
+ * bytes in the directory.
+ *
+ * max_inline_size % 512 = 312 [always]
+ * rec overhead for '.' and '..' = 32
+ * So, 312 - 32 = 280.
+ *
+ */
+ almost_full_entries = max_inline_size / 512;
+ almost_full_entries *= 512;
+ almost_full_entries /= 32;
+
+ /*
+ * Now we add enough 32 byte entries to fill that remaining 280
bytes:
+ *
+ * 280 / 32 = 8
+ *
+ * And we'll be left over with 24 bytes:
+ *
+ * 280 % 32 = 24
+ *
+ * Which can easily be overflowed by adding one more 32 byte
entry.
+ */
+ almost_full_entries += 8;
+ /*if user-specified operated_entries larger than this,decrease
to a
+ right inlined number
+ */
+ if (operated_entries > almost_full_entries)
+ operated_entries = almost_full_entries - 1;
+
+ return almost_full_entries;
+
+}
+static void get_directory_almost_full(int minus_this_many)
+{
+ int almost_full_entries;
+
+ almost_full_entries = get_max_inlined_entries(max_inline_size);
+
+ almost_full_entries -= minus_this_many;
+
+ /* Need up to 20 characters to get a 32 byte entry */
+ create_files("filename-", almost_full_entries);
+}
+
+static void random_unlink(int iters)
+{
+ int i, ret;
+ struct my_dirent *dirent;
+
+ while (iters > 0) {
+ i = get_rand(0, num_dirents - 1);
+ dirent = &dirents[i];
+
+ if (is_dot_entry(dirent))
+ continue;
+ if (dirent->name_len == 0)
+ continue;
+
+ ret = unlink_dirent(dirent);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "unlink failure %d: %s\n", ret,
+ strerror(ret));
+ exit(ret);
+ }
+
+ dirent->name_len = 0;
+ iters--;
+ }
+}
+
+static void random_fill_empty_entries(int iters)
+{
+ int i, ret, fd;
+ struct my_dirent *dirent;
+
+ while (iters > 0) {
+ i = get_rand(0, num_dirents - 1);
+ dirent = &dirents[i];
+
+ if (is_dot_entry(dirent))
+ continue;
+ if (dirent->name_len > 0)
+ continue;
+
+ //sprintf(path1, "%s%011d", "filename-", i);
+ dirent->type = S_IFREG >> S_SHIFT;
+ dirent->name_len = strlen(dirent->name);
+ dirent->seen = 0;
+ //strcpy(dirent->name, path1);
+
+ sprintf(path, "%s/%s", dir_name, dirent->name);
+
+ fd = open(path, FILE_BUFFERED_RW_FLAGS, FILE_MODE);
+ if (fd == -1) {
+ ret = errno;
+ fprintf(stderr, "open failure %d: %s\n", ret, strerror(ret));
+ exit(ret);
+ }
+
+ close(fd);
+
+ iters--;
+ }
+}
+
+static void random_rename_same_reclen(int iters)
+{
+ int i, ret;
+ struct my_dirent *dirent;
+
+ while (iters > 0) {
+ i = get_rand(0, num_dirents - 1);
+ dirent = &dirents[i];
+
+ if (is_dot_entry(dirent))
+ continue;
+ if (dirent->name_len == 0)
+ continue;
+
+ /*
+ * We already renamed this one
+ */
+ /*
+ if (dirent->name[0] == 'R')
+ continue;
+ */
+
+ strcpy(path, dirent->name);
+ path[0] = 'R';
+ sprintf(path1, "%s/%s", dir_name, path);
+ sprintf(path, "%s/%s", dir_name, dirent->name);
+
+ ret = rename(path, path1);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "rename failure %d: %s\n", ret,
+ strerror(ret));
+
+ fprintf(stderr, "Failed rename from %s to %s\n",
+ path, path1);
+
+ exit(ret);
+ }
+ dirent->name[0] = 'R';
+ iters--;
+ }
+}
+
+static void random_deleting_rename(int iters)
+{
+ int i, j, ret;
+ struct my_dirent *dirent1, *dirent2;
+
+ while (iters--) {
+ i = get_rand(0, num_dirents - 1);
+ j = get_rand(0, num_dirents - 1);
+ dirent1 = &dirents[i];
+ dirent2 = &dirents[j];
+
+ if (dirent1 == dirent2)
+ continue;
+ if (is_dot_entry(dirent1) || is_dot_entry(dirent2))
+ continue;
+ if (dirent1->name_len == 0 || dirent2->name_len == 0)
+ continue;
+
+ sprintf(path, "%s/%s", dir_name, dirent1->name);
+ sprintf(path1, "%s/%s", dir_name, dirent2->name);
+
+ ret = rename(path, path1);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "rename failure %d: %s\n", ret,
+ strerror(ret));
+
+ fprintf(stderr, "Failed rename from %s to %s\n",
+ path, path1);
+
+ exit(ret);
+ }
+ dirent2->type = dirent1->type;
+ dirent1->name_len = 0;
+ }
+}
+
+static void verify_dirents(void)
+{
+ int i, ret;
+ DIR *dir;
+ struct dirent *dirent;
+ struct my_dirent *my_dirent;
+
+ dir = opendir(dir_name);
+ if (dir == NULL) {
+ ret = errno;
+ fprintf(stderr, "opendir failure %d: %s\n", ret, strerror(ret));
+ exit(ret);
+ }
+
+ dirent = readdir(dir);
+ while (dirent) {
+ my_dirent = find_my_dirent(dirent->d_name);
+ if (!my_dirent) {
+ fprintf(stderr, "Verify failure: got nonexistent "
+ "dirent: (ino %lu, reclen: %u, type: %u, "
+ "name: %s)\n",
+ dirent->d_ino, dirent->d_reclen,
+ dirent->d_type, dirent->d_name);
+ exit(1);
+ }
+
+ if (my_dirent->type != dirent->d_type) {
+ fprintf(stderr, "Verify failure: bad dirent type: "
+ "memory: (type: %u, name_len: %u, name: %s), "
+ "kernel: (ino %lu, reclen: %u, type: %u, "
+ "name: %s)\n",
+ my_dirent->type, my_dirent->name_len,
+ my_dirent->name, dirent->d_ino,
+ dirent->d_reclen, dirent->d_type,
+ dirent->d_name);
+ exit(1);
+ }
+
+ if (my_dirent->seen) {
+ fprintf(stderr, "Verify failure: duplicate dirent: "
+ "(type: %u, name_len: %u, name: %s)\n",
+ my_dirent->type, my_dirent->name_len,
+ my_dirent->name);
+ exit(1);
+ }
+
+ my_dirent->seen++;
+
+ dirent = readdir(dir);
+ }
+
+ for (i = 0; i < num_dirents; i++) {
+ my_dirent = &dirents[i];
+
+ if (my_dirent->seen != 0 || my_dirent->name_len == 0)
+ continue;
+
+ fprintf(stderr, "Verify failure: missing dirent: "
+ "(type: %u, name_len: %u, name: %s)\n", my_dirent->type,
+ my_dirent->name_len, my_dirent->name);
+ exit(1);
+ }
+ closedir(dir);
+}
+
+static int is_dir_inlined(char *dirent_name, unsigned long *i_size,
+ unsigned int *id_count)
+{
+ int ret;
+ uint64_t workplace_blk_no = 1;
+ uint64_t testdir_blk_no = 1;
+ char *buf = NULL;
+ struct ocfs2_dinode *di;
+ struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
+
+ sync();
+
+ ocfs2_malloc_block(fs->fs_io, &buf);
+
+ /*lookup worksplace inode*/
+ ret = ocfs2_lookup(fs, sb->s_root_blkno, WORK_PLACE,
+ strlen(WORK_PLACE), NULL, &workplace_blk_no);
+ if ( ret < 0 ) {
+ fprintf(stderr, "failed to lookup work_place(%s)'s"
+ " inode blkno\n", work_place);
+ ocfs2_free(&buf);
+ exit(ret);
+ }
+
+ /*lookup file inode,then read*/
+ ret = ocfs2_lookup(fs, workplace_blk_no, dirent_name,
strlen(dirent_name),
+ NULL, &testdir_blk_no);
+ if ( ret < 0 ) {
+ fprintf(stderr, "failed to lookup file(%s/%s)'s"
+ " inode blkno\n", work_place, dirent_name);
+ ocfs2_free(&buf);
+ exit(ret);
+ }
+
+ ret = ocfs2_read_inode(fs, testdir_blk_no, buf);
+ if ( ret < 0 ) {
+ fprintf(stderr, "failed to read file(%s/%s/%s)'s"
+ " inode.\n", mount_point, WORK_PLACE, dirent_name);
+ ocfs2_free(&buf);
+ exit(ret);
+ }
+
+ di = (struct ocfs2_dinode *)buf;
+ *i_size = di->i_size;
+ *id_count = ((di->id2).i_data).id_count;
+
+ if (di->i_dyn_features & OCFS2_INLINE_DATA_FL)
+ ret = 1;
+ else
+ ret = 0;
+
+ ocfs2_free(&buf);
+ return ret;
+}
+
+static void should_inlined_or_not(int is_inlined, int should_inlined,
int test_no)
+{
+ /* is_inlined represent if the ret is inlined or not
+ while should_inlined represnt if we expect it inlined or not.
+ */
+ if (should_inlined) {
+ if (!is_inlined) {
+ fprintf(stderr, "After Test #%d, dir %s should be "
+ "inlined here!\n", test_no, dir_name);
+ fprintf(stderr, "Dir(%s): i_size = %d,id_count = %d\n",
+ dir_name, i_size, id_count);
+ exit(-1);
+ }
+
+ } else {
+ if (is_inlined) {
+ fprintf(stderr, "After Test #%d, dir %s should be "
+ "extented here!\n", test_no, dir_name);
+ fprintf(stderr, "Dir(%s): i_size = %d,id_count = %d\n",
+ dir_name, i_size, id_count);
+ exit(-1);
+
+ }
+ }
+
+ return;
+}
+
+/*
+ * [I] Basic tests of inline-dir code.
+ * 1) Basic add files
+ * 2) Basic delete files
+ * 3) Basic rename files
+ * 4) Add / Remove / Re-add to fragment dir
+ */
+static void run_basic_tests(void)
+{
+ int ret;
+
+ printf("Test %d: fill directory\n", testno);
+ create_and_prep_dir();
+ get_directory_almost_full(0);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ testno++;
+
+ printf("Test %d: expand inlined dir to extent exactly\n", testno);
+ /*Should be 13 bits len dirent_name*/
+ create_file("Iam13bitshere");
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 0, testno);
+ testno++;
+
+ printf("Test %d: remove directory\n", testno);
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: rename files with same namelen\n", testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_rename_same_reclen(operated_entries);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+ testno++;
+
+
+ printf("Test %d: rename files with same namelen on top of each other
\n", testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_deleting_rename(operated_entries);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: random unlink/fill entries.\n", testno);
+ create_and_prep_dir();
+ get_directory_almost_full(0);
+ random_unlink(operated_entries);
+ random_fill_empty_entries(operated_entries);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: random rename/unlink files with same namelen\n",
+ testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_unlink(operated_entries / 2);
+ random_rename_same_reclen(operated_entries / 2);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: fragment directory with unlinks/creates/renames\n",
+ testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_unlink(operated_entries / 2);
+ random_rename_same_reclen(operated_entries / 2);
+ create_files("frag1a", operated_entries / 2);
+ random_unlink(operated_entries / 2);
+ create_files("frag1b", operated_entries / 2);
+ random_deleting_rename(operated_entries / 2);
+ random_rename_same_reclen(operated_entries / 2);
+ create_files("frag1c", operated_entries / 2);
+ verify_dirents();
+ destroy_dir();
+ testno++;
+}
+
+/*
+ * [II] Tests intended to push a dir out to extents
+ * 1) Add enough files to push out one block
+ * 2) Add enough files to push out two blocks
+ * 3) Fragment dir, add enough files to push out to extents
+ */
+static void run_large_dir_tests(void)
+{
+ int ret;
+
+ printf("Test %d: Add file name large enough to push out one block\n",
testno);
+ create_and_prep_dir();
+ get_directory_almost_full(0);
+ create_files("Pushedfn-", 1);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 0, testno);
+ /*verify i_size should be one block size here*/
+ if (i_size != blocksize) {
+ fprintf(stderr, "i_size should be %d,while it's % here!\n",
+ blocksize, i_size);
+ }
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: Add file name large enough to push out two blocks\n",
testno);
+ create_and_prep_dir();
+ get_directory_almost_full(0);
+
create_files("this_is_an_intentionally_long_filename_prefix_to_stress_the_dir_code-this_is_an_intentionally_long_filename_prefix_to_stress_the_dir_code-this_is_an_intentionally_long_filename_prefix_to_stress_the_dir_code", 1);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 0, testno);
+ /*verify i_size should be one block size here*/
+ if (i_size != blocksize * 2) {
+ fprintf(stderr, "i_size should be %d,while it's % here!\n",
+ blocksize * 2, i_size);
+ }
+ destroy_dir();
+ testno++;
+
+ printf("Test %d: fragment directory then push out to extents.\n",
+ testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_unlink(operated_entries / 2);
+ random_rename_same_reclen(operated_entries / 2);
+ create_files("frag2a", operated_entries / 2);
+ random_unlink(operated_entries / 2);
+ create_files("frag2b", operated_entries / 2);
+ random_deleting_rename(operated_entries / 2);
+ random_rename_same_reclen(operated_entries / 2);
+ create_files("frag2c", operated_entries / 2 + 1);
+ create_files("frag2d", operated_entries / 2 + 1);
+ verify_dirents();
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 0, testno);
+ destroy_dir();
+ testno++;
+}
+
+static void sigchld_handler()
+{
+ pid_t pid;
+ union wait status;
+
+ while (1) {
+ pid = wait3(&status, WNOHANG, NULL);
+ if (pid <= 0)
+ break;
+ }
+}
+
+static void sigint_handler()
+{
+ /*Kill all children*/
+ int i;
+ int process_nums;
+
+ if (do_multi_process_test)
+ process_nums = child_nums;
+ else
+ process_nums = file_nums;
+
+ for (i = 0; i < process_nums; i++)
+ kill(child_pid_list[i], SIGTERM);
+
+ free(child_pid_list);
+ signal(SIGINT, SIG_DFL);
+ kill(getpid(), SIGINT);
+
+}
+
+static void sigterm_handler()
+{
+ /*Kill all children*/
+ int i;
+ int process_nums;
+
+ if (do_multi_process_test)
+ process_nums = child_nums;
+ else
+ process_nums = file_nums;
+
+ for (i = 0; i < process_nums; i++)
+ kill(child_pid_list[i], SIGTERM);
+
+ free(child_pid_list);
+ signal(SIGTERM, SIG_DFL);
+ kill(getpid(), SIGTERM);
+
+}
+
+static int set_semvalue(void)
+{
+ union semun sem_union;
+
+ sem_union.val = 1;
+ if (semctl(sem_id, 0, SETVAL, sem_union) == -1) {
+ perror("semctl");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int semaphore_p(void)
+{
+ struct sembuf sem_b;
+
+ sem_b.sem_num = 0;
+ sem_b.sem_op = -1; /* P() */
+ sem_b.sem_flg = SEM_UNDO;
+ if (semop(sem_id, &sem_b, 1) == -1) {
+ fprintf(stderr, "semaphore_p failed\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int semaphore_v(void)
+{
+ struct sembuf sem_b;
+
+ sem_b.sem_num = 0;
+ sem_b.sem_op = 1; /* V() */
+ sem_b.sem_flg = SEM_UNDO;
+ if (semop(sem_id, &sem_b, 1) == -1) {
+ fprintf(stderr, "semaphore_v failed\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static void del_semvalue(void)
+{
+ union semun sem_union;
+
+ if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
+ fprintf(stderr, "Failed to delete semaphore\n");
+}
+
+static void run_concurrent_test(void)
+{
+ int ret, rc;
+ int i, j, status;
+ struct my_dirent *old_dirents;
+ key_t sem_key = IPC_PRIVATE, shm_key = IPC_PRIVATE;
+
+ pid_t pid;
+
+ if (!do_multi_process_test)
+ return;
+
+ printf("Test %d: concurrent dir RW with multiple processes!\n",
testno);
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+
+ /*get and init semaphore*/
+ sem_id = semget(sem_key, 1, 0766 | IPC_CREAT);
+ if (set_semvalue() < 0) {
+ fprintf(stderr,"Set semaphore value failed!\n");
+ exit(1);
+ }
+
+ /*should use shared memory here */
+ old_dirents = dirents;
+ dirents = NULL;
+
+ shm_id = shmget(shm_key, sizeof(struct my_dirent) * MAX_DIRENTS,
IPC_CREAT | 0766);
+ if (shm_id < 0) {
+ perror("shmget");
+ exit(1);
+ }
+
+ dirents = (struct my_dirent *)shmat(shm_id, 0, 0);
+ if (dirents < 0) {
+ perror("shmat");
+ exit(1);
+ }
+
+ shmctl(shm_id, IPC_RMID, 0);
+ memmove(dirents, old_dirents, sizeof(struct my_dirent) * MAX_DIRENTS);
+
+ /*flush out the father's i/o buffer*/
+ fflush(stderr);
+ fflush(stdout);
+
+ signal(SIGCHLD, sigchld_handler);
+
+ for (i = 0; i < child_nums; i++) {
+ pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "Fork process error!\n");
+ exit(pid);
+ }
+ if (pid == 0) {
+ if (semaphore_p() < 0)
+ exit(-1);
+ /*Concurrent rename for dirents*/
+ random_rename_same_reclen(operated_entries);
+ if (semaphore_v() < 0)
+ exit(-1);
+ /*child exits normally*/
+ sleep(1);
+ exit(0);
+ }
+ if (pid > 0)
+ child_pid_list[i] = pid;
+ }
+
+ signal(SIGINT, sigint_handler);
+ signal(SIGTERM, sigterm_handler);
+
+ /*father wait all children to leave*/
+ for (i = 0; i < child_nums; i ++) {
+ ret = waitpid(child_pid_list[i], &status,0);
+ rc = WEXITSTATUS(status);
+ if (rc) {
+ fprintf(stderr, "Child %d exits abnormally with "
+ "RC=%d\n", child_pid_list[i], rc);
+ exit(rc);
+ }
+ }
+ /*father help to verfiy dirents' consistency*/
+ sleep(2);
+ verify_dirents();
+
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+
+ /*detach shared memory*/
+ if (shmdt(dirents) == -1) {
+ perror("shmdt");
+ exit(1);
+ }
+
+ dirents = old_dirents;
+ testno++;
+
+ return;
+}
+
+static void run_multiple_test(void)
+{
+ int i, j, status;
+
+ pid_t pid;
+ int ret, rc;
+
+ if (!do_multi_file_test)
+ return;
+
+ printf("Test %d: multiple dirs RW with multiple processes!\n",
testno);
+
+ fflush(stderr);
+ fflush(stdout);
+
+ signal(SIGCHLD, sigchld_handler);
+
+ for (i = 0; i < file_nums; i++) {
+ pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "Fork process error!\n");
+ exit(pid);
+ }
+ if (pid == 0) {
+ create_and_prep_dir();
+ get_directory_almost_full(1);
+ random_rename_same_reclen(operated_entries);
+ random_unlink(operated_entries);
+ random_fill_empty_entries(operated_entries);
+ verify_dirents();
+ sync();
+ sleep(1);
+ ret = is_dir_inlined(dirent_name, &i_size, &id_count);
+ should_inlined_or_not(ret, 1, testno);
+ destroy_dir();
+ exit(0);
+
+ }
+ if (pid > 0)
+ child_pid_list[i] = pid;
+
+ }
+
+ signal(SIGINT, sigint_handler);
+ signal(SIGTERM, sigterm_handler);
+
+ /*father wait all children to leave*/
+ for (i = 0; i < file_nums; i ++) {
+ ret = waitpid(child_pid_list[i], &status,0);
+ rc = WEXITSTATUS(status);
+ if (rc) {
+ fprintf(stderr, "Child %d exits abnormally with "
+ "RC=%d\n", child_pid_list[i], rc);
+ exit(rc);
+ }
+ }
+
+ testno++;
+ return;
+}
+
+static int open_ocfs2_volume(char *device_name)
+{
+ int open_flags = OCFS2_FLAG_HEARTBEAT_DEV_OK | OCFS2_FLAG_RO;
+ int ret;
+
+ ret = ocfs2_open(device_name, open_flags, 0, 0, &fs);
+ if (ret < 0) {
+ fprintf(stderr,"Not a ocfs2 volume!\n");
+ return ret;
+ }
+
+ ocfs2_sb = OCFS2_RAW_SB(fs->fs_super);
+ if (!(ocfs2_sb->s_feature_incompat &
OCFS2_FEATURE_INCOMPAT_INLINE_DATA)) {
+ fprintf(stderr,"Inline-data not supported"
+ " on this ocfs2 volume\n");
+ return -1;
+ }
+
+ blocksize = 1 << ocfs2_sb->s_blocksize_bits;
+ clustersize = 1 << ocfs2_sb->s_clustersize_bits;
+ max_inline_size = ocfs2_max_inline_data(blocksize);
+
+ return 0;
+}
+
+static void setup(int argc, char *argv[])
+{
+ int ret;
+
+ prog = strrchr(argv[0], '/');
+ if (prog == NULL)
+ prog = argv[0];
+ else
+ prog++;
+
+ if (parse_opts(argc, argv))
+ usage();
+
+ ret = open_ocfs2_volume(device);
+ if (ret < 0) {
+ fprintf(stderr,"Open_ocfs2_volume failed!\n");
+ exit(ret);
+ }
+
+ if (do_multi_process_test)
+ child_pid_list = (pid_t *)malloc(sizeof(pid_t) * child_nums);
+ if (do_multi_file_test)
+ child_pid_list = (pid_t *)malloc(sizeof(pid_t) * file_nums);
+
+ dirents = (struct my_dirent *)malloc(sizeof(struct my_dirent) *
MAX_DIRENTS);
+ memset(dirents, 0, sizeof(struct my_dirent) * MAX_DIRENTS);
+
+ srand(getpid());
+ page_size = sysconf(_SC_PAGESIZE);
+
+ snprintf(work_place, 255, "%s/%s",mount_point,WORK_PLACE);
+ mkdir(work_place, FILE_MODE);
+
+ printf("BlockSize:\t\t%d\nMax Inline Data Size:\t%d\n"
+ "ClusterSize:\t\t%d\nPageSize:\t\t%d\nWorkingPlace:\t\t%s\n"
+ "NumOfMaxInlinedEntries:\t\t%d\n\n", blocksize,
max_inline_size,
+ clustersize, page_size, work_place,
+ get_max_inlined_entries(max_inline_size));
+
+ return ;
+
+}
+
+static void teardown(void)
+{
+ if (dirents)
+ free(dirents);
+
+ if (child_pid_list)
+ free(child_pid_list);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+
+ setup(argc,argv);
+
+ for (i = 0; i < iteration; i++){
+
+ printf("################Test Round %d################\n", i);
+ testno = 1;
+ run_basic_tests();
+ run_large_dir_tests();
+ run_multiple_test();
+ run_concurrent_test();
+ printf("All File I/O Tests Passed\n");
+
+ }
+
+ teardown();
+
+ return 0;
+}
More information about the Ocfs2-tools-devel
mailing list