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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 9.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
29/** @addtogroup fs
30 * @{
[8dc72b64]31 */
[62da45a]32
33/**
[8dc72b64]34 * @file vfs_register.c
[62da45a]35 * @brief
36 */
37
38#include <ipc/services.h>
39#include <async.h>
40#include <fibril.h>
[df908b3]41#include <fibril_synch.h>
[62da45a]42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
[19f857a]45#include <str.h>
[62da45a]46#include <ctype.h>
[3e6a98c5]47#include <stdbool.h>
[d9c8c81]48#include <adt/list.h>
[62da45a]49#include <as.h>
50#include <assert.h>
51#include <atomic.h>
[b14d9f9]52#include <vfs/vfs.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
59atomic_t fs_handle_next = {
60 .count = 1
61};
62
63/** Verify the VFS info structure.
64 *
[79ae36dd]65 * @param info Info structure to be verified.
66 *
67 * @return Non-zero if the info structure is sane, zero otherwise.
[62da45a]68 *
69 */
70static bool vfs_info_sane(vfs_info_t *info)
71{
72 int i;
[79ae36dd]73
[62da45a]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 }
[79ae36dd]82
[62da45a]83 for (i = 1; i < FS_NAME_MAXLEN; i++) {
84 if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
85 (info->name[i] != '-') && (info->name[i] != '_')) {
86 if (info->name[i] == '\0') {
87 break;
88 } else {
89 dprintf("The name contains illegal "
90 "characters.\n");
91 return false;
92 }
93 }
94 }
[79ae36dd]95
[62da45a]96 /*
97 * This check is not redundant. It ensures that the name is
98 * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
99 */
100 if (info->name[i] != '\0') {
101 dprintf("The name is not properly NULL-terminated.\n");
102 return false;
103 }
104
105 return true;
106}
107
108/** VFS_REGISTER protocol function.
109 *
[79ae36dd]110 * @param rid Hash of the call with the request.
111 * @param request Call structure with the request.
112 *
[62da45a]113 */
114void vfs_register(ipc_callid_t rid, ipc_call_t *request)
115{
[d9e68d0]116 dprintf("Processing VFS_REGISTER request received from %zx.\n",
[62da45a]117 request->in_phone_hash);
118
[b4cbef1]119 vfs_info_t *vfs_info;
[b7fd2a0]120 errno_t rc = async_data_write_accept((void **) &vfs_info, false,
[eda925a]121 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
[62da45a]122
[b4cbef1]123 if (rc != EOK) {
124 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
125 rc);
[ffa2c8ef]126 async_answer_0(rid, rc);
[62da45a]127 return;
128 }
[b4cbef1]129
[62da45a]130 /*
131 * Allocate and initialize a buffer for the fs_info structure.
132 */
[b4cbef1]133 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
[62da45a]134 if (!fs_info) {
135 dprintf("Could not allocate memory for FS info.\n");
[ffa2c8ef]136 async_answer_0(rid, ENOMEM);
[62da45a]137 return;
138 }
[b4cbef1]139
[62da45a]140 link_initialize(&fs_info->fs_link);
[b4cbef1]141 fs_info->vfs_info = *vfs_info;
142 free(vfs_info);
143
[62da45a]144 dprintf("VFS info delivered.\n");
[b4cbef1]145
[62da45a]146 if (!vfs_info_sane(&fs_info->vfs_info)) {
147 free(fs_info);
[ffa2c8ef]148 async_answer_0(rid, EINVAL);
[62da45a]149 return;
150 }
[b4cbef1]151
[b72efe8]152 fibril_mutex_lock(&fs_list_lock);
[b4cbef1]153
[62da45a]154 /*
155 * Check for duplicit registrations.
156 */
[4979403]157 if (fs_name_to_handle(fs_info->vfs_info.instance,
[286286c]158 fs_info->vfs_info.name, false)) {
[62da45a]159 /*
160 * We already register a fs like this.
161 */
162 dprintf("FS is already registered.\n");
[b72efe8]163 fibril_mutex_unlock(&fs_list_lock);
[62da45a]164 free(fs_info);
[8a637a4]165 async_answer_0(rid, EEXIST);
[62da45a]166 return;
167 }
[b4cbef1]168
[62da45a]169 /*
170 * Add fs_info to the list of registered FS's.
171 */
172 dprintf("Inserting FS into the list of registered file systems.\n");
[b72efe8]173 list_append(&fs_info->fs_link, &fs_list);
[07deef5]174
[62da45a]175 /*
176 * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
177 * that a callback connection is created and we have a phone through
178 * which to forward VFS requests to it.
179 */
[79ae36dd]180 fs_info->sess = async_callback_receive(EXCHANGE_PARALLEL);
181 if (!fs_info->sess) {
182 dprintf("Callback connection expected\n");
[62da45a]183 list_remove(&fs_info->fs_link);
[b72efe8]184 fibril_mutex_unlock(&fs_list_lock);
[62da45a]185 free(fs_info);
[ffa2c8ef]186 async_answer_0(rid, EINVAL);
[62da45a]187 return;
188 }
[c1c0184]189
[62da45a]190 dprintf("Callback connection to FS created.\n");
[b4cbef1]191
[62da45a]192 /*
193 * The client will want us to send him the address space area with PLB.
194 */
[b4cbef1]195
196 size_t size;
[79ae36dd]197 ipc_callid_t callid;
[0da4e41]198 if (!async_share_in_receive(&callid, &size)) {
[d9e68d0]199 dprintf("Unexpected call\n");
[62da45a]200 list_remove(&fs_info->fs_link);
[b72efe8]201 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]202 async_hangup(fs_info->sess);
[62da45a]203 free(fs_info);
[ffa2c8ef]204 async_answer_0(callid, EINVAL);
205 async_answer_0(rid, EINVAL);
[62da45a]206 return;
207 }
208
209 /*
210 * We can only send the client address space area PLB_SIZE bytes long.
211 */
212 if (size != PLB_SIZE) {
[d9e68d0]213 dprintf("Client suggests wrong size of PFB, size = %zu\n", size);
[62da45a]214 list_remove(&fs_info->fs_link);
[b72efe8]215 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]216 async_hangup(fs_info->sess);
[62da45a]217 free(fs_info);
[ffa2c8ef]218 async_answer_0(callid, EINVAL);
219 async_answer_0(rid, EINVAL);
[62da45a]220 return;
221 }
[b4cbef1]222
[62da45a]223 /*
224 * Commit to read-only sharing the PLB with the client.
225 */
[0da4e41]226 (void) async_share_in_finalize(callid, plb,
[62da45a]227 AS_AREA_READ | AS_AREA_CACHEABLE);
[b4cbef1]228
[62da45a]229 dprintf("Sharing PLB.\n");
[b4cbef1]230
[62da45a]231 /*
232 * That was it. The FS has been registered.
233 * In reply to the VFS_REGISTER request, we assign the client file
234 * system a global file system handle.
235 */
[f2ec8c8]236 fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
[ffa2c8ef]237 async_answer_1(rid, EOK, (sysarg_t) fs_info->fs_handle);
[62da45a]238
[b72efe8]239 fibril_condvar_broadcast(&fs_list_cv);
240 fibril_mutex_unlock(&fs_list_lock);
[62da45a]241
242 dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
243 FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
244}
245
[79ae36dd]246/** Begin an exchange for a given file system handle
[62da45a]247 *
[79ae36dd]248 * @param handle File system handle.
249 *
250 * @return Exchange for a multi-call request.
251 * @return NULL if no such file exists.
[62da45a]252 *
253 */
[79ae36dd]254async_exch_t *vfs_exchange_grab(fs_handle_t handle)
[62da45a]255{
256 /*
[79ae36dd]257 * For now, we don't try to be very clever and very fast.
[b72efe8]258 * We simply lookup the session in fs_list and
[79ae36dd]259 * begin an exchange.
[62da45a]260 */
[b72efe8]261 fibril_mutex_lock(&fs_list_lock);
[79ae36dd]262
[feeac0d]263 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[62da45a]264 if (fs->fs_handle == handle) {
[b72efe8]265 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]266
267 assert(fs->sess);
268 async_exch_t *exch = async_exchange_begin(fs->sess);
269
270 assert(exch);
271 return exch;
[62da45a]272 }
273 }
[79ae36dd]274
[b72efe8]275 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]276
277 return NULL;
[62da45a]278}
279
[79ae36dd]280/** End VFS server exchange.
281 *
282 * @param exch VFS server exchange.
[62da45a]283 *
284 */
[79ae36dd]285void vfs_exchange_release(async_exch_t *exch)
[62da45a]286{
[79ae36dd]287 async_exchange_end(exch);
[62da45a]288}
289
290/** Convert file system name to its handle.
291 *
[79ae36dd]292 * @param name File system name.
293 * @param lock If true, the function will lock and unlock the
[b72efe8]294 * fs_list_lock.
[79ae36dd]295 *
296 * @return File system handle or zero if file system not found.
[62da45a]297 *
298 */
[0d35511]299fs_handle_t fs_name_to_handle(unsigned int instance, const char *name, bool lock)
[62da45a]300{
301 int handle = 0;
302
303 if (lock)
[b72efe8]304 fibril_mutex_lock(&fs_list_lock);
[79ae36dd]305
[feeac0d]306 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[4979403]307 if (str_cmp(fs->vfs_info.name, name) == 0 &&
[286286c]308 instance == fs->vfs_info.instance) {
[62da45a]309 handle = fs->fs_handle;
310 break;
311 }
312 }
[79ae36dd]313
[62da45a]314 if (lock)
[b72efe8]315 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]316
[62da45a]317 return handle;
318}
319
[f07f6b2]320/** Find the VFS info structure.
321 *
[79ae36dd]322 * @param handle FS handle for which the VFS info structure is sought.
323 *
324 * @return VFS info structure on success or NULL otherwise.
325 *
[f07f6b2]326 */
327vfs_info_t *fs_handle_to_info(fs_handle_t handle)
328{
329 vfs_info_t *info = NULL;
[79ae36dd]330
[b72efe8]331 fibril_mutex_lock(&fs_list_lock);
[feeac0d]332 list_foreach(fs_list, fs_link, fs_info_t, fs) {
[f07f6b2]333 if (fs->fs_handle == handle) {
334 info = &fs->vfs_info;
335 break;
336 }
337 }
[b72efe8]338 fibril_mutex_unlock(&fs_list_lock);
[79ae36dd]339
[f07f6b2]340 return info;
341}
342
[b14d9f9]343/** Get list of file system types.
344 *
345 * @param fstypes Place to store list of file system types. Free using
346 * vfs_fstypes_free().
347 *
[cde999a]348 * @return EOK on success or an error code
[b14d9f9]349 */
[b7fd2a0]350errno_t vfs_get_fstypes(vfs_fstypes_t *fstypes)
[b14d9f9]351{
352 size_t size;
353 size_t count;
354 size_t l;
355
356 fibril_mutex_lock(&fs_list_lock);
357
358 size = 0;
359 count = 0;
360 list_foreach(fs_list, fs_link, fs_info_t, fs) {
361 size += str_size(fs->vfs_info.name) + 1;
362 count++;
363 }
364
365 if (size == 0)
366 size = 1;
367
368 fstypes->buf = calloc(1, size);
369 if (fstypes->buf == NULL) {
370 fibril_mutex_unlock(&fs_list_lock);
371 return ENOMEM;
372 }
373
374 fstypes->fstypes = calloc(sizeof(char *), count);
375 if (fstypes->fstypes == NULL) {
376 free(fstypes->buf);
377 fstypes->buf = NULL;
378 fibril_mutex_unlock(&fs_list_lock);
379 return ENOMEM;
380 }
381
382 fstypes->size = size;
383
384 size = 0; count = 0;
385 list_foreach(fs_list, fs_link, fs_info_t, fs) {
386 l = str_size(fs->vfs_info.name) + 1;
387 memcpy(fstypes->buf + size, fs->vfs_info.name, l);
388 fstypes->fstypes[count] = &fstypes->buf[size];
389 size += l;
390 count++;
391 }
392
393 fibril_mutex_unlock(&fs_list_lock);
394 return EOK;
395}
396
[62da45a]397/**
398 * @}
[79ae36dd]399 */
Note: See TracBrowser for help on using the repository browser.