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 | |
---|
32 | #include <rtems/libio_.h> |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | rtems_chain_node node; |
---|
36 | rtems_filesystem_table_t entry; |
---|
37 | } filesystem_node; |
---|
38 | |
---|
39 | static RTEMS_CHAIN_DEFINE_EMPTY(filesystem_chain); |
---|
40 | |
---|
41 | bool rtems_filesystem_iterate( |
---|
42 | rtems_per_filesystem_routine routine, |
---|
43 | void *routine_arg |
---|
44 | ) |
---|
45 | { |
---|
46 | const rtems_filesystem_table_t *table_entry = &rtems_filesystem_table [0]; |
---|
47 | rtems_chain_node *node = NULL; |
---|
48 | bool stop = false; |
---|
49 | |
---|
50 | while ( table_entry->type && !stop ) { |
---|
51 | stop = (*routine)( table_entry, routine_arg ); |
---|
52 | ++table_entry; |
---|
53 | } |
---|
54 | |
---|
55 | if ( !stop ) { |
---|
56 | rtems_libio_lock(); |
---|
57 | for ( |
---|
58 | node = rtems_chain_first( &filesystem_chain ); |
---|
59 | !rtems_chain_is_tail( &filesystem_chain, node ) && !stop; |
---|
60 | node = rtems_chain_next( node ) |
---|
61 | ) { |
---|
62 | const filesystem_node *fsn = (filesystem_node *) node; |
---|
63 | |
---|
64 | stop = (*routine)( &fsn->entry, routine_arg ); |
---|
65 | } |
---|
66 | rtems_libio_unlock(); |
---|
67 | } |
---|
68 | |
---|
69 | return stop; |
---|
70 | } |
---|
71 | |
---|
72 | typedef struct { |
---|
73 | const char *type; |
---|
74 | rtems_filesystem_fsmount_me_t mount_h; |
---|
75 | } find_arg; |
---|
76 | |
---|
77 | static bool find_handler(const rtems_filesystem_table_t *entry, void *arg) |
---|
78 | { |
---|
79 | find_arg *fa = arg; |
---|
80 | |
---|
81 | if ( strcmp( entry->type, fa->type ) != 0 ) { |
---|
82 | return false; |
---|
83 | } else { |
---|
84 | fa->mount_h = entry->mount_h; |
---|
85 | |
---|
86 | return true; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | rtems_filesystem_fsmount_me_t |
---|
91 | rtems_filesystem_get_mount_handler( |
---|
92 | const char *type |
---|
93 | ) |
---|
94 | { |
---|
95 | find_arg fa = { |
---|
96 | .type = type, |
---|
97 | .mount_h = NULL |
---|
98 | }; |
---|
99 | |
---|
100 | if ( type != NULL ) { |
---|
101 | rtems_filesystem_iterate( find_handler, &fa ); |
---|
102 | } |
---|
103 | |
---|
104 | return fa.mount_h; |
---|
105 | } |
---|
106 | |
---|
107 | int |
---|
108 | rtems_filesystem_register( |
---|
109 | const char *type, |
---|
110 | rtems_filesystem_fsmount_me_t mount_h |
---|
111 | ) |
---|
112 | { |
---|
113 | size_t fsn_size = sizeof( filesystem_node ) + strlen(type) + 1; |
---|
114 | filesystem_node *fsn = malloc( fsn_size ); |
---|
115 | char *type_storage = (char *) fsn + sizeof( filesystem_node ); |
---|
116 | |
---|
117 | if ( fsn == NULL ) |
---|
118 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
119 | |
---|
120 | strcpy(type_storage, type); |
---|
121 | fsn->entry.type = type_storage; |
---|
122 | fsn->entry.mount_h = mount_h; |
---|
123 | |
---|
124 | rtems_libio_lock(); |
---|
125 | if ( rtems_filesystem_get_mount_handler( type ) == NULL ) { |
---|
126 | rtems_chain_append( &filesystem_chain, &fsn->node ); |
---|
127 | } else { |
---|
128 | rtems_libio_unlock(); |
---|
129 | free( fsn ); |
---|
130 | |
---|
131 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
132 | } |
---|
133 | rtems_libio_unlock(); |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | int |
---|
139 | rtems_filesystem_unregister( |
---|
140 | const char *type |
---|
141 | ) |
---|
142 | { |
---|
143 | rtems_chain_node *node = NULL; |
---|
144 | |
---|
145 | if ( type == NULL ) { |
---|
146 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
147 | } |
---|
148 | |
---|
149 | rtems_libio_lock(); |
---|
150 | for ( |
---|
151 | node = rtems_chain_first( &filesystem_chain ); |
---|
152 | !rtems_chain_is_tail( &filesystem_chain, node ); |
---|
153 | node = rtems_chain_next( node ) |
---|
154 | ) { |
---|
155 | filesystem_node *fsn = (filesystem_node *) node; |
---|
156 | |
---|
157 | if ( strcmp( fsn->entry.type, type ) == 0 ) { |
---|
158 | rtems_chain_extract( node ); |
---|
159 | free( fsn ); |
---|
160 | rtems_libio_unlock(); |
---|
161 | |
---|
162 | return 0; |
---|
163 | } |
---|
164 | } |
---|
165 | rtems_libio_unlock(); |
---|
166 | |
---|
167 | rtems_set_errno_and_return_minus_one( ENOENT ); |
---|
168 | } |
---|