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

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

'Exchange' is better than 'transaction', because it does not evoke ACID semantics (which we don't have).

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