Changes in uspace/lib/block/libblock.c [b72efe8:08cba4b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/block/libblock.c
rb72efe8 r08cba4b 39 39 #include "libblock.h" 40 40 #include "../../srv/vfs/vfs.h" 41 #include <ipc/ devmap.h>41 #include <ipc/loc.h> 42 42 #include <ipc/bd.h> 43 43 #include <ipc/services.h> … … 78 78 typedef struct { 79 79 link_t link; 80 devmap_handle_t devmap_handle;80 service_id_t service_id; 81 81 async_sess_t *sess; 82 82 fibril_mutex_t comm_area_lock; … … 95 95 static aoff64_t ba_ltop(devcon_t *, aoff64_t); 96 96 97 static devcon_t *devcon_search( devmap_handle_t devmap_handle)97 static devcon_t *devcon_search(service_id_t service_id) 98 98 { 99 99 fibril_mutex_lock(&dcl_lock); … … 101 101 list_foreach(dcl, cur) { 102 102 devcon_t *devcon = list_get_instance(cur, devcon_t, link); 103 if (devcon-> devmap_handle == devmap_handle) {103 if (devcon->service_id == service_id) { 104 104 fibril_mutex_unlock(&dcl_lock); 105 105 return devcon; … … 111 111 } 112 112 113 static int devcon_add( devmap_handle_t devmap_handle, async_sess_t *sess,113 static int devcon_add(service_id_t service_id, async_sess_t *sess, 114 114 size_t bsize, void *comm_area, size_t comm_size) 115 115 { … … 124 124 125 125 link_initialize(&devcon->link); 126 devcon-> devmap_handle = devmap_handle;126 devcon->service_id = service_id; 127 127 devcon->sess = sess; 128 128 fibril_mutex_initialize(&devcon->comm_area_lock); … … 137 137 list_foreach(dcl, cur) { 138 138 devcon_t *d = list_get_instance(cur, devcon_t, link); 139 if (d-> devmap_handle == devmap_handle) {139 if (d->service_id == service_id) { 140 140 fibril_mutex_unlock(&dcl_lock); 141 141 free(devcon); … … 155 155 } 156 156 157 int block_init(exch_mgmt_t mgmt, devmap_handle_t devmap_handle,157 int block_init(exch_mgmt_t mgmt, service_id_t service_id, 158 158 size_t comm_size) 159 159 { … … 163 163 return ENOMEM; 164 164 165 async_sess_t *sess = devmap_device_connect(mgmt, devmap_handle,165 async_sess_t *sess = loc_service_connect(mgmt, service_id, 166 166 IPC_FLAG_BLOCKING); 167 167 if (!sess) { … … 190 190 } 191 191 192 rc = devcon_add( devmap_handle, sess, bsize, comm_area, comm_size);192 rc = devcon_add(service_id, sess, bsize, comm_area, comm_size); 193 193 if (rc != EOK) { 194 194 munmap(comm_area, comm_size); … … 200 200 } 201 201 202 void block_fini( devmap_handle_t devmap_handle)203 { 204 devcon_t *devcon = devcon_search( devmap_handle);202 void block_fini(service_id_t service_id) 203 { 204 devcon_t *devcon = devcon_search(service_id); 205 205 assert(devcon); 206 206 207 207 if (devcon->cache) 208 (void) block_cache_fini( devmap_handle);208 (void) block_cache_fini(service_id); 209 209 210 210 devcon_remove(devcon); … … 219 219 } 220 220 221 int block_bb_read( devmap_handle_t devmap_handle, aoff64_t ba)221 int block_bb_read(service_id_t service_id, aoff64_t ba) 222 222 { 223 223 void *bb_buf; 224 224 int rc; 225 225 226 devcon_t *devcon = devcon_search( devmap_handle);226 devcon_t *devcon = devcon_search(service_id); 227 227 if (!devcon) 228 228 return ENOENT; … … 249 249 } 250 250 251 void *block_bb_get( devmap_handle_t devmap_handle)252 { 253 devcon_t *devcon = devcon_search( devmap_handle);251 void *block_bb_get(service_id_t service_id) 252 { 253 devcon_t *devcon = devcon_search(service_id); 254 254 assert(devcon); 255 255 return devcon->bb_buf; … … 258 258 static hash_index_t cache_hash(unsigned long *key) 259 259 { 260 return *key& (CACHE_BUCKETS - 1);260 return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1); 261 261 } 262 262 … … 264 264 { 265 265 block_t *b = hash_table_get_instance(item, block_t, hash_link); 266 return b->lba == *key;266 return b->lba == MERGE_LOUP32(key[0], key[1]); 267 267 } 268 268 … … 277 277 }; 278 278 279 int block_cache_init( devmap_handle_t devmap_handle, size_t size, unsigned blocks,279 int block_cache_init(service_id_t service_id, size_t size, unsigned blocks, 280 280 enum cache_mode mode) 281 281 { 282 devcon_t *devcon = devcon_search( devmap_handle);282 devcon_t *devcon = devcon_search(service_id); 283 283 cache_t *cache; 284 284 if (!devcon) … … 305 305 cache->blocks_cluster = cache->lblock_size / devcon->pblock_size; 306 306 307 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,307 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2, 308 308 &cache_ops)) { 309 309 free(cache); … … 315 315 } 316 316 317 int block_cache_fini( devmap_handle_t devmap_handle)318 { 319 devcon_t *devcon = devcon_search( devmap_handle);317 int block_cache_fini(service_id_t service_id) 318 { 319 devcon_t *devcon = devcon_search(service_id); 320 320 cache_t *cache; 321 321 int rc; … … 344 344 } 345 345 346 unsigned long key = b->lba; 347 hash_table_remove(&cache->block_hash, &key, 1); 346 unsigned long key[2] = { 347 LOWER32(b->lba), 348 UPPER32(b->lba) 349 }; 350 hash_table_remove(&cache->block_hash, key, 2); 348 351 349 352 free(b->data); … … 384 387 * @param block Pointer to where the function will store the 385 388 * block pointer on success. 386 * @param devmap_handle Device handleof the block device.389 * @param service_id Service ID of the block device. 387 390 * @param ba Block address (logical). 388 391 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get() … … 392 395 * @return EOK on success or a negative error code. 393 396 */ 394 int block_get(block_t **block, devmap_handle_t devmap_handle, aoff64_t ba, int flags)397 int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags) 395 398 { 396 399 devcon_t *devcon; … … 398 401 block_t *b; 399 402 link_t *l; 400 unsigned long key = ba; 403 unsigned long key[2] = { 404 LOWER32(ba), 405 UPPER32(ba) 406 }; 407 401 408 int rc; 402 409 403 devcon = devcon_search( devmap_handle);410 devcon = devcon_search(service_id); 404 411 405 412 assert(devcon); … … 413 420 414 421 fibril_mutex_lock(&cache->lock); 415 l = hash_table_find(&cache->block_hash, &key);422 l = hash_table_find(&cache->block_hash, key); 416 423 if (l) { 417 424 found: … … 451 458 * Try to recycle a block from the free list. 452 459 */ 453 unsigned long temp_key;454 460 recycle: 455 461 if (list_empty(&cache->free_list)) { … … 499 505 goto retry; 500 506 } 501 l = hash_table_find(&cache->block_hash, &key);507 l = hash_table_find(&cache->block_hash, key); 502 508 if (l) { 503 509 /* … … 522 528 */ 523 529 list_remove(&b->free_link); 524 temp_key = b->lba; 525 hash_table_remove(&cache->block_hash, &temp_key, 1); 530 unsigned long temp_key[2] = { 531 LOWER32(b->lba), 532 UPPER32(b->lba) 533 }; 534 hash_table_remove(&cache->block_hash, temp_key, 2); 526 535 } 527 536 528 537 block_initialize(b); 529 b-> devmap_handle = devmap_handle;538 b->service_id = service_id; 530 539 b->size = cache->lblock_size; 531 540 b->lba = ba; 532 541 b->pba = ba_ltop(devcon, b->lba); 533 hash_table_insert(&cache->block_hash, &key, &b->hash_link);542 hash_table_insert(&cache->block_hash, key, &b->hash_link); 534 543 535 544 /* … … 577 586 int block_put(block_t *block) 578 587 { 579 devcon_t *devcon = devcon_search(block-> devmap_handle);588 devcon_t *devcon = devcon_search(block->service_id); 580 589 cache_t *cache; 581 590 unsigned blocks_cached; … … 643 652 * Take the block out of the cache and free it. 644 653 */ 645 unsigned long key = block->lba; 646 hash_table_remove(&cache->block_hash, &key, 1); 654 unsigned long key[2] = { 655 LOWER32(block->lba), 656 UPPER32(block->lba) 657 }; 658 hash_table_remove(&cache->block_hash, key, 2); 647 659 fibril_mutex_unlock(&block->lock); 648 660 free(block->data); … … 675 687 /** Read sequential data from a block device. 676 688 * 677 * @param devmap_handle Device handleof the block device.689 * @param service_id Service ID of the block device. 678 690 * @param bufpos Pointer to the first unread valid offset within the 679 691 * communication buffer. … … 687 699 * @return EOK on success or a negative return code on failure. 688 700 */ 689 int block_seqread( devmap_handle_t devmap_handle, size_t *bufpos, size_t *buflen,701 int block_seqread(service_id_t service_id, size_t *bufpos, size_t *buflen, 690 702 aoff64_t *pos, void *dst, size_t size) 691 703 { … … 695 707 devcon_t *devcon; 696 708 697 devcon = devcon_search( devmap_handle);709 devcon = devcon_search(service_id); 698 710 assert(devcon); 699 711 block_size = devcon->pblock_size; … … 741 753 /** Read blocks directly from device (bypass cache). 742 754 * 743 * @param devmap_handle Device handleof the block device.755 * @param service_id Service ID of the block device. 744 756 * @param ba Address of first block (physical). 745 757 * @param cnt Number of blocks. … … 748 760 * @return EOK on success or negative error code on failure. 749 761 */ 750 int block_read_direct( devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt, void *buf)762 int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf) 751 763 { 752 764 devcon_t *devcon; 753 765 int rc; 754 766 755 devcon = devcon_search( devmap_handle);767 devcon = devcon_search(service_id); 756 768 assert(devcon); 757 769 … … 769 781 /** Write blocks directly to device (bypass cache). 770 782 * 771 * @param devmap_handle Device handleof the block device.783 * @param service_id Service ID of the block device. 772 784 * @param ba Address of first block (physical). 773 785 * @param cnt Number of blocks. … … 776 788 * @return EOK on success or negative error code on failure. 777 789 */ 778 int block_write_direct( devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt,790 int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt, 779 791 const void *data) 780 792 { … … 782 794 int rc; 783 795 784 devcon = devcon_search( devmap_handle);796 devcon = devcon_search(service_id); 785 797 assert(devcon); 786 798 … … 797 809 /** Get device block size. 798 810 * 799 * @param devmap_handle Device handleof the block device.811 * @param service_id Service ID of the block device. 800 812 * @param bsize Output block size. 801 813 * 802 814 * @return EOK on success or negative error code on failure. 803 815 */ 804 int block_get_bsize( devmap_handle_t devmap_handle, size_t *bsize)816 int block_get_bsize(service_id_t service_id, size_t *bsize) 805 817 { 806 818 devcon_t *devcon; 807 819 808 devcon = devcon_search( devmap_handle);820 devcon = devcon_search(service_id); 809 821 assert(devcon); 810 822 … … 814 826 /** Get number of blocks on device. 815 827 * 816 * @param devmap_handle Device handleof the block device.828 * @param service_id Service ID of the block device. 817 829 * @param nblocks Output number of blocks. 818 830 * 819 831 * @return EOK on success or negative error code on failure. 820 832 */ 821 int block_get_nblocks( devmap_handle_t devmap_handle, aoff64_t *nblocks)822 { 823 devcon_t *devcon = devcon_search( devmap_handle);833 int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks) 834 { 835 devcon_t *devcon = devcon_search(service_id); 824 836 assert(devcon); 825 837 … … 829 841 /** Read bytes directly from the device (bypass cache) 830 842 * 831 * @param devmap_handle Device handleof the block device.843 * @param service_id Service ID of the block device. 832 844 * @param abs_offset Absolute offset in bytes where to start reading 833 845 * @param bytes Number of bytes to read … … 836 848 * @return EOK on success or negative error code on failure. 837 849 */ 838 int block_read_bytes_direct( devmap_handle_t devmap_handle, aoff64_t abs_offset,850 int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset, 839 851 size_t bytes, void *data) 840 852 { … … 848 860 size_t offset; 849 861 850 rc = block_get_bsize( devmap_handle, &phys_block_size);862 rc = block_get_bsize(service_id, &phys_block_size); 851 863 if (rc != EOK) { 852 864 return rc; … … 866 878 } 867 879 868 rc = block_read_direct( devmap_handle, first_block, blocks, buffer);880 rc = block_read_direct(service_id, first_block, blocks, buffer); 869 881 if (rc != EOK) { 870 882 free(buffer); … … 877 889 878 890 return EOK; 891 } 892 893 /** Get TOC from device. 894 * 895 * @param service_id Service ID of the block device. 896 * @param session Starting session. 897 * 898 * @return Allocated TOC structure. 899 * @return NULL on failure. 900 * 901 */ 902 toc_block_t *block_get_toc(service_id_t service_id, uint8_t session) 903 { 904 devcon_t *devcon = devcon_search(service_id); 905 assert(devcon); 906 907 toc_block_t *toc = NULL; 908 909 fibril_mutex_lock(&devcon->comm_area_lock); 910 911 async_exch_t *exch = async_exchange_begin(devcon->sess); 912 int rc = async_req_1_0(exch, BD_READ_TOC, session); 913 async_exchange_end(exch); 914 915 if (rc == EOK) { 916 toc = (toc_block_t *) malloc(sizeof(toc_block_t)); 917 if (toc != NULL) { 918 memset(toc, 0, sizeof(toc_block_t)); 919 memcpy(toc, devcon->comm_area, 920 min(devcon->pblock_size, sizeof(toc_block_t))); 921 } 922 } 923 924 925 fibril_mutex_unlock(&devcon->comm_area_lock); 926 927 return toc; 879 928 } 880 929 … … 900 949 printf("Error %d reading %zu blocks starting at block %" PRIuOFF64 901 950 " from device handle %" PRIun "\n", rc, cnt, ba, 902 devcon-> devmap_handle);951 devcon->service_id); 903 952 #ifndef NDEBUG 904 953 stacktrace_print(); … … 929 978 if (rc != EOK) { 930 979 printf("Error %d writing %zu blocks starting at block %" PRIuOFF64 931 " to device handle %" PRIun "\n", rc, cnt, ba, devcon-> devmap_handle);980 " to device handle %" PRIun "\n", rc, cnt, ba, devcon->service_id); 932 981 #ifndef NDEBUG 933 982 stacktrace_print();
Note:
See TracChangeset
for help on using the changeset viewer.