source: mainline/uspace/lib/block/libblock.c@ c3f95d8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c3f95d8 was c3f95d8, checked in by Martin Sucha <sucha14@…>, 14 years ago

Merged mainline changes

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