source: rtems/cpukit/libblock/src/bdbuf.c @ 0d15414e

4.104.115
Last change on this file since 0d15414e was 0d15414e, checked in by Chris Johns <chrisj@…>, on 08/05/09 at 00:00:54

009-08-05 Chris Johns <chrisj@…>

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