blob: e56afbc1621c05464678de69c9815e9d4d6803a5 [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
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
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 Bonzini9a304d22012-08-22 15:30:31 +020089/* Definitions for opaque data types */
90
91typedef struct NBDRequest NBDRequest;
92
93struct NBDRequest {
94 QSIMPLEQ_ENTRY(NBDRequest) entry;
95 NBDClient *client;
96 uint8_t *data;
97};
98
99struct NBDExport {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200100 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200101 void (*close)(NBDExport *exp);
102
Max Reitzaadf99a2014-11-18 12:21:18 +0100103 BlockBackend *blk;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200104 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200105 off_t dev_offset;
106 off_t size;
107 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200108 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200109 QTAILQ_ENTRY(NBDExport) next;
Max Reitz958c7172014-06-20 21:57:32 +0200110
111 AioContext *ctx;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200112};
113
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200114static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
115
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200116struct NBDClient {
117 int refcount;
118 void (*close)(NBDClient *client);
119
120 NBDExport *exp;
121 int sock;
122
123 Coroutine *recv_coroutine;
124
125 CoMutex send_lock;
126 Coroutine *send_coroutine;
127
Max Reitz958c7172014-06-20 21:57:32 +0200128 bool can_read;
129
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200130 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200131 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200132 bool closing;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200133};
134
bellard7a5ca862008-05-27 21:13:40 +0000135/* That's all folks */
136
Max Reitz958c7172014-06-20 21:57:32 +0200137static void nbd_set_handlers(NBDClient *client);
138static void nbd_unset_handlers(NBDClient *client);
139static void nbd_update_can_read(NBDClient *client);
140
Paolo Bonzini185b4332012-03-05 08:56:10 +0100141ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000142{
143 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100144 int err;
bellard7a5ca862008-05-27 21:13:40 +0000145
Paolo Bonziniae255e52011-09-08 14:28:59 +0200146 if (qemu_in_coroutine()) {
147 if (do_read) {
148 return qemu_co_recv(fd, buffer, size);
149 } else {
150 return qemu_co_send(fd, buffer, size);
151 }
152 }
153
bellard7a5ca862008-05-27 21:13:40 +0000154 while (offset < size) {
155 ssize_t len;
156
157 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000158 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000159 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000160 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000161 }
162
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100163 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100164 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000165
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100166 /* recoverable error */
Paolo Bonzini79d9b652014-07-09 11:53:09 +0200167 if (err == EINTR || (offset > 0 && (err == EAGAIN || err == EWOULDBLOCK))) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100168 continue;
169 }
170
171 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100172 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000173 }
174
175 /* eof */
176 if (len == 0) {
177 break;
178 }
179
bellard7a5ca862008-05-27 21:13:40 +0000180 offset += len;
181 }
182
183 return offset;
184}
185
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100186static ssize_t read_sync(int fd, void *buffer, size_t size)
187{
188 /* Sockets are kept in blocking mode in the negotiation phase. After
189 * that, a non-readable socket simply means that another thread stole
190 * our request/reply. Synchronization is done with recv_coroutine, so
191 * that this is coroutine-safe.
192 */
193 return nbd_wr_sync(fd, buffer, size, true);
194}
195
196static ssize_t write_sync(int fd, void *buffer, size_t size)
197{
198 int ret;
199 do {
200 /* For writes, we do expect the socket to be writable. */
201 ret = nbd_wr_sync(fd, buffer, size, false);
202 } while (ret == -EAGAIN);
203 return ret;
204}
205
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200206/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000207
208 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000209 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200210
211 or
212
213 Server Client
214 Negotiate #1
215 Option
216 Negotiate #2
217
218 ----
219
220 followed by
221
222 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000223 Request
224 Response
225 Request
226 Response
227 ...
228 ...
229 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200230
bellard7a5ca862008-05-27 21:13:40 +0000231*/
232
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100233static int nbd_send_rep(int csock, uint32_t type, uint32_t opt)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200234{
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200235 uint64_t magic;
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100236 uint32_t len;
237
238 magic = cpu_to_be64(NBD_REP_MAGIC);
239 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
240 LOG("write failed (rep magic)");
241 return -EINVAL;
242 }
243 opt = cpu_to_be32(opt);
244 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
245 LOG("write failed (rep opt)");
246 return -EINVAL;
247 }
248 type = cpu_to_be32(type);
249 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
250 LOG("write failed (rep type)");
251 return -EINVAL;
252 }
253 len = cpu_to_be32(0);
254 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
255 LOG("write failed (rep data length)");
256 return -EINVAL;
257 }
258 return 0;
259}
260
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100261static int nbd_send_rep_list(int csock, NBDExport *exp)
262{
263 uint64_t magic, name_len;
264 uint32_t opt, type, len;
265
266 name_len = strlen(exp->name);
267 magic = cpu_to_be64(NBD_REP_MAGIC);
268 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
269 LOG("write failed (magic)");
270 return -EINVAL;
271 }
272 opt = cpu_to_be32(NBD_OPT_LIST);
273 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
274 LOG("write failed (opt)");
275 return -EINVAL;
276 }
277 type = cpu_to_be32(NBD_REP_SERVER);
278 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
279 LOG("write failed (reply type)");
280 return -EINVAL;
281 }
282 len = cpu_to_be32(name_len + sizeof(len));
283 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
284 LOG("write failed (length)");
285 return -EINVAL;
286 }
287 len = cpu_to_be32(name_len);
288 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
289 LOG("write failed (length)");
290 return -EINVAL;
291 }
292 if (write_sync(csock, exp->name, name_len) != name_len) {
293 LOG("write failed (buffer)");
294 return -EINVAL;
295 }
296 return 0;
297}
298
299static int nbd_handle_list(NBDClient *client, uint32_t length)
300{
301 int csock;
302 NBDExport *exp;
303
304 csock = client->sock;
305 if (length) {
306 return nbd_send_rep(csock, NBD_REP_ERR_INVALID, NBD_OPT_LIST);
307 }
308
309 /* For each export, send a NBD_REP_SERVER reply. */
310 QTAILQ_FOREACH(exp, &exports, next) {
311 if (nbd_send_rep_list(csock, exp)) {
312 return -EINVAL;
313 }
314 }
315 /* Finish with a NBD_REP_ACK. */
316 return nbd_send_rep(csock, NBD_REP_ACK, NBD_OPT_LIST);
317}
318
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100319static int nbd_handle_export_name(NBDClient *client, uint32_t length)
320{
321 int rc = -EINVAL, csock = client->sock;
322 char name[256];
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200323
324 /* Client sends:
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200325 [20 .. xx] export name (length bytes)
326 */
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200327 TRACE("Checking length");
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200328 if (length > 255) {
329 LOG("Bad length received");
330 goto fail;
331 }
332 if (read_sync(csock, name, length) != length) {
333 LOG("read failed");
334 goto fail;
335 }
336 name[length] = '\0';
337
338 client->exp = nbd_export_find(name);
339 if (!client->exp) {
340 LOG("export not found");
341 goto fail;
342 }
343
344 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
345 nbd_export_get(client->exp);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200346 rc = 0;
347fail:
348 return rc;
349}
350
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100351static int nbd_receive_options(NBDClient *client)
352{
353 while (1) {
354 int csock = client->sock;
355 uint32_t tmp, length;
356 uint64_t magic;
357
358 /* Client sends:
359 [ 0 .. 3] client flags
360 [ 4 .. 11] NBD_OPTS_MAGIC
361 [12 .. 15] NBD option
362 [16 .. 19] length
363 ... Rest of request
364 */
365
366 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
367 LOG("read failed");
368 return -EINVAL;
369 }
370 TRACE("Checking client flags");
371 tmp = be32_to_cpu(tmp);
372 if (tmp != 0 && tmp != NBD_FLAG_C_FIXED_NEWSTYLE) {
373 LOG("Bad client flags received");
374 return -EINVAL;
375 }
376
377 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
378 LOG("read failed");
379 return -EINVAL;
380 }
381 TRACE("Checking opts magic");
382 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
383 LOG("Bad magic received");
384 return -EINVAL;
385 }
386
387 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
388 LOG("read failed");
389 return -EINVAL;
390 }
391
392 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
393 LOG("read failed");
394 return -EINVAL;
395 }
396 length = be32_to_cpu(length);
397
398 TRACE("Checking option");
399 switch (be32_to_cpu(tmp)) {
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100400 case NBD_OPT_LIST:
401 if (nbd_handle_list(client, length) < 0) {
402 return 1;
403 }
404 break;
405
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100406 case NBD_OPT_ABORT:
407 return -EINVAL;
408
409 case NBD_OPT_EXPORT_NAME:
410 return nbd_handle_export_name(client, length);
411
412 default:
413 tmp = be32_to_cpu(tmp);
414 LOG("Unsupported option 0x%x", tmp);
415 nbd_send_rep(client->sock, NBD_REP_ERR_UNSUP, tmp);
416 return -EINVAL;
417 }
418 }
419}
420
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200421static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000422{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200423 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000424 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100425 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200426 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
427 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000428
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200429 /* Negotiation header without options:
430 [ 0 .. 7] passwd ("NBDMAGIC")
431 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000432 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200433 [24 .. 25] server flags (0)
Hani Benhabiles5672ee52014-05-13 00:35:16 +0100434 [26 .. 27] export flags
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200435 [28 .. 151] reserved (0)
436
437 Negotiation header with options, part 1:
438 [ 0 .. 7] passwd ("NBDMAGIC")
439 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
440 [16 .. 17] server flags (0)
441
442 part 2 (after options are sent):
443 [18 .. 25] size
444 [26 .. 27] export flags
445 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000446 */
bellard7a5ca862008-05-27 21:13:40 +0000447
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100448 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100449 rc = -EINVAL;
450
Nick Thomasb2e3d872011-02-22 15:44:51 +0000451 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100452 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000453 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200454 if (client->exp) {
455 assert ((client->exp->nbdflags & ~65535) == 0);
456 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
457 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
458 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
459 } else {
460 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100461 cpu_to_be16w((uint16_t *)(buf + 16), NBD_FLAG_FIXED_NEWSTYLE);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200462 }
bellard7a5ca862008-05-27 21:13:40 +0000463
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200464 if (client->exp) {
465 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
466 LOG("write failed");
467 goto fail;
468 }
469 } else {
470 if (write_sync(csock, buf, 18) != 18) {
471 LOG("write failed");
472 goto fail;
473 }
474 rc = nbd_receive_options(client);
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100475 if (rc != 0) {
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200476 LOG("option negotiation failed");
477 goto fail;
478 }
479
480 assert ((client->exp->nbdflags & ~65535) == 0);
481 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
482 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
483 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
484 LOG("write failed");
485 goto fail;
486 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000487 }
bellard7a5ca862008-05-27 21:13:40 +0000488
Dong Xu Wang07f35072011-11-22 18:06:26 +0800489 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100490 rc = 0;
491fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100492 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100493 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000494}
495
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200496int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
Max Reitz1ce52842015-01-26 21:02:59 -0500497 off_t *size, size_t *blocksize, Error **errp)
bellard7a5ca862008-05-27 21:13:40 +0000498{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000499 char buf[256];
500 uint64_t magic, s;
501 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100502 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000503
Dong Xu Wang07f35072011-11-22 18:06:26 +0800504 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000505
Paolo Bonzini185b4332012-03-05 08:56:10 +0100506 rc = -EINVAL;
507
Nick Thomasb2e3d872011-02-22 15:44:51 +0000508 if (read_sync(csock, buf, 8) != 8) {
Max Reitz1ce52842015-01-26 21:02:59 -0500509 error_setg(errp, "Failed to read data");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100510 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000511 }
bellard7a5ca862008-05-27 21:13:40 +0000512
Nick Thomasb2e3d872011-02-22 15:44:51 +0000513 buf[8] = '\0';
514 if (strlen(buf) == 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500515 error_setg(errp, "Server connection closed unexpectedly");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100516 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000517 }
bellard7a5ca862008-05-27 21:13:40 +0000518
Nick Thomasb2e3d872011-02-22 15:44:51 +0000519 TRACE("Magic is %c%c%c%c%c%c%c%c",
520 qemu_isprint(buf[0]) ? buf[0] : '.',
521 qemu_isprint(buf[1]) ? buf[1] : '.',
522 qemu_isprint(buf[2]) ? buf[2] : '.',
523 qemu_isprint(buf[3]) ? buf[3] : '.',
524 qemu_isprint(buf[4]) ? buf[4] : '.',
525 qemu_isprint(buf[5]) ? buf[5] : '.',
526 qemu_isprint(buf[6]) ? buf[6] : '.',
527 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000528
Nick Thomasb2e3d872011-02-22 15:44:51 +0000529 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500530 error_setg(errp, "Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100531 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000532 }
bellard7a5ca862008-05-27 21:13:40 +0000533
Nick Thomasb2e3d872011-02-22 15:44:51 +0000534 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500535 error_setg(errp, "Failed to read magic");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100536 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000537 }
538 magic = be64_to_cpu(magic);
539 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000540
Nick Thomasb2e3d872011-02-22 15:44:51 +0000541 if (name) {
542 uint32_t reserved = 0;
543 uint32_t opt;
544 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200545
Nick Thomasb2e3d872011-02-22 15:44:51 +0000546 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200547 if (magic != NBD_OPTS_MAGIC) {
Max Reitz1ce52842015-01-26 21:02:59 -0500548 if (magic == NBD_CLIENT_MAGIC) {
549 error_setg(errp, "Server does not support export names");
550 } else {
551 error_setg(errp, "Bad magic received");
552 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100553 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000554 }
555 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500556 error_setg(errp, "Failed to read server flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100557 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000558 }
559 *flags = be16_to_cpu(tmp) << 16;
560 /* reserved for future use */
561 if (write_sync(csock, &reserved, sizeof(reserved)) !=
562 sizeof(reserved)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500563 error_setg(errp, "Failed to read reserved field");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100564 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000565 }
566 /* write the export name */
567 magic = cpu_to_be64(magic);
568 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500569 error_setg(errp, "Failed to send export name magic");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100570 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000571 }
572 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
573 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500574 error_setg(errp, "Failed to send export name option number");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100575 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000576 }
577 namesize = cpu_to_be32(strlen(name));
578 if (write_sync(csock, &namesize, sizeof(namesize)) !=
579 sizeof(namesize)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500580 error_setg(errp, "Failed to send export name length");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100581 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000582 }
583 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500584 error_setg(errp, "Failed to send export name");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100585 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000586 }
587 } else {
588 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200589
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200590 if (magic != NBD_CLIENT_MAGIC) {
Max Reitz1ce52842015-01-26 21:02:59 -0500591 if (magic == NBD_OPTS_MAGIC) {
592 error_setg(errp, "Server requires an export name");
593 } else {
594 error_setg(errp, "Bad magic received");
595 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100596 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000597 }
598 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200599
Nick Thomasb2e3d872011-02-22 15:44:51 +0000600 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500601 error_setg(errp, "Failed to read export length");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100602 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000603 }
604 *size = be64_to_cpu(s);
605 *blocksize = 1024;
606 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200607
Nick Thomasb2e3d872011-02-22 15:44:51 +0000608 if (!name) {
609 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500610 error_setg(errp, "Failed to read export flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100611 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000612 }
613 *flags = be32_to_cpup(flags);
614 } else {
615 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
Max Reitz1ce52842015-01-26 21:02:59 -0500616 error_setg(errp, "Failed to read export flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100617 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000618 }
619 *flags |= be32_to_cpu(tmp);
620 }
621 if (read_sync(csock, &buf, 124) != 124) {
Max Reitz1ce52842015-01-26 21:02:59 -0500622 error_setg(errp, "Failed to read reserved block");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100623 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000624 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100625 rc = 0;
626
627fail:
628 return rc;
thscd831bd2008-07-03 10:23:51 +0000629}
bellard7a5ca862008-05-27 21:13:40 +0000630
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200631#ifdef __linux__
632int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000633{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800634 TRACE("Setting NBD socket");
635
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100636 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800637 int serrno = errno;
638 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100639 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800640 }
641
Nick Thomasb2e3d872011-02-22 15:44:51 +0000642 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000643
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100644 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000645 int serrno = errno;
646 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100647 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000648 }
bellard7a5ca862008-05-27 21:13:40 +0000649
Blue Swirl0bfcd592010-05-22 08:02:12 +0000650 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000651
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100652 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000653 int serrno = errno;
654 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100655 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000656 }
bellard7a5ca862008-05-27 21:13:40 +0000657
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100658 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
659 if (errno == ENOTTY) {
660 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
661 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200662
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100663 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
664 int serrno = errno;
665 LOG("Failed setting read-only attribute");
666 return -serrno;
667 }
668 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200669 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100670 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100671 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200672 }
673 }
674
Nick Thomasb2e3d872011-02-22 15:44:51 +0000675 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000676
Nick Thomasb2e3d872011-02-22 15:44:51 +0000677 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000678}
679
680int nbd_disconnect(int fd)
681{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000682 ioctl(fd, NBD_CLEAR_QUE);
683 ioctl(fd, NBD_DISCONNECT);
684 ioctl(fd, NBD_CLEAR_SOCK);
685 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000686}
687
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200688int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000689{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000690 int ret;
691 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000692
Nick Thomasb2e3d872011-02-22 15:44:51 +0000693 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000694
Nick Thomasb2e3d872011-02-22 15:44:51 +0000695 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100696 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100697 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
698 * the socket via NBD_DISCONNECT. We do not want to return 1 in
699 * that case.
700 */
701 ret = 0;
702 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000703 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000704
Nick Thomasb2e3d872011-02-22 15:44:51 +0000705 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000706
Nick Thomasb2e3d872011-02-22 15:44:51 +0000707 TRACE("Clearing NBD queue");
708 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000709
Nick Thomasb2e3d872011-02-22 15:44:51 +0000710 TRACE("Clearing NBD socket");
711 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000712
Nick Thomasb2e3d872011-02-22 15:44:51 +0000713 errno = serrno;
714 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000715}
aliguori03ff3ca2008-09-15 15:51:35 +0000716#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200717int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000718{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100719 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000720}
721
722int nbd_disconnect(int fd)
723{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100724 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000725}
726
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200727int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000728{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100729 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000730}
731#endif
bellard7a5ca862008-05-27 21:13:40 +0000732
Paolo Bonzini94e73402012-03-07 11:25:01 +0100733ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000734{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200735 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100736 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000737
Nick Thomasb2e3d872011-02-22 15:44:51 +0000738 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
739 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
740 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
741 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
742 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000743
Nick Thomasb2e3d872011-02-22 15:44:51 +0000744 TRACE("Sending request to client: "
745 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
746 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000747
Paolo Bonzini185b4332012-03-05 08:56:10 +0100748 ret = write_sync(csock, buf, sizeof(buf));
749 if (ret < 0) {
750 return ret;
751 }
752
753 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000754 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100755 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000756 }
757 return 0;
ths75818252008-07-03 13:41:03 +0000758}
759
Paolo Bonzini94e73402012-03-07 11:25:01 +0100760static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000761{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200762 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000763 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100764 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000765
Paolo Bonzini185b4332012-03-05 08:56:10 +0100766 ret = read_sync(csock, buf, sizeof(buf));
767 if (ret < 0) {
768 return ret;
769 }
770
771 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000772 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100773 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000774 }
bellard7a5ca862008-05-27 21:13:40 +0000775
Nick Thomasb2e3d872011-02-22 15:44:51 +0000776 /* Request
777 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
778 [ 4 .. 7] type (0 == READ, 1 == WRITE)
779 [ 8 .. 15] handle
780 [16 .. 23] from
781 [24 .. 27] len
782 */
bellard7a5ca862008-05-27 21:13:40 +0000783
Nick Thomasb2e3d872011-02-22 15:44:51 +0000784 magic = be32_to_cpup((uint32_t*)buf);
785 request->type = be32_to_cpup((uint32_t*)(buf + 4));
786 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
787 request->from = be64_to_cpup((uint64_t*)(buf + 16));
788 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000789
Nick Thomasb2e3d872011-02-22 15:44:51 +0000790 TRACE("Got request: "
791 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
792 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000793
Nick Thomasb2e3d872011-02-22 15:44:51 +0000794 if (magic != NBD_REQUEST_MAGIC) {
795 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100796 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000797 }
798 return 0;
ths75818252008-07-03 13:41:03 +0000799}
bellard7a5ca862008-05-27 21:13:40 +0000800
Paolo Bonzini94e73402012-03-07 11:25:01 +0100801ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000802{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000803 uint8_t buf[NBD_REPLY_SIZE];
804 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100805 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000806
Paolo Bonzini185b4332012-03-05 08:56:10 +0100807 ret = read_sync(csock, buf, sizeof(buf));
808 if (ret < 0) {
809 return ret;
810 }
811
812 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000813 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100814 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000815 }
bellard7a5ca862008-05-27 21:13:40 +0000816
Nick Thomasb2e3d872011-02-22 15:44:51 +0000817 /* Reply
818 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
819 [ 4 .. 7] error (0 == no error)
820 [ 7 .. 15] handle
821 */
ths75818252008-07-03 13:41:03 +0000822
Nick Thomasb2e3d872011-02-22 15:44:51 +0000823 magic = be32_to_cpup((uint32_t*)buf);
824 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
825 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000826
Nick Thomasb2e3d872011-02-22 15:44:51 +0000827 TRACE("Got reply: "
828 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
829 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000830
Nick Thomasb2e3d872011-02-22 15:44:51 +0000831 if (magic != NBD_REPLY_MAGIC) {
832 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100833 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000834 }
835 return 0;
ths75818252008-07-03 13:41:03 +0000836}
837
Paolo Bonzini94e73402012-03-07 11:25:01 +0100838static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000839{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200840 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100841 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000842
Nick Thomasb2e3d872011-02-22 15:44:51 +0000843 /* Reply
844 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
845 [ 4 .. 7] error (0 == no error)
846 [ 7 .. 15] handle
847 */
848 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
849 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
850 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000851
Nick Thomasb2e3d872011-02-22 15:44:51 +0000852 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000853
Paolo Bonzini185b4332012-03-05 08:56:10 +0100854 ret = write_sync(csock, buf, sizeof(buf));
855 if (ret < 0) {
856 return ret;
857 }
858
859 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000860 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100861 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000862 }
863 return 0;
ths75818252008-07-03 13:41:03 +0000864}
865
Paolo Bonzini41996e32011-09-19 15:25:40 +0200866#define MAX_NBD_REQUESTS 16
867
Paolo Bonzinice339672012-09-18 13:17:52 +0200868void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200869{
870 client->refcount++;
871}
872
Paolo Bonzinice339672012-09-18 13:17:52 +0200873void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200874{
875 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200876 /* The last reference should be dropped by client->close,
877 * which is called by nbd_client_close.
878 */
879 assert(client->closing);
880
Max Reitz958c7172014-06-20 21:57:32 +0200881 nbd_unset_handlers(client);
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200882 close(client->sock);
883 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200884 if (client->exp) {
885 QTAILQ_REMOVE(&client->exp->clients, client, next);
886 nbd_export_put(client->exp);
887 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200888 g_free(client);
889 }
890}
891
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200892void nbd_client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200893{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200894 if (client->closing) {
895 return;
896 }
897
898 client->closing = true;
899
900 /* Force requests to finish. They will drop their own references,
901 * then we'll close the socket and free the NBDClient.
902 */
903 shutdown(client->sock, 2);
904
905 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200906 if (client->close) {
907 client->close(client);
908 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200909}
910
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200911static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200912{
913 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200914
Paolo Bonzini41996e32011-09-19 15:25:40 +0200915 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
916 client->nb_requests++;
Max Reitz958c7172014-06-20 21:57:32 +0200917 nbd_update_can_read(client);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200918
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200919 req = g_slice_new0(NBDRequest);
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200920 nbd_client_get(client);
921 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200922 return req;
923}
924
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200925static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200926{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200927 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200928
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200929 if (req->data) {
930 qemu_vfree(req->data);
931 }
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200932 g_slice_free(NBDRequest, req);
933
Max Reitz958c7172014-06-20 21:57:32 +0200934 client->nb_requests--;
935 nbd_update_can_read(client);
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200936 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200937}
938
Max Reitzaadf99a2014-11-18 12:21:18 +0100939static void blk_aio_attached(AioContext *ctx, void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +0200940{
941 NBDExport *exp = opaque;
942 NBDClient *client;
943
944 TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx);
945
946 exp->ctx = ctx;
947
948 QTAILQ_FOREACH(client, &exp->clients, next) {
949 nbd_set_handlers(client);
950 }
951}
952
Max Reitzaadf99a2014-11-18 12:21:18 +0100953static void blk_aio_detach(void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +0200954{
955 NBDExport *exp = opaque;
956 NBDClient *client;
957
958 TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx);
959
960 QTAILQ_FOREACH(client, &exp->clients, next) {
961 nbd_unset_handlers(client);
962 }
963
964 exp->ctx = NULL;
965}
966
Max Reitze1401772014-11-18 12:21:17 +0100967NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
968 uint32_t nbdflags, void (*close)(NBDExport *))
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200969{
970 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200971 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200972 QTAILQ_INIT(&exp->clients);
Max Reitzaadf99a2014-11-18 12:21:18 +0100973 exp->blk = blk;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200974 exp->dev_offset = dev_offset;
975 exp->nbdflags = nbdflags;
Max Reitzaadf99a2014-11-18 12:21:18 +0100976 exp->size = size == -1 ? blk_getlength(blk) : size;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200977 exp->close = close;
Max Reitzaadf99a2014-11-18 12:21:18 +0100978 exp->ctx = blk_get_aio_context(blk);
979 blk_ref(blk);
980 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +1100981 /*
982 * NBD exports are used for non-shared storage migration. Make sure
983 * that BDRV_O_INCOMING is cleared and the image is ready for write
984 * access since the export could be available before migration handover.
985 */
Max Reitzaadf99a2014-11-18 12:21:18 +0100986 blk_invalidate_cache(blk, NULL);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200987 return exp;
988}
989
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200990NBDExport *nbd_export_find(const char *name)
991{
992 NBDExport *exp;
993 QTAILQ_FOREACH(exp, &exports, next) {
994 if (strcmp(name, exp->name) == 0) {
995 return exp;
996 }
997 }
998
999 return NULL;
1000}
1001
1002void nbd_export_set_name(NBDExport *exp, const char *name)
1003{
1004 if (exp->name == name) {
1005 return;
1006 }
1007
1008 nbd_export_get(exp);
1009 if (exp->name != NULL) {
1010 g_free(exp->name);
1011 exp->name = NULL;
1012 QTAILQ_REMOVE(&exports, exp, next);
1013 nbd_export_put(exp);
1014 }
1015 if (name != NULL) {
1016 nbd_export_get(exp);
1017 exp->name = g_strdup(name);
1018 QTAILQ_INSERT_TAIL(&exports, exp, next);
1019 }
1020 nbd_export_put(exp);
1021}
1022
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001023void nbd_export_close(NBDExport *exp)
1024{
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001025 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001026
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001027 nbd_export_get(exp);
1028 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1029 nbd_client_close(client);
1030 }
Paolo Bonzini125afda2012-09-18 14:31:44 +02001031 nbd_export_set_name(exp, NULL);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001032 nbd_export_put(exp);
Max Reitzaadf99a2014-11-18 12:21:18 +01001033 if (exp->blk) {
1034 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1035 blk_aio_detach, exp);
1036 blk_unref(exp->blk);
1037 exp->blk = NULL;
Fam Zheng38b54b62013-08-23 09:14:50 +08001038 }
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001039}
1040
1041void nbd_export_get(NBDExport *exp)
1042{
1043 assert(exp->refcount > 0);
1044 exp->refcount++;
1045}
1046
1047void nbd_export_put(NBDExport *exp)
1048{
1049 assert(exp->refcount > 0);
1050 if (exp->refcount == 1) {
1051 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001052 }
1053
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001054 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001055 assert(exp->name == NULL);
1056
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +02001057 if (exp->close) {
1058 exp->close(exp);
1059 }
1060
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001061 g_free(exp);
1062 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001063}
1064
Max Reitze1401772014-11-18 12:21:17 +01001065BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
Paolo Bonzini125afda2012-09-18 14:31:44 +02001066{
Max Reitzaadf99a2014-11-18 12:21:18 +01001067 return exp->blk;
Paolo Bonzini125afda2012-09-18 14:31:44 +02001068}
1069
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001070void nbd_export_close_all(void)
1071{
1072 NBDExport *exp, *next;
1073
1074 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1075 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001076 }
1077}
1078
Paolo Bonzini94e73402012-03-07 11:25:01 +01001079static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
1080 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +02001081{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001082 NBDClient *client = req->client;
1083 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001084 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +02001085
Paolo Bonzini262db382011-09-19 15:19:27 +02001086 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini262db382011-09-19 15:19:27 +02001087 client->send_coroutine = qemu_coroutine_self();
Max Reitz958c7172014-06-20 21:57:32 +02001088 nbd_set_handlers(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001089
Paolo Bonzini22045592011-09-19 14:25:30 +02001090 if (!len) {
1091 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +02001092 } else {
1093 socket_set_cork(csock, 1);
1094 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001095 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001096 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +02001097 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +01001098 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +02001099 }
1100 }
Paolo Bonzini22045592011-09-19 14:25:30 +02001101 socket_set_cork(csock, 0);
1102 }
Paolo Bonzini262db382011-09-19 15:19:27 +02001103
1104 client->send_coroutine = NULL;
Max Reitz958c7172014-06-20 21:57:32 +02001105 nbd_set_handlers(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001106 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +02001107 return rc;
1108}
1109
Paolo Bonzini94e73402012-03-07 11:25:01 +01001110static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +02001111{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001112 NBDClient *client = req->client;
1113 int csock = client->sock;
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001114 uint32_t command;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001115 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001116
Paolo Bonzini262db382011-09-19 15:19:27 +02001117 client->recv_coroutine = qemu_coroutine_self();
Max Reitz958c7172014-06-20 21:57:32 +02001118 nbd_update_can_read(client);
1119
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001120 rc = nbd_receive_request(csock, request);
1121 if (rc < 0) {
1122 if (rc != -EAGAIN) {
1123 rc = -EIO;
1124 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001125 goto out;
1126 }
1127
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001128 if (request->len > NBD_MAX_BUFFER_SIZE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001129 LOG("len (%u) is larger than max len (%u)",
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001130 request->len, NBD_MAX_BUFFER_SIZE);
Paolo Bonzinia030b342011-09-19 15:07:54 +02001131 rc = -EINVAL;
1132 goto out;
1133 }
1134
1135 if ((request->from + request->len) < request->from) {
1136 LOG("integer overflow detected! "
1137 "you're probably being attacked");
1138 rc = -EINVAL;
1139 goto out;
1140 }
1141
1142 TRACE("Decoding type");
1143
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001144 command = request->type & NBD_CMD_MASK_COMMAND;
1145 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001146 req->data = blk_blockalign(client->exp->blk, request->len);
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001147 }
1148 if (command == NBD_CMD_WRITE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001149 TRACE("Reading %u byte(s)", request->len);
1150
Paolo Bonzini262db382011-09-19 15:19:27 +02001151 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001152 LOG("reading from socket failed");
1153 rc = -EIO;
1154 goto out;
1155 }
1156 }
1157 rc = 0;
1158
1159out:
Paolo Bonzini262db382011-09-19 15:19:27 +02001160 client->recv_coroutine = NULL;
Max Reitz958c7172014-06-20 21:57:32 +02001161 nbd_update_can_read(client);
1162
Paolo Bonzinia030b342011-09-19 15:07:54 +02001163 return rc;
1164}
1165
Paolo Bonzini262db382011-09-19 15:19:27 +02001166static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00001167{
Paolo Bonzini262db382011-09-19 15:19:27 +02001168 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001169 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001170 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001171 struct nbd_request request;
1172 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001173 ssize_t ret;
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001174 uint32_t command;
ths75818252008-07-03 13:41:03 +00001175
Nick Thomasb2e3d872011-02-22 15:44:51 +00001176 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001177 if (client->closing) {
1178 return;
1179 }
ths75818252008-07-03 13:41:03 +00001180
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001181 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001182 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001183 if (ret == -EAGAIN) {
1184 goto done;
1185 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001186 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001187 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001188 }
ths75818252008-07-03 13:41:03 +00001189
Paolo Bonzinifae69412011-09-19 16:04:36 +02001190 reply.handle = request.handle;
1191 reply.error = 0;
1192
Paolo Bonzinia030b342011-09-19 15:07:54 +02001193 if (ret < 0) {
1194 reply.error = -ret;
1195 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001196 }
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001197 command = request.type & NBD_CMD_MASK_COMMAND;
1198 if (command != NBD_CMD_DISC && (request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001199 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1200 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001201 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001202 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001203 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001204 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001205 }
bellard7a5ca862008-05-27 21:13:40 +00001206
Hani Benhabiles8c5d1ab2014-05-18 11:50:05 +01001207 switch (command) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001208 case NBD_CMD_READ:
1209 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001210
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001211 if (request.type & NBD_CMD_FLAG_FUA) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001212 ret = blk_co_flush(exp->blk);
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001213 if (ret < 0) {
1214 LOG("flush failed");
1215 reply.error = -ret;
1216 goto error_reply;
1217 }
1218 }
1219
Max Reitzaadf99a2014-11-18 12:21:18 +01001220 ret = blk_read(exp->blk,
1221 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1222 req->data, request.len / BDRV_SECTOR_SIZE);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001223 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001224 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001225 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001226 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001227 }
bellard7a5ca862008-05-27 21:13:40 +00001228
Nick Thomasb2e3d872011-02-22 15:44:51 +00001229 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001230 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001231 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001232 break;
1233 case NBD_CMD_WRITE:
1234 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001235
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001236 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001237 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001238 reply.error = EROFS;
1239 goto error_reply;
1240 }
bellard7a5ca862008-05-27 21:13:40 +00001241
Paolo Bonzinifae69412011-09-19 16:04:36 +02001242 TRACE("Writing to device");
1243
Max Reitzaadf99a2014-11-18 12:21:18 +01001244 ret = blk_write(exp->blk,
1245 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1246 req->data, request.len / BDRV_SECTOR_SIZE);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001247 if (ret < 0) {
1248 LOG("writing to file failed");
1249 reply.error = -ret;
1250 goto error_reply;
1251 }
1252
1253 if (request.type & NBD_CMD_FLAG_FUA) {
Max Reitzaadf99a2014-11-18 12:21:18 +01001254 ret = blk_co_flush(exp->blk);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001255 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001256 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001257 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001258 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001259 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001260 }
bellard7a5ca862008-05-27 21:13:40 +00001261
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001262 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001263 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001264 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001265 break;
1266 case NBD_CMD_DISC:
1267 TRACE("Request type is DISCONNECT");
1268 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001269 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001270 case NBD_CMD_FLUSH:
1271 TRACE("Request type is FLUSH");
1272
Max Reitzaadf99a2014-11-18 12:21:18 +01001273 ret = blk_co_flush(exp->blk);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001274 if (ret < 0) {
1275 LOG("flush failed");
1276 reply.error = -ret;
1277 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001278 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001279 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001280 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001281 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001282 case NBD_CMD_TRIM:
1283 TRACE("Request type is TRIM");
Max Reitzaadf99a2014-11-18 12:21:18 +01001284 ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset)
1285 / BDRV_SECTOR_SIZE,
1286 request.len / BDRV_SECTOR_SIZE);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001287 if (ret < 0) {
1288 LOG("discard failed");
1289 reply.error = -ret;
1290 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001291 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001292 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001293 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001294 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001295 default:
1296 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001297 invalid_request:
1298 reply.error = -EINVAL;
1299 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001300 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001301 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001302 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001303 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001304 }
bellard7a5ca862008-05-27 21:13:40 +00001305
Nick Thomasb2e3d872011-02-22 15:44:51 +00001306 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001307
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001308done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001309 nbd_request_put(req);
1310 return;
1311
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001312out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001313 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +02001314 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001315}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001316
Paolo Bonzini1743b512011-09-19 14:33:23 +02001317static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001318{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001319 NBDClient *client = opaque;
1320
Paolo Bonzini262db382011-09-19 15:19:27 +02001321 if (client->recv_coroutine) {
1322 qemu_coroutine_enter(client->recv_coroutine, NULL);
1323 } else {
1324 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001325 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001326}
1327
Paolo Bonzini262db382011-09-19 15:19:27 +02001328static void nbd_restart_write(void *opaque)
1329{
1330 NBDClient *client = opaque;
1331
1332 qemu_coroutine_enter(client->send_coroutine, NULL);
1333}
1334
Max Reitz958c7172014-06-20 21:57:32 +02001335static void nbd_set_handlers(NBDClient *client)
1336{
1337 if (client->exp && client->exp->ctx) {
1338 aio_set_fd_handler(client->exp->ctx, client->sock,
1339 client->can_read ? nbd_read : NULL,
1340 client->send_coroutine ? nbd_restart_write : NULL,
1341 client);
1342 }
1343}
1344
1345static void nbd_unset_handlers(NBDClient *client)
1346{
1347 if (client->exp && client->exp->ctx) {
1348 aio_set_fd_handler(client->exp->ctx, client->sock, NULL, NULL, NULL);
1349 }
1350}
1351
1352static void nbd_update_can_read(NBDClient *client)
1353{
1354 bool can_read = client->recv_coroutine ||
1355 client->nb_requests < MAX_NBD_REQUESTS;
1356
1357 if (can_read != client->can_read) {
1358 client->can_read = can_read;
1359 nbd_set_handlers(client);
1360
1361 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1362 * in nbd_set_handlers() will have taken care of that */
1363 }
1364}
1365
Paolo Bonzini1743b512011-09-19 14:33:23 +02001366NBDClient *nbd_client_new(NBDExport *exp, int csock,
1367 void (*close)(NBDClient *))
1368{
1369 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001370 client = g_malloc0(sizeof(NBDClient));
1371 client->refcount = 1;
1372 client->exp = exp;
1373 client->sock = csock;
Max Reitz958c7172014-06-20 21:57:32 +02001374 client->can_read = true;
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001375 if (nbd_send_negotiate(client)) {
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001376 g_free(client);
1377 return NULL;
1378 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001379 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001380 qemu_co_mutex_init(&client->send_lock);
Max Reitz958c7172014-06-20 21:57:32 +02001381 nbd_set_handlers(client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001382
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001383 if (exp) {
1384 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1385 nbd_export_get(exp);
1386 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001387 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001388}