source: rtems/cpukit/libblock/src/bdbuf.c @ 945884fe

4.104.115
Last change on this file since 945884fe was 945884fe, checked in by Chris Johns <chrisj@…>, on 08/06/09 at 03:58:09

2009-08-06 Chris Johns <chrisj@…>

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