Changeset 1ee00b7 in mainline for uspace/lib/libblock/libblock.c
- Timestamp:
- 2009-08-30T22:25:48Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a830611
- Parents:
- ff62c6d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/libblock/libblock.c
rff62c6d r1ee00b7 50 50 #include <adt/list.h> 51 51 #include <adt/hash_table.h> 52 #include <macros.h> 52 53 #include <mem.h> 53 54 … … 62 63 typedef struct { 63 64 fibril_mutex_t lock; 64 size_t block_size; /**< Block size. */65 size_t lblock_size; /**< Logical block size. */ 65 66 unsigned block_count; /**< Total number of blocks. */ 66 67 unsigned blocks_cached; /**< Number of cached blocks. */ … … 78 79 size_t com_size; 79 80 void *bb_buf; 80 off_t bb_off;81 size_t bb_size;81 bn_t bb_addr; 82 size_t pblock_size; /**< Physical block size. */ 82 83 cache_t *cache; 83 84 } devcon_t; 84 85 85 static int read_block(devcon_t *devcon, bn_t boff, size_t block_size); 86 static int write_block(devcon_t *devcon, bn_t boff, size_t block_size); 86 static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt); 87 static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt); 88 static size_t get_block_size(int dev_phone, size_t *bsize); 87 89 88 90 static devcon_t *devcon_search(dev_handle_t dev_handle) … … 102 104 } 103 105 104 static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,105 size_t com_size)106 static int devcon_add(dev_handle_t dev_handle, int dev_phone, size_t bsize, 107 void *com_area, size_t com_size) 106 108 { 107 109 link_t *cur; 108 110 devcon_t *devcon; 111 112 if (com_size < bsize) 113 return EINVAL; 109 114 110 115 devcon = malloc(sizeof(devcon_t)); … … 119 124 devcon->com_size = com_size; 120 125 devcon->bb_buf = NULL; 121 devcon->bb_ off= 0;122 devcon-> bb_size = 0;126 devcon->bb_addr = 0; 127 devcon->pblock_size = bsize; 123 128 devcon->cache = NULL; 124 129 … … 149 154 int dev_phone; 150 155 void *com_area; 151 156 size_t bsize; 157 152 158 com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, 153 159 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); … … 169 175 return rc; 170 176 } 177 178 if (get_block_size(dev_phone, &bsize) != EOK) { 179 munmap(com_area, com_size); 180 ipc_hangup(dev_phone); 181 return rc; 182 } 171 183 172 rc = devcon_add(dev_handle, dev_phone, com_area, com_size);184 rc = devcon_add(dev_handle, dev_phone, bsize, com_area, com_size); 173 185 if (rc != EOK) { 174 186 munmap(com_area, com_size); … … 201 213 } 202 214 203 int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)215 int block_bb_read(dev_handle_t dev_handle, bn_t ba) 204 216 { 205 217 void *bb_buf; … … 211 223 if (devcon->bb_buf) 212 224 return EEXIST; 213 bb_buf = malloc( size);225 bb_buf = malloc(devcon->pblock_size); 214 226 if (!bb_buf) 215 227 return ENOMEM; 216 228 217 229 fibril_mutex_lock(&devcon->com_area_lock); 218 rc = read_block (devcon, 0, size);230 rc = read_blocks(devcon, 0, 1); 219 231 if (rc != EOK) { 220 232 fibril_mutex_unlock(&devcon->com_area_lock); … … 222 234 return rc; 223 235 } 224 memcpy(bb_buf, devcon->com_area, size);236 memcpy(bb_buf, devcon->com_area, devcon->pblock_size); 225 237 fibril_mutex_unlock(&devcon->com_area_lock); 226 238 227 239 devcon->bb_buf = bb_buf; 228 devcon->bb_off = off; 229 devcon->bb_size = size; 240 devcon->bb_addr = ba; 230 241 231 242 return EOK; … … 275 286 fibril_mutex_initialize(&cache->lock); 276 287 list_initialize(&cache->free_head); 277 cache-> block_size = size;288 cache->lblock_size = size; 278 289 cache->block_count = blocks; 279 290 cache->blocks_cached = 0; 280 291 cache->mode = mode; 292 293 /* No block size translation a.t.m. */ 294 assert(cache->lblock_size == devcon->pblock_size); 281 295 282 296 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, … … 368 382 if (!b) 369 383 goto recycle; 370 b->data = malloc(cache-> block_size);384 b->data = malloc(cache->lblock_size); 371 385 if (!b->data) { 372 386 free(b); … … 400 414 fibril_mutex_lock(&devcon->com_area_lock); 401 415 memcpy(devcon->com_area, b->data, b->size); 402 rc = write_block(devcon, b->boff, 403 cache->block_size); 416 rc = write_blocks(devcon, b->boff, 1); 404 417 fibril_mutex_unlock(&devcon->com_area_lock); 405 418 if (rc != EOK) { … … 437 450 block_initialize(b); 438 451 b->dev_handle = dev_handle; 439 b->size = cache-> block_size;452 b->size = cache->lblock_size; 440 453 b->boff = boff; 441 454 hash_table_insert(&cache->block_hash, &key, &b->hash_link); … … 455 468 */ 456 469 fibril_mutex_lock(&devcon->com_area_lock); 457 rc = read_block (devcon, b->boff, cache->block_size);458 memcpy(b->data, devcon->com_area, cache-> block_size);470 rc = read_blocks(devcon, b->boff, 1); 471 memcpy(b->data, devcon->com_area, cache->lblock_size); 459 472 fibril_mutex_unlock(&devcon->com_area_lock); 460 473 if (rc != EOK) … … 510 523 fibril_mutex_lock(&devcon->com_area_lock); 511 524 memcpy(devcon->com_area, block->data, block->size); 512 rc = write_block (devcon, block->boff, block->size);525 rc = write_blocks(devcon, block->boff, 1); 513 526 fibril_mutex_unlock(&devcon->com_area_lock); 514 527 block->dirty = false; … … 588 601 */ 589 602 int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, 590 off_t *pos, void *dst, size_t size , size_t block_size)603 off_t *pos, void *dst, size_t size) 591 604 { 592 605 off_t offset = 0; 593 606 size_t left = size; 594 devcon_t *devcon = devcon_search(dev_handle); 607 size_t block_size; 608 devcon_t *devcon; 609 610 devcon = devcon_search(dev_handle); 595 611 assert(devcon); 612 block_size = devcon->pblock_size; 596 613 597 614 fibril_mutex_lock(&devcon->com_area_lock); … … 620 637 int rc; 621 638 622 rc = read_block (devcon, *pos / block_size, block_size);639 rc = read_blocks(devcon, *pos / block_size, 1); 623 640 if (rc != EOK) { 624 641 fibril_mutex_unlock(&devcon->com_area_lock); … … 635 652 } 636 653 637 /** Read block from block device.654 /** Read blocks from block device. 638 655 * 639 656 * @param devcon Device connection. 640 * @param b off Block index.641 * @param block_size Block size.657 * @param ba Address of first block. 658 * @param cnt Number of blocks. 642 659 * @param src Buffer for storing the data. 643 660 * 644 661 * @return EOK on success or negative error code on failure. 645 662 */ 646 static int read_block(devcon_t *devcon, bn_t boff, size_t block_size) 647 { 648 ipcarg_t retval; 663 static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt) 664 { 649 665 int rc; 650 666 651 667 assert(devcon); 652 rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size, 653 &retval); 654 if ((rc != EOK) || (retval != EOK)) 655 return (rc != EOK ? rc : (int) retval); 656 657 return EOK; 668 rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba), 669 UPPER32(ba), cnt); 670 return rc; 658 671 } 659 672 … … 661 674 * 662 675 * @param devcon Device connection. 663 * @param b off Block index.664 * @param block_size Block size.676 * @param ba Address of first block. 677 * @param cnt Number of blocks. 665 678 * @param src Buffer containing the data to write. 666 679 * 667 680 * @return EOK on success or negative error code on failure. 668 681 */ 669 static int write_block(devcon_t *devcon, bn_t boff, size_t block_size) 670 { 671 ipcarg_t retval; 682 static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt) 683 { 672 684 int rc; 673 685 674 686 assert(devcon); 675 rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size, 676 &retval); 677 if ((rc != EOK) || (retval != EOK)) 678 return (rc != EOK ? rc : (int) retval); 679 680 return EOK; 687 rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba), 688 UPPER32(ba), cnt); 689 return rc; 690 } 691 692 /** Get block size used by the device. */ 693 static size_t get_block_size(int dev_phone, size_t *bsize) 694 { 695 ipcarg_t bs; 696 int rc; 697 698 rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs); 699 if (rc == EOK) 700 *bsize = (size_t) bs; 701 702 return rc; 681 703 } 682 704
Note:
See TracChangeset
for help on using the changeset viewer.