source: rtems/cpukit/libblock/src/bdbuf.c @ 721fe34

4.11
Last change on this file since 721fe34 was 721fe34, checked in by Joel Sherrill <joel.sherrill@…>, on May 31, 2012 at 8:34:36 PM

Fix C files which had two semi-colons at EOL

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