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  *      diotest2.c
     23  *
     24  * DESCRIPTION
     25  *	Program tests the combinations of direct and buffered read, write.
     26  *	The bufsize should be in n*4k size for direct read, write. The offset
     27  *	value marks the starting position in file from where to start the
     28  *	read and write. Larger files can be created using the offset parameter.
     29  *	Test data file can be specified through commandline and is useful
     30  *	for running test with raw devices as a file.
     31  *	Test blocks:
     32  *	[1] Direct read, Buffered write
     33  *	[2] Direct write, Buffered read
     34  *	[3] Direct read, Direct write
     35  *
     36  * USAGE
     37  *      diotest2 [-b bufsize] [-o offset] [-i iterations] [-f filename]
     38  *
     39  * History
     40  *	04/22/2002	Narasimha Sharoff nsharoff (at) us.ibm.com
     41  *
     42  * RESTRICTIONS
     43  *	None
     44 */
     45 
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <unistd.h>
     49 #include <fcntl.h>
     50 #include <string.h>
     51 #include <sys/file.h>
     52 #include <sys/types.h>
     53 #include <sys/syscall.h>
     54 #include <errno.h>
     55 
     56 #include "diotest_routines.h"
     57 
     58 #include "test.h"
     59 
     60 char *TCID = "diotest02";	/* Test program identifier.    */
     61 int TST_TOTAL = 3;		/* Total number of test conditions */
     62 
     63 #ifdef O_DIRECT
     64 
     65 #define	BUFSIZE	4096
     66 #define TRUE	1
     67 #define LEN	30
     68 #define READ_DIRECT 1
     69 #define WRITE_DIRECT 2
     70 #define RDWR_DIRECT 3
     71 
     72 int fd1 = -1;
     73 char filename[LEN];
     74 int bufsize = BUFSIZE;
     75 
     76 /*
     77  * runtest: write the data to the file. Read the data from the file and compare.
     78  *	For each iteration, write data starting at offse+iter*bufsize
     79  *	location in the file and read from there.
     80 */
     81 int runtest(int fd_r, int fd_w, int iter, off64_t offset, int action)
     82 {
     83 	char *buf1;
     84 	char *buf2;
     85 	int i;
     86 
     87 	/* Allocate for buffers */
     88 	if ((buf1 = valloc(bufsize)) == 0) {
     89 		tst_resm(TFAIL, "valloc() buf1 failed: %s", strerror(errno));
     90 		return (-1);
     91 	}
     92 	if ((buf2 = valloc(bufsize)) == 0) {
     93 		tst_resm(TFAIL, "valloc() buf2 failed: %s", strerror(errno));
     94 		return (-1);
     95 	}
     96 
     97 	/* seek bufsize*iteration and write. seek and read. verify. */
     98 	for (i = 0; i < iter; i++) {
     99 		fillbuf(buf1, bufsize, i);
    100 		if (lseek(fd_w, offset + iter * bufsize, SEEK_SET) < 0) {
    101 			tst_resm(TFAIL, "lseek before write failed: %s",
    102 				 strerror(errno));
    103 			return (-1);
    104 		}
    105 		if (write(fd_w, buf1, bufsize) < bufsize) {
    106 			tst_resm(TFAIL, "write failed: %s", strerror(errno));
    107 			return (-1);
    108 		}
    109 		if (lseek(fd_r, offset + iter * bufsize, SEEK_SET) < 0) {
    110 			tst_resm(TFAIL, "lseek before read failed: %s",
    111 				 strerror(errno));
    112 			return (-1);
    113 		}
    114 		if (read(fd_r, buf2, bufsize) < bufsize) {
    115 			tst_resm(TFAIL, "read failed: %s", strerror(errno));
    116 			return (-1);
    117 		}
    118 		if (bufcmp(buf1, buf2, bufsize) != 0) {
    119 			tst_resm(TFAIL, "read/write comparision failed");
    120 			return (-1);
    121 		}
    122 	}
    123 	return 0;
    124 }
    125 
    126 static void prg_usage(void)
    127 {
    128 	fprintf(stderr,
    129 		"Usage: diotest2 [-b bufsize] [-o offset] [-i iterations] [-f filename]\n");
    130 	exit(1);
    131 }
    132 
    133 static void setup(void);
    134 static void cleanup(void);
    135 
    136 int main(int argc, char *argv[])
    137 {
    138 	int iter = 100;		/* Iterations. Default 100 */
    139 	off64_t offset = 0;	/* Offset. Default 0 */
    140 	int i, action, fd_r, fd_w;
    141 	int fail_count = 0, total = 0, failed = 0;
    142 
    143 	/* Options */
    144 	sprintf(filename, "testdata-2.%ld", syscall(__NR_gettid));
    145 	while ((i = getopt(argc, argv, "b:o:i:f:")) != -1) {
    146 		switch (i) {
    147 		case 'b':
    148 			if ((bufsize = atoi(optarg)) <= 0) {
    149 				fprintf(stderr, "bufsize must be > 0\n");
    150 				prg_usage();
    151 			}
    152 			if (bufsize % 4096 != 0) {
    153 				fprintf(stderr,
    154 					"bufsize must be multiple of 4k\n");
    155 				prg_usage();
    156 			}
    157 			break;
    158 		case 'o':
    159 			if ((offset = atoi(optarg)) <= 0) {
    160 				fprintf(stderr, "offset must be > 0\n");
    161 				prg_usage();
    162 			}
    163 			break;
    164 		case 'i':
    165 			if ((iter = atoi(optarg)) <= 0) {
    166 				fprintf(stderr, "iterations must be > 0\n");
    167 				prg_usage();
    168 			}
    169 			break;
    170 		case 'f':
    171 			strcpy(filename, optarg);
    172 			break;
    173 		default:
    174 			prg_usage();
    175 		}
    176 	}
    177 
    178 	setup();
    179 
    180 	/* Testblock-1: Read with Direct IO, Write without */
    181 	action = READ_DIRECT;
    182 	if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0)
    183 		tst_brkm(TBROK | TERRNO, cleanup,
    184 			 "open(%s, O_WRONLY..) failed", filename);
    185 	if ((fd_r = open(filename, O_DIRECT | O_RDONLY, 0666)) < 0)
    186 		tst_brkm(TBROK | TERRNO, cleanup,
    187 			 "open(%s, O_DIRECT|O_RDONLY..) failed", filename);
    188 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    189 		failed = TRUE;
    190 		fail_count++;
    191 		tst_resm(TFAIL, "Read with Direct IO, Write without");
    192 	} else
    193 		tst_resm(TPASS, "Read with Direct IO, Write without");
    194 	close(fd_w);
    195 	close(fd_r);
    196 	unlink(filename);
    197 	total++;
    198 
    199 	/* Testblock-2: Write with Direct IO, Read without */
    200 	action = WRITE_DIRECT;
    201 	if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) == -1)
    202 		tst_brkm(TBROK | TERRNO, cleanup,
    203 			 "open(%s, O_DIRECT|O_WRONLY..) failed", filename);
    204 	if ((fd_r = open(filename, O_RDONLY | O_CREAT, 0666)) == -1)
    205 		tst_brkm(TBROK | TERRNO, cleanup,
    206 			 "open(%s, O_RDONLY..) failed", filename);
    207 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    208 		failed = TRUE;
    209 		fail_count++;
    210 		tst_resm(TFAIL, "Write with Direct IO, Read without");
    211 	} else
    212 		tst_resm(TPASS, "Write with Direct IO, Read without");
    213 	close(fd_w);
    214 	close(fd_r);
    215 	unlink(filename);
    216 	total++;
    217 
    218 	/* Testblock-3: Read, Write with Direct IO. */
    219 	action = RDWR_DIRECT;
    220 	if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) == -1)
    221 		tst_brkm(TBROK | TERRNO, cleanup,
    222 			 "open(%s, O_DIRECT|O_WRONLY|O_CREAT, ..) failed",
    223 			 filename);
    224 	if ((fd_r = open(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) == -1)
    225 		tst_brkm(TBROK | TERRNO, cleanup,
    226 			 "open(%s, O_DIRECT|O_RDONLY|O_CREAT, ..) failed",
    227 			 filename);
    228 	if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
    229 		failed = TRUE;
    230 		fail_count++;
    231 		tst_resm(TFAIL, "Read, Write with Direct IO");
    232 	} else
    233 		tst_resm(TPASS, "Read, Write with Direct IO");
    234 	close(fd_w);
    235 	close(fd_r);
    236 	unlink(filename);
    237 	total++;
    238 
    239 	if (failed)
    240 		tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total);
    241 	else
    242 		tst_resm(TINFO, "%d testblocks %d iterations completed",
    243 			 total, iter);
    244 	cleanup();
    245 
    246 	tst_exit();
    247 
    248 }
    249 
    250 static void setup(void)
    251 {
    252 	tst_tmpdir();
    253 
    254 	if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) == -1)
    255 		tst_brkm(TBROK | TERRNO, cleanup,
    256 			 "open(%s, O_CREAT|O_EXCL, ..) failed", filename);
    257 	close(fd1);
    258 
    259 	/* Test for filesystem support of O_DIRECT */
    260 	if ((fd1 = open(filename, O_DIRECT, 0600)) == -1)
    261 		tst_brkm(TCONF | TERRNO, cleanup,
    262 			 "open(%s, O_DIRECT, ..) failed", filename);
    263 	close(fd1);
    264 
    265 }
    266 
    267 static void cleanup(void)
    268 {
    269 	if (fd1 != -1)
    270 		unlink(filename);
    271 
    272 	tst_rmdir();
    273 
    274 }
    275 
    276 #else /* O_DIRECT */
    277 int main()
    278 {
    279 	tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
    280 }
    281 #endif /* O_DIRECT */
    282