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