Changes in uspace/srv/fs/fat/fat_idx.c [991f645:ca093b3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_idx.c
r991f645 rca093b3 39 39 #include "../../vfs/vfs.h" 40 40 #include <errno.h> 41 #include <str .h>41 #include <string.h> 42 42 #include <adt/hash_table.h> 43 43 #include <adt/list.h> 44 44 #include <assert.h> 45 #include <fibril_sync h.h>45 #include <fibril_sync.h> 46 46 47 47 /** Each instance of this type describes one interval of freed VFS indices. */ … … 58 58 typedef struct { 59 59 link_t link; 60 dev map_handle_t devmap_handle;60 dev_handle_t dev_handle; 61 61 62 62 /** Next unassigned index. */ … … 75 75 static LIST_INITIALIZE(unused_head); 76 76 77 static void unused_initialize(unused_t *u, dev map_handle_t devmap_handle)77 static void unused_initialize(unused_t *u, dev_handle_t dev_handle) 78 78 { 79 79 link_initialize(&u->link); 80 u->dev map_handle = devmap_handle;80 u->dev_handle = dev_handle; 81 81 u->next = 0; 82 82 u->remaining = ((uint64_t)((fs_index_t)-1)) + 1; … … 84 84 } 85 85 86 static unused_t *unused_find(dev map_handle_t devmap_handle, bool lock)86 static unused_t *unused_find(dev_handle_t dev_handle, bool lock) 87 87 { 88 88 unused_t *u; … … 93 93 for (l = unused_head.next; l != &unused_head; l = l->next) { 94 94 u = list_get_instance(l, unused_t, link); 95 if (u->dev map_handle == devmap_handle)95 if (u->dev_handle == dev_handle) 96 96 return u; 97 97 } … … 106 106 /** 107 107 * Global hash table of all used fat_idx_t structures. 108 * The index structures are hashed by the dev map_handle, parent node's first108 * The index structures are hashed by the dev_handle, parent node's first 109 109 * cluster and index within the parent directory. 110 110 */ … … 120 120 static hash_index_t pos_hash(unsigned long key[]) 121 121 { 122 dev map_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];122 dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; 123 123 fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; 124 124 unsigned pdi = (unsigned)key[UPH_PDI_KEY]; … … 140 140 h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << 141 141 (UPH_BUCKETS_LOG / 2); 142 h |= (dev map_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<142 h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << 143 143 (3 * (UPH_BUCKETS_LOG / 4)); 144 144 … … 148 148 static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) 149 149 { 150 dev map_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];151 fat_cluster_t pfc ;152 unsigned pdi ;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 153 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); 154 154 155 switch (keys) { 156 case 1: 157 return (devmap_handle == fidx->devmap_handle); 158 case 3: 159 pfc = (fat_cluster_t) key[UPH_PFC_KEY]; 160 pdi = (unsigned) key[UPH_PDI_KEY]; 161 return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) && 162 (pdi == fidx->pdi); 163 default: 164 assert((keys == 1) || (keys == 3)); 165 } 166 167 return 0; 155 return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && 156 (pdi == fidx->pdi); 168 157 } 169 158 … … 181 170 /** 182 171 * Global hash table of all used fat_idx_t structures. 183 * The index structures are hashed by the dev map_handle and index.172 * The index structures are hashed by the dev_handle and index. 184 173 */ 185 174 static hash_table_t ui_hash; … … 193 182 static hash_index_t idx_hash(unsigned long key[]) 194 183 { 195 dev map_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];184 dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; 196 185 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; 197 186 198 187 hash_index_t h; 199 188 200 h = dev map_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);189 h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); 201 190 h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << 202 191 (UIH_BUCKETS_LOG / 2); … … 207 196 static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) 208 197 { 209 dev map_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];210 fs_index_t index ;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]; 211 200 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); 212 201 213 switch (keys) { 214 case 1: 215 return (devmap_handle == fidx->devmap_handle); 216 case 2: 217 index = (fs_index_t) key[UIH_INDEX_KEY]; 218 return (devmap_handle == fidx->devmap_handle) && 219 (index == fidx->index); 220 default: 221 assert((keys == 1) || (keys == 2)); 222 } 223 224 return 0; 202 return (dev_handle == fidx->dev_handle) && (index == fidx->index); 225 203 } 226 204 227 205 static void idx_remove_callback(link_t *item) 228 206 { 229 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); 230 231 free(fidx); 207 /* nothing to do */ 232 208 } 233 209 … … 239 215 240 216 /** Allocate a VFS index which is not currently in use. */ 241 static bool fat_index_alloc(dev map_handle_t devmap_handle, fs_index_t *index)217 static bool fat_index_alloc(dev_handle_t dev_handle, fs_index_t *index) 242 218 { 243 219 unused_t *u; 244 220 245 221 assert(index); 246 u = unused_find(dev map_handle, true);222 u = unused_find(dev_handle, true); 247 223 if (!u) 248 224 return false; … … 301 277 302 278 /** Free a VFS index, which is no longer in use. */ 303 static void fat_index_free(dev map_handle_t devmap_handle, fs_index_t index)279 static void fat_index_free(dev_handle_t dev_handle, fs_index_t index) 304 280 { 305 281 unused_t *u; 306 282 307 u = unused_find(dev map_handle, true);283 u = unused_find(dev_handle, true); 308 284 assert(u); 309 285 … … 363 339 } 364 340 365 static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle)341 static fat_idx_t *fat_idx_create(dev_handle_t dev_handle) 366 342 { 367 343 fat_idx_t *fidx; … … 369 345 fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); 370 346 if (!fidx) 371 return ENOMEM;372 if (!fat_index_alloc(dev map_handle, &fidx->index)) {347 return NULL; 348 if (!fat_index_alloc(dev_handle, &fidx->index)) { 373 349 free(fidx); 374 return ENOSPC;350 return NULL; 375 351 } 376 352 … … 378 354 link_initialize(&fidx->uih_link); 379 355 fibril_mutex_initialize(&fidx->lock); 380 fidx->dev map_handle = devmap_handle;356 fidx->dev_handle = dev_handle; 381 357 fidx->pfc = FAT_CLST_RES0; /* no parent yet */ 382 358 fidx->pdi = 0; 383 359 fidx->nodep = NULL; 384 360 385 *fidxp = fidx; 386 return EOK; 387 } 388 389 int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle) 361 return fidx; 362 } 363 364 fat_idx_t *fat_idx_get_new(dev_handle_t dev_handle) 390 365 { 391 366 fat_idx_t *fidx; 392 int rc;393 367 394 368 fibril_mutex_lock(&used_lock); 395 rc = fat_idx_create(&fidx, devmap_handle);396 if ( rc != EOK) {369 fidx = fat_idx_create(dev_handle); 370 if (!fidx) { 397 371 fibril_mutex_unlock(&used_lock); 398 return rc;372 return NULL; 399 373 } 400 374 401 375 unsigned long ikey[] = { 402 [UIH_DH_KEY] = dev map_handle,376 [UIH_DH_KEY] = dev_handle, 403 377 [UIH_INDEX_KEY] = fidx->index, 404 378 }; … … 408 382 fibril_mutex_unlock(&used_lock); 409 383 410 *fidxp = fidx; 411 return EOK; 384 return fidx; 412 385 } 413 386 414 387 fat_idx_t * 415 fat_idx_get_by_pos(dev map_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi)388 fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) 416 389 { 417 390 fat_idx_t *fidx; 418 391 link_t *l; 419 392 unsigned long pkey[] = { 420 [UPH_DH_KEY] = dev map_handle,393 [UPH_DH_KEY] = dev_handle, 421 394 [UPH_PFC_KEY] = pfc, 422 395 [UPH_PDI_KEY] = pdi, … … 428 401 fidx = hash_table_get_instance(l, fat_idx_t, uph_link); 429 402 } else { 430 int rc; 431 432 rc = fat_idx_create(&fidx, devmap_handle); 433 if (rc != EOK) { 403 fidx = fat_idx_create(dev_handle); 404 if (!fidx) { 434 405 fibril_mutex_unlock(&used_lock); 435 406 return NULL; … … 437 408 438 409 unsigned long ikey[] = { 439 [UIH_DH_KEY] = dev map_handle,410 [UIH_DH_KEY] = dev_handle, 440 411 [UIH_INDEX_KEY] = fidx->index, 441 412 }; … … 456 427 { 457 428 unsigned long pkey[] = { 458 [UPH_DH_KEY] = idx->dev map_handle,429 [UPH_DH_KEY] = idx->dev_handle, 459 430 [UPH_PFC_KEY] = idx->pfc, 460 431 [UPH_PDI_KEY] = idx->pdi, … … 469 440 { 470 441 unsigned long pkey[] = { 471 [UPH_DH_KEY] = idx->dev map_handle,442 [UPH_DH_KEY] = idx->dev_handle, 472 443 [UPH_PFC_KEY] = idx->pfc, 473 444 [UPH_PDI_KEY] = idx->pdi, … … 480 451 481 452 fat_idx_t * 482 fat_idx_get_by_index(dev map_handle_t devmap_handle, fs_index_t index)453 fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index) 483 454 { 484 455 fat_idx_t *fidx = NULL; 485 456 link_t *l; 486 457 unsigned long ikey[] = { 487 [UIH_DH_KEY] = dev map_handle,458 [UIH_DH_KEY] = dev_handle, 488 459 [UIH_INDEX_KEY] = index, 489 460 }; … … 507 478 { 508 479 unsigned long ikey[] = { 509 [UIH_DH_KEY] = idx->dev map_handle,480 [UIH_DH_KEY] = idx->dev_handle, 510 481 [UIH_INDEX_KEY] = idx->index, 511 482 }; 512 devmap_handle_t devmap_handle = idx->devmap_handle;513 fs_index_t index = idx->index;514 483 515 484 assert(idx->pfc == FAT_CLST_RES0); … … 524 493 fibril_mutex_unlock(&used_lock); 525 494 /* Release the VFS index. */ 526 fat_index_free(devmap_handle, index); 527 /* The index structure itself is freed in idx_remove_callback(). */ 495 fat_index_free(idx->dev_handle, idx->index); 496 /* Deallocate the structure. */ 497 free(idx); 528 498 } 529 499 … … 546 516 } 547 517 548 int fat_idx_init_by_dev map_handle(devmap_handle_t devmap_handle)518 int fat_idx_init_by_dev_handle(dev_handle_t dev_handle) 549 519 { 550 520 unused_t *u; … … 554 524 if (!u) 555 525 return ENOMEM; 556 unused_initialize(u, dev map_handle);526 unused_initialize(u, dev_handle); 557 527 fibril_mutex_lock(&unused_lock); 558 if (!unused_find(dev map_handle, false)) {528 if (!unused_find(dev_handle, false)) 559 529 list_append(&u->link, &unused_head); 560 } else { 561 free(u); 530 else 562 531 rc = EEXIST; 563 }564 532 fibril_mutex_unlock(&unused_lock); 565 533 return rc; 566 534 } 567 535 568 void fat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle) 569 { 570 unsigned long ikey[] = { 571 [UIH_DH_KEY] = devmap_handle 572 }; 573 unsigned long pkey[] = { 574 [UPH_DH_KEY] = devmap_handle 575 }; 576 577 /* 578 * Remove this instance's index structure from up_hash and ui_hash. 579 * Process up_hash first and ui_hash second because the index structure 580 * is actually removed in idx_remove_callback(). 581 */ 582 fibril_mutex_lock(&used_lock); 583 hash_table_remove(&up_hash, pkey, 1); 584 hash_table_remove(&ui_hash, ikey, 1); 585 fibril_mutex_unlock(&used_lock); 586 587 /* 588 * Free the unused and freed structures for this instance. 589 */ 590 unused_t *u = unused_find(devmap_handle, true); 536 void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle) 537 { 538 unused_t *u; 539 540 u = unused_find(dev_handle, true); 591 541 assert(u); 592 542 list_remove(&u->link);
Note:
See TracChangeset
for help on using the changeset viewer.