| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2008 Martin Decky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libblock
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "libblock.h"
|
|---|
| 39 | #include "../../srv/vfs/vfs.h"
|
|---|
| 40 | #include <ipc/devmap.h>
|
|---|
| 41 | #include <ipc/bd.h>
|
|---|
| 42 | #include <ipc/services.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <sys/mman.h>
|
|---|
| 45 | #include <async.h>
|
|---|
| 46 | #include <ipc/ipc.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 <sys/typefmt.h>
|
|---|
| 55 | #include <stacktrace.h>
|
|---|
| 56 |
|
|---|
| 57 | /** Lock protecting the device connection list */
|
|---|
| 58 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
|---|
| 59 | /** Device connection list head. */
|
|---|
| 60 | static LIST_INITIALIZE(dcl_head);
|
|---|
| 61 |
|
|---|
| 62 | #define CACHE_BUCKETS_LOG2 10
|
|---|
| 63 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
|---|
| 64 |
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | fibril_mutex_t lock;
|
|---|
| 67 | size_t lblock_size; /**< Logical block size. */
|
|---|
| 68 | unsigned blocks_cluster; /**< Physical blocks per block_t */
|
|---|
| 69 | unsigned block_count; /**< Total number of blocks. */
|
|---|
| 70 | unsigned blocks_cached; /**< Number of cached blocks. */
|
|---|
| 71 | hash_table_t block_hash;
|
|---|
| 72 | link_t free_head;
|
|---|
| 73 | enum cache_mode mode;
|
|---|
| 74 | } cache_t;
|
|---|
| 75 |
|
|---|
| 76 | typedef struct {
|
|---|
| 77 | link_t link;
|
|---|
| 78 | devmap_handle_t devmap_handle;
|
|---|
| 79 | int dev_phone;
|
|---|
| 80 | fibril_mutex_t comm_area_lock;
|
|---|
| 81 | void *comm_area;
|
|---|
| 82 | size_t comm_size;
|
|---|
| 83 | void *bb_buf;
|
|---|
| 84 | aoff64_t bb_addr;
|
|---|
| 85 | size_t pblock_size; /**< Physical block size. */
|
|---|
| 86 | cache_t *cache;
|
|---|
| 87 | } devcon_t;
|
|---|
| 88 |
|
|---|
| 89 | static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
|
|---|
| 90 | static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
|
|---|
| 91 | static int get_block_size(int dev_phone, size_t *bsize);
|
|---|
| 92 | static int get_num_blocks(int dev_phone, aoff64_t *nblocks);
|
|---|
| 93 | static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba);
|
|---|
| 94 |
|
|---|
| 95 | static devcon_t *devcon_search(devmap_handle_t devmap_handle)
|
|---|
| 96 | {
|
|---|
| 97 | link_t *cur;
|
|---|
| 98 |
|
|---|
| 99 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 100 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
|---|
| 101 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
|---|
| 102 | if (devcon->devmap_handle == devmap_handle) {
|
|---|
| 103 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 104 | return devcon;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 108 | return NULL;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static int devcon_add(devmap_handle_t devmap_handle, int dev_phone, size_t bsize,
|
|---|
| 112 | void *comm_area, size_t comm_size)
|
|---|
| 113 | {
|
|---|
| 114 | link_t *cur;
|
|---|
| 115 | devcon_t *devcon;
|
|---|
| 116 |
|
|---|
| 117 | if (comm_size < bsize)
|
|---|
| 118 | return EINVAL;
|
|---|
| 119 |
|
|---|
| 120 | devcon = malloc(sizeof(devcon_t));
|
|---|
| 121 | if (!devcon)
|
|---|
| 122 | return ENOMEM;
|
|---|
| 123 |
|
|---|
| 124 | link_initialize(&devcon->link);
|
|---|
| 125 | devcon->devmap_handle = devmap_handle;
|
|---|
| 126 | devcon->dev_phone = dev_phone;
|
|---|
| 127 | fibril_mutex_initialize(&devcon->comm_area_lock);
|
|---|
| 128 | devcon->comm_area = comm_area;
|
|---|
| 129 | devcon->comm_size = comm_size;
|
|---|
| 130 | devcon->bb_buf = NULL;
|
|---|
| 131 | devcon->bb_addr = 0;
|
|---|
| 132 | devcon->pblock_size = bsize;
|
|---|
| 133 | devcon->cache = NULL;
|
|---|
| 134 |
|
|---|
| 135 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 136 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
|---|
| 137 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
|---|
| 138 | if (d->devmap_handle == devmap_handle) {
|
|---|
| 139 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 140 | free(devcon);
|
|---|
| 141 | return EEXIST;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | list_append(&devcon->link, &dcl_head);
|
|---|
| 145 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 146 | return EOK;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static void devcon_remove(devcon_t *devcon)
|
|---|
| 150 | {
|
|---|
| 151 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 152 | list_remove(&devcon->link);
|
|---|
| 153 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | int block_init(devmap_handle_t devmap_handle, size_t comm_size)
|
|---|
| 157 | {
|
|---|
| 158 | int rc;
|
|---|
| 159 | int dev_phone;
|
|---|
| 160 | void *comm_area;
|
|---|
| 161 | size_t bsize;
|
|---|
| 162 |
|
|---|
| 163 | comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
|
|---|
| 164 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
|---|
| 165 | if (!comm_area) {
|
|---|
| 166 | return ENOMEM;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | dev_phone = devmap_device_connect(devmap_handle, IPC_FLAG_BLOCKING);
|
|---|
| 170 | if (dev_phone < 0) {
|
|---|
| 171 | munmap(comm_area, comm_size);
|
|---|
| 172 | return dev_phone;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | rc = async_share_out_start(dev_phone, comm_area,
|
|---|
| 176 | AS_AREA_READ | AS_AREA_WRITE);
|
|---|
| 177 | if (rc != EOK) {
|
|---|
| 178 | munmap(comm_area, comm_size);
|
|---|
| 179 | ipc_hangup(dev_phone);
|
|---|
| 180 | return rc;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (get_block_size(dev_phone, &bsize) != EOK) {
|
|---|
| 184 | munmap(comm_area, comm_size);
|
|---|
| 185 | ipc_hangup(dev_phone);
|
|---|
| 186 | return rc;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | rc = devcon_add(devmap_handle, dev_phone, bsize, comm_area, comm_size);
|
|---|
| 190 | if (rc != EOK) {
|
|---|
| 191 | munmap(comm_area, comm_size);
|
|---|
| 192 | ipc_hangup(dev_phone);
|
|---|
| 193 | return rc;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | return EOK;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void block_fini(devmap_handle_t devmap_handle)
|
|---|
| 200 | {
|
|---|
| 201 | devcon_t *devcon = devcon_search(devmap_handle);
|
|---|
| 202 | assert(devcon);
|
|---|
| 203 |
|
|---|
| 204 | if (devcon->cache)
|
|---|
| 205 | (void) block_cache_fini(devmap_handle);
|
|---|
| 206 |
|
|---|
| 207 | devcon_remove(devcon);
|
|---|
| 208 |
|
|---|
| 209 | if (devcon->bb_buf)
|
|---|
| 210 | free(devcon->bb_buf);
|
|---|
| 211 |
|
|---|
| 212 | munmap(devcon->comm_area, devcon->comm_size);
|
|---|
| 213 | ipc_hangup(devcon->dev_phone);
|
|---|
| 214 |
|
|---|
| 215 | free(devcon);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | int block_bb_read(devmap_handle_t devmap_handle, aoff64_t ba)
|
|---|
| 219 | {
|
|---|
| 220 | void *bb_buf;
|
|---|
| 221 | int rc;
|
|---|
| 222 |
|
|---|
| 223 | devcon_t *devcon = devcon_search(devmap_handle);
|
|---|
| 224 | if (!devcon)
|
|---|
| 225 | return ENOENT;
|
|---|
| 226 | if (devcon->bb_buf)
|
|---|
| 227 | return EEXIST;
|
|---|
| 228 | bb_buf = malloc(devcon->pblock_size);
|
|---|
| 229 | if (!bb_buf)
|
|---|
| 230 | return ENOMEM;
|
|---|
| 231 |
|
|---|
| 232 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 233 | rc = read_blocks(devcon, 0, 1);
|
|---|
| 234 | if (rc != EOK) {
|
|---|
| 235 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 236 | free(bb_buf);
|
|---|
| 237 | return rc;
|
|---|
| 238 | }
|
|---|
| 239 | memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
|
|---|
| 240 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 241 |
|
|---|
| 242 | devcon->bb_buf = bb_buf;
|
|---|
| 243 | devcon->bb_addr = ba;
|
|---|
| 244 |
|
|---|
| 245 | return EOK;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | void *block_bb_get(devmap_handle_t devmap_handle)
|
|---|
| 249 | {
|
|---|
| 250 | devcon_t *devcon = devcon_search(devmap_handle);
|
|---|
| 251 | assert(devcon);
|
|---|
| 252 | return devcon->bb_buf;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | static hash_index_t cache_hash(unsigned long *key)
|
|---|
| 256 | {
|
|---|
| 257 | return *key & (CACHE_BUCKETS - 1);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
|---|
| 261 | {
|
|---|
| 262 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
|---|
| 263 | return b->lba == *key;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | static void cache_remove_callback(link_t *item)
|
|---|
| 267 | {
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | static hash_table_operations_t cache_ops = {
|
|---|
| 271 | .hash = cache_hash,
|
|---|
| 272 | .compare = cache_compare,
|
|---|
| 273 | .remove_callback = cache_remove_callback
|
|---|
| 274 | };
|
|---|
| 275 |
|
|---|
| 276 | int block_cache_init(devmap_handle_t devmap_handle, size_t size, unsigned blocks,
|
|---|
| 277 | enum cache_mode mode)
|
|---|
| 278 | {
|
|---|
| 279 | devcon_t *devcon = devcon_search(devmap_handle);
|
|---|
| 280 | cache_t *cache;
|
|---|
| 281 | if (!devcon)
|
|---|
| 282 | return ENOENT;
|
|---|
| 283 | if (devcon->cache)
|
|---|
| 284 | return EEXIST;
|
|---|
| 285 | cache = malloc(sizeof(cache_t));
|
|---|
| 286 | if (!cache)
|
|---|
| 287 | return ENOMEM;
|
|---|
| 288 |
|
|---|
| 289 | fibril_mutex_initialize(&cache->lock);
|
|---|
| 290 | list_initialize(&cache->free_head);
|
|---|
| 291 | cache->lblock_size = size;
|
|---|
| 292 | cache->block_count = blocks;
|
|---|
| 293 | cache->blocks_cached = 0;
|
|---|
| 294 | cache->mode = mode;
|
|---|
| 295 |
|
|---|
| 296 | /* Allow 1:1 or small-to-large block size translation */
|
|---|
| 297 | if (cache->lblock_size % devcon->pblock_size != 0)
|
|---|
| 298 | return ENOTSUP;
|
|---|
| 299 |
|
|---|
| 300 | cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
|
|---|
| 301 |
|
|---|
| 302 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
|
|---|
| 303 | &cache_ops)) {
|
|---|
| 304 | free(cache);
|
|---|
| 305 | return ENOMEM;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | devcon->cache = cache;
|
|---|
| 309 | return EOK;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | int block_cache_fini(devmap_handle_t devmap_handle)
|
|---|
| 313 | {
|
|---|
| 314 | devcon_t *devcon = devcon_search(devmap_handle);
|
|---|
| 315 | cache_t *cache;
|
|---|
| 316 | int rc;
|
|---|
| 317 |
|
|---|
| 318 | if (!devcon)
|
|---|
| 319 | return ENOENT;
|
|---|
| 320 | if (!devcon->cache)
|
|---|
| 321 | return EOK;
|
|---|
| 322 | cache = devcon->cache;
|
|---|
| 323 |
|
|---|
| 324 | /*
|
|---|
| 325 | * We are expecting to find all blocks for this device handle on the
|
|---|
| 326 | * free list, i.e. the block reference count should be zero. Do not
|
|---|
| 327 | * bother with the cache and block locks because we are single-threaded.
|
|---|
| 328 | */
|
|---|
| 329 | while (!list_empty(&cache->free_head)) {
|
|---|
| 330 | block_t *b = list_get_instance(cache->free_head.next,
|
|---|
| 331 | block_t, free_link);
|
|---|
| 332 |
|
|---|
| 333 | list_remove(&b->free_link);
|
|---|
| 334 | if (b->dirty) {
|
|---|
| 335 | memcpy(devcon->comm_area, b->data, b->size);
|
|---|
| 336 | rc = write_blocks(devcon, b->pba, cache->blocks_cluster);
|
|---|
| 337 | if (rc != EOK)
|
|---|
| 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | unsigned long key = b->lba;
|
|---|
| 342 | hash_table_remove(&cache->block_hash, &key, 1);
|
|---|
| 343 |
|
|---|
| 344 | free(b->data);
|
|---|
| 345 | free(b);
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | hash_table_destroy(&cache->block_hash);
|
|---|
| 349 | devcon->cache = NULL;
|
|---|
| 350 | free(cache);
|
|---|
| 351 |
|
|---|
| 352 | return EOK;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | #define CACHE_LO_WATERMARK 10
|
|---|
| 356 | #define CACHE_HI_WATERMARK 20
|
|---|
| 357 | static bool cache_can_grow(cache_t *cache)
|
|---|
| 358 | {
|
|---|
| 359 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
|---|
| 360 | return true;
|
|---|
| 361 | if (!list_empty(&cache->free_head))
|
|---|
| 362 | return false;
|
|---|
| 363 | return true;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | static void block_initialize(block_t *b)
|
|---|
| 367 | {
|
|---|
| 368 | fibril_mutex_initialize(&b->lock);
|
|---|
| 369 | b->refcnt = 1;
|
|---|
| 370 | b->dirty = false;
|
|---|
| 371 | b->toxic = false;
|
|---|
| 372 | fibril_rwlock_initialize(&b->contents_lock);
|
|---|
| 373 | link_initialize(&b->free_link);
|
|---|
| 374 | link_initialize(&b->hash_link);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Instantiate a block in memory and get a reference to it.
|
|---|
| 378 | *
|
|---|
| 379 | * @param block Pointer to where the function will store the
|
|---|
| 380 | * block pointer on success.
|
|---|
| 381 | * @param devmap_handle Device handle of the block device.
|
|---|
| 382 | * @param ba Block address (logical).
|
|---|
| 383 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
|---|
| 384 | * will not read the contents of the block from the
|
|---|
| 385 | * device.
|
|---|
| 386 | *
|
|---|
| 387 | * @return EOK on success or a negative error code.
|
|---|
| 388 | */
|
|---|
| 389 | int block_get(block_t **block, devmap_handle_t devmap_handle, aoff64_t ba, int flags)
|
|---|
| 390 | {
|
|---|
| 391 | devcon_t *devcon;
|
|---|
| 392 | cache_t *cache;
|
|---|
| 393 | block_t *b;
|
|---|
| 394 | link_t *l;
|
|---|
| 395 | unsigned long key = ba;
|
|---|
| 396 | int rc;
|
|---|
| 397 |
|
|---|
| 398 | devcon = devcon_search(devmap_handle);
|
|---|
| 399 |
|
|---|
| 400 | assert(devcon);
|
|---|
| 401 | assert(devcon->cache);
|
|---|
| 402 |
|
|---|
| 403 | cache = devcon->cache;
|
|---|
| 404 |
|
|---|
| 405 | retry:
|
|---|
| 406 | rc = EOK;
|
|---|
| 407 | b = NULL;
|
|---|
| 408 |
|
|---|
| 409 | fibril_mutex_lock(&cache->lock);
|
|---|
| 410 | l = hash_table_find(&cache->block_hash, &key);
|
|---|
| 411 | if (l) {
|
|---|
| 412 | /*
|
|---|
| 413 | * We found the block in the cache.
|
|---|
| 414 | */
|
|---|
| 415 | b = hash_table_get_instance(l, block_t, hash_link);
|
|---|
| 416 | fibril_mutex_lock(&b->lock);
|
|---|
| 417 | if (b->refcnt++ == 0)
|
|---|
| 418 | list_remove(&b->free_link);
|
|---|
| 419 | if (b->toxic)
|
|---|
| 420 | rc = EIO;
|
|---|
| 421 | fibril_mutex_unlock(&b->lock);
|
|---|
| 422 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 423 | } else {
|
|---|
| 424 | /*
|
|---|
| 425 | * The block was not found in the cache.
|
|---|
| 426 | */
|
|---|
| 427 | if (cache_can_grow(cache)) {
|
|---|
| 428 | /*
|
|---|
| 429 | * We can grow the cache by allocating new blocks.
|
|---|
| 430 | * Should the allocation fail, we fail over and try to
|
|---|
| 431 | * recycle a block from the cache.
|
|---|
| 432 | */
|
|---|
| 433 | b = malloc(sizeof(block_t));
|
|---|
| 434 | if (!b)
|
|---|
| 435 | goto recycle;
|
|---|
| 436 | b->data = malloc(cache->lblock_size);
|
|---|
| 437 | if (!b->data) {
|
|---|
| 438 | free(b);
|
|---|
| 439 | goto recycle;
|
|---|
| 440 | }
|
|---|
| 441 | cache->blocks_cached++;
|
|---|
| 442 | } else {
|
|---|
| 443 | /*
|
|---|
| 444 | * Try to recycle a block from the free list.
|
|---|
| 445 | */
|
|---|
| 446 | unsigned long temp_key;
|
|---|
| 447 | recycle:
|
|---|
| 448 | if (list_empty(&cache->free_head)) {
|
|---|
| 449 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 450 | rc = ENOMEM;
|
|---|
| 451 | goto out;
|
|---|
| 452 | }
|
|---|
| 453 | l = cache->free_head.next;
|
|---|
| 454 | b = list_get_instance(l, block_t, free_link);
|
|---|
| 455 |
|
|---|
| 456 | fibril_mutex_lock(&b->lock);
|
|---|
| 457 | if (b->dirty) {
|
|---|
| 458 | /*
|
|---|
| 459 | * The block needs to be written back to the
|
|---|
| 460 | * device before it changes identity. Do this
|
|---|
| 461 | * while not holding the cache lock so that
|
|---|
| 462 | * concurrency is not impeded. Also move the
|
|---|
| 463 | * block to the end of the free list so that we
|
|---|
| 464 | * do not slow down other instances of
|
|---|
| 465 | * block_get() draining the free list.
|
|---|
| 466 | */
|
|---|
| 467 | list_remove(&b->free_link);
|
|---|
| 468 | list_append(&b->free_link, &cache->free_head);
|
|---|
| 469 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 470 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 471 | memcpy(devcon->comm_area, b->data, b->size);
|
|---|
| 472 | rc = write_blocks(devcon, b->pba,
|
|---|
| 473 | cache->blocks_cluster);
|
|---|
| 474 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 475 | if (rc != EOK) {
|
|---|
| 476 | /*
|
|---|
| 477 | * We did not manage to write the block
|
|---|
| 478 | * to the device. Keep it around for
|
|---|
| 479 | * another try. Hopefully, we will grab
|
|---|
| 480 | * another block next time.
|
|---|
| 481 | */
|
|---|
| 482 | fibril_mutex_unlock(&b->lock);
|
|---|
| 483 | goto retry;
|
|---|
| 484 | }
|
|---|
| 485 | b->dirty = false;
|
|---|
| 486 | if (!fibril_mutex_trylock(&cache->lock)) {
|
|---|
| 487 | /*
|
|---|
| 488 | * Somebody is probably racing with us.
|
|---|
| 489 | * Unlock the block and retry.
|
|---|
| 490 | */
|
|---|
| 491 | fibril_mutex_unlock(&b->lock);
|
|---|
| 492 | goto retry;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | }
|
|---|
| 496 | fibril_mutex_unlock(&b->lock);
|
|---|
| 497 |
|
|---|
| 498 | /*
|
|---|
| 499 | * Unlink the block from the free list and the hash
|
|---|
| 500 | * table.
|
|---|
| 501 | */
|
|---|
| 502 | list_remove(&b->free_link);
|
|---|
| 503 | temp_key = b->lba;
|
|---|
| 504 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | block_initialize(b);
|
|---|
| 508 | b->devmap_handle = devmap_handle;
|
|---|
| 509 | b->size = cache->lblock_size;
|
|---|
| 510 | b->lba = ba;
|
|---|
| 511 | b->pba = ba_ltop(devcon, b->lba);
|
|---|
| 512 | hash_table_insert(&cache->block_hash, &key, &b->hash_link);
|
|---|
| 513 |
|
|---|
| 514 | /*
|
|---|
| 515 | * Lock the block before releasing the cache lock. Thus we don't
|
|---|
| 516 | * kill concurrent operations on the cache while doing I/O on
|
|---|
| 517 | * the block.
|
|---|
| 518 | */
|
|---|
| 519 | fibril_mutex_lock(&b->lock);
|
|---|
| 520 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 521 |
|
|---|
| 522 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
|---|
| 523 | /*
|
|---|
| 524 | * The block contains old or no data. We need to read
|
|---|
| 525 | * the new contents from the device.
|
|---|
| 526 | */
|
|---|
| 527 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 528 | rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
|
|---|
| 529 | memcpy(b->data, devcon->comm_area, cache->lblock_size);
|
|---|
| 530 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 531 | if (rc != EOK)
|
|---|
| 532 | b->toxic = true;
|
|---|
| 533 | } else
|
|---|
| 534 | rc = EOK;
|
|---|
| 535 |
|
|---|
| 536 | fibril_mutex_unlock(&b->lock);
|
|---|
| 537 | }
|
|---|
| 538 | out:
|
|---|
| 539 | if ((rc != EOK) && b) {
|
|---|
| 540 | assert(b->toxic);
|
|---|
| 541 | (void) block_put(b);
|
|---|
| 542 | b = NULL;
|
|---|
| 543 | }
|
|---|
| 544 | *block = b;
|
|---|
| 545 | return rc;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | /** Release a reference to a block.
|
|---|
| 549 | *
|
|---|
| 550 | * If the last reference is dropped, the block is put on the free list.
|
|---|
| 551 | *
|
|---|
| 552 | * @param block Block of which a reference is to be released.
|
|---|
| 553 | *
|
|---|
| 554 | * @return EOK on success or a negative error code.
|
|---|
| 555 | */
|
|---|
| 556 | int block_put(block_t *block)
|
|---|
| 557 | {
|
|---|
| 558 | devcon_t *devcon = devcon_search(block->devmap_handle);
|
|---|
| 559 | cache_t *cache;
|
|---|
| 560 | unsigned blocks_cached;
|
|---|
| 561 | enum cache_mode mode;
|
|---|
| 562 | int rc = EOK;
|
|---|
| 563 |
|
|---|
| 564 | assert(devcon);
|
|---|
| 565 | assert(devcon->cache);
|
|---|
| 566 |
|
|---|
| 567 | cache = devcon->cache;
|
|---|
| 568 |
|
|---|
| 569 | retry:
|
|---|
| 570 | fibril_mutex_lock(&cache->lock);
|
|---|
| 571 | blocks_cached = cache->blocks_cached;
|
|---|
| 572 | mode = cache->mode;
|
|---|
| 573 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 574 |
|
|---|
| 575 | /*
|
|---|
| 576 | * Determine whether to sync the block. Syncing the block is best done
|
|---|
| 577 | * when not holding the cache lock as it does not impede concurrency.
|
|---|
| 578 | * Since the situation may have changed when we unlocked the cache, the
|
|---|
| 579 | * blocks_cached and mode variables are mere hints. We will recheck the
|
|---|
| 580 | * conditions later when the cache lock is held again.
|
|---|
| 581 | */
|
|---|
| 582 | fibril_mutex_lock(&block->lock);
|
|---|
| 583 | if (block->toxic)
|
|---|
| 584 | block->dirty = false; /* will not write back toxic block */
|
|---|
| 585 | if (block->dirty && (block->refcnt == 1) &&
|
|---|
| 586 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
|---|
| 587 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 588 | memcpy(devcon->comm_area, block->data, block->size);
|
|---|
| 589 | rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
|
|---|
| 590 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 591 | block->dirty = false;
|
|---|
| 592 | }
|
|---|
| 593 | fibril_mutex_unlock(&block->lock);
|
|---|
| 594 |
|
|---|
| 595 | fibril_mutex_lock(&cache->lock);
|
|---|
| 596 | fibril_mutex_lock(&block->lock);
|
|---|
| 597 | if (!--block->refcnt) {
|
|---|
| 598 | /*
|
|---|
| 599 | * Last reference to the block was dropped. Either free the
|
|---|
| 600 | * block or put it on the free list. In case of an I/O error,
|
|---|
| 601 | * free the block.
|
|---|
| 602 | */
|
|---|
| 603 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
|---|
| 604 | (rc != EOK)) {
|
|---|
| 605 | /*
|
|---|
| 606 | * Currently there are too many cached blocks or there
|
|---|
| 607 | * was an I/O error when writing the block back to the
|
|---|
| 608 | * device.
|
|---|
| 609 | */
|
|---|
| 610 | if (block->dirty) {
|
|---|
| 611 | /*
|
|---|
| 612 | * We cannot sync the block while holding the
|
|---|
| 613 | * cache lock. Release everything and retry.
|
|---|
| 614 | */
|
|---|
| 615 | block->refcnt++;
|
|---|
| 616 | fibril_mutex_unlock(&block->lock);
|
|---|
| 617 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 618 | goto retry;
|
|---|
| 619 | }
|
|---|
| 620 | /*
|
|---|
| 621 | * Take the block out of the cache and free it.
|
|---|
| 622 | */
|
|---|
| 623 | unsigned long key = block->lba;
|
|---|
| 624 | hash_table_remove(&cache->block_hash, &key, 1);
|
|---|
| 625 | free(block);
|
|---|
| 626 | free(block->data);
|
|---|
| 627 | cache->blocks_cached--;
|
|---|
| 628 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 629 | return rc;
|
|---|
| 630 | }
|
|---|
| 631 | /*
|
|---|
| 632 | * Put the block on the free list.
|
|---|
| 633 | */
|
|---|
| 634 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
|---|
| 635 | /*
|
|---|
| 636 | * We cannot sync the block while holding the cache
|
|---|
| 637 | * lock. Release everything and retry.
|
|---|
| 638 | */
|
|---|
| 639 | block->refcnt++;
|
|---|
| 640 | fibril_mutex_unlock(&block->lock);
|
|---|
| 641 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 642 | goto retry;
|
|---|
| 643 | }
|
|---|
| 644 | list_append(&block->free_link, &cache->free_head);
|
|---|
| 645 | }
|
|---|
| 646 | fibril_mutex_unlock(&block->lock);
|
|---|
| 647 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 648 |
|
|---|
| 649 | return rc;
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | /** Read sequential data from a block device.
|
|---|
| 653 | *
|
|---|
| 654 | * @param devmap_handle Device handle of the block device.
|
|---|
| 655 | * @param bufpos Pointer to the first unread valid offset within the
|
|---|
| 656 | * communication buffer.
|
|---|
| 657 | * @param buflen Pointer to the number of unread bytes that are ready in
|
|---|
| 658 | * the communication buffer.
|
|---|
| 659 | * @param pos Device position to be read.
|
|---|
| 660 | * @param dst Destination buffer.
|
|---|
| 661 | * @param size Size of the destination buffer.
|
|---|
| 662 | * @param block_size Block size to be used for the transfer.
|
|---|
| 663 | *
|
|---|
| 664 | * @return EOK on success or a negative return code on failure.
|
|---|
| 665 | */
|
|---|
| 666 | int block_seqread(devmap_handle_t devmap_handle, size_t *bufpos, size_t *buflen,
|
|---|
| 667 | aoff64_t *pos, void *dst, size_t size)
|
|---|
| 668 | {
|
|---|
| 669 | size_t offset = 0;
|
|---|
| 670 | size_t left = size;
|
|---|
| 671 | size_t block_size;
|
|---|
| 672 | devcon_t *devcon;
|
|---|
| 673 |
|
|---|
| 674 | devcon = devcon_search(devmap_handle);
|
|---|
| 675 | assert(devcon);
|
|---|
| 676 | block_size = devcon->pblock_size;
|
|---|
| 677 |
|
|---|
| 678 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 679 | while (left > 0) {
|
|---|
| 680 | size_t rd;
|
|---|
| 681 |
|
|---|
| 682 | if (*bufpos + left < *buflen)
|
|---|
| 683 | rd = left;
|
|---|
| 684 | else
|
|---|
| 685 | rd = *buflen - *bufpos;
|
|---|
| 686 |
|
|---|
| 687 | if (rd > 0) {
|
|---|
| 688 | /*
|
|---|
| 689 | * Copy the contents of the communication buffer to the
|
|---|
| 690 | * destination buffer.
|
|---|
| 691 | */
|
|---|
| 692 | memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
|
|---|
| 693 | offset += rd;
|
|---|
| 694 | *bufpos += rd;
|
|---|
| 695 | *pos += rd;
|
|---|
| 696 | left -= rd;
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | if (*bufpos == *buflen) {
|
|---|
| 700 | /* Refill the communication buffer with a new block. */
|
|---|
| 701 | int rc;
|
|---|
| 702 |
|
|---|
| 703 | rc = read_blocks(devcon, *pos / block_size, 1);
|
|---|
| 704 | if (rc != EOK) {
|
|---|
| 705 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 706 | return rc;
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | *bufpos = 0;
|
|---|
| 710 | *buflen = block_size;
|
|---|
| 711 | }
|
|---|
| 712 | }
|
|---|
| 713 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 714 |
|
|---|
| 715 | return EOK;
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | /** Read blocks directly from device (bypass cache).
|
|---|
| 719 | *
|
|---|
| 720 | * @param devmap_handle Device handle of the block device.
|
|---|
| 721 | * @param ba Address of first block (physical).
|
|---|
| 722 | * @param cnt Number of blocks.
|
|---|
| 723 | * @param src Buffer for storing the data.
|
|---|
| 724 | *
|
|---|
| 725 | * @return EOK on success or negative error code on failure.
|
|---|
| 726 | */
|
|---|
| 727 | int block_read_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt, void *buf)
|
|---|
| 728 | {
|
|---|
| 729 | devcon_t *devcon;
|
|---|
| 730 | int rc;
|
|---|
| 731 |
|
|---|
| 732 | devcon = devcon_search(devmap_handle);
|
|---|
| 733 | assert(devcon);
|
|---|
| 734 |
|
|---|
| 735 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 736 |
|
|---|
| 737 | rc = read_blocks(devcon, ba, cnt);
|
|---|
| 738 | if (rc == EOK)
|
|---|
| 739 | memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
|
|---|
| 740 |
|
|---|
| 741 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 742 |
|
|---|
| 743 | return rc;
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | /** Write blocks directly to device (bypass cache).
|
|---|
| 747 | *
|
|---|
| 748 | * @param devmap_handle Device handle of the block device.
|
|---|
| 749 | * @param ba Address of first block (physical).
|
|---|
| 750 | * @param cnt Number of blocks.
|
|---|
| 751 | * @param src The data to be written.
|
|---|
| 752 | *
|
|---|
| 753 | * @return EOK on success or negative error code on failure.
|
|---|
| 754 | */
|
|---|
| 755 | int block_write_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt,
|
|---|
| 756 | const void *data)
|
|---|
| 757 | {
|
|---|
| 758 | devcon_t *devcon;
|
|---|
| 759 | int rc;
|
|---|
| 760 |
|
|---|
| 761 | devcon = devcon_search(devmap_handle);
|
|---|
| 762 | assert(devcon);
|
|---|
| 763 |
|
|---|
| 764 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 765 |
|
|---|
| 766 | memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
|
|---|
| 767 | rc = write_blocks(devcon, ba, cnt);
|
|---|
| 768 |
|
|---|
| 769 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 770 |
|
|---|
| 771 | return rc;
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | /** Get device block size.
|
|---|
| 775 | *
|
|---|
| 776 | * @param devmap_handle Device handle of the block device.
|
|---|
| 777 | * @param bsize Output block size.
|
|---|
| 778 | *
|
|---|
| 779 | * @return EOK on success or negative error code on failure.
|
|---|
| 780 | */
|
|---|
| 781 | int block_get_bsize(devmap_handle_t devmap_handle, size_t *bsize)
|
|---|
| 782 | {
|
|---|
| 783 | devcon_t *devcon;
|
|---|
| 784 |
|
|---|
| 785 | devcon = devcon_search(devmap_handle);
|
|---|
| 786 | assert(devcon);
|
|---|
| 787 |
|
|---|
| 788 | return get_block_size(devcon->dev_phone, bsize);
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | /** Get number of blocks on device.
|
|---|
| 792 | *
|
|---|
| 793 | * @param devmap_handle Device handle of the block device.
|
|---|
| 794 | * @param nblocks Output number of blocks.
|
|---|
| 795 | *
|
|---|
| 796 | * @return EOK on success or negative error code on failure.
|
|---|
| 797 | */
|
|---|
| 798 | int block_get_nblocks(devmap_handle_t devmap_handle, aoff64_t *nblocks)
|
|---|
| 799 | {
|
|---|
| 800 | devcon_t *devcon;
|
|---|
| 801 |
|
|---|
| 802 | devcon = devcon_search(devmap_handle);
|
|---|
| 803 | assert(devcon);
|
|---|
| 804 |
|
|---|
| 805 | return get_num_blocks(devcon->dev_phone, nblocks);
|
|---|
| 806 | }
|
|---|
| 807 |
|
|---|
| 808 | /** Read blocks from block device.
|
|---|
| 809 | *
|
|---|
| 810 | * @param devcon Device connection.
|
|---|
| 811 | * @param ba Address of first block.
|
|---|
| 812 | * @param cnt Number of blocks.
|
|---|
| 813 | * @param src Buffer for storing the data.
|
|---|
| 814 | *
|
|---|
| 815 | * @return EOK on success or negative error code on failure.
|
|---|
| 816 | */
|
|---|
| 817 | static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
|---|
| 818 | {
|
|---|
| 819 | int rc;
|
|---|
| 820 |
|
|---|
| 821 | assert(devcon);
|
|---|
| 822 | rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
|
|---|
| 823 | UPPER32(ba), cnt);
|
|---|
| 824 | if (rc != EOK) {
|
|---|
| 825 | printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
|
|---|
| 826 | " from device handle %" PRIun "\n", rc, cnt, ba,
|
|---|
| 827 | devcon->devmap_handle);
|
|---|
| 828 | #ifndef NDEBUG
|
|---|
| 829 | stacktrace_print();
|
|---|
| 830 | #endif
|
|---|
| 831 | }
|
|---|
| 832 | return rc;
|
|---|
| 833 | }
|
|---|
| 834 |
|
|---|
| 835 | /** Write block to block device.
|
|---|
| 836 | *
|
|---|
| 837 | * @param devcon Device connection.
|
|---|
| 838 | * @param ba Address of first block.
|
|---|
| 839 | * @param cnt Number of blocks.
|
|---|
| 840 | * @param src Buffer containing the data to write.
|
|---|
| 841 | *
|
|---|
| 842 | * @return EOK on success or negative error code on failure.
|
|---|
| 843 | */
|
|---|
| 844 | static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
|---|
| 845 | {
|
|---|
| 846 | int rc;
|
|---|
| 847 |
|
|---|
| 848 | assert(devcon);
|
|---|
| 849 | rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
|
|---|
| 850 | UPPER32(ba), cnt);
|
|---|
| 851 | if (rc != EOK) {
|
|---|
| 852 | printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
|
|---|
| 853 | " to device handle %" PRIun "\n", rc, cnt, ba, devcon->devmap_handle);
|
|---|
| 854 | #ifndef NDEBUG
|
|---|
| 855 | stacktrace_print();
|
|---|
| 856 | #endif
|
|---|
| 857 | }
|
|---|
| 858 | return rc;
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | /** Get block size used by the device. */
|
|---|
| 862 | static int get_block_size(int dev_phone, size_t *bsize)
|
|---|
| 863 | {
|
|---|
| 864 | ipcarg_t bs;
|
|---|
| 865 | int rc;
|
|---|
| 866 |
|
|---|
| 867 | rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs);
|
|---|
| 868 | if (rc == EOK)
|
|---|
| 869 | *bsize = (size_t) bs;
|
|---|
| 870 |
|
|---|
| 871 | return rc;
|
|---|
| 872 | }
|
|---|
| 873 |
|
|---|
| 874 | /** Get total number of blocks on block device. */
|
|---|
| 875 | static int get_num_blocks(int dev_phone, aoff64_t *nblocks)
|
|---|
| 876 | {
|
|---|
| 877 | ipcarg_t nb_l, nb_h;
|
|---|
| 878 | int rc;
|
|---|
| 879 |
|
|---|
| 880 | rc = async_req_0_2(dev_phone, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
|
|---|
| 881 | if (rc == EOK) {
|
|---|
| 882 | *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
|
|---|
| 883 | }
|
|---|
| 884 |
|
|---|
| 885 | return rc;
|
|---|
| 886 | }
|
|---|
| 887 |
|
|---|
| 888 | /** Convert logical block address to physical block address. */
|
|---|
| 889 | static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
|
|---|
| 890 | {
|
|---|
| 891 | assert(devcon->cache != NULL);
|
|---|
| 892 | return lba * devcon->cache->blocks_cluster;
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | /** @}
|
|---|
| 896 | */
|
|---|