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