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