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