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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffa2c8ef was ffa2c8ef, checked in by Martin Decky <martin@…>, 14 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[62da45a]1/*
2 * Copyright (c) 2008 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 * @{
[8dc72b64]31 */
[62da45a]32
33/**
[8dc72b64]34 * @file vfs_register.c
[62da45a]35 * @brief
36 */
37
38#include <ipc/services.h>
39#include <async.h>
40#include <fibril.h>
[df908b3]41#include <fibril_synch.h>
[62da45a]42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
[19f857a]45#include <str.h>
[62da45a]46#include <ctype.h>
47#include <bool.h>
[d9c8c81]48#include <adt/list.h>
[62da45a]49#include <as.h>
50#include <assert.h>
51#include <atomic.h>
52#include "vfs.h"
53
[7b47fa2]54FIBRIL_CONDVAR_INITIALIZE(fs_head_cv);
[230260ac]55FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
[7b47fa2]56LIST_INITIALIZE(fs_head);
[62da45a]57
58atomic_t fs_handle_next = {
59 .count = 1
60};
61
62/** Verify the VFS info structure.
63 *
64 * @param info Info structure to be verified.
65 *
66 * @return Non-zero if the info structure is sane, zero otherwise.
67 */
68static bool vfs_info_sane(vfs_info_t *info)
69{
70 int i;
71
72 /*
73 * Check if the name is non-empty and is composed solely of ASCII
74 * characters [a-z]+[a-z0-9_-]*.
75 */
76 if (!islower(info->name[0])) {
77 dprintf("The name doesn't start with a lowercase character.\n");
78 return false;
79 }
80 for (i = 1; i < FS_NAME_MAXLEN; i++) {
81 if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
82 (info->name[i] != '-') && (info->name[i] != '_')) {
83 if (info->name[i] == '\0') {
84 break;
85 } else {
86 dprintf("The name contains illegal "
87 "characters.\n");
88 return false;
89 }
90 }
91 }
92 /*
93 * This check is not redundant. It ensures that the name is
94 * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
95 */
96 if (info->name[i] != '\0') {
97 dprintf("The name is not properly NULL-terminated.\n");
98 return false;
99 }
100
101 return true;
102}
103
104/** VFS_REGISTER protocol function.
105 *
106 * @param rid Hash of the call with the request.
107 * @param request Call structure with the request.
108 */
109void vfs_register(ipc_callid_t rid, ipc_call_t *request)
110{
[c1c0184]111 int phone;
112
[62da45a]113 dprintf("Processing VFS_REGISTER request received from %p.\n",
114 request->in_phone_hash);
115
[b4cbef1]116 vfs_info_t *vfs_info;
[eda925a]117 int rc = async_data_write_accept((void **) &vfs_info, false,
118 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
[62da45a]119
[b4cbef1]120 if (rc != EOK) {
121 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
122 rc);
[ffa2c8ef]123 async_answer_0(rid, rc);
[62da45a]124 return;
125 }
[b4cbef1]126
[62da45a]127 /*
128 * Allocate and initialize a buffer for the fs_info structure.
129 */
[b4cbef1]130 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
[62da45a]131 if (!fs_info) {
132 dprintf("Could not allocate memory for FS info.\n");
[ffa2c8ef]133 async_answer_0(rid, ENOMEM);
[62da45a]134 return;
135 }
[b4cbef1]136
[62da45a]137 link_initialize(&fs_info->fs_link);
[b4cbef1]138 fs_info->vfs_info = *vfs_info;
139 free(vfs_info);
140
[62da45a]141 dprintf("VFS info delivered.\n");
[b4cbef1]142
[62da45a]143 if (!vfs_info_sane(&fs_info->vfs_info)) {
144 free(fs_info);
[ffa2c8ef]145 async_answer_0(rid, EINVAL);
[62da45a]146 return;
147 }
[b4cbef1]148
[230260ac]149 fibril_mutex_lock(&fs_head_lock);
[b4cbef1]150
[62da45a]151 /*
152 * Check for duplicit registrations.
153 */
154 if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
155 /*
156 * We already register a fs like this.
157 */
158 dprintf("FS is already registered.\n");
[230260ac]159 fibril_mutex_unlock(&fs_head_lock);
[62da45a]160 free(fs_info);
[ffa2c8ef]161 async_answer_0(rid, EEXISTS);
[62da45a]162 return;
163 }
[b4cbef1]164
[62da45a]165 /*
166 * Add fs_info to the list of registered FS's.
167 */
168 dprintf("Inserting FS into the list of registered file systems.\n");
169 list_append(&fs_info->fs_link, &fs_head);
[07deef5]170
[62da45a]171 /*
172 * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
173 * that a callback connection is created and we have a phone through
174 * which to forward VFS requests to it.
175 */
[b4cbef1]176 ipc_call_t call;
177 ipc_callid_t callid = async_get_call(&call);
[228e490]178 if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
179 dprintf("Unexpected call, method = %d\n", IPC_GET_IMETHOD(call));
[62da45a]180 list_remove(&fs_info->fs_link);
[230260ac]181 fibril_mutex_unlock(&fs_head_lock);
[62da45a]182 free(fs_info);
[ffa2c8ef]183 async_answer_0(callid, EINVAL);
184 async_answer_0(rid, EINVAL);
[62da45a]185 return;
186 }
[c1c0184]187
188 phone = IPC_GET_ARG5(call);
[9d12059]189 async_session_create(&fs_info->session, phone, 0);
[ffa2c8ef]190 async_answer_0(callid, EOK);
[b4cbef1]191
[62da45a]192 dprintf("Callback connection to FS created.\n");
[b4cbef1]193
[62da45a]194 /*
195 * The client will want us to send him the address space area with PLB.
196 */
[b4cbef1]197
198 size_t size;
[0da4e41]199 if (!async_share_in_receive(&callid, &size)) {
[228e490]200 dprintf("Unexpected call, method = %d\n", IPC_GET_IMETHOD(call));
[62da45a]201 list_remove(&fs_info->fs_link);
[230260ac]202 fibril_mutex_unlock(&fs_head_lock);
[c1c0184]203 async_session_destroy(&fs_info->session);
[ffa2c8ef]204 async_hangup(phone);
[62da45a]205 free(fs_info);
[ffa2c8ef]206 async_answer_0(callid, EINVAL);
207 async_answer_0(rid, EINVAL);
[62da45a]208 return;
209 }
210
211 /*
212 * We can only send the client address space area PLB_SIZE bytes long.
213 */
214 if (size != PLB_SIZE) {
215 dprintf("Client suggests wrong size of PFB, size = %d\n", size);
216 list_remove(&fs_info->fs_link);
[230260ac]217 fibril_mutex_unlock(&fs_head_lock);
[c1c0184]218 async_session_destroy(&fs_info->session);
[ffa2c8ef]219 async_hangup(phone);
[62da45a]220 free(fs_info);
[ffa2c8ef]221 async_answer_0(callid, EINVAL);
222 async_answer_0(rid, EINVAL);
[62da45a]223 return;
224 }
[b4cbef1]225
[62da45a]226 /*
227 * Commit to read-only sharing the PLB with the client.
228 */
[0da4e41]229 (void) async_share_in_finalize(callid, plb,
[62da45a]230 AS_AREA_READ | AS_AREA_CACHEABLE);
[b4cbef1]231
[62da45a]232 dprintf("Sharing PLB.\n");
[b4cbef1]233
[62da45a]234 /*
235 * That was it. The FS has been registered.
236 * In reply to the VFS_REGISTER request, we assign the client file
237 * system a global file system handle.
238 */
[f2ec8c8]239 fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
[ffa2c8ef]240 async_answer_1(rid, EOK, (sysarg_t) fs_info->fs_handle);
[62da45a]241
[9593bc8]242 fibril_condvar_broadcast(&fs_head_cv);
[230260ac]243 fibril_mutex_unlock(&fs_head_lock);
[62da45a]244
245 dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
246 FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
247}
248
249/** For a given file system handle, implement policy for allocating a phone.
250 *
251 * @param handle File system handle.
252 *
253 * @return Phone over which a multi-call request can be safely
254 * sent. Return 0 if no phone was found.
255 */
[f2ec8c8]256int vfs_grab_phone(fs_handle_t handle)
[62da45a]257{
[df908b3]258 link_t *cur;
259 fs_info_t *fs;
[34ca870]260 int phone;
261
[62da45a]262 /*
[34ca870]263 * For now, we don't try to be very clever and very fast. We simply
264 * lookup the phone in the fs_head list and duplicate it. The duplicate
265 * phone will be returned to the client and the client will use it for
266 * communication. In the future, we should cache the connections so
267 * that they do not have to be reestablished over and over again.
[62da45a]268 */
[230260ac]269 fibril_mutex_lock(&fs_head_lock);
[62da45a]270 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
271 fs = list_get_instance(cur, fs_info_t, fs_link);
272 if (fs->fs_handle == handle) {
[230260ac]273 fibril_mutex_unlock(&fs_head_lock);
[d3a1ade3]274 phone = async_exchange_begin(&fs->session);
[34ca870]275
276 assert(phone > 0);
277 return phone;
[62da45a]278 }
279 }
[230260ac]280 fibril_mutex_unlock(&fs_head_lock);
[62da45a]281 return 0;
282}
283
[34ca870]284/** Tell VFS that the phone is not needed anymore.
[62da45a]285 *
286 * @param phone Phone to FS task.
287 */
[df908b3]288void vfs_release_phone(fs_handle_t handle, int phone)
[62da45a]289{
[df908b3]290 link_t *cur;
291 fs_info_t *fs;
292
293 fibril_mutex_lock(&fs_head_lock);
294 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
295 fs = list_get_instance(cur, fs_info_t, fs_link);
296 if (fs->fs_handle == handle) {
297 fibril_mutex_unlock(&fs_head_lock);
[d3a1ade3]298 async_exchange_end(&fs->session, phone);
[df908b3]299 return;
300 }
301 }
302 /* should not really get here */
303 abort();
304 fibril_mutex_unlock(&fs_head_lock);
[62da45a]305}
306
307/** Convert file system name to its handle.
308 *
309 * @param name File system name.
[230260ac]310 * @param lock If true, the function will lock and unlock the
311 * fs_head_lock.
[62da45a]312 *
313 * @return File system handle or zero if file system not found.
314 */
[f2ec8c8]315fs_handle_t fs_name_to_handle(char *name, bool lock)
[62da45a]316{
317 int handle = 0;
318
319 if (lock)
[230260ac]320 fibril_mutex_lock(&fs_head_lock);
[62da45a]321 link_t *cur;
322 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
323 fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
[47a6708]324 if (str_cmp(fs->vfs_info.name, name) == 0) {
[62da45a]325 handle = fs->fs_handle;
326 break;
327 }
328 }
329 if (lock)
[230260ac]330 fibril_mutex_unlock(&fs_head_lock);
[62da45a]331 return handle;
332}
333
[f07f6b2]334/** Find the VFS info structure.
335 *
336 * @param handle FS handle for which the VFS info structure is sought.
337 * @return VFS info structure on success or NULL otherwise.
338 */
339vfs_info_t *fs_handle_to_info(fs_handle_t handle)
340{
341 vfs_info_t *info = NULL;
342 link_t *cur;
343
344 fibril_mutex_lock(&fs_head_lock);
345 for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
346 fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
347 if (fs->fs_handle == handle) {
348 info = &fs->vfs_info;
349 break;
350 }
351 }
352 fibril_mutex_unlock(&fs_head_lock);
353
354 return info;
355}
356
[62da45a]357/**
358 * @}
359 */
Note: See TracBrowser for help on using the repository browser.