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

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

Fix compiler warnings.

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