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