mars/kernel/mars_usebuf.c

379 lines
9.0 KiB
C
Raw Normal View History

2010-07-30 05:46:22 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
2010-11-26 13:45:10 +00:00
// Usebuf brick (just for demonstration)
2010-07-30 05:46:22 +00:00
/* FIXME: this code has been unused for a long time, it is unlikly
* to work at all.
*/
2010-07-30 05:46:22 +00:00
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
//#define IO_DEBUGGING
//#define STAT_DEBUGGING
2010-12-10 17:40:20 +00:00
//#define FAKE_ALL // only for testing
//#define DIRECT_IO // shortcut solely for testing: do direct IO
//#define DIRECT_WRITE // only for testing: this risks trashing the data by omitting read-before-write in case of false sharing
2010-07-30 05:46:22 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_usebuf.h"
2010-11-26 13:45:10 +00:00
#define SHORTCUT
2010-07-30 05:46:22 +00:00
///////////////////////// own helper functions ////////////////////////
2010-11-26 13:45:10 +00:00
////////////////// own brick / input / output operations //////////////////
static int usebuf_get_info(struct usebuf_output *output, struct mars_info *info)
2010-07-30 05:46:22 +00:00
{
2010-11-26 13:45:10 +00:00
struct usebuf_input *input = output->brick->inputs[0];
return GENERIC_INPUT_CALL(input, mars_get_info, info);
2010-07-30 05:46:22 +00:00
}
2010-12-10 17:40:20 +00:00
static inline
2010-12-15 12:13:18 +00:00
void _usebuf_copy(struct mref_object *mref, struct mref_object *sub_mref, int rw)
2010-11-26 13:45:10 +00:00
{
MARS_IO("memcpy rw = %d %p %p %d\n", rw, mref->ref_data, sub_mref->ref_data, mref->ref_len);
2010-12-10 17:40:20 +00:00
#ifndef FAKE_ALL
if (rw == 0) {
memcpy(mref->ref_data, sub_mref->ref_data, mref->ref_len);
} else {
memcpy(sub_mref->ref_data, mref->ref_data, mref->ref_len);
}
#endif
2010-11-26 13:45:10 +00:00
}
2010-08-06 07:38:18 +00:00
2010-11-26 13:45:10 +00:00
static void _usebuf_endio(struct generic_callback *cb)
2010-07-30 05:46:22 +00:00
{
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *mref_a = cb->cb_private;
struct mref_object *mref;
struct usebuf_mref_aspect *sub_mref_a;
struct mref_object *sub_mref;
2010-07-30 05:46:22 +00:00
LAST_CALLBACK(cb);
2010-11-26 13:45:10 +00:00
CHECK_PTR(mref_a, done);
mref = mref_a->object;
CHECK_PTR(mref, done);
2010-12-10 17:40:20 +00:00
sub_mref_a = mref_a->sub_mref_a;
CHECK_PTR(sub_mref_a, done);
sub_mref = sub_mref_a->object;
2010-11-26 13:45:10 +00:00
CHECK_PTR(sub_mref, done);
2010-07-30 05:46:22 +00:00
2010-12-10 17:40:20 +00:00
//MARS_INF("HALLO %p %p len = %d may_write = %d rw = %d flags = %d\n", mref, sub_mref, sub_mref->ref_len, sub_mref->ref_may_write, sub_mref->ref_rw, sub_mref->ref_flags);
if (mref->ref_data != sub_mref->ref_data && cb->cb_error >= 0) {
if (sub_mref->ref_may_write == 0) {
2010-12-15 12:13:18 +00:00
if (sub_mref->ref_flags & MREF_UPTODATE) {
2010-12-10 17:40:20 +00:00
_usebuf_copy(mref, sub_mref, 0);
2010-12-15 12:13:18 +00:00
mref->ref_flags |= MREF_UPTODATE;
2010-12-10 17:40:20 +00:00
}
#ifndef FAKE_ALL
} else if (sub_mref->ref_rw == 0) {
MARS_IO("re-kick %p\n", sub_mref);
2010-12-10 17:40:20 +00:00
sub_mref->ref_rw = 1;
_usebuf_copy(mref, sub_mref, 1);
2010-12-15 12:13:18 +00:00
mref->ref_flags |= MREF_UPTODATE;
GENERIC_INPUT_CALL(mref_a->input, mref_io, sub_mref);
2010-12-10 17:40:20 +00:00
return;
#endif
2010-11-26 13:45:10 +00:00
}
2010-07-30 05:46:22 +00:00
}
2010-11-26 13:45:10 +00:00
#if 1
2010-12-10 17:40:20 +00:00
if (mref_a->yyy++ > 0)
MARS_ERR("yyy = %d\n", mref_a->yyy - 1);
if (cb->cb_error < 0)
MARS_ERR("error = %d\n", cb->cb_error);
#endif
CHECKED_CALLBACK(mref, cb->cb_error, done);
2010-07-30 05:46:22 +00:00
if (!_mref_put(mref))
2010-11-26 13:45:10 +00:00
return;
2010-12-10 17:40:20 +00:00
2010-11-26 13:45:10 +00:00
#if 1
_mref_put(sub_mref);
2010-11-26 13:45:10 +00:00
#endif
2010-12-15 12:13:18 +00:00
usebuf_free_mref(mref);
2010-11-26 13:45:10 +00:00
done:;
2010-07-30 05:46:22 +00:00
}
2010-12-15 12:13:18 +00:00
static int usebuf_ref_get(struct usebuf_output *output, struct mref_object *mref)
2010-07-30 05:46:22 +00:00
{
2010-11-26 13:45:10 +00:00
struct usebuf_input *input = output->brick->inputs[0];
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *mref_a;
struct usebuf_mref_aspect *sub_mref_a;
struct mref_object *sub_mref;
2010-11-26 13:45:10 +00:00
int status = 0;
2010-07-30 05:46:22 +00:00
2010-12-10 17:40:20 +00:00
might_sleep();
mref_a = usebuf_mref_get_aspect(output->brick, mref);
2010-08-05 15:54:48 +00:00
if (unlikely(!mref_a)) {
2010-08-04 17:32:04 +00:00
MARS_FAT("cannot get aspect\n");
2010-11-26 13:45:10 +00:00
return -EILSEQ;
2010-08-08 14:02:54 +00:00
}
2010-08-04 17:32:04 +00:00
2010-12-10 17:40:20 +00:00
sub_mref_a = mref_a->sub_mref_a;
if (!sub_mref_a) {
2012-02-02 15:25:43 +00:00
sub_mref = usebuf_alloc_mref(output->brick);
2010-11-26 13:45:10 +00:00
if (unlikely(!sub_mref)) {
MARS_FAT("cannot get sub_mref\n");
return -ENOMEM;
2010-08-02 16:31:10 +00:00
}
sub_mref_a = usebuf_mref_get_aspect(output->brick, sub_mref);
2010-12-10 17:40:20 +00:00
if (unlikely(!sub_mref_a)) {
MARS_FAT("cannot get aspect\n");
return -EILSEQ;
}
mref_a->sub_mref_a = sub_mref_a;
sub_mref->ref_pos = mref->ref_pos;
sub_mref->ref_len = mref->ref_len;
sub_mref->ref_may_write = mref->ref_may_write;
#ifdef DIRECT_IO // shortcut solely for testing: do direct IO
2010-11-26 13:45:10 +00:00
if (!mref->ref_data)
MARS_ERR("NULL.......\n");
sub_mref->ref_data = mref->ref_data;
#else // normal case: buffered IO
sub_mref->ref_data = NULL;
#endif
SETUP_CALLBACK(sub_mref, _usebuf_endio, mref_a);
2010-12-10 17:40:20 +00:00
mref->ref_flags = 0;
} else {
sub_mref = sub_mref_a->object;
#if 1
MARS_ERR("please do not use this broken feature\n");
#endif
2010-07-30 05:46:22 +00:00
}
2010-12-10 17:40:20 +00:00
2010-12-15 12:13:18 +00:00
status = GENERIC_INPUT_CALL(input, mref_get, sub_mref);
2010-08-04 17:32:04 +00:00
if (status < 0) {
2010-11-26 13:45:10 +00:00
return status;
}
2010-12-10 17:40:20 +00:00
mref->ref_len = sub_mref->ref_len;
//MARS_INF("GOT %p %p flags = %d\n", mref, sub_mref, sub_mref->ref_flags);
2010-11-26 13:45:10 +00:00
if (!mref->ref_data) {
MARS_INF("uiiiiiiiiiii\n");
mref->ref_data = sub_mref->ref_data;
}
_mref_get(mref);
2010-07-30 05:46:22 +00:00
2010-11-26 13:45:10 +00:00
return status;
2010-08-06 07:38:18 +00:00
}
2010-12-15 12:13:18 +00:00
static void usebuf_ref_put(struct usebuf_output *output, struct mref_object *mref)
2010-08-06 07:38:18 +00:00
{
struct usebuf_input *input = output->brick->inputs[0];
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *mref_a;
struct usebuf_mref_aspect *sub_mref_a;
struct mref_object *sub_mref;
2010-08-06 07:38:18 +00:00
mref_a = usebuf_mref_get_aspect(output->brick, mref);
2010-11-26 13:45:10 +00:00
if (unlikely(!mref_a)) {
MARS_FAT("cannot get aspect\n");
2010-08-06 07:38:18 +00:00
return;
}
2010-11-26 13:45:10 +00:00
2010-12-10 17:40:20 +00:00
sub_mref_a = mref_a->sub_mref_a;
if (!sub_mref_a) {
MARS_FAT("sub_mref_a is missing\n");
return;
}
sub_mref = sub_mref_a->object;
2010-11-26 13:45:10 +00:00
if (!sub_mref) {
MARS_FAT("sub_mref is missing\n");
return;
}
if (!_mref_put(mref))
2010-11-26 13:45:10 +00:00
return;
2010-12-15 12:13:18 +00:00
GENERIC_INPUT_CALL(input, mref_put, sub_mref);
usebuf_free_mref(mref);
2010-08-06 07:38:18 +00:00
}
2010-12-15 12:13:18 +00:00
static void usebuf_ref_io(struct usebuf_output *output, struct mref_object *mref)
2010-07-30 05:46:22 +00:00
{
struct usebuf_input *input = output->brick->inputs[0];
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *mref_a;
struct usebuf_mref_aspect *sub_mref_a;
struct mref_object *sub_mref;
2010-11-26 13:45:10 +00:00
int error = -EILSEQ;
2010-07-30 05:46:22 +00:00
2010-12-10 17:40:20 +00:00
might_sleep();
_mref_check(mref);
mref_a = usebuf_mref_get_aspect(output->brick, mref);
2010-11-26 13:45:10 +00:00
if (unlikely(!mref_a)) {
MARS_FAT("cannot get aspect\n");
goto err;
2010-07-30 05:46:22 +00:00
}
2010-11-26 13:45:10 +00:00
2010-12-10 17:40:20 +00:00
sub_mref_a = mref_a->sub_mref_a;
if (!sub_mref_a) {
MARS_FAT("sub_mref is missing\n");
goto err;
}
sub_mref = sub_mref_a->object;
2010-11-26 13:45:10 +00:00
if (!sub_mref) {
MARS_FAT("sub_mref is missing\n");
goto err;
2010-07-30 05:46:22 +00:00
}
2010-08-04 17:32:04 +00:00
2010-12-10 17:40:20 +00:00
if (mref->ref_rw != 0 && sub_mref->ref_may_write == 0) {
MARS_ERR("mref_may_write was not set before\n");
goto err;
2010-07-30 05:46:22 +00:00
}
_mref_get(mref);
2010-11-26 13:45:10 +00:00
2010-12-10 17:40:20 +00:00
sub_mref->ref_rw = mref->ref_rw;
sub_mref->ref_len = mref->ref_len;
mref_a->input = input;
2010-11-26 13:45:10 +00:00
/* Optimization: when buffered IO is used and buffer is already
* uptodate, skip real IO operation.
*/
2010-12-10 17:40:20 +00:00
if (mref->ref_rw != 0) {
#ifdef DIRECT_WRITE
sub_mref->ref_rw = 1;
#else // normal case
sub_mref->ref_rw = 0;
2010-12-15 12:13:18 +00:00
if (sub_mref->ref_flags & MREF_UPTODATE) {
2010-12-10 17:40:20 +00:00
sub_mref->ref_rw = 1;
}
#endif
2010-12-15 12:13:18 +00:00
} else if (sub_mref->ref_flags & MREF_UPTODATE) {
MARS_IO("direct _usebuf_endio\n");
_usebuf_endio(sub_mref->object_cb);
2010-12-10 17:40:20 +00:00
return;
2010-07-30 05:46:22 +00:00
}
2010-12-10 17:40:20 +00:00
if (mref->ref_data != sub_mref->ref_data) {
if (sub_mref->ref_rw != 0) {
_usebuf_copy(mref, sub_mref, 1);
2010-12-15 12:13:18 +00:00
mref->ref_flags |= MREF_UPTODATE;
2010-12-10 17:40:20 +00:00
}
}
#ifdef FAKE_ALL
_usebuf_endio(sub_mref->ref_cb);
return;
#endif
2010-12-15 12:13:18 +00:00
GENERIC_INPUT_CALL(input, mref_io, sub_mref);
2010-07-30 05:46:22 +00:00
2010-11-26 13:45:10 +00:00
return;
2010-07-30 05:46:22 +00:00
2010-11-26 13:45:10 +00:00
err:
SIMPLE_CALLBACK(mref, error);
2010-11-26 13:45:10 +00:00
return;
2010-07-30 05:46:22 +00:00
}
//////////////// object / aspect constructors / destructors ///////////////
static int usebuf_mref_aspect_init_fn(struct generic_aspect *_ini)
2010-07-30 05:46:22 +00:00
{
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *ini = (void*)_ini;
2010-11-26 13:45:10 +00:00
(void)ini;
2010-07-30 05:46:22 +00:00
return 0;
}
static void usebuf_mref_aspect_exit_fn(struct generic_aspect *_ini)
2010-08-08 09:03:42 +00:00
{
2010-12-15 12:13:18 +00:00
struct usebuf_mref_aspect *ini = (void*)_ini;
2010-08-08 09:03:42 +00:00
(void)ini;
}
2010-07-30 05:46:22 +00:00
MARS_MAKE_STATICS(usebuf);
////////////////////// brick constructors / destructors ////////////////////
static int usebuf_brick_construct(struct usebuf_brick *brick)
{
return 0;
}
static int usebuf_output_construct(struct usebuf_output *output)
{
return 0;
}
///////////////////////// static structs ////////////////////////
static struct usebuf_brick_ops usebuf_brick_ops = {
};
static struct usebuf_output_ops usebuf_output_ops = {
.mars_get_info = usebuf_get_info,
2010-12-15 12:13:18 +00:00
.mref_get = usebuf_ref_get,
.mref_put = usebuf_ref_put,
.mref_io = usebuf_ref_io,
2010-07-30 05:46:22 +00:00
};
2010-08-10 17:39:30 +00:00
const struct usebuf_input_type usebuf_input_type = {
2010-07-30 05:46:22 +00:00
.type_name = "usebuf_input",
.input_size = sizeof(struct usebuf_input),
};
static const struct usebuf_input_type *usebuf_input_types[] = {
&usebuf_input_type,
};
2010-08-10 17:39:30 +00:00
const struct usebuf_output_type usebuf_output_type = {
2010-07-30 05:46:22 +00:00
.type_name = "usebuf_output",
.output_size = sizeof(struct usebuf_output),
.master_ops = &usebuf_output_ops,
.output_construct = &usebuf_output_construct,
};
static const struct usebuf_output_type *usebuf_output_types[] = {
&usebuf_output_type,
};
const struct usebuf_brick_type usebuf_brick_type = {
.type_name = "usebuf_brick",
.brick_size = sizeof(struct usebuf_brick),
.max_inputs = 1,
.max_outputs = 1,
.master_ops = &usebuf_brick_ops,
.aspect_types = usebuf_aspect_types,
2010-07-30 05:46:22 +00:00
.default_input_types = usebuf_input_types,
.default_output_types = usebuf_output_types,
.brick_construct = &usebuf_brick_construct,
};
EXPORT_SYMBOL_GPL(usebuf_brick_type);
////////////////// module init stuff /////////////////////////
int __init init_mars_usebuf(void)
2010-07-30 05:46:22 +00:00
{
2012-02-01 15:25:50 +00:00
MARS_INF("init_usebuf()\n");
2010-07-30 05:46:22 +00:00
return usebuf_register_brick_type();
}
void __exit exit_mars_usebuf(void)
2010-07-30 05:46:22 +00:00
{
2012-02-01 15:25:50 +00:00
MARS_INF("exit_usebuf()\n");
2010-07-30 05:46:22 +00:00
usebuf_unregister_brick_type();
}
#ifndef CONFIG_MARS_HAVE_BIGMODULE
2010-07-30 05:46:22 +00:00
MODULE_DESCRIPTION("MARS usebuf brick");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_mars_usebuf);
module_exit(exit_mars_usebuf);
#endif