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