mars/mars_if_device.c

289 lines
6.9 KiB
C
Raw Normal View History

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-07-30 05:46:22 +00:00
static int if_device_endio(struct mars_io_object *mio, int error)
2010-06-14 14:27:40 +00:00
{
struct bio *bio = mio->orig_bio;
if (bio) {
2010-06-18 11:44:14 +00:00
mio->orig_bio = NULL;
2010-07-30 05:46:22 +00:00
if (bio->bi_size && !error)
error = -EIO;
if (error) {
MARS_ERR("NYI: error=%d RETRY LOGIC %u\n", error, bio->bi_size);
2010-06-14 14:27:40 +00:00
}
2010-07-30 05:46:22 +00:00
bio_endio(bio, error);
2010-06-14 14:27:40 +00:00
} // else lower layers have already signalled the orig_bio
2010-08-02 16:31:10 +00:00
if_device_free_mars_io(mio);
2010-06-14 14:27:40 +00:00
return 0;
}
2010-06-20 18:55:34 +00:00
/* accept a linux bio, wrap it into struct mars_io_object and call mars_io() on it.
2010-06-14 14:27:40 +00:00
*/
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;
2010-06-20 18:55:34 +00:00
struct mars_io_object *mio = NULL;
2010-06-14 14:27:40 +00:00
int error = -ENOSYS;
MARS_DBG("make_request(%d)\n", bio->bi_size);
2010-06-16 13:21:30 +00:00
2010-06-14 14:27:40 +00:00
if (!input || !input->connect)
goto err;
output = input->connect;
if (!output->ops || !output->ops->mars_io)
goto err;
error = -ENOMEM;
2010-08-01 20:21:18 +00:00
mio = if_device_alloc_mars_io(output, &input->mio_object_layout);
2010-06-22 13:21:42 +00:00
if (!mio)
2010-07-30 11:50:20 +00:00
goto err;
2010-06-22 13:21:42 +00:00
2010-06-14 14:27:40 +00:00
mio->orig_bio = bio;
mio->mars_endio = if_device_endio;
error = output->ops->mars_io(output, mio);
if (error)
goto err_free;
2010-06-16 13:21:30 +00:00
2010-06-14 14:27:40 +00:00
return 0;
err_free:
2010-07-30 11:50:20 +00:00
kfree(mio);
2010-06-14 14:27:40 +00:00
err:
2010-06-16 13:21:30 +00:00
MARS_ERR("cannot submit request, status=%d\n", error);
if (!mio)
bio_endio(bio, error);
//else mars_endio() callback must have been called, which is responsible for cleanup
2010-06-14 14:27:40 +00:00
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 //////////////////
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 ///////////////
static int if_device_mars_io_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
{
return 0;
}
static int if_device_mars_buf_aspect_init_fn(struct generic_aspect *_ini, void *_init_data)
{
return 0;
}
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)
{
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;
2010-06-16 13:21:30 +00:00
int capacity = 2 * 1024 * 1024 * 4; //TODO: make this dynamic
2010-06-14 14:27:40 +00:00
2010-07-30 05:46:22 +00:00
//MARS_DBG("1\n");
2010-06-14 14:27:40 +00:00
q = blk_alloc_queue(GFP_KERNEL);
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-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;
}
///////////////////////// static structs ////////////////////////
static struct if_device_brick_ops if_device_brick_ops = {
};
2010-07-23 11:55:18 +00:00
static 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-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;
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);