1 | /*
|
---|
2 | * Copyright (c) 2007 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file vfs_register.c
|
---|
35 | * @brief VFS_REGISTER method.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <ipc/ipc.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <fibril.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include <ctype.h>
|
---|
47 | #include <bool.h>
|
---|
48 | #include <futex.h>
|
---|
49 | #include <as.h>
|
---|
50 | #include <libadt/list.h>
|
---|
51 | #include <assert.h>
|
---|
52 | #include "vfs.h"
|
---|
53 |
|
---|
54 | atomic_t fs_head_futex = FUTEX_INITIALIZER;
|
---|
55 | link_t fs_head;
|
---|
56 |
|
---|
57 | atomic_t fs_handle_next = {
|
---|
58 | .count = 1
|
---|
59 | };
|
---|
60 |
|
---|
61 | /** Verify the VFS info structure.
|
---|
62 | *
|
---|
63 | * @param info Info structure to be verified.
|
---|
64 | *
|
---|
65 | * @return Non-zero if the info structure is sane, zero otherwise.
|
---|
66 | */
|
---|
67 | static bool vfs_info_sane(vfs_info_t *info)
|
---|
68 | {
|
---|
69 | int i;
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Check if the name is non-empty and is composed solely of ASCII
|
---|
73 | * characters [a-z]+[a-z0-9_-]*.
|
---|
74 | */
|
---|
75 | if (!islower(info->name[0])) {
|
---|
76 | dprintf("The name doesn't start with a lowercase character.\n");
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | for (i = 1; i < FS_NAME_MAXLEN; i++) {
|
---|
80 | if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
|
---|
81 | (info->name[i] != '-') && (info->name[i] != '_')) {
|
---|
82 | if (info->name[i] == '\0') {
|
---|
83 | break;
|
---|
84 | } else {
|
---|
85 | dprintf("The name contains illegal "
|
---|
86 | "characters.\n");
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Check if the FS implements mandatory VFS operations.
|
---|
95 | */
|
---|
96 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
|
---|
97 | dprintf("Operation VFS_REGISTER not defined by the client.\n");
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
|
---|
101 | dprintf("Operation VFS_MOUNT not defined by the client.\n");
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
|
---|
105 | dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
|
---|
109 | dprintf("Operation VFS_LOOKUP not defined by the client.\n");
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
|
---|
113 | dprintf("Operation VFS_OPEN not defined by the client.\n");
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
|
---|
117 | dprintf("Operation VFS_CLOSE not defined by the client.\n");
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
|
---|
121 | dprintf("Operation VFS_READ not defined by the client.\n");
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Check if each operation is either not defined, defined or default.
|
---|
127 | */
|
---|
128 | for (i = VFS_FIRST; i < VFS_LAST; i++) {
|
---|
129 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
|
---|
130 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
|
---|
131 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
|
---|
132 | dprintf("Operation info not understood.\n");
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** VFS_REGISTER protocol function.
|
---|
140 | *
|
---|
141 | * @param rid Hash of the call with the request.
|
---|
142 | * @param request Call structure with the request.
|
---|
143 | */
|
---|
144 | void vfs_register(ipc_callid_t rid, ipc_call_t *request)
|
---|
145 | {
|
---|
146 | ipc_callid_t callid;
|
---|
147 | ipc_call_t call;
|
---|
148 | int rc;
|
---|
149 | size_t size;
|
---|
150 |
|
---|
151 | dprintf("Processing VFS_REGISTER request received from %p.\n",
|
---|
152 | request->in_phone_hash);
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * The first call has to be IPC_M_DATA_SEND in which we receive the
|
---|
156 | * VFS info structure from the client FS.
|
---|
157 | */
|
---|
158 | if (!ipc_data_receive(&callid, NULL, &size)) {
|
---|
159 | /*
|
---|
160 | * The client doesn't obey the same protocol as we do.
|
---|
161 | */
|
---|
162 | dprintf("Receiving of VFS info failed.\n");
|
---|
163 | ipc_answer_0(callid, EINVAL);
|
---|
164 | ipc_answer_0(rid, EINVAL);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | dprintf("VFS info received, size = %d\n", size);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * We know the size of the VFS info structure. See if the client
|
---|
172 | * understands this easy concept too.
|
---|
173 | */
|
---|
174 | if (size != sizeof(vfs_info_t)) {
|
---|
175 | /*
|
---|
176 | * The client is sending us something, which cannot be
|
---|
177 | * the info structure.
|
---|
178 | */
|
---|
179 | dprintf("Received VFS info has bad size.\n");
|
---|
180 | ipc_answer_0(callid, EINVAL);
|
---|
181 | ipc_answer_0(rid, EINVAL);
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Allocate and initialize a buffer for the fs_info structure.
|
---|
187 | */
|
---|
188 | fs_info_t *fs_info;
|
---|
189 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
|
---|
190 | if (!fs_info) {
|
---|
191 | dprintf("Could not allocate memory for FS info.\n");
|
---|
192 | ipc_answer_0(callid, ENOMEM);
|
---|
193 | ipc_answer_0(rid, ENOMEM);
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | link_initialize(&fs_info->fs_link);
|
---|
197 |
|
---|
198 | rc = ipc_data_deliver(callid, &fs_info->vfs_info, size);
|
---|
199 | if (rc != EOK) {
|
---|
200 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
|
---|
201 | rc);
|
---|
202 | free(fs_info);
|
---|
203 | ipc_answer_0(callid, rc);
|
---|
204 | ipc_answer_0(rid, rc);
|
---|
205 | return;
|
---|
206 | }
|
---|
207 |
|
---|
208 | dprintf("VFS info delivered.\n");
|
---|
209 |
|
---|
210 | if (!vfs_info_sane(&fs_info->vfs_info)) {
|
---|
211 | free(fs_info);
|
---|
212 | ipc_answer_0(callid, EINVAL);
|
---|
213 | ipc_answer_0(rid, EINVAL);
|
---|
214 | return;
|
---|
215 | }
|
---|
216 |
|
---|
217 | futex_down(&fs_head_futex);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Check for duplicit registrations.
|
---|
221 | */
|
---|
222 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
|
---|
223 | /*
|
---|
224 | * We already register a fs like this.
|
---|
225 | */
|
---|
226 | dprintf("FS is already registered.\n");
|
---|
227 | futex_up(&fs_head_futex);
|
---|
228 | free(fs_info);
|
---|
229 | ipc_answer_0(callid, EEXISTS);
|
---|
230 | ipc_answer_0(rid, EEXISTS);
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Add fs_info to the list of registered FS's.
|
---|
236 | */
|
---|
237 | dprintf("Inserting FS into the list of registered file systems.\n");
|
---|
238 | list_append(&fs_info->fs_link, &fs_head);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
|
---|
242 | * that a callback connection is created and we have a phone through
|
---|
243 | * which to forward VFS requests to it.
|
---|
244 | */
|
---|
245 | callid = async_get_call(&call);
|
---|
246 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
247 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
248 | list_remove(&fs_info->fs_link);
|
---|
249 | futex_up(&fs_head_futex);
|
---|
250 | free(fs_info);
|
---|
251 | ipc_answer_0(callid, EINVAL);
|
---|
252 | ipc_answer_0(rid, EINVAL);
|
---|
253 | return;
|
---|
254 | }
|
---|
255 | fs_info->phone = IPC_GET_ARG5(call);
|
---|
256 | ipc_answer_0(callid, EOK);
|
---|
257 |
|
---|
258 | dprintf("Callback connection to FS created.\n");
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * The client will want us to send him the address space area with PLB.
|
---|
262 | */
|
---|
263 | callid = async_get_call(&call);
|
---|
264 | if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
|
---|
265 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
266 | list_remove(&fs_info->fs_link);
|
---|
267 | futex_up(&fs_head_futex);
|
---|
268 | ipc_hangup(fs_info->phone);
|
---|
269 | free(fs_info);
|
---|
270 | ipc_answer_0(callid, EINVAL);
|
---|
271 | ipc_answer_0(rid, EINVAL);
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * We can only send the client address space area PLB_SIZE bytes long.
|
---|
277 | */
|
---|
278 | size = IPC_GET_ARG2(call);
|
---|
279 | if (size != PLB_SIZE) {
|
---|
280 | dprintf("Client suggests wrong size of PFB, size = %d\n", size);
|
---|
281 | list_remove(&fs_info->fs_link);
|
---|
282 | futex_up(&fs_head_futex);
|
---|
283 | ipc_hangup(fs_info->phone);
|
---|
284 | free(fs_info);
|
---|
285 | ipc_answer_0(callid, EINVAL);
|
---|
286 | ipc_answer_0(rid, EINVAL);
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Commit to read-only sharing the PLB with the client.
|
---|
292 | */
|
---|
293 | ipc_answer_2(callid, EOK, (ipcarg_t) plb,
|
---|
294 | (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
|
---|
295 |
|
---|
296 | dprintf("Sharing PLB.\n");
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * That was it. The FS has been registered.
|
---|
300 | * In reply to the VFS_REGISTER request, we assign the client file
|
---|
301 | * system a global file system handle.
|
---|
302 | */
|
---|
303 | fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
|
---|
304 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
|
---|
305 |
|
---|
306 | futex_up(&fs_head_futex);
|
---|
307 |
|
---|
308 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
|
---|
309 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** For a given file system handle, implement policy for allocating a phone.
|
---|
313 | *
|
---|
314 | * @param handle File system handle.
|
---|
315 | *
|
---|
316 | * @return Phone over which a multi-call request can be safely
|
---|
317 | * sent. Return 0 if no phone was found.
|
---|
318 | */
|
---|
319 | int vfs_grab_phone(int handle)
|
---|
320 | {
|
---|
321 | /*
|
---|
322 | * For now, we don't try to be very clever and very fast.
|
---|
323 | * We simply lookup the phone in the fs_head list. We currently don't
|
---|
324 | * open any additional phones (even though that itself would be pretty
|
---|
325 | * straightforward; housekeeping multiple open phones to a FS task would
|
---|
326 | * be more demanding). Instead, we simply take the respective
|
---|
327 | * phone_futex and keep it until vfs_release_phone().
|
---|
328 | */
|
---|
329 | futex_down(&fs_head_futex);
|
---|
330 | link_t *cur;
|
---|
331 | fs_info_t *fs;
|
---|
332 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
333 | fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
334 | if (fs->fs_handle == handle) {
|
---|
335 | futex_up(&fs_head_futex);
|
---|
336 | /*
|
---|
337 | * For now, take the futex unconditionally.
|
---|
338 | * Oh yeah, serialization rocks.
|
---|
339 | * It will be up'ed in vfs_release_phone().
|
---|
340 | */
|
---|
341 | futex_down(&fs->phone_futex);
|
---|
342 | /*
|
---|
343 | * Avoid deadlock with other fibrils in the same thread
|
---|
344 | * by disabling fibril preemption.
|
---|
345 | */
|
---|
346 | fibril_inc_sercount();
|
---|
347 | return fs->phone;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | futex_up(&fs_head_futex);
|
---|
351 | return 0;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /** Tell VFS that the phone is in use for any request.
|
---|
355 | *
|
---|
356 | * @param phone Phone to FS task.
|
---|
357 | */
|
---|
358 | void vfs_release_phone(int phone)
|
---|
359 | {
|
---|
360 | bool found = false;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Undo the fibril_inc_sercount() done in vfs_grab_phone().
|
---|
364 | */
|
---|
365 | fibril_dec_sercount();
|
---|
366 |
|
---|
367 | futex_down(&fs_head_futex);
|
---|
368 | link_t *cur;
|
---|
369 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
370 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
371 | if (fs->phone == phone) {
|
---|
372 | found = true;
|
---|
373 | futex_up(&fs_head_futex);
|
---|
374 | futex_up(&fs->phone_futex);
|
---|
375 | return;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | futex_up(&fs_head_futex);
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Not good to get here.
|
---|
382 | */
|
---|
383 | assert(found == true);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /** Convert file system name to its handle.
|
---|
387 | *
|
---|
388 | * @param name File system name.
|
---|
389 | * @param lock If true, the function will down and up the
|
---|
390 | * fs_head_futex.
|
---|
391 | *
|
---|
392 | * @return File system handle or zero if file system not found.
|
---|
393 | */
|
---|
394 | int fs_name_to_handle(char *name, bool lock)
|
---|
395 | {
|
---|
396 | int handle = 0;
|
---|
397 |
|
---|
398 | if (lock)
|
---|
399 | futex_down(&fs_head_futex);
|
---|
400 | link_t *cur;
|
---|
401 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
402 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
403 | if (strcmp(fs->vfs_info.name, name) == 0) { /* XXX: strncmp() */
|
---|
404 | handle = fs->fs_handle;
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | if (lock)
|
---|
409 | futex_up(&fs_head_futex);
|
---|
410 | return handle;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * @}
|
---|
415 | */
|
---|