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

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

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 18.9 KB
Line 
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
53typedef struct {
54 devmap_handle_type_t type;
55 devmap_handle_t handle;
56} devfs_node_t;
57
58/** Opened devices structure */
59typedef struct {
60 devmap_handle_t handle;
61 async_sess_t *sess; /**< If NULL, 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 */
68static hash_table_t devices;
69
70/** Hash table mutex */
71static 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. */
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);
86 return (dev->handle == (devmap_handle_t) key[DEVICES_KEY_HANDLE]);
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
100static 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
124static 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
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;
132 int ret;
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 ret = devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
149 free(devs);
150 return ret;
151 }
152 }
153
154 free(devs);
155 }
156
157 /* Search root namespace */
158 devmap_handle_t namespace;
159 if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
160 count = devmap_get_devices(namespace, &devs);
161
162 if (count > 0) {
163 size_t pos;
164 for (pos = 0; pos < count; pos++) {
165 if (str_cmp(devs[pos].name, component) == 0) {
166 ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
167 free(devs);
168 return ret;
169 }
170 }
171
172 free(devs);
173 }
174 }
175
176 *rfn = NULL;
177 return EOK;
178 }
179
180 if (node->type == DEV_HANDLE_NAMESPACE) {
181 /* Namespace directory */
182
183 dev_desc_t *devs;
184 size_t count = devmap_get_devices(node->handle, &devs);
185 if (count > 0) {
186 size_t pos;
187 for (pos = 0; pos < count; pos++) {
188 if (str_cmp(devs[pos].name, component) == 0) {
189 ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
190 free(devs);
191 return ret;
192 }
193 }
194
195 free(devs);
196 }
197
198 *rfn = NULL;
199 return EOK;
200 }
201
202 *rfn = NULL;
203 return EOK;
204}
205
206static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
207{
208 return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
209}
210
211static int devfs_node_open(fs_node_t *fn)
212{
213 devfs_node_t *node = (devfs_node_t *) fn->data;
214
215 if (node->handle == 0) {
216 /* Root directory */
217 return EOK;
218 }
219
220 devmap_handle_type_t type = devmap_handle_probe(node->handle);
221
222 if (type == DEV_HANDLE_NAMESPACE) {
223 /* Namespace directory */
224 return EOK;
225 }
226
227 if (type == DEV_HANDLE_DEVICE) {
228 /* Device node */
229
230 unsigned long key[] = {
231 [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
232 };
233 link_t *lnk;
234
235 fibril_mutex_lock(&devices_mutex);
236restart:
237 lnk = hash_table_find(&devices, key);
238 if (lnk == NULL) {
239 device_t *dev = (device_t *) malloc(sizeof(device_t));
240 if (dev == NULL) {
241 fibril_mutex_unlock(&devices_mutex);
242 return ENOMEM;
243 }
244
245 dev->handle = node->handle;
246
247 /* Mark as incomplete */
248 dev->sess = NULL;
249 dev->refcount = 1;
250 fibril_condvar_initialize(&dev->cv);
251
252 /*
253 * Insert the incomplete device structure so that other
254 * fibrils will not race with us when we drop the mutex
255 * below.
256 */
257 hash_table_insert(&devices, key, &dev->link);
258
259 /*
260 * Drop the mutex to allow recursive devfs requests.
261 */
262 fibril_mutex_unlock(&devices_mutex);
263
264 async_sess_t *sess = devmap_device_connect(EXCHANGE_SERIALIZE,
265 node->handle, 0);
266
267 fibril_mutex_lock(&devices_mutex);
268
269 /*
270 * Notify possible waiters about this device structure
271 * being completed (or destroyed).
272 */
273 fibril_condvar_broadcast(&dev->cv);
274
275 if (!sess) {
276 /*
277 * Connecting failed, need to remove the
278 * entry and free the device structure.
279 */
280 hash_table_remove(&devices, key, DEVICES_KEYS);
281 fibril_mutex_unlock(&devices_mutex);
282
283 return ENOENT;
284 }
285
286 /* Set the correct session. */
287 dev->sess = sess;
288 } else {
289 device_t *dev = hash_table_get_instance(lnk, device_t, link);
290
291 if (!dev->sess) {
292 /*
293 * Wait until the device structure is completed
294 * and start from the beginning as the device
295 * structure might have entirely disappeared
296 * while we were not holding the mutex in
297 * fibril_condvar_wait().
298 */
299 fibril_condvar_wait(&dev->cv, &devices_mutex);
300 goto restart;
301 }
302
303 dev->refcount++;
304 }
305
306 fibril_mutex_unlock(&devices_mutex);
307
308 return EOK;
309 }
310
311 return ENOENT;
312}
313
314static int devfs_node_put(fs_node_t *fn)
315{
316 free(fn->data);
317 free(fn);
318 return EOK;
319}
320
321static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
322{
323 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
324
325 *rfn = NULL;
326 return ENOTSUP;
327}
328
329static int devfs_destroy_node(fs_node_t *fn)
330{
331 return ENOTSUP;
332}
333
334static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
335{
336 return ENOTSUP;
337}
338
339static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
340{
341 return ENOTSUP;
342}
343
344static int devfs_has_children(bool *has_children, fs_node_t *fn)
345{
346 devfs_node_t *node = (devfs_node_t *) fn->data;
347
348 if (node->handle == 0) {
349 size_t count = devmap_count_namespaces();
350 if (count > 0) {
351 *has_children = true;
352 return EOK;
353 }
354
355 /* Root namespace */
356 devmap_handle_t namespace;
357 if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
358 count = devmap_count_devices(namespace);
359 if (count > 0) {
360 *has_children = true;
361 return EOK;
362 }
363 }
364
365 *has_children = false;
366 return EOK;
367 }
368
369 if (node->type == DEV_HANDLE_NAMESPACE) {
370 size_t count = devmap_count_devices(node->handle);
371 if (count > 0) {
372 *has_children = true;
373 return EOK;
374 }
375
376 *has_children = false;
377 return EOK;
378 }
379
380 *has_children = false;
381 return EOK;
382}
383
384static fs_index_t devfs_index_get(fs_node_t *fn)
385{
386 devfs_node_t *node = (devfs_node_t *) fn->data;
387 return node->handle;
388}
389
390static aoff64_t devfs_size_get(fs_node_t *fn)
391{
392 return 0;
393}
394
395static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
396{
397 devfs_node_t *node = (devfs_node_t *) fn->data;
398
399 if (node->handle == 0)
400 return 0;
401
402 return 1;
403}
404
405static char devfs_plb_get_char(unsigned pos)
406{
407 return devfs_reg.plb_ro[pos % PLB_SIZE];
408}
409
410static bool devfs_is_directory(fs_node_t *fn)
411{
412 devfs_node_t *node = (devfs_node_t *) fn->data;
413
414 return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
415}
416
417static bool devfs_is_file(fs_node_t *fn)
418{
419 devfs_node_t *node = (devfs_node_t *) fn->data;
420
421 return (node->type == DEV_HANDLE_DEVICE);
422}
423
424static devmap_handle_t devfs_device_get(fs_node_t *fn)
425{
426 devfs_node_t *node = (devfs_node_t *) fn->data;
427
428 if (node->type == DEV_HANDLE_DEVICE)
429 return node->handle;
430
431 return 0;
432}
433
434/** libfs operations */
435libfs_ops_t devfs_libfs_ops = {
436 .root_get = devfs_root_get,
437 .match = devfs_match,
438 .node_get = devfs_node_get,
439 .node_open = devfs_node_open,
440 .node_put = devfs_node_put,
441 .create = devfs_create_node,
442 .destroy = devfs_destroy_node,
443 .link = devfs_link_node,
444 .unlink = devfs_unlink_node,
445 .has_children = devfs_has_children,
446 .index_get = devfs_index_get,
447 .size_get = devfs_size_get,
448 .lnkcnt_get = devfs_lnkcnt_get,
449 .plb_get_char = devfs_plb_get_char,
450 .is_directory = devfs_is_directory,
451 .is_file = devfs_is_file,
452 .device_get = devfs_device_get
453};
454
455bool devfs_init(void)
456{
457 if (!hash_table_create(&devices, DEVICES_BUCKETS,
458 DEVICES_KEYS, &devices_ops))
459 return false;
460
461 return true;
462}
463
464void devfs_mounted(ipc_callid_t rid, ipc_call_t *request)
465{
466 char *opts;
467
468 /* Accept the mount options */
469 sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
470 0, NULL);
471 if (retval != EOK) {
472 async_answer_0(rid, retval);
473 return;
474 }
475
476 free(opts);
477 async_answer_3(rid, EOK, 0, 0, 0);
478}
479
480void devfs_mount(ipc_callid_t rid, ipc_call_t *request)
481{
482 libfs_mount(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
483}
484
485void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
486{
487 async_answer_0(rid, ENOTSUP);
488}
489
490void devfs_unmount(ipc_callid_t rid, ipc_call_t *request)
491{
492 libfs_unmount(&devfs_libfs_ops, rid, request);
493}
494
495void devfs_lookup(ipc_callid_t rid, ipc_call_t *request)
496{
497 libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
498}
499
500void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
501{
502 libfs_open_node(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
503}
504
505void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
506{
507 libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
508}
509
510void devfs_read(ipc_callid_t rid, ipc_call_t *request)
511{
512 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
513 aoff64_t pos =
514 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
515
516 if (index == 0) {
517 ipc_callid_t callid;
518 size_t size;
519 if (!async_data_read_receive(&callid, &size)) {
520 async_answer_0(callid, EINVAL);
521 async_answer_0(rid, EINVAL);
522 return;
523 }
524
525 dev_desc_t *desc;
526 size_t count = devmap_get_namespaces(&desc);
527
528 /* Get rid of root namespace */
529 size_t i;
530 for (i = 0; i < count; i++) {
531 if (str_cmp(desc[i].name, "") == 0) {
532 if (pos >= i)
533 pos++;
534
535 break;
536 }
537 }
538
539 if (pos < count) {
540 async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
541 free(desc);
542 async_answer_1(rid, EOK, 1);
543 return;
544 }
545
546 free(desc);
547 pos -= count;
548
549 /* Search root namespace */
550 devmap_handle_t namespace;
551 if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
552 count = devmap_get_devices(namespace, &desc);
553
554 if (pos < count) {
555 async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
556 free(desc);
557 async_answer_1(rid, EOK, 1);
558 return;
559 }
560
561 free(desc);
562 }
563
564 async_answer_0(callid, ENOENT);
565 async_answer_1(rid, ENOENT, 0);
566 return;
567 }
568
569 devmap_handle_type_t type = devmap_handle_probe(index);
570
571 if (type == DEV_HANDLE_NAMESPACE) {
572 /* Namespace directory */
573 ipc_callid_t callid;
574 size_t size;
575 if (!async_data_read_receive(&callid, &size)) {
576 async_answer_0(callid, EINVAL);
577 async_answer_0(rid, EINVAL);
578 return;
579 }
580
581 dev_desc_t *desc;
582 size_t count = devmap_get_devices(index, &desc);
583
584 if (pos < count) {
585 async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
586 free(desc);
587 async_answer_1(rid, EOK, 1);
588 return;
589 }
590
591 free(desc);
592 async_answer_0(callid, ENOENT);
593 async_answer_1(rid, ENOENT, 0);
594 return;
595 }
596
597 if (type == DEV_HANDLE_DEVICE) {
598 /* Device node */
599
600 unsigned long key[] = {
601 [DEVICES_KEY_HANDLE] = (unsigned long) index
602 };
603
604 fibril_mutex_lock(&devices_mutex);
605 link_t *lnk = hash_table_find(&devices, key);
606 if (lnk == NULL) {
607 fibril_mutex_unlock(&devices_mutex);
608 async_answer_0(rid, ENOENT);
609 return;
610 }
611
612 device_t *dev = hash_table_get_instance(lnk, device_t, link);
613 assert(dev->sess);
614
615 ipc_callid_t callid;
616 if (!async_data_read_receive(&callid, NULL)) {
617 fibril_mutex_unlock(&devices_mutex);
618 async_answer_0(callid, EINVAL);
619 async_answer_0(rid, EINVAL);
620 return;
621 }
622
623 /* Make a request at the driver */
624 async_exch_t *exch = async_exchange_begin(dev->sess);
625
626 ipc_call_t answer;
627 aid_t msg = async_send_3(exch, IPC_GET_IMETHOD(*request),
628 IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
629 IPC_GET_ARG3(*request), &answer);
630
631 /* Forward the IPC_M_DATA_READ request to the driver */
632 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
633
634 async_exchange_end(exch);
635
636 fibril_mutex_unlock(&devices_mutex);
637
638 /* Wait for reply from the driver. */
639 sysarg_t rc;
640 async_wait_for(msg, &rc);
641 size_t bytes = IPC_GET_ARG1(answer);
642
643 /* Driver reply is the final result of the whole operation */
644 async_answer_1(rid, rc, bytes);
645 return;
646 }
647
648 async_answer_0(rid, ENOENT);
649}
650
651void devfs_write(ipc_callid_t rid, ipc_call_t *request)
652{
653 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
654 if (index == 0) {
655 async_answer_0(rid, ENOTSUP);
656 return;
657 }
658
659 devmap_handle_type_t type = devmap_handle_probe(index);
660
661 if (type == DEV_HANDLE_NAMESPACE) {
662 /* Namespace directory */
663 async_answer_0(rid, ENOTSUP);
664 return;
665 }
666
667 if (type == DEV_HANDLE_DEVICE) {
668 /* Device node */
669 unsigned long key[] = {
670 [DEVICES_KEY_HANDLE] = (unsigned long) index
671 };
672
673 fibril_mutex_lock(&devices_mutex);
674 link_t *lnk = hash_table_find(&devices, key);
675 if (lnk == NULL) {
676 fibril_mutex_unlock(&devices_mutex);
677 async_answer_0(rid, ENOENT);
678 return;
679 }
680
681 device_t *dev = hash_table_get_instance(lnk, device_t, link);
682 assert(dev->sess);
683
684 ipc_callid_t callid;
685 if (!async_data_write_receive(&callid, NULL)) {
686 fibril_mutex_unlock(&devices_mutex);
687 async_answer_0(callid, EINVAL);
688 async_answer_0(rid, EINVAL);
689 return;
690 }
691
692 /* Make a request at the driver */
693 async_exch_t *exch = async_exchange_begin(dev->sess);
694
695 ipc_call_t answer;
696 aid_t msg = async_send_3(exch, IPC_GET_IMETHOD(*request),
697 IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
698 IPC_GET_ARG3(*request), &answer);
699
700 /* Forward the IPC_M_DATA_WRITE request to the driver */
701 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
702
703 async_exchange_end(exch);
704
705 fibril_mutex_unlock(&devices_mutex);
706
707 /* Wait for reply from the driver. */
708 sysarg_t rc;
709 async_wait_for(msg, &rc);
710 size_t bytes = IPC_GET_ARG1(answer);
711
712 /* Driver reply is the final result of the whole operation */
713 async_answer_1(rid, rc, bytes);
714 return;
715 }
716
717 async_answer_0(rid, ENOENT);
718}
719
720void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
721{
722 async_answer_0(rid, ENOTSUP);
723}
724
725void devfs_close(ipc_callid_t rid, ipc_call_t *request)
726{
727 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
728
729 if (index == 0) {
730 async_answer_0(rid, EOK);
731 return;
732 }
733
734 devmap_handle_type_t type = devmap_handle_probe(index);
735
736 if (type == DEV_HANDLE_NAMESPACE) {
737 /* Namespace directory */
738 async_answer_0(rid, EOK);
739 return;
740 }
741
742 if (type == DEV_HANDLE_DEVICE) {
743 unsigned long key[] = {
744 [DEVICES_KEY_HANDLE] = (unsigned long) index
745 };
746
747 fibril_mutex_lock(&devices_mutex);
748 link_t *lnk = hash_table_find(&devices, key);
749 if (lnk == NULL) {
750 fibril_mutex_unlock(&devices_mutex);
751 async_answer_0(rid, ENOENT);
752 return;
753 }
754
755 device_t *dev = hash_table_get_instance(lnk, device_t, link);
756 assert(dev->sess);
757 dev->refcount--;
758
759 if (dev->refcount == 0) {
760 async_hangup(dev->sess);
761 hash_table_remove(&devices, key, DEVICES_KEYS);
762 }
763
764 fibril_mutex_unlock(&devices_mutex);
765
766 async_answer_0(rid, EOK);
767 return;
768 }
769
770 async_answer_0(rid, ENOENT);
771}
772
773void devfs_sync(ipc_callid_t rid, ipc_call_t *request)
774{
775 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
776
777 if (index == 0) {
778 async_answer_0(rid, EOK);
779 return;
780 }
781
782 devmap_handle_type_t type = devmap_handle_probe(index);
783
784 if (type == DEV_HANDLE_NAMESPACE) {
785 /* Namespace directory */
786 async_answer_0(rid, EOK);
787 return;
788 }
789
790 if (type == DEV_HANDLE_DEVICE) {
791 unsigned long key[] = {
792 [DEVICES_KEY_HANDLE] = (unsigned long) index
793 };
794
795 fibril_mutex_lock(&devices_mutex);
796 link_t *lnk = hash_table_find(&devices, key);
797 if (lnk == NULL) {
798 fibril_mutex_unlock(&devices_mutex);
799 async_answer_0(rid, ENOENT);
800 return;
801 }
802
803 device_t *dev = hash_table_get_instance(lnk, device_t, link);
804 assert(dev->sess);
805
806 /* Make a request at the driver */
807 async_exch_t *exch = async_exchange_begin(dev->sess);
808
809 ipc_call_t answer;
810 aid_t msg = async_send_2(exch, IPC_GET_IMETHOD(*request),
811 IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
812
813 async_exchange_end(exch);
814
815 fibril_mutex_unlock(&devices_mutex);
816
817 /* Wait for reply from the driver */
818 sysarg_t rc;
819 async_wait_for(msg, &rc);
820
821 /* Driver reply is the final result of the whole operation */
822 async_answer_0(rid, rc);
823 return;
824 }
825
826 async_answer_0(rid, ENOENT);
827}
828
829void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
830{
831 async_answer_0(rid, ENOTSUP);
832}
833
834/**
835 * @}
836 */
Note: See TracBrowser for help on using the repository browser.