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_data_string_receive(&opts, 0);
|
---|
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_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
437 | {
|
---|
438 | libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
439 | }
|
---|
440 |
|
---|
441 | void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
442 | {
|
---|
443 | libfs_open_node(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
444 | }
|
---|
445 |
|
---|
446 | void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
447 | {
|
---|
448 | libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
---|
449 | }
|
---|
450 |
|
---|
451 | void devfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
452 | {
|
---|
453 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
454 | off_t pos = (off_t) IPC_GET_ARG3(*request);
|
---|
455 |
|
---|
456 | if (index == 0) {
|
---|
457 | ipc_callid_t callid;
|
---|
458 | size_t size;
|
---|
459 | if (!async_data_read_receive(&callid, &size)) {
|
---|
460 | ipc_answer_0(callid, EINVAL);
|
---|
461 | ipc_answer_0(rid, EINVAL);
|
---|
462 | return;
|
---|
463 | }
|
---|
464 |
|
---|
465 | dev_desc_t *desc;
|
---|
466 | size_t count = devmap_get_namespaces(&desc);
|
---|
467 |
|
---|
468 | /* Get rid of root namespace */
|
---|
469 | size_t i;
|
---|
470 | for (i = 0; i < count; i++) {
|
---|
471 | if (str_cmp(desc[i].name, "") == 0) {
|
---|
472 | if (pos >= i)
|
---|
473 | pos++;
|
---|
474 |
|
---|
475 | break;
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | if (pos < count) {
|
---|
480 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
481 | free(desc);
|
---|
482 | ipc_answer_1(rid, EOK, 1);
|
---|
483 | return;
|
---|
484 | }
|
---|
485 |
|
---|
486 | free(desc);
|
---|
487 | pos -= count;
|
---|
488 |
|
---|
489 | /* Search root namespace */
|
---|
490 | dev_handle_t namespace;
|
---|
491 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
---|
492 | count = devmap_get_devices(namespace, &desc);
|
---|
493 |
|
---|
494 | if (pos < count) {
|
---|
495 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
496 | free(desc);
|
---|
497 | ipc_answer_1(rid, EOK, 1);
|
---|
498 | return;
|
---|
499 | }
|
---|
500 |
|
---|
501 | free(desc);
|
---|
502 | }
|
---|
503 |
|
---|
504 | ipc_answer_0(callid, ENOENT);
|
---|
505 | ipc_answer_1(rid, ENOENT, 0);
|
---|
506 | return;
|
---|
507 | }
|
---|
508 |
|
---|
509 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
510 |
|
---|
511 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
512 | /* Namespace directory */
|
---|
513 | ipc_callid_t callid;
|
---|
514 | size_t size;
|
---|
515 | if (!async_data_read_receive(&callid, &size)) {
|
---|
516 | ipc_answer_0(callid, EINVAL);
|
---|
517 | ipc_answer_0(rid, EINVAL);
|
---|
518 | return;
|
---|
519 | }
|
---|
520 |
|
---|
521 | dev_desc_t *desc;
|
---|
522 | size_t count = devmap_get_devices(index, &desc);
|
---|
523 |
|
---|
524 | if (pos < count) {
|
---|
525 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
---|
526 | free(desc);
|
---|
527 | ipc_answer_1(rid, EOK, 1);
|
---|
528 | return;
|
---|
529 | }
|
---|
530 |
|
---|
531 | free(desc);
|
---|
532 | ipc_answer_0(callid, ENOENT);
|
---|
533 | ipc_answer_1(rid, ENOENT, 0);
|
---|
534 | return;
|
---|
535 | }
|
---|
536 |
|
---|
537 | if (type == DEV_HANDLE_DEVICE) {
|
---|
538 | /* Device node */
|
---|
539 |
|
---|
540 | unsigned long key[] = {
|
---|
541 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
542 | };
|
---|
543 |
|
---|
544 | fibril_mutex_lock(&devices_mutex);
|
---|
545 | link_t *lnk = hash_table_find(&devices, key);
|
---|
546 | if (lnk == NULL) {
|
---|
547 | fibril_mutex_unlock(&devices_mutex);
|
---|
548 | ipc_answer_0(rid, ENOENT);
|
---|
549 | return;
|
---|
550 | }
|
---|
551 |
|
---|
552 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
553 |
|
---|
554 | ipc_callid_t callid;
|
---|
555 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
556 | fibril_mutex_unlock(&devices_mutex);
|
---|
557 | ipc_answer_0(callid, EINVAL);
|
---|
558 | ipc_answer_0(rid, EINVAL);
|
---|
559 | return;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /* Make a request at the driver */
|
---|
563 | ipc_call_t answer;
|
---|
564 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request),
|
---|
565 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
---|
566 | IPC_GET_ARG3(*request), &answer);
|
---|
567 |
|
---|
568 | /* Forward the IPC_M_DATA_READ request to the driver */
|
---|
569 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
570 | fibril_mutex_unlock(&devices_mutex);
|
---|
571 |
|
---|
572 | /* Wait for reply from the driver. */
|
---|
573 | ipcarg_t rc;
|
---|
574 | async_wait_for(msg, &rc);
|
---|
575 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
576 |
|
---|
577 | /* Driver reply is the final result of the whole operation */
|
---|
578 | ipc_answer_1(rid, rc, bytes);
|
---|
579 | return;
|
---|
580 | }
|
---|
581 |
|
---|
582 | ipc_answer_0(rid, ENOENT);
|
---|
583 | }
|
---|
584 |
|
---|
585 | void devfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
586 | {
|
---|
587 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
588 | off_t pos = (off_t) IPC_GET_ARG3(*request);
|
---|
589 |
|
---|
590 | if (index == 0) {
|
---|
591 | ipc_answer_0(rid, ENOTSUP);
|
---|
592 | return;
|
---|
593 | }
|
---|
594 |
|
---|
595 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
596 |
|
---|
597 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
598 | /* Namespace directory */
|
---|
599 | ipc_answer_0(rid, ENOTSUP);
|
---|
600 | return;
|
---|
601 | }
|
---|
602 |
|
---|
603 | if (type == DEV_HANDLE_DEVICE) {
|
---|
604 | /* Device node */
|
---|
605 | unsigned long key[] = {
|
---|
606 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
607 | };
|
---|
608 |
|
---|
609 | fibril_mutex_lock(&devices_mutex);
|
---|
610 | link_t *lnk = hash_table_find(&devices, key);
|
---|
611 | if (lnk == NULL) {
|
---|
612 | fibril_mutex_unlock(&devices_mutex);
|
---|
613 | ipc_answer_0(rid, ENOENT);
|
---|
614 | return;
|
---|
615 | }
|
---|
616 |
|
---|
617 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
618 |
|
---|
619 | ipc_callid_t callid;
|
---|
620 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
621 | fibril_mutex_unlock(&devices_mutex);
|
---|
622 | ipc_answer_0(callid, EINVAL);
|
---|
623 | ipc_answer_0(rid, EINVAL);
|
---|
624 | return;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /* Make a request at the driver */
|
---|
628 | ipc_call_t answer;
|
---|
629 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request),
|
---|
630 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
---|
631 | IPC_GET_ARG3(*request), &answer);
|
---|
632 |
|
---|
633 | /* Forward the IPC_M_DATA_WRITE request to the driver */
|
---|
634 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
635 |
|
---|
636 | fibril_mutex_unlock(&devices_mutex);
|
---|
637 |
|
---|
638 | /* Wait for reply from the driver. */
|
---|
639 | ipcarg_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 | ipc_answer_1(rid, rc, bytes);
|
---|
645 | return;
|
---|
646 | }
|
---|
647 |
|
---|
648 | ipc_answer_0(rid, ENOENT);
|
---|
649 | }
|
---|
650 |
|
---|
651 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
652 | {
|
---|
653 | ipc_answer_0(rid, ENOTSUP);
|
---|
654 | }
|
---|
655 |
|
---|
656 | void devfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
657 | {
|
---|
658 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
659 |
|
---|
660 | if (index == 0) {
|
---|
661 | ipc_answer_0(rid, EOK);
|
---|
662 | return;
|
---|
663 | }
|
---|
664 |
|
---|
665 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
666 |
|
---|
667 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
668 | /* Namespace directory */
|
---|
669 | ipc_answer_0(rid, EOK);
|
---|
670 | return;
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (type == DEV_HANDLE_DEVICE) {
|
---|
674 | unsigned long key[] = {
|
---|
675 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
676 | };
|
---|
677 |
|
---|
678 | fibril_mutex_lock(&devices_mutex);
|
---|
679 | link_t *lnk = hash_table_find(&devices, key);
|
---|
680 | if (lnk == NULL) {
|
---|
681 | fibril_mutex_unlock(&devices_mutex);
|
---|
682 | ipc_answer_0(rid, ENOENT);
|
---|
683 | return;
|
---|
684 | }
|
---|
685 |
|
---|
686 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
687 | dev->refcount--;
|
---|
688 |
|
---|
689 | if (dev->refcount == 0) {
|
---|
690 | ipc_hangup(dev->phone);
|
---|
691 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
---|
692 | }
|
---|
693 |
|
---|
694 | fibril_mutex_unlock(&devices_mutex);
|
---|
695 |
|
---|
696 | ipc_answer_0(rid, EOK);
|
---|
697 | return;
|
---|
698 | }
|
---|
699 |
|
---|
700 | ipc_answer_0(rid, ENOENT);
|
---|
701 | }
|
---|
702 |
|
---|
703 | void devfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
704 | {
|
---|
705 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
706 |
|
---|
707 | if (index == 0) {
|
---|
708 | ipc_answer_0(rid, EOK);
|
---|
709 | return;
|
---|
710 | }
|
---|
711 |
|
---|
712 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
713 |
|
---|
714 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
715 | /* Namespace directory */
|
---|
716 | ipc_answer_0(rid, EOK);
|
---|
717 | return;
|
---|
718 | }
|
---|
719 |
|
---|
720 | if (type == DEV_HANDLE_DEVICE) {
|
---|
721 | unsigned long key[] = {
|
---|
722 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
723 | };
|
---|
724 |
|
---|
725 | fibril_mutex_lock(&devices_mutex);
|
---|
726 | link_t *lnk = hash_table_find(&devices, key);
|
---|
727 | if (lnk == NULL) {
|
---|
728 | fibril_mutex_unlock(&devices_mutex);
|
---|
729 | ipc_answer_0(rid, ENOENT);
|
---|
730 | return;
|
---|
731 | }
|
---|
732 |
|
---|
733 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
734 |
|
---|
735 | /* Make a request at the driver */
|
---|
736 | ipc_call_t answer;
|
---|
737 | aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request),
|
---|
738 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
|
---|
739 |
|
---|
740 | fibril_mutex_unlock(&devices_mutex);
|
---|
741 |
|
---|
742 | /* Wait for reply from the driver */
|
---|
743 | ipcarg_t rc;
|
---|
744 | async_wait_for(msg, &rc);
|
---|
745 |
|
---|
746 | /* Driver reply is the final result of the whole operation */
|
---|
747 | ipc_answer_0(rid, rc);
|
---|
748 | return;
|
---|
749 | }
|
---|
750 |
|
---|
751 | ipc_answer_0(rid, ENOENT);
|
---|
752 | }
|
---|
753 |
|
---|
754 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
755 | {
|
---|
756 | ipc_answer_0(rid, ENOTSUP);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * @}
|
---|
761 | */
|
---|