source: mainline/uspace/srv/vfs/vfs_node.c@ 42a619b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 42a619b 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
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
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}
[ec01adf]84
[320c884]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{
[553492be]91 fibril_mutex_lock(&nodes_mutex);
[b818cff]92 _vfs_node_addref(node);
[553492be]93 fibril_mutex_unlock(&nodes_mutex);
[320c884]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{
[f17667a]104 bool free_vfs_node = false;
105 bool free_fs_node = false;
[79ae36dd]106
[553492be]107 fibril_mutex_lock(&nodes_mutex);
[79ae36dd]108
[b818cff]109 if (node->refcnt-- == 1) {
[79ae36dd]110
[f17667a]111 /*
112 * We are dropping the last reference to this node.
113 * Remove it from the VFS node hash table.
114 */
[79ae36dd]115
[b818cff]116 unsigned long key[] = {
117 [KEY_FS_HANDLE] = node->fs_handle,
[15f3c3f]118 [KEY_DEV_HANDLE] = node->service_id,
[b818cff]119 [KEY_INDEX] = node->index
120 };
[79ae36dd]121
[b818cff]122 hash_table_remove(&nodes, key, 3);
[f17667a]123 free_vfs_node = true;
[79ae36dd]124
[f17667a]125 if (!node->lnkcnt)
126 free_fs_node = true;
[b818cff]127 }
[79ae36dd]128
[553492be]129 fibril_mutex_unlock(&nodes_mutex);
[79ae36dd]130
[f17667a]131 if (free_fs_node) {
[79ae36dd]132
133 /*
[f17667a]134 * The node is not visible in the file system namespace.
135 * Free up its resources.
136 */
[79ae36dd]137
138 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
139 sysarg_t rc = async_req_2_0(exch, VFS_OUT_DESTROY,
[15f3c3f]140 (sysarg_t) node->service_id, (sysarg_t)node->index);
[79ae36dd]141
[f17667a]142 assert(rc == EOK);
[79ae36dd]143 vfs_exchange_release(exch);
[f17667a]144 }
[79ae36dd]145
[f17667a]146 if (free_vfs_node)
147 free(node);
[320c884]148}
149
[c4aca2c]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,
[15f3c3f]162 [KEY_DEV_HANDLE] = node->service_id,
[c4aca2c]163 [KEY_INDEX] = node->index
164 };
165 hash_table_remove(&nodes, key, 3);
166 fibril_mutex_unlock(&nodes_mutex);
167 free(node);
168}
169
[320c884]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 *
[eb27ce5a]178 * @param result Populated lookup result structure.
[320c884]179 *
180 * @return VFS node corresponding to the given triplet.
181 */
[eb27ce5a]182vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
[ec01adf]183{
[b818cff]184 unsigned long key[] = {
[eb27ce5a]185 [KEY_FS_HANDLE] = result->triplet.fs_handle,
[15f3c3f]186 [KEY_DEV_HANDLE] = result->triplet.service_id,
[eb27ce5a]187 [KEY_INDEX] = result->triplet.index
[b818cff]188 };
189 link_t *tmp;
190 vfs_node_t *node;
191
[553492be]192 fibril_mutex_lock(&nodes_mutex);
[b818cff]193 tmp = hash_table_find(&nodes, key);
194 if (!tmp) {
195 node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
196 if (!node) {
[553492be]197 fibril_mutex_unlock(&nodes_mutex);
[b818cff]198 return NULL;
199 }
200 memset(node, 0, sizeof(vfs_node_t));
[eb27ce5a]201 node->fs_handle = result->triplet.fs_handle;
[15f3c3f]202 node->service_id = result->triplet.service_id;
[eb27ce5a]203 node->index = result->triplet.index;
204 node->size = result->size;
[b5553a2]205 node->lnkcnt = result->lnkcnt;
[b17186d]206 node->type = result->type;
[b818cff]207 link_initialize(&node->nh_link);
[230260ac]208 fibril_rwlock_initialize(&node->contents_rwlock);
[b818cff]209 hash_table_insert(&nodes, key, &node->nh_link);
210 } else {
[05b9912]211 node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
[b17186d]212 if (node->type == VFS_NODE_UNKNOWN &&
213 result->type != VFS_NODE_UNKNOWN) {
214 /* Upgrade the node type. */
215 node->type = result->type;
216 }
[b818cff]217 }
[7fff5eab]218
[8b58fc1]219 assert(node->size == result->size || node->type != VFS_NODE_FILE);
[b5553a2]220 assert(node->lnkcnt == result->lnkcnt);
[b17186d]221 assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN);
[7fff5eab]222
[b818cff]223 _vfs_node_addref(node);
[553492be]224 fibril_mutex_unlock(&nodes_mutex);
[b818cff]225
226 return node;
[320c884]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);
[ec01adf]240}
241
[b818cff]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
[a806bd61]247 return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1);
[b818cff]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);
[ed903174]253 return (node->fs_handle == (fs_handle_t) key[KEY_FS_HANDLE]) &&
[15f3c3f]254 (node->service_id == key[KEY_DEV_HANDLE]) &&
[b818cff]255 (node->index == key[KEY_INDEX]);
256}
257
258void nodes_remove_callback(link_t *item)
259{
260}
261
[319f4fb]262struct refcnt_data {
263 /** Sum of all reference counts for this file system instance. */
264 unsigned refcnt;
265 fs_handle_t fs_handle;
[15f3c3f]266 service_id_t service_id;
[319f4fb]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) &&
[15f3c3f]275 (node->service_id == rd->service_id))
[319f4fb]276 rd->refcnt += node->refcnt;
277}
278
279unsigned
[15f3c3f]280vfs_nodes_refcount_sum_get(fs_handle_t fs_handle, service_id_t service_id)
[319f4fb]281{
282 struct refcnt_data rd = {
283 .refcnt = 0,
284 .fs_handle = fs_handle,
[15f3c3f]285 .service_id = service_id
[319f4fb]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
[44451ee]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,
[42a619b]307 (sysarg_t) node->service_id, (sysarg_t) node->index, &answer);
[44451ee]308
309 vfs_exchange_release(exch);
310
311 sysarg_t rc;
312 async_wait_for(req, &rc);
313
314 return rc;
315}
316
[ec01adf]317/**
318 * @}
[05b9912]319 */
Note: See TracBrowser for help on using the repository browser.