source: mainline/uspace/srv/fs/fat/fat_idx.c@ 17fa0280

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17fa0280 was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

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