blob: b3d9654499ca01e4125fdb0d0ab0963945fe03a3 [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"
Max Reitze1401772014-11-18 12:21:17 +010020#include "sysemu/block-backend.h"
bellard7a5ca862008-05-27 21:13:40 +000021
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010022#include "qemu/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
Hani Benhabilesf5076b52014-06-07 01:32:31 +010059/* This is all part of the "official" NBD API.
60 *
61 * The most up-to-date documentation is available at:
62 * https://github.com/yoe/nbd/blob/master/doc/proto.txt
63 */
bellard7a5ca862008-05-27 21:13:40 +000064
Paolo Bonzinifa26c262012-08-22 15:13:30 +020065#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
Nick Thomasb2e3d872011-02-22 15:44:51 +000066#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000067#define NBD_REQUEST_MAGIC 0x25609513
68#define NBD_REPLY_MAGIC 0x67446698
Paolo Bonzinifa26c262012-08-22 15:13:30 +020069#define NBD_OPTS_MAGIC 0x49484156454F5054LL
70#define NBD_CLIENT_MAGIC 0x0000420281861253LL
Hani Benhabilesf5076b52014-06-07 01:32:31 +010071#define NBD_REP_MAGIC 0x3e889045565a9LL
bellard7a5ca862008-05-27 21:13:40 +000072
73#define NBD_SET_SOCK _IO(0xab, 0)
74#define NBD_SET_BLKSIZE _IO(0xab, 1)
75#define NBD_SET_SIZE _IO(0xab, 2)
76#define NBD_DO_IT _IO(0xab, 3)
77#define NBD_CLEAR_SOCK _IO(0xab, 4)
78#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000079#define NBD_PRINT_DEBUG _IO(0xab, 6)
80#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000081#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020082#define NBD_SET_TIMEOUT _IO(0xab, 9)
83#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000084
Hani Benhabilesf5076b52014-06-07 01:32:31 +010085#define NBD_OPT_EXPORT_NAME (1)
86#define NBD_OPT_ABORT (2)
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +010087#define NBD_OPT_LIST (3)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020088
Paolo Bonzinica441482015-05-07 17:25:10 +020089/* NBD errors are based on errno numbers, so there is a 1:1 mapping,
90 * but only a limited set of errno values is specified in the protocol.
91 * Everything else is squashed to EINVAL.
92 */
93#define NBD_SUCCESS 0
94#define NBD_EPERM 1
95#define NBD_EIO 5
96#define NBD_ENOMEM 12
97#define NBD_EINVAL 22
98#define NBD_ENOSPC 28
99
100static int system_errno_to_nbd_errno(int err)
101{
102 switch (err) {
103 case 0:
104 return NBD_SUCCESS;
105 case EPERM:
106 return NBD_EPERM;
107 case EIO:
108 return NBD_EIO;
109 case ENOMEM:
110 return NBD_ENOMEM;
111#ifdef EDQUOT
112 case EDQUOT:
113#endif
114 case EFBIG:
115 case ENOSPC:
116 return NBD_ENOSPC;
117 case EINVAL:
118 default:
119 return NBD_EINVAL;
120 }
121}
122
123static int nbd_errno_to_system_errno(int err)
124{
125 switch (err) {
126 case NBD_SUCCESS:
127 return 0;
128 case NBD_EPERM:
129 return EPERM;
130 case NBD_EIO:
131 return EIO;
132 case NBD_ENOMEM:
133 return ENOMEM;
134 case NBD_ENOSPC:
135 return ENOSPC;
136 case NBD_EINVAL:
137 default:
138 return EINVAL;
139 }
140}
141
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200142/* Definitions for opaque data types */
143
144typedef struct NBDRequest NBDRequest;
145
146struct NBDRequest {
147 QSIMPLEQ_ENTRY(NBDRequest) entry;
148 NBDClient *client;
149 uint8_t *data;
150};
151
152struct NBDExport {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200153 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200154 void (*close)(NBDExport *exp);
155
Max Reitzaadf99a2014-11-18 12:21:18 +0100156 BlockBackend *blk;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200157 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200158 off_t dev_offset;
159 off_t size;
160 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200161 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200162 QTAILQ_ENTRY(NBDExport) next;
Max Reitz958c7172014-06-20 21:57:32 +0200163
164 AioContext *ctx;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200165};
166
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200167static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
168
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200169struct NBDClient {
170 int refcount;
171 void (*close)(NBDClient *client);
172
173 NBDExport *exp;
174 int sock;
175
176 Coroutine *recv_coroutine;
177
178 CoMutex send_lock;
179 Coroutine *send_coroutine;
180
Max Reitz958c7172014-06-20 21:57:32 +0200181 bool can_read;
182
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200183 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200184 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200185 bool closing;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200186};
187
bellard7a5ca862008-05-27 21:13:40 +0000188/* That's all folks */
189
Max Reitz958c7172014-06-20 21:57:32 +0200190static void nbd_set_handlers(NBDClient *client);
191static void nbd_unset_handlers(NBDClient *client);
192static void nbd_update_can_read(NBDClient *client);
193
Paolo Bonzini185b4332012-03-05 08:56:10 +0100194ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000195{
196 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100197 int err;
bellard7a5ca862008-05-27 21:13:40 +0000198
Paolo Bonziniae255e52011-09-08 14:28:59 +0200199 if (qemu_in_coroutine()) {
200 if (do_read) {
201 return qemu_co_recv(fd, buffer, size);
202 } else {
203 return qemu_co_send(fd, buffer, size);
204 }
205 }
206
bellard7a5ca862008-05-27 21:13:40 +0000207 while (offset < size) {
208 ssize_t len;
209
210 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000211 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000212 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000213 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000214 }
215
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100216 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100217 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000218
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100219 /* recoverable error */
Paolo Bonzini79d9b652014-07-09 11:53:09 +0200220 if (err == EINTR || (offset > 0 && (err == EAGAIN || err == EWOULDBLOCK))) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100221 continue;
222 }
223
224 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100225 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000226 }
227
228 /* eof */
229 if (len == 0) {
230 break;
231 }
232
bellard7a5ca862008-05-27 21:13:40 +0000233 offset += len;
234 }
235
236 return offset;
237}
238
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100239static ssize_t read_sync(int fd, void *buffer, size_t size)
240{
241 /* Sockets are kept in blocking mode in the negotiation phase. After
242 * that, a non-readable socket simply means that another thread stole
243 * our request/reply. Synchronization is done with recv_coroutine, so
244 * that this is coroutine-safe.
245 */
246 return nbd_wr_sync(fd, buffer, size, true);
247}
248
Max Reitz0379f472015-02-25 13:08:34 -0500249static ssize_t drop_sync(int fd, size_t size)
250{
251 ssize_t ret, dropped = size;
252 uint8_t *buffer = g_malloc(MIN(65536, size));
253
254 while (size > 0) {
255 ret = read_sync(fd, buffer, MIN(65536, size));
256 if (ret < 0) {
257 g_free(buffer);
258 return ret;
259 }
260
261 assert(ret <= size);
262 size -= ret;
263 }
264
265 g_free(buffer);
266 return dropped;
267}
268
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100269static ssize_t write_sync(int fd, void *buffer, size_t size)
270{
271 int ret;
272 do {
273 /* For writes, we do expect the socket to be writable. */
274 ret = nbd_wr_sync(fd, buffer, size, false);
275 } while (ret == -EAGAIN);
276 return ret;
277}
278
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200279/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000280
281 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000282 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200283
284 or
285
286 Server Client
287 Negotiate #1
288 Option
289 Negotiate #2
290
291 ----
292
293 followed by
294
295 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000296 Request
297 Response
298 Request
299 Response
300 ...
301 ...
302 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200303
bellard7a5ca862008-05-27 21:13:40 +0000304*/
305
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100306static int nbd_send_rep(int csock, uint32_t type, uint32_t opt)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200307{
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200308 uint64_t magic;
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100309 uint32_t len;
310
311 magic = cpu_to_be64(NBD_REP_MAGIC);
312 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
313 LOG("write failed (rep magic)");
314 return -EINVAL;
315 }
316 opt = cpu_to_be32(opt);
317 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
318 LOG("write failed (rep opt)");
319 return -EINVAL;
320 }
321 type = cpu_to_be32(type);
322 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
323 LOG("write failed (rep type)");
324 return -EINVAL;
325 }
326 len = cpu_to_be32(0);
327 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
328 LOG("write failed (rep data length)");
329 return -EINVAL;
330 }
331 return 0;
332}
333
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100334static int nbd_send_rep_list(int csock, NBDExport *exp)
335{
336 uint64_t magic, name_len;
337 uint32_t opt, type, len;
338
339 name_len = strlen(exp->name);
340 magic = cpu_to_be64(NBD_REP_MAGIC);
341 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
342 LOG("write failed (magic)");
343 return -EINVAL;
344 }
345 opt = cpu_to_be32(NBD_OPT_LIST);
346 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
347 LOG("write failed (opt)");
348 return -EINVAL;
349 }
350 type = cpu_to_be32(NBD_REP_SERVER);
351 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
352 LOG("write failed (reply type)");
353 return -EINVAL;
354 }
355 len = cpu_to_be32(name_len + sizeof(len));
356 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
357 LOG("write failed (length)");
358 return -EINVAL;
359 }
360 len = cpu_to_be32(name_len);
361 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
362 LOG("write failed (length)");
363 return -EINVAL;
364 }
365 if (write_sync(csock, exp->name, name_len) != name_len) {
366 LOG("write failed (buffer)");
367 return -EINVAL;
368 }
369 return 0;
370}
371
372static int nbd_handle_list(NBDClient *client, uint32_t length)
373{
374 int csock;
375 NBDExport *exp;
376
377 csock = client->sock;
378 if (length) {
Max Reitz0379f472015-02-25 13:08:34 -0500379 if (drop_sync(csock, length) != length) {
380 return -EIO;
381 }
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100382 return nbd_send_rep(csock, NBD_REP_ERR_INVALID, NBD_OPT_LIST);
383 }
384
385 /* For each export, send a NBD_REP_SERVER reply. */
386 QTAILQ_FOREACH(exp, &exports, next) {
387 if (nbd_send_rep_list(csock, exp)) {
388 return -EINVAL;
389 }
390 }
391 /* Finish with a NBD_REP_ACK. */
392 return nbd_send_rep(csock, NBD_REP_ACK, NBD_OPT_LIST);
393}
394
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100395static int nbd_handle_export_name(NBDClient *client, uint32_t length)
396{
397 int rc = -EINVAL, csock = client->sock;
398 char name[256];
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200399
400 /* Client sends:
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200401 [20 .. xx] export name (length bytes)
402 */
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200403 TRACE("Checking length");
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200404 if (length > 255) {
405 LOG("Bad length received");
406 goto fail;
407 }
408 if (read_sync(csock, name, length) != length) {
409 LOG("read failed");
410 goto fail;
411 }
412 name[length] = '\0';
413
414 client->exp = nbd_export_find(name);
415 if (!client->exp) {
416 LOG("export not found");
417 goto fail;
418 }
419
420 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
421 nbd_export_get(client->exp);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200422 rc = 0;
423fail:
424 return rc;
425}
426
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100427static int nbd_receive_options(NBDClient *client)
428{
Max Reitz9c122ad2015-02-25 13:08:31 -0500429 int csock = client->sock;
430 uint32_t flags;
431
432 /* Client sends:
433 [ 0 .. 3] client flags
434
435 [ 0 .. 7] NBD_OPTS_MAGIC
436 [ 8 .. 11] NBD option
437 [12 .. 15] Data length
438 ... Rest of request
439
440 [ 0 .. 7] NBD_OPTS_MAGIC
441 [ 8 .. 11] Second NBD option
442 [12 .. 15] Data length
443 ... Rest of request
444 */
445
446 if (read_sync(csock, &flags, sizeof(flags)) != sizeof(flags)) {
447 LOG("read failed");
448 return -EIO;
449 }
450 TRACE("Checking client flags");
451 be32_to_cpus(&flags);
452 if (flags != 0 && flags != NBD_FLAG_C_FIXED_NEWSTYLE) {
453 LOG("Bad client flags received");
454 return -EIO;
455 }
456
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100457 while (1) {
Max Reitz9c122ad2015-02-25 13:08:31 -0500458 int ret;
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100459 uint32_t tmp, length;
460 uint64_t magic;
461
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100462 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
463 LOG("read failed");
464 return -EINVAL;
465 }
466 TRACE("Checking opts magic");
467 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
468 LOG("Bad magic received");
469 return -EINVAL;
470 }
471
472 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
473 LOG("read failed");
474 return -EINVAL;
475 }
476
477 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
478 LOG("read failed");
479 return -EINVAL;
480 }
481 length = be32_to_cpu(length);
482
483 TRACE("Checking option");
484 switch (be32_to_cpu(tmp)) {
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100485 case NBD_OPT_LIST:
Max Reitz892f5a52015-02-25 13:08:19 -0500486 ret = nbd_handle_list(client, length);
487 if (ret < 0) {
488 return ret;
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100489 }
490 break;
491
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100492 case NBD_OPT_ABORT:
493 return -EINVAL;
494
495 case NBD_OPT_EXPORT_NAME:
496 return nbd_handle_export_name(client, length);
497
498 default:
499 tmp = be32_to_cpu(tmp);
500 LOG("Unsupported option 0x%x", tmp);
501 nbd_send_rep(client->sock, NBD_REP_ERR_UNSUP, tmp);
502 return -EINVAL;
503 }
504 }
505}
506
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200507static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000508{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200509 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000510 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100511 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200512 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
513 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000514
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200515 /* Negotiation header without options:
516 [ 0 .. 7] passwd ("NBDMAGIC")
517 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000518 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200519 [24 .. 25] server flags (0)
Hani Benhabiles5672ee52014-05-13 00:35:16 +0100520 [26 .. 27] export flags
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200521 [28 .. 151] reserved (0)
522
523 Negotiation header with options, part 1:
524 [ 0 .. 7] passwd ("NBDMAGIC")
525 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
526 [16 .. 17] server flags (0)
527
528 part 2 (after options are sent):
529 [18 .. 25] size
530 [26 .. 27] export flags
531 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000532 */
bellard7a5ca862008-05-27 21:13:40 +0000533
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100534 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100535 rc = -EINVAL;
536
Nick Thomasb2e3d872011-02-22 15:44:51 +0000537 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100538 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000539 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200540 if (client->exp) {
541 assert ((client->exp->nbdflags & ~65535) == 0);
542 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
543 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
544 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
545 } else {
546 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100547 cpu_to_be16w((uint16_t *)(buf + 16), NBD_FLAG_FIXED_NEWSTYLE);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200548 }
bellard7a5ca862008-05-27 21:13:40 +0000549
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200550 if (client->exp) {
551 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
552 LOG("write failed");
553 goto fail;
554 }
555 } else {
556 if (write_sync(csock, buf, 18) != 18) {
557 LOG("write failed");
558 goto fail;
559 }
560 rc = nbd_receive_options(client);
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100561 if (rc != 0) {
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200562 LOG("option negotiation failed");
563 goto fail;
564 }
565
566 assert ((client->exp->nbdflags & ~65535) == 0);
567 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
568 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
569 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
570 LOG("write failed");
571 goto fail;
572 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000573 }
bellard7a5ca862008-05-27 21:13:40 +0000574
Dong Xu Wang07f35072011-11-22 18:06:26 +0800575 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100576 rc = 0;
577fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100578 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100579 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000580}
581
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200582int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
Max Reitz3f472652015-02-25 13:08:25 -0500583 off_t *size, Error **errp)
bellard7a5ca862008-05-27 21:13:40 +0000584{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000585 char buf[256];
586 uint64_t magic, s;
587 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100588 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000589
Dong Xu Wang07f35072011-11-22 18:06:26 +0800590 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000591
Paolo Bonzini185b4332012-03-05 08:56:10 +0100592 rc = -EINVAL;
593
Nick Thomasb2e3d872011-02-22 15:44:51 +0000594 if (read_sync(csock, buf, 8) != 8) {
Max Reitz1ce52842015-01-26 21:02:59 -0500595 error_setg(errp, "Failed to read data");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100596 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000597 }
bellard7a5ca862008-05-27 21:13:40 +0000598
Nick Thomasb2e3d872011-02-22 15:44:51 +0000599 buf[8] = '\0';
600 if (strlen(buf) == 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500601 error_setg(errp, "Server connection closed unexpectedly");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100602 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000603 }
bellard7a5ca862008-05-27 21:13:40 +0000604
Nick Thomasb2e3d872011-02-22 15:44:51 +0000605 TRACE("Magic is %c%c%c%c%c%c%c%c",
606 qemu_isprint(buf[0]) ? buf[0] : '.',
607 qemu_isprint(buf[1]) ? buf[1] : '.',
608 qemu_isprint(buf[2]) ? buf[2] : '.',
609 qemu_isprint(buf[3]) ? buf[3] : '.',
610 qemu_isprint(buf[4]) ? buf[4] : '.',
611 qemu_isprint(buf[5]) ? buf[5] : '.',
612 qemu_isprint(buf[6]) ? buf[6] : '.',
613 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000614
Nick Thomasb2e3d872011-02-22 15:44:51 +0000615 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500616 error_setg(errp, "Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100617 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000618 }
bellard7a5ca862008-05-27 21:13:40 +0000619
Nick Thomasb2e3d872011-02-22 15:44:51 +0000620 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500621 error_setg(errp, "Failed to read magic");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100622 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000623 }
624 magic = be64_to_cpu(magic);
625 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000626
Nick Thomasb2e3d872011-02-22 15:44:51 +0000627 if (name) {
628 uint32_t reserved = 0;
629 uint32_t opt;
630 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200631
Nick Thomasb2e3d872011-02-22 15:44:51 +0000632 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200633 if (magic != NBD_OPTS_MAGIC) {
Max Reitz1ce52842015-01-26 21:02:59 -0500634 if (magic == NBD_CLIENT_MAGIC) {
635 error_setg(errp, "Server does not support export names");
636 } else {
637 error_setg(errp, "Bad magic received");
638 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100639 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000640 }
641 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500642 error_setg(errp, "Failed to read server flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100643 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000644 }
645 *flags = be16_to_cpu(tmp) << 16;
646 /* reserved for future use */
647 if (write_sync(csock, &reserved, sizeof(reserved)) !=
648 sizeof(reserved)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500649 error_setg(errp, "Failed to read reserved field");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100650 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000651 }
652 /* write the export name */
653 magic = cpu_to_be64(magic);
654 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500655 error_setg(errp, "Failed to send export name magic");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100656 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000657 }
658 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
659 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500660 error_setg(errp, "Failed to send export name option number");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100661 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000662 }
663 namesize = cpu_to_be32(strlen(name));
664 if (write_sync(csock, &namesize, sizeof(namesize)) !=
665 sizeof(namesize)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500666 error_setg(errp, "Failed to send export name length");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100667 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000668 }
669 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500670 error_setg(errp, "Failed to send export name");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100671 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000672 }
673 } else {
674 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200675
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200676 if (magic != NBD_CLIENT_MAGIC) {
Max Reitz1ce52842015-01-26 21:02:59 -0500677 if (magic == NBD_OPTS_MAGIC) {
678 error_setg(errp, "Server requires an export name");
679 } else {
680 error_setg(errp, "Bad magic received");
681 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100682 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000683 }
684 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200685
Nick Thomasb2e3d872011-02-22 15:44:51 +0000686 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500687 error_setg(errp, "Failed to read export length");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100688 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000689 }
690 *size = be64_to_cpu(s);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000691 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200692
Nick Thomasb2e3d872011-02-22 15:44:51 +0000693 if (!name) {
694 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500695 error_setg(errp, "Failed to read export flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100696 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000697 }
698 *flags = be32_to_cpup(flags);
699 } else {
700 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500701 error_setg(errp, "Failed to read export flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100702 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000703 }
Max Reitz48c7d802015-02-25 13:08:32 -0500704 *flags |= be16_to_cpu(tmp);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000705 }
706 if (read_sync(csock, &buf, 124) != 124) {
Max Reitz1ce52842015-01-26 21:02:59 -0500707 error_setg(errp, "Failed to read reserved block");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100708 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000709 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100710 rc = 0;
711
712fail:
713 return rc;
thscd831bd2008-07-03 10:23:51 +0000714}
bellard7a5ca862008-05-27 21:13:40 +0000715
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200716#ifdef __linux__
Max Reitz3f472652015-02-25 13:08:25 -0500717int nbd_init(int fd, int csock, uint32_t flags, off_t size)
thscd831bd2008-07-03 10:23:51 +0000718{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800719 TRACE("Setting NBD socket");
720
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100721 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800722 int serrno = errno;
723 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100724 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800725 }
726
Max Reitz3f472652015-02-25 13:08:25 -0500727 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
bellard7a5ca862008-05-27 21:13:40 +0000728
Max Reitz3f472652015-02-25 13:08:25 -0500729 if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000730 int serrno = errno;
731 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100732 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000733 }
bellard7a5ca862008-05-27 21:13:40 +0000734
Max Reitz3f472652015-02-25 13:08:25 -0500735 TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
bellard7a5ca862008-05-27 21:13:40 +0000736
Bogdan Purcareatad064d9f2015-04-03 11:01:54 +0000737 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000738 int serrno = errno;
739 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100740 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000741 }
bellard7a5ca862008-05-27 21:13:40 +0000742
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100743 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
744 if (errno == ENOTTY) {
745 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
746 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200747
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100748 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
749 int serrno = errno;
750 LOG("Failed setting read-only attribute");
751 return -serrno;
752 }
753 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200754 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100755 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100756 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200757 }
758 }
759
Nick Thomasb2e3d872011-02-22 15:44:51 +0000760 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000761
Nick Thomasb2e3d872011-02-22 15:44:51 +0000762 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000763}
764
765int nbd_disconnect(int fd)
766{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000767 ioctl(fd, NBD_CLEAR_QUE);
768 ioctl(fd, NBD_DISCONNECT);
769 ioctl(fd, NBD_CLEAR_SOCK);
770 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000771}
772
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200773int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000774{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000775 int ret;
776 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000777
Nick Thomasb2e3d872011-02-22 15:44:51 +0000778 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000779
Nick Thomasb2e3d872011-02-22 15:44:51 +0000780 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100781 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100782 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
783 * the socket via NBD_DISCONNECT. We do not want to return 1 in
784 * that case.
785 */
786 ret = 0;
787 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000788 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000789
Nick Thomasb2e3d872011-02-22 15:44:51 +0000790 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000791
Nick Thomasb2e3d872011-02-22 15:44:51 +0000792 TRACE("Clearing NBD queue");
793 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000794
Nick Thomasb2e3d872011-02-22 15:44:51 +0000795 TRACE("Clearing NBD socket");
796 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000797
Nick Thomasb2e3d872011-02-22 15:44:51 +0000798 errno = serrno;
799 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000800}
aliguori03ff3ca2008-09-15 15:51:35 +0000801#else
Max Reitz3f472652015-02-25 13:08:25 -0500802int nbd_init(int fd, int csock, uint32_t flags, off_t size)
aliguori03ff3ca2008-09-15 15:51:35 +0000803{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100804 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000805}
806
807int nbd_disconnect(int fd)
808{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100809 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000810}
811
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200812int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000813{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100814 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000815}
816#endif
bellard7a5ca862008-05-27 21:13:40 +0000817
Paolo Bonzini94e73402012-03-07 11:25:01 +0100818ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000819{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200820 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100821 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000822
Nick Thomasb2e3d872011-02-22 15:44:51 +0000823 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
824 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
825 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
826 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
827 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000828
Nick Thomasb2e3d872011-02-22 15:44:51 +0000829 TRACE("Sending request to client: "
830 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
831 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000832
Paolo Bonzini185b4332012-03-05 08:56:10 +0100833 ret = write_sync(csock, buf, sizeof(buf));
834 if (ret < 0) {
835 return ret;
836 }
837
838 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000839 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100840 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000841 }
842 return 0;
ths75818252008-07-03 13:41:03 +0000843}
844
Paolo Bonzini94e73402012-03-07 11:25:01 +0100845static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000846{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200847 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000848 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100849 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000850
Paolo Bonzini185b4332012-03-05 08:56:10 +0100851 ret = read_sync(csock, buf, sizeof(buf));
852 if (ret < 0) {
853 return ret;
854 }
855
856 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000857 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100858 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000859 }
bellard7a5ca862008-05-27 21:13:40 +0000860
Nick Thomasb2e3d872011-02-22 15:44:51 +0000861 /* Request
862 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
863 [ 4 .. 7] type (0 == READ, 1 == WRITE)
864 [ 8 .. 15] handle
865 [16 .. 23] from
866 [24 .. 27] len
867 */
bellard7a5ca862008-05-27 21:13:40 +0000868
Nick Thomasb2e3d872011-02-22 15:44:51 +0000869 magic = be32_to_cpup((uint32_t*)buf);
870 request->type = be32_to_cpup((uint32_t*)(buf + 4));
871 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
872 request->from = be64_to_cpup((uint64_t*)(buf + 16));
873 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000874
Nick Thomasb2e3d872011-02-22 15:44:51 +0000875 TRACE("Got request: "
876 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
877 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000878
Nick Thomasb2e3d872011-02-22 15:44:51 +0000879 if (magic != NBD_REQUEST_MAGIC) {
880 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100881 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000882 }
883 return 0;
ths75818252008-07-03 13:41:03 +0000884}
bellard7a5ca862008-05-27 21:13:40 +0000885
Paolo Bonzini94e73402012-03-07 11:25:01 +0100886ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000887{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000888 uint8_t buf[NBD_REPLY_SIZE];
889 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100890 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000891
Paolo Bonzini185b4332012-03-05 08:56:10 +0100892 ret = read_sync(csock, buf, sizeof(buf));
893 if (ret < 0) {
894 return ret;
895 }
896
897 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000898 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100899 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000900 }
bellard7a5ca862008-05-27 21:13:40 +0000901
Nick Thomasb2e3d872011-02-22 15:44:51 +0000902 /* Reply
903 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
904 [ 4 .. 7] error (0 == no error)
905 [ 7 .. 15] handle
906 */
ths75818252008-07-03 13:41:03 +0000907
Nick Thomasb2e3d872011-02-22 15:44:51 +0000908 magic = be32_to_cpup((uint32_t*)buf);
909 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
910 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000911
Paolo Bonzinica441482015-05-07 17:25:10 +0200912 reply->error = nbd_errno_to_system_errno(reply->error);
913
Nick Thomasb2e3d872011-02-22 15:44:51 +0000914 TRACE("Got reply: "
915 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
916 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000917
Nick Thomasb2e3d872011-02-22 15:44:51 +0000918 if (magic != NBD_REPLY_MAGIC) {
919 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100920 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000921 }
922 return 0;
ths75818252008-07-03 13:41:03 +0000923}
924
Paolo Bonzini94e73402012-03-07 11:25:01 +0100925static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000926{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200927 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100928 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000929
Paolo Bonzinica441482015-05-07 17:25:10 +0200930 reply->error = system_errno_to_nbd_errno(reply->error);
931
Nick Thomasb2e3d872011-02-22 15:44:51 +0000932 /* Reply
933 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
934 [ 4 .. 7] error (0 == no error)
935 [ 7 .. 15] handle
936 */
937 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
938 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
939 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000940
Nick Thomasb2e3d872011-02-22 15:44:51 +0000941 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000942
Paolo Bonzini185b4332012-03-05 08:56:10 +0100943 ret = write_sync(csock, buf, sizeof(buf));
944 if (ret < 0) {
945 return ret;
946 }
947
948 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000949 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100950 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000951 }
952 return 0;
ths75818252008-07-03 13:41:03 +0000953}
954
Paolo Bonzini41996e32011-09-19 15:25:40 +0200955#define MAX_NBD_REQUESTS 16
956
Paolo Bonzinice339672012-09-18 13:17:52 +0200957void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200958{
959 client->refcount++;
960}
961
Paolo Bonzinice339672012-09-18 13:17:52 +0200962void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200963{
964 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200965 /* The last reference should be dropped by client->close,
Max Reitzf53a8292015-02-06 16:06:16 -0500966 * which is called by client_close.
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200967 */
968 assert(client->closing);
969
Max Reitz958c7172014-06-20 21:57:32 +0200970 nbd_unset_handlers(client);
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200971 close(client->sock);
972 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200973 if (client->exp) {
974 QTAILQ_REMOVE(&client->exp->clients, client, next);
975 nbd_export_put(client->exp);
976 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200977 g_free(client);
978 }
979}
980
Max Reitzf53a8292015-02-06 16:06:16 -0500981static void client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200982{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200983 if (client->closing) {
984 return;
985 }
986
987 client->closing = true;
988
989 /* Force requests to finish. They will drop their own references,
990 * then we'll close the socket and free the NBDClient.
991 */
992 shutdown(client->sock, 2);
993
994 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200995 if (client->close) {
996 client->close(client);
997 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200998}
999
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001000static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001001{
1002 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001003
Paolo Bonzini41996e32011-09-19 15:25:40 +02001004 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1005 client->nb_requests++;
Max Reitz958c7172014-06-20 21:57:32 +02001006 nbd_update_can_read(client);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001007
Paolo Bonzini17294042015-10-01 12:59:08 +02001008 req = g_new0(NBDRequest, 1);
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001009 nbd_client_get(client);
1010 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001011 return req;
1012}
1013
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001014static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001015{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001016 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +02001017
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001018 if (req->data) {
1019 qemu_vfree(req->data);
1020 }
Paolo Bonzini17294042015-10-01 12:59:08 +02001021 g_free(req);
Stefan Hajnoczie1adb272013-05-02 14:23:07 +02001022
Max Reitz958c7172014-06-20 21:57:32 +02001023 client->nb_requests--;
1024 nbd_update_can_read(client);
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001025 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001026}
1027
Max Reitzaadf99a2014-11-18 12:21:18 +01001028static void blk_aio_attached(AioContext *ctx, void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +02001029{
1030 NBDExport *exp = opaque;
1031 NBDClient *client;
1032
1033 TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx);
1034
1035 exp->ctx = ctx;
1036
1037 QTAILQ_FOREACH(client, &exp->clients, next) {
1038 nbd_set_handlers(client);
1039 }
1040}
1041
Max Reitzaadf99a2014-11-18 12:21:18 +01001042static void blk_aio_detach(void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +02001043{
1044 NBDExport *exp = opaque;
1045 NBDClient *client;
1046
1047 TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx);
1048
1049 QTAILQ_FOREACH(client, &exp->clients, next) {
1050 nbd_unset_handlers(client);
1051 }
1052
1053 exp->ctx = NULL;
1054}
1055
Max Reitze1401772014-11-18 12:21:17 +01001056NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
Max Reitz98f44bb2015-02-25 13:08:21 -05001057 uint32_t nbdflags, void (*close)(NBDExport *),
1058 Error **errp)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001059{
1060 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001061 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001062 QTAILQ_INIT(&exp->clients);
Max Reitzaadf99a2014-11-18 12:21:18 +01001063 exp->blk = blk;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001064 exp->dev_offset = dev_offset;
1065 exp->nbdflags = nbdflags;
Max Reitz98f44bb2015-02-25 13:08:21 -05001066 exp->size = size < 0 ? blk_getlength(blk) : size;
1067 if (exp->size < 0) {
1068 error_setg_errno(errp, -exp->size,
1069 "Failed to determine the NBD export's length");
1070 goto fail;
1071 }
1072 exp->size -= exp->size % BDRV_SECTOR_SIZE;
1073
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +02001074 exp->close = close;
Max Reitzaadf99a2014-11-18 12:21:18 +01001075 exp->ctx = blk_get_aio_context(blk);
1076 blk_ref(blk);
1077 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11001078 /*
1079 * NBD exports are used for non-shared storage migration. Make sure
1080 * that BDRV_O_INCOMING is cleared and the image is ready for write
1081 * access since the export could be available before migration handover.
1082 */
Max Reitzaadf99a2014-11-18 12:21:18 +01001083 blk_invalidate_cache(blk, NULL);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001084 return exp;
Max Reitz98f44bb2015-02-25 13:08:21 -05001085
1086fail:
1087 g_free(exp);
1088 return NULL;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001089}
1090
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001091NBDExport *nbd_export_find(const char *name)
1092{
1093 NBDExport *exp;
1094 QTAILQ_FOREACH(exp, &exports, next) {
1095 if (strcmp(name, exp->name) == 0) {
1096 return exp;
1097 }
1098 }
1099
1100 return NULL;
1101}
1102
1103void nbd_export_set_name(NBDExport *exp, const char *name)
1104{
1105 if (exp->name == name) {
1106 return;
1107 }
1108
1109 nbd_export_get(exp);
1110 if (exp->name != NULL) {
1111 g_free(exp->name);
1112 exp->name = NULL;
1113 QTAILQ_REMOVE(&exports, exp, next);
1114 nbd_export_put(exp);
1115 }
1116 if (name != NULL) {
1117 nbd_export_get(exp);
1118 exp->name = g_strdup(name);
1119 QTAILQ_INSERT_TAIL(&exports, exp, next);
1120 }
1121 nbd_export_put(exp);
1122}
1123
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001124void nbd_export_close(NBDExport *exp)
1125{
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001126 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001127
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001128 nbd_export_get(exp);
1129 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
Max Reitzf53a8292015-02-06 16:06:16 -05001130 client_close(client);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001131 }
Paolo Bonzini125afda2012-09-18 14:31:44 +02001132 nbd_export_set_name(exp, NULL);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001133 nbd_export_put(exp);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001134}
1135
1136void nbd_export_get(NBDExport *exp)
1137{
1138 assert(exp->refcount > 0);
1139 exp->refcount++;
1140}
1141
1142void nbd_export_put(NBDExport *exp)
1143{
1144 assert(exp->refcount > 0);
1145 if (exp->refcount == 1) {
1146 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001147 }
1148
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001149 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001150 assert(exp->name == NULL);
1151
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +02001152 if (exp->close) {
1153 exp->close(exp);
1154 }
1155
Wen Congyangd6268342015-09-16 16:35:46 +08001156 if (exp->blk) {
1157 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1158 blk_aio_detach, exp);
1159 blk_unref(exp->blk);
1160 exp->blk = NULL;
1161 }
1162
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001163 g_free(exp);
1164 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001165}
1166
Max Reitze1401772014-11-18 12:21:17 +01001167BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
Paolo Bonzini125afda2012-09-18 14:31:44 +02001168{
Max Reitzaadf99a2014-11-18 12:21:18 +01001169 return exp->blk;
Paolo Bonzini125afda2012-09-18 14:31:44 +02001170}
1171
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001172void nbd_export_close_all(void)
1173{
1174 NBDExport *exp, *next;
1175
1176 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1177 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001178 }
1179}
1180
Paolo Bonzini94e73402012-03-07 11:25:01 +01001181static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
1182 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +02001183{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001184 NBDClient *client = req->client;
1185 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001186 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +02001187
Paolo Bonzini262db382011-09-19 15:19:27 +02001188 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini262db382011-09-19 15:19:27 +02001189 client->send_coroutine = qemu_coroutine_self();
Max Reitz958c7172014-06-20 21:57:32 +02001190 nbd_set_handlers(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001191
Paolo Bonzini22045592011-09-19 14:25:30 +02001192 if (!len) {
1193 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +02001194 } else {
1195 socket_set_cork(csock, 1);
1196 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001197 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001198 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +02001199 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +01001200 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +02001201 }
1202 }
Paolo Bonzini22045592011-09-19 14:25:30 +02001203 socket_set_cork(csock, 0);
1204 }
Paolo Bonzini262db382011-09-19 15:19:27 +02001205
1206 client->send_coroutine = NULL;
Max Reitz958c7172014-06-20 21:57:32 +02001207 nbd_set_handlers(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001208 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +02001209 return rc;
1210}
1211
Paolo Bonzini94e73402012-03-07 11:25:01 +01001212static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +02001213{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001214 NBDClient *client = req->client;
1215 int csock = client->sock;
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001216 uint32_t command;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001217 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001218
Paolo Bonzini262db382011-09-19 15:19:27 +02001219 client->recv_coroutine = qemu_coroutine_self();
Max Reitz958c7172014-06-20 21:57:32 +02001220 nbd_update_can_read(client);
1221
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001222 rc = nbd_receive_request(csock, request);
1223 if (rc < 0) {
1224 if (rc != -EAGAIN) {
1225 rc = -EIO;
1226 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001227 goto out;
1228 }
1229
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001230 if (request->len > NBD_MAX_BUFFER_SIZE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001231 LOG("len (%u) is larger than max len (%u)",
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001232 request->len, NBD_MAX_BUFFER_SIZE);
Paolo Bonzinia030b342011-09-19 15:07:54 +02001233 rc = -EINVAL;
1234 goto out;
1235 }
1236
1237 if ((request->from + request->len) < request->from) {
1238 LOG("integer overflow detected! "
1239 "you're probably being attacked");
1240 rc = -EINVAL;
1241 goto out;
1242 }
1243
1244 TRACE("Decoding type");
1245
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001246 command = request->type & NBD_CMD_MASK_COMMAND;
1247 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001248 req->data = blk_blockalign(client->exp->blk, request->len);
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001249 }
1250 if (command == NBD_CMD_WRITE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001251 TRACE("Reading %u byte(s)", request->len);
1252
Paolo Bonzini262db382011-09-19 15:19:27 +02001253 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001254 LOG("reading from socket failed");
1255 rc = -EIO;
1256 goto out;
1257 }
1258 }
1259 rc = 0;
1260
1261out:
Paolo Bonzini262db382011-09-19 15:19:27 +02001262 client->recv_coroutine = NULL;
Max Reitz958c7172014-06-20 21:57:32 +02001263 nbd_update_can_read(client);
1264
Paolo Bonzinia030b342011-09-19 15:07:54 +02001265 return rc;
1266}
1267
Paolo Bonzini262db382011-09-19 15:19:27 +02001268static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00001269{
Paolo Bonzini262db382011-09-19 15:19:27 +02001270 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001271 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001272 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001273 struct nbd_request request;
1274 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001275 ssize_t ret;
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001276 uint32_t command;
ths75818252008-07-03 13:41:03 +00001277
Nick Thomasb2e3d872011-02-22 15:44:51 +00001278 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001279 if (client->closing) {
1280 return;
1281 }
ths75818252008-07-03 13:41:03 +00001282
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001283 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001284 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001285 if (ret == -EAGAIN) {
1286 goto done;
1287 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001288 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001289 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001290 }
ths75818252008-07-03 13:41:03 +00001291
Paolo Bonzinifae69412011-09-19 16:04:36 +02001292 reply.handle = request.handle;
1293 reply.error = 0;
1294
Paolo Bonzinia030b342011-09-19 15:07:54 +02001295 if (ret < 0) {
1296 reply.error = -ret;
1297 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001298 }
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001299 command = request.type & NBD_CMD_MASK_COMMAND;
1300 if (command != NBD_CMD_DISC && (request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001301 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1302 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001303 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001304 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001305 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001306 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001307 }
bellard7a5ca862008-05-27 21:13:40 +00001308
Wen Congyangd6268342015-09-16 16:35:46 +08001309 if (client->closing) {
1310 /*
1311 * The client may be closed when we are blocked in
1312 * nbd_co_receive_request()
1313 */
1314 goto done;
1315 }
1316
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001317 switch (command) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001318 case NBD_CMD_READ:
1319 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001320
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001321 if (request.type & NBD_CMD_FLAG_FUA) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001322 ret = blk_co_flush(exp->blk);
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001323 if (ret < 0) {
1324 LOG("flush failed");
1325 reply.error = -ret;
1326 goto error_reply;
1327 }
1328 }
1329
Max Reitzaadf99a2014-11-18 12:21:18 +01001330 ret = blk_read(exp->blk,
1331 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1332 req->data, request.len / BDRV_SECTOR_SIZE);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001333 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001334 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001335 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001336 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001337 }
bellard7a5ca862008-05-27 21:13:40 +00001338
Nick Thomasb2e3d872011-02-22 15:44:51 +00001339 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001340 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001341 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001342 break;
1343 case NBD_CMD_WRITE:
1344 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001345
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001346 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001347 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001348 reply.error = EROFS;
1349 goto error_reply;
1350 }
bellard7a5ca862008-05-27 21:13:40 +00001351
Paolo Bonzinifae69412011-09-19 16:04:36 +02001352 TRACE("Writing to device");
1353
Max Reitzaadf99a2014-11-18 12:21:18 +01001354 ret = blk_write(exp->blk,
1355 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1356 req->data, request.len / BDRV_SECTOR_SIZE);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001357 if (ret < 0) {
1358 LOG("writing to file failed");
1359 reply.error = -ret;
1360 goto error_reply;
1361 }
1362
1363 if (request.type & NBD_CMD_FLAG_FUA) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001364 ret = blk_co_flush(exp->blk);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001365 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001366 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001367 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001368 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001369 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001370 }
bellard7a5ca862008-05-27 21:13:40 +00001371
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001372 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001373 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001374 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001375 break;
1376 case NBD_CMD_DISC:
1377 TRACE("Request type is DISCONNECT");
1378 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001379 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001380 case NBD_CMD_FLUSH:
1381 TRACE("Request type is FLUSH");
1382
Max Reitzaadf99a2014-11-18 12:21:18 +01001383 ret = blk_co_flush(exp->blk);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001384 if (ret < 0) {
1385 LOG("flush failed");
1386 reply.error = -ret;
1387 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001388 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001389 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001390 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001391 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001392 case NBD_CMD_TRIM:
1393 TRACE("Request type is TRIM");
Max Reitzaadf99a2014-11-18 12:21:18 +01001394 ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset)
1395 / BDRV_SECTOR_SIZE,
1396 request.len / BDRV_SECTOR_SIZE);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001397 if (ret < 0) {
1398 LOG("discard failed");
1399 reply.error = -ret;
1400 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001401 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001402 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001403 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001404 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001405 default:
1406 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001407 invalid_request:
Yik Fang8b2f0ab2015-02-12 06:21:51 +00001408 reply.error = EINVAL;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001409 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001410 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001411 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001412 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001413 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001414 }
bellard7a5ca862008-05-27 21:13:40 +00001415
Nick Thomasb2e3d872011-02-22 15:44:51 +00001416 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001417
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001418done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001419 nbd_request_put(req);
1420 return;
1421
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001422out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001423 nbd_request_put(req);
Max Reitzf53a8292015-02-06 16:06:16 -05001424 client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001425}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001426
Paolo Bonzini1743b512011-09-19 14:33:23 +02001427static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001428{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001429 NBDClient *client = opaque;
1430
Paolo Bonzini262db382011-09-19 15:19:27 +02001431 if (client->recv_coroutine) {
1432 qemu_coroutine_enter(client->recv_coroutine, NULL);
1433 } else {
1434 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001435 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001436}
1437
Paolo Bonzini262db382011-09-19 15:19:27 +02001438static void nbd_restart_write(void *opaque)
1439{
1440 NBDClient *client = opaque;
1441
1442 qemu_coroutine_enter(client->send_coroutine, NULL);
1443}
1444
Max Reitz958c7172014-06-20 21:57:32 +02001445static void nbd_set_handlers(NBDClient *client)
1446{
1447 if (client->exp && client->exp->ctx) {
1448 aio_set_fd_handler(client->exp->ctx, client->sock,
Fam Zheng172cc122015-10-23 11:08:06 +08001449 true,
Max Reitz958c7172014-06-20 21:57:32 +02001450 client->can_read ? nbd_read : NULL,
1451 client->send_coroutine ? nbd_restart_write : NULL,
1452 client);
1453 }
1454}
1455
1456static void nbd_unset_handlers(NBDClient *client)
1457{
1458 if (client->exp && client->exp->ctx) {
Fam Zhengdca21ef2015-10-23 11:08:05 +08001459 aio_set_fd_handler(client->exp->ctx, client->sock,
Fam Zheng172cc122015-10-23 11:08:06 +08001460 true, NULL, NULL, NULL);
Max Reitz958c7172014-06-20 21:57:32 +02001461 }
1462}
1463
1464static void nbd_update_can_read(NBDClient *client)
1465{
1466 bool can_read = client->recv_coroutine ||
1467 client->nb_requests < MAX_NBD_REQUESTS;
1468
1469 if (can_read != client->can_read) {
1470 client->can_read = can_read;
1471 nbd_set_handlers(client);
1472
1473 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1474 * in nbd_set_handlers() will have taken care of that */
1475 }
1476}
1477
Paolo Bonzini1743b512011-09-19 14:33:23 +02001478NBDClient *nbd_client_new(NBDExport *exp, int csock,
1479 void (*close)(NBDClient *))
1480{
1481 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001482 client = g_malloc0(sizeof(NBDClient));
1483 client->refcount = 1;
1484 client->exp = exp;
1485 client->sock = csock;
Max Reitz958c7172014-06-20 21:57:32 +02001486 client->can_read = true;
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001487 if (nbd_send_negotiate(client)) {
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001488 g_free(client);
1489 return NULL;
1490 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001491 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001492 qemu_co_mutex_init(&client->send_lock);
Max Reitz958c7172014-06-20 21:57:32 +02001493 nbd_set_handlers(client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001494
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001495 if (exp) {
1496 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1497 nbd_export_get(exp);
1498 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001499 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001500}