Rev 2927 | Rev 3009 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2927 | Rev 2948 | ||
---|---|---|---|
Line 66... | Line 66... | ||
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 | ||
- | 77 | /** Futex protecting the up_hash and ui_hash. |
|
- | 78 | * |
|
- | 79 | * The locking strategy assumes that there will be at most one fibril for each |
|
- | 80 | * dev_handle. Therefore it will be sufficient to hold the futex for shorter |
|
- | 81 | * times (i.e. only during hash table operations as opposed to holding it the |
|
- | 82 | * whole time between an unsuccessful find and the following insert). Should the |
|
- | 83 | * assumption break, the locking strategy for this futex will have to be |
|
- | 84 | * reconsidered. |
|
- | 85 | */ |
|
- | 86 | static futex_t used_futex = FUTEX_INITIALIZER; |
|
- | 87 | ||
73 | /** |
88 | /** |
74 | * Global hash table of all used fat_idx_t structures. |
89 | * Global hash table of all used fat_idx_t structures. |
75 | * The index structures are hashed by the dev_handle, parent node's first |
90 | * The index structures are hashed by the dev_handle, parent node's first |
76 | * cluster and index within the parent directory. |
91 | * cluster and index within the parent directory. |
77 | */ |
92 | */ |
Line 185... | Line 200... | ||
185 | { |
200 | { |
186 | link_t *l; |
201 | link_t *l; |
187 | unused_t *u; |
202 | unused_t *u; |
188 | 203 | ||
189 | assert(index); |
204 | assert(index); |
- | 205 | futex_down(&unused_futex); |
|
190 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
206 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
191 | u = list_get_instance(l, unused_t, link); |
207 | u = list_get_instance(l, unused_t, link); |
192 | if (u->dev_handle == dev_handle) |
208 | if (u->dev_handle == dev_handle) |
193 | goto hit; |
209 | goto hit; |
194 | } |
210 | } |
- | 211 | futex_up(&unused_futex); |
|
195 | 212 | ||
196 | /* dev_handle not found */ |
213 | /* dev_handle not found */ |
197 | return false; |
214 | return false; |
198 | 215 | ||
199 | hit: |
216 | hit: |
200 | if (list_empty(&u->freed_head)) { |
217 | if (list_empty(&u->freed_head)) { |
Line 203... | Line 220... | ||
203 | * There are no freed indices, allocate one directly |
220 | * There are no freed indices, allocate one directly |
204 | * from the counter. |
221 | * from the counter. |
205 | */ |
222 | */ |
206 | *index = u->next++; |
223 | *index = u->next++; |
207 | --u->remaining; |
224 | --u->remaining; |
- | 225 | futex_up(&unused_futex); |
|
208 | return true; |
226 | return true; |
209 | } |
227 | } |
210 | } else { |
228 | } else { |
211 | /* There are some freed indices which we can reuse. */ |
229 | /* There are some freed indices which we can reuse. */ |
212 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
230 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
Line 215... | Line 233... | ||
215 | if (f->first++ == f->last) { |
233 | if (f->first++ == f->last) { |
216 | /* Destroy the interval. */ |
234 | /* Destroy the interval. */ |
217 | list_remove(&f->link); |
235 | list_remove(&f->link); |
218 | free(f); |
236 | free(f); |
219 | } |
237 | } |
- | 238 | futex_up(&unused_futex); |
|
220 | return true; |
239 | return true; |
221 | } |
240 | } |
222 | /* |
241 | /* |
223 | * We ran out of indices, which is extremely unlikely with FAT16, but |
242 | * We ran out of indices, which is extremely unlikely with FAT16, but |
224 | * theoretically still possible (e.g. too many open unlinked nodes or |
243 | * theoretically still possible (e.g. too many open unlinked nodes or |
225 | * too many zero-sized nodes). |
244 | * too many zero-sized nodes). |
226 | */ |
245 | */ |
- | 246 | futex_up(&unused_futex); |
|
227 | return false; |
247 | return false; |
228 | } |
248 | } |
229 | 249 | ||
230 | /** If possible, coalesce two intervals of freed indices. */ |
250 | /** If possible, coalesce two intervals of freed indices. */ |
231 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
251 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
Line 250... | Line 270... | ||
250 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
270 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
251 | { |
271 | { |
252 | link_t *l; |
272 | link_t *l; |
253 | unused_t *u; |
273 | unused_t *u; |
254 | 274 | ||
- | 275 | futex_down(&unused_futex); |
|
255 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
276 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
256 | u = list_get_instance(l, unused_t, link); |
277 | u = list_get_instance(l, unused_t, link); |
257 | if (u->dev_handle == dev_handle) |
278 | if (u->dev_handle == dev_handle) |
258 | goto hit; |
279 | goto hit; |
259 | } |
280 | } |
- | 281 | futex_up(&unused_futex); |
|
260 | 282 | ||
261 | /* should not happen */ |
283 | /* should not happen */ |
262 | assert(0); |
284 | assert(0); |
263 | 285 | ||
264 | hit: |
286 | hit: |
265 | if (u->next == index + 1) { |
287 | if (u->next == index + 1) { |
266 | /* The index can be returned directly to the counter. */ |
288 | /* The index can be returned directly to the counter. */ |
267 | u->next--; |
289 | u->next--; |
268 | u->remaining++; |
290 | u->remaining++; |
269 | return; |
- | |
270 | } else { |
291 | } else { |
271 | /* |
292 | /* |
272 | * The index must be returned either to an existing freed |
293 | * The index must be returned either to an existing freed |
273 | * interval or a new interval must be created. |
294 | * interval or a new interval must be created. |
274 | */ |
295 | */ |
Line 280... | Line 301... | ||
280 | if (f->first == index + 1) { |
301 | if (f->first == index + 1) { |
281 | f->first--; |
302 | f->first--; |
282 | if (lnk->prev != &u->freed_head) |
303 | if (lnk->prev != &u->freed_head) |
283 | try_coalesce_intervals(lnk->prev, lnk, |
304 | try_coalesce_intervals(lnk->prev, lnk, |
284 | lnk); |
305 | lnk); |
- | 306 | futex_up(&unused_futex); |
|
285 | return; |
307 | return; |
286 | } |
308 | } |
287 | if (f->last == index - 1) { |
309 | if (f->last == index - 1) { |
288 | f->last++; |
310 | f->last++; |
289 | if (lnk->next != &u->freed_head) |
311 | if (lnk->next != &u->freed_head) |
290 | try_coalesce_intervals(lnk, lnk->next, |
312 | try_coalesce_intervals(lnk, lnk->next, |
291 | lnk); |
313 | lnk); |
- | 314 | futex_up(&unused_futex); |
|
292 | return; |
315 | return; |
293 | } |
316 | } |
294 | if (index > f->first) { |
317 | if (index > f->first) { |
295 | n = malloc(sizeof(freed_t)); |
318 | n = malloc(sizeof(freed_t)); |
296 | /* TODO: sleep until allocation succeeds */ |
319 | /* TODO: sleep until allocation succeeds */ |
297 | assert(n); |
320 | assert(n); |
298 | link_initialize(&n->link); |
321 | link_initialize(&n->link); |
299 | n->first = index; |
322 | n->first = index; |
300 | n->last = index; |
323 | n->last = index; |
301 | list_insert_before(&n->link, lnk); |
324 | list_insert_before(&n->link, lnk); |
- | 325 | futex_up(&unused_futex); |
|
302 | return; |
326 | return; |
303 | } |
327 | } |
304 | 328 | ||
305 | } |
329 | } |
306 | /* The index will form the last interval. */ |
330 | /* The index will form the last interval. */ |
Line 310... | Line 334... | ||
310 | link_initialize(&n->link); |
334 | link_initialize(&n->link); |
311 | n->first = index; |
335 | n->first = index; |
312 | n->last = index; |
336 | n->last = index; |
313 | list_append(&n->link, &u->freed_head); |
337 | list_append(&n->link, &u->freed_head); |
314 | } |
338 | } |
- | 339 | futex_up(&unused_futex); |
|
315 | } |
340 | } |
316 | 341 | ||
317 | fat_idx_t * |
342 | fat_idx_t * |
318 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
343 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
319 | { |
344 | { |
Line 323... | Line 348... | ||
323 | [UPH_DH_KEY] = dev_handle, |
348 | [UPH_DH_KEY] = dev_handle, |
324 | [UPH_PFC_KEY] = pfc, |
349 | [UPH_PFC_KEY] = pfc, |
325 | [UPH_PDI_KEY] = pdi, |
350 | [UPH_PDI_KEY] = pdi, |
326 | }; |
351 | }; |
327 | 352 | ||
- | 353 | futex_down(&used_futex); |
|
328 | l = hash_table_find(&up_hash, pkey); |
354 | l = hash_table_find(&up_hash, pkey); |
- | 355 | futex_up(&used_futex); |
|
329 | if (l) { |
356 | if (l) { |
330 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
357 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
331 | } else { |
358 | } else { |
332 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
359 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
333 | if (!fidx) { |
360 | if (!fidx) { |
Line 348... | Line 375... | ||
348 | fidx->dev_handle = dev_handle; |
375 | fidx->dev_handle = dev_handle; |
349 | fidx->pfc = pfc; |
376 | fidx->pfc = pfc; |
350 | fidx->pdi = pdi; |
377 | fidx->pdi = pdi; |
351 | fidx->nodep = NULL; |
378 | fidx->nodep = NULL; |
352 | 379 | ||
- | 380 | futex_down(&used_futex); |
|
353 | hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
381 | hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
354 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
382 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
- | 383 | futex_up(&used_futex); |
|
355 | } |
384 | } |
356 | 385 | ||
357 | return fidx; |
386 | return fidx; |
358 | } |
387 | } |
359 | 388 | ||
Line 365... | Line 394... | ||
365 | unsigned long ikey[] = { |
394 | unsigned long ikey[] = { |
366 | [UIH_DH_KEY] = dev_handle, |
395 | [UIH_DH_KEY] = dev_handle, |
367 | [UIH_INDEX_KEY] = index, |
396 | [UIH_INDEX_KEY] = index, |
368 | }; |
397 | }; |
369 | 398 | ||
- | 399 | futex_down(&used_futex); |
|
370 | l = hash_table_find(&ui_hash, ikey); |
400 | l = hash_table_find(&ui_hash, ikey); |
- | 401 | futex_up(&used_futex); |
|
371 | if (l) { |
402 | if (l) { |
372 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
403 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
373 | } |
404 | } |
374 | 405 | ||
375 | return fidx; |
406 | return fidx; |