summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohann Latocha2012-01-19 17:20:02 +0100
committerJohann Latocha2012-01-19 17:20:02 +0100
commitb7fd7219ce4fc73939c912d4a02f5b8e4301ede7 (patch)
tree4a36476deebc1b7c66c459472a6b0183eaff3b47
parentinitial commit (diff)
downloaddnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.tar.gz
dnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.tar.xz
dnbd3-b7fd7219ce4fc73939c912d4a02f5b8e4301ede7.zip
First working version :)
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt11
-rw-r--r--COPYING340
-rw-r--r--Kbuild.in2
-rw-r--r--src/client/client.bak75
-rw-r--r--src/client/client.c22
-rw-r--r--src/config.h2
-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
-rw-r--r--src/server/file.c58
-rw-r--r--src/server/file.h7
-rw-r--r--src/server/server.c43
-rw-r--r--src/types.h (renamed from src/include/types.h)15
21 files changed, 949 insertions, 712 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..567609b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 369238a..c1874d0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,9 +7,9 @@ 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_C_FLAGS_RELEASE "-O2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64")
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64")
-SET(CMAKE_CXX_FLAGS_RELEASE "-O2" )
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64" )
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
@@ -40,6 +40,7 @@ TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT})
SET(MODULE_NAME dnbd3)
SET(MODULE_FILE ${MODULE_NAME}.ko)
FILE(GLOB_RECURSE MODULE_SOURCE_FILES src/kernel/*.c)
+FILE(GLOB_RECURSE MODULE_HEADER_FILES src/kernel/*.h src/*.h)
SET(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build")
@@ -53,8 +54,9 @@ 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)
+FOREACH(MODULE_HEADER_FILE ${MODULE_HEADER_FILES})
+ CONFIGURE_FILE(${MODULE_HEADER_FILE} ${CMAKE_BINARY_DIR} COPYONLY)
+ENDFOREACH( MODULE_HEADER_FILE )
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_BINARY_DIR}/${MODULE_FILE}
@@ -65,4 +67,3 @@ ADD_CUSTOM_COMMAND(
)
ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE})
-
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d60c31a
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/Kbuild.in b/Kbuild.in
index c16df74..68b4be1 100644
--- a/Kbuild.in
+++ b/Kbuild.in
@@ -1,2 +1,2 @@
obj-m := ${MODULE_NAME}.o
-${MODULE_NAME}-objs += main.o \ No newline at end of file
+${MODULE_NAME}-objs += core.o blk.o net.o utils.o \ No newline at end of file
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 <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
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 <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 <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
@@ -6,7 +26,7 @@
#include <sys/ioctl.h>
#include <netinet/in.h>
-#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/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_ */
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 <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
deleted file mode 100644
index e61849a..0000000
--- a/src/server/file.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#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
index e38a0e3..dfa53d7 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -12,12 +12,12 @@
#include <getopt.h>
#include <netinet/in.h>
#include <sys/sendfile.h>
+#include <sys/stat.h>
#include <pthread.h>
-#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/include/types.h b/src/types.h
index ea34616..3005f62 100644
--- a/src/include/types.h
+++ b/src/types.h
@@ -1,22 +1,24 @@
#ifndef TYPES_H_
#define TYPES_H_
-#include "../config.h"
-
-#define DNBD_MAGIC 'd'
-
-#define CMD_GET_BLOCK 1
-#define CMD_GET_SIZE 2
+#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)
@@ -24,6 +26,7 @@ typedef struct dnbd3_request {
typedef struct dnbd3_reply {
uint16_t cmd;
uint64_t filesize;
+ char handle[8];
} dnbd3_reply_t;
#pragma pack(0)