2010-06-14 14:27:40 +00:00
|
|
|
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
|
|
|
|
|
|
|
|
/* Interface to a Linux device.
|
|
|
|
* 1 Input, 0 Outputs.
|
|
|
|
*/
|
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//#define BRICK_DEBUGGING
|
|
|
|
//#define MARS_DEBUGGING
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
#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;
|
|
|
|
|
2010-06-22 13:21:42 +00:00
|
|
|
//////////////// object / aspect constructors / destructors ///////////////
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
///////////////////////// linux operations ////////////////////////
|
|
|
|
|
|
|
|
/* callback
|
|
|
|
*/
|
2010-08-08 14:02:54 +00:00
|
|
|
static void _if_device_endio(struct generic_callback *cb)
|
2010-06-14 14:27:40 +00:00
|
|
|
{
|
2010-08-08 14:02:54 +00:00
|
|
|
struct mars_ref_object *mref = cb->cb_private;
|
2010-08-05 15:54:48 +00:00
|
|
|
struct bio *bio = mref->orig_bio;
|
2010-08-04 17:32:04 +00:00
|
|
|
int error;
|
|
|
|
if (unlikely(!bio)) {
|
|
|
|
MARS_FAT("callback with no bio called. something is very wrong here!\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-08 14:02:54 +00:00
|
|
|
error = cb->cb_error;
|
2010-08-04 17:32:04 +00:00
|
|
|
if (unlikely(error < 0)) {
|
|
|
|
MARS_ERR("NYI: error=%d RETRY LOGIC %u\n", error, bio->bi_size);
|
|
|
|
}
|
|
|
|
if (likely(error > 0)) { // bio conventions are slightly different...
|
|
|
|
error = 0;
|
|
|
|
bio->bi_size = 0;
|
|
|
|
}
|
|
|
|
bio_endio(bio, error);
|
2010-06-14 14:27:40 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
/* accept a linux bio, wrap it into mref and call buf_io() on it.
|
2010-06-14 14:27:40 +00:00
|
|
|
*/
|
|
|
|
static int if_device_make_request(struct request_queue *q, struct bio *bio)
|
|
|
|
{
|
2010-08-10 17:39:30 +00:00
|
|
|
struct if_device_input *input;
|
|
|
|
struct if_device_brick *brick;
|
2010-08-05 15:54:48 +00:00
|
|
|
struct mars_ref_object *mref = NULL;
|
2010-08-08 14:02:54 +00:00
|
|
|
struct if_device_mars_ref_aspect *mref_a;
|
|
|
|
struct generic_callback *cb;
|
2010-08-04 17:32:04 +00:00
|
|
|
int rw = bio->bi_rw & 1;
|
|
|
|
int error = -ENOSYS;
|
2010-06-14 14:27:40 +00:00
|
|
|
|
|
|
|
MARS_DBG("make_request(%d)\n", bio->bi_size);
|
2010-06-16 13:21:30 +00:00
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
input = q->queuedata;
|
2010-08-04 17:32:04 +00:00
|
|
|
if (unlikely(!input))
|
|
|
|
goto err;
|
2010-06-14 14:27:40 +00:00
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
brick = input->brick;
|
|
|
|
if (unlikely(!brick))
|
2010-08-04 17:32:04 +00:00
|
|
|
goto err;
|
2010-06-14 14:27:40 +00:00
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
/* THIS IS PROVISIONARY
|
|
|
|
*/
|
|
|
|
while (unlikely(!brick->is_active)) {
|
|
|
|
msleep(100);
|
|
|
|
}
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
error = -ENOMEM;
|
2010-08-10 17:39:30 +00:00
|
|
|
mref = if_device_alloc_mars_ref(&brick->hidden_output, &input->mref_object_layout);
|
2010-08-05 15:54:48 +00:00
|
|
|
if (unlikely(!mref))
|
2010-07-30 11:50:20 +00:00
|
|
|
goto err;
|
2010-06-22 13:21:42 +00:00
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
mref_a = if_device_mars_ref_get_aspect(&brick->hidden_output, mref);
|
2010-08-08 14:02:54 +00:00
|
|
|
if (unlikely(!mref_a))
|
|
|
|
goto err;
|
|
|
|
cb = &mref_a->cb;
|
|
|
|
cb->cb_fn = _if_device_endio;
|
|
|
|
cb->cb_private = mref;
|
|
|
|
cb->cb_error = 0;
|
|
|
|
cb->cb_prev = NULL;
|
|
|
|
mref->ref_cb = cb;
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
mars_ref_attach_bio(mref, bio);
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
GENERIC_INPUT_CALL(input, mars_ref_io, mref, rw);
|
2010-08-05 15:54:48 +00:00
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
GENERIC_INPUT_CALL(input, mars_ref_put, mref);
|
2010-06-16 13:21:30 +00:00
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2010-06-16 13:21:30 +00:00
|
|
|
MARS_ERR("cannot submit request, status=%d\n", error);
|
2010-08-05 15:54:48 +00:00
|
|
|
if (!mref)
|
2010-06-16 13:21:30 +00:00
|
|
|
bio_endio(bio, error);
|
2010-08-04 17:32:04 +00:00
|
|
|
return error;
|
2010-06-14 14:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 //////////////////
|
|
|
|
|
2010-06-17 16:57:10 +00:00
|
|
|
static void if_device_unplug(struct request_queue *q)
|
|
|
|
{
|
|
|
|
//struct if_device_input *input = q->queuedata;
|
|
|
|
MARS_DBG("UNPLUG\n");
|
|
|
|
queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
|
|
|
|
//blk_run_address_space(lo->lo_backing_file->f_mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
//////////////// object / aspect constructors / destructors ///////////////
|
|
|
|
|
2010-08-05 15:54:48 +00:00
|
|
|
static int if_device_mars_ref_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
|
2010-07-23 11:55:18 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-08 09:03:42 +00:00
|
|
|
static void if_device_mars_ref_aspect_exit_fn(struct generic_aspect *_ini, void *_init_data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
MARS_MAKE_STATICS(if_device);
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
//////////////////////// contructors / destructors ////////////////////////
|
|
|
|
|
|
|
|
static int if_device_brick_construct(struct if_device_brick *brick)
|
|
|
|
{
|
2010-08-10 17:39:30 +00:00
|
|
|
struct if_device_output *hidden = &brick->hidden_output;
|
|
|
|
_if_device_output_init(brick, hidden, "internal");
|
2010-06-14 14:27:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int if_device_brick_destruct(struct if_device_brick *brick)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:12:30 +00:00
|
|
|
static int if_device_switch(struct if_device_brick *brick, bool state)
|
2010-06-14 14:27:40 +00:00
|
|
|
{
|
2010-08-26 17:12:30 +00:00
|
|
|
struct if_device_input *input = brick->inputs[0];
|
2010-06-14 14:27:40 +00:00
|
|
|
struct request_queue *q;
|
|
|
|
struct gendisk *disk;
|
|
|
|
int minor;
|
2010-08-26 17:12:30 +00:00
|
|
|
struct mars_info info = {};
|
|
|
|
unsigned long capacity;
|
|
|
|
int status;
|
2010-06-14 14:27:40 +00:00
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//MARS_DBG("1\n");
|
2010-08-26 17:12:30 +00:00
|
|
|
|
|
|
|
status = GENERIC_INPUT_CALL(input, mars_get_info, &info);
|
|
|
|
if (status < 0) {
|
|
|
|
MARS_ERR("cannot get device info, status=%d\n", status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
capacity = info.current_size >> 9; // TODO: make this dynamic
|
|
|
|
|
2010-08-04 17:32:04 +00:00
|
|
|
q = blk_alloc_queue(GFP_MARS);
|
2010-06-14 14:27:40 +00:00
|
|
|
if (!q) {
|
|
|
|
MARS_ERR("cannot allocate device request queue\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2010-06-17 16:57:10 +00:00
|
|
|
q->queuedata = input;
|
2010-06-14 14:27:40 +00:00
|
|
|
input->q = q;
|
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//MARS_DBG("2\n");
|
2010-06-14 14:27:40 +00:00
|
|
|
disk = alloc_disk(1);
|
|
|
|
if (!disk) {
|
|
|
|
MARS_ERR("cannot allocate gendisk\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//MARS_DBG("3\n");
|
2010-06-14 14:27:40 +00:00
|
|
|
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;
|
2010-06-16 13:21:30 +00:00
|
|
|
set_capacity(disk, capacity);
|
2010-06-14 14:27:40 +00:00
|
|
|
|
|
|
|
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);
|
2010-06-17 16:57:10 +00:00
|
|
|
q->unplug_fn = if_device_unplug;
|
2010-06-18 11:44:14 +00:00
|
|
|
spin_lock_init(&input->req_lock);
|
|
|
|
q->queue_lock = &input->req_lock; // needed!
|
2010-06-17 19:36:06 +00:00
|
|
|
//blk_queue_ordered(q, QUEUE_ORDERED_DRAIN, NULL);//???
|
2010-06-14 14:27:40 +00:00
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//MARS_DBG("4\n");
|
2010-06-14 14:27:40 +00:00
|
|
|
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);
|
|
|
|
#endif
|
|
|
|
|
2010-06-18 11:44:14 +00:00
|
|
|
// point of no return
|
2010-07-30 05:46:22 +00:00
|
|
|
//MARS_DBG("99999\n");
|
2010-06-14 14:27:40 +00:00
|
|
|
add_disk(disk);
|
|
|
|
input->disk = disk;
|
2010-06-18 11:44:14 +00:00
|
|
|
//set_device_ro(input->bdev, 0); // TODO: implement modes
|
2010-08-26 17:12:30 +00:00
|
|
|
brick->is_active = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int if_device_input_construct(struct if_device_input *input)
|
|
|
|
{
|
2010-06-14 14:27:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
static int if_device_output_construct(struct if_device_output *output)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
///////////////////////// static structs ////////////////////////
|
|
|
|
|
|
|
|
static struct if_device_brick_ops if_device_brick_ops = {
|
2010-08-26 17:12:30 +00:00
|
|
|
.brick_switch = if_device_switch,
|
2010-06-14 14:27:40 +00:00
|
|
|
};
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
static struct if_device_output_ops if_device_output_ops = {
|
|
|
|
.make_object_layout = if_device_make_object_layout,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct if_device_input_type if_device_input_type = {
|
2010-06-14 14:27:40 +00:00
|
|
|
.type_name = "if_device_input",
|
|
|
|
.input_size = sizeof(struct if_device_input),
|
|
|
|
.input_construct = &if_device_input_construct,
|
|
|
|
.input_destruct = &if_device_input_destruct,
|
|
|
|
};
|
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
static const struct if_device_input_type *if_device_input_types[] = {
|
2010-06-14 14:27:40 +00:00
|
|
|
&if_device_input_type,
|
|
|
|
};
|
|
|
|
|
2010-08-10 17:39:30 +00:00
|
|
|
const struct if_device_output_type if_device_output_type = {
|
|
|
|
.type_name = "if_device_output",
|
|
|
|
.output_size = sizeof(struct if_device_output),
|
|
|
|
.master_ops = &if_device_output_ops,
|
|
|
|
.output_construct = &if_device_output_construct,
|
|
|
|
.aspect_types = if_device_aspect_types,
|
|
|
|
.layout_code = {
|
|
|
|
[BRICK_OBJ_MARS_REF] = LAYOUT_ALL,
|
|
|
|
}
|
|
|
|
};
|
2010-07-23 11:55:18 +00:00
|
|
|
const struct if_device_brick_type if_device_brick_type = {
|
2010-06-14 14:27:40 +00:00
|
|
|
.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;
|
2010-08-04 17:32:04 +00:00
|
|
|
|
|
|
|
(void)if_device_aspect_types; // not used, shut up gcc
|
|
|
|
|
2010-06-14 14:27:40 +00:00
|
|
|
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);
|