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

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

Merge from lp:~romanenko-oleg/helenos/fat.

  • Property mode set to 100644
File size: 14.6 KB
RevLine 
[4c3c4a5]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 {
[9cc1f43]60 link_t link;
[375ab5e]61 service_id_t service_id;
[4c3c4a5]62
63 /** Next unassigned index. */
[9cc1f43]64 fs_index_t next;
[4c3c4a5]65 /** Number of remaining unassigned indices. */
[9cc1f43]66 uint64_t remaining;
[4c3c4a5]67
68 /** Sorted list of intervals of freed indices. */
[e090244c]69 list_t freed_list;
[4c3c4a5]70} unused_t;
71
72/** Mutex protecting the list of unused structures. */
73static FIBRIL_MUTEX_INITIALIZE(unused_lock);
74
75/** List of unused structures. */
[9cc1f43]76static LIST_INITIALIZE(unused_list);
[4c3c4a5]77
[375ab5e]78static void unused_initialize(unused_t *u, service_id_t service_id)
[4c3c4a5]79{
80 link_initialize(&u->link);
[375ab5e]81 u->service_id = service_id;
[4c3c4a5]82 u->next = 0;
83 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
[9cc1f43]84 list_initialize(&u->freed_list);
[4c3c4a5]85}
86
[375ab5e]87static unused_t *unused_find(service_id_t service_id, bool lock)
[4c3c4a5]88{
89 unused_t *u;
90
91 if (lock)
92 fibril_mutex_lock(&unused_lock);
[9cc1f43]93 list_foreach(unused_list, l) {
[4c3c4a5]94 u = list_get_instance(l, unused_t, link);
[375ab5e]95 if (u->service_id == service_id)
[4c3c4a5]96 return u;
97 }
[9cc1f43]98
[4c3c4a5]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.
[375ab5e]109 * The index structures are hashed by the service_id, parent node's first
[4c3c4a5]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
[375ab5e]117#define UPH_SID_KEY 0
[4c3c4a5]118#define UPH_PFC_KEY 1
119#define UPH_PDI_KEY 2
120
121static hash_index_t pos_hash(unsigned long key[])
122{
[375ab5e]123 service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
[4c3c4a5]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);
[375ab5e]143 h |= (service_id & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
[4c3c4a5]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{
[375ab5e]151 service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
[4c3c4a5]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:
[375ab5e]158 return (service_id == fidx->service_id);
[4c3c4a5]159 case 3:
160 pfc = (exfat_cluster_t) key[UPH_PFC_KEY];
161 pdi = (unsigned) key[UPH_PDI_KEY];
[375ab5e]162 return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
[4c3c4a5]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.
[375ab5e]184 * The index structures are hashed by the service_id and index.
[4c3c4a5]185 */
186static hash_table_t ui_hash;
187
188#define UIH_BUCKETS_LOG 12
189#define UIH_BUCKETS (1 << UIH_BUCKETS_LOG)
190
[375ab5e]191#define UIH_SID_KEY 0
[4c3c4a5]192#define UIH_INDEX_KEY 1
193
194static hash_index_t idx_hash(unsigned long key[])
195{
[375ab5e]196 service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
[4c3c4a5]197 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
198
199 hash_index_t h;
200
[375ab5e]201 h = service_id & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
[4c3c4a5]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{
[375ab5e]210 service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
[4c3c4a5]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:
[375ab5e]216 return (service_id == fidx->service_id);
[4c3c4a5]217 case 2:
218 index = (fs_index_t) key[UIH_INDEX_KEY];
[375ab5e]219 return (service_id == fidx->service_id) &&
[4c3c4a5]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. */
[375ab5e]242static bool exfat_index_alloc(service_id_t service_id, fs_index_t *index)
[4c3c4a5]243{
244 unused_t *u;
245
246 assert(index);
[375ab5e]247 u = unused_find(service_id, true);
[4c3c4a5]248 if (!u)
249 return false;
250
[9cc1f43]251 if (list_empty(&u->freed_list)) {
[4c3c4a5]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. */
[9cc1f43]264 freed_t *f = list_get_instance(list_first(&u->freed_list),
265 freed_t, link);
[4c3c4a5]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. */
[375ab5e]304static void exfat_index_free(service_id_t service_id, fs_index_t index)
[4c3c4a5]305{
306 unused_t *u;
307
[375ab5e]308 u = unused_find(service_id, true);
[4c3c4a5]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;
[9cc1f43]322 for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
[4c3c4a5]323 lnk = lnk->next) {
324 freed_t *f = list_get_instance(lnk, freed_t, link);
325 if (f->first == index + 1) {
326 f->first--;
[9cc1f43]327 if (lnk->prev != &u->freed_list.head)
[4c3c4a5]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++;
[9cc1f43]335 if (lnk->next != &u->freed_list.head)
[4c3c4a5]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;
[9cc1f43]361 list_append(&n->link, &u->freed_list);
[4c3c4a5]362 }
363 fibril_mutex_unlock(&unused_lock);
364}
365
[375ab5e]366static int exfat_idx_create(exfat_idx_t **fidxp, service_id_t service_id)
[4c3c4a5]367{
368 exfat_idx_t *fidx;
369
370 fidx = (exfat_idx_t *) malloc(sizeof(exfat_idx_t));
371 if (!fidx)
372 return ENOMEM;
[375ab5e]373 if (!exfat_index_alloc(service_id, &fidx->index)) {
[4c3c4a5]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);
[375ab5e]381 fidx->service_id = service_id;
[4c3c4a5]382 fidx->pfc = 0; /* no parent yet */
383 fidx->pdi = 0;
384 fidx->nodep = NULL;
385
386 *fidxp = fidx;
387 return EOK;
388}
389
[375ab5e]390int exfat_idx_get_new(exfat_idx_t **fidxp, service_id_t service_id)
[4c3c4a5]391{
392 exfat_idx_t *fidx;
393 int rc;
394
395 fibril_mutex_lock(&used_lock);
[375ab5e]396 rc = exfat_idx_create(&fidx, service_id);
[4c3c4a5]397 if (rc != EOK) {
398 fibril_mutex_unlock(&used_lock);
399 return rc;
400 }
401
402 unsigned long ikey[] = {
[375ab5e]403 [UIH_SID_KEY] = service_id,
[4c3c4a5]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 *
[375ab5e]416exfat_idx_get_by_pos(service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
[4c3c4a5]417{
418 exfat_idx_t *fidx;
419 link_t *l;
420 unsigned long pkey[] = {
[375ab5e]421 [UPH_SID_KEY] = service_id,
[4c3c4a5]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
[375ab5e]433 rc = exfat_idx_create(&fidx, service_id);
[4c3c4a5]434 if (rc != EOK) {
435 fibril_mutex_unlock(&used_lock);
436 return NULL;
437 }
438
439 unsigned long ikey[] = {
[375ab5e]440 [UIH_SID_KEY] = service_id,
[4c3c4a5]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[] = {
[375ab5e]459 [UPH_SID_KEY] = idx->service_id,
[4c3c4a5]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[] = {
[375ab5e]472 [UPH_SID_KEY] = idx->service_id,
[4c3c4a5]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 *
[375ab5e]483exfat_idx_get_by_index(service_id_t service_id, fs_index_t index)
[4c3c4a5]484{
485 exfat_idx_t *fidx = NULL;
486 link_t *l;
487 unsigned long ikey[] = {
[375ab5e]488 [UIH_SID_KEY] = service_id,
[4c3c4a5]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[] = {
[375ab5e]510 [UIH_SID_KEY] = idx->service_id,
[4c3c4a5]511 [UIH_INDEX_KEY] = idx->index,
512 };
[375ab5e]513 service_id_t service_id = idx->service_id;
[4c3c4a5]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. */
[375ab5e]528 exfat_index_free(service_id, index);
[4c3c4a5]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
[375ab5e]550int exfat_idx_init_by_service_id(service_id_t service_id)
[4c3c4a5]551{
552 unused_t *u;
553 int rc = EOK;
554
555 u = (unused_t *) malloc(sizeof(unused_t));
556 if (!u)
557 return ENOMEM;
[375ab5e]558 unused_initialize(u, service_id);
[4c3c4a5]559 fibril_mutex_lock(&unused_lock);
[375ab5e]560 if (!unused_find(service_id, false)) {
[9cc1f43]561 list_append(&u->link, &unused_list);
[4c3c4a5]562 } else {
563 free(u);
564 rc = EEXIST;
565 }
566 fibril_mutex_unlock(&unused_lock);
567 return rc;
568}
569
[375ab5e]570void exfat_idx_fini_by_service_id(service_id_t service_id)
[4c3c4a5]571{
572 unsigned long ikey[] = {
[375ab5e]573 [UIH_SID_KEY] = service_id
[4c3c4a5]574 };
575 unsigned long pkey[] = {
[375ab5e]576 [UPH_SID_KEY] = service_id
[4c3c4a5]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 */
[375ab5e]592 unused_t *u = unused_find(service_id, true);
[4c3c4a5]593 assert(u);
594 list_remove(&u->link);
595 fibril_mutex_unlock(&unused_lock);
596
[9cc1f43]597 while (!list_empty(&u->freed_list)) {
[4c3c4a5]598 freed_t *f;
[9cc1f43]599 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
[4c3c4a5]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.