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