mars/mars_buf.c

857 lines
23 KiB
C
Raw Normal View History

2010-07-07 14:09:16 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
// Buf brick (just for demonstration)
2010-07-30 05:46:22 +00:00
//#define BRICK_DEBUGGING
2010-07-23 11:55:18 +00:00
//#define MARS_DEBUGGING
2010-07-07 14:09:16 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
2010-07-23 11:55:18 +00:00
#include <linux/delay.h>
2010-07-07 14:09:16 +00:00
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_buf.h"
///////////////////////// own helper functions ////////////////////////
static inline int buf_hash(struct buf_brick *brick, loff_t pos)
{
return (pos >> brick->backing_order) % MARS_BUF_HASH_MAX;
}
static struct buf_head *hash_find(struct buf_brick *brick, loff_t pos)
{
int hash = buf_hash(brick, pos);
struct list_head *start = &brick->cache_anchors[hash];
struct list_head *tmp;
struct buf_head *res;
for (tmp = start->next; ; tmp = tmp->next) {
if (tmp == start)
return NULL;
res = container_of(tmp, struct buf_head, bf_hash_head);
if (res->bf_pos == pos)
break;
}
return res;
}
static inline void hash_insert(struct buf_brick *brick, struct buf_head *elem)
{
int hash = buf_hash(brick, elem->bf_pos);
struct list_head *start = &brick->cache_anchors[hash];
list_add(&elem->bf_hash_head, start);
}
static inline void free_buf(struct buf_brick *brick, struct buf_head *bf)
{
free_pages((unsigned long)bf->bf_data, brick->backing_order);
kfree(bf);
}
/* brick->buf_lock must be held
*/
2010-07-30 05:46:22 +00:00
static inline void prune_cache(struct buf_brick *brick, int max_count, unsigned long *flags)
2010-07-07 14:09:16 +00:00
{
2010-07-30 05:46:22 +00:00
#if 1
return;
#endif
while (brick->alloc_count > max_count) {
2010-07-07 14:09:16 +00:00
struct buf_head *bf;
if (list_empty(&brick->free_anchor))
break;
bf = container_of(brick->free_anchor.next, struct buf_head, bf_lru_head);
list_del(&bf->bf_lru_head);
brick->current_count--;
brick->alloc_count--;
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, *flags);
2010-07-07 14:09:16 +00:00
free_buf(brick, bf);
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, *flags);
2010-07-07 14:09:16 +00:00
}
}
static struct mars_io_object_layout *buf_init_object_layout(struct buf_output *output)
{
const int layout_size = 1024;
const int max_aspects = 16;
struct mars_io_object_layout *res;
int status;
void *data = kzalloc(layout_size, GFP_KERNEL);
if (!data) {
MARS_ERR("emergency, cannot allocate object_layout!\n");
return NULL;
}
res = mars_io_init_object_layout(data, layout_size, max_aspects, &mars_io_type);
if (unlikely(!res)) {
MARS_ERR("emergency, cannot init object_layout!\n");
goto err_free;
}
status = output->ops->make_object_layout(output, (struct generic_object_layout*)res);
if (unlikely(status < 0)) {
MARS_ERR("emergency, cannot add aspects to object_layout!\n");
goto err_free;
}
MARS_INF("OK, object_layout init succeeded.\n");
return res;
err_free:
kfree(res);
return NULL;
}
static inline int get_info(struct buf_brick *brick)
{
struct buf_input *input = brick->inputs[0];
int status = GENERIC_INPUT_CALL(input, mars_get_info, &brick->base_info);
if (status >= 0) {
brick->got_info = 1;
}
return status;
}
2010-07-30 05:46:22 +00:00
/* Convert from arbitrary/odd kernel address/length to struct page,
2010-07-07 14:09:16 +00:00
* create bio from it, round up/down to full sectors.
2010-07-30 05:46:22 +00:00
* return the length (may be smaller or even larger than requested)
2010-07-07 14:09:16 +00:00
*/
2010-07-30 05:46:22 +00:00
static int make_bio(struct buf_brick *brick, struct bio **_bio, void *data, loff_t pos, int len)
2010-07-07 14:09:16 +00:00
{
2010-07-30 05:46:22 +00:00
unsigned long long sector;
2010-07-07 14:09:16 +00:00
int sector_offset;
int page_offset;
2010-07-30 05:46:22 +00:00
int page_len;
2010-07-07 14:09:16 +00:00
int bvec_count;
int status;
int i;
struct page *page;
struct bio *bio = NULL;
struct block_device *bdev;
if (unlikely(!brick->got_info)) {
struct request_queue *q;
status = get_info(brick);
if (status < 0)
goto out;
2010-07-23 11:55:18 +00:00
bdev = brick->base_info.backing_file->f_mapping->host->i_sb->s_bdev;
2010-07-07 14:09:16 +00:00
q = bdev_get_queue(bdev);
brick->bvec_max = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
2010-07-23 11:55:18 +00:00
} else {
bdev = brick->base_info.backing_file->f_mapping->host->i_sb->s_bdev;
2010-07-07 14:09:16 +00:00
}
2010-07-30 05:46:22 +00:00
if (unlikely(len <= 0)) {
MARS_ERR("bad bio len %d\n", len);
status = -EINVAL;
goto out;
}
sector = pos >> 9; // TODO: make dynamic
2010-07-07 14:09:16 +00:00
sector_offset = pos & ((1 << 9) - 1); // TODO: make dynamic
2010-07-30 05:46:22 +00:00
2010-07-07 14:09:16 +00:00
// round down to start of first sector
data -= sector_offset;
2010-07-23 11:55:18 +00:00
len += sector_offset;
2010-07-07 14:09:16 +00:00
pos -= sector_offset;
2010-07-30 05:46:22 +00:00
// round up to full sector
len = (((len - 1) >> 9) + 1) << 9; // TODO: make dynamic
// map onto pages. TODO: allow higher-order pages (performance!)
2010-07-07 14:09:16 +00:00
page_offset = pos & (PAGE_SIZE - 1);
2010-07-30 05:46:22 +00:00
page_len = len + page_offset;
bvec_count = (page_len - 1) / PAGE_SIZE + 1;
2010-07-07 14:09:16 +00:00
if (bvec_count > brick->bvec_max)
bvec_count = brick->bvec_max;
bio = bio_alloc(GFP_KERNEL, bvec_count);
status = -ENOMEM;
if (!bio)
goto out;
status = 0;
for (i = 0; i < bvec_count && len > 0; i++) {
int myrest = PAGE_SIZE - page_offset;
int mylen = len;
2010-07-30 05:46:22 +00:00
2010-07-07 14:09:16 +00:00
if (mylen > myrest)
mylen = myrest;
page = virt_to_page(data);
bio->bi_io_vec[i].bv_page = page;
2010-07-30 05:46:22 +00:00
bio->bi_io_vec[i].bv_len = mylen;
2010-07-07 14:09:16 +00:00
bio->bi_io_vec[i].bv_offset = page_offset;
data += mylen;
len -= mylen;
status += mylen;
page_offset = 0;
}
2010-07-30 05:46:22 +00:00
if (unlikely(len)) {
bio_put(bio);
bio = NULL;
MARS_ERR("computation of bvec_count %d was wrong, diff=%d\n", bvec_count, len);
status = -EIO;
goto out;
}
2010-07-07 14:09:16 +00:00
bio->bi_vcnt = i;
bio->bi_idx = 0;
2010-07-30 05:46:22 +00:00
bio->bi_size = status;
2010-07-07 14:09:16 +00:00
bio->bi_sector = sector;
bio->bi_bdev = bdev;
2010-07-23 11:55:18 +00:00
bio->bi_private = NULL; // must be filled in later
bio->bi_end_io = NULL; // must be filled in later
2010-07-30 05:46:22 +00:00
bio->bi_rw = 0; // must be filled in later
// ignore rounding-down on return
2010-07-23 11:55:18 +00:00
if (status >= sector_offset)
status -= sector_offset;
2010-07-07 14:09:16 +00:00
out:
*_bio = bio;
return status;
}
////////////////// own brick / input / output operations //////////////////
static int buf_io(struct buf_output *output, struct mars_io_object *mio)
{
struct buf_input *input = output->brick->inputs[0];
return GENERIC_INPUT_CALL(input, mars_io, mio);
}
static int buf_get_info(struct buf_output *output, struct mars_info *info)
{
struct buf_input *input = output->brick->inputs[0];
return GENERIC_INPUT_CALL(input, mars_get_info, info);
}
static int buf_buf_get(struct buf_output *output, struct mars_buf_object **_mbuf, struct mars_buf_object_layout *buf_layout, loff_t pos, int len)
{
struct buf_brick *brick = output->brick;
void *data;
struct mars_buf_object *mbuf;
struct buf_mars_buf_aspect *mbuf_a;
struct buf_head *bf;
loff_t base_pos;
2010-07-30 05:46:22 +00:00
int base_offset;
unsigned long flags;
2010-07-07 14:09:16 +00:00
int status = -EILSEQ;
data = kzalloc(buf_layout->object_size, GFP_KERNEL);
if (!data)
return -ENOMEM;
mbuf = mars_buf_construct(data, buf_layout);
if (!mbuf)
goto err_free;
2010-07-23 11:55:18 +00:00
mbuf_a = buf_mars_buf_get_aspect(output, mbuf);
2010-07-07 14:09:16 +00:00
if (!mbuf_a)
goto err_free;
mbuf->buf_pos = pos;
2010-07-30 05:46:22 +00:00
base_pos = pos & ~(loff_t)(brick->backing_size - 1);
2010-07-07 14:09:16 +00:00
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-07 14:09:16 +00:00
bf = hash_find(brick, base_pos);
if (!bf) {
2010-07-23 11:55:18 +00:00
MARS_DBG("buf_get() hash nothing found\n");
2010-07-07 14:09:16 +00:00
if (unlikely(list_empty(&brick->free_anchor))) {
struct buf_head *test_bf;
2010-07-23 11:55:18 +00:00
MARS_DBG("buf_get() alloc new buf_head\n");
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
2010-07-07 14:09:16 +00:00
status = -ENOMEM;
bf = kzalloc(sizeof(struct buf_head), GFP_KERNEL);
if (!bf)
goto err_free;
bf->bf_data = (void*)__get_free_pages(GFP_KERNEL, brick->backing_order);
if (!bf->bf_data)
goto err_free2;
bf->bf_brick = brick;
2010-07-23 11:55:18 +00:00
atomic_set(&bf->bf_bio_count, 0);
//INIT_LIST_HEAD(&bf->bf_mbuf_anchor);
2010-07-07 14:09:16 +00:00
INIT_LIST_HEAD(&bf->bf_lru_head);
INIT_LIST_HEAD(&bf->bf_hash_head);
2010-07-23 11:55:18 +00:00
INIT_LIST_HEAD(&bf->bf_io_pending_anchor);
2010-07-07 14:09:16 +00:00
INIT_LIST_HEAD(&bf->bf_again_write_pending_anchor);
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-07 14:09:16 +00:00
brick->alloc_count++;
/* during the open lock, somebody might have raced
* against us at the same base_pos...
*/
test_bf = hash_find(brick, base_pos);
if (unlikely(test_bf)) {
free_buf(brick, bf);
bf = test_bf;
}
} else {
bf = container_of(brick->free_anchor.next, struct buf_head, bf_lru_head);
}
2010-07-23 11:55:18 +00:00
MARS_DBG("buf_get() bf=%p\n", bf);
2010-07-07 14:09:16 +00:00
bf->bf_pos = base_pos;
bf->bf_flags = 0;
2010-07-23 11:55:18 +00:00
atomic_set(&bf->bf_count, 0);
2010-07-07 14:09:16 +00:00
hash_insert(brick, bf);
brick->current_count++;
}
2010-07-23 11:55:18 +00:00
mbuf_a->bfa_bf = bf;
atomic_inc(&bf->bf_count);
MARS_DBG("buf_get() bf=%p initial bf_count=%d\n", bf, atomic_read(&bf->bf_count));
2010-07-07 14:09:16 +00:00
list_del_init(&bf->bf_lru_head);
mbuf->buf_flags = bf->bf_flags;
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
base_offset = (pos - base_pos);
if (base_offset < 0 || base_offset >= brick->backing_size) {
MARS_ERR("bad base_offset %d\n", base_offset);
}
mbuf->buf_data = bf->bf_data + base_offset;
mbuf->buf_len = brick->backing_size - base_offset;
2010-07-07 14:09:16 +00:00
*_mbuf = mbuf;
if (len > mbuf->buf_len)
len = mbuf->buf_len;
return len;
err_free2:
kfree(bf);
err_free:
kfree(data);
return status;
}
2010-07-23 11:55:18 +00:00
static int _buf_buf_put(struct buf_head *bf)
2010-07-07 14:09:16 +00:00
{
2010-07-23 11:55:18 +00:00
struct buf_brick *brick;
2010-07-30 05:46:22 +00:00
unsigned long flags;
2010-07-07 14:09:16 +00:00
2010-07-23 11:55:18 +00:00
MARS_DBG("_buf_buf_put() bf=%p bf_count=%d\n", bf, atomic_read(&bf->bf_count));
if (!atomic_dec_and_test(&bf->bf_count))
return 0;
2010-07-07 14:09:16 +00:00
2010-07-23 11:55:18 +00:00
MARS_DBG("_buf_buf_put() ZERO_COUNT\n");
2010-07-07 14:09:16 +00:00
2010-07-23 11:55:18 +00:00
brick = bf->bf_brick;
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
if (atomic_read(&bf->bf_count) <= 0) {
2010-07-07 14:09:16 +00:00
struct list_head *where = &brick->lru_anchor;
2010-07-30 05:46:22 +00:00
if (bf->bf_flags & (MARS_BUF_READING | MARS_BUF_WRITING)) {
MARS_ERR("bad bf_flags %d\n", bf->bf_flags);
}
2010-07-07 14:09:16 +00:00
if (unlikely(!(bf->bf_flags & MARS_BUF_UPTODATE))) {
list_del_init(&bf->bf_hash_head);
brick->current_count--;
where = &brick->free_anchor;
}
list_del(&bf->bf_lru_head);
list_add(&bf->bf_lru_head, where);
}
2010-07-23 11:55:18 +00:00
2010-07-07 14:09:16 +00:00
// lru freeing
while (brick->current_count > brick->max_count) {
if (list_empty(&brick->lru_anchor))
break;
bf = container_of(brick->lru_anchor.prev, struct buf_head, bf_lru_head);
list_del(&bf->bf_lru_head);
list_del_init(&bf->bf_hash_head);
brick->current_count--;
list_add(&bf->bf_lru_head, &brick->free_anchor);
}
2010-07-23 11:55:18 +00:00
2010-07-30 05:46:22 +00:00
prune_cache(brick, brick->max_count, &flags);
2010-07-23 11:55:18 +00:00
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
2010-07-07 14:09:16 +00:00
return 0;
}
2010-07-23 11:55:18 +00:00
static int buf_buf_put(struct buf_output *output, struct mars_buf_object *mbuf)
2010-07-07 14:09:16 +00:00
{
struct buf_mars_buf_aspect *mbuf_a;
struct buf_head *bf;
2010-07-23 11:55:18 +00:00
mbuf_a = buf_mars_buf_get_aspect(output, mbuf);
2010-07-07 14:09:16 +00:00
if (!mbuf_a)
return -EILSEQ;
2010-07-23 11:55:18 +00:00
bf = mbuf_a->bfa_bf;
MARS_DBG("buf_buf_put() mbuf=%p mbuf_a=%p bf=%p\n", mbuf, mbuf_a, bf);
return _buf_buf_put(bf);
}
2010-07-07 14:09:16 +00:00
2010-07-30 05:46:22 +00:00
static int _buf_endio(struct mars_io_object *mio, int error)
2010-07-23 11:55:18 +00:00
{
struct bio *bio = mio->orig_bio;
MARS_DBG("_buf_endio() mio=%p bio=%p\n", mio, bio);
if (bio) {
2010-07-30 05:46:22 +00:00
if (unlikely(bio->bi_size && !error))
error = -EIO;
if (error < 0) {
MARS_ERR("_buf_endio() error=%d bi_size=%d\n", error, bio->bi_size);
2010-07-07 14:09:16 +00:00
}
2010-07-30 05:46:22 +00:00
bio_endio(bio, error);
mio->orig_bio = NULL;
bio_put(bio);
2010-07-23 11:55:18 +00:00
} // else lower layers have already signalled the orig_bio
kfree(mio);
return 0;
}
static void _buf_bio_callback(struct bio *bio, int code);
2010-07-07 14:09:16 +00:00
2010-07-30 05:46:22 +00:00
static int _buf_make_bios(struct buf_brick *brick, struct buf_head *bf, void *start_data, loff_t start_pos, int start_len, int rw)
2010-07-23 11:55:18 +00:00
{
2010-07-30 05:46:22 +00:00
#if 1
loff_t bf_end = bf->bf_pos + brick->backing_size;
loff_t end_pos;
if (start_pos < bf->bf_pos || start_pos >= bf_end) {
MARS_ERR("bad start_pos %llu (%llu ... %llu)\n", start_pos, bf->bf_pos, bf_end);
return -EINVAL;
}
end_pos = start_pos + start_len;
if (end_pos <= bf->bf_pos || end_pos > bf_end) {
MARS_ERR("bad end_pos %llu (%llu ... %llu)\n", end_pos, bf->bf_pos, bf_end);
return -EINVAL;
}
if (!start_data) {
MARS_ERR("bad start_data\n");
return -EINVAL;
}
if (start_len <= 0) {
MARS_ERR("bad start_len %d\n", start_len);
return -EINVAL;
}
#endif
2010-07-23 11:55:18 +00:00
while (start_len > 0) {
struct buf_input *input = brick->inputs[0];
2010-07-07 14:09:16 +00:00
struct mars_io_object *data;
struct mars_io_object *mio;
2010-07-23 11:55:18 +00:00
struct buf_mars_io_aspect *mio_a;
2010-07-07 14:09:16 +00:00
struct bio *bio = NULL;
2010-07-23 11:55:18 +00:00
int len;
2010-07-07 14:09:16 +00:00
int status;
if (unlikely(!brick->mio_layout)) {
2010-07-23 11:55:18 +00:00
brick->mio_layout = buf_init_object_layout(brick->outputs[0]);
2010-07-07 14:09:16 +00:00
if (!brick->mio_layout)
return -ENOMEM;
}
data = kzalloc(brick->mio_layout->object_size, GFP_KERNEL);
if (!data)
return -ENOMEM;
mio = mars_io_construct(data, brick->mio_layout);
if (!mio)
goto err_free;
2010-07-23 11:55:18 +00:00
mio_a = buf_mars_io_get_aspect(brick->outputs[0], mio);
if (!mio_a)
goto err_free2;
2010-07-30 05:46:22 +00:00
len = make_bio(brick, &bio, start_data, start_pos, start_len);
2010-07-23 11:55:18 +00:00
if (len < 0 || !bio)
2010-07-07 14:09:16 +00:00
goto err_free2;
2010-07-23 11:55:18 +00:00
mio_a->mia_bf = bf;
atomic_inc(&bf->bf_bio_count);
bio->bi_private = mio_a;
bio->bi_end_io = _buf_bio_callback;
2010-07-30 05:46:22 +00:00
bio->bi_rw = rw;
2010-07-23 11:55:18 +00:00
mio->orig_bio = bio;
mio->mars_endio = _buf_endio;
MARS_DBG("starting buf IO mio=%p bio=%p len=%d bf=%p bf_count=%d bf_bio_count=%d\n", mio, bio, len, bf, atomic_read(&bf->bf_count), atomic_read(&bf->bf_bio_count));
#if 1
status = GENERIC_INPUT_CALL(input, mars_io, mio);
if (status < 0)
goto err_free3;
#else
// fake IO for testing
bio->bi_size = 0;
mio->mars_endio(mio);
#endif
2010-07-30 05:46:22 +00:00
start_data += len;
start_pos += len;
2010-07-23 11:55:18 +00:00
start_len -= len;
continue;
err_free3:
atomic_dec(&bf->bf_bio_count);
bio_put(bio);
2010-07-07 14:09:16 +00:00
err_free2:
kfree(mio);
err_free:
kfree(data);
2010-07-30 05:46:22 +00:00
MARS_ERR("cannot start buf IO\n");
2010-07-07 14:09:16 +00:00
return -EIO;
}
return 0;
}
2010-07-23 11:55:18 +00:00
static void _buf_bio_callback(struct bio *bio, int code)
{
struct buf_mars_io_aspect *mio_a;
struct buf_head *bf;
struct buf_brick *brick;
void *start_data = NULL;
loff_t start_pos = 0;
int start_len = 0;
int old_flags;
2010-07-30 05:46:22 +00:00
unsigned long flags;
2010-07-23 11:55:18 +00:00
mio_a = bio->bi_private;
bf = mio_a->mia_bf;
MARS_DBG("_buf_bio_callback() mio=%p bio=%p bf=%p bf_count=%d bf_bio_count=%d code=%d\n", mio_a->object, bio, bf, atomic_read(&bf->bf_count), atomic_read(&bf->bf_bio_count), code);
if (unlikely(mio_a->mia_end_io_called)) {
MARS_ERR("Oops, somebody called us twice on the same bio. I'm not amused.\n");
msleep(5000);
return;
} else {
mio_a->mia_end_io_called = true;
}
if (code < 0) {
2010-07-30 05:46:22 +00:00
MARS_ERR("BIO ERROR %d (old=%d)\n", code, bf->bf_bio_status);
2010-07-23 11:55:18 +00:00
// this can race, but we don't worry about the exact error code
bf->bf_bio_status = code;
}
if (!atomic_dec_and_test(&bf->bf_bio_count))
return;
MARS_DBG("_buf_bio_callback() ZERO_COUNT mio=%p bio=%p bf=%p code=%d\n", mio_a->object, bio, bf, code);
brick = bf->bf_brick;
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
// signal success by calling all callbacks.
while (!list_empty(&bf->bf_io_pending_anchor)) {
struct buf_mars_buf_callback_aspect *mbuf_cb_a = container_of(bf->bf_io_pending_anchor.next, struct buf_mars_buf_callback_aspect, bfc_pending_head);
struct mars_buf_callback_object *mbuf_cb = mbuf_cb_a->object;
2010-07-30 05:46:22 +00:00
if (mbuf_cb_a->bfc_bfa->bfa_bf != bf) {
MARS_ERR("bad pointers %p != %p\n", mbuf_cb_a->bfc_bfa->bfa_bf, bf);
}
2010-07-23 11:55:18 +00:00
mbuf_cb->cb_error = bf->bf_bio_status;
list_del(&mbuf_cb_a->bfc_pending_head);
/* drop normal refcount.
* full _buf_buf_put() not needed, see below. */
atomic_dec(&bf->bf_count);
MARS_DBG("_buf_bio_callback() bf=%p now bf_count=%d\n", bf, atomic_read(&bf->bf_count));
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
mbuf_cb->cb_buf_endio(mbuf_cb);
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
}
old_flags = bf->bf_flags;
if (!bf->bf_bio_status && (old_flags & MARS_BUF_READING)) {
bf->bf_flags |= MARS_BUF_UPTODATE;
}
// clear the flags. may be re-enabled later.
bf->bf_flags &= ~(MARS_BUF_READING | MARS_BUF_WRITING);
/* move pending jobs to work.
* this is in essence an automatic restart mechanism.
*/
while (!list_empty(&bf->bf_again_write_pending_anchor)) {
struct buf_mars_buf_callback_aspect *mbuf_cb_a = container_of(bf->bf_again_write_pending_anchor.next, struct buf_mars_buf_callback_aspect, bfc_pending_head);
struct mars_buf_object *mbuf = mbuf_cb_a->bfc_bfa->object;
2010-07-30 05:46:22 +00:00
if (mbuf_cb_a->bfc_bfa->bfa_bf != bf) {
MARS_ERR("bad pointers %p != %p\n", mbuf_cb_a->bfc_bfa->bfa_bf, bf);
}
2010-07-23 11:55:18 +00:00
list_del(&mbuf_cb_a->bfc_pending_head);
list_add_tail(&mbuf_cb_a->bfc_pending_head, &bf->bf_io_pending_anchor);
// re-enable flags
bf->bf_flags |= MARS_BUF_WRITING;
bf->bf_bio_status = 0;
if (!start_len) {
2010-07-30 05:46:22 +00:00
// first time: only flush the accected area
2010-07-23 11:55:18 +00:00
start_data = mbuf->buf_data;
start_pos = mbuf->buf_pos;
start_len = mbuf->buf_len;
} else if (start_data != mbuf->buf_data ||
start_pos != mbuf->buf_pos ||
start_len != mbuf->buf_len) {
2010-07-30 05:46:22 +00:00
// another time: flush the whole buffer
2010-07-23 11:55:18 +00:00
start_data = bf->bf_data;
start_pos = bf->bf_pos;
start_len = brick->backing_size;
}
}
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
if (start_len) {
2010-07-30 05:46:22 +00:00
MARS_DBG("ATTENTION %d\n", start_len);
2010-07-23 11:55:18 +00:00
// in this case, the extra refcount is kept => nothing to do
2010-07-30 05:46:22 +00:00
_buf_make_bios(brick, bf, start_data, start_pos, start_len, WRITE);
2010-07-23 11:55:18 +00:00
} else if (old_flags & (MARS_BUF_READING | MARS_BUF_WRITING)) {
// drop extra refcount for pending IO
_buf_buf_put(bf);
}
}
static int buf_buf_io(struct buf_output *output, struct mars_buf_callback_object *mbuf_cb)
{
struct buf_brick *brick = output->brick;
struct mars_buf_object *mbuf = mbuf_cb->cb_mbuf;
struct buf_mars_buf_aspect *mbuf_a;
struct buf_mars_buf_callback_aspect *mbuf_cb_a;
struct buf_head *bf;
void *start_data = NULL;
loff_t start_pos = 0;
int start_len = 0;
2010-07-30 05:46:22 +00:00
unsigned long flags;
2010-07-23 11:55:18 +00:00
if (!mbuf) {
MARS_ERR("internal problem: forgotten to supply mbuf\n");
return -EILSEQ;
}
mbuf_cb_a = buf_mars_buf_callback_get_aspect(output, mbuf_cb);
if (!mbuf_cb_a) {
MARS_ERR("internal problem: mbuf_cb aspect does not work\n");
return -EILSEQ;
}
mbuf_a = buf_mars_buf_get_aspect(output, mbuf);
if (!mbuf_a) {
MARS_ERR("internal problem: mbuf aspect does not work\n");
return -EILSEQ;
}
mbuf_cb_a->bfc_bfa = mbuf_a;
bf = mbuf_a->bfa_bf;
2010-07-30 05:46:22 +00:00
traced_lock(&brick->buf_lock, flags);
2010-07-23 11:55:18 +00:00
if (mbuf_cb->cb_rw) { // WRITE
2010-07-30 05:46:22 +00:00
if (bf->bf_flags & MARS_BUF_READING) {
MARS_ERR("bad bf_flags %d\n", bf->bf_flags);
}
2010-07-23 11:55:18 +00:00
if (!(bf->bf_flags & MARS_BUF_WRITING)) {
2010-07-30 05:46:22 +00:00
// by definition, a writeout buffer is uptodate
bf->bf_flags |= (MARS_BUF_WRITING | MARS_BUF_UPTODATE);
2010-07-23 11:55:18 +00:00
bf->bf_bio_status = 0;
// grab an extra refcount for pending IO
atomic_inc(&bf->bf_count);
MARS_DBG("buf_buf_io() bf=%p extra bf_count=%d\n", bf, atomic_read(&bf->bf_count));
2010-07-30 05:46:22 +00:00
#if 0
2010-07-23 11:55:18 +00:00
start_data = mbuf->buf_data;
start_pos = mbuf->buf_pos;
start_len = mbuf->buf_len;
2010-07-30 05:46:22 +00:00
#else
start_data = (void*)((unsigned long)mbuf->buf_data & ~(unsigned long)(brick->backing_size - 1));
start_pos = mbuf->buf_pos & ~(loff_t)(brick->backing_size - 1);
start_len = brick->backing_size;
#endif
2010-07-23 11:55:18 +00:00
list_add(&mbuf_cb_a->bfc_pending_head, &bf->bf_io_pending_anchor);
} else {
list_add(&mbuf_cb_a->bfc_pending_head, &bf->bf_again_write_pending_anchor);
2010-07-30 05:46:22 +00:00
MARS_DBG("postponing %lld %d\n", mbuf->buf_pos, mbuf->buf_len);
2010-07-23 11:55:18 +00:00
}
} else { // READ
if (bf->bf_flags & (MARS_BUF_UPTODATE | MARS_BUF_WRITING)) {
2010-07-30 05:46:22 +00:00
goto already_done;
2010-07-23 11:55:18 +00:00
}
if (!(bf->bf_flags & MARS_BUF_READING)) {
bf->bf_flags |= MARS_BUF_READING;
bf->bf_bio_status = 0;
// grab an extra refcount for pending IO
atomic_inc(&bf->bf_count);
MARS_DBG("buf_buf_io() bf=%p extra bf_count=%d\n", bf, atomic_read(&bf->bf_count));
2010-07-30 05:46:22 +00:00
// always read the whole buffer
start_data = (void*)((unsigned long)mbuf->buf_data & ~(unsigned long)(brick->backing_size - 1));
start_pos = mbuf->buf_pos & ~(loff_t)(brick->backing_size - 1);
2010-07-23 11:55:18 +00:00
start_len = brick->backing_size;
}
list_add(&mbuf_cb_a->bfc_pending_head, &bf->bf_io_pending_anchor);
}
// grab normal refcount for each mbuf_cb
atomic_inc(&bf->bf_count);
MARS_DBG("buf_buf_io() bf=%p normal bf_count=%d\n", bf, atomic_read(&bf->bf_count));
2010-07-30 05:46:22 +00:00
traced_unlock(&brick->buf_lock, flags);
if (!start_len)
return 0;
return _buf_make_bios(brick, bf, start_data, start_pos, start_len, mbuf_cb->cb_rw);
already_done:
traced_unlock(&brick->buf_lock, flags);
return mbuf_cb->cb_buf_endio(mbuf_cb);
2010-07-23 11:55:18 +00:00
}
2010-07-07 14:09:16 +00:00
//////////////// object / aspect constructors / destructors ///////////////
2010-07-23 11:55:18 +00:00
static int buf_mars_io_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
2010-07-07 14:09:16 +00:00
{
struct buf_mars_io_aspect *ini = (void*)_ini;
2010-07-23 11:55:18 +00:00
ini->mia_bf = NULL;
ini->mia_end_io_called = false;
2010-07-07 14:09:16 +00:00
return 0;
}
2010-07-23 11:55:18 +00:00
static int buf_mars_buf_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
2010-07-07 14:09:16 +00:00
{
struct buf_mars_buf_aspect *ini = (void*)_ini;
2010-07-23 11:55:18 +00:00
ini->bfa_bf = NULL;
return 0;
}
static int buf_mars_buf_callback_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
{
struct buf_mars_buf_callback_aspect *ini = (void*)_ini;
INIT_LIST_HEAD(&ini->bfc_pending_head);
ini->bfc_bfa = NULL;
2010-07-07 14:09:16 +00:00
return 0;
}
2010-07-23 11:55:18 +00:00
MARS_MAKE_STATICS(buf);
2010-07-07 14:09:16 +00:00
////////////////////// brick constructors / destructors ////////////////////
static int buf_brick_construct(struct buf_brick *brick)
{
int i;
brick->backing_order = 5; // TODO: make this configurable
brick->backing_size = PAGE_SIZE << brick->backing_order;
brick->max_count = 32; // TODO: make this configurable
brick->current_count = 0;
brick->alloc_count = 0;
spin_lock_init(&brick->buf_lock);
INIT_LIST_HEAD(&brick->free_anchor);
INIT_LIST_HEAD(&brick->lru_anchor);
for (i = 0; i < MARS_BUF_HASH_MAX; i++) {
INIT_LIST_HEAD(&brick->cache_anchors[i]);
}
return 0;
}
static int buf_output_construct(struct buf_output *output)
{
return 0;
}
2010-07-30 05:46:22 +00:00
static int buf_brick_destruct(struct buf_brick *brick)
{
unsigned long flags;
traced_lock(&brick->buf_lock, flags);
prune_cache(brick, 0, &flags);
traced_unlock(&brick->buf_lock, flags);
return 0;
}
2010-07-07 14:09:16 +00:00
///////////////////////// static structs ////////////////////////
static struct buf_brick_ops buf_brick_ops = {
};
static struct buf_output_ops buf_output_ops = {
.make_object_layout = buf_make_object_layout,
.mars_io = buf_io,
.mars_get_info = buf_get_info,
.mars_buf_get = buf_buf_get,
.mars_buf_put = buf_buf_put,
.mars_buf_io = buf_buf_io,
};
2010-07-23 11:55:18 +00:00
static const struct buf_input_type buf_input_type = {
2010-07-07 14:09:16 +00:00
.type_name = "buf_input",
.input_size = sizeof(struct buf_input),
};
2010-07-23 11:55:18 +00:00
static const struct buf_input_type *buf_input_types[] = {
2010-07-07 14:09:16 +00:00
&buf_input_type,
};
2010-07-23 11:55:18 +00:00
static const struct buf_output_type buf_output_type = {
2010-07-07 14:09:16 +00:00
.type_name = "buf_output",
.output_size = sizeof(struct buf_output),
.master_ops = &buf_output_ops,
.output_construct = &buf_output_construct,
2010-07-30 05:46:22 +00:00
.aspect_types = buf_aspect_types,
.layout_code = {
[BRICK_OBJ_MARS_IO] = LAYOUT_ALL,
[BRICK_OBJ_MARS_BUF] = LAYOUT_NONE,
[BRICK_OBJ_MARS_BUF_CALLBACK] = LAYOUT_NONE,
}
2010-07-07 14:09:16 +00:00
};
2010-07-23 11:55:18 +00:00
static const struct buf_output_type *buf_output_types[] = {
2010-07-07 14:09:16 +00:00
&buf_output_type,
};
2010-07-23 11:55:18 +00:00
const struct buf_brick_type buf_brick_type = {
2010-07-07 14:09:16 +00:00
.type_name = "buf_brick",
.brick_size = sizeof(struct buf_brick),
.max_inputs = 1,
.max_outputs = 1,
.master_ops = &buf_brick_ops,
.default_input_types = buf_input_types,
.default_output_types = buf_output_types,
.brick_construct = &buf_brick_construct,
2010-07-30 05:46:22 +00:00
.brick_destruct = &buf_brick_destruct,
2010-07-07 14:09:16 +00:00
};
EXPORT_SYMBOL_GPL(buf_brick_type);
////////////////// module init stuff /////////////////////////
static int __init init_buf(void)
{
printk(MARS_INFO "init_buf()\n");
return buf_register_brick_type();
}
static void __exit exit_buf(void)
{
printk(MARS_INFO "exit_buf()\n");
buf_unregister_brick_type();
}
MODULE_DESCRIPTION("MARS buf brick");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_buf);
module_exit(exit_buf);