source: mainline/uspace/srv/vfs/vfs_register.c@ cc76be3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc76be3 was cc76be3, checked in by Jakub Jermar <jakub@…>, 18 years ago

VFS work.
During VFS_REGISTER, use strncmp() instead of strcmp().
Add one excessive convenience byte to vfs_info.name to support the
above-mentioned change. In case the fs name spans all available
characters, make sure this convenience byte is zero.

  • Property mode set to 100644
File size: 11.3 KB
Line 
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
54atomic_t fs_head_futex = FUTEX_INITIALIZER;
55link_t fs_head;
56
57atomic_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 */
67static 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 * This check is not redundant. It ensures that the name is
93 * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
94 */
95 if (info->name[i] != '\0') {
96 dprintf("The name is not properly NULL-terminated.\n");
97 return false;
98 }
99
100
101 /*
102 * Check if the FS implements mandatory VFS operations.
103 */
104 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
105 dprintf("Operation VFS_REGISTER not defined by the client.\n");
106 return false;
107 }
108 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
109 dprintf("Operation VFS_MOUNT not defined by the client.\n");
110 return false;
111 }
112 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
113 dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
114 return false;
115 }
116 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
117 dprintf("Operation VFS_LOOKUP not defined by the client.\n");
118 return false;
119 }
120 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
121 dprintf("Operation VFS_OPEN not defined by the client.\n");
122 return false;
123 }
124 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
125 dprintf("Operation VFS_CLOSE not defined by the client.\n");
126 return false;
127 }
128 if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
129 dprintf("Operation VFS_READ not defined by the client.\n");
130 return false;
131 }
132
133 /*
134 * Check if each operation is either not defined, defined or default.
135 */
136 for (i = VFS_FIRST; i < VFS_LAST; i++) {
137 if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
138 (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
139 (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
140 dprintf("Operation info not understood.\n");
141 return false;
142 }
143 }
144 return true;
145}
146
147/** VFS_REGISTER protocol function.
148 *
149 * @param rid Hash of the call with the request.
150 * @param request Call structure with the request.
151 */
152void vfs_register(ipc_callid_t rid, ipc_call_t *request)
153{
154 ipc_callid_t callid;
155 ipc_call_t call;
156 int rc;
157 size_t size;
158
159 dprintf("Processing VFS_REGISTER request received from %p.\n",
160 request->in_phone_hash);
161
162 /*
163 * The first call has to be IPC_M_DATA_SEND in which we receive the
164 * VFS info structure from the client FS.
165 */
166 if (!ipc_data_receive(&callid, NULL, &size)) {
167 /*
168 * The client doesn't obey the same protocol as we do.
169 */
170 dprintf("Receiving of VFS info failed.\n");
171 ipc_answer_0(callid, EINVAL);
172 ipc_answer_0(rid, EINVAL);
173 return;
174 }
175
176 dprintf("VFS info received, size = %d\n", size);
177
178 /*
179 * We know the size of the VFS info structure. See if the client
180 * understands this easy concept too.
181 */
182 if (size != sizeof(vfs_info_t)) {
183 /*
184 * The client is sending us something, which cannot be
185 * the info structure.
186 */
187 dprintf("Received VFS info has bad size.\n");
188 ipc_answer_0(callid, EINVAL);
189 ipc_answer_0(rid, EINVAL);
190 return;
191 }
192
193 /*
194 * Allocate and initialize a buffer for the fs_info structure.
195 */
196 fs_info_t *fs_info;
197 fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
198 if (!fs_info) {
199 dprintf("Could not allocate memory for FS info.\n");
200 ipc_answer_0(callid, ENOMEM);
201 ipc_answer_0(rid, ENOMEM);
202 return;
203 }
204 link_initialize(&fs_info->fs_link);
205
206 rc = ipc_data_deliver(callid, &fs_info->vfs_info, size);
207 if (rc != EOK) {
208 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
209 rc);
210 free(fs_info);
211 ipc_answer_0(callid, rc);
212 ipc_answer_0(rid, rc);
213 return;
214 }
215
216 dprintf("VFS info delivered.\n");
217
218 if (!vfs_info_sane(&fs_info->vfs_info)) {
219 free(fs_info);
220 ipc_answer_0(callid, EINVAL);
221 ipc_answer_0(rid, EINVAL);
222 return;
223 }
224
225 futex_down(&fs_head_futex);
226
227 /*
228 * Check for duplicit registrations.
229 */
230 if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
231 /*
232 * We already register a fs like this.
233 */
234 dprintf("FS is already registered.\n");
235 futex_up(&fs_head_futex);
236 free(fs_info);
237 ipc_answer_0(callid, EEXISTS);
238 ipc_answer_0(rid, EEXISTS);
239 return;
240 }
241
242 /*
243 * Add fs_info to the list of registered FS's.
244 */
245 dprintf("Inserting FS into the list of registered file systems.\n");
246 list_append(&fs_info->fs_link, &fs_head);
247
248 /*
249 * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
250 * that a callback connection is created and we have a phone through
251 * which to forward VFS requests to it.
252 */
253 callid = async_get_call(&call);
254 if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
255 dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
256 list_remove(&fs_info->fs_link);
257 futex_up(&fs_head_futex);
258 free(fs_info);
259 ipc_answer_0(callid, EINVAL);
260 ipc_answer_0(rid, EINVAL);
261 return;
262 }
263 fs_info->phone = IPC_GET_ARG5(call);
264 ipc_answer_0(callid, EOK);
265
266 dprintf("Callback connection to FS created.\n");
267
268 /*
269 * The client will want us to send him the address space area with PLB.
270 */
271 callid = async_get_call(&call);
272 if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
273 dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
274 list_remove(&fs_info->fs_link);
275 futex_up(&fs_head_futex);
276 ipc_hangup(fs_info->phone);
277 free(fs_info);
278 ipc_answer_0(callid, EINVAL);
279 ipc_answer_0(rid, EINVAL);
280 return;
281 }
282
283 /*
284 * We can only send the client address space area PLB_SIZE bytes long.
285 */
286 size = IPC_GET_ARG2(call);
287 if (size != PLB_SIZE) {
288 dprintf("Client suggests wrong size of PFB, size = %d\n", size);
289 list_remove(&fs_info->fs_link);
290 futex_up(&fs_head_futex);
291 ipc_hangup(fs_info->phone);
292 free(fs_info);
293 ipc_answer_0(callid, EINVAL);
294 ipc_answer_0(rid, EINVAL);
295 return;
296 }
297
298 /*
299 * Commit to read-only sharing the PLB with the client.
300 */
301 ipc_answer_2(callid, EOK, (ipcarg_t) plb,
302 (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
303
304 dprintf("Sharing PLB.\n");
305
306 /*
307 * That was it. The FS has been registered.
308 * In reply to the VFS_REGISTER request, we assign the client file
309 * system a global file system handle.
310 */
311 fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
312 ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
313
314 futex_up(&fs_head_futex);
315
316 dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
317 FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
318}
319
320/** For a given file system handle, implement policy for allocating a phone.
321 *
322 * @param handle File system handle.
323 *
324 * @return Phone over which a multi-call request can be safely
325 * sent. Return 0 if no phone was found.
326 */
327int vfs_grab_phone(int handle)
328{
329 /*
330 * For now, we don't try to be very clever and very fast.
331 * We simply lookup the phone in the fs_head list. We currently don't
332 * open any additional phones (even though that itself would be pretty
333 * straightforward; housekeeping multiple open phones to a FS task would
334 * be more demanding). Instead, we simply take the respective
335 * phone_futex and keep it until vfs_release_phone().
336 */
337 futex_down(&fs_head_futex);
338 link_t *cur;
339 fs_info_t *fs;
340 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
341 fs = list_get_instance(cur, fs_info_t, fs_link);
342 if (fs->fs_handle == handle) {
343 futex_up(&fs_head_futex);
344 /*
345 * For now, take the futex unconditionally.
346 * Oh yeah, serialization rocks.
347 * It will be up'ed in vfs_release_phone().
348 */
349 futex_down(&fs->phone_futex);
350 /*
351 * Avoid deadlock with other fibrils in the same thread
352 * by disabling fibril preemption.
353 */
354 fibril_inc_sercount();
355 return fs->phone;
356 }
357 }
358 futex_up(&fs_head_futex);
359 return 0;
360}
361
362/** Tell VFS that the phone is in use for any request.
363 *
364 * @param phone Phone to FS task.
365 */
366void vfs_release_phone(int phone)
367{
368 bool found = false;
369
370 /*
371 * Undo the fibril_inc_sercount() done in vfs_grab_phone().
372 */
373 fibril_dec_sercount();
374
375 futex_down(&fs_head_futex);
376 link_t *cur;
377 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
378 fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
379 if (fs->phone == phone) {
380 found = true;
381 futex_up(&fs_head_futex);
382 futex_up(&fs->phone_futex);
383 return;
384 }
385 }
386 futex_up(&fs_head_futex);
387
388 /*
389 * Not good to get here.
390 */
391 assert(found == true);
392}
393
394/** Convert file system name to its handle.
395 *
396 * @param name File system name.
397 * @param lock If true, the function will down and up the
398 * fs_head_futex.
399 *
400 * @return File system handle or zero if file system not found.
401 */
402int fs_name_to_handle(char *name, bool lock)
403{
404 int handle = 0;
405
406 if (lock)
407 futex_down(&fs_head_futex);
408 link_t *cur;
409 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
410 fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
411 if (strncmp(fs->vfs_info.name, name,
412 sizeof(fs->vfs_info.name)) == 0) {
413 handle = fs->fs_handle;
414 break;
415 }
416 }
417 if (lock)
418 futex_up(&fs_head_futex);
419 return handle;
420}
421
422/**
423 * @}
424 */
Note: See TracBrowser for help on using the repository browser.