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