| 1 | /* | 
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar | 
|---|
| 3 | * Copyright (c) 2008 Martin Decky | 
|---|
| 4 | * Copyright (c) 2011 Martin Sucha | 
|---|
| 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 |  | 
|---|
| 31 | /** @addtogroup libblock | 
|---|
| 32 | * @{ | 
|---|
| 33 | */ | 
|---|
| 34 | /** | 
|---|
| 35 | * @file | 
|---|
| 36 | * @brief | 
|---|
| 37 | */ | 
|---|
| 38 |  | 
|---|
| 39 | #include <ipc/loc.h> | 
|---|
| 40 | #include <ipc/services.h> | 
|---|
| 41 | #include <errno.h> | 
|---|
| 42 | #include <async.h> | 
|---|
| 43 | #include <as.h> | 
|---|
| 44 | #include <assert.h> | 
|---|
| 45 | #include <bd.h> | 
|---|
| 46 | #include <fibril_synch.h> | 
|---|
| 47 | #include <adt/list.h> | 
|---|
| 48 | #include <adt/hash_table.h> | 
|---|
| 49 | #include <macros.h> | 
|---|
| 50 | #include <mem.h> | 
|---|
| 51 | #include <stdlib.h> | 
|---|
| 52 | #include <stdio.h> | 
|---|
| 53 | #include <stacktrace.h> | 
|---|
| 54 | #include <str_error.h> | 
|---|
| 55 | #include <offset.h> | 
|---|
| 56 | #include <inttypes.h> | 
|---|
| 57 | #include "block.h" | 
|---|
| 58 |  | 
|---|
| 59 | #define MAX_WRITE_RETRIES 10 | 
|---|
| 60 |  | 
|---|
| 61 | /** Lock protecting the device connection list */ | 
|---|
| 62 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock); | 
|---|
| 63 | /** Device connection list head. */ | 
|---|
| 64 | static LIST_INITIALIZE(dcl); | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | typedef struct { | 
|---|
| 68 | fibril_mutex_t lock; | 
|---|
| 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. */ | 
|---|
| 73 | hash_table_t block_hash; | 
|---|
| 74 | list_t free_list; | 
|---|
| 75 | enum cache_mode mode; | 
|---|
| 76 | } cache_t; | 
|---|
| 77 |  | 
|---|
| 78 | typedef struct { | 
|---|
| 79 | link_t link; | 
|---|
| 80 | service_id_t service_id; | 
|---|
| 81 | async_sess_t *sess; | 
|---|
| 82 | bd_t *bd; | 
|---|
| 83 | void *bb_buf; | 
|---|
| 84 | aoff64_t bb_addr; | 
|---|
| 85 | aoff64_t pblocks;    /**< Number of physical blocks */ | 
|---|
| 86 | size_t pblock_size;  /**< Physical block size. */ | 
|---|
| 87 | cache_t *cache; | 
|---|
| 88 | } devcon_t; | 
|---|
| 89 |  | 
|---|
| 90 | static errno_t read_blocks(devcon_t *, aoff64_t, size_t, void *, size_t); | 
|---|
| 91 | static errno_t write_blocks(devcon_t *, aoff64_t, size_t, void *, size_t); | 
|---|
| 92 | static aoff64_t ba_ltop(devcon_t *, aoff64_t); | 
|---|
| 93 |  | 
|---|
| 94 | static devcon_t *devcon_search(service_id_t service_id) | 
|---|
| 95 | { | 
|---|
| 96 | fibril_mutex_lock(&dcl_lock); | 
|---|
| 97 |  | 
|---|
| 98 | list_foreach(dcl, link, devcon_t, devcon) { | 
|---|
| 99 | if (devcon->service_id == service_id) { | 
|---|
| 100 | fibril_mutex_unlock(&dcl_lock); | 
|---|
| 101 | return devcon; | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | fibril_mutex_unlock(&dcl_lock); | 
|---|
| 106 | return NULL; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | static errno_t devcon_add(service_id_t service_id, async_sess_t *sess, | 
|---|
| 110 | size_t bsize, aoff64_t dev_size, bd_t *bd) | 
|---|
| 111 | { | 
|---|
| 112 | devcon_t *devcon; | 
|---|
| 113 |  | 
|---|
| 114 | devcon = malloc(sizeof(devcon_t)); | 
|---|
| 115 | if (!devcon) | 
|---|
| 116 | return ENOMEM; | 
|---|
| 117 |  | 
|---|
| 118 | link_initialize(&devcon->link); | 
|---|
| 119 | devcon->service_id = service_id; | 
|---|
| 120 | devcon->sess = sess; | 
|---|
| 121 | devcon->bd = bd; | 
|---|
| 122 | devcon->bb_buf = NULL; | 
|---|
| 123 | devcon->bb_addr = 0; | 
|---|
| 124 | devcon->pblock_size = bsize; | 
|---|
| 125 | devcon->pblocks = dev_size; | 
|---|
| 126 | devcon->cache = NULL; | 
|---|
| 127 |  | 
|---|
| 128 | fibril_mutex_lock(&dcl_lock); | 
|---|
| 129 | list_foreach(dcl, link, devcon_t, d) { | 
|---|
| 130 | if (d->service_id == service_id) { | 
|---|
| 131 | fibril_mutex_unlock(&dcl_lock); | 
|---|
| 132 | free(devcon); | 
|---|
| 133 | return EEXIST; | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 | list_append(&devcon->link, &dcl); | 
|---|
| 137 | fibril_mutex_unlock(&dcl_lock); | 
|---|
| 138 | return EOK; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | static void devcon_remove(devcon_t *devcon) | 
|---|
| 142 | { | 
|---|
| 143 | fibril_mutex_lock(&dcl_lock); | 
|---|
| 144 | list_remove(&devcon->link); | 
|---|
| 145 | fibril_mutex_unlock(&dcl_lock); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | errno_t block_init(service_id_t service_id, size_t comm_size) | 
|---|
| 149 | { | 
|---|
| 150 | bd_t *bd; | 
|---|
| 151 |  | 
|---|
| 152 | async_sess_t *sess = loc_service_connect(service_id, INTERFACE_BLOCK, | 
|---|
| 153 | IPC_FLAG_BLOCKING); | 
|---|
| 154 | if (!sess) { | 
|---|
| 155 | return ENOENT; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | errno_t rc = bd_open(sess, &bd); | 
|---|
| 159 | if (rc != EOK) { | 
|---|
| 160 | async_hangup(sess); | 
|---|
| 161 | return rc; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | size_t bsize; | 
|---|
| 165 | rc = bd_get_block_size(bd, &bsize); | 
|---|
| 166 | if (rc != EOK) { | 
|---|
| 167 | bd_close(bd); | 
|---|
| 168 | async_hangup(sess); | 
|---|
| 169 | return rc; | 
|---|
| 170 | } | 
|---|
| 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 | } | 
|---|
| 179 |  | 
|---|
| 180 | rc = devcon_add(service_id, sess, bsize, dev_size, bd); | 
|---|
| 181 | if (rc != EOK) { | 
|---|
| 182 | bd_close(bd); | 
|---|
| 183 | async_hangup(sess); | 
|---|
| 184 | return rc; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | return EOK; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | void block_fini(service_id_t service_id) | 
|---|
| 191 | { | 
|---|
| 192 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 193 | assert(devcon); | 
|---|
| 194 |  | 
|---|
| 195 | if (devcon->cache) | 
|---|
| 196 | (void) block_cache_fini(service_id); | 
|---|
| 197 |  | 
|---|
| 198 | (void)bd_sync_cache(devcon->bd, 0, 0); | 
|---|
| 199 |  | 
|---|
| 200 | devcon_remove(devcon); | 
|---|
| 201 |  | 
|---|
| 202 | if (devcon->bb_buf) | 
|---|
| 203 | free(devcon->bb_buf); | 
|---|
| 204 |  | 
|---|
| 205 | bd_close(devcon->bd); | 
|---|
| 206 | async_hangup(devcon->sess); | 
|---|
| 207 |  | 
|---|
| 208 | free(devcon); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | errno_t block_bb_read(service_id_t service_id, aoff64_t ba) | 
|---|
| 212 | { | 
|---|
| 213 | void *bb_buf; | 
|---|
| 214 | errno_t rc; | 
|---|
| 215 |  | 
|---|
| 216 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 217 | if (!devcon) | 
|---|
| 218 | return ENOENT; | 
|---|
| 219 | if (devcon->bb_buf) | 
|---|
| 220 | return EEXIST; | 
|---|
| 221 | bb_buf = malloc(devcon->pblock_size); | 
|---|
| 222 | if (!bb_buf) | 
|---|
| 223 | return ENOMEM; | 
|---|
| 224 |  | 
|---|
| 225 | rc = read_blocks(devcon, 0, 1, bb_buf, devcon->pblock_size); | 
|---|
| 226 | if (rc != EOK) { | 
|---|
| 227 | free(bb_buf); | 
|---|
| 228 | return rc; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | devcon->bb_buf = bb_buf; | 
|---|
| 232 | devcon->bb_addr = ba; | 
|---|
| 233 |  | 
|---|
| 234 | return EOK; | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | void *block_bb_get(service_id_t service_id) | 
|---|
| 238 | { | 
|---|
| 239 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 240 | assert(devcon); | 
|---|
| 241 | return devcon->bb_buf; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | static size_t cache_key_hash(void *key) | 
|---|
| 245 | { | 
|---|
| 246 | aoff64_t *lba = (aoff64_t *)key; | 
|---|
| 247 | return *lba; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | static size_t cache_hash(const ht_link_t *item) | 
|---|
| 251 | { | 
|---|
| 252 | block_t *b = hash_table_get_inst(item, block_t, hash_link); | 
|---|
| 253 | return b->lba; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | static bool cache_key_equal(void *key, const ht_link_t *item) | 
|---|
| 257 | { | 
|---|
| 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; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 |  | 
|---|
| 264 | static hash_table_ops_t cache_ops = { | 
|---|
| 265 | .hash = cache_hash, | 
|---|
| 266 | .key_hash = cache_key_hash, | 
|---|
| 267 | .key_equal = cache_key_equal, | 
|---|
| 268 | .equal = NULL, | 
|---|
| 269 | .remove_callback = NULL | 
|---|
| 270 | }; | 
|---|
| 271 |  | 
|---|
| 272 | errno_t block_cache_init(service_id_t service_id, size_t size, unsigned blocks, | 
|---|
| 273 | enum cache_mode mode) | 
|---|
| 274 | { | 
|---|
| 275 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 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 |  | 
|---|
| 285 | fibril_mutex_initialize(&cache->lock); | 
|---|
| 286 | list_initialize(&cache->free_list); | 
|---|
| 287 | cache->lblock_size = size; | 
|---|
| 288 | cache->block_count = blocks; | 
|---|
| 289 | cache->blocks_cached = 0; | 
|---|
| 290 | cache->mode = mode; | 
|---|
| 291 |  | 
|---|
| 292 | /* Allow 1:1 or small-to-large block size translation */ | 
|---|
| 293 | if (cache->lblock_size % devcon->pblock_size != 0) { | 
|---|
| 294 | free(cache); | 
|---|
| 295 | return ENOTSUP; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | cache->blocks_cluster = cache->lblock_size / devcon->pblock_size; | 
|---|
| 299 |  | 
|---|
| 300 | if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) { | 
|---|
| 301 | free(cache); | 
|---|
| 302 | return ENOMEM; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | devcon->cache = cache; | 
|---|
| 306 | return EOK; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | errno_t block_cache_fini(service_id_t service_id) | 
|---|
| 310 | { | 
|---|
| 311 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 312 | cache_t *cache; | 
|---|
| 313 | errno_t 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 | */ | 
|---|
| 326 | while (!list_empty(&cache->free_list)) { | 
|---|
| 327 | block_t *b = list_get_instance(list_first(&cache->free_list), | 
|---|
| 328 | block_t, free_link); | 
|---|
| 329 |  | 
|---|
| 330 | list_remove(&b->free_link); | 
|---|
| 331 | if (b->dirty) { | 
|---|
| 332 | rc = write_blocks(devcon, b->pba, cache->blocks_cluster, | 
|---|
| 333 | b->data, b->size); | 
|---|
| 334 | if (rc != EOK) | 
|---|
| 335 | return rc; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | hash_table_remove_item(&cache->block_hash, &b->hash_link); | 
|---|
| 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 |  | 
|---|
| 351 | #define CACHE_LO_WATERMARK      10 | 
|---|
| 352 | #define CACHE_HI_WATERMARK      20 | 
|---|
| 353 | static bool cache_can_grow(cache_t *cache) | 
|---|
| 354 | { | 
|---|
| 355 | if (cache->blocks_cached < CACHE_LO_WATERMARK) | 
|---|
| 356 | return true; | 
|---|
| 357 | if (!list_empty(&cache->free_list)) | 
|---|
| 358 | return false; | 
|---|
| 359 | return true; | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | static void block_initialize(block_t *b) | 
|---|
| 363 | { | 
|---|
| 364 | fibril_mutex_initialize(&b->lock); | 
|---|
| 365 | b->refcnt = 1; | 
|---|
| 366 | b->write_failures = 0; | 
|---|
| 367 | b->dirty = false; | 
|---|
| 368 | b->toxic = false; | 
|---|
| 369 | fibril_rwlock_initialize(&b->contents_lock); | 
|---|
| 370 | link_initialize(&b->free_link); | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | /** Instantiate a block in memory and get a reference to it. | 
|---|
| 374 | * | 
|---|
| 375 | * @param block                 Pointer to where the function will store the | 
|---|
| 376 | *                              block pointer on success. | 
|---|
| 377 | * @param service_id            Service ID of the block device. | 
|---|
| 378 | * @param ba                    Block address (logical). | 
|---|
| 379 | * @param flags                 If BLOCK_FLAGS_NOREAD is specified, block_get() | 
|---|
| 380 | *                              will not read the contents of the block from the | 
|---|
| 381 | *                              device. | 
|---|
| 382 | * | 
|---|
| 383 | * @return                      EOK on success or an error code. | 
|---|
| 384 | */ | 
|---|
| 385 | errno_t block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags) | 
|---|
| 386 | { | 
|---|
| 387 | devcon_t *devcon; | 
|---|
| 388 | cache_t *cache; | 
|---|
| 389 | block_t *b; | 
|---|
| 390 | link_t *link; | 
|---|
| 391 | aoff64_t p_ba; | 
|---|
| 392 | errno_t rc; | 
|---|
| 393 |  | 
|---|
| 394 | devcon = devcon_search(service_id); | 
|---|
| 395 |  | 
|---|
| 396 | assert(devcon); | 
|---|
| 397 | assert(devcon->cache); | 
|---|
| 398 |  | 
|---|
| 399 | cache = devcon->cache; | 
|---|
| 400 |  | 
|---|
| 401 | /* | 
|---|
| 402 | * Check whether the logical block (or part of it) is beyond | 
|---|
| 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 |  | 
|---|
| 413 | retry: | 
|---|
| 414 | rc = EOK; | 
|---|
| 415 | b = NULL; | 
|---|
| 416 |  | 
|---|
| 417 | fibril_mutex_lock(&cache->lock); | 
|---|
| 418 | ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba); | 
|---|
| 419 | if (hlink) { | 
|---|
| 420 | found: | 
|---|
| 421 | /* | 
|---|
| 422 | * We found the block in the cache. | 
|---|
| 423 | */ | 
|---|
| 424 | b = hash_table_get_inst(hlink, block_t, hash_link); | 
|---|
| 425 | fibril_mutex_lock(&b->lock); | 
|---|
| 426 | if (b->refcnt++ == 0) | 
|---|
| 427 | list_remove(&b->free_link); | 
|---|
| 428 | if (b->toxic) | 
|---|
| 429 | rc = EIO; | 
|---|
| 430 | fibril_mutex_unlock(&b->lock); | 
|---|
| 431 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 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; | 
|---|
| 445 | b->data = malloc(cache->lblock_size); | 
|---|
| 446 | if (!b->data) { | 
|---|
| 447 | free(b); | 
|---|
| 448 | b = NULL; | 
|---|
| 449 | goto recycle; | 
|---|
| 450 | } | 
|---|
| 451 | cache->blocks_cached++; | 
|---|
| 452 | } else { | 
|---|
| 453 | /* | 
|---|
| 454 | * Try to recycle a block from the free list. | 
|---|
| 455 | */ | 
|---|
| 456 | recycle: | 
|---|
| 457 | if (list_empty(&cache->free_list)) { | 
|---|
| 458 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 459 | rc = ENOMEM; | 
|---|
| 460 | goto out; | 
|---|
| 461 | } | 
|---|
| 462 | link = list_first(&cache->free_list); | 
|---|
| 463 | b = list_get_instance(link, block_t, free_link); | 
|---|
| 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); | 
|---|
| 477 | list_append(&b->free_link, &cache->free_list); | 
|---|
| 478 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 479 | rc = write_blocks(devcon, b->pba, | 
|---|
| 480 | cache->blocks_cluster, b->data, b->size); | 
|---|
| 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 | */ | 
|---|
| 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 %" | 
|---|
| 494 | PRIuOFF64 "from device handle %" PRIun "\n" | 
|---|
| 495 | "SEVERE DATA LOSS POSSIBLE\n", | 
|---|
| 496 | b->lba, devcon->service_id); | 
|---|
| 497 | } | 
|---|
| 498 | } else | 
|---|
| 499 | b->write_failures = 0; | 
|---|
| 500 |  | 
|---|
| 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 | } | 
|---|
| 510 | hlink = hash_table_find(&cache->block_hash, &ba); | 
|---|
| 511 | if (hlink) { | 
|---|
| 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 | } | 
|---|
| 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); | 
|---|
| 533 | hash_table_remove_item(&cache->block_hash, &b->hash_link); | 
|---|
| 534 | } | 
|---|
| 535 |  | 
|---|
| 536 | block_initialize(b); | 
|---|
| 537 | b->service_id = service_id; | 
|---|
| 538 | b->size = cache->lblock_size; | 
|---|
| 539 | b->lba = ba; | 
|---|
| 540 | b->pba = ba_ltop(devcon, b->lba); | 
|---|
| 541 | hash_table_insert(&cache->block_hash, &b->hash_link); | 
|---|
| 542 |  | 
|---|
| 543 | /* | 
|---|
| 544 | * Lock the block before releasing the cache lock. Thus we don't | 
|---|
| 545 | * kill concurrent operations on the cache while doing I/O on | 
|---|
| 546 | * the block. | 
|---|
| 547 | */ | 
|---|
| 548 | fibril_mutex_lock(&b->lock); | 
|---|
| 549 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 550 |  | 
|---|
| 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 | */ | 
|---|
| 556 | rc = read_blocks(devcon, b->pba, cache->blocks_cluster, | 
|---|
| 557 | b->data, cache->lblock_size); | 
|---|
| 558 | if (rc != EOK) | 
|---|
| 559 | b->toxic = true; | 
|---|
| 560 | } else | 
|---|
| 561 | rc = EOK; | 
|---|
| 562 |  | 
|---|
| 563 | fibril_mutex_unlock(&b->lock); | 
|---|
| 564 | } | 
|---|
| 565 | out: | 
|---|
| 566 | if ((rc != EOK) && b) { | 
|---|
| 567 | assert(b->toxic); | 
|---|
| 568 | (void) block_put(b); | 
|---|
| 569 | b = NULL; | 
|---|
| 570 | } | 
|---|
| 571 | *block = b; | 
|---|
| 572 | return rc; | 
|---|
| 573 | } | 
|---|
| 574 |  | 
|---|
| 575 | /** Release a reference to a block. | 
|---|
| 576 | * | 
|---|
| 577 | * If the last reference is dropped, the block is put on the free list. | 
|---|
| 578 | * | 
|---|
| 579 | * @param block         Block of which a reference is to be released. | 
|---|
| 580 | * | 
|---|
| 581 | * @return              EOK on success or an error code. | 
|---|
| 582 | */ | 
|---|
| 583 | errno_t block_put(block_t *block) | 
|---|
| 584 | { | 
|---|
| 585 | devcon_t *devcon = devcon_search(block->service_id); | 
|---|
| 586 | cache_t *cache; | 
|---|
| 587 | unsigned blocks_cached; | 
|---|
| 588 | enum cache_mode mode; | 
|---|
| 589 | errno_t rc = EOK; | 
|---|
| 590 |  | 
|---|
| 591 | assert(devcon); | 
|---|
| 592 | assert(devcon->cache); | 
|---|
| 593 | assert(block->refcnt >= 1); | 
|---|
| 594 |  | 
|---|
| 595 | cache = devcon->cache; | 
|---|
| 596 |  | 
|---|
| 597 | retry: | 
|---|
| 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); | 
|---|
| 611 | if (block->toxic) | 
|---|
| 612 | block->dirty = false;   /* will not write back toxic block */ | 
|---|
| 613 | if (block->dirty && (block->refcnt == 1) && | 
|---|
| 614 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) { | 
|---|
| 615 | rc = write_blocks(devcon, block->pba, cache->blocks_cluster, | 
|---|
| 616 | block->data, block->size); | 
|---|
| 617 | if (rc == EOK) | 
|---|
| 618 | block->write_failures = 0; | 
|---|
| 619 | block->dirty = false; | 
|---|
| 620 | } | 
|---|
| 621 | fibril_mutex_unlock(&block->lock); | 
|---|
| 622 |  | 
|---|
| 623 | fibril_mutex_lock(&cache->lock); | 
|---|
| 624 | fibril_mutex_lock(&block->lock); | 
|---|
| 625 | if (!--block->refcnt) { | 
|---|
| 626 | /* | 
|---|
| 627 | * Last reference to the block was dropped. Either free the | 
|---|
| 628 | * block or put it on the free list. In case of an I/O error, | 
|---|
| 629 | * free the block. | 
|---|
| 630 | */ | 
|---|
| 631 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) || | 
|---|
| 632 | (rc != EOK)) { | 
|---|
| 633 | /* | 
|---|
| 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. | 
|---|
| 637 | */ | 
|---|
| 638 | if (block->dirty) { | 
|---|
| 639 | /* | 
|---|
| 640 | * We cannot sync the block while holding the | 
|---|
| 641 | * cache lock. Release everything and retry. | 
|---|
| 642 | */ | 
|---|
| 643 | block->refcnt++; | 
|---|
| 644 |  | 
|---|
| 645 | if (block->write_failures < MAX_WRITE_RETRIES) { | 
|---|
| 646 | block->write_failures++; | 
|---|
| 647 | fibril_mutex_unlock(&block->lock); | 
|---|
| 648 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 649 | goto retry; | 
|---|
| 650 | } else { | 
|---|
| 651 | printf("Too many errors writing block %" | 
|---|
| 652 | PRIuOFF64 "from device handle %" PRIun "\n" | 
|---|
| 653 | "SEVERE DATA LOSS POSSIBLE\n", | 
|---|
| 654 | block->lba, devcon->service_id); | 
|---|
| 655 | } | 
|---|
| 656 | } | 
|---|
| 657 | /* | 
|---|
| 658 | * Take the block out of the cache and free it. | 
|---|
| 659 | */ | 
|---|
| 660 | hash_table_remove_item(&cache->block_hash, &block->hash_link); | 
|---|
| 661 | fibril_mutex_unlock(&block->lock); | 
|---|
| 662 | free(block->data); | 
|---|
| 663 | free(block); | 
|---|
| 664 | cache->blocks_cached--; | 
|---|
| 665 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 666 | return rc; | 
|---|
| 667 | } | 
|---|
| 668 | /* | 
|---|
| 669 | * Put the block on the free list. | 
|---|
| 670 | */ | 
|---|
| 671 | if (cache->mode != CACHE_MODE_WB && block->dirty) { | 
|---|
| 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; | 
|---|
| 680 | } | 
|---|
| 681 | list_append(&block->free_link, &cache->free_list); | 
|---|
| 682 | } | 
|---|
| 683 | fibril_mutex_unlock(&block->lock); | 
|---|
| 684 | fibril_mutex_unlock(&cache->lock); | 
|---|
| 685 |  | 
|---|
| 686 | return rc; | 
|---|
| 687 | } | 
|---|
| 688 |  | 
|---|
| 689 | /** Read sequential data from a block device. | 
|---|
| 690 | * | 
|---|
| 691 | * @param service_id    Service ID of the block device. | 
|---|
| 692 | * @param buf           Buffer for holding one block | 
|---|
| 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 | * | 
|---|
| 702 | * @return              EOK on success or an error code on failure. | 
|---|
| 703 | */ | 
|---|
| 704 | errno_t block_seqread(service_id_t service_id, void *buf, size_t *bufpos, | 
|---|
| 705 | size_t *buflen, aoff64_t *pos, void *dst, size_t size) | 
|---|
| 706 | { | 
|---|
| 707 | size_t offset = 0; | 
|---|
| 708 | size_t left = size; | 
|---|
| 709 | size_t block_size; | 
|---|
| 710 | devcon_t *devcon; | 
|---|
| 711 |  | 
|---|
| 712 | devcon = devcon_search(service_id); | 
|---|
| 713 | assert(devcon); | 
|---|
| 714 | block_size = devcon->pblock_size; | 
|---|
| 715 |  | 
|---|
| 716 | while (left > 0) { | 
|---|
| 717 | size_t rd; | 
|---|
| 718 |  | 
|---|
| 719 | if (*bufpos + left < *buflen) | 
|---|
| 720 | rd = left; | 
|---|
| 721 | else | 
|---|
| 722 | rd = *buflen - *bufpos; | 
|---|
| 723 |  | 
|---|
| 724 | if (rd > 0) { | 
|---|
| 725 | /* | 
|---|
| 726 | * Copy the contents of the communication buffer to the | 
|---|
| 727 | * destination buffer. | 
|---|
| 728 | */ | 
|---|
| 729 | memcpy(dst + offset, buf + *bufpos, rd); | 
|---|
| 730 | offset += rd; | 
|---|
| 731 | *bufpos += rd; | 
|---|
| 732 | *pos += rd; | 
|---|
| 733 | left -= rd; | 
|---|
| 734 | } | 
|---|
| 735 |  | 
|---|
| 736 | if (*bufpos == *buflen) { | 
|---|
| 737 | /* Refill the communication buffer with a new block. */ | 
|---|
| 738 | errno_t rc; | 
|---|
| 739 |  | 
|---|
| 740 | rc = read_blocks(devcon, *pos / block_size, 1, buf, | 
|---|
| 741 | devcon->pblock_size); | 
|---|
| 742 | if (rc != EOK) { | 
|---|
| 743 | return rc; | 
|---|
| 744 | } | 
|---|
| 745 |  | 
|---|
| 746 | *bufpos = 0; | 
|---|
| 747 | *buflen = block_size; | 
|---|
| 748 | } | 
|---|
| 749 | } | 
|---|
| 750 |  | 
|---|
| 751 | return EOK; | 
|---|
| 752 | } | 
|---|
| 753 |  | 
|---|
| 754 | /** Read blocks directly from device (bypass cache). | 
|---|
| 755 | * | 
|---|
| 756 | * @param service_id    Service ID of the block device. | 
|---|
| 757 | * @param ba            Address of first block (physical). | 
|---|
| 758 | * @param cnt           Number of blocks. | 
|---|
| 759 | * @param src           Buffer for storing the data. | 
|---|
| 760 | * | 
|---|
| 761 | * @return              EOK on success or an error code on failure. | 
|---|
| 762 | */ | 
|---|
| 763 | errno_t block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf) | 
|---|
| 764 | { | 
|---|
| 765 | devcon_t *devcon; | 
|---|
| 766 |  | 
|---|
| 767 | devcon = devcon_search(service_id); | 
|---|
| 768 | assert(devcon); | 
|---|
| 769 |  | 
|---|
| 770 | return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt); | 
|---|
| 771 | } | 
|---|
| 772 |  | 
|---|
| 773 | /** Write blocks directly to device (bypass cache). | 
|---|
| 774 | * | 
|---|
| 775 | * @param service_id    Service ID of the block device. | 
|---|
| 776 | * @param ba            Address of first block (physical). | 
|---|
| 777 | * @param cnt           Number of blocks. | 
|---|
| 778 | * @param src           The data to be written. | 
|---|
| 779 | * | 
|---|
| 780 | * @return              EOK on success or an error code on failure. | 
|---|
| 781 | */ | 
|---|
| 782 | errno_t block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt, | 
|---|
| 783 | const void *data) | 
|---|
| 784 | { | 
|---|
| 785 | devcon_t *devcon; | 
|---|
| 786 |  | 
|---|
| 787 | devcon = devcon_search(service_id); | 
|---|
| 788 | assert(devcon); | 
|---|
| 789 |  | 
|---|
| 790 | return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt); | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 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 | * | 
|---|
| 799 | * @return              EOK on success or an error code on failure. | 
|---|
| 800 | */ | 
|---|
| 801 | errno_t block_sync_cache(service_id_t service_id, aoff64_t ba, size_t cnt) | 
|---|
| 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 |  | 
|---|
| 811 | /** Get device block size. | 
|---|
| 812 | * | 
|---|
| 813 | * @param service_id    Service ID of the block device. | 
|---|
| 814 | * @param bsize         Output block size. | 
|---|
| 815 | * | 
|---|
| 816 | * @return              EOK on success or an error code on failure. | 
|---|
| 817 | */ | 
|---|
| 818 | errno_t block_get_bsize(service_id_t service_id, size_t *bsize) | 
|---|
| 819 | { | 
|---|
| 820 | devcon_t *devcon; | 
|---|
| 821 |  | 
|---|
| 822 | devcon = devcon_search(service_id); | 
|---|
| 823 | assert(devcon); | 
|---|
| 824 |  | 
|---|
| 825 | return bd_get_block_size(devcon->bd, bsize); | 
|---|
| 826 | } | 
|---|
| 827 |  | 
|---|
| 828 | /** Get number of blocks on device. | 
|---|
| 829 | * | 
|---|
| 830 | * @param service_id    Service ID of the block device. | 
|---|
| 831 | * @param nblocks       Output number of blocks. | 
|---|
| 832 | * | 
|---|
| 833 | * @return              EOK on success or an error code on failure. | 
|---|
| 834 | */ | 
|---|
| 835 | errno_t block_get_nblocks(service_id_t service_id, aoff64_t *nblocks) | 
|---|
| 836 | { | 
|---|
| 837 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 838 | assert(devcon); | 
|---|
| 839 |  | 
|---|
| 840 | return bd_get_num_blocks(devcon->bd, nblocks); | 
|---|
| 841 | } | 
|---|
| 842 |  | 
|---|
| 843 | /** Read bytes directly from the device (bypass cache) | 
|---|
| 844 | * | 
|---|
| 845 | * @param service_id    Service ID of the block device. | 
|---|
| 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 | 
|---|
| 849 | * | 
|---|
| 850 | * @return              EOK on success or an error code on failure. | 
|---|
| 851 | */ | 
|---|
| 852 | errno_t block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset, | 
|---|
| 853 | size_t bytes, void *data) | 
|---|
| 854 | { | 
|---|
| 855 | errno_t rc; | 
|---|
| 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; | 
|---|
| 863 |  | 
|---|
| 864 | rc = block_get_bsize(service_id, &phys_block_size); | 
|---|
| 865 | if (rc != EOK) { | 
|---|
| 866 | return rc; | 
|---|
| 867 | } | 
|---|
| 868 |  | 
|---|
| 869 | /* calculate data position and required space */ | 
|---|
| 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; | 
|---|
| 875 |  | 
|---|
| 876 | /* read the data into memory */ | 
|---|
| 877 | buffer = malloc(buf_size); | 
|---|
| 878 | if (buffer == NULL) { | 
|---|
| 879 | return ENOMEM; | 
|---|
| 880 | } | 
|---|
| 881 |  | 
|---|
| 882 | rc = block_read_direct(service_id, first_block, blocks, buffer); | 
|---|
| 883 | if (rc != EOK) { | 
|---|
| 884 | free(buffer); | 
|---|
| 885 | return rc; | 
|---|
| 886 | } | 
|---|
| 887 |  | 
|---|
| 888 | /* copy the data from the buffer */ | 
|---|
| 889 | memcpy(data, buffer + offset, bytes); | 
|---|
| 890 | free(buffer); | 
|---|
| 891 |  | 
|---|
| 892 | return EOK; | 
|---|
| 893 | } | 
|---|
| 894 |  | 
|---|
| 895 | /** Get TOC from device. | 
|---|
| 896 | * | 
|---|
| 897 | * @param service_id Service ID of the block device. | 
|---|
| 898 | * @param session    Starting session. | 
|---|
| 899 | * | 
|---|
| 900 | * @return Allocated TOC structure. | 
|---|
| 901 | * @return EOK on success or an error code. | 
|---|
| 902 | * | 
|---|
| 903 | */ | 
|---|
| 904 | errno_t block_read_toc(service_id_t service_id, uint8_t session, void *buf, | 
|---|
| 905 | size_t bufsize) | 
|---|
| 906 | { | 
|---|
| 907 | devcon_t *devcon = devcon_search(service_id); | 
|---|
| 908 |  | 
|---|
| 909 | assert(devcon); | 
|---|
| 910 | return bd_read_toc(devcon->bd, session, buf, bufsize); | 
|---|
| 911 | } | 
|---|
| 912 |  | 
|---|
| 913 | /** Read blocks from block device. | 
|---|
| 914 | * | 
|---|
| 915 | * @param devcon        Device connection. | 
|---|
| 916 | * @param ba            Address of first block. | 
|---|
| 917 | * @param cnt           Number of blocks. | 
|---|
| 918 | * @param src           Buffer for storing the data. | 
|---|
| 919 | * | 
|---|
| 920 | * @return              EOK on success or an error code on failure. | 
|---|
| 921 | */ | 
|---|
| 922 | static errno_t read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf, | 
|---|
| 923 | size_t size) | 
|---|
| 924 | { | 
|---|
| 925 | assert(devcon); | 
|---|
| 926 |  | 
|---|
| 927 | errno_t rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size); | 
|---|
| 928 | if (rc != EOK) { | 
|---|
| 929 | printf("Error %s reading %zu blocks starting at block %" PRIuOFF64 | 
|---|
| 930 | " from device handle %" PRIun "\n", str_error_name(rc), cnt, ba, | 
|---|
| 931 | devcon->service_id); | 
|---|
| 932 | #ifndef NDEBUG | 
|---|
| 933 | stacktrace_print(); | 
|---|
| 934 | #endif | 
|---|
| 935 | } | 
|---|
| 936 |  | 
|---|
| 937 | return rc; | 
|---|
| 938 | } | 
|---|
| 939 |  | 
|---|
| 940 | /** Write block to block device. | 
|---|
| 941 | * | 
|---|
| 942 | * @param devcon        Device connection. | 
|---|
| 943 | * @param ba            Address of first block. | 
|---|
| 944 | * @param cnt           Number of blocks. | 
|---|
| 945 | * @param src           Buffer containing the data to write. | 
|---|
| 946 | * | 
|---|
| 947 | * @return              EOK on success or an error code on failure. | 
|---|
| 948 | */ | 
|---|
| 949 | static errno_t write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data, | 
|---|
| 950 | size_t size) | 
|---|
| 951 | { | 
|---|
| 952 | assert(devcon); | 
|---|
| 953 |  | 
|---|
| 954 | errno_t rc = bd_write_blocks(devcon->bd, ba, cnt, data, size); | 
|---|
| 955 | if (rc != EOK) { | 
|---|
| 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); | 
|---|
| 958 | #ifndef NDEBUG | 
|---|
| 959 | stacktrace_print(); | 
|---|
| 960 | #endif | 
|---|
| 961 | } | 
|---|
| 962 |  | 
|---|
| 963 | return rc; | 
|---|
| 964 | } | 
|---|
| 965 |  | 
|---|
| 966 | /** Convert logical block address to physical block address. */ | 
|---|
| 967 | static 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 |  | 
|---|
| 973 | /** @} | 
|---|
| 974 | */ | 
|---|