[ec01adf] | 1 | /*
|
---|
[eb27ce5a] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[ec01adf] | 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 vfs_node.c
|
---|
| 35 | * @brief Various operations on VFS nodes have their home in this file.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include "vfs.h"
|
---|
[b818cff] | 39 | #include <stdlib.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[1e4cada] | 41 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 42 | #include <adt/hash_table.h>
|
---|
[062d900] | 43 | #include <adt/hash.h>
|
---|
[7fff5eab] | 44 | #include <assert.h>
|
---|
[f17667a] | 45 | #include <async.h>
|
---|
| 46 | #include <errno.h>
|
---|
[b818cff] | 47 |
|
---|
[553492be] | 48 | /** Mutex protecting the VFS node hash table. */
|
---|
| 49 | FIBRIL_MUTEX_INITIALIZE(nodes_mutex);
|
---|
[b818cff] | 50 |
|
---|
| 51 | #define NODES_BUCKETS_LOG 8
|
---|
| 52 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG)
|
---|
| 53 |
|
---|
| 54 | /** VFS node hash table containing all active, in-memory VFS nodes. */
|
---|
| 55 | hash_table_t nodes;
|
---|
| 56 |
|
---|
| 57 | #define KEY_FS_HANDLE 0
|
---|
| 58 | #define KEY_DEV_HANDLE 1
|
---|
| 59 | #define KEY_INDEX 2
|
---|
| 60 |
|
---|
[062d900] | 61 | static size_t nodes_key_hash(void *);
|
---|
| 62 | static size_t nodes_hash(const ht_link_t *);
|
---|
| 63 | static bool nodes_key_equal(void *, const ht_link_t *);
|
---|
| 64 | static vfs_triplet_t node_triplet(vfs_node_t *node);
|
---|
[b818cff] | 65 |
|
---|
| 66 | /** VFS node hash table operations. */
|
---|
[062d900] | 67 | hash_table_ops_t nodes_ops = {
|
---|
[b818cff] | 68 | .hash = nodes_hash,
|
---|
[062d900] | 69 | .key_hash = nodes_key_hash,
|
---|
| 70 | .key_equal = nodes_key_equal,
|
---|
[4e00f87] | 71 | .equal = NULL,
|
---|
| 72 | .remove_callback = NULL,
|
---|
[b818cff] | 73 | };
|
---|
| 74 |
|
---|
| 75 | /** Initialize the VFS node hash table.
|
---|
| 76 | *
|
---|
| 77 | * @return Return true on success, false on failure.
|
---|
| 78 | */
|
---|
| 79 | bool vfs_nodes_init(void)
|
---|
| 80 | {
|
---|
[062d900] | 81 | return hash_table_create(&nodes, 0, 0, &nodes_ops);
|
---|
[b818cff] | 82 | }
|
---|
| 83 |
|
---|
| 84 | static inline void _vfs_node_addref(vfs_node_t *node)
|
---|
| 85 | {
|
---|
| 86 | node->refcnt++;
|
---|
| 87 | }
|
---|
[ec01adf] | 88 |
|
---|
[320c884] | 89 | /** Increment reference count of a VFS node.
|
---|
| 90 | *
|
---|
| 91 | * @param node VFS node that will have its refcnt incremented.
|
---|
| 92 | */
|
---|
| 93 | void vfs_node_addref(vfs_node_t *node)
|
---|
| 94 | {
|
---|
[553492be] | 95 | fibril_mutex_lock(&nodes_mutex);
|
---|
[b818cff] | 96 | _vfs_node_addref(node);
|
---|
[553492be] | 97 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[320c884] | 98 | }
|
---|
| 99 |
|
---|
| 100 | /** Decrement reference count of a VFS node.
|
---|
| 101 | *
|
---|
| 102 | * This function handles the case when the reference count drops to zero.
|
---|
| 103 | *
|
---|
| 104 | * @param node VFS node that will have its refcnt decremented.
|
---|
| 105 | */
|
---|
| 106 | void vfs_node_delref(vfs_node_t *node)
|
---|
| 107 | {
|
---|
[f17667a] | 108 | bool free_vfs_node = false;
|
---|
| 109 | bool free_fs_node = false;
|
---|
[79ae36dd] | 110 |
|
---|
[553492be] | 111 | fibril_mutex_lock(&nodes_mutex);
|
---|
[79ae36dd] | 112 |
|
---|
[b818cff] | 113 | if (node->refcnt-- == 1) {
|
---|
[79ae36dd] | 114 |
|
---|
[f17667a] | 115 | /*
|
---|
| 116 | * We are dropping the last reference to this node.
|
---|
| 117 | * Remove it from the VFS node hash table.
|
---|
| 118 | */
|
---|
[79ae36dd] | 119 |
|
---|
[062d900] | 120 | hash_table_remove_item(&nodes, &node->nh_link);
|
---|
[f17667a] | 121 | free_vfs_node = true;
|
---|
[79ae36dd] | 122 |
|
---|
[f17667a] | 123 | if (!node->lnkcnt)
|
---|
| 124 | free_fs_node = true;
|
---|
[b818cff] | 125 | }
|
---|
[79ae36dd] | 126 |
|
---|
[553492be] | 127 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[79ae36dd] | 128 |
|
---|
[f17667a] | 129 | if (free_fs_node) {
|
---|
[79ae36dd] | 130 |
|
---|
| 131 | /*
|
---|
[f17667a] | 132 | * The node is not visible in the file system namespace.
|
---|
| 133 | * Free up its resources.
|
---|
| 134 | */
|
---|
[79ae36dd] | 135 |
|
---|
| 136 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
| 137 | sysarg_t rc = async_req_2_0(exch, VFS_OUT_DESTROY,
|
---|
[15f3c3f] | 138 | (sysarg_t) node->service_id, (sysarg_t)node->index);
|
---|
[79ae36dd] | 139 |
|
---|
[f17667a] | 140 | assert(rc == EOK);
|
---|
[79ae36dd] | 141 | vfs_exchange_release(exch);
|
---|
[f17667a] | 142 | }
|
---|
[79ae36dd] | 143 |
|
---|
[f17667a] | 144 | if (free_vfs_node)
|
---|
| 145 | free(node);
|
---|
[320c884] | 146 | }
|
---|
| 147 |
|
---|
[c4aca2c] | 148 | /** Forget node.
|
---|
| 149 | *
|
---|
| 150 | * This function will remove the node from the node hash table and deallocate
|
---|
| 151 | * its memory, regardless of the node's reference count.
|
---|
| 152 | *
|
---|
| 153 | * @param node Node to be forgotten.
|
---|
| 154 | */
|
---|
| 155 | void vfs_node_forget(vfs_node_t *node)
|
---|
| 156 | {
|
---|
| 157 | fibril_mutex_lock(&nodes_mutex);
|
---|
[062d900] | 158 | hash_table_remove_item(&nodes, &node->nh_link);
|
---|
[c4aca2c] | 159 | fibril_mutex_unlock(&nodes_mutex);
|
---|
| 160 | free(node);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[320c884] | 163 | /** Find VFS node.
|
---|
| 164 | *
|
---|
| 165 | * This function will try to lookup the given triplet in the VFS node hash
|
---|
| 166 | * table. In case the triplet is not found there, a new VFS node is created.
|
---|
| 167 | * In any case, the VFS node will have its reference count incremented. Every
|
---|
| 168 | * node returned by this call should be eventually put back by calling
|
---|
| 169 | * vfs_node_put() on it.
|
---|
| 170 | *
|
---|
[eb27ce5a] | 171 | * @param result Populated lookup result structure.
|
---|
[320c884] | 172 | *
|
---|
| 173 | * @return VFS node corresponding to the given triplet.
|
---|
| 174 | */
|
---|
[eb27ce5a] | 175 | vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
|
---|
[ec01adf] | 176 | {
|
---|
[b818cff] | 177 | vfs_node_t *node;
|
---|
| 178 |
|
---|
[553492be] | 179 | fibril_mutex_lock(&nodes_mutex);
|
---|
[062d900] | 180 | ht_link_t *tmp = hash_table_find(&nodes, &result->triplet);
|
---|
[b818cff] | 181 | if (!tmp) {
|
---|
| 182 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
|
---|
| 183 | if (!node) {
|
---|
[553492be] | 184 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[b818cff] | 185 | return NULL;
|
---|
| 186 | }
|
---|
| 187 | memset(node, 0, sizeof(vfs_node_t));
|
---|
[eb27ce5a] | 188 | node->fs_handle = result->triplet.fs_handle;
|
---|
[15f3c3f] | 189 | node->service_id = result->triplet.service_id;
|
---|
[eb27ce5a] | 190 | node->index = result->triplet.index;
|
---|
| 191 | node->size = result->size;
|
---|
[b5553a2] | 192 | node->lnkcnt = result->lnkcnt;
|
---|
[b17186d] | 193 | node->type = result->type;
|
---|
[230260ac] | 194 | fibril_rwlock_initialize(&node->contents_rwlock);
|
---|
[062d900] | 195 | hash_table_insert(&nodes, &node->nh_link);
|
---|
[b818cff] | 196 | } else {
|
---|
[062d900] | 197 | node = hash_table_get_inst(tmp, vfs_node_t, nh_link);
|
---|
[b17186d] | 198 | if (node->type == VFS_NODE_UNKNOWN &&
|
---|
| 199 | result->type != VFS_NODE_UNKNOWN) {
|
---|
| 200 | /* Upgrade the node type. */
|
---|
| 201 | node->type = result->type;
|
---|
| 202 | }
|
---|
[b818cff] | 203 | }
|
---|
[7fff5eab] | 204 |
|
---|
[8b58fc1] | 205 | assert(node->size == result->size || node->type != VFS_NODE_FILE);
|
---|
[b5553a2] | 206 | assert(node->lnkcnt == result->lnkcnt);
|
---|
[b17186d] | 207 | assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN);
|
---|
[7fff5eab] | 208 |
|
---|
[b818cff] | 209 | _vfs_node_addref(node);
|
---|
[553492be] | 210 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[b818cff] | 211 |
|
---|
| 212 | return node;
|
---|
[320c884] | 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Return VFS node when no longer needed by the caller.
|
---|
| 216 | *
|
---|
| 217 | * This function will remove the reference on the VFS node created by
|
---|
| 218 | * vfs_node_get(). This function can only be called as a closing bracket to the
|
---|
| 219 | * preceding vfs_node_get() call.
|
---|
| 220 | *
|
---|
| 221 | * @param node VFS node being released.
|
---|
| 222 | */
|
---|
| 223 | void vfs_node_put(vfs_node_t *node)
|
---|
| 224 | {
|
---|
| 225 | vfs_node_delref(node);
|
---|
[ec01adf] | 226 | }
|
---|
| 227 |
|
---|
[319f4fb] | 228 | struct refcnt_data {
|
---|
| 229 | /** Sum of all reference counts for this file system instance. */
|
---|
| 230 | unsigned refcnt;
|
---|
| 231 | fs_handle_t fs_handle;
|
---|
[15f3c3f] | 232 | service_id_t service_id;
|
---|
[319f4fb] | 233 | };
|
---|
| 234 |
|
---|
[062d900] | 235 | static bool refcnt_visitor(ht_link_t *item, void *arg)
|
---|
[319f4fb] | 236 | {
|
---|
[062d900] | 237 | vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
|
---|
[319f4fb] | 238 | struct refcnt_data *rd = (void *) arg;
|
---|
| 239 |
|
---|
| 240 | if ((node->fs_handle == rd->fs_handle) &&
|
---|
[15f3c3f] | 241 | (node->service_id == rd->service_id))
|
---|
[319f4fb] | 242 | rd->refcnt += node->refcnt;
|
---|
[062d900] | 243 |
|
---|
| 244 | return true;
|
---|
[319f4fb] | 245 | }
|
---|
| 246 |
|
---|
| 247 | unsigned
|
---|
[15f3c3f] | 248 | vfs_nodes_refcount_sum_get(fs_handle_t fs_handle, service_id_t service_id)
|
---|
[319f4fb] | 249 | {
|
---|
| 250 | struct refcnt_data rd = {
|
---|
| 251 | .refcnt = 0,
|
---|
| 252 | .fs_handle = fs_handle,
|
---|
[15f3c3f] | 253 | .service_id = service_id
|
---|
[319f4fb] | 254 | };
|
---|
| 255 |
|
---|
| 256 | fibril_mutex_lock(&nodes_mutex);
|
---|
| 257 | hash_table_apply(&nodes, refcnt_visitor, &rd);
|
---|
| 258 | fibril_mutex_unlock(&nodes_mutex);
|
---|
| 259 |
|
---|
| 260 | return rd.refcnt;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[44451ee] | 263 |
|
---|
| 264 | /** Perform a remote node open operation.
|
---|
| 265 | *
|
---|
| 266 | * @return EOK on success or an error code from errno.h.
|
---|
| 267 | *
|
---|
| 268 | */
|
---|
| 269 | int vfs_open_node_remote(vfs_node_t *node)
|
---|
| 270 | {
|
---|
| 271 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
| 272 |
|
---|
| 273 | ipc_call_t answer;
|
---|
| 274 | aid_t req = async_send_2(exch, VFS_OUT_OPEN_NODE,
|
---|
[42a619b] | 275 | (sysarg_t) node->service_id, (sysarg_t) node->index, &answer);
|
---|
[44451ee] | 276 |
|
---|
| 277 | vfs_exchange_release(exch);
|
---|
| 278 |
|
---|
| 279 | sysarg_t rc;
|
---|
| 280 | async_wait_for(req, &rc);
|
---|
| 281 |
|
---|
| 282 | return rc;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[062d900] | 285 |
|
---|
| 286 | static size_t nodes_key_hash(void *key)
|
---|
| 287 | {
|
---|
| 288 | vfs_triplet_t *tri = key;
|
---|
| 289 | size_t hash = hash_combine(tri->fs_handle, tri->index);
|
---|
| 290 | return hash_combine(hash, tri->service_id);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | static size_t nodes_hash(const ht_link_t *item)
|
---|
| 294 | {
|
---|
| 295 | vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
|
---|
| 296 | vfs_triplet_t tri = node_triplet(node);
|
---|
| 297 | return nodes_key_hash(&tri);
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | static bool nodes_key_equal(void *key, const ht_link_t *item)
|
---|
| 301 | {
|
---|
| 302 | vfs_triplet_t *tri = key;
|
---|
| 303 | vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
|
---|
| 304 | return node->fs_handle == tri->fs_handle
|
---|
| 305 | && node->service_id == tri->service_id
|
---|
| 306 | && node->index == tri->index;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | static inline vfs_triplet_t node_triplet(vfs_node_t *node)
|
---|
| 310 | {
|
---|
| 311 | vfs_triplet_t tri = {
|
---|
| 312 | .fs_handle = node->fs_handle,
|
---|
| 313 | .service_id = node->service_id,
|
---|
| 314 | .index = node->index
|
---|
| 315 | };
|
---|
| 316 |
|
---|
| 317 | return tri;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[ec01adf] | 320 | /**
|
---|
| 321 | * @}
|
---|
[05b9912] | 322 | */
|
---|