blob: 2606403a41facd02d5b50d14dcd407b3aa62bd1c [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
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/nbd.h"
20#include "block/block.h"
bellard7a5ca862008-05-27 21:13:40 +000021
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/coroutine.h"
Paolo Bonzini262db382011-09-19 15:19:27 +020023
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
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/sockets.h"
40#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 {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +020092 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +020093 void (*close)(NBDExport *exp);
94
Paolo Bonzini9a304d22012-08-22 15:30:31 +020095 BlockDriverState *bs;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020096 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020097 off_t dev_offset;
98 off_t size;
99 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200100 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200101 QTAILQ_ENTRY(NBDExport) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200102};
103
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200104static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
105
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200106struct NBDClient {
107 int refcount;
108 void (*close)(NBDClient *client);
109
110 NBDExport *exp;
111 int sock;
112
113 Coroutine *recv_coroutine;
114
115 CoMutex send_lock;
116 Coroutine *send_coroutine;
117
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200118 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200119 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200120 bool closing;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200121};
122
bellard7a5ca862008-05-27 21:13:40 +0000123/* That's all folks */
124
Paolo Bonzini185b4332012-03-05 08:56:10 +0100125ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000126{
127 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100128 int err;
bellard7a5ca862008-05-27 21:13:40 +0000129
Paolo Bonziniae255e52011-09-08 14:28:59 +0200130 if (qemu_in_coroutine()) {
131 if (do_read) {
132 return qemu_co_recv(fd, buffer, size);
133 } else {
134 return qemu_co_send(fd, buffer, size);
135 }
136 }
137
bellard7a5ca862008-05-27 21:13:40 +0000138 while (offset < size) {
139 ssize_t len;
140
141 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000142 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000143 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000144 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000145 }
146
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100147 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100148 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000149
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100150 /* recoverable error */
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100151 if (err == EINTR || (offset > 0 && err == EAGAIN)) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100152 continue;
153 }
154
155 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100156 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000157 }
158
159 /* eof */
160 if (len == 0) {
161 break;
162 }
163
bellard7a5ca862008-05-27 21:13:40 +0000164 offset += len;
165 }
166
167 return offset;
168}
169
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100170static ssize_t read_sync(int fd, void *buffer, size_t size)
171{
172 /* Sockets are kept in blocking mode in the negotiation phase. After
173 * that, a non-readable socket simply means that another thread stole
174 * our request/reply. Synchronization is done with recv_coroutine, so
175 * that this is coroutine-safe.
176 */
177 return nbd_wr_sync(fd, buffer, size, true);
178}
179
180static ssize_t write_sync(int fd, void *buffer, size_t size)
181{
182 int ret;
183 do {
184 /* For writes, we do expect the socket to be writable. */
185 ret = nbd_wr_sync(fd, buffer, size, false);
186 } while (ret == -EAGAIN);
187 return ret;
188}
189
Nick Thomasc12504c2011-02-22 15:44:53 +0000190static void combine_addr(char *buf, size_t len, const char* address,
191 uint16_t port)
192{
193 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
194 if (strstr(address, ":")) {
195 snprintf(buf, len, "[%s]:%u", address, port);
196 } else {
197 snprintf(buf, len, "%s:%u", address, port);
198 }
199}
200
Kevin Wolff17c90b2013-03-15 11:55:29 +0100201int tcp_socket_outgoing_opts(QemuOpts *opts)
202{
203 Error *local_err = NULL;
204 int fd = inet_connect_opts(opts, &local_err, NULL, NULL);
205 if (local_err != NULL) {
206 qerror_report_err(local_err);
207 error_free(local_err);
208 }
209
210 return fd;
211}
212
bellard7a5ca862008-05-27 21:13:40 +0000213int tcp_socket_incoming(const char *address, uint16_t port)
214{
Nick Thomasc12504c2011-02-22 15:44:53 +0000215 char address_and_port[128];
216 combine_addr(address_and_port, 128, address, port);
217 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000218}
219
Nick Thomasc12504c2011-02-22 15:44:53 +0000220int tcp_socket_incoming_spec(const char *address_and_port)
221{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200222 Error *local_err = NULL;
223 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
224
225 if (local_err != NULL) {
226 qerror_report_err(local_err);
227 error_free(local_err);
228 }
229 return fd;
Nick Thomasc12504c2011-02-22 15:44:53 +0000230}
231
thscd831bd2008-07-03 10:23:51 +0000232int unix_socket_incoming(const char *path)
233{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200234 Error *local_err = NULL;
235 int fd = unix_listen(path, NULL, 0, &local_err);
thscd831bd2008-07-03 10:23:51 +0000236
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200237 if (local_err != NULL) {
238 qerror_report_err(local_err);
239 error_free(local_err);
240 }
241 return fd;
thscd831bd2008-07-03 10:23:51 +0000242}
243
244int unix_socket_outgoing(const char *path)
245{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200246 Error *local_err = NULL;
247 int fd = unix_connect(path, &local_err);
248
249 if (local_err != NULL) {
250 qerror_report_err(local_err);
251 error_free(local_err);
252 }
253 return fd;
thscd831bd2008-07-03 10:23:51 +0000254}
thscd831bd2008-07-03 10:23:51 +0000255
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200256/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000257
258 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000259 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200260
261 or
262
263 Server Client
264 Negotiate #1
265 Option
266 Negotiate #2
267
268 ----
269
270 followed by
271
272 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000273 Request
274 Response
275 Request
276 Response
277 ...
278 ...
279 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200280
bellard7a5ca862008-05-27 21:13:40 +0000281*/
282
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200283static int nbd_receive_options(NBDClient *client)
284{
285 int csock = client->sock;
286 char name[256];
287 uint32_t tmp, length;
288 uint64_t magic;
289 int rc;
290
291 /* Client sends:
292 [ 0 .. 3] reserved (0)
293 [ 4 .. 11] NBD_OPTS_MAGIC
294 [12 .. 15] NBD_OPT_EXPORT_NAME
295 [16 .. 19] length
296 [20 .. xx] export name (length bytes)
297 */
298
299 rc = -EINVAL;
300 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
301 LOG("read failed");
302 goto fail;
303 }
304 TRACE("Checking reserved");
305 if (tmp != 0) {
306 LOG("Bad reserved received");
307 goto fail;
308 }
309
310 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
311 LOG("read failed");
312 goto fail;
313 }
314 TRACE("Checking reserved");
315 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
316 LOG("Bad magic received");
317 goto fail;
318 }
319
320 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
321 LOG("read failed");
322 goto fail;
323 }
324 TRACE("Checking option");
325 if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
326 LOG("Bad option received");
327 goto fail;
328 }
329
330 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
331 LOG("read failed");
332 goto fail;
333 }
334 TRACE("Checking length");
335 length = be32_to_cpu(length);
336 if (length > 255) {
337 LOG("Bad length received");
338 goto fail;
339 }
340 if (read_sync(csock, name, length) != length) {
341 LOG("read failed");
342 goto fail;
343 }
344 name[length] = '\0';
345
346 client->exp = nbd_export_find(name);
347 if (!client->exp) {
348 LOG("export not found");
349 goto fail;
350 }
351
352 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
353 nbd_export_get(client->exp);
354
355 TRACE("Option negotiation succeeded.");
356 rc = 0;
357fail:
358 return rc;
359}
360
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200361static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000362{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200363 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000364 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100365 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200366 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
367 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000368
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200369 /* Negotiation header without options:
370 [ 0 .. 7] passwd ("NBDMAGIC")
371 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000372 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200373 [24 .. 25] server flags (0)
374 [24 .. 27] export flags
375 [28 .. 151] reserved (0)
376
377 Negotiation header with options, part 1:
378 [ 0 .. 7] passwd ("NBDMAGIC")
379 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
380 [16 .. 17] server flags (0)
381
382 part 2 (after options are sent):
383 [18 .. 25] size
384 [26 .. 27] export flags
385 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000386 */
bellard7a5ca862008-05-27 21:13:40 +0000387
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100388 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100389 rc = -EINVAL;
390
Nick Thomasb2e3d872011-02-22 15:44:51 +0000391 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100392 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000393 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200394 if (client->exp) {
395 assert ((client->exp->nbdflags & ~65535) == 0);
396 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
397 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
398 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
399 } else {
400 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
401 }
bellard7a5ca862008-05-27 21:13:40 +0000402
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200403 if (client->exp) {
404 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
405 LOG("write failed");
406 goto fail;
407 }
408 } else {
409 if (write_sync(csock, buf, 18) != 18) {
410 LOG("write failed");
411 goto fail;
412 }
413 rc = nbd_receive_options(client);
414 if (rc < 0) {
415 LOG("option negotiation failed");
416 goto fail;
417 }
418
419 assert ((client->exp->nbdflags & ~65535) == 0);
420 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
421 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
422 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
423 LOG("write failed");
424 goto fail;
425 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000426 }
bellard7a5ca862008-05-27 21:13:40 +0000427
Dong Xu Wang07f35072011-11-22 18:06:26 +0800428 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100429 rc = 0;
430fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100431 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100432 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000433}
434
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200435int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
436 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000437{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000438 char buf[256];
439 uint64_t magic, s;
440 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100441 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000442
Dong Xu Wang07f35072011-11-22 18:06:26 +0800443 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000444
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100445 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100446 rc = -EINVAL;
447
Nick Thomasb2e3d872011-02-22 15:44:51 +0000448 if (read_sync(csock, buf, 8) != 8) {
449 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100450 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000451 }
bellard7a5ca862008-05-27 21:13:40 +0000452
Nick Thomasb2e3d872011-02-22 15:44:51 +0000453 buf[8] = '\0';
454 if (strlen(buf) == 0) {
455 LOG("server connection closed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100456 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000457 }
bellard7a5ca862008-05-27 21:13:40 +0000458
Nick Thomasb2e3d872011-02-22 15:44:51 +0000459 TRACE("Magic is %c%c%c%c%c%c%c%c",
460 qemu_isprint(buf[0]) ? buf[0] : '.',
461 qemu_isprint(buf[1]) ? buf[1] : '.',
462 qemu_isprint(buf[2]) ? buf[2] : '.',
463 qemu_isprint(buf[3]) ? buf[3] : '.',
464 qemu_isprint(buf[4]) ? buf[4] : '.',
465 qemu_isprint(buf[5]) ? buf[5] : '.',
466 qemu_isprint(buf[6]) ? buf[6] : '.',
467 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000468
Nick Thomasb2e3d872011-02-22 15:44:51 +0000469 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
470 LOG("Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100471 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000472 }
bellard7a5ca862008-05-27 21:13:40 +0000473
Nick Thomasb2e3d872011-02-22 15:44:51 +0000474 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
475 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100476 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000477 }
478 magic = be64_to_cpu(magic);
479 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000480
Nick Thomasb2e3d872011-02-22 15:44:51 +0000481 if (name) {
482 uint32_t reserved = 0;
483 uint32_t opt;
484 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200485
Nick Thomasb2e3d872011-02-22 15:44:51 +0000486 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200487 if (magic != NBD_OPTS_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000488 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100489 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000490 }
491 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
492 LOG("flags read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100493 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000494 }
495 *flags = be16_to_cpu(tmp) << 16;
496 /* reserved for future use */
497 if (write_sync(csock, &reserved, sizeof(reserved)) !=
498 sizeof(reserved)) {
499 LOG("write failed (reserved)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100500 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000501 }
502 /* write the export name */
503 magic = cpu_to_be64(magic);
504 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
505 LOG("write failed (magic)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100506 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000507 }
508 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
509 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
510 LOG("write failed (opt)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100511 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000512 }
513 namesize = cpu_to_be32(strlen(name));
514 if (write_sync(csock, &namesize, sizeof(namesize)) !=
515 sizeof(namesize)) {
516 LOG("write failed (namesize)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100517 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000518 }
519 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
520 LOG("write failed (name)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100521 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000522 }
523 } else {
524 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200525
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200526 if (magic != NBD_CLIENT_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000527 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100528 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000529 }
530 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200531
Nick Thomasb2e3d872011-02-22 15:44:51 +0000532 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
533 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100534 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000535 }
536 *size = be64_to_cpu(s);
537 *blocksize = 1024;
538 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200539
Nick Thomasb2e3d872011-02-22 15:44:51 +0000540 if (!name) {
541 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
542 LOG("read failed (flags)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100543 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000544 }
545 *flags = be32_to_cpup(flags);
546 } else {
547 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
548 LOG("read failed (tmp)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100549 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000550 }
551 *flags |= be32_to_cpu(tmp);
552 }
553 if (read_sync(csock, &buf, 124) != 124) {
554 LOG("read failed (buf)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100555 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000556 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100557 rc = 0;
558
559fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100560 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100561 return rc;
thscd831bd2008-07-03 10:23:51 +0000562}
bellard7a5ca862008-05-27 21:13:40 +0000563
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200564#ifdef __linux__
565int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000566{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800567 TRACE("Setting NBD socket");
568
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100569 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800570 int serrno = errno;
571 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100572 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800573 }
574
Nick Thomasb2e3d872011-02-22 15:44:51 +0000575 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000576
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100577 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000578 int serrno = errno;
579 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100580 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000581 }
bellard7a5ca862008-05-27 21:13:40 +0000582
Blue Swirl0bfcd592010-05-22 08:02:12 +0000583 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000584
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100585 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000586 int serrno = errno;
587 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100588 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000589 }
bellard7a5ca862008-05-27 21:13:40 +0000590
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100591 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
592 if (errno == ENOTTY) {
593 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
594 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200595
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100596 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
597 int serrno = errno;
598 LOG("Failed setting read-only attribute");
599 return -serrno;
600 }
601 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200602 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100603 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100604 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200605 }
606 }
607
Nick Thomasb2e3d872011-02-22 15:44:51 +0000608 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000609
Nick Thomasb2e3d872011-02-22 15:44:51 +0000610 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000611}
612
613int nbd_disconnect(int fd)
614{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000615 ioctl(fd, NBD_CLEAR_QUE);
616 ioctl(fd, NBD_DISCONNECT);
617 ioctl(fd, NBD_CLEAR_SOCK);
618 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000619}
620
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200621int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000622{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000623 int ret;
624 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000625
Nick Thomasb2e3d872011-02-22 15:44:51 +0000626 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000627
Nick Thomasb2e3d872011-02-22 15:44:51 +0000628 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100629 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100630 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
631 * the socket via NBD_DISCONNECT. We do not want to return 1 in
632 * that case.
633 */
634 ret = 0;
635 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000636 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000637
Nick Thomasb2e3d872011-02-22 15:44:51 +0000638 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000639
Nick Thomasb2e3d872011-02-22 15:44:51 +0000640 TRACE("Clearing NBD queue");
641 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000642
Nick Thomasb2e3d872011-02-22 15:44:51 +0000643 TRACE("Clearing NBD socket");
644 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000645
Nick Thomasb2e3d872011-02-22 15:44:51 +0000646 errno = serrno;
647 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000648}
aliguori03ff3ca2008-09-15 15:51:35 +0000649#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200650int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000651{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100652 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000653}
654
655int nbd_disconnect(int fd)
656{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100657 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000658}
659
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200660int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000661{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100662 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000663}
664#endif
bellard7a5ca862008-05-27 21:13:40 +0000665
Paolo Bonzini94e73402012-03-07 11:25:01 +0100666ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000667{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200668 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100669 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000670
Nick Thomasb2e3d872011-02-22 15:44:51 +0000671 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
672 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
673 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
674 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
675 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000676
Nick Thomasb2e3d872011-02-22 15:44:51 +0000677 TRACE("Sending request to client: "
678 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
679 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000680
Paolo Bonzini185b4332012-03-05 08:56:10 +0100681 ret = write_sync(csock, buf, sizeof(buf));
682 if (ret < 0) {
683 return ret;
684 }
685
686 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000687 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100688 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000689 }
690 return 0;
ths75818252008-07-03 13:41:03 +0000691}
692
Paolo Bonzini94e73402012-03-07 11:25:01 +0100693static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000694{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200695 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000696 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100697 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000698
Paolo Bonzini185b4332012-03-05 08:56:10 +0100699 ret = read_sync(csock, buf, sizeof(buf));
700 if (ret < 0) {
701 return ret;
702 }
703
704 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000705 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100706 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000707 }
bellard7a5ca862008-05-27 21:13:40 +0000708
Nick Thomasb2e3d872011-02-22 15:44:51 +0000709 /* Request
710 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
711 [ 4 .. 7] type (0 == READ, 1 == WRITE)
712 [ 8 .. 15] handle
713 [16 .. 23] from
714 [24 .. 27] len
715 */
bellard7a5ca862008-05-27 21:13:40 +0000716
Nick Thomasb2e3d872011-02-22 15:44:51 +0000717 magic = be32_to_cpup((uint32_t*)buf);
718 request->type = be32_to_cpup((uint32_t*)(buf + 4));
719 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
720 request->from = be64_to_cpup((uint64_t*)(buf + 16));
721 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000722
Nick Thomasb2e3d872011-02-22 15:44:51 +0000723 TRACE("Got request: "
724 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
725 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000726
Nick Thomasb2e3d872011-02-22 15:44:51 +0000727 if (magic != NBD_REQUEST_MAGIC) {
728 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100729 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000730 }
731 return 0;
ths75818252008-07-03 13:41:03 +0000732}
bellard7a5ca862008-05-27 21:13:40 +0000733
Paolo Bonzini94e73402012-03-07 11:25:01 +0100734ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000735{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000736 uint8_t buf[NBD_REPLY_SIZE];
737 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100738 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000739
Paolo Bonzini185b4332012-03-05 08:56:10 +0100740 ret = read_sync(csock, buf, sizeof(buf));
741 if (ret < 0) {
742 return ret;
743 }
744
745 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000746 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100747 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000748 }
bellard7a5ca862008-05-27 21:13:40 +0000749
Nick Thomasb2e3d872011-02-22 15:44:51 +0000750 /* Reply
751 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
752 [ 4 .. 7] error (0 == no error)
753 [ 7 .. 15] handle
754 */
ths75818252008-07-03 13:41:03 +0000755
Nick Thomasb2e3d872011-02-22 15:44:51 +0000756 magic = be32_to_cpup((uint32_t*)buf);
757 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
758 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000759
Nick Thomasb2e3d872011-02-22 15:44:51 +0000760 TRACE("Got reply: "
761 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
762 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000763
Nick Thomasb2e3d872011-02-22 15:44:51 +0000764 if (magic != NBD_REPLY_MAGIC) {
765 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100766 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000767 }
768 return 0;
ths75818252008-07-03 13:41:03 +0000769}
770
Paolo Bonzini94e73402012-03-07 11:25:01 +0100771static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000772{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200773 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100774 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000775
Nick Thomasb2e3d872011-02-22 15:44:51 +0000776 /* Reply
777 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
778 [ 4 .. 7] error (0 == no error)
779 [ 7 .. 15] handle
780 */
781 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
782 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
783 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000784
Nick Thomasb2e3d872011-02-22 15:44:51 +0000785 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000786
Paolo Bonzini185b4332012-03-05 08:56:10 +0100787 ret = write_sync(csock, buf, sizeof(buf));
788 if (ret < 0) {
789 return ret;
790 }
791
792 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000793 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100794 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000795 }
796 return 0;
ths75818252008-07-03 13:41:03 +0000797}
798
Paolo Bonzini41996e32011-09-19 15:25:40 +0200799#define MAX_NBD_REQUESTS 16
800
Paolo Bonzinice339672012-09-18 13:17:52 +0200801void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200802{
803 client->refcount++;
804}
805
Paolo Bonzinice339672012-09-18 13:17:52 +0200806void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200807{
808 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200809 /* The last reference should be dropped by client->close,
810 * which is called by nbd_client_close.
811 */
812 assert(client->closing);
813
814 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
815 close(client->sock);
816 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200817 if (client->exp) {
818 QTAILQ_REMOVE(&client->exp->clients, client, next);
819 nbd_export_put(client->exp);
820 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200821 g_free(client);
822 }
823}
824
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200825void nbd_client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200826{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200827 if (client->closing) {
828 return;
829 }
830
831 client->closing = true;
832
833 /* Force requests to finish. They will drop their own references,
834 * then we'll close the socket and free the NBDClient.
835 */
836 shutdown(client->sock, 2);
837
838 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200839 if (client->close) {
840 client->close(client);
841 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200842}
843
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200844static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200845{
846 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200847
Paolo Bonzini41996e32011-09-19 15:25:40 +0200848 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
849 client->nb_requests++;
850
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200851 req = g_slice_new0(NBDRequest);
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200852 nbd_client_get(client);
853 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200854 return req;
855}
856
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200857static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200858{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200859 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200860
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200861 if (req->data) {
862 qemu_vfree(req->data);
863 }
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200864 g_slice_free(NBDRequest, req);
865
Paolo Bonzini41996e32011-09-19 15:25:40 +0200866 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
867 qemu_notify_event();
868 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200869 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200870}
871
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200872NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200873 off_t size, uint32_t nbdflags,
874 void (*close)(NBDExport *))
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200875{
876 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200877 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200878 QTAILQ_INIT(&exp->clients);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200879 exp->bs = bs;
880 exp->dev_offset = dev_offset;
881 exp->nbdflags = nbdflags;
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100882 exp->size = size == -1 ? bdrv_getlength(bs) : size;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200883 exp->close = close;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200884 return exp;
885}
886
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200887NBDExport *nbd_export_find(const char *name)
888{
889 NBDExport *exp;
890 QTAILQ_FOREACH(exp, &exports, next) {
891 if (strcmp(name, exp->name) == 0) {
892 return exp;
893 }
894 }
895
896 return NULL;
897}
898
899void nbd_export_set_name(NBDExport *exp, const char *name)
900{
901 if (exp->name == name) {
902 return;
903 }
904
905 nbd_export_get(exp);
906 if (exp->name != NULL) {
907 g_free(exp->name);
908 exp->name = NULL;
909 QTAILQ_REMOVE(&exports, exp, next);
910 nbd_export_put(exp);
911 }
912 if (name != NULL) {
913 nbd_export_get(exp);
914 exp->name = g_strdup(name);
915 QTAILQ_INSERT_TAIL(&exports, exp, next);
916 }
917 nbd_export_put(exp);
918}
919
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200920void nbd_export_close(NBDExport *exp)
921{
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200922 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200923
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200924 nbd_export_get(exp);
925 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
926 nbd_client_close(client);
927 }
Paolo Bonzini125afda2012-09-18 14:31:44 +0200928 nbd_export_set_name(exp, NULL);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200929 nbd_export_put(exp);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200930}
931
932void nbd_export_get(NBDExport *exp)
933{
934 assert(exp->refcount > 0);
935 exp->refcount++;
936}
937
938void nbd_export_put(NBDExport *exp)
939{
940 assert(exp->refcount > 0);
941 if (exp->refcount == 1) {
942 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200943 }
944
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200945 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200946 assert(exp->name == NULL);
947
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200948 if (exp->close) {
949 exp->close(exp);
950 }
951
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200952 g_free(exp);
953 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200954}
955
Paolo Bonzini125afda2012-09-18 14:31:44 +0200956BlockDriverState *nbd_export_get_blockdev(NBDExport *exp)
957{
958 return exp->bs;
959}
960
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200961void nbd_export_close_all(void)
962{
963 NBDExport *exp, *next;
964
965 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
966 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200967 }
968}
969
Paolo Bonzini41996e32011-09-19 15:25:40 +0200970static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +0200971static void nbd_read(void *opaque);
972static void nbd_restart_write(void *opaque);
973
Paolo Bonzini94e73402012-03-07 11:25:01 +0100974static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
975 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +0200976{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200977 NBDClient *client = req->client;
978 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100979 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +0200980
Paolo Bonzini262db382011-09-19 15:19:27 +0200981 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200982 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
983 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200984 client->send_coroutine = qemu_coroutine_self();
985
Paolo Bonzini22045592011-09-19 14:25:30 +0200986 if (!len) {
987 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +0200988 } else {
989 socket_set_cork(csock, 1);
990 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100991 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200992 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +0200993 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100994 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +0200995 }
996 }
Paolo Bonzini22045592011-09-19 14:25:30 +0200997 socket_set_cork(csock, 0);
998 }
Paolo Bonzini262db382011-09-19 15:19:27 +0200999
1000 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +02001001 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001002 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +02001003 return rc;
1004}
1005
Paolo Bonzini94e73402012-03-07 11:25:01 +01001006static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +02001007{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001008 NBDClient *client = req->client;
1009 int csock = client->sock;
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001010 uint32_t command;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001011 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001012
Paolo Bonzini262db382011-09-19 15:19:27 +02001013 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001014 rc = nbd_receive_request(csock, request);
1015 if (rc < 0) {
1016 if (rc != -EAGAIN) {
1017 rc = -EIO;
1018 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001019 goto out;
1020 }
1021
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001022 if (request->len > NBD_MAX_BUFFER_SIZE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001023 LOG("len (%u) is larger than max len (%u)",
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001024 request->len, NBD_MAX_BUFFER_SIZE);
Paolo Bonzinia030b342011-09-19 15:07:54 +02001025 rc = -EINVAL;
1026 goto out;
1027 }
1028
1029 if ((request->from + request->len) < request->from) {
1030 LOG("integer overflow detected! "
1031 "you're probably being attacked");
1032 rc = -EINVAL;
1033 goto out;
1034 }
1035
1036 TRACE("Decoding type");
1037
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001038 command = request->type & NBD_CMD_MASK_COMMAND;
1039 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
1040 req->data = qemu_blockalign(client->exp->bs, request->len);
1041 }
1042 if (command == NBD_CMD_WRITE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001043 TRACE("Reading %u byte(s)", request->len);
1044
Paolo Bonzini262db382011-09-19 15:19:27 +02001045 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001046 LOG("reading from socket failed");
1047 rc = -EIO;
1048 goto out;
1049 }
1050 }
1051 rc = 0;
1052
1053out:
Paolo Bonzini262db382011-09-19 15:19:27 +02001054 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001055 return rc;
1056}
1057
Paolo Bonzini262db382011-09-19 15:19:27 +02001058static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00001059{
Paolo Bonzini262db382011-09-19 15:19:27 +02001060 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001061 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001062 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001063 struct nbd_request request;
1064 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001065 ssize_t ret;
ths75818252008-07-03 13:41:03 +00001066
Nick Thomasb2e3d872011-02-22 15:44:51 +00001067 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001068 if (client->closing) {
1069 return;
1070 }
ths75818252008-07-03 13:41:03 +00001071
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001072 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001073 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001074 if (ret == -EAGAIN) {
1075 goto done;
1076 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001077 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001078 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001079 }
ths75818252008-07-03 13:41:03 +00001080
Paolo Bonzinifae69412011-09-19 16:04:36 +02001081 reply.handle = request.handle;
1082 reply.error = 0;
1083
Paolo Bonzinia030b342011-09-19 15:07:54 +02001084 if (ret < 0) {
1085 reply.error = -ret;
1086 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001087 }
bellard7a5ca862008-05-27 21:13:40 +00001088
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001089 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001090 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1091 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001092 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001093 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001094 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001095 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001096 }
bellard7a5ca862008-05-27 21:13:40 +00001097
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001098 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001099 case NBD_CMD_READ:
1100 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001101
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001102 if (request.type & NBD_CMD_FLAG_FUA) {
1103 ret = bdrv_co_flush(exp->bs);
1104 if (ret < 0) {
1105 LOG("flush failed");
1106 reply.error = -ret;
1107 goto error_reply;
1108 }
1109 }
1110
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001111 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001112 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001113 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001114 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001115 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001116 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001117 }
bellard7a5ca862008-05-27 21:13:40 +00001118
Nick Thomasb2e3d872011-02-22 15:44:51 +00001119 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001120 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001121 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001122 break;
1123 case NBD_CMD_WRITE:
1124 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001125
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001126 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001127 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001128 reply.error = EROFS;
1129 goto error_reply;
1130 }
bellard7a5ca862008-05-27 21:13:40 +00001131
Paolo Bonzinifae69412011-09-19 16:04:36 +02001132 TRACE("Writing to device");
1133
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001134 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001135 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001136 if (ret < 0) {
1137 LOG("writing to file failed");
1138 reply.error = -ret;
1139 goto error_reply;
1140 }
1141
1142 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001143 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001144 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001145 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001146 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001147 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001148 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001149 }
bellard7a5ca862008-05-27 21:13:40 +00001150
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001151 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001152 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001153 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001154 break;
1155 case NBD_CMD_DISC:
1156 TRACE("Request type is DISCONNECT");
1157 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001158 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001159 case NBD_CMD_FLUSH:
1160 TRACE("Request type is FLUSH");
1161
Paolo Bonzini262db382011-09-19 15:19:27 +02001162 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001163 if (ret < 0) {
1164 LOG("flush failed");
1165 reply.error = -ret;
1166 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001167 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001168 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001169 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001170 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001171 case NBD_CMD_TRIM:
1172 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +02001173 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
1174 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001175 if (ret < 0) {
1176 LOG("discard failed");
1177 reply.error = -ret;
1178 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001179 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001180 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001181 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001182 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001183 default:
1184 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001185 invalid_request:
1186 reply.error = -EINVAL;
1187 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001188 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001189 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001190 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001191 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001192 }
bellard7a5ca862008-05-27 21:13:40 +00001193
Nick Thomasb2e3d872011-02-22 15:44:51 +00001194 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001195
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001196done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001197 nbd_request_put(req);
1198 return;
1199
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001200out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001201 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +02001202 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001203}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001204
Paolo Bonzini41996e32011-09-19 15:25:40 +02001205static int nbd_can_read(void *opaque)
1206{
1207 NBDClient *client = opaque;
1208
1209 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
1210}
1211
Paolo Bonzini1743b512011-09-19 14:33:23 +02001212static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001213{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001214 NBDClient *client = opaque;
1215
Paolo Bonzini262db382011-09-19 15:19:27 +02001216 if (client->recv_coroutine) {
1217 qemu_coroutine_enter(client->recv_coroutine, NULL);
1218 } else {
1219 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001220 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001221}
1222
Paolo Bonzini262db382011-09-19 15:19:27 +02001223static void nbd_restart_write(void *opaque)
1224{
1225 NBDClient *client = opaque;
1226
1227 qemu_coroutine_enter(client->send_coroutine, NULL);
1228}
1229
Paolo Bonzini1743b512011-09-19 14:33:23 +02001230NBDClient *nbd_client_new(NBDExport *exp, int csock,
1231 void (*close)(NBDClient *))
1232{
1233 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001234 client = g_malloc0(sizeof(NBDClient));
1235 client->refcount = 1;
1236 client->exp = exp;
1237 client->sock = csock;
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001238 if (nbd_send_negotiate(client) < 0) {
1239 g_free(client);
1240 return NULL;
1241 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001242 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001243 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001244 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001245
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001246 if (exp) {
1247 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1248 nbd_export_get(exp);
1249 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001250 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001251}