summaryrefslogtreecommitdiffstats
path: root/src/kernel
diff options
context:
space:
mode:
authorJohann Latocha2012-01-19 17:20:02 +0100
committerJohann Latocha2012-01-19 17:20:02 +0100
commitb7fd7219ce4fc73939c912d4a02f5b8e4301ede7 (patch)
tree4a36476deebc1b7c66c459472a6b0183eaff3b47 /src/kernel
parentinitial commit (diff)
downloaddnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.tar.gz
dnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.tar.xz
dnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.zip
First working version :)
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/blk.c76
-rw-r--r--src/kernel/blk.h33
-rw-r--r--src/kernel/core.c94
-rw-r--r--src/kernel/dnbd3.h47
-rw-r--r--src/kernel/main.c216
-rw-r--r--src/kernel/main.c.bak322
-rw-r--r--src/kernel/net.c206
-rw-r--r--src/kernel/net.h32
-rw-r--r--src/kernel/utils.c33
-rw-r--r--src/kernel/utils.h26
10 files changed, 547 insertions, 538 deletions
diff --git a/src/kernel/blk.c b/src/kernel/blk.c
new file mode 100644
index 0000000..bf7f4e8
--- /dev/null
+++ b/src/kernel/blk.c
@@ -0,0 +1,76 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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 "blk.h"
+#include "net.h"
+
+struct block_device_operations dnbd3_blk_ops =
+{ .owner = THIS_MODULE, .ioctl = dnbd3_blk_ioctl, };
+
+int dnbd3_blk_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
+ unsigned long arg)
+{
+ switch (cmd)
+ {
+ case IOCTL_SET_HOST:
+ _host = (char *) arg;
+ break;
+
+ case IOCTL_SET_PORT:
+ _port = (char *) arg;
+ break;
+
+ case IOCTL_CONNECT:
+ dnbd3_net_connect();
+ break;
+ case BLKFLSBUF:
+ break;
+
+ default:
+ return -1;
+
+ }
+ return 0;
+}
+
+void dnbd3_blk_request(struct request_queue *q)
+{
+ struct request *req;
+
+ if (!_sock)
+ return;
+
+ while ((req = blk_fetch_request(q)) != NULL)
+ {
+ if (req->cmd_type != REQ_TYPE_FS)
+ {
+ __blk_end_request_all(req, 0);
+ continue;
+ }
+
+ if (rq_data_dir(req) == READ)
+ {
+ list_add_tail(&req->queuelist, &_request_queue_send);
+ spin_unlock_irq(q->queue_lock);
+ wake_up(&_process_queue_send);
+ spin_lock_irq(q->queue_lock);
+ }
+ }
+}
diff --git a/src/kernel/blk.h b/src/kernel/blk.h
new file mode 100644
index 0000000..d2e8162
--- /dev/null
+++ b/src/kernel/blk.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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.
+ *
+ */
+
+#ifndef BLK_H_
+#define BLK_H_
+
+#include "dnbd3.h"
+
+extern struct block_device_operations dnbd3_blk_ops;
+
+int dnbd3_blk_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
+ unsigned long arg);
+
+void dnbd3_blk_request(struct request_queue *q);
+
+#endif /* BLK_H_ */
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 <johann@latocha.de>
+ *
+ * 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");
diff --git a/src/kernel/dnbd3.h b/src/kernel/dnbd3.h
new file mode 100644
index 0000000..11eee48
--- /dev/null
+++ b/src/kernel/dnbd3.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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.
+ *
+ */
+
+#ifndef DNBD_H_
+#define DNBD_H_
+
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/blkdev.h>
+#include <net/sock.h>
+
+#include "config.h"
+#include "types.h"
+
+// block
+extern struct gendisk *disk;
+extern spinlock_t dnbd3_lock;
+
+// network
+extern char* _host;
+extern char* _port;
+extern struct socket *_sock;
+
+// process
+extern wait_queue_head_t _process_queue_send;
+extern wait_queue_head_t _process_queue_receive;
+extern struct list_head _request_queue_send;
+extern struct list_head _request_queue_receive;
+
+#endif /* DNBD_H_ */
diff --git a/src/kernel/main.c b/src/kernel/main.c
deleted file mode 100644
index 1802b5f..0000000
--- a/src/kernel/main.c
+++ /dev/null
@@ -1,216 +0,0 @@
-#include <linux/fs.h>
-#include <linux/module.h>
-#include <linux/blkdev.h>
-#include <net/sock.h>
-
-// Own
-#include "config.h"
-#include "include/types.h"
-
-static int major;
-static struct gendisk *disk;
-static struct request_queue *dnbd3_queue;
-
-DEFINE_SPINLOCK( dnbd3_lock);
-
-static struct socket *_sock;
-static struct dnbd3_request _dnbd3_request;
-static struct dnbd3_reply _dnbd3_reply;
-
-static char* host;
-static char* port;
-
-unsigned int inet_addr(char *str)
-{
- int a, b, c, d;
- char arr[4];
- sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
- arr[0] = a;
- arr[1] = b;
- arr[2] = c;
- arr[3] = d;
- return *(unsigned int*) arr;
-}
-
-void connect(void)
-{
- if (!host || !port)
- {
- printk("ERROR: Host or port not set.");
- return;
- }
-
- // initialize socket
- struct sockaddr_in sin;
- if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &_sock) < 0)
- {
- printk("ERROR: dnbd3 couldn't create socket.\n");
- return;
- }
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = inet_addr(host);
- sin.sin_port = htons(simple_strtol(port, NULL, 10));
- if (kernel_connect(_sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0)
- {
- printk("ERROR: dnbd3 couldn't connect to given host.\n");
- return;
- }
-
- // prepare message
- struct msghdr msg;
- struct kvec iov;
- _sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
-
- // send request
- _dnbd3_request.cmd = CMD_GET_SIZE;
- iov.iov_base = &_dnbd3_request;
- iov.iov_len = sizeof(_dnbd3_request);
- kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_request));
-
- // receive replay
- iov.iov_base = &_dnbd3_reply;
- iov.iov_len = sizeof(_dnbd3_reply);
- kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_reply), msg.msg_flags);
-
- // set filesize
- printk("INFO: dnbd3 filesize: %llu\n", _dnbd3_reply.filesize);
- set_capacity(disk, _dnbd3_reply.filesize >> 9); /* 512 Byte blocks */
-}
-
-void dnbd3_request(struct request_queue *q)
-{
- if (!_sock)
- return;
-
- struct request *req;
- struct msghdr msg;
- struct kvec iov;
-
- _sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
-
- while ((req = blk_fetch_request(q)) != NULL)
- {
- if (req->cmd_type != REQ_TYPE_FS)
- {
- if (!__blk_end_request_cur(req, 0))
- req = blk_fetch_request(q);
- continue;
- }
-
- spin_unlock_irq(q->queue_lock);
- if (rq_data_dir(req) == READ)
- {
- _dnbd3_request.cmd = CMD_GET_BLOCK;
- _dnbd3_request.offset = blk_rq_pos(req) << 9; // *512
- _dnbd3_request.size = blk_rq_bytes(req); // blk_rq_bytes() Returns bytes left to complete in the entire request
-
- // send request
- iov.iov_base = &_dnbd3_request;
- iov.iov_len = sizeof(_dnbd3_request);
- kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_request));
-
- // receive replay
- struct req_iterator iter;
- struct bio_vec *bvec;
- rq_for_each_segment(bvec, req, iter)
- {
- iov.iov_base = kmap(bvec->bv_page) + bvec->bv_offset;
- iov.iov_len = bvec->bv_len;
- kernel_recvmsg(_sock, &msg, &iov, 1, bvec->bv_len, msg.msg_flags);
- kunmap(bvec->bv_page);
- }
- }
-
- spin_lock_irq(q->queue_lock);
- __blk_end_request_all(req, 0);
- }
-}
-
-int dnbd3_ioctl(struct block_device *bdev, fmode_t mode,
- unsigned int cmd, unsigned long arg)
-{
- switch (cmd)
- {
- case IOCTL_SET_HOST:
- host = (char *) arg;
- break;
-
- case IOCTL_SET_PORT:
- port = (char *) arg;
- break;
-
- case IOCTL_CONNECT:
- connect();
- break;
- case BLKFLSBUF:
- // TODO: if missing, hdparm tells "BLKFLSBUF failed: Operation not permitted". Figure out what this should do.
- break;
-
- default:
- return -1;
-
- }
- return 0;
-}
-
-struct block_device_operations dnbd3_ops =
-{ .owner = THIS_MODULE, .ioctl = dnbd3_ioctl, };
-
-static int __init dnbd3_init(void)
-{
- // Init blkdev
- 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_ops;
-
- if ((dnbd3_queue = blk_init_queue(&dnbd3_request, &dnbd3_lock)) == NULL)
- {
- printk("ERROR: dnbd3 blk_init_queue failed.\n");
- return -EIO;
- }
-
- blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set logical block size for the queue
- disk->queue = dnbd3_queue;
-
- add_disk(disk);
- 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");
diff --git a/src/kernel/main.c.bak b/src/kernel/main.c.bak
deleted file mode 100644
index 600ed3c..0000000
--- a/src/kernel/main.c.bak
+++ /dev/null
@@ -1,322 +0,0 @@
-#include <linux/fs.h>
-#include <linux/module.h>
-#include <linux/blkdev.h>
-
-// Own
-#include "config.h"
-#include "include/types.h"
-
-// Network
-#include <net/sock.h>
-//#include <linux/net.h>
-//#include <net/ip.h>
-
-static int major;
-static struct gendisk *disk;
-static struct request_queue *dnbd3_queue;
-
-DEFINE_SPINLOCK( dnbd3_lock);
-
-struct socket *sock;
-struct dnbd3_request r;
-struct dnbd3_reply rp;
-
-uint64_t filesize;
-
-char* host;
-char* port;
-
-// TODO: schauen obs nicht eine fertige gibt
-static unsigned int inet_addr(char *str)
-{
- int a, b, c, d;
- char arr[4];
- sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
- arr[0] = a;
- arr[1] = b;
- arr[2] = c;
- arr[3] = d;
- return *(unsigned int*) arr;
-}
-
-static void connect()
-{
- if (!host || !port)
- {
- printk("ERROR: Host or port not set.");
- return;
- }
-
- // Init kernel-socket
-// static unsigned char address[4] =
-// { 132, 230, 4, 29 };
-
- struct sockaddr_in sin;
-
- if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock) < 0)
- {
- printk("DNBD3: Couldn't create socket.\n");
- }
- sin.sin_family = AF_INET;
- //memcpy(&sin.sin_addr.s_addr, address, 4);
- sin.sin_addr.s_addr = inet_addr(host);
- sin.sin_port = htons(simple_strtol(port, NULL, 10));
- if (kernel_connect(sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0)
- {
- printk("DNBD3: Couldn't connect.\n");
- return;
- }
-
- // Ask for filesize
- struct msghdr msg;
- struct kvec iov;
- int size, result;
- void *buf = &r;
- r.cmd = CMD_GET_SIZE;
- size = sizeof(r);
- do
- {
- sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- iov.iov_base = (char *) buf;
- iov.iov_len = size;
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE
-
- result = kernel_sendmsg(sock, &msg, &iov, 1, size);
-
- if (result <= 0)
- {
- if (result == 0)
- result = -EPIPE; /* short read */
- break;
- }
- size -= result;
- buf += result;
- } while (size > 0);
- buf = &rp;
- size = sizeof(rp);
- do
- {
- sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- iov.iov_base = (char *) buf;
- iov.iov_len = size;
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE
-
- result = kernel_recvmsg(sock, &msg, &iov, 1, size, msg.msg_flags);
-
- if (result <= 0)
- {
- if (result == 0)
- result = -EPIPE; /* short read */
- break;
- }
- size -= result;
- buf += result;
- } while (size > 0);
-
- filesize = rp.num;
- printk("Filesize: %llu\n", filesize);
-
- //filesize = 721127424;
- set_capacity(disk, filesize >> 9); /* 512 Byte blocks */
-
-}
-
-static void dnbd3_request(struct request_queue *q)
-{
- if (!sock)
- return;
-
- struct request *req;
- unsigned long start, to_copy;
- int size, result;
- void *buf;
- struct msghdr msg;
- struct kvec iov;
-
- while ((req = blk_fetch_request(q)) != NULL)
- {
- if (req->cmd_type != REQ_TYPE_FS)
- {
- if (!__blk_end_request_cur(req, 0))
- req = blk_fetch_request(q);
- continue;
- }
- start = blk_rq_pos(req) << 9; // *512
-
- //to_copy = blk_rq_cur_bytes(req); // Returns bytes left to complete in the current segment
- to_copy = blk_rq_bytes(req); // blk_rq_bytes() Returns bytes left to complete in the entire request
-
- spin_unlock_irq(q->queue_lock);
- if ((start + to_copy) <= filesize)
- {
- if (rq_data_dir(req) == READ)
- {
- // Send msg
- //printk("Send...\n");
- buf = &r;
- r.cmd = CMD_GET_BLOCK;
- r.num = start;
- r.to_copy = to_copy;
- size = sizeof(r);
- do
- {
- sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- iov.iov_base = (char *) buf;
- iov.iov_len = size;
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
-
- result = kernel_sendmsg(sock, &msg, &iov, 1, size);
-
- if (result <= 0)
- {
- if (result == 0)
- result = -EPIPE; /* short read */
- break;
- }
- size -= result;
- buf += result;
- } while (size > 0);
-
- // Test - receive msg
- struct req_iterator iter;
- struct bio_vec *bvec;
- rq_for_each_segment(bvec, req, iter)
- {
- void *kaddr = kmap(bvec->bv_page);
- buf = kaddr + bvec->bv_offset;
- size = bvec->bv_len;
- do
- {
- sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
- iov.iov_base = buf;
- iov.iov_len = size;
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
-
- result = kernel_recvmsg(sock, &msg, &iov, 1, size,
- msg.msg_flags);
-
- if (result <= 0)
- {
- if (result == 0)
- result = -EPIPE; /* short read */
- break;
- }
- size -= result;
- buf += result;
-
- if (size > 0)
- printk("SIZE > 0\n");
-
- } while (size > 0);
-
- kunmap(bvec->bv_page);
- }
-
- }
- else
- {
- printk("ERROR: Write not supported.");
- }
- }
- else
- {
- printk("ERROR: %ld not in range...\n", start + to_copy);
- }
- spin_lock_irq(q->queue_lock);
- __blk_end_request_all(req, 0);
- }
-}
-
-static int dnbd3_ioctl(struct block_device *bdev, fmode_t mode,
- unsigned int cmd, unsigned long arg)
-{
- switch (cmd)
- {
-
- case IOCTL_SET_HOST:
- host = (char *) arg;
- break;
-
- case IOCTL_SET_PORT:
- port = (char *) arg;
- break;
-
- case IOCTL_CONNECT:
- connect();
- break;
-
- default:
- return -1;
-
- }
- return 0;
-}
-
-static struct block_device_operations dnbd3_ops =
-{ .owner = THIS_MODULE, .ioctl = dnbd3_ioctl, };
-
-static int __init dnbd3_init(void)
-{
-
- // Init blkdev
- if ((major = register_blkdev(0, "dnbd")) == 0)
- {
- printk("dnbd3: can't get majornumber\n");
- return -EIO;
- }
- printk("major: %d\n", major);
- if (!(disk = alloc_disk(1)))
- {
- printk("alloc_disk failed ...\n");
- goto out;
- }
- disk->major = major;
- disk->first_minor = 0;
- sprintf(disk->disk_name, "dnbd0");
- //set_capacity(disk, filesize >> 9); /* 512 Byte blocks */
- set_capacity(disk, 0);
- disk->fops = &dnbd3_ops;
-
- if ((dnbd3_queue = blk_init_queue(&dnbd3_request, &dnbd3_lock)) == NULL)
- goto out;
-
- blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set logical block size for the queue
-// blk_queue_max_hw_sectors(dnbd3_queue, DNBD3_BLOCK_SIZE / KERNEL_SECTOR_SIZE); // set max sectors for a request for this queue, min 8
-// blk_queue_max_segments(dnbd3_queue, 1); // set max hw segments for a request for this queue
-// blk_queue_max_segment_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set max segment size for blk_rq_map_sg, min 4096
- disk->queue = dnbd3_queue;
-
- add_disk(disk);
- return 0;
- out: return -EIO;
-}
-
-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);
-}
-
-module_init( dnbd3_init);
-module_exit( dnbd3_exit);
-MODULE_LICENSE("GPL");
diff --git a/src/kernel/net.c b/src/kernel/net.c
new file mode 100644
index 0000000..0e7a871
--- /dev/null
+++ b/src/kernel/net.c
@@ -0,0 +1,206 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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 "net.h"
+#include "utils.h"
+
+void dnbd3_net_connect(void)
+{
+ struct sockaddr_in sin;
+ struct msghdr msg;
+ struct kvec iov;
+ struct dnbd3_request dnbd3_request;
+ struct dnbd3_reply dnbd3_reply;
+ struct task_struct *thread_send;
+ struct task_struct *thread_receive;
+
+ if (!_host || !_port)
+ {
+ printk("ERROR: Host or port not set.");
+ return;
+ }
+
+ // initialize socket
+ if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &_sock) < 0)
+ {
+ printk("ERROR: dnbd3 couldn't create socket.\n");
+ return;
+ }
+ _sock->sk->sk_allocation = GFP_NOIO;
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = inet_addr(_host);
+ sin.sin_port = htons(simple_strtol(_port, NULL, 10));
+ if (kernel_connect(_sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0)
+ {
+ printk("ERROR: dnbd3 couldn't connect to given host.\n");
+ return;
+ }
+
+ // prepare message and send request
+ dnbd3_request.cmd = CMD_GET_SIZE;
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+ msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
+
+ iov.iov_base = &dnbd3_request;
+ iov.iov_len = sizeof(dnbd3_request);
+ kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_request));
+
+ // receive replay
+ iov.iov_base = &dnbd3_reply;
+ iov.iov_len = sizeof(dnbd3_reply);
+ kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_reply), msg.msg_flags);
+
+ // set filesize
+ // FIXME: files > 4GB
+ printk("INFO: dnbd3 filesize: %llu\n", dnbd3_reply.filesize);
+ set_capacity(disk, dnbd3_reply.filesize >> 9); /* 512 Byte blocks */
+
+ // start sending thread
+ thread_send = kthread_create(dnbd3_net_send, NULL, "none");
+ wake_up_process(thread_send);
+
+ // start receiving thread
+ thread_receive = kthread_create(dnbd3_net_receive, NULL, "none");
+ wake_up_process(thread_receive);
+}
+
+int dnbd3_net_send(void *data)
+{
+ struct dnbd3_request dnbd3_request;
+ struct request *blk_request;
+ struct msghdr msg;
+ struct kvec iov;
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+ msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
+
+ set_user_nice(current, -20);
+
+ while (!kthread_should_stop() || !list_empty(&_request_queue_send))
+ {
+ wait_event_interruptible(_process_queue_send,
+ kthread_should_stop() || !list_empty(&_request_queue_send));
+
+ if (list_empty(&_request_queue_send))
+ continue;
+
+ // extract block request
+ spin_lock_irq(&dnbd3_lock);
+ blk_request = list_entry(_request_queue_send.next, struct request, queuelist);
+ list_del_init(&blk_request->queuelist);
+ spin_unlock_irq(&dnbd3_lock);
+
+ // prepare net request
+ dnbd3_request.cmd = CMD_GET_BLOCK;
+ dnbd3_request.offset = blk_rq_pos(blk_request) << 9; // *512
+ dnbd3_request.size = blk_rq_bytes(blk_request); // blk_rq_bytes() Returns bytes left to complete in the entire request
+ memcpy(dnbd3_request.handle, &blk_request, sizeof(blk_request));
+ iov.iov_base = &dnbd3_request;
+ iov.iov_len = sizeof(dnbd3_request);
+
+ // send net request
+ if (kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_request)) <= 0)
+ printk("ERROR: kernel_sendmsg\n");
+
+ spin_lock_irq(&dnbd3_lock);
+ list_add_tail(&blk_request->queuelist, &_request_queue_receive);
+ spin_unlock_irq(&dnbd3_lock);
+ wake_up(&_process_queue_receive);
+ }
+ return 0;
+}
+
+int dnbd3_net_receive(void *data)
+{
+ struct dnbd3_reply dnbd3_reply;
+ struct request *blk_request;
+ struct msghdr msg;
+ struct kvec iov;
+ struct req_iterator iter;
+ struct bio_vec *bvec;
+ unsigned long flags;
+ sigset_t blocked, oldset;
+ struct request *tmp_request, *received_request;
+ void *kaddr;
+ int size;
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+ msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE
+
+ set_user_nice(current, -20);
+
+ while (!kthread_should_stop() || !list_empty(&_request_queue_receive))
+ {
+ wait_event_interruptible(_process_queue_receive,
+ kthread_should_stop() || !list_empty(&_request_queue_receive));
+
+ // receive net replay
+ iov.iov_base = &dnbd3_reply;
+ iov.iov_len = sizeof(dnbd3_reply);
+ kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_reply),
+ msg.msg_flags);
+
+ // search for replied request in queue
+ received_request = *(struct request **) dnbd3_reply.handle;
+ spin_lock_irq(&dnbd3_lock);
+ list_for_each_entry_safe(blk_request, tmp_request,
+ &_request_queue_receive, queuelist)
+ {
+ if (blk_request != received_request)
+ continue;
+
+ list_del_init(&blk_request->queuelist);
+ break;
+ }
+ spin_unlock_irq(&dnbd3_lock);
+
+ // receive data and answer to block layer
+ rq_for_each_segment(bvec, blk_request, iter)
+ {
+ siginitsetinv(&blocked, sigmask(SIGKILL));
+ sigprocmask(SIG_SETMASK, &blocked, &oldset);
+
+ kaddr = kmap(bvec->bv_page) + bvec->bv_offset;
+ size = bvec->bv_len;
+ iov.iov_base = kaddr;
+ iov.iov_len = size;
+ kernel_recvmsg(_sock, &msg, &iov, 1, size, msg.msg_flags);
+ kunmap(bvec->bv_page);
+
+ sigprocmask(SIG_SETMASK, &oldset, NULL);
+ }
+
+ spin_lock_irqsave(&dnbd3_lock, flags);
+ __blk_end_request_all(blk_request, 0);
+ spin_unlock_irqrestore(&dnbd3_lock, flags);
+ }
+
+ return 0;
+}
+
diff --git a/src/kernel/net.h b/src/kernel/net.h
new file mode 100644
index 0000000..22c0cbd
--- /dev/null
+++ b/src/kernel/net.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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.
+ *
+ */
+
+#ifndef NET_H_
+#define NET_H_
+
+#include "dnbd3.h"
+
+void dnbd3_net_connect(void);
+
+int dnbd3_net_send(void *data);
+
+int dnbd3_net_receive(void *data);
+
+#endif /* NET_H_ */
diff --git a/src/kernel/utils.c b/src/kernel/utils.c
new file mode 100644
index 0000000..423ceb9
--- /dev/null
+++ b/src/kernel/utils.c
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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 <linux/kernel.h>
+
+unsigned int inet_addr(char *str)
+{
+ int a, b, c, d;
+ char arr[4];
+ sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
+ arr[0] = a;
+ arr[1] = b;
+ arr[2] = c;
+ arr[3] = d;
+ return *(unsigned int*) arr;
+}
diff --git a/src/kernel/utils.h b/src/kernel/utils.h
new file mode 100644
index 0000000..6b7fcee
--- /dev/null
+++ b/src/kernel/utils.h
@@ -0,0 +1,26 @@
+/*
+ * This file is part of the Distributed Network Block Device 3
+ *
+ * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
+ *
+ * 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.
+ *
+ */
+
+#ifndef UTILS_H_
+#define UTILS_H_
+
+unsigned int inet_addr(char *str);
+
+#endif /* UTILS_H_ */