Rev 2890 | Rev 2945 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2890 | Rev 2944 | ||
---|---|---|---|
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 fat_idx.c |
34 | * @file fat_idx.c |
35 | * @brief Layer for translating FAT entities to VFS node indices. |
35 | * @brief Layer for translating FAT entities to VFS node indices. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "../../vfs/vfs.h" |
39 | #include "../../vfs/vfs.h" |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <string.h> |
41 | #include <string.h> |
42 | #include <libadt/hash_table.h> |
42 | #include <libadt/hash_table.h> |
43 | #include <libadt/list.h> |
43 | #include <libadt/list.h> |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <futex.h> |
45 | #include <futex.h> |
46 | 46 | ||
47 | /** Each instance of this type describes one interval of freed VFS indices. */ |
47 | /** Each instance of this type describes one interval of freed VFS indices. */ |
48 | typedef struct { |
48 | typedef struct { |
49 | link_t link; |
49 | link_t link; |
50 | fs_index_t first; |
50 | fs_index_t first; |
51 | fs_index_t last; |
51 | fs_index_t last; |
52 | } freed_t; |
52 | } freed_t; |
53 | 53 | ||
54 | /** |
54 | /** |
55 | * Each instance of this type describes state of all VFS indices that |
55 | * Each instance of this type describes state of all VFS indices that |
56 | * are currently unused. |
56 | * are currently unused. |
57 | */ |
57 | */ |
58 | typedef struct { |
58 | typedef struct { |
59 | link_t link; |
59 | link_t link; |
60 | dev_handle_t dev_handle; |
60 | dev_handle_t dev_handle; |
61 | 61 | ||
62 | /** Next unassigned index. */ |
62 | /** Next unassigned index. */ |
63 | fs_index_t next; |
63 | fs_index_t next; |
64 | /** Number of remaining unassigned indices. */ |
64 | /** Number of remaining unassigned indices. */ |
65 | uint64_t remaining; |
65 | uint64_t remaining; |
66 | 66 | ||
67 | /** Sorted list of intervals of freed indices. */ |
67 | /** Sorted list of intervals of freed indices. */ |
68 | link_t freed_head; |
68 | link_t freed_head; |
69 | } unused_t; |
69 | } unused_t; |
70 | 70 | ||
- | 71 | /** Futex protecting the list of unused structures. */ |
|
- | 72 | static futex_t unused_futex = FUTEX_INITIALIZER; |
|
- | 73 | ||
- | 74 | /** List of unused structures. */ |
|
71 | static LIST_INITIALIZE(unused_head); |
75 | static LIST_INITIALIZE(unused_head); |
72 | 76 | ||
73 | /** |
77 | /** |
74 | * Global hash table of all used fat_idx_t structures. |
78 | * Global hash table of all used fat_idx_t structures. |
75 | * The index structures are hashed by the dev_handle, parent node's first |
79 | * The index structures are hashed by the dev_handle, parent node's first |
76 | * cluster and index within the parent directory. |
80 | * cluster and index within the parent directory. |
77 | */ |
81 | */ |
78 | static hash_table_t up_hash; |
82 | static hash_table_t up_hash; |
79 | 83 | ||
80 | #define UPH_BUCKETS_LOG 12 |
84 | #define UPH_BUCKETS_LOG 12 |
81 | #define UPH_BUCKETS (1 << UPH_BUCKETS_LOG) |
85 | #define UPH_BUCKETS (1 << UPH_BUCKETS_LOG) |
82 | 86 | ||
83 | #define UPH_DH_KEY 0 |
87 | #define UPH_DH_KEY 0 |
84 | #define UPH_PFC_KEY 1 |
88 | #define UPH_PFC_KEY 1 |
85 | #define UPH_PDI_KEY 2 |
89 | #define UPH_PDI_KEY 2 |
86 | 90 | ||
87 | static hash_index_t pos_hash(unsigned long key[]) |
91 | static hash_index_t pos_hash(unsigned long key[]) |
88 | { |
92 | { |
89 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
93 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
90 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
94 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
91 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
95 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
92 | 96 | ||
93 | hash_index_t h; |
97 | hash_index_t h; |
94 | 98 | ||
95 | /* |
99 | /* |
96 | * The least significant half of all bits are the least significant bits |
100 | * The least significant half of all bits are the least significant bits |
97 | * of the parent node's first cluster. |
101 | * of the parent node's first cluster. |
98 | * |
102 | * |
99 | * The least significant half of the most significant half of all bits |
103 | * The least significant half of the most significant half of all bits |
100 | * are the least significant bits of the node's dentry index within the |
104 | * are the least significant bits of the node's dentry index within the |
101 | * parent directory node. |
105 | * parent directory node. |
102 | * |
106 | * |
103 | * The most significant half of the most significant half of all bits |
107 | * The most significant half of the most significant half of all bits |
104 | * are the least significant bits of the device handle. |
108 | * are the least significant bits of the device handle. |
105 | */ |
109 | */ |
106 | h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1); |
110 | h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1); |
107 | h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
111 | h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
108 | (UPH_BUCKETS_LOG / 2); |
112 | (UPH_BUCKETS_LOG / 2); |
109 | h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
113 | h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
110 | (3 * (UPH_BUCKETS_LOG / 4)); |
114 | (3 * (UPH_BUCKETS_LOG / 4)); |
111 | 115 | ||
112 | return h; |
116 | return h; |
113 | } |
117 | } |
114 | 118 | ||
115 | static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) |
119 | static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) |
116 | { |
120 | { |
117 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
121 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
118 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
122 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
119 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
123 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
120 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); |
124 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); |
121 | 125 | ||
122 | return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && |
126 | return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && |
123 | (pdi == fidx->pdi); |
127 | (pdi == fidx->pdi); |
124 | } |
128 | } |
125 | 129 | ||
126 | static void pos_remove_callback(link_t *item) |
130 | static void pos_remove_callback(link_t *item) |
127 | { |
131 | { |
128 | /* nothing to do */ |
132 | /* nothing to do */ |
129 | } |
133 | } |
130 | 134 | ||
131 | static hash_table_operations_t uph_ops = { |
135 | static hash_table_operations_t uph_ops = { |
132 | .hash = pos_hash, |
136 | .hash = pos_hash, |
133 | .compare = pos_compare, |
137 | .compare = pos_compare, |
134 | .remove_callback = pos_remove_callback, |
138 | .remove_callback = pos_remove_callback, |
135 | }; |
139 | }; |
136 | 140 | ||
137 | /** |
141 | /** |
138 | * Global hash table of all used fat_idx_t structures. |
142 | * Global hash table of all used fat_idx_t structures. |
139 | * The index structures are hashed by the dev_handle and index. |
143 | * The index structures are hashed by the dev_handle and index. |
140 | */ |
144 | */ |
141 | static hash_table_t ui_hash; |
145 | static hash_table_t ui_hash; |
142 | 146 | ||
143 | #define UIH_BUCKETS_LOG 12 |
147 | #define UIH_BUCKETS_LOG 12 |
144 | #define UIH_BUCKETS (1 << UIH_BUCKETS_LOG) |
148 | #define UIH_BUCKETS (1 << UIH_BUCKETS_LOG) |
145 | 149 | ||
146 | #define UIH_DH_KEY 0 |
150 | #define UIH_DH_KEY 0 |
147 | #define UIH_INDEX_KEY 1 |
151 | #define UIH_INDEX_KEY 1 |
148 | 152 | ||
149 | static hash_index_t idx_hash(unsigned long key[]) |
153 | static hash_index_t idx_hash(unsigned long key[]) |
150 | { |
154 | { |
151 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
155 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
152 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
156 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
153 | 157 | ||
154 | hash_index_t h; |
158 | hash_index_t h; |
155 | 159 | ||
156 | h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); |
160 | h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); |
157 | h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << |
161 | h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << |
158 | (UIH_BUCKETS_LOG / 2); |
162 | (UIH_BUCKETS_LOG / 2); |
159 | 163 | ||
160 | return h; |
164 | return h; |
161 | } |
165 | } |
162 | 166 | ||
163 | static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) |
167 | static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) |
164 | { |
168 | { |
165 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
169 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
166 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
170 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
167 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); |
171 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); |
168 | 172 | ||
169 | return (dev_handle == fidx->dev_handle) && (index == fidx->index); |
173 | return (dev_handle == fidx->dev_handle) && (index == fidx->index); |
170 | } |
174 | } |
171 | 175 | ||
172 | static void idx_remove_callback(link_t *item) |
176 | static void idx_remove_callback(link_t *item) |
173 | { |
177 | { |
174 | /* nothing to do */ |
178 | /* nothing to do */ |
175 | } |
179 | } |
176 | 180 | ||
177 | static hash_table_operations_t uih_ops = { |
181 | static hash_table_operations_t uih_ops = { |
178 | .hash = idx_hash, |
182 | .hash = idx_hash, |
179 | .compare = idx_compare, |
183 | .compare = idx_compare, |
180 | .remove_callback = idx_remove_callback, |
184 | .remove_callback = idx_remove_callback, |
181 | }; |
185 | }; |
182 | 186 | ||
183 | /** Allocate a VFS index which is not currently in use. */ |
187 | /** Allocate a VFS index which is not currently in use. */ |
184 | static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index) |
188 | static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index) |
185 | { |
189 | { |
186 | link_t *l; |
190 | link_t *l; |
187 | unused_t *u; |
191 | unused_t *u; |
188 | 192 | ||
189 | assert(index); |
193 | assert(index); |
- | 194 | futex_down(&unused_futex); |
|
190 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
195 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
191 | u = list_get_instance(l, unused_t, link); |
196 | u = list_get_instance(l, unused_t, link); |
192 | if (u->dev_handle == dev_handle) |
197 | if (u->dev_handle == dev_handle) |
193 | goto hit; |
198 | goto hit; |
194 | } |
199 | } |
- | 200 | futex_up(&unused_futex); |
|
195 | 201 | ||
196 | /* dev_handle not found */ |
202 | /* dev_handle not found */ |
197 | return false; |
203 | return false; |
198 | 204 | ||
199 | hit: |
205 | hit: |
200 | if (list_empty(&u->freed_head)) { |
206 | if (list_empty(&u->freed_head)) { |
201 | if (u->remaining) { |
207 | if (u->remaining) { |
202 | /* |
208 | /* |
203 | * There are no freed indices, allocate one directly |
209 | * There are no freed indices, allocate one directly |
204 | * from the counter. |
210 | * from the counter. |
205 | */ |
211 | */ |
206 | *index = u->next++; |
212 | *index = u->next++; |
207 | --u->remaining; |
213 | --u->remaining; |
- | 214 | futex_up(&unused_futex); |
|
208 | return true; |
215 | return true; |
209 | } |
216 | } |
210 | } else { |
217 | } else { |
211 | /* There are some freed indices which we can reuse. */ |
218 | /* There are some freed indices which we can reuse. */ |
212 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
219 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
213 | link); |
220 | link); |
214 | *index = f->first; |
221 | *index = f->first; |
215 | if (f->first++ == f->last) { |
222 | if (f->first++ == f->last) { |
216 | /* Destroy the interval. */ |
223 | /* Destroy the interval. */ |
217 | list_remove(&f->link); |
224 | list_remove(&f->link); |
218 | free(f); |
225 | free(f); |
219 | } |
226 | } |
- | 227 | futex_up(&unused_futex); |
|
220 | return true; |
228 | return true; |
221 | } |
229 | } |
222 | /* |
230 | /* |
223 | * We ran out of indices, which is extremely unlikely with FAT16, but |
231 | * We ran out of indices, which is extremely unlikely with FAT16, but |
224 | * theoretically still possible (e.g. too many open unlinked nodes or |
232 | * theoretically still possible (e.g. too many open unlinked nodes or |
225 | * too many zero-sized nodes). |
233 | * too many zero-sized nodes). |
226 | */ |
234 | */ |
- | 235 | futex_up(&unused_futex); |
|
227 | return false; |
236 | return false; |
228 | } |
237 | } |
229 | 238 | ||
230 | /** If possible, coalesce two intervals of freed indices. */ |
239 | /** If possible, coalesce two intervals of freed indices. */ |
231 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
240 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
232 | { |
241 | { |
233 | freed_t *fl = list_get_instance(l, freed_t, link); |
242 | freed_t *fl = list_get_instance(l, freed_t, link); |
234 | freed_t *fr = list_get_instance(r, freed_t, link); |
243 | freed_t *fr = list_get_instance(r, freed_t, link); |
235 | 244 | ||
236 | if (fl->last + 1 == fr->first) { |
245 | if (fl->last + 1 == fr->first) { |
237 | if (cur == l) { |
246 | if (cur == l) { |
238 | fl->last = fr->last; |
247 | fl->last = fr->last; |
239 | list_remove(r); |
248 | list_remove(r); |
240 | free(r); |
249 | free(r); |
241 | } else { |
250 | } else { |
242 | fr->first = fl->first; |
251 | fr->first = fl->first; |
243 | list_remove(l); |
252 | list_remove(l); |
244 | free(l); |
253 | free(l); |
245 | } |
254 | } |
246 | } |
255 | } |
247 | } |
256 | } |
248 | 257 | ||
249 | /** Free a VFS index, which is no longer in use. */ |
258 | /** Free a VFS index, which is no longer in use. */ |
250 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
259 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
251 | { |
260 | { |
252 | link_t *l; |
261 | link_t *l; |
253 | unused_t *u; |
262 | unused_t *u; |
254 | 263 | ||
- | 264 | futex_down(&unused_futex); |
|
255 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
265 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
256 | u = list_get_instance(l, unused_t, link); |
266 | u = list_get_instance(l, unused_t, link); |
257 | if (u->dev_handle == dev_handle) |
267 | if (u->dev_handle == dev_handle) |
258 | goto hit; |
268 | goto hit; |
259 | } |
269 | } |
- | 270 | futex_up(&unused_futex); |
|
260 | 271 | ||
261 | /* should not happen */ |
272 | /* should not happen */ |
262 | assert(0); |
273 | assert(0); |
263 | 274 | ||
264 | hit: |
275 | hit: |
265 | if (u->next == index + 1) { |
276 | if (u->next == index + 1) { |
266 | /* The index can be returned directly to the counter. */ |
277 | /* The index can be returned directly to the counter. */ |
267 | u->next--; |
278 | u->next--; |
268 | u->remaining++; |
279 | u->remaining++; |
269 | return; |
- | |
270 | } else { |
280 | } else { |
271 | /* |
281 | /* |
272 | * The index must be returned either to an existing freed |
282 | * The index must be returned either to an existing freed |
273 | * interval or a new interval must be created. |
283 | * interval or a new interval must be created. |
274 | */ |
284 | */ |
275 | link_t *lnk; |
285 | link_t *lnk; |
276 | freed_t *n; |
286 | freed_t *n; |
277 | for (lnk = u->freed_head.next; lnk != &u->freed_head; |
287 | for (lnk = u->freed_head.next; lnk != &u->freed_head; |
278 | lnk = lnk->next) { |
288 | lnk = lnk->next) { |
279 | freed_t *f = list_get_instance(lnk, freed_t, link); |
289 | freed_t *f = list_get_instance(lnk, freed_t, link); |
280 | if (f->first == index + 1) { |
290 | if (f->first == index + 1) { |
281 | f->first--; |
291 | f->first--; |
282 | if (lnk->prev != &u->freed_head) |
292 | if (lnk->prev != &u->freed_head) |
283 | try_coalesce_intervals(lnk->prev, lnk, |
293 | try_coalesce_intervals(lnk->prev, lnk, |
284 | lnk); |
294 | lnk); |
- | 295 | futex_up(&unused_futex); |
|
285 | return; |
296 | return; |
286 | } |
297 | } |
287 | if (f->last == index - 1) { |
298 | if (f->last == index - 1) { |
288 | f->last++; |
299 | f->last++; |
289 | if (lnk->next != &u->freed_head) |
300 | if (lnk->next != &u->freed_head) |
290 | try_coalesce_intervals(lnk, lnk->next, |
301 | try_coalesce_intervals(lnk, lnk->next, |
291 | lnk); |
302 | lnk); |
- | 303 | futex_up(&unused_futex); |
|
292 | return; |
304 | return; |
293 | } |
305 | } |
294 | if (index > f->first) { |
306 | if (index > f->first) { |
295 | n = malloc(sizeof(freed_t)); |
307 | n = malloc(sizeof(freed_t)); |
296 | /* TODO: sleep until allocation succeeds */ |
308 | /* TODO: sleep until allocation succeeds */ |
297 | assert(n); |
309 | assert(n); |
298 | link_initialize(&n->link); |
310 | link_initialize(&n->link); |
299 | n->first = index; |
311 | n->first = index; |
300 | n->last = index; |
312 | n->last = index; |
301 | list_insert_before(&n->link, lnk); |
313 | list_insert_before(&n->link, lnk); |
- | 314 | futex_up(&unused_futex); |
|
302 | return; |
315 | return; |
303 | } |
316 | } |
304 | 317 | ||
305 | } |
318 | } |
306 | /* The index will form the last interval. */ |
319 | /* The index will form the last interval. */ |
307 | n = malloc(sizeof(freed_t)); |
320 | n = malloc(sizeof(freed_t)); |
308 | /* TODO: sleep until allocation succeeds */ |
321 | /* TODO: sleep until allocation succeeds */ |
309 | assert(n); |
322 | assert(n); |
310 | link_initialize(&n->link); |
323 | link_initialize(&n->link); |
311 | n->first = index; |
324 | n->first = index; |
312 | n->last = index; |
325 | n->last = index; |
313 | list_append(&n->link, &u->freed_head); |
326 | list_append(&n->link, &u->freed_head); |
314 | } |
327 | } |
- | 328 | futex_up(&unused_futex); |
|
315 | } |
329 | } |
316 | 330 | ||
317 | fat_idx_t * |
331 | fat_idx_t * |
318 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
332 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
319 | { |
333 | { |
320 | fat_idx_t *fidx; |
334 | fat_idx_t *fidx; |
321 | link_t *l; |
335 | link_t *l; |
322 | unsigned long pkey[] = { |
336 | unsigned long pkey[] = { |
323 | [UPH_DH_KEY] = dev_handle, |
337 | [UPH_DH_KEY] = dev_handle, |
324 | [UPH_PFC_KEY] = pfc, |
338 | [UPH_PFC_KEY] = pfc, |
325 | [UPH_PDI_KEY] = pdi, |
339 | [UPH_PDI_KEY] = pdi, |
326 | }; |
340 | }; |
327 | 341 | ||
328 | l = hash_table_find(&up_hash, pkey); |
342 | l = hash_table_find(&up_hash, pkey); |
329 | if (l) { |
343 | if (l) { |
330 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
344 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
331 | } else { |
345 | } else { |
332 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
346 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
333 | if (!fidx) { |
347 | if (!fidx) { |
334 | return NULL; |
348 | return NULL; |
335 | } |
349 | } |
336 | if (!fat_idx_alloc(dev_handle, &fidx->index)) { |
350 | if (!fat_idx_alloc(dev_handle, &fidx->index)) { |
337 | free(fidx); |
351 | free(fidx); |
338 | return NULL; |
352 | return NULL; |
339 | } |
353 | } |
340 | 354 | ||
341 | unsigned long ikey[] = { |
355 | unsigned long ikey[] = { |
342 | [UIH_DH_KEY] = dev_handle, |
356 | [UIH_DH_KEY] = dev_handle, |
343 | [UIH_INDEX_KEY] = fidx->index, |
357 | [UIH_INDEX_KEY] = fidx->index, |
344 | }; |
358 | }; |
345 | 359 | ||
346 | link_initialize(&fidx->uph_link); |
360 | link_initialize(&fidx->uph_link); |
347 | link_initialize(&fidx->uih_link); |
361 | link_initialize(&fidx->uih_link); |
348 | fidx->dev_handle = dev_handle; |
362 | fidx->dev_handle = dev_handle; |
349 | fidx->pfc = pfc; |
363 | fidx->pfc = pfc; |
350 | fidx->pdi = pdi; |
364 | fidx->pdi = pdi; |
351 | fidx->nodep = NULL; |
365 | fidx->nodep = NULL; |
352 | 366 | ||
353 | hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
367 | hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
354 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
368 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
355 | } |
369 | } |
356 | 370 | ||
357 | return fidx; |
371 | return fidx; |
358 | } |
372 | } |
359 | 373 | ||
360 | fat_idx_t * |
374 | fat_idx_t * |
361 | fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index) |
375 | fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index) |
362 | { |
376 | { |
363 | fat_idx_t *fidx = NULL; |
377 | fat_idx_t *fidx = NULL; |
364 | link_t *l; |
378 | link_t *l; |
365 | unsigned long ikey[] = { |
379 | unsigned long ikey[] = { |
366 | [UIH_DH_KEY] = dev_handle, |
380 | [UIH_DH_KEY] = dev_handle, |
367 | [UIH_INDEX_KEY] = index, |
381 | [UIH_INDEX_KEY] = index, |
368 | }; |
382 | }; |
369 | 383 | ||
370 | l = hash_table_find(&ui_hash, ikey); |
384 | l = hash_table_find(&ui_hash, ikey); |
371 | if (l) { |
385 | if (l) { |
372 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
386 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
373 | } |
387 | } |
374 | 388 | ||
375 | return fidx; |
389 | return fidx; |
376 | } |
390 | } |
377 | 391 | ||
378 | 392 |