xref: /openbmc/qemu/nbd/client.c (revision 80adf54e)
1 /*
2  *  Copyright (C) 2016-2017 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 "trace.h"
23 #include "nbd-internal.h"
24 
25 static int nbd_errno_to_system_errno(int err)
26 {
27     int ret;
28     switch (err) {
29     case NBD_SUCCESS:
30         ret = 0;
31         break;
32     case NBD_EPERM:
33         ret = EPERM;
34         break;
35     case NBD_EIO:
36         ret = EIO;
37         break;
38     case NBD_ENOMEM:
39         ret = ENOMEM;
40         break;
41     case NBD_ENOSPC:
42         ret = ENOSPC;
43         break;
44     case NBD_ESHUTDOWN:
45         ret = ESHUTDOWN;
46         break;
47     default:
48         trace_nbd_unknown_error(err);
49         /* fallthrough */
50     case NBD_EINVAL:
51         ret = EINVAL;
52         break;
53     }
54     return ret;
55 }
56 
57 /* Definitions for opaque data types */
58 
59 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
60 
61 /* That's all folks */
62 
63 /* Basic flow for negotiation
64 
65    Server         Client
66    Negotiate
67 
68    or
69 
70    Server         Client
71    Negotiate #1
72                   Option
73    Negotiate #2
74 
75    ----
76 
77    followed by
78 
79    Server         Client
80                   Request
81    Response
82                   Request
83    Response
84                   ...
85    ...
86                   Request (type == 2)
87 
88 */
89 
90 /* Send an option request.
91  *
92  * The request is for option @opt, with @data containing @len bytes of
93  * additional payload for the request (@len may be -1 to treat @data as
94  * a C string; and @data may be NULL if @len is 0).
95  * Return 0 if successful, -1 with errp set if it is impossible to
96  * continue. */
97 static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
98                                    uint32_t len, const char *data,
99                                    Error **errp)
100 {
101     nbd_option req;
102     QEMU_BUILD_BUG_ON(sizeof(req) != 16);
103 
104     if (len == -1) {
105         req.length = len = strlen(data);
106     }
107     trace_nbd_send_option_request(opt, nbd_opt_lookup(opt), len);
108 
109     stq_be_p(&req.magic, NBD_OPTS_MAGIC);
110     stl_be_p(&req.option, opt);
111     stl_be_p(&req.length, len);
112 
113     if (nbd_write(ioc, &req, sizeof(req), errp) < 0) {
114         error_prepend(errp, "Failed to send option request header");
115         return -1;
116     }
117 
118     if (len && nbd_write(ioc, (char *) data, len, errp) < 0) {
119         error_prepend(errp, "Failed to send option request data");
120         return -1;
121     }
122 
123     return 0;
124 }
125 
126 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
127  * not going to attempt further negotiation. */
128 static void nbd_send_opt_abort(QIOChannel *ioc)
129 {
130     /* Technically, a compliant server is supposed to reply to us; but
131      * older servers disconnected instead. At any rate, we're allowed
132      * to disconnect without waiting for the server reply, so we don't
133      * even care if the request makes it to the server, let alone
134      * waiting around for whether the server replies. */
135     nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
136 }
137 
138 
139 /* Receive the header of an option reply, which should match the given
140  * opt.  Read through the length field, but NOT the length bytes of
141  * payload. Return 0 if successful, -1 with errp set if it is
142  * impossible to continue. */
143 static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
144                                     nbd_opt_reply *reply, Error **errp)
145 {
146     QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
147     if (nbd_read(ioc, reply, sizeof(*reply), errp) < 0) {
148         error_prepend(errp, "failed to read option reply");
149         nbd_send_opt_abort(ioc);
150         return -1;
151     }
152     be64_to_cpus(&reply->magic);
153     be32_to_cpus(&reply->option);
154     be32_to_cpus(&reply->type);
155     be32_to_cpus(&reply->length);
156 
157     trace_nbd_receive_option_reply(reply->option, nbd_opt_lookup(reply->option),
158                                    reply->type, nbd_rep_lookup(reply->type),
159                                    reply->length);
160 
161     if (reply->magic != NBD_REP_MAGIC) {
162         error_setg(errp, "Unexpected option reply magic");
163         nbd_send_opt_abort(ioc);
164         return -1;
165     }
166     if (reply->option != opt) {
167         error_setg(errp, "Unexpected option type %x expected %x",
168                    reply->option, opt);
169         nbd_send_opt_abort(ioc);
170         return -1;
171     }
172     return 0;
173 }
174 
175 /* If reply represents success, return 1 without further action.
176  * If reply represents an error, consume the optional payload of
177  * the packet on ioc.  Then return 0 for unsupported (so the client
178  * can fall back to other approaches), or -1 with errp set for other
179  * errors.
180  */
181 static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
182                                 Error **errp)
183 {
184     char *msg = NULL;
185     int result = -1;
186 
187     if (!(reply->type & (1 << 31))) {
188         return 1;
189     }
190 
191     if (reply->length) {
192         if (reply->length > NBD_MAX_BUFFER_SIZE) {
193             error_setg(errp, "server error 0x%" PRIx32
194                        " (%s) message is too long",
195                        reply->type, nbd_rep_lookup(reply->type));
196             goto cleanup;
197         }
198         msg = g_malloc(reply->length + 1);
199         if (nbd_read(ioc, msg, reply->length, errp) < 0) {
200             error_prepend(errp, "failed to read option error 0x%" PRIx32
201                           " (%s) message",
202                           reply->type, nbd_rep_lookup(reply->type));
203             goto cleanup;
204         }
205         msg[reply->length] = '\0';
206     }
207 
208     switch (reply->type) {
209     case NBD_REP_ERR_UNSUP:
210         trace_nbd_reply_err_unsup(reply->option, nbd_opt_lookup(reply->option));
211         result = 0;
212         goto cleanup;
213 
214     case NBD_REP_ERR_POLICY:
215         error_setg(errp, "Denied by server for option %" PRIx32 " (%s)",
216                    reply->option, nbd_opt_lookup(reply->option));
217         break;
218 
219     case NBD_REP_ERR_INVALID:
220         error_setg(errp, "Invalid data length for option %" PRIx32 " (%s)",
221                    reply->option, nbd_opt_lookup(reply->option));
222         break;
223 
224     case NBD_REP_ERR_PLATFORM:
225         error_setg(errp, "Server lacks support for option %" PRIx32 " (%s)",
226                    reply->option, nbd_opt_lookup(reply->option));
227         break;
228 
229     case NBD_REP_ERR_TLS_REQD:
230         error_setg(errp, "TLS negotiation required before option %" PRIx32
231                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
232         break;
233 
234     case NBD_REP_ERR_UNKNOWN:
235         error_setg(errp, "Requested export not available");
236         break;
237 
238     case NBD_REP_ERR_SHUTDOWN:
239         error_setg(errp, "Server shutting down before option %" PRIx32 " (%s)",
240                    reply->option, nbd_opt_lookup(reply->option));
241         break;
242 
243     case NBD_REP_ERR_BLOCK_SIZE_REQD:
244         error_setg(errp, "Server requires INFO_BLOCK_SIZE for option %" PRIx32
245                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
246         break;
247 
248     default:
249         error_setg(errp, "Unknown error code when asking for option %" PRIx32
250                    " (%s)", reply->option, nbd_opt_lookup(reply->option));
251         break;
252     }
253 
254     if (msg) {
255         error_append_hint(errp, "server reported: %s\n", msg);
256     }
257 
258  cleanup:
259     g_free(msg);
260     if (result < 0) {
261         nbd_send_opt_abort(ioc);
262     }
263     return result;
264 }
265 
266 /* Process another portion of the NBD_OPT_LIST reply.  Set *@match if
267  * the current reply matches @want or if the server does not support
268  * NBD_OPT_LIST, otherwise leave @match alone.  Return 0 if iteration
269  * is complete, positive if more replies are expected, or negative
270  * with @errp set if an unrecoverable error occurred. */
271 static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
272                             Error **errp)
273 {
274     nbd_opt_reply reply;
275     uint32_t len;
276     uint32_t namelen;
277     char name[NBD_MAX_NAME_SIZE + 1];
278     int error;
279 
280     if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
281         return -1;
282     }
283     error = nbd_handle_reply_err(ioc, &reply, errp);
284     if (error <= 0) {
285         /* The server did not support NBD_OPT_LIST, so set *match on
286          * the assumption that any name will be accepted.  */
287         *match = true;
288         return error;
289     }
290     len = reply.length;
291 
292     if (reply.type == NBD_REP_ACK) {
293         if (len != 0) {
294             error_setg(errp, "length too long for option end");
295             nbd_send_opt_abort(ioc);
296             return -1;
297         }
298         return 0;
299     } else if (reply.type != NBD_REP_SERVER) {
300         error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
301                    reply.type, NBD_REP_SERVER);
302         nbd_send_opt_abort(ioc);
303         return -1;
304     }
305 
306     if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
307         error_setg(errp, "incorrect option length %" PRIu32, len);
308         nbd_send_opt_abort(ioc);
309         return -1;
310     }
311     if (nbd_read(ioc, &namelen, sizeof(namelen), errp) < 0) {
312         error_prepend(errp, "failed to read option name length");
313         nbd_send_opt_abort(ioc);
314         return -1;
315     }
316     namelen = be32_to_cpu(namelen);
317     len -= sizeof(namelen);
318     if (len < namelen) {
319         error_setg(errp, "incorrect option name length");
320         nbd_send_opt_abort(ioc);
321         return -1;
322     }
323     if (namelen != strlen(want)) {
324         if (nbd_drop(ioc, len, errp) < 0) {
325             error_prepend(errp, "failed to skip export name with wrong length");
326             nbd_send_opt_abort(ioc);
327             return -1;
328         }
329         return 1;
330     }
331 
332     assert(namelen < sizeof(name));
333     if (nbd_read(ioc, name, namelen, errp) < 0) {
334         error_prepend(errp, "failed to read export name");
335         nbd_send_opt_abort(ioc);
336         return -1;
337     }
338     name[namelen] = '\0';
339     len -= namelen;
340     if (nbd_drop(ioc, len, errp) < 0) {
341         error_prepend(errp, "failed to read export description");
342         nbd_send_opt_abort(ioc);
343         return -1;
344     }
345     if (!strcmp(name, want)) {
346         *match = true;
347     }
348     return 1;
349 }
350 
351 
352 /* Returns -1 if NBD_OPT_GO proves the export @wantname cannot be
353  * used, 0 if NBD_OPT_GO is unsupported (fall back to NBD_OPT_LIST and
354  * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to
355  * go (with @info populated). */
356 static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
357                       NBDExportInfo *info, Error **errp)
358 {
359     nbd_opt_reply reply;
360     uint32_t len = strlen(wantname);
361     uint16_t type;
362     int error;
363     char *buf;
364 
365     /* The protocol requires that the server send NBD_INFO_EXPORT with
366      * a non-zero flags (at least NBD_FLAG_HAS_FLAGS must be set); so
367      * flags still 0 is a witness of a broken server. */
368     info->flags = 0;
369 
370     trace_nbd_opt_go_start(wantname);
371     buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1);
372     stl_be_p(buf, len);
373     memcpy(buf + 4, wantname, len);
374     /* At most one request, everything else up to server */
375     stw_be_p(buf + 4 + len, info->request_sizes);
376     if (info->request_sizes) {
377         stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE);
378     }
379     error = nbd_send_option_request(ioc, NBD_OPT_GO,
380                                     4 + len + 2 + 2 * info->request_sizes,
381                                     buf, errp);
382     g_free(buf);
383     if (error < 0) {
384         return -1;
385     }
386 
387     while (1) {
388         if (nbd_receive_option_reply(ioc, NBD_OPT_GO, &reply, errp) < 0) {
389             return -1;
390         }
391         error = nbd_handle_reply_err(ioc, &reply, errp);
392         if (error <= 0) {
393             return error;
394         }
395         len = reply.length;
396 
397         if (reply.type == NBD_REP_ACK) {
398             /* Server is done sending info and moved into transmission
399                phase, but make sure it sent flags */
400             if (len) {
401                 error_setg(errp, "server sent invalid NBD_REP_ACK");
402                 nbd_send_opt_abort(ioc);
403                 return -1;
404             }
405             if (!info->flags) {
406                 error_setg(errp, "broken server omitted NBD_INFO_EXPORT");
407                 nbd_send_opt_abort(ioc);
408                 return -1;
409             }
410             trace_nbd_opt_go_success();
411             return 1;
412         }
413         if (reply.type != NBD_REP_INFO) {
414             error_setg(errp, "unexpected reply type %" PRIx32
415                        " (%s), expected %x",
416                        reply.type, nbd_rep_lookup(reply.type), NBD_REP_INFO);
417             nbd_send_opt_abort(ioc);
418             return -1;
419         }
420         if (len < sizeof(type)) {
421             error_setg(errp, "NBD_REP_INFO length %" PRIu32 " is too short",
422                        len);
423             nbd_send_opt_abort(ioc);
424             return -1;
425         }
426         if (nbd_read(ioc, &type, sizeof(type), errp) < 0) {
427             error_prepend(errp, "failed to read info type");
428             nbd_send_opt_abort(ioc);
429             return -1;
430         }
431         len -= sizeof(type);
432         be16_to_cpus(&type);
433         switch (type) {
434         case NBD_INFO_EXPORT:
435             if (len != sizeof(info->size) + sizeof(info->flags)) {
436                 error_setg(errp, "remaining export info len %" PRIu32
437                            " is unexpected size", len);
438                 nbd_send_opt_abort(ioc);
439                 return -1;
440             }
441             if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) {
442                 error_prepend(errp, "failed to read info size");
443                 nbd_send_opt_abort(ioc);
444                 return -1;
445             }
446             be64_to_cpus(&info->size);
447             if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) {
448                 error_prepend(errp, "failed to read info flags");
449                 nbd_send_opt_abort(ioc);
450                 return -1;
451             }
452             be16_to_cpus(&info->flags);
453             trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
454             break;
455 
456         case NBD_INFO_BLOCK_SIZE:
457             if (len != sizeof(info->min_block) * 3) {
458                 error_setg(errp, "remaining export info len %" PRIu32
459                            " is unexpected size", len);
460                 nbd_send_opt_abort(ioc);
461                 return -1;
462             }
463             if (nbd_read(ioc, &info->min_block, sizeof(info->min_block),
464                          errp) < 0) {
465                 error_prepend(errp, "failed to read info minimum block size");
466                 nbd_send_opt_abort(ioc);
467                 return -1;
468             }
469             be32_to_cpus(&info->min_block);
470             if (!is_power_of_2(info->min_block)) {
471                 error_setg(errp, "server minimum block size %" PRId32
472                            "is not a power of two", info->min_block);
473                 nbd_send_opt_abort(ioc);
474                 return -1;
475             }
476             if (nbd_read(ioc, &info->opt_block, sizeof(info->opt_block),
477                          errp) < 0) {
478                 error_prepend(errp, "failed to read info preferred block size");
479                 nbd_send_opt_abort(ioc);
480                 return -1;
481             }
482             be32_to_cpus(&info->opt_block);
483             if (!is_power_of_2(info->opt_block) ||
484                 info->opt_block < info->min_block) {
485                 error_setg(errp, "server preferred block size %" PRId32
486                            "is not valid", info->opt_block);
487                 nbd_send_opt_abort(ioc);
488                 return -1;
489             }
490             if (nbd_read(ioc, &info->max_block, sizeof(info->max_block),
491                          errp) < 0) {
492                 error_prepend(errp, "failed to read info maximum block size");
493                 nbd_send_opt_abort(ioc);
494                 return -1;
495             }
496             be32_to_cpus(&info->max_block);
497             trace_nbd_opt_go_info_block_size(info->min_block, info->opt_block,
498                                              info->max_block);
499             break;
500 
501         default:
502             trace_nbd_opt_go_info_unknown(type, nbd_info_lookup(type));
503             if (nbd_drop(ioc, len, errp) < 0) {
504                 error_prepend(errp, "Failed to read info payload");
505                 nbd_send_opt_abort(ioc);
506                 return -1;
507             }
508             break;
509         }
510     }
511 }
512 
513 /* Return -1 on failure, 0 if wantname is an available export. */
514 static int nbd_receive_query_exports(QIOChannel *ioc,
515                                      const char *wantname,
516                                      Error **errp)
517 {
518     bool foundExport = false;
519 
520     trace_nbd_receive_query_exports_start(wantname);
521     if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
522         return -1;
523     }
524 
525     while (1) {
526         int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
527 
528         if (ret < 0) {
529             /* Server gave unexpected reply */
530             return -1;
531         } else if (ret == 0) {
532             /* Done iterating. */
533             if (!foundExport) {
534                 error_setg(errp, "No export with name '%s' available",
535                            wantname);
536                 nbd_send_opt_abort(ioc);
537                 return -1;
538             }
539             trace_nbd_receive_query_exports_success(wantname);
540             return 0;
541         }
542     }
543 }
544 
545 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
546                                         QCryptoTLSCreds *tlscreds,
547                                         const char *hostname, Error **errp)
548 {
549     nbd_opt_reply reply;
550     QIOChannelTLS *tioc;
551     struct NBDTLSHandshakeData data = { 0 };
552 
553     trace_nbd_receive_starttls_request();
554     if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
555         return NULL;
556     }
557 
558     trace_nbd_receive_starttls_reply();
559     if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
560         return NULL;
561     }
562 
563     if (reply.type != NBD_REP_ACK) {
564         error_setg(errp, "Server rejected request to start TLS %" PRIx32,
565                    reply.type);
566         nbd_send_opt_abort(ioc);
567         return NULL;
568     }
569 
570     if (reply.length != 0) {
571         error_setg(errp, "Start TLS response was not zero %" PRIu32,
572                    reply.length);
573         nbd_send_opt_abort(ioc);
574         return NULL;
575     }
576 
577     trace_nbd_receive_starttls_new_client();
578     tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
579     if (!tioc) {
580         return NULL;
581     }
582     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
583     data.loop = g_main_loop_new(g_main_context_default(), FALSE);
584     trace_nbd_receive_starttls_tls_handshake();
585     qio_channel_tls_handshake(tioc,
586                               nbd_tls_handshake,
587                               &data,
588                               NULL);
589 
590     if (!data.complete) {
591         g_main_loop_run(data.loop);
592     }
593     g_main_loop_unref(data.loop);
594     if (data.error) {
595         error_propagate(errp, data.error);
596         object_unref(OBJECT(tioc));
597         return NULL;
598     }
599 
600     return QIO_CHANNEL(tioc);
601 }
602 
603 
604 int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
605                           QCryptoTLSCreds *tlscreds, const char *hostname,
606                           QIOChannel **outioc, NBDExportInfo *info,
607                           Error **errp)
608 {
609     char buf[256];
610     uint64_t magic;
611     int rc;
612     bool zeroes = true;
613 
614     trace_nbd_receive_negotiate(tlscreds, hostname ? hostname : "<null>");
615 
616     rc = -EINVAL;
617 
618     if (outioc) {
619         *outioc = NULL;
620     }
621     if (tlscreds && !outioc) {
622         error_setg(errp, "Output I/O channel required for TLS");
623         goto fail;
624     }
625 
626     if (nbd_read(ioc, buf, 8, errp) < 0) {
627         error_prepend(errp, "Failed to read data");
628         goto fail;
629     }
630 
631     buf[8] = '\0';
632     if (strlen(buf) == 0) {
633         error_setg(errp, "Server connection closed unexpectedly");
634         goto fail;
635     }
636 
637     magic = ldq_be_p(buf);
638     trace_nbd_receive_negotiate_magic(magic);
639 
640     if (memcmp(buf, "NBDMAGIC", 8) != 0) {
641         error_setg(errp, "Invalid magic received");
642         goto fail;
643     }
644 
645     if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
646         error_prepend(errp, "Failed to read magic");
647         goto fail;
648     }
649     magic = be64_to_cpu(magic);
650     trace_nbd_receive_negotiate_magic(magic);
651 
652     if (magic == NBD_OPTS_MAGIC) {
653         uint32_t clientflags = 0;
654         uint16_t globalflags;
655         bool fixedNewStyle = false;
656 
657         if (nbd_read(ioc, &globalflags, sizeof(globalflags), errp) < 0) {
658             error_prepend(errp, "Failed to read server flags");
659             goto fail;
660         }
661         globalflags = be16_to_cpu(globalflags);
662         trace_nbd_receive_negotiate_server_flags(globalflags);
663         if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
664             fixedNewStyle = true;
665             clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
666         }
667         if (globalflags & NBD_FLAG_NO_ZEROES) {
668             zeroes = false;
669             clientflags |= NBD_FLAG_C_NO_ZEROES;
670         }
671         /* client requested flags */
672         clientflags = cpu_to_be32(clientflags);
673         if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) {
674             error_prepend(errp, "Failed to send clientflags field");
675             goto fail;
676         }
677         if (tlscreds) {
678             if (fixedNewStyle) {
679                 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
680                 if (!*outioc) {
681                     goto fail;
682                 }
683                 ioc = *outioc;
684             } else {
685                 error_setg(errp, "Server does not support STARTTLS");
686                 goto fail;
687             }
688         }
689         if (!name) {
690             trace_nbd_receive_negotiate_default_name();
691             name = "";
692         }
693         if (fixedNewStyle) {
694             int result;
695 
696             /* Try NBD_OPT_GO first - if it works, we are done (it
697              * also gives us a good message if the server requires
698              * TLS).  If it is not available, fall back to
699              * NBD_OPT_LIST for nicer error messages about a missing
700              * export, then use NBD_OPT_EXPORT_NAME.  */
701             result = nbd_opt_go(ioc, name, info, errp);
702             if (result < 0) {
703                 goto fail;
704             }
705             if (result > 0) {
706                 return 0;
707             }
708             /* Check our desired export is present in the
709              * server export list. Since NBD_OPT_EXPORT_NAME
710              * cannot return an error message, running this
711              * query gives us better error reporting if the
712              * export name is not available.
713              */
714             if (nbd_receive_query_exports(ioc, name, errp) < 0) {
715                 goto fail;
716             }
717         }
718         /* write the export name request */
719         if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
720                                     errp) < 0) {
721             goto fail;
722         }
723 
724         /* Read the response */
725         if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) {
726             error_prepend(errp, "Failed to read export length");
727             goto fail;
728         }
729         be64_to_cpus(&info->size);
730 
731         if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) {
732             error_prepend(errp, "Failed to read export flags");
733             goto fail;
734         }
735         be16_to_cpus(&info->flags);
736     } else if (magic == NBD_CLIENT_MAGIC) {
737         uint32_t oldflags;
738 
739         if (name) {
740             error_setg(errp, "Server does not support export names");
741             goto fail;
742         }
743         if (tlscreds) {
744             error_setg(errp, "Server does not support STARTTLS");
745             goto fail;
746         }
747 
748         if (nbd_read(ioc, &info->size, sizeof(info->size), errp) < 0) {
749             error_prepend(errp, "Failed to read export length");
750             goto fail;
751         }
752         be64_to_cpus(&info->size);
753 
754         if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
755             error_prepend(errp, "Failed to read export flags");
756             goto fail;
757         }
758         be32_to_cpus(&oldflags);
759         if (oldflags & ~0xffff) {
760             error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
761             goto fail;
762         }
763         info->flags = oldflags;
764     } else {
765         error_setg(errp, "Bad magic received");
766         goto fail;
767     }
768 
769     trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
770     if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
771         error_prepend(errp, "Failed to read reserved block");
772         goto fail;
773     }
774     rc = 0;
775 
776 fail:
777     return rc;
778 }
779 
780 #ifdef __linux__
781 int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
782              Error **errp)
783 {
784     unsigned long sector_size = MAX(BDRV_SECTOR_SIZE, info->min_block);
785     unsigned long sectors = info->size / sector_size;
786 
787     /* FIXME: Once the kernel module is patched to honor block sizes,
788      * and to advertise that fact to user space, we should update the
789      * hand-off to the kernel to use any block sizes we learned. */
790     assert(!info->request_sizes);
791     if (info->size / sector_size != sectors) {
792         error_setg(errp, "Export size %" PRIu64 " too large for 32-bit kernel",
793                    info->size);
794         return -E2BIG;
795     }
796 
797     trace_nbd_init_set_socket();
798 
799     if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
800         int serrno = errno;
801         error_setg(errp, "Failed to set NBD socket");
802         return -serrno;
803     }
804 
805     trace_nbd_init_set_block_size(sector_size);
806 
807     if (ioctl(fd, NBD_SET_BLKSIZE, sector_size) < 0) {
808         int serrno = errno;
809         error_setg(errp, "Failed setting NBD block size");
810         return -serrno;
811     }
812 
813     trace_nbd_init_set_size(sectors);
814     if (info->size % sector_size) {
815         trace_nbd_init_trailing_bytes(info->size % sector_size);
816     }
817 
818     if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
819         int serrno = errno;
820         error_setg(errp, "Failed setting size (in blocks)");
821         return -serrno;
822     }
823 
824     if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) info->flags) < 0) {
825         if (errno == ENOTTY) {
826             int read_only = (info->flags & NBD_FLAG_READ_ONLY) != 0;
827             trace_nbd_init_set_readonly();
828 
829             if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
830                 int serrno = errno;
831                 error_setg(errp, "Failed setting read-only attribute");
832                 return -serrno;
833             }
834         } else {
835             int serrno = errno;
836             error_setg(errp, "Failed setting flags");
837             return -serrno;
838         }
839     }
840 
841     trace_nbd_init_finish();
842 
843     return 0;
844 }
845 
846 int nbd_client(int fd)
847 {
848     int ret;
849     int serrno;
850 
851     trace_nbd_client_loop();
852 
853     ret = ioctl(fd, NBD_DO_IT);
854     if (ret < 0 && errno == EPIPE) {
855         /* NBD_DO_IT normally returns EPIPE when someone has disconnected
856          * the socket via NBD_DISCONNECT.  We do not want to return 1 in
857          * that case.
858          */
859         ret = 0;
860     }
861     serrno = errno;
862 
863     trace_nbd_client_loop_ret(ret, strerror(serrno));
864 
865     trace_nbd_client_clear_queue();
866     ioctl(fd, NBD_CLEAR_QUE);
867 
868     trace_nbd_client_clear_socket();
869     ioctl(fd, NBD_CLEAR_SOCK);
870 
871     errno = serrno;
872     return ret;
873 }
874 
875 int nbd_disconnect(int fd)
876 {
877     ioctl(fd, NBD_CLEAR_QUE);
878     ioctl(fd, NBD_DISCONNECT);
879     ioctl(fd, NBD_CLEAR_SOCK);
880     return 0;
881 }
882 
883 #else
884 int nbd_init(int fd, QIOChannelSocket *ioc, NBDExportInfo *info,
885 	     Error **errp)
886 {
887     error_setg(errp, "nbd_init is only supported on Linux");
888     return -ENOTSUP;
889 }
890 
891 int nbd_client(int fd)
892 {
893     return -ENOTSUP;
894 }
895 int nbd_disconnect(int fd)
896 {
897     return -ENOTSUP;
898 }
899 #endif
900 
901 ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
902 {
903     uint8_t buf[NBD_REQUEST_SIZE];
904 
905     trace_nbd_send_request(request->from, request->len, request->handle,
906                            request->flags, request->type,
907                            nbd_cmd_lookup(request->type));
908 
909     stl_be_p(buf, NBD_REQUEST_MAGIC);
910     stw_be_p(buf + 4, request->flags);
911     stw_be_p(buf + 6, request->type);
912     stq_be_p(buf + 8, request->handle);
913     stq_be_p(buf + 16, request->from);
914     stl_be_p(buf + 24, request->len);
915 
916     return nbd_write(ioc, buf, sizeof(buf), NULL);
917 }
918 
919 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
920 {
921     uint8_t buf[NBD_REPLY_SIZE];
922     uint32_t magic;
923     ssize_t ret;
924 
925     ret = nbd_read_eof(ioc, buf, sizeof(buf), errp);
926     if (ret <= 0) {
927         return ret;
928     }
929 
930     if (ret != sizeof(buf)) {
931         error_setg(errp, "read failed");
932         return -EINVAL;
933     }
934 
935     /* Reply
936        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
937        [ 4 ..  7]    error   (0 == no error)
938        [ 7 .. 15]    handle
939      */
940 
941     magic = ldl_be_p(buf);
942     reply->error  = ldl_be_p(buf + 4);
943     reply->handle = ldq_be_p(buf + 8);
944 
945     reply->error = nbd_errno_to_system_errno(reply->error);
946 
947     if (reply->error == ESHUTDOWN) {
948         /* This works even on mingw which lacks a native ESHUTDOWN */
949         error_setg(errp, "server shutting down");
950         return -EINVAL;
951     }
952     trace_nbd_receive_reply(magic, reply->error, reply->handle);
953 
954     if (magic != NBD_REPLY_MAGIC) {
955         error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
956         return -EINVAL;
957     }
958     return sizeof(buf);
959 }
960 
961