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