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
Line 
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 * @{
31 */
32
33/**
34 * @file vfs_register.c
35 * @brief
36 */
37
38#include <ipc/services.h>
39#include <async.h>
40#include <fibril.h>
41#include <fibril_synch.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <str.h>
46#include <ctype.h>
47#include <bool.h>
48#include <adt/list.h>
49#include <as.h>
50#include <assert.h>
51#include <atomic.h>
52#include "vfs.h"
53
54FIBRIL_CONDVAR_INITIALIZE(fs_head_cv);
55FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
56LIST_INITIALIZE(fs_head);
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{
111 int phone;
112
113 dprintf("Processing VFS_REGISTER request received from %p.\n",
114 request->in_phone_hash);
115
116 vfs_info_t *vfs_info;
117 int rc = async_data_write_accept((void **) &vfs_info, false,
118 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
119
120 if (rc != EOK) {
121 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
122 rc);
123 async_answer_0(rid, rc);
124 return;
125 }
126
127 /*
128 * Allocate and initialize a buffer for the fs_info structure.
129 */
130 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
131 if (!fs_info) {
132 dprintf("Could not allocate memory for FS info.\n");
133 async_answer_0(rid, ENOMEM);
134 return;
135 }
136
137 link_initialize(&fs_info->fs_link);
138 fs_info->vfs_info = *vfs_info;
139 free(vfs_info);
140
141 dprintf("VFS info delivered.\n");
142
143 if (!vfs_info_sane(&fs_info->vfs_info)) {
144 free(fs_info);
145 async_answer_0(rid, EINVAL);
146 return;
147 }
148
149 fibril_mutex_lock(&fs_head_lock);
150
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");
159 fibril_mutex_unlock(&fs_head_lock);
160 free(fs_info);
161 async_answer_0(rid, EEXISTS);
162 return;
163 }
164
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);
170
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 */
176 ipc_call_t call;
177 ipc_callid_t callid = async_get_call(&call);
178 if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
179 dprintf("Unexpected call, method = %d\n", IPC_GET_IMETHOD(call));
180 list_remove(&fs_info->fs_link);
181 fibril_mutex_unlock(&fs_head_lock);
182 free(fs_info);
183 async_answer_0(callid, EINVAL);
184 async_answer_0(rid, EINVAL);
185 return;
186 }
187
188 phone = IPC_GET_ARG5(call);
189 async_session_create(&fs_info->session, phone, 0);
190 async_answer_0(callid, EOK);
191
192 dprintf("Callback connection to FS created.\n");
193
194 /*
195 * The client will want us to send him the address space area with PLB.
196 */
197
198 size_t size;
199 if (!async_share_in_receive(&callid, &size)) {
200 dprintf("Unexpected call, method = %d\n", IPC_GET_IMETHOD(call));
201 list_remove(&fs_info->fs_link);
202 fibril_mutex_unlock(&fs_head_lock);
203 async_session_destroy(&fs_info->session);
204 async_hangup(phone);
205 free(fs_info);
206 async_answer_0(callid, EINVAL);
207 async_answer_0(rid, EINVAL);
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);
217 fibril_mutex_unlock(&fs_head_lock);
218 async_session_destroy(&fs_info->session);
219 async_hangup(phone);
220 free(fs_info);
221 async_answer_0(callid, EINVAL);
222 async_answer_0(rid, EINVAL);
223 return;
224 }
225
226 /*
227 * Commit to read-only sharing the PLB with the client.
228 */
229 (void) async_share_in_finalize(callid, plb,
230 AS_AREA_READ | AS_AREA_CACHEABLE);
231
232 dprintf("Sharing PLB.\n");
233
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 */
239 fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
240 async_answer_1(rid, EOK, (sysarg_t) fs_info->fs_handle);
241
242 fibril_condvar_broadcast(&fs_head_cv);
243 fibril_mutex_unlock(&fs_head_lock);
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 */
256int vfs_grab_phone(fs_handle_t handle)
257{
258 link_t *cur;
259 fs_info_t *fs;
260 int phone;
261
262 /*
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.
268 */
269 fibril_mutex_lock(&fs_head_lock);
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) {
273 fibril_mutex_unlock(&fs_head_lock);
274 phone = async_exchange_begin(&fs->session);
275
276 assert(phone > 0);
277 return phone;
278 }
279 }
280 fibril_mutex_unlock(&fs_head_lock);
281 return 0;
282}
283
284/** Tell VFS that the phone is not needed anymore.
285 *
286 * @param phone Phone to FS task.
287 */
288void vfs_release_phone(fs_handle_t handle, int phone)
289{
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);
298 async_exchange_end(&fs->session, phone);
299 return;
300 }
301 }
302 /* should not really get here */
303 abort();
304 fibril_mutex_unlock(&fs_head_lock);
305}
306
307/** Convert file system name to its handle.
308 *
309 * @param name File system name.
310 * @param lock If true, the function will lock and unlock the
311 * fs_head_lock.
312 *
313 * @return File system handle or zero if file system not found.
314 */
315fs_handle_t fs_name_to_handle(char *name, bool lock)
316{
317 int handle = 0;
318
319 if (lock)
320 fibril_mutex_lock(&fs_head_lock);
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);
324 if (str_cmp(fs->vfs_info.name, name) == 0) {
325 handle = fs->fs_handle;
326 break;
327 }
328 }
329 if (lock)
330 fibril_mutex_unlock(&fs_head_lock);
331 return handle;
332}
333
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
357/**
358 * @}
359 */
Note: See TracBrowser for help on using the repository browser.