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
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
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"
39#include <stdlib.h>
40#include <str.h>
41#include <fibril_synch.h>
42#include <adt/hash_table.h>
43#include <assert.h>
44#include <async.h>
45#include <errno.h>
46
47/** Mutex protecting the VFS node hash table. */
48FIBRIL_MUTEX_INITIALIZE(nodes_mutex);
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
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 *);
63
64/** VFS node hash table operations. */
65hash_table_ops_t nodes_ops = {
66 .hash = nodes_hash,
67 .key_hash = nodes_key_hash,
68 .match = nodes_match,
69 .equal = 0,
70 .remove_callback = 0,
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{
79 return hash_table_create(&nodes, 0, 3, &nodes_ops);
80}
81
82static inline void _vfs_node_addref(vfs_node_t *node)
83{
84 node->refcnt++;
85}
86
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{
93 fibril_mutex_lock(&nodes_mutex);
94 _vfs_node_addref(node);
95 fibril_mutex_unlock(&nodes_mutex);
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{
106 bool free_vfs_node = false;
107 bool free_fs_node = false;
108
109 fibril_mutex_lock(&nodes_mutex);
110
111 if (node->refcnt-- == 1) {
112
113 /*
114 * We are dropping the last reference to this node.
115 * Remove it from the VFS node hash table.
116 */
117
118 unsigned long key[] = {
119 [KEY_FS_HANDLE] = node->fs_handle,
120 [KEY_DEV_HANDLE] = node->service_id,
121 [KEY_INDEX] = node->index
122 };
123
124 hash_table_remove(&nodes, key, 3);
125 free_vfs_node = true;
126
127 if (!node->lnkcnt)
128 free_fs_node = true;
129 }
130
131 fibril_mutex_unlock(&nodes_mutex);
132
133 if (free_fs_node) {
134
135 /*
136 * The node is not visible in the file system namespace.
137 * Free up its resources.
138 */
139
140 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
141 sysarg_t rc = async_req_2_0(exch, VFS_OUT_DESTROY,
142 (sysarg_t) node->service_id, (sysarg_t)node->index);
143
144 assert(rc == EOK);
145 vfs_exchange_release(exch);
146 }
147
148 if (free_vfs_node)
149 free(node);
150}
151
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,
164 [KEY_DEV_HANDLE] = node->service_id,
165 [KEY_INDEX] = node->index
166 };
167 hash_table_remove(&nodes, key, 3);
168 fibril_mutex_unlock(&nodes_mutex);
169 free(node);
170}
171
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 *
180 * @param result Populated lookup result structure.
181 *
182 * @return VFS node corresponding to the given triplet.
183 */
184vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
185{
186 unsigned long key[] = {
187 [KEY_FS_HANDLE] = result->triplet.fs_handle,
188 [KEY_DEV_HANDLE] = result->triplet.service_id,
189 [KEY_INDEX] = result->triplet.index
190 };
191 link_t *tmp;
192 vfs_node_t *node;
193
194 fibril_mutex_lock(&nodes_mutex);
195 tmp = hash_table_find(&nodes, key);
196 if (!tmp) {
197 node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
198 if (!node) {
199 fibril_mutex_unlock(&nodes_mutex);
200 return NULL;
201 }
202 memset(node, 0, sizeof(vfs_node_t));
203 node->fs_handle = result->triplet.fs_handle;
204 node->service_id = result->triplet.service_id;
205 node->index = result->triplet.index;
206 node->size = result->size;
207 node->lnkcnt = result->lnkcnt;
208 node->type = result->type;
209 link_initialize(&node->nh_link);
210 fibril_rwlock_initialize(&node->contents_rwlock);
211 hash_table_insert(&nodes, &node->nh_link);
212 } else {
213 node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
214 if (node->type == VFS_NODE_UNKNOWN &&
215 result->type != VFS_NODE_UNKNOWN) {
216 /* Upgrade the node type. */
217 node->type = result->type;
218 }
219 }
220
221 assert(node->size == result->size || node->type != VFS_NODE_FILE);
222 assert(node->lnkcnt == result->lnkcnt);
223 assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN);
224
225 _vfs_node_addref(node);
226 fibril_mutex_unlock(&nodes_mutex);
227
228 return node;
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);
242}
243
244size_t nodes_key_hash(unsigned long key[])
245{
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 };
263
264 return nodes_key_hash(key);
265}
266
267
268bool nodes_match(unsigned long key[], size_t keys, const link_t *item)
269{
270 vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
271 return (node->fs_handle == (fs_handle_t) key[KEY_FS_HANDLE]) &&
272 (node->service_id == key[KEY_DEV_HANDLE]) &&
273 (node->index == key[KEY_INDEX]);
274}
275
276
277struct refcnt_data {
278 /** Sum of all reference counts for this file system instance. */
279 unsigned refcnt;
280 fs_handle_t fs_handle;
281 service_id_t service_id;
282};
283
284static bool refcnt_visitor(link_t *item, void *arg)
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) &&
290 (node->service_id == rd->service_id))
291 rd->refcnt += node->refcnt;
292
293 return true;
294}
295
296unsigned
297vfs_nodes_refcount_sum_get(fs_handle_t fs_handle, service_id_t service_id)
298{
299 struct refcnt_data rd = {
300 .refcnt = 0,
301 .fs_handle = fs_handle,
302 .service_id = service_id
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
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,
324 (sysarg_t) node->service_id, (sysarg_t) node->index, &answer);
325
326 vfs_exchange_release(exch);
327
328 sysarg_t rc;
329 async_wait_for(req, &rc);
330
331 return rc;
332}
333
334/**
335 * @}
336 */
Note: See TracBrowser for help on using the repository browser.