source: mainline/uspace/srv/fs/fat/fat_idx.c@ 062d900

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 062d900 was 062d900, checked in by Jakub Jermar <jakub@…>, 13 years ago

Cherrypick userspace hash table changes from lp:~adam-hraska+lp/helenos/rcu/.

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