1 | /*
|
---|
2 | * Copyright (c) 2009 Martin Decky
|
---|
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 devfs_ops.c
|
---|
35 | * @brief Implementation of VFS operations for the devfs file system server.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <macros.h>
|
---|
39 | #include <bool.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <malloc.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <libfs.h>
|
---|
44 | #include <fibril_synch.h>
|
---|
45 | #include <adt/hash_table.h>
|
---|
46 | #include <ipc/devmap.h>
|
---|
47 | #include <sys/stat.h>
|
---|
48 | #include <libfs.h>
|
---|
49 | #include <assert.h>
|
---|
50 | #include "devfs.h"
|
---|
51 | #include "devfs_ops.h"
|
---|
52 |
|
---|
53 | typedef struct {
|
---|
54 | devmap_handle_type_t type;
|
---|
55 | devmap_handle_t handle;
|
---|
56 | } devfs_node_t;
|
---|
57 |
|
---|
58 | /** Opened devices structure */
|
---|
59 | typedef struct {
|
---|
60 | devmap_handle_t handle;
|
---|
61 | int phone; /**< When < 0, the structure is incomplete. */
|
---|
62 | size_t refcount;
|
---|
63 | link_t link;
|
---|
64 | fibril_condvar_t cv; /**< Broadcast when completed. */
|
---|
65 | } device_t;
|
---|
66 |
|
---|
67 | /** Hash table of opened devices */
|
---|
68 | static hash_table_t devices;
|
---|
69 |
|
---|
70 | /** Hash table mutex */
|
---|
71 | static FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
72 |
|
---|
73 | #define DEVICES_KEYS 1
|
---|
74 | #define DEVICES_KEY_HANDLE 0
|
---|
75 | #define DEVICES_BUCKETS 256
|
---|
76 |
|
---|
77 | /* Implementation of hash table interface for the nodes hash table. */
|
---|
78 | static hash_index_t devices_hash(unsigned long key[])
|
---|
79 | {
|
---|
80 | return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int devices_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
84 | {
|
---|
85 | device_t *dev = hash_table_get_instance(item, device_t, link);
|
---|
86 | return (dev->handle == (devmap_handle_t) key[DEVICES_KEY_HANDLE]);
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void devices_remove_callback(link_t *item)
|
---|
90 | {
|
---|
91 | free(hash_table_get_instance(item, device_t, link));
|
---|
92 | }
|
---|
93 |
|
---|
94 | static hash_table_operations_t devices_ops = {
|
---|
95 | .hash = devices_hash,
|
---|
96 | .compare = devices_compare,
|
---|
97 | .remove_callback = devices_remove_callback
|
---|
98 | };
|
---|
99 |
|
---|
100 | static int devfs_node_get_internal(fs_node_t **rfn, devmap_handle_type_t type,
|
---|
101 | devmap_handle_t handle)
|
---|
102 | {
|
---|
103 | devfs_node_t *node = (devfs_node_t *) malloc(sizeof(devfs_node_t));
|
---|
104 | if (node == NULL) {
|
---|
105 | *rfn = NULL;
|
---|
106 | return ENOMEM;
|
---|
107 | }
|
---|
108 |
|
---|
109 | *rfn = (fs_node_t *) malloc(sizeof(fs_node_t));
|
---|
110 | if (*rfn == NULL) {
|
---|
111 | free(node);
|
---|
112 | *rfn = NULL;
|
---|
113 | return ENOMEM;
|
---|
114 | }
|
---|
115 |
|
---|
116 | fs_node_initialize(*rfn);
|
---|
117 | node->type = type;
|
---|
118 | node->handle = handle;
|
---|
119 |
|
---|
120 | (*rfn)->data = node;
|
---|
121 | return EOK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int devfs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
125 | {
|
---|
126 | return devfs_node_get_internal(rfn, DEV_HANDLE_NONE, 0);
|
---|
127 | }
|
---|
128 |
|
---|
129 | static int devfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
130 | {
|
---|
131 | devfs_node_t *node = (devfs_node_t *) pfn->data;
|
---|
132 |
|
---|
133 | if (node->handle == 0) {
|
---|
134 | /* Root directory */
|
---|
135 |
|
---|
136 | dev_desc_t *devs;
|
---|
137 | size_t count = devmap_get_namespaces(&devs);
|
---|
138 |
|
---|
139 | if (count > 0) {
|
---|
140 | size_t pos;
|
---|
141 | for (pos = 0; pos < count; pos++) {
|
---|
142 | /* Ignore root namespace */
|
---|
143 | if (str_cmp(devs[pos].name, "") == 0)
|
---|
144 | continue;
|
---|
145 |
|
---|
146 | if (str_cmp(devs[pos].name, component) == 0) {
|
---|
147 | free(devs);
|
---|
148 | return devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | free(devs);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Search root namespace */
|
---|
156 | devmap_handle_t namespace;
|
---|
157 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
---|
158 | count = devmap_get_devices(namespace, &devs);
|
---|
159 |
|
---|
160 | if (count > 0) {
|
---|
161 | size_t pos;
|
---|
162 | for (pos = 0; pos < count; pos++) {
|
---|
163 | if (str_cmp(devs[pos].name, component) == 0) {
|
---|
164 | free(devs);
|
---|
165 | return devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | free(devs);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | *rfn = NULL;
|
---|
174 | return EOK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (node->type == DEV_HANDLE_NAMESPACE) {
|
---|
178 | /* Namespace directory */
|
---|
179 |
|
---|
180 | dev_desc_t *devs;
|
---|
181 | size_t count = devmap_get_devices(node->handle, &devs);
|
---|
182 | if (count > 0) {
|
---|
183 | size_t pos;
|
---|
184 | for (pos = 0; pos < count; pos++) {
|
---|
185 | if (str_cmp(devs[pos].name, component) == 0) {
|
---|
186 | free(devs);
|
---|
187 | return devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | free(devs);
|
---|
192 | }
|
---|
193 |
|
---|
194 | *rfn = NULL;
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | *rfn = NULL;
|
---|
199 | return EOK;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
203 | {
|
---|
204 | return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
|
---|
205 | }
|
---|
206 |
|
---|
207 | static int devfs_node_open(fs_node_t *fn)
|
---|
208 | {
|
---|
209 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
210 |
|
---|
211 | if (node->handle == 0) {
|
---|
212 | /* Root directory */
|
---|
213 | return EOK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | devmap_handle_type_t type = devmap_handle_probe(node->handle);
|
---|
217 |
|
---|
218 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
219 | /* Namespace directory */
|
---|
220 | return EOK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (type == DEV_HANDLE_DEVICE) {
|
---|
224 | /* Device node */
|
---|
225 |
|
---|
226 | unsigned long key[] = {
|
---|
227 | [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
|
---|
228 | };
|
---|
229 | link_t *lnk;
|
---|
230 |
|
---|
231 | fibril_mutex_lock(&devices_mutex);
|
---|
232 | restart:
|
---|
233 | lnk = hash_table_find(&devices, key);
|
---|
234 | if (lnk == NULL) {
|
---|
235 | device_t *dev = (device_t *) malloc(sizeof(device_t));
|
---|
236 | if (dev == NULL) {
|
---|
237 | fibril_mutex_unlock(&devices_mutex);
|
---|
238 | return ENOMEM;
|
---|
239 | }
|
---|
240 |
|
---|
241 | dev->handle = node->handle;
|
---|
242 | dev->phone = -1; /* mark as incomplete */
|
---|
243 | dev->refcount = 1;
|
---|
244 | fibril_condvar_initialize(&dev->cv);
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Insert the incomplete device structure so that other
|
---|
248 | * fibrils will not race with us when we drop the mutex
|
---|
249 | * below.
|
---|
250 | */
|
---|
251 | hash_table_insert(&devices, key, &dev->link);
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Drop the mutex to allow recursive devfs requests.
|
---|
255 | */
|
---|
256 | fibril_mutex_unlock(&devices_mutex);
|
---|
257 |
|
---|
258 | int phone = devmap_device_connect(node->handle, 0);
|
---|
259 |
|
---|
260 | fibril_mutex_lock(&devices_mutex);
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Notify possible waiters about this device structure
|
---|
264 | * being completed (or destroyed).
|
---|
265 | */
|
---|
266 | fibril_condvar_broadcast(&dev->cv);
|
---|
267 |
|
---|
268 | if (phone < 0) {
|
---|
269 | /*
|
---|
270 | * Connecting failed, need to remove the
|
---|
271 | * entry and free the device structure.
|
---|
272 | */
|
---|
273 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
---|
274 | fibril_mutex_unlock(&devices_mutex);
|
---|
275 |
|
---|
276 | free(dev);
|
---|
277 | return ENOENT;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* Set the correct phone. */
|
---|
281 | dev->phone = phone;
|
---|
282 | } else {
|
---|
283 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
284 |
|
---|
285 | if (dev->phone < 0) {
|
---|
286 | /*
|
---|
287 | * Wait until the device structure is completed
|
---|
288 | * and start from the beginning as the device
|
---|
289 | * structure might have entirely disappeared
|
---|
290 | * while we were not holding the mutex in
|
---|
291 | * fibril_condvar_wait().
|
---|
292 | */
|
---|
293 | fibril_condvar_wait(&dev->cv, &devices_mutex);
|
---|
294 | goto restart;
|
---|
295 | }
|
---|
296 |
|
---|
297 | dev->refcount++;
|
---|
298 | }
|
---|
299 |
|
---|
300 | fibril_mutex_unlock(&devices_mutex);
|
---|
301 |
|
---|
302 | return EOK;
|
---|
303 | }
|
---|
304 |
|
---|
305 | return ENOENT;
|
---|
306 | }
|
---|
307 |
|
---|
308 | static int devfs_node_put(fs_node_t *fn)
|
---|
309 | {
|
---|
310 | free(fn->data);
|
---|
311 | free(fn);
|
---|
312 | return EOK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
---|
316 | {
|
---|
317 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
318 |
|
---|
319 | *rfn = NULL;
|
---|
320 | return ENOTSUP;
|
---|
321 | }
|
---|
322 |
|
---|
323 | static int devfs_destroy_node(fs_node_t *fn)
|
---|
324 | {
|
---|
325 | return ENOTSUP;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
329 | {
|
---|
330 | return ENOTSUP;
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
334 | {
|
---|
335 | return ENOTSUP;
|
---|
336 | }
|
---|
337 |
|
---|
338 | static int devfs_has_children(bool *has_children, fs_node_t *fn)
|
---|
339 | {
|
---|
340 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
341 |
|
---|
342 | if (node->handle == 0) {
|
---|
343 | size_t count = devmap_count_namespaces();
|
---|
344 | if (count > 0) {
|
---|
345 | *has_children = true;
|
---|
346 | return EOK;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* Root namespace */
|
---|
350 | devmap_handle_t namespace;
|
---|
351 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
---|
352 | count = devmap_count_devices(namespace);
|
---|
353 | if (count > 0) {
|
---|
354 | *has_children = true;
|
---|
355 | return EOK;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | *has_children = false;
|
---|
360 | return EOK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (node->type == DEV_HANDLE_NAMESPACE) {
|
---|
364 | size_t count = devmap_count_devices(node->handle);
|
---|
365 | if (count > 0) {
|
---|
366 | *has_children = true;
|
---|
367 | return EOK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | *has_children = false;
|
---|
371 | return EOK;
|
---|
372 | }
|
---|
373 |
|
---|
374 | *has_children = false;
|
---|
375 | return EOK;
|
---|
376 | }
|
---|
377 |
|
---|
378 | static fs_index_t devfs_index_get(fs_node_t *fn)
|
---|
379 | {
|
---|
380 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
381 | return node->handle;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static aoff64_t devfs_size_get(fs_node_t *fn)
|
---|
385 | {
|
---|
386 | return 0;
|
---|
387 | }
|
---|
388 |
|
---|
389 | static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
|
---|
390 | {
|
---|
391 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
392 |
|
---|
393 | if (node->handle == 0)
|
---|
394 | return 0;
|
---|
395 |
|
---|
396 | return 1;
|
---|
397 | }
|
---|
398 |
|
---|
399 | static char devfs_plb_get_char(unsigned pos)
|
---|
400 | {
|
---|
401 | return devfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
402 | }
|
---|
403 |
|
---|
404 | static bool devfs_is_directory(fs_node_t *fn)
|
---|
405 | {
|
---|
406 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
407 |
|
---|
408 | return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
|
---|
409 | }
|
---|
410 |
|
---|
411 | static bool devfs_is_file(fs_node_t *fn)
|
---|
412 | {
|
---|
413 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
414 |
|
---|
415 | return (node->type == DEV_HANDLE_DEVICE);
|
---|
416 | }
|
---|
417 |
|
---|
418 | static devmap_handle_t devfs_device_get(fs_node_t *fn)
|
---|
419 | {
|
---|
420 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
421 |
|
---|
422 | if (node->type == DEV_HANDLE_DEVICE)
|
---|
423 | return node->handle;
|
---|
424 |
|
---|
425 | return 0;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /** libfs operations */
|
---|
429 | libfs_ops_t devfs_libfs_ops = {
|
---|
430 | .root_get = devfs_root_get,
|
---|
431 | .match = devfs_match,
|
---|
432 | .node_get = devfs_node_get,
|
---|
433 | .node_open = devfs_node_open,
|
---|
434 | .node_put = devfs_node_put,
|
---|
435 | .create = devfs_create_node,
|
---|
436 | .destroy = devfs_destroy_node,
|
---|
437 | .link = devfs_link_node,
|
---|
438 | .unlink = devfs_unlink_node,
|
---|
439 | .has_children = devfs_has_children,
|
---|
440 | .index_get = devfs_index_get,
|
---|
441 | .size_get = devfs_size_get,
|
---|
442 | .lnkcnt_get = devfs_lnkcnt_get,
|
---|
443 | .plb_get_char = devfs_plb_get_char,
|
---|
444 | .is_directory = devfs_is_directory,
|
---|
445 | .is_file = devfs_is_file,
|
---|
446 | .device_get = devfs_device_get
|
---|
447 | };
|
---|
448 |
|
---|
449 | bool devfs_init(void)
|
---|
450 | {
|
---|
451 | if (!hash_table_create(&devices, DEVICES_BUCKETS,
|
---|
452 | DEVICES_KEYS, &devices_ops))
|
---|
453 | return false;
|
---|
454 |
|
---|
455 | return true;
|
---|
456 | }
|
---|
457 |
|
---|
458 | void devfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
459 | {
|
---|
460 | char *opts;
|
---|
461 |
|
---|
462 | /* Accept the mount options */
|
---|
463 | sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
|
---|
464 | 0, NULL);
|
---|
465 | if (retval != EOK) {
|
---|
466 | async_answer_0(rid, retval);
|
---|
467 | return;
|
---|
468 | }
|
---|
469 |
|
---|
470 | free(opts);
|
---|
471 | async_answer_3(rid, EOK, 0, 0, 0);
|
---|
472 | }
|
---|
473 |
|
---|
474 | void devfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
475 | {
|
---|
476 | libfs_mount(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
477 | }
|
---|
478 |
|
---|
479 | void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
480 | {
|
---|
481 | async_answer_0(rid, ENOTSUP);
|
---|
482 | }
|
---|
483 |
|
---|
484 | void devfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
485 | {
|
---|
486 | libfs_unmount(&devfs_libfs_ops, rid, request);
|
---|
487 | }
|
---|
488 |
|
---|
489 | void devfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
490 | {
|
---|
491 | libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
492 | }
|
---|
493 |
|
---|
494 | void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
495 | {
|
---|
496 | libfs_open_node(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
497 | }
|
---|
498 |
|
---|
499 | void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
500 | {
|
---|
501 | libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
502 | }
|
---|
503 |
|
---|
504 | void devfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
505 | {
|
---|
506 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
507 | aoff64_t pos =
|
---|
508 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
509 |
|
---|
510 | if (index == 0) {
|
---|
511 | ipc_callid_t callid;
|
---|
512 | size_t size;
|
---|
513 | if (!async_data_read_receive(&callid, &size)) {
|
---|
514 | async_answer_0(callid, EINVAL);
|
---|
515 | async_answer_0(rid, EINVAL);
|
---|
516 | return;
|
---|
517 | }
|
---|
518 |
|
---|
519 | dev_desc_t *desc;
|
---|
520 | size_t count = devmap_get_namespaces(&desc);
|
---|
521 |
|
---|
522 | /* Get rid of root namespace */
|
---|
523 | size_t i;
|
---|
524 | for (i = 0; i < count; i++) {
|
---|
525 | if (str_cmp(desc[i].name, "") == 0) {
|
---|
526 | if (pos >= i)
|
---|
527 | pos++;
|
---|
528 |
|
---|
529 | break;
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | if (pos < count) {
|
---|
534 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
535 | free(desc);
|
---|
536 | async_answer_1(rid, EOK, 1);
|
---|
537 | return;
|
---|
538 | }
|
---|
539 |
|
---|
540 | free(desc);
|
---|
541 | pos -= count;
|
---|
542 |
|
---|
543 | /* Search root namespace */
|
---|
544 | devmap_handle_t namespace;
|
---|
545 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
---|
546 | count = devmap_get_devices(namespace, &desc);
|
---|
547 |
|
---|
548 | if (pos < count) {
|
---|
549 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
550 | free(desc);
|
---|
551 | async_answer_1(rid, EOK, 1);
|
---|
552 | return;
|
---|
553 | }
|
---|
554 |
|
---|
555 | free(desc);
|
---|
556 | }
|
---|
557 |
|
---|
558 | async_answer_0(callid, ENOENT);
|
---|
559 | async_answer_1(rid, ENOENT, 0);
|
---|
560 | return;
|
---|
561 | }
|
---|
562 |
|
---|
563 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
564 |
|
---|
565 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
566 | /* Namespace directory */
|
---|
567 | ipc_callid_t callid;
|
---|
568 | size_t size;
|
---|
569 | if (!async_data_read_receive(&callid, &size)) {
|
---|
570 | async_answer_0(callid, EINVAL);
|
---|
571 | async_answer_0(rid, EINVAL);
|
---|
572 | return;
|
---|
573 | }
|
---|
574 |
|
---|
575 | dev_desc_t *desc;
|
---|
576 | size_t count = devmap_get_devices(index, &desc);
|
---|
577 |
|
---|
578 | if (pos < count) {
|
---|
579 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
580 | free(desc);
|
---|
581 | async_answer_1(rid, EOK, 1);
|
---|
582 | return;
|
---|
583 | }
|
---|
584 |
|
---|
585 | free(desc);
|
---|
586 | async_answer_0(callid, ENOENT);
|
---|
587 | async_answer_1(rid, ENOENT, 0);
|
---|
588 | return;
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (type == DEV_HANDLE_DEVICE) {
|
---|
592 | /* Device node */
|
---|
593 |
|
---|
594 | unsigned long key[] = {
|
---|
595 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
596 | };
|
---|
597 |
|
---|
598 | fibril_mutex_lock(&devices_mutex);
|
---|
599 | link_t *lnk = hash_table_find(&devices, key);
|
---|
600 | if (lnk == NULL) {
|
---|
601 | fibril_mutex_unlock(&devices_mutex);
|
---|
602 | async_answer_0(rid, ENOENT);
|
---|
603 | return;
|
---|
604 | }
|
---|
605 |
|
---|
606 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
607 | assert(dev->phone >= 0);
|
---|
608 |
|
---|
609 | ipc_callid_t callid;
|
---|
610 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
611 | fibril_mutex_unlock(&devices_mutex);
|
---|
612 | async_answer_0(callid, EINVAL);
|
---|
613 | async_answer_0(rid, EINVAL);
|
---|
614 | return;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /* Make a request at the driver */
|
---|
618 | ipc_call_t answer;
|
---|
619 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
620 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
---|
621 | IPC_GET_ARG3(*request), &answer);
|
---|
622 |
|
---|
623 | /* Forward the IPC_M_DATA_READ request to the driver */
|
---|
624 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
625 | fibril_mutex_unlock(&devices_mutex);
|
---|
626 |
|
---|
627 | /* Wait for reply from the driver. */
|
---|
628 | sysarg_t rc;
|
---|
629 | async_wait_for(msg, &rc);
|
---|
630 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
631 |
|
---|
632 | /* Driver reply is the final result of the whole operation */
|
---|
633 | async_answer_1(rid, rc, bytes);
|
---|
634 | return;
|
---|
635 | }
|
---|
636 |
|
---|
637 | async_answer_0(rid, ENOENT);
|
---|
638 | }
|
---|
639 |
|
---|
640 | void devfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
641 | {
|
---|
642 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
643 | if (index == 0) {
|
---|
644 | async_answer_0(rid, ENOTSUP);
|
---|
645 | return;
|
---|
646 | }
|
---|
647 |
|
---|
648 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
649 |
|
---|
650 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
651 | /* Namespace directory */
|
---|
652 | async_answer_0(rid, ENOTSUP);
|
---|
653 | return;
|
---|
654 | }
|
---|
655 |
|
---|
656 | if (type == DEV_HANDLE_DEVICE) {
|
---|
657 | /* Device node */
|
---|
658 | unsigned long key[] = {
|
---|
659 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
660 | };
|
---|
661 |
|
---|
662 | fibril_mutex_lock(&devices_mutex);
|
---|
663 | link_t *lnk = hash_table_find(&devices, key);
|
---|
664 | if (lnk == NULL) {
|
---|
665 | fibril_mutex_unlock(&devices_mutex);
|
---|
666 | async_answer_0(rid, ENOENT);
|
---|
667 | return;
|
---|
668 | }
|
---|
669 |
|
---|
670 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
671 | assert(dev->phone >= 0);
|
---|
672 |
|
---|
673 | ipc_callid_t callid;
|
---|
674 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
675 | fibril_mutex_unlock(&devices_mutex);
|
---|
676 | async_answer_0(callid, EINVAL);
|
---|
677 | async_answer_0(rid, EINVAL);
|
---|
678 | return;
|
---|
679 | }
|
---|
680 |
|
---|
681 | /* Make a request at the driver */
|
---|
682 | ipc_call_t answer;
|
---|
683 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
684 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
---|
685 | IPC_GET_ARG3(*request), &answer);
|
---|
686 |
|
---|
687 | /* Forward the IPC_M_DATA_WRITE request to the driver */
|
---|
688 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
689 |
|
---|
690 | fibril_mutex_unlock(&devices_mutex);
|
---|
691 |
|
---|
692 | /* Wait for reply from the driver. */
|
---|
693 | sysarg_t rc;
|
---|
694 | async_wait_for(msg, &rc);
|
---|
695 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
696 |
|
---|
697 | /* Driver reply is the final result of the whole operation */
|
---|
698 | async_answer_1(rid, rc, bytes);
|
---|
699 | return;
|
---|
700 | }
|
---|
701 |
|
---|
702 | async_answer_0(rid, ENOENT);
|
---|
703 | }
|
---|
704 |
|
---|
705 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
706 | {
|
---|
707 | async_answer_0(rid, ENOTSUP);
|
---|
708 | }
|
---|
709 |
|
---|
710 | void devfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
711 | {
|
---|
712 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
713 |
|
---|
714 | if (index == 0) {
|
---|
715 | async_answer_0(rid, EOK);
|
---|
716 | return;
|
---|
717 | }
|
---|
718 |
|
---|
719 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
720 |
|
---|
721 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
722 | /* Namespace directory */
|
---|
723 | async_answer_0(rid, EOK);
|
---|
724 | return;
|
---|
725 | }
|
---|
726 |
|
---|
727 | if (type == DEV_HANDLE_DEVICE) {
|
---|
728 | unsigned long key[] = {
|
---|
729 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
730 | };
|
---|
731 |
|
---|
732 | fibril_mutex_lock(&devices_mutex);
|
---|
733 | link_t *lnk = hash_table_find(&devices, key);
|
---|
734 | if (lnk == NULL) {
|
---|
735 | fibril_mutex_unlock(&devices_mutex);
|
---|
736 | async_answer_0(rid, ENOENT);
|
---|
737 | return;
|
---|
738 | }
|
---|
739 |
|
---|
740 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
741 | assert(dev->phone >= 0);
|
---|
742 | dev->refcount--;
|
---|
743 |
|
---|
744 | if (dev->refcount == 0) {
|
---|
745 | async_hangup(dev->phone);
|
---|
746 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
---|
747 | }
|
---|
748 |
|
---|
749 | fibril_mutex_unlock(&devices_mutex);
|
---|
750 |
|
---|
751 | async_answer_0(rid, EOK);
|
---|
752 | return;
|
---|
753 | }
|
---|
754 |
|
---|
755 | async_answer_0(rid, ENOENT);
|
---|
756 | }
|
---|
757 |
|
---|
758 | void devfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
759 | {
|
---|
760 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
761 |
|
---|
762 | if (index == 0) {
|
---|
763 | async_answer_0(rid, EOK);
|
---|
764 | return;
|
---|
765 | }
|
---|
766 |
|
---|
767 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
768 |
|
---|
769 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
770 | /* Namespace directory */
|
---|
771 | async_answer_0(rid, EOK);
|
---|
772 | return;
|
---|
773 | }
|
---|
774 |
|
---|
775 | if (type == DEV_HANDLE_DEVICE) {
|
---|
776 | unsigned long key[] = {
|
---|
777 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
778 | };
|
---|
779 |
|
---|
780 | fibril_mutex_lock(&devices_mutex);
|
---|
781 | link_t *lnk = hash_table_find(&devices, key);
|
---|
782 | if (lnk == NULL) {
|
---|
783 | fibril_mutex_unlock(&devices_mutex);
|
---|
784 | async_answer_0(rid, ENOENT);
|
---|
785 | return;
|
---|
786 | }
|
---|
787 |
|
---|
788 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
789 | assert(dev->phone >= 0);
|
---|
790 |
|
---|
791 | /* Make a request at the driver */
|
---|
792 | ipc_call_t answer;
|
---|
793 | aid_t msg = async_send_2(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
794 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
|
---|
795 |
|
---|
796 | fibril_mutex_unlock(&devices_mutex);
|
---|
797 |
|
---|
798 | /* Wait for reply from the driver */
|
---|
799 | sysarg_t rc;
|
---|
800 | async_wait_for(msg, &rc);
|
---|
801 |
|
---|
802 | /* Driver reply is the final result of the whole operation */
|
---|
803 | async_answer_0(rid, rc);
|
---|
804 | return;
|
---|
805 | }
|
---|
806 |
|
---|
807 | async_answer_0(rid, ENOENT);
|
---|
808 | }
|
---|
809 |
|
---|
810 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
811 | {
|
---|
812 | async_answer_0(rid, ENOTSUP);
|
---|
813 | }
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * @}
|
---|
817 | */
|
---|