import mars-02.tgz

This commit is contained in:
Thomas Schoebel-Theuer 2010-06-14 15:27:40 +01:00
parent 0899d3afb1
commit ca517f7309
10 changed files with 1448 additions and 0 deletions

36
Kconfig Normal file
View File

@ -0,0 +1,36 @@
#
# MARS configuration
#
config MARS
tristate "storage system MARS"
default n
---help---
Experimental storage System.
config MARS_DUMMY
tristate "MARS dummy brick"
depends on MARS
default m
---help---
Experimental storage System.
config MARS_IF_DEVICE
tristate "interface to a linux device"
depends on MARS
default m
---help---
Experimental storage System.
config MARS_DEVICE_SYNC
tristate "interface to a linux file, synchronous"
depends on MARS
default m
---help---
Experimental storage System.
config MARS_TEST
tristate "provisionary"
depends on MARS
default m
---help---
Experimental storage System.

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
#
# Makefile for MARS
#
obj-$(CONFIG_MARS) += mars_generic.o
obj-$(CONFIG_MARS_DUMMY) += mars_dummy.o
obj-$(CONFIG_MARS_IF_DEVICE) += mars_if_device.o
obj-$(CONFIG_MARS_DEVICE_SYNC) += mars_device_sync.o
obj-$(CONFIG_MARS_TEST) += mars_test.o
#mars-objs := mars_generic.o

530
mars.h Normal file
View File

@ -0,0 +1,530 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#ifndef MARS_H
#define MARS_H
#include <linux/list.h>
#ifdef _STRATEGY
#define _STRATEGY_CODE(X) X
#define _NORMAL_CODE(X) /**/
#else
#define _STRATEGY_CODE(X) /**/
#define _NORMAL_CODE(X) X
#endif
#define MARS_INFO "MARS_INFO: "
#define MARS_ERROR "MARS_ERROR: "
#define MARS_ERR(args...) printk(MARS_ERROR args)
#define MARS_DBG(args...) printk("MARS_DEBUG: " args)
/////////////////////////////////////////////////////////////////////////
// definitions for generic objects
#define MAX_DEFAULT_ASPECTS 8
#define GENERIC_OBJECT_LAYOUT(PREFIX) \
int max_size; \
int rest_size; \
int max_aspects; \
int nr_aspects; \
int *aspect_sizes; \
int *aspect_offsets; \
void *alloc_ptr; \
struct generic_object_layout {
GENERIC_OBJECT_LAYOUT(generic);
};
#ifdef _STRATEGY
#define GENERIC_OBJECT_LAYOUT_FUNCTIONS(PREFIX) \
extern inline struct PREFIX##_object_layout *PREFIX##_init_object_layout(void *data, int size, int max_aspects) \
{ \
struct PREFIX##_object_layout *object_layout = data; \
size -= sizeof(struct PREFIX##_object_layout); \
if (size < 0) \
return NULL; \
object_layout->max_aspects = max_aspects; \
object_layout->nr_aspects = 0; \
object_layout->max_size = sizeof(struct PREFIX##_object_layout); \
data += sizeof(struct PREFIX##_object_layout); \
size -= max_aspects * sizeof(void*) * 2; \
if (size < 0) \
return NULL; \
object_layout->rest_size = size; \
object_layout->aspect_sizes = data; \
data += max_aspects * sizeof(void*); \
object_layout->aspect_offsets = data; \
data += max_aspects * sizeof(void*); \
object_layout->alloc_ptr = data; \
return object_layout; \
} \
\
extern int PREFIX##_add_aspect(struct PREFIX##_object_layout *object_layout, int aspect_size) \
{ \
int slot = object_layout->nr_aspects; \
int max_aspects = object_layout->max_aspects; \
if (unlikely(slot >= max_aspects)) { \
void *data = object_layout->alloc_ptr; \
void *old; \
int size = object_layout->rest_size; \
max_aspects <<= 1; \
size -= max_aspects * sizeof(void*) * 2; \
if (size < 0) \
return -ENOMEM; \
object_layout->rest_size = size; \
old = object_layout->aspect_sizes; \
object_layout->aspect_sizes = data; \
memcpy(data, old, max_aspects * sizeof(void*)); \
data += max_aspects * sizeof(void*); \
old = object_layout->aspect_offsets; \
object_layout->aspect_offsets = data; \
memcpy(data, old, max_aspects * sizeof(void*)); \
data += max_aspects * sizeof(void*); \
object_layout->alloc_ptr = data; \
object_layout->max_aspects = max_aspects; \
} \
object_layout->aspect_sizes[slot] = aspect_size; \
object_layout->aspect_offsets[slot] = object_layout->max_size; \
object_layout->max_size += aspect_size; \
object_layout->nr_aspects++; \
return slot; \
} \
#endif
#define GENERIC_OBJECT(PREFIX) \
struct PREFIX##_object_layout *object_layout; \
int object_size; \
struct generic_object {
GENERIC_OBJECT(generic);
};
#define GENERIC_ASPECT(PREFIX) \
struct PREFIX##_object *object; \
struct generic_aspect {
GENERIC_ASPECT(generic);
};
#define GENERIC_OBJECT_FUNCTIONS(PREFIX) \
extern inline struct PREFIX##_object *PREFIX##_construct(void *data, struct PREFIX##_object_layout *object_layout) \
{ \
int i; \
struct PREFIX##_object *obj = data; \
obj->object_layout = object_layout; \
for (i = 0; i < object_layout->nr_aspects; i++) { \
struct PREFIX##_aspect *aspect = data + object_layout->aspect_offsets[i]; \
aspect->object = obj; \
} \
return obj; \
} \
\
extern inline void *PREFIX##_get_aspect(struct PREFIX##_object *obj, int slot) \
{ \
if (slot < 0 || slot >= obj->object_layout->nr_aspects) \
return NULL; \
return (void*)obj + obj->object_layout->aspect_offsets[slot]; \
} \
\
/////////////////////////////////////////////////////////////////////////
// definitions for generic bricks
struct generic_input;
struct generic_output;
struct generic_brick_ops;
struct generic_output_ops;
struct generic_brick_type;
#define GENERIC_BRICK(PREFIX) \
char *brick_name; \
struct PREFIX##_brick_type *type; \
struct PREFIX##_brick_ops *ops; \
int nr_inputs; \
int nr_outputs; \
struct PREFIX##_input **inputs; \
struct PREFIX##_output **outputs; \
struct list_head tmp_head; \
struct generic_brick {
GENERIC_BRICK(generic);
};
#define GENERIC_INPUT(PREFIX) \
char *input_name; \
struct PREFIX##_brick *brick; \
struct PREFIX##_input_type *type; \
struct PREFIX##_output *connect; \
struct generic_input {
GENERIC_INPUT(generic);
};
#define GENERIC_OUTPUT(PREFIX) \
char *output_name; \
struct PREFIX##_brick *brick; \
struct PREFIX##_output_type *type; \
struct PREFIX##_output_ops *ops; \
int nr_connected; \
struct generic_output {
GENERIC_OUTPUT(generic);
};
#define _GENERIC_CALL(ERROR,PTR,OP,...) \
((PTR)->ops->OP ? (PTR)->ops->OP(__VA_ARGS__) : ERROR)
#define GENERIC_CALL(PTR,OP,...) \
_GENERIC_CALL(0, PTR, OP, __VA_ARGS__)
#define GENERIC_CALL_FORCE(PTR,OP,...) \
_GENERIC_CALL(-ENOSYS, PTR, OP, __VA_ARGS__)
#define GENERIC_BRICK_OPS(PREFIX) \
int (*brick_start)(struct PREFIX##_brick *brick); \
int (*brick_stop)(struct PREFIX##_brick *brick); \
struct generic_brick_ops {
GENERIC_BRICK_OPS(generic);
};
#define GENERIC_OUTPUT_OPS(PREFIX) \
int (*output_start)(struct PREFIX##_output *output); \
int (*output_stop)(struct PREFIX##_output *output); \
struct generic_output_ops {
GENERIC_OUTPUT_OPS(generic)
};
// although possible, *_type should never be extended
#define GENERIC_BRICK_TYPE(PREFIX) \
char type_name[32]; \
int brick_size; \
int max_inputs; \
int max_outputs; \
struct PREFIX##_input_type **default_input_types; \
char **default_input_names; \
struct PREFIX##_output_type **default_output_types; \
char **default_output_names; \
struct PREFIX##_brick_ops *master_ops; \
struct PREFIX##input_types **default_type; \
int (*brick_construct)(struct PREFIX##_brick *brick); \
int (*brick_destruct)(struct PREFIX##_brick *brick); \
struct generic_brick_type {
GENERIC_BRICK_TYPE(generic);
};
#define GENERIC_INPUT_TYPE(PREFIX) \
char type_name[32]; \
int input_size; \
int (*input_construct)(struct PREFIX##_input *input); \
int (*input_destruct)(struct PREFIX##_input *input); \
struct generic_input_type {
GENERIC_INPUT_TYPE(generic);
};
#define GENERIC_OUTPUT_TYPE(PREFIX) \
char type_name[32]; \
int output_size; \
struct PREFIX##_output_ops *master_ops; \
int (*output_construct)(struct PREFIX##_output *output); \
int (*output_destruct)(struct PREFIX##_output *output); \
struct generic_output_type {
GENERIC_OUTPUT_TYPE(generic);
};
int generic_register_brick_type(struct generic_brick_type *new_type);
int generic_unregister_brick_type(struct generic_brick_type *old_type);
#ifdef _STRATEGY // call this only in strategy bricks, never in ordinary bricks
// you need this only if you circumvent generic_brick_init_full()
extern inline int generic_brick_init(struct generic_brick_type *type, struct generic_brick *brick, char *brick_name)
{
brick->brick_name = brick_name;
brick->type = type;
brick->ops = type->master_ops;
brick->nr_inputs = 0;
brick->nr_outputs = 0;
brick->tmp_head.next = brick->tmp_head.prev = &brick->tmp_head;
return 0;
}
extern inline int generic_input_init(struct generic_brick *brick, int index, struct generic_input_type *type, struct generic_input *input, char *input_name)
{
if (index < 0 || index >= brick->type->max_inputs)
return -ENOMEM;
if (brick->inputs[index])
return -EEXIST;
input->input_name = input_name;
input->brick = brick;
input->type = type;
input->connect = NULL;
brick->inputs[index] = input;
brick->nr_inputs++;
return 0;
}
extern inline int generic_output_init(struct generic_brick *brick, int index, struct generic_output_type *type, struct generic_output *output, char *output_name)
{
if (index < 0 || index >= brick->type->max_outputs)
return -ENOMEM;
if (brick->outputs[index])
return -EEXIST;
output->output_name = output_name;
output->brick = brick;
output->type = type;
output->ops = type->master_ops;
output->nr_connected = 0;
brick->outputs[index] = output;
brick->nr_outputs++;
return 0;
}
extern inline int generic_size(struct generic_brick_type *brick_type)
{
int size = brick_type->brick_size;
int i;
size += brick_type->max_inputs * sizeof(void*);
for (i = 0; i < brick_type->max_inputs; i++) {
size += brick_type->default_input_types[i]->input_size;
}
size += brick_type->max_outputs * sizeof(void*);
for (i = 0; i < brick_type->max_outputs; i++) {
size += brick_type->default_output_types[i]->output_size;
}
return size;
}
/* If possible, use this instead of generic_*_init().
* input_types and output_types may be NULL => use default_*_types
*/
int generic_brick_init_full(
void *data,
int size,
struct generic_brick_type *brick_type,
struct generic_input_type **input_types,
struct generic_output_type **output_types,
char **names);
int generic_brick_exit_full(
struct generic_brick *brick);
extern inline int generic_connect(struct generic_input *input, struct generic_output *output)
{
MARS_DBG("generic_connect(input=%p, output=%p)\n", input, output);
if (!input || !output)
return -EINVAL;
if (input->connect)
return -EEXIST;
input->connect = output;
output->nr_connected++; //TODO: protect against races, e.g. atomic_t
MARS_DBG("now nr_connected=%d\n", output->nr_connected);
return 0;
}
extern inline int generic_disconnect(struct generic_input *input)
{
MARS_DBG("generic_disconnect(input=%p)\n", input);
if (!input)
return -EINVAL;
if (input->connect) {
input->connect->nr_connected--; //TODO: protect against races, e.g. atomic_t
MARS_DBG("now nr_connected=%d\n", input->connect->nr_connected);
input->connect = NULL;
}
return 0;
}
#endif // _STRATEGY
// simple wrappers for type safety
#define GENERIC_MAKE_FUNCTIONS(PREFIX) \
extern inline int PREFIX##_register_brick_type(void) \
{ \
extern struct PREFIX##_brick_type PREFIX##_brick_type; \
return generic_register_brick_type((struct generic_brick_type*)&PREFIX##_brick_type); \
}\
\
extern inline int PREFIX##_unregister_brick_type(void) \
{ \
extern struct PREFIX##_brick_type PREFIX##_brick_type; \
return generic_unregister_brick_type((struct generic_brick_type*)&PREFIX##_brick_type); \
} \
\
_STRATEGY_CODE( \
extern struct PREFIX##_brick_type PREFIX##_brick_type; \
extern struct PREFIX##_input_type PREFIX##_input_type; \
extern struct PREFIX##_output_type PREFIX##_output_type; \
\
static inline int PREFIX##_brick_init(struct PREFIX##_brick *brick, char *brick_name) \
{ \
return generic_brick_init((struct generic_brick_type*)&PREFIX##_brick_type, (struct generic_brick*)brick, brick_name); \
} \
\
static inline int PREFIX##_input_init(struct PREFIX##_brick *brick, int index, struct PREFIX##_input *input, char *input_name) \
{ \
return generic_input_init( \
(struct generic_brick*)brick, \
index, \
(struct generic_input_type*)&PREFIX##_input_type, \
(struct generic_input*)input, \
input_name); \
} \
\
static inline int PREFIX##_output_init(struct PREFIX##_brick *brick, int index, struct PREFIX##_input *output, char *output_name) \
{ \
return generic_output_init( \
(struct generic_brick*)brick, \
index, \
(struct generic_output_type*)&PREFIX##_output_type, \
(struct generic_output*)output, \
output_name); \
} \
\
extern inline int PREFIX##_size(struct PREFIX##_brick_type *brick_type) \
{ \
return generic_size((struct generic_brick_type*)brick_type); \
} \
\
extern inline int PREFIX##_brick_init_full( \
void *data, \
int size, \
struct PREFIX##_brick_type *brick_type, \
struct PREFIX##_input_type **input_types, \
struct PREFIX##_output_type **output_types, \
char **names) \
{ \
return generic_brick_init_full( \
data, \
size, \
(struct generic_brick_type*)brick_type, \
(struct generic_input_type**)input_types, \
(struct generic_output_type**)output_types, \
(char**)names); \
} \
\
extern inline int PREFIX##_brick_exit_full( \
struct PREFIX##_brick *brick) \
{ \
return generic_brick_exit_full( \
(struct generic_brick*)brick); \
} \
)
/* Define a pair of connectable subtypes.
* For type safety, use this for all possible combinations.
* Yes, this may become quadratic in large type systems, but
* (a) thou shalt not define many types,
* (b) these macros generate only definitions, no code by itself
*/
#define GENERIC_MAKE_CONNECT(INPUT_PREFIX,OUTPUT_PREFIX) \
\
_STRATEGY_CODE( \
\
extern inline int INPUT_PREFIX##_##OUTPUT_PREFIX##_connect( \
struct INPUT_PREFIX##_input *input, \
struct OUTPUT_PREFIX##_output *output) \
{ \
return generic_connect((struct generic_input*)input, (struct generic_output*)output); \
} \
\
extern inline int INPUT_PREFIX##_##OUTPUT_PREFIX####_disconnect( \
struct INPUT_PREFIX##_input *input) \
{ \
return generic_disconnect((struct generic_input*)input); \
} \
)
/////////////////////////////////////////////////////////////////////////
// MARS-specific definitions
#define HT_SHIFT 6 //????
#define MARS_MAX_SEGMENT_SIZE (1U << (9+HT_SHIFT))
#define MARS_IO(PREFIX) \
GENERIC_OBJECT(PREFIX); \
struct bio *orig_bio; \
struct bio *mars_bio; \
int (*mars_endio)(struct mars_io *mio); \
struct mars_io {
MARS_IO(mars);
};
#define MARS_BRICK(PREFIX) \
GENERIC_BRICK(PREFIX); \
struct mars_brick {
MARS_BRICK(mars);
};
#define MARS_INPUT(PREFIX) \
GENERIC_INPUT(PREFIX); \
struct mars_input {
MARS_INPUT(mars);
};
#define MARS_OUTPUT(PREFIX) \
GENERIC_OUTPUT(PREFIX); \
struct mars_output {
MARS_OUTPUT(mars);
};
#define MARS_BRICK_OPS(PREFIX) \
GENERIC_BRICK_OPS(PREFIX); \
int (*mars_dummy_op)(int); \
#define MARS_OUTPUT_OPS(PREFIX) \
GENERIC_OUTPUT_OPS(PREFIX); \
int (*mars_io)(struct PREFIX##_output *output, struct mars_io *mio); \
// all non-extendable types
#define _MARS_TYPES(PREFIX) \
struct PREFIX##_brick_ops { \
MARS_BRICK_OPS(PREFIX); \
}; \
\
struct PREFIX##_output_ops { \
MARS_OUTPUT_OPS(PREFIX); \
}; \
\
struct PREFIX##_brick_type { \
GENERIC_BRICK_TYPE(PREFIX); \
}; \
\
struct PREFIX##_input_type { \
GENERIC_INPUT_TYPE(PREFIX); \
}; \
\
struct PREFIX##_output_type { \
GENERIC_OUTPUT_TYPE(PREFIX); \
}; \
\
GENERIC_MAKE_FUNCTIONS(PREFIX); \
GENERIC_MAKE_CONNECT(PREFIX,PREFIX); \
#define MARS_TYPES(PREFIX) \
_MARS_TYPES(PREFIX) \
GENERIC_MAKE_CONNECT(generic,PREFIX); \
GENERIC_MAKE_CONNECT(mars,PREFIX); \
/////////////////////////////////////////////////////////////////////////
// MARS-specific helper functions
_MARS_TYPES(mars);
GENERIC_MAKE_CONNECT(generic,mars);
#endif

84
mars_device_sync.c Normal file
View File

@ -0,0 +1,84 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_device_sync.h"
////////////////// own brick / input / output operations //////////////////
static int device_sync_mars_io(struct device_sync_output *output, struct mars_io *mio)
{
struct bio *bio = mio->orig_bio;
mio->mars_endio(mio);
return 0;
}
///////////////////////// contructors / destructors ////////////////////////
static int device_sync_brick_construct(struct device_sync_brick *brick)
{
return 0;
}
static int device_sync_output_construct(struct device_sync_output *output)
{
return 0;
}
///////////////////////// static structs ////////////////////////
static struct device_sync_brick_ops device_sync_brick_ops = {
};
static struct device_sync_output_ops device_sync_output_ops = {
.mars_io = device_sync_mars_io,
};
static struct device_sync_output_type device_sync_output_type = {
.type_name = "device_sync_output",
.output_size = sizeof(struct device_sync_output),
.master_ops = &device_sync_output_ops,
.output_construct = &device_sync_output_construct,
};
static struct device_sync_output_type *device_sync_output_types[] = {
&device_sync_output_type,
};
struct device_sync_brick_type device_sync_brick_type = {
.type_name = "device_sync_brick",
.brick_size = sizeof(struct device_sync_brick),
.max_inputs = 0,
.max_outputs = 1,
.master_ops = &device_sync_brick_ops,
.default_output_types = device_sync_output_types,
.brick_construct = &device_sync_brick_construct,
};
EXPORT_SYMBOL_GPL(device_sync_brick_type);
////////////////// module init stuff /////////////////////////
static int __init init_device_sync(void)
{
printk(MARS_INFO "init_device_sync()\n");
return device_sync_register_brick_type();
}
static void __exit exit_device_sync(void)
{
printk(MARS_INFO "exit_device_sync()\n");
device_sync_unregister_brick_type();
}
MODULE_DESCRIPTION("MARS device_sync brick");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_device_sync);
module_exit(exit_device_sync);

14
mars_device_sync.h Normal file
View File

@ -0,0 +1,14 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
struct device_sync_brick {
MARS_BRICK(device_sync);
};
struct device_sync_input {
MARS_INPUT(device_sync);
};
struct device_sync_output {
MARS_OUTPUT(device_sync);
};
MARS_TYPES(device_sync);

104
mars_dummy.c Normal file
View File

@ -0,0 +1,104 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
// Dummy brick (just for demonstration)
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
struct dummy_brick {
MARS_BRICK(dummy);
int my_own;
};
struct dummy_input {
MARS_INPUT(dummy);
};
struct dummy_output {
MARS_OUTPUT(dummy);
int my_own;
};
MARS_TYPES(dummy);
////////////////// own brick / input / output operations //////////////////
///////////////////////// contructors / destructors ////////////////////////
static int dummy_brick_construct(struct dummy_brick *brick)
{
brick->my_own = 0;
return 0;
}
static int dummy_output_construct(struct dummy_output *output)
{
output->my_own = 0;
return 0;
}
///////////////////////// static structs ////////////////////////
static struct dummy_brick_ops dummy_brick_ops = {
};
static struct dummy_output_ops dummy_output_ops = {
};
static struct dummy_input_type dummy_input_type = {
.type_name = "dummy_input",
.input_size = sizeof(struct dummy_input),
};
static struct dummy_input_type *dummy_input_types[] = {
&dummy_input_type,
};
static struct dummy_output_type dummy_output_type = {
.type_name = "dummy_output",
.output_size = sizeof(struct dummy_output),
.master_ops = &dummy_output_ops,
.output_construct = &dummy_output_construct,
};
static struct dummy_output_type *dummy_output_types[] = {
&dummy_output_type,
};
struct dummy_brick_type dummy_brick_type = {
.type_name = "dummy_brick",
.brick_size = sizeof(struct dummy_brick),
.max_inputs = 1,
.max_outputs = 1,
.master_ops = &dummy_brick_ops,
.default_input_types = dummy_input_types,
.default_output_types = dummy_output_types,
.brick_construct = &dummy_brick_construct,
};
EXPORT_SYMBOL_GPL(dummy_brick_type);
////////////////// module init stuff /////////////////////////
static int __init init_dummy(void)
{
printk(MARS_INFO "init_dummy()\n");
return dummy_register_brick_type();
}
static void __exit exit_dummy(void)
{
printk(MARS_INFO "exit_dummy()\n");
dummy_unregister_brick_type();
}
MODULE_DESCRIPTION("MARS dummy brick");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_dummy);
module_exit(exit_dummy);

305
mars_generic.c Normal file
View File

@ -0,0 +1,305 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#define _STRATEGY
#include "mars.h"
//////////////////////////////////////////////////////////////
// testing.....
#if 1
#include <linux/slab.h>
GENERIC_OBJECT_LAYOUT_FUNCTIONS(generic);
GENERIC_OBJECT_FUNCTIONS(generic);
void test(void)
{
char data[1024];
struct generic_object *obj;
struct generic_object_layout *t = generic_init_object_layout(data, sizeof(data), 4);
int slot = generic_add_aspect(t, 17);
char *my_aspect;
obj = kmalloc(t->max_size, GFP_KERNEL);
obj = generic_construct(obj, t);
my_aspect = (void*)obj + obj->object_layout->aspect_offsets[slot];
my_aspect = generic_get_aspect(obj, slot);
}
#endif
//////////////////////////////////////////////////////////////
#define MAX_BRICK_TYPES 64
static int nr_brick_types = 0;
static struct generic_brick_type *brick_types[MAX_BRICK_TYPES] = {};
int generic_register_brick_type(struct generic_brick_type *new_type)
{
int i;
int found = -1;
MARS_DBG("generic_register_brick_type()\n");
for (i = 0; i < nr_brick_types; i++) {
if (!brick_types[i]) {
found = i;
continue;
}
if (!strcmp(brick_types[i]->type_name, new_type->type_name)) {
printk("sorry, bricktype %s is already registered.\n", new_type->type_name);
return -EEXIST;
}
}
if (found < 0) {
if (nr_brick_types >= MAX_BRICK_TYPES) {
printk("sorry, cannot register bricktype %s.\n", new_type->type_name);
return -EEXIST;
}
found = nr_brick_types++;
}
brick_types[found] = new_type;
MARS_DBG("generic_register_brick_type() done.\n");
return 0;
}
EXPORT_SYMBOL_GPL(generic_register_brick_type);
int generic_unregister_brick_type(struct generic_brick_type *old_type)
{
MARS_DBG("generic_unregister_brick_type()\n");
return -1; // NYI
}
EXPORT_SYMBOL_GPL(generic_unregister_brick_type);
int generic_brick_init_full(
void *data,
int size,
struct generic_brick_type *brick_type,
struct generic_input_type **input_types,
struct generic_output_type **output_types,
char **names)
{
struct generic_brick *brick = data;
int status;
int i;
MARS_DBG("generic_brick_init_full()\n");
// first, call the generic constructors
status = generic_brick_init(brick_type, brick, *names++);
if (status)
return status;
data += brick_type->brick_size;
size -= brick_type->brick_size;
if (size < 0)
return -ENOMEM;
if (!input_types) {
MARS_DBG("generic_brick_init_full: switch to default input_types\n");
input_types = brick_type->default_input_types;
names = brick_type->default_input_names;
}
if (input_types) {
MARS_DBG("generic_brick_init_full: input_types\n");
brick->inputs = data;
data += sizeof(void*) * brick_type->max_inputs;
size -= sizeof(void*) * brick_type->max_inputs;
if (size < 0)
return -1;
for (i = 0; i < brick_type->max_inputs; i++) {
struct generic_input *input = data;
struct generic_input_type *type = *input_types++;
MARS_DBG("generic_brick_init_full: calling generic_input_init()\n");
status = generic_input_init(brick, i, type, input, names ? *names++ : type->type_name);
if (status)
return status;
data += type->input_size;
size -= type->input_size;
if (size < 0)
return -ENOMEM;
}
}
if (!output_types) {
MARS_DBG("generic_brick_init_full: switch to default output_types\n");
output_types = brick_type->default_output_types;
names = brick_type->default_output_names;
}
if (output_types) {
MARS_DBG("generic_brick_init_full: output_types\n");
brick->outputs = data;
data += sizeof(void*) * brick_type->max_outputs;
size -= sizeof(void*) * brick_type->max_outputs;
if (size < 0)
return -1;
for (i = 0; i < brick_type->max_outputs; i++) {
struct generic_output *output = data;
struct generic_output_type *type = *output_types++;
MARS_DBG("generic_brick_init_full: calling generic_output_init()\n");
generic_output_init(brick, i, type, output, names ? *names++ : type->type_name);
if (status)
return status;
data += type->output_size;
size -= type->output_size;
if (size < 0)
return -ENOMEM;
}
}
// call the specific constructors
MARS_DBG("generic_brick_init_full: call specific contructors.\n");
if (brick_type->brick_construct) {
MARS_DBG("generic_brick_init_full: calling brick_construct()\n");
status = brick_type->brick_construct(brick);
if (status)
return status;
}
for (i = 0; i < brick_type->max_inputs; i++) {
struct generic_input *input = brick->inputs[i];
if (!input)
continue;
if (!input->type) {
MARS_ERR("input has no associated type!\n");
continue;
}
if (input->type->input_construct) {
MARS_DBG("generic_brick_init_full: calling input_construct()\n");
status = input->type->input_construct(input);
if (status)
return status;
}
}
for (i = 0; i < brick_type->max_outputs; i++) {
struct generic_output *output = brick->outputs[i];
if (!output)
continue;
if (!output->type) {
MARS_ERR("output has no associated type!\n");
continue;
}
if (output->type->output_construct) {
MARS_DBG("generic_brick_init_full: calling output_construct()\n");
status = output->type->output_construct(output);
if (status)
return status;
}
}
return 0;
}
EXPORT_SYMBOL_GPL(generic_brick_init_full);
int generic_brick_exit_full(struct generic_brick *brick)
{
int i;
int status;
// first, check all outputs
for (i = 0; i < brick->nr_outputs; i++) {
struct generic_output *output = brick->outputs[i];
if (!output)
continue;
if (!output->type) {
MARS_ERR("output has no associated type!\n");
continue;
}
if (output->nr_connected) {
MARS_DBG("output is connected!\n");
return -EPERM;
}
}
// ok, test succeeded. start desctruction...
for (i = 0; i < brick->type->max_outputs; i++) {
struct generic_output *output = brick->outputs[i];
if (!output)
continue;
if (!output->type) {
MARS_ERR("output has no associated type!\n");
continue;
}
if (output->type->output_destruct) {
MARS_DBG("generic_brick_exit_full: calling output_destruct()\n");
status = output->type->output_destruct(output);
if (status)
return status;
brick->outputs[i] = NULL; // others may remain leftover
}
}
for (i = 0; i < brick->type->max_inputs; i++) {
struct generic_input *input = brick->inputs[i];
if (!input)
continue;
if (!input->type) {
MARS_ERR("input has no associated type!\n");
continue;
}
if (input->type->input_destruct) {
MARS_DBG("generic_brick_exit_full: calling input_destruct()\n");
status = input->type->input_destruct(input);
if (status)
return status;
brick->inputs[i] = NULL; // others may remain leftover
status = generic_disconnect(input);
if (status)
return status;
}
}
if (brick->type->brick_destruct) {
MARS_DBG("generic_brick_exit_full: calling brick_destruct()\n");
status = brick->type->brick_destruct(brick);
if (status)
return status;
}
return 0;
}
EXPORT_SYMBOL_GPL(generic_brick_exit_full);
int generic_brick_exit_recursively(struct generic_brick *brick)
{
int final_status = 0;
LIST_HEAD(head);
list_add(&brick->tmp_head, &head);
while (!list_empty(&head)) {
int i;
int status;
brick = container_of(head.next, struct generic_brick, tmp_head);
for (i = 0; i < brick->nr_outputs; i++) {
struct generic_output *output = brick->outputs[i];
if (output->nr_connected) {
list_del(&brick->tmp_head);
continue;
}
}
list_del(&brick->tmp_head);
for (i = 0; i < brick->nr_inputs; i++) {
struct generic_input *input = brick->inputs[i];
if (input->connect) {
struct generic_brick *other = input->connect->brick;
list_add(&other->tmp_head, &head);
}
}
status = generic_brick_exit_full(brick);
if (status)
final_status = status;
}
return final_status;
}
EXPORT_SYMBOL_GPL(generic_brick_exit_recursively);
/////////////////////////////////////////////////////////////////////
static int __init init_mars(void)
{
printk(MARS_INFO "init_mars()\n");
return 0;
}
static void __exit exit_mars(void)
{
printk(MARS_INFO "exit_mars()\n");
}
MODULE_DESCRIPTION("MARS block storage");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_mars);
module_exit(exit_mars);

256
mars_if_device.c Normal file
View File

@ -0,0 +1,256 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
/* Interface to a Linux device.
* 1 Input, 0 Outputs.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_if_device.h"
///////////////////////// own static definitions ////////////////////////
static int device_minor = 0;
///////////////////////// linux operations ////////////////////////
/* callback
*/
static int if_device_endio(struct mars_io *mio)
{
struct bio *bio = mio->orig_bio;
if (bio) {
if (!bio->bi_size) {
bio_endio(bio, 0);
} else {
MARS_ERR("NYI: RETRY LOGIC\n");
bio_endio(bio, -EIO);
}
} // else lower layers have already signalled the orig_bio
return 0;
}
/* accept a linux bio, wrap it into struct mars_io and call mars_io() on it.
*/
static int if_device_make_request(struct request_queue *q, struct bio *bio)
{
struct if_device_input *input = q->queuedata;
struct if_device_output *output;
struct mars_io *mio;
int error = -ENOSYS;
MARS_DBG("make_request(%d)\n", bio->bi_size);
#if 0
bio->bi_size = 0;
bio_endio(bio, 0);
#else
if (!input || !input->connect)
goto err;
output = input->connect;
if (!output->ops || !output->ops->mars_io)
goto err;
error = -ENOMEM;
mio = kzalloc(input->mio_size, GFP_KERNEL);
if (!mio)
goto err;
mio->orig_bio = bio;
mio->mars_bio = NULL;
mio->mars_endio = if_device_endio;
error = output->ops->mars_io(output, mio);
if (error)
goto err_free;
#endif
return 0;
err_free:
kfree(mio);
err:
bio_endio(bio, error);
return 0;
}
static int if_device_open(struct block_device *bdev, fmode_t mode)
{
struct if_device_input *input = bdev->bd_disk->private_data;
(void)input;
MARS_DBG("if_device_open()\n");
return 0;
}
static int if_device_release(struct gendisk *gd, fmode_t mode)
{
MARS_DBG("if_device_close()\n");
return 0;
}
static const struct block_device_operations if_device_blkdev_ops = {
.owner = THIS_MODULE,
.open = if_device_open,
.release = if_device_release,
};
////////////////// own brick / input / output operations //////////////////
//////////////////////// contructors / destructors ////////////////////////
static int if_device_brick_construct(struct if_device_brick *brick)
{
return 0;
}
static int if_device_brick_destruct(struct if_device_brick *brick)
{
return 0;
}
static int if_device_input_construct(struct if_device_input *input)
{
struct request_queue *q;
struct gendisk *disk;
int minor;
MARS_DBG("1\n");
q = blk_alloc_queue(GFP_KERNEL);
if (!q) {
MARS_ERR("cannot allocate device request queue\n");
return -ENOMEM;
}
q->queuedata = input;
input->q = q;
MARS_DBG("2\n");
disk = alloc_disk(1);
if (!disk) {
MARS_ERR("cannot allocate gendisk\n");
return -ENOMEM;
}
MARS_DBG("3\n");
minor = device_minor++; //TODO: protect against races (e.g. atomic_t)
disk->queue = q;
disk->major = MARS_MAJOR; //TODO: make this dynamic for >256 devices
disk->first_minor = minor;
disk->fops = &if_device_blkdev_ops;
sprintf(disk->disk_name, "mars%d", minor);
MARS_DBG("created device name %s\n", disk->disk_name);
disk->private_data = input;
set_capacity(disk, 1024); //XXX
blk_queue_make_request(q, if_device_make_request);
blk_queue_max_segment_size(q, MARS_MAX_SEGMENT_SIZE);
blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
MARS_DBG("4\n");
input->bdev = bdget(MKDEV(disk->major, minor));
/* we have no partitions. we contain only ourselves. */
input->bdev->bd_contains = input->bdev;
#if 0 // ???
q->backing_dev_info.congested_fn = mars_congested;
q->backing_dev_info.congested_data = input;
#endif
#if 0 // ???
blk_queue_merge_bvec(q, mars_merge_bvec);
q->queue_lock = &input->req_lock; /* needed since we use */
/* plugging on a queue, that actually has no requests! */
q->unplug_fn = mars_unplug_fn;
#endif
MARS_DBG("99999\n");
add_disk(disk);
input->disk = disk;
input->mio_size = 1024; //TODO: make this dynamic
return 0;
}
static int if_device_input_destruct(struct if_device_input *input)
{
if (input->bdev)
bdput(input->bdev);
if (input->disk) {
del_gendisk(input->disk);
//put_disk(input->disk);
}
if (input->q)
blk_cleanup_queue(input->q);
return 0;
}
///////////////////////// static structs ////////////////////////
static struct if_device_brick_ops if_device_brick_ops = {
};
static struct if_device_input_type if_device_input_type = {
.type_name = "if_device_input",
.input_size = sizeof(struct if_device_input),
.input_construct = &if_device_input_construct,
.input_destruct = &if_device_input_destruct,
};
static struct if_device_input_type *if_device_input_types[] = {
&if_device_input_type,
};
struct if_device_brick_type if_device_brick_type = {
.type_name = "if_device_brick",
.brick_size = sizeof(struct if_device_brick),
.max_inputs = 1,
.max_outputs = 0,
.master_ops = &if_device_brick_ops,
.default_input_types = if_device_input_types,
.brick_construct = &if_device_brick_construct,
.brick_destruct = &if_device_brick_destruct,
};
EXPORT_SYMBOL_GPL(if_device_brick_type);
////////////////// module init stuff /////////////////////////
static void __exit exit_if_device(void)
{
int status;
printk(MARS_INFO "exit_if_device()\n");
status = if_device_unregister_brick_type();
unregister_blkdev(DRBD_MAJOR, "mars");
}
static int __init init_if_device(void)
{
int status;
printk(MARS_INFO "init_if_device()\n");
status = register_blkdev(DRBD_MAJOR, "mars");
if (status)
return status;
status = if_device_register_brick_type();
if (status)
goto err_device;
return status;
err_device:
MARS_ERR("init_if_device() status=%d\n", status);
exit_if_device();
return status;
}
MODULE_DESCRIPTION("MARS if_device");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_if_device);
module_exit(exit_if_device);

18
mars_if_device.h Normal file
View File

@ -0,0 +1,18 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
struct if_device_brick {
MARS_BRICK(if_device);
};
struct if_device_input {
MARS_INPUT(if_device);
struct request_queue *q;
struct gendisk *disk;
struct block_device *bdev;
int mio_size;
};
struct if_device_output {
MARS_OUTPUT(if_device);
};
MARS_TYPES(if_device);

89
mars_test.c Normal file
View File

@ -0,0 +1,89 @@
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#define _STRATEGY
#include "mars.h"
#include "mars_if_device.h"
#include "mars_device_sync.h"
GENERIC_MAKE_CONNECT(if_device, device_sync);
static struct if_device_brick *if_brick = NULL;
static struct device_sync_brick *device_brick = NULL;
void make_test_instance(void)
{
static char *names[] = { "brick" };
int size = 1024;
int status;
void *mem = kzalloc(size, GFP_KERNEL);
if (!mem) {
MARS_ERR("cannot grab test memory\n");
return;
}
MARS_DBG("starting....\n");
status = device_sync_brick_init_full(mem, size, &device_sync_brick_type, NULL, NULL, names);
MARS_DBG("done (status=%d)\n", status);
if (!status)
device_brick = mem;
mem = kzalloc(size, GFP_KERNEL);
if (!mem) {
MARS_ERR("cannot grab test memory\n");
return;
}
status = if_device_brick_init_full(mem, size, &if_device_brick_type, NULL, NULL, names);
MARS_DBG("done (status=%d)\n", status);
if (!status)
if_brick = mem;
status = if_device_device_sync_connect(if_brick->inputs[0], device_brick->outputs[0]);
MARS_DBG("connect (status=%d)\n", status);
}
void destroy_test_instance(void)
{
if (if_brick) {
if_device_device_sync_disconnect(if_brick->inputs[0]);
if_device_brick_exit_full(if_brick);
kfree(if_brick);
if_brick = NULL;
}
if (device_brick) {
device_sync_brick_exit_full(device_brick);
kfree(device_brick);
device_brick = NULL;
}
}
static void __exit exit_test(void)
{
MARS_DBG("destroy_test_instance()\n");
destroy_test_instance();
}
static int __init init_test(void)
{
MARS_DBG("make_test_instance()\n");
make_test_instance();
return 0;
}
MODULE_DESCRIPTION("MARS TEST");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_test);
module_exit(exit_test);