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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b596d0d was 61eb2ce2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Make hash table operations immutable, because global mutable state is evil

  • Property mode set to 100644
File size: 13.4 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
[b1834a01]29/** @addtogroup exfat
[4c3c4a5]30 * @{
[1b20da0]31 */
[4c3c4a5]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>
[062d900]43#include <adt/hash.h>
[4c3c4a5]44#include <adt/list.h>
45#include <assert.h>
46#include <fibril_synch.h>
[38d150e]47#include <stdlib.h>
[4c3c4a5]48
49/** Each instance of this type describes one interval of freed VFS indices. */
50typedef struct {
51 link_t link;
52 fs_index_t first;
53 fs_index_t last;
54} freed_t;
55
56/**
57 * Each instance of this type describes state of all VFS indices that
58 * are currently unused.
59 */
60typedef struct {
[9cc1f43]61 link_t link;
[375ab5e]62 service_id_t service_id;
[4c3c4a5]63
64 /** Next unassigned index. */
[9cc1f43]65 fs_index_t next;
[4c3c4a5]66 /** Number of remaining unassigned indices. */
[9cc1f43]67 uint64_t remaining;
[4c3c4a5]68
69 /** Sorted list of intervals of freed indices. */
[e090244c]70 list_t freed_list;
[4c3c4a5]71} unused_t;
72
73/** Mutex protecting the list of unused structures. */
74static FIBRIL_MUTEX_INITIALIZE(unused_lock);
75
76/** List of unused structures. */
[9cc1f43]77static LIST_INITIALIZE(unused_list);
[4c3c4a5]78
[375ab5e]79static void unused_initialize(unused_t *u, service_id_t service_id)
[4c3c4a5]80{
81 link_initialize(&u->link);
[375ab5e]82 u->service_id = service_id;
[4c3c4a5]83 u->next = 0;
84 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
[9cc1f43]85 list_initialize(&u->freed_list);
[4c3c4a5]86}
87
[375ab5e]88static unused_t *unused_find(service_id_t service_id, bool lock)
[4c3c4a5]89{
90 if (lock)
91 fibril_mutex_lock(&unused_lock);
[062d900]92
[feeac0d]93 list_foreach(unused_list, link, unused_t, u) {
[1b20da0]94 if (u->service_id == service_id)
[4c3c4a5]95 return u;
96 }
[9cc1f43]97
[4c3c4a5]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 exfat_idx_t structures.
[375ab5e]108 * The index structures are hashed by the service_id, parent node's first
[4c3c4a5]109 * cluster and index within the parent directory.
[1b20da0]110 */
[4c3c4a5]111static hash_table_t up_hash;
112
[062d900]113typedef struct {
114 service_id_t service_id;
115 exfat_cluster_t pfc;
116 unsigned pdi;
117} pos_key_t;
[4c3c4a5]118
[5e801dc]119static inline size_t pos_key_hash(const void *key)
[4c3c4a5]120{
[5e801dc]121 const pos_key_t *pos = key;
[a35b458]122
[062d900]123 size_t hash = 0;
124 hash = hash_combine(pos->pfc, pos->pdi);
125 return hash_combine(hash, pos->service_id);
[4c3c4a5]126}
127
[062d900]128static size_t pos_hash(const ht_link_t *item)
[4c3c4a5]129{
[062d900]130 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
[a35b458]131
[062d900]132 pos_key_t pkey = {
133 .service_id = fidx->service_id,
134 .pfc = fidx->pfc,
135 .pdi = fidx->pdi,
136 };
[a35b458]137
[062d900]138 return pos_key_hash(&pkey);
[4c3c4a5]139}
140
[5e801dc]141static bool pos_key_equal(const void *key, const ht_link_t *item)
[4c3c4a5]142{
[5e801dc]143 const pos_key_t *pos = key;
[062d900]144 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
[a35b458]145
[18b6a88]146 return pos->service_id == fidx->service_id &&
147 pos->pdi == fidx->pdi &&
148 pos->pfc == fidx->pfc;
[4c3c4a5]149}
150
[61eb2ce2]151static const hash_table_ops_t uph_ops = {
[4c3c4a5]152 .hash = pos_hash,
[062d900]153 .key_hash = pos_key_hash,
154 .key_equal = pos_key_equal,
[4e00f87]155 .equal = NULL,
156 .remove_callback = NULL,
[4c3c4a5]157};
158
159/**
160 * Global hash table of all used fat_idx_t structures.
[375ab5e]161 * The index structures are hashed by the service_id and index.
[4c3c4a5]162 */
163static hash_table_t ui_hash;
164
[062d900]165typedef struct {
166 service_id_t service_id;
167 fs_index_t index;
168} idx_key_t;
[4c3c4a5]169
[5e801dc]170static size_t idx_key_hash(const void *key_arg)
[4c3c4a5]171{
[5e801dc]172 const idx_key_t *key = key_arg;
[062d900]173 return hash_combine(key->service_id, key->index);
[4c3c4a5]174}
175
[062d900]176static size_t idx_hash(const ht_link_t *item)
[4c3c4a5]177{
[062d900]178 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
179 return hash_combine(fidx->service_id, fidx->index);
180}
[4c3c4a5]181
[5e801dc]182static bool idx_key_equal(const void *key_arg, const ht_link_t *item)
[062d900]183{
184 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
[5e801dc]185 const idx_key_t *key = key_arg;
[a35b458]186
[062d900]187 return key->index == fidx->index && key->service_id == fidx->service_id;
[4c3c4a5]188}
189
[062d900]190static void idx_remove_callback(ht_link_t *item)
[4c3c4a5]191{
[062d900]192 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
[4c3c4a5]193
194 free(fidx);
195}
196
[61eb2ce2]197static const hash_table_ops_t uih_ops = {
[4c3c4a5]198 .hash = idx_hash,
[062d900]199 .key_hash = idx_key_hash,
200 .key_equal = idx_key_equal,
[4e00f87]201 .equal = NULL,
[4c3c4a5]202 .remove_callback = idx_remove_callback,
203};
204
205/** Allocate a VFS index which is not currently in use. */
[375ab5e]206static bool exfat_index_alloc(service_id_t service_id, fs_index_t *index)
[4c3c4a5]207{
208 unused_t *u;
[a35b458]209
[4c3c4a5]210 assert(index);
[375ab5e]211 u = unused_find(service_id, true);
[4c3c4a5]212 if (!u)
[1b20da0]213 return false;
[4c3c4a5]214
[9cc1f43]215 if (list_empty(&u->freed_list)) {
[1b20da0]216 if (u->remaining) {
[4c3c4a5]217 /*
218 * There are no freed indices, allocate one directly
219 * from the counter.
220 */
221 *index = u->next++;
222 --u->remaining;
223 fibril_mutex_unlock(&unused_lock);
224 return true;
225 }
226 } else {
227 /* There are some freed indices which we can reuse. */
[9cc1f43]228 freed_t *f = list_get_instance(list_first(&u->freed_list),
229 freed_t, link);
[4c3c4a5]230 *index = f->first;
231 if (f->first++ == f->last) {
232 /* Destroy the interval. */
233 list_remove(&f->link);
234 free(f);
235 }
236 fibril_mutex_unlock(&unused_lock);
237 return true;
238 }
239 /*
240 * We ran out of indices, which is extremely unlikely with FAT16, but
241 * theoretically still possible (e.g. too many open unlinked nodes or
242 * too many zero-sized nodes).
243 */
244 fibril_mutex_unlock(&unused_lock);
245 return false;
246}
247
248/** If possible, coalesce two intervals of freed indices. */
249static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
250{
251 freed_t *fl = list_get_instance(l, freed_t, link);
252 freed_t *fr = list_get_instance(r, freed_t, link);
253
254 if (fl->last + 1 == fr->first) {
255 if (cur == l) {
256 fl->last = fr->last;
257 list_remove(r);
258 free(r);
259 } else {
260 fr->first = fl->first;
261 list_remove(l);
262 free(l);
263 }
264 }
265}
266
267/** Free a VFS index, which is no longer in use. */
[375ab5e]268static void exfat_index_free(service_id_t service_id, fs_index_t index)
[4c3c4a5]269{
270 unused_t *u;
271
[375ab5e]272 u = unused_find(service_id, true);
[4c3c4a5]273 assert(u);
274
275 if (u->next == index + 1) {
276 /* The index can be returned directly to the counter. */
277 u->next--;
278 u->remaining++;
279 } else {
280 /*
281 * The index must be returned either to an existing freed
282 * interval or a new interval must be created.
283 */
284 link_t *lnk;
285 freed_t *n;
[9cc1f43]286 for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
[4c3c4a5]287 lnk = lnk->next) {
288 freed_t *f = list_get_instance(lnk, freed_t, link);
289 if (f->first == index + 1) {
290 f->first--;
[9cc1f43]291 if (lnk->prev != &u->freed_list.head)
[4c3c4a5]292 try_coalesce_intervals(lnk->prev, lnk,
293 lnk);
294 fibril_mutex_unlock(&unused_lock);
295 return;
296 }
297 if (f->last == index - 1) {
298 f->last++;
[9cc1f43]299 if (lnk->next != &u->freed_list.head)
[4c3c4a5]300 try_coalesce_intervals(lnk, lnk->next,
301 lnk);
302 fibril_mutex_unlock(&unused_lock);
303 return;
304 }
305 if (index > f->first) {
306 n = malloc(sizeof(freed_t));
307 /* TODO: sleep until allocation succeeds */
308 assert(n);
309 link_initialize(&n->link);
310 n->first = index;
311 n->last = index;
312 list_insert_before(&n->link, lnk);
313 fibril_mutex_unlock(&unused_lock);
314 return;
315 }
316
317 }
318 /* The index will form the last interval. */
319 n = malloc(sizeof(freed_t));
320 /* TODO: sleep until allocation succeeds */
321 assert(n);
322 link_initialize(&n->link);
323 n->first = index;
324 n->last = index;
[9cc1f43]325 list_append(&n->link, &u->freed_list);
[4c3c4a5]326 }
327 fibril_mutex_unlock(&unused_lock);
328}
329
[b7fd2a0]330static errno_t exfat_idx_create(exfat_idx_t **fidxp, service_id_t service_id)
[4c3c4a5]331{
332 exfat_idx_t *fidx;
333
334 fidx = (exfat_idx_t *) malloc(sizeof(exfat_idx_t));
[1b20da0]335 if (!fidx)
[4c3c4a5]336 return ENOMEM;
[375ab5e]337 if (!exfat_index_alloc(service_id, &fidx->index)) {
[4c3c4a5]338 free(fidx);
339 return ENOSPC;
340 }
[a35b458]341
[4c3c4a5]342 fibril_mutex_initialize(&fidx->lock);
[375ab5e]343 fidx->service_id = service_id;
[4c3c4a5]344 fidx->pfc = 0; /* no parent yet */
345 fidx->pdi = 0;
346 fidx->nodep = NULL;
347
348 *fidxp = fidx;
349 return EOK;
350}
351
[b7fd2a0]352errno_t exfat_idx_get_new(exfat_idx_t **fidxp, service_id_t service_id)
[4c3c4a5]353{
354 exfat_idx_t *fidx;
[b7fd2a0]355 errno_t rc;
[4c3c4a5]356
357 fibril_mutex_lock(&used_lock);
[375ab5e]358 rc = exfat_idx_create(&fidx, service_id);
[4c3c4a5]359 if (rc != EOK) {
360 fibril_mutex_unlock(&used_lock);
361 return rc;
362 }
[a35b458]363
[062d900]364 hash_table_insert(&ui_hash, &fidx->uih_link);
[4c3c4a5]365 fibril_mutex_lock(&fidx->lock);
366 fibril_mutex_unlock(&used_lock);
367
368 *fidxp = fidx;
369 return EOK;
370}
371
372exfat_idx_t *
[375ab5e]373exfat_idx_get_by_pos(service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
[4c3c4a5]374{
375 exfat_idx_t *fidx;
[a35b458]376
[062d900]377 pos_key_t pos_key = {
378 .service_id = service_id,
379 .pfc = pfc,
380 .pdi = pdi,
[4c3c4a5]381 };
382
383 fibril_mutex_lock(&used_lock);
[062d900]384 ht_link_t *l = hash_table_find(&up_hash, &pos_key);
[4c3c4a5]385 if (l) {
[062d900]386 fidx = hash_table_get_inst(l, exfat_idx_t, uph_link);
[4c3c4a5]387 } else {
[b7fd2a0]388 errno_t rc;
[4c3c4a5]389
[375ab5e]390 rc = exfat_idx_create(&fidx, service_id);
[4c3c4a5]391 if (rc != EOK) {
392 fibril_mutex_unlock(&used_lock);
393 return NULL;
394 }
[a35b458]395
[4c3c4a5]396 fidx->pfc = pfc;
397 fidx->pdi = pdi;
398
[062d900]399 hash_table_insert(&up_hash, &fidx->uph_link);
400 hash_table_insert(&ui_hash, &fidx->uih_link);
[4c3c4a5]401 }
402 fibril_mutex_lock(&fidx->lock);
403 fibril_mutex_unlock(&used_lock);
404
405 return fidx;
406}
407
408void exfat_idx_hashin(exfat_idx_t *idx)
409{
410 fibril_mutex_lock(&used_lock);
[062d900]411 hash_table_insert(&up_hash, &idx->uph_link);
[4c3c4a5]412 fibril_mutex_unlock(&used_lock);
413}
414
415void exfat_idx_hashout(exfat_idx_t *idx)
416{
417 fibril_mutex_lock(&used_lock);
[062d900]418 hash_table_remove_item(&up_hash, &idx->uph_link);
[4c3c4a5]419 fibril_mutex_unlock(&used_lock);
420}
421
422exfat_idx_t *
[375ab5e]423exfat_idx_get_by_index(service_id_t service_id, fs_index_t index)
[4c3c4a5]424{
425 exfat_idx_t *fidx = NULL;
[062d900]426
427 idx_key_t idx_key = {
428 .service_id = service_id,
429 .index = index,
[4c3c4a5]430 };
431
432 fibril_mutex_lock(&used_lock);
[062d900]433 ht_link_t *l = hash_table_find(&ui_hash, &idx_key);
[4c3c4a5]434 if (l) {
[062d900]435 fidx = hash_table_get_inst(l, exfat_idx_t, uih_link);
[4c3c4a5]436 fibril_mutex_lock(&fidx->lock);
437 }
438 fibril_mutex_unlock(&used_lock);
439
440 return fidx;
441}
442
443/** Destroy the index structure.
444 *
445 * @param idx The index structure to be destroyed.
446 */
447void exfat_idx_destroy(exfat_idx_t *idx)
448{
[062d900]449 idx_key_t idx_key = {
450 .service_id = idx->service_id,
451 .index = idx->index,
[4c3c4a5]452 };
453
454 /* TODO: assert(idx->pfc == FAT_CLST_RES0); */
455 assert(idx->pfc == 0);
456
457 fibril_mutex_lock(&used_lock);
458 /*
459 * Since we can only free unlinked nodes, the index structure is not
460 * present in the position hash (uph). We therefore hash it out from
461 * the index hash only.
462 */
[062d900]463 hash_table_remove(&ui_hash, &idx_key);
[4c3c4a5]464 fibril_mutex_unlock(&used_lock);
465 /* Release the VFS index. */
[062d900]466 exfat_index_free(idx_key.service_id, idx_key.index);
[4c3c4a5]467 /* The index structure itself is freed in idx_remove_callback(). */
468}
469
[b7fd2a0]470errno_t exfat_idx_init(void)
[4c3c4a5]471{
[1b20da0]472 if (!hash_table_create(&up_hash, 0, 0, &uph_ops))
[4c3c4a5]473 return ENOMEM;
[062d900]474 if (!hash_table_create(&ui_hash, 0, 0, &uih_ops)) {
[4c3c4a5]475 hash_table_destroy(&up_hash);
476 return ENOMEM;
477 }
478 return EOK;
479}
480
481void exfat_idx_fini(void)
482{
483 /* We assume the hash tables are empty. */
[062d900]484 assert(hash_table_empty(&up_hash) && hash_table_empty(&ui_hash));
[4c3c4a5]485 hash_table_destroy(&up_hash);
486 hash_table_destroy(&ui_hash);
487}
488
[b7fd2a0]489errno_t exfat_idx_init_by_service_id(service_id_t service_id)
[4c3c4a5]490{
491 unused_t *u;
[b7fd2a0]492 errno_t rc = EOK;
[4c3c4a5]493
494 u = (unused_t *) malloc(sizeof(unused_t));
495 if (!u)
496 return ENOMEM;
[375ab5e]497 unused_initialize(u, service_id);
[4c3c4a5]498 fibril_mutex_lock(&unused_lock);
[375ab5e]499 if (!unused_find(service_id, false)) {
[9cc1f43]500 list_append(&u->link, &unused_list);
[4c3c4a5]501 } else {
502 free(u);
503 rc = EEXIST;
504 }
505 fibril_mutex_unlock(&unused_lock);
506 return rc;
507}
508
[062d900]509static bool rm_pos_service_id(ht_link_t *item, void *arg)
[4c3c4a5]510{
[18b6a88]511 service_id_t service_id = *(service_id_t *)arg;
[062d900]512 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
513
514 if (fidx->service_id == service_id) {
515 hash_table_remove_item(&up_hash, item);
516 }
[a35b458]517
[062d900]518 return true;
519}
520
521static bool rm_idx_service_id(ht_link_t *item, void *arg)
522{
[18b6a88]523 service_id_t service_id = *(service_id_t *)arg;
[062d900]524 exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
[4c3c4a5]525
[062d900]526 if (fidx->service_id == service_id) {
527 hash_table_remove_item(&ui_hash, item);
528 }
[a35b458]529
[062d900]530 return true;
531}
532
533void exfat_idx_fini_by_service_id(service_id_t service_id)
534{
[4c3c4a5]535 /*
536 * Remove this instance's index structure from up_hash and ui_hash.
537 * Process up_hash first and ui_hash second because the index structure
[1b20da0]538 * is actually removed in idx_remove_callback().
[4c3c4a5]539 */
540 fibril_mutex_lock(&used_lock);
[062d900]541 hash_table_apply(&up_hash, rm_pos_service_id, &service_id);
542 hash_table_apply(&ui_hash, rm_idx_service_id, &service_id);
[4c3c4a5]543 fibril_mutex_unlock(&used_lock);
544
545 /*
546 * Free the unused and freed structures for this instance.
547 */
[375ab5e]548 unused_t *u = unused_find(service_id, true);
[4c3c4a5]549 assert(u);
550 list_remove(&u->link);
551 fibril_mutex_unlock(&unused_lock);
552
[9cc1f43]553 while (!list_empty(&u->freed_list)) {
[4c3c4a5]554 freed_t *f;
[9cc1f43]555 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
[4c3c4a5]556 list_remove(&f->link);
557 free(f);
558 }
[1b20da0]559 free(u);
[4c3c4a5]560}
561
562/**
563 * @}
[1b20da0]564 */
Note: See TracBrowser for help on using the repository browser.