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 | */ |
---|
89 | typedef struct rtems_rfs_block_map_t |
---|
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 | * Set the size offset for the map. |
---|
167 | */ |
---|
168 | #define rtems_rfs_block_map_set_size_offset(_m, _o) ((_m)->size.offset = (_o)) |
---|
169 | |
---|
170 | /** |
---|
171 | * Are we at the last block in the map ? |
---|
172 | */ |
---|
173 | #define rtems_rfs_block_map_last(_m) \ |
---|
174 | rtems_rfs_block_pos_last_block (&(_m)->bpos, &(_m)->size) |
---|
175 | |
---|
176 | /** |
---|
177 | * Is the position past the end of the block ? |
---|
178 | */ |
---|
179 | #define rtems_rfs_block_map_past_end(_m, _p) \ |
---|
180 | rtems_rfs_block_pos_past_end (_p, &(_m)->size) |
---|
181 | |
---|
182 | /** |
---|
183 | * Return the current position in the map. |
---|
184 | */ |
---|
185 | #define rtems_rfs_block_map_pos(_f, _m) \ |
---|
186 | rtems_rfs_block_get_pos (_f, &(_m)->bpos) |
---|
187 | |
---|
188 | /** |
---|
189 | * Return the map's current block number. |
---|
190 | */ |
---|
191 | #define rtems_rfs_block_map_block(_m) ((_m)->bpos.bno) |
---|
192 | |
---|
193 | /** |
---|
194 | * Return the map's current block offset. |
---|
195 | */ |
---|
196 | #define rtems_rfs_block_map_block_offset(_m) ((_m)->bpos.boff) |
---|
197 | |
---|
198 | /** |
---|
199 | * Open a block map. The block map data in the inode is copied into the |
---|
200 | * map. The buffer handles are opened. The block position is set to the start |
---|
201 | * so a seek of offset 0 will return the first block. |
---|
202 | * |
---|
203 | * @param fs The file system data. |
---|
204 | * @param inode The inode the map belongs to. |
---|
205 | * @param map The map that is opened. |
---|
206 | * @return int The error number (errno). No error if 0. |
---|
207 | */ |
---|
208 | int rtems_rfs_block_map_open (rtems_rfs_file_system* fs, |
---|
209 | rtems_rfs_inode_handle* inode, |
---|
210 | rtems_rfs_block_map* map); |
---|
211 | |
---|
212 | /** |
---|
213 | * Close the map. The buffer handles are closed and any help buffers are |
---|
214 | * released. |
---|
215 | * |
---|
216 | * @param fs The file system data. |
---|
217 | * @param map The map that is opened. |
---|
218 | * @return int The error number (errno). No error if 0. |
---|
219 | */ |
---|
220 | int rtems_rfs_block_map_close (rtems_rfs_file_system* fs, |
---|
221 | rtems_rfs_block_map* map); |
---|
222 | |
---|
223 | /** |
---|
224 | * Find a block number in the map from the position provided. |
---|
225 | * |
---|
226 | * @param fs The file system data. |
---|
227 | * @param map The map to search. |
---|
228 | * @param bpos The block position to find. |
---|
229 | * @param block Pointer to place the block in when found. |
---|
230 | * @return int The error number (errno). No error if 0. |
---|
231 | */ |
---|
232 | int rtems_rfs_block_map_find (rtems_rfs_file_system* fs, |
---|
233 | rtems_rfs_block_map* map, |
---|
234 | rtems_rfs_block_pos* bpos, |
---|
235 | rtems_rfs_buffer_block* block); |
---|
236 | |
---|
237 | /** |
---|
238 | * Seek around the map. |
---|
239 | * |
---|
240 | * @param fs The file system data. |
---|
241 | * @param map The map to search. |
---|
242 | * @param offset The distance to seek. It is signed. |
---|
243 | * @param block Pointer to place the block in when found. |
---|
244 | * @retval ENXIO Failed to seek because it is outside the block map. |
---|
245 | * @return int The error number (errno). No error if 0. |
---|
246 | */ |
---|
247 | int rtems_rfs_block_map_seek (rtems_rfs_file_system* fs, |
---|
248 | rtems_rfs_block_map* map, |
---|
249 | rtems_rfs_pos_rel offset, |
---|
250 | rtems_rfs_buffer_block* block); |
---|
251 | |
---|
252 | /** |
---|
253 | * Seek to the next block. |
---|
254 | * |
---|
255 | * @param fs The file system data. |
---|
256 | * @param map The map to search. |
---|
257 | * @param block Pointer to place the block in when found. |
---|
258 | * @retval ENXIO Failed to seek because it is outside the block map. |
---|
259 | * @return int The error number (errno). No error if 0. |
---|
260 | */ |
---|
261 | int rtems_rfs_block_map_next_block (rtems_rfs_file_system* fs, |
---|
262 | rtems_rfs_block_map* map, |
---|
263 | rtems_rfs_buffer_block* block); |
---|
264 | |
---|
265 | /** |
---|
266 | * Grow the block map by the specified number of blocks. |
---|
267 | * |
---|
268 | * @param fs The file system data. |
---|
269 | * @param map Pointer to the open map to grow. |
---|
270 | * @param blocks The number of blocks to grow the map by. |
---|
271 | * @param new_block The first of the blocks allocated to the map. |
---|
272 | * @return int The error number (errno). No error if 0. |
---|
273 | */ |
---|
274 | int rtems_rfs_block_map_grow (rtems_rfs_file_system* fs, |
---|
275 | rtems_rfs_block_map* map, |
---|
276 | size_t blocks, |
---|
277 | rtems_rfs_block_no* new_block); |
---|
278 | |
---|
279 | /** |
---|
280 | * Grow the block map by the specified number of blocks. |
---|
281 | * |
---|
282 | * @param fs The file system data. |
---|
283 | * @param map Pointer to the open map to shrink. |
---|
284 | * @param blocks The number of blocks to shrink the map by. If more than the |
---|
285 | * number of blocks the map is emptied. |
---|
286 | * @return int The error number (errno). No error if 0. |
---|
287 | */ |
---|
288 | int rtems_rfs_block_map_shrink (rtems_rfs_file_system* fs, |
---|
289 | rtems_rfs_block_map* map, |
---|
290 | size_t blocks); |
---|
291 | |
---|
292 | /** |
---|
293 | * Free all blocks in the map. |
---|
294 | * |
---|
295 | * @param fs The file system data. |
---|
296 | * @param map Pointer to the open map to free all blocks from. |
---|
297 | * @return int The error number (errno). No error if 0. |
---|
298 | */ |
---|
299 | int rtems_rfs_block_map_free_all (rtems_rfs_file_system* fs, |
---|
300 | rtems_rfs_block_map* map); |
---|
301 | |
---|
302 | #endif |
---|