Changeset cfa8738 in mainline for uspace/lib/libblock/libblock.c
- Timestamp:
- 2009-08-30T22:30:42Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d061efe
- Parents:
- ff62c6d (diff), a830611 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/libblock/libblock.c
rff62c6d rcfa8738 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. */ … … 74 75 dev_handle_t dev_handle; 75 76 int dev_phone; 76 fibril_mutex_t com _area_lock;77 void *com _area;78 size_t com _size;77 fibril_mutex_t comm_area_lock; 78 void *comm_area; 79 size_t comm_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 *comm_area, size_t comm_size) 106 108 { 107 109 link_t *cur; 108 110 devcon_t *devcon; 111 112 if (comm_size < bsize) 113 return EINVAL; 109 114 110 115 devcon = malloc(sizeof(devcon_t)); … … 115 120 devcon->dev_handle = dev_handle; 116 121 devcon->dev_phone = dev_phone; 117 fibril_mutex_initialize(&devcon->com _area_lock);118 devcon->com _area = com_area;119 devcon->com _size = com_size;122 fibril_mutex_initialize(&devcon->comm_area_lock); 123 devcon->comm_area = comm_area; 124 devcon->comm_size = comm_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 … … 144 149 } 145 150 146 int block_init(dev_handle_t dev_handle, size_t com _size)151 int block_init(dev_handle_t dev_handle, size_t comm_size) 147 152 { 148 153 int rc; 149 154 int dev_phone; 150 void *com_area; 151 152 com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, 155 void *comm_area; 156 size_t bsize; 157 158 comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE, 153 159 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); 154 if (!com _area) {160 if (!comm_area) { 155 161 return ENOMEM; 156 162 } … … 158 164 dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING); 159 165 if (dev_phone < 0) { 160 munmap(com _area, com_size);166 munmap(comm_area, comm_size); 161 167 return dev_phone; 162 168 } 163 169 164 rc = ipc_share_out_start(dev_phone, com _area,170 rc = ipc_share_out_start(dev_phone, comm_area, 165 171 AS_AREA_READ | AS_AREA_WRITE); 166 172 if (rc != EOK) { 167 munmap(com _area, com_size);173 munmap(comm_area, comm_size); 168 174 ipc_hangup(dev_phone); 169 175 return rc; 170 176 } 177 178 if (get_block_size(dev_phone, &bsize) != EOK) { 179 munmap(comm_area, comm_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, comm_area, comm_size); 173 185 if (rc != EOK) { 174 munmap(com _area, com_size);186 munmap(comm_area, comm_size); 175 187 ipc_hangup(dev_phone); 176 188 return rc; … … 195 207 } 196 208 197 munmap(devcon->com _area, devcon->com_size);209 munmap(devcon->comm_area, devcon->comm_size); 198 210 ipc_hangup(devcon->dev_phone); 199 211 … … 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 217 fibril_mutex_lock(&devcon->com _area_lock);218 rc = read_block (devcon, 0, size);228 229 fibril_mutex_lock(&devcon->comm_area_lock); 230 rc = read_blocks(devcon, 0, 1); 219 231 if (rc != EOK) { 220 fibril_mutex_unlock(&devcon->com _area_lock);232 fibril_mutex_unlock(&devcon->comm_area_lock); 221 233 free(bb_buf); 222 234 return rc; 223 235 } 224 memcpy(bb_buf, devcon->com _area,size);225 fibril_mutex_unlock(&devcon->com _area_lock);236 memcpy(bb_buf, devcon->comm_area, devcon->pblock_size); 237 fibril_mutex_unlock(&devcon->comm_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); … … 398 412 list_append(&b->free_link, &cache->free_head); 399 413 fibril_mutex_unlock(&cache->lock); 400 fibril_mutex_lock(&devcon->com_area_lock); 401 memcpy(devcon->com_area, b->data, b->size); 402 rc = write_block(devcon, b->boff, 403 cache->block_size); 404 fibril_mutex_unlock(&devcon->com_area_lock); 414 fibril_mutex_lock(&devcon->comm_area_lock); 415 memcpy(devcon->comm_area, b->data, b->size); 416 rc = write_blocks(devcon, b->boff, 1); 417 fibril_mutex_unlock(&devcon->comm_area_lock); 405 418 if (rc != EOK) { 406 419 /* … … 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); … … 454 467 * the new contents from the device. 455 468 */ 456 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);459 fibril_mutex_unlock(&devcon->com _area_lock);469 fibril_mutex_lock(&devcon->comm_area_lock); 470 rc = read_blocks(devcon, b->boff, 1); 471 memcpy(b->data, devcon->comm_area, cache->lblock_size); 472 fibril_mutex_unlock(&devcon->comm_area_lock); 460 473 if (rc != EOK) 461 474 b->toxic = true; … … 508 521 if (block->dirty && (block->refcnt == 1) && 509 522 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) { 510 fibril_mutex_lock(&devcon->com _area_lock);511 memcpy(devcon->com _area, block->data, block->size);512 rc = write_block (devcon, block->boff, block->size);513 fibril_mutex_unlock(&devcon->com _area_lock);523 fibril_mutex_lock(&devcon->comm_area_lock); 524 memcpy(devcon->comm_area, block->data, block->size); 525 rc = write_blocks(devcon, block->boff, 1); 526 fibril_mutex_unlock(&devcon->comm_area_lock); 514 527 block->dirty = false; 515 528 } … … 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 fibril_mutex_lock(&devcon->com _area_lock);614 fibril_mutex_lock(&devcon->comm_area_lock); 598 615 while (left > 0) { 599 616 size_t rd; … … 609 626 * destination buffer. 610 627 */ 611 memcpy(dst + offset, devcon->com _area + *bufpos, rd);628 memcpy(dst + offset, devcon->comm_area + *bufpos, rd); 612 629 offset += rd; 613 630 *bufpos += rd; … … 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 fibril_mutex_unlock(&devcon->com _area_lock);641 fibril_mutex_unlock(&devcon->comm_area_lock); 625 642 return rc; 626 643 } … … 630 647 } 631 648 } 632 fibril_mutex_unlock(&devcon->com _area_lock);649 fibril_mutex_unlock(&devcon->comm_area_lock); 633 650 634 651 return EOK; 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.