summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJohann Latocha2011-11-29 15:06:45 +0100
committerJohann Latocha2011-11-29 15:06:45 +0100
commit9970fb00c79834703bc990d052439290338467be (patch)
tree9311a9c766193464b269894b18b7dd4f967652a0 /src
downloaddnbd3-9970fb00c79834703bc990d052439290338467be.tar.gz
dnbd3-9970fb00c79834703bc990d052439290338467be.tar.xz
dnbd3-9970fb00c79834703bc990d052439290338467be.zip
initial commit
Diffstat (limited to 'src')
-rw-r--r--src/client/client.bak75
-rw-r--r--src/client/client.c95
-rw-r--r--src/config.h9
-rw-r--r--src/include/types.h30
-rw-r--r--src/kernel/main.c216
-rw-r--r--src/kernel/main.c.bak322
-rw-r--r--src/server/file.c58
-rw-r--r--src/server/file.h7
-rw-r--r--src/server/server.c173
-rw-r--r--src/version.h7
10 files changed, 992 insertions, 0 deletions
diff --git a/src/client/client.bak b/src/client/client.bak
new file mode 100644
index 0000000..bacfa52
--- /dev/null
+++ b/src/client/client.bak
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+#include <math.h>
+
+#include "../include/types.h"
+
+#define FILE_SIZE 721127424
+
+int main(int argc, char *argv[])
+{
+ struct sockaddr_in server;
+ unsigned long addr;
+ int sock;
+
+ // Create socket
+ sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sock < 0)
+ {
+ printf("ERROR: Socket failure\n");
+ return EXIT_FAILURE;
+ }
+
+ addr = inet_addr(HOST);
+ memcpy((char *) &server.sin_addr, &addr, sizeof(addr));
+ server.sin_family = AF_INET; // IPv4
+ server.sin_port = htons(PORT); // set port number
+
+ // Connect to server
+ if (connect(sock, (struct sockaddr*) &server, sizeof(server)) < 0)
+ {
+ printf("ERROR: Connect failure\n");
+ return EXIT_FAILURE;
+ }
+
+ // Set data
+ struct dnbd3_request request;
+ struct dnbd3_reply reply;
+ request.num = 0;
+
+ // Send to server
+ int i;
+ off_t blocks = FILE_SIZE / DNBD3_BLOCK_SIZE;
+ int e = log(DNBD3_BLOCK_SIZE) / log(2); // logarithmus dualis
+
+
+ for (i = 0; i < blocks; i++)
+ {
+ request.num = i << e; // multiplie by e
+ send(sock, (char *) &request, sizeof(request), 0);
+ recv(sock, &reply, DNBD3_BLOCK_SIZE, MSG_WAITALL);
+ write(STDOUT_FILENO, reply.data, DNBD3_BLOCK_SIZE);
+ }
+
+ /* Fetch "rest" bytes */
+ int rest = FILE_SIZE % DNBD3_BLOCK_SIZE;
+ if (rest != 0)
+ {
+ request.num = i * DNBD3_BLOCK_SIZE;
+ send(sock, (char *) &request, sizeof(request), 0);
+ recv(sock, &reply, sizeof(struct dnbd3_reply), MSG_WAITALL);
+ write(STDOUT_FILENO, reply.data, rest);
+ }
+
+ close(sock);
+ return EXIT_SUCCESS;
+}
diff --git a/src/client/client.c b/src/client/client.c
new file mode 100644
index 0000000..87f20d5
--- /dev/null
+++ b/src/client/client.c
@@ -0,0 +1,95 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include "../include/types.h"
+#include "../version.h"
+
+void print_help(char* argv_0)
+{
+ printf("Usage: %s -H <host> -p <port> -d <device>\n", argv_0);
+ printf("Start the DNBD3 client.\n");
+ printf("-H or --host \t\t Host running dnbd3-server.\n");
+ printf("-p or --port \t\t Port used by server.\n");
+ printf("-d or --device \t\t DNBD3 device name.\n");
+ printf("-h or --help \t\t Show this help text and quit.\n");
+ printf("-v or --version \t Show version and quit.\n");
+ exit(EXIT_SUCCESS);
+}
+
+void print_version()
+{
+ printf("Version: %s\n", VERSION_STRING);
+ exit(EXIT_SUCCESS);
+}
+
+int main(int argc, char *argv[])
+{
+ char *host = NULL;
+ char *port = NULL;
+ char *dev = NULL;
+
+ int opt = 0;
+ int longIndex = 0;
+ static const char *optString = "H:p:d:hv?";
+ static const struct option longOpts[] =
+ {
+ { "host", required_argument, NULL, 'H' },
+ { "port", required_argument, NULL, 'p' },
+ { "device", required_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'v' }, };
+
+ opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
+
+ while (opt != -1)
+ {
+ switch (opt)
+ {
+ case 'H':
+ host = optarg;
+ break;
+ case 'p':
+ port = optarg;
+ break;
+ case 'd':
+ dev = optarg;
+ break;
+ case 'h':
+ print_help(argv[0]);
+ break;
+ case 'v':
+ print_version();
+ break;
+ case '?':
+ print_help(argv[0]);
+ }
+ opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
+ }
+
+ if (!host || !port || !dev)
+ {
+ printf("FATAL: Not enough information specified\n");
+ exit(EXIT_FAILURE);
+ }
+
+ int fd;
+ fd = open(dev, O_RDONLY);
+
+ if (ioctl(fd, IOCTL_SET_HOST, host) < 0)
+ printf("ERROR: ioctl not successful\n");
+
+ if (ioctl(fd, IOCTL_SET_PORT, port) < 0)
+ printf("ERROR: ioctl not successful\n");
+
+ if (ioctl(fd, IOCTL_CONNECT) < 0)
+ printf("ERROR: ioctl not successful\n");
+
+ close(fd);
+
+ exit(EXIT_SUCCESS);
+}
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..ec41da2
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,9 @@
+#ifndef CONFIG_H_
+#define CONFIG_H_
+
+#define PORT 5003 // TODO: make obsolete
+
+#define KERNEL_SECTOR_SIZE 512
+#define DNBD3_BLOCK_SIZE 4096
+
+#endif /* CONFIG_H_ */
diff --git a/src/include/types.h b/src/include/types.h
new file mode 100644
index 0000000..ea34616
--- /dev/null
+++ b/src/include/types.h
@@ -0,0 +1,30 @@
+#ifndef TYPES_H_
+#define TYPES_H_
+
+#include "../config.h"
+
+#define DNBD_MAGIC 'd'
+
+#define CMD_GET_BLOCK 1
+#define CMD_GET_SIZE 2
+
+#define IOCTL_SET_HOST _IO(0xab, 1)
+#define IOCTL_SET_PORT _IO(0xab, 2)
+#define IOCTL_CONNECT _IO(0xab, 3)
+
+#pragma pack(1)
+typedef struct dnbd3_request {
+ uint16_t cmd;
+ uint64_t offset;
+ uint64_t size;
+} dnbd3_request_t;
+#pragma pack(0)
+
+#pragma pack(1)
+typedef struct dnbd3_reply {
+ uint16_t cmd;
+ uint64_t filesize;
+} dnbd3_reply_t;
+#pragma pack(0)
+
+#endif /* TYPES_H_ */
diff --git a/src/kernel/main.c b/src/kernel/main.c
new file mode 100644
index 0000000..1802b5f
--- /dev/null
+++ b/src/kernel/main.c
@@ -0,0 +1,216 @@
+#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
new file mode 100644
index 0000000..600ed3c
--- /dev/null
+++ b/src/kernel/main.c.bak
@@ -0,0 +1,322 @@
+#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/server/file.c b/src/server/file.c
new file mode 100644
index 0000000..c53ca15
--- /dev/null
+++ b/src/server/file.c
@@ -0,0 +1,58 @@
+#include <fcntl.h>
+#include <errno.h>
+#include "file.h"
+
+int file_open(char *filename)
+{
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ return -1;
+
+ struct stat st;
+ if (fstat(fd, &st) == -1)
+ return -1;
+
+ return fd;
+}
+
+int file_getsize(int fd, off_t *size)
+{
+ *size = lseek64(fd, 0, SEEK_END);
+
+ if (*size == -1)
+ return -1;
+
+ return 0;
+}
+
+int file_read(int fd, void *buf, size_t size, off_t pos)
+{
+ off_t newpos = lseek(fd, pos, SEEK_SET);
+
+ if (newpos == -1)
+ return -1;
+
+ size_t nleft = size;
+ ssize_t nread;
+ char *ptr = buf;
+
+ while (nleft > 0)
+ {
+ if ((nread = read(fd, ptr, nleft)) < 0)
+ {
+ if (errno == EINTR)
+ continue;
+
+ return -1;
+ }
+ if (nread == 0)
+ {
+ break;
+ }
+
+ nleft -= nread;
+ ptr += nread;
+ }
+
+ return 0;
+}
diff --git a/src/server/file.h b/src/server/file.h
new file mode 100644
index 0000000..e61849a
--- /dev/null
+++ b/src/server/file.h
@@ -0,0 +1,7 @@
+#include <unistd.h>
+
+int file_open(char *filename);
+
+int file_getsize(int fd, off_t *size);
+
+int file_read(int fd, void *buf, size_t size, off_t pos);
diff --git a/src/server/server.c b/src/server/server.c
new file mode 100644
index 0000000..e38a0e3
--- /dev/null
+++ b/src/server/server.c
@@ -0,0 +1,173 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <signal.h>
+#include <getopt.h>
+#include <netinet/in.h>
+#include <sys/sendfile.h>
+
+#include <pthread.h>
+
+#include "../include/types.h"
+#include "../version.h"
+#include "file.h"
+
+int file;
+
+void print_help(char* argv_0)
+{
+
+ printf("Usage: %s [OPTIONS]...\n", argv_0);
+ printf("Start the DNBD3 server.\n");
+ printf("-f or --file \t\t File to export.\n");
+ printf("-h or --help \t\t Show this help text and quit.\n");
+ printf("-v or --version \t Show version and quit.\n");
+ exit(0);
+}
+
+void print_version()
+{
+ printf("Version: %s\n", VERSION_STRING);
+ exit(0);
+}
+
+void handle_sigpipe(int signum)
+{
+ printf("Program received signal SIGPIPE, Broken pipe (errno: %i)\n", errno);
+ return;
+}
+
+void *echo(void *client_socket)
+{
+ int sock = (int) client_socket;
+ struct dnbd3_request request;
+ struct dnbd3_reply reply;
+ uint16_t cmd;
+ off_t filesize;
+
+ while (recv(sock, &request, sizeof(struct dnbd3_request), MSG_WAITALL) > 0)
+ {
+ cmd = request.cmd;
+// char buf[request.size];
+ switch (cmd)
+ {
+ case CMD_GET_SIZE:
+ reply.cmd = request.cmd;
+ file_getsize(file, &filesize);
+ reply.filesize = filesize;
+ send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0);
+ break;
+
+ case CMD_GET_BLOCK:
+// printf("CMD: %i, Byte: %llu, Size: %llu\n",request.cmd, request.offset, request.size);
+// file_read(file, buf, request.size, request.offset);
+// send(sock, (char *) buf, request.size, 0);
+ sendfile(sock, file, (off_t *) &request.offset, request.size);
+ break;
+
+ default:
+ ;
+ }
+
+ }
+ close(sock);
+ printf("Client exit.\n");
+ pthread_exit((void *)0);
+}
+
+int main(int argc, char* argv[])
+{
+ int opt = 0;
+ int longIndex = 0;
+ static const char *optString = "f:hv?";
+ static const struct option longOpts[] =
+ {
+ { "file", required_argument, NULL, 'f' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'v' } };
+
+ opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
+ if (opt == -1)
+ print_help(argv[0]);
+
+ while (opt != -1)
+ {
+ switch (opt)
+ {
+ case 'f':
+ file = file_open(optarg);
+ break;
+ case 'h':
+ print_help(argv[0]);
+ break;
+ case 'v':
+ print_version();
+ break;
+ case '?':
+ exit(1);
+ }
+ opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
+ }
+
+ signal(SIGPIPE, handle_sigpipe);
+
+ struct sockaddr_in server;
+ struct sockaddr_in client[50];
+ int sock, fd;
+ unsigned int len;
+ pthread_t thread[50];
+ int i=1;
+
+ // Create socket
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sock < 0)
+ {
+ printf("ERROR: Socket failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ memset(&server, 0, sizeof(server));
+ server.sin_family = AF_INET; // IPv4
+ server.sin_addr.s_addr = htonl(INADDR_ANY); // Take all IPs
+ server.sin_port = htons(PORT); // set port number
+
+ // Bind to socket
+ if (bind(sock, (struct sockaddr*) &server, sizeof(server)) < 0)
+ {
+ printf("ERROR: Bind failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ // Listen on socket
+ if (listen(sock, 50) == -1)
+ {
+ printf("ERROR: Listen failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("INFO: Server is ready...\n");
+
+ // TODO: dyn threads
+ while (1)
+ {
+ len = sizeof(client);
+ fd = accept(sock, (struct sockaddr*) &client[i], &len);
+ if (fd < 0)
+ {
+ printf("ERROR: Accept failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("INFO: Client: %s connected\n", inet_ntoa(client[i].sin_addr));
+ pthread_create(&(thread[i]), NULL, echo, (void *) fd);
+ pthread_detach(thread[i++]);
+ }
+ return EXIT_SUCCESS;
+}
diff --git a/src/version.h b/src/version.h
new file mode 100644
index 0000000..86e034a
--- /dev/null
+++ b/src/version.h
@@ -0,0 +1,7 @@
+#ifndef VERSION_H_
+#define VERSION_H_
+
+#define VERSION_STRING "0.1.0"
+#define VERSION_NUMBER 010
+
+#endif /* VERSION_H_ */