source: rtems/cpukit/libblock/include/rtems/diskdevs.h @ df84ca2

4.115
Last change on this file since df84ca2 was df84ca2, checked in by Sebastian Huber <sebastian.huber@…>, on 06/04/12 at 11:26:13

libblock: Rename structure

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_disk
5 *
6 * @brief Block device disk management API.
7 */
8
9/*
10 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 * Author: Victor V. Vengerov <vvv@oktet.ru>
12 */
13
14#ifndef _RTEMS_DISKDEVS_H
15#define _RTEMS_DISKDEVS_H
16
17#include <rtems.h>
18#include <rtems/libio.h>
19#include <rtems/chain.h>
20#include <stdlib.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26typedef struct rtems_disk_device rtems_disk_device;
27
28/**
29 * @defgroup rtems_disk Block Device Disk Management
30 *
31 * @ingroup rtems_libblock
32 *
33 * @brief This module provides functions to manage disk devices.
34 *
35 * A disk is a set of blocks which are identified by a consecutive set of
36 * non-negative integers starting at zero.  There are also logical disks which
37 * contain a subset of consecutive disk blocks.  The logical disks are used to
38 * represent the partitions of a disk.  The disk devices are accessed via the
39 * @ref rtems_bdbuf "block device buffer module".
40 *
41 * @{
42 */
43
44/**
45 * @brief Block device block index type.
46 */
47typedef uint32_t rtems_blkdev_bnum;
48
49/**
50 * @brief Block device IO control handler type.
51 */
52typedef int (*rtems_block_device_ioctl)(
53  rtems_disk_device *dd,
54  uint32_t req,
55  void *argp
56);
57
58/**
59 * @brief Trigger value to disable further read-ahead requests.
60 */
61#define RTEMS_DISK_READ_AHEAD_NO_TRIGGER ((rtems_blkdev_bnum) -1)
62
63/**
64 * @brief Block device read-ahead control.
65 */
66typedef struct {
67  /**
68   * @brief Chain node for the read-ahead request queue of the read-ahead task.
69   */
70  rtems_chain_node node;
71
72  /**
73   * @brief Block value to trigger the read-ahead request.
74   *
75   * A value of @ref RTEMS_DISK_READ_AHEAD_NO_TRIGGER will disable further
76   * read-ahead requests since no valid block can have this value.
77   */
78  rtems_blkdev_bnum trigger;
79
80  /**
81   * @brief Start block for the next read-ahead request.
82   *
83   * In case the trigger value is out of range of valid blocks, this value my
84   * be arbitrary.
85   */
86  rtems_blkdev_bnum next;
87} rtems_blkdev_read_ahead;
88
89/**
90 * @brief Description of a disk device (logical and physical disks).
91 *
92 * An array of pointer tables to rtems_disk_device structures is maintained.
93 * The first table will be indexed by the major number and the second table
94 * will be indexed by the minor number.  This allows quick lookup using a data
95 * structure of moderated size.
96 */
97struct rtems_disk_device {
98  /**
99   * @brief Device identifier (concatenation of major and minor number).
100   */
101  dev_t dev;
102
103  /**
104   * @brief Physical device identifier (equals the @c dev entry if it specifies a
105   * physical device).
106   */
107  rtems_disk_device *phys_dev;
108
109  /**
110   * @brief Driver capabilities.
111   */
112  uint32_t capabilities;
113
114  /**
115   * @brief Disk device name.
116   */
117  char *name;
118
119  /**
120   * @brief Usage counter.
121   *
122   * Devices cannot be deleted if they are in use.
123   */
124  unsigned uses;
125
126  /**
127   * @brief Start media block number.
128   *
129   * Equals zero for physical devices.  It is a media block offset to the
130   * related physical device for logical device.
131   */
132  rtems_blkdev_bnum start;
133
134  /**
135   * @brief Size of the physical or logical disk in media blocks.
136   */
137  rtems_blkdev_bnum size;
138
139  /**
140   * @brief Media block size in bytes.
141   *
142   * This is the media transfer unit the hardware defaults to.
143   */
144  uint32_t media_block_size;
145
146  /**
147   * @brief Block size in bytes.
148   *
149   * This is the minimum transfer unit.  It may be a multiple of the media
150   * block size. It must be positive.
151   *
152   * @see rtems_bdbuf_set_block_size().
153   */
154  uint32_t block_size;
155
156  /**
157   * @brief Block count.
158   *
159   * @see rtems_bdbuf_set_block_size().
160   */
161  rtems_blkdev_bnum block_count;
162
163  /**
164   * @brief Media blocks per device blocks.
165   *
166   * @see rtems_bdbuf_set_block_size().
167   */
168  uint32_t media_blocks_per_block;
169
170  /**
171   * @brief Block to media block shift.
172   *
173   * In case this value is non-negative the media block of a block can be
174   * calculated as media block = block << block_to_media_block_shift, otherwise
175   * a 64-bit operation will be used.
176   *
177   * @see rtems_bdbuf_set_block_size().
178   */
179  int block_to_media_block_shift;
180
181  /**
182   * @brief Buffer descriptors per group count.
183   *
184   * @see rtems_bdbuf_set_block_size().
185   */
186  size_t bds_per_group;
187
188  /**
189   * @brief IO control handler for this disk.
190   */
191  rtems_block_device_ioctl ioctl;
192
193  /**
194   * @brief Private data for the disk driver.
195   */
196  void *driver_data;
197
198  /**
199   * @brief Indicates that this disk should be deleted as soon as the last user
200   * releases this disk.
201   */
202  bool deleted;
203
204  /**
205   * @brief Read-ahead control for this disk.
206   */
207  rtems_blkdev_read_ahead read_ahead;
208};
209
210/**
211 * @name Disk Device Data
212 *
213 * @{
214 */
215
216static inline dev_t rtems_disk_get_device_identifier(
217  const rtems_disk_device *dd
218)
219{
220  return dd->dev;
221}
222
223static inline rtems_device_major_number rtems_disk_get_major_number(
224  const rtems_disk_device *dd
225)
226{
227  return rtems_filesystem_dev_major_t(dd->dev);
228}
229
230static inline rtems_device_minor_number rtems_disk_get_minor_number(
231  const rtems_disk_device *dd
232)
233{
234  return rtems_filesystem_dev_minor_t(dd->dev);
235}
236
237static inline void *rtems_disk_get_driver_data(
238  const rtems_disk_device *dd
239)
240{
241  return dd->driver_data;
242}
243
244static inline uint32_t rtems_disk_get_media_block_size(
245  const rtems_disk_device *dd
246)
247{
248  return dd->media_block_size;
249}
250
251static inline uint32_t rtems_disk_get_block_size(
252  const rtems_disk_device *dd
253)
254{
255  return dd->block_size;
256}
257
258static inline rtems_blkdev_bnum rtems_disk_get_block_begin(
259  const rtems_disk_device *dd
260)
261{
262  return dd->start;
263}
264
265static inline rtems_blkdev_bnum rtems_disk_get_block_count(
266  const rtems_disk_device *dd
267)
268{
269  return dd->size;
270}
271
272/** @} */
273
274/**
275 * @name Disk Device Maintainance
276 *
277 * @{
278 */
279
280/**
281 * @brief Creates a physical disk with device identifier @a dev.
282 *
283 * The block size @a block_size must be positive.  The disk will have
284 * @a block_count blocks.  The block index starts with zero.  The associated disk
285 * device driver will be invoked via the IO control handler @a handler.  A
286 * device node will be registered in the file system with absolute path @a
287 * name, if @a name is not @c NULL.  This function is usually invoked from a
288 * block device driver during initialization when a physical device is detected
289 * in the system.  The device driver provides an IO control handler to allow
290 * block device operations.
291 *
292 * @retval RTEMS_SUCCESSFUL Successful operation.
293 * @retval RTEMS_NOT_CONFIGURED Cannot lock disk device operation mutex.
294 * @retval RTEMS_INVALID_ADDRESS IO control handler is @c NULL.
295 * @retval RTEMS_INVALID_NUMBER Block size is invalid.
296 * @retval RTEMS_NO_MEMORY Not enough memory.
297 * @retval RTEMS_RESOURCE_IN_USE Disk device descriptor is already in use.
298 * @retval RTEMS_UNSATISFIED Cannot create device node.
299 */
300rtems_status_code rtems_disk_create_phys(
301  dev_t dev,
302  uint32_t block_size,
303  rtems_blkdev_bnum block_count,
304  rtems_block_device_ioctl handler,
305  void *driver_data,
306  const char *name
307);
308
309/**
310 * @brief Creates a logical disk with device identifier @a dev.
311 *
312 * A logical disk manages a subset of consecutive blocks contained in the
313 * physical disk with identifier @a phys.  The start block index of the logical
314 * disk device is @a block_begin.  The block count of the logcal disk will be
315 * @a block_count.  The blocks must be within the range of blocks managed by
316 * the associated physical disk device.  A device node will be registered in
317 * the file system with absolute path @a name, if @a name is not @c NULL.  The
318 * block size and IO control handler are inherited by the physical disk.
319 *
320 * @retval RTEMS_SUCCESSFUL Successful operation.
321 * @retval RTEMS_NOT_CONFIGURED Cannot lock disk device operation mutex.
322 * @retval RTEMS_INVALID_ID Specified physical disk identifier does not
323 * correspond to a physical disk.
324 * @retval RTEMS_INVALID_NUMBER Begin block or block count are out of range.
325 * @retval RTEMS_NO_MEMORY Not enough memory.
326 * @retval RTEMS_RESOURCE_IN_USE Disk device descriptor for logical disk
327 * identifier is already in use.
328 * @retval RTEMS_UNSATISFIED Cannot create device node.
329 */
330rtems_status_code rtems_disk_create_log(
331  dev_t dev,
332  dev_t phys,
333  rtems_blkdev_bnum block_begin,
334  rtems_blkdev_bnum block_count,
335  const char *name
336);
337
338/**
339 * @brief Deletes a physical or logical disk device with identifier @a dev.
340 *
341 * Marks the disk device as deleted.  When a physical disk device is deleted,
342 * all corresponding logical disk devices will marked as deleted too.  Disks
343 * that are marked as deleted and have a usage counter of zero will be deleted.
344 * The corresponding device nodes will be removed from the file system.  In
345 * case of a physical disk deletion the IO control handler will be invoked with
346 * a RTEMS_BLKIO_DELETED request.  Disks that are still in use will be deleted
347 * upon release.
348 *
349 * @retval RTEMS_SUCCESSFUL Successful operation.
350 * @retval RTEMS_NOT_CONFIGURED Cannot lock disk device operation mutex.
351 * @retval RTEMS_INVALID_ID No disk for specified device identifier.
352 */
353rtems_status_code rtems_disk_delete(dev_t dev);
354
355/**
356 * @brief Returns the disk device descriptor for the device identifier @a dev.
357 *
358 * Increments usage counter by one.  You should release the disk device
359 * descriptor with rtems_disk_release().
360 *
361 * @return Pointer to the disk device descriptor or @c NULL if no corresponding
362 * disk exists.
363 */
364rtems_disk_device *rtems_disk_obtain(dev_t dev);
365
366/**
367 * @brief Releases the disk device descriptor @a dd.
368 *
369 * Decrements usage counter by one.
370 *
371 * @retval RTEMS_SUCCESSFUL Successful operation.
372 */
373rtems_status_code rtems_disk_release(rtems_disk_device *dd);
374
375/** @} */
376
377/**
378 * @name Disk Management
379 *
380 * @{
381 */
382
383/**
384 * @brief Initializes the disk device management.
385 *
386 * This functions returns successful if the disk device management is already
387 * initialized.  There is no protection against concurrent access.
388 *
389 * @retval RTEMS_SUCCESSFUL Successful initialization.
390 * @retval RTEMS_NO_MEMORY Not enough memory or no semaphore available.
391 * @retval RTEMS_UNSATISFIED Block device buffer initialization failed.
392 */
393rtems_status_code rtems_disk_io_initialize(void);
394
395/**
396 * @brief Releases all resources allocated for disk device management.
397 *
398 * There is no protection against concurrent access.  If parts of the system
399 * are still in use the behaviour is undefined.
400 *
401 * @retval RTEMS_SUCCESSFUL Successful operation.
402 */
403rtems_status_code rtems_disk_io_done(void);
404
405/** @} */
406
407/** @} */
408
409/**
410 * @brief Disk device iterator.
411 *
412 * Returns the next disk device descriptor with a device identifier larger than
413 * @a dev.  If there is no such device, @c NULL will be returned.  Use minus
414 * one to start the search.
415 *
416 * @code
417 * rtems_status_code sc = RTEMS_SUCCESSFUL;
418 * rtems_disk_device *dd = (dev_t) -1;
419 *
420 * while (sc == RTEMS_SUCCESSFUL && (dd = rtems_disk_next(dev)) != NULL) {
421 *   dev = rtems_disk_get_device_identifier(dd);
422 *   sc = rtems_disk_release(dd);
423 * }
424 * @endcode
425 */
426rtems_disk_device *rtems_disk_next(dev_t dev);
427
428/* Internal function, do not use */
429rtems_status_code rtems_disk_init_phys(
430  rtems_disk_device *dd,
431  uint32_t block_size,
432  rtems_blkdev_bnum block_count,
433  rtems_block_device_ioctl handler,
434  void *driver_data
435);
436
437/* Internal function, do not use */
438rtems_status_code rtems_disk_init_log(
439  rtems_disk_device *dd,
440  rtems_disk_device *phys_dd,
441  rtems_blkdev_bnum block_begin,
442  rtems_blkdev_bnum block_count
443);
444
445#ifdef __cplusplus
446}
447#endif
448
449#endif
Note: See TracBrowser for help on using the repository browser.