source: mainline/uspace/srv/vfs/vfs_register.c@ 508b0df1

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

Remove uspace <atomic.h>, use <stdatomic.h> instead

  • Property mode set to 100644
File size: 9.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 <ipc/services.h>
39#include <async.h>
40#include <fibril.h>
41#include <fibril_synch.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <str.h>
46#include <ctype.h>
47#include <stdbool.h>
48#include <adt/list.h>
49#include <as.h>
50#include <assert.h>
51#include <stdatomic.h>
52#include <vfs/vfs.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 dprintf("Processing VFS_REGISTER request received from %zx.\n",
114 req->in_phone_hash);
115
116 vfs_info_t *vfs_info;
117 errno_t rc = async_data_write_accept((void **) &vfs_info, false,
118 sizeof(vfs_info_t), sizeof(vfs_info_t), 0, NULL);
119
120 if (rc != EOK) {
121 dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
122 rc);
123 async_answer_0(req, rc);
124 return;
125 }
126
127 /*
128 * Allocate and initialize a buffer for the fs_info structure.
129 */
130 fs_info_t *fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
131 if (!fs_info) {
132 dprintf("Could not allocate memory for FS info.\n");
133 async_answer_0(req, ENOMEM);
134 return;
135 }
136
137 link_initialize(&fs_info->fs_link);
138 fs_info->vfs_info = *vfs_info;
139 free(vfs_info);
140
141 dprintf("VFS info delivered.\n");
142
143 if (!vfs_info_sane(&fs_info->vfs_info)) {
144 free(fs_info);
145 async_answer_0(req, EINVAL);
146 return;
147 }
148
149 fibril_mutex_lock(&fs_list_lock);
150
151 /*
152 * Check for duplicit registrations.
153 */
154 if (fs_name_to_handle(fs_info->vfs_info.instance,
155 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_list_lock);
161 free(fs_info);
162 async_answer_0(req, EEXIST);
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_list);
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 fs_info->sess = async_callback_receive(EXCHANGE_PARALLEL);
178 if (!fs_info->sess) {
179 dprintf("Callback connection expected\n");
180 list_remove(&fs_info->fs_link);
181 fibril_mutex_unlock(&fs_list_lock);
182 free(fs_info);
183 async_answer_0(req, EINVAL);
184 return;
185 }
186
187 /* FIXME: Work around problem with callback sessions */
188 async_sess_args_set(fs_info->sess, INTERFACE_VFS_DRIVER_CB, 0, 0);
189
190 dprintf("Callback connection to FS created.\n");
191
192 /*
193 * The client will want us to send him the address space area with PLB.
194 */
195
196 ipc_call_t call;
197 size_t size;
198 if (!async_share_in_receive(&call, &size)) {
199 dprintf("Unexpected call\n");
200 list_remove(&fs_info->fs_link);
201 fibril_mutex_unlock(&fs_list_lock);
202 async_hangup(fs_info->sess);
203 free(fs_info);
204 async_answer_0(&call, EINVAL);
205 async_answer_0(req, EINVAL);
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) {
213 dprintf("Client suggests wrong size of PFB, size = %zu\n", size);
214 list_remove(&fs_info->fs_link);
215 fibril_mutex_unlock(&fs_list_lock);
216 async_hangup(fs_info->sess);
217 free(fs_info);
218 async_answer_0(&call, EINVAL);
219 async_answer_0(req, EINVAL);
220 return;
221 }
222
223 /*
224 * Commit to read-only sharing the PLB with the client.
225 */
226 (void) async_share_in_finalize(&call, plb,
227 AS_AREA_READ | AS_AREA_CACHEABLE);
228
229 dprintf("Sharing PLB.\n");
230
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 */
236 fs_info->fs_handle = atomic_fetch_add(&fs_handle_next, 1);
237 async_answer_1(req, EOK, (sysarg_t) fs_info->fs_handle);
238
239 fibril_condvar_broadcast(&fs_list_cv);
240 fibril_mutex_unlock(&fs_list_lock);
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
246/** Begin an exchange for a given file system handle
247 *
248 * @param handle File system handle.
249 *
250 * @return Exchange for a multi-call request.
251 * @return NULL if no such file exists.
252 *
253 */
254async_exch_t *vfs_exchange_grab(fs_handle_t handle)
255{
256 /*
257 * For now, we don't try to be very clever and very fast.
258 * We simply lookup the session in fs_list and
259 * begin an exchange.
260 */
261 fibril_mutex_lock(&fs_list_lock);
262
263 list_foreach(fs_list, fs_link, fs_info_t, fs) {
264 if (fs->fs_handle == handle) {
265 fibril_mutex_unlock(&fs_list_lock);
266
267 assert(fs->sess);
268 async_exch_t *exch = async_exchange_begin(fs->sess);
269
270 assert(exch);
271 return exch;
272 }
273 }
274
275 fibril_mutex_unlock(&fs_list_lock);
276
277 return NULL;
278}
279
280/** End VFS server exchange.
281 *
282 * @param exch VFS server exchange.
283 *
284 */
285void vfs_exchange_release(async_exch_t *exch)
286{
287 async_exchange_end(exch);
288}
289
290/** Convert file system name to its handle.
291 *
292 * @param name File system name.
293 * @param lock If true, the function will lock and unlock the
294 * fs_list_lock.
295 *
296 * @return File system handle or zero if file system not found.
297 *
298 */
299fs_handle_t fs_name_to_handle(unsigned int instance, const char *name, bool lock)
300{
301 int handle = 0;
302
303 if (lock)
304 fibril_mutex_lock(&fs_list_lock);
305
306 list_foreach(fs_list, fs_link, fs_info_t, fs) {
307 if (str_cmp(fs->vfs_info.name, name) == 0 &&
308 instance == fs->vfs_info.instance) {
309 handle = fs->fs_handle;
310 break;
311 }
312 }
313
314 if (lock)
315 fibril_mutex_unlock(&fs_list_lock);
316
317 return handle;
318}
319
320/** Find the VFS info structure.
321 *
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 *
326 */
327vfs_info_t *fs_handle_to_info(fs_handle_t handle)
328{
329 vfs_info_t *info = NULL;
330
331 fibril_mutex_lock(&fs_list_lock);
332 list_foreach(fs_list, fs_link, fs_info_t, fs) {
333 if (fs->fs_handle == handle) {
334 info = &fs->vfs_info;
335 break;
336 }
337 }
338 fibril_mutex_unlock(&fs_list_lock);
339
340 return info;
341}
342
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 *
348 * @return EOK on success or an error code
349 */
350errno_t vfs_get_fstypes(vfs_fstypes_t *fstypes)
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;
385 count = 0;
386 list_foreach(fs_list, fs_link, fs_info_t, fs) {
387 l = str_size(fs->vfs_info.name) + 1;
388 memcpy(fstypes->buf + size, fs->vfs_info.name, l);
389 fstypes->fstypes[count] = &fstypes->buf[size];
390 size += l;
391 count++;
392 }
393
394 fibril_mutex_unlock(&fs_list_lock);
395 return EOK;
396}
397
398/**
399 * @}
400 */
Note: See TracBrowser for help on using the repository browser.