Rev 2523 | Rev 2527 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2523 | Rev 2526 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file vfs.c |
34 | * @file vfs.c |
35 | * @brief VFS multiplexer for HelenOS. |
35 | * @brief VFS multiplexer for HelenOS. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <ipc/services.h> |
39 | #include <ipc/services.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | #include <errno.h> |
41 | #include <errno.h> |
42 | #include <stdlib.h> |
42 | #include <stdlib.h> |
- | 43 | #include <string.h> |
|
- | 44 | #include <ctype.h> |
|
43 | #include <bool.h> |
45 | #include <bool.h> |
- | 46 | #include <futex.h> |
|
- | 47 | #include <libadt/list.h> |
|
44 | #include "vfs.h" |
48 | #include "vfs.h" |
45 | 49 | ||
- | 50 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
|
- | 51 | link_t fs_head; |
|
- | 52 | ||
46 | /** Verify the VFS info structure. |
53 | /** Verify the VFS info structure. |
47 | * |
54 | * |
48 | * @param info Info structure to be verified. |
55 | * @param info Info structure to be verified. |
49 | * |
56 | * |
50 | * @return Non-zero if the info structure is sane, zero otherwise. |
57 | * @return Non-zero if the info structure is sane, zero otherwise. |
51 | */ |
58 | */ |
52 | static int vfs_info_sane(vfs_info_t *info) |
59 | static bool vfs_info_sane(vfs_info_t *info) |
53 | { |
60 | { |
- | 61 | int i; |
|
- | 62 | ||
- | 63 | /* |
|
- | 64 | * Check if the name is non-empty and is composed solely of ASCII |
|
- | 65 | * characters [a-z]+[a-z0-9_-]*. |
|
- | 66 | */ |
|
- | 67 | if (!islower(info->name[0])) |
|
- | 68 | return false; |
|
- | 69 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
|
- | 70 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
|
- | 71 | (info->name[i] != '-') && (info->name[i] != '_')) { |
|
- | 72 | if (info->name[i] == '\0') |
|
- | 73 | break; |
|
- | 74 | else |
|
- | 75 | return false; |
|
- | 76 | } |
|
- | 77 | } |
|
- | 78 | ||
- | 79 | ||
- | 80 | /* |
|
- | 81 | * Check if the FS implements mandatory VFS operations. |
|
- | 82 | */ |
|
- | 83 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) |
|
- | 84 | return false; |
|
- | 85 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) |
|
- | 86 | return false; |
|
- | 87 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) |
|
- | 88 | return false; |
|
- | 89 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) |
|
- | 90 | return false; |
|
- | 91 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) |
|
- | 92 | return false; |
|
- | 93 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) |
|
- | 94 | return false; |
|
- | 95 | ||
- | 96 | /* |
|
- | 97 | * Check if each operation is either not defined, defined or default. |
|
- | 98 | */ |
|
- | 99 | for (i = VFS_FIRST; i < VFS_LAST; i++) { |
|
- | 100 | if ((IPC_METHOD_TO_VFS_OP(i) != VFS_OP_NULL) && |
|
- | 101 | (IPC_METHOD_TO_VFS_OP(i) != VFS_OP_DEFAULT) && |
|
- | 102 | (IPC_METHOD_TO_VFS_OP(i) != VFS_OP_DEFINED)) |
|
- | 103 | return false; |
|
- | 104 | } |
|
54 | return 1; /* XXX */ |
105 | return true; |
55 | } |
106 | } |
56 | 107 | ||
57 | /** VFS_REGISTER protocol function. |
108 | /** VFS_REGISTER protocol function. |
58 | * |
109 | * |
59 | * @param rid Hash of the call with the request. |
110 | * @param rid Hash of the call with the request. |
60 | * @param request Call structure with the request. |
111 | * @param request Call structure with the request. |
61 | */ |
112 | */ |
62 | static void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
113 | static void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
63 | { |
114 | { |
64 | ipc_callid_t callid; |
115 | ipc_callid_t callid; |
65 | ipc_call_t call; |
116 | ipc_call_t call; |
66 | int rc; |
117 | int rc; |
67 | size_t size; |
118 | size_t size; |
68 | 119 | ||
69 | /* |
120 | /* |
70 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
121 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
71 | * VFS info structure from the client FS. |
122 | * VFS info structure from the client FS. |
72 | */ |
123 | */ |
73 | if (!ipc_data_send_accept(&callid, &call, NULL, &size)) { |
124 | if (!ipc_data_send_accept(&callid, &call, NULL, &size)) { |
74 | /* |
125 | /* |
75 | * The client doesn't obey the same protocol as we do. |
126 | * The client doesn't obey the same protocol as we do. |
76 | */ |
127 | */ |
77 | ipc_answer_fast(callid, EINVAL, 0, 0); |
128 | ipc_answer_fast(callid, EINVAL, 0, 0); |
78 | ipc_answer_fast(rid, EINVAL, 0, 0); |
129 | ipc_answer_fast(rid, EINVAL, 0, 0); |
79 | return; |
130 | return; |
80 | } |
131 | } |
81 | 132 | ||
82 | /* |
133 | /* |
83 | * We know the size of the info structure. See if the client understands |
134 | * We know the size of the VFS info structure. See if the client |
84 | * this easy concept too. |
135 | * understands this easy concept too. |
85 | */ |
136 | */ |
86 | if (size != sizeof(vfs_info_t)) { |
137 | if (size != sizeof(vfs_info_t)) { |
87 | /* |
138 | /* |
88 | * The client is sending us something, which cannot be |
139 | * The client is sending us something, which cannot be |
89 | * the info structure. |
140 | * the info structure. |
90 | */ |
141 | */ |
91 | ipc_answer_fast(callid, EINVAL, 0, 0); |
142 | ipc_answer_fast(callid, EINVAL, 0, 0); |
92 | ipc_answer_fast(rid, EINVAL, 0, 0); |
143 | ipc_answer_fast(rid, EINVAL, 0, 0); |
93 | return; |
144 | return; |
94 | } |
145 | } |
95 | vfs_info_t *info; |
146 | fs_info_t *fs_info; |
96 | 147 | ||
97 | /* |
148 | /* |
98 | * Allocate a buffer for the info structure. |
149 | * Allocate and initialize a buffer for the fs_info structure. |
99 | */ |
150 | */ |
100 | info = (vfs_info_t *) malloc(sizeof(vfs_info_t)); |
151 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
101 | if (!info) { |
152 | if (!fs_info) { |
102 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
153 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
103 | ipc_answer_fast(rid, ENOMEM, 0, 0); |
154 | ipc_answer_fast(rid, ENOMEM, 0, 0); |
104 | return; |
155 | return; |
105 | } |
156 | } |
- | 157 | link_initialize(&fs_info->fs_link); |
|
106 | 158 | ||
107 | rc = ipc_data_send_answer(callid, &call, info, size); |
159 | rc = ipc_data_send_answer(callid, &call, &fs_info->vfs_info, size); |
108 | if (!rc) { |
160 | if (!rc) { |
109 | free(info); |
161 | free(fs_info); |
110 | ipc_answer_fast(callid, rc, 0, 0); |
162 | ipc_answer_fast(callid, rc, 0, 0); |
111 | ipc_answer_fast(rid, rc, 0, 0); |
163 | ipc_answer_fast(rid, rc, 0, 0); |
112 | return; |
164 | return; |
113 | } |
165 | } |
114 | 166 | ||
115 | if (!vfs_info_sane(info)) { |
167 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
116 | free(info); |
168 | free(fs_info); |
117 | ipc_answer_fast(callid, EINVAL, 0, 0); |
169 | ipc_answer_fast(callid, EINVAL, 0, 0); |
118 | ipc_answer_fast(rid, EINVAL, 0, 0); |
170 | ipc_answer_fast(rid, EINVAL, 0, 0); |
119 | return; |
171 | return; |
120 | } |
172 | } |
121 | 173 | ||
- | 174 | futex_down(&fs_head_futex); |
|
- | 175 | ||
- | 176 | /* |
|
- | 177 | * Check for duplicit registrations. |
|
- | 178 | */ |
|
- | 179 | link_t *cur; |
|
- | 180 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
|
- | 181 | fs_info_t *fi = list_get_instance(cur, fs_info_t, |
|
- | 182 | fs_link); |
|
- | 183 | /* TODO: replace strcmp with strncmp once we have it */ |
|
- | 184 | if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) { |
|
- | 185 | /* |
|
- | 186 | * We already register a fs like this. |
|
- | 187 | */ |
|
- | 188 | futex_up(&fs_head_futex); |
|
- | 189 | free(fs_info); |
|
- | 190 | ipc_answer_fast(callid, EEXISTS, 0, 0); |
|
- | 191 | ipc_answer_fast(rid, EEXISTS, 0, 0); |
|
- | 192 | return; |
|
- | 193 | } |
|
- | 194 | } |
|
- | 195 | ||
- | 196 | /* |
|
- | 197 | * TODO: |
|
- | 198 | * 1. send the client the IPC_M_CONNECT_TO_ME call so that it makes a |
|
- | 199 | * callback connection. |
|
- | 200 | * 2. add the fs_info into fs_head |
|
- | 201 | */ |
|
- | 202 | ||
- | 203 | futex_up(&fs_head_futex); |
|
122 | } |
204 | } |
123 | 205 | ||
124 | static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall) |
206 | static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall) |
125 | { |
207 | { |
126 | bool keep_on_going = 1; |
208 | bool keep_on_going = 1; |
127 | 209 | ||
128 | /* |
210 | /* |
129 | * The connection was opened via the IPC_CONNECT_ME_TO call. |
211 | * The connection was opened via the IPC_CONNECT_ME_TO call. |
130 | * This call needs to be answered. |
212 | * This call needs to be answered. |
131 | */ |
213 | */ |
132 | ipc_answer_fast(iid, EOK, 0, 0); |
214 | ipc_answer_fast(iid, EOK, 0, 0); |
133 | 215 | ||
134 | /* |
216 | /* |
135 | * Here we enter the main connection fibril loop. |
217 | * Here we enter the main connection fibril loop. |
136 | * The logic behind this loop and the protocol is that we'd like to keep |
218 | * The logic behind this loop and the protocol is that we'd like to keep |
137 | * each connection open for a while before we close it. The benefit of |
219 | * each connection open for a while before we close it. The benefit of |
138 | * this is that the client doesn't have to establish a new connection |
220 | * this is that the client doesn't have to establish a new connection |
139 | * upon each request. On the other hand, the client must be ready to |
221 | * upon each request. On the other hand, the client must be ready to |
140 | * re-establish a connection if we hang it up due to reaching of maximum |
222 | * re-establish a connection if we hang it up due to reaching of maximum |
141 | * number of requests per connection or due to the client timing out. |
223 | * number of requests per connection or due to the client timing out. |
142 | */ |
224 | */ |
143 | 225 | ||
144 | while (keep_on_going) { |
226 | while (keep_on_going) { |
145 | ipc_callid_t callid; |
227 | ipc_callid_t callid; |
146 | ipc_call_t call; |
228 | ipc_call_t call; |
147 | 229 | ||
148 | callid = async_get_call(&call); |
230 | callid = async_get_call(&call); |
149 | 231 | ||
150 | switch (IPC_GET_METHOD(call)) { |
232 | switch (IPC_GET_METHOD(call)) { |
151 | case IPC_M_PHONE_HUNGUP: |
233 | case IPC_M_PHONE_HUNGUP: |
152 | keep_on_going = false; |
234 | keep_on_going = false; |
153 | break; |
235 | break; |
154 | case VFS_REGISTER: |
236 | case VFS_REGISTER: |
155 | vfs_register(callid, &call); |
237 | vfs_register(callid, &call); |
156 | keep_on_going = false; |
238 | keep_on_going = false; |
157 | break; |
239 | break; |
158 | case VFS_MOUNT: |
240 | case VFS_MOUNT: |
159 | case VFS_UNMOUNT: |
241 | case VFS_UNMOUNT: |
160 | case VFS_OPEN: |
242 | case VFS_OPEN: |
161 | case VFS_CREATE: |
243 | case VFS_CREATE: |
162 | case VFS_CLOSE: |
244 | case VFS_CLOSE: |
163 | case VFS_READ: |
245 | case VFS_READ: |
164 | case VFS_WRITE: |
246 | case VFS_WRITE: |
165 | case VFS_SEEK: |
247 | case VFS_SEEK: |
166 | default: |
248 | default: |
167 | ipc_answer_fast(callid, ENOTSUP, 0, 0); |
249 | ipc_answer_fast(callid, ENOTSUP, 0, 0); |
168 | break; |
250 | break; |
169 | } |
251 | } |
170 | } |
252 | } |
171 | 253 | ||
172 | /* TODO: cleanup after the client */ |
254 | /* TODO: cleanup after the client */ |
173 | 255 | ||
174 | } |
256 | } |
175 | 257 | ||
176 | int main(int argc, char **argv) |
258 | int main(int argc, char **argv) |
177 | { |
259 | { |
178 | ipcarg_t phonead; |
260 | ipcarg_t phonead; |
179 | 261 | ||
- | 262 | list_initialize(&fs_head); |
|
180 | async_set_client_connection(vfs_connection); |
263 | async_set_client_connection(vfs_connection); |
181 | ipc_connect_to_me(PHONE_NS, SERVICE_VFS, 0, &phonead); |
264 | ipc_connect_to_me(PHONE_NS, SERVICE_VFS, 0, &phonead); |
182 | async_manager(); |
265 | async_manager(); |
183 | return 0; |
266 | return 0; |
184 | } |
267 | } |
185 | 268 | ||
186 | /** |
269 | /** |
187 | * @} |
270 | * @} |
188 | */ |
271 | */ |
189 | 272 |