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