source: mainline/uspace/lib/block/block.c@ f77c1c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f77c1c9 was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 23.5 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
[15f3c3f]39#include <ipc/loc.h>
[7858bc5f]40#include <ipc/services.h>
[fc840d9]41#include <errno.h>
42#include <async.h>
43#include <as.h>
44#include <assert.h>
[4802dd7]45#include <bd.h>
[1e4cada]46#include <fibril_synch.h>
[d9c8c81]47#include <adt/list.h>
48#include <adt/hash_table.h>
[1ee00b7]49#include <macros.h>
[d00ae4c]50#include <mem.h>
[38d150e]51#include <stdlib.h>
[c7bbf029]52#include <stdio.h>
[16fc3c9]53#include <stacktrace.h>
[c1694b6b]54#include <str_error.h>
[7354b5e]55#include <offset.h>
56#include <inttypes.h>
[f73b291]57#include "block.h"
[fc840d9]58
[3d35386]59#define MAX_WRITE_RETRIES 10
60
[916bf1a]61/** Lock protecting the device connection list */
[4e1b57d]62static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
[916bf1a]63/** Device connection list head. */
[b72efe8]64static LIST_INITIALIZE(dcl);
[916bf1a]65
[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;
[3d35386]85 aoff64_t pblocks; /**< Number of physical blocks */
[79ae36dd]86 size_t pblock_size; /**< Physical block size. */
[f1ba5d6]87 cache_t *cache;
[916bf1a]88} devcon_t;
89
[4802dd7]90static int read_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
91static int write_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
[79ae36dd]92static aoff64_t ba_ltop(devcon_t *, aoff64_t);
[1fbe064b]93
[15f3c3f]94static devcon_t *devcon_search(service_id_t service_id)
[916bf1a]95{
[4e1b57d]96 fibril_mutex_lock(&dcl_lock);
[79ae36dd]97
[feeac0d]98 list_foreach(dcl, link, devcon_t, devcon) {
[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,
[3d35386]110 size_t bsize, aoff64_t dev_size, 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;
[3d35386]125 devcon->pblocks = dev_size;
[f1ba5d6]126 devcon->cache = NULL;
[79ae36dd]127
[4e1b57d]128 fibril_mutex_lock(&dcl_lock);
[feeac0d]129 list_foreach(dcl, link, devcon_t, d) {
[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
[fc22069]148int block_init(service_id_t service_id, size_t comm_size)
[7858bc5f]149{
[4802dd7]150 bd_t *bd;
151
[f9b2cb4c]152 async_sess_t *sess = loc_service_connect(service_id, INTERFACE_BLOCK,
[79ae36dd]153 IPC_FLAG_BLOCKING);
154 if (!sess) {
155 return ENOENT;
[7858bc5f]156 }
[79ae36dd]157
[4802dd7]158 int rc = bd_open(sess, &bd);
[7858bc5f]159 if (rc != EOK) {
[79ae36dd]160 async_hangup(sess);
[7858bc5f]161 return rc;
162 }
[79ae36dd]163
164 size_t bsize;
[4802dd7]165 rc = bd_get_block_size(bd, &bsize);
[79ae36dd]166 if (rc != EOK) {
[4802dd7]167 bd_close(bd);
[79ae36dd]168 async_hangup(sess);
[1ee00b7]169 return rc;
170 }
[3d35386]171
172 aoff64_t dev_size;
173 rc = bd_get_num_blocks(bd, &dev_size);
174 if (rc != EOK) {
175 bd_close(bd);
176 async_hangup(sess);
177 return rc;
178 }
[916bf1a]179
[3d35386]180 rc = devcon_add(service_id, sess, bsize, dev_size, bd);
[916bf1a]181 if (rc != EOK) {
[4802dd7]182 bd_close(bd);
[79ae36dd]183 async_hangup(sess);
[916bf1a]184 return rc;
185 }
[79ae36dd]186
[7858bc5f]187 return EOK;
188}
189
[15f3c3f]190void block_fini(service_id_t service_id)
[7858bc5f]191{
[15f3c3f]192 devcon_t *devcon = devcon_search(service_id);
[916bf1a]193 assert(devcon);
194
[64bc4b6]195 if (devcon->cache)
[15f3c3f]196 (void) block_cache_fini(service_id);
[79ae36dd]197
[dd8b6a8]198 (void)bd_sync_cache(devcon->bd, 0, 0);
199
[916bf1a]200 devcon_remove(devcon);
[79ae36dd]201
[6284978]202 if (devcon->bb_buf)
203 free(devcon->bb_buf);
[79ae36dd]204
[4802dd7]205 bd_close(devcon->bd);
[79ae36dd]206 async_hangup(devcon->sess);
207
208 free(devcon);
[7858bc5f]209}
210
[15f3c3f]211int block_bb_read(service_id_t service_id, aoff64_t ba)
[6284978]212{
213 void *bb_buf;
[0c243b4]214 int rc;
[6284978]215
[15f3c3f]216 devcon_t *devcon = devcon_search(service_id);
[6284978]217 if (!devcon)
218 return ENOENT;
219 if (devcon->bb_buf)
220 return EEXIST;
[1ee00b7]221 bb_buf = malloc(devcon->pblock_size);
[6284978]222 if (!bb_buf)
223 return ENOMEM;
[1ee00b7]224
[4802dd7]225 rc = read_blocks(devcon, 0, 1, bb_buf, devcon->pblock_size);
[0c243b4]226 if (rc != EOK) {
[6284978]227 free(bb_buf);
[0c243b4]228 return rc;
[6284978]229 }
[6408be3]230
[6284978]231 devcon->bb_buf = bb_buf;
[1ee00b7]232 devcon->bb_addr = ba;
[6284978]233
234 return EOK;
235}
236
[15f3c3f]237void *block_bb_get(service_id_t service_id)
[7858bc5f]238{
[15f3c3f]239 devcon_t *devcon = devcon_search(service_id);
[916bf1a]240 assert(devcon);
241 return devcon->bb_buf;
[7858bc5f]242}
243
[062d900]244static size_t cache_key_hash(void *key)
[f1ba5d6]245{
[062d900]246 aoff64_t *lba = (aoff64_t*)key;
247 return *lba;
[f1ba5d6]248}
249
[062d900]250static size_t cache_hash(const ht_link_t *item)
[f1ba5d6]251{
[062d900]252 block_t *b = hash_table_get_inst(item, block_t, hash_link);
253 return b->lba;
[f1ba5d6]254}
255
[062d900]256static bool cache_key_equal(void *key, const ht_link_t *item)
[f1ba5d6]257{
[062d900]258 aoff64_t *lba = (aoff64_t*)key;
259 block_t *b = hash_table_get_inst(item, block_t, hash_link);
260 return b->lba == *lba;
[f1ba5d6]261}
262
[062d900]263
264static hash_table_ops_t cache_ops = {
[f1ba5d6]265 .hash = cache_hash,
[062d900]266 .key_hash = cache_key_hash,
267 .key_equal = cache_key_equal,
[4e00f87]268 .equal = NULL,
269 .remove_callback = NULL
[f1ba5d6]270};
271
[15f3c3f]272int block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
[1fbe064b]273 enum cache_mode mode)
[f1ba5d6]274{
[15f3c3f]275 devcon_t *devcon = devcon_search(service_id);
[f1ba5d6]276 cache_t *cache;
277 if (!devcon)
278 return ENOENT;
279 if (devcon->cache)
280 return EEXIST;
281 cache = malloc(sizeof(cache_t));
282 if (!cache)
283 return ENOMEM;
284
[4e1b57d]285 fibril_mutex_initialize(&cache->lock);
[b72efe8]286 list_initialize(&cache->free_list);
[1ee00b7]287 cache->lblock_size = size;
[f1ba5d6]288 cache->block_count = blocks;
[d68e4d5]289 cache->blocks_cached = 0;
[1fbe064b]290 cache->mode = mode;
[f1ba5d6]291
[f092718]292 /* Allow 1:1 or small-to-large block size translation */
[37cf3792]293 if (cache->lblock_size % devcon->pblock_size != 0) {
294 free(cache);
[f092718]295 return ENOTSUP;
[37cf3792]296 }
[f092718]297
298 cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
[1ee00b7]299
[062d900]300 if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
[f1ba5d6]301 free(cache);
302 return ENOMEM;
303 }
304
305 devcon->cache = cache;
306 return EOK;
307}
308
[15f3c3f]309int block_cache_fini(service_id_t service_id)
[64bc4b6]310{
[15f3c3f]311 devcon_t *devcon = devcon_search(service_id);
[64bc4b6]312 cache_t *cache;
313 int rc;
314
315 if (!devcon)
316 return ENOENT;
317 if (!devcon->cache)
318 return EOK;
319 cache = devcon->cache;
320
321 /*
322 * We are expecting to find all blocks for this device handle on the
323 * free list, i.e. the block reference count should be zero. Do not
324 * bother with the cache and block locks because we are single-threaded.
325 */
[b72efe8]326 while (!list_empty(&cache->free_list)) {
327 block_t *b = list_get_instance(list_first(&cache->free_list),
[64bc4b6]328 block_t, free_link);
329
330 list_remove(&b->free_link);
331 if (b->dirty) {
[4802dd7]332 rc = write_blocks(devcon, b->pba, cache->blocks_cluster,
333 b->data, b->size);
[64bc4b6]334 if (rc != EOK)
335 return rc;
336 }
337
[062d900]338 hash_table_remove_item(&cache->block_hash, &b->hash_link);
[64bc4b6]339
340 free(b->data);
341 free(b);
342 }
343
344 hash_table_destroy(&cache->block_hash);
345 devcon->cache = NULL;
346 free(cache);
347
348 return EOK;
349}
350
[d68e4d5]351#define CACHE_LO_WATERMARK 10
352#define CACHE_HI_WATERMARK 20
[e1c88d5]353static bool cache_can_grow(cache_t *cache)
[fc840d9]354{
[d68e4d5]355 if (cache->blocks_cached < CACHE_LO_WATERMARK)
356 return true;
[b72efe8]357 if (!list_empty(&cache->free_list))
[d68e4d5]358 return false;
[e1c88d5]359 return true;
360}
361
362static void block_initialize(block_t *b)
363{
[4e1b57d]364 fibril_mutex_initialize(&b->lock);
[e1c88d5]365 b->refcnt = 1;
[3d35386]366 b->write_failures = 0;
[e1c88d5]367 b->dirty = false;
[cd688d9]368 b->toxic = false;
[4e1b57d]369 fibril_rwlock_initialize(&b->contents_lock);
[e1c88d5]370 link_initialize(&b->free_link);
371}
372
373/** Instantiate a block in memory and get a reference to it.
374 *
[c91f2d1b]375 * @param block Pointer to where the function will store the
376 * block pointer on success.
[15f3c3f]377 * @param service_id Service ID of the block device.
[a6ba0c9]378 * @param ba Block address (logical).
[1d8cdb1]379 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
380 * will not read the contents of the block from the
381 * device.
[e1c88d5]382 *
[c91f2d1b]383 * @return EOK on success or a negative error code.
[e1c88d5]384 */
[15f3c3f]385int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
[e1c88d5]386{
387 devcon_t *devcon;
388 cache_t *cache;
[fc840d9]389 block_t *b;
[062d900]390 link_t *link;
[3d35386]391 aoff64_t p_ba;
[b7b3fda]392 int rc;
[e1c88d5]393
[15f3c3f]394 devcon = devcon_search(service_id);
[fc840d9]395
[e1c88d5]396 assert(devcon);
397 assert(devcon->cache);
[fc840d9]398
[e1c88d5]399 cache = devcon->cache;
[02ee6bf5]400
[3d35386]401 /* Check whether the logical block (or part of it) is beyond
402 * the end of the device or not.
403 */
404 p_ba = ba_ltop(devcon, ba);
405 p_ba += cache->blocks_cluster;
406 if (p_ba >= devcon->pblocks) {
407 /* This request cannot be satisfied */
408 return EIO;
409 }
410
411
[02ee6bf5]412retry:
[b7b3fda]413 rc = EOK;
[4f690cd]414 b = NULL;
[b7b3fda]415
[4e1b57d]416 fibril_mutex_lock(&cache->lock);
[062d900]417 ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
418 if (hlink) {
[5716e9a]419found:
[e1c88d5]420 /*
421 * We found the block in the cache.
422 */
[062d900]423 b = hash_table_get_inst(hlink, block_t, hash_link);
[4e1b57d]424 fibril_mutex_lock(&b->lock);
[e1c88d5]425 if (b->refcnt++ == 0)
426 list_remove(&b->free_link);
[402a18f]427 if (b->toxic)
428 rc = EIO;
[4e1b57d]429 fibril_mutex_unlock(&b->lock);
430 fibril_mutex_unlock(&cache->lock);
[e1c88d5]431 } else {
432 /*
433 * The block was not found in the cache.
434 */
435 if (cache_can_grow(cache)) {
436 /*
437 * We can grow the cache by allocating new blocks.
438 * Should the allocation fail, we fail over and try to
439 * recycle a block from the cache.
440 */
441 b = malloc(sizeof(block_t));
442 if (!b)
443 goto recycle;
[1ee00b7]444 b->data = malloc(cache->lblock_size);
[e1c88d5]445 if (!b->data) {
446 free(b);
[0dfaa099]447 b = NULL;
[e1c88d5]448 goto recycle;
449 }
[d68e4d5]450 cache->blocks_cached++;
[e1c88d5]451 } else {
452 /*
453 * Try to recycle a block from the free list.
454 */
455recycle:
[b72efe8]456 if (list_empty(&cache->free_list)) {
[7a56b1ed]457 fibril_mutex_unlock(&cache->lock);
458 rc = ENOMEM;
459 goto out;
460 }
[062d900]461 link = list_first(&cache->free_list);
462 b = list_get_instance(link, block_t, free_link);
[02ee6bf5]463
464 fibril_mutex_lock(&b->lock);
465 if (b->dirty) {
466 /*
467 * The block needs to be written back to the
468 * device before it changes identity. Do this
469 * while not holding the cache lock so that
470 * concurrency is not impeded. Also move the
471 * block to the end of the free list so that we
472 * do not slow down other instances of
473 * block_get() draining the free list.
474 */
475 list_remove(&b->free_link);
[b72efe8]476 list_append(&b->free_link, &cache->free_list);
[02ee6bf5]477 fibril_mutex_unlock(&cache->lock);
[f092718]478 rc = write_blocks(devcon, b->pba,
[4802dd7]479 cache->blocks_cluster, b->data, b->size);
[402a18f]480 if (rc != EOK) {
481 /*
482 * We did not manage to write the block
483 * to the device. Keep it around for
484 * another try. Hopefully, we will grab
485 * another block next time.
486 */
[3d35386]487 if (b->write_failures < MAX_WRITE_RETRIES) {
488 b->write_failures++;
489 fibril_mutex_unlock(&b->lock);
490 goto retry;
491 } else {
492 printf("Too many errors writing block %"
493 PRIuOFF64 "from device handle %" PRIun "\n"
494 "SEVERE DATA LOSS POSSIBLE\n",
495 b->lba, devcon->service_id);
496 }
497 } else
498 b->write_failures = 0;
499
[02ee6bf5]500 b->dirty = false;
501 if (!fibril_mutex_trylock(&cache->lock)) {
502 /*
503 * Somebody is probably racing with us.
504 * Unlock the block and retry.
505 */
506 fibril_mutex_unlock(&b->lock);
507 goto retry;
508 }
[062d900]509 hlink = hash_table_find(&cache->block_hash, &ba);
510 if (hlink) {
[5716e9a]511 /*
512 * Someone else must have already
513 * instantiated the block while we were
514 * not holding the cache lock.
515 * Leave the recycled block on the
516 * freelist and continue as if we
517 * found the block of interest during
518 * the first try.
519 */
520 fibril_mutex_unlock(&b->lock);
521 goto found;
522 }
[02ee6bf5]523
524 }
525 fibril_mutex_unlock(&b->lock);
526
527 /*
528 * Unlink the block from the free list and the hash
529 * table.
530 */
531 list_remove(&b->free_link);
[062d900]532 hash_table_remove_item(&cache->block_hash, &b->hash_link);
[e1c88d5]533 }
[fc840d9]534
[e1c88d5]535 block_initialize(b);
[15f3c3f]536 b->service_id = service_id;
[1ee00b7]537 b->size = cache->lblock_size;
[a6ba0c9]538 b->lba = ba;
539 b->pba = ba_ltop(devcon, b->lba);
[062d900]540 hash_table_insert(&cache->block_hash, &b->hash_link);
[a6d97fb9]541
542 /*
543 * Lock the block before releasing the cache lock. Thus we don't
[5ac8918]544 * kill concurrent operations on the cache while doing I/O on
545 * the block.
[a6d97fb9]546 */
[4e1b57d]547 fibril_mutex_lock(&b->lock);
548 fibril_mutex_unlock(&cache->lock);
[a6d97fb9]549
[1d8cdb1]550 if (!(flags & BLOCK_FLAGS_NOREAD)) {
551 /*
552 * The block contains old or no data. We need to read
553 * the new contents from the device.
554 */
[4802dd7]555 rc = read_blocks(devcon, b->pba, cache->blocks_cluster,
556 b->data, cache->lblock_size);
[402a18f]557 if (rc != EOK)
558 b->toxic = true;
559 } else
560 rc = EOK;
[fc840d9]561
[4e1b57d]562 fibril_mutex_unlock(&b->lock);
[a6d97fb9]563 }
[7a56b1ed]564out:
[4f690cd]565 if ((rc != EOK) && b) {
566 assert(b->toxic);
567 (void) block_put(b);
568 b = NULL;
569 }
[c91f2d1b]570 *block = b;
[402a18f]571 return rc;
[fc840d9]572}
573
[d5a720cf]574/** Release a reference to a block.
575 *
[a6d97fb9]576 * If the last reference is dropped, the block is put on the free list.
[d5a720cf]577 *
578 * @param block Block of which a reference is to be released.
[c91f2d1b]579 *
580 * @return EOK on success or a negative error code.
[d5a720cf]581 */
[c91f2d1b]582int block_put(block_t *block)
[fc840d9]583{
[15f3c3f]584 devcon_t *devcon = devcon_search(block->service_id);
[d5a720cf]585 cache_t *cache;
[ddfc39a3]586 unsigned blocks_cached;
587 enum cache_mode mode;
[402a18f]588 int rc = EOK;
[d5a720cf]589
590 assert(devcon);
591 assert(devcon->cache);
[0f1cf7a]592 assert(block->refcnt >= 1);
[d5a720cf]593
594 cache = devcon->cache;
[ddfc39a3]595
596retry:
597 fibril_mutex_lock(&cache->lock);
598 blocks_cached = cache->blocks_cached;
599 mode = cache->mode;
600 fibril_mutex_unlock(&cache->lock);
601
602 /*
603 * Determine whether to sync the block. Syncing the block is best done
604 * when not holding the cache lock as it does not impede concurrency.
605 * Since the situation may have changed when we unlocked the cache, the
606 * blocks_cached and mode variables are mere hints. We will recheck the
607 * conditions later when the cache lock is held again.
608 */
609 fibril_mutex_lock(&block->lock);
[402a18f]610 if (block->toxic)
611 block->dirty = false; /* will not write back toxic block */
[ddfc39a3]612 if (block->dirty && (block->refcnt == 1) &&
613 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
[4802dd7]614 rc = write_blocks(devcon, block->pba, cache->blocks_cluster,
615 block->data, block->size);
[3d35386]616 if (rc == EOK)
617 block->write_failures = 0;
[ddfc39a3]618 block->dirty = false;
619 }
620 fibril_mutex_unlock(&block->lock);
621
[4e1b57d]622 fibril_mutex_lock(&cache->lock);
623 fibril_mutex_lock(&block->lock);
[d5a720cf]624 if (!--block->refcnt) {
625 /*
[d68e4d5]626 * Last reference to the block was dropped. Either free the
[402a18f]627 * block or put it on the free list. In case of an I/O error,
628 * free the block.
[d68e4d5]629 */
[402a18f]630 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
631 (rc != EOK)) {
[d68e4d5]632 /*
[402a18f]633 * Currently there are too many cached blocks or there
634 * was an I/O error when writing the block back to the
635 * device.
[d68e4d5]636 */
637 if (block->dirty) {
[ddfc39a3]638 /*
639 * We cannot sync the block while holding the
640 * cache lock. Release everything and retry.
641 */
642 block->refcnt++;
[3d35386]643
644 if (block->write_failures < MAX_WRITE_RETRIES) {
645 block->write_failures++;
646 fibril_mutex_unlock(&block->lock);
[c1f26834]647 fibril_mutex_unlock(&cache->lock);
[3d35386]648 goto retry;
649 } else {
650 printf("Too many errors writing block %"
651 PRIuOFF64 "from device handle %" PRIun "\n"
652 "SEVERE DATA LOSS POSSIBLE\n",
653 block->lba, devcon->service_id);
654 }
[d68e4d5]655 }
656 /*
657 * Take the block out of the cache and free it.
658 */
[062d900]659 hash_table_remove_item(&cache->block_hash, &block->hash_link);
[956d4df8]660 fibril_mutex_unlock(&block->lock);
[d68e4d5]661 free(block->data);
[b9e6205]662 free(block);
[d68e4d5]663 cache->blocks_cached--;
664 fibril_mutex_unlock(&cache->lock);
[402a18f]665 return rc;
[d68e4d5]666 }
667 /*
668 * Put the block on the free list.
[d5a720cf]669 */
[1fbe064b]670 if (cache->mode != CACHE_MODE_WB && block->dirty) {
[ddfc39a3]671 /*
672 * We cannot sync the block while holding the cache
673 * lock. Release everything and retry.
674 */
675 block->refcnt++;
676 fibril_mutex_unlock(&block->lock);
677 fibril_mutex_unlock(&cache->lock);
678 goto retry;
[1fbe064b]679 }
[b72efe8]680 list_append(&block->free_link, &cache->free_list);
[d5a720cf]681 }
[4e1b57d]682 fibril_mutex_unlock(&block->lock);
683 fibril_mutex_unlock(&cache->lock);
[c91f2d1b]684
[402a18f]685 return rc;
[d5a720cf]686}
687
[6408be3]688/** Read sequential data from a block device.
[d5a720cf]689 *
[15f3c3f]690 * @param service_id Service ID of the block device.
[4802dd7]691 * @param buf Buffer for holding one block
[d5a720cf]692 * @param bufpos Pointer to the first unread valid offset within the
693 * communication buffer.
694 * @param buflen Pointer to the number of unread bytes that are ready in
695 * the communication buffer.
696 * @param pos Device position to be read.
697 * @param dst Destination buffer.
698 * @param size Size of the destination buffer.
699 * @param block_size Block size to be used for the transfer.
700 *
701 * @return EOK on success or a negative return code on failure.
702 */
[4802dd7]703int block_seqread(service_id_t service_id, void *buf, size_t *bufpos,
704 size_t *buflen, aoff64_t *pos, void *dst, size_t size)
[d5a720cf]705{
[ed903174]706 size_t offset = 0;
[d5a720cf]707 size_t left = size;
[1ee00b7]708 size_t block_size;
709 devcon_t *devcon;
710
[15f3c3f]711 devcon = devcon_search(service_id);
[d5a720cf]712 assert(devcon);
[1ee00b7]713 block_size = devcon->pblock_size;
[e1c88d5]714
[d5a720cf]715 while (left > 0) {
716 size_t rd;
717
718 if (*bufpos + left < *buflen)
719 rd = left;
720 else
721 rd = *buflen - *bufpos;
722
723 if (rd > 0) {
724 /*
725 * Copy the contents of the communication buffer to the
726 * destination buffer.
727 */
[4802dd7]728 memcpy(dst + offset, buf + *bufpos, rd);
[d5a720cf]729 offset += rd;
730 *bufpos += rd;
731 *pos += rd;
732 left -= rd;
733 }
734
[ed903174]735 if (*bufpos == *buflen) {
[d5a720cf]736 /* Refill the communication buffer with a new block. */
[6408be3]737 int rc;
738
[4802dd7]739 rc = read_blocks(devcon, *pos / block_size, 1, buf,
740 devcon->pblock_size);
[d68e4d5]741 if (rc != EOK) {
[6408be3]742 return rc;
[d68e4d5]743 }
[d5a720cf]744
745 *bufpos = 0;
746 *buflen = block_size;
747 }
748 }
749
750 return EOK;
[fc840d9]751}
752
[00b1d20e]753/** Read blocks directly from device (bypass cache).
754 *
[15f3c3f]755 * @param service_id Service ID of the block device.
[a6ba0c9]756 * @param ba Address of first block (physical).
[00b1d20e]757 * @param cnt Number of blocks.
758 * @param src Buffer for storing the data.
759 *
760 * @return EOK on success or negative error code on failure.
761 */
[15f3c3f]762int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
[00b1d20e]763{
764 devcon_t *devcon;
765
[15f3c3f]766 devcon = devcon_search(service_id);
[00b1d20e]767 assert(devcon);
768
[4802dd7]769 return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt);
[00b1d20e]770}
771
772/** Write blocks directly to device (bypass cache).
773 *
[15f3c3f]774 * @param service_id Service ID of the block device.
[a6ba0c9]775 * @param ba Address of first block (physical).
[00b1d20e]776 * @param cnt Number of blocks.
777 * @param src The data to be written.
778 *
779 * @return EOK on success or negative error code on failure.
780 */
[15f3c3f]781int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
[00b1d20e]782 const void *data)
783{
784 devcon_t *devcon;
785
[15f3c3f]786 devcon = devcon_search(service_id);
[00b1d20e]787 assert(devcon);
788
[4802dd7]789 return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt);
[00b1d20e]790}
791
[78d50bd]792/** Synchronize blocks to persistent storage.
793 *
794 * @param service_id Service ID of the block device.
795 * @param ba Address of first block (physical).
796 * @param cnt Number of blocks.
797 *
798 * @return EOK on success or negative error code on failure.
799 */
800int block_sync_cache(service_id_t service_id, aoff64_t ba, size_t cnt)
801{
802 devcon_t *devcon;
803
804 devcon = devcon_search(service_id);
805 assert(devcon);
806
807 return bd_sync_cache(devcon->bd, ba, cnt);
808}
809
[00b1d20e]810/** Get device block size.
811 *
[15f3c3f]812 * @param service_id Service ID of the block device.
[00b1d20e]813 * @param bsize Output block size.
814 *
815 * @return EOK on success or negative error code on failure.
816 */
[15f3c3f]817int block_get_bsize(service_id_t service_id, size_t *bsize)
[00b1d20e]818{
819 devcon_t *devcon;
820
[15f3c3f]821 devcon = devcon_search(service_id);
[00b1d20e]822 assert(devcon);
[4802dd7]823
824 return bd_get_block_size(devcon->bd, bsize);
[00b1d20e]825}
826
[08232ee]827/** Get number of blocks on device.
828 *
[15f3c3f]829 * @param service_id Service ID of the block device.
[08232ee]830 * @param nblocks Output number of blocks.
831 *
832 * @return EOK on success or negative error code on failure.
833 */
[15f3c3f]834int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
[08232ee]835{
[15f3c3f]836 devcon_t *devcon = devcon_search(service_id);
[08232ee]837 assert(devcon);
[3d35386]838
[4802dd7]839 return bd_get_num_blocks(devcon->bd, nblocks);
[08232ee]840}
841
[e272949]842/** Read bytes directly from the device (bypass cache)
843 *
[15f3c3f]844 * @param service_id Service ID of the block device.
[e272949]845 * @param abs_offset Absolute offset in bytes where to start reading
846 * @param bytes Number of bytes to read
847 * @param data Buffer that receives the data
848 *
849 * @return EOK on success or negative error code on failure.
850 */
[15f3c3f]851int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
[e272949]852 size_t bytes, void *data)
853{
854 int rc;
855 size_t phys_block_size;
856 size_t buf_size;
857 void *buffer;
858 aoff64_t first_block;
859 aoff64_t last_block;
860 size_t blocks;
861 size_t offset;
862
[15f3c3f]863 rc = block_get_bsize(service_id, &phys_block_size);
[e272949]864 if (rc != EOK) {
865 return rc;
866 }
867
[c4aa9cf]868 /* calculate data position and required space */
[e272949]869 first_block = abs_offset / phys_block_size;
870 offset = abs_offset % phys_block_size;
871 last_block = (abs_offset + bytes - 1) / phys_block_size;
872 blocks = last_block - first_block + 1;
873 buf_size = blocks * phys_block_size;
874
[c4aa9cf]875 /* read the data into memory */
[e272949]876 buffer = malloc(buf_size);
877 if (buffer == NULL) {
878 return ENOMEM;
879 }
880
[15f3c3f]881 rc = block_read_direct(service_id, first_block, blocks, buffer);
[e272949]882 if (rc != EOK) {
883 free(buffer);
884 return rc;
885 }
886
[c4aa9cf]887 /* copy the data from the buffer */
[e272949]888 memcpy(data, buffer + offset, bytes);
889 free(buffer);
[f73b291]890
[e272949]891 return EOK;
892}
893
[4046b2f4]894/** Get TOC from device.
895 *
896 * @param service_id Service ID of the block device.
897 * @param session Starting session.
898 *
[08cba4b]899 * @return Allocated TOC structure.
[3abf70c7]900 * @return EOK on success or negative error code.
[4046b2f4]901 *
902 */
[3abf70c7]903int block_read_toc(service_id_t service_id, uint8_t session, void *buf,
904 size_t bufsize)
[4046b2f4]905{
906 devcon_t *devcon = devcon_search(service_id);
[08cba4b]907
[4802dd7]908 assert(devcon);
[3abf70c7]909 return bd_read_toc(devcon->bd, session, buf, bufsize);
[4046b2f4]910}
911
[1ee00b7]912/** Read blocks from block device.
[6408be3]913 *
914 * @param devcon Device connection.
[1ee00b7]915 * @param ba Address of first block.
916 * @param cnt Number of blocks.
[6408be3]917 * @param src Buffer for storing the data.
918 *
919 * @return EOK on success or negative error code on failure.
920 */
[4802dd7]921static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf,
922 size_t size)
[6408be3]923{
924 assert(devcon);
[79ae36dd]925
[4802dd7]926 int rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size);
[16fc3c9]927 if (rc != EOK) {
[c1694b6b]928 printf("Error %s reading %zu blocks starting at block %" PRIuOFF64
929 " from device handle %" PRIun "\n", str_error_name(rc), cnt, ba,
[15f3c3f]930 devcon->service_id);
[16fc3c9]931#ifndef NDEBUG
932 stacktrace_print();
933#endif
934 }
[79ae36dd]935
[1ee00b7]936 return rc;
[6408be3]937}
938
[1fbe064b]939/** Write block to block device.
940 *
941 * @param devcon Device connection.
[1ee00b7]942 * @param ba Address of first block.
943 * @param cnt Number of blocks.
[1fbe064b]944 * @param src Buffer containing the data to write.
945 *
946 * @return EOK on success or negative error code on failure.
947 */
[4802dd7]948static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data,
949 size_t size)
[1fbe064b]950{
951 assert(devcon);
[79ae36dd]952
[4802dd7]953 int rc = bd_write_blocks(devcon->bd, ba, cnt, data, size);
[16fc3c9]954 if (rc != EOK) {
[c1694b6b]955 printf("Error %s writing %zu blocks starting at block %" PRIuOFF64
956 " to device handle %" PRIun "\n", str_error_name(rc), cnt, ba, devcon->service_id);
[16fc3c9]957#ifndef NDEBUG
958 stacktrace_print();
959#endif
960 }
[79ae36dd]961
[1ee00b7]962 return rc;
963}
[1fbe064b]964
[f092718]965/** Convert logical block address to physical block address. */
966static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
967{
968 assert(devcon->cache != NULL);
969 return lba * devcon->cache->blocks_cluster;
970}
971
[fc840d9]972/** @}
973 */
Note: See TracBrowser for help on using the repository browser.