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