plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * 07/2001 Ported by Wayne Boyer |
| 4 | * |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 19 | /* |
| 20 | * DESCRIPTION |
| 21 | * test 1: |
| 22 | * Read with an invalid file descriptor, and expect an EBADF. |
| 23 | * |
| 24 | * test 2: |
| 25 | * The parameter passed to read is a directory, check if the errno is |
| 26 | * set to EISDIR. |
| 27 | * |
| 28 | * test 3: |
| 29 | * Buf is outside the accessible address space, expect an EFAULT. |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 30 | * |
| 31 | * test 4: |
| 32 | * The file was opened with the O_DIRECT flag, and transfer sizes was not |
| 33 | * multiples of the logical block size of the file system, expect an |
| 34 | * EINVAL. |
| 35 | * |
| 36 | * test 5: |
| 37 | * The file was opened with the O_DIRECT flag, and the alignment of the |
| 38 | * user buffer was not multiples of the logical block size of the file |
| 39 | * system, expect an EINVAL. |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #define _GNU_SOURCE |
| 43 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <errno.h> |
| 46 | #include <unistd.h> |
| 47 | #include <fcntl.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 48 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include "test.h" |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 50 | #include "safe_macros.h" |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 51 | #include "tst_fs_type.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "read02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 55 | static int badfd = -1; |
Zeng Linggang | 9018b0a | 2014-05-27 14:26:23 +0800 | [diff] [blame] | 56 | static int fd2, fd3, fd4 = -1; |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 57 | static char buf[BUFSIZ]; |
| 58 | static void *outside_buf = (void *)-1; |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 59 | static void *addr4; |
| 60 | static void *addr5; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Zeng Linggang | 9018b0a | 2014-05-27 14:26:23 +0800 | [diff] [blame] | 62 | static long fs_type; |
| 63 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 64 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | int *fd; |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 66 | void **buf; |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 67 | size_t count; |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 68 | int exp_error; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | } TC[] = { |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 70 | {&badfd, (void **)&buf, 1, EBADF}, |
| 71 | {&fd2, (void **)&buf, 1, EISDIR}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 72 | #ifndef UCLINUX |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 73 | {&fd3, &outside_buf, 1, EFAULT}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 74 | #endif |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 75 | {&fd4, &addr4, 1, EINVAL}, |
| 76 | {&fd4, &addr5, 4096, EINVAL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 79 | int TST_TOTAL = ARRAY_SIZE(TC); |
| 80 | static void setup(void); |
| 81 | static void cleanup(void); |
| 82 | static void read_verify(const struct test_case_t *); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 83 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 84 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | { |
| 86 | int i; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 87 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 88 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 90 | msg = parse_opts(ac, av, NULL, NULL); |
| 91 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 92 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | setup(); |
| 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 97 | tst_count = 0; |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 98 | for (i = 0; i < TST_TOTAL; i++) |
| 99 | read_verify(&TC[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | } |
| 101 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 102 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 105 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | TEST_PAUSE; |
| 110 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 111 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 113 | fd2 = SAFE_OPEN(cleanup, ".", O_DIRECTORY); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 114 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 115 | SAFE_FILE_PRINTF(cleanup, "test_file", "A"); |
| 116 | |
| 117 | fd3 = SAFE_OPEN(cleanup, "test_file", O_RDWR); |
| 118 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 119 | #if !defined(UCLINUX) |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 120 | outside_buf = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 121 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 122 | #endif |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 123 | |
| 124 | addr4 = SAFE_MEMALIGN(cleanup, getpagesize(), (4096 * 10)); |
| 125 | addr5 = addr4 + 1; |
| 126 | |
Zeng Linggang | 9018b0a | 2014-05-27 14:26:23 +0800 | [diff] [blame] | 127 | fs_type = tst_fs_type(cleanup, "."); |
| 128 | if (fs_type != TST_TMPFS_MAGIC) |
| 129 | fd4 = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_DIRECT); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 132 | static void read_verify(const struct test_case_t *test) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | { |
Zeng Linggang | 9018b0a | 2014-05-27 14:26:23 +0800 | [diff] [blame] | 134 | if (test->fd == &fd4 && *test->fd == -1) { |
| 135 | tst_resm(TCONF, "O_DIRECT not supported on %s filesystem", |
| 136 | tst_fs_type_name(fs_type)); |
| 137 | return; |
| 138 | } |
| 139 | |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 140 | TEST(read(*test->fd, *test->buf, test->count)); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | |
Cyril Hrubis | 82cfa33 | 2014-08-28 10:41:57 +0200 | [diff] [blame] | 142 | if (*test->fd == fd4 && TEST_RETURN >= 0) { |
| 143 | tst_resm(TPASS, |
| 144 | "O_DIRECT unaligned reads fallbacks to buffered I/O"); |
| 145 | return; |
| 146 | } |
| 147 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 148 | if (TEST_RETURN != -1) { |
| 149 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 150 | return; |
| 151 | } |
| 152 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 153 | if (TEST_ERRNO == test->exp_error) { |
| 154 | tst_resm(TPASS | TTERRNO, "expected failure"); |
| 155 | } else { |
| 156 | tst_resm(TFAIL | TTERRNO, "unexpected error expected %d", |
| 157 | test->exp_error); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void cleanup(void) |
| 162 | { |
Zeng Linggang | dbf7cd5 | 2014-05-06 15:52:01 +0800 | [diff] [blame] | 163 | free(addr4); |
| 164 | |
| 165 | if (fd4 > 0) |
| 166 | close(fd4); |
| 167 | |
Zeng Linggang | 6791228 | 2014-05-06 15:51:01 +0800 | [diff] [blame] | 168 | if (fd3 > 0) |
| 169 | close(fd3); |
| 170 | |
| 171 | if (fd2 > 0) |
| 172 | close(fd2); |
| 173 | |
| 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |