[fc840d9] | 1 | /*
|
---|
[7ae01d5] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[ed903174] | 3 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 4 | * Copyright (c) 2008 Martin Decky
|
---|
[e272949] | 5 | * Copyright (c) 2011 Martin Sucha
|
---|
[fc840d9] | 6 | * All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
---|
| 9 | * modification, are permitted provided that the following conditions
|
---|
| 10 | * are met:
|
---|
| 11 | *
|
---|
| 12 | * - Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 16 | * documentation and/or other materials provided with the distribution.
|
---|
| 17 | * - The name of the author may not be used to endorse or promote products
|
---|
| 18 | * derived from this software without specific prior written permission.
|
---|
| 19 | *
|
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
[97c9da8] | 32 | /** @addtogroup libblock
|
---|
[fc840d9] | 33 | * @{
|
---|
[97c9da8] | 34 | */
|
---|
[fc840d9] | 35 | /**
|
---|
| 36 | * @file
|
---|
| 37 | * @brief
|
---|
| 38 | */
|
---|
| 39 |
|
---|
[15f3c3f] | 40 | #include <ipc/loc.h>
|
---|
[7858bc5f] | 41 | #include <ipc/services.h>
|
---|
[fc840d9] | 42 | #include <errno.h>
|
---|
| 43 | #include <async.h>
|
---|
| 44 | #include <as.h>
|
---|
| 45 | #include <assert.h>
|
---|
[4802dd7] | 46 | #include <bd.h>
|
---|
[1e4cada] | 47 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 48 | #include <adt/list.h>
|
---|
| 49 | #include <adt/hash_table.h>
|
---|
[1ee00b7] | 50 | #include <macros.h>
|
---|
[d00ae4c] | 51 | #include <mem.h>
|
---|
[38d150e] | 52 | #include <stdlib.h>
|
---|
[c7bbf029] | 53 | #include <stdio.h>
|
---|
[16fc3c9] | 54 | #include <stacktrace.h>
|
---|
[c1694b6b] | 55 | #include <str_error.h>
|
---|
[7354b5e] | 56 | #include <offset.h>
|
---|
| 57 | #include <inttypes.h>
|
---|
[f73b291] | 58 | #include "block.h"
|
---|
[fc840d9] | 59 |
|
---|
[3d35386] | 60 | #define MAX_WRITE_RETRIES 10
|
---|
| 61 |
|
---|
[916bf1a] | 62 | /** Lock protecting the device connection list */
|
---|
[4e1b57d] | 63 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
---|
[916bf1a] | 64 | /** Device connection list head. */
|
---|
[b72efe8] | 65 | static LIST_INITIALIZE(dcl);
|
---|
[916bf1a] | 66 |
|
---|
[f1ba5d6] | 67 | typedef 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] | 78 | typedef 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] | 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);
|
---|
[79ae36dd] | 92 | static aoff64_t ba_ltop(devcon_t *, aoff64_t);
|
---|
[1fbe064b] | 93 |
|
---|
[15f3c3f] | 94 | static 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] | 109 | static 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 |
|
---|
| 141 | static 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 |
|
---|
[7ae01d5] | 148 | errno_t block_init(service_id_t service_id)
|
---|
[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] | 190 | void 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] | 211 | errno_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] | 237 | void *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 |
|
---|
[5e801dc] | 244 | static size_t cache_key_hash(const void *key)
|
---|
[f1ba5d6] | 245 | {
|
---|
[5e801dc] | 246 | const aoff64_t *lba = key;
|
---|
[062d900] | 247 | return *lba;
|
---|
[f1ba5d6] | 248 | }
|
---|
| 249 |
|
---|
[062d900] | 250 | static 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 |
|
---|
[0db0df2] | 256 | static bool cache_key_equal(const void *key, size_t hash, const ht_link_t *item)
|
---|
[f1ba5d6] | 257 | {
|
---|
[5e801dc] | 258 | const aoff64_t *lba = key;
|
---|
[062d900] | 259 | block_t *b = hash_table_get_inst(item, block_t, hash_link);
|
---|
| 260 | return b->lba == *lba;
|
---|
[f1ba5d6] | 261 | }
|
---|
| 262 |
|
---|
[61eb2ce2] | 263 | static const hash_table_ops_t cache_ops = {
|
---|
[f1ba5d6] | 264 | .hash = cache_hash,
|
---|
[062d900] | 265 | .key_hash = cache_key_hash,
|
---|
| 266 | .key_equal = cache_key_equal,
|
---|
[4e00f87] | 267 | .equal = NULL,
|
---|
| 268 | .remove_callback = NULL
|
---|
[f1ba5d6] | 269 | };
|
---|
| 270 |
|
---|
[b7fd2a0] | 271 | errno_t block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
|
---|
[1fbe064b] | 272 | enum cache_mode mode)
|
---|
[f1ba5d6] | 273 | {
|
---|
[15f3c3f] | 274 | devcon_t *devcon = devcon_search(service_id);
|
---|
[f1ba5d6] | 275 | cache_t *cache;
|
---|
| 276 | if (!devcon)
|
---|
| 277 | return ENOENT;
|
---|
| 278 | if (devcon->cache)
|
---|
| 279 | return EEXIST;
|
---|
| 280 | cache = malloc(sizeof(cache_t));
|
---|
| 281 | if (!cache)
|
---|
| 282 | return ENOMEM;
|
---|
[a35b458] | 283 |
|
---|
[4e1b57d] | 284 | fibril_mutex_initialize(&cache->lock);
|
---|
[b72efe8] | 285 | list_initialize(&cache->free_list);
|
---|
[1ee00b7] | 286 | cache->lblock_size = size;
|
---|
[f1ba5d6] | 287 | cache->block_count = blocks;
|
---|
[d68e4d5] | 288 | cache->blocks_cached = 0;
|
---|
[1fbe064b] | 289 | cache->mode = mode;
|
---|
[f1ba5d6] | 290 |
|
---|
[f092718] | 291 | /* Allow 1:1 or small-to-large block size translation */
|
---|
[37cf3792] | 292 | if (cache->lblock_size % devcon->pblock_size != 0) {
|
---|
| 293 | free(cache);
|
---|
[f092718] | 294 | return ENOTSUP;
|
---|
[37cf3792] | 295 | }
|
---|
[f092718] | 296 |
|
---|
| 297 | cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
|
---|
[1ee00b7] | 298 |
|
---|
[062d900] | 299 | if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
|
---|
[f1ba5d6] | 300 | free(cache);
|
---|
| 301 | return ENOMEM;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | devcon->cache = cache;
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[b7fd2a0] | 308 | errno_t block_cache_fini(service_id_t service_id)
|
---|
[64bc4b6] | 309 | {
|
---|
[15f3c3f] | 310 | devcon_t *devcon = devcon_search(service_id);
|
---|
[64bc4b6] | 311 | cache_t *cache;
|
---|
[b7fd2a0] | 312 | errno_t rc;
|
---|
[64bc4b6] | 313 |
|
---|
| 314 | if (!devcon)
|
---|
| 315 | return ENOENT;
|
---|
| 316 | if (!devcon->cache)
|
---|
| 317 | return EOK;
|
---|
| 318 | cache = devcon->cache;
|
---|
[a35b458] | 319 |
|
---|
[64bc4b6] | 320 | /*
|
---|
| 321 | * We are expecting to find all blocks for this device handle on the
|
---|
| 322 | * free list, i.e. the block reference count should be zero. Do not
|
---|
| 323 | * bother with the cache and block locks because we are single-threaded.
|
---|
| 324 | */
|
---|
[b72efe8] | 325 | while (!list_empty(&cache->free_list)) {
|
---|
| 326 | block_t *b = list_get_instance(list_first(&cache->free_list),
|
---|
[64bc4b6] | 327 | block_t, free_link);
|
---|
| 328 |
|
---|
| 329 | list_remove(&b->free_link);
|
---|
| 330 | if (b->dirty) {
|
---|
[4802dd7] | 331 | rc = write_blocks(devcon, b->pba, cache->blocks_cluster,
|
---|
| 332 | b->data, b->size);
|
---|
[64bc4b6] | 333 | if (rc != EOK)
|
---|
| 334 | return rc;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[062d900] | 337 | hash_table_remove_item(&cache->block_hash, &b->hash_link);
|
---|
[a35b458] | 338 |
|
---|
[64bc4b6] | 339 | free(b->data);
|
---|
| 340 | free(b);
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | hash_table_destroy(&cache->block_hash);
|
---|
| 344 | devcon->cache = NULL;
|
---|
| 345 | free(cache);
|
---|
| 346 |
|
---|
| 347 | return EOK;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[1b20da0] | 350 | #define CACHE_LO_WATERMARK 10
|
---|
| 351 | #define CACHE_HI_WATERMARK 20
|
---|
[e1c88d5] | 352 | static bool cache_can_grow(cache_t *cache)
|
---|
[fc840d9] | 353 | {
|
---|
[d68e4d5] | 354 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
---|
| 355 | return true;
|
---|
[b72efe8] | 356 | if (!list_empty(&cache->free_list))
|
---|
[d68e4d5] | 357 | return false;
|
---|
[e1c88d5] | 358 | return true;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | static void block_initialize(block_t *b)
|
---|
| 362 | {
|
---|
[4e1b57d] | 363 | fibril_mutex_initialize(&b->lock);
|
---|
[e1c88d5] | 364 | b->refcnt = 1;
|
---|
[3d35386] | 365 | b->write_failures = 0;
|
---|
[e1c88d5] | 366 | b->dirty = false;
|
---|
[cd688d9] | 367 | b->toxic = false;
|
---|
[4e1b57d] | 368 | fibril_rwlock_initialize(&b->contents_lock);
|
---|
[e1c88d5] | 369 | link_initialize(&b->free_link);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /** Instantiate a block in memory and get a reference to it.
|
---|
| 373 | *
|
---|
[c91f2d1b] | 374 | * @param block Pointer to where the function will store the
|
---|
| 375 | * block pointer on success.
|
---|
[15f3c3f] | 376 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 377 | * @param ba Block address (logical).
|
---|
[1d8cdb1] | 378 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
| 379 | * will not read the contents of the block from the
|
---|
| 380 | * device.
|
---|
[e1c88d5] | 381 | *
|
---|
[cde999a] | 382 | * @return EOK on success or an error code.
|
---|
[e1c88d5] | 383 | */
|
---|
[b7fd2a0] | 384 | errno_t block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
|
---|
[e1c88d5] | 385 | {
|
---|
| 386 | devcon_t *devcon;
|
---|
| 387 | cache_t *cache;
|
---|
[fc840d9] | 388 | block_t *b;
|
---|
[062d900] | 389 | link_t *link;
|
---|
[3d35386] | 390 | aoff64_t p_ba;
|
---|
[b7fd2a0] | 391 | errno_t rc;
|
---|
[a35b458] | 392 |
|
---|
[15f3c3f] | 393 | devcon = devcon_search(service_id);
|
---|
[fc840d9] | 394 |
|
---|
[e1c88d5] | 395 | assert(devcon);
|
---|
| 396 | assert(devcon->cache);
|
---|
[a35b458] | 397 |
|
---|
[e1c88d5] | 398 | cache = devcon->cache;
|
---|
[02ee6bf5] | 399 |
|
---|
[7c3fb9b] | 400 | /*
|
---|
| 401 | * Check whether the logical block (or part of it) is beyond
|
---|
[3d35386] | 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 |
|
---|
[02ee6bf5] | 411 | retry:
|
---|
[b7b3fda] | 412 | rc = EOK;
|
---|
[4f690cd] | 413 | b = NULL;
|
---|
[b7b3fda] | 414 |
|
---|
[4e1b57d] | 415 | fibril_mutex_lock(&cache->lock);
|
---|
[062d900] | 416 | ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
|
---|
| 417 | if (hlink) {
|
---|
[1433ecda] | 418 | found:
|
---|
[e1c88d5] | 419 | /*
|
---|
| 420 | * We found the block in the cache.
|
---|
| 421 | */
|
---|
[062d900] | 422 | b = hash_table_get_inst(hlink, block_t, hash_link);
|
---|
[4e1b57d] | 423 | fibril_mutex_lock(&b->lock);
|
---|
[e1c88d5] | 424 | if (b->refcnt++ == 0)
|
---|
| 425 | list_remove(&b->free_link);
|
---|
[402a18f] | 426 | if (b->toxic)
|
---|
| 427 | rc = EIO;
|
---|
[4e1b57d] | 428 | fibril_mutex_unlock(&b->lock);
|
---|
| 429 | fibril_mutex_unlock(&cache->lock);
|
---|
[e1c88d5] | 430 | } else {
|
---|
| 431 | /*
|
---|
| 432 | * The block was not found in the cache.
|
---|
| 433 | */
|
---|
| 434 | if (cache_can_grow(cache)) {
|
---|
| 435 | /*
|
---|
| 436 | * We can grow the cache by allocating new blocks.
|
---|
| 437 | * Should the allocation fail, we fail over and try to
|
---|
| 438 | * recycle a block from the cache.
|
---|
| 439 | */
|
---|
| 440 | b = malloc(sizeof(block_t));
|
---|
| 441 | if (!b)
|
---|
| 442 | goto recycle;
|
---|
[1ee00b7] | 443 | b->data = malloc(cache->lblock_size);
|
---|
[e1c88d5] | 444 | if (!b->data) {
|
---|
| 445 | free(b);
|
---|
[0dfaa099] | 446 | b = NULL;
|
---|
[e1c88d5] | 447 | goto recycle;
|
---|
| 448 | }
|
---|
[d68e4d5] | 449 | cache->blocks_cached++;
|
---|
[e1c88d5] | 450 | } else {
|
---|
| 451 | /*
|
---|
| 452 | * Try to recycle a block from the free list.
|
---|
| 453 | */
|
---|
[1433ecda] | 454 | recycle:
|
---|
[b72efe8] | 455 | if (list_empty(&cache->free_list)) {
|
---|
[7a56b1ed] | 456 | fibril_mutex_unlock(&cache->lock);
|
---|
| 457 | rc = ENOMEM;
|
---|
| 458 | goto out;
|
---|
| 459 | }
|
---|
[062d900] | 460 | link = list_first(&cache->free_list);
|
---|
| 461 | b = list_get_instance(link, block_t, free_link);
|
---|
[02ee6bf5] | 462 |
|
---|
| 463 | fibril_mutex_lock(&b->lock);
|
---|
| 464 | if (b->dirty) {
|
---|
| 465 | /*
|
---|
| 466 | * The block needs to be written back to the
|
---|
| 467 | * device before it changes identity. Do this
|
---|
| 468 | * while not holding the cache lock so that
|
---|
| 469 | * concurrency is not impeded. Also move the
|
---|
| 470 | * block to the end of the free list so that we
|
---|
| 471 | * do not slow down other instances of
|
---|
| 472 | * block_get() draining the free list.
|
---|
| 473 | */
|
---|
| 474 | list_remove(&b->free_link);
|
---|
[b72efe8] | 475 | list_append(&b->free_link, &cache->free_list);
|
---|
[02ee6bf5] | 476 | fibril_mutex_unlock(&cache->lock);
|
---|
[f092718] | 477 | rc = write_blocks(devcon, b->pba,
|
---|
[4802dd7] | 478 | cache->blocks_cluster, b->data, b->size);
|
---|
[402a18f] | 479 | if (rc != EOK) {
|
---|
| 480 | /*
|
---|
| 481 | * We did not manage to write the block
|
---|
| 482 | * to the device. Keep it around for
|
---|
| 483 | * another try. Hopefully, we will grab
|
---|
| 484 | * another block next time.
|
---|
| 485 | */
|
---|
[3d35386] | 486 | if (b->write_failures < MAX_WRITE_RETRIES) {
|
---|
| 487 | b->write_failures++;
|
---|
| 488 | fibril_mutex_unlock(&b->lock);
|
---|
| 489 | goto retry;
|
---|
| 490 | } else {
|
---|
| 491 | printf("Too many errors writing block %"
|
---|
[1433ecda] | 492 | PRIuOFF64 "from device handle %" PRIun "\n"
|
---|
[3d35386] | 493 | "SEVERE DATA LOSS POSSIBLE\n",
|
---|
[1433ecda] | 494 | b->lba, devcon->service_id);
|
---|
[3d35386] | 495 | }
|
---|
| 496 | } else
|
---|
| 497 | b->write_failures = 0;
|
---|
| 498 |
|
---|
[02ee6bf5] | 499 | b->dirty = false;
|
---|
| 500 | if (!fibril_mutex_trylock(&cache->lock)) {
|
---|
| 501 | /*
|
---|
| 502 | * Somebody is probably racing with us.
|
---|
| 503 | * Unlock the block and retry.
|
---|
| 504 | */
|
---|
| 505 | fibril_mutex_unlock(&b->lock);
|
---|
| 506 | goto retry;
|
---|
| 507 | }
|
---|
[062d900] | 508 | hlink = hash_table_find(&cache->block_hash, &ba);
|
---|
| 509 | if (hlink) {
|
---|
[5716e9a] | 510 | /*
|
---|
| 511 | * Someone else must have already
|
---|
| 512 | * instantiated the block while we were
|
---|
| 513 | * not holding the cache lock.
|
---|
| 514 | * Leave the recycled block on the
|
---|
| 515 | * freelist and continue as if we
|
---|
| 516 | * found the block of interest during
|
---|
| 517 | * the first try.
|
---|
| 518 | */
|
---|
| 519 | fibril_mutex_unlock(&b->lock);
|
---|
| 520 | goto found;
|
---|
| 521 | }
|
---|
[02ee6bf5] | 522 |
|
---|
| 523 | }
|
---|
| 524 | fibril_mutex_unlock(&b->lock);
|
---|
| 525 |
|
---|
| 526 | /*
|
---|
| 527 | * Unlink the block from the free list and the hash
|
---|
| 528 | * table.
|
---|
| 529 | */
|
---|
| 530 | list_remove(&b->free_link);
|
---|
[062d900] | 531 | hash_table_remove_item(&cache->block_hash, &b->hash_link);
|
---|
[e1c88d5] | 532 | }
|
---|
[fc840d9] | 533 |
|
---|
[e1c88d5] | 534 | block_initialize(b);
|
---|
[15f3c3f] | 535 | b->service_id = service_id;
|
---|
[1ee00b7] | 536 | b->size = cache->lblock_size;
|
---|
[a6ba0c9] | 537 | b->lba = ba;
|
---|
| 538 | b->pba = ba_ltop(devcon, b->lba);
|
---|
[062d900] | 539 | hash_table_insert(&cache->block_hash, &b->hash_link);
|
---|
[a6d97fb9] | 540 |
|
---|
| 541 | /*
|
---|
| 542 | * Lock the block before releasing the cache lock. Thus we don't
|
---|
[5ac8918] | 543 | * kill concurrent operations on the cache while doing I/O on
|
---|
| 544 | * the block.
|
---|
[a6d97fb9] | 545 | */
|
---|
[4e1b57d] | 546 | fibril_mutex_lock(&b->lock);
|
---|
| 547 | fibril_mutex_unlock(&cache->lock);
|
---|
[a6d97fb9] | 548 |
|
---|
[1d8cdb1] | 549 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
---|
| 550 | /*
|
---|
| 551 | * The block contains old or no data. We need to read
|
---|
| 552 | * the new contents from the device.
|
---|
| 553 | */
|
---|
[4802dd7] | 554 | rc = read_blocks(devcon, b->pba, cache->blocks_cluster,
|
---|
| 555 | b->data, cache->lblock_size);
|
---|
[1b20da0] | 556 | if (rc != EOK)
|
---|
[402a18f] | 557 | b->toxic = true;
|
---|
| 558 | } else
|
---|
| 559 | rc = EOK;
|
---|
[fc840d9] | 560 |
|
---|
[4e1b57d] | 561 | fibril_mutex_unlock(&b->lock);
|
---|
[a6d97fb9] | 562 | }
|
---|
[7a56b1ed] | 563 | out:
|
---|
[4f690cd] | 564 | if ((rc != EOK) && b) {
|
---|
| 565 | assert(b->toxic);
|
---|
| 566 | (void) block_put(b);
|
---|
| 567 | b = NULL;
|
---|
| 568 | }
|
---|
[c91f2d1b] | 569 | *block = b;
|
---|
[402a18f] | 570 | return rc;
|
---|
[fc840d9] | 571 | }
|
---|
| 572 |
|
---|
[d5a720cf] | 573 | /** Release a reference to a block.
|
---|
| 574 | *
|
---|
[a6d97fb9] | 575 | * If the last reference is dropped, the block is put on the free list.
|
---|
[d5a720cf] | 576 | *
|
---|
| 577 | * @param block Block of which a reference is to be released.
|
---|
[c91f2d1b] | 578 | *
|
---|
[cde999a] | 579 | * @return EOK on success or an error code.
|
---|
[d5a720cf] | 580 | */
|
---|
[b7fd2a0] | 581 | errno_t block_put(block_t *block)
|
---|
[fc840d9] | 582 | {
|
---|
[15f3c3f] | 583 | devcon_t *devcon = devcon_search(block->service_id);
|
---|
[d5a720cf] | 584 | cache_t *cache;
|
---|
[ddfc39a3] | 585 | unsigned blocks_cached;
|
---|
| 586 | enum cache_mode mode;
|
---|
[b7fd2a0] | 587 | errno_t rc = EOK;
|
---|
[d5a720cf] | 588 |
|
---|
| 589 | assert(devcon);
|
---|
| 590 | assert(devcon->cache);
|
---|
[0f1cf7a] | 591 | assert(block->refcnt >= 1);
|
---|
[d5a720cf] | 592 |
|
---|
| 593 | cache = devcon->cache;
|
---|
[ddfc39a3] | 594 |
|
---|
| 595 | retry:
|
---|
| 596 | fibril_mutex_lock(&cache->lock);
|
---|
| 597 | blocks_cached = cache->blocks_cached;
|
---|
| 598 | mode = cache->mode;
|
---|
| 599 | fibril_mutex_unlock(&cache->lock);
|
---|
| 600 |
|
---|
| 601 | /*
|
---|
| 602 | * Determine whether to sync the block. Syncing the block is best done
|
---|
| 603 | * when not holding the cache lock as it does not impede concurrency.
|
---|
| 604 | * Since the situation may have changed when we unlocked the cache, the
|
---|
| 605 | * blocks_cached and mode variables are mere hints. We will recheck the
|
---|
| 606 | * conditions later when the cache lock is held again.
|
---|
| 607 | */
|
---|
| 608 | fibril_mutex_lock(&block->lock);
|
---|
[402a18f] | 609 | if (block->toxic)
|
---|
| 610 | block->dirty = false; /* will not write back toxic block */
|
---|
[ddfc39a3] | 611 | if (block->dirty && (block->refcnt == 1) &&
|
---|
| 612 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
---|
[4802dd7] | 613 | rc = write_blocks(devcon, block->pba, cache->blocks_cluster,
|
---|
| 614 | block->data, block->size);
|
---|
[3d35386] | 615 | if (rc == EOK)
|
---|
| 616 | block->write_failures = 0;
|
---|
[ddfc39a3] | 617 | block->dirty = false;
|
---|
| 618 | }
|
---|
| 619 | fibril_mutex_unlock(&block->lock);
|
---|
| 620 |
|
---|
[4e1b57d] | 621 | fibril_mutex_lock(&cache->lock);
|
---|
| 622 | fibril_mutex_lock(&block->lock);
|
---|
[d5a720cf] | 623 | if (!--block->refcnt) {
|
---|
| 624 | /*
|
---|
[d68e4d5] | 625 | * Last reference to the block was dropped. Either free the
|
---|
[402a18f] | 626 | * block or put it on the free list. In case of an I/O error,
|
---|
| 627 | * free the block.
|
---|
[d68e4d5] | 628 | */
|
---|
[402a18f] | 629 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
---|
| 630 | (rc != EOK)) {
|
---|
[d68e4d5] | 631 | /*
|
---|
[402a18f] | 632 | * Currently there are too many cached blocks or there
|
---|
| 633 | * was an I/O error when writing the block back to the
|
---|
| 634 | * device.
|
---|
[d68e4d5] | 635 | */
|
---|
| 636 | if (block->dirty) {
|
---|
[ddfc39a3] | 637 | /*
|
---|
| 638 | * We cannot sync the block while holding the
|
---|
| 639 | * cache lock. Release everything and retry.
|
---|
| 640 | */
|
---|
| 641 | block->refcnt++;
|
---|
[3d35386] | 642 |
|
---|
| 643 | if (block->write_failures < MAX_WRITE_RETRIES) {
|
---|
| 644 | block->write_failures++;
|
---|
| 645 | fibril_mutex_unlock(&block->lock);
|
---|
[c1f26834] | 646 | fibril_mutex_unlock(&cache->lock);
|
---|
[3d35386] | 647 | goto retry;
|
---|
| 648 | } else {
|
---|
| 649 | printf("Too many errors writing block %"
|
---|
[1433ecda] | 650 | PRIuOFF64 "from device handle %" PRIun "\n"
|
---|
[3d35386] | 651 | "SEVERE DATA LOSS POSSIBLE\n",
|
---|
[1433ecda] | 652 | block->lba, devcon->service_id);
|
---|
[3d35386] | 653 | }
|
---|
[d68e4d5] | 654 | }
|
---|
| 655 | /*
|
---|
| 656 | * Take the block out of the cache and free it.
|
---|
| 657 | */
|
---|
[062d900] | 658 | hash_table_remove_item(&cache->block_hash, &block->hash_link);
|
---|
[956d4df8] | 659 | fibril_mutex_unlock(&block->lock);
|
---|
[d68e4d5] | 660 | free(block->data);
|
---|
[b9e6205] | 661 | free(block);
|
---|
[d68e4d5] | 662 | cache->blocks_cached--;
|
---|
| 663 | fibril_mutex_unlock(&cache->lock);
|
---|
[402a18f] | 664 | return rc;
|
---|
[d68e4d5] | 665 | }
|
---|
| 666 | /*
|
---|
| 667 | * Put the block on the free list.
|
---|
[d5a720cf] | 668 | */
|
---|
[1fbe064b] | 669 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
---|
[ddfc39a3] | 670 | /*
|
---|
| 671 | * We cannot sync the block while holding the cache
|
---|
| 672 | * lock. Release everything and retry.
|
---|
| 673 | */
|
---|
| 674 | block->refcnt++;
|
---|
| 675 | fibril_mutex_unlock(&block->lock);
|
---|
| 676 | fibril_mutex_unlock(&cache->lock);
|
---|
| 677 | goto retry;
|
---|
[1fbe064b] | 678 | }
|
---|
[b72efe8] | 679 | list_append(&block->free_link, &cache->free_list);
|
---|
[d5a720cf] | 680 | }
|
---|
[4e1b57d] | 681 | fibril_mutex_unlock(&block->lock);
|
---|
| 682 | fibril_mutex_unlock(&cache->lock);
|
---|
[c91f2d1b] | 683 |
|
---|
[402a18f] | 684 | return rc;
|
---|
[d5a720cf] | 685 | }
|
---|
| 686 |
|
---|
[6408be3] | 687 | /** Read sequential data from a block device.
|
---|
[d5a720cf] | 688 | *
|
---|
[15f3c3f] | 689 | * @param service_id Service ID of the block device.
|
---|
[4802dd7] | 690 | * @param buf Buffer for holding one block
|
---|
[d5a720cf] | 691 | * @param bufpos Pointer to the first unread valid offset within the
|
---|
| 692 | * communication buffer.
|
---|
| 693 | * @param buflen Pointer to the number of unread bytes that are ready in
|
---|
| 694 | * the communication buffer.
|
---|
| 695 | * @param pos Device position to be read.
|
---|
| 696 | * @param dst Destination buffer.
|
---|
| 697 | * @param size Size of the destination buffer.
|
---|
| 698 | * @param block_size Block size to be used for the transfer.
|
---|
| 699 | *
|
---|
[cde999a] | 700 | * @return EOK on success or an error code on failure.
|
---|
[d5a720cf] | 701 | */
|
---|
[b7fd2a0] | 702 | errno_t block_seqread(service_id_t service_id, void *buf, size_t *bufpos,
|
---|
[4802dd7] | 703 | size_t *buflen, aoff64_t *pos, void *dst, size_t size)
|
---|
[d5a720cf] | 704 | {
|
---|
[ed903174] | 705 | size_t offset = 0;
|
---|
[d5a720cf] | 706 | size_t left = size;
|
---|
[1ee00b7] | 707 | size_t block_size;
|
---|
| 708 | devcon_t *devcon;
|
---|
| 709 |
|
---|
[15f3c3f] | 710 | devcon = devcon_search(service_id);
|
---|
[d5a720cf] | 711 | assert(devcon);
|
---|
[1ee00b7] | 712 | block_size = devcon->pblock_size;
|
---|
[a35b458] | 713 |
|
---|
[d5a720cf] | 714 | while (left > 0) {
|
---|
| 715 | size_t rd;
|
---|
[a35b458] | 716 |
|
---|
[d5a720cf] | 717 | if (*bufpos + left < *buflen)
|
---|
| 718 | rd = left;
|
---|
| 719 | else
|
---|
| 720 | rd = *buflen - *bufpos;
|
---|
[a35b458] | 721 |
|
---|
[d5a720cf] | 722 | if (rd > 0) {
|
---|
| 723 | /*
|
---|
| 724 | * Copy the contents of the communication buffer to the
|
---|
| 725 | * destination buffer.
|
---|
| 726 | */
|
---|
[4802dd7] | 727 | memcpy(dst + offset, buf + *bufpos, rd);
|
---|
[d5a720cf] | 728 | offset += rd;
|
---|
| 729 | *bufpos += rd;
|
---|
| 730 | *pos += rd;
|
---|
| 731 | left -= rd;
|
---|
| 732 | }
|
---|
[a35b458] | 733 |
|
---|
[ed903174] | 734 | if (*bufpos == *buflen) {
|
---|
[d5a720cf] | 735 | /* Refill the communication buffer with a new block. */
|
---|
[b7fd2a0] | 736 | errno_t rc;
|
---|
[6408be3] | 737 |
|
---|
[4802dd7] | 738 | rc = read_blocks(devcon, *pos / block_size, 1, buf,
|
---|
| 739 | devcon->pblock_size);
|
---|
[d68e4d5] | 740 | if (rc != EOK) {
|
---|
[6408be3] | 741 | return rc;
|
---|
[d68e4d5] | 742 | }
|
---|
[a35b458] | 743 |
|
---|
[d5a720cf] | 744 | *bufpos = 0;
|
---|
| 745 | *buflen = block_size;
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
[a35b458] | 748 |
|
---|
[d5a720cf] | 749 | return EOK;
|
---|
[fc840d9] | 750 | }
|
---|
| 751 |
|
---|
[00b1d20e] | 752 | /** Read blocks directly from device (bypass cache).
|
---|
| 753 | *
|
---|
[15f3c3f] | 754 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 755 | * @param ba Address of first block (physical).
|
---|
[00b1d20e] | 756 | * @param cnt Number of blocks.
|
---|
| 757 | * @param src Buffer for storing the data.
|
---|
| 758 | *
|
---|
[cde999a] | 759 | * @return EOK on success or an error code on failure.
|
---|
[00b1d20e] | 760 | */
|
---|
[b7fd2a0] | 761 | errno_t block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
|
---|
[00b1d20e] | 762 | {
|
---|
| 763 | devcon_t *devcon;
|
---|
| 764 |
|
---|
[15f3c3f] | 765 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 766 | assert(devcon);
|
---|
| 767 |
|
---|
[4802dd7] | 768 | return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt);
|
---|
[00b1d20e] | 769 | }
|
---|
| 770 |
|
---|
| 771 | /** Write blocks directly to device (bypass cache).
|
---|
| 772 | *
|
---|
[15f3c3f] | 773 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 774 | * @param ba Address of first block (physical).
|
---|
[00b1d20e] | 775 | * @param cnt Number of blocks.
|
---|
| 776 | * @param src The data to be written.
|
---|
| 777 | *
|
---|
[cde999a] | 778 | * @return EOK on success or an error code on failure.
|
---|
[00b1d20e] | 779 | */
|
---|
[b7fd2a0] | 780 | errno_t block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
|
---|
[00b1d20e] | 781 | const void *data)
|
---|
| 782 | {
|
---|
| 783 | devcon_t *devcon;
|
---|
| 784 |
|
---|
[15f3c3f] | 785 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 786 | assert(devcon);
|
---|
| 787 |
|
---|
[4802dd7] | 788 | return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt);
|
---|
[00b1d20e] | 789 | }
|
---|
| 790 |
|
---|
[78d50bd] | 791 | /** Synchronize blocks to persistent storage.
|
---|
| 792 | *
|
---|
| 793 | * @param service_id Service ID of the block device.
|
---|
| 794 | * @param ba Address of first block (physical).
|
---|
| 795 | * @param cnt Number of blocks.
|
---|
| 796 | *
|
---|
[cde999a] | 797 | * @return EOK on success or an error code on failure.
|
---|
[78d50bd] | 798 | */
|
---|
[b7fd2a0] | 799 | errno_t block_sync_cache(service_id_t service_id, aoff64_t ba, size_t cnt)
|
---|
[78d50bd] | 800 | {
|
---|
| 801 | devcon_t *devcon;
|
---|
| 802 |
|
---|
| 803 | devcon = devcon_search(service_id);
|
---|
| 804 | assert(devcon);
|
---|
| 805 |
|
---|
| 806 | return bd_sync_cache(devcon->bd, ba, cnt);
|
---|
| 807 | }
|
---|
| 808 |
|
---|
[00b1d20e] | 809 | /** Get device block size.
|
---|
| 810 | *
|
---|
[15f3c3f] | 811 | * @param service_id Service ID of the block device.
|
---|
[00b1d20e] | 812 | * @param bsize Output block size.
|
---|
| 813 | *
|
---|
[cde999a] | 814 | * @return EOK on success or an error code on failure.
|
---|
[00b1d20e] | 815 | */
|
---|
[b7fd2a0] | 816 | errno_t block_get_bsize(service_id_t service_id, size_t *bsize)
|
---|
[00b1d20e] | 817 | {
|
---|
| 818 | devcon_t *devcon;
|
---|
| 819 |
|
---|
[15f3c3f] | 820 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 821 | assert(devcon);
|
---|
[4802dd7] | 822 |
|
---|
| 823 | return bd_get_block_size(devcon->bd, bsize);
|
---|
[00b1d20e] | 824 | }
|
---|
| 825 |
|
---|
[08232ee] | 826 | /** Get number of blocks on device.
|
---|
| 827 | *
|
---|
[15f3c3f] | 828 | * @param service_id Service ID of the block device.
|
---|
[08232ee] | 829 | * @param nblocks Output number of blocks.
|
---|
| 830 | *
|
---|
[cde999a] | 831 | * @return EOK on success or an error code on failure.
|
---|
[08232ee] | 832 | */
|
---|
[b7fd2a0] | 833 | errno_t block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
|
---|
[08232ee] | 834 | {
|
---|
[15f3c3f] | 835 | devcon_t *devcon = devcon_search(service_id);
|
---|
[08232ee] | 836 | assert(devcon);
|
---|
[3d35386] | 837 |
|
---|
[4802dd7] | 838 | return bd_get_num_blocks(devcon->bd, nblocks);
|
---|
[08232ee] | 839 | }
|
---|
| 840 |
|
---|
[e272949] | 841 | /** Read bytes directly from the device (bypass cache)
|
---|
[1b20da0] | 842 | *
|
---|
[15f3c3f] | 843 | * @param service_id Service ID of the block device.
|
---|
[e272949] | 844 | * @param abs_offset Absolute offset in bytes where to start reading
|
---|
| 845 | * @param bytes Number of bytes to read
|
---|
| 846 | * @param data Buffer that receives the data
|
---|
[1b20da0] | 847 | *
|
---|
[cde999a] | 848 | * @return EOK on success or an error code on failure.
|
---|
[e272949] | 849 | */
|
---|
[b7fd2a0] | 850 | errno_t block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
|
---|
[e272949] | 851 | size_t bytes, void *data)
|
---|
| 852 | {
|
---|
[b7fd2a0] | 853 | errno_t rc;
|
---|
[e272949] | 854 | size_t phys_block_size;
|
---|
| 855 | size_t buf_size;
|
---|
| 856 | void *buffer;
|
---|
| 857 | aoff64_t first_block;
|
---|
| 858 | aoff64_t last_block;
|
---|
| 859 | size_t blocks;
|
---|
| 860 | size_t offset;
|
---|
[a35b458] | 861 |
|
---|
[15f3c3f] | 862 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
[e272949] | 863 | if (rc != EOK) {
|
---|
| 864 | return rc;
|
---|
| 865 | }
|
---|
[a35b458] | 866 |
|
---|
[c4aa9cf] | 867 | /* calculate data position and required space */
|
---|
[e272949] | 868 | first_block = abs_offset / phys_block_size;
|
---|
| 869 | offset = abs_offset % phys_block_size;
|
---|
| 870 | last_block = (abs_offset + bytes - 1) / phys_block_size;
|
---|
| 871 | blocks = last_block - first_block + 1;
|
---|
| 872 | buf_size = blocks * phys_block_size;
|
---|
[a35b458] | 873 |
|
---|
[c4aa9cf] | 874 | /* read the data into memory */
|
---|
[e272949] | 875 | buffer = malloc(buf_size);
|
---|
| 876 | if (buffer == NULL) {
|
---|
| 877 | return ENOMEM;
|
---|
| 878 | }
|
---|
[a35b458] | 879 |
|
---|
[15f3c3f] | 880 | rc = block_read_direct(service_id, first_block, blocks, buffer);
|
---|
[e272949] | 881 | if (rc != EOK) {
|
---|
| 882 | free(buffer);
|
---|
| 883 | return rc;
|
---|
| 884 | }
|
---|
[a35b458] | 885 |
|
---|
[c4aa9cf] | 886 | /* copy the data from the buffer */
|
---|
[e272949] | 887 | memcpy(data, buffer + offset, bytes);
|
---|
| 888 | free(buffer);
|
---|
[a35b458] | 889 |
|
---|
[e272949] | 890 | return EOK;
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[4046b2f4] | 893 | /** Get TOC from device.
|
---|
| 894 | *
|
---|
| 895 | * @param service_id Service ID of the block device.
|
---|
| 896 | * @param session Starting session.
|
---|
| 897 | *
|
---|
[08cba4b] | 898 | * @return Allocated TOC structure.
|
---|
[cde999a] | 899 | * @return EOK on success or an error code.
|
---|
[4046b2f4] | 900 | *
|
---|
| 901 | */
|
---|
[b7fd2a0] | 902 | errno_t block_read_toc(service_id_t service_id, uint8_t session, void *buf,
|
---|
[3abf70c7] | 903 | size_t bufsize)
|
---|
[4046b2f4] | 904 | {
|
---|
| 905 | devcon_t *devcon = devcon_search(service_id);
|
---|
[a35b458] | 906 |
|
---|
[4802dd7] | 907 | assert(devcon);
|
---|
[3abf70c7] | 908 | return bd_read_toc(devcon->bd, session, buf, bufsize);
|
---|
[4046b2f4] | 909 | }
|
---|
| 910 |
|
---|
[1ee00b7] | 911 | /** Read blocks from block device.
|
---|
[6408be3] | 912 | *
|
---|
| 913 | * @param devcon Device connection.
|
---|
[1ee00b7] | 914 | * @param ba Address of first block.
|
---|
| 915 | * @param cnt Number of blocks.
|
---|
[6408be3] | 916 | * @param src Buffer for storing the data.
|
---|
| 917 | *
|
---|
[cde999a] | 918 | * @return EOK on success or an error code on failure.
|
---|
[6408be3] | 919 | */
|
---|
[b7fd2a0] | 920 | static errno_t read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf,
|
---|
[4802dd7] | 921 | size_t size)
|
---|
[6408be3] | 922 | {
|
---|
| 923 | assert(devcon);
|
---|
[a35b458] | 924 |
|
---|
[b7fd2a0] | 925 | errno_t rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size);
|
---|
[16fc3c9] | 926 | if (rc != EOK) {
|
---|
[c1694b6b] | 927 | printf("Error %s reading %zu blocks starting at block %" PRIuOFF64
|
---|
| 928 | " from device handle %" PRIun "\n", str_error_name(rc), cnt, ba,
|
---|
[15f3c3f] | 929 | devcon->service_id);
|
---|
[16fc3c9] | 930 | #ifndef NDEBUG
|
---|
| 931 | stacktrace_print();
|
---|
| 932 | #endif
|
---|
| 933 | }
|
---|
[a35b458] | 934 |
|
---|
[1ee00b7] | 935 | return rc;
|
---|
[6408be3] | 936 | }
|
---|
| 937 |
|
---|
[1fbe064b] | 938 | /** Write block to block device.
|
---|
| 939 | *
|
---|
| 940 | * @param devcon Device connection.
|
---|
[1ee00b7] | 941 | * @param ba Address of first block.
|
---|
| 942 | * @param cnt Number of blocks.
|
---|
[1fbe064b] | 943 | * @param src Buffer containing the data to write.
|
---|
| 944 | *
|
---|
[cde999a] | 945 | * @return EOK on success or an error code on failure.
|
---|
[1fbe064b] | 946 | */
|
---|
[b7fd2a0] | 947 | static errno_t write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data,
|
---|
[4802dd7] | 948 | size_t size)
|
---|
[1fbe064b] | 949 | {
|
---|
| 950 | assert(devcon);
|
---|
[a35b458] | 951 |
|
---|
[b7fd2a0] | 952 | errno_t rc = bd_write_blocks(devcon->bd, ba, cnt, data, size);
|
---|
[16fc3c9] | 953 | if (rc != EOK) {
|
---|
[c1694b6b] | 954 | printf("Error %s writing %zu blocks starting at block %" PRIuOFF64
|
---|
| 955 | " to device handle %" PRIun "\n", str_error_name(rc), cnt, ba, devcon->service_id);
|
---|
[16fc3c9] | 956 | #ifndef NDEBUG
|
---|
| 957 | stacktrace_print();
|
---|
| 958 | #endif
|
---|
| 959 | }
|
---|
[a35b458] | 960 |
|
---|
[1ee00b7] | 961 | return rc;
|
---|
| 962 | }
|
---|
[1fbe064b] | 963 |
|
---|
[f092718] | 964 | /** Convert logical block address to physical block address. */
|
---|
| 965 | static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
|
---|
| 966 | {
|
---|
| 967 | assert(devcon->cache != NULL);
|
---|
| 968 | return lba * devcon->cache->blocks_cluster;
|
---|
| 969 | }
|
---|
| 970 |
|
---|
[fc840d9] | 971 | /** @}
|
---|
| 972 | */
|
---|