[64734fc] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup test_bdbuf |
---|
| 5 | * |
---|
| 6 | * @brief Bdbuf test for disk driver error handling.. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
[5005bfc] | 10 | * Copyright (c) 2009, 2018 embedded brains GmbH. All rights reserved. |
---|
[796967c] | 11 | * |
---|
| 12 | * embedded brains GmbH |
---|
[5005bfc] | 13 | * Dornierstr. 4 |
---|
[796967c] | 14 | * 82178 Puchheim |
---|
| 15 | * Germany |
---|
| 16 | * <rtems@embedded-brains.de> |
---|
[64734fc] | 17 | * |
---|
| 18 | * The license and distribution terms for this file may be |
---|
| 19 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 20 | * http://www.rtems.org/license/LICENSE. |
---|
[64734fc] | 21 | */ |
---|
| 22 | |
---|
[7d3f9c6] | 23 | #ifdef HAVE_CONFIG_H |
---|
| 24 | #include "config.h" |
---|
| 25 | #endif |
---|
| 26 | |
---|
[24d0ee57] | 27 | #include "tmacros.h" |
---|
| 28 | |
---|
[5005bfc] | 29 | #include <sys/stat.h> |
---|
[64734fc] | 30 | #include <assert.h> |
---|
| 31 | #include <errno.h> |
---|
[5005bfc] | 32 | #include <fcntl.h> |
---|
[4cf41fd] | 33 | #include <stdlib.h> |
---|
[5005bfc] | 34 | #include <unistd.h> |
---|
[64734fc] | 35 | |
---|
| 36 | #include <rtems.h> |
---|
| 37 | #include <rtems/bdbuf.h> |
---|
[f8b2eb03] | 38 | #include <rtems/test.h> |
---|
[64734fc] | 39 | |
---|
[dc0eed7] | 40 | #include <bsp.h> |
---|
| 41 | |
---|
[f8b2eb03] | 42 | const char rtems_test_name[] = "BLOCK 9"; |
---|
| 43 | |
---|
[64734fc] | 44 | #define ASSERT_SC(sc) assert((sc) == RTEMS_SUCCESSFUL) |
---|
| 45 | |
---|
| 46 | #define BLOCK_SIZE sizeof(char) |
---|
| 47 | |
---|
| 48 | #define BLOCK_COUNT 4 |
---|
| 49 | |
---|
| 50 | #define BLOCK_READ_IO_ERROR 0 |
---|
| 51 | #define BLOCK_READ_UNSATISFIED 1 |
---|
| 52 | #define BLOCK_READ_SUCCESSFUL 2 |
---|
| 53 | #define BLOCK_WRITE_IO_ERROR 3 |
---|
| 54 | |
---|
[5005bfc] | 55 | #define DISK_PATH "/disk" |
---|
[64734fc] | 56 | |
---|
[5005bfc] | 57 | static char disk_data [BLOCK_COUNT]; |
---|
[64734fc] | 58 | |
---|
| 59 | static int disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg) |
---|
| 60 | { |
---|
| 61 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 62 | |
---|
| 63 | if (req == RTEMS_BLKIO_REQUEST) { |
---|
| 64 | rtems_blkdev_request *r = arg; |
---|
| 65 | uint32_t i = 0; |
---|
| 66 | |
---|
| 67 | for (i = 0; i < r->bufnum; ++i) { |
---|
| 68 | rtems_blkdev_sg_buffer *sg = &r->bufs [i]; |
---|
| 69 | char *buf = sg->buffer; |
---|
| 70 | |
---|
[9f0a68c] | 71 | if (sg->length == 1) { |
---|
| 72 | switch (r->req) { |
---|
| 73 | case RTEMS_BLKDEV_REQ_READ: |
---|
| 74 | switch (sg->block) { |
---|
| 75 | case BLOCK_READ_IO_ERROR: |
---|
| 76 | sc = RTEMS_IO_ERROR; |
---|
| 77 | break; |
---|
| 78 | case BLOCK_READ_UNSATISFIED: |
---|
| 79 | sc = RTEMS_UNSATISFIED; |
---|
| 80 | break; |
---|
| 81 | case BLOCK_READ_SUCCESSFUL: |
---|
| 82 | case BLOCK_WRITE_IO_ERROR: |
---|
| 83 | *buf = disk_data [sg->block]; |
---|
| 84 | break; |
---|
| 85 | default: |
---|
| 86 | sc = RTEMS_IO_ERROR; |
---|
| 87 | break; |
---|
| 88 | } |
---|
| 89 | break; |
---|
| 90 | case RTEMS_BLKDEV_REQ_WRITE: |
---|
| 91 | switch (sg->block) { |
---|
| 92 | case BLOCK_READ_IO_ERROR: |
---|
| 93 | case BLOCK_READ_UNSATISFIED: |
---|
| 94 | case BLOCK_READ_SUCCESSFUL: |
---|
| 95 | disk_data [sg->block] = *buf; |
---|
| 96 | break; |
---|
| 97 | case BLOCK_WRITE_IO_ERROR: |
---|
| 98 | sc = RTEMS_IO_ERROR; |
---|
| 99 | break; |
---|
| 100 | default: |
---|
| 101 | sc = RTEMS_IO_ERROR; |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | break; |
---|
| 105 | default: |
---|
| 106 | sc = RTEMS_IO_ERROR; |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | } else { |
---|
| 110 | sc = RTEMS_IO_ERROR; |
---|
[64734fc] | 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
[9f0a68c] | 114 | rtems_blkdev_request_done(r, sc); |
---|
[64734fc] | 115 | |
---|
| 116 | return 0; |
---|
| 117 | } else { |
---|
| 118 | return rtems_blkdev_ioctl(dd, req, arg); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
[5005bfc] | 122 | static void disk_register( |
---|
[64734fc] | 123 | uint32_t block_size, |
---|
| 124 | rtems_blkdev_bnum block_count, |
---|
[5005bfc] | 125 | rtems_disk_device **dd |
---|
[64734fc] | 126 | ) |
---|
| 127 | { |
---|
[5005bfc] | 128 | rtems_status_code sc; |
---|
| 129 | int fd; |
---|
| 130 | int rv; |
---|
[64734fc] | 131 | |
---|
[5005bfc] | 132 | sc = rtems_blkdev_create( |
---|
| 133 | DISK_PATH, |
---|
[64734fc] | 134 | block_size, |
---|
| 135 | block_count, |
---|
| 136 | disk_ioctl, |
---|
| 137 | NULL |
---|
| 138 | ); |
---|
| 139 | ASSERT_SC(sc); |
---|
| 140 | |
---|
[5005bfc] | 141 | fd = open(DISK_PATH, O_RDWR); |
---|
| 142 | rtems_test_assert(fd >= 0); |
---|
| 143 | |
---|
| 144 | rv = rtems_disk_fd_get_disk_device(fd, dd); |
---|
| 145 | rtems_test_assert(rv == 0); |
---|
[64734fc] | 146 | |
---|
[5005bfc] | 147 | rv = close(fd); |
---|
| 148 | rtems_test_assert(rv == 0); |
---|
[64734fc] | 149 | } |
---|
| 150 | |
---|
| 151 | static void check_read( |
---|
[40284de] | 152 | rtems_disk_device *dd, |
---|
[64734fc] | 153 | rtems_blkdev_bnum block, |
---|
| 154 | rtems_status_code expected_sc |
---|
| 155 | ) |
---|
| 156 | { |
---|
| 157 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 158 | rtems_bdbuf_buffer *bd = NULL; |
---|
| 159 | |
---|
[796967c] | 160 | sc = rtems_bdbuf_read(dd, block, &bd); |
---|
[64734fc] | 161 | assert(sc == expected_sc); |
---|
| 162 | |
---|
| 163 | if (sc == RTEMS_SUCCESSFUL) { |
---|
| 164 | sc = rtems_bdbuf_release(bd); |
---|
| 165 | ASSERT_SC(sc); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | static rtems_task Init(rtems_task_argument argument) |
---|
| 170 | { |
---|
| 171 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 172 | rtems_bdbuf_buffer *bd = NULL; |
---|
[796967c] | 173 | rtems_disk_device *dd = NULL; |
---|
[64734fc] | 174 | |
---|
[24d0ee57] | 175 | TEST_BEGIN(); |
---|
[64734fc] | 176 | |
---|
[5005bfc] | 177 | disk_register(BLOCK_SIZE, BLOCK_COUNT, &dd); |
---|
[796967c] | 178 | |
---|
| 179 | check_read(dd, BLOCK_READ_IO_ERROR, RTEMS_IO_ERROR); |
---|
| 180 | check_read(dd, BLOCK_READ_UNSATISFIED, RTEMS_UNSATISFIED); |
---|
| 181 | check_read(dd, BLOCK_READ_SUCCESSFUL, RTEMS_SUCCESSFUL); |
---|
| 182 | check_read(dd, BLOCK_WRITE_IO_ERROR, RTEMS_SUCCESSFUL); |
---|
[64734fc] | 183 | |
---|
| 184 | /* Check write IO error */ |
---|
| 185 | |
---|
[796967c] | 186 | sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd); |
---|
[64734fc] | 187 | ASSERT_SC(sc); |
---|
| 188 | |
---|
| 189 | bd->buffer [0] = 1; |
---|
| 190 | |
---|
| 191 | sc = rtems_bdbuf_sync(bd); |
---|
| 192 | ASSERT_SC(sc); |
---|
| 193 | |
---|
[796967c] | 194 | sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd); |
---|
[64734fc] | 195 | ASSERT_SC(sc); |
---|
| 196 | |
---|
| 197 | assert(bd->buffer [0] == 0); |
---|
| 198 | |
---|
| 199 | sc = rtems_bdbuf_release(bd); |
---|
| 200 | ASSERT_SC(sc); |
---|
| 201 | |
---|
| 202 | /* Check write to deleted disk */ |
---|
| 203 | |
---|
[796967c] | 204 | sc = rtems_bdbuf_read(dd, BLOCK_READ_SUCCESSFUL, &bd); |
---|
[64734fc] | 205 | ASSERT_SC(sc); |
---|
| 206 | |
---|
[5005bfc] | 207 | sc = unlink(DISK_PATH); |
---|
[64734fc] | 208 | ASSERT_SC(sc); |
---|
| 209 | |
---|
| 210 | sc = rtems_bdbuf_sync(bd); |
---|
| 211 | ASSERT_SC(sc); |
---|
| 212 | |
---|
[24d0ee57] | 213 | TEST_END(); |
---|
[64734fc] | 214 | |
---|
| 215 | exit(0); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | #define CONFIGURE_INIT |
---|
| 219 | |
---|
| 220 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
[c4b8b147] | 221 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
[64734fc] | 222 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
| 223 | |
---|
[5005bfc] | 224 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
| 225 | |
---|
[b6911069] | 226 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
[64734fc] | 227 | |
---|
[f8b2eb03] | 228 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
| 229 | |
---|
[64734fc] | 230 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
| 231 | |
---|
| 232 | #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE |
---|
| 233 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE |
---|
| 234 | #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (BLOCK_SIZE * BLOCK_COUNT) |
---|
| 235 | |
---|
| 236 | #include <rtems/confdefs.h> |
---|