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 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.com/license/LICENSE. |
---|
12 | * |
---|
13 | * $Id$ |
---|
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 | #include <assert.h> |
---|
30 | |
---|
31 | #include <rtems/libio_.h> |
---|
32 | |
---|
33 | /* |
---|
34 | * External defined by confdefs.h or the user. |
---|
35 | */ |
---|
36 | extern const rtems_filesystem_table_t configuration_filesystem_table[]; |
---|
37 | |
---|
38 | /* |
---|
39 | * Points to a list of filesystems added at runtime. |
---|
40 | */ |
---|
41 | extern rtems_chain_control *rtems_filesystem_table; |
---|
42 | |
---|
43 | /* |
---|
44 | * Mount table list. |
---|
45 | */ |
---|
46 | extern rtems_chain_control rtems_filesystem_mount_table_control; |
---|
47 | extern bool rtems_filesystem_mount_table_control_init; |
---|
48 | |
---|
49 | /* |
---|
50 | * Get the first entry in the filesystem table. |
---|
51 | */ |
---|
52 | const rtems_filesystem_table_t* |
---|
53 | rtems_filesystem_table_first( |
---|
54 | void |
---|
55 | ) |
---|
56 | { |
---|
57 | /* |
---|
58 | * We can assume this because it is the root file system. |
---|
59 | */ |
---|
60 | return &configuration_filesystem_table[0]; |
---|
61 | } |
---|
62 | |
---|
63 | /* |
---|
64 | * Get the next entry in the file system table. |
---|
65 | */ |
---|
66 | const rtems_filesystem_table_t* |
---|
67 | rtems_filesystem_table_next( |
---|
68 | rtems_filesystem_table_t *entry |
---|
69 | ) |
---|
70 | { |
---|
71 | const rtems_filesystem_table_t* fs; |
---|
72 | |
---|
73 | fs = rtems_filesystem_table_first( ); |
---|
74 | |
---|
75 | while ( fs->type && ( fs != entry ) ) |
---|
76 | ++fs; |
---|
77 | |
---|
78 | if ( fs->type ) { |
---|
79 | ++fs; |
---|
80 | if ( fs->type ) |
---|
81 | return fs; |
---|
82 | } |
---|
83 | |
---|
84 | if ( rtems_filesystem_table ) { |
---|
85 | rtems_chain_node* node; |
---|
86 | for (node = rtems_chain_first( rtems_filesystem_table ); |
---|
87 | !rtems_chain_is_tail( rtems_filesystem_table, node); |
---|
88 | node = rtems_chain_next( node )) { |
---|
89 | rtems_filesystem_table_node_t* tnode; |
---|
90 | tnode = (rtems_filesystem_table_node_t*) node; |
---|
91 | if ( entry == &tnode->entry ) { |
---|
92 | node = rtems_chain_next( node ); |
---|
93 | if ( !rtems_chain_is_tail( rtems_filesystem_table, node ) ) { |
---|
94 | tnode = (rtems_filesystem_table_node_t*) node; |
---|
95 | return &tnode->entry; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * Get the first entry in the mount table. |
---|
106 | */ |
---|
107 | rtems_filesystem_mount_table_entry_t* |
---|
108 | rtems_filesystem_mounts_first( |
---|
109 | void |
---|
110 | ) |
---|
111 | { |
---|
112 | rtems_filesystem_mount_table_entry_t* entry = NULL; |
---|
113 | if ( rtems_filesystem_mount_table_control_init ) { |
---|
114 | if ( !rtems_chain_is_empty( &rtems_filesystem_mount_table_control ) ) |
---|
115 | entry = (rtems_filesystem_mount_table_entry_t*) |
---|
116 | rtems_chain_first( &rtems_filesystem_mount_table_control ); |
---|
117 | } |
---|
118 | return entry; |
---|
119 | } |
---|
120 | |
---|
121 | /* |
---|
122 | * Get the next entry in the mount table. |
---|
123 | */ |
---|
124 | rtems_filesystem_mount_table_entry_t* |
---|
125 | rtems_filesystem_mounts_next( |
---|
126 | rtems_filesystem_mount_table_entry_t *entry |
---|
127 | ) |
---|
128 | { |
---|
129 | if ( !rtems_filesystem_mount_table_control_init || !entry ) |
---|
130 | return NULL; |
---|
131 | return (rtems_filesystem_mount_table_entry_t*) rtems_chain_next( &entry->Node ); |
---|
132 | } |
---|
133 | |
---|
134 | /* |
---|
135 | * Register a file system. |
---|
136 | */ |
---|
137 | int |
---|
138 | rtems_filesystem_register( |
---|
139 | const char *type, |
---|
140 | rtems_filesystem_fsmount_me_t mount_h |
---|
141 | ) |
---|
142 | { |
---|
143 | rtems_filesystem_table_node_t *fs; |
---|
144 | if ( !rtems_filesystem_table ) { |
---|
145 | rtems_filesystem_table = malloc( sizeof( rtems_chain_control ) ); |
---|
146 | if ( !rtems_filesystem_table ) |
---|
147 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
148 | rtems_chain_initialize_empty ( rtems_filesystem_table ); |
---|
149 | } |
---|
150 | fs = malloc( sizeof( rtems_filesystem_table_node_t ) ); |
---|
151 | if ( !fs ) |
---|
152 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
153 | fs->entry.type = strdup( type ); |
---|
154 | if ( !fs->entry.type ) { |
---|
155 | free( fs ); |
---|
156 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
157 | } |
---|
158 | fs->entry.mount_h = mount_h; |
---|
159 | rtems_chain_append( rtems_filesystem_table, &fs->node ); |
---|
160 | return 0; |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | * Unregister a file system. |
---|
165 | */ |
---|
166 | int |
---|
167 | rtems_filesystem_unregister( |
---|
168 | const char *type |
---|
169 | ) |
---|
170 | { |
---|
171 | if ( rtems_filesystem_table ) { |
---|
172 | rtems_chain_node *node; |
---|
173 | for (node = rtems_chain_first( rtems_filesystem_table ); |
---|
174 | !rtems_chain_is_tail( rtems_filesystem_table, node ); |
---|
175 | node = rtems_chain_next( node ) ) { |
---|
176 | rtems_filesystem_table_node_t *fs; |
---|
177 | fs = (rtems_filesystem_table_node_t*) node; |
---|
178 | if ( strcmp( fs->entry.type, type ) == 0 ) { |
---|
179 | rtems_chain_extract( node ); |
---|
180 | free( (void*) fs->entry.type ); |
---|
181 | free( fs ); |
---|
182 | return 0; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | rtems_set_errno_and_return_minus_one( ENOENT ); |
---|
187 | } |
---|