source: mainline/uspace/srv/vfs/vfs_node.c@ 3bb732b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3bb732b was 0ca7286, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

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