mars/kernel/xio_bricks/unused/xio_usebuf.c

376 lines
8.9 KiB
C
Raw Normal View History

2014-11-21 10:51:34 +00:00
/*
* MARS Long Distance Replication Software
*
* This file is part of MARS project: http://schoebel.github.io/mars/
*
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
* Copyright (C) 2011-2014 1&1 Internet AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2015-12-31 09:41:15 +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 XIO_DEBUGGING
2015-12-31 09:41:15 +00:00
//#define FAKE_ALL /* only for testing */
//#define DIRECT_IO /* shortcut solely for testing: do direct IO */
2015-12-31 09:41:15 +00:00
/* only for testing: this risks trashing the data by omitting read-before-write in case of false sharing */
2015-12-31 09:41:13 +00:00
//#define DIRECT_WRITE
2010-07-30 05:46:22 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "../xio.h"
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:15 +00:00
/************************ own type definitions ***********************/
2010-07-30 05:46:22 +00:00
#include "xio_usebuf.h"
2010-07-30 05:46:22 +00:00
2010-11-26 13:45:10 +00:00
#define SHORTCUT
2015-12-31 09:41:15 +00:00
/************************ own helper functions ***********************/
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:15 +00:00
/***************** own brick * input * output operations *****************/
2010-11-26 13:45:10 +00:00
static int usebuf_get_info(struct usebuf_output *output, struct xio_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];
2015-12-31 09:41:13 +00:00
return GENERIC_INPUT_CALL(input, xio_get_info, info);
2010-07-30 05:46:22 +00:00
}
2010-12-10 17:40:20 +00:00
static inline
2015-12-31 09:41:19 +00:00
void _usebuf_copy(struct aio_object *aio, struct aio_object *sub_aio, int rw)
2010-11-26 13:45:10 +00:00
{
2010-12-10 17:40:20 +00:00
#ifndef FAKE_ALL
2015-12-31 09:41:14 +00:00
if (rw == 0)
2015-12-31 09:41:19 +00:00
memcpy(aio->io_data, sub_aio->io_data, aio->io_len);
2015-12-31 09:41:14 +00:00
else
2015-12-31 09:41:19 +00:00
memcpy(sub_aio->io_data, aio->io_data, aio->io_len);
2010-12-10 17:40:20 +00:00
#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
{
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *aio_a = cb->cb_private;
struct aio_object *aio;
struct usebuf_aio_aspect *sub_aio_a;
struct aio_object *sub_aio;
2010-07-30 05:46:22 +00:00
LAST_CALLBACK(cb);
2015-12-31 09:41:19 +00:00
CHECK_PTR(aio_a, done);
aio = aio_a->object;
CHECK_PTR(aio, done);
sub_aio_a = aio_a->sub_aio_a;
CHECK_PTR(sub_aio_a, done);
sub_aio = sub_aio_a->object;
CHECK_PTR(sub_aio, done);
if (aio->io_data != sub_aio->io_data && cb->cb_error >= 0) {
if (sub_aio->io_may_write == 0) {
if (sub_aio->io_flags & AIO_UPTODATE) {
_usebuf_copy(aio, sub_aio, 0);
aio->io_flags |= AIO_UPTODATE;
2010-12-10 17:40:20 +00:00
}
#ifndef FAKE_ALL
2015-12-31 09:41:19 +00:00
} else if (sub_aio->io_rw == 0) {
sub_aio->io_rw = 1;
_usebuf_copy(aio, sub_aio, 1);
aio->io_flags |= AIO_UPTODATE;
GENERIC_INPUT_CALL(aio_a->input, aio_io, sub_aio);
goto out_return;
2010-12-10 17:40:20 +00:00
#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
2015-12-31 09:41:19 +00:00
if (aio_a->yyy++ > 0)
XIO_ERR("yyy = %d\n", aio_a->yyy - 1);
2010-12-10 17:40:20 +00:00
if (cb->cb_error < 0)
XIO_ERR("error = %d\n", cb->cb_error);
2010-12-10 17:40:20 +00:00
#endif
2015-12-31 09:41:19 +00:00
CHECKED_CALLBACK(aio, cb->cb_error, done);
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:19 +00:00
if (!obj_put(aio))
goto out_return;
2010-11-26 13:45:10 +00:00
#if 1
2015-12-31 09:41:19 +00:00
obj_put(sub_aio);
2010-11-26 13:45:10 +00:00
#endif
2015-12-31 09:41:19 +00:00
obj_free(aio);
2010-11-26 13:45:10 +00:00
done:;
out_return:;
2010-07-30 05:46:22 +00:00
}
2015-12-31 09:41:19 +00:00
static int usebuf_io_get(struct usebuf_output *output, struct aio_object *aio)
2010-07-30 05:46:22 +00:00
{
2010-11-26 13:45:10 +00:00
struct usebuf_input *input = output->brick->inputs[0];
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *aio_a;
struct usebuf_aio_aspect *sub_aio_a;
struct aio_object *sub_aio;
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();
2015-12-31 09:41:19 +00:00
aio_a = usebuf_aio_get_aspect(output->brick, aio);
if (unlikely(!aio_a)) {
XIO_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
2015-12-31 09:41:19 +00:00
sub_aio_a = aio_a->sub_aio_a;
if (!sub_aio_a) {
sub_aio = usebuf_alloc_aio(output->brick);
2010-08-02 16:31:10 +00:00
2015-12-31 09:41:19 +00:00
sub_aio_a = usebuf_aio_get_aspect(output->brick, sub_aio);
if (unlikely(!sub_aio_a)) {
XIO_FAT("cannot get aspect\n");
2010-12-10 17:40:20 +00:00
return -EILSEQ;
}
2015-12-31 09:41:19 +00:00
aio_a->sub_aio_a = sub_aio_a;
sub_aio->io_pos = aio->io_pos;
sub_aio->io_len = aio->io_len;
sub_aio->io_may_write = aio->io_may_write;
2015-12-31 09:41:15 +00:00
#ifdef DIRECT_IO /* shortcut solely for testing: do direct IO */
2015-12-31 09:41:19 +00:00
if (!aio->io_data)
XIO_ERR("NULL.......\n");
2015-12-31 09:41:19 +00:00
sub_aio->io_data = aio->io_data;
2015-12-31 09:41:15 +00:00
#else /* normal case: buffered IO */
2015-12-31 09:41:19 +00:00
sub_aio->io_data = NULL;
2010-11-26 13:45:10 +00:00
#endif
2015-12-31 09:41:19 +00:00
SETUP_CALLBACK(sub_aio, _usebuf_endio, aio_a);
aio->io_flags = 0;
2010-12-10 17:40:20 +00:00
} else {
2015-12-31 09:41:19 +00:00
sub_aio = sub_aio_a->object;
2010-12-10 17:40:20 +00:00
#if 1
XIO_ERR("please do not use this broken feature\n");
2015-12-31 09:41:13 +00:00
#endif
2010-07-30 05:46:22 +00:00
}
2010-12-10 17:40:20 +00:00
2015-12-31 09:41:19 +00:00
status = GENERIC_INPUT_CALL(input, aio_get, sub_aio);
2015-12-31 09:41:14 +00:00
if (status < 0)
2010-11-26 13:45:10 +00:00
return status;
2010-12-10 17:40:20 +00:00
2015-12-31 09:41:19 +00:00
aio->io_len = sub_aio->io_len;
if (!aio->io_data)
aio->io_data = sub_aio->io_data;
obj_get(aio);
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
}
2015-12-31 09:41:19 +00:00
static void usebuf_io_put(struct usebuf_output *output, struct aio_object *aio)
2010-08-06 07:38:18 +00:00
{
struct usebuf_input *input = output->brick->inputs[0];
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *aio_a;
struct usebuf_aio_aspect *sub_aio_a;
struct aio_object *sub_aio;
2010-08-06 07:38:18 +00:00
2015-12-31 09:41:19 +00:00
aio_a = usebuf_aio_get_aspect(output->brick, aio);
if (unlikely(!aio_a)) {
XIO_FAT("cannot get aspect\n");
goto out_return;
2010-08-06 07:38:18 +00:00
}
2010-11-26 13:45:10 +00:00
2015-12-31 09:41:19 +00:00
sub_aio_a = aio_a->sub_aio_a;
if (!sub_aio_a) {
XIO_FAT("sub_aio_a is missing\n");
goto out_return;
2010-12-10 17:40:20 +00:00
}
2015-12-31 09:41:19 +00:00
sub_aio = sub_aio_a->object;
if (!sub_aio) {
XIO_FAT("sub_aio is missing\n");
goto out_return;
2010-11-26 13:45:10 +00:00
}
2015-12-31 09:41:19 +00:00
if (!obj_put(aio))
goto out_return;
2015-12-31 09:41:19 +00:00
GENERIC_INPUT_CALL(input, aio_put, sub_aio);
obj_free(aio);
out_return:;
2010-08-06 07:38:18 +00:00
}
2015-12-31 09:41:19 +00:00
static void usebuf_io_io(struct usebuf_output *output, struct aio_object *aio)
2010-07-30 05:46:22 +00:00
{
struct usebuf_input *input = output->brick->inputs[0];
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *aio_a;
struct usebuf_aio_aspect *sub_aio_a;
struct aio_object *sub_aio;
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();
2015-12-31 09:41:19 +00:00
obj_check(aio);
2015-12-31 09:41:19 +00:00
aio_a = usebuf_aio_get_aspect(output->brick, aio);
if (unlikely(!aio_a)) {
XIO_FAT("cannot get aspect\n");
2010-11-26 13:45:10 +00:00
goto err;
2010-07-30 05:46:22 +00:00
}
2010-11-26 13:45:10 +00:00
2015-12-31 09:41:19 +00:00
sub_aio_a = aio_a->sub_aio_a;
if (!sub_aio_a) {
XIO_FAT("sub_aio is missing\n");
2010-12-10 17:40:20 +00:00
goto err;
}
2015-12-31 09:41:19 +00:00
sub_aio = sub_aio_a->object;
if (!sub_aio) {
XIO_FAT("sub_aio is missing\n");
2010-11-26 13:45:10 +00:00
goto err;
2010-07-30 05:46:22 +00:00
}
2010-08-04 17:32:04 +00:00
2015-12-31 09:41:19 +00:00
if (aio->io_rw != 0 && sub_aio->io_may_write == 0) {
XIO_ERR("aio_may_write was not set before\n");
2010-12-10 17:40:20 +00:00
goto err;
2010-07-30 05:46:22 +00:00
}
2015-12-31 09:41:19 +00:00
obj_get(aio);
2010-11-26 13:45:10 +00:00
2015-12-31 09:41:19 +00:00
sub_aio->io_rw = aio->io_rw;
sub_aio->io_len = aio->io_len;
aio_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.
*/
2015-12-31 09:41:19 +00:00
if (aio->io_rw != 0) {
2010-12-10 17:40:20 +00:00
#ifdef DIRECT_WRITE
2015-12-31 09:41:19 +00:00
sub_aio->io_rw = 1;
2015-12-31 09:41:15 +00:00
#else /* normal case */
2015-12-31 09:41:19 +00:00
sub_aio->io_rw = 0;
if (sub_aio->io_flags & AIO_UPTODATE)
sub_aio->io_rw = 1;
2010-12-10 17:40:20 +00:00
#endif
2015-12-31 09:41:19 +00:00
} else if (sub_aio->io_flags & AIO_UPTODATE) {
_usebuf_endio(sub_aio->object_cb);
goto out_return;
2010-07-30 05:46:22 +00:00
}
2015-12-31 09:41:19 +00:00
if (aio->io_data != sub_aio->io_data) {
if (sub_aio->io_rw != 0) {
_usebuf_copy(aio, sub_aio, 1);
aio->io_flags |= AIO_UPTODATE;
2010-12-10 17:40:20 +00:00
}
}
#ifdef FAKE_ALL
2015-12-31 09:41:19 +00:00
_usebuf_endio(sub_aio->io_cb);
goto out_return;
2010-12-10 17:40:20 +00:00
#endif
2015-12-31 09:41:19 +00:00
GENERIC_INPUT_CALL(input, aio_io, sub_aio);
2010-07-30 05:46:22 +00:00
goto out_return;
2010-11-26 13:45:10 +00:00
err:
2015-12-31 09:41:19 +00:00
SIMPLE_CALLBACK(aio, error);
goto out_return;
out_return:;
2010-07-30 05:46:22 +00:00
}
2015-12-31 09:41:15 +00:00
/*************** object * aspect constructors * destructors **************/
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:19 +00:00
static int usebuf_aio_aspect_init_fn(struct generic_aspect *_ini)
2010-07-30 05:46:22 +00:00
{
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *ini = (void *)_ini;
2015-12-31 09:41:13 +00:00
2010-11-26 13:45:10 +00:00
(void)ini;
2010-07-30 05:46:22 +00:00
return 0;
}
2015-12-31 09:41:19 +00:00
static void usebuf_aio_aspect_exit_fn(struct generic_aspect *_ini)
2010-08-08 09:03:42 +00:00
{
2015-12-31 09:41:19 +00:00
struct usebuf_aio_aspect *ini = (void *)_ini;
2015-12-31 09:41:13 +00:00
2010-08-08 09:03:42 +00:00
(void)ini;
}
XIO_MAKE_STATICS(usebuf);
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:15 +00:00
/********************* brick constructors * destructors *******************/
2010-07-30 05:46:22 +00:00
static int usebuf_brick_construct(struct usebuf_brick *brick)
{
return 0;
}
static int usebuf_output_construct(struct usebuf_output *output)
{
return 0;
}
2015-12-31 09:41:15 +00:00
/************************ static structs ***********************/
2010-07-30 05:46:22 +00:00
2015-12-31 09:41:10 +00:00
static struct usebuf_brick_ops usebuf_brick_ops;
2010-07-30 05:46:22 +00:00
static struct usebuf_output_ops usebuf_output_ops = {
.xio_get_info = usebuf_get_info,
2015-12-31 09:41:19 +00:00
.aio_get = usebuf_io_get,
.aio_put = usebuf_io_put,
.aio_io = usebuf_io_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,
};
2015-12-31 09:41:15 +00:00
/***************** module init stuff ************************/
2010-07-30 05:46:22 +00:00
int __init init_xio_usebuf(void)
2010-07-30 05:46:22 +00:00
{
XIO_INF("init_usebuf()\n");
2010-07-30 05:46:22 +00:00
return usebuf_register_brick_type();
}
void exit_xio_usebuf(void)
2010-07-30 05:46:22 +00:00
{
XIO_INF("exit_usebuf()\n");
2010-07-30 05:46:22 +00:00
usebuf_unregister_brick_type();
}