summaryrefslogtreecommitdiffstats
path: root/src/kernel/main.c.bak
blob: 600ed3c3e5241c4afef73b43c7024d3939344f76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/blkdev.h>

// Own
#include "config.h"
#include "include/types.h"

// Network
#include <net/sock.h>
//#include <linux/net.h>
//#include <net/ip.h>

static int major;
static struct gendisk *disk;
static struct request_queue *dnbd3_queue;

DEFINE_SPINLOCK( dnbd3_lock);

struct socket *sock;
struct dnbd3_request r;
struct dnbd3_reply rp;

uint64_t filesize;

char* host;
char* port;

// TODO: schauen obs nicht eine fertige gibt
static unsigned int inet_addr(char *str)
{
	int a, b, c, d;
	char arr[4];
	sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
	arr[0] = a;
	arr[1] = b;
	arr[2] = c;
	arr[3] = d;
	return *(unsigned int*) arr;
}

static void connect()
{
	if (!host || !port)
	{
		printk("ERROR: Host or port not set.");
		return;
	}

	// Init kernel-socket
//	static unsigned char address[4] =
//	{ 132, 230, 4, 29 };

	struct sockaddr_in sin;

	if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock) < 0)
	{
		printk("DNBD3: Couldn't create socket.\n");
	}
	sin.sin_family = AF_INET;
	//memcpy(&sin.sin_addr.s_addr, address, 4);
	sin.sin_addr.s_addr = inet_addr(host);
	sin.sin_port = htons(simple_strtol(port, NULL, 10));
	if (kernel_connect(sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0)
	{
		printk("DNBD3: Couldn't connect.\n");
		return;
	}

	// Ask for filesize
	struct msghdr msg;
	struct kvec iov;
	int size, result;
	void *buf = &r;
	r.cmd = CMD_GET_SIZE;
	size = sizeof(r);
	do
	{
		sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
		iov.iov_base = (char *) buf;
		iov.iov_len = size;
		msg.msg_name = NULL;
		msg.msg_namelen = 0;
		msg.msg_control = NULL;
		msg.msg_controllen = 0;
		msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE

		result = kernel_sendmsg(sock, &msg, &iov, 1, size);

		if (result <= 0)
		{
			if (result == 0)
				result = -EPIPE; /* short read */
			break;
		}
		size -= result;
		buf += result;
	} while (size > 0);
	buf = &rp;
	size = sizeof(rp);
	do
	{
		sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
		iov.iov_base = (char *) buf;
		iov.iov_len = size;
		msg.msg_name = NULL;
		msg.msg_namelen = 0;
		msg.msg_control = NULL;
		msg.msg_controllen = 0;
		msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE

		result = kernel_recvmsg(sock, &msg, &iov, 1, size, msg.msg_flags);

		if (result <= 0)
		{
			if (result == 0)
				result = -EPIPE; /* short read */
			break;
		}
		size -= result;
		buf += result;
	} while (size > 0);

	filesize = rp.num;
	printk("Filesize: %llu\n", filesize);

	//filesize = 721127424;
	set_capacity(disk, filesize >> 9); /* 512 Byte blocks */

}

static void dnbd3_request(struct request_queue *q)
{
	if (!sock)
		return;

	struct request *req;
	unsigned long start, to_copy;
	int size, result;
	void *buf;
	struct msghdr msg;
	struct kvec iov;

	while ((req = blk_fetch_request(q)) != NULL)
	{
		if (req->cmd_type != REQ_TYPE_FS)
		{
			if (!__blk_end_request_cur(req, 0))
				req = blk_fetch_request(q);
			continue;
		}
		start = blk_rq_pos(req) << 9; // *512

		//to_copy = blk_rq_cur_bytes(req);	// Returns bytes left to complete in the current segment
		to_copy = blk_rq_bytes(req); // blk_rq_bytes() Returns bytes left to complete in the entire request

		spin_unlock_irq(q->queue_lock);
		if ((start + to_copy) <= filesize)
		{
			if (rq_data_dir(req) == READ)
			{
				// Send msg
				//printk("Send...\n");
				buf = &r;
				r.cmd = CMD_GET_BLOCK;
				r.num = start;
				r.to_copy = to_copy;
				size = sizeof(r);
				do
				{
					sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
					iov.iov_base = (char *) buf;
					iov.iov_len = size;
					msg.msg_name = NULL;
					msg.msg_namelen = 0;
					msg.msg_control = NULL;
					msg.msg_controllen = 0;
					msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE

					result = kernel_sendmsg(sock, &msg, &iov, 1, size);

					if (result <= 0)
					{
						if (result == 0)
							result = -EPIPE; /* short read */
						break;
					}
					size -= result;
					buf += result;
				} while (size > 0);

				// Test - receive msg
				struct req_iterator iter;
				struct bio_vec *bvec;
				rq_for_each_segment(bvec, req, iter)
					{
						void *kaddr = kmap(bvec->bv_page);
						buf = kaddr + bvec->bv_offset;
						size = bvec->bv_len;
						do
						{
							sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed.
							iov.iov_base = buf;
							iov.iov_len = size;
							msg.msg_name = NULL;
							msg.msg_namelen = 0;
							msg.msg_control = NULL;
							msg.msg_controllen = 0;
							msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE

							result = kernel_recvmsg(sock, &msg, &iov, 1, size,
									msg.msg_flags);

							if (result <= 0)
							{
								if (result == 0)
									result = -EPIPE; /* short read */
								break;
							}
							size -= result;
							buf += result;

							if (size > 0)
								printk("SIZE > 0\n");

						} while (size > 0);

						kunmap(bvec->bv_page);
					}

			}
			else
			{
				printk("ERROR: Write not supported.");
			}
		}
		else
		{
			printk("ERROR: %ld not in range...\n", start + to_copy);
		}
		spin_lock_irq(q->queue_lock);
		__blk_end_request_all(req, 0);
	}
}

static int dnbd3_ioctl(struct block_device *bdev, fmode_t mode,
		unsigned int cmd, unsigned long arg)
{
	switch (cmd)
	{

	case IOCTL_SET_HOST:
		host = (char *) arg;
		break;

	case IOCTL_SET_PORT:
		port = (char *) arg;
		break;

	case IOCTL_CONNECT:
		connect();
		break;

	default:
		return -1;

	}
	return 0;
}

static struct block_device_operations dnbd3_ops =
{ .owner = THIS_MODULE, .ioctl = dnbd3_ioctl, };

static int __init dnbd3_init(void)
{

	// Init blkdev
	if ((major = register_blkdev(0, "dnbd")) == 0)
	{
		printk("dnbd3: can't get majornumber\n");
		return -EIO;
	}
	printk("major: %d\n", major);
	if (!(disk = alloc_disk(1)))
	{
		printk("alloc_disk failed ...\n");
		goto out;
	}
	disk->major = major;
	disk->first_minor = 0;
	sprintf(disk->disk_name, "dnbd0");
	//set_capacity(disk, filesize >> 9); /* 512 Byte blocks */
	set_capacity(disk, 0);
	disk->fops = &dnbd3_ops;

	if ((dnbd3_queue = blk_init_queue(&dnbd3_request, &dnbd3_lock)) == NULL)
		goto out;

	blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set logical block size for the queue
//	blk_queue_max_hw_sectors(dnbd3_queue, DNBD3_BLOCK_SIZE / KERNEL_SECTOR_SIZE); // set max sectors for a request for this queue, min 8
//	blk_queue_max_segments(dnbd3_queue, 1); // set max hw segments for a request for this queue
//	blk_queue_max_segment_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set max segment size for blk_rq_map_sg, min 4096
	disk->queue = dnbd3_queue;

	add_disk(disk);
	return 0;
	out: return -EIO;
}

static void __exit dnbd3_exit(void)
{
	if (sock)
		sock_release(sock);
	unregister_blkdev(major, "dnbd");
	del_gendisk(disk);
	put_disk(disk);
	blk_cleanup_queue(dnbd3_queue);
}

module_init( dnbd3_init);
module_exit( dnbd3_exit);
MODULE_LICENSE("GPL");