source: rtems/cpukit/libblock/include/rtems/bdbuf.h @ cec5c069

4.104.114.95
Last change on this file since cec5c069 was cec5c069, checked in by Joel Sherrill <joel.sherrill@…>, on 08/21/08 at 16:17:35

2008-08-21 Joel Sherrill <joel.sherrill@…>

  • libblock/include/rtems/bdbuf.h, libblock/include/rtems/diskdevs.h, libblock/src/bdbuf.c, libblock/src/blkdev.c, libblock/src/diskdevs.c, libblock/src/ramdisk.c: Eliminate sign mismatch warnings.
  • Property mode set to 100644
File size: 14.2 KB
Line 
1/**
2 * @file rtems/bdbuf.h
3 *
4 * Block Device Buffer Management
5 */
6 
7/*
8 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
9 * Author: Victor V. Vengerov <vvv@oktet.ru>
10 *
11 * Copyright (C) 2008 Chris Johns <chrisj@rtems.org>
12 *    Rewritten to remove score mutex access. Fixes many performance
13 *    issues.
14 *
15 * @(#) bdbuf.h,v 1.9 2005/02/02 00:06:18 joel Exp
16 */
17
18#ifndef _RTEMS_BDBUF_H
19#define _RTEMS_BDBUF_H
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#include <rtems.h>
26#include <rtems/libio.h>
27#include <rtems/chain.h>
28
29#include "rtems/blkdev.h"
30#include "rtems/diskdevs.h"
31
32/**
33 * State of a buffer in the cache.
34 */
35typedef enum
36{
37  RTEMS_BDBUF_STATE_EMPTY = 0,            /*< Not in use. */
38  RTEMS_BDBUF_STATE_READ_AHEAD = 1,       /*< Holds read ahead data only */
39  RTEMS_BDBUF_STATE_CACHED = 2,           /*< In the cache and available */
40  RTEMS_BDBUF_STATE_ACCESS = 3,           /*< The user has the buffer */
41  RTEMS_BDBUF_STATE_MODIFIED = 4,         /*< In the cache but modified */
42  RTEMS_BDBUF_STATE_ACCESS_MODIFIED = 5,  /*< With the user but modified */
43  RTEMS_BDBUF_STATE_SYNC = 6,             /*< Requested to be sync'ed */
44  RTEMS_BDBUF_STATE_TRANSFER = 7          /*< Being transferred to or from disk */
45} rtems_bdbuf_buf_state;
46
47/**
48 * To manage buffers we using buffer descriptors (BD). A BD holds a buffer plus
49 * a range of other information related to managing the buffer in the cache. To
50 * speed-up buffer lookup descriptors are organized in AVL-Tree.  The fields
51 * 'dev' and 'block' are search keys.
52 */
53typedef struct rtems_bdbuf_buffer
54{
55  rtems_chain_node link;       /* Link in the BD onto a number of lists. */
56
57  struct rtems_bdbuf_avl_node
58  {
59    signed char                cache;  /*< Cache */
60    struct rtems_bdbuf_buffer* left;   /*< Left Child */
61    struct rtems_bdbuf_buffer* right;  /*< Right Child */
62    signed char                bal;    /*< The balance of the sub-tree */
63  } avl;
64
65  dev_t             dev;        /*< device number */
66  rtems_blkdev_bnum block;      /*< block number on the device */
67
68  unsigned char*    buffer;     /*< Pointer to the buffer memory area */
69  int               error;      /*< If not 0 indicate an error value (errno)
70                                 * which can be used by user later */
71
72  volatile rtems_bdbuf_buf_state state;  /*< State of the buffer. */
73
74  volatile uint32_t waiters;    /*< The number of threads waiting on this
75                                 * buffer. */
76  rtems_bdpool_id pool;         /*< Identifier of buffer pool to which this buffer
77                                    belongs */
78
79  volatile uint32_t hold_timer; /*< Timer to indicate how long a buffer
80                                 * has been held in the cache modified. */
81} rtems_bdbuf_buffer;
82
83/**
84 * The groups of the blocks with the same size are collected in a pool. Note
85 * that a several of the buffer's groups with the same size can exists.
86 */
87typedef struct rtems_bdbuf_pool
88{
89  uint32_t            blksize;           /*< The size of the blocks (in bytes) */
90  uint32_t            nblks;             /*< Number of blocks in this pool */
91
92  uint32_t            flags;             /*< Configuration flags */
93
94  rtems_id            lock;              /*< The pool lock. Lock this data and
95                                          * all BDs. */
96  rtems_id            sync_lock;         /*< Sync calls lock writes. */
97  boolean             sync_active;       /*< True if a sync is active. */
98  rtems_id            sync_requester;    /*< The sync requester. */
99  dev_t               sync_device;       /*< The device to sync */
100
101  rtems_bdbuf_buffer* tree;             /*< Buffer descriptor lookup AVL tree
102                                         * root */
103  rtems_chain_control ready;            /*< Free buffers list (or read-ahead) */
104  rtems_chain_control lru;              /*< Last recently used list */
105  rtems_chain_control modified;         /*< Modified buffers list */
106  rtems_chain_control sync;             /*< Buffers to sync list */
107
108  rtems_id            access;           /*< Obtain if waiting for a buffer in the
109                                         * ACCESS state. */
110  volatile uint32_t   access_waiters;   /*< Count of access blockers. */
111  rtems_id            transfer;         /*< Obtain if waiting for a buffer in the
112                                         * TRANSFER state. */
113  volatile uint32_t   transfer_waiters; /*< Count of transfer blockers. */
114  rtems_id            waiting;          /*< Obtain if waiting for a buffer and the
115                                         * none are available. */
116  volatile uint32_t   wait_waiters;     /*< Count of waiting blockers. */
117
118  rtems_bdbuf_buffer* bds;              /*< Pointer to table of buffer descriptors
119                                         * allocated for this buffer pool. */
120  void*               buffers;          /*< The buffer's memory. */
121} rtems_bdbuf_pool;
122
123/**
124 * Configuration structure describes block configuration (size, amount, memory
125 * location) for buffering layer pool.
126 */
127typedef struct rtems_bdbuf_pool_config {
128  int            size;      /*< Size of block */
129  int            num;       /*< Number of blocks of appropriate size */
130  unsigned char* mem_area;  /*< Pointer to the blocks location or NULL, in this
131                             * case memory for blocks will be allocated by
132                             * Buffering Layer with the help of RTEMS partition
133                             * manager */
134} rtems_bdbuf_pool_config;
135
136/**
137 * External references provided by the user for each pool in the system.
138 */
139extern rtems_bdbuf_pool_config rtems_bdbuf_pool_configuration[];
140extern int                     rtems_bdbuf_pool_configuration_size;
141
142/**
143 * Buffering configuration definition. See confdefs.h for support on using this
144 * structure.
145 */
146typedef struct rtems_bdbuf_config {
147  uint32_t            max_read_ahead_blocks; /*< Number of blocks to read ahead. */
148  uint32_t            max_write_blocks;      /*< Number of blocks to write at once. */
149  rtems_task_priority swapout_priority;      /*< Priority of the swap out task. */
150  uint32_t            swapout_period;        /*< Period swapout checks buf timers. */
151  uint32_t            swap_block_hold;       /*< Period a buffer is held. */
152} rtems_bdbuf_config;
153
154/**
155 * External referernce to the configuration. The configuration is provided by
156 * the user.
157 */
158extern rtems_bdbuf_config rtems_bdbuf_configuration;
159
160/**
161 * The max_read_ahead_blocks value is altered if there are fewer buffers
162 * than this defined max. This stops thrashing in the cache.
163 */
164#define RTEMS_BDBUF_MAX_READ_AHEAD_BLOCKS_DEFAULT    32
165#define RTEMS_BDBUF_MAX_WRITE_BLOCKS_DEFAULT         16
166#define RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT    15
167#define RTEMS_BDBUF_SWAPOUT_TASK_SWAP_PERIOD_DEFAULT 250  /* milli-seconds */
168#define RTEMS_BDBUF_SWAPOUT_TASK_BLOCK_HOLD_DEFAULT  1000 /* milli-seconds */
169
170/**
171 * Prepare buffering layer to work - initialize buffer descritors and (if it is
172 * neccessary) buffers. Buffers will be allocated accoriding to the
173 * configuration table, each entry describes the size of block and the size of
174 * the pool. After initialization all blocks is placed into the ready state.
175 * lists.
176 *
177 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
178 *         successfully or error code if error is occured)
179 */
180rtems_status_code
181rtems_bdbuf_init (void);
182
183/**
184 * Get block buffer for data to be written into. The buffers is set to the
185 * access or modifed access state. If the buffer is in the cache and modified
186 * the state is access modified else the state is access. This buffer contents
187 * are not initialised if the buffer is not already in the cache. If the block
188 * is already resident in memory it is returned how-ever if not in memory the
189 * buffer is not read from disk. This call is used when writing the whole block
190 * on a disk rather than just changing a part of it. If there is no buffers
191 * available this call will block. A buffer obtained with this call will not be
192 * involved in a transfer request and will not be returned to another user
193 * until released. If the buffer is already with a user when this call is made
194 * the call is blocked until the buffer is returned. The highest priority
195 * waiter will obtain the buffer first.
196 *
197 * The block number is the linear block number. This is relative to the start
198 * of the partition on the media.
199 *
200 * @param device Device number (constructed of major and minor device number)
201 * @param block  Linear media block number
202 * @param bd     Reference to the buffer descriptor pointer.
203 *
204 * @return       RTEMS status code (RTEMS_SUCCESSFUL if operation completed
205 *               successfully or error code if error is occured)
206 */
207rtems_status_code
208rtems_bdbuf_get (dev_t device, rtems_blkdev_bnum block, rtems_bdbuf_buffer** bd);
209
210/**
211 * Get the block buffer and if not already in the cache read from the disk. If
212 * specified block already cached return. The buffer is set to the access or
213 * modifed access state. If the buffer is in the cache and modified the state
214 * is access modified else the state is access. If block is already being read
215 * from disk for being written to disk this call blocks. If the buffer is
216 * waiting to be written it is removed from modified queue and returned to the
217 * user. If the buffer is not in the cache a new buffer is obtained and the
218 * data read from disk. The call may block until these operations complete. A
219 * buffer obtained with this call will not be involved in a transfer request
220 * and will not be returned to another user until released. If the buffer is
221 * already with a user when this call is made the call is blocked until the
222 * buffer is returned. The highest priority waiter will obtain the buffer
223 * first.
224 *
225 * @param device Device number (constructed of major and minor device number)
226 * @param block  Linear media block number
227 * @param bd     Reference to the buffer descriptor pointer.
228 *
229 * @return       RTEMS status code (RTEMS_SUCCESSFUL if operation completed
230 *               successfully or error code if error is occured)
231 */
232rtems_status_code
233rtems_bdbuf_read (dev_t device, rtems_blkdev_bnum block, rtems_bdbuf_buffer** bd);
234
235/**
236 * Release the buffer obtained by a read call back to the cache. If the buffer
237 * was obtained by a get call and was not already in the cache the release
238 * modified call should be used. A buffer released with this call obtained by a
239 * get call may not be in sync with the contents on disk. If the buffer was in
240 * the cache and modified before this call it will be returned to the modified
241 * queue. The buffers is returned to the end of the LRU list.
242 *
243 * @param bd Reference to the buffer descriptor.
244 *
245 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
246 *         successfully or error code if error is occured)
247 */
248rtems_status_code
249rtems_bdbuf_release (rtems_bdbuf_buffer* bd);
250
251/**
252 * Release the buffer allocated with a get or read call placing it on the
253 * modidied list.  If the buffer was not released modified before the hold
254 * timer is set to the configuration value. If the buffer had been released
255 * modified before but not written to disk the hold timer is not updated. The
256 * buffer will be written to disk when the hold timer has expired, there are
257 * not more buffers available in the cache and a get or read buffer needs one
258 * or a sync call has been made. If the buffer is obtained with a get or read
259 * before the hold timer has expired the buffer will be returned to the user.
260 *
261 * @param bd Reference to the buffer descriptor.
262 *
263 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
264 *         successfully or error code if error is occured)
265 */
266rtems_status_code
267rtems_bdbuf_release_modified (rtems_bdbuf_buffer* bd);
268
269/**
270 * Release the buffer as modified and wait until it has been synchronized with
271 * the disk by writing it. This buffer will be the first to be transfer to disk
272 * and other buffers may also be written if the maximum number of blocks in a
273 * requests allows it.
274 *
275 * @note This code does not lock the sync mutex and stop additions to the
276 *       modified queue.
277
278 * @param bd Reference to the buffer descriptor.
279 *
280 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
281 *         successfully or error code if error is occured)
282 */
283rtems_status_code
284rtems_bdbuf_sync (rtems_bdbuf_buffer* bd);
285
286/**
287 * Synchronize all modified buffers for this device with the disk and wait
288 * until the transfers have completed. The sync mutex for the pool is locked
289 * stopping the addition of any further modifed buffers. It is only the
290 * currently modified buffers that are written.
291 *
292 * @note Nesting calls to sync multiple devices attached to a single pool will
293 * be handled sequentially. A nested call will be blocked until the first sync
294 * request has complete. This is only true for device using the same pool.
295 *
296 * @param dev Block device number
297 *
298 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
299 *         successfully or error code if error is occured)
300 */
301rtems_status_code
302rtems_bdbuf_syncdev (dev_t dev);
303
304/**
305 * Find first appropriate buffer pool. This primitive returns the index of
306 * first buffer pool which block size is greater than or equal to specified
307 * size.
308 *
309 * @param block_size Requested block size
310 * @param pool The pool to use for the requested pool size.
311 *
312 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
313 *         successfully or error code if error is occured)
314 * @retval RTEMS_INVALID_SIZE The specified block size is invalid (not a power
315 *         of 2)
316 * @retval RTEMS_NOT_DEFINED The buffer pool for this or greater block size
317 *         is not configured.
318 */
319rtems_status_code
320rtems_bdbuf_find_pool (uint32_t block_size, rtems_bdpool_id *pool);
321
322/**
323 * Obtain characteristics of buffer pool with specified number.
324 *
325 * @param pool Buffer pool number
326 * @param block_size Block size for which buffer pool is configured returned
327 *                   there
328 * @param blocks Number of buffers in buffer pool.
329 *
330 * RETURNS:
331 * @return RTEMS status code (RTEMS_SUCCESSFUL if operation completed
332 *         successfully or error code if error is occured)
333 * @retval RTEMS_INVALID_SIZE The appropriate buffer pool is not configured.
334 *
335 * @note Buffer pools enumerated continuously starting from 0.
336 */
337rtems_status_code
338rtems_bdbuf_get_pool_info (rtems_bdpool_id pool, int *block_size, int *blocks);
339
340#ifdef __cplusplus
341}
342#endif
343
344#endif
Note: See TracBrowser for help on using the repository browser.