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