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