[fc840d9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2008 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[97c9da8] | 30 | /** @addtogroup libblock
|
---|
[fc840d9] | 31 | * @{
|
---|
[97c9da8] | 32 | */
|
---|
[fc840d9] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[97c9da8] | 38 | #include "libblock.h"
|
---|
[fc840d9] | 39 | #include "../../srv/vfs/vfs.h"
|
---|
| 40 | #include "../../srv/rd/rd.h"
|
---|
[7858bc5f] | 41 | #include <ipc/devmap.h>
|
---|
| 42 | #include <ipc/services.h>
|
---|
[fc840d9] | 43 | #include <errno.h>
|
---|
[7858bc5f] | 44 | #include <sys/mman.h>
|
---|
[fc840d9] | 45 | #include <async.h>
|
---|
| 46 | #include <ipc/ipc.h>
|
---|
| 47 | #include <as.h>
|
---|
| 48 | #include <assert.h>
|
---|
[916bf1a] | 49 | #include <futex.h>
|
---|
| 50 | #include <libadt/list.h>
|
---|
[f1ba5d6] | 51 | #include <libadt/hash_table.h>
|
---|
[fc840d9] | 52 |
|
---|
[916bf1a] | 53 | /** Lock protecting the device connection list */
|
---|
| 54 | static futex_t dcl_lock = FUTEX_INITIALIZER;
|
---|
| 55 | /** Device connection list head. */
|
---|
| 56 | static LIST_INITIALIZE(dcl_head);
|
---|
| 57 |
|
---|
[f1ba5d6] | 58 | #define CACHE_BUCKETS_LOG2 10
|
---|
| 59 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
---|
| 60 |
|
---|
| 61 | typedef struct {
|
---|
| 62 | futex_t lock;
|
---|
| 63 | size_t block_size; /**< Block size. */
|
---|
| 64 | unsigned block_count; /**< Total number of blocks. */
|
---|
| 65 | hash_table_t block_hash;
|
---|
| 66 | link_t free_head;
|
---|
| 67 | } cache_t;
|
---|
| 68 |
|
---|
[916bf1a] | 69 | typedef struct {
|
---|
| 70 | link_t link;
|
---|
| 71 | int dev_handle;
|
---|
| 72 | int dev_phone;
|
---|
| 73 | void *com_area;
|
---|
| 74 | size_t com_size;
|
---|
| 75 | void *bb_buf;
|
---|
| 76 | off_t bb_off;
|
---|
| 77 | size_t bb_size;
|
---|
[f1ba5d6] | 78 | cache_t *cache;
|
---|
[916bf1a] | 79 | } devcon_t;
|
---|
| 80 |
|
---|
| 81 | static devcon_t *devcon_search(dev_handle_t dev_handle)
|
---|
| 82 | {
|
---|
| 83 | link_t *cur;
|
---|
| 84 |
|
---|
| 85 | futex_down(&dcl_lock);
|
---|
| 86 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
| 87 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
---|
| 88 | if (devcon->dev_handle == dev_handle) {
|
---|
| 89 | futex_up(&dcl_lock);
|
---|
| 90 | return devcon;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | futex_up(&dcl_lock);
|
---|
| 94 | return NULL;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
|
---|
[6284978] | 98 | size_t com_size)
|
---|
[916bf1a] | 99 | {
|
---|
| 100 | link_t *cur;
|
---|
| 101 | devcon_t *devcon;
|
---|
| 102 |
|
---|
| 103 | devcon = malloc(sizeof(devcon_t));
|
---|
| 104 | if (!devcon)
|
---|
| 105 | return ENOMEM;
|
---|
| 106 |
|
---|
| 107 | link_initialize(&devcon->link);
|
---|
| 108 | devcon->dev_handle = dev_handle;
|
---|
| 109 | devcon->dev_phone = dev_phone;
|
---|
| 110 | devcon->com_area = com_area;
|
---|
| 111 | devcon->com_size = com_size;
|
---|
[6284978] | 112 | devcon->bb_buf = NULL;
|
---|
| 113 | devcon->bb_off = 0;
|
---|
| 114 | devcon->bb_size = 0;
|
---|
[f1ba5d6] | 115 | devcon->cache = NULL;
|
---|
[916bf1a] | 116 |
|
---|
| 117 | futex_down(&dcl_lock);
|
---|
| 118 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
| 119 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
---|
| 120 | if (d->dev_handle == dev_handle) {
|
---|
| 121 | futex_up(&dcl_lock);
|
---|
| 122 | free(devcon);
|
---|
| 123 | return EEXIST;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | list_append(&devcon->link, &dcl_head);
|
---|
| 127 | futex_up(&dcl_lock);
|
---|
| 128 | return EOK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static void devcon_remove(devcon_t *devcon)
|
---|
| 132 | {
|
---|
| 133 | futex_down(&dcl_lock);
|
---|
| 134 | list_remove(&devcon->link);
|
---|
| 135 | futex_up(&dcl_lock);
|
---|
| 136 | }
|
---|
[7858bc5f] | 137 |
|
---|
[6284978] | 138 | int block_init(dev_handle_t dev_handle, size_t com_size)
|
---|
[7858bc5f] | 139 | {
|
---|
| 140 | int rc;
|
---|
[916bf1a] | 141 | int dev_phone;
|
---|
| 142 | void *com_area;
|
---|
| 143 |
|
---|
| 144 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
|
---|
[7858bc5f] | 145 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
---|
[916bf1a] | 146 | if (!com_area) {
|
---|
[7858bc5f] | 147 | return ENOMEM;
|
---|
| 148 | }
|
---|
[97c9da8] | 149 | dev_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP,
|
---|
[7858bc5f] | 150 | DEVMAP_CONNECT_TO_DEVICE, dev_handle);
|
---|
| 151 |
|
---|
| 152 | if (dev_phone < 0) {
|
---|
[916bf1a] | 153 | munmap(com_area, com_size);
|
---|
[7858bc5f] | 154 | return dev_phone;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[916bf1a] | 157 | rc = ipc_share_out_start(dev_phone, com_area,
|
---|
[7858bc5f] | 158 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
| 159 | if (rc != EOK) {
|
---|
[916bf1a] | 160 | munmap(com_area, com_size);
|
---|
[7858bc5f] | 161 | ipc_hangup(dev_phone);
|
---|
| 162 | return rc;
|
---|
| 163 | }
|
---|
[916bf1a] | 164 |
|
---|
[6284978] | 165 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
|
---|
[916bf1a] | 166 | if (rc != EOK) {
|
---|
| 167 | munmap(com_area, com_size);
|
---|
| 168 | ipc_hangup(dev_phone);
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[7858bc5f] | 172 | return EOK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void block_fini(dev_handle_t dev_handle)
|
---|
| 176 | {
|
---|
[916bf1a] | 177 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 178 | assert(devcon);
|
---|
| 179 |
|
---|
| 180 | devcon_remove(devcon);
|
---|
| 181 |
|
---|
[6284978] | 182 | if (devcon->bb_buf)
|
---|
| 183 | free(devcon->bb_buf);
|
---|
[f1ba5d6] | 184 |
|
---|
| 185 | if (devcon->cache) {
|
---|
| 186 | hash_table_destroy(&devcon->cache->block_hash);
|
---|
| 187 | free(devcon->cache);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[916bf1a] | 190 | munmap(devcon->com_area, devcon->com_size);
|
---|
| 191 | ipc_hangup(devcon->dev_phone);
|
---|
| 192 |
|
---|
| 193 | free(devcon);
|
---|
[7858bc5f] | 194 | }
|
---|
| 195 |
|
---|
[6284978] | 196 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
|
---|
| 197 | {
|
---|
| 198 | void *bb_buf;
|
---|
[0c243b4] | 199 | int rc;
|
---|
[6284978] | 200 |
|
---|
| 201 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 202 | if (!devcon)
|
---|
| 203 | return ENOENT;
|
---|
| 204 | if (devcon->bb_buf)
|
---|
| 205 | return EEXIST;
|
---|
| 206 | bb_buf = malloc(size);
|
---|
| 207 | if (!bb_buf)
|
---|
| 208 | return ENOMEM;
|
---|
| 209 |
|
---|
| 210 | off_t bufpos = 0;
|
---|
| 211 | size_t buflen = 0;
|
---|
[0c243b4] | 212 | rc = block_read(dev_handle, &bufpos, &buflen, &off,
|
---|
| 213 | bb_buf, size, size);
|
---|
| 214 | if (rc != EOK) {
|
---|
[6284978] | 215 | free(bb_buf);
|
---|
[0c243b4] | 216 | return rc;
|
---|
[6284978] | 217 | }
|
---|
| 218 | devcon->bb_buf = bb_buf;
|
---|
| 219 | devcon->bb_off = off;
|
---|
| 220 | devcon->bb_size = size;
|
---|
| 221 |
|
---|
| 222 | return EOK;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[7858bc5f] | 225 | void *block_bb_get(dev_handle_t dev_handle)
|
---|
| 226 | {
|
---|
[916bf1a] | 227 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 228 | assert(devcon);
|
---|
| 229 | return devcon->bb_buf;
|
---|
[7858bc5f] | 230 | }
|
---|
| 231 |
|
---|
[f1ba5d6] | 232 | static hash_index_t cache_hash(unsigned long *key)
|
---|
| 233 | {
|
---|
| 234 | return *key & (CACHE_BUCKETS - 1);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
---|
| 238 | {
|
---|
| 239 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
---|
| 240 | return b->boff == *key;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | static void cache_remove_callback(link_t *item)
|
---|
| 244 | {
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | static hash_table_operations_t cache_ops = {
|
---|
| 248 | .hash = cache_hash,
|
---|
| 249 | .compare = cache_compare,
|
---|
| 250 | .remove_callback = cache_remove_callback
|
---|
| 251 | };
|
---|
| 252 |
|
---|
| 253 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks)
|
---|
| 254 | {
|
---|
| 255 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 256 | cache_t *cache;
|
---|
| 257 | if (!devcon)
|
---|
| 258 | return ENOENT;
|
---|
| 259 | if (devcon->cache)
|
---|
| 260 | return EEXIST;
|
---|
| 261 | cache = malloc(sizeof(cache_t));
|
---|
| 262 | if (!cache)
|
---|
| 263 | return ENOMEM;
|
---|
| 264 |
|
---|
[e1c88d5] | 265 | futex_initialize(&cache->lock, 1);
|
---|
[f1ba5d6] | 266 | list_initialize(&cache->free_head);
|
---|
| 267 | cache->block_size = size;
|
---|
| 268 | cache->block_count = blocks;
|
---|
| 269 |
|
---|
| 270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
|
---|
| 271 | &cache_ops)) {
|
---|
| 272 | free(cache);
|
---|
| 273 | return ENOMEM;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | devcon->cache = cache;
|
---|
| 277 | return EOK;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[e1c88d5] | 280 | static bool cache_can_grow(cache_t *cache)
|
---|
[fc840d9] | 281 | {
|
---|
[e1c88d5] | 282 | return true;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | static void block_initialize(block_t *b)
|
---|
| 286 | {
|
---|
| 287 | futex_initialize(&b->lock, 1);
|
---|
| 288 | b->refcnt = 1;
|
---|
| 289 | b->dirty = false;
|
---|
| 290 | rwlock_initialize(&b->contents_lock);
|
---|
| 291 | link_initialize(&b->free_link);
|
---|
| 292 | link_initialize(&b->hash_link);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /** Instantiate a block in memory and get a reference to it.
|
---|
| 296 | *
|
---|
| 297 | * @param dev_handle Device handle of the block device.
|
---|
| 298 | * @param boff Block offset.
|
---|
[1d8cdb1] | 299 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
| 300 | * will not read the contents of the block from the
|
---|
| 301 | * device.
|
---|
[e1c88d5] | 302 | *
|
---|
| 303 | * @return Block structure.
|
---|
| 304 | */
|
---|
[1d8cdb1] | 305 | block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags)
|
---|
[e1c88d5] | 306 | {
|
---|
| 307 | devcon_t *devcon;
|
---|
| 308 | cache_t *cache;
|
---|
[fc840d9] | 309 | block_t *b;
|
---|
[e1c88d5] | 310 | link_t *l;
|
---|
| 311 | unsigned long key = boff;
|
---|
| 312 |
|
---|
| 313 | devcon = devcon_search(dev_handle);
|
---|
[fc840d9] | 314 |
|
---|
[e1c88d5] | 315 | assert(devcon);
|
---|
| 316 | assert(devcon->cache);
|
---|
[fc840d9] | 317 |
|
---|
[e1c88d5] | 318 | cache = devcon->cache;
|
---|
| 319 | futex_down(&cache->lock);
|
---|
| 320 | l = hash_table_find(&cache->block_hash, &key);
|
---|
| 321 | if (l) {
|
---|
| 322 | /*
|
---|
| 323 | * We found the block in the cache.
|
---|
| 324 | */
|
---|
| 325 | b = hash_table_get_instance(l, block_t, hash_link);
|
---|
| 326 | futex_down(&b->lock);
|
---|
| 327 | if (b->refcnt++ == 0)
|
---|
| 328 | list_remove(&b->free_link);
|
---|
| 329 | futex_up(&b->lock);
|
---|
[a6d97fb9] | 330 | futex_up(&cache->lock);
|
---|
[e1c88d5] | 331 | } else {
|
---|
| 332 | /*
|
---|
| 333 | * The block was not found in the cache.
|
---|
| 334 | */
|
---|
| 335 | int rc;
|
---|
| 336 | off_t bufpos = 0;
|
---|
| 337 | size_t buflen = 0;
|
---|
| 338 | off_t pos = boff * cache->block_size;
|
---|
[a6d97fb9] | 339 | bool sync = false;
|
---|
[e1c88d5] | 340 |
|
---|
| 341 | if (cache_can_grow(cache)) {
|
---|
| 342 | /*
|
---|
| 343 | * We can grow the cache by allocating new blocks.
|
---|
| 344 | * Should the allocation fail, we fail over and try to
|
---|
| 345 | * recycle a block from the cache.
|
---|
| 346 | */
|
---|
| 347 | b = malloc(sizeof(block_t));
|
---|
| 348 | if (!b)
|
---|
| 349 | goto recycle;
|
---|
| 350 | b->data = malloc(cache->block_size);
|
---|
| 351 | if (!b->data) {
|
---|
| 352 | free(b);
|
---|
| 353 | goto recycle;
|
---|
| 354 | }
|
---|
| 355 | } else {
|
---|
| 356 | /*
|
---|
| 357 | * Try to recycle a block from the free list.
|
---|
| 358 | */
|
---|
| 359 | unsigned long temp_key;
|
---|
| 360 | recycle:
|
---|
| 361 | assert(!list_empty(&cache->free_head));
|
---|
| 362 | l = cache->free_head.next;
|
---|
| 363 | list_remove(l);
|
---|
| 364 | b = hash_table_get_instance(l, block_t, hash_link);
|
---|
[a6d97fb9] | 365 | sync = b->dirty;
|
---|
[e1c88d5] | 366 | temp_key = b->boff;
|
---|
| 367 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
---|
| 368 | }
|
---|
[fc840d9] | 369 |
|
---|
[e1c88d5] | 370 | block_initialize(b);
|
---|
| 371 | b->dev_handle = dev_handle;
|
---|
| 372 | b->size = cache->block_size;
|
---|
| 373 | b->boff = boff;
|
---|
[a6d97fb9] | 374 | hash_table_insert(&cache->block_hash, &key, &b->hash_link);
|
---|
| 375 |
|
---|
| 376 | /*
|
---|
| 377 | * Lock the block before releasing the cache lock. Thus we don't
|
---|
| 378 | * kill concurent operations on the cache while doing I/O on the
|
---|
| 379 | * block.
|
---|
| 380 | */
|
---|
| 381 | futex_down(&b->lock);
|
---|
| 382 | futex_up(&cache->lock);
|
---|
| 383 |
|
---|
| 384 | if (sync) {
|
---|
| 385 | /*
|
---|
| 386 | * The block is dirty and needs to be written back to
|
---|
| 387 | * the device before we can read in the new contents.
|
---|
| 388 | */
|
---|
| 389 | abort(); /* TODO: block_write() */
|
---|
| 390 | }
|
---|
[1d8cdb1] | 391 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
---|
| 392 | /*
|
---|
| 393 | * The block contains old or no data. We need to read
|
---|
| 394 | * the new contents from the device.
|
---|
| 395 | */
|
---|
| 396 | rc = block_read(dev_handle, &bufpos, &buflen, &pos,
|
---|
| 397 | b->data, cache->block_size, cache->block_size);
|
---|
| 398 | assert(rc == EOK);
|
---|
| 399 | }
|
---|
[fc840d9] | 400 |
|
---|
[a6d97fb9] | 401 | futex_up(&b->lock);
|
---|
| 402 | }
|
---|
[fc840d9] | 403 | return b;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[d5a720cf] | 406 | /** Release a reference to a block.
|
---|
| 407 | *
|
---|
[a6d97fb9] | 408 | * If the last reference is dropped, the block is put on the free list.
|
---|
[d5a720cf] | 409 | *
|
---|
| 410 | * @param block Block of which a reference is to be released.
|
---|
| 411 | */
|
---|
[fc840d9] | 412 | void block_put(block_t *block)
|
---|
| 413 | {
|
---|
[d5a720cf] | 414 | devcon_t *devcon = devcon_search(block->dev_handle);
|
---|
| 415 | cache_t *cache;
|
---|
| 416 |
|
---|
| 417 | assert(devcon);
|
---|
| 418 | assert(devcon->cache);
|
---|
| 419 |
|
---|
| 420 | cache = devcon->cache;
|
---|
| 421 | futex_down(&cache->lock);
|
---|
| 422 | futex_down(&block->lock);
|
---|
| 423 | if (!--block->refcnt) {
|
---|
| 424 | /*
|
---|
| 425 | * Last reference to the block was dropped, put the block on the
|
---|
| 426 | * free list.
|
---|
| 427 | */
|
---|
| 428 | list_append(&block->free_link, &cache->free_head);
|
---|
| 429 | }
|
---|
| 430 | futex_up(&block->lock);
|
---|
[a6d97fb9] | 431 | futex_up(&cache->lock);
|
---|
[d5a720cf] | 432 | }
|
---|
| 433 |
|
---|
| 434 | /** Read data from a block device.
|
---|
| 435 | *
|
---|
| 436 | * @param dev_handle Device handle of the block device.
|
---|
| 437 | * @param bufpos Pointer to the first unread valid offset within the
|
---|
| 438 | * communication buffer.
|
---|
| 439 | * @param buflen Pointer to the number of unread bytes that are ready in
|
---|
| 440 | * the communication buffer.
|
---|
| 441 | * @param pos Device position to be read.
|
---|
| 442 | * @param dst Destination buffer.
|
---|
| 443 | * @param size Size of the destination buffer.
|
---|
| 444 | * @param block_size Block size to be used for the transfer.
|
---|
| 445 | *
|
---|
| 446 | * @return EOK on success or a negative return code on failure.
|
---|
| 447 | */
|
---|
| 448 | int
|
---|
| 449 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
|
---|
| 450 | size_t size, size_t block_size)
|
---|
| 451 | {
|
---|
| 452 | off_t offset = 0;
|
---|
| 453 | size_t left = size;
|
---|
| 454 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 455 | assert(devcon);
|
---|
[e1c88d5] | 456 |
|
---|
[d5a720cf] | 457 | while (left > 0) {
|
---|
| 458 | size_t rd;
|
---|
| 459 |
|
---|
| 460 | if (*bufpos + left < *buflen)
|
---|
| 461 | rd = left;
|
---|
| 462 | else
|
---|
| 463 | rd = *buflen - *bufpos;
|
---|
| 464 |
|
---|
| 465 | if (rd > 0) {
|
---|
| 466 | /*
|
---|
| 467 | * Copy the contents of the communication buffer to the
|
---|
| 468 | * destination buffer.
|
---|
| 469 | */
|
---|
| 470 | memcpy(dst + offset, devcon->com_area + *bufpos, rd);
|
---|
| 471 | offset += rd;
|
---|
| 472 | *bufpos += rd;
|
---|
| 473 | *pos += rd;
|
---|
| 474 | left -= rd;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | if (*bufpos == *buflen) {
|
---|
| 478 | /* Refill the communication buffer with a new block. */
|
---|
| 479 | ipcarg_t retval;
|
---|
| 480 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
|
---|
| 481 | *pos / block_size, block_size, &retval);
|
---|
| 482 | if ((rc != EOK) || (retval != EOK))
|
---|
| 483 | return (rc != EOK ? rc : retval);
|
---|
| 484 |
|
---|
| 485 | *bufpos = 0;
|
---|
| 486 | *buflen = block_size;
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | return EOK;
|
---|
[fc840d9] | 491 | }
|
---|
| 492 |
|
---|
| 493 | /** @}
|
---|
| 494 | */
|
---|