From b7fd7219ce4fc73939c912d4a02f5b8e4301ede7 Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Thu, 19 Jan 2012 17:20:02 +0100 Subject: First working version :) --- src/kernel/core.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/kernel/core.c (limited to 'src/kernel/core.c') diff --git a/src/kernel/core.c b/src/kernel/core.c new file mode 100644 index 0000000..3b6ce54 --- /dev/null +++ b/src/kernel/core.c @@ -0,0 +1,94 @@ +/* + * This file is part of the Distributed Network Block Device 3 + * + * Copyright(c) 2011-2012 Johann Latocha + * + * This file may be licensed under the terms of of the + * GNU General Public License Version 2 (the ``GPL''). + * + * Software distributed under the License is distributed + * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either + * express or implied. See the GPL for the specific language + * governing rights and limitations. + * + * You should have received a copy of the GPL along with this + * program. If not, go to http://www.gnu.org/licenses/gpl.html + * or write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "dnbd3.h" +#include "blk.h" + +// block +int major; +struct gendisk *disk; +struct request_queue *dnbd3_queue; +spinlock_t dnbd3_lock; + +// network +char* _host; +char* _port; +struct socket *_sock; + +// process +wait_queue_head_t _process_queue_send; +wait_queue_head_t _process_queue_receive; +struct list_head _request_queue_send; +struct list_head _request_queue_receive; + +static int __init dnbd3_init(void) +{ + // initialize queues + init_waitqueue_head(&_process_queue_send); + init_waitqueue_head(&_process_queue_receive); + INIT_LIST_HEAD(&_request_queue_send); + INIT_LIST_HEAD(&_request_queue_receive); + + // initialize block device + if ((major = register_blkdev(0, "dnbd")) == 0) + { + printk("ERROR: dnbd3 register_blkdev failed.\n"); + return -EIO; + } + if (!(disk = alloc_disk(1))) + { + printk("ERROR: dnbd3 alloc_disk failed.\n"); + return -EIO; + } + disk->major = major; + disk->first_minor = 0; + sprintf(disk->disk_name, "dnbd0"); + set_capacity(disk, 0); + set_disk_ro(disk, 1); + disk->fops = &dnbd3_blk_ops; + spin_lock_init(&dnbd3_lock); + if ((dnbd3_queue = blk_init_queue(&dnbd3_blk_request, &dnbd3_lock)) == NULL) + { + printk("ERROR: dnbd3 blk_init_queue failed.\n"); + return -EIO; + } + blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); + disk->queue = dnbd3_queue; + + add_disk(disk); // must be last + + printk("INFO: dnbd3 init successful.\n"); + return 0; +} + +static void __exit dnbd3_exit(void) +{ + if (_sock) + sock_release(_sock); + unregister_blkdev(major, "dnbd"); + del_gendisk(disk); + put_disk(disk); + blk_cleanup_queue(dnbd3_queue); + printk("INFO: dnbd3 exit.\n"); +} + +module_init( dnbd3_init); +module_exit( dnbd3_exit); +MODULE_LICENSE("GPL"); -- cgit v1.2.3-55-g7522