source: mainline/uspace/srv/vfs/vfs_register.c@ 63a3276

Last change on this file since 63a3276 was 63a3276, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Instrumented locsrv for autostart

  • also refactored unit name derivation in other brokers
  • exposee creation is not used in unit's lifecycle (failed assertion)

Conflicts:

uspace/lib/c/generic/loc.c
uspace/srv/devman/driver.c
uspace/srv/devman/drv_conn.c
uspace/srv/hid/compositor/compositor.c
uspace/srv/locsrv/locsrv.c
uspace/srv/vfs/vfs.h
uspace/srv/vfs/vfs_ops.c
uspace/srv/vfs/vfs_register.c

  • Property mode set to 100644
File size: 10.5 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 vfs
30 * @{
31 */
32
33/**
34 * @file vfs_register.c
35 * @brief
36 */
37
38#include <adt/list.h>
39#include <as.h>
40#include <assert.h>
41#include <async.h>
42#include <atomic.h>
43#include <ctype.h>
44#include <errno.h>
45#include <fibril.h>
46#include <fibril_synch.h>
47#include <ipc/services.h>
48#include <stdbool.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <str.h>
52#include <sysman/broker.h>
53#include "vfs.h"
54
55FIBRIL_CONDVAR_INITIALIZE(fs_list_cv);
56FIBRIL_MUTEX_INITIALIZE(fs_list_lock);
57LIST_INITIALIZE(fs_list);
58
59static atomic_int fs_handle_next = 1;
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 *
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
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 /*
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 req Call structure with the request.
109 *
110 */
111void vfs_register(ipc_call_t *req)
112{
113 vfs_info_t *vfs_info;
114 errno_t rc = async_data_write_accept((void **) &vfs_info, false,
115 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
116
117 if (rc != EOK) {
118 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
119 rc);
120 async_answer_0(req, rc);
121 return;
122 }
123
124 /*
125 * Allocate and initialize a buffer for the fs_info structure.
126 */
127 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
128 if (!fs_info) {
129 dprintf("Could not allocate memory for FS info.\n");
130 async_answer_0(req, ENOMEM);
131 return;
132 }
133
134 link_initialize(&fs_info->fs_link);
135 fs_info->vfs_info = *vfs_info;
136 free(vfs_info);
137
138 dprintf("VFS info delivered.\n");
139
140 if (!vfs_info_sane(&fs_info->vfs_info)) {
141 free(fs_info);
142 async_answer_0(req, EINVAL);
143 return;
144 }
145
146 fibril_mutex_lock(&fs_list_lock);
147
148 /*
149 * Check for duplicit registrations.
150 */
151 if (fs_name_to_handle(fs_info->vfs_info.name,
152 fs_info->vfs_info.instance, false)) {
153 /*
154 * We already register a fs like this.
155 */
156 dprintf("FS is already registered.\n");
157 fibril_mutex_unlock(&fs_list_lock);
158 free(fs_info);
159 async_answer_0(req, EEXIST);
160 return;
161 }
162
163 /* Notify sysman about started FS server */
164 char *unit_name = NULL;
165 rc = fs_unit_name(fs_info->vfs_info.name, fs_info->vfs_info.instance,
166 &unit_name);
167 if (rc != EOK) {
168 dprintf("Unknow unit name for FS server.\n");
169 fibril_mutex_unlock(&fs_list_lock);
170 free(fs_info);
171 async_answer_0(rid, rc);
172 return;
173 }
174 sysman_main_exposee_added(unit_name, request->in_task_id);
175 free(unit_name);
176
177 /*
178 * Add fs_info to the list of registered FS's.
179 */
180 dprintf("Inserting FS into the list of registered file systems.\n");
181 list_append(&fs_info->fs_link, &fs_list);
182
183 /*
184 * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
185 * that a callback connection is created and we have a phone through
186 * which to forward VFS requests to it.
187 */
188 fs_info->sess = async_callback_receive(EXCHANGE_PARALLEL);
189 if (!fs_info->sess) {
190 dprintf("Callback connection expected\n");
191 list_remove(&fs_info->fs_link);
192 fibril_mutex_unlock(&fs_list_lock);
193 free(fs_info);
194 async_answer_0(req, EINVAL);
195 return;
196 }
197
198 /* FIXME: Work around problem with callback sessions */
199 async_sess_args_set(fs_info->sess, INTERFACE_VFS_DRIVER_CB, 0, 0);
200
201 dprintf("Callback connection to FS created.\n");
202
203 /*
204 * The client will want us to send him the address space area with PLB.
205 */
206
207 ipc_call_t call;
208 size_t size;
209 if (!async_share_in_receive(&call, &size)) {
210 dprintf("Unexpected call\n");
211 list_remove(&fs_info->fs_link);
212 fibril_mutex_unlock(&fs_list_lock);
213 async_hangup(fs_info->sess);
214 free(fs_info);
215 async_answer_0(&call, EINVAL);
216 async_answer_0(req, EINVAL);
217 return;
218 }
219
220 /*
221 * We can only send the client address space area PLB_SIZE bytes long.
222 */
223 if (size != PLB_SIZE) {
224 dprintf("Client suggests wrong size of PFB, size = %zu\n", size);
225 list_remove(&fs_info->fs_link);
226 fibril_mutex_unlock(&fs_list_lock);
227 async_hangup(fs_info->sess);
228 free(fs_info);
229 async_answer_0(&call, EINVAL);
230 async_answer_0(req, EINVAL);
231 return;
232 }
233
234 /*
235 * Commit to read-only sharing the PLB with the client.
236 */
237 (void) async_share_in_finalize(&call, plb,
238 AS_AREA_READ | AS_AREA_CACHEABLE);
239
240 dprintf("Sharing PLB.\n");
241
242 /*
243 * That was it. The FS has been registered.
244 * In reply to the VFS_REGISTER request, we assign the client file
245 * system a global file system handle.
246 */
247 fs_info->fs_handle = atomic_fetch_add(&fs_handle_next, 1);
248 async_answer_1(req, EOK, (sysarg_t) fs_info->fs_handle);
249
250 fibril_condvar_broadcast(&fs_list_cv);
251 fibril_mutex_unlock(&fs_list_lock);
252
253 dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
254 FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
255}
256
257/** Begin an exchange for a given file system handle
258 *
259 * @param handle File system handle.
260 *
261 * @return Exchange for a multi-call request.
262 * @return NULL if no such file exists.
263 *
264 */
265async_exch_t *vfs_exchange_grab(fs_handle_t handle)
266{
267 /*
268 * For now, we don't try to be very clever and very fast.
269 * We simply lookup the session in fs_list and
270 * begin an exchange.
271 */
272 fibril_mutex_lock(&fs_list_lock);
273
274 list_foreach(fs_list, fs_link, fs_info_t, fs) {
275 if (fs->fs_handle == handle) {
276 fibril_mutex_unlock(&fs_list_lock);
277
278 assert(fs->sess);
279 async_exch_t *exch = async_exchange_begin(fs->sess);
280
281 assert(exch);
282 return exch;
283 }
284 }
285
286 fibril_mutex_unlock(&fs_list_lock);
287
288 return NULL;
289}
290
291/** End VFS server exchange.
292 *
293 * @param exch VFS server exchange.
294 *
295 */
296void vfs_exchange_release(async_exch_t *exch)
297{
298 async_exchange_end(exch);
299}
300
301/** Convert file system name to its handle.
302 *
303 * @param name File system name.
304 * @param instance
305 * @param lock If true, the function will lock and unlock the
306 * fs_list_lock.
307 *
308 * @return File system handle or zero if file system not found.
309 *
310 */
311fs_handle_t fs_name_to_handle(const char *name, unsigned int instance, bool lock)
312{
313 int handle = 0;
314
315 if (lock)
316 fibril_mutex_lock(&fs_list_lock);
317
318 list_foreach(fs_list, fs_link, fs_info_t, fs) {
319 if (str_cmp(fs->vfs_info.name, name) == 0 &&
320 instance == fs->vfs_info.instance) {
321 handle = fs->fs_handle;
322 break;
323 }
324 }
325
326 if (lock)
327 fibril_mutex_unlock(&fs_list_lock);
328
329 return handle;
330}
331
332/** Find the VFS info structure.
333 *
334 * @param handle FS handle for which the VFS info structure is sought.
335 *
336 * @return VFS info structure on success or NULL otherwise.
337 *
338 */
339vfs_info_t *fs_handle_to_info(fs_handle_t handle)
340{
341 vfs_info_t *info = NULL;
342
343 fibril_mutex_lock(&fs_list_lock);
344 list_foreach(fs_list, fs_link, fs_info_t, fs) {
345 if (fs->fs_handle == handle) {
346 info = &fs->vfs_info;
347 break;
348 }
349 }
350 fibril_mutex_unlock(&fs_list_lock);
351
352 return info;
353}
354
355/** Get list of file system types.
356 *
357 * @param fstypes Place to store list of file system types. Free using
358 * vfs_fstypes_free().
359 *
360 * @return EOK on success or an error code
361 */
362errno_t vfs_get_fstypes(vfs_fstypes_t *fstypes)
363{
364 size_t size;
365 size_t count;
366 size_t l;
367
368 fibril_mutex_lock(&fs_list_lock);
369
370 size = 0;
371 count = 0;
372 list_foreach(fs_list, fs_link, fs_info_t, fs) {
373 size += str_size(fs->vfs_info.name) + 1;
374 count++;
375 }
376
377 if (size == 0)
378 size = 1;
379
380 fstypes->buf = calloc(1, size);
381 if (fstypes->buf == NULL) {
382 fibril_mutex_unlock(&fs_list_lock);
383 return ENOMEM;
384 }
385
386 fstypes->fstypes = calloc(sizeof(char *), count);
387 if (fstypes->fstypes == NULL) {
388 free(fstypes->buf);
389 fstypes->buf = NULL;
390 fibril_mutex_unlock(&fs_list_lock);
391 return ENOMEM;
392 }
393
394 fstypes->size = size;
395
396 size = 0;
397 count = 0;
398 list_foreach(fs_list, fs_link, fs_info_t, fs) {
399 l = str_size(fs->vfs_info.name) + 1;
400 memcpy(fstypes->buf + size, fs->vfs_info.name, l);
401 fstypes->fstypes[count] = &fstypes->buf[size];
402 size += l;
403 count++;
404 }
405
406 fibril_mutex_unlock(&fs_list_lock);
407 return EOK;
408}
409
410/** Get name of unit that represents the filesystem server
411 *
412 * Unit name is made simply by considering service of the same name as
413 * given FS name.
414 * TODO instance identifier is not implemented.
415 *
416 * @param[in] fs_name
417 * @param[in] instance
418 * @param[out] unit_name_ptr should be free'd after use, not touched on fail
419 *
420 * @return EOK on success
421 * @return ENOMEM
422 */
423errno_t fs_unit_name(const char *fs_name, unsigned int instance,
424 char **unit_name_ptr)
425{
426 assert(instance == 0);
427
428 char *unit_name = NULL;
429 asprintf(&unit_name, "%s%c%s", fs_name, UNIT_NAME_SEPARATOR,
430 UNIT_SVC_TYPE_NAME);
431
432 if (unit_name == NULL) {
433 return ENOMEM;
434 } else {
435 *unit_name_ptr = unit_name;
436 return EOK;
437 }
438}
439
440/**
441 * @}
442 */
Note: See TracBrowser for help on using the repository browser.