]>
Commit | Line | Data |
---|---|---|
75818250 | 1 | /* |
a16a7907 | 2 | * Copyright (C) 2016-2018 Red Hat, Inc. |
7a5ca864 FB |
3 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
4 | * | |
798bfe00 | 5 | * Network Block Device Server Side |
7a5ca864 FB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; under version 2 of the License. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
75818250 | 18 | */ |
7a5ca864 | 19 | |
d38ea87a | 20 | #include "qemu/osdep.h" |
da34e65c | 21 | #include "qapi/error.h" |
9588463e | 22 | #include "trace.h" |
798bfe00 | 23 | #include "nbd-internal.h" |
416e34bd | 24 | #include "qemu/units.h" |
ca441480 | 25 | |
e7b1948d | 26 | #define NBD_META_ID_BASE_ALLOCATION 0 |
3d068aff VSO |
27 | #define NBD_META_ID_DIRTY_BITMAP 1 |
28 | ||
416e34bd EB |
29 | /* |
30 | * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical | |
3d068aff VSO |
31 | * constant. If an increase is needed, note that the NBD protocol |
32 | * recommends no larger than 32 mb, so that the client won't consider | |
416e34bd EB |
33 | * the reply as a denial of service attack. |
34 | */ | |
35 | #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8) | |
e7b1948d | 36 | |
ca441480 PB |
37 | static int system_errno_to_nbd_errno(int err) |
38 | { | |
39 | switch (err) { | |
40 | case 0: | |
41 | return NBD_SUCCESS; | |
42 | case EPERM: | |
c0301fcc | 43 | case EROFS: |
ca441480 PB |
44 | return NBD_EPERM; |
45 | case EIO: | |
46 | return NBD_EIO; | |
47 | case ENOMEM: | |
48 | return NBD_ENOMEM; | |
49 | #ifdef EDQUOT | |
50 | case EDQUOT: | |
51 | #endif | |
52 | case EFBIG: | |
53 | case ENOSPC: | |
54 | return NBD_ENOSPC; | |
bae245d1 EB |
55 | case EOVERFLOW: |
56 | return NBD_EOVERFLOW; | |
b6f5d3b5 EB |
57 | case ESHUTDOWN: |
58 | return NBD_ESHUTDOWN; | |
ca441480 PB |
59 | case EINVAL: |
60 | default: | |
61 | return NBD_EINVAL; | |
62 | } | |
63 | } | |
64 | ||
9a304d29 PB |
65 | /* Definitions for opaque data types */ |
66 | ||
315f78ab | 67 | typedef struct NBDRequestData NBDRequestData; |
9a304d29 | 68 | |
315f78ab EB |
69 | struct NBDRequestData { |
70 | QSIMPLEQ_ENTRY(NBDRequestData) entry; | |
9a304d29 PB |
71 | NBDClient *client; |
72 | uint8_t *data; | |
29b6c3b3 | 73 | bool complete; |
9a304d29 PB |
74 | }; |
75 | ||
76 | struct NBDExport { | |
2c8d9f06 | 77 | int refcount; |
0ddf08db PB |
78 | void (*close)(NBDExport *exp); |
79 | ||
aadf99a7 | 80 | BlockBackend *blk; |
ee0a19ec | 81 | char *name; |
b1a75b33 | 82 | char *description; |
9d26dfcb EB |
83 | uint64_t dev_offset; |
84 | uint64_t size; | |
7423f417 | 85 | uint16_t nbdflags; |
4b9441f6 | 86 | QTAILQ_HEAD(, NBDClient) clients; |
ee0a19ec | 87 | QTAILQ_ENTRY(NBDExport) next; |
958c717d HR |
88 | |
89 | AioContext *ctx; | |
741cc431 | 90 | |
cd7fca95 | 91 | BlockBackend *eject_notifier_blk; |
741cc431 | 92 | Notifier eject_notifier; |
3d068aff VSO |
93 | |
94 | BdrvDirtyBitmap *export_bitmap; | |
95 | char *export_bitmap_context; | |
9a304d29 PB |
96 | }; |
97 | ||
ee0a19ec PB |
98 | static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); |
99 | ||
e7b1948d VSO |
100 | /* NBDExportMetaContexts represents a list of contexts to be exported, |
101 | * as selected by NBD_OPT_SET_META_CONTEXT. Also used for | |
102 | * NBD_OPT_LIST_META_CONTEXT. */ | |
103 | typedef struct NBDExportMetaContexts { | |
af736e54 | 104 | NBDExport *exp; |
e7b1948d VSO |
105 | bool valid; /* means that negotiation of the option finished without |
106 | errors */ | |
107 | bool base_allocation; /* export base:allocation context (block status) */ | |
3d068aff | 108 | bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */ |
e7b1948d VSO |
109 | } NBDExportMetaContexts; |
110 | ||
9a304d29 PB |
111 | struct NBDClient { |
112 | int refcount; | |
0c9390d9 | 113 | void (*close_fn)(NBDClient *client, bool negotiated); |
9a304d29 PB |
114 | |
115 | NBDExport *exp; | |
f95910fe | 116 | QCryptoTLSCreds *tlscreds; |
b25e12da | 117 | char *tlsauthz; |
1c778ef7 DB |
118 | QIOChannelSocket *sioc; /* The underlying data channel */ |
119 | QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */ | |
9a304d29 PB |
120 | |
121 | Coroutine *recv_coroutine; | |
122 | ||
123 | CoMutex send_lock; | |
124 | Coroutine *send_coroutine; | |
125 | ||
4b9441f6 | 126 | QTAILQ_ENTRY(NBDClient) next; |
9a304d29 | 127 | int nb_requests; |
ff2b68aa | 128 | bool closing; |
5c54e7fa | 129 | |
6e280648 EB |
130 | uint32_t check_align; /* If non-zero, check for aligned client requests */ |
131 | ||
5c54e7fa | 132 | bool structured_reply; |
e7b1948d | 133 | NBDExportMetaContexts export_meta; |
9a304d29 | 134 | |
0cfae925 VSO |
135 | uint32_t opt; /* Current option being negotiated */ |
136 | uint32_t optlen; /* remaining length of data in ioc for the option being | |
137 | negotiated now */ | |
138 | }; | |
7a5ca864 | 139 | |
ff82911c | 140 | static void nbd_client_receive_next_request(NBDClient *client); |
958c717d | 141 | |
6b8c01e7 | 142 | /* Basic flow for negotiation |
7a5ca864 FB |
143 | |
144 | Server Client | |
7a5ca864 | 145 | Negotiate |
6b8c01e7 PB |
146 | |
147 | or | |
148 | ||
149 | Server Client | |
150 | Negotiate #1 | |
151 | Option | |
152 | Negotiate #2 | |
153 | ||
154 | ---- | |
155 | ||
156 | followed by | |
157 | ||
158 | Server Client | |
7a5ca864 FB |
159 | Request |
160 | Response | |
161 | Request | |
162 | Response | |
163 | ... | |
164 | ... | |
165 | Request (type == 2) | |
6b8c01e7 | 166 | |
7a5ca864 FB |
167 | */ |
168 | ||
1d17922a VSO |
169 | static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option, |
170 | uint32_t type, uint32_t length) | |
171 | { | |
172 | stq_be_p(&rep->magic, NBD_REP_MAGIC); | |
173 | stl_be_p(&rep->option, option); | |
174 | stl_be_p(&rep->type, type); | |
175 | stl_be_p(&rep->length, length); | |
176 | } | |
177 | ||
526e5c65 EB |
178 | /* Send a reply header, including length, but no payload. |
179 | * Return -errno on error, 0 on success. */ | |
0cfae925 VSO |
180 | static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type, |
181 | uint32_t len, Error **errp) | |
6b8c01e7 | 182 | { |
1d17922a | 183 | NBDOptionReply rep; |
6b8c01e7 | 184 | |
1d17922a | 185 | trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt), |
3736cc5b | 186 | type, nbd_rep_lookup(type), len); |
f95910fe | 187 | |
f37708f6 | 188 | assert(len < NBD_MAX_BUFFER_SIZE); |
2fd2c840 | 189 | |
1d17922a VSO |
190 | set_be_option_rep(&rep, client->opt, type, len); |
191 | return nbd_write(client->ioc, &rep, sizeof(rep), errp); | |
f5076b5a | 192 | } |
6b8c01e7 | 193 | |
526e5c65 EB |
194 | /* Send a reply header with default 0 length. |
195 | * Return -errno on error, 0 on success. */ | |
0cfae925 | 196 | static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type, |
2fd2c840 | 197 | Error **errp) |
526e5c65 | 198 | { |
0cfae925 | 199 | return nbd_negotiate_send_rep_len(client, type, 0, errp); |
526e5c65 EB |
200 | } |
201 | ||
36683283 EB |
202 | /* Send an error reply. |
203 | * Return -errno on error, 0 on success. */ | |
41f5dfaf EB |
204 | static int GCC_FMT_ATTR(4, 0) |
205 | nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type, | |
206 | Error **errp, const char *fmt, va_list va) | |
36683283 | 207 | { |
36683283 EB |
208 | char *msg; |
209 | int ret; | |
210 | size_t len; | |
211 | ||
36683283 | 212 | msg = g_strdup_vprintf(fmt, va); |
36683283 EB |
213 | len = strlen(msg); |
214 | assert(len < 4096); | |
9588463e | 215 | trace_nbd_negotiate_send_rep_err(msg); |
0cfae925 | 216 | ret = nbd_negotiate_send_rep_len(client, type, len, errp); |
36683283 EB |
217 | if (ret < 0) { |
218 | goto out; | |
219 | } | |
0cfae925 | 220 | if (nbd_write(client->ioc, msg, len, errp) < 0) { |
2fd2c840 | 221 | error_prepend(errp, "write failed (error message): "); |
36683283 EB |
222 | ret = -EIO; |
223 | } else { | |
224 | ret = 0; | |
225 | } | |
2fd2c840 | 226 | |
36683283 EB |
227 | out: |
228 | g_free(msg); | |
229 | return ret; | |
230 | } | |
231 | ||
41f5dfaf EB |
232 | /* Send an error reply. |
233 | * Return -errno on error, 0 on success. */ | |
234 | static int GCC_FMT_ATTR(4, 5) | |
235 | nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, | |
236 | Error **errp, const char *fmt, ...) | |
237 | { | |
238 | va_list va; | |
239 | int ret; | |
240 | ||
241 | va_start(va, fmt); | |
242 | ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va); | |
243 | va_end(va); | |
244 | return ret; | |
245 | } | |
246 | ||
894e0280 EB |
247 | /* Drop remainder of the current option, and send a reply with the |
248 | * given error type and message. Return -errno on read or write | |
249 | * failure; or 0 if connection is still live. */ | |
2e425fd5 VSO |
250 | static int GCC_FMT_ATTR(4, 0) |
251 | nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp, | |
252 | const char *fmt, va_list va) | |
894e0280 EB |
253 | { |
254 | int ret = nbd_drop(client->ioc, client->optlen, errp); | |
894e0280 EB |
255 | |
256 | client->optlen = 0; | |
257 | if (!ret) { | |
894e0280 | 258 | ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va); |
894e0280 EB |
259 | } |
260 | return ret; | |
261 | } | |
262 | ||
2e425fd5 VSO |
263 | static int GCC_FMT_ATTR(4, 5) |
264 | nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp, | |
265 | const char *fmt, ...) | |
266 | { | |
267 | int ret; | |
268 | va_list va; | |
269 | ||
270 | va_start(va, fmt); | |
271 | ret = nbd_opt_vdrop(client, type, errp, fmt, va); | |
272 | va_end(va); | |
273 | ||
274 | return ret; | |
275 | } | |
276 | ||
277 | static int GCC_FMT_ATTR(3, 4) | |
278 | nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...) | |
279 | { | |
280 | int ret; | |
281 | va_list va; | |
282 | ||
283 | va_start(va, fmt); | |
284 | ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va); | |
285 | va_end(va); | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
894e0280 EB |
290 | /* Read size bytes from the unparsed payload of the current option. |
291 | * Return -errno on I/O error, 0 if option was completely handled by | |
292 | * sending a reply about inconsistent lengths, or 1 on success. */ | |
293 | static int nbd_opt_read(NBDClient *client, void *buffer, size_t size, | |
294 | Error **errp) | |
295 | { | |
296 | if (size > client->optlen) { | |
2e425fd5 VSO |
297 | return nbd_opt_invalid(client, errp, |
298 | "Inconsistent lengths in option %s", | |
299 | nbd_opt_lookup(client->opt)); | |
894e0280 EB |
300 | } |
301 | client->optlen -= size; | |
302 | return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1; | |
303 | } | |
304 | ||
e7b1948d VSO |
305 | /* Drop size bytes from the unparsed payload of the current option. |
306 | * Return -errno on I/O error, 0 if option was completely handled by | |
307 | * sending a reply about inconsistent lengths, or 1 on success. */ | |
308 | static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp) | |
309 | { | |
310 | if (size > client->optlen) { | |
311 | return nbd_opt_invalid(client, errp, | |
312 | "Inconsistent lengths in option %s", | |
313 | nbd_opt_lookup(client->opt)); | |
314 | } | |
315 | client->optlen -= size; | |
316 | return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1; | |
317 | } | |
318 | ||
12296459 VSO |
319 | /* nbd_opt_read_name |
320 | * | |
321 | * Read a string with the format: | |
322 | * uint32_t len (<= NBD_MAX_NAME_SIZE) | |
323 | * len bytes string (not 0-terminated) | |
324 | * | |
325 | * @name should be enough to store NBD_MAX_NAME_SIZE+1. | |
326 | * If @length is non-null, it will be set to the actual string length. | |
327 | * | |
328 | * Return -errno on I/O error, 0 if option was completely handled by | |
329 | * sending a reply about inconsistent lengths, or 1 on success. | |
330 | */ | |
331 | static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length, | |
332 | Error **errp) | |
333 | { | |
334 | int ret; | |
335 | uint32_t len; | |
336 | ||
337 | ret = nbd_opt_read(client, &len, sizeof(len), errp); | |
338 | if (ret <= 0) { | |
339 | return ret; | |
340 | } | |
80c7c2b0 | 341 | len = cpu_to_be32(len); |
12296459 VSO |
342 | |
343 | if (len > NBD_MAX_NAME_SIZE) { | |
344 | return nbd_opt_invalid(client, errp, | |
345 | "Invalid name length: %" PRIu32, len); | |
346 | } | |
347 | ||
348 | ret = nbd_opt_read(client, name, len, errp); | |
349 | if (ret <= 0) { | |
350 | return ret; | |
351 | } | |
352 | name[len] = '\0'; | |
353 | ||
354 | if (length) { | |
355 | *length = len; | |
356 | } | |
357 | ||
358 | return 1; | |
359 | } | |
360 | ||
526e5c65 EB |
361 | /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. |
362 | * Return -errno on error, 0 on success. */ | |
0cfae925 | 363 | static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, |
2fd2c840 | 364 | Error **errp) |
32d7d2e0 | 365 | { |
b1a75b33 | 366 | size_t name_len, desc_len; |
526e5c65 | 367 | uint32_t len; |
b1a75b33 EB |
368 | const char *name = exp->name ? exp->name : ""; |
369 | const char *desc = exp->description ? exp->description : ""; | |
0cfae925 | 370 | QIOChannel *ioc = client->ioc; |
2e5c9ad6 | 371 | int ret; |
32d7d2e0 | 372 | |
9588463e | 373 | trace_nbd_negotiate_send_rep_list(name, desc); |
b1a75b33 EB |
374 | name_len = strlen(name); |
375 | desc_len = strlen(desc); | |
526e5c65 | 376 | len = name_len + desc_len + sizeof(len); |
0cfae925 | 377 | ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp); |
2e5c9ad6 VSO |
378 | if (ret < 0) { |
379 | return ret; | |
32d7d2e0 | 380 | } |
526e5c65 | 381 | |
32d7d2e0 | 382 | len = cpu_to_be32(name_len); |
2fd2c840 VSO |
383 | if (nbd_write(ioc, &len, sizeof(len), errp) < 0) { |
384 | error_prepend(errp, "write failed (name length): "); | |
b1a75b33 EB |
385 | return -EINVAL; |
386 | } | |
2fd2c840 VSO |
387 | |
388 | if (nbd_write(ioc, name, name_len, errp) < 0) { | |
389 | error_prepend(errp, "write failed (name buffer): "); | |
32d7d2e0 HB |
390 | return -EINVAL; |
391 | } | |
2fd2c840 VSO |
392 | |
393 | if (nbd_write(ioc, desc, desc_len, errp) < 0) { | |
394 | error_prepend(errp, "write failed (description buffer): "); | |
32d7d2e0 HB |
395 | return -EINVAL; |
396 | } | |
2fd2c840 | 397 | |
32d7d2e0 HB |
398 | return 0; |
399 | } | |
400 | ||
526e5c65 EB |
401 | /* Process the NBD_OPT_LIST command, with a potential series of replies. |
402 | * Return -errno on error, 0 on success. */ | |
e68c35cf | 403 | static int nbd_negotiate_handle_list(NBDClient *client, Error **errp) |
32d7d2e0 | 404 | { |
32d7d2e0 | 405 | NBDExport *exp; |
0cfae925 | 406 | assert(client->opt == NBD_OPT_LIST); |
32d7d2e0 | 407 | |
32d7d2e0 HB |
408 | /* For each export, send a NBD_REP_SERVER reply. */ |
409 | QTAILQ_FOREACH(exp, &exports, next) { | |
0cfae925 | 410 | if (nbd_negotiate_send_rep_list(client, exp, errp)) { |
32d7d2e0 HB |
411 | return -EINVAL; |
412 | } | |
413 | } | |
414 | /* Finish with a NBD_REP_ACK. */ | |
0cfae925 | 415 | return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); |
32d7d2e0 HB |
416 | } |
417 | ||
af736e54 | 418 | static void nbd_check_meta_export(NBDClient *client) |
e7b1948d | 419 | { |
af736e54 | 420 | client->export_meta.valid &= client->exp == client->export_meta.exp; |
e7b1948d VSO |
421 | } |
422 | ||
f37708f6 EB |
423 | /* Send a reply to NBD_OPT_EXPORT_NAME. |
424 | * Return -errno on error, 0 on success. */ | |
0cfae925 | 425 | static int nbd_negotiate_handle_export_name(NBDClient *client, |
23e099c3 | 426 | uint16_t myflags, bool no_zeroes, |
2fd2c840 | 427 | Error **errp) |
f5076b5a | 428 | { |
943cec86 | 429 | char name[NBD_MAX_NAME_SIZE + 1]; |
5f66d060 | 430 | char buf[NBD_REPLY_EXPORT_NAME_SIZE] = ""; |
23e099c3 EB |
431 | size_t len; |
432 | int ret; | |
6b8c01e7 | 433 | |
f5076b5a HB |
434 | /* Client sends: |
435 | [20 .. xx] export name (length bytes) | |
5f66d060 EB |
436 | Server replies: |
437 | [ 0 .. 7] size | |
438 | [ 8 .. 9] export flags | |
439 | [10 .. 133] reserved (0) [unless no_zeroes] | |
f5076b5a | 440 | */ |
9588463e | 441 | trace_nbd_negotiate_handle_export_name(); |
0cfae925 | 442 | if (client->optlen >= sizeof(name)) { |
2fd2c840 | 443 | error_setg(errp, "Bad length received"); |
d9faeed8 | 444 | return -EINVAL; |
6b8c01e7 | 445 | } |
e6798f06 | 446 | if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) { |
32f158a6 | 447 | return -EIO; |
6b8c01e7 | 448 | } |
0cfae925 VSO |
449 | name[client->optlen] = '\0'; |
450 | client->optlen = 0; | |
6b8c01e7 | 451 | |
9588463e | 452 | trace_nbd_negotiate_handle_export_name_request(name); |
9344e5f5 | 453 | |
6b8c01e7 PB |
454 | client->exp = nbd_export_find(name); |
455 | if (!client->exp) { | |
2fd2c840 | 456 | error_setg(errp, "export not found"); |
d9faeed8 | 457 | return -EINVAL; |
6b8c01e7 PB |
458 | } |
459 | ||
23e099c3 EB |
460 | trace_nbd_negotiate_new_style_size_flags(client->exp->size, |
461 | client->exp->nbdflags | myflags); | |
462 | stq_be_p(buf, client->exp->size); | |
463 | stw_be_p(buf + 8, client->exp->nbdflags | myflags); | |
464 | len = no_zeroes ? 10 : sizeof(buf); | |
465 | ret = nbd_write(client->ioc, buf, len, errp); | |
466 | if (ret < 0) { | |
467 | error_prepend(errp, "write failed: "); | |
468 | return ret; | |
469 | } | |
470 | ||
6b8c01e7 PB |
471 | QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); |
472 | nbd_export_get(client->exp); | |
af736e54 | 473 | nbd_check_meta_export(client); |
d9faeed8 VSO |
474 | |
475 | return 0; | |
6b8c01e7 PB |
476 | } |
477 | ||
f37708f6 EB |
478 | /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes. |
479 | * The buffer does NOT include the info type prefix. | |
480 | * Return -errno on error, 0 if ready to send more. */ | |
0cfae925 | 481 | static int nbd_negotiate_send_info(NBDClient *client, |
f37708f6 EB |
482 | uint16_t info, uint32_t length, void *buf, |
483 | Error **errp) | |
484 | { | |
485 | int rc; | |
486 | ||
487 | trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length); | |
0cfae925 | 488 | rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO, |
f37708f6 EB |
489 | sizeof(info) + length, errp); |
490 | if (rc < 0) { | |
491 | return rc; | |
492 | } | |
80c7c2b0 | 493 | info = cpu_to_be16(info); |
f37708f6 EB |
494 | if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) { |
495 | return -EIO; | |
496 | } | |
497 | if (nbd_write(client->ioc, buf, length, errp) < 0) { | |
498 | return -EIO; | |
499 | } | |
500 | return 0; | |
501 | } | |
502 | ||
a16a7907 EB |
503 | /* nbd_reject_length: Handle any unexpected payload. |
504 | * @fatal requests that we quit talking to the client, even if we are able | |
505 | * to successfully send an error reply. | |
506 | * Return: | |
507 | * -errno transmission error occurred or @fatal was requested, errp is set | |
508 | * 0 error message successfully sent to client, errp is not set | |
509 | */ | |
0cfae925 | 510 | static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp) |
a16a7907 EB |
511 | { |
512 | int ret; | |
513 | ||
0cfae925 | 514 | assert(client->optlen); |
2e425fd5 VSO |
515 | ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length", |
516 | nbd_opt_lookup(client->opt)); | |
a16a7907 | 517 | if (fatal && !ret) { |
894e0280 | 518 | error_setg(errp, "option '%s' has unexpected length", |
0cfae925 | 519 | nbd_opt_lookup(client->opt)); |
a16a7907 EB |
520 | return -EINVAL; |
521 | } | |
522 | return ret; | |
523 | } | |
524 | ||
f37708f6 EB |
525 | /* Handle NBD_OPT_INFO and NBD_OPT_GO. |
526 | * Return -errno on error, 0 if ready for next option, and 1 to move | |
527 | * into transmission phase. */ | |
0cfae925 | 528 | static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags, |
f37708f6 EB |
529 | Error **errp) |
530 | { | |
531 | int rc; | |
532 | char name[NBD_MAX_NAME_SIZE + 1]; | |
533 | NBDExport *exp; | |
534 | uint16_t requests; | |
535 | uint16_t request; | |
536 | uint32_t namelen; | |
537 | bool sendname = false; | |
0c1d50bd EB |
538 | bool blocksize = false; |
539 | uint32_t sizes[3]; | |
f37708f6 | 540 | char buf[sizeof(uint64_t) + sizeof(uint16_t)]; |
6e280648 | 541 | uint32_t check_align = 0; |
f37708f6 EB |
542 | |
543 | /* Client sends: | |
544 | 4 bytes: L, name length (can be 0) | |
545 | L bytes: export name | |
546 | 2 bytes: N, number of requests (can be 0) | |
547 | N * 2 bytes: N requests | |
548 | */ | |
12296459 | 549 | rc = nbd_opt_read_name(client, name, &namelen, errp); |
894e0280 EB |
550 | if (rc <= 0) { |
551 | return rc; | |
f37708f6 | 552 | } |
f37708f6 EB |
553 | trace_nbd_negotiate_handle_export_name_request(name); |
554 | ||
894e0280 EB |
555 | rc = nbd_opt_read(client, &requests, sizeof(requests), errp); |
556 | if (rc <= 0) { | |
557 | return rc; | |
f37708f6 | 558 | } |
80c7c2b0 | 559 | requests = be16_to_cpu(requests); |
f37708f6 | 560 | trace_nbd_negotiate_handle_info_requests(requests); |
f37708f6 | 561 | while (requests--) { |
894e0280 EB |
562 | rc = nbd_opt_read(client, &request, sizeof(request), errp); |
563 | if (rc <= 0) { | |
564 | return rc; | |
f37708f6 | 565 | } |
80c7c2b0 | 566 | request = be16_to_cpu(request); |
f37708f6 EB |
567 | trace_nbd_negotiate_handle_info_request(request, |
568 | nbd_info_lookup(request)); | |
0c1d50bd EB |
569 | /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE; |
570 | * everything else is either a request we don't know or | |
571 | * something we send regardless of request */ | |
572 | switch (request) { | |
573 | case NBD_INFO_NAME: | |
f37708f6 | 574 | sendname = true; |
0c1d50bd EB |
575 | break; |
576 | case NBD_INFO_BLOCK_SIZE: | |
577 | blocksize = true; | |
578 | break; | |
f37708f6 EB |
579 | } |
580 | } | |
894e0280 EB |
581 | if (client->optlen) { |
582 | return nbd_reject_length(client, false, errp); | |
583 | } | |
f37708f6 EB |
584 | |
585 | exp = nbd_export_find(name); | |
586 | if (!exp) { | |
0cfae925 VSO |
587 | return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN, |
588 | errp, "export '%s' not present", | |
f37708f6 EB |
589 | name); |
590 | } | |
591 | ||
592 | /* Don't bother sending NBD_INFO_NAME unless client requested it */ | |
593 | if (sendname) { | |
0cfae925 | 594 | rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name, |
f37708f6 EB |
595 | errp); |
596 | if (rc < 0) { | |
597 | return rc; | |
598 | } | |
599 | } | |
600 | ||
601 | /* Send NBD_INFO_DESCRIPTION only if available, regardless of | |
602 | * client request */ | |
603 | if (exp->description) { | |
604 | size_t len = strlen(exp->description); | |
605 | ||
0cfae925 | 606 | rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION, |
f37708f6 EB |
607 | len, exp->description, errp); |
608 | if (rc < 0) { | |
609 | return rc; | |
610 | } | |
611 | } | |
612 | ||
0c1d50bd EB |
613 | /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size |
614 | * according to whether the client requested it, and according to | |
615 | * whether this is OPT_INFO or OPT_GO. */ | |
b0245d64 EB |
616 | /* minimum - 1 for back-compat, or actual if client will obey it. */ |
617 | if (client->opt == NBD_OPT_INFO || blocksize) { | |
6e280648 | 618 | check_align = sizes[0] = blk_get_request_alignment(exp->blk); |
b0245d64 EB |
619 | } else { |
620 | sizes[0] = 1; | |
621 | } | |
622 | assert(sizes[0] <= NBD_MAX_BUFFER_SIZE); | |
0c1d50bd EB |
623 | /* preferred - Hard-code to 4096 for now. |
624 | * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */ | |
b0245d64 | 625 | sizes[1] = MAX(4096, sizes[0]); |
0c1d50bd EB |
626 | /* maximum - At most 32M, but smaller as appropriate. */ |
627 | sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE); | |
628 | trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]); | |
80c7c2b0 PM |
629 | sizes[0] = cpu_to_be32(sizes[0]); |
630 | sizes[1] = cpu_to_be32(sizes[1]); | |
631 | sizes[2] = cpu_to_be32(sizes[2]); | |
0cfae925 | 632 | rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE, |
0c1d50bd EB |
633 | sizeof(sizes), sizes, errp); |
634 | if (rc < 0) { | |
635 | return rc; | |
636 | } | |
637 | ||
f37708f6 EB |
638 | /* Send NBD_INFO_EXPORT always */ |
639 | trace_nbd_negotiate_new_style_size_flags(exp->size, | |
640 | exp->nbdflags | myflags); | |
641 | stq_be_p(buf, exp->size); | |
642 | stw_be_p(buf + 8, exp->nbdflags | myflags); | |
0cfae925 | 643 | rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT, |
f37708f6 EB |
644 | sizeof(buf), buf, errp); |
645 | if (rc < 0) { | |
646 | return rc; | |
647 | } | |
648 | ||
099fbcd6 EB |
649 | /* |
650 | * If the client is just asking for NBD_OPT_INFO, but forgot to | |
651 | * request block sizes in a situation that would impact | |
652 | * performance, then return an error. But for NBD_OPT_GO, we | |
653 | * tolerate all clients, regardless of alignments. | |
654 | */ | |
655 | if (client->opt == NBD_OPT_INFO && !blocksize && | |
656 | blk_get_request_alignment(exp->blk) > 1) { | |
0cfae925 VSO |
657 | return nbd_negotiate_send_rep_err(client, |
658 | NBD_REP_ERR_BLOCK_SIZE_REQD, | |
0c1d50bd EB |
659 | errp, |
660 | "request NBD_INFO_BLOCK_SIZE to " | |
661 | "use this export"); | |
662 | } | |
663 | ||
f37708f6 | 664 | /* Final reply */ |
0cfae925 | 665 | rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); |
f37708f6 EB |
666 | if (rc < 0) { |
667 | return rc; | |
668 | } | |
669 | ||
0cfae925 | 670 | if (client->opt == NBD_OPT_GO) { |
f37708f6 | 671 | client->exp = exp; |
6e280648 | 672 | client->check_align = check_align; |
f37708f6 EB |
673 | QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); |
674 | nbd_export_get(client->exp); | |
af736e54 | 675 | nbd_check_meta_export(client); |
f37708f6 EB |
676 | rc = 1; |
677 | } | |
678 | return rc; | |
f37708f6 EB |
679 | } |
680 | ||
681 | ||
36683283 EB |
682 | /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the |
683 | * new channel for all further (now-encrypted) communication. */ | |
f95910fe | 684 | static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client, |
2fd2c840 | 685 | Error **errp) |
f95910fe DB |
686 | { |
687 | QIOChannel *ioc; | |
688 | QIOChannelTLS *tioc; | |
689 | struct NBDTLSHandshakeData data = { 0 }; | |
690 | ||
0cfae925 VSO |
691 | assert(client->opt == NBD_OPT_STARTTLS); |
692 | ||
9588463e | 693 | trace_nbd_negotiate_handle_starttls(); |
f95910fe | 694 | ioc = client->ioc; |
f95910fe | 695 | |
0cfae925 | 696 | if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) { |
63d5ef86 EB |
697 | return NULL; |
698 | } | |
f95910fe DB |
699 | |
700 | tioc = qio_channel_tls_new_server(ioc, | |
701 | client->tlscreds, | |
b25e12da | 702 | client->tlsauthz, |
2fd2c840 | 703 | errp); |
f95910fe DB |
704 | if (!tioc) { |
705 | return NULL; | |
706 | } | |
707 | ||
0d73f725 | 708 | qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls"); |
9588463e | 709 | trace_nbd_negotiate_handle_starttls_handshake(); |
f95910fe DB |
710 | data.loop = g_main_loop_new(g_main_context_default(), FALSE); |
711 | qio_channel_tls_handshake(tioc, | |
712 | nbd_tls_handshake, | |
713 | &data, | |
1939ccda | 714 | NULL, |
f95910fe DB |
715 | NULL); |
716 | ||
717 | if (!data.complete) { | |
718 | g_main_loop_run(data.loop); | |
719 | } | |
720 | g_main_loop_unref(data.loop); | |
721 | if (data.error) { | |
722 | object_unref(OBJECT(tioc)); | |
2fd2c840 | 723 | error_propagate(errp, data.error); |
f95910fe DB |
724 | return NULL; |
725 | } | |
726 | ||
727 | return QIO_CHANNEL(tioc); | |
728 | } | |
729 | ||
e7b1948d VSO |
730 | /* nbd_negotiate_send_meta_context |
731 | * | |
732 | * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT | |
733 | * | |
734 | * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead. | |
735 | */ | |
736 | static int nbd_negotiate_send_meta_context(NBDClient *client, | |
737 | const char *context, | |
738 | uint32_t context_id, | |
739 | Error **errp) | |
740 | { | |
741 | NBDOptionReplyMetaContext opt; | |
742 | struct iovec iov[] = { | |
743 | {.iov_base = &opt, .iov_len = sizeof(opt)}, | |
744 | {.iov_base = (void *)context, .iov_len = strlen(context)} | |
745 | }; | |
746 | ||
747 | if (client->opt == NBD_OPT_LIST_META_CONTEXT) { | |
748 | context_id = 0; | |
749 | } | |
750 | ||
2b53af25 | 751 | trace_nbd_negotiate_meta_query_reply(context, context_id); |
e7b1948d VSO |
752 | set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT, |
753 | sizeof(opt) - sizeof(opt.h) + iov[1].iov_len); | |
754 | stl_be_p(&opt.context_id, context_id); | |
755 | ||
756 | return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0; | |
757 | } | |
758 | ||
b0769d8f VSO |
759 | /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern. |
760 | * @match is never set to false. | |
e7b1948d VSO |
761 | * |
762 | * Return -errno on I/O error, 0 if option was completely handled by | |
dbb8b396 VSO |
763 | * sending a reply about inconsistent lengths, or 1 on success. |
764 | * | |
b0769d8f VSO |
765 | * Note: return code = 1 doesn't mean that we've read exactly @pattern. |
766 | * It only means that there are no errors. | |
dbb8b396 | 767 | */ |
b0769d8f VSO |
768 | static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match, |
769 | Error **errp) | |
e7b1948d VSO |
770 | { |
771 | int ret; | |
b0769d8f VSO |
772 | char *query; |
773 | size_t len = strlen(pattern); | |
e7b1948d | 774 | |
b0769d8f | 775 | assert(len); |
e7b1948d | 776 | |
b0769d8f | 777 | query = g_malloc(len); |
e7b1948d VSO |
778 | ret = nbd_opt_read(client, query, len, errp); |
779 | if (ret <= 0) { | |
b0769d8f | 780 | g_free(query); |
e7b1948d VSO |
781 | return ret; |
782 | } | |
783 | ||
b0769d8f VSO |
784 | if (strncmp(query, pattern, len) == 0) { |
785 | trace_nbd_negotiate_meta_query_parse(pattern); | |
786 | *match = true; | |
dbb8b396 | 787 | } else { |
b0769d8f | 788 | trace_nbd_negotiate_meta_query_skip("pattern not matched"); |
e7b1948d | 789 | } |
b0769d8f | 790 | g_free(query); |
e7b1948d VSO |
791 | |
792 | return 1; | |
793 | } | |
794 | ||
b0769d8f VSO |
795 | /* |
796 | * Read @len bytes, and set @match to true if they match @pattern, or if @len | |
797 | * is 0 and the client is performing _LIST_. @match is never set to false. | |
798 | * | |
799 | * Return -errno on I/O error, 0 if option was completely handled by | |
800 | * sending a reply about inconsistent lengths, or 1 on success. | |
801 | * | |
802 | * Note: return code = 1 doesn't mean that we've read exactly @pattern. | |
803 | * It only means that there are no errors. | |
804 | */ | |
805 | static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern, | |
806 | uint32_t len, bool *match, Error **errp) | |
807 | { | |
808 | if (len == 0) { | |
809 | if (client->opt == NBD_OPT_LIST_META_CONTEXT) { | |
810 | *match = true; | |
811 | } | |
812 | trace_nbd_negotiate_meta_query_parse("empty"); | |
813 | return 1; | |
814 | } | |
815 | ||
816 | if (len != strlen(pattern)) { | |
817 | trace_nbd_negotiate_meta_query_skip("different lengths"); | |
818 | return nbd_opt_skip(client, len, errp); | |
819 | } | |
820 | ||
821 | return nbd_meta_pattern(client, pattern, match, errp); | |
822 | } | |
823 | ||
824 | /* nbd_meta_base_query | |
825 | * | |
826 | * Handle queries to 'base' namespace. For now, only the base:allocation | |
827 | * context is available. 'len' is the amount of text remaining to be read from | |
828 | * the current name, after the 'base:' portion has been stripped. | |
829 | * | |
830 | * Return -errno on I/O error, 0 if option was completely handled by | |
831 | * sending a reply about inconsistent lengths, or 1 on success. | |
832 | */ | |
833 | static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta, | |
834 | uint32_t len, Error **errp) | |
835 | { | |
836 | return nbd_meta_empty_or_pattern(client, "allocation", len, | |
837 | &meta->base_allocation, errp); | |
838 | } | |
839 | ||
3d068aff VSO |
840 | /* nbd_meta_bitmap_query |
841 | * | |
842 | * Handle query to 'qemu:' namespace. | |
843 | * @len is the amount of text remaining to be read from the current name, after | |
844 | * the 'qemu:' portion has been stripped. | |
845 | * | |
846 | * Return -errno on I/O error, 0 if option was completely handled by | |
847 | * sending a reply about inconsistent lengths, or 1 on success. */ | |
848 | static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta, | |
849 | uint32_t len, Error **errp) | |
850 | { | |
851 | bool dirty_bitmap = false; | |
852 | size_t dirty_bitmap_len = strlen("dirty-bitmap:"); | |
853 | int ret; | |
854 | ||
855 | if (!meta->exp->export_bitmap) { | |
856 | trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported"); | |
857 | return nbd_opt_skip(client, len, errp); | |
858 | } | |
859 | ||
860 | if (len == 0) { | |
861 | if (client->opt == NBD_OPT_LIST_META_CONTEXT) { | |
862 | meta->bitmap = true; | |
863 | } | |
864 | trace_nbd_negotiate_meta_query_parse("empty"); | |
865 | return 1; | |
866 | } | |
867 | ||
868 | if (len < dirty_bitmap_len) { | |
869 | trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:"); | |
870 | return nbd_opt_skip(client, len, errp); | |
871 | } | |
872 | ||
873 | len -= dirty_bitmap_len; | |
874 | ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp); | |
875 | if (ret <= 0) { | |
876 | return ret; | |
877 | } | |
878 | if (!dirty_bitmap) { | |
879 | trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:"); | |
880 | return nbd_opt_skip(client, len, errp); | |
881 | } | |
882 | ||
883 | trace_nbd_negotiate_meta_query_parse("dirty-bitmap:"); | |
884 | ||
885 | return nbd_meta_empty_or_pattern( | |
886 | client, meta->exp->export_bitmap_context + | |
887 | strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp); | |
888 | } | |
889 | ||
e7b1948d VSO |
890 | /* nbd_negotiate_meta_query |
891 | * | |
892 | * Parse namespace name and call corresponding function to parse body of the | |
893 | * query. | |
894 | * | |
895 | * The only supported namespace now is 'base'. | |
896 | * | |
897 | * The function aims not wasting time and memory to read long unknown namespace | |
898 | * names. | |
899 | * | |
900 | * Return -errno on I/O error, 0 if option was completely handled by | |
901 | * sending a reply about inconsistent lengths, or 1 on success. */ | |
902 | static int nbd_negotiate_meta_query(NBDClient *client, | |
903 | NBDExportMetaContexts *meta, Error **errp) | |
904 | { | |
3d068aff VSO |
905 | /* |
906 | * Both 'qemu' and 'base' namespaces have length = 5 including a | |
907 | * colon. If another length namespace is later introduced, this | |
908 | * should certainly be refactored. | |
909 | */ | |
e7b1948d | 910 | int ret; |
3d068aff VSO |
911 | size_t ns_len = 5; |
912 | char ns[5]; | |
e7b1948d VSO |
913 | uint32_t len; |
914 | ||
915 | ret = nbd_opt_read(client, &len, sizeof(len), errp); | |
916 | if (ret <= 0) { | |
917 | return ret; | |
918 | } | |
80c7c2b0 | 919 | len = cpu_to_be32(len); |
e7b1948d | 920 | |
3d068aff | 921 | if (len < ns_len) { |
2b53af25 | 922 | trace_nbd_negotiate_meta_query_skip("length too short"); |
e7b1948d VSO |
923 | return nbd_opt_skip(client, len, errp); |
924 | } | |
925 | ||
3d068aff VSO |
926 | len -= ns_len; |
927 | ret = nbd_opt_read(client, ns, ns_len, errp); | |
e7b1948d VSO |
928 | if (ret <= 0) { |
929 | return ret; | |
930 | } | |
3d068aff VSO |
931 | |
932 | if (!strncmp(ns, "base:", ns_len)) { | |
933 | trace_nbd_negotiate_meta_query_parse("base:"); | |
934 | return nbd_meta_base_query(client, meta, len, errp); | |
935 | } else if (!strncmp(ns, "qemu:", ns_len)) { | |
936 | trace_nbd_negotiate_meta_query_parse("qemu:"); | |
937 | return nbd_meta_qemu_query(client, meta, len, errp); | |
e7b1948d VSO |
938 | } |
939 | ||
3d068aff VSO |
940 | trace_nbd_negotiate_meta_query_skip("unknown namespace"); |
941 | return nbd_opt_skip(client, len, errp); | |
e7b1948d VSO |
942 | } |
943 | ||
944 | /* nbd_negotiate_meta_queries | |
945 | * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT | |
946 | * | |
947 | * Return -errno on I/O error, or 0 if option was completely handled. */ | |
948 | static int nbd_negotiate_meta_queries(NBDClient *client, | |
949 | NBDExportMetaContexts *meta, Error **errp) | |
950 | { | |
951 | int ret; | |
af736e54 | 952 | char export_name[NBD_MAX_NAME_SIZE + 1]; |
e7b1948d VSO |
953 | NBDExportMetaContexts local_meta; |
954 | uint32_t nb_queries; | |
955 | int i; | |
956 | ||
957 | if (!client->structured_reply) { | |
958 | return nbd_opt_invalid(client, errp, | |
959 | "request option '%s' when structured reply " | |
960 | "is not negotiated", | |
961 | nbd_opt_lookup(client->opt)); | |
962 | } | |
963 | ||
964 | if (client->opt == NBD_OPT_LIST_META_CONTEXT) { | |
965 | /* Only change the caller's meta on SET. */ | |
966 | meta = &local_meta; | |
967 | } | |
968 | ||
969 | memset(meta, 0, sizeof(*meta)); | |
970 | ||
af736e54 | 971 | ret = nbd_opt_read_name(client, export_name, NULL, errp); |
e7b1948d VSO |
972 | if (ret <= 0) { |
973 | return ret; | |
974 | } | |
975 | ||
af736e54 VSO |
976 | meta->exp = nbd_export_find(export_name); |
977 | if (meta->exp == NULL) { | |
e7b1948d | 978 | return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp, |
af736e54 | 979 | "export '%s' not present", export_name); |
e7b1948d VSO |
980 | } |
981 | ||
982 | ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp); | |
983 | if (ret <= 0) { | |
984 | return ret; | |
985 | } | |
80c7c2b0 | 986 | nb_queries = cpu_to_be32(nb_queries); |
2b53af25 | 987 | trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt), |
af736e54 | 988 | export_name, nb_queries); |
e7b1948d VSO |
989 | |
990 | if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) { | |
991 | /* enable all known contexts */ | |
992 | meta->base_allocation = true; | |
e31d8024 | 993 | meta->bitmap = !!meta->exp->export_bitmap; |
e7b1948d VSO |
994 | } else { |
995 | for (i = 0; i < nb_queries; ++i) { | |
996 | ret = nbd_negotiate_meta_query(client, meta, errp); | |
997 | if (ret <= 0) { | |
998 | return ret; | |
999 | } | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | if (meta->base_allocation) { | |
1004 | ret = nbd_negotiate_send_meta_context(client, "base:allocation", | |
1005 | NBD_META_ID_BASE_ALLOCATION, | |
1006 | errp); | |
1007 | if (ret < 0) { | |
1008 | return ret; | |
1009 | } | |
1010 | } | |
1011 | ||
3d068aff VSO |
1012 | if (meta->bitmap) { |
1013 | ret = nbd_negotiate_send_meta_context(client, | |
1014 | meta->exp->export_bitmap_context, | |
1015 | NBD_META_ID_DIRTY_BITMAP, | |
1016 | errp); | |
1017 | if (ret < 0) { | |
1018 | return ret; | |
1019 | } | |
1020 | } | |
1021 | ||
e7b1948d VSO |
1022 | ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); |
1023 | if (ret == 0) { | |
1024 | meta->valid = true; | |
1025 | } | |
1026 | ||
1027 | return ret; | |
1028 | } | |
1029 | ||
1e120ffe | 1030 | /* nbd_negotiate_options |
f37708f6 EB |
1031 | * Process all NBD_OPT_* client option commands, during fixed newstyle |
1032 | * negotiation. | |
1e120ffe | 1033 | * Return: |
2fd2c840 VSO |
1034 | * -errno on error, errp is set |
1035 | * 0 on successful negotiation, errp is not set | |
1036 | * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect, | |
1037 | * errp is not set | |
1e120ffe | 1038 | */ |
23e099c3 EB |
1039 | static int nbd_negotiate_options(NBDClient *client, uint16_t myflags, |
1040 | Error **errp) | |
f5076b5a | 1041 | { |
9c122ada | 1042 | uint32_t flags; |
26afa868 | 1043 | bool fixedNewstyle = false; |
23e099c3 | 1044 | bool no_zeroes = false; |
9c122ada HR |
1045 | |
1046 | /* Client sends: | |
1047 | [ 0 .. 3] client flags | |
1048 | ||
f37708f6 | 1049 | Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO: |
9c122ada HR |
1050 | [ 0 .. 7] NBD_OPTS_MAGIC |
1051 | [ 8 .. 11] NBD option | |
1052 | [12 .. 15] Data length | |
1053 | ... Rest of request | |
1054 | ||
1055 | [ 0 .. 7] NBD_OPTS_MAGIC | |
1056 | [ 8 .. 11] Second NBD option | |
1057 | [12 .. 15] Data length | |
1058 | ... Rest of request | |
1059 | */ | |
1060 | ||
e6798f06 | 1061 | if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) { |
9c122ada HR |
1062 | return -EIO; |
1063 | } | |
621c4f4e | 1064 | trace_nbd_negotiate_options_flags(flags); |
26afa868 | 1065 | if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) { |
26afa868 DB |
1066 | fixedNewstyle = true; |
1067 | flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE; | |
1068 | } | |
c203c59a | 1069 | if (flags & NBD_FLAG_C_NO_ZEROES) { |
23e099c3 | 1070 | no_zeroes = true; |
c203c59a EB |
1071 | flags &= ~NBD_FLAG_C_NO_ZEROES; |
1072 | } | |
26afa868 | 1073 | if (flags != 0) { |
2fd2c840 | 1074 | error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags); |
621c4f4e | 1075 | return -EINVAL; |
9c122ada HR |
1076 | } |
1077 | ||
f5076b5a | 1078 | while (1) { |
9c122ada | 1079 | int ret; |
7f9039cd | 1080 | uint32_t option, length; |
f5076b5a HB |
1081 | uint64_t magic; |
1082 | ||
e6798f06 | 1083 | if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) { |
f5076b5a HB |
1084 | return -EINVAL; |
1085 | } | |
9588463e VSO |
1086 | trace_nbd_negotiate_options_check_magic(magic); |
1087 | if (magic != NBD_OPTS_MAGIC) { | |
2fd2c840 | 1088 | error_setg(errp, "Bad magic received"); |
f5076b5a HB |
1089 | return -EINVAL; |
1090 | } | |
1091 | ||
e6798f06 | 1092 | if (nbd_read32(client->ioc, &option, "option", errp) < 0) { |
f5076b5a HB |
1093 | return -EINVAL; |
1094 | } | |
0cfae925 | 1095 | client->opt = option; |
f5076b5a | 1096 | |
e6798f06 | 1097 | if (nbd_read32(client->ioc, &length, "option length", errp) < 0) { |
f5076b5a HB |
1098 | return -EINVAL; |
1099 | } | |
894e0280 | 1100 | assert(!client->optlen); |
0cfae925 | 1101 | client->optlen = length; |
f5076b5a | 1102 | |
fdad35ef EB |
1103 | if (length > NBD_MAX_BUFFER_SIZE) { |
1104 | error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)", | |
1105 | length, NBD_MAX_BUFFER_SIZE); | |
1106 | return -EINVAL; | |
1107 | } | |
1108 | ||
3736cc5b EB |
1109 | trace_nbd_negotiate_options_check_option(option, |
1110 | nbd_opt_lookup(option)); | |
f95910fe DB |
1111 | if (client->tlscreds && |
1112 | client->ioc == (QIOChannel *)client->sioc) { | |
1113 | QIOChannel *tioc; | |
1114 | if (!fixedNewstyle) { | |
7f9039cd | 1115 | error_setg(errp, "Unsupported option 0x%" PRIx32, option); |
f95910fe DB |
1116 | return -EINVAL; |
1117 | } | |
7f9039cd | 1118 | switch (option) { |
f95910fe | 1119 | case NBD_OPT_STARTTLS: |
e68c35cf EB |
1120 | if (length) { |
1121 | /* Unconditionally drop the connection if the client | |
1122 | * can't start a TLS negotiation correctly */ | |
0cfae925 | 1123 | return nbd_reject_length(client, true, errp); |
e68c35cf EB |
1124 | } |
1125 | tioc = nbd_negotiate_handle_starttls(client, errp); | |
f95910fe DB |
1126 | if (!tioc) { |
1127 | return -EIO; | |
1128 | } | |
8cbee49e | 1129 | ret = 0; |
f95910fe DB |
1130 | object_unref(OBJECT(client->ioc)); |
1131 | client->ioc = QIO_CHANNEL(tioc); | |
1132 | break; | |
1133 | ||
d1129a8a EB |
1134 | case NBD_OPT_EXPORT_NAME: |
1135 | /* No way to return an error to client, so drop connection */ | |
2fd2c840 | 1136 | error_setg(errp, "Option 0x%x not permitted before TLS", |
7f9039cd | 1137 | option); |
d1129a8a EB |
1138 | return -EINVAL; |
1139 | ||
f95910fe | 1140 | default: |
3e99ebb9 EB |
1141 | /* Let the client keep trying, unless they asked to |
1142 | * quit. Always try to give an error back to the | |
1143 | * client; but when replying to OPT_ABORT, be aware | |
1144 | * that the client may hang up before receiving the | |
1145 | * error, in which case we are fine ignoring the | |
1146 | * resulting EPIPE. */ | |
1147 | ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD, | |
1148 | option == NBD_OPT_ABORT ? NULL : errp, | |
894e0280 | 1149 | "Option 0x%" PRIx32 |
0b0bb124 | 1150 | " not permitted before TLS", option); |
7f9039cd | 1151 | if (option == NBD_OPT_ABORT) { |
1e120ffe | 1152 | return 1; |
b6f5d3b5 | 1153 | } |
d1129a8a | 1154 | break; |
f95910fe DB |
1155 | } |
1156 | } else if (fixedNewstyle) { | |
7f9039cd | 1157 | switch (option) { |
26afa868 | 1158 | case NBD_OPT_LIST: |
e68c35cf | 1159 | if (length) { |
0cfae925 | 1160 | ret = nbd_reject_length(client, false, errp); |
e68c35cf EB |
1161 | } else { |
1162 | ret = nbd_negotiate_handle_list(client, errp); | |
1163 | } | |
26afa868 DB |
1164 | break; |
1165 | ||
1166 | case NBD_OPT_ABORT: | |
b6f5d3b5 EB |
1167 | /* NBD spec says we must try to reply before |
1168 | * disconnecting, but that we must also tolerate | |
1169 | * guests that don't wait for our reply. */ | |
0cfae925 | 1170 | nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL); |
1e120ffe | 1171 | return 1; |
26afa868 DB |
1172 | |
1173 | case NBD_OPT_EXPORT_NAME: | |
0cfae925 | 1174 | return nbd_negotiate_handle_export_name(client, |
23e099c3 EB |
1175 | myflags, no_zeroes, |
1176 | errp); | |
26afa868 | 1177 | |
f37708f6 EB |
1178 | case NBD_OPT_INFO: |
1179 | case NBD_OPT_GO: | |
0cfae925 | 1180 | ret = nbd_negotiate_handle_info(client, myflags, errp); |
f37708f6 EB |
1181 | if (ret == 1) { |
1182 | assert(option == NBD_OPT_GO); | |
1183 | return 0; | |
1184 | } | |
f37708f6 EB |
1185 | break; |
1186 | ||
f95910fe | 1187 | case NBD_OPT_STARTTLS: |
e68c35cf | 1188 | if (length) { |
0cfae925 | 1189 | ret = nbd_reject_length(client, false, errp); |
e68c35cf | 1190 | } else if (client->tlscreds) { |
0cfae925 VSO |
1191 | ret = nbd_negotiate_send_rep_err(client, |
1192 | NBD_REP_ERR_INVALID, errp, | |
36683283 | 1193 | "TLS already enabled"); |
f95910fe | 1194 | } else { |
0cfae925 VSO |
1195 | ret = nbd_negotiate_send_rep_err(client, |
1196 | NBD_REP_ERR_POLICY, errp, | |
36683283 | 1197 | "TLS not configured"); |
63d5ef86 | 1198 | } |
d1129a8a | 1199 | break; |
5c54e7fa VSO |
1200 | |
1201 | case NBD_OPT_STRUCTURED_REPLY: | |
1202 | if (length) { | |
0cfae925 | 1203 | ret = nbd_reject_length(client, false, errp); |
5c54e7fa VSO |
1204 | } else if (client->structured_reply) { |
1205 | ret = nbd_negotiate_send_rep_err( | |
0cfae925 | 1206 | client, NBD_REP_ERR_INVALID, errp, |
5c54e7fa VSO |
1207 | "structured reply already negotiated"); |
1208 | } else { | |
0cfae925 | 1209 | ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); |
5c54e7fa VSO |
1210 | client->structured_reply = true; |
1211 | myflags |= NBD_FLAG_SEND_DF; | |
1212 | } | |
1213 | break; | |
1214 | ||
e7b1948d VSO |
1215 | case NBD_OPT_LIST_META_CONTEXT: |
1216 | case NBD_OPT_SET_META_CONTEXT: | |
1217 | ret = nbd_negotiate_meta_queries(client, &client->export_meta, | |
1218 | errp); | |
1219 | break; | |
1220 | ||
26afa868 | 1221 | default: |
894e0280 | 1222 | ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp, |
28fb494f | 1223 | "Unsupported option %" PRIu32 " (%s)", |
894e0280 | 1224 | option, nbd_opt_lookup(option)); |
156f6a10 | 1225 | break; |
26afa868 DB |
1226 | } |
1227 | } else { | |
1228 | /* | |
1229 | * If broken new-style we should drop the connection | |
1230 | * for anything except NBD_OPT_EXPORT_NAME | |
1231 | */ | |
7f9039cd | 1232 | switch (option) { |
26afa868 | 1233 | case NBD_OPT_EXPORT_NAME: |
0cfae925 | 1234 | return nbd_negotiate_handle_export_name(client, |
23e099c3 EB |
1235 | myflags, no_zeroes, |
1236 | errp); | |
26afa868 DB |
1237 | |
1238 | default: | |
28fb494f | 1239 | error_setg(errp, "Unsupported option %" PRIu32 " (%s)", |
3736cc5b | 1240 | option, nbd_opt_lookup(option)); |
26afa868 | 1241 | return -EINVAL; |
32d7d2e0 | 1242 | } |
f5076b5a | 1243 | } |
8cbee49e EB |
1244 | if (ret < 0) { |
1245 | return ret; | |
1246 | } | |
f5076b5a HB |
1247 | } |
1248 | } | |
1249 | ||
1e120ffe VSO |
1250 | /* nbd_negotiate |
1251 | * Return: | |
2fd2c840 VSO |
1252 | * -errno on error, errp is set |
1253 | * 0 on successful negotiation, errp is not set | |
1254 | * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect, | |
1255 | * errp is not set | |
1e120ffe | 1256 | */ |
2fd2c840 | 1257 | static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp) |
7a5ca864 | 1258 | { |
5f66d060 | 1259 | char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = ""; |
2e5c9ad6 | 1260 | int ret; |
7423f417 | 1261 | const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | |
1f4d6d18 | 1262 | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA | |
bc37b06a | 1263 | NBD_FLAG_SEND_WRITE_ZEROES | NBD_FLAG_SEND_CACHE); |
b2e3d87f | 1264 | |
5f66d060 | 1265 | /* Old style negotiation header, no room for options |
6b8c01e7 PB |
1266 | [ 0 .. 7] passwd ("NBDMAGIC") |
1267 | [ 8 .. 15] magic (NBD_CLIENT_MAGIC) | |
b2e3d87f | 1268 | [16 .. 23] size |
5f66d060 | 1269 | [24 .. 27] export flags (zero-extended) |
6b8c01e7 PB |
1270 | [28 .. 151] reserved (0) |
1271 | ||
5f66d060 | 1272 | New style negotiation header, client can send options |
6b8c01e7 PB |
1273 | [ 0 .. 7] passwd ("NBDMAGIC") |
1274 | [ 8 .. 15] magic (NBD_OPTS_MAGIC) | |
1275 | [16 .. 17] server flags (0) | |
f37708f6 | 1276 | ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO.... |
b2e3d87f NT |
1277 | */ |
1278 | ||
1c778ef7 | 1279 | qio_channel_set_blocking(client->ioc, false, NULL); |
185b4338 | 1280 | |
9588463e | 1281 | trace_nbd_negotiate_begin(); |
b2e3d87f | 1282 | memcpy(buf, "NBDMAGIC", 8); |
f95910fe | 1283 | |
7f7dfe2a VSO |
1284 | stq_be_p(buf + 8, NBD_OPTS_MAGIC); |
1285 | stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES); | |
b2e3d87f | 1286 | |
7f7dfe2a VSO |
1287 | if (nbd_write(client->ioc, buf, 18, errp) < 0) { |
1288 | error_prepend(errp, "write failed: "); | |
1289 | return -EINVAL; | |
1290 | } | |
1291 | ret = nbd_negotiate_options(client, myflags, errp); | |
1292 | if (ret != 0) { | |
1293 | if (ret < 0) { | |
1294 | error_prepend(errp, "option negotiation failed: "); | |
6b8c01e7 | 1295 | } |
7f7dfe2a | 1296 | return ret; |
b2e3d87f NT |
1297 | } |
1298 | ||
0cfae925 | 1299 | assert(!client->optlen); |
9588463e | 1300 | trace_nbd_negotiate_success(); |
d9faeed8 VSO |
1301 | |
1302 | return 0; | |
7a5ca864 FB |
1303 | } |
1304 | ||
2fd2c840 VSO |
1305 | static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request, |
1306 | Error **errp) | |
75818250 | 1307 | { |
fa26c26b | 1308 | uint8_t buf[NBD_REQUEST_SIZE]; |
b2e3d87f | 1309 | uint32_t magic; |
a0dc63a6 | 1310 | int ret; |
b2e3d87f | 1311 | |
e6798f06 | 1312 | ret = nbd_read(ioc, buf, sizeof(buf), "request", errp); |
185b4338 PB |
1313 | if (ret < 0) { |
1314 | return ret; | |
1315 | } | |
1316 | ||
b2e3d87f NT |
1317 | /* Request |
1318 | [ 0 .. 3] magic (NBD_REQUEST_MAGIC) | |
b626b51a EB |
1319 | [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) |
1320 | [ 6 .. 7] type (NBD_CMD_READ, ...) | |
b2e3d87f NT |
1321 | [ 8 .. 15] handle |
1322 | [16 .. 23] from | |
1323 | [24 .. 27] len | |
1324 | */ | |
1325 | ||
773dce3c | 1326 | magic = ldl_be_p(buf); |
b626b51a EB |
1327 | request->flags = lduw_be_p(buf + 4); |
1328 | request->type = lduw_be_p(buf + 6); | |
773dce3c PM |
1329 | request->handle = ldq_be_p(buf + 8); |
1330 | request->from = ldq_be_p(buf + 16); | |
1331 | request->len = ldl_be_p(buf + 24); | |
b2e3d87f | 1332 | |
9588463e VSO |
1333 | trace_nbd_receive_request(magic, request->flags, request->type, |
1334 | request->from, request->len); | |
b2e3d87f NT |
1335 | |
1336 | if (magic != NBD_REQUEST_MAGIC) { | |
2fd2c840 | 1337 | error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic); |
185b4338 | 1338 | return -EINVAL; |
b2e3d87f NT |
1339 | } |
1340 | return 0; | |
75818250 TS |
1341 | } |
1342 | ||
41996e38 PB |
1343 | #define MAX_NBD_REQUESTS 16 |
1344 | ||
ce33967a | 1345 | void nbd_client_get(NBDClient *client) |
1743b515 PB |
1346 | { |
1347 | client->refcount++; | |
1348 | } | |
1349 | ||
ce33967a | 1350 | void nbd_client_put(NBDClient *client) |
1743b515 PB |
1351 | { |
1352 | if (--client->refcount == 0) { | |
ff2b68aa | 1353 | /* The last reference should be dropped by client->close, |
f53a829b | 1354 | * which is called by client_close. |
ff2b68aa PB |
1355 | */ |
1356 | assert(client->closing); | |
1357 | ||
ff82911c | 1358 | qio_channel_detach_aio_context(client->ioc); |
1c778ef7 DB |
1359 | object_unref(OBJECT(client->sioc)); |
1360 | object_unref(OBJECT(client->ioc)); | |
f95910fe DB |
1361 | if (client->tlscreds) { |
1362 | object_unref(OBJECT(client->tlscreds)); | |
1363 | } | |
b25e12da | 1364 | g_free(client->tlsauthz); |
6b8c01e7 PB |
1365 | if (client->exp) { |
1366 | QTAILQ_REMOVE(&client->exp->clients, client, next); | |
1367 | nbd_export_put(client->exp); | |
1368 | } | |
1743b515 PB |
1369 | g_free(client); |
1370 | } | |
1371 | } | |
1372 | ||
0c9390d9 | 1373 | static void client_close(NBDClient *client, bool negotiated) |
1743b515 | 1374 | { |
ff2b68aa PB |
1375 | if (client->closing) { |
1376 | return; | |
1377 | } | |
1378 | ||
1379 | client->closing = true; | |
1380 | ||
1381 | /* Force requests to finish. They will drop their own references, | |
1382 | * then we'll close the socket and free the NBDClient. | |
1383 | */ | |
1c778ef7 DB |
1384 | qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, |
1385 | NULL); | |
ff2b68aa PB |
1386 | |
1387 | /* Also tell the client, so that they release their reference. */ | |
0c9390d9 EB |
1388 | if (client->close_fn) { |
1389 | client->close_fn(client, negotiated); | |
1743b515 | 1390 | } |
1743b515 PB |
1391 | } |
1392 | ||
315f78ab | 1393 | static NBDRequestData *nbd_request_get(NBDClient *client) |
d9a73806 | 1394 | { |
315f78ab | 1395 | NBDRequestData *req; |
72deddc5 | 1396 | |
41996e38 PB |
1397 | assert(client->nb_requests <= MAX_NBD_REQUESTS - 1); |
1398 | client->nb_requests++; | |
1399 | ||
315f78ab | 1400 | req = g_new0(NBDRequestData, 1); |
72deddc5 PB |
1401 | nbd_client_get(client); |
1402 | req->client = client; | |
d9a73806 PB |
1403 | return req; |
1404 | } | |
1405 | ||
315f78ab | 1406 | static void nbd_request_put(NBDRequestData *req) |
d9a73806 | 1407 | { |
72deddc5 | 1408 | NBDClient *client = req->client; |
e1adb27a | 1409 | |
2d821488 SH |
1410 | if (req->data) { |
1411 | qemu_vfree(req->data); | |
1412 | } | |
1729404c | 1413 | g_free(req); |
e1adb27a | 1414 | |
958c717d | 1415 | client->nb_requests--; |
ff82911c PB |
1416 | nbd_client_receive_next_request(client); |
1417 | ||
72deddc5 | 1418 | nbd_client_put(client); |
d9a73806 PB |
1419 | } |
1420 | ||
aadf99a7 | 1421 | static void blk_aio_attached(AioContext *ctx, void *opaque) |
f2149281 HR |
1422 | { |
1423 | NBDExport *exp = opaque; | |
1424 | NBDClient *client; | |
1425 | ||
9588463e | 1426 | trace_nbd_blk_aio_attached(exp->name, ctx); |
f2149281 HR |
1427 | |
1428 | exp->ctx = ctx; | |
1429 | ||
1430 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
ff82911c PB |
1431 | qio_channel_attach_aio_context(client->ioc, ctx); |
1432 | if (client->recv_coroutine) { | |
1433 | aio_co_schedule(ctx, client->recv_coroutine); | |
1434 | } | |
1435 | if (client->send_coroutine) { | |
1436 | aio_co_schedule(ctx, client->send_coroutine); | |
1437 | } | |
f2149281 HR |
1438 | } |
1439 | } | |
1440 | ||
aadf99a7 | 1441 | static void blk_aio_detach(void *opaque) |
f2149281 HR |
1442 | { |
1443 | NBDExport *exp = opaque; | |
1444 | NBDClient *client; | |
1445 | ||
9588463e | 1446 | trace_nbd_blk_aio_detach(exp->name, exp->ctx); |
f2149281 HR |
1447 | |
1448 | QTAILQ_FOREACH(client, &exp->clients, next) { | |
ff82911c | 1449 | qio_channel_detach_aio_context(client->ioc); |
f2149281 HR |
1450 | } |
1451 | ||
1452 | exp->ctx = NULL; | |
1453 | } | |
1454 | ||
741cc431 HR |
1455 | static void nbd_eject_notifier(Notifier *n, void *data) |
1456 | { | |
1457 | NBDExport *exp = container_of(n, NBDExport, eject_notifier); | |
1458 | nbd_export_close(exp); | |
1459 | } | |
1460 | ||
9d26dfcb EB |
1461 | NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset, |
1462 | uint64_t size, const char *name, const char *desc, | |
678ba275 EB |
1463 | const char *bitmap, uint16_t nbdflags, |
1464 | void (*close)(NBDExport *), bool writethrough, | |
1465 | BlockBackend *on_eject_blk, Error **errp) | |
af49bbbe | 1466 | { |
3dff24f2 | 1467 | AioContext *ctx; |
cd7fca95 | 1468 | BlockBackend *blk; |
e8d3eb74 | 1469 | NBDExport *exp = g_new0(NBDExport, 1); |
8a7ce4f9 | 1470 | uint64_t perm; |
d7086422 | 1471 | int ret; |
cd7fca95 | 1472 | |
3dff24f2 KW |
1473 | /* |
1474 | * NBD exports are used for non-shared storage migration. Make sure | |
1475 | * that BDRV_O_INACTIVE is cleared and the image is ready for write | |
1476 | * access since the export could be available before migration handover. | |
1477 | */ | |
3fa4c765 | 1478 | assert(name); |
3dff24f2 KW |
1479 | ctx = bdrv_get_aio_context(bs); |
1480 | aio_context_acquire(ctx); | |
1481 | bdrv_invalidate_cache(bs, NULL); | |
1482 | aio_context_release(ctx); | |
1483 | ||
8a7ce4f9 KW |
1484 | /* Don't allow resize while the NBD server is running, otherwise we don't |
1485 | * care what happens with the node. */ | |
1486 | perm = BLK_PERM_CONSISTENT_READ; | |
1487 | if ((nbdflags & NBD_FLAG_READ_ONLY) == 0) { | |
1488 | perm |= BLK_PERM_WRITE; | |
1489 | } | |
d861ab3a KW |
1490 | blk = blk_new(bdrv_get_aio_context(bs), perm, |
1491 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | | |
1492 | BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD); | |
d7086422 KW |
1493 | ret = blk_insert_bs(blk, bs, errp); |
1494 | if (ret < 0) { | |
1495 | goto fail; | |
1496 | } | |
cd7fca95 | 1497 | blk_set_enable_write_cache(blk, !writethrough); |
45e92a90 | 1498 | blk_set_allow_aio_context_change(blk, true); |
cd7fca95 | 1499 | |
2c8d9f06 | 1500 | exp->refcount = 1; |
4b9441f6 | 1501 | QTAILQ_INIT(&exp->clients); |
aadf99a7 | 1502 | exp->blk = blk; |
9d26dfcb | 1503 | assert(dev_offset <= INT64_MAX); |
af49bbbe | 1504 | exp->dev_offset = dev_offset; |
3fa4c765 | 1505 | exp->name = g_strdup(name); |
9d26dfcb | 1506 | exp->description = g_strdup(desc); |
af49bbbe | 1507 | exp->nbdflags = nbdflags; |
9d26dfcb | 1508 | assert(size <= INT64_MAX - dev_offset); |
7596bbb3 | 1509 | exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE); |
98f44bbe | 1510 | |
678ba275 EB |
1511 | if (bitmap) { |
1512 | BdrvDirtyBitmap *bm = NULL; | |
678ba275 EB |
1513 | |
1514 | while (true) { | |
1515 | bm = bdrv_find_dirty_bitmap(bs, bitmap); | |
1516 | if (bm != NULL || bs->backing == NULL) { | |
1517 | break; | |
1518 | } | |
1519 | ||
1520 | bs = bs->backing->bs; | |
1521 | } | |
1522 | ||
1523 | if (bm == NULL) { | |
1524 | error_setg(errp, "Bitmap '%s' is not found", bitmap); | |
1525 | goto fail; | |
1526 | } | |
1527 | ||
3ae96d66 | 1528 | if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) { |
3b78a927 JS |
1529 | goto fail; |
1530 | } | |
1531 | ||
678ba275 EB |
1532 | if ((nbdflags & NBD_FLAG_READ_ONLY) && bdrv_is_writable(bs) && |
1533 | bdrv_dirty_bitmap_enabled(bm)) { | |
1534 | error_setg(errp, | |
1535 | "Enabled bitmap '%s' incompatible with readonly export", | |
1536 | bitmap); | |
1537 | goto fail; | |
1538 | } | |
1539 | ||
27a1b301 | 1540 | bdrv_dirty_bitmap_set_busy(bm, true); |
678ba275 EB |
1541 | exp->export_bitmap = bm; |
1542 | exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s", | |
1543 | bitmap); | |
1544 | } | |
1545 | ||
0ddf08db | 1546 | exp->close = close; |
aadf99a7 | 1547 | exp->ctx = blk_get_aio_context(blk); |
aadf99a7 | 1548 | blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp); |
741cc431 | 1549 | |
cd7fca95 KW |
1550 | if (on_eject_blk) { |
1551 | blk_ref(on_eject_blk); | |
1552 | exp->eject_notifier_blk = on_eject_blk; | |
1553 | exp->eject_notifier.notify = nbd_eject_notifier; | |
1554 | blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier); | |
1555 | } | |
3fa4c765 EB |
1556 | QTAILQ_INSERT_TAIL(&exports, exp, next); |
1557 | nbd_export_get(exp); | |
af49bbbe | 1558 | return exp; |
98f44bbe HR |
1559 | |
1560 | fail: | |
cd7fca95 | 1561 | blk_unref(blk); |
3fa4c765 EB |
1562 | g_free(exp->name); |
1563 | g_free(exp->description); | |
98f44bbe HR |
1564 | g_free(exp); |
1565 | return NULL; | |
af49bbbe PB |
1566 | } |
1567 | ||
ee0a19ec PB |
1568 | NBDExport *nbd_export_find(const char *name) |
1569 | { | |
1570 | NBDExport *exp; | |
1571 | QTAILQ_FOREACH(exp, &exports, next) { | |
1572 | if (strcmp(name, exp->name) == 0) { | |
1573 | return exp; | |
1574 | } | |
1575 | } | |
1576 | ||
1577 | return NULL; | |
1578 | } | |
1579 | ||
af49bbbe PB |
1580 | void nbd_export_close(NBDExport *exp) |
1581 | { | |
4b9441f6 | 1582 | NBDClient *client, *next; |
2c8d9f06 | 1583 | |
4b9441f6 | 1584 | nbd_export_get(exp); |
3fa4c765 EB |
1585 | /* |
1586 | * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a | |
1587 | * close mode that stops advertising the export to new clients but | |
1588 | * still permits existing clients to run to completion? Because of | |
1589 | * that possibility, nbd_export_close() can be called more than | |
1590 | * once on an export. | |
1591 | */ | |
4b9441f6 | 1592 | QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) { |
0c9390d9 | 1593 | client_close(client, true); |
4b9441f6 | 1594 | } |
3fa4c765 EB |
1595 | if (exp->name) { |
1596 | nbd_export_put(exp); | |
1597 | g_free(exp->name); | |
1598 | exp->name = NULL; | |
1599 | QTAILQ_REMOVE(&exports, exp, next); | |
1600 | } | |
1601 | g_free(exp->description); | |
1602 | exp->description = NULL; | |
4b9441f6 | 1603 | nbd_export_put(exp); |
2c8d9f06 PB |
1604 | } |
1605 | ||
a3b0dc75 VSO |
1606 | void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp) |
1607 | { | |
1608 | if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) { | |
1609 | nbd_export_close(exp); | |
1610 | return; | |
1611 | } | |
1612 | ||
1613 | assert(mode == NBD_SERVER_REMOVE_MODE_SAFE); | |
1614 | ||
1615 | error_setg(errp, "export '%s' still in use", exp->name); | |
1616 | error_append_hint(errp, "Use mode='hard' to force client disconnect\n"); | |
1617 | } | |
1618 | ||
2c8d9f06 PB |
1619 | void nbd_export_get(NBDExport *exp) |
1620 | { | |
1621 | assert(exp->refcount > 0); | |
1622 | exp->refcount++; | |
1623 | } | |
1624 | ||
1625 | void nbd_export_put(NBDExport *exp) | |
1626 | { | |
1627 | assert(exp->refcount > 0); | |
1628 | if (exp->refcount == 1) { | |
1629 | nbd_export_close(exp); | |
d9a73806 PB |
1630 | } |
1631 | ||
9156245e VSO |
1632 | /* nbd_export_close() may theoretically reduce refcount to 0. It may happen |
1633 | * if someone calls nbd_export_put() on named export not through | |
1634 | * nbd_export_set_name() when refcount is 1. So, let's assert that | |
1635 | * it is > 0. | |
1636 | */ | |
1637 | assert(exp->refcount > 0); | |
2c8d9f06 | 1638 | if (--exp->refcount == 0) { |
ee0a19ec | 1639 | assert(exp->name == NULL); |
b1a75b33 | 1640 | assert(exp->description == NULL); |
ee0a19ec | 1641 | |
0ddf08db PB |
1642 | if (exp->close) { |
1643 | exp->close(exp); | |
1644 | } | |
1645 | ||
d6268348 | 1646 | if (exp->blk) { |
cd7fca95 KW |
1647 | if (exp->eject_notifier_blk) { |
1648 | notifier_remove(&exp->eject_notifier); | |
1649 | blk_unref(exp->eject_notifier_blk); | |
1650 | } | |
d6268348 WC |
1651 | blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, |
1652 | blk_aio_detach, exp); | |
1653 | blk_unref(exp->blk); | |
1654 | exp->blk = NULL; | |
1655 | } | |
1656 | ||
3d068aff | 1657 | if (exp->export_bitmap) { |
27a1b301 | 1658 | bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false); |
3d068aff VSO |
1659 | g_free(exp->export_bitmap_context); |
1660 | } | |
1661 | ||
2c8d9f06 PB |
1662 | g_free(exp); |
1663 | } | |
af49bbbe PB |
1664 | } |
1665 | ||
e140177d | 1666 | BlockBackend *nbd_export_get_blockdev(NBDExport *exp) |
125afda8 | 1667 | { |
aadf99a7 | 1668 | return exp->blk; |
125afda8 PB |
1669 | } |
1670 | ||
ee0a19ec PB |
1671 | void nbd_export_close_all(void) |
1672 | { | |
1673 | NBDExport *exp, *next; | |
1674 | ||
1675 | QTAILQ_FOREACH_SAFE(exp, &exports, next, next) { | |
1676 | nbd_export_close(exp); | |
ee0a19ec PB |
1677 | } |
1678 | } | |
1679 | ||
de79bfc3 VSO |
1680 | static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov, |
1681 | unsigned niov, Error **errp) | |
1682 | { | |
1683 | int ret; | |
1684 | ||
1685 | g_assert(qemu_in_coroutine()); | |
1686 | qemu_co_mutex_lock(&client->send_lock); | |
1687 | client->send_coroutine = qemu_coroutine_self(); | |
1688 | ||
1689 | ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0; | |
1690 | ||
1691 | client->send_coroutine = NULL; | |
1692 | qemu_co_mutex_unlock(&client->send_lock); | |
1693 | ||
1694 | return ret; | |
1695 | } | |
1696 | ||
caad5384 VSO |
1697 | static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error, |
1698 | uint64_t handle) | |
1699 | { | |
1700 | stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC); | |
1701 | stl_be_p(&reply->error, error); | |
1702 | stq_be_p(&reply->handle, handle); | |
1703 | } | |
1704 | ||
978df1b6 | 1705 | static int nbd_co_send_simple_reply(NBDClient *client, |
14cea41d VSO |
1706 | uint64_t handle, |
1707 | uint32_t error, | |
978df1b6 VSO |
1708 | void *data, |
1709 | size_t len, | |
1710 | Error **errp) | |
22045592 | 1711 | { |
de79bfc3 | 1712 | NBDSimpleReply reply; |
14cea41d | 1713 | int nbd_err = system_errno_to_nbd_errno(error); |
de79bfc3 VSO |
1714 | struct iovec iov[] = { |
1715 | {.iov_base = &reply, .iov_len = sizeof(reply)}, | |
1716 | {.iov_base = data, .iov_len = len} | |
1717 | }; | |
6fb2b972 | 1718 | |
e7a78d0e EB |
1719 | trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err), |
1720 | len); | |
de79bfc3 | 1721 | set_be_simple_reply(&reply, nbd_err, handle); |
262db388 | 1722 | |
de79bfc3 | 1723 | return nbd_co_send_iov(client, iov, len ? 2 : 1, errp); |
22045592 PB |
1724 | } |
1725 | ||
5c54e7fa VSO |
1726 | static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags, |
1727 | uint16_t type, uint64_t handle, uint32_t length) | |
1728 | { | |
1729 | stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); | |
1730 | stw_be_p(&chunk->flags, flags); | |
1731 | stw_be_p(&chunk->type, type); | |
1732 | stq_be_p(&chunk->handle, handle); | |
1733 | stl_be_p(&chunk->length, length); | |
1734 | } | |
1735 | ||
ef8c887e EB |
1736 | static int coroutine_fn nbd_co_send_structured_done(NBDClient *client, |
1737 | uint64_t handle, | |
1738 | Error **errp) | |
1739 | { | |
1740 | NBDStructuredReplyChunk chunk; | |
1741 | struct iovec iov[] = { | |
1742 | {.iov_base = &chunk, .iov_len = sizeof(chunk)}, | |
1743 | }; | |
1744 | ||
1745 | trace_nbd_co_send_structured_done(handle); | |
1746 | set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0); | |
1747 | ||
1748 | return nbd_co_send_iov(client, iov, 1, errp); | |
1749 | } | |
1750 | ||
5c54e7fa VSO |
1751 | static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, |
1752 | uint64_t handle, | |
1753 | uint64_t offset, | |
1754 | void *data, | |
1755 | size_t size, | |
418638d3 | 1756 | bool final, |
5c54e7fa VSO |
1757 | Error **errp) |
1758 | { | |
efdc0c10 | 1759 | NBDStructuredReadData chunk; |
5c54e7fa VSO |
1760 | struct iovec iov[] = { |
1761 | {.iov_base = &chunk, .iov_len = sizeof(chunk)}, | |
1762 | {.iov_base = data, .iov_len = size} | |
1763 | }; | |
1764 | ||
ef8c887e | 1765 | assert(size); |
5c54e7fa | 1766 | trace_nbd_co_send_structured_read(handle, offset, data, size); |
418638d3 EB |
1767 | set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0, |
1768 | NBD_REPLY_TYPE_OFFSET_DATA, handle, | |
1769 | sizeof(chunk) - sizeof(chunk.h) + size); | |
5c54e7fa VSO |
1770 | stq_be_p(&chunk.offset, offset); |
1771 | ||
1772 | return nbd_co_send_iov(client, iov, 2, errp); | |
1773 | } | |
1774 | ||
60ace2ba VSO |
1775 | static int coroutine_fn nbd_co_send_structured_error(NBDClient *client, |
1776 | uint64_t handle, | |
1777 | uint32_t error, | |
1778 | const char *msg, | |
1779 | Error **errp) | |
1780 | { | |
1781 | NBDStructuredError chunk; | |
1782 | int nbd_err = system_errno_to_nbd_errno(error); | |
1783 | struct iovec iov[] = { | |
1784 | {.iov_base = &chunk, .iov_len = sizeof(chunk)}, | |
1785 | {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0}, | |
1786 | }; | |
1787 | ||
1788 | assert(nbd_err); | |
1789 | trace_nbd_co_send_structured_error(handle, nbd_err, | |
1790 | nbd_err_lookup(nbd_err), msg ? msg : ""); | |
1791 | set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle, | |
1792 | sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len); | |
1793 | stl_be_p(&chunk.error, nbd_err); | |
1794 | stw_be_p(&chunk.message_length, iov[1].iov_len); | |
1795 | ||
1796 | return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp); | |
1797 | } | |
1798 | ||
37e02aeb VSO |
1799 | /* Do a sparse read and send the structured reply to the client. |
1800 | * Returns -errno if sending fails. bdrv_block_status_above() failure is | |
1801 | * reported to the client, at which point this function succeeds. | |
1802 | */ | |
418638d3 EB |
1803 | static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, |
1804 | uint64_t handle, | |
1805 | uint64_t offset, | |
1806 | uint8_t *data, | |
1807 | size_t size, | |
1808 | Error **errp) | |
1809 | { | |
1810 | int ret = 0; | |
1811 | NBDExport *exp = client->exp; | |
1812 | size_t progress = 0; | |
1813 | ||
1814 | while (progress < size) { | |
1815 | int64_t pnum; | |
1816 | int status = bdrv_block_status_above(blk_bs(exp->blk), NULL, | |
1817 | offset + progress, | |
1818 | size - progress, &pnum, NULL, | |
1819 | NULL); | |
e2de3256 | 1820 | bool final; |
418638d3 EB |
1821 | |
1822 | if (status < 0) { | |
37e02aeb VSO |
1823 | char *msg = g_strdup_printf("unable to check for holes: %s", |
1824 | strerror(-status)); | |
1825 | ||
1826 | ret = nbd_co_send_structured_error(client, handle, -status, msg, | |
1827 | errp); | |
1828 | g_free(msg); | |
1829 | return ret; | |
418638d3 EB |
1830 | } |
1831 | assert(pnum && pnum <= size - progress); | |
e2de3256 | 1832 | final = progress + pnum == size; |
418638d3 EB |
1833 | if (status & BDRV_BLOCK_ZERO) { |
1834 | NBDStructuredReadHole chunk; | |
1835 | struct iovec iov[] = { | |
1836 | {.iov_base = &chunk, .iov_len = sizeof(chunk)}, | |
1837 | }; | |
1838 | ||
1839 | trace_nbd_co_send_structured_read_hole(handle, offset + progress, | |
1840 | pnum); | |
e2de3256 EB |
1841 | set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0, |
1842 | NBD_REPLY_TYPE_OFFSET_HOLE, | |
418638d3 EB |
1843 | handle, sizeof(chunk) - sizeof(chunk.h)); |
1844 | stq_be_p(&chunk.offset, offset + progress); | |
1845 | stl_be_p(&chunk.length, pnum); | |
1846 | ret = nbd_co_send_iov(client, iov, 1, errp); | |
1847 | } else { | |
1848 | ret = blk_pread(exp->blk, offset + progress + exp->dev_offset, | |
1849 | data + progress, pnum); | |
1850 | if (ret < 0) { | |
1851 | error_setg_errno(errp, -ret, "reading from file failed"); | |
1852 | break; | |
1853 | } | |
1854 | ret = nbd_co_send_structured_read(client, handle, offset + progress, | |
e2de3256 | 1855 | data + progress, pnum, final, |
418638d3 EB |
1856 | errp); |
1857 | } | |
1858 | ||
1859 | if (ret < 0) { | |
1860 | break; | |
1861 | } | |
1862 | progress += pnum; | |
1863 | } | |
418638d3 EB |
1864 | return ret; |
1865 | } | |
1866 | ||
fb7afc79 VSO |
1867 | /* |
1868 | * Populate @extents from block status. Update @bytes to be the actual | |
1869 | * length encoded (which may be smaller than the original), and update | |
1870 | * @nb_extents to the number of extents used. | |
1871 | * | |
1872 | * Returns zero on success and -errno on bdrv_block_status_above failure. | |
1873 | */ | |
1874 | static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset, | |
1875 | uint64_t *bytes, NBDExtent *extents, | |
1876 | unsigned int *nb_extents) | |
e7b1948d | 1877 | { |
fb7afc79 VSO |
1878 | uint64_t remaining_bytes = *bytes; |
1879 | NBDExtent *extent = extents, *extents_end = extents + *nb_extents; | |
1880 | bool first_extent = true; | |
e7b1948d | 1881 | |
fb7afc79 | 1882 | assert(*nb_extents); |
e7b1948d VSO |
1883 | while (remaining_bytes) { |
1884 | uint32_t flags; | |
1885 | int64_t num; | |
1886 | int ret = bdrv_block_status_above(bs, NULL, offset, remaining_bytes, | |
1887 | &num, NULL, NULL); | |
fb7afc79 | 1888 | |
e7b1948d VSO |
1889 | if (ret < 0) { |
1890 | return ret; | |
1891 | } | |
1892 | ||
1893 | flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) | | |
1894 | (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0); | |
1895 | ||
fb7afc79 | 1896 | if (first_extent) { |
e7b1948d | 1897 | extent->flags = flags; |
fb7afc79 VSO |
1898 | extent->length = num; |
1899 | first_extent = false; | |
2178a569 | 1900 | } else if (flags == extent->flags) { |
fb7afc79 VSO |
1901 | /* extend current extent */ |
1902 | extent->length += num; | |
1903 | } else { | |
1904 | if (extent + 1 == extents_end) { | |
1905 | break; | |
1906 | } | |
1907 | ||
1908 | /* start new extent */ | |
1909 | extent++; | |
1910 | extent->flags = flags; | |
1911 | extent->length = num; | |
e7b1948d | 1912 | } |
2178a569 EB |
1913 | offset += num; |
1914 | remaining_bytes -= num; | |
fb7afc79 | 1915 | } |
e7b1948d | 1916 | |
fb7afc79 VSO |
1917 | extents_end = extent + 1; |
1918 | ||
1919 | for (extent = extents; extent < extents_end; extent++) { | |
80c7c2b0 PM |
1920 | extent->flags = cpu_to_be32(extent->flags); |
1921 | extent->length = cpu_to_be32(extent->length); | |
e7b1948d VSO |
1922 | } |
1923 | ||
fb7afc79 VSO |
1924 | *bytes -= remaining_bytes; |
1925 | *nb_extents = extents_end - extents; | |
e7b1948d VSO |
1926 | |
1927 | return 0; | |
1928 | } | |
1929 | ||
1930 | /* nbd_co_send_extents | |
3d068aff VSO |
1931 | * |
1932 | * @length is only for tracing purposes (and may be smaller or larger | |
1933 | * than the client's original request). @last controls whether | |
1934 | * NBD_REPLY_FLAG_DONE is sent. @extents should already be in | |
1935 | * big-endian format. | |
1936 | */ | |
e7b1948d | 1937 | static int nbd_co_send_extents(NBDClient *client, uint64_t handle, |
3d068aff VSO |
1938 | NBDExtent *extents, unsigned int nb_extents, |
1939 | uint64_t length, bool last, | |
e7b1948d VSO |
1940 | uint32_t context_id, Error **errp) |
1941 | { | |
1942 | NBDStructuredMeta chunk; | |
1943 | ||
1944 | struct iovec iov[] = { | |
1945 | {.iov_base = &chunk, .iov_len = sizeof(chunk)}, | |
1946 | {.iov_base = extents, .iov_len = nb_extents * sizeof(extents[0])} | |
1947 | }; | |
1948 | ||
3d068aff VSO |
1949 | trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last); |
1950 | set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0, | |
1951 | NBD_REPLY_TYPE_BLOCK_STATUS, | |
e7b1948d VSO |
1952 | handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len); |
1953 | stl_be_p(&chunk.context_id, context_id); | |
1954 | ||
1955 | return nbd_co_send_iov(client, iov, 2, errp); | |
1956 | } | |
1957 | ||
1958 | /* Get block status from the exported device and send it to the client */ | |
1959 | static int nbd_co_send_block_status(NBDClient *client, uint64_t handle, | |
1960 | BlockDriverState *bs, uint64_t offset, | |
fb7afc79 VSO |
1961 | uint32_t length, bool dont_fragment, |
1962 | bool last, uint32_t context_id, | |
1963 | Error **errp) | |
e7b1948d VSO |
1964 | { |
1965 | int ret; | |
416e34bd | 1966 | unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS; |
fb7afc79 VSO |
1967 | NBDExtent *extents = g_new(NBDExtent, nb_extents); |
1968 | uint64_t final_length = length; | |
e7b1948d | 1969 | |
fb7afc79 VSO |
1970 | ret = blockstatus_to_extents(bs, offset, &final_length, extents, |
1971 | &nb_extents); | |
e7b1948d | 1972 | if (ret < 0) { |
fb7afc79 | 1973 | g_free(extents); |
e7b1948d VSO |
1974 | return nbd_co_send_structured_error( |
1975 | client, handle, -ret, "can't get block status", errp); | |
1976 | } | |
1977 | ||
fb7afc79 VSO |
1978 | ret = nbd_co_send_extents(client, handle, extents, nb_extents, |
1979 | final_length, last, context_id, errp); | |
1980 | ||
1981 | g_free(extents); | |
1982 | ||
1983 | return ret; | |
3d068aff VSO |
1984 | } |
1985 | ||
1986 | /* | |
1987 | * Populate @extents from a dirty bitmap. Unless @dont_fragment, the | |
1988 | * final extent may exceed the original @length. Store in @length the | |
1989 | * byte length encoded (which may be smaller or larger than the | |
1990 | * original), and return the number of extents used. | |
1991 | */ | |
1992 | static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset, | |
1993 | uint64_t *length, NBDExtent *extents, | |
1994 | unsigned int nb_extents, | |
1995 | bool dont_fragment) | |
1996 | { | |
45eb6fb6 | 1997 | uint64_t begin = offset, end = offset; |
3d068aff VSO |
1998 | uint64_t overall_end = offset + *length; |
1999 | unsigned int i = 0; | |
2000 | BdrvDirtyBitmapIter *it; | |
2001 | bool dirty; | |
2002 | ||
2003 | bdrv_dirty_bitmap_lock(bitmap); | |
2004 | ||
2005 | it = bdrv_dirty_iter_new(bitmap); | |
2006 | dirty = bdrv_get_dirty_locked(NULL, bitmap, offset); | |
2007 | ||
2008 | assert(begin < overall_end && nb_extents); | |
2009 | while (begin < overall_end && i < nb_extents) { | |
6545916d VSO |
2010 | bool next_dirty = !dirty; |
2011 | ||
3d068aff | 2012 | if (dirty) { |
76d570dc | 2013 | end = bdrv_dirty_bitmap_next_zero(bitmap, begin, UINT64_MAX); |
3d068aff VSO |
2014 | } else { |
2015 | bdrv_set_dirty_iter(it, begin); | |
2016 | end = bdrv_dirty_iter_next(it); | |
2017 | } | |
2018 | if (end == -1 || end - begin > UINT32_MAX) { | |
2019 | /* Cap to an aligned value < 4G beyond begin. */ | |
2020 | end = MIN(bdrv_dirty_bitmap_size(bitmap), | |
2021 | begin + UINT32_MAX + 1 - | |
2022 | bdrv_dirty_bitmap_granularity(bitmap)); | |
6545916d | 2023 | next_dirty = dirty; |
3d068aff VSO |
2024 | } |
2025 | if (dont_fragment && end > overall_end) { | |
2026 | end = overall_end; | |
2027 | } | |
2028 | ||
2029 | extents[i].length = cpu_to_be32(end - begin); | |
2030 | extents[i].flags = cpu_to_be32(dirty ? NBD_STATE_DIRTY : 0); | |
2031 | i++; | |
2032 | begin = end; | |
6545916d | 2033 | dirty = next_dirty; |
3d068aff VSO |
2034 | } |
2035 | ||
2036 | bdrv_dirty_iter_free(it); | |
2037 | ||
2038 | bdrv_dirty_bitmap_unlock(bitmap); | |
2039 | ||
7606c99a | 2040 | assert(offset < end); |
3d068aff VSO |
2041 | *length = end - offset; |
2042 | return i; | |
2043 | } | |
2044 | ||
2045 | static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle, | |
2046 | BdrvDirtyBitmap *bitmap, uint64_t offset, | |
2047 | uint32_t length, bool dont_fragment, bool last, | |
2048 | uint32_t context_id, Error **errp) | |
2049 | { | |
2050 | int ret; | |
416e34bd | 2051 | unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS; |
3d068aff VSO |
2052 | NBDExtent *extents = g_new(NBDExtent, nb_extents); |
2053 | uint64_t final_length = length; | |
2054 | ||
2055 | nb_extents = bitmap_to_extents(bitmap, offset, &final_length, extents, | |
2056 | nb_extents, dont_fragment); | |
2057 | ||
2058 | ret = nbd_co_send_extents(client, handle, extents, nb_extents, | |
2059 | final_length, last, context_id, errp); | |
2060 | ||
2061 | g_free(extents); | |
2062 | ||
2063 | return ret; | |
e7b1948d VSO |
2064 | } |
2065 | ||
2a6e128b VSO |
2066 | /* nbd_co_receive_request |
2067 | * Collect a client request. Return 0 if request looks valid, -EIO to drop | |
2068 | * connection right away, and any other negative value to report an error to | |
2069 | * the client (although the caller may still need to disconnect after reporting | |
2070 | * the error). | |
2071 | */ | |
2fd2c840 VSO |
2072 | static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request, |
2073 | Error **errp) | |
a030b347 | 2074 | { |
72deddc5 | 2075 | NBDClient *client = req->client; |
5c54e7fa | 2076 | int valid_flags; |
a030b347 | 2077 | |
1c778ef7 | 2078 | g_assert(qemu_in_coroutine()); |
ff82911c | 2079 | assert(client->recv_coroutine == qemu_coroutine_self()); |
2fd2c840 | 2080 | if (nbd_receive_request(client->ioc, request, errp) < 0) { |
ee898b87 | 2081 | return -EIO; |
a030b347 PB |
2082 | } |
2083 | ||
3736cc5b EB |
2084 | trace_nbd_co_receive_request_decode_type(request->handle, request->type, |
2085 | nbd_cmd_lookup(request->type)); | |
29b6c3b3 | 2086 | |
b626b51a | 2087 | if (request->type != NBD_CMD_WRITE) { |
29b6c3b3 EB |
2088 | /* No payload, we are ready to read the next request. */ |
2089 | req->complete = true; | |
2090 | } | |
2091 | ||
b626b51a | 2092 | if (request->type == NBD_CMD_DISC) { |
29b6c3b3 EB |
2093 | /* Special case: we're going to disconnect without a reply, |
2094 | * whether or not flags, from, or len are bogus */ | |
ee898b87 | 2095 | return -EIO; |
29b6c3b3 EB |
2096 | } |
2097 | ||
bc37b06a VSO |
2098 | if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE || |
2099 | request->type == NBD_CMD_CACHE) | |
2100 | { | |
eb38c3b6 | 2101 | if (request->len > NBD_MAX_BUFFER_SIZE) { |
2fd2c840 VSO |
2102 | error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)", |
2103 | request->len, NBD_MAX_BUFFER_SIZE); | |
ee898b87 | 2104 | return -EINVAL; |
eb38c3b6 PB |
2105 | } |
2106 | ||
f1c17521 PB |
2107 | req->data = blk_try_blockalign(client->exp->blk, request->len); |
2108 | if (req->data == NULL) { | |
2fd2c840 | 2109 | error_setg(errp, "No memory"); |
ee898b87 | 2110 | return -ENOMEM; |
f1c17521 | 2111 | } |
2d821488 | 2112 | } |
b626b51a | 2113 | if (request->type == NBD_CMD_WRITE) { |
e6798f06 VSO |
2114 | if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data", |
2115 | errp) < 0) | |
2116 | { | |
ee898b87 | 2117 | return -EIO; |
a030b347 | 2118 | } |
29b6c3b3 | 2119 | req->complete = true; |
6fb2b972 | 2120 | |
9588463e VSO |
2121 | trace_nbd_co_receive_request_payload_received(request->handle, |
2122 | request->len); | |
a030b347 | 2123 | } |
29b6c3b3 | 2124 | |
fed5f8f8 EB |
2125 | /* Sanity checks. */ |
2126 | if (client->exp->nbdflags & NBD_FLAG_READ_ONLY && | |
2127 | (request->type == NBD_CMD_WRITE || | |
2128 | request->type == NBD_CMD_WRITE_ZEROES || | |
2129 | request->type == NBD_CMD_TRIM)) { | |
2130 | error_setg(errp, "Export is read-only"); | |
2131 | return -EROFS; | |
2132 | } | |
2133 | if (request->from > client->exp->size || | |
9d26dfcb | 2134 | request->len > client->exp->size - request->from) { |
2fd2c840 VSO |
2135 | error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32 |
2136 | ", Size: %" PRIu64, request->from, request->len, | |
9d26dfcb | 2137 | client->exp->size); |
fed5f8f8 EB |
2138 | return (request->type == NBD_CMD_WRITE || |
2139 | request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL; | |
29b6c3b3 | 2140 | } |
6e280648 EB |
2141 | if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len, |
2142 | client->check_align)) { | |
2143 | /* | |
2144 | * The block layer gracefully handles unaligned requests, but | |
2145 | * it's still worth tracing client non-compliance | |
2146 | */ | |
2147 | trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type), | |
2148 | request->from, | |
2149 | request->len, | |
2150 | client->check_align); | |
2151 | } | |
5c54e7fa VSO |
2152 | valid_flags = NBD_CMD_FLAG_FUA; |
2153 | if (request->type == NBD_CMD_READ && client->structured_reply) { | |
2154 | valid_flags |= NBD_CMD_FLAG_DF; | |
2155 | } else if (request->type == NBD_CMD_WRITE_ZEROES) { | |
2156 | valid_flags |= NBD_CMD_FLAG_NO_HOLE; | |
e7b1948d VSO |
2157 | } else if (request->type == NBD_CMD_BLOCK_STATUS) { |
2158 | valid_flags |= NBD_CMD_FLAG_REQ_ONE; | |
ab7c548e | 2159 | } |
5c54e7fa VSO |
2160 | if (request->flags & ~valid_flags) { |
2161 | error_setg(errp, "unsupported flags for command %s (got 0x%x)", | |
2162 | nbd_cmd_lookup(request->type), request->flags); | |
ee898b87 | 2163 | return -EINVAL; |
1f4d6d18 | 2164 | } |
29b6c3b3 | 2165 | |
ee898b87 | 2166 | return 0; |
a030b347 PB |
2167 | } |
2168 | ||
6a417599 VSO |
2169 | /* Send simple reply without a payload, or a structured error |
2170 | * @error_msg is ignored if @ret >= 0 | |
2171 | * Returns 0 if connection is still live, -errno on failure to talk to client | |
2172 | */ | |
2173 | static coroutine_fn int nbd_send_generic_reply(NBDClient *client, | |
2174 | uint64_t handle, | |
2175 | int ret, | |
2176 | const char *error_msg, | |
2177 | Error **errp) | |
2178 | { | |
2179 | if (client->structured_reply && ret < 0) { | |
2180 | return nbd_co_send_structured_error(client, handle, -ret, error_msg, | |
2181 | errp); | |
2182 | } else { | |
2183 | return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0, | |
2184 | NULL, 0, errp); | |
2185 | } | |
2186 | } | |
2187 | ||
2188 | /* Handle NBD_CMD_READ request. | |
2189 | * Return -errno if sending fails. Other errors are reported directly to the | |
2190 | * client as an error reply. */ | |
2191 | static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request, | |
2192 | uint8_t *data, Error **errp) | |
2193 | { | |
2194 | int ret; | |
2195 | NBDExport *exp = client->exp; | |
2196 | ||
bc37b06a | 2197 | assert(request->type == NBD_CMD_READ || request->type == NBD_CMD_CACHE); |
6a417599 VSO |
2198 | |
2199 | /* XXX: NBD Protocol only documents use of FUA with WRITE */ | |
2200 | if (request->flags & NBD_CMD_FLAG_FUA) { | |
2201 | ret = blk_co_flush(exp->blk); | |
2202 | if (ret < 0) { | |
2203 | return nbd_send_generic_reply(client, request->handle, ret, | |
2204 | "flush failed", errp); | |
2205 | } | |
2206 | } | |
2207 | ||
2208 | if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) && | |
2f454def VSO |
2209 | request->len && request->type != NBD_CMD_CACHE) |
2210 | { | |
6a417599 VSO |
2211 | return nbd_co_send_sparse_read(client, request->handle, request->from, |
2212 | data, request->len, errp); | |
2213 | } | |
2214 | ||
2215 | ret = blk_pread(exp->blk, request->from + exp->dev_offset, data, | |
2216 | request->len); | |
bc37b06a | 2217 | if (ret < 0 || request->type == NBD_CMD_CACHE) { |
6a417599 VSO |
2218 | return nbd_send_generic_reply(client, request->handle, ret, |
2219 | "reading from file failed", errp); | |
2220 | } | |
2221 | ||
2222 | if (client->structured_reply) { | |
2223 | if (request->len) { | |
2224 | return nbd_co_send_structured_read(client, request->handle, | |
2225 | request->from, data, | |
2226 | request->len, true, errp); | |
2227 | } else { | |
2228 | return nbd_co_send_structured_done(client, request->handle, errp); | |
2229 | } | |
2230 | } else { | |
2231 | return nbd_co_send_simple_reply(client, request->handle, 0, | |
2232 | data, request->len, errp); | |
2233 | } | |
2234 | } | |
2235 | ||
6f302e60 VSO |
2236 | /* Handle NBD request. |
2237 | * Return -errno if sending fails. Other errors are reported directly to the | |
2238 | * client as an error reply. */ | |
2239 | static coroutine_fn int nbd_handle_request(NBDClient *client, | |
2240 | NBDRequest *request, | |
2241 | uint8_t *data, Error **errp) | |
2242 | { | |
2243 | int ret; | |
2244 | int flags; | |
2245 | NBDExport *exp = client->exp; | |
2246 | char *msg; | |
2247 | ||
2248 | switch (request->type) { | |
2249 | case NBD_CMD_READ: | |
bc37b06a | 2250 | case NBD_CMD_CACHE: |
6f302e60 VSO |
2251 | return nbd_do_cmd_read(client, request, data, errp); |
2252 | ||
2253 | case NBD_CMD_WRITE: | |
2254 | flags = 0; | |
2255 | if (request->flags & NBD_CMD_FLAG_FUA) { | |
2256 | flags |= BDRV_REQ_FUA; | |
2257 | } | |
2258 | ret = blk_pwrite(exp->blk, request->from + exp->dev_offset, | |
2259 | data, request->len, flags); | |
2260 | return nbd_send_generic_reply(client, request->handle, ret, | |
2261 | "writing to file failed", errp); | |
2262 | ||
2263 | case NBD_CMD_WRITE_ZEROES: | |
2264 | flags = 0; | |
2265 | if (request->flags & NBD_CMD_FLAG_FUA) { | |
2266 | flags |= BDRV_REQ_FUA; | |
2267 | } | |
2268 | if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) { | |
2269 | flags |= BDRV_REQ_MAY_UNMAP; | |
2270 | } | |
2271 | ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset, | |
2272 | request->len, flags); | |
2273 | return nbd_send_generic_reply(client, request->handle, ret, | |
2274 | "writing to file failed", errp); | |
2275 | ||
2276 | case NBD_CMD_DISC: | |
2277 | /* unreachable, thanks to special case in nbd_co_receive_request() */ | |
2278 | abort(); | |
2279 | ||
2280 | case NBD_CMD_FLUSH: | |
2281 | ret = blk_co_flush(exp->blk); | |
2282 | return nbd_send_generic_reply(client, request->handle, ret, | |
2283 | "flush failed", errp); | |
2284 | ||
2285 | case NBD_CMD_TRIM: | |
2286 | ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset, | |
2287 | request->len); | |
65529782 EB |
2288 | if (ret == 0 && request->flags & NBD_CMD_FLAG_FUA) { |
2289 | ret = blk_co_flush(exp->blk); | |
2290 | } | |
6f302e60 VSO |
2291 | return nbd_send_generic_reply(client, request->handle, ret, |
2292 | "discard failed", errp); | |
2293 | ||
e7b1948d | 2294 | case NBD_CMD_BLOCK_STATUS: |
d8b20291 EB |
2295 | if (!request->len) { |
2296 | return nbd_send_generic_reply(client, request->handle, -EINVAL, | |
2297 | "need non-zero length", errp); | |
2298 | } | |
3d068aff VSO |
2299 | if (client->export_meta.valid && |
2300 | (client->export_meta.base_allocation || | |
2301 | client->export_meta.bitmap)) | |
2302 | { | |
fb7afc79 VSO |
2303 | bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE; |
2304 | ||
3d068aff VSO |
2305 | if (client->export_meta.base_allocation) { |
2306 | ret = nbd_co_send_block_status(client, request->handle, | |
2307 | blk_bs(exp->blk), request->from, | |
fb7afc79 | 2308 | request->len, dont_fragment, |
3d068aff VSO |
2309 | !client->export_meta.bitmap, |
2310 | NBD_META_ID_BASE_ALLOCATION, | |
2311 | errp); | |
2312 | if (ret < 0) { | |
2313 | return ret; | |
2314 | } | |
2315 | } | |
2316 | ||
2317 | if (client->export_meta.bitmap) { | |
2318 | ret = nbd_co_send_bitmap(client, request->handle, | |
2319 | client->exp->export_bitmap, | |
2320 | request->from, request->len, | |
fb7afc79 | 2321 | dont_fragment, |
3d068aff VSO |
2322 | true, NBD_META_ID_DIRTY_BITMAP, errp); |
2323 | if (ret < 0) { | |
2324 | return ret; | |
2325 | } | |
2326 | } | |
2327 | ||
2328 | return ret; | |
e7b1948d VSO |
2329 | } else { |
2330 | return nbd_send_generic_reply(client, request->handle, -EINVAL, | |
2331 | "CMD_BLOCK_STATUS not negotiated", | |
2332 | errp); | |
2333 | } | |
2334 | ||
6f302e60 VSO |
2335 | default: |
2336 | msg = g_strdup_printf("invalid request type (%" PRIu32 ") received", | |
2337 | request->type); | |
2338 | ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg, | |
2339 | errp); | |
2340 | g_free(msg); | |
2341 | return ret; | |
2342 | } | |
2343 | } | |
2344 | ||
ff82911c PB |
2345 | /* Owns a reference to the NBDClient passed as opaque. */ |
2346 | static coroutine_fn void nbd_trip(void *opaque) | |
75818250 | 2347 | { |
262db388 | 2348 | NBDClient *client = opaque; |
315f78ab | 2349 | NBDRequestData *req; |
ff82911c | 2350 | NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */ |
a0dc63a6 | 2351 | int ret; |
2fd2c840 | 2352 | Error *local_err = NULL; |
b2e3d87f | 2353 | |
9588463e | 2354 | trace_nbd_trip(); |
ff2b68aa | 2355 | if (client->closing) { |
ff82911c | 2356 | nbd_client_put(client); |
ff2b68aa PB |
2357 | return; |
2358 | } | |
b2e3d87f | 2359 | |
ff2b68aa | 2360 | req = nbd_request_get(client); |
2fd2c840 | 2361 | ret = nbd_co_receive_request(req, &request, &local_err); |
ee898b87 | 2362 | client->recv_coroutine = NULL; |
b2e3d87f | 2363 | |
d6268348 WC |
2364 | if (client->closing) { |
2365 | /* | |
2366 | * The client may be closed when we are blocked in | |
2367 | * nbd_co_receive_request() | |
2368 | */ | |
2369 | goto done; | |
2370 | } | |
2371 | ||
a0d7ce20 VSO |
2372 | nbd_client_receive_next_request(client); |
2373 | if (ret == -EIO) { | |
2374 | goto disconnect; | |
2375 | } | |
2376 | ||
2377 | if (ret < 0) { | |
6a417599 VSO |
2378 | /* It wans't -EIO, so, according to nbd_co_receive_request() |
2379 | * semantics, we should return the error to the client. */ | |
2380 | Error *export_err = local_err; | |
2381 | ||
2382 | local_err = NULL; | |
2383 | ret = nbd_send_generic_reply(client, request.handle, -EINVAL, | |
2384 | error_get_pretty(export_err), &local_err); | |
2385 | error_free(export_err); | |
6f302e60 VSO |
2386 | } else { |
2387 | ret = nbd_handle_request(client, &request, req->data, &local_err); | |
5c54e7fa VSO |
2388 | } |
2389 | if (ret < 0) { | |
c7b97282 | 2390 | error_prepend(&local_err, "Failed to send reply: "); |
2fd2c840 VSO |
2391 | goto disconnect; |
2392 | } | |
2393 | ||
8c372a02 VSO |
2394 | /* We must disconnect after NBD_CMD_WRITE if we did not |
2395 | * read the payload. | |
2396 | */ | |
2fd2c840 VSO |
2397 | if (!req->complete) { |
2398 | error_setg(&local_err, "Request handling failed in intermediate state"); | |
8c372a02 | 2399 | goto disconnect; |
b2e3d87f NT |
2400 | } |
2401 | ||
7fe7b68b | 2402 | done: |
262db388 | 2403 | nbd_request_put(req); |
ff82911c | 2404 | nbd_client_put(client); |
262db388 PB |
2405 | return; |
2406 | ||
8c372a02 | 2407 | disconnect: |
2fd2c840 VSO |
2408 | if (local_err) { |
2409 | error_reportf_err(local_err, "Disconnect client, due to: "); | |
2410 | } | |
72deddc5 | 2411 | nbd_request_put(req); |
0c9390d9 | 2412 | client_close(client, true); |
ff82911c | 2413 | nbd_client_put(client); |
7a5ca864 | 2414 | } |
af49bbbe | 2415 | |
ff82911c | 2416 | static void nbd_client_receive_next_request(NBDClient *client) |
958c717d | 2417 | { |
ff82911c PB |
2418 | if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) { |
2419 | nbd_client_get(client); | |
2420 | client->recv_coroutine = qemu_coroutine_create(nbd_trip, client); | |
2421 | aio_co_schedule(client->exp->ctx, client->recv_coroutine); | |
958c717d HR |
2422 | } |
2423 | } | |
2424 | ||
1a6245a5 FZ |
2425 | static coroutine_fn void nbd_co_client_start(void *opaque) |
2426 | { | |
c84087f2 | 2427 | NBDClient *client = opaque; |
2fd2c840 | 2428 | Error *local_err = NULL; |
1a6245a5 | 2429 | |
df8ad9f1 EB |
2430 | qemu_co_mutex_init(&client->send_lock); |
2431 | ||
2fd2c840 VSO |
2432 | if (nbd_negotiate(client, &local_err)) { |
2433 | if (local_err) { | |
2434 | error_report_err(local_err); | |
2435 | } | |
0c9390d9 | 2436 | client_close(client, false); |
c84087f2 | 2437 | return; |
1a6245a5 | 2438 | } |
ff82911c PB |
2439 | |
2440 | nbd_client_receive_next_request(client); | |
1a6245a5 FZ |
2441 | } |
2442 | ||
0c9390d9 | 2443 | /* |
7f7dfe2a VSO |
2444 | * Create a new client listener using the given channel @sioc. |
2445 | * Begin servicing it in a coroutine. When the connection closes, call | |
2446 | * @close_fn with an indication of whether the client completed negotiation. | |
0c9390d9 | 2447 | */ |
7f7dfe2a | 2448 | void nbd_client_new(QIOChannelSocket *sioc, |
f95910fe | 2449 | QCryptoTLSCreds *tlscreds, |
b25e12da | 2450 | const char *tlsauthz, |
0c9390d9 | 2451 | void (*close_fn)(NBDClient *, bool)) |
af49bbbe | 2452 | { |
1743b515 | 2453 | NBDClient *client; |
c84087f2 | 2454 | Coroutine *co; |
1a6245a5 | 2455 | |
e8d3eb74 | 2456 | client = g_new0(NBDClient, 1); |
1743b515 | 2457 | client->refcount = 1; |
f95910fe DB |
2458 | client->tlscreds = tlscreds; |
2459 | if (tlscreds) { | |
2460 | object_ref(OBJECT(client->tlscreds)); | |
2461 | } | |
b25e12da | 2462 | client->tlsauthz = g_strdup(tlsauthz); |
1c778ef7 DB |
2463 | client->sioc = sioc; |
2464 | object_ref(OBJECT(client->sioc)); | |
2465 | client->ioc = QIO_CHANNEL(sioc); | |
2466 | object_ref(OBJECT(client->ioc)); | |
0c9390d9 | 2467 | client->close_fn = close_fn; |
2c8d9f06 | 2468 | |
c84087f2 VSO |
2469 | co = qemu_coroutine_create(nbd_co_client_start, client); |
2470 | qemu_coroutine_enter(co); | |
af49bbbe | 2471 | } |