source: mainline/uspace/lib/libblock/libblock.c@ e2b9a993

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2b9a993 was 64bc4b6, checked in by Jakub Jermar <jakub@…>, 16 years ago

Add block_cache_fini().

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