| [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;
|
|---|
| [79ae36dd] | 61 | async_sess_t *sess; /**< If NULL, the structure is incomplete. */
|
|---|
| [17fd1d4] | 62 | size_t refcount;
|
|---|
| 63 | link_t link;
|
|---|
| [79ae36dd] | 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;
|
|---|
| [79ae36dd] | 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;
|
|---|
| [79ae36dd] | 246 |
|
|---|
| 247 | /* Mark as incomplete */
|
|---|
| 248 | dev->sess = NULL;
|
|---|
| [a7e04d16] | 249 | dev->refcount = 1;
|
|---|
| 250 | fibril_condvar_initialize(&dev->cv);
|
|---|
| [79ae36dd] | 251 |
|
|---|
| [a7e04d16] | 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);
|
|---|
| [79ae36dd] | 258 |
|
|---|
| [a7e04d16] | 259 | /*
|
|---|
| 260 | * Drop the mutex to allow recursive devfs requests.
|
|---|
| 261 | */
|
|---|
| 262 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [79ae36dd] | 263 |
|
|---|
| 264 | async_sess_t *sess = devmap_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 265 | node->handle, 0);
|
|---|
| 266 |
|
|---|
| [a7e04d16] | 267 | fibril_mutex_lock(&devices_mutex);
|
|---|
| [79ae36dd] | 268 |
|
|---|
| [a7e04d16] | 269 | /*
|
|---|
| 270 | * Notify possible waiters about this device structure
|
|---|
| 271 | * being completed (or destroyed).
|
|---|
| 272 | */
|
|---|
| 273 | fibril_condvar_broadcast(&dev->cv);
|
|---|
| [79ae36dd] | 274 |
|
|---|
| 275 | if (!sess) {
|
|---|
| [a7e04d16] | 276 | /*
|
|---|
| 277 | * Connecting failed, need to remove the
|
|---|
| 278 | * entry and free the device structure.
|
|---|
| 279 | */
|
|---|
| 280 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| [2dfd9fa] | 281 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [79ae36dd] | 282 |
|
|---|
| [1313ee9] | 283 | return ENOENT;
|
|---|
| [17fd1d4] | 284 | }
|
|---|
| 285 |
|
|---|
| [79ae36dd] | 286 | /* Set the correct session. */
|
|---|
| 287 | dev->sess = sess;
|
|---|
| [1313ee9] | 288 | } else {
|
|---|
| 289 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| [79ae36dd] | 290 |
|
|---|
| 291 | if (!dev->sess) {
|
|---|
| [a7e04d16] | 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 |
|
|---|
| [1313ee9] | 303 | dev->refcount++;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 307 |
|
|---|
| 308 | return EOK;
|
|---|
| [a095d20] | 309 | }
|
|---|
| [1313ee9] | 310 |
|
|---|
| 311 | return ENOENT;
|
|---|
| [a095d20] | 312 | }
|
|---|
| 313 |
|
|---|
| [1313ee9] | 314 | static int devfs_node_put(fs_node_t *fn)
|
|---|
| [a095d20] | 315 | {
|
|---|
| [1313ee9] | 316 | free(fn->data);
|
|---|
| 317 | free(fn);
|
|---|
| 318 | return EOK;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| [991f645] | 321 | static int devfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
|---|
| [1313ee9] | 322 | {
|
|---|
| 323 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| [a095d20] | 324 |
|
|---|
| [1313ee9] | 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;
|
|---|
| [a095d20] | 347 |
|
|---|
| [1313ee9] | 348 | if (node->handle == 0) {
|
|---|
| 349 | size_t count = devmap_count_namespaces();
|
|---|
| 350 | if (count > 0) {
|
|---|
| 351 | *has_children = true;
|
|---|
| 352 | return EOK;
|
|---|
| [17fd1d4] | 353 | }
|
|---|
| 354 |
|
|---|
| [1313ee9] | 355 | /* Root namespace */
|
|---|
| [991f645] | 356 | devmap_handle_t namespace;
|
|---|
| [1313ee9] | 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 | }
|
|---|
| [17fd1d4] | 363 | }
|
|---|
| 364 |
|
|---|
| [1313ee9] | 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 | }
|
|---|
| [17fd1d4] | 375 |
|
|---|
| [1313ee9] | 376 | *has_children = false;
|
|---|
| 377 | return EOK;
|
|---|
| [a095d20] | 378 | }
|
|---|
| 379 |
|
|---|
| [1313ee9] | 380 | *has_children = false;
|
|---|
| 381 | return EOK;
|
|---|
| [17fd1d4] | 382 | }
|
|---|
| 383 |
|
|---|
| [1313ee9] | 384 | static fs_index_t devfs_index_get(fs_node_t *fn)
|
|---|
| [17fd1d4] | 385 | {
|
|---|
| [1313ee9] | 386 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| 387 | return node->handle;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| [ed903174] | 390 | static aoff64_t devfs_size_get(fs_node_t *fn)
|
|---|
| [1313ee9] | 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;
|
|---|
| [17fd1d4] | 398 |
|
|---|
| [1313ee9] | 399 | if (node->handle == 0)
|
|---|
| 400 | return 0;
|
|---|
| 401 |
|
|---|
| 402 | return 1;
|
|---|
| 403 | }
|
|---|
| [852b801] | 404 |
|
|---|
| [1313ee9] | 405 | static bool devfs_is_directory(fs_node_t *fn)
|
|---|
| 406 | {
|
|---|
| 407 | devfs_node_t *node = (devfs_node_t *) fn->data;
|
|---|
| [852b801] | 408 |
|
|---|
| [1313ee9] | 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 |
|
|---|
| [991f645] | 419 | static devmap_handle_t devfs_device_get(fs_node_t *fn)
|
|---|
| [1313ee9] | 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 |
|
|---|
| [efcebe1] | 458 | static int devfs_mounted(devmap_handle_t devmap_handle, const char *opts,
|
|---|
| 459 | fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
|
|---|
| [3c11713] | 460 | {
|
|---|
| [efcebe1] | 461 | *index = 0;
|
|---|
| 462 | *size = 0;
|
|---|
| 463 | *lnkcnt = 0;
|
|---|
| 464 | return EOK;
|
|---|
| [1313ee9] | 465 | }
|
|---|
| 466 |
|
|---|
| [efcebe1] | 467 | static int devfs_unmounted(devmap_handle_t devmap_handle)
|
|---|
| [1313ee9] | 468 | {
|
|---|
| [efcebe1] | 469 | return ENOTSUP;
|
|---|
| [17fd1d4] | 470 | }
|
|---|
| 471 |
|
|---|
| [efcebe1] | 472 | static int
|
|---|
| 473 | devfs_read(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
|
|---|
| 474 | size_t *rbytes)
|
|---|
| [17fd1d4] | 475 | {
|
|---|
| [1313ee9] | 476 | if (index == 0) {
|
|---|
| 477 | ipc_callid_t callid;
|
|---|
| 478 | size_t size;
|
|---|
| 479 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| [ffa2c8ef] | 480 | async_answer_0(callid, EINVAL);
|
|---|
| [efcebe1] | 481 | return EINVAL;
|
|---|
| [1313ee9] | 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);
|
|---|
| [efcebe1] | 501 | *rbytes = 1;
|
|---|
| 502 | return EOK;
|
|---|
| [1313ee9] | 503 | }
|
|---|
| 504 |
|
|---|
| 505 | free(desc);
|
|---|
| 506 | pos -= count;
|
|---|
| 507 |
|
|---|
| 508 | /* Search root namespace */
|
|---|
| [991f645] | 509 | devmap_handle_t namespace;
|
|---|
| [1313ee9] | 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);
|
|---|
| [efcebe1] | 516 | *rbytes = 1;
|
|---|
| 517 | return EOK;
|
|---|
| [1313ee9] | 518 | }
|
|---|
| 519 |
|
|---|
| 520 | free(desc);
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| [ffa2c8ef] | 523 | async_answer_0(callid, ENOENT);
|
|---|
| [efcebe1] | 524 | return ENOENT;
|
|---|
| [1313ee9] | 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)) {
|
|---|
| [ffa2c8ef] | 534 | async_answer_0(callid, EINVAL);
|
|---|
| [efcebe1] | 535 | return EINVAL;
|
|---|
| [1313ee9] | 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);
|
|---|
| [efcebe1] | 544 | *rbytes = 1;
|
|---|
| 545 | return EOK;
|
|---|
| [1313ee9] | 546 | }
|
|---|
| 547 |
|
|---|
| 548 | free(desc);
|
|---|
| [ffa2c8ef] | 549 | async_answer_0(callid, ENOENT);
|
|---|
| [efcebe1] | 550 | return ENOENT;
|
|---|
| [1313ee9] | 551 | }
|
|---|
| 552 |
|
|---|
| 553 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 554 | /* Device node */
|
|---|
| 555 |
|
|---|
| [17fd1d4] | 556 | unsigned long key[] = {
|
|---|
| 557 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 558 | };
|
|---|
| 559 |
|
|---|
| [2dfd9fa] | 560 | fibril_mutex_lock(&devices_mutex);
|
|---|
| [17fd1d4] | 561 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 562 | if (lnk == NULL) {
|
|---|
| [2dfd9fa] | 563 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [efcebe1] | 564 | return ENOENT;
|
|---|
| [17fd1d4] | 565 | }
|
|---|
| 566 |
|
|---|
| 567 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| [79ae36dd] | 568 | assert(dev->sess);
|
|---|
| [17fd1d4] | 569 |
|
|---|
| 570 | ipc_callid_t callid;
|
|---|
| [0da4e41] | 571 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| [2dfd9fa] | 572 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [ffa2c8ef] | 573 | async_answer_0(callid, EINVAL);
|
|---|
| [efcebe1] | 574 | return EINVAL;
|
|---|
| [17fd1d4] | 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /* Make a request at the driver */
|
|---|
| [79ae36dd] | 578 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 579 |
|
|---|
| [17fd1d4] | 580 | ipc_call_t answer;
|
|---|
| [efcebe1] | 581 | aid_t msg = async_send_4(exch, VFS_OUT_READ, devmap_handle,
|
|---|
| 582 | index, LOWER32(pos), UPPER32(pos), &answer);
|
|---|
| [17fd1d4] | 583 |
|
|---|
| 584 | /* Forward the IPC_M_DATA_READ request to the driver */
|
|---|
| [79ae36dd] | 585 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 586 |
|
|---|
| 587 | async_exchange_end(exch);
|
|---|
| 588 |
|
|---|
| [2dfd9fa] | 589 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [17fd1d4] | 590 |
|
|---|
| 591 | /* Wait for reply from the driver. */
|
|---|
| [96b02eb9] | 592 | sysarg_t rc;
|
|---|
| [17fd1d4] | 593 | async_wait_for(msg, &rc);
|
|---|
| 594 |
|
|---|
| [efcebe1] | 595 | *rbytes = IPC_GET_ARG1(answer);
|
|---|
| 596 | return rc;
|
|---|
| [a095d20] | 597 | }
|
|---|
| [1313ee9] | 598 |
|
|---|
| [efcebe1] | 599 | return ENOENT;
|
|---|
| [a095d20] | 600 | }
|
|---|
| 601 |
|
|---|
| [efcebe1] | 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)
|
|---|
| [a095d20] | 605 | {
|
|---|
| [efcebe1] | 606 | if (index == 0)
|
|---|
| 607 | return ENOTSUP;
|
|---|
| [1313ee9] | 608 |
|
|---|
| 609 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 610 |
|
|---|
| 611 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 612 | /* Namespace directory */
|
|---|
| [efcebe1] | 613 | return ENOTSUP;
|
|---|
| [1313ee9] | 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| 617 | /* Device node */
|
|---|
| [17fd1d4] | 618 | unsigned long key[] = {
|
|---|
| 619 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 620 | };
|
|---|
| 621 |
|
|---|
| [2dfd9fa] | 622 | fibril_mutex_lock(&devices_mutex);
|
|---|
| [17fd1d4] | 623 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 624 | if (lnk == NULL) {
|
|---|
| [2dfd9fa] | 625 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [efcebe1] | 626 | return ENOENT;
|
|---|
| [17fd1d4] | 627 | }
|
|---|
| 628 |
|
|---|
| 629 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| [79ae36dd] | 630 | assert(dev->sess);
|
|---|
| [17fd1d4] | 631 |
|
|---|
| 632 | ipc_callid_t callid;
|
|---|
| [0da4e41] | 633 | if (!async_data_write_receive(&callid, NULL)) {
|
|---|
| [2dfd9fa] | 634 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [ffa2c8ef] | 635 | async_answer_0(callid, EINVAL);
|
|---|
| [efcebe1] | 636 | return EINVAL;
|
|---|
| [17fd1d4] | 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /* Make a request at the driver */
|
|---|
| [79ae36dd] | 640 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 641 |
|
|---|
| [17fd1d4] | 642 | ipc_call_t answer;
|
|---|
| [efcebe1] | 643 | aid_t msg = async_send_4(exch, VFS_OUT_WRITE, devmap_handle,
|
|---|
| 644 | index, LOWER32(pos), UPPER32(pos), &answer);
|
|---|
| [17fd1d4] | 645 |
|
|---|
| 646 | /* Forward the IPC_M_DATA_WRITE request to the driver */
|
|---|
| [79ae36dd] | 647 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 648 |
|
|---|
| 649 | async_exchange_end(exch);
|
|---|
| [17fd1d4] | 650 |
|
|---|
| [2dfd9fa] | 651 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 652 |
|
|---|
| [17fd1d4] | 653 | /* Wait for reply from the driver. */
|
|---|
| [96b02eb9] | 654 | sysarg_t rc;
|
|---|
| [17fd1d4] | 655 | async_wait_for(msg, &rc);
|
|---|
| 656 |
|
|---|
| [efcebe1] | 657 | *wbytes = IPC_GET_ARG1(answer);
|
|---|
| 658 | *nsize = 0;
|
|---|
| 659 | return rc;
|
|---|
| [a095d20] | 660 | }
|
|---|
| [1313ee9] | 661 |
|
|---|
| [efcebe1] | 662 | return ENOENT;
|
|---|
| [a095d20] | 663 | }
|
|---|
| 664 |
|
|---|
| [efcebe1] | 665 | static int
|
|---|
| 666 | devfs_truncate(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t size)
|
|---|
| [a095d20] | 667 | {
|
|---|
| [efcebe1] | 668 | return ENOTSUP;
|
|---|
| [a095d20] | 669 | }
|
|---|
| 670 |
|
|---|
| [efcebe1] | 671 | static int devfs_close(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| [17fd1d4] | 672 | {
|
|---|
| [efcebe1] | 673 | if (index == 0)
|
|---|
| 674 | return EOK;
|
|---|
| [1313ee9] | 675 |
|
|---|
| 676 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 677 |
|
|---|
| 678 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 679 | /* Namespace directory */
|
|---|
| [efcebe1] | 680 | return EOK;
|
|---|
| [1313ee9] | 681 | }
|
|---|
| 682 |
|
|---|
| 683 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| [17fd1d4] | 684 | unsigned long key[] = {
|
|---|
| 685 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 686 | };
|
|---|
| 687 |
|
|---|
| [2dfd9fa] | 688 | fibril_mutex_lock(&devices_mutex);
|
|---|
| [17fd1d4] | 689 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 690 | if (lnk == NULL) {
|
|---|
| [2dfd9fa] | 691 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [efcebe1] | 692 | return ENOENT;
|
|---|
| [17fd1d4] | 693 | }
|
|---|
| 694 |
|
|---|
| 695 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| [79ae36dd] | 696 | assert(dev->sess);
|
|---|
| [17fd1d4] | 697 | dev->refcount--;
|
|---|
| 698 |
|
|---|
| 699 | if (dev->refcount == 0) {
|
|---|
| [79ae36dd] | 700 | async_hangup(dev->sess);
|
|---|
| [17fd1d4] | 701 | hash_table_remove(&devices, key, DEVICES_KEYS);
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| [2dfd9fa] | 704 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 705 |
|
|---|
| [efcebe1] | 706 | return EOK;
|
|---|
| [1313ee9] | 707 | }
|
|---|
| 708 |
|
|---|
| [efcebe1] | 709 | return ENOENT;
|
|---|
| [17fd1d4] | 710 | }
|
|---|
| 711 |
|
|---|
| [efcebe1] | 712 | static int devfs_sync(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| [17fd1d4] | 713 | {
|
|---|
| [efcebe1] | 714 | if (index == 0)
|
|---|
| 715 | return EOK;
|
|---|
| [1313ee9] | 716 |
|
|---|
| 717 | devmap_handle_type_t type = devmap_handle_probe(index);
|
|---|
| 718 |
|
|---|
| 719 | if (type == DEV_HANDLE_NAMESPACE) {
|
|---|
| 720 | /* Namespace directory */
|
|---|
| [efcebe1] | 721 | return EOK;
|
|---|
| [1313ee9] | 722 | }
|
|---|
| 723 |
|
|---|
| 724 | if (type == DEV_HANDLE_DEVICE) {
|
|---|
| [17fd1d4] | 725 | unsigned long key[] = {
|
|---|
| 726 | [DEVICES_KEY_HANDLE] = (unsigned long) index
|
|---|
| 727 | };
|
|---|
| 728 |
|
|---|
| [2dfd9fa] | 729 | fibril_mutex_lock(&devices_mutex);
|
|---|
| [17fd1d4] | 730 | link_t *lnk = hash_table_find(&devices, key);
|
|---|
| 731 | if (lnk == NULL) {
|
|---|
| [2dfd9fa] | 732 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| [efcebe1] | 733 | return ENOENT;
|
|---|
| [17fd1d4] | 734 | }
|
|---|
| 735 |
|
|---|
| 736 | device_t *dev = hash_table_get_instance(lnk, device_t, link);
|
|---|
| [79ae36dd] | 737 | assert(dev->sess);
|
|---|
| [17fd1d4] | 738 |
|
|---|
| 739 | /* Make a request at the driver */
|
|---|
| [79ae36dd] | 740 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 741 |
|
|---|
| [17fd1d4] | 742 | ipc_call_t answer;
|
|---|
| [efcebe1] | 743 | aid_t msg = async_send_2(exch, VFS_OUT_SYNC, devmap_handle,
|
|---|
| 744 | index, &answer);
|
|---|
| [17fd1d4] | 745 |
|
|---|
| [79ae36dd] | 746 | async_exchange_end(exch);
|
|---|
| 747 |
|
|---|
| [2dfd9fa] | 748 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 749 |
|
|---|
| [17fd1d4] | 750 | /* Wait for reply from the driver */
|
|---|
| [96b02eb9] | 751 | sysarg_t rc;
|
|---|
| [17fd1d4] | 752 | async_wait_for(msg, &rc);
|
|---|
| 753 |
|
|---|
| [efcebe1] | 754 | return rc;
|
|---|
| [1313ee9] | 755 | }
|
|---|
| 756 |
|
|---|
| [efcebe1] | 757 | return ENOENT;
|
|---|
| [17fd1d4] | 758 | }
|
|---|
| 759 |
|
|---|
| [efcebe1] | 760 | static int devfs_destroy(devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| [a095d20] | 761 | {
|
|---|
| [efcebe1] | 762 | return ENOTSUP;
|
|---|
| [a095d20] | 763 | }
|
|---|
| 764 |
|
|---|
| [efcebe1] | 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 |
|
|---|
| [a095d20] | 776 | /**
|
|---|
| 777 | * @}
|
|---|
| [17fd1d4] | 778 | */
|
|---|