| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Martin Decky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file devfs_ops.c
|
|---|
| 35 | * @brief Implementation of VFS operations for the devfs file system server.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <macros.h>
|
|---|
| 39 | #include <bool.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 | #include <libfs.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include <adt/hash_table.h>
|
|---|
| 46 | #include <ipc/devmap.h>
|
|---|
| 47 | #include <sys/stat.h>
|
|---|
| 48 | #include <libfs.h>
|
|---|
| 49 | #include <assert.h>
|
|---|
| 50 | #include "devfs.h"
|
|---|
| 51 | #include "devfs_ops.h"
|
|---|
| 52 |
|
|---|
| 53 | typedef struct {
|
|---|
| 54 | devmap_handle_type_t type;
|
|---|
| 55 | devmap_handle_t handle;
|
|---|
| 56 | } devfs_node_t;
|
|---|
| 57 |
|
|---|
| 58 | /** Opened devices structure */
|
|---|
| 59 | typedef struct {
|
|---|
| 60 | devmap_handle_t handle;
|
|---|
| 61 | int phone; /**< When < 0, the structure is incomplete. */
|
|---|
| 62 | size_t refcount;
|
|---|
| 63 | link_t link;
|
|---|
| 64 | fibril_condvar_t cv; /**< Broadcast when completed. */
|
|---|
| 65 | } device_t;
|
|---|
| 66 |
|
|---|
| 67 | /** Hash table of opened devices */
|
|---|
| 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 | int ret;
|
|---|
| 133 |
|
|---|
| 134 | if (node->handle == 0) {
|
|---|
| 135 | /* Root directory */
|
|---|
| 136 |
|
|---|
| 137 | dev_desc_t *devs;
|
|---|
| 138 | size_t count = devmap_get_namespaces(&devs);
|
|---|
| 139 |
|
|---|
| 140 | if (count > 0) {
|
|---|
| 141 | size_t pos;
|
|---|
| 142 | for (pos = 0; pos < count; pos++) {
|
|---|
| 143 | /* Ignore root namespace */
|
|---|
| 144 | if (str_cmp(devs[pos].name, "") == 0)
|
|---|
| 145 | continue;
|
|---|
| 146 |
|
|---|
| 147 | if (str_cmp(devs[pos].name, component) == 0) {
|
|---|
| 148 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
|
|---|
| 149 | free(devs);
|
|---|
| 150 | return ret;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | free(devs);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /* Search root namespace */
|
|---|
| 158 | devmap_handle_t namespace;
|
|---|
| 159 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
|---|
| 160 | count = devmap_get_devices(namespace, &devs);
|
|---|
| 161 |
|
|---|
| 162 | if (count > 0) {
|
|---|
| 163 | size_t pos;
|
|---|
| 164 | for (pos = 0; pos < count; pos++) {
|
|---|
| 165 | if (str_cmp(devs[pos].name, component) == 0) {
|
|---|
| 166 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
|---|
| 167 | free(devs);
|
|---|
| 168 | return ret;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | free(devs);
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | *rfn = NULL;
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (node->type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 181 | /* Namespace directory */
|
|---|
| 182 |
|
|---|
| 183 | dev_desc_t *devs;
|
|---|
| 184 | size_t count = devmap_get_devices(node->handle, &devs);
|
|---|
| 185 | if (count > 0) {
|
|---|
| 186 | size_t pos;
|
|---|
| 187 | for (pos = 0; pos < count; pos++) {
|
|---|
| 188 | if (str_cmp(devs[pos].name, component) == 0) {
|
|---|
| 189 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
|---|
| 190 | free(devs);
|
|---|
| 191 | return ret;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | free(devs);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | *rfn = NULL;
|
|---|
| 199 | return EOK;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | *rfn = NULL;
|
|---|
| 203 | return EOK;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| 207 | {
|
|---|
| 208 | return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | static int devfs_node_open(fs_node_t *fn)
|
|---|
| 212 | {
|
|---|
| 213 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 214 |
|
|---|
| 215 | if (node->handle == 0) {
|
|---|
| 216 | /* Root directory */
|
|---|
| 217 | return EOK;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | devmap_handle_type_t type = devmap_handle_probe(node->handle);
|
|---|
| 221 |
|
|---|
| 222 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 223 | /* Namespace directory */
|
|---|
| 224 | return EOK;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 228 | /* Device node */
|
|---|
| 229 |
|
|---|
| 230 | unsigned long key[] = {
|
|---|
| 231 | [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
|
|---|
| 232 | };
|
|---|
| 233 | link_t *lnk;
|
|---|
| 234 |
|
|---|
| 235 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 236 | restart:
|
|---|
| 237 | lnk = hash_table_find(&devices, key);
|
|---|
| 238 | if (lnk == NULL) {
|
|---|
| 239 | device_t *dev = (device_t *) malloc(sizeof(device_t));
|
|---|
| 240 | if (dev == NULL) {
|
|---|
| 241 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 242 | return ENOMEM;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | dev->handle = node->handle;
|
|---|
| 246 | dev->phone = -1; /* mark as incomplete */
|
|---|
| 247 | dev->refcount = 1;
|
|---|
| 248 | fibril_condvar_initialize(&dev->cv);
|
|---|
| 249 |
|
|---|
| 250 | /*
|
|---|
| 251 | * Insert the incomplete device structure so that other
|
|---|
| 252 | * fibrils will not race with us when we drop the mutex
|
|---|
| 253 | * below.
|
|---|
| 254 | */
|
|---|
| 255 | hash_table_insert(&devices, key, &dev->link);
|
|---|
| 256 |
|
|---|
| 257 | /*
|
|---|
| 258 | * Drop the mutex to allow recursive devfs requests.
|
|---|
| 259 | */
|
|---|
| 260 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 261 |
|
|---|
| 262 | int phone = devmap_device_connect(node->handle, 0);
|
|---|
| 263 |
|
|---|
| 264 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 265 |
|
|---|
| 266 | /*
|
|---|
| 267 | * Notify possible waiters about this device structure
|
|---|
| 268 | * being completed (or destroyed).
|
|---|
| 269 | */
|
|---|
| 270 | fibril_condvar_broadcast(&dev->cv);
|
|---|
| 271 |
|
|---|
| 272 | if (phone < 0) {
|
|---|
| 273 | /*
|
|---|
| 274 | * Connecting failed, need to remove the
|
|---|
| 275 | * entry and free the device structure.
|
|---|
| 276 | */
|
|---|
| 277 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| 278 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 279 |
|
|---|
| 280 | return ENOENT;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /* Set the correct phone. */
|
|---|
| 284 | dev->phone = phone;
|
|---|
| 285 | } else {
|
|---|
| 286 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 287 |
|
|---|
| 288 | if (dev->phone < 0) {
|
|---|
| 289 | /*
|
|---|
| 290 | * Wait until the device structure is completed
|
|---|
| 291 | * and start from the beginning as the device
|
|---|
| 292 | * structure might have entirely disappeared
|
|---|
| 293 | * while we were not holding the mutex in
|
|---|
| 294 | * fibril_condvar_wait().
|
|---|
| 295 | */
|
|---|
| 296 | fibril_condvar_wait(&dev->cv, &devices_mutex);
|
|---|
| 297 | goto restart;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | dev->refcount++;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 304 |
|
|---|
| 305 | return EOK;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | return ENOENT;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | static int devfs_node_put(fs_node_t *fn)
|
|---|
| 312 | {
|
|---|
| 313 | free(fn->data);
|
|---|
| 314 | free(fn);
|
|---|
| 315 | return EOK;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
|---|
| 319 | {
|
|---|
| 320 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| 321 |
|
|---|
| 322 | *rfn = NULL;
|
|---|
| 323 | return ENOTSUP;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | static int devfs_destroy_node(fs_node_t *fn)
|
|---|
| 327 | {
|
|---|
| 328 | return ENOTSUP;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 332 | {
|
|---|
| 333 | return ENOTSUP;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 337 | {
|
|---|
| 338 | return ENOTSUP;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | static int devfs_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| 342 | {
|
|---|
| 343 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 344 |
|
|---|
| 345 | if (node->handle == 0) {
|
|---|
| 346 | size_t count = devmap_count_namespaces();
|
|---|
| 347 | if (count > 0) {
|
|---|
| 348 | *has_children = true;
|
|---|
| 349 | return EOK;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /* Root namespace */
|
|---|
| 353 | devmap_handle_t namespace;
|
|---|
| 354 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
|---|
| 355 | count = devmap_count_devices(namespace);
|
|---|
| 356 | if (count > 0) {
|
|---|
| 357 | *has_children = true;
|
|---|
| 358 | return EOK;
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | *has_children = false;
|
|---|
| 363 | return EOK;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | if (node->type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 367 | size_t count = devmap_count_devices(node->handle);
|
|---|
| 368 | if (count > 0) {
|
|---|
| 369 | *has_children = true;
|
|---|
| 370 | return EOK;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | *has_children = false;
|
|---|
| 374 | return EOK;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | *has_children = false;
|
|---|
| 378 | return EOK;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | static fs_index_t devfs_index_get(fs_node_t *fn)
|
|---|
| 382 | {
|
|---|
| 383 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 384 | return node->handle;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | static aoff64_t devfs_size_get(fs_node_t *fn)
|
|---|
| 388 | {
|
|---|
| 389 | return 0;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
|
|---|
| 393 | {
|
|---|
| 394 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 395 |
|
|---|
| 396 | if (node->handle == 0)
|
|---|
| 397 | return 0;
|
|---|
| 398 |
|
|---|
| 399 | return 1;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | static char devfs_plb_get_char(unsigned pos)
|
|---|
| 403 | {
|
|---|
| 404 | return devfs_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | static bool devfs_is_directory(fs_node_t *fn)
|
|---|
| 408 | {
|
|---|
| 409 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 410 |
|
|---|
| 411 | return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | static bool devfs_is_file(fs_node_t *fn)
|
|---|
| 415 | {
|
|---|
| 416 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 417 |
|
|---|
| 418 | return (node->type == DEV_HANDLE_DEVICE);
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | static devmap_handle_t devfs_device_get(fs_node_t *fn)
|
|---|
| 422 | {
|
|---|
| 423 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 424 |
|
|---|
| 425 | if (node->type == DEV_HANDLE_DEVICE)
|
|---|
| 426 | return node->handle;
|
|---|
| 427 |
|
|---|
| 428 | return 0;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /** libfs operations */
|
|---|
| 432 | libfs_ops_t devfs_libfs_ops = {
|
|---|
| 433 | .root_get = devfs_root_get,
|
|---|
| 434 | .match = devfs_match,
|
|---|
| 435 | .node_get = devfs_node_get,
|
|---|
| 436 | .node_open = devfs_node_open,
|
|---|
| 437 | .node_put = devfs_node_put,
|
|---|
| 438 | .create = devfs_create_node,
|
|---|
| 439 | .destroy = devfs_destroy_node,
|
|---|
| 440 | .link = devfs_link_node,
|
|---|
| 441 | .unlink = devfs_unlink_node,
|
|---|
| 442 | .has_children = devfs_has_children,
|
|---|
| 443 | .index_get = devfs_index_get,
|
|---|
| 444 | .size_get = devfs_size_get,
|
|---|
| 445 | .lnkcnt_get = devfs_lnkcnt_get,
|
|---|
| 446 | .plb_get_char = devfs_plb_get_char,
|
|---|
| 447 | .is_directory = devfs_is_directory,
|
|---|
| 448 | .is_file = devfs_is_file,
|
|---|
| 449 | .device_get = devfs_device_get
|
|---|
| 450 | };
|
|---|
| 451 |
|
|---|
| 452 | bool devfs_init(void)
|
|---|
| 453 | {
|
|---|
| 454 | if (!hash_table_create(&devices, DEVICES_BUCKETS,
|
|---|
| 455 | DEVICES_KEYS, &devices_ops))
|
|---|
| 456 | return false;
|
|---|
| 457 |
|
|---|
| 458 | return true;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | void devfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 462 | {
|
|---|
| 463 | char *opts;
|
|---|
| 464 |
|
|---|
| 465 | /* Accept the mount options */
|
|---|
| 466 | sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
|
|---|
| 467 | 0, NULL);
|
|---|
| 468 | if (retval != EOK) {
|
|---|
| 469 | async_answer_0(rid, retval);
|
|---|
| 470 | return;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | free(opts);
|
|---|
| 474 | async_answer_3(rid, EOK, 0, 0, 0);
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | void devfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 478 | {
|
|---|
| 479 | libfs_mount(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 483 | {
|
|---|
| 484 | async_answer_0(rid, ENOTSUP);
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | void devfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 488 | {
|
|---|
| 489 | libfs_unmount(&devfs_libfs_ops, rid, request);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | void devfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 493 | {
|
|---|
| 494 | libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 498 | {
|
|---|
| 499 | libfs_open_node(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 503 | {
|
|---|
| 504 | libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | void devfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 508 | {
|
|---|
| 509 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 510 | aoff64_t pos =
|
|---|
| 511 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 512 |
|
|---|
| 513 | if (index == 0) {
|
|---|
| 514 | ipc_callid_t callid;
|
|---|
| 515 | size_t size;
|
|---|
| 516 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 517 | async_answer_0(callid, EINVAL);
|
|---|
| 518 | async_answer_0(rid, EINVAL);
|
|---|
| 519 | return;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | dev_desc_t *desc;
|
|---|
| 523 | size_t count = devmap_get_namespaces(&desc);
|
|---|
| 524 |
|
|---|
| 525 | /* Get rid of root namespace */
|
|---|
| 526 | size_t i;
|
|---|
| 527 | for (i = 0; i < count; i++) {
|
|---|
| 528 | if (str_cmp(desc[i].name, "") == 0) {
|
|---|
| 529 | if (pos >= i)
|
|---|
| 530 | pos++;
|
|---|
| 531 |
|
|---|
| 532 | break;
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | if (pos < count) {
|
|---|
| 537 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 538 | free(desc);
|
|---|
| 539 | async_answer_1(rid, EOK, 1);
|
|---|
| 540 | return;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | free(desc);
|
|---|
| 544 | pos -= count;
|
|---|
| 545 |
|
|---|
| 546 | /* Search root namespace */
|
|---|
| 547 | devmap_handle_t namespace;
|
|---|
| 548 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
|---|
| 549 | count = devmap_get_devices(namespace, &desc);
|
|---|
| 550 |
|
|---|
| 551 | if (pos < count) {
|
|---|
| 552 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 553 | free(desc);
|
|---|
| 554 | async_answer_1(rid, EOK, 1);
|
|---|
| 555 | return;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | free(desc);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | async_answer_0(callid, ENOENT);
|
|---|
| 562 | async_answer_1(rid, ENOENT, 0);
|
|---|
| 563 | return;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 567 |
|
|---|
| 568 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 569 | /* Namespace directory */
|
|---|
| 570 | ipc_callid_t callid;
|
|---|
| 571 | size_t size;
|
|---|
| 572 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 573 | async_answer_0(callid, EINVAL);
|
|---|
| 574 | async_answer_0(rid, EINVAL);
|
|---|
| 575 | return;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | dev_desc_t *desc;
|
|---|
| 579 | size_t count = devmap_get_devices(index, &desc);
|
|---|
| 580 |
|
|---|
| 581 | if (pos < count) {
|
|---|
| 582 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 583 | free(desc);
|
|---|
| 584 | async_answer_1(rid, EOK, 1);
|
|---|
| 585 | return;
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | free(desc);
|
|---|
| 589 | async_answer_0(callid, ENOENT);
|
|---|
| 590 | async_answer_1(rid, ENOENT, 0);
|
|---|
| 591 | return;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 595 | /* Device node */
|
|---|
| 596 |
|
|---|
| 597 | unsigned long key[] = {
|
|---|
| 598 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 599 | };
|
|---|
| 600 |
|
|---|
| 601 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 602 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 603 | if (lnk == NULL) {
|
|---|
| 604 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 605 | async_answer_0(rid, ENOENT);
|
|---|
| 606 | return;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 610 | assert(dev->phone >= 0);
|
|---|
| 611 |
|
|---|
| 612 | ipc_callid_t callid;
|
|---|
| 613 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| 614 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 615 | async_answer_0(callid, EINVAL);
|
|---|
| 616 | async_answer_0(rid, EINVAL);
|
|---|
| 617 | return;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | /* Make a request at the driver */
|
|---|
| 621 | ipc_call_t answer;
|
|---|
| 622 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
|---|
| 623 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
|---|
| 624 | IPC_GET_ARG3(*request), &answer);
|
|---|
| 625 |
|
|---|
| 626 | /* Forward the IPC_M_DATA_READ request to the driver */
|
|---|
| 627 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 628 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 629 |
|
|---|
| 630 | /* Wait for reply from the driver. */
|
|---|
| 631 | sysarg_t rc;
|
|---|
| 632 | async_wait_for(msg, &rc);
|
|---|
| 633 | size_t bytes = IPC_GET_ARG1(answer);
|
|---|
| 634 |
|
|---|
| 635 | /* Driver reply is the final result of the whole operation */
|
|---|
| 636 | async_answer_1(rid, rc, bytes);
|
|---|
| 637 | return;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | async_answer_0(rid, ENOENT);
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | void devfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 644 | {
|
|---|
| 645 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 646 | if (index == 0) {
|
|---|
| 647 | async_answer_0(rid, ENOTSUP);
|
|---|
| 648 | return;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 652 |
|
|---|
| 653 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 654 | /* Namespace directory */
|
|---|
| 655 | async_answer_0(rid, ENOTSUP);
|
|---|
| 656 | return;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 660 | /* Device node */
|
|---|
| 661 | unsigned long key[] = {
|
|---|
| 662 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 663 | };
|
|---|
| 664 |
|
|---|
| 665 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 666 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 667 | if (lnk == NULL) {
|
|---|
| 668 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 669 | async_answer_0(rid, ENOENT);
|
|---|
| 670 | return;
|
|---|
| 671 | }
|
|---|
| 672 |
|
|---|
| 673 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 674 | assert(dev->phone >= 0);
|
|---|
| 675 |
|
|---|
| 676 | ipc_callid_t callid;
|
|---|
| 677 | if (!async_data_write_receive(&callid, NULL)) {
|
|---|
| 678 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 679 | async_answer_0(callid, EINVAL);
|
|---|
| 680 | async_answer_0(rid, EINVAL);
|
|---|
| 681 | return;
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | /* Make a request at the driver */
|
|---|
| 685 | ipc_call_t answer;
|
|---|
| 686 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
|---|
| 687 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
|
|---|
| 688 | IPC_GET_ARG3(*request), &answer);
|
|---|
| 689 |
|
|---|
| 690 | /* Forward the IPC_M_DATA_WRITE request to the driver */
|
|---|
| 691 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 692 |
|
|---|
| 693 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 694 |
|
|---|
| 695 | /* Wait for reply from the driver. */
|
|---|
| 696 | sysarg_t rc;
|
|---|
| 697 | async_wait_for(msg, &rc);
|
|---|
| 698 | size_t bytes = IPC_GET_ARG1(answer);
|
|---|
| 699 |
|
|---|
| 700 | /* Driver reply is the final result of the whole operation */
|
|---|
| 701 | async_answer_1(rid, rc, bytes);
|
|---|
| 702 | return;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | async_answer_0(rid, ENOENT);
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 709 | {
|
|---|
| 710 | async_answer_0(rid, ENOTSUP);
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | void devfs_close(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 | async_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 | async_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 | async_answer_0(rid, ENOENT);
|
|---|
| 740 | return;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 744 | assert(dev->phone >= 0);
|
|---|
| 745 | dev->refcount--;
|
|---|
| 746 |
|
|---|
| 747 | if (dev->refcount == 0) {
|
|---|
| 748 | async_hangup(dev->phone);
|
|---|
| 749 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 753 |
|
|---|
| 754 | async_answer_0(rid, EOK);
|
|---|
| 755 | return;
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | async_answer_0(rid, ENOENT);
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| 761 | void devfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 762 | {
|
|---|
| 763 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 764 |
|
|---|
| 765 | if (index == 0) {
|
|---|
| 766 | async_answer_0(rid, EOK);
|
|---|
| 767 | return;
|
|---|
| 768 | }
|
|---|
| 769 |
|
|---|
| 770 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 771 |
|
|---|
| 772 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 773 | /* Namespace directory */
|
|---|
| 774 | async_answer_0(rid, EOK);
|
|---|
| 775 | return;
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 779 | unsigned long key[] = {
|
|---|
| 780 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 781 | };
|
|---|
| 782 |
|
|---|
| 783 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 784 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 785 | if (lnk == NULL) {
|
|---|
| 786 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 787 | async_answer_0(rid, ENOENT);
|
|---|
| 788 | return;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 792 | assert(dev->phone >= 0);
|
|---|
| 793 |
|
|---|
| 794 | /* Make a request at the driver */
|
|---|
| 795 | ipc_call_t answer;
|
|---|
| 796 | aid_t msg = async_send_2(dev->phone, IPC_GET_IMETHOD(*request),
|
|---|
| 797 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
|
|---|
| 798 |
|
|---|
| 799 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 800 |
|
|---|
| 801 | /* Wait for reply from the driver */
|
|---|
| 802 | sysarg_t rc;
|
|---|
| 803 | async_wait_for(msg, &rc);
|
|---|
| 804 |
|
|---|
| 805 | /* Driver reply is the final result of the whole operation */
|
|---|
| 806 | async_answer_0(rid, rc);
|
|---|
| 807 | return;
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | async_answer_0(rid, ENOENT);
|
|---|
| 811 | }
|
|---|
| 812 |
|
|---|
| 813 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 814 | {
|
|---|
| 815 | async_answer_0(rid, ENOTSUP);
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | /**
|
|---|
| 819 | * @}
|
|---|
| 820 | */
|
|---|