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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f3e5b5 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
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;
[79ae36dd]61 async_sess_t *sess; /**< If NULL, the structure is incomplete. */
[17fd1d4]62 size_t refcount;
63 link_t link;
[79ae36dd]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;
[b366a1bc]132 int ret;
[a095d20]133
[1313ee9]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) {
[b366a1bc]148 ret = devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
[1313ee9]149 free(devs);
[b366a1bc]150 return ret;
[1313ee9]151 }
152 }
153
154 free(devs);
155 }
156
157 /* Search root namespace */
[991f645]158 devmap_handle_t namespace;
[1313ee9]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) {
[b366a1bc]166 ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
[1313ee9]167 free(devs);
[b366a1bc]168 return ret;
[1313ee9]169 }
170 }
171
172 free(devs);
173 }
174 }
175
176 *rfn = NULL;
177 return EOK;
[a095d20]178 }
179
[1313ee9]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) {
[b366a1bc]189 ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
[1313ee9]190 free(devs);
[b366a1bc]191 return ret;
[1313ee9]192 }
193 }
194
195 free(devs);
196 }
197
198 *rfn = NULL;
199 return EOK;
[a095d20]200 }
201
[1313ee9]202 *rfn = NULL;
203 return EOK;
[a095d20]204}
205
[991f645]206static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
[a095d20]207{
[1313ee9]208 return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
[a095d20]209}
210
[1313ee9]211static int devfs_node_open(fs_node_t *fn)
[a095d20]212{
[1313ee9]213 devfs_node_t *node = (devfs_node_t *) fn->data;
[a095d20]214
[1313ee9]215 if (node->handle == 0) {
216 /* Root directory */
217 return EOK;
[a095d20]218 }
219
[1313ee9]220 devmap_handle_type_t type = devmap_handle_probe(node->handle);
[a095d20]221
[1313ee9]222 if (type == DEV_HANDLE_NAMESPACE) {
223 /* Namespace directory */
224 return EOK;
[a095d20]225 }
226
[1313ee9]227 if (type == DEV_HANDLE_DEVICE) {
228 /* Device node */
229
230 unsigned long key[] = {
231 [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
232 };
[a7e04d16]233 link_t *lnk;
[79ae36dd]234
[1313ee9]235 fibril_mutex_lock(&devices_mutex);
[a7e04d16]236restart:
237 lnk = hash_table_find(&devices, key);
[1313ee9]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;
[a095d20]243 }
244
[a7e04d16]245 dev->handle = node->handle;
[79ae36dd]246
247 /* Mark as incomplete */
248 dev->sess = NULL;
[a7e04d16]249 dev->refcount = 1;
250 fibril_condvar_initialize(&dev->cv);
[79ae36dd]251
[a7e04d16]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);
[79ae36dd]258
[a7e04d16]259 /*
260 * Drop the mutex to allow recursive devfs requests.
261 */
262 fibril_mutex_unlock(&devices_mutex);
[79ae36dd]263
264 async_sess_t *sess = devmap_device_connect(EXCHANGE_SERIALIZE,
265 node->handle, 0);
266
[a7e04d16]267 fibril_mutex_lock(&devices_mutex);
[79ae36dd]268
[a7e04d16]269 /*
270 * Notify possible waiters about this device structure
271 * being completed (or destroyed).
272 */
273 fibril_condvar_broadcast(&dev->cv);
[79ae36dd]274
275 if (!sess) {
[a7e04d16]276 /*
277 * Connecting failed, need to remove the
278 * entry and free the device structure.
279 */
280 hash_table_remove(&devices, key, DEVICES_KEYS);
[2dfd9fa]281 fibril_mutex_unlock(&devices_mutex);
[79ae36dd]282
[1313ee9]283 return ENOENT;
[17fd1d4]284 }
285
[79ae36dd]286 /* Set the correct session. */
287 dev->sess = sess;
[1313ee9]288 } else {
289 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[79ae36dd]290
291 if (!dev->sess) {
[a7e04d16]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
[1313ee9]303 dev->refcount++;
304 }
305
306 fibril_mutex_unlock(&devices_mutex);
307
308 return EOK;
[a095d20]309 }
[1313ee9]310
311 return ENOENT;
[a095d20]312}
313
[1313ee9]314static int devfs_node_put(fs_node_t *fn)
[a095d20]315{
[1313ee9]316 free(fn->data);
317 free(fn);
318 return EOK;
319}
320
[991f645]321static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
[1313ee9]322{
323 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
[a095d20]324
[1313ee9]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;
[a095d20]347
[1313ee9]348 if (node->handle == 0) {
349 size_t count = devmap_count_namespaces();
350 if (count > 0) {
351 *has_children = true;
352 return EOK;
[17fd1d4]353 }
354
[1313ee9]355 /* Root namespace */
[991f645]356 devmap_handle_t namespace;
[1313ee9]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 }
[17fd1d4]363 }
364
[1313ee9]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 }
[17fd1d4]375
[1313ee9]376 *has_children = false;
377 return EOK;
[a095d20]378 }
379
[1313ee9]380 *has_children = false;
381 return EOK;
[17fd1d4]382}
383
[1313ee9]384static fs_index_t devfs_index_get(fs_node_t *fn)
[17fd1d4]385{
[1313ee9]386 devfs_node_t *node = (devfs_node_t *) fn->data;
387 return node->handle;
388}
389
[ed903174]390static aoff64_t devfs_size_get(fs_node_t *fn)
[1313ee9]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;
[17fd1d4]398
[1313ee9]399 if (node->handle == 0)
400 return 0;
401
402 return 1;
403}
[852b801]404
[1313ee9]405static char devfs_plb_get_char(unsigned pos)
406{
407 return devfs_reg.plb_ro[pos % PLB_SIZE];
408}
[852b801]409
[1313ee9]410static bool devfs_is_directory(fs_node_t *fn)
411{
412 devfs_node_t *node = (devfs_node_t *) fn->data;
[852b801]413
[1313ee9]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
[991f645]424static devmap_handle_t devfs_device_get(fs_node_t *fn)
[1313ee9]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 */
[96b02eb9]469 sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
[eda925a]470 0, NULL);
[1313ee9]471 if (retval != EOK) {
[ffa2c8ef]472 async_answer_0(rid, retval);
[1313ee9]473 return;
[852b801]474 }
[1313ee9]475
476 free(opts);
[ffa2c8ef]477 async_answer_3(rid, EOK, 0, 0, 0);
[1313ee9]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
[3c11713]485void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
486{
[ffa2c8ef]487 async_answer_0(rid, ENOTSUP);
[3c11713]488}
489
490void devfs_unmount(ipc_callid_t rid, ipc_call_t *request)
491{
492 libfs_unmount(&devfs_libfs_ops, rid, request);
493}
494
[1313ee9]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}
[852b801]499
[1313ee9]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);
[17fd1d4]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);
[ed903174]513 aoff64_t pos =
514 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
[17fd1d4]515
[1313ee9]516 if (index == 0) {
517 ipc_callid_t callid;
518 size_t size;
519 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]520 async_answer_0(callid, EINVAL);
521 async_answer_0(rid, EINVAL);
[1313ee9]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);
[ffa2c8ef]542 async_answer_1(rid, EOK, 1);
[1313ee9]543 return;
544 }
545
546 free(desc);
547 pos -= count;
548
549 /* Search root namespace */
[991f645]550 devmap_handle_t namespace;
[1313ee9]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);
[ffa2c8ef]557 async_answer_1(rid, EOK, 1);
[1313ee9]558 return;
559 }
560
561 free(desc);
562 }
563
[ffa2c8ef]564 async_answer_0(callid, ENOENT);
565 async_answer_1(rid, ENOENT, 0);
[1313ee9]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)) {
[ffa2c8ef]576 async_answer_0(callid, EINVAL);
577 async_answer_0(rid, EINVAL);
[1313ee9]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);
[ffa2c8ef]587 async_answer_1(rid, EOK, 1);
[1313ee9]588 return;
589 }
590
591 free(desc);
[ffa2c8ef]592 async_answer_0(callid, ENOENT);
593 async_answer_1(rid, ENOENT, 0);
[1313ee9]594 return;
595 }
596
597 if (type == DEV_HANDLE_DEVICE) {
598 /* Device node */
599
[17fd1d4]600 unsigned long key[] = {
601 [DEVICES_KEY_HANDLE] = (unsigned long) index
602 };
603
[2dfd9fa]604 fibril_mutex_lock(&devices_mutex);
[17fd1d4]605 link_t *lnk = hash_table_find(&devices, key);
606 if (lnk == NULL) {
[2dfd9fa]607 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]608 async_answer_0(rid, ENOENT);
[17fd1d4]609 return;
610 }
611
612 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[79ae36dd]613 assert(dev->sess);
[17fd1d4]614
615 ipc_callid_t callid;
[0da4e41]616 if (!async_data_read_receive(&callid, NULL)) {
[2dfd9fa]617 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]618 async_answer_0(callid, EINVAL);
619 async_answer_0(rid, EINVAL);
[17fd1d4]620 return;
621 }
622
623 /* Make a request at the driver */
[79ae36dd]624 async_exch_t *exch = async_exchange_begin(dev->sess);
625
[17fd1d4]626 ipc_call_t answer;
[79ae36dd]627 aid_t msg = async_send_3(exch, IPC_GET_IMETHOD(*request),
[17fd1d4]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 */
[79ae36dd]632 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
633
634 async_exchange_end(exch);
635
[2dfd9fa]636 fibril_mutex_unlock(&devices_mutex);
[17fd1d4]637
638 /* Wait for reply from the driver. */
[96b02eb9]639 sysarg_t rc;
[17fd1d4]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 */
[ffa2c8ef]644 async_answer_1(rid, rc, bytes);
[1313ee9]645 return;
[a095d20]646 }
[1313ee9]647
[ffa2c8ef]648 async_answer_0(rid, ENOENT);
[a095d20]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);
[1313ee9]654 if (index == 0) {
[ffa2c8ef]655 async_answer_0(rid, ENOTSUP);
[1313ee9]656 return;
657 }
658
659 devmap_handle_type_t type = devmap_handle_probe(index);
660
661 if (type == DEV_HANDLE_NAMESPACE) {
662 /* Namespace directory */
[ffa2c8ef]663 async_answer_0(rid, ENOTSUP);
[1313ee9]664 return;
665 }
666
667 if (type == DEV_HANDLE_DEVICE) {
668 /* Device node */
[17fd1d4]669 unsigned long key[] = {
670 [DEVICES_KEY_HANDLE] = (unsigned long) index
671 };
672
[2dfd9fa]673 fibril_mutex_lock(&devices_mutex);
[17fd1d4]674 link_t *lnk = hash_table_find(&devices, key);
675 if (lnk == NULL) {
[2dfd9fa]676 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]677 async_answer_0(rid, ENOENT);
[17fd1d4]678 return;
679 }
680
681 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[79ae36dd]682 assert(dev->sess);
[17fd1d4]683
684 ipc_callid_t callid;
[0da4e41]685 if (!async_data_write_receive(&callid, NULL)) {
[2dfd9fa]686 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]687 async_answer_0(callid, EINVAL);
688 async_answer_0(rid, EINVAL);
[17fd1d4]689 return;
690 }
691
692 /* Make a request at the driver */
[79ae36dd]693 async_exch_t *exch = async_exchange_begin(dev->sess);
694
[17fd1d4]695 ipc_call_t answer;
[79ae36dd]696 aid_t msg = async_send_3(exch, IPC_GET_IMETHOD(*request),
[17fd1d4]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 */
[79ae36dd]701 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
702
703 async_exchange_end(exch);
[17fd1d4]704
[2dfd9fa]705 fibril_mutex_unlock(&devices_mutex);
706
[17fd1d4]707 /* Wait for reply from the driver. */
[96b02eb9]708 sysarg_t rc;
[17fd1d4]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 */
[ffa2c8ef]713 async_answer_1(rid, rc, bytes);
[1313ee9]714 return;
[a095d20]715 }
[1313ee9]716
[ffa2c8ef]717 async_answer_0(rid, ENOENT);
[a095d20]718}
719
720void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
721{
[ffa2c8ef]722 async_answer_0(rid, ENOTSUP);
[a095d20]723}
724
[17fd1d4]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
[1313ee9]729 if (index == 0) {
[ffa2c8ef]730 async_answer_0(rid, EOK);
[1313ee9]731 return;
732 }
733
734 devmap_handle_type_t type = devmap_handle_probe(index);
735
736 if (type == DEV_HANDLE_NAMESPACE) {
737 /* Namespace directory */
[ffa2c8ef]738 async_answer_0(rid, EOK);
[1313ee9]739 return;
740 }
741
742 if (type == DEV_HANDLE_DEVICE) {
[17fd1d4]743 unsigned long key[] = {
744 [DEVICES_KEY_HANDLE] = (unsigned long) index
745 };
746
[2dfd9fa]747 fibril_mutex_lock(&devices_mutex);
[17fd1d4]748 link_t *lnk = hash_table_find(&devices, key);
749 if (lnk == NULL) {
[2dfd9fa]750 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]751 async_answer_0(rid, ENOENT);
[17fd1d4]752 return;
753 }
754
755 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[79ae36dd]756 assert(dev->sess);
[17fd1d4]757 dev->refcount--;
758
759 if (dev->refcount == 0) {
[79ae36dd]760 async_hangup(dev->sess);
[17fd1d4]761 hash_table_remove(&devices, key, DEVICES_KEYS);
762 }
763
[2dfd9fa]764 fibril_mutex_unlock(&devices_mutex);
765
[ffa2c8ef]766 async_answer_0(rid, EOK);
[1313ee9]767 return;
768 }
769
[ffa2c8ef]770 async_answer_0(rid, ENOENT);
[17fd1d4]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
[1313ee9]777 if (index == 0) {
[ffa2c8ef]778 async_answer_0(rid, EOK);
[1313ee9]779 return;
780 }
781
782 devmap_handle_type_t type = devmap_handle_probe(index);
783
784 if (type == DEV_HANDLE_NAMESPACE) {
785 /* Namespace directory */
[ffa2c8ef]786 async_answer_0(rid, EOK);
[1313ee9]787 return;
788 }
789
790 if (type == DEV_HANDLE_DEVICE) {
[17fd1d4]791 unsigned long key[] = {
792 [DEVICES_KEY_HANDLE] = (unsigned long) index
793 };
794
[2dfd9fa]795 fibril_mutex_lock(&devices_mutex);
[17fd1d4]796 link_t *lnk = hash_table_find(&devices, key);
797 if (lnk == NULL) {
[2dfd9fa]798 fibril_mutex_unlock(&devices_mutex);
[ffa2c8ef]799 async_answer_0(rid, ENOENT);
[17fd1d4]800 return;
801 }
802
803 device_t *dev = hash_table_get_instance(lnk, device_t, link);
[79ae36dd]804 assert(dev->sess);
[17fd1d4]805
806 /* Make a request at the driver */
[79ae36dd]807 async_exch_t *exch = async_exchange_begin(dev->sess);
808
[17fd1d4]809 ipc_call_t answer;
[79ae36dd]810 aid_t msg = async_send_2(exch, IPC_GET_IMETHOD(*request),
[17fd1d4]811 IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
812
[79ae36dd]813 async_exchange_end(exch);
814
[2dfd9fa]815 fibril_mutex_unlock(&devices_mutex);
816
[17fd1d4]817 /* Wait for reply from the driver */
[96b02eb9]818 sysarg_t rc;
[17fd1d4]819 async_wait_for(msg, &rc);
820
821 /* Driver reply is the final result of the whole operation */
[ffa2c8ef]822 async_answer_0(rid, rc);
[1313ee9]823 return;
824 }
825
[ffa2c8ef]826 async_answer_0(rid, ENOENT);
[17fd1d4]827}
828
[a095d20]829void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
830{
[ffa2c8ef]831 async_answer_0(rid, ENOTSUP);
[a095d20]832}
833
834/**
835 * @}
[17fd1d4]836 */
Note: See TracBrowser for help on using the repository browser.