xref: /openbmc/qemu/nbd/client.c (revision db725815985654007ade0fd53590d613fd657208)
1 /*
2  *  Copyright (C) 2016-2018 Red Hat, Inc.
3  *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
4  *
5  *  Network Block Device Client 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 "qemu/queue.h"
23 #include "trace.h"
24 #include "nbd-internal.h"
25 #include "qemu/cutils.h"
26 
27 /* Definitions for opaque data types */
28 
29 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
30 
31 /* That's all folks */
32 
33 /* Basic flow for negotiation
34 
35    Server         Client
36    Negotiate
37 
38    or
39 
40    Server         Client
41    Negotiate #1
42                   Option
43    Negotiate #2
44 
45    ----
46 
47    followed by
48 
49    Server         Client
50                   Request
51    Response
52                   Request
53    Response
54                   ...
55    ...
56                   Request (type == 2)
57 
58 */
59 
60 /* Send an option request.
61  *
62  * The request is for option @opt, with @data containing @len bytes of
63  * additional payload for the request (@len may be -1 to treat @data as
64  * a C string; and @data may be NULL if @len is 0).
65  * Return 0 if successful, -1 with errp set if it is impossible to
66  * continue. */
67 static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
68                                    uint32_t len, const char *data,
69                                    Error **errp)
70 {
71     NBDOption req;
72     QEMU_BUILD_BUG_ON(sizeof(req) != 16);
73 
74     if (len == -1) {
75         req.length = len = strlen(data);
76     }
77     trace_nbd_send_option_request(opt, nbd_opt_lookup(opt), len);
78 
79     stq_be_p(&req.magic, NBD_OPTS_MAGIC);
80     stl_be_p(&req.option, opt);
81     stl_be_p(&req.length, len);
82 
83     if (nbd_write(ioc, &req, sizeof(req), errp) < 0) {
84         error_prepend(errp, "Failed to send option request header: ");
85         return -1;
86     }
87 
88     if (len && nbd_write(ioc, (char *) data, len, errp) < 0) {
89         error_prepend(errp, "Failed to send option request data: ");
90         return -1;
91     }
92 
93     return 0;
94 }
95 
96 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
97  * not going to attempt further negotiation. */
98 static void nbd_send_opt_abort(QIOChannel *ioc)
99 {
100     /* Technically, a compliant server is supposed to reply to us; but
101      * older servers disconnected instead. At any rate, we're allowed
102      * to disconnect without waiting for the server reply, so we don't
103      * even care if the request makes it to the server, let alone
104      * waiting around for whether the server replies. */
105     nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
106 }
107 
108 
109 /* Receive the header of an option reply, which should match the given
110  * opt.  Read through the length field, but NOT the length bytes of
111  * payload. Return 0 if successful, -1 with errp set if it is
112  * impossible to continue. */
113 static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
114                                     NBDOptionReply *reply, Error **errp)
115 {
116     QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
117     if (nbd_read(ioc, reply, sizeof(*reply), "option reply", errp) < 0) {
118         nbd_send_opt_abort(ioc);
119         return -1;
120     }
121     reply->magic = be64_to_cpu(reply->magic);
122     reply->option = be32_to_cpu(reply->option);
123     reply->type = be32_to_cpu(reply->type);
124     reply->length = be32_to_cpu(reply->length);
125 
126     trace_nbd_receive_option_reply(reply->option, nbd_opt_lookup(reply->option),
127                                    reply->type, nbd_rep_lookup(reply->type),
128                                    reply->length);
129 
130     if (reply->magic != NBD_REP_MAGIC) {
131         error_setg(errp, "Unexpected option reply magic");
132         nbd_send_opt_abort(ioc);
133         return -1;
134     }
135     if (reply->option != opt) {
136         error_setg(errp, "Unexpected option type %u (%s), expected %u (%s)",
137                    reply->option, nbd_opt_lookup(reply->option),
138                    opt, nbd_opt_lookup(opt));
139         nbd_send_opt_abort(ioc);
140         return -1;
141     }
142     return 0;
143 }
144 
145 /* If reply represents success, return 1 without further action.
146  * If reply represents an error, consume the optional payload of
147  * the packet on ioc.  Then return 0 for unsupported (so the client
148  * can fall back to other approaches), or -1 with errp set for other
149  * errors.
150  */
151 static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply,
152                                 Error **errp)
153 {
154     char *msg = NULL;
155     int result = -1;
156 
157     if (!(reply->type & (1 << 31))) {
158         return 1;
159     }
160 
161     if (reply->length) {
162         if (reply->length > NBD_MAX_BUFFER_SIZE) {
163             error_setg(errp, "server error %" PRIu32
164                        " (%s) message is too long",
165                        reply->type, nbd_rep_lookup(reply->type));
166             goto cleanup;
167         }
168         msg = g_malloc(reply->length + 1);
169         if (nbd_read(ioc, msg, reply->length, NULL, errp) < 0) {
170             error_prepend(errp, "Failed to read option error %" PRIu32
171                           " (%s) message: ",
172                           reply->type, nbd_rep_lookup(reply->type));
173             goto cleanup;
174         }
175         msg[reply->length] = '\0';
176         trace_nbd_server_error_msg(reply->type,
177                                    nbd_reply_type_lookup(reply->type), msg);
178     }
179 
180     switch (reply->type) {
181     case NBD_REP_ERR_UNSUP:
182         trace_nbd_reply_err_unsup(reply->option, nbd_opt_lookup(reply->option));
183         result = 0;
184         goto cleanup;
185 
186     case NBD_REP_ERR_POLICY:
187         error_setg(errp, "Denied by server for option %" PRIu32 " (%s)",
188                    reply->option, nbd_opt_lookup(reply->option));
189         break;
190 
191     case NBD_REP_ERR_INVALID:
192         error_setg(errp, "Invalid parameters for option %" PRIu32 " (%s)",
193                    reply->option, nbd_opt_lookup(reply->option));
194         break;
195 
196     case NBD_REP_ERR_PLATFORM:
197         error_setg(errp, "Server lacks support for option %" PRIu32 " (%s)",
198                    reply->option, nbd_opt_lookup(reply->option));
199         break;
200 
201     case NBD_REP_ERR_TLS_REQD:
202         error_setg(errp, "TLS negotiation required before option %" PRIu32
203                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
204         break;
205 
206     case NBD_REP_ERR_UNKNOWN:
207         error_setg(errp, "Requested export not available");
208         break;
209 
210     case NBD_REP_ERR_SHUTDOWN:
211         error_setg(errp, "Server shutting down before option %" PRIu32 " (%s)",
212                    reply->option, nbd_opt_lookup(reply->option));
213         break;
214 
215     case NBD_REP_ERR_BLOCK_SIZE_REQD:
216         error_setg(errp, "Server requires INFO_BLOCK_SIZE for option %" PRIu32
217                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
218         break;
219 
220     default:
221         error_setg(errp, "Unknown error code when asking for option %" PRIu32
222                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
223         break;
224     }
225 
226     if (msg) {
227         error_append_hint(errp, "server reported: %s\n", msg);
228     }
229 
230  cleanup:
231     g_free(msg);
232     if (result < 0) {
233         nbd_send_opt_abort(ioc);
234     }
235     return result;
236 }
237 
238 /* nbd_receive_list:
239  * Process another portion of the NBD_OPT_LIST reply, populating any
240  * name received into *@name. If @description is non-NULL, and the
241  * server provided a description, that is also populated. The caller
242  * must eventually call g_free() on success.
243  * Returns 1 if name and description were set and iteration must continue,
244  *         0 if iteration is complete (including if OPT_LIST unsupported),
245  *         -1 with @errp set if an unrecoverable error occurred.
246  */
247 static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
248                             Error **errp)
249 {
250     int ret = -1;
251     NBDOptionReply reply;
252     uint32_t len;
253     uint32_t namelen;
254     char *local_name = NULL;
255     char *local_desc = NULL;
256     int error;
257 
258     if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
259         return -1;
260     }
261     error = nbd_handle_reply_err(ioc, &reply, errp);
262     if (error <= 0) {
263         return error;
264     }
265     len = reply.length;
266 
267     if (reply.type == NBD_REP_ACK) {
268         if (len != 0) {
269             error_setg(errp, "length too long for option end");
270             nbd_send_opt_abort(ioc);
271             return -1;
272         }
273         return 0;
274     } else if (reply.type != NBD_REP_SERVER) {
275         error_setg(errp, "Unexpected reply type %u (%s), expected %u (%s)",
276                    reply.type, nbd_rep_lookup(reply.type),
277                    NBD_REP_SERVER, nbd_rep_lookup(NBD_REP_SERVER));
278         nbd_send_opt_abort(ioc);
279         return -1;
280     }
281 
282     if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
283         error_setg(errp, "incorrect option length %" PRIu32, len);
284         nbd_send_opt_abort(ioc);
285         return -1;
286     }
287     if (nbd_read32(ioc, &namelen, "option name length", errp) < 0) {
288         nbd_send_opt_abort(ioc);
289         return -1;
290     }
291     len -= sizeof(namelen);
292     if (len < namelen) {
293         error_setg(errp, "incorrect option name length");
294         nbd_send_opt_abort(ioc);
295         return -1;
296     }
297 
298     local_name = g_malloc(namelen + 1);
299     if (nbd_read(ioc, local_name, namelen, "export name", errp) < 0) {
300         nbd_send_opt_abort(ioc);
301         goto out;
302     }
303     local_name[namelen] = '\0';
304     len -= namelen;
305     if (len) {
306         local_desc = g_malloc(len + 1);
307         if (nbd_read(ioc, local_desc, len, "export description", errp) < 0) {
308             nbd_send_opt_abort(ioc);
309             goto out;
310         }
311         local_desc[len] = '\0';
312     }
313 
314     trace_nbd_receive_list(local_name, local_desc ?: "");
315     *name = local_name;
316     local_name = NULL;
317     if (description) {
318         *description = local_desc;
319         local_desc = NULL;
320     }
321     ret = 1;
322 
323  out:
324     g_free(local_name);
325     g_free(local_desc);
326     return ret;
327 }
328 
329 
330 /*
331  * nbd_opt_info_or_go:
332  * Send option for NBD_OPT_INFO or NBD_OPT_GO and parse the reply.
333  * Returns -1 if the option proves the export @info->name cannot be
334  * used, 0 if the option is unsupported (fall back to NBD_OPT_LIST and
335  * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to
336  * go (with the rest of @info populated).
337  */
338 static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt,
339                               NBDExportInfo *info, Error **errp)
340 {
341     NBDOptionReply reply;
342     uint32_t len = strlen(info->name);
343     uint16_t type;
344     int error;
345     char *buf;
346 
347     /* The protocol requires that the server send NBD_INFO_EXPORT with
348      * a non-zero flags (at least NBD_FLAG_HAS_FLAGS must be set); so
349      * flags still 0 is a witness of a broken server. */
350     info->flags = 0;
351 
352     assert(opt == NBD_OPT_GO || opt == NBD_OPT_INFO);
353     trace_nbd_opt_info_go_start(nbd_opt_lookup(opt), info->name);
354     buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1);
355     stl_be_p(buf, len);
356     memcpy(buf + 4, info->name, len);
357     /* At most one request, everything else up to server */
358     stw_be_p(buf + 4 + len, info->request_sizes);
359     if (info->request_sizes) {
360         stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE);
361     }
362     error = nbd_send_option_request(ioc, opt,
363                                     4 + len + 2 + 2 * info->request_sizes,
364                                     buf, errp);
365     g_free(buf);
366     if (error < 0) {
367         return -1;
368     }
369 
370     while (1) {
371         if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
372             return -1;
373         }
374         error = nbd_handle_reply_err(ioc, &reply, errp);
375         if (error <= 0) {
376             return error;
377         }
378         len = reply.length;
379 
380         if (reply.type == NBD_REP_ACK) {
381             /*
382              * Server is done sending info, and moved into transmission
383              * phase for NBD_OPT_GO, but make sure it sent flags
384              */
385             if (len) {
386                 error_setg(errp, "server sent invalid NBD_REP_ACK");
387                 return -1;
388             }
389             if (!info->flags) {
390                 error_setg(errp, "broken server omitted NBD_INFO_EXPORT");
391                 return -1;
392             }
393             trace_nbd_opt_info_go_success(nbd_opt_lookup(opt));
394             return 1;
395         }
396         if (reply.type != NBD_REP_INFO) {
397             error_setg(errp, "unexpected reply type %u (%s), expected %u (%s)",
398                        reply.type, nbd_rep_lookup(reply.type),
399                        NBD_REP_INFO, nbd_rep_lookup(NBD_REP_INFO));
400             nbd_send_opt_abort(ioc);
401             return -1;
402         }
403         if (len < sizeof(type)) {
404             error_setg(errp, "NBD_REP_INFO length %" PRIu32 " is too short",
405                        len);
406             nbd_send_opt_abort(ioc);
407             return -1;
408         }
409         if (nbd_read16(ioc, &type, "info type", errp) < 0) {
410             nbd_send_opt_abort(ioc);
411             return -1;
412         }
413         len -= sizeof(type);
414         switch (type) {
415         case NBD_INFO_EXPORT:
416             if (len != sizeof(info->size) + sizeof(info->flags)) {
417                 error_setg(errp, "remaining export info len %" PRIu32
418                            " is unexpected size", len);
419                 nbd_send_opt_abort(ioc);
420                 return -1;
421             }
422             if (nbd_read64(ioc, &info->size, "info size", errp) < 0) {
423                 nbd_send_opt_abort(ioc);
424                 return -1;
425             }
426             if (nbd_read16(ioc, &info->flags, "info flags", errp) < 0) {
427                 nbd_send_opt_abort(ioc);
428                 return -1;
429             }
430             if (info->min_block &&
431                 !QEMU_IS_ALIGNED(info->size, info->min_block)) {
432                 error_setg(errp, "export size %" PRIu64 " is not multiple of "
433                            "minimum block size %" PRIu32, info->size,
434                            info->min_block);
435                 nbd_send_opt_abort(ioc);
436                 return -1;
437             }
438             trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
439             break;
440 
441         case NBD_INFO_BLOCK_SIZE:
442             if (len != sizeof(info->min_block) * 3) {
443                 error_setg(errp, "remaining export info len %" PRIu32
444                            " is unexpected size", len);
445                 nbd_send_opt_abort(ioc);
446                 return -1;
447             }
448             if (nbd_read32(ioc, &info->min_block, "info minimum block size",
449                            errp) < 0) {
450                 nbd_send_opt_abort(ioc);
451                 return -1;
452             }
453             if (!is_power_of_2(info->min_block)) {
454                 error_setg(errp, "server minimum block size %" PRIu32
455                            " is not a power of two", info->min_block);
456                 nbd_send_opt_abort(ioc);
457                 return -1;
458             }
459             if (nbd_read32(ioc, &info->opt_block, "info preferred block size",
460                            errp) < 0)
461             {
462                 nbd_send_opt_abort(ioc);
463                 return -1;
464             }
465             if (!is_power_of_2(info->opt_block) ||
466                 info->opt_block < info->min_block) {
467                 error_setg(errp, "server preferred block size %" PRIu32
468                            " is not valid", info->opt_block);
469                 nbd_send_opt_abort(ioc);
470                 return -1;
471             }
472             if (nbd_read32(ioc, &info->max_block, "info maximum block size",
473                            errp) < 0)
474             {
475                 nbd_send_opt_abort(ioc);
476                 return -1;
477             }
478             if (info->max_block < info->min_block) {
479                 error_setg(errp, "server maximum block size %" PRIu32
480                            " is not valid", info->max_block);
481                 nbd_send_opt_abort(ioc);
482                 return -1;
483             }
484             trace_nbd_opt_info_block_size(info->min_block, info->opt_block,
485                                           info->max_block);
486             break;
487 
488         default:
489             trace_nbd_opt_info_unknown(type, nbd_info_lookup(type));
490             if (nbd_drop(ioc, len, errp) < 0) {
491                 error_prepend(errp, "Failed to read info payload: ");
492                 nbd_send_opt_abort(ioc);
493                 return -1;
494             }
495             break;
496         }
497     }
498 }
499 
500 /* Return -1 on failure, 0 if wantname is an available export. */
501 static int nbd_receive_query_exports(QIOChannel *ioc,
502                                      const char *wantname,
503                                      Error **errp)
504 {
505     bool list_empty = true;
506     bool found_export = false;
507 
508     trace_nbd_receive_query_exports_start(wantname);
509     if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
510         return -1;
511     }
512 
513     while (1) {
514         char *name;
515         int ret = nbd_receive_list(ioc, &name, NULL, errp);
516 
517         if (ret < 0) {
518             /* Server gave unexpected reply */
519             return -1;
520         } else if (ret == 0) {
521             /* Done iterating. */
522             if (list_empty) {
523                 /*
524                  * We don't have enough context to tell a server that
525                  * sent an empty list apart from a server that does
526                  * not support the list command; but as this function
527                  * is just used to trigger a nicer error message
528                  * before trying NBD_OPT_EXPORT_NAME, assume the
529                  * export is available.
530                  */
531                 return 0;
532             } else if (!found_export) {
533                 error_setg(errp, "No export with name '%s' available",
534                            wantname);
535                 nbd_send_opt_abort(ioc);
536                 return -1;
537             }
538             trace_nbd_receive_query_exports_success(wantname);
539             return 0;
540         }
541         list_empty = false;
542         if (!strcmp(name, wantname)) {
543             found_export = true;
544         }
545         g_free(name);
546     }
547 }
548 
549 /* nbd_request_simple_option: Send an option request, and parse the reply
550  * return 1 for successful negotiation,
551  *        0 if operation is unsupported,
552  *        -1 with errp set for any other error
553  */
554 static int nbd_request_simple_option(QIOChannel *ioc, int opt, Error **errp)
555 {
556     NBDOptionReply reply;
557     int error;
558 
559     if (nbd_send_option_request(ioc, opt, 0, NULL, errp) < 0) {
560         return -1;
561     }
562 
563     if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
564         return -1;
565     }
566     error = nbd_handle_reply_err(ioc, &reply, errp);
567     if (error <= 0) {
568         return error;
569     }
570 
571     if (reply.type != NBD_REP_ACK) {
572         error_setg(errp, "Server answered option %d (%s) with unexpected "
573                    "reply %" PRIu32 " (%s)", opt, nbd_opt_lookup(opt),
574                    reply.type, nbd_rep_lookup(reply.type));
575         nbd_send_opt_abort(ioc);
576         return -1;
577     }
578 
579     if (reply.length != 0) {
580         error_setg(errp, "Option %d ('%s') response length is %" PRIu32
581                    " (it should be zero)", opt, nbd_opt_lookup(opt),
582                    reply.length);
583         nbd_send_opt_abort(ioc);
584         return -1;
585     }
586 
587     return 1;
588 }
589 
590 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
591                                         QCryptoTLSCreds *tlscreds,
592                                         const char *hostname, Error **errp)
593 {
594     int ret;
595     QIOChannelTLS *tioc;
596     struct NBDTLSHandshakeData data = { 0 };
597 
598     ret = nbd_request_simple_option(ioc, NBD_OPT_STARTTLS, errp);
599     if (ret <= 0) {
600         if (ret == 0) {
601             error_setg(errp, "Server don't support STARTTLS option");
602             nbd_send_opt_abort(ioc);
603         }
604         return NULL;
605     }
606 
607     trace_nbd_receive_starttls_new_client();
608     tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
609     if (!tioc) {
610         return NULL;
611     }
612     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
613     data.loop = g_main_loop_new(g_main_context_default(), FALSE);
614     trace_nbd_receive_starttls_tls_handshake();
615     qio_channel_tls_handshake(tioc,
616                               nbd_tls_handshake,
617                               &data,
618                               NULL,
619                               NULL);
620 
621     if (!data.complete) {
622         g_main_loop_run(data.loop);
623     }
624     g_main_loop_unref(data.loop);
625     if (data.error) {
626         error_propagate(errp, data.error);
627         object_unref(OBJECT(tioc));
628         return NULL;
629     }
630 
631     return QIO_CHANNEL(tioc);
632 }
633 
634 /*
635  * nbd_send_meta_query:
636  * Send 0 or 1 set/list meta context queries.
637  * Return 0 on success, -1 with errp set for any error
638  */
639 static int nbd_send_meta_query(QIOChannel *ioc, uint32_t opt,
640                                const char *export, const char *query,
641                                Error **errp)
642 {
643     int ret;
644     uint32_t export_len = strlen(export);
645     uint32_t queries = !!query;
646     uint32_t query_len = 0;
647     uint32_t data_len;
648     char *data;
649     char *p;
650 
651     data_len = sizeof(export_len) + export_len + sizeof(queries);
652     if (query) {
653         query_len = strlen(query);
654         data_len += sizeof(query_len) + query_len;
655     } else {
656         assert(opt == NBD_OPT_LIST_META_CONTEXT);
657     }
658     p = data = g_malloc(data_len);
659 
660     trace_nbd_opt_meta_request(nbd_opt_lookup(opt), query ?: "(all)", export);
661     stl_be_p(p, export_len);
662     memcpy(p += sizeof(export_len), export, export_len);
663     stl_be_p(p += export_len, queries);
664     if (query) {
665         stl_be_p(p += sizeof(queries), query_len);
666         memcpy(p += sizeof(query_len), query, query_len);
667     }
668 
669     ret = nbd_send_option_request(ioc, opt, data_len, data, errp);
670     g_free(data);
671     return ret;
672 }
673 
674 /*
675  * nbd_receive_one_meta_context:
676  * Called in a loop to receive and trace one set/list meta context reply.
677  * Pass non-NULL @name or @id to collect results back to the caller, which
678  * must eventually call g_free().
679  * return 1 if name is set and iteration must continue,
680  *        0 if iteration is complete (including if option is unsupported),
681  *        -1 with errp set for any error
682  */
683 static int nbd_receive_one_meta_context(QIOChannel *ioc,
684                                         uint32_t opt,
685                                         char **name,
686                                         uint32_t *id,
687                                         Error **errp)
688 {
689     int ret;
690     NBDOptionReply reply;
691     char *local_name = NULL;
692     uint32_t local_id;
693 
694     if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
695         return -1;
696     }
697 
698     ret = nbd_handle_reply_err(ioc, &reply, errp);
699     if (ret <= 0) {
700         return ret;
701     }
702 
703     if (reply.type == NBD_REP_ACK) {
704         if (reply.length != 0) {
705             error_setg(errp, "Unexpected length to ACK response");
706             nbd_send_opt_abort(ioc);
707             return -1;
708         }
709         return 0;
710     } else if (reply.type != NBD_REP_META_CONTEXT) {
711         error_setg(errp, "Unexpected reply type %u (%s), expected %u (%s)",
712                    reply.type, nbd_rep_lookup(reply.type),
713                    NBD_REP_META_CONTEXT, nbd_rep_lookup(NBD_REP_META_CONTEXT));
714         nbd_send_opt_abort(ioc);
715         return -1;
716     }
717 
718     if (reply.length <= sizeof(local_id) ||
719         reply.length > NBD_MAX_BUFFER_SIZE) {
720         error_setg(errp, "Failed to negotiate meta context, server "
721                    "answered with unexpected length %" PRIu32,
722                    reply.length);
723         nbd_send_opt_abort(ioc);
724         return -1;
725     }
726 
727     if (nbd_read32(ioc, &local_id, "context id", errp) < 0) {
728         return -1;
729     }
730 
731     reply.length -= sizeof(local_id);
732     local_name = g_malloc(reply.length + 1);
733     if (nbd_read(ioc, local_name, reply.length, "context name", errp) < 0) {
734         g_free(local_name);
735         return -1;
736     }
737     local_name[reply.length] = '\0';
738     trace_nbd_opt_meta_reply(nbd_opt_lookup(opt), local_name, local_id);
739 
740     if (name) {
741         *name = local_name;
742     } else {
743         g_free(local_name);
744     }
745     if (id) {
746         *id = local_id;
747     }
748     return 1;
749 }
750 
751 /*
752  * nbd_negotiate_simple_meta_context:
753  * Request the server to set the meta context for export @info->name
754  * using @info->x_dirty_bitmap with a fallback to "base:allocation",
755  * setting @info->context_id to the resulting id. Fail if the server
756  * responds with more than one context or with a context different
757  * than the query.
758  * return 1 for successful negotiation,
759  *        0 if operation is unsupported,
760  *        -1 with errp set for any other error
761  */
762 static int nbd_negotiate_simple_meta_context(QIOChannel *ioc,
763                                              NBDExportInfo *info,
764                                              Error **errp)
765 {
766     /*
767      * TODO: Removing the x_dirty_bitmap hack will mean refactoring
768      * this function to request and store ids for multiple contexts
769      * (both base:allocation and a dirty bitmap), at which point this
770      * function should lose the term _simple.
771      */
772     int ret;
773     const char *context = info->x_dirty_bitmap ?: "base:allocation";
774     bool received = false;
775     char *name = NULL;
776 
777     if (nbd_send_meta_query(ioc, NBD_OPT_SET_META_CONTEXT,
778                             info->name, context, errp) < 0) {
779         return -1;
780     }
781 
782     ret = nbd_receive_one_meta_context(ioc, NBD_OPT_SET_META_CONTEXT,
783                                        &name, &info->context_id, errp);
784     if (ret < 0) {
785         return -1;
786     }
787     if (ret == 1) {
788         if (strcmp(context, name)) {
789             error_setg(errp, "Failed to negotiate meta context '%s', server "
790                        "answered with different context '%s'", context,
791                        name);
792             g_free(name);
793             nbd_send_opt_abort(ioc);
794             return -1;
795         }
796         g_free(name);
797         received = true;
798 
799         ret = nbd_receive_one_meta_context(ioc, NBD_OPT_SET_META_CONTEXT,
800                                            NULL, NULL, errp);
801         if (ret < 0) {
802             return -1;
803         }
804     }
805     if (ret != 0) {
806         error_setg(errp, "Server answered with more than one context");
807         nbd_send_opt_abort(ioc);
808         return -1;
809     }
810     return received;
811 }
812 
813 /*
814  * nbd_list_meta_contexts:
815  * Request the server to list all meta contexts for export @info->name.
816  * return 0 if list is complete (even if empty),
817  *        -1 with errp set for any error
818  */
819 static int nbd_list_meta_contexts(QIOChannel *ioc,
820                                   NBDExportInfo *info,
821                                   Error **errp)
822 {
823     int ret;
824     int seen_any = false;
825     int seen_qemu = false;
826 
827     if (nbd_send_meta_query(ioc, NBD_OPT_LIST_META_CONTEXT,
828                             info->name, NULL, errp) < 0) {
829         return -1;
830     }
831 
832     while (1) {
833         char *context;
834 
835         ret = nbd_receive_one_meta_context(ioc, NBD_OPT_LIST_META_CONTEXT,
836                                            &context, NULL, errp);
837         if (ret == 0 && seen_any && !seen_qemu) {
838             /*
839              * Work around qemu 3.0 bug: the server forgot to send
840              * "qemu:" replies to 0 queries. If we saw at least one
841              * reply (probably base:allocation), but none of them were
842              * qemu:, then run a more specific query to make sure.
843              */
844             seen_qemu = true;
845             if (nbd_send_meta_query(ioc, NBD_OPT_LIST_META_CONTEXT,
846                                     info->name, "qemu:", errp) < 0) {
847                 return -1;
848             }
849             continue;
850         }
851         if (ret <= 0) {
852             return ret;
853         }
854         seen_any = true;
855         seen_qemu |= strstart(context, "qemu:", NULL);
856         info->contexts = g_renew(char *, info->contexts, ++info->n_contexts);
857         info->contexts[info->n_contexts - 1] = context;
858     }
859 }
860 
861 /*
862  * nbd_start_negotiate:
863  * Start the handshake to the server.  After a positive return, the server
864  * is ready to accept additional NBD_OPT requests.
865  * Returns: negative errno: failure talking to server
866  *          0: server is oldstyle, must call nbd_negotiate_finish_oldstyle
867  *          1: server is newstyle, but can only accept EXPORT_NAME
868  *          2: server is newstyle, but lacks structured replies
869  *          3: server is newstyle and set up for structured replies
870  */
871 static int nbd_start_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
872                                const char *hostname, QIOChannel **outioc,
873                                bool structured_reply, bool *zeroes,
874                                Error **errp)
875 {
876     uint64_t magic;
877 
878     trace_nbd_start_negotiate(tlscreds, hostname ? hostname : "<null>");
879 
880     if (zeroes) {
881         *zeroes = true;
882     }
883     if (outioc) {
884         *outioc = NULL;
885     }
886     if (tlscreds && !outioc) {
887         error_setg(errp, "Output I/O channel required for TLS");
888         return -EINVAL;
889     }
890 
891     if (nbd_read64(ioc, &magic, "initial magic", errp) < 0) {
892         return -EINVAL;
893     }
894     trace_nbd_receive_negotiate_magic(magic);
895 
896     if (magic != NBD_INIT_MAGIC) {
897         error_setg(errp, "Bad initial magic received: 0x%" PRIx64, magic);
898         return -EINVAL;
899     }
900 
901     if (nbd_read64(ioc, &magic, "server magic", errp) < 0) {
902         return -EINVAL;
903     }
904     trace_nbd_receive_negotiate_magic(magic);
905 
906     if (magic == NBD_OPTS_MAGIC) {
907         uint32_t clientflags = 0;
908         uint16_t globalflags;
909         bool fixedNewStyle = false;
910 
911         if (nbd_read16(ioc, &globalflags, "server flags", errp) < 0) {
912             return -EINVAL;
913         }
914         trace_nbd_receive_negotiate_server_flags(globalflags);
915         if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
916             fixedNewStyle = true;
917             clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
918         }
919         if (globalflags & NBD_FLAG_NO_ZEROES) {
920             if (zeroes) {
921                 *zeroes = false;
922             }
923             clientflags |= NBD_FLAG_C_NO_ZEROES;
924         }
925         /* client requested flags */
926         clientflags = cpu_to_be32(clientflags);
927         if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) {
928             error_prepend(errp, "Failed to send clientflags field: ");
929             return -EINVAL;
930         }
931         if (tlscreds) {
932             if (fixedNewStyle) {
933                 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
934                 if (!*outioc) {
935                     return -EINVAL;
936                 }
937                 ioc = *outioc;
938             } else {
939                 error_setg(errp, "Server does not support STARTTLS");
940                 return -EINVAL;
941             }
942         }
943         if (fixedNewStyle) {
944             int result = 0;
945 
946             if (structured_reply) {
947                 result = nbd_request_simple_option(ioc,
948                                                    NBD_OPT_STRUCTURED_REPLY,
949                                                    errp);
950                 if (result < 0) {
951                     return -EINVAL;
952                 }
953             }
954             return 2 + result;
955         } else {
956             return 1;
957         }
958     } else if (magic == NBD_CLIENT_MAGIC) {
959         if (tlscreds) {
960             error_setg(errp, "Server does not support STARTTLS");
961             return -EINVAL;
962         }
963         return 0;
964     } else {
965         error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic);
966         return -EINVAL;
967     }
968 }
969 
970 /*
971  * nbd_negotiate_finish_oldstyle:
972  * Populate @info with the size and export flags from an oldstyle server,
973  * but does not consume 124 bytes of reserved zero padding.
974  * Returns 0 on success, -1 with @errp set on failure
975  */
976 static int nbd_negotiate_finish_oldstyle(QIOChannel *ioc, NBDExportInfo *info,
977                                          Error **errp)
978 {
979     uint32_t oldflags;
980 
981     if (nbd_read64(ioc, &info->size, "export length", errp) < 0) {
982         return -EINVAL;
983     }
984 
985     if (nbd_read32(ioc, &oldflags, "export flags", errp) < 0) {
986         return -EINVAL;
987     }
988     if (oldflags & ~0xffff) {
989         error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
990         return -EINVAL;
991     }
992     info->flags = oldflags;
993     return 0;
994 }
995 
996 /*
997  * nbd_receive_negotiate:
998  * Connect to server, complete negotiation, and move into transmission phase.
999  * Returns: negative errno: failure talking to server
1000  *          0: server is connected
1001  */
1002 int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
1003                           const char *hostname, QIOChannel **outioc,
1004                           NBDExportInfo *info, Error **errp)
1005 {
1006     int result;
1007     bool zeroes;
1008     bool base_allocation = info->base_allocation;
1009 
1010     assert(info->name);
1011     trace_nbd_receive_negotiate_name(info->name);
1012 
1013     result = nbd_start_negotiate(ioc, tlscreds, hostname, outioc,
1014                                  info->structured_reply, &zeroes, errp);
1015 
1016     info->structured_reply = false;
1017     info->base_allocation = false;
1018     if (tlscreds && *outioc) {
1019         ioc = *outioc;
1020     }
1021 
1022     switch (result) {
1023     case 3: /* newstyle, with structured replies */
1024         info->structured_reply = true;
1025         if (base_allocation) {
1026             result = nbd_negotiate_simple_meta_context(ioc, info, errp);
1027             if (result < 0) {
1028                 return -EINVAL;
1029             }
1030             info->base_allocation = result == 1;
1031         }
1032         /* fall through */
1033     case 2: /* newstyle, try OPT_GO */
1034         /* Try NBD_OPT_GO first - if it works, we are done (it
1035          * also gives us a good message if the server requires
1036          * TLS).  If it is not available, fall back to
1037          * NBD_OPT_LIST for nicer error messages about a missing
1038          * export, then use NBD_OPT_EXPORT_NAME.  */
1039         result = nbd_opt_info_or_go(ioc, NBD_OPT_GO, info, errp);
1040         if (result < 0) {
1041             return -EINVAL;
1042         }
1043         if (result > 0) {
1044             return 0;
1045         }
1046         /* Check our desired export is present in the
1047          * server export list. Since NBD_OPT_EXPORT_NAME
1048          * cannot return an error message, running this
1049          * query gives us better error reporting if the
1050          * export name is not available.
1051          */
1052         if (nbd_receive_query_exports(ioc, info->name, errp) < 0) {
1053             return -EINVAL;
1054         }
1055         /* fall through */
1056     case 1: /* newstyle, but limited to EXPORT_NAME */
1057         /* write the export name request */
1058         if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, info->name,
1059                                     errp) < 0) {
1060             return -EINVAL;
1061         }
1062 
1063         /* Read the response */
1064         if (nbd_read64(ioc, &info->size, "export length", errp) < 0) {
1065             return -EINVAL;
1066         }
1067 
1068         if (nbd_read16(ioc, &info->flags, "export flags", errp) < 0) {
1069             return -EINVAL;
1070         }
1071         break;
1072     case 0: /* oldstyle, parse length and flags */
1073         if (*info->name) {
1074             error_setg(errp, "Server does not support non-empty export names");
1075             return -EINVAL;
1076         }
1077         if (nbd_negotiate_finish_oldstyle(ioc, info, errp) < 0) {
1078             return -EINVAL;
1079         }
1080         break;
1081     default:
1082         return result;
1083     }
1084 
1085     trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
1086     if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
1087         error_prepend(errp, "Failed to read reserved block: ");
1088         return -EINVAL;
1089     }
1090     return 0;
1091 }
1092 
1093 /* Clean up result of nbd_receive_export_list */
1094 void nbd_free_export_list(NBDExportInfo *info, int count)
1095 {
1096     int i, j;
1097 
1098     if (!info) {
1099         return;
1100     }
1101 
1102     for (i = 0; i < count; i++) {
1103         g_free(info[i].name);
1104         g_free(info[i].description);
1105         for (j = 0; j < info[i].n_contexts; j++) {
1106             g_free(info[i].contexts[j]);
1107         }
1108         g_free(info[i].contexts);
1109     }
1110     g_free(info);
1111 }
1112 
1113 /*
1114  * nbd_receive_export_list:
1115  * Query details about a server's exports, then disconnect without
1116  * going into transmission phase. Return a count of the exports listed
1117  * in @info by the server, or -1 on error. Caller must free @info using
1118  * nbd_free_export_list().
1119  */
1120 int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
1121                             const char *hostname, NBDExportInfo **info,
1122                             Error **errp)
1123 {
1124     int result;
1125     int count = 0;
1126     int i;
1127     int rc;
1128     int ret = -1;
1129     NBDExportInfo *array = NULL;
1130     QIOChannel *sioc = NULL;
1131 
1132     *info = NULL;
1133     result = nbd_start_negotiate(ioc, tlscreds, hostname, &sioc, true, NULL,
1134                                  errp);
1135     if (tlscreds && sioc) {
1136         ioc = sioc;
1137     }
1138 
1139     switch (result) {
1140     case 2:
1141     case 3:
1142         /* newstyle - use NBD_OPT_LIST to populate array, then try
1143          * NBD_OPT_INFO on each array member. If structured replies
1144          * are enabled, also try NBD_OPT_LIST_META_CONTEXT. */
1145         if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
1146             goto out;
1147         }
1148         while (1) {
1149             char *name;
1150             char *desc;
1151 
1152             rc = nbd_receive_list(ioc, &name, &desc, errp);
1153             if (rc < 0) {
1154                 goto out;
1155             } else if (rc == 0) {
1156                 break;
1157             }
1158             array = g_renew(NBDExportInfo, array, ++count);
1159             memset(&array[count - 1], 0, sizeof(*array));
1160             array[count - 1].name = name;
1161             array[count - 1].description = desc;
1162             array[count - 1].structured_reply = result == 3;
1163         }
1164 
1165         for (i = 0; i < count; i++) {
1166             array[i].request_sizes = true;
1167             rc = nbd_opt_info_or_go(ioc, NBD_OPT_INFO, &array[i], errp);
1168             if (rc < 0) {
1169                 goto out;
1170             } else if (rc == 0) {
1171                 /*
1172                  * Pointless to try rest of loop. If OPT_INFO doesn't work,
1173                  * it's unlikely that meta contexts work either
1174                  */
1175                 break;
1176             }
1177 
1178             if (result == 3 &&
1179                 nbd_list_meta_contexts(ioc, &array[i], errp) < 0) {
1180                 goto out;
1181             }
1182         }
1183 
1184         /* Send NBD_OPT_ABORT as a courtesy before hanging up */
1185         nbd_send_opt_abort(ioc);
1186         break;
1187     case 1: /* newstyle, but limited to EXPORT_NAME */
1188         error_setg(errp, "Server does not support export lists");
1189         /* We can't even send NBD_OPT_ABORT, so merely hang up */
1190         goto out;
1191     case 0: /* oldstyle, parse length and flags */
1192         array = g_new0(NBDExportInfo, 1);
1193         array->name = g_strdup("");
1194         count = 1;
1195 
1196         if (nbd_negotiate_finish_oldstyle(ioc, array, errp) < 0) {
1197             goto out;
1198         }
1199 
1200         /* Send NBD_CMD_DISC as a courtesy to the server, but ignore all
1201          * errors now that we have the information we wanted. */
1202         if (nbd_drop(ioc, 124, NULL) == 0) {
1203             NBDRequest request = { .type = NBD_CMD_DISC };
1204 
1205             nbd_send_request(ioc, &request);
1206         }
1207         break;
1208     default:
1209         goto out;
1210     }
1211 
1212     *info = array;
1213     array = NULL;
1214     ret = count;
1215 
1216  out:
1217     qio_channel_shutdown(ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
1218     qio_channel_close(ioc, NULL);
1219     object_unref(OBJECT(sioc));
1220     nbd_free_export_list(array, count);
1221     return ret;
1222 }
1223 
1224 #ifdef __linux__
1225 int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
1226              Error **errp)
1227 {
1228     unsigned long sector_size = MAX(BDRV_SECTOR_SIZE, info->min_block);
1229     unsigned long sectors = info->size / sector_size;
1230 
1231     /* FIXME: Once the kernel module is patched to honor block sizes,
1232      * and to advertise that fact to user space, we should update the
1233      * hand-off to the kernel to use any block sizes we learned. */
1234     assert(!info->request_sizes);
1235     if (info->size / sector_size != sectors) {
1236         error_setg(errp, "Export size %" PRIu64 " too large for 32-bit kernel",
1237                    info->size);
1238         return -E2BIG;
1239     }
1240 
1241     trace_nbd_init_set_socket();
1242 
1243     if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
1244         int serrno = errno;
1245         error_setg(errp, "Failed to set NBD socket");
1246         return -serrno;
1247     }
1248 
1249     trace_nbd_init_set_block_size(sector_size);
1250 
1251     if (ioctl(fd, NBD_SET_BLKSIZE, sector_size) < 0) {
1252         int serrno = errno;
1253         error_setg(errp, "Failed setting NBD block size");
1254         return -serrno;
1255     }
1256 
1257     trace_nbd_init_set_size(sectors);
1258     if (info->size % sector_size) {
1259         trace_nbd_init_trailing_bytes(info->size % sector_size);
1260     }
1261 
1262     if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
1263         int serrno = errno;
1264         error_setg(errp, "Failed setting size (in blocks)");
1265         return -serrno;
1266     }
1267 
1268     if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) info->flags) < 0) {
1269         if (errno == ENOTTY) {
1270             int read_only = (info->flags & NBD_FLAG_READ_ONLY) != 0;
1271             trace_nbd_init_set_readonly();
1272 
1273             if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
1274                 int serrno = errno;
1275                 error_setg(errp, "Failed setting read-only attribute");
1276                 return -serrno;
1277             }
1278         } else {
1279             int serrno = errno;
1280             error_setg(errp, "Failed setting flags");
1281             return -serrno;
1282         }
1283     }
1284 
1285     trace_nbd_init_finish();
1286 
1287     return 0;
1288 }
1289 
1290 int nbd_client(int fd)
1291 {
1292     int ret;
1293     int serrno;
1294 
1295     trace_nbd_client_loop();
1296 
1297     ret = ioctl(fd, NBD_DO_IT);
1298     if (ret < 0 && errno == EPIPE) {
1299         /* NBD_DO_IT normally returns EPIPE when someone has disconnected
1300          * the socket via NBD_DISCONNECT.  We do not want to return 1 in
1301          * that case.
1302          */
1303         ret = 0;
1304     }
1305     serrno = errno;
1306 
1307     trace_nbd_client_loop_ret(ret, strerror(serrno));
1308 
1309     trace_nbd_client_clear_queue();
1310     ioctl(fd, NBD_CLEAR_QUE);
1311 
1312     trace_nbd_client_clear_socket();
1313     ioctl(fd, NBD_CLEAR_SOCK);
1314 
1315     errno = serrno;
1316     return ret;
1317 }
1318 
1319 int nbd_disconnect(int fd)
1320 {
1321     ioctl(fd, NBD_CLEAR_QUE);
1322     ioctl(fd, NBD_DISCONNECT);
1323     ioctl(fd, NBD_CLEAR_SOCK);
1324     return 0;
1325 }
1326 
1327 #endif /* __linux__ */
1328 
1329 int nbd_send_request(QIOChannel *ioc, NBDRequest *request)
1330 {
1331     uint8_t buf[NBD_REQUEST_SIZE];
1332 
1333     trace_nbd_send_request(request->from, request->len, request->handle,
1334                            request->flags, request->type,
1335                            nbd_cmd_lookup(request->type));
1336 
1337     stl_be_p(buf, NBD_REQUEST_MAGIC);
1338     stw_be_p(buf + 4, request->flags);
1339     stw_be_p(buf + 6, request->type);
1340     stq_be_p(buf + 8, request->handle);
1341     stq_be_p(buf + 16, request->from);
1342     stl_be_p(buf + 24, request->len);
1343 
1344     return nbd_write(ioc, buf, sizeof(buf), NULL);
1345 }
1346 
1347 /* nbd_receive_simple_reply
1348  * Read simple reply except magic field (which should be already read).
1349  * Payload is not read (payload is possible for CMD_READ, but here we even
1350  * don't know whether it take place or not).
1351  */
1352 static int nbd_receive_simple_reply(QIOChannel *ioc, NBDSimpleReply *reply,
1353                                     Error **errp)
1354 {
1355     int ret;
1356 
1357     assert(reply->magic == NBD_SIMPLE_REPLY_MAGIC);
1358 
1359     ret = nbd_read(ioc, (uint8_t *)reply + sizeof(reply->magic),
1360                    sizeof(*reply) - sizeof(reply->magic), "reply", errp);
1361     if (ret < 0) {
1362         return ret;
1363     }
1364 
1365     reply->error = be32_to_cpu(reply->error);
1366     reply->handle = be64_to_cpu(reply->handle);
1367 
1368     return 0;
1369 }
1370 
1371 /* nbd_receive_structured_reply_chunk
1372  * Read structured reply chunk except magic field (which should be already
1373  * read).
1374  * Payload is not read.
1375  */
1376 static int nbd_receive_structured_reply_chunk(QIOChannel *ioc,
1377                                               NBDStructuredReplyChunk *chunk,
1378                                               Error **errp)
1379 {
1380     int ret;
1381 
1382     assert(chunk->magic == NBD_STRUCTURED_REPLY_MAGIC);
1383 
1384     ret = nbd_read(ioc, (uint8_t *)chunk + sizeof(chunk->magic),
1385                    sizeof(*chunk) - sizeof(chunk->magic), "structured chunk",
1386                    errp);
1387     if (ret < 0) {
1388         return ret;
1389     }
1390 
1391     chunk->flags = be16_to_cpu(chunk->flags);
1392     chunk->type = be16_to_cpu(chunk->type);
1393     chunk->handle = be64_to_cpu(chunk->handle);
1394     chunk->length = be32_to_cpu(chunk->length);
1395 
1396     return 0;
1397 }
1398 
1399 /* nbd_read_eof
1400  * Tries to read @size bytes from @ioc.
1401  * Returns 1 on success
1402  *         0 on eof, when no data was read (errp is not set)
1403  *         negative errno on failure (errp is set)
1404  */
1405 static inline int coroutine_fn
1406 nbd_read_eof(BlockDriverState *bs, QIOChannel *ioc, void *buffer, size_t size,
1407              Error **errp)
1408 {
1409     bool partial = false;
1410 
1411     assert(size);
1412     while (size > 0) {
1413         struct iovec iov = { .iov_base = buffer, .iov_len = size };
1414         ssize_t len;
1415 
1416         len = qio_channel_readv(ioc, &iov, 1, errp);
1417         if (len == QIO_CHANNEL_ERR_BLOCK) {
1418             bdrv_dec_in_flight(bs);
1419             qio_channel_yield(ioc, G_IO_IN);
1420             bdrv_inc_in_flight(bs);
1421             continue;
1422         } else if (len < 0) {
1423             return -EIO;
1424         } else if (len == 0) {
1425             if (partial) {
1426                 error_setg(errp,
1427                            "Unexpected end-of-file before all bytes were read");
1428                 return -EIO;
1429             } else {
1430                 return 0;
1431             }
1432         }
1433 
1434         partial = true;
1435         size -= len;
1436         buffer = (uint8_t*) buffer + len;
1437     }
1438     return 1;
1439 }
1440 
1441 /* nbd_receive_reply
1442  *
1443  * Decreases bs->in_flight while waiting for a new reply. This yield is where
1444  * we wait indefinitely and the coroutine must be able to be safely reentered
1445  * for nbd_client_attach_aio_context().
1446  *
1447  * Returns 1 on success
1448  *         0 on eof, when no data was read (errp is not set)
1449  *         negative errno on failure (errp is set)
1450  */
1451 int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc,
1452                                    NBDReply *reply, Error **errp)
1453 {
1454     int ret;
1455     const char *type;
1456 
1457     ret = nbd_read_eof(bs, ioc, &reply->magic, sizeof(reply->magic), errp);
1458     if (ret <= 0) {
1459         return ret;
1460     }
1461 
1462     reply->magic = be32_to_cpu(reply->magic);
1463 
1464     switch (reply->magic) {
1465     case NBD_SIMPLE_REPLY_MAGIC:
1466         ret = nbd_receive_simple_reply(ioc, &reply->simple, errp);
1467         if (ret < 0) {
1468             break;
1469         }
1470         trace_nbd_receive_simple_reply(reply->simple.error,
1471                                        nbd_err_lookup(reply->simple.error),
1472                                        reply->handle);
1473         break;
1474     case NBD_STRUCTURED_REPLY_MAGIC:
1475         ret = nbd_receive_structured_reply_chunk(ioc, &reply->structured, errp);
1476         if (ret < 0) {
1477             break;
1478         }
1479         type = nbd_reply_type_lookup(reply->structured.type);
1480         trace_nbd_receive_structured_reply_chunk(reply->structured.flags,
1481                                                  reply->structured.type, type,
1482                                                  reply->structured.handle,
1483                                                  reply->structured.length);
1484         break;
1485     default:
1486         error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", reply->magic);
1487         return -EINVAL;
1488     }
1489     if (ret < 0) {
1490         return ret;
1491     }
1492 
1493     return 1;
1494 }
1495 
1496