[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"
|
---|
[15f3c3f] | 41 | #include <ipc/loc.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;
|
---|
[15f3c3f] | 80 | service_id_t service_id;
|
---|
[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 |
|
---|
[15f3c3f] | 97 | static devcon_t *devcon_search(service_id_t service_id)
|
---|
[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);
|
---|
[15f3c3f] | 103 | if (devcon->service_id == service_id) {
|
---|
[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 |
|
---|
[15f3c3f] | 113 | static int devcon_add(service_id_t service_id, async_sess_t *sess,
|
---|
[79ae36dd] | 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);
|
---|
[15f3c3f] | 126 | devcon->service_id = service_id;
|
---|
[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);
|
---|
[15f3c3f] | 139 | if (d->service_id == service_id) {
|
---|
[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 |
|
---|
[15f3c3f] | 157 | int block_init(exch_mgmt_t mgmt, service_id_t service_id,
|
---|
[79ae36dd] | 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 |
|
---|
[15f3c3f] | 165 | async_sess_t *sess = loc_service_connect(mgmt, service_id,
|
---|
[79ae36dd] | 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 |
|
---|
[15f3c3f] | 192 | rc = devcon_add(service_id, 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 |
|
---|
[15f3c3f] | 202 | void block_fini(service_id_t service_id)
|
---|
[7858bc5f] | 203 | {
|
---|
[15f3c3f] | 204 | devcon_t *devcon = devcon_search(service_id);
|
---|
[916bf1a] | 205 | assert(devcon);
|
---|
| 206 |
|
---|
[64bc4b6] | 207 | if (devcon->cache)
|
---|
[15f3c3f] | 208 | (void) block_cache_fini(service_id);
|
---|
[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 |
|
---|
[15f3c3f] | 221 | int block_bb_read(service_id_t service_id, aoff64_t ba)
|
---|
[6284978] | 222 | {
|
---|
| 223 | void *bb_buf;
|
---|
[0c243b4] | 224 | int rc;
|
---|
[6284978] | 225 |
|
---|
[15f3c3f] | 226 | devcon_t *devcon = devcon_search(service_id);
|
---|
[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 |
|
---|
[15f3c3f] | 251 | void *block_bb_get(service_id_t service_id)
|
---|
[7858bc5f] | 252 | {
|
---|
[15f3c3f] | 253 | devcon_t *devcon = devcon_search(service_id);
|
---|
[916bf1a] | 254 | assert(devcon);
|
---|
| 255 | return devcon->bb_buf;
|
---|
[7858bc5f] | 256 | }
|
---|
| 257 |
|
---|
[f1ba5d6] | 258 | static hash_index_t cache_hash(unsigned long *key)
|
---|
| 259 | {
|
---|
[867e2555] | 260 | return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
|
---|
[f1ba5d6] | 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);
|
---|
[867e2555] | 266 | return b->lba == MERGE_LOUP32(key[0], key[1]);
|
---|
[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 |
|
---|
[15f3c3f] | 279 | int block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
|
---|
[1fbe064b] | 280 | enum cache_mode mode)
|
---|
[f1ba5d6] | 281 | {
|
---|
[15f3c3f] | 282 | devcon_t *devcon = devcon_search(service_id);
|
---|
[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 |
|
---|
[867e2555] | 307 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
|
---|
[f1ba5d6] | 308 | &cache_ops)) {
|
---|
| 309 | free(cache);
|
---|
| 310 | return ENOMEM;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | devcon->cache = cache;
|
---|
| 314 | return EOK;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[15f3c3f] | 317 | int block_cache_fini(service_id_t service_id)
|
---|
[64bc4b6] | 318 | {
|
---|
[15f3c3f] | 319 | devcon_t *devcon = devcon_search(service_id);
|
---|
[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 |
|
---|
[867e2555] | 346 | unsigned long key[2] = {
|
---|
| 347 | LOWER32(b->lba),
|
---|
| 348 | UPPER32(b->lba)
|
---|
| 349 | };
|
---|
| 350 | hash_table_remove(&cache->block_hash, key, 2);
|
---|
[64bc4b6] | 351 |
|
---|
| 352 | free(b->data);
|
---|
| 353 | free(b);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | hash_table_destroy(&cache->block_hash);
|
---|
| 357 | devcon->cache = NULL;
|
---|
| 358 | free(cache);
|
---|
| 359 |
|
---|
| 360 | return EOK;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[d68e4d5] | 363 | #define CACHE_LO_WATERMARK 10
|
---|
| 364 | #define CACHE_HI_WATERMARK 20
|
---|
[e1c88d5] | 365 | static bool cache_can_grow(cache_t *cache)
|
---|
[fc840d9] | 366 | {
|
---|
[d68e4d5] | 367 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
---|
| 368 | return true;
|
---|
[b72efe8] | 369 | if (!list_empty(&cache->free_list))
|
---|
[d68e4d5] | 370 | return false;
|
---|
[e1c88d5] | 371 | return true;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | static void block_initialize(block_t *b)
|
---|
| 375 | {
|
---|
[4e1b57d] | 376 | fibril_mutex_initialize(&b->lock);
|
---|
[e1c88d5] | 377 | b->refcnt = 1;
|
---|
| 378 | b->dirty = false;
|
---|
[cd688d9] | 379 | b->toxic = false;
|
---|
[4e1b57d] | 380 | fibril_rwlock_initialize(&b->contents_lock);
|
---|
[e1c88d5] | 381 | link_initialize(&b->free_link);
|
---|
| 382 | link_initialize(&b->hash_link);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /** Instantiate a block in memory and get a reference to it.
|
---|
| 386 | *
|
---|
[c91f2d1b] | 387 | * @param block Pointer to where the function will store the
|
---|
| 388 | * block pointer on success.
|
---|
[15f3c3f] | 389 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 390 | * @param ba Block address (logical).
|
---|
[1d8cdb1] | 391 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
| 392 | * will not read the contents of the block from the
|
---|
| 393 | * device.
|
---|
[e1c88d5] | 394 | *
|
---|
[c91f2d1b] | 395 | * @return EOK on success or a negative error code.
|
---|
[e1c88d5] | 396 | */
|
---|
[15f3c3f] | 397 | int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
|
---|
[e1c88d5] | 398 | {
|
---|
| 399 | devcon_t *devcon;
|
---|
| 400 | cache_t *cache;
|
---|
[fc840d9] | 401 | block_t *b;
|
---|
[e1c88d5] | 402 | link_t *l;
|
---|
[867e2555] | 403 | unsigned long key[2] = {
|
---|
| 404 | LOWER32(ba),
|
---|
| 405 | UPPER32(ba)
|
---|
| 406 | };
|
---|
| 407 |
|
---|
[b7b3fda] | 408 | int rc;
|
---|
[e1c88d5] | 409 |
|
---|
[15f3c3f] | 410 | devcon = devcon_search(service_id);
|
---|
[fc840d9] | 411 |
|
---|
[e1c88d5] | 412 | assert(devcon);
|
---|
| 413 | assert(devcon->cache);
|
---|
[fc840d9] | 414 |
|
---|
[e1c88d5] | 415 | cache = devcon->cache;
|
---|
[02ee6bf5] | 416 |
|
---|
| 417 | retry:
|
---|
[b7b3fda] | 418 | rc = EOK;
|
---|
[4f690cd] | 419 | b = NULL;
|
---|
[b7b3fda] | 420 |
|
---|
[4e1b57d] | 421 | fibril_mutex_lock(&cache->lock);
|
---|
[867e2555] | 422 | l = hash_table_find(&cache->block_hash, key);
|
---|
[e1c88d5] | 423 | if (l) {
|
---|
[5716e9a] | 424 | found:
|
---|
[e1c88d5] | 425 | /*
|
---|
| 426 | * We found the block in the cache.
|
---|
| 427 | */
|
---|
| 428 | b = hash_table_get_instance(l, block_t, hash_link);
|
---|
[4e1b57d] | 429 | fibril_mutex_lock(&b->lock);
|
---|
[e1c88d5] | 430 | if (b->refcnt++ == 0)
|
---|
| 431 | list_remove(&b->free_link);
|
---|
[402a18f] | 432 | if (b->toxic)
|
---|
| 433 | rc = EIO;
|
---|
[4e1b57d] | 434 | fibril_mutex_unlock(&b->lock);
|
---|
| 435 | fibril_mutex_unlock(&cache->lock);
|
---|
[e1c88d5] | 436 | } else {
|
---|
| 437 | /*
|
---|
| 438 | * The block was not found in the cache.
|
---|
| 439 | */
|
---|
| 440 | if (cache_can_grow(cache)) {
|
---|
| 441 | /*
|
---|
| 442 | * We can grow the cache by allocating new blocks.
|
---|
| 443 | * Should the allocation fail, we fail over and try to
|
---|
| 444 | * recycle a block from the cache.
|
---|
| 445 | */
|
---|
| 446 | b = malloc(sizeof(block_t));
|
---|
| 447 | if (!b)
|
---|
| 448 | goto recycle;
|
---|
[1ee00b7] | 449 | b->data = malloc(cache->lblock_size);
|
---|
[e1c88d5] | 450 | if (!b->data) {
|
---|
| 451 | free(b);
|
---|
[0dfaa099] | 452 | b = NULL;
|
---|
[e1c88d5] | 453 | goto recycle;
|
---|
| 454 | }
|
---|
[d68e4d5] | 455 | cache->blocks_cached++;
|
---|
[e1c88d5] | 456 | } else {
|
---|
| 457 | /*
|
---|
| 458 | * Try to recycle a block from the free list.
|
---|
| 459 | */
|
---|
| 460 | recycle:
|
---|
[b72efe8] | 461 | if (list_empty(&cache->free_list)) {
|
---|
[7a56b1ed] | 462 | fibril_mutex_unlock(&cache->lock);
|
---|
| 463 | rc = ENOMEM;
|
---|
| 464 | goto out;
|
---|
| 465 | }
|
---|
[b72efe8] | 466 | l = list_first(&cache->free_list);
|
---|
[d68e4d5] | 467 | b = list_get_instance(l, block_t, free_link);
|
---|
[02ee6bf5] | 468 |
|
---|
| 469 | fibril_mutex_lock(&b->lock);
|
---|
| 470 | if (b->dirty) {
|
---|
| 471 | /*
|
---|
| 472 | * The block needs to be written back to the
|
---|
| 473 | * device before it changes identity. Do this
|
---|
| 474 | * while not holding the cache lock so that
|
---|
| 475 | * concurrency is not impeded. Also move the
|
---|
| 476 | * block to the end of the free list so that we
|
---|
| 477 | * do not slow down other instances of
|
---|
| 478 | * block_get() draining the free list.
|
---|
| 479 | */
|
---|
| 480 | list_remove(&b->free_link);
|
---|
[b72efe8] | 481 | list_append(&b->free_link, &cache->free_list);
|
---|
[02ee6bf5] | 482 | fibril_mutex_unlock(&cache->lock);
|
---|
[a830611] | 483 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 484 | memcpy(devcon->comm_area, b->data, b->size);
|
---|
[f092718] | 485 | rc = write_blocks(devcon, b->pba,
|
---|
| 486 | cache->blocks_cluster);
|
---|
[a830611] | 487 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[402a18f] | 488 | if (rc != EOK) {
|
---|
| 489 | /*
|
---|
| 490 | * We did not manage to write the block
|
---|
| 491 | * to the device. Keep it around for
|
---|
| 492 | * another try. Hopefully, we will grab
|
---|
| 493 | * another block next time.
|
---|
| 494 | */
|
---|
| 495 | fibril_mutex_unlock(&b->lock);
|
---|
| 496 | goto retry;
|
---|
| 497 | }
|
---|
[02ee6bf5] | 498 | b->dirty = false;
|
---|
| 499 | if (!fibril_mutex_trylock(&cache->lock)) {
|
---|
| 500 | /*
|
---|
| 501 | * Somebody is probably racing with us.
|
---|
| 502 | * Unlock the block and retry.
|
---|
| 503 | */
|
---|
| 504 | fibril_mutex_unlock(&b->lock);
|
---|
| 505 | goto retry;
|
---|
| 506 | }
|
---|
[867e2555] | 507 | l = hash_table_find(&cache->block_hash, key);
|
---|
[5716e9a] | 508 | if (l) {
|
---|
| 509 | /*
|
---|
| 510 | * Someone else must have already
|
---|
| 511 | * instantiated the block while we were
|
---|
| 512 | * not holding the cache lock.
|
---|
| 513 | * Leave the recycled block on the
|
---|
| 514 | * freelist and continue as if we
|
---|
| 515 | * found the block of interest during
|
---|
| 516 | * the first try.
|
---|
| 517 | */
|
---|
| 518 | fibril_mutex_unlock(&b->lock);
|
---|
| 519 | goto found;
|
---|
| 520 | }
|
---|
[02ee6bf5] | 521 |
|
---|
| 522 | }
|
---|
| 523 | fibril_mutex_unlock(&b->lock);
|
---|
| 524 |
|
---|
| 525 | /*
|
---|
| 526 | * Unlink the block from the free list and the hash
|
---|
| 527 | * table.
|
---|
| 528 | */
|
---|
| 529 | list_remove(&b->free_link);
|
---|
[867e2555] | 530 | unsigned long temp_key[2] = {
|
---|
| 531 | LOWER32(b->lba),
|
---|
| 532 | UPPER32(b->lba)
|
---|
| 533 | };
|
---|
| 534 | hash_table_remove(&cache->block_hash, temp_key, 2);
|
---|
[e1c88d5] | 535 | }
|
---|
[fc840d9] | 536 |
|
---|
[e1c88d5] | 537 | block_initialize(b);
|
---|
[15f3c3f] | 538 | b->service_id = service_id;
|
---|
[1ee00b7] | 539 | b->size = cache->lblock_size;
|
---|
[a6ba0c9] | 540 | b->lba = ba;
|
---|
| 541 | b->pba = ba_ltop(devcon, b->lba);
|
---|
[867e2555] | 542 | hash_table_insert(&cache->block_hash, key, &b->hash_link);
|
---|
[a6d97fb9] | 543 |
|
---|
| 544 | /*
|
---|
| 545 | * Lock the block before releasing the cache lock. Thus we don't
|
---|
[5ac8918] | 546 | * kill concurrent operations on the cache while doing I/O on
|
---|
| 547 | * the block.
|
---|
[a6d97fb9] | 548 | */
|
---|
[4e1b57d] | 549 | fibril_mutex_lock(&b->lock);
|
---|
| 550 | fibril_mutex_unlock(&cache->lock);
|
---|
[a6d97fb9] | 551 |
|
---|
[1d8cdb1] | 552 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
---|
| 553 | /*
|
---|
| 554 | * The block contains old or no data. We need to read
|
---|
| 555 | * the new contents from the device.
|
---|
| 556 | */
|
---|
[a830611] | 557 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
[f092718] | 558 | rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
|
---|
[a830611] | 559 | memcpy(b->data, devcon->comm_area, cache->lblock_size);
|
---|
| 560 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[402a18f] | 561 | if (rc != EOK)
|
---|
| 562 | b->toxic = true;
|
---|
| 563 | } else
|
---|
| 564 | rc = EOK;
|
---|
[fc840d9] | 565 |
|
---|
[4e1b57d] | 566 | fibril_mutex_unlock(&b->lock);
|
---|
[a6d97fb9] | 567 | }
|
---|
[7a56b1ed] | 568 | out:
|
---|
[4f690cd] | 569 | if ((rc != EOK) && b) {
|
---|
| 570 | assert(b->toxic);
|
---|
| 571 | (void) block_put(b);
|
---|
| 572 | b = NULL;
|
---|
| 573 | }
|
---|
[c91f2d1b] | 574 | *block = b;
|
---|
[402a18f] | 575 | return rc;
|
---|
[fc840d9] | 576 | }
|
---|
| 577 |
|
---|
[d5a720cf] | 578 | /** Release a reference to a block.
|
---|
| 579 | *
|
---|
[a6d97fb9] | 580 | * If the last reference is dropped, the block is put on the free list.
|
---|
[d5a720cf] | 581 | *
|
---|
| 582 | * @param block Block of which a reference is to be released.
|
---|
[c91f2d1b] | 583 | *
|
---|
| 584 | * @return EOK on success or a negative error code.
|
---|
[d5a720cf] | 585 | */
|
---|
[c91f2d1b] | 586 | int block_put(block_t *block)
|
---|
[fc840d9] | 587 | {
|
---|
[15f3c3f] | 588 | devcon_t *devcon = devcon_search(block->service_id);
|
---|
[d5a720cf] | 589 | cache_t *cache;
|
---|
[ddfc39a3] | 590 | unsigned blocks_cached;
|
---|
| 591 | enum cache_mode mode;
|
---|
[402a18f] | 592 | int rc = EOK;
|
---|
[d5a720cf] | 593 |
|
---|
| 594 | assert(devcon);
|
---|
| 595 | assert(devcon->cache);
|
---|
[0f1cf7a] | 596 | assert(block->refcnt >= 1);
|
---|
[d5a720cf] | 597 |
|
---|
| 598 | cache = devcon->cache;
|
---|
[ddfc39a3] | 599 |
|
---|
| 600 | retry:
|
---|
| 601 | fibril_mutex_lock(&cache->lock);
|
---|
| 602 | blocks_cached = cache->blocks_cached;
|
---|
| 603 | mode = cache->mode;
|
---|
| 604 | fibril_mutex_unlock(&cache->lock);
|
---|
| 605 |
|
---|
| 606 | /*
|
---|
| 607 | * Determine whether to sync the block. Syncing the block is best done
|
---|
| 608 | * when not holding the cache lock as it does not impede concurrency.
|
---|
| 609 | * Since the situation may have changed when we unlocked the cache, the
|
---|
| 610 | * blocks_cached and mode variables are mere hints. We will recheck the
|
---|
| 611 | * conditions later when the cache lock is held again.
|
---|
| 612 | */
|
---|
| 613 | fibril_mutex_lock(&block->lock);
|
---|
[402a18f] | 614 | if (block->toxic)
|
---|
| 615 | block->dirty = false; /* will not write back toxic block */
|
---|
[ddfc39a3] | 616 | if (block->dirty && (block->refcnt == 1) &&
|
---|
| 617 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
---|
[a830611] | 618 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 619 | memcpy(devcon->comm_area, block->data, block->size);
|
---|
[f092718] | 620 | rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
|
---|
[a830611] | 621 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[ddfc39a3] | 622 | block->dirty = false;
|
---|
| 623 | }
|
---|
| 624 | fibril_mutex_unlock(&block->lock);
|
---|
| 625 |
|
---|
[4e1b57d] | 626 | fibril_mutex_lock(&cache->lock);
|
---|
| 627 | fibril_mutex_lock(&block->lock);
|
---|
[d5a720cf] | 628 | if (!--block->refcnt) {
|
---|
| 629 | /*
|
---|
[d68e4d5] | 630 | * Last reference to the block was dropped. Either free the
|
---|
[402a18f] | 631 | * block or put it on the free list. In case of an I/O error,
|
---|
| 632 | * free the block.
|
---|
[d68e4d5] | 633 | */
|
---|
[402a18f] | 634 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
---|
| 635 | (rc != EOK)) {
|
---|
[d68e4d5] | 636 | /*
|
---|
[402a18f] | 637 | * Currently there are too many cached blocks or there
|
---|
| 638 | * was an I/O error when writing the block back to the
|
---|
| 639 | * device.
|
---|
[d68e4d5] | 640 | */
|
---|
| 641 | if (block->dirty) {
|
---|
[ddfc39a3] | 642 | /*
|
---|
| 643 | * We cannot sync the block while holding the
|
---|
| 644 | * cache lock. Release everything and retry.
|
---|
| 645 | */
|
---|
| 646 | block->refcnt++;
|
---|
| 647 | fibril_mutex_unlock(&block->lock);
|
---|
| 648 | fibril_mutex_unlock(&cache->lock);
|
---|
| 649 | goto retry;
|
---|
[d68e4d5] | 650 | }
|
---|
| 651 | /*
|
---|
| 652 | * Take the block out of the cache and free it.
|
---|
| 653 | */
|
---|
[867e2555] | 654 | unsigned long key[2] = {
|
---|
| 655 | LOWER32(block->lba),
|
---|
| 656 | UPPER32(block->lba)
|
---|
| 657 | };
|
---|
| 658 | hash_table_remove(&cache->block_hash, key, 2);
|
---|
[956d4df8] | 659 | fibril_mutex_unlock(&block->lock);
|
---|
[d68e4d5] | 660 | free(block->data);
|
---|
[b9e6205] | 661 | free(block);
|
---|
[d68e4d5] | 662 | cache->blocks_cached--;
|
---|
| 663 | fibril_mutex_unlock(&cache->lock);
|
---|
[402a18f] | 664 | return rc;
|
---|
[d68e4d5] | 665 | }
|
---|
| 666 | /*
|
---|
| 667 | * Put the block on the free list.
|
---|
[d5a720cf] | 668 | */
|
---|
[1fbe064b] | 669 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
---|
[ddfc39a3] | 670 | /*
|
---|
| 671 | * We cannot sync the block while holding the cache
|
---|
| 672 | * lock. Release everything and retry.
|
---|
| 673 | */
|
---|
| 674 | block->refcnt++;
|
---|
| 675 | fibril_mutex_unlock(&block->lock);
|
---|
| 676 | fibril_mutex_unlock(&cache->lock);
|
---|
| 677 | goto retry;
|
---|
[1fbe064b] | 678 | }
|
---|
[b72efe8] | 679 | list_append(&block->free_link, &cache->free_list);
|
---|
[d5a720cf] | 680 | }
|
---|
[4e1b57d] | 681 | fibril_mutex_unlock(&block->lock);
|
---|
| 682 | fibril_mutex_unlock(&cache->lock);
|
---|
[c91f2d1b] | 683 |
|
---|
[402a18f] | 684 | return rc;
|
---|
[d5a720cf] | 685 | }
|
---|
| 686 |
|
---|
[6408be3] | 687 | /** Read sequential data from a block device.
|
---|
[d5a720cf] | 688 | *
|
---|
[15f3c3f] | 689 | * @param service_id Service ID of the block device.
|
---|
[d5a720cf] | 690 | * @param bufpos Pointer to the first unread valid offset within the
|
---|
| 691 | * communication buffer.
|
---|
| 692 | * @param buflen Pointer to the number of unread bytes that are ready in
|
---|
| 693 | * the communication buffer.
|
---|
| 694 | * @param pos Device position to be read.
|
---|
| 695 | * @param dst Destination buffer.
|
---|
| 696 | * @param size Size of the destination buffer.
|
---|
| 697 | * @param block_size Block size to be used for the transfer.
|
---|
| 698 | *
|
---|
| 699 | * @return EOK on success or a negative return code on failure.
|
---|
| 700 | */
|
---|
[15f3c3f] | 701 | int block_seqread(service_id_t service_id, size_t *bufpos, size_t *buflen,
|
---|
[ed903174] | 702 | aoff64_t *pos, void *dst, size_t size)
|
---|
[d5a720cf] | 703 | {
|
---|
[ed903174] | 704 | size_t offset = 0;
|
---|
[d5a720cf] | 705 | size_t left = size;
|
---|
[1ee00b7] | 706 | size_t block_size;
|
---|
| 707 | devcon_t *devcon;
|
---|
| 708 |
|
---|
[15f3c3f] | 709 | devcon = devcon_search(service_id);
|
---|
[d5a720cf] | 710 | assert(devcon);
|
---|
[1ee00b7] | 711 | block_size = devcon->pblock_size;
|
---|
[e1c88d5] | 712 |
|
---|
[a830611] | 713 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
[d5a720cf] | 714 | while (left > 0) {
|
---|
| 715 | size_t rd;
|
---|
| 716 |
|
---|
| 717 | if (*bufpos + left < *buflen)
|
---|
| 718 | rd = left;
|
---|
| 719 | else
|
---|
| 720 | rd = *buflen - *bufpos;
|
---|
| 721 |
|
---|
| 722 | if (rd > 0) {
|
---|
| 723 | /*
|
---|
| 724 | * Copy the contents of the communication buffer to the
|
---|
| 725 | * destination buffer.
|
---|
| 726 | */
|
---|
[a830611] | 727 | memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
|
---|
[d5a720cf] | 728 | offset += rd;
|
---|
| 729 | *bufpos += rd;
|
---|
| 730 | *pos += rd;
|
---|
| 731 | left -= rd;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[ed903174] | 734 | if (*bufpos == *buflen) {
|
---|
[d5a720cf] | 735 | /* Refill the communication buffer with a new block. */
|
---|
[6408be3] | 736 | int rc;
|
---|
| 737 |
|
---|
[1ee00b7] | 738 | rc = read_blocks(devcon, *pos / block_size, 1);
|
---|
[d68e4d5] | 739 | if (rc != EOK) {
|
---|
[a830611] | 740 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[6408be3] | 741 | return rc;
|
---|
[d68e4d5] | 742 | }
|
---|
[d5a720cf] | 743 |
|
---|
| 744 | *bufpos = 0;
|
---|
| 745 | *buflen = block_size;
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
[a830611] | 748 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
[d5a720cf] | 749 |
|
---|
| 750 | return EOK;
|
---|
[fc840d9] | 751 | }
|
---|
| 752 |
|
---|
[00b1d20e] | 753 | /** Read blocks directly from device (bypass cache).
|
---|
| 754 | *
|
---|
[15f3c3f] | 755 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 756 | * @param ba Address of first block (physical).
|
---|
[00b1d20e] | 757 | * @param cnt Number of blocks.
|
---|
| 758 | * @param src Buffer for storing the data.
|
---|
| 759 | *
|
---|
| 760 | * @return EOK on success or negative error code on failure.
|
---|
| 761 | */
|
---|
[15f3c3f] | 762 | int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
|
---|
[00b1d20e] | 763 | {
|
---|
| 764 | devcon_t *devcon;
|
---|
| 765 | int rc;
|
---|
| 766 |
|
---|
[15f3c3f] | 767 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 768 | assert(devcon);
|
---|
| 769 |
|
---|
| 770 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 771 |
|
---|
| 772 | rc = read_blocks(devcon, ba, cnt);
|
---|
| 773 | if (rc == EOK)
|
---|
| 774 | memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
|
---|
| 775 |
|
---|
| 776 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
| 777 |
|
---|
| 778 | return rc;
|
---|
| 779 | }
|
---|
| 780 |
|
---|
| 781 | /** Write blocks directly to device (bypass cache).
|
---|
| 782 | *
|
---|
[15f3c3f] | 783 | * @param service_id Service ID of the block device.
|
---|
[a6ba0c9] | 784 | * @param ba Address of first block (physical).
|
---|
[00b1d20e] | 785 | * @param cnt Number of blocks.
|
---|
| 786 | * @param src The data to be written.
|
---|
| 787 | *
|
---|
| 788 | * @return EOK on success or negative error code on failure.
|
---|
| 789 | */
|
---|
[15f3c3f] | 790 | int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
|
---|
[00b1d20e] | 791 | const void *data)
|
---|
| 792 | {
|
---|
| 793 | devcon_t *devcon;
|
---|
| 794 | int rc;
|
---|
| 795 |
|
---|
[15f3c3f] | 796 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 797 | assert(devcon);
|
---|
| 798 |
|
---|
| 799 | fibril_mutex_lock(&devcon->comm_area_lock);
|
---|
| 800 |
|
---|
| 801 | memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
|
---|
[dccf721] | 802 | rc = write_blocks(devcon, ba, cnt);
|
---|
[00b1d20e] | 803 |
|
---|
| 804 | fibril_mutex_unlock(&devcon->comm_area_lock);
|
---|
| 805 |
|
---|
| 806 | return rc;
|
---|
| 807 | }
|
---|
| 808 |
|
---|
| 809 | /** Get device block size.
|
---|
| 810 | *
|
---|
[15f3c3f] | 811 | * @param service_id Service ID of the block device.
|
---|
[00b1d20e] | 812 | * @param bsize Output block size.
|
---|
| 813 | *
|
---|
| 814 | * @return EOK on success or negative error code on failure.
|
---|
| 815 | */
|
---|
[15f3c3f] | 816 | int block_get_bsize(service_id_t service_id, size_t *bsize)
|
---|
[00b1d20e] | 817 | {
|
---|
| 818 | devcon_t *devcon;
|
---|
| 819 |
|
---|
[15f3c3f] | 820 | devcon = devcon_search(service_id);
|
---|
[00b1d20e] | 821 | assert(devcon);
|
---|
| 822 |
|
---|
[79ae36dd] | 823 | return get_block_size(devcon->sess, bsize);
|
---|
[00b1d20e] | 824 | }
|
---|
| 825 |
|
---|
[08232ee] | 826 | /** Get number of blocks on device.
|
---|
| 827 | *
|
---|
[15f3c3f] | 828 | * @param service_id Service ID of the block device.
|
---|
[08232ee] | 829 | * @param nblocks Output number of blocks.
|
---|
| 830 | *
|
---|
| 831 | * @return EOK on success or negative error code on failure.
|
---|
| 832 | */
|
---|
[15f3c3f] | 833 | int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
|
---|
[08232ee] | 834 | {
|
---|
[15f3c3f] | 835 | devcon_t *devcon = devcon_search(service_id);
|
---|
[08232ee] | 836 | assert(devcon);
|
---|
| 837 |
|
---|
[79ae36dd] | 838 | return get_num_blocks(devcon->sess, nblocks);
|
---|
[08232ee] | 839 | }
|
---|
| 840 |
|
---|
[e272949] | 841 | /** Read bytes directly from the device (bypass cache)
|
---|
| 842 | *
|
---|
[15f3c3f] | 843 | * @param service_id Service ID of the block device.
|
---|
[e272949] | 844 | * @param abs_offset Absolute offset in bytes where to start reading
|
---|
| 845 | * @param bytes Number of bytes to read
|
---|
| 846 | * @param data Buffer that receives the data
|
---|
| 847 | *
|
---|
| 848 | * @return EOK on success or negative error code on failure.
|
---|
| 849 | */
|
---|
[15f3c3f] | 850 | int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
|
---|
[e272949] | 851 | size_t bytes, void *data)
|
---|
| 852 | {
|
---|
| 853 | int rc;
|
---|
| 854 | size_t phys_block_size;
|
---|
| 855 | size_t buf_size;
|
---|
| 856 | void *buffer;
|
---|
| 857 | aoff64_t first_block;
|
---|
| 858 | aoff64_t last_block;
|
---|
| 859 | size_t blocks;
|
---|
| 860 | size_t offset;
|
---|
| 861 |
|
---|
[15f3c3f] | 862 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
[e272949] | 863 | if (rc != EOK) {
|
---|
| 864 | return rc;
|
---|
| 865 | }
|
---|
| 866 |
|
---|
[c4aa9cf] | 867 | /* calculate data position and required space */
|
---|
[e272949] | 868 | first_block = abs_offset / phys_block_size;
|
---|
| 869 | offset = abs_offset % phys_block_size;
|
---|
| 870 | last_block = (abs_offset + bytes - 1) / phys_block_size;
|
---|
| 871 | blocks = last_block - first_block + 1;
|
---|
| 872 | buf_size = blocks * phys_block_size;
|
---|
| 873 |
|
---|
[c4aa9cf] | 874 | /* read the data into memory */
|
---|
[e272949] | 875 | buffer = malloc(buf_size);
|
---|
| 876 | if (buffer == NULL) {
|
---|
| 877 | return ENOMEM;
|
---|
| 878 | }
|
---|
| 879 |
|
---|
[15f3c3f] | 880 | rc = block_read_direct(service_id, first_block, blocks, buffer);
|
---|
[e272949] | 881 | if (rc != EOK) {
|
---|
| 882 | free(buffer);
|
---|
| 883 | return rc;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[c4aa9cf] | 886 | /* copy the data from the buffer */
|
---|
[e272949] | 887 | memcpy(data, buffer + offset, bytes);
|
---|
| 888 | free(buffer);
|
---|
| 889 |
|
---|
| 890 | return EOK;
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[1ee00b7] | 893 | /** Read blocks from block device.
|
---|
[6408be3] | 894 | *
|
---|
| 895 | * @param devcon Device connection.
|
---|
[1ee00b7] | 896 | * @param ba Address of first block.
|
---|
| 897 | * @param cnt Number of blocks.
|
---|
[6408be3] | 898 | * @param src Buffer for storing the data.
|
---|
| 899 | *
|
---|
| 900 | * @return EOK on success or negative error code on failure.
|
---|
| 901 | */
|
---|
[ed903174] | 902 | static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
---|
[6408be3] | 903 | {
|
---|
| 904 | assert(devcon);
|
---|
[79ae36dd] | 905 |
|
---|
| 906 | async_exch_t *exch = async_exchange_begin(devcon->sess);
|
---|
| 907 | int rc = async_req_3_0(exch, BD_READ_BLOCKS, LOWER32(ba),
|
---|
[1ee00b7] | 908 | UPPER32(ba), cnt);
|
---|
[79ae36dd] | 909 | async_exchange_end(exch);
|
---|
| 910 |
|
---|
[16fc3c9] | 911 | if (rc != EOK) {
|
---|
[7e752b2] | 912 | printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
|
---|
| 913 | " from device handle %" PRIun "\n", rc, cnt, ba,
|
---|
[15f3c3f] | 914 | devcon->service_id);
|
---|
[16fc3c9] | 915 | #ifndef NDEBUG
|
---|
| 916 | stacktrace_print();
|
---|
| 917 | #endif
|
---|
| 918 | }
|
---|
[79ae36dd] | 919 |
|
---|
[1ee00b7] | 920 | return rc;
|
---|
[6408be3] | 921 | }
|
---|
| 922 |
|
---|
[1fbe064b] | 923 | /** Write block to block device.
|
---|
| 924 | *
|
---|
| 925 | * @param devcon Device connection.
|
---|
[1ee00b7] | 926 | * @param ba Address of first block.
|
---|
| 927 | * @param cnt Number of blocks.
|
---|
[1fbe064b] | 928 | * @param src Buffer containing the data to write.
|
---|
| 929 | *
|
---|
| 930 | * @return EOK on success or negative error code on failure.
|
---|
| 931 | */
|
---|
[ed903174] | 932 | static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
|
---|
[1fbe064b] | 933 | {
|
---|
| 934 | assert(devcon);
|
---|
[79ae36dd] | 935 |
|
---|
| 936 | async_exch_t *exch = async_exchange_begin(devcon->sess);
|
---|
| 937 | int rc = async_req_3_0(exch, BD_WRITE_BLOCKS, LOWER32(ba),
|
---|
[1ee00b7] | 938 | UPPER32(ba), cnt);
|
---|
[79ae36dd] | 939 | async_exchange_end(exch);
|
---|
| 940 |
|
---|
[16fc3c9] | 941 | if (rc != EOK) {
|
---|
[7e752b2] | 942 | printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
|
---|
[15f3c3f] | 943 | " to device handle %" PRIun "\n", rc, cnt, ba, devcon->service_id);
|
---|
[16fc3c9] | 944 | #ifndef NDEBUG
|
---|
| 945 | stacktrace_print();
|
---|
| 946 | #endif
|
---|
| 947 | }
|
---|
[79ae36dd] | 948 |
|
---|
[1ee00b7] | 949 | return rc;
|
---|
| 950 | }
|
---|
[1fbe064b] | 951 |
|
---|
[1ee00b7] | 952 | /** Get block size used by the device. */
|
---|
[79ae36dd] | 953 | static int get_block_size(async_sess_t *sess, size_t *bsize)
|
---|
[1ee00b7] | 954 | {
|
---|
[96b02eb9] | 955 | sysarg_t bs;
|
---|
[79ae36dd] | 956 |
|
---|
| 957 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 958 | int rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bs);
|
---|
| 959 | async_exchange_end(exch);
|
---|
| 960 |
|
---|
[1ee00b7] | 961 | if (rc == EOK)
|
---|
| 962 | *bsize = (size_t) bs;
|
---|
[79ae36dd] | 963 |
|
---|
[1ee00b7] | 964 | return rc;
|
---|
[1fbe064b] | 965 | }
|
---|
| 966 |
|
---|
[08232ee] | 967 | /** Get total number of blocks on block device. */
|
---|
[79ae36dd] | 968 | static int get_num_blocks(async_sess_t *sess, aoff64_t *nblocks)
|
---|
[08232ee] | 969 | {
|
---|
[79ae36dd] | 970 | sysarg_t nb_l;
|
---|
| 971 | sysarg_t nb_h;
|
---|
| 972 |
|
---|
| 973 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 974 | int rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
|
---|
| 975 | async_exchange_end(exch);
|
---|
| 976 |
|
---|
| 977 | if (rc == EOK)
|
---|
[ed903174] | 978 | *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
|
---|
[79ae36dd] | 979 |
|
---|
[08232ee] | 980 | return rc;
|
---|
| 981 | }
|
---|
| 982 |
|
---|
[f092718] | 983 | /** Convert logical block address to physical block address. */
|
---|
| 984 | static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
|
---|
| 985 | {
|
---|
| 986 | assert(devcon->cache != NULL);
|
---|
| 987 | return lba * devcon->cache->blocks_cluster;
|
---|
| 988 | }
|
---|
| 989 |
|
---|
[fc840d9] | 990 | /** @}
|
---|
| 991 | */
|
---|