[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 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file fat_idx.c
|
---|
| 35 | * @brief Layer for translating FAT entities to VFS node indices.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include "fat.h"
|
---|
[5a324099] | 39 | #include "../../vfs/vfs.h"
|
---|
[297f1197] | 40 | #include <errno.h>
|
---|
| 41 | #include <string.h>
|
---|
[d9c8c81] | 42 | #include <adt/hash_table.h>
|
---|
| 43 | #include <adt/list.h>
|
---|
[297f1197] | 44 | #include <assert.h>
|
---|
[1e4cada] | 45 | #include <fibril_synch.h>
|
---|
[297f1197] | 46 |
|
---|
[5a324099] | 47 | /** Each instance of this type describes one interval of freed VFS indices. */
|
---|
| 48 | typedef struct {
|
---|
| 49 | link_t link;
|
---|
| 50 | fs_index_t first;
|
---|
| 51 | fs_index_t last;
|
---|
| 52 | } freed_t;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Each instance of this type describes state of all VFS indices that
|
---|
| 56 | * are currently unused.
|
---|
| 57 | */
|
---|
| 58 | typedef struct {
|
---|
| 59 | link_t link;
|
---|
| 60 | dev_handle_t dev_handle;
|
---|
| 61 |
|
---|
| 62 | /** Next unassigned index. */
|
---|
| 63 | fs_index_t next;
|
---|
| 64 | /** Number of remaining unassigned indices. */
|
---|
| 65 | uint64_t remaining;
|
---|
| 66 |
|
---|
| 67 | /** Sorted list of intervals of freed indices. */
|
---|
| 68 | link_t freed_head;
|
---|
| 69 | } unused_t;
|
---|
| 70 |
|
---|
[6ebe721] | 71 | /** Mutex protecting the list of unused structures. */
|
---|
| 72 | static FIBRIL_MUTEX_INITIALIZE(unused_lock);
|
---|
[4452366] | 73 |
|
---|
| 74 | /** List of unused structures. */
|
---|
[5a324099] | 75 | static LIST_INITIALIZE(unused_head);
|
---|
| 76 |
|
---|
[cde485d] | 77 | static void unused_initialize(unused_t *u, dev_handle_t dev_handle)
|
---|
| 78 | {
|
---|
| 79 | link_initialize(&u->link);
|
---|
| 80 | u->dev_handle = dev_handle;
|
---|
| 81 | u->next = 0;
|
---|
| 82 | u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
|
---|
| 83 | list_initialize(&u->freed_head);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[b7b6753] | 86 | static unused_t *unused_find(dev_handle_t dev_handle, bool lock)
|
---|
| 87 | {
|
---|
| 88 | unused_t *u;
|
---|
| 89 | link_t *l;
|
---|
| 90 |
|
---|
| 91 | if (lock)
|
---|
[6ebe721] | 92 | fibril_mutex_lock(&unused_lock);
|
---|
[b7b6753] | 93 | for (l = unused_head.next; l != &unused_head; l = l->next) {
|
---|
| 94 | u = list_get_instance(l, unused_t, link);
|
---|
| 95 | if (u->dev_handle == dev_handle)
|
---|
| 96 | return u;
|
---|
| 97 | }
|
---|
| 98 | if (lock)
|
---|
[6ebe721] | 99 | fibril_mutex_unlock(&unused_lock);
|
---|
[b7b6753] | 100 | return NULL;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[6ebe721] | 103 | /** Mutex protecting the up_hash and ui_hash. */
|
---|
| 104 | static FIBRIL_MUTEX_INITIALIZE(used_lock);
|
---|
[78a1b7b] | 105 |
|
---|
[4797132] | 106 | /**
|
---|
| 107 | * Global hash table of all used fat_idx_t structures.
|
---|
| 108 | * The index structures are hashed by the dev_handle, parent node's first
|
---|
| 109 | * cluster and index within the parent directory.
|
---|
| 110 | */
|
---|
| 111 | static hash_table_t up_hash;
|
---|
[9a5ccfb3] | 112 |
|
---|
[4797132] | 113 | #define UPH_BUCKETS_LOG 12
|
---|
| 114 | #define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
|
---|
[9a5ccfb3] | 115 |
|
---|
[4797132] | 116 | #define UPH_DH_KEY 0
|
---|
| 117 | #define UPH_PFC_KEY 1
|
---|
| 118 | #define UPH_PDI_KEY 2
|
---|
[9a5ccfb3] | 119 |
|
---|
[4797132] | 120 | static hash_index_t pos_hash(unsigned long key[])
|
---|
[9a5ccfb3] | 121 | {
|
---|
[4797132] | 122 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
|
---|
| 123 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
|
---|
| 124 | unsigned pdi = (unsigned)key[UPH_PDI_KEY];
|
---|
[9a5ccfb3] | 125 |
|
---|
| 126 | hash_index_t h;
|
---|
| 127 |
|
---|
| 128 | /*
|
---|
| 129 | * The least significant half of all bits are the least significant bits
|
---|
| 130 | * of the parent node's first cluster.
|
---|
| 131 | *
|
---|
| 132 | * The least significant half of the most significant half of all bits
|
---|
| 133 | * are the least significant bits of the node's dentry index within the
|
---|
| 134 | * parent directory node.
|
---|
| 135 | *
|
---|
| 136 | * The most significant half of the most significant half of all bits
|
---|
| 137 | * are the least significant bits of the device handle.
|
---|
| 138 | */
|
---|
[4797132] | 139 | h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1);
|
---|
| 140 | h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
|
---|
| 141 | (UPH_BUCKETS_LOG / 2);
|
---|
| 142 | h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
|
---|
| 143 | (3 * (UPH_BUCKETS_LOG / 4));
|
---|
[9a5ccfb3] | 144 |
|
---|
| 145 | return h;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[4797132] | 148 | static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[9a5ccfb3] | 149 | {
|
---|
[4797132] | 150 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
|
---|
| 151 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
|
---|
| 152 | unsigned pdi = (unsigned)key[UPH_PDI_KEY];
|
---|
| 153 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
|
---|
[9a5ccfb3] | 154 |
|
---|
| 155 | return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
|
---|
| 156 | (pdi == fidx->pdi);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[4797132] | 159 | static void pos_remove_callback(link_t *item)
|
---|
| 160 | {
|
---|
| 161 | /* nothing to do */
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | static hash_table_operations_t uph_ops = {
|
---|
| 165 | .hash = pos_hash,
|
---|
| 166 | .compare = pos_compare,
|
---|
| 167 | .remove_callback = pos_remove_callback,
|
---|
| 168 | };
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Global hash table of all used fat_idx_t structures.
|
---|
| 172 | * The index structures are hashed by the dev_handle and index.
|
---|
| 173 | */
|
---|
| 174 | static hash_table_t ui_hash;
|
---|
| 175 |
|
---|
| 176 | #define UIH_BUCKETS_LOG 12
|
---|
| 177 | #define UIH_BUCKETS (1 << UIH_BUCKETS_LOG)
|
---|
| 178 |
|
---|
| 179 | #define UIH_DH_KEY 0
|
---|
| 180 | #define UIH_INDEX_KEY 1
|
---|
| 181 |
|
---|
| 182 | static hash_index_t idx_hash(unsigned long key[])
|
---|
| 183 | {
|
---|
| 184 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
|
---|
| 185 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
|
---|
| 186 |
|
---|
| 187 | hash_index_t h;
|
---|
| 188 |
|
---|
| 189 | h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
|
---|
| 190 | h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
|
---|
| 191 | (UIH_BUCKETS_LOG / 2);
|
---|
| 192 |
|
---|
| 193 | return h;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 197 | {
|
---|
| 198 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
|
---|
| 199 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
|
---|
| 200 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
|
---|
| 201 |
|
---|
| 202 | return (dev_handle == fidx->dev_handle) && (index == fidx->index);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[9a5ccfb3] | 205 | static void idx_remove_callback(link_t *item)
|
---|
| 206 | {
|
---|
| 207 | /* nothing to do */
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[4797132] | 210 | static hash_table_operations_t uih_ops = {
|
---|
[9a5ccfb3] | 211 | .hash = idx_hash,
|
---|
| 212 | .compare = idx_compare,
|
---|
| 213 | .remove_callback = idx_remove_callback,
|
---|
| 214 | };
|
---|
| 215 |
|
---|
[5a324099] | 216 | /** Allocate a VFS index which is not currently in use. */
|
---|
[48eb7a14] | 217 | static bool fat_index_alloc(dev_handle_t dev_handle, fs_index_t *index)
|
---|
[5a324099] | 218 | {
|
---|
| 219 | unused_t *u;
|
---|
| 220 |
|
---|
| 221 | assert(index);
|
---|
[b7b6753] | 222 | u = unused_find(dev_handle, true);
|
---|
| 223 | if (!u)
|
---|
| 224 | return false;
|
---|
[5a324099] | 225 |
|
---|
| 226 | if (list_empty(&u->freed_head)) {
|
---|
| 227 | if (u->remaining) {
|
---|
| 228 | /*
|
---|
| 229 | * There are no freed indices, allocate one directly
|
---|
| 230 | * from the counter.
|
---|
| 231 | */
|
---|
| 232 | *index = u->next++;
|
---|
| 233 | --u->remaining;
|
---|
[6ebe721] | 234 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 235 | return true;
|
---|
| 236 | }
|
---|
| 237 | } else {
|
---|
| 238 | /* There are some freed indices which we can reuse. */
|
---|
| 239 | freed_t *f = list_get_instance(u->freed_head.next, freed_t,
|
---|
| 240 | link);
|
---|
| 241 | *index = f->first;
|
---|
| 242 | if (f->first++ == f->last) {
|
---|
| 243 | /* Destroy the interval. */
|
---|
| 244 | list_remove(&f->link);
|
---|
| 245 | free(f);
|
---|
| 246 | }
|
---|
[6ebe721] | 247 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 248 | return true;
|
---|
| 249 | }
|
---|
| 250 | /*
|
---|
| 251 | * We ran out of indices, which is extremely unlikely with FAT16, but
|
---|
| 252 | * theoretically still possible (e.g. too many open unlinked nodes or
|
---|
| 253 | * too many zero-sized nodes).
|
---|
| 254 | */
|
---|
[6ebe721] | 255 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 256 | return false;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[0c1ad7ac] | 259 | /** If possible, coalesce two intervals of freed indices. */
|
---|
| 260 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
|
---|
[5a324099] | 261 | {
|
---|
| 262 | freed_t *fl = list_get_instance(l, freed_t, link);
|
---|
| 263 | freed_t *fr = list_get_instance(r, freed_t, link);
|
---|
| 264 |
|
---|
| 265 | if (fl->last + 1 == fr->first) {
|
---|
| 266 | if (cur == l) {
|
---|
| 267 | fl->last = fr->last;
|
---|
| 268 | list_remove(r);
|
---|
| 269 | free(r);
|
---|
| 270 | } else {
|
---|
| 271 | fr->first = fl->first;
|
---|
| 272 | list_remove(l);
|
---|
| 273 | free(l);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /** Free a VFS index, which is no longer in use. */
|
---|
[48eb7a14] | 279 | static void fat_index_free(dev_handle_t dev_handle, fs_index_t index)
|
---|
[5a324099] | 280 | {
|
---|
| 281 | unused_t *u;
|
---|
| 282 |
|
---|
[b7b6753] | 283 | u = unused_find(dev_handle, true);
|
---|
| 284 | assert(u);
|
---|
[5a324099] | 285 |
|
---|
| 286 | if (u->next == index + 1) {
|
---|
| 287 | /* The index can be returned directly to the counter. */
|
---|
| 288 | u->next--;
|
---|
| 289 | u->remaining++;
|
---|
| 290 | } else {
|
---|
| 291 | /*
|
---|
| 292 | * The index must be returned either to an existing freed
|
---|
| 293 | * interval or a new interval must be created.
|
---|
| 294 | */
|
---|
| 295 | link_t *lnk;
|
---|
| 296 | freed_t *n;
|
---|
| 297 | for (lnk = u->freed_head.next; lnk != &u->freed_head;
|
---|
| 298 | lnk = lnk->next) {
|
---|
| 299 | freed_t *f = list_get_instance(lnk, freed_t, link);
|
---|
| 300 | if (f->first == index + 1) {
|
---|
| 301 | f->first--;
|
---|
| 302 | if (lnk->prev != &u->freed_head)
|
---|
[0c1ad7ac] | 303 | try_coalesce_intervals(lnk->prev, lnk,
|
---|
[5a324099] | 304 | lnk);
|
---|
[6ebe721] | 305 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 306 | return;
|
---|
| 307 | }
|
---|
| 308 | if (f->last == index - 1) {
|
---|
| 309 | f->last++;
|
---|
| 310 | if (lnk->next != &u->freed_head)
|
---|
[0c1ad7ac] | 311 | try_coalesce_intervals(lnk, lnk->next,
|
---|
[5a324099] | 312 | lnk);
|
---|
[6ebe721] | 313 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 314 | return;
|
---|
| 315 | }
|
---|
| 316 | if (index > f->first) {
|
---|
| 317 | n = malloc(sizeof(freed_t));
|
---|
| 318 | /* TODO: sleep until allocation succeeds */
|
---|
| 319 | assert(n);
|
---|
| 320 | link_initialize(&n->link);
|
---|
| 321 | n->first = index;
|
---|
| 322 | n->last = index;
|
---|
| 323 | list_insert_before(&n->link, lnk);
|
---|
[6ebe721] | 324 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 325 | return;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | }
|
---|
| 329 | /* The index will form the last interval. */
|
---|
| 330 | n = malloc(sizeof(freed_t));
|
---|
| 331 | /* TODO: sleep until allocation succeeds */
|
---|
| 332 | assert(n);
|
---|
| 333 | link_initialize(&n->link);
|
---|
| 334 | n->first = index;
|
---|
| 335 | n->last = index;
|
---|
| 336 | list_append(&n->link, &u->freed_head);
|
---|
| 337 | }
|
---|
[6ebe721] | 338 | fibril_mutex_unlock(&unused_lock);
|
---|
[5a324099] | 339 | }
|
---|
| 340 |
|
---|
[9a15176] | 341 | static int fat_idx_create(fat_idx_t **fidxp, dev_handle_t dev_handle)
|
---|
[9a3d5f0] | 342 | {
|
---|
| 343 | fat_idx_t *fidx;
|
---|
| 344 |
|
---|
| 345 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
|
---|
| 346 | if (!fidx)
|
---|
[9a15176] | 347 | return ENOMEM;
|
---|
[48eb7a14] | 348 | if (!fat_index_alloc(dev_handle, &fidx->index)) {
|
---|
[9a3d5f0] | 349 | free(fidx);
|
---|
[9a15176] | 350 | return ENOSPC;
|
---|
[9a3d5f0] | 351 | }
|
---|
| 352 |
|
---|
| 353 | link_initialize(&fidx->uph_link);
|
---|
| 354 | link_initialize(&fidx->uih_link);
|
---|
[6ebe721] | 355 | fibril_mutex_initialize(&fidx->lock);
|
---|
[9a3d5f0] | 356 | fidx->dev_handle = dev_handle;
|
---|
| 357 | fidx->pfc = FAT_CLST_RES0; /* no parent yet */
|
---|
| 358 | fidx->pdi = 0;
|
---|
| 359 | fidx->nodep = NULL;
|
---|
| 360 |
|
---|
[9a15176] | 361 | *fidxp = fidx;
|
---|
| 362 | return EOK;
|
---|
[9a3d5f0] | 363 | }
|
---|
| 364 |
|
---|
[9a15176] | 365 | int fat_idx_get_new(fat_idx_t **fidxp, dev_handle_t dev_handle)
|
---|
[9a3d5f0] | 366 | {
|
---|
| 367 | fat_idx_t *fidx;
|
---|
[9a15176] | 368 | int rc;
|
---|
[9a3d5f0] | 369 |
|
---|
[6ebe721] | 370 | fibril_mutex_lock(&used_lock);
|
---|
[9a15176] | 371 | rc = fat_idx_create(&fidx, dev_handle);
|
---|
| 372 | if (rc != EOK) {
|
---|
[6ebe721] | 373 | fibril_mutex_unlock(&used_lock);
|
---|
[9a15176] | 374 | return rc;
|
---|
[9a3d5f0] | 375 | }
|
---|
| 376 |
|
---|
| 377 | unsigned long ikey[] = {
|
---|
| 378 | [UIH_DH_KEY] = dev_handle,
|
---|
| 379 | [UIH_INDEX_KEY] = fidx->index,
|
---|
| 380 | };
|
---|
| 381 |
|
---|
| 382 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
|
---|
[6ebe721] | 383 | fibril_mutex_lock(&fidx->lock);
|
---|
| 384 | fibril_mutex_unlock(&used_lock);
|
---|
[9a3d5f0] | 385 |
|
---|
[9a15176] | 386 | *fidxp = fidx;
|
---|
| 387 | return EOK;
|
---|
[9a3d5f0] | 388 | }
|
---|
| 389 |
|
---|
[4797132] | 390 | fat_idx_t *
|
---|
| 391 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
|
---|
[297f1197] | 392 | {
|
---|
[9a5ccfb3] | 393 | fat_idx_t *fidx;
|
---|
| 394 | link_t *l;
|
---|
[4797132] | 395 | unsigned long pkey[] = {
|
---|
| 396 | [UPH_DH_KEY] = dev_handle,
|
---|
| 397 | [UPH_PFC_KEY] = pfc,
|
---|
| 398 | [UPH_PDI_KEY] = pdi,
|
---|
[9a5ccfb3] | 399 | };
|
---|
| 400 |
|
---|
[6ebe721] | 401 | fibril_mutex_lock(&used_lock);
|
---|
[4797132] | 402 | l = hash_table_find(&up_hash, pkey);
|
---|
[9a5ccfb3] | 403 | if (l) {
|
---|
[4797132] | 404 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
|
---|
[9a5ccfb3] | 405 | } else {
|
---|
[9a15176] | 406 | int rc;
|
---|
| 407 |
|
---|
| 408 | rc = fat_idx_create(&fidx, dev_handle);
|
---|
| 409 | if (rc != EOK) {
|
---|
[6ebe721] | 410 | fibril_mutex_unlock(&used_lock);
|
---|
[9a5ccfb3] | 411 | return NULL;
|
---|
| 412 | }
|
---|
[4797132] | 413 |
|
---|
| 414 | unsigned long ikey[] = {
|
---|
| 415 | [UIH_DH_KEY] = dev_handle,
|
---|
| 416 | [UIH_INDEX_KEY] = fidx->index,
|
---|
| 417 | };
|
---|
| 418 |
|
---|
[9a5ccfb3] | 419 | fidx->pfc = pfc;
|
---|
| 420 | fidx->pdi = pdi;
|
---|
[4797132] | 421 |
|
---|
| 422 | hash_table_insert(&up_hash, pkey, &fidx->uph_link);
|
---|
| 423 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
|
---|
| 424 | }
|
---|
[6ebe721] | 425 | fibril_mutex_lock(&fidx->lock);
|
---|
| 426 | fibril_mutex_unlock(&used_lock);
|
---|
[4797132] | 427 |
|
---|
| 428 | return fidx;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[0fdd6bb] | 431 | void fat_idx_hashin(fat_idx_t *idx)
|
---|
| 432 | {
|
---|
| 433 | unsigned long pkey[] = {
|
---|
| 434 | [UPH_DH_KEY] = idx->dev_handle,
|
---|
| 435 | [UPH_PFC_KEY] = idx->pfc,
|
---|
| 436 | [UPH_PDI_KEY] = idx->pdi,
|
---|
| 437 | };
|
---|
| 438 |
|
---|
[6ebe721] | 439 | fibril_mutex_lock(&used_lock);
|
---|
[0fdd6bb] | 440 | hash_table_insert(&up_hash, pkey, &idx->uph_link);
|
---|
[6ebe721] | 441 | fibril_mutex_unlock(&used_lock);
|
---|
[0fdd6bb] | 442 | }
|
---|
| 443 |
|
---|
[a31c1ccf] | 444 | void fat_idx_hashout(fat_idx_t *idx)
|
---|
| 445 | {
|
---|
| 446 | unsigned long pkey[] = {
|
---|
| 447 | [UPH_DH_KEY] = idx->dev_handle,
|
---|
| 448 | [UPH_PFC_KEY] = idx->pfc,
|
---|
| 449 | [UPH_PDI_KEY] = idx->pdi,
|
---|
| 450 | };
|
---|
| 451 |
|
---|
[6ebe721] | 452 | fibril_mutex_lock(&used_lock);
|
---|
[a31c1ccf] | 453 | hash_table_remove(&up_hash, pkey, 3);
|
---|
[6ebe721] | 454 | fibril_mutex_unlock(&used_lock);
|
---|
[a31c1ccf] | 455 | }
|
---|
| 456 |
|
---|
[4797132] | 457 | fat_idx_t *
|
---|
| 458 | fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index)
|
---|
| 459 | {
|
---|
| 460 | fat_idx_t *fidx = NULL;
|
---|
| 461 | link_t *l;
|
---|
| 462 | unsigned long ikey[] = {
|
---|
| 463 | [UIH_DH_KEY] = dev_handle,
|
---|
| 464 | [UIH_INDEX_KEY] = index,
|
---|
| 465 | };
|
---|
| 466 |
|
---|
[6ebe721] | 467 | fibril_mutex_lock(&used_lock);
|
---|
[4797132] | 468 | l = hash_table_find(&ui_hash, ikey);
|
---|
| 469 | if (l) {
|
---|
| 470 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
|
---|
[ca093b3] | 471 | fibril_mutex_lock(&fidx->lock);
|
---|
[9a5ccfb3] | 472 | }
|
---|
[6ebe721] | 473 | fibril_mutex_unlock(&used_lock);
|
---|
[9a5ccfb3] | 474 |
|
---|
| 475 | return fidx;
|
---|
[297f1197] | 476 | }
|
---|
[9a5ccfb3] | 477 |
|
---|
[48eb7a14] | 478 | /** Destroy the index structure.
|
---|
| 479 | *
|
---|
| 480 | * @param idx The index structure to be destroyed.
|
---|
| 481 | */
|
---|
| 482 | void fat_idx_destroy(fat_idx_t *idx)
|
---|
| 483 | {
|
---|
| 484 | unsigned long ikey[] = {
|
---|
| 485 | [UIH_DH_KEY] = idx->dev_handle,
|
---|
| 486 | [UIH_INDEX_KEY] = idx->index,
|
---|
| 487 | };
|
---|
| 488 |
|
---|
| 489 | assert(idx->pfc == FAT_CLST_RES0);
|
---|
| 490 |
|
---|
[6ebe721] | 491 | fibril_mutex_lock(&used_lock);
|
---|
[48eb7a14] | 492 | /*
|
---|
| 493 | * Since we can only free unlinked nodes, the index structure is not
|
---|
| 494 | * present in the position hash (uph). We therefore hash it out from
|
---|
| 495 | * the index hash only.
|
---|
| 496 | */
|
---|
| 497 | hash_table_remove(&ui_hash, ikey, 2);
|
---|
[6ebe721] | 498 | fibril_mutex_unlock(&used_lock);
|
---|
[48eb7a14] | 499 | /* Release the VFS index. */
|
---|
| 500 | fat_index_free(idx->dev_handle, idx->index);
|
---|
| 501 | /* Deallocate the structure. */
|
---|
| 502 | free(idx);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[cde485d] | 505 | int fat_idx_init(void)
|
---|
| 506 | {
|
---|
| 507 | if (!hash_table_create(&up_hash, UPH_BUCKETS, 3, &uph_ops))
|
---|
| 508 | return ENOMEM;
|
---|
| 509 | if (!hash_table_create(&ui_hash, UIH_BUCKETS, 2, &uih_ops)) {
|
---|
| 510 | hash_table_destroy(&up_hash);
|
---|
| 511 | return ENOMEM;
|
---|
| 512 | }
|
---|
| 513 | return EOK;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | void fat_idx_fini(void)
|
---|
| 517 | {
|
---|
| 518 | /* We assume the hash tables are empty. */
|
---|
| 519 | hash_table_destroy(&up_hash);
|
---|
| 520 | hash_table_destroy(&ui_hash);
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | int fat_idx_init_by_dev_handle(dev_handle_t dev_handle)
|
---|
| 524 | {
|
---|
[b7b6753] | 525 | unused_t *u;
|
---|
| 526 | int rc = EOK;
|
---|
| 527 |
|
---|
| 528 | u = (unused_t *) malloc(sizeof(unused_t));
|
---|
[cde485d] | 529 | if (!u)
|
---|
| 530 | return ENOMEM;
|
---|
| 531 | unused_initialize(u, dev_handle);
|
---|
[6ebe721] | 532 | fibril_mutex_lock(&unused_lock);
|
---|
[b7b6753] | 533 | if (!unused_find(dev_handle, false))
|
---|
| 534 | list_append(&u->link, &unused_head);
|
---|
| 535 | else
|
---|
| 536 | rc = EEXIST;
|
---|
[6ebe721] | 537 | fibril_mutex_unlock(&unused_lock);
|
---|
[b7b6753] | 538 | return rc;
|
---|
[cde485d] | 539 | }
|
---|
| 540 |
|
---|
| 541 | void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle)
|
---|
| 542 | {
|
---|
| 543 | unused_t *u;
|
---|
| 544 |
|
---|
[b7b6753] | 545 | u = unused_find(dev_handle, true);
|
---|
| 546 | assert(u);
|
---|
[cde485d] | 547 | list_remove(&u->link);
|
---|
[6ebe721] | 548 | fibril_mutex_unlock(&unused_lock);
|
---|
[cde485d] | 549 |
|
---|
| 550 | while (!list_empty(&u->freed_head)) {
|
---|
| 551 | freed_t *f;
|
---|
| 552 | f = list_get_instance(u->freed_head.next, freed_t, link);
|
---|
| 553 | list_remove(&f->link);
|
---|
| 554 | free(f);
|
---|
| 555 | }
|
---|
| 556 | free(u);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
| 560 | * @}
|
---|
| 561 | */
|
---|