• 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/syscall.h>
66 #include <errno.h>
67 
68 #include "diotest_routines.h"
69 
70 #include "test.h"
71 #include "safe_macros.h"
72 #include "lapi/mmap.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 = MMAP_GRANULARITY;
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 	case TST_FUSE_MAGIC:
273 		tst_resm(TCONF, "%s supports odd count IO",
274 			 tst_fs_type_name(fs_type));
275 	break;
276 	default:
277 		ret = runtest_f(fd, buf2, offset, count, EINVAL, 3, "odd count");
278 		testcheck_end(ret, &failed, &fail_count,
279 					"Odd count of read and write");
280 	}
281 
282 	total++;
283 
284 	/* Test-4: Read beyond the file size */
285 	offset = bufsize * (fblocks + 10);
286 	count = bufsize;
287 	if (lseek(fd, offset, SEEK_SET) < 0) {
288 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
289 		failed = TRUE;
290 		fail_count++;
291 		tst_resm(TFAIL, "Read beyond the file size");
292 	} else {
293 		errno = 0;
294 		ret = read(fd, buf2, count);
295 		if (ret > 0 || (ret < 0 && errno != EINVAL)) {
296 			tst_resm(TFAIL,
297 				 "allows read beyond file size. returns %d: %s",
298 				 ret, strerror(errno));
299 			failed = TRUE;
300 			fail_count++;
301 		} else
302 			tst_resm(TPASS, "Read beyond the file size");
303 	}
304 	total++;
305 
306 	/* Test-5: Invalid file descriptor */
307 	offset = 4096;
308 	count = bufsize;
309 	newfd = -1;
310 	ret = runtest_f(newfd, buf2, offset, count, EBADF, 5, "negative fd");
311 	testcheck_end(ret, &failed, &fail_count, "Invalid file descriptor");
312 	total++;
313 
314 	/* Test-6: Out of range file descriptor */
315 	count = bufsize;
316 	offset = 4096;
317 	if ((newfd = getdtablesize()) < 0) {
318 		tst_resm(TFAIL, "getdtablesize() failed: %s", strerror(errno));
319 		failed = TRUE;
320 		tst_resm(TFAIL, "Out of range file descriptor");
321 	} else {
322 		ret = runtest_f(newfd, buf2, offset, count, EBADF, 6,
323 			      "out of range fd");
324 		testcheck_end(ret, &failed, &fail_count,
325 					"Out of range file descriptor");
326 	}
327 	close(newfd);
328 	total++;
329 
330 	/* Test-7: Closed file descriptor */
331 	offset = 4096;
332 	count = bufsize;
333 	SAFE_CLOSE(cleanup, fd);
334 	ret = runtest_f(fd, buf2, offset, count, EBADF, 7, "closed fd");
335 	testcheck_end(ret, &failed, &fail_count, "Closed file descriptor");
336 	total++;
337 
338 	/* Test-9: removed */
339 	tst_resm(TPASS, "removed");
340 
341 	/* Test-9: Character device (/dev/null) read, write */
342 	offset = 0;
343 	count = bufsize;
344 	if ((newfd = open("/dev/null", O_DIRECT | O_RDWR)) < 0) {
345 		tst_resm(TCONF, "Direct I/O on /dev/null is not supported");
346 	} else {
347 		ret = runtest_s(newfd, buf2, offset, count, 9, "/dev/null");
348 		testcheck_end(ret, &failed, &fail_count,
349 					"character device read, write");
350 	}
351 	close(newfd);
352 	total++;
353 
354 	/* Test-10: read, write to a mmaped file */
355 	offset = 4096;
356 	count = bufsize;
357 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
358 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
359 			 filename, strerror(errno));
360 	}
361 	shm_base = mmap(0, 0x100000, PROT_READ | PROT_WRITE,
362 			MAP_SHARED, fd, 0);
363 	if (shm_base == (caddr_t) - 1) {
364 		tst_brkm(TBROK, cleanup, "can't mmap file: %s",
365 			 strerror(errno));
366 	}
367 	ret = runtest_s(fd, buf2, offset, count, 10, "mmapped file");
368 	testcheck_end(ret, &failed, &fail_count,
369 				"read, write to a mmaped file");
370 	total++;
371 
372 	/* Test-11: read, write to an unmaped file with munmap */
373 	if ((ret = munmap(shm_base, 0x100000)) < 0) {
374 		tst_brkm(TBROK, cleanup, "can't unmap file: %s",
375 			 strerror(errno));
376 	}
377 	ret = runtest_s(fd, buf2, offset, count, 11, "unmapped file");
378 	testcheck_end(ret, &failed, &fail_count,
379 				"read, write to an unmapped file");
380 	close(fd);
381 	total++;
382 
383 	/* Test-12: read from file not open for reading */
384 	offset = 4096;
385 	count = bufsize;
386 	if ((fd = open(filename, O_DIRECT | O_WRONLY)) < 0) {
387 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
388 			 filename, strerror(errno));
389 	}
390 	if (lseek(fd, offset, SEEK_SET) < 0) {
391 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
392 		failed = TRUE;
393 		fail_count++;
394 	} else {
395 		errno = 0;
396 		ret = read(fd, buf2, count);
397 		if (ret >= 0 || errno != EBADF) {
398 			tst_resm(TFAIL,
399 				 "allows read on file not open for reading. returns %d: %s",
400 				 ret, strerror(errno));
401 			failed = TRUE;
402 			fail_count++;
403 		} else
404 			tst_resm(TPASS, "read from file not open for reading");
405 	}
406 	close(fd);
407 	total++;
408 
409 	/* Test-13: write to file not open for writing */
410 	offset = 4096;
411 	count = bufsize;
412 	if ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) {
413 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
414 			 filename, strerror(errno));
415 	}
416 	if (lseek(fd, offset, SEEK_SET) < 0) {
417 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
418 		failed = TRUE;
419 		fail_count++;
420 	} else {
421 		errno = 0;
422 		ret = write(fd, buf2, count);
423 		if (ret >= 0 || errno != EBADF) {
424 			tst_resm(TFAIL,
425 				 "allows write on file not open for writing. returns %d: %s",
426 				 ret, strerror(errno));
427 			failed = TRUE;
428 			fail_count++;
429 		} else
430 			tst_resm(TPASS, "write to file not open for writing");
431 	}
432 	close(fd);
433 	total++;
434 
435 	/* Test-14: read, write with non-aligned buffer */
436 	offset = 4096;
437 	count = bufsize;
438 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
439 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
440 			 filename, strerror(errno));
441 	}
442 	switch (fs_type) {
443 	case TST_NFS_MAGIC:
444 	case TST_BTRFS_MAGIC:
445 	case TST_FUSE_MAGIC:
446 		tst_resm(TCONF, "%s supports non-aligned buffer",
447 			 tst_fs_type_name(fs_type));
448 	break;
449 	default:
450 		ret = runtest_f(fd, buf2 + 1, offset, count, EINVAL, 14,
451 					" nonaligned buf");
452 		testcheck_end(ret, &failed, &fail_count,
453 				"read, write with non-aligned buffer");
454 	}
455 	close(fd);
456 	total++;
457 
458 	/* Test-15: read, write buffer in read-only space */
459 	offset = 4096;
460 	count = bufsize;
461 	l_fail = 0;
462 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
463 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
464 			 filename, strerror(errno));
465 	}
466 	if (lseek(fd, offset, SEEK_SET) < 0) {
467 		tst_resm(TFAIL, "lseek before read failed: %s",
468 			 strerror(errno));
469 		l_fail = TRUE;
470 	} else {
471 		errno = 0;
472 		ret = read(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
473 			 count);
474 		if (ret >= 0 || errno != EFAULT) {
475 			tst_resm(TFAIL,
476 				 "read to read-only space. returns %d: %s", ret,
477 				 strerror(errno));
478 			l_fail = TRUE;
479 		}
480 	}
481 	if (lseek(fd, offset, SEEK_SET) < 0) {
482 		tst_resm(TFAIL, "lseek before write failed: %s",
483 			 strerror(errno));
484 		l_fail = TRUE;
485 	} else {
486 		ret = write(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
487 			  count);
488 		if (ret < 0) {
489 			tst_resm(TFAIL,
490 				 "write to read-only space. returns %d: %s",
491 				 ret, strerror(errno));
492 			l_fail = TRUE;
493 		}
494 	}
495 	testcheck_end(l_fail, &failed, &fail_count,
496 				"read, write buffer in read-only space");
497 	close(fd);
498 	total++;
499 
500 	/* Test-16: read, write in non-existant space */
501 	offset = 4096;
502 	count = bufsize;
503 	if ((buf1 =
504 	     (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1))) == NULL) {
505 		tst_brkm(TBROK | TERRNO, cleanup, "sbrk failed");
506 	}
507 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
508 		tst_brkm(TBROK | TERRNO, cleanup,
509 			 "open(%s, O_DIRECT|O_RDWR) failed", filename);
510 	}
511 	ret = runtest_f(fd, buf1, offset, count, EFAULT, 16,
512 		      " nonexistant space");
513 	testcheck_end(ret, &failed, &fail_count,
514 				"read, write in non-existant space");
515 	total++;
516 	close(fd);
517 
518 	/* Test-17: read, write for file with O_SYNC */
519 	offset = 4096;
520 	count = bufsize;
521 	if ((fd = open(filename, O_DIRECT | O_RDWR | O_SYNC)) < 0) {
522 		tst_brkm(TBROK, cleanup,
523 			 "open(%s, O_DIRECT|O_RDWR|O_SYNC failed)", filename);
524 	}
525 	ret = runtest_s(fd, buf2, offset, count, 17, "opened with O_SYNC");
526 	testcheck_end(ret, &failed, &fail_count,
527 				"read, write for file with O_SYNC");
528 	total++;
529 	close(fd);
530 
531 	unlink(filename);
532 	if (failed)
533 		tst_resm(TINFO, "%d/%d test blocks failed", fail_count, total);
534 	else
535 		tst_resm(TINFO, "%d testblocks completed", total);
536 	cleanup();
537 
538 	tst_exit();
539 }
540 
setup(void)541 static void setup(void)
542 {
543 	struct sigaction act;
544 
545 	tst_tmpdir();
546 
547 	act.sa_handler = SIG_IGN;
548 	act.sa_flags = 0;
549 	sigemptyset(&act.sa_mask);
550 	(void)sigaction(SIGXFSZ, &act, NULL);
551 	sprintf(filename, "testdata-4.%ld", syscall(__NR_gettid));
552 
553 	if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
554 		tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
555 			 filename, strerror(errno));
556 	}
557 	close(fd1);
558 
559 	/* Test for filesystem support of O_DIRECT */
560 	if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
561 		tst_brkm(TCONF, cleanup,
562 			 "O_DIRECT is not supported by this filesystem. %s",
563 			 strerror(errno));
564 	}
565 	close(fd1);
566 
567 	fs_type = tst_fs_type(cleanup, ".");
568 }
569 
cleanup(void)570 static void cleanup(void)
571 {
572 	if (fd1 != -1)
573 		unlink(filename);
574 
575 	tst_rmdir();
576 
577 }
578 
579 #else /* O_DIRECT */
580 
main()581 int main()
582 {
583 	tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
584 }
585 #endif /* O_DIRECT */
586