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
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
[b1834a01]29/** @addtogroup vfs
[62da45a]30 * @{
[8dc72b64]31 */
[62da45a]32
33/**
[8dc72b64]34 * @file vfs_register.c
[62da45a]35 * @brief
36 */
37
[63a3276]38#include <adt/list.h>
39#include <as.h>
40#include <assert.h>
[62da45a]41#include <async.h>
[63a3276]42#include <atomic.h>
43#include <ctype.h>
44#include <errno.h>
[62da45a]45#include <fibril.h>
[df908b3]46#include <fibril_synch.h>
[63a3276]47#include <ipc/services.h>
48#include <stdbool.h>
[62da45a]49#include <stdio.h>
50#include <stdlib.h>
[19f857a]51#include <str.h>
[63a3276]52#include <sysman/broker.h>
[62da45a]53#include "vfs.h"
54
[b72efe8]55FIBRIL_CONDVAR_INITIALIZE(fs_list_cv);
56FIBRIL_MUTEX_INITIALIZE(fs_list_lock);
57LIST_INITIALIZE(fs_list);
[62da45a]58
[508b0df1]59static atomic_int fs_handle_next = 1;
[62da45a]60
61/** Verify the VFS info structure.
62 *
[79ae36dd]63 * @param info Info structure to be verified.
64 *
65 * @return Non-zero if the info structure is sane, zero otherwise.
[62da45a]66 *
67 */
68static bool vfs_info_sane(vfs_info_t *info)
69{
70 int i;
[a35b458]71
[62da45a]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 }
[a35b458]80
[62da45a]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 }
[a35b458]93
[62da45a]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') {
[1b20da0]99 dprintf("The name is not properly NULL-terminated.\n");
[62da45a]100 return false;
101 }
[a35b458]102
[62da45a]103 return true;
104}
105
106/** VFS_REGISTER protocol function.
107 *
[984a9ba]108 * @param req Call structure with the request.
[79ae36dd]109 *
[62da45a]110 */
[984a9ba]111void vfs_register(ipc_call_t *req)
[62da45a]112{
[b4cbef1]113 vfs_info_t *vfs_info;
[b7fd2a0]114 errno_t rc = async_data_write_accept((void **) &vfs_info, false,
[eda925a]115 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
[a35b458]116
[b4cbef1]117 if (rc != EOK) {
118 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
119 rc);
[984a9ba]120 async_answer_0(req, rc);
[62da45a]121 return;
122 }
[a35b458]123
[62da45a]124 /*
125 * Allocate and initialize a buffer for the fs_info structure.
126 */
[b4cbef1]127 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
[62da45a]128 if (!fs_info) {
129 dprintf("Could not allocate memory for FS info.\n");
[984a9ba]130 async_answer_0(req, ENOMEM);
[62da45a]131 return;
132 }
[a35b458]133
[62da45a]134 link_initialize(&fs_info->fs_link);
[b4cbef1]135 fs_info->vfs_info = *vfs_info;
136 free(vfs_info);
[a35b458]137
[62da45a]138 dprintf("VFS info delivered.\n");
[a35b458]139
[62da45a]140 if (!vfs_info_sane(&fs_info->vfs_info)) {
141 free(fs_info);
[984a9ba]142 async_answer_0(req, EINVAL);
[62da45a]143 return;
144 }
[a35b458]145
[b72efe8]146 fibril_mutex_lock(&fs_list_lock);
[a35b458]147
[62da45a]148 /*
149 * Check for duplicit registrations.
150 */
[63a3276]151 if (fs_name_to_handle(fs_info->vfs_info.name,
152 fs_info->vfs_info.instance, false)) {
[62da45a]153 /*
154 * We already register a fs like this.
155 */
156 dprintf("FS is already registered.\n");
[b72efe8]157 fibril_mutex_unlock(&fs_list_lock);
[62da45a]158 free(fs_info);
[984a9ba]159 async_answer_0(req, EEXIST);
[62da45a]160 return;
161 }
[a35b458]162
[63a3276]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
[62da45a]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");
[b72efe8]181 list_append(&fs_info->fs_link, &fs_list);
[a35b458]182
[62da45a]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 */
[79ae36dd]188 fs_info->sess = async_callback_receive(EXCHANGE_PARALLEL);
189 if (!fs_info->sess) {
190 dprintf("Callback connection expected\n");
[62da45a]191 list_remove(&fs_info->fs_link);
[b72efe8]192 fibril_mutex_unlock(&fs_list_lock);
[62da45a]193 free(fs_info);
[984a9ba]194 async_answer_0(req, EINVAL);
[62da45a]195 return;
196 }
[a35b458]197
[9b1baac]198 /* FIXME: Work around problem with callback sessions */
199 async_sess_args_set(fs_info->sess, INTERFACE_VFS_DRIVER_CB, 0, 0);
200
[62da45a]201 dprintf("Callback connection to FS created.\n");
[a35b458]202
[62da45a]203 /*
204 * The client will want us to send him the address space area with PLB.
205 */
[a35b458]206
[984a9ba]207 ipc_call_t call;
[b4cbef1]208 size_t size;
[984a9ba]209 if (!async_share_in_receive(&call, &size)) {
[d9e68d0]210 dprintf("Unexpected call\n");
[62da45a]211 list_remove(&fs_info->fs_link);
[b72efe8]212 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]213 async_hangup(fs_info->sess);
[62da45a]214 free(fs_info);
[984a9ba]215 async_answer_0(&call, EINVAL);
216 async_answer_0(req, EINVAL);
[62da45a]217 return;
218 }
[a35b458]219
[62da45a]220 /*
221 * We can only send the client address space area PLB_SIZE bytes long.
222 */
223 if (size != PLB_SIZE) {
[d9e68d0]224 dprintf("Client suggests wrong size of PFB, size = %zu\n", size);
[62da45a]225 list_remove(&fs_info->fs_link);
[b72efe8]226 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]227 async_hangup(fs_info->sess);
[62da45a]228 free(fs_info);
[984a9ba]229 async_answer_0(&call, EINVAL);
230 async_answer_0(req, EINVAL);
[62da45a]231 return;
232 }
[a35b458]233
[62da45a]234 /*
235 * Commit to read-only sharing the PLB with the client.
236 */
[984a9ba]237 (void) async_share_in_finalize(&call, plb,
[62da45a]238 AS_AREA_READ | AS_AREA_CACHEABLE);
[a35b458]239
[62da45a]240 dprintf("Sharing PLB.\n");
[a35b458]241
[62da45a]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 */
[508b0df1]247 fs_info->fs_handle = atomic_fetch_add(&fs_handle_next, 1);
[984a9ba]248 async_answer_1(req, EOK, (sysarg_t) fs_info->fs_handle);
[a35b458]249
[b72efe8]250 fibril_condvar_broadcast(&fs_list_cv);
251 fibril_mutex_unlock(&fs_list_lock);
[a35b458]252
[62da45a]253 dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
254 FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
255}
256
[79ae36dd]257/** Begin an exchange for a given file system handle
[62da45a]258 *
[79ae36dd]259 * @param handle File system handle.
260 *
261 * @return Exchange for a multi-call request.
262 * @return NULL if no such file exists.
[62da45a]263 *
264 */
[79ae36dd]265async_exch_t *vfs_exchange_grab(fs_handle_t handle)
[62da45a]266{
267 /*
[79ae36dd]268 * For now, we don't try to be very clever and very fast.
[b72efe8]269 * We simply lookup the session in fs_list and
[79ae36dd]270 * begin an exchange.
[62da45a]271 */
[b72efe8]272 fibril_mutex_lock(&fs_list_lock);
[a35b458]273
[feeac0d]274 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[62da45a]275 if (fs->fs_handle == handle) {
[b72efe8]276 fibril_mutex_unlock(&fs_list_lock);
[a35b458]277
[79ae36dd]278 assert(fs->sess);
279 async_exch_t *exch = async_exchange_begin(fs->sess);
[a35b458]280
[79ae36dd]281 assert(exch);
282 return exch;
[62da45a]283 }
284 }
[a35b458]285
[b72efe8]286 fibril_mutex_unlock(&fs_list_lock);
[a35b458]287
[79ae36dd]288 return NULL;
[62da45a]289}
290
[79ae36dd]291/** End VFS server exchange.
292 *
293 * @param exch VFS server exchange.
[62da45a]294 *
295 */
[79ae36dd]296void vfs_exchange_release(async_exch_t *exch)
[62da45a]297{
[79ae36dd]298 async_exchange_end(exch);
[62da45a]299}
300
301/** Convert file system name to its handle.
302 *
[79ae36dd]303 * @param name File system name.
[63a3276]304 * @param instance
[79ae36dd]305 * @param lock If true, the function will lock and unlock the
[b72efe8]306 * fs_list_lock.
[79ae36dd]307 *
308 * @return File system handle or zero if file system not found.
[62da45a]309 *
310 */
[63a3276]311fs_handle_t fs_name_to_handle(const char *name, unsigned int instance, bool lock)
[62da45a]312{
313 int handle = 0;
[a35b458]314
[62da45a]315 if (lock)
[b72efe8]316 fibril_mutex_lock(&fs_list_lock);
[a35b458]317
[feeac0d]318 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[4979403]319 if (str_cmp(fs->vfs_info.name, name) == 0 &&
[286286c]320 instance == fs->vfs_info.instance) {
[62da45a]321 handle = fs->fs_handle;
322 break;
323 }
324 }
[a35b458]325
[62da45a]326 if (lock)
[b72efe8]327 fibril_mutex_unlock(&fs_list_lock);
[a35b458]328
[62da45a]329 return handle;
330}
331
[f07f6b2]332/** Find the VFS info structure.
333 *
[79ae36dd]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 *
[f07f6b2]338 */
339vfs_info_t *fs_handle_to_info(fs_handle_t handle)
340{
341 vfs_info_t *info = NULL;
[a35b458]342
[b72efe8]343 fibril_mutex_lock(&fs_list_lock);
[feeac0d]344 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[1b20da0]345 if (fs->fs_handle == handle) {
[f07f6b2]346 info = &fs->vfs_info;
347 break;
348 }
349 }
[b72efe8]350 fibril_mutex_unlock(&fs_list_lock);
[a35b458]351
[f07f6b2]352 return info;
353}
354
[b14d9f9]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 *
[cde999a]360 * @return EOK on success or an error code
[b14d9f9]361 */
[b7fd2a0]362errno_t vfs_get_fstypes(vfs_fstypes_t *fstypes)
[b14d9f9]363{
364 size_t size;
365 size_t count;
366 size_t l;
367
368 fibril_mutex_lock(&fs_list_lock);
[a35b458]369
[b14d9f9]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 }
[a35b458]376
[b14d9f9]377 if (size == 0)
378 size = 1;
[a35b458]379
[b14d9f9]380 fstypes->buf = calloc(1, size);
381 if (fstypes->buf == NULL) {
382 fibril_mutex_unlock(&fs_list_lock);
383 return ENOMEM;
384 }
[a35b458]385
[b14d9f9]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 }
[a35b458]393
[b14d9f9]394 fstypes->size = size;
[a35b458]395
[3bacee1]396 size = 0;
397 count = 0;
[b14d9f9]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
[63a3276]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
[62da45a]440/**
441 * @}
[79ae36dd]442 */
Note: See TracBrowser for help on using the repository browser.