blob: 5a3088d232f6403ad5a400de3752b019a5abad6f [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"
bellard7a5ca862008-05-27 21:13:40 +000021
Paolo Bonzini262db382011-09-19 15:19:27 +020022#include "qemu-coroutine.h"
23
bellard7a5ca862008-05-27 21:13:40 +000024#include <errno.h>
25#include <string.h>
aliguori03ff3ca2008-09-15 15:51:35 +000026#ifndef _WIN32
bellard7a5ca862008-05-27 21:13:40 +000027#include <sys/ioctl.h>
aliguori03ff3ca2008-09-15 15:51:35 +000028#endif
Andreas Färber5dc2eec2010-09-20 00:50:46 +020029#if defined(__sun__) || defined(__HAIKU__)
aliguori7e00eb92008-08-02 01:57:02 +000030#include <sys/ioccom.h>
31#endif
bellard7a5ca862008-05-27 21:13:40 +000032#include <ctype.h>
33#include <inttypes.h>
bellard7a5ca862008-05-27 21:13:40 +000034
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +020035#ifdef __linux__
36#include <linux/fs.h>
37#endif
38
aliguori03ff3ca2008-09-15 15:51:35 +000039#include "qemu_socket.h"
Paolo Bonzinid9a73802011-09-19 14:18:33 +020040#include "qemu-queue.h"
ths75818252008-07-03 13:41:03 +000041
aliguori03ff3ca2008-09-15 15:51:35 +000042//#define DEBUG_NBD
43
44#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000045#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000046 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000047} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000048#else
49#define TRACE(msg, ...) \
50 do { } while (0)
51#endif
bellard7a5ca862008-05-27 21:13:40 +000052
53#define LOG(msg, ...) do { \
54 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
55 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
56} while(0)
57
bellard7a5ca862008-05-27 21:13:40 +000058/* This is all part of the "official" NBD API */
59
Paolo Bonzinifa26c262012-08-22 15:13:30 +020060#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
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
Paolo Bonzinifa26c262012-08-22 15:13:30 +020064#define NBD_OPTS_MAGIC 0x49484156454F5054LL
65#define NBD_CLIENT_MAGIC 0x0000420281861253LL
bellard7a5ca862008-05-27 21:13:40 +000066
67#define NBD_SET_SOCK _IO(0xab, 0)
68#define NBD_SET_BLKSIZE _IO(0xab, 1)
69#define NBD_SET_SIZE _IO(0xab, 2)
70#define NBD_DO_IT _IO(0xab, 3)
71#define NBD_CLEAR_SOCK _IO(0xab, 4)
72#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000073#define NBD_PRINT_DEBUG _IO(0xab, 6)
74#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000075#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020076#define NBD_SET_TIMEOUT _IO(0xab, 9)
77#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000078
Nick Thomasb2e3d872011-02-22 15:44:51 +000079#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020080
Paolo Bonzini9a304d22012-08-22 15:30:31 +020081/* Definitions for opaque data types */
82
83typedef struct NBDRequest NBDRequest;
84
85struct NBDRequest {
86 QSIMPLEQ_ENTRY(NBDRequest) entry;
87 NBDClient *client;
88 uint8_t *data;
89};
90
91struct NBDExport {
92 BlockDriverState *bs;
93 off_t dev_offset;
94 off_t size;
95 uint32_t nbdflags;
96 QSIMPLEQ_HEAD(, NBDRequest) requests;
97};
98
99struct NBDClient {
100 int refcount;
101 void (*close)(NBDClient *client);
102
103 NBDExport *exp;
104 int sock;
105
106 Coroutine *recv_coroutine;
107
108 CoMutex send_lock;
109 Coroutine *send_coroutine;
110
111 int nb_requests;
112};
113
bellard7a5ca862008-05-27 21:13:40 +0000114/* That's all folks */
115
Paolo Bonzini185b4332012-03-05 08:56:10 +0100116ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000117{
118 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100119 int err;
bellard7a5ca862008-05-27 21:13:40 +0000120
Paolo Bonziniae255e52011-09-08 14:28:59 +0200121 if (qemu_in_coroutine()) {
122 if (do_read) {
123 return qemu_co_recv(fd, buffer, size);
124 } else {
125 return qemu_co_send(fd, buffer, size);
126 }
127 }
128
bellard7a5ca862008-05-27 21:13:40 +0000129 while (offset < size) {
130 ssize_t len;
131
132 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000133 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000134 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000135 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000136 }
137
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100138 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100139 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000140
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100141 /* recoverable error */
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100142 if (err == EINTR || (offset > 0 && err == EAGAIN)) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100143 continue;
144 }
145
146 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100147 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000148 }
149
150 /* eof */
151 if (len == 0) {
152 break;
153 }
154
bellard7a5ca862008-05-27 21:13:40 +0000155 offset += len;
156 }
157
158 return offset;
159}
160
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100161static ssize_t read_sync(int fd, void *buffer, size_t size)
162{
163 /* Sockets are kept in blocking mode in the negotiation phase. After
164 * that, a non-readable socket simply means that another thread stole
165 * our request/reply. Synchronization is done with recv_coroutine, so
166 * that this is coroutine-safe.
167 */
168 return nbd_wr_sync(fd, buffer, size, true);
169}
170
171static ssize_t write_sync(int fd, void *buffer, size_t size)
172{
173 int ret;
174 do {
175 /* For writes, we do expect the socket to be writable. */
176 ret = nbd_wr_sync(fd, buffer, size, false);
177 } while (ret == -EAGAIN);
178 return ret;
179}
180
Nick Thomasc12504c2011-02-22 15:44:53 +0000181static void combine_addr(char *buf, size_t len, const char* address,
182 uint16_t port)
183{
184 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
185 if (strstr(address, ":")) {
186 snprintf(buf, len, "[%s]:%u", address, port);
187 } else {
188 snprintf(buf, len, "%s:%u", address, port);
189 }
190}
191
ths75818252008-07-03 13:41:03 +0000192int tcp_socket_outgoing(const char *address, uint16_t port)
bellard7a5ca862008-05-27 21:13:40 +0000193{
Nick Thomasc12504c2011-02-22 15:44:53 +0000194 char address_and_port[128];
195 combine_addr(address_and_port, 128, address, port);
196 return tcp_socket_outgoing_spec(address_and_port);
197}
bellard7a5ca862008-05-27 21:13:40 +0000198
Nick Thomasc12504c2011-02-22 15:44:53 +0000199int tcp_socket_outgoing_spec(const char *address_and_port)
200{
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300201 return inet_connect(address_and_port, true, NULL, NULL);
bellard7a5ca862008-05-27 21:13:40 +0000202}
203
204int tcp_socket_incoming(const char *address, uint16_t port)
205{
Nick Thomasc12504c2011-02-22 15:44:53 +0000206 char address_and_port[128];
207 combine_addr(address_and_port, 128, address, port);
208 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000209}
210
Nick Thomasc12504c2011-02-22 15:44:53 +0000211int tcp_socket_incoming_spec(const char *address_and_port)
212{
213 char *ostr = NULL;
214 int olen = 0;
Amos Kong029409e2012-05-11 00:28:26 +0800215 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0, NULL);
Nick Thomasc12504c2011-02-22 15:44:53 +0000216}
217
thscd831bd2008-07-03 10:23:51 +0000218int unix_socket_incoming(const char *path)
219{
Nick Thomasc12504c2011-02-22 15:44:53 +0000220 char *ostr = NULL;
221 int olen = 0;
thscd831bd2008-07-03 10:23:51 +0000222
Nick Thomasc12504c2011-02-22 15:44:53 +0000223 return unix_listen(path, ostr, olen);
thscd831bd2008-07-03 10:23:51 +0000224}
225
226int unix_socket_outgoing(const char *path)
227{
Nick Thomasc12504c2011-02-22 15:44:53 +0000228 return unix_connect(path);
thscd831bd2008-07-03 10:23:51 +0000229}
thscd831bd2008-07-03 10:23:51 +0000230
bellard7a5ca862008-05-27 21:13:40 +0000231/* Basic flow
232
233 Server Client
234
235 Negotiate
236 Request
237 Response
238 Request
239 Response
240 ...
241 ...
242 Request (type == 2)
243*/
244
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200245static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000246{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200247 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000248 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100249 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000250
Nick Thomasb2e3d872011-02-22 15:44:51 +0000251 /* Negotiate
252 [ 0 .. 7] passwd ("NBDMAGIC")
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200253 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000254 [16 .. 23] size
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200255 [24 .. 27] flags
256 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000257 */
bellard7a5ca862008-05-27 21:13:40 +0000258
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100259 socket_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100260 rc = -EINVAL;
261
Nick Thomasb2e3d872011-02-22 15:44:51 +0000262 TRACE("Beginning negotiation.");
263 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200264 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200265 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200266 cpu_to_be32w((uint32_t*)(buf + 24),
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200267 client->exp->nbdflags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
Paolo Bonzini7a706632011-10-21 13:17:14 +0200268 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200269 memset(buf + 28, 0, 124);
bellard7a5ca862008-05-27 21:13:40 +0000270
Nick Thomasb2e3d872011-02-22 15:44:51 +0000271 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
272 LOG("write failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100273 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000274 }
bellard7a5ca862008-05-27 21:13:40 +0000275
Dong Xu Wang07f35072011-11-22 18:06:26 +0800276 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100277 rc = 0;
278fail:
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100279 socket_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100280 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000281}
282
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200283int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
284 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000285{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000286 char buf[256];
287 uint64_t magic, s;
288 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100289 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000290
Dong Xu Wang07f35072011-11-22 18:06:26 +0800291 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000292
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100293 socket_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100294 rc = -EINVAL;
295
Nick Thomasb2e3d872011-02-22 15:44:51 +0000296 if (read_sync(csock, buf, 8) != 8) {
297 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100298 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000299 }
bellard7a5ca862008-05-27 21:13:40 +0000300
Nick Thomasb2e3d872011-02-22 15:44:51 +0000301 buf[8] = '\0';
302 if (strlen(buf) == 0) {
303 LOG("server connection closed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100304 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000305 }
bellard7a5ca862008-05-27 21:13:40 +0000306
Nick Thomasb2e3d872011-02-22 15:44:51 +0000307 TRACE("Magic is %c%c%c%c%c%c%c%c",
308 qemu_isprint(buf[0]) ? buf[0] : '.',
309 qemu_isprint(buf[1]) ? buf[1] : '.',
310 qemu_isprint(buf[2]) ? buf[2] : '.',
311 qemu_isprint(buf[3]) ? buf[3] : '.',
312 qemu_isprint(buf[4]) ? buf[4] : '.',
313 qemu_isprint(buf[5]) ? buf[5] : '.',
314 qemu_isprint(buf[6]) ? buf[6] : '.',
315 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000316
Nick Thomasb2e3d872011-02-22 15:44:51 +0000317 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
318 LOG("Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100319 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000320 }
bellard7a5ca862008-05-27 21:13:40 +0000321
Nick Thomasb2e3d872011-02-22 15:44:51 +0000322 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
323 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100324 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000325 }
326 magic = be64_to_cpu(magic);
327 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000328
Nick Thomasb2e3d872011-02-22 15:44:51 +0000329 if (name) {
330 uint32_t reserved = 0;
331 uint32_t opt;
332 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200333
Nick Thomasb2e3d872011-02-22 15:44:51 +0000334 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200335 if (magic != NBD_OPTS_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000336 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100337 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000338 }
339 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
340 LOG("flags read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100341 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000342 }
343 *flags = be16_to_cpu(tmp) << 16;
344 /* reserved for future use */
345 if (write_sync(csock, &reserved, sizeof(reserved)) !=
346 sizeof(reserved)) {
347 LOG("write failed (reserved)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100348 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000349 }
350 /* write the export name */
351 magic = cpu_to_be64(magic);
352 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
353 LOG("write failed (magic)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100354 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000355 }
356 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
357 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
358 LOG("write failed (opt)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100359 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000360 }
361 namesize = cpu_to_be32(strlen(name));
362 if (write_sync(csock, &namesize, sizeof(namesize)) !=
363 sizeof(namesize)) {
364 LOG("write failed (namesize)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100365 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000366 }
367 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
368 LOG("write failed (name)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100369 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000370 }
371 } else {
372 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200373
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200374 if (magic != NBD_CLIENT_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000375 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100376 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000377 }
378 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200379
Nick Thomasb2e3d872011-02-22 15:44:51 +0000380 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
381 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100382 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000383 }
384 *size = be64_to_cpu(s);
385 *blocksize = 1024;
386 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200387
Nick Thomasb2e3d872011-02-22 15:44:51 +0000388 if (!name) {
389 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
390 LOG("read failed (flags)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100391 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000392 }
393 *flags = be32_to_cpup(flags);
394 } else {
395 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
396 LOG("read failed (tmp)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100397 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000398 }
399 *flags |= be32_to_cpu(tmp);
400 }
401 if (read_sync(csock, &buf, 124) != 124) {
402 LOG("read failed (buf)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100403 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000404 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100405 rc = 0;
406
407fail:
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100408 socket_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100409 return rc;
thscd831bd2008-07-03 10:23:51 +0000410}
bellard7a5ca862008-05-27 21:13:40 +0000411
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200412#ifdef __linux__
413int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000414{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800415 TRACE("Setting NBD socket");
416
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100417 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800418 int serrno = errno;
419 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100420 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800421 }
422
Nick Thomasb2e3d872011-02-22 15:44:51 +0000423 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000424
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100425 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000426 int serrno = errno;
427 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100428 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000429 }
bellard7a5ca862008-05-27 21:13:40 +0000430
Blue Swirl0bfcd592010-05-22 08:02:12 +0000431 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000432
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100433 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000434 int serrno = errno;
435 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100436 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000437 }
bellard7a5ca862008-05-27 21:13:40 +0000438
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200439 if (flags & NBD_FLAG_READ_ONLY) {
440 int read_only = 1;
441 TRACE("Setting readonly attribute");
442
443 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
444 int serrno = errno;
445 LOG("Failed setting read-only attribute");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100446 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200447 }
448 }
449
Paolo Bonzini973b3d02011-09-08 17:24:56 +0200450 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
451 && errno != ENOTTY) {
452 int serrno = errno;
453 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100454 return -serrno;
Paolo Bonzini973b3d02011-09-08 17:24:56 +0200455 }
456
Nick Thomasb2e3d872011-02-22 15:44:51 +0000457 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000458
Nick Thomasb2e3d872011-02-22 15:44:51 +0000459 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000460}
461
462int nbd_disconnect(int fd)
463{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000464 ioctl(fd, NBD_CLEAR_QUE);
465 ioctl(fd, NBD_DISCONNECT);
466 ioctl(fd, NBD_CLEAR_SOCK);
467 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000468}
469
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200470int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000471{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000472 int ret;
473 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000474
Nick Thomasb2e3d872011-02-22 15:44:51 +0000475 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000476
Nick Thomasb2e3d872011-02-22 15:44:51 +0000477 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100478 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100479 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
480 * the socket via NBD_DISCONNECT. We do not want to return 1 in
481 * that case.
482 */
483 ret = 0;
484 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000485 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000486
Nick Thomasb2e3d872011-02-22 15:44:51 +0000487 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000488
Nick Thomasb2e3d872011-02-22 15:44:51 +0000489 TRACE("Clearing NBD queue");
490 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000491
Nick Thomasb2e3d872011-02-22 15:44:51 +0000492 TRACE("Clearing NBD socket");
493 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000494
Nick Thomasb2e3d872011-02-22 15:44:51 +0000495 errno = serrno;
496 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000497}
aliguori03ff3ca2008-09-15 15:51:35 +0000498#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200499int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000500{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100501 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000502}
503
504int nbd_disconnect(int fd)
505{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100506 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000507}
508
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200509int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000510{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100511 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000512}
513#endif
bellard7a5ca862008-05-27 21:13:40 +0000514
Paolo Bonzini94e73402012-03-07 11:25:01 +0100515ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000516{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200517 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100518 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000519
Nick Thomasb2e3d872011-02-22 15:44:51 +0000520 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
521 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
522 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
523 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
524 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000525
Nick Thomasb2e3d872011-02-22 15:44:51 +0000526 TRACE("Sending request to client: "
527 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
528 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000529
Paolo Bonzini185b4332012-03-05 08:56:10 +0100530 ret = write_sync(csock, buf, sizeof(buf));
531 if (ret < 0) {
532 return ret;
533 }
534
535 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000536 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100537 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000538 }
539 return 0;
ths75818252008-07-03 13:41:03 +0000540}
541
Paolo Bonzini94e73402012-03-07 11:25:01 +0100542static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000543{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200544 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000545 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100546 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000547
Paolo Bonzini185b4332012-03-05 08:56:10 +0100548 ret = read_sync(csock, buf, sizeof(buf));
549 if (ret < 0) {
550 return ret;
551 }
552
553 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000554 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100555 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000556 }
bellard7a5ca862008-05-27 21:13:40 +0000557
Nick Thomasb2e3d872011-02-22 15:44:51 +0000558 /* Request
559 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
560 [ 4 .. 7] type (0 == READ, 1 == WRITE)
561 [ 8 .. 15] handle
562 [16 .. 23] from
563 [24 .. 27] len
564 */
bellard7a5ca862008-05-27 21:13:40 +0000565
Nick Thomasb2e3d872011-02-22 15:44:51 +0000566 magic = be32_to_cpup((uint32_t*)buf);
567 request->type = be32_to_cpup((uint32_t*)(buf + 4));
568 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
569 request->from = be64_to_cpup((uint64_t*)(buf + 16));
570 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000571
Nick Thomasb2e3d872011-02-22 15:44:51 +0000572 TRACE("Got request: "
573 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
574 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000575
Nick Thomasb2e3d872011-02-22 15:44:51 +0000576 if (magic != NBD_REQUEST_MAGIC) {
577 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100578 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000579 }
580 return 0;
ths75818252008-07-03 13:41:03 +0000581}
bellard7a5ca862008-05-27 21:13:40 +0000582
Paolo Bonzini94e73402012-03-07 11:25:01 +0100583ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000584{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000585 uint8_t buf[NBD_REPLY_SIZE];
586 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100587 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000588
Paolo Bonzini185b4332012-03-05 08:56:10 +0100589 ret = read_sync(csock, buf, sizeof(buf));
590 if (ret < 0) {
591 return ret;
592 }
593
594 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000595 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100596 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000597 }
bellard7a5ca862008-05-27 21:13:40 +0000598
Nick Thomasb2e3d872011-02-22 15:44:51 +0000599 /* Reply
600 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
601 [ 4 .. 7] error (0 == no error)
602 [ 7 .. 15] handle
603 */
ths75818252008-07-03 13:41:03 +0000604
Nick Thomasb2e3d872011-02-22 15:44:51 +0000605 magic = be32_to_cpup((uint32_t*)buf);
606 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
607 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000608
Nick Thomasb2e3d872011-02-22 15:44:51 +0000609 TRACE("Got reply: "
610 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
611 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000612
Nick Thomasb2e3d872011-02-22 15:44:51 +0000613 if (magic != NBD_REPLY_MAGIC) {
614 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100615 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000616 }
617 return 0;
ths75818252008-07-03 13:41:03 +0000618}
619
Paolo Bonzini94e73402012-03-07 11:25:01 +0100620static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000621{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200622 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100623 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000624
Nick Thomasb2e3d872011-02-22 15:44:51 +0000625 /* Reply
626 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
627 [ 4 .. 7] error (0 == no error)
628 [ 7 .. 15] handle
629 */
630 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
631 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
632 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000633
Nick Thomasb2e3d872011-02-22 15:44:51 +0000634 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000635
Paolo Bonzini185b4332012-03-05 08:56:10 +0100636 ret = write_sync(csock, buf, sizeof(buf));
637 if (ret < 0) {
638 return ret;
639 }
640
641 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000642 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100643 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000644 }
645 return 0;
ths75818252008-07-03 13:41:03 +0000646}
647
Paolo Bonzini41996e32011-09-19 15:25:40 +0200648#define MAX_NBD_REQUESTS 16
649
Paolo Bonzini1743b512011-09-19 14:33:23 +0200650static void nbd_client_get(NBDClient *client)
651{
652 client->refcount++;
653}
654
655static void nbd_client_put(NBDClient *client)
656{
657 if (--client->refcount == 0) {
658 g_free(client);
659 }
660}
661
662static void nbd_client_close(NBDClient *client)
663{
664 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
665 close(client->sock);
666 client->sock = -1;
667 if (client->close) {
668 client->close(client);
669 }
670 nbd_client_put(client);
671}
672
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200673static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200674{
675 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200676 NBDExport *exp = client->exp;
677
Paolo Bonzini41996e32011-09-19 15:25:40 +0200678 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
679 client->nb_requests++;
680
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200681 if (QSIMPLEQ_EMPTY(&exp->requests)) {
682 req = g_malloc0(sizeof(NBDRequest));
683 req->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
684 } else {
685 req = QSIMPLEQ_FIRST(&exp->requests);
686 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
687 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200688 nbd_client_get(client);
689 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200690 return req;
691}
692
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200693static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200694{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200695 NBDClient *client = req->client;
696 QSIMPLEQ_INSERT_HEAD(&client->exp->requests, req, entry);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200697 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
698 qemu_notify_event();
699 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200700 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200701}
702
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200703NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
704 off_t size, uint32_t nbdflags)
705{
706 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200707 QSIMPLEQ_INIT(&exp->requests);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200708 exp->bs = bs;
709 exp->dev_offset = dev_offset;
710 exp->nbdflags = nbdflags;
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100711 exp->size = size == -1 ? bdrv_getlength(bs) : size;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200712 return exp;
713}
714
715void nbd_export_close(NBDExport *exp)
716{
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200717 while (!QSIMPLEQ_EMPTY(&exp->requests)) {
718 NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
719 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
720 qemu_vfree(first->data);
721 g_free(first);
722 }
723
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200724 bdrv_close(exp->bs);
725 g_free(exp);
726}
727
Paolo Bonzini41996e32011-09-19 15:25:40 +0200728static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +0200729static void nbd_read(void *opaque);
730static void nbd_restart_write(void *opaque);
731
Paolo Bonzini94e73402012-03-07 11:25:01 +0100732static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
733 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +0200734{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200735 NBDClient *client = req->client;
736 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100737 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +0200738
Paolo Bonzini262db382011-09-19 15:19:27 +0200739 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200740 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
741 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200742 client->send_coroutine = qemu_coroutine_self();
743
Paolo Bonzini22045592011-09-19 14:25:30 +0200744 if (!len) {
745 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +0200746 } else {
747 socket_set_cork(csock, 1);
748 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100749 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200750 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +0200751 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100752 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +0200753 }
754 }
Paolo Bonzini22045592011-09-19 14:25:30 +0200755 socket_set_cork(csock, 0);
756 }
Paolo Bonzini262db382011-09-19 15:19:27 +0200757
758 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +0200759 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200760 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +0200761 return rc;
762}
763
Paolo Bonzini94e73402012-03-07 11:25:01 +0100764static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +0200765{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200766 NBDClient *client = req->client;
767 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100768 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200769
Paolo Bonzini262db382011-09-19 15:19:27 +0200770 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100771 rc = nbd_receive_request(csock, request);
772 if (rc < 0) {
773 if (rc != -EAGAIN) {
774 rc = -EIO;
775 }
Paolo Bonzinia030b342011-09-19 15:07:54 +0200776 goto out;
777 }
778
779 if (request->len > NBD_BUFFER_SIZE) {
780 LOG("len (%u) is larger than max len (%u)",
781 request->len, NBD_BUFFER_SIZE);
782 rc = -EINVAL;
783 goto out;
784 }
785
786 if ((request->from + request->len) < request->from) {
787 LOG("integer overflow detected! "
788 "you're probably being attacked");
789 rc = -EINVAL;
790 goto out;
791 }
792
793 TRACE("Decoding type");
794
795 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
796 TRACE("Reading %u byte(s)", request->len);
797
Paolo Bonzini262db382011-09-19 15:19:27 +0200798 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +0200799 LOG("reading from socket failed");
800 rc = -EIO;
801 goto out;
802 }
803 }
804 rc = 0;
805
806out:
Paolo Bonzini262db382011-09-19 15:19:27 +0200807 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200808 return rc;
809}
810
Paolo Bonzini262db382011-09-19 15:19:27 +0200811static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +0000812{
Paolo Bonzini262db382011-09-19 15:19:27 +0200813 NBDClient *client = opaque;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200814 NBDRequest *req = nbd_request_get(client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200815 NBDExport *exp = client->exp;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000816 struct nbd_request request;
817 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100818 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000819
Nick Thomasb2e3d872011-02-22 15:44:51 +0000820 TRACE("Reading request.");
ths75818252008-07-03 13:41:03 +0000821
Paolo Bonzini262db382011-09-19 15:19:27 +0200822 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100823 if (ret == -EAGAIN) {
824 goto done;
825 }
Paolo Bonzinia030b342011-09-19 15:07:54 +0200826 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200827 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200828 }
ths75818252008-07-03 13:41:03 +0000829
Paolo Bonzinifae69412011-09-19 16:04:36 +0200830 reply.handle = request.handle;
831 reply.error = 0;
832
Paolo Bonzinia030b342011-09-19 15:07:54 +0200833 if (ret < 0) {
834 reply.error = -ret;
835 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000836 }
bellard7a5ca862008-05-27 21:13:40 +0000837
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200838 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000839 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
840 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200841 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +0200842 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000843 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +0200844 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000845 }
bellard7a5ca862008-05-27 21:13:40 +0000846
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200847 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000848 case NBD_CMD_READ:
849 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +0000850
Paolo Bonzinie25ceb72012-04-19 11:59:11 +0200851 if (request.type & NBD_CMD_FLAG_FUA) {
852 ret = bdrv_co_flush(exp->bs);
853 if (ret < 0) {
854 LOG("flush failed");
855 reply.error = -ret;
856 goto error_reply;
857 }
858 }
859
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200860 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200861 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200862 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000863 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200864 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +0200865 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000866 }
bellard7a5ca862008-05-27 21:13:40 +0000867
Nick Thomasb2e3d872011-02-22 15:44:51 +0000868 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +0200869 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200870 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000871 break;
872 case NBD_CMD_WRITE:
873 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +0000874
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200875 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000876 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +0200877 reply.error = EROFS;
878 goto error_reply;
879 }
bellard7a5ca862008-05-27 21:13:40 +0000880
Paolo Bonzinifae69412011-09-19 16:04:36 +0200881 TRACE("Writing to device");
882
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200883 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200884 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +0200885 if (ret < 0) {
886 LOG("writing to file failed");
887 reply.error = -ret;
888 goto error_reply;
889 }
890
891 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200892 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200893 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +0200894 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200895 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +0200896 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200897 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000898 }
bellard7a5ca862008-05-27 21:13:40 +0000899
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100900 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200901 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100902 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000903 break;
904 case NBD_CMD_DISC:
905 TRACE("Request type is DISCONNECT");
906 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +0200907 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +0200908 case NBD_CMD_FLUSH:
909 TRACE("Request type is FLUSH");
910
Paolo Bonzini262db382011-09-19 15:19:27 +0200911 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +0200912 if (ret < 0) {
913 LOG("flush failed");
914 reply.error = -ret;
915 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100916 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200917 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100918 }
Paolo Bonzini1486d042011-10-21 13:17:14 +0200919 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +0200920 case NBD_CMD_TRIM:
921 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +0200922 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
923 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +0200924 if (ret < 0) {
925 LOG("discard failed");
926 reply.error = -ret;
927 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100928 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200929 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100930 }
Paolo Bonzini7a706632011-10-21 13:17:14 +0200931 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000932 default:
933 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +0200934 invalid_request:
935 reply.error = -EINVAL;
936 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100937 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200938 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100939 }
Paolo Bonzinifae69412011-09-19 16:04:36 +0200940 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000941 }
bellard7a5ca862008-05-27 21:13:40 +0000942
Nick Thomasb2e3d872011-02-22 15:44:51 +0000943 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +0000944
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100945done:
Paolo Bonzini262db382011-09-19 15:19:27 +0200946 nbd_request_put(req);
947 return;
948
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200949out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200950 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +0200951 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +0000952}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200953
Paolo Bonzini41996e32011-09-19 15:25:40 +0200954static int nbd_can_read(void *opaque)
955{
956 NBDClient *client = opaque;
957
958 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
959}
960
Paolo Bonzini1743b512011-09-19 14:33:23 +0200961static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200962{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200963 NBDClient *client = opaque;
964
Paolo Bonzini262db382011-09-19 15:19:27 +0200965 if (client->recv_coroutine) {
966 qemu_coroutine_enter(client->recv_coroutine, NULL);
967 } else {
968 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200969 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200970}
971
Paolo Bonzini262db382011-09-19 15:19:27 +0200972static void nbd_restart_write(void *opaque)
973{
974 NBDClient *client = opaque;
975
976 qemu_coroutine_enter(client->send_coroutine, NULL);
977}
978
Paolo Bonzini1743b512011-09-19 14:33:23 +0200979NBDClient *nbd_client_new(NBDExport *exp, int csock,
980 void (*close)(NBDClient *))
981{
982 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +0200983 client = g_malloc0(sizeof(NBDClient));
984 client->refcount = 1;
985 client->exp = exp;
986 client->sock = csock;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200987 if (nbd_send_negotiate(client) < 0) {
988 g_free(client);
989 return NULL;
990 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200991 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +0200992 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200993 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini1743b512011-09-19 14:33:23 +0200994 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200995}