1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Header File for the In-Memory File System |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2011. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _RTEMS_IMFS_H |
---|
17 | #define _RTEMS_IMFS_H |
---|
18 | |
---|
19 | #include <limits.h> |
---|
20 | |
---|
21 | #include <rtems/libio_.h> |
---|
22 | #include <rtems/pipe.h> |
---|
23 | |
---|
24 | /** |
---|
25 | * @brief In-Memory File System Support. |
---|
26 | * |
---|
27 | * @defgroup IMFS In-Memory File System Support |
---|
28 | * |
---|
29 | * @ingroup FileSystemTypesAndMount |
---|
30 | */ |
---|
31 | /**@{*/ |
---|
32 | |
---|
33 | #ifdef __cplusplus |
---|
34 | extern "C" { |
---|
35 | #endif |
---|
36 | |
---|
37 | /* |
---|
38 | * Data types |
---|
39 | */ |
---|
40 | |
---|
41 | struct IMFS_jnode_tt; |
---|
42 | typedef struct IMFS_jnode_tt IMFS_jnode_t; |
---|
43 | |
---|
44 | /** |
---|
45 | * IMFS "memfile" information |
---|
46 | * |
---|
47 | * The data structure for the in-memory "memfiles" is based on classic UNIX. |
---|
48 | * |
---|
49 | * block_ptr is a pointer to a block of IMFS_MEMFILE_BYTES_PER_BLOCK in |
---|
50 | * length which could be data or a table of pointers to blocks. |
---|
51 | * |
---|
52 | * Setting IMFS_MEMFILE_BYTES_PER_BLOCK to different values has a significant |
---|
53 | * impact on the maximum file size supported as well as the amount of |
---|
54 | * memory wasted due to internal file fragmentation. The following |
---|
55 | * is a list of maximum file sizes based on various settings |
---|
56 | * |
---|
57 | * @code |
---|
58 | * max_filesize with blocks of 16 is 1,328 |
---|
59 | * max_filesize with blocks of 32 is 18,656 |
---|
60 | * max_filesize with blocks of 64 is 279,488 |
---|
61 | * max_filesize with blocks of 128 is 4,329,344 |
---|
62 | * max_filesize with blocks of 256 is 68,173,568 |
---|
63 | * max_filesize with blocks of 512 is 1,082,195,456 |
---|
64 | * @endcode |
---|
65 | */ |
---|
66 | #define IMFS_MEMFILE_DEFAULT_BYTES_PER_BLOCK 128 |
---|
67 | extern int imfs_rq_memfile_bytes_per_block; |
---|
68 | extern int imfs_memfile_bytes_per_block; |
---|
69 | |
---|
70 | #define IMFS_MEMFILE_BYTES_PER_BLOCK imfs_memfile_bytes_per_block |
---|
71 | #define IMFS_MEMFILE_BLOCK_SLOTS \ |
---|
72 | (IMFS_MEMFILE_BYTES_PER_BLOCK / sizeof(void *)) |
---|
73 | |
---|
74 | typedef uint8_t *block_p; |
---|
75 | typedef block_p *block_ptr; |
---|
76 | |
---|
77 | /* |
---|
78 | * Important block numbers for "memfiles" |
---|
79 | */ |
---|
80 | #define FIRST_INDIRECT (0) |
---|
81 | #define LAST_INDIRECT (IMFS_MEMFILE_BLOCK_SLOTS - 1) |
---|
82 | |
---|
83 | #define FIRST_DOUBLY_INDIRECT (LAST_INDIRECT + 1) |
---|
84 | #define LAST_DOUBLY_INDIRECT \ |
---|
85 | (LAST_INDIRECT + \ |
---|
86 | (IMFS_MEMFILE_BLOCK_SLOTS * IMFS_MEMFILE_BLOCK_SLOTS)) |
---|
87 | |
---|
88 | #define FIRST_TRIPLY_INDIRECT (LAST_DOUBLY_INDIRECT + 1) |
---|
89 | #define LAST_TRIPLY_INDIRECT \ |
---|
90 | (LAST_DOUBLY_INDIRECT +\ |
---|
91 | (IMFS_MEMFILE_BLOCK_SLOTS * \ |
---|
92 | IMFS_MEMFILE_BLOCK_SLOTS * IMFS_MEMFILE_BLOCK_SLOTS)) |
---|
93 | |
---|
94 | #define IMFS_MEMFILE_MAXIMUM_SIZE \ |
---|
95 | (LAST_TRIPLY_INDIRECT * IMFS_MEMFILE_BYTES_PER_BLOCK) |
---|
96 | |
---|
97 | /** @} */ |
---|
98 | |
---|
99 | /** |
---|
100 | * @addtogroup IMFSGenericNodes |
---|
101 | */ |
---|
102 | /**@{*/ |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Initializes an IMFS node. |
---|
106 | * |
---|
107 | * @param[in] node The IMFS node. |
---|
108 | * @param[in] arg The user provided argument pointer. It may contain node |
---|
109 | * specific initialization information. |
---|
110 | * |
---|
111 | * @retval node Successful operation. |
---|
112 | * @retval NULL An error occurred. The @c errno indicates the error. This |
---|
113 | * will abort the make operation. |
---|
114 | * |
---|
115 | * @see IMFS_node_control, IMFS_node_initialize_default(), and |
---|
116 | * IMFS_node_initialize_generic(). |
---|
117 | */ |
---|
118 | typedef IMFS_jnode_t *(*IMFS_node_control_initialize)( |
---|
119 | IMFS_jnode_t *node, |
---|
120 | void *arg |
---|
121 | ); |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief Returns the node and does nothing else. |
---|
125 | * |
---|
126 | * @param[in] node The IMFS node. |
---|
127 | * @param[in] arg The user provided argument pointer. It is not used. |
---|
128 | * |
---|
129 | * @retval node Returns always the node passed as parameter. |
---|
130 | * |
---|
131 | * @see IMFS_node_control. |
---|
132 | */ |
---|
133 | IMFS_jnode_t *IMFS_node_initialize_default( |
---|
134 | IMFS_jnode_t *node, |
---|
135 | void *arg |
---|
136 | ); |
---|
137 | |
---|
138 | /** |
---|
139 | * @brief Returns the node and sets the generic node context. |
---|
140 | * |
---|
141 | * @param[in] node The IMFS node. |
---|
142 | * @param[in] arg The user provided argument pointer. It must contain the |
---|
143 | * generic context. |
---|
144 | * |
---|
145 | * @retval node Returns always the node passed as parameter. |
---|
146 | * |
---|
147 | * @see IMFS_node_control. |
---|
148 | */ |
---|
149 | IMFS_jnode_t *IMFS_node_initialize_generic( |
---|
150 | IMFS_jnode_t *node, |
---|
151 | void *arg |
---|
152 | ); |
---|
153 | |
---|
154 | /** |
---|
155 | * @brief Prepares the removal of an IMFS node from its parent directory. |
---|
156 | * |
---|
157 | * @param[in] node The IMFS node. |
---|
158 | * |
---|
159 | * @retval node Successful operation. |
---|
160 | * @retval NULL An error occurred. The @c errno indicates the error. This |
---|
161 | * will abort the removal operation. |
---|
162 | * |
---|
163 | * @see IMFS_node_control and IMFS_node_remove_default(). |
---|
164 | */ |
---|
165 | typedef IMFS_jnode_t *(*IMFS_node_control_remove)( |
---|
166 | IMFS_jnode_t *node |
---|
167 | ); |
---|
168 | |
---|
169 | /** |
---|
170 | * @brief Returns the node and does nothing else. |
---|
171 | * |
---|
172 | * @param[in] node The IMFS node. |
---|
173 | * |
---|
174 | * @retval node Returns always the node passed as parameter. |
---|
175 | * |
---|
176 | * @see IMFS_node_control. |
---|
177 | */ |
---|
178 | IMFS_jnode_t *IMFS_node_remove_default( |
---|
179 | IMFS_jnode_t *node |
---|
180 | ); |
---|
181 | |
---|
182 | /** |
---|
183 | * @brief Destroys an IMFS node. |
---|
184 | * |
---|
185 | * @param[in] node The IMFS node. |
---|
186 | * |
---|
187 | * @see IMFS_node_control and IMFS_node_destroy_default(). |
---|
188 | */ |
---|
189 | typedef void (*IMFS_node_control_destroy)( IMFS_jnode_t *node ); |
---|
190 | |
---|
191 | /** |
---|
192 | * @brief Frees the node. |
---|
193 | * |
---|
194 | * @param[in] node The IMFS node. |
---|
195 | * |
---|
196 | * @see IMFS_node_control. |
---|
197 | */ |
---|
198 | void IMFS_node_destroy_default( IMFS_jnode_t *node ); |
---|
199 | |
---|
200 | /** |
---|
201 | * @brief IMFS node control. |
---|
202 | */ |
---|
203 | typedef struct { |
---|
204 | const rtems_filesystem_file_handlers_r *handlers; |
---|
205 | IMFS_node_control_initialize node_initialize; |
---|
206 | IMFS_node_control_remove node_remove; |
---|
207 | IMFS_node_control_destroy node_destroy; |
---|
208 | } IMFS_node_control; |
---|
209 | |
---|
210 | typedef struct { |
---|
211 | IMFS_node_control node_control; |
---|
212 | size_t node_size; |
---|
213 | } IMFS_mknod_control; |
---|
214 | |
---|
215 | /** @} */ |
---|
216 | |
---|
217 | /** |
---|
218 | * @addtogroup IMFS |
---|
219 | */ |
---|
220 | /**@{*/ |
---|
221 | |
---|
222 | /* |
---|
223 | * Maximum length of a "basename" of an IMFS file/node. |
---|
224 | */ |
---|
225 | |
---|
226 | #define IMFS_NAME_MAX 32 |
---|
227 | |
---|
228 | /* |
---|
229 | * The control structure for an IMFS jnode. |
---|
230 | */ |
---|
231 | |
---|
232 | struct IMFS_jnode_tt { |
---|
233 | rtems_chain_node Node; /* for chaining them together */ |
---|
234 | IMFS_jnode_t *Parent; /* Parent node */ |
---|
235 | char name[IMFS_NAME_MAX+1]; /* "basename" */ |
---|
236 | mode_t st_mode; /* File mode */ |
---|
237 | unsigned short reference_count; |
---|
238 | nlink_t st_nlink; /* Link count */ |
---|
239 | |
---|
240 | uid_t st_uid; /* User ID of owner */ |
---|
241 | gid_t st_gid; /* Group ID of owner */ |
---|
242 | |
---|
243 | time_t stat_atime; /* Time of last access */ |
---|
244 | time_t stat_mtime; /* Time of last modification */ |
---|
245 | time_t stat_ctime; /* Time of last status change */ |
---|
246 | const IMFS_node_control *control; |
---|
247 | }; |
---|
248 | |
---|
249 | typedef struct { |
---|
250 | IMFS_jnode_t Node; |
---|
251 | rtems_chain_control Entries; |
---|
252 | rtems_filesystem_mount_table_entry_t *mt_fs; |
---|
253 | } IMFS_directory_t; |
---|
254 | |
---|
255 | typedef struct { |
---|
256 | IMFS_jnode_t Node; |
---|
257 | rtems_device_major_number major; |
---|
258 | rtems_device_minor_number minor; |
---|
259 | } IMFS_device_t; |
---|
260 | |
---|
261 | typedef struct { |
---|
262 | IMFS_jnode_t Node; |
---|
263 | IMFS_jnode_t *link_node; |
---|
264 | } IMFS_link_t; |
---|
265 | |
---|
266 | typedef struct { |
---|
267 | IMFS_jnode_t Node; |
---|
268 | char *name; |
---|
269 | } IMFS_sym_link_t; |
---|
270 | |
---|
271 | typedef struct { |
---|
272 | IMFS_jnode_t Node; |
---|
273 | size_t size; /* size of file in bytes */ |
---|
274 | } IMFS_filebase_t; |
---|
275 | |
---|
276 | typedef struct { |
---|
277 | IMFS_filebase_t File; |
---|
278 | block_ptr indirect; /* array of 128 data blocks pointers */ |
---|
279 | block_ptr doubly_indirect; /* 128 indirect blocks */ |
---|
280 | block_ptr triply_indirect; /* 128 doubly indirect blocks */ |
---|
281 | } IMFS_memfile_t; |
---|
282 | |
---|
283 | typedef struct { |
---|
284 | IMFS_filebase_t File; |
---|
285 | block_p direct; /* pointer to file image */ |
---|
286 | } IMFS_linearfile_t; |
---|
287 | |
---|
288 | /* Support copy on write for linear files */ |
---|
289 | typedef union { |
---|
290 | IMFS_jnode_t Node; |
---|
291 | IMFS_filebase_t File; |
---|
292 | IMFS_memfile_t Memfile; |
---|
293 | IMFS_linearfile_t Linearfile; |
---|
294 | } IMFS_file_t; |
---|
295 | |
---|
296 | typedef struct { |
---|
297 | IMFS_jnode_t Node; |
---|
298 | pipe_control_t *pipe; |
---|
299 | } IMFS_fifo_t; |
---|
300 | |
---|
301 | typedef struct { |
---|
302 | IMFS_jnode_t Node; |
---|
303 | void *context; |
---|
304 | } IMFS_generic_t; |
---|
305 | |
---|
306 | static inline IMFS_directory_t *IMFS_iop_to_directory( |
---|
307 | const rtems_libio_t *iop |
---|
308 | ) |
---|
309 | { |
---|
310 | return (IMFS_directory_t *) iop->pathinfo.node_access; |
---|
311 | } |
---|
312 | |
---|
313 | static inline IMFS_device_t *IMFS_iop_to_device( const rtems_libio_t *iop ) |
---|
314 | { |
---|
315 | return (IMFS_device_t *) iop->pathinfo.node_access; |
---|
316 | } |
---|
317 | |
---|
318 | static inline IMFS_file_t *IMFS_iop_to_file( const rtems_libio_t *iop ) |
---|
319 | { |
---|
320 | return (IMFS_file_t *) iop->pathinfo.node_access; |
---|
321 | } |
---|
322 | |
---|
323 | static inline IMFS_memfile_t *IMFS_iop_to_memfile( const rtems_libio_t *iop ) |
---|
324 | { |
---|
325 | return (IMFS_memfile_t *) iop->pathinfo.node_access; |
---|
326 | } |
---|
327 | |
---|
328 | static inline void IMFS_update_atime( IMFS_jnode_t *jnode ) |
---|
329 | { |
---|
330 | struct timeval now; |
---|
331 | |
---|
332 | gettimeofday( &now, 0 ); |
---|
333 | |
---|
334 | jnode->stat_atime = now.tv_sec; |
---|
335 | } |
---|
336 | |
---|
337 | static inline void IMFS_update_mtime( IMFS_jnode_t *jnode ) |
---|
338 | { |
---|
339 | struct timeval now; |
---|
340 | |
---|
341 | gettimeofday( &now, 0 ); |
---|
342 | |
---|
343 | jnode->stat_mtime = now.tv_sec; |
---|
344 | } |
---|
345 | |
---|
346 | static inline void IMFS_update_ctime( IMFS_jnode_t *jnode ) |
---|
347 | { |
---|
348 | struct timeval now; |
---|
349 | |
---|
350 | gettimeofday( &now, 0 ); |
---|
351 | |
---|
352 | jnode->stat_ctime = now.tv_sec; |
---|
353 | } |
---|
354 | |
---|
355 | static inline void IMFS_mtime_ctime_update( IMFS_jnode_t *jnode ) |
---|
356 | { |
---|
357 | struct timeval now; |
---|
358 | |
---|
359 | gettimeofday( &now, 0 ); |
---|
360 | |
---|
361 | jnode->stat_mtime = now.tv_sec; |
---|
362 | jnode->stat_ctime = now.tv_sec; |
---|
363 | } |
---|
364 | |
---|
365 | typedef struct { |
---|
366 | const IMFS_mknod_control *directory; |
---|
367 | const IMFS_mknod_control *device; |
---|
368 | const IMFS_mknod_control *file; |
---|
369 | const IMFS_mknod_control *fifo; |
---|
370 | } IMFS_mknod_controls; |
---|
371 | |
---|
372 | typedef struct { |
---|
373 | IMFS_directory_t Root_directory; |
---|
374 | const IMFS_mknod_controls *mknod_controls; |
---|
375 | } IMFS_fs_info_t; |
---|
376 | |
---|
377 | typedef struct { |
---|
378 | IMFS_fs_info_t *fs_info; |
---|
379 | const rtems_filesystem_operations_table *ops; |
---|
380 | const IMFS_mknod_controls *mknod_controls; |
---|
381 | } IMFS_mount_data; |
---|
382 | |
---|
383 | /* |
---|
384 | * Shared Data |
---|
385 | */ |
---|
386 | |
---|
387 | extern const IMFS_mknod_control IMFS_mknod_control_directory; |
---|
388 | extern const IMFS_mknod_control IMFS_mknod_control_device; |
---|
389 | extern const IMFS_mknod_control IMFS_mknod_control_memfile; |
---|
390 | extern const IMFS_node_control IMFS_node_control_linfile; |
---|
391 | extern const IMFS_mknod_control IMFS_mknod_control_fifo; |
---|
392 | extern const IMFS_mknod_control IMFS_mknod_control_enosys; |
---|
393 | |
---|
394 | extern const rtems_filesystem_limits_and_options_t IMFS_LIMITS_AND_OPTIONS; |
---|
395 | |
---|
396 | /* |
---|
397 | * Routines |
---|
398 | */ |
---|
399 | |
---|
400 | extern int IMFS_initialize( |
---|
401 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
402 | const void *data |
---|
403 | ); |
---|
404 | |
---|
405 | extern int IMFS_initialize_support( |
---|
406 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
407 | const void *data |
---|
408 | ); |
---|
409 | |
---|
410 | /** |
---|
411 | * @brief Unmount this instance of IMFS. |
---|
412 | */ |
---|
413 | extern void IMFS_fsunmount( |
---|
414 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
415 | ); |
---|
416 | |
---|
417 | /** |
---|
418 | * @brief RTEMS load tarfs. |
---|
419 | * |
---|
420 | * This file implements the "mount" procedure for tar-based IMFS |
---|
421 | * extensions. The TAR is not actually mounted under the IMFS. |
---|
422 | * Directories from the TAR file are created as usual in the IMFS. |
---|
423 | * File entries are created as IMFS_LINEAR_FILE nodes with their nods |
---|
424 | * pointing to addresses in the TAR image. |
---|
425 | * |
---|
426 | * Here we create the mountpoint directory and load the tarfs at |
---|
427 | * that node. Once the IMFS has been mounted, we work through the |
---|
428 | * tar image and perform as follows: |
---|
429 | * - For directories, simply call mkdir(). The IMFS creates nodes as |
---|
430 | * needed. |
---|
431 | * - For files, we make our own calls to IMFS eval_for_make and |
---|
432 | * create_node. |
---|
433 | * |
---|
434 | * TAR file format: |
---|
435 | * |
---|
436 | * @code |
---|
437 | * Offset Length Contents |
---|
438 | * 0 100 bytes File name ('\0' terminated, 99 maxmum length) |
---|
439 | * 100 8 bytes File mode (in octal ascii) |
---|
440 | * 108 8 bytes User ID (in octal ascii) |
---|
441 | * 116 8 bytes Group ID (in octal ascii) |
---|
442 | * 124 12 bytes File size (s) (in octal ascii) |
---|
443 | * 136 12 bytes Modify time (in octal ascii) |
---|
444 | * 148 8 bytes Header checksum (in octal ascii) |
---|
445 | * 156 1 bytes Link flag |
---|
446 | * 157 100 bytes Linkname ('\0' terminated, 99 maxmum length) |
---|
447 | * 257 8 bytes Magic PAX ("ustar\0" + 2 bytes padding) |
---|
448 | * 257 8 bytes Magic GNU tar ("ustar \0") |
---|
449 | * 265 32 bytes User name ('\0' terminated, 31 maxmum length) |
---|
450 | * 297 32 bytes Group name ('\0' terminated, 31 maxmum length) |
---|
451 | * 329 8 bytes Major device ID (in octal ascii) |
---|
452 | * 337 8 bytes Minor device ID (in octal ascii) |
---|
453 | * 345 167 bytes Padding |
---|
454 | * 512 (s+p) bytes File contents (s+p) := (((s) + 511) & ~511), |
---|
455 | * round up to 512 bytes |
---|
456 | * @endcode |
---|
457 | * |
---|
458 | * Checksum: |
---|
459 | * @code |
---|
460 | * int i, sum; |
---|
461 | * char *header = tar_header_pointer; |
---|
462 | * |
---|
463 | * sum = 0; |
---|
464 | * for (i = 0; i < 512; i++) |
---|
465 | * sum += 0xFF & header[i]; |
---|
466 | * @endcode |
---|
467 | */ |
---|
468 | extern int rtems_tarfs_load( |
---|
469 | const char *mountpoint, |
---|
470 | uint8_t *tar_image, |
---|
471 | size_t tar_size |
---|
472 | ); |
---|
473 | |
---|
474 | /** |
---|
475 | * @brief Destroy an IMFS node. |
---|
476 | */ |
---|
477 | extern void IMFS_node_destroy( IMFS_jnode_t *node ); |
---|
478 | |
---|
479 | /** |
---|
480 | * @brief Clone an IMFS node. |
---|
481 | */ |
---|
482 | extern int IMFS_node_clone( rtems_filesystem_location_info_t *loc ); |
---|
483 | |
---|
484 | /** |
---|
485 | * @brief Free an IMFS node. |
---|
486 | */ |
---|
487 | extern void IMFS_node_free( const rtems_filesystem_location_info_t *loc ); |
---|
488 | |
---|
489 | /** |
---|
490 | * @brief Perform a status processing for the IMFS. |
---|
491 | * |
---|
492 | * This routine provides a stat for the IMFS file system. |
---|
493 | */ |
---|
494 | extern int IMFS_stat( |
---|
495 | const rtems_filesystem_location_info_t *loc, |
---|
496 | struct stat *buf |
---|
497 | ); |
---|
498 | |
---|
499 | extern int IMFS_stat_file( |
---|
500 | const rtems_filesystem_location_info_t *loc, |
---|
501 | struct stat *buf |
---|
502 | ); |
---|
503 | |
---|
504 | /** |
---|
505 | * @brief IMFS evaluation node support. |
---|
506 | */ |
---|
507 | extern void IMFS_eval_path( |
---|
508 | rtems_filesystem_eval_path_context_t *ctx |
---|
509 | ); |
---|
510 | |
---|
511 | /** |
---|
512 | * @brief Create a new IMFS link node. |
---|
513 | * |
---|
514 | * The following rouine creates a new link node under parent with the |
---|
515 | * name given in name. The link node is set to point to the node at |
---|
516 | * to_loc. |
---|
517 | */ |
---|
518 | extern int IMFS_link( |
---|
519 | const rtems_filesystem_location_info_t *parentloc, |
---|
520 | const rtems_filesystem_location_info_t *targetloc, |
---|
521 | const char *name, |
---|
522 | size_t namelen |
---|
523 | ); |
---|
524 | |
---|
525 | /** |
---|
526 | * @brief Change the owner of IMFS. |
---|
527 | * |
---|
528 | * This routine is the implementation of the chown() system |
---|
529 | * call for the IMFS. |
---|
530 | */ |
---|
531 | extern int IMFS_chown( |
---|
532 | const rtems_filesystem_location_info_t *loc, |
---|
533 | uid_t owner, |
---|
534 | gid_t group |
---|
535 | ); |
---|
536 | |
---|
537 | /** |
---|
538 | * @brief Create an IMFS node. |
---|
539 | * |
---|
540 | * Routine to create a node in the IMFS file system. |
---|
541 | */ |
---|
542 | extern int IMFS_mknod( |
---|
543 | const rtems_filesystem_location_info_t *parentloc, |
---|
544 | const char *name, |
---|
545 | size_t namelen, |
---|
546 | mode_t mode, |
---|
547 | dev_t dev |
---|
548 | ); |
---|
549 | |
---|
550 | extern IMFS_jnode_t *IMFS_initialize_node( |
---|
551 | IMFS_jnode_t *node, |
---|
552 | const IMFS_node_control *node_control, |
---|
553 | const char *name, |
---|
554 | size_t namelen, |
---|
555 | mode_t mode, |
---|
556 | void *arg |
---|
557 | ); |
---|
558 | |
---|
559 | /** |
---|
560 | * @brief Create an IMFS node. |
---|
561 | * |
---|
562 | * Create an IMFS filesystem node of an arbitrary type that is NOT |
---|
563 | * the root directory node. |
---|
564 | */ |
---|
565 | extern IMFS_jnode_t *IMFS_create_node( |
---|
566 | const rtems_filesystem_location_info_t *parentloc, |
---|
567 | const IMFS_node_control *node_control, |
---|
568 | size_t node_size, |
---|
569 | const char *name, |
---|
570 | size_t namelen, |
---|
571 | mode_t mode, |
---|
572 | void *arg |
---|
573 | ); |
---|
574 | |
---|
575 | static inline bool IMFS_is_imfs_instance( |
---|
576 | const rtems_filesystem_location_info_t *loc |
---|
577 | ) |
---|
578 | { |
---|
579 | return loc->mt_entry->ops->clonenod_h == IMFS_node_clone; |
---|
580 | } |
---|
581 | |
---|
582 | /** @} */ |
---|
583 | |
---|
584 | /** |
---|
585 | * @defgroup IMFSGenericNodes IMFS Generic Nodes |
---|
586 | * |
---|
587 | * @ingroup LibIO |
---|
588 | * |
---|
589 | * @brief Generic nodes are an alternative to standard drivers in RTEMS. |
---|
590 | * |
---|
591 | * The handlers of a generic node are called with less overhead compared to the |
---|
592 | * standard driver operations. The usage of file system node handlers enable |
---|
593 | * more features like support for fsync() and fdatasync(). The generic nodes |
---|
594 | * use the reference counting of the IMFS. This provides automatic node |
---|
595 | * destruction when the last reference vanishes. |
---|
596 | * |
---|
597 | * @{ |
---|
598 | */ |
---|
599 | |
---|
600 | /** |
---|
601 | * @brief Initializer for a generic node control. |
---|
602 | * |
---|
603 | * @param[in] handlers The file system node handlers. |
---|
604 | * @param[in] init The node initialization method. |
---|
605 | * @param[in] destroy The node destruction method. |
---|
606 | */ |
---|
607 | #define IMFS_GENERIC_INITIALIZER( handlers, init, destroy ) \ |
---|
608 | { \ |
---|
609 | ( handlers ), \ |
---|
610 | ( init ), \ |
---|
611 | IMFS_node_remove_default, \ |
---|
612 | ( destroy ) \ |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * @brief Makes a generic IMFS node. |
---|
617 | * |
---|
618 | * @param[in] path The path to the new generic IMFS node. |
---|
619 | * @param[in] mode The node mode. |
---|
620 | * @param[in] node_control The node control. |
---|
621 | * @param[in] context The node control handler context. |
---|
622 | * |
---|
623 | * @retval 0 Successful operation. |
---|
624 | * @retval -1 An error occurred. The @c errno indicates the error. |
---|
625 | * |
---|
626 | * @code |
---|
627 | * #include <sys/stat.h> |
---|
628 | * #include <assert.h> |
---|
629 | * #include <fcntl.h> |
---|
630 | * |
---|
631 | * #include <rtems/imfs.h> |
---|
632 | * |
---|
633 | * static const rtems_filesystem_file_handlers_r some_node_handlers = { |
---|
634 | * ... |
---|
635 | * }; |
---|
636 | * |
---|
637 | * static IMFS_jnode_t *some_node_init(IMFS_jnode_t *node, void *arg) |
---|
638 | * { |
---|
639 | * void *context; |
---|
640 | * |
---|
641 | * node = IMFS_node_initialize_generic(node, arg); |
---|
642 | * context = IMFS_generic_get_context_by_node(node); |
---|
643 | * |
---|
644 | * return node; |
---|
645 | * } |
---|
646 | * |
---|
647 | * static void some_node_destroy(IMFS_jnode_t *node) |
---|
648 | * { |
---|
649 | * void *context = IMFS_generic_get_context_by_node(node); |
---|
650 | * |
---|
651 | * IMFS_node_destroy_default(node); |
---|
652 | * } |
---|
653 | * |
---|
654 | * static const IMFS_node_control some_node_control = IMFS_GENERIC_INITIALIZER( |
---|
655 | * &some_node_handlers, |
---|
656 | * some_node_init, |
---|
657 | * some_node_destroy |
---|
658 | * ); |
---|
659 | * |
---|
660 | * void example(void *some_node_context) |
---|
661 | * { |
---|
662 | * int rv; |
---|
663 | * |
---|
664 | * rv = IMFS_make_generic_node( |
---|
665 | * "/path/to/some/generic/node", |
---|
666 | * S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
667 | * &some_node_control, |
---|
668 | * some_node_context |
---|
669 | * ); |
---|
670 | * assert(rv == 0); |
---|
671 | * } |
---|
672 | * @endcode |
---|
673 | */ |
---|
674 | extern int IMFS_make_generic_node( |
---|
675 | const char *path, |
---|
676 | mode_t mode, |
---|
677 | const IMFS_node_control *node_control, |
---|
678 | void *context |
---|
679 | ); |
---|
680 | |
---|
681 | /** @} */ |
---|
682 | |
---|
683 | /** |
---|
684 | * @addtogroup IMFS |
---|
685 | */ |
---|
686 | /**@{*/ |
---|
687 | |
---|
688 | /** |
---|
689 | * @brief Mount an IMFS. |
---|
690 | */ |
---|
691 | extern int IMFS_mount( |
---|
692 | rtems_filesystem_mount_table_entry_t *mt_entry /* IN */ |
---|
693 | ); |
---|
694 | |
---|
695 | /** |
---|
696 | * @brief Unmount an IMFS. |
---|
697 | */ |
---|
698 | extern int IMFS_unmount( |
---|
699 | rtems_filesystem_mount_table_entry_t *mt_entry /* IN */ |
---|
700 | ); |
---|
701 | |
---|
702 | /** |
---|
703 | * @brief Read the next directory of the IMFS. |
---|
704 | * |
---|
705 | * This routine will read the next directory entry based on the directory |
---|
706 | * offset. The offset should be equal to -n- time the size of an individual |
---|
707 | * dirent structure. If n is not an integer multiple of the sizeof a |
---|
708 | * dirent structure, an integer division will be performed to determine |
---|
709 | * directory entry that will be returned in the buffer. Count should reflect |
---|
710 | * -m- times the sizeof dirent bytes to be placed in the buffer. |
---|
711 | * If there are not -m- dirent elements from the current directory position |
---|
712 | * to the end of the exisiting file, the remaining entries will be placed in |
---|
713 | * the buffer and the returned value will be equal to -m actual- times the |
---|
714 | * size of a directory entry. |
---|
715 | */ |
---|
716 | extern ssize_t imfs_dir_read( |
---|
717 | rtems_libio_t *iop, /* IN */ |
---|
718 | void *buffer, /* IN */ |
---|
719 | size_t count /* IN */ |
---|
720 | ); |
---|
721 | |
---|
722 | /** |
---|
723 | * @name IMFS Memory File Handlers |
---|
724 | * |
---|
725 | * This section contains the set of handlers used to process operations on |
---|
726 | * IMFS memory file nodes. The memory files are created in memory using |
---|
727 | * malloc'ed memory. Thus any data stored in one of these files is lost |
---|
728 | * at system shutdown unless special arrangements to copy the data to |
---|
729 | * some type of non-volailte storage are made by the application. |
---|
730 | */ |
---|
731 | /**@{*/ |
---|
732 | |
---|
733 | extern ssize_t IMFS_memfile_write( |
---|
734 | IMFS_memfile_t *memfile, |
---|
735 | off_t start, |
---|
736 | const unsigned char *source, |
---|
737 | unsigned int length |
---|
738 | ); |
---|
739 | |
---|
740 | /** @} */ |
---|
741 | |
---|
742 | /** |
---|
743 | * @name IMFS Device Node Handlers |
---|
744 | * |
---|
745 | * This section contains the set of handlers used to map operations on |
---|
746 | * IMFS device nodes onto calls to the RTEMS Classic API IO Manager. |
---|
747 | */ |
---|
748 | /**@{*/ |
---|
749 | |
---|
750 | extern int device_open( |
---|
751 | rtems_libio_t *iop, /* IN */ |
---|
752 | const char *pathname, /* IN */ |
---|
753 | int oflag, /* IN */ |
---|
754 | mode_t mode /* IN */ |
---|
755 | ); |
---|
756 | |
---|
757 | extern int device_close( |
---|
758 | rtems_libio_t *iop /* IN */ |
---|
759 | ); |
---|
760 | |
---|
761 | extern ssize_t device_read( |
---|
762 | rtems_libio_t *iop, /* IN */ |
---|
763 | void *buffer, /* IN */ |
---|
764 | size_t count /* IN */ |
---|
765 | ); |
---|
766 | |
---|
767 | extern ssize_t device_write( |
---|
768 | rtems_libio_t *iop, /* IN */ |
---|
769 | const void *buffer, /* IN */ |
---|
770 | size_t count /* IN */ |
---|
771 | ); |
---|
772 | |
---|
773 | extern int device_ioctl( |
---|
774 | rtems_libio_t *iop, |
---|
775 | ioctl_command_t command, |
---|
776 | void *buffer |
---|
777 | ); |
---|
778 | |
---|
779 | extern int device_ftruncate( |
---|
780 | rtems_libio_t *iop, /* IN */ |
---|
781 | off_t length /* IN */ |
---|
782 | ); |
---|
783 | |
---|
784 | /** @} */ |
---|
785 | |
---|
786 | /** |
---|
787 | * @brief Set IMFS file access and modification times. |
---|
788 | * |
---|
789 | * |
---|
790 | * This routine is the implementation of the utime() system |
---|
791 | * call for the IMFS. |
---|
792 | */ |
---|
793 | extern int IMFS_utime( |
---|
794 | const rtems_filesystem_location_info_t *loc, |
---|
795 | time_t actime, |
---|
796 | time_t modtime |
---|
797 | ); |
---|
798 | |
---|
799 | /** |
---|
800 | * @brief Change the IMFS file mode. |
---|
801 | */ |
---|
802 | extern int IMFS_fchmod( |
---|
803 | const rtems_filesystem_location_info_t *loc, |
---|
804 | mode_t mode |
---|
805 | ); |
---|
806 | |
---|
807 | /** |
---|
808 | * @brief Create a new IMFS symbolic link node. |
---|
809 | * |
---|
810 | * The following rouine creates a new symbolic link node under parent |
---|
811 | * with the name given in name. The node is set to point to the node at |
---|
812 | * to_loc. |
---|
813 | */ |
---|
814 | extern int IMFS_symlink( |
---|
815 | const rtems_filesystem_location_info_t *parentloc, |
---|
816 | const char *name, |
---|
817 | size_t namelen, |
---|
818 | const char *target |
---|
819 | ); |
---|
820 | |
---|
821 | /** |
---|
822 | * @brief Put IMFS symbolic link into buffer. |
---|
823 | * |
---|
824 | * The following rouine puts the symbolic links destination name into |
---|
825 | * buff. |
---|
826 | * |
---|
827 | */ |
---|
828 | extern ssize_t IMFS_readlink( |
---|
829 | const rtems_filesystem_location_info_t *loc, |
---|
830 | char *buf, |
---|
831 | size_t bufsize |
---|
832 | ); |
---|
833 | |
---|
834 | /** |
---|
835 | * @brief Rename the IMFS. |
---|
836 | * |
---|
837 | * The following rouine creates a new link node under parent with the |
---|
838 | * name given in name and removes the old. |
---|
839 | */ |
---|
840 | extern int IMFS_rename( |
---|
841 | const rtems_filesystem_location_info_t *oldparentloc, |
---|
842 | const rtems_filesystem_location_info_t *oldloc, |
---|
843 | const rtems_filesystem_location_info_t *newparentloc, |
---|
844 | const char *name, |
---|
845 | size_t namelen |
---|
846 | ); |
---|
847 | /** |
---|
848 | * @brief IMFS node removal handler. |
---|
849 | * |
---|
850 | * This file contains the handler used to remove a node when a file type |
---|
851 | * does not require special actions. |
---|
852 | */ |
---|
853 | extern int IMFS_rmnod( |
---|
854 | const rtems_filesystem_location_info_t *parentloc, |
---|
855 | const rtems_filesystem_location_info_t *loc |
---|
856 | ); |
---|
857 | |
---|
858 | /* |
---|
859 | * Turn on IMFS assertions when RTEMS_DEBUG is defined. |
---|
860 | */ |
---|
861 | #ifdef RTEMS_DEBUG |
---|
862 | #include <assert.h> |
---|
863 | |
---|
864 | #define IMFS_assert(_x) assert(_x) |
---|
865 | #else |
---|
866 | #define IMFS_assert(_x) |
---|
867 | #endif |
---|
868 | |
---|
869 | static inline void IMFS_Set_handlers( rtems_filesystem_location_info_t *loc ) |
---|
870 | { |
---|
871 | IMFS_jnode_t *node = (IMFS_jnode_t *) loc->node_access; |
---|
872 | |
---|
873 | loc->handlers = node->control->handlers; |
---|
874 | } |
---|
875 | |
---|
876 | static inline void IMFS_add_to_directory( |
---|
877 | IMFS_jnode_t *dir_node, |
---|
878 | IMFS_jnode_t *entry_node |
---|
879 | ) |
---|
880 | { |
---|
881 | IMFS_directory_t *dir = (IMFS_directory_t *) dir_node; |
---|
882 | |
---|
883 | entry_node->Parent = dir_node; |
---|
884 | rtems_chain_append_unprotected( &dir->Entries, &entry_node->Node ); |
---|
885 | } |
---|
886 | |
---|
887 | static inline void IMFS_remove_from_directory( IMFS_jnode_t *node ) |
---|
888 | { |
---|
889 | IMFS_assert( node->Parent != NULL ); |
---|
890 | node->Parent = NULL; |
---|
891 | rtems_chain_extract_unprotected( &node->Node ); |
---|
892 | } |
---|
893 | |
---|
894 | static inline bool IMFS_is_directory( const IMFS_jnode_t *node ) |
---|
895 | { |
---|
896 | return S_ISDIR( node->st_mode ); |
---|
897 | } |
---|
898 | |
---|
899 | #define IMFS_STAT_FMT_HARD_LINK 0 |
---|
900 | |
---|
901 | static inline bool IMFS_is_hard_link( mode_t mode ) |
---|
902 | { |
---|
903 | return ( mode & S_IFMT ) == IMFS_STAT_FMT_HARD_LINK; |
---|
904 | } |
---|
905 | |
---|
906 | static inline ino_t IMFS_node_to_ino( const IMFS_jnode_t *node ) |
---|
907 | { |
---|
908 | return (ino_t) node; |
---|
909 | } |
---|
910 | |
---|
911 | /** @} */ |
---|
912 | |
---|
913 | /** |
---|
914 | * @addtogroup IMFSGenericNodes |
---|
915 | */ |
---|
916 | /**@{*/ |
---|
917 | |
---|
918 | static inline void *IMFS_generic_get_context_by_node( |
---|
919 | const IMFS_jnode_t *node |
---|
920 | ) |
---|
921 | { |
---|
922 | const IMFS_generic_t *generic = (const IMFS_generic_t *) node; |
---|
923 | |
---|
924 | return generic->context; |
---|
925 | } |
---|
926 | |
---|
927 | static inline void *IMFS_generic_get_context_by_location( |
---|
928 | const rtems_filesystem_location_info_t *loc |
---|
929 | ) |
---|
930 | { |
---|
931 | return loc->node_access_2; |
---|
932 | } |
---|
933 | |
---|
934 | static inline void *IMFS_generic_get_context_by_iop( |
---|
935 | const rtems_libio_t *iop |
---|
936 | ) |
---|
937 | { |
---|
938 | return IMFS_generic_get_context_by_location( &iop->pathinfo ); |
---|
939 | } |
---|
940 | |
---|
941 | static inline dev_t IMFS_generic_get_device_identifier_by_node( |
---|
942 | const IMFS_jnode_t *node |
---|
943 | ) |
---|
944 | { |
---|
945 | return rtems_filesystem_make_dev_t_from_pointer( node ); |
---|
946 | } |
---|
947 | |
---|
948 | #ifdef __cplusplus |
---|
949 | } |
---|
950 | #endif |
---|
951 | /** @} */ |
---|
952 | #endif |
---|
953 | /* end of include file */ |
---|