]>
Commit | Line | Data |
---|---|---|
75818250 | 1 | /* |
7a5ca864 FB |
2 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
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 | |
f95910fe DB |
199 | TRACE("Reply opt=%x type=%x", type, opt); |
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 | ||
69b49502 | 229 | TRACE("Advertizing 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 DB |
394 | if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) { |
395 | TRACE("Support supports fixed newstyle handshake"); | |
396 | fixedNewstyle = true; | |
397 | flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE; | |
398 | } | |
399 | if (flags != 0) { | |
400 | TRACE("Unknown client flags 0x%x 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 | ||
26afa868 | 434 | TRACE("Checking option 0x%x", clientflags); |
f95910fe DB |
435 | if (client->tlscreds && |
436 | client->ioc == (QIOChannel *)client->sioc) { | |
437 | QIOChannel *tioc; | |
438 | if (!fixedNewstyle) { | |
439 | TRACE("Unsupported option 0x%x", clientflags); | |
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 | ||
452 | default: | |
453 | TRACE("Option 0x%x not permitted before TLS", clientflags); | |
454 | nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_TLS_REQD, | |
455 | clientflags); | |
456 | return -EINVAL; | |
457 | } | |
458 | } else if (fixedNewstyle) { | |
26afa868 DB |
459 | switch (clientflags) { |
460 | case NBD_OPT_LIST: | |
461 | ret = nbd_negotiate_handle_list(client, length); | |
462 | if (ret < 0) { | |
463 | return ret; | |
464 | } | |
465 | break; | |
466 | ||
467 | case NBD_OPT_ABORT: | |
468 | return -EINVAL; | |
469 | ||
470 | case NBD_OPT_EXPORT_NAME: | |
471 | return nbd_negotiate_handle_export_name(client, length); | |
472 | ||
f95910fe DB |
473 | case NBD_OPT_STARTTLS: |
474 | if (client->tlscreds) { | |
475 | TRACE("TLS already enabled"); | |
476 | nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_INVALID, | |
477 | clientflags); | |
478 | } else { | |
479 | TRACE("TLS not configured"); | |
480 | nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_POLICY, | |
481 | clientflags); | |
482 | } | |
483 | return -EINVAL; | |
26afa868 DB |
484 | default: |
485 | TRACE("Unsupported option 0x%x", clientflags); | |
486 | nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_UNSUP, | |
487 | clientflags); | |
488 | return -EINVAL; | |
489 | } | |
490 | } else { | |
491 | /* | |
492 | * If broken new-style we should drop the connection | |
493 | * for anything except NBD_OPT_EXPORT_NAME | |
494 | */ | |
495 | switch (clientflags) { | |
496 | case NBD_OPT_EXPORT_NAME: | |
497 | return nbd_negotiate_handle_export_name(client, length); | |
498 | ||
499 | default: | |
500 | TRACE("Unsupported option 0x%x", clientflags); | |
501 | return -EINVAL; | |
32d7d2e0 | 502 | } |
f5076b5a HB |
503 | } |
504 | } | |
505 | } | |
506 | ||
1a6245a5 FZ |
507 | typedef struct { |
508 | NBDClient *client; | |
509 | Coroutine *co; | |
510 | } NBDClientNewData; | |
511 | ||
512 | static coroutine_fn int nbd_negotiate(NBDClientNewData *data) | |
7a5ca864 | 513 | { |
1a6245a5 | 514 | NBDClient *client = data->client; |
b2e3d87f | 515 | char buf[8 + 8 + 8 + 128]; |
185b4338 | 516 | int rc; |
6b8c01e7 PB |
517 | const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | |
518 | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); | |
f95910fe | 519 | bool oldStyle; |
b2e3d87f | 520 | |
f95910fe | 521 | /* Old style negotiation header without options |
6b8c01e7 PB |
522 | [ 0 .. 7] passwd ("NBDMAGIC") |
523 | [ 8 .. 15] magic (NBD_CLIENT_MAGIC) | |
b2e3d87f | 524 | [16 .. 23] size |
6b8c01e7 | 525 | [24 .. 25] server flags (0) |
5672ee54 | 526 | [26 .. 27] export flags |
6b8c01e7 PB |
527 | [28 .. 151] reserved (0) |
528 | ||
f95910fe | 529 | New style negotiation header with options |
6b8c01e7 PB |
530 | [ 0 .. 7] passwd ("NBDMAGIC") |
531 | [ 8 .. 15] magic (NBD_OPTS_MAGIC) | |
532 | [16 .. 17] server flags (0) | |
f95910fe | 533 | ....options sent.... |
6b8c01e7 PB |
534 | [18 .. 25] size |
535 | [26 .. 27] export flags | |
536 | [28 .. 151] reserved (0) | |
b2e3d87f NT |
537 | */ |
538 | ||
1c778ef7 | 539 | qio_channel_set_blocking(client->ioc, false, NULL); |
185b4338 PB |
540 | rc = -EINVAL; |
541 | ||
b2e3d87f | 542 | TRACE("Beginning negotiation."); |
8ffaaba0 | 543 | memset(buf, 0, sizeof(buf)); |
b2e3d87f | 544 | memcpy(buf, "NBDMAGIC", 8); |
f95910fe DB |
545 | |
546 | oldStyle = client->exp != NULL && !client->tlscreds; | |
547 | if (oldStyle) { | |
6b8c01e7 | 548 | assert ((client->exp->nbdflags & ~65535) == 0); |
667ad26f JS |
549 | stq_be_p(buf + 8, NBD_CLIENT_MAGIC); |
550 | stq_be_p(buf + 16, client->exp->size); | |
551 | stw_be_p(buf + 26, client->exp->nbdflags | myflags); | |
6b8c01e7 | 552 | } else { |
667ad26f JS |
553 | stq_be_p(buf + 8, NBD_OPTS_MAGIC); |
554 | stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE); | |
6b8c01e7 | 555 | } |
b2e3d87f | 556 | |
f95910fe DB |
557 | if (oldStyle) { |
558 | if (client->tlscreds) { | |
559 | TRACE("TLS cannot be enabled with oldstyle protocol"); | |
560 | goto fail; | |
561 | } | |
1c778ef7 | 562 | if (nbd_negotiate_write(client->ioc, buf, sizeof(buf)) != sizeof(buf)) { |
6b8c01e7 PB |
563 | LOG("write failed"); |
564 | goto fail; | |
565 | } | |
566 | } else { | |
1c778ef7 | 567 | if (nbd_negotiate_write(client->ioc, buf, 18) != 18) { |
6b8c01e7 PB |
568 | LOG("write failed"); |
569 | goto fail; | |
570 | } | |
1a6245a5 | 571 | rc = nbd_negotiate_options(client); |
f5076b5a | 572 | if (rc != 0) { |
6b8c01e7 PB |
573 | LOG("option negotiation failed"); |
574 | goto fail; | |
575 | } | |
576 | ||
577 | assert ((client->exp->nbdflags & ~65535) == 0); | |
667ad26f JS |
578 | stq_be_p(buf + 18, client->exp->size); |
579 | stw_be_p(buf + 26, client->exp->nbdflags | myflags); | |
1c778ef7 DB |
580 | if (nbd_negotiate_write(client->ioc, buf + 18, sizeof(buf) - 18) != |
581 | sizeof(buf) - 18) { | |
6b8c01e7 PB |
582 | LOG("write failed"); |
583 | goto fail; | |
584 | } | |
b2e3d87f NT |
585 | } |
586 | ||
07f35073 | 587 | TRACE("Negotiation succeeded."); |
185b4338 PB |
588 | rc = 0; |
589 | fail: | |
590 | return rc; | |
7a5ca864 FB |
591 | } |
592 | ||
b90fb4b8 | 593 | #ifdef __linux__ |
7a5ca864 FB |
594 | |
595 | int nbd_disconnect(int fd) | |
596 | { | |
b2e3d87f NT |
597 | ioctl(fd, NBD_CLEAR_QUE); |
598 | ioctl(fd, NBD_DISCONNECT); | |
599 | ioctl(fd, NBD_CLEAR_SOCK); | |
600 | return 0; | |
7a5ca864 FB |
601 | } |
602 | ||
03ff3ca3 | 603 | #else |
03ff3ca3 AL |
604 | |
605 | int nbd_disconnect(int fd) | |
606 | { | |
185b4338 | 607 | return -ENOTSUP; |
03ff3ca3 | 608 | } |
03ff3ca3 | 609 | #endif |
7a5ca864 | 610 | |
1c778ef7 | 611 | static ssize_t nbd_receive_request(QIOChannel *ioc, struct nbd_request *request) |
75818250 | 612 | { |
fa26c26b | 613 | uint8_t buf[NBD_REQUEST_SIZE]; |
b2e3d87f | 614 | uint32_t magic; |
185b4338 | 615 | ssize_t ret; |
b2e3d87f | 616 | |
1c778ef7 | 617 | ret = read_sync(ioc, buf, sizeof(buf)); |
185b4338 PB |
618 | if (ret < 0) { |
619 | return ret; | |
620 | } | |
621 | ||
622 | if (ret != sizeof(buf)) { | |
b2e3d87f | 623 | LOG("read failed"); |
185b4338 | 624 | return -EINVAL; |
b2e3d87f NT |
625 | } |
626 | ||
627 | /* Request | |
628 | [ 0 .. 3] magic (NBD_REQUEST_MAGIC) | |
629 | [ 4 .. 7] type (0 == READ, 1 == WRITE) | |
630 | [ 8 .. 15] handle | |
631 | [16 .. 23] from | |
632 | [24 .. 27] len | |
633 | */ | |
634 | ||
635 | magic = be32_to_cpup((uint32_t*)buf); | |
636 | request->type = be32_to_cpup((uint32_t*)(buf + 4)); | |
637 | request->handle = be64_to_cpup((uint64_t*)(buf + 8)); | |
638 | request->from = be64_to_cpup((uint64_t*)(buf + 16)); | |
639 | request->len = be32_to_cpup((uint32_t*)(buf + 24)); | |
640 | ||
641 | TRACE("Got request: " | |
642 | "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }", | |
643 | magic, request->type, request->from, request->len); | |
644 | ||
645 | if (magic != NBD_REQUEST_MAGIC) { | |
646 | LOG("invalid magic (got 0x%x)", magic); | |
185b4338 | 647 | return -EINVAL; |
b2e3d87f NT |
648 | } |
649 | return 0; | |
75818250 TS |
650 | } |
651 | ||
1c778ef7 | 652 | static ssize_t nbd_send_reply(QIOChannel *ioc, struct nbd_reply *reply) |
75818250 | 653 | { |
fa26c26b | 654 | uint8_t buf[NBD_REPLY_SIZE]; |
185b4338 | 655 | ssize_t ret; |
b2e3d87f | 656 | |
ca441480 PB |
657 | reply->error = system_errno_to_nbd_errno(reply->error); |
658 | ||
b2e3d87f NT |
659 | /* Reply |
660 | [ 0 .. 3] magic (NBD_REPLY_MAGIC) | |
661 | [ 4 .. 7] error (0 == no error) | |
662 | [ 7 .. 15] handle | |
663 | */ | |
667ad26f JS |
664 | stl_be_p(buf, NBD_REPLY_MAGIC); |
665 | stl_be_p(buf + 4, reply->error); | |
666 | stq_be_p(buf + 8, reply->handle); | |
b2e3d87f NT |
667 | |
668 | TRACE("Sending response to client"); | |
669 | ||
1c778ef7 | 670 | ret = write_sync(ioc, buf, sizeof(buf)); |
185b4338 PB |
671 | if (ret < 0) { |
672 | return ret; | |
673 | } | |
674 | ||
675 | if (ret != sizeof(buf)) { | |
b2e3d87f | 676 | LOG("writing to socket failed"); |
185b4338 | 677 | return -EINVAL; |
b2e3d87f NT |
678 | } |
679 | return 0; | |
75818250 | 680 | } |
7a5ca864 | 681 | |
41996e38 PB |
682 | #define MAX_NBD_REQUESTS 16 |
683 | ||
ce33967a | 684 | void nbd_client_get(NBDClient *client) |
1743b515 PB |
685 | { |
686 | client->refcount++; | |
687 | } | |
688 | ||
ce33967a | 689 | void nbd_client_put(NBDClient *client) |
1743b515 PB |
690 | { |
691 | if (--client->refcount == 0) { | |
ff2b68aa | 692 | /* The last reference should be dropped by client->close, |
f53a829b | 693 | * which is called by client_close. |
ff2b68aa PB |
694 | */ |
695 | assert(client->closing); | |
696 | ||
958c717d | 697 | nbd_unset_handlers(client); |
1c778ef7 DB |
698 | object_unref(OBJECT(client->sioc)); |
699 | object_unref(OBJECT(client->ioc)); | |
f95910fe DB |
700 | if (client->tlscreds) { |
701 | object_unref(OBJECT(client->tlscreds)); | |
702 | } | |
703 | g_free(client->tlsaclname); | |
6b8c01e7 PB |
704 | if (client->exp) { |
705 | QTAILQ_REMOVE(&client->exp->clients, client, next); | |
706 | nbd_export_put(client->exp); | |
707 | } | |
1743b515 PB |
708 | g_free(client); |
709 | } | |
710 | } | |
711 | ||
f53a829b | 712 | static void client_close(NBDClient *client) |
1743b515 | 713 | { |
ff2b68aa PB |
714 | if (client->closing) { |
715 | return; | |
716 | } | |
717 | ||
718 | client->closing = true; | |
719 | ||
720 | /* Force requests to finish. They will drop their own references, | |
721 | * then we'll close the socket and free the NBDClient. | |
722 | */ | |
1c778ef7 DB |
723 | qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, |
724 | NULL); | |
ff2b68aa PB |
725 | |
726 | /* Also tell the client, so that they release their reference. */ | |
1743b515 PB |
727 | if (client->close) { |
728 | client->close(client); | |
729 | } | |
1743b515 PB |
730 | } |
731 | ||
72deddc5 | 732 | static NBDRequest *nbd_request_get(NBDClient *client) |
d9a73806 PB |
733 | { |
734 | NBDRequest *req; | |
72deddc5 | 735 | |
41996e38 PB |
736 | assert(client->nb_requests <= MAX_NBD_REQUESTS - 1); |
737 | client->nb_requests++; | |
958c717d | 738 | nbd_update_can_read(client); |
41996e38 | 739 | |
1729404c | 740 | req = g_new0(NBDRequest, 1); |
72deddc5 PB |
741 | nbd_client_get(client); |
742 | req->client = client; | |
d9a73806 PB |
743 | return req; |
744 | } | |
745 | ||
72deddc5 | 746 | static void nbd_request_put(NBDRequest *req) |
d9a73806 | 747 | { |
72deddc5 | 748 | NBDClient *client = req->client; |
e1adb27a | 749 | |
2d821488 SH |
750 | if (req->data) { |
751 | qemu_vfree(req->data); | |
752 | } | |
1729404c | 753 | g_free(req); |
e1adb27a | 754 | |
958c717d HR |
755 | client->nb_requests--; |
756 | nbd_update_can_read(client); | |
72deddc5 | 757 | nbd_client_put(client); |
d9a73806 PB |
758 | } |
759 | ||
aadf99a7 | 760 | static void blk_aio_attached(AioContext *ctx, void *opaque) |
f2149281 HR |
761 | { |
762 | NBDExport *exp = opaque; | |
763 | NBDClient *client; | |
764 | ||
765 | TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx); | |
766 | ||
767 | exp->ctx = ctx; | |
768 | ||
769 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
770 | nbd_set_handlers(client); | |
771 | } | |
772 | } | |
773 | ||
aadf99a7 | 774 | static void blk_aio_detach(void *opaque) |
f2149281 HR |
775 | { |
776 | NBDExport *exp = opaque; | |
777 | NBDClient *client; | |
778 | ||
779 | TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx); | |
780 | ||
781 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
782 | nbd_unset_handlers(client); | |
783 | } | |
784 | ||
785 | exp->ctx = NULL; | |
786 | } | |
787 | ||
741cc431 HR |
788 | static void nbd_eject_notifier(Notifier *n, void *data) |
789 | { | |
790 | NBDExport *exp = container_of(n, NBDExport, eject_notifier); | |
791 | nbd_export_close(exp); | |
792 | } | |
793 | ||
e140177d | 794 | NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size, |
98f44bbe HR |
795 | uint32_t nbdflags, void (*close)(NBDExport *), |
796 | Error **errp) | |
af49bbbe PB |
797 | { |
798 | NBDExport *exp = g_malloc0(sizeof(NBDExport)); | |
2c8d9f06 | 799 | exp->refcount = 1; |
4b9441f6 | 800 | QTAILQ_INIT(&exp->clients); |
aadf99a7 | 801 | exp->blk = blk; |
af49bbbe PB |
802 | exp->dev_offset = dev_offset; |
803 | exp->nbdflags = nbdflags; | |
98f44bbe HR |
804 | exp->size = size < 0 ? blk_getlength(blk) : size; |
805 | if (exp->size < 0) { | |
806 | error_setg_errno(errp, -exp->size, | |
807 | "Failed to determine the NBD export's length"); | |
808 | goto fail; | |
809 | } | |
810 | exp->size -= exp->size % BDRV_SECTOR_SIZE; | |
811 | ||
0ddf08db | 812 | exp->close = close; |
aadf99a7 HR |
813 | exp->ctx = blk_get_aio_context(blk); |
814 | blk_ref(blk); | |
815 | blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp); | |
741cc431 HR |
816 | |
817 | exp->eject_notifier.notify = nbd_eject_notifier; | |
818 | blk_add_remove_bs_notifier(blk, &exp->eject_notifier); | |
819 | ||
7ea2d269 AK |
820 | /* |
821 | * NBD exports are used for non-shared storage migration. Make sure | |
04c01a5c | 822 | * that BDRV_O_INACTIVE is cleared and the image is ready for write |
7ea2d269 AK |
823 | * access since the export could be available before migration handover. |
824 | */ | |
e5f3e12e | 825 | aio_context_acquire(exp->ctx); |
aadf99a7 | 826 | blk_invalidate_cache(blk, NULL); |
e5f3e12e | 827 | aio_context_release(exp->ctx); |
af49bbbe | 828 | return exp; |
98f44bbe HR |
829 | |
830 | fail: | |
831 | g_free(exp); | |
832 | return NULL; | |
af49bbbe PB |
833 | } |
834 | ||
ee0a19ec PB |
835 | NBDExport *nbd_export_find(const char *name) |
836 | { | |
837 | NBDExport *exp; | |
838 | QTAILQ_FOREACH(exp, &exports, next) { | |
839 | if (strcmp(name, exp->name) == 0) { | |
840 | return exp; | |
841 | } | |
842 | } | |
843 | ||
844 | return NULL; | |
845 | } | |
846 | ||
847 | void nbd_export_set_name(NBDExport *exp, const char *name) | |
848 | { | |
849 | if (exp->name == name) { | |
850 | return; | |
851 | } | |
852 | ||
853 | nbd_export_get(exp); | |
854 | if (exp->name != NULL) { | |
855 | g_free(exp->name); | |
856 | exp->name = NULL; | |
857 | QTAILQ_REMOVE(&exports, exp, next); | |
858 | nbd_export_put(exp); | |
859 | } | |
860 | if (name != NULL) { | |
861 | nbd_export_get(exp); | |
862 | exp->name = g_strdup(name); | |
863 | QTAILQ_INSERT_TAIL(&exports, exp, next); | |
864 | } | |
865 | nbd_export_put(exp); | |
866 | } | |
867 | ||
af49bbbe PB |
868 | void nbd_export_close(NBDExport *exp) |
869 | { | |
4b9441f6 | 870 | NBDClient *client, *next; |
2c8d9f06 | 871 | |
4b9441f6 PB |
872 | nbd_export_get(exp); |
873 | QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) { | |
f53a829b | 874 | client_close(client); |
4b9441f6 | 875 | } |
125afda8 | 876 | nbd_export_set_name(exp, NULL); |
4b9441f6 | 877 | nbd_export_put(exp); |
2c8d9f06 PB |
878 | } |
879 | ||
880 | void nbd_export_get(NBDExport *exp) | |
881 | { | |
882 | assert(exp->refcount > 0); | |
883 | exp->refcount++; | |
884 | } | |
885 | ||
886 | void nbd_export_put(NBDExport *exp) | |
887 | { | |
888 | assert(exp->refcount > 0); | |
889 | if (exp->refcount == 1) { | |
890 | nbd_export_close(exp); | |
d9a73806 PB |
891 | } |
892 | ||
2c8d9f06 | 893 | if (--exp->refcount == 0) { |
ee0a19ec PB |
894 | assert(exp->name == NULL); |
895 | ||
0ddf08db PB |
896 | if (exp->close) { |
897 | exp->close(exp); | |
898 | } | |
899 | ||
d6268348 | 900 | if (exp->blk) { |
741cc431 | 901 | notifier_remove(&exp->eject_notifier); |
d6268348 WC |
902 | blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, |
903 | blk_aio_detach, exp); | |
904 | blk_unref(exp->blk); | |
905 | exp->blk = NULL; | |
906 | } | |
907 | ||
2c8d9f06 PB |
908 | g_free(exp); |
909 | } | |
af49bbbe PB |
910 | } |
911 | ||
e140177d | 912 | BlockBackend *nbd_export_get_blockdev(NBDExport *exp) |
125afda8 | 913 | { |
aadf99a7 | 914 | return exp->blk; |
125afda8 PB |
915 | } |
916 | ||
ee0a19ec PB |
917 | void nbd_export_close_all(void) |
918 | { | |
919 | NBDExport *exp, *next; | |
920 | ||
921 | QTAILQ_FOREACH_SAFE(exp, &exports, next, next) { | |
922 | nbd_export_close(exp); | |
ee0a19ec PB |
923 | } |
924 | } | |
925 | ||
94e7340b PB |
926 | static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply, |
927 | int len) | |
22045592 | 928 | { |
72deddc5 | 929 | NBDClient *client = req->client; |
94e7340b | 930 | ssize_t rc, ret; |
22045592 | 931 | |
1c778ef7 | 932 | g_assert(qemu_in_coroutine()); |
262db388 | 933 | qemu_co_mutex_lock(&client->send_lock); |
262db388 | 934 | client->send_coroutine = qemu_coroutine_self(); |
958c717d | 935 | nbd_set_handlers(client); |
262db388 | 936 | |
22045592 | 937 | if (!len) { |
1c778ef7 | 938 | rc = nbd_send_reply(client->ioc, reply); |
22045592 | 939 | } else { |
1c778ef7 DB |
940 | qio_channel_set_cork(client->ioc, true); |
941 | rc = nbd_send_reply(client->ioc, reply); | |
fc19f8a0 | 942 | if (rc >= 0) { |
1c778ef7 | 943 | ret = write_sync(client->ioc, req->data, len); |
22045592 | 944 | if (ret != len) { |
185b4338 | 945 | rc = -EIO; |
22045592 PB |
946 | } |
947 | } | |
1c778ef7 | 948 | qio_channel_set_cork(client->ioc, false); |
22045592 | 949 | } |
262db388 PB |
950 | |
951 | client->send_coroutine = NULL; | |
958c717d | 952 | nbd_set_handlers(client); |
262db388 | 953 | qemu_co_mutex_unlock(&client->send_lock); |
22045592 PB |
954 | return rc; |
955 | } | |
956 | ||
94e7340b | 957 | static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request) |
a030b347 | 958 | { |
72deddc5 | 959 | NBDClient *client = req->client; |
2d821488 | 960 | uint32_t command; |
94e7340b | 961 | ssize_t rc; |
a030b347 | 962 | |
1c778ef7 | 963 | g_assert(qemu_in_coroutine()); |
262db388 | 964 | client->recv_coroutine = qemu_coroutine_self(); |
958c717d HR |
965 | nbd_update_can_read(client); |
966 | ||
1c778ef7 | 967 | rc = nbd_receive_request(client->ioc, request); |
7fe7b68b PB |
968 | if (rc < 0) { |
969 | if (rc != -EAGAIN) { | |
970 | rc = -EIO; | |
971 | } | |
a030b347 PB |
972 | goto out; |
973 | } | |
974 | ||
a030b347 PB |
975 | if ((request->from + request->len) < request->from) { |
976 | LOG("integer overflow detected! " | |
977 | "you're probably being attacked"); | |
978 | rc = -EINVAL; | |
979 | goto out; | |
980 | } | |
981 | ||
982 | TRACE("Decoding type"); | |
983 | ||
2d821488 SH |
984 | command = request->type & NBD_CMD_MASK_COMMAND; |
985 | if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) { | |
eb38c3b6 PB |
986 | if (request->len > NBD_MAX_BUFFER_SIZE) { |
987 | LOG("len (%u) is larger than max len (%u)", | |
988 | request->len, NBD_MAX_BUFFER_SIZE); | |
989 | rc = -EINVAL; | |
990 | goto out; | |
991 | } | |
992 | ||
f1c17521 PB |
993 | req->data = blk_try_blockalign(client->exp->blk, request->len); |
994 | if (req->data == NULL) { | |
995 | rc = -ENOMEM; | |
996 | goto out; | |
997 | } | |
2d821488 SH |
998 | } |
999 | if (command == NBD_CMD_WRITE) { | |
a030b347 PB |
1000 | TRACE("Reading %u byte(s)", request->len); |
1001 | ||
1c778ef7 | 1002 | if (read_sync(client->ioc, req->data, request->len) != request->len) { |
a030b347 PB |
1003 | LOG("reading from socket failed"); |
1004 | rc = -EIO; | |
1005 | goto out; | |
1006 | } | |
1007 | } | |
1008 | rc = 0; | |
1009 | ||
1010 | out: | |
262db388 | 1011 | client->recv_coroutine = NULL; |
958c717d HR |
1012 | nbd_update_can_read(client); |
1013 | ||
a030b347 PB |
1014 | return rc; |
1015 | } | |
1016 | ||
262db388 | 1017 | static void nbd_trip(void *opaque) |
75818250 | 1018 | { |
262db388 | 1019 | NBDClient *client = opaque; |
1743b515 | 1020 | NBDExport *exp = client->exp; |
ff2b68aa | 1021 | NBDRequest *req; |
b2e3d87f NT |
1022 | struct nbd_request request; |
1023 | struct nbd_reply reply; | |
94e7340b | 1024 | ssize_t ret; |
8c5d1abb | 1025 | uint32_t command; |
b2e3d87f NT |
1026 | |
1027 | TRACE("Reading request."); | |
ff2b68aa PB |
1028 | if (client->closing) { |
1029 | return; | |
1030 | } | |
b2e3d87f | 1031 | |
ff2b68aa | 1032 | req = nbd_request_get(client); |
262db388 | 1033 | ret = nbd_co_receive_request(req, &request); |
7fe7b68b PB |
1034 | if (ret == -EAGAIN) { |
1035 | goto done; | |
1036 | } | |
a030b347 | 1037 | if (ret == -EIO) { |
d9a73806 | 1038 | goto out; |
a030b347 | 1039 | } |
b2e3d87f | 1040 | |
fae69416 PB |
1041 | reply.handle = request.handle; |
1042 | reply.error = 0; | |
1043 | ||
a030b347 PB |
1044 | if (ret < 0) { |
1045 | reply.error = -ret; | |
1046 | goto error_reply; | |
b2e3d87f | 1047 | } |
8c5d1abb HB |
1048 | command = request.type & NBD_CMD_MASK_COMMAND; |
1049 | if (command != NBD_CMD_DISC && (request.from + request.len) > exp->size) { | |
b2e3d87f NT |
1050 | LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64 |
1051 | ", Offset: %" PRIu64 "\n", | |
af49bbbe | 1052 | request.from, request.len, |
0fee8f34 | 1053 | (uint64_t)exp->size, (uint64_t)exp->dev_offset); |
b2e3d87f | 1054 | LOG("requested operation past EOF--bad client?"); |
fae69416 | 1055 | goto invalid_request; |
b2e3d87f NT |
1056 | } |
1057 | ||
d6268348 WC |
1058 | if (client->closing) { |
1059 | /* | |
1060 | * The client may be closed when we are blocked in | |
1061 | * nbd_co_receive_request() | |
1062 | */ | |
1063 | goto done; | |
1064 | } | |
1065 | ||
8c5d1abb | 1066 | switch (command) { |
b2e3d87f NT |
1067 | case NBD_CMD_READ: |
1068 | TRACE("Request type is READ"); | |
1069 | ||
e25ceb76 | 1070 | if (request.type & NBD_CMD_FLAG_FUA) { |
aadf99a7 | 1071 | ret = blk_co_flush(exp->blk); |
e25ceb76 PB |
1072 | if (ret < 0) { |
1073 | LOG("flush failed"); | |
1074 | reply.error = -ret; | |
1075 | goto error_reply; | |
1076 | } | |
1077 | } | |
1078 | ||
aadf99a7 HR |
1079 | ret = blk_read(exp->blk, |
1080 | (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE, | |
1081 | req->data, request.len / BDRV_SECTOR_SIZE); | |
adcf6302 | 1082 | if (ret < 0) { |
b2e3d87f | 1083 | LOG("reading from file failed"); |
adcf6302 | 1084 | reply.error = -ret; |
fae69416 | 1085 | goto error_reply; |
b2e3d87f | 1086 | } |
b2e3d87f NT |
1087 | |
1088 | TRACE("Read %u byte(s)", request.len); | |
262db388 | 1089 | if (nbd_co_send_reply(req, &reply, request.len) < 0) |
d9a73806 | 1090 | goto out; |
b2e3d87f NT |
1091 | break; |
1092 | case NBD_CMD_WRITE: | |
1093 | TRACE("Request type is WRITE"); | |
1094 | ||
af49bbbe | 1095 | if (exp->nbdflags & NBD_FLAG_READ_ONLY) { |
b2e3d87f | 1096 | TRACE("Server is read-only, return error"); |
fae69416 PB |
1097 | reply.error = EROFS; |
1098 | goto error_reply; | |
1099 | } | |
1100 | ||
1101 | TRACE("Writing to device"); | |
1102 | ||
aadf99a7 HR |
1103 | ret = blk_write(exp->blk, |
1104 | (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE, | |
1105 | req->data, request.len / BDRV_SECTOR_SIZE); | |
fae69416 PB |
1106 | if (ret < 0) { |
1107 | LOG("writing to file failed"); | |
1108 | reply.error = -ret; | |
1109 | goto error_reply; | |
1110 | } | |
b2e3d87f | 1111 | |
fae69416 | 1112 | if (request.type & NBD_CMD_FLAG_FUA) { |
aadf99a7 | 1113 | ret = blk_co_flush(exp->blk); |
adcf6302 | 1114 | if (ret < 0) { |
fae69416 | 1115 | LOG("flush failed"); |
adcf6302 | 1116 | reply.error = -ret; |
fae69416 | 1117 | goto error_reply; |
2c7989a9 | 1118 | } |
b2e3d87f NT |
1119 | } |
1120 | ||
fc19f8a0 | 1121 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1122 | goto out; |
fc19f8a0 | 1123 | } |
b2e3d87f NT |
1124 | break; |
1125 | case NBD_CMD_DISC: | |
1126 | TRACE("Request type is DISCONNECT"); | |
1127 | errno = 0; | |
262db388 | 1128 | goto out; |
1486d04a PB |
1129 | case NBD_CMD_FLUSH: |
1130 | TRACE("Request type is FLUSH"); | |
1131 | ||
aadf99a7 | 1132 | ret = blk_co_flush(exp->blk); |
1486d04a PB |
1133 | if (ret < 0) { |
1134 | LOG("flush failed"); | |
1135 | reply.error = -ret; | |
1136 | } | |
fc19f8a0 | 1137 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1138 | goto out; |
fc19f8a0 | 1139 | } |
7a706633 PB |
1140 | break; |
1141 | case NBD_CMD_TRIM: | |
1142 | TRACE("Request type is TRIM"); | |
aadf99a7 HR |
1143 | ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset) |
1144 | / BDRV_SECTOR_SIZE, | |
1145 | request.len / BDRV_SECTOR_SIZE); | |
7a706633 PB |
1146 | if (ret < 0) { |
1147 | LOG("discard failed"); | |
1148 | reply.error = -ret; | |
1149 | } | |
fc19f8a0 | 1150 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1151 | goto out; |
fc19f8a0 | 1152 | } |
1486d04a | 1153 | break; |
b2e3d87f NT |
1154 | default: |
1155 | LOG("invalid request type (%u) received", request.type); | |
fae69416 | 1156 | invalid_request: |
8b2f0abf | 1157 | reply.error = EINVAL; |
fae69416 | 1158 | error_reply: |
fc19f8a0 | 1159 | if (nbd_co_send_reply(req, &reply, 0) < 0) { |
d9a73806 | 1160 | goto out; |
fc19f8a0 | 1161 | } |
fae69416 | 1162 | break; |
b2e3d87f NT |
1163 | } |
1164 | ||
1165 | TRACE("Request/Reply complete"); | |
1166 | ||
7fe7b68b | 1167 | done: |
262db388 PB |
1168 | nbd_request_put(req); |
1169 | return; | |
1170 | ||
d9a73806 | 1171 | out: |
72deddc5 | 1172 | nbd_request_put(req); |
f53a829b | 1173 | client_close(client); |
7a5ca864 | 1174 | } |
af49bbbe | 1175 | |
1743b515 PB |
1176 | static void nbd_read(void *opaque) |
1177 | { | |
1178 | NBDClient *client = opaque; | |
1179 | ||
262db388 PB |
1180 | if (client->recv_coroutine) { |
1181 | qemu_coroutine_enter(client->recv_coroutine, NULL); | |
1182 | } else { | |
1183 | qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client); | |
1743b515 | 1184 | } |
1743b515 PB |
1185 | } |
1186 | ||
262db388 PB |
1187 | static void nbd_restart_write(void *opaque) |
1188 | { | |
1189 | NBDClient *client = opaque; | |
1190 | ||
1191 | qemu_coroutine_enter(client->send_coroutine, NULL); | |
1192 | } | |
1193 | ||
958c717d HR |
1194 | static void nbd_set_handlers(NBDClient *client) |
1195 | { | |
1196 | if (client->exp && client->exp->ctx) { | |
1c778ef7 | 1197 | aio_set_fd_handler(client->exp->ctx, client->sioc->fd, |
172cc129 | 1198 | true, |
958c717d HR |
1199 | client->can_read ? nbd_read : NULL, |
1200 | client->send_coroutine ? nbd_restart_write : NULL, | |
1201 | client); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | static void nbd_unset_handlers(NBDClient *client) | |
1206 | { | |
1207 | if (client->exp && client->exp->ctx) { | |
1c778ef7 | 1208 | aio_set_fd_handler(client->exp->ctx, client->sioc->fd, |
172cc129 | 1209 | true, NULL, NULL, NULL); |
958c717d HR |
1210 | } |
1211 | } | |
1212 | ||
1213 | static void nbd_update_can_read(NBDClient *client) | |
1214 | { | |
1215 | bool can_read = client->recv_coroutine || | |
1216 | client->nb_requests < MAX_NBD_REQUESTS; | |
1217 | ||
1218 | if (can_read != client->can_read) { | |
1219 | client->can_read = can_read; | |
1220 | nbd_set_handlers(client); | |
1221 | ||
1222 | /* There is no need to invoke aio_notify(), since aio_set_fd_handler() | |
1223 | * in nbd_set_handlers() will have taken care of that */ | |
1224 | } | |
1225 | } | |
1226 | ||
1a6245a5 FZ |
1227 | static coroutine_fn void nbd_co_client_start(void *opaque) |
1228 | { | |
1229 | NBDClientNewData *data = opaque; | |
1230 | NBDClient *client = data->client; | |
1231 | NBDExport *exp = client->exp; | |
1232 | ||
1233 | if (exp) { | |
1234 | nbd_export_get(exp); | |
1235 | } | |
1236 | if (nbd_negotiate(data)) { | |
d3780c2d | 1237 | client_close(client); |
1a6245a5 FZ |
1238 | goto out; |
1239 | } | |
1240 | qemu_co_mutex_init(&client->send_lock); | |
1241 | nbd_set_handlers(client); | |
1242 | ||
1243 | if (exp) { | |
1244 | QTAILQ_INSERT_TAIL(&exp->clients, client, next); | |
1245 | } | |
1246 | out: | |
1247 | g_free(data); | |
1248 | } | |
1249 | ||
1c778ef7 DB |
1250 | void nbd_client_new(NBDExport *exp, |
1251 | QIOChannelSocket *sioc, | |
f95910fe DB |
1252 | QCryptoTLSCreds *tlscreds, |
1253 | const char *tlsaclname, | |
1c778ef7 | 1254 | void (*close_fn)(NBDClient *)) |
af49bbbe | 1255 | { |
1743b515 | 1256 | NBDClient *client; |
1a6245a5 FZ |
1257 | NBDClientNewData *data = g_new(NBDClientNewData, 1); |
1258 | ||
1743b515 PB |
1259 | client = g_malloc0(sizeof(NBDClient)); |
1260 | client->refcount = 1; | |
1261 | client->exp = exp; | |
f95910fe DB |
1262 | client->tlscreds = tlscreds; |
1263 | if (tlscreds) { | |
1264 | object_ref(OBJECT(client->tlscreds)); | |
1265 | } | |
1266 | client->tlsaclname = g_strdup(tlsaclname); | |
1c778ef7 DB |
1267 | client->sioc = sioc; |
1268 | object_ref(OBJECT(client->sioc)); | |
1269 | client->ioc = QIO_CHANNEL(sioc); | |
1270 | object_ref(OBJECT(client->ioc)); | |
958c717d | 1271 | client->can_read = true; |
ee7d7aab | 1272 | client->close = close_fn; |
2c8d9f06 | 1273 | |
1a6245a5 FZ |
1274 | data->client = client; |
1275 | data->co = qemu_coroutine_create(nbd_co_client_start); | |
1276 | qemu_coroutine_enter(data->co, data); | |
af49bbbe | 1277 | } |