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 System File Support |
---|
16 | * |
---|
17 | * This file provides the support functions. |
---|
18 | */ |
---|
19 | |
---|
20 | #if !defined (_RTEMS_RFS_FILE_H_) |
---|
21 | #define _RTEMS_RFS_FILE_H_ |
---|
22 | |
---|
23 | #include <rtems/libio_.h> |
---|
24 | |
---|
25 | #include <rtems/rfs/rtems-rfs-block.h> |
---|
26 | #include <rtems/rfs/rtems-rfs-data.h> |
---|
27 | #include <rtems/rfs/rtems-rfs-file-system.h> |
---|
28 | #include <rtems/rfs/rtems-rfs-inode.h> |
---|
29 | |
---|
30 | /** |
---|
31 | * File data that is shared by various file handles accessing the same file. We |
---|
32 | * hold various inode values common to the file that can change frequently so |
---|
33 | * the inode is not thrashed yet we meet the requirements of the POSIX |
---|
34 | * standard. The stat call needs to check the shared file data. |
---|
35 | */ |
---|
36 | typedef struct _rtems_rfs_file_shared |
---|
37 | { |
---|
38 | /** |
---|
39 | * The shared parts are maintained as a list. |
---|
40 | */ |
---|
41 | rtems_chain_node link; |
---|
42 | |
---|
43 | /** |
---|
44 | * Reference count the users of this data. |
---|
45 | */ |
---|
46 | int references; |
---|
47 | |
---|
48 | /** |
---|
49 | * The inode for the file. |
---|
50 | */ |
---|
51 | rtems_rfs_inode_handle inode; |
---|
52 | |
---|
53 | /** |
---|
54 | * The block map for the file. The handle holds the file's position not the |
---|
55 | * map. |
---|
56 | */ |
---|
57 | rtems_rfs_block_map map; |
---|
58 | |
---|
59 | /** |
---|
60 | * The size of the file as taken from the inode. The map's size and |
---|
61 | * this size should be the same. |
---|
62 | */ |
---|
63 | rtems_rfs_block_size size; |
---|
64 | |
---|
65 | /** |
---|
66 | * The access time. The last time the file was read. |
---|
67 | */ |
---|
68 | rtems_rfs_time atime; |
---|
69 | |
---|
70 | /** |
---|
71 | * The modified time. The last time the file was written too. |
---|
72 | */ |
---|
73 | rtems_rfs_time mtime; |
---|
74 | |
---|
75 | /** |
---|
76 | * The change time. The last time the inode was written too. |
---|
77 | */ |
---|
78 | rtems_rfs_time ctime; |
---|
79 | |
---|
80 | /** |
---|
81 | * Hold a pointer to the file system data so users can take the handle and |
---|
82 | * use it without the needing to hold the file system data pointer. |
---|
83 | */ |
---|
84 | rtems_rfs_file_system* fs; |
---|
85 | |
---|
86 | } rtems_rfs_file_shared; |
---|
87 | |
---|
88 | /** |
---|
89 | * Get the atime. |
---|
90 | * |
---|
91 | * @param shared The shared file data. |
---|
92 | * @return rtems_rfs_time The atime. |
---|
93 | */ |
---|
94 | static inline rtems_rfs_time |
---|
95 | rtems_rfs_file_shared_get_atime (rtems_rfs_file_shared* shared) |
---|
96 | { |
---|
97 | return shared->atime; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Get the mtime. |
---|
102 | * |
---|
103 | * @param shared The shared file data. |
---|
104 | * @return rtems_rfs_time The mtime. |
---|
105 | */ |
---|
106 | static inline rtems_rfs_time |
---|
107 | rtems_rfs_file_shared_get_mtime (rtems_rfs_file_shared* shared) |
---|
108 | { |
---|
109 | return shared->mtime; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Get the ctime. |
---|
114 | * |
---|
115 | * @param shared The shared file data. |
---|
116 | * @return rtems_rfs_time The ctime. |
---|
117 | */ |
---|
118 | static inline rtems_rfs_time |
---|
119 | rtems_rfs_file_shared_get_ctime (rtems_rfs_file_shared* shared) |
---|
120 | { |
---|
121 | return shared->ctime; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Get the block count. |
---|
126 | * |
---|
127 | * @param shared The shared file data. |
---|
128 | * @return uint32_t The block count. |
---|
129 | */ |
---|
130 | static inline uint32_t |
---|
131 | rtems_rfs_file_shared_get_block_count (rtems_rfs_file_shared* shared) |
---|
132 | { |
---|
133 | return shared->size.count; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Get the block offset. |
---|
138 | * |
---|
139 | * @param shared The shared file data. |
---|
140 | * @return uint16_t The block offset. |
---|
141 | */ |
---|
142 | static inline uint16_t |
---|
143 | rtems_rfs_file_shared_get_block_offset (rtems_rfs_file_shared* shared) |
---|
144 | { |
---|
145 | return shared->size.offset; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Calculate the size of data. |
---|
150 | * |
---|
151 | * @param fs The file system data. |
---|
152 | * @oaram shared The shared file data. |
---|
153 | * @return rtems_rfs_pos The data size in bytes. |
---|
154 | */ |
---|
155 | static inline rtems_rfs_pos |
---|
156 | rtems_rfs_file_shared_get_size (rtems_rfs_file_system* fs, |
---|
157 | rtems_rfs_file_shared* shared) |
---|
158 | { |
---|
159 | return rtems_rfs_block_get_size (fs, &shared->size); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * File flags. |
---|
164 | */ |
---|
165 | #define RTEMS_RFS_FILE_NO_ATIME_UPDATE (1 << 0) /**< Do not update the atime |
---|
166 | * field in the inode if |
---|
167 | * set. */ |
---|
168 | #define RTEMS_RFS_FILE_NO_MTIME_UPDATE (1 << 1) /**< Do not update the mtime |
---|
169 | * field in the inode if |
---|
170 | * set. */ |
---|
171 | #define RTEMS_RFS_FILE_NO_LENGTH_UPDATE (1 << 2) /**< Do not update the position |
---|
172 | * field in the inode if |
---|
173 | * set. */ |
---|
174 | |
---|
175 | /** |
---|
176 | * File data used to managed an open file. |
---|
177 | */ |
---|
178 | typedef struct _rtems_rfs_file_handle |
---|
179 | { |
---|
180 | /** |
---|
181 | * Special flags that can be controlled by the fctrl call. |
---|
182 | */ |
---|
183 | uint32_t flags; |
---|
184 | |
---|
185 | /** |
---|
186 | * The buffer of data at the file's position. |
---|
187 | */ |
---|
188 | rtems_rfs_buffer_handle buffer; |
---|
189 | |
---|
190 | /** |
---|
191 | * The block position of this file handle. |
---|
192 | */ |
---|
193 | rtems_rfs_block_pos bpos; |
---|
194 | |
---|
195 | /** |
---|
196 | * Pointer to the shared file data. |
---|
197 | */ |
---|
198 | rtems_rfs_file_shared* shared; |
---|
199 | |
---|
200 | } rtems_rfs_file_handle; |
---|
201 | |
---|
202 | /** |
---|
203 | * Access the data in the buffer. |
---|
204 | */ |
---|
205 | #define rtems_rfs_file_data(_f) \ |
---|
206 | (rtems_rfs_buffer_data (&(_f)->buffer) + (_f)->bpos.boff) |
---|
207 | |
---|
208 | /** |
---|
209 | * Return the file system data pointer given a file handle. |
---|
210 | */ |
---|
211 | #define rtems_rfs_file_fs(_f) ((_f)->shared->fs) |
---|
212 | |
---|
213 | /** |
---|
214 | * Return the file's inode handle pointer given a file handle. |
---|
215 | */ |
---|
216 | #define rtems_rfs_file_inode(_f) (&(_f)->shared->inode) |
---|
217 | |
---|
218 | /** |
---|
219 | * Return the file's block map pointer given a file handle. |
---|
220 | */ |
---|
221 | #define rtems_rfs_file_map(_f) (&(_f)->shared->map) |
---|
222 | |
---|
223 | /** |
---|
224 | * Return the file's block position pointer given a file handle. |
---|
225 | */ |
---|
226 | #define rtems_rfs_file_bpos(_f) (&(_f)->bpos) |
---|
227 | |
---|
228 | /** |
---|
229 | * Return the file's block number given a file handle. |
---|
230 | */ |
---|
231 | #define rtems_rfs_file_block(_f) ((_f)->bpos.bno) |
---|
232 | |
---|
233 | /** |
---|
234 | * Return the file's block offset given a file handle. |
---|
235 | */ |
---|
236 | #define rtems_rfs_file_block_offset(_f) ((_f)->bpos.boff) |
---|
237 | |
---|
238 | /** |
---|
239 | * Set the file's block position given a file position (absolute). |
---|
240 | */ |
---|
241 | #define rtems_rfs_file_set_bpos(_f, _p) \ |
---|
242 | rtems_rfs_block_get_bpos (rtems_rfs_file_fs (_f), _p, (&(_f)->bpos)) |
---|
243 | |
---|
244 | /** |
---|
245 | * Return the file's buffer handle pointer given a file handle. |
---|
246 | */ |
---|
247 | #define rtems_rfs_file_buffer(_f) (&(_f)->buffer) |
---|
248 | |
---|
249 | /** |
---|
250 | * Update the access time field of the inode when reading if flagged to do so. |
---|
251 | */ |
---|
252 | #define rtems_rfs_file_update_atime(_f) \ |
---|
253 | (((_f)->flags & RTEMS_RFS_FILE_NO_ATIME_UPDATE) == 0) |
---|
254 | |
---|
255 | /** |
---|
256 | * Update the modified time field of the inode when writing if flagged to do so. |
---|
257 | */ |
---|
258 | #define rtems_rfs_file_update_mtime(_f) \ |
---|
259 | (((_f)->flags & RTEMS_RFS_FILE_NO_MTIME_UPDATE) == 0) |
---|
260 | |
---|
261 | /** |
---|
262 | * Update the length field of the inode. |
---|
263 | */ |
---|
264 | #define rtems_rfs_file_update_length(_f) \ |
---|
265 | (((_f)->flags & RTEMS_RFS_FILE_NO_LENGTH_UPDATE) == 0) |
---|
266 | |
---|
267 | /** |
---|
268 | * Return the shared size varable. |
---|
269 | */ |
---|
270 | #define rtems_rfs_file_get_size(_f) \ |
---|
271 | (&(_f)->shared->size) |
---|
272 | |
---|
273 | /** |
---|
274 | * Return the size of file. |
---|
275 | */ |
---|
276 | #define rtems_rfs_file_size(_f) \ |
---|
277 | rtems_rfs_file_shared_get_size (rtems_rfs_file_fs (_f), (_f)->shared) |
---|
278 | |
---|
279 | /** |
---|
280 | * Return the file block count. |
---|
281 | */ |
---|
282 | #define rtems_rfs_file_size_count(_f) \ |
---|
283 | rtems_rfs_file_shared_get_block_count ((_f)->shared) |
---|
284 | |
---|
285 | /** |
---|
286 | * Return the file block offset. |
---|
287 | */ |
---|
288 | #define rtems_rfs_file_size_offset(_f) \ |
---|
289 | rtems_rfs_file_shared_get_block_offset ((_f)->shared) |
---|
290 | |
---|
291 | /** |
---|
292 | * Open a file handle. |
---|
293 | * |
---|
294 | * @param fs The file system. |
---|
295 | * @param ino The inode number of the file to be opened. |
---|
296 | * @param handle Return the handle pointer in this handle. |
---|
297 | * @return int The error number (errno). No error if 0. |
---|
298 | */ |
---|
299 | int rtems_rfs_file_open (rtems_rfs_file_system* fs, |
---|
300 | rtems_rfs_ino ino, |
---|
301 | uint32_t flags, |
---|
302 | rtems_rfs_file_handle** handle); |
---|
303 | |
---|
304 | /** |
---|
305 | * Close an open file handle. |
---|
306 | * |
---|
307 | * @param fs The file system. |
---|
308 | * @param handle The open file handle. |
---|
309 | * @return int The error number (errno). No error if 0. |
---|
310 | */ |
---|
311 | int rtems_rfs_file_close (rtems_rfs_file_system* fs, |
---|
312 | rtems_rfs_file_handle* handle); |
---|
313 | |
---|
314 | /** |
---|
315 | * Start I/O on a block of a file. This call only requests the block from the |
---|
316 | * media if reading and makes the buffer available to you the via the |
---|
317 | * rtems_rfs_file_data interface after the call. The available amount data is |
---|
318 | * taken from the current file position until the end of the block. The file |
---|
319 | * position is not adujsted until the I/O ends. An I/O request cannot perform |
---|
320 | * I/O past the end of a block so the call returns the amount of data |
---|
321 | * available. |
---|
322 | * |
---|
323 | * @param handle The file handle. |
---|
324 | * @param available The amount of data available for I/O. |
---|
325 | * @param read The I/O operation is a read so the block is read from the media. |
---|
326 | * @return int The error number (errno). No error if 0. |
---|
327 | */ |
---|
328 | int rtems_rfs_file_io_start (rtems_rfs_file_handle* handle, |
---|
329 | size_t* available, |
---|
330 | bool read); |
---|
331 | |
---|
332 | /** |
---|
333 | * End the I/O. Any buffers held in the file handle and returned to the |
---|
334 | * cache. If inode updating is not disable and the I/O is a read the atime |
---|
335 | * field is updated and if a write I/O the mtime is updated. |
---|
336 | * |
---|
337 | * If the file's position is updated by the size amount. |
---|
338 | * |
---|
339 | * @param handle The file handle. |
---|
340 | * @param size The amount of data read or written. |
---|
341 | * @param read The I/O was a read if true else it was a write. |
---|
342 | * @return int The error number (errno). No error if 0. |
---|
343 | */ |
---|
344 | int rtems_rfs_file_io_end (rtems_rfs_file_handle* handle, |
---|
345 | size_t size, |
---|
346 | bool read); |
---|
347 | |
---|
348 | /** |
---|
349 | * Release the I/O resources without any changes. If data has changed in the |
---|
350 | * buffer and the buffer was not already released as modified the data will be |
---|
351 | * lost. |
---|
352 | * |
---|
353 | * @param handle The file handle. |
---|
354 | * @return int The error number (errno). No error if 0. |
---|
355 | */ |
---|
356 | int rtems_rfs_file_io_release (rtems_rfs_file_handle* handle); |
---|
357 | |
---|
358 | /** |
---|
359 | * The file to the position returning the old position. The position is |
---|
360 | * abolute. |
---|
361 | * |
---|
362 | * @param handle The file handle. |
---|
363 | * @param pos The position to seek to. |
---|
364 | * @param new_pos The actual position. |
---|
365 | * @return int The error number (errno). No error if 0. |
---|
366 | */ |
---|
367 | int rtems_rfs_file_seek (rtems_rfs_file_handle* handle, |
---|
368 | rtems_rfs_pos pos, |
---|
369 | rtems_rfs_pos* new_pos); |
---|
370 | |
---|
371 | /** |
---|
372 | * Set the size of the file to the new size. This can extend the file to a new |
---|
373 | * size. |
---|
374 | * |
---|
375 | * @param handle The file handle. |
---|
376 | * @param size The new size of the file. |
---|
377 | * @return int The error number (errno). No error if 0. |
---|
378 | */ |
---|
379 | int rtems_rfs_file_set_size (rtems_rfs_file_handle* handle, |
---|
380 | rtems_rfs_pos size); |
---|
381 | |
---|
382 | /** |
---|
383 | * Return the shared file data for an ino. |
---|
384 | * |
---|
385 | * @param fs The file system data. |
---|
386 | * @param ino The inode number to locate the data for. |
---|
387 | * @return rtems_rfs_file_shared* The shared data or NULL is not located. |
---|
388 | */ |
---|
389 | rtems_rfs_file_shared* rtems_rfs_file_get_shared (rtems_rfs_file_system* fs, |
---|
390 | rtems_rfs_ino ino); |
---|
391 | |
---|
392 | |
---|
393 | #endif |
---|