37,8 → 37,8 |
|
#include "libblock.h" |
#include "../../srv/vfs/vfs.h" |
#include "../../srv/rd/rd.h" |
#include <ipc/devmap.h> |
#include <ipc/bd.h> |
#include <ipc/services.h> |
#include <errno.h> |
#include <sys/mman.h> |
46,12 → 46,13 |
#include <ipc/ipc.h> |
#include <as.h> |
#include <assert.h> |
#include <futex.h> |
#include <libadt/list.h> |
#include <libadt/hash_table.h> |
#include <fibril_sync.h> |
#include <adt/list.h> |
#include <adt/hash_table.h> |
#include <mem.h> |
|
/** Lock protecting the device connection list */ |
static futex_t dcl_lock = FUTEX_INITIALIZER; |
static FIBRIL_MUTEX_INITIALIZE(dcl_lock); |
/** Device connection list head. */ |
static LIST_INITIALIZE(dcl_head); |
|
59,16 → 60,17 |
#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
|
typedef struct { |
futex_t lock; |
fibril_mutex_t lock; |
size_t block_size; /**< Block size. */ |
unsigned block_count; /**< Total number of blocks. */ |
hash_table_t block_hash; |
link_t free_head; |
enum cache_mode mode; |
} cache_t; |
|
typedef struct { |
link_t link; |
int dev_handle; |
dev_handle_t dev_handle; |
int dev_phone; |
void *com_area; |
size_t com_size; |
78,19 → 80,22 |
cache_t *cache; |
} devcon_t; |
|
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size, |
const void *src); |
|
static devcon_t *devcon_search(dev_handle_t dev_handle) |
{ |
link_t *cur; |
|
futex_down(&dcl_lock); |
fibril_mutex_lock(&dcl_lock); |
for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
if (devcon->dev_handle == dev_handle) { |
futex_up(&dcl_lock); |
fibril_mutex_unlock(&dcl_lock); |
return devcon; |
} |
} |
futex_up(&dcl_lock); |
fibril_mutex_unlock(&dcl_lock); |
return NULL; |
} |
|
114,25 → 119,25 |
devcon->bb_size = 0; |
devcon->cache = NULL; |
|
futex_down(&dcl_lock); |
fibril_mutex_lock(&dcl_lock); |
for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
devcon_t *d = list_get_instance(cur, devcon_t, link); |
if (d->dev_handle == dev_handle) { |
futex_up(&dcl_lock); |
fibril_mutex_unlock(&dcl_lock); |
free(devcon); |
return EEXIST; |
} |
} |
list_append(&devcon->link, &dcl_head); |
futex_up(&dcl_lock); |
fibril_mutex_unlock(&dcl_lock); |
return EOK; |
} |
|
static void devcon_remove(devcon_t *devcon) |
{ |
futex_down(&dcl_lock); |
fibril_mutex_lock(&dcl_lock); |
list_remove(&devcon->link); |
futex_up(&dcl_lock); |
fibril_mutex_unlock(&dcl_lock); |
} |
|
int block_init(dev_handle_t dev_handle, size_t com_size) |
146,9 → 151,8 |
if (!com_area) { |
return ENOMEM; |
} |
dev_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, |
DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
|
dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING); |
if (dev_phone < 0) { |
munmap(com_area, com_size); |
return dev_phone; |
250,7 → 254,8 |
.remove_callback = cache_remove_callback |
}; |
|
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks) |
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks, |
enum cache_mode mode) |
{ |
devcon_t *devcon = devcon_search(dev_handle); |
cache_t *cache; |
262,10 → 267,11 |
if (!cache) |
return ENOMEM; |
|
futex_initialize(&cache->lock, 1); |
fibril_mutex_initialize(&cache->lock); |
list_initialize(&cache->free_head); |
cache->block_size = size; |
cache->block_count = blocks; |
cache->mode = mode; |
|
if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
&cache_ops)) { |
284,10 → 290,10 |
|
static void block_initialize(block_t *b) |
{ |
futex_initialize(&b->lock, 1); |
fibril_mutex_initialize(&b->lock); |
b->refcnt = 1; |
b->dirty = false; |
rwlock_initialize(&b->contents_lock); |
fibril_rwlock_initialize(&b->contents_lock); |
link_initialize(&b->free_link); |
link_initialize(&b->hash_link); |
} |
316,7 → 322,7 |
assert(devcon->cache); |
|
cache = devcon->cache; |
futex_down(&cache->lock); |
fibril_mutex_lock(&cache->lock); |
l = hash_table_find(&cache->block_hash, &key); |
if (l) { |
/* |
323,11 → 329,11 |
* We found the block in the cache. |
*/ |
b = hash_table_get_instance(l, block_t, hash_link); |
futex_down(&b->lock); |
fibril_mutex_lock(&b->lock); |
if (b->refcnt++ == 0) |
list_remove(&b->free_link); |
futex_up(&b->lock); |
futex_up(&cache->lock); |
fibril_mutex_unlock(&b->lock); |
fibril_mutex_unlock(&cache->lock); |
} else { |
/* |
* The block was not found in the cache. |
378,8 → 384,8 |
* kill concurent operations on the cache while doing I/O on the |
* block. |
*/ |
futex_down(&b->lock); |
futex_up(&cache->lock); |
fibril_mutex_lock(&b->lock); |
fibril_mutex_unlock(&cache->lock); |
|
if (sync) { |
/* |
398,7 → 404,7 |
assert(rc == EOK); |
} |
|
futex_up(&b->lock); |
fibril_mutex_unlock(&b->lock); |
} |
return b; |
} |
413,13 → 419,14 |
{ |
devcon_t *devcon = devcon_search(block->dev_handle); |
cache_t *cache; |
int rc; |
|
assert(devcon); |
assert(devcon->cache); |
|
cache = devcon->cache; |
futex_down(&cache->lock); |
futex_down(&block->lock); |
fibril_mutex_lock(&cache->lock); |
fibril_mutex_lock(&block->lock); |
if (!--block->refcnt) { |
/* |
* Last reference to the block was dropped, put the block on the |
426,9 → 433,16 |
* free list. |
*/ |
list_append(&block->free_link, &cache->free_head); |
if (cache->mode != CACHE_MODE_WB && block->dirty) { |
rc = write_block(devcon, block->boff, block->size, |
block->data); |
assert(rc == EOK); |
|
block->dirty = false; |
} |
} |
futex_up(&block->lock); |
futex_up(&cache->lock); |
fibril_mutex_unlock(&block->lock); |
fibril_mutex_unlock(&cache->lock); |
} |
|
/** Read data from a block device. |
446,8 → 460,8 |
* @return EOK on success or a negative return code on failure. |
*/ |
int |
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
size_t size, size_t block_size) |
block_read(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, |
void *dst, size_t size, size_t block_size) |
{ |
off_t offset = 0; |
size_t left = size; |
474,13 → 488,13 |
left -= rd; |
} |
|
if (*bufpos == *buflen) { |
if (*bufpos == (off_t) *buflen) { |
/* Refill the communication buffer with a new block. */ |
ipcarg_t retval; |
int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
int rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, |
*pos / block_size, block_size, &retval); |
if ((rc != EOK) || (retval != EOK)) |
return (rc != EOK ? rc : retval); |
return (rc != EOK ? rc : (int) retval); |
|
*bufpos = 0; |
*buflen = block_size; |
490,5 → 504,31 |
return EOK; |
} |
|
/** Write block to block device. |
* |
* @param devcon Device connection. |
* @param boff Block index. |
* @param block_size Block size. |
* @param src Buffer containing the data to write. |
* |
* @return EOK on success or negative error code on failure. |
*/ |
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size, |
const void *src) |
{ |
ipcarg_t retval; |
int rc; |
|
assert(devcon); |
memcpy(devcon->com_area, src, block_size); |
|
rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, |
boff, block_size, &retval); |
if ((rc != EOK) || (retval != EOK)) |
return (rc != EOK ? rc : (int) retval); |
|
return EOK; |
} |
|
/** @} |
*/ |