source: mainline/uspace/srv/fs/exfat/exfat_idx.c@ 25c60f4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25c60f4 was e090244c, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

Fix for exfat_idx.c

  • Property mode set to 100644
File size: 14.8 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 exfat_idx.c
35 * @brief Layer for translating exFAT entities to VFS node indices.
36 */
37
38#include "exfat.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#include <malloc.h>
47
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;
61 devmap_handle_t devmap_handle;
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 list_t freed_list;
70} unused_t;
71
72/** Mutex protecting the list of unused structures. */
73static FIBRIL_MUTEX_INITIALIZE(unused_lock);
74
75/** List of unused structures. */
76static LIST_INITIALIZE(unused_list);
77
78static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle)
79{
80 link_initialize(&u->link);
81 u->devmap_handle = devmap_handle;
82 u->next = 0;
83 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
84 list_initialize(&u->freed_list);
85}
86
87static unused_t *unused_find(devmap_handle_t devmap_handle, bool lock)
88{
89 unused_t *u;
90
91 if (lock)
92 fibril_mutex_lock(&unused_lock);
93 list_foreach(unused_list, l) {
94 u = list_get_instance(l, unused_t, link);
95 if (u->devmap_handle == devmap_handle)
96 return u;
97 }
98
99 if (lock)
100 fibril_mutex_unlock(&unused_lock);
101 return NULL;
102}
103
104/** Mutex protecting the up_hash and ui_hash. */
105static FIBRIL_MUTEX_INITIALIZE(used_lock);
106
107/**
108 * Global hash table of all used exfat_idx_t structures.
109 * The index structures are hashed by the devmap_handle, parent node's first
110 * cluster and index within the parent directory.
111 */
112static hash_table_t up_hash;
113
114#define UPH_BUCKETS_LOG 12
115#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
116
117#define UPH_DH_KEY 0
118#define UPH_PFC_KEY 1
119#define UPH_PDI_KEY 2
120
121static hash_index_t pos_hash(unsigned long key[])
122{
123 devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
124 exfat_cluster_t pfc = (exfat_cluster_t)key[UPH_PFC_KEY];
125 unsigned pdi = (unsigned)key[UPH_PDI_KEY];
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 */
140 h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1);
141 h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
142 (UPH_BUCKETS_LOG / 2);
143 h |= (devmap_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
144 (3 * (UPH_BUCKETS_LOG / 4));
145
146 return h;
147}
148
149static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
150{
151 devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
152 exfat_cluster_t pfc;
153 unsigned pdi;
154 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uph_link);
155
156 switch (keys) {
157 case 1:
158 return (devmap_handle == fidx->devmap_handle);
159 case 3:
160 pfc = (exfat_cluster_t) key[UPH_PFC_KEY];
161 pdi = (unsigned) key[UPH_PDI_KEY];
162 return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) &&
163 (pdi == fidx->pdi);
164 default:
165 assert((keys == 1) || (keys == 3));
166 }
167
168 return 0;
169}
170
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.
184 * The index structures are hashed by the devmap_handle and index.
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{
196 devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
197 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
198
199 hash_index_t h;
200
201 h = devmap_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
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{
210 devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
211 fs_index_t index;
212 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
213
214 switch (keys) {
215 case 1:
216 return (devmap_handle == fidx->devmap_handle);
217 case 2:
218 index = (fs_index_t) key[UIH_INDEX_KEY];
219 return (devmap_handle == fidx->devmap_handle) &&
220 (index == fidx->index);
221 default:
222 assert((keys == 1) || (keys == 2));
223 }
224
225 return 0;
226}
227
228static void idx_remove_callback(link_t *item)
229{
230 exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
231
232 free(fidx);
233}
234
235static hash_table_operations_t uih_ops = {
236 .hash = idx_hash,
237 .compare = idx_compare,
238 .remove_callback = idx_remove_callback,
239};
240
241/** Allocate a VFS index which is not currently in use. */
242static bool exfat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index)
243{
244 unused_t *u;
245
246 assert(index);
247 u = unused_find(devmap_handle, true);
248 if (!u)
249 return false;
250
251 if (list_empty(&u->freed_list)) {
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;
259 fibril_mutex_unlock(&unused_lock);
260 return true;
261 }
262 } else {
263 /* There are some freed indices which we can reuse. */
264 freed_t *f = list_get_instance(list_first(&u->freed_list),
265 freed_t, link);
266 *index = f->first;
267 if (f->first++ == f->last) {
268 /* Destroy the interval. */
269 list_remove(&f->link);
270 free(f);
271 }
272 fibril_mutex_unlock(&unused_lock);
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 */
280 fibril_mutex_unlock(&unused_lock);
281 return false;
282}
283
284/** If possible, coalesce two intervals of freed indices. */
285static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
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. */
304static void exfat_index_free(devmap_handle_t devmap_handle, fs_index_t index)
305{
306 unused_t *u;
307
308 u = unused_find(devmap_handle, true);
309 assert(u);
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_list.head.next; lnk != &u->freed_list.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_list.head)
328 try_coalesce_intervals(lnk->prev, lnk,
329 lnk);
330 fibril_mutex_unlock(&unused_lock);
331 return;
332 }
333 if (f->last == index - 1) {
334 f->last++;
335 if (lnk->next != &u->freed_list.head)
336 try_coalesce_intervals(lnk, lnk->next,
337 lnk);
338 fibril_mutex_unlock(&unused_lock);
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);
349 fibril_mutex_unlock(&unused_lock);
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_list);
362 }
363 fibril_mutex_unlock(&unused_lock);
364}
365
366static int exfat_idx_create(exfat_idx_t **fidxp, devmap_handle_t devmap_handle)
367{
368 exfat_idx_t *fidx;
369
370 fidx = (exfat_idx_t *) malloc(sizeof(exfat_idx_t));
371 if (!fidx)
372 return ENOMEM;
373 if (!exfat_index_alloc(devmap_handle, &fidx->index)) {
374 free(fidx);
375 return ENOSPC;
376 }
377
378 link_initialize(&fidx->uph_link);
379 link_initialize(&fidx->uih_link);
380 fibril_mutex_initialize(&fidx->lock);
381 fidx->devmap_handle = devmap_handle;
382 fidx->pfc = 0; /* no parent yet */
383 fidx->pdi = 0;
384 fidx->nodep = NULL;
385
386 *fidxp = fidx;
387 return EOK;
388}
389
390int exfat_idx_get_new(exfat_idx_t **fidxp, devmap_handle_t devmap_handle)
391{
392 exfat_idx_t *fidx;
393 int rc;
394
395 fibril_mutex_lock(&used_lock);
396 rc = exfat_idx_create(&fidx, devmap_handle);
397 if (rc != EOK) {
398 fibril_mutex_unlock(&used_lock);
399 return rc;
400 }
401
402 unsigned long ikey[] = {
403 [UIH_DH_KEY] = devmap_handle,
404 [UIH_INDEX_KEY] = fidx->index,
405 };
406
407 hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
408 fibril_mutex_lock(&fidx->lock);
409 fibril_mutex_unlock(&used_lock);
410
411 *fidxp = fidx;
412 return EOK;
413}
414
415exfat_idx_t *
416exfat_idx_get_by_pos(devmap_handle_t devmap_handle, exfat_cluster_t pfc, unsigned pdi)
417{
418 exfat_idx_t *fidx;
419 link_t *l;
420 unsigned long pkey[] = {
421 [UPH_DH_KEY] = devmap_handle,
422 [UPH_PFC_KEY] = pfc,
423 [UPH_PDI_KEY] = pdi,
424 };
425
426 fibril_mutex_lock(&used_lock);
427 l = hash_table_find(&up_hash, pkey);
428 if (l) {
429 fidx = hash_table_get_instance(l, exfat_idx_t, uph_link);
430 } else {
431 int rc;
432
433 rc = exfat_idx_create(&fidx, devmap_handle);
434 if (rc != EOK) {
435 fibril_mutex_unlock(&used_lock);
436 return NULL;
437 }
438
439 unsigned long ikey[] = {
440 [UIH_DH_KEY] = devmap_handle,
441 [UIH_INDEX_KEY] = fidx->index,
442 };
443
444 fidx->pfc = pfc;
445 fidx->pdi = pdi;
446
447 hash_table_insert(&up_hash, pkey, &fidx->uph_link);
448 hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
449 }
450 fibril_mutex_lock(&fidx->lock);
451 fibril_mutex_unlock(&used_lock);
452
453 return fidx;
454}
455
456void exfat_idx_hashin(exfat_idx_t *idx)
457{
458 unsigned long pkey[] = {
459 [UPH_DH_KEY] = idx->devmap_handle,
460 [UPH_PFC_KEY] = idx->pfc,
461 [UPH_PDI_KEY] = idx->pdi,
462 };
463
464 fibril_mutex_lock(&used_lock);
465 hash_table_insert(&up_hash, pkey, &idx->uph_link);
466 fibril_mutex_unlock(&used_lock);
467}
468
469void exfat_idx_hashout(exfat_idx_t *idx)
470{
471 unsigned long pkey[] = {
472 [UPH_DH_KEY] = idx->devmap_handle,
473 [UPH_PFC_KEY] = idx->pfc,
474 [UPH_PDI_KEY] = idx->pdi,
475 };
476
477 fibril_mutex_lock(&used_lock);
478 hash_table_remove(&up_hash, pkey, 3);
479 fibril_mutex_unlock(&used_lock);
480}
481
482exfat_idx_t *
483exfat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index)
484{
485 exfat_idx_t *fidx = NULL;
486 link_t *l;
487 unsigned long ikey[] = {
488 [UIH_DH_KEY] = devmap_handle,
489 [UIH_INDEX_KEY] = index,
490 };
491
492 fibril_mutex_lock(&used_lock);
493 l = hash_table_find(&ui_hash, ikey);
494 if (l) {
495 fidx = hash_table_get_instance(l, exfat_idx_t, uih_link);
496 fibril_mutex_lock(&fidx->lock);
497 }
498 fibril_mutex_unlock(&used_lock);
499
500 return fidx;
501}
502
503/** Destroy the index structure.
504 *
505 * @param idx The index structure to be destroyed.
506 */
507void exfat_idx_destroy(exfat_idx_t *idx)
508{
509 unsigned long ikey[] = {
510 [UIH_DH_KEY] = idx->devmap_handle,
511 [UIH_INDEX_KEY] = idx->index,
512 };
513 devmap_handle_t devmap_handle = idx->devmap_handle;
514 fs_index_t index = idx->index;
515
516 /* TODO: assert(idx->pfc == FAT_CLST_RES0); */
517 assert(idx->pfc == 0);
518
519 fibril_mutex_lock(&used_lock);
520 /*
521 * Since we can only free unlinked nodes, the index structure is not
522 * present in the position hash (uph). We therefore hash it out from
523 * the index hash only.
524 */
525 hash_table_remove(&ui_hash, ikey, 2);
526 fibril_mutex_unlock(&used_lock);
527 /* Release the VFS index. */
528 exfat_index_free(devmap_handle, index);
529 /* The index structure itself is freed in idx_remove_callback(). */
530}
531
532int exfat_idx_init(void)
533{
534 if (!hash_table_create(&up_hash, UPH_BUCKETS, 3, &uph_ops))
535 return ENOMEM;
536 if (!hash_table_create(&ui_hash, UIH_BUCKETS, 2, &uih_ops)) {
537 hash_table_destroy(&up_hash);
538 return ENOMEM;
539 }
540 return EOK;
541}
542
543void exfat_idx_fini(void)
544{
545 /* We assume the hash tables are empty. */
546 hash_table_destroy(&up_hash);
547 hash_table_destroy(&ui_hash);
548}
549
550int exfat_idx_init_by_devmap_handle(devmap_handle_t devmap_handle)
551{
552 unused_t *u;
553 int rc = EOK;
554
555 u = (unused_t *) malloc(sizeof(unused_t));
556 if (!u)
557 return ENOMEM;
558 unused_initialize(u, devmap_handle);
559 fibril_mutex_lock(&unused_lock);
560 if (!unused_find(devmap_handle, false)) {
561 list_append(&u->link, &unused_list);
562 } else {
563 free(u);
564 rc = EEXIST;
565 }
566 fibril_mutex_unlock(&unused_lock);
567 return rc;
568}
569
570void exfat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle)
571{
572 unsigned long ikey[] = {
573 [UIH_DH_KEY] = devmap_handle
574 };
575 unsigned long pkey[] = {
576 [UPH_DH_KEY] = devmap_handle
577 };
578
579 /*
580 * Remove this instance's index structure from up_hash and ui_hash.
581 * Process up_hash first and ui_hash second because the index structure
582 * is actually removed in idx_remove_callback().
583 */
584 fibril_mutex_lock(&used_lock);
585 hash_table_remove(&up_hash, pkey, 1);
586 hash_table_remove(&ui_hash, ikey, 1);
587 fibril_mutex_unlock(&used_lock);
588
589 /*
590 * Free the unused and freed structures for this instance.
591 */
592 unused_t *u = unused_find(devmap_handle, true);
593 assert(u);
594 list_remove(&u->link);
595 fibril_mutex_unlock(&unused_lock);
596
597 while (!list_empty(&u->freed_list)) {
598 freed_t *f;
599 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
600 list_remove(&f->link);
601 free(f);
602 }
603 free(u);
604}
605
606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.