[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>
|
---|
[1ee00b7] | 52 | #include <macros.h>
|
---|
[d00ae4c] | 53 | #include <mem.h>
|
---|
[fc840d9] | 54 |
|
---|
[916bf1a] | 55 | /** Lock protecting the device connection list */
|
---|
[4e1b57d] | 56 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
---|
[916bf1a] | 57 | /** Device connection list head. */
|
---|
| 58 | static LIST_INITIALIZE(dcl_head);
|
---|
| 59 |
|
---|
[f1ba5d6] | 60 | #define CACHE_BUCKETS_LOG2 10
|
---|
| 61 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
---|
| 62 |
|
---|
| 63 | typedef struct {
|
---|
[4e1b57d] | 64 | fibril_mutex_t lock;
|
---|
[1ee00b7] | 65 | size_t lblock_size; /**< Logical block size. */
|
---|
[f1ba5d6] | 66 | unsigned block_count; /**< Total number of blocks. */
|
---|
[d68e4d5] | 67 | unsigned blocks_cached; /**< Number of cached blocks. */
|
---|
[f1ba5d6] | 68 | hash_table_t block_hash;
|
---|
| 69 | link_t free_head;
|
---|
[1fbe064b] | 70 | enum cache_mode mode;
|
---|
[f1ba5d6] | 71 | } cache_t;
|
---|
| 72 |
|
---|
[916bf1a] | 73 | typedef struct {
|
---|
| 74 | link_t link;
|
---|
[ad8fc510] | 75 | dev_handle_t dev_handle;
|
---|
[916bf1a] | 76 | int dev_phone;
|
---|
[a830611] | 77 | fibril_mutex_t comm_area_lock;
|
---|
| 78 | void *comm_area;
|
---|
| 79 | size_t comm_size;
|
---|
[916bf1a] | 80 | void *bb_buf;
|
---|
[1ee00b7] | 81 | bn_t bb_addr;
|
---|
| 82 | size_t pblock_size; /**< Physical block size. */
|
---|
[f1ba5d6] | 83 | cache_t *cache;
|
---|
[916bf1a] | 84 | } devcon_t;
|
---|
| 85 |
|
---|
[1ee00b7] | 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);
|
---|
[00b1d20e] | 88 | static int get_block_size(int dev_phone, size_t *bsize);
|
---|
[1fbe064b] | 89 |
|
---|
[916bf1a] | 90 | static devcon_t *devcon_search(dev_handle_t dev_handle)
|
---|
| 91 | {
|
---|
| 92 | link_t *cur;
|
---|
| 93 |
|
---|
[4e1b57d] | 94 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 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) {
|
---|
[4e1b57d] | 98 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 99 | return devcon;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[4e1b57d] | 102 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 103 | return NULL;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[1ee00b7] | 106 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, size_t bsize,
|
---|
[a830611] | 107 | void *comm_area, size_t comm_size)
|
---|
[916bf1a] | 108 | {
|
---|
| 109 | link_t *cur;
|
---|
| 110 | devcon_t *devcon;
|
---|
| 111 |
|
---|
[a830611] | 112 | if (comm_size < bsize)
|
---|
[1ee00b7] | 113 | return EINVAL;
|
---|
| 114 |
|
---|
[916bf1a] | 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;
|
---|
[a830611] | 122 | fibril_mutex_initialize(&devcon->comm_area_lock);
|
---|
| 123 | devcon->comm_area = comm_area;
|
---|
| 124 | devcon->comm_size = comm_size;
|
---|
[6284978] | 125 | devcon->bb_buf = NULL;
|
---|
[1ee00b7] | 126 | devcon->bb_addr = 0;
|
---|
| 127 | devcon->pblock_size = bsize;
|
---|
[f1ba5d6] | 128 | devcon->cache = NULL;
|
---|
[916bf1a] | 129 |
|
---|
[4e1b57d] | 130 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 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) {
|
---|
[4e1b57d] | 134 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 135 | free(devcon);
|
---|
| 136 | return EEXIST;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | list_append(&devcon->link, &dcl_head);
|
---|
[4e1b57d] | 140 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 141 | return EOK;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static void devcon_remove(devcon_t *devcon)
|
---|
| 145 | {
|
---|
[4e1b57d] | 146 | fibril_mutex_lock(&dcl_lock);
|
---|
[916bf1a] | 147 | list_remove(&devcon->link);
|
---|
[4e1b57d] | 148 | fibril_mutex_unlock(&dcl_lock);
|
---|
[916bf1a] | 149 | }
|
---|
[7858bc5f] | 150 |
|
---|
[a830611] | 151 | int block_init(dev_handle_t dev_handle, size_t comm_size)
|
---|
[7858bc5f] | 152 | {
|
---|
| 153 | int rc;
|
---|
[916bf1a] | 154 | int dev_phone;
|
---|
[a830611] | 155 | void *comm_area;
|
---|
[1ee00b7] | 156 | size_t bsize;
|
---|
| 157 |
|
---|
[a830611] | 158 | comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
|
---|
[7858bc5f] | 159 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
---|
[a830611] | 160 | if (!comm_area) {
|
---|
[7858bc5f] | 161 | return ENOMEM;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[1090b8c] | 164 | dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
|
---|
[7858bc5f] | 165 | if (dev_phone < 0) {
|
---|
[a830611] | 166 | munmap(comm_area, comm_size);
|
---|
[7858bc5f] | 167 | return dev_phone;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[a830611] | 170 | rc = ipc_share_out_start(dev_phone, comm_area,
|
---|
[7858bc5f] | 171 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
| 172 | if (rc != EOK) {
|
---|
[a830611] | 173 | munmap(comm_area, comm_size);
|
---|
[7858bc5f] | 174 | ipc_hangup(dev_phone);
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
[1ee00b7] | 177 |
|
---|
| 178 | if (get_block_size(dev_phone, &bsize) != EOK) {
|
---|
[a830611] | 179 | munmap(comm_area, comm_size);
|
---|
[1ee00b7] | 180 | ipc_hangup(dev_phone);
|
---|
| 181 | return rc;
|
---|
| 182 | }
|
---|
[916bf1a] | 183 |
|
---|
[a830611] | 184 | rc = devcon_add(dev_handle, dev_phone, bsize, comm_area, comm_size);
|
---|
[916bf1a] | 185 | if (rc != EOK) {
|
---|
[a830611] | 186 | munmap(comm_area, comm_size);
|
---|
[916bf1a] | 187 | ipc_hangup(dev_phone);
|
---|
| 188 | return rc;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[7858bc5f] | 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void block_fini(dev_handle_t dev_handle)
|
---|
| 195 | {
|
---|
[916bf1a] | 196 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 197 | assert(devcon);
|
---|
| 198 |
|
---|
| 199 | devcon_remove(devcon);
|
---|
| 200 |
|
---|
[6284978] | 201 | if (devcon->bb_buf)
|
---|
| 202 | free(devcon->bb_buf);
|
---|
[f1ba5d6] | 203 |
|
---|
| 204 | if (devcon->cache) {
|
---|
| 205 | hash_table_destroy(&devcon->cache->block_hash);
|
---|
| 206 | free(devcon->cache);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[a830611] | 209 | munmap(devcon->comm_area, devcon->comm_size);
|
---|
[916bf1a] | 210 | ipc_hangup(devcon->dev_phone);
|
---|
| 211 |
|
---|
| 212 | free(devcon);
|
---|
[7858bc5f] | 213 | }
|
---|
| 214 |
|
---|
[1ee00b7] | 215 | int block_bb_read(dev_handle_t dev_handle, bn_t ba)
|
---|
[6284978] | 216 | {
|
---|
| 217 | void *bb_buf;
|
---|
[0c243b4] | 218 | int rc;
|
---|
[6284978] | 219 |
|
---|
| 220 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 221 | if (!devcon)
|
---|
| 222 | return ENOENT;
|
---|
| 223 | if (devcon->bb_buf)
|
---|
| 224 | return EEXIST;
|
---|
[1ee00b7] | 225 | bb_buf = malloc(devcon->pblock_size);
|
---|
[6284978] | 226 | if (!bb_buf)
|
---|
| 227 | return ENOMEM;
|
---|
[1ee00b7] | 228 |
|
---|
[a830611] | 229 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
[1ee00b7] | 230 | rc = read_blocks(devcon, 0, 1);
|
---|
[0c243b4] | 231 | if (rc != EOK) {
|
---|
[a830611] | 232 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[6284978] | 233 | free(bb_buf);
|
---|
[0c243b4] | 234 | return rc;
|
---|
[6284978] | 235 | }
|
---|
[a830611] | 236 | memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
|
---|
| 237 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[6408be3] | 238 |
|
---|
[6284978] | 239 | devcon->bb_buf = bb_buf;
|
---|
[1ee00b7] | 240 | devcon->bb_addr = ba;
|
---|
[6284978] | 241 |
|
---|
| 242 | return EOK;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[7858bc5f] | 245 | void *block_bb_get(dev_handle_t dev_handle)
|
---|
| 246 | {
|
---|
[916bf1a] | 247 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
| 248 | assert(devcon);
|
---|
| 249 | return devcon->bb_buf;
|
---|
[7858bc5f] | 250 | }
|
---|
| 251 |
|
---|
[f1ba5d6] | 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 |
|
---|
[1fbe064b] | 273 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
|
---|
| 274 | enum cache_mode mode)
|
---|
[f1ba5d6] | 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 |
|
---|
[4e1b57d] | 286 | fibril_mutex_initialize(&cache->lock);
|
---|
[f1ba5d6] | 287 | list_initialize(&cache->free_head);
|
---|
[1ee00b7] | 288 | cache->lblock_size = size;
|
---|
[f1ba5d6] | 289 | cache->block_count = blocks;
|
---|
[d68e4d5] | 290 | cache->blocks_cached = 0;
|
---|
[1fbe064b] | 291 | cache->mode = mode;
|
---|
[f1ba5d6] | 292 |
|
---|
[1ee00b7] | 293 | /* No block size translation a.t.m. */
|
---|
| 294 | assert(cache->lblock_size == devcon->pblock_size);
|
---|
| 295 |
|
---|
[f1ba5d6] | 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 |
|
---|
[d68e4d5] | 306 | #define CACHE_LO_WATERMARK 10
|
---|
| 307 | #define CACHE_HI_WATERMARK 20
|
---|
[e1c88d5] | 308 | static bool cache_can_grow(cache_t *cache)
|
---|
[fc840d9] | 309 | {
|
---|
[d68e4d5] | 310 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
---|
| 311 | return true;
|
---|
| 312 | if (!list_empty(&cache->free_head))
|
---|
| 313 | return false;
|
---|
[e1c88d5] | 314 | return true;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | static void block_initialize(block_t *b)
|
---|
| 318 | {
|
---|
[4e1b57d] | 319 | fibril_mutex_initialize(&b->lock);
|
---|
[e1c88d5] | 320 | b->refcnt = 1;
|
---|
| 321 | b->dirty = false;
|
---|
[cd688d9] | 322 | b->toxic = false;
|
---|
[4e1b57d] | 323 | fibril_rwlock_initialize(&b->contents_lock);
|
---|
[e1c88d5] | 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 | *
|
---|
[c91f2d1b] | 330 | * @param block Pointer to where the function will store the
|
---|
| 331 | * block pointer on success.
|
---|
[e1c88d5] | 332 | * @param dev_handle Device handle of the block device.
|
---|
| 333 | * @param boff Block offset.
|
---|
[1d8cdb1] | 334 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
| 335 | * will not read the contents of the block from the
|
---|
| 336 | * device.
|
---|
[e1c88d5] | 337 | *
|
---|
[c91f2d1b] | 338 | * @return EOK on success or a negative error code.
|
---|
[e1c88d5] | 339 | */
|
---|
[c91f2d1b] | 340 | int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
|
---|
[e1c88d5] | 341 | {
|
---|
| 342 | devcon_t *devcon;
|
---|
| 343 | cache_t *cache;
|
---|
[fc840d9] | 344 | block_t *b;
|
---|
[e1c88d5] | 345 | link_t *l;
|
---|
| 346 | unsigned long key = boff;
|
---|
[b7b3fda] | 347 | int rc;
|
---|
[e1c88d5] | 348 |
|
---|
| 349 | devcon = devcon_search(dev_handle);
|
---|
[fc840d9] | 350 |
|
---|
[e1c88d5] | 351 | assert(devcon);
|
---|
| 352 | assert(devcon->cache);
|
---|
[fc840d9] | 353 |
|
---|
[e1c88d5] | 354 | cache = devcon->cache;
|
---|
[02ee6bf5] | 355 |
|
---|
| 356 | retry:
|
---|
[b7b3fda] | 357 | rc = EOK;
|
---|
[4f690cd] | 358 | b = NULL;
|
---|
[b7b3fda] | 359 |
|
---|
[4e1b57d] | 360 | fibril_mutex_lock(&cache->lock);
|
---|
[e1c88d5] | 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);
|
---|
[4e1b57d] | 367 | fibril_mutex_lock(&b->lock);
|
---|
[e1c88d5] | 368 | if (b->refcnt++ == 0)
|
---|
| 369 | list_remove(&b->free_link);
|
---|
[402a18f] | 370 | if (b->toxic)
|
---|
| 371 | rc = EIO;
|
---|
[4e1b57d] | 372 | fibril_mutex_unlock(&b->lock);
|
---|
| 373 | fibril_mutex_unlock(&cache->lock);
|
---|
[e1c88d5] | 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;
|
---|
[1ee00b7] | 387 | b->data = malloc(cache->lblock_size);
|
---|
[e1c88d5] | 388 | if (!b->data) {
|
---|
| 389 | free(b);
|
---|
| 390 | goto recycle;
|
---|
| 391 | }
|
---|
[d68e4d5] | 392 | cache->blocks_cached++;
|
---|
[e1c88d5] | 393 | } else {
|
---|
| 394 | /*
|
---|
| 395 | * Try to recycle a block from the free list.
|
---|
| 396 | */
|
---|
| 397 | unsigned long temp_key;
|
---|
| 398 | recycle:
|
---|
[7a56b1ed] | 399 | if (list_empty(&cache->free_head)) {
|
---|
| 400 | fibril_mutex_unlock(&cache->lock);
|
---|
| 401 | rc = ENOMEM;
|
---|
| 402 | goto out;
|
---|
| 403 | }
|
---|
[e1c88d5] | 404 | l = cache->free_head.next;
|
---|
[d68e4d5] | 405 | b = list_get_instance(l, block_t, free_link);
|
---|
[02ee6bf5] | 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);
|
---|
[a830611] | 421 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 422 | memcpy(devcon->comm_area, b->data, b->size);
|
---|
[1ee00b7] | 423 | rc = write_blocks(devcon, b->boff, 1);
|
---|
[a830611] | 424 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[402a18f] | 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 | }
|
---|
[02ee6bf5] | 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);
|
---|
[e1c88d5] | 453 | temp_key = b->boff;
|
---|
| 454 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
---|
| 455 | }
|
---|
[fc840d9] | 456 |
|
---|
[e1c88d5] | 457 | block_initialize(b);
|
---|
| 458 | b->dev_handle = dev_handle;
|
---|
[1ee00b7] | 459 | b->size = cache->lblock_size;
|
---|
[e1c88d5] | 460 | b->boff = boff;
|
---|
[a6d97fb9] | 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
|
---|
[5ac8918] | 465 | * kill concurrent operations on the cache while doing I/O on
|
---|
| 466 | * the block.
|
---|
[a6d97fb9] | 467 | */
|
---|
[4e1b57d] | 468 | fibril_mutex_lock(&b->lock);
|
---|
| 469 | fibril_mutex_unlock(&cache->lock);
|
---|
[a6d97fb9] | 470 |
|
---|
[1d8cdb1] | 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 | */
|
---|
[a830611] | 476 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
[1ee00b7] | 477 | rc = read_blocks(devcon, b->boff, 1);
|
---|
[a830611] | 478 | memcpy(b->data, devcon->comm_area, cache->lblock_size);
|
---|
| 479 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[402a18f] | 480 | if (rc != EOK)
|
---|
| 481 | b->toxic = true;
|
---|
| 482 | } else
|
---|
| 483 | rc = EOK;
|
---|
[fc840d9] | 484 |
|
---|
[4e1b57d] | 485 | fibril_mutex_unlock(&b->lock);
|
---|
[a6d97fb9] | 486 | }
|
---|
[7a56b1ed] | 487 | out:
|
---|
[4f690cd] | 488 | if ((rc != EOK) && b) {
|
---|
| 489 | assert(b->toxic);
|
---|
| 490 | (void) block_put(b);
|
---|
| 491 | b = NULL;
|
---|
| 492 | }
|
---|
[c91f2d1b] | 493 | *block = b;
|
---|
[402a18f] | 494 | return rc;
|
---|
[fc840d9] | 495 | }
|
---|
| 496 |
|
---|
[d5a720cf] | 497 | /** Release a reference to a block.
|
---|
| 498 | *
|
---|
[a6d97fb9] | 499 | * If the last reference is dropped, the block is put on the free list.
|
---|
[d5a720cf] | 500 | *
|
---|
| 501 | * @param block Block of which a reference is to be released.
|
---|
[c91f2d1b] | 502 | *
|
---|
| 503 | * @return EOK on success or a negative error code.
|
---|
[d5a720cf] | 504 | */
|
---|
[c91f2d1b] | 505 | int block_put(block_t *block)
|
---|
[fc840d9] | 506 | {
|
---|
[d5a720cf] | 507 | devcon_t *devcon = devcon_search(block->dev_handle);
|
---|
| 508 | cache_t *cache;
|
---|
[ddfc39a3] | 509 | unsigned blocks_cached;
|
---|
| 510 | enum cache_mode mode;
|
---|
[402a18f] | 511 | int rc = EOK;
|
---|
[d5a720cf] | 512 |
|
---|
| 513 | assert(devcon);
|
---|
| 514 | assert(devcon->cache);
|
---|
| 515 |
|
---|
| 516 | cache = devcon->cache;
|
---|
[ddfc39a3] | 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);
|
---|
[402a18f] | 532 | if (block->toxic)
|
---|
| 533 | block->dirty = false; /* will not write back toxic block */
|
---|
[ddfc39a3] | 534 | if (block->dirty && (block->refcnt == 1) &&
|
---|
| 535 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
---|
[a830611] | 536 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 537 | memcpy(devcon->comm_area, block->data, block->size);
|
---|
[1ee00b7] | 538 | rc = write_blocks(devcon, block->boff, 1);
|
---|
[a830611] | 539 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[ddfc39a3] | 540 | block->dirty = false;
|
---|
| 541 | }
|
---|
| 542 | fibril_mutex_unlock(&block->lock);
|
---|
| 543 |
|
---|
[4e1b57d] | 544 | fibril_mutex_lock(&cache->lock);
|
---|
| 545 | fibril_mutex_lock(&block->lock);
|
---|
[d5a720cf] | 546 | if (!--block->refcnt) {
|
---|
| 547 | /*
|
---|
[d68e4d5] | 548 | * Last reference to the block was dropped. Either free the
|
---|
[402a18f] | 549 | * block or put it on the free list. In case of an I/O error,
|
---|
| 550 | * free the block.
|
---|
[d68e4d5] | 551 | */
|
---|
[402a18f] | 552 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
---|
| 553 | (rc != EOK)) {
|
---|
[d68e4d5] | 554 | /*
|
---|
[402a18f] | 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.
|
---|
[d68e4d5] | 558 | */
|
---|
| 559 | if (block->dirty) {
|
---|
[ddfc39a3] | 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;
|
---|
[d68e4d5] | 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);
|
---|
[402a18f] | 578 | return rc;
|
---|
[d68e4d5] | 579 | }
|
---|
| 580 | /*
|
---|
| 581 | * Put the block on the free list.
|
---|
[d5a720cf] | 582 | */
|
---|
[1fbe064b] | 583 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
---|
[ddfc39a3] | 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;
|
---|
[1fbe064b] | 592 | }
|
---|
[ddfc39a3] | 593 | list_append(&block->free_link, &cache->free_head);
|
---|
[d5a720cf] | 594 | }
|
---|
[4e1b57d] | 595 | fibril_mutex_unlock(&block->lock);
|
---|
| 596 | fibril_mutex_unlock(&cache->lock);
|
---|
[c91f2d1b] | 597 |
|
---|
[402a18f] | 598 | return rc;
|
---|
[d5a720cf] | 599 | }
|
---|
| 600 |
|
---|
[6408be3] | 601 | /** Read sequential data from a block device.
|
---|
[d5a720cf] | 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 | */
|
---|
[6408be3] | 615 | int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
|
---|
[1ee00b7] | 616 | off_t *pos, void *dst, size_t size)
|
---|
[d5a720cf] | 617 | {
|
---|
| 618 | off_t offset = 0;
|
---|
| 619 | size_t left = size;
|
---|
[1ee00b7] | 620 | size_t block_size;
|
---|
| 621 | devcon_t *devcon;
|
---|
| 622 |
|
---|
| 623 | devcon = devcon_search(dev_handle);
|
---|
[d5a720cf] | 624 | assert(devcon);
|
---|
[1ee00b7] | 625 | block_size = devcon->pblock_size;
|
---|
[e1c88d5] | 626 |
|
---|
[a830611] | 627 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
[d5a720cf] | 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 | */
|
---|
[a830611] | 641 | memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
|
---|
[d5a720cf] | 642 | offset += rd;
|
---|
| 643 | *bufpos += rd;
|
---|
| 644 | *pos += rd;
|
---|
| 645 | left -= rd;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
[62140db] | 648 | if (*bufpos == (off_t) *buflen) {
|
---|
[d5a720cf] | 649 | /* Refill the communication buffer with a new block. */
|
---|
[6408be3] | 650 | int rc;
|
---|
| 651 |
|
---|
[1ee00b7] | 652 | rc = read_blocks(devcon, *pos / block_size, 1);
|
---|
[d68e4d5] | 653 | if (rc != EOK) {
|
---|
[a830611] | 654 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[6408be3] | 655 | return rc;
|
---|
[d68e4d5] | 656 | }
|
---|
[d5a720cf] | 657 |
|
---|
| 658 | *bufpos = 0;
|
---|
| 659 | *buflen = block_size;
|
---|
| 660 | }
|
---|
| 661 | }
|
---|
[a830611] | 662 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[d5a720cf] | 663 |
|
---|
| 664 | return EOK;
|
---|
[fc840d9] | 665 | }
|
---|
| 666 |
|
---|
[00b1d20e] | 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 |
|
---|
[1ee00b7] | 740 | /** Read blocks from block device.
|
---|
[6408be3] | 741 | *
|
---|
| 742 | * @param devcon Device connection.
|
---|
[1ee00b7] | 743 | * @param ba Address of first block.
|
---|
| 744 | * @param cnt Number of blocks.
|
---|
[6408be3] | 745 | * @param src Buffer for storing the data.
|
---|
| 746 | *
|
---|
| 747 | * @return EOK on success or negative error code on failure.
|
---|
| 748 | */
|
---|
[1ee00b7] | 749 | static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
|
---|
[6408be3] | 750 | {
|
---|
| 751 | int rc;
|
---|
| 752 |
|
---|
| 753 | assert(devcon);
|
---|
[1ee00b7] | 754 | rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
|
---|
| 755 | UPPER32(ba), cnt);
|
---|
| 756 | return rc;
|
---|
[6408be3] | 757 | }
|
---|
| 758 |
|
---|
[1fbe064b] | 759 | /** Write block to block device.
|
---|
| 760 | *
|
---|
| 761 | * @param devcon Device connection.
|
---|
[1ee00b7] | 762 | * @param ba Address of first block.
|
---|
| 763 | * @param cnt Number of blocks.
|
---|
[1fbe064b] | 764 | * @param src Buffer containing the data to write.
|
---|
| 765 | *
|
---|
| 766 | * @return EOK on success or negative error code on failure.
|
---|
| 767 | */
|
---|
[1ee00b7] | 768 | static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
|
---|
[1fbe064b] | 769 | {
|
---|
| 770 | int rc;
|
---|
| 771 |
|
---|
| 772 | assert(devcon);
|
---|
[1ee00b7] | 773 | rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
|
---|
| 774 | UPPER32(ba), cnt);
|
---|
| 775 | return rc;
|
---|
| 776 | }
|
---|
[1fbe064b] | 777 |
|
---|
[1ee00b7] | 778 | /** Get block size used by the device. */
|
---|
[00b1d20e] | 779 | static int get_block_size(int dev_phone, size_t *bsize)
|
---|
[1ee00b7] | 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;
|
---|
[1fbe064b] | 789 | }
|
---|
| 790 |
|
---|
[fc840d9] | 791 | /** @}
|
---|
| 792 | */
|
---|