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