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