blob: 406e555bc69fcdd35c0a49f094ba17d2d43c6a9b [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ths75818252008-07-03 13:41:03 +000017 */
bellard7a5ca862008-05-27 21:13:40 +000018
19#include "nbd.h"
Markus Armbrusterab359cd2011-09-06 18:58:58 +020020#include "block.h"
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020021#include "block_int.h"
bellard7a5ca862008-05-27 21:13:40 +000022
Paolo Bonzini262db382011-09-19 15:19:27 +020023#include "qemu-coroutine.h"
24
bellard7a5ca862008-05-27 21:13:40 +000025#include <errno.h>
26#include <string.h>
aliguori03ff3ca2008-09-15 15:51:35 +000027#ifndef _WIN32
bellard7a5ca862008-05-27 21:13:40 +000028#include <sys/ioctl.h>
aliguori03ff3ca2008-09-15 15:51:35 +000029#endif
Andreas Färber5dc2eec2010-09-20 00:50:46 +020030#if defined(__sun__) || defined(__HAIKU__)
aliguori7e00eb92008-08-02 01:57:02 +000031#include <sys/ioccom.h>
32#endif
bellard7a5ca862008-05-27 21:13:40 +000033#include <ctype.h>
34#include <inttypes.h>
bellard7a5ca862008-05-27 21:13:40 +000035
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +020036#ifdef __linux__
37#include <linux/fs.h>
38#endif
39
aliguori03ff3ca2008-09-15 15:51:35 +000040#include "qemu_socket.h"
Paolo Bonzinid9a73802011-09-19 14:18:33 +020041#include "qemu-queue.h"
ths75818252008-07-03 13:41:03 +000042
aliguori03ff3ca2008-09-15 15:51:35 +000043//#define DEBUG_NBD
44
45#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000046#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000047 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000048} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000049#else
50#define TRACE(msg, ...) \
51 do { } while (0)
52#endif
bellard7a5ca862008-05-27 21:13:40 +000053
54#define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
57} while(0)
58
bellard7a5ca862008-05-27 21:13:40 +000059/* This is all part of the "official" NBD API */
60
Nick Thomasb2e3d872011-02-22 15:44:51 +000061#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000062#define NBD_REQUEST_MAGIC 0x25609513
63#define NBD_REPLY_MAGIC 0x67446698
64
65#define NBD_SET_SOCK _IO(0xab, 0)
66#define NBD_SET_BLKSIZE _IO(0xab, 1)
67#define NBD_SET_SIZE _IO(0xab, 2)
68#define NBD_DO_IT _IO(0xab, 3)
69#define NBD_CLEAR_SOCK _IO(0xab, 4)
70#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000071#define NBD_PRINT_DEBUG _IO(0xab, 6)
72#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000073#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020074#define NBD_SET_TIMEOUT _IO(0xab, 9)
75#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000076
Nick Thomasb2e3d872011-02-22 15:44:51 +000077#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020078
bellard7a5ca862008-05-27 21:13:40 +000079/* That's all folks */
80
ths75818252008-07-03 13:41:03 +000081#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
82#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
bellard7a5ca862008-05-27 21:13:40 +000083
ths75818252008-07-03 13:41:03 +000084size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +000085{
86 size_t offset = 0;
87
Paolo Bonziniae255e52011-09-08 14:28:59 +020088 if (qemu_in_coroutine()) {
89 if (do_read) {
90 return qemu_co_recv(fd, buffer, size);
91 } else {
92 return qemu_co_send(fd, buffer, size);
93 }
94 }
95
bellard7a5ca862008-05-27 21:13:40 +000096 while (offset < size) {
97 ssize_t len;
98
99 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000100 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000101 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000102 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000103 }
104
aliguori03ff3ca2008-09-15 15:51:35 +0000105 if (len == -1)
106 errno = socket_error();
107
bellard7a5ca862008-05-27 21:13:40 +0000108 /* recoverable error */
ths75818252008-07-03 13:41:03 +0000109 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
bellard7a5ca862008-05-27 21:13:40 +0000110 continue;
111 }
112
113 /* eof */
114 if (len == 0) {
115 break;
116 }
117
118 /* unrecoverable error */
119 if (len == -1) {
120 return 0;
121 }
122
123 offset += len;
124 }
125
126 return offset;
127}
128
Nick Thomasc12504c2011-02-22 15:44:53 +0000129static void combine_addr(char *buf, size_t len, const char* address,
130 uint16_t port)
131{
132 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
133 if (strstr(address, ":")) {
134 snprintf(buf, len, "[%s]:%u", address, port);
135 } else {
136 snprintf(buf, len, "%s:%u", address, port);
137 }
138}
139
ths75818252008-07-03 13:41:03 +0000140int tcp_socket_outgoing(const char *address, uint16_t port)
bellard7a5ca862008-05-27 21:13:40 +0000141{
Nick Thomasc12504c2011-02-22 15:44:53 +0000142 char address_and_port[128];
143 combine_addr(address_and_port, 128, address, port);
144 return tcp_socket_outgoing_spec(address_and_port);
145}
bellard7a5ca862008-05-27 21:13:40 +0000146
Nick Thomasc12504c2011-02-22 15:44:53 +0000147int tcp_socket_outgoing_spec(const char *address_and_port)
148{
149 return inet_connect(address_and_port, SOCK_STREAM);
bellard7a5ca862008-05-27 21:13:40 +0000150}
151
152int tcp_socket_incoming(const char *address, uint16_t port)
153{
Nick Thomasc12504c2011-02-22 15:44:53 +0000154 char address_and_port[128];
155 combine_addr(address_and_port, 128, address, port);
156 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
Nick Thomasc12504c2011-02-22 15:44:53 +0000159int tcp_socket_incoming_spec(const char *address_and_port)
160{
161 char *ostr = NULL;
162 int olen = 0;
163 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
164}
165
thscd831bd2008-07-03 10:23:51 +0000166int unix_socket_incoming(const char *path)
167{
Nick Thomasc12504c2011-02-22 15:44:53 +0000168 char *ostr = NULL;
169 int olen = 0;
thscd831bd2008-07-03 10:23:51 +0000170
Nick Thomasc12504c2011-02-22 15:44:53 +0000171 return unix_listen(path, ostr, olen);
thscd831bd2008-07-03 10:23:51 +0000172}
173
174int unix_socket_outgoing(const char *path)
175{
Nick Thomasc12504c2011-02-22 15:44:53 +0000176 return unix_connect(path);
thscd831bd2008-07-03 10:23:51 +0000177}
thscd831bd2008-07-03 10:23:51 +0000178
bellard7a5ca862008-05-27 21:13:40 +0000179/* Basic flow
180
181 Server Client
182
183 Negotiate
184 Request
185 Response
186 Request
187 Response
188 ...
189 ...
190 Request (type == 2)
191*/
192
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200193static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
bellard7a5ca862008-05-27 21:13:40 +0000194{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000195 char buf[8 + 8 + 8 + 128];
bellard7a5ca862008-05-27 21:13:40 +0000196
Nick Thomasb2e3d872011-02-22 15:44:51 +0000197 /* Negotiate
198 [ 0 .. 7] passwd ("NBDMAGIC")
199 [ 8 .. 15] magic (0x00420281861253)
200 [16 .. 23] size
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200201 [24 .. 27] flags
202 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000203 */
bellard7a5ca862008-05-27 21:13:40 +0000204
Nick Thomasb2e3d872011-02-22 15:44:51 +0000205 TRACE("Beginning negotiation.");
206 memcpy(buf, "NBDMAGIC", 8);
207 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
208 cpu_to_be64w((uint64_t*)(buf + 16), size);
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200209 cpu_to_be32w((uint32_t*)(buf + 24),
Paolo Bonzini7a706632011-10-21 13:17:14 +0200210 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
211 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200212 memset(buf + 28, 0, 124);
bellard7a5ca862008-05-27 21:13:40 +0000213
Nick Thomasb2e3d872011-02-22 15:44:51 +0000214 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
215 LOG("write failed");
216 errno = EINVAL;
217 return -1;
218 }
bellard7a5ca862008-05-27 21:13:40 +0000219
Dong Xu Wang07f35072011-11-22 18:06:26 +0800220 TRACE("Negotiation succeeded.");
bellard7a5ca862008-05-27 21:13:40 +0000221
Nick Thomasb2e3d872011-02-22 15:44:51 +0000222 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000223}
224
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200225int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
226 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000227{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000228 char buf[256];
229 uint64_t magic, s;
230 uint16_t tmp;
bellard7a5ca862008-05-27 21:13:40 +0000231
Dong Xu Wang07f35072011-11-22 18:06:26 +0800232 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000233
Nick Thomasb2e3d872011-02-22 15:44:51 +0000234 if (read_sync(csock, buf, 8) != 8) {
235 LOG("read failed");
236 errno = EINVAL;
237 return -1;
238 }
bellard7a5ca862008-05-27 21:13:40 +0000239
Nick Thomasb2e3d872011-02-22 15:44:51 +0000240 buf[8] = '\0';
241 if (strlen(buf) == 0) {
242 LOG("server connection closed");
243 errno = EINVAL;
244 return -1;
245 }
bellard7a5ca862008-05-27 21:13:40 +0000246
Nick Thomasb2e3d872011-02-22 15:44:51 +0000247 TRACE("Magic is %c%c%c%c%c%c%c%c",
248 qemu_isprint(buf[0]) ? buf[0] : '.',
249 qemu_isprint(buf[1]) ? buf[1] : '.',
250 qemu_isprint(buf[2]) ? buf[2] : '.',
251 qemu_isprint(buf[3]) ? buf[3] : '.',
252 qemu_isprint(buf[4]) ? buf[4] : '.',
253 qemu_isprint(buf[5]) ? buf[5] : '.',
254 qemu_isprint(buf[6]) ? buf[6] : '.',
255 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000256
Nick Thomasb2e3d872011-02-22 15:44:51 +0000257 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
258 LOG("Invalid magic received");
259 errno = EINVAL;
260 return -1;
261 }
bellard7a5ca862008-05-27 21:13:40 +0000262
Nick Thomasb2e3d872011-02-22 15:44:51 +0000263 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
264 LOG("read failed");
265 errno = EINVAL;
266 return -1;
267 }
268 magic = be64_to_cpu(magic);
269 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000270
Nick Thomasb2e3d872011-02-22 15:44:51 +0000271 if (name) {
272 uint32_t reserved = 0;
273 uint32_t opt;
274 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200275
Nick Thomasb2e3d872011-02-22 15:44:51 +0000276 TRACE("Checking magic (opts_magic)");
277 if (magic != 0x49484156454F5054LL) {
278 LOG("Bad magic received");
279 errno = EINVAL;
280 return -1;
281 }
282 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
283 LOG("flags read failed");
284 errno = EINVAL;
285 return -1;
286 }
287 *flags = be16_to_cpu(tmp) << 16;
288 /* reserved for future use */
289 if (write_sync(csock, &reserved, sizeof(reserved)) !=
290 sizeof(reserved)) {
291 LOG("write failed (reserved)");
292 errno = EINVAL;
293 return -1;
294 }
295 /* write the export name */
296 magic = cpu_to_be64(magic);
297 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
298 LOG("write failed (magic)");
299 errno = EINVAL;
300 return -1;
301 }
302 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
303 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
304 LOG("write failed (opt)");
305 errno = EINVAL;
306 return -1;
307 }
308 namesize = cpu_to_be32(strlen(name));
309 if (write_sync(csock, &namesize, sizeof(namesize)) !=
310 sizeof(namesize)) {
311 LOG("write failed (namesize)");
312 errno = EINVAL;
313 return -1;
314 }
315 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
316 LOG("write failed (name)");
317 errno = EINVAL;
318 return -1;
319 }
320 } else {
321 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200322
Nick Thomasb2e3d872011-02-22 15:44:51 +0000323 if (magic != 0x00420281861253LL) {
324 LOG("Bad magic received");
325 errno = EINVAL;
326 return -1;
327 }
328 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200329
Nick Thomasb2e3d872011-02-22 15:44:51 +0000330 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
331 LOG("read failed");
332 errno = EINVAL;
333 return -1;
334 }
335 *size = be64_to_cpu(s);
336 *blocksize = 1024;
337 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200338
Nick Thomasb2e3d872011-02-22 15:44:51 +0000339 if (!name) {
340 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
341 LOG("read failed (flags)");
342 errno = EINVAL;
343 return -1;
344 }
345 *flags = be32_to_cpup(flags);
346 } else {
347 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
348 LOG("read failed (tmp)");
349 errno = EINVAL;
350 return -1;
351 }
352 *flags |= be32_to_cpu(tmp);
353 }
354 if (read_sync(csock, &buf, 124) != 124) {
355 LOG("read failed (buf)");
356 errno = EINVAL;
357 return -1;
358 }
thscd831bd2008-07-03 10:23:51 +0000359 return 0;
360}
bellard7a5ca862008-05-27 21:13:40 +0000361
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200362#ifdef __linux__
363int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000364{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800365 TRACE("Setting NBD socket");
366
367 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
368 int serrno = errno;
369 LOG("Failed to set NBD socket");
370 errno = serrno;
371 return -1;
372 }
373
Nick Thomasb2e3d872011-02-22 15:44:51 +0000374 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000375
Nick Thomasb2e3d872011-02-22 15:44:51 +0000376 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
377 int serrno = errno;
378 LOG("Failed setting NBD block size");
379 errno = serrno;
380 return -1;
381 }
bellard7a5ca862008-05-27 21:13:40 +0000382
Blue Swirl0bfcd592010-05-22 08:02:12 +0000383 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000384
Nick Thomasb2e3d872011-02-22 15:44:51 +0000385 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
386 int serrno = errno;
387 LOG("Failed setting size (in blocks)");
388 errno = serrno;
389 return -1;
390 }
bellard7a5ca862008-05-27 21:13:40 +0000391
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200392 if (flags & NBD_FLAG_READ_ONLY) {
393 int read_only = 1;
394 TRACE("Setting readonly attribute");
395
396 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
397 int serrno = errno;
398 LOG("Failed setting read-only attribute");
399 errno = serrno;
400 return -1;
401 }
402 }
403
Paolo Bonzini973b3d02011-09-08 17:24:56 +0200404 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
405 && errno != ENOTTY) {
406 int serrno = errno;
407 LOG("Failed setting flags");
408 errno = serrno;
409 return -1;
410 }
411
Nick Thomasb2e3d872011-02-22 15:44:51 +0000412 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000413
Nick Thomasb2e3d872011-02-22 15:44:51 +0000414 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000415}
416
417int nbd_disconnect(int fd)
418{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000419 ioctl(fd, NBD_CLEAR_QUE);
420 ioctl(fd, NBD_DISCONNECT);
421 ioctl(fd, NBD_CLEAR_SOCK);
422 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000423}
424
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200425int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000426{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000427 int ret;
428 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000429
Nick Thomasb2e3d872011-02-22 15:44:51 +0000430 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000431
Nick Thomasb2e3d872011-02-22 15:44:51 +0000432 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzini74624682011-11-04 15:51:18 +0100433 if (ret == -1 && errno == EPIPE) {
434 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
435 * the socket via NBD_DISCONNECT. We do not want to return 1 in
436 * that case.
437 */
438 ret = 0;
439 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000440 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000441
Nick Thomasb2e3d872011-02-22 15:44:51 +0000442 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000443
Nick Thomasb2e3d872011-02-22 15:44:51 +0000444 TRACE("Clearing NBD queue");
445 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000446
Nick Thomasb2e3d872011-02-22 15:44:51 +0000447 TRACE("Clearing NBD socket");
448 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000449
Nick Thomasb2e3d872011-02-22 15:44:51 +0000450 errno = serrno;
451 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000452}
aliguori03ff3ca2008-09-15 15:51:35 +0000453#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200454int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000455{
456 errno = ENOTSUP;
457 return -1;
458}
459
460int nbd_disconnect(int fd)
461{
462 errno = ENOTSUP;
463 return -1;
464}
465
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200466int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000467{
468 errno = ENOTSUP;
469 return -1;
470}
471#endif
bellard7a5ca862008-05-27 21:13:40 +0000472
ths75818252008-07-03 13:41:03 +0000473int nbd_send_request(int csock, struct nbd_request *request)
474{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000475 uint8_t buf[4 + 4 + 8 + 8 + 4];
ths75818252008-07-03 13:41:03 +0000476
Nick Thomasb2e3d872011-02-22 15:44:51 +0000477 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
478 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
479 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
480 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
481 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000482
Nick Thomasb2e3d872011-02-22 15:44:51 +0000483 TRACE("Sending request to client: "
484 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
485 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000486
Nick Thomasb2e3d872011-02-22 15:44:51 +0000487 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
488 LOG("writing to socket failed");
489 errno = EINVAL;
490 return -1;
491 }
492 return 0;
ths75818252008-07-03 13:41:03 +0000493}
494
ths75818252008-07-03 13:41:03 +0000495static int nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000496{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000497 uint8_t buf[4 + 4 + 8 + 8 + 4];
498 uint32_t magic;
bellard7a5ca862008-05-27 21:13:40 +0000499
Nick Thomasb2e3d872011-02-22 15:44:51 +0000500 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
501 LOG("read failed");
502 errno = EINVAL;
503 return -1;
504 }
bellard7a5ca862008-05-27 21:13:40 +0000505
Nick Thomasb2e3d872011-02-22 15:44:51 +0000506 /* Request
507 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
508 [ 4 .. 7] type (0 == READ, 1 == WRITE)
509 [ 8 .. 15] handle
510 [16 .. 23] from
511 [24 .. 27] len
512 */
bellard7a5ca862008-05-27 21:13:40 +0000513
Nick Thomasb2e3d872011-02-22 15:44:51 +0000514 magic = be32_to_cpup((uint32_t*)buf);
515 request->type = be32_to_cpup((uint32_t*)(buf + 4));
516 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
517 request->from = be64_to_cpup((uint64_t*)(buf + 16));
518 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000519
Nick Thomasb2e3d872011-02-22 15:44:51 +0000520 TRACE("Got request: "
521 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
522 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000523
Nick Thomasb2e3d872011-02-22 15:44:51 +0000524 if (magic != NBD_REQUEST_MAGIC) {
525 LOG("invalid magic (got 0x%x)", magic);
526 errno = EINVAL;
527 return -1;
528 }
529 return 0;
ths75818252008-07-03 13:41:03 +0000530}
bellard7a5ca862008-05-27 21:13:40 +0000531
ths75818252008-07-03 13:41:03 +0000532int nbd_receive_reply(int csock, struct nbd_reply *reply)
533{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000534 uint8_t buf[NBD_REPLY_SIZE];
535 uint32_t magic;
ths75818252008-07-03 13:41:03 +0000536
Nick Thomasb2e3d872011-02-22 15:44:51 +0000537 memset(buf, 0xAA, sizeof(buf));
ths75818252008-07-03 13:41:03 +0000538
Nick Thomasb2e3d872011-02-22 15:44:51 +0000539 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
540 LOG("read failed");
541 errno = EINVAL;
542 return -1;
543 }
bellard7a5ca862008-05-27 21:13:40 +0000544
Nick Thomasb2e3d872011-02-22 15:44:51 +0000545 /* Reply
546 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
547 [ 4 .. 7] error (0 == no error)
548 [ 7 .. 15] handle
549 */
ths75818252008-07-03 13:41:03 +0000550
Nick Thomasb2e3d872011-02-22 15:44:51 +0000551 magic = be32_to_cpup((uint32_t*)buf);
552 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
553 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000554
Nick Thomasb2e3d872011-02-22 15:44:51 +0000555 TRACE("Got reply: "
556 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
557 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000558
Nick Thomasb2e3d872011-02-22 15:44:51 +0000559 if (magic != NBD_REPLY_MAGIC) {
560 LOG("invalid magic (got 0x%x)", magic);
561 errno = EINVAL;
562 return -1;
563 }
564 return 0;
ths75818252008-07-03 13:41:03 +0000565}
566
567static int nbd_send_reply(int csock, struct nbd_reply *reply)
568{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000569 uint8_t buf[4 + 4 + 8];
ths75818252008-07-03 13:41:03 +0000570
Nick Thomasb2e3d872011-02-22 15:44:51 +0000571 /* Reply
572 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
573 [ 4 .. 7] error (0 == no error)
574 [ 7 .. 15] handle
575 */
576 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
577 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
578 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000579
Nick Thomasb2e3d872011-02-22 15:44:51 +0000580 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000581
Nick Thomasb2e3d872011-02-22 15:44:51 +0000582 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
583 LOG("writing to socket failed");
584 errno = EINVAL;
585 return -1;
586 }
587 return 0;
ths75818252008-07-03 13:41:03 +0000588}
589
Paolo Bonzini41996e32011-09-19 15:25:40 +0200590#define MAX_NBD_REQUESTS 16
591
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200592typedef struct NBDRequest NBDRequest;
593
594struct NBDRequest {
595 QSIMPLEQ_ENTRY(NBDRequest) entry;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200596 NBDClient *client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200597 uint8_t *data;
598};
599
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200600struct NBDExport {
601 BlockDriverState *bs;
602 off_t dev_offset;
603 off_t size;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200604 uint32_t nbdflags;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200605 QSIMPLEQ_HEAD(, NBDRequest) requests;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200606};
607
Paolo Bonzini1743b512011-09-19 14:33:23 +0200608struct NBDClient {
609 int refcount;
610 void (*close)(NBDClient *client);
611
612 NBDExport *exp;
613 int sock;
Paolo Bonzini262db382011-09-19 15:19:27 +0200614
615 Coroutine *recv_coroutine;
616
617 CoMutex send_lock;
618 Coroutine *send_coroutine;
Paolo Bonzini41996e32011-09-19 15:25:40 +0200619
620 int nb_requests;
Paolo Bonzini1743b512011-09-19 14:33:23 +0200621};
622
623static void nbd_client_get(NBDClient *client)
624{
625 client->refcount++;
626}
627
628static void nbd_client_put(NBDClient *client)
629{
630 if (--client->refcount == 0) {
631 g_free(client);
632 }
633}
634
635static void nbd_client_close(NBDClient *client)
636{
637 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
638 close(client->sock);
639 client->sock = -1;
640 if (client->close) {
641 client->close(client);
642 }
643 nbd_client_put(client);
644}
645
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200646static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200647{
648 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200649 NBDExport *exp = client->exp;
650
Paolo Bonzini41996e32011-09-19 15:25:40 +0200651 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
652 client->nb_requests++;
653
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200654 if (QSIMPLEQ_EMPTY(&exp->requests)) {
655 req = g_malloc0(sizeof(NBDRequest));
656 req->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
657 } else {
658 req = QSIMPLEQ_FIRST(&exp->requests);
659 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
660 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200661 nbd_client_get(client);
662 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200663 return req;
664}
665
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200666static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200667{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200668 NBDClient *client = req->client;
669 QSIMPLEQ_INSERT_HEAD(&client->exp->requests, req, entry);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200670 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
671 qemu_notify_event();
672 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200673 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200674}
675
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200676NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
677 off_t size, uint32_t nbdflags)
678{
679 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200680 QSIMPLEQ_INIT(&exp->requests);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200681 exp->bs = bs;
682 exp->dev_offset = dev_offset;
683 exp->nbdflags = nbdflags;
684 exp->size = size == -1 ? exp->bs->total_sectors * 512 : size;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200685 return exp;
686}
687
688void nbd_export_close(NBDExport *exp)
689{
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200690 while (!QSIMPLEQ_EMPTY(&exp->requests)) {
691 NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
692 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
693 qemu_vfree(first->data);
694 g_free(first);
695 }
696
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200697 bdrv_close(exp->bs);
698 g_free(exp);
699}
700
Paolo Bonzini41996e32011-09-19 15:25:40 +0200701static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +0200702static void nbd_read(void *opaque);
703static void nbd_restart_write(void *opaque);
704
705static int nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200706 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +0200707{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200708 NBDClient *client = req->client;
709 int csock = client->sock;
Paolo Bonzini22045592011-09-19 14:25:30 +0200710 int rc, ret;
711
Paolo Bonzini262db382011-09-19 15:19:27 +0200712 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200713 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
714 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200715 client->send_coroutine = qemu_coroutine_self();
716
Paolo Bonzini22045592011-09-19 14:25:30 +0200717 if (!len) {
718 rc = nbd_send_reply(csock, reply);
719 if (rc == -1) {
720 rc = -errno;
721 }
722 } else {
723 socket_set_cork(csock, 1);
724 rc = nbd_send_reply(csock, reply);
725 if (rc != -1) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200726 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +0200727 if (ret != len) {
728 errno = EIO;
729 rc = -1;
730 }
731 }
732 if (rc == -1) {
733 rc = -errno;
734 }
735 socket_set_cork(csock, 0);
736 }
Paolo Bonzini262db382011-09-19 15:19:27 +0200737
738 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +0200739 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200740 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +0200741 return rc;
742}
743
Paolo Bonzini262db382011-09-19 15:19:27 +0200744static int nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +0200745{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200746 NBDClient *client = req->client;
747 int csock = client->sock;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200748 int rc;
749
Paolo Bonzini262db382011-09-19 15:19:27 +0200750 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzinia030b342011-09-19 15:07:54 +0200751 if (nbd_receive_request(csock, request) == -1) {
752 rc = -EIO;
753 goto out;
754 }
755
756 if (request->len > NBD_BUFFER_SIZE) {
757 LOG("len (%u) is larger than max len (%u)",
758 request->len, NBD_BUFFER_SIZE);
759 rc = -EINVAL;
760 goto out;
761 }
762
763 if ((request->from + request->len) < request->from) {
764 LOG("integer overflow detected! "
765 "you're probably being attacked");
766 rc = -EINVAL;
767 goto out;
768 }
769
770 TRACE("Decoding type");
771
772 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
773 TRACE("Reading %u byte(s)", request->len);
774
Paolo Bonzini262db382011-09-19 15:19:27 +0200775 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +0200776 LOG("reading from socket failed");
777 rc = -EIO;
778 goto out;
779 }
780 }
781 rc = 0;
782
783out:
Paolo Bonzini262db382011-09-19 15:19:27 +0200784 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200785 return rc;
786}
787
Paolo Bonzini262db382011-09-19 15:19:27 +0200788static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +0000789{
Paolo Bonzini262db382011-09-19 15:19:27 +0200790 NBDClient *client = opaque;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200791 NBDRequest *req = nbd_request_get(client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200792 NBDExport *exp = client->exp;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000793 struct nbd_request request;
794 struct nbd_reply reply;
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200795 int ret;
ths75818252008-07-03 13:41:03 +0000796
Nick Thomasb2e3d872011-02-22 15:44:51 +0000797 TRACE("Reading request.");
ths75818252008-07-03 13:41:03 +0000798
Paolo Bonzini262db382011-09-19 15:19:27 +0200799 ret = nbd_co_receive_request(req, &request);
Paolo Bonzinia030b342011-09-19 15:07:54 +0200800 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200801 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200802 }
ths75818252008-07-03 13:41:03 +0000803
Paolo Bonzinifae69412011-09-19 16:04:36 +0200804 reply.handle = request.handle;
805 reply.error = 0;
806
Paolo Bonzinia030b342011-09-19 15:07:54 +0200807 if (ret < 0) {
808 reply.error = -ret;
809 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000810 }
bellard7a5ca862008-05-27 21:13:40 +0000811
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200812 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000813 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
814 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200815 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +0200816 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000817 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +0200818 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000819 }
bellard7a5ca862008-05-27 21:13:40 +0000820
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200821 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000822 case NBD_CMD_READ:
823 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +0000824
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200825 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200826 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200827 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000828 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200829 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +0200830 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000831 }
bellard7a5ca862008-05-27 21:13:40 +0000832
Nick Thomasb2e3d872011-02-22 15:44:51 +0000833 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +0200834 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200835 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000836 break;
837 case NBD_CMD_WRITE:
838 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +0000839
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200840 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000841 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +0200842 reply.error = EROFS;
843 goto error_reply;
844 }
bellard7a5ca862008-05-27 21:13:40 +0000845
Paolo Bonzinifae69412011-09-19 16:04:36 +0200846 TRACE("Writing to device");
847
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200848 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200849 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +0200850 if (ret < 0) {
851 LOG("writing to file failed");
852 reply.error = -ret;
853 goto error_reply;
854 }
855
856 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200857 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200858 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +0200859 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200860 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +0200861 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200862 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000863 }
bellard7a5ca862008-05-27 21:13:40 +0000864
Paolo Bonzini262db382011-09-19 15:19:27 +0200865 if (nbd_co_send_reply(req, &reply, 0) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200866 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000867 break;
868 case NBD_CMD_DISC:
869 TRACE("Request type is DISCONNECT");
870 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +0200871 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +0200872 case NBD_CMD_FLUSH:
873 TRACE("Request type is FLUSH");
874
Paolo Bonzini262db382011-09-19 15:19:27 +0200875 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +0200876 if (ret < 0) {
877 LOG("flush failed");
878 reply.error = -ret;
879 }
880
Paolo Bonzini262db382011-09-19 15:19:27 +0200881 if (nbd_co_send_reply(req, &reply, 0) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200882 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +0200883 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +0200884 case NBD_CMD_TRIM:
885 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +0200886 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
887 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +0200888 if (ret < 0) {
889 LOG("discard failed");
890 reply.error = -ret;
891 }
Paolo Bonzini262db382011-09-19 15:19:27 +0200892 if (nbd_co_send_reply(req, &reply, 0) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200893 goto out;
Paolo Bonzini7a706632011-10-21 13:17:14 +0200894 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000895 default:
896 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +0200897 invalid_request:
898 reply.error = -EINVAL;
899 error_reply:
Paolo Bonzini262db382011-09-19 15:19:27 +0200900 if (nbd_co_send_reply(req, &reply, 0) == -1)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200901 goto out;
Paolo Bonzinifae69412011-09-19 16:04:36 +0200902 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000903 }
bellard7a5ca862008-05-27 21:13:40 +0000904
Nick Thomasb2e3d872011-02-22 15:44:51 +0000905 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +0000906
Paolo Bonzini262db382011-09-19 15:19:27 +0200907 nbd_request_put(req);
908 return;
909
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200910out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200911 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +0200912 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +0000913}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200914
Paolo Bonzini41996e32011-09-19 15:25:40 +0200915static int nbd_can_read(void *opaque)
916{
917 NBDClient *client = opaque;
918
919 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
920}
921
Paolo Bonzini1743b512011-09-19 14:33:23 +0200922static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200923{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200924 NBDClient *client = opaque;
925
Paolo Bonzini262db382011-09-19 15:19:27 +0200926 if (client->recv_coroutine) {
927 qemu_coroutine_enter(client->recv_coroutine, NULL);
928 } else {
929 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200930 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200931}
932
Paolo Bonzini262db382011-09-19 15:19:27 +0200933static void nbd_restart_write(void *opaque)
934{
935 NBDClient *client = opaque;
936
937 qemu_coroutine_enter(client->send_coroutine, NULL);
938}
939
Paolo Bonzini1743b512011-09-19 14:33:23 +0200940NBDClient *nbd_client_new(NBDExport *exp, int csock,
941 void (*close)(NBDClient *))
942{
943 NBDClient *client;
944 if (nbd_send_negotiate(csock, exp->size, exp->nbdflags) == -1) {
945 return NULL;
946 }
947 client = g_malloc0(sizeof(NBDClient));
948 client->refcount = 1;
949 client->exp = exp;
950 client->sock = csock;
951 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +0200952 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200953 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200954 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200955}