| 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 "libblock.h"
|
|---|
| 40 | #include "../../srv/vfs/vfs.h"
|
|---|
| 41 | #include <ipc/loc.h>
|
|---|
| 42 | #include <ipc/bd.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <sys/mman.h>
|
|---|
| 46 | #include <async.h>
|
|---|
| 47 | #include <as.h>
|
|---|
| 48 | #include <assert.h>
|
|---|
| 49 | #include <fibril_synch.h>
|
|---|
| 50 | #include <adt/list.h>
|
|---|
| 51 | #include <adt/hash_table.h>
|
|---|
| 52 | #include <macros.h>
|
|---|
| 53 | #include <mem.h>
|
|---|
| 54 | #include <malloc.h>
|
|---|
| 55 | #include <stdio.h>
|
|---|
| 56 | #include <sys/typefmt.h>
|
|---|
| 57 | #include <stacktrace.h>
|
|---|
| 58 |
|
|---|
| 59 | /** Lock protecting the device connection list */
|
|---|
| 60 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
|---|
| 61 | /** Device connection list head. */
|
|---|
| 62 | static LIST_INITIALIZE(dcl);
|
|---|
| 63 |
|
|---|
| 64 | #define CACHE_BUCKETS_LOG2 10
|
|---|
| 65 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
|---|
| 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 | fibril_mutex_t comm_area_lock;
|
|---|
| 83 | void *comm_area;
|
|---|
| 84 | size_t comm_size;
|
|---|
| 85 | void *bb_buf;
|
|---|
| 86 | aoff64_t bb_addr;
|
|---|
| 87 | size_t pblock_size; /**< Physical block size. */
|
|---|
| 88 | cache_t *cache;
|
|---|
| 89 | } devcon_t;
|
|---|
| 90 |
|
|---|
| 91 | static int read_blocks(devcon_t *, aoff64_t, size_t);
|
|---|
| 92 | static int write_blocks(devcon_t *, aoff64_t, size_t);
|
|---|
| 93 | static int get_block_size(async_sess_t *, size_t *);
|
|---|
| 94 | static int get_num_blocks(async_sess_t *, aoff64_t *);
|
|---|
| 95 | static aoff64_t ba_ltop(devcon_t *, aoff64_t);
|
|---|
| 96 |
|
|---|
| 97 | static devcon_t *devcon_search(service_id_t service_id)
|
|---|
| 98 | {
|
|---|
| 99 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 100 |
|
|---|
| 101 | list_foreach(dcl, cur) {
|
|---|
| 102 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
|---|
| 103 | if (devcon->service_id == service_id) {
|
|---|
| 104 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 105 | return devcon;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 110 | return NULL;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | static int devcon_add(service_id_t service_id, async_sess_t *sess,
|
|---|
| 114 | size_t bsize, void *comm_area, size_t comm_size)
|
|---|
| 115 | {
|
|---|
| 116 | devcon_t *devcon;
|
|---|
| 117 |
|
|---|
| 118 | if (comm_size < bsize)
|
|---|
| 119 | return EINVAL;
|
|---|
| 120 |
|
|---|
| 121 | devcon = malloc(sizeof(devcon_t));
|
|---|
| 122 | if (!devcon)
|
|---|
| 123 | return ENOMEM;
|
|---|
| 124 |
|
|---|
| 125 | link_initialize(&devcon->link);
|
|---|
| 126 | devcon->service_id = service_id;
|
|---|
| 127 | devcon->sess = sess;
|
|---|
| 128 | fibril_mutex_initialize(&devcon->comm_area_lock);
|
|---|
| 129 | devcon->comm_area = comm_area;
|
|---|
| 130 | devcon->comm_size = comm_size;
|
|---|
| 131 | devcon->bb_buf = NULL;
|
|---|
| 132 | devcon->bb_addr = 0;
|
|---|
| 133 | devcon->pblock_size = bsize;
|
|---|
| 134 | devcon->cache = NULL;
|
|---|
| 135 |
|
|---|
| 136 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 137 | list_foreach(dcl, cur) {
|
|---|
| 138 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
|---|
| 139 | if (d->service_id == service_id) {
|
|---|
| 140 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 141 | free(devcon);
|
|---|
| 142 | return EEXIST;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | list_append(&devcon->link, &dcl);
|
|---|
| 146 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 147 | return EOK;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | static void devcon_remove(devcon_t *devcon)
|
|---|
| 151 | {
|
|---|
| 152 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 153 | list_remove(&devcon->link);
|
|---|
| 154 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | int block_init(exch_mgmt_t mgmt, service_id_t service_id,
|
|---|
| 158 | size_t comm_size)
|
|---|
| 159 | {
|
|---|
| 160 | void *comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
|
|---|
| 161 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
|---|
| 162 | if (!comm_area)
|
|---|
| 163 | return ENOMEM;
|
|---|
| 164 |
|
|---|
| 165 | async_sess_t *sess = loc_service_connect(mgmt, service_id,
|
|---|
| 166 | IPC_FLAG_BLOCKING);
|
|---|
| 167 | if (!sess) {
|
|---|
| 168 | munmap(comm_area, comm_size);
|
|---|
| 169 | return ENOENT;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 173 | int rc = async_share_out_start(exch, comm_area,
|
|---|
| 174 | AS_AREA_READ | AS_AREA_WRITE);
|
|---|
| 175 | async_exchange_end(exch);
|
|---|
| 176 |
|
|---|
| 177 | if (rc != EOK) {
|
|---|
| 178 | munmap(comm_area, comm_size);
|
|---|
| 179 | async_hangup(sess);
|
|---|
| 180 | return rc;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | size_t bsize;
|
|---|
| 184 | rc = get_block_size(sess, &bsize);
|
|---|
| 185 |
|
|---|
| 186 | if (rc != EOK) {
|
|---|
| 187 | munmap(comm_area, comm_size);
|
|---|
| 188 | async_hangup(sess);
|
|---|
| 189 | return rc;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | rc = devcon_add(service_id, sess, bsize, comm_area, comm_size);
|
|---|
| 193 | if (rc != EOK) {
|
|---|
| 194 | munmap(comm_area, comm_size);
|
|---|
| 195 | async_hangup(sess);
|
|---|
| 196 | return rc;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | return EOK;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | void block_fini(service_id_t service_id)
|
|---|
| 203 | {
|
|---|
| 204 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 205 | assert(devcon);
|
|---|
| 206 |
|
|---|
| 207 | if (devcon->cache)
|
|---|
| 208 | (void) block_cache_fini(service_id);
|
|---|
| 209 |
|
|---|
| 210 | devcon_remove(devcon);
|
|---|
| 211 |
|
|---|
| 212 | if (devcon->bb_buf)
|
|---|
| 213 | free(devcon->bb_buf);
|
|---|
| 214 |
|
|---|
| 215 | munmap(devcon->comm_area, devcon->comm_size);
|
|---|
| 216 | async_hangup(devcon->sess);
|
|---|
| 217 |
|
|---|
| 218 | free(devcon);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | int block_bb_read(service_id_t service_id, aoff64_t ba)
|
|---|
| 222 | {
|
|---|
| 223 | void *bb_buf;
|
|---|
| 224 | int rc;
|
|---|
| 225 |
|
|---|
| 226 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 227 | if (!devcon)
|
|---|
| 228 | return ENOENT;
|
|---|
| 229 | if (devcon->bb_buf)
|
|---|
| 230 | return EEXIST;
|
|---|
| 231 | bb_buf = malloc(devcon->pblock_size);
|
|---|
| 232 | if (!bb_buf)
|
|---|
| 233 | return ENOMEM;
|
|---|
| 234 |
|
|---|
| 235 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 236 | rc = read_blocks(devcon, 0, 1);
|
|---|
| 237 | if (rc != EOK) {
|
|---|
| 238 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 239 | free(bb_buf);
|
|---|
| 240 | return rc;
|
|---|
| 241 | }
|
|---|
| 242 | memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
|
|---|
| 243 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 244 |
|
|---|
| 245 | devcon->bb_buf = bb_buf;
|
|---|
| 246 | devcon->bb_addr = ba;
|
|---|
| 247 |
|
|---|
| 248 | return EOK;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | void *block_bb_get(service_id_t service_id)
|
|---|
| 252 | {
|
|---|
| 253 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 254 | assert(devcon);
|
|---|
| 255 | return devcon->bb_buf;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | static hash_index_t cache_hash(unsigned long *key)
|
|---|
| 259 | {
|
|---|
| 260 | return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
|---|
| 264 | {
|
|---|
| 265 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
|---|
| 266 | return b->lba == MERGE_LOUP32(key[0], key[1]);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | static void cache_remove_callback(link_t *item)
|
|---|
| 270 | {
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | static hash_table_operations_t cache_ops = {
|
|---|
| 274 | .hash = cache_hash,
|
|---|
| 275 | .compare = cache_compare,
|
|---|
| 276 | .remove_callback = cache_remove_callback
|
|---|
| 277 | };
|
|---|
| 278 |
|
|---|
| 279 | int block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
|
|---|
| 280 | enum cache_mode mode)
|
|---|
| 281 | {
|
|---|
| 282 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 283 | cache_t *cache;
|
|---|
| 284 | if (!devcon)
|
|---|
| 285 | return ENOENT;
|
|---|
| 286 | if (devcon->cache)
|
|---|
| 287 | return EEXIST;
|
|---|
| 288 | cache = malloc(sizeof(cache_t));
|
|---|
| 289 | if (!cache)
|
|---|
| 290 | return ENOMEM;
|
|---|
| 291 |
|
|---|
| 292 | fibril_mutex_initialize(&cache->lock);
|
|---|
| 293 | list_initialize(&cache->free_list);
|
|---|
| 294 | cache->lblock_size = size;
|
|---|
| 295 | cache->block_count = blocks;
|
|---|
| 296 | cache->blocks_cached = 0;
|
|---|
| 297 | cache->mode = mode;
|
|---|
| 298 |
|
|---|
| 299 | /* Allow 1:1 or small-to-large block size translation */
|
|---|
| 300 | if (cache->lblock_size % devcon->pblock_size != 0) {
|
|---|
| 301 | free(cache);
|
|---|
| 302 | return ENOTSUP;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
|
|---|
| 306 |
|
|---|
| 307 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
|
|---|
| 308 | &cache_ops)) {
|
|---|
| 309 | free(cache);
|
|---|
| 310 | return ENOMEM;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | devcon->cache = cache;
|
|---|
| 314 | return EOK;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | int block_cache_fini(service_id_t service_id)
|
|---|
| 318 | {
|
|---|
| 319 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 320 | cache_t *cache;
|
|---|
| 321 | int rc;
|
|---|
| 322 |
|
|---|
| 323 | if (!devcon)
|
|---|
| 324 | return ENOENT;
|
|---|
| 325 | if (!devcon->cache)
|
|---|
| 326 | return EOK;
|
|---|
| 327 | cache = devcon->cache;
|
|---|
| 328 |
|
|---|
| 329 | /*
|
|---|
| 330 | * We are expecting to find all blocks for this device handle on the
|
|---|
| 331 | * free list, i.e. the block reference count should be zero. Do not
|
|---|
| 332 | * bother with the cache and block locks because we are single-threaded.
|
|---|
| 333 | */
|
|---|
| 334 | while (!list_empty(&cache->free_list)) {
|
|---|
| 335 | block_t *b = list_get_instance(list_first(&cache->free_list),
|
|---|
| 336 | block_t, free_link);
|
|---|
| 337 |
|
|---|
| 338 | list_remove(&b->free_link);
|
|---|
| 339 | if (b->dirty) {
|
|---|
| 340 | memcpy(devcon->comm_area, b->data, b->size);
|
|---|
| 341 | rc = write_blocks(devcon, b->pba, cache->blocks_cluster);
|
|---|
| 342 | if (rc != EOK)
|
|---|
| 343 | return rc;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | unsigned long key[2] = {
|
|---|
| 347 | LOWER32(b->lba),
|
|---|
| 348 | UPPER32(b->lba)
|
|---|
| 349 | };
|
|---|
| 350 | hash_table_remove(&cache->block_hash, key, 2);
|
|---|
| 351 |
|
|---|
| 352 | free(b->data);
|
|---|
| 353 | free(b);
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | hash_table_destroy(&cache->block_hash);
|
|---|
| 357 | devcon->cache = NULL;
|
|---|
| 358 | free(cache);
|
|---|
| 359 |
|
|---|
| 360 | return EOK;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | #define CACHE_LO_WATERMARK 10
|
|---|
| 364 | #define CACHE_HI_WATERMARK 20
|
|---|
| 365 | static bool cache_can_grow(cache_t *cache)
|
|---|
| 366 | {
|
|---|
| 367 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
|---|
| 368 | return true;
|
|---|
| 369 | if (!list_empty(&cache->free_list))
|
|---|
| 370 | return false;
|
|---|
| 371 | return true;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | static void block_initialize(block_t *b)
|
|---|
| 375 | {
|
|---|
| 376 | fibril_mutex_initialize(&b->lock);
|
|---|
| 377 | b->refcnt = 1;
|
|---|
| 378 | b->dirty = false;
|
|---|
| 379 | b->toxic = false;
|
|---|
| 380 | fibril_rwlock_initialize(&b->contents_lock);
|
|---|
| 381 | link_initialize(&b->free_link);
|
|---|
| 382 | link_initialize(&b->hash_link);
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | /** Instantiate a block in memory and get a reference to it.
|
|---|
| 386 | *
|
|---|
| 387 | * @param block Pointer to where the function will store the
|
|---|
| 388 | * block pointer on success.
|
|---|
| 389 | * @param service_id Service ID of the block device.
|
|---|
| 390 | * @param ba Block address (logical).
|
|---|
| 391 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
|---|
| 392 | * will not read the contents of the block from the
|
|---|
| 393 | * device.
|
|---|
| 394 | *
|
|---|
| 395 | * @return EOK on success or a negative error code.
|
|---|
| 396 | */
|
|---|
| 397 | int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
|
|---|
| 398 | {
|
|---|
| 399 | devcon_t *devcon;
|
|---|
| 400 | cache_t *cache;
|
|---|
| 401 | block_t *b;
|
|---|
| 402 | link_t *l;
|
|---|
| 403 | unsigned long key[2] = {
|
|---|
| 404 | LOWER32(ba),
|
|---|
| 405 | UPPER32(ba)
|
|---|
| 406 | };
|
|---|
| 407 |
|
|---|
| 408 | int rc;
|
|---|
| 409 |
|
|---|
| 410 | devcon = devcon_search(service_id);
|
|---|
| 411 |
|
|---|
| 412 | assert(devcon);
|
|---|
| 413 | assert(devcon->cache);
|
|---|
| 414 |
|
|---|
| 415 | cache = devcon->cache;
|
|---|
| 416 |
|
|---|
| 417 | retry:
|
|---|
| 418 | rc = EOK;
|
|---|
| 419 | b = NULL;
|
|---|
| 420 |
|
|---|
| 421 | fibril_mutex_lock(&cache->lock);
|
|---|
| 422 | l = hash_table_find(&cache->block_hash, key);
|
|---|
| 423 | if (l) {
|
|---|
| 424 | found:
|
|---|
| 425 | /*
|
|---|
| 426 | * We found the block in the cache.
|
|---|
| 427 | */
|
|---|
| 428 | b = hash_table_get_instance(l, block_t, hash_link);
|
|---|
| 429 | fibril_mutex_lock(&b->lock);
|
|---|
| 430 | if (b->refcnt++ == 0)
|
|---|
| 431 | list_remove(&b->free_link);
|
|---|
| 432 | if (b->toxic)
|
|---|
| 433 | rc = EIO;
|
|---|
| 434 | fibril_mutex_unlock(&b->lock);
|
|---|
| 435 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 436 | } else {
|
|---|
| 437 | /*
|
|---|
| 438 | * The block was not found in the cache.
|
|---|
| 439 | */
|
|---|
| 440 | if (cache_can_grow(cache)) {
|
|---|
| 441 | /*
|
|---|
| 442 | * We can grow the cache by allocating new blocks.
|
|---|
| 443 | * Should the allocation fail, we fail over and try to
|
|---|
| 444 | * recycle a block from the cache.
|
|---|
| 445 | */
|
|---|
| 446 | b = malloc(sizeof(block_t));
|
|---|
| 447 | if (!b)
|
|---|
| 448 | goto recycle;
|
|---|
| 449 | b->data = malloc(cache->lblock_size);
|
|---|
| 450 | if (!b->data) {
|
|---|
| 451 | free(b);
|
|---|
| 452 | b = NULL;
|
|---|
| 453 | goto recycle;
|
|---|
| 454 | }
|
|---|
| 455 | cache->blocks_cached++;
|
|---|
| 456 | } else {
|
|---|
| 457 | /*
|
|---|
| 458 | * Try to recycle a block from the free list.
|
|---|
| 459 | */
|
|---|
| 460 | recycle:
|
|---|
| 461 | if (list_empty(&cache->free_list)) {
|
|---|
| 462 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 463 | rc = ENOMEM;
|
|---|
| 464 | goto out;
|
|---|
| 465 | }
|
|---|
| 466 | l = list_first(&cache->free_list);
|
|---|
| 467 | b = list_get_instance(l, block_t, free_link);
|
|---|
| 468 |
|
|---|
| 469 | fibril_mutex_lock(&b->lock);
|
|---|
| 470 | if (b->dirty) {
|
|---|
| 471 | /*
|
|---|
| 472 | * The block needs to be written back to the
|
|---|
| 473 | * device before it changes identity. Do this
|
|---|
| 474 | * while not holding the cache lock so that
|
|---|
| 475 | * concurrency is not impeded. Also move the
|
|---|
| 476 | * block to the end of the free list so that we
|
|---|
| 477 | * do not slow down other instances of
|
|---|
| 478 | * block_get() draining the free list.
|
|---|
| 479 | */
|
|---|
| 480 | list_remove(&b->free_link);
|
|---|
| 481 | list_append(&b->free_link, &cache->free_list);
|
|---|
| 482 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 483 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 484 | memcpy(devcon->comm_area, b->data, b->size);
|
|---|
| 485 | rc = write_blocks(devcon, b->pba,
|
|---|
| 486 | cache->blocks_cluster);
|
|---|
| 487 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 488 | if (rc != EOK) {
|
|---|
| 489 | /*
|
|---|
| 490 | * We did not manage to write the block
|
|---|
| 491 | * to the device. Keep it around for
|
|---|
| 492 | * another try. Hopefully, we will grab
|
|---|
| 493 | * another block next time.
|
|---|
| 494 | */
|
|---|
| 495 | fibril_mutex_unlock(&b->lock);
|
|---|
| 496 | goto retry;
|
|---|
| 497 | }
|
|---|
| 498 | b->dirty = false;
|
|---|
| 499 | if (!fibril_mutex_trylock(&cache->lock)) {
|
|---|
| 500 | /*
|
|---|
| 501 | * Somebody is probably racing with us.
|
|---|
| 502 | * Unlock the block and retry.
|
|---|
| 503 | */
|
|---|
| 504 | fibril_mutex_unlock(&b->lock);
|
|---|
| 505 | goto retry;
|
|---|
| 506 | }
|
|---|
| 507 | l = hash_table_find(&cache->block_hash, key);
|
|---|
| 508 | if (l) {
|
|---|
| 509 | /*
|
|---|
| 510 | * Someone else must have already
|
|---|
| 511 | * instantiated the block while we were
|
|---|
| 512 | * not holding the cache lock.
|
|---|
| 513 | * Leave the recycled block on the
|
|---|
| 514 | * freelist and continue as if we
|
|---|
| 515 | * found the block of interest during
|
|---|
| 516 | * the first try.
|
|---|
| 517 | */
|
|---|
| 518 | fibril_mutex_unlock(&b->lock);
|
|---|
| 519 | goto found;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | }
|
|---|
| 523 | fibril_mutex_unlock(&b->lock);
|
|---|
| 524 |
|
|---|
| 525 | /*
|
|---|
| 526 | * Unlink the block from the free list and the hash
|
|---|
| 527 | * table.
|
|---|
| 528 | */
|
|---|
| 529 | list_remove(&b->free_link);
|
|---|
| 530 | unsigned long temp_key[2] = {
|
|---|
| 531 | LOWER32(b->lba),
|
|---|
| 532 | UPPER32(b->lba)
|
|---|
| 533 | };
|
|---|
| 534 | hash_table_remove(&cache->block_hash, temp_key, 2);
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | block_initialize(b);
|
|---|
| 538 | b->service_id = service_id;
|
|---|
| 539 | b->size = cache->lblock_size;
|
|---|
| 540 | b->lba = ba;
|
|---|
| 541 | b->pba = ba_ltop(devcon, b->lba);
|
|---|
| 542 | hash_table_insert(&cache->block_hash, key, &b->hash_link);
|
|---|
| 543 |
|
|---|
| 544 | /*
|
|---|
| 545 | * Lock the block before releasing the cache lock. Thus we don't
|
|---|
| 546 | * kill concurrent operations on the cache while doing I/O on
|
|---|
| 547 | * the block.
|
|---|
| 548 | */
|
|---|
| 549 | fibril_mutex_lock(&b->lock);
|
|---|
| 550 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 551 |
|
|---|
| 552 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
|---|
| 553 | /*
|
|---|
| 554 | * The block contains old or no data. We need to read
|
|---|
| 555 | * the new contents from the device.
|
|---|
| 556 | */
|
|---|
| 557 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 558 | rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
|
|---|
| 559 | memcpy(b->data, devcon->comm_area, cache->lblock_size);
|
|---|
| 560 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 561 | if (rc != EOK)
|
|---|
| 562 | b->toxic = true;
|
|---|
| 563 | } else
|
|---|
| 564 | rc = EOK;
|
|---|
| 565 |
|
|---|
| 566 | fibril_mutex_unlock(&b->lock);
|
|---|
| 567 | }
|
|---|
| 568 | out:
|
|---|
| 569 | if ((rc != EOK) && b) {
|
|---|
| 570 | assert(b->toxic);
|
|---|
| 571 | (void) block_put(b);
|
|---|
| 572 | b = NULL;
|
|---|
| 573 | }
|
|---|
| 574 | *block = b;
|
|---|
| 575 | return rc;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /** Release a reference to a block.
|
|---|
| 579 | *
|
|---|
| 580 | * If the last reference is dropped, the block is put on the free list.
|
|---|
| 581 | *
|
|---|
| 582 | * @param block Block of which a reference is to be released.
|
|---|
| 583 | *
|
|---|
| 584 | * @return EOK on success or a negative error code.
|
|---|
| 585 | */
|
|---|
| 586 | int block_put(block_t *block)
|
|---|
| 587 | {
|
|---|
| 588 | devcon_t *devcon = devcon_search(block->service_id);
|
|---|
| 589 | cache_t *cache;
|
|---|
| 590 | unsigned blocks_cached;
|
|---|
| 591 | enum cache_mode mode;
|
|---|
| 592 | int rc = EOK;
|
|---|
| 593 |
|
|---|
| 594 | assert(devcon);
|
|---|
| 595 | assert(devcon->cache);
|
|---|
| 596 | assert(block->refcnt >= 1);
|
|---|
| 597 |
|
|---|
| 598 | cache = devcon->cache;
|
|---|
| 599 |
|
|---|
| 600 | retry:
|
|---|
| 601 | fibril_mutex_lock(&cache->lock);
|
|---|
| 602 | blocks_cached = cache->blocks_cached;
|
|---|
| 603 | mode = cache->mode;
|
|---|
| 604 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 605 |
|
|---|
| 606 | /*
|
|---|
| 607 | * Determine whether to sync the block. Syncing the block is best done
|
|---|
| 608 | * when not holding the cache lock as it does not impede concurrency.
|
|---|
| 609 | * Since the situation may have changed when we unlocked the cache, the
|
|---|
| 610 | * blocks_cached and mode variables are mere hints. We will recheck the
|
|---|
| 611 | * conditions later when the cache lock is held again.
|
|---|
| 612 | */
|
|---|
| 613 | fibril_mutex_lock(&block->lock);
|
|---|
| 614 | if (block->toxic)
|
|---|
| 615 | block->dirty = false; /* will not write back toxic block */
|
|---|
| 616 | if (block->dirty && (block->refcnt == 1) &&
|
|---|
| 617 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
|---|
| 618 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 619 | memcpy(devcon->comm_area, block->data, block->size);
|
|---|
| 620 | rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
|
|---|
| 621 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 622 | block->dirty = false;
|
|---|
| 623 | }
|
|---|
| 624 | fibril_mutex_unlock(&block->lock);
|
|---|
| 625 |
|
|---|
| 626 | fibril_mutex_lock(&cache->lock);
|
|---|
| 627 | fibril_mutex_lock(&block->lock);
|
|---|
| 628 | if (!--block->refcnt) {
|
|---|
| 629 | /*
|
|---|
| 630 | * Last reference to the block was dropped. Either free the
|
|---|
| 631 | * block or put it on the free list. In case of an I/O error,
|
|---|
| 632 | * free the block.
|
|---|
| 633 | */
|
|---|
| 634 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
|---|
| 635 | (rc != EOK)) {
|
|---|
| 636 | /*
|
|---|
| 637 | * Currently there are too many cached blocks or there
|
|---|
| 638 | * was an I/O error when writing the block back to the
|
|---|
| 639 | * device.
|
|---|
| 640 | */
|
|---|
| 641 | if (block->dirty) {
|
|---|
| 642 | /*
|
|---|
| 643 | * We cannot sync the block while holding the
|
|---|
| 644 | * cache lock. Release everything and retry.
|
|---|
| 645 | */
|
|---|
| 646 | block->refcnt++;
|
|---|
| 647 | fibril_mutex_unlock(&block->lock);
|
|---|
| 648 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 649 | goto retry;
|
|---|
| 650 | }
|
|---|
| 651 | /*
|
|---|
| 652 | * Take the block out of the cache and free it.
|
|---|
| 653 | */
|
|---|
| 654 | unsigned long key[2] = {
|
|---|
| 655 | LOWER32(block->lba),
|
|---|
| 656 | UPPER32(block->lba)
|
|---|
| 657 | };
|
|---|
| 658 | hash_table_remove(&cache->block_hash, key, 2);
|
|---|
| 659 | fibril_mutex_unlock(&block->lock);
|
|---|
| 660 | free(block->data);
|
|---|
| 661 | free(block);
|
|---|
| 662 | cache->blocks_cached--;
|
|---|
| 663 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 664 | return rc;
|
|---|
| 665 | }
|
|---|
| 666 | /*
|
|---|
| 667 | * Put the block on the free list.
|
|---|
| 668 | */
|
|---|
| 669 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
|---|
| 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;
|
|---|
| 678 | }
|
|---|
| 679 | list_append(&block->free_link, &cache->free_list);
|
|---|
| 680 | }
|
|---|
| 681 | fibril_mutex_unlock(&block->lock);
|
|---|
| 682 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 683 |
|
|---|
| 684 | return rc;
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | /** Read sequential data from a block device.
|
|---|
| 688 | *
|
|---|
| 689 | * @param service_id Service ID of the block device.
|
|---|
| 690 | * @param bufpos Pointer to the first unread valid offset within the
|
|---|
| 691 | * communication buffer.
|
|---|
| 692 | * @param buflen Pointer to the number of unread bytes that are ready in
|
|---|
| 693 | * the communication buffer.
|
|---|
| 694 | * @param pos Device position to be read.
|
|---|
| 695 | * @param dst Destination buffer.
|
|---|
| 696 | * @param size Size of the destination buffer.
|
|---|
| 697 | * @param block_size Block size to be used for the transfer.
|
|---|
| 698 | *
|
|---|
| 699 | * @return EOK on success or a negative return code on failure.
|
|---|
| 700 | */
|
|---|
| 701 | int block_seqread(service_id_t service_id, size_t *bufpos, size_t *buflen,
|
|---|
| 702 | aoff64_t *pos, void *dst, size_t size)
|
|---|
| 703 | {
|
|---|
| 704 | size_t offset = 0;
|
|---|
| 705 | size_t left = size;
|
|---|
| 706 | size_t block_size;
|
|---|
| 707 | devcon_t *devcon;
|
|---|
| 708 |
|
|---|
| 709 | devcon = devcon_search(service_id);
|
|---|
| 710 | assert(devcon);
|
|---|
| 711 | block_size = devcon->pblock_size;
|
|---|
| 712 |
|
|---|
| 713 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 714 | while (left > 0) {
|
|---|
| 715 | size_t rd;
|
|---|
| 716 |
|
|---|
| 717 | if (*bufpos + left < *buflen)
|
|---|
| 718 | rd = left;
|
|---|
| 719 | else
|
|---|
| 720 | rd = *buflen - *bufpos;
|
|---|
| 721 |
|
|---|
| 722 | if (rd > 0) {
|
|---|
| 723 | /*
|
|---|
| 724 | * Copy the contents of the communication buffer to the
|
|---|
| 725 | * destination buffer.
|
|---|
| 726 | */
|
|---|
| 727 | memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
|
|---|
| 728 | offset += rd;
|
|---|
| 729 | *bufpos += rd;
|
|---|
| 730 | *pos += rd;
|
|---|
| 731 | left -= rd;
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | if (*bufpos == *buflen) {
|
|---|
| 735 | /* Refill the communication buffer with a new block. */
|
|---|
| 736 | int rc;
|
|---|
| 737 |
|
|---|
| 738 | rc = read_blocks(devcon, *pos / block_size, 1);
|
|---|
| 739 | if (rc != EOK) {
|
|---|
| 740 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 741 | return rc;
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | *bufpos = 0;
|
|---|
| 745 | *buflen = block_size;
|
|---|
| 746 | }
|
|---|
| 747 | }
|
|---|
| 748 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 749 |
|
|---|
| 750 | return EOK;
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | /** Read blocks directly from device (bypass cache).
|
|---|
| 754 | *
|
|---|
| 755 | * @param service_id Service ID of the block device.
|
|---|
| 756 | * @param ba Address of first block (physical).
|
|---|
| 757 | * @param cnt Number of blocks.
|
|---|
| 758 | * @param src Buffer for storing the data.
|
|---|
| 759 | *
|
|---|
| 760 | * @return EOK on success or negative error code on failure.
|
|---|
| 761 | */
|
|---|
| 762 | int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
|
|---|
| 763 | {
|
|---|
| 764 | devcon_t *devcon;
|
|---|
| 765 | int rc;
|
|---|
| 766 |
|
|---|
| 767 | devcon = devcon_search(service_id);
|
|---|
| 768 | assert(devcon);
|
|---|
| 769 |
|
|---|
| 770 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 771 |
|
|---|
| 772 | rc = read_blocks(devcon, ba, cnt);
|
|---|
| 773 | if (rc == EOK)
|
|---|
| 774 | memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
|
|---|
| 775 |
|
|---|
| 776 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 777 |
|
|---|
| 778 | return rc;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | /** Write blocks directly to device (bypass cache).
|
|---|
| 782 | *
|
|---|
| 783 | * @param service_id Service ID of the block device.
|
|---|
| 784 | * @param ba Address of first block (physical).
|
|---|
| 785 | * @param cnt Number of blocks.
|
|---|
| 786 | * @param src The data to be written.
|
|---|
| 787 | *
|
|---|
| 788 | * @return EOK on success or negative error code on failure.
|
|---|
| 789 | */
|
|---|
| 790 | int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
|
|---|
| 791 | const void *data)
|
|---|
| 792 | {
|
|---|
| 793 | devcon_t *devcon;
|
|---|
| 794 | int rc;
|
|---|
| 795 |
|
|---|
| 796 | devcon = devcon_search(service_id);
|
|---|
| 797 | assert(devcon);
|
|---|
| 798 |
|
|---|
| 799 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 800 |
|
|---|
| 801 | memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
|
|---|
| 802 | rc = write_blocks(devcon, ba, cnt);
|
|---|
| 803 |
|
|---|
| 804 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 805 |
|
|---|
| 806 | return rc;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | /** Get device block size.
|
|---|
| 810 | *
|
|---|
| 811 | * @param service_id Service ID of the block device.
|
|---|
| 812 | * @param bsize Output block size.
|
|---|
| 813 | *
|
|---|
| 814 | * @return EOK on success or negative error code on failure.
|
|---|
| 815 | */
|
|---|
| 816 | int block_get_bsize(service_id_t service_id, size_t *bsize)
|
|---|
| 817 | {
|
|---|
| 818 | devcon_t *devcon;
|
|---|
| 819 |
|
|---|
| 820 | devcon = devcon_search(service_id);
|
|---|
| 821 | assert(devcon);
|
|---|
| 822 |
|
|---|
| 823 | return get_block_size(devcon->sess, bsize);
|
|---|
| 824 | }
|
|---|
| 825 |
|
|---|
| 826 | /** Get number of blocks on device.
|
|---|
| 827 | *
|
|---|
| 828 | * @param service_id Service ID of the block device.
|
|---|
| 829 | * @param nblocks Output number of blocks.
|
|---|
| 830 | *
|
|---|
| 831 | * @return EOK on success or negative error code on failure.
|
|---|
| 832 | */
|
|---|
| 833 | int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
|
|---|
| 834 | {
|
|---|
| 835 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 836 | assert(devcon);
|
|---|
| 837 |
|
|---|
| 838 | return get_num_blocks(devcon->sess, nblocks);
|
|---|
| 839 | }
|
|---|
| 840 |
|
|---|
| 841 | /** Read bytes directly from the device (bypass cache)
|
|---|
| 842 | *
|
|---|
| 843 | * @param service_id Service ID of the block device.
|
|---|
| 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
|
|---|
| 847 | *
|
|---|
| 848 | * @return EOK on success or negative error code on failure.
|
|---|
| 849 | */
|
|---|
| 850 | int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
|
|---|
| 851 | size_t bytes, void *data)
|
|---|
| 852 | {
|
|---|
| 853 | int rc;
|
|---|
| 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;
|
|---|
| 861 |
|
|---|
| 862 | rc = block_get_bsize(service_id, &phys_block_size);
|
|---|
| 863 | if (rc != EOK) {
|
|---|
| 864 | return rc;
|
|---|
| 865 | }
|
|---|
| 866 |
|
|---|
| 867 | /* calculate data position and required space */
|
|---|
| 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;
|
|---|
| 873 |
|
|---|
| 874 | /* read the data into memory */
|
|---|
| 875 | buffer = malloc(buf_size);
|
|---|
| 876 | if (buffer == NULL) {
|
|---|
| 877 | return ENOMEM;
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | rc = block_read_direct(service_id, first_block, blocks, buffer);
|
|---|
| 881 | if (rc != EOK) {
|
|---|
| 882 | free(buffer);
|
|---|
| 883 | return rc;
|
|---|
| 884 | }
|
|---|
| 885 |
|
|---|
| 886 | /* copy the data from the buffer */
|
|---|
| 887 | memcpy(data, buffer + offset, bytes);
|
|---|
| 888 | free(buffer);
|
|---|
| 889 |
|
|---|
| 890 | return EOK;
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | /** Get TOC from device.
|
|---|
| 894 | *
|
|---|
| 895 | * @param service_id Service ID of the block device.
|
|---|
| 896 | * @param session Starting session.
|
|---|
| 897 | *
|
|---|
| 898 | * @return Allocated TOC structure.
|
|---|
| 899 | * @return NULL on failure.
|
|---|
| 900 | *
|
|---|
| 901 | */
|
|---|
| 902 | toc_block_t *block_get_toc(service_id_t service_id, uint8_t session)
|
|---|
| 903 | {
|
|---|
| 904 | devcon_t *devcon = devcon_search(service_id);
|
|---|
| 905 | assert(devcon);
|
|---|
| 906 |
|
|---|
| 907 | toc_block_t *toc = NULL;
|
|---|
| 908 |
|
|---|
| 909 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 910 |
|
|---|
| 911 | async_exch_t *exch = async_exchange_begin(devcon->sess);
|
|---|
| 912 | int rc = async_req_1_0(exch, BD_READ_TOC, session);
|
|---|
| 913 | async_exchange_end(exch);
|
|---|
| 914 |
|
|---|
| 915 | if (rc == EOK) {
|
|---|
| 916 | toc = (toc_block_t *) malloc(sizeof(toc_block_t));
|
|---|
| 917 | if (toc != NULL) {
|
|---|
| 918 | memset(toc, 0, sizeof(toc_block_t));
|
|---|
| 919 | memcpy(toc, devcon->comm_area,
|
|---|
| 920 | min(devcon->pblock_size, sizeof(toc_block_t)));
|
|---|
| 921 | }
|
|---|
| 922 | }
|
|---|
| 923 |
|
|---|
| 924 |
|
|---|
| 925 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 926 |
|
|---|
| 927 | return toc;
|
|---|
| 928 | }
|
|---|
| 929 |
|
|---|
| 930 | /** Read blocks from block device.
|
|---|
| 931 | *
|
|---|
| 932 | * @param devcon Device connection.
|
|---|
| 933 | * @param ba Address of first block.
|
|---|
| 934 | * @param cnt Number of blocks.
|
|---|
| 935 | * @param src Buffer for storing the data.
|
|---|
| 936 | *
|
|---|
| 937 | * @return EOK on success or negative error code on failure.
|
|---|
| 938 | */
|
|---|
| 939 | static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
|---|
| 940 | {
|
|---|
| 941 | assert(devcon);
|
|---|
| 942 |
|
|---|
| 943 | async_exch_t *exch = async_exchange_begin(devcon->sess);
|
|---|
| 944 | int rc = async_req_3_0(exch, BD_READ_BLOCKS, LOWER32(ba),
|
|---|
| 945 | UPPER32(ba), cnt);
|
|---|
| 946 | async_exchange_end(exch);
|
|---|
| 947 |
|
|---|
| 948 | if (rc != EOK) {
|
|---|
| 949 | printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
|
|---|
| 950 | " from device handle %" PRIun "\n", rc, cnt, ba,
|
|---|
| 951 | devcon->service_id);
|
|---|
| 952 | #ifndef NDEBUG
|
|---|
| 953 | stacktrace_print();
|
|---|
| 954 | #endif
|
|---|
| 955 | }
|
|---|
| 956 |
|
|---|
| 957 | return rc;
|
|---|
| 958 | }
|
|---|
| 959 |
|
|---|
| 960 | /** Write block to block device.
|
|---|
| 961 | *
|
|---|
| 962 | * @param devcon Device connection.
|
|---|
| 963 | * @param ba Address of first block.
|
|---|
| 964 | * @param cnt Number of blocks.
|
|---|
| 965 | * @param src Buffer containing the data to write.
|
|---|
| 966 | *
|
|---|
| 967 | * @return EOK on success or negative error code on failure.
|
|---|
| 968 | */
|
|---|
| 969 | static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
|---|
| 970 | {
|
|---|
| 971 | assert(devcon);
|
|---|
| 972 |
|
|---|
| 973 | async_exch_t *exch = async_exchange_begin(devcon->sess);
|
|---|
| 974 | int rc = async_req_3_0(exch, BD_WRITE_BLOCKS, LOWER32(ba),
|
|---|
| 975 | UPPER32(ba), cnt);
|
|---|
| 976 | async_exchange_end(exch);
|
|---|
| 977 |
|
|---|
| 978 | if (rc != EOK) {
|
|---|
| 979 | printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
|
|---|
| 980 | " to device handle %" PRIun "\n", rc, cnt, ba, devcon->service_id);
|
|---|
| 981 | #ifndef NDEBUG
|
|---|
| 982 | stacktrace_print();
|
|---|
| 983 | #endif
|
|---|
| 984 | }
|
|---|
| 985 |
|
|---|
| 986 | return rc;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | /** Get block size used by the device. */
|
|---|
| 990 | static int get_block_size(async_sess_t *sess, size_t *bsize)
|
|---|
| 991 | {
|
|---|
| 992 | sysarg_t bs;
|
|---|
| 993 |
|
|---|
| 994 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 995 | int rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bs);
|
|---|
| 996 | async_exchange_end(exch);
|
|---|
| 997 |
|
|---|
| 998 | if (rc == EOK)
|
|---|
| 999 | *bsize = (size_t) bs;
|
|---|
| 1000 |
|
|---|
| 1001 | return rc;
|
|---|
| 1002 | }
|
|---|
| 1003 |
|
|---|
| 1004 | /** Get total number of blocks on block device. */
|
|---|
| 1005 | static int get_num_blocks(async_sess_t *sess, aoff64_t *nblocks)
|
|---|
| 1006 | {
|
|---|
| 1007 | sysarg_t nb_l;
|
|---|
| 1008 | sysarg_t nb_h;
|
|---|
| 1009 |
|
|---|
| 1010 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 1011 | int rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
|
|---|
| 1012 | async_exchange_end(exch);
|
|---|
| 1013 |
|
|---|
| 1014 | if (rc == EOK)
|
|---|
| 1015 | *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
|
|---|
| 1016 |
|
|---|
| 1017 | return rc;
|
|---|
| 1018 | }
|
|---|
| 1019 |
|
|---|
| 1020 | /** Convert logical block address to physical block address. */
|
|---|
| 1021 | static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
|
|---|
| 1022 | {
|
|---|
| 1023 | assert(devcon->cache != NULL);
|
|---|
| 1024 | return lba * devcon->cache->blocks_cluster;
|
|---|
| 1025 | }
|
|---|
| 1026 |
|
|---|
| 1027 | /** @}
|
|---|
| 1028 | */
|
|---|