• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *      diotest4.c
23  *
24  * DESCRIPTION
25  *	The program generates error conditions and verifies the error
26  *	code generated with the expected error value. The program also
27  *	tests some of the boundary condtions. The size of test file created
28  *	is filesize_in_blocks * 4k.
29  *	Test blocks:
30  *	[1] Negative Offset
31  *	[2] Negative count - removed 08/01/2003 - robbiew
32  *	[3] Odd count of read and write
33  *	[4] Read beyond the file size
34  *	[5] Invalid file descriptor
35  *	[6] Out of range file descriptor
36  *	[7] Closed file descriptor
37  *	[8] Directory read, write - removed 10/7/2002 - plars
38  *	[9] Character device (/dev/null) read, write
39  *	[10] read, write to a mmaped file
40  *	[11] read, write to an unmaped file with munmap
41  *	[12] read from file not open for reading
42  *	[13] write to file not open for writing
43  *	[14] read, write with non-aligned buffer
44  *	[15] read, write buffer in read-only space
45  *	[16] read, write in non-existant space
46  *	[17] read, write for file with O_SYNC
47  *
48  * USAGE
49  *      diotest4 [-b filesize_in_blocks]
50  *
51  * History
52  *	04/22/2002	Narasimha Sharoff nsharoff@us.ibm.com
53  *
54  * RESTRICTIONS
55  *	None
56 */
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include <signal.h>
62 #include <sys/file.h>
63 #include <fcntl.h>
64 #include <sys/types.h>
65 #include <sys/mman.h>
66 #include <sys/syscall.h>
67 #include <errno.h>
68 #include <sys/shm.h>
69 
70 #include "diotest_routines.h"
71 
72 #include "test.h"
73 
74 char *TCID = "diotest4";	/* Test program identifier.    */
75 int TST_TOTAL = 17;		/* Total number of test conditions */
76 
77 static long fs_type;
78 
79 #ifdef O_DIRECT
80 
81 #define BUFSIZE 	4096
82 #define TRUE	1
83 #define LEN	30
84 
85 #ifdef __GNUC__
86 #define ADDRESS_OF_MAIN __builtin_extract_return_addr(__builtin_return_address(0))
87 #else
88 #define ADDRESS_OF_MAIN main
89 #endif
90 
91 /*
92  * runtest_f: Do read, writes. Verify the error value obtained by
93  *	running read or write with the expected error value (errnum).
94 */
95 int
runtest_f(int fd,char * buf,int offset,int count,int errnum,int testnum,char * msg)96 runtest_f(int fd, char *buf, int offset, int count, int errnum, int testnum,
97 	  char *msg)
98 {
99 	int ret;
100 	int l_fail = 0;
101 
102 	if (lseek(fd, offset, SEEK_SET) < 0) {
103 		if (errno != errnum) {
104 			tst_resm(TFAIL, "lseek before read failed: %s",
105 				 strerror(errno));
106 			l_fail = TRUE;
107 		}
108 	} else {
109 		errno = 0;
110 		ret = read(fd, buf, count);
111 		if (ret >= 0 || errno != errnum) {
112 			tst_resm(TFAIL, "read allows %s. returns %d: %s",
113 				 msg, ret, strerror(errno));
114 			l_fail = TRUE;
115 		}
116 	}
117 	if (lseek(fd, offset, SEEK_SET) < 0) {
118 		if (errno != errnum) {
119 			tst_resm(TFAIL, "lseek before write failed: %s",
120 				 strerror(errno));
121 			l_fail = TRUE;
122 		}
123 	} else {
124 		errno = 0;
125 		ret = write(fd, buf, count);
126 		if (ret >= 0 || errno != errnum) {
127 			tst_resm(TFAIL, "write allows %s.returns %d: %s",
128 				 msg, ret, strerror(errno));
129 			l_fail = TRUE;
130 		}
131 	}
132 	return (l_fail);
133 }
134 
135 /*
136  * runtest_s: Do read, writes. Verify the they run successfully.
137 */
runtest_s(int fd,char * buf,int offset,int count,int testnum,char * msg)138 int runtest_s(int fd, char *buf, int offset, int count, int testnum, char *msg)
139 {
140 	int ret;
141 	int l_fail = 0;
142 
143 	if (lseek(fd, offset, SEEK_SET) < 0) {
144 		tst_resm(TFAIL, "lseek before read failed: %s",
145 			 strerror(errno));
146 		l_fail = TRUE;
147 	} else {
148 		if ((ret = read(fd, buf, count)) < 0) {
149 			tst_resm(TFAIL, "read failed for %s. returns %d: %s",
150 				 msg, ret, strerror(errno));
151 			l_fail = TRUE;
152 		}
153 	}
154 	if (lseek(fd, offset, SEEK_SET) < 0) {
155 		tst_resm(TFAIL, "lseek before write failed: %s",
156 			 strerror(errno));
157 		l_fail = TRUE;
158 	} else {
159 		if ((ret = write(fd, buf, count)) < 0) {
160 			tst_resm(TFAIL, "write failed for %s. returns %d: %s",
161 				 msg, ret, strerror(errno));
162 			l_fail = TRUE;
163 		}
164 	}
165 	return (l_fail);
166 }
167 
prg_usage(void)168 static void prg_usage(void)
169 {
170 	fprintf(stderr, "Usage: diotest4 [-b filesize_in_blocks]\n");
171 	exit(1);
172 }
173 
testcheck_end(int ret,int * failed,int * fail_count,char * msg)174 static void testcheck_end(int ret, int *failed, int *fail_count, char *msg)
175 {
176 	if (ret != 0) {
177 		*failed = TRUE;
178 		(*fail_count)++;
179 		tst_resm(TFAIL, "%s", msg);
180 	} else
181 		tst_resm(TPASS, "%s", msg);
182 }
183 
184 static void setup(void);
185 static void cleanup(void);
186 static int fd1 = -1;
187 static char filename[LEN];
188 
main(int argc,char * argv[])189 int main(int argc, char *argv[])
190 {
191 	int fblocks = 1;	/* Iterations. Default 1 */
192 	int bufsize = BUFSIZE;
193 	int count, ret;
194 	int offset;
195 	int fd, newfd;
196 	int i, l_fail = 0, fail_count = 0, total = 0;
197 	int failed = 0;
198 	int shmsz = SHMLBA;
199 	int pagemask = ~(sysconf(_SC_PAGE_SIZE) - 1);
200 	char *buf0, *buf1, *buf2;
201 	caddr_t shm_base;
202 
203 	/* Options */
204 	while ((i = getopt(argc, argv, "b:")) != -1) {
205 		switch (i) {
206 		case 'b':
207 			if ((fblocks = atoi(optarg)) <= 0) {
208 				fprintf(stderr, "fblocks must be > 0\n");
209 				prg_usage();
210 			}
211 			break;
212 		default:
213 			prg_usage();
214 		}
215 	}
216 
217 	setup();
218 
219 	/* Open file and fill, allocate for buffer */
220 	if ((fd = open(filename, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
221 		tst_brkm(TBROK, cleanup, "open failed for %s: %s",
222 			 filename, strerror(errno));
223 	}
224 	if ((buf0 = valloc(bufsize)) == NULL) {
225 		tst_brkm(TBROK, cleanup, "valloc() buf0 failed: %s",
226 			 strerror(errno));
227 	}
228 	for (i = 1; i < fblocks; i++) {
229 		fillbuf(buf0, bufsize, (char)i);
230 		if (write(fd, buf0, bufsize) < 0) {
231 			tst_brkm(TBROK, cleanup, "write failed for %s: %s",
232 				 filename, strerror(errno));
233 		}
234 	}
235 	close(fd);
236 	if ((buf2 = valloc(bufsize)) == NULL) {
237 		tst_brkm(TBROK, cleanup, "valloc() buf2 failed: %s",
238 			 strerror(errno));
239 	}
240 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
241 		tst_brkm(TBROK, cleanup, "open failed for %s: %s",
242 			 filename, strerror(errno));
243 	}
244 
245 	/* Test-1: Negative Offset */
246 	offset = -1;
247 	count = bufsize;
248 	errno = 0;
249 	ret = lseek(fd, offset, SEEK_SET);
250 	if ((ret >= 0) || (errno != EINVAL)) {
251 		tst_resm(TFAIL, "lseek allows negative offset. returns %d:%s",
252 			 ret, strerror(errno));
253 		failed = TRUE;
254 		fail_count++;
255 	} else
256 		tst_resm(TPASS, "Negative Offset");
257 	total++;
258 
259 	/* Test-2: Removed */
260 	tst_resm(TPASS, "removed");
261 
262 	/* Test-3: Odd count of read and write */
263 	offset = 0;
264 	count = 1;
265 	lseek(fd, 0, SEEK_SET);
266 	if (write(fd, buf2, 4096) == -1) {
267 		tst_resm(TFAIL, "can't write to file %d", ret);
268 	}
269 	switch (fs_type) {
270 	case TST_NFS_MAGIC:
271 	case TST_BTRFS_MAGIC:
272 		tst_resm(TCONF, "%s supports odd count IO",
273 			 tst_fs_type_name(fs_type));
274 	break;
275 	default:
276 		ret = runtest_f(fd, buf2, offset, count, EINVAL, 3, "odd count");
277 		testcheck_end(ret, &failed, &fail_count,
278 					"Odd count of read and write");
279 	}
280 
281 	total++;
282 
283 	/* Test-4: Read beyond the file size */
284 	offset = bufsize * (fblocks + 10);
285 	count = bufsize;
286 	if (lseek(fd, offset, SEEK_SET) < 0) {
287 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
288 		failed = TRUE;
289 		fail_count++;
290 		tst_resm(TFAIL, "Read beyond the file size");
291 	} else {
292 		errno = 0;
293 		ret = read(fd, buf2, count);
294 		if (ret > 0 || (ret < 0 && errno != EINVAL)) {
295 			tst_resm(TFAIL,
296 				 "allows read beyond file size. returns %d: %s",
297 				 ret, strerror(errno));
298 			failed = TRUE;
299 			fail_count++;
300 		} else
301 			tst_resm(TPASS, "Read beyond the file size");
302 	}
303 	total++;
304 
305 	/* Test-5: Invalid file descriptor */
306 	offset = 4096;
307 	count = bufsize;
308 	newfd = -1;
309 	ret = runtest_f(newfd, buf2, offset, count, EBADF, 5, "negative fd");
310 	testcheck_end(ret, &failed, &fail_count, "Invalid file descriptor");
311 	total++;
312 
313 	/* Test-6: Out of range file descriptor */
314 	count = bufsize;
315 	offset = 4096;
316 	if ((newfd = getdtablesize()) < 0) {
317 		tst_resm(TFAIL, "getdtablesize() failed: %s", strerror(errno));
318 		failed = TRUE;
319 		tst_resm(TFAIL, "Out of range file descriptor");
320 	} else {
321 		ret = runtest_f(newfd, buf2, offset, count, EBADF, 6,
322 			      "out of range fd");
323 		testcheck_end(ret, &failed, &fail_count,
324 					"Out of range file descriptor");
325 	}
326 	close(newfd);
327 	total++;
328 
329 	/* Test-7: Closed file descriptor */
330 	offset = 4096;
331 	count = bufsize;
332 	if (close(fd) < 0) {
333 		tst_brkm(TBROK, cleanup, "can't close fd %d: %s", fd,
334 			 strerror(errno));
335 	}
336 	ret = runtest_f(fd, buf2, offset, count, EBADF, 7, "closed fd");
337 	testcheck_end(ret, &failed, &fail_count, "Closed file descriptor");
338 	total++;
339 
340 	/* Test-9: removed */
341 	tst_resm(TPASS, "removed");
342 
343 	/* Test-9: Character device (/dev/null) read, write */
344 	offset = 0;
345 	count = bufsize;
346 	if ((newfd = open("/dev/null", O_DIRECT | O_RDWR)) < 0) {
347 		tst_resm(TCONF, "Direct I/O on /dev/null is not supported");
348 	} else {
349 		ret = runtest_s(newfd, buf2, offset, count, 9, "/dev/null");
350 		testcheck_end(ret, &failed, &fail_count,
351 					"character device read, write");
352 	}
353 	close(newfd);
354 	total++;
355 
356 	/* Test-10: read, write to a mmaped file */
357 	shm_base = (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1));
358 	if (shm_base == NULL) {
359 		tst_brkm(TBROK, cleanup, "sbrk failed: %s", strerror(errno));
360 	}
361 	offset = 4096;
362 	count = bufsize;
363 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
364 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
365 			 filename, strerror(errno));
366 	}
367 	shm_base = mmap(shm_base, 0x100000, PROT_READ | PROT_WRITE,
368 			MAP_SHARED | MAP_FIXED, fd, 0);
369 	if (shm_base == (caddr_t) - 1) {
370 		tst_brkm(TBROK, cleanup, "can't mmap file: %s",
371 			 strerror(errno));
372 	}
373 	ret = runtest_s(fd, buf2, offset, count, 10, "mmapped file");
374 	testcheck_end(ret, &failed, &fail_count,
375 				"read, write to a mmaped file");
376 	total++;
377 
378 	/* Test-11: read, write to an unmaped file with munmap */
379 	if ((ret = munmap(shm_base, 0x100000)) < 0) {
380 		tst_brkm(TBROK, cleanup, "can't unmap file: %s",
381 			 strerror(errno));
382 	}
383 	ret = runtest_s(fd, buf2, offset, count, 11, "unmapped file");
384 	testcheck_end(ret, &failed, &fail_count,
385 				"read, write to an unmapped file");
386 	close(fd);
387 	total++;
388 
389 	/* Test-12: read from file not open for reading */
390 	offset = 4096;
391 	count = bufsize;
392 	if ((fd = open(filename, O_DIRECT | O_WRONLY)) < 0) {
393 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
394 			 filename, strerror(errno));
395 	}
396 	if (lseek(fd, offset, SEEK_SET) < 0) {
397 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
398 		failed = TRUE;
399 		fail_count++;
400 	} else {
401 		errno = 0;
402 		ret = read(fd, buf2, count);
403 		if (ret >= 0 || errno != EBADF) {
404 			tst_resm(TFAIL,
405 				 "allows read on file not open for reading. returns %d: %s",
406 				 ret, strerror(errno));
407 			failed = TRUE;
408 			fail_count++;
409 		} else
410 			tst_resm(TPASS, "read from file not open for reading");
411 	}
412 	close(fd);
413 	total++;
414 
415 	/* Test-13: write to file not open for writing */
416 	offset = 4096;
417 	count = bufsize;
418 	if ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) {
419 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
420 			 filename, strerror(errno));
421 	}
422 	if (lseek(fd, offset, SEEK_SET) < 0) {
423 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
424 		failed = TRUE;
425 		fail_count++;
426 	} else {
427 		errno = 0;
428 		ret = write(fd, buf2, count);
429 		if (ret >= 0 || errno != EBADF) {
430 			tst_resm(TFAIL,
431 				 "allows write on file not open for writing. returns %d: %s",
432 				 ret, strerror(errno));
433 			failed = TRUE;
434 			fail_count++;
435 		} else
436 			tst_resm(TPASS, "write to file not open for writing");
437 	}
438 	close(fd);
439 	total++;
440 
441 	/* Test-14: read, write with non-aligned buffer */
442 	offset = 4096;
443 	count = bufsize;
444 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
445 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
446 			 filename, strerror(errno));
447 	}
448 	switch (fs_type) {
449 	case TST_NFS_MAGIC:
450 	case TST_BTRFS_MAGIC:
451 		tst_resm(TCONF, "%s supports non-aligned buffer",
452 			 tst_fs_type_name(fs_type));
453 	break;
454 	default:
455 		ret = runtest_f(fd, buf2 + 1, offset, count, EINVAL, 14,
456 					" nonaligned buf");
457 		testcheck_end(ret, &failed, &fail_count,
458 				"read, write with non-aligned buffer");
459 	}
460 	close(fd);
461 	total++;
462 
463 	/* Test-15: read, write buffer in read-only space */
464 	offset = 4096;
465 	count = bufsize;
466 	l_fail = 0;
467 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
468 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
469 			 filename, strerror(errno));
470 	}
471 	if (lseek(fd, offset, SEEK_SET) < 0) {
472 		tst_resm(TFAIL, "lseek before read failed: %s",
473 			 strerror(errno));
474 		l_fail = TRUE;
475 	} else {
476 		errno = 0;
477 		ret = read(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
478 			 count);
479 		if (ret >= 0 || errno != EFAULT) {
480 			tst_resm(TFAIL,
481 				 "read to read-only space. returns %d: %s", ret,
482 				 strerror(errno));
483 			l_fail = TRUE;
484 		}
485 	}
486 	if (lseek(fd, offset, SEEK_SET) < 0) {
487 		tst_resm(TFAIL, "lseek before write failed: %s",
488 			 strerror(errno));
489 		l_fail = TRUE;
490 	} else {
491 		ret = write(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
492 			  count);
493 		if (ret < 0) {
494 			tst_resm(TFAIL,
495 				 "write to read-only space. returns %d: %s",
496 				 ret, strerror(errno));
497 			l_fail = TRUE;
498 		}
499 	}
500 	testcheck_end(l_fail, &failed, &fail_count,
501 				"read, write buffer in read-only space");
502 	close(fd);
503 	total++;
504 
505 	/* Test-16: read, write in non-existant space */
506 	offset = 4096;
507 	count = bufsize;
508 	if ((buf1 =
509 	     (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1))) == NULL) {
510 		tst_brkm(TBROK | TERRNO, cleanup, "sbrk failed");
511 	}
512 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
513 		tst_brkm(TBROK | TERRNO, cleanup,
514 			 "open(%s, O_DIRECT|O_RDWR) failed", filename);
515 	}
516 	ret = runtest_f(fd, buf1, offset, count, EFAULT, 16,
517 		      " nonexistant space");
518 	testcheck_end(ret, &failed, &fail_count,
519 				"read, write in non-existant space");
520 	total++;
521 	close(fd);
522 
523 	/* Test-17: read, write for file with O_SYNC */
524 	offset = 4096;
525 	count = bufsize;
526 	if ((fd = open(filename, O_DIRECT | O_RDWR | O_SYNC)) < 0) {
527 		tst_brkm(TBROK, cleanup,
528 			 "open(%s, O_DIRECT|O_RDWR|O_SYNC failed)", filename);
529 	}
530 	ret = runtest_s(fd, buf2, offset, count, 17, "opened with O_SYNC");
531 	testcheck_end(ret, &failed, &fail_count,
532 				"read, write for file with O_SYNC");
533 	total++;
534 	close(fd);
535 
536 	unlink(filename);
537 	if (failed)
538 		tst_resm(TINFO, "%d/%d test blocks failed", fail_count, total);
539 	else
540 		tst_resm(TINFO, "%d testblocks completed", total);
541 	cleanup();
542 
543 	tst_exit();
544 }
545 
setup(void)546 static void setup(void)
547 {
548 	struct sigaction act;
549 
550 	tst_tmpdir();
551 
552 	act.sa_handler = SIG_IGN;
553 	act.sa_flags = 0;
554 	sigemptyset(&act.sa_mask);
555 	(void)sigaction(SIGXFSZ, &act, NULL);
556 	sprintf(filename, "testdata-4.%ld", syscall(__NR_gettid));
557 
558 	if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
559 		tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
560 			 filename, strerror(errno));
561 	}
562 	close(fd1);
563 
564 	/* Test for filesystem support of O_DIRECT */
565 	if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
566 		tst_brkm(TCONF, cleanup,
567 			 "O_DIRECT is not supported by this filesystem. %s",
568 			 strerror(errno));
569 	}
570 	close(fd1);
571 
572 	fs_type = tst_fs_type(cleanup, ".");
573 }
574 
cleanup(void)575 static void cleanup(void)
576 {
577 	if (fd1 != -1)
578 		unlink(filename);
579 
580 	tst_rmdir();
581 
582 }
583 
584 #else /* O_DIRECT */
585 
main()586 int main()
587 {
588 	tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
589 }
590 #endif /* O_DIRECT */
591