xref: /openbmc/qemu/nbd/server.c (revision 8d8cb956)
1 /*
2  *  Copyright (C) 2016-2017 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 #include "qapi/error.h"
22 #include "trace.h"
23 #include "nbd-internal.h"
24 
25 static int system_errno_to_nbd_errno(int err)
26 {
27     switch (err) {
28     case 0:
29         return NBD_SUCCESS;
30     case EPERM:
31     case EROFS:
32         return NBD_EPERM;
33     case EIO:
34         return NBD_EIO;
35     case ENOMEM:
36         return NBD_ENOMEM;
37 #ifdef EDQUOT
38     case EDQUOT:
39 #endif
40     case EFBIG:
41     case ENOSPC:
42         return NBD_ENOSPC;
43     case EOVERFLOW:
44         return NBD_EOVERFLOW;
45     case ESHUTDOWN:
46         return NBD_ESHUTDOWN;
47     case EINVAL:
48     default:
49         return NBD_EINVAL;
50     }
51 }
52 
53 /* Definitions for opaque data types */
54 
55 typedef struct NBDRequestData NBDRequestData;
56 
57 struct NBDRequestData {
58     QSIMPLEQ_ENTRY(NBDRequestData) entry;
59     NBDClient *client;
60     uint8_t *data;
61     bool complete;
62 };
63 
64 struct NBDExport {
65     int refcount;
66     void (*close)(NBDExport *exp);
67 
68     BlockBackend *blk;
69     char *name;
70     char *description;
71     off_t dev_offset;
72     off_t size;
73     uint16_t nbdflags;
74     QTAILQ_HEAD(, NBDClient) clients;
75     QTAILQ_ENTRY(NBDExport) next;
76 
77     AioContext *ctx;
78 
79     BlockBackend *eject_notifier_blk;
80     Notifier eject_notifier;
81 };
82 
83 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
84 
85 struct NBDClient {
86     int refcount;
87     void (*close_fn)(NBDClient *client, bool negotiated);
88 
89     NBDExport *exp;
90     QCryptoTLSCreds *tlscreds;
91     char *tlsaclname;
92     QIOChannelSocket *sioc; /* The underlying data channel */
93     QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
94 
95     Coroutine *recv_coroutine;
96 
97     CoMutex send_lock;
98     Coroutine *send_coroutine;
99 
100     QTAILQ_ENTRY(NBDClient) next;
101     int nb_requests;
102     bool closing;
103 
104     bool structured_reply;
105 };
106 
107 /* That's all folks */
108 
109 static void nbd_client_receive_next_request(NBDClient *client);
110 
111 /* Basic flow for negotiation
112 
113    Server         Client
114    Negotiate
115 
116    or
117 
118    Server         Client
119    Negotiate #1
120                   Option
121    Negotiate #2
122 
123    ----
124 
125    followed by
126 
127    Server         Client
128                   Request
129    Response
130                   Request
131    Response
132                   ...
133    ...
134                   Request (type == 2)
135 
136 */
137 
138 /* Send a reply header, including length, but no payload.
139  * Return -errno on error, 0 on success. */
140 static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
141                                       uint32_t opt, uint32_t len, Error **errp)
142 {
143     uint64_t magic;
144 
145     trace_nbd_negotiate_send_rep_len(opt, nbd_opt_lookup(opt),
146                                      type, nbd_rep_lookup(type), len);
147 
148     assert(len < NBD_MAX_BUFFER_SIZE);
149     magic = cpu_to_be64(NBD_REP_MAGIC);
150     if (nbd_write(ioc, &magic, sizeof(magic), errp) < 0) {
151         error_prepend(errp, "write failed (rep magic): ");
152         return -EINVAL;
153     }
154 
155     opt = cpu_to_be32(opt);
156     if (nbd_write(ioc, &opt, sizeof(opt), errp) < 0) {
157         error_prepend(errp, "write failed (rep opt): ");
158         return -EINVAL;
159     }
160 
161     type = cpu_to_be32(type);
162     if (nbd_write(ioc, &type, sizeof(type), errp) < 0) {
163         error_prepend(errp, "write failed (rep type): ");
164         return -EINVAL;
165     }
166 
167     len = cpu_to_be32(len);
168     if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
169         error_prepend(errp, "write failed (rep data length): ");
170         return -EINVAL;
171     }
172     return 0;
173 }
174 
175 /* Send a reply header with default 0 length.
176  * Return -errno on error, 0 on success. */
177 static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt,
178                                   Error **errp)
179 {
180     return nbd_negotiate_send_rep_len(ioc, type, opt, 0, errp);
181 }
182 
183 /* Send an error reply.
184  * Return -errno on error, 0 on success. */
185 static int GCC_FMT_ATTR(5, 6)
186 nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
187                            uint32_t opt, Error **errp, const char *fmt, ...)
188 {
189     va_list va;
190     char *msg;
191     int ret;
192     size_t len;
193 
194     va_start(va, fmt);
195     msg = g_strdup_vprintf(fmt, va);
196     va_end(va);
197     len = strlen(msg);
198     assert(len < 4096);
199     trace_nbd_negotiate_send_rep_err(msg);
200     ret = nbd_negotiate_send_rep_len(ioc, type, opt, len, errp);
201     if (ret < 0) {
202         goto out;
203     }
204     if (nbd_write(ioc, msg, len, errp) < 0) {
205         error_prepend(errp, "write failed (error message): ");
206         ret = -EIO;
207     } else {
208         ret = 0;
209     }
210 
211 out:
212     g_free(msg);
213     return ret;
214 }
215 
216 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
217  * Return -errno on error, 0 on success. */
218 static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp,
219                                        Error **errp)
220 {
221     size_t name_len, desc_len;
222     uint32_t len;
223     const char *name = exp->name ? exp->name : "";
224     const char *desc = exp->description ? exp->description : "";
225     int ret;
226 
227     trace_nbd_negotiate_send_rep_list(name, desc);
228     name_len = strlen(name);
229     desc_len = strlen(desc);
230     len = name_len + desc_len + sizeof(len);
231     ret = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len,
232                                      errp);
233     if (ret < 0) {
234         return ret;
235     }
236 
237     len = cpu_to_be32(name_len);
238     if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
239         error_prepend(errp, "write failed (name length): ");
240         return -EINVAL;
241     }
242 
243     if (nbd_write(ioc, name, name_len, errp) < 0) {
244         error_prepend(errp, "write failed (name buffer): ");
245         return -EINVAL;
246     }
247 
248     if (nbd_write(ioc, desc, desc_len, errp) < 0) {
249         error_prepend(errp, "write failed (description buffer): ");
250         return -EINVAL;
251     }
252 
253     return 0;
254 }
255 
256 /* Process the NBD_OPT_LIST command, with a potential series of replies.
257  * Return -errno on error, 0 on success. */
258 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
259 {
260     NBDExport *exp;
261 
262     /* For each export, send a NBD_REP_SERVER reply. */
263     QTAILQ_FOREACH(exp, &exports, next) {
264         if (nbd_negotiate_send_rep_list(client->ioc, exp, errp)) {
265             return -EINVAL;
266         }
267     }
268     /* Finish with a NBD_REP_ACK. */
269     return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST, errp);
270 }
271 
272 /* Send a reply to NBD_OPT_EXPORT_NAME.
273  * Return -errno on error, 0 on success. */
274 static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
275                                             uint16_t myflags, bool no_zeroes,
276                                             Error **errp)
277 {
278     char name[NBD_MAX_NAME_SIZE + 1];
279     char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
280     size_t len;
281     int ret;
282 
283     /* Client sends:
284         [20 ..  xx]   export name (length bytes)
285        Server replies:
286         [ 0 ..   7]   size
287         [ 8 ..   9]   export flags
288         [10 .. 133]   reserved     (0) [unless no_zeroes]
289      */
290     trace_nbd_negotiate_handle_export_name();
291     if (length >= sizeof(name)) {
292         error_setg(errp, "Bad length received");
293         return -EINVAL;
294     }
295     if (nbd_read(client->ioc, name, length, errp) < 0) {
296         error_prepend(errp, "read failed: ");
297         return -EINVAL;
298     }
299     name[length] = '\0';
300 
301     trace_nbd_negotiate_handle_export_name_request(name);
302 
303     client->exp = nbd_export_find(name);
304     if (!client->exp) {
305         error_setg(errp, "export not found");
306         return -EINVAL;
307     }
308 
309     trace_nbd_negotiate_new_style_size_flags(client->exp->size,
310                                              client->exp->nbdflags | myflags);
311     stq_be_p(buf, client->exp->size);
312     stw_be_p(buf + 8, client->exp->nbdflags | myflags);
313     len = no_zeroes ? 10 : sizeof(buf);
314     ret = nbd_write(client->ioc, buf, len, errp);
315     if (ret < 0) {
316         error_prepend(errp, "write failed: ");
317         return ret;
318     }
319 
320     QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
321     nbd_export_get(client->exp);
322 
323     return 0;
324 }
325 
326 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
327  * The buffer does NOT include the info type prefix.
328  * Return -errno on error, 0 if ready to send more. */
329 static int nbd_negotiate_send_info(NBDClient *client, uint32_t opt,
330                                    uint16_t info, uint32_t length, void *buf,
331                                    Error **errp)
332 {
333     int rc;
334 
335     trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
336     rc = nbd_negotiate_send_rep_len(client->ioc, NBD_REP_INFO, opt,
337                                     sizeof(info) + length, errp);
338     if (rc < 0) {
339         return rc;
340     }
341     cpu_to_be16s(&info);
342     if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
343         return -EIO;
344     }
345     if (nbd_write(client->ioc, buf, length, errp) < 0) {
346         return -EIO;
347     }
348     return 0;
349 }
350 
351 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
352  * Return -errno on error, 0 if ready for next option, and 1 to move
353  * into transmission phase.  */
354 static int nbd_negotiate_handle_info(NBDClient *client, uint32_t length,
355                                      uint32_t opt, uint16_t myflags,
356                                      Error **errp)
357 {
358     int rc;
359     char name[NBD_MAX_NAME_SIZE + 1];
360     NBDExport *exp;
361     uint16_t requests;
362     uint16_t request;
363     uint32_t namelen;
364     bool sendname = false;
365     bool blocksize = false;
366     uint32_t sizes[3];
367     char buf[sizeof(uint64_t) + sizeof(uint16_t)];
368     const char *msg;
369 
370     /* Client sends:
371         4 bytes: L, name length (can be 0)
372         L bytes: export name
373         2 bytes: N, number of requests (can be 0)
374         N * 2 bytes: N requests
375     */
376     if (length < sizeof(namelen) + sizeof(requests)) {
377         msg = "overall request too short";
378         goto invalid;
379     }
380     if (nbd_read(client->ioc, &namelen, sizeof(namelen), errp) < 0) {
381         return -EIO;
382     }
383     be32_to_cpus(&namelen);
384     length -= sizeof(namelen);
385     if (namelen > length - sizeof(requests) || (length - namelen) % 2) {
386         msg = "name length is incorrect";
387         goto invalid;
388     }
389     if (nbd_read(client->ioc, name, namelen, errp) < 0) {
390         return -EIO;
391     }
392     name[namelen] = '\0';
393     length -= namelen;
394     trace_nbd_negotiate_handle_export_name_request(name);
395 
396     if (nbd_read(client->ioc, &requests, sizeof(requests), errp) < 0) {
397         return -EIO;
398     }
399     be16_to_cpus(&requests);
400     length -= sizeof(requests);
401     trace_nbd_negotiate_handle_info_requests(requests);
402     if (requests != length / sizeof(request)) {
403         msg = "incorrect number of  requests for overall length";
404         goto invalid;
405     }
406     while (requests--) {
407         if (nbd_read(client->ioc, &request, sizeof(request), errp) < 0) {
408             return -EIO;
409         }
410         be16_to_cpus(&request);
411         length -= sizeof(request);
412         trace_nbd_negotiate_handle_info_request(request,
413                                                 nbd_info_lookup(request));
414         /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
415          * everything else is either a request we don't know or
416          * something we send regardless of request */
417         switch (request) {
418         case NBD_INFO_NAME:
419             sendname = true;
420             break;
421         case NBD_INFO_BLOCK_SIZE:
422             blocksize = true;
423             break;
424         }
425     }
426 
427     exp = nbd_export_find(name);
428     if (!exp) {
429         return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_UNKNOWN,
430                                           opt, errp, "export '%s' not present",
431                                           name);
432     }
433 
434     /* Don't bother sending NBD_INFO_NAME unless client requested it */
435     if (sendname) {
436         rc = nbd_negotiate_send_info(client, opt, NBD_INFO_NAME, length, name,
437                                      errp);
438         if (rc < 0) {
439             return rc;
440         }
441     }
442 
443     /* Send NBD_INFO_DESCRIPTION only if available, regardless of
444      * client request */
445     if (exp->description) {
446         size_t len = strlen(exp->description);
447 
448         rc = nbd_negotiate_send_info(client, opt, NBD_INFO_DESCRIPTION,
449                                      len, exp->description, errp);
450         if (rc < 0) {
451             return rc;
452         }
453     }
454 
455     /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
456      * according to whether the client requested it, and according to
457      * whether this is OPT_INFO or OPT_GO. */
458     /* minimum - 1 for back-compat, or 512 if client is new enough.
459      * TODO: consult blk_bs(blk)->bl.request_alignment? */
460     sizes[0] = (opt == NBD_OPT_INFO || blocksize) ? BDRV_SECTOR_SIZE : 1;
461     /* preferred - Hard-code to 4096 for now.
462      * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
463     sizes[1] = 4096;
464     /* maximum - At most 32M, but smaller as appropriate. */
465     sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
466     trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
467     cpu_to_be32s(&sizes[0]);
468     cpu_to_be32s(&sizes[1]);
469     cpu_to_be32s(&sizes[2]);
470     rc = nbd_negotiate_send_info(client, opt, NBD_INFO_BLOCK_SIZE,
471                                  sizeof(sizes), sizes, errp);
472     if (rc < 0) {
473         return rc;
474     }
475 
476     /* Send NBD_INFO_EXPORT always */
477     trace_nbd_negotiate_new_style_size_flags(exp->size,
478                                              exp->nbdflags | myflags);
479     stq_be_p(buf, exp->size);
480     stw_be_p(buf + 8, exp->nbdflags | myflags);
481     rc = nbd_negotiate_send_info(client, opt, NBD_INFO_EXPORT,
482                                  sizeof(buf), buf, errp);
483     if (rc < 0) {
484         return rc;
485     }
486 
487     /* If the client is just asking for NBD_OPT_INFO, but forgot to
488      * request block sizes, return an error.
489      * TODO: consult blk_bs(blk)->request_align, and only error if it
490      * is not 1? */
491     if (opt == NBD_OPT_INFO && !blocksize) {
492         return nbd_negotiate_send_rep_err(client->ioc,
493                                           NBD_REP_ERR_BLOCK_SIZE_REQD, opt,
494                                           errp,
495                                           "request NBD_INFO_BLOCK_SIZE to "
496                                           "use this export");
497     }
498 
499     /* Final reply */
500     rc = nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, opt, errp);
501     if (rc < 0) {
502         return rc;
503     }
504 
505     if (opt == NBD_OPT_GO) {
506         client->exp = exp;
507         QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
508         nbd_export_get(client->exp);
509         rc = 1;
510     }
511     return rc;
512 
513  invalid:
514     if (nbd_drop(client->ioc, length, errp) < 0) {
515         return -EIO;
516     }
517     return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_INVALID, opt,
518                                       errp, "%s", msg);
519 }
520 
521 
522 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
523  * new channel for all further (now-encrypted) communication. */
524 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
525                                                  Error **errp)
526 {
527     QIOChannel *ioc;
528     QIOChannelTLS *tioc;
529     struct NBDTLSHandshakeData data = { 0 };
530 
531     trace_nbd_negotiate_handle_starttls();
532     ioc = client->ioc;
533 
534     if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
535                                NBD_OPT_STARTTLS, errp) < 0) {
536         return NULL;
537     }
538 
539     tioc = qio_channel_tls_new_server(ioc,
540                                       client->tlscreds,
541                                       client->tlsaclname,
542                                       errp);
543     if (!tioc) {
544         return NULL;
545     }
546 
547     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
548     trace_nbd_negotiate_handle_starttls_handshake();
549     data.loop = g_main_loop_new(g_main_context_default(), FALSE);
550     qio_channel_tls_handshake(tioc,
551                               nbd_tls_handshake,
552                               &data,
553                               NULL);
554 
555     if (!data.complete) {
556         g_main_loop_run(data.loop);
557     }
558     g_main_loop_unref(data.loop);
559     if (data.error) {
560         object_unref(OBJECT(tioc));
561         error_propagate(errp, data.error);
562         return NULL;
563     }
564 
565     return QIO_CHANNEL(tioc);
566 }
567 
568 /* nbd_reject_length: Handle any unexpected payload.
569  * @fatal requests that we quit talking to the client, even if we are able
570  * to successfully send an error to the guest.
571  * Return:
572  * -errno  transmission error occurred or @fatal was requested, errp is set
573  * 0       error message successfully sent to client, errp is not set
574  */
575 static int nbd_reject_length(NBDClient *client, uint32_t length,
576                              uint32_t option, bool fatal, Error **errp)
577 {
578     int ret;
579 
580     assert(length);
581     if (nbd_drop(client->ioc, length, errp) < 0) {
582         return -EIO;
583     }
584     ret = nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_INVALID,
585                                      option, errp,
586                                      "option '%s' should have zero length",
587                                      nbd_opt_lookup(option));
588     if (fatal && !ret) {
589         error_setg(errp, "option '%s' should have zero length",
590                    nbd_opt_lookup(option));
591         return -EINVAL;
592     }
593     return ret;
594 }
595 
596 /* nbd_negotiate_options
597  * Process all NBD_OPT_* client option commands, during fixed newstyle
598  * negotiation.
599  * Return:
600  * -errno  on error, errp is set
601  * 0       on successful negotiation, errp is not set
602  * 1       if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
603  *         errp is not set
604  */
605 static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
606                                  Error **errp)
607 {
608     uint32_t flags;
609     bool fixedNewstyle = false;
610     bool no_zeroes = false;
611 
612     /* Client sends:
613         [ 0 ..   3]   client flags
614 
615        Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
616         [ 0 ..   7]   NBD_OPTS_MAGIC
617         [ 8 ..  11]   NBD option
618         [12 ..  15]   Data length
619         ...           Rest of request
620 
621         [ 0 ..   7]   NBD_OPTS_MAGIC
622         [ 8 ..  11]   Second NBD option
623         [12 ..  15]   Data length
624         ...           Rest of request
625     */
626 
627     if (nbd_read(client->ioc, &flags, sizeof(flags), errp) < 0) {
628         error_prepend(errp, "read failed: ");
629         return -EIO;
630     }
631     be32_to_cpus(&flags);
632     trace_nbd_negotiate_options_flags(flags);
633     if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
634         fixedNewstyle = true;
635         flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
636     }
637     if (flags & NBD_FLAG_C_NO_ZEROES) {
638         no_zeroes = true;
639         flags &= ~NBD_FLAG_C_NO_ZEROES;
640     }
641     if (flags != 0) {
642         error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
643         return -EINVAL;
644     }
645 
646     while (1) {
647         int ret;
648         uint32_t option, length;
649         uint64_t magic;
650 
651         if (nbd_read(client->ioc, &magic, sizeof(magic), errp) < 0) {
652             error_prepend(errp, "read failed: ");
653             return -EINVAL;
654         }
655         magic = be64_to_cpu(magic);
656         trace_nbd_negotiate_options_check_magic(magic);
657         if (magic != NBD_OPTS_MAGIC) {
658             error_setg(errp, "Bad magic received");
659             return -EINVAL;
660         }
661 
662         if (nbd_read(client->ioc, &option,
663                      sizeof(option), errp) < 0) {
664             error_prepend(errp, "read failed: ");
665             return -EINVAL;
666         }
667         option = be32_to_cpu(option);
668 
669         if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) {
670             error_prepend(errp, "read failed: ");
671             return -EINVAL;
672         }
673         length = be32_to_cpu(length);
674 
675         trace_nbd_negotiate_options_check_option(option,
676                                                  nbd_opt_lookup(option));
677         if (client->tlscreds &&
678             client->ioc == (QIOChannel *)client->sioc) {
679             QIOChannel *tioc;
680             if (!fixedNewstyle) {
681                 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
682                 return -EINVAL;
683             }
684             switch (option) {
685             case NBD_OPT_STARTTLS:
686                 if (length) {
687                     /* Unconditionally drop the connection if the client
688                      * can't start a TLS negotiation correctly */
689                     return nbd_reject_length(client, length, option, true,
690                                              errp);
691                 }
692                 tioc = nbd_negotiate_handle_starttls(client, errp);
693                 if (!tioc) {
694                     return -EIO;
695                 }
696                 ret = 0;
697                 object_unref(OBJECT(client->ioc));
698                 client->ioc = QIO_CHANNEL(tioc);
699                 break;
700 
701             case NBD_OPT_EXPORT_NAME:
702                 /* No way to return an error to client, so drop connection */
703                 error_setg(errp, "Option 0x%x not permitted before TLS",
704                            option);
705                 return -EINVAL;
706 
707             default:
708                 if (nbd_drop(client->ioc, length, errp) < 0) {
709                     return -EIO;
710                 }
711                 ret = nbd_negotiate_send_rep_err(client->ioc,
712                                                  NBD_REP_ERR_TLS_REQD,
713                                                  option, errp,
714                                                  "Option 0x%" PRIx32
715                                                  "not permitted before TLS",
716                                                  option);
717                 /* Let the client keep trying, unless they asked to
718                  * quit. In this mode, we've already sent an error, so
719                  * we can't ack the abort.  */
720                 if (option == NBD_OPT_ABORT) {
721                     return 1;
722                 }
723                 break;
724             }
725         } else if (fixedNewstyle) {
726             switch (option) {
727             case NBD_OPT_LIST:
728                 if (length) {
729                     ret = nbd_reject_length(client, length, option, false,
730                                             errp);
731                 } else {
732                     ret = nbd_negotiate_handle_list(client, errp);
733                 }
734                 break;
735 
736             case NBD_OPT_ABORT:
737                 /* NBD spec says we must try to reply before
738                  * disconnecting, but that we must also tolerate
739                  * guests that don't wait for our reply. */
740                 nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, option, NULL);
741                 return 1;
742 
743             case NBD_OPT_EXPORT_NAME:
744                 return nbd_negotiate_handle_export_name(client, length,
745                                                         myflags, no_zeroes,
746                                                         errp);
747 
748             case NBD_OPT_INFO:
749             case NBD_OPT_GO:
750                 ret = nbd_negotiate_handle_info(client, length, option,
751                                                 myflags, errp);
752                 if (ret == 1) {
753                     assert(option == NBD_OPT_GO);
754                     return 0;
755                 }
756                 break;
757 
758             case NBD_OPT_STARTTLS:
759                 if (length) {
760                     ret = nbd_reject_length(client, length, option, false,
761                                             errp);
762                 } else if (client->tlscreds) {
763                     ret = nbd_negotiate_send_rep_err(client->ioc,
764                                                      NBD_REP_ERR_INVALID,
765                                                      option, errp,
766                                                      "TLS already enabled");
767                 } else {
768                     ret = nbd_negotiate_send_rep_err(client->ioc,
769                                                      NBD_REP_ERR_POLICY,
770                                                      option, errp,
771                                                      "TLS not configured");
772                 }
773                 break;
774 
775             case NBD_OPT_STRUCTURED_REPLY:
776                 if (length) {
777                     ret = nbd_reject_length(client, length, option, false,
778                                             errp);
779                 } else if (client->structured_reply) {
780                     ret = nbd_negotiate_send_rep_err(
781                         client->ioc, NBD_REP_ERR_INVALID, option, errp,
782                         "structured reply already negotiated");
783                 } else {
784                     ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
785                                                  option, errp);
786                     client->structured_reply = true;
787                     myflags |= NBD_FLAG_SEND_DF;
788                 }
789                 break;
790 
791             default:
792                 if (nbd_drop(client->ioc, length, errp) < 0) {
793                     return -EIO;
794                 }
795                 ret = nbd_negotiate_send_rep_err(client->ioc,
796                                                  NBD_REP_ERR_UNSUP,
797                                                  option, errp,
798                                                  "Unsupported option 0x%"
799                                                  PRIx32 " (%s)", option,
800                                                  nbd_opt_lookup(option));
801                 break;
802             }
803         } else {
804             /*
805              * If broken new-style we should drop the connection
806              * for anything except NBD_OPT_EXPORT_NAME
807              */
808             switch (option) {
809             case NBD_OPT_EXPORT_NAME:
810                 return nbd_negotiate_handle_export_name(client, length,
811                                                         myflags, no_zeroes,
812                                                         errp);
813 
814             default:
815                 error_setg(errp, "Unsupported option 0x%" PRIx32 " (%s)",
816                            option, nbd_opt_lookup(option));
817                 return -EINVAL;
818             }
819         }
820         if (ret < 0) {
821             return ret;
822         }
823     }
824 }
825 
826 /* nbd_negotiate
827  * Return:
828  * -errno  on error, errp is set
829  * 0       on successful negotiation, errp is not set
830  * 1       if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
831  *         errp is not set
832  */
833 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
834 {
835     char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
836     int ret;
837     const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
838                               NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA |
839                               NBD_FLAG_SEND_WRITE_ZEROES);
840     bool oldStyle;
841 
842     /* Old style negotiation header, no room for options
843         [ 0 ..   7]   passwd       ("NBDMAGIC")
844         [ 8 ..  15]   magic        (NBD_CLIENT_MAGIC)
845         [16 ..  23]   size
846         [24 ..  27]   export flags (zero-extended)
847         [28 .. 151]   reserved     (0)
848 
849        New style negotiation header, client can send options
850         [ 0 ..   7]   passwd       ("NBDMAGIC")
851         [ 8 ..  15]   magic        (NBD_OPTS_MAGIC)
852         [16 ..  17]   server flags (0)
853         ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
854      */
855 
856     qio_channel_set_blocking(client->ioc, false, NULL);
857 
858     trace_nbd_negotiate_begin();
859     memcpy(buf, "NBDMAGIC", 8);
860 
861     oldStyle = client->exp != NULL && !client->tlscreds;
862     if (oldStyle) {
863         trace_nbd_negotiate_old_style(client->exp->size,
864                                       client->exp->nbdflags | myflags);
865         stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
866         stq_be_p(buf + 16, client->exp->size);
867         stl_be_p(buf + 24, client->exp->nbdflags | myflags);
868 
869         if (nbd_write(client->ioc, buf, sizeof(buf), errp) < 0) {
870             error_prepend(errp, "write failed: ");
871             return -EINVAL;
872         }
873     } else {
874         stq_be_p(buf + 8, NBD_OPTS_MAGIC);
875         stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
876 
877         if (nbd_write(client->ioc, buf, 18, errp) < 0) {
878             error_prepend(errp, "write failed: ");
879             return -EINVAL;
880         }
881         ret = nbd_negotiate_options(client, myflags, errp);
882         if (ret != 0) {
883             if (ret < 0) {
884                 error_prepend(errp, "option negotiation failed: ");
885             }
886             return ret;
887         }
888     }
889 
890     trace_nbd_negotiate_success();
891 
892     return 0;
893 }
894 
895 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
896                                Error **errp)
897 {
898     uint8_t buf[NBD_REQUEST_SIZE];
899     uint32_t magic;
900     int ret;
901 
902     ret = nbd_read(ioc, buf, sizeof(buf), errp);
903     if (ret < 0) {
904         return ret;
905     }
906 
907     /* Request
908        [ 0 ..  3]   magic   (NBD_REQUEST_MAGIC)
909        [ 4 ..  5]   flags   (NBD_CMD_FLAG_FUA, ...)
910        [ 6 ..  7]   type    (NBD_CMD_READ, ...)
911        [ 8 .. 15]   handle
912        [16 .. 23]   from
913        [24 .. 27]   len
914      */
915 
916     magic = ldl_be_p(buf);
917     request->flags  = lduw_be_p(buf + 4);
918     request->type   = lduw_be_p(buf + 6);
919     request->handle = ldq_be_p(buf + 8);
920     request->from   = ldq_be_p(buf + 16);
921     request->len    = ldl_be_p(buf + 24);
922 
923     trace_nbd_receive_request(magic, request->flags, request->type,
924                               request->from, request->len);
925 
926     if (magic != NBD_REQUEST_MAGIC) {
927         error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
928         return -EINVAL;
929     }
930     return 0;
931 }
932 
933 #define MAX_NBD_REQUESTS 16
934 
935 void nbd_client_get(NBDClient *client)
936 {
937     client->refcount++;
938 }
939 
940 void nbd_client_put(NBDClient *client)
941 {
942     if (--client->refcount == 0) {
943         /* The last reference should be dropped by client->close,
944          * which is called by client_close.
945          */
946         assert(client->closing);
947 
948         qio_channel_detach_aio_context(client->ioc);
949         object_unref(OBJECT(client->sioc));
950         object_unref(OBJECT(client->ioc));
951         if (client->tlscreds) {
952             object_unref(OBJECT(client->tlscreds));
953         }
954         g_free(client->tlsaclname);
955         if (client->exp) {
956             QTAILQ_REMOVE(&client->exp->clients, client, next);
957             nbd_export_put(client->exp);
958         }
959         g_free(client);
960     }
961 }
962 
963 static void client_close(NBDClient *client, bool negotiated)
964 {
965     if (client->closing) {
966         return;
967     }
968 
969     client->closing = true;
970 
971     /* Force requests to finish.  They will drop their own references,
972      * then we'll close the socket and free the NBDClient.
973      */
974     qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
975                          NULL);
976 
977     /* Also tell the client, so that they release their reference.  */
978     if (client->close_fn) {
979         client->close_fn(client, negotiated);
980     }
981 }
982 
983 static NBDRequestData *nbd_request_get(NBDClient *client)
984 {
985     NBDRequestData *req;
986 
987     assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
988     client->nb_requests++;
989 
990     req = g_new0(NBDRequestData, 1);
991     nbd_client_get(client);
992     req->client = client;
993     return req;
994 }
995 
996 static void nbd_request_put(NBDRequestData *req)
997 {
998     NBDClient *client = req->client;
999 
1000     if (req->data) {
1001         qemu_vfree(req->data);
1002     }
1003     g_free(req);
1004 
1005     client->nb_requests--;
1006     nbd_client_receive_next_request(client);
1007 
1008     nbd_client_put(client);
1009 }
1010 
1011 static void blk_aio_attached(AioContext *ctx, void *opaque)
1012 {
1013     NBDExport *exp = opaque;
1014     NBDClient *client;
1015 
1016     trace_nbd_blk_aio_attached(exp->name, ctx);
1017 
1018     exp->ctx = ctx;
1019 
1020     QTAILQ_FOREACH(client, &exp->clients, next) {
1021         qio_channel_attach_aio_context(client->ioc, ctx);
1022         if (client->recv_coroutine) {
1023             aio_co_schedule(ctx, client->recv_coroutine);
1024         }
1025         if (client->send_coroutine) {
1026             aio_co_schedule(ctx, client->send_coroutine);
1027         }
1028     }
1029 }
1030 
1031 static void blk_aio_detach(void *opaque)
1032 {
1033     NBDExport *exp = opaque;
1034     NBDClient *client;
1035 
1036     trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1037 
1038     QTAILQ_FOREACH(client, &exp->clients, next) {
1039         qio_channel_detach_aio_context(client->ioc);
1040     }
1041 
1042     exp->ctx = NULL;
1043 }
1044 
1045 static void nbd_eject_notifier(Notifier *n, void *data)
1046 {
1047     NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1048     nbd_export_close(exp);
1049 }
1050 
1051 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
1052                           uint16_t nbdflags, void (*close)(NBDExport *),
1053                           bool writethrough, BlockBackend *on_eject_blk,
1054                           Error **errp)
1055 {
1056     AioContext *ctx;
1057     BlockBackend *blk;
1058     NBDExport *exp = g_new0(NBDExport, 1);
1059     uint64_t perm;
1060     int ret;
1061 
1062     /*
1063      * NBD exports are used for non-shared storage migration.  Make sure
1064      * that BDRV_O_INACTIVE is cleared and the image is ready for write
1065      * access since the export could be available before migration handover.
1066      */
1067     ctx = bdrv_get_aio_context(bs);
1068     aio_context_acquire(ctx);
1069     bdrv_invalidate_cache(bs, NULL);
1070     aio_context_release(ctx);
1071 
1072     /* Don't allow resize while the NBD server is running, otherwise we don't
1073      * care what happens with the node. */
1074     perm = BLK_PERM_CONSISTENT_READ;
1075     if ((nbdflags & NBD_FLAG_READ_ONLY) == 0) {
1076         perm |= BLK_PERM_WRITE;
1077     }
1078     blk = blk_new(perm, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1079                         BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1080     ret = blk_insert_bs(blk, bs, errp);
1081     if (ret < 0) {
1082         goto fail;
1083     }
1084     blk_set_enable_write_cache(blk, !writethrough);
1085 
1086     exp->refcount = 1;
1087     QTAILQ_INIT(&exp->clients);
1088     exp->blk = blk;
1089     exp->dev_offset = dev_offset;
1090     exp->nbdflags = nbdflags;
1091     exp->size = size < 0 ? blk_getlength(blk) : size;
1092     if (exp->size < 0) {
1093         error_setg_errno(errp, -exp->size,
1094                          "Failed to determine the NBD export's length");
1095         goto fail;
1096     }
1097     exp->size -= exp->size % BDRV_SECTOR_SIZE;
1098 
1099     exp->close = close;
1100     exp->ctx = blk_get_aio_context(blk);
1101     blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1102 
1103     if (on_eject_blk) {
1104         blk_ref(on_eject_blk);
1105         exp->eject_notifier_blk = on_eject_blk;
1106         exp->eject_notifier.notify = nbd_eject_notifier;
1107         blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
1108     }
1109     return exp;
1110 
1111 fail:
1112     blk_unref(blk);
1113     g_free(exp);
1114     return NULL;
1115 }
1116 
1117 NBDExport *nbd_export_find(const char *name)
1118 {
1119     NBDExport *exp;
1120     QTAILQ_FOREACH(exp, &exports, next) {
1121         if (strcmp(name, exp->name) == 0) {
1122             return exp;
1123         }
1124     }
1125 
1126     return NULL;
1127 }
1128 
1129 void nbd_export_set_name(NBDExport *exp, const char *name)
1130 {
1131     if (exp->name == name) {
1132         return;
1133     }
1134 
1135     nbd_export_get(exp);
1136     if (exp->name != NULL) {
1137         g_free(exp->name);
1138         exp->name = NULL;
1139         QTAILQ_REMOVE(&exports, exp, next);
1140         nbd_export_put(exp);
1141     }
1142     if (name != NULL) {
1143         nbd_export_get(exp);
1144         exp->name = g_strdup(name);
1145         QTAILQ_INSERT_TAIL(&exports, exp, next);
1146     }
1147     nbd_export_put(exp);
1148 }
1149 
1150 void nbd_export_set_description(NBDExport *exp, const char *description)
1151 {
1152     g_free(exp->description);
1153     exp->description = g_strdup(description);
1154 }
1155 
1156 void nbd_export_close(NBDExport *exp)
1157 {
1158     NBDClient *client, *next;
1159 
1160     nbd_export_get(exp);
1161     QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1162         client_close(client, true);
1163     }
1164     nbd_export_set_name(exp, NULL);
1165     nbd_export_set_description(exp, NULL);
1166     nbd_export_put(exp);
1167 }
1168 
1169 void nbd_export_get(NBDExport *exp)
1170 {
1171     assert(exp->refcount > 0);
1172     exp->refcount++;
1173 }
1174 
1175 void nbd_export_put(NBDExport *exp)
1176 {
1177     assert(exp->refcount > 0);
1178     if (exp->refcount == 1) {
1179         nbd_export_close(exp);
1180     }
1181 
1182     if (--exp->refcount == 0) {
1183         assert(exp->name == NULL);
1184         assert(exp->description == NULL);
1185 
1186         if (exp->close) {
1187             exp->close(exp);
1188         }
1189 
1190         if (exp->blk) {
1191             if (exp->eject_notifier_blk) {
1192                 notifier_remove(&exp->eject_notifier);
1193                 blk_unref(exp->eject_notifier_blk);
1194             }
1195             blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1196                                             blk_aio_detach, exp);
1197             blk_unref(exp->blk);
1198             exp->blk = NULL;
1199         }
1200 
1201         g_free(exp);
1202     }
1203 }
1204 
1205 BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
1206 {
1207     return exp->blk;
1208 }
1209 
1210 void nbd_export_close_all(void)
1211 {
1212     NBDExport *exp, *next;
1213 
1214     QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1215         nbd_export_close(exp);
1216     }
1217 }
1218 
1219 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1220                                         unsigned niov, Error **errp)
1221 {
1222     int ret;
1223 
1224     g_assert(qemu_in_coroutine());
1225     qemu_co_mutex_lock(&client->send_lock);
1226     client->send_coroutine = qemu_coroutine_self();
1227 
1228     ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1229 
1230     client->send_coroutine = NULL;
1231     qemu_co_mutex_unlock(&client->send_lock);
1232 
1233     return ret;
1234 }
1235 
1236 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1237                                        uint64_t handle)
1238 {
1239     stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1240     stl_be_p(&reply->error, error);
1241     stq_be_p(&reply->handle, handle);
1242 }
1243 
1244 static int nbd_co_send_simple_reply(NBDClient *client,
1245                                     uint64_t handle,
1246                                     uint32_t error,
1247                                     void *data,
1248                                     size_t len,
1249                                     Error **errp)
1250 {
1251     NBDSimpleReply reply;
1252     int nbd_err = system_errno_to_nbd_errno(error);
1253     struct iovec iov[] = {
1254         {.iov_base = &reply, .iov_len = sizeof(reply)},
1255         {.iov_base = data, .iov_len = len}
1256     };
1257 
1258     trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1259                                    len);
1260     set_be_simple_reply(&reply, nbd_err, handle);
1261 
1262     return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1263 }
1264 
1265 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1266                                 uint16_t type, uint64_t handle, uint32_t length)
1267 {
1268     stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1269     stw_be_p(&chunk->flags, flags);
1270     stw_be_p(&chunk->type, type);
1271     stq_be_p(&chunk->handle, handle);
1272     stl_be_p(&chunk->length, length);
1273 }
1274 
1275 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1276                                                     uint64_t handle,
1277                                                     uint64_t offset,
1278                                                     void *data,
1279                                                     size_t size,
1280                                                     Error **errp)
1281 {
1282     NBDStructuredRead chunk;
1283     struct iovec iov[] = {
1284         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1285         {.iov_base = data, .iov_len = size}
1286     };
1287 
1288     trace_nbd_co_send_structured_read(handle, offset, data, size);
1289     set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_OFFSET_DATA,
1290                  handle, sizeof(chunk) - sizeof(chunk.h) + size);
1291     stq_be_p(&chunk.offset, offset);
1292 
1293     return nbd_co_send_iov(client, iov, 2, errp);
1294 }
1295 
1296 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1297                                                      uint64_t handle,
1298                                                      uint32_t error,
1299                                                      const char *msg,
1300                                                      Error **errp)
1301 {
1302     NBDStructuredError chunk;
1303     int nbd_err = system_errno_to_nbd_errno(error);
1304     struct iovec iov[] = {
1305         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1306         {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1307     };
1308 
1309     assert(nbd_err);
1310     trace_nbd_co_send_structured_error(handle, nbd_err,
1311                                        nbd_err_lookup(nbd_err), msg ? msg : "");
1312     set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1313                  sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1314     stl_be_p(&chunk.error, nbd_err);
1315     stw_be_p(&chunk.message_length, iov[1].iov_len);
1316 
1317     return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1318 }
1319 
1320 /* nbd_co_receive_request
1321  * Collect a client request. Return 0 if request looks valid, -EIO to drop
1322  * connection right away, and any other negative value to report an error to
1323  * the client (although the caller may still need to disconnect after reporting
1324  * the error).
1325  */
1326 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
1327                                   Error **errp)
1328 {
1329     NBDClient *client = req->client;
1330     int valid_flags;
1331 
1332     g_assert(qemu_in_coroutine());
1333     assert(client->recv_coroutine == qemu_coroutine_self());
1334     if (nbd_receive_request(client->ioc, request, errp) < 0) {
1335         return -EIO;
1336     }
1337 
1338     trace_nbd_co_receive_request_decode_type(request->handle, request->type,
1339                                              nbd_cmd_lookup(request->type));
1340 
1341     if (request->type != NBD_CMD_WRITE) {
1342         /* No payload, we are ready to read the next request.  */
1343         req->complete = true;
1344     }
1345 
1346     if (request->type == NBD_CMD_DISC) {
1347         /* Special case: we're going to disconnect without a reply,
1348          * whether or not flags, from, or len are bogus */
1349         return -EIO;
1350     }
1351 
1352     /* Check for sanity in the parameters, part 1.  Defer as many
1353      * checks as possible until after reading any NBD_CMD_WRITE
1354      * payload, so we can try and keep the connection alive.  */
1355     if ((request->from + request->len) < request->from) {
1356         error_setg(errp,
1357                    "integer overflow detected, you're probably being attacked");
1358         return -EINVAL;
1359     }
1360 
1361     if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) {
1362         if (request->len > NBD_MAX_BUFFER_SIZE) {
1363             error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1364                        request->len, NBD_MAX_BUFFER_SIZE);
1365             return -EINVAL;
1366         }
1367 
1368         req->data = blk_try_blockalign(client->exp->blk, request->len);
1369         if (req->data == NULL) {
1370             error_setg(errp, "No memory");
1371             return -ENOMEM;
1372         }
1373     }
1374     if (request->type == NBD_CMD_WRITE) {
1375         if (nbd_read(client->ioc, req->data, request->len, errp) < 0) {
1376             error_prepend(errp, "reading from socket failed: ");
1377             return -EIO;
1378         }
1379         req->complete = true;
1380 
1381         trace_nbd_co_receive_request_payload_received(request->handle,
1382                                                       request->len);
1383     }
1384 
1385     /* Sanity checks, part 2. */
1386     if (request->from + request->len > client->exp->size) {
1387         error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
1388                    ", Size: %" PRIu64, request->from, request->len,
1389                    (uint64_t)client->exp->size);
1390         return request->type == NBD_CMD_WRITE ? -ENOSPC : -EINVAL;
1391     }
1392     valid_flags = NBD_CMD_FLAG_FUA;
1393     if (request->type == NBD_CMD_READ && client->structured_reply) {
1394         valid_flags |= NBD_CMD_FLAG_DF;
1395     } else if (request->type == NBD_CMD_WRITE_ZEROES) {
1396         valid_flags |= NBD_CMD_FLAG_NO_HOLE;
1397     }
1398     if (request->flags & ~valid_flags) {
1399         error_setg(errp, "unsupported flags for command %s (got 0x%x)",
1400                    nbd_cmd_lookup(request->type), request->flags);
1401         return -EINVAL;
1402     }
1403 
1404     return 0;
1405 }
1406 
1407 /* Owns a reference to the NBDClient passed as opaque.  */
1408 static coroutine_fn void nbd_trip(void *opaque)
1409 {
1410     NBDClient *client = opaque;
1411     NBDExport *exp = client->exp;
1412     NBDRequestData *req;
1413     NBDRequest request = { 0 };    /* GCC thinks it can be used uninitialized */
1414     int ret;
1415     int flags;
1416     int reply_data_len = 0;
1417     Error *local_err = NULL;
1418     char *msg = NULL;
1419 
1420     trace_nbd_trip();
1421     if (client->closing) {
1422         nbd_client_put(client);
1423         return;
1424     }
1425 
1426     req = nbd_request_get(client);
1427     ret = nbd_co_receive_request(req, &request, &local_err);
1428     client->recv_coroutine = NULL;
1429     nbd_client_receive_next_request(client);
1430     if (ret == -EIO) {
1431         goto disconnect;
1432     }
1433 
1434     if (ret < 0) {
1435         goto reply;
1436     }
1437 
1438     if (client->closing) {
1439         /*
1440          * The client may be closed when we are blocked in
1441          * nbd_co_receive_request()
1442          */
1443         goto done;
1444     }
1445 
1446     switch (request.type) {
1447     case NBD_CMD_READ:
1448         /* XXX: NBD Protocol only documents use of FUA with WRITE */
1449         if (request.flags & NBD_CMD_FLAG_FUA) {
1450             ret = blk_co_flush(exp->blk);
1451             if (ret < 0) {
1452                 error_setg_errno(&local_err, -ret, "flush failed");
1453                 break;
1454             }
1455         }
1456 
1457         ret = blk_pread(exp->blk, request.from + exp->dev_offset,
1458                         req->data, request.len);
1459         if (ret < 0) {
1460             error_setg_errno(&local_err, -ret, "reading from file failed");
1461             break;
1462         }
1463 
1464         reply_data_len = request.len;
1465 
1466         break;
1467     case NBD_CMD_WRITE:
1468         if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
1469             error_setg(&local_err, "Export is read-only");
1470             ret = -EROFS;
1471             break;
1472         }
1473 
1474         flags = 0;
1475         if (request.flags & NBD_CMD_FLAG_FUA) {
1476             flags |= BDRV_REQ_FUA;
1477         }
1478         ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
1479                          req->data, request.len, flags);
1480         if (ret < 0) {
1481             error_setg_errno(&local_err, -ret, "writing to file failed");
1482         }
1483 
1484         break;
1485     case NBD_CMD_WRITE_ZEROES:
1486         if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
1487             error_setg(&local_err, "Export is read-only");
1488             ret = -EROFS;
1489             break;
1490         }
1491 
1492         flags = 0;
1493         if (request.flags & NBD_CMD_FLAG_FUA) {
1494             flags |= BDRV_REQ_FUA;
1495         }
1496         if (!(request.flags & NBD_CMD_FLAG_NO_HOLE)) {
1497             flags |= BDRV_REQ_MAY_UNMAP;
1498         }
1499         ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset,
1500                                 request.len, flags);
1501         if (ret < 0) {
1502             error_setg_errno(&local_err, -ret, "writing to file failed");
1503         }
1504 
1505         break;
1506     case NBD_CMD_DISC:
1507         /* unreachable, thanks to special case in nbd_co_receive_request() */
1508         abort();
1509 
1510     case NBD_CMD_FLUSH:
1511         ret = blk_co_flush(exp->blk);
1512         if (ret < 0) {
1513             error_setg_errno(&local_err, -ret, "flush failed");
1514         }
1515 
1516         break;
1517     case NBD_CMD_TRIM:
1518         ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
1519                               request.len);
1520         if (ret < 0) {
1521             error_setg_errno(&local_err, -ret, "discard failed");
1522         }
1523 
1524         break;
1525     default:
1526         error_setg(&local_err, "invalid request type (%" PRIu32 ") received",
1527                    request.type);
1528         ret = -EINVAL;
1529     }
1530 
1531 reply:
1532     if (local_err) {
1533         /* If we get here, local_err was not a fatal error, and should be sent
1534          * to the client. */
1535         assert(ret < 0);
1536         msg = g_strdup(error_get_pretty(local_err));
1537         error_report_err(local_err);
1538         local_err = NULL;
1539     }
1540 
1541     if (client->structured_reply &&
1542         (ret < 0 || request.type == NBD_CMD_READ)) {
1543         if (ret < 0) {
1544             ret = nbd_co_send_structured_error(req->client, request.handle,
1545                                                -ret, msg, &local_err);
1546         } else {
1547             ret = nbd_co_send_structured_read(req->client, request.handle,
1548                                               request.from, req->data,
1549                                               reply_data_len, &local_err);
1550         }
1551     } else {
1552         ret = nbd_co_send_simple_reply(req->client, request.handle,
1553                                        ret < 0 ? -ret : 0,
1554                                        req->data, reply_data_len, &local_err);
1555     }
1556     g_free(msg);
1557     if (ret < 0) {
1558         error_prepend(&local_err, "Failed to send reply: ");
1559         goto disconnect;
1560     }
1561 
1562     /* We must disconnect after NBD_CMD_WRITE if we did not
1563      * read the payload.
1564      */
1565     if (!req->complete) {
1566         error_setg(&local_err, "Request handling failed in intermediate state");
1567         goto disconnect;
1568     }
1569 
1570 done:
1571     nbd_request_put(req);
1572     nbd_client_put(client);
1573     return;
1574 
1575 disconnect:
1576     if (local_err) {
1577         error_reportf_err(local_err, "Disconnect client, due to: ");
1578     }
1579     nbd_request_put(req);
1580     client_close(client, true);
1581     nbd_client_put(client);
1582 }
1583 
1584 static void nbd_client_receive_next_request(NBDClient *client)
1585 {
1586     if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
1587         nbd_client_get(client);
1588         client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
1589         aio_co_schedule(client->exp->ctx, client->recv_coroutine);
1590     }
1591 }
1592 
1593 static coroutine_fn void nbd_co_client_start(void *opaque)
1594 {
1595     NBDClient *client = opaque;
1596     NBDExport *exp = client->exp;
1597     Error *local_err = NULL;
1598 
1599     if (exp) {
1600         nbd_export_get(exp);
1601         QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1602     }
1603     qemu_co_mutex_init(&client->send_lock);
1604 
1605     if (nbd_negotiate(client, &local_err)) {
1606         if (local_err) {
1607             error_report_err(local_err);
1608         }
1609         client_close(client, false);
1610         return;
1611     }
1612 
1613     nbd_client_receive_next_request(client);
1614 }
1615 
1616 /*
1617  * Create a new client listener on the given export @exp, using the
1618  * given channel @sioc.  Begin servicing it in a coroutine.  When the
1619  * connection closes, call @close_fn with an indication of whether the
1620  * client completed negotiation.
1621  */
1622 void nbd_client_new(NBDExport *exp,
1623                     QIOChannelSocket *sioc,
1624                     QCryptoTLSCreds *tlscreds,
1625                     const char *tlsaclname,
1626                     void (*close_fn)(NBDClient *, bool))
1627 {
1628     NBDClient *client;
1629     Coroutine *co;
1630 
1631     client = g_new0(NBDClient, 1);
1632     client->refcount = 1;
1633     client->exp = exp;
1634     client->tlscreds = tlscreds;
1635     if (tlscreds) {
1636         object_ref(OBJECT(client->tlscreds));
1637     }
1638     client->tlsaclname = g_strdup(tlsaclname);
1639     client->sioc = sioc;
1640     object_ref(OBJECT(client->sioc));
1641     client->ioc = QIO_CHANNEL(sioc);
1642     object_ref(OBJECT(client->ioc));
1643     client->close_fn = close_fn;
1644 
1645     co = qemu_coroutine_create(nbd_co_client_start, client);
1646     qemu_coroutine_enter(co);
1647 }
1648