]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2016 Red Hat, Inc. | |
3 | * Copyright (C) 2005 Anthony Liguori <[email protected]> | |
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 "nbd-internal.h" | |
23 | ||
24 | static int nbd_errno_to_system_errno(int err) | |
25 | { | |
26 | switch (err) { | |
27 | case NBD_SUCCESS: | |
28 | return 0; | |
29 | case NBD_EPERM: | |
30 | return EPERM; | |
31 | case NBD_EIO: | |
32 | return EIO; | |
33 | case NBD_ENOMEM: | |
34 | return ENOMEM; | |
35 | case NBD_ENOSPC: | |
36 | return ENOSPC; | |
37 | default: | |
38 | TRACE("Squashing unexpected error %d to EINVAL", err); | |
39 | /* fallthrough */ | |
40 | case NBD_EINVAL: | |
41 | return EINVAL; | |
42 | } | |
43 | } | |
44 | ||
45 | /* Definitions for opaque data types */ | |
46 | ||
47 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); | |
48 | ||
49 | /* That's all folks */ | |
50 | ||
51 | /* Basic flow for negotiation | |
52 | ||
53 | Server Client | |
54 | Negotiate | |
55 | ||
56 | or | |
57 | ||
58 | Server Client | |
59 | Negotiate #1 | |
60 | Option | |
61 | Negotiate #2 | |
62 | ||
63 | ---- | |
64 | ||
65 | followed by | |
66 | ||
67 | Server Client | |
68 | Request | |
69 | Response | |
70 | Request | |
71 | Response | |
72 | ... | |
73 | ... | |
74 | Request (type == 2) | |
75 | ||
76 | */ | |
77 | ||
78 | /* Discard length bytes from channel. Return -errno on failure, or | |
79 | * the amount of bytes consumed. */ | |
80 | static ssize_t drop_sync(QIOChannel *ioc, size_t size) | |
81 | { | |
82 | ssize_t ret, dropped = size; | |
83 | char small[1024]; | |
84 | char *buffer; | |
85 | ||
86 | buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size)); | |
87 | while (size > 0) { | |
88 | ret = read_sync(ioc, buffer, MIN(65536, size)); | |
89 | if (ret < 0) { | |
90 | goto cleanup; | |
91 | } | |
92 | assert(ret <= size); | |
93 | size -= ret; | |
94 | } | |
95 | ret = dropped; | |
96 | ||
97 | cleanup: | |
98 | if (buffer != small) { | |
99 | g_free(buffer); | |
100 | } | |
101 | return ret; | |
102 | } | |
103 | ||
104 | /* Send an option request. | |
105 | * | |
106 | * The request is for option @opt, with @data containing @len bytes of | |
107 | * additional payload for the request (@len may be -1 to treat @data as | |
108 | * a C string; and @data may be NULL if @len is 0). | |
109 | * Return 0 if successful, -1 with errp set if it is impossible to | |
110 | * continue. */ | |
111 | static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt, | |
112 | uint32_t len, const char *data, | |
113 | Error **errp) | |
114 | { | |
115 | nbd_option req; | |
116 | QEMU_BUILD_BUG_ON(sizeof(req) != 16); | |
117 | ||
118 | if (len == -1) { | |
119 | req.length = len = strlen(data); | |
120 | } | |
121 | TRACE("Sending option request %" PRIu32", len %" PRIu32, opt, len); | |
122 | ||
123 | stq_be_p(&req.magic, NBD_OPTS_MAGIC); | |
124 | stl_be_p(&req.option, opt); | |
125 | stl_be_p(&req.length, len); | |
126 | ||
127 | if (write_sync(ioc, &req, sizeof(req)) != sizeof(req)) { | |
128 | error_setg(errp, "Failed to send option request header"); | |
129 | return -1; | |
130 | } | |
131 | ||
132 | if (len && write_sync(ioc, (char *) data, len) != len) { | |
133 | error_setg(errp, "Failed to send option request data"); | |
134 | return -1; | |
135 | } | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are | |
141 | * not going to attempt further negotiation. */ | |
142 | static void nbd_send_opt_abort(QIOChannel *ioc) | |
143 | { | |
144 | /* Technically, a compliant server is supposed to reply to us; but | |
145 | * older servers disconnected instead. At any rate, we're allowed | |
146 | * to disconnect without waiting for the server reply, so we don't | |
147 | * even care if the request makes it to the server, let alone | |
148 | * waiting around for whether the server replies. */ | |
149 | nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL); | |
150 | } | |
151 | ||
152 | ||
153 | /* Receive the header of an option reply, which should match the given | |
154 | * opt. Read through the length field, but NOT the length bytes of | |
155 | * payload. Return 0 if successful, -1 with errp set if it is | |
156 | * impossible to continue. */ | |
157 | static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt, | |
158 | nbd_opt_reply *reply, Error **errp) | |
159 | { | |
160 | QEMU_BUILD_BUG_ON(sizeof(*reply) != 20); | |
161 | if (read_sync(ioc, reply, sizeof(*reply)) != sizeof(*reply)) { | |
162 | error_setg(errp, "failed to read option reply"); | |
163 | nbd_send_opt_abort(ioc); | |
164 | return -1; | |
165 | } | |
166 | be64_to_cpus(&reply->magic); | |
167 | be32_to_cpus(&reply->option); | |
168 | be32_to_cpus(&reply->type); | |
169 | be32_to_cpus(&reply->length); | |
170 | ||
171 | TRACE("Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32, | |
172 | reply->option, reply->type, reply->length); | |
173 | ||
174 | if (reply->magic != NBD_REP_MAGIC) { | |
175 | error_setg(errp, "Unexpected option reply magic"); | |
176 | nbd_send_opt_abort(ioc); | |
177 | return -1; | |
178 | } | |
179 | if (reply->option != opt) { | |
180 | error_setg(errp, "Unexpected option type %x expected %x", | |
181 | reply->option, opt); | |
182 | nbd_send_opt_abort(ioc); | |
183 | return -1; | |
184 | } | |
185 | return 0; | |
186 | } | |
187 | ||
188 | /* If reply represents success, return 1 without further action. | |
189 | * If reply represents an error, consume the optional payload of | |
190 | * the packet on ioc. Then return 0 for unsupported (so the client | |
191 | * can fall back to other approaches), or -1 with errp set for other | |
192 | * errors. | |
193 | */ | |
194 | static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply, | |
195 | Error **errp) | |
196 | { | |
197 | char *msg = NULL; | |
198 | int result = -1; | |
199 | ||
200 | if (!(reply->type & (1 << 31))) { | |
201 | return 1; | |
202 | } | |
203 | ||
204 | if (reply->length) { | |
205 | if (reply->length > NBD_MAX_BUFFER_SIZE) { | |
206 | error_setg(errp, "server's error message is too long"); | |
207 | goto cleanup; | |
208 | } | |
209 | msg = g_malloc(reply->length + 1); | |
210 | if (read_sync(ioc, msg, reply->length) != reply->length) { | |
211 | error_setg(errp, "failed to read option error message"); | |
212 | goto cleanup; | |
213 | } | |
214 | msg[reply->length] = '\0'; | |
215 | } | |
216 | ||
217 | switch (reply->type) { | |
218 | case NBD_REP_ERR_UNSUP: | |
219 | TRACE("server doesn't understand request %" PRIx32 | |
220 | ", attempting fallback", reply->option); | |
221 | result = 0; | |
222 | goto cleanup; | |
223 | ||
224 | case NBD_REP_ERR_POLICY: | |
225 | error_setg(errp, "Denied by server for option %" PRIx32, | |
226 | reply->option); | |
227 | break; | |
228 | ||
229 | case NBD_REP_ERR_INVALID: | |
230 | error_setg(errp, "Invalid data length for option %" PRIx32, | |
231 | reply->option); | |
232 | break; | |
233 | ||
234 | case NBD_REP_ERR_TLS_REQD: | |
235 | error_setg(errp, "TLS negotiation required before option %" PRIx32, | |
236 | reply->option); | |
237 | break; | |
238 | ||
239 | default: | |
240 | error_setg(errp, "Unknown error code when asking for option %" PRIx32, | |
241 | reply->option); | |
242 | break; | |
243 | } | |
244 | ||
245 | if (msg) { | |
246 | error_append_hint(errp, "%s\n", msg); | |
247 | } | |
248 | ||
249 | cleanup: | |
250 | g_free(msg); | |
251 | if (result < 0) { | |
252 | nbd_send_opt_abort(ioc); | |
253 | } | |
254 | return result; | |
255 | } | |
256 | ||
257 | static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) | |
258 | { | |
259 | nbd_opt_reply reply; | |
260 | uint32_t len; | |
261 | uint32_t namelen; | |
262 | int error; | |
263 | ||
264 | *name = NULL; | |
265 | if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { | |
266 | return -1; | |
267 | } | |
268 | error = nbd_handle_reply_err(ioc, &reply, errp); | |
269 | if (error <= 0) { | |
270 | return error; | |
271 | } | |
272 | len = reply.length; | |
273 | ||
274 | if (reply.type == NBD_REP_ACK) { | |
275 | if (len != 0) { | |
276 | error_setg(errp, "length too long for option end"); | |
277 | nbd_send_opt_abort(ioc); | |
278 | return -1; | |
279 | } | |
280 | } else if (reply.type == NBD_REP_SERVER) { | |
281 | if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) { | |
282 | error_setg(errp, "incorrect option length %" PRIu32, len); | |
283 | nbd_send_opt_abort(ioc); | |
284 | return -1; | |
285 | } | |
286 | if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) { | |
287 | error_setg(errp, "failed to read option name length"); | |
288 | nbd_send_opt_abort(ioc); | |
289 | return -1; | |
290 | } | |
291 | namelen = be32_to_cpu(namelen); | |
292 | len -= sizeof(namelen); | |
293 | if (len < namelen) { | |
294 | error_setg(errp, "incorrect option name length"); | |
295 | nbd_send_opt_abort(ioc); | |
296 | return -1; | |
297 | } | |
298 | if (namelen > NBD_MAX_NAME_SIZE) { | |
299 | error_setg(errp, "export name length too long %" PRIu32, namelen); | |
300 | nbd_send_opt_abort(ioc); | |
301 | return -1; | |
302 | } | |
303 | ||
304 | *name = g_new0(char, namelen + 1); | |
305 | if (read_sync(ioc, *name, namelen) != namelen) { | |
306 | error_setg(errp, "failed to read export name"); | |
307 | g_free(*name); | |
308 | *name = NULL; | |
309 | nbd_send_opt_abort(ioc); | |
310 | return -1; | |
311 | } | |
312 | (*name)[namelen] = '\0'; | |
313 | len -= namelen; | |
314 | if (drop_sync(ioc, len) != len) { | |
315 | error_setg(errp, "failed to read export description"); | |
316 | g_free(*name); | |
317 | *name = NULL; | |
318 | nbd_send_opt_abort(ioc); | |
319 | return -1; | |
320 | } | |
321 | } else { | |
322 | error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x", | |
323 | reply.type, NBD_REP_SERVER); | |
324 | nbd_send_opt_abort(ioc); | |
325 | return -1; | |
326 | } | |
327 | return 1; | |
328 | } | |
329 | ||
330 | ||
331 | static int nbd_receive_query_exports(QIOChannel *ioc, | |
332 | const char *wantname, | |
333 | Error **errp) | |
334 | { | |
335 | bool foundExport = false; | |
336 | ||
337 | TRACE("Querying export list"); | |
338 | if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) { | |
339 | return -1; | |
340 | } | |
341 | ||
342 | TRACE("Reading available export names"); | |
343 | while (1) { | |
344 | char *name = NULL; | |
345 | int ret = nbd_receive_list(ioc, &name, errp); | |
346 | ||
347 | if (ret < 0) { | |
348 | g_free(name); | |
349 | name = NULL; | |
350 | return -1; | |
351 | } | |
352 | if (ret == 0) { | |
353 | /* Server doesn't support export listing, so | |
354 | * we will just assume an export with our | |
355 | * wanted name exists */ | |
356 | foundExport = true; | |
357 | break; | |
358 | } | |
359 | if (name == NULL) { | |
360 | TRACE("End of export name list"); | |
361 | break; | |
362 | } | |
363 | if (g_str_equal(name, wantname)) { | |
364 | foundExport = true; | |
365 | TRACE("Found desired export name '%s'", name); | |
366 | } else { | |
367 | TRACE("Ignored export name '%s'", name); | |
368 | } | |
369 | g_free(name); | |
370 | } | |
371 | ||
372 | if (!foundExport) { | |
373 | error_setg(errp, "No export with name '%s' available", wantname); | |
374 | nbd_send_opt_abort(ioc); | |
375 | return -1; | |
376 | } | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, | |
382 | QCryptoTLSCreds *tlscreds, | |
383 | const char *hostname, Error **errp) | |
384 | { | |
385 | nbd_opt_reply reply; | |
386 | QIOChannelTLS *tioc; | |
387 | struct NBDTLSHandshakeData data = { 0 }; | |
388 | ||
389 | TRACE("Requesting TLS from server"); | |
390 | if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) { | |
391 | return NULL; | |
392 | } | |
393 | ||
394 | TRACE("Getting TLS reply from server"); | |
395 | if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) { | |
396 | return NULL; | |
397 | } | |
398 | ||
399 | if (reply.type != NBD_REP_ACK) { | |
400 | error_setg(errp, "Server rejected request to start TLS %" PRIx32, | |
401 | reply.type); | |
402 | nbd_send_opt_abort(ioc); | |
403 | return NULL; | |
404 | } | |
405 | ||
406 | if (reply.length != 0) { | |
407 | error_setg(errp, "Start TLS response was not zero %" PRIu32, | |
408 | reply.length); | |
409 | nbd_send_opt_abort(ioc); | |
410 | return NULL; | |
411 | } | |
412 | ||
413 | TRACE("TLS request approved, setting up TLS"); | |
414 | tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp); | |
415 | if (!tioc) { | |
416 | return NULL; | |
417 | } | |
418 | qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls"); | |
419 | data.loop = g_main_loop_new(g_main_context_default(), FALSE); | |
420 | TRACE("Starting TLS handshake"); | |
421 | qio_channel_tls_handshake(tioc, | |
422 | nbd_tls_handshake, | |
423 | &data, | |
424 | NULL); | |
425 | ||
426 | if (!data.complete) { | |
427 | g_main_loop_run(data.loop); | |
428 | } | |
429 | g_main_loop_unref(data.loop); | |
430 | if (data.error) { | |
431 | error_propagate(errp, data.error); | |
432 | object_unref(OBJECT(tioc)); | |
433 | return NULL; | |
434 | } | |
435 | ||
436 | return QIO_CHANNEL(tioc); | |
437 | } | |
438 | ||
439 | ||
440 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, | |
441 | QCryptoTLSCreds *tlscreds, const char *hostname, | |
442 | QIOChannel **outioc, | |
443 | off_t *size, Error **errp) | |
444 | { | |
445 | char buf[256]; | |
446 | uint64_t magic, s; | |
447 | int rc; | |
448 | ||
449 | TRACE("Receiving negotiation tlscreds=%p hostname=%s.", | |
450 | tlscreds, hostname ? hostname : "<null>"); | |
451 | ||
452 | rc = -EINVAL; | |
453 | ||
454 | if (outioc) { | |
455 | *outioc = NULL; | |
456 | } | |
457 | if (tlscreds && !outioc) { | |
458 | error_setg(errp, "Output I/O channel required for TLS"); | |
459 | goto fail; | |
460 | } | |
461 | ||
462 | if (read_sync(ioc, buf, 8) != 8) { | |
463 | error_setg(errp, "Failed to read data"); | |
464 | goto fail; | |
465 | } | |
466 | ||
467 | buf[8] = '\0'; | |
468 | if (strlen(buf) == 0) { | |
469 | error_setg(errp, "Server connection closed unexpectedly"); | |
470 | goto fail; | |
471 | } | |
472 | ||
473 | TRACE("Magic is %c%c%c%c%c%c%c%c", | |
474 | qemu_isprint(buf[0]) ? buf[0] : '.', | |
475 | qemu_isprint(buf[1]) ? buf[1] : '.', | |
476 | qemu_isprint(buf[2]) ? buf[2] : '.', | |
477 | qemu_isprint(buf[3]) ? buf[3] : '.', | |
478 | qemu_isprint(buf[4]) ? buf[4] : '.', | |
479 | qemu_isprint(buf[5]) ? buf[5] : '.', | |
480 | qemu_isprint(buf[6]) ? buf[6] : '.', | |
481 | qemu_isprint(buf[7]) ? buf[7] : '.'); | |
482 | ||
483 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
484 | error_setg(errp, "Invalid magic received"); | |
485 | goto fail; | |
486 | } | |
487 | ||
488 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { | |
489 | error_setg(errp, "Failed to read magic"); | |
490 | goto fail; | |
491 | } | |
492 | magic = be64_to_cpu(magic); | |
493 | TRACE("Magic is 0x%" PRIx64, magic); | |
494 | ||
495 | if (magic == NBD_OPTS_MAGIC) { | |
496 | uint32_t clientflags = 0; | |
497 | uint16_t globalflags; | |
498 | bool fixedNewStyle = false; | |
499 | ||
500 | if (read_sync(ioc, &globalflags, sizeof(globalflags)) != | |
501 | sizeof(globalflags)) { | |
502 | error_setg(errp, "Failed to read server flags"); | |
503 | goto fail; | |
504 | } | |
505 | globalflags = be16_to_cpu(globalflags); | |
506 | TRACE("Global flags are %" PRIx32, globalflags); | |
507 | if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { | |
508 | fixedNewStyle = true; | |
509 | TRACE("Server supports fixed new style"); | |
510 | clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; | |
511 | } | |
512 | /* client requested flags */ | |
513 | clientflags = cpu_to_be32(clientflags); | |
514 | if (write_sync(ioc, &clientflags, sizeof(clientflags)) != | |
515 | sizeof(clientflags)) { | |
516 | error_setg(errp, "Failed to send clientflags field"); | |
517 | goto fail; | |
518 | } | |
519 | if (tlscreds) { | |
520 | if (fixedNewStyle) { | |
521 | *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp); | |
522 | if (!*outioc) { | |
523 | goto fail; | |
524 | } | |
525 | ioc = *outioc; | |
526 | } else { | |
527 | error_setg(errp, "Server does not support STARTTLS"); | |
528 | goto fail; | |
529 | } | |
530 | } | |
531 | if (!name) { | |
532 | TRACE("Using default NBD export name \"\""); | |
533 | name = ""; | |
534 | } | |
535 | if (fixedNewStyle) { | |
536 | /* Check our desired export is present in the | |
537 | * server export list. Since NBD_OPT_EXPORT_NAME | |
538 | * cannot return an error message, running this | |
539 | * query gives us good error reporting if the | |
540 | * server required TLS | |
541 | */ | |
542 | if (nbd_receive_query_exports(ioc, name, errp) < 0) { | |
543 | goto fail; | |
544 | } | |
545 | } | |
546 | /* write the export name request */ | |
547 | if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name, | |
548 | errp) < 0) { | |
549 | goto fail; | |
550 | } | |
551 | ||
552 | /* Read the response */ | |
553 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
554 | error_setg(errp, "Failed to read export length"); | |
555 | goto fail; | |
556 | } | |
557 | *size = be64_to_cpu(s); | |
558 | ||
559 | if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { | |
560 | error_setg(errp, "Failed to read export flags"); | |
561 | goto fail; | |
562 | } | |
563 | be16_to_cpus(flags); | |
564 | } else if (magic == NBD_CLIENT_MAGIC) { | |
565 | uint32_t oldflags; | |
566 | ||
567 | if (name) { | |
568 | error_setg(errp, "Server does not support export names"); | |
569 | goto fail; | |
570 | } | |
571 | if (tlscreds) { | |
572 | error_setg(errp, "Server does not support STARTTLS"); | |
573 | goto fail; | |
574 | } | |
575 | ||
576 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
577 | error_setg(errp, "Failed to read export length"); | |
578 | goto fail; | |
579 | } | |
580 | *size = be64_to_cpu(s); | |
581 | TRACE("Size is %" PRIu64, *size); | |
582 | ||
583 | if (read_sync(ioc, &oldflags, sizeof(oldflags)) != sizeof(oldflags)) { | |
584 | error_setg(errp, "Failed to read export flags"); | |
585 | goto fail; | |
586 | } | |
587 | be32_to_cpus(&oldflags); | |
588 | if (oldflags & ~0xffff) { | |
589 | error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags); | |
590 | goto fail; | |
591 | } | |
592 | *flags = oldflags; | |
593 | } else { | |
594 | error_setg(errp, "Bad magic received"); | |
595 | goto fail; | |
596 | } | |
597 | ||
598 | TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags); | |
599 | if (drop_sync(ioc, 124) != 124) { | |
600 | error_setg(errp, "Failed to read reserved block"); | |
601 | goto fail; | |
602 | } | |
603 | rc = 0; | |
604 | ||
605 | fail: | |
606 | return rc; | |
607 | } | |
608 | ||
609 | #ifdef __linux__ | |
610 | int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size) | |
611 | { | |
612 | unsigned long sectors = size / BDRV_SECTOR_SIZE; | |
613 | if (size / BDRV_SECTOR_SIZE != sectors) { | |
614 | LOG("Export size %lld too large for 32-bit kernel", (long long) size); | |
615 | return -E2BIG; | |
616 | } | |
617 | ||
618 | TRACE("Setting NBD socket"); | |
619 | ||
620 | if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) { | |
621 | int serrno = errno; | |
622 | LOG("Failed to set NBD socket"); | |
623 | return -serrno; | |
624 | } | |
625 | ||
626 | TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE); | |
627 | ||
628 | if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) { | |
629 | int serrno = errno; | |
630 | LOG("Failed setting NBD block size"); | |
631 | return -serrno; | |
632 | } | |
633 | ||
634 | TRACE("Setting size to %lu block(s)", sectors); | |
635 | if (size % BDRV_SECTOR_SIZE) { | |
636 | TRACE("Ignoring trailing %d bytes of export", | |
637 | (int) (size % BDRV_SECTOR_SIZE)); | |
638 | } | |
639 | ||
640 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) { | |
641 | int serrno = errno; | |
642 | LOG("Failed setting size (in blocks)"); | |
643 | return -serrno; | |
644 | } | |
645 | ||
646 | if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) { | |
647 | if (errno == ENOTTY) { | |
648 | int read_only = (flags & NBD_FLAG_READ_ONLY) != 0; | |
649 | TRACE("Setting readonly attribute"); | |
650 | ||
651 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
652 | int serrno = errno; | |
653 | LOG("Failed setting read-only attribute"); | |
654 | return -serrno; | |
655 | } | |
656 | } else { | |
657 | int serrno = errno; | |
658 | LOG("Failed setting flags"); | |
659 | return -serrno; | |
660 | } | |
661 | } | |
662 | ||
663 | TRACE("Negotiation ended"); | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | int nbd_client(int fd) | |
669 | { | |
670 | int ret; | |
671 | int serrno; | |
672 | ||
673 | TRACE("Doing NBD loop"); | |
674 | ||
675 | ret = ioctl(fd, NBD_DO_IT); | |
676 | if (ret < 0 && errno == EPIPE) { | |
677 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
678 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
679 | * that case. | |
680 | */ | |
681 | ret = 0; | |
682 | } | |
683 | serrno = errno; | |
684 | ||
685 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); | |
686 | ||
687 | TRACE("Clearing NBD queue"); | |
688 | ioctl(fd, NBD_CLEAR_QUE); | |
689 | ||
690 | TRACE("Clearing NBD socket"); | |
691 | ioctl(fd, NBD_CLEAR_SOCK); | |
692 | ||
693 | errno = serrno; | |
694 | return ret; | |
695 | } | |
696 | ||
697 | int nbd_disconnect(int fd) | |
698 | { | |
699 | ioctl(fd, NBD_CLEAR_QUE); | |
700 | ioctl(fd, NBD_DISCONNECT); | |
701 | ioctl(fd, NBD_CLEAR_SOCK); | |
702 | return 0; | |
703 | } | |
704 | ||
705 | #else | |
706 | int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size) | |
707 | { | |
708 | return -ENOTSUP; | |
709 | } | |
710 | ||
711 | int nbd_client(int fd) | |
712 | { | |
713 | return -ENOTSUP; | |
714 | } | |
715 | int nbd_disconnect(int fd) | |
716 | { | |
717 | return -ENOTSUP; | |
718 | } | |
719 | #endif | |
720 | ||
721 | ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request) | |
722 | { | |
723 | uint8_t buf[NBD_REQUEST_SIZE]; | |
724 | ssize_t ret; | |
725 | ||
726 | TRACE("Sending request to server: " | |
727 | "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 | |
728 | ", .flags = %" PRIx16 ", .type = %" PRIu16 " }", | |
729 | request->from, request->len, request->handle, | |
730 | request->flags, request->type); | |
731 | ||
732 | stl_be_p(buf, NBD_REQUEST_MAGIC); | |
733 | stw_be_p(buf + 4, request->flags); | |
734 | stw_be_p(buf + 6, request->type); | |
735 | stq_be_p(buf + 8, request->handle); | |
736 | stq_be_p(buf + 16, request->from); | |
737 | stl_be_p(buf + 24, request->len); | |
738 | ||
739 | ret = write_sync(ioc, buf, sizeof(buf)); | |
740 | if (ret < 0) { | |
741 | return ret; | |
742 | } | |
743 | ||
744 | if (ret != sizeof(buf)) { | |
745 | LOG("writing to socket failed"); | |
746 | return -EINVAL; | |
747 | } | |
748 | return 0; | |
749 | } | |
750 | ||
751 | ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply) | |
752 | { | |
753 | uint8_t buf[NBD_REPLY_SIZE]; | |
754 | uint32_t magic; | |
755 | ssize_t ret; | |
756 | ||
757 | ret = read_sync(ioc, buf, sizeof(buf)); | |
758 | if (ret < 0) { | |
759 | return ret; | |
760 | } | |
761 | ||
762 | if (ret != sizeof(buf)) { | |
763 | LOG("read failed"); | |
764 | return -EINVAL; | |
765 | } | |
766 | ||
767 | /* Reply | |
768 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
769 | [ 4 .. 7] error (0 == no error) | |
770 | [ 7 .. 15] handle | |
771 | */ | |
772 | ||
773 | magic = ldl_be_p(buf); | |
774 | reply->error = ldl_be_p(buf + 4); | |
775 | reply->handle = ldq_be_p(buf + 8); | |
776 | ||
777 | reply->error = nbd_errno_to_system_errno(reply->error); | |
778 | ||
779 | TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32 | |
780 | ", handle = %" PRIu64" }", | |
781 | magic, reply->error, reply->handle); | |
782 | ||
783 | if (magic != NBD_REPLY_MAGIC) { | |
784 | LOG("invalid magic (got 0x%" PRIx32 ")", magic); | |
785 | return -EINVAL; | |
786 | } | |
787 | return 0; | |
788 | } | |
789 |