source: mainline/uspace/lib/block/block.c@ 5e904dd

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

Fix block comment formatting (ccheck).

  • 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
[b7fd2a0]90static errno_t read_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
91static errno_t 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);
[a35b458]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 }
[a35b458]104
[4e1b57d]105 fibril_mutex_unlock(&dcl_lock);
[916bf1a]106 return NULL;
107}
108
[b7fd2a0]109static errno_t 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;
[a35b458]113
[916bf1a]114 devcon = malloc(sizeof(devcon_t));
115 if (!devcon)
116 return ENOMEM;
[a35b458]117
[916bf1a]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;
[a35b458]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
[b7fd2a0]148errno_t 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 }
[a35b458]157
[b7fd2a0]158 errno_t rc = bd_open(sess, &bd);
[7858bc5f]159 if (rc != EOK) {
[79ae36dd]160 async_hangup(sess);
[7858bc5f]161 return rc;
162 }
[a35b458]163
[79ae36dd]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 }
[a35b458]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 }
[a35b458]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);
[a35b458]194
[64bc4b6]195 if (devcon->cache)
[15f3c3f]196 (void) block_cache_fini(service_id);
[a35b458]197
[dd8b6a8]198 (void)bd_sync_cache(devcon->bd, 0, 0);
[a35b458]199
[916bf1a]200 devcon_remove(devcon);
[a35b458]201
[6284978]202 if (devcon->bb_buf)
203 free(devcon->bb_buf);
[a35b458]204
[4802dd7]205 bd_close(devcon->bd);
[79ae36dd]206 async_hangup(devcon->sess);
[a35b458]207
[79ae36dd]208 free(devcon);
[7858bc5f]209}
210
[b7fd2a0]211errno_t block_bb_read(service_id_t service_id, aoff64_t ba)
[6284978]212{
213 void *bb_buf;
[b7fd2a0]214 errno_t 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) {
[1433ecda]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{
[1433ecda]246 aoff64_t *lba = (aoff64_t *)key;
[062d900]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{
[1433ecda]258 aoff64_t *lba = (aoff64_t *)key;
[062d900]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
[b7fd2a0]272errno_t 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;
[a35b458]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
[b7fd2a0]309errno_t block_cache_fini(service_id_t service_id)
[64bc4b6]310{
[15f3c3f]311 devcon_t *devcon = devcon_search(service_id);
[64bc4b6]312 cache_t *cache;
[b7fd2a0]313 errno_t rc;
[64bc4b6]314
315 if (!devcon)
316 return ENOENT;
317 if (!devcon->cache)
318 return EOK;
319 cache = devcon->cache;
[a35b458]320
[64bc4b6]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);
[a35b458]339
[64bc4b6]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
[1b20da0]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 *
[cde999a]383 * @return EOK on success or an error code.
[e1c88d5]384 */
[b7fd2a0]385errno_t 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;
[b7fd2a0]392 errno_t rc;
[a35b458]393
[15f3c3f]394 devcon = devcon_search(service_id);
[fc840d9]395
[e1c88d5]396 assert(devcon);
397 assert(devcon->cache);
[a35b458]398
[e1c88d5]399 cache = devcon->cache;
[02ee6bf5]400
[7c3fb9b]401 /*
402 * Check whether the logical block (or part of it) is beyond
[3d35386]403 * the end of the device or not.
404 */
405 p_ba = ba_ltop(devcon, ba);
406 p_ba += cache->blocks_cluster;
407 if (p_ba >= devcon->pblocks) {
408 /* This request cannot be satisfied */
409 return EIO;
410 }
411
412
[02ee6bf5]413retry:
[b7b3fda]414 rc = EOK;
[4f690cd]415 b = NULL;
[b7b3fda]416
[4e1b57d]417 fibril_mutex_lock(&cache->lock);
[062d900]418 ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
419 if (hlink) {
[1433ecda]420 found:
[e1c88d5]421 /*
422 * We found the block in the cache.
423 */
[062d900]424 b = hash_table_get_inst(hlink, block_t, hash_link);
[4e1b57d]425 fibril_mutex_lock(&b->lock);
[e1c88d5]426 if (b->refcnt++ == 0)
427 list_remove(&b->free_link);
[402a18f]428 if (b->toxic)
429 rc = EIO;
[4e1b57d]430 fibril_mutex_unlock(&b->lock);
431 fibril_mutex_unlock(&cache->lock);
[e1c88d5]432 } else {
433 /*
434 * The block was not found in the cache.
435 */
436 if (cache_can_grow(cache)) {
437 /*
438 * We can grow the cache by allocating new blocks.
439 * Should the allocation fail, we fail over and try to
440 * recycle a block from the cache.
441 */
442 b = malloc(sizeof(block_t));
443 if (!b)
444 goto recycle;
[1ee00b7]445 b->data = malloc(cache->lblock_size);
[e1c88d5]446 if (!b->data) {
447 free(b);
[0dfaa099]448 b = NULL;
[e1c88d5]449 goto recycle;
450 }
[d68e4d5]451 cache->blocks_cached++;
[e1c88d5]452 } else {
453 /*
454 * Try to recycle a block from the free list.
455 */
[1433ecda]456 recycle:
[b72efe8]457 if (list_empty(&cache->free_list)) {
[7a56b1ed]458 fibril_mutex_unlock(&cache->lock);
459 rc = ENOMEM;
460 goto out;
461 }
[062d900]462 link = list_first(&cache->free_list);
463 b = list_get_instance(link, block_t, free_link);
[02ee6bf5]464
465 fibril_mutex_lock(&b->lock);
466 if (b->dirty) {
467 /*
468 * The block needs to be written back to the
469 * device before it changes identity. Do this
470 * while not holding the cache lock so that
471 * concurrency is not impeded. Also move the
472 * block to the end of the free list so that we
473 * do not slow down other instances of
474 * block_get() draining the free list.
475 */
476 list_remove(&b->free_link);
[b72efe8]477 list_append(&b->free_link, &cache->free_list);
[02ee6bf5]478 fibril_mutex_unlock(&cache->lock);
[f092718]479 rc = write_blocks(devcon, b->pba,
[4802dd7]480 cache->blocks_cluster, b->data, b->size);
[402a18f]481 if (rc != EOK) {
482 /*
483 * We did not manage to write the block
484 * to the device. Keep it around for
485 * another try. Hopefully, we will grab
486 * another block next time.
487 */
[3d35386]488 if (b->write_failures < MAX_WRITE_RETRIES) {
489 b->write_failures++;
490 fibril_mutex_unlock(&b->lock);
491 goto retry;
492 } else {
493 printf("Too many errors writing block %"
[1433ecda]494 PRIuOFF64 "from device handle %" PRIun "\n"
[3d35386]495 "SEVERE DATA LOSS POSSIBLE\n",
[1433ecda]496 b->lba, devcon->service_id);
[3d35386]497 }
498 } else
499 b->write_failures = 0;
500
[02ee6bf5]501 b->dirty = false;
502 if (!fibril_mutex_trylock(&cache->lock)) {
503 /*
504 * Somebody is probably racing with us.
505 * Unlock the block and retry.
506 */
507 fibril_mutex_unlock(&b->lock);
508 goto retry;
509 }
[062d900]510 hlink = hash_table_find(&cache->block_hash, &ba);
511 if (hlink) {
[5716e9a]512 /*
513 * Someone else must have already
514 * instantiated the block while we were
515 * not holding the cache lock.
516 * Leave the recycled block on the
517 * freelist and continue as if we
518 * found the block of interest during
519 * the first try.
520 */
521 fibril_mutex_unlock(&b->lock);
522 goto found;
523 }
[02ee6bf5]524
525 }
526 fibril_mutex_unlock(&b->lock);
527
528 /*
529 * Unlink the block from the free list and the hash
530 * table.
531 */
532 list_remove(&b->free_link);
[062d900]533 hash_table_remove_item(&cache->block_hash, &b->hash_link);
[e1c88d5]534 }
[fc840d9]535
[e1c88d5]536 block_initialize(b);
[15f3c3f]537 b->service_id = service_id;
[1ee00b7]538 b->size = cache->lblock_size;
[a6ba0c9]539 b->lba = ba;
540 b->pba = ba_ltop(devcon, b->lba);
[062d900]541 hash_table_insert(&cache->block_hash, &b->hash_link);
[a6d97fb9]542
543 /*
544 * Lock the block before releasing the cache lock. Thus we don't
[5ac8918]545 * kill concurrent operations on the cache while doing I/O on
546 * the block.
[a6d97fb9]547 */
[4e1b57d]548 fibril_mutex_lock(&b->lock);
549 fibril_mutex_unlock(&cache->lock);
[a6d97fb9]550
[1d8cdb1]551 if (!(flags & BLOCK_FLAGS_NOREAD)) {
552 /*
553 * The block contains old or no data. We need to read
554 * the new contents from the device.
555 */
[4802dd7]556 rc = read_blocks(devcon, b->pba, cache->blocks_cluster,
557 b->data, cache->lblock_size);
[1b20da0]558 if (rc != EOK)
[402a18f]559 b->toxic = true;
560 } else
561 rc = EOK;
[fc840d9]562
[4e1b57d]563 fibril_mutex_unlock(&b->lock);
[a6d97fb9]564 }
[7a56b1ed]565out:
[4f690cd]566 if ((rc != EOK) && b) {
567 assert(b->toxic);
568 (void) block_put(b);
569 b = NULL;
570 }
[c91f2d1b]571 *block = b;
[402a18f]572 return rc;
[fc840d9]573}
574
[d5a720cf]575/** Release a reference to a block.
576 *
[a6d97fb9]577 * If the last reference is dropped, the block is put on the free list.
[d5a720cf]578 *
579 * @param block Block of which a reference is to be released.
[c91f2d1b]580 *
[cde999a]581 * @return EOK on success or an error code.
[d5a720cf]582 */
[b7fd2a0]583errno_t block_put(block_t *block)
[fc840d9]584{
[15f3c3f]585 devcon_t *devcon = devcon_search(block->service_id);
[d5a720cf]586 cache_t *cache;
[ddfc39a3]587 unsigned blocks_cached;
588 enum cache_mode mode;
[b7fd2a0]589 errno_t rc = EOK;
[d5a720cf]590
591 assert(devcon);
592 assert(devcon->cache);
[0f1cf7a]593 assert(block->refcnt >= 1);
[d5a720cf]594
595 cache = devcon->cache;
[ddfc39a3]596
597retry:
598 fibril_mutex_lock(&cache->lock);
599 blocks_cached = cache->blocks_cached;
600 mode = cache->mode;
601 fibril_mutex_unlock(&cache->lock);
602
603 /*
604 * Determine whether to sync the block. Syncing the block is best done
605 * when not holding the cache lock as it does not impede concurrency.
606 * Since the situation may have changed when we unlocked the cache, the
607 * blocks_cached and mode variables are mere hints. We will recheck the
608 * conditions later when the cache lock is held again.
609 */
610 fibril_mutex_lock(&block->lock);
[402a18f]611 if (block->toxic)
612 block->dirty = false; /* will not write back toxic block */
[ddfc39a3]613 if (block->dirty && (block->refcnt == 1) &&
614 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
[4802dd7]615 rc = write_blocks(devcon, block->pba, cache->blocks_cluster,
616 block->data, block->size);
[3d35386]617 if (rc == EOK)
618 block->write_failures = 0;
[ddfc39a3]619 block->dirty = false;
620 }
621 fibril_mutex_unlock(&block->lock);
622
[4e1b57d]623 fibril_mutex_lock(&cache->lock);
624 fibril_mutex_lock(&block->lock);
[d5a720cf]625 if (!--block->refcnt) {
626 /*
[d68e4d5]627 * Last reference to the block was dropped. Either free the
[402a18f]628 * block or put it on the free list. In case of an I/O error,
629 * free the block.
[d68e4d5]630 */
[402a18f]631 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
632 (rc != EOK)) {
[d68e4d5]633 /*
[402a18f]634 * Currently there are too many cached blocks or there
635 * was an I/O error when writing the block back to the
636 * device.
[d68e4d5]637 */
638 if (block->dirty) {
[ddfc39a3]639 /*
640 * We cannot sync the block while holding the
641 * cache lock. Release everything and retry.
642 */
643 block->refcnt++;
[3d35386]644
645 if (block->write_failures < MAX_WRITE_RETRIES) {
646 block->write_failures++;
647 fibril_mutex_unlock(&block->lock);
[c1f26834]648 fibril_mutex_unlock(&cache->lock);
[3d35386]649 goto retry;
650 } else {
651 printf("Too many errors writing block %"
[1433ecda]652 PRIuOFF64 "from device handle %" PRIun "\n"
[3d35386]653 "SEVERE DATA LOSS POSSIBLE\n",
[1433ecda]654 block->lba, devcon->service_id);
[3d35386]655 }
[d68e4d5]656 }
657 /*
658 * Take the block out of the cache and free it.
659 */
[062d900]660 hash_table_remove_item(&cache->block_hash, &block->hash_link);
[956d4df8]661 fibril_mutex_unlock(&block->lock);
[d68e4d5]662 free(block->data);
[b9e6205]663 free(block);
[d68e4d5]664 cache->blocks_cached--;
665 fibril_mutex_unlock(&cache->lock);
[402a18f]666 return rc;
[d68e4d5]667 }
668 /*
669 * Put the block on the free list.
[d5a720cf]670 */
[1fbe064b]671 if (cache->mode != CACHE_MODE_WB && block->dirty) {
[ddfc39a3]672 /*
673 * We cannot sync the block while holding the cache
674 * lock. Release everything and retry.
675 */
676 block->refcnt++;
677 fibril_mutex_unlock(&block->lock);
678 fibril_mutex_unlock(&cache->lock);
679 goto retry;
[1fbe064b]680 }
[b72efe8]681 list_append(&block->free_link, &cache->free_list);
[d5a720cf]682 }
[4e1b57d]683 fibril_mutex_unlock(&block->lock);
684 fibril_mutex_unlock(&cache->lock);
[c91f2d1b]685
[402a18f]686 return rc;
[d5a720cf]687}
688
[6408be3]689/** Read sequential data from a block device.
[d5a720cf]690 *
[15f3c3f]691 * @param service_id Service ID of the block device.
[4802dd7]692 * @param buf Buffer for holding one block
[d5a720cf]693 * @param bufpos Pointer to the first unread valid offset within the
694 * communication buffer.
695 * @param buflen Pointer to the number of unread bytes that are ready in
696 * the communication buffer.
697 * @param pos Device position to be read.
698 * @param dst Destination buffer.
699 * @param size Size of the destination buffer.
700 * @param block_size Block size to be used for the transfer.
701 *
[cde999a]702 * @return EOK on success or an error code on failure.
[d5a720cf]703 */
[b7fd2a0]704errno_t block_seqread(service_id_t service_id, void *buf, size_t *bufpos,
[4802dd7]705 size_t *buflen, aoff64_t *pos, void *dst, size_t size)
[d5a720cf]706{
[ed903174]707 size_t offset = 0;
[d5a720cf]708 size_t left = size;
[1ee00b7]709 size_t block_size;
710 devcon_t *devcon;
711
[15f3c3f]712 devcon = devcon_search(service_id);
[d5a720cf]713 assert(devcon);
[1ee00b7]714 block_size = devcon->pblock_size;
[a35b458]715
[d5a720cf]716 while (left > 0) {
717 size_t rd;
[a35b458]718
[d5a720cf]719 if (*bufpos + left < *buflen)
720 rd = left;
721 else
722 rd = *buflen - *bufpos;
[a35b458]723
[d5a720cf]724 if (rd > 0) {
725 /*
726 * Copy the contents of the communication buffer to the
727 * destination buffer.
728 */
[4802dd7]729 memcpy(dst + offset, buf + *bufpos, rd);
[d5a720cf]730 offset += rd;
731 *bufpos += rd;
732 *pos += rd;
733 left -= rd;
734 }
[a35b458]735
[ed903174]736 if (*bufpos == *buflen) {
[d5a720cf]737 /* Refill the communication buffer with a new block. */
[b7fd2a0]738 errno_t rc;
[6408be3]739
[4802dd7]740 rc = read_blocks(devcon, *pos / block_size, 1, buf,
741 devcon->pblock_size);
[d68e4d5]742 if (rc != EOK) {
[6408be3]743 return rc;
[d68e4d5]744 }
[a35b458]745
[d5a720cf]746 *bufpos = 0;
747 *buflen = block_size;
748 }
749 }
[a35b458]750
[d5a720cf]751 return EOK;
[fc840d9]752}
753
[00b1d20e]754/** Read blocks directly from device (bypass cache).
755 *
[15f3c3f]756 * @param service_id Service ID of the block device.
[a6ba0c9]757 * @param ba Address of first block (physical).
[00b1d20e]758 * @param cnt Number of blocks.
759 * @param src Buffer for storing the data.
760 *
[cde999a]761 * @return EOK on success or an error code on failure.
[00b1d20e]762 */
[b7fd2a0]763errno_t block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
[00b1d20e]764{
765 devcon_t *devcon;
766
[15f3c3f]767 devcon = devcon_search(service_id);
[00b1d20e]768 assert(devcon);
769
[4802dd7]770 return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt);
[00b1d20e]771}
772
773/** Write blocks directly to device (bypass cache).
774 *
[15f3c3f]775 * @param service_id Service ID of the block device.
[a6ba0c9]776 * @param ba Address of first block (physical).
[00b1d20e]777 * @param cnt Number of blocks.
778 * @param src The data to be written.
779 *
[cde999a]780 * @return EOK on success or an error code on failure.
[00b1d20e]781 */
[b7fd2a0]782errno_t block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
[00b1d20e]783 const void *data)
784{
785 devcon_t *devcon;
786
[15f3c3f]787 devcon = devcon_search(service_id);
[00b1d20e]788 assert(devcon);
789
[4802dd7]790 return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt);
[00b1d20e]791}
792
[78d50bd]793/** Synchronize blocks to persistent storage.
794 *
795 * @param service_id Service ID of the block device.
796 * @param ba Address of first block (physical).
797 * @param cnt Number of blocks.
798 *
[cde999a]799 * @return EOK on success or an error code on failure.
[78d50bd]800 */
[b7fd2a0]801errno_t block_sync_cache(service_id_t service_id, aoff64_t ba, size_t cnt)
[78d50bd]802{
803 devcon_t *devcon;
804
805 devcon = devcon_search(service_id);
806 assert(devcon);
807
808 return bd_sync_cache(devcon->bd, ba, cnt);
809}
810
[00b1d20e]811/** Get device block size.
812 *
[15f3c3f]813 * @param service_id Service ID of the block device.
[00b1d20e]814 * @param bsize Output block size.
815 *
[cde999a]816 * @return EOK on success or an error code on failure.
[00b1d20e]817 */
[b7fd2a0]818errno_t block_get_bsize(service_id_t service_id, size_t *bsize)
[00b1d20e]819{
820 devcon_t *devcon;
821
[15f3c3f]822 devcon = devcon_search(service_id);
[00b1d20e]823 assert(devcon);
[4802dd7]824
825 return bd_get_block_size(devcon->bd, bsize);
[00b1d20e]826}
827
[08232ee]828/** Get number of blocks on device.
829 *
[15f3c3f]830 * @param service_id Service ID of the block device.
[08232ee]831 * @param nblocks Output number of blocks.
832 *
[cde999a]833 * @return EOK on success or an error code on failure.
[08232ee]834 */
[b7fd2a0]835errno_t block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
[08232ee]836{
[15f3c3f]837 devcon_t *devcon = devcon_search(service_id);
[08232ee]838 assert(devcon);
[3d35386]839
[4802dd7]840 return bd_get_num_blocks(devcon->bd, nblocks);
[08232ee]841}
842
[e272949]843/** Read bytes directly from the device (bypass cache)
[1b20da0]844 *
[15f3c3f]845 * @param service_id Service ID of the block device.
[e272949]846 * @param abs_offset Absolute offset in bytes where to start reading
847 * @param bytes Number of bytes to read
848 * @param data Buffer that receives the data
[1b20da0]849 *
[cde999a]850 * @return EOK on success or an error code on failure.
[e272949]851 */
[b7fd2a0]852errno_t block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
[e272949]853 size_t bytes, void *data)
854{
[b7fd2a0]855 errno_t rc;
[e272949]856 size_t phys_block_size;
857 size_t buf_size;
858 void *buffer;
859 aoff64_t first_block;
860 aoff64_t last_block;
861 size_t blocks;
862 size_t offset;
[a35b458]863
[15f3c3f]864 rc = block_get_bsize(service_id, &phys_block_size);
[e272949]865 if (rc != EOK) {
866 return rc;
867 }
[a35b458]868
[c4aa9cf]869 /* calculate data position and required space */
[e272949]870 first_block = abs_offset / phys_block_size;
871 offset = abs_offset % phys_block_size;
872 last_block = (abs_offset + bytes - 1) / phys_block_size;
873 blocks = last_block - first_block + 1;
874 buf_size = blocks * phys_block_size;
[a35b458]875
[c4aa9cf]876 /* read the data into memory */
[e272949]877 buffer = malloc(buf_size);
878 if (buffer == NULL) {
879 return ENOMEM;
880 }
[a35b458]881
[15f3c3f]882 rc = block_read_direct(service_id, first_block, blocks, buffer);
[e272949]883 if (rc != EOK) {
884 free(buffer);
885 return rc;
886 }
[a35b458]887
[c4aa9cf]888 /* copy the data from the buffer */
[e272949]889 memcpy(data, buffer + offset, bytes);
890 free(buffer);
[a35b458]891
[e272949]892 return EOK;
893}
894
[4046b2f4]895/** Get TOC from device.
896 *
897 * @param service_id Service ID of the block device.
898 * @param session Starting session.
899 *
[08cba4b]900 * @return Allocated TOC structure.
[cde999a]901 * @return EOK on success or an error code.
[4046b2f4]902 *
903 */
[b7fd2a0]904errno_t block_read_toc(service_id_t service_id, uint8_t session, void *buf,
[3abf70c7]905 size_t bufsize)
[4046b2f4]906{
907 devcon_t *devcon = devcon_search(service_id);
[a35b458]908
[4802dd7]909 assert(devcon);
[3abf70c7]910 return bd_read_toc(devcon->bd, session, buf, bufsize);
[4046b2f4]911}
912
[1ee00b7]913/** Read blocks from block device.
[6408be3]914 *
915 * @param devcon Device connection.
[1ee00b7]916 * @param ba Address of first block.
917 * @param cnt Number of blocks.
[6408be3]918 * @param src Buffer for storing the data.
919 *
[cde999a]920 * @return EOK on success or an error code on failure.
[6408be3]921 */
[b7fd2a0]922static errno_t read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf,
[4802dd7]923 size_t size)
[6408be3]924{
925 assert(devcon);
[a35b458]926
[b7fd2a0]927 errno_t rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size);
[16fc3c9]928 if (rc != EOK) {
[c1694b6b]929 printf("Error %s reading %zu blocks starting at block %" PRIuOFF64
930 " from device handle %" PRIun "\n", str_error_name(rc), cnt, ba,
[15f3c3f]931 devcon->service_id);
[16fc3c9]932#ifndef NDEBUG
933 stacktrace_print();
934#endif
935 }
[a35b458]936
[1ee00b7]937 return rc;
[6408be3]938}
939
[1fbe064b]940/** Write block to block device.
941 *
942 * @param devcon Device connection.
[1ee00b7]943 * @param ba Address of first block.
944 * @param cnt Number of blocks.
[1fbe064b]945 * @param src Buffer containing the data to write.
946 *
[cde999a]947 * @return EOK on success or an error code on failure.
[1fbe064b]948 */
[b7fd2a0]949static errno_t write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data,
[4802dd7]950 size_t size)
[1fbe064b]951{
952 assert(devcon);
[a35b458]953
[b7fd2a0]954 errno_t rc = bd_write_blocks(devcon->bd, ba, cnt, data, size);
[16fc3c9]955 if (rc != EOK) {
[c1694b6b]956 printf("Error %s writing %zu blocks starting at block %" PRIuOFF64
957 " to device handle %" PRIun "\n", str_error_name(rc), cnt, ba, devcon->service_id);
[16fc3c9]958#ifndef NDEBUG
959 stacktrace_print();
960#endif
961 }
[a35b458]962
[1ee00b7]963 return rc;
964}
[1fbe064b]965
[f092718]966/** Convert logical block address to physical block address. */
967static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
968{
969 assert(devcon->cache != NULL);
970 return lba * devcon->cache->blocks_cluster;
971}
972
[fc840d9]973/** @}
974 */
Note: See TracBrowser for help on using the repository browser.