source: mainline/uspace/lib/block/libblock.c@ 4802dd7

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

Factor out client and server IPC stubs for block devices.

  • Property mode set to 100644
File size: 22.3 KB
RevLine 
[fc840d9]1/*
[ed903174]2 * Copyright (c) 2008 Jakub Jermar
3 * Copyright (c) 2008 Martin Decky
[e272949]4 * Copyright (c) 2011 Martin Sucha
[fc840d9]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[97c9da8]31/** @addtogroup libblock
[fc840d9]32 * @{
[97c9da8]33 */
[fc840d9]34/**
35 * @file
36 * @brief
37 */
38
[97c9da8]39#include "libblock.h"
[fc840d9]40#include "../../srv/vfs/vfs.h"
[15f3c3f]41#include <ipc/loc.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 <as.h>
47#include <assert.h>
[4802dd7]48#include <bd.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>
[c7bbf029]54#include <malloc.h>
55#include <stdio.h>
[16fc3c9]56#include <sys/typefmt.h>
57#include <stacktrace.h>
[fc840d9]58
[916bf1a]59/** Lock protecting the device connection list */
[4e1b57d]60static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
[916bf1a]61/** Device connection list head. */
[b72efe8]62static LIST_INITIALIZE(dcl);
[916bf1a]63
[79ae36dd]64#define CACHE_BUCKETS_LOG2 10
65#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
[f1ba5d6]66
67typedef struct {
[4e1b57d]68 fibril_mutex_t lock;
[79ae36dd]69 size_t lblock_size; /**< Logical block size. */
70 unsigned blocks_cluster; /**< Physical blocks per block_t */
71 unsigned block_count; /**< Total number of blocks. */
72 unsigned blocks_cached; /**< Number of cached blocks. */
[f1ba5d6]73 hash_table_t block_hash;
[b72efe8]74 list_t free_list;
[1fbe064b]75 enum cache_mode mode;
[f1ba5d6]76} cache_t;
77
[916bf1a]78typedef struct {
79 link_t link;
[15f3c3f]80 service_id_t service_id;
[79ae36dd]81 async_sess_t *sess;
[4802dd7]82 bd_t *bd;
[916bf1a]83 void *bb_buf;
[ed903174]84 aoff64_t bb_addr;
[79ae36dd]85 size_t pblock_size; /**< Physical block size. */
[f1ba5d6]86 cache_t *cache;
[916bf1a]87} devcon_t;
88
[4802dd7]89static int read_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
90static int write_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
[79ae36dd]91static aoff64_t ba_ltop(devcon_t *, aoff64_t);
[1fbe064b]92
[15f3c3f]93static devcon_t *devcon_search(service_id_t service_id)
[916bf1a]94{
[4e1b57d]95 fibril_mutex_lock(&dcl_lock);
[79ae36dd]96
[b72efe8]97 list_foreach(dcl, cur) {
[916bf1a]98 devcon_t *devcon = list_get_instance(cur, devcon_t, link);
[15f3c3f]99 if (devcon->service_id == service_id) {
[4e1b57d]100 fibril_mutex_unlock(&dcl_lock);
[916bf1a]101 return devcon;
102 }
103 }
[79ae36dd]104
[4e1b57d]105 fibril_mutex_unlock(&dcl_lock);
[916bf1a]106 return NULL;
107}
108
[15f3c3f]109static int devcon_add(service_id_t service_id, async_sess_t *sess,
[4802dd7]110 size_t bsize, bd_t *bd)
[916bf1a]111{
112 devcon_t *devcon;
[79ae36dd]113
[916bf1a]114 devcon = malloc(sizeof(devcon_t));
115 if (!devcon)
116 return ENOMEM;
117
118 link_initialize(&devcon->link);
[15f3c3f]119 devcon->service_id = service_id;
[79ae36dd]120 devcon->sess = sess;
[4802dd7]121 devcon->bd = bd;
[6284978]122 devcon->bb_buf = NULL;
[1ee00b7]123 devcon->bb_addr = 0;
124 devcon->pblock_size = bsize;
[f1ba5d6]125 devcon->cache = NULL;
[79ae36dd]126
[4e1b57d]127 fibril_mutex_lock(&dcl_lock);
[b72efe8]128 list_foreach(dcl, cur) {
[916bf1a]129 devcon_t *d = list_get_instance(cur, devcon_t, link);
[15f3c3f]130 if (d->service_id == service_id) {
[4e1b57d]131 fibril_mutex_unlock(&dcl_lock);
[916bf1a]132 free(devcon);
133 return EEXIST;
134 }
135 }
[b72efe8]136 list_append(&devcon->link, &dcl);
[4e1b57d]137 fibril_mutex_unlock(&dcl_lock);
[916bf1a]138 return EOK;
139}
140
141static void devcon_remove(devcon_t *devcon)
142{
[4e1b57d]143 fibril_mutex_lock(&dcl_lock);
[916bf1a]144 list_remove(&devcon->link);
[4e1b57d]145 fibril_mutex_unlock(&dcl_lock);
[916bf1a]146}
[7858bc5f]147
[15f3c3f]148int block_init(exch_mgmt_t mgmt, service_id_t service_id,
[79ae36dd]149 size_t comm_size)
[7858bc5f]150{
[4802dd7]151 bd_t *bd;
152
[15f3c3f]153 async_sess_t *sess = loc_service_connect(mgmt, service_id,
[79ae36dd]154 IPC_FLAG_BLOCKING);
155 if (!sess) {
156 return ENOENT;
[7858bc5f]157 }
[79ae36dd]158
[4802dd7]159 int rc = bd_open(sess, &bd);
[7858bc5f]160 if (rc != EOK) {
[79ae36dd]161 async_hangup(sess);
[7858bc5f]162 return rc;
163 }
[79ae36dd]164
165 size_t bsize;
[4802dd7]166 rc = bd_get_block_size(bd, &bsize);
[79ae36dd]167 if (rc != EOK) {
[4802dd7]168 bd_close(bd);
[79ae36dd]169 async_hangup(sess);
[1ee00b7]170 return rc;
171 }
[916bf1a]172
[4802dd7]173 rc = devcon_add(service_id, sess, bsize, bd);
[916bf1a]174 if (rc != EOK) {
[4802dd7]175 bd_close(bd);
[79ae36dd]176 async_hangup(sess);
[916bf1a]177 return rc;
178 }
[79ae36dd]179
[7858bc5f]180 return EOK;
181}
182
[15f3c3f]183void block_fini(service_id_t service_id)
[7858bc5f]184{
[15f3c3f]185 devcon_t *devcon = devcon_search(service_id);
[916bf1a]186 assert(devcon);
187
[64bc4b6]188 if (devcon->cache)
[15f3c3f]189 (void) block_cache_fini(service_id);
[79ae36dd]190
[916bf1a]191 devcon_remove(devcon);
[79ae36dd]192
[6284978]193 if (devcon->bb_buf)
194 free(devcon->bb_buf);
[79ae36dd]195
[4802dd7]196 bd_close(devcon->bd);
[79ae36dd]197 async_hangup(devcon->sess);
198
199 free(devcon);
[7858bc5f]200}
201
[15f3c3f]202int block_bb_read(service_id_t service_id, aoff64_t ba)
[6284978]203{
204 void *bb_buf;
[0c243b4]205 int rc;
[6284978]206
[15f3c3f]207 devcon_t *devcon = devcon_search(service_id);
[6284978]208 if (!devcon)
209 return ENOENT;
210 if (devcon->bb_buf)
211 return EEXIST;
[1ee00b7]212 bb_buf = malloc(devcon->pblock_size);
[6284978]213 if (!bb_buf)
214 return ENOMEM;
[1ee00b7]215
[4802dd7]216 rc = read_blocks(devcon, 0, 1, bb_buf, devcon->pblock_size);
[0c243b4]217 if (rc != EOK) {
[6284978]218 free(bb_buf);
[0c243b4]219 return rc;
[6284978]220 }
[6408be3]221
[6284978]222 devcon->bb_buf = bb_buf;
[1ee00b7]223 devcon->bb_addr = ba;
[6284978]224
225 return EOK;
226}
227
[15f3c3f]228void *block_bb_get(service_id_t service_id)
[7858bc5f]229{
[15f3c3f]230 devcon_t *devcon = devcon_search(service_id);
[916bf1a]231 assert(devcon);
232 return devcon->bb_buf;
[7858bc5f]233}
234
[f1ba5d6]235static hash_index_t cache_hash(unsigned long *key)
236{
[867e2555]237 return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
[f1ba5d6]238}
239
240static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
241{
242 block_t *b = hash_table_get_instance(item, block_t, hash_link);
[867e2555]243 return b->lba == MERGE_LOUP32(key[0], key[1]);
[f1ba5d6]244}
245
246static void cache_remove_callback(link_t *item)
247{
248}
249
250static hash_table_operations_t cache_ops = {
251 .hash = cache_hash,
252 .compare = cache_compare,
253 .remove_callback = cache_remove_callback
254};
255
[15f3c3f]256int block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
[1fbe064b]257 enum cache_mode mode)
[f1ba5d6]258{
[15f3c3f]259 devcon_t *devcon = devcon_search(service_id);
[f1ba5d6]260 cache_t *cache;
261 if (!devcon)
262 return ENOENT;
263 if (devcon->cache)
264 return EEXIST;
265 cache = malloc(sizeof(cache_t));
266 if (!cache)
267 return ENOMEM;
268
[4e1b57d]269 fibril_mutex_initialize(&cache->lock);
[b72efe8]270 list_initialize(&cache->free_list);
[1ee00b7]271 cache->lblock_size = size;
[f1ba5d6]272 cache->block_count = blocks;
[d68e4d5]273 cache->blocks_cached = 0;
[1fbe064b]274 cache->mode = mode;
[f1ba5d6]275
[f092718]276 /* Allow 1:1 or small-to-large block size translation */
[37cf3792]277 if (cache->lblock_size % devcon->pblock_size != 0) {
278 free(cache);
[f092718]279 return ENOTSUP;
[37cf3792]280 }
[f092718]281
282 cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
[1ee00b7]283
[867e2555]284 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
[f1ba5d6]285 &cache_ops)) {
286 free(cache);
287 return ENOMEM;
288 }
289
290 devcon->cache = cache;
291 return EOK;
292}
293
[15f3c3f]294int block_cache_fini(service_id_t service_id)
[64bc4b6]295{
[15f3c3f]296 devcon_t *devcon = devcon_search(service_id);
[64bc4b6]297 cache_t *cache;
298 int rc;
299
300 if (!devcon)
301 return ENOENT;
302 if (!devcon->cache)
303 return EOK;
304 cache = devcon->cache;
305
306 /*
307 * We are expecting to find all blocks for this device handle on the
308 * free list, i.e. the block reference count should be zero. Do not
309 * bother with the cache and block locks because we are single-threaded.
310 */
[b72efe8]311 while (!list_empty(&cache->free_list)) {
312 block_t *b = list_get_instance(list_first(&cache->free_list),
[64bc4b6]313 block_t, free_link);
314
315 list_remove(&b->free_link);
316 if (b->dirty) {
[4802dd7]317 rc = write_blocks(devcon, b->pba, cache->blocks_cluster,
318 b->data, b->size);
[64bc4b6]319 if (rc != EOK)
320 return rc;
321 }
322
[867e2555]323 unsigned long key[2] = {
324 LOWER32(b->lba),
325 UPPER32(b->lba)
326 };
327 hash_table_remove(&cache->block_hash, key, 2);
[64bc4b6]328
329 free(b->data);
330 free(b);
331 }
332
333 hash_table_destroy(&cache->block_hash);
334 devcon->cache = NULL;
335 free(cache);
336
337 return EOK;
338}
339
[d68e4d5]340#define CACHE_LO_WATERMARK 10
341#define CACHE_HI_WATERMARK 20
[e1c88d5]342static bool cache_can_grow(cache_t *cache)
[fc840d9]343{
[d68e4d5]344 if (cache->blocks_cached < CACHE_LO_WATERMARK)
345 return true;
[b72efe8]346 if (!list_empty(&cache->free_list))
[d68e4d5]347 return false;
[e1c88d5]348 return true;
349}
350
351static void block_initialize(block_t *b)
352{
[4e1b57d]353 fibril_mutex_initialize(&b->lock);
[e1c88d5]354 b->refcnt = 1;
355 b->dirty = false;
[cd688d9]356 b->toxic = false;
[4e1b57d]357 fibril_rwlock_initialize(&b->contents_lock);
[e1c88d5]358 link_initialize(&b->free_link);
359 link_initialize(&b->hash_link);
360}
361
362/** Instantiate a block in memory and get a reference to it.
363 *
[c91f2d1b]364 * @param block Pointer to where the function will store the
365 * block pointer on success.
[15f3c3f]366 * @param service_id Service ID of the block device.
[a6ba0c9]367 * @param ba Block address (logical).
[1d8cdb1]368 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
369 * will not read the contents of the block from the
370 * device.
[e1c88d5]371 *
[c91f2d1b]372 * @return EOK on success or a negative error code.
[e1c88d5]373 */
[15f3c3f]374int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
[e1c88d5]375{
376 devcon_t *devcon;
377 cache_t *cache;
[fc840d9]378 block_t *b;
[e1c88d5]379 link_t *l;
[867e2555]380 unsigned long key[2] = {
381 LOWER32(ba),
382 UPPER32(ba)
383 };
384
[b7b3fda]385 int rc;
[e1c88d5]386
[15f3c3f]387 devcon = devcon_search(service_id);
[fc840d9]388
[e1c88d5]389 assert(devcon);
390 assert(devcon->cache);
[fc840d9]391
[e1c88d5]392 cache = devcon->cache;
[02ee6bf5]393
394retry:
[b7b3fda]395 rc = EOK;
[4f690cd]396 b = NULL;
[b7b3fda]397
[4e1b57d]398 fibril_mutex_lock(&cache->lock);
[867e2555]399 l = hash_table_find(&cache->block_hash, key);
[e1c88d5]400 if (l) {
[5716e9a]401found:
[e1c88d5]402 /*
403 * We found the block in the cache.
404 */
405 b = hash_table_get_instance(l, block_t, hash_link);
[4e1b57d]406 fibril_mutex_lock(&b->lock);
[e1c88d5]407 if (b->refcnt++ == 0)
408 list_remove(&b->free_link);
[402a18f]409 if (b->toxic)
410 rc = EIO;
[4e1b57d]411 fibril_mutex_unlock(&b->lock);
412 fibril_mutex_unlock(&cache->lock);
[e1c88d5]413 } else {
414 /*
415 * The block was not found in the cache.
416 */
417 if (cache_can_grow(cache)) {
418 /*
419 * We can grow the cache by allocating new blocks.
420 * Should the allocation fail, we fail over and try to
421 * recycle a block from the cache.
422 */
423 b = malloc(sizeof(block_t));
424 if (!b)
425 goto recycle;
[1ee00b7]426 b->data = malloc(cache->lblock_size);
[e1c88d5]427 if (!b->data) {
428 free(b);
[0dfaa099]429 b = NULL;
[e1c88d5]430 goto recycle;
431 }
[d68e4d5]432 cache->blocks_cached++;
[e1c88d5]433 } else {
434 /*
435 * Try to recycle a block from the free list.
436 */
437recycle:
[b72efe8]438 if (list_empty(&cache->free_list)) {
[7a56b1ed]439 fibril_mutex_unlock(&cache->lock);
440 rc = ENOMEM;
441 goto out;
442 }
[b72efe8]443 l = list_first(&cache->free_list);
[d68e4d5]444 b = list_get_instance(l, block_t, free_link);
[02ee6bf5]445
446 fibril_mutex_lock(&b->lock);
447 if (b->dirty) {
448 /*
449 * The block needs to be written back to the
450 * device before it changes identity. Do this
451 * while not holding the cache lock so that
452 * concurrency is not impeded. Also move the
453 * block to the end of the free list so that we
454 * do not slow down other instances of
455 * block_get() draining the free list.
456 */
457 list_remove(&b->free_link);
[b72efe8]458 list_append(&b->free_link, &cache->free_list);
[02ee6bf5]459 fibril_mutex_unlock(&cache->lock);
[f092718]460 rc = write_blocks(devcon, b->pba,
[4802dd7]461 cache->blocks_cluster, b->data, b->size);
[402a18f]462 if (rc != EOK) {
463 /*
464 * We did not manage to write the block
465 * to the device. Keep it around for
466 * another try. Hopefully, we will grab
467 * another block next time.
468 */
469 fibril_mutex_unlock(&b->lock);
470 goto retry;
471 }
[02ee6bf5]472 b->dirty = false;
473 if (!fibril_mutex_trylock(&cache->lock)) {
474 /*
475 * Somebody is probably racing with us.
476 * Unlock the block and retry.
477 */
478 fibril_mutex_unlock(&b->lock);
479 goto retry;
480 }
[867e2555]481 l = hash_table_find(&cache->block_hash, key);
[5716e9a]482 if (l) {
483 /*
484 * Someone else must have already
485 * instantiated the block while we were
486 * not holding the cache lock.
487 * Leave the recycled block on the
488 * freelist and continue as if we
489 * found the block of interest during
490 * the first try.
491 */
492 fibril_mutex_unlock(&b->lock);
493 goto found;
494 }
[02ee6bf5]495
496 }
497 fibril_mutex_unlock(&b->lock);
498
499 /*
500 * Unlink the block from the free list and the hash
501 * table.
502 */
503 list_remove(&b->free_link);
[867e2555]504 unsigned long temp_key[2] = {
505 LOWER32(b->lba),
506 UPPER32(b->lba)
507 };
508 hash_table_remove(&cache->block_hash, temp_key, 2);
[e1c88d5]509 }
[fc840d9]510
[e1c88d5]511 block_initialize(b);
[15f3c3f]512 b->service_id = service_id;
[1ee00b7]513 b->size = cache->lblock_size;
[a6ba0c9]514 b->lba = ba;
515 b->pba = ba_ltop(devcon, b->lba);
[867e2555]516 hash_table_insert(&cache->block_hash, key, &b->hash_link);
[a6d97fb9]517
518 /*
519 * Lock the block before releasing the cache lock. Thus we don't
[5ac8918]520 * kill concurrent operations on the cache while doing I/O on
521 * the block.
[a6d97fb9]522 */
[4e1b57d]523 fibril_mutex_lock(&b->lock);
524 fibril_mutex_unlock(&cache->lock);
[a6d97fb9]525
[1d8cdb1]526 if (!(flags & BLOCK_FLAGS_NOREAD)) {
527 /*
528 * The block contains old or no data. We need to read
529 * the new contents from the device.
530 */
[4802dd7]531 rc = read_blocks(devcon, b->pba, cache->blocks_cluster,
532 b->data, cache->lblock_size);
[402a18f]533 if (rc != EOK)
534 b->toxic = true;
535 } else
536 rc = EOK;
[fc840d9]537
[4e1b57d]538 fibril_mutex_unlock(&b->lock);
[a6d97fb9]539 }
[7a56b1ed]540out:
[4f690cd]541 if ((rc != EOK) && b) {
542 assert(b->toxic);
543 (void) block_put(b);
544 b = NULL;
545 }
[c91f2d1b]546 *block = b;
[402a18f]547 return rc;
[fc840d9]548}
549
[d5a720cf]550/** Release a reference to a block.
551 *
[a6d97fb9]552 * If the last reference is dropped, the block is put on the free list.
[d5a720cf]553 *
554 * @param block Block of which a reference is to be released.
[c91f2d1b]555 *
556 * @return EOK on success or a negative error code.
[d5a720cf]557 */
[c91f2d1b]558int block_put(block_t *block)
[fc840d9]559{
[15f3c3f]560 devcon_t *devcon = devcon_search(block->service_id);
[d5a720cf]561 cache_t *cache;
[ddfc39a3]562 unsigned blocks_cached;
563 enum cache_mode mode;
[402a18f]564 int rc = EOK;
[d5a720cf]565
566 assert(devcon);
567 assert(devcon->cache);
[0f1cf7a]568 assert(block->refcnt >= 1);
[d5a720cf]569
570 cache = devcon->cache;
[ddfc39a3]571
572retry:
573 fibril_mutex_lock(&cache->lock);
574 blocks_cached = cache->blocks_cached;
575 mode = cache->mode;
576 fibril_mutex_unlock(&cache->lock);
577
578 /*
579 * Determine whether to sync the block. Syncing the block is best done
580 * when not holding the cache lock as it does not impede concurrency.
581 * Since the situation may have changed when we unlocked the cache, the
582 * blocks_cached and mode variables are mere hints. We will recheck the
583 * conditions later when the cache lock is held again.
584 */
585 fibril_mutex_lock(&block->lock);
[402a18f]586 if (block->toxic)
587 block->dirty = false; /* will not write back toxic block */
[ddfc39a3]588 if (block->dirty && (block->refcnt == 1) &&
589 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
[4802dd7]590 rc = write_blocks(devcon, block->pba, cache->blocks_cluster,
591 block->data, block->size);
[ddfc39a3]592 block->dirty = false;
593 }
594 fibril_mutex_unlock(&block->lock);
595
[4e1b57d]596 fibril_mutex_lock(&cache->lock);
597 fibril_mutex_lock(&block->lock);
[d5a720cf]598 if (!--block->refcnt) {
599 /*
[d68e4d5]600 * Last reference to the block was dropped. Either free the
[402a18f]601 * block or put it on the free list. In case of an I/O error,
602 * free the block.
[d68e4d5]603 */
[402a18f]604 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
605 (rc != EOK)) {
[d68e4d5]606 /*
[402a18f]607 * Currently there are too many cached blocks or there
608 * was an I/O error when writing the block back to the
609 * device.
[d68e4d5]610 */
611 if (block->dirty) {
[ddfc39a3]612 /*
613 * We cannot sync the block while holding the
614 * cache lock. Release everything and retry.
615 */
616 block->refcnt++;
617 fibril_mutex_unlock(&block->lock);
618 fibril_mutex_unlock(&cache->lock);
619 goto retry;
[d68e4d5]620 }
621 /*
622 * Take the block out of the cache and free it.
623 */
[867e2555]624 unsigned long key[2] = {
625 LOWER32(block->lba),
626 UPPER32(block->lba)
627 };
628 hash_table_remove(&cache->block_hash, key, 2);
[956d4df8]629 fibril_mutex_unlock(&block->lock);
[d68e4d5]630 free(block->data);
[b9e6205]631 free(block);
[d68e4d5]632 cache->blocks_cached--;
633 fibril_mutex_unlock(&cache->lock);
[402a18f]634 return rc;
[d68e4d5]635 }
636 /*
637 * Put the block on the free list.
[d5a720cf]638 */
[1fbe064b]639 if (cache->mode != CACHE_MODE_WB && block->dirty) {
[ddfc39a3]640 /*
641 * We cannot sync the block while holding the cache
642 * lock. Release everything and retry.
643 */
644 block->refcnt++;
645 fibril_mutex_unlock(&block->lock);
646 fibril_mutex_unlock(&cache->lock);
647 goto retry;
[1fbe064b]648 }
[b72efe8]649 list_append(&block->free_link, &cache->free_list);
[d5a720cf]650 }
[4e1b57d]651 fibril_mutex_unlock(&block->lock);
652 fibril_mutex_unlock(&cache->lock);
[c91f2d1b]653
[402a18f]654 return rc;
[d5a720cf]655}
656
[6408be3]657/** Read sequential data from a block device.
[d5a720cf]658 *
[15f3c3f]659 * @param service_id Service ID of the block device.
[4802dd7]660 * @param buf Buffer for holding one block
[d5a720cf]661 * @param bufpos Pointer to the first unread valid offset within the
662 * communication buffer.
663 * @param buflen Pointer to the number of unread bytes that are ready in
664 * the communication buffer.
665 * @param pos Device position to be read.
666 * @param dst Destination buffer.
667 * @param size Size of the destination buffer.
668 * @param block_size Block size to be used for the transfer.
669 *
670 * @return EOK on success or a negative return code on failure.
671 */
[4802dd7]672int block_seqread(service_id_t service_id, void *buf, size_t *bufpos,
673 size_t *buflen, aoff64_t *pos, void *dst, size_t size)
[d5a720cf]674{
[ed903174]675 size_t offset = 0;
[d5a720cf]676 size_t left = size;
[1ee00b7]677 size_t block_size;
678 devcon_t *devcon;
679
[15f3c3f]680 devcon = devcon_search(service_id);
[d5a720cf]681 assert(devcon);
[1ee00b7]682 block_size = devcon->pblock_size;
[e1c88d5]683
[d5a720cf]684 while (left > 0) {
685 size_t rd;
686
687 if (*bufpos + left < *buflen)
688 rd = left;
689 else
690 rd = *buflen - *bufpos;
691
692 if (rd > 0) {
693 /*
694 * Copy the contents of the communication buffer to the
695 * destination buffer.
696 */
[4802dd7]697 memcpy(dst + offset, buf + *bufpos, rd);
[d5a720cf]698 offset += rd;
699 *bufpos += rd;
700 *pos += rd;
701 left -= rd;
702 }
703
[ed903174]704 if (*bufpos == *buflen) {
[d5a720cf]705 /* Refill the communication buffer with a new block. */
[6408be3]706 int rc;
707
[4802dd7]708 rc = read_blocks(devcon, *pos / block_size, 1, buf,
709 devcon->pblock_size);
[d68e4d5]710 if (rc != EOK) {
[6408be3]711 return rc;
[d68e4d5]712 }
[d5a720cf]713
714 *bufpos = 0;
715 *buflen = block_size;
716 }
717 }
718
719 return EOK;
[fc840d9]720}
721
[00b1d20e]722/** Read blocks directly from device (bypass cache).
723 *
[15f3c3f]724 * @param service_id Service ID of the block device.
[a6ba0c9]725 * @param ba Address of first block (physical).
[00b1d20e]726 * @param cnt Number of blocks.
727 * @param src Buffer for storing the data.
728 *
729 * @return EOK on success or negative error code on failure.
730 */
[15f3c3f]731int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
[00b1d20e]732{
733 devcon_t *devcon;
734
[15f3c3f]735 devcon = devcon_search(service_id);
[00b1d20e]736 assert(devcon);
737
[4802dd7]738 return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt);
[00b1d20e]739}
740
741/** Write blocks directly to device (bypass cache).
742 *
[15f3c3f]743 * @param service_id Service ID of the block device.
[a6ba0c9]744 * @param ba Address of first block (physical).
[00b1d20e]745 * @param cnt Number of blocks.
746 * @param src The data to be written.
747 *
748 * @return EOK on success or negative error code on failure.
749 */
[15f3c3f]750int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
[00b1d20e]751 const void *data)
752{
753 devcon_t *devcon;
754
[15f3c3f]755 devcon = devcon_search(service_id);
[00b1d20e]756 assert(devcon);
757
[4802dd7]758 return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt);
[00b1d20e]759}
760
761/** Get device block size.
762 *
[15f3c3f]763 * @param service_id Service ID of the block device.
[00b1d20e]764 * @param bsize Output block size.
765 *
766 * @return EOK on success or negative error code on failure.
767 */
[15f3c3f]768int block_get_bsize(service_id_t service_id, size_t *bsize)
[00b1d20e]769{
770 devcon_t *devcon;
771
[15f3c3f]772 devcon = devcon_search(service_id);
[00b1d20e]773 assert(devcon);
[4802dd7]774
775 return bd_get_block_size(devcon->bd, bsize);
[00b1d20e]776}
777
[08232ee]778/** Get number of blocks on device.
779 *
[15f3c3f]780 * @param service_id Service ID of the block device.
[08232ee]781 * @param nblocks Output number of blocks.
782 *
783 * @return EOK on success or negative error code on failure.
784 */
[15f3c3f]785int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
[08232ee]786{
[15f3c3f]787 devcon_t *devcon = devcon_search(service_id);
[08232ee]788 assert(devcon);
789
[4802dd7]790 return bd_get_num_blocks(devcon->bd, nblocks);
[08232ee]791}
792
[e272949]793/** Read bytes directly from the device (bypass cache)
794 *
[15f3c3f]795 * @param service_id Service ID of the block device.
[e272949]796 * @param abs_offset Absolute offset in bytes where to start reading
797 * @param bytes Number of bytes to read
798 * @param data Buffer that receives the data
799 *
800 * @return EOK on success or negative error code on failure.
801 */
[15f3c3f]802int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
[e272949]803 size_t bytes, void *data)
804{
805 int rc;
806 size_t phys_block_size;
807 size_t buf_size;
808 void *buffer;
809 aoff64_t first_block;
810 aoff64_t last_block;
811 size_t blocks;
812 size_t offset;
813
[15f3c3f]814 rc = block_get_bsize(service_id, &phys_block_size);
[e272949]815 if (rc != EOK) {
816 return rc;
817 }
818
[c4aa9cf]819 /* calculate data position and required space */
[e272949]820 first_block = abs_offset / phys_block_size;
821 offset = abs_offset % phys_block_size;
822 last_block = (abs_offset + bytes - 1) / phys_block_size;
823 blocks = last_block - first_block + 1;
824 buf_size = blocks * phys_block_size;
825
[c4aa9cf]826 /* read the data into memory */
[e272949]827 buffer = malloc(buf_size);
828 if (buffer == NULL) {
829 return ENOMEM;
830 }
831
[15f3c3f]832 rc = block_read_direct(service_id, first_block, blocks, buffer);
[e272949]833 if (rc != EOK) {
834 free(buffer);
835 return rc;
836 }
837
[c4aa9cf]838 /* copy the data from the buffer */
[e272949]839 memcpy(data, buffer + offset, bytes);
840 free(buffer);
[4802dd7]841
[e272949]842 return EOK;
843}
844
[4046b2f4]845/** Get TOC from device.
846 *
847 * @param service_id Service ID of the block device.
848 * @param session Starting session.
849 *
[08cba4b]850 * @return Allocated TOC structure.
851 * @return NULL on failure.
[4046b2f4]852 *
853 */
[08cba4b]854toc_block_t *block_get_toc(service_id_t service_id, uint8_t session)
[4046b2f4]855{
856 devcon_t *devcon = devcon_search(service_id);
[08cba4b]857 toc_block_t *toc = NULL;
[4802dd7]858 int rc;
[08cba4b]859
[4802dd7]860 assert(devcon);
[4046b2f4]861
[4802dd7]862 toc = (toc_block_t *) malloc(sizeof(toc_block_t));
863 if (toc == NULL)
864 return NULL;
[08cba4b]865
[4802dd7]866 rc = bd_read_toc(devcon->bd, session, toc, sizeof(toc_block_t));
867 if (rc != EOK) {
868 free(toc);
869 return NULL;
[08cba4b]870 }
871
872 return toc;
[4046b2f4]873}
874
[1ee00b7]875/** Read blocks from block device.
[6408be3]876 *
877 * @param devcon Device connection.
[1ee00b7]878 * @param ba Address of first block.
879 * @param cnt Number of blocks.
[6408be3]880 * @param src Buffer for storing the data.
881 *
882 * @return EOK on success or negative error code on failure.
883 */
[4802dd7]884static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf,
885 size_t size)
[6408be3]886{
887 assert(devcon);
[79ae36dd]888
[4802dd7]889 int rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size);
[16fc3c9]890 if (rc != EOK) {
[7e752b2]891 printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
892 " from device handle %" PRIun "\n", rc, cnt, ba,
[15f3c3f]893 devcon->service_id);
[16fc3c9]894#ifndef NDEBUG
895 stacktrace_print();
896#endif
897 }
[79ae36dd]898
[1ee00b7]899 return rc;
[6408be3]900}
901
[1fbe064b]902/** Write block to block device.
903 *
904 * @param devcon Device connection.
[1ee00b7]905 * @param ba Address of first block.
906 * @param cnt Number of blocks.
[1fbe064b]907 * @param src Buffer containing the data to write.
908 *
909 * @return EOK on success or negative error code on failure.
910 */
[4802dd7]911static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data,
912 size_t size)
[1fbe064b]913{
914 assert(devcon);
[79ae36dd]915
[4802dd7]916 int rc = bd_write_blocks(devcon->bd, ba, cnt, data, size);
[16fc3c9]917 if (rc != EOK) {
[7e752b2]918 printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
[15f3c3f]919 " to device handle %" PRIun "\n", rc, cnt, ba, devcon->service_id);
[16fc3c9]920#ifndef NDEBUG
921 stacktrace_print();
922#endif
923 }
[79ae36dd]924
[1ee00b7]925 return rc;
926}
[1fbe064b]927
[f092718]928/** Convert logical block address to physical block address. */
929static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
930{
931 assert(devcon->cache != NULL);
932 return lba * devcon->cache->blocks_cluster;
933}
934
[fc840d9]935/** @}
936 */
Note: See TracBrowser for help on using the repository browser.