Home | History | Annotate | Download | only in direct_io
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2002
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  *      diotest5.c
     23  *
     24  * DESCRIPTION
     25  *	The programs test buffered and direct IO with vector arrays using
     26  *	readv() and writev() calls.
     27  *	Test blocks
     28  *	[1] Direct readv, Buffered writev
     29  *	[2] Direct writev, Buffered readv
     30  *	[3] Direct readv, Direct writev
     31  *	The bufsize should be in n*4k size for direct readv, writev. The offset
     32  *	value marks the starting position in file from where to start the
     33  *	write and read. (Using larger offset, larger files can be tested).
     34  *	The nvector gives vector array size.  Test data file can be
     35  * 	specified through commandline and is useful for running test with
     36  * 	raw devices as a file.
     37  *
     38  * USAGE
     39  *      diotest5 [-b bufsize] [-o offset] [-i iterations]
     40  *			[-v nvector] [-f filename]
     41  *
     42  * History
     43  *	04/29/2002	Narasimha Sharoff nsharoff (at) us.ibm.com
     44  *
     45  * RESTRICTIONS
     46  *	None
     47 */
     48 
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <unistd.h>
     52 #include <string.h>
     53 #include <sys/file.h>
     54 #include <sys/fcntl.h>
     55 #include <sys/syscall.h>
     56 #include <sys/uio.h>
     57 #include <errno.h>
     58 
     59 #include "diotest_routines.h"
     60 
     61 #include "test.h"
     62 
     63 char *TCID = "diotest05";	/* Test program identifier.    */
     64 int TST_TOTAL = 3;		/* Total number of test conditions */
     65 
     66 #ifdef O_DIRECT
     67 
     68 #define	BUFSIZE	4096
     69 #define TRUE 1
     70 #define LEN 30
     71 #define	READ_DIRECT 1
     72 #define	WRITE_DIRECT 2
     73 #define	RDWR_DIRECT 3
     74 
     75 static int bufsize = BUFSIZE;	/* Buffer size. Default 4k */
     76 static int iter = 20;		/* Iterations. Default 20 */
     77 static int nvector = 20;	/* Vector array. Default 20 */
     78 static off64_t offset = 0;	/* Start offset. Default 0 */
     79 static char filename[LEN];	/* Test data file */
     80 static int fd1 = -1;
     81 /*
     82  * runtest: Write the data in vector array to the file. Read the data
     83  *	from the file into another vectory array and verify. Repeat the test.
     84 */
     85 int runtest(int fd_r, int fd_w, int iter, off64_t offset, int action)
     86 {
     87 	int i, bufsize = BUFSIZE;
     88 	struct iovec *iov1, *iov2, *iovp;
     89 
     90 	/* Allocate for buffers and data pointers */
     91 	if ((iov1 =
     92 	     (struct iovec *)valloc(sizeof(struct iovec) * nvector)) == NULL) {
     93 		tst_resm(TFAIL, "valloc() buf1 failed: %s", strerror(errno));
     94 		return (-1);
     95 	}
     96 	if ((iov2 =
     97 	     (struct iovec *)valloc(sizeof(struct iovec) * nvector)) == NULL) {
     98 		tst_resm(TFAIL, "valloc buf2 failed: %s", strerror(errno));
     99 		return (-1);
    100 	}
    101 	for (i = 0, iovp = iov1; i < nvector; iovp++, i++) {
    102 		if ((iovp->iov_base = valloc(bufsize)) == NULL) {
    103 			tst_resm(TFAIL, "valloc for iovp->iov_base: %s",
    104 				 strerror(errno));
    105 			return (-1);
    106 		}
    107 		iovp->iov_len = bufsize;
    108 	}
    109 	for (i = 0, iovp = iov2; i < nvector; iovp++, i++) {
    110 		if ((iovp->iov_base = valloc(bufsize)) == NULL) {
    111 			tst_resm(TFAIL, "valloc, iov2 for iovp->iov_base: %s",
    112 				 strerror(errno));
    113 			return (-1);
    114 		}
    115 		iovp->iov_len = bufsize;
    116 	}
    117 
    118 	/* Test */
    119 	for (i = 0; i < iter; i++) {
    120 		vfillbuf(iov1, nvector, i);
    121 		vfillbuf(iov2, nvector, i + 1);
    122 		if (lseek(fd_w, offset, SEEK_SET) < 0) {
    123 			tst_resm(TFAIL, "lseek before writev failed: %s",
    124 				 strerror(errno));
    125 			return (-1);
    126 		}
    127 		if (writev(fd_w, iov1, nvector) < 0) {
    128 			tst_resm(TFAIL, "writev failed: %s", strerror(errno));
    129 			return (-1);
    130 		}
    131 		if (lseek(fd_r, offset, SEEK_SET) < 0) {
    132 			tst_resm(TFAIL, "lseek before readv failed: %s",
    133 				 strerror(errno));
    134 			return (-1);
    135 		}
    136 		if (readv(fd_r, iov2, nvector) < 0) {
    137 			tst_resm(TFAIL, "readv failed: %s", strerror(errno));
    138 			return (-1);
    139 		}
    140 		if (vbufcmp(iov1, iov2, nvector) != 0) {
    141 			tst_resm(TFAIL, "readv/writev comparision failed");
    142 			return (-1);
    143 		}
    144 	}
    145 
    146 	/* Cleanup */
    147 	for (i = 0, iovp = iov1; i < nvector; iovp++, i++) {
    148 		free(iovp->iov_base);
    149 	}
    150 	for (i = 0, iovp = iov2; i < nvector; iovp++, i++) {
    151 		free(iovp->iov_base);
    152 	}
    153 	free(iov1);
    154 	free(iov2);
    155 	return 0;
    156 }
    157 
    158 /*
    159  * prg_usage: Display the program usage
    160 */
    161 void prg_usage()
    162 {
    163 	fprintf(stderr,
    164 		"Usage: diotest5 [-b bufsize] [-o offset] [ -i iteration] [ -v nvector] [-f filename]\n");
    165 	exit(1);
    166 }
    167 
    168 static void setup(void);
    169 static void cleanup(void);
    170 
    171 int main(int argc, char *argv[])
    172 {
    173 	int i, action, fd_r, fd_w;
    174 	int fail_count = 0, total = 0, failed = 0;
    175 
    176 	/* Options */
    177 	sprintf(filename, "testdata-5.%ld", syscall(__NR_gettid));
    178 	while ((i = getopt(argc, argv, "b:o:i:v:f:")) != -1) {
    179 		switch (i) {
    180 		case 'b':
    181 			if ((bufsize = atoi(optarg)) <= 0) {
    182 				fprintf(stderr, "bufsize must be > 0");
    183 				prg_usage();
    184 			}
    185 			if (bufsize % 4096 != 0) {
    186 				fprintf(stderr, "bufsize must be > 0");
    187 				prg_usage();
    188 			}
    189 			break;
    190 		case 'o':
    191 			if ((offset = atoll(optarg)) <= 0) {
    192 				fprintf(stderr, "offset must be > 0");
    193 				prg_usage();
    194 			}
    195 			break;
    196 		case 'i':
    197 			if ((iter = atoi(optarg)) <= 0) {
    198 				fprintf(stderr, "iterations must be > 0");
    199 				prg_usage();
    200 			}
    201 			break;
    202 		case 'v':
    203 			if ((nvector = atoi(optarg)) <= 0) {
    204 				fprintf(stderr, "vector array must be > 0");
    205 				prg_usage();
    206 			}
    207 			break;
    208 		case 'f':
    209 			strcpy(filename, optarg);
    210 			break;
    211 		default:
    212 			prg_usage();
    213 		}
    214 	}
    215 
    216 	setup();
    217 
    218 	/* Testblock-1: Read with Direct IO, Write without */
    219 	action = READ_DIRECT;
    220 	if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) {
    221 		tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
    222 			 filename, strerror(errno));
    223 	}
    224 	if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) {
    225 		tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
    226 			 filename, strerror(errno));
    227 	}
    228 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    229 		failed = TRUE;
    230 		fail_count++;
    231 		tst_resm(TFAIL, "Read with Direct IO, Write without");
    232 	} else
    233 		tst_resm(TPASS, "Read with Direct IO, Write without");
    234 
    235 	unlink(filename);
    236 	close(fd_r);
    237 	close(fd_w);
    238 	total++;
    239 
    240 	/* Testblock-2: Write with Direct IO, Read without */
    241 	action = WRITE_DIRECT;
    242 	if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
    243 		tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
    244 			 filename, strerror(errno));
    245 	}
    246 	if ((fd_r = open64(filename, O_RDONLY | O_CREAT, 0666)) < 0) {
    247 		tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
    248 			 filename, strerror(errno));
    249 	}
    250 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    251 		failed = TRUE;
    252 		fail_count++;
    253 		tst_resm(TFAIL, "Write with Direct IO, Read without");
    254 	} else
    255 		tst_resm(TPASS, "Write with Direct IO, Read without");
    256 	unlink(filename);
    257 	close(fd_r);
    258 	close(fd_w);
    259 	total++;
    260 
    261 	/* Testblock-3: Read, Write with Direct IO */
    262 	action = RDWR_DIRECT;
    263 	if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
    264 		tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
    265 			 filename, strerror(errno));
    266 	}
    267 	if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) {
    268 		tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
    269 			 filename, strerror(errno));
    270 	}
    271 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    272 		failed = TRUE;
    273 		fail_count++;
    274 		tst_resm(TFAIL, "Read, Write with Direct IO");
    275 	} else
    276 		tst_resm(TPASS, "Read, Write with Direct IO");
    277 	unlink(filename);
    278 	close(fd_r);
    279 	close(fd_w);
    280 	total++;
    281 
    282 	if (failed)
    283 		tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total);
    284 	else
    285 		tst_resm(TINFO,
    286 			 "%d testblocks %d iterations with %d vector array completed",
    287 			 total, iter, nvector);
    288 
    289 	cleanup();
    290 
    291 	tst_exit();
    292 }
    293 
    294 static void setup(void)
    295 {
    296 	tst_tmpdir();
    297 
    298 	if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
    299 		tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
    300 			 filename, strerror(errno));
    301 	}
    302 	close(fd1);
    303 
    304 	/* Test for filesystem support of O_DIRECT */
    305 	if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
    306 		tst_brkm(TCONF, cleanup,
    307 			 "O_DIRECT is not supported by this filesystem. %s",
    308 			 strerror(errno));
    309 	}
    310 	close(fd1);
    311 }
    312 
    313 static void cleanup(void)
    314 {
    315 	if (fd1 != -1)
    316 		unlink(filename);
    317 
    318 	tst_rmdir();
    319 
    320 }
    321 #else /* O_DIRECT */
    322 
    323 int main()
    324 {
    325 
    326 	tst_resm(TCONF, "O_DIRECT is not defined.");
    327 	return 0;
    328 }
    329 #endif /* O_DIRECT */
    330