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 Position and Size Management. |
---|
16 | * |
---|
17 | * These functions manage the position in a block map as well as a size of data |
---|
18 | * held in a block map. The position is the block count plus the offset into |
---|
19 | * the last block where a block position of 0 and an offset of 0 is the start |
---|
20 | * of a map. The size has a block count plus an offset, but the offset into the |
---|
21 | * last block gives the actual size of the data in the map. This means a size |
---|
22 | * will always have a block count greater than 0 when the file is not empty. A |
---|
23 | * size offset of 0 and a non-zero block count means the length if aligned to |
---|
24 | * the end of the block. For this reason there are 2 similar types so we know |
---|
25 | * which set of rules are in use and the reason for this file. |
---|
26 | */ |
---|
27 | |
---|
28 | #if !defined (_RTEMS_RFS_BLOCK_POS_H_) |
---|
29 | #define _RTEMS_RFS_BLOCK_POS_H_ |
---|
30 | |
---|
31 | #include <rtems/rfs/rtems-rfs-file-system.h> |
---|
32 | #include <rtems/rfs/rtems-rfs-inode.h> |
---|
33 | |
---|
34 | /** |
---|
35 | * The block number is the same type as the inode block number. This makes sure |
---|
36 | * the sizes of the types match. |
---|
37 | */ |
---|
38 | typedef rtems_rfs_inode_block rtems_rfs_block_no; |
---|
39 | |
---|
40 | /** |
---|
41 | * The offset into a block. |
---|
42 | */ |
---|
43 | typedef uint32_t rtems_rfs_block_off; |
---|
44 | |
---|
45 | /** |
---|
46 | * A block position is a block number times the block size plus the offset. The |
---|
47 | * block field can be used hold a block number for the position as a look up |
---|
48 | * cache. |
---|
49 | */ |
---|
50 | typedef struct _rtems_rfs_block_pos |
---|
51 | { |
---|
52 | /** |
---|
53 | * The block index in the map. Range is from 0 to the maps block count minus |
---|
54 | * 1. |
---|
55 | */ |
---|
56 | rtems_rfs_block_no bno; |
---|
57 | |
---|
58 | /** |
---|
59 | * The offset into the block. Must be less than the block size. |
---|
60 | */ |
---|
61 | rtems_rfs_block_off boff; |
---|
62 | |
---|
63 | /** |
---|
64 | * The block number that the bpos + boff map to. The 0 value is invalid and |
---|
65 | * means no block number has been set. |
---|
66 | */ |
---|
67 | rtems_rfs_block_no block; |
---|
68 | |
---|
69 | } rtems_rfs_block_pos; |
---|
70 | |
---|
71 | /** |
---|
72 | * Copy a block position. |
---|
73 | * |
---|
74 | * @param _lhs The left hand side. |
---|
75 | * @param _rhs The right hand side. |
---|
76 | */ |
---|
77 | #define rtems_rfs_block_copy_bpos(_lhs, _rhs) \ |
---|
78 | do { (_lhs)->bno = (_rhs)->bno; \ |
---|
79 | (_lhs)->boff = (_rhs)->boff; \ |
---|
80 | (_lhs)->block = (_rhs)->block; } while (0) |
---|
81 | |
---|
82 | /** |
---|
83 | * Zero a block position. |
---|
84 | * |
---|
85 | * @param bpos A pointer to the block position. |
---|
86 | */ |
---|
87 | static inline void |
---|
88 | rtems_rfs_block_set_bpos_zero (rtems_rfs_block_pos* bpos) |
---|
89 | { |
---|
90 | bpos->bno = 0; |
---|
91 | bpos->boff = 0; |
---|
92 | bpos->block = 0; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Given a position compute the block number and block offset. |
---|
97 | * |
---|
98 | * @param fs The file system data. |
---|
99 | * @param pos The position as an absolute offset from the start. |
---|
100 | * @param bpos Pointer to the block position to fill in. |
---|
101 | */ |
---|
102 | void rtems_rfs_block_get_bpos (rtems_rfs_file_system* fs, |
---|
103 | rtems_rfs_pos pos, |
---|
104 | rtems_rfs_block_pos* bpos); |
---|
105 | |
---|
106 | /** |
---|
107 | * Given a block position compute the absolute offset. |
---|
108 | * |
---|
109 | * @param fs The file system data. |
---|
110 | * @param bpos Pointer to the block position to fill in. |
---|
111 | * @return rtems_rfs_pos The absolute offset. |
---|
112 | */ |
---|
113 | rtems_rfs_pos rtems_rfs_block_get_pos (rtems_rfs_file_system* fs, |
---|
114 | rtems_rfs_block_pos* bpos); |
---|
115 | |
---|
116 | /** |
---|
117 | * Add the relative position to the block position. The relative position is |
---|
118 | * signed. |
---|
119 | * |
---|
120 | * @param fs The file system data. |
---|
121 | * @param offset The relative offset add to the block position. |
---|
122 | * @param bpos Pointer to the block position to fill in. |
---|
123 | */ |
---|
124 | static inline void |
---|
125 | rtems_rfs_block_add_pos (rtems_rfs_file_system* fs, |
---|
126 | rtems_rfs_pos_rel offset, |
---|
127 | rtems_rfs_block_pos* bpos) |
---|
128 | { |
---|
129 | rtems_rfs_block_get_bpos (fs, |
---|
130 | rtems_rfs_block_get_pos (fs, bpos) + offset, |
---|
131 | bpos); |
---|
132 | bpos->block = 0; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * A block size is the number of blocks less one plus the offset where the |
---|
137 | * offset must be less than the block size. |
---|
138 | */ |
---|
139 | typedef struct _rtems_rfs_block_size |
---|
140 | { |
---|
141 | /** |
---|
142 | * The count of blocks in a map. A 0 means no blocks and a zero length and |
---|
143 | * the offset should also be 0. |
---|
144 | */ |
---|
145 | rtems_rfs_block_no count; |
---|
146 | |
---|
147 | /** |
---|
148 | * The offset into the block. An offset of 0 means block size, ie the first |
---|
149 | * byte of the next block which is not allocated. |
---|
150 | */ |
---|
151 | rtems_rfs_block_off offset; |
---|
152 | |
---|
153 | } rtems_rfs_block_size; |
---|
154 | |
---|
155 | /** |
---|
156 | * Copy a block size. |
---|
157 | * |
---|
158 | * @param _lhs The left hand side. |
---|
159 | * @param _rhs The right hand side. |
---|
160 | */ |
---|
161 | #define rtems_rfs_block_copy_size(_lhs, _rhs) \ |
---|
162 | do { (_lhs)->count = (_rhs)->count; \ |
---|
163 | (_lhs)->offset = (_rhs)->offset; } while (0) |
---|
164 | |
---|
165 | /** |
---|
166 | * Last block ? |
---|
167 | */ |
---|
168 | #define rtems_rfs_block_pos_last_block(_p, _s) \ |
---|
169 | ((((_p)->bno == 0) && ((_s)->count == 0)) || ((_p)->bno == ((_s)->count - 1))) |
---|
170 | |
---|
171 | /** |
---|
172 | * Last block ? |
---|
173 | */ |
---|
174 | #define rtems_rfs_block_pos_past_end(_p, _s) \ |
---|
175 | (((_p)->bno && ((_s)->count == 0)) || \ |
---|
176 | ((_p)->bno >= (_s)->count) || \ |
---|
177 | (((_p)->bno == ((_s)->count - 1)) && ((_p)->boff > (_s)->offset))) |
---|
178 | |
---|
179 | /** |
---|
180 | * Is the block position past the end. |
---|
181 | */ |
---|
182 | #define rtems_rfs_block_pos_block_past_end(_p, _s) \ |
---|
183 | (((_p)->bno && ((_s)->count == 0)) || ((_p)->bno >= (_s)->count)) |
---|
184 | |
---|
185 | /** |
---|
186 | * Copy the size to the block position. Note the block position and the size |
---|
187 | * have different block counts. |
---|
188 | */ |
---|
189 | #define rtems_rfs_block_size_get_bpos(_s, _b) \ |
---|
190 | do { (_b)->bno = (_s)->count; \ |
---|
191 | (_b)->boff = (_s)->offset; \ |
---|
192 | (_b)->block = 0; \ |
---|
193 | if ((_b)->boff) --(_b)->bno; } while (0) |
---|
194 | |
---|
195 | /** |
---|
196 | * Zero a block size. |
---|
197 | * |
---|
198 | * @param size A pointer to the block size. |
---|
199 | */ |
---|
200 | static inline void |
---|
201 | rtems_rfs_block_set_size_zero (rtems_rfs_block_size* size) |
---|
202 | { |
---|
203 | size->count = 0; |
---|
204 | size->offset = 0; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Set the size given a position. |
---|
209 | * |
---|
210 | * @param fs The file system data. |
---|
211 | * @param pos The position as an absolute offset from the start. |
---|
212 | * @param size Pointer to the block size to fill in. |
---|
213 | */ |
---|
214 | void rtems_rfs_block_get_block_size (rtems_rfs_file_system* fs, |
---|
215 | rtems_rfs_pos pos, |
---|
216 | rtems_rfs_block_size* size); |
---|
217 | |
---|
218 | /** |
---|
219 | * Calculate the position given the number of blocks and the offset. If the |
---|
220 | * block count is 0 the size is 0. If the block is greater than 0 and the |
---|
221 | * offset is 0 the size is number of blocks multipled by the block size and if |
---|
222 | * the offset is not 0 it is the offset into the last block. For example if |
---|
223 | * blocks is 1 and offset is 0 the size is the block size. If the block count |
---|
224 | * is 1 and size is 100 the size is 100. |
---|
225 | * |
---|
226 | * @param fs The file system data. |
---|
227 | * @param size The size in blocks and offset. |
---|
228 | * @return rtems_rfs_pos The size in bytes. |
---|
229 | */ |
---|
230 | rtems_rfs_pos rtems_rfs_block_get_size (rtems_rfs_file_system* fs, |
---|
231 | rtems_rfs_block_size* size); |
---|
232 | |
---|
233 | #endif |
---|