blob: 030f56b5c7c51a34db1d1771ef75975d6506c208 [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"
Alex Bligh6a1751b2013-08-21 16:02:47 +010041#include "qemu/main-loop.h"
ths75818252008-07-03 13:41:03 +000042
aliguori03ff3ca2008-09-15 15:51:35 +000043//#define DEBUG_NBD
44
45#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000046#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000047 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000048} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000049#else
50#define TRACE(msg, ...) \
51 do { } while (0)
52#endif
bellard7a5ca862008-05-27 21:13:40 +000053
54#define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
57} while(0)
58
bellard7a5ca862008-05-27 21:13:40 +000059/* This is all part of the "official" NBD API */
60
Paolo Bonzinifa26c262012-08-22 15:13:30 +020061#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
Nick Thomasb2e3d872011-02-22 15:44:51 +000062#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000063#define NBD_REQUEST_MAGIC 0x25609513
64#define NBD_REPLY_MAGIC 0x67446698
Paolo Bonzinifa26c262012-08-22 15:13:30 +020065#define NBD_OPTS_MAGIC 0x49484156454F5054LL
66#define NBD_CLIENT_MAGIC 0x0000420281861253LL
bellard7a5ca862008-05-27 21:13:40 +000067
68#define NBD_SET_SOCK _IO(0xab, 0)
69#define NBD_SET_BLKSIZE _IO(0xab, 1)
70#define NBD_SET_SIZE _IO(0xab, 2)
71#define NBD_DO_IT _IO(0xab, 3)
72#define NBD_CLEAR_SOCK _IO(0xab, 4)
73#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000074#define NBD_PRINT_DEBUG _IO(0xab, 6)
75#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000076#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020077#define NBD_SET_TIMEOUT _IO(0xab, 9)
78#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000079
Nick Thomasb2e3d872011-02-22 15:44:51 +000080#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020081
Paolo Bonzini9a304d22012-08-22 15:30:31 +020082/* Definitions for opaque data types */
83
84typedef struct NBDRequest NBDRequest;
85
86struct NBDRequest {
87 QSIMPLEQ_ENTRY(NBDRequest) entry;
88 NBDClient *client;
89 uint8_t *data;
90};
91
92struct NBDExport {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +020093 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +020094 void (*close)(NBDExport *exp);
95
Paolo Bonzini9a304d22012-08-22 15:30:31 +020096 BlockDriverState *bs;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020097 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020098 off_t dev_offset;
99 off_t size;
100 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200101 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200102 QTAILQ_ENTRY(NBDExport) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200103};
104
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200105static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
106
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200107struct NBDClient {
108 int refcount;
109 void (*close)(NBDClient *client);
110
111 NBDExport *exp;
112 int sock;
113
114 Coroutine *recv_coroutine;
115
116 CoMutex send_lock;
117 Coroutine *send_coroutine;
118
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200119 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200120 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200121 bool closing;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200122};
123
bellard7a5ca862008-05-27 21:13:40 +0000124/* That's all folks */
125
Paolo Bonzini185b4332012-03-05 08:56:10 +0100126ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000127{
128 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100129 int err;
bellard7a5ca862008-05-27 21:13:40 +0000130
Paolo Bonziniae255e52011-09-08 14:28:59 +0200131 if (qemu_in_coroutine()) {
132 if (do_read) {
133 return qemu_co_recv(fd, buffer, size);
134 } else {
135 return qemu_co_send(fd, buffer, size);
136 }
137 }
138
bellard7a5ca862008-05-27 21:13:40 +0000139 while (offset < size) {
140 ssize_t len;
141
142 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000143 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000144 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000145 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000146 }
147
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100148 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100149 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000150
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100151 /* recoverable error */
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100152 if (err == EINTR || (offset > 0 && err == EAGAIN)) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100153 continue;
154 }
155
156 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100157 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000158 }
159
160 /* eof */
161 if (len == 0) {
162 break;
163 }
164
bellard7a5ca862008-05-27 21:13:40 +0000165 offset += len;
166 }
167
168 return offset;
169}
170
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100171static ssize_t read_sync(int fd, void *buffer, size_t size)
172{
173 /* Sockets are kept in blocking mode in the negotiation phase. After
174 * that, a non-readable socket simply means that another thread stole
175 * our request/reply. Synchronization is done with recv_coroutine, so
176 * that this is coroutine-safe.
177 */
178 return nbd_wr_sync(fd, buffer, size, true);
179}
180
181static ssize_t write_sync(int fd, void *buffer, size_t size)
182{
183 int ret;
184 do {
185 /* For writes, we do expect the socket to be writable. */
186 ret = nbd_wr_sync(fd, buffer, size, false);
187 } while (ret == -EAGAIN);
188 return ret;
189}
190
Nick Thomasc12504c2011-02-22 15:44:53 +0000191static void combine_addr(char *buf, size_t len, const char* address,
192 uint16_t port)
193{
194 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
195 if (strstr(address, ":")) {
196 snprintf(buf, len, "[%s]:%u", address, port);
197 } else {
198 snprintf(buf, len, "%s:%u", address, port);
199 }
200}
201
Kevin Wolff17c90b2013-03-15 11:55:29 +0100202int tcp_socket_outgoing_opts(QemuOpts *opts)
203{
204 Error *local_err = NULL;
205 int fd = inet_connect_opts(opts, &local_err, NULL, NULL);
206 if (local_err != NULL) {
207 qerror_report_err(local_err);
208 error_free(local_err);
209 }
210
211 return fd;
212}
213
bellard7a5ca862008-05-27 21:13:40 +0000214int tcp_socket_incoming(const char *address, uint16_t port)
215{
Nick Thomasc12504c2011-02-22 15:44:53 +0000216 char address_and_port[128];
217 combine_addr(address_and_port, 128, address, port);
218 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000219}
220
Nick Thomasc12504c2011-02-22 15:44:53 +0000221int tcp_socket_incoming_spec(const char *address_and_port)
222{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200223 Error *local_err = NULL;
224 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
225
226 if (local_err != NULL) {
227 qerror_report_err(local_err);
228 error_free(local_err);
229 }
230 return fd;
Nick Thomasc12504c2011-02-22 15:44:53 +0000231}
232
thscd831bd2008-07-03 10:23:51 +0000233int unix_socket_incoming(const char *path)
234{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200235 Error *local_err = NULL;
236 int fd = unix_listen(path, NULL, 0, &local_err);
thscd831bd2008-07-03 10:23:51 +0000237
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200238 if (local_err != NULL) {
239 qerror_report_err(local_err);
240 error_free(local_err);
241 }
242 return fd;
thscd831bd2008-07-03 10:23:51 +0000243}
244
245int unix_socket_outgoing(const char *path)
246{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200247 Error *local_err = NULL;
248 int fd = unix_connect(path, &local_err);
249
250 if (local_err != NULL) {
251 qerror_report_err(local_err);
252 error_free(local_err);
253 }
254 return fd;
thscd831bd2008-07-03 10:23:51 +0000255}
thscd831bd2008-07-03 10:23:51 +0000256
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200257/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000258
259 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000260 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200261
262 or
263
264 Server Client
265 Negotiate #1
266 Option
267 Negotiate #2
268
269 ----
270
271 followed by
272
273 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000274 Request
275 Response
276 Request
277 Response
278 ...
279 ...
280 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200281
bellard7a5ca862008-05-27 21:13:40 +0000282*/
283
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200284static int nbd_receive_options(NBDClient *client)
285{
286 int csock = client->sock;
287 char name[256];
288 uint32_t tmp, length;
289 uint64_t magic;
290 int rc;
291
292 /* Client sends:
293 [ 0 .. 3] reserved (0)
294 [ 4 .. 11] NBD_OPTS_MAGIC
295 [12 .. 15] NBD_OPT_EXPORT_NAME
296 [16 .. 19] length
297 [20 .. xx] export name (length bytes)
298 */
299
300 rc = -EINVAL;
301 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
302 LOG("read failed");
303 goto fail;
304 }
305 TRACE("Checking reserved");
306 if (tmp != 0) {
307 LOG("Bad reserved received");
308 goto fail;
309 }
310
311 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
312 LOG("read failed");
313 goto fail;
314 }
315 TRACE("Checking reserved");
316 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
317 LOG("Bad magic received");
318 goto fail;
319 }
320
321 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
322 LOG("read failed");
323 goto fail;
324 }
325 TRACE("Checking option");
326 if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
327 LOG("Bad option received");
328 goto fail;
329 }
330
331 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
332 LOG("read failed");
333 goto fail;
334 }
335 TRACE("Checking length");
336 length = be32_to_cpu(length);
337 if (length > 255) {
338 LOG("Bad length received");
339 goto fail;
340 }
341 if (read_sync(csock, name, length) != length) {
342 LOG("read failed");
343 goto fail;
344 }
345 name[length] = '\0';
346
347 client->exp = nbd_export_find(name);
348 if (!client->exp) {
349 LOG("export not found");
350 goto fail;
351 }
352
353 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
354 nbd_export_get(client->exp);
355
356 TRACE("Option negotiation succeeded.");
357 rc = 0;
358fail:
359 return rc;
360}
361
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200362static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000363{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200364 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000365 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100366 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200367 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
368 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000369
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200370 /* Negotiation header without options:
371 [ 0 .. 7] passwd ("NBDMAGIC")
372 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000373 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200374 [24 .. 25] server flags (0)
375 [24 .. 27] export flags
376 [28 .. 151] reserved (0)
377
378 Negotiation header with options, part 1:
379 [ 0 .. 7] passwd ("NBDMAGIC")
380 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
381 [16 .. 17] server flags (0)
382
383 part 2 (after options are sent):
384 [18 .. 25] size
385 [26 .. 27] export flags
386 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000387 */
bellard7a5ca862008-05-27 21:13:40 +0000388
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100389 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100390 rc = -EINVAL;
391
Nick Thomasb2e3d872011-02-22 15:44:51 +0000392 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100393 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000394 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200395 if (client->exp) {
396 assert ((client->exp->nbdflags & ~65535) == 0);
397 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
398 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
399 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
400 } else {
401 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
402 }
bellard7a5ca862008-05-27 21:13:40 +0000403
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200404 if (client->exp) {
405 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
406 LOG("write failed");
407 goto fail;
408 }
409 } else {
410 if (write_sync(csock, buf, 18) != 18) {
411 LOG("write failed");
412 goto fail;
413 }
414 rc = nbd_receive_options(client);
415 if (rc < 0) {
416 LOG("option negotiation failed");
417 goto fail;
418 }
419
420 assert ((client->exp->nbdflags & ~65535) == 0);
421 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
422 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
423 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
424 LOG("write failed");
425 goto fail;
426 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000427 }
bellard7a5ca862008-05-27 21:13:40 +0000428
Dong Xu Wang07f35072011-11-22 18:06:26 +0800429 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100430 rc = 0;
431fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100432 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100433 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000434}
435
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200436int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
437 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000438{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000439 char buf[256];
440 uint64_t magic, s;
441 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100442 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000443
Dong Xu Wang07f35072011-11-22 18:06:26 +0800444 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000445
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:
560 return rc;
thscd831bd2008-07-03 10:23:51 +0000561}
bellard7a5ca862008-05-27 21:13:40 +0000562
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200563#ifdef __linux__
564int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000565{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800566 TRACE("Setting NBD socket");
567
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100568 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800569 int serrno = errno;
570 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100571 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800572 }
573
Nick Thomasb2e3d872011-02-22 15:44:51 +0000574 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000575
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100576 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000577 int serrno = errno;
578 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100579 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000580 }
bellard7a5ca862008-05-27 21:13:40 +0000581
Blue Swirl0bfcd592010-05-22 08:02:12 +0000582 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000583
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100584 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000585 int serrno = errno;
586 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100587 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000588 }
bellard7a5ca862008-05-27 21:13:40 +0000589
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100590 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
591 if (errno == ENOTTY) {
592 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
593 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200594
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100595 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
596 int serrno = errno;
597 LOG("Failed setting read-only attribute");
598 return -serrno;
599 }
600 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200601 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100602 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100603 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200604 }
605 }
606
Nick Thomasb2e3d872011-02-22 15:44:51 +0000607 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000608
Nick Thomasb2e3d872011-02-22 15:44:51 +0000609 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000610}
611
612int nbd_disconnect(int fd)
613{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000614 ioctl(fd, NBD_CLEAR_QUE);
615 ioctl(fd, NBD_DISCONNECT);
616 ioctl(fd, NBD_CLEAR_SOCK);
617 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000618}
619
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200620int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000621{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000622 int ret;
623 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000624
Nick Thomasb2e3d872011-02-22 15:44:51 +0000625 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000626
Nick Thomasb2e3d872011-02-22 15:44:51 +0000627 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100628 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100629 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
630 * the socket via NBD_DISCONNECT. We do not want to return 1 in
631 * that case.
632 */
633 ret = 0;
634 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000635 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000636
Nick Thomasb2e3d872011-02-22 15:44:51 +0000637 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000638
Nick Thomasb2e3d872011-02-22 15:44:51 +0000639 TRACE("Clearing NBD queue");
640 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000641
Nick Thomasb2e3d872011-02-22 15:44:51 +0000642 TRACE("Clearing NBD socket");
643 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000644
Nick Thomasb2e3d872011-02-22 15:44:51 +0000645 errno = serrno;
646 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000647}
aliguori03ff3ca2008-09-15 15:51:35 +0000648#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200649int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000650{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100651 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000652}
653
654int nbd_disconnect(int fd)
655{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100656 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000657}
658
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200659int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000660{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100661 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000662}
663#endif
bellard7a5ca862008-05-27 21:13:40 +0000664
Paolo Bonzini94e73402012-03-07 11:25:01 +0100665ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000666{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200667 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100668 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000669
Nick Thomasb2e3d872011-02-22 15:44:51 +0000670 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
671 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
672 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
673 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
674 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000675
Nick Thomasb2e3d872011-02-22 15:44:51 +0000676 TRACE("Sending request to client: "
677 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
678 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000679
Paolo Bonzini185b4332012-03-05 08:56:10 +0100680 ret = write_sync(csock, buf, sizeof(buf));
681 if (ret < 0) {
682 return ret;
683 }
684
685 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000686 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100687 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000688 }
689 return 0;
ths75818252008-07-03 13:41:03 +0000690}
691
Paolo Bonzini94e73402012-03-07 11:25:01 +0100692static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000693{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200694 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000695 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100696 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000697
Paolo Bonzini185b4332012-03-05 08:56:10 +0100698 ret = read_sync(csock, buf, sizeof(buf));
699 if (ret < 0) {
700 return ret;
701 }
702
703 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000704 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100705 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000706 }
bellard7a5ca862008-05-27 21:13:40 +0000707
Nick Thomasb2e3d872011-02-22 15:44:51 +0000708 /* Request
709 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
710 [ 4 .. 7] type (0 == READ, 1 == WRITE)
711 [ 8 .. 15] handle
712 [16 .. 23] from
713 [24 .. 27] len
714 */
bellard7a5ca862008-05-27 21:13:40 +0000715
Nick Thomasb2e3d872011-02-22 15:44:51 +0000716 magic = be32_to_cpup((uint32_t*)buf);
717 request->type = be32_to_cpup((uint32_t*)(buf + 4));
718 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
719 request->from = be64_to_cpup((uint64_t*)(buf + 16));
720 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000721
Nick Thomasb2e3d872011-02-22 15:44:51 +0000722 TRACE("Got request: "
723 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
724 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000725
Nick Thomasb2e3d872011-02-22 15:44:51 +0000726 if (magic != NBD_REQUEST_MAGIC) {
727 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100728 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000729 }
730 return 0;
ths75818252008-07-03 13:41:03 +0000731}
bellard7a5ca862008-05-27 21:13:40 +0000732
Paolo Bonzini94e73402012-03-07 11:25:01 +0100733ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000734{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000735 uint8_t buf[NBD_REPLY_SIZE];
736 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100737 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000738
Paolo Bonzini185b4332012-03-05 08:56:10 +0100739 ret = read_sync(csock, buf, sizeof(buf));
740 if (ret < 0) {
741 return ret;
742 }
743
744 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000745 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100746 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000747 }
bellard7a5ca862008-05-27 21:13:40 +0000748
Nick Thomasb2e3d872011-02-22 15:44:51 +0000749 /* Reply
750 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
751 [ 4 .. 7] error (0 == no error)
752 [ 7 .. 15] handle
753 */
ths75818252008-07-03 13:41:03 +0000754
Nick Thomasb2e3d872011-02-22 15:44:51 +0000755 magic = be32_to_cpup((uint32_t*)buf);
756 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
757 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000758
Nick Thomasb2e3d872011-02-22 15:44:51 +0000759 TRACE("Got reply: "
760 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
761 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000762
Nick Thomasb2e3d872011-02-22 15:44:51 +0000763 if (magic != NBD_REPLY_MAGIC) {
764 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100765 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000766 }
767 return 0;
ths75818252008-07-03 13:41:03 +0000768}
769
Paolo Bonzini94e73402012-03-07 11:25:01 +0100770static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000771{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200772 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100773 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000774
Nick Thomasb2e3d872011-02-22 15:44:51 +0000775 /* Reply
776 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
777 [ 4 .. 7] error (0 == no error)
778 [ 7 .. 15] handle
779 */
780 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
781 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
782 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000783
Nick Thomasb2e3d872011-02-22 15:44:51 +0000784 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000785
Paolo Bonzini185b4332012-03-05 08:56:10 +0100786 ret = write_sync(csock, buf, sizeof(buf));
787 if (ret < 0) {
788 return ret;
789 }
790
791 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000792 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100793 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000794 }
795 return 0;
ths75818252008-07-03 13:41:03 +0000796}
797
Paolo Bonzini41996e32011-09-19 15:25:40 +0200798#define MAX_NBD_REQUESTS 16
799
Paolo Bonzinice339672012-09-18 13:17:52 +0200800void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200801{
802 client->refcount++;
803}
804
Paolo Bonzinice339672012-09-18 13:17:52 +0200805void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200806{
807 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200808 /* The last reference should be dropped by client->close,
809 * which is called by nbd_client_close.
810 */
811 assert(client->closing);
812
813 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
814 close(client->sock);
815 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200816 if (client->exp) {
817 QTAILQ_REMOVE(&client->exp->clients, client, next);
818 nbd_export_put(client->exp);
819 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200820 g_free(client);
821 }
822}
823
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200824void nbd_client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200825{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200826 if (client->closing) {
827 return;
828 }
829
830 client->closing = true;
831
832 /* Force requests to finish. They will drop their own references,
833 * then we'll close the socket and free the NBDClient.
834 */
835 shutdown(client->sock, 2);
836
837 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200838 if (client->close) {
839 client->close(client);
840 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200841}
842
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200843static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200844{
845 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200846
Paolo Bonzini41996e32011-09-19 15:25:40 +0200847 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
848 client->nb_requests++;
849
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200850 req = g_slice_new0(NBDRequest);
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200851 nbd_client_get(client);
852 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200853 return req;
854}
855
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200856static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200857{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200858 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200859
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200860 if (req->data) {
861 qemu_vfree(req->data);
862 }
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200863 g_slice_free(NBDRequest, req);
864
Paolo Bonzini41996e32011-09-19 15:25:40 +0200865 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
866 qemu_notify_event();
867 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200868 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200869}
870
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200871NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200872 off_t size, uint32_t nbdflags,
873 void (*close)(NBDExport *))
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200874{
875 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200876 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200877 QTAILQ_INIT(&exp->clients);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200878 exp->bs = bs;
879 exp->dev_offset = dev_offset;
880 exp->nbdflags = nbdflags;
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100881 exp->size = size == -1 ? bdrv_getlength(bs) : size;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200882 exp->close = close;
Fam Zheng38b54b62013-08-23 09:14:50 +0800883 bdrv_ref(bs);
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);
Fam Zheng38b54b62013-08-23 09:14:50 +0800930 if (exp->bs) {
931 bdrv_unref(exp->bs);
932 exp->bs = NULL;
933 }
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200934}
935
936void nbd_export_get(NBDExport *exp)
937{
938 assert(exp->refcount > 0);
939 exp->refcount++;
940}
941
942void nbd_export_put(NBDExport *exp)
943{
944 assert(exp->refcount > 0);
945 if (exp->refcount == 1) {
946 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200947 }
948
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200949 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200950 assert(exp->name == NULL);
951
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200952 if (exp->close) {
953 exp->close(exp);
954 }
955
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200956 g_free(exp);
957 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200958}
959
Paolo Bonzini125afda2012-09-18 14:31:44 +0200960BlockDriverState *nbd_export_get_blockdev(NBDExport *exp)
961{
962 return exp->bs;
963}
964
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200965void nbd_export_close_all(void)
966{
967 NBDExport *exp, *next;
968
969 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
970 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200971 }
972}
973
Paolo Bonzini41996e32011-09-19 15:25:40 +0200974static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +0200975static void nbd_read(void *opaque);
976static void nbd_restart_write(void *opaque);
977
Paolo Bonzini94e73402012-03-07 11:25:01 +0100978static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
979 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +0200980{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200981 NBDClient *client = req->client;
982 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100983 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +0200984
Paolo Bonzini262db382011-09-19 15:19:27 +0200985 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200986 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
987 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200988 client->send_coroutine = qemu_coroutine_self();
989
Paolo Bonzini22045592011-09-19 14:25:30 +0200990 if (!len) {
991 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +0200992 } else {
993 socket_set_cork(csock, 1);
994 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100995 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200996 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +0200997 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100998 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +0200999 }
1000 }
Paolo Bonzini22045592011-09-19 14:25:30 +02001001 socket_set_cork(csock, 0);
1002 }
Paolo Bonzini262db382011-09-19 15:19:27 +02001003
1004 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +02001005 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001006 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +02001007 return rc;
1008}
1009
Paolo Bonzini94e73402012-03-07 11:25:01 +01001010static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +02001011{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001012 NBDClient *client = req->client;
1013 int csock = client->sock;
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001014 uint32_t command;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001015 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001016
Paolo Bonzini262db382011-09-19 15:19:27 +02001017 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001018 rc = nbd_receive_request(csock, request);
1019 if (rc < 0) {
1020 if (rc != -EAGAIN) {
1021 rc = -EIO;
1022 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001023 goto out;
1024 }
1025
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001026 if (request->len > NBD_MAX_BUFFER_SIZE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001027 LOG("len (%u) is larger than max len (%u)",
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001028 request->len, NBD_MAX_BUFFER_SIZE);
Paolo Bonzinia030b342011-09-19 15:07:54 +02001029 rc = -EINVAL;
1030 goto out;
1031 }
1032
1033 if ((request->from + request->len) < request->from) {
1034 LOG("integer overflow detected! "
1035 "you're probably being attacked");
1036 rc = -EINVAL;
1037 goto out;
1038 }
1039
1040 TRACE("Decoding type");
1041
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001042 command = request->type & NBD_CMD_MASK_COMMAND;
1043 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
1044 req->data = qemu_blockalign(client->exp->bs, request->len);
1045 }
1046 if (command == NBD_CMD_WRITE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001047 TRACE("Reading %u byte(s)", request->len);
1048
Paolo Bonzini262db382011-09-19 15:19:27 +02001049 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001050 LOG("reading from socket failed");
1051 rc = -EIO;
1052 goto out;
1053 }
1054 }
1055 rc = 0;
1056
1057out:
Paolo Bonzini262db382011-09-19 15:19:27 +02001058 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001059 return rc;
1060}
1061
Paolo Bonzini262db382011-09-19 15:19:27 +02001062static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00001063{
Paolo Bonzini262db382011-09-19 15:19:27 +02001064 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001065 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001066 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001067 struct nbd_request request;
1068 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001069 ssize_t ret;
ths75818252008-07-03 13:41:03 +00001070
Nick Thomasb2e3d872011-02-22 15:44:51 +00001071 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001072 if (client->closing) {
1073 return;
1074 }
ths75818252008-07-03 13:41:03 +00001075
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001076 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001077 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001078 if (ret == -EAGAIN) {
1079 goto done;
1080 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001081 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001082 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001083 }
ths75818252008-07-03 13:41:03 +00001084
Paolo Bonzinifae69412011-09-19 16:04:36 +02001085 reply.handle = request.handle;
1086 reply.error = 0;
1087
Paolo Bonzinia030b342011-09-19 15:07:54 +02001088 if (ret < 0) {
1089 reply.error = -ret;
1090 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001091 }
bellard7a5ca862008-05-27 21:13:40 +00001092
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001093 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001094 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1095 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001096 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001097 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001098 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001099 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001100 }
bellard7a5ca862008-05-27 21:13:40 +00001101
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001102 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001103 case NBD_CMD_READ:
1104 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001105
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001106 if (request.type & NBD_CMD_FLAG_FUA) {
1107 ret = bdrv_co_flush(exp->bs);
1108 if (ret < 0) {
1109 LOG("flush failed");
1110 reply.error = -ret;
1111 goto error_reply;
1112 }
1113 }
1114
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001115 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001116 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001117 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001118 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001119 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001120 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001121 }
bellard7a5ca862008-05-27 21:13:40 +00001122
Nick Thomasb2e3d872011-02-22 15:44:51 +00001123 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001124 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001125 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001126 break;
1127 case NBD_CMD_WRITE:
1128 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001129
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001130 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001131 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001132 reply.error = EROFS;
1133 goto error_reply;
1134 }
bellard7a5ca862008-05-27 21:13:40 +00001135
Paolo Bonzinifae69412011-09-19 16:04:36 +02001136 TRACE("Writing to device");
1137
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001138 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001139 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001140 if (ret < 0) {
1141 LOG("writing to file failed");
1142 reply.error = -ret;
1143 goto error_reply;
1144 }
1145
1146 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001147 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001148 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001149 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001150 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001151 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001152 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001153 }
bellard7a5ca862008-05-27 21:13:40 +00001154
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001155 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001156 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001157 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001158 break;
1159 case NBD_CMD_DISC:
1160 TRACE("Request type is DISCONNECT");
1161 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001162 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001163 case NBD_CMD_FLUSH:
1164 TRACE("Request type is FLUSH");
1165
Paolo Bonzini262db382011-09-19 15:19:27 +02001166 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001167 if (ret < 0) {
1168 LOG("flush failed");
1169 reply.error = -ret;
1170 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001171 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001172 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001173 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001174 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001175 case NBD_CMD_TRIM:
1176 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +02001177 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
1178 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001179 if (ret < 0) {
1180 LOG("discard failed");
1181 reply.error = -ret;
1182 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001183 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001184 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001185 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001186 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001187 default:
1188 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001189 invalid_request:
1190 reply.error = -EINVAL;
1191 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001192 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001193 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001194 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001195 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001196 }
bellard7a5ca862008-05-27 21:13:40 +00001197
Nick Thomasb2e3d872011-02-22 15:44:51 +00001198 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001199
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001200done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001201 nbd_request_put(req);
1202 return;
1203
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001204out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001205 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +02001206 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001207}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001208
Paolo Bonzini41996e32011-09-19 15:25:40 +02001209static int nbd_can_read(void *opaque)
1210{
1211 NBDClient *client = opaque;
1212
1213 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
1214}
1215
Paolo Bonzini1743b512011-09-19 14:33:23 +02001216static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001217{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001218 NBDClient *client = opaque;
1219
Paolo Bonzini262db382011-09-19 15:19:27 +02001220 if (client->recv_coroutine) {
1221 qemu_coroutine_enter(client->recv_coroutine, NULL);
1222 } else {
1223 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001224 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001225}
1226
Paolo Bonzini262db382011-09-19 15:19:27 +02001227static void nbd_restart_write(void *opaque)
1228{
1229 NBDClient *client = opaque;
1230
1231 qemu_coroutine_enter(client->send_coroutine, NULL);
1232}
1233
Paolo Bonzini1743b512011-09-19 14:33:23 +02001234NBDClient *nbd_client_new(NBDExport *exp, int csock,
1235 void (*close)(NBDClient *))
1236{
1237 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001238 client = g_malloc0(sizeof(NBDClient));
1239 client->refcount = 1;
1240 client->exp = exp;
1241 client->sock = csock;
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001242 if (nbd_send_negotiate(client) < 0) {
1243 g_free(client);
1244 return NULL;
1245 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001246 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001247 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001248 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001249
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001250 if (exp) {
1251 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1252 nbd_export_get(exp);
1253 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001254 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001255}