| 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 |
|
|---|
| 55 | /** Lock protecting the device connection list */
|
|---|
| 56 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
|---|
| 57 | /** Device connection list head. */
|
|---|
| 58 | static LIST_INITIALIZE(dcl_head);
|
|---|
| 59 |
|
|---|
| 60 | #define CACHE_BUCKETS_LOG2 10
|
|---|
| 61 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
|---|
| 62 |
|
|---|
| 63 | typedef struct {
|
|---|
| 64 | fibril_mutex_t lock;
|
|---|
| 65 | size_t lblock_size; /**< Logical block size. */
|
|---|
| 66 | unsigned block_count; /**< Total number of blocks. */
|
|---|
| 67 | unsigned blocks_cached; /**< Number of cached blocks. */
|
|---|
| 68 | hash_table_t block_hash;
|
|---|
| 69 | link_t free_head;
|
|---|
| 70 | enum cache_mode mode;
|
|---|
| 71 | } cache_t;
|
|---|
| 72 |
|
|---|
| 73 | typedef struct {
|
|---|
| 74 | link_t link;
|
|---|
| 75 | dev_handle_t dev_handle;
|
|---|
| 76 | int dev_phone;
|
|---|
| 77 | fibril_mutex_t comm_area_lock;
|
|---|
| 78 | void *comm_area;
|
|---|
| 79 | size_t comm_size;
|
|---|
| 80 | void *bb_buf;
|
|---|
| 81 | bn_t bb_addr;
|
|---|
| 82 | size_t pblock_size; /**< Physical block size. */
|
|---|
| 83 | cache_t *cache;
|
|---|
| 84 | } devcon_t;
|
|---|
| 85 |
|
|---|
| 86 | static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
|
|---|
| 87 | static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
|
|---|
| 88 | static int get_block_size(int dev_phone, size_t *bsize);
|
|---|
| 89 |
|
|---|
| 90 | static devcon_t *devcon_search(dev_handle_t dev_handle)
|
|---|
| 91 | {
|
|---|
| 92 | link_t *cur;
|
|---|
| 93 |
|
|---|
| 94 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 95 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
|---|
| 96 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
|---|
| 97 | if (devcon->dev_handle == dev_handle) {
|
|---|
| 98 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 99 | return devcon;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 103 | return NULL;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, size_t bsize,
|
|---|
| 107 | void *comm_area, size_t comm_size)
|
|---|
| 108 | {
|
|---|
| 109 | link_t *cur;
|
|---|
| 110 | devcon_t *devcon;
|
|---|
| 111 |
|
|---|
| 112 | if (comm_size < bsize)
|
|---|
| 113 | return EINVAL;
|
|---|
| 114 |
|
|---|
| 115 | devcon = malloc(sizeof(devcon_t));
|
|---|
| 116 | if (!devcon)
|
|---|
| 117 | return ENOMEM;
|
|---|
| 118 |
|
|---|
| 119 | link_initialize(&devcon->link);
|
|---|
| 120 | devcon->dev_handle = dev_handle;
|
|---|
| 121 | devcon->dev_phone = dev_phone;
|
|---|
| 122 | fibril_mutex_initialize(&devcon->comm_area_lock);
|
|---|
| 123 | devcon->comm_area = comm_area;
|
|---|
| 124 | devcon->comm_size = comm_size;
|
|---|
| 125 | devcon->bb_buf = NULL;
|
|---|
| 126 | devcon->bb_addr = 0;
|
|---|
| 127 | devcon->pblock_size = bsize;
|
|---|
| 128 | devcon->cache = NULL;
|
|---|
| 129 |
|
|---|
| 130 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 131 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
|---|
| 132 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
|---|
| 133 | if (d->dev_handle == dev_handle) {
|
|---|
| 134 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 135 | free(devcon);
|
|---|
| 136 | return EEXIST;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | list_append(&devcon->link, &dcl_head);
|
|---|
| 140 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static void devcon_remove(devcon_t *devcon)
|
|---|
| 145 | {
|
|---|
| 146 | fibril_mutex_lock(&dcl_lock);
|
|---|
| 147 | list_remove(&devcon->link);
|
|---|
| 148 | fibril_mutex_unlock(&dcl_lock);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | int block_init(dev_handle_t dev_handle, size_t comm_size)
|
|---|
| 152 | {
|
|---|
| 153 | int rc;
|
|---|
| 154 | int dev_phone;
|
|---|
| 155 | void *comm_area;
|
|---|
| 156 | size_t bsize;
|
|---|
| 157 |
|
|---|
| 158 | comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
|
|---|
| 159 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
|---|
| 160 | if (!comm_area) {
|
|---|
| 161 | return ENOMEM;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
|
|---|
| 165 | if (dev_phone < 0) {
|
|---|
| 166 | munmap(comm_area, comm_size);
|
|---|
| 167 | return dev_phone;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | rc = async_share_out_start(dev_phone, comm_area,
|
|---|
| 171 | AS_AREA_READ | AS_AREA_WRITE);
|
|---|
| 172 | if (rc != EOK) {
|
|---|
| 173 | munmap(comm_area, comm_size);
|
|---|
| 174 | ipc_hangup(dev_phone);
|
|---|
| 175 | return rc;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | if (get_block_size(dev_phone, &bsize) != EOK) {
|
|---|
| 179 | munmap(comm_area, comm_size);
|
|---|
| 180 | ipc_hangup(dev_phone);
|
|---|
| 181 | return rc;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | rc = devcon_add(dev_handle, dev_phone, bsize, comm_area, comm_size);
|
|---|
| 185 | if (rc != EOK) {
|
|---|
| 186 | munmap(comm_area, comm_size);
|
|---|
| 187 | ipc_hangup(dev_phone);
|
|---|
| 188 | return rc;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return EOK;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | void block_fini(dev_handle_t dev_handle)
|
|---|
| 195 | {
|
|---|
| 196 | devcon_t *devcon = devcon_search(dev_handle);
|
|---|
| 197 | assert(devcon);
|
|---|
| 198 |
|
|---|
| 199 | devcon_remove(devcon);
|
|---|
| 200 |
|
|---|
| 201 | if (devcon->bb_buf)
|
|---|
| 202 | free(devcon->bb_buf);
|
|---|
| 203 |
|
|---|
| 204 | if (devcon->cache) {
|
|---|
| 205 | hash_table_destroy(&devcon->cache->block_hash);
|
|---|
| 206 | free(devcon->cache);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | munmap(devcon->comm_area, devcon->comm_size);
|
|---|
| 210 | ipc_hangup(devcon->dev_phone);
|
|---|
| 211 |
|
|---|
| 212 | free(devcon);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | int block_bb_read(dev_handle_t dev_handle, bn_t ba)
|
|---|
| 216 | {
|
|---|
| 217 | void *bb_buf;
|
|---|
| 218 | int rc;
|
|---|
| 219 |
|
|---|
| 220 | devcon_t *devcon = devcon_search(dev_handle);
|
|---|
| 221 | if (!devcon)
|
|---|
| 222 | return ENOENT;
|
|---|
| 223 | if (devcon->bb_buf)
|
|---|
| 224 | return EEXIST;
|
|---|
| 225 | bb_buf = malloc(devcon->pblock_size);
|
|---|
| 226 | if (!bb_buf)
|
|---|
| 227 | return ENOMEM;
|
|---|
| 228 |
|
|---|
| 229 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 230 | rc = read_blocks(devcon, 0, 1);
|
|---|
| 231 | if (rc != EOK) {
|
|---|
| 232 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 233 | free(bb_buf);
|
|---|
| 234 | return rc;
|
|---|
| 235 | }
|
|---|
| 236 | memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
|
|---|
| 237 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 238 |
|
|---|
| 239 | devcon->bb_buf = bb_buf;
|
|---|
| 240 | devcon->bb_addr = ba;
|
|---|
| 241 |
|
|---|
| 242 | return EOK;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void *block_bb_get(dev_handle_t dev_handle)
|
|---|
| 246 | {
|
|---|
| 247 | devcon_t *devcon = devcon_search(dev_handle);
|
|---|
| 248 | assert(devcon);
|
|---|
| 249 | return devcon->bb_buf;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | static hash_index_t cache_hash(unsigned long *key)
|
|---|
| 253 | {
|
|---|
| 254 | return *key & (CACHE_BUCKETS - 1);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
|---|
| 258 | {
|
|---|
| 259 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
|---|
| 260 | return b->boff == *key;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | static void cache_remove_callback(link_t *item)
|
|---|
| 264 | {
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | static hash_table_operations_t cache_ops = {
|
|---|
| 268 | .hash = cache_hash,
|
|---|
| 269 | .compare = cache_compare,
|
|---|
| 270 | .remove_callback = cache_remove_callback
|
|---|
| 271 | };
|
|---|
| 272 |
|
|---|
| 273 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
|
|---|
| 274 | enum cache_mode mode)
|
|---|
| 275 | {
|
|---|
| 276 | devcon_t *devcon = devcon_search(dev_handle);
|
|---|
| 277 | cache_t *cache;
|
|---|
| 278 | if (!devcon)
|
|---|
| 279 | return ENOENT;
|
|---|
| 280 | if (devcon->cache)
|
|---|
| 281 | return EEXIST;
|
|---|
| 282 | cache = malloc(sizeof(cache_t));
|
|---|
| 283 | if (!cache)
|
|---|
| 284 | return ENOMEM;
|
|---|
| 285 |
|
|---|
| 286 | fibril_mutex_initialize(&cache->lock);
|
|---|
| 287 | list_initialize(&cache->free_head);
|
|---|
| 288 | cache->lblock_size = size;
|
|---|
| 289 | cache->block_count = blocks;
|
|---|
| 290 | cache->blocks_cached = 0;
|
|---|
| 291 | cache->mode = mode;
|
|---|
| 292 |
|
|---|
| 293 | /* No block size translation a.t.m. */
|
|---|
| 294 | assert(cache->lblock_size == devcon->pblock_size);
|
|---|
| 295 |
|
|---|
| 296 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
|
|---|
| 297 | &cache_ops)) {
|
|---|
| 298 | free(cache);
|
|---|
| 299 | return ENOMEM;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | devcon->cache = cache;
|
|---|
| 303 | return EOK;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | #define CACHE_LO_WATERMARK 10
|
|---|
| 307 | #define CACHE_HI_WATERMARK 20
|
|---|
| 308 | static bool cache_can_grow(cache_t *cache)
|
|---|
| 309 | {
|
|---|
| 310 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
|---|
| 311 | return true;
|
|---|
| 312 | if (!list_empty(&cache->free_head))
|
|---|
| 313 | return false;
|
|---|
| 314 | return true;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | static void block_initialize(block_t *b)
|
|---|
| 318 | {
|
|---|
| 319 | fibril_mutex_initialize(&b->lock);
|
|---|
| 320 | b->refcnt = 1;
|
|---|
| 321 | b->dirty = false;
|
|---|
| 322 | b->toxic = false;
|
|---|
| 323 | fibril_rwlock_initialize(&b->contents_lock);
|
|---|
| 324 | link_initialize(&b->free_link);
|
|---|
| 325 | link_initialize(&b->hash_link);
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /** Instantiate a block in memory and get a reference to it.
|
|---|
| 329 | *
|
|---|
| 330 | * @param block Pointer to where the function will store the
|
|---|
| 331 | * block pointer on success.
|
|---|
| 332 | * @param dev_handle Device handle of the block device.
|
|---|
| 333 | * @param boff Block offset.
|
|---|
| 334 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
|---|
| 335 | * will not read the contents of the block from the
|
|---|
| 336 | * device.
|
|---|
| 337 | *
|
|---|
| 338 | * @return EOK on success or a negative error code.
|
|---|
| 339 | */
|
|---|
| 340 | int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
|
|---|
| 341 | {
|
|---|
| 342 | devcon_t *devcon;
|
|---|
| 343 | cache_t *cache;
|
|---|
| 344 | block_t *b;
|
|---|
| 345 | link_t *l;
|
|---|
| 346 | unsigned long key = boff;
|
|---|
| 347 | int rc;
|
|---|
| 348 |
|
|---|
| 349 | devcon = devcon_search(dev_handle);
|
|---|
| 350 |
|
|---|
| 351 | assert(devcon);
|
|---|
| 352 | assert(devcon->cache);
|
|---|
| 353 |
|
|---|
| 354 | cache = devcon->cache;
|
|---|
| 355 |
|
|---|
| 356 | retry:
|
|---|
| 357 | rc = EOK;
|
|---|
| 358 | b = NULL;
|
|---|
| 359 |
|
|---|
| 360 | fibril_mutex_lock(&cache->lock);
|
|---|
| 361 | l = hash_table_find(&cache->block_hash, &key);
|
|---|
| 362 | if (l) {
|
|---|
| 363 | /*
|
|---|
| 364 | * We found the block in the cache.
|
|---|
| 365 | */
|
|---|
| 366 | b = hash_table_get_instance(l, block_t, hash_link);
|
|---|
| 367 | fibril_mutex_lock(&b->lock);
|
|---|
| 368 | if (b->refcnt++ == 0)
|
|---|
| 369 | list_remove(&b->free_link);
|
|---|
| 370 | if (b->toxic)
|
|---|
| 371 | rc = EIO;
|
|---|
| 372 | fibril_mutex_unlock(&b->lock);
|
|---|
| 373 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 374 | } else {
|
|---|
| 375 | /*
|
|---|
| 376 | * The block was not found in the cache.
|
|---|
| 377 | */
|
|---|
| 378 | if (cache_can_grow(cache)) {
|
|---|
| 379 | /*
|
|---|
| 380 | * We can grow the cache by allocating new blocks.
|
|---|
| 381 | * Should the allocation fail, we fail over and try to
|
|---|
| 382 | * recycle a block from the cache.
|
|---|
| 383 | */
|
|---|
| 384 | b = malloc(sizeof(block_t));
|
|---|
| 385 | if (!b)
|
|---|
| 386 | goto recycle;
|
|---|
| 387 | b->data = malloc(cache->lblock_size);
|
|---|
| 388 | if (!b->data) {
|
|---|
| 389 | free(b);
|
|---|
| 390 | goto recycle;
|
|---|
| 391 | }
|
|---|
| 392 | cache->blocks_cached++;
|
|---|
| 393 | } else {
|
|---|
| 394 | /*
|
|---|
| 395 | * Try to recycle a block from the free list.
|
|---|
| 396 | */
|
|---|
| 397 | unsigned long temp_key;
|
|---|
| 398 | recycle:
|
|---|
| 399 | if (list_empty(&cache->free_head)) {
|
|---|
| 400 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 401 | rc = ENOMEM;
|
|---|
| 402 | goto out;
|
|---|
| 403 | }
|
|---|
| 404 | l = cache->free_head.next;
|
|---|
| 405 | b = list_get_instance(l, block_t, free_link);
|
|---|
| 406 |
|
|---|
| 407 | fibril_mutex_lock(&b->lock);
|
|---|
| 408 | if (b->dirty) {
|
|---|
| 409 | /*
|
|---|
| 410 | * The block needs to be written back to the
|
|---|
| 411 | * device before it changes identity. Do this
|
|---|
| 412 | * while not holding the cache lock so that
|
|---|
| 413 | * concurrency is not impeded. Also move the
|
|---|
| 414 | * block to the end of the free list so that we
|
|---|
| 415 | * do not slow down other instances of
|
|---|
| 416 | * block_get() draining the free list.
|
|---|
| 417 | */
|
|---|
| 418 | list_remove(&b->free_link);
|
|---|
| 419 | list_append(&b->free_link, &cache->free_head);
|
|---|
| 420 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 421 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 422 | memcpy(devcon->comm_area, b->data, b->size);
|
|---|
| 423 | rc = write_blocks(devcon, b->boff, 1);
|
|---|
| 424 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 425 | if (rc != EOK) {
|
|---|
| 426 | /*
|
|---|
| 427 | * We did not manage to write the block
|
|---|
| 428 | * to the device. Keep it around for
|
|---|
| 429 | * another try. Hopefully, we will grab
|
|---|
| 430 | * another block next time.
|
|---|
| 431 | */
|
|---|
| 432 | fibril_mutex_unlock(&b->lock);
|
|---|
| 433 | goto retry;
|
|---|
| 434 | }
|
|---|
| 435 | b->dirty = false;
|
|---|
| 436 | if (!fibril_mutex_trylock(&cache->lock)) {
|
|---|
| 437 | /*
|
|---|
| 438 | * Somebody is probably racing with us.
|
|---|
| 439 | * Unlock the block and retry.
|
|---|
| 440 | */
|
|---|
| 441 | fibril_mutex_unlock(&b->lock);
|
|---|
| 442 | goto retry;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | }
|
|---|
| 446 | fibril_mutex_unlock(&b->lock);
|
|---|
| 447 |
|
|---|
| 448 | /*
|
|---|
| 449 | * Unlink the block from the free list and the hash
|
|---|
| 450 | * table.
|
|---|
| 451 | */
|
|---|
| 452 | list_remove(&b->free_link);
|
|---|
| 453 | temp_key = b->boff;
|
|---|
| 454 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | block_initialize(b);
|
|---|
| 458 | b->dev_handle = dev_handle;
|
|---|
| 459 | b->size = cache->lblock_size;
|
|---|
| 460 | b->boff = boff;
|
|---|
| 461 | hash_table_insert(&cache->block_hash, &key, &b->hash_link);
|
|---|
| 462 |
|
|---|
| 463 | /*
|
|---|
| 464 | * Lock the block before releasing the cache lock. Thus we don't
|
|---|
| 465 | * kill concurrent operations on the cache while doing I/O on
|
|---|
| 466 | * the block.
|
|---|
| 467 | */
|
|---|
| 468 | fibril_mutex_lock(&b->lock);
|
|---|
| 469 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 470 |
|
|---|
| 471 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
|---|
| 472 | /*
|
|---|
| 473 | * The block contains old or no data. We need to read
|
|---|
| 474 | * the new contents from the device.
|
|---|
| 475 | */
|
|---|
| 476 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 477 | rc = read_blocks(devcon, b->boff, 1);
|
|---|
| 478 | memcpy(b->data, devcon->comm_area, cache->lblock_size);
|
|---|
| 479 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 480 | if (rc != EOK)
|
|---|
| 481 | b->toxic = true;
|
|---|
| 482 | } else
|
|---|
| 483 | rc = EOK;
|
|---|
| 484 |
|
|---|
| 485 | fibril_mutex_unlock(&b->lock);
|
|---|
| 486 | }
|
|---|
| 487 | out:
|
|---|
| 488 | if ((rc != EOK) && b) {
|
|---|
| 489 | assert(b->toxic);
|
|---|
| 490 | (void) block_put(b);
|
|---|
| 491 | b = NULL;
|
|---|
| 492 | }
|
|---|
| 493 | *block = b;
|
|---|
| 494 | return rc;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /** Release a reference to a block.
|
|---|
| 498 | *
|
|---|
| 499 | * If the last reference is dropped, the block is put on the free list.
|
|---|
| 500 | *
|
|---|
| 501 | * @param block Block of which a reference is to be released.
|
|---|
| 502 | *
|
|---|
| 503 | * @return EOK on success or a negative error code.
|
|---|
| 504 | */
|
|---|
| 505 | int block_put(block_t *block)
|
|---|
| 506 | {
|
|---|
| 507 | devcon_t *devcon = devcon_search(block->dev_handle);
|
|---|
| 508 | cache_t *cache;
|
|---|
| 509 | unsigned blocks_cached;
|
|---|
| 510 | enum cache_mode mode;
|
|---|
| 511 | int rc = EOK;
|
|---|
| 512 |
|
|---|
| 513 | assert(devcon);
|
|---|
| 514 | assert(devcon->cache);
|
|---|
| 515 |
|
|---|
| 516 | cache = devcon->cache;
|
|---|
| 517 |
|
|---|
| 518 | retry:
|
|---|
| 519 | fibril_mutex_lock(&cache->lock);
|
|---|
| 520 | blocks_cached = cache->blocks_cached;
|
|---|
| 521 | mode = cache->mode;
|
|---|
| 522 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 523 |
|
|---|
| 524 | /*
|
|---|
| 525 | * Determine whether to sync the block. Syncing the block is best done
|
|---|
| 526 | * when not holding the cache lock as it does not impede concurrency.
|
|---|
| 527 | * Since the situation may have changed when we unlocked the cache, the
|
|---|
| 528 | * blocks_cached and mode variables are mere hints. We will recheck the
|
|---|
| 529 | * conditions later when the cache lock is held again.
|
|---|
| 530 | */
|
|---|
| 531 | fibril_mutex_lock(&block->lock);
|
|---|
| 532 | if (block->toxic)
|
|---|
| 533 | block->dirty = false; /* will not write back toxic block */
|
|---|
| 534 | if (block->dirty && (block->refcnt == 1) &&
|
|---|
| 535 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
|---|
| 536 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 537 | memcpy(devcon->comm_area, block->data, block->size);
|
|---|
| 538 | rc = write_blocks(devcon, block->boff, 1);
|
|---|
| 539 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 540 | block->dirty = false;
|
|---|
| 541 | }
|
|---|
| 542 | fibril_mutex_unlock(&block->lock);
|
|---|
| 543 |
|
|---|
| 544 | fibril_mutex_lock(&cache->lock);
|
|---|
| 545 | fibril_mutex_lock(&block->lock);
|
|---|
| 546 | if (!--block->refcnt) {
|
|---|
| 547 | /*
|
|---|
| 548 | * Last reference to the block was dropped. Either free the
|
|---|
| 549 | * block or put it on the free list. In case of an I/O error,
|
|---|
| 550 | * free the block.
|
|---|
| 551 | */
|
|---|
| 552 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
|---|
| 553 | (rc != EOK)) {
|
|---|
| 554 | /*
|
|---|
| 555 | * Currently there are too many cached blocks or there
|
|---|
| 556 | * was an I/O error when writing the block back to the
|
|---|
| 557 | * device.
|
|---|
| 558 | */
|
|---|
| 559 | if (block->dirty) {
|
|---|
| 560 | /*
|
|---|
| 561 | * We cannot sync the block while holding the
|
|---|
| 562 | * cache lock. Release everything and retry.
|
|---|
| 563 | */
|
|---|
| 564 | block->refcnt++;
|
|---|
| 565 | fibril_mutex_unlock(&block->lock);
|
|---|
| 566 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 567 | goto retry;
|
|---|
| 568 | }
|
|---|
| 569 | /*
|
|---|
| 570 | * Take the block out of the cache and free it.
|
|---|
| 571 | */
|
|---|
| 572 | unsigned long key = block->boff;
|
|---|
| 573 | hash_table_remove(&cache->block_hash, &key, 1);
|
|---|
| 574 | free(block);
|
|---|
| 575 | free(block->data);
|
|---|
| 576 | cache->blocks_cached--;
|
|---|
| 577 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 578 | return rc;
|
|---|
| 579 | }
|
|---|
| 580 | /*
|
|---|
| 581 | * Put the block on the free list.
|
|---|
| 582 | */
|
|---|
| 583 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
|---|
| 584 | /*
|
|---|
| 585 | * We cannot sync the block while holding the cache
|
|---|
| 586 | * lock. Release everything and retry.
|
|---|
| 587 | */
|
|---|
| 588 | block->refcnt++;
|
|---|
| 589 | fibril_mutex_unlock(&block->lock);
|
|---|
| 590 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 591 | goto retry;
|
|---|
| 592 | }
|
|---|
| 593 | list_append(&block->free_link, &cache->free_head);
|
|---|
| 594 | }
|
|---|
| 595 | fibril_mutex_unlock(&block->lock);
|
|---|
| 596 | fibril_mutex_unlock(&cache->lock);
|
|---|
| 597 |
|
|---|
| 598 | return rc;
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /** Read sequential data from a block device.
|
|---|
| 602 | *
|
|---|
| 603 | * @param dev_handle Device handle of the block device.
|
|---|
| 604 | * @param bufpos Pointer to the first unread valid offset within the
|
|---|
| 605 | * communication buffer.
|
|---|
| 606 | * @param buflen Pointer to the number of unread bytes that are ready in
|
|---|
| 607 | * the communication buffer.
|
|---|
| 608 | * @param pos Device position to be read.
|
|---|
| 609 | * @param dst Destination buffer.
|
|---|
| 610 | * @param size Size of the destination buffer.
|
|---|
| 611 | * @param block_size Block size to be used for the transfer.
|
|---|
| 612 | *
|
|---|
| 613 | * @return EOK on success or a negative return code on failure.
|
|---|
| 614 | */
|
|---|
| 615 | int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
|
|---|
| 616 | off_t *pos, void *dst, size_t size)
|
|---|
| 617 | {
|
|---|
| 618 | off_t offset = 0;
|
|---|
| 619 | size_t left = size;
|
|---|
| 620 | size_t block_size;
|
|---|
| 621 | devcon_t *devcon;
|
|---|
| 622 |
|
|---|
| 623 | devcon = devcon_search(dev_handle);
|
|---|
| 624 | assert(devcon);
|
|---|
| 625 | block_size = devcon->pblock_size;
|
|---|
| 626 |
|
|---|
| 627 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 628 | while (left > 0) {
|
|---|
| 629 | size_t rd;
|
|---|
| 630 |
|
|---|
| 631 | if (*bufpos + left < *buflen)
|
|---|
| 632 | rd = left;
|
|---|
| 633 | else
|
|---|
| 634 | rd = *buflen - *bufpos;
|
|---|
| 635 |
|
|---|
| 636 | if (rd > 0) {
|
|---|
| 637 | /*
|
|---|
| 638 | * Copy the contents of the communication buffer to the
|
|---|
| 639 | * destination buffer.
|
|---|
| 640 | */
|
|---|
| 641 | memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
|
|---|
| 642 | offset += rd;
|
|---|
| 643 | *bufpos += rd;
|
|---|
| 644 | *pos += rd;
|
|---|
| 645 | left -= rd;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | if (*bufpos == (off_t) *buflen) {
|
|---|
| 649 | /* Refill the communication buffer with a new block. */
|
|---|
| 650 | int rc;
|
|---|
| 651 |
|
|---|
| 652 | rc = read_blocks(devcon, *pos / block_size, 1);
|
|---|
| 653 | if (rc != EOK) {
|
|---|
| 654 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 655 | return rc;
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | *bufpos = 0;
|
|---|
| 659 | *buflen = block_size;
|
|---|
| 660 | }
|
|---|
| 661 | }
|
|---|
| 662 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 663 |
|
|---|
| 664 | return EOK;
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | /** Read blocks directly from device (bypass cache).
|
|---|
| 668 | *
|
|---|
| 669 | * @param dev_handle Device handle of the block device.
|
|---|
| 670 | * @param ba Address of first block.
|
|---|
| 671 | * @param cnt Number of blocks.
|
|---|
| 672 | * @param src Buffer for storing the data.
|
|---|
| 673 | *
|
|---|
| 674 | * @return EOK on success or negative error code on failure.
|
|---|
| 675 | */
|
|---|
| 676 | int block_read_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt, void *buf)
|
|---|
| 677 | {
|
|---|
| 678 | devcon_t *devcon;
|
|---|
| 679 | int rc;
|
|---|
| 680 |
|
|---|
| 681 | devcon = devcon_search(dev_handle);
|
|---|
| 682 | assert(devcon);
|
|---|
| 683 |
|
|---|
| 684 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 685 |
|
|---|
| 686 | rc = read_blocks(devcon, ba, cnt);
|
|---|
| 687 | if (rc == EOK)
|
|---|
| 688 | memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
|
|---|
| 689 |
|
|---|
| 690 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 691 |
|
|---|
| 692 | return rc;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | /** Write blocks directly to device (bypass cache).
|
|---|
| 696 | *
|
|---|
| 697 | * @param dev_handle Device handle of the block device.
|
|---|
| 698 | * @param ba Address of first block.
|
|---|
| 699 | * @param cnt Number of blocks.
|
|---|
| 700 | * @param src The data to be written.
|
|---|
| 701 | *
|
|---|
| 702 | * @return EOK on success or negative error code on failure.
|
|---|
| 703 | */
|
|---|
| 704 | int block_write_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt,
|
|---|
| 705 | const void *data)
|
|---|
| 706 | {
|
|---|
| 707 | devcon_t *devcon;
|
|---|
| 708 | int rc;
|
|---|
| 709 |
|
|---|
| 710 | devcon = devcon_search(dev_handle);
|
|---|
| 711 | assert(devcon);
|
|---|
| 712 |
|
|---|
| 713 | fibril_mutex_lock(&devcon->comm_area_lock);
|
|---|
| 714 |
|
|---|
| 715 | memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
|
|---|
| 716 | rc = read_blocks(devcon, ba, cnt);
|
|---|
| 717 |
|
|---|
| 718 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
|---|
| 719 |
|
|---|
| 720 | return rc;
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /** Get device block size.
|
|---|
| 724 | *
|
|---|
| 725 | * @param dev_handle Device handle of the block device.
|
|---|
| 726 | * @param bsize Output block size.
|
|---|
| 727 | *
|
|---|
| 728 | * @return EOK on success or negative error code on failure.
|
|---|
| 729 | */
|
|---|
| 730 | int block_get_bsize(dev_handle_t dev_handle, size_t *bsize)
|
|---|
| 731 | {
|
|---|
| 732 | devcon_t *devcon;
|
|---|
| 733 |
|
|---|
| 734 | devcon = devcon_search(dev_handle);
|
|---|
| 735 | assert(devcon);
|
|---|
| 736 |
|
|---|
| 737 | return get_block_size(devcon->dev_phone, bsize);
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | /** Read blocks from block device.
|
|---|
| 741 | *
|
|---|
| 742 | * @param devcon Device connection.
|
|---|
| 743 | * @param ba Address of first block.
|
|---|
| 744 | * @param cnt Number of blocks.
|
|---|
| 745 | * @param src Buffer for storing the data.
|
|---|
| 746 | *
|
|---|
| 747 | * @return EOK on success or negative error code on failure.
|
|---|
| 748 | */
|
|---|
| 749 | static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
|
|---|
| 750 | {
|
|---|
| 751 | int rc;
|
|---|
| 752 |
|
|---|
| 753 | assert(devcon);
|
|---|
| 754 | rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
|
|---|
| 755 | UPPER32(ba), cnt);
|
|---|
| 756 | return rc;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /** Write block to block device.
|
|---|
| 760 | *
|
|---|
| 761 | * @param devcon Device connection.
|
|---|
| 762 | * @param ba Address of first block.
|
|---|
| 763 | * @param cnt Number of blocks.
|
|---|
| 764 | * @param src Buffer containing the data to write.
|
|---|
| 765 | *
|
|---|
| 766 | * @return EOK on success or negative error code on failure.
|
|---|
| 767 | */
|
|---|
| 768 | static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
|
|---|
| 769 | {
|
|---|
| 770 | int rc;
|
|---|
| 771 |
|
|---|
| 772 | assert(devcon);
|
|---|
| 773 | rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
|
|---|
| 774 | UPPER32(ba), cnt);
|
|---|
| 775 | return rc;
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | /** Get block size used by the device. */
|
|---|
| 779 | static int get_block_size(int dev_phone, size_t *bsize)
|
|---|
| 780 | {
|
|---|
| 781 | ipcarg_t bs;
|
|---|
| 782 | int rc;
|
|---|
| 783 |
|
|---|
| 784 | rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs);
|
|---|
| 785 | if (rc == EOK)
|
|---|
| 786 | *bsize = (size_t) bs;
|
|---|
| 787 |
|
|---|
| 788 | return rc;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | /** @}
|
|---|
| 792 | */
|
|---|