source: rtems/cpukit/libblock/include/rtems/diskdevs.h @ 73c09b3b

4.115
Last change on this file since 73c09b3b was 73c09b3b, checked in by Sebastian Huber <sebastian.huber@…>, on 05/30/12 at 11:40:34

libblock: Simplify disk management

Add block_count and media_blocks_per_block to rtems_disk_device. Add
and use rtems_disk_init_phys() and rtems_disk_init_log().

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