Skip to content
Browse files

Added mmapwrite.c and readmmap.c because needed for mmap test script.

  • Loading branch information...
1 parent 7ac95df commit cc8e73b892c4c90fe090d1b27578f1be925b2a20 root committed Nov 25, 2010
View
239 ZFS-Test_Suite/bin/file_trunc.c
@@ -0,0 +1,239 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+#pragma ident "@(#)file_trunc.c 1.2 07/05/25 SMI"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include <sys/errno.h>
+#include <sys/time.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
+#include <sys/param.h>
+#include <string.h>
+
+#define FSIZE 256*1024*1024
+#define BSIZE 512
+
+/* Initialize Globals */
+static long fsize = FSIZE;
+static size_t bsize = BSIZE;
+static int count = 0;
+static int rflag = 0;
+static int seed = 0;
+static int vflag = 0;
+static int errflag = 0;
+static off_t offset = 0;
+static char *filename = NULL;
+
+static void usage(char *execname);
+static void parse_options(int argc, char *argv[]);
+static void do_write(int fd);
+static void do_trunc(int fd);
+
+static void
+usage(char *execname)
+{
+ (void) fprintf(stderr,
+ "usage: %s [-b blocksize] [-c count] [-f filesize]"
+ " [-o offset] [-s seed] [-r] [-v] filename\n", execname);
+ (void) exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i = 0;
+ int fd = -1;
+
+ parse_options(argc, argv);
+
+ fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
+ if (fd < 0) {
+ perror("open");
+ exit(3);
+ }
+
+ while (i < count) {
+ (void) do_write(fd);
+ (void) do_trunc(fd);
+
+ i++;
+ }
+
+ (void) close(fd);
+ return (0);
+}
+
+static void
+parse_options(int argc, char *argv[])
+{
+ int c;
+
+ extern char *optarg;
+ extern int optind, optopt;
+
+ count = fsize / bsize;
+ seed = time(NULL);
+ while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {
+ switch (c) {
+ case 'b':
+ bsize = atoi(optarg);
+ break;
+
+ case 'c':
+ count = atoi(optarg);
+ break;
+
+ case 'f':
+ fsize = atoi(optarg);
+ break;
+
+ case 'o':
+ offset = atoi(optarg);
+ break;
+
+ case 'r':
+ rflag++;
+ break;
+
+ case 's':
+ seed = atoi(optarg);
+ break;
+
+ case 'v':
+ vflag++;
+ break;
+
+ case ':':
+ (void) fprintf(stderr,
+ "Option -%c requires an operand\n", optopt);
+ errflag++;
+ break;
+
+ case '?':
+ (void) fprintf(stderr,
+ "Unrecognized option: -%c\n", optopt);
+ errflag++;
+ break;
+ }
+
+ if (errflag) {
+ (void) usage(argv[0]);
+ }
+ }
+ if (argc <= optind) {
+ (void) fprintf(stderr,
+ "No filename specified\n");
+ usage(argv[0]);
+ }
+ filename = argv[optind];
+
+ if (vflag) {
+ (void) fprintf(stderr, "Seed = %d\n", seed);
+ }
+ srandom(seed);
+}
+
+static void
+do_write(int fd)
+{
+ off_t roffset = 0;
+ char *buf = NULL;
+ char *rbuf = NULL;
+
+ buf = (char *)calloc(1, bsize);
+ rbuf = (char *)calloc(1, bsize);
+ if (buf == NULL || rbuf == NULL) {
+ perror("malloc");
+ exit(4);
+ }
+
+ roffset = random() % fsize;
+ if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
+ perror("lseek");
+ exit(5);
+ }
+
+ buf = "ZFS Test Suite Truncation Test";
+ if (write(fd, buf, bsize) < bsize) {
+ perror("write");
+ exit(6);
+ }
+
+ if (rflag) {
+ if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
+ perror("lseek");
+ exit(7);
+ }
+
+ if (read(fd, rbuf, bsize) < bsize) {
+ perror("read");
+ exit(8);
+ }
+
+ if (memcmp(buf, rbuf, bsize) != 0) {
+ perror("memcmp");
+ exit(9);
+ }
+ }
+ if (vflag) {
+ (void) fprintf(stderr,
+ "Wrote to offset %ld\n", (offset + roffset));
+ if (rflag) {
+ (void) fprintf(stderr,
+ "Read back from offset %ld\n", (offset + roffset));
+ }
+ }
+
+ (void) free(buf);
+ (void) free(rbuf);
+}
+
+static void
+do_trunc(int fd)
+{
+ off_t roffset = 0;
+
+ roffset = random() % fsize;
+ if (ftruncate64(fd, (offset + roffset)) < 0) {
+ perror("truncate");
+ exit(7);
+ }
+
+ if (vflag) {
+ (void) fprintf(stderr,
+ "Truncated at offset %ld\n",
+ (offset + roffset));
+ }
+}
View
233 ZFS-Test_Suite/bin/file_write.c
@@ -0,0 +1,233 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "@(#)file_write.c 1.4 07/10/09 SMI"
+
+#include "file_common.h"
+#include <libgen.h>
+
+static unsigned char bigbuffer[BIGBUFFERSIZE];
+
+/*
+ * Writes (or appends) a given value to a file repeatedly.
+ * See header file for defaults.
+ */
+
+static void usage(void);
+
+int
+main(int argc, char **argv)
+{
+ int bigfd;
+ int c;
+ int oflag = 0;
+ int err = 0;
+ int k;
+ long i;
+ int64_t good_writes = 0;
+ uchar_t nxtfillchar;
+ /*
+ * Default Parameters
+ */
+ int write_count = BIGFILESIZE;
+ uchar_t fillchar = DATA;
+ int block_size = BLOCKSZ;
+ char *filename = NULL;
+ char *operation = NULL;
+ offset_t noffset, offset = 0;
+ int verbose = 0;
+ int rsync = 0;
+ int wsync = 0;
+
+ /*
+ * Process Arguments
+ */
+ while ((c = getopt(argc, argv, "b:c:d:s:f:o:vwr")) != -1) {
+ switch (c) {
+ case 'b':
+ block_size = atoi(optarg);
+ break;
+ case 'c':
+ write_count = atoi(optarg);
+ break;
+ case 'd':
+ fillchar = atoi(optarg);
+ break;
+ case 's':
+ offset = atoll(optarg);
+ break;
+ case 'f':
+ filename = optarg;
+ break;
+ case 'o':
+ operation = optarg;
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ case 'w':
+ wsync = 1;
+ break;
+ case 'r':
+ rsync = 1;
+ break;
+ case '?':
+ (void) printf("unknown arg %c\n", optopt);
+ usage();
+ break;
+ }
+ }
+
+ /*
+ * Validate Parameters
+ */
+ if (!filename) {
+ (void) printf("Filename not specified (-f <file>)\n");
+ err++;
+ }
+
+ if (!operation) {
+ (void) printf("Operation not specified (-o <operation>).\n");
+ err++;
+ }
+
+ if (block_size > BIGBUFFERSIZE) {
+ (void) printf("block_size is too large max==%d.\n",
+ BIGBUFFERSIZE);
+ err++;
+ }
+
+ if (err) usage();
+
+ /*
+ * Prepare the buffer and determine the requested operation
+ */
+ nxtfillchar = fillchar;
+ k = 0;
+
+ for (i = 0; i < block_size; i++) {
+ bigbuffer[i] = nxtfillchar;
+
+ if (fillchar == 0) {
+ if ((k % DATA_RANGE) == 0) {
+ k = 0;
+ }
+ nxtfillchar = k++;
+ }
+ }
+
+ /*
+ * using the strncmp of operation will make the operation match the
+ * first shortest match - as the operations are unique from the first
+ * character this means that we match single character operations
+ */
+ if ((strncmp(operation, "create", strlen(operation) + 1)) == 0 ||
+ (strncmp(operation, "overwrite", strlen(operation) + 1)) == 0) {
+ oflag = (O_RDWR|O_CREAT);
+ } else if ((strncmp(operation, "append", strlen(operation) + 1)) == 0) {
+ oflag = (O_RDWR|O_APPEND);
+ } else {
+ (void) printf("valid operations are <create|append> not '%s'\n",
+ operation);
+ usage();
+ }
+
+ if (rsync) {
+ oflag = oflag | O_RSYNC;
+ }
+
+ if (wsync) {
+ oflag = oflag | O_SYNC;
+ }
+
+ /*
+ * Given an operation (create/overwrite/append), open the file
+ * accordingly and perform a write of the appropriate type.
+ */
+ if ((bigfd = open(filename, oflag, 0666)) == -1) {
+ (void) printf("open %s: failed [%s]%d. Aborting!\n", filename,
+ strerror(errno), errno);
+ exit(errno);
+ }
+ noffset = llseek(bigfd, offset, SEEK_SET);
+ if (noffset != offset) {
+ (void) printf("llseek %s (%lld/%lld) failed [%s]%d.Aborting!\n",
+ filename, offset, noffset, strerror(errno), errno);
+ exit(errno);
+ }
+
+ if (verbose) {
+ (void) printf("%s: block_size = %d, write_count = %d, "
+ "offset = %lld, data = %s%d\n", filename, block_size,
+ write_count, offset,
+ (fillchar == 0) ? "0->" : "",
+ (fillchar == 0) ? DATA_RANGE : fillchar);
+ }
+
+ for (i = 0; i < write_count; i++) {
+ ssize_t n;
+
+ if ((n = write(bigfd, &bigbuffer, block_size)) == -1) {
+ (void) printf("write failed (%ld), good_writes = %lld, "
+ "error: %s[%d]\n", (long)n, good_writes,
+ strerror(errno),
+ errno);
+ exit(errno);
+ }
+ good_writes++;
+ }
+
+ if (verbose) {
+ (void) printf("Success: good_writes = %lld (%lld)\n",
+ good_writes, (good_writes * block_size));
+ }
+
+ return (0);
+}
+
+static void
+usage(void)
+{
+ char *base = (char *)"file_write";
+ char *exec = (char *)getexecname();
+
+ if (exec != NULL)
+ exec = strdup(exec);
+ if (exec != NULL)
+ base = basename(exec);
+
+ (void) printf("Usage: %s [-v] -o {create,overwrite,append} -f file_name"
+ " [-b block_size]\n"
+ "\t[-s offset] [-c write_count] [-d data]\n"
+ "\twhere [data] equal to zero causes chars "
+ "0->%d to be repeated throughout\n", base, DATA_RANGE);
+
+ if (exec) {
+ free(exec);
+ }
+
+ exit(1);
+}
View
BIN ZFS-Test_Suite/bin/mmapwrite
Binary file not shown.
View
97 ZFS-Test_Suite/bin/mmapwrite.c
@@ -0,0 +1,97 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "@(#)mmapwrite.c 1.4 07/05/25 SMI"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <pthread.h>
+
+/*
+ * --------------------------------------------------------------------
+ * Bug Id: 5032643
+ *
+ * Simply writing to a file and mmaping that file at the same time can
+ * result in deadlock. Nothing perverse like writing from the file's
+ * own mapping is required.
+ * --------------------------------------------------------------------
+ */
+
+static void *
+mapper(void *fdp)
+{
+ void *addr;
+ int fd = *(int *)fdp;
+
+ if ((addr =
+ mmap(0, 8192, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+ for (;;) {
+ if (mmap(addr, 8192, PROT_READ,
+ MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+ }
+ /* NOTREACHED */
+ return ((void *)1);
+}
+
+int
+main(int argc, char **argv)
+{
+ int fd;
+ char buf[BUFSIZ];
+ pthread_t thread;
+ if (argc != 2) {
+ (void) printf("usage: %s <file name>\n", argv[0]);
+ exit(1);
+ }
+
+ if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1) {
+ perror("open");
+ exit(1);
+ }
+
+ if (pthread_create(&thread, NULL, mapper, &fd) != 0) {
+ perror("pthread_create");
+ exit(1);
+ }
+ for (;;) {
+ if (write(fd, buf, sizeof (buf)) == -1) {
+ perror("write");
+ exit(1);
+ }
+ }
+
+ /* NOTREACHED */
+ return (0);
+}
View
BIN ZFS-Test_Suite/bin/readmmap
Binary file not shown.
View
139 ZFS-Test_Suite/bin/readmmap.c
@@ -0,0 +1,139 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "@(#)readmmap.c 1.4 07/05/25 SMI"
+
+/*
+ * --------------------------------------------------------------
+ * BugId 5047993 : Getting bad read data.
+ *
+ * Usage: readmmap <filename>
+ *
+ * where:
+ * filename is an absolute path to the file name.
+ *
+ * Return values:
+ * 1 : error
+ * 0 : no errors
+ * --------------------------------------------------------------
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/mman.h>
+
+int
+main(int argc, char **argv)
+{
+ char *filename = "badfile";
+ size_t size = 4395;
+ size_t idx = 0;
+ char *buf = NULL;
+ char *map = NULL;
+ int fd = -1, bytes, retval = 0;
+ unsigned seed;
+
+ if (argc < 2 || optind == argc) {
+ (void) fprintf(stderr,
+ "usage: %s <file name>\n", argv[0]);
+ exit(1);
+ }
+
+ if ((buf = calloc(1, size)) == NULL) {
+ perror("calloc");
+ exit(1);
+ }
+
+ filename = argv[optind];
+
+ (void) remove(filename);
+
+ fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
+ if (fd == -1) {
+ perror("open to create");
+ retval = 1;
+ goto end;
+ }
+
+ bytes = write(fd, buf, size);
+ if (bytes != size) {
+ (void) printf("short write: %d != %ud\n", bytes, size);
+ retval = 1;
+ goto end;
+ }
+
+ map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ if (map == MAP_FAILED) {
+ perror("mmap");
+ retval = 1;
+ goto end;
+ }
+ seed = time(NULL);
+ srandom(seed);
+
+ idx = random() % size;
+ map[idx] = 1;
+
+ if (msync(map, size, MS_SYNC) != 0) {
+ perror("msync");
+ retval = 1;
+ goto end;
+ }
+
+ if (munmap(map, size) != 0) {
+ perror("munmap");
+ retval = 1;
+ goto end;
+ }
+
+ bytes = pread(fd, buf, size, 0);
+ if (bytes != size) {
+ (void) printf("short read: %d != %ud\n", bytes, size);
+ retval = 1;
+ goto end;
+ }
+
+ if (buf[idx] != 1) {
+ (void) printf(
+ "bad data from read! got buf[%ud]=%d, expected 1\n",
+ idx, buf[idx]);
+ retval = 1;
+ goto end;
+ }
+
+ (void) printf("good data from read: buf[%ud]=1\n", idx);
+end:
+ if (fd != -1) {
+ (void) close(fd);
+ }
+ if (buf != NULL) {
+ free(buf);
+ }
+
+ return (retval);
+}

0 comments on commit cc8e73b

Please sign in to comment.
Something went wrong with that request. Please try again.