| 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 | async_sess_t *sess; /**< If NULL, 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 |
|
|---|
| 247 | /* Mark as incomplete */
|
|---|
| 248 | dev->sess = NULL;
|
|---|
| 249 | dev->refcount = 1;
|
|---|
| 250 | fibril_condvar_initialize(&dev->cv);
|
|---|
| 251 |
|
|---|
| 252 | /*
|
|---|
| 253 | * Insert the incomplete device structure so that other
|
|---|
| 254 | * fibrils will not race with us when we drop the mutex
|
|---|
| 255 | * below.
|
|---|
| 256 | */
|
|---|
| 257 | hash_table_insert(&devices, key, &dev->link);
|
|---|
| 258 |
|
|---|
| 259 | /*
|
|---|
| 260 | * Drop the mutex to allow recursive devfs requests.
|
|---|
| 261 | */
|
|---|
| 262 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 263 |
|
|---|
| 264 | async_sess_t *sess = devmap_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 265 | node->handle, 0);
|
|---|
| 266 |
|
|---|
| 267 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 268 |
|
|---|
| 269 | /*
|
|---|
| 270 | * Notify possible waiters about this device structure
|
|---|
| 271 | * being completed (or destroyed).
|
|---|
| 272 | */
|
|---|
| 273 | fibril_condvar_broadcast(&dev->cv);
|
|---|
| 274 |
|
|---|
| 275 | if (!sess) {
|
|---|
| 276 | /*
|
|---|
| 277 | * Connecting failed, need to remove the
|
|---|
| 278 | * entry and free the device structure.
|
|---|
| 279 | */
|
|---|
| 280 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| 281 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 282 |
|
|---|
| 283 | return ENOENT;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /* Set the correct session. */
|
|---|
| 287 | dev->sess = sess;
|
|---|
| 288 | } else {
|
|---|
| 289 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 290 |
|
|---|
| 291 | if (!dev->sess) {
|
|---|
| 292 | /*
|
|---|
| 293 | * Wait until the device structure is completed
|
|---|
| 294 | * and start from the beginning as the device
|
|---|
| 295 | * structure might have entirely disappeared
|
|---|
| 296 | * while we were not holding the mutex in
|
|---|
| 297 | * fibril_condvar_wait().
|
|---|
| 298 | */
|
|---|
| 299 | fibril_condvar_wait(&dev->cv, &devices_mutex);
|
|---|
| 300 | goto restart;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | dev->refcount++;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 307 |
|
|---|
| 308 | return EOK;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | return ENOENT;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | static int devfs_node_put(fs_node_t *fn)
|
|---|
| 315 | {
|
|---|
| 316 | free(fn->data);
|
|---|
| 317 | free(fn);
|
|---|
| 318 | return EOK;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
|---|
| 322 | {
|
|---|
| 323 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| 324 |
|
|---|
| 325 | *rfn = NULL;
|
|---|
| 326 | return ENOTSUP;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | static int devfs_destroy_node(fs_node_t *fn)
|
|---|
| 330 | {
|
|---|
| 331 | return ENOTSUP;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 335 | {
|
|---|
| 336 | return ENOTSUP;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 340 | {
|
|---|
| 341 | return ENOTSUP;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | static int devfs_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| 345 | {
|
|---|
| 346 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 347 |
|
|---|
| 348 | if (node->handle == 0) {
|
|---|
| 349 | size_t count = devmap_count_namespaces();
|
|---|
| 350 | if (count > 0) {
|
|---|
| 351 | *has_children = true;
|
|---|
| 352 | return EOK;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /* Root namespace */
|
|---|
| 356 | devmap_handle_t namespace;
|
|---|
| 357 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
|---|
| 358 | count = devmap_count_devices(namespace);
|
|---|
| 359 | if (count > 0) {
|
|---|
| 360 | *has_children = true;
|
|---|
| 361 | return EOK;
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | *has_children = false;
|
|---|
| 366 | return EOK;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | if (node->type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 370 | size_t count = devmap_count_devices(node->handle);
|
|---|
| 371 | if (count > 0) {
|
|---|
| 372 | *has_children = true;
|
|---|
| 373 | return EOK;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | *has_children = false;
|
|---|
| 377 | return EOK;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | *has_children = false;
|
|---|
| 381 | return EOK;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | static fs_index_t devfs_index_get(fs_node_t *fn)
|
|---|
| 385 | {
|
|---|
| 386 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 387 | return node->handle;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | static aoff64_t devfs_size_get(fs_node_t *fn)
|
|---|
| 391 | {
|
|---|
| 392 | return 0;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
|
|---|
| 396 | {
|
|---|
| 397 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 398 |
|
|---|
| 399 | if (node->handle == 0)
|
|---|
| 400 | return 0;
|
|---|
| 401 |
|
|---|
| 402 | return 1;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | static bool devfs_is_directory(fs_node_t *fn)
|
|---|
| 406 | {
|
|---|
| 407 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 408 |
|
|---|
| 409 | return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | static bool devfs_is_file(fs_node_t *fn)
|
|---|
| 413 | {
|
|---|
| 414 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 415 |
|
|---|
| 416 | return (node->type == DEV_HANDLE_DEVICE);
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | static devmap_handle_t devfs_device_get(fs_node_t *fn)
|
|---|
| 420 | {
|
|---|
| 421 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 422 |
|
|---|
| 423 | if (node->type == DEV_HANDLE_DEVICE)
|
|---|
| 424 | return node->handle;
|
|---|
| 425 |
|
|---|
| 426 | return 0;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | /** libfs operations */
|
|---|
| 430 | libfs_ops_t devfs_libfs_ops = {
|
|---|
| 431 | .root_get = devfs_root_get,
|
|---|
| 432 | .match = devfs_match,
|
|---|
| 433 | .node_get = devfs_node_get,
|
|---|
| 434 | .node_open = devfs_node_open,
|
|---|
| 435 | .node_put = devfs_node_put,
|
|---|
| 436 | .create = devfs_create_node,
|
|---|
| 437 | .destroy = devfs_destroy_node,
|
|---|
| 438 | .link = devfs_link_node,
|
|---|
| 439 | .unlink = devfs_unlink_node,
|
|---|
| 440 | .has_children = devfs_has_children,
|
|---|
| 441 | .index_get = devfs_index_get,
|
|---|
| 442 | .size_get = devfs_size_get,
|
|---|
| 443 | .lnkcnt_get = devfs_lnkcnt_get,
|
|---|
| 444 | .is_directory = devfs_is_directory,
|
|---|
| 445 | .is_file = devfs_is_file,
|
|---|
| 446 | .device_get = devfs_device_get
|
|---|
| 447 | };
|
|---|
| 448 |
|
|---|
| 449 | bool devfs_init(void)
|
|---|
| 450 | {
|
|---|
| 451 | if (!hash_table_create(&devices, DEVICES_BUCKETS,
|
|---|
| 452 | DEVICES_KEYS, &devices_ops))
|
|---|
| 453 | return false;
|
|---|
| 454 |
|
|---|
| 455 | return true;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | static int devfs_mounted(devmap_handle_t devmap_handle, const char *opts,
|
|---|
| 459 | fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
|
|---|
| 460 | {
|
|---|
| 461 | *index = 0;
|
|---|
| 462 | *size = 0;
|
|---|
| 463 | *lnkcnt = 0;
|
|---|
| 464 | return EOK;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | static int devfs_unmounted(devmap_handle_t devmap_handle)
|
|---|
| 468 | {
|
|---|
| 469 | return ENOTSUP;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | static int
|
|---|
| 473 | devfs_read(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
|
|---|
| 474 | size_t *rbytes)
|
|---|
| 475 | {
|
|---|
| 476 | if (index == 0) {
|
|---|
| 477 | ipc_callid_t callid;
|
|---|
| 478 | size_t size;
|
|---|
| 479 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 480 | async_answer_0(callid, EINVAL);
|
|---|
| 481 | return EINVAL;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | dev_desc_t *desc;
|
|---|
| 485 | size_t count = devmap_get_namespaces(&desc);
|
|---|
| 486 |
|
|---|
| 487 | /* Get rid of root namespace */
|
|---|
| 488 | size_t i;
|
|---|
| 489 | for (i = 0; i < count; i++) {
|
|---|
| 490 | if (str_cmp(desc[i].name, "") == 0) {
|
|---|
| 491 | if (pos >= i)
|
|---|
| 492 | pos++;
|
|---|
| 493 |
|
|---|
| 494 | break;
|
|---|
| 495 | }
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | if (pos < count) {
|
|---|
| 499 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 500 | free(desc);
|
|---|
| 501 | *rbytes = 1;
|
|---|
| 502 | return EOK;
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | free(desc);
|
|---|
| 506 | pos -= count;
|
|---|
| 507 |
|
|---|
| 508 | /* Search root namespace */
|
|---|
| 509 | devmap_handle_t namespace;
|
|---|
| 510 | if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
|
|---|
| 511 | count = devmap_get_devices(namespace, &desc);
|
|---|
| 512 |
|
|---|
| 513 | if (pos < count) {
|
|---|
| 514 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 515 | free(desc);
|
|---|
| 516 | *rbytes = 1;
|
|---|
| 517 | return EOK;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | free(desc);
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | async_answer_0(callid, ENOENT);
|
|---|
| 524 | return ENOENT;
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 528 |
|
|---|
| 529 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 530 | /* Namespace directory */
|
|---|
| 531 | ipc_callid_t callid;
|
|---|
| 532 | size_t size;
|
|---|
| 533 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 534 | async_answer_0(callid, EINVAL);
|
|---|
| 535 | return EINVAL;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | dev_desc_t *desc;
|
|---|
| 539 | size_t count = devmap_get_devices(index, &desc);
|
|---|
| 540 |
|
|---|
| 541 | if (pos < count) {
|
|---|
| 542 | async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
|
|---|
| 543 | free(desc);
|
|---|
| 544 | *rbytes = 1;
|
|---|
| 545 | return EOK;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | free(desc);
|
|---|
| 549 | async_answer_0(callid, ENOENT);
|
|---|
| 550 | return ENOENT;
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 554 | /* Device node */
|
|---|
| 555 |
|
|---|
| 556 | unsigned long key[] = {
|
|---|
| 557 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 558 | };
|
|---|
| 559 |
|
|---|
| 560 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 561 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 562 | if (lnk == NULL) {
|
|---|
| 563 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 564 | return ENOENT;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 568 | assert(dev->sess);
|
|---|
| 569 |
|
|---|
| 570 | ipc_callid_t callid;
|
|---|
| 571 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| 572 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 573 | async_answer_0(callid, EINVAL);
|
|---|
| 574 | return EINVAL;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /* Make a request at the driver */
|
|---|
| 578 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 579 |
|
|---|
| 580 | ipc_call_t answer;
|
|---|
| 581 | aid_t msg = async_send_4(exch, VFS_OUT_READ, devmap_handle,
|
|---|
| 582 | index, LOWER32(pos), UPPER32(pos), &answer);
|
|---|
| 583 |
|
|---|
| 584 | /* Forward the IPC_M_DATA_READ request to the driver */
|
|---|
| 585 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 586 |
|
|---|
| 587 | async_exchange_end(exch);
|
|---|
| 588 |
|
|---|
| 589 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 590 |
|
|---|
| 591 | /* Wait for reply from the driver. */
|
|---|
| 592 | sysarg_t rc;
|
|---|
| 593 | async_wait_for(msg, &rc);
|
|---|
| 594 |
|
|---|
| 595 | *rbytes = IPC_GET_ARG1(answer);
|
|---|
| 596 | return rc;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | return ENOENT;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | static int
|
|---|
| 603 | devfs_write(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
|
|---|
| 604 | size_t *wbytes, aoff64_t *nsize)
|
|---|
| 605 | {
|
|---|
| 606 | if (index == 0)
|
|---|
| 607 | return ENOTSUP;
|
|---|
| 608 |
|
|---|
| 609 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 610 |
|
|---|
| 611 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 612 | /* Namespace directory */
|
|---|
| 613 | return ENOTSUP;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 617 | /* Device node */
|
|---|
| 618 | unsigned long key[] = {
|
|---|
| 619 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 620 | };
|
|---|
| 621 |
|
|---|
| 622 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 623 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 624 | if (lnk == NULL) {
|
|---|
| 625 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 626 | return ENOENT;
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 630 | assert(dev->sess);
|
|---|
| 631 |
|
|---|
| 632 | ipc_callid_t callid;
|
|---|
| 633 | if (!async_data_write_receive(&callid, NULL)) {
|
|---|
| 634 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 635 | async_answer_0(callid, EINVAL);
|
|---|
| 636 | return EINVAL;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /* Make a request at the driver */
|
|---|
| 640 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 641 |
|
|---|
| 642 | ipc_call_t answer;
|
|---|
| 643 | aid_t msg = async_send_4(exch, VFS_OUT_WRITE, devmap_handle,
|
|---|
| 644 | index, LOWER32(pos), UPPER32(pos), &answer);
|
|---|
| 645 |
|
|---|
| 646 | /* Forward the IPC_M_DATA_WRITE request to the driver */
|
|---|
| 647 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 648 |
|
|---|
| 649 | async_exchange_end(exch);
|
|---|
| 650 |
|
|---|
| 651 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 652 |
|
|---|
| 653 | /* Wait for reply from the driver. */
|
|---|
| 654 | sysarg_t rc;
|
|---|
| 655 | async_wait_for(msg, &rc);
|
|---|
| 656 |
|
|---|
| 657 | *wbytes = IPC_GET_ARG1(answer);
|
|---|
| 658 | *nsize = 0;
|
|---|
| 659 | return rc;
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | return ENOENT;
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | static int
|
|---|
| 666 | devfs_truncate(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t size)
|
|---|
| 667 | {
|
|---|
| 668 | return ENOTSUP;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | static int devfs_close(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| 672 | {
|
|---|
| 673 | if (index == 0)
|
|---|
| 674 | return EOK;
|
|---|
| 675 |
|
|---|
| 676 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 677 |
|
|---|
| 678 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 679 | /* Namespace directory */
|
|---|
| 680 | return EOK;
|
|---|
| 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 | return ENOENT;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 696 | assert(dev->sess);
|
|---|
| 697 | dev->refcount--;
|
|---|
| 698 |
|
|---|
| 699 | if (dev->refcount == 0) {
|
|---|
| 700 | async_hangup(dev->sess);
|
|---|
| 701 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 705 |
|
|---|
| 706 | return EOK;
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | return ENOENT;
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | static int devfs_sync(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| 713 | {
|
|---|
| 714 | if (index == 0)
|
|---|
| 715 | return EOK;
|
|---|
| 716 |
|
|---|
| 717 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 718 |
|
|---|
| 719 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 720 | /* Namespace directory */
|
|---|
| 721 | return EOK;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 725 | unsigned long key[] = {
|
|---|
| 726 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 727 | };
|
|---|
| 728 |
|
|---|
| 729 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 730 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 731 | if (lnk == NULL) {
|
|---|
| 732 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 733 | return ENOENT;
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| 737 | assert(dev->sess);
|
|---|
| 738 |
|
|---|
| 739 | /* Make a request at the driver */
|
|---|
| 740 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 741 |
|
|---|
| 742 | ipc_call_t answer;
|
|---|
| 743 | aid_t msg = async_send_2(exch, VFS_OUT_SYNC, devmap_handle,
|
|---|
| 744 | index, &answer);
|
|---|
| 745 |
|
|---|
| 746 | async_exchange_end(exch);
|
|---|
| 747 |
|
|---|
| 748 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 749 |
|
|---|
| 750 | /* Wait for reply from the driver */
|
|---|
| 751 | sysarg_t rc;
|
|---|
| 752 | async_wait_for(msg, &rc);
|
|---|
| 753 |
|
|---|
| 754 | return rc;
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | return ENOENT;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | static int devfs_destroy(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| 761 | {
|
|---|
| 762 | return ENOTSUP;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | vfs_out_ops_t devfs_ops = {
|
|---|
| 766 | .mounted = devfs_mounted,
|
|---|
| 767 | .unmounted = devfs_unmounted,
|
|---|
| 768 | .read = devfs_read,
|
|---|
| 769 | .write = devfs_write,
|
|---|
| 770 | .truncate = devfs_truncate,
|
|---|
| 771 | .close = devfs_close,
|
|---|
| 772 | .destroy = devfs_destroy,
|
|---|
| 773 | .sync = devfs_sync,
|
|---|
| 774 | };
|
|---|
| 775 |
|
|---|
| 776 | /**
|
|---|
| 777 | * @}
|
|---|
| 778 | */
|
|---|