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

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

improve stack traces and assertions
reduce header files pollution

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