1 | /* |
---|
2 | * mount() |
---|
3 | * |
---|
4 | * Mange the mount table. You can iterate on mounts and file systems, as well |
---|
5 | * as add and remove file systems not in the file system confiration table. |
---|
6 | * |
---|
7 | * COPYRIGHT (c) Chris Johns <chrisj@rtems.org> 2010. |
---|
8 | * |
---|
9 | * Copyright (c) 2010 embedded brains GmbH. |
---|
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 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <rtems/chain.h> |
---|
23 | #include <rtems/seterr.h> |
---|
24 | #include <fcntl.h> |
---|
25 | #include <unistd.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | #include <rtems/libio_.h> |
---|
31 | |
---|
32 | typedef struct { |
---|
33 | rtems_chain_node node; |
---|
34 | rtems_filesystem_table_t entry; |
---|
35 | } filesystem_node; |
---|
36 | |
---|
37 | static RTEMS_CHAIN_DEFINE_EMPTY(filesystem_chain); |
---|
38 | |
---|
39 | bool rtems_filesystem_iterate( |
---|
40 | rtems_per_filesystem_routine routine, |
---|
41 | void *routine_arg |
---|
42 | ) |
---|
43 | { |
---|
44 | rtems_chain_control *chain = &filesystem_chain; |
---|
45 | const rtems_filesystem_table_t *table_entry = &rtems_filesystem_table [0]; |
---|
46 | rtems_chain_node *node = NULL; |
---|
47 | bool stop = false; |
---|
48 | |
---|
49 | while ( table_entry->type && !stop ) { |
---|
50 | stop = (*routine)( table_entry, routine_arg ); |
---|
51 | ++table_entry; |
---|
52 | } |
---|
53 | |
---|
54 | if ( !stop ) { |
---|
55 | rtems_libio_lock(); |
---|
56 | for ( |
---|
57 | node = rtems_chain_first( chain ); |
---|
58 | !rtems_chain_is_tail( chain, node ) && !stop; |
---|
59 | node = rtems_chain_next( node ) |
---|
60 | ) { |
---|
61 | const filesystem_node *fsn = (filesystem_node *) node; |
---|
62 | |
---|
63 | stop = (*routine)( &fsn->entry, routine_arg ); |
---|
64 | } |
---|
65 | rtems_libio_unlock(); |
---|
66 | } |
---|
67 | |
---|
68 | return stop; |
---|
69 | } |
---|
70 | |
---|
71 | typedef struct { |
---|
72 | const char *type; |
---|
73 | rtems_filesystem_fsmount_me_t mount_h; |
---|
74 | } find_arg; |
---|
75 | |
---|
76 | static bool find_handler(const rtems_filesystem_table_t *entry, void *arg) |
---|
77 | { |
---|
78 | find_arg *fa = arg; |
---|
79 | |
---|
80 | if ( strcmp( entry->type, fa->type ) != 0 ) { |
---|
81 | return false; |
---|
82 | } else { |
---|
83 | fa->mount_h = entry->mount_h; |
---|
84 | |
---|
85 | return true; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | rtems_filesystem_fsmount_me_t |
---|
90 | rtems_filesystem_get_mount_handler( |
---|
91 | const char *type |
---|
92 | ) |
---|
93 | { |
---|
94 | find_arg fa = { |
---|
95 | .type = type, |
---|
96 | .mount_h = NULL |
---|
97 | }; |
---|
98 | |
---|
99 | if ( type != NULL ) { |
---|
100 | rtems_filesystem_iterate( find_handler, &fa ); |
---|
101 | } |
---|
102 | |
---|
103 | return fa.mount_h; |
---|
104 | } |
---|
105 | |
---|
106 | int |
---|
107 | rtems_filesystem_register( |
---|
108 | const char *type, |
---|
109 | rtems_filesystem_fsmount_me_t mount_h |
---|
110 | ) |
---|
111 | { |
---|
112 | rtems_chain_control *chain = &filesystem_chain; |
---|
113 | size_t type_size = strlen(type) + 1; |
---|
114 | size_t fsn_size = sizeof( filesystem_node ) + type_size; |
---|
115 | filesystem_node *fsn = malloc( fsn_size ); |
---|
116 | char *type_storage = (char *) fsn + sizeof( *fsn ); |
---|
117 | |
---|
118 | if ( fsn == NULL ) |
---|
119 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
120 | |
---|
121 | memcpy(type_storage, type, type_size); |
---|
122 | fsn->entry.type = type_storage; |
---|
123 | fsn->entry.mount_h = mount_h; |
---|
124 | |
---|
125 | rtems_libio_lock(); |
---|
126 | if ( rtems_filesystem_get_mount_handler( type ) == NULL ) { |
---|
127 | rtems_chain_append_unprotected( chain, &fsn->node ); |
---|
128 | } else { |
---|
129 | rtems_libio_unlock(); |
---|
130 | free( fsn ); |
---|
131 | |
---|
132 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
133 | } |
---|
134 | rtems_libio_unlock(); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | int |
---|
140 | rtems_filesystem_unregister( |
---|
141 | const char *type |
---|
142 | ) |
---|
143 | { |
---|
144 | rtems_chain_control *chain = &filesystem_chain; |
---|
145 | rtems_chain_node *node = NULL; |
---|
146 | |
---|
147 | if ( type == NULL ) { |
---|
148 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
149 | } |
---|
150 | |
---|
151 | rtems_libio_lock(); |
---|
152 | for ( |
---|
153 | node = rtems_chain_first( chain ); |
---|
154 | !rtems_chain_is_tail( chain, node ); |
---|
155 | node = rtems_chain_next( node ) |
---|
156 | ) { |
---|
157 | filesystem_node *fsn = (filesystem_node *) node; |
---|
158 | |
---|
159 | if ( strcmp( fsn->entry.type, type ) == 0 ) { |
---|
160 | rtems_chain_extract_unprotected( node ); |
---|
161 | free( fsn ); |
---|
162 | rtems_libio_unlock(); |
---|
163 | |
---|
164 | return 0; |
---|
165 | } |
---|
166 | } |
---|
167 | rtems_libio_unlock(); |
---|
168 | |
---|
169 | rtems_set_errno_and_return_minus_one( ENOENT ); |
---|
170 | } |
---|