source: rtems/cpukit/libfs/src/rfs/rtems-rfs-block.h @ 66b8047

4.115
Last change on this file since 66b8047 was 66b8047, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/06/11 at 12:44:24

Remove stray whitespaces.

  • Property mode set to 100644
File size: 10.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.com/license/LICENSE.
7 *
8 *  $Id$
9 */
10/**
11 * @file
12 *
13 * @ingroup rtems-rfs
14 *
15 * RTEMS File Systems Block Management.
16 *
17 * These functions manage the blocks used in the file system.
18 */
19
20#if !defined (_RTEMS_RFS_BLOCK_H_)
21#define _RTEMS_RFS_BLOCK_H_
22
23#include <rtems/rfs/rtems-rfs-block-pos.h>
24#include <rtems/rfs/rtems-rfs-buffer.h>
25#include <rtems/rfs/rtems-rfs-data.h>
26#include <rtems/rfs/rtems-rfs-file-system.h>
27
28/**
29 * Get a block number in the media format and return it in the host format.
30 *
31 * @param _h The buffer handle of the block.
32 * @param _b The block number index.
33 * @return uint32_t The block number.
34 */
35#define rtems_rfs_block_get_number(_h, _b) \
36  ((rtems_rfs_block_no) \
37   (rtems_rfs_read_u32 (rtems_rfs_buffer_data (_h) + \
38                        ((_b) * sizeof (rtems_rfs_block_no)))))
39
40/**
41 * Set a block number in the media format given a number in the host format.
42 *
43 * @param _h The buffer handle of the block.
44 * @param _b The block number index, ie the number of block number not the
45 *           buffer offset.
46 * @param _n The block number.
47 */
48#define rtems_rfs_block_set_number(_h, _b, _n) \
49  do { \
50    rtems_rfs_write_u32 (rtems_rfs_buffer_data (_h) + \
51                         ((_b) * sizeof (rtems_rfs_block_no)), (_n)); \
52    rtems_rfs_buffer_mark_dirty (_h); \
53  } while (0)
54
55/**
56 * A block map manges the block lists that originate from an inode. The inode
57 * contains a number of block numbers. A block map takes those block numbers
58 * and manages them.
59 *
60 * The blocks cannot have all ones as a block number nor block 0. The block map
61 * is series of block numbers in a blocks. The size of the map determines the
62 * way the block numbers are stored. The map uses the following:
63 *
64 * @li @e Direct Access,
65 * @li @e Single Indirect Access, and
66 * @li @e Double Indirect Access.
67 *
68 * Direct access has the blocks numbers in the inode slots. The Single Indirect
69 * Access has block numbers in the inode slots that pointer to a table of block
70 * numbers that point to data blocks. The Double Indirect Access has block
71 * numbers in the inode that point to Single Indirect block tables.
72 *
73 * The inode can hold a number of Direct, Single Indirect, and Double Indirect
74 * block tables. The move from Direct to Single occurs then the block count in
75 * the map is above the number of slots in the inode. The move from Single to
76 * Double occurs when the map block count is greated than the block numbers per
77 * block multipled by the slots in the inode. The move from Single to Double
78 * occurs when the map block count is over the block numbers per block squared
79 * multipled by the number of slots in the inode.
80 *
81 * The block map can managed files of the follow size verses block size with 5
82 * inode slots:
83 *
84 *  @li 41,943,040 bytes for a 512 byte block size,
85 *  @li 335,544,320 bytes for a 1024 byte block size,
86 *  @li 2,684,354,560 bytes for a 2048 byte block size, and
87 *  @li 21,474,836,480 bytes for a 4096 byte block size.
88 */
89typedef struct rtems_rfs_block_map_s
90{
91  /**
92   * Is the map dirty ?
93   */
94  bool dirty;
95
96  /**
97   * The inode this map is attached to.
98   */
99  rtems_rfs_inode_handle* inode;
100
101  /**
102   * The size of the map.
103   */
104  rtems_rfs_block_size size;
105
106  /**
107   * The block map position. Used to navigate the map when seeking. The find
108   * call is to a position in the file/directory and is a block number plus
109   * offset. The block find only needs to locate a block and not worry about
110   * the offset while a seek can be less than a block size yet move across a
111   * block boundary. Therefore the position a block map has to maintain must
112   * include the offset so seeks work.
113   */
114  rtems_rfs_block_pos bpos;
115
116  /**
117   * The last map block allocated. This is used as the goal when allocating a
118   * new map block.
119   */
120  rtems_rfs_block_no last_map_block;
121
122  /**
123   * The last data block allocated. This is used as the goal when allocating a
124   * new data block.
125   */
126  rtems_rfs_block_no last_data_block;
127
128  /**
129   * The block map.
130   */
131  uint32_t blocks[RTEMS_RFS_INODE_BLOCKS];
132
133  /**
134   * Singly Buffer handle.
135   */
136  rtems_rfs_buffer_handle singly_buffer;
137
138  /**
139   * Doubly Buffer handle.
140   */
141  rtems_rfs_buffer_handle doubly_buffer;
142
143} rtems_rfs_block_map;
144
145/**
146 * Is the map dirty ?
147 */
148#define rtems_rfs_block_map_is_dirty(_m) ((_m)->dirty)
149
150/**
151 * Return the block count in the map.
152 */
153#define rtems_rfs_block_map_count(_m) ((_m)->size.count)
154
155/**
156 * Return the map's size element.
157 */
158#define rtems_rfs_block_map_size(_m) (&((_m)->size))
159
160/**
161 * Return the size offset for the map.
162 */
163#define rtems_rfs_block_map_size_offset(_m) ((_m)->size.offset)
164
165/**
166 * Are we at the last block in the map ?
167 */
168#define rtems_rfs_block_map_last(_m) \
169  rtems_rfs_block_pos_last_block (&(_m)->bpos, &(_m)->size)
170
171/**
172 * Is the position past the end of the block ?
173 */
174#define rtems_rfs_block_map_past_end(_m, _p) \
175  rtems_rfs_block_pos_past_end (_p, &(_m)->size)
176
177/**
178 * Return the current position in the map.
179 */
180#define rtems_rfs_block_map_pos(_f, _m) \
181  rtems_rfs_block_get_pos (_f, &(_m)->bpos)
182
183/**
184 * Return the map's current block number.
185 */
186#define rtems_rfs_block_map_block(_m) ((_m)->bpos.bno)
187
188/**
189 * Return the map's current block offset.
190 */
191#define rtems_rfs_block_map_block_offset(_m) ((_m)->bpos.boff)
192
193/**
194 * Set the size offset for the map. The map is tagged as dirty.
195 *
196 * @param map Pointer to the open map to set the offset in.
197 * @param offset The offset to set in the map's size.
198 */
199static inline void
200rtems_rfs_block_map_set_size_offset (rtems_rfs_block_map* map,
201                                     rtems_rfs_block_off  offset)
202{
203  map->size.offset = offset;
204  map->dirty = true;
205}
206
207/**
208 * Set the map's size. The map is tagged as dirty.
209 *
210 * @param map Pointer to the open map to set the offset in.
211 * @param size The size to set in the map's size.
212 */
213static inline void
214rtems_rfs_block_map_set_size (rtems_rfs_block_map*  map,
215                              rtems_rfs_block_size* size)
216{
217  rtems_rfs_block_copy_size (&map->size, size);
218  map->dirty = true;
219}
220/**
221 * Open a block map. The block map data in the inode is copied into the
222 * map. The buffer handles are opened. The block position is set to the start
223 * so a seek of offset 0 will return the first block.
224 *
225 * @param fs The file system data.
226 * @param inode The inode the map belongs to.
227 * @param map The map that is opened.
228 * @return int The error number (errno). No error if 0.
229 */
230int rtems_rfs_block_map_open (rtems_rfs_file_system*  fs,
231                              rtems_rfs_inode_handle* inode,
232                              rtems_rfs_block_map*    map);
233
234/**
235 * Close the map. The buffer handles are closed and any help buffers are
236 * released.
237 *
238 * @param fs The file system data.
239 * @param map The map that is opened.
240 * @return int The error number (errno). No error if 0.
241 */
242int rtems_rfs_block_map_close (rtems_rfs_file_system* fs,
243                               rtems_rfs_block_map*   map);
244
245/**
246 * Find a block number in the map from the position provided.
247 *
248 * @param fs The file system data.
249 * @param map The map to search.
250 * @param bpos The block position to find.
251 * @param block Pointer to place the block in when found.
252 * @return int The error number (errno). No error if 0.
253 */
254int rtems_rfs_block_map_find (rtems_rfs_file_system*  fs,
255                              rtems_rfs_block_map*    map,
256                              rtems_rfs_block_pos*    bpos,
257                              rtems_rfs_buffer_block* block);
258
259/**
260 * Seek around the map.
261 *
262 * @param fs The file system data.
263 * @param map The map to search.
264 * @param offset The distance to seek. It is signed.
265 * @param block Pointer to place the block in when found.
266 * @retval ENXIO Failed to seek because it is outside the block map.
267 * @return int The error number (errno). No error if 0.
268 */
269int rtems_rfs_block_map_seek (rtems_rfs_file_system*  fs,
270                              rtems_rfs_block_map*    map,
271                              rtems_rfs_pos_rel       offset,
272                              rtems_rfs_buffer_block* block);
273
274/**
275 * Seek to the next block.
276 *
277 * @param fs The file system data.
278 * @param map The map to search.
279 * @param block Pointer to place the block in when found.
280 * @retval ENXIO Failed to seek because it is outside the block map.
281 * @return int The error number (errno). No error if 0.
282 */
283int rtems_rfs_block_map_next_block (rtems_rfs_file_system*  fs,
284                                    rtems_rfs_block_map*    map,
285                                    rtems_rfs_buffer_block* block);
286
287/**
288 * Grow the block map by the specified number of blocks.
289 *
290 * @param fs The file system data.
291 * @param map Pointer to the open map to grow.
292 * @param blocks The number of blocks to grow the map by.
293 * @param new_block The first of the blocks allocated to the map.
294 * @return int The error number (errno). No error if 0.
295 */
296int rtems_rfs_block_map_grow (rtems_rfs_file_system* fs,
297                              rtems_rfs_block_map*   map,
298                              size_t                 blocks,
299                              rtems_rfs_block_no*    new_block);
300
301/**
302 * Grow the block map by the specified number of blocks.
303 *
304 * @param fs The file system data.
305 * @param map Pointer to the open map to shrink.
306 * @param blocks The number of blocks to shrink the map by. If more than the
307 *               number of blocks the map is emptied.
308 * @return int The error number (errno). No error if 0.
309 */
310int rtems_rfs_block_map_shrink (rtems_rfs_file_system* fs,
311                                rtems_rfs_block_map*   map,
312                                size_t                 blocks);
313
314/**
315 * Free all blocks in the map.
316 *
317 * @param fs The file system data.
318 * @param map Pointer to the open map to free all blocks from.
319 * @return int The error number (errno). No error if 0.
320 */
321int rtems_rfs_block_map_free_all (rtems_rfs_file_system* fs,
322                                  rtems_rfs_block_map*   map);
323
324#endif
Note: See TracBrowser for help on using the repository browser.