source: mainline/uspace/lib/libblock/libblock.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 08232ee, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Obtain block device size automatically, if possible. Implement ftell(). Fix seek implementation in VFS if whence == SEEK_END.

  • Property mode set to 100644
File size: 20.0 KB
RevLine 
[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"
[7858bc5f]40#include <ipc/devmap.h>
[c5747fe]41#include <ipc/bd.h>
[7858bc5f]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>
[1e4cada]49#include <fibril_synch.h>
[d9c8c81]50#include <adt/list.h>
51#include <adt/hash_table.h>
[1ee00b7]52#include <macros.h>
[d00ae4c]53#include <mem.h>
[fc840d9]54
[916bf1a]55/** Lock protecting the device connection list */
[4e1b57d]56static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
[916bf1a]57/** Device connection list head. */
58static LIST_INITIALIZE(dcl_head);
59
[f1ba5d6]60#define CACHE_BUCKETS_LOG2 10
61#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
62
63typedef struct {
[4e1b57d]64 fibril_mutex_t lock;
[1ee00b7]65 size_t lblock_size; /**< Logical block size. */
[f1ba5d6]66 unsigned block_count; /**< Total number of blocks. */
[d68e4d5]67 unsigned blocks_cached; /**< Number of cached blocks. */
[f1ba5d6]68 hash_table_t block_hash;
69 link_t free_head;
[1fbe064b]70 enum cache_mode mode;
[f1ba5d6]71} cache_t;
72
[916bf1a]73typedef struct {
74 link_t link;
[ad8fc510]75 dev_handle_t dev_handle;
[916bf1a]76 int dev_phone;
[a830611]77 fibril_mutex_t comm_area_lock;
78 void *comm_area;
79 size_t comm_size;
[916bf1a]80 void *bb_buf;
[1ee00b7]81 bn_t bb_addr;
82 size_t pblock_size; /**< Physical block size. */
[f1ba5d6]83 cache_t *cache;
[916bf1a]84} devcon_t;
85
[1ee00b7]86static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
87static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
[00b1d20e]88static int get_block_size(int dev_phone, size_t *bsize);
[08232ee]89static int get_num_blocks(int dev_phone, bn_t *nblocks);
[1fbe064b]90
[916bf1a]91static devcon_t *devcon_search(dev_handle_t dev_handle)
92{
93 link_t *cur;
94
[4e1b57d]95 fibril_mutex_lock(&dcl_lock);
[916bf1a]96 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
97 devcon_t *devcon = list_get_instance(cur, devcon_t, link);
98 if (devcon->dev_handle == dev_handle) {
[4e1b57d]99 fibril_mutex_unlock(&dcl_lock);
[916bf1a]100 return devcon;
101 }
102 }
[4e1b57d]103 fibril_mutex_unlock(&dcl_lock);
[916bf1a]104 return NULL;
105}
106
[1ee00b7]107static int devcon_add(dev_handle_t dev_handle, int dev_phone, size_t bsize,
[a830611]108 void *comm_area, size_t comm_size)
[916bf1a]109{
110 link_t *cur;
111 devcon_t *devcon;
112
[a830611]113 if (comm_size < bsize)
[1ee00b7]114 return EINVAL;
115
[916bf1a]116 devcon = malloc(sizeof(devcon_t));
117 if (!devcon)
118 return ENOMEM;
119
120 link_initialize(&devcon->link);
121 devcon->dev_handle = dev_handle;
122 devcon->dev_phone = dev_phone;
[a830611]123 fibril_mutex_initialize(&devcon->comm_area_lock);
124 devcon->comm_area = comm_area;
125 devcon->comm_size = comm_size;
[6284978]126 devcon->bb_buf = NULL;
[1ee00b7]127 devcon->bb_addr = 0;
128 devcon->pblock_size = bsize;
[f1ba5d6]129 devcon->cache = NULL;
[916bf1a]130
[4e1b57d]131 fibril_mutex_lock(&dcl_lock);
[916bf1a]132 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
133 devcon_t *d = list_get_instance(cur, devcon_t, link);
134 if (d->dev_handle == dev_handle) {
[4e1b57d]135 fibril_mutex_unlock(&dcl_lock);
[916bf1a]136 free(devcon);
137 return EEXIST;
138 }
139 }
140 list_append(&devcon->link, &dcl_head);
[4e1b57d]141 fibril_mutex_unlock(&dcl_lock);
[916bf1a]142 return EOK;
143}
144
145static void devcon_remove(devcon_t *devcon)
146{
[4e1b57d]147 fibril_mutex_lock(&dcl_lock);
[916bf1a]148 list_remove(&devcon->link);
[4e1b57d]149 fibril_mutex_unlock(&dcl_lock);
[916bf1a]150}
[7858bc5f]151
[a830611]152int block_init(dev_handle_t dev_handle, size_t comm_size)
[7858bc5f]153{
154 int rc;
[916bf1a]155 int dev_phone;
[a830611]156 void *comm_area;
[1ee00b7]157 size_t bsize;
158
[a830611]159 comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
[7858bc5f]160 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
[a830611]161 if (!comm_area) {
[7858bc5f]162 return ENOMEM;
163 }
164
[1090b8c]165 dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
[7858bc5f]166 if (dev_phone < 0) {
[a830611]167 munmap(comm_area, comm_size);
[7858bc5f]168 return dev_phone;
169 }
170
[0da4e41]171 rc = async_share_out_start(dev_phone, comm_area,
[7858bc5f]172 AS_AREA_READ | AS_AREA_WRITE);
173 if (rc != EOK) {
[a830611]174 munmap(comm_area, comm_size);
[7858bc5f]175 ipc_hangup(dev_phone);
176 return rc;
177 }
[1ee00b7]178
179 if (get_block_size(dev_phone, &bsize) != EOK) {
[a830611]180 munmap(comm_area, comm_size);
[1ee00b7]181 ipc_hangup(dev_phone);
182 return rc;
183 }
[916bf1a]184
[a830611]185 rc = devcon_add(dev_handle, dev_phone, bsize, comm_area, comm_size);
[916bf1a]186 if (rc != EOK) {
[a830611]187 munmap(comm_area, comm_size);
[916bf1a]188 ipc_hangup(dev_phone);
189 return rc;
190 }
191
[7858bc5f]192 return EOK;
193}
194
195void block_fini(dev_handle_t dev_handle)
196{
[916bf1a]197 devcon_t *devcon = devcon_search(dev_handle);
198 assert(devcon);
199
200 devcon_remove(devcon);
201
[6284978]202 if (devcon->bb_buf)
203 free(devcon->bb_buf);
[f1ba5d6]204
205 if (devcon->cache) {
206 hash_table_destroy(&devcon->cache->block_hash);
207 free(devcon->cache);
208 }
209
[a830611]210 munmap(devcon->comm_area, devcon->comm_size);
[916bf1a]211 ipc_hangup(devcon->dev_phone);
212
213 free(devcon);
[7858bc5f]214}
215
[1ee00b7]216int block_bb_read(dev_handle_t dev_handle, bn_t ba)
[6284978]217{
218 void *bb_buf;
[0c243b4]219 int rc;
[6284978]220
221 devcon_t *devcon = devcon_search(dev_handle);
222 if (!devcon)
223 return ENOENT;
224 if (devcon->bb_buf)
225 return EEXIST;
[1ee00b7]226 bb_buf = malloc(devcon->pblock_size);
[6284978]227 if (!bb_buf)
228 return ENOMEM;
[1ee00b7]229
[a830611]230 fibril_mutex_lock(&devcon->comm_area_lock);
[1ee00b7]231 rc = read_blocks(devcon, 0, 1);
[0c243b4]232 if (rc != EOK) {
[a830611]233 fibril_mutex_unlock(&devcon->comm_area_lock);
[6284978]234 free(bb_buf);
[0c243b4]235 return rc;
[6284978]236 }
[a830611]237 memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
238 fibril_mutex_unlock(&devcon->comm_area_lock);
[6408be3]239
[6284978]240 devcon->bb_buf = bb_buf;
[1ee00b7]241 devcon->bb_addr = ba;
[6284978]242
243 return EOK;
244}
245
[7858bc5f]246void *block_bb_get(dev_handle_t dev_handle)
247{
[916bf1a]248 devcon_t *devcon = devcon_search(dev_handle);
249 assert(devcon);
250 return devcon->bb_buf;
[7858bc5f]251}
252
[f1ba5d6]253static hash_index_t cache_hash(unsigned long *key)
254{
255 return *key & (CACHE_BUCKETS - 1);
256}
257
258static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
259{
260 block_t *b = hash_table_get_instance(item, block_t, hash_link);
261 return b->boff == *key;
262}
263
264static void cache_remove_callback(link_t *item)
265{
266}
267
268static hash_table_operations_t cache_ops = {
269 .hash = cache_hash,
270 .compare = cache_compare,
271 .remove_callback = cache_remove_callback
272};
273
[1fbe064b]274int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
275 enum cache_mode mode)
[f1ba5d6]276{
277 devcon_t *devcon = devcon_search(dev_handle);
278 cache_t *cache;
279 if (!devcon)
280 return ENOENT;
281 if (devcon->cache)
282 return EEXIST;
283 cache = malloc(sizeof(cache_t));
284 if (!cache)
285 return ENOMEM;
286
[4e1b57d]287 fibril_mutex_initialize(&cache->lock);
[f1ba5d6]288 list_initialize(&cache->free_head);
[1ee00b7]289 cache->lblock_size = size;
[f1ba5d6]290 cache->block_count = blocks;
[d68e4d5]291 cache->blocks_cached = 0;
[1fbe064b]292 cache->mode = mode;
[f1ba5d6]293
[1ee00b7]294 /* No block size translation a.t.m. */
295 assert(cache->lblock_size == devcon->pblock_size);
296
[f1ba5d6]297 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
298 &cache_ops)) {
299 free(cache);
300 return ENOMEM;
301 }
302
303 devcon->cache = cache;
304 return EOK;
305}
306
[d68e4d5]307#define CACHE_LO_WATERMARK 10
308#define CACHE_HI_WATERMARK 20
[e1c88d5]309static bool cache_can_grow(cache_t *cache)
[fc840d9]310{
[d68e4d5]311 if (cache->blocks_cached < CACHE_LO_WATERMARK)
312 return true;
313 if (!list_empty(&cache->free_head))
314 return false;
[e1c88d5]315 return true;
316}
317
318static void block_initialize(block_t *b)
319{
[4e1b57d]320 fibril_mutex_initialize(&b->lock);
[e1c88d5]321 b->refcnt = 1;
322 b->dirty = false;
[cd688d9]323 b->toxic = false;
[4e1b57d]324 fibril_rwlock_initialize(&b->contents_lock);
[e1c88d5]325 link_initialize(&b->free_link);
326 link_initialize(&b->hash_link);
327}
328
329/** Instantiate a block in memory and get a reference to it.
330 *
[c91f2d1b]331 * @param block Pointer to where the function will store the
332 * block pointer on success.
[e1c88d5]333 * @param dev_handle Device handle of the block device.
334 * @param boff Block offset.
[1d8cdb1]335 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
336 * will not read the contents of the block from the
337 * device.
[e1c88d5]338 *
[c91f2d1b]339 * @return EOK on success or a negative error code.
[e1c88d5]340 */
[c91f2d1b]341int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
[e1c88d5]342{
343 devcon_t *devcon;
344 cache_t *cache;
[fc840d9]345 block_t *b;
[e1c88d5]346 link_t *l;
347 unsigned long key = boff;
[b7b3fda]348 int rc;
[e1c88d5]349
350 devcon = devcon_search(dev_handle);
[fc840d9]351
[e1c88d5]352 assert(devcon);
353 assert(devcon->cache);
[fc840d9]354
[e1c88d5]355 cache = devcon->cache;
[02ee6bf5]356
357retry:
[b7b3fda]358 rc = EOK;
[4f690cd]359 b = NULL;
[b7b3fda]360
[4e1b57d]361 fibril_mutex_lock(&cache->lock);
[e1c88d5]362 l = hash_table_find(&cache->block_hash, &key);
363 if (l) {
364 /*
365 * We found the block in the cache.
366 */
367 b = hash_table_get_instance(l, block_t, hash_link);
[4e1b57d]368 fibril_mutex_lock(&b->lock);
[e1c88d5]369 if (b->refcnt++ == 0)
370 list_remove(&b->free_link);
[402a18f]371 if (b->toxic)
372 rc = EIO;
[4e1b57d]373 fibril_mutex_unlock(&b->lock);
374 fibril_mutex_unlock(&cache->lock);
[e1c88d5]375 } else {
376 /*
377 * The block was not found in the cache.
378 */
379 if (cache_can_grow(cache)) {
380 /*
381 * We can grow the cache by allocating new blocks.
382 * Should the allocation fail, we fail over and try to
383 * recycle a block from the cache.
384 */
385 b = malloc(sizeof(block_t));
386 if (!b)
387 goto recycle;
[1ee00b7]388 b->data = malloc(cache->lblock_size);
[e1c88d5]389 if (!b->data) {
390 free(b);
391 goto recycle;
392 }
[d68e4d5]393 cache->blocks_cached++;
[e1c88d5]394 } else {
395 /*
396 * Try to recycle a block from the free list.
397 */
398 unsigned long temp_key;
399recycle:
[7a56b1ed]400 if (list_empty(&cache->free_head)) {
401 fibril_mutex_unlock(&cache->lock);
402 rc = ENOMEM;
403 goto out;
404 }
[e1c88d5]405 l = cache->free_head.next;
[d68e4d5]406 b = list_get_instance(l, block_t, free_link);
[02ee6bf5]407
408 fibril_mutex_lock(&b->lock);
409 if (b->dirty) {
410 /*
411 * The block needs to be written back to the
412 * device before it changes identity. Do this
413 * while not holding the cache lock so that
414 * concurrency is not impeded. Also move the
415 * block to the end of the free list so that we
416 * do not slow down other instances of
417 * block_get() draining the free list.
418 */
419 list_remove(&b->free_link);
420 list_append(&b->free_link, &cache->free_head);
421 fibril_mutex_unlock(&cache->lock);
[a830611]422 fibril_mutex_lock(&devcon->comm_area_lock);
423 memcpy(devcon->comm_area, b->data, b->size);
[1ee00b7]424 rc = write_blocks(devcon, b->boff, 1);
[a830611]425 fibril_mutex_unlock(&devcon->comm_area_lock);
[402a18f]426 if (rc != EOK) {
427 /*
428 * We did not manage to write the block
429 * to the device. Keep it around for
430 * another try. Hopefully, we will grab
431 * another block next time.
432 */
433 fibril_mutex_unlock(&b->lock);
434 goto retry;
435 }
[02ee6bf5]436 b->dirty = false;
437 if (!fibril_mutex_trylock(&cache->lock)) {
438 /*
439 * Somebody is probably racing with us.
440 * Unlock the block and retry.
441 */
442 fibril_mutex_unlock(&b->lock);
443 goto retry;
444 }
445
446 }
447 fibril_mutex_unlock(&b->lock);
448
449 /*
450 * Unlink the block from the free list and the hash
451 * table.
452 */
453 list_remove(&b->free_link);
[e1c88d5]454 temp_key = b->boff;
455 hash_table_remove(&cache->block_hash, &temp_key, 1);
456 }
[fc840d9]457
[e1c88d5]458 block_initialize(b);
459 b->dev_handle = dev_handle;
[1ee00b7]460 b->size = cache->lblock_size;
[e1c88d5]461 b->boff = boff;
[a6d97fb9]462 hash_table_insert(&cache->block_hash, &key, &b->hash_link);
463
464 /*
465 * Lock the block before releasing the cache lock. Thus we don't
[5ac8918]466 * kill concurrent operations on the cache while doing I/O on
467 * the block.
[a6d97fb9]468 */
[4e1b57d]469 fibril_mutex_lock(&b->lock);
470 fibril_mutex_unlock(&cache->lock);
[a6d97fb9]471
[1d8cdb1]472 if (!(flags & BLOCK_FLAGS_NOREAD)) {
473 /*
474 * The block contains old or no data. We need to read
475 * the new contents from the device.
476 */
[a830611]477 fibril_mutex_lock(&devcon->comm_area_lock);
[1ee00b7]478 rc = read_blocks(devcon, b->boff, 1);
[a830611]479 memcpy(b->data, devcon->comm_area, cache->lblock_size);
480 fibril_mutex_unlock(&devcon->comm_area_lock);
[402a18f]481 if (rc != EOK)
482 b->toxic = true;
483 } else
484 rc = EOK;
[fc840d9]485
[4e1b57d]486 fibril_mutex_unlock(&b->lock);
[a6d97fb9]487 }
[7a56b1ed]488out:
[4f690cd]489 if ((rc != EOK) && b) {
490 assert(b->toxic);
491 (void) block_put(b);
492 b = NULL;
493 }
[c91f2d1b]494 *block = b;
[402a18f]495 return rc;
[fc840d9]496}
497
[d5a720cf]498/** Release a reference to a block.
499 *
[a6d97fb9]500 * If the last reference is dropped, the block is put on the free list.
[d5a720cf]501 *
502 * @param block Block of which a reference is to be released.
[c91f2d1b]503 *
504 * @return EOK on success or a negative error code.
[d5a720cf]505 */
[c91f2d1b]506int block_put(block_t *block)
[fc840d9]507{
[d5a720cf]508 devcon_t *devcon = devcon_search(block->dev_handle);
509 cache_t *cache;
[ddfc39a3]510 unsigned blocks_cached;
511 enum cache_mode mode;
[402a18f]512 int rc = EOK;
[d5a720cf]513
514 assert(devcon);
515 assert(devcon->cache);
516
517 cache = devcon->cache;
[ddfc39a3]518
519retry:
520 fibril_mutex_lock(&cache->lock);
521 blocks_cached = cache->blocks_cached;
522 mode = cache->mode;
523 fibril_mutex_unlock(&cache->lock);
524
525 /*
526 * Determine whether to sync the block. Syncing the block is best done
527 * when not holding the cache lock as it does not impede concurrency.
528 * Since the situation may have changed when we unlocked the cache, the
529 * blocks_cached and mode variables are mere hints. We will recheck the
530 * conditions later when the cache lock is held again.
531 */
532 fibril_mutex_lock(&block->lock);
[402a18f]533 if (block->toxic)
534 block->dirty = false; /* will not write back toxic block */
[ddfc39a3]535 if (block->dirty && (block->refcnt == 1) &&
536 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
[a830611]537 fibril_mutex_lock(&devcon->comm_area_lock);
538 memcpy(devcon->comm_area, block->data, block->size);
[1ee00b7]539 rc = write_blocks(devcon, block->boff, 1);
[a830611]540 fibril_mutex_unlock(&devcon->comm_area_lock);
[ddfc39a3]541 block->dirty = false;
542 }
543 fibril_mutex_unlock(&block->lock);
544
[4e1b57d]545 fibril_mutex_lock(&cache->lock);
546 fibril_mutex_lock(&block->lock);
[d5a720cf]547 if (!--block->refcnt) {
548 /*
[d68e4d5]549 * Last reference to the block was dropped. Either free the
[402a18f]550 * block or put it on the free list. In case of an I/O error,
551 * free the block.
[d68e4d5]552 */
[402a18f]553 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
554 (rc != EOK)) {
[d68e4d5]555 /*
[402a18f]556 * Currently there are too many cached blocks or there
557 * was an I/O error when writing the block back to the
558 * device.
[d68e4d5]559 */
560 if (block->dirty) {
[ddfc39a3]561 /*
562 * We cannot sync the block while holding the
563 * cache lock. Release everything and retry.
564 */
565 block->refcnt++;
566 fibril_mutex_unlock(&block->lock);
567 fibril_mutex_unlock(&cache->lock);
568 goto retry;
[d68e4d5]569 }
570 /*
571 * Take the block out of the cache and free it.
572 */
573 unsigned long key = block->boff;
574 hash_table_remove(&cache->block_hash, &key, 1);
575 free(block);
576 free(block->data);
577 cache->blocks_cached--;
578 fibril_mutex_unlock(&cache->lock);
[402a18f]579 return rc;
[d68e4d5]580 }
581 /*
582 * Put the block on the free list.
[d5a720cf]583 */
[1fbe064b]584 if (cache->mode != CACHE_MODE_WB && block->dirty) {
[ddfc39a3]585 /*
586 * We cannot sync the block while holding the cache
587 * lock. Release everything and retry.
588 */
589 block->refcnt++;
590 fibril_mutex_unlock(&block->lock);
591 fibril_mutex_unlock(&cache->lock);
592 goto retry;
[1fbe064b]593 }
[ddfc39a3]594 list_append(&block->free_link, &cache->free_head);
[d5a720cf]595 }
[4e1b57d]596 fibril_mutex_unlock(&block->lock);
597 fibril_mutex_unlock(&cache->lock);
[c91f2d1b]598
[402a18f]599 return rc;
[d5a720cf]600}
601
[6408be3]602/** Read sequential data from a block device.
[d5a720cf]603 *
604 * @param dev_handle Device handle of the block device.
605 * @param bufpos Pointer to the first unread valid offset within the
606 * communication buffer.
607 * @param buflen Pointer to the number of unread bytes that are ready in
608 * the communication buffer.
609 * @param pos Device position to be read.
610 * @param dst Destination buffer.
611 * @param size Size of the destination buffer.
612 * @param block_size Block size to be used for the transfer.
613 *
614 * @return EOK on success or a negative return code on failure.
615 */
[6408be3]616int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
[1ee00b7]617 off_t *pos, void *dst, size_t size)
[d5a720cf]618{
619 off_t offset = 0;
620 size_t left = size;
[1ee00b7]621 size_t block_size;
622 devcon_t *devcon;
623
624 devcon = devcon_search(dev_handle);
[d5a720cf]625 assert(devcon);
[1ee00b7]626 block_size = devcon->pblock_size;
[e1c88d5]627
[a830611]628 fibril_mutex_lock(&devcon->comm_area_lock);
[d5a720cf]629 while (left > 0) {
630 size_t rd;
631
632 if (*bufpos + left < *buflen)
633 rd = left;
634 else
635 rd = *buflen - *bufpos;
636
637 if (rd > 0) {
638 /*
639 * Copy the contents of the communication buffer to the
640 * destination buffer.
641 */
[a830611]642 memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
[d5a720cf]643 offset += rd;
644 *bufpos += rd;
645 *pos += rd;
646 left -= rd;
647 }
648
[62140db]649 if (*bufpos == (off_t) *buflen) {
[d5a720cf]650 /* Refill the communication buffer with a new block. */
[6408be3]651 int rc;
652
[1ee00b7]653 rc = read_blocks(devcon, *pos / block_size, 1);
[d68e4d5]654 if (rc != EOK) {
[a830611]655 fibril_mutex_unlock(&devcon->comm_area_lock);
[6408be3]656 return rc;
[d68e4d5]657 }
[d5a720cf]658
659 *bufpos = 0;
660 *buflen = block_size;
661 }
662 }
[a830611]663 fibril_mutex_unlock(&devcon->comm_area_lock);
[d5a720cf]664
665 return EOK;
[fc840d9]666}
667
[00b1d20e]668/** Read blocks directly from device (bypass cache).
669 *
670 * @param dev_handle Device handle of the block device.
671 * @param ba Address of first block.
672 * @param cnt Number of blocks.
673 * @param src Buffer for storing the data.
674 *
675 * @return EOK on success or negative error code on failure.
676 */
677int block_read_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt, void *buf)
678{
679 devcon_t *devcon;
680 int rc;
681
682 devcon = devcon_search(dev_handle);
683 assert(devcon);
684
685 fibril_mutex_lock(&devcon->comm_area_lock);
686
687 rc = read_blocks(devcon, ba, cnt);
688 if (rc == EOK)
689 memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
690
691 fibril_mutex_unlock(&devcon->comm_area_lock);
692
693 return rc;
694}
695
696/** Write blocks directly to device (bypass cache).
697 *
698 * @param dev_handle Device handle of the block device.
699 * @param ba Address of first block.
700 * @param cnt Number of blocks.
701 * @param src The data to be written.
702 *
703 * @return EOK on success or negative error code on failure.
704 */
705int block_write_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt,
706 const void *data)
707{
708 devcon_t *devcon;
709 int rc;
710
711 devcon = devcon_search(dev_handle);
712 assert(devcon);
713
714 fibril_mutex_lock(&devcon->comm_area_lock);
715
716 memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
[dccf721]717 rc = write_blocks(devcon, ba, cnt);
[00b1d20e]718
719 fibril_mutex_unlock(&devcon->comm_area_lock);
720
721 return rc;
722}
723
724/** Get device block size.
725 *
726 * @param dev_handle Device handle of the block device.
727 * @param bsize Output block size.
728 *
729 * @return EOK on success or negative error code on failure.
730 */
731int block_get_bsize(dev_handle_t dev_handle, size_t *bsize)
732{
733 devcon_t *devcon;
734
735 devcon = devcon_search(dev_handle);
736 assert(devcon);
737
738 return get_block_size(devcon->dev_phone, bsize);
739}
740
[08232ee]741/** Get number of blocks on device.
742 *
743 * @param dev_handle Device handle of the block device.
744 * @param nblocks Output number of blocks.
745 *
746 * @return EOK on success or negative error code on failure.
747 */
748int block_get_nblocks(dev_handle_t dev_handle, bn_t *nblocks)
749{
750 devcon_t *devcon;
751
752 devcon = devcon_search(dev_handle);
753 assert(devcon);
754
755 return get_num_blocks(devcon->dev_phone, nblocks);
756}
757
[1ee00b7]758/** Read blocks from block device.
[6408be3]759 *
760 * @param devcon Device connection.
[1ee00b7]761 * @param ba Address of first block.
762 * @param cnt Number of blocks.
[6408be3]763 * @param src Buffer for storing the data.
764 *
765 * @return EOK on success or negative error code on failure.
766 */
[1ee00b7]767static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
[6408be3]768{
769 int rc;
770
771 assert(devcon);
[1ee00b7]772 rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
773 UPPER32(ba), cnt);
774 return rc;
[6408be3]775}
776
[1fbe064b]777/** Write block to block device.
778 *
779 * @param devcon Device connection.
[1ee00b7]780 * @param ba Address of first block.
781 * @param cnt Number of blocks.
[1fbe064b]782 * @param src Buffer containing the data to write.
783 *
784 * @return EOK on success or negative error code on failure.
785 */
[1ee00b7]786static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
[1fbe064b]787{
788 int rc;
789
790 assert(devcon);
[1ee00b7]791 rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
792 UPPER32(ba), cnt);
793 return rc;
794}
[1fbe064b]795
[1ee00b7]796/** Get block size used by the device. */
[00b1d20e]797static int get_block_size(int dev_phone, size_t *bsize)
[1ee00b7]798{
799 ipcarg_t bs;
800 int rc;
801
802 rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs);
803 if (rc == EOK)
804 *bsize = (size_t) bs;
805
806 return rc;
[1fbe064b]807}
808
[08232ee]809/** Get total number of blocks on block device. */
810static int get_num_blocks(int dev_phone, bn_t *nblocks)
811{
812 ipcarg_t nb_l, nb_h;
813 int rc;
814
815 rc = async_req_0_2(dev_phone, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
816 if (rc == EOK) {
817 *nblocks = (bn_t) MERGE_LOUP32(nb_l, nb_h);
818 }
819
820 return rc;
821}
822
[fc840d9]823/** @}
824 */
Note: See TracBrowser for help on using the repository browser.