[a095d20] | 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 |
|
---|
[ed903174] | 38 | #include <macros.h>
|
---|
[a095d20] | 39 | #include <bool.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <malloc.h>
|
---|
[19f857a] | 42 | #include <str.h>
|
---|
[a095d20] | 43 | #include <libfs.h>
|
---|
[1e4cada] | 44 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 45 | #include <adt/hash_table.h>
|
---|
[1313ee9] | 46 | #include <ipc/devmap.h>
|
---|
[852b801] | 47 | #include <sys/stat.h>
|
---|
[1313ee9] | 48 | #include <libfs.h>
|
---|
| 49 | #include <assert.h>
|
---|
[a095d20] | 50 | #include "devfs.h"
|
---|
| 51 | #include "devfs_ops.h"
|
---|
| 52 |
|
---|
[1313ee9] | 53 | typedef struct {
|
---|
| 54 | devmap_handle_type_t type;
|
---|
[991f645] | 55 | devmap_handle_t handle;
|
---|
[1313ee9] | 56 | } devfs_node_t;
|
---|
[a095d20] | 57 |
|
---|
[17fd1d4] | 58 | /** Opened devices structure */
|
---|
| 59 | typedef struct {
|
---|
[991f645] | 60 | devmap_handle_t handle;
|
---|
[a7e04d16] | 61 | int phone; /**< When < 0, the structure is incomplete. */
|
---|
[17fd1d4] | 62 | size_t refcount;
|
---|
| 63 | link_t link;
|
---|
[a7e04d16] | 64 | fibril_condvar_t cv; /**< Broadcast when completed. */
|
---|
[17fd1d4] | 65 | } device_t;
|
---|
| 66 |
|
---|
| 67 | /** Hash table of opened devices */
|
---|
| 68 | static hash_table_t devices;
|
---|
| 69 |
|
---|
[2dfd9fa] | 70 | /** Hash table mutex */
|
---|
| 71 | static FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
| 72 |
|
---|
[17fd1d4] | 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);
|
---|
[991f645] | 86 | return (dev->handle == (devmap_handle_t) key[DEVICES_KEY_HANDLE]);
|
---|
[17fd1d4] | 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 |
|
---|
[1313ee9] | 100 | static int devfs_node_get_internal(fs_node_t **rfn, devmap_handle_type_t type,
|
---|
[991f645] | 101 | devmap_handle_t handle)
|
---|
[a095d20] | 102 | {
|
---|
[1313ee9] | 103 | devfs_node_t *node = (devfs_node_t *) malloc(sizeof(devfs_node_t));
|
---|
| 104 | if (node == NULL) {
|
---|
| 105 | *rfn = NULL;
|
---|
| 106 | return ENOMEM;
|
---|
| 107 | }
|
---|
[17fd1d4] | 108 |
|
---|
[1313ee9] | 109 | *rfn = (fs_node_t *) malloc(sizeof(fs_node_t));
|
---|
| 110 | if (*rfn == NULL) {
|
---|
| 111 | free(node);
|
---|
| 112 | *rfn = NULL;
|
---|
| 113 | return ENOMEM;
|
---|
| 114 | }
|
---|
[a095d20] | 115 |
|
---|
[1313ee9] | 116 | fs_node_initialize(*rfn);
|
---|
| 117 | node->type = type;
|
---|
| 118 | node->handle = handle;
|
---|
| 119 |
|
---|
| 120 | (*rfn)->data = node;
|
---|
| 121 | return EOK;
|
---|
[a095d20] | 122 | }
|
---|
| 123 |
|
---|
[991f645] | 124 | static int devfs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
[a095d20] | 125 | {
|
---|
[1313ee9] | 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;
|
---|
[b366a1bc] | 132 | int ret;
|
---|
[a095d20] | 133 |
|
---|
[1313ee9] | 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) {
|
---|
[b366a1bc] | 148 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
|
---|
[1313ee9] | 149 | free(devs);
|
---|
[b366a1bc] | 150 | return ret;
|
---|
[1313ee9] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | free(devs);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /* Search root namespace */
|
---|
[991f645] | 158 | devmap_handle_t namespace;
|
---|
[1313ee9] | 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) {
|
---|
[b366a1bc] | 166 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
---|
[1313ee9] | 167 | free(devs);
|
---|
[b366a1bc] | 168 | return ret;
|
---|
[1313ee9] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | free(devs);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | *rfn = NULL;
|
---|
| 177 | return EOK;
|
---|
[a095d20] | 178 | }
|
---|
| 179 |
|
---|
[1313ee9] | 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) {
|
---|
[b366a1bc] | 189 | ret = devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
|
---|
[1313ee9] | 190 | free(devs);
|
---|
[b366a1bc] | 191 | return ret;
|
---|
[1313ee9] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | free(devs);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | *rfn = NULL;
|
---|
| 199 | return EOK;
|
---|
[a095d20] | 200 | }
|
---|
| 201 |
|
---|
[1313ee9] | 202 | *rfn = NULL;
|
---|
| 203 | return EOK;
|
---|
[a095d20] | 204 | }
|
---|
| 205 |
|
---|
[991f645] | 206 | static int devfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
[a095d20] | 207 | {
|
---|
[1313ee9] | 208 | return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
|
---|
[a095d20] | 209 | }
|
---|
| 210 |
|
---|
[1313ee9] | 211 | static int devfs_node_open(fs_node_t *fn)
|
---|
[a095d20] | 212 | {
|
---|
[1313ee9] | 213 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
[a095d20] | 214 |
|
---|
[1313ee9] | 215 | if (node->handle == 0) {
|
---|
| 216 | /* Root directory */
|
---|
| 217 | return EOK;
|
---|
[a095d20] | 218 | }
|
---|
| 219 |
|
---|
[1313ee9] | 220 | devmap_handle_type_t type = devmap_handle_probe(node->handle);
|
---|
[a095d20] | 221 |
|
---|
[1313ee9] | 222 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
| 223 | /* Namespace directory */
|
---|
| 224 | return EOK;
|
---|
[a095d20] | 225 | }
|
---|
| 226 |
|
---|
[1313ee9] | 227 | if (type == DEV_HANDLE_DEVICE) {
|
---|
| 228 | /* Device node */
|
---|
| 229 |
|
---|
| 230 | unsigned long key[] = {
|
---|
| 231 | [DEVICES_KEY_HANDLE] = (unsigned long) node->handle
|
---|
| 232 | };
|
---|
[a7e04d16] | 233 | link_t *lnk;
|
---|
| 234 |
|
---|
[1313ee9] | 235 | fibril_mutex_lock(&devices_mutex);
|
---|
[a7e04d16] | 236 | restart:
|
---|
| 237 | lnk = hash_table_find(&devices, key);
|
---|
[1313ee9] | 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;
|
---|
[a095d20] | 243 | }
|
---|
| 244 |
|
---|
[a7e04d16] | 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 |
|
---|
[1313ee9] | 262 | int phone = devmap_device_connect(node->handle, 0);
|
---|
[a7e04d16] | 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 |
|
---|
[1313ee9] | 272 | if (phone < 0) {
|
---|
[a7e04d16] | 273 | /*
|
---|
| 274 | * Connecting failed, need to remove the
|
---|
| 275 | * entry and free the device structure.
|
---|
| 276 | */
|
---|
| 277 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
---|
[2dfd9fa] | 278 | fibril_mutex_unlock(&devices_mutex);
|
---|
[a7e04d16] | 279 |
|
---|
[1313ee9] | 280 | return ENOENT;
|
---|
[17fd1d4] | 281 | }
|
---|
| 282 |
|
---|
[a7e04d16] | 283 | /* Set the correct phone. */
|
---|
[1313ee9] | 284 | dev->phone = phone;
|
---|
| 285 | } else {
|
---|
| 286 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
[a7e04d16] | 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 |
|
---|
[1313ee9] | 300 | dev->refcount++;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 304 |
|
---|
| 305 | return EOK;
|
---|
[a095d20] | 306 | }
|
---|
[1313ee9] | 307 |
|
---|
| 308 | return ENOENT;
|
---|
[a095d20] | 309 | }
|
---|
| 310 |
|
---|
[1313ee9] | 311 | static int devfs_node_put(fs_node_t *fn)
|
---|
[a095d20] | 312 | {
|
---|
[1313ee9] | 313 | free(fn->data);
|
---|
| 314 | free(fn);
|
---|
| 315 | return EOK;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[991f645] | 318 | static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
---|
[1313ee9] | 319 | {
|
---|
| 320 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
[a095d20] | 321 |
|
---|
[1313ee9] | 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;
|
---|
[a095d20] | 344 |
|
---|
[1313ee9] | 345 | if (node->handle == 0) {
|
---|
| 346 | size_t count = devmap_count_namespaces();
|
---|
| 347 | if (count > 0) {
|
---|
| 348 | *has_children = true;
|
---|
| 349 | return EOK;
|
---|
[17fd1d4] | 350 | }
|
---|
| 351 |
|
---|
[1313ee9] | 352 | /* Root namespace */
|
---|
[991f645] | 353 | devmap_handle_t namespace;
|
---|
[1313ee9] | 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 | }
|
---|
[17fd1d4] | 360 | }
|
---|
| 361 |
|
---|
[1313ee9] | 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 | }
|
---|
[17fd1d4] | 372 |
|
---|
[1313ee9] | 373 | *has_children = false;
|
---|
| 374 | return EOK;
|
---|
[a095d20] | 375 | }
|
---|
| 376 |
|
---|
[1313ee9] | 377 | *has_children = false;
|
---|
| 378 | return EOK;
|
---|
[17fd1d4] | 379 | }
|
---|
| 380 |
|
---|
[1313ee9] | 381 | static fs_index_t devfs_index_get(fs_node_t *fn)
|
---|
[17fd1d4] | 382 | {
|
---|
[1313ee9] | 383 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
| 384 | return node->handle;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[ed903174] | 387 | static aoff64_t devfs_size_get(fs_node_t *fn)
|
---|
[1313ee9] | 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;
|
---|
[17fd1d4] | 395 |
|
---|
[1313ee9] | 396 | if (node->handle == 0)
|
---|
| 397 | return 0;
|
---|
| 398 |
|
---|
| 399 | return 1;
|
---|
| 400 | }
|
---|
[852b801] | 401 |
|
---|
[1313ee9] | 402 | static char devfs_plb_get_char(unsigned pos)
|
---|
| 403 | {
|
---|
| 404 | return devfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 405 | }
|
---|
[852b801] | 406 |
|
---|
[1313ee9] | 407 | static bool devfs_is_directory(fs_node_t *fn)
|
---|
| 408 | {
|
---|
| 409 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
---|
[852b801] | 410 |
|
---|
[1313ee9] | 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 |
|
---|
[991f645] | 421 | static devmap_handle_t devfs_device_get(fs_node_t *fn)
|
---|
[1313ee9] | 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 */
|
---|
[96b02eb9] | 466 | sysarg_t retval = async_data_write_accept((void **) &opts, true, 0, 0,
|
---|
[eda925a] | 467 | 0, NULL);
|
---|
[1313ee9] | 468 | if (retval != EOK) {
|
---|
[ffa2c8ef] | 469 | async_answer_0(rid, retval);
|
---|
[1313ee9] | 470 | return;
|
---|
[852b801] | 471 | }
|
---|
[1313ee9] | 472 |
|
---|
| 473 | free(opts);
|
---|
[ffa2c8ef] | 474 | async_answer_3(rid, EOK, 0, 0, 0);
|
---|
[1313ee9] | 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 |
|
---|
[3c11713] | 482 | void devfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 483 | {
|
---|
[ffa2c8ef] | 484 | async_answer_0(rid, ENOTSUP);
|
---|
[3c11713] | 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 |
|
---|
[1313ee9] | 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 | }
|
---|
[852b801] | 496 |
|
---|
[1313ee9] | 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);
|
---|
[17fd1d4] | 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);
|
---|
[ed903174] | 510 | aoff64_t pos =
|
---|
| 511 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
[17fd1d4] | 512 |
|
---|
[1313ee9] | 513 | if (index == 0) {
|
---|
| 514 | ipc_callid_t callid;
|
---|
| 515 | size_t size;
|
---|
| 516 | if (!async_data_read_receive(&callid, &size)) {
|
---|
[ffa2c8ef] | 517 | async_answer_0(callid, EINVAL);
|
---|
| 518 | async_answer_0(rid, EINVAL);
|
---|
[1313ee9] | 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);
|
---|
[ffa2c8ef] | 539 | async_answer_1(rid, EOK, 1);
|
---|
[1313ee9] | 540 | return;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | free(desc);
|
---|
| 544 | pos -= count;
|
---|
| 545 |
|
---|
| 546 | /* Search root namespace */
|
---|
[991f645] | 547 | devmap_handle_t namespace;
|
---|
[1313ee9] | 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);
|
---|
[ffa2c8ef] | 554 | async_answer_1(rid, EOK, 1);
|
---|
[1313ee9] | 555 | return;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | free(desc);
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[ffa2c8ef] | 561 | async_answer_0(callid, ENOENT);
|
---|
| 562 | async_answer_1(rid, ENOENT, 0);
|
---|
[1313ee9] | 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)) {
|
---|
[ffa2c8ef] | 573 | async_answer_0(callid, EINVAL);
|
---|
| 574 | async_answer_0(rid, EINVAL);
|
---|
[1313ee9] | 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);
|
---|
[ffa2c8ef] | 584 | async_answer_1(rid, EOK, 1);
|
---|
[1313ee9] | 585 | return;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | free(desc);
|
---|
[ffa2c8ef] | 589 | async_answer_0(callid, ENOENT);
|
---|
| 590 | async_answer_1(rid, ENOENT, 0);
|
---|
[1313ee9] | 591 | return;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | if (type == DEV_HANDLE_DEVICE) {
|
---|
| 595 | /* Device node */
|
---|
| 596 |
|
---|
[17fd1d4] | 597 | unsigned long key[] = {
|
---|
| 598 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
| 599 | };
|
---|
| 600 |
|
---|
[2dfd9fa] | 601 | fibril_mutex_lock(&devices_mutex);
|
---|
[17fd1d4] | 602 | link_t *lnk = hash_table_find(&devices, key);
|
---|
| 603 | if (lnk == NULL) {
|
---|
[2dfd9fa] | 604 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 605 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 606 | return;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
[a7e04d16] | 610 | assert(dev->phone >= 0);
|
---|
[17fd1d4] | 611 |
|
---|
| 612 | ipc_callid_t callid;
|
---|
[0da4e41] | 613 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
[2dfd9fa] | 614 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 615 | async_answer_0(callid, EINVAL);
|
---|
| 616 | async_answer_0(rid, EINVAL);
|
---|
[17fd1d4] | 617 | return;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | /* Make a request at the driver */
|
---|
| 621 | ipc_call_t answer;
|
---|
[228e490] | 622 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
[17fd1d4] | 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 */
|
---|
[ffa2c8ef] | 627 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[2dfd9fa] | 628 | fibril_mutex_unlock(&devices_mutex);
|
---|
[17fd1d4] | 629 |
|
---|
| 630 | /* Wait for reply from the driver. */
|
---|
[96b02eb9] | 631 | sysarg_t rc;
|
---|
[17fd1d4] | 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 */
|
---|
[ffa2c8ef] | 636 | async_answer_1(rid, rc, bytes);
|
---|
[1313ee9] | 637 | return;
|
---|
[a095d20] | 638 | }
|
---|
[1313ee9] | 639 |
|
---|
[ffa2c8ef] | 640 | async_answer_0(rid, ENOENT);
|
---|
[a095d20] | 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);
|
---|
[1313ee9] | 646 | if (index == 0) {
|
---|
[ffa2c8ef] | 647 | async_answer_0(rid, ENOTSUP);
|
---|
[1313ee9] | 648 | return;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
| 652 |
|
---|
| 653 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
| 654 | /* Namespace directory */
|
---|
[ffa2c8ef] | 655 | async_answer_0(rid, ENOTSUP);
|
---|
[1313ee9] | 656 | return;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | if (type == DEV_HANDLE_DEVICE) {
|
---|
| 660 | /* Device node */
|
---|
[17fd1d4] | 661 | unsigned long key[] = {
|
---|
| 662 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
| 663 | };
|
---|
| 664 |
|
---|
[2dfd9fa] | 665 | fibril_mutex_lock(&devices_mutex);
|
---|
[17fd1d4] | 666 | link_t *lnk = hash_table_find(&devices, key);
|
---|
| 667 | if (lnk == NULL) {
|
---|
[2dfd9fa] | 668 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 669 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 670 | return;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
[a7e04d16] | 674 | assert(dev->phone >= 0);
|
---|
[17fd1d4] | 675 |
|
---|
| 676 | ipc_callid_t callid;
|
---|
[0da4e41] | 677 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
[2dfd9fa] | 678 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 679 | async_answer_0(callid, EINVAL);
|
---|
| 680 | async_answer_0(rid, EINVAL);
|
---|
[17fd1d4] | 681 | return;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /* Make a request at the driver */
|
---|
| 685 | ipc_call_t answer;
|
---|
[228e490] | 686 | aid_t msg = async_send_3(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
[17fd1d4] | 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 */
|
---|
[ffa2c8ef] | 691 | async_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[17fd1d4] | 692 |
|
---|
[2dfd9fa] | 693 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 694 |
|
---|
[17fd1d4] | 695 | /* Wait for reply from the driver. */
|
---|
[96b02eb9] | 696 | sysarg_t rc;
|
---|
[17fd1d4] | 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 */
|
---|
[ffa2c8ef] | 701 | async_answer_1(rid, rc, bytes);
|
---|
[1313ee9] | 702 | return;
|
---|
[a095d20] | 703 | }
|
---|
[1313ee9] | 704 |
|
---|
[ffa2c8ef] | 705 | async_answer_0(rid, ENOENT);
|
---|
[a095d20] | 706 | }
|
---|
| 707 |
|
---|
| 708 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 709 | {
|
---|
[ffa2c8ef] | 710 | async_answer_0(rid, ENOTSUP);
|
---|
[a095d20] | 711 | }
|
---|
| 712 |
|
---|
[17fd1d4] | 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 |
|
---|
[1313ee9] | 717 | if (index == 0) {
|
---|
[ffa2c8ef] | 718 | async_answer_0(rid, EOK);
|
---|
[1313ee9] | 719 | return;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
| 723 |
|
---|
| 724 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
| 725 | /* Namespace directory */
|
---|
[ffa2c8ef] | 726 | async_answer_0(rid, EOK);
|
---|
[1313ee9] | 727 | return;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | if (type == DEV_HANDLE_DEVICE) {
|
---|
[17fd1d4] | 731 | unsigned long key[] = {
|
---|
| 732 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
| 733 | };
|
---|
| 734 |
|
---|
[2dfd9fa] | 735 | fibril_mutex_lock(&devices_mutex);
|
---|
[17fd1d4] | 736 | link_t *lnk = hash_table_find(&devices, key);
|
---|
| 737 | if (lnk == NULL) {
|
---|
[2dfd9fa] | 738 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 739 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 740 | return;
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
[a7e04d16] | 744 | assert(dev->phone >= 0);
|
---|
[17fd1d4] | 745 | dev->refcount--;
|
---|
| 746 |
|
---|
| 747 | if (dev->refcount == 0) {
|
---|
[ffa2c8ef] | 748 | async_hangup(dev->phone);
|
---|
[17fd1d4] | 749 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
---|
| 750 | }
|
---|
| 751 |
|
---|
[2dfd9fa] | 752 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 753 |
|
---|
[ffa2c8ef] | 754 | async_answer_0(rid, EOK);
|
---|
[1313ee9] | 755 | return;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[ffa2c8ef] | 758 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 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 |
|
---|
[1313ee9] | 765 | if (index == 0) {
|
---|
[ffa2c8ef] | 766 | async_answer_0(rid, EOK);
|
---|
[1313ee9] | 767 | return;
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | devmap_handle_type_t type = devmap_handle_probe(index);
|
---|
| 771 |
|
---|
| 772 | if (type == DEV_HANDLE_NAMESPACE) {
|
---|
| 773 | /* Namespace directory */
|
---|
[ffa2c8ef] | 774 | async_answer_0(rid, EOK);
|
---|
[1313ee9] | 775 | return;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | if (type == DEV_HANDLE_DEVICE) {
|
---|
[17fd1d4] | 779 | unsigned long key[] = {
|
---|
| 780 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
---|
| 781 | };
|
---|
| 782 |
|
---|
[2dfd9fa] | 783 | fibril_mutex_lock(&devices_mutex);
|
---|
[17fd1d4] | 784 | link_t *lnk = hash_table_find(&devices, key);
|
---|
| 785 | if (lnk == NULL) {
|
---|
[2dfd9fa] | 786 | fibril_mutex_unlock(&devices_mutex);
|
---|
[ffa2c8ef] | 787 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 788 | return;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
---|
[a7e04d16] | 792 | assert(dev->phone >= 0);
|
---|
[17fd1d4] | 793 |
|
---|
| 794 | /* Make a request at the driver */
|
---|
| 795 | ipc_call_t answer;
|
---|
[228e490] | 796 | aid_t msg = async_send_2(dev->phone, IPC_GET_IMETHOD(*request),
|
---|
[17fd1d4] | 797 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
|
---|
| 798 |
|
---|
[2dfd9fa] | 799 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 800 |
|
---|
[17fd1d4] | 801 | /* Wait for reply from the driver */
|
---|
[96b02eb9] | 802 | sysarg_t rc;
|
---|
[17fd1d4] | 803 | async_wait_for(msg, &rc);
|
---|
| 804 |
|
---|
| 805 | /* Driver reply is the final result of the whole operation */
|
---|
[ffa2c8ef] | 806 | async_answer_0(rid, rc);
|
---|
[1313ee9] | 807 | return;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
[ffa2c8ef] | 810 | async_answer_0(rid, ENOENT);
|
---|
[17fd1d4] | 811 | }
|
---|
| 812 |
|
---|
[a095d20] | 813 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 814 | {
|
---|
[ffa2c8ef] | 815 | async_answer_0(rid, ENOTSUP);
|
---|
[a095d20] | 816 | }
|
---|
| 817 |
|
---|
| 818 | /**
|
---|
| 819 | * @}
|
---|
[17fd1d4] | 820 | */
|
---|