]>
Commit | Line | Data |
---|---|---|
798bfe00 FZ |
1 | /* |
2 | * Copyright (C) 2005 Anthony Liguori <[email protected]> | |
3 | * | |
4 | * Network Block Device Client Side | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
d38ea87a | 19 | #include "qemu/osdep.h" |
798bfe00 FZ |
20 | #include "nbd-internal.h" |
21 | ||
22 | static int nbd_errno_to_system_errno(int err) | |
23 | { | |
24 | switch (err) { | |
25 | case NBD_SUCCESS: | |
26 | return 0; | |
27 | case NBD_EPERM: | |
28 | return EPERM; | |
29 | case NBD_EIO: | |
30 | return EIO; | |
31 | case NBD_ENOMEM: | |
32 | return ENOMEM; | |
33 | case NBD_ENOSPC: | |
34 | return ENOSPC; | |
35 | case NBD_EINVAL: | |
36 | default: | |
37 | return EINVAL; | |
38 | } | |
39 | } | |
40 | ||
41 | /* Definitions for opaque data types */ | |
42 | ||
43 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); | |
44 | ||
45 | /* That's all folks */ | |
46 | ||
47 | /* Basic flow for negotiation | |
48 | ||
49 | Server Client | |
50 | Negotiate | |
51 | ||
52 | or | |
53 | ||
54 | Server Client | |
55 | Negotiate #1 | |
56 | Option | |
57 | Negotiate #2 | |
58 | ||
59 | ---- | |
60 | ||
61 | followed by | |
62 | ||
63 | Server Client | |
64 | Request | |
65 | Response | |
66 | Request | |
67 | Response | |
68 | ... | |
69 | ... | |
70 | Request (type == 2) | |
71 | ||
72 | */ | |
73 | ||
9344e5f5 DB |
74 | |
75 | static int nbd_handle_reply_err(uint32_t opt, uint32_t type, Error **errp) | |
76 | { | |
77 | if (!(type & (1 << 31))) { | |
78 | return 0; | |
79 | } | |
80 | ||
81 | switch (type) { | |
82 | case NBD_REP_ERR_UNSUP: | |
83 | error_setg(errp, "Unsupported option type %x", opt); | |
84 | break; | |
85 | ||
86 | case NBD_REP_ERR_INVALID: | |
87 | error_setg(errp, "Invalid data length for option %x", opt); | |
88 | break; | |
89 | ||
90 | default: | |
91 | error_setg(errp, "Unknown error code when asking for option %x", opt); | |
92 | break; | |
93 | } | |
94 | ||
95 | return -1; | |
96 | } | |
97 | ||
98 | static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) | |
99 | { | |
100 | uint64_t magic; | |
101 | uint32_t opt; | |
102 | uint32_t type; | |
103 | uint32_t len; | |
104 | uint32_t namelen; | |
105 | ||
106 | *name = NULL; | |
107 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { | |
108 | error_setg(errp, "failed to read list option magic"); | |
109 | return -1; | |
110 | } | |
111 | magic = be64_to_cpu(magic); | |
112 | if (magic != NBD_REP_MAGIC) { | |
113 | error_setg(errp, "Unexpected option list magic"); | |
114 | return -1; | |
115 | } | |
116 | if (read_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) { | |
117 | error_setg(errp, "failed to read list option"); | |
118 | return -1; | |
119 | } | |
120 | opt = be32_to_cpu(opt); | |
121 | if (opt != NBD_OPT_LIST) { | |
122 | error_setg(errp, "Unexpected option type %x expected %x", | |
123 | opt, NBD_OPT_LIST); | |
124 | return -1; | |
125 | } | |
126 | ||
127 | if (read_sync(ioc, &type, sizeof(type)) != sizeof(type)) { | |
128 | error_setg(errp, "failed to read list option type"); | |
129 | return -1; | |
130 | } | |
131 | type = be32_to_cpu(type); | |
132 | if (type == NBD_REP_ERR_UNSUP) { | |
133 | return 0; | |
134 | } | |
135 | if (nbd_handle_reply_err(opt, type, errp) < 0) { | |
136 | return -1; | |
137 | } | |
138 | ||
139 | if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) { | |
140 | error_setg(errp, "failed to read option length"); | |
141 | return -1; | |
142 | } | |
143 | len = be32_to_cpu(len); | |
144 | ||
145 | if (type == NBD_REP_ACK) { | |
146 | if (len != 0) { | |
147 | error_setg(errp, "length too long for option end"); | |
148 | return -1; | |
149 | } | |
150 | } else if (type == NBD_REP_SERVER) { | |
151 | if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) { | |
152 | error_setg(errp, "failed to read option name length"); | |
153 | return -1; | |
154 | } | |
155 | namelen = be32_to_cpu(namelen); | |
156 | if (len != (namelen + sizeof(namelen))) { | |
157 | error_setg(errp, "incorrect option mame length"); | |
158 | return -1; | |
159 | } | |
160 | if (namelen > 255) { | |
161 | error_setg(errp, "export name length too long %d", namelen); | |
162 | return -1; | |
163 | } | |
164 | ||
165 | *name = g_new0(char, namelen + 1); | |
166 | if (read_sync(ioc, *name, namelen) != namelen) { | |
167 | error_setg(errp, "failed to read export name"); | |
168 | g_free(*name); | |
169 | *name = NULL; | |
170 | return -1; | |
171 | } | |
172 | (*name)[namelen] = '\0'; | |
173 | } else { | |
174 | error_setg(errp, "Unexpected reply type %x expected %x", | |
175 | type, NBD_REP_SERVER); | |
176 | return -1; | |
177 | } | |
178 | return 1; | |
179 | } | |
180 | ||
181 | ||
182 | static int nbd_receive_query_exports(QIOChannel *ioc, | |
183 | const char *wantname, | |
184 | Error **errp) | |
185 | { | |
186 | uint64_t magic = cpu_to_be64(NBD_OPTS_MAGIC); | |
187 | uint32_t opt = cpu_to_be32(NBD_OPT_LIST); | |
188 | uint32_t length = 0; | |
189 | bool foundExport = false; | |
190 | ||
191 | TRACE("Querying export list"); | |
192 | if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { | |
193 | error_setg(errp, "Failed to send list option magic"); | |
194 | return -1; | |
195 | } | |
196 | ||
197 | if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) { | |
198 | error_setg(errp, "Failed to send list option number"); | |
199 | return -1; | |
200 | } | |
201 | ||
202 | if (write_sync(ioc, &length, sizeof(length)) != sizeof(length)) { | |
203 | error_setg(errp, "Failed to send list option length"); | |
204 | return -1; | |
205 | } | |
206 | ||
207 | TRACE("Reading available export names"); | |
208 | while (1) { | |
209 | char *name = NULL; | |
210 | int ret = nbd_receive_list(ioc, &name, errp); | |
211 | ||
212 | if (ret < 0) { | |
213 | g_free(name); | |
214 | name = NULL; | |
215 | return -1; | |
216 | } | |
217 | if (ret == 0) { | |
218 | /* Server doesn't support export listing, so | |
219 | * we will just assume an export with our | |
220 | * wanted name exists */ | |
221 | foundExport = true; | |
222 | break; | |
223 | } | |
224 | if (name == NULL) { | |
225 | TRACE("End of export name list"); | |
226 | break; | |
227 | } | |
228 | if (g_str_equal(name, wantname)) { | |
229 | foundExport = true; | |
230 | TRACE("Found desired export name '%s'", name); | |
231 | } else { | |
232 | TRACE("Ignored export name '%s'", name); | |
233 | } | |
234 | g_free(name); | |
235 | } | |
236 | ||
237 | if (!foundExport) { | |
238 | error_setg(errp, "No export with name '%s' available", wantname); | |
239 | return -1; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
1c778ef7 | 245 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, |
798bfe00 FZ |
246 | off_t *size, Error **errp) |
247 | { | |
248 | char buf[256]; | |
249 | uint64_t magic, s; | |
798bfe00 FZ |
250 | int rc; |
251 | ||
252 | TRACE("Receiving negotiation."); | |
253 | ||
254 | rc = -EINVAL; | |
255 | ||
1c778ef7 | 256 | if (read_sync(ioc, buf, 8) != 8) { |
798bfe00 FZ |
257 | error_setg(errp, "Failed to read data"); |
258 | goto fail; | |
259 | } | |
260 | ||
261 | buf[8] = '\0'; | |
262 | if (strlen(buf) == 0) { | |
263 | error_setg(errp, "Server connection closed unexpectedly"); | |
264 | goto fail; | |
265 | } | |
266 | ||
267 | TRACE("Magic is %c%c%c%c%c%c%c%c", | |
268 | qemu_isprint(buf[0]) ? buf[0] : '.', | |
269 | qemu_isprint(buf[1]) ? buf[1] : '.', | |
270 | qemu_isprint(buf[2]) ? buf[2] : '.', | |
271 | qemu_isprint(buf[3]) ? buf[3] : '.', | |
272 | qemu_isprint(buf[4]) ? buf[4] : '.', | |
273 | qemu_isprint(buf[5]) ? buf[5] : '.', | |
274 | qemu_isprint(buf[6]) ? buf[6] : '.', | |
275 | qemu_isprint(buf[7]) ? buf[7] : '.'); | |
276 | ||
277 | if (memcmp(buf, "NBDMAGIC", 8) != 0) { | |
278 | error_setg(errp, "Invalid magic received"); | |
279 | goto fail; | |
280 | } | |
281 | ||
1c778ef7 | 282 | if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
283 | error_setg(errp, "Failed to read magic"); |
284 | goto fail; | |
285 | } | |
286 | magic = be64_to_cpu(magic); | |
287 | TRACE("Magic is 0x%" PRIx64, magic); | |
288 | ||
f72d705f | 289 | if (magic == NBD_OPTS_MAGIC) { |
e2a9d9a3 | 290 | uint32_t clientflags = 0; |
798bfe00 FZ |
291 | uint32_t opt; |
292 | uint32_t namesize; | |
e2a9d9a3 DB |
293 | uint16_t globalflags; |
294 | uint16_t exportflags; | |
9344e5f5 | 295 | bool fixedNewStyle = false; |
798bfe00 | 296 | |
e2a9d9a3 DB |
297 | if (read_sync(ioc, &globalflags, sizeof(globalflags)) != |
298 | sizeof(globalflags)) { | |
798bfe00 FZ |
299 | error_setg(errp, "Failed to read server flags"); |
300 | goto fail; | |
301 | } | |
9344e5f5 DB |
302 | globalflags = be16_to_cpu(globalflags); |
303 | *flags = globalflags << 16; | |
304 | TRACE("Global flags are %x", globalflags); | |
e2a9d9a3 | 305 | if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) { |
9344e5f5 | 306 | fixedNewStyle = true; |
e2a9d9a3 DB |
307 | TRACE("Server supports fixed new style"); |
308 | clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; | |
309 | } | |
310 | /* client requested flags */ | |
9344e5f5 | 311 | clientflags = cpu_to_be32(clientflags); |
e2a9d9a3 DB |
312 | if (write_sync(ioc, &clientflags, sizeof(clientflags)) != |
313 | sizeof(clientflags)) { | |
314 | error_setg(errp, "Failed to send clientflags field"); | |
798bfe00 FZ |
315 | goto fail; |
316 | } | |
f72d705f | 317 | if (!name) { |
69b49502 DB |
318 | TRACE("Using default NBD export name \"\""); |
319 | name = ""; | |
f72d705f | 320 | } |
9344e5f5 DB |
321 | if (fixedNewStyle) { |
322 | /* Check our desired export is present in the | |
323 | * server export list. Since NBD_OPT_EXPORT_NAME | |
324 | * cannot return an error message, running this | |
325 | * query gives us good error reporting if the | |
326 | * server required TLS | |
327 | */ | |
328 | if (nbd_receive_query_exports(ioc, name, errp) < 0) { | |
329 | goto fail; | |
330 | } | |
331 | } | |
332 | /* write the export name */ | |
798bfe00 | 333 | magic = cpu_to_be64(magic); |
1c778ef7 | 334 | if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
798bfe00 FZ |
335 | error_setg(errp, "Failed to send export name magic"); |
336 | goto fail; | |
337 | } | |
338 | opt = cpu_to_be32(NBD_OPT_EXPORT_NAME); | |
1c778ef7 | 339 | if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) { |
798bfe00 FZ |
340 | error_setg(errp, "Failed to send export name option number"); |
341 | goto fail; | |
342 | } | |
343 | namesize = cpu_to_be32(strlen(name)); | |
1c778ef7 | 344 | if (write_sync(ioc, &namesize, sizeof(namesize)) != |
798bfe00 FZ |
345 | sizeof(namesize)) { |
346 | error_setg(errp, "Failed to send export name length"); | |
347 | goto fail; | |
348 | } | |
1c778ef7 | 349 | if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) { |
798bfe00 FZ |
350 | error_setg(errp, "Failed to send export name"); |
351 | goto fail; | |
352 | } | |
f72d705f DB |
353 | |
354 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
355 | error_setg(errp, "Failed to read export length"); | |
798bfe00 FZ |
356 | goto fail; |
357 | } | |
f72d705f DB |
358 | *size = be64_to_cpu(s); |
359 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 360 | |
e2a9d9a3 DB |
361 | if (read_sync(ioc, &exportflags, sizeof(exportflags)) != |
362 | sizeof(exportflags)) { | |
f72d705f DB |
363 | error_setg(errp, "Failed to read export flags"); |
364 | goto fail; | |
365 | } | |
9344e5f5 DB |
366 | exportflags = be16_to_cpu(exportflags); |
367 | *flags |= exportflags; | |
368 | TRACE("Export flags are %x", exportflags); | |
f72d705f DB |
369 | } else if (magic == NBD_CLIENT_MAGIC) { |
370 | if (name) { | |
371 | error_setg(errp, "Server does not support export names"); | |
372 | goto fail; | |
373 | } | |
374 | ||
375 | if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { | |
376 | error_setg(errp, "Failed to read export length"); | |
377 | goto fail; | |
378 | } | |
379 | *size = be64_to_cpu(s); | |
380 | TRACE("Size is %" PRIu64, *size); | |
798bfe00 | 381 | |
1c778ef7 | 382 | if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { |
798bfe00 FZ |
383 | error_setg(errp, "Failed to read export flags"); |
384 | goto fail; | |
385 | } | |
386 | *flags = be32_to_cpup(flags); | |
387 | } else { | |
f72d705f DB |
388 | error_setg(errp, "Bad magic received"); |
389 | goto fail; | |
798bfe00 | 390 | } |
f72d705f | 391 | |
1c778ef7 | 392 | if (read_sync(ioc, &buf, 124) != 124) { |
798bfe00 FZ |
393 | error_setg(errp, "Failed to read reserved block"); |
394 | goto fail; | |
395 | } | |
396 | rc = 0; | |
397 | ||
398 | fail: | |
399 | return rc; | |
400 | } | |
401 | ||
402 | #ifdef __linux__ | |
1c778ef7 | 403 | int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size) |
798bfe00 FZ |
404 | { |
405 | TRACE("Setting NBD socket"); | |
406 | ||
1c778ef7 | 407 | if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) { |
798bfe00 FZ |
408 | int serrno = errno; |
409 | LOG("Failed to set NBD socket"); | |
410 | return -serrno; | |
411 | } | |
412 | ||
413 | TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE); | |
414 | ||
415 | if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) { | |
416 | int serrno = errno; | |
417 | LOG("Failed setting NBD block size"); | |
418 | return -serrno; | |
419 | } | |
420 | ||
421 | TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE)); | |
422 | ||
423 | if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) { | |
424 | int serrno = errno; | |
425 | LOG("Failed setting size (in blocks)"); | |
426 | return -serrno; | |
427 | } | |
428 | ||
429 | if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) { | |
430 | if (errno == ENOTTY) { | |
431 | int read_only = (flags & NBD_FLAG_READ_ONLY) != 0; | |
432 | TRACE("Setting readonly attribute"); | |
433 | ||
434 | if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { | |
435 | int serrno = errno; | |
436 | LOG("Failed setting read-only attribute"); | |
437 | return -serrno; | |
438 | } | |
439 | } else { | |
440 | int serrno = errno; | |
441 | LOG("Failed setting flags"); | |
442 | return -serrno; | |
443 | } | |
444 | } | |
445 | ||
446 | TRACE("Negotiation ended"); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | int nbd_client(int fd) | |
452 | { | |
453 | int ret; | |
454 | int serrno; | |
455 | ||
456 | TRACE("Doing NBD loop"); | |
457 | ||
458 | ret = ioctl(fd, NBD_DO_IT); | |
459 | if (ret < 0 && errno == EPIPE) { | |
460 | /* NBD_DO_IT normally returns EPIPE when someone has disconnected | |
461 | * the socket via NBD_DISCONNECT. We do not want to return 1 in | |
462 | * that case. | |
463 | */ | |
464 | ret = 0; | |
465 | } | |
466 | serrno = errno; | |
467 | ||
468 | TRACE("NBD loop returned %d: %s", ret, strerror(serrno)); | |
469 | ||
470 | TRACE("Clearing NBD queue"); | |
471 | ioctl(fd, NBD_CLEAR_QUE); | |
472 | ||
473 | TRACE("Clearing NBD socket"); | |
474 | ioctl(fd, NBD_CLEAR_SOCK); | |
475 | ||
476 | errno = serrno; | |
477 | return ret; | |
478 | } | |
479 | #else | |
1c778ef7 | 480 | int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size) |
798bfe00 FZ |
481 | { |
482 | return -ENOTSUP; | |
483 | } | |
484 | ||
485 | int nbd_client(int fd) | |
486 | { | |
487 | return -ENOTSUP; | |
488 | } | |
489 | #endif | |
490 | ||
1c778ef7 | 491 | ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request) |
798bfe00 FZ |
492 | { |
493 | uint8_t buf[NBD_REQUEST_SIZE]; | |
494 | ssize_t ret; | |
495 | ||
496 | cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC); | |
497 | cpu_to_be32w((uint32_t*)(buf + 4), request->type); | |
498 | cpu_to_be64w((uint64_t*)(buf + 8), request->handle); | |
499 | cpu_to_be64w((uint64_t*)(buf + 16), request->from); | |
500 | cpu_to_be32w((uint32_t*)(buf + 24), request->len); | |
501 | ||
502 | TRACE("Sending request to client: " | |
503 | "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}", | |
504 | request->from, request->len, request->handle, request->type); | |
505 | ||
1c778ef7 | 506 | ret = write_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
507 | if (ret < 0) { |
508 | return ret; | |
509 | } | |
510 | ||
511 | if (ret != sizeof(buf)) { | |
512 | LOG("writing to socket failed"); | |
513 | return -EINVAL; | |
514 | } | |
515 | return 0; | |
516 | } | |
517 | ||
1c778ef7 | 518 | ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply) |
798bfe00 FZ |
519 | { |
520 | uint8_t buf[NBD_REPLY_SIZE]; | |
521 | uint32_t magic; | |
522 | ssize_t ret; | |
523 | ||
1c778ef7 | 524 | ret = read_sync(ioc, buf, sizeof(buf)); |
798bfe00 FZ |
525 | if (ret < 0) { |
526 | return ret; | |
527 | } | |
528 | ||
529 | if (ret != sizeof(buf)) { | |
530 | LOG("read failed"); | |
531 | return -EINVAL; | |
532 | } | |
533 | ||
534 | /* Reply | |
535 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
536 | [ 4 .. 7] error (0 == no error) | |
537 | [ 7 .. 15] handle | |
538 | */ | |
539 | ||
540 | magic = be32_to_cpup((uint32_t*)buf); | |
541 | reply->error = be32_to_cpup((uint32_t*)(buf + 4)); | |
542 | reply->handle = be64_to_cpup((uint64_t*)(buf + 8)); | |
543 | ||
544 | reply->error = nbd_errno_to_system_errno(reply->error); | |
545 | ||
546 | TRACE("Got reply: " | |
547 | "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }", | |
548 | magic, reply->error, reply->handle); | |
549 | ||
550 | if (magic != NBD_REPLY_MAGIC) { | |
551 | LOG("invalid magic (got 0x%x)", magic); | |
552 | return -EINVAL; | |
553 | } | |
554 | return 0; | |
555 | } | |
556 |