xref: /openbmc/qemu/nbd/server.c (revision b6076afc)
1 /*
2  *  Copyright (C) 2016-2018 Red Hat, Inc.
3  *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
4  *
5  *  Network Block Device Server Side
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "qemu/osdep.h"
21 
22 #include "block/export.h"
23 #include "qapi/error.h"
24 #include "qemu/queue.h"
25 #include "trace.h"
26 #include "nbd-internal.h"
27 #include "qemu/units.h"
28 
29 #define NBD_META_ID_BASE_ALLOCATION 0
30 #define NBD_META_ID_DIRTY_BITMAP 1
31 
32 /*
33  * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
34  * constant. If an increase is needed, note that the NBD protocol
35  * recommends no larger than 32 mb, so that the client won't consider
36  * the reply as a denial of service attack.
37  */
38 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
39 
40 static int system_errno_to_nbd_errno(int err)
41 {
42     switch (err) {
43     case 0:
44         return NBD_SUCCESS;
45     case EPERM:
46     case EROFS:
47         return NBD_EPERM;
48     case EIO:
49         return NBD_EIO;
50     case ENOMEM:
51         return NBD_ENOMEM;
52 #ifdef EDQUOT
53     case EDQUOT:
54 #endif
55     case EFBIG:
56     case ENOSPC:
57         return NBD_ENOSPC;
58     case EOVERFLOW:
59         return NBD_EOVERFLOW;
60     case ENOTSUP:
61 #if ENOTSUP != EOPNOTSUPP
62     case EOPNOTSUPP:
63 #endif
64         return NBD_ENOTSUP;
65     case ESHUTDOWN:
66         return NBD_ESHUTDOWN;
67     case EINVAL:
68     default:
69         return NBD_EINVAL;
70     }
71 }
72 
73 /* Definitions for opaque data types */
74 
75 typedef struct NBDRequestData NBDRequestData;
76 
77 struct NBDRequestData {
78     QSIMPLEQ_ENTRY(NBDRequestData) entry;
79     NBDClient *client;
80     uint8_t *data;
81     bool complete;
82 };
83 
84 struct NBDExport {
85     BlockExport common;
86 
87     BlockBackend *blk;
88     char *name;
89     char *description;
90     uint64_t size;
91     uint16_t nbdflags;
92     QTAILQ_HEAD(, NBDClient) clients;
93     QTAILQ_ENTRY(NBDExport) next;
94 
95     BlockBackend *eject_notifier_blk;
96     Notifier eject_notifier;
97 
98     BdrvDirtyBitmap *export_bitmap;
99     char *export_bitmap_context;
100 };
101 
102 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
103 static QTAILQ_HEAD(, NBDExport) closed_exports =
104         QTAILQ_HEAD_INITIALIZER(closed_exports);
105 
106 /* NBDExportMetaContexts represents a list of contexts to be exported,
107  * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
108  * NBD_OPT_LIST_META_CONTEXT. */
109 typedef struct NBDExportMetaContexts {
110     NBDExport *exp;
111     bool valid; /* means that negotiation of the option finished without
112                    errors */
113     bool base_allocation; /* export base:allocation context (block status) */
114     bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
115 } NBDExportMetaContexts;
116 
117 struct NBDClient {
118     int refcount;
119     void (*close_fn)(NBDClient *client, bool negotiated);
120 
121     NBDExport *exp;
122     QCryptoTLSCreds *tlscreds;
123     char *tlsauthz;
124     QIOChannelSocket *sioc; /* The underlying data channel */
125     QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
126 
127     Coroutine *recv_coroutine;
128 
129     CoMutex send_lock;
130     Coroutine *send_coroutine;
131 
132     QTAILQ_ENTRY(NBDClient) next;
133     int nb_requests;
134     bool closing;
135 
136     uint32_t check_align; /* If non-zero, check for aligned client requests */
137 
138     bool structured_reply;
139     NBDExportMetaContexts export_meta;
140 
141     uint32_t opt; /* Current option being negotiated */
142     uint32_t optlen; /* remaining length of data in ioc for the option being
143                         negotiated now */
144 };
145 
146 static void nbd_client_receive_next_request(NBDClient *client);
147 
148 /* Basic flow for negotiation
149 
150    Server         Client
151    Negotiate
152 
153    or
154 
155    Server         Client
156    Negotiate #1
157                   Option
158    Negotiate #2
159 
160    ----
161 
162    followed by
163 
164    Server         Client
165                   Request
166    Response
167                   Request
168    Response
169                   ...
170    ...
171                   Request (type == 2)
172 
173 */
174 
175 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
176                                      uint32_t type, uint32_t length)
177 {
178     stq_be_p(&rep->magic, NBD_REP_MAGIC);
179     stl_be_p(&rep->option, option);
180     stl_be_p(&rep->type, type);
181     stl_be_p(&rep->length, length);
182 }
183 
184 /* Send a reply header, including length, but no payload.
185  * Return -errno on error, 0 on success. */
186 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
187                                       uint32_t len, Error **errp)
188 {
189     NBDOptionReply rep;
190 
191     trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
192                                      type, nbd_rep_lookup(type), len);
193 
194     assert(len < NBD_MAX_BUFFER_SIZE);
195 
196     set_be_option_rep(&rep, client->opt, type, len);
197     return nbd_write(client->ioc, &rep, sizeof(rep), errp);
198 }
199 
200 /* Send a reply header with default 0 length.
201  * Return -errno on error, 0 on success. */
202 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
203                                   Error **errp)
204 {
205     return nbd_negotiate_send_rep_len(client, type, 0, errp);
206 }
207 
208 /* Send an error reply.
209  * Return -errno on error, 0 on success. */
210 static int GCC_FMT_ATTR(4, 0)
211 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
212                             Error **errp, const char *fmt, va_list va)
213 {
214     ERRP_GUARD();
215     g_autofree char *msg = NULL;
216     int ret;
217     size_t len;
218 
219     msg = g_strdup_vprintf(fmt, va);
220     len = strlen(msg);
221     assert(len < NBD_MAX_STRING_SIZE);
222     trace_nbd_negotiate_send_rep_err(msg);
223     ret = nbd_negotiate_send_rep_len(client, type, len, errp);
224     if (ret < 0) {
225         return ret;
226     }
227     if (nbd_write(client->ioc, msg, len, errp) < 0) {
228         error_prepend(errp, "write failed (error message): ");
229         return -EIO;
230     }
231 
232     return 0;
233 }
234 
235 /*
236  * Return a malloc'd copy of @name suitable for use in an error reply.
237  */
238 static char *
239 nbd_sanitize_name(const char *name)
240 {
241     if (strnlen(name, 80) < 80) {
242         return g_strdup(name);
243     }
244     /* XXX Should we also try to sanitize any control characters? */
245     return g_strdup_printf("%.80s...", name);
246 }
247 
248 /* Send an error reply.
249  * Return -errno on error, 0 on success. */
250 static int GCC_FMT_ATTR(4, 5)
251 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
252                            Error **errp, const char *fmt, ...)
253 {
254     va_list va;
255     int ret;
256 
257     va_start(va, fmt);
258     ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
259     va_end(va);
260     return ret;
261 }
262 
263 /* Drop remainder of the current option, and send a reply with the
264  * given error type and message. Return -errno on read or write
265  * failure; or 0 if connection is still live. */
266 static int GCC_FMT_ATTR(4, 0)
267 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
268               const char *fmt, va_list va)
269 {
270     int ret = nbd_drop(client->ioc, client->optlen, errp);
271 
272     client->optlen = 0;
273     if (!ret) {
274         ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
275     }
276     return ret;
277 }
278 
279 static int GCC_FMT_ATTR(4, 5)
280 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
281              const char *fmt, ...)
282 {
283     int ret;
284     va_list va;
285 
286     va_start(va, fmt);
287     ret = nbd_opt_vdrop(client, type, errp, fmt, va);
288     va_end(va);
289 
290     return ret;
291 }
292 
293 static int GCC_FMT_ATTR(3, 4)
294 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
295 {
296     int ret;
297     va_list va;
298 
299     va_start(va, fmt);
300     ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
301     va_end(va);
302 
303     return ret;
304 }
305 
306 /* Read size bytes from the unparsed payload of the current option.
307  * Return -errno on I/O error, 0 if option was completely handled by
308  * sending a reply about inconsistent lengths, or 1 on success. */
309 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
310                         Error **errp)
311 {
312     if (size > client->optlen) {
313         return nbd_opt_invalid(client, errp,
314                                "Inconsistent lengths in option %s",
315                                nbd_opt_lookup(client->opt));
316     }
317     client->optlen -= size;
318     return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
319 }
320 
321 /* Drop size bytes from the unparsed payload of the current option.
322  * Return -errno on I/O error, 0 if option was completely handled by
323  * sending a reply about inconsistent lengths, or 1 on success. */
324 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
325 {
326     if (size > client->optlen) {
327         return nbd_opt_invalid(client, errp,
328                                "Inconsistent lengths in option %s",
329                                nbd_opt_lookup(client->opt));
330     }
331     client->optlen -= size;
332     return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
333 }
334 
335 /* nbd_opt_read_name
336  *
337  * Read a string with the format:
338  *   uint32_t len     (<= NBD_MAX_STRING_SIZE)
339  *   len bytes string (not 0-terminated)
340  *
341  * On success, @name will be allocated.
342  * If @length is non-null, it will be set to the actual string length.
343  *
344  * Return -errno on I/O error, 0 if option was completely handled by
345  * sending a reply about inconsistent lengths, or 1 on success.
346  */
347 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
348                              Error **errp)
349 {
350     int ret;
351     uint32_t len;
352     g_autofree char *local_name = NULL;
353 
354     *name = NULL;
355     ret = nbd_opt_read(client, &len, sizeof(len), errp);
356     if (ret <= 0) {
357         return ret;
358     }
359     len = cpu_to_be32(len);
360 
361     if (len > NBD_MAX_STRING_SIZE) {
362         return nbd_opt_invalid(client, errp,
363                                "Invalid name length: %" PRIu32, len);
364     }
365 
366     local_name = g_malloc(len + 1);
367     ret = nbd_opt_read(client, local_name, len, errp);
368     if (ret <= 0) {
369         return ret;
370     }
371     local_name[len] = '\0';
372 
373     if (length) {
374         *length = len;
375     }
376     *name = g_steal_pointer(&local_name);
377 
378     return 1;
379 }
380 
381 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
382  * Return -errno on error, 0 on success. */
383 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
384                                        Error **errp)
385 {
386     ERRP_GUARD();
387     size_t name_len, desc_len;
388     uint32_t len;
389     const char *name = exp->name ? exp->name : "";
390     const char *desc = exp->description ? exp->description : "";
391     QIOChannel *ioc = client->ioc;
392     int ret;
393 
394     trace_nbd_negotiate_send_rep_list(name, desc);
395     name_len = strlen(name);
396     desc_len = strlen(desc);
397     assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
398     len = name_len + desc_len + sizeof(len);
399     ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
400     if (ret < 0) {
401         return ret;
402     }
403 
404     len = cpu_to_be32(name_len);
405     if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
406         error_prepend(errp, "write failed (name length): ");
407         return -EINVAL;
408     }
409 
410     if (nbd_write(ioc, name, name_len, errp) < 0) {
411         error_prepend(errp, "write failed (name buffer): ");
412         return -EINVAL;
413     }
414 
415     if (nbd_write(ioc, desc, desc_len, errp) < 0) {
416         error_prepend(errp, "write failed (description buffer): ");
417         return -EINVAL;
418     }
419 
420     return 0;
421 }
422 
423 /* Process the NBD_OPT_LIST command, with a potential series of replies.
424  * Return -errno on error, 0 on success. */
425 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
426 {
427     NBDExport *exp;
428     assert(client->opt == NBD_OPT_LIST);
429 
430     /* For each export, send a NBD_REP_SERVER reply. */
431     QTAILQ_FOREACH(exp, &exports, next) {
432         if (nbd_negotiate_send_rep_list(client, exp, errp)) {
433             return -EINVAL;
434         }
435     }
436     /* Finish with a NBD_REP_ACK. */
437     return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
438 }
439 
440 static void nbd_check_meta_export(NBDClient *client)
441 {
442     client->export_meta.valid &= client->exp == client->export_meta.exp;
443 }
444 
445 /* Send a reply to NBD_OPT_EXPORT_NAME.
446  * Return -errno on error, 0 on success. */
447 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
448                                             Error **errp)
449 {
450     ERRP_GUARD();
451     g_autofree char *name = NULL;
452     char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
453     size_t len;
454     int ret;
455     uint16_t myflags;
456 
457     /* Client sends:
458         [20 ..  xx]   export name (length bytes)
459        Server replies:
460         [ 0 ..   7]   size
461         [ 8 ..   9]   export flags
462         [10 .. 133]   reserved     (0) [unless no_zeroes]
463      */
464     trace_nbd_negotiate_handle_export_name();
465     if (client->optlen > NBD_MAX_STRING_SIZE) {
466         error_setg(errp, "Bad length received");
467         return -EINVAL;
468     }
469     name = g_malloc(client->optlen + 1);
470     if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
471         return -EIO;
472     }
473     name[client->optlen] = '\0';
474     client->optlen = 0;
475 
476     trace_nbd_negotiate_handle_export_name_request(name);
477 
478     client->exp = nbd_export_find(name);
479     if (!client->exp) {
480         error_setg(errp, "export not found");
481         return -EINVAL;
482     }
483 
484     myflags = client->exp->nbdflags;
485     if (client->structured_reply) {
486         myflags |= NBD_FLAG_SEND_DF;
487     }
488     trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
489     stq_be_p(buf, client->exp->size);
490     stw_be_p(buf + 8, myflags);
491     len = no_zeroes ? 10 : sizeof(buf);
492     ret = nbd_write(client->ioc, buf, len, errp);
493     if (ret < 0) {
494         error_prepend(errp, "write failed: ");
495         return ret;
496     }
497 
498     QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
499     blk_exp_ref(&client->exp->common);
500     nbd_check_meta_export(client);
501 
502     return 0;
503 }
504 
505 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
506  * The buffer does NOT include the info type prefix.
507  * Return -errno on error, 0 if ready to send more. */
508 static int nbd_negotiate_send_info(NBDClient *client,
509                                    uint16_t info, uint32_t length, void *buf,
510                                    Error **errp)
511 {
512     int rc;
513 
514     trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
515     rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
516                                     sizeof(info) + length, errp);
517     if (rc < 0) {
518         return rc;
519     }
520     info = cpu_to_be16(info);
521     if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
522         return -EIO;
523     }
524     if (nbd_write(client->ioc, buf, length, errp) < 0) {
525         return -EIO;
526     }
527     return 0;
528 }
529 
530 /* nbd_reject_length: Handle any unexpected payload.
531  * @fatal requests that we quit talking to the client, even if we are able
532  * to successfully send an error reply.
533  * Return:
534  * -errno  transmission error occurred or @fatal was requested, errp is set
535  * 0       error message successfully sent to client, errp is not set
536  */
537 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
538 {
539     int ret;
540 
541     assert(client->optlen);
542     ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
543                           nbd_opt_lookup(client->opt));
544     if (fatal && !ret) {
545         error_setg(errp, "option '%s' has unexpected length",
546                    nbd_opt_lookup(client->opt));
547         return -EINVAL;
548     }
549     return ret;
550 }
551 
552 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
553  * Return -errno on error, 0 if ready for next option, and 1 to move
554  * into transmission phase.  */
555 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
556 {
557     int rc;
558     g_autofree char *name = NULL;
559     NBDExport *exp;
560     uint16_t requests;
561     uint16_t request;
562     uint32_t namelen;
563     bool sendname = false;
564     bool blocksize = false;
565     uint32_t sizes[3];
566     char buf[sizeof(uint64_t) + sizeof(uint16_t)];
567     uint32_t check_align = 0;
568     uint16_t myflags;
569 
570     /* Client sends:
571         4 bytes: L, name length (can be 0)
572         L bytes: export name
573         2 bytes: N, number of requests (can be 0)
574         N * 2 bytes: N requests
575     */
576     rc = nbd_opt_read_name(client, &name, &namelen, errp);
577     if (rc <= 0) {
578         return rc;
579     }
580     trace_nbd_negotiate_handle_export_name_request(name);
581 
582     rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
583     if (rc <= 0) {
584         return rc;
585     }
586     requests = be16_to_cpu(requests);
587     trace_nbd_negotiate_handle_info_requests(requests);
588     while (requests--) {
589         rc = nbd_opt_read(client, &request, sizeof(request), errp);
590         if (rc <= 0) {
591             return rc;
592         }
593         request = be16_to_cpu(request);
594         trace_nbd_negotiate_handle_info_request(request,
595                                                 nbd_info_lookup(request));
596         /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
597          * everything else is either a request we don't know or
598          * something we send regardless of request */
599         switch (request) {
600         case NBD_INFO_NAME:
601             sendname = true;
602             break;
603         case NBD_INFO_BLOCK_SIZE:
604             blocksize = true;
605             break;
606         }
607     }
608     if (client->optlen) {
609         return nbd_reject_length(client, false, errp);
610     }
611 
612     exp = nbd_export_find(name);
613     if (!exp) {
614         g_autofree char *sane_name = nbd_sanitize_name(name);
615 
616         return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
617                                           errp, "export '%s' not present",
618                                           sane_name);
619     }
620 
621     /* Don't bother sending NBD_INFO_NAME unless client requested it */
622     if (sendname) {
623         rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
624                                      errp);
625         if (rc < 0) {
626             return rc;
627         }
628     }
629 
630     /* Send NBD_INFO_DESCRIPTION only if available, regardless of
631      * client request */
632     if (exp->description) {
633         size_t len = strlen(exp->description);
634 
635         assert(len <= NBD_MAX_STRING_SIZE);
636         rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
637                                      len, exp->description, errp);
638         if (rc < 0) {
639             return rc;
640         }
641     }
642 
643     /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
644      * according to whether the client requested it, and according to
645      * whether this is OPT_INFO or OPT_GO. */
646     /* minimum - 1 for back-compat, or actual if client will obey it. */
647     if (client->opt == NBD_OPT_INFO || blocksize) {
648         check_align = sizes[0] = blk_get_request_alignment(exp->blk);
649     } else {
650         sizes[0] = 1;
651     }
652     assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
653     /* preferred - Hard-code to 4096 for now.
654      * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
655     sizes[1] = MAX(4096, sizes[0]);
656     /* maximum - At most 32M, but smaller as appropriate. */
657     sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
658     trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
659     sizes[0] = cpu_to_be32(sizes[0]);
660     sizes[1] = cpu_to_be32(sizes[1]);
661     sizes[2] = cpu_to_be32(sizes[2]);
662     rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
663                                  sizeof(sizes), sizes, errp);
664     if (rc < 0) {
665         return rc;
666     }
667 
668     /* Send NBD_INFO_EXPORT always */
669     myflags = exp->nbdflags;
670     if (client->structured_reply) {
671         myflags |= NBD_FLAG_SEND_DF;
672     }
673     trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
674     stq_be_p(buf, exp->size);
675     stw_be_p(buf + 8, myflags);
676     rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
677                                  sizeof(buf), buf, errp);
678     if (rc < 0) {
679         return rc;
680     }
681 
682     /*
683      * If the client is just asking for NBD_OPT_INFO, but forgot to
684      * request block sizes in a situation that would impact
685      * performance, then return an error. But for NBD_OPT_GO, we
686      * tolerate all clients, regardless of alignments.
687      */
688     if (client->opt == NBD_OPT_INFO && !blocksize &&
689         blk_get_request_alignment(exp->blk) > 1) {
690         return nbd_negotiate_send_rep_err(client,
691                                           NBD_REP_ERR_BLOCK_SIZE_REQD,
692                                           errp,
693                                           "request NBD_INFO_BLOCK_SIZE to "
694                                           "use this export");
695     }
696 
697     /* Final reply */
698     rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
699     if (rc < 0) {
700         return rc;
701     }
702 
703     if (client->opt == NBD_OPT_GO) {
704         client->exp = exp;
705         client->check_align = check_align;
706         QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
707         blk_exp_ref(&client->exp->common);
708         nbd_check_meta_export(client);
709         rc = 1;
710     }
711     return rc;
712 }
713 
714 
715 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
716  * new channel for all further (now-encrypted) communication. */
717 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
718                                                  Error **errp)
719 {
720     QIOChannel *ioc;
721     QIOChannelTLS *tioc;
722     struct NBDTLSHandshakeData data = { 0 };
723 
724     assert(client->opt == NBD_OPT_STARTTLS);
725 
726     trace_nbd_negotiate_handle_starttls();
727     ioc = client->ioc;
728 
729     if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
730         return NULL;
731     }
732 
733     tioc = qio_channel_tls_new_server(ioc,
734                                       client->tlscreds,
735                                       client->tlsauthz,
736                                       errp);
737     if (!tioc) {
738         return NULL;
739     }
740 
741     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
742     trace_nbd_negotiate_handle_starttls_handshake();
743     data.loop = g_main_loop_new(g_main_context_default(), FALSE);
744     qio_channel_tls_handshake(tioc,
745                               nbd_tls_handshake,
746                               &data,
747                               NULL,
748                               NULL);
749 
750     if (!data.complete) {
751         g_main_loop_run(data.loop);
752     }
753     g_main_loop_unref(data.loop);
754     if (data.error) {
755         object_unref(OBJECT(tioc));
756         error_propagate(errp, data.error);
757         return NULL;
758     }
759 
760     return QIO_CHANNEL(tioc);
761 }
762 
763 /* nbd_negotiate_send_meta_context
764  *
765  * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
766  *
767  * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
768  */
769 static int nbd_negotiate_send_meta_context(NBDClient *client,
770                                            const char *context,
771                                            uint32_t context_id,
772                                            Error **errp)
773 {
774     NBDOptionReplyMetaContext opt;
775     struct iovec iov[] = {
776         {.iov_base = &opt, .iov_len = sizeof(opt)},
777         {.iov_base = (void *)context, .iov_len = strlen(context)}
778     };
779 
780     assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
781     if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
782         context_id = 0;
783     }
784 
785     trace_nbd_negotiate_meta_query_reply(context, context_id);
786     set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
787                       sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
788     stl_be_p(&opt.context_id, context_id);
789 
790     return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
791 }
792 
793 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
794  * @match is never set to false.
795  *
796  * Return -errno on I/O error, 0 if option was completely handled by
797  * sending a reply about inconsistent lengths, or 1 on success.
798  *
799  * Note: return code = 1 doesn't mean that we've read exactly @pattern.
800  * It only means that there are no errors.
801  */
802 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
803                             Error **errp)
804 {
805     int ret;
806     char *query;
807     size_t len = strlen(pattern);
808 
809     assert(len);
810 
811     query = g_malloc(len);
812     ret = nbd_opt_read(client, query, len, errp);
813     if (ret <= 0) {
814         g_free(query);
815         return ret;
816     }
817 
818     if (strncmp(query, pattern, len) == 0) {
819         trace_nbd_negotiate_meta_query_parse(pattern);
820         *match = true;
821     } else {
822         trace_nbd_negotiate_meta_query_skip("pattern not matched");
823     }
824     g_free(query);
825 
826     return 1;
827 }
828 
829 /*
830  * Read @len bytes, and set @match to true if they match @pattern, or if @len
831  * is 0 and the client is performing _LIST_. @match is never set to false.
832  *
833  * Return -errno on I/O error, 0 if option was completely handled by
834  * sending a reply about inconsistent lengths, or 1 on success.
835  *
836  * Note: return code = 1 doesn't mean that we've read exactly @pattern.
837  * It only means that there are no errors.
838  */
839 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
840                                      uint32_t len, bool *match, Error **errp)
841 {
842     if (len == 0) {
843         if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
844             *match = true;
845         }
846         trace_nbd_negotiate_meta_query_parse("empty");
847         return 1;
848     }
849 
850     if (len != strlen(pattern)) {
851         trace_nbd_negotiate_meta_query_skip("different lengths");
852         return nbd_opt_skip(client, len, errp);
853     }
854 
855     return nbd_meta_pattern(client, pattern, match, errp);
856 }
857 
858 /* nbd_meta_base_query
859  *
860  * Handle queries to 'base' namespace. For now, only the base:allocation
861  * context is available.  'len' is the amount of text remaining to be read from
862  * the current name, after the 'base:' portion has been stripped.
863  *
864  * Return -errno on I/O error, 0 if option was completely handled by
865  * sending a reply about inconsistent lengths, or 1 on success.
866  */
867 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
868                                uint32_t len, Error **errp)
869 {
870     return nbd_meta_empty_or_pattern(client, "allocation", len,
871                                      &meta->base_allocation, errp);
872 }
873 
874 /* nbd_meta_bitmap_query
875  *
876  * Handle query to 'qemu:' namespace.
877  * @len is the amount of text remaining to be read from the current name, after
878  * the 'qemu:' portion has been stripped.
879  *
880  * Return -errno on I/O error, 0 if option was completely handled by
881  * sending a reply about inconsistent lengths, or 1 on success. */
882 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
883                                uint32_t len, Error **errp)
884 {
885     bool dirty_bitmap = false;
886     size_t dirty_bitmap_len = strlen("dirty-bitmap:");
887     int ret;
888 
889     if (!meta->exp->export_bitmap) {
890         trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
891         return nbd_opt_skip(client, len, errp);
892     }
893 
894     if (len == 0) {
895         if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
896             meta->bitmap = true;
897         }
898         trace_nbd_negotiate_meta_query_parse("empty");
899         return 1;
900     }
901 
902     if (len < dirty_bitmap_len) {
903         trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
904         return nbd_opt_skip(client, len, errp);
905     }
906 
907     len -= dirty_bitmap_len;
908     ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
909     if (ret <= 0) {
910         return ret;
911     }
912     if (!dirty_bitmap) {
913         trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
914         return nbd_opt_skip(client, len, errp);
915     }
916 
917     trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
918 
919     return nbd_meta_empty_or_pattern(
920             client, meta->exp->export_bitmap_context +
921             strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
922 }
923 
924 /* nbd_negotiate_meta_query
925  *
926  * Parse namespace name and call corresponding function to parse body of the
927  * query.
928  *
929  * The only supported namespaces are 'base' and 'qemu'.
930  *
931  * The function aims not wasting time and memory to read long unknown namespace
932  * names.
933  *
934  * Return -errno on I/O error, 0 if option was completely handled by
935  * sending a reply about inconsistent lengths, or 1 on success. */
936 static int nbd_negotiate_meta_query(NBDClient *client,
937                                     NBDExportMetaContexts *meta, Error **errp)
938 {
939     /*
940      * Both 'qemu' and 'base' namespaces have length = 5 including a
941      * colon. If another length namespace is later introduced, this
942      * should certainly be refactored.
943      */
944     int ret;
945     size_t ns_len = 5;
946     char ns[5];
947     uint32_t len;
948 
949     ret = nbd_opt_read(client, &len, sizeof(len), errp);
950     if (ret <= 0) {
951         return ret;
952     }
953     len = cpu_to_be32(len);
954 
955     if (len > NBD_MAX_STRING_SIZE) {
956         trace_nbd_negotiate_meta_query_skip("length too long");
957         return nbd_opt_skip(client, len, errp);
958     }
959     if (len < ns_len) {
960         trace_nbd_negotiate_meta_query_skip("length too short");
961         return nbd_opt_skip(client, len, errp);
962     }
963 
964     len -= ns_len;
965     ret = nbd_opt_read(client, ns, ns_len, errp);
966     if (ret <= 0) {
967         return ret;
968     }
969 
970     if (!strncmp(ns, "base:", ns_len)) {
971         trace_nbd_negotiate_meta_query_parse("base:");
972         return nbd_meta_base_query(client, meta, len, errp);
973     } else if (!strncmp(ns, "qemu:", ns_len)) {
974         trace_nbd_negotiate_meta_query_parse("qemu:");
975         return nbd_meta_qemu_query(client, meta, len, errp);
976     }
977 
978     trace_nbd_negotiate_meta_query_skip("unknown namespace");
979     return nbd_opt_skip(client, len, errp);
980 }
981 
982 /* nbd_negotiate_meta_queries
983  * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
984  *
985  * Return -errno on I/O error, or 0 if option was completely handled. */
986 static int nbd_negotiate_meta_queries(NBDClient *client,
987                                       NBDExportMetaContexts *meta, Error **errp)
988 {
989     int ret;
990     g_autofree char *export_name = NULL;
991     NBDExportMetaContexts local_meta;
992     uint32_t nb_queries;
993     int i;
994 
995     if (!client->structured_reply) {
996         return nbd_opt_invalid(client, errp,
997                                "request option '%s' when structured reply "
998                                "is not negotiated",
999                                nbd_opt_lookup(client->opt));
1000     }
1001 
1002     if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1003         /* Only change the caller's meta on SET. */
1004         meta = &local_meta;
1005     }
1006 
1007     memset(meta, 0, sizeof(*meta));
1008 
1009     ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1010     if (ret <= 0) {
1011         return ret;
1012     }
1013 
1014     meta->exp = nbd_export_find(export_name);
1015     if (meta->exp == NULL) {
1016         g_autofree char *sane_name = nbd_sanitize_name(export_name);
1017 
1018         return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1019                             "export '%s' not present", sane_name);
1020     }
1021 
1022     ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
1023     if (ret <= 0) {
1024         return ret;
1025     }
1026     nb_queries = cpu_to_be32(nb_queries);
1027     trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1028                                      export_name, nb_queries);
1029 
1030     if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1031         /* enable all known contexts */
1032         meta->base_allocation = true;
1033         meta->bitmap = !!meta->exp->export_bitmap;
1034     } else {
1035         for (i = 0; i < nb_queries; ++i) {
1036             ret = nbd_negotiate_meta_query(client, meta, errp);
1037             if (ret <= 0) {
1038                 return ret;
1039             }
1040         }
1041     }
1042 
1043     if (meta->base_allocation) {
1044         ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1045                                               NBD_META_ID_BASE_ALLOCATION,
1046                                               errp);
1047         if (ret < 0) {
1048             return ret;
1049         }
1050     }
1051 
1052     if (meta->bitmap) {
1053         ret = nbd_negotiate_send_meta_context(client,
1054                                               meta->exp->export_bitmap_context,
1055                                               NBD_META_ID_DIRTY_BITMAP,
1056                                               errp);
1057         if (ret < 0) {
1058             return ret;
1059         }
1060     }
1061 
1062     ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1063     if (ret == 0) {
1064         meta->valid = true;
1065     }
1066 
1067     return ret;
1068 }
1069 
1070 /* nbd_negotiate_options
1071  * Process all NBD_OPT_* client option commands, during fixed newstyle
1072  * negotiation.
1073  * Return:
1074  * -errno  on error, errp is set
1075  * 0       on successful negotiation, errp is not set
1076  * 1       if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1077  *         errp is not set
1078  */
1079 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1080 {
1081     uint32_t flags;
1082     bool fixedNewstyle = false;
1083     bool no_zeroes = false;
1084 
1085     /* Client sends:
1086         [ 0 ..   3]   client flags
1087 
1088        Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1089         [ 0 ..   7]   NBD_OPTS_MAGIC
1090         [ 8 ..  11]   NBD option
1091         [12 ..  15]   Data length
1092         ...           Rest of request
1093 
1094         [ 0 ..   7]   NBD_OPTS_MAGIC
1095         [ 8 ..  11]   Second NBD option
1096         [12 ..  15]   Data length
1097         ...           Rest of request
1098     */
1099 
1100     if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1101         return -EIO;
1102     }
1103     trace_nbd_negotiate_options_flags(flags);
1104     if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1105         fixedNewstyle = true;
1106         flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1107     }
1108     if (flags & NBD_FLAG_C_NO_ZEROES) {
1109         no_zeroes = true;
1110         flags &= ~NBD_FLAG_C_NO_ZEROES;
1111     }
1112     if (flags != 0) {
1113         error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1114         return -EINVAL;
1115     }
1116 
1117     while (1) {
1118         int ret;
1119         uint32_t option, length;
1120         uint64_t magic;
1121 
1122         if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1123             return -EINVAL;
1124         }
1125         trace_nbd_negotiate_options_check_magic(magic);
1126         if (magic != NBD_OPTS_MAGIC) {
1127             error_setg(errp, "Bad magic received");
1128             return -EINVAL;
1129         }
1130 
1131         if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1132             return -EINVAL;
1133         }
1134         client->opt = option;
1135 
1136         if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1137             return -EINVAL;
1138         }
1139         assert(!client->optlen);
1140         client->optlen = length;
1141 
1142         if (length > NBD_MAX_BUFFER_SIZE) {
1143             error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1144                        length, NBD_MAX_BUFFER_SIZE);
1145             return -EINVAL;
1146         }
1147 
1148         trace_nbd_negotiate_options_check_option(option,
1149                                                  nbd_opt_lookup(option));
1150         if (client->tlscreds &&
1151             client->ioc == (QIOChannel *)client->sioc) {
1152             QIOChannel *tioc;
1153             if (!fixedNewstyle) {
1154                 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1155                 return -EINVAL;
1156             }
1157             switch (option) {
1158             case NBD_OPT_STARTTLS:
1159                 if (length) {
1160                     /* Unconditionally drop the connection if the client
1161                      * can't start a TLS negotiation correctly */
1162                     return nbd_reject_length(client, true, errp);
1163                 }
1164                 tioc = nbd_negotiate_handle_starttls(client, errp);
1165                 if (!tioc) {
1166                     return -EIO;
1167                 }
1168                 ret = 0;
1169                 object_unref(OBJECT(client->ioc));
1170                 client->ioc = QIO_CHANNEL(tioc);
1171                 break;
1172 
1173             case NBD_OPT_EXPORT_NAME:
1174                 /* No way to return an error to client, so drop connection */
1175                 error_setg(errp, "Option 0x%x not permitted before TLS",
1176                            option);
1177                 return -EINVAL;
1178 
1179             default:
1180                 /* Let the client keep trying, unless they asked to
1181                  * quit. Always try to give an error back to the
1182                  * client; but when replying to OPT_ABORT, be aware
1183                  * that the client may hang up before receiving the
1184                  * error, in which case we are fine ignoring the
1185                  * resulting EPIPE. */
1186                 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1187                                    option == NBD_OPT_ABORT ? NULL : errp,
1188                                    "Option 0x%" PRIx32
1189                                    " not permitted before TLS", option);
1190                 if (option == NBD_OPT_ABORT) {
1191                     return 1;
1192                 }
1193                 break;
1194             }
1195         } else if (fixedNewstyle) {
1196             switch (option) {
1197             case NBD_OPT_LIST:
1198                 if (length) {
1199                     ret = nbd_reject_length(client, false, errp);
1200                 } else {
1201                     ret = nbd_negotiate_handle_list(client, errp);
1202                 }
1203                 break;
1204 
1205             case NBD_OPT_ABORT:
1206                 /* NBD spec says we must try to reply before
1207                  * disconnecting, but that we must also tolerate
1208                  * guests that don't wait for our reply. */
1209                 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1210                 return 1;
1211 
1212             case NBD_OPT_EXPORT_NAME:
1213                 return nbd_negotiate_handle_export_name(client, no_zeroes,
1214                                                         errp);
1215 
1216             case NBD_OPT_INFO:
1217             case NBD_OPT_GO:
1218                 ret = nbd_negotiate_handle_info(client, errp);
1219                 if (ret == 1) {
1220                     assert(option == NBD_OPT_GO);
1221                     return 0;
1222                 }
1223                 break;
1224 
1225             case NBD_OPT_STARTTLS:
1226                 if (length) {
1227                     ret = nbd_reject_length(client, false, errp);
1228                 } else if (client->tlscreds) {
1229                     ret = nbd_negotiate_send_rep_err(client,
1230                                                      NBD_REP_ERR_INVALID, errp,
1231                                                      "TLS already enabled");
1232                 } else {
1233                     ret = nbd_negotiate_send_rep_err(client,
1234                                                      NBD_REP_ERR_POLICY, errp,
1235                                                      "TLS not configured");
1236                 }
1237                 break;
1238 
1239             case NBD_OPT_STRUCTURED_REPLY:
1240                 if (length) {
1241                     ret = nbd_reject_length(client, false, errp);
1242                 } else if (client->structured_reply) {
1243                     ret = nbd_negotiate_send_rep_err(
1244                         client, NBD_REP_ERR_INVALID, errp,
1245                         "structured reply already negotiated");
1246                 } else {
1247                     ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1248                     client->structured_reply = true;
1249                 }
1250                 break;
1251 
1252             case NBD_OPT_LIST_META_CONTEXT:
1253             case NBD_OPT_SET_META_CONTEXT:
1254                 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1255                                                  errp);
1256                 break;
1257 
1258             default:
1259                 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1260                                    "Unsupported option %" PRIu32 " (%s)",
1261                                    option, nbd_opt_lookup(option));
1262                 break;
1263             }
1264         } else {
1265             /*
1266              * If broken new-style we should drop the connection
1267              * for anything except NBD_OPT_EXPORT_NAME
1268              */
1269             switch (option) {
1270             case NBD_OPT_EXPORT_NAME:
1271                 return nbd_negotiate_handle_export_name(client, no_zeroes,
1272                                                         errp);
1273 
1274             default:
1275                 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1276                            option, nbd_opt_lookup(option));
1277                 return -EINVAL;
1278             }
1279         }
1280         if (ret < 0) {
1281             return ret;
1282         }
1283     }
1284 }
1285 
1286 /* nbd_negotiate
1287  * Return:
1288  * -errno  on error, errp is set
1289  * 0       on successful negotiation, errp is not set
1290  * 1       if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1291  *         errp is not set
1292  */
1293 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1294 {
1295     ERRP_GUARD();
1296     char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1297     int ret;
1298 
1299     /* Old style negotiation header, no room for options
1300         [ 0 ..   7]   passwd       ("NBDMAGIC")
1301         [ 8 ..  15]   magic        (NBD_CLIENT_MAGIC)
1302         [16 ..  23]   size
1303         [24 ..  27]   export flags (zero-extended)
1304         [28 .. 151]   reserved     (0)
1305 
1306        New style negotiation header, client can send options
1307         [ 0 ..   7]   passwd       ("NBDMAGIC")
1308         [ 8 ..  15]   magic        (NBD_OPTS_MAGIC)
1309         [16 ..  17]   server flags (0)
1310         ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1311      */
1312 
1313     qio_channel_set_blocking(client->ioc, false, NULL);
1314 
1315     trace_nbd_negotiate_begin();
1316     memcpy(buf, "NBDMAGIC", 8);
1317 
1318     stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1319     stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1320 
1321     if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1322         error_prepend(errp, "write failed: ");
1323         return -EINVAL;
1324     }
1325     ret = nbd_negotiate_options(client, errp);
1326     if (ret != 0) {
1327         if (ret < 0) {
1328             error_prepend(errp, "option negotiation failed: ");
1329         }
1330         return ret;
1331     }
1332 
1333     /* Attach the channel to the same AioContext as the export */
1334     if (client->exp && client->exp->common.ctx) {
1335         qio_channel_attach_aio_context(client->ioc, client->exp->common.ctx);
1336     }
1337 
1338     assert(!client->optlen);
1339     trace_nbd_negotiate_success();
1340 
1341     return 0;
1342 }
1343 
1344 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1345                                Error **errp)
1346 {
1347     uint8_t buf[NBD_REQUEST_SIZE];
1348     uint32_t magic;
1349     int ret;
1350 
1351     ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1352     if (ret < 0) {
1353         return ret;
1354     }
1355 
1356     /* Request
1357        [ 0 ..  3]   magic   (NBD_REQUEST_MAGIC)
1358        [ 4 ..  5]   flags   (NBD_CMD_FLAG_FUA, ...)
1359        [ 6 ..  7]   type    (NBD_CMD_READ, ...)
1360        [ 8 .. 15]   handle
1361        [16 .. 23]   from
1362        [24 .. 27]   len
1363      */
1364 
1365     magic = ldl_be_p(buf);
1366     request->flags  = lduw_be_p(buf + 4);
1367     request->type   = lduw_be_p(buf + 6);
1368     request->handle = ldq_be_p(buf + 8);
1369     request->from   = ldq_be_p(buf + 16);
1370     request->len    = ldl_be_p(buf + 24);
1371 
1372     trace_nbd_receive_request(magic, request->flags, request->type,
1373                               request->from, request->len);
1374 
1375     if (magic != NBD_REQUEST_MAGIC) {
1376         error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1377         return -EINVAL;
1378     }
1379     return 0;
1380 }
1381 
1382 #define MAX_NBD_REQUESTS 16
1383 
1384 void nbd_client_get(NBDClient *client)
1385 {
1386     client->refcount++;
1387 }
1388 
1389 void nbd_client_put(NBDClient *client)
1390 {
1391     if (--client->refcount == 0) {
1392         /* The last reference should be dropped by client->close,
1393          * which is called by client_close.
1394          */
1395         assert(client->closing);
1396 
1397         qio_channel_detach_aio_context(client->ioc);
1398         object_unref(OBJECT(client->sioc));
1399         object_unref(OBJECT(client->ioc));
1400         if (client->tlscreds) {
1401             object_unref(OBJECT(client->tlscreds));
1402         }
1403         g_free(client->tlsauthz);
1404         if (client->exp) {
1405             QTAILQ_REMOVE(&client->exp->clients, client, next);
1406             blk_exp_unref(&client->exp->common);
1407         }
1408         g_free(client);
1409     }
1410 }
1411 
1412 static void client_close(NBDClient *client, bool negotiated)
1413 {
1414     if (client->closing) {
1415         return;
1416     }
1417 
1418     client->closing = true;
1419 
1420     /* Force requests to finish.  They will drop their own references,
1421      * then we'll close the socket and free the NBDClient.
1422      */
1423     qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1424                          NULL);
1425 
1426     /* Also tell the client, so that they release their reference.  */
1427     if (client->close_fn) {
1428         client->close_fn(client, negotiated);
1429     }
1430 }
1431 
1432 static NBDRequestData *nbd_request_get(NBDClient *client)
1433 {
1434     NBDRequestData *req;
1435 
1436     assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1437     client->nb_requests++;
1438 
1439     req = g_new0(NBDRequestData, 1);
1440     nbd_client_get(client);
1441     req->client = client;
1442     return req;
1443 }
1444 
1445 static void nbd_request_put(NBDRequestData *req)
1446 {
1447     NBDClient *client = req->client;
1448 
1449     if (req->data) {
1450         qemu_vfree(req->data);
1451     }
1452     g_free(req);
1453 
1454     client->nb_requests--;
1455     nbd_client_receive_next_request(client);
1456 
1457     nbd_client_put(client);
1458 }
1459 
1460 static void blk_aio_attached(AioContext *ctx, void *opaque)
1461 {
1462     NBDExport *exp = opaque;
1463     NBDClient *client;
1464 
1465     trace_nbd_blk_aio_attached(exp->name, ctx);
1466 
1467     exp->common.ctx = ctx;
1468 
1469     QTAILQ_FOREACH(client, &exp->clients, next) {
1470         qio_channel_attach_aio_context(client->ioc, ctx);
1471         if (client->recv_coroutine) {
1472             aio_co_schedule(ctx, client->recv_coroutine);
1473         }
1474         if (client->send_coroutine) {
1475             aio_co_schedule(ctx, client->send_coroutine);
1476         }
1477     }
1478 }
1479 
1480 static void blk_aio_detach(void *opaque)
1481 {
1482     NBDExport *exp = opaque;
1483     NBDClient *client;
1484 
1485     trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
1486 
1487     QTAILQ_FOREACH(client, &exp->clients, next) {
1488         qio_channel_detach_aio_context(client->ioc);
1489     }
1490 
1491     exp->common.ctx = NULL;
1492 }
1493 
1494 static void nbd_eject_notifier(Notifier *n, void *data)
1495 {
1496     NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1497     AioContext *aio_context;
1498 
1499     aio_context = exp->common.ctx;
1500     aio_context_acquire(aio_context);
1501     nbd_export_close(exp);
1502     aio_context_release(aio_context);
1503 }
1504 
1505 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1506 {
1507     NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1508     assert(exp->drv == &blk_exp_nbd);
1509     assert(nbd_exp->eject_notifier_blk == NULL);
1510 
1511     blk_ref(blk);
1512     nbd_exp->eject_notifier_blk = blk;
1513     nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1514     blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1515 }
1516 
1517 NBDExport *nbd_export_new(BlockDriverState *bs,
1518                           const char *name, const char *desc,
1519                           const char *bitmap, bool readonly, bool shared,
1520                           bool writethrough, Error **errp)
1521 {
1522     AioContext *ctx;
1523     BlockBackend *blk;
1524     NBDExport *exp;
1525     int64_t size;
1526     uint64_t perm;
1527     int ret;
1528 
1529     size = bdrv_getlength(bs);
1530     if (size < 0) {
1531         error_setg_errno(errp, -size,
1532                          "Failed to determine the NBD export's length");
1533         return NULL;
1534     }
1535 
1536     ctx = bdrv_get_aio_context(bs);
1537 
1538     exp = g_new0(NBDExport, 1);
1539     exp->common = (BlockExport) {
1540         .drv        = &blk_exp_nbd,
1541         .refcount   = 1,
1542         .ctx        = ctx,
1543     };
1544 
1545     /*
1546      * NBD exports are used for non-shared storage migration.  Make sure
1547      * that BDRV_O_INACTIVE is cleared and the image is ready for write
1548      * access since the export could be available before migration handover.
1549      * ctx was acquired in the caller.
1550      */
1551     assert(name && strlen(name) <= NBD_MAX_STRING_SIZE);
1552 
1553     bdrv_invalidate_cache(bs, NULL);
1554 
1555     /* Don't allow resize while the NBD server is running, otherwise we don't
1556      * care what happens with the node. */
1557     perm = BLK_PERM_CONSISTENT_READ;
1558     if (!readonly) {
1559         perm |= BLK_PERM_WRITE;
1560     }
1561     blk = blk_new(ctx, perm,
1562                   BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1563                   BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1564     ret = blk_insert_bs(blk, bs, errp);
1565     if (ret < 0) {
1566         goto fail;
1567     }
1568     blk_set_enable_write_cache(blk, !writethrough);
1569     blk_set_allow_aio_context_change(blk, true);
1570 
1571     QTAILQ_INIT(&exp->clients);
1572     exp->blk = blk;
1573     exp->name = g_strdup(name);
1574     assert(!desc || strlen(desc) <= NBD_MAX_STRING_SIZE);
1575     exp->description = g_strdup(desc);
1576     exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1577                      NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1578     if (readonly) {
1579         exp->nbdflags |= NBD_FLAG_READ_ONLY;
1580         if (shared) {
1581             exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1582         }
1583     } else {
1584         exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1585                           NBD_FLAG_SEND_FAST_ZERO);
1586     }
1587     exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1588 
1589     if (bitmap) {
1590         BdrvDirtyBitmap *bm = NULL;
1591 
1592         while (bs) {
1593             bm = bdrv_find_dirty_bitmap(bs, bitmap);
1594             if (bm != NULL) {
1595                 break;
1596             }
1597 
1598             bs = bdrv_filter_or_cow_bs(bs);
1599         }
1600 
1601         if (bm == NULL) {
1602             error_setg(errp, "Bitmap '%s' is not found", bitmap);
1603             goto fail;
1604         }
1605 
1606         if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1607             goto fail;
1608         }
1609 
1610         if (readonly && bdrv_is_writable(bs) &&
1611             bdrv_dirty_bitmap_enabled(bm)) {
1612             error_setg(errp,
1613                        "Enabled bitmap '%s' incompatible with readonly export",
1614                        bitmap);
1615             goto fail;
1616         }
1617 
1618         bdrv_dirty_bitmap_set_busy(bm, true);
1619         exp->export_bitmap = bm;
1620         assert(strlen(bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
1621         exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1622                                                      bitmap);
1623         assert(strlen(exp->export_bitmap_context) < NBD_MAX_STRING_SIZE);
1624     }
1625 
1626     blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1627 
1628     blk_exp_ref(&exp->common);
1629     QTAILQ_INSERT_TAIL(&exports, exp, next);
1630 
1631     return exp;
1632 
1633 fail:
1634     blk_unref(blk);
1635     g_free(exp->name);
1636     g_free(exp->description);
1637     g_free(exp);
1638     return NULL;
1639 }
1640 
1641 NBDExport *nbd_export_find(const char *name)
1642 {
1643     NBDExport *exp;
1644     QTAILQ_FOREACH(exp, &exports, next) {
1645         if (strcmp(name, exp->name) == 0) {
1646             return exp;
1647         }
1648     }
1649 
1650     return NULL;
1651 }
1652 
1653 AioContext *
1654 nbd_export_aio_context(NBDExport *exp)
1655 {
1656     return exp->common.ctx;
1657 }
1658 
1659 void nbd_export_close(NBDExport *exp)
1660 {
1661     NBDClient *client, *next;
1662 
1663     blk_exp_ref(&exp->common);
1664     /*
1665      * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1666      * close mode that stops advertising the export to new clients but
1667      * still permits existing clients to run to completion? Because of
1668      * that possibility, nbd_export_close() can be called more than
1669      * once on an export.
1670      */
1671     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1672         client_close(client, true);
1673     }
1674     if (exp->name) {
1675         blk_exp_unref(&exp->common);
1676         g_free(exp->name);
1677         exp->name = NULL;
1678         QTAILQ_REMOVE(&exports, exp, next);
1679         QTAILQ_INSERT_TAIL(&closed_exports, exp, next);
1680     }
1681     blk_exp_unref(&exp->common);
1682 }
1683 
1684 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1685 {
1686     ERRP_GUARD();
1687     if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1688         nbd_export_close(exp);
1689         return;
1690     }
1691 
1692     assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1693 
1694     error_setg(errp, "export '%s' still in use", exp->name);
1695     error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1696 }
1697 
1698 static void nbd_export_delete(BlockExport *blk_exp)
1699 {
1700     NBDExport *exp = container_of(blk_exp, NBDExport, common);
1701 
1702     assert(exp->name == NULL);
1703     assert(QTAILQ_EMPTY(&exp->clients));
1704 
1705     g_free(exp->description);
1706     exp->description = NULL;
1707 
1708     if (exp->blk) {
1709         if (exp->eject_notifier_blk) {
1710             notifier_remove(&exp->eject_notifier);
1711             blk_unref(exp->eject_notifier_blk);
1712         }
1713         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1714                                         blk_aio_detach, exp);
1715         blk_unref(exp->blk);
1716         exp->blk = NULL;
1717     }
1718 
1719     if (exp->export_bitmap) {
1720         bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1721         g_free(exp->export_bitmap_context);
1722     }
1723 
1724     QTAILQ_REMOVE(&closed_exports, exp, next);
1725     g_free(exp);
1726     aio_wait_kick();
1727 }
1728 
1729 const BlockExportDriver blk_exp_nbd = {
1730     .type               = BLOCK_EXPORT_TYPE_NBD,
1731     .create             = nbd_export_create,
1732     .delete             = nbd_export_delete,
1733 };
1734 
1735 void nbd_export_close_all(void)
1736 {
1737     NBDExport *exp, *next;
1738     AioContext *aio_context;
1739 
1740     QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1741         aio_context = exp->common.ctx;
1742         aio_context_acquire(aio_context);
1743         nbd_export_close(exp);
1744         aio_context_release(aio_context);
1745     }
1746 
1747     AIO_WAIT_WHILE(NULL, !(QTAILQ_EMPTY(&exports) &&
1748                            QTAILQ_EMPTY(&closed_exports)));
1749 }
1750 
1751 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1752                                         unsigned niov, Error **errp)
1753 {
1754     int ret;
1755 
1756     g_assert(qemu_in_coroutine());
1757     qemu_co_mutex_lock(&client->send_lock);
1758     client->send_coroutine = qemu_coroutine_self();
1759 
1760     ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1761 
1762     client->send_coroutine = NULL;
1763     qemu_co_mutex_unlock(&client->send_lock);
1764 
1765     return ret;
1766 }
1767 
1768 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1769                                        uint64_t handle)
1770 {
1771     stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1772     stl_be_p(&reply->error, error);
1773     stq_be_p(&reply->handle, handle);
1774 }
1775 
1776 static int nbd_co_send_simple_reply(NBDClient *client,
1777                                     uint64_t handle,
1778                                     uint32_t error,
1779                                     void *data,
1780                                     size_t len,
1781                                     Error **errp)
1782 {
1783     NBDSimpleReply reply;
1784     int nbd_err = system_errno_to_nbd_errno(error);
1785     struct iovec iov[] = {
1786         {.iov_base = &reply, .iov_len = sizeof(reply)},
1787         {.iov_base = data, .iov_len = len}
1788     };
1789 
1790     trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1791                                    len);
1792     set_be_simple_reply(&reply, nbd_err, handle);
1793 
1794     return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1795 }
1796 
1797 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1798                                 uint16_t type, uint64_t handle, uint32_t length)
1799 {
1800     stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1801     stw_be_p(&chunk->flags, flags);
1802     stw_be_p(&chunk->type, type);
1803     stq_be_p(&chunk->handle, handle);
1804     stl_be_p(&chunk->length, length);
1805 }
1806 
1807 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1808                                                     uint64_t handle,
1809                                                     Error **errp)
1810 {
1811     NBDStructuredReplyChunk chunk;
1812     struct iovec iov[] = {
1813         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1814     };
1815 
1816     trace_nbd_co_send_structured_done(handle);
1817     set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1818 
1819     return nbd_co_send_iov(client, iov, 1, errp);
1820 }
1821 
1822 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1823                                                     uint64_t handle,
1824                                                     uint64_t offset,
1825                                                     void *data,
1826                                                     size_t size,
1827                                                     bool final,
1828                                                     Error **errp)
1829 {
1830     NBDStructuredReadData chunk;
1831     struct iovec iov[] = {
1832         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1833         {.iov_base = data, .iov_len = size}
1834     };
1835 
1836     assert(size);
1837     trace_nbd_co_send_structured_read(handle, offset, data, size);
1838     set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1839                  NBD_REPLY_TYPE_OFFSET_DATA, handle,
1840                  sizeof(chunk) - sizeof(chunk.h) + size);
1841     stq_be_p(&chunk.offset, offset);
1842 
1843     return nbd_co_send_iov(client, iov, 2, errp);
1844 }
1845 
1846 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1847                                                      uint64_t handle,
1848                                                      uint32_t error,
1849                                                      const char *msg,
1850                                                      Error **errp)
1851 {
1852     NBDStructuredError chunk;
1853     int nbd_err = system_errno_to_nbd_errno(error);
1854     struct iovec iov[] = {
1855         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1856         {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1857     };
1858 
1859     assert(nbd_err);
1860     trace_nbd_co_send_structured_error(handle, nbd_err,
1861                                        nbd_err_lookup(nbd_err), msg ? msg : "");
1862     set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1863                  sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1864     stl_be_p(&chunk.error, nbd_err);
1865     stw_be_p(&chunk.message_length, iov[1].iov_len);
1866 
1867     return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1868 }
1869 
1870 /* Do a sparse read and send the structured reply to the client.
1871  * Returns -errno if sending fails. bdrv_block_status_above() failure is
1872  * reported to the client, at which point this function succeeds.
1873  */
1874 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1875                                                 uint64_t handle,
1876                                                 uint64_t offset,
1877                                                 uint8_t *data,
1878                                                 size_t size,
1879                                                 Error **errp)
1880 {
1881     int ret = 0;
1882     NBDExport *exp = client->exp;
1883     size_t progress = 0;
1884 
1885     while (progress < size) {
1886         int64_t pnum;
1887         int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1888                                              offset + progress,
1889                                              size - progress, &pnum, NULL,
1890                                              NULL);
1891         bool final;
1892 
1893         if (status < 0) {
1894             char *msg = g_strdup_printf("unable to check for holes: %s",
1895                                         strerror(-status));
1896 
1897             ret = nbd_co_send_structured_error(client, handle, -status, msg,
1898                                                errp);
1899             g_free(msg);
1900             return ret;
1901         }
1902         assert(pnum && pnum <= size - progress);
1903         final = progress + pnum == size;
1904         if (status & BDRV_BLOCK_ZERO) {
1905             NBDStructuredReadHole chunk;
1906             struct iovec iov[] = {
1907                 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1908             };
1909 
1910             trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1911                                                    pnum);
1912             set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1913                          NBD_REPLY_TYPE_OFFSET_HOLE,
1914                          handle, sizeof(chunk) - sizeof(chunk.h));
1915             stq_be_p(&chunk.offset, offset + progress);
1916             stl_be_p(&chunk.length, pnum);
1917             ret = nbd_co_send_iov(client, iov, 1, errp);
1918         } else {
1919             ret = blk_pread(exp->blk, offset + progress, data + progress, pnum);
1920             if (ret < 0) {
1921                 error_setg_errno(errp, -ret, "reading from file failed");
1922                 break;
1923             }
1924             ret = nbd_co_send_structured_read(client, handle, offset + progress,
1925                                               data + progress, pnum, final,
1926                                               errp);
1927         }
1928 
1929         if (ret < 0) {
1930             break;
1931         }
1932         progress += pnum;
1933     }
1934     return ret;
1935 }
1936 
1937 typedef struct NBDExtentArray {
1938     NBDExtent *extents;
1939     unsigned int nb_alloc;
1940     unsigned int count;
1941     uint64_t total_length;
1942     bool can_add;
1943     bool converted_to_be;
1944 } NBDExtentArray;
1945 
1946 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
1947 {
1948     NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
1949 
1950     ea->nb_alloc = nb_alloc;
1951     ea->extents = g_new(NBDExtent, nb_alloc);
1952     ea->can_add = true;
1953 
1954     return ea;
1955 }
1956 
1957 static void nbd_extent_array_free(NBDExtentArray *ea)
1958 {
1959     g_free(ea->extents);
1960     g_free(ea);
1961 }
1962 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
1963 
1964 /* Further modifications of the array after conversion are abandoned */
1965 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
1966 {
1967     int i;
1968 
1969     assert(!ea->converted_to_be);
1970     ea->can_add = false;
1971     ea->converted_to_be = true;
1972 
1973     for (i = 0; i < ea->count; i++) {
1974         ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
1975         ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
1976     }
1977 }
1978 
1979 /*
1980  * Add extent to NBDExtentArray. If extent can't be added (no available space),
1981  * return -1.
1982  * For safety, when returning -1 for the first time, .can_add is set to false,
1983  * further call to nbd_extent_array_add() will crash.
1984  * (to avoid the situation, when after failing to add an extent (returned -1),
1985  * user miss this failure and add another extent, which is successfully added
1986  * (array is full, but new extent may be squashed into the last one), then we
1987  * have invalid array with skipped extent)
1988  */
1989 static int nbd_extent_array_add(NBDExtentArray *ea,
1990                                 uint32_t length, uint32_t flags)
1991 {
1992     assert(ea->can_add);
1993 
1994     if (!length) {
1995         return 0;
1996     }
1997 
1998     /* Extend previous extent if flags are the same */
1999     if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
2000         uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
2001 
2002         if (sum <= UINT32_MAX) {
2003             ea->extents[ea->count - 1].length = sum;
2004             ea->total_length += length;
2005             return 0;
2006         }
2007     }
2008 
2009     if (ea->count >= ea->nb_alloc) {
2010         ea->can_add = false;
2011         return -1;
2012     }
2013 
2014     ea->total_length += length;
2015     ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
2016     ea->count++;
2017 
2018     return 0;
2019 }
2020 
2021 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
2022                                   uint64_t bytes, NBDExtentArray *ea)
2023 {
2024     while (bytes) {
2025         uint32_t flags;
2026         int64_t num;
2027         int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
2028                                           NULL, NULL);
2029 
2030         if (ret < 0) {
2031             return ret;
2032         }
2033 
2034         flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
2035                 (ret & BDRV_BLOCK_ZERO      ? NBD_STATE_ZERO : 0);
2036 
2037         if (nbd_extent_array_add(ea, num, flags) < 0) {
2038             return 0;
2039         }
2040 
2041         offset += num;
2042         bytes -= num;
2043     }
2044 
2045     return 0;
2046 }
2047 
2048 /*
2049  * nbd_co_send_extents
2050  *
2051  * @ea is converted to BE by the function
2052  * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2053  */
2054 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
2055                                NBDExtentArray *ea,
2056                                bool last, uint32_t context_id, Error **errp)
2057 {
2058     NBDStructuredMeta chunk;
2059     struct iovec iov[] = {
2060         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2061         {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
2062     };
2063 
2064     nbd_extent_array_convert_to_be(ea);
2065 
2066     trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2067                               last);
2068     set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2069                  NBD_REPLY_TYPE_BLOCK_STATUS,
2070                  handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2071     stl_be_p(&chunk.context_id, context_id);
2072 
2073     return nbd_co_send_iov(client, iov, 2, errp);
2074 }
2075 
2076 /* Get block status from the exported device and send it to the client */
2077 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2078                                     BlockDriverState *bs, uint64_t offset,
2079                                     uint32_t length, bool dont_fragment,
2080                                     bool last, uint32_t context_id,
2081                                     Error **errp)
2082 {
2083     int ret;
2084     unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2085     g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2086 
2087     ret = blockstatus_to_extents(bs, offset, length, ea);
2088     if (ret < 0) {
2089         return nbd_co_send_structured_error(
2090                 client, handle, -ret, "can't get block status", errp);
2091     }
2092 
2093     return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2094 }
2095 
2096 /* Populate @ea from a dirty bitmap. */
2097 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2098                               uint64_t offset, uint64_t length,
2099                               NBDExtentArray *es)
2100 {
2101     int64_t start, dirty_start, dirty_count;
2102     int64_t end = offset + length;
2103     bool full = false;
2104 
2105     bdrv_dirty_bitmap_lock(bitmap);
2106 
2107     for (start = offset;
2108          bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2109                                            &dirty_start, &dirty_count);
2110          start = dirty_start + dirty_count)
2111     {
2112         if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2113             (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2114         {
2115             full = true;
2116             break;
2117         }
2118     }
2119 
2120     if (!full) {
2121         /* last non dirty extent */
2122         nbd_extent_array_add(es, end - start, 0);
2123     }
2124 
2125     bdrv_dirty_bitmap_unlock(bitmap);
2126 }
2127 
2128 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2129                               BdrvDirtyBitmap *bitmap, uint64_t offset,
2130                               uint32_t length, bool dont_fragment, bool last,
2131                               uint32_t context_id, Error **errp)
2132 {
2133     unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2134     g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2135 
2136     bitmap_to_extents(bitmap, offset, length, ea);
2137 
2138     return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2139 }
2140 
2141 /* nbd_co_receive_request
2142  * Collect a client request. Return 0 if request looks valid, -EIO to drop
2143  * connection right away, and any other negative value to report an error to
2144  * the client (although the caller may still need to disconnect after reporting
2145  * the error).
2146  */
2147 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2148                                   Error **errp)
2149 {
2150     NBDClient *client = req->client;
2151     int valid_flags;
2152 
2153     g_assert(qemu_in_coroutine());
2154     assert(client->recv_coroutine == qemu_coroutine_self());
2155     if (nbd_receive_request(client->ioc, request, errp) < 0) {
2156         return -EIO;
2157     }
2158 
2159     trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2160                                              nbd_cmd_lookup(request->type));
2161 
2162     if (request->type != NBD_CMD_WRITE) {
2163         /* No payload, we are ready to read the next request.  */
2164         req->complete = true;
2165     }
2166 
2167     if (request->type == NBD_CMD_DISC) {
2168         /* Special case: we're going to disconnect without a reply,
2169          * whether or not flags, from, or len are bogus */
2170         return -EIO;
2171     }
2172 
2173     if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2174         request->type == NBD_CMD_CACHE)
2175     {
2176         if (request->len > NBD_MAX_BUFFER_SIZE) {
2177             error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2178                        request->len, NBD_MAX_BUFFER_SIZE);
2179             return -EINVAL;
2180         }
2181 
2182         if (request->type != NBD_CMD_CACHE) {
2183             req->data = blk_try_blockalign(client->exp->blk, request->len);
2184             if (req->data == NULL) {
2185                 error_setg(errp, "No memory");
2186                 return -ENOMEM;
2187             }
2188         }
2189     }
2190 
2191     if (request->type == NBD_CMD_WRITE) {
2192         if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2193                      errp) < 0)
2194         {
2195             return -EIO;
2196         }
2197         req->complete = true;
2198 
2199         trace_nbd_co_receive_request_payload_received(request->handle,
2200                                                       request->len);
2201     }
2202 
2203     /* Sanity checks. */
2204     if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2205         (request->type == NBD_CMD_WRITE ||
2206          request->type == NBD_CMD_WRITE_ZEROES ||
2207          request->type == NBD_CMD_TRIM)) {
2208         error_setg(errp, "Export is read-only");
2209         return -EROFS;
2210     }
2211     if (request->from > client->exp->size ||
2212         request->len > client->exp->size - request->from) {
2213         error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2214                    ", Size: %" PRIu64, request->from, request->len,
2215                    client->exp->size);
2216         return (request->type == NBD_CMD_WRITE ||
2217                 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2218     }
2219     if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2220                                                 client->check_align)) {
2221         /*
2222          * The block layer gracefully handles unaligned requests, but
2223          * it's still worth tracing client non-compliance
2224          */
2225         trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2226                                               request->from,
2227                                               request->len,
2228                                               client->check_align);
2229     }
2230     valid_flags = NBD_CMD_FLAG_FUA;
2231     if (request->type == NBD_CMD_READ && client->structured_reply) {
2232         valid_flags |= NBD_CMD_FLAG_DF;
2233     } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2234         valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2235     } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2236         valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2237     }
2238     if (request->flags & ~valid_flags) {
2239         error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2240                    nbd_cmd_lookup(request->type), request->flags);
2241         return -EINVAL;
2242     }
2243 
2244     return 0;
2245 }
2246 
2247 /* Send simple reply without a payload, or a structured error
2248  * @error_msg is ignored if @ret >= 0
2249  * Returns 0 if connection is still live, -errno on failure to talk to client
2250  */
2251 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2252                                                uint64_t handle,
2253                                                int ret,
2254                                                const char *error_msg,
2255                                                Error **errp)
2256 {
2257     if (client->structured_reply && ret < 0) {
2258         return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2259                                             errp);
2260     } else {
2261         return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2262                                         NULL, 0, errp);
2263     }
2264 }
2265 
2266 /* Handle NBD_CMD_READ request.
2267  * Return -errno if sending fails. Other errors are reported directly to the
2268  * client as an error reply. */
2269 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2270                                         uint8_t *data, Error **errp)
2271 {
2272     int ret;
2273     NBDExport *exp = client->exp;
2274 
2275     assert(request->type == NBD_CMD_READ);
2276 
2277     /* XXX: NBD Protocol only documents use of FUA with WRITE */
2278     if (request->flags & NBD_CMD_FLAG_FUA) {
2279         ret = blk_co_flush(exp->blk);
2280         if (ret < 0) {
2281             return nbd_send_generic_reply(client, request->handle, ret,
2282                                           "flush failed", errp);
2283         }
2284     }
2285 
2286     if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2287         request->len)
2288     {
2289         return nbd_co_send_sparse_read(client, request->handle, request->from,
2290                                        data, request->len, errp);
2291     }
2292 
2293     ret = blk_pread(exp->blk, request->from, data, request->len);
2294     if (ret < 0) {
2295         return nbd_send_generic_reply(client, request->handle, ret,
2296                                       "reading from file failed", errp);
2297     }
2298 
2299     if (client->structured_reply) {
2300         if (request->len) {
2301             return nbd_co_send_structured_read(client, request->handle,
2302                                                request->from, data,
2303                                                request->len, true, errp);
2304         } else {
2305             return nbd_co_send_structured_done(client, request->handle, errp);
2306         }
2307     } else {
2308         return nbd_co_send_simple_reply(client, request->handle, 0,
2309                                         data, request->len, errp);
2310     }
2311 }
2312 
2313 /*
2314  * nbd_do_cmd_cache
2315  *
2316  * Handle NBD_CMD_CACHE request.
2317  * Return -errno if sending fails. Other errors are reported directly to the
2318  * client as an error reply.
2319  */
2320 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2321                                          Error **errp)
2322 {
2323     int ret;
2324     NBDExport *exp = client->exp;
2325 
2326     assert(request->type == NBD_CMD_CACHE);
2327 
2328     ret = blk_co_preadv(exp->blk, request->from, request->len,
2329                         NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2330 
2331     return nbd_send_generic_reply(client, request->handle, ret,
2332                                   "caching data failed", errp);
2333 }
2334 
2335 /* Handle NBD request.
2336  * Return -errno if sending fails. Other errors are reported directly to the
2337  * client as an error reply. */
2338 static coroutine_fn int nbd_handle_request(NBDClient *client,
2339                                            NBDRequest *request,
2340                                            uint8_t *data, Error **errp)
2341 {
2342     int ret;
2343     int flags;
2344     NBDExport *exp = client->exp;
2345     char *msg;
2346 
2347     switch (request->type) {
2348     case NBD_CMD_CACHE:
2349         return nbd_do_cmd_cache(client, request, errp);
2350 
2351     case NBD_CMD_READ:
2352         return nbd_do_cmd_read(client, request, data, errp);
2353 
2354     case NBD_CMD_WRITE:
2355         flags = 0;
2356         if (request->flags & NBD_CMD_FLAG_FUA) {
2357             flags |= BDRV_REQ_FUA;
2358         }
2359         ret = blk_pwrite(exp->blk, request->from, data, request->len, flags);
2360         return nbd_send_generic_reply(client, request->handle, ret,
2361                                       "writing to file failed", errp);
2362 
2363     case NBD_CMD_WRITE_ZEROES:
2364         flags = 0;
2365         if (request->flags & NBD_CMD_FLAG_FUA) {
2366             flags |= BDRV_REQ_FUA;
2367         }
2368         if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2369             flags |= BDRV_REQ_MAY_UNMAP;
2370         }
2371         if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2372             flags |= BDRV_REQ_NO_FALLBACK;
2373         }
2374         ret = 0;
2375         /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2376         while (ret >= 0 && request->len) {
2377             int align = client->check_align ?: 1;
2378             int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2379                                                         align));
2380             ret = blk_pwrite_zeroes(exp->blk, request->from, len, flags);
2381             request->len -= len;
2382             request->from += len;
2383         }
2384         return nbd_send_generic_reply(client, request->handle, ret,
2385                                       "writing to file failed", errp);
2386 
2387     case NBD_CMD_DISC:
2388         /* unreachable, thanks to special case in nbd_co_receive_request() */
2389         abort();
2390 
2391     case NBD_CMD_FLUSH:
2392         ret = blk_co_flush(exp->blk);
2393         return nbd_send_generic_reply(client, request->handle, ret,
2394                                       "flush failed", errp);
2395 
2396     case NBD_CMD_TRIM:
2397         ret = 0;
2398         /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2399         while (ret >= 0 && request->len) {
2400             int align = client->check_align ?: 1;
2401             int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2402                                                         align));
2403             ret = blk_co_pdiscard(exp->blk, request->from, len);
2404             request->len -= len;
2405             request->from += len;
2406         }
2407         if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2408             ret = blk_co_flush(exp->blk);
2409         }
2410         return nbd_send_generic_reply(client, request->handle, ret,
2411                                       "discard failed", errp);
2412 
2413     case NBD_CMD_BLOCK_STATUS:
2414         if (!request->len) {
2415             return nbd_send_generic_reply(client, request->handle, -EINVAL,
2416                                           "need non-zero length", errp);
2417         }
2418         if (client->export_meta.valid &&
2419             (client->export_meta.base_allocation ||
2420              client->export_meta.bitmap))
2421         {
2422             bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2423 
2424             if (client->export_meta.base_allocation) {
2425                 ret = nbd_co_send_block_status(client, request->handle,
2426                                                blk_bs(exp->blk), request->from,
2427                                                request->len, dont_fragment,
2428                                                !client->export_meta.bitmap,
2429                                                NBD_META_ID_BASE_ALLOCATION,
2430                                                errp);
2431                 if (ret < 0) {
2432                     return ret;
2433                 }
2434             }
2435 
2436             if (client->export_meta.bitmap) {
2437                 ret = nbd_co_send_bitmap(client, request->handle,
2438                                          client->exp->export_bitmap,
2439                                          request->from, request->len,
2440                                          dont_fragment,
2441                                          true, NBD_META_ID_DIRTY_BITMAP, errp);
2442                 if (ret < 0) {
2443                     return ret;
2444                 }
2445             }
2446 
2447             return 0;
2448         } else {
2449             return nbd_send_generic_reply(client, request->handle, -EINVAL,
2450                                           "CMD_BLOCK_STATUS not negotiated",
2451                                           errp);
2452         }
2453 
2454     default:
2455         msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2456                               request->type);
2457         ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2458                                      errp);
2459         g_free(msg);
2460         return ret;
2461     }
2462 }
2463 
2464 /* Owns a reference to the NBDClient passed as opaque.  */
2465 static coroutine_fn void nbd_trip(void *opaque)
2466 {
2467     NBDClient *client = opaque;
2468     NBDRequestData *req;
2469     NBDRequest request = { 0 };    /* GCC thinks it can be used uninitialized */
2470     int ret;
2471     Error *local_err = NULL;
2472 
2473     trace_nbd_trip();
2474     if (client->closing) {
2475         nbd_client_put(client);
2476         return;
2477     }
2478 
2479     req = nbd_request_get(client);
2480     ret = nbd_co_receive_request(req, &request, &local_err);
2481     client->recv_coroutine = NULL;
2482 
2483     if (client->closing) {
2484         /*
2485          * The client may be closed when we are blocked in
2486          * nbd_co_receive_request()
2487          */
2488         goto done;
2489     }
2490 
2491     nbd_client_receive_next_request(client);
2492     if (ret == -EIO) {
2493         goto disconnect;
2494     }
2495 
2496     if (ret < 0) {
2497         /* It wans't -EIO, so, according to nbd_co_receive_request()
2498          * semantics, we should return the error to the client. */
2499         Error *export_err = local_err;
2500 
2501         local_err = NULL;
2502         ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2503                                      error_get_pretty(export_err), &local_err);
2504         error_free(export_err);
2505     } else {
2506         ret = nbd_handle_request(client, &request, req->data, &local_err);
2507     }
2508     if (ret < 0) {
2509         error_prepend(&local_err, "Failed to send reply: ");
2510         goto disconnect;
2511     }
2512 
2513     /* We must disconnect after NBD_CMD_WRITE if we did not
2514      * read the payload.
2515      */
2516     if (!req->complete) {
2517         error_setg(&local_err, "Request handling failed in intermediate state");
2518         goto disconnect;
2519     }
2520 
2521 done:
2522     nbd_request_put(req);
2523     nbd_client_put(client);
2524     return;
2525 
2526 disconnect:
2527     if (local_err) {
2528         error_reportf_err(local_err, "Disconnect client, due to: ");
2529     }
2530     nbd_request_put(req);
2531     client_close(client, true);
2532     nbd_client_put(client);
2533 }
2534 
2535 static void nbd_client_receive_next_request(NBDClient *client)
2536 {
2537     if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2538         nbd_client_get(client);
2539         client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2540         aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);
2541     }
2542 }
2543 
2544 static coroutine_fn void nbd_co_client_start(void *opaque)
2545 {
2546     NBDClient *client = opaque;
2547     Error *local_err = NULL;
2548 
2549     qemu_co_mutex_init(&client->send_lock);
2550 
2551     if (nbd_negotiate(client, &local_err)) {
2552         if (local_err) {
2553             error_report_err(local_err);
2554         }
2555         client_close(client, false);
2556         return;
2557     }
2558 
2559     nbd_client_receive_next_request(client);
2560 }
2561 
2562 /*
2563  * Create a new client listener using the given channel @sioc.
2564  * Begin servicing it in a coroutine.  When the connection closes, call
2565  * @close_fn with an indication of whether the client completed negotiation.
2566  */
2567 void nbd_client_new(QIOChannelSocket *sioc,
2568                     QCryptoTLSCreds *tlscreds,
2569                     const char *tlsauthz,
2570                     void (*close_fn)(NBDClient *, bool))
2571 {
2572     NBDClient *client;
2573     Coroutine *co;
2574 
2575     client = g_new0(NBDClient, 1);
2576     client->refcount = 1;
2577     client->tlscreds = tlscreds;
2578     if (tlscreds) {
2579         object_ref(OBJECT(client->tlscreds));
2580     }
2581     client->tlsauthz = g_strdup(tlsauthz);
2582     client->sioc = sioc;
2583     object_ref(OBJECT(client->sioc));
2584     client->ioc = QIO_CHANNEL(sioc);
2585     object_ref(OBJECT(client->ioc));
2586     client->close_fn = close_fn;
2587 
2588     co = qemu_coroutine_create(nbd_co_client_start, client);
2589     qemu_coroutine_enter(co);
2590 }
2591