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
69 #include "diotest_routines.h"
70
71 #include "test.h"
72 #include "safe_macros.h"
73 #include "lapi/mmap.h"
74
75 char *TCID = "diotest4"; /* Test program identifier. */
76 int TST_TOTAL = 17; /* Total number of test conditions */
77
78 static long fs_type;
79
80 #ifdef O_DIRECT
81
82 #define BUFSIZE 4096
83 #define TRUE 1
84 #define LEN 30
85
86 #ifdef __GNUC__
87 #define ADDRESS_OF_MAIN __builtin_extract_return_addr(__builtin_return_address(0))
88 #else
89 #define ADDRESS_OF_MAIN main
90 #endif
91
92 /*
93 * runtest_f: Do read, writes. Verify the error value obtained by
94 * running read or write with the expected error value (errnum).
95 */
96 int
runtest_f(int fd,char * buf,int offset,int count,int errnum,int testnum,char * msg)97 runtest_f(int fd, char *buf, int offset, int count, int errnum, int testnum,
98 char *msg)
99 {
100 int ret;
101 int l_fail = 0;
102
103 if (lseek(fd, offset, SEEK_SET) < 0) {
104 if (errno != errnum) {
105 tst_resm(TFAIL, "lseek before read failed: %s",
106 strerror(errno));
107 l_fail = TRUE;
108 }
109 } else {
110 errno = 0;
111 ret = read(fd, buf, count);
112 if (ret >= 0 || errno != errnum) {
113 tst_resm(TFAIL, "read allows %s. returns %d: %s",
114 msg, ret, strerror(errno));
115 l_fail = TRUE;
116 }
117 }
118 if (lseek(fd, offset, SEEK_SET) < 0) {
119 if (errno != errnum) {
120 tst_resm(TFAIL, "lseek before write failed: %s",
121 strerror(errno));
122 l_fail = TRUE;
123 }
124 } else {
125 errno = 0;
126 ret = write(fd, buf, count);
127 if (ret >= 0 || errno != errnum) {
128 tst_resm(TFAIL, "write allows %s.returns %d: %s",
129 msg, ret, strerror(errno));
130 l_fail = TRUE;
131 }
132 }
133 return (l_fail);
134 }
135
136 /*
137 * runtest_s: Do read, writes. Verify the they run successfully.
138 */
runtest_s(int fd,char * buf,int offset,int count,int testnum,char * msg)139 int runtest_s(int fd, char *buf, int offset, int count, int testnum, char *msg)
140 {
141 int ret;
142 int l_fail = 0;
143
144 if (lseek(fd, offset, SEEK_SET) < 0) {
145 tst_resm(TFAIL, "lseek before read failed: %s",
146 strerror(errno));
147 l_fail = TRUE;
148 } else {
149 if ((ret = read(fd, buf, count)) < 0) {
150 tst_resm(TFAIL, "read failed for %s. returns %d: %s",
151 msg, ret, strerror(errno));
152 l_fail = TRUE;
153 }
154 }
155 if (lseek(fd, offset, SEEK_SET) < 0) {
156 tst_resm(TFAIL, "lseek before write failed: %s",
157 strerror(errno));
158 l_fail = TRUE;
159 } else {
160 if ((ret = write(fd, buf, count)) < 0) {
161 tst_resm(TFAIL, "write failed for %s. returns %d: %s",
162 msg, ret, strerror(errno));
163 l_fail = TRUE;
164 }
165 }
166 return (l_fail);
167 }
168
prg_usage(void)169 static void prg_usage(void)
170 {
171 fprintf(stderr, "Usage: diotest4 [-b filesize_in_blocks]\n");
172 exit(1);
173 }
174
testcheck_end(int ret,int * failed,int * fail_count,char * msg)175 static void testcheck_end(int ret, int *failed, int *fail_count, char *msg)
176 {
177 if (ret != 0) {
178 *failed = TRUE;
179 (*fail_count)++;
180 tst_resm(TFAIL, "%s", msg);
181 } else
182 tst_resm(TPASS, "%s", msg);
183 }
184
185 static void setup(void);
186 static void cleanup(void);
187 static int fd1 = -1;
188 static char filename[LEN];
189
main(int argc,char * argv[])190 int main(int argc, char *argv[])
191 {
192 int fblocks = 1; /* Iterations. Default 1 */
193 int bufsize = BUFSIZE;
194 int count, ret;
195 int offset;
196 int fd, newfd;
197 int i, l_fail = 0, fail_count = 0, total = 0;
198 int failed = 0;
199 int shmsz = MMAP_GRANULARITY;
200 int pagemask = ~(sysconf(_SC_PAGE_SIZE) - 1);
201 char *buf0, *buf1, *buf2;
202 caddr_t shm_base;
203
204 /* Options */
205 while ((i = getopt(argc, argv, "b:")) != -1) {
206 switch (i) {
207 case 'b':
208 if ((fblocks = atoi(optarg)) <= 0) {
209 fprintf(stderr, "fblocks must be > 0\n");
210 prg_usage();
211 }
212 break;
213 default:
214 prg_usage();
215 }
216 }
217
218 setup();
219
220 /* Open file and fill, allocate for buffer */
221 if ((fd = open(filename, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
222 tst_brkm(TBROK, cleanup, "open failed for %s: %s",
223 filename, strerror(errno));
224 }
225 if ((buf0 = valloc(bufsize)) == NULL) {
226 tst_brkm(TBROK, cleanup, "valloc() buf0 failed: %s",
227 strerror(errno));
228 }
229 for (i = 1; i < fblocks; i++) {
230 fillbuf(buf0, bufsize, (char)i);
231 if (write(fd, buf0, bufsize) < 0) {
232 tst_brkm(TBROK, cleanup, "write failed for %s: %s",
233 filename, strerror(errno));
234 }
235 }
236 close(fd);
237 if ((buf2 = valloc(bufsize)) == NULL) {
238 tst_brkm(TBROK, cleanup, "valloc() buf2 failed: %s",
239 strerror(errno));
240 }
241 if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
242 tst_brkm(TBROK, cleanup, "open failed for %s: %s",
243 filename, strerror(errno));
244 }
245
246 /* Test-1: Negative Offset */
247 offset = -1;
248 count = bufsize;
249 errno = 0;
250 ret = lseek(fd, offset, SEEK_SET);
251 if ((ret >= 0) || (errno != EINVAL)) {
252 tst_resm(TFAIL, "lseek allows negative offset. returns %d:%s",
253 ret, strerror(errno));
254 failed = TRUE;
255 fail_count++;
256 } else
257 tst_resm(TPASS, "Negative Offset");
258 total++;
259
260 /* Test-2: Removed */
261 tst_resm(TPASS, "removed");
262
263 /* Test-3: Odd count of read and write */
264 offset = 0;
265 count = 1;
266 lseek(fd, 0, SEEK_SET);
267 if (write(fd, buf2, 4096) == -1) {
268 tst_resm(TFAIL, "can't write to file %d", ret);
269 }
270 switch (fs_type) {
271 case TST_NFS_MAGIC:
272 case TST_BTRFS_MAGIC:
273 case TST_FUSE_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 tst_resm(TCONF, "%s supports non-aligned buffer",
448 tst_fs_type_name(fs_type));
449 break;
450 default:
451 ret = runtest_f(fd, buf2 + 1, offset, count, EINVAL, 14,
452 " nonaligned buf");
453 testcheck_end(ret, &failed, &fail_count,
454 "read, write with non-aligned buffer");
455 }
456 close(fd);
457 total++;
458
459 /* Test-15: read, write buffer in read-only space */
460 offset = 4096;
461 count = bufsize;
462 l_fail = 0;
463 if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
464 tst_brkm(TBROK, cleanup, "can't open %s: %s",
465 filename, strerror(errno));
466 }
467 if (lseek(fd, offset, SEEK_SET) < 0) {
468 tst_resm(TFAIL, "lseek before read failed: %s",
469 strerror(errno));
470 l_fail = TRUE;
471 } else {
472 errno = 0;
473 ret = read(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
474 count);
475 if (ret >= 0 || errno != EFAULT) {
476 tst_resm(TFAIL,
477 "read to read-only space. returns %d: %s", ret,
478 strerror(errno));
479 l_fail = TRUE;
480 }
481 }
482 if (lseek(fd, offset, SEEK_SET) < 0) {
483 tst_resm(TFAIL, "lseek before write failed: %s",
484 strerror(errno));
485 l_fail = TRUE;
486 } else {
487 ret = write(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask),
488 count);
489 if (ret < 0) {
490 tst_resm(TFAIL,
491 "write to read-only space. returns %d: %s",
492 ret, strerror(errno));
493 l_fail = TRUE;
494 }
495 }
496 testcheck_end(l_fail, &failed, &fail_count,
497 "read, write buffer in read-only space");
498 close(fd);
499 total++;
500
501 /* Test-16: read, write in non-existant space */
502 offset = 4096;
503 count = bufsize;
504 if ((buf1 =
505 (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1))) == NULL) {
506 tst_brkm(TBROK | TERRNO, cleanup, "sbrk failed");
507 }
508 if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) {
509 tst_brkm(TBROK | TERRNO, cleanup,
510 "open(%s, O_DIRECT|O_RDWR) failed", filename);
511 }
512 ret = runtest_f(fd, buf1, offset, count, EFAULT, 16,
513 " nonexistant space");
514 testcheck_end(ret, &failed, &fail_count,
515 "read, write in non-existant space");
516 total++;
517 close(fd);
518
519 /* Test-17: read, write for file with O_SYNC */
520 offset = 4096;
521 count = bufsize;
522 if ((fd = open(filename, O_DIRECT | O_RDWR | O_SYNC)) < 0) {
523 tst_brkm(TBROK, cleanup,
524 "open(%s, O_DIRECT|O_RDWR|O_SYNC failed)", filename);
525 }
526 ret = runtest_s(fd, buf2, offset, count, 17, "opened with O_SYNC");
527 testcheck_end(ret, &failed, &fail_count,
528 "read, write for file with O_SYNC");
529 total++;
530 close(fd);
531
532 unlink(filename);
533 if (failed)
534 tst_resm(TINFO, "%d/%d test blocks failed", fail_count, total);
535 else
536 tst_resm(TINFO, "%d testblocks completed", total);
537 cleanup();
538
539 tst_exit();
540 }
541
setup(void)542 static void setup(void)
543 {
544 struct sigaction act;
545
546 tst_tmpdir();
547
548 act.sa_handler = SIG_IGN;
549 act.sa_flags = 0;
550 sigemptyset(&act.sa_mask);
551 (void)sigaction(SIGXFSZ, &act, NULL);
552 sprintf(filename, "testdata-4.%ld", syscall(__NR_gettid));
553
554 if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
555 tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
556 filename, strerror(errno));
557 }
558 close(fd1);
559
560 /* Test for filesystem support of O_DIRECT */
561 if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
562 tst_brkm(TCONF, cleanup,
563 "O_DIRECT is not supported by this filesystem. %s",
564 strerror(errno));
565 }
566 close(fd1);
567
568 fs_type = tst_fs_type(cleanup, ".");
569 }
570
cleanup(void)571 static void cleanup(void)
572 {
573 if (fd1 != -1)
574 unlink(filename);
575
576 tst_rmdir();
577
578 }
579
580 #else /* O_DIRECT */
581
main()582 int main()
583 {
584 tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
585 }
586 #endif /* O_DIRECT */
587