[57aa979] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
[4670d91] | 4 | * @ingroup rtems_bdbuf |
---|
| 5 | * |
---|
[57aa979] | 6 | * Block device buffer management. |
---|
| 7 | */ |
---|
| 8 | |
---|
[e51bd96] | 9 | /* |
---|
| 10 | * Disk I/O buffering |
---|
| 11 | * Buffer managment |
---|
| 12 | * |
---|
| 13 | * Copyright (C) 2001 OKTET Ltd., St.-Peterburg, Russia |
---|
| 14 | * Author: Andrey G. Ivanov <Andrey.Ivanov@oktet.ru> |
---|
| 15 | * Victor V. Vengerov <vvv@oktet.ru> |
---|
[df6348bb] | 16 | * Alexander Kukuta <kam@oktet.ru> |
---|
[e51bd96] | 17 | * |
---|
[0d15414e] | 18 | * Copyright (C) 2008,2009 Chris Johns <chrisj@rtems.org> |
---|
[c21c850e] | 19 | * Rewritten to remove score mutex access. Fixes many performance |
---|
| 20 | * issues. |
---|
| 21 | * |
---|
[3d14a45] | 22 | * @(#) bdbuf.c,v 1.14 2004/04/17 08:15:17 ralf Exp |
---|
[e51bd96] | 23 | */ |
---|
| 24 | |
---|
[3899a537] | 25 | /** |
---|
| 26 | * Set to 1 to enable debug tracing. |
---|
| 27 | */ |
---|
| 28 | #define RTEMS_BDBUF_TRACE 0 |
---|
| 29 | |
---|
[006fa1ef] | 30 | #if HAVE_CONFIG_H |
---|
| 31 | #include "config.h" |
---|
| 32 | #endif |
---|
| 33 | |
---|
[e51bd96] | 34 | #include <rtems.h> |
---|
[3899a537] | 35 | #include <rtems/error.h> |
---|
[57aa979] | 36 | #include <rtems/malloc.h> |
---|
[e51bd96] | 37 | #include <limits.h> |
---|
| 38 | #include <errno.h> |
---|
| 39 | #include <assert.h> |
---|
| 40 | |
---|
[3899a537] | 41 | #if RTEMS_BDBUF_TRACE |
---|
| 42 | #include <stdio.h> |
---|
[0ebfac19] | 43 | #endif |
---|
| 44 | |
---|
[3899a537] | 45 | #include "rtems/bdbuf.h" |
---|
[e51bd96] | 46 | |
---|
[0d15414e] | 47 | /* |
---|
| 48 | * Simpler label for this file. |
---|
| 49 | */ |
---|
| 50 | #define bdbuf_config rtems_bdbuf_configuration |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * A swapout transfer transaction data. This data is passed to a worked thread |
---|
| 54 | * to handle the write phase of the transfer. |
---|
| 55 | */ |
---|
| 56 | typedef struct rtems_bdbuf_swapout_transfer |
---|
| 57 | { |
---|
| 58 | rtems_chain_control bds; /**< The transfer list of BDs. */ |
---|
| 59 | dev_t dev; /**< The device the transfer is for. */ |
---|
| 60 | rtems_blkdev_request* write_req; /**< The write request array. */ |
---|
| 61 | } rtems_bdbuf_swapout_transfer; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Swapout worker thread. These are available to take processing from the |
---|
| 65 | * main swapout thread and handle the I/O operation. |
---|
| 66 | */ |
---|
| 67 | typedef struct rtems_bdbuf_swapout_worker |
---|
| 68 | { |
---|
| 69 | rtems_chain_node link; /**< The threads sit on a chain when |
---|
| 70 | * idle. */ |
---|
| 71 | rtems_id id; /**< The id of the task so we can wake |
---|
| 72 | * it. */ |
---|
| 73 | volatile bool enabled; /**< The worked is enabled. */ |
---|
| 74 | rtems_bdbuf_swapout_transfer transfer; /**< The transfer data for this |
---|
| 75 | * thread. */ |
---|
| 76 | } rtems_bdbuf_swapout_worker; |
---|
| 77 | |
---|
[3899a537] | 78 | /** |
---|
[0d15414e] | 79 | * The BD buffer cache. |
---|
[0ebfac19] | 80 | */ |
---|
[0d15414e] | 81 | typedef struct rtems_bdbuf_cache |
---|
| 82 | { |
---|
| 83 | rtems_id swapout; /**< Swapout task ID */ |
---|
| 84 | volatile bool swapout_enabled; /**< Swapout is only running if |
---|
| 85 | * enabled. Set to false to kill the |
---|
| 86 | * swap out task. It deletes itself. */ |
---|
| 87 | rtems_chain_control swapout_workers; /**< The work threads for the swapout |
---|
| 88 | * task. */ |
---|
| 89 | |
---|
| 90 | rtems_bdbuf_buffer* bds; /**< Pointer to table of buffer |
---|
| 91 | * descriptors. */ |
---|
| 92 | void* buffers; /**< The buffer's memory. */ |
---|
| 93 | size_t buffer_min_count; /**< Number of minimum size buffers |
---|
| 94 | * that fit the buffer memory. */ |
---|
| 95 | size_t max_bds_per_group; /**< The number of BDs of minimum |
---|
| 96 | * buffer size that fit in a group. */ |
---|
| 97 | uint32_t flags; /**< Configuration flags. */ |
---|
| 98 | |
---|
| 99 | rtems_id lock; /**< The cache lock. It locks all |
---|
| 100 | * cache data, BD and lists. */ |
---|
| 101 | rtems_id sync_lock; /**< Sync calls block writes. */ |
---|
| 102 | volatile bool sync_active; /**< True if a sync is active. */ |
---|
| 103 | volatile rtems_id sync_requester; /**< The sync requester. */ |
---|
| 104 | volatile dev_t sync_device; /**< The device to sync and -1 not a |
---|
| 105 | * device sync. */ |
---|
| 106 | |
---|
| 107 | rtems_bdbuf_buffer* tree; /**< Buffer descriptor lookup AVL tree |
---|
| 108 | * root. There is only one. */ |
---|
| 109 | rtems_chain_control ready; /**< Free buffers list, read-ahead, or |
---|
| 110 | * resized group buffers. */ |
---|
| 111 | rtems_chain_control lru; /**< Least recently used list */ |
---|
| 112 | rtems_chain_control modified; /**< Modified buffers list */ |
---|
| 113 | rtems_chain_control sync; /**< Buffers to sync list */ |
---|
| 114 | |
---|
| 115 | rtems_id access; /**< Obtain if waiting for a buffer in |
---|
| 116 | * the ACCESS state. */ |
---|
| 117 | volatile uint32_t access_waiters; /**< Count of access blockers. */ |
---|
| 118 | rtems_id transfer; /**< Obtain if waiting for a buffer in |
---|
| 119 | * the TRANSFER state. */ |
---|
| 120 | volatile uint32_t transfer_waiters; /**< Count of transfer blockers. */ |
---|
| 121 | rtems_id waiting; /**< Obtain if waiting for a buffer |
---|
| 122 | * and the none are available. */ |
---|
| 123 | volatile uint32_t wait_waiters; /**< Count of waiting blockers. */ |
---|
| 124 | |
---|
| 125 | size_t group_count; /**< The number of groups. */ |
---|
| 126 | rtems_bdbuf_group* groups; /**< The groups. */ |
---|
| 127 | |
---|
| 128 | bool initialised; /**< Initialised state. */ |
---|
| 129 | } rtems_bdbuf_cache; |
---|
[3899a537] | 130 | |
---|
| 131 | /** |
---|
| 132 | * Fatal errors |
---|
[0ebfac19] | 133 | */ |
---|
[c21c850e] | 134 | #define RTEMS_BLKDEV_FATAL_ERROR(n) \ |
---|
| 135 | (((uint32_t)'B' << 24) | ((uint32_t)(n) & (uint32_t)0x00FFFFFF)) |
---|
[3899a537] | 136 | |
---|
[0d15414e] | 137 | #define RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY RTEMS_BLKDEV_FATAL_ERROR(1) |
---|
| 138 | #define RTEMS_BLKDEV_FATAL_BDBUF_SWAPOUT RTEMS_BLKDEV_FATAL_ERROR(2) |
---|
| 139 | #define RTEMS_BLKDEV_FATAL_BDBUF_SYNC_LOCK RTEMS_BLKDEV_FATAL_ERROR(3) |
---|
| 140 | #define RTEMS_BLKDEV_FATAL_BDBUF_SYNC_UNLOCK RTEMS_BLKDEV_FATAL_ERROR(4) |
---|
| 141 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_LOCK RTEMS_BLKDEV_FATAL_ERROR(5) |
---|
| 142 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_UNLOCK RTEMS_BLKDEV_FATAL_ERROR(6) |
---|
| 143 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_1 RTEMS_BLKDEV_FATAL_ERROR(7) |
---|
| 144 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_2 RTEMS_BLKDEV_FATAL_ERROR(8) |
---|
| 145 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_3 RTEMS_BLKDEV_FATAL_ERROR(9) |
---|
| 146 | #define RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAKE RTEMS_BLKDEV_FATAL_ERROR(10) |
---|
| 147 | #define RTEMS_BLKDEV_FATAL_BDBUF_SO_WAKE RTEMS_BLKDEV_FATAL_ERROR(11) |
---|
| 148 | #define RTEMS_BLKDEV_FATAL_BDBUF_SO_NOMEM RTEMS_BLKDEV_FATAL_ERROR(12) |
---|
| 149 | #define RTEMS_BLKDEV_FATAL_BDBUF_SO_WK_CREATE RTEMS_BLKDEV_FATAL_ERROR(13) |
---|
| 150 | #define RTEMS_BLKDEV_FATAL_BDBUF_SO_WK_START RTEMS_BLKDEV_FATAL_ERROR(14) |
---|
| 151 | #define BLKDEV_FATAL_BDBUF_SWAPOUT_RE RTEMS_BLKDEV_FATAL_ERROR(15) |
---|
| 152 | #define BLKDEV_FATAL_BDBUF_SWAPOUT_TS RTEMS_BLKDEV_FATAL_ERROR(16) |
---|
[3899a537] | 153 | |
---|
[c21c850e] | 154 | /** |
---|
| 155 | * The events used in this code. These should be system events rather than |
---|
| 156 | * application events. |
---|
| 157 | */ |
---|
[3899a537] | 158 | #define RTEMS_BDBUF_TRANSFER_SYNC RTEMS_EVENT_1 |
---|
| 159 | #define RTEMS_BDBUF_SWAPOUT_SYNC RTEMS_EVENT_2 |
---|
| 160 | |
---|
[c21c850e] | 161 | /** |
---|
| 162 | * The swap out task size. Should be more than enough for most drivers with |
---|
| 163 | * tracing turned on. |
---|
| 164 | */ |
---|
[3899a537] | 165 | #define SWAPOUT_TASK_STACK_SIZE (8 * 1024) |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Lock semaphore attributes. This is used for locking type mutexes. |
---|
[c21c850e] | 169 | * |
---|
| 170 | * @warning Priority inheritance is on. |
---|
[3899a537] | 171 | */ |
---|
[0d15414e] | 172 | #define RTEMS_BDBUF_CACHE_LOCK_ATTRIBS \ |
---|
[3899a537] | 173 | (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \ |
---|
| 174 | RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL) |
---|
[048dcd2b] | 175 | |
---|
[3899a537] | 176 | /** |
---|
| 177 | * Waiter semaphore attributes. |
---|
| 178 | * |
---|
[c21c850e] | 179 | * @warning Do not configure as inherit priority. If a driver is in the driver |
---|
| 180 | * initialisation table this locked semaphore will have the IDLE task |
---|
| 181 | * as the holder and a blocking task will raise the priority of the |
---|
| 182 | * IDLE task which can cause unsual side effects. |
---|
[3899a537] | 183 | */ |
---|
[0d15414e] | 184 | #define RTEMS_BDBUF_CACHE_WAITER_ATTRIBS \ |
---|
[c21c850e] | 185 | (RTEMS_PRIORITY | RTEMS_SIMPLE_BINARY_SEMAPHORE | \ |
---|
[3899a537] | 186 | RTEMS_NO_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL) |
---|
[e51bd96] | 187 | |
---|
[3899a537] | 188 | /* |
---|
| 189 | * The swap out task. |
---|
| 190 | */ |
---|
| 191 | static rtems_task rtems_bdbuf_swapout_task(rtems_task_argument arg); |
---|
[048dcd2b] | 192 | |
---|
[3899a537] | 193 | /** |
---|
[0d15414e] | 194 | * The Buffer Descriptor cache. |
---|
[3899a537] | 195 | */ |
---|
[0d15414e] | 196 | static rtems_bdbuf_cache bdbuf_cache; |
---|
[e51bd96] | 197 | |
---|
[3899a537] | 198 | /** |
---|
| 199 | * Print a message to the bdbuf trace output and flush it. |
---|
| 200 | * |
---|
| 201 | * @param format The format string. See printf for details. |
---|
| 202 | * @param ... The arguments for the format text. |
---|
| 203 | * @return int The number of bytes written to the output. |
---|
| 204 | */ |
---|
| 205 | #if RTEMS_BDBUF_TRACE |
---|
[4f971343] | 206 | bool rtems_bdbuf_tracer; |
---|
[3899a537] | 207 | static void |
---|
| 208 | rtems_bdbuf_printf (const char *format, ...) |
---|
| 209 | { |
---|
| 210 | va_list args; |
---|
| 211 | va_start (args, format); |
---|
| 212 | if (rtems_bdbuf_tracer) |
---|
| 213 | { |
---|
| 214 | fprintf (stdout, "bdbuf:"); |
---|
| 215 | vfprintf (stdout, format, args); |
---|
| 216 | fflush (stdout); |
---|
| 217 | } |
---|
| 218 | } |
---|
[e51bd96] | 219 | #endif |
---|
| 220 | |
---|
[3899a537] | 221 | /** |
---|
| 222 | * The default maximum height of 32 allows for AVL trees having between |
---|
| 223 | * 5,704,880 and 4,294,967,295 nodes, depending on order of insertion. You may |
---|
| 224 | * change this compile-time constant as you wish. |
---|
| 225 | */ |
---|
| 226 | #ifndef RTEMS_BDBUF_AVL_MAX_HEIGHT |
---|
| 227 | #define RTEMS_BDBUF_AVL_MAX_HEIGHT (32) |
---|
[e51bd96] | 228 | #endif |
---|
| 229 | |
---|
[3899a537] | 230 | /** |
---|
| 231 | * Searches for the node with specified dev/block. |
---|
[e51bd96] | 232 | * |
---|
[3899a537] | 233 | * @param root pointer to the root node of the AVL-Tree |
---|
| 234 | * @param dev device search key |
---|
| 235 | * @param block block search key |
---|
| 236 | * @retval NULL node with the specified dev/block is not found |
---|
| 237 | * @return pointer to the node with specified dev/block |
---|
[e51bd96] | 238 | */ |
---|
[3899a537] | 239 | static rtems_bdbuf_buffer * |
---|
| 240 | rtems_bdbuf_avl_search (rtems_bdbuf_buffer** root, |
---|
| 241 | dev_t dev, |
---|
| 242 | rtems_blkdev_bnum block) |
---|
[e51bd96] | 243 | { |
---|
[3899a537] | 244 | rtems_bdbuf_buffer* p = *root; |
---|
[df6348bb] | 245 | |
---|
[3899a537] | 246 | while ((p != NULL) && ((p->dev != dev) || (p->block != block))) |
---|
| 247 | { |
---|
| 248 | if ((p->dev < dev) || ((p->dev == dev) && (p->block < block))) |
---|
[e51bd96] | 249 | { |
---|
[3899a537] | 250 | p = p->avl.right; |
---|
| 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | p = p->avl.left; |
---|
[e51bd96] | 255 | } |
---|
[3899a537] | 256 | } |
---|
[048dcd2b] | 257 | |
---|
[3899a537] | 258 | return p; |
---|
[e51bd96] | 259 | } |
---|
| 260 | |
---|
[3899a537] | 261 | /** |
---|
| 262 | * Inserts the specified node to the AVl-Tree. |
---|
[e51bd96] | 263 | * |
---|
[3899a537] | 264 | * @param root pointer to the root node of the AVL-Tree |
---|
| 265 | * @param node Pointer to the node to add. |
---|
| 266 | * @retval 0 The node added successfully |
---|
| 267 | * @retval -1 An error occured |
---|
[e51bd96] | 268 | */ |
---|
| 269 | static int |
---|
[3899a537] | 270 | rtems_bdbuf_avl_insert(rtems_bdbuf_buffer** root, |
---|
| 271 | rtems_bdbuf_buffer* node) |
---|
[e51bd96] | 272 | { |
---|
[3899a537] | 273 | dev_t dev = node->dev; |
---|
| 274 | rtems_blkdev_bnum block = node->block; |
---|
| 275 | |
---|
| 276 | rtems_bdbuf_buffer* p = *root; |
---|
| 277 | rtems_bdbuf_buffer* q; |
---|
| 278 | rtems_bdbuf_buffer* p1; |
---|
| 279 | rtems_bdbuf_buffer* p2; |
---|
| 280 | rtems_bdbuf_buffer* buf_stack[RTEMS_BDBUF_AVL_MAX_HEIGHT]; |
---|
| 281 | rtems_bdbuf_buffer** buf_prev = buf_stack; |
---|
| 282 | |
---|
[4f971343] | 283 | bool modified = false; |
---|
[3899a537] | 284 | |
---|
| 285 | if (p == NULL) |
---|
| 286 | { |
---|
| 287 | *root = node; |
---|
| 288 | node->avl.left = NULL; |
---|
| 289 | node->avl.right = NULL; |
---|
| 290 | node->avl.bal = 0; |
---|
| 291 | return 0; |
---|
| 292 | } |
---|
[e51bd96] | 293 | |
---|
[3899a537] | 294 | while (p != NULL) |
---|
| 295 | { |
---|
| 296 | *buf_prev++ = p; |
---|
[e51bd96] | 297 | |
---|
[3899a537] | 298 | if ((p->dev < dev) || ((p->dev == dev) && (p->block < block))) |
---|
[e51bd96] | 299 | { |
---|
[3899a537] | 300 | p->avl.cache = 1; |
---|
| 301 | q = p->avl.right; |
---|
| 302 | if (q == NULL) |
---|
| 303 | { |
---|
| 304 | q = node; |
---|
| 305 | p->avl.right = q = node; |
---|
| 306 | break; |
---|
| 307 | } |
---|
[e51bd96] | 308 | } |
---|
[3899a537] | 309 | else if ((p->dev != dev) || (p->block != block)) |
---|
[e51bd96] | 310 | { |
---|
[3899a537] | 311 | p->avl.cache = -1; |
---|
| 312 | q = p->avl.left; |
---|
| 313 | if (q == NULL) |
---|
| 314 | { |
---|
| 315 | q = node; |
---|
| 316 | p->avl.left = q; |
---|
| 317 | break; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | else |
---|
| 321 | { |
---|
| 322 | return -1; |
---|
[e51bd96] | 323 | } |
---|
| 324 | |
---|
[3899a537] | 325 | p = q; |
---|
| 326 | } |
---|
[e51bd96] | 327 | |
---|
[3899a537] | 328 | q->avl.left = q->avl.right = NULL; |
---|
| 329 | q->avl.bal = 0; |
---|
[4f971343] | 330 | modified = true; |
---|
[3899a537] | 331 | buf_prev--; |
---|
[e51bd96] | 332 | |
---|
[3899a537] | 333 | while (modified) |
---|
| 334 | { |
---|
| 335 | if (p->avl.cache == -1) |
---|
| 336 | { |
---|
| 337 | switch (p->avl.bal) |
---|
| 338 | { |
---|
| 339 | case 1: |
---|
| 340 | p->avl.bal = 0; |
---|
[4f971343] | 341 | modified = false; |
---|
[3899a537] | 342 | break; |
---|
| 343 | |
---|
| 344 | case 0: |
---|
| 345 | p->avl.bal = -1; |
---|
| 346 | break; |
---|
| 347 | |
---|
| 348 | case -1: |
---|
| 349 | p1 = p->avl.left; |
---|
| 350 | if (p1->avl.bal == -1) /* simple LL-turn */ |
---|
| 351 | { |
---|
| 352 | p->avl.left = p1->avl.right; |
---|
| 353 | p1->avl.right = p; |
---|
| 354 | p->avl.bal = 0; |
---|
| 355 | p = p1; |
---|
| 356 | } |
---|
| 357 | else /* double LR-turn */ |
---|
| 358 | { |
---|
| 359 | p2 = p1->avl.right; |
---|
| 360 | p1->avl.right = p2->avl.left; |
---|
| 361 | p2->avl.left = p1; |
---|
| 362 | p->avl.left = p2->avl.right; |
---|
| 363 | p2->avl.right = p; |
---|
| 364 | if (p2->avl.bal == -1) p->avl.bal = +1; else p->avl.bal = 0; |
---|
| 365 | if (p2->avl.bal == +1) p1->avl.bal = -1; else p1->avl.bal = 0; |
---|
| 366 | p = p2; |
---|
| 367 | } |
---|
| 368 | p->avl.bal = 0; |
---|
[4f971343] | 369 | modified = false; |
---|
[3899a537] | 370 | break; |
---|
| 371 | |
---|
| 372 | default: |
---|
| 373 | break; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | else |
---|
| 377 | { |
---|
| 378 | switch (p->avl.bal) |
---|
| 379 | { |
---|
| 380 | case -1: |
---|
| 381 | p->avl.bal = 0; |
---|
[4f971343] | 382 | modified = false; |
---|
[3899a537] | 383 | break; |
---|
| 384 | |
---|
| 385 | case 0: |
---|
| 386 | p->avl.bal = 1; |
---|
| 387 | break; |
---|
| 388 | |
---|
| 389 | case 1: |
---|
| 390 | p1 = p->avl.right; |
---|
| 391 | if (p1->avl.bal == 1) /* simple RR-turn */ |
---|
| 392 | { |
---|
| 393 | p->avl.right = p1->avl.left; |
---|
| 394 | p1->avl.left = p; |
---|
| 395 | p->avl.bal = 0; |
---|
| 396 | p = p1; |
---|
| 397 | } |
---|
| 398 | else /* double RL-turn */ |
---|
| 399 | { |
---|
| 400 | p2 = p1->avl.left; |
---|
| 401 | p1->avl.left = p2->avl.right; |
---|
| 402 | p2->avl.right = p1; |
---|
| 403 | p->avl.right = p2->avl.left; |
---|
| 404 | p2->avl.left = p; |
---|
| 405 | if (p2->avl.bal == +1) p->avl.bal = -1; else p->avl.bal = 0; |
---|
| 406 | if (p2->avl.bal == -1) p1->avl.bal = +1; else p1->avl.bal = 0; |
---|
| 407 | p = p2; |
---|
| 408 | } |
---|
| 409 | p->avl.bal = 0; |
---|
[4f971343] | 410 | modified = false; |
---|
[3899a537] | 411 | break; |
---|
| 412 | |
---|
| 413 | default: |
---|
| 414 | break; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | q = p; |
---|
| 418 | if (buf_prev > buf_stack) |
---|
| 419 | { |
---|
| 420 | p = *--buf_prev; |
---|
| 421 | |
---|
| 422 | if (p->avl.cache == -1) |
---|
| 423 | { |
---|
| 424 | p->avl.left = q; |
---|
| 425 | } |
---|
| 426 | else |
---|
| 427 | { |
---|
| 428 | p->avl.right = q; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | else |
---|
| 432 | { |
---|
| 433 | *root = p; |
---|
| 434 | break; |
---|
| 435 | } |
---|
| 436 | }; |
---|
[e51bd96] | 437 | |
---|
[3899a537] | 438 | return 0; |
---|
[e51bd96] | 439 | } |
---|
| 440 | |
---|
| 441 | |
---|
[3899a537] | 442 | /** |
---|
| 443 | * Removes the node from the tree. |
---|
[e51bd96] | 444 | * |
---|
[57aa979] | 445 | * @param root Pointer to pointer to the root node |
---|
[3899a537] | 446 | * @param node Pointer to the node to remove |
---|
| 447 | * @retval 0 Item removed |
---|
| 448 | * @retval -1 No such item found |
---|
[e51bd96] | 449 | */ |
---|
| 450 | static int |
---|
[3899a537] | 451 | rtems_bdbuf_avl_remove(rtems_bdbuf_buffer** root, |
---|
| 452 | const rtems_bdbuf_buffer* node) |
---|
[e51bd96] | 453 | { |
---|
[3899a537] | 454 | dev_t dev = node->dev; |
---|
| 455 | rtems_blkdev_bnum block = node->block; |
---|
[e51bd96] | 456 | |
---|
[3899a537] | 457 | rtems_bdbuf_buffer* p = *root; |
---|
| 458 | rtems_bdbuf_buffer* q; |
---|
| 459 | rtems_bdbuf_buffer* r; |
---|
| 460 | rtems_bdbuf_buffer* s; |
---|
| 461 | rtems_bdbuf_buffer* p1; |
---|
| 462 | rtems_bdbuf_buffer* p2; |
---|
| 463 | rtems_bdbuf_buffer* buf_stack[RTEMS_BDBUF_AVL_MAX_HEIGHT]; |
---|
| 464 | rtems_bdbuf_buffer** buf_prev = buf_stack; |
---|
[e51bd96] | 465 | |
---|
[4f971343] | 466 | bool modified = false; |
---|
[e51bd96] | 467 | |
---|
[3899a537] | 468 | memset (buf_stack, 0, sizeof(buf_stack)); |
---|
[e51bd96] | 469 | |
---|
[3899a537] | 470 | while (p != NULL) |
---|
| 471 | { |
---|
| 472 | *buf_prev++ = p; |
---|
[e51bd96] | 473 | |
---|
[3899a537] | 474 | if ((p->dev < dev) || ((p->dev == dev) && (p->block < block))) |
---|
| 475 | { |
---|
| 476 | p->avl.cache = 1; |
---|
| 477 | p = p->avl.right; |
---|
| 478 | } |
---|
| 479 | else if ((p->dev != dev) || (p->block != block)) |
---|
| 480 | { |
---|
| 481 | p->avl.cache = -1; |
---|
| 482 | p = p->avl.left; |
---|
[df6348bb] | 483 | } |
---|
[3899a537] | 484 | else |
---|
| 485 | { |
---|
| 486 | /* node found */ |
---|
| 487 | break; |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | if (p == NULL) |
---|
| 492 | { |
---|
| 493 | /* there is no such node */ |
---|
| 494 | return -1; |
---|
| 495 | } |
---|
[048dcd2b] | 496 | |
---|
[3899a537] | 497 | q = p; |
---|
| 498 | |
---|
| 499 | buf_prev--; |
---|
| 500 | if (buf_prev > buf_stack) |
---|
| 501 | { |
---|
| 502 | p = *(buf_prev - 1); |
---|
| 503 | } |
---|
| 504 | else |
---|
| 505 | { |
---|
| 506 | p = NULL; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | /* at this moment q - is a node to delete, p is q's parent */ |
---|
| 510 | if (q->avl.right == NULL) |
---|
| 511 | { |
---|
| 512 | r = q->avl.left; |
---|
| 513 | if (r != NULL) |
---|
[df6348bb] | 514 | { |
---|
[3899a537] | 515 | r->avl.bal = 0; |
---|
[e51bd96] | 516 | } |
---|
[3899a537] | 517 | q = r; |
---|
| 518 | } |
---|
| 519 | else |
---|
| 520 | { |
---|
| 521 | rtems_bdbuf_buffer **t; |
---|
[e51bd96] | 522 | |
---|
[3899a537] | 523 | r = q->avl.right; |
---|
[df6348bb] | 524 | |
---|
[3899a537] | 525 | if (r->avl.left == NULL) |
---|
[e51bd96] | 526 | { |
---|
[3899a537] | 527 | r->avl.left = q->avl.left; |
---|
| 528 | r->avl.bal = q->avl.bal; |
---|
| 529 | r->avl.cache = 1; |
---|
| 530 | *buf_prev++ = q = r; |
---|
[e51bd96] | 531 | } |
---|
| 532 | else |
---|
| 533 | { |
---|
[3899a537] | 534 | t = buf_prev++; |
---|
| 535 | s = r; |
---|
| 536 | |
---|
| 537 | while (s->avl.left != NULL) |
---|
| 538 | { |
---|
| 539 | *buf_prev++ = r = s; |
---|
| 540 | s = r->avl.left; |
---|
| 541 | r->avl.cache = -1; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | s->avl.left = q->avl.left; |
---|
| 545 | r->avl.left = s->avl.right; |
---|
| 546 | s->avl.right = q->avl.right; |
---|
| 547 | s->avl.bal = q->avl.bal; |
---|
| 548 | s->avl.cache = 1; |
---|
| 549 | |
---|
| 550 | *t = q = s; |
---|
[e51bd96] | 551 | } |
---|
[3899a537] | 552 | } |
---|
[df6348bb] | 553 | |
---|
[3899a537] | 554 | if (p != NULL) |
---|
| 555 | { |
---|
| 556 | if (p->avl.cache == -1) |
---|
[e51bd96] | 557 | { |
---|
[3899a537] | 558 | p->avl.left = q; |
---|
[e51bd96] | 559 | } |
---|
| 560 | else |
---|
| 561 | { |
---|
[3899a537] | 562 | p->avl.right = q; |
---|
[e51bd96] | 563 | } |
---|
[3899a537] | 564 | } |
---|
| 565 | else |
---|
| 566 | { |
---|
| 567 | *root = q; |
---|
| 568 | } |
---|
[e51bd96] | 569 | |
---|
[4f971343] | 570 | modified = true; |
---|
[3899a537] | 571 | |
---|
| 572 | while (modified) |
---|
| 573 | { |
---|
| 574 | if (buf_prev > buf_stack) |
---|
[e51bd96] | 575 | { |
---|
[3899a537] | 576 | p = *--buf_prev; |
---|
[df6348bb] | 577 | } |
---|
| 578 | else |
---|
| 579 | { |
---|
[3899a537] | 580 | break; |
---|
[df6348bb] | 581 | } |
---|
[e51bd96] | 582 | |
---|
[3899a537] | 583 | if (p->avl.cache == -1) |
---|
[df6348bb] | 584 | { |
---|
[3899a537] | 585 | /* rebalance left branch */ |
---|
| 586 | switch (p->avl.bal) |
---|
| 587 | { |
---|
| 588 | case -1: |
---|
| 589 | p->avl.bal = 0; |
---|
| 590 | break; |
---|
| 591 | case 0: |
---|
| 592 | p->avl.bal = 1; |
---|
[4f971343] | 593 | modified = false; |
---|
[3899a537] | 594 | break; |
---|
| 595 | |
---|
| 596 | case +1: |
---|
| 597 | p1 = p->avl.right; |
---|
| 598 | |
---|
| 599 | if (p1->avl.bal >= 0) /* simple RR-turn */ |
---|
| 600 | { |
---|
| 601 | p->avl.right = p1->avl.left; |
---|
| 602 | p1->avl.left = p; |
---|
| 603 | |
---|
| 604 | if (p1->avl.bal == 0) |
---|
[e51bd96] | 605 | { |
---|
[3899a537] | 606 | p1->avl.bal = -1; |
---|
[4f971343] | 607 | modified = false; |
---|
[df6348bb] | 608 | } |
---|
[3899a537] | 609 | else |
---|
[e51bd96] | 610 | { |
---|
[3899a537] | 611 | p->avl.bal = 0; |
---|
| 612 | p1->avl.bal = 0; |
---|
[df6348bb] | 613 | } |
---|
[3899a537] | 614 | p = p1; |
---|
| 615 | } |
---|
| 616 | else /* double RL-turn */ |
---|
| 617 | { |
---|
| 618 | p2 = p1->avl.left; |
---|
| 619 | |
---|
| 620 | p1->avl.left = p2->avl.right; |
---|
| 621 | p2->avl.right = p1; |
---|
| 622 | p->avl.right = p2->avl.left; |
---|
| 623 | p2->avl.left = p; |
---|
| 624 | |
---|
| 625 | if (p2->avl.bal == +1) p->avl.bal = -1; else p->avl.bal = 0; |
---|
| 626 | if (p2->avl.bal == -1) p1->avl.bal = 1; else p1->avl.bal = 0; |
---|
| 627 | |
---|
| 628 | p = p2; |
---|
| 629 | p2->avl.bal = 0; |
---|
| 630 | } |
---|
| 631 | break; |
---|
| 632 | |
---|
| 633 | default: |
---|
| 634 | break; |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | else |
---|
| 638 | { |
---|
| 639 | /* rebalance right branch */ |
---|
| 640 | switch (p->avl.bal) |
---|
| 641 | { |
---|
| 642 | case +1: |
---|
| 643 | p->avl.bal = 0; |
---|
| 644 | break; |
---|
| 645 | |
---|
| 646 | case 0: |
---|
| 647 | p->avl.bal = -1; |
---|
[4f971343] | 648 | modified = false; |
---|
[3899a537] | 649 | break; |
---|
| 650 | |
---|
| 651 | case -1: |
---|
| 652 | p1 = p->avl.left; |
---|
| 653 | |
---|
| 654 | if (p1->avl.bal <= 0) /* simple LL-turn */ |
---|
| 655 | { |
---|
| 656 | p->avl.left = p1->avl.right; |
---|
| 657 | p1->avl.right = p; |
---|
| 658 | if (p1->avl.bal == 0) |
---|
[df6348bb] | 659 | { |
---|
[3899a537] | 660 | p1->avl.bal = 1; |
---|
[4f971343] | 661 | modified = false; |
---|
[df6348bb] | 662 | } |
---|
| 663 | else |
---|
| 664 | { |
---|
[3899a537] | 665 | p->avl.bal = 0; |
---|
| 666 | p1->avl.bal = 0; |
---|
[df6348bb] | 667 | } |
---|
[3899a537] | 668 | p = p1; |
---|
| 669 | } |
---|
| 670 | else /* double LR-turn */ |
---|
| 671 | { |
---|
| 672 | p2 = p1->avl.right; |
---|
| 673 | |
---|
| 674 | p1->avl.right = p2->avl.left; |
---|
| 675 | p2->avl.left = p1; |
---|
| 676 | p->avl.left = p2->avl.right; |
---|
| 677 | p2->avl.right = p; |
---|
| 678 | |
---|
| 679 | if (p2->avl.bal == -1) p->avl.bal = 1; else p->avl.bal = 0; |
---|
| 680 | if (p2->avl.bal == +1) p1->avl.bal = -1; else p1->avl.bal = 0; |
---|
| 681 | |
---|
| 682 | p = p2; |
---|
| 683 | p2->avl.bal = 0; |
---|
| 684 | } |
---|
| 685 | break; |
---|
| 686 | |
---|
| 687 | default: |
---|
| 688 | break; |
---|
| 689 | } |
---|
| 690 | } |
---|
[df6348bb] | 691 | |
---|
[3899a537] | 692 | if (buf_prev > buf_stack) |
---|
| 693 | { |
---|
| 694 | q = *(buf_prev - 1); |
---|
| 695 | |
---|
| 696 | if (q->avl.cache == -1) |
---|
| 697 | { |
---|
| 698 | q->avl.left = p; |
---|
| 699 | } |
---|
| 700 | else |
---|
| 701 | { |
---|
| 702 | q->avl.right = p; |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | else |
---|
| 706 | { |
---|
| 707 | *root = p; |
---|
| 708 | break; |
---|
[df6348bb] | 709 | } |
---|
[048dcd2b] | 710 | |
---|
[3899a537] | 711 | } |
---|
| 712 | |
---|
| 713 | return 0; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | /** |
---|
[0d15414e] | 717 | * Lock the mutex. A single task can nest calls. |
---|
[3899a537] | 718 | * |
---|
[0d15414e] | 719 | * @param lock The mutex to lock. |
---|
| 720 | * @param fatal_error_code The error code if the call fails. |
---|
[3899a537] | 721 | */ |
---|
[0d15414e] | 722 | static void |
---|
| 723 | rtems_bdbuf_lock (rtems_id lock, uint32_t fatal_error_code) |
---|
[3899a537] | 724 | { |
---|
[0d15414e] | 725 | rtems_status_code sc = rtems_semaphore_obtain (lock, |
---|
| 726 | RTEMS_WAIT, |
---|
| 727 | RTEMS_NO_TIMEOUT); |
---|
| 728 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 729 | rtems_fatal_error_occurred (fatal_error_code); |
---|
[3899a537] | 730 | } |
---|
| 731 | |
---|
| 732 | /** |
---|
[0d15414e] | 733 | * Unlock the mutex. |
---|
[3899a537] | 734 | * |
---|
[0d15414e] | 735 | * @param lock The mutex to unlock. |
---|
| 736 | * @param fatal_error_code The error code if the call fails. |
---|
[3899a537] | 737 | */ |
---|
| 738 | static void |
---|
[0d15414e] | 739 | rtems_bdbuf_unlock (rtems_id lock, uint32_t fatal_error_code) |
---|
[3899a537] | 740 | { |
---|
[0d15414e] | 741 | rtems_status_code sc = rtems_semaphore_release (lock); |
---|
[3899a537] | 742 | if (sc != RTEMS_SUCCESSFUL) |
---|
[0d15414e] | 743 | rtems_fatal_error_occurred (fatal_error_code); |
---|
[3899a537] | 744 | } |
---|
| 745 | |
---|
| 746 | /** |
---|
[0d15414e] | 747 | * Lock the cache. A single task can nest calls. |
---|
[3899a537] | 748 | */ |
---|
| 749 | static void |
---|
[0d15414e] | 750 | rtems_bdbuf_lock_cache (void) |
---|
[3899a537] | 751 | { |
---|
[0d15414e] | 752 | rtems_bdbuf_lock (bdbuf_cache.lock, RTEMS_BLKDEV_FATAL_BDBUF_CACHE_LOCK); |
---|
[3899a537] | 753 | } |
---|
| 754 | |
---|
| 755 | /** |
---|
[0d15414e] | 756 | * Unlock the cache. |
---|
[3899a537] | 757 | */ |
---|
| 758 | static void |
---|
[0d15414e] | 759 | rtems_bdbuf_unlock_cache (void) |
---|
[3899a537] | 760 | { |
---|
[0d15414e] | 761 | rtems_bdbuf_unlock (bdbuf_cache.lock, RTEMS_BLKDEV_FATAL_BDBUF_CACHE_UNLOCK); |
---|
[3899a537] | 762 | } |
---|
| 763 | |
---|
| 764 | /** |
---|
[0d15414e] | 765 | * Lock the cache's sync. A single task can nest calls. |
---|
[3899a537] | 766 | */ |
---|
| 767 | static void |
---|
[0d15414e] | 768 | rtems_bdbuf_lock_sync (void) |
---|
[3899a537] | 769 | { |
---|
[0d15414e] | 770 | rtems_bdbuf_lock (bdbuf_cache.sync_lock, RTEMS_BLKDEV_FATAL_BDBUF_SYNC_LOCK); |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | /** |
---|
| 774 | * Unlock the cache's sync lock. Any blocked writers are woken. |
---|
| 775 | */ |
---|
| 776 | static void |
---|
| 777 | rtems_bdbuf_unlock_sync (void) |
---|
| 778 | { |
---|
| 779 | rtems_bdbuf_unlock (bdbuf_cache.sync_lock, |
---|
| 780 | RTEMS_BLKDEV_FATAL_BDBUF_SYNC_UNLOCK); |
---|
[3899a537] | 781 | } |
---|
| 782 | |
---|
| 783 | /** |
---|
[c21c850e] | 784 | * Wait until woken. Semaphores are used so a number of tasks can wait and can |
---|
| 785 | * be woken at once. Task events would require we maintain a list of tasks to |
---|
| 786 | * be woken and this would require storgage and we do not know the number of |
---|
| 787 | * tasks that could be waiting. |
---|
[3899a537] | 788 | * |
---|
[0d15414e] | 789 | * While we have the cache locked we can try and claim the semaphore and |
---|
| 790 | * therefore know when we release the lock to the cache we will block until the |
---|
[3899a537] | 791 | * semaphore is released. This may even happen before we get to block. |
---|
| 792 | * |
---|
| 793 | * A counter is used to save the release call when no one is waiting. |
---|
| 794 | * |
---|
[0d15414e] | 795 | * The function assumes the cache is locked on entry and it will be locked on |
---|
[c21c850e] | 796 | * exit. |
---|
[3899a537] | 797 | * |
---|
| 798 | * @param sema The semaphore to block on and wait. |
---|
| 799 | * @param waiters The wait counter for this semaphore. |
---|
| 800 | */ |
---|
| 801 | static void |
---|
[0d15414e] | 802 | rtems_bdbuf_wait (rtems_id* sema, volatile uint32_t* waiters) |
---|
[3899a537] | 803 | { |
---|
| 804 | rtems_status_code sc; |
---|
| 805 | rtems_mode prev_mode; |
---|
| 806 | |
---|
| 807 | /* |
---|
| 808 | * Indicate we are waiting. |
---|
| 809 | */ |
---|
| 810 | *waiters += 1; |
---|
| 811 | |
---|
| 812 | /* |
---|
[0d15414e] | 813 | * Disable preemption then unlock the cache and block. There is no POSIX |
---|
| 814 | * condition variable in the core API so this is a work around. |
---|
[3899a537] | 815 | * |
---|
[0d15414e] | 816 | * The issue is a task could preempt after the cache is unlocked because it is |
---|
| 817 | * blocking or just hits that window, and before this task has blocked on the |
---|
| 818 | * semaphore. If the preempting task flushes the queue this task will not see |
---|
| 819 | * the flush and may block for ever or until another transaction flushes this |
---|
[3899a537] | 820 | * semaphore. |
---|
| 821 | */ |
---|
| 822 | sc = rtems_task_mode (RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &prev_mode); |
---|
| 823 | |
---|
| 824 | if (sc != RTEMS_SUCCESSFUL) |
---|
[0d15414e] | 825 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_1); |
---|
[3899a537] | 826 | |
---|
| 827 | /* |
---|
[0d15414e] | 828 | * Unlock the cache, wait, and lock the cache when we return. |
---|
[3899a537] | 829 | */ |
---|
[0d15414e] | 830 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 831 | |
---|
| 832 | sc = rtems_semaphore_obtain (*sema, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 833 | |
---|
| 834 | if (sc != RTEMS_UNSATISFIED) |
---|
[0d15414e] | 835 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_2); |
---|
[3899a537] | 836 | |
---|
[0d15414e] | 837 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 838 | |
---|
| 839 | sc = rtems_task_mode (prev_mode, RTEMS_ALL_MODE_MASKS, &prev_mode); |
---|
| 840 | |
---|
| 841 | if (sc != RTEMS_SUCCESSFUL) |
---|
[0d15414e] | 842 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAIT_3); |
---|
[3899a537] | 843 | |
---|
| 844 | *waiters -= 1; |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | /** |
---|
| 848 | * Wake a blocked resource. The resource has a counter that lets us know if |
---|
| 849 | * there are any waiters. |
---|
| 850 | * |
---|
| 851 | * @param sema The semaphore to release. |
---|
| 852 | * @param waiters The wait counter for this semaphore. |
---|
| 853 | */ |
---|
| 854 | static void |
---|
| 855 | rtems_bdbuf_wake (rtems_id sema, volatile uint32_t* waiters) |
---|
| 856 | { |
---|
| 857 | if (*waiters) |
---|
| 858 | { |
---|
| 859 | rtems_status_code sc; |
---|
| 860 | |
---|
| 861 | sc = rtems_semaphore_flush (sema); |
---|
| 862 | |
---|
| 863 | if (sc != RTEMS_SUCCESSFUL) |
---|
[0d15414e] | 864 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CACHE_WAKE); |
---|
[3899a537] | 865 | } |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /** |
---|
| 869 | * Add a buffer descriptor to the modified list. This modified list is treated |
---|
[0d15414e] | 870 | * a litte differently to the other lists. To access it you must have the cache |
---|
[c21c850e] | 871 | * locked and this is assumed to be the case on entry to this call. |
---|
| 872 | * |
---|
[0d15414e] | 873 | * If the cache has a device being sync'ed and the bd is for that device the |
---|
[c21c850e] | 874 | * call must block and wait until the sync is over before adding the bd to the |
---|
| 875 | * modified list. Once a sync happens for a device no bd's can be added the |
---|
| 876 | * modified list. The disk image is forced to be snapshot at that moment in |
---|
| 877 | * time. |
---|
| 878 | * |
---|
| 879 | * and you must |
---|
[3899a537] | 880 | * hold the sync lock. The sync lock is used to block writes while a sync is |
---|
| 881 | * active. |
---|
| 882 | * |
---|
[0d15414e] | 883 | * @param bd The bd to queue to the cache's modified list. |
---|
[3899a537] | 884 | */ |
---|
| 885 | static void |
---|
[0d15414e] | 886 | rtems_bdbuf_append_modified (rtems_bdbuf_buffer* bd) |
---|
[3899a537] | 887 | { |
---|
| 888 | /* |
---|
[0d15414e] | 889 | * If the cache has a device being sync'ed check if this bd is for that |
---|
| 890 | * device. If it is unlock the cache and block on the sync lock. Once we have |
---|
| 891 | * the sync lock release it. |
---|
[3899a537] | 892 | */ |
---|
[0d15414e] | 893 | if (bdbuf_cache.sync_active && (bdbuf_cache.sync_device == bd->dev)) |
---|
[3899a537] | 894 | { |
---|
[0d15414e] | 895 | rtems_bdbuf_unlock_cache (); |
---|
| 896 | /* Wait for the sync lock */ |
---|
| 897 | rtems_bdbuf_lock_sync (); |
---|
| 898 | rtems_bdbuf_unlock_sync (); |
---|
| 899 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 900 | } |
---|
| 901 | |
---|
| 902 | bd->state = RTEMS_BDBUF_STATE_MODIFIED; |
---|
| 903 | |
---|
[0d15414e] | 904 | rtems_chain_append (&bdbuf_cache.modified, &bd->link); |
---|
[3899a537] | 905 | } |
---|
| 906 | |
---|
[c21c850e] | 907 | /** |
---|
| 908 | * Wait the swapper task. |
---|
| 909 | */ |
---|
[3899a537] | 910 | static void |
---|
[a5fb40cf] | 911 | rtems_bdbuf_wake_swapper (void) |
---|
[3899a537] | 912 | { |
---|
[0d15414e] | 913 | rtems_status_code sc = rtems_event_send (bdbuf_cache.swapout, |
---|
[3899a537] | 914 | RTEMS_BDBUF_SWAPOUT_SYNC); |
---|
| 915 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 916 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_WAKE); |
---|
[e51bd96] | 917 | } |
---|
| 918 | |
---|
[c21c850e] | 919 | /** |
---|
[0d15414e] | 920 | * Compute the number of BDs per group for a given buffer size. |
---|
[e51bd96] | 921 | * |
---|
[0d15414e] | 922 | * @param size The buffer size. It can be any size and we scale up. |
---|
| 923 | */ |
---|
| 924 | static size_t |
---|
| 925 | rtems_bdbuf_bds_per_group (size_t size) |
---|
| 926 | { |
---|
| 927 | size_t bufs_per_size; |
---|
| 928 | size_t bds_per_size; |
---|
| 929 | |
---|
| 930 | if (size > rtems_bdbuf_configuration.buffer_max) |
---|
| 931 | return 0; |
---|
| 932 | |
---|
| 933 | bufs_per_size = ((size - 1) / bdbuf_config.buffer_min) + 1; |
---|
| 934 | |
---|
| 935 | for (bds_per_size = 1; |
---|
| 936 | bds_per_size < bufs_per_size; |
---|
| 937 | bds_per_size <<= 1) |
---|
| 938 | ; |
---|
| 939 | |
---|
| 940 | return bdbuf_cache.max_bds_per_group / bds_per_size; |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | /** |
---|
| 944 | * Reallocate a group. The BDs currently allocated in the group are removed |
---|
| 945 | * from the ALV tree and any lists then the new BD's are prepended to the ready |
---|
| 946 | * list of the cache. |
---|
[e51bd96] | 947 | * |
---|
[0d15414e] | 948 | * @param group The group to reallocate. |
---|
| 949 | * @param new_bds_per_group The new count of BDs per group. |
---|
[e51bd96] | 950 | */ |
---|
[0d15414e] | 951 | static void |
---|
| 952 | rtems_bdbuf_group_realloc (rtems_bdbuf_group* group, size_t new_bds_per_group) |
---|
[e51bd96] | 953 | { |
---|
[3899a537] | 954 | rtems_bdbuf_buffer* bd; |
---|
[0d15414e] | 955 | int b; |
---|
| 956 | size_t bufs_per_bd; |
---|
[57aa979] | 957 | |
---|
[0d15414e] | 958 | bufs_per_bd = bdbuf_cache.max_bds_per_group / group->bds_per_group; |
---|
| 959 | |
---|
| 960 | for (b = 0, bd = group->bdbuf; |
---|
| 961 | b < group->bds_per_group; |
---|
| 962 | b++, bd += bufs_per_bd) |
---|
[57aa979] | 963 | { |
---|
[0d15414e] | 964 | if ((bd->state == RTEMS_BDBUF_STATE_CACHED) || |
---|
| 965 | (bd->state == RTEMS_BDBUF_STATE_MODIFIED) || |
---|
| 966 | (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD)) |
---|
| 967 | { |
---|
| 968 | if (rtems_bdbuf_avl_remove (&bdbuf_cache.tree, bd) != 0) |
---|
| 969 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
| 970 | rtems_chain_extract (&bd->link); |
---|
| 971 | } |
---|
[57aa979] | 972 | } |
---|
[3899a537] | 973 | |
---|
[0d15414e] | 974 | group->bds_per_group = new_bds_per_group; |
---|
| 975 | bufs_per_bd = bdbuf_cache.max_bds_per_group / new_bds_per_group; |
---|
| 976 | |
---|
| 977 | for (b = 0, bd = group->bdbuf; |
---|
| 978 | b < group->bds_per_group; |
---|
| 979 | b++, bd += bufs_per_bd) |
---|
| 980 | rtems_chain_prepend (&bdbuf_cache.ready, &bd->link); |
---|
| 981 | } |
---|
| 982 | |
---|
| 983 | /** |
---|
| 984 | * Get the next BD from the list. This call assumes the cache is locked. |
---|
| 985 | * |
---|
| 986 | * @param bds_per_group The number of BDs per block we are need. |
---|
| 987 | * @param list The list to find the BD on. |
---|
| 988 | * @return The next BD if found or NULL is none are available. |
---|
| 989 | */ |
---|
| 990 | static rtems_bdbuf_buffer* |
---|
| 991 | rtems_bdbuf_get_next_bd (size_t bds_per_group, |
---|
| 992 | rtems_chain_control* list) |
---|
| 993 | { |
---|
| 994 | rtems_chain_node* node = rtems_chain_first (list); |
---|
| 995 | while (!rtems_chain_is_tail (list, node)) |
---|
| 996 | { |
---|
| 997 | rtems_bdbuf_buffer* bd = (rtems_bdbuf_buffer*) node; |
---|
| 998 | |
---|
| 999 | /* |
---|
| 1000 | * If this bd is already part of a group that supports the same number of |
---|
| 1001 | * BDs per group return it. If the bd is part of another group check the |
---|
| 1002 | * number of users and if 0 we can take this group and resize it. |
---|
| 1003 | */ |
---|
| 1004 | if (bd->group->bds_per_group == bds_per_group) |
---|
| 1005 | { |
---|
| 1006 | rtems_chain_extract (node); |
---|
| 1007 | bd->group->users++; |
---|
| 1008 | return bd; |
---|
| 1009 | } |
---|
| 1010 | |
---|
| 1011 | if (bd->group->users == 0) |
---|
| 1012 | { |
---|
| 1013 | /* |
---|
| 1014 | * We use the group to locate the start of the BDs for this group. |
---|
| 1015 | */ |
---|
| 1016 | rtems_bdbuf_group_realloc (bd->group, bds_per_group); |
---|
| 1017 | bd = (rtems_bdbuf_buffer*) rtems_chain_get (&bdbuf_cache.ready); |
---|
| 1018 | return bd; |
---|
| 1019 | } |
---|
| 1020 | |
---|
| 1021 | node = rtems_chain_next (node); |
---|
| 1022 | } |
---|
[3899a537] | 1023 | |
---|
[0d15414e] | 1024 | return NULL; |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | /** |
---|
| 1028 | * Initialise the cache. |
---|
| 1029 | * |
---|
| 1030 | * @return rtems_status_code The initialisation status. |
---|
| 1031 | */ |
---|
| 1032 | rtems_status_code |
---|
| 1033 | rtems_bdbuf_init (void) |
---|
| 1034 | { |
---|
| 1035 | rtems_bdbuf_group* group; |
---|
| 1036 | rtems_bdbuf_buffer* bd; |
---|
| 1037 | uint8_t* buffer; |
---|
| 1038 | int b; |
---|
| 1039 | int cache_aligment; |
---|
| 1040 | rtems_status_code sc; |
---|
| 1041 | |
---|
| 1042 | #if RTEMS_BDBUF_TRACE |
---|
| 1043 | rtems_bdbuf_printf ("init\n"); |
---|
| 1044 | #endif |
---|
| 1045 | |
---|
[3899a537] | 1046 | /* |
---|
[0d15414e] | 1047 | * Check the configuration table values. |
---|
[3899a537] | 1048 | */ |
---|
[0d15414e] | 1049 | if ((bdbuf_config.buffer_max % bdbuf_config.buffer_min) != 0) |
---|
| 1050 | return RTEMS_INVALID_NUMBER; |
---|
[3899a537] | 1051 | |
---|
[0d15414e] | 1052 | /* |
---|
| 1053 | * We use a special variable to manage the initialisation incase we have |
---|
| 1054 | * completing threads doing this. You may get errors if the another thread |
---|
| 1055 | * makes a call and we have not finished initialisation. |
---|
| 1056 | */ |
---|
| 1057 | if (bdbuf_cache.initialised) |
---|
| 1058 | return RTEMS_RESOURCE_IN_USE; |
---|
[3899a537] | 1059 | |
---|
[0d15414e] | 1060 | bdbuf_cache.initialised = true; |
---|
| 1061 | |
---|
[3899a537] | 1062 | /* |
---|
[0d15414e] | 1063 | * For unspecified cache alignments we use the CPU alignment. |
---|
[3899a537] | 1064 | */ |
---|
[0d15414e] | 1065 | cache_aligment = 32; /* FIXME rtems_cache_get_data_line_size() */ |
---|
| 1066 | if (cache_aligment <= 0) |
---|
| 1067 | cache_aligment = CPU_ALIGNMENT; |
---|
[048dcd2b] | 1068 | |
---|
[0d15414e] | 1069 | bdbuf_cache.sync_active = false; |
---|
| 1070 | bdbuf_cache.sync_device = -1; |
---|
| 1071 | bdbuf_cache.sync_requester = 0; |
---|
| 1072 | bdbuf_cache.tree = NULL; |
---|
| 1073 | |
---|
| 1074 | rtems_chain_initialize_empty (&bdbuf_cache.swapout_workers); |
---|
| 1075 | rtems_chain_initialize_empty (&bdbuf_cache.ready); |
---|
| 1076 | rtems_chain_initialize_empty (&bdbuf_cache.lru); |
---|
| 1077 | rtems_chain_initialize_empty (&bdbuf_cache.modified); |
---|
| 1078 | rtems_chain_initialize_empty (&bdbuf_cache.sync); |
---|
| 1079 | |
---|
| 1080 | bdbuf_cache.access = 0; |
---|
| 1081 | bdbuf_cache.access_waiters = 0; |
---|
| 1082 | bdbuf_cache.transfer = 0; |
---|
| 1083 | bdbuf_cache.transfer_waiters = 0; |
---|
| 1084 | bdbuf_cache.waiting = 0; |
---|
| 1085 | bdbuf_cache.wait_waiters = 0; |
---|
[048dcd2b] | 1086 | |
---|
[0d15414e] | 1087 | /* |
---|
| 1088 | * Create the locks for the cache. |
---|
| 1089 | */ |
---|
| 1090 | sc = rtems_semaphore_create (rtems_build_name ('B', 'D', 'C', 'l'), |
---|
| 1091 | 1, RTEMS_BDBUF_CACHE_LOCK_ATTRIBS, 0, |
---|
| 1092 | &bdbuf_cache.lock); |
---|
[3899a537] | 1093 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1094 | { |
---|
[0d15414e] | 1095 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1096 | return sc; |
---|
| 1097 | } |
---|
[048dcd2b] | 1098 | |
---|
[0d15414e] | 1099 | rtems_bdbuf_lock_cache (); |
---|
| 1100 | |
---|
| 1101 | sc = rtems_semaphore_create (rtems_build_name ('B', 'D', 'C', 's'), |
---|
| 1102 | 1, RTEMS_BDBUF_CACHE_LOCK_ATTRIBS, 0, |
---|
| 1103 | &bdbuf_cache.sync_lock); |
---|
[3899a537] | 1104 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1105 | { |
---|
[0d15414e] | 1106 | rtems_bdbuf_unlock_cache (); |
---|
| 1107 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1108 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1109 | return sc; |
---|
| 1110 | } |
---|
| 1111 | |
---|
[0d15414e] | 1112 | sc = rtems_semaphore_create (rtems_build_name ('B', 'D', 'C', 'a'), |
---|
| 1113 | 0, RTEMS_BDBUF_CACHE_WAITER_ATTRIBS, 0, |
---|
| 1114 | &bdbuf_cache.access); |
---|
[3899a537] | 1115 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1116 | { |
---|
[0d15414e] | 1117 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1118 | rtems_bdbuf_unlock_cache (); |
---|
| 1119 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1120 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1121 | return sc; |
---|
| 1122 | } |
---|
[048dcd2b] | 1123 | |
---|
[0d15414e] | 1124 | sc = rtems_semaphore_create (rtems_build_name ('B', 'D', 'C', 't'), |
---|
| 1125 | 0, RTEMS_BDBUF_CACHE_WAITER_ATTRIBS, 0, |
---|
| 1126 | &bdbuf_cache.transfer); |
---|
[3899a537] | 1127 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1128 | { |
---|
[0d15414e] | 1129 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1130 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1131 | rtems_bdbuf_unlock_cache (); |
---|
| 1132 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1133 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1134 | return sc; |
---|
| 1135 | } |
---|
| 1136 | |
---|
[0d15414e] | 1137 | sc = rtems_semaphore_create (rtems_build_name ('B', 'D', 'C', 'w'), |
---|
| 1138 | 0, RTEMS_BDBUF_CACHE_WAITER_ATTRIBS, 0, |
---|
| 1139 | &bdbuf_cache.waiting); |
---|
[3899a537] | 1140 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1141 | { |
---|
[0d15414e] | 1142 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1143 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1144 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1145 | rtems_bdbuf_unlock_cache (); |
---|
| 1146 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1147 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1148 | return sc; |
---|
| 1149 | } |
---|
| 1150 | |
---|
[0d15414e] | 1151 | /* |
---|
| 1152 | * Allocate the memory for the buffer descriptors. |
---|
| 1153 | */ |
---|
| 1154 | bdbuf_cache.bds = calloc (sizeof (rtems_bdbuf_buffer), |
---|
| 1155 | bdbuf_config.size / bdbuf_config.buffer_min); |
---|
| 1156 | if (!bdbuf_cache.bds) |
---|
| 1157 | { |
---|
| 1158 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1159 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1160 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1161 | rtems_bdbuf_unlock_cache (); |
---|
| 1162 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1163 | bdbuf_cache.initialised = false; |
---|
| 1164 | return RTEMS_NO_MEMORY; |
---|
| 1165 | } |
---|
[e51bd96] | 1166 | |
---|
[0d15414e] | 1167 | /* |
---|
| 1168 | * Compute the various number of elements in the cache. |
---|
| 1169 | */ |
---|
| 1170 | bdbuf_cache.buffer_min_count = |
---|
| 1171 | bdbuf_config.size / bdbuf_config.buffer_min; |
---|
| 1172 | bdbuf_cache.max_bds_per_group = |
---|
| 1173 | bdbuf_config.buffer_max / bdbuf_config.buffer_min; |
---|
| 1174 | bdbuf_cache.group_count = |
---|
| 1175 | bdbuf_cache.buffer_min_count / bdbuf_cache.max_bds_per_group; |
---|
[048dcd2b] | 1176 | |
---|
[3899a537] | 1177 | /* |
---|
[0d15414e] | 1178 | * Allocate the memory for the buffer descriptors. |
---|
[3899a537] | 1179 | */ |
---|
[0d15414e] | 1180 | bdbuf_cache.groups = calloc (sizeof (rtems_bdbuf_group), |
---|
| 1181 | bdbuf_cache.group_count); |
---|
| 1182 | if (!bdbuf_cache.groups) |
---|
| 1183 | { |
---|
| 1184 | free (bdbuf_cache.bds); |
---|
| 1185 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1186 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1187 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1188 | rtems_bdbuf_unlock_cache (); |
---|
| 1189 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1190 | bdbuf_cache.initialised = false; |
---|
| 1191 | return RTEMS_NO_MEMORY; |
---|
| 1192 | } |
---|
[3899a537] | 1193 | |
---|
[0d15414e] | 1194 | /* |
---|
| 1195 | * Allocate memory for buffer memory. The buffer memory will be cache |
---|
| 1196 | * aligned. It is possible to free the memory allocated by rtems_memalign() |
---|
| 1197 | * with free(). Return 0 if allocated. |
---|
| 1198 | */ |
---|
| 1199 | if (rtems_memalign ((void **) &bdbuf_cache.buffers, |
---|
| 1200 | cache_aligment, |
---|
| 1201 | bdbuf_cache.buffer_min_count * bdbuf_config.buffer_min) != 0) |
---|
| 1202 | { |
---|
| 1203 | free (bdbuf_cache.groups); |
---|
| 1204 | free (bdbuf_cache.bds); |
---|
| 1205 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1206 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1207 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1208 | rtems_bdbuf_unlock_cache (); |
---|
| 1209 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1210 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1211 | return RTEMS_NO_MEMORY; |
---|
[0d15414e] | 1212 | } |
---|
[3899a537] | 1213 | |
---|
| 1214 | /* |
---|
[0d15414e] | 1215 | * The cache is empty after opening so we need to add all the buffers to it |
---|
| 1216 | * and initialise the groups. |
---|
[3899a537] | 1217 | */ |
---|
[0d15414e] | 1218 | for (b = 0, group = bdbuf_cache.groups, |
---|
| 1219 | bd = bdbuf_cache.bds, buffer = bdbuf_cache.buffers; |
---|
| 1220 | b < bdbuf_cache.buffer_min_count; |
---|
| 1221 | b++, bd++, buffer += bdbuf_config.buffer_min) |
---|
[3899a537] | 1222 | { |
---|
[0d15414e] | 1223 | bd->dev = -1; |
---|
| 1224 | bd->group = group; |
---|
| 1225 | bd->buffer = buffer; |
---|
| 1226 | bd->avl.left = NULL; |
---|
| 1227 | bd->avl.right = NULL; |
---|
| 1228 | bd->state = RTEMS_BDBUF_STATE_EMPTY; |
---|
| 1229 | bd->error = 0; |
---|
| 1230 | bd->waiters = 0; |
---|
| 1231 | bd->hold_timer = 0; |
---|
| 1232 | |
---|
| 1233 | rtems_chain_append (&bdbuf_cache.ready, &bd->link); |
---|
| 1234 | |
---|
| 1235 | if ((b % bdbuf_cache.max_bds_per_group) == |
---|
| 1236 | (bdbuf_cache.max_bds_per_group - 1)) |
---|
| 1237 | group++; |
---|
[3899a537] | 1238 | } |
---|
[e51bd96] | 1239 | |
---|
[0d15414e] | 1240 | for (b = 0, |
---|
| 1241 | group = bdbuf_cache.groups, |
---|
| 1242 | bd = bdbuf_cache.bds; |
---|
| 1243 | b < bdbuf_cache.group_count; |
---|
| 1244 | b++, |
---|
| 1245 | group++, |
---|
| 1246 | bd += bdbuf_cache.max_bds_per_group) |
---|
| 1247 | { |
---|
| 1248 | group->bds_per_group = bdbuf_cache.max_bds_per_group; |
---|
| 1249 | group->users = 0; |
---|
| 1250 | group->bdbuf = bd; |
---|
| 1251 | } |
---|
| 1252 | |
---|
[3899a537] | 1253 | /* |
---|
[0d15414e] | 1254 | * Create and start swapout task. This task will create and manage the worker |
---|
| 1255 | * threads. |
---|
[3899a537] | 1256 | */ |
---|
[0d15414e] | 1257 | bdbuf_cache.swapout_enabled = true; |
---|
[3899a537] | 1258 | |
---|
| 1259 | sc = rtems_task_create (rtems_build_name('B', 'S', 'W', 'P'), |
---|
[0d15414e] | 1260 | (bdbuf_config.swapout_priority ? |
---|
| 1261 | bdbuf_config.swapout_priority : |
---|
[3899a537] | 1262 | RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT), |
---|
| 1263 | SWAPOUT_TASK_STACK_SIZE, |
---|
| 1264 | RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR, |
---|
| 1265 | RTEMS_LOCAL | RTEMS_NO_FLOATING_POINT, |
---|
[0d15414e] | 1266 | &bdbuf_cache.swapout); |
---|
[3899a537] | 1267 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1268 | { |
---|
[0d15414e] | 1269 | free (bdbuf_cache.buffers); |
---|
| 1270 | free (bdbuf_cache.groups); |
---|
| 1271 | free (bdbuf_cache.bds); |
---|
| 1272 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1273 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1274 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1275 | rtems_bdbuf_unlock_cache (); |
---|
| 1276 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1277 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1278 | return sc; |
---|
| 1279 | } |
---|
[e51bd96] | 1280 | |
---|
[0d15414e] | 1281 | sc = rtems_task_start (bdbuf_cache.swapout, |
---|
[3899a537] | 1282 | rtems_bdbuf_swapout_task, |
---|
[0d15414e] | 1283 | (rtems_task_argument) &bdbuf_cache); |
---|
[3899a537] | 1284 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1285 | { |
---|
[0d15414e] | 1286 | rtems_task_delete (bdbuf_cache.swapout); |
---|
| 1287 | free (bdbuf_cache.buffers); |
---|
| 1288 | free (bdbuf_cache.groups); |
---|
| 1289 | free (bdbuf_cache.bds); |
---|
| 1290 | rtems_semaphore_delete (bdbuf_cache.transfer); |
---|
| 1291 | rtems_semaphore_delete (bdbuf_cache.access); |
---|
| 1292 | rtems_semaphore_delete (bdbuf_cache.sync_lock); |
---|
| 1293 | rtems_bdbuf_unlock_cache (); |
---|
| 1294 | rtems_semaphore_delete (bdbuf_cache.lock); |
---|
| 1295 | bdbuf_cache.initialised = false; |
---|
[3899a537] | 1296 | return sc; |
---|
| 1297 | } |
---|
[e51bd96] | 1298 | |
---|
[0d15414e] | 1299 | rtems_bdbuf_unlock_cache (); |
---|
| 1300 | |
---|
[3899a537] | 1301 | return RTEMS_SUCCESSFUL; |
---|
[e51bd96] | 1302 | } |
---|
| 1303 | |
---|
[3899a537] | 1304 | /** |
---|
| 1305 | * Get a buffer for this device and block. This function returns a buffer once |
---|
| 1306 | * placed into the AVL tree. If no buffer is available and it is not a read |
---|
[0d15414e] | 1307 | * ahead request and no buffers are waiting to the written to disk wait until a |
---|
| 1308 | * buffer is available. If buffers are waiting to be written to disk and none |
---|
| 1309 | * are available expire the hold timer's of the queued buffers and wake the |
---|
| 1310 | * swap out task. If the buffer is for a read ahead transfer return NULL if |
---|
| 1311 | * there are no buffers available or the buffer is already in the cache. |
---|
[e51bd96] | 1312 | * |
---|
[0d15414e] | 1313 | * The AVL tree of buffers for the cache is searched and if not found obtain a |
---|
| 1314 | * buffer and insert it into the AVL tree. Buffers are first obtained from the |
---|
| 1315 | * ready list until all empty/ready buffers are used. Once all buffers are in |
---|
| 1316 | * use the LRU list is searched for a buffer of the same group size or a group |
---|
| 1317 | * that has no active buffers in use. A buffer taken from the LRU list is |
---|
| 1318 | * removed from the AVL tree and assigned the new block number. The ready or |
---|
| 1319 | * LRU list buffer is initialised to this device and block. If no buffers are |
---|
| 1320 | * available due to the ready and LRU lists being empty a check is made of the |
---|
| 1321 | * modified list. Buffers may be queued waiting for the hold timer to |
---|
| 1322 | * expire. These buffers should be written to disk and returned to the LRU list |
---|
| 1323 | * where they can be used. If buffers are on the modified list the max. write |
---|
| 1324 | * block size of buffers have their hold timer's expired and the swap out task |
---|
| 1325 | * woken. The caller then blocks on the waiting semaphore and counter. When |
---|
| 1326 | * buffers return from the upper layers (access) or lower driver (transfer) the |
---|
| 1327 | * blocked caller task is woken and this procedure is repeated. The repeat |
---|
| 1328 | * handles a case of a another thread pre-empting getting a buffer first and |
---|
| 1329 | * adding it to the AVL tree. |
---|
[3899a537] | 1330 | * |
---|
| 1331 | * A buffer located in the AVL tree means it is already in the cache and maybe |
---|
| 1332 | * in use somewhere. The buffer can be either: |
---|
[e51bd96] | 1333 | * |
---|
[3899a537] | 1334 | * # Cached. Not being accessed or part of a media transfer. |
---|
[c21c850e] | 1335 | * # Access or modifed access. Is with an upper layer being accessed. |
---|
[3899a537] | 1336 | * # Transfer. Is with the driver and part of a media transfer. |
---|
| 1337 | * |
---|
[c21c850e] | 1338 | * If cached we assign the new state, extract it from any list it maybe part of |
---|
| 1339 | * and return to the user. |
---|
[3899a537] | 1340 | * |
---|
[0d15414e] | 1341 | * This function assumes the cache the buffer is being taken from is locked and |
---|
| 1342 | * it will make sure the cache is locked when it returns. The cache will be |
---|
[c21c850e] | 1343 | * unlocked if the call could block. |
---|
[3899a537] | 1344 | * |
---|
[0d15414e] | 1345 | * Variable sized buffer is handled by groups. A group is the size of the |
---|
| 1346 | * maximum buffer that can be allocated. The group can size in multiples of the |
---|
| 1347 | * minimum buffer size where the mulitples are 1,2,4,8, etc. If the buffer is |
---|
| 1348 | * found in the AVL tree the number of BDs in the group is check and if |
---|
| 1349 | * different the buffer size for the block has changed. The buffer needs to be |
---|
| 1350 | * invalidated. |
---|
[e51bd96] | 1351 | * |
---|
[0d15414e] | 1352 | * @param dd The disk device. Has the configured block size. |
---|
| 1353 | * @param bds_per_group The number of BDs in a group for this block. |
---|
| 1354 | * @param block Absolute media block number for the device |
---|
| 1355 | * @param read_ahead The get is for a read ahead buffer if true |
---|
| 1356 | * @return RTEMS status code (if operation completed successfully or error |
---|
[c21c850e] | 1357 | * code if error is occured) |
---|
[e51bd96] | 1358 | */ |
---|
[3899a537] | 1359 | static rtems_bdbuf_buffer* |
---|
[0d15414e] | 1360 | rtems_bdbuf_get_buffer (rtems_disk_device* dd, |
---|
| 1361 | size_t bds_per_group, |
---|
[3899a537] | 1362 | rtems_blkdev_bnum block, |
---|
[4f971343] | 1363 | bool read_ahead) |
---|
[e51bd96] | 1364 | { |
---|
[0d15414e] | 1365 | dev_t device = dd->dev; |
---|
[3899a537] | 1366 | rtems_bdbuf_buffer* bd; |
---|
[4f971343] | 1367 | bool available; |
---|
[0d15414e] | 1368 | |
---|
[3899a537] | 1369 | /* |
---|
| 1370 | * Loop until we get a buffer. Under load we could find no buffers are |
---|
[c21c850e] | 1371 | * available requiring this task to wait until some become available before |
---|
[0d15414e] | 1372 | * proceeding. There is no timeout. If this call is to block and the buffer |
---|
| 1373 | * is for a read ahead buffer return NULL. The read ahead is nice but not |
---|
| 1374 | * that important. |
---|
[3899a537] | 1375 | * |
---|
| 1376 | * The search procedure is repeated as another thread could have pre-empted |
---|
| 1377 | * us while we waited for a buffer, obtained an empty buffer and loaded the |
---|
[0d15414e] | 1378 | * AVL tree with the one we are after. In this case we move down and wait for |
---|
| 1379 | * the buffer to return to the cache. |
---|
[3899a537] | 1380 | */ |
---|
| 1381 | do |
---|
| 1382 | { |
---|
| 1383 | /* |
---|
| 1384 | * Search for buffer descriptor for this dev/block key. |
---|
| 1385 | */ |
---|
[0d15414e] | 1386 | bd = rtems_bdbuf_avl_search (&bdbuf_cache.tree, device, block); |
---|
[048dcd2b] | 1387 | |
---|
[3899a537] | 1388 | /* |
---|
| 1389 | * No buffer in the cache for this block. We need to obtain a buffer and |
---|
| 1390 | * this means take a buffer that is ready to use. If all buffers are in use |
---|
| 1391 | * take the least recently used buffer. If there are none then the cache is |
---|
| 1392 | * empty. All the buffers are either queued to be written to disk or with |
---|
| 1393 | * the user. We cannot do much with the buffers with the user how-ever with |
---|
| 1394 | * the modified buffers waiting to be written to disk flush the maximum |
---|
[c21c850e] | 1395 | * number transfered in a block to disk. After this all that can be done is |
---|
| 1396 | * to wait for a buffer to return to the cache. |
---|
[3899a537] | 1397 | */ |
---|
| 1398 | if (!bd) |
---|
[e51bd96] | 1399 | { |
---|
[3899a537] | 1400 | /* |
---|
[0d15414e] | 1401 | * Assign new buffer descriptor from the ready list if one is present. If |
---|
| 1402 | * the ready queue is empty get the oldest buffer from LRU list. If the |
---|
[c21c850e] | 1403 | * LRU list is empty there are no available buffers check the modified |
---|
| 1404 | * list. |
---|
[3899a537] | 1405 | */ |
---|
[0d15414e] | 1406 | bd = rtems_bdbuf_get_next_bd (bds_per_group, &bdbuf_cache.ready); |
---|
| 1407 | |
---|
| 1408 | if (!bd) |
---|
[3899a537] | 1409 | { |
---|
| 1410 | /* |
---|
[0d15414e] | 1411 | * No unused or read-ahead buffers. |
---|
[3899a537] | 1412 | * |
---|
[c21c850e] | 1413 | * If this is a read ahead buffer just return. No need to place further |
---|
| 1414 | * pressure on the cache by reading something that may be needed when |
---|
[0d15414e] | 1415 | * we have data in the cache that was needed and may still be in the |
---|
| 1416 | * future. |
---|
[3899a537] | 1417 | */ |
---|
| 1418 | if (read_ahead) |
---|
| 1419 | return NULL; |
---|
| 1420 | |
---|
| 1421 | /* |
---|
| 1422 | * Check the LRU list. |
---|
| 1423 | */ |
---|
[0d15414e] | 1424 | bd = rtems_bdbuf_get_next_bd (bds_per_group, &bdbuf_cache.lru); |
---|
[3899a537] | 1425 | |
---|
| 1426 | if (bd) |
---|
[e51bd96] | 1427 | { |
---|
[3899a537] | 1428 | /* |
---|
| 1429 | * Remove the buffer from the AVL tree. |
---|
| 1430 | */ |
---|
[0d15414e] | 1431 | if (rtems_bdbuf_avl_remove (&bdbuf_cache.tree, bd) != 0) |
---|
[3899a537] | 1432 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
[e51bd96] | 1433 | } |
---|
| 1434 | else |
---|
| 1435 | { |
---|
[3899a537] | 1436 | /* |
---|
| 1437 | * If there are buffers on the modified list expire the hold timer |
---|
[c21c850e] | 1438 | * and wake the swap out task then wait else just go and wait. |
---|
[0d15414e] | 1439 | * |
---|
| 1440 | * The check for an empty list is made so the swapper is only woken |
---|
| 1441 | * when if timers are changed. |
---|
[3899a537] | 1442 | */ |
---|
[0d15414e] | 1443 | if (!rtems_chain_is_empty (&bdbuf_cache.modified)) |
---|
[3899a537] | 1444 | { |
---|
[0d15414e] | 1445 | rtems_chain_node* node = rtems_chain_first (&bdbuf_cache.modified); |
---|
[cec5c069] | 1446 | uint32_t write_blocks = 0; |
---|
[3899a537] | 1447 | |
---|
[0d15414e] | 1448 | while ((write_blocks < bdbuf_config.max_write_blocks) && |
---|
| 1449 | !rtems_chain_is_tail (&bdbuf_cache.modified, node)) |
---|
[e51bd96] | 1450 | { |
---|
[3899a537] | 1451 | rtems_bdbuf_buffer* bd = (rtems_bdbuf_buffer*) node; |
---|
| 1452 | bd->hold_timer = 0; |
---|
| 1453 | write_blocks++; |
---|
[0d15414e] | 1454 | node = rtems_chain_next (node); |
---|
[e51bd96] | 1455 | } |
---|
| 1456 | |
---|
[3899a537] | 1457 | rtems_bdbuf_wake_swapper (); |
---|
| 1458 | } |
---|
| 1459 | |
---|
| 1460 | /* |
---|
[0d15414e] | 1461 | * Wait for a buffer to be returned to the cache. The buffer will be |
---|
[3899a537] | 1462 | * placed on the LRU list. |
---|
| 1463 | */ |
---|
[0d15414e] | 1464 | rtems_bdbuf_wait (&bdbuf_cache.waiting, &bdbuf_cache.wait_waiters); |
---|
[e51bd96] | 1465 | } |
---|
[3899a537] | 1466 | } |
---|
| 1467 | else |
---|
| 1468 | { |
---|
[0d15414e] | 1469 | /* |
---|
| 1470 | * We have a new buffer for this block. |
---|
| 1471 | */ |
---|
[3899a537] | 1472 | if ((bd->state != RTEMS_BDBUF_STATE_EMPTY) && |
---|
| 1473 | (bd->state != RTEMS_BDBUF_STATE_READ_AHEAD)) |
---|
| 1474 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
| 1475 | |
---|
| 1476 | if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD) |
---|
[e51bd96] | 1477 | { |
---|
[0d15414e] | 1478 | if (rtems_bdbuf_avl_remove (&bdbuf_cache.tree, bd) != 0) |
---|
[3899a537] | 1479 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
[e51bd96] | 1480 | } |
---|
[3899a537] | 1481 | } |
---|
| 1482 | |
---|
| 1483 | if (bd) |
---|
| 1484 | { |
---|
| 1485 | bd->dev = device; |
---|
| 1486 | bd->block = block; |
---|
| 1487 | bd->avl.left = NULL; |
---|
| 1488 | bd->avl.right = NULL; |
---|
| 1489 | bd->state = RTEMS_BDBUF_STATE_EMPTY; |
---|
| 1490 | bd->error = 0; |
---|
| 1491 | bd->waiters = 0; |
---|
| 1492 | |
---|
[0d15414e] | 1493 | if (rtems_bdbuf_avl_insert (&bdbuf_cache.tree, bd) != 0) |
---|
[3899a537] | 1494 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
| 1495 | |
---|
| 1496 | return bd; |
---|
| 1497 | } |
---|
[e51bd96] | 1498 | } |
---|
[0d15414e] | 1499 | else |
---|
| 1500 | { |
---|
| 1501 | /* |
---|
| 1502 | * We have the buffer for the block from the cache. Check if the buffer |
---|
| 1503 | * in the cache is the same size and the requested size we are after. |
---|
| 1504 | */ |
---|
| 1505 | if (bd->group->bds_per_group != bds_per_group) |
---|
| 1506 | { |
---|
| 1507 | bd->state = RTEMS_BDBUF_STATE_EMPTY; |
---|
| 1508 | rtems_chain_extract (&bd->link); |
---|
| 1509 | rtems_chain_prepend (&bdbuf_cache.ready, &bd->link); |
---|
| 1510 | bd = NULL; |
---|
| 1511 | } |
---|
| 1512 | } |
---|
[3899a537] | 1513 | } |
---|
| 1514 | while (!bd); |
---|
| 1515 | |
---|
| 1516 | /* |
---|
| 1517 | * If the buffer is for read ahead and it exists in the AVL cache or is being |
---|
| 1518 | * accessed or being transfered then return NULL. |
---|
| 1519 | */ |
---|
| 1520 | if (read_ahead) |
---|
| 1521 | return NULL; |
---|
| 1522 | |
---|
| 1523 | /* |
---|
[c21c850e] | 1524 | * Loop waiting for the buffer to enter the cached state. If the buffer is in |
---|
| 1525 | * the access or transfer state then wait until it is not. |
---|
[3899a537] | 1526 | */ |
---|
[4f971343] | 1527 | available = false; |
---|
[3899a537] | 1528 | while (!available) |
---|
| 1529 | { |
---|
| 1530 | switch (bd->state) |
---|
[e51bd96] | 1531 | { |
---|
[3899a537] | 1532 | case RTEMS_BDBUF_STATE_CACHED: |
---|
| 1533 | case RTEMS_BDBUF_STATE_MODIFIED: |
---|
| 1534 | case RTEMS_BDBUF_STATE_READ_AHEAD: |
---|
[4f971343] | 1535 | available = true; |
---|
[3899a537] | 1536 | break; |
---|
| 1537 | |
---|
| 1538 | case RTEMS_BDBUF_STATE_ACCESS: |
---|
| 1539 | case RTEMS_BDBUF_STATE_ACCESS_MODIFIED: |
---|
| 1540 | bd->waiters++; |
---|
[0d15414e] | 1541 | rtems_bdbuf_wait (&bdbuf_cache.access, |
---|
| 1542 | &bdbuf_cache.access_waiters); |
---|
[3899a537] | 1543 | bd->waiters--; |
---|
| 1544 | break; |
---|
| 1545 | |
---|
| 1546 | case RTEMS_BDBUF_STATE_SYNC: |
---|
| 1547 | case RTEMS_BDBUF_STATE_TRANSFER: |
---|
| 1548 | bd->waiters++; |
---|
[0d15414e] | 1549 | rtems_bdbuf_wait (&bdbuf_cache.transfer, |
---|
| 1550 | &bdbuf_cache.transfer_waiters); |
---|
[3899a537] | 1551 | bd->waiters--; |
---|
| 1552 | break; |
---|
| 1553 | |
---|
| 1554 | default: |
---|
| 1555 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
[e51bd96] | 1556 | } |
---|
[3899a537] | 1557 | } |
---|
[048dcd2b] | 1558 | |
---|
[3899a537] | 1559 | /* |
---|
| 1560 | * Buffer is linked to the LRU, modifed, or sync lists. Remove it from there. |
---|
| 1561 | */ |
---|
| 1562 | rtems_chain_extract (&bd->link); |
---|
[e51bd96] | 1563 | |
---|
[3899a537] | 1564 | return bd; |
---|
[e51bd96] | 1565 | } |
---|
| 1566 | |
---|
[3899a537] | 1567 | rtems_status_code |
---|
| 1568 | rtems_bdbuf_get (dev_t device, |
---|
| 1569 | rtems_blkdev_bnum block, |
---|
| 1570 | rtems_bdbuf_buffer** bdp) |
---|
[e51bd96] | 1571 | { |
---|
[3899a537] | 1572 | rtems_disk_device* dd; |
---|
| 1573 | rtems_bdbuf_buffer* bd; |
---|
[0d15414e] | 1574 | size_t bds_per_group; |
---|
| 1575 | |
---|
| 1576 | if (!bdbuf_cache.initialised) |
---|
| 1577 | return RTEMS_NOT_CONFIGURED; |
---|
[3899a537] | 1578 | |
---|
| 1579 | /* |
---|
[0d15414e] | 1580 | * Do not hold the cache lock when obtaining the disk table. |
---|
[3899a537] | 1581 | */ |
---|
| 1582 | dd = rtems_disk_obtain (device); |
---|
[0d15414e] | 1583 | if (!dd) |
---|
[3899a537] | 1584 | return RTEMS_INVALID_ID; |
---|
| 1585 | |
---|
| 1586 | if (block >= dd->size) |
---|
| 1587 | { |
---|
| 1588 | rtems_disk_release (dd); |
---|
[0d15414e] | 1589 | return RTEMS_INVALID_ADDRESS; |
---|
[3899a537] | 1590 | } |
---|
| 1591 | |
---|
[0d15414e] | 1592 | bds_per_group = rtems_bdbuf_bds_per_group (dd->block_size); |
---|
| 1593 | if (!bds_per_group) |
---|
| 1594 | { |
---|
| 1595 | rtems_disk_release (dd); |
---|
| 1596 | return RTEMS_INVALID_NUMBER; |
---|
| 1597 | } |
---|
[3899a537] | 1598 | |
---|
[0d15414e] | 1599 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 1600 | |
---|
| 1601 | #if RTEMS_BDBUF_TRACE |
---|
[57aa979] | 1602 | /* Print the block index relative to the physical disk */ |
---|
| 1603 | rtems_bdbuf_printf ("get: %d (dev = %08x)\n", block + dd->start, device); |
---|
[3899a537] | 1604 | #endif |
---|
| 1605 | |
---|
[0d15414e] | 1606 | bd = rtems_bdbuf_get_buffer (dd, bds_per_group, block + dd->start, false); |
---|
[3899a537] | 1607 | |
---|
| 1608 | if (bd->state == RTEMS_BDBUF_STATE_MODIFIED) |
---|
| 1609 | bd->state = RTEMS_BDBUF_STATE_ACCESS_MODIFIED; |
---|
| 1610 | else |
---|
| 1611 | bd->state = RTEMS_BDBUF_STATE_ACCESS; |
---|
[0d15414e] | 1612 | |
---|
| 1613 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 1614 | |
---|
[6f162ed] | 1615 | rtems_disk_release(dd); |
---|
| 1616 | |
---|
[3899a537] | 1617 | *bdp = bd; |
---|
[0d15414e] | 1618 | |
---|
[3899a537] | 1619 | return RTEMS_SUCCESSFUL; |
---|
[e51bd96] | 1620 | } |
---|
| 1621 | |
---|
[c21c850e] | 1622 | /** |
---|
| 1623 | * Call back handler called by the low level driver when the transfer has |
---|
| 1624 | * completed. This function may be invoked from interrupt handler. |
---|
[e51bd96] | 1625 | * |
---|
[c21c850e] | 1626 | * @param arg Arbitrary argument specified in block device request |
---|
| 1627 | * structure (in this case - pointer to the appropriate |
---|
| 1628 | * block device request structure). |
---|
| 1629 | * @param status I/O completion status |
---|
| 1630 | * @param error errno error code if status != RTEMS_SUCCESSFUL |
---|
[e51bd96] | 1631 | */ |
---|
| 1632 | static void |
---|
[3899a537] | 1633 | rtems_bdbuf_read_done (void* arg, rtems_status_code status, int error) |
---|
[e51bd96] | 1634 | { |
---|
[3899a537] | 1635 | rtems_blkdev_request* req = (rtems_blkdev_request*) arg; |
---|
| 1636 | |
---|
| 1637 | req->error = error; |
---|
| 1638 | req->status = status; |
---|
| 1639 | |
---|
| 1640 | rtems_event_send (req->io_task, RTEMS_BDBUF_TRANSFER_SYNC); |
---|
[e51bd96] | 1641 | } |
---|
| 1642 | |
---|
| 1643 | rtems_status_code |
---|
[3899a537] | 1644 | rtems_bdbuf_read (dev_t device, |
---|
| 1645 | rtems_blkdev_bnum block, |
---|
| 1646 | rtems_bdbuf_buffer** bdp) |
---|
[e51bd96] | 1647 | { |
---|
[3899a537] | 1648 | rtems_disk_device* dd; |
---|
| 1649 | rtems_bdbuf_buffer* bd = NULL; |
---|
[cec5c069] | 1650 | uint32_t read_ahead_count; |
---|
[3899a537] | 1651 | rtems_blkdev_request* req; |
---|
[0d15414e] | 1652 | size_t bds_per_group; |
---|
[3899a537] | 1653 | |
---|
[0d15414e] | 1654 | if (!bdbuf_cache.initialised) |
---|
| 1655 | return RTEMS_NOT_CONFIGURED; |
---|
| 1656 | |
---|
[3899a537] | 1657 | /* |
---|
| 1658 | * @todo This type of request structure is wrong and should be removed. |
---|
| 1659 | */ |
---|
| 1660 | #define bdbuf_alloc(size) __builtin_alloca (size) |
---|
| 1661 | |
---|
| 1662 | req = bdbuf_alloc (sizeof (rtems_blkdev_request) + |
---|
| 1663 | (sizeof ( rtems_blkdev_sg_buffer) * |
---|
| 1664 | rtems_bdbuf_configuration.max_read_ahead_blocks)); |
---|
| 1665 | |
---|
| 1666 | /* |
---|
[0d15414e] | 1667 | * Do not hold the cache lock when obtaining the disk table. |
---|
[3899a537] | 1668 | */ |
---|
| 1669 | dd = rtems_disk_obtain (device); |
---|
[0d15414e] | 1670 | if (!dd) |
---|
[3899a537] | 1671 | return RTEMS_INVALID_ID; |
---|
| 1672 | |
---|
[57aa979] | 1673 | if (block >= dd->size) { |
---|
[3899a537] | 1674 | rtems_disk_release(dd); |
---|
| 1675 | return RTEMS_INVALID_NUMBER; |
---|
| 1676 | } |
---|
[57aa979] | 1677 | |
---|
[0d15414e] | 1678 | bds_per_group = rtems_bdbuf_bds_per_group (dd->block_size); |
---|
| 1679 | if (!bds_per_group) |
---|
| 1680 | { |
---|
| 1681 | rtems_disk_release (dd); |
---|
| 1682 | return RTEMS_INVALID_NUMBER; |
---|
| 1683 | } |
---|
| 1684 | |
---|
[57aa979] | 1685 | #if RTEMS_BDBUF_TRACE |
---|
| 1686 | /* Print the block index relative to the physical disk */ |
---|
| 1687 | rtems_bdbuf_printf ("read: %d (dev = %08x)\n", block + dd->start, device); |
---|
| 1688 | #endif |
---|
[048dcd2b] | 1689 | |
---|
[3899a537] | 1690 | req->bufnum = 0; |
---|
[e51bd96] | 1691 | |
---|
[3899a537] | 1692 | /* |
---|
| 1693 | * Read the block plus the required number of blocks ahead. The number of |
---|
| 1694 | * blocks to read ahead is configured by the user and limited by the size of |
---|
| 1695 | * the disk or reaching a read ahead block that is also cached. |
---|
| 1696 | * |
---|
| 1697 | * Limit the blocks read by the size of the disk. |
---|
| 1698 | */ |
---|
| 1699 | if ((rtems_bdbuf_configuration.max_read_ahead_blocks + block) < dd->size) |
---|
| 1700 | read_ahead_count = rtems_bdbuf_configuration.max_read_ahead_blocks; |
---|
| 1701 | else |
---|
| 1702 | read_ahead_count = dd->size - block; |
---|
[048dcd2b] | 1703 | |
---|
[0d15414e] | 1704 | rtems_bdbuf_lock_cache (); |
---|
[e51bd96] | 1705 | |
---|
[2eb89ad] | 1706 | while (req->bufnum < read_ahead_count) |
---|
[3899a537] | 1707 | { |
---|
| 1708 | /* |
---|
| 1709 | * Get the buffer for the requested block. If the block is cached then |
---|
| 1710 | * return it. If it is not cached transfer the block from the disk media |
---|
| 1711 | * into memory. |
---|
| 1712 | * |
---|
| 1713 | * We need to clean up any buffers allocated and not passed back to the |
---|
| 1714 | * caller. |
---|
| 1715 | */ |
---|
[0d15414e] | 1716 | bd = rtems_bdbuf_get_buffer (dd, bds_per_group, |
---|
[57aa979] | 1717 | block + dd->start + req->bufnum, |
---|
[4f971343] | 1718 | req->bufnum == 0 ? false : true); |
---|
[e51bd96] | 1719 | |
---|
[3899a537] | 1720 | /* |
---|
| 1721 | * Read ahead buffer is in the cache or none available. Read what we |
---|
| 1722 | * can. |
---|
| 1723 | */ |
---|
| 1724 | if (!bd) |
---|
| 1725 | break; |
---|
[e51bd96] | 1726 | |
---|
[3899a537] | 1727 | /* |
---|
| 1728 | * Is the block we are interested in the cache ? |
---|
| 1729 | */ |
---|
| 1730 | if ((bd->state == RTEMS_BDBUF_STATE_CACHED) || |
---|
| 1731 | (bd->state == RTEMS_BDBUF_STATE_MODIFIED)) |
---|
| 1732 | break; |
---|
[048dcd2b] | 1733 | |
---|
[3899a537] | 1734 | bd->state = RTEMS_BDBUF_STATE_TRANSFER; |
---|
| 1735 | bd->error = 0; |
---|
[e51bd96] | 1736 | |
---|
[3899a537] | 1737 | /* |
---|
[c21c850e] | 1738 | * @todo The use of these req blocks is not a great design. The req is a |
---|
| 1739 | * struct with a single 'bufs' declared in the req struct and the |
---|
| 1740 | * others are added in the outer level struct. This relies on the |
---|
| 1741 | * structs joining as a single array and that assumes the compiler |
---|
| 1742 | * packs the structs. Why not just place on a list ? The BD has a |
---|
| 1743 | * node that can be used. |
---|
[3899a537] | 1744 | */ |
---|
[2eb89ad] | 1745 | req->bufs[req->bufnum].user = bd; |
---|
| 1746 | req->bufs[req->bufnum].block = bd->block; |
---|
| 1747 | req->bufs[req->bufnum].length = dd->block_size; |
---|
| 1748 | req->bufs[req->bufnum].buffer = bd->buffer; |
---|
[3899a537] | 1749 | req->bufnum++; |
---|
| 1750 | } |
---|
[0ebfac19] | 1751 | |
---|
[3899a537] | 1752 | /* |
---|
| 1753 | * Transfer any requested buffers. If the request count is 0 we have found |
---|
| 1754 | * the block in the cache so return it. |
---|
| 1755 | */ |
---|
[2eb89ad] | 1756 | if (req->bufnum) |
---|
[3899a537] | 1757 | { |
---|
| 1758 | /* |
---|
[0d15414e] | 1759 | * Unlock the cache. We have the buffer for the block and it will be in the |
---|
[3899a537] | 1760 | * access or transfer state. We may also have a number of read ahead blocks |
---|
| 1761 | * if we need to transfer data. At this point any other threads can gain |
---|
[0d15414e] | 1762 | * access to the cache and if they are after any of the buffers we have |
---|
| 1763 | * they will block and be woken when the buffer is returned to the cache. |
---|
[3899a537] | 1764 | * |
---|
| 1765 | * If a transfer is needed the I/O operation will occur with pre-emption |
---|
[0d15414e] | 1766 | * enabled and the cache unlocked. This is a change to the previous version |
---|
[3899a537] | 1767 | * of the bdbuf code. |
---|
| 1768 | */ |
---|
[8c44190a] | 1769 | rtems_event_set out; |
---|
| 1770 | int result; |
---|
| 1771 | uint32_t b; |
---|
| 1772 | |
---|
| 1773 | /* |
---|
| 1774 | * Flush any events. |
---|
| 1775 | */ |
---|
| 1776 | rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC, |
---|
| 1777 | RTEMS_EVENT_ALL | RTEMS_NO_WAIT, |
---|
| 1778 | 0, &out); |
---|
| 1779 | |
---|
[0d15414e] | 1780 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 1781 | |
---|
| 1782 | req->req = RTEMS_BLKDEV_REQ_READ; |
---|
| 1783 | req->req_done = rtems_bdbuf_read_done; |
---|
| 1784 | req->done_arg = req; |
---|
| 1785 | req->io_task = rtems_task_self (); |
---|
| 1786 | req->status = RTEMS_RESOURCE_IN_USE; |
---|
| 1787 | req->error = 0; |
---|
| 1788 | |
---|
| 1789 | result = dd->ioctl (dd->phys_dev->dev, RTEMS_BLKIO_REQUEST, req); |
---|
| 1790 | |
---|
| 1791 | /* |
---|
| 1792 | * Inspection of the DOS FS code shows the result from this function is |
---|
| 1793 | * handled and a buffer must be returned. |
---|
| 1794 | */ |
---|
| 1795 | if (result < 0) |
---|
[0ebfac19] | 1796 | { |
---|
[3899a537] | 1797 | req->error = errno; |
---|
| 1798 | req->status = RTEMS_IO_ERROR; |
---|
| 1799 | } |
---|
| 1800 | else |
---|
| 1801 | { |
---|
| 1802 | rtems_status_code sc; |
---|
[8c44190a] | 1803 | |
---|
[3899a537] | 1804 | sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC, |
---|
| 1805 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
| 1806 | 0, &out); |
---|
| 1807 | |
---|
| 1808 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 1809 | rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE); |
---|
[0ebfac19] | 1810 | } |
---|
| 1811 | |
---|
[0d15414e] | 1812 | rtems_bdbuf_lock_cache (); |
---|
[0ebfac19] | 1813 | |
---|
[2eb89ad] | 1814 | for (b = 1; b < req->bufnum; b++) |
---|
[0ebfac19] | 1815 | { |
---|
[3899a537] | 1816 | bd = req->bufs[b].user; |
---|
| 1817 | bd->error = req->error; |
---|
| 1818 | bd->state = RTEMS_BDBUF_STATE_READ_AHEAD; |
---|
| 1819 | rtems_bdbuf_release (bd); |
---|
[0ebfac19] | 1820 | } |
---|
| 1821 | |
---|
[3899a537] | 1822 | bd = req->bufs[0].user; |
---|
| 1823 | } |
---|
[0ebfac19] | 1824 | |
---|
[3899a537] | 1825 | /* |
---|
| 1826 | * The data for this block is cached in the buffer. |
---|
| 1827 | */ |
---|
| 1828 | if (bd->state == RTEMS_BDBUF_STATE_MODIFIED) |
---|
| 1829 | bd->state = RTEMS_BDBUF_STATE_ACCESS_MODIFIED; |
---|
| 1830 | else |
---|
| 1831 | bd->state = RTEMS_BDBUF_STATE_ACCESS; |
---|
[048dcd2b] | 1832 | |
---|
[0d15414e] | 1833 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 1834 | rtems_disk_release (dd); |
---|
[0ebfac19] | 1835 | |
---|
[3899a537] | 1836 | *bdp = bd; |
---|
[e51bd96] | 1837 | |
---|
[3899a537] | 1838 | return RTEMS_SUCCESSFUL; |
---|
| 1839 | } |
---|
[e51bd96] | 1840 | |
---|
[3899a537] | 1841 | rtems_status_code |
---|
| 1842 | rtems_bdbuf_release (rtems_bdbuf_buffer* bd) |
---|
[e51bd96] | 1843 | { |
---|
[0d15414e] | 1844 | if (!bdbuf_cache.initialised) |
---|
| 1845 | return RTEMS_NOT_CONFIGURED; |
---|
[e51bd96] | 1846 | |
---|
[3899a537] | 1847 | if (bd == NULL) |
---|
| 1848 | return RTEMS_INVALID_ADDRESS; |
---|
[048dcd2b] | 1849 | |
---|
[0d15414e] | 1850 | rtems_bdbuf_lock_cache (); |
---|
[e51bd96] | 1851 | |
---|
[3899a537] | 1852 | #if RTEMS_BDBUF_TRACE |
---|
| 1853 | rtems_bdbuf_printf ("release: %d\n", bd->block); |
---|
| 1854 | #endif |
---|
| 1855 | |
---|
| 1856 | if (bd->state == RTEMS_BDBUF_STATE_ACCESS_MODIFIED) |
---|
| 1857 | { |
---|
[0d15414e] | 1858 | rtems_bdbuf_append_modified (bd); |
---|
[3899a537] | 1859 | } |
---|
| 1860 | else |
---|
| 1861 | { |
---|
| 1862 | /* |
---|
[c21c850e] | 1863 | * If this is a read ahead buffer place the ready queue. Buffers are taken |
---|
| 1864 | * from here first. If we prepend then get from the queue the buffers |
---|
| 1865 | * furthermost from the read buffer will be used. |
---|
[3899a537] | 1866 | */ |
---|
| 1867 | if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD) |
---|
[0d15414e] | 1868 | rtems_chain_prepend (&bdbuf_cache.ready, &bd->link); |
---|
[3899a537] | 1869 | else |
---|
[e51bd96] | 1870 | { |
---|
[3899a537] | 1871 | bd->state = RTEMS_BDBUF_STATE_CACHED; |
---|
[0d15414e] | 1872 | rtems_chain_append (&bdbuf_cache.lru, &bd->link); |
---|
[e51bd96] | 1873 | } |
---|
[0d15414e] | 1874 | |
---|
| 1875 | /* |
---|
| 1876 | * One less user for the group of bds. |
---|
| 1877 | */ |
---|
| 1878 | bd->group->users--; |
---|
[3899a537] | 1879 | } |
---|
| 1880 | |
---|
| 1881 | /* |
---|
| 1882 | * If there are threads waiting to access the buffer wake them. Wake any |
---|
| 1883 | * waiters if this is the first buffer to placed back onto the queue. |
---|
| 1884 | */ |
---|
| 1885 | if (bd->waiters) |
---|
[0d15414e] | 1886 | rtems_bdbuf_wake (bdbuf_cache.access, &bdbuf_cache.access_waiters); |
---|
[3899a537] | 1887 | else |
---|
| 1888 | { |
---|
| 1889 | if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD) |
---|
| 1890 | { |
---|
[0d15414e] | 1891 | if (rtems_chain_has_only_one_node (&bdbuf_cache.ready)) |
---|
| 1892 | rtems_bdbuf_wake (bdbuf_cache.waiting, &bdbuf_cache.wait_waiters); |
---|
[3899a537] | 1893 | } |
---|
| 1894 | else |
---|
| 1895 | { |
---|
[0d15414e] | 1896 | if (rtems_chain_has_only_one_node (&bdbuf_cache.lru)) |
---|
| 1897 | rtems_bdbuf_wake (bdbuf_cache.waiting, &bdbuf_cache.wait_waiters); |
---|
[3899a537] | 1898 | } |
---|
| 1899 | } |
---|
| 1900 | |
---|
[0d15414e] | 1901 | rtems_bdbuf_unlock_cache (); |
---|
[e51bd96] | 1902 | |
---|
[3899a537] | 1903 | return RTEMS_SUCCESSFUL; |
---|
| 1904 | } |
---|
[e51bd96] | 1905 | |
---|
| 1906 | rtems_status_code |
---|
[3899a537] | 1907 | rtems_bdbuf_release_modified (rtems_bdbuf_buffer* bd) |
---|
[e51bd96] | 1908 | { |
---|
[0d15414e] | 1909 | if (!bdbuf_cache.initialised) |
---|
| 1910 | return RTEMS_NOT_CONFIGURED; |
---|
[3899a537] | 1911 | |
---|
[0d15414e] | 1912 | if (!bd) |
---|
[3899a537] | 1913 | return RTEMS_INVALID_ADDRESS; |
---|
[e51bd96] | 1914 | |
---|
[0d15414e] | 1915 | rtems_bdbuf_lock_cache (); |
---|
[048dcd2b] | 1916 | |
---|
[3899a537] | 1917 | #if RTEMS_BDBUF_TRACE |
---|
| 1918 | rtems_bdbuf_printf ("release modified: %d\n", bd->block); |
---|
| 1919 | #endif |
---|
| 1920 | |
---|
| 1921 | bd->hold_timer = rtems_bdbuf_configuration.swap_block_hold; |
---|
| 1922 | |
---|
[0d15414e] | 1923 | rtems_bdbuf_append_modified (bd); |
---|
[048dcd2b] | 1924 | |
---|
[3899a537] | 1925 | if (bd->waiters) |
---|
[0d15414e] | 1926 | rtems_bdbuf_wake (bdbuf_cache.access, &bdbuf_cache.access_waiters); |
---|
[3899a537] | 1927 | |
---|
[0d15414e] | 1928 | rtems_bdbuf_unlock_cache (); |
---|
[048dcd2b] | 1929 | |
---|
[3899a537] | 1930 | return RTEMS_SUCCESSFUL; |
---|
[e51bd96] | 1931 | } |
---|
| 1932 | |
---|
| 1933 | rtems_status_code |
---|
[3899a537] | 1934 | rtems_bdbuf_sync (rtems_bdbuf_buffer* bd) |
---|
[e51bd96] | 1935 | { |
---|
[0d15414e] | 1936 | bool available; |
---|
[3899a537] | 1937 | |
---|
| 1938 | #if RTEMS_BDBUF_TRACE |
---|
| 1939 | rtems_bdbuf_printf ("sync: %d\n", bd->block); |
---|
| 1940 | #endif |
---|
| 1941 | |
---|
[0d15414e] | 1942 | if (!bdbuf_cache.initialised) |
---|
| 1943 | return RTEMS_NOT_CONFIGURED; |
---|
[3899a537] | 1944 | |
---|
[0d15414e] | 1945 | if (!bd) |
---|
| 1946 | return RTEMS_INVALID_ADDRESS; |
---|
[3899a537] | 1947 | |
---|
[0d15414e] | 1948 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 1949 | |
---|
| 1950 | bd->state = RTEMS_BDBUF_STATE_SYNC; |
---|
[e51bd96] | 1951 | |
---|
[0d15414e] | 1952 | rtems_chain_append (&bdbuf_cache.sync, &bd->link); |
---|
[048dcd2b] | 1953 | |
---|
[3899a537] | 1954 | rtems_bdbuf_wake_swapper (); |
---|
[e51bd96] | 1955 | |
---|
[4f971343] | 1956 | available = false; |
---|
[3899a537] | 1957 | while (!available) |
---|
| 1958 | { |
---|
| 1959 | switch (bd->state) |
---|
[e51bd96] | 1960 | { |
---|
[3899a537] | 1961 | case RTEMS_BDBUF_STATE_CACHED: |
---|
| 1962 | case RTEMS_BDBUF_STATE_READ_AHEAD: |
---|
| 1963 | case RTEMS_BDBUF_STATE_MODIFIED: |
---|
| 1964 | case RTEMS_BDBUF_STATE_ACCESS: |
---|
| 1965 | case RTEMS_BDBUF_STATE_ACCESS_MODIFIED: |
---|
[4f971343] | 1966 | available = true; |
---|
[3899a537] | 1967 | break; |
---|
| 1968 | |
---|
| 1969 | case RTEMS_BDBUF_STATE_SYNC: |
---|
| 1970 | case RTEMS_BDBUF_STATE_TRANSFER: |
---|
| 1971 | bd->waiters++; |
---|
[0d15414e] | 1972 | rtems_bdbuf_wait (&bdbuf_cache.transfer, &bdbuf_cache.transfer_waiters); |
---|
[3899a537] | 1973 | bd->waiters--; |
---|
| 1974 | break; |
---|
| 1975 | |
---|
| 1976 | default: |
---|
| 1977 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY); |
---|
[e51bd96] | 1978 | } |
---|
[3899a537] | 1979 | } |
---|
[048dcd2b] | 1980 | |
---|
[0d15414e] | 1981 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 1982 | |
---|
| 1983 | return RTEMS_SUCCESSFUL; |
---|
[e51bd96] | 1984 | } |
---|
| 1985 | |
---|
| 1986 | rtems_status_code |
---|
[3899a537] | 1987 | rtems_bdbuf_syncdev (dev_t dev) |
---|
[e51bd96] | 1988 | { |
---|
[3899a537] | 1989 | rtems_disk_device* dd; |
---|
| 1990 | rtems_status_code sc; |
---|
| 1991 | rtems_event_set out; |
---|
[e51bd96] | 1992 | |
---|
[3899a537] | 1993 | #if RTEMS_BDBUF_TRACE |
---|
| 1994 | rtems_bdbuf_printf ("syncdev: %08x\n", dev); |
---|
| 1995 | #endif |
---|
[e51bd96] | 1996 | |
---|
[0d15414e] | 1997 | if (!bdbuf_cache.initialised) |
---|
| 1998 | return RTEMS_NOT_CONFIGURED; |
---|
| 1999 | |
---|
[3899a537] | 2000 | /* |
---|
[0d15414e] | 2001 | * Do not hold the cache lock when obtaining the disk table. |
---|
[3899a537] | 2002 | */ |
---|
| 2003 | dd = rtems_disk_obtain (dev); |
---|
[0d15414e] | 2004 | if (!dd) |
---|
[3899a537] | 2005 | return RTEMS_INVALID_ID; |
---|
| 2006 | |
---|
| 2007 | /* |
---|
[0d15414e] | 2008 | * Take the sync lock before locking the cache. Once we have the sync lock we |
---|
| 2009 | * can lock the cache. If another thread has the sync lock it will cause this |
---|
| 2010 | * thread to block until it owns the sync lock then it can own the cache. The |
---|
| 2011 | * sync lock can only be obtained with the cache unlocked. |
---|
[3899a537] | 2012 | */ |
---|
| 2013 | |
---|
[0d15414e] | 2014 | rtems_bdbuf_lock_sync (); |
---|
| 2015 | rtems_bdbuf_lock_cache (); |
---|
[e51bd96] | 2016 | |
---|
[c21c850e] | 2017 | /* |
---|
[0d15414e] | 2018 | * Set the cache to have a sync active for a specific device and let the swap |
---|
[c21c850e] | 2019 | * out task know the id of the requester to wake when done. |
---|
| 2020 | * |
---|
| 2021 | * The swap out task will negate the sync active flag when no more buffers |
---|
[0d15414e] | 2022 | * for the device are held on the "modified for sync" queues. |
---|
[c21c850e] | 2023 | */ |
---|
[0d15414e] | 2024 | bdbuf_cache.sync_active = true; |
---|
| 2025 | bdbuf_cache.sync_requester = rtems_task_self (); |
---|
| 2026 | bdbuf_cache.sync_device = dev; |
---|
[3899a537] | 2027 | |
---|
| 2028 | rtems_bdbuf_wake_swapper (); |
---|
[0d15414e] | 2029 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 2030 | |
---|
| 2031 | sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC, |
---|
| 2032 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
| 2033 | 0, &out); |
---|
[e51bd96] | 2034 | |
---|
[3899a537] | 2035 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2036 | rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE); |
---|
| 2037 | |
---|
[0d15414e] | 2038 | rtems_bdbuf_unlock_sync (); |
---|
[3899a537] | 2039 | |
---|
[0d15414e] | 2040 | return rtems_disk_release (dd); |
---|
[e51bd96] | 2041 | } |
---|
| 2042 | |
---|
[c21c850e] | 2043 | /** |
---|
| 2044 | * Call back handler called by the low level driver when the transfer has |
---|
| 2045 | * completed. This function may be invoked from interrupt handler. |
---|
[e51bd96] | 2046 | * |
---|
[c21c850e] | 2047 | * @param arg Arbitrary argument specified in block device request |
---|
| 2048 | * structure (in this case - pointer to the appropriate |
---|
| 2049 | * block device request structure). |
---|
| 2050 | * @param status I/O completion status |
---|
| 2051 | * @param error errno error code if status != RTEMS_SUCCESSFUL |
---|
[e51bd96] | 2052 | */ |
---|
[3899a537] | 2053 | static void |
---|
| 2054 | rtems_bdbuf_write_done(void *arg, rtems_status_code status, int error) |
---|
| 2055 | { |
---|
| 2056 | rtems_blkdev_request* req = (rtems_blkdev_request*) arg; |
---|
| 2057 | |
---|
| 2058 | req->error = error; |
---|
| 2059 | req->status = status; |
---|
| 2060 | |
---|
| 2061 | rtems_event_send (req->io_task, RTEMS_BDBUF_TRANSFER_SYNC); |
---|
| 2062 | } |
---|
| 2063 | |
---|
| 2064 | /** |
---|
[0d15414e] | 2065 | * Swapout transfer to the driver. The driver will break this I/O into groups |
---|
| 2066 | * of consecutive write requests is multiple consecutive buffers are required |
---|
| 2067 | * by the driver. |
---|
[c21c850e] | 2068 | * |
---|
[0d15414e] | 2069 | * @param transfer The transfer transaction. |
---|
[3899a537] | 2070 | */ |
---|
| 2071 | static void |
---|
[0d15414e] | 2072 | rtems_bdbuf_swapout_write (rtems_bdbuf_swapout_transfer* transfer) |
---|
[e51bd96] | 2073 | { |
---|
[0d15414e] | 2074 | rtems_disk_device* dd; |
---|
[3899a537] | 2075 | |
---|
[0d15414e] | 2076 | #if RTEMS_BDBUF_TRACE |
---|
| 2077 | rtems_bdbuf_printf ("swapout transfer: %08x\n", transfer->dev); |
---|
| 2078 | #endif |
---|
[3899a537] | 2079 | |
---|
| 2080 | /* |
---|
[7baa484] | 2081 | * If there are buffers to transfer to the media transfer them. |
---|
[3899a537] | 2082 | */ |
---|
[0d15414e] | 2083 | if (!rtems_chain_is_empty (&transfer->bds)) |
---|
[3899a537] | 2084 | { |
---|
| 2085 | /* |
---|
[0d15414e] | 2086 | * Obtain the disk device. The cache's mutex has been released to avoid a |
---|
[7baa484] | 2087 | * dead lock. |
---|
[3899a537] | 2088 | */ |
---|
[0d15414e] | 2089 | dd = rtems_disk_obtain (transfer->dev); |
---|
| 2090 | if (dd) |
---|
[3899a537] | 2091 | { |
---|
| 2092 | /* |
---|
| 2093 | * The last block number used when the driver only supports |
---|
| 2094 | * continuous blocks in a single request. |
---|
| 2095 | */ |
---|
| 2096 | uint32_t last_block = 0; |
---|
| 2097 | |
---|
| 2098 | /* |
---|
[c21c850e] | 2099 | * Take as many buffers as configured and pass to the driver. Note, the |
---|
[7baa484] | 2100 | * API to the drivers has an array of buffers and if a chain was passed |
---|
[c21c850e] | 2101 | * we could have just passed the list. If the driver API is updated it |
---|
| 2102 | * should be possible to make this change with little effect in this |
---|
| 2103 | * code. The array that is passed is broken in design and should be |
---|
[7baa484] | 2104 | * removed. Merging members of a struct into the first member is |
---|
[c21c850e] | 2105 | * trouble waiting to happen. |
---|
[3899a537] | 2106 | */ |
---|
[0d15414e] | 2107 | transfer->write_req->status = RTEMS_RESOURCE_IN_USE; |
---|
| 2108 | transfer->write_req->error = 0; |
---|
| 2109 | transfer->write_req->bufnum = 0; |
---|
[3899a537] | 2110 | |
---|
[0d15414e] | 2111 | while (!rtems_chain_is_empty (&transfer->bds)) |
---|
[3899a537] | 2112 | { |
---|
| 2113 | rtems_bdbuf_buffer* bd = |
---|
[0d15414e] | 2114 | (rtems_bdbuf_buffer*) rtems_chain_get (&transfer->bds); |
---|
[3899a537] | 2115 | |
---|
[8c5d3743] | 2116 | bool write = false; |
---|
[3899a537] | 2117 | |
---|
| 2118 | /* |
---|
[c21c850e] | 2119 | * If the device only accepts sequential buffers and this is not the |
---|
| 2120 | * first buffer (the first is always sequential, and the buffer is not |
---|
| 2121 | * sequential then put the buffer back on the transfer chain and write |
---|
| 2122 | * the committed buffers. |
---|
[3899a537] | 2123 | */ |
---|
| 2124 | |
---|
[0d15414e] | 2125 | #if RTEMS_BDBUF_TRACE |
---|
| 2126 | rtems_bdbuf_printf ("swapout write: bd:%d, bufnum:%d mode:%s\n", |
---|
| 2127 | bd->block, transfer->write_req->bufnum, |
---|
| 2128 | dd->phys_dev->capabilities & |
---|
| 2129 | RTEMS_BLKDEV_CAP_MULTISECTOR_CONT ? "MULIT" : "SCAT"); |
---|
| 2130 | #endif |
---|
| 2131 | |
---|
| 2132 | if ((dd->phys_dev->capabilities & RTEMS_BLKDEV_CAP_MULTISECTOR_CONT) && |
---|
| 2133 | transfer->write_req->bufnum && |
---|
[3899a537] | 2134 | (bd->block != (last_block + 1))) |
---|
| 2135 | { |
---|
[0d15414e] | 2136 | rtems_chain_prepend (&transfer->bds, &bd->link); |
---|
[4f971343] | 2137 | write = true; |
---|
[3899a537] | 2138 | } |
---|
| 2139 | else |
---|
| 2140 | { |
---|
[0d15414e] | 2141 | rtems_blkdev_sg_buffer* buf; |
---|
| 2142 | buf = &transfer->write_req->bufs[transfer->write_req->bufnum]; |
---|
| 2143 | transfer->write_req->bufnum++; |
---|
| 2144 | buf->user = bd; |
---|
| 2145 | buf->block = bd->block; |
---|
| 2146 | buf->length = dd->block_size; |
---|
| 2147 | buf->buffer = bd->buffer; |
---|
| 2148 | last_block = bd->block; |
---|
[3899a537] | 2149 | } |
---|
| 2150 | |
---|
| 2151 | /* |
---|
[c21c850e] | 2152 | * Perform the transfer if there are no more buffers, or the transfer |
---|
| 2153 | * size has reached the configured max. value. |
---|
[3899a537] | 2154 | */ |
---|
[048dcd2b] | 2155 | |
---|
[0d15414e] | 2156 | if (rtems_chain_is_empty (&transfer->bds) || |
---|
| 2157 | (transfer->write_req->bufnum >= rtems_bdbuf_configuration.max_write_blocks)) |
---|
[4f971343] | 2158 | write = true; |
---|
[048dcd2b] | 2159 | |
---|
[3899a537] | 2160 | if (write) |
---|
[e51bd96] | 2161 | { |
---|
[3899a537] | 2162 | int result; |
---|
[cec5c069] | 2163 | uint32_t b; |
---|
[3899a537] | 2164 | |
---|
[0d15414e] | 2165 | #if RTEMS_BDBUF_TRACE |
---|
| 2166 | rtems_bdbuf_printf ("swapout write: writing bufnum:%d\n", |
---|
| 2167 | transfer->write_req->bufnum); |
---|
| 2168 | #endif |
---|
[3899a537] | 2169 | /* |
---|
[0d15414e] | 2170 | * Perform the transfer. No cache locks, no preemption, only the disk |
---|
[c21c850e] | 2171 | * device is being held. |
---|
[3899a537] | 2172 | */ |
---|
[0d15414e] | 2173 | result = dd->phys_dev->ioctl (dd->phys_dev->dev, |
---|
| 2174 | RTEMS_BLKIO_REQUEST, transfer->write_req); |
---|
[3899a537] | 2175 | |
---|
| 2176 | if (result < 0) |
---|
| 2177 | { |
---|
[0d15414e] | 2178 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 2179 | |
---|
[0d15414e] | 2180 | for (b = 0; b < transfer->write_req->bufnum; b++) |
---|
[3899a537] | 2181 | { |
---|
[0d15414e] | 2182 | bd = transfer->write_req->bufs[b].user; |
---|
[3899a537] | 2183 | bd->state = RTEMS_BDBUF_STATE_MODIFIED; |
---|
| 2184 | bd->error = errno; |
---|
| 2185 | |
---|
| 2186 | /* |
---|
[0d15414e] | 2187 | * Place back on the cache's modified queue and try again. |
---|
[3899a537] | 2188 | * |
---|
[c21c850e] | 2189 | * @warning Not sure this is the best option but I do not know |
---|
| 2190 | * what else can be done. |
---|
[3899a537] | 2191 | */ |
---|
[0d15414e] | 2192 | rtems_chain_append (&bdbuf_cache.modified, &bd->link); |
---|
[3899a537] | 2193 | } |
---|
| 2194 | } |
---|
| 2195 | else |
---|
| 2196 | { |
---|
| 2197 | rtems_status_code sc = 0; |
---|
| 2198 | rtems_event_set out; |
---|
| 2199 | |
---|
| 2200 | sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC, |
---|
| 2201 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
| 2202 | 0, &out); |
---|
| 2203 | |
---|
| 2204 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2205 | rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE); |
---|
| 2206 | |
---|
[0d15414e] | 2207 | rtems_bdbuf_lock_cache (); |
---|
[3899a537] | 2208 | |
---|
[0d15414e] | 2209 | for (b = 0; b < transfer->write_req->bufnum; b++) |
---|
[3899a537] | 2210 | { |
---|
[0d15414e] | 2211 | bd = transfer->write_req->bufs[b].user; |
---|
[3899a537] | 2212 | bd->state = RTEMS_BDBUF_STATE_CACHED; |
---|
| 2213 | bd->error = 0; |
---|
[0d15414e] | 2214 | bd->group->users--; |
---|
| 2215 | |
---|
| 2216 | rtems_chain_append (&bdbuf_cache.lru, &bd->link); |
---|
[3899a537] | 2217 | |
---|
| 2218 | if (bd->waiters) |
---|
[0d15414e] | 2219 | rtems_bdbuf_wake (bdbuf_cache.transfer, &bdbuf_cache.transfer_waiters); |
---|
[3899a537] | 2220 | else |
---|
| 2221 | { |
---|
[0d15414e] | 2222 | if (rtems_chain_has_only_one_node (&bdbuf_cache.lru)) |
---|
| 2223 | rtems_bdbuf_wake (bdbuf_cache.waiting, &bdbuf_cache.wait_waiters); |
---|
[3899a537] | 2224 | } |
---|
| 2225 | } |
---|
| 2226 | } |
---|
[0d15414e] | 2227 | |
---|
| 2228 | rtems_bdbuf_unlock_cache (); |
---|
[3899a537] | 2229 | |
---|
[0d15414e] | 2230 | transfer->write_req->status = RTEMS_RESOURCE_IN_USE; |
---|
| 2231 | transfer->write_req->error = 0; |
---|
| 2232 | transfer->write_req->bufnum = 0; |
---|
[e51bd96] | 2233 | } |
---|
[3899a537] | 2234 | } |
---|
| 2235 | |
---|
| 2236 | rtems_disk_release (dd); |
---|
| 2237 | } |
---|
[0d15414e] | 2238 | else |
---|
| 2239 | { |
---|
| 2240 | /* |
---|
| 2241 | * We have buffers but no device. Put the BDs back onto the |
---|
| 2242 | * ready queue and exit. |
---|
| 2243 | */ |
---|
| 2244 | /* @todo fixme */ |
---|
| 2245 | } |
---|
| 2246 | } |
---|
| 2247 | } |
---|
| 2248 | |
---|
| 2249 | /** |
---|
| 2250 | * Process the modified list of buffers. There is a sync or modified list that |
---|
| 2251 | * needs to be handled so we have a common function to do the work. |
---|
| 2252 | * |
---|
| 2253 | * @param dev The device to handle. If -1 no device is selected so select the |
---|
| 2254 | * device of the first buffer to be written to disk. |
---|
| 2255 | * @param chain The modified chain to process. |
---|
| 2256 | * @param transfer The chain to append buffers to be written too. |
---|
| 2257 | * @param sync_active If true this is a sync operation so expire all timers. |
---|
| 2258 | * @param update_timers If true update the timers. |
---|
| 2259 | * @param timer_delta It update_timers is true update the timers by this |
---|
| 2260 | * amount. |
---|
| 2261 | */ |
---|
| 2262 | static void |
---|
| 2263 | rtems_bdbuf_swapout_modified_processing (dev_t* dev, |
---|
| 2264 | rtems_chain_control* chain, |
---|
| 2265 | rtems_chain_control* transfer, |
---|
| 2266 | bool sync_active, |
---|
| 2267 | bool update_timers, |
---|
| 2268 | uint32_t timer_delta) |
---|
| 2269 | { |
---|
| 2270 | if (!rtems_chain_is_empty (chain)) |
---|
| 2271 | { |
---|
| 2272 | rtems_chain_node* node = rtems_chain_head (chain); |
---|
| 2273 | node = node->next; |
---|
| 2274 | |
---|
| 2275 | while (!rtems_chain_is_tail (chain, node)) |
---|
| 2276 | { |
---|
| 2277 | rtems_bdbuf_buffer* bd = (rtems_bdbuf_buffer*) node; |
---|
| 2278 | |
---|
| 2279 | /* |
---|
| 2280 | * Check if the buffer's hold timer has reached 0. If a sync is active |
---|
| 2281 | * force all the timers to 0. |
---|
| 2282 | * |
---|
| 2283 | * @note Lots of sync requests will skew this timer. It should be based |
---|
| 2284 | * on TOD to be accurate. Does it matter ? |
---|
| 2285 | */ |
---|
| 2286 | if (sync_active) |
---|
| 2287 | bd->hold_timer = 0; |
---|
| 2288 | |
---|
| 2289 | if (bd->hold_timer) |
---|
| 2290 | { |
---|
| 2291 | if (update_timers) |
---|
| 2292 | { |
---|
| 2293 | if (bd->hold_timer > timer_delta) |
---|
| 2294 | bd->hold_timer -= timer_delta; |
---|
| 2295 | else |
---|
| 2296 | bd->hold_timer = 0; |
---|
| 2297 | } |
---|
| 2298 | |
---|
| 2299 | if (bd->hold_timer) |
---|
| 2300 | { |
---|
| 2301 | node = node->next; |
---|
| 2302 | continue; |
---|
| 2303 | } |
---|
| 2304 | } |
---|
| 2305 | |
---|
| 2306 | /* |
---|
| 2307 | * This assumes we can set dev_t to -1 which is just an |
---|
| 2308 | * assumption. Cannot use the transfer list being empty the sync dev |
---|
| 2309 | * calls sets the dev to use. |
---|
| 2310 | */ |
---|
| 2311 | if (*dev == (dev_t)-1) |
---|
| 2312 | *dev = bd->dev; |
---|
| 2313 | |
---|
| 2314 | if (bd->dev == *dev) |
---|
| 2315 | { |
---|
| 2316 | rtems_chain_node* next_node = node->next; |
---|
| 2317 | rtems_chain_node* tnode = rtems_chain_tail (transfer); |
---|
| 2318 | |
---|
| 2319 | /* |
---|
| 2320 | * The blocks on the transfer list are sorted in block order. This |
---|
| 2321 | * means multi-block transfers for drivers that require consecutive |
---|
| 2322 | * blocks perform better with sorted blocks and for real disks it may |
---|
| 2323 | * help lower head movement. |
---|
| 2324 | */ |
---|
| 2325 | |
---|
| 2326 | bd->state = RTEMS_BDBUF_STATE_TRANSFER; |
---|
| 2327 | |
---|
| 2328 | rtems_chain_extract (node); |
---|
| 2329 | |
---|
| 2330 | tnode = tnode->previous; |
---|
| 2331 | |
---|
| 2332 | while (node && !rtems_chain_is_head (transfer, tnode)) |
---|
| 2333 | { |
---|
| 2334 | rtems_bdbuf_buffer* tbd = (rtems_bdbuf_buffer*) tnode; |
---|
| 2335 | |
---|
| 2336 | if (bd->block > tbd->block) |
---|
| 2337 | { |
---|
| 2338 | rtems_chain_insert (tnode, node); |
---|
| 2339 | node = NULL; |
---|
| 2340 | } |
---|
| 2341 | else |
---|
| 2342 | tnode = tnode->previous; |
---|
| 2343 | } |
---|
| 2344 | |
---|
| 2345 | if (node) |
---|
| 2346 | rtems_chain_prepend (transfer, node); |
---|
| 2347 | |
---|
| 2348 | node = next_node; |
---|
| 2349 | } |
---|
| 2350 | else |
---|
| 2351 | { |
---|
| 2352 | node = node->next; |
---|
| 2353 | } |
---|
| 2354 | } |
---|
| 2355 | } |
---|
| 2356 | } |
---|
| 2357 | |
---|
| 2358 | /** |
---|
| 2359 | * Process the cache's modified buffers. Check the sync list first then the |
---|
| 2360 | * modified list extracting the buffers suitable to be written to disk. We have |
---|
| 2361 | * a device at a time. The task level loop will repeat this operation while |
---|
| 2362 | * there are buffers to be written. If the transfer fails place the buffers |
---|
| 2363 | * back on the modified list and try again later. The cache is unlocked while |
---|
| 2364 | * the buffers are being written to disk. |
---|
| 2365 | * |
---|
| 2366 | * @param timer_delta It update_timers is true update the timers by this |
---|
| 2367 | * amount. |
---|
| 2368 | * @param update_timers If true update the timers. |
---|
| 2369 | * @param transfer The transfer transaction data. |
---|
| 2370 | * |
---|
| 2371 | * @retval true Buffers where written to disk so scan again. |
---|
| 2372 | * @retval false No buffers where written to disk. |
---|
| 2373 | */ |
---|
| 2374 | static bool |
---|
| 2375 | rtems_bdbuf_swapout_processing (unsigned long timer_delta, |
---|
| 2376 | bool update_timers, |
---|
| 2377 | rtems_bdbuf_swapout_transfer* transfer) |
---|
| 2378 | { |
---|
| 2379 | rtems_bdbuf_swapout_worker* worker; |
---|
| 2380 | bool transfered_buffers = false; |
---|
| 2381 | |
---|
| 2382 | rtems_bdbuf_lock_cache (); |
---|
| 2383 | |
---|
| 2384 | /* |
---|
| 2385 | * If a sync is active do not use a worker because the current code does not |
---|
| 2386 | * cleaning up after. We need to know the buffers have been written when |
---|
| 2387 | * syncing to release sync lock and currently worker threads do not return to |
---|
| 2388 | * here. We do not know the worker is the last in a sequence of sync writes |
---|
| 2389 | * until after we have it running so we do not know to tell it to release the |
---|
| 2390 | * lock. The simplest solution is to get the main swap out task perform all |
---|
| 2391 | * sync operations. |
---|
| 2392 | */ |
---|
| 2393 | if (bdbuf_cache.sync_active) |
---|
| 2394 | worker = NULL; |
---|
| 2395 | else |
---|
| 2396 | { |
---|
| 2397 | worker = (rtems_bdbuf_swapout_worker*) |
---|
| 2398 | rtems_chain_get (&bdbuf_cache.swapout_workers); |
---|
| 2399 | if (worker) |
---|
| 2400 | transfer = &worker->transfer; |
---|
[3899a537] | 2401 | } |
---|
[0d15414e] | 2402 | |
---|
| 2403 | rtems_chain_initialize_empty (&transfer->bds); |
---|
| 2404 | transfer->dev = -1; |
---|
| 2405 | |
---|
| 2406 | /* |
---|
| 2407 | * When the sync is for a device limit the sync to that device. If the sync |
---|
| 2408 | * is for a buffer handle process the devices in the order on the sync |
---|
| 2409 | * list. This means the dev is -1. |
---|
| 2410 | */ |
---|
| 2411 | if (bdbuf_cache.sync_active) |
---|
| 2412 | transfer->dev = bdbuf_cache.sync_device; |
---|
| 2413 | |
---|
| 2414 | /* |
---|
| 2415 | * If we have any buffers in the sync queue move them to the modified |
---|
| 2416 | * list. The first sync buffer will select the device we use. |
---|
| 2417 | */ |
---|
| 2418 | rtems_bdbuf_swapout_modified_processing (&transfer->dev, |
---|
| 2419 | &bdbuf_cache.sync, |
---|
| 2420 | &transfer->bds, |
---|
| 2421 | true, false, |
---|
| 2422 | timer_delta); |
---|
| 2423 | |
---|
| 2424 | /* |
---|
| 2425 | * Process the cache's modified list. |
---|
| 2426 | */ |
---|
| 2427 | rtems_bdbuf_swapout_modified_processing (&transfer->dev, |
---|
| 2428 | &bdbuf_cache.modified, |
---|
| 2429 | &transfer->bds, |
---|
| 2430 | bdbuf_cache.sync_active, |
---|
| 2431 | update_timers, |
---|
| 2432 | timer_delta); |
---|
[3899a537] | 2433 | |
---|
[0d15414e] | 2434 | /* |
---|
| 2435 | * We have all the buffers that have been modified for this device so the |
---|
| 2436 | * cache can be unlocked because the state of each buffer has been set to |
---|
| 2437 | * TRANSFER. |
---|
| 2438 | */ |
---|
| 2439 | rtems_bdbuf_unlock_cache (); |
---|
| 2440 | |
---|
| 2441 | /* |
---|
| 2442 | * If there are buffers to transfer to the media transfer them. |
---|
| 2443 | */ |
---|
| 2444 | if (!rtems_chain_is_empty (&transfer->bds)) |
---|
| 2445 | { |
---|
| 2446 | if (worker) |
---|
| 2447 | { |
---|
| 2448 | rtems_status_code sc = rtems_event_send (worker->id, |
---|
| 2449 | RTEMS_BDBUF_SWAPOUT_SYNC); |
---|
| 2450 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2451 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_WAKE); |
---|
| 2452 | } |
---|
| 2453 | else |
---|
| 2454 | { |
---|
| 2455 | rtems_bdbuf_swapout_write (transfer); |
---|
| 2456 | } |
---|
| 2457 | |
---|
| 2458 | transfered_buffers = true; |
---|
| 2459 | } |
---|
| 2460 | |
---|
| 2461 | if (bdbuf_cache.sync_active && !transfered_buffers) |
---|
[c21c850e] | 2462 | { |
---|
[0d15414e] | 2463 | rtems_id sync_requester; |
---|
| 2464 | rtems_bdbuf_lock_cache (); |
---|
| 2465 | sync_requester = bdbuf_cache.sync_requester; |
---|
| 2466 | bdbuf_cache.sync_active = false; |
---|
| 2467 | bdbuf_cache.sync_requester = 0; |
---|
| 2468 | rtems_bdbuf_unlock_cache (); |
---|
[c21c850e] | 2469 | if (sync_requester) |
---|
| 2470 | rtems_event_send (sync_requester, RTEMS_BDBUF_TRANSFER_SYNC); |
---|
| 2471 | } |
---|
[3899a537] | 2472 | |
---|
[0d15414e] | 2473 | return transfered_buffers; |
---|
[e51bd96] | 2474 | } |
---|
| 2475 | |
---|
[3899a537] | 2476 | /** |
---|
[0d15414e] | 2477 | * Allocate the write request and initialise it for good measure. |
---|
[c21c850e] | 2478 | * |
---|
[0d15414e] | 2479 | * @return rtems_blkdev_request* The write reference memory. |
---|
[e51bd96] | 2480 | */ |
---|
[0d15414e] | 2481 | static rtems_blkdev_request* |
---|
| 2482 | rtems_bdbuf_swapout_writereq_alloc (void) |
---|
[e51bd96] | 2483 | { |
---|
[3899a537] | 2484 | /* |
---|
| 2485 | * @note chrisj The rtems_blkdev_request and the array at the end is a hack. |
---|
| 2486 | * I am disappointment at finding code like this in RTEMS. The request should |
---|
| 2487 | * have been a rtems_chain_control. Simple, fast and less storage as the node |
---|
| 2488 | * is already part of the buffer structure. |
---|
| 2489 | */ |
---|
[0d15414e] | 2490 | rtems_blkdev_request* write_req = |
---|
[3899a537] | 2491 | malloc (sizeof (rtems_blkdev_request) + |
---|
| 2492 | (rtems_bdbuf_configuration.max_write_blocks * |
---|
| 2493 | sizeof (rtems_blkdev_sg_buffer))); |
---|
| 2494 | |
---|
| 2495 | if (!write_req) |
---|
| 2496 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_NOMEM); |
---|
| 2497 | |
---|
| 2498 | write_req->req = RTEMS_BLKDEV_REQ_WRITE; |
---|
| 2499 | write_req->req_done = rtems_bdbuf_write_done; |
---|
| 2500 | write_req->done_arg = write_req; |
---|
| 2501 | write_req->io_task = rtems_task_self (); |
---|
| 2502 | |
---|
[0d15414e] | 2503 | return write_req; |
---|
| 2504 | } |
---|
| 2505 | |
---|
| 2506 | /** |
---|
| 2507 | * The swapout worker thread body. |
---|
| 2508 | * |
---|
| 2509 | * @param arg A pointer to the worker thread's private data. |
---|
| 2510 | * @return rtems_task Not used. |
---|
| 2511 | */ |
---|
| 2512 | static rtems_task |
---|
| 2513 | rtems_bdbuf_swapout_worker_task (rtems_task_argument arg) |
---|
| 2514 | { |
---|
| 2515 | rtems_bdbuf_swapout_worker* worker = (rtems_bdbuf_swapout_worker*) arg; |
---|
| 2516 | |
---|
| 2517 | while (worker->enabled) |
---|
| 2518 | { |
---|
| 2519 | rtems_event_set out; |
---|
| 2520 | rtems_status_code sc; |
---|
| 2521 | |
---|
| 2522 | sc = rtems_event_receive (RTEMS_BDBUF_SWAPOUT_SYNC, |
---|
| 2523 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
| 2524 | RTEMS_NO_TIMEOUT, |
---|
| 2525 | &out); |
---|
| 2526 | |
---|
| 2527 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2528 | rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE); |
---|
| 2529 | |
---|
| 2530 | rtems_bdbuf_swapout_write (&worker->transfer); |
---|
| 2531 | |
---|
| 2532 | rtems_bdbuf_lock_cache (); |
---|
| 2533 | |
---|
| 2534 | rtems_chain_initialize_empty (&worker->transfer.bds); |
---|
| 2535 | worker->transfer.dev = -1; |
---|
| 2536 | |
---|
| 2537 | rtems_chain_append (&bdbuf_cache.swapout_workers, &worker->link); |
---|
| 2538 | |
---|
| 2539 | rtems_bdbuf_unlock_cache (); |
---|
| 2540 | } |
---|
| 2541 | |
---|
| 2542 | free (worker->transfer.write_req); |
---|
| 2543 | free (worker); |
---|
| 2544 | |
---|
| 2545 | rtems_task_delete (RTEMS_SELF); |
---|
| 2546 | } |
---|
| 2547 | |
---|
| 2548 | /** |
---|
| 2549 | * Open the swapout worker threads. |
---|
| 2550 | */ |
---|
| 2551 | static void |
---|
| 2552 | rtems_bdbuf_swapout_workers_open (void) |
---|
| 2553 | { |
---|
| 2554 | rtems_status_code sc; |
---|
| 2555 | int w; |
---|
| 2556 | |
---|
| 2557 | rtems_bdbuf_lock_cache (); |
---|
| 2558 | |
---|
| 2559 | for (w = 0; w < rtems_bdbuf_configuration.swapout_workers; w++) |
---|
| 2560 | { |
---|
| 2561 | rtems_bdbuf_swapout_worker* worker; |
---|
| 2562 | |
---|
| 2563 | worker = malloc (sizeof (rtems_bdbuf_swapout_worker)); |
---|
| 2564 | if (!worker) |
---|
| 2565 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_NOMEM); |
---|
| 2566 | |
---|
| 2567 | rtems_chain_append (&bdbuf_cache.swapout_workers, &worker->link); |
---|
| 2568 | worker->enabled = true; |
---|
| 2569 | worker->transfer.write_req = rtems_bdbuf_swapout_writereq_alloc (); |
---|
| 2570 | |
---|
| 2571 | rtems_chain_initialize_empty (&worker->transfer.bds); |
---|
| 2572 | worker->transfer.dev = -1; |
---|
| 2573 | |
---|
| 2574 | sc = rtems_task_create (rtems_build_name('B', 'D', 'o', 'a' + w), |
---|
| 2575 | (rtems_bdbuf_configuration.swapout_priority ? |
---|
| 2576 | rtems_bdbuf_configuration.swapout_priority : |
---|
| 2577 | RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT), |
---|
| 2578 | SWAPOUT_TASK_STACK_SIZE, |
---|
| 2579 | RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR, |
---|
| 2580 | RTEMS_LOCAL | RTEMS_NO_FLOATING_POINT, |
---|
| 2581 | &worker->id); |
---|
| 2582 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2583 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_WK_CREATE); |
---|
| 2584 | |
---|
| 2585 | sc = rtems_task_start (worker->id, |
---|
| 2586 | rtems_bdbuf_swapout_worker_task, |
---|
| 2587 | (rtems_task_argument) worker); |
---|
| 2588 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2589 | rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_WK_START); |
---|
| 2590 | } |
---|
| 2591 | |
---|
| 2592 | rtems_bdbuf_unlock_cache (); |
---|
| 2593 | } |
---|
| 2594 | |
---|
| 2595 | /** |
---|
| 2596 | * Close the swapout worker threads. |
---|
| 2597 | */ |
---|
| 2598 | static void |
---|
| 2599 | rtems_bdbuf_swapout_workers_close (void) |
---|
| 2600 | { |
---|
| 2601 | rtems_chain_node* node; |
---|
| 2602 | |
---|
| 2603 | rtems_bdbuf_lock_cache (); |
---|
| 2604 | |
---|
| 2605 | node = rtems_chain_first (&bdbuf_cache.swapout_workers); |
---|
| 2606 | while (!rtems_chain_is_tail (&bdbuf_cache.swapout_workers, node)) |
---|
| 2607 | { |
---|
| 2608 | rtems_bdbuf_swapout_worker* worker = (rtems_bdbuf_swapout_worker*) node; |
---|
| 2609 | worker->enabled = false; |
---|
| 2610 | rtems_event_send (worker->id, RTEMS_BDBUF_SWAPOUT_SYNC); |
---|
| 2611 | node = rtems_chain_next (node); |
---|
| 2612 | } |
---|
| 2613 | |
---|
| 2614 | rtems_bdbuf_unlock_cache (); |
---|
| 2615 | } |
---|
| 2616 | |
---|
| 2617 | /** |
---|
| 2618 | * Body of task which takes care on flushing modified buffers to the disk. |
---|
| 2619 | * |
---|
| 2620 | * @param arg A pointer to the global cache data. Use the global variable and |
---|
| 2621 | * not this. |
---|
| 2622 | * @return rtems_task Not used. |
---|
| 2623 | */ |
---|
| 2624 | static rtems_task |
---|
| 2625 | rtems_bdbuf_swapout_task (rtems_task_argument arg) |
---|
| 2626 | { |
---|
| 2627 | rtems_bdbuf_swapout_transfer transfer; |
---|
| 2628 | uint32_t period_in_ticks; |
---|
| 2629 | const uint32_t period_in_msecs = bdbuf_config.swapout_period;; |
---|
| 2630 | uint32_t timer_delta; |
---|
| 2631 | |
---|
| 2632 | transfer.write_req = rtems_bdbuf_swapout_writereq_alloc (); |
---|
| 2633 | rtems_chain_initialize_empty (&transfer.bds); |
---|
| 2634 | transfer.dev = -1; |
---|
| 2635 | |
---|
| 2636 | /* |
---|
| 2637 | * Localise the period. |
---|
| 2638 | */ |
---|
[26fb4aa] | 2639 | period_in_ticks = RTEMS_MICROSECONDS_TO_TICKS (period_in_msecs * 1000); |
---|
[3899a537] | 2640 | |
---|
| 2641 | /* |
---|
[c21c850e] | 2642 | * This is temporary. Needs to be changed to use the real time clock. |
---|
[3899a537] | 2643 | */ |
---|
| 2644 | timer_delta = period_in_msecs; |
---|
| 2645 | |
---|
[0d15414e] | 2646 | /* |
---|
| 2647 | * Create the worker threads. |
---|
| 2648 | */ |
---|
| 2649 | rtems_bdbuf_swapout_workers_open (); |
---|
| 2650 | |
---|
| 2651 | while (bdbuf_cache.swapout_enabled) |
---|
[3899a537] | 2652 | { |
---|
[0d15414e] | 2653 | rtems_event_set out; |
---|
| 2654 | rtems_status_code sc; |
---|
[e51bd96] | 2655 | |
---|
[0ebfac19] | 2656 | /* |
---|
[3899a537] | 2657 | * Only update the timers once in the processing cycle. |
---|
| 2658 | */ |
---|
[4f971343] | 2659 | bool update_timers = true; |
---|
[3899a537] | 2660 | |
---|
| 2661 | /* |
---|
[c21c850e] | 2662 | * If we write buffers to any disk perform a check again. We only write a |
---|
[0d15414e] | 2663 | * single device at a time and the cache may have more than one device's |
---|
[c21c850e] | 2664 | * buffers modified waiting to be written. |
---|
[0ebfac19] | 2665 | */ |
---|
[4f971343] | 2666 | bool transfered_buffers; |
---|
[3899a537] | 2667 | |
---|
| 2668 | do |
---|
[e51bd96] | 2669 | { |
---|
[4f971343] | 2670 | transfered_buffers = false; |
---|
[3899a537] | 2671 | |
---|
| 2672 | /* |
---|
[0d15414e] | 2673 | * Extact all the buffers we find for a specific device. The device is |
---|
| 2674 | * the first one we find on a modified list. Process the sync queue of |
---|
| 2675 | * buffers first. |
---|
[3899a537] | 2676 | */ |
---|
[0d15414e] | 2677 | if (rtems_bdbuf_swapout_processing (timer_delta, |
---|
| 2678 | update_timers, |
---|
| 2679 | &transfer)) |
---|
[3899a537] | 2680 | { |
---|
[0d15414e] | 2681 | transfered_buffers = true; |
---|
[3899a537] | 2682 | } |
---|
[0d15414e] | 2683 | |
---|
[3899a537] | 2684 | /* |
---|
| 2685 | * Only update the timers once. |
---|
| 2686 | */ |
---|
[4f971343] | 2687 | update_timers = false; |
---|
[e51bd96] | 2688 | } |
---|
[3899a537] | 2689 | while (transfered_buffers); |
---|
| 2690 | |
---|
| 2691 | sc = rtems_event_receive (RTEMS_BDBUF_SWAPOUT_SYNC, |
---|
| 2692 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
| 2693 | period_in_ticks, |
---|
| 2694 | &out); |
---|
| 2695 | |
---|
| 2696 | if ((sc != RTEMS_SUCCESSFUL) && (sc != RTEMS_TIMEOUT)) |
---|
| 2697 | rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE); |
---|
| 2698 | } |
---|
| 2699 | |
---|
[0d15414e] | 2700 | rtems_bdbuf_swapout_workers_close (); |
---|
| 2701 | |
---|
| 2702 | free (transfer.write_req); |
---|
[3899a537] | 2703 | |
---|
| 2704 | rtems_task_delete (RTEMS_SELF); |
---|
[e51bd96] | 2705 | } |
---|
| 2706 | |
---|