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