source: mainline/uspace/srv/fs/devfs/devfs_ops.c@ ffa2c8ef

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffa2c8ef was ffa2c8ef, checked in by Martin Decky <martin@…>, 14 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 18.6 KB
RevLine 
[a095d20]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
[ed903174]38#include <macros.h>
[a095d20]39#include <bool.h>
40#include <errno.h>
41#include <malloc.h>
[19f857a]42#include <str.h>
[a095d20]43#include <libfs.h>
[1e4cada]44#include <fibril_synch.h>
[d9c8c81]45#include <adt/hash_table.h>
[1313ee9]46#include <ipc/devmap.h>
[852b801]47#include <sys/stat.h>
[1313ee9]48#include <libfs.h>
49#include <assert.h>
[a095d20]50#include "devfs.h"
51#include "devfs_ops.h"
52
[1313ee9]53typedef struct {
54 devmap_handle_type_t type;
[991f645]55 devmap_handle_t handle;
[1313ee9]56} devfs_node_t;
[a095d20]57
[17fd1d4]58/** Opened devices structure */
59typedef struct {
[991f645]60 devmap_handle_t handle;
[a7e04d16]61 int phone; /**< When < 0, the structure is incomplete. */
[17fd1d4]62 size_t refcount;
63 link_t link;
[a7e04d16]64 fibril_condvar_t cv; /**< Broadcast when completed. */
[17fd1d4]65} device_t;
66
67/** Hash table of opened devices */
68static hash_table_t devices;
69
[2dfd9fa]70/** Hash table mutex */
71static FIBRIL_MUTEX_INITIALIZE(devices_mutex);
72
[17fd1d4]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. */
78static hash_index_t devices_hash(unsigned long key[])
79{
80 return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS;
81}
82
83static 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);
[991f645]86 return (dev->handle == (devmap_handle_t) key[DEVICES_KEY_HANDLE]);
[17fd1d4]87}
88
89static void devices_remove_callback(link_t *item)
90{
91 free(hash_table_get_instance(item, device_t, link));
92}
93
94static hash_table_operations_t devices_ops = {
95 .hash = devices_hash,
96 .compare = devices_compare,
97 .remove_callback = devices_remove_callback
98};
99
[1313ee9]100static int devfs_node_get_internal(fs_node_t **rfn, devmap_handle_type_t type,
[991f645]101 devmap_handle_t handle)
[a095d20]102{
[1313ee9]103 devfs_node_t *node = (devfs_node_t *) malloc(sizeof(devfs_node_t));
104 if (node == NULL) {
105 *rfn = NULL;
106 return ENOMEM;
107 }
[17fd1d4]108
[1313ee9]109 *rfn = (fs_node_t *) malloc(sizeof(fs_node_t));
110 if (*rfn == NULL) {
111 free(node);
112 *rfn = NULL;
113 return ENOMEM;
114 }
[a095d20]115
[1313ee9]116 fs_node_initialize(*rfn);
117 node->type = type;
118 node->handle = handle;
119
120 (*rfn)->data = node;
121 return EOK;
[a095d20]122}
123
[991f645]124static int devfs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
[a095d20]125{
[1313ee9]126 return devfs_node_get_internal(rfn, DEV_HANDLE_NONE, 0);
127}
128
129static 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;
[a095d20]132
[1313ee9]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 */
[991f645]156 devmap_handle_t namespace;
[1313ee9]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;
[a095d20]175 }
176
[1313ee9]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;
[a095d20]196 }
197
[1313ee9]198 *rfn = NULL;
199 return EOK;
[a095d20]200}
201
[991f645]202static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
[a095d20]203{
[1313ee9]204 return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
[a095d20]205}
206
[1313ee9]207static int devfs_node_open(fs_node_t *fn)
[a095d20]208{
[1313ee9]209 devfs_node_t *node = (devfs_node_t *) fn->data;
[a095d20]210
[1313ee9]211 if (node->handle == 0) {
212 /* Root directory */
213 return EOK;
[a095d20]214 }
215
[1313ee9]216 devmap_handle_type_t type = devmap_handle_probe(node->handle);
[a095d20]217
[1313ee9]218 if (type == DEV_HANDLE_NAMESPACE) {
219 /* Namespace directory */
220 return EOK;
[a095d20]221 }
222
[1313ee9]223 if (type == DEV_HANDLE_DEVICE) {
224 /* Device node */
225
226 unsigned long key[] = {
227 [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
228 };
[a7e04d16]229 link_t *lnk;
230
[1313ee9]231 fibril_mutex_lock(&devices_mutex);
[a7e04d16]232restart:
233 lnk = hash_table_find(&devices, key);
[1313ee9]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;
[a095d20]239 }
240
[a7e04d16]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
[1313ee9]258 int phone = devmap_device_connect(node->handle, 0);
[a7e04d16]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
[1313ee9]268 if (phone < 0) {
[a7e04d16]269 /*
270 * Connecting failed, need to remove the
271 * entry and free the device structure.
272 */
273 hash_table_remove(&devices, key, DEVICES_KEYS);
[2dfd9fa]274 fibril_mutex_unlock(&devices_mutex);
[a7e04d16]275
[1313ee9]276 free(dev);
277 return ENOENT;
[17fd1d4]278 }
279
[a7e04d16]280 /* Set the correct phone. */
[1313ee9]281 dev->phone = phone;
282 } else {
283 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[a7e04d16]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
[1313ee9]297 dev->refcount++;
298 }
299
300 fibril_mutex_unlock(&devices_mutex);
301
302 return EOK;
[a095d20]303 }
[1313ee9]304
305 return ENOENT;
[a095d20]306}
307
[1313ee9]308static int devfs_node_put(fs_node_t *fn)
[a095d20]309{
[1313ee9]310 free(fn->data);
311 free(fn);
312 return EOK;
313}
314
[991f645]315static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
[1313ee9]316{
317 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
[a095d20]318
[1313ee9]319 *rfn = NULL;
320 return ENOTSUP;
321}
322
323static int devfs_destroy_node(fs_node_t *fn)
324{
325 return ENOTSUP;
326}
327
328static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
329{
330 return ENOTSUP;
331}
332
333static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
334{
335 return ENOTSUP;
336}
337
338static int devfs_has_children(bool *has_children, fs_node_t *fn)
339{
340 devfs_node_t *node = (devfs_node_t *) fn->data;
[a095d20]341
[1313ee9]342 if (node->handle == 0) {
343 size_t count = devmap_count_namespaces();
344 if (count > 0) {
345 *has_children = true;
346 return EOK;
[17fd1d4]347 }
348
[1313ee9]349 /* Root namespace */
[991f645]350 devmap_handle_t namespace;
[1313ee9]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 }
[17fd1d4]357 }
358
[1313ee9]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 }
[17fd1d4]369
[1313ee9]370 *has_children = false;
371 return EOK;
[a095d20]372 }
373
[1313ee9]374 *has_children = false;
375 return EOK;
[17fd1d4]376}
377
[1313ee9]378static fs_index_t devfs_index_get(fs_node_t *fn)
[17fd1d4]379{
[1313ee9]380 devfs_node_t *node = (devfs_node_t *) fn->data;
381 return node->handle;
382}
383
[ed903174]384static aoff64_t devfs_size_get(fs_node_t *fn)
[1313ee9]385{
386 return 0;
387}
388
389static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
390{
391 devfs_node_t *node = (devfs_node_t *) fn->data;
[17fd1d4]392
[1313ee9]393 if (node->handle == 0)
394 return 0;
395
396 return 1;
397}
[852b801]398
[1313ee9]399static char devfs_plb_get_char(unsigned pos)
400{
401 return devfs_reg.plb_ro[pos % PLB_SIZE];
402}
[852b801]403
[1313ee9]404static bool devfs_is_directory(fs_node_t *fn)
405{
406 devfs_node_t *node = (devfs_node_t *) fn->data;
[852b801]407
[1313ee9]408 return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
409}
410
411static 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
[991f645]418static devmap_handle_t devfs_device_get(fs_node_t *fn)
[1313ee9]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 */
429libfs_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
449bool 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
458void devfs_mounted(ipc_callid_t rid, ipc_call_t *request)
459{
460 char *opts;
461
462 /* Accept the mount options */
[96b02eb9]463 sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
[eda925a]464 0, NULL);
[1313ee9]465 if (retval != EOK) {
[ffa2c8ef]466 async_answer_0(rid, retval);
[1313ee9]467 return;
[852b801]468 }
[1313ee9]469
470 free(opts);
[ffa2c8ef]471 async_answer_3(rid, EOK, 0, 0, 0);
[1313ee9]472}
473
474void 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
[3c11713]479void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
480{
[ffa2c8ef]481 async_answer_0(rid, ENOTSUP);
[3c11713]482}
483
484void devfs_unmount(ipc_callid_t rid, ipc_call_t *request)
485{
486 libfs_unmount(&devfs_libfs_ops, rid, request);
487}
488
[1313ee9]489void devfs_lookup(ipc_callid_t rid, ipc_call_t *request)
490{
491 libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
492}
[852b801]493
[1313ee9]494void 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
499void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
500{
501 libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
[17fd1d4]502}
503
504void devfs_read(ipc_callid_t rid, ipc_call_t *request)
505{
506 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[ed903174]507 aoff64_t pos =
508 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
[17fd1d4]509
[1313ee9]510 if (index == 0) {
511 ipc_callid_t callid;
512 size_t size;
513 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]514 async_answer_0(callid, EINVAL);
515 async_answer_0(rid, EINVAL);
[1313ee9]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);
[ffa2c8ef]536 async_answer_1(rid, EOK, 1);
[1313ee9]537 return;
538 }
539
540 free(desc);
541 pos -= count;
542
543 /* Search root namespace */
[991f645]544 devmap_handle_t namespace;
[1313ee9]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);
[ffa2c8ef]551 async_answer_1(rid, EOK, 1);
[1313ee9]552 return;
553 }
554
555 free(desc);
556 }
557
[ffa2c8ef]558 async_answer_0(callid, ENOENT);
559 async_answer_1(rid, ENOENT, 0);
[1313ee9]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)) {
[ffa2c8ef]570 async_answer_0(callid, EINVAL);
571 async_answer_0(rid, EINVAL);
[1313ee9]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);
[ffa2c8ef]581 async_answer_1(rid, EOK, 1);
[1313ee9]582 return;
583 }
584
585 free(desc);
[ffa2c8ef]586 async_answer_0(callid, ENOENT);
587 async_answer_1(rid, ENOENT, 0);
[1313ee9]588 return;
589 }
590
591 if (type == DEV_HANDLE_DEVICE) {
592 /* Device node */
593
[17fd1d4]594 unsigned long key[] = {
595 [DEVICES_KEY_HANDLE] = (unsigned long) index
596 };
597
[2dfd9fa]598 fibril_mutex_lock(&devices_mutex);
[17fd1d4]599 link_t *lnk = hash_table_find(&devices, key);
600 if (lnk == NULL) {
[2dfd9fa]601 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]602 async_answer_0(rid, ENOENT);
[17fd1d4]603 return;
604 }
605
606 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[a7e04d16]607 assert(dev->phone >= 0);
[17fd1d4]608
609 ipc_callid_t callid;
[0da4e41]610 if (!async_data_read_receive(&callid, NULL)) {
[2dfd9fa]611 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]612 async_answer_0(callid, EINVAL);
613 async_answer_0(rid, EINVAL);
[17fd1d4]614 return;
615 }
616
617 /* Make a request at the driver */
618 ipc_call_t answer;
[228e490]619 aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
[17fd1d4]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 */
[ffa2c8ef]624 async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[2dfd9fa]625 fibril_mutex_unlock(&devices_mutex);
[17fd1d4]626
627 /* Wait for reply from the driver. */
[96b02eb9]628 sysarg_t rc;
[17fd1d4]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 */
[ffa2c8ef]633 async_answer_1(rid, rc, bytes);
[1313ee9]634 return;
[a095d20]635 }
[1313ee9]636
[ffa2c8ef]637 async_answer_0(rid, ENOENT);
[a095d20]638}
639
640void devfs_write(ipc_callid_t rid, ipc_call_t *request)
641{
642 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
[1313ee9]643 if (index == 0) {
[ffa2c8ef]644 async_answer_0(rid, ENOTSUP);
[1313ee9]645 return;
646 }
647
648 devmap_handle_type_t type = devmap_handle_probe(index);
649
650 if (type == DEV_HANDLE_NAMESPACE) {
651 /* Namespace directory */
[ffa2c8ef]652 async_answer_0(rid, ENOTSUP);
[1313ee9]653 return;
654 }
655
656 if (type == DEV_HANDLE_DEVICE) {
657 /* Device node */
[17fd1d4]658 unsigned long key[] = {
659 [DEVICES_KEY_HANDLE] = (unsigned long) index
660 };
661
[2dfd9fa]662 fibril_mutex_lock(&devices_mutex);
[17fd1d4]663 link_t *lnk = hash_table_find(&devices, key);
664 if (lnk == NULL) {
[2dfd9fa]665 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]666 async_answer_0(rid, ENOENT);
[17fd1d4]667 return;
668 }
669
670 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[a7e04d16]671 assert(dev->phone >= 0);
[17fd1d4]672
673 ipc_callid_t callid;
[0da4e41]674 if (!async_data_write_receive(&callid, NULL)) {
[2dfd9fa]675 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]676 async_answer_0(callid, EINVAL);
677 async_answer_0(rid, EINVAL);
[17fd1d4]678 return;
679 }
680
681 /* Make a request at the driver */
682 ipc_call_t answer;
[228e490]683 aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
[17fd1d4]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 */
[ffa2c8ef]688 async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[17fd1d4]689
[2dfd9fa]690 fibril_mutex_unlock(&devices_mutex);
691
[17fd1d4]692 /* Wait for reply from the driver. */
[96b02eb9]693 sysarg_t rc;
[17fd1d4]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 */
[ffa2c8ef]698 async_answer_1(rid, rc, bytes);
[1313ee9]699 return;
[a095d20]700 }
[1313ee9]701
[ffa2c8ef]702 async_answer_0(rid, ENOENT);
[a095d20]703}
704
705void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
706{
[ffa2c8ef]707 async_answer_0(rid, ENOTSUP);
[a095d20]708}
709
[17fd1d4]710void devfs_close(ipc_callid_t rid, ipc_call_t *request)
711{
712 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
713
[1313ee9]714 if (index == 0) {
[ffa2c8ef]715 async_answer_0(rid, EOK);
[1313ee9]716 return;
717 }
718
719 devmap_handle_type_t type = devmap_handle_probe(index);
720
721 if (type == DEV_HANDLE_NAMESPACE) {
722 /* Namespace directory */
[ffa2c8ef]723 async_answer_0(rid, EOK);
[1313ee9]724 return;
725 }
726
727 if (type == DEV_HANDLE_DEVICE) {
[17fd1d4]728 unsigned long key[] = {
729 [DEVICES_KEY_HANDLE] = (unsigned long) index
730 };
731
[2dfd9fa]732 fibril_mutex_lock(&devices_mutex);
[17fd1d4]733 link_t *lnk = hash_table_find(&devices, key);
734 if (lnk == NULL) {
[2dfd9fa]735 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]736 async_answer_0(rid, ENOENT);
[17fd1d4]737 return;
738 }
739
740 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[a7e04d16]741 assert(dev->phone >= 0);
[17fd1d4]742 dev->refcount--;
743
744 if (dev->refcount == 0) {
[ffa2c8ef]745 async_hangup(dev->phone);
[17fd1d4]746 hash_table_remove(&devices, key, DEVICES_KEYS);
747 }
748
[2dfd9fa]749 fibril_mutex_unlock(&devices_mutex);
750
[ffa2c8ef]751 async_answer_0(rid, EOK);
[1313ee9]752 return;
753 }
754
[ffa2c8ef]755 async_answer_0(rid, ENOENT);
[17fd1d4]756}
757
758void devfs_sync(ipc_callid_t rid, ipc_call_t *request)
759{
760 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
761
[1313ee9]762 if (index == 0) {
[ffa2c8ef]763 async_answer_0(rid, EOK);
[1313ee9]764 return;
765 }
766
767 devmap_handle_type_t type = devmap_handle_probe(index);
768
769 if (type == DEV_HANDLE_NAMESPACE) {
770 /* Namespace directory */
[ffa2c8ef]771 async_answer_0(rid, EOK);
[1313ee9]772 return;
773 }
774
775 if (type == DEV_HANDLE_DEVICE) {
[17fd1d4]776 unsigned long key[] = {
777 [DEVICES_KEY_HANDLE] = (unsigned long) index
778 };
779
[2dfd9fa]780 fibril_mutex_lock(&devices_mutex);
[17fd1d4]781 link_t *lnk = hash_table_find(&devices, key);
782 if (lnk == NULL) {
[2dfd9fa]783 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]784 async_answer_0(rid, ENOENT);
[17fd1d4]785 return;
786 }
787
788 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[a7e04d16]789 assert(dev->phone >= 0);
[17fd1d4]790
791 /* Make a request at the driver */
792 ipc_call_t answer;
[228e490]793 aid_t msg = async_send_2(dev->phone, IPC_GET_IMETHOD(*request),
[17fd1d4]794 IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
795
[2dfd9fa]796 fibril_mutex_unlock(&devices_mutex);
797
[17fd1d4]798 /* Wait for reply from the driver */
[96b02eb9]799 sysarg_t rc;
[17fd1d4]800 async_wait_for(msg, &rc);
801
802 /* Driver reply is the final result of the whole operation */
[ffa2c8ef]803 async_answer_0(rid, rc);
[1313ee9]804 return;
805 }
806
[ffa2c8ef]807 async_answer_0(rid, ENOENT);
[17fd1d4]808}
809
[a095d20]810void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
811{
[ffa2c8ef]812 async_answer_0(rid, ENOTSUP);
[a095d20]813}
814
815/**
816 * @}
[17fd1d4]817 */
Note: See TracBrowser for help on using the repository browser.