[fc840d9] | 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 |
|
---|
[97c9da8] | 30 | /** @addtogroup libblock
|
---|
[fc840d9] | 31 | * @{
|
---|
[97c9da8] | 32 | */
|
---|
[fc840d9] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[97c9da8] | 38 | #include "libblock.h"
|
---|
[fc840d9] | 39 | #include "../../srv/vfs/vfs.h"
|
---|
[7858bc5f] | 40 | #include <ipc/devmap.h>
|
---|
[c5747fe] | 41 | #include <ipc/bd.h>
|
---|
[7858bc5f] | 42 | #include <ipc/services.h>
|
---|
[fc840d9] | 43 | #include <errno.h>
|
---|
[7858bc5f] | 44 | #include <sys/mman.h>
|
---|
[fc840d9] | 45 | #include <async.h>
|
---|
| 46 | #include <ipc/ipc.h>
|
---|
| 47 | #include <as.h>
|
---|
| 48 | #include <assert.h>
|
---|
[4e1b57d] | 49 | #include <fibril_sync.h>
|
---|
[d9c8c81] | 50 | #include <adt/list.h>
|
---|
| 51 | #include <adt/hash_table.h>
|
---|
[d00ae4c] | 52 | #include <mem.h>
|
---|
[fc840d9] | 53 |
|
---|
[916bf1a] | 54 | /** Lock protecting the device connection list */
|
---|
[4e1b57d] | 55 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
---|
[916bf1a] | 56 | /** Device connection list head. */
|
---|
| 57 | static LIST_INITIALIZE(dcl_head);
|
---|
| 58 |
|
---|
[f1ba5d6] | 59 | #define CACHE_BUCKETS_LOG2 10
|
---|
| 60 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
---|
| 61 |
|
---|
| 62 | typedef struct {
|
---|
[4e1b57d] | 63 | fibril_mutex_t lock;
|
---|
[f1ba5d6] | 64 | size_t block_size; /**< Block size. */
|
---|
| 65 | unsigned block_count; /**< Total number of blocks. */
|
---|
[d68e4d5] | 66 | unsigned blocks_cached; /**< Number of cached blocks. */
|
---|
[f1ba5d6] | 67 | hash_table_t block_hash;
|
---|
| 68 | link_t free_head;
|
---|
[1fbe064b] | 69 | enum cache_mode mode;
|
---|
[f1ba5d6] | 70 | } cache_t;
|
---|
| 71 |
|
---|
[916bf1a] | 72 | typedef struct {
|
---|
| 73 | link_t link;
|
---|
[ad8fc510] | 74 | dev_handle_t dev_handle;
|
---|
[916bf1a] | 75 | int dev_phone;
|
---|
[d68e4d5] | 76 | fibril_mutex_t com_area_lock;
|
---|
[916bf1a] | 77 | void *com_area;
|
---|
| 78 | size_t com_size;
|
---|
| 79 | void *bb_buf;
|
---|
| 80 | off_t bb_off;
|
---|
| 81 | size_t bb_size;
|
---|
[f1ba5d6] | 82 | cache_t *cache;
|
---|
[916bf1a] | 83 | } devcon_t;
|
---|
| 84 |
|
---|
[6408be3] | 85 | static int read_block(devcon_t *devcon, bn_t boff, size_t block_size);
|
---|
| 86 | static int write_block(devcon_t *devcon, bn_t boff, size_t block_size);
|
---|
[1fbe064b] | 87 |
|
---|
[916bf1a] | 88 | static devcon_t *devcon_search(dev_handle_t dev_handle)
|
---|
| 89 | {
|
---|
| 90 | link_t *cur;
|
---|
| 91 |
|
---|
[4e1b57d] | 92 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 93 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
| 94 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
---|
| 95 | if (devcon->dev_handle == dev_handle) {
|
---|
[4e1b57d] | 96 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 97 | return devcon;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[4e1b57d] | 100 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 101 | return NULL;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
|
---|
[6284978] | 105 | size_t com_size)
|
---|
[916bf1a] | 106 | {
|
---|
| 107 | link_t *cur;
|
---|
| 108 | devcon_t *devcon;
|
---|
| 109 |
|
---|
| 110 | devcon = malloc(sizeof(devcon_t));
|
---|
| 111 | if (!devcon)
|
---|
| 112 | return ENOMEM;
|
---|
| 113 |
|
---|
| 114 | link_initialize(&devcon->link);
|
---|
| 115 | devcon->dev_handle = dev_handle;
|
---|
| 116 | devcon->dev_phone = dev_phone;
|
---|
[d68e4d5] | 117 | fibril_mutex_initialize(&devcon->com_area_lock);
|
---|
[916bf1a] | 118 | devcon->com_area = com_area;
|
---|
| 119 | devcon->com_size = com_size;
|
---|
[6284978] | 120 | devcon->bb_buf = NULL;
|
---|
| 121 | devcon->bb_off = 0;
|
---|
| 122 | devcon->bb_size = 0;
|
---|
[f1ba5d6] | 123 | devcon->cache = NULL;
|
---|
[916bf1a] | 124 |
|
---|
[4e1b57d] | 125 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 126 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
| 127 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
---|
| 128 | if (d->dev_handle == dev_handle) {
|
---|
[4e1b57d] | 129 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 130 | free(devcon);
|
---|
| 131 | return EEXIST;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | list_append(&devcon->link, &dcl_head);
|
---|
[4e1b57d] | 135 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 136 | return EOK;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | static void devcon_remove(devcon_t *devcon)
|
---|
| 140 | {
|
---|
[4e1b57d] | 141 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 142 | list_remove(&devcon->link);
|
---|
[4e1b57d] | 143 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 144 | }
|
---|
[7858bc5f] | 145 |
|
---|
[6284978] | 146 | int block_init(dev_handle_t dev_handle, size_t com_size)
|
---|
[7858bc5f] | 147 | {
|
---|
| 148 | int rc;
|
---|
[916bf1a] | 149 | int dev_phone;
|
---|
| 150 | void *com_area;
|
---|
| 151 |
|
---|
| 152 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
|
---|
[7858bc5f] | 153 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
---|
[916bf1a] | 154 | if (!com_area) {
|
---|
[7858bc5f] | 155 | return ENOMEM;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1090b8c] | 158 | dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
|
---|
[7858bc5f] | 159 | if (dev_phone < 0) {
|
---|
[916bf1a] | 160 | munmap(com_area, com_size);
|
---|
[7858bc5f] | 161 | return dev_phone;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[916bf1a] | 164 | rc = ipc_share_out_start(dev_phone, com_area,
|
---|
[7858bc5f] | 165 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
| 166 | if (rc != EOK) {
|
---|
[916bf1a] | 167 | munmap(com_area, com_size);
|
---|
[7858bc5f] | 168 | ipc_hangup(dev_phone);
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
[916bf1a] | 171 |
|
---|
[6284978] | 172 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
|
---|
[916bf1a] | 173 | if (rc != EOK) {
|
---|
| 174 | munmap(com_area, com_size);
|
---|
| 175 | ipc_hangup(dev_phone);
|
---|
| 176 | return rc;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[7858bc5f] | 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void block_fini(dev_handle_t dev_handle)
|
---|
| 183 | {
|
---|
[916bf1a] | 184 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 185 | assert(devcon);
|
---|
| 186 |
|
---|
| 187 | devcon_remove(devcon);
|
---|
| 188 |
|
---|
[6284978] | 189 | if (devcon->bb_buf)
|
---|
| 190 | free(devcon->bb_buf);
|
---|
[f1ba5d6] | 191 |
|
---|
| 192 | if (devcon->cache) {
|
---|
| 193 | hash_table_destroy(&devcon->cache->block_hash);
|
---|
| 194 | free(devcon->cache);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[916bf1a] | 197 | munmap(devcon->com_area, devcon->com_size);
|
---|
| 198 | ipc_hangup(devcon->dev_phone);
|
---|
| 199 |
|
---|
| 200 | free(devcon);
|
---|
[7858bc5f] | 201 | }
|
---|
| 202 |
|
---|
[6284978] | 203 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
|
---|
| 204 | {
|
---|
| 205 | void *bb_buf;
|
---|
[0c243b4] | 206 | int rc;
|
---|
[6284978] | 207 |
|
---|
| 208 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 209 | if (!devcon)
|
---|
| 210 | return ENOENT;
|
---|
| 211 | if (devcon->bb_buf)
|
---|
| 212 | return EEXIST;
|
---|
| 213 | bb_buf = malloc(size);
|
---|
| 214 | if (!bb_buf)
|
---|
| 215 | return ENOMEM;
|
---|
| 216 |
|
---|
[d68e4d5] | 217 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
[6408be3] | 218 | rc = read_block(devcon, 0, size);
|
---|
[0c243b4] | 219 | if (rc != EOK) {
|
---|
[d68e4d5] | 220 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
[6284978] | 221 | free(bb_buf);
|
---|
[0c243b4] | 222 | return rc;
|
---|
[6284978] | 223 | }
|
---|
[6408be3] | 224 | memcpy(bb_buf, devcon->com_area, size);
|
---|
[d68e4d5] | 225 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
[6408be3] | 226 |
|
---|
[6284978] | 227 | devcon->bb_buf = bb_buf;
|
---|
| 228 | devcon->bb_off = off;
|
---|
| 229 | devcon->bb_size = size;
|
---|
| 230 |
|
---|
| 231 | return EOK;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[7858bc5f] | 234 | void *block_bb_get(dev_handle_t dev_handle)
|
---|
| 235 | {
|
---|
[916bf1a] | 236 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 237 | assert(devcon);
|
---|
| 238 | return devcon->bb_buf;
|
---|
[7858bc5f] | 239 | }
|
---|
| 240 |
|
---|
[f1ba5d6] | 241 | static hash_index_t cache_hash(unsigned long *key)
|
---|
| 242 | {
|
---|
| 243 | return *key & (CACHE_BUCKETS - 1);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
---|
| 247 | {
|
---|
| 248 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
---|
| 249 | return b->boff == *key;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | static void cache_remove_callback(link_t *item)
|
---|
| 253 | {
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | static hash_table_operations_t cache_ops = {
|
---|
| 257 | .hash = cache_hash,
|
---|
| 258 | .compare = cache_compare,
|
---|
| 259 | .remove_callback = cache_remove_callback
|
---|
| 260 | };
|
---|
| 261 |
|
---|
[1fbe064b] | 262 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
|
---|
| 263 | enum cache_mode mode)
|
---|
[f1ba5d6] | 264 | {
|
---|
| 265 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 266 | cache_t *cache;
|
---|
| 267 | if (!devcon)
|
---|
| 268 | return ENOENT;
|
---|
| 269 | if (devcon->cache)
|
---|
| 270 | return EEXIST;
|
---|
| 271 | cache = malloc(sizeof(cache_t));
|
---|
| 272 | if (!cache)
|
---|
| 273 | return ENOMEM;
|
---|
| 274 |
|
---|
[4e1b57d] | 275 | fibril_mutex_initialize(&cache->lock);
|
---|
[f1ba5d6] | 276 | list_initialize(&cache->free_head);
|
---|
| 277 | cache->block_size = size;
|
---|
| 278 | cache->block_count = blocks;
|
---|
[d68e4d5] | 279 | cache->blocks_cached = 0;
|
---|
[1fbe064b] | 280 | cache->mode = mode;
|
---|
[f1ba5d6] | 281 |
|
---|
| 282 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
|
---|
| 283 | &cache_ops)) {
|
---|
| 284 | free(cache);
|
---|
| 285 | return ENOMEM;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | devcon->cache = cache;
|
---|
| 289 | return EOK;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[d68e4d5] | 292 | #define CACHE_LO_WATERMARK 10
|
---|
| 293 | #define CACHE_HI_WATERMARK 20
|
---|
[e1c88d5] | 294 | static bool cache_can_grow(cache_t *cache)
|
---|
[fc840d9] | 295 | {
|
---|
[d68e4d5] | 296 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
---|
| 297 | return true;
|
---|
| 298 | if (!list_empty(&cache->free_head))
|
---|
| 299 | return false;
|
---|
[e1c88d5] | 300 | return true;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | static void block_initialize(block_t *b)
|
---|
| 304 | {
|
---|
[4e1b57d] | 305 | fibril_mutex_initialize(&b->lock);
|
---|
[e1c88d5] | 306 | b->refcnt = 1;
|
---|
| 307 | b->dirty = false;
|
---|
[cd688d9] | 308 | b->toxic = false;
|
---|
[4e1b57d] | 309 | fibril_rwlock_initialize(&b->contents_lock);
|
---|
[e1c88d5] | 310 | link_initialize(&b->free_link);
|
---|
| 311 | link_initialize(&b->hash_link);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /** Instantiate a block in memory and get a reference to it.
|
---|
| 315 | *
|
---|
[c91f2d1b] | 316 | * @param block Pointer to where the function will store the
|
---|
| 317 | * block pointer on success.
|
---|
[e1c88d5] | 318 | * @param dev_handle Device handle of the block device.
|
---|
| 319 | * @param boff Block offset.
|
---|
[1d8cdb1] | 320 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
| 321 | * will not read the contents of the block from the
|
---|
| 322 | * device.
|
---|
[e1c88d5] | 323 | *
|
---|
[c91f2d1b] | 324 | * @return EOK on success or a negative error code.
|
---|
[e1c88d5] | 325 | */
|
---|
[c91f2d1b] | 326 | int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
|
---|
[e1c88d5] | 327 | {
|
---|
| 328 | devcon_t *devcon;
|
---|
| 329 | cache_t *cache;
|
---|
[fc840d9] | 330 | block_t *b;
|
---|
[e1c88d5] | 331 | link_t *l;
|
---|
| 332 | unsigned long key = boff;
|
---|
| 333 |
|
---|
| 334 | devcon = devcon_search(dev_handle);
|
---|
[fc840d9] | 335 |
|
---|
[e1c88d5] | 336 | assert(devcon);
|
---|
| 337 | assert(devcon->cache);
|
---|
[fc840d9] | 338 |
|
---|
[e1c88d5] | 339 | cache = devcon->cache;
|
---|
[02ee6bf5] | 340 |
|
---|
| 341 | retry:
|
---|
[4e1b57d] | 342 | fibril_mutex_lock(&cache->lock);
|
---|
[e1c88d5] | 343 | l = hash_table_find(&cache->block_hash, &key);
|
---|
| 344 | if (l) {
|
---|
| 345 | /*
|
---|
| 346 | * We found the block in the cache.
|
---|
| 347 | */
|
---|
| 348 | b = hash_table_get_instance(l, block_t, hash_link);
|
---|
[4e1b57d] | 349 | fibril_mutex_lock(&b->lock);
|
---|
[e1c88d5] | 350 | if (b->refcnt++ == 0)
|
---|
| 351 | list_remove(&b->free_link);
|
---|
[4e1b57d] | 352 | fibril_mutex_unlock(&b->lock);
|
---|
| 353 | fibril_mutex_unlock(&cache->lock);
|
---|
[e1c88d5] | 354 | } else {
|
---|
| 355 | /*
|
---|
| 356 | * The block was not found in the cache.
|
---|
| 357 | */
|
---|
| 358 | int rc;
|
---|
| 359 |
|
---|
| 360 | if (cache_can_grow(cache)) {
|
---|
| 361 | /*
|
---|
| 362 | * We can grow the cache by allocating new blocks.
|
---|
| 363 | * Should the allocation fail, we fail over and try to
|
---|
| 364 | * recycle a block from the cache.
|
---|
| 365 | */
|
---|
| 366 | b = malloc(sizeof(block_t));
|
---|
| 367 | if (!b)
|
---|
| 368 | goto recycle;
|
---|
| 369 | b->data = malloc(cache->block_size);
|
---|
| 370 | if (!b->data) {
|
---|
| 371 | free(b);
|
---|
| 372 | goto recycle;
|
---|
| 373 | }
|
---|
[d68e4d5] | 374 | cache->blocks_cached++;
|
---|
[e1c88d5] | 375 | } else {
|
---|
| 376 | /*
|
---|
| 377 | * Try to recycle a block from the free list.
|
---|
| 378 | */
|
---|
| 379 | unsigned long temp_key;
|
---|
| 380 | recycle:
|
---|
| 381 | assert(!list_empty(&cache->free_head));
|
---|
| 382 | l = cache->free_head.next;
|
---|
[d68e4d5] | 383 | b = list_get_instance(l, block_t, free_link);
|
---|
[02ee6bf5] | 384 |
|
---|
| 385 | fibril_mutex_lock(&b->lock);
|
---|
| 386 | if (b->dirty) {
|
---|
| 387 | /*
|
---|
| 388 | * The block needs to be written back to the
|
---|
| 389 | * device before it changes identity. Do this
|
---|
| 390 | * while not holding the cache lock so that
|
---|
| 391 | * concurrency is not impeded. Also move the
|
---|
| 392 | * block to the end of the free list so that we
|
---|
| 393 | * do not slow down other instances of
|
---|
| 394 | * block_get() draining the free list.
|
---|
| 395 | */
|
---|
| 396 | list_remove(&b->free_link);
|
---|
| 397 | list_append(&b->free_link, &cache->free_head);
|
---|
| 398 | fibril_mutex_unlock(&cache->lock);
|
---|
| 399 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
| 400 | memcpy(devcon->com_area, b->data, b->size);
|
---|
| 401 | rc = write_block(devcon, b->boff,
|
---|
| 402 | cache->block_size);
|
---|
| 403 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
| 404 | assert(rc == EOK);
|
---|
| 405 | b->dirty = false;
|
---|
| 406 | if (!fibril_mutex_trylock(&cache->lock)) {
|
---|
| 407 | /*
|
---|
| 408 | * Somebody is probably racing with us.
|
---|
| 409 | * Unlock the block and retry.
|
---|
| 410 | */
|
---|
| 411 | fibril_mutex_unlock(&b->lock);
|
---|
| 412 | goto retry;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | }
|
---|
| 416 | fibril_mutex_unlock(&b->lock);
|
---|
| 417 |
|
---|
| 418 | /*
|
---|
| 419 | * Unlink the block from the free list and the hash
|
---|
| 420 | * table.
|
---|
| 421 | */
|
---|
| 422 | list_remove(&b->free_link);
|
---|
[e1c88d5] | 423 | temp_key = b->boff;
|
---|
| 424 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
---|
| 425 | }
|
---|
[fc840d9] | 426 |
|
---|
[e1c88d5] | 427 | block_initialize(b);
|
---|
| 428 | b->dev_handle = dev_handle;
|
---|
| 429 | b->size = cache->block_size;
|
---|
| 430 | b->boff = boff;
|
---|
[a6d97fb9] | 431 | hash_table_insert(&cache->block_hash, &key, &b->hash_link);
|
---|
| 432 |
|
---|
| 433 | /*
|
---|
| 434 | * Lock the block before releasing the cache lock. Thus we don't
|
---|
[5ac8918] | 435 | * kill concurrent operations on the cache while doing I/O on
|
---|
| 436 | * the block.
|
---|
[a6d97fb9] | 437 | */
|
---|
[4e1b57d] | 438 | fibril_mutex_lock(&b->lock);
|
---|
| 439 | fibril_mutex_unlock(&cache->lock);
|
---|
[a6d97fb9] | 440 |
|
---|
[1d8cdb1] | 441 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
---|
| 442 | /*
|
---|
| 443 | * The block contains old or no data. We need to read
|
---|
| 444 | * the new contents from the device.
|
---|
| 445 | */
|
---|
[d68e4d5] | 446 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
[6408be3] | 447 | rc = read_block(devcon, b->boff, cache->block_size);
|
---|
[1d8cdb1] | 448 | assert(rc == EOK);
|
---|
[6408be3] | 449 | memcpy(b->data, devcon->com_area, cache->block_size);
|
---|
[d68e4d5] | 450 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
[1d8cdb1] | 451 | }
|
---|
[fc840d9] | 452 |
|
---|
[4e1b57d] | 453 | fibril_mutex_unlock(&b->lock);
|
---|
[a6d97fb9] | 454 | }
|
---|
[c91f2d1b] | 455 | *block = b;
|
---|
| 456 | return EOK;
|
---|
[fc840d9] | 457 | }
|
---|
| 458 |
|
---|
[d5a720cf] | 459 | /** Release a reference to a block.
|
---|
| 460 | *
|
---|
[a6d97fb9] | 461 | * If the last reference is dropped, the block is put on the free list.
|
---|
[d5a720cf] | 462 | *
|
---|
| 463 | * @param block Block of which a reference is to be released.
|
---|
[c91f2d1b] | 464 | *
|
---|
| 465 | * @return EOK on success or a negative error code.
|
---|
[d5a720cf] | 466 | */
|
---|
[c91f2d1b] | 467 | int block_put(block_t *block)
|
---|
[fc840d9] | 468 | {
|
---|
[d5a720cf] | 469 | devcon_t *devcon = devcon_search(block->dev_handle);
|
---|
| 470 | cache_t *cache;
|
---|
[ddfc39a3] | 471 | unsigned blocks_cached;
|
---|
| 472 | enum cache_mode mode;
|
---|
[1fbe064b] | 473 | int rc;
|
---|
[d5a720cf] | 474 |
|
---|
| 475 | assert(devcon);
|
---|
| 476 | assert(devcon->cache);
|
---|
| 477 |
|
---|
| 478 | cache = devcon->cache;
|
---|
[ddfc39a3] | 479 |
|
---|
| 480 | retry:
|
---|
| 481 | fibril_mutex_lock(&cache->lock);
|
---|
| 482 | blocks_cached = cache->blocks_cached;
|
---|
| 483 | mode = cache->mode;
|
---|
| 484 | fibril_mutex_unlock(&cache->lock);
|
---|
| 485 |
|
---|
| 486 | /*
|
---|
| 487 | * Determine whether to sync the block. Syncing the block is best done
|
---|
| 488 | * when not holding the cache lock as it does not impede concurrency.
|
---|
| 489 | * Since the situation may have changed when we unlocked the cache, the
|
---|
| 490 | * blocks_cached and mode variables are mere hints. We will recheck the
|
---|
| 491 | * conditions later when the cache lock is held again.
|
---|
| 492 | */
|
---|
| 493 | fibril_mutex_lock(&block->lock);
|
---|
| 494 | if (block->dirty && (block->refcnt == 1) &&
|
---|
| 495 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
---|
| 496 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
| 497 | memcpy(devcon->com_area, block->data, block->size);
|
---|
| 498 | rc = write_block(devcon, block->boff, block->size);
|
---|
| 499 | assert(rc == EOK);
|
---|
| 500 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
| 501 | block->dirty = false;
|
---|
| 502 | }
|
---|
| 503 | fibril_mutex_unlock(&block->lock);
|
---|
| 504 |
|
---|
[4e1b57d] | 505 | fibril_mutex_lock(&cache->lock);
|
---|
| 506 | fibril_mutex_lock(&block->lock);
|
---|
[d5a720cf] | 507 | if (!--block->refcnt) {
|
---|
| 508 | /*
|
---|
[d68e4d5] | 509 | * Last reference to the block was dropped. Either free the
|
---|
| 510 | * block or put it on the free list.
|
---|
| 511 | */
|
---|
| 512 | if (cache->blocks_cached > CACHE_HI_WATERMARK) {
|
---|
| 513 | /*
|
---|
| 514 | * Currently there are too many cached blocks.
|
---|
| 515 | */
|
---|
| 516 | if (block->dirty) {
|
---|
[ddfc39a3] | 517 | /*
|
---|
| 518 | * We cannot sync the block while holding the
|
---|
| 519 | * cache lock. Release everything and retry.
|
---|
| 520 | */
|
---|
| 521 | block->refcnt++;
|
---|
| 522 | fibril_mutex_unlock(&block->lock);
|
---|
| 523 | fibril_mutex_unlock(&cache->lock);
|
---|
| 524 | goto retry;
|
---|
[d68e4d5] | 525 | }
|
---|
| 526 | /*
|
---|
| 527 | * Take the block out of the cache and free it.
|
---|
| 528 | */
|
---|
| 529 | unsigned long key = block->boff;
|
---|
| 530 | hash_table_remove(&cache->block_hash, &key, 1);
|
---|
| 531 | free(block);
|
---|
| 532 | free(block->data);
|
---|
| 533 | cache->blocks_cached--;
|
---|
| 534 | fibril_mutex_unlock(&cache->lock);
|
---|
| 535 | return;
|
---|
| 536 | }
|
---|
| 537 | /*
|
---|
| 538 | * Put the block on the free list.
|
---|
[d5a720cf] | 539 | */
|
---|
[1fbe064b] | 540 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
---|
[ddfc39a3] | 541 | /*
|
---|
| 542 | * We cannot sync the block while holding the cache
|
---|
| 543 | * lock. Release everything and retry.
|
---|
| 544 | */
|
---|
| 545 | block->refcnt++;
|
---|
| 546 | fibril_mutex_unlock(&block->lock);
|
---|
| 547 | fibril_mutex_unlock(&cache->lock);
|
---|
| 548 | goto retry;
|
---|
[1fbe064b] | 549 | }
|
---|
[ddfc39a3] | 550 | list_append(&block->free_link, &cache->free_head);
|
---|
[d5a720cf] | 551 | }
|
---|
[4e1b57d] | 552 | fibril_mutex_unlock(&block->lock);
|
---|
| 553 | fibril_mutex_unlock(&cache->lock);
|
---|
[c91f2d1b] | 554 |
|
---|
| 555 | return EOK;
|
---|
[d5a720cf] | 556 | }
|
---|
| 557 |
|
---|
[6408be3] | 558 | /** Read sequential data from a block device.
|
---|
[d5a720cf] | 559 | *
|
---|
| 560 | * @param dev_handle Device handle of the block device.
|
---|
| 561 | * @param bufpos Pointer to the first unread valid offset within the
|
---|
| 562 | * communication buffer.
|
---|
| 563 | * @param buflen Pointer to the number of unread bytes that are ready in
|
---|
| 564 | * the communication buffer.
|
---|
| 565 | * @param pos Device position to be read.
|
---|
| 566 | * @param dst Destination buffer.
|
---|
| 567 | * @param size Size of the destination buffer.
|
---|
| 568 | * @param block_size Block size to be used for the transfer.
|
---|
| 569 | *
|
---|
| 570 | * @return EOK on success or a negative return code on failure.
|
---|
| 571 | */
|
---|
[6408be3] | 572 | int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
|
---|
| 573 | off_t *pos, void *dst, size_t size, size_t block_size)
|
---|
[d5a720cf] | 574 | {
|
---|
| 575 | off_t offset = 0;
|
---|
| 576 | size_t left = size;
|
---|
| 577 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 578 | assert(devcon);
|
---|
[e1c88d5] | 579 |
|
---|
[d68e4d5] | 580 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
[d5a720cf] | 581 | while (left > 0) {
|
---|
| 582 | size_t rd;
|
---|
| 583 |
|
---|
| 584 | if (*bufpos + left < *buflen)
|
---|
| 585 | rd = left;
|
---|
| 586 | else
|
---|
| 587 | rd = *buflen - *bufpos;
|
---|
| 588 |
|
---|
| 589 | if (rd > 0) {
|
---|
| 590 | /*
|
---|
| 591 | * Copy the contents of the communication buffer to the
|
---|
| 592 | * destination buffer.
|
---|
| 593 | */
|
---|
| 594 | memcpy(dst + offset, devcon->com_area + *bufpos, rd);
|
---|
| 595 | offset += rd;
|
---|
| 596 | *bufpos += rd;
|
---|
| 597 | *pos += rd;
|
---|
| 598 | left -= rd;
|
---|
| 599 | }
|
---|
| 600 |
|
---|
[62140db] | 601 | if (*bufpos == (off_t) *buflen) {
|
---|
[d5a720cf] | 602 | /* Refill the communication buffer with a new block. */
|
---|
[6408be3] | 603 | int rc;
|
---|
| 604 |
|
---|
| 605 | rc = read_block(devcon, *pos / block_size, block_size);
|
---|
[d68e4d5] | 606 | if (rc != EOK) {
|
---|
| 607 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
[6408be3] | 608 | return rc;
|
---|
[d68e4d5] | 609 | }
|
---|
[d5a720cf] | 610 |
|
---|
| 611 | *bufpos = 0;
|
---|
| 612 | *buflen = block_size;
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
[d68e4d5] | 615 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
[d5a720cf] | 616 |
|
---|
| 617 | return EOK;
|
---|
[fc840d9] | 618 | }
|
---|
| 619 |
|
---|
[6408be3] | 620 | /** Read block from block device.
|
---|
| 621 | *
|
---|
| 622 | * @param devcon Device connection.
|
---|
| 623 | * @param boff Block index.
|
---|
| 624 | * @param block_size Block size.
|
---|
| 625 | * @param src Buffer for storing the data.
|
---|
| 626 | *
|
---|
| 627 | * @return EOK on success or negative error code on failure.
|
---|
| 628 | */
|
---|
| 629 | static int read_block(devcon_t *devcon, bn_t boff, size_t block_size)
|
---|
| 630 | {
|
---|
| 631 | ipcarg_t retval;
|
---|
| 632 | int rc;
|
---|
| 633 |
|
---|
| 634 | assert(devcon);
|
---|
| 635 | rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size,
|
---|
| 636 | &retval);
|
---|
| 637 | if ((rc != EOK) || (retval != EOK))
|
---|
| 638 | return (rc != EOK ? rc : (int) retval);
|
---|
| 639 |
|
---|
| 640 | return EOK;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[1fbe064b] | 643 | /** Write block to block device.
|
---|
| 644 | *
|
---|
| 645 | * @param devcon Device connection.
|
---|
| 646 | * @param boff Block index.
|
---|
| 647 | * @param block_size Block size.
|
---|
| 648 | * @param src Buffer containing the data to write.
|
---|
| 649 | *
|
---|
| 650 | * @return EOK on success or negative error code on failure.
|
---|
| 651 | */
|
---|
[6408be3] | 652 | static int write_block(devcon_t *devcon, bn_t boff, size_t block_size)
|
---|
[1fbe064b] | 653 | {
|
---|
| 654 | ipcarg_t retval;
|
---|
| 655 | int rc;
|
---|
| 656 |
|
---|
| 657 | assert(devcon);
|
---|
[6408be3] | 658 | rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size,
|
---|
| 659 | &retval);
|
---|
[1fbe064b] | 660 | if ((rc != EOK) || (retval != EOK))
|
---|
| 661 | return (rc != EOK ? rc : (int) retval);
|
---|
| 662 |
|
---|
| 663 | return EOK;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
[fc840d9] | 666 | /** @}
|
---|
| 667 | */
|
---|