Rev 2730 | Rev 2740 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2730 | Rev 2731 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file vfs_node.c |
34 | * @file vfs_node.c |
35 | * @brief Various operations on VFS nodes have their home in this file. |
35 | * @brief Various operations on VFS nodes have their home in this file. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "vfs.h" |
38 | #include "vfs.h" |
39 | #include <stdlib.h> |
39 | #include <stdlib.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | #include <atomic.h> |
41 | #include <atomic.h> |
42 | #include <futex.h> |
42 | #include <futex.h> |
43 | #include <rwlock.h> |
43 | #include <rwlock.h> |
44 | #include <libadt/hash_table.h> |
44 | #include <libadt/hash_table.h> |
45 | #include <assert.h> |
45 | #include <assert.h> |
- | 46 | #include <async.h> |
|
- | 47 | #include <errno.h> |
|
46 | 48 | ||
47 | /** Futex protecting the VFS node hash table. */ |
49 | /** Futex protecting the VFS node hash table. */ |
48 | atomic_t nodes_futex = FUTEX_INITIALIZER; |
50 | atomic_t nodes_futex = FUTEX_INITIALIZER; |
49 | 51 | ||
50 | #define NODES_BUCKETS_LOG 8 |
52 | #define NODES_BUCKETS_LOG 8 |
51 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
53 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
52 | 54 | ||
53 | /** VFS node hash table containing all active, in-memory VFS nodes. */ |
55 | /** VFS node hash table containing all active, in-memory VFS nodes. */ |
54 | hash_table_t nodes; |
56 | hash_table_t nodes; |
55 | 57 | ||
56 | #define KEY_FS_HANDLE 0 |
58 | #define KEY_FS_HANDLE 0 |
57 | #define KEY_DEV_HANDLE 1 |
59 | #define KEY_DEV_HANDLE 1 |
58 | #define KEY_INDEX 2 |
60 | #define KEY_INDEX 2 |
59 | 61 | ||
60 | static hash_index_t nodes_hash(unsigned long []); |
62 | static hash_index_t nodes_hash(unsigned long []); |
61 | static int nodes_compare(unsigned long [], hash_count_t, link_t *); |
63 | static int nodes_compare(unsigned long [], hash_count_t, link_t *); |
62 | static void nodes_remove_callback(link_t *); |
64 | static void nodes_remove_callback(link_t *); |
63 | 65 | ||
64 | /** VFS node hash table operations. */ |
66 | /** VFS node hash table operations. */ |
65 | hash_table_operations_t nodes_ops = { |
67 | hash_table_operations_t nodes_ops = { |
66 | .hash = nodes_hash, |
68 | .hash = nodes_hash, |
67 | .compare = nodes_compare, |
69 | .compare = nodes_compare, |
68 | .remove_callback = nodes_remove_callback |
70 | .remove_callback = nodes_remove_callback |
69 | }; |
71 | }; |
70 | 72 | ||
71 | /** Initialize the VFS node hash table. |
73 | /** Initialize the VFS node hash table. |
72 | * |
74 | * |
73 | * @return Return true on success, false on failure. |
75 | * @return Return true on success, false on failure. |
74 | */ |
76 | */ |
75 | bool vfs_nodes_init(void) |
77 | bool vfs_nodes_init(void) |
76 | { |
78 | { |
77 | return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops); |
79 | return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops); |
78 | } |
80 | } |
79 | 81 | ||
80 | static inline void _vfs_node_addref(vfs_node_t *node) |
82 | static inline void _vfs_node_addref(vfs_node_t *node) |
81 | { |
83 | { |
82 | node->refcnt++; |
84 | node->refcnt++; |
83 | } |
85 | } |
84 | 86 | ||
85 | /** Increment reference count of a VFS node. |
87 | /** Increment reference count of a VFS node. |
86 | * |
88 | * |
87 | * @param node VFS node that will have its refcnt incremented. |
89 | * @param node VFS node that will have its refcnt incremented. |
88 | */ |
90 | */ |
89 | void vfs_node_addref(vfs_node_t *node) |
91 | void vfs_node_addref(vfs_node_t *node) |
90 | { |
92 | { |
91 | futex_down(&nodes_futex); |
93 | futex_down(&nodes_futex); |
92 | _vfs_node_addref(node); |
94 | _vfs_node_addref(node); |
93 | futex_up(&nodes_futex); |
95 | futex_up(&nodes_futex); |
94 | } |
96 | } |
95 | 97 | ||
96 | /** Decrement reference count of a VFS node. |
98 | /** Decrement reference count of a VFS node. |
97 | * |
99 | * |
98 | * This function handles the case when the reference count drops to zero. |
100 | * This function handles the case when the reference count drops to zero. |
99 | * |
101 | * |
100 | * @param node VFS node that will have its refcnt decremented. |
102 | * @param node VFS node that will have its refcnt decremented. |
101 | */ |
103 | */ |
102 | void vfs_node_delref(vfs_node_t *node) |
104 | void vfs_node_delref(vfs_node_t *node) |
103 | { |
105 | { |
- | 106 | bool free_vfs_node = false; |
|
- | 107 | bool free_fs_node = false; |
|
- | 108 | ||
104 | futex_down(&nodes_futex); |
109 | futex_down(&nodes_futex); |
105 | if (node->refcnt-- == 1) { |
110 | if (node->refcnt-- == 1) { |
- | 111 | /* |
|
- | 112 | * We are dropping the last reference to this node. |
|
- | 113 | * Remove it from the VFS node hash table. |
|
- | 114 | */ |
|
106 | unsigned long key[] = { |
115 | unsigned long key[] = { |
107 | [KEY_FS_HANDLE] = node->fs_handle, |
116 | [KEY_FS_HANDLE] = node->fs_handle, |
108 | [KEY_DEV_HANDLE] = node->dev_handle, |
117 | [KEY_DEV_HANDLE] = node->dev_handle, |
109 | [KEY_INDEX] = node->index |
118 | [KEY_INDEX] = node->index |
110 | }; |
119 | }; |
111 | hash_table_remove(&nodes, key, 3); |
120 | hash_table_remove(&nodes, key, 3); |
- | 121 | free_vfs_node = true; |
|
- | 122 | if (!node->lnkcnt) |
|
- | 123 | free_fs_node = true; |
|
112 | } |
124 | } |
113 | futex_up(&nodes_futex); |
125 | futex_up(&nodes_futex); |
- | 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); |
|
114 | } |
141 | } |
115 | 142 | ||
116 | /** Find VFS node. |
143 | /** Find VFS node. |
117 | * |
144 | * |
118 | * This function will try to lookup the given triplet in the VFS node hash |
145 | * This function will try to lookup the given triplet in the VFS node hash |
119 | * table. In case the triplet is not found there, a new VFS node is created. |
146 | * table. In case the triplet is not found there, a new VFS node is created. |
120 | * In any case, the VFS node will have its reference count incremented. Every |
147 | * In any case, the VFS node will have its reference count incremented. Every |
121 | * node returned by this call should be eventually put back by calling |
148 | * node returned by this call should be eventually put back by calling |
122 | * vfs_node_put() on it. |
149 | * vfs_node_put() on it. |
123 | * |
150 | * |
124 | * @param result Populated lookup result structure. |
151 | * @param result Populated lookup result structure. |
125 | * |
152 | * |
126 | * @return VFS node corresponding to the given triplet. |
153 | * @return VFS node corresponding to the given triplet. |
127 | */ |
154 | */ |
128 | vfs_node_t *vfs_node_get(vfs_lookup_res_t *result) |
155 | vfs_node_t *vfs_node_get(vfs_lookup_res_t *result) |
129 | { |
156 | { |
130 | unsigned long key[] = { |
157 | unsigned long key[] = { |
131 | [KEY_FS_HANDLE] = result->triplet.fs_handle, |
158 | [KEY_FS_HANDLE] = result->triplet.fs_handle, |
132 | [KEY_DEV_HANDLE] = result->triplet.dev_handle, |
159 | [KEY_DEV_HANDLE] = result->triplet.dev_handle, |
133 | [KEY_INDEX] = result->triplet.index |
160 | [KEY_INDEX] = result->triplet.index |
134 | }; |
161 | }; |
135 | link_t *tmp; |
162 | link_t *tmp; |
136 | vfs_node_t *node; |
163 | vfs_node_t *node; |
137 | 164 | ||
138 | futex_down(&nodes_futex); |
165 | futex_down(&nodes_futex); |
139 | tmp = hash_table_find(&nodes, key); |
166 | tmp = hash_table_find(&nodes, key); |
140 | if (!tmp) { |
167 | if (!tmp) { |
141 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
168 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
142 | if (!node) { |
169 | if (!node) { |
143 | futex_up(&nodes_futex); |
170 | futex_up(&nodes_futex); |
144 | return NULL; |
171 | return NULL; |
145 | } |
172 | } |
146 | memset(node, 0, sizeof(vfs_node_t)); |
173 | memset(node, 0, sizeof(vfs_node_t)); |
147 | node->fs_handle = result->triplet.fs_handle; |
174 | node->fs_handle = result->triplet.fs_handle; |
148 | node->dev_handle = result->triplet.fs_handle; |
175 | node->dev_handle = result->triplet.fs_handle; |
149 | node->index = result->triplet.index; |
176 | node->index = result->triplet.index; |
150 | node->size = result->size; |
177 | node->size = result->size; |
151 | node->lnkcnt = result->lnkcnt; |
178 | node->lnkcnt = result->lnkcnt; |
152 | link_initialize(&node->nh_link); |
179 | link_initialize(&node->nh_link); |
153 | rwlock_initialize(&node->contents_rwlock); |
180 | rwlock_initialize(&node->contents_rwlock); |
154 | hash_table_insert(&nodes, key, &node->nh_link); |
181 | hash_table_insert(&nodes, key, &node->nh_link); |
155 | } else { |
182 | } else { |
156 | node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
183 | node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
157 | } |
184 | } |
158 | 185 | ||
159 | assert(node->size == result->size); |
186 | assert(node->size == result->size); |
160 | assert(node->lnkcnt == result->lnkcnt); |
187 | assert(node->lnkcnt == result->lnkcnt); |
161 | 188 | ||
162 | _vfs_node_addref(node); |
189 | _vfs_node_addref(node); |
163 | futex_up(&nodes_futex); |
190 | futex_up(&nodes_futex); |
164 | 191 | ||
165 | return node; |
192 | return node; |
166 | } |
193 | } |
167 | 194 | ||
168 | /** Return VFS node when no longer needed by the caller. |
195 | /** Return VFS node when no longer needed by the caller. |
169 | * |
196 | * |
170 | * This function will remove the reference on the VFS node created by |
197 | * This function will remove the reference on the VFS node created by |
171 | * vfs_node_get(). This function can only be called as a closing bracket to the |
198 | * vfs_node_get(). This function can only be called as a closing bracket to the |
172 | * preceding vfs_node_get() call. |
199 | * preceding vfs_node_get() call. |
173 | * |
200 | * |
174 | * @param node VFS node being released. |
201 | * @param node VFS node being released. |
175 | */ |
202 | */ |
176 | void vfs_node_put(vfs_node_t *node) |
203 | void vfs_node_put(vfs_node_t *node) |
177 | { |
204 | { |
178 | vfs_node_delref(node); |
205 | vfs_node_delref(node); |
179 | } |
206 | } |
180 | 207 | ||
181 | hash_index_t nodes_hash(unsigned long key[]) |
208 | hash_index_t nodes_hash(unsigned long key[]) |
182 | { |
209 | { |
183 | hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4); |
210 | hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4); |
184 | hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2); |
211 | hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2); |
185 | 212 | ||
186 | return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1); |
213 | return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1); |
187 | } |
214 | } |
188 | 215 | ||
189 | int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
216 | int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
190 | { |
217 | { |
191 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
218 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
192 | return (node->fs_handle == key[KEY_FS_HANDLE]) && |
219 | return (node->fs_handle == key[KEY_FS_HANDLE]) && |
193 | (node->dev_handle == key[KEY_DEV_HANDLE]) && |
220 | (node->dev_handle == key[KEY_DEV_HANDLE]) && |
194 | (node->index == key[KEY_INDEX]); |
221 | (node->index == key[KEY_INDEX]); |
195 | } |
222 | } |
196 | 223 | ||
197 | void nodes_remove_callback(link_t *item) |
224 | void nodes_remove_callback(link_t *item) |
198 | { |
225 | { |
199 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
- | |
200 | free(node); |
- | |
201 | } |
226 | } |
202 | 227 | ||
203 | /** |
228 | /** |
204 | * @} |
229 | * @} |
205 | */ |
230 | */ |
206 | 231 |