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/client/client.bak | 75 ------------ src/client/client.c | 22 +++- src/config.h | 2 +- src/include/types.h | 30 ----- src/kernel/blk.c | 76 ++++++++++++ src/kernel/blk.h | 33 ++++++ src/kernel/core.c | 94 +++++++++++++++ src/kernel/dnbd3.h | 47 ++++++++ src/kernel/main.c | 216 --------------------------------- src/kernel/main.c.bak | 322 -------------------------------------------------- src/kernel/net.c | 206 ++++++++++++++++++++++++++++++++ src/kernel/net.h | 32 +++++ src/kernel/utils.c | 33 ++++++ src/kernel/utils.h | 26 ++++ src/server/file.c | 58 --------- src/server/file.h | 7 -- src/server/server.c | 43 +++---- src/types.h | 33 ++++++ 18 files changed, 625 insertions(+), 730 deletions(-) delete mode 100644 src/client/client.bak delete mode 100644 src/include/types.h create mode 100644 src/kernel/blk.c create mode 100644 src/kernel/blk.h create mode 100644 src/kernel/core.c create mode 100644 src/kernel/dnbd3.h delete mode 100644 src/kernel/main.c delete mode 100644 src/kernel/main.c.bak create mode 100644 src/kernel/net.c create mode 100644 src/kernel/net.h create mode 100644 src/kernel/utils.c create mode 100644 src/kernel/utils.h delete mode 100644 src/server/file.c delete mode 100644 src/server/file.h create mode 100644 src/types.h (limited to 'src') diff --git a/src/client/client.bak b/src/client/client.bak deleted file mode 100644 index bacfa52..0000000 --- a/src/client/client.bak +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#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 index 87f20d5..68b7f5c 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -1,3 +1,23 @@ +/* + * 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 #include #include @@ -6,7 +26,7 @@ #include #include -#include "../include/types.h" +#include "../types.h" #include "../version.h" void print_help(char* argv_0) diff --git a/src/config.h b/src/config.h index ec41da2..1d31f60 100644 --- a/src/config.h +++ b/src/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H_ #define CONFIG_H_ -#define PORT 5003 // TODO: make obsolete +#define PORT 5003 #define KERNEL_SECTOR_SIZE 512 #define DNBD3_BLOCK_SIZE 4096 diff --git a/src/include/types.h b/src/include/types.h deleted file mode 100644 index ea34616..0000000 --- a/src/include/types.h +++ /dev/null @@ -1,30 +0,0 @@ -#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/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 + * + * 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 + * + * 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 + * + * 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 + * + * 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 +#include +#include +#include + +#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 -#include -#include -#include - -// 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 -#include -#include - -// Own -#include "config.h" -#include "include/types.h" - -// Network -#include -//#include -//#include - -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 + * + * 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 + * + * 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 + * + * 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 + +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 + * + * 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_ */ diff --git a/src/server/file.c b/src/server/file.c deleted file mode 100644 index c53ca15..0000000 --- a/src/server/file.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#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 deleted file mode 100644 index e61849a..0000000 --- a/src/server/file.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -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 index e38a0e3..dfa53d7 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -12,12 +12,12 @@ #include #include #include +#include #include -#include "../include/types.h" +#include "../types.h" #include "../version.h" -#include "file.h" int file; @@ -40,40 +40,44 @@ void print_version() void handle_sigpipe(int signum) { - printf("Program received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); + printf("ERROR: Received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); return; } -void *echo(void *client_socket) +void *handle_query(void *client_socket) { int sock = (int) client_socket; struct dnbd3_request request; struct dnbd3_reply reply; uint16_t cmd; off_t filesize; + struct stat st; + fstat(file, &st); + filesize = st.st_size; 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); + reply.cmd = request.cmd; + memcpy(reply.handle, request.handle, sizeof(request.handle)); + send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); + + if (sendfile(sock, file, (off_t *) &request.offset, request.size) <0) + printf("ERROR: sendfile returned -1\n"); + break; default: - ; + printf("ERROR: Unknown command\n"); } } @@ -102,7 +106,7 @@ int main(int argc, char* argv[]) switch (opt) { case 'f': - file = file_open(optarg); + file = open(optarg, O_RDONLY); break; case 'h': print_help(argv[0]); @@ -119,11 +123,9 @@ int main(int argc, char* argv[]) signal(SIGPIPE, handle_sigpipe); struct sockaddr_in server; - struct sockaddr_in client[50]; + struct sockaddr_in client; int sock, fd; unsigned int len; - pthread_t thread[50]; - int i=1; // Create socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -154,20 +156,21 @@ int main(int argc, char* argv[]) printf("INFO: Server is ready...\n"); - // TODO: dyn threads while (1) { len = sizeof(client); - fd = accept(sock, (struct sockaddr*) &client[i], &len); + fd = accept(sock, (struct sockaddr*) &client, &len); if (fd < 0) { printf("ERROR: Accept failure\n"); exit(EXIT_FAILURE); } + printf("INFO: Client: %s connected\n", inet_ntoa(client.sin_addr)); - printf("INFO: Client: %s connected\n", inet_ntoa(client[i].sin_addr)); - pthread_create(&(thread[i]), NULL, echo, (void *) fd); - pthread_detach(thread[i++]); + // FIXME: catch SIGKILL/SIGTERM and close all socket before exit + pthread_t thread; + pthread_create(&(thread), NULL, handle_query, (void *) fd); + pthread_detach(thread); } return EXIT_SUCCESS; } diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..3005f62 --- /dev/null +++ b/src/types.h @@ -0,0 +1,33 @@ +#ifndef TYPES_H_ +#define TYPES_H_ + +#include "config.h" + +// ioctl +#define DNBD3_MAGIC 'd' +#define IOCTL_SET_HOST _IO(0xab, 1) +#define IOCTL_SET_PORT _IO(0xab, 2) +#define IOCTL_CONNECT _IO(0xab, 3) + +// network +#define CMD_GET_BLOCK 1 +#define CMD_GET_SIZE 2 + +#pragma pack(1) +typedef struct dnbd3_request { + uint16_t cmd; + uint64_t offset; + uint64_t size; + char handle[8]; +} dnbd3_request_t; +#pragma pack(0) + +#pragma pack(1) +typedef struct dnbd3_reply { + uint16_t cmd; + uint64_t filesize; + char handle[8]; +} dnbd3_reply_t; +#pragma pack(0) + +#endif /* TYPES_H_ */ -- cgit v1.2.3-55-g7522