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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aec2ad4 was 9d12059, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Pass arg1 directly to async_session_create(). arg1 will be used often, so make it easy to use. Also when arg1 is used as portid, (phone, portid) is a port reference, so the two fields belong together and could be potentially fused together in the future.

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