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 #define NBD_META_ID_BASE_ALLOCATION 0
26 #define NBD_META_ID_DIRTY_BITMAP 1
28 /* NBD_MAX_BITMAP_EXTENTS: 1 mb of extents data. An empirical
29 * constant. If an increase is needed, note that the NBD protocol
30 * recommends no larger than 32 mb, so that the client won't consider
31 * the reply as a denial of service attack. */
32 #define NBD_MAX_BITMAP_EXTENTS (0x100000 / 8)
34 static int system_errno_to_nbd_errno(int err
)
62 /* Definitions for opaque data types */
64 typedef struct NBDRequestData NBDRequestData
;
66 struct NBDRequestData
{
67 QSIMPLEQ_ENTRY(NBDRequestData
) entry
;
75 void (*close
)(NBDExport
*exp
);
83 QTAILQ_HEAD(, NBDClient
) clients
;
84 QTAILQ_ENTRY(NBDExport
) next
;
88 BlockBackend
*eject_notifier_blk
;
89 Notifier eject_notifier
;
91 BdrvDirtyBitmap
*export_bitmap
;
92 char *export_bitmap_context
;
95 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
97 /* NBDExportMetaContexts represents a list of contexts to be exported,
98 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
99 * NBD_OPT_LIST_META_CONTEXT. */
100 typedef struct NBDExportMetaContexts
{
102 bool valid
; /* means that negotiation of the option finished without
104 bool base_allocation
; /* export base:allocation context (block status) */
105 bool bitmap
; /* export qemu:dirty-bitmap:<export bitmap name> */
106 } NBDExportMetaContexts
;
110 void (*close_fn
)(NBDClient
*client
, bool negotiated
);
113 QCryptoTLSCreds
*tlscreds
;
115 QIOChannelSocket
*sioc
; /* The underlying data channel */
116 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
118 Coroutine
*recv_coroutine
;
121 Coroutine
*send_coroutine
;
123 QTAILQ_ENTRY(NBDClient
) next
;
127 bool structured_reply
;
128 NBDExportMetaContexts export_meta
;
130 uint32_t opt
; /* Current option being negotiated */
131 uint32_t optlen
; /* remaining length of data in ioc for the option being
135 static void nbd_client_receive_next_request(NBDClient
*client
);
137 /* Basic flow for negotiation
164 static inline void set_be_option_rep(NBDOptionReply
*rep
, uint32_t option
,
165 uint32_t type
, uint32_t length
)
167 stq_be_p(&rep
->magic
, NBD_REP_MAGIC
);
168 stl_be_p(&rep
->option
, option
);
169 stl_be_p(&rep
->type
, type
);
170 stl_be_p(&rep
->length
, length
);
173 /* Send a reply header, including length, but no payload.
174 * Return -errno on error, 0 on success. */
175 static int nbd_negotiate_send_rep_len(NBDClient
*client
, uint32_t type
,
176 uint32_t len
, Error
**errp
)
180 trace_nbd_negotiate_send_rep_len(client
->opt
, nbd_opt_lookup(client
->opt
),
181 type
, nbd_rep_lookup(type
), len
);
183 assert(len
< NBD_MAX_BUFFER_SIZE
);
185 set_be_option_rep(&rep
, client
->opt
, type
, len
);
186 return nbd_write(client
->ioc
, &rep
, sizeof(rep
), errp
);
189 /* Send a reply header with default 0 length.
190 * Return -errno on error, 0 on success. */
191 static int nbd_negotiate_send_rep(NBDClient
*client
, uint32_t type
,
194 return nbd_negotiate_send_rep_len(client
, type
, 0, errp
);
197 /* Send an error reply.
198 * Return -errno on error, 0 on success. */
199 static int GCC_FMT_ATTR(4, 0)
200 nbd_negotiate_send_rep_verr(NBDClient
*client
, uint32_t type
,
201 Error
**errp
, const char *fmt
, va_list va
)
207 msg
= g_strdup_vprintf(fmt
, va
);
210 trace_nbd_negotiate_send_rep_err(msg
);
211 ret
= nbd_negotiate_send_rep_len(client
, type
, len
, errp
);
215 if (nbd_write(client
->ioc
, msg
, len
, errp
) < 0) {
216 error_prepend(errp
, "write failed (error message): ");
227 /* Send an error reply.
228 * Return -errno on error, 0 on success. */
229 static int GCC_FMT_ATTR(4, 5)
230 nbd_negotiate_send_rep_err(NBDClient
*client
, uint32_t type
,
231 Error
**errp
, const char *fmt
, ...)
237 ret
= nbd_negotiate_send_rep_verr(client
, type
, errp
, fmt
, va
);
242 /* Drop remainder of the current option, and send a reply with the
243 * given error type and message. Return -errno on read or write
244 * failure; or 0 if connection is still live. */
245 static int GCC_FMT_ATTR(4, 0)
246 nbd_opt_vdrop(NBDClient
*client
, uint32_t type
, Error
**errp
,
247 const char *fmt
, va_list va
)
249 int ret
= nbd_drop(client
->ioc
, client
->optlen
, errp
);
253 ret
= nbd_negotiate_send_rep_verr(client
, type
, errp
, fmt
, va
);
258 static int GCC_FMT_ATTR(4, 5)
259 nbd_opt_drop(NBDClient
*client
, uint32_t type
, Error
**errp
,
260 const char *fmt
, ...)
266 ret
= nbd_opt_vdrop(client
, type
, errp
, fmt
, va
);
272 static int GCC_FMT_ATTR(3, 4)
273 nbd_opt_invalid(NBDClient
*client
, Error
**errp
, const char *fmt
, ...)
279 ret
= nbd_opt_vdrop(client
, NBD_REP_ERR_INVALID
, errp
, fmt
, va
);
285 /* Read size bytes from the unparsed payload of the current option.
286 * Return -errno on I/O error, 0 if option was completely handled by
287 * sending a reply about inconsistent lengths, or 1 on success. */
288 static int nbd_opt_read(NBDClient
*client
, void *buffer
, size_t size
,
291 if (size
> client
->optlen
) {
292 return nbd_opt_invalid(client
, errp
,
293 "Inconsistent lengths in option %s",
294 nbd_opt_lookup(client
->opt
));
296 client
->optlen
-= size
;
297 return qio_channel_read_all(client
->ioc
, buffer
, size
, errp
) < 0 ? -EIO
: 1;
300 /* Drop size bytes from the unparsed payload of the current option.
301 * Return -errno on I/O error, 0 if option was completely handled by
302 * sending a reply about inconsistent lengths, or 1 on success. */
303 static int nbd_opt_skip(NBDClient
*client
, size_t size
, Error
**errp
)
305 if (size
> client
->optlen
) {
306 return nbd_opt_invalid(client
, errp
,
307 "Inconsistent lengths in option %s",
308 nbd_opt_lookup(client
->opt
));
310 client
->optlen
-= size
;
311 return nbd_drop(client
->ioc
, size
, errp
) < 0 ? -EIO
: 1;
316 * Read a string with the format:
317 * uint32_t len (<= NBD_MAX_NAME_SIZE)
318 * len bytes string (not 0-terminated)
320 * @name should be enough to store NBD_MAX_NAME_SIZE+1.
321 * If @length is non-null, it will be set to the actual string length.
323 * Return -errno on I/O error, 0 if option was completely handled by
324 * sending a reply about inconsistent lengths, or 1 on success.
326 static int nbd_opt_read_name(NBDClient
*client
, char *name
, uint32_t *length
,
332 ret
= nbd_opt_read(client
, &len
, sizeof(len
), errp
);
336 len
= cpu_to_be32(len
);
338 if (len
> NBD_MAX_NAME_SIZE
) {
339 return nbd_opt_invalid(client
, errp
,
340 "Invalid name length: %" PRIu32
, len
);
343 ret
= nbd_opt_read(client
, name
, len
, errp
);
356 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
357 * Return -errno on error, 0 on success. */
358 static int nbd_negotiate_send_rep_list(NBDClient
*client
, NBDExport
*exp
,
361 size_t name_len
, desc_len
;
363 const char *name
= exp
->name
? exp
->name
: "";
364 const char *desc
= exp
->description
? exp
->description
: "";
365 QIOChannel
*ioc
= client
->ioc
;
368 trace_nbd_negotiate_send_rep_list(name
, desc
);
369 name_len
= strlen(name
);
370 desc_len
= strlen(desc
);
371 len
= name_len
+ desc_len
+ sizeof(len
);
372 ret
= nbd_negotiate_send_rep_len(client
, NBD_REP_SERVER
, len
, errp
);
377 len
= cpu_to_be32(name_len
);
378 if (nbd_write(ioc
, &len
, sizeof(len
), errp
) < 0) {
379 error_prepend(errp
, "write failed (name length): ");
383 if (nbd_write(ioc
, name
, name_len
, errp
) < 0) {
384 error_prepend(errp
, "write failed (name buffer): ");
388 if (nbd_write(ioc
, desc
, desc_len
, errp
) < 0) {
389 error_prepend(errp
, "write failed (description buffer): ");
396 /* Process the NBD_OPT_LIST command, with a potential series of replies.
397 * Return -errno on error, 0 on success. */
398 static int nbd_negotiate_handle_list(NBDClient
*client
, Error
**errp
)
401 assert(client
->opt
== NBD_OPT_LIST
);
403 /* For each export, send a NBD_REP_SERVER reply. */
404 QTAILQ_FOREACH(exp
, &exports
, next
) {
405 if (nbd_negotiate_send_rep_list(client
, exp
, errp
)) {
409 /* Finish with a NBD_REP_ACK. */
410 return nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
413 static void nbd_check_meta_export(NBDClient
*client
)
415 client
->export_meta
.valid
&= client
->exp
== client
->export_meta
.exp
;
418 /* Send a reply to NBD_OPT_EXPORT_NAME.
419 * Return -errno on error, 0 on success. */
420 static int nbd_negotiate_handle_export_name(NBDClient
*client
,
421 uint16_t myflags
, bool no_zeroes
,
424 char name
[NBD_MAX_NAME_SIZE
+ 1];
425 char buf
[NBD_REPLY_EXPORT_NAME_SIZE
] = "";
430 [20 .. xx] export name (length bytes)
433 [ 8 .. 9] export flags
434 [10 .. 133] reserved (0) [unless no_zeroes]
436 trace_nbd_negotiate_handle_export_name();
437 if (client
->optlen
>= sizeof(name
)) {
438 error_setg(errp
, "Bad length received");
441 if (nbd_read(client
->ioc
, name
, client
->optlen
, errp
) < 0) {
442 error_prepend(errp
, "read failed: ");
445 name
[client
->optlen
] = '\0';
448 trace_nbd_negotiate_handle_export_name_request(name
);
450 client
->exp
= nbd_export_find(name
);
452 error_setg(errp
, "export not found");
456 trace_nbd_negotiate_new_style_size_flags(client
->exp
->size
,
457 client
->exp
->nbdflags
| myflags
);
458 stq_be_p(buf
, client
->exp
->size
);
459 stw_be_p(buf
+ 8, client
->exp
->nbdflags
| myflags
);
460 len
= no_zeroes
? 10 : sizeof(buf
);
461 ret
= nbd_write(client
->ioc
, buf
, len
, errp
);
463 error_prepend(errp
, "write failed: ");
467 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
468 nbd_export_get(client
->exp
);
469 nbd_check_meta_export(client
);
474 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
475 * The buffer does NOT include the info type prefix.
476 * Return -errno on error, 0 if ready to send more. */
477 static int nbd_negotiate_send_info(NBDClient
*client
,
478 uint16_t info
, uint32_t length
, void *buf
,
483 trace_nbd_negotiate_send_info(info
, nbd_info_lookup(info
), length
);
484 rc
= nbd_negotiate_send_rep_len(client
, NBD_REP_INFO
,
485 sizeof(info
) + length
, errp
);
489 info
= cpu_to_be16(info
);
490 if (nbd_write(client
->ioc
, &info
, sizeof(info
), errp
) < 0) {
493 if (nbd_write(client
->ioc
, buf
, length
, errp
) < 0) {
499 /* nbd_reject_length: Handle any unexpected payload.
500 * @fatal requests that we quit talking to the client, even if we are able
501 * to successfully send an error reply.
503 * -errno transmission error occurred or @fatal was requested, errp is set
504 * 0 error message successfully sent to client, errp is not set
506 static int nbd_reject_length(NBDClient
*client
, bool fatal
, Error
**errp
)
510 assert(client
->optlen
);
511 ret
= nbd_opt_invalid(client
, errp
, "option '%s' has unexpected length",
512 nbd_opt_lookup(client
->opt
));
514 error_setg(errp
, "option '%s' has unexpected length",
515 nbd_opt_lookup(client
->opt
));
521 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
522 * Return -errno on error, 0 if ready for next option, and 1 to move
523 * into transmission phase. */
524 static int nbd_negotiate_handle_info(NBDClient
*client
, uint16_t myflags
,
528 char name
[NBD_MAX_NAME_SIZE
+ 1];
533 bool sendname
= false;
534 bool blocksize
= false;
536 char buf
[sizeof(uint64_t) + sizeof(uint16_t)];
539 4 bytes: L, name length (can be 0)
541 2 bytes: N, number of requests (can be 0)
542 N * 2 bytes: N requests
544 rc
= nbd_opt_read_name(client
, name
, &namelen
, errp
);
548 trace_nbd_negotiate_handle_export_name_request(name
);
550 rc
= nbd_opt_read(client
, &requests
, sizeof(requests
), errp
);
554 requests
= be16_to_cpu(requests
);
555 trace_nbd_negotiate_handle_info_requests(requests
);
557 rc
= nbd_opt_read(client
, &request
, sizeof(request
), errp
);
561 request
= be16_to_cpu(request
);
562 trace_nbd_negotiate_handle_info_request(request
,
563 nbd_info_lookup(request
));
564 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
565 * everything else is either a request we don't know or
566 * something we send regardless of request */
571 case NBD_INFO_BLOCK_SIZE
:
576 if (client
->optlen
) {
577 return nbd_reject_length(client
, false, errp
);
580 exp
= nbd_export_find(name
);
582 return nbd_negotiate_send_rep_err(client
, NBD_REP_ERR_UNKNOWN
,
583 errp
, "export '%s' not present",
587 /* Don't bother sending NBD_INFO_NAME unless client requested it */
589 rc
= nbd_negotiate_send_info(client
, NBD_INFO_NAME
, namelen
, name
,
596 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
598 if (exp
->description
) {
599 size_t len
= strlen(exp
->description
);
601 rc
= nbd_negotiate_send_info(client
, NBD_INFO_DESCRIPTION
,
602 len
, exp
->description
, errp
);
608 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
609 * according to whether the client requested it, and according to
610 * whether this is OPT_INFO or OPT_GO. */
611 /* minimum - 1 for back-compat, or 512 if client is new enough.
612 * TODO: consult blk_bs(blk)->bl.request_alignment? */
614 (client
->opt
== NBD_OPT_INFO
|| blocksize
) ? BDRV_SECTOR_SIZE
: 1;
615 /* preferred - Hard-code to 4096 for now.
616 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
618 /* maximum - At most 32M, but smaller as appropriate. */
619 sizes
[2] = MIN(blk_get_max_transfer(exp
->blk
), NBD_MAX_BUFFER_SIZE
);
620 trace_nbd_negotiate_handle_info_block_size(sizes
[0], sizes
[1], sizes
[2]);
621 sizes
[0] = cpu_to_be32(sizes
[0]);
622 sizes
[1] = cpu_to_be32(sizes
[1]);
623 sizes
[2] = cpu_to_be32(sizes
[2]);
624 rc
= nbd_negotiate_send_info(client
, NBD_INFO_BLOCK_SIZE
,
625 sizeof(sizes
), sizes
, errp
);
630 /* Send NBD_INFO_EXPORT always */
631 trace_nbd_negotiate_new_style_size_flags(exp
->size
,
632 exp
->nbdflags
| myflags
);
633 stq_be_p(buf
, exp
->size
);
634 stw_be_p(buf
+ 8, exp
->nbdflags
| myflags
);
635 rc
= nbd_negotiate_send_info(client
, NBD_INFO_EXPORT
,
636 sizeof(buf
), buf
, errp
);
641 /* If the client is just asking for NBD_OPT_INFO, but forgot to
642 * request block sizes, return an error.
643 * TODO: consult blk_bs(blk)->request_align, and only error if it
645 if (client
->opt
== NBD_OPT_INFO
&& !blocksize
) {
646 return nbd_negotiate_send_rep_err(client
,
647 NBD_REP_ERR_BLOCK_SIZE_REQD
,
649 "request NBD_INFO_BLOCK_SIZE to "
654 rc
= nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
659 if (client
->opt
== NBD_OPT_GO
) {
661 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
662 nbd_export_get(client
->exp
);
663 nbd_check_meta_export(client
);
670 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
671 * new channel for all further (now-encrypted) communication. */
672 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
677 struct NBDTLSHandshakeData data
= { 0 };
679 assert(client
->opt
== NBD_OPT_STARTTLS
);
681 trace_nbd_negotiate_handle_starttls();
684 if (nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
) < 0) {
688 tioc
= qio_channel_tls_new_server(ioc
,
696 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-server-tls");
697 trace_nbd_negotiate_handle_starttls_handshake();
698 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
699 qio_channel_tls_handshake(tioc
,
705 if (!data
.complete
) {
706 g_main_loop_run(data
.loop
);
708 g_main_loop_unref(data
.loop
);
710 object_unref(OBJECT(tioc
));
711 error_propagate(errp
, data
.error
);
715 return QIO_CHANNEL(tioc
);
718 /* nbd_negotiate_send_meta_context
720 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
722 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
724 static int nbd_negotiate_send_meta_context(NBDClient
*client
,
729 NBDOptionReplyMetaContext opt
;
730 struct iovec iov
[] = {
731 {.iov_base
= &opt
, .iov_len
= sizeof(opt
)},
732 {.iov_base
= (void *)context
, .iov_len
= strlen(context
)}
735 if (client
->opt
== NBD_OPT_LIST_META_CONTEXT
) {
739 trace_nbd_negotiate_meta_query_reply(context
, context_id
);
740 set_be_option_rep(&opt
.h
, client
->opt
, NBD_REP_META_CONTEXT
,
741 sizeof(opt
) - sizeof(opt
.h
) + iov
[1].iov_len
);
742 stl_be_p(&opt
.context_id
, context_id
);
744 return qio_channel_writev_all(client
->ioc
, iov
, 2, errp
) < 0 ? -EIO
: 0;
747 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
748 * @match is never set to false.
750 * Return -errno on I/O error, 0 if option was completely handled by
751 * sending a reply about inconsistent lengths, or 1 on success.
753 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
754 * It only means that there are no errors.
756 static int nbd_meta_pattern(NBDClient
*client
, const char *pattern
, bool *match
,
761 size_t len
= strlen(pattern
);
765 query
= g_malloc(len
);
766 ret
= nbd_opt_read(client
, query
, len
, errp
);
772 if (strncmp(query
, pattern
, len
) == 0) {
773 trace_nbd_negotiate_meta_query_parse(pattern
);
776 trace_nbd_negotiate_meta_query_skip("pattern not matched");
784 * Read @len bytes, and set @match to true if they match @pattern, or if @len
785 * is 0 and the client is performing _LIST_. @match is never set to false.
787 * Return -errno on I/O error, 0 if option was completely handled by
788 * sending a reply about inconsistent lengths, or 1 on success.
790 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
791 * It only means that there are no errors.
793 static int nbd_meta_empty_or_pattern(NBDClient
*client
, const char *pattern
,
794 uint32_t len
, bool *match
, Error
**errp
)
797 if (client
->opt
== NBD_OPT_LIST_META_CONTEXT
) {
800 trace_nbd_negotiate_meta_query_parse("empty");
804 if (len
!= strlen(pattern
)) {
805 trace_nbd_negotiate_meta_query_skip("different lengths");
806 return nbd_opt_skip(client
, len
, errp
);
809 return nbd_meta_pattern(client
, pattern
, match
, errp
);
812 /* nbd_meta_base_query
814 * Handle queries to 'base' namespace. For now, only the base:allocation
815 * context is available. 'len' is the amount of text remaining to be read from
816 * the current name, after the 'base:' portion has been stripped.
818 * Return -errno on I/O error, 0 if option was completely handled by
819 * sending a reply about inconsistent lengths, or 1 on success.
821 static int nbd_meta_base_query(NBDClient
*client
, NBDExportMetaContexts
*meta
,
822 uint32_t len
, Error
**errp
)
824 return nbd_meta_empty_or_pattern(client
, "allocation", len
,
825 &meta
->base_allocation
, errp
);
828 /* nbd_meta_bitmap_query
830 * Handle query to 'qemu:' namespace.
831 * @len is the amount of text remaining to be read from the current name, after
832 * the 'qemu:' portion has been stripped.
834 * Return -errno on I/O error, 0 if option was completely handled by
835 * sending a reply about inconsistent lengths, or 1 on success. */
836 static int nbd_meta_qemu_query(NBDClient
*client
, NBDExportMetaContexts
*meta
,
837 uint32_t len
, Error
**errp
)
839 bool dirty_bitmap
= false;
840 size_t dirty_bitmap_len
= strlen("dirty-bitmap:");
843 if (!meta
->exp
->export_bitmap
) {
844 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
845 return nbd_opt_skip(client
, len
, errp
);
849 if (client
->opt
== NBD_OPT_LIST_META_CONTEXT
) {
852 trace_nbd_negotiate_meta_query_parse("empty");
856 if (len
< dirty_bitmap_len
) {
857 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
858 return nbd_opt_skip(client
, len
, errp
);
861 len
-= dirty_bitmap_len
;
862 ret
= nbd_meta_pattern(client
, "dirty-bitmap:", &dirty_bitmap
, errp
);
867 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
868 return nbd_opt_skip(client
, len
, errp
);
871 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
873 return nbd_meta_empty_or_pattern(
874 client
, meta
->exp
->export_bitmap_context
+
875 strlen("qemu:dirty_bitmap:"), len
, &meta
->bitmap
, errp
);
878 /* nbd_negotiate_meta_query
880 * Parse namespace name and call corresponding function to parse body of the
883 * The only supported namespace now is 'base'.
885 * The function aims not wasting time and memory to read long unknown namespace
888 * Return -errno on I/O error, 0 if option was completely handled by
889 * sending a reply about inconsistent lengths, or 1 on success. */
890 static int nbd_negotiate_meta_query(NBDClient
*client
,
891 NBDExportMetaContexts
*meta
, Error
**errp
)
894 * Both 'qemu' and 'base' namespaces have length = 5 including a
895 * colon. If another length namespace is later introduced, this
896 * should certainly be refactored.
903 ret
= nbd_opt_read(client
, &len
, sizeof(len
), errp
);
907 len
= cpu_to_be32(len
);
910 trace_nbd_negotiate_meta_query_skip("length too short");
911 return nbd_opt_skip(client
, len
, errp
);
915 ret
= nbd_opt_read(client
, ns
, ns_len
, errp
);
920 if (!strncmp(ns
, "base:", ns_len
)) {
921 trace_nbd_negotiate_meta_query_parse("base:");
922 return nbd_meta_base_query(client
, meta
, len
, errp
);
923 } else if (!strncmp(ns
, "qemu:", ns_len
)) {
924 trace_nbd_negotiate_meta_query_parse("qemu:");
925 return nbd_meta_qemu_query(client
, meta
, len
, errp
);
928 trace_nbd_negotiate_meta_query_skip("unknown namespace");
929 return nbd_opt_skip(client
, len
, errp
);
932 /* nbd_negotiate_meta_queries
933 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
935 * Return -errno on I/O error, or 0 if option was completely handled. */
936 static int nbd_negotiate_meta_queries(NBDClient
*client
,
937 NBDExportMetaContexts
*meta
, Error
**errp
)
940 char export_name
[NBD_MAX_NAME_SIZE
+ 1];
941 NBDExportMetaContexts local_meta
;
945 if (!client
->structured_reply
) {
946 return nbd_opt_invalid(client
, errp
,
947 "request option '%s' when structured reply "
949 nbd_opt_lookup(client
->opt
));
952 if (client
->opt
== NBD_OPT_LIST_META_CONTEXT
) {
953 /* Only change the caller's meta on SET. */
957 memset(meta
, 0, sizeof(*meta
));
959 ret
= nbd_opt_read_name(client
, export_name
, NULL
, errp
);
964 meta
->exp
= nbd_export_find(export_name
);
965 if (meta
->exp
== NULL
) {
966 return nbd_opt_drop(client
, NBD_REP_ERR_UNKNOWN
, errp
,
967 "export '%s' not present", export_name
);
970 ret
= nbd_opt_read(client
, &nb_queries
, sizeof(nb_queries
), errp
);
974 nb_queries
= cpu_to_be32(nb_queries
);
975 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client
->opt
),
976 export_name
, nb_queries
);
978 if (client
->opt
== NBD_OPT_LIST_META_CONTEXT
&& !nb_queries
) {
979 /* enable all known contexts */
980 meta
->base_allocation
= true;
982 for (i
= 0; i
< nb_queries
; ++i
) {
983 ret
= nbd_negotiate_meta_query(client
, meta
, errp
);
990 if (meta
->base_allocation
) {
991 ret
= nbd_negotiate_send_meta_context(client
, "base:allocation",
992 NBD_META_ID_BASE_ALLOCATION
,
1000 ret
= nbd_negotiate_send_meta_context(client
,
1001 meta
->exp
->export_bitmap_context
,
1002 NBD_META_ID_DIRTY_BITMAP
,
1009 ret
= nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
1017 /* nbd_negotiate_options
1018 * Process all NBD_OPT_* client option commands, during fixed newstyle
1021 * -errno on error, errp is set
1022 * 0 on successful negotiation, errp is not set
1023 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1026 static int nbd_negotiate_options(NBDClient
*client
, uint16_t myflags
,
1030 bool fixedNewstyle
= false;
1031 bool no_zeroes
= false;
1034 [ 0 .. 3] client flags
1036 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1037 [ 0 .. 7] NBD_OPTS_MAGIC
1038 [ 8 .. 11] NBD option
1039 [12 .. 15] Data length
1042 [ 0 .. 7] NBD_OPTS_MAGIC
1043 [ 8 .. 11] Second NBD option
1044 [12 .. 15] Data length
1048 if (nbd_read(client
->ioc
, &flags
, sizeof(flags
), errp
) < 0) {
1049 error_prepend(errp
, "read failed: ");
1052 flags
= be32_to_cpu(flags
);
1053 trace_nbd_negotiate_options_flags(flags
);
1054 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
1055 fixedNewstyle
= true;
1056 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
1058 if (flags
& NBD_FLAG_C_NO_ZEROES
) {
1060 flags
&= ~NBD_FLAG_C_NO_ZEROES
;
1063 error_setg(errp
, "Unknown client flags 0x%" PRIx32
" received", flags
);
1069 uint32_t option
, length
;
1072 if (nbd_read(client
->ioc
, &magic
, sizeof(magic
), errp
) < 0) {
1073 error_prepend(errp
, "read failed: ");
1076 magic
= be64_to_cpu(magic
);
1077 trace_nbd_negotiate_options_check_magic(magic
);
1078 if (magic
!= NBD_OPTS_MAGIC
) {
1079 error_setg(errp
, "Bad magic received");
1083 if (nbd_read(client
->ioc
, &option
,
1084 sizeof(option
), errp
) < 0) {
1085 error_prepend(errp
, "read failed: ");
1088 option
= be32_to_cpu(option
);
1089 client
->opt
= option
;
1091 if (nbd_read(client
->ioc
, &length
, sizeof(length
), errp
) < 0) {
1092 error_prepend(errp
, "read failed: ");
1095 length
= be32_to_cpu(length
);
1096 assert(!client
->optlen
);
1097 client
->optlen
= length
;
1099 if (length
> NBD_MAX_BUFFER_SIZE
) {
1100 error_setg(errp
, "len (%" PRIu32
" ) is larger than max len (%u)",
1101 length
, NBD_MAX_BUFFER_SIZE
);
1105 trace_nbd_negotiate_options_check_option(option
,
1106 nbd_opt_lookup(option
));
1107 if (client
->tlscreds
&&
1108 client
->ioc
== (QIOChannel
*)client
->sioc
) {
1110 if (!fixedNewstyle
) {
1111 error_setg(errp
, "Unsupported option 0x%" PRIx32
, option
);
1115 case NBD_OPT_STARTTLS
:
1117 /* Unconditionally drop the connection if the client
1118 * can't start a TLS negotiation correctly */
1119 return nbd_reject_length(client
, true, errp
);
1121 tioc
= nbd_negotiate_handle_starttls(client
, errp
);
1126 object_unref(OBJECT(client
->ioc
));
1127 client
->ioc
= QIO_CHANNEL(tioc
);
1130 case NBD_OPT_EXPORT_NAME
:
1131 /* No way to return an error to client, so drop connection */
1132 error_setg(errp
, "Option 0x%x not permitted before TLS",
1137 ret
= nbd_opt_drop(client
, NBD_REP_ERR_TLS_REQD
, errp
,
1139 "not permitted before TLS", option
);
1140 /* Let the client keep trying, unless they asked to
1141 * quit. In this mode, we've already sent an error, so
1142 * we can't ack the abort. */
1143 if (option
== NBD_OPT_ABORT
) {
1148 } else if (fixedNewstyle
) {
1152 ret
= nbd_reject_length(client
, false, errp
);
1154 ret
= nbd_negotiate_handle_list(client
, errp
);
1159 /* NBD spec says we must try to reply before
1160 * disconnecting, but that we must also tolerate
1161 * guests that don't wait for our reply. */
1162 nbd_negotiate_send_rep(client
, NBD_REP_ACK
, NULL
);
1165 case NBD_OPT_EXPORT_NAME
:
1166 return nbd_negotiate_handle_export_name(client
,
1172 ret
= nbd_negotiate_handle_info(client
, myflags
, errp
);
1174 assert(option
== NBD_OPT_GO
);
1179 case NBD_OPT_STARTTLS
:
1181 ret
= nbd_reject_length(client
, false, errp
);
1182 } else if (client
->tlscreds
) {
1183 ret
= nbd_negotiate_send_rep_err(client
,
1184 NBD_REP_ERR_INVALID
, errp
,
1185 "TLS already enabled");
1187 ret
= nbd_negotiate_send_rep_err(client
,
1188 NBD_REP_ERR_POLICY
, errp
,
1189 "TLS not configured");
1193 case NBD_OPT_STRUCTURED_REPLY
:
1195 ret
= nbd_reject_length(client
, false, errp
);
1196 } else if (client
->structured_reply
) {
1197 ret
= nbd_negotiate_send_rep_err(
1198 client
, NBD_REP_ERR_INVALID
, errp
,
1199 "structured reply already negotiated");
1201 ret
= nbd_negotiate_send_rep(client
, NBD_REP_ACK
, errp
);
1202 client
->structured_reply
= true;
1203 myflags
|= NBD_FLAG_SEND_DF
;
1207 case NBD_OPT_LIST_META_CONTEXT
:
1208 case NBD_OPT_SET_META_CONTEXT
:
1209 ret
= nbd_negotiate_meta_queries(client
, &client
->export_meta
,
1214 ret
= nbd_opt_drop(client
, NBD_REP_ERR_UNSUP
, errp
,
1215 "Unsupported option %" PRIu32
" (%s)",
1216 option
, nbd_opt_lookup(option
));
1221 * If broken new-style we should drop the connection
1222 * for anything except NBD_OPT_EXPORT_NAME
1225 case NBD_OPT_EXPORT_NAME
:
1226 return nbd_negotiate_handle_export_name(client
,
1231 error_setg(errp
, "Unsupported option %" PRIu32
" (%s)",
1232 option
, nbd_opt_lookup(option
));
1244 * -errno on error, errp is set
1245 * 0 on successful negotiation, errp is not set
1246 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1249 static coroutine_fn
int nbd_negotiate(NBDClient
*client
, Error
**errp
)
1251 char buf
[NBD_OLDSTYLE_NEGOTIATE_SIZE
] = "";
1253 const uint16_t myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
1254 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
|
1255 NBD_FLAG_SEND_WRITE_ZEROES
| NBD_FLAG_SEND_CACHE
);
1257 /* Old style negotiation header, no room for options
1258 [ 0 .. 7] passwd ("NBDMAGIC")
1259 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1261 [24 .. 27] export flags (zero-extended)
1262 [28 .. 151] reserved (0)
1264 New style negotiation header, client can send options
1265 [ 0 .. 7] passwd ("NBDMAGIC")
1266 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1267 [16 .. 17] server flags (0)
1268 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1271 qio_channel_set_blocking(client
->ioc
, false, NULL
);
1273 trace_nbd_negotiate_begin();
1274 memcpy(buf
, "NBDMAGIC", 8);
1276 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
1277 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
| NBD_FLAG_NO_ZEROES
);
1279 if (nbd_write(client
->ioc
, buf
, 18, errp
) < 0) {
1280 error_prepend(errp
, "write failed: ");
1283 ret
= nbd_negotiate_options(client
, myflags
, errp
);
1286 error_prepend(errp
, "option negotiation failed: ");
1291 assert(!client
->optlen
);
1292 trace_nbd_negotiate_success();
1297 static int nbd_receive_request(QIOChannel
*ioc
, NBDRequest
*request
,
1300 uint8_t buf
[NBD_REQUEST_SIZE
];
1304 ret
= nbd_read(ioc
, buf
, sizeof(buf
), errp
);
1310 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1311 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1312 [ 6 .. 7] type (NBD_CMD_READ, ...)
1318 magic
= ldl_be_p(buf
);
1319 request
->flags
= lduw_be_p(buf
+ 4);
1320 request
->type
= lduw_be_p(buf
+ 6);
1321 request
->handle
= ldq_be_p(buf
+ 8);
1322 request
->from
= ldq_be_p(buf
+ 16);
1323 request
->len
= ldl_be_p(buf
+ 24);
1325 trace_nbd_receive_request(magic
, request
->flags
, request
->type
,
1326 request
->from
, request
->len
);
1328 if (magic
!= NBD_REQUEST_MAGIC
) {
1329 error_setg(errp
, "invalid magic (got 0x%" PRIx32
")", magic
);
1335 #define MAX_NBD_REQUESTS 16
1337 void nbd_client_get(NBDClient
*client
)
1342 void nbd_client_put(NBDClient
*client
)
1344 if (--client
->refcount
== 0) {
1345 /* The last reference should be dropped by client->close,
1346 * which is called by client_close.
1348 assert(client
->closing
);
1350 qio_channel_detach_aio_context(client
->ioc
);
1351 object_unref(OBJECT(client
->sioc
));
1352 object_unref(OBJECT(client
->ioc
));
1353 if (client
->tlscreds
) {
1354 object_unref(OBJECT(client
->tlscreds
));
1356 g_free(client
->tlsaclname
);
1358 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
1359 nbd_export_put(client
->exp
);
1365 static void client_close(NBDClient
*client
, bool negotiated
)
1367 if (client
->closing
) {
1371 client
->closing
= true;
1373 /* Force requests to finish. They will drop their own references,
1374 * then we'll close the socket and free the NBDClient.
1376 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
1379 /* Also tell the client, so that they release their reference. */
1380 if (client
->close_fn
) {
1381 client
->close_fn(client
, negotiated
);
1385 static NBDRequestData
*nbd_request_get(NBDClient
*client
)
1387 NBDRequestData
*req
;
1389 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
1390 client
->nb_requests
++;
1392 req
= g_new0(NBDRequestData
, 1);
1393 nbd_client_get(client
);
1394 req
->client
= client
;
1398 static void nbd_request_put(NBDRequestData
*req
)
1400 NBDClient
*client
= req
->client
;
1403 qemu_vfree(req
->data
);
1407 client
->nb_requests
--;
1408 nbd_client_receive_next_request(client
);
1410 nbd_client_put(client
);
1413 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
1415 NBDExport
*exp
= opaque
;
1418 trace_nbd_blk_aio_attached(exp
->name
, ctx
);
1422 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1423 qio_channel_attach_aio_context(client
->ioc
, ctx
);
1424 if (client
->recv_coroutine
) {
1425 aio_co_schedule(ctx
, client
->recv_coroutine
);
1427 if (client
->send_coroutine
) {
1428 aio_co_schedule(ctx
, client
->send_coroutine
);
1433 static void blk_aio_detach(void *opaque
)
1435 NBDExport
*exp
= opaque
;
1438 trace_nbd_blk_aio_detach(exp
->name
, exp
->ctx
);
1440 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1441 qio_channel_detach_aio_context(client
->ioc
);
1447 static void nbd_eject_notifier(Notifier
*n
, void *data
)
1449 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
1450 nbd_export_close(exp
);
1453 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
, off_t size
,
1454 uint16_t nbdflags
, void (*close
)(NBDExport
*),
1455 bool writethrough
, BlockBackend
*on_eject_blk
,
1460 NBDExport
*exp
= g_new0(NBDExport
, 1);
1465 * NBD exports are used for non-shared storage migration. Make sure
1466 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1467 * access since the export could be available before migration handover.
1469 ctx
= bdrv_get_aio_context(bs
);
1470 aio_context_acquire(ctx
);
1471 bdrv_invalidate_cache(bs
, NULL
);
1472 aio_context_release(ctx
);
1474 /* Don't allow resize while the NBD server is running, otherwise we don't
1475 * care what happens with the node. */
1476 perm
= BLK_PERM_CONSISTENT_READ
;
1477 if ((nbdflags
& NBD_FLAG_READ_ONLY
) == 0) {
1478 perm
|= BLK_PERM_WRITE
;
1480 blk
= blk_new(perm
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
|
1481 BLK_PERM_WRITE
| BLK_PERM_GRAPH_MOD
);
1482 ret
= blk_insert_bs(blk
, bs
, errp
);
1486 blk_set_enable_write_cache(blk
, !writethrough
);
1489 QTAILQ_INIT(&exp
->clients
);
1491 exp
->dev_offset
= dev_offset
;
1492 exp
->nbdflags
= nbdflags
;
1493 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
1494 if (exp
->size
< 0) {
1495 error_setg_errno(errp
, -exp
->size
,
1496 "Failed to determine the NBD export's length");
1499 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
1502 exp
->ctx
= blk_get_aio_context(blk
);
1503 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
1506 blk_ref(on_eject_blk
);
1507 exp
->eject_notifier_blk
= on_eject_blk
;
1508 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
1509 blk_add_remove_bs_notifier(on_eject_blk
, &exp
->eject_notifier
);
1519 NBDExport
*nbd_export_find(const char *name
)
1522 QTAILQ_FOREACH(exp
, &exports
, next
) {
1523 if (strcmp(name
, exp
->name
) == 0) {
1531 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
1533 if (exp
->name
== name
) {
1537 nbd_export_get(exp
);
1538 if (exp
->name
!= NULL
) {
1541 QTAILQ_REMOVE(&exports
, exp
, next
);
1542 nbd_export_put(exp
);
1545 nbd_export_get(exp
);
1546 exp
->name
= g_strdup(name
);
1547 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1549 nbd_export_put(exp
);
1552 void nbd_export_set_description(NBDExport
*exp
, const char *description
)
1554 g_free(exp
->description
);
1555 exp
->description
= g_strdup(description
);
1558 void nbd_export_close(NBDExport
*exp
)
1560 NBDClient
*client
, *next
;
1562 nbd_export_get(exp
);
1563 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1564 client_close(client
, true);
1566 nbd_export_set_name(exp
, NULL
);
1567 nbd_export_set_description(exp
, NULL
);
1568 nbd_export_put(exp
);
1571 void nbd_export_remove(NBDExport
*exp
, NbdServerRemoveMode mode
, Error
**errp
)
1573 if (mode
== NBD_SERVER_REMOVE_MODE_HARD
|| QTAILQ_EMPTY(&exp
->clients
)) {
1574 nbd_export_close(exp
);
1578 assert(mode
== NBD_SERVER_REMOVE_MODE_SAFE
);
1580 error_setg(errp
, "export '%s' still in use", exp
->name
);
1581 error_append_hint(errp
, "Use mode='hard' to force client disconnect\n");
1584 void nbd_export_get(NBDExport
*exp
)
1586 assert(exp
->refcount
> 0);
1590 void nbd_export_put(NBDExport
*exp
)
1592 assert(exp
->refcount
> 0);
1593 if (exp
->refcount
== 1) {
1594 nbd_export_close(exp
);
1597 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1598 * if someone calls nbd_export_put() on named export not through
1599 * nbd_export_set_name() when refcount is 1. So, let's assert that
1602 assert(exp
->refcount
> 0);
1603 if (--exp
->refcount
== 0) {
1604 assert(exp
->name
== NULL
);
1605 assert(exp
->description
== NULL
);
1612 if (exp
->eject_notifier_blk
) {
1613 notifier_remove(&exp
->eject_notifier
);
1614 blk_unref(exp
->eject_notifier_blk
);
1616 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
1617 blk_aio_detach
, exp
);
1618 blk_unref(exp
->blk
);
1622 if (exp
->export_bitmap
) {
1623 bdrv_dirty_bitmap_set_qmp_locked(exp
->export_bitmap
, false);
1624 g_free(exp
->export_bitmap_context
);
1631 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1636 void nbd_export_close_all(void)
1638 NBDExport
*exp
, *next
;
1640 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1641 nbd_export_close(exp
);
1645 static int coroutine_fn
nbd_co_send_iov(NBDClient
*client
, struct iovec
*iov
,
1646 unsigned niov
, Error
**errp
)
1650 g_assert(qemu_in_coroutine());
1651 qemu_co_mutex_lock(&client
->send_lock
);
1652 client
->send_coroutine
= qemu_coroutine_self();
1654 ret
= qio_channel_writev_all(client
->ioc
, iov
, niov
, errp
) < 0 ? -EIO
: 0;
1656 client
->send_coroutine
= NULL
;
1657 qemu_co_mutex_unlock(&client
->send_lock
);
1662 static inline void set_be_simple_reply(NBDSimpleReply
*reply
, uint64_t error
,
1665 stl_be_p(&reply
->magic
, NBD_SIMPLE_REPLY_MAGIC
);
1666 stl_be_p(&reply
->error
, error
);
1667 stq_be_p(&reply
->handle
, handle
);
1670 static int nbd_co_send_simple_reply(NBDClient
*client
,
1677 NBDSimpleReply reply
;
1678 int nbd_err
= system_errno_to_nbd_errno(error
);
1679 struct iovec iov
[] = {
1680 {.iov_base
= &reply
, .iov_len
= sizeof(reply
)},
1681 {.iov_base
= data
, .iov_len
= len
}
1684 trace_nbd_co_send_simple_reply(handle
, nbd_err
, nbd_err_lookup(nbd_err
),
1686 set_be_simple_reply(&reply
, nbd_err
, handle
);
1688 return nbd_co_send_iov(client
, iov
, len
? 2 : 1, errp
);
1691 static inline void set_be_chunk(NBDStructuredReplyChunk
*chunk
, uint16_t flags
,
1692 uint16_t type
, uint64_t handle
, uint32_t length
)
1694 stl_be_p(&chunk
->magic
, NBD_STRUCTURED_REPLY_MAGIC
);
1695 stw_be_p(&chunk
->flags
, flags
);
1696 stw_be_p(&chunk
->type
, type
);
1697 stq_be_p(&chunk
->handle
, handle
);
1698 stl_be_p(&chunk
->length
, length
);
1701 static int coroutine_fn
nbd_co_send_structured_done(NBDClient
*client
,
1705 NBDStructuredReplyChunk chunk
;
1706 struct iovec iov
[] = {
1707 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1710 trace_nbd_co_send_structured_done(handle
);
1711 set_be_chunk(&chunk
, NBD_REPLY_FLAG_DONE
, NBD_REPLY_TYPE_NONE
, handle
, 0);
1713 return nbd_co_send_iov(client
, iov
, 1, errp
);
1716 static int coroutine_fn
nbd_co_send_structured_read(NBDClient
*client
,
1724 NBDStructuredReadData chunk
;
1725 struct iovec iov
[] = {
1726 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1727 {.iov_base
= data
, .iov_len
= size
}
1731 trace_nbd_co_send_structured_read(handle
, offset
, data
, size
);
1732 set_be_chunk(&chunk
.h
, final
? NBD_REPLY_FLAG_DONE
: 0,
1733 NBD_REPLY_TYPE_OFFSET_DATA
, handle
,
1734 sizeof(chunk
) - sizeof(chunk
.h
) + size
);
1735 stq_be_p(&chunk
.offset
, offset
);
1737 return nbd_co_send_iov(client
, iov
, 2, errp
);
1740 static int coroutine_fn
nbd_co_send_structured_error(NBDClient
*client
,
1746 NBDStructuredError chunk
;
1747 int nbd_err
= system_errno_to_nbd_errno(error
);
1748 struct iovec iov
[] = {
1749 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1750 {.iov_base
= (char *)msg
, .iov_len
= msg
? strlen(msg
) : 0},
1754 trace_nbd_co_send_structured_error(handle
, nbd_err
,
1755 nbd_err_lookup(nbd_err
), msg
? msg
: "");
1756 set_be_chunk(&chunk
.h
, NBD_REPLY_FLAG_DONE
, NBD_REPLY_TYPE_ERROR
, handle
,
1757 sizeof(chunk
) - sizeof(chunk
.h
) + iov
[1].iov_len
);
1758 stl_be_p(&chunk
.error
, nbd_err
);
1759 stw_be_p(&chunk
.message_length
, iov
[1].iov_len
);
1761 return nbd_co_send_iov(client
, iov
, 1 + !!iov
[1].iov_len
, errp
);
1764 /* Do a sparse read and send the structured reply to the client.
1765 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1766 * reported to the client, at which point this function succeeds.
1768 static int coroutine_fn
nbd_co_send_sparse_read(NBDClient
*client
,
1776 NBDExport
*exp
= client
->exp
;
1777 size_t progress
= 0;
1779 while (progress
< size
) {
1781 int status
= bdrv_block_status_above(blk_bs(exp
->blk
), NULL
,
1783 size
- progress
, &pnum
, NULL
,
1788 char *msg
= g_strdup_printf("unable to check for holes: %s",
1791 ret
= nbd_co_send_structured_error(client
, handle
, -status
, msg
,
1796 assert(pnum
&& pnum
<= size
- progress
);
1797 final
= progress
+ pnum
== size
;
1798 if (status
& BDRV_BLOCK_ZERO
) {
1799 NBDStructuredReadHole chunk
;
1800 struct iovec iov
[] = {
1801 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1804 trace_nbd_co_send_structured_read_hole(handle
, offset
+ progress
,
1806 set_be_chunk(&chunk
.h
, final
? NBD_REPLY_FLAG_DONE
: 0,
1807 NBD_REPLY_TYPE_OFFSET_HOLE
,
1808 handle
, sizeof(chunk
) - sizeof(chunk
.h
));
1809 stq_be_p(&chunk
.offset
, offset
+ progress
);
1810 stl_be_p(&chunk
.length
, pnum
);
1811 ret
= nbd_co_send_iov(client
, iov
, 1, errp
);
1813 ret
= blk_pread(exp
->blk
, offset
+ progress
+ exp
->dev_offset
,
1814 data
+ progress
, pnum
);
1816 error_setg_errno(errp
, -ret
, "reading from file failed");
1819 ret
= nbd_co_send_structured_read(client
, handle
, offset
+ progress
,
1820 data
+ progress
, pnum
, final
,
1833 * Populate @extents from block status. Update @bytes to be the actual
1834 * length encoded (which may be smaller than the original), and update
1835 * @nb_extents to the number of extents used.
1837 * Returns zero on success and -errno on bdrv_block_status_above failure.
1839 static int blockstatus_to_extents(BlockDriverState
*bs
, uint64_t offset
,
1840 uint64_t *bytes
, NBDExtent
*extents
,
1841 unsigned int *nb_extents
)
1843 uint64_t remaining_bytes
= *bytes
;
1844 NBDExtent
*extent
= extents
, *extents_end
= extents
+ *nb_extents
;
1845 bool first_extent
= true;
1847 assert(*nb_extents
);
1848 while (remaining_bytes
) {
1851 int ret
= bdrv_block_status_above(bs
, NULL
, offset
, remaining_bytes
,
1858 flags
= (ret
& BDRV_BLOCK_ALLOCATED
? 0 : NBD_STATE_HOLE
) |
1859 (ret
& BDRV_BLOCK_ZERO
? NBD_STATE_ZERO
: 0);
1861 remaining_bytes
-= num
;
1864 extent
->flags
= flags
;
1865 extent
->length
= num
;
1866 first_extent
= false;
1870 if (flags
== extent
->flags
) {
1871 /* extend current extent */
1872 extent
->length
+= num
;
1874 if (extent
+ 1 == extents_end
) {
1878 /* start new extent */
1880 extent
->flags
= flags
;
1881 extent
->length
= num
;
1885 extents_end
= extent
+ 1;
1887 for (extent
= extents
; extent
< extents_end
; extent
++) {
1888 extent
->flags
= cpu_to_be32(extent
->flags
);
1889 extent
->length
= cpu_to_be32(extent
->length
);
1892 *bytes
-= remaining_bytes
;
1893 *nb_extents
= extents_end
- extents
;
1898 /* nbd_co_send_extents
1900 * @length is only for tracing purposes (and may be smaller or larger
1901 * than the client's original request). @last controls whether
1902 * NBD_REPLY_FLAG_DONE is sent. @extents should already be in
1903 * big-endian format.
1905 static int nbd_co_send_extents(NBDClient
*client
, uint64_t handle
,
1906 NBDExtent
*extents
, unsigned int nb_extents
,
1907 uint64_t length
, bool last
,
1908 uint32_t context_id
, Error
**errp
)
1910 NBDStructuredMeta chunk
;
1912 struct iovec iov
[] = {
1913 {.iov_base
= &chunk
, .iov_len
= sizeof(chunk
)},
1914 {.iov_base
= extents
, .iov_len
= nb_extents
* sizeof(extents
[0])}
1917 trace_nbd_co_send_extents(handle
, nb_extents
, context_id
, length
, last
);
1918 set_be_chunk(&chunk
.h
, last
? NBD_REPLY_FLAG_DONE
: 0,
1919 NBD_REPLY_TYPE_BLOCK_STATUS
,
1920 handle
, sizeof(chunk
) - sizeof(chunk
.h
) + iov
[1].iov_len
);
1921 stl_be_p(&chunk
.context_id
, context_id
);
1923 return nbd_co_send_iov(client
, iov
, 2, errp
);
1926 /* Get block status from the exported device and send it to the client */
1927 static int nbd_co_send_block_status(NBDClient
*client
, uint64_t handle
,
1928 BlockDriverState
*bs
, uint64_t offset
,
1929 uint32_t length
, bool dont_fragment
,
1930 bool last
, uint32_t context_id
,
1934 unsigned int nb_extents
= dont_fragment
? 1 : NBD_MAX_BITMAP_EXTENTS
;
1935 NBDExtent
*extents
= g_new(NBDExtent
, nb_extents
);
1936 uint64_t final_length
= length
;
1938 ret
= blockstatus_to_extents(bs
, offset
, &final_length
, extents
,
1942 return nbd_co_send_structured_error(
1943 client
, handle
, -ret
, "can't get block status", errp
);
1946 ret
= nbd_co_send_extents(client
, handle
, extents
, nb_extents
,
1947 final_length
, last
, context_id
, errp
);
1955 * Populate @extents from a dirty bitmap. Unless @dont_fragment, the
1956 * final extent may exceed the original @length. Store in @length the
1957 * byte length encoded (which may be smaller or larger than the
1958 * original), and return the number of extents used.
1960 static unsigned int bitmap_to_extents(BdrvDirtyBitmap
*bitmap
, uint64_t offset
,
1961 uint64_t *length
, NBDExtent
*extents
,
1962 unsigned int nb_extents
,
1965 uint64_t begin
= offset
, end
= offset
;
1966 uint64_t overall_end
= offset
+ *length
;
1968 BdrvDirtyBitmapIter
*it
;
1971 bdrv_dirty_bitmap_lock(bitmap
);
1973 it
= bdrv_dirty_iter_new(bitmap
);
1974 dirty
= bdrv_get_dirty_locked(NULL
, bitmap
, offset
);
1976 assert(begin
< overall_end
&& nb_extents
);
1977 while (begin
< overall_end
&& i
< nb_extents
) {
1978 bool next_dirty
= !dirty
;
1981 end
= bdrv_dirty_bitmap_next_zero(bitmap
, begin
);
1983 bdrv_set_dirty_iter(it
, begin
);
1984 end
= bdrv_dirty_iter_next(it
);
1986 if (end
== -1 || end
- begin
> UINT32_MAX
) {
1987 /* Cap to an aligned value < 4G beyond begin. */
1988 end
= MIN(bdrv_dirty_bitmap_size(bitmap
),
1989 begin
+ UINT32_MAX
+ 1 -
1990 bdrv_dirty_bitmap_granularity(bitmap
));
1993 if (dont_fragment
&& end
> overall_end
) {
1997 extents
[i
].length
= cpu_to_be32(end
- begin
);
1998 extents
[i
].flags
= cpu_to_be32(dirty
? NBD_STATE_DIRTY
: 0);
2004 bdrv_dirty_iter_free(it
);
2006 bdrv_dirty_bitmap_unlock(bitmap
);
2008 assert(offset
< end
);
2009 *length
= end
- offset
;
2013 static int nbd_co_send_bitmap(NBDClient
*client
, uint64_t handle
,
2014 BdrvDirtyBitmap
*bitmap
, uint64_t offset
,
2015 uint32_t length
, bool dont_fragment
, bool last
,
2016 uint32_t context_id
, Error
**errp
)
2019 unsigned int nb_extents
= dont_fragment
? 1 : NBD_MAX_BITMAP_EXTENTS
;
2020 NBDExtent
*extents
= g_new(NBDExtent
, nb_extents
);
2021 uint64_t final_length
= length
;
2023 nb_extents
= bitmap_to_extents(bitmap
, offset
, &final_length
, extents
,
2024 nb_extents
, dont_fragment
);
2026 ret
= nbd_co_send_extents(client
, handle
, extents
, nb_extents
,
2027 final_length
, last
, context_id
, errp
);
2034 /* nbd_co_receive_request
2035 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2036 * connection right away, and any other negative value to report an error to
2037 * the client (although the caller may still need to disconnect after reporting
2040 static int nbd_co_receive_request(NBDRequestData
*req
, NBDRequest
*request
,
2043 NBDClient
*client
= req
->client
;
2046 g_assert(qemu_in_coroutine());
2047 assert(client
->recv_coroutine
== qemu_coroutine_self());
2048 if (nbd_receive_request(client
->ioc
, request
, errp
) < 0) {
2052 trace_nbd_co_receive_request_decode_type(request
->handle
, request
->type
,
2053 nbd_cmd_lookup(request
->type
));
2055 if (request
->type
!= NBD_CMD_WRITE
) {
2056 /* No payload, we are ready to read the next request. */
2057 req
->complete
= true;
2060 if (request
->type
== NBD_CMD_DISC
) {
2061 /* Special case: we're going to disconnect without a reply,
2062 * whether or not flags, from, or len are bogus */
2066 if (request
->type
== NBD_CMD_READ
|| request
->type
== NBD_CMD_WRITE
||
2067 request
->type
== NBD_CMD_CACHE
)
2069 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
2070 error_setg(errp
, "len (%" PRIu32
" ) is larger than max len (%u)",
2071 request
->len
, NBD_MAX_BUFFER_SIZE
);
2075 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
2076 if (req
->data
== NULL
) {
2077 error_setg(errp
, "No memory");
2081 if (request
->type
== NBD_CMD_WRITE
) {
2082 if (nbd_read(client
->ioc
, req
->data
, request
->len
, errp
) < 0) {
2083 error_prepend(errp
, "reading from socket failed: ");
2086 req
->complete
= true;
2088 trace_nbd_co_receive_request_payload_received(request
->handle
,
2092 /* Sanity checks. */
2093 if (client
->exp
->nbdflags
& NBD_FLAG_READ_ONLY
&&
2094 (request
->type
== NBD_CMD_WRITE
||
2095 request
->type
== NBD_CMD_WRITE_ZEROES
||
2096 request
->type
== NBD_CMD_TRIM
)) {
2097 error_setg(errp
, "Export is read-only");
2100 if (request
->from
> client
->exp
->size
||
2101 request
->from
+ request
->len
> client
->exp
->size
) {
2102 error_setg(errp
, "operation past EOF; From: %" PRIu64
", Len: %" PRIu32
2103 ", Size: %" PRIu64
, request
->from
, request
->len
,
2104 (uint64_t)client
->exp
->size
);
2105 return (request
->type
== NBD_CMD_WRITE
||
2106 request
->type
== NBD_CMD_WRITE_ZEROES
) ? -ENOSPC
: -EINVAL
;
2108 valid_flags
= NBD_CMD_FLAG_FUA
;
2109 if (request
->type
== NBD_CMD_READ
&& client
->structured_reply
) {
2110 valid_flags
|= NBD_CMD_FLAG_DF
;
2111 } else if (request
->type
== NBD_CMD_WRITE_ZEROES
) {
2112 valid_flags
|= NBD_CMD_FLAG_NO_HOLE
;
2113 } else if (request
->type
== NBD_CMD_BLOCK_STATUS
) {
2114 valid_flags
|= NBD_CMD_FLAG_REQ_ONE
;
2116 if (request
->flags
& ~valid_flags
) {
2117 error_setg(errp
, "unsupported flags for command %s (got 0x%x)",
2118 nbd_cmd_lookup(request
->type
), request
->flags
);
2125 /* Send simple reply without a payload, or a structured error
2126 * @error_msg is ignored if @ret >= 0
2127 * Returns 0 if connection is still live, -errno on failure to talk to client
2129 static coroutine_fn
int nbd_send_generic_reply(NBDClient
*client
,
2132 const char *error_msg
,
2135 if (client
->structured_reply
&& ret
< 0) {
2136 return nbd_co_send_structured_error(client
, handle
, -ret
, error_msg
,
2139 return nbd_co_send_simple_reply(client
, handle
, ret
< 0 ? -ret
: 0,
2144 /* Handle NBD_CMD_READ request.
2145 * Return -errno if sending fails. Other errors are reported directly to the
2146 * client as an error reply. */
2147 static coroutine_fn
int nbd_do_cmd_read(NBDClient
*client
, NBDRequest
*request
,
2148 uint8_t *data
, Error
**errp
)
2151 NBDExport
*exp
= client
->exp
;
2153 assert(request
->type
== NBD_CMD_READ
|| request
->type
== NBD_CMD_CACHE
);
2155 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2156 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
2157 ret
= blk_co_flush(exp
->blk
);
2159 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2160 "flush failed", errp
);
2164 if (client
->structured_reply
&& !(request
->flags
& NBD_CMD_FLAG_DF
) &&
2165 request
->len
&& request
->type
!= NBD_CMD_CACHE
)
2167 return nbd_co_send_sparse_read(client
, request
->handle
, request
->from
,
2168 data
, request
->len
, errp
);
2171 ret
= blk_pread(exp
->blk
, request
->from
+ exp
->dev_offset
, data
,
2173 if (ret
< 0 || request
->type
== NBD_CMD_CACHE
) {
2174 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2175 "reading from file failed", errp
);
2178 if (client
->structured_reply
) {
2180 return nbd_co_send_structured_read(client
, request
->handle
,
2181 request
->from
, data
,
2182 request
->len
, true, errp
);
2184 return nbd_co_send_structured_done(client
, request
->handle
, errp
);
2187 return nbd_co_send_simple_reply(client
, request
->handle
, 0,
2188 data
, request
->len
, errp
);
2192 /* Handle NBD request.
2193 * Return -errno if sending fails. Other errors are reported directly to the
2194 * client as an error reply. */
2195 static coroutine_fn
int nbd_handle_request(NBDClient
*client
,
2196 NBDRequest
*request
,
2197 uint8_t *data
, Error
**errp
)
2201 NBDExport
*exp
= client
->exp
;
2204 switch (request
->type
) {
2207 return nbd_do_cmd_read(client
, request
, data
, errp
);
2211 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
2212 flags
|= BDRV_REQ_FUA
;
2214 ret
= blk_pwrite(exp
->blk
, request
->from
+ exp
->dev_offset
,
2215 data
, request
->len
, flags
);
2216 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2217 "writing to file failed", errp
);
2219 case NBD_CMD_WRITE_ZEROES
:
2221 if (request
->flags
& NBD_CMD_FLAG_FUA
) {
2222 flags
|= BDRV_REQ_FUA
;
2224 if (!(request
->flags
& NBD_CMD_FLAG_NO_HOLE
)) {
2225 flags
|= BDRV_REQ_MAY_UNMAP
;
2227 ret
= blk_pwrite_zeroes(exp
->blk
, request
->from
+ exp
->dev_offset
,
2228 request
->len
, flags
);
2229 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2230 "writing to file failed", errp
);
2233 /* unreachable, thanks to special case in nbd_co_receive_request() */
2237 ret
= blk_co_flush(exp
->blk
);
2238 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2239 "flush failed", errp
);
2242 ret
= blk_co_pdiscard(exp
->blk
, request
->from
+ exp
->dev_offset
,
2244 if (ret
== 0 && request
->flags
& NBD_CMD_FLAG_FUA
) {
2245 ret
= blk_co_flush(exp
->blk
);
2247 return nbd_send_generic_reply(client
, request
->handle
, ret
,
2248 "discard failed", errp
);
2250 case NBD_CMD_BLOCK_STATUS
:
2251 if (!request
->len
) {
2252 return nbd_send_generic_reply(client
, request
->handle
, -EINVAL
,
2253 "need non-zero length", errp
);
2255 if (client
->export_meta
.valid
&&
2256 (client
->export_meta
.base_allocation
||
2257 client
->export_meta
.bitmap
))
2259 bool dont_fragment
= request
->flags
& NBD_CMD_FLAG_REQ_ONE
;
2261 if (client
->export_meta
.base_allocation
) {
2262 ret
= nbd_co_send_block_status(client
, request
->handle
,
2263 blk_bs(exp
->blk
), request
->from
,
2264 request
->len
, dont_fragment
,
2265 !client
->export_meta
.bitmap
,
2266 NBD_META_ID_BASE_ALLOCATION
,
2273 if (client
->export_meta
.bitmap
) {
2274 ret
= nbd_co_send_bitmap(client
, request
->handle
,
2275 client
->exp
->export_bitmap
,
2276 request
->from
, request
->len
,
2278 true, NBD_META_ID_DIRTY_BITMAP
, errp
);
2286 return nbd_send_generic_reply(client
, request
->handle
, -EINVAL
,
2287 "CMD_BLOCK_STATUS not negotiated",
2292 msg
= g_strdup_printf("invalid request type (%" PRIu32
") received",
2294 ret
= nbd_send_generic_reply(client
, request
->handle
, -EINVAL
, msg
,
2301 /* Owns a reference to the NBDClient passed as opaque. */
2302 static coroutine_fn
void nbd_trip(void *opaque
)
2304 NBDClient
*client
= opaque
;
2305 NBDRequestData
*req
;
2306 NBDRequest request
= { 0 }; /* GCC thinks it can be used uninitialized */
2308 Error
*local_err
= NULL
;
2311 if (client
->closing
) {
2312 nbd_client_put(client
);
2316 req
= nbd_request_get(client
);
2317 ret
= nbd_co_receive_request(req
, &request
, &local_err
);
2318 client
->recv_coroutine
= NULL
;
2320 if (client
->closing
) {
2322 * The client may be closed when we are blocked in
2323 * nbd_co_receive_request()
2328 nbd_client_receive_next_request(client
);
2334 /* It wans't -EIO, so, according to nbd_co_receive_request()
2335 * semantics, we should return the error to the client. */
2336 Error
*export_err
= local_err
;
2339 ret
= nbd_send_generic_reply(client
, request
.handle
, -EINVAL
,
2340 error_get_pretty(export_err
), &local_err
);
2341 error_free(export_err
);
2343 ret
= nbd_handle_request(client
, &request
, req
->data
, &local_err
);
2346 error_prepend(&local_err
, "Failed to send reply: ");
2350 /* We must disconnect after NBD_CMD_WRITE if we did not
2353 if (!req
->complete
) {
2354 error_setg(&local_err
, "Request handling failed in intermediate state");
2359 nbd_request_put(req
);
2360 nbd_client_put(client
);
2365 error_reportf_err(local_err
, "Disconnect client, due to: ");
2367 nbd_request_put(req
);
2368 client_close(client
, true);
2369 nbd_client_put(client
);
2372 static void nbd_client_receive_next_request(NBDClient
*client
)
2374 if (!client
->recv_coroutine
&& client
->nb_requests
< MAX_NBD_REQUESTS
) {
2375 nbd_client_get(client
);
2376 client
->recv_coroutine
= qemu_coroutine_create(nbd_trip
, client
);
2377 aio_co_schedule(client
->exp
->ctx
, client
->recv_coroutine
);
2381 static coroutine_fn
void nbd_co_client_start(void *opaque
)
2383 NBDClient
*client
= opaque
;
2384 Error
*local_err
= NULL
;
2386 qemu_co_mutex_init(&client
->send_lock
);
2388 if (nbd_negotiate(client
, &local_err
)) {
2390 error_report_err(local_err
);
2392 client_close(client
, false);
2396 nbd_client_receive_next_request(client
);
2400 * Create a new client listener using the given channel @sioc.
2401 * Begin servicing it in a coroutine. When the connection closes, call
2402 * @close_fn with an indication of whether the client completed negotiation.
2404 void nbd_client_new(QIOChannelSocket
*sioc
,
2405 QCryptoTLSCreds
*tlscreds
,
2406 const char *tlsaclname
,
2407 void (*close_fn
)(NBDClient
*, bool))
2412 client
= g_new0(NBDClient
, 1);
2413 client
->refcount
= 1;
2414 client
->tlscreds
= tlscreds
;
2416 object_ref(OBJECT(client
->tlscreds
));
2418 client
->tlsaclname
= g_strdup(tlsaclname
);
2419 client
->sioc
= sioc
;
2420 object_ref(OBJECT(client
->sioc
));
2421 client
->ioc
= QIO_CHANNEL(sioc
);
2422 object_ref(OBJECT(client
->ioc
));
2423 client
->close_fn
= close_fn
;
2425 co
= qemu_coroutine_create(nbd_co_client_start
, client
);
2426 qemu_coroutine_enter(co
);
2429 void nbd_export_bitmap(NBDExport
*exp
, const char *bitmap
,
2430 const char *bitmap_export_name
, Error
**errp
)
2432 BdrvDirtyBitmap
*bm
= NULL
;
2433 BlockDriverState
*bs
= blk_bs(exp
->blk
);
2435 if (exp
->export_bitmap
) {
2436 error_setg(errp
, "Export bitmap is already set");
2441 bm
= bdrv_find_dirty_bitmap(bs
, bitmap
);
2442 if (bm
!= NULL
|| bs
->backing
== NULL
) {
2446 bs
= bs
->backing
->bs
;
2450 error_setg(errp
, "Bitmap '%s' is not found", bitmap
);
2454 if (bdrv_dirty_bitmap_enabled(bm
)) {
2455 error_setg(errp
, "Bitmap '%s' is enabled", bitmap
);
2459 if (bdrv_dirty_bitmap_qmp_locked(bm
)) {
2460 error_setg(errp
, "Bitmap '%s' is locked", bitmap
);
2464 bdrv_dirty_bitmap_set_qmp_locked(bm
, true);
2465 exp
->export_bitmap
= bm
;
2466 exp
->export_bitmap_context
=
2467 g_strdup_printf("qemu:dirty-bitmap:%s", bitmap_export_name
);