2 * Copyright (C) 2016-2018 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Server Side
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
23 #include "nbd-internal.h"
25 static int system_errno_to_nbd_errno(int err
)
53 /* Definitions for opaque data types */
55 typedef struct NBDRequestData NBDRequestData
;
57 struct NBDRequestData
{
58 QSIMPLEQ_ENTRY(NBDRequestData
) entry
;
66 void (*close
)(NBDExport
*exp
);
74 QTAILQ_HEAD(, NBDClient
) clients
;
75 QTAILQ_ENTRY(NBDExport
) next
;
79 BlockBackend
*eject_notifier_blk
;
80 Notifier eject_notifier
;
83 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
87 void (*close_fn
)(NBDClient
*client
, bool negotiated
);
90 QCryptoTLSCreds
*tlscreds
;
92 QIOChannelSocket
*sioc
; /* The underlying data channel */
93 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
95 Coroutine
*recv_coroutine
;
98 Coroutine
*send_coroutine
;
100 QTAILQ_ENTRY(NBDClient
) next
;
104 bool structured_reply
;
106 uint32_t opt
; /* Current option being negotiated */
107 uint32_t optlen
; /* remaining length of data in ioc for the option being
111 static void nbd_client_receive_next_request(NBDClient
*client
);
113 /* Basic flow for negotiation
140 static inline void set_be_option_rep(NBDOptionReply
*rep
, uint32_t option
,
141 uint32_t type
, uint32_t length
)
143 stq_be_p(&rep
->magic
, NBD_REP_MAGIC
);
144 stl_be_p(&rep
->option
, option
);
145 stl_be_p(&rep
->type
, type
);
146 stl_be_p(&rep
->length
, length
);
149 /* Send a reply header, including length, but no payload.
150 * Return -errno on error, 0 on success. */
151 static int nbd_negotiate_send_rep_len(NBDClient
*client
, uint32_t type
,
152 uint32_t len
, Error
**errp
)
156 trace_nbd_negotiate_send_rep_len(client
->opt
, nbd_opt_lookup(client
->opt
),
157 type
, nbd_rep_lookup(type
), len
);
159 assert(len
< NBD_MAX_BUFFER_SIZE
);
161 set_be_option_rep(&rep
, client
->opt
, type
, len
);
162 return nbd_write(client
->ioc
, &rep
, sizeof(rep
), errp
);
165 /* Send a reply header with default 0 length.
166 * Return -errno on error, 0 on success. */
167 static int nbd_negotiate_send_rep(NBDClient
*client
, uint32_t type
,
170 return nbd_negotiate_send_rep_len(client
, type
, 0, errp
);
173 /* Send an error reply.
174 * Return -errno on error, 0 on success. */
175 static int GCC_FMT_ATTR(4, 0)
176 nbd_negotiate_send_rep_verr(NBDClient
*client
, uint32_t type
,
177 Error
**errp
, const char *fmt
, va_list va
)
183 msg
= g_strdup_vprintf(fmt
, va
);
186 trace_nbd_negotiate_send_rep_err(msg
);
187 ret
= nbd_negotiate_send_rep_len(client
, type
, len
, errp
);
191 if (nbd_write(client
->ioc
, msg
, len
, errp
) < 0) {
192 error_prepend(errp
, "write failed (error message): ");
203 /* Send an error reply.
204 * Return -errno on error, 0 on success. */
205 static int GCC_FMT_ATTR(4, 5)
206 nbd_negotiate_send_rep_err(NBDClient
*client
, uint32_t type
,
207 Error
**errp
, const char *fmt
, ...)
213 ret
= nbd_negotiate_send_rep_verr(client
, type
, errp
, fmt
, va
);
218 /* Drop remainder of the current option, and send a reply with the
219 * given error type and message. Return -errno on read or write
220 * failure; or 0 if connection is still live. */
221 static int GCC_FMT_ATTR(4, 0)
222 nbd_opt_vdrop(NBDClient
*client
, uint32_t type
, Error
**errp
,
223 const char *fmt
, va_list va
)
225 int ret
= nbd_drop(client
->ioc
, client
->optlen
, errp
);
229 ret
= nbd_negotiate_send_rep_verr(client
, type
, errp
, fmt
, va
);
234 static int GCC_FMT_ATTR(4, 5)
235 nbd_opt_drop(NBDClient
*client
, uint32_t type
, Error
**errp
,
236 const char *fmt
, ...)
242 ret
= nbd_opt_vdrop(client
, type
, errp
, fmt
, va
);
248 static int GCC_FMT_ATTR(3, 4)
249 nbd_opt_invalid(NBDClient
*client
, Error
**errp
, const char *fmt
, ...)
255 ret
= nbd_opt_vdrop(client
, NBD_REP_ERR_INVALID
, errp
, fmt
, va
);
261 /* Read size bytes from the unparsed payload of the current option.
262 * Return -errno on I/O error, 0 if option was completely handled by
263 * sending a reply about inconsistent lengths, or 1 on success. */
264 static int nbd_opt_read(NBDClient
*client
, void *buffer
, size_t size
,
267 if (size
> client
->optlen
) {
268 return nbd_opt_invalid(client
, errp
,
269 "Inconsistent lengths in option %s",
270 nbd_opt_lookup(client
->opt
));
272 client
->optlen
-= size
;
273 return qio_channel_read_all(client
->ioc
, buffer
, size
, errp
) < 0 ? -EIO
: 1;
278 * Read a string with the format:
279 * uint32_t len (<= NBD_MAX_NAME_SIZE)
280 * len bytes string (not 0-terminated)
282 * @name should be enough to store NBD_MAX_NAME_SIZE+1.
283 * If @length is non-null, it will be set to the actual string length.
285 * Return -errno on I/O error, 0 if option was completely handled by
286 * sending a reply about inconsistent lengths, or 1 on success.
288 static int nbd_opt_read_name(NBDClient
*client
, char *name
, uint32_t *length
,
294 ret
= nbd_opt_read(client
, &len
, sizeof(len
), errp
);
300 if (len
> NBD_MAX_NAME_SIZE
) {
301 return nbd_opt_invalid(client
, errp
,
302 "Invalid name length: %" PRIu32
, len
);
305 ret
= nbd_opt_read(client
, name
, len
, errp
);
318 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
319 * Return -errno on error, 0 on success. */
320 static int nbd_negotiate_send_rep_list(NBDClient
*client
, NBDExport
*exp
,
323 size_t name_len
, desc_len
;
325 const char *name
= exp
->name
? exp
->name
: "";
326 const char *desc
= exp
->description
? exp
->description
: "";
327 QIOChannel
*ioc
= client
->ioc
;
330 trace_nbd_negotiate_send_rep_list(name
, desc
);
331 name_len
= strlen(name
);
332 desc_len
= strlen(desc
);
333 len
= name_len
+ desc_len
+ sizeof(len
);
334 ret
= nbd_negotiate_send_rep_len(client
, NBD_REP_SERVER
, len
, errp
);
339 len
= cpu_to_be32(name_len
);
340 if (nbd_write(ioc
, &len
, sizeof(len
), errp
) < 0) {
341 error_prepend(errp
, "write failed (name length): ");
345 if (nbd_write(ioc
, name
, name_len
, errp
) < 0) {
346 error_prepend(errp
, "write failed (name buffer): ");
350 if (nbd_write(ioc
, desc
, desc_len
, errp
) < 0) {
351 error_prepend(errp
, "write failed (description buffer): ");
358 /* Process the NBD_OPT_LIST command, with a potential series of replies.
359 * Return -errno on error, 0 on success. */
360 static int nbd_negotiate_handle_list(NBDClient
*client
, Error
**errp
)
363 assert(client
->opt
== NBD_OPT_LIST
);
365 /* For each export, send a NBD_REP_SERVER reply. */
366 QTAILQ_FOREACH(exp
, &exports
, next
) {
367 if (nbd_negotiate_send_rep_list(client
, exp
, errp
)) {
371 /* Finish with a NBD_REP_ACK. */
372 return nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
375 /* Send a reply to NBD_OPT_EXPORT_NAME.
376 * Return -errno on error, 0 on success. */
377 static int nbd_negotiate_handle_export_name(NBDClient
*client
,
378 uint16_t myflags
, bool no_zeroes
,
381 char name
[NBD_MAX_NAME_SIZE
+ 1];
382 char buf
[NBD_REPLY_EXPORT_NAME_SIZE
] = "";
387 [20 .. xx] export name (length bytes)
390 [ 8 .. 9] export flags
391 [10 .. 133] reserved (0) [unless no_zeroes]
393 trace_nbd_negotiate_handle_export_name();
394 if (client
->optlen
>= sizeof(name
)) {
395 error_setg(errp
, "Bad length received");
398 if (nbd_read(client
->ioc
, name
, client
->optlen
, errp
) < 0) {
399 error_prepend(errp
, "read failed: ");
402 name
[client
->optlen
] = '\0';
405 trace_nbd_negotiate_handle_export_name_request(name
);
407 client
->exp
= nbd_export_find(name
);
409 error_setg(errp
, "export not found");
413 trace_nbd_negotiate_new_style_size_flags(client
->exp
->size
,
414 client
->exp
->nbdflags
| myflags
);
415 stq_be_p(buf
, client
->exp
->size
);
416 stw_be_p(buf
+ 8, client
->exp
->nbdflags
| myflags
);
417 len
= no_zeroes
? 10 : sizeof(buf
);
418 ret
= nbd_write(client
->ioc
, buf
, len
, errp
);
420 error_prepend(errp
, "write failed: ");
424 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
425 nbd_export_get(client
->exp
);
430 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
431 * The buffer does NOT include the info type prefix.
432 * Return -errno on error, 0 if ready to send more. */
433 static int nbd_negotiate_send_info(NBDClient
*client
,
434 uint16_t info
, uint32_t length
, void *buf
,
439 trace_nbd_negotiate_send_info(info
, nbd_info_lookup(info
), length
);
440 rc
= nbd_negotiate_send_rep_len(client
, NBD_REP_INFO
,
441 sizeof(info
) + length
, errp
);
446 if (nbd_write(client
->ioc
, &info
, sizeof(info
), errp
) < 0) {
449 if (nbd_write(client
->ioc
, buf
, length
, errp
) < 0) {
455 /* nbd_reject_length: Handle any unexpected payload.
456 * @fatal requests that we quit talking to the client, even if we are able
457 * to successfully send an error reply.
459 * -errno transmission error occurred or @fatal was requested, errp is set
460 * 0 error message successfully sent to client, errp is not set
462 static int nbd_reject_length(NBDClient
*client
, bool fatal
, Error
**errp
)
466 assert(client
->optlen
);
467 ret
= nbd_opt_invalid(client
, errp
, "option '%s' has unexpected length",
468 nbd_opt_lookup(client
->opt
));
470 error_setg(errp
, "option '%s' has unexpected length",
471 nbd_opt_lookup(client
->opt
));
477 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
478 * Return -errno on error, 0 if ready for next option, and 1 to move
479 * into transmission phase. */
480 static int nbd_negotiate_handle_info(NBDClient
*client
, uint16_t myflags
,
484 char name
[NBD_MAX_NAME_SIZE
+ 1];
489 bool sendname
= false;
490 bool blocksize
= false;
492 char buf
[sizeof(uint64_t) + sizeof(uint16_t)];
495 4 bytes: L, name length (can be 0)
497 2 bytes: N, number of requests (can be 0)
498 N * 2 bytes: N requests
500 rc
= nbd_opt_read_name(client
, name
, &namelen
, errp
);
504 trace_nbd_negotiate_handle_export_name_request(name
);
506 rc
= nbd_opt_read(client
, &requests
, sizeof(requests
), errp
);
510 be16_to_cpus(&requests
);
511 trace_nbd_negotiate_handle_info_requests(requests
);
513 rc
= nbd_opt_read(client
, &request
, sizeof(request
), errp
);
517 be16_to_cpus(&request
);
518 trace_nbd_negotiate_handle_info_request(request
,
519 nbd_info_lookup(request
));
520 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
521 * everything else is either a request we don't know or
522 * something we send regardless of request */
527 case NBD_INFO_BLOCK_SIZE
:
532 if (client
->optlen
) {
533 return nbd_reject_length(client
, false, errp
);
536 exp
= nbd_export_find(name
);
538 return nbd_negotiate_send_rep_err(client
, NBD_REP_ERR_UNKNOWN
,
539 errp
, "export '%s' not present",
543 /* Don't bother sending NBD_INFO_NAME unless client requested it */
545 rc
= nbd_negotiate_send_info(client
, NBD_INFO_NAME
, namelen
, name
,
552 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
554 if (exp
->description
) {
555 size_t len
= strlen(exp
->description
);
557 rc
= nbd_negotiate_send_info(client
, NBD_INFO_DESCRIPTION
,
558 len
, exp
->description
, errp
);
564 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
565 * according to whether the client requested it, and according to
566 * whether this is OPT_INFO or OPT_GO. */
567 /* minimum - 1 for back-compat, or 512 if client is new enough.
568 * TODO: consult blk_bs(blk)->bl.request_alignment? */
570 (client
->opt
== NBD_OPT_INFO
|| blocksize
) ? BDRV_SECTOR_SIZE
: 1;
571 /* preferred - Hard-code to 4096 for now.
572 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
574 /* maximum - At most 32M, but smaller as appropriate. */
575 sizes
[2] = MIN(blk_get_max_transfer(exp
->blk
), NBD_MAX_BUFFER_SIZE
);
576 trace_nbd_negotiate_handle_info_block_size(sizes
[0], sizes
[1], sizes
[2]);
577 cpu_to_be32s(&sizes
[0]);
578 cpu_to_be32s(&sizes
[1]);
579 cpu_to_be32s(&sizes
[2]);
580 rc
= nbd_negotiate_send_info(client
, NBD_INFO_BLOCK_SIZE
,
581 sizeof(sizes
), sizes
, errp
);
586 /* Send NBD_INFO_EXPORT always */
587 trace_nbd_negotiate_new_style_size_flags(exp
->size
,
588 exp
->nbdflags
| myflags
);
589 stq_be_p(buf
, exp
->size
);
590 stw_be_p(buf
+ 8, exp
->nbdflags
| myflags
);
591 rc
= nbd_negotiate_send_info(client
, NBD_INFO_EXPORT
,
592 sizeof(buf
), buf
, errp
);
597 /* If the client is just asking for NBD_OPT_INFO, but forgot to
598 * request block sizes, return an error.
599 * TODO: consult blk_bs(blk)->request_align, and only error if it
601 if (client
->opt
== NBD_OPT_INFO
&& !blocksize
) {
602 return nbd_negotiate_send_rep_err(client
,
603 NBD_REP_ERR_BLOCK_SIZE_REQD
,
605 "request NBD_INFO_BLOCK_SIZE to "
610 rc
= nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
615 if (client
->opt
== NBD_OPT_GO
) {
617 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
618 nbd_export_get(client
->exp
);
625 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
626 * new channel for all further (now-encrypted) communication. */
627 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
632 struct NBDTLSHandshakeData data
= { 0 };
634 assert(client
->opt
== NBD_OPT_STARTTLS
);
636 trace_nbd_negotiate_handle_starttls();
639 if (nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
) < 0) {
643 tioc
= qio_channel_tls_new_server(ioc
,
651 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-server-tls");
652 trace_nbd_negotiate_handle_starttls_handshake();
653 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
654 qio_channel_tls_handshake(tioc
,
660 if (!data
.complete
) {
661 g_main_loop_run(data
.loop
);
663 g_main_loop_unref(data
.loop
);
665 object_unref(OBJECT(tioc
));
666 error_propagate(errp
, data
.error
);
670 return QIO_CHANNEL(tioc
);
673 /* nbd_negotiate_options
674 * Process all NBD_OPT_* client option commands, during fixed newstyle
677 * -errno on error, errp is set
678 * 0 on successful negotiation, errp is not set
679 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
682 static int nbd_negotiate_options(NBDClient
*client
, uint16_t myflags
,
686 bool fixedNewstyle
= false;
687 bool no_zeroes
= false;
690 [ 0 .. 3] client flags
692 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
693 [ 0 .. 7] NBD_OPTS_MAGIC
694 [ 8 .. 11] NBD option
695 [12 .. 15] Data length
698 [ 0 .. 7] NBD_OPTS_MAGIC
699 [ 8 .. 11] Second NBD option
700 [12 .. 15] Data length
704 if (nbd_read(client
->ioc
, &flags
, sizeof(flags
), errp
) < 0) {
705 error_prepend(errp
, "read failed: ");
708 be32_to_cpus(&flags
);
709 trace_nbd_negotiate_options_flags(flags
);
710 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
711 fixedNewstyle
= true;
712 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
714 if (flags
& NBD_FLAG_C_NO_ZEROES
) {
716 flags
&= ~NBD_FLAG_C_NO_ZEROES
;
719 error_setg(errp
, "Unknown client flags 0x%" PRIx32
" received", flags
);
725 uint32_t option
, length
;
728 if (nbd_read(client
->ioc
, &magic
, sizeof(magic
), errp
) < 0) {
729 error_prepend(errp
, "read failed: ");
732 magic
= be64_to_cpu(magic
);
733 trace_nbd_negotiate_options_check_magic(magic
);
734 if (magic
!= NBD_OPTS_MAGIC
) {
735 error_setg(errp
, "Bad magic received");
739 if (nbd_read(client
->ioc
, &option
,
740 sizeof(option
), errp
) < 0) {
741 error_prepend(errp
, "read failed: ");
744 option
= be32_to_cpu(option
);
745 client
->opt
= option
;
747 if (nbd_read(client
->ioc
, &length
, sizeof(length
), errp
) < 0) {
748 error_prepend(errp
, "read failed: ");
751 length
= be32_to_cpu(length
);
752 assert(!client
->optlen
);
753 client
->optlen
= length
;
755 if (length
> NBD_MAX_BUFFER_SIZE
) {
756 error_setg(errp
, "len (%" PRIu32
" ) is larger than max len (%u)",
757 length
, NBD_MAX_BUFFER_SIZE
);
761 trace_nbd_negotiate_options_check_option(option
,
762 nbd_opt_lookup(option
));
763 if (client
->tlscreds
&&
764 client
->ioc
== (QIOChannel
*)client
->sioc
) {
766 if (!fixedNewstyle
) {
767 error_setg(errp
, "Unsupported option 0x%" PRIx32
, option
);
771 case NBD_OPT_STARTTLS
:
773 /* Unconditionally drop the connection if the client
774 * can't start a TLS negotiation correctly */
775 return nbd_reject_length(client
, true, errp
);
777 tioc
= nbd_negotiate_handle_starttls(client
, errp
);
782 object_unref(OBJECT(client
->ioc
));
783 client
->ioc
= QIO_CHANNEL(tioc
);
786 case NBD_OPT_EXPORT_NAME
:
787 /* No way to return an error to client, so drop connection */
788 error_setg(errp
, "Option 0x%x not permitted before TLS",
793 ret
= nbd_opt_drop(client
, NBD_REP_ERR_TLS_REQD
, errp
,
795 "not permitted before TLS", option
);
796 /* Let the client keep trying, unless they asked to
797 * quit. In this mode, we've already sent an error, so
798 * we can't ack the abort. */
799 if (option
== NBD_OPT_ABORT
) {
804 } else if (fixedNewstyle
) {
808 ret
= nbd_reject_length(client
, false, errp
);
810 ret
= nbd_negotiate_handle_list(client
, errp
);
815 /* NBD spec says we must try to reply before
816 * disconnecting, but that we must also tolerate
817 * guests that don't wait for our reply. */
818 nbd_negotiate_send_rep(client
, NBD_REP_ACK
, NULL
);
821 case NBD_OPT_EXPORT_NAME
:
822 return nbd_negotiate_handle_export_name(client
,
828 ret
= nbd_negotiate_handle_info(client
, myflags
, errp
);
830 assert(option
== NBD_OPT_GO
);
835 case NBD_OPT_STARTTLS
:
837 ret
= nbd_reject_length(client
, false, errp
);
838 } else if (client
->tlscreds
) {
839 ret
= nbd_negotiate_send_rep_err(client
,
840 NBD_REP_ERR_INVALID
, errp
,
841 "TLS already enabled");
843 ret
= nbd_negotiate_send_rep_err(client
,
844 NBD_REP_ERR_POLICY
, errp
,
845 "TLS not configured");
849 case NBD_OPT_STRUCTURED_REPLY
:
851 ret
= nbd_reject_length(client
, false, errp
);
852 } else if (client
->structured_reply
) {
853 ret
= nbd_negotiate_send_rep_err(
854 client
, NBD_REP_ERR_INVALID
, errp
,
855 "structured reply already negotiated");
857 ret
= nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
858 client
->structured_reply
= true;
859 myflags
|= NBD_FLAG_SEND_DF
;
864 ret
= nbd_opt_drop(client
, NBD_REP_ERR_UNSUP
, errp
,
865 "Unsupported option %" PRIu32
" (%s)",
866 option
, nbd_opt_lookup(option
));
871 * If broken new-style we should drop the connection
872 * for anything except NBD_OPT_EXPORT_NAME
875 case NBD_OPT_EXPORT_NAME
:
876 return nbd_negotiate_handle_export_name(client
,
881 error_setg(errp
, "Unsupported option %" PRIu32
" (%s)",
882 option
, nbd_opt_lookup(option
));
894 * -errno on error, errp is set
895 * 0 on successful negotiation, errp is not set
896 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
899 static coroutine_fn
int nbd_negotiate(NBDClient
*client
, Error
**errp
)
901 char buf
[NBD_OLDSTYLE_NEGOTIATE_SIZE
] = "";
903 const uint16_t myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
904 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
|
905 NBD_FLAG_SEND_WRITE_ZEROES
);
908 /* Old style negotiation header, no room for options
909 [ 0 .. 7] passwd ("NBDMAGIC")
910 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
912 [24 .. 27] export flags (zero-extended)
913 [28 .. 151] reserved (0)
915 New style negotiation header, client can send options
916 [ 0 .. 7] passwd ("NBDMAGIC")
917 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
918 [16 .. 17] server flags (0)
919 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
922 qio_channel_set_blocking(client
->ioc
, false, NULL
);
924 trace_nbd_negotiate_begin();
925 memcpy(buf
, "NBDMAGIC", 8);
927 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
929 trace_nbd_negotiate_old_style(client
->exp
->size
,
930 client
->exp
->nbdflags
| myflags
);
931 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
932 stq_be_p(buf
+ 16, client
->exp
->size
);
933 stl_be_p(buf
+ 24, client
->exp
->nbdflags
| myflags
);
935 if (nbd_write(client
->ioc
, buf
, sizeof(buf
), errp
) < 0) {
936 error_prepend(errp
, "write failed: ");
940 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
941 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
| NBD_FLAG_NO_ZEROES
);
943 if (nbd_write(client
->ioc
, buf
, 18, errp
) < 0) {
944 error_prepend(errp
, "write failed: ");
947 ret
= nbd_negotiate_options(client
, myflags
, errp
);
950 error_prepend(errp
, "option negotiation failed: ");
956 assert(!client
->optlen
);
957 trace_nbd_negotiate_success();
962 static int nbd_receive_request(QIOChannel
*ioc
, NBDRequest
*request
,
965 uint8_t buf
[NBD_REQUEST_SIZE
];
969 ret
= nbd_read(ioc
, buf
, sizeof(buf
), errp
);
975 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
976 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
977 [ 6 .. 7] type (NBD_CMD_READ, ...)
983 magic
= ldl_be_p(buf
);
984 request
->flags
= lduw_be_p(buf
+ 4);
985 request
->type
= lduw_be_p(buf
+ 6);
986 request
->handle
= ldq_be_p(buf
+ 8);
987 request
->from
= ldq_be_p(buf
+ 16);
988 request
->len
= ldl_be_p(buf
+ 24);
990 trace_nbd_receive_request(magic
, request
->flags
, request
->type
,
991 request
->from
, request
->len
);
993 if (magic
!= NBD_REQUEST_MAGIC
) {
994 error_setg(errp
, "invalid magic (got 0x%" PRIx32
")", magic
);
1000 #define MAX_NBD_REQUESTS 16
1002 void nbd_client_get(NBDClient
*client
)
1007 void nbd_client_put(NBDClient
*client
)
1009 if (--client
->refcount
== 0) {
1010 /* The last reference should be dropped by client->close,
1011 * which is called by client_close.
1013 assert(client
->closing
);
1015 qio_channel_detach_aio_context(client
->ioc
);
1016 object_unref(OBJECT(client
->sioc
));
1017 object_unref(OBJECT(client
->ioc
));
1018 if (client
->tlscreds
) {
1019 object_unref(OBJECT(client
->tlscreds
));
1021 g_free(client
->tlsaclname
);
1023 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
1024 nbd_export_put(client
->exp
);
1030 static void client_close(NBDClient
*client
, bool negotiated
)
1032 if (client
->closing
) {
1036 client
->closing
= true;
1038 /* Force requests to finish. They will drop their own references,
1039 * then we'll close the socket and free the NBDClient.
1041 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
1044 /* Also tell the client, so that they release their reference. */
1045 if (client
->close_fn
) {
1046 client
->close_fn(client
, negotiated
);
1050 static NBDRequestData
*nbd_request_get(NBDClient
*client
)
1052 NBDRequestData
*req
;
1054 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
1055 client
->nb_requests
++;
1057 req
= g_new0(NBDRequestData
, 1);
1058 nbd_client_get(client
);
1059 req
->client
= client
;
1063 static void nbd_request_put(NBDRequestData
*req
)
1065 NBDClient
*client
= req
->client
;
1068 qemu_vfree(req
->data
);
1072 client
->nb_requests
--;
1073 nbd_client_receive_next_request(client
);
1075 nbd_client_put(client
);
1078 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
1080 NBDExport
*exp
= opaque
;
1083 trace_nbd_blk_aio_attached(exp
->name
, ctx
);
1087 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1088 qio_channel_attach_aio_context(client
->ioc
, ctx
);
1089 if (client
->recv_coroutine
) {
1090 aio_co_schedule(ctx
, client
->recv_coroutine
);
1092 if (client
->send_coroutine
) {
1093 aio_co_schedule(ctx
, client
->send_coroutine
);
1098 static void blk_aio_detach(void *opaque
)
1100 NBDExport
*exp
= opaque
;
1103 trace_nbd_blk_aio_detach(exp
->name
, exp
->ctx
);
1105 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1106 qio_channel_detach_aio_context(client
->ioc
);
1112 static void nbd_eject_notifier(Notifier
*n
, void *data
)
1114 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
1115 nbd_export_close(exp
);
1118 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
, off_t size
,
1119 uint16_t nbdflags
, void (*close
)(NBDExport
*),
1120 bool writethrough
, BlockBackend
*on_eject_blk
,
1125 NBDExport
*exp
= g_new0(NBDExport
, 1);
1130 * NBD exports are used for non-shared storage migration. Make sure
1131 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1132 * access since the export could be available before migration handover.
1134 ctx
= bdrv_get_aio_context(bs
);
1135 aio_context_acquire(ctx
);
1136 bdrv_invalidate_cache(bs
, NULL
);
1137 aio_context_release(ctx
);
1139 /* Don't allow resize while the NBD server is running, otherwise we don't
1140 * care what happens with the node. */
1141 perm
= BLK_PERM_CONSISTENT_READ
;
1142 if ((nbdflags
& NBD_FLAG_READ_ONLY
) == 0) {
1143 perm
|= BLK_PERM_WRITE
;
1145 blk
= blk_new(perm
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
|
1146 BLK_PERM_WRITE
| BLK_PERM_GRAPH_MOD
);
1147 ret
= blk_insert_bs(blk
, bs
, errp
);
1151 blk_set_enable_write_cache(blk
, !writethrough
);
1154 QTAILQ_INIT(&exp
->clients
);
1156 exp
->dev_offset
= dev_offset
;
1157 exp
->nbdflags
= nbdflags
;
1158 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
1159 if (exp
->size
< 0) {
1160 error_setg_errno(errp
, -exp
->size
,
1161 "Failed to determine the NBD export's length");
1164 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
1167 exp
->ctx
= blk_get_aio_context(blk
);
1168 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
1171 blk_ref(on_eject_blk
);
1172 exp
->eject_notifier_blk
= on_eject_blk
;
1173 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
1174 blk_add_remove_bs_notifier(on_eject_blk
, &exp
->eject_notifier
);
1184 NBDExport
*nbd_export_find(const char *name
)
1187 QTAILQ_FOREACH(exp
, &exports
, next
) {
1188 if (strcmp(name
, exp
->name
) == 0) {
1196 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
1198 if (exp
->name
== name
) {
1202 nbd_export_get(exp
);
1203 if (exp
->name
!= NULL
) {
1206 QTAILQ_REMOVE(&exports
, exp
, next
);
1207 nbd_export_put(exp
);
1210 nbd_export_get(exp
);
1211 exp
->name
= g_strdup(name
);
1212 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1214 nbd_export_put(exp
);
1217 void nbd_export_set_description(NBDExport
*exp
, const char *description
)
1219 g_free(exp
->description
);
1220 exp
->description
= g_strdup(description
);
1223 void nbd_export_close(NBDExport
*exp
)
1225 NBDClient
*client
, *next
;
1227 nbd_export_get(exp
);
1228 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1229 client_close(client
, true);
1231 nbd_export_set_name(exp
, NULL
);
1232 nbd_export_set_description(exp
, NULL
);
1233 nbd_export_put(exp
);
1236 void nbd_export_remove(NBDExport
*exp
, NbdServerRemoveMode mode
, Error
**errp
)
1238 if (mode
== NBD_SERVER_REMOVE_MODE_HARD
|| QTAILQ_EMPTY(&exp
->clients
)) {
1239 nbd_export_close(exp
);
1243 assert(mode
== NBD_SERVER_REMOVE_MODE_SAFE
);
1245 error_setg(errp
, "export '%s' still in use", exp
->name
);
1246 error_append_hint(errp
, "Use mode='hard' to force client disconnect\n");
1249 void nbd_export_get(NBDExport
*exp
)
1251 assert(exp
->refcount
> 0);
1255 void nbd_export_put(NBDExport
*exp
)
1257 assert(exp
->refcount
> 0);
1258 if (exp
->refcount
== 1) {
1259 nbd_export_close(exp
);
1262 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1263 * if someone calls nbd_export_put() on named export not through
1264 * nbd_export_set_name() when refcount is 1. So, let's assert that
1267 assert(exp
->refcount
> 0);
1268 if (--exp
->refcount
== 0) {
1269 assert(exp
->name
== NULL
);
1270 assert(exp
->description
== NULL
);
1277 if (exp
->eject_notifier_blk
) {
1278 notifier_remove(&exp
->eject_notifier
);
1279 blk_unref(exp
->eject_notifier_blk
);
1281 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
1282 blk_aio_detach
, exp
);
1283 blk_unref(exp
->blk
);
1291 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1296 void nbd_export_close_all(void)
1298 NBDExport
*exp
, *next
;
1300 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1301 nbd_export_close(exp
);
1305 static int coroutine_fn
nbd_co_send_iov(NBDClient
*client
, struct iovec
*iov
,
1306 unsigned niov
, Error
**errp
)
1310 g_assert(qemu_in_coroutine());
1311 qemu_co_mutex_lock(&client
->send_lock
);
1312 client
->send_coroutine
= qemu_coroutine_self();
1314 ret
= qio_channel_writev_all(client
->ioc
, iov
, niov
, errp
) < 0 ? -EIO
: 0;
1316 client
->send_coroutine
= NULL
;
1317 qemu_co_mutex_unlock(&client
->send_lock
);
1322 static inline void set_be_simple_reply(NBDSimpleReply
*reply
, uint64_t error
,
1325 stl_be_p(&reply
->magic
, NBD_SIMPLE_REPLY_MAGIC
);
1326 stl_be_p(&reply
->error
, error
);
1327 stq_be_p(&reply
->handle
, handle
);
1330 static int nbd_co_send_simple_reply(NBDClient
*client
,
1337 NBDSimpleReply reply
;
1338 int nbd_err
= system_errno_to_nbd_errno(error
);
1339 struct iovec iov
[] = {
1340 {.iov_base
= &reply
, .iov_len
= sizeof(reply
)},
1341 {.iov_base
= data
, .iov_len
= len
}
1344 trace_nbd_co_send_simple_reply(handle
, nbd_err
, nbd_err_lookup(nbd_err
),
1346 set_be_simple_reply(&reply
, nbd_err
, handle
);
1348 return nbd_co_send_iov(client
, iov
, len
? 2 : 1, errp
);
1351 static inline void set_be_chunk(NBDStructuredReplyChunk
*chunk
, uint16_t flags
,
1352 uint16_t type
, uint64_t handle
, uint32_t length
)
1354 stl_be_p(&chunk
->magic
, NBD_STRUCTURED_REPLY_MAGIC
);
1355 stw_be_p(&chunk
->flags
, flags
);
1356 stw_be_p(&chunk
->type
, type
);
1357 stq_be_p(&chunk
->handle
, handle
);
1358 stl_be_p(&chunk
->length
, length
);
1361 static int coroutine_fn
nbd_co_send_structured_done(NBDClient
*client
,
1365 NBDStructuredReplyChunk chunk
;
1366 struct iovec iov
[] = {
1367 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1370 trace_nbd_co_send_structured_done(handle
);
1371 set_be_chunk(&chunk
, NBD_REPLY_FLAG_DONE
, NBD_REPLY_TYPE_NONE
, handle
, 0);
1373 return nbd_co_send_iov(client
, iov
, 1, errp
);
1376 static int coroutine_fn
nbd_co_send_structured_read(NBDClient
*client
,
1384 NBDStructuredReadData chunk
;
1385 struct iovec iov
[] = {
1386 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1387 {.iov_base
= data
, .iov_len
= size
}
1391 trace_nbd_co_send_structured_read(handle
, offset
, data
, size
);
1392 set_be_chunk(&chunk
.h
, final
? NBD_REPLY_FLAG_DONE
: 0,
1393 NBD_REPLY_TYPE_OFFSET_DATA
, handle
,
1394 sizeof(chunk
) - sizeof(chunk
.h
) + size
);
1395 stq_be_p(&chunk
.offset
, offset
);
1397 return nbd_co_send_iov(client
, iov
, 2, errp
);
1400 static int coroutine_fn
nbd_co_send_structured_error(NBDClient
*client
,
1406 NBDStructuredError chunk
;
1407 int nbd_err
= system_errno_to_nbd_errno(error
);
1408 struct iovec iov
[] = {
1409 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1410 {.iov_base
= (char *)msg
, .iov_len
= msg
? strlen(msg
) : 0},
1414 trace_nbd_co_send_structured_error(handle
, nbd_err
,
1415 nbd_err_lookup(nbd_err
), msg
? msg
: "");
1416 set_be_chunk(&chunk
.h
, NBD_REPLY_FLAG_DONE
, NBD_REPLY_TYPE_ERROR
, handle
,
1417 sizeof(chunk
) - sizeof(chunk
.h
) + iov
[1].iov_len
);
1418 stl_be_p(&chunk
.error
, nbd_err
);
1419 stw_be_p(&chunk
.message_length
, iov
[1].iov_len
);
1421 return nbd_co_send_iov(client
, iov
, 1 + !!iov
[1].iov_len
, errp
);
1424 /* Do a sparse read and send the structured reply to the client.
1425 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1426 * reported to the client, at which point this function succeeds.
1428 static int coroutine_fn
nbd_co_send_sparse_read(NBDClient
*client
,
1436 NBDExport
*exp
= client
->exp
;
1437 size_t progress
= 0;
1439 while (progress
< size
) {
1441 int status
= bdrv_block_status_above(blk_bs(exp
->blk
), NULL
,
1443 size
- progress
, &pnum
, NULL
,
1448 char *msg
= g_strdup_printf("unable to check for holes: %s",
1451 ret
= nbd_co_send_structured_error(client
, handle
, -status
, msg
,
1456 assert(pnum
&& pnum
<= size
- progress
);
1457 final
= progress
+ pnum
== size
;
1458 if (status
& BDRV_BLOCK_ZERO
) {
1459 NBDStructuredReadHole chunk
;
1460 struct iovec iov
[] = {
1461 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1464 trace_nbd_co_send_structured_read_hole(handle
, offset
+ progress
,
1466 set_be_chunk(&chunk
.h
, final
? NBD_REPLY_FLAG_DONE
: 0,
1467 NBD_REPLY_TYPE_OFFSET_HOLE
,
1468 handle
, sizeof(chunk
) - sizeof(chunk
.h
));
1469 stq_be_p(&chunk
.offset
, offset
+ progress
);
1470 stl_be_p(&chunk
.length
, pnum
);
1471 ret
= nbd_co_send_iov(client
, iov
, 1, errp
);
1473 ret
= blk_pread(exp
->blk
, offset
+ progress
+ exp
->dev_offset
,
1474 data
+ progress
, pnum
);
1476 error_setg_errno(errp
, -ret
, "reading from file failed");
1479 ret
= nbd_co_send_structured_read(client
, handle
, offset
+ progress
,
1480 data
+ progress
, pnum
, final
,
1492 /* nbd_co_receive_request
1493 * Collect a client request. Return 0 if request looks valid, -EIO to drop
1494 * connection right away, and any other negative value to report an error to
1495 * the client (although the caller may still need to disconnect after reporting
1498 static int nbd_co_receive_request(NBDRequestData
*req
, NBDRequest
*request
,
1501 NBDClient
*client
= req
->client
;
1504 g_assert(qemu_in_coroutine());
1505 assert(client
->recv_coroutine
== qemu_coroutine_self());
1506 if (nbd_receive_request(client
->ioc
, request
, errp
) < 0) {
1510 trace_nbd_co_receive_request_decode_type(request
->handle
, request
->type
,
1511 nbd_cmd_lookup(request
->type
));
1513 if (request
->type
!= NBD_CMD_WRITE
) {
1514 /* No payload, we are ready to read the next request. */
1515 req
->complete
= true;
1518 if (request
->type
== NBD_CMD_DISC
) {
1519 /* Special case: we're going to disconnect without a reply,
1520 * whether or not flags, from, or len are bogus */
1524 if (request
->type
== NBD_CMD_READ
|| request
->type
== NBD_CMD_WRITE
) {
1525 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1526 error_setg(errp
, "len (%" PRIu32
" ) is larger than max len (%u)",
1527 request
->len
, NBD_MAX_BUFFER_SIZE
);
1531 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
1532 if (req
->data
== NULL
) {
1533 error_setg(errp
, "No memory");
1537 if (request
->type
== NBD_CMD_WRITE
) {
1538 if (nbd_read(client
->ioc
, req
->data
, request
->len
, errp
) < 0) {
1539 error_prepend(errp
, "reading from socket failed: ");
1542 req
->complete
= true;
1544 trace_nbd_co_receive_request_payload_received(request
->handle
,
1548 /* Sanity checks. */
1549 if (client
->exp
->nbdflags
& NBD_FLAG_READ_ONLY
&&
1550 (request
->type
== NBD_CMD_WRITE
||
1551 request
->type
== NBD_CMD_WRITE_ZEROES
||
1552 request
->type
== NBD_CMD_TRIM
)) {
1553 error_setg(errp
, "Export is read-only");
1556 if (request
->from
> client
->exp
->size
||
1557 request
->from
+ request
->len
> client
->exp
->size
) {
1558 error_setg(errp
, "operation past EOF; From: %" PRIu64
", Len: %" PRIu32
1559 ", Size: %" PRIu64
, request
->from
, request
->len
,
1560 (uint64_t)client
->exp
->size
);
1561 return (request
->type
== NBD_CMD_WRITE
||
1562 request
->type
== NBD_CMD_WRITE_ZEROES
) ? -ENOSPC
: -EINVAL
;
1564 valid_flags
= NBD_CMD_FLAG_FUA
;
1565 if (request
->type
== NBD_CMD_READ
&& client
->structured_reply
) {
1566 valid_flags
|= NBD_CMD_FLAG_DF
;
1567 } else if (request
->type
== NBD_CMD_WRITE_ZEROES
) {
1568 valid_flags
|= NBD_CMD_FLAG_NO_HOLE
;
1570 if (request
->flags
& ~valid_flags
) {
1571 error_setg(errp
, "unsupported flags for command %s (got 0x%x)",
1572 nbd_cmd_lookup(request
->type
), request
->flags
);
1579 /* Send simple reply without a payload, or a structured error
1580 * @error_msg is ignored if @ret >= 0
1581 * Returns 0 if connection is still live, -errno on failure to talk to client
1583 static coroutine_fn
int nbd_send_generic_reply(NBDClient
*client
,
1586 const char *error_msg
,
1589 if (client
->structured_reply
&& ret
< 0) {
1590 return nbd_co_send_structured_error(client
, handle
, -ret
, error_msg
,
1593 return nbd_co_send_simple_reply(client
, handle
, ret
< 0 ? -ret
: 0,
1598 /* Handle NBD_CMD_READ request.
1599 * Return -errno if sending fails. Other errors are reported directly to the
1600 * client as an error reply. */
1601 static coroutine_fn
int nbd_do_cmd_read(NBDClient
*client
, NBDRequest
*request
,
1602 uint8_t *data
, Error
**errp
)
1605 NBDExport
*exp
= client
->exp
;
1607 assert(request
->type
== NBD_CMD_READ
);
1609 /* XXX: NBD Protocol only documents use of FUA with WRITE */
1610 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
1611 ret
= blk_co_flush(exp
->blk
);
1613 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1614 "flush failed", errp
);
1618 if (client
->structured_reply
&& !(request
->flags
& NBD_CMD_FLAG_DF
) &&
1620 return nbd_co_send_sparse_read(client
, request
->handle
, request
->from
,
1621 data
, request
->len
, errp
);
1624 ret
= blk_pread(exp
->blk
, request
->from
+ exp
->dev_offset
, data
,
1627 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1628 "reading from file failed", errp
);
1631 if (client
->structured_reply
) {
1633 return nbd_co_send_structured_read(client
, request
->handle
,
1634 request
->from
, data
,
1635 request
->len
, true, errp
);
1637 return nbd_co_send_structured_done(client
, request
->handle
, errp
);
1640 return nbd_co_send_simple_reply(client
, request
->handle
, 0,
1641 data
, request
->len
, errp
);
1645 /* Handle NBD request.
1646 * Return -errno if sending fails. Other errors are reported directly to the
1647 * client as an error reply. */
1648 static coroutine_fn
int nbd_handle_request(NBDClient
*client
,
1649 NBDRequest
*request
,
1650 uint8_t *data
, Error
**errp
)
1654 NBDExport
*exp
= client
->exp
;
1657 switch (request
->type
) {
1659 return nbd_do_cmd_read(client
, request
, data
, errp
);
1663 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
1664 flags
|= BDRV_REQ_FUA
;
1666 ret
= blk_pwrite(exp
->blk
, request
->from
+ exp
->dev_offset
,
1667 data
, request
->len
, flags
);
1668 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1669 "writing to file failed", errp
);
1671 case NBD_CMD_WRITE_ZEROES
:
1673 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
1674 flags
|= BDRV_REQ_FUA
;
1676 if (!(request
->flags
& NBD_CMD_FLAG_NO_HOLE
)) {
1677 flags
|= BDRV_REQ_MAY_UNMAP
;
1679 ret
= blk_pwrite_zeroes(exp
->blk
, request
->from
+ exp
->dev_offset
,
1680 request
->len
, flags
);
1681 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1682 "writing to file failed", errp
);
1685 /* unreachable, thanks to special case in nbd_co_receive_request() */
1689 ret
= blk_co_flush(exp
->blk
);
1690 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1691 "flush failed", errp
);
1694 ret
= blk_co_pdiscard(exp
->blk
, request
->from
+ exp
->dev_offset
,
1696 if (ret
== 0 && request
->flags
& NBD_CMD_FLAG_FUA
) {
1697 ret
= blk_co_flush(exp
->blk
);
1699 return nbd_send_generic_reply(client
, request
->handle
, ret
,
1700 "discard failed", errp
);
1703 msg
= g_strdup_printf("invalid request type (%" PRIu32
") received",
1705 ret
= nbd_send_generic_reply(client
, request
->handle
, -EINVAL
, msg
,
1712 /* Owns a reference to the NBDClient passed as opaque. */
1713 static coroutine_fn
void nbd_trip(void *opaque
)
1715 NBDClient
*client
= opaque
;
1716 NBDRequestData
*req
;
1717 NBDRequest request
= { 0 }; /* GCC thinks it can be used uninitialized */
1719 Error
*local_err
= NULL
;
1722 if (client
->closing
) {
1723 nbd_client_put(client
);
1727 req
= nbd_request_get(client
);
1728 ret
= nbd_co_receive_request(req
, &request
, &local_err
);
1729 client
->recv_coroutine
= NULL
;
1731 if (client
->closing
) {
1733 * The client may be closed when we are blocked in
1734 * nbd_co_receive_request()
1739 nbd_client_receive_next_request(client
);
1745 /* It wans't -EIO, so, according to nbd_co_receive_request()
1746 * semantics, we should return the error to the client. */
1747 Error
*export_err
= local_err
;
1750 ret
= nbd_send_generic_reply(client
, request
.handle
, -EINVAL
,
1751 error_get_pretty(export_err
), &local_err
);
1752 error_free(export_err
);
1754 ret
= nbd_handle_request(client
, &request
, req
->data
, &local_err
);
1757 error_prepend(&local_err
, "Failed to send reply: ");
1761 /* We must disconnect after NBD_CMD_WRITE if we did not
1764 if (!req
->complete
) {
1765 error_setg(&local_err
, "Request handling failed in intermediate state");
1770 nbd_request_put(req
);
1771 nbd_client_put(client
);
1776 error_reportf_err(local_err
, "Disconnect client, due to: ");
1778 nbd_request_put(req
);
1779 client_close(client
, true);
1780 nbd_client_put(client
);
1783 static void nbd_client_receive_next_request(NBDClient
*client
)
1785 if (!client
->recv_coroutine
&& client
->nb_requests
< MAX_NBD_REQUESTS
) {
1786 nbd_client_get(client
);
1787 client
->recv_coroutine
= qemu_coroutine_create(nbd_trip
, client
);
1788 aio_co_schedule(client
->exp
->ctx
, client
->recv_coroutine
);
1792 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1794 NBDClient
*client
= opaque
;
1795 NBDExport
*exp
= client
->exp
;
1796 Error
*local_err
= NULL
;
1799 nbd_export_get(exp
);
1800 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1802 qemu_co_mutex_init(&client
->send_lock
);
1804 if (nbd_negotiate(client
, &local_err
)) {
1806 error_report_err(local_err
);
1808 client_close(client
, false);
1812 nbd_client_receive_next_request(client
);
1816 * Create a new client listener on the given export @exp, using the
1817 * given channel @sioc. Begin servicing it in a coroutine. When the
1818 * connection closes, call @close_fn with an indication of whether the
1819 * client completed negotiation.
1821 void nbd_client_new(NBDExport
*exp
,
1822 QIOChannelSocket
*sioc
,
1823 QCryptoTLSCreds
*tlscreds
,
1824 const char *tlsaclname
,
1825 void (*close_fn
)(NBDClient
*, bool))
1830 client
= g_new0(NBDClient
, 1);
1831 client
->refcount
= 1;
1833 client
->tlscreds
= tlscreds
;
1835 object_ref(OBJECT(client
->tlscreds
));
1837 client
->tlsaclname
= g_strdup(tlsaclname
);
1838 client
->sioc
= sioc
;
1839 object_ref(OBJECT(client
->sioc
));
1840 client
->ioc
= QIO_CHANNEL(sioc
);
1841 object_ref(OBJECT(client
->ioc
));
1842 client
->close_fn
= close_fn
;
1844 co
= qemu_coroutine_create(nbd_co_client_start
, client
);
1845 qemu_coroutine_enter(co
);