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 <sys/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 
168 /*
169  * prg_usage - Display the program usage
170 */
prg_usage()171 void prg_usage()
172 {
173 	fprintf(stderr, "Usage: diotest4 [-b filesize_in_blocks]\n");
174 	exit(1);
175 }
176 
testcheck_end(int ret,int * failed,int * fail_count,char * msg)177 static void testcheck_end(int ret, int *failed, int *fail_count, char *msg)
178 {
179 	if (ret != 0) {
180 		*failed = TRUE;
181 		(*fail_count)++;
182 		tst_resm(TFAIL, "%s", msg);
183 	} else
184 		tst_resm(TPASS, "%s", msg);
185 }
186 
187 static void setup(void);
188 static void cleanup(void);
189 static int fd1 = -1;
190 static char filename[LEN];
191 
main(int argc,char * argv[])192 int main(int argc, char *argv[])
193 {
194 	int fblocks = 1;	/* Iterations. Default 1 */
195 	int bufsize = BUFSIZE;
196 	int count, ret;
197 	int offset;
198 	int fd, newfd;
199 	int i, l_fail = 0, fail_count = 0, total = 0;
200 	int failed = 0;
201 	int shmsz = SHMLBA;
202 	int pagemask = ~(sysconf(_SC_PAGE_SIZE) - 1);
203 	char *buf0, *buf1, *buf2;
204 	caddr_t shm_base;
205 
206 	/* Options */
207 	while ((i = getopt(argc, argv, "b:")) != -1) {
208 		switch (i) {
209 		case 'b':
210 			if ((fblocks = atoi(optarg)) <= 0) {
211 				fprintf(stderr, "fblocks must be > 0\n");
212 				prg_usage();
213 			}
214 			break;
215 		default:
216 			prg_usage();
217 		}
218 	}
219 
220 	setup();
221 
222 	/* Open file and fill, allocate for buffer */
223 	if ((fd = open(filename, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
224 		tst_brkm(TBROK, cleanup, "open failed for %s: %s",
225 			 filename, strerror(errno));
226 	}
227 	if ((buf0 = valloc(BUFSIZE)) == NULL) {
228 		tst_brkm(TBROK, cleanup, "valloc() buf0 failed: %s",
229 			 strerror(errno));
230 	}
231 	for (i = 1; i < fblocks; i++) {
232 		fillbuf(buf0, BUFSIZE, (char)i);
233 		if (write(fd, buf0, BUFSIZE) < 0) {
234 			tst_brkm(TBROK, cleanup, "write failed for %s: %s",
235 				 filename, strerror(errno));
236 		}
237 	}
238 	close(fd);
239 	if ((buf2 = valloc(BUFSIZE)) == NULL) {
240 		tst_brkm(TBROK, cleanup, "valloc() buf2 failed: %s",
241 			 strerror(errno));
242 	}
243 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
244 		tst_brkm(TBROK, cleanup, "open failed for %s: %s",
245 			 filename, strerror(errno));
246 	}
247 
248 	/* Test-1: Negative Offset */
249 	offset = -1;
250 	count = bufsize;
251 	errno = 0;
252 	ret = lseek(fd, offset, SEEK_SET);
253 	if ((ret >= 0) || (errno != EINVAL)) {
254 		tst_resm(TFAIL, "lseek allows negative offset. returns %d:%s",
255 			 ret, strerror(errno));
256 		failed = TRUE;
257 		fail_count++;
258 	} else
259 		tst_resm(TPASS, "Negative Offset");
260 	total++;
261 
262 	/* Test-2: Removed */
263 	tst_resm(TPASS, "removed");
264 
265 	/* Test-3: Odd count of read and write */
266 	offset = 0;
267 	count = 1;
268 	lseek(fd, 0, SEEK_SET);
269 	if (write(fd, buf2, 4096) == -1) {
270 		tst_resm(TFAIL, "can't write to file %d", ret);
271 	}
272 	switch (fs_type) {
273 	case TST_NFS_MAGIC:
274 	case TST_BTRFS_MAGIC:
275 		tst_resm(TCONF, "%s supports odd count IO",
276 			 tst_fs_type_name(fs_type));
277 	break;
278 	default:
279 		ret = runtest_f(fd, buf2, offset, count, EINVAL, 3, "odd count");
280 		testcheck_end(ret, &failed, &fail_count,
281 					"Odd count of read and write");
282 	}
283 
284 	total++;
285 
286 	/* Test-4: Read beyond the file size */
287 	offset = BUFSIZE * (fblocks + 10);
288 	count = bufsize;
289 	if (lseek(fd, offset, SEEK_SET) < 0) {
290 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
291 		failed = TRUE;
292 		fail_count++;
293 		tst_resm(TFAIL, "Read beyond the file size");
294 	} else {
295 		errno = 0;
296 		ret = read(fd, buf2, count);
297 		if (ret > 0 || (ret < 0 && errno != EINVAL)) {
298 			tst_resm(TFAIL,
299 				 "allows read beyond file size. returns %d: %s",
300 				 ret, strerror(errno));
301 			failed = TRUE;
302 			fail_count++;
303 		} else
304 			tst_resm(TPASS, "Read beyond the file size");
305 	}
306 	total++;
307 
308 	/* Test-5: Invalid file descriptor */
309 	offset = 4096;
310 	count = bufsize;
311 	newfd = -1;
312 	ret = runtest_f(newfd, buf2, offset, count, EBADF, 5, "negative fd");
313 	testcheck_end(ret, &failed, &fail_count, "Invalid file descriptor");
314 	total++;
315 
316 	/* Test-6: Out of range file descriptor */
317 	count = bufsize;
318 	offset = 4096;
319 	if ((newfd = getdtablesize()) < 0) {
320 		tst_resm(TFAIL, "getdtablesize() failed: %s", strerror(errno));
321 		failed = TRUE;
322 		tst_resm(TFAIL, "Out of range file descriptor");
323 	} else {
324 		ret = runtest_f(newfd, buf2, offset, count, EBADF, 6,
325 			      "out of range fd");
326 		testcheck_end(ret, &failed, &fail_count,
327 					"Out of range file descriptor");
328 	}
329 	close(newfd);
330 	total++;
331 
332 	/* Test-7: Closed file descriptor */
333 	offset = 4096;
334 	count = bufsize;
335 	if (close(fd) < 0) {
336 		tst_brkm(TBROK, cleanup, "can't close fd %d: %s", fd,
337 			 strerror(errno));
338 	}
339 	ret = runtest_f(fd, buf2, offset, count, EBADF, 7, "closed fd");
340 	testcheck_end(ret, &failed, &fail_count, "Closed file descriptor");
341 	total++;
342 
343 	/* Test-9: removed */
344 	tst_resm(TPASS, "removed");
345 
346 	/* Test-9: Character device (/dev/null) read, write */
347 	offset = 0;
348 	count = bufsize;
349 	if ((newfd = open("/dev/null", O_DIRECT | O_RDWR)) < 0) {
350 		tst_resm(TCONF, "Direct I/O on /dev/null is not supported");
351 	} else {
352 		ret = runtest_s(newfd, buf2, offset, count, 9, "/dev/null");
353 		testcheck_end(ret, &failed, &fail_count,
354 					"character device read, write");
355 	}
356 	close(newfd);
357 	total++;
358 
359 	/* Test-10: read, write to a mmaped file */
360 	shm_base = (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1));
361 	if (shm_base == NULL) {
362 		tst_brkm(TBROK, cleanup, "sbrk failed: %s", strerror(errno));
363 	}
364 	offset = 4096;
365 	count = bufsize;
366 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
367 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
368 			 filename, strerror(errno));
369 	}
370 	shm_base = mmap(shm_base, 0x100000, PROT_READ | PROT_WRITE,
371 			MAP_SHARED | MAP_FIXED, fd, 0);
372 	if (shm_base == (caddr_t) - 1) {
373 		tst_brkm(TBROK, cleanup, "can't mmap file: %s",
374 			 strerror(errno));
375 	}
376 	ret = runtest_s(fd, buf2, offset, count, 10, "mmapped file");
377 	testcheck_end(ret, &failed, &fail_count,
378 				"read, write to a mmaped file");
379 	total++;
380 
381 	/* Test-11: read, write to an unmaped file with munmap */
382 	if ((ret = munmap(shm_base, 0x100000)) < 0) {
383 		tst_brkm(TBROK, cleanup, "can't unmap file: %s",
384 			 strerror(errno));
385 	}
386 	ret = runtest_s(fd, buf2, offset, count, 11, "unmapped file");
387 	testcheck_end(ret, &failed, &fail_count,
388 				"read, write to an unmapped file");
389 	close(fd);
390 	total++;
391 
392 	/* Test-12: read from file not open for reading */
393 	offset = 4096;
394 	count = bufsize;
395 	if ((fd = open(filename, O_DIRECT | O_WRONLY)) < 0) {
396 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
397 			 filename, strerror(errno));
398 	}
399 	if (lseek(fd, offset, SEEK_SET) < 0) {
400 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
401 		failed = TRUE;
402 		fail_count++;
403 	} else {
404 		errno = 0;
405 		ret = read(fd, buf2, count);
406 		if (ret >= 0 || errno != EBADF) {
407 			tst_resm(TFAIL,
408 				 "allows read on file not open for reading. returns %d: %s",
409 				 ret, strerror(errno));
410 			failed = TRUE;
411 			fail_count++;
412 		} else
413 			tst_resm(TPASS, "read from file not open for reading");
414 	}
415 	close(fd);
416 	total++;
417 
418 	/* Test-13: write to file not open for writing */
419 	offset = 4096;
420 	count = bufsize;
421 	if ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) {
422 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
423 			 filename, strerror(errno));
424 	}
425 	if (lseek(fd, offset, SEEK_SET) < 0) {
426 		tst_resm(TFAIL, "lseek failed: %s", strerror(errno));
427 		failed = TRUE;
428 		fail_count++;
429 	} else {
430 		errno = 0;
431 		ret = write(fd, buf2, count);
432 		if (ret >= 0 || errno != EBADF) {
433 			tst_resm(TFAIL,
434 				 "allows write on file not open for writing. returns %d: %s",
435 				 ret, strerror(errno));
436 			failed = TRUE;
437 			fail_count++;
438 		} else
439 			tst_resm(TPASS, "write to file not open for writing");
440 	}
441 	close(fd);
442 	total++;
443 
444 	/* Test-14: read, write with non-aligned buffer */
445 	offset = 4096;
446 	count = bufsize;
447 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
448 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
449 			 filename, strerror(errno));
450 	}
451 	switch (fs_type) {
452 	case TST_NFS_MAGIC:
453 	case TST_BTRFS_MAGIC:
454 		tst_resm(TCONF, "%s supports non-aligned buffer",
455 			 tst_fs_type_name(fs_type));
456 	break;
457 	default:
458 		ret = runtest_f(fd, buf2 + 1, offset, count, EINVAL, 14,
459 					" nonaligned buf");
460 		testcheck_end(ret, &failed, &fail_count,
461 				"read, write with non-aligned buffer");
462 	}
463 	close(fd);
464 	total++;
465 
466 	/* Test-15: read, write buffer in read-only space */
467 	offset = 4096;
468 	count = bufsize;
469 	l_fail = 0;
470 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
471 		tst_brkm(TBROK, cleanup, "can't open %s: %s",
472 			 filename, strerror(errno));
473 	}
474 	if (lseek(fd, offset, SEEK_SET) < 0) {
475 		tst_resm(TFAIL, "lseek before read failed: %s",
476 			 strerror(errno));
477 		l_fail = TRUE;
478 	} else {
479 		errno = 0;
480 		ret = read(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
481 			 count);
482 		if (ret >= 0 || errno != EFAULT) {
483 			tst_resm(TFAIL,
484 				 "read to read-only space. returns %d: %s", ret,
485 				 strerror(errno));
486 			l_fail = TRUE;
487 		}
488 	}
489 	if (lseek(fd, offset, SEEK_SET) < 0) {
490 		tst_resm(TFAIL, "lseek before write failed: %s",
491 			 strerror(errno));
492 		l_fail = TRUE;
493 	} else {
494 		ret = write(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
495 			  count);
496 		if (ret < 0) {
497 			tst_resm(TFAIL,
498 				 "write to read-only space. returns %d: %s",
499 				 ret, strerror(errno));
500 			l_fail = TRUE;
501 		}
502 	}
503 	testcheck_end(l_fail, &failed, &fail_count,
504 				"read, write buffer in read-only space");
505 	close(fd);
506 	total++;
507 
508 	/* Test-16: read, write in non-existant space */
509 	offset = 4096;
510 	count = bufsize;
511 	if ((buf1 =
512 	     (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1))) == NULL) {
513 		tst_brkm(TBROK | TERRNO, cleanup, "sbrk failed");
514 	}
515 	if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
516 		tst_brkm(TBROK | TERRNO, cleanup,
517 			 "open(%s, O_DIRECT|O_RDWR) failed", filename);
518 	}
519 	ret = runtest_f(fd, buf1, offset, count, EFAULT, 16,
520 		      " nonexistant space");
521 	testcheck_end(ret, &failed, &fail_count,
522 				"read, write in non-existant space");
523 	total++;
524 	close(fd);
525 
526 	/* Test-17: read, write for file with O_SYNC */
527 	offset = 4096;
528 	count = bufsize;
529 	if ((fd = open(filename, O_DIRECT | O_RDWR | O_SYNC)) < 0) {
530 		tst_brkm(TBROK, cleanup,
531 			 "open(%s, O_DIRECT|O_RDWR|O_SYNC failed)", filename);
532 	}
533 	ret = runtest_s(fd, buf2, offset, count, 17, "opened with O_SYNC");
534 	testcheck_end(ret, &failed, &fail_count,
535 				"read, write for file with O_SYNC");
536 	total++;
537 	close(fd);
538 
539 	unlink(filename);
540 	if (failed)
541 		tst_resm(TINFO, "%d/%d test blocks failed", fail_count, total);
542 	else
543 		tst_resm(TINFO, "%d testblocks completed", total);
544 	cleanup();
545 
546 	tst_exit();
547 }
548 
setup(void)549 static void setup(void)
550 {
551 	struct sigaction act;
552 
553 	tst_tmpdir();
554 
555 	act.sa_handler = SIG_IGN;
556 	act.sa_flags = 0;
557 	sigemptyset(&act.sa_mask);
558 	(void)sigaction(SIGXFSZ, &act, NULL);
559 	sprintf(filename, "testdata-4.%ld", syscall(__NR_gettid));
560 
561 	if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
562 		tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
563 			 filename, strerror(errno));
564 	}
565 	close(fd1);
566 
567 	/* Test for filesystem support of O_DIRECT */
568 	if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
569 		tst_brkm(TCONF, cleanup,
570 			 "O_DIRECT is not supported by this filesystem. %s",
571 			 strerror(errno));
572 	}
573 	close(fd1);
574 
575 	fs_type = tst_fs_type(cleanup, ".");
576 }
577 
cleanup(void)578 static void cleanup(void)
579 {
580 	if (fd1 != -1)
581 		unlink(filename);
582 
583 	tst_rmdir();
584 
585 }
586 
587 #else /* O_DIRECT */
588 
main()589 int main()
590 {
591 	tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
592 }
593 #endif /* O_DIRECT */
594