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