source: mainline/uspace/srv/vfs/vfs_node.c@ 2a3214e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2a3214e was 42a619b, checked in by Jakub Jermar <jakub@…>, 14 years ago

Merge from lp:~jakub/helenos/camp2011.

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