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

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

Rename dev_handle_t to devmap_handle_t and make it explicitly clear that
dev_handle_t is a handle understood by devmap.

  • Property mode set to 100644
File size: 14.7 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/list.h>
44#include <assert.h>
45#include <fibril_synch.h>
46
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 devmap_handle_t devmap_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
71/** Mutex protecting the list of unused structures. */
72static FIBRIL_MUTEX_INITIALIZE(unused_lock);
73
74/** List of unused structures. */
75static LIST_INITIALIZE(unused_head);
76
77static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle)
78{
79 link_initialize(&u->link);
80 u->devmap_handle = devmap_handle;
81 u->next = 0;
82 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
83 list_initialize(&u->freed_head);
84}
85
86static unused_t *unused_find(devmap_handle_t devmap_handle, bool lock)
87{
88 unused_t *u;
89 link_t *l;
90
91 if (lock)
92 fibril_mutex_lock(&unused_lock);
93 for (l = unused_head.next; l != &unused_head; l = l->next) {
94 u = list_get_instance(l, unused_t, link);
95 if (u->devmap_handle == devmap_handle)
96 return u;
97 }
98 if (lock)
99 fibril_mutex_unlock(&unused_lock);
100 return NULL;
101}
102
103/** Mutex protecting the up_hash and ui_hash. */
104static FIBRIL_MUTEX_INITIALIZE(used_lock);
105
106/**
107 * Global hash table of all used fat_idx_t structures.
108 * The index structures are hashed by the devmap_handle, parent node's first
109 * cluster and index within the parent directory.
110 */
111static hash_table_t up_hash;
112
113#define UPH_BUCKETS_LOG 12
114#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
115
116#define UPH_DH_KEY 0
117#define UPH_PFC_KEY 1
118#define UPH_PDI_KEY 2
119
120static hash_index_t pos_hash(unsigned long key[])
121{
122 devmap_handle_t devmap_handle = (devmap_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];
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 */
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 |= (devmap_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
143 (3 * (UPH_BUCKETS_LOG / 4));
144
145 return h;
146}
147
148static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
149{
150 devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
151 fat_cluster_t pfc;
152 unsigned pdi;
153 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
154
155 switch (keys) {
156 case 1:
157 return (devmap_handle == fidx->devmap_handle);
158 case 3:
159 pfc = (fat_cluster_t) key[UPH_PFC_KEY];
160 pdi = (unsigned) key[UPH_PDI_KEY];
161 return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) &&
162 (pdi == fidx->pdi);
163 default:
164 assert((keys == 1) || (keys == 3));
165 }
166
167 return 0;
168}
169
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 devmap_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 devmap_handle_t devmap_handle = (devmap_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 = devmap_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 devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
210 fs_index_t index;
211 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
212
213 switch (keys) {
214 case 1:
215 return (devmap_handle == fidx->devmap_handle);
216 case 2:
217 index = (fs_index_t) key[UIH_INDEX_KEY];
218 return (devmap_handle == fidx->devmap_handle) &&
219 (index == fidx->index);
220 default:
221 assert((keys == 1) || (keys == 2));
222 }
223
224 return 0;
225}
226
227static void idx_remove_callback(link_t *item)
228{
229 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
230
231 free(fidx);
232}
233
234static hash_table_operations_t uih_ops = {
235 .hash = idx_hash,
236 .compare = idx_compare,
237 .remove_callback = idx_remove_callback,
238};
239
240/** Allocate a VFS index which is not currently in use. */
241static bool fat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index)
242{
243 unused_t *u;
244
245 assert(index);
246 u = unused_find(devmap_handle, true);
247 if (!u)
248 return false;
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;
258 fibril_mutex_unlock(&unused_lock);
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 }
271 fibril_mutex_unlock(&unused_lock);
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 */
279 fibril_mutex_unlock(&unused_lock);
280 return false;
281}
282
283/** If possible, coalesce two intervals of freed indices. */
284static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
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. */
303static void fat_index_free(devmap_handle_t devmap_handle, fs_index_t index)
304{
305 unused_t *u;
306
307 u = unused_find(devmap_handle, true);
308 assert(u);
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)
327 try_coalesce_intervals(lnk->prev, lnk,
328 lnk);
329 fibril_mutex_unlock(&unused_lock);
330 return;
331 }
332 if (f->last == index - 1) {
333 f->last++;
334 if (lnk->next != &u->freed_head)
335 try_coalesce_intervals(lnk, lnk->next,
336 lnk);
337 fibril_mutex_unlock(&unused_lock);
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);
348 fibril_mutex_unlock(&unused_lock);
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 }
362 fibril_mutex_unlock(&unused_lock);
363}
364
365static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
366{
367 fat_idx_t *fidx;
368
369 fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
370 if (!fidx)
371 return ENOMEM;
372 if (!fat_index_alloc(devmap_handle, &fidx->index)) {
373 free(fidx);
374 return ENOSPC;
375 }
376
377 link_initialize(&fidx->uph_link);
378 link_initialize(&fidx->uih_link);
379 fibril_mutex_initialize(&fidx->lock);
380 fidx->devmap_handle = devmap_handle;
381 fidx->pfc = FAT_CLST_RES0; /* no parent yet */
382 fidx->pdi = 0;
383 fidx->nodep = NULL;
384
385 *fidxp = fidx;
386 return EOK;
387}
388
389int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
390{
391 fat_idx_t *fidx;
392 int rc;
393
394 fibril_mutex_lock(&used_lock);
395 rc = fat_idx_create(&fidx, devmap_handle);
396 if (rc != EOK) {
397 fibril_mutex_unlock(&used_lock);
398 return rc;
399 }
400
401 unsigned long ikey[] = {
402 [UIH_DH_KEY] = devmap_handle,
403 [UIH_INDEX_KEY] = fidx->index,
404 };
405
406 hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
407 fibril_mutex_lock(&fidx->lock);
408 fibril_mutex_unlock(&used_lock);
409
410 *fidxp = fidx;
411 return EOK;
412}
413
414fat_idx_t *
415fat_idx_get_by_pos(devmap_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi)
416{
417 fat_idx_t *fidx;
418 link_t *l;
419 unsigned long pkey[] = {
420 [UPH_DH_KEY] = devmap_handle,
421 [UPH_PFC_KEY] = pfc,
422 [UPH_PDI_KEY] = pdi,
423 };
424
425 fibril_mutex_lock(&used_lock);
426 l = hash_table_find(&up_hash, pkey);
427 if (l) {
428 fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
429 } else {
430 int rc;
431
432 rc = fat_idx_create(&fidx, devmap_handle);
433 if (rc != EOK) {
434 fibril_mutex_unlock(&used_lock);
435 return NULL;
436 }
437
438 unsigned long ikey[] = {
439 [UIH_DH_KEY] = devmap_handle,
440 [UIH_INDEX_KEY] = fidx->index,
441 };
442
443 fidx->pfc = pfc;
444 fidx->pdi = pdi;
445
446 hash_table_insert(&up_hash, pkey, &fidx->uph_link);
447 hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
448 }
449 fibril_mutex_lock(&fidx->lock);
450 fibril_mutex_unlock(&used_lock);
451
452 return fidx;
453}
454
455void fat_idx_hashin(fat_idx_t *idx)
456{
457 unsigned long pkey[] = {
458 [UPH_DH_KEY] = idx->devmap_handle,
459 [UPH_PFC_KEY] = idx->pfc,
460 [UPH_PDI_KEY] = idx->pdi,
461 };
462
463 fibril_mutex_lock(&used_lock);
464 hash_table_insert(&up_hash, pkey, &idx->uph_link);
465 fibril_mutex_unlock(&used_lock);
466}
467
468void fat_idx_hashout(fat_idx_t *idx)
469{
470 unsigned long pkey[] = {
471 [UPH_DH_KEY] = idx->devmap_handle,
472 [UPH_PFC_KEY] = idx->pfc,
473 [UPH_PDI_KEY] = idx->pdi,
474 };
475
476 fibril_mutex_lock(&used_lock);
477 hash_table_remove(&up_hash, pkey, 3);
478 fibril_mutex_unlock(&used_lock);
479}
480
481fat_idx_t *
482fat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index)
483{
484 fat_idx_t *fidx = NULL;
485 link_t *l;
486 unsigned long ikey[] = {
487 [UIH_DH_KEY] = devmap_handle,
488 [UIH_INDEX_KEY] = index,
489 };
490
491 fibril_mutex_lock(&used_lock);
492 l = hash_table_find(&ui_hash, ikey);
493 if (l) {
494 fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
495 fibril_mutex_lock(&fidx->lock);
496 }
497 fibril_mutex_unlock(&used_lock);
498
499 return fidx;
500}
501
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->devmap_handle,
510 [UIH_INDEX_KEY] = idx->index,
511 };
512 devmap_handle_t devmap_handle = idx->devmap_handle;
513 fs_index_t index = idx->index;
514
515 assert(idx->pfc == FAT_CLST_RES0);
516
517 fibril_mutex_lock(&used_lock);
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);
524 fibril_mutex_unlock(&used_lock);
525 /* Release the VFS index. */
526 fat_index_free(devmap_handle, index);
527 /* The index structure itself is freed in idx_remove_callback(). */
528}
529
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_devmap_handle(devmap_handle_t devmap_handle)
549{
550 unused_t *u;
551 int rc = EOK;
552
553 u = (unused_t *) malloc(sizeof(unused_t));
554 if (!u)
555 return ENOMEM;
556 unused_initialize(u, devmap_handle);
557 fibril_mutex_lock(&unused_lock);
558 if (!unused_find(devmap_handle, false)) {
559 list_append(&u->link, &unused_head);
560 } else {
561 free(u);
562 rc = EEXIST;
563 }
564 fibril_mutex_unlock(&unused_lock);
565 return rc;
566}
567
568void fat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle)
569{
570 unsigned long ikey[] = {
571 [UIH_DH_KEY] = devmap_handle
572 };
573 unsigned long pkey[] = {
574 [UPH_DH_KEY] = devmap_handle
575 };
576
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(devmap_handle, true);
591 assert(u);
592 list_remove(&u->link);
593 fibril_mutex_unlock(&unused_lock);
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.