2
* Copyright (c) 2008 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
35
* @brief Layer for translating FAT entities to VFS node indices.
39
#include "../../vfs/vfs.h"
42
#include <adt/hash_table.h>
45
#include <fibril_sync.h>
47
/** Each instance of this type describes one interval of freed VFS indices. */
55
* Each instance of this type describes state of all VFS indices that
56
* are currently unused.
60
dev_handle_t dev_handle;
62
/** Next unassigned index. */
64
/** Number of remaining unassigned indices. */
67
/** Sorted list of intervals of freed indices. */
71
/** Mutex protecting the list of unused structures. */
72
static FIBRIL_MUTEX_INITIALIZE(unused_lock);
74
/** List of unused structures. */
75
static LIST_INITIALIZE(unused_head);
77
static void unused_initialize(unused_t *u, dev_handle_t dev_handle)
79
link_initialize(&u->link);
80
u->dev_handle = dev_handle;
82
u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
83
list_initialize(&u->freed_head);
86
static unused_t *unused_find(dev_handle_t dev_handle, bool 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->dev_handle == dev_handle)
99
fibril_mutex_unlock(&unused_lock);
103
/** Mutex protecting the up_hash and ui_hash. */
104
static FIBRIL_MUTEX_INITIALIZE(used_lock);
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.
111
static hash_table_t up_hash;
113
#define UPH_BUCKETS_LOG 12
114
#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
117
#define UPH_PFC_KEY 1
118
#define UPH_PDI_KEY 2
120
static hash_index_t pos_hash(unsigned long key[])
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];
129
* The least significant half of all bits are the least significant bits
130
* of the parent node's first cluster.
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.
136
* The most significant half of the most significant half of all bits
137
* are the least significant bits of the device handle.
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));
148
static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
150
dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
151
fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
152
unsigned pdi = (unsigned)key[UPH_PDI_KEY];
153
fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
155
return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
159
static void pos_remove_callback(link_t *item)
164
static hash_table_operations_t uph_ops = {
166
.compare = pos_compare,
167
.remove_callback = pos_remove_callback,
171
* Global hash table of all used fat_idx_t structures.
172
* The index structures are hashed by the dev_handle and index.
174
static hash_table_t ui_hash;
176
#define UIH_BUCKETS_LOG 12
177
#define UIH_BUCKETS (1 << UIH_BUCKETS_LOG)
180
#define UIH_INDEX_KEY 1
182
static hash_index_t idx_hash(unsigned long key[])
184
dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
185
fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
189
h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
190
h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
191
(UIH_BUCKETS_LOG / 2);
196
static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
198
dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
199
fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
200
fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
202
return (dev_handle == fidx->dev_handle) && (index == fidx->index);
205
static void idx_remove_callback(link_t *item)
210
static hash_table_operations_t uih_ops = {
212
.compare = idx_compare,
213
.remove_callback = idx_remove_callback,
216
/** Allocate a VFS index which is not currently in use. */
217
static bool fat_index_alloc(dev_handle_t dev_handle, fs_index_t *index)
222
u = unused_find(dev_handle, true);
226
if (list_empty(&u->freed_head)) {
229
* There are no freed indices, allocate one directly
234
fibril_mutex_unlock(&unused_lock);
238
/* There are some freed indices which we can reuse. */
239
freed_t *f = list_get_instance(u->freed_head.next, freed_t,
242
if (f->first++ == f->last) {
243
/* Destroy the interval. */
244
list_remove(&f->link);
247
fibril_mutex_unlock(&unused_lock);
251
* We ran out of indices, which is extremely unlikely with FAT16, but
252
* theoretically still possible (e.g. too many open unlinked nodes or
253
* too many zero-sized nodes).
255
fibril_mutex_unlock(&unused_lock);
259
/** If possible, coalesce two intervals of freed indices. */
260
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
262
freed_t *fl = list_get_instance(l, freed_t, link);
263
freed_t *fr = list_get_instance(r, freed_t, link);
265
if (fl->last + 1 == fr->first) {
271
fr->first = fl->first;
278
/** Free a VFS index, which is no longer in use. */
279
static void fat_index_free(dev_handle_t dev_handle, fs_index_t index)
283
u = unused_find(dev_handle, true);
286
if (u->next == index + 1) {
287
/* The index can be returned directly to the counter. */
292
* The index must be returned either to an existing freed
293
* interval or a new interval must be created.
297
for (lnk = u->freed_head.next; lnk != &u->freed_head;
299
freed_t *f = list_get_instance(lnk, freed_t, link);
300
if (f->first == index + 1) {
302
if (lnk->prev != &u->freed_head)
303
try_coalesce_intervals(lnk->prev, lnk,
305
fibril_mutex_unlock(&unused_lock);
308
if (f->last == index - 1) {
310
if (lnk->next != &u->freed_head)
311
try_coalesce_intervals(lnk, lnk->next,
313
fibril_mutex_unlock(&unused_lock);
316
if (index > f->first) {
317
n = malloc(sizeof(freed_t));
318
/* TODO: sleep until allocation succeeds */
320
link_initialize(&n->link);
323
list_insert_before(&n->link, lnk);
324
fibril_mutex_unlock(&unused_lock);
329
/* The index will form the last interval. */
330
n = malloc(sizeof(freed_t));
331
/* TODO: sleep until allocation succeeds */
333
link_initialize(&n->link);
336
list_append(&n->link, &u->freed_head);
338
fibril_mutex_unlock(&unused_lock);
341
static fat_idx_t *fat_idx_create(dev_handle_t dev_handle)
345
fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
348
if (!fat_index_alloc(dev_handle, &fidx->index)) {
353
link_initialize(&fidx->uph_link);
354
link_initialize(&fidx->uih_link);
355
fibril_mutex_initialize(&fidx->lock);
356
fidx->dev_handle = dev_handle;
357
fidx->pfc = FAT_CLST_RES0; /* no parent yet */
364
fat_idx_t *fat_idx_get_new(dev_handle_t dev_handle)
368
fibril_mutex_lock(&used_lock);
369
fidx = fat_idx_create(dev_handle);
371
fibril_mutex_unlock(&used_lock);
375
unsigned long ikey[] = {
376
[UIH_DH_KEY] = dev_handle,
377
[UIH_INDEX_KEY] = fidx->index,
380
hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
381
fibril_mutex_lock(&fidx->lock);
382
fibril_mutex_unlock(&used_lock);
388
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
392
unsigned long pkey[] = {
393
[UPH_DH_KEY] = dev_handle,
398
fibril_mutex_lock(&used_lock);
399
l = hash_table_find(&up_hash, pkey);
401
fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
403
fidx = fat_idx_create(dev_handle);
405
fibril_mutex_unlock(&used_lock);
409
unsigned long ikey[] = {
410
[UIH_DH_KEY] = dev_handle,
411
[UIH_INDEX_KEY] = fidx->index,
417
hash_table_insert(&up_hash, pkey, &fidx->uph_link);
418
hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
420
fibril_mutex_lock(&fidx->lock);
421
fibril_mutex_unlock(&used_lock);
426
void fat_idx_hashin(fat_idx_t *idx)
428
unsigned long pkey[] = {
429
[UPH_DH_KEY] = idx->dev_handle,
430
[UPH_PFC_KEY] = idx->pfc,
431
[UPH_PDI_KEY] = idx->pdi,
434
fibril_mutex_lock(&used_lock);
435
hash_table_insert(&up_hash, pkey, &idx->uph_link);
436
fibril_mutex_unlock(&used_lock);
439
void fat_idx_hashout(fat_idx_t *idx)
441
unsigned long pkey[] = {
442
[UPH_DH_KEY] = idx->dev_handle,
443
[UPH_PFC_KEY] = idx->pfc,
444
[UPH_PDI_KEY] = idx->pdi,
447
fibril_mutex_lock(&used_lock);
448
hash_table_remove(&up_hash, pkey, 3);
449
fibril_mutex_unlock(&used_lock);
453
fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index)
455
fat_idx_t *fidx = NULL;
457
unsigned long ikey[] = {
458
[UIH_DH_KEY] = dev_handle,
459
[UIH_INDEX_KEY] = index,
462
fibril_mutex_lock(&used_lock);
463
l = hash_table_find(&ui_hash, ikey);
465
fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
466
fibril_mutex_lock(&fidx->lock);
468
fibril_mutex_unlock(&used_lock);
473
/** Destroy the index structure.
475
* @param idx The index structure to be destroyed.
477
void fat_idx_destroy(fat_idx_t *idx)
479
unsigned long ikey[] = {
480
[UIH_DH_KEY] = idx->dev_handle,
481
[UIH_INDEX_KEY] = idx->index,
484
assert(idx->pfc == FAT_CLST_RES0);
486
fibril_mutex_lock(&used_lock);
488
* Since we can only free unlinked nodes, the index structure is not
489
* present in the position hash (uph). We therefore hash it out from
490
* the index hash only.
492
hash_table_remove(&ui_hash, ikey, 2);
493
fibril_mutex_unlock(&used_lock);
494
/* Release the VFS index. */
495
fat_index_free(idx->dev_handle, idx->index);
496
/* Deallocate the structure. */
500
int fat_idx_init(void)
502
if (!hash_table_create(&up_hash, UPH_BUCKETS, 3, &uph_ops))
504
if (!hash_table_create(&ui_hash, UIH_BUCKETS, 2, &uih_ops)) {
505
hash_table_destroy(&up_hash);
511
void fat_idx_fini(void)
513
/* We assume the hash tables are empty. */
514
hash_table_destroy(&up_hash);
515
hash_table_destroy(&ui_hash);
518
int fat_idx_init_by_dev_handle(dev_handle_t dev_handle)
523
u = (unused_t *) malloc(sizeof(unused_t));
526
unused_initialize(u, dev_handle);
527
fibril_mutex_lock(&unused_lock);
528
if (!unused_find(dev_handle, false))
529
list_append(&u->link, &unused_head);
532
fibril_mutex_unlock(&unused_lock);
536
void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle)
540
u = unused_find(dev_handle, true);
542
list_remove(&u->link);
543
fibril_mutex_unlock(&unused_lock);
545
while (!list_empty(&u->freed_head)) {
547
f = list_get_instance(u->freed_head.next, freed_t, link);
548
list_remove(&f->link);