Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /**
0002  * @file
0003  *
0004  * @ingroup rtems_bdbuf
0005  *
0006  * @brief Block Device Buffer Management
0007  */
0008 
0009 /*
0010  * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
0011  * Author: Victor V. Vengerov <vvv@oktet.ru>
0012  *
0013  * Copyright (C) 2008,2009 Chris Johns <chrisj@rtems.org>
0014  *    Rewritten to remove score mutex access. Fixes many performance
0015  *    issues.
0016  *    Change to support demand driven variable buffer sizes.
0017  *
0018  * Copyright (C) 2009, 2012 embedded brains GmbH & Co. KG
0019  */
0020 
0021 #ifndef _RTEMS_BDBUF_H
0022 #define _RTEMS_BDBUF_H
0023 
0024 #include <rtems.h>
0025 #include <rtems/libio.h>
0026 #include <rtems/chain.h>
0027 
0028 #include <rtems/blkdev.h>
0029 #include <rtems/diskdevs.h>
0030 
0031 #ifdef __cplusplus
0032 extern "C" {
0033 #endif
0034 
0035 /**
0036  * @defgroup rtems_libblock Block Device Library
0037  *
0038  * @ingroup RTEMSDeviceDrivers
0039  *
0040  * @brief Block device modules.
0041  */
0042 
0043 /**
0044  * @defgroup rtems_bdbuf Block Device Buffer Management
0045  *
0046  * @ingroup rtems_libblock
0047  *
0048  * The Block Device Buffer Management implements a cache between the disk
0049  * devices and file systems.  The code provides read-ahead and write queuing to
0050  * the drivers and fast cache look-up using an AVL tree.
0051  *
0052  * The block size used by a file system can be set at runtime and must be a
0053  * multiple of the disk device block size.  The disk device's physical block
0054  * size is called the media block size.  The file system can set the block size
0055  * it uses to a larger multiple of the media block size.  The driver must be
0056  * able to handle buffers sizes larger than one media block.
0057  *
0058  * The user configures the amount of memory to be used as buffers in the cache,
0059  * and the minimum and maximum buffer size.  The cache will allocate additional
0060  * memory for the buffer descriptors and groups.  There are enough buffer
0061  * descriptors allocated so all the buffer memory can be used as minimum sized
0062  * buffers.
0063  *
0064  * The cache is a single pool of buffers.  The buffer memory is divided into
0065  * groups where the size of buffer memory allocated to a group is the maximum
0066  * buffer size.  A group's memory can be divided down into small buffer sizes
0067  * that are a multiple of 2 of the minimum buffer size.  A group is the minimum
0068  * allocation unit for buffers of a specific size.  If a buffer of maximum size
0069  * is request the group will have a single buffer.  If a buffer of minimum size
0070  * is requested the group is divided into minimum sized buffers and the
0071  * remaining buffers are held ready for use.  A group keeps track of which
0072  * buffers are with a file system or driver and groups who have buffer in use
0073  * cannot be realloced.  Groups with no buffers in use can be taken and
0074  * realloced to a new size.  This is how buffers of different sizes move around
0075  * the cache.
0076 
0077  * The buffers are held in various lists in the cache.  All buffers follow this
0078  * state machine:
0079  *
0080  * @dot
0081  * digraph state {
0082  *   size="16,8";
0083  *   f [label="FREE",style="filled",fillcolor="aquamarine"];
0084  *   e [label="EMPTY",style="filled",fillcolor="seagreen"];
0085  *   c [label="CACHED",style="filled",fillcolor="chartreuse"];
0086  *   ac [label="ACCESS CACHED",style="filled",fillcolor="royalblue"];
0087  *   am [label="ACCESS MODIFIED",style="filled",fillcolor="royalblue"];
0088  *   ae [label="ACCESS EMPTY",style="filled",fillcolor="royalblue"];
0089  *   ap [label="ACCESS PURGED",style="filled",fillcolor="royalblue"];
0090  *   t [label="TRANSFER",style="filled",fillcolor="red"];
0091  *   tp [label="TRANSFER PURGED",style="filled",fillcolor="red"];
0092  *   s [label="SYNC",style="filled",fillcolor="red"];
0093  *   m [label="MODIFIED",style="filled",fillcolor="gold"];
0094  *   i [label="INITIAL"];
0095  *
0096  *   legend_transfer [label="Transfer Wake-Up",fontcolor="red",shape="none"];
0097  *   legend_access [label="Access Wake-Up",fontcolor="royalblue",shape="none"];
0098  *
0099  *   i -> f [label="Init"];
0100  *   f -> e [label="Buffer Recycle"];
0101  *   e -> ae [label="Get"];
0102  *   e -> t [label="Read"];
0103  *   e -> f [label="Nobody Waits"];
0104  *   c -> ac [label="Get\nRead"];
0105  *   c -> e [label="Buffer Recycle\nPurge"];
0106  *   c -> f [label="Reallocate\nBlock Size Changed"];
0107  *   t -> c [label="Transfer Done",color="red",fontcolor="red"];
0108  *   t -> e [label="Transfer Error",color="red",fontcolor="red"];
0109  *   t -> tp [label="Purge"];
0110  *   tp -> e [label="Transfer Done\nTransfer Error",color="red",fontcolor="red"];
0111  *   m -> t [label="Swapout"];
0112  *   m -> s [label="Block Size Changed"];
0113  *   m -> am [label="Get\nRead"];
0114  *   m -> e [label="Purge"];
0115  *   ac -> m [label="Release Modified",color="royalblue",fontcolor="royalblue"];
0116  *   ac -> s [label="Sync",color="royalblue",fontcolor="royalblue"];
0117  *   ac -> c [label="Release",color="royalblue",fontcolor="royalblue"];
0118  *   ac -> ap [label="Purge"];
0119  *   am -> m [label="Release\nRelease Modified",color="royalblue",fontcolor="royalblue"];
0120  *   am -> s [label="Sync",color="royalblue",fontcolor="royalblue"];
0121  *   am -> ap [label="Purge"];
0122  *   ae -> m [label="Release Modified",color="royalblue",fontcolor="royalblue"];
0123  *   ae -> s [label="Sync",color="royalblue",fontcolor="royalblue"];
0124  *   ae -> e [label="Release",color="royalblue",fontcolor="royalblue"];
0125  *   ae -> ap [label="Purge"];
0126  *   ap -> e [label="Release\nRelease Modified\nSync",color="royalblue",fontcolor="royalblue"];
0127  *   s -> t [label="Swapout"];
0128  *   s -> e [label="Purge",color="red",fontcolor="red"];
0129  * }
0130  * @enddot
0131  *
0132  * Empty or cached buffers are added to the LRU list and removed from this
0133  * queue when a caller requests a buffer.  This is referred to as getting a
0134  * buffer in the code and the event get in the state diagram.  The buffer is
0135  * assigned to a block and inserted to the AVL based on the block/device key.
0136  * If the block is to be read by the user and not in the cache it is transfered
0137  * from the disk into memory.  If no buffers are on the LRU list the modified
0138  * list is checked.  If buffers are on the modified the swap out task will be
0139  * woken.  The request blocks until a buffer is available for recycle.
0140  *
0141  * A block being accessed is given to the file system layer and not accessible
0142  * to another requester until released back to the cache.  The same goes to a
0143  * buffer in the transfer state.  The transfer state means being read or
0144  * written.  If the file system has modified the block and releases it as
0145  * modified it placed on the cache's modified list and a hold timer
0146  * initialised.  The buffer is held for the hold time before being written to
0147  * disk.  Buffers are held for a configurable period of time on the modified
0148  * list as a write sets the state to transfer and this locks the buffer out
0149  * from the file system until the write completes.  Buffers are often accessed
0150  * and modified in a series of small updates so if sent to the disk when
0151  * released as modified the user would have to block waiting until it had been
0152  * written.  This would be a performance problem.
0153  *
0154  * The code performs multiple block reads and writes.  Multiple block reads or
0155  * read-ahead increases performance with hardware that supports it.  It also
0156  * helps with a large cache as the disk head movement is reduced.  It however
0157  * is a speculative operation so excessive use can remove valuable and needed
0158  * blocks from the cache.  The read-ahead is triggered after two misses of
0159  * ascending consecutive blocks or a read hit of a block read by the
0160  * most-resent read-ahead transfer.  The read-ahead works per disk, but all
0161  * transfers are issued by the read-ahead task.
0162  *
0163  * The cache has the following lists of buffers:
0164  *  - LRU: Accessed or transfered buffers released in least recently used
0165  *  order.  Empty buffers will be placed to the front.
0166  *  - Modified: Buffers waiting to be written to disk.
0167  *  - Sync: Buffers to be synchronized with the disk.
0168  *
0169  * A cache look-up will be performed to find a suitable buffer.  A suitable
0170  * buffer is one that matches the same allocation size as the device the buffer
0171  * is for.  The a buffer's group has no buffers in use with the file system or
0172  * driver the group is reallocated.  This means the buffers in the group are
0173  * invalidated, resized and placed on the LRU queue.  There is a performance
0174  * issue with this design.  The reallocation of a group may forced recently
0175  * accessed buffers out of the cache when they should not.  The design should be
0176  * change to have groups on a LRU list if they have no buffers in use.
0177  */
0178 /**@{**/
0179 
0180 /**
0181  * @brief State of a buffer of the cache.
0182  *
0183  * The state has several implications.  Depending on the state a buffer can be
0184  * in the AVL tree, in a list, in use by an entity and a group user or not.
0185  *
0186  * <table>
0187  *   <tr>
0188  *     <th>State</th><th>Valid Data</th><th>AVL Tree</th>
0189  *     <th>LRU List</th><th>Modified List</th><th>Synchronization List</th>
0190  *     <th>Group User</th><th>External User</th>
0191  *   </tr>
0192  *   <tr>
0193  *     <td>FREE</td><td></td><td></td>
0194  *     <td>X</td><td></td><td></td><td></td><td></td>
0195  *   </tr>
0196  *   <tr>
0197  *     <td>EMPTY</td><td></td><td>X</td>
0198  *     <td></td><td></td><td></td><td></td><td></td>
0199  *   </tr>
0200  *   <tr>
0201  *     <td>CACHED</td><td>X</td><td>X</td>
0202  *     <td>X</td><td></td><td></td><td></td><td></td>
0203  *   </tr>
0204  *   <tr>
0205  *     <td>ACCESS CACHED</td><td>X</td><td>X</td>
0206  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0207  *   </tr>
0208  *   <tr>
0209  *     <td>ACCESS MODIFIED</td><td>X</td><td>X</td>
0210  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0211  *   </tr>
0212  *   <tr>
0213  *     <td>ACCESS EMPTY</td><td></td><td>X</td>
0214  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0215  *   </tr>
0216  *   <tr>
0217  *     <td>ACCESS PURGED</td><td></td><td>X</td>
0218  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0219  *   </tr>
0220  *   <tr>
0221  *     <td>MODIFIED</td><td>X</td><td>X</td>
0222  *     <td></td><td>X</td><td></td><td>X</td><td></td>
0223  *   </tr>
0224  *   <tr>
0225  *     <td>SYNC</td><td>X</td><td>X</td>
0226  *     <td></td><td></td><td>X</td><td>X</td><td></td>
0227  *   </tr>
0228  *   <tr>
0229  *     <td>TRANSFER</td><td>X</td><td>X</td>
0230  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0231  *   </tr>
0232  *   <tr>
0233  *     <td>TRANSFER PURGED</td><td></td><td>X</td>
0234  *     <td></td><td></td><td></td><td>X</td><td>X</td>
0235  *   </tr>
0236  * </table>
0237  */
0238 typedef enum
0239 {
0240   /**
0241    * @brief Free.
0242    */
0243   RTEMS_BDBUF_STATE_FREE = 0,
0244 
0245   /**
0246    * @brief Empty.
0247    */
0248   RTEMS_BDBUF_STATE_EMPTY,
0249 
0250   /**
0251    * @brief Cached.
0252    */
0253   RTEMS_BDBUF_STATE_CACHED,
0254 
0255   /**
0256    * @brief Accessed by upper layer with cached data.
0257    */
0258   RTEMS_BDBUF_STATE_ACCESS_CACHED,
0259 
0260   /**
0261    * @brief Accessed by upper layer with modified data.
0262    */
0263   RTEMS_BDBUF_STATE_ACCESS_MODIFIED,
0264 
0265   /**
0266    * @brief Accessed by upper layer with invalid data.
0267    */
0268   RTEMS_BDBUF_STATE_ACCESS_EMPTY,
0269 
0270   /**
0271    * @brief Accessed by upper layer with purged data.
0272    */
0273   RTEMS_BDBUF_STATE_ACCESS_PURGED,
0274 
0275   /**
0276    * @brief Modified by upper layer.
0277    */
0278   RTEMS_BDBUF_STATE_MODIFIED,
0279 
0280   /**
0281    * @brief Scheduled for synchronization.
0282    */
0283   RTEMS_BDBUF_STATE_SYNC,
0284 
0285   /**
0286    * @brief In transfer by block device driver.
0287    */
0288   RTEMS_BDBUF_STATE_TRANSFER,
0289 
0290   /**
0291    * @brief In transfer by block device driver and purged.
0292    */
0293   RTEMS_BDBUF_STATE_TRANSFER_PURGED
0294 } rtems_bdbuf_buf_state;
0295 
0296 /**
0297  * Forward reference to the block.
0298  */
0299 struct rtems_bdbuf_group;
0300 typedef struct rtems_bdbuf_group rtems_bdbuf_group;
0301 
0302 /**
0303  * To manage buffers we using buffer descriptors (BD). A BD holds a buffer plus
0304  * a range of other information related to managing the buffer in the cache. To
0305  * speed-up buffer lookup descriptors are organized in AVL-Tree. The fields
0306  * 'dd' and 'block' are search keys.
0307  */
0308 typedef struct rtems_bdbuf_buffer
0309 {
0310   rtems_chain_node link;       /**< Link the BD onto a number of lists. */
0311 
0312   struct rtems_bdbuf_avl_node
0313   {
0314     struct rtems_bdbuf_buffer* left;   /**< Left Child */
0315     struct rtems_bdbuf_buffer* right;  /**< Right Child */
0316     signed char                cache;  /**< Cache */
0317     signed char                bal;    /**< The balance of the sub-tree */
0318   } avl;
0319 
0320   rtems_disk_device *dd;        /**< disk device */
0321 
0322   rtems_blkdev_bnum block;      /**< block number on the device */
0323 
0324   unsigned char*    buffer;     /**< Pointer to the buffer memory area */
0325 
0326   rtems_bdbuf_buf_state state;           /**< State of the buffer. */
0327 
0328   uint32_t waiters;              /**< The number of threads waiting on this
0329                                   * buffer. */
0330   rtems_bdbuf_group* group;      /**< Pointer to the group of BDs this BD is
0331                                   * part of. */
0332   uint32_t hold_timer;           /**< Timer to indicate how long a buffer
0333                                   * has been held in the cache modified. */
0334 
0335   int   references;              /**< Allow reference counting by owner. */
0336   void* user;                    /**< User data. */
0337 } rtems_bdbuf_buffer;
0338 
0339 /**
0340  * A group is a continuous block of buffer descriptors. A group covers the
0341  * maximum configured buffer size and is the allocation size for the buffers to
0342  * a specific buffer size. If you allocate a buffer to be a specific size, all
0343  * buffers in the group, if there are more than 1 will also be that size. The
0344  * number of buffers in a group is a multiple of 2, ie 1, 2, 4, 8, etc.
0345  */
0346 struct rtems_bdbuf_group
0347 {
0348   rtems_chain_node    link;          /**< Link the groups on a LRU list if they
0349                                       * have no buffers in use. */
0350   size_t              bds_per_group; /**< The number of BD allocated to this
0351                                       * group. This value must be a multiple of
0352                                       * 2. */
0353   uint32_t            users;         /**< How many users the block has. */
0354   rtems_bdbuf_buffer* bdbuf;         /**< First BD this block covers. */
0355 };
0356 
0357 /**
0358  * Buffering configuration definition. See confdefs.h for support on using this
0359  * structure.
0360  */
0361 typedef struct rtems_bdbuf_config {
0362   uint32_t            max_read_ahead_blocks;   /**< Number of blocks to read
0363                                                 * ahead. */
0364   uint32_t            max_write_blocks;        /**< Number of blocks to write
0365                                                 * at once. */
0366   rtems_task_priority swapout_priority;        /**< Priority of the swap out
0367                                                 * task. */
0368   uint32_t            swapout_period;          /**< Period swap-out checks buf
0369                                                 * timers. */
0370   uint32_t            swap_block_hold;         /**< Period a buffer is held. */
0371   size_t              swapout_workers;         /**< The number of worker
0372                                                 * threads for the swap-out
0373                                                 * task. */
0374   rtems_task_priority swapout_worker_priority; /**< Priority of the swap out
0375                                                 * task. */
0376   size_t              task_stack_size;         /**< Task stack size for swap-out
0377                                                 * task and worker threads. */
0378   size_t              size;                    /**< Size of memory in the
0379                                                 * cache */
0380   uint32_t            buffer_min;              /**< Minimum buffer size. */
0381   uint32_t            buffer_max;              /**< Maximum buffer size
0382                                                 * supported. It is also the
0383                                                 * allocation size. */
0384   rtems_task_priority read_ahead_priority;     /**< Priority of the read-ahead
0385                                                 * task. */
0386 } rtems_bdbuf_config;
0387 
0388 /**
0389  * External reference to the configuration.
0390  *
0391  * The configuration is provided by the application.
0392  */
0393 extern const rtems_bdbuf_config rtems_bdbuf_configuration;
0394 
0395 /**
0396  * The default value for the maximum read-ahead blocks disables the read-ahead
0397  * feature.
0398  */
0399 #define RTEMS_BDBUF_MAX_READ_AHEAD_BLOCKS_DEFAULT    0
0400 
0401 /**
0402  * Default maximum number of blocks to write at once.
0403  */
0404 #define RTEMS_BDBUF_MAX_WRITE_BLOCKS_DEFAULT         16
0405 
0406 /**
0407  * Default swap-out task priority.
0408  */
0409 #define RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT    15
0410 
0411 /**
0412  * Default swap-out task swap period in milli seconds.
0413  */
0414 #define RTEMS_BDBUF_SWAPOUT_TASK_SWAP_PERIOD_DEFAULT 250
0415 
0416 /**
0417  * Default swap-out task block hold time in milli seconds.
0418  */
0419 #define RTEMS_BDBUF_SWAPOUT_TASK_BLOCK_HOLD_DEFAULT  1000
0420 
0421 /**
0422  * Default swap-out worker tasks. Currently disabled.
0423  */
0424 #define RTEMS_BDBUF_SWAPOUT_WORKER_TASKS_DEFAULT     0
0425 
0426 /**
0427  * Default swap-out worker task priority. The same as the swap-out task.
0428  */
0429 #define RTEMS_BDBUF_SWAPOUT_WORKER_TASK_PRIORITY_DEFAULT \
0430                              RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT
0431 
0432 /**
0433  * Default read-ahead task priority.  The same as the swap-out task.
0434  */
0435 #define RTEMS_BDBUF_READ_AHEAD_TASK_PRIORITY_DEFAULT \
0436   RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT
0437 
0438 /**
0439  * Default task stack size for swap-out and worker tasks.
0440  */
0441 #define RTEMS_BDBUF_TASK_STACK_SIZE_DEFAULT RTEMS_MINIMUM_STACK_SIZE
0442 
0443 /**
0444  * Default size of memory allocated to the cache.
0445  */
0446 #define RTEMS_BDBUF_CACHE_MEMORY_SIZE_DEFAULT (64 * 512)
0447 
0448 /**
0449  * Default minimum size of buffers.
0450  */
0451 #define RTEMS_BDBUF_BUFFER_MIN_SIZE_DEFAULT (512)
0452 
0453 /**
0454  * Default maximum size of buffers.
0455  */
0456 #define RTEMS_BDBUF_BUFFER_MAX_SIZE_DEFAULT (4096)
0457 
0458 /**
0459  * Prepare buffering layer to work - initialize buffer descritors and (if it is
0460  * neccessary) buffers. After initialization all blocks is placed into the
0461  * ready state.
0462  *
0463  * @retval RTEMS_SUCCESSFUL Successful operation. 
0464  * @retval RTEMS_CALLED_FROM_ISR Called from an interrupt context.
0465  * @retval RTEMS_INVALID_NUMBER The buffer maximum is not an integral multiple
0466  * of the buffer minimum.  The maximum read-ahead blocks count is too large.
0467  * @retval RTEMS_RESOURCE_IN_USE Already initialized.
0468  * @retval RTEMS_UNSATISFIED Not enough resources.
0469  */
0470 rtems_status_code
0471 rtems_bdbuf_init (void);
0472 
0473 /**
0474  * Get block buffer for data to be written into. The buffers is set to the
0475  * access or modified access state. If the buffer is in the cache and modified
0476  * the state is access modified else the state is access. This buffer contents
0477  * are not initialised if the buffer is not already in the cache. If the block
0478  * is already resident in memory it is returned how-ever if not in memory the
0479  * buffer is not read from disk. This call is used when writing the whole block
0480  * on a disk rather than just changing a part of it. If there is no buffers
0481  * available this call will block. A buffer obtained with this call will not be
0482  * involved in a transfer request and will not be returned to another user
0483  * until released. If the buffer is already with a user when this call is made
0484  * the call is blocked until the buffer is returned. The highest priority
0485  * waiter will obtain the buffer first.
0486  *
0487  * The block number is the linear block number. This is relative to the start
0488  * of the partition on the media.
0489  *
0490  * Before you can use this function, the rtems_bdbuf_init() routine must be
0491  * called at least once to initialize the cache, otherwise a fatal error will
0492  * occur.
0493  *
0494  * @param dd [in] The disk device.
0495  * @param block [in] Linear media block number.
0496  * @param bd [out] Reference to the buffer descriptor pointer.
0497  *
0498  * @retval RTEMS_SUCCESSFUL Successful operation. 
0499  * @retval RTEMS_INVALID_ID Invalid block number.
0500  */
0501 rtems_status_code
0502 rtems_bdbuf_get (
0503   rtems_disk_device *dd,
0504   rtems_blkdev_bnum block,
0505   rtems_bdbuf_buffer** bd
0506 );
0507 
0508 /**
0509  * Get the block buffer and if not already in the cache read from the disk. If
0510  * specified block already cached return. The buffer is set to the access or
0511  * modified access state. If the buffer is in the cache and modified the state
0512  * is access modified else the state is access. If block is already being read
0513  * from disk for being written to disk this call blocks. If the buffer is
0514  * waiting to be written it is removed from modified queue and returned to the
0515  * user. If the buffer is not in the cache a new buffer is obtained and the
0516  * data read from disk. The call may block until these operations complete. A
0517  * buffer obtained with this call will not be involved in a transfer request
0518  * and will not be returned to another user until released. If the buffer is
0519  * already with a user when this call is made the call is blocked until the
0520  * buffer is returned. The highest priority waiter will obtain the buffer
0521  * first.
0522  *
0523  * Before you can use this function, the rtems_bdbuf_init() routine must be
0524  * called at least once to initialize the cache, otherwise a fatal error will
0525  * occur.
0526  *
0527  * @param dd [in] The disk device.
0528  * @param block [in] Linear media block number.
0529  * @param bd [out] Reference to the buffer descriptor pointer.
0530  *
0531  * @retval RTEMS_SUCCESSFUL Successful operation. 
0532  * @retval RTEMS_INVALID_ID Invalid block number.
0533  * @retval RTEMS_IO_ERROR IO error.
0534  */
0535 rtems_status_code
0536 rtems_bdbuf_read (
0537   rtems_disk_device *dd,
0538   rtems_blkdev_bnum block,
0539   rtems_bdbuf_buffer** bd
0540 );
0541 
0542 /**
0543  * @brief Give a hint which blocks should be cached next.
0544  *
0545  * Provide a hint to the read ahead mechanism which blocks should be cached
0546  * next. This overwrites the default linear pattern. You should use it in (for
0547  * example) a file system to tell bdbuf where the next part of a fragmented file
0548  * is. If you know the length of the file, you can provide that too.
0549  *
0550  * Before you can use this function, the rtems_bdbuf_init() routine must be
0551  * called at least once to initialize everything. Otherwise you might get
0552  * unexpected results.
0553  *
0554  * @param dd [in] The disk device.
0555  * @param block [in] Linear media block number.
0556  * @param nr_blocks [in] Number of consecutive blocks that can be pre-fetched.
0557  */
0558 void
0559 rtems_bdbuf_peek (
0560   rtems_disk_device *dd,
0561   rtems_blkdev_bnum block,
0562   uint32_t nr_blocks
0563 );
0564 
0565 /**
0566  * Release the buffer obtained by a read call back to the cache. If the buffer
0567  * was obtained by a get call and was not already in the cache the release
0568  * modified call should be used. A buffer released with this call obtained by a
0569  * get call may not be in sync with the contents on disk. If the buffer was in
0570  * the cache and modified before this call it will be returned to the modified
0571  * queue. The buffers is returned to the end of the LRU list.
0572  *
0573  * Before you can use this function, the rtems_bdbuf_init() routine must be
0574  * called at least once to initialize the cache, otherwise a fatal error will
0575  * occur.
0576  *
0577  * @param bd [in] Reference to the buffer descriptor.  The buffer descriptor
0578  * reference must not be @c NULL and must be obtained via rtems_bdbuf_get() or
0579  * rtems_bdbuf_read().
0580  *
0581  * @retval RTEMS_SUCCESSFUL Successful operation. 
0582  * @retval RTEMS_INVALID_ADDRESS The reference is NULL.
0583  */
0584 rtems_status_code
0585 rtems_bdbuf_release (rtems_bdbuf_buffer* bd);
0586 
0587 /**
0588  * Release the buffer allocated with a get or read call placing it on the
0589  * modified list.  If the buffer was not released modified before the hold
0590  * timer is set to the configuration value. If the buffer had been released
0591  * modified before but not written to disk the hold timer is not updated. The
0592  * buffer will be written to disk when the hold timer has expired, there are
0593  * not more buffers available in the cache and a get or read buffer needs one
0594  * or a sync call has been made. If the buffer is obtained with a get or read
0595  * before the hold timer has expired the buffer will be returned to the user.
0596  *
0597  * Before you can use this function, the rtems_bdbuf_init() routine must be
0598  * called at least once to initialize the cache, otherwise a fatal error will
0599  * occur.
0600  *
0601  * @param bd [in] Reference to the buffer descriptor.  The buffer descriptor
0602  * reference must not be @c NULL and must be obtained via rtems_bdbuf_get() or
0603  * rtems_bdbuf_read().
0604  *
0605  * @retval RTEMS_SUCCESSFUL Successful operation. 
0606  * @retval RTEMS_INVALID_ADDRESS The reference is NULL.
0607  */
0608 rtems_status_code
0609 rtems_bdbuf_release_modified (rtems_bdbuf_buffer* bd);
0610 
0611 /**
0612  * Release the buffer as modified and wait until it has been synchronized with
0613  * the disk by writing it. This buffer will be the first to be transfer to disk
0614  * and other buffers may also be written if the maximum number of blocks in a
0615  * requests allows it.
0616  *
0617  * @note This code does not lock the sync mutex and stop additions to the
0618  *       modified queue.
0619  *
0620  * Before you can use this function, the rtems_bdbuf_init() routine must be
0621  * called at least once to initialize the cache, otherwise a fatal error will
0622  * occur.
0623  *
0624  * @param bd [in] Reference to the buffer descriptor.  The buffer descriptor
0625  * reference must not be @c NULL and must be obtained via rtems_bdbuf_get() or
0626  * rtems_bdbuf_read().
0627  *
0628  * @retval RTEMS_SUCCESSFUL Successful operation. 
0629  * @retval RTEMS_INVALID_ADDRESS The reference is NULL.
0630  */
0631 rtems_status_code
0632 rtems_bdbuf_sync (rtems_bdbuf_buffer* bd);
0633 
0634 /**
0635  * Synchronize all modified buffers for this device with the disk and wait
0636  * until the transfers have completed. The sync mutex for the cache is locked
0637  * stopping the addition of any further modified buffers. It is only the
0638  * currently modified buffers that are written.
0639  *
0640  * @note Nesting calls to sync multiple devices will be handled sequentially. A
0641  * nested call will be blocked until the first sync request has complete.
0642  *
0643  * Before you can use this function, the rtems_bdbuf_init() routine must be
0644  * called at least once to initialize the cache, otherwise a fatal error will
0645  * occur.
0646  *
0647  * @param dd [in] The disk device.
0648  *
0649  * @retval RTEMS_SUCCESSFUL Successful operation. 
0650  */
0651 rtems_status_code
0652 rtems_bdbuf_syncdev (rtems_disk_device *dd);
0653 
0654 /**
0655  * @brief Purges all buffers corresponding to the disk device @a dd.
0656  *
0657  * This may result in loss of data.  The read-ahead state of this device is reset.
0658  *
0659  * Before you can use this function, the rtems_bdbuf_init() routine must be
0660  * called at least once to initialize the cache, otherwise a fatal error will
0661  * occur.
0662  *
0663  * @param dd [in] The disk device.
0664  */
0665 void
0666 rtems_bdbuf_purge_dev (rtems_disk_device *dd);
0667 
0668 /**
0669  * @brief Sets the block size of a disk device.
0670  *
0671  * This will set the block size derived fields of the disk device.  If
0672  * requested the disk device is synchronized before the block size change
0673  * occurs.  Since the cache is unlocked during the synchronization operation
0674  * some tasks may access the disk device in the meantime.  This may result in
0675  * loss of data.  After the synchronization the disk device is purged to ensure
0676  * a consistent cache state and the block size change occurs.  This also resets
0677  * the read-ahead state of this disk device.  Due to the purge operation this
0678  * may result in loss of data.
0679  *
0680  * Before you can use this function, the rtems_bdbuf_init() routine must be
0681  * called at least once to initialize the cache, otherwise a fatal error will
0682  * occur.
0683  *
0684  * @param dd [in, out] The disk device.
0685  * @param block_size [in] The new block size in bytes.
0686  * @param sync [in] If @c true, then synchronize the disk device before the
0687  * block size change.
0688  *
0689  * @retval RTEMS_SUCCESSFUL Successful operation. 
0690  * @retval RTEMS_INVALID_NUMBER Invalid block size.
0691  */
0692 rtems_status_code
0693 rtems_bdbuf_set_block_size (rtems_disk_device *dd,
0694                             uint32_t           block_size,
0695                             bool               sync);
0696 
0697 /**
0698  * @brief Returns the block device statistics.
0699  */
0700 void
0701 rtems_bdbuf_get_device_stats (const rtems_disk_device *dd,
0702                               rtems_blkdev_stats      *stats);
0703 
0704 /**
0705  * @brief Resets the block device statistics.
0706  */
0707 void
0708 rtems_bdbuf_reset_device_stats (rtems_disk_device *dd);
0709 
0710 /** @} */
0711 
0712 #ifdef __cplusplus
0713 }
0714 #endif
0715 
0716 #endif