]> git.proxmox.com Git - mirror_qemu.git/blame - nbd/server.c
nbd-server: Use a separate BlockBackend
[mirror_qemu.git] / nbd / server.c
CommitLineData
75818250 1/*
7a5ca864
FB
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
798bfe00 4 * Network Block Device Server Side
7a5ca864
FB
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
8167ee88 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
75818250 17 */
7a5ca864 18
d38ea87a 19#include "qemu/osdep.h"
da34e65c 20#include "qapi/error.h"
798bfe00 21#include "nbd-internal.h"
ca441480
PB
22
23static int system_errno_to_nbd_errno(int err)
24{
25 switch (err) {
26 case 0:
27 return NBD_SUCCESS;
28 case EPERM:
c0301fcc 29 case EROFS:
ca441480
PB
30 return NBD_EPERM;
31 case EIO:
32 return NBD_EIO;
33 case ENOMEM:
34 return NBD_ENOMEM;
35#ifdef EDQUOT
36 case EDQUOT:
37#endif
38 case EFBIG:
39 case ENOSPC:
40 return NBD_ENOSPC;
41 case EINVAL:
42 default:
43 return NBD_EINVAL;
44 }
45}
46
9a304d29
PB
47/* Definitions for opaque data types */
48
49typedef struct NBDRequest NBDRequest;
50
51struct NBDRequest {
52 QSIMPLEQ_ENTRY(NBDRequest) entry;
53 NBDClient *client;
54 uint8_t *data;
29b6c3b3 55 bool complete;
9a304d29
PB
56};
57
58struct NBDExport {
2c8d9f06 59 int refcount;
0ddf08db
PB
60 void (*close)(NBDExport *exp);
61
aadf99a7 62 BlockBackend *blk;
ee0a19ec 63 char *name;
9a304d29
PB
64 off_t dev_offset;
65 off_t size;
7423f417 66 uint16_t nbdflags;
4b9441f6 67 QTAILQ_HEAD(, NBDClient) clients;
ee0a19ec 68 QTAILQ_ENTRY(NBDExport) next;
958c717d
HR
69
70 AioContext *ctx;
741cc431 71
cd7fca95 72 BlockBackend *eject_notifier_blk;
741cc431 73 Notifier eject_notifier;
9a304d29
PB
74};
75
ee0a19ec
PB
76static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
77
9a304d29
PB
78struct NBDClient {
79 int refcount;
80 void (*close)(NBDClient *client);
81
82 NBDExport *exp;
f95910fe
DB
83 QCryptoTLSCreds *tlscreds;
84 char *tlsaclname;
1c778ef7
DB
85 QIOChannelSocket *sioc; /* The underlying data channel */
86 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
9a304d29
PB
87
88 Coroutine *recv_coroutine;
89
90 CoMutex send_lock;
91 Coroutine *send_coroutine;
92
958c717d
HR
93 bool can_read;
94
4b9441f6 95 QTAILQ_ENTRY(NBDClient) next;
9a304d29 96 int nb_requests;
ff2b68aa 97 bool closing;
9a304d29
PB
98};
99
7a5ca864
FB
100/* That's all folks */
101
958c717d
HR
102static void nbd_set_handlers(NBDClient *client);
103static void nbd_unset_handlers(NBDClient *client);
104static void nbd_update_can_read(NBDClient *client);
105
1c778ef7
DB
106static gboolean nbd_negotiate_continue(QIOChannel *ioc,
107 GIOCondition condition,
108 void *opaque)
1a6245a5 109{
0b8b8753 110 qemu_coroutine_enter(opaque);
1c778ef7 111 return TRUE;
1a6245a5
FZ
112}
113
1c778ef7 114static ssize_t nbd_negotiate_read(QIOChannel *ioc, void *buffer, size_t size)
1a6245a5
FZ
115{
116 ssize_t ret;
1c778ef7 117 guint watch;
1a6245a5
FZ
118
119 assert(qemu_in_coroutine());
120 /* Negotiation are always in main loop. */
1c778ef7
DB
121 watch = qio_channel_add_watch(ioc,
122 G_IO_IN,
123 nbd_negotiate_continue,
124 qemu_coroutine_self(),
125 NULL);
126 ret = read_sync(ioc, buffer, size);
127 g_source_remove(watch);
1a6245a5
FZ
128 return ret;
129
130}
131
1c778ef7 132static ssize_t nbd_negotiate_write(QIOChannel *ioc, void *buffer, size_t size)
1a6245a5
FZ
133{
134 ssize_t ret;
1c778ef7 135 guint watch;
1a6245a5
FZ
136
137 assert(qemu_in_coroutine());
138 /* Negotiation are always in main loop. */
1c778ef7
DB
139 watch = qio_channel_add_watch(ioc,
140 G_IO_OUT,
141 nbd_negotiate_continue,
142 qemu_coroutine_self(),
143 NULL);
144 ret = write_sync(ioc, buffer, size);
145 g_source_remove(watch);
1a6245a5
FZ
146 return ret;
147}
148
1c778ef7 149static ssize_t nbd_negotiate_drop_sync(QIOChannel *ioc, size_t size)
0379f474
HR
150{
151 ssize_t ret, dropped = size;
152 uint8_t *buffer = g_malloc(MIN(65536, size));
153
154 while (size > 0) {
1c778ef7 155 ret = nbd_negotiate_read(ioc, buffer, MIN(65536, size));
0379f474
HR
156 if (ret < 0) {
157 g_free(buffer);
158 return ret;
159 }
160
161 assert(ret <= size);
162 size -= ret;
163 }
164
165 g_free(buffer);
166 return dropped;
167}
168
6b8c01e7 169/* Basic flow for negotiation
7a5ca864
FB
170
171 Server Client
7a5ca864 172 Negotiate
6b8c01e7
PB
173
174 or
175
176 Server Client
177 Negotiate #1
178 Option
179 Negotiate #2
180
181 ----
182
183 followed by
184
185 Server Client
7a5ca864
FB
186 Request
187 Response
188 Request
189 Response
190 ...
191 ...
192 Request (type == 2)
6b8c01e7 193
7a5ca864
FB
194*/
195
1c778ef7 196static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt)
6b8c01e7 197{
6b8c01e7 198 uint64_t magic;
f5076b5a 199 uint32_t len;
6b8c01e7 200
2cb34749 201 TRACE("Reply opt=%" PRIx32 " type=%" PRIx32, type, opt);
f95910fe 202
f5076b5a 203 magic = cpu_to_be64(NBD_REP_MAGIC);
1c778ef7 204 if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
f5076b5a
HB
205 LOG("write failed (rep magic)");
206 return -EINVAL;
6b8c01e7 207 }
f5076b5a 208 opt = cpu_to_be32(opt);
1c778ef7 209 if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
f5076b5a
HB
210 LOG("write failed (rep opt)");
211 return -EINVAL;
6b8c01e7 212 }
f5076b5a 213 type = cpu_to_be32(type);
1c778ef7 214 if (nbd_negotiate_write(ioc, &type, sizeof(type)) != sizeof(type)) {
f5076b5a
HB
215 LOG("write failed (rep type)");
216 return -EINVAL;
6b8c01e7 217 }
f5076b5a 218 len = cpu_to_be32(0);
1c778ef7 219 if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
f5076b5a
HB
220 LOG("write failed (rep data length)");
221 return -EINVAL;
6b8c01e7 222 }
f5076b5a
HB
223 return 0;
224}
6b8c01e7 225
1c778ef7 226static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
32d7d2e0
HB
227{
228 uint64_t magic, name_len;
229 uint32_t opt, type, len;
230
2cb34749 231 TRACE("Advertising export name '%s'", exp->name ? exp->name : "");
32d7d2e0
HB
232 name_len = strlen(exp->name);
233 magic = cpu_to_be64(NBD_REP_MAGIC);
1c778ef7 234 if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
32d7d2e0
HB
235 LOG("write failed (magic)");
236 return -EINVAL;
237 }
238 opt = cpu_to_be32(NBD_OPT_LIST);
1c778ef7 239 if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
32d7d2e0
HB
240 LOG("write failed (opt)");
241 return -EINVAL;
242 }
243 type = cpu_to_be32(NBD_REP_SERVER);
1c778ef7 244 if (nbd_negotiate_write(ioc, &type, sizeof(type)) != sizeof(type)) {
32d7d2e0
HB
245 LOG("write failed (reply type)");
246 return -EINVAL;
247 }
248 len = cpu_to_be32(name_len + sizeof(len));
1c778ef7 249 if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
32d7d2e0
HB
250 LOG("write failed (length)");
251 return -EINVAL;
252 }
253 len = cpu_to_be32(name_len);
1c778ef7 254 if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
32d7d2e0
HB
255 LOG("write failed (length)");
256 return -EINVAL;
257 }
1c778ef7 258 if (nbd_negotiate_write(ioc, exp->name, name_len) != name_len) {
32d7d2e0
HB
259 LOG("write failed (buffer)");
260 return -EINVAL;
261 }
262 return 0;
263}
264
1a6245a5 265static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
32d7d2e0 266{
32d7d2e0
HB
267 NBDExport *exp;
268
32d7d2e0 269 if (length) {
1c778ef7 270 if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
0379f474
HR
271 return -EIO;
272 }
1c778ef7
DB
273 return nbd_negotiate_send_rep(client->ioc,
274 NBD_REP_ERR_INVALID, NBD_OPT_LIST);
32d7d2e0
HB
275 }
276
277 /* For each export, send a NBD_REP_SERVER reply. */
278 QTAILQ_FOREACH(exp, &exports, next) {
1c778ef7 279 if (nbd_negotiate_send_rep_list(client->ioc, exp)) {
32d7d2e0
HB
280 return -EINVAL;
281 }
282 }
283 /* Finish with a NBD_REP_ACK. */
1c778ef7 284 return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST);
32d7d2e0
HB
285}
286
1a6245a5 287static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length)
f5076b5a 288{
1c778ef7 289 int rc = -EINVAL;
943cec86 290 char name[NBD_MAX_NAME_SIZE + 1];
6b8c01e7 291
f5076b5a
HB
292 /* Client sends:
293 [20 .. xx] export name (length bytes)
294 */
6b8c01e7 295 TRACE("Checking length");
943cec86 296 if (length >= sizeof(name)) {
6b8c01e7
PB
297 LOG("Bad length received");
298 goto fail;
299 }
1c778ef7 300 if (nbd_negotiate_read(client->ioc, name, length) != length) {
6b8c01e7
PB
301 LOG("read failed");
302 goto fail;
303 }
304 name[length] = '\0';
305
9344e5f5
DB
306 TRACE("Client requested export '%s'", name);
307
6b8c01e7
PB
308 client->exp = nbd_export_find(name);
309 if (!client->exp) {
310 LOG("export not found");
311 goto fail;
312 }
313
314 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
315 nbd_export_get(client->exp);
6b8c01e7
PB
316 rc = 0;
317fail:
318 return rc;
319}
320
f95910fe
DB
321
322static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
323 uint32_t length)
324{
325 QIOChannel *ioc;
326 QIOChannelTLS *tioc;
327 struct NBDTLSHandshakeData data = { 0 };
328
329 TRACE("Setting up TLS");
330 ioc = client->ioc;
331 if (length) {
332 if (nbd_negotiate_drop_sync(ioc, length) != length) {
333 return NULL;
334 }
335 nbd_negotiate_send_rep(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS);
336 return NULL;
337 }
338
63d5ef86
EB
339 if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
340 NBD_OPT_STARTTLS) < 0) {
341 return NULL;
342 }
f95910fe
DB
343
344 tioc = qio_channel_tls_new_server(ioc,
345 client->tlscreds,
346 client->tlsaclname,
347 NULL);
348 if (!tioc) {
349 return NULL;
350 }
351
352 TRACE("Starting TLS handshake");
353 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
354 qio_channel_tls_handshake(tioc,
355 nbd_tls_handshake,
356 &data,
357 NULL);
358
359 if (!data.complete) {
360 g_main_loop_run(data.loop);
361 }
362 g_main_loop_unref(data.loop);
363 if (data.error) {
364 object_unref(OBJECT(tioc));
365 error_free(data.error);
366 return NULL;
367 }
368
369 return QIO_CHANNEL(tioc);
370}
371
372
1a6245a5 373static int nbd_negotiate_options(NBDClient *client)
f5076b5a 374{
9c122ada 375 uint32_t flags;
26afa868 376 bool fixedNewstyle = false;
9c122ada
HR
377
378 /* Client sends:
379 [ 0 .. 3] client flags
380
381 [ 0 .. 7] NBD_OPTS_MAGIC
382 [ 8 .. 11] NBD option
383 [12 .. 15] Data length
384 ... Rest of request
385
386 [ 0 .. 7] NBD_OPTS_MAGIC
387 [ 8 .. 11] Second NBD option
388 [12 .. 15] Data length
389 ... Rest of request
390 */
391
1c778ef7
DB
392 if (nbd_negotiate_read(client->ioc, &flags, sizeof(flags)) !=
393 sizeof(flags)) {
9c122ada
HR
394 LOG("read failed");
395 return -EIO;
396 }
397 TRACE("Checking client flags");
398 be32_to_cpus(&flags);
26afa868 399 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
2cb34749 400 TRACE("Client supports fixed newstyle handshake");
26afa868
DB
401 fixedNewstyle = true;
402 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
403 }
404 if (flags != 0) {
2cb34749 405 TRACE("Unknown client flags 0x%" PRIx32 " received", flags);
9c122ada
HR
406 return -EIO;
407 }
408
f5076b5a 409 while (1) {
9c122ada 410 int ret;
26afa868 411 uint32_t clientflags, length;
f5076b5a
HB
412 uint64_t magic;
413
1c778ef7
DB
414 if (nbd_negotiate_read(client->ioc, &magic, sizeof(magic)) !=
415 sizeof(magic)) {
f5076b5a
HB
416 LOG("read failed");
417 return -EINVAL;
418 }
419 TRACE("Checking opts magic");
420 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
421 LOG("Bad magic received");
422 return -EINVAL;
423 }
424
26afa868
DB
425 if (nbd_negotiate_read(client->ioc, &clientflags,
426 sizeof(clientflags)) != sizeof(clientflags)) {
f5076b5a
HB
427 LOG("read failed");
428 return -EINVAL;
429 }
26afa868 430 clientflags = be32_to_cpu(clientflags);
f5076b5a 431
1c778ef7
DB
432 if (nbd_negotiate_read(client->ioc, &length, sizeof(length)) !=
433 sizeof(length)) {
f5076b5a
HB
434 LOG("read failed");
435 return -EINVAL;
436 }
437 length = be32_to_cpu(length);
438
2cb34749 439 TRACE("Checking option 0x%" PRIx32, clientflags);
f95910fe
DB
440 if (client->tlscreds &&
441 client->ioc == (QIOChannel *)client->sioc) {
442 QIOChannel *tioc;
443 if (!fixedNewstyle) {
2cb34749 444 TRACE("Unsupported option 0x%" PRIx32, clientflags);
f95910fe
DB
445 return -EINVAL;
446 }
447 switch (clientflags) {
448 case NBD_OPT_STARTTLS:
449 tioc = nbd_negotiate_handle_starttls(client, length);
450 if (!tioc) {
451 return -EIO;
452 }
453 object_unref(OBJECT(client->ioc));
454 client->ioc = QIO_CHANNEL(tioc);
455 break;
456
d1129a8a
EB
457 case NBD_OPT_EXPORT_NAME:
458 /* No way to return an error to client, so drop connection */
459 TRACE("Option 0x%x not permitted before TLS", clientflags);
460 return -EINVAL;
461
f95910fe 462 default:
2cb34749
EB
463 TRACE("Option 0x%" PRIx32 " not permitted before TLS",
464 clientflags);
d1129a8a
EB
465 if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
466 return -EIO;
467 }
63d5ef86
EB
468 ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_TLS_REQD,
469 clientflags);
470 if (ret < 0) {
471 return ret;
472 }
d1129a8a 473 break;
f95910fe
DB
474 }
475 } else if (fixedNewstyle) {
26afa868
DB
476 switch (clientflags) {
477 case NBD_OPT_LIST:
478 ret = nbd_negotiate_handle_list(client, length);
479 if (ret < 0) {
480 return ret;
481 }
482 break;
483
484 case NBD_OPT_ABORT:
485 return -EINVAL;
486
487 case NBD_OPT_EXPORT_NAME:
488 return nbd_negotiate_handle_export_name(client, length);
489
f95910fe 490 case NBD_OPT_STARTTLS:
d1129a8a
EB
491 if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
492 return -EIO;
493 }
f95910fe
DB
494 if (client->tlscreds) {
495 TRACE("TLS already enabled");
63d5ef86
EB
496 ret = nbd_negotiate_send_rep(client->ioc,
497 NBD_REP_ERR_INVALID,
498 clientflags);
f95910fe
DB
499 } else {
500 TRACE("TLS not configured");
63d5ef86
EB
501 ret = nbd_negotiate_send_rep(client->ioc,
502 NBD_REP_ERR_POLICY,
503 clientflags);
504 }
505 if (ret < 0) {
506 return ret;
f95910fe 507 }
d1129a8a 508 break;
26afa868 509 default:
2cb34749 510 TRACE("Unsupported option 0x%" PRIx32, clientflags);
156f6a10
EB
511 if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
512 return -EIO;
513 }
63d5ef86
EB
514 ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_UNSUP,
515 clientflags);
516 if (ret < 0) {
517 return ret;
518 }
156f6a10 519 break;
26afa868
DB
520 }
521 } else {
522 /*
523 * If broken new-style we should drop the connection
524 * for anything except NBD_OPT_EXPORT_NAME
525 */
526 switch (clientflags) {
527 case NBD_OPT_EXPORT_NAME:
528 return nbd_negotiate_handle_export_name(client, length);
529
530 default:
2cb34749 531 TRACE("Unsupported option 0x%" PRIx32, clientflags);
26afa868 532 return -EINVAL;
32d7d2e0 533 }
f5076b5a
HB
534 }
535 }
536}
537
1a6245a5
FZ
538typedef struct {
539 NBDClient *client;
540 Coroutine *co;
541} NBDClientNewData;
542
543static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
7a5ca864 544{
1a6245a5 545 NBDClient *client = data->client;
b2e3d87f 546 char buf[8 + 8 + 8 + 128];
185b4338 547 int rc;
7423f417
EB
548 const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
549 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
f95910fe 550 bool oldStyle;
b2e3d87f 551
f95910fe 552 /* Old style negotiation header without options
6b8c01e7
PB
553 [ 0 .. 7] passwd ("NBDMAGIC")
554 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
b2e3d87f 555 [16 .. 23] size
6b8c01e7 556 [24 .. 25] server flags (0)
5672ee54 557 [26 .. 27] export flags
6b8c01e7
PB
558 [28 .. 151] reserved (0)
559
f95910fe 560 New style negotiation header with options
6b8c01e7
PB
561 [ 0 .. 7] passwd ("NBDMAGIC")
562 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
563 [16 .. 17] server flags (0)
f95910fe 564 ....options sent....
6b8c01e7
PB
565 [18 .. 25] size
566 [26 .. 27] export flags
567 [28 .. 151] reserved (0)
b2e3d87f
NT
568 */
569
1c778ef7 570 qio_channel_set_blocking(client->ioc, false, NULL);
185b4338
PB
571 rc = -EINVAL;
572
b2e3d87f 573 TRACE("Beginning negotiation.");
8ffaaba0 574 memset(buf, 0, sizeof(buf));
b2e3d87f 575 memcpy(buf, "NBDMAGIC", 8);
f95910fe
DB
576
577 oldStyle = client->exp != NULL && !client->tlscreds;
578 if (oldStyle) {
2cb34749
EB
579 TRACE("advertising size %" PRIu64 " and flags %x",
580 client->exp->size, client->exp->nbdflags | myflags);
667ad26f
JS
581 stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
582 stq_be_p(buf + 16, client->exp->size);
583 stw_be_p(buf + 26, client->exp->nbdflags | myflags);
6b8c01e7 584 } else {
667ad26f
JS
585 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
586 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE);
6b8c01e7 587 }
b2e3d87f 588
f95910fe
DB
589 if (oldStyle) {
590 if (client->tlscreds) {
591 TRACE("TLS cannot be enabled with oldstyle protocol");
592 goto fail;
593 }
1c778ef7 594 if (nbd_negotiate_write(client->ioc, buf, sizeof(buf)) != sizeof(buf)) {
6b8c01e7
PB
595 LOG("write failed");
596 goto fail;
597 }
598 } else {
1c778ef7 599 if (nbd_negotiate_write(client->ioc, buf, 18) != 18) {
6b8c01e7
PB
600 LOG("write failed");
601 goto fail;
602 }
1a6245a5 603 rc = nbd_negotiate_options(client);
f5076b5a 604 if (rc != 0) {
6b8c01e7
PB
605 LOG("option negotiation failed");
606 goto fail;
607 }
608
2cb34749
EB
609 TRACE("advertising size %" PRIu64 " and flags %x",
610 client->exp->size, client->exp->nbdflags | myflags);
667ad26f
JS
611 stq_be_p(buf + 18, client->exp->size);
612 stw_be_p(buf + 26, client->exp->nbdflags | myflags);
1c778ef7
DB
613 if (nbd_negotiate_write(client->ioc, buf + 18, sizeof(buf) - 18) !=
614 sizeof(buf) - 18) {
6b8c01e7
PB
615 LOG("write failed");
616 goto fail;
617 }
b2e3d87f
NT
618 }
619
07f35073 620 TRACE("Negotiation succeeded.");
185b4338
PB
621 rc = 0;
622fail:
623 return rc;
7a5ca864
FB
624}
625
1c778ef7 626static ssize_t nbd_receive_request(QIOChannel *ioc, struct nbd_request *request)
75818250 627{
fa26c26b 628 uint8_t buf[NBD_REQUEST_SIZE];
b2e3d87f 629 uint32_t magic;
185b4338 630 ssize_t ret;
b2e3d87f 631
1c778ef7 632 ret = read_sync(ioc, buf, sizeof(buf));
185b4338
PB
633 if (ret < 0) {
634 return ret;
635 }
636
637 if (ret != sizeof(buf)) {
b2e3d87f 638 LOG("read failed");
185b4338 639 return -EINVAL;
b2e3d87f
NT
640 }
641
642 /* Request
643 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
644 [ 4 .. 7] type (0 == READ, 1 == WRITE)
645 [ 8 .. 15] handle
646 [16 .. 23] from
647 [24 .. 27] len
648 */
649
773dce3c
PM
650 magic = ldl_be_p(buf);
651 request->type = ldl_be_p(buf + 4);
652 request->handle = ldq_be_p(buf + 8);
653 request->from = ldq_be_p(buf + 16);
654 request->len = ldl_be_p(buf + 24);
b2e3d87f 655
2cb34749
EB
656 TRACE("Got request: { magic = 0x%" PRIx32 ", .type = %" PRIx32
657 ", from = %" PRIu64 " , len = %" PRIu32 " }",
b2e3d87f
NT
658 magic, request->type, request->from, request->len);
659
660 if (magic != NBD_REQUEST_MAGIC) {
2cb34749 661 LOG("invalid magic (got 0x%" PRIx32 ")", magic);
185b4338 662 return -EINVAL;
b2e3d87f
NT
663 }
664 return 0;
75818250
TS
665}
666
1c778ef7 667static ssize_t nbd_send_reply(QIOChannel *ioc, struct nbd_reply *reply)
75818250 668{
fa26c26b 669 uint8_t buf[NBD_REPLY_SIZE];
185b4338 670 ssize_t ret;
b2e3d87f 671
ca441480
PB
672 reply->error = system_errno_to_nbd_errno(reply->error);
673
2cb34749
EB
674 TRACE("Sending response to client: { .error = %" PRId32
675 ", handle = %" PRIu64 " }",
7548fe31
EB
676 reply->error, reply->handle);
677
b2e3d87f
NT
678 /* Reply
679 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
680 [ 4 .. 7] error (0 == no error)
681 [ 7 .. 15] handle
682 */
667ad26f
JS
683 stl_be_p(buf, NBD_REPLY_MAGIC);
684 stl_be_p(buf + 4, reply->error);
685 stq_be_p(buf + 8, reply->handle);
b2e3d87f 686
1c778ef7 687 ret = write_sync(ioc, buf, sizeof(buf));
185b4338
PB
688 if (ret < 0) {
689 return ret;
690 }
691
692 if (ret != sizeof(buf)) {
b2e3d87f 693 LOG("writing to socket failed");
185b4338 694 return -EINVAL;
b2e3d87f
NT
695 }
696 return 0;
75818250 697}
7a5ca864 698
41996e38
PB
699#define MAX_NBD_REQUESTS 16
700
ce33967a 701void nbd_client_get(NBDClient *client)
1743b515
PB
702{
703 client->refcount++;
704}
705
ce33967a 706void nbd_client_put(NBDClient *client)
1743b515
PB
707{
708 if (--client->refcount == 0) {
ff2b68aa 709 /* The last reference should be dropped by client->close,
f53a829b 710 * which is called by client_close.
ff2b68aa
PB
711 */
712 assert(client->closing);
713
958c717d 714 nbd_unset_handlers(client);
1c778ef7
DB
715 object_unref(OBJECT(client->sioc));
716 object_unref(OBJECT(client->ioc));
f95910fe
DB
717 if (client->tlscreds) {
718 object_unref(OBJECT(client->tlscreds));
719 }
720 g_free(client->tlsaclname);
6b8c01e7
PB
721 if (client->exp) {
722 QTAILQ_REMOVE(&client->exp->clients, client, next);
723 nbd_export_put(client->exp);
724 }
1743b515
PB
725 g_free(client);
726 }
727}
728
f53a829b 729static void client_close(NBDClient *client)
1743b515 730{
ff2b68aa
PB
731 if (client->closing) {
732 return;
733 }
734
735 client->closing = true;
736
737 /* Force requests to finish. They will drop their own references,
738 * then we'll close the socket and free the NBDClient.
739 */
1c778ef7
DB
740 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
741 NULL);
ff2b68aa
PB
742
743 /* Also tell the client, so that they release their reference. */
1743b515
PB
744 if (client->close) {
745 client->close(client);
746 }
1743b515
PB
747}
748
72deddc5 749static NBDRequest *nbd_request_get(NBDClient *client)
d9a73806
PB
750{
751 NBDRequest *req;
72deddc5 752
41996e38
PB
753 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
754 client->nb_requests++;
958c717d 755 nbd_update_can_read(client);
41996e38 756
1729404c 757 req = g_new0(NBDRequest, 1);
72deddc5
PB
758 nbd_client_get(client);
759 req->client = client;
d9a73806
PB
760 return req;
761}
762
72deddc5 763static void nbd_request_put(NBDRequest *req)
d9a73806 764{
72deddc5 765 NBDClient *client = req->client;
e1adb27a 766
2d821488
SH
767 if (req->data) {
768 qemu_vfree(req->data);
769 }
1729404c 770 g_free(req);
e1adb27a 771
958c717d
HR
772 client->nb_requests--;
773 nbd_update_can_read(client);
72deddc5 774 nbd_client_put(client);
d9a73806
PB
775}
776
aadf99a7 777static void blk_aio_attached(AioContext *ctx, void *opaque)
f2149281
HR
778{
779 NBDExport *exp = opaque;
780 NBDClient *client;
781
782 TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx);
783
784 exp->ctx = ctx;
785
786 QTAILQ_FOREACH(client, &exp->clients, next) {
787 nbd_set_handlers(client);
788 }
789}
790
aadf99a7 791static void blk_aio_detach(void *opaque)
f2149281
HR
792{
793 NBDExport *exp = opaque;
794 NBDClient *client;
795
796 TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx);
797
798 QTAILQ_FOREACH(client, &exp->clients, next) {
799 nbd_unset_handlers(client);
800 }
801
802 exp->ctx = NULL;
803}
804
741cc431
HR
805static void nbd_eject_notifier(Notifier *n, void *data)
806{
807 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
808 nbd_export_close(exp);
809}
810
cd7fca95 811NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7423f417 812 uint16_t nbdflags, void (*close)(NBDExport *),
cd7fca95 813 bool writethrough, BlockBackend *on_eject_blk,
98f44bbe 814 Error **errp)
af49bbbe 815{
cd7fca95 816 BlockBackend *blk;
af49bbbe 817 NBDExport *exp = g_malloc0(sizeof(NBDExport));
cd7fca95
KW
818
819 blk = blk_new();
820 blk_insert_bs(blk, bs);
821 blk_set_enable_write_cache(blk, !writethrough);
822
2c8d9f06 823 exp->refcount = 1;
4b9441f6 824 QTAILQ_INIT(&exp->clients);
aadf99a7 825 exp->blk = blk;
af49bbbe
PB
826 exp->dev_offset = dev_offset;
827 exp->nbdflags = nbdflags;
98f44bbe
HR
828 exp->size = size < 0 ? blk_getlength(blk) : size;
829 if (exp->size < 0) {
830 error_setg_errno(errp, -exp->size,
831 "Failed to determine the NBD export's length");
832 goto fail;
833 }
834 exp->size -= exp->size % BDRV_SECTOR_SIZE;
835
0ddf08db 836 exp->close = close;
aadf99a7 837 exp->ctx = blk_get_aio_context(blk);
aadf99a7 838 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
741cc431 839
cd7fca95
KW
840 if (on_eject_blk) {
841 blk_ref(on_eject_blk);
842 exp->eject_notifier_blk = on_eject_blk;
843 exp->eject_notifier.notify = nbd_eject_notifier;
844 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
845 }
741cc431 846
7ea2d269
AK
847 /*
848 * NBD exports are used for non-shared storage migration. Make sure
04c01a5c 849 * that BDRV_O_INACTIVE is cleared and the image is ready for write
7ea2d269
AK
850 * access since the export could be available before migration handover.
851 */
e5f3e12e 852 aio_context_acquire(exp->ctx);
aadf99a7 853 blk_invalidate_cache(blk, NULL);
e5f3e12e 854 aio_context_release(exp->ctx);
af49bbbe 855 return exp;
98f44bbe
HR
856
857fail:
cd7fca95 858 blk_unref(blk);
98f44bbe
HR
859 g_free(exp);
860 return NULL;
af49bbbe
PB
861}
862
ee0a19ec
PB
863NBDExport *nbd_export_find(const char *name)
864{
865 NBDExport *exp;
866 QTAILQ_FOREACH(exp, &exports, next) {
867 if (strcmp(name, exp->name) == 0) {
868 return exp;
869 }
870 }
871
872 return NULL;
873}
874
875void nbd_export_set_name(NBDExport *exp, const char *name)
876{
877 if (exp->name == name) {
878 return;
879 }
880
881 nbd_export_get(exp);
882 if (exp->name != NULL) {
883 g_free(exp->name);
884 exp->name = NULL;
885 QTAILQ_REMOVE(&exports, exp, next);
886 nbd_export_put(exp);
887 }
888 if (name != NULL) {
889 nbd_export_get(exp);
890 exp->name = g_strdup(name);
891 QTAILQ_INSERT_TAIL(&exports, exp, next);
892 }
893 nbd_export_put(exp);
894}
895
af49bbbe
PB
896void nbd_export_close(NBDExport *exp)
897{
4b9441f6 898 NBDClient *client, *next;
2c8d9f06 899
4b9441f6
PB
900 nbd_export_get(exp);
901 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
f53a829b 902 client_close(client);
4b9441f6 903 }
125afda8 904 nbd_export_set_name(exp, NULL);
4b9441f6 905 nbd_export_put(exp);
2c8d9f06
PB
906}
907
908void nbd_export_get(NBDExport *exp)
909{
910 assert(exp->refcount > 0);
911 exp->refcount++;
912}
913
914void nbd_export_put(NBDExport *exp)
915{
916 assert(exp->refcount > 0);
917 if (exp->refcount == 1) {
918 nbd_export_close(exp);
d9a73806
PB
919 }
920
2c8d9f06 921 if (--exp->refcount == 0) {
ee0a19ec
PB
922 assert(exp->name == NULL);
923
0ddf08db
PB
924 if (exp->close) {
925 exp->close(exp);
926 }
927
d6268348 928 if (exp->blk) {
cd7fca95
KW
929 if (exp->eject_notifier_blk) {
930 notifier_remove(&exp->eject_notifier);
931 blk_unref(exp->eject_notifier_blk);
932 }
d6268348
WC
933 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
934 blk_aio_detach, exp);
935 blk_unref(exp->blk);
936 exp->blk = NULL;
937 }
938
2c8d9f06
PB
939 g_free(exp);
940 }
af49bbbe
PB
941}
942
e140177d 943BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
125afda8 944{
aadf99a7 945 return exp->blk;
125afda8
PB
946}
947
ee0a19ec
PB
948void nbd_export_close_all(void)
949{
950 NBDExport *exp, *next;
951
952 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
953 nbd_export_close(exp);
ee0a19ec
PB
954 }
955}
956
94e7340b
PB
957static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
958 int len)
22045592 959{
72deddc5 960 NBDClient *client = req->client;
94e7340b 961 ssize_t rc, ret;
22045592 962
1c778ef7 963 g_assert(qemu_in_coroutine());
262db388 964 qemu_co_mutex_lock(&client->send_lock);
262db388 965 client->send_coroutine = qemu_coroutine_self();
958c717d 966 nbd_set_handlers(client);
262db388 967
22045592 968 if (!len) {
1c778ef7 969 rc = nbd_send_reply(client->ioc, reply);
22045592 970 } else {
1c778ef7
DB
971 qio_channel_set_cork(client->ioc, true);
972 rc = nbd_send_reply(client->ioc, reply);
fc19f8a0 973 if (rc >= 0) {
1c778ef7 974 ret = write_sync(client->ioc, req->data, len);
22045592 975 if (ret != len) {
185b4338 976 rc = -EIO;
22045592
PB
977 }
978 }
1c778ef7 979 qio_channel_set_cork(client->ioc, false);
22045592 980 }
262db388
PB
981
982 client->send_coroutine = NULL;
958c717d 983 nbd_set_handlers(client);
262db388 984 qemu_co_mutex_unlock(&client->send_lock);
22045592
PB
985 return rc;
986}
987
29b6c3b3
EB
988/* Collect a client request. Return 0 if request looks valid, -EAGAIN
989 * to keep trying the collection, -EIO to drop connection right away,
990 * and any other negative value to report an error to the client
991 * (although the caller may still need to disconnect after reporting
992 * the error). */
993static ssize_t nbd_co_receive_request(NBDRequest *req,
994 struct nbd_request *request)
a030b347 995{
72deddc5 996 NBDClient *client = req->client;
2d821488 997 uint32_t command;
94e7340b 998 ssize_t rc;
a030b347 999
1c778ef7 1000 g_assert(qemu_in_coroutine());
262db388 1001 client->recv_coroutine = qemu_coroutine_self();
958c717d
HR
1002 nbd_update_can_read(client);
1003
1c778ef7 1004 rc = nbd_receive_request(client->ioc, request);
7fe7b68b
PB
1005 if (rc < 0) {
1006 if (rc != -EAGAIN) {
1007 rc = -EIO;
1008 }
a030b347
PB
1009 goto out;
1010 }
1011
29b6c3b3
EB
1012 TRACE("Decoding type");
1013
1014 command = request->type & NBD_CMD_MASK_COMMAND;
1015 if (command != NBD_CMD_WRITE) {
1016 /* No payload, we are ready to read the next request. */
1017 req->complete = true;
1018 }
1019
1020 if (command == NBD_CMD_DISC) {
1021 /* Special case: we're going to disconnect without a reply,
1022 * whether or not flags, from, or len are bogus */
1023 TRACE("Request type is DISCONNECT");
1024 rc = -EIO;
1025 goto out;
1026 }
1027
1028 /* Check for sanity in the parameters, part 1. Defer as many
1029 * checks as possible until after reading any NBD_CMD_WRITE
1030 * payload, so we can try and keep the connection alive. */
a030b347 1031 if ((request->from + request->len) < request->from) {
29b6c3b3 1032 LOG("integer overflow detected, you're probably being attacked");
a030b347
PB
1033 rc = -EINVAL;
1034 goto out;
1035 }
1036
2d821488 1037 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
eb38c3b6 1038 if (request->len > NBD_MAX_BUFFER_SIZE) {
2cb34749 1039 LOG("len (%" PRIu32" ) is larger than max len (%u)",
eb38c3b6
PB
1040 request->len, NBD_MAX_BUFFER_SIZE);
1041 rc = -EINVAL;
1042 goto out;
1043 }
1044
f1c17521
PB
1045 req->data = blk_try_blockalign(client->exp->blk, request->len);
1046 if (req->data == NULL) {
1047 rc = -ENOMEM;
1048 goto out;
1049 }
2d821488
SH
1050 }
1051 if (command == NBD_CMD_WRITE) {
2cb34749 1052 TRACE("Reading %" PRIu32 " byte(s)", request->len);
a030b347 1053
1c778ef7 1054 if (read_sync(client->ioc, req->data, request->len) != request->len) {
a030b347
PB
1055 LOG("reading from socket failed");
1056 rc = -EIO;
1057 goto out;
1058 }
29b6c3b3 1059 req->complete = true;
a030b347 1060 }
29b6c3b3
EB
1061
1062 /* Sanity checks, part 2. */
1063 if (request->from + request->len > client->exp->size) {
1064 LOG("operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
1065 ", Size: %" PRIu64, request->from, request->len,
1066 (uint64_t)client->exp->size);
1067 rc = command == NBD_CMD_WRITE ? -ENOSPC : -EINVAL;
1068 goto out;
1069 }
ab7c548e
EB
1070 if (request->type & ~NBD_CMD_MASK_COMMAND & ~NBD_CMD_FLAG_FUA) {
1071 LOG("unsupported flags (got 0x%x)",
1072 request->type & ~NBD_CMD_MASK_COMMAND);
5bee0f47
EB
1073 rc = -EINVAL;
1074 goto out;
ab7c548e 1075 }
29b6c3b3 1076
a030b347
PB
1077 rc = 0;
1078
1079out:
262db388 1080 client->recv_coroutine = NULL;
958c717d
HR
1081 nbd_update_can_read(client);
1082
a030b347
PB
1083 return rc;
1084}
1085
262db388 1086static void nbd_trip(void *opaque)
75818250 1087{
262db388 1088 NBDClient *client = opaque;
1743b515 1089 NBDExport *exp = client->exp;
ff2b68aa 1090 NBDRequest *req;
b2e3d87f
NT
1091 struct nbd_request request;
1092 struct nbd_reply reply;
94e7340b 1093 ssize_t ret;
8c5d1abb 1094 uint32_t command;
a0c30369 1095 int flags;
b2e3d87f
NT
1096
1097 TRACE("Reading request.");
ff2b68aa
PB
1098 if (client->closing) {
1099 return;
1100 }
b2e3d87f 1101
ff2b68aa 1102 req = nbd_request_get(client);
262db388 1103 ret = nbd_co_receive_request(req, &request);
7fe7b68b
PB
1104 if (ret == -EAGAIN) {
1105 goto done;
1106 }
a030b347 1107 if (ret == -EIO) {
d9a73806 1108 goto out;
a030b347 1109 }
b2e3d87f 1110
fae69416
PB
1111 reply.handle = request.handle;
1112 reply.error = 0;
1113
a030b347
PB
1114 if (ret < 0) {
1115 reply.error = -ret;
1116 goto error_reply;
b2e3d87f 1117 }
8c5d1abb 1118 command = request.type & NBD_CMD_MASK_COMMAND;
b2e3d87f 1119
d6268348
WC
1120 if (client->closing) {
1121 /*
1122 * The client may be closed when we are blocked in
1123 * nbd_co_receive_request()
1124 */
1125 goto done;
1126 }
1127
8c5d1abb 1128 switch (command) {
b2e3d87f
NT
1129 case NBD_CMD_READ:
1130 TRACE("Request type is READ");
1131
e25ceb76 1132 if (request.type & NBD_CMD_FLAG_FUA) {
aadf99a7 1133 ret = blk_co_flush(exp->blk);
e25ceb76
PB
1134 if (ret < 0) {
1135 LOG("flush failed");
1136 reply.error = -ret;
1137 goto error_reply;
1138 }
1139 }
1140
df7b97ff
EB
1141 ret = blk_pread(exp->blk, request.from + exp->dev_offset,
1142 req->data, request.len);
adcf6302 1143 if (ret < 0) {
b2e3d87f 1144 LOG("reading from file failed");
adcf6302 1145 reply.error = -ret;
fae69416 1146 goto error_reply;
b2e3d87f 1147 }
b2e3d87f 1148
2cb34749 1149 TRACE("Read %" PRIu32" byte(s)", request.len);
262db388 1150 if (nbd_co_send_reply(req, &reply, request.len) < 0)
d9a73806 1151 goto out;
b2e3d87f
NT
1152 break;
1153 case NBD_CMD_WRITE:
1154 TRACE("Request type is WRITE");
1155
af49bbbe 1156 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
b2e3d87f 1157 TRACE("Server is read-only, return error");
fae69416
PB
1158 reply.error = EROFS;
1159 goto error_reply;
1160 }
1161
1162 TRACE("Writing to device");
1163
a0c30369
EB
1164 flags = 0;
1165 if (request.type & NBD_CMD_FLAG_FUA) {
1166 flags |= BDRV_REQ_FUA;
1167 }
df7b97ff 1168 ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
a0c30369 1169 req->data, request.len, flags);
fae69416
PB
1170 if (ret < 0) {
1171 LOG("writing to file failed");
1172 reply.error = -ret;
1173 goto error_reply;
1174 }
b2e3d87f 1175
fc19f8a0 1176 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1177 goto out;
fc19f8a0 1178 }
b2e3d87f 1179 break;
29b6c3b3 1180
b2e3d87f 1181 case NBD_CMD_DISC:
29b6c3b3
EB
1182 /* unreachable, thanks to special case in nbd_co_receive_request() */
1183 abort();
1184
1486d04a
PB
1185 case NBD_CMD_FLUSH:
1186 TRACE("Request type is FLUSH");
1187
aadf99a7 1188 ret = blk_co_flush(exp->blk);
1486d04a
PB
1189 if (ret < 0) {
1190 LOG("flush failed");
1191 reply.error = -ret;
1192 }
fc19f8a0 1193 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1194 goto out;
fc19f8a0 1195 }
7a706633
PB
1196 break;
1197 case NBD_CMD_TRIM:
1198 TRACE("Request type is TRIM");
1c6c4bb7
EB
1199 ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
1200 request.len);
1201 if (ret < 0) {
1202 LOG("discard failed");
1203 reply.error = -ret;
7a706633 1204 }
fc19f8a0 1205 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1206 goto out;
fc19f8a0 1207 }
1486d04a 1208 break;
b2e3d87f 1209 default:
2cb34749 1210 LOG("invalid request type (%" PRIu32 ") received", request.type);
8b2f0abf 1211 reply.error = EINVAL;
fae69416 1212 error_reply:
29b6c3b3
EB
1213 /* We must disconnect after NBD_CMD_WRITE if we did not
1214 * read the payload.
1215 */
1216 if (nbd_co_send_reply(req, &reply, 0) < 0 || !req->complete) {
d9a73806 1217 goto out;
fc19f8a0 1218 }
fae69416 1219 break;
b2e3d87f
NT
1220 }
1221
1222 TRACE("Request/Reply complete");
1223
7fe7b68b 1224done:
262db388
PB
1225 nbd_request_put(req);
1226 return;
1227
d9a73806 1228out:
72deddc5 1229 nbd_request_put(req);
f53a829b 1230 client_close(client);
7a5ca864 1231}
af49bbbe 1232
1743b515
PB
1233static void nbd_read(void *opaque)
1234{
1235 NBDClient *client = opaque;
1236
262db388 1237 if (client->recv_coroutine) {
0b8b8753 1238 qemu_coroutine_enter(client->recv_coroutine);
262db388 1239 } else {
0b8b8753 1240 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip, client));
1743b515 1241 }
1743b515
PB
1242}
1243
262db388
PB
1244static void nbd_restart_write(void *opaque)
1245{
1246 NBDClient *client = opaque;
1247
0b8b8753 1248 qemu_coroutine_enter(client->send_coroutine);
262db388
PB
1249}
1250
958c717d
HR
1251static void nbd_set_handlers(NBDClient *client)
1252{
1253 if (client->exp && client->exp->ctx) {
1c778ef7 1254 aio_set_fd_handler(client->exp->ctx, client->sioc->fd,
172cc129 1255 true,
958c717d
HR
1256 client->can_read ? nbd_read : NULL,
1257 client->send_coroutine ? nbd_restart_write : NULL,
1258 client);
1259 }
1260}
1261
1262static void nbd_unset_handlers(NBDClient *client)
1263{
1264 if (client->exp && client->exp->ctx) {
1c778ef7 1265 aio_set_fd_handler(client->exp->ctx, client->sioc->fd,
172cc129 1266 true, NULL, NULL, NULL);
958c717d
HR
1267 }
1268}
1269
1270static void nbd_update_can_read(NBDClient *client)
1271{
1272 bool can_read = client->recv_coroutine ||
1273 client->nb_requests < MAX_NBD_REQUESTS;
1274
1275 if (can_read != client->can_read) {
1276 client->can_read = can_read;
1277 nbd_set_handlers(client);
1278
1279 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1280 * in nbd_set_handlers() will have taken care of that */
1281 }
1282}
1283
1a6245a5
FZ
1284static coroutine_fn void nbd_co_client_start(void *opaque)
1285{
1286 NBDClientNewData *data = opaque;
1287 NBDClient *client = data->client;
1288 NBDExport *exp = client->exp;
1289
1290 if (exp) {
1291 nbd_export_get(exp);
1292 }
1293 if (nbd_negotiate(data)) {
d3780c2d 1294 client_close(client);
1a6245a5
FZ
1295 goto out;
1296 }
1297 qemu_co_mutex_init(&client->send_lock);
1298 nbd_set_handlers(client);
1299
1300 if (exp) {
1301 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1302 }
1303out:
1304 g_free(data);
1305}
1306
1c778ef7
DB
1307void nbd_client_new(NBDExport *exp,
1308 QIOChannelSocket *sioc,
f95910fe
DB
1309 QCryptoTLSCreds *tlscreds,
1310 const char *tlsaclname,
1c778ef7 1311 void (*close_fn)(NBDClient *))
af49bbbe 1312{
1743b515 1313 NBDClient *client;
1a6245a5
FZ
1314 NBDClientNewData *data = g_new(NBDClientNewData, 1);
1315
1743b515
PB
1316 client = g_malloc0(sizeof(NBDClient));
1317 client->refcount = 1;
1318 client->exp = exp;
f95910fe
DB
1319 client->tlscreds = tlscreds;
1320 if (tlscreds) {
1321 object_ref(OBJECT(client->tlscreds));
1322 }
1323 client->tlsaclname = g_strdup(tlsaclname);
1c778ef7
DB
1324 client->sioc = sioc;
1325 object_ref(OBJECT(client->sioc));
1326 client->ioc = QIO_CHANNEL(sioc);
1327 object_ref(OBJECT(client->ioc));
958c717d 1328 client->can_read = true;
ee7d7aab 1329 client->close = close_fn;
2c8d9f06 1330
1a6245a5 1331 data->client = client;
0b8b8753
PB
1332 data->co = qemu_coroutine_create(nbd_co_client_start, data);
1333 qemu_coroutine_enter(data->co);
af49bbbe 1334}