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