Changes in uspace/srv/fs/fat/fat_idx.c [ca093b3:991f645] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_idx.c
rca093b3 r991f645 39 39 #include "../../vfs/vfs.h" 40 40 #include <errno.h> 41 #include <str ing.h>41 #include <str.h> 42 42 #include <adt/hash_table.h> 43 43 #include <adt/list.h> 44 44 #include <assert.h> 45 #include <fibril_sync .h>45 #include <fibril_synch.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 _handle_t dev_handle;60 devmap_handle_t devmap_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 _handle_t dev_handle)77 static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle) 78 78 { 79 79 link_initialize(&u->link); 80 u->dev _handle = dev_handle;80 u->devmap_handle = devmap_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 _handle_t dev_handle, bool lock)86 static unused_t *unused_find(devmap_handle_t devmap_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 _handle == dev_handle)95 if (u->devmap_handle == devmap_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 _handle, parent node's first108 * The index structures are hashed by the devmap_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 _handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];122 devmap_handle_t devmap_handle = (devmap_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 _handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<142 h |= (devmap_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 _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];150 devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY]; 151 fat_cluster_t pfc; 152 unsigned pdi; 153 153 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); 154 154 155 return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && 156 (pdi == fidx->pdi); 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; 157 168 } 158 169 … … 170 181 /** 171 182 * Global hash table of all used fat_idx_t structures. 172 * The index structures are hashed by the dev _handle and index.183 * The index structures are hashed by the devmap_handle and index. 173 184 */ 174 185 static hash_table_t ui_hash; … … 182 193 static hash_index_t idx_hash(unsigned long key[]) 183 194 { 184 dev _handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];195 devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY]; 185 196 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; 186 197 187 198 hash_index_t h; 188 199 189 h = dev _handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);200 h = devmap_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); 190 201 h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << 191 202 (UIH_BUCKETS_LOG / 2); … … 196 207 static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) 197 208 { 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];209 devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY]; 210 fs_index_t index; 200 211 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); 201 212 202 return (dev_handle == fidx->dev_handle) && (index == fidx->index); 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; 203 225 } 204 226 205 227 static void idx_remove_callback(link_t *item) 206 228 { 207 /* nothing to do */ 229 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); 230 231 free(fidx); 208 232 } 209 233 … … 215 239 216 240 /** Allocate a VFS index which is not currently in use. */ 217 static bool fat_index_alloc(dev _handle_t dev_handle, fs_index_t *index)241 static bool fat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index) 218 242 { 219 243 unused_t *u; 220 244 221 245 assert(index); 222 u = unused_find(dev _handle, true);246 u = unused_find(devmap_handle, true); 223 247 if (!u) 224 248 return false; … … 277 301 278 302 /** Free a VFS index, which is no longer in use. */ 279 static void fat_index_free(dev _handle_t dev_handle, fs_index_t index)303 static void fat_index_free(devmap_handle_t devmap_handle, fs_index_t index) 280 304 { 281 305 unused_t *u; 282 306 283 u = unused_find(dev _handle, true);307 u = unused_find(devmap_handle, true); 284 308 assert(u); 285 309 … … 339 363 } 340 364 341 static fat_idx_t *fat_idx_create(dev_handle_t dev_handle)365 static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle) 342 366 { 343 367 fat_idx_t *fidx; … … 345 369 fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); 346 370 if (!fidx) 347 return NULL;348 if (!fat_index_alloc(dev _handle, &fidx->index)) {371 return ENOMEM; 372 if (!fat_index_alloc(devmap_handle, &fidx->index)) { 349 373 free(fidx); 350 return NULL;374 return ENOSPC; 351 375 } 352 376 … … 354 378 link_initialize(&fidx->uih_link); 355 379 fibril_mutex_initialize(&fidx->lock); 356 fidx->dev _handle = dev_handle;380 fidx->devmap_handle = devmap_handle; 357 381 fidx->pfc = FAT_CLST_RES0; /* no parent yet */ 358 382 fidx->pdi = 0; 359 383 fidx->nodep = NULL; 360 384 361 return fidx; 362 } 363 364 fat_idx_t *fat_idx_get_new(dev_handle_t dev_handle) 385 *fidxp = fidx; 386 return EOK; 387 } 388 389 int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle) 365 390 { 366 391 fat_idx_t *fidx; 392 int rc; 367 393 368 394 fibril_mutex_lock(&used_lock); 369 fidx = fat_idx_create(dev_handle);370 if ( !fidx) {395 rc = fat_idx_create(&fidx, devmap_handle); 396 if (rc != EOK) { 371 397 fibril_mutex_unlock(&used_lock); 372 return NULL;398 return rc; 373 399 } 374 400 375 401 unsigned long ikey[] = { 376 [UIH_DH_KEY] = dev _handle,402 [UIH_DH_KEY] = devmap_handle, 377 403 [UIH_INDEX_KEY] = fidx->index, 378 404 }; … … 382 408 fibril_mutex_unlock(&used_lock); 383 409 384 return fidx; 410 *fidxp = fidx; 411 return EOK; 385 412 } 386 413 387 414 fat_idx_t * 388 fat_idx_get_by_pos(dev _handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)415 fat_idx_get_by_pos(devmap_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi) 389 416 { 390 417 fat_idx_t *fidx; 391 418 link_t *l; 392 419 unsigned long pkey[] = { 393 [UPH_DH_KEY] = dev _handle,420 [UPH_DH_KEY] = devmap_handle, 394 421 [UPH_PFC_KEY] = pfc, 395 422 [UPH_PDI_KEY] = pdi, … … 401 428 fidx = hash_table_get_instance(l, fat_idx_t, uph_link); 402 429 } else { 403 fidx = fat_idx_create(dev_handle); 404 if (!fidx) { 430 int rc; 431 432 rc = fat_idx_create(&fidx, devmap_handle); 433 if (rc != EOK) { 405 434 fibril_mutex_unlock(&used_lock); 406 435 return NULL; … … 408 437 409 438 unsigned long ikey[] = { 410 [UIH_DH_KEY] = dev _handle,439 [UIH_DH_KEY] = devmap_handle, 411 440 [UIH_INDEX_KEY] = fidx->index, 412 441 }; … … 427 456 { 428 457 unsigned long pkey[] = { 429 [UPH_DH_KEY] = idx->dev _handle,458 [UPH_DH_KEY] = idx->devmap_handle, 430 459 [UPH_PFC_KEY] = idx->pfc, 431 460 [UPH_PDI_KEY] = idx->pdi, … … 440 469 { 441 470 unsigned long pkey[] = { 442 [UPH_DH_KEY] = idx->dev _handle,471 [UPH_DH_KEY] = idx->devmap_handle, 443 472 [UPH_PFC_KEY] = idx->pfc, 444 473 [UPH_PDI_KEY] = idx->pdi, … … 451 480 452 481 fat_idx_t * 453 fat_idx_get_by_index(dev _handle_t dev_handle, fs_index_t index)482 fat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index) 454 483 { 455 484 fat_idx_t *fidx = NULL; 456 485 link_t *l; 457 486 unsigned long ikey[] = { 458 [UIH_DH_KEY] = dev _handle,487 [UIH_DH_KEY] = devmap_handle, 459 488 [UIH_INDEX_KEY] = index, 460 489 }; … … 478 507 { 479 508 unsigned long ikey[] = { 480 [UIH_DH_KEY] = idx->dev _handle,509 [UIH_DH_KEY] = idx->devmap_handle, 481 510 [UIH_INDEX_KEY] = idx->index, 482 511 }; 512 devmap_handle_t devmap_handle = idx->devmap_handle; 513 fs_index_t index = idx->index; 483 514 484 515 assert(idx->pfc == FAT_CLST_RES0); … … 493 524 fibril_mutex_unlock(&used_lock); 494 525 /* Release the VFS index. */ 495 fat_index_free(idx->dev_handle, idx->index); 496 /* Deallocate the structure. */ 497 free(idx); 526 fat_index_free(devmap_handle, index); 527 /* The index structure itself is freed in idx_remove_callback(). */ 498 528 } 499 529 … … 516 546 } 517 547 518 int fat_idx_init_by_dev _handle(dev_handle_t dev_handle)548 int fat_idx_init_by_devmap_handle(devmap_handle_t devmap_handle) 519 549 { 520 550 unused_t *u; … … 524 554 if (!u) 525 555 return ENOMEM; 526 unused_initialize(u, dev _handle);556 unused_initialize(u, devmap_handle); 527 557 fibril_mutex_lock(&unused_lock); 528 if (!unused_find(dev _handle, false))558 if (!unused_find(devmap_handle, false)) { 529 559 list_append(&u->link, &unused_head); 530 else 560 } else { 561 free(u); 531 562 rc = EEXIST; 563 } 532 564 fibril_mutex_unlock(&unused_lock); 533 565 return rc; 534 566 } 535 567 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); 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); 541 591 assert(u); 542 592 list_remove(&u->link);
Note:
See TracChangeset
for help on using the changeset viewer.