Rev 2730 | Rev 2740 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2590 | jermar | 1 | /* |
2691 | jermar | 2 | * Copyright (c) 2008 Jakub Jermar |
2590 | 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" |
||
2596 | jermar | 39 | #include <stdlib.h> |
40 | #include <string.h> |
||
41 | #include <atomic.h> |
||
42 | #include <futex.h> |
||
2680 | jermar | 43 | #include <rwlock.h> |
2596 | jermar | 44 | #include <libadt/hash_table.h> |
2687 | jermar | 45 | #include <assert.h> |
2731 | jermar | 46 | #include <async.h> |
47 | #include <errno.h> |
||
2590 | jermar | 48 | |
2596 | jermar | 49 | /** Futex protecting the VFS node hash table. */ |
50 | atomic_t nodes_futex = FUTEX_INITIALIZER; |
||
51 | |||
52 | #define NODES_BUCKETS_LOG 8 |
||
53 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
||
54 | |||
55 | /** VFS node hash table containing all active, in-memory VFS nodes. */ |
||
56 | hash_table_t nodes; |
||
57 | |||
58 | #define KEY_FS_HANDLE 0 |
||
59 | #define KEY_DEV_HANDLE 1 |
||
60 | #define KEY_INDEX 2 |
||
61 | |||
62 | static hash_index_t nodes_hash(unsigned long []); |
||
63 | static int nodes_compare(unsigned long [], hash_count_t, link_t *); |
||
64 | static void nodes_remove_callback(link_t *); |
||
65 | |||
66 | /** VFS node hash table operations. */ |
||
67 | hash_table_operations_t nodes_ops = { |
||
68 | .hash = nodes_hash, |
||
69 | .compare = nodes_compare, |
||
70 | .remove_callback = nodes_remove_callback |
||
71 | }; |
||
72 | |||
73 | /** Initialize the VFS node hash table. |
||
74 | * |
||
75 | * @return Return true on success, false on failure. |
||
76 | */ |
||
77 | bool vfs_nodes_init(void) |
||
78 | { |
||
79 | return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops); |
||
80 | } |
||
81 | |||
82 | static inline void _vfs_node_addref(vfs_node_t *node) |
||
83 | { |
||
84 | node->refcnt++; |
||
85 | } |
||
86 | |||
2593 | jermar | 87 | /** Increment reference count of a VFS node. |
88 | * |
||
89 | * @param node VFS node that will have its refcnt incremented. |
||
90 | */ |
||
91 | void vfs_node_addref(vfs_node_t *node) |
||
92 | { |
||
2596 | jermar | 93 | futex_down(&nodes_futex); |
94 | _vfs_node_addref(node); |
||
95 | futex_up(&nodes_futex); |
||
2593 | jermar | 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 | */ |
||
104 | void vfs_node_delref(vfs_node_t *node) |
||
105 | { |
||
2731 | jermar | 106 | bool free_vfs_node = false; |
107 | bool free_fs_node = false; |
||
108 | |||
2596 | jermar | 109 | futex_down(&nodes_futex); |
110 | if (node->refcnt-- == 1) { |
||
2731 | jermar | 111 | /* |
112 | * We are dropping the last reference to this node. |
||
113 | * Remove it from the VFS node hash table. |
||
114 | */ |
||
2596 | jermar | 115 | unsigned long key[] = { |
116 | [KEY_FS_HANDLE] = node->fs_handle, |
||
117 | [KEY_DEV_HANDLE] = node->dev_handle, |
||
118 | [KEY_INDEX] = node->index |
||
119 | }; |
||
120 | hash_table_remove(&nodes, key, 3); |
||
2731 | jermar | 121 | free_vfs_node = true; |
122 | if (!node->lnkcnt) |
||
123 | free_fs_node = true; |
||
2596 | jermar | 124 | } |
125 | futex_up(&nodes_futex); |
||
2731 | jermar | 126 | |
127 | if (free_fs_node) { |
||
128 | /* |
||
129 | * The node is not visible in the file system namespace. |
||
130 | * Free up its resources. |
||
131 | */ |
||
132 | int phone = vfs_grab_phone(node->fs_handle); |
||
133 | ipcarg_t rc; |
||
134 | rc = async_req_2_0(phone, VFS_FREE, (ipcarg_t)node->dev_handle, |
||
135 | (ipcarg_t)node->index); |
||
136 | assert(rc == EOK); |
||
137 | vfs_release_phone(phone); |
||
138 | } |
||
139 | if (free_vfs_node) |
||
140 | free(node); |
||
2593 | jermar | 141 | } |
142 | |||
143 | /** Find VFS node. |
||
144 | * |
||
145 | * This function will try to lookup the given triplet in the VFS node hash |
||
146 | * table. In case the triplet is not found there, a new VFS node is created. |
||
147 | * In any case, the VFS node will have its reference count incremented. Every |
||
148 | * node returned by this call should be eventually put back by calling |
||
149 | * vfs_node_put() on it. |
||
150 | * |
||
2691 | jermar | 151 | * @param result Populated lookup result structure. |
2593 | jermar | 152 | * |
153 | * @return VFS node corresponding to the given triplet. |
||
154 | */ |
||
2691 | jermar | 155 | vfs_node_t *vfs_node_get(vfs_lookup_res_t *result) |
2590 | jermar | 156 | { |
2596 | jermar | 157 | unsigned long key[] = { |
2691 | jermar | 158 | [KEY_FS_HANDLE] = result->triplet.fs_handle, |
159 | [KEY_DEV_HANDLE] = result->triplet.dev_handle, |
||
160 | [KEY_INDEX] = result->triplet.index |
||
2596 | jermar | 161 | }; |
162 | link_t *tmp; |
||
163 | vfs_node_t *node; |
||
164 | |||
165 | futex_down(&nodes_futex); |
||
166 | tmp = hash_table_find(&nodes, key); |
||
167 | if (!tmp) { |
||
168 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
||
169 | if (!node) { |
||
170 | futex_up(&nodes_futex); |
||
171 | return NULL; |
||
172 | } |
||
173 | memset(node, 0, sizeof(vfs_node_t)); |
||
2691 | jermar | 174 | node->fs_handle = result->triplet.fs_handle; |
175 | node->dev_handle = result->triplet.fs_handle; |
||
176 | node->index = result->triplet.index; |
||
177 | node->size = result->size; |
||
2730 | jermar | 178 | node->lnkcnt = result->lnkcnt; |
2596 | jermar | 179 | link_initialize(&node->nh_link); |
2680 | jermar | 180 | rwlock_initialize(&node->contents_rwlock); |
2596 | jermar | 181 | hash_table_insert(&nodes, key, &node->nh_link); |
182 | } else { |
||
183 | node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
||
184 | } |
||
2687 | jermar | 185 | |
2691 | jermar | 186 | assert(node->size == result->size); |
2730 | jermar | 187 | assert(node->lnkcnt == result->lnkcnt); |
2687 | jermar | 188 | |
2596 | jermar | 189 | _vfs_node_addref(node); |
190 | futex_up(&nodes_futex); |
||
191 | |||
192 | return node; |
||
2590 | jermar | 193 | } |
194 | |||
2593 | jermar | 195 | /** Return VFS node when no longer needed by the caller. |
196 | * |
||
197 | * This function will remove the reference on the VFS node created by |
||
198 | * vfs_node_get(). This function can only be called as a closing bracket to the |
||
199 | * preceding vfs_node_get() call. |
||
200 | * |
||
201 | * @param node VFS node being released. |
||
202 | */ |
||
203 | void vfs_node_put(vfs_node_t *node) |
||
204 | { |
||
205 | vfs_node_delref(node); |
||
206 | } |
||
207 | |||
2596 | jermar | 208 | hash_index_t nodes_hash(unsigned long key[]) |
209 | { |
||
210 | hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4); |
||
211 | hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2); |
||
212 | |||
2652 | jermar | 213 | return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1); |
2596 | jermar | 214 | } |
215 | |||
216 | int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
||
217 | { |
||
218 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
||
219 | return (node->fs_handle == key[KEY_FS_HANDLE]) && |
||
220 | (node->dev_handle == key[KEY_DEV_HANDLE]) && |
||
221 | (node->index == key[KEY_INDEX]); |
||
222 | } |
||
223 | |||
224 | void nodes_remove_callback(link_t *item) |
||
225 | { |
||
226 | } |
||
227 | |||
2590 | jermar | 228 | /** |
229 | * @} |
||
230 | */ |