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.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/stat.h> |
---|
24 | #include <rtems/chain.h> |
---|
25 | #include <rtems/seterr.h> |
---|
26 | #include <fcntl.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | #include <assert.h> |
---|
32 | |
---|
33 | #include <rtems/libio_.h> |
---|
34 | |
---|
35 | typedef struct { |
---|
36 | rtems_chain_node node; |
---|
37 | rtems_filesystem_table_t entry; |
---|
38 | } filesystem_node; |
---|
39 | |
---|
40 | static RTEMS_CHAIN_DEFINE_EMPTY(filesystem_chain); |
---|
41 | |
---|
42 | bool rtems_filesystem_iterate( |
---|
43 | rtems_per_filesystem_routine routine, |
---|
44 | void *routine_arg |
---|
45 | ) |
---|
46 | { |
---|
47 | const rtems_filesystem_table_t *table_entry = &rtems_filesystem_table [0]; |
---|
48 | rtems_chain_node *node = NULL; |
---|
49 | bool stop = false; |
---|
50 | |
---|
51 | while ( table_entry->type && !stop ) { |
---|
52 | stop = (*routine)( table_entry, routine_arg ); |
---|
53 | ++table_entry; |
---|
54 | } |
---|
55 | |
---|
56 | if ( !stop ) { |
---|
57 | rtems_libio_lock(); |
---|
58 | for ( |
---|
59 | node = rtems_chain_first( &filesystem_chain ); |
---|
60 | !rtems_chain_is_tail( &filesystem_chain, node ) && !stop; |
---|
61 | node = rtems_chain_next( node ) |
---|
62 | ) { |
---|
63 | const filesystem_node *fsn = (filesystem_node *) node; |
---|
64 | |
---|
65 | stop = (*routine)( &fsn->entry, routine_arg ); |
---|
66 | } |
---|
67 | rtems_libio_unlock(); |
---|
68 | } |
---|
69 | |
---|
70 | return stop; |
---|
71 | } |
---|
72 | |
---|
73 | typedef struct { |
---|
74 | const char *type; |
---|
75 | rtems_filesystem_fsmount_me_t mount_h; |
---|
76 | } find_arg; |
---|
77 | |
---|
78 | static bool find_handler(const rtems_filesystem_table_t *entry, void *arg) |
---|
79 | { |
---|
80 | find_arg *fa = arg; |
---|
81 | |
---|
82 | if ( strcmp( entry->type, fa->type ) != 0 ) { |
---|
83 | return false; |
---|
84 | } else { |
---|
85 | fa->mount_h = entry->mount_h; |
---|
86 | |
---|
87 | return true; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | rtems_filesystem_fsmount_me_t |
---|
92 | rtems_filesystem_get_mount_handler( |
---|
93 | const char *type |
---|
94 | ) |
---|
95 | { |
---|
96 | find_arg fa = { |
---|
97 | .type = type, |
---|
98 | .mount_h = NULL |
---|
99 | }; |
---|
100 | |
---|
101 | if ( type != NULL ) { |
---|
102 | rtems_filesystem_iterate( find_handler, &fa ); |
---|
103 | } |
---|
104 | |
---|
105 | return fa.mount_h; |
---|
106 | } |
---|
107 | |
---|
108 | int |
---|
109 | rtems_filesystem_register( |
---|
110 | const char *type, |
---|
111 | rtems_filesystem_fsmount_me_t mount_h |
---|
112 | ) |
---|
113 | { |
---|
114 | size_t fsn_size = sizeof( filesystem_node ) + strlen(type) + 1; |
---|
115 | filesystem_node *fsn = malloc( fsn_size ); |
---|
116 | char *type_storage = (char *) fsn + sizeof( filesystem_node ); |
---|
117 | |
---|
118 | if ( fsn == NULL ) |
---|
119 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
120 | |
---|
121 | strcpy(type_storage, type); |
---|
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( &filesystem_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_node *node = NULL; |
---|
145 | |
---|
146 | if ( type == NULL ) { |
---|
147 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
148 | } |
---|
149 | |
---|
150 | rtems_libio_lock(); |
---|
151 | for ( |
---|
152 | node = rtems_chain_first( &filesystem_chain ); |
---|
153 | !rtems_chain_is_tail( &filesystem_chain, node ); |
---|
154 | node = rtems_chain_next( node ) |
---|
155 | ) { |
---|
156 | filesystem_node *fsn = (filesystem_node *) node; |
---|
157 | |
---|
158 | if ( strcmp( fsn->entry.type, type ) == 0 ) { |
---|
159 | rtems_chain_extract( node ); |
---|
160 | free( fsn ); |
---|
161 | rtems_libio_unlock(); |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|
165 | } |
---|
166 | rtems_libio_unlock(); |
---|
167 | |
---|
168 | rtems_set_errno_and_return_minus_one( ENOENT ); |
---|
169 | } |
---|