From 9970fb00c79834703bc990d052439290338467be Mon Sep 17 00:00:00 2001
From: Johann Latocha
Date: Tue, 29 Nov 2011 15:06:45 +0100
Subject: initial commit
---
.cproject | 351 ++++++++++++++++++++++++++++++++++++++++++++++++++
.project | 110 ++++++++++++++++
CMakeLists.txt | 68 ++++++++++
Kbuild.in | 2 +
src/client/client.bak | 75 +++++++++++
src/client/client.c | 95 ++++++++++++++
src/config.h | 9 ++
src/include/types.h | 30 +++++
src/kernel/main.c | 216 +++++++++++++++++++++++++++++++
src/kernel/main.c.bak | 322 +++++++++++++++++++++++++++++++++++++++++++++
src/server/file.c | 58 +++++++++
src/server/file.h | 7 +
src/server/server.c | 173 +++++++++++++++++++++++++
src/version.h | 7 +
14 files changed, 1523 insertions(+)
create mode 100644 .cproject
create mode 100644 .project
create mode 100644 CMakeLists.txt
create mode 100644 Kbuild.in
create mode 100644 src/client/client.bak
create mode 100644 src/client/client.c
create mode 100644 src/config.h
create mode 100644 src/include/types.h
create mode 100644 src/kernel/main.c
create mode 100644 src/kernel/main.c.bak
create mode 100644 src/server/file.c
create mode 100644 src/server/file.h
create mode 100644 src/server/server.c
create mode 100644 src/version.h
diff --git a/.cproject b/.cproject
new file mode 100644
index 0000000..f3c17a4
--- /dev/null
+++ b/.cproject
@@ -0,0 +1,351 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ /usr/bin/make
+
+ dnbd3-server
+ true
+ false
+
+
+ /usr/bin/make
+
+ dnbd3-server/fast
+ true
+ false
+
+
+ /usr/bin/make
+
+ rebuild_cache
+ true
+ false
+
+
+ /usr/bin/make
+
+ all
+ true
+ false
+
+
+ /usr/bin/make
+
+ clean
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/file.c.o
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/file.c.i
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/file.c.s
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/main.c.o
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/main.c.i
+ true
+ false
+
+
+ /usr/bin/make
+
+ src/server/main.c.s
+ true
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.project b/.project
new file mode 100644
index 0000000..453481e
--- /dev/null
+++ b/.project
@@ -0,0 +1,110 @@
+
+
+ dnbd3
+
+
+
+
+
+ org.eclipse.cdt.make.core.makeBuilder
+ clean,full,incremental,
+
+
+ org.eclipse.cdt.core.errorOutputParser
+ org.eclipse.cdt.core.MakeErrorParser;org.eclipse.cdt.core.GCCErrorParser;org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GLDErrorParser;
+
+
+ org.eclipse.cdt.make.core.append_environment
+ true
+
+
+ org.eclipse.cdt.make.core.autoBuildTarget
+ all
+
+
+ org.eclipse.cdt.make.core.build.arguments
+ -C /home/jjl/Data/Workspace/cpp/dnbd3/build
+
+
+ org.eclipse.cdt.make.core.build.command
+ /usr/bin/make
+
+
+ org.eclipse.cdt.make.core.build.location
+ /home/jjl/Data/Workspace/cpp/dnbd3/build
+
+
+ org.eclipse.cdt.make.core.build.target.auto
+ all
+
+
+ org.eclipse.cdt.make.core.build.target.clean
+ clean
+
+
+ org.eclipse.cdt.make.core.build.target.inc
+ all
+
+
+ org.eclipse.cdt.make.core.buildArguments
+
+
+
+ org.eclipse.cdt.make.core.buildLocation
+ /home/jjl/Workspace/dnbd3/build
+
+
+ org.eclipse.cdt.make.core.cleanBuildTarget
+ clean
+
+
+ org.eclipse.cdt.make.core.contents
+ org.eclipse.cdt.make.core.activeConfigSettings
+
+
+ org.eclipse.cdt.make.core.enableAutoBuild
+ false
+
+
+ org.eclipse.cdt.make.core.enableCleanBuild
+ true
+
+
+ org.eclipse.cdt.make.core.enableFullBuild
+ true
+
+
+ org.eclipse.cdt.make.core.enabledIncrementalBuild
+ true
+
+
+ org.eclipse.cdt.make.core.environment
+ CMAKE_NO_VERBOSE=1|VERBOSE=1|
+
+
+ org.eclipse.cdt.make.core.fullBuildTarget
+ all
+
+
+ org.eclipse.cdt.make.core.stopOnError
+ true
+
+
+ org.eclipse.cdt.make.core.useDefaultBuildCmd
+ false
+
+
+
+
+ org.eclipse.cdt.make.core.ScannerConfigBuilder
+
+
+
+
+
+ org.eclipse.cdt.core.ccnature
+ org.eclipse.cdt.make.core.makeNature
+ org.eclipse.cdt.make.core.ScannerConfigNature
+ org.eclipse.cdt.core.cnature
+
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..369238a
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,68 @@
+################################################################################
+# GENERAL #
+################################################################################
+
+PROJECT(dnbd3)
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)
+
+SET(CMAKE_BUILD_TYPE Debug)
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64")
+SET(CMAKE_C_FLAGS_RELEASE "-O2")
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64")
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2" )
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
+
+FIND_PACKAGE(Threads)
+
+################################################################################
+# CLIENT #
+################################################################################
+
+FILE(GLOB_RECURSE CLIENT_SRCS src/client/*.c)
+ADD_EXECUTABLE(dnbd3-client ${CLIENT_SRCS})
+
+
+
+################################################################################
+# SERVER #
+################################################################################
+
+FILE(GLOB_RECURSE SERVER_SRCS src/server/*.c)
+ADD_EXECUTABLE(dnbd3-server ${SERVER_SRCS})
+TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT})
+
+
+################################################################################
+# MODULE #
+################################################################################
+
+SET(MODULE_NAME dnbd3)
+SET(MODULE_FILE ${MODULE_NAME}.ko)
+FILE(GLOB_RECURSE MODULE_SOURCE_FILES src/kernel/*.c)
+
+SET(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build")
+
+SET(KBUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -C ${KERNEL_DIR}
+ M=${CMAKE_BINARY_DIR} modules
+)
+
+CONFIGURE_FILE(Kbuild.in ${CMAKE_BINARY_DIR}/Kbuild)
+
+FOREACH(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES})
+ CONFIGURE_FILE(${MODULE_SOURCE_FILE} ${CMAKE_BINARY_DIR} COPYONLY)
+ENDFOREACH( MODULE_SOURCE_FILE )
+
+CONFIGURE_FILE(src/config.h ${CMAKE_BINARY_DIR} COPYONLY)
+CONFIGURE_FILE(src/include/types.h ${CMAKE_BINARY_DIR}/include/types.h COPYONLY)
+
+ADD_CUSTOM_COMMAND(
+ OUTPUT ${CMAKE_BINARY_DIR}/${MODULE_FILE}
+ COMMAND ${KBUILD_COMMAND}
+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+ DEPENDS ${MODULE_SOURCE_FILES} Kbuild.in
+ VERBATIM
+)
+
+ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE})
+
diff --git a/Kbuild.in b/Kbuild.in
new file mode 100644
index 0000000..c16df74
--- /dev/null
+++ b/Kbuild.in
@@ -0,0 +1,2 @@
+obj-m := ${MODULE_NAME}.o
+${MODULE_NAME}-objs += main.o
\ No newline at end of file
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
+#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
new file mode 100644
index 0000000..87f20d5
--- /dev/null
+++ b/src/client/client.c
@@ -0,0 +1,95 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "../include/types.h"
+#include "../version.h"
+
+void print_help(char* argv_0)
+{
+ printf("Usage: %s -H -p -d \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
+#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
new file mode 100644
index 0000000..600ed3c
--- /dev/null
+++ b/src/kernel/main.c.bak
@@ -0,0 +1,322 @@
+#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/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
+#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
new file mode 100644
index 0000000..e61849a
--- /dev/null
+++ b/src/server/file.h
@@ -0,0 +1,7 @@
+#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
new file mode 100644
index 0000000..e38a0e3
--- /dev/null
+++ b/src/server/server.c
@@ -0,0 +1,173 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#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_ */
--
cgit v1.2.3-55-g7522