Rev 2680 | Rev 2691 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2680 | Rev 2687 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 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 | 46 | ||
46 | /** Futex protecting the VFS node hash table. */ |
47 | /** Futex protecting the VFS node hash table. */ |
47 | atomic_t nodes_futex = FUTEX_INITIALIZER; |
48 | atomic_t nodes_futex = FUTEX_INITIALIZER; |
48 | 49 | ||
49 | #define NODES_BUCKETS_LOG 8 |
50 | #define NODES_BUCKETS_LOG 8 |
50 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
51 | #define NODES_BUCKETS (1 << NODES_BUCKETS_LOG) |
51 | 52 | ||
52 | /** VFS node hash table containing all active, in-memory VFS nodes. */ |
53 | /** VFS node hash table containing all active, in-memory VFS nodes. */ |
53 | hash_table_t nodes; |
54 | hash_table_t nodes; |
54 | 55 | ||
55 | #define KEY_FS_HANDLE 0 |
56 | #define KEY_FS_HANDLE 0 |
56 | #define KEY_DEV_HANDLE 1 |
57 | #define KEY_DEV_HANDLE 1 |
57 | #define KEY_INDEX 2 |
58 | #define KEY_INDEX 2 |
58 | 59 | ||
59 | static hash_index_t nodes_hash(unsigned long []); |
60 | static hash_index_t nodes_hash(unsigned long []); |
60 | static int nodes_compare(unsigned long [], hash_count_t, link_t *); |
61 | static int nodes_compare(unsigned long [], hash_count_t, link_t *); |
61 | static void nodes_remove_callback(link_t *); |
62 | static void nodes_remove_callback(link_t *); |
62 | 63 | ||
63 | /** VFS node hash table operations. */ |
64 | /** VFS node hash table operations. */ |
64 | hash_table_operations_t nodes_ops = { |
65 | hash_table_operations_t nodes_ops = { |
65 | .hash = nodes_hash, |
66 | .hash = nodes_hash, |
66 | .compare = nodes_compare, |
67 | .compare = nodes_compare, |
67 | .remove_callback = nodes_remove_callback |
68 | .remove_callback = nodes_remove_callback |
68 | }; |
69 | }; |
69 | 70 | ||
70 | /** Initialize the VFS node hash table. |
71 | /** Initialize the VFS node hash table. |
71 | * |
72 | * |
72 | * @return Return true on success, false on failure. |
73 | * @return Return true on success, false on failure. |
73 | */ |
74 | */ |
74 | bool vfs_nodes_init(void) |
75 | bool vfs_nodes_init(void) |
75 | { |
76 | { |
76 | return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops); |
77 | return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops); |
77 | } |
78 | } |
78 | 79 | ||
79 | static inline void _vfs_node_addref(vfs_node_t *node) |
80 | static inline void _vfs_node_addref(vfs_node_t *node) |
80 | { |
81 | { |
81 | node->refcnt++; |
82 | node->refcnt++; |
82 | } |
83 | } |
83 | 84 | ||
84 | /** Increment reference count of a VFS node. |
85 | /** Increment reference count of a VFS node. |
85 | * |
86 | * |
86 | * @param node VFS node that will have its refcnt incremented. |
87 | * @param node VFS node that will have its refcnt incremented. |
87 | */ |
88 | */ |
88 | void vfs_node_addref(vfs_node_t *node) |
89 | void vfs_node_addref(vfs_node_t *node) |
89 | { |
90 | { |
90 | futex_down(&nodes_futex); |
91 | futex_down(&nodes_futex); |
91 | _vfs_node_addref(node); |
92 | _vfs_node_addref(node); |
92 | futex_up(&nodes_futex); |
93 | futex_up(&nodes_futex); |
93 | } |
94 | } |
94 | 95 | ||
95 | /** Decrement reference count of a VFS node. |
96 | /** Decrement reference count of a VFS node. |
96 | * |
97 | * |
97 | * This function handles the case when the reference count drops to zero. |
98 | * This function handles the case when the reference count drops to zero. |
98 | * |
99 | * |
99 | * @param node VFS node that will have its refcnt decremented. |
100 | * @param node VFS node that will have its refcnt decremented. |
100 | */ |
101 | */ |
101 | void vfs_node_delref(vfs_node_t *node) |
102 | void vfs_node_delref(vfs_node_t *node) |
102 | { |
103 | { |
103 | futex_down(&nodes_futex); |
104 | futex_down(&nodes_futex); |
104 | if (node->refcnt-- == 1) { |
105 | if (node->refcnt-- == 1) { |
105 | unsigned long key[] = { |
106 | unsigned long key[] = { |
106 | [KEY_FS_HANDLE] = node->fs_handle, |
107 | [KEY_FS_HANDLE] = node->fs_handle, |
107 | [KEY_DEV_HANDLE] = node->dev_handle, |
108 | [KEY_DEV_HANDLE] = node->dev_handle, |
108 | [KEY_INDEX] = node->index |
109 | [KEY_INDEX] = node->index |
109 | }; |
110 | }; |
110 | hash_table_remove(&nodes, key, 3); |
111 | hash_table_remove(&nodes, key, 3); |
111 | } |
112 | } |
112 | futex_up(&nodes_futex); |
113 | futex_up(&nodes_futex); |
113 | } |
114 | } |
114 | 115 | ||
115 | /** Find VFS node. |
116 | /** Find VFS node. |
116 | * |
117 | * |
117 | * This function will try to lookup the given triplet in the VFS node hash |
118 | * This function will try to lookup the given triplet in the VFS node hash |
118 | * table. In case the triplet is not found there, a new VFS node is created. |
119 | * table. In case the triplet is not found there, a new VFS node is created. |
119 | * In any case, the VFS node will have its reference count incremented. Every |
120 | * In any case, the VFS node will have its reference count incremented. Every |
120 | * node returned by this call should be eventually put back by calling |
121 | * node returned by this call should be eventually put back by calling |
121 | * vfs_node_put() on it. |
122 | * vfs_node_put() on it. |
122 | * |
123 | * |
123 | * @param triplet Triplet encoding the identity of the VFS node. |
124 | * @param triplet Triplet encoding the identity of the VFS node. |
- | 125 | * @param size Size of the node as filled by vfs_lookup_internal(). |
|
124 | * |
126 | * |
125 | * @return VFS node corresponding to the given triplet. |
127 | * @return VFS node corresponding to the given triplet. |
126 | */ |
128 | */ |
127 | vfs_node_t *vfs_node_get(vfs_triplet_t *triplet) |
129 | vfs_node_t *vfs_node_get(vfs_triplet_t *triplet, size_t size) |
128 | { |
130 | { |
129 | unsigned long key[] = { |
131 | unsigned long key[] = { |
130 | [KEY_FS_HANDLE] = triplet->fs_handle, |
132 | [KEY_FS_HANDLE] = triplet->fs_handle, |
131 | [KEY_DEV_HANDLE] = triplet->dev_handle, |
133 | [KEY_DEV_HANDLE] = triplet->dev_handle, |
132 | [KEY_INDEX] = triplet->index |
134 | [KEY_INDEX] = triplet->index |
133 | }; |
135 | }; |
134 | link_t *tmp; |
136 | link_t *tmp; |
135 | vfs_node_t *node; |
137 | vfs_node_t *node; |
136 | 138 | ||
137 | futex_down(&nodes_futex); |
139 | futex_down(&nodes_futex); |
138 | tmp = hash_table_find(&nodes, key); |
140 | tmp = hash_table_find(&nodes, key); |
139 | if (!tmp) { |
141 | if (!tmp) { |
140 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
142 | node = (vfs_node_t *) malloc(sizeof(vfs_node_t)); |
141 | if (!node) { |
143 | if (!node) { |
142 | futex_up(&nodes_futex); |
144 | futex_up(&nodes_futex); |
143 | return NULL; |
145 | return NULL; |
144 | } |
146 | } |
145 | memset(node, 0, sizeof(vfs_node_t)); |
147 | memset(node, 0, sizeof(vfs_node_t)); |
146 | node->fs_handle = triplet->fs_handle; |
148 | node->fs_handle = triplet->fs_handle; |
147 | node->dev_handle = triplet->fs_handle; |
149 | node->dev_handle = triplet->fs_handle; |
148 | node->index = triplet->index; |
150 | node->index = triplet->index; |
- | 151 | node->size = size; |
|
149 | link_initialize(&node->nh_link); |
152 | link_initialize(&node->nh_link); |
150 | rwlock_initialize(&node->contents_rwlock); |
153 | rwlock_initialize(&node->contents_rwlock); |
151 | hash_table_insert(&nodes, key, &node->nh_link); |
154 | hash_table_insert(&nodes, key, &node->nh_link); |
152 | } else { |
155 | } else { |
153 | node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
156 | node = hash_table_get_instance(tmp, vfs_node_t, nh_link); |
154 | } |
157 | } |
- | 158 | ||
- | 159 | assert(node->size == size); |
|
- | 160 | ||
155 | _vfs_node_addref(node); |
161 | _vfs_node_addref(node); |
156 | futex_up(&nodes_futex); |
162 | futex_up(&nodes_futex); |
157 | 163 | ||
158 | return node; |
164 | return node; |
159 | } |
165 | } |
160 | 166 | ||
161 | /** Return VFS node when no longer needed by the caller. |
167 | /** Return VFS node when no longer needed by the caller. |
162 | * |
168 | * |
163 | * This function will remove the reference on the VFS node created by |
169 | * This function will remove the reference on the VFS node created by |
164 | * vfs_node_get(). This function can only be called as a closing bracket to the |
170 | * vfs_node_get(). This function can only be called as a closing bracket to the |
165 | * preceding vfs_node_get() call. |
171 | * preceding vfs_node_get() call. |
166 | * |
172 | * |
167 | * @param node VFS node being released. |
173 | * @param node VFS node being released. |
168 | */ |
174 | */ |
169 | void vfs_node_put(vfs_node_t *node) |
175 | void vfs_node_put(vfs_node_t *node) |
170 | { |
176 | { |
171 | vfs_node_delref(node); |
177 | vfs_node_delref(node); |
172 | } |
178 | } |
173 | 179 | ||
174 | hash_index_t nodes_hash(unsigned long key[]) |
180 | hash_index_t nodes_hash(unsigned long key[]) |
175 | { |
181 | { |
176 | hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4); |
182 | hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4); |
177 | hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2); |
183 | hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2); |
178 | 184 | ||
179 | return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1); |
185 | return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1); |
180 | } |
186 | } |
181 | 187 | ||
182 | int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
188 | int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
183 | { |
189 | { |
184 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
190 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
185 | return (node->fs_handle == key[KEY_FS_HANDLE]) && |
191 | return (node->fs_handle == key[KEY_FS_HANDLE]) && |
186 | (node->dev_handle == key[KEY_DEV_HANDLE]) && |
192 | (node->dev_handle == key[KEY_DEV_HANDLE]) && |
187 | (node->index == key[KEY_INDEX]); |
193 | (node->index == key[KEY_INDEX]); |
188 | } |
194 | } |
189 | 195 | ||
190 | void nodes_remove_callback(link_t *item) |
196 | void nodes_remove_callback(link_t *item) |
191 | { |
197 | { |
192 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
198 | vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link); |
193 | free(node); |
199 | free(node); |
194 | } |
200 | } |
195 | 201 | ||
196 | /** |
202 | /** |
197 | * @} |
203 | * @} |
198 | */ |
204 | */ |
199 | 205 |