]>
Commit | Line | Data |
---|---|---|
75818250 | 1 | /* |
b626b51a | 2 | * Copyright (C) 2016 Red Hat, Inc. |
7a5ca864 FB |
3 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
4 | * | |
798bfe00 | 5 | * Network Block Device Server Side |
7a5ca864 FB |
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 | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
75818250 | 18 | */ |
7a5ca864 | 19 | |
d38ea87a | 20 | #include "qemu/osdep.h" |
da34e65c | 21 | #include "qapi/error.h" |
798bfe00 | 22 | #include "nbd-internal.h" |
ca441480 PB |
23 | |
24 | static int system_errno_to_nbd_errno(int err) | |
25 | { | |
26 | switch (err) { | |
27 | case 0: | |
28 | return NBD_SUCCESS; | |
29 | case EPERM: | |
c0301fcc | 30 | case EROFS: |
ca441480 PB |
31 | return NBD_EPERM; |
32 | case EIO: | |
33 | return NBD_EIO; | |
34 | case ENOMEM: | |
35 | return NBD_ENOMEM; | |
36 | #ifdef EDQUOT | |
37 | case EDQUOT: | |
38 | #endif | |
39 | case EFBIG: | |
40 | case ENOSPC: | |
41 | return NBD_ENOSPC; | |
b6f5d3b5 EB |
42 | case ESHUTDOWN: |
43 | return NBD_ESHUTDOWN; | |
ca441480 PB |
44 | case EINVAL: |
45 | default: | |
46 | return NBD_EINVAL; | |
47 | } | |
48 | } | |
49 | ||
9a304d29 PB |
50 | /* Definitions for opaque data types */ |
51 | ||
315f78ab | 52 | typedef struct NBDRequestData NBDRequestData; |
9a304d29 | 53 | |
315f78ab EB |
54 | struct NBDRequestData { |
55 | QSIMPLEQ_ENTRY(NBDRequestData) entry; | |
9a304d29 PB |
56 | NBDClient *client; |
57 | uint8_t *data; | |
29b6c3b3 | 58 | bool complete; |
9a304d29 PB |
59 | }; |
60 | ||
61 | struct NBDExport { | |
2c8d9f06 | 62 | int refcount; |
0ddf08db PB |
63 | void (*close)(NBDExport *exp); |
64 | ||
aadf99a7 | 65 | BlockBackend *blk; |
ee0a19ec | 66 | char *name; |
b1a75b33 | 67 | char *description; |
9a304d29 PB |
68 | off_t dev_offset; |
69 | off_t size; | |
7423f417 | 70 | uint16_t nbdflags; |
4b9441f6 | 71 | QTAILQ_HEAD(, NBDClient) clients; |
ee0a19ec | 72 | QTAILQ_ENTRY(NBDExport) next; |
958c717d HR |
73 | |
74 | AioContext *ctx; | |
741cc431 | 75 | |
cd7fca95 | 76 | BlockBackend *eject_notifier_blk; |
741cc431 | 77 | Notifier eject_notifier; |
9a304d29 PB |
78 | }; |
79 | ||
ee0a19ec PB |
80 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); |
81 | ||
9a304d29 PB |
82 | struct NBDClient { |
83 | int refcount; | |
84 | void (*close)(NBDClient *client); | |
85 | ||
c203c59a | 86 | bool no_zeroes; |
9a304d29 | 87 | NBDExport *exp; |
f95910fe DB |
88 | QCryptoTLSCreds *tlscreds; |
89 | char *tlsaclname; | |
1c778ef7 DB |
90 | QIOChannelSocket *sioc; /* The underlying data channel */ |
91 | QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */ | |
9a304d29 PB |
92 | |
93 | Coroutine *recv_coroutine; | |
94 | ||
95 | CoMutex send_lock; | |
96 | Coroutine *send_coroutine; | |
97 | ||
4b9441f6 | 98 | QTAILQ_ENTRY(NBDClient) next; |
9a304d29 | 99 | int nb_requests; |
ff2b68aa | 100 | bool closing; |
9a304d29 PB |
101 | }; |
102 | ||
7a5ca864 FB |
103 | /* That's all folks */ |
104 | ||
ff82911c | 105 | static void nbd_client_receive_next_request(NBDClient *client); |
958c717d | 106 | |
1c778ef7 DB |
107 | static gboolean nbd_negotiate_continue(QIOChannel *ioc, |
108 | GIOCondition condition, | |
109 | void *opaque) | |
1a6245a5 | 110 | { |
0b8b8753 | 111 | qemu_coroutine_enter(opaque); |
1c778ef7 | 112 | return TRUE; |
1a6245a5 FZ |
113 | } |
114 | ||
1c778ef7 | 115 | static ssize_t nbd_negotiate_read(QIOChannel *ioc, void *buffer, size_t size) |
1a6245a5 FZ |
116 | { |
117 | ssize_t ret; | |
1c778ef7 | 118 | guint watch; |
1a6245a5 FZ |
119 | |
120 | assert(qemu_in_coroutine()); | |
121 | /* Negotiation are always in main loop. */ | |
1c778ef7 DB |
122 | watch = qio_channel_add_watch(ioc, |
123 | G_IO_IN, | |
124 | nbd_negotiate_continue, | |
125 | qemu_coroutine_self(), | |
126 | NULL); | |
127 | ret = read_sync(ioc, buffer, size); | |
128 | g_source_remove(watch); | |
1a6245a5 FZ |
129 | return ret; |
130 | ||
131 | } | |
132 | ||
b1a75b33 EB |
133 | static ssize_t nbd_negotiate_write(QIOChannel *ioc, const void *buffer, |
134 | size_t size) | |
1a6245a5 FZ |
135 | { |
136 | ssize_t ret; | |
1c778ef7 | 137 | guint watch; |
1a6245a5 FZ |
138 | |
139 | assert(qemu_in_coroutine()); | |
140 | /* Negotiation are always in main loop. */ | |
1c778ef7 DB |
141 | watch = qio_channel_add_watch(ioc, |
142 | G_IO_OUT, | |
143 | nbd_negotiate_continue, | |
144 | qemu_coroutine_self(), | |
145 | NULL); | |
146 | ret = write_sync(ioc, buffer, size); | |
147 | g_source_remove(watch); | |
1a6245a5 FZ |
148 | return ret; |
149 | } | |
150 | ||
1c778ef7 | 151 | static ssize_t nbd_negotiate_drop_sync(QIOChannel *ioc, size_t size) |
0379f474 HR |
152 | { |
153 | ssize_t ret, dropped = size; | |
154 | uint8_t *buffer = g_malloc(MIN(65536, size)); | |
155 | ||
156 | while (size > 0) { | |
1c778ef7 | 157 | ret = nbd_negotiate_read(ioc, buffer, MIN(65536, size)); |
0379f474 HR |
158 | if (ret < 0) { |
159 | g_free(buffer); | |
160 | return ret; | |
161 | } | |
162 | ||
163 | assert(ret <= size); | |
164 | size -= ret; | |
165 | } | |
166 | ||
167 | g_free(buffer); | |
168 | return dropped; | |
169 | } | |
170 | ||
6b8c01e7 | 171 | /* Basic flow for negotiation |
7a5ca864 FB |
172 | |
173 | Server Client | |
7a5ca864 | 174 | Negotiate |
6b8c01e7 PB |
175 | |
176 | or | |
177 | ||
178 | Server Client | |
179 | Negotiate #1 | |
180 | Option | |
181 | Negotiate #2 | |
182 | ||
183 | ---- | |
184 | ||
185 | followed by | |
186 | ||
187 | Server Client | |
7a5ca864 FB |
188 | Request |
189 | Response | |
190 | Request | |
191 | Response | |
192 | ... | |
193 | ... | |
194 | Request (type == 2) | |
6b8c01e7 | 195 | |
7a5ca864 FB |
196 | */ |
197 | ||
526e5c65 EB |
198 | /* Send a reply header, including length, but no payload. |
199 | * Return -errno on error, 0 on success. */ | |
200 | static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type, | |
201 | uint32_t opt, uint32_t len) | |
6b8c01e7 | 202 | { |
6b8c01e7 | 203 | uint64_t magic; |
6b8c01e7 | 204 | |
526e5c65 EB |
205 | TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32, |
206 | type, opt, len); | |
f95910fe | 207 | |
f5076b5a | 208 | magic = cpu_to_be64(NBD_REP_MAGIC); |
1c778ef7 | 209 | if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) != sizeof(magic)) { |
f5076b5a HB |
210 | LOG("write failed (rep magic)"); |
211 | return -EINVAL; | |
6b8c01e7 | 212 | } |
f5076b5a | 213 | opt = cpu_to_be32(opt); |
1c778ef7 | 214 | if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) != sizeof(opt)) { |
f5076b5a HB |
215 | LOG("write failed (rep opt)"); |
216 | return -EINVAL; | |
6b8c01e7 | 217 | } |
f5076b5a | 218 | type = cpu_to_be32(type); |
1c778ef7 | 219 | if (nbd_negotiate_write(ioc, &type, sizeof(type)) != sizeof(type)) { |
f5076b5a HB |
220 | LOG("write failed (rep type)"); |
221 | return -EINVAL; | |
6b8c01e7 | 222 | } |
526e5c65 | 223 | len = cpu_to_be32(len); |
1c778ef7 | 224 | if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) { |
f5076b5a HB |
225 | LOG("write failed (rep data length)"); |
226 | return -EINVAL; | |
6b8c01e7 | 227 | } |
f5076b5a HB |
228 | return 0; |
229 | } | |
6b8c01e7 | 230 | |
526e5c65 EB |
231 | /* Send a reply header with default 0 length. |
232 | * Return -errno on error, 0 on success. */ | |
233 | static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt) | |
234 | { | |
235 | return nbd_negotiate_send_rep_len(ioc, type, opt, 0); | |
236 | } | |
237 | ||
36683283 EB |
238 | /* Send an error reply. |
239 | * Return -errno on error, 0 on success. */ | |
240 | static int GCC_FMT_ATTR(4, 5) | |
241 | nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type, | |
242 | uint32_t opt, const char *fmt, ...) | |
243 | { | |
244 | va_list va; | |
245 | char *msg; | |
246 | int ret; | |
247 | size_t len; | |
248 | ||
249 | va_start(va, fmt); | |
250 | msg = g_strdup_vprintf(fmt, va); | |
251 | va_end(va); | |
252 | len = strlen(msg); | |
253 | assert(len < 4096); | |
254 | TRACE("sending error message \"%s\"", msg); | |
255 | ret = nbd_negotiate_send_rep_len(ioc, type, opt, len); | |
256 | if (ret < 0) { | |
257 | goto out; | |
258 | } | |
259 | if (nbd_negotiate_write(ioc, msg, len) != len) { | |
260 | LOG("write failed (error message)"); | |
261 | ret = -EIO; | |
262 | } else { | |
263 | ret = 0; | |
264 | } | |
265 | out: | |
266 | g_free(msg); | |
267 | return ret; | |
268 | } | |
269 | ||
526e5c65 EB |
270 | /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. |
271 | * Return -errno on error, 0 on success. */ | |
1c778ef7 | 272 | static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp) |
32d7d2e0 | 273 | { |
b1a75b33 | 274 | size_t name_len, desc_len; |
526e5c65 | 275 | uint32_t len; |
b1a75b33 EB |
276 | const char *name = exp->name ? exp->name : ""; |
277 | const char *desc = exp->description ? exp->description : ""; | |
526e5c65 | 278 | int rc; |
32d7d2e0 | 279 | |
b1a75b33 EB |
280 | TRACE("Advertising export name '%s' description '%s'", name, desc); |
281 | name_len = strlen(name); | |
282 | desc_len = strlen(desc); | |
526e5c65 EB |
283 | len = name_len + desc_len + sizeof(len); |
284 | rc = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len); | |
285 | if (rc < 0) { | |
286 | return rc; | |
32d7d2e0 | 287 | } |
526e5c65 | 288 | |
32d7d2e0 | 289 | len = cpu_to_be32(name_len); |
1c778ef7 | 290 | if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) { |
b1a75b33 EB |
291 | LOG("write failed (name length)"); |
292 | return -EINVAL; | |
293 | } | |
294 | if (nbd_negotiate_write(ioc, name, name_len) != name_len) { | |
295 | LOG("write failed (name buffer)"); | |
32d7d2e0 HB |
296 | return -EINVAL; |
297 | } | |
b1a75b33 EB |
298 | if (nbd_negotiate_write(ioc, desc, desc_len) != desc_len) { |
299 | LOG("write failed (description buffer)"); | |
32d7d2e0 HB |
300 | return -EINVAL; |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
526e5c65 EB |
305 | /* Process the NBD_OPT_LIST command, with a potential series of replies. |
306 | * Return -errno on error, 0 on success. */ | |
1a6245a5 | 307 | static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length) |
32d7d2e0 | 308 | { |
32d7d2e0 HB |
309 | NBDExport *exp; |
310 | ||
32d7d2e0 | 311 | if (length) { |
1c778ef7 | 312 | if (nbd_negotiate_drop_sync(client->ioc, length) != length) { |
0379f474 HR |
313 | return -EIO; |
314 | } | |
36683283 EB |
315 | return nbd_negotiate_send_rep_err(client->ioc, |
316 | NBD_REP_ERR_INVALID, NBD_OPT_LIST, | |
317 | "OPT_LIST should not have length"); | |
32d7d2e0 HB |
318 | } |
319 | ||
320 | /* For each export, send a NBD_REP_SERVER reply. */ | |
321 | QTAILQ_FOREACH(exp, &exports, next) { | |
1c778ef7 | 322 | if (nbd_negotiate_send_rep_list(client->ioc, exp)) { |
32d7d2e0 HB |
323 | return -EINVAL; |
324 | } | |
325 | } | |
326 | /* Finish with a NBD_REP_ACK. */ | |
1c778ef7 | 327 | return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST); |
32d7d2e0 HB |
328 | } |
329 | ||
1a6245a5 | 330 | static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length) |
f5076b5a | 331 | { |
1c778ef7 | 332 | int rc = -EINVAL; |
943cec86 | 333 | char name[NBD_MAX_NAME_SIZE + 1]; |
6b8c01e7 | 334 | |
f5076b5a HB |
335 | /* Client sends: |
336 | [20 .. xx] export name (length bytes) | |
337 | */ | |
6b8c01e7 | 338 | TRACE("Checking length"); |
943cec86 | 339 | if (length >= sizeof(name)) { |
6b8c01e7 PB |
340 | LOG("Bad length received"); |
341 | goto fail; | |
342 | } | |
1c778ef7 | 343 | if (nbd_negotiate_read(client->ioc, name, length) != length) { |
6b8c01e7 PB |
344 | LOG("read failed"); |
345 | goto fail; | |
346 | } | |
347 | name[length] = '\0'; | |
348 | ||
9344e5f5 DB |
349 | TRACE("Client requested export '%s'", name); |
350 | ||
6b8c01e7 PB |
351 | client->exp = nbd_export_find(name); |
352 | if (!client->exp) { | |
353 | LOG("export not found"); | |
354 | goto fail; | |
355 | } | |
356 | ||
357 | QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); | |
358 | nbd_export_get(client->exp); | |
6b8c01e7 PB |
359 | rc = 0; |
360 | fail: | |
361 | return rc; | |
362 | } | |
363 | ||
36683283 EB |
364 | /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the |
365 | * new channel for all further (now-encrypted) communication. */ | |
f95910fe DB |
366 | static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client, |
367 | uint32_t length) | |
368 | { | |
369 | QIOChannel *ioc; | |
370 | QIOChannelTLS *tioc; | |
371 | struct NBDTLSHandshakeData data = { 0 }; | |
372 | ||
373 | TRACE("Setting up TLS"); | |
374 | ioc = client->ioc; | |
375 | if (length) { | |
376 | if (nbd_negotiate_drop_sync(ioc, length) != length) { | |
377 | return NULL; | |
378 | } | |
36683283 EB |
379 | nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS, |
380 | "OPT_STARTTLS should not have length"); | |
f95910fe DB |
381 | return NULL; |
382 | } | |
383 | ||
63d5ef86 EB |
384 | if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, |
385 | NBD_OPT_STARTTLS) < 0) { | |
386 | return NULL; | |
387 | } | |
f95910fe DB |
388 | |
389 | tioc = qio_channel_tls_new_server(ioc, | |
390 | client->tlscreds, | |
391 | client->tlsaclname, | |
392 | NULL); | |
393 | if (!tioc) { | |
394 | return NULL; | |
395 | } | |
396 | ||
0d73f725 | 397 | qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls"); |
f95910fe DB |
398 | TRACE("Starting TLS handshake"); |
399 | data.loop = g_main_loop_new(g_main_context_default(), FALSE); | |
400 | qio_channel_tls_handshake(tioc, | |
401 | nbd_tls_handshake, | |
402 | &data, | |
403 | NULL); | |
404 | ||
405 | if (!data.complete) { | |
406 | g_main_loop_run(data.loop); | |
407 | } | |
408 | g_main_loop_unref(data.loop); | |
409 | if (data.error) { | |
410 | object_unref(OBJECT(tioc)); | |
411 | error_free(data.error); | |
412 | return NULL; | |
413 | } | |
414 | ||
415 | return QIO_CHANNEL(tioc); | |
416 | } | |
417 | ||
418 | ||
526e5c65 EB |
419 | /* Process all NBD_OPT_* client option commands. |
420 | * Return -errno on error, 0 on success. */ | |
1a6245a5 | 421 | static int nbd_negotiate_options(NBDClient *client) |
f5076b5a | 422 | { |
9c122ada | 423 | uint32_t flags; |
26afa868 | 424 | bool fixedNewstyle = false; |
9c122ada HR |
425 | |
426 | /* Client sends: | |
427 | [ 0 .. 3] client flags | |
428 | ||
429 | [ 0 .. 7] NBD_OPTS_MAGIC | |
430 | [ 8 .. 11] NBD option | |
431 | [12 .. 15] Data length | |
432 | ... Rest of request | |
433 | ||
434 | [ 0 .. 7] NBD_OPTS_MAGIC | |
435 | [ 8 .. 11] Second NBD option | |
436 | [12 .. 15] Data length | |
437 | ... Rest of request | |
438 | */ | |
439 | ||
1c778ef7 DB |
440 | if (nbd_negotiate_read(client->ioc, &flags, sizeof(flags)) != |
441 | sizeof(flags)) { | |
9c122ada HR |
442 | LOG("read failed"); |
443 | return -EIO; | |
444 | } | |
445 | TRACE("Checking client flags"); | |
446 | be32_to_cpus(&flags); | |
26afa868 | 447 | if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) { |
2cb34749 | 448 | TRACE("Client supports fixed newstyle handshake"); |
26afa868 DB |
449 | fixedNewstyle = true; |
450 | flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE; | |
451 | } | |
c203c59a EB |
452 | if (flags & NBD_FLAG_C_NO_ZEROES) { |
453 | TRACE("Client supports no zeroes at handshake end"); | |
454 | client->no_zeroes = true; | |
455 | flags &= ~NBD_FLAG_C_NO_ZEROES; | |
456 | } | |
26afa868 | 457 | if (flags != 0) { |
2cb34749 | 458 | TRACE("Unknown client flags 0x%" PRIx32 " received", flags); |
9c122ada HR |
459 | return -EIO; |
460 | } | |
461 | ||
f5076b5a | 462 | while (1) { |
9c122ada | 463 | int ret; |
26afa868 | 464 | uint32_t clientflags, length; |
f5076b5a HB |
465 | uint64_t magic; |
466 | ||
1c778ef7 DB |
467 | if (nbd_negotiate_read(client->ioc, &magic, sizeof(magic)) != |
468 | sizeof(magic)) { | |
f5076b5a HB |
469 | LOG("read failed"); |
470 | return -EINVAL; | |
471 | } | |
472 | TRACE("Checking opts magic"); | |
473 | if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) { | |
474 | LOG("Bad magic received"); | |
475 | return -EINVAL; | |
476 | } | |
477 | ||
26afa868 DB |
478 | if (nbd_negotiate_read(client->ioc, &clientflags, |
479 | sizeof(clientflags)) != sizeof(clientflags)) { | |
f5076b5a HB |
480 | LOG("read failed"); |
481 | return -EINVAL; | |
482 | } | |
26afa868 | 483 | clientflags = be32_to_cpu(clientflags); |
f5076b5a | 484 | |
1c778ef7 DB |
485 | if (nbd_negotiate_read(client->ioc, &length, sizeof(length)) != |
486 | sizeof(length)) { | |
f5076b5a HB |
487 | LOG("read failed"); |
488 | return -EINVAL; | |
489 | } | |
490 | length = be32_to_cpu(length); | |
491 | ||
2cb34749 | 492 | TRACE("Checking option 0x%" PRIx32, clientflags); |
f95910fe DB |
493 | if (client->tlscreds && |
494 | client->ioc == (QIOChannel *)client->sioc) { | |
495 | QIOChannel *tioc; | |
496 | if (!fixedNewstyle) { | |
2cb34749 | 497 | TRACE("Unsupported option 0x%" PRIx32, clientflags); |
f95910fe DB |
498 | return -EINVAL; |
499 | } | |
500 | switch (clientflags) { | |
501 | case NBD_OPT_STARTTLS: | |
502 | tioc = nbd_negotiate_handle_starttls(client, length); | |
503 | if (!tioc) { | |
504 | return -EIO; | |
505 | } | |
506 | object_unref(OBJECT(client->ioc)); | |
507 | client->ioc = QIO_CHANNEL(tioc); | |
508 | break; | |
509 | ||
d1129a8a EB |
510 | case NBD_OPT_EXPORT_NAME: |
511 | /* No way to return an error to client, so drop connection */ | |
512 | TRACE("Option 0x%x not permitted before TLS", clientflags); | |
513 | return -EINVAL; | |
514 | ||
f95910fe | 515 | default: |
d1129a8a EB |
516 | if (nbd_negotiate_drop_sync(client->ioc, length) != length) { |
517 | return -EIO; | |
518 | } | |
36683283 EB |
519 | ret = nbd_negotiate_send_rep_err(client->ioc, |
520 | NBD_REP_ERR_TLS_REQD, | |
521 | clientflags, | |
522 | "Option 0x%" PRIx32 | |
523 | "not permitted before TLS", | |
524 | clientflags); | |
63d5ef86 EB |
525 | if (ret < 0) { |
526 | return ret; | |
527 | } | |
b6f5d3b5 EB |
528 | /* Let the client keep trying, unless they asked to quit */ |
529 | if (clientflags == NBD_OPT_ABORT) { | |
530 | return -EINVAL; | |
531 | } | |
d1129a8a | 532 | break; |
f95910fe DB |
533 | } |
534 | } else if (fixedNewstyle) { | |
26afa868 DB |
535 | switch (clientflags) { |
536 | case NBD_OPT_LIST: | |
537 | ret = nbd_negotiate_handle_list(client, length); | |
538 | if (ret < 0) { | |
539 | return ret; | |
540 | } | |
541 | break; | |
542 | ||
543 | case NBD_OPT_ABORT: | |
b6f5d3b5 EB |
544 | /* NBD spec says we must try to reply before |
545 | * disconnecting, but that we must also tolerate | |
546 | * guests that don't wait for our reply. */ | |
547 | nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, clientflags); | |
26afa868 DB |
548 | return -EINVAL; |
549 | ||
550 | case NBD_OPT_EXPORT_NAME: | |
551 | return nbd_negotiate_handle_export_name(client, length); | |
552 | ||
f95910fe | 553 | case NBD_OPT_STARTTLS: |
d1129a8a EB |
554 | if (nbd_negotiate_drop_sync(client->ioc, length) != length) { |
555 | return -EIO; | |
556 | } | |
f95910fe | 557 | if (client->tlscreds) { |
36683283 EB |
558 | ret = nbd_negotiate_send_rep_err(client->ioc, |
559 | NBD_REP_ERR_INVALID, | |
560 | clientflags, | |
561 | "TLS already enabled"); | |
f95910fe | 562 | } else { |
36683283 EB |
563 | ret = nbd_negotiate_send_rep_err(client->ioc, |
564 | NBD_REP_ERR_POLICY, | |
565 | clientflags, | |
566 | "TLS not configured"); | |
63d5ef86 EB |
567 | } |
568 | if (ret < 0) { | |
569 | return ret; | |
f95910fe | 570 | } |
d1129a8a | 571 | break; |
26afa868 | 572 | default: |
156f6a10 EB |
573 | if (nbd_negotiate_drop_sync(client->ioc, length) != length) { |
574 | return -EIO; | |
575 | } | |
36683283 EB |
576 | ret = nbd_negotiate_send_rep_err(client->ioc, |
577 | NBD_REP_ERR_UNSUP, | |
578 | clientflags, | |
579 | "Unsupported option 0x%" | |
580 | PRIx32, | |
581 | clientflags); | |
63d5ef86 EB |
582 | if (ret < 0) { |
583 | return ret; | |
584 | } | |
156f6a10 | 585 | break; |
26afa868 DB |
586 | } |
587 | } else { | |
588 | /* | |
589 | * If broken new-style we should drop the connection | |
590 | * for anything except NBD_OPT_EXPORT_NAME | |
591 | */ | |
592 | switch (clientflags) { | |
593 | case NBD_OPT_EXPORT_NAME: | |
594 | return nbd_negotiate_handle_export_name(client, length); | |
595 | ||
596 | default: | |
2cb34749 | 597 | TRACE("Unsupported option 0x%" PRIx32, clientflags); |
26afa868 | 598 | return -EINVAL; |
32d7d2e0 | 599 | } |
f5076b5a HB |
600 | } |
601 | } | |
602 | } | |
603 | ||
1a6245a5 FZ |
604 | typedef struct { |
605 | NBDClient *client; | |
606 | Coroutine *co; | |
607 | } NBDClientNewData; | |
608 | ||
609 | static coroutine_fn int nbd_negotiate(NBDClientNewData *data) | |
7a5ca864 | 610 | { |
1a6245a5 | 611 | NBDClient *client = data->client; |
b2e3d87f | 612 | char buf[8 + 8 + 8 + 128]; |
185b4338 | 613 | int rc; |
7423f417 | 614 | const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | |
1f4d6d18 EB |
615 | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA | |
616 | NBD_FLAG_SEND_WRITE_ZEROES); | |
f95910fe | 617 | bool oldStyle; |
c203c59a | 618 | size_t len; |
b2e3d87f | 619 | |
f95910fe | 620 | /* Old style negotiation header without options |
6b8c01e7 PB |
621 | [ 0 .. 7] passwd ("NBDMAGIC") |
622 | [ 8 .. 15] magic (NBD_CLIENT_MAGIC) | |
b2e3d87f | 623 | [16 .. 23] size |
6b8c01e7 | 624 | [24 .. 25] server flags (0) |
5672ee54 | 625 | [26 .. 27] export flags |
6b8c01e7 PB |
626 | [28 .. 151] reserved (0) |
627 | ||
f95910fe | 628 | New style negotiation header with options |
6b8c01e7 PB |
629 | [ 0 .. 7] passwd ("NBDMAGIC") |
630 | [ 8 .. 15] magic (NBD_OPTS_MAGIC) | |
631 | [16 .. 17] server flags (0) | |
f95910fe | 632 | ....options sent.... |
6b8c01e7 PB |
633 | [18 .. 25] size |
634 | [26 .. 27] export flags | |
c203c59a | 635 | [28 .. 151] reserved (0, omit if no_zeroes) |
b2e3d87f NT |
636 | */ |
637 | ||
1c778ef7 | 638 | qio_channel_set_blocking(client->ioc, false, NULL); |
185b4338 PB |
639 | rc = -EINVAL; |
640 | ||
b2e3d87f | 641 | TRACE("Beginning negotiation."); |
8ffaaba0 | 642 | memset(buf, 0, sizeof(buf)); |
b2e3d87f | 643 | memcpy(buf, "NBDMAGIC", 8); |
f95910fe DB |
644 | |
645 | oldStyle = client->exp != NULL && !client->tlscreds; | |
646 | if (oldStyle) { | |
2cb34749 EB |
647 | TRACE("advertising size %" PRIu64 " and flags %x", |
648 | client->exp->size, client->exp->nbdflags | myflags); | |
667ad26f JS |
649 | stq_be_p(buf + 8, NBD_CLIENT_MAGIC); |
650 | stq_be_p(buf + 16, client->exp->size); | |
651 | stw_be_p(buf + 26, client->exp->nbdflags | myflags); | |
6b8c01e7 | 652 | } else { |
667ad26f | 653 | stq_be_p(buf + 8, NBD_OPTS_MAGIC); |
c203c59a | 654 | stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES); |
6b8c01e7 | 655 | } |
b2e3d87f | 656 | |
f95910fe DB |
657 | if (oldStyle) { |
658 | if (client->tlscreds) { | |
659 | TRACE("TLS cannot be enabled with oldstyle protocol"); | |
660 | goto fail; | |
661 | } | |
1c778ef7 | 662 | if (nbd_negotiate_write(client->ioc, buf, sizeof(buf)) != sizeof(buf)) { |
6b8c01e7 PB |
663 | LOG("write failed"); |
664 | goto fail; | |
665 | } | |
666 | } else { | |
1c778ef7 | 667 | if (nbd_negotiate_write(client->ioc, buf, 18) != 18) { |
6b8c01e7 PB |
668 | LOG("write failed"); |
669 | goto fail; | |
670 | } | |
1a6245a5 | 671 | rc = nbd_negotiate_options(client); |
f5076b5a | 672 | if (rc != 0) { |
6b8c01e7 PB |
673 | LOG("option negotiation failed"); |
674 | goto fail; | |
675 | } | |
676 | ||
2cb34749 EB |
677 | TRACE("advertising size %" PRIu64 " and flags %x", |
678 | client->exp->size, client->exp->nbdflags | myflags); | |
667ad26f JS |
679 | stq_be_p(buf + 18, client->exp->size); |
680 | stw_be_p(buf + 26, client->exp->nbdflags | myflags); | |
c203c59a EB |
681 | len = client->no_zeroes ? 10 : sizeof(buf) - 18; |
682 | if (nbd_negotiate_write(client->ioc, buf + 18, len) != len) { | |
6b8c01e7 PB |
683 | LOG("write failed"); |
684 | goto fail; | |
685 | } | |
b2e3d87f NT |
686 | } |
687 | ||
07f35073 | 688 | TRACE("Negotiation succeeded."); |
185b4338 PB |
689 | rc = 0; |
690 | fail: | |
691 | return rc; | |
7a5ca864 FB |
692 | } |
693 | ||
ed2dd912 | 694 | static ssize_t nbd_receive_request(QIOChannel *ioc, NBDRequest *request) |
75818250 | 695 | { |
fa26c26b | 696 | uint8_t buf[NBD_REQUEST_SIZE]; |
b2e3d87f | 697 | uint32_t magic; |
185b4338 | 698 | ssize_t ret; |
b2e3d87f | 699 | |
1c778ef7 | 700 | ret = read_sync(ioc, buf, sizeof(buf)); |
185b4338 PB |
701 | if (ret < 0) { |
702 | return ret; | |
703 | } | |
704 | ||
705 | if (ret != sizeof(buf)) { | |
b2e3d87f | 706 | LOG("read failed"); |
185b4338 | 707 | return -EINVAL; |
b2e3d87f NT |
708 | } |
709 | ||
710 | /* Request | |
711 | [ 0 .. 3] magic (NBD_REQUEST_MAGIC) | |
b626b51a EB |
712 | [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) |
713 | [ 6 .. 7] type (NBD_CMD_READ, ...) | |
b2e3d87f NT |
714 | [ 8 .. 15] handle |
715 | [16 .. 23] from | |
716 | [24 .. 27] len | |
717 | */ | |
718 | ||
773dce3c | 719 | magic = ldl_be_p(buf); |
b626b51a EB |
720 | request->flags = lduw_be_p(buf + 4); |
721 | request->type = lduw_be_p(buf + 6); | |
773dce3c PM |
722 | request->handle = ldq_be_p(buf + 8); |
723 | request->from = ldq_be_p(buf + 16); | |
724 | request->len = ldl_be_p(buf + 24); | |
b2e3d87f | 725 | |
b626b51a EB |
726 | TRACE("Got request: { magic = 0x%" PRIx32 ", .flags = %" PRIx16 |
727 | ", .type = %" PRIx16 ", from = %" PRIu64 ", len = %" PRIu32 " }", | |
728 | magic, request->flags, request->type, request->from, request->len); | |
b2e3d87f NT |
729 | |
730 | if (magic != NBD_REQUEST_MAGIC) { | |
2cb34749 | 731 | LOG("invalid magic (got 0x%" PRIx32 ")", magic); |
185b4338 | 732 | return -EINVAL; |
b2e3d87f NT |
733 | } |
734 | return 0; | |
75818250 TS |
735 | } |
736 | ||
ed2dd912 | 737 | static ssize_t nbd_send_reply(QIOChannel *ioc, NBDReply *reply) |
75818250 | 738 | { |
fa26c26b | 739 | uint8_t buf[NBD_REPLY_SIZE]; |
185b4338 | 740 | ssize_t ret; |
b2e3d87f | 741 | |
ca441480 PB |
742 | reply->error = system_errno_to_nbd_errno(reply->error); |
743 | ||
2cb34749 EB |
744 | TRACE("Sending response to client: { .error = %" PRId32 |
745 | ", handle = %" PRIu64 " }", | |
7548fe31 EB |
746 | reply->error, reply->handle); |
747 | ||
b2e3d87f NT |
748 | /* Reply |
749 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
750 | [ 4 .. 7] error (0 == no error) | |
751 | [ 7 .. 15] handle | |
752 | */ | |
667ad26f JS |
753 | stl_be_p(buf, NBD_REPLY_MAGIC); |
754 | stl_be_p(buf + 4, reply->error); | |
755 | stq_be_p(buf + 8, reply->handle); | |
b2e3d87f | 756 | |
1c778ef7 | 757 | ret = write_sync(ioc, buf, sizeof(buf)); |
185b4338 PB |
758 | if (ret < 0) { |
759 | return ret; | |
760 | } | |
761 | ||
762 | if (ret != sizeof(buf)) { | |
b2e3d87f | 763 | LOG("writing to socket failed"); |
185b4338 | 764 | return -EINVAL; |
b2e3d87f NT |
765 | } |
766 | return 0; | |
75818250 | 767 | } |
7a5ca864 | 768 | |
41996e38 PB |
769 | #define MAX_NBD_REQUESTS 16 |
770 | ||
ce33967a | 771 | void nbd_client_get(NBDClient *client) |
1743b515 PB |
772 | { |
773 | client->refcount++; | |
774 | } | |
775 | ||
ce33967a | 776 | void nbd_client_put(NBDClient *client) |
1743b515 PB |
777 | { |
778 | if (--client->refcount == 0) { | |
ff2b68aa | 779 | /* The last reference should be dropped by client->close, |
f53a829b | 780 | * which is called by client_close. |
ff2b68aa PB |
781 | */ |
782 | assert(client->closing); | |
783 | ||
ff82911c | 784 | qio_channel_detach_aio_context(client->ioc); |
1c778ef7 DB |
785 | object_unref(OBJECT(client->sioc)); |
786 | object_unref(OBJECT(client->ioc)); | |
f95910fe DB |
787 | if (client->tlscreds) { |
788 | object_unref(OBJECT(client->tlscreds)); | |
789 | } | |
790 | g_free(client->tlsaclname); | |
6b8c01e7 PB |
791 | if (client->exp) { |
792 | QTAILQ_REMOVE(&client->exp->clients, client, next); | |
793 | nbd_export_put(client->exp); | |
794 | } | |
1743b515 PB |
795 | g_free(client); |
796 | } | |
797 | } | |
798 | ||
f53a829b | 799 | static void client_close(NBDClient *client) |
1743b515 | 800 | { |
ff2b68aa PB |
801 | if (client->closing) { |
802 | return; | |
803 | } | |
804 | ||
805 | client->closing = true; | |
806 | ||
807 | /* Force requests to finish. They will drop their own references, | |
808 | * then we'll close the socket and free the NBDClient. | |
809 | */ | |
1c778ef7 DB |
810 | qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, |
811 | NULL); | |
ff2b68aa PB |
812 | |
813 | /* Also tell the client, so that they release their reference. */ | |
1743b515 PB |
814 | if (client->close) { |
815 | client->close(client); | |
816 | } | |
1743b515 PB |
817 | } |
818 | ||
315f78ab | 819 | static NBDRequestData *nbd_request_get(NBDClient *client) |
d9a73806 | 820 | { |
315f78ab | 821 | NBDRequestData *req; |
72deddc5 | 822 | |
41996e38 PB |
823 | assert(client->nb_requests <= MAX_NBD_REQUESTS - 1); |
824 | client->nb_requests++; | |
825 | ||
315f78ab | 826 | req = g_new0(NBDRequestData, 1); |
72deddc5 PB |
827 | nbd_client_get(client); |
828 | req->client = client; | |
d9a73806 PB |
829 | return req; |
830 | } | |
831 | ||
315f78ab | 832 | static void nbd_request_put(NBDRequestData *req) |
d9a73806 | 833 | { |
72deddc5 | 834 | NBDClient *client = req->client; |
e1adb27a | 835 | |
2d821488 SH |
836 | if (req->data) { |
837 | qemu_vfree(req->data); | |
838 | } | |
1729404c | 839 | g_free(req); |
e1adb27a | 840 | |
958c717d | 841 | client->nb_requests--; |
ff82911c PB |
842 | nbd_client_receive_next_request(client); |
843 | ||
72deddc5 | 844 | nbd_client_put(client); |
d9a73806 PB |
845 | } |
846 | ||
aadf99a7 | 847 | static void blk_aio_attached(AioContext *ctx, void *opaque) |
f2149281 HR |
848 | { |
849 | NBDExport *exp = opaque; | |
850 | NBDClient *client; | |
851 | ||
852 | TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx); | |
853 | ||
854 | exp->ctx = ctx; | |
855 | ||
856 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
ff82911c PB |
857 | qio_channel_attach_aio_context(client->ioc, ctx); |
858 | if (client->recv_coroutine) { | |
859 | aio_co_schedule(ctx, client->recv_coroutine); | |
860 | } | |
861 | if (client->send_coroutine) { | |
862 | aio_co_schedule(ctx, client->send_coroutine); | |
863 | } | |
f2149281 HR |
864 | } |
865 | } | |
866 | ||
aadf99a7 | 867 | static void blk_aio_detach(void *opaque) |
f2149281 HR |
868 | { |
869 | NBDExport *exp = opaque; | |
870 | NBDClient *client; | |
871 | ||
872 | TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx); | |
873 | ||
874 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
ff82911c | 875 | qio_channel_detach_aio_context(client->ioc); |
f2149281 HR |
876 | } |
877 | ||
878 | exp->ctx = NULL; | |
879 | } | |
880 | ||
741cc431 HR |
881 | static void nbd_eject_notifier(Notifier *n, void *data) |
882 | { | |
883 | NBDExport *exp = container_of(n, NBDExport, eject_notifier); | |
884 | nbd_export_close(exp); | |
885 | } | |
886 | ||
cd7fca95 | 887 | NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size, |
7423f417 | 888 | uint16_t nbdflags, void (*close)(NBDExport *), |
cd7fca95 | 889 | bool writethrough, BlockBackend *on_eject_blk, |
98f44bbe | 890 | Error **errp) |
af49bbbe | 891 | { |
cd7fca95 | 892 | BlockBackend *blk; |
af49bbbe | 893 | NBDExport *exp = g_malloc0(sizeof(NBDExport)); |
cd7fca95 KW |
894 | |
895 | blk = blk_new(); | |
896 | blk_insert_bs(blk, bs); | |
897 | blk_set_enable_write_cache(blk, !writethrough); | |
898 | ||
2c8d9f06 | 899 | exp->refcount = 1; |
4b9441f6 | 900 | QTAILQ_INIT(&exp->clients); |
aadf99a7 | 901 | exp->blk = blk; |
af49bbbe PB |
902 | exp->dev_offset = dev_offset; |
903 | exp->nbdflags = nbdflags; | |
98f44bbe HR |
904 | exp->size = size < 0 ? blk_getlength(blk) : size; |
905 | if (exp->size < 0) { | |
906 | error_setg_errno(errp, -exp->size, | |
907 | "Failed to determine the NBD export's length"); | |
908 | goto fail; | |
909 | } | |
910 | exp->size -= exp->size % BDRV_SECTOR_SIZE; | |
911 | ||
0ddf08db | 912 | exp->close = close; |
aadf99a7 | 913 | exp->ctx = blk_get_aio_context(blk); |
aadf99a7 | 914 | blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp); |
741cc431 | 915 | |
cd7fca95 KW |
916 | if (on_eject_blk) { |
917 | blk_ref(on_eject_blk); | |
918 | exp->eject_notifier_blk = on_eject_blk; | |
919 | exp->eject_notifier.notify = nbd_eject_notifier; | |
920 | blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier); | |
921 | } | |
741cc431 | 922 | |
7ea2d269 AK |
923 | /* |
924 | * NBD exports are used for non-shared storage migration. Make sure | |
04c01a5c | 925 | * that BDRV_O_INACTIVE is cleared and the image is ready for write |
7ea2d269 AK |
926 | * access since the export could be available before migration handover. |
927 | */ | |
e5f3e12e | 928 | aio_context_acquire(exp->ctx); |
aadf99a7 | 929 | blk_invalidate_cache(blk, NULL); |
e5f3e12e | 930 | aio_context_release(exp->ctx); |
af49bbbe | 931 | return exp; |
98f44bbe HR |
932 | |
933 | fail: | |
cd7fca95 | 934 | blk_unref(blk); |
98f44bbe HR |
935 | g_free(exp); |
936 | return NULL; | |
af49bbbe PB |
937 | } |
938 | ||
ee0a19ec PB |
939 | NBDExport *nbd_export_find(const char *name) |
940 | { | |
941 | NBDExport *exp; | |
942 | QTAILQ_FOREACH(exp, &exports, next) { | |
943 | if (strcmp(name, exp->name) == 0) { | |
944 | return exp; | |
945 | } | |
946 | } | |
947 | ||
948 | return NULL; | |
949 | } | |
950 | ||
951 | void nbd_export_set_name(NBDExport *exp, const char *name) | |
952 | { | |
953 | if (exp->name == name) { | |
954 | return; | |
955 | } | |
956 | ||
957 | nbd_export_get(exp); | |
958 | if (exp->name != NULL) { | |
959 | g_free(exp->name); | |
960 | exp->name = NULL; | |
961 | QTAILQ_REMOVE(&exports, exp, next); | |
962 | nbd_export_put(exp); | |
963 | } | |
964 | if (name != NULL) { | |
965 | nbd_export_get(exp); | |
966 | exp->name = g_strdup(name); | |
967 | QTAILQ_INSERT_TAIL(&exports, exp, next); | |
968 | } | |
969 | nbd_export_put(exp); | |
970 | } | |
971 | ||
b1a75b33 EB |
972 | void nbd_export_set_description(NBDExport *exp, const char *description) |
973 | { | |
974 | g_free(exp->description); | |
975 | exp->description = g_strdup(description); | |
976 | } | |
977 | ||
af49bbbe PB |
978 | void nbd_export_close(NBDExport *exp) |
979 | { | |
4b9441f6 | 980 | NBDClient *client, *next; |
2c8d9f06 | 981 | |
4b9441f6 PB |
982 | nbd_export_get(exp); |
983 | QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) { | |
f53a829b | 984 | client_close(client); |
4b9441f6 | 985 | } |
125afda8 | 986 | nbd_export_set_name(exp, NULL); |
b1a75b33 | 987 | nbd_export_set_description(exp, NULL); |
4b9441f6 | 988 | nbd_export_put(exp); |
2c8d9f06 PB |
989 | } |
990 | ||
991 | void nbd_export_get(NBDExport *exp) | |
992 | { | |
993 | assert(exp->refcount > 0); | |
994 | exp->refcount++; | |
995 | } | |
996 | ||
997 | void nbd_export_put(NBDExport *exp) | |
998 | { | |
999 | assert(exp->refcount > 0); | |
1000 | if (exp->refcount == 1) { | |
1001 | nbd_export_close(exp); | |
d9a73806 PB |
1002 | } |
1003 | ||
2c8d9f06 | 1004 | if (--exp->refcount == 0) { |
ee0a19ec | 1005 | assert(exp->name == NULL); |
b1a75b33 | 1006 | assert(exp->description == NULL); |
ee0a19ec | 1007 | |
0ddf08db PB |
1008 | if (exp->close) { |
1009 | exp->close(exp); | |
1010 | } | |
1011 | ||
d6268348 | 1012 | if (exp->blk) { |
cd7fca95 KW |
1013 | if (exp->eject_notifier_blk) { |
1014 | notifier_remove(&exp->eject_notifier); | |
1015 | blk_unref(exp->eject_notifier_blk); | |
1016 | } | |
d6268348 WC |
1017 | blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, |
1018 | blk_aio_detach, exp); | |
1019 | blk_unref(exp->blk); | |
1020 | exp->blk = NULL; | |
1021 | } | |
1022 | ||
2c8d9f06 PB |
1023 | g_free(exp); |
1024 | } | |
af49bbbe PB |
1025 | } |
1026 | ||
e140177d | 1027 | BlockBackend *nbd_export_get_blockdev(NBDExport *exp) |
125afda8 | 1028 | { |
aadf99a7 | 1029 | return exp->blk; |
125afda8 PB |
1030 | } |
1031 | ||
ee0a19ec PB |
1032 | void nbd_export_close_all(void) |
1033 | { | |
1034 | NBDExport *exp, *next; | |
1035 | ||
1036 | QTAILQ_FOREACH_SAFE(exp, &exports, next, next) { | |
1037 | nbd_export_close(exp); | |
ee0a19ec PB |
1038 | } |
1039 | } | |
1040 | ||
ed2dd912 | 1041 | static ssize_t nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, |
94e7340b | 1042 | int len) |
22045592 | 1043 | { |
72deddc5 | 1044 | NBDClient *client = req->client; |
94e7340b | 1045 | ssize_t rc, ret; |
22045592 | 1046 | |
1c778ef7 | 1047 | g_assert(qemu_in_coroutine()); |
262db388 | 1048 | qemu_co_mutex_lock(&client->send_lock); |
262db388 PB |
1049 | client->send_coroutine = qemu_coroutine_self(); |
1050 | ||
22045592 | 1051 | if (!len) { |
1c778ef7 | 1052 | rc = nbd_send_reply(client->ioc, reply); |
22045592 | 1053 | } else { |
1c778ef7 DB |
1054 | qio_channel_set_cork(client->ioc, true); |
1055 | rc = nbd_send_reply(client->ioc, reply); | |
fc19f8a0 | 1056 | if (rc >= 0) { |
1c778ef7 | 1057 | ret = write_sync(client->ioc, req->data, len); |
22045592 | 1058 | if (ret != len) { |
185b4338 | 1059 | rc = -EIO; |
22045592 PB |
1060 | } |
1061 | } | |
1c778ef7 | 1062 | qio_channel_set_cork(client->ioc, false); |
22045592 | 1063 | } |
262db388 PB |
1064 | |
1065 | client->send_coroutine = NULL; | |
262db388 | 1066 | qemu_co_mutex_unlock(&client->send_lock); |
22045592 PB |
1067 | return rc; |
1068 | } | |
1069 | ||
29b6c3b3 EB |
1070 | /* Collect a client request. Return 0 if request looks valid, -EAGAIN |
1071 | * to keep trying the collection, -EIO to drop connection right away, | |
1072 | * and any other negative value to report an error to the client | |
1073 | * (although the caller may still need to disconnect after reporting | |
1074 | * the error). */ | |
315f78ab | 1075 | static ssize_t nbd_co_receive_request(NBDRequestData *req, |
ed2dd912 | 1076 | NBDRequest *request) |
a030b347 | 1077 | { |
72deddc5 | 1078 | NBDClient *client = req->client; |
94e7340b | 1079 | ssize_t rc; |
a030b347 | 1080 | |
1c778ef7 | 1081 | g_assert(qemu_in_coroutine()); |
ff82911c | 1082 | assert(client->recv_coroutine == qemu_coroutine_self()); |
1c778ef7 | 1083 | rc = nbd_receive_request(client->ioc, request); |
7fe7b68b PB |
1084 | if (rc < 0) { |
1085 | if (rc != -EAGAIN) { | |
1086 | rc = -EIO; | |
1087 | } | |
a030b347 PB |
1088 | goto out; |
1089 | } | |
1090 | ||
29b6c3b3 EB |
1091 | TRACE("Decoding type"); |
1092 | ||
b626b51a | 1093 | if (request->type != NBD_CMD_WRITE) { |
29b6c3b3 EB |
1094 | /* No payload, we are ready to read the next request. */ |
1095 | req->complete = true; | |
1096 | } | |
1097 | ||
b626b51a | 1098 | if (request->type == NBD_CMD_DISC) { |
29b6c3b3 EB |
1099 | /* Special case: we're going to disconnect without a reply, |
1100 | * whether or not flags, from, or len are bogus */ | |
1101 | TRACE("Request type is DISCONNECT"); | |
1102 | rc = -EIO; | |
1103 | goto out; | |
1104 | } | |
1105 | ||
1106 | /* Check for sanity in the parameters, part 1. Defer as many | |
1107 | * checks as possible until after reading any NBD_CMD_WRITE | |
1108 | * payload, so we can try and keep the connection alive. */ | |
a030b347 | 1109 | if ((request->from + request->len) < request->from) { |
29b6c3b3 | 1110 | LOG("integer overflow detected, you're probably being attacked"); |
a030b347 PB |
1111 | rc = -EINVAL; |
1112 | goto out; | |
1113 | } | |
1114 | ||
b626b51a | 1115 | if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) { |
eb38c3b6 | 1116 | if (request->len > NBD_MAX_BUFFER_SIZE) { |
2cb34749 | 1117 | LOG("len (%" PRIu32" ) is larger than max len (%u)", |
eb38c3b6 PB |
1118 | request->len, NBD_MAX_BUFFER_SIZE); |
1119 | rc = -EINVAL; | |
1120 | goto out; | |
1121 | } | |
1122 | ||
f1c17521 PB |
1123 | req->data = blk_try_blockalign(client->exp->blk, request->len); |
1124 | if (req->data == NULL) { | |
1125 | rc = -ENOMEM; | |
1126 | goto out; | |
1127 | } | |
2d821488 | 1128 | } |
b626b51a | 1129 | if (request->type == NBD_CMD_WRITE) { |
2cb34749 | 1130 | TRACE("Reading %" PRIu32 " byte(s)", request->len); |
a030b347 | 1131 | |
1c778ef7 | 1132 | if (read_sync(client->ioc, req->data, request->len) != request->len) { |
a030b347 PB |
1133 | LOG("reading from socket failed"); |
1134 | rc = -EIO; | |
1135 | goto out; | |
1136 | } | |
29b6c3b3 | 1137 | req->complete = true; |
a030b347 | 1138 | } |
29b6c3b3 EB |
1139 | |
1140 | /* Sanity checks, part 2. */ | |
1141 | if (request->from + request->len > client->exp->size) { | |
1142 | LOG("operation past EOF; From: %" PRIu64 ", Len: %" PRIu32 | |
1143 | ", Size: %" PRIu64, request->from, request->len, | |
1144 | (uint64_t)client->exp->size); | |
b626b51a | 1145 | rc = request->type == NBD_CMD_WRITE ? -ENOSPC : -EINVAL; |
29b6c3b3 EB |
1146 | goto out; |
1147 | } | |
1f4d6d18 | 1148 | if (request->flags & ~(NBD_CMD_FLAG_FUA | NBD_CMD_FLAG_NO_HOLE)) { |
b626b51a | 1149 | LOG("unsupported flags (got 0x%x)", request->flags); |
5bee0f47 EB |
1150 | rc = -EINVAL; |
1151 | goto out; | |
ab7c548e | 1152 | } |
1f4d6d18 EB |
1153 | if (request->type != NBD_CMD_WRITE_ZEROES && |
1154 | (request->flags & NBD_CMD_FLAG_NO_HOLE)) { | |
1155 | LOG("unexpected flags (got 0x%x)", request->flags); | |
1156 | rc = -EINVAL; | |
1157 | goto out; | |
1158 | } | |
29b6c3b3 | 1159 | |
a030b347 PB |
1160 | rc = 0; |
1161 | ||
1162 | out: | |
262db388 | 1163 | client->recv_coroutine = NULL; |
ff82911c | 1164 | nbd_client_receive_next_request(client); |
958c717d | 1165 | |
a030b347 PB |
1166 | return rc; |
1167 | } | |
1168 | ||
ff82911c PB |
1169 | /* Owns a reference to the NBDClient passed as opaque. */ |
1170 | static coroutine_fn void nbd_trip(void *opaque) | |
75818250 | 1171 | { |
262db388 | 1172 | NBDClient *client = opaque; |
1743b515 | 1173 | NBDExport *exp = client->exp; |
315f78ab | 1174 | NBDRequestData *req; |
ff82911c | 1175 | NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */ |
ed2dd912 | 1176 | NBDReply reply; |
94e7340b | 1177 | ssize_t ret; |
a0c30369 | 1178 | int flags; |
b2e3d87f NT |
1179 | |
1180 | TRACE("Reading request."); | |
ff2b68aa | 1181 | if (client->closing) { |
ff82911c | 1182 | nbd_client_put(client); |
ff2b68aa PB |
1183 | return; |
1184 | } | |
b2e3d87f | 1185 | |
ff2b68aa | 1186 | req = nbd_request_get(client); |
262db388 | 1187 | ret = nbd_co_receive_request(req, &request); |
7fe7b68b PB |
1188 | if (ret == -EAGAIN) { |
1189 | goto done; | |
1190 | } | |
a030b347 | 1191 | if (ret == -EIO) { |
d9a73806 | 1192 | goto out; |
a030b347 | 1193 | } |
b2e3d87f | 1194 | |
fae69416 PB |
1195 | reply.handle = request.handle; |
1196 | reply.error = 0; | |
1197 | ||
a030b347 PB |
1198 | if (ret < 0) { |
1199 | reply.error = -ret; | |
1200 | goto error_reply; | |
b2e3d87f | 1201 | } |
b2e3d87f | 1202 | |
d6268348 WC |
1203 | if (client->closing) { |
1204 | /* | |
1205 | * The client may be closed when we are blocked in | |
1206 | * nbd_co_receive_request() | |
1207 | */ | |
1208 | goto done; | |
1209 | } | |
1210 | ||
b626b51a | 1211 | switch (request.type) { |
b2e3d87f NT |
1212 | case NBD_CMD_READ: |
1213 | TRACE("Request type is READ"); | |
1214 | ||
b626b51a EB |
1215 | /* XXX: NBD Protocol only documents use of FUA with WRITE */ |
1216 | if (request.flags & NBD_CMD_FLAG_FUA) { | |
aadf99a7 | 1217 | ret = blk_co_flush(exp->blk); |
e25ceb76 PB |
1218 | if (ret < 0) { |
1219 | LOG("flush failed"); | |
1220 | reply.error = -ret; | |
1221 | goto error_reply; | |
1222 | } | |
1223 | } | |
1224 | ||
df7b97ff EB |
1225 | ret = blk_pread(exp->blk, request.from + exp->dev_offset, |
1226 | req->data, request.len); | |
adcf6302 | 1227 | if (ret < 0) { |
b2e3d87f | 1228 | LOG("reading from file failed"); |
adcf6302 | 1229 | reply.error = -ret; |
fae69416 | 1230 | goto error_reply; |
b2e3d87f | 1231 | } |
b2e3d87f | 1232 | |
2cb34749 | 1233 | TRACE("Read %" PRIu32" byte(s)", request.len); |
262db388 | 1234 | if (nbd_co_send_reply(req, &reply, request.len) < 0) |
d9a73806 | 1235 | goto out; |
b2e3d87f NT |
1236 | break; |
1237 | case NBD_CMD_WRITE: | |
1238 | TRACE("Request type is WRITE"); | |
1239 | ||
af49bbbe | 1240 | if (exp->nbdflags & NBD_FLAG_READ_ONLY) { |
b2e3d87f | 1241 | TRACE("Server is read-only, return error"); |
fae69416 PB |
1242 | reply.error = EROFS; |
1243 | goto error_reply; | |
1244 | } | |
1245 | ||
1246 | TRACE("Writing to device"); | |
1247 | ||
a0c30369 | 1248 | flags = 0; |
b626b51a | 1249 | if (request.flags & NBD_CMD_FLAG_FUA) { |
a0c30369 EB |
1250 | flags |= BDRV_REQ_FUA; |
1251 | } | |
df7b97ff | 1252 | ret = blk_pwrite(exp->blk, request.from + exp->dev_offset, |
a0c30369 | 1253 | req->data, request.len, flags); |
fae69416 PB |
1254 | if (ret < 0) { |
1255 | LOG("writing to file failed"); | |
1256 | reply.error = -ret; | |
1257 | goto error_reply; | |
1258 | } | |
b2e3d87f | 1259 | |
1f4d6d18 EB |
1260 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
1261 | goto out; | |
1262 | } | |
1263 | break; | |
1264 | ||
1265 | case NBD_CMD_WRITE_ZEROES: | |
1266 | TRACE("Request type is WRITE_ZEROES"); | |
1267 | ||
1268 | if (exp->nbdflags & NBD_FLAG_READ_ONLY) { | |
1269 | TRACE("Server is read-only, return error"); | |
1270 | reply.error = EROFS; | |
1271 | goto error_reply; | |
1272 | } | |
1273 | ||
1274 | TRACE("Writing to device"); | |
1275 | ||
1276 | flags = 0; | |
1277 | if (request.flags & NBD_CMD_FLAG_FUA) { | |
1278 | flags |= BDRV_REQ_FUA; | |
1279 | } | |
1280 | if (!(request.flags & NBD_CMD_FLAG_NO_HOLE)) { | |
1281 | flags |= BDRV_REQ_MAY_UNMAP; | |
1282 | } | |
1283 | ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset, | |
1284 | request.len, flags); | |
1285 | if (ret < 0) { | |
1286 | LOG("writing to file failed"); | |
1287 | reply.error = -ret; | |
1288 | goto error_reply; | |
1289 | } | |
1290 | ||
fc19f8a0 | 1291 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1292 | goto out; |
fc19f8a0 | 1293 | } |
b2e3d87f | 1294 | break; |
29b6c3b3 | 1295 | |
b2e3d87f | 1296 | case NBD_CMD_DISC: |
29b6c3b3 EB |
1297 | /* unreachable, thanks to special case in nbd_co_receive_request() */ |
1298 | abort(); | |
1299 | ||
1486d04a PB |
1300 | case NBD_CMD_FLUSH: |
1301 | TRACE("Request type is FLUSH"); | |
1302 | ||
aadf99a7 | 1303 | ret = blk_co_flush(exp->blk); |
1486d04a PB |
1304 | if (ret < 0) { |
1305 | LOG("flush failed"); | |
1306 | reply.error = -ret; | |
1307 | } | |
fc19f8a0 | 1308 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1309 | goto out; |
fc19f8a0 | 1310 | } |
7a706633 PB |
1311 | break; |
1312 | case NBD_CMD_TRIM: | |
1313 | TRACE("Request type is TRIM"); | |
1c6c4bb7 EB |
1314 | ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset, |
1315 | request.len); | |
1316 | if (ret < 0) { | |
1317 | LOG("discard failed"); | |
1318 | reply.error = -ret; | |
7a706633 | 1319 | } |
fc19f8a0 | 1320 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1321 | goto out; |
fc19f8a0 | 1322 | } |
1486d04a | 1323 | break; |
b2e3d87f | 1324 | default: |
2cb34749 | 1325 | LOG("invalid request type (%" PRIu32 ") received", request.type); |
8b2f0abf | 1326 | reply.error = EINVAL; |
fae69416 | 1327 | error_reply: |
29b6c3b3 EB |
1328 | /* We must disconnect after NBD_CMD_WRITE if we did not |
1329 | * read the payload. | |
1330 | */ | |
1331 | if (nbd_co_send_reply(req, &reply, 0) < 0 || !req->complete) { | |
d9a73806 | 1332 | goto out; |
fc19f8a0 | 1333 | } |
fae69416 | 1334 | break; |
b2e3d87f NT |
1335 | } |
1336 | ||
1337 | TRACE("Request/Reply complete"); | |
1338 | ||
7fe7b68b | 1339 | done: |
262db388 | 1340 | nbd_request_put(req); |
ff82911c | 1341 | nbd_client_put(client); |
262db388 PB |
1342 | return; |
1343 | ||
d9a73806 | 1344 | out: |
72deddc5 | 1345 | nbd_request_put(req); |
f53a829b | 1346 | client_close(client); |
ff82911c | 1347 | nbd_client_put(client); |
7a5ca864 | 1348 | } |
af49bbbe | 1349 | |
ff82911c | 1350 | static void nbd_client_receive_next_request(NBDClient *client) |
958c717d | 1351 | { |
ff82911c PB |
1352 | if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) { |
1353 | nbd_client_get(client); | |
1354 | client->recv_coroutine = qemu_coroutine_create(nbd_trip, client); | |
1355 | aio_co_schedule(client->exp->ctx, client->recv_coroutine); | |
958c717d HR |
1356 | } |
1357 | } | |
1358 | ||
1a6245a5 FZ |
1359 | static coroutine_fn void nbd_co_client_start(void *opaque) |
1360 | { | |
1361 | NBDClientNewData *data = opaque; | |
1362 | NBDClient *client = data->client; | |
1363 | NBDExport *exp = client->exp; | |
1364 | ||
1365 | if (exp) { | |
1366 | nbd_export_get(exp); | |
1367 | } | |
1368 | if (nbd_negotiate(data)) { | |
d3780c2d | 1369 | client_close(client); |
1a6245a5 FZ |
1370 | goto out; |
1371 | } | |
1372 | qemu_co_mutex_init(&client->send_lock); | |
1a6245a5 FZ |
1373 | |
1374 | if (exp) { | |
1375 | QTAILQ_INSERT_TAIL(&exp->clients, client, next); | |
1376 | } | |
ff82911c PB |
1377 | |
1378 | nbd_client_receive_next_request(client); | |
1379 | ||
1a6245a5 FZ |
1380 | out: |
1381 | g_free(data); | |
1382 | } | |
1383 | ||
1c778ef7 DB |
1384 | void nbd_client_new(NBDExport *exp, |
1385 | QIOChannelSocket *sioc, | |
f95910fe DB |
1386 | QCryptoTLSCreds *tlscreds, |
1387 | const char *tlsaclname, | |
1c778ef7 | 1388 | void (*close_fn)(NBDClient *)) |
af49bbbe | 1389 | { |
1743b515 | 1390 | NBDClient *client; |
1a6245a5 FZ |
1391 | NBDClientNewData *data = g_new(NBDClientNewData, 1); |
1392 | ||
1743b515 PB |
1393 | client = g_malloc0(sizeof(NBDClient)); |
1394 | client->refcount = 1; | |
1395 | client->exp = exp; | |
f95910fe DB |
1396 | client->tlscreds = tlscreds; |
1397 | if (tlscreds) { | |
1398 | object_ref(OBJECT(client->tlscreds)); | |
1399 | } | |
1400 | client->tlsaclname = g_strdup(tlsaclname); | |
1c778ef7 DB |
1401 | client->sioc = sioc; |
1402 | object_ref(OBJECT(client->sioc)); | |
1403 | client->ioc = QIO_CHANNEL(sioc); | |
1404 | object_ref(OBJECT(client->ioc)); | |
ee7d7aab | 1405 | client->close = close_fn; |
2c8d9f06 | 1406 | |
1a6245a5 | 1407 | data->client = client; |
0b8b8753 PB |
1408 | data->co = qemu_coroutine_create(nbd_co_client_start, data); |
1409 | qemu_coroutine_enter(data->co); | |
af49bbbe | 1410 | } |