source: mainline/uspace/srv/vfs/vfs_node.c@ c31d773

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

Grab the nodes_futex when manipulating node link counts in rename() and
_unlink().

Fix VFS futex definitions and declarations by replacing atomic_t by futex_t.

  • Property mode set to 100644
File size: 6.3 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>
40#include <string.h>
41#include <futex.h>
[9413c0d]42#include <rwlock.h>
[b818cff]43#include <libadt/hash_table.h>
[7fff5eab]44#include <assert.h>
[f17667a]45#include <async.h>
46#include <errno.h>
[b818cff]47
48/** Futex protecting the VFS node hash table. */
[c31d773]49futex_t nodes_futex = FUTEX_INITIALIZER;
[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. */
55hash_table_t nodes;
56
57#define KEY_FS_HANDLE 0
58#define KEY_DEV_HANDLE 1
59#define KEY_INDEX 2
60
61static hash_index_t nodes_hash(unsigned long []);
62static int nodes_compare(unsigned long [], hash_count_t, link_t *);
63static void nodes_remove_callback(link_t *);
64
65/** VFS node hash table operations. */
66hash_table_operations_t nodes_ops = {
67 .hash = nodes_hash,
68 .compare = nodes_compare,
69 .remove_callback = nodes_remove_callback
70};
71
72/** Initialize the VFS node hash table.
73 *
74 * @return Return true on success, false on failure.
75 */
76bool vfs_nodes_init(void)
77{
78 return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops);
79}
80
81static inline void _vfs_node_addref(vfs_node_t *node)
82{
83 node->refcnt++;
84}
[ec01adf]85
[320c884]86/** Increment reference count of a VFS node.
87 *
88 * @param node VFS node that will have its refcnt incremented.
89 */
90void vfs_node_addref(vfs_node_t *node)
91{
[b818cff]92 futex_down(&nodes_futex);
93 _vfs_node_addref(node);
94 futex_up(&nodes_futex);
[320c884]95}
96
97/** Decrement reference count of a VFS node.
98 *
99 * This function handles the case when the reference count drops to zero.
100 *
101 * @param node VFS node that will have its refcnt decremented.
102 */
103void vfs_node_delref(vfs_node_t *node)
104{
[f17667a]105 bool free_vfs_node = false;
106 bool free_fs_node = false;
107
[b818cff]108 futex_down(&nodes_futex);
109 if (node->refcnt-- == 1) {
[f17667a]110 /*
111 * We are dropping the last reference to this node.
112 * Remove it from the VFS node hash table.
113 */
[b818cff]114 unsigned long key[] = {
115 [KEY_FS_HANDLE] = node->fs_handle,
116 [KEY_DEV_HANDLE] = node->dev_handle,
117 [KEY_INDEX] = node->index
118 };
119 hash_table_remove(&nodes, key, 3);
[f17667a]120 free_vfs_node = true;
121 if (!node->lnkcnt)
122 free_fs_node = true;
[b818cff]123 }
124 futex_up(&nodes_futex);
[f17667a]125
126 if (free_fs_node) {
127 /*
128 * The node is not visible in the file system namespace.
129 * Free up its resources.
130 */
131 int phone = vfs_grab_phone(node->fs_handle);
132 ipcarg_t rc;
[fdb7795]133 rc = async_req_2_0(phone, VFS_DESTROY,
134 (ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
[f17667a]135 assert(rc == EOK);
136 vfs_release_phone(phone);
137 }
138 if (free_vfs_node)
139 free(node);
[320c884]140}
141
142/** Find VFS node.
143 *
144 * This function will try to lookup the given triplet in the VFS node hash
145 * table. In case the triplet is not found there, a new VFS node is created.
146 * In any case, the VFS node will have its reference count incremented. Every
147 * node returned by this call should be eventually put back by calling
148 * vfs_node_put() on it.
149 *
[eb27ce5a]150 * @param result Populated lookup result structure.
[320c884]151 *
152 * @return VFS node corresponding to the given triplet.
153 */
[eb27ce5a]154vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
[ec01adf]155{
[b818cff]156 unsigned long key[] = {
[eb27ce5a]157 [KEY_FS_HANDLE] = result->triplet.fs_handle,
158 [KEY_DEV_HANDLE] = result->triplet.dev_handle,
159 [KEY_INDEX] = result->triplet.index
[b818cff]160 };
161 link_t *tmp;
162 vfs_node_t *node;
163
164 futex_down(&nodes_futex);
165 tmp = hash_table_find(&nodes, key);
166 if (!tmp) {
167 node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
168 if (!node) {
169 futex_up(&nodes_futex);
170 return NULL;
171 }
172 memset(node, 0, sizeof(vfs_node_t));
[eb27ce5a]173 node->fs_handle = result->triplet.fs_handle;
[d1ce550]174 node->dev_handle = result->triplet.dev_handle;
[eb27ce5a]175 node->index = result->triplet.index;
176 node->size = result->size;
[b5553a2]177 node->lnkcnt = result->lnkcnt;
[b818cff]178 link_initialize(&node->nh_link);
[9413c0d]179 rwlock_initialize(&node->contents_rwlock);
[b818cff]180 hash_table_insert(&nodes, key, &node->nh_link);
181 } else {
182 node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
183 }
[7fff5eab]184
[eb27ce5a]185 assert(node->size == result->size);
[b5553a2]186 assert(node->lnkcnt == result->lnkcnt);
[7fff5eab]187
[b818cff]188 _vfs_node_addref(node);
189 futex_up(&nodes_futex);
190
191 return node;
[320c884]192}
193
194/** Return VFS node when no longer needed by the caller.
195 *
196 * This function will remove the reference on the VFS node created by
197 * vfs_node_get(). This function can only be called as a closing bracket to the
198 * preceding vfs_node_get() call.
199 *
200 * @param node VFS node being released.
201 */
202void vfs_node_put(vfs_node_t *node)
203{
204 vfs_node_delref(node);
[ec01adf]205}
206
[b818cff]207hash_index_t nodes_hash(unsigned long key[])
208{
209 hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4);
210 hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2);
211
[a806bd61]212 return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1);
[b818cff]213}
214
215int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
216{
217 vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
218 return (node->fs_handle == key[KEY_FS_HANDLE]) &&
219 (node->dev_handle == key[KEY_DEV_HANDLE]) &&
220 (node->index == key[KEY_INDEX]);
221}
222
223void nodes_remove_callback(link_t *item)
224{
225}
226
[ec01adf]227/**
228 * @}
229 */
Note: See TracBrowser for help on using the repository browser.