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