source: mainline/uspace/srv/fs/fat/fat_idx.c

Last change on this file was 0db0df2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Hash table improvements

Implement hash_table_foreach macro, analogous to list_foreach.

Remove superfluous argument to hash_table_find_next().
(If the user needs to recheck the part of the list already
checked by hash_table_find(), they can just rerun that function.)

Add hash argument to hash_table_ops_t::key_equal.
The big change here is that users with big keys can store the hash
value alongside key in their entries, and for the low low cost of
sizeof(size_t) bytes eliminate a bunch of expensive key comparisons.

Also added a hash function for strings and arbitrary data.
Found this one by asking ChatGPT, because the latency of accesses
to my book collection is currently a couple of hours.

+ Some drive-by unused #include removal.

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[297f1197]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 fat
[297f1197]30 * @{
[1b20da0]31 */
[297f1197]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 <errno.h>
[19f857a]40#include <str.h>
[d9c8c81]41#include <adt/hash_table.h>
[062d900]42#include <adt/hash.h>
[d9c8c81]43#include <adt/list.h>
[297f1197]44#include <assert.h>
[1e4cada]45#include <fibril_synch.h>
[38d150e]46#include <stdlib.h>
[297f1197]47
[5a324099]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 {
[062d900]60 link_t link;
61 service_id_t service_id;
[5a324099]62
63 /** Next unassigned index. */
[a9d85df]64 fs_index_t next;
[5a324099]65 /** Number of remaining unassigned indices. */
[a9d85df]66 uint64_t remaining;
[5a324099]67
68 /** Sorted list of intervals of freed indices. */
[a9d85df]69 list_t freed_list;
[5a324099]70} unused_t;
71
[6ebe721]72/** Mutex protecting the list of unused structures. */
73static FIBRIL_MUTEX_INITIALIZE(unused_lock);
[4452366]74
75/** List of unused structures. */
[b72efe8]76static LIST_INITIALIZE(unused_list);
[5a324099]77
[15f3c3f]78static void unused_initialize(unused_t *u, service_id_t service_id)
[cde485d]79{
80 link_initialize(&u->link);
[15f3c3f]81 u->service_id = service_id;
[cde485d]82 u->next = 0;
83 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
[b72efe8]84 list_initialize(&u->freed_list);
[cde485d]85}
86
[15f3c3f]87static unused_t *unused_find(service_id_t service_id, bool lock)
[b7b6753]88{
89 if (lock)
[6ebe721]90 fibril_mutex_lock(&unused_lock);
[b72efe8]91
[feeac0d]92 list_foreach(unused_list, link, unused_t, u) {
[1b20da0]93 if (u->service_id == service_id)
[b7b6753]94 return u;
95 }
[062d900]96
[b7b6753]97 if (lock)
[6ebe721]98 fibril_mutex_unlock(&unused_lock);
[b7b6753]99 return NULL;
100}
101
[6ebe721]102/** Mutex protecting the up_hash and ui_hash. */
103static FIBRIL_MUTEX_INITIALIZE(used_lock);
[78a1b7b]104
[4797132]105/**
106 * Global hash table of all used fat_idx_t structures.
[15f3c3f]107 * The index structures are hashed by the service_id, parent node's first
[4797132]108 * cluster and index within the parent directory.
[1b20da0]109 */
[4797132]110static hash_table_t up_hash;
[9a5ccfb3]111
[062d900]112typedef struct {
113 service_id_t service_id;
114 fat_cluster_t pfc;
115 unsigned pdi;
116} pos_key_t;
[9a5ccfb3]117
[5e801dc]118static inline size_t pos_key_hash(const void *key)
[9a5ccfb3]119{
[5e801dc]120 const pos_key_t *pos = key;
[a35b458]121
[062d900]122 size_t hash = 0;
123 hash = hash_combine(pos->pfc, pos->pdi);
124 return hash_combine(hash, pos->service_id);
[9a5ccfb3]125}
126
[062d900]127static size_t pos_hash(const ht_link_t *item)
[9a5ccfb3]128{
[062d900]129 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
[a35b458]130
[062d900]131 pos_key_t pkey = {
132 .service_id = fidx->service_id,
133 .pfc = fidx->pfc,
134 .pdi = fidx->pdi,
135 };
[a35b458]136
[062d900]137 return pos_key_hash(&pkey);
[9a5ccfb3]138}
139
[0db0df2]140static bool pos_key_equal(const void *key, size_t hash, const ht_link_t *item)
[4797132]141{
[5e801dc]142 const pos_key_t *pos = key;
[062d900]143 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
[a35b458]144
[18b6a88]145 return pos->service_id == fidx->service_id &&
146 pos->pdi == fidx->pdi &&
147 pos->pfc == fidx->pfc;
[4797132]148}
149
[61eb2ce2]150static const hash_table_ops_t uph_ops = {
[4797132]151 .hash = pos_hash,
[062d900]152 .key_hash = pos_key_hash,
153 .key_equal = pos_key_equal,
[4e00f87]154 .equal = NULL,
155 .remove_callback = NULL,
[4797132]156};
157
158/**
159 * Global hash table of all used fat_idx_t structures.
[15f3c3f]160 * The index structures are hashed by the service_id and index.
[4797132]161 */
162static hash_table_t ui_hash;
163
[062d900]164typedef struct {
165 service_id_t service_id;
166 fs_index_t index;
167} idx_key_t;
[4797132]168
[5e801dc]169static size_t idx_key_hash(const void *key_arg)
[4797132]170{
[5e801dc]171 const idx_key_t *key = key_arg;
[062d900]172 return hash_combine(key->service_id, key->index);
[4797132]173}
174
[062d900]175static size_t idx_hash(const ht_link_t *item)
[4797132]176{
[062d900]177 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
178 return hash_combine(fidx->service_id, fidx->index);
179}
[393bef1]180
[0db0df2]181static bool idx_key_equal(const void *key_arg, size_t hash,
182 const ht_link_t *item)
[062d900]183{
184 fat_idx_t *fidx = hash_table_get_inst(item, fat_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;
[4797132]188}
189
[062d900]190static void idx_remove_callback(ht_link_t *item)
[9a5ccfb3]191{
[062d900]192 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
[430de97]193
194 free(fidx);
[9a5ccfb3]195}
196
[61eb2ce2]197static const hash_table_ops_t uih_ops = {
[9a5ccfb3]198 .hash = idx_hash,
[062d900]199 .key_hash = idx_key_hash,
200 .key_equal = idx_key_equal,
[4e00f87]201 .equal = NULL,
[9a5ccfb3]202 .remove_callback = idx_remove_callback,
203};
204
[5a324099]205/** Allocate a VFS index which is not currently in use. */
[15f3c3f]206static bool fat_index_alloc(service_id_t service_id, fs_index_t *index)
[5a324099]207{
208 unused_t *u;
[a35b458]209
[5a324099]210 assert(index);
[15f3c3f]211 u = unused_find(service_id, true);
[b7b6753]212 if (!u)
[1b20da0]213 return false;
[5a324099]214
[b72efe8]215 if (list_empty(&u->freed_list)) {
[1b20da0]216 if (u->remaining) {
[5a324099]217 /*
218 * There are no freed indices, allocate one directly
219 * from the counter.
220 */
221 *index = u->next++;
222 --u->remaining;
[6ebe721]223 fibril_mutex_unlock(&unused_lock);
[5a324099]224 return true;
225 }
226 } else {
227 /* There are some freed indices which we can reuse. */
[b72efe8]228 freed_t *f = list_get_instance(list_first(&u->freed_list),
229 freed_t, link);
[5a324099]230 *index = f->first;
231 if (f->first++ == f->last) {
232 /* Destroy the interval. */
233 list_remove(&f->link);
234 free(f);
235 }
[6ebe721]236 fibril_mutex_unlock(&unused_lock);
[5a324099]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 */
[6ebe721]244 fibril_mutex_unlock(&unused_lock);
[5a324099]245 return false;
246}
247
[0c1ad7ac]248/** If possible, coalesce two intervals of freed indices. */
249static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
[5a324099]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. */
[15f3c3f]268static void fat_index_free(service_id_t service_id, fs_index_t index)
[5a324099]269{
270 unused_t *u;
271
[15f3c3f]272 u = unused_find(service_id, true);
[b7b6753]273 assert(u);
[5a324099]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;
[b72efe8]286 for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
[5a324099]287 lnk = lnk->next) {
288 freed_t *f = list_get_instance(lnk, freed_t, link);
289 if (f->first == index + 1) {
290 f->first--;
[b72efe8]291 if (lnk->prev != &u->freed_list.head)
[0c1ad7ac]292 try_coalesce_intervals(lnk->prev, lnk,
[5a324099]293 lnk);
[6ebe721]294 fibril_mutex_unlock(&unused_lock);
[5a324099]295 return;
296 }
297 if (f->last == index - 1) {
298 f->last++;
[b72efe8]299 if (lnk->next != &u->freed_list.head)
[0c1ad7ac]300 try_coalesce_intervals(lnk, lnk->next,
[5a324099]301 lnk);
[6ebe721]302 fibril_mutex_unlock(&unused_lock);
[5a324099]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);
[6ebe721]313 fibril_mutex_unlock(&unused_lock);
[5a324099]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;
[b72efe8]325 list_append(&n->link, &u->freed_list);
[5a324099]326 }
[6ebe721]327 fibril_mutex_unlock(&unused_lock);
[5a324099]328}
329
[b7fd2a0]330static errno_t fat_idx_create(fat_idx_t **fidxp, service_id_t service_id)
[9a3d5f0]331{
332 fat_idx_t *fidx;
333
334 fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
[1b20da0]335 if (!fidx)
[9a15176]336 return ENOMEM;
[15f3c3f]337 if (!fat_index_alloc(service_id, &fidx->index)) {
[9a3d5f0]338 free(fidx);
[9a15176]339 return ENOSPC;
[9a3d5f0]340 }
[a35b458]341
[6ebe721]342 fibril_mutex_initialize(&fidx->lock);
[15f3c3f]343 fidx->service_id = service_id;
[9a3d5f0]344 fidx->pfc = FAT_CLST_RES0; /* no parent yet */
345 fidx->pdi = 0;
346 fidx->nodep = NULL;
347
[9a15176]348 *fidxp = fidx;
349 return EOK;
[9a3d5f0]350}
351
[b7fd2a0]352errno_t fat_idx_get_new(fat_idx_t **fidxp, service_id_t service_id)
[9a3d5f0]353{
354 fat_idx_t *fidx;
[b7fd2a0]355 errno_t rc;
[9a3d5f0]356
[6ebe721]357 fibril_mutex_lock(&used_lock);
[15f3c3f]358 rc = fat_idx_create(&fidx, service_id);
[9a15176]359 if (rc != EOK) {
[6ebe721]360 fibril_mutex_unlock(&used_lock);
[9a15176]361 return rc;
[9a3d5f0]362 }
[a35b458]363
[062d900]364 hash_table_insert(&ui_hash, &fidx->uih_link);
[6ebe721]365 fibril_mutex_lock(&fidx->lock);
366 fibril_mutex_unlock(&used_lock);
[9a3d5f0]367
[9a15176]368 *fidxp = fidx;
369 return EOK;
[9a3d5f0]370}
371
[4797132]372fat_idx_t *
[15f3c3f]373fat_idx_get_by_pos(service_id_t service_id, fat_cluster_t pfc, unsigned pdi)
[297f1197]374{
[9a5ccfb3]375 fat_idx_t *fidx;
[062d900]376
377 pos_key_t pos_key = {
378 .service_id = service_id,
379 .pfc = pfc,
380 .pdi = pdi,
[9a5ccfb3]381 };
382
[6ebe721]383 fibril_mutex_lock(&used_lock);
[062d900]384 ht_link_t *l = hash_table_find(&up_hash, &pos_key);
[9a5ccfb3]385 if (l) {
[062d900]386 fidx = hash_table_get_inst(l, fat_idx_t, uph_link);
[9a5ccfb3]387 } else {
[b7fd2a0]388 errno_t rc;
[9a15176]389
[15f3c3f]390 rc = fat_idx_create(&fidx, service_id);
[9a15176]391 if (rc != EOK) {
[6ebe721]392 fibril_mutex_unlock(&used_lock);
[9a5ccfb3]393 return NULL;
394 }
[a35b458]395
[9a5ccfb3]396 fidx->pfc = pfc;
397 fidx->pdi = pdi;
[4797132]398
[062d900]399 hash_table_insert(&up_hash, &fidx->uph_link);
400 hash_table_insert(&ui_hash, &fidx->uih_link);
[4797132]401 }
[6ebe721]402 fibril_mutex_lock(&fidx->lock);
403 fibril_mutex_unlock(&used_lock);
[4797132]404
405 return fidx;
406}
407
[0fdd6bb]408void fat_idx_hashin(fat_idx_t *idx)
409{
[6ebe721]410 fibril_mutex_lock(&used_lock);
[062d900]411 hash_table_insert(&up_hash, &idx->uph_link);
[6ebe721]412 fibril_mutex_unlock(&used_lock);
[0fdd6bb]413}
414
[a31c1ccf]415void fat_idx_hashout(fat_idx_t *idx)
416{
[6ebe721]417 fibril_mutex_lock(&used_lock);
[062d900]418 hash_table_remove_item(&up_hash, &idx->uph_link);
[6ebe721]419 fibril_mutex_unlock(&used_lock);
[a31c1ccf]420}
421
[4797132]422fat_idx_t *
[15f3c3f]423fat_idx_get_by_index(service_id_t service_id, fs_index_t index)
[4797132]424{
425 fat_idx_t *fidx = NULL;
[062d900]426
427 idx_key_t idx_key = {
428 .service_id = service_id,
429 .index = index,
[4797132]430 };
431
[6ebe721]432 fibril_mutex_lock(&used_lock);
[062d900]433 ht_link_t *l = hash_table_find(&ui_hash, &idx_key);
[4797132]434 if (l) {
[062d900]435 fidx = hash_table_get_inst(l, fat_idx_t, uih_link);
[ca093b3]436 fibril_mutex_lock(&fidx->lock);
[9a5ccfb3]437 }
[6ebe721]438 fibril_mutex_unlock(&used_lock);
[9a5ccfb3]439
440 return fidx;
[297f1197]441}
[9a5ccfb3]442
[48eb7a14]443/** Destroy the index structure.
444 *
445 * @param idx The index structure to be destroyed.
446 */
447void fat_idx_destroy(fat_idx_t *idx)
448{
[062d900]449 idx_key_t idx_key = {
450 .service_id = idx->service_id,
451 .index = idx->index,
[48eb7a14]452 };
453
454 assert(idx->pfc == FAT_CLST_RES0);
455
[6ebe721]456 fibril_mutex_lock(&used_lock);
[48eb7a14]457 /*
458 * Since we can only free unlinked nodes, the index structure is not
459 * present in the position hash (uph). We therefore hash it out from
460 * the index hash only.
461 */
[062d900]462 hash_table_remove(&ui_hash, &idx_key);
[6ebe721]463 fibril_mutex_unlock(&used_lock);
[48eb7a14]464 /* Release the VFS index. */
[062d900]465 fat_index_free(idx_key.service_id, idx_key.index);
[430de97]466 /* The index structure itself is freed in idx_remove_callback(). */
[48eb7a14]467}
468
[b7fd2a0]469errno_t fat_idx_init(void)
[cde485d]470{
[1b20da0]471 if (!hash_table_create(&up_hash, 0, 0, &uph_ops))
[cde485d]472 return ENOMEM;
[062d900]473 if (!hash_table_create(&ui_hash, 0, 0, &uih_ops)) {
[cde485d]474 hash_table_destroy(&up_hash);
475 return ENOMEM;
476 }
477 return EOK;
478}
479
480void fat_idx_fini(void)
481{
482 /* We assume the hash tables are empty. */
[062d900]483 assert(hash_table_empty(&up_hash) && hash_table_empty(&ui_hash));
[cde485d]484 hash_table_destroy(&up_hash);
485 hash_table_destroy(&ui_hash);
486}
487
[b7fd2a0]488errno_t fat_idx_init_by_service_id(service_id_t service_id)
[cde485d]489{
[b7b6753]490 unused_t *u;
[b7fd2a0]491 errno_t rc = EOK;
[b7b6753]492
493 u = (unused_t *) malloc(sizeof(unused_t));
[cde485d]494 if (!u)
495 return ENOMEM;
[15f3c3f]496 unused_initialize(u, service_id);
[6ebe721]497 fibril_mutex_lock(&unused_lock);
[15f3c3f]498 if (!unused_find(service_id, false)) {
[b72efe8]499 list_append(&u->link, &unused_list);
[593585df]500 } else {
501 free(u);
[b7b6753]502 rc = EEXIST;
[593585df]503 }
[6ebe721]504 fibril_mutex_unlock(&unused_lock);
[b7b6753]505 return rc;
[cde485d]506}
507
[062d900]508static bool rm_pos_service_id(ht_link_t *item, void *arg)
[cde485d]509{
[18b6a88]510 service_id_t service_id = *(service_id_t *)arg;
[062d900]511 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
512
513 if (fidx->service_id == service_id) {
514 hash_table_remove_item(&up_hash, item);
515 }
[a35b458]516
[062d900]517 return true;
518}
519
520static bool rm_idx_service_id(ht_link_t *item, void *arg)
521{
[18b6a88]522 service_id_t service_id = *(service_id_t *)arg;
[062d900]523 fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
[cde485d]524
[062d900]525 if (fidx->service_id == service_id) {
526 hash_table_remove_item(&ui_hash, item);
527 }
[a35b458]528
[062d900]529 return true;
530}
531
532void fat_idx_fini_by_service_id(service_id_t service_id)
533{
[430de97]534 /*
535 * Remove this instance's index structure from up_hash and ui_hash.
536 * Process up_hash first and ui_hash second because the index structure
[1b20da0]537 * is actually removed in idx_remove_callback().
[430de97]538 */
539 fibril_mutex_lock(&used_lock);
[062d900]540 hash_table_apply(&up_hash, rm_pos_service_id, &service_id);
541 hash_table_apply(&ui_hash, rm_idx_service_id, &service_id);
[430de97]542 fibril_mutex_unlock(&used_lock);
543
544 /*
545 * Free the unused and freed structures for this instance.
546 */
[15f3c3f]547 unused_t *u = unused_find(service_id, true);
[b7b6753]548 assert(u);
[cde485d]549 list_remove(&u->link);
[6ebe721]550 fibril_mutex_unlock(&unused_lock);
[cde485d]551
[b72efe8]552 while (!list_empty(&u->freed_list)) {
[cde485d]553 freed_t *f;
[b72efe8]554 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
[cde485d]555 list_remove(&f->link);
556 free(f);
557 }
[1b20da0]558 free(u);
[cde485d]559}
560
561/**
562 * @}
[1b20da0]563 */
Note: See TracBrowser for help on using the repository browser.