source: mainline/uspace/srv/fs/exfat/exfat_idx.c@ 0ca7286

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ca7286 was 0ca7286, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub 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 exfat_idx.c
35 * @brief Layer for translating exFAT entities to VFS node indices.
36 */
37
38#include "exfat.h"
39#include "../../vfs/vfs.h"
40#include <errno.h>
41#include <str.h>
42#include <adt/hash_table.h>
43#include <adt/list.h>
44#include <assert.h>
45#include <fibril_synch.h>
46#include <malloc.h>
47
48/** Each instance of this type describes one interval of freed VFS indices. */
49typedef struct {
50 link_t link;
51 fs_index_t first;
52 fs_index_t last;
53} freed_t;
54
55/**
56 * Each instance of this type describes state of all VFS indices that
57 * are currently unused.
58 */
59typedef struct {
60 link_t link;
61 service_id_t service_id;
62
63 /** Next unassigned index. */
64 fs_index_t next;
65 /** Number of remaining unassigned indices. */
66 uint64_t remaining;
67
68 /** Sorted list of intervals of freed indices. */
69 list_t freed_list;
70} unused_t;
71
72/** Mutex protecting the list of unused structures. */
73static FIBRIL_MUTEX_INITIALIZE(unused_lock);
74
75/** List of unused structures. */
76static LIST_INITIALIZE(unused_list);
77
78static void unused_initialize(unused_t *u, service_id_t service_id)
79{
80 link_initialize(&u->link);
81 u->service_id = service_id;
82 u->next = 0;
83 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
84 list_initialize(&u->freed_list);
85}
86
87static unused_t *unused_find(service_id_t service_id, bool lock)
88{
89 unused_t *u;
90
91 if (lock)
92 fibril_mutex_lock(&unused_lock);
93 list_foreach(unused_list, l) {
94 u = list_get_instance(l, unused_t, link);
95 if (u->service_id == service_id)
96 return u;
97 }
98
99 if (lock)
100 fibril_mutex_unlock(&unused_lock);
101 return NULL;
102}
103
104/** Mutex protecting the up_hash and ui_hash. */
105static FIBRIL_MUTEX_INITIALIZE(used_lock);
106
107/**
108 * Global hash table of all used exfat_idx_t structures.
109 * The index structures are hashed by the service_id, parent node's first
110 * cluster and index within the parent directory.
111 */
112static hash_table_t up_hash;
113
114#define UPH_SID_KEY 0
115#define UPH_PFC_KEY 1
116#define UPH_PDI_KEY 2
117
118static size_t pos_key_hash(unsigned long key[])
119{
120 /* Inspired by Effective Java, 2nd edition. */
121 size_t hash = 17;
122
123 hash = 31 * hash + key[UPH_PFC_KEY];
124 hash = 31 * hash + key[UPH_PDI_KEY];
125 hash = 31 * hash + key[UPH_SID_KEY];
126
127 return hash;
128}
129
130static size_t pos_hash(const link_t *item)
131{
132 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uph_link);
133
134 unsigned long pkey[] = {
135 [UPH_SID_KEY] = fidx->service_id,
136 [UPH_PFC_KEY] = fidx->pfc,
137 [UPH_PDI_KEY] = fidx->pdi,
138 };
139
140 return pos_key_hash(pkey);
141}
142
143static bool pos_match(unsigned long key[], size_t keys, const link_t *item)
144{
145 service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
146 exfat_cluster_t pfc;
147 unsigned pdi;
148 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uph_link);
149
150 switch (keys) {
151 case 1:
152 return (service_id == fidx->service_id);
153 case 3:
154 pfc = (exfat_cluster_t) key[UPH_PFC_KEY];
155 pdi = (unsigned) key[UPH_PDI_KEY];
156 return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
157 (pdi == fidx->pdi);
158 default:
159 assert((keys == 1) || (keys == 3));
160 }
161
162 return 0;
163}
164
165static hash_table_ops_t uph_ops = {
166 .hash = pos_hash,
167 .key_hash = pos_key_hash,
168 .match = pos_match,
169 .equal = 0,
170 .remove_callback = 0,
171};
172
173/**
174 * Global hash table of all used fat_idx_t structures.
175 * The index structures are hashed by the service_id and index.
176 */
177static hash_table_t ui_hash;
178
179#define UIH_SID_KEY 0
180#define UIH_INDEX_KEY 1
181
182static size_t idx_key_hash(unsigned long key[])
183{
184 service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
185 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
186
187 /*
188 * Compute a simple hash unlimited by specific table size as per:
189 * Effective Java, 2nd edition.
190 */
191 size_t hash = 17;
192 hash = 31 * hash + (size_t)service_id;
193 hash = 31 * hash + (size_t)index;
194 return hash;
195}
196
197static size_t idx_hash(const link_t *item)
198{
199 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
200
201 unsigned long ikey[] = {
202 [UIH_SID_KEY] = fidx->service_id,
203 [UIH_INDEX_KEY] = fidx->index,
204 };
205
206 return idx_key_hash(ikey);
207}
208
209static bool idx_match(unsigned long key[], size_t keys, const link_t *item)
210{
211 service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
212 fs_index_t index;
213 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
214
215 switch (keys) {
216 case 1:
217 return (service_id == fidx->service_id);
218 case 2:
219 index = (fs_index_t) key[UIH_INDEX_KEY];
220 return (service_id == fidx->service_id) &&
221 (index == fidx->index);
222 default:
223 assert((keys == 1) || (keys == 2));
224 }
225
226 return 0;
227}
228
229static void idx_remove_callback(link_t *item)
230{
231 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
232
233 free(fidx);
234}
235
236static hash_table_ops_t uih_ops = {
237 .hash = idx_hash,
238 .key_hash = idx_key_hash,
239 .match = idx_match,
240 .equal = 0,
241 .remove_callback = idx_remove_callback,
242};
243
244/** Allocate a VFS index which is not currently in use. */
245static bool exfat_index_alloc(service_id_t service_id, fs_index_t *index)
246{
247 unused_t *u;
248
249 assert(index);
250 u = unused_find(service_id, true);
251 if (!u)
252 return false;
253
254 if (list_empty(&u->freed_list)) {
255 if (u->remaining) {
256 /*
257 * There are no freed indices, allocate one directly
258 * from the counter.
259 */
260 *index = u->next++;
261 --u->remaining;
262 fibril_mutex_unlock(&unused_lock);
263 return true;
264 }
265 } else {
266 /* There are some freed indices which we can reuse. */
267 freed_t *f = list_get_instance(list_first(&u->freed_list),
268 freed_t, link);
269 *index = f->first;
270 if (f->first++ == f->last) {
271 /* Destroy the interval. */
272 list_remove(&f->link);
273 free(f);
274 }
275 fibril_mutex_unlock(&unused_lock);
276 return true;
277 }
278 /*
279 * We ran out of indices, which is extremely unlikely with FAT16, but
280 * theoretically still possible (e.g. too many open unlinked nodes or
281 * too many zero-sized nodes).
282 */
283 fibril_mutex_unlock(&unused_lock);
284 return false;
285}
286
287/** If possible, coalesce two intervals of freed indices. */
288static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
289{
290 freed_t *fl = list_get_instance(l, freed_t, link);
291 freed_t *fr = list_get_instance(r, freed_t, link);
292
293 if (fl->last + 1 == fr->first) {
294 if (cur == l) {
295 fl->last = fr->last;
296 list_remove(r);
297 free(r);
298 } else {
299 fr->first = fl->first;
300 list_remove(l);
301 free(l);
302 }
303 }
304}
305
306/** Free a VFS index, which is no longer in use. */
307static void exfat_index_free(service_id_t service_id, fs_index_t index)
308{
309 unused_t *u;
310
311 u = unused_find(service_id, true);
312 assert(u);
313
314 if (u->next == index + 1) {
315 /* The index can be returned directly to the counter. */
316 u->next--;
317 u->remaining++;
318 } else {
319 /*
320 * The index must be returned either to an existing freed
321 * interval or a new interval must be created.
322 */
323 link_t *lnk;
324 freed_t *n;
325 for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
326 lnk = lnk->next) {
327 freed_t *f = list_get_instance(lnk, freed_t, link);
328 if (f->first == index + 1) {
329 f->first--;
330 if (lnk->prev != &u->freed_list.head)
331 try_coalesce_intervals(lnk->prev, lnk,
332 lnk);
333 fibril_mutex_unlock(&unused_lock);
334 return;
335 }
336 if (f->last == index - 1) {
337 f->last++;
338 if (lnk->next != &u->freed_list.head)
339 try_coalesce_intervals(lnk, lnk->next,
340 lnk);
341 fibril_mutex_unlock(&unused_lock);
342 return;
343 }
344 if (index > f->first) {
345 n = malloc(sizeof(freed_t));
346 /* TODO: sleep until allocation succeeds */
347 assert(n);
348 link_initialize(&n->link);
349 n->first = index;
350 n->last = index;
351 list_insert_before(&n->link, lnk);
352 fibril_mutex_unlock(&unused_lock);
353 return;
354 }
355
356 }
357 /* The index will form the last interval. */
358 n = malloc(sizeof(freed_t));
359 /* TODO: sleep until allocation succeeds */
360 assert(n);
361 link_initialize(&n->link);
362 n->first = index;
363 n->last = index;
364 list_append(&n->link, &u->freed_list);
365 }
366 fibril_mutex_unlock(&unused_lock);
367}
368
369static int exfat_idx_create(exfat_idx_t **fidxp, service_id_t service_id)
370{
371 exfat_idx_t *fidx;
372
373 fidx = (exfat_idx_t *) malloc(sizeof(exfat_idx_t));
374 if (!fidx)
375 return ENOMEM;
376 if (!exfat_index_alloc(service_id, &fidx->index)) {
377 free(fidx);
378 return ENOSPC;
379 }
380
381 link_initialize(&fidx->uph_link);
382 link_initialize(&fidx->uih_link);
383 fibril_mutex_initialize(&fidx->lock);
384 fidx->service_id = service_id;
385 fidx->pfc = 0; /* no parent yet */
386 fidx->pdi = 0;
387 fidx->nodep = NULL;
388
389 *fidxp = fidx;
390 return EOK;
391}
392
393int exfat_idx_get_new(exfat_idx_t **fidxp, service_id_t service_id)
394{
395 exfat_idx_t *fidx;
396 int rc;
397
398 fibril_mutex_lock(&used_lock);
399 rc = exfat_idx_create(&fidx, service_id);
400 if (rc != EOK) {
401 fibril_mutex_unlock(&used_lock);
402 return rc;
403 }
404
405 hash_table_insert(&ui_hash, &fidx->uih_link);
406 fibril_mutex_lock(&fidx->lock);
407 fibril_mutex_unlock(&used_lock);
408
409 *fidxp = fidx;
410 return EOK;
411}
412
413exfat_idx_t *
414exfat_idx_get_by_pos(service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
415{
416 exfat_idx_t *fidx;
417 link_t *l;
418 unsigned long pkey[] = {
419 [UPH_SID_KEY] = service_id,
420 [UPH_PFC_KEY] = pfc,
421 [UPH_PDI_KEY] = pdi,
422 };
423
424 fibril_mutex_lock(&used_lock);
425 l = hash_table_find(&up_hash, pkey);
426 if (l) {
427 fidx = hash_table_get_instance(l, exfat_idx_t, uph_link);
428 } else {
429 int rc;
430
431 rc = exfat_idx_create(&fidx, service_id);
432 if (rc != EOK) {
433 fibril_mutex_unlock(&used_lock);
434 return NULL;
435 }
436
437 fidx->pfc = pfc;
438 fidx->pdi = pdi;
439
440 hash_table_insert(&up_hash, &fidx->uph_link);
441 hash_table_insert(&ui_hash, &fidx->uih_link);
442 }
443 fibril_mutex_lock(&fidx->lock);
444 fibril_mutex_unlock(&used_lock);
445
446 return fidx;
447}
448
449void exfat_idx_hashin(exfat_idx_t *idx)
450{
451 fibril_mutex_lock(&used_lock);
452 hash_table_insert(&up_hash, &idx->uph_link);
453 fibril_mutex_unlock(&used_lock);
454}
455
456void exfat_idx_hashout(exfat_idx_t *idx)
457{
458 unsigned long pkey[] = {
459 [UPH_SID_KEY] = idx->service_id,
460 [UPH_PFC_KEY] = idx->pfc,
461 [UPH_PDI_KEY] = idx->pdi,
462 };
463
464 fibril_mutex_lock(&used_lock);
465 hash_table_remove(&up_hash, pkey, 3);
466 fibril_mutex_unlock(&used_lock);
467}
468
469exfat_idx_t *
470exfat_idx_get_by_index(service_id_t service_id, fs_index_t index)
471{
472 exfat_idx_t *fidx = NULL;
473 link_t *l;
474 unsigned long ikey[] = {
475 [UIH_SID_KEY] = service_id,
476 [UIH_INDEX_KEY] = index,
477 };
478
479 fibril_mutex_lock(&used_lock);
480 l = hash_table_find(&ui_hash, ikey);
481 if (l) {
482 fidx = hash_table_get_instance(l, exfat_idx_t, uih_link);
483 fibril_mutex_lock(&fidx->lock);
484 }
485 fibril_mutex_unlock(&used_lock);
486
487 return fidx;
488}
489
490/** Destroy the index structure.
491 *
492 * @param idx The index structure to be destroyed.
493 */
494void exfat_idx_destroy(exfat_idx_t *idx)
495{
496 unsigned long ikey[] = {
497 [UIH_SID_KEY] = idx->service_id,
498 [UIH_INDEX_KEY] = idx->index,
499 };
500 service_id_t service_id = idx->service_id;
501 fs_index_t index = idx->index;
502
503 /* TODO: assert(idx->pfc == FAT_CLST_RES0); */
504 assert(idx->pfc == 0);
505
506 fibril_mutex_lock(&used_lock);
507 /*
508 * Since we can only free unlinked nodes, the index structure is not
509 * present in the position hash (uph). We therefore hash it out from
510 * the index hash only.
511 */
512 hash_table_remove(&ui_hash, ikey, 2);
513 fibril_mutex_unlock(&used_lock);
514 /* Release the VFS index. */
515 exfat_index_free(service_id, index);
516 /* The index structure itself is freed in idx_remove_callback(). */
517}
518
519int exfat_idx_init(void)
520{
521 if (!hash_table_create(&up_hash, 0, 3, &uph_ops))
522 return ENOMEM;
523 if (!hash_table_create(&ui_hash, 0, 2, &uih_ops)) {
524 hash_table_destroy(&up_hash);
525 return ENOMEM;
526 }
527 return EOK;
528}
529
530void exfat_idx_fini(void)
531{
532 /* We assume the hash tables are empty. */
533 hash_table_destroy(&up_hash);
534 hash_table_destroy(&ui_hash);
535}
536
537int exfat_idx_init_by_service_id(service_id_t service_id)
538{
539 unused_t *u;
540 int rc = EOK;
541
542 u = (unused_t *) malloc(sizeof(unused_t));
543 if (!u)
544 return ENOMEM;
545 unused_initialize(u, service_id);
546 fibril_mutex_lock(&unused_lock);
547 if (!unused_find(service_id, false)) {
548 list_append(&u->link, &unused_list);
549 } else {
550 free(u);
551 rc = EEXIST;
552 }
553 fibril_mutex_unlock(&unused_lock);
554 return rc;
555}
556
557void exfat_idx_fini_by_service_id(service_id_t service_id)
558{
559 unsigned long ikey[] = {
560 [UIH_SID_KEY] = service_id
561 };
562 unsigned long pkey[] = {
563 [UPH_SID_KEY] = service_id
564 };
565
566 /*
567 * Remove this instance's index structure from up_hash and ui_hash.
568 * Process up_hash first and ui_hash second because the index structure
569 * is actually removed in idx_remove_callback().
570 */
571 fibril_mutex_lock(&used_lock);
572 hash_table_remove(&up_hash, pkey, 1);
573 hash_table_remove(&ui_hash, ikey, 1);
574 fibril_mutex_unlock(&used_lock);
575
576 /*
577 * Free the unused and freed structures for this instance.
578 */
579 unused_t *u = unused_find(service_id, true);
580 assert(u);
581 list_remove(&u->link);
582 fibril_mutex_unlock(&unused_lock);
583
584 while (!list_empty(&u->freed_list)) {
585 freed_t *f;
586 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
587 list_remove(&f->link);
588 free(f);
589 }
590 free(u);
591}
592
593/**
594 * @}
595 */
Note: See TracBrowser for help on using the repository browser.