blob: b60ebc3ab6ac1a6e9dfb9b2d523e76b4a4bf0b4e [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
Eric Blakeebd57062020-09-30 07:11:03 -05002 * Copyright (C) 2016-2020 Red Hat, Inc.
bellard7a5ca862008-05-27 21:13:40 +00003 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 *
Fam Zheng798bfe02016-01-14 16:41:02 +08005 * Network Block Device Server Side
bellard7a5ca862008-05-27 21:13:40 +00006 *
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ths75818252008-07-03 13:41:03 +000018 */
bellard7a5ca862008-05-27 21:13:40 +000019
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Kevin Wolf56ee8622020-09-24 17:26:50 +020021
22#include "block/export.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Markus Armbrusterdc5e9ac2019-08-12 07:23:49 +020024#include "qemu/queue.h"
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +030025#include "trace.h"
Fam Zheng798bfe02016-01-14 16:41:02 +080026#include "nbd-internal.h"
Eric Blake416e34b2019-05-10 10:17:35 -050027#include "qemu/units.h"
Paolo Bonzinica441482015-05-07 17:25:10 +020028
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +030029#define NBD_META_ID_BASE_ALLOCATION 0
Eric Blake71719cd2020-10-27 00:05:54 -050030#define NBD_META_ID_ALLOCATION_DEPTH 1
Eric Blake3b1f2442020-10-27 00:05:52 -050031/* Dirty bitmaps use 'NBD_META_ID_DIRTY_BITMAP + i', so keep this id last. */
Eric Blake71719cd2020-10-27 00:05:54 -050032#define NBD_META_ID_DIRTY_BITMAP 2
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +030033
Eric Blake416e34b2019-05-10 10:17:35 -050034/*
35 * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +030036 * constant. If an increase is needed, note that the NBD protocol
37 * recommends no larger than 32 mb, so that the client won't consider
Eric Blake416e34b2019-05-10 10:17:35 -050038 * the reply as a denial of service attack.
39 */
40#define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +030041
Paolo Bonzinica441482015-05-07 17:25:10 +020042static int system_errno_to_nbd_errno(int err)
43{
44 switch (err) {
45 case 0:
46 return NBD_SUCCESS;
47 case EPERM:
Eric Blakec0301fc2016-04-05 21:35:02 -060048 case EROFS:
Paolo Bonzinica441482015-05-07 17:25:10 +020049 return NBD_EPERM;
50 case EIO:
51 return NBD_EIO;
52 case ENOMEM:
53 return NBD_ENOMEM;
54#ifdef EDQUOT
55 case EDQUOT:
56#endif
57 case EFBIG:
58 case ENOSPC:
59 return NBD_ENOSPC;
Eric Blakebae245d2017-10-27 12:40:28 +020060 case EOVERFLOW:
61 return NBD_EOVERFLOW;
Eric Blake0a479542019-08-23 09:37:23 -050062 case ENOTSUP:
63#if ENOTSUP != EOPNOTSUPP
64 case EOPNOTSUPP:
65#endif
66 return NBD_ENOTSUP;
Eric Blakeb6f5d3b2016-10-14 13:33:16 -050067 case ESHUTDOWN:
68 return NBD_ESHUTDOWN;
Paolo Bonzinica441482015-05-07 17:25:10 +020069 case EINVAL:
70 default:
71 return NBD_EINVAL;
72 }
73}
74
Paolo Bonzini9a304d22012-08-22 15:30:31 +020075/* Definitions for opaque data types */
76
Eric Blake315f78a2016-10-14 13:33:05 -050077typedef struct NBDRequestData NBDRequestData;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020078
Eric Blake315f78a2016-10-14 13:33:05 -050079struct NBDRequestData {
80 QSIMPLEQ_ENTRY(NBDRequestData) entry;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020081 NBDClient *client;
82 uint8_t *data;
Eric Blake29b6c3b2016-05-11 16:39:37 -060083 bool complete;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020084};
85
86struct NBDExport {
Kevin Wolf56ee8622020-09-24 17:26:50 +020087 BlockExport common;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +020088
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020089 char *name;
Eric Blakeb1a75b32016-10-14 13:33:03 -050090 char *description;
Eric Blake9d26dfc2019-01-17 13:36:43 -060091 uint64_t size;
Eric Blake7423f412016-07-21 13:34:46 -060092 uint16_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +020093 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020094 QTAILQ_ENTRY(NBDExport) next;
Max Reitz958c7172014-06-20 21:57:32 +020095
Kevin Wolfcd7fca92016-07-06 11:22:39 +020096 BlockBackend *eject_notifier_blk;
Max Reitz741cc432016-01-29 16:36:06 +010097 Notifier eject_notifier;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +030098
Eric Blake71719cd2020-10-27 00:05:54 -050099 bool allocation_depth;
Eric Blake3b1f2442020-10-27 00:05:52 -0500100 BdrvDirtyBitmap **export_bitmaps;
101 size_t nr_export_bitmaps;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200102};
103
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200104static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
105
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300106/* NBDExportMetaContexts represents a list of contexts to be exported,
107 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
108 * NBD_OPT_LIST_META_CONTEXT. */
109typedef struct NBDExportMetaContexts {
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +0300110 NBDExport *exp;
Eric Blake47ec4852020-10-27 00:05:51 -0500111 size_t count; /* number of negotiated contexts */
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300112 bool base_allocation; /* export base:allocation context (block status) */
Eric Blake71719cd2020-10-27 00:05:54 -0500113 bool allocation_depth; /* export qemu:allocation-depth */
Eric Blake3b1f2442020-10-27 00:05:52 -0500114 bool *bitmaps; /*
115 * export qemu:dirty-bitmap:<export bitmap name>,
116 * sized by exp->nr_export_bitmaps
117 */
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300118} NBDExportMetaContexts;
119
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200120struct NBDClient {
121 int refcount;
Eric Blake0c9390d2017-06-08 17:26:17 -0500122 void (*close_fn)(NBDClient *client, bool negotiated);
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200123
124 NBDExport *exp;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000125 QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000126 char *tlsauthz;
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000127 QIOChannelSocket *sioc; /* The underlying data channel */
128 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200129
130 Coroutine *recv_coroutine;
131
132 CoMutex send_lock;
133 Coroutine *send_coroutine;
134
Sergio Lopezf148ae72020-12-14 18:05:18 +0100135 bool read_yielding;
136 bool quiescing;
137
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200138 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200139 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200140 bool closing;
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +0200141
Eric Blake6e280642019-04-02 22:05:21 -0500142 uint32_t check_align; /* If non-zero, check for aligned client requests */
143
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +0200144 bool structured_reply;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300145 NBDExportMetaContexts export_meta;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200146
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600147 uint32_t opt; /* Current option being negotiated */
148 uint32_t optlen; /* remaining length of data in ioc for the option being
149 negotiated now */
150};
bellard7a5ca862008-05-27 21:13:40 +0000151
Paolo Bonziniff829112017-02-13 14:52:24 +0100152static void nbd_client_receive_next_request(NBDClient *client);
Max Reitz958c7172014-06-20 21:57:32 +0200153
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200154/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000155
156 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000157 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200158
159 or
160
161 Server Client
162 Negotiate #1
163 Option
164 Negotiate #2
165
166 ----
167
168 followed by
169
170 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000171 Request
172 Response
173 Request
174 Response
175 ...
176 ...
177 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200178
bellard7a5ca862008-05-27 21:13:40 +0000179*/
180
Vladimir Sementsov-Ogievskiy1d179222018-01-10 17:08:25 -0600181static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
182 uint32_t type, uint32_t length)
183{
184 stq_be_p(&rep->magic, NBD_REP_MAGIC);
185 stl_be_p(&rep->option, option);
186 stl_be_p(&rep->type, type);
187 stl_be_p(&rep->length, length);
188}
189
Eric Blake526e5c62016-10-14 13:33:08 -0500190/* Send a reply header, including length, but no payload.
191 * Return -errno on error, 0 on success. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600192static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
193 uint32_t len, Error **errp)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200194{
Vladimir Sementsov-Ogievskiy1d179222018-01-10 17:08:25 -0600195 NBDOptionReply rep;
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100196
Vladimir Sementsov-Ogievskiy1d179222018-01-10 17:08:25 -0600197 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
Eric Blake3736cc52017-07-07 15:30:43 -0500198 type, nbd_rep_lookup(type), len);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000199
Eric Blakef37708f2017-07-07 15:30:46 -0500200 assert(len < NBD_MAX_BUFFER_SIZE);
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300201
Vladimir Sementsov-Ogievskiy1d179222018-01-10 17:08:25 -0600202 set_be_option_rep(&rep, client->opt, type, len);
203 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100204}
205
Eric Blake526e5c62016-10-14 13:33:08 -0500206/* Send a reply header with default 0 length.
207 * Return -errno on error, 0 on success. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600208static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300209 Error **errp)
Eric Blake526e5c62016-10-14 13:33:08 -0500210{
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600211 return nbd_negotiate_send_rep_len(client, type, 0, errp);
Eric Blake526e5c62016-10-14 13:33:08 -0500212}
213
Eric Blake36683282016-10-14 13:33:09 -0500214/* Send an error reply.
215 * Return -errno on error, 0 on success. */
Eric Blake41f5dfa2018-01-10 17:08:23 -0600216static int GCC_FMT_ATTR(4, 0)
217nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
218 Error **errp, const char *fmt, va_list va)
Eric Blake36683282016-10-14 13:33:09 -0500219{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200220 ERRP_GUARD();
Eric Blakedf18c042019-08-24 12:28:12 -0500221 g_autofree char *msg = NULL;
Eric Blake36683282016-10-14 13:33:09 -0500222 int ret;
223 size_t len;
224
Eric Blake36683282016-10-14 13:33:09 -0500225 msg = g_strdup_vprintf(fmt, va);
Eric Blake36683282016-10-14 13:33:09 -0500226 len = strlen(msg);
Eric Blake5c4fe012020-06-08 13:26:37 -0500227 assert(len < NBD_MAX_STRING_SIZE);
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300228 trace_nbd_negotiate_send_rep_err(msg);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600229 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
Eric Blake36683282016-10-14 13:33:09 -0500230 if (ret < 0) {
Eric Blakedf18c042019-08-24 12:28:12 -0500231 return ret;
Eric Blake36683282016-10-14 13:33:09 -0500232 }
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600233 if (nbd_write(client->ioc, msg, len, errp) < 0) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300234 error_prepend(errp, "write failed (error message): ");
Eric Blakedf18c042019-08-24 12:28:12 -0500235 return -EIO;
Eric Blake36683282016-10-14 13:33:09 -0500236 }
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300237
Eric Blakedf18c042019-08-24 12:28:12 -0500238 return 0;
Eric Blake36683282016-10-14 13:33:09 -0500239}
240
Eric Blake5c4fe012020-06-08 13:26:37 -0500241/*
242 * Return a malloc'd copy of @name suitable for use in an error reply.
243 */
244static char *
245nbd_sanitize_name(const char *name)
246{
247 if (strnlen(name, 80) < 80) {
248 return g_strdup(name);
249 }
250 /* XXX Should we also try to sanitize any control characters? */
251 return g_strdup_printf("%.80s...", name);
252}
253
Eric Blake41f5dfa2018-01-10 17:08:23 -0600254/* Send an error reply.
255 * Return -errno on error, 0 on success. */
256static int GCC_FMT_ATTR(4, 5)
257nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
258 Error **errp, const char *fmt, ...)
259{
260 va_list va;
261 int ret;
262
263 va_start(va, fmt);
264 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
265 va_end(va);
266 return ret;
267}
268
Eric Blake894e0282018-01-10 17:08:24 -0600269/* Drop remainder of the current option, and send a reply with the
270 * given error type and message. Return -errno on read or write
271 * failure; or 0 if connection is still live. */
Vladimir Sementsov-Ogievskiy2e425fd2018-03-12 18:21:19 +0300272static int GCC_FMT_ATTR(4, 0)
273nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
274 const char *fmt, va_list va)
275{
276 int ret = nbd_drop(client->ioc, client->optlen, errp);
277
278 client->optlen = 0;
279 if (!ret) {
280 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
281 }
282 return ret;
283}
284
Eric Blake894e0282018-01-10 17:08:24 -0600285static int GCC_FMT_ATTR(4, 5)
286nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
287 const char *fmt, ...)
288{
Vladimir Sementsov-Ogievskiy2e425fd2018-03-12 18:21:19 +0300289 int ret;
Eric Blake894e0282018-01-10 17:08:24 -0600290 va_list va;
291
Vladimir Sementsov-Ogievskiy2e425fd2018-03-12 18:21:19 +0300292 va_start(va, fmt);
293 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
294 va_end(va);
295
296 return ret;
297}
298
299static int GCC_FMT_ATTR(3, 4)
300nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
301{
302 int ret;
303 va_list va;
304
305 va_start(va, fmt);
306 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
307 va_end(va);
308
Eric Blake894e0282018-01-10 17:08:24 -0600309 return ret;
310}
311
312/* Read size bytes from the unparsed payload of the current option.
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500313 * If @check_nul, require that no NUL bytes appear in buffer.
Eric Blake894e0282018-01-10 17:08:24 -0600314 * Return -errno on I/O error, 0 if option was completely handled by
315 * sending a reply about inconsistent lengths, or 1 on success. */
316static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500317 bool check_nul, Error **errp)
Eric Blake894e0282018-01-10 17:08:24 -0600318{
319 if (size > client->optlen) {
Vladimir Sementsov-Ogievskiy2e425fd2018-03-12 18:21:19 +0300320 return nbd_opt_invalid(client, errp,
321 "Inconsistent lengths in option %s",
322 nbd_opt_lookup(client->opt));
Eric Blake894e0282018-01-10 17:08:24 -0600323 }
324 client->optlen -= size;
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500325 if (qio_channel_read_all(client->ioc, buffer, size, errp) < 0) {
326 return -EIO;
327 }
328
329 if (check_nul && strnlen(buffer, size) != size) {
330 return nbd_opt_invalid(client, errp,
331 "Unexpected embedded NUL in option %s",
332 nbd_opt_lookup(client->opt));
333 }
334 return 1;
Eric Blake894e0282018-01-10 17:08:24 -0600335}
336
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300337/* Drop size bytes from the unparsed payload of the current option.
338 * Return -errno on I/O error, 0 if option was completely handled by
339 * sending a reply about inconsistent lengths, or 1 on success. */
340static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
341{
342 if (size > client->optlen) {
343 return nbd_opt_invalid(client, errp,
344 "Inconsistent lengths in option %s",
345 nbd_opt_lookup(client->opt));
346 }
347 client->optlen -= size;
348 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
349}
350
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300351/* nbd_opt_read_name
352 *
353 * Read a string with the format:
Eric Blake93676c82019-11-13 20:46:34 -0600354 * uint32_t len (<= NBD_MAX_STRING_SIZE)
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300355 * len bytes string (not 0-terminated)
356 *
Eric Blake9d7ab222019-11-13 20:46:32 -0600357 * On success, @name will be allocated.
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300358 * If @length is non-null, it will be set to the actual string length.
359 *
360 * Return -errno on I/O error, 0 if option was completely handled by
361 * sending a reply about inconsistent lengths, or 1 on success.
362 */
Eric Blake9d7ab222019-11-13 20:46:32 -0600363static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300364 Error **errp)
365{
366 int ret;
367 uint32_t len;
Eric Blake9d7ab222019-11-13 20:46:32 -0600368 g_autofree char *local_name = NULL;
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300369
Eric Blake9d7ab222019-11-13 20:46:32 -0600370 *name = NULL;
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500371 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300372 if (ret <= 0) {
373 return ret;
374 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100375 len = cpu_to_be32(len);
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300376
Eric Blake93676c82019-11-13 20:46:34 -0600377 if (len > NBD_MAX_STRING_SIZE) {
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300378 return nbd_opt_invalid(client, errp,
379 "Invalid name length: %" PRIu32, len);
380 }
381
Eric Blake9d7ab222019-11-13 20:46:32 -0600382 local_name = g_malloc(len + 1);
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500383 ret = nbd_opt_read(client, local_name, len, true, errp);
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300384 if (ret <= 0) {
385 return ret;
386 }
Eric Blake9d7ab222019-11-13 20:46:32 -0600387 local_name[len] = '\0';
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300388
389 if (length) {
390 *length = len;
391 }
Eric Blake9d7ab222019-11-13 20:46:32 -0600392 *name = g_steal_pointer(&local_name);
Vladimir Sementsov-Ogievskiy12296452018-03-12 18:21:20 +0300393
394 return 1;
395}
396
Eric Blake526e5c62016-10-14 13:33:08 -0500397/* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
398 * Return -errno on error, 0 on success. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600399static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300400 Error **errp)
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100401{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200402 ERRP_GUARD();
Eric Blakeb1a75b32016-10-14 13:33:03 -0500403 size_t name_len, desc_len;
Eric Blake526e5c62016-10-14 13:33:08 -0500404 uint32_t len;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500405 const char *name = exp->name ? exp->name : "";
406 const char *desc = exp->description ? exp->description : "";
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600407 QIOChannel *ioc = client->ioc;
Vladimir Sementsov-Ogievskiy2e5c9ad2017-06-02 18:01:49 +0300408 int ret;
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100409
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300410 trace_nbd_negotiate_send_rep_list(name, desc);
Eric Blakeb1a75b32016-10-14 13:33:03 -0500411 name_len = strlen(name);
412 desc_len = strlen(desc);
Eric Blake93676c82019-11-13 20:46:34 -0600413 assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
Eric Blake526e5c62016-10-14 13:33:08 -0500414 len = name_len + desc_len + sizeof(len);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600415 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
Vladimir Sementsov-Ogievskiy2e5c9ad2017-06-02 18:01:49 +0300416 if (ret < 0) {
417 return ret;
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100418 }
Eric Blake526e5c62016-10-14 13:33:08 -0500419
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100420 len = cpu_to_be32(name_len);
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300421 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
422 error_prepend(errp, "write failed (name length): ");
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100423 return -EINVAL;
424 }
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300425
426 if (nbd_write(ioc, name, name_len, errp) < 0) {
427 error_prepend(errp, "write failed (name buffer): ");
Eric Blakeb1a75b32016-10-14 13:33:03 -0500428 return -EINVAL;
429 }
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300430
431 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
432 error_prepend(errp, "write failed (description buffer): ");
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100433 return -EINVAL;
434 }
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300435
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100436 return 0;
437}
438
Eric Blake526e5c62016-10-14 13:33:08 -0500439/* Process the NBD_OPT_LIST command, with a potential series of replies.
440 * Return -errno on error, 0 on success. */
Eric Blakee68c35c2017-10-27 12:40:31 +0200441static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100442{
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100443 NBDExport *exp;
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600444 assert(client->opt == NBD_OPT_LIST);
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100445
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100446 /* For each export, send a NBD_REP_SERVER reply. */
447 QTAILQ_FOREACH(exp, &exports, next) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600448 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100449 return -EINVAL;
450 }
451 }
452 /* Finish with a NBD_REP_ACK. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600453 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +0100454}
455
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +0300456static void nbd_check_meta_export(NBDClient *client)
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300457{
Eric Blake47ec4852020-10-27 00:05:51 -0500458 if (client->exp != client->export_meta.exp) {
459 client->export_meta.count = 0;
460 }
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300461}
462
Eric Blakef37708f2017-07-07 15:30:46 -0500463/* Send a reply to NBD_OPT_EXPORT_NAME.
464 * Return -errno on error, 0 on success. */
Eric Blakedbb38ca2019-08-23 09:37:22 -0500465static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300466 Error **errp)
Hani Benhabilesf5076b52014-06-07 01:32:31 +0100467{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200468 ERRP_GUARD();
Eric Blake9d7ab222019-11-13 20:46:32 -0600469 g_autofree char *name = NULL;
Eric Blake5f66d062017-07-17 14:26:35 -0500470 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
Eric Blake23e099c2017-07-07 15:30:45 -0500471 size_t len;
472 int ret;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500473 uint16_t myflags;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200474
475 /* Client sends:
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200476 [20 .. xx] export name (length bytes)
Eric Blake5f66d062017-07-17 14:26:35 -0500477 Server replies:
478 [ 0 .. 7] size
479 [ 8 .. 9] export flags
480 [10 .. 133] reserved (0) [unless no_zeroes]
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200481 */
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300482 trace_nbd_negotiate_handle_export_name();
Eric Blake93676c82019-11-13 20:46:34 -0600483 if (client->optlen > NBD_MAX_STRING_SIZE) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300484 error_setg(errp, "Bad length received");
Vladimir Sementsov-Ogievskiyd9faeed2017-06-02 18:01:48 +0300485 return -EINVAL;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200486 }
Eric Blake9d7ab222019-11-13 20:46:32 -0600487 name = g_malloc(client->optlen + 1);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300488 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
Eric Blake32f158a2018-01-10 17:08:22 -0600489 return -EIO;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200490 }
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600491 name[client->optlen] = '\0';
492 client->optlen = 0;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200493
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300494 trace_nbd_negotiate_handle_export_name_request(name);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000495
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200496 client->exp = nbd_export_find(name);
497 if (!client->exp) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300498 error_setg(errp, "export not found");
Vladimir Sementsov-Ogievskiyd9faeed2017-06-02 18:01:48 +0300499 return -EINVAL;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200500 }
501
Eric Blakedbb38ca2019-08-23 09:37:22 -0500502 myflags = client->exp->nbdflags;
503 if (client->structured_reply) {
504 myflags |= NBD_FLAG_SEND_DF;
505 }
506 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
Eric Blake23e099c2017-07-07 15:30:45 -0500507 stq_be_p(buf, client->exp->size);
Eric Blakedbb38ca2019-08-23 09:37:22 -0500508 stw_be_p(buf + 8, myflags);
Eric Blake23e099c2017-07-07 15:30:45 -0500509 len = no_zeroes ? 10 : sizeof(buf);
510 ret = nbd_write(client->ioc, buf, len, errp);
511 if (ret < 0) {
512 error_prepend(errp, "write failed: ");
513 return ret;
514 }
515
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200516 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
Kevin Wolfc69de1b2020-09-24 17:26:59 +0200517 blk_exp_ref(&client->exp->common);
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +0300518 nbd_check_meta_export(client);
Vladimir Sementsov-Ogievskiyd9faeed2017-06-02 18:01:48 +0300519
520 return 0;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200521}
522
Eric Blakef37708f2017-07-07 15:30:46 -0500523/* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
524 * The buffer does NOT include the info type prefix.
525 * Return -errno on error, 0 if ready to send more. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600526static int nbd_negotiate_send_info(NBDClient *client,
Eric Blakef37708f2017-07-07 15:30:46 -0500527 uint16_t info, uint32_t length, void *buf,
528 Error **errp)
529{
530 int rc;
531
532 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600533 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
Eric Blakef37708f2017-07-07 15:30:46 -0500534 sizeof(info) + length, errp);
535 if (rc < 0) {
536 return rc;
537 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100538 info = cpu_to_be16(info);
Eric Blakef37708f2017-07-07 15:30:46 -0500539 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
540 return -EIO;
541 }
542 if (nbd_write(client->ioc, buf, length, errp) < 0) {
543 return -EIO;
544 }
545 return 0;
546}
547
Eric Blakea16a7902018-01-10 17:08:20 -0600548/* nbd_reject_length: Handle any unexpected payload.
549 * @fatal requests that we quit talking to the client, even if we are able
550 * to successfully send an error reply.
551 * Return:
552 * -errno transmission error occurred or @fatal was requested, errp is set
553 * 0 error message successfully sent to client, errp is not set
554 */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600555static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
Eric Blakea16a7902018-01-10 17:08:20 -0600556{
557 int ret;
558
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600559 assert(client->optlen);
Vladimir Sementsov-Ogievskiy2e425fd2018-03-12 18:21:19 +0300560 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
561 nbd_opt_lookup(client->opt));
Eric Blakea16a7902018-01-10 17:08:20 -0600562 if (fatal && !ret) {
Eric Blake894e0282018-01-10 17:08:24 -0600563 error_setg(errp, "option '%s' has unexpected length",
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600564 nbd_opt_lookup(client->opt));
Eric Blakea16a7902018-01-10 17:08:20 -0600565 return -EINVAL;
566 }
567 return ret;
568}
569
Eric Blakef37708f2017-07-07 15:30:46 -0500570/* Handle NBD_OPT_INFO and NBD_OPT_GO.
571 * Return -errno on error, 0 if ready for next option, and 1 to move
572 * into transmission phase. */
Eric Blakedbb38ca2019-08-23 09:37:22 -0500573static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
Eric Blakef37708f2017-07-07 15:30:46 -0500574{
575 int rc;
Eric Blake9d7ab222019-11-13 20:46:32 -0600576 g_autofree char *name = NULL;
Eric Blakef37708f2017-07-07 15:30:46 -0500577 NBDExport *exp;
578 uint16_t requests;
579 uint16_t request;
Christian Borntraegerbbc35fc2020-09-30 17:58:57 +0200580 uint32_t namelen = 0;
Eric Blakef37708f2017-07-07 15:30:46 -0500581 bool sendname = false;
Eric Blake0c1d50b2017-07-07 15:30:48 -0500582 bool blocksize = false;
583 uint32_t sizes[3];
Eric Blakef37708f2017-07-07 15:30:46 -0500584 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
Eric Blake6e280642019-04-02 22:05:21 -0500585 uint32_t check_align = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500586 uint16_t myflags;
Eric Blakef37708f2017-07-07 15:30:46 -0500587
588 /* Client sends:
589 4 bytes: L, name length (can be 0)
590 L bytes: export name
591 2 bytes: N, number of requests (can be 0)
592 N * 2 bytes: N requests
593 */
Eric Blake9d7ab222019-11-13 20:46:32 -0600594 rc = nbd_opt_read_name(client, &name, &namelen, errp);
Eric Blake894e0282018-01-10 17:08:24 -0600595 if (rc <= 0) {
596 return rc;
Eric Blakef37708f2017-07-07 15:30:46 -0500597 }
Eric Blakef37708f2017-07-07 15:30:46 -0500598 trace_nbd_negotiate_handle_export_name_request(name);
599
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500600 rc = nbd_opt_read(client, &requests, sizeof(requests), false, errp);
Eric Blake894e0282018-01-10 17:08:24 -0600601 if (rc <= 0) {
602 return rc;
Eric Blakef37708f2017-07-07 15:30:46 -0500603 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100604 requests = be16_to_cpu(requests);
Eric Blakef37708f2017-07-07 15:30:46 -0500605 trace_nbd_negotiate_handle_info_requests(requests);
Eric Blakef37708f2017-07-07 15:30:46 -0500606 while (requests--) {
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500607 rc = nbd_opt_read(client, &request, sizeof(request), false, errp);
Eric Blake894e0282018-01-10 17:08:24 -0600608 if (rc <= 0) {
609 return rc;
Eric Blakef37708f2017-07-07 15:30:46 -0500610 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100611 request = be16_to_cpu(request);
Eric Blakef37708f2017-07-07 15:30:46 -0500612 trace_nbd_negotiate_handle_info_request(request,
613 nbd_info_lookup(request));
Eric Blake0c1d50b2017-07-07 15:30:48 -0500614 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
615 * everything else is either a request we don't know or
616 * something we send regardless of request */
617 switch (request) {
618 case NBD_INFO_NAME:
Eric Blakef37708f2017-07-07 15:30:46 -0500619 sendname = true;
Eric Blake0c1d50b2017-07-07 15:30:48 -0500620 break;
621 case NBD_INFO_BLOCK_SIZE:
622 blocksize = true;
623 break;
Eric Blakef37708f2017-07-07 15:30:46 -0500624 }
625 }
Eric Blake894e0282018-01-10 17:08:24 -0600626 if (client->optlen) {
627 return nbd_reject_length(client, false, errp);
628 }
Eric Blakef37708f2017-07-07 15:30:46 -0500629
630 exp = nbd_export_find(name);
631 if (!exp) {
Eric Blake5c4fe012020-06-08 13:26:37 -0500632 g_autofree char *sane_name = nbd_sanitize_name(name);
633
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600634 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
635 errp, "export '%s' not present",
Eric Blake5c4fe012020-06-08 13:26:37 -0500636 sane_name);
Eric Blakef37708f2017-07-07 15:30:46 -0500637 }
638
639 /* Don't bother sending NBD_INFO_NAME unless client requested it */
640 if (sendname) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600641 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
Eric Blakef37708f2017-07-07 15:30:46 -0500642 errp);
643 if (rc < 0) {
644 return rc;
645 }
646 }
647
648 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
649 * client request */
650 if (exp->description) {
651 size_t len = strlen(exp->description);
652
Eric Blake93676c82019-11-13 20:46:34 -0600653 assert(len <= NBD_MAX_STRING_SIZE);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600654 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
Eric Blakef37708f2017-07-07 15:30:46 -0500655 len, exp->description, errp);
656 if (rc < 0) {
657 return rc;
658 }
659 }
660
Eric Blake0c1d50b2017-07-07 15:30:48 -0500661 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
662 * according to whether the client requested it, and according to
663 * whether this is OPT_INFO or OPT_GO. */
Eric Blakeb0245d62019-03-30 20:36:36 -0500664 /* minimum - 1 for back-compat, or actual if client will obey it. */
665 if (client->opt == NBD_OPT_INFO || blocksize) {
Kevin Wolf37a4f702020-09-24 17:27:08 +0200666 check_align = sizes[0] = blk_get_request_alignment(exp->common.blk);
Eric Blakeb0245d62019-03-30 20:36:36 -0500667 } else {
668 sizes[0] = 1;
669 }
670 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
Eric Blake0c1d50b2017-07-07 15:30:48 -0500671 /* preferred - Hard-code to 4096 for now.
672 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
Eric Blakeb0245d62019-03-30 20:36:36 -0500673 sizes[1] = MAX(4096, sizes[0]);
Eric Blake0c1d50b2017-07-07 15:30:48 -0500674 /* maximum - At most 32M, but smaller as appropriate. */
Kevin Wolf37a4f702020-09-24 17:27:08 +0200675 sizes[2] = MIN(blk_get_max_transfer(exp->common.blk), NBD_MAX_BUFFER_SIZE);
Eric Blake0c1d50b2017-07-07 15:30:48 -0500676 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100677 sizes[0] = cpu_to_be32(sizes[0]);
678 sizes[1] = cpu_to_be32(sizes[1]);
679 sizes[2] = cpu_to_be32(sizes[2]);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600680 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
Eric Blake0c1d50b2017-07-07 15:30:48 -0500681 sizeof(sizes), sizes, errp);
682 if (rc < 0) {
683 return rc;
684 }
685
Eric Blakef37708f2017-07-07 15:30:46 -0500686 /* Send NBD_INFO_EXPORT always */
Eric Blakedbb38ca2019-08-23 09:37:22 -0500687 myflags = exp->nbdflags;
688 if (client->structured_reply) {
689 myflags |= NBD_FLAG_SEND_DF;
690 }
691 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
Eric Blakef37708f2017-07-07 15:30:46 -0500692 stq_be_p(buf, exp->size);
Eric Blakedbb38ca2019-08-23 09:37:22 -0500693 stw_be_p(buf + 8, myflags);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600694 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
Eric Blakef37708f2017-07-07 15:30:46 -0500695 sizeof(buf), buf, errp);
696 if (rc < 0) {
697 return rc;
698 }
699
Eric Blake099fbcd2019-04-02 22:05:22 -0500700 /*
701 * If the client is just asking for NBD_OPT_INFO, but forgot to
702 * request block sizes in a situation that would impact
703 * performance, then return an error. But for NBD_OPT_GO, we
704 * tolerate all clients, regardless of alignments.
705 */
706 if (client->opt == NBD_OPT_INFO && !blocksize &&
Kevin Wolf37a4f702020-09-24 17:27:08 +0200707 blk_get_request_alignment(exp->common.blk) > 1) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600708 return nbd_negotiate_send_rep_err(client,
709 NBD_REP_ERR_BLOCK_SIZE_REQD,
Eric Blake0c1d50b2017-07-07 15:30:48 -0500710 errp,
711 "request NBD_INFO_BLOCK_SIZE to "
712 "use this export");
713 }
714
Eric Blakef37708f2017-07-07 15:30:46 -0500715 /* Final reply */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600716 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
Eric Blakef37708f2017-07-07 15:30:46 -0500717 if (rc < 0) {
718 return rc;
719 }
720
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600721 if (client->opt == NBD_OPT_GO) {
Eric Blakef37708f2017-07-07 15:30:46 -0500722 client->exp = exp;
Eric Blake6e280642019-04-02 22:05:21 -0500723 client->check_align = check_align;
Eric Blakef37708f2017-07-07 15:30:46 -0500724 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
Kevin Wolfc69de1b2020-09-24 17:26:59 +0200725 blk_exp_ref(&client->exp->common);
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +0300726 nbd_check_meta_export(client);
Eric Blakef37708f2017-07-07 15:30:46 -0500727 rc = 1;
728 }
729 return rc;
Eric Blakef37708f2017-07-07 15:30:46 -0500730}
731
732
Eric Blake36683282016-10-14 13:33:09 -0500733/* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
734 * new channel for all further (now-encrypted) communication. */
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000735static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300736 Error **errp)
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000737{
738 QIOChannel *ioc;
739 QIOChannelTLS *tioc;
740 struct NBDTLSHandshakeData data = { 0 };
741
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600742 assert(client->opt == NBD_OPT_STARTTLS);
743
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300744 trace_nbd_negotiate_handle_starttls();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000745 ioc = client->ioc;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000746
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -0600747 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
Eric Blake63d5ef82016-05-11 16:39:36 -0600748 return NULL;
749 }
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000750
751 tioc = qio_channel_tls_new_server(ioc,
752 client->tlscreds,
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000753 client->tlsauthz,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300754 errp);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000755 if (!tioc) {
756 return NULL;
757 }
758
Daniel P. Berrange0d73f722016-09-30 11:57:14 +0100759 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300760 trace_nbd_negotiate_handle_starttls_handshake();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000761 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
762 qio_channel_tls_handshake(tioc,
763 nbd_tls_handshake,
764 &data,
Peter Xu1939ccd2018-03-05 14:43:24 +0800765 NULL,
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000766 NULL);
767
768 if (!data.complete) {
769 g_main_loop_run(data.loop);
770 }
771 g_main_loop_unref(data.loop);
772 if (data.error) {
773 object_unref(OBJECT(tioc));
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +0300774 error_propagate(errp, data.error);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000775 return NULL;
776 }
777
778 return QIO_CHANNEL(tioc);
779}
780
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300781/* nbd_negotiate_send_meta_context
782 *
783 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
784 *
785 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
786 */
787static int nbd_negotiate_send_meta_context(NBDClient *client,
788 const char *context,
789 uint32_t context_id,
790 Error **errp)
791{
792 NBDOptionReplyMetaContext opt;
793 struct iovec iov[] = {
794 {.iov_base = &opt, .iov_len = sizeof(opt)},
795 {.iov_base = (void *)context, .iov_len = strlen(context)}
796 };
797
Eric Blake93676c82019-11-13 20:46:34 -0600798 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300799 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
800 context_id = 0;
801 }
802
Eric Blake2b53af22018-03-30 08:09:50 -0500803 trace_nbd_negotiate_meta_query_reply(context, context_id);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300804 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
805 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
806 stl_be_p(&opt.context_id, context_id);
807
808 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
809}
810
Eric Blakeebd57062020-09-30 07:11:03 -0500811/*
812 * Return true if @query matches @pattern, or if @query is empty when
813 * the @client is performing _LIST_.
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500814 */
Eric Blakeebd57062020-09-30 07:11:03 -0500815static bool nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
816 const char *query)
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500817{
Eric Blakeebd57062020-09-30 07:11:03 -0500818 if (!*query) {
819 trace_nbd_negotiate_meta_query_parse("empty");
820 return client->opt == NBD_OPT_LIST_META_CONTEXT;
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500821 }
Eric Blakeebd57062020-09-30 07:11:03 -0500822 if (strcmp(query, pattern) == 0) {
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500823 trace_nbd_negotiate_meta_query_parse(pattern);
Eric Blakeebd57062020-09-30 07:11:03 -0500824 return true;
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500825 }
Eric Blakeebd57062020-09-30 07:11:03 -0500826 trace_nbd_negotiate_meta_query_skip("pattern not matched");
827 return false;
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500828}
829
830/*
Eric Blakeebd57062020-09-30 07:11:03 -0500831 * Return true and adjust @str in place if it begins with @prefix.
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500832 */
Eric Blakeebd57062020-09-30 07:11:03 -0500833static bool nbd_strshift(const char **str, const char *prefix)
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500834{
Eric Blakeebd57062020-09-30 07:11:03 -0500835 size_t len = strlen(prefix);
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500836
Eric Blakeebd57062020-09-30 07:11:03 -0500837 if (strncmp(*str, prefix, len) == 0) {
838 *str += len;
839 return true;
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500840 }
Eric Blakeebd57062020-09-30 07:11:03 -0500841 return false;
Vladimir Sementsov-Ogievskiyb0769d82018-06-19 16:55:09 -0500842}
843
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300844/* nbd_meta_base_query
845 *
Vladimir Sementsov-Ogievskiydbb8b392018-06-09 18:17:53 +0300846 * Handle queries to 'base' namespace. For now, only the base:allocation
Eric Blakeebd57062020-09-30 07:11:03 -0500847 * context is available. Return true if @query has been handled.
Vladimir Sementsov-Ogievskiydbb8b392018-06-09 18:17:53 +0300848 */
Eric Blakeebd57062020-09-30 07:11:03 -0500849static bool nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
850 const char *query)
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300851{
Eric Blakeebd57062020-09-30 07:11:03 -0500852 if (!nbd_strshift(&query, "base:")) {
853 return false;
854 }
855 trace_nbd_negotiate_meta_query_parse("base:");
856
857 if (nbd_meta_empty_or_pattern(client, "allocation", query)) {
858 meta->base_allocation = true;
859 }
860 return true;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300861}
862
Eric Blakeebd57062020-09-30 07:11:03 -0500863/* nbd_meta_qemu_query
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300864 *
Eric Blakeebd57062020-09-30 07:11:03 -0500865 * Handle queries to 'qemu' namespace. For now, only the qemu:dirty-bitmap:
Eric Blake71719cd2020-10-27 00:05:54 -0500866 * and qemu:allocation-depth contexts are available. Return true if @query
867 * has been handled.
Eric Blakeebd57062020-09-30 07:11:03 -0500868 */
869static bool nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
870 const char *query)
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300871{
Eric Blake3b1f2442020-10-27 00:05:52 -0500872 size_t i;
873
Eric Blakeebd57062020-09-30 07:11:03 -0500874 if (!nbd_strshift(&query, "qemu:")) {
875 return false;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300876 }
Eric Blakeebd57062020-09-30 07:11:03 -0500877 trace_nbd_negotiate_meta_query_parse("qemu:");
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300878
Eric Blakeebd57062020-09-30 07:11:03 -0500879 if (!*query) {
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300880 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
Eric Blake71719cd2020-10-27 00:05:54 -0500881 meta->allocation_depth = meta->exp->allocation_depth;
Eric Blake3b1f2442020-10-27 00:05:52 -0500882 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300883 }
884 trace_nbd_negotiate_meta_query_parse("empty");
Eric Blakeebd57062020-09-30 07:11:03 -0500885 return true;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300886 }
887
Eric Blake71719cd2020-10-27 00:05:54 -0500888 if (strcmp(query, "allocation-depth") == 0) {
889 trace_nbd_negotiate_meta_query_parse("allocation-depth");
890 meta->allocation_depth = meta->exp->allocation_depth;
891 return true;
892 }
893
Eric Blakeebd57062020-09-30 07:11:03 -0500894 if (nbd_strshift(&query, "dirty-bitmap:")) {
895 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
Eric Blake3b1f2442020-10-27 00:05:52 -0500896 if (!*query) {
897 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
898 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
899 }
900 trace_nbd_negotiate_meta_query_parse("empty");
Eric Blakeebd57062020-09-30 07:11:03 -0500901 return true;
902 }
Eric Blake3b1f2442020-10-27 00:05:52 -0500903
904 for (i = 0; i < meta->exp->nr_export_bitmaps; i++) {
905 const char *bm_name;
906
907 bm_name = bdrv_dirty_bitmap_name(meta->exp->export_bitmaps[i]);
908 if (strcmp(bm_name, query) == 0) {
909 meta->bitmaps[i] = true;
910 trace_nbd_negotiate_meta_query_parse(query);
911 return true;
912 }
Eric Blakeebd57062020-09-30 07:11:03 -0500913 }
Eric Blake3b1f2442020-10-27 00:05:52 -0500914 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap match");
Eric Blakeebd57062020-09-30 07:11:03 -0500915 return true;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300916 }
917
Eric Blake71719cd2020-10-27 00:05:54 -0500918 trace_nbd_negotiate_meta_query_skip("unknown qemu context");
Eric Blakeebd57062020-09-30 07:11:03 -0500919 return true;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300920}
921
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300922/* nbd_negotiate_meta_query
923 *
924 * Parse namespace name and call corresponding function to parse body of the
925 * query.
926 *
Eric Blake93676c82019-11-13 20:46:34 -0600927 * The only supported namespaces are 'base' and 'qemu'.
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300928 *
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300929 * Return -errno on I/O error, 0 if option was completely handled by
930 * sending a reply about inconsistent lengths, or 1 on success. */
931static int nbd_negotiate_meta_query(NBDClient *client,
932 NBDExportMetaContexts *meta, Error **errp)
933{
934 int ret;
Eric Blakeebd57062020-09-30 07:11:03 -0500935 g_autofree char *query = NULL;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300936 uint32_t len;
937
Eric Blaked1e2c3e2020-09-30 07:11:02 -0500938 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300939 if (ret <= 0) {
940 return ret;
941 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100942 len = cpu_to_be32(len);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300943
Eric Blake93676c82019-11-13 20:46:34 -0600944 if (len > NBD_MAX_STRING_SIZE) {
945 trace_nbd_negotiate_meta_query_skip("length too long");
946 return nbd_opt_skip(client, len, errp);
947 }
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300948
Eric Blakeebd57062020-09-30 07:11:03 -0500949 query = g_malloc(len + 1);
950 ret = nbd_opt_read(client, query, len, true, errp);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300951 if (ret <= 0) {
952 return ret;
953 }
Eric Blakeebd57062020-09-30 07:11:03 -0500954 query[len] = '\0';
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300955
Eric Blakeebd57062020-09-30 07:11:03 -0500956 if (nbd_meta_base_query(client, meta, query)) {
957 return 1;
958 }
959 if (nbd_meta_qemu_query(client, meta, query)) {
960 return 1;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300961 }
962
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +0300963 trace_nbd_negotiate_meta_query_skip("unknown namespace");
Eric Blakeebd57062020-09-30 07:11:03 -0500964 return 1;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300965}
966
967/* nbd_negotiate_meta_queries
968 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
969 *
970 * Return -errno on I/O error, or 0 if option was completely handled. */
971static int nbd_negotiate_meta_queries(NBDClient *client,
972 NBDExportMetaContexts *meta, Error **errp)
973{
974 int ret;
Eric Blake9d7ab222019-11-13 20:46:32 -0600975 g_autofree char *export_name = NULL;
Eric Blake3b1f2442020-10-27 00:05:52 -0500976 g_autofree bool *bitmaps = NULL;
977 NBDExportMetaContexts local_meta = {0};
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300978 uint32_t nb_queries;
Eric Blake3b1f2442020-10-27 00:05:52 -0500979 size_t i;
Eric Blake47ec4852020-10-27 00:05:51 -0500980 size_t count = 0;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300981
982 if (!client->structured_reply) {
983 return nbd_opt_invalid(client, errp,
984 "request option '%s' when structured reply "
985 "is not negotiated",
986 nbd_opt_lookup(client->opt));
987 }
988
989 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
990 /* Only change the caller's meta on SET. */
991 meta = &local_meta;
992 }
993
Eric Blake3b1f2442020-10-27 00:05:52 -0500994 g_free(meta->bitmaps);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300995 memset(meta, 0, sizeof(*meta));
996
Eric Blake9d7ab222019-11-13 20:46:32 -0600997 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +0300998 if (ret <= 0) {
999 return ret;
1000 }
1001
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +03001002 meta->exp = nbd_export_find(export_name);
1003 if (meta->exp == NULL) {
Eric Blake5c4fe012020-06-08 13:26:37 -05001004 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1005
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001006 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
Eric Blake5c4fe012020-06-08 13:26:37 -05001007 "export '%s' not present", sane_name);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001008 }
Eric Blake3b1f2442020-10-27 00:05:52 -05001009 meta->bitmaps = g_new0(bool, meta->exp->nr_export_bitmaps);
1010 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1011 bitmaps = meta->bitmaps;
1012 }
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001013
Eric Blaked1e2c3e2020-09-30 07:11:02 -05001014 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), false, errp);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001015 if (ret <= 0) {
1016 return ret;
1017 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +01001018 nb_queries = cpu_to_be32(nb_queries);
Eric Blake2b53af22018-03-30 08:09:50 -05001019 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
Vladimir Sementsov-Ogievskiyaf736e52018-06-09 18:17:54 +03001020 export_name, nb_queries);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001021
1022 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1023 /* enable all known contexts */
1024 meta->base_allocation = true;
Eric Blake71719cd2020-10-27 00:05:54 -05001025 meta->allocation_depth = meta->exp->allocation_depth;
Eric Blake3b1f2442020-10-27 00:05:52 -05001026 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001027 } else {
1028 for (i = 0; i < nb_queries; ++i) {
1029 ret = nbd_negotiate_meta_query(client, meta, errp);
1030 if (ret <= 0) {
1031 return ret;
1032 }
1033 }
1034 }
1035
1036 if (meta->base_allocation) {
1037 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1038 NBD_META_ID_BASE_ALLOCATION,
1039 errp);
1040 if (ret < 0) {
1041 return ret;
1042 }
Eric Blake47ec4852020-10-27 00:05:51 -05001043 count++;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001044 }
1045
Eric Blake71719cd2020-10-27 00:05:54 -05001046 if (meta->allocation_depth) {
1047 ret = nbd_negotiate_send_meta_context(client, "qemu:allocation-depth",
1048 NBD_META_ID_ALLOCATION_DEPTH,
1049 errp);
1050 if (ret < 0) {
1051 return ret;
1052 }
1053 count++;
1054 }
1055
Eric Blake3b1f2442020-10-27 00:05:52 -05001056 for (i = 0; i < meta->exp->nr_export_bitmaps; i++) {
1057 const char *bm_name;
1058 g_autofree char *context = NULL;
1059
1060 if (!meta->bitmaps[i]) {
1061 continue;
1062 }
1063
1064 bm_name = bdrv_dirty_bitmap_name(meta->exp->export_bitmaps[i]);
1065 context = g_strdup_printf("qemu:dirty-bitmap:%s", bm_name);
Eric Blake02e87e32020-10-27 00:05:50 -05001066
1067 ret = nbd_negotiate_send_meta_context(client, context,
Eric Blake3b1f2442020-10-27 00:05:52 -05001068 NBD_META_ID_DIRTY_BITMAP + i,
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03001069 errp);
1070 if (ret < 0) {
1071 return ret;
1072 }
Eric Blake47ec4852020-10-27 00:05:51 -05001073 count++;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03001074 }
1075
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001076 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1077 if (ret == 0) {
Eric Blake47ec4852020-10-27 00:05:51 -05001078 meta->count = count;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001079 }
1080
1081 return ret;
1082}
1083
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001084/* nbd_negotiate_options
Eric Blakef37708f2017-07-07 15:30:46 -05001085 * Process all NBD_OPT_* client option commands, during fixed newstyle
1086 * negotiation.
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001087 * Return:
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001088 * -errno on error, errp is set
1089 * 0 on successful negotiation, errp is not set
1090 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1091 * errp is not set
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001092 */
Eric Blakedbb38ca2019-08-23 09:37:22 -05001093static int nbd_negotiate_options(NBDClient *client, Error **errp)
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001094{
Max Reitz9c122ad2015-02-25 13:08:31 -05001095 uint32_t flags;
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001096 bool fixedNewstyle = false;
Eric Blake23e099c2017-07-07 15:30:45 -05001097 bool no_zeroes = false;
Max Reitz9c122ad2015-02-25 13:08:31 -05001098
1099 /* Client sends:
1100 [ 0 .. 3] client flags
1101
Eric Blakef37708f2017-07-07 15:30:46 -05001102 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
Max Reitz9c122ad2015-02-25 13:08:31 -05001103 [ 0 .. 7] NBD_OPTS_MAGIC
1104 [ 8 .. 11] NBD option
1105 [12 .. 15] Data length
1106 ... Rest of request
1107
1108 [ 0 .. 7] NBD_OPTS_MAGIC
1109 [ 8 .. 11] Second NBD option
1110 [12 .. 15] Data length
1111 ... Rest of request
1112 */
1113
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001114 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
Max Reitz9c122ad2015-02-25 13:08:31 -05001115 return -EIO;
1116 }
Eric Blake621c4f42017-07-07 15:30:44 -05001117 trace_nbd_negotiate_options_flags(flags);
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001118 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001119 fixedNewstyle = true;
1120 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1121 }
Eric Blakec203c592016-10-14 13:33:14 -05001122 if (flags & NBD_FLAG_C_NO_ZEROES) {
Eric Blake23e099c2017-07-07 15:30:45 -05001123 no_zeroes = true;
Eric Blakec203c592016-10-14 13:33:14 -05001124 flags &= ~NBD_FLAG_C_NO_ZEROES;
1125 }
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001126 if (flags != 0) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001127 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
Eric Blake621c4f42017-07-07 15:30:44 -05001128 return -EINVAL;
Max Reitz9c122ad2015-02-25 13:08:31 -05001129 }
1130
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001131 while (1) {
Max Reitz9c122ad2015-02-25 13:08:31 -05001132 int ret;
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001133 uint32_t option, length;
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001134 uint64_t magic;
1135
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001136 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001137 return -EINVAL;
1138 }
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001139 trace_nbd_negotiate_options_check_magic(magic);
1140 if (magic != NBD_OPTS_MAGIC) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001141 error_setg(errp, "Bad magic received");
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001142 return -EINVAL;
1143 }
1144
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001145 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001146 return -EINVAL;
1147 }
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001148 client->opt = option;
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001149
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001150 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001151 return -EINVAL;
1152 }
Eric Blake894e0282018-01-10 17:08:24 -06001153 assert(!client->optlen);
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001154 client->optlen = length;
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001155
Eric Blakefdad35e2017-11-22 16:25:16 -06001156 if (length > NBD_MAX_BUFFER_SIZE) {
1157 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1158 length, NBD_MAX_BUFFER_SIZE);
1159 return -EINVAL;
1160 }
1161
Eric Blake3736cc52017-07-07 15:30:43 -05001162 trace_nbd_negotiate_options_check_option(option,
1163 nbd_opt_lookup(option));
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001164 if (client->tlscreds &&
1165 client->ioc == (QIOChannel *)client->sioc) {
1166 QIOChannel *tioc;
1167 if (!fixedNewstyle) {
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001168 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001169 return -EINVAL;
1170 }
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001171 switch (option) {
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001172 case NBD_OPT_STARTTLS:
Eric Blakee68c35c2017-10-27 12:40:31 +02001173 if (length) {
1174 /* Unconditionally drop the connection if the client
1175 * can't start a TLS negotiation correctly */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001176 return nbd_reject_length(client, true, errp);
Eric Blakee68c35c2017-10-27 12:40:31 +02001177 }
1178 tioc = nbd_negotiate_handle_starttls(client, errp);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001179 if (!tioc) {
1180 return -EIO;
1181 }
Eric Blake8cbee492017-10-27 12:40:30 +02001182 ret = 0;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001183 object_unref(OBJECT(client->ioc));
1184 client->ioc = QIO_CHANNEL(tioc);
1185 break;
1186
Eric Blaked1129a82016-04-14 16:02:23 -06001187 case NBD_OPT_EXPORT_NAME:
1188 /* No way to return an error to client, so drop connection */
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001189 error_setg(errp, "Option 0x%x not permitted before TLS",
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001190 option);
Eric Blaked1129a82016-04-14 16:02:23 -06001191 return -EINVAL;
1192
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001193 default:
Eric Blake3e99ebb2018-11-17 16:32:21 -06001194 /* Let the client keep trying, unless they asked to
1195 * quit. Always try to give an error back to the
1196 * client; but when replying to OPT_ABORT, be aware
1197 * that the client may hang up before receiving the
1198 * error, in which case we are fine ignoring the
1199 * resulting EPIPE. */
1200 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1201 option == NBD_OPT_ABORT ? NULL : errp,
Eric Blake894e0282018-01-10 17:08:24 -06001202 "Option 0x%" PRIx32
Daniel P. Berrangé0b0bb122018-11-16 15:53:20 +00001203 " not permitted before TLS", option);
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001204 if (option == NBD_OPT_ABORT) {
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001205 return 1;
Eric Blakeb6f5d3b2016-10-14 13:33:16 -05001206 }
Eric Blaked1129a82016-04-14 16:02:23 -06001207 break;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001208 }
1209 } else if (fixedNewstyle) {
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001210 switch (option) {
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001211 case NBD_OPT_LIST:
Eric Blakee68c35c2017-10-27 12:40:31 +02001212 if (length) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001213 ret = nbd_reject_length(client, false, errp);
Eric Blakee68c35c2017-10-27 12:40:31 +02001214 } else {
1215 ret = nbd_negotiate_handle_list(client, errp);
1216 }
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001217 break;
1218
1219 case NBD_OPT_ABORT:
Eric Blakeb6f5d3b2016-10-14 13:33:16 -05001220 /* NBD spec says we must try to reply before
1221 * disconnecting, but that we must also tolerate
1222 * guests that don't wait for our reply. */
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001223 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001224 return 1;
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001225
1226 case NBD_OPT_EXPORT_NAME:
Eric Blakedbb38ca2019-08-23 09:37:22 -05001227 return nbd_negotiate_handle_export_name(client, no_zeroes,
Eric Blake23e099c2017-07-07 15:30:45 -05001228 errp);
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001229
Eric Blakef37708f2017-07-07 15:30:46 -05001230 case NBD_OPT_INFO:
1231 case NBD_OPT_GO:
Eric Blakedbb38ca2019-08-23 09:37:22 -05001232 ret = nbd_negotiate_handle_info(client, errp);
Eric Blakef37708f2017-07-07 15:30:46 -05001233 if (ret == 1) {
1234 assert(option == NBD_OPT_GO);
1235 return 0;
1236 }
Eric Blakef37708f2017-07-07 15:30:46 -05001237 break;
1238
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001239 case NBD_OPT_STARTTLS:
Eric Blakee68c35c2017-10-27 12:40:31 +02001240 if (length) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001241 ret = nbd_reject_length(client, false, errp);
Eric Blakee68c35c2017-10-27 12:40:31 +02001242 } else if (client->tlscreds) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001243 ret = nbd_negotiate_send_rep_err(client,
1244 NBD_REP_ERR_INVALID, errp,
Eric Blake36683282016-10-14 13:33:09 -05001245 "TLS already enabled");
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001246 } else {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001247 ret = nbd_negotiate_send_rep_err(client,
1248 NBD_REP_ERR_POLICY, errp,
Eric Blake36683282016-10-14 13:33:09 -05001249 "TLS not configured");
Eric Blake63d5ef82016-05-11 16:39:36 -06001250 }
Eric Blaked1129a82016-04-14 16:02:23 -06001251 break;
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001252
1253 case NBD_OPT_STRUCTURED_REPLY:
1254 if (length) {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001255 ret = nbd_reject_length(client, false, errp);
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001256 } else if (client->structured_reply) {
1257 ret = nbd_negotiate_send_rep_err(
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001258 client, NBD_REP_ERR_INVALID, errp,
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001259 "structured reply already negotiated");
1260 } else {
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001261 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001262 client->structured_reply = true;
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001263 }
1264 break;
1265
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03001266 case NBD_OPT_LIST_META_CONTEXT:
1267 case NBD_OPT_SET_META_CONTEXT:
1268 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1269 errp);
1270 break;
1271
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001272 default:
Eric Blake894e0282018-01-10 17:08:24 -06001273 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +03001274 "Unsupported option %" PRIu32 " (%s)",
Eric Blake894e0282018-01-10 17:08:24 -06001275 option, nbd_opt_lookup(option));
Eric Blake156f6a12016-04-06 16:48:38 -06001276 break;
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +01001277 }
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001278 } else {
1279 /*
1280 * If broken new-style we should drop the connection
1281 * for anything except NBD_OPT_EXPORT_NAME
1282 */
Vladimir Sementsov-Ogievskiy7f9039c2017-07-07 18:29:16 +03001283 switch (option) {
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001284 case NBD_OPT_EXPORT_NAME:
Eric Blakedbb38ca2019-08-23 09:37:22 -05001285 return nbd_negotiate_handle_export_name(client, no_zeroes,
Eric Blake23e099c2017-07-07 15:30:45 -05001286 errp);
Hani Benhabiles32d7d2e2014-06-07 01:32:32 +01001287
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001288 default:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +03001289 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
Eric Blake3736cc52017-07-07 15:30:43 -05001290 option, nbd_opt_lookup(option));
Daniel P. Berrange26afa862016-02-10 18:41:06 +00001291 return -EINVAL;
1292 }
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001293 }
Eric Blake8cbee492017-10-27 12:40:30 +02001294 if (ret < 0) {
1295 return ret;
1296 }
Hani Benhabilesf5076b52014-06-07 01:32:31 +01001297 }
1298}
1299
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001300/* nbd_negotiate
1301 * Return:
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001302 * -errno on error, errp is set
1303 * 0 on successful negotiation, errp is not set
1304 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1305 * errp is not set
Vladimir Sementsov-Ogievskiy1e120ff2017-07-07 18:29:09 +03001306 */
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001307static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
bellard7a5ca862008-05-27 21:13:40 +00001308{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +02001309 ERRP_GUARD();
Eric Blake5f66d062017-07-17 14:26:35 -05001310 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
Vladimir Sementsov-Ogievskiy2e5c9ad2017-06-02 18:01:49 +03001311 int ret;
bellard7a5ca862008-05-27 21:13:40 +00001312
Eric Blake5f66d062017-07-17 14:26:35 -05001313 /* Old style negotiation header, no room for options
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001314 [ 0 .. 7] passwd ("NBDMAGIC")
1315 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +00001316 [16 .. 23] size
Eric Blake5f66d062017-07-17 14:26:35 -05001317 [24 .. 27] export flags (zero-extended)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001318 [28 .. 151] reserved (0)
1319
Eric Blake5f66d062017-07-17 14:26:35 -05001320 New style negotiation header, client can send options
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001321 [ 0 .. 7] passwd ("NBDMAGIC")
1322 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1323 [16 .. 17] server flags (0)
Eric Blakef37708f2017-07-07 15:30:46 -05001324 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
Nick Thomasb2e3d872011-02-22 15:44:51 +00001325 */
bellard7a5ca862008-05-27 21:13:40 +00001326
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +00001327 qio_channel_set_blocking(client->ioc, false, NULL);
Paolo Bonzini185b4332012-03-05 08:56:10 +01001328
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001329 trace_nbd_negotiate_begin();
Nick Thomasb2e3d872011-02-22 15:44:51 +00001330 memcpy(buf, "NBDMAGIC", 8);
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001331
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03001332 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1333 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
bellard7a5ca862008-05-27 21:13:40 +00001334
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03001335 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1336 error_prepend(errp, "write failed: ");
1337 return -EINVAL;
1338 }
Eric Blakedbb38ca2019-08-23 09:37:22 -05001339 ret = nbd_negotiate_options(client, errp);
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03001340 if (ret != 0) {
1341 if (ret < 0) {
1342 error_prepend(errp, "option negotiation failed: ");
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001343 }
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03001344 return ret;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001345 }
bellard7a5ca862008-05-27 21:13:40 +00001346
Sergio Lopezb4961242019-09-12 13:00:33 +02001347 /* Attach the channel to the same AioContext as the export */
Kevin Wolf8612c682020-09-24 17:27:00 +02001348 if (client->exp && client->exp->common.ctx) {
1349 qio_channel_attach_aio_context(client->ioc, client->exp->common.ctx);
Sergio Lopezb4961242019-09-12 13:00:33 +02001350 }
1351
Vladimir Sementsov-Ogievskiy0cfae922018-01-10 17:08:21 -06001352 assert(!client->optlen);
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001353 trace_nbd_negotiate_success();
Vladimir Sementsov-Ogievskiyd9faeed2017-06-02 18:01:48 +03001354
1355 return 0;
bellard7a5ca862008-05-27 21:13:40 +00001356}
1357
Sergio Lopezf148ae72020-12-14 18:05:18 +01001358/* nbd_read_eof
1359 * Tries to read @size bytes from @ioc. This is a local implementation of
1360 * qio_channel_readv_all_eof. We have it here because we need it to be
1361 * interruptible and to know when the coroutine is yielding.
1362 * Returns 1 on success
1363 * 0 on eof, when no data was read (errp is not set)
1364 * negative errno on failure (errp is set)
1365 */
1366static inline int coroutine_fn
1367nbd_read_eof(NBDClient *client, void *buffer, size_t size, Error **errp)
1368{
1369 bool partial = false;
1370
1371 assert(size);
1372 while (size > 0) {
1373 struct iovec iov = { .iov_base = buffer, .iov_len = size };
1374 ssize_t len;
1375
1376 len = qio_channel_readv(client->ioc, &iov, 1, errp);
1377 if (len == QIO_CHANNEL_ERR_BLOCK) {
1378 client->read_yielding = true;
1379 qio_channel_yield(client->ioc, G_IO_IN);
1380 client->read_yielding = false;
1381 if (client->quiescing) {
1382 return -EAGAIN;
1383 }
1384 continue;
1385 } else if (len < 0) {
1386 return -EIO;
1387 } else if (len == 0) {
1388 if (partial) {
1389 error_setg(errp,
1390 "Unexpected end-of-file before all bytes were read");
1391 return -EIO;
1392 } else {
1393 return 0;
1394 }
1395 }
1396
1397 partial = true;
1398 size -= len;
1399 buffer = (uint8_t *) buffer + len;
1400 }
1401 return 1;
1402}
1403
1404static int nbd_receive_request(NBDClient *client, NBDRequest *request,
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001405 Error **errp)
bellard7a5ca862008-05-27 21:13:40 +00001406{
Paolo Bonzinifa26c262012-08-22 15:13:30 +02001407 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +00001408 uint32_t magic;
Vladimir Sementsov-Ogievskiya0dc63a2017-06-02 18:01:42 +03001409 int ret;
bellard7a5ca862008-05-27 21:13:40 +00001410
Sergio Lopezf148ae72020-12-14 18:05:18 +01001411 ret = nbd_read_eof(client, buf, sizeof(buf), errp);
Paolo Bonzini185b4332012-03-05 08:56:10 +01001412 if (ret < 0) {
1413 return ret;
1414 }
1415
Nick Thomasb2e3d872011-02-22 15:44:51 +00001416 /* Request
1417 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
Eric Blakeb626b512016-10-14 13:33:04 -05001418 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1419 [ 6 .. 7] type (NBD_CMD_READ, ...)
Nick Thomasb2e3d872011-02-22 15:44:51 +00001420 [ 8 .. 15] handle
1421 [16 .. 23] from
1422 [24 .. 27] len
1423 */
bellard7a5ca862008-05-27 21:13:40 +00001424
Peter Maydell773dce32016-06-10 16:00:36 +01001425 magic = ldl_be_p(buf);
Eric Blakeb626b512016-10-14 13:33:04 -05001426 request->flags = lduw_be_p(buf + 4);
1427 request->type = lduw_be_p(buf + 6);
Peter Maydell773dce32016-06-10 16:00:36 +01001428 request->handle = ldq_be_p(buf + 8);
1429 request->from = ldq_be_p(buf + 16);
1430 request->len = ldl_be_p(buf + 24);
bellard7a5ca862008-05-27 21:13:40 +00001431
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001432 trace_nbd_receive_request(magic, request->flags, request->type,
1433 request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +00001434
Nick Thomasb2e3d872011-02-22 15:44:51 +00001435 if (magic != NBD_REQUEST_MAGIC) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03001436 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +01001437 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001438 }
1439 return 0;
ths75818252008-07-03 13:41:03 +00001440}
bellard7a5ca862008-05-27 21:13:40 +00001441
Paolo Bonzini41996e32011-09-19 15:25:40 +02001442#define MAX_NBD_REQUESTS 16
1443
Paolo Bonzinice339672012-09-18 13:17:52 +02001444void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +02001445{
1446 client->refcount++;
1447}
1448
Paolo Bonzinice339672012-09-18 13:17:52 +02001449void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +02001450{
1451 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001452 /* The last reference should be dropped by client->close,
Max Reitzf53a8292015-02-06 16:06:16 -05001453 * which is called by client_close.
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001454 */
1455 assert(client->closing);
1456
Paolo Bonziniff829112017-02-13 14:52:24 +01001457 qio_channel_detach_aio_context(client->ioc);
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +00001458 object_unref(OBJECT(client->sioc));
1459 object_unref(OBJECT(client->ioc));
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00001460 if (client->tlscreds) {
1461 object_unref(OBJECT(client->tlscreds));
1462 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +00001463 g_free(client->tlsauthz);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001464 if (client->exp) {
1465 QTAILQ_REMOVE(&client->exp->clients, client, next);
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001466 blk_exp_unref(&client->exp->common);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001467 }
Eric Blake3b1f2442020-10-27 00:05:52 -05001468 g_free(client->export_meta.bitmaps);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001469 g_free(client);
1470 }
1471}
1472
Eric Blake0c9390d2017-06-08 17:26:17 -05001473static void client_close(NBDClient *client, bool negotiated)
Paolo Bonzini1743b512011-09-19 14:33:23 +02001474{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001475 if (client->closing) {
1476 return;
1477 }
1478
1479 client->closing = true;
1480
1481 /* Force requests to finish. They will drop their own references,
1482 * then we'll close the socket and free the NBDClient.
1483 */
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +00001484 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1485 NULL);
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001486
1487 /* Also tell the client, so that they release their reference. */
Eric Blake0c9390d2017-06-08 17:26:17 -05001488 if (client->close_fn) {
1489 client->close_fn(client, negotiated);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001490 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001491}
1492
Eric Blake315f78a2016-10-14 13:33:05 -05001493static NBDRequestData *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001494{
Eric Blake315f78a2016-10-14 13:33:05 -05001495 NBDRequestData *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001496
Paolo Bonzini41996e32011-09-19 15:25:40 +02001497 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1498 client->nb_requests++;
1499
Eric Blake315f78a2016-10-14 13:33:05 -05001500 req = g_new0(NBDRequestData, 1);
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001501 nbd_client_get(client);
1502 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001503 return req;
1504}
1505
Eric Blake315f78a2016-10-14 13:33:05 -05001506static void nbd_request_put(NBDRequestData *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001507{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001508 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +02001509
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02001510 if (req->data) {
1511 qemu_vfree(req->data);
1512 }
Paolo Bonzini17294042015-10-01 12:59:08 +02001513 g_free(req);
Stefan Hajnoczie1adb272013-05-02 14:23:07 +02001514
Max Reitz958c7172014-06-20 21:57:32 +02001515 client->nb_requests--;
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001516
1517 if (client->quiescing && client->nb_requests == 0) {
1518 aio_wait_kick();
1519 }
1520
Paolo Bonziniff829112017-02-13 14:52:24 +01001521 nbd_client_receive_next_request(client);
1522
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001523 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001524}
1525
Max Reitzaadf99a2014-11-18 12:21:18 +01001526static void blk_aio_attached(AioContext *ctx, void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +02001527{
1528 NBDExport *exp = opaque;
1529 NBDClient *client;
1530
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001531 trace_nbd_blk_aio_attached(exp->name, ctx);
Max Reitzf2149282014-06-20 21:57:34 +02001532
Kevin Wolf8612c682020-09-24 17:27:00 +02001533 exp->common.ctx = ctx;
Max Reitzf2149282014-06-20 21:57:34 +02001534
1535 QTAILQ_FOREACH(client, &exp->clients, next) {
Paolo Bonziniff829112017-02-13 14:52:24 +01001536 qio_channel_attach_aio_context(client->ioc, ctx);
Sergio Lopezf148ae72020-12-14 18:05:18 +01001537
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001538 assert(client->nb_requests == 0);
Sergio Lopezf148ae72020-12-14 18:05:18 +01001539 assert(client->recv_coroutine == NULL);
1540 assert(client->send_coroutine == NULL);
Max Reitzf2149282014-06-20 21:57:34 +02001541 }
1542}
1543
Max Reitzaadf99a2014-11-18 12:21:18 +01001544static void blk_aio_detach(void *opaque)
Max Reitzf2149282014-06-20 21:57:34 +02001545{
1546 NBDExport *exp = opaque;
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001547 NBDClient *client;
Max Reitzf2149282014-06-20 21:57:34 +02001548
Kevin Wolf8612c682020-09-24 17:27:00 +02001549 trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
Max Reitzf2149282014-06-20 21:57:34 +02001550
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001551 QTAILQ_FOREACH(client, &exp->clients, next) {
1552 qio_channel_detach_aio_context(client->ioc);
1553 }
Max Reitzf2149282014-06-20 21:57:34 +02001554
Kevin Wolf8612c682020-09-24 17:27:00 +02001555 exp->common.ctx = NULL;
Max Reitzf2149282014-06-20 21:57:34 +02001556}
1557
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001558static void nbd_drained_begin(void *opaque)
1559{
1560 NBDExport *exp = opaque;
1561 NBDClient *client;
1562
1563 QTAILQ_FOREACH(client, &exp->clients, next) {
1564 client->quiescing = true;
1565 }
1566}
1567
1568static void nbd_drained_end(void *opaque)
1569{
1570 NBDExport *exp = opaque;
1571 NBDClient *client;
1572
1573 QTAILQ_FOREACH(client, &exp->clients, next) {
1574 client->quiescing = false;
1575 nbd_client_receive_next_request(client);
1576 }
1577}
1578
1579static bool nbd_drained_poll(void *opaque)
1580{
1581 NBDExport *exp = opaque;
1582 NBDClient *client;
1583
1584 QTAILQ_FOREACH(client, &exp->clients, next) {
1585 if (client->nb_requests != 0) {
1586 /*
1587 * If there's a coroutine waiting for a request on nbd_read_eof()
1588 * enter it here so we don't depend on the client to wake it up.
1589 */
1590 if (client->recv_coroutine != NULL && client->read_yielding) {
1591 qemu_aio_coroutine_enter(exp->common.ctx,
1592 client->recv_coroutine);
1593 }
1594
1595 return true;
1596 }
1597 }
1598
1599 return false;
1600}
1601
Max Reitz741cc432016-01-29 16:36:06 +01001602static void nbd_eject_notifier(Notifier *n, void *data)
1603{
1604 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
Eric Blake61bc8462019-09-16 21:39:17 -05001605
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001606 blk_exp_request_shutdown(&exp->common);
Max Reitz741cc432016-01-29 16:36:06 +01001607}
1608
Kevin Wolf9b562c62020-09-24 17:26:53 +02001609void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1610{
1611 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1612 assert(exp->drv == &blk_exp_nbd);
1613 assert(nbd_exp->eject_notifier_blk == NULL);
1614
1615 blk_ref(blk);
1616 nbd_exp->eject_notifier_blk = blk;
1617 nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1618 blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1619}
1620
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001621static const BlockDevOps nbd_block_ops = {
1622 .drained_begin = nbd_drained_begin,
1623 .drained_end = nbd_drained_end,
1624 .drained_poll = nbd_drained_poll,
1625};
1626
Kevin Wolf5b1cb492020-09-24 17:27:12 +02001627static int nbd_export_create(BlockExport *blk_exp, BlockExportOptions *exp_args,
1628 Error **errp)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001629{
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001630 NBDExport *exp = container_of(blk_exp, NBDExport, common);
Kevin Wolf5b1cb492020-09-24 17:27:12 +02001631 BlockExportOptionsNbd *arg = &exp_args->u.nbd;
Kevin Wolf331170e2020-09-24 17:27:09 +02001632 BlockBackend *blk = blk_exp->blk;
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001633 int64_t size;
Kevin Wolf331170e2020-09-24 17:27:09 +02001634 uint64_t perm, shared_perm;
Kevin Wolf5b1cb492020-09-24 17:27:12 +02001635 bool readonly = !exp_args->writable;
1636 bool shared = !exp_args->writable;
Eric Blakecbad81c2020-10-27 00:05:49 -05001637 strList *bitmaps;
Eric Blake3b1f2442020-10-27 00:05:52 -05001638 size_t i;
Kevin Wolfd7086422017-01-13 19:02:32 +01001639 int ret;
Kevin Wolfcd7fca92016-07-06 11:22:39 +02001640
Kevin Wolf5b1cb492020-09-24 17:27:12 +02001641 assert(exp_args->type == BLOCK_EXPORT_TYPE_NBD);
1642
1643 if (!nbd_server_is_running()) {
1644 error_setg(errp, "NBD server not running");
1645 return -EINVAL;
1646 }
1647
1648 if (!arg->has_name) {
1649 arg->name = exp_args->node_name;
1650 }
1651
1652 if (strlen(arg->name) > NBD_MAX_STRING_SIZE) {
1653 error_setg(errp, "export name '%s' too long", arg->name);
1654 return -EINVAL;
1655 }
1656
1657 if (arg->description && strlen(arg->description) > NBD_MAX_STRING_SIZE) {
1658 error_setg(errp, "description '%s' too long", arg->description);
1659 return -EINVAL;
1660 }
1661
1662 if (nbd_export_find(arg->name)) {
1663 error_setg(errp, "NBD server already has export named '%s'", arg->name);
1664 return -EEXIST;
1665 }
1666
Kevin Wolf331170e2020-09-24 17:27:09 +02001667 size = blk_getlength(blk);
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001668 if (size < 0) {
1669 error_setg_errno(errp, -size,
1670 "Failed to determine the NBD export's length");
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001671 return size;
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001672 }
1673
Kevin Wolf8a7ce4f2017-02-09 15:43:38 +01001674 /* Don't allow resize while the NBD server is running, otherwise we don't
1675 * care what happens with the node. */
Kevin Wolf331170e2020-09-24 17:27:09 +02001676 blk_get_perm(blk, &perm, &shared_perm);
Kevin Wolf331170e2020-09-24 17:27:09 +02001677 ret = blk_set_perm(blk, perm, shared_perm & ~BLK_PERM_RESIZE, errp);
Kevin Wolfd7086422017-01-13 19:02:32 +01001678 if (ret < 0) {
Kevin Wolf331170e2020-09-24 17:27:09 +02001679 return ret;
Kevin Wolfd7086422017-01-13 19:02:32 +01001680 }
Kevin Wolf331170e2020-09-24 17:27:09 +02001681
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001682 QTAILQ_INIT(&exp->clients);
Kevin Wolf5b1cb492020-09-24 17:27:12 +02001683 exp->name = g_strdup(arg->name);
1684 exp->description = g_strdup(arg->description);
Eric Blakedbb38ca2019-08-23 09:37:22 -05001685 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1686 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1687 if (readonly) {
1688 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1689 if (shared) {
1690 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1691 }
1692 } else {
Eric Blakeb491dbb2019-08-23 09:37:25 -05001693 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1694 NBD_FLAG_SEND_FAST_ZERO);
Eric Blakedbb38ca2019-08-23 09:37:22 -05001695 }
Eric Blake7596bbb2019-01-17 13:36:42 -06001696 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
Max Reitz98f44bb2015-02-25 13:08:21 -05001697
Eric Blakecbad81c2020-10-27 00:05:49 -05001698 for (bitmaps = arg->bitmaps; bitmaps; bitmaps = bitmaps->next) {
Eric Blake3b1f2442020-10-27 00:05:52 -05001699 exp->nr_export_bitmaps++;
1700 }
1701 exp->export_bitmaps = g_new0(BdrvDirtyBitmap *, exp->nr_export_bitmaps);
1702 for (i = 0, bitmaps = arg->bitmaps; bitmaps;
1703 i++, bitmaps = bitmaps->next) {
Eric Blakecbad81c2020-10-27 00:05:49 -05001704 const char *bitmap = bitmaps->value;
Kevin Wolf331170e2020-09-24 17:27:09 +02001705 BlockDriverState *bs = blk_bs(blk);
Eric Blake678ba272019-01-11 13:47:19 -06001706 BdrvDirtyBitmap *bm = NULL;
Eric Blake678ba272019-01-11 13:47:19 -06001707
Max Reitzee2f94c2019-06-12 18:55:04 +02001708 while (bs) {
Eric Blakecbad81c2020-10-27 00:05:49 -05001709 bm = bdrv_find_dirty_bitmap(bs, bitmap);
Max Reitzee2f94c2019-06-12 18:55:04 +02001710 if (bm != NULL) {
Eric Blake678ba272019-01-11 13:47:19 -06001711 break;
1712 }
1713
Max Reitzee2f94c2019-06-12 18:55:04 +02001714 bs = bdrv_filter_or_cow_bs(bs);
Eric Blake678ba272019-01-11 13:47:19 -06001715 }
1716
1717 if (bm == NULL) {
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001718 ret = -ENOENT;
Eric Blakecbad81c2020-10-27 00:05:49 -05001719 error_setg(errp, "Bitmap '%s' is not found", bitmap);
Eric Blake678ba272019-01-11 13:47:19 -06001720 goto fail;
1721 }
1722
John Snow3ae96d62019-03-12 12:05:49 -04001723 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001724 ret = -EINVAL;
John Snow3b78a922019-03-12 12:05:48 -04001725 goto fail;
1726 }
1727
Eric Blakedbb38ca2019-08-23 09:37:22 -05001728 if (readonly && bdrv_is_writable(bs) &&
Eric Blake678ba272019-01-11 13:47:19 -06001729 bdrv_dirty_bitmap_enabled(bm)) {
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001730 ret = -EINVAL;
Eric Blake678ba272019-01-11 13:47:19 -06001731 error_setg(errp,
1732 "Enabled bitmap '%s' incompatible with readonly export",
Eric Blakecbad81c2020-10-27 00:05:49 -05001733 bitmap);
Eric Blake678ba272019-01-11 13:47:19 -06001734 goto fail;
1735 }
1736
Eric Blake3b1f2442020-10-27 00:05:52 -05001737 exp->export_bitmaps[i] = bm;
Eric Blakecbad81c2020-10-27 00:05:49 -05001738 assert(strlen(bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
Eric Blake678ba272019-01-11 13:47:19 -06001739 }
1740
Eric Blake3b1f2442020-10-27 00:05:52 -05001741 /* Mark bitmaps busy in a separate loop, to simplify roll-back concerns. */
1742 for (i = 0; i < exp->nr_export_bitmaps; i++) {
1743 bdrv_dirty_bitmap_set_busy(exp->export_bitmaps[i], true);
1744 }
1745
Eric Blakedbc7b012020-10-27 00:05:55 -05001746 exp->allocation_depth = arg->allocation_depth;
1747
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001748 /*
1749 * We need to inhibit request queuing in the block layer to ensure we can
1750 * be properly quiesced when entering a drained section, as our coroutines
1751 * servicing pending requests might enter blk_pread().
1752 */
1753 blk_set_disable_request_queuing(blk, true);
1754
Max Reitzaadf99a2014-11-18 12:21:18 +01001755 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
Max Reitz741cc432016-01-29 16:36:06 +01001756
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001757 blk_set_dev_ops(blk, &nbd_block_ops, exp);
1758
Eric Blake3fa4c762019-01-11 13:47:16 -06001759 QTAILQ_INSERT_TAIL(&exports, exp, next);
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001760
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001761 return 0;
Max Reitz98f44bb2015-02-25 13:08:21 -05001762
1763fail:
Eric Blake3b1f2442020-10-27 00:05:52 -05001764 g_free(exp->export_bitmaps);
Eric Blake3fa4c762019-01-11 13:47:16 -06001765 g_free(exp->name);
1766 g_free(exp->description);
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001767 return ret;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001768}
1769
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001770NBDExport *nbd_export_find(const char *name)
1771{
1772 NBDExport *exp;
1773 QTAILQ_FOREACH(exp, &exports, next) {
1774 if (strcmp(name, exp->name) == 0) {
1775 return exp;
1776 }
1777 }
1778
1779 return NULL;
1780}
1781
Eric Blake61bc8462019-09-16 21:39:17 -05001782AioContext *
1783nbd_export_aio_context(NBDExport *exp)
1784{
Kevin Wolf8612c682020-09-24 17:27:00 +02001785 return exp->common.ctx;
Eric Blake61bc8462019-09-16 21:39:17 -05001786}
1787
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001788static void nbd_export_request_shutdown(BlockExport *blk_exp)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001789{
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001790 NBDExport *exp = container_of(blk_exp, NBDExport, common);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001791 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001792
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001793 blk_exp_ref(&exp->common);
Eric Blake3fa4c762019-01-11 13:47:16 -06001794 /*
1795 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1796 * close mode that stops advertising the export to new clients but
1797 * still permits existing clients to run to completion? Because of
1798 * that possibility, nbd_export_close() can be called more than
1799 * once on an export.
1800 */
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001801 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
Eric Blake0c9390d2017-06-08 17:26:17 -05001802 client_close(client, true);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +02001803 }
Eric Blake3fa4c762019-01-11 13:47:16 -06001804 if (exp->name) {
Eric Blake3fa4c762019-01-11 13:47:16 -06001805 g_free(exp->name);
1806 exp->name = NULL;
1807 QTAILQ_REMOVE(&exports, exp, next);
1808 }
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001809 blk_exp_unref(&exp->common);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001810}
1811
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001812static void nbd_export_delete(BlockExport *blk_exp)
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001813{
Eric Blake3b1f2442020-10-27 00:05:52 -05001814 size_t i;
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001815 NBDExport *exp = container_of(blk_exp, NBDExport, common);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001816
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001817 assert(exp->name == NULL);
1818 assert(QTAILQ_EMPTY(&exp->clients));
Kevin Wolfdbc9e942020-09-24 17:26:58 +02001819
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001820 g_free(exp->description);
1821 exp->description = NULL;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +02001822
Kevin Wolf37a4f702020-09-24 17:27:08 +02001823 if (exp->common.blk) {
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001824 if (exp->eject_notifier_blk) {
1825 notifier_remove(&exp->eject_notifier);
1826 blk_unref(exp->eject_notifier_blk);
Wen Congyangd6268342015-09-16 16:35:46 +08001827 }
Kevin Wolf37a4f702020-09-24 17:27:08 +02001828 blk_remove_aio_context_notifier(exp->common.blk, blk_aio_attached,
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001829 blk_aio_detach, exp);
Sergio Lopezfd6afc52021-06-02 08:05:52 +02001830 blk_set_disable_request_queuing(exp->common.blk, false);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001831 }
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001832
Eric Blake3b1f2442020-10-27 00:05:52 -05001833 for (i = 0; i < exp->nr_export_bitmaps; i++) {
1834 bdrv_dirty_bitmap_set_busy(exp->export_bitmaps[i], false);
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001835 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001836}
1837
Kevin Wolf56ee8622020-09-24 17:26:50 +02001838const BlockExportDriver blk_exp_nbd = {
1839 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfa6ff7982020-09-24 17:27:02 +02001840 .instance_size = sizeof(NBDExport),
Kevin Wolf56ee8622020-09-24 17:26:50 +02001841 .create = nbd_export_create,
Kevin Wolfc69de1b2020-09-24 17:26:59 +02001842 .delete = nbd_export_delete,
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001843 .request_shutdown = nbd_export_request_shutdown,
Kevin Wolf56ee8622020-09-24 17:26:50 +02001844};
1845
Vladimir Sementsov-Ogievskiyde79bfc2017-10-12 17:29:00 -05001846static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1847 unsigned niov, Error **errp)
1848{
1849 int ret;
1850
1851 g_assert(qemu_in_coroutine());
1852 qemu_co_mutex_lock(&client->send_lock);
1853 client->send_coroutine = qemu_coroutine_self();
1854
1855 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1856
1857 client->send_coroutine = NULL;
1858 qemu_co_mutex_unlock(&client->send_lock);
1859
1860 return ret;
1861}
1862
Vladimir Sementsov-Ogievskiycaad5382017-10-12 12:53:10 +03001863static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1864 uint64_t handle)
1865{
1866 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1867 stl_be_p(&reply->error, error);
1868 stq_be_p(&reply->handle, handle);
1869}
1870
Vladimir Sementsov-Ogievskiy978df1b2017-10-12 12:53:12 +03001871static int nbd_co_send_simple_reply(NBDClient *client,
Vladimir Sementsov-Ogievskiy14cea412017-10-12 17:05:06 -05001872 uint64_t handle,
1873 uint32_t error,
Vladimir Sementsov-Ogievskiy978df1b2017-10-12 12:53:12 +03001874 void *data,
1875 size_t len,
1876 Error **errp)
Paolo Bonzini22045592011-09-19 14:25:30 +02001877{
Vladimir Sementsov-Ogievskiyde79bfc2017-10-12 17:29:00 -05001878 NBDSimpleReply reply;
Vladimir Sementsov-Ogievskiy14cea412017-10-12 17:05:06 -05001879 int nbd_err = system_errno_to_nbd_errno(error);
Vladimir Sementsov-Ogievskiyde79bfc2017-10-12 17:29:00 -05001880 struct iovec iov[] = {
1881 {.iov_base = &reply, .iov_len = sizeof(reply)},
1882 {.iov_base = data, .iov_len = len}
1883 };
Vladimir Sementsov-Ogievskiy6fb2b972017-07-07 18:29:17 +03001884
Eric Blakee7a78d02017-10-27 12:40:26 +02001885 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1886 len);
Vladimir Sementsov-Ogievskiyde79bfc2017-10-12 17:29:00 -05001887 set_be_simple_reply(&reply, nbd_err, handle);
Vladimir Sementsov-Ogievskiy6fb2b972017-07-07 18:29:17 +03001888
Vladimir Sementsov-Ogievskiyde79bfc2017-10-12 17:29:00 -05001889 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
Paolo Bonzini22045592011-09-19 14:25:30 +02001890}
1891
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001892static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1893 uint16_t type, uint64_t handle, uint32_t length)
1894{
1895 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1896 stw_be_p(&chunk->flags, flags);
1897 stw_be_p(&chunk->type, type);
1898 stq_be_p(&chunk->handle, handle);
1899 stl_be_p(&chunk->length, length);
1900}
1901
Eric Blakeef8c8872017-11-08 15:57:03 -06001902static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1903 uint64_t handle,
1904 Error **errp)
1905{
1906 NBDStructuredReplyChunk chunk;
1907 struct iovec iov[] = {
1908 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1909 };
1910
1911 trace_nbd_co_send_structured_done(handle);
1912 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1913
1914 return nbd_co_send_iov(client, iov, 1, errp);
1915}
1916
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001917static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1918 uint64_t handle,
1919 uint64_t offset,
1920 void *data,
1921 size_t size,
Eric Blake418638d2017-11-06 21:09:11 -06001922 bool final,
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001923 Error **errp)
1924{
Eric Blakeefdc0c12017-11-08 15:57:00 -06001925 NBDStructuredReadData chunk;
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001926 struct iovec iov[] = {
1927 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1928 {.iov_base = data, .iov_len = size}
1929 };
1930
Eric Blakeef8c8872017-11-08 15:57:03 -06001931 assert(size);
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001932 trace_nbd_co_send_structured_read(handle, offset, data, size);
Eric Blake418638d2017-11-06 21:09:11 -06001933 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1934 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1935 sizeof(chunk) - sizeof(chunk.h) + size);
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02001936 stq_be_p(&chunk.offset, offset);
1937
1938 return nbd_co_send_iov(client, iov, 2, errp);
1939}
1940
Vladimir Sementsov-Ogievskiy60ace2b2018-03-08 21:46:32 +03001941static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1942 uint64_t handle,
1943 uint32_t error,
1944 const char *msg,
1945 Error **errp)
1946{
1947 NBDStructuredError chunk;
1948 int nbd_err = system_errno_to_nbd_errno(error);
1949 struct iovec iov[] = {
1950 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1951 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1952 };
1953
1954 assert(nbd_err);
1955 trace_nbd_co_send_structured_error(handle, nbd_err,
1956 nbd_err_lookup(nbd_err), msg ? msg : "");
1957 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1958 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1959 stl_be_p(&chunk.error, nbd_err);
1960 stw_be_p(&chunk.message_length, iov[1].iov_len);
1961
1962 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1963}
1964
Vladimir Sementsov-Ogievskiy37e02ae2018-03-08 21:46:33 +03001965/* Do a sparse read and send the structured reply to the client.
1966 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1967 * reported to the client, at which point this function succeeds.
1968 */
Eric Blake418638d2017-11-06 21:09:11 -06001969static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1970 uint64_t handle,
1971 uint64_t offset,
1972 uint8_t *data,
1973 size_t size,
1974 Error **errp)
1975{
1976 int ret = 0;
1977 NBDExport *exp = client->exp;
1978 size_t progress = 0;
1979
1980 while (progress < size) {
1981 int64_t pnum;
Kevin Wolf37a4f702020-09-24 17:27:08 +02001982 int status = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
Eric Blake418638d2017-11-06 21:09:11 -06001983 offset + progress,
1984 size - progress, &pnum, NULL,
1985 NULL);
Eric Blakee2de3252017-11-06 21:09:12 -06001986 bool final;
Eric Blake418638d2017-11-06 21:09:11 -06001987
1988 if (status < 0) {
Vladimir Sementsov-Ogievskiy37e02ae2018-03-08 21:46:33 +03001989 char *msg = g_strdup_printf("unable to check for holes: %s",
1990 strerror(-status));
1991
1992 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1993 errp);
1994 g_free(msg);
1995 return ret;
Eric Blake418638d2017-11-06 21:09:11 -06001996 }
1997 assert(pnum && pnum <= size - progress);
Eric Blakee2de3252017-11-06 21:09:12 -06001998 final = progress + pnum == size;
Eric Blake418638d2017-11-06 21:09:11 -06001999 if (status & BDRV_BLOCK_ZERO) {
2000 NBDStructuredReadHole chunk;
2001 struct iovec iov[] = {
2002 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2003 };
2004
2005 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
2006 pnum);
Eric Blakee2de3252017-11-06 21:09:12 -06002007 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
2008 NBD_REPLY_TYPE_OFFSET_HOLE,
Eric Blake418638d2017-11-06 21:09:11 -06002009 handle, sizeof(chunk) - sizeof(chunk.h));
2010 stq_be_p(&chunk.offset, offset + progress);
2011 stl_be_p(&chunk.length, pnum);
2012 ret = nbd_co_send_iov(client, iov, 1, errp);
2013 } else {
Kevin Wolf37a4f702020-09-24 17:27:08 +02002014 ret = blk_pread(exp->common.blk, offset + progress,
2015 data + progress, pnum);
Eric Blake418638d2017-11-06 21:09:11 -06002016 if (ret < 0) {
2017 error_setg_errno(errp, -ret, "reading from file failed");
2018 break;
2019 }
2020 ret = nbd_co_send_structured_read(client, handle, offset + progress,
Eric Blakee2de3252017-11-06 21:09:12 -06002021 data + progress, pnum, final,
Eric Blake418638d2017-11-06 21:09:11 -06002022 errp);
2023 }
2024
2025 if (ret < 0) {
2026 break;
2027 }
2028 progress += pnum;
2029 }
Eric Blake418638d2017-11-06 21:09:11 -06002030 return ret;
2031}
2032
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002033typedef struct NBDExtentArray {
2034 NBDExtent *extents;
2035 unsigned int nb_alloc;
2036 unsigned int count;
2037 uint64_t total_length;
2038 bool can_add;
2039 bool converted_to_be;
2040} NBDExtentArray;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002041
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002042static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
2043{
2044 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
2045
2046 ea->nb_alloc = nb_alloc;
2047 ea->extents = g_new(NBDExtent, nb_alloc);
2048 ea->can_add = true;
2049
2050 return ea;
2051}
2052
2053static void nbd_extent_array_free(NBDExtentArray *ea)
2054{
2055 g_free(ea->extents);
2056 g_free(ea);
2057}
2058G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
2059
2060/* Further modifications of the array after conversion are abandoned */
2061static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
2062{
2063 int i;
2064
2065 assert(!ea->converted_to_be);
2066 ea->can_add = false;
2067 ea->converted_to_be = true;
2068
2069 for (i = 0; i < ea->count; i++) {
2070 ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
2071 ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
2072 }
2073}
2074
2075/*
2076 * Add extent to NBDExtentArray. If extent can't be added (no available space),
2077 * return -1.
2078 * For safety, when returning -1 for the first time, .can_add is set to false,
2079 * further call to nbd_extent_array_add() will crash.
2080 * (to avoid the situation, when after failing to add an extent (returned -1),
2081 * user miss this failure and add another extent, which is successfully added
2082 * (array is full, but new extent may be squashed into the last one), then we
2083 * have invalid array with skipped extent)
2084 */
2085static int nbd_extent_array_add(NBDExtentArray *ea,
2086 uint32_t length, uint32_t flags)
2087{
2088 assert(ea->can_add);
2089
2090 if (!length) {
2091 return 0;
2092 }
2093
2094 /* Extend previous extent if flags are the same */
2095 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
2096 uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
2097
2098 if (sum <= UINT32_MAX) {
2099 ea->extents[ea->count - 1].length = sum;
2100 ea->total_length += length;
2101 return 0;
2102 }
2103 }
2104
2105 if (ea->count >= ea->nb_alloc) {
2106 ea->can_add = false;
2107 return -1;
2108 }
2109
2110 ea->total_length += length;
2111 ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
2112 ea->count++;
2113
2114 return 0;
2115}
2116
2117static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
2118 uint64_t bytes, NBDExtentArray *ea)
2119{
2120 while (bytes) {
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002121 uint32_t flags;
2122 int64_t num;
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002123 int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
2124 NULL, NULL);
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002125
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002126 if (ret < 0) {
2127 return ret;
2128 }
2129
Nir Soffer0da98562021-02-19 18:07:52 +02002130 flags = (ret & BDRV_BLOCK_DATA ? 0 : NBD_STATE_HOLE) |
2131 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002132
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002133 if (nbd_extent_array_add(ea, num, flags) < 0) {
2134 return 0;
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002135 }
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002136
Eric Blake2178a562019-04-02 22:05:20 -05002137 offset += num;
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002138 bytes -= num;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002139 }
2140
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002141 return 0;
2142}
2143
Eric Blake71719cd2020-10-27 00:05:54 -05002144static int blockalloc_to_extents(BlockDriverState *bs, uint64_t offset,
2145 uint64_t bytes, NBDExtentArray *ea)
2146{
2147 while (bytes) {
2148 int64_t num;
2149 int ret = bdrv_is_allocated_above(bs, NULL, false, offset, bytes,
2150 &num);
2151
2152 if (ret < 0) {
2153 return ret;
2154 }
2155
2156 if (nbd_extent_array_add(ea, num, ret) < 0) {
2157 return 0;
2158 }
2159
2160 offset += num;
2161 bytes -= num;
2162 }
2163
2164 return 0;
2165}
2166
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002167/*
2168 * nbd_co_send_extents
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002169 *
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002170 * @ea is converted to BE by the function
2171 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002172 */
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002173static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002174 NBDExtentArray *ea,
2175 bool last, uint32_t context_id, Error **errp)
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002176{
2177 NBDStructuredMeta chunk;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002178 struct iovec iov[] = {
2179 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002180 {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002181 };
2182
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002183 nbd_extent_array_convert_to_be(ea);
2184
2185 trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2186 last);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002187 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2188 NBD_REPLY_TYPE_BLOCK_STATUS,
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002189 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2190 stl_be_p(&chunk.context_id, context_id);
2191
2192 return nbd_co_send_iov(client, iov, 2, errp);
2193}
2194
2195/* Get block status from the exported device and send it to the client */
2196static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2197 BlockDriverState *bs, uint64_t offset,
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002198 uint32_t length, bool dont_fragment,
2199 bool last, uint32_t context_id,
2200 Error **errp)
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002201{
2202 int ret;
Eric Blake416e34b2019-05-10 10:17:35 -05002203 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002204 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002205
Eric Blake71719cd2020-10-27 00:05:54 -05002206 if (context_id == NBD_META_ID_BASE_ALLOCATION) {
2207 ret = blockstatus_to_extents(bs, offset, length, ea);
2208 } else {
2209 ret = blockalloc_to_extents(bs, offset, length, ea);
2210 }
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002211 if (ret < 0) {
2212 return nbd_co_send_structured_error(
2213 client, handle, -ret, "can't get block status", errp);
2214 }
2215
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002216 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002217}
2218
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002219/* Populate @ea from a dirty bitmap. */
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002220static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2221 uint64_t offset, uint64_t length,
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002222 NBDExtentArray *es)
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002223{
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002224 int64_t start, dirty_start, dirty_count;
2225 int64_t end = offset + length;
2226 bool full = false;
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002227
2228 bdrv_dirty_bitmap_lock(bitmap);
2229
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002230 for (start = offset;
2231 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2232 &dirty_start, &dirty_count);
2233 start = dirty_start + dirty_count)
2234 {
2235 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2236 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2237 {
2238 full = true;
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002239 break;
2240 }
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002241 }
2242
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002243 if (!full) {
Eric Blakec0b21f22020-11-06 14:36:11 -06002244 /* last non dirty extent, nothing to do if array is now full */
2245 (void) nbd_extent_array_add(es, end - start, 0);
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002246 }
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002247
2248 bdrv_dirty_bitmap_unlock(bitmap);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002249}
2250
2251static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2252 BdrvDirtyBitmap *bitmap, uint64_t offset,
2253 uint32_t length, bool dont_fragment, bool last,
2254 uint32_t context_id, Error **errp)
2255{
Eric Blake416e34b2019-05-10 10:17:35 -05002256 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002257 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002258
Vladimir Sementsov-Ogievskiydacbb6e2020-02-05 14:20:40 +03002259 bitmap_to_extents(bitmap, offset, length, ea);
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002260
Vladimir Sementsov-Ogievskiy89cbc7e2020-02-05 14:20:39 +03002261 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002262}
2263
Vladimir Sementsov-Ogievskiy2a6e1282017-06-02 18:01:44 +03002264/* nbd_co_receive_request
2265 * Collect a client request. Return 0 if request looks valid, -EIO to drop
Sergio Lopezf148ae72020-12-14 18:05:18 +01002266 * connection right away, -EAGAIN to indicate we were interrupted and the
2267 * channel should be quiesced, and any other negative value to report an error
2268 * to the client (although the caller may still need to disconnect after
2269 * reporting the error).
Vladimir Sementsov-Ogievskiy2a6e1282017-06-02 18:01:44 +03002270 */
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002271static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2272 Error **errp)
Paolo Bonzinia030b342011-09-19 15:07:54 +02002273{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02002274 NBDClient *client = req->client;
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02002275 int valid_flags;
Sergio Lopezf148ae72020-12-14 18:05:18 +01002276 int ret;
Paolo Bonzinia030b342011-09-19 15:07:54 +02002277
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +00002278 g_assert(qemu_in_coroutine());
Paolo Bonziniff829112017-02-13 14:52:24 +01002279 assert(client->recv_coroutine == qemu_coroutine_self());
Sergio Lopezf148ae72020-12-14 18:05:18 +01002280 ret = nbd_receive_request(client, request, errp);
2281 if (ret < 0) {
2282 return ret;
Paolo Bonzinia030b342011-09-19 15:07:54 +02002283 }
2284
Eric Blake3736cc52017-07-07 15:30:43 -05002285 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2286 nbd_cmd_lookup(request->type));
Eric Blake29b6c3b2016-05-11 16:39:37 -06002287
Eric Blakeb626b512016-10-14 13:33:04 -05002288 if (request->type != NBD_CMD_WRITE) {
Eric Blake29b6c3b2016-05-11 16:39:37 -06002289 /* No payload, we are ready to read the next request. */
2290 req->complete = true;
2291 }
2292
Eric Blakeb626b512016-10-14 13:33:04 -05002293 if (request->type == NBD_CMD_DISC) {
Eric Blake29b6c3b2016-05-11 16:39:37 -06002294 /* Special case: we're going to disconnect without a reply,
2295 * whether or not flags, from, or len are bogus */
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002296 return -EIO;
Eric Blake29b6c3b2016-05-11 16:39:37 -06002297 }
2298
Vladimir Sementsov-Ogievskiybc37b062018-04-13 17:31:56 +03002299 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2300 request->type == NBD_CMD_CACHE)
2301 {
Paolo Bonzinieb38c3b2016-01-07 14:32:42 +01002302 if (request->len > NBD_MAX_BUFFER_SIZE) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002303 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2304 request->len, NBD_MAX_BUFFER_SIZE);
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002305 return -EINVAL;
Paolo Bonzinieb38c3b2016-01-07 14:32:42 +01002306 }
2307
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002308 if (request->type != NBD_CMD_CACHE) {
Kevin Wolf37a4f702020-09-24 17:27:08 +02002309 req->data = blk_try_blockalign(client->exp->common.blk,
2310 request->len);
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002311 if (req->data == NULL) {
2312 error_setg(errp, "No memory");
2313 return -ENOMEM;
2314 }
Paolo Bonzinif1c17522016-01-07 14:34:13 +01002315 }
Stefan Hajnoczi2d821482013-05-02 14:23:08 +02002316 }
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002317
Eric Blakeb626b512016-10-14 13:33:04 -05002318 if (request->type == NBD_CMD_WRITE) {
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03002319 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2320 errp) < 0)
2321 {
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002322 return -EIO;
Paolo Bonzinia030b342011-09-19 15:07:54 +02002323 }
Eric Blake29b6c3b2016-05-11 16:39:37 -06002324 req->complete = true;
Vladimir Sementsov-Ogievskiy6fb2b972017-07-07 18:29:17 +03002325
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03002326 trace_nbd_co_receive_request_payload_received(request->handle,
2327 request->len);
Paolo Bonzinia030b342011-09-19 15:07:54 +02002328 }
Eric Blake29b6c3b2016-05-11 16:39:37 -06002329
Eric Blakefed5f8f2017-11-15 15:35:56 -06002330 /* Sanity checks. */
2331 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2332 (request->type == NBD_CMD_WRITE ||
2333 request->type == NBD_CMD_WRITE_ZEROES ||
2334 request->type == NBD_CMD_TRIM)) {
2335 error_setg(errp, "Export is read-only");
2336 return -EROFS;
2337 }
2338 if (request->from > client->exp->size ||
Eric Blake9d26dfc2019-01-17 13:36:43 -06002339 request->len > client->exp->size - request->from) {
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002340 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2341 ", Size: %" PRIu64, request->from, request->len,
Eric Blake9d26dfc2019-01-17 13:36:43 -06002342 client->exp->size);
Eric Blakefed5f8f2017-11-15 15:35:56 -06002343 return (request->type == NBD_CMD_WRITE ||
2344 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
Eric Blake29b6c3b2016-05-11 16:39:37 -06002345 }
Eric Blake6e280642019-04-02 22:05:21 -05002346 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2347 client->check_align)) {
2348 /*
2349 * The block layer gracefully handles unaligned requests, but
2350 * it's still worth tracing client non-compliance
2351 */
2352 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2353 request->from,
2354 request->len,
2355 client->check_align);
2356 }
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02002357 valid_flags = NBD_CMD_FLAG_FUA;
2358 if (request->type == NBD_CMD_READ && client->structured_reply) {
2359 valid_flags |= NBD_CMD_FLAG_DF;
2360 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
Eric Blakeb491dbb2019-08-23 09:37:25 -05002361 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002362 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2363 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
Eric Blakeab7c5482016-05-11 16:39:38 -06002364 }
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02002365 if (request->flags & ~valid_flags) {
2366 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2367 nbd_cmd_lookup(request->type), request->flags);
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002368 return -EINVAL;
Eric Blake1f4d6d12016-10-14 13:33:17 -05002369 }
Eric Blake29b6c3b2016-05-11 16:39:37 -06002370
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002371 return 0;
Paolo Bonzinia030b342011-09-19 15:07:54 +02002372}
2373
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002374/* Send simple reply without a payload, or a structured error
2375 * @error_msg is ignored if @ret >= 0
2376 * Returns 0 if connection is still live, -errno on failure to talk to client
2377 */
2378static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2379 uint64_t handle,
2380 int ret,
2381 const char *error_msg,
2382 Error **errp)
2383{
2384 if (client->structured_reply && ret < 0) {
2385 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2386 errp);
2387 } else {
2388 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2389 NULL, 0, errp);
2390 }
2391}
2392
2393/* Handle NBD_CMD_READ request.
2394 * Return -errno if sending fails. Other errors are reported directly to the
2395 * client as an error reply. */
2396static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2397 uint8_t *data, Error **errp)
2398{
2399 int ret;
2400 NBDExport *exp = client->exp;
2401
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002402 assert(request->type == NBD_CMD_READ);
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002403
2404 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2405 if (request->flags & NBD_CMD_FLAG_FUA) {
Kevin Wolf37a4f702020-09-24 17:27:08 +02002406 ret = blk_co_flush(exp->common.blk);
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002407 if (ret < 0) {
2408 return nbd_send_generic_reply(client, request->handle, ret,
2409 "flush failed", errp);
2410 }
2411 }
2412
2413 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002414 request->len)
Vladimir Sementsov-Ogievskiy2f454de2018-10-03 17:47:38 +03002415 {
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002416 return nbd_co_send_sparse_read(client, request->handle, request->from,
2417 data, request->len, errp);
2418 }
2419
Kevin Wolf37a4f702020-09-24 17:27:08 +02002420 ret = blk_pread(exp->common.blk, request->from, data, request->len);
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002421 if (ret < 0) {
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002422 return nbd_send_generic_reply(client, request->handle, ret,
2423 "reading from file failed", errp);
2424 }
2425
2426 if (client->structured_reply) {
2427 if (request->len) {
2428 return nbd_co_send_structured_read(client, request->handle,
2429 request->from, data,
2430 request->len, true, errp);
2431 } else {
2432 return nbd_co_send_structured_done(client, request->handle, errp);
2433 }
2434 } else {
2435 return nbd_co_send_simple_reply(client, request->handle, 0,
2436 data, request->len, errp);
2437 }
2438}
2439
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002440/*
2441 * nbd_do_cmd_cache
2442 *
2443 * Handle NBD_CMD_CACHE request.
2444 * Return -errno if sending fails. Other errors are reported directly to the
2445 * client as an error reply.
2446 */
2447static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2448 Error **errp)
2449{
2450 int ret;
2451 NBDExport *exp = client->exp;
2452
2453 assert(request->type == NBD_CMD_CACHE);
2454
Kevin Wolf37a4f702020-09-24 17:27:08 +02002455 ret = blk_co_preadv(exp->common.blk, request->from, request->len,
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002456 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2457
2458 return nbd_send_generic_reply(client, request->handle, ret,
2459 "caching data failed", errp);
2460}
2461
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002462/* Handle NBD request.
2463 * Return -errno if sending fails. Other errors are reported directly to the
2464 * client as an error reply. */
2465static coroutine_fn int nbd_handle_request(NBDClient *client,
2466 NBDRequest *request,
2467 uint8_t *data, Error **errp)
2468{
2469 int ret;
2470 int flags;
2471 NBDExport *exp = client->exp;
2472 char *msg;
Eric Blake3b1f2442020-10-27 00:05:52 -05002473 size_t i;
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002474
2475 switch (request->type) {
Vladimir Sementsov-Ogievskiybc37b062018-04-13 17:31:56 +03002476 case NBD_CMD_CACHE:
Vladimir Sementsov-Ogievskiy7fa5c562019-07-25 13:05:50 +03002477 return nbd_do_cmd_cache(client, request, errp);
2478
2479 case NBD_CMD_READ:
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002480 return nbd_do_cmd_read(client, request, data, errp);
2481
2482 case NBD_CMD_WRITE:
2483 flags = 0;
2484 if (request->flags & NBD_CMD_FLAG_FUA) {
2485 flags |= BDRV_REQ_FUA;
2486 }
Kevin Wolf37a4f702020-09-24 17:27:08 +02002487 ret = blk_pwrite(exp->common.blk, request->from, data, request->len,
2488 flags);
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002489 return nbd_send_generic_reply(client, request->handle, ret,
2490 "writing to file failed", errp);
2491
2492 case NBD_CMD_WRITE_ZEROES:
2493 flags = 0;
2494 if (request->flags & NBD_CMD_FLAG_FUA) {
2495 flags |= BDRV_REQ_FUA;
2496 }
2497 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2498 flags |= BDRV_REQ_MAY_UNMAP;
2499 }
Eric Blakeb491dbb2019-08-23 09:37:25 -05002500 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2501 flags |= BDRV_REQ_NO_FALLBACK;
2502 }
Eric Blake890cbcc2020-07-22 16:22:31 -05002503 ret = 0;
2504 /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2505 while (ret >= 0 && request->len) {
2506 int align = client->check_align ?: 1;
2507 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2508 align));
Kevin Wolf37a4f702020-09-24 17:27:08 +02002509 ret = blk_pwrite_zeroes(exp->common.blk, request->from, len, flags);
Eric Blake890cbcc2020-07-22 16:22:31 -05002510 request->len -= len;
2511 request->from += len;
2512 }
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002513 return nbd_send_generic_reply(client, request->handle, ret,
2514 "writing to file failed", errp);
2515
2516 case NBD_CMD_DISC:
2517 /* unreachable, thanks to special case in nbd_co_receive_request() */
2518 abort();
2519
2520 case NBD_CMD_FLUSH:
Kevin Wolf37a4f702020-09-24 17:27:08 +02002521 ret = blk_co_flush(exp->common.blk);
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002522 return nbd_send_generic_reply(client, request->handle, ret,
2523 "flush failed", errp);
2524
2525 case NBD_CMD_TRIM:
Eric Blake890cbcc2020-07-22 16:22:31 -05002526 ret = 0;
2527 /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2528 while (ret >= 0 && request->len) {
2529 int align = client->check_align ?: 1;
2530 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2531 align));
Kevin Wolf37a4f702020-09-24 17:27:08 +02002532 ret = blk_co_pdiscard(exp->common.blk, request->from, len);
Eric Blake890cbcc2020-07-22 16:22:31 -05002533 request->len -= len;
2534 request->from += len;
2535 }
2536 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
Kevin Wolf37a4f702020-09-24 17:27:08 +02002537 ret = blk_co_flush(exp->common.blk);
Eric Blake65529782018-03-07 16:57:32 -06002538 }
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002539 return nbd_send_generic_reply(client, request->handle, ret,
2540 "discard failed", errp);
2541
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002542 case NBD_CMD_BLOCK_STATUS:
Eric Blaked8b20292018-06-21 07:49:37 -05002543 if (!request->len) {
2544 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2545 "need non-zero length", errp);
2546 }
Eric Blake47ec4852020-10-27 00:05:51 -05002547 if (client->export_meta.count) {
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002548 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
Eric Blake47ec4852020-10-27 00:05:51 -05002549 int contexts_remaining = client->export_meta.count;
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002550
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002551 if (client->export_meta.base_allocation) {
2552 ret = nbd_co_send_block_status(client, request->handle,
Kevin Wolf37a4f702020-09-24 17:27:08 +02002553 blk_bs(exp->common.blk),
2554 request->from,
Vladimir Sementsov-Ogievskiyfb7afc72018-07-04 14:23:02 +03002555 request->len, dont_fragment,
Eric Blake47ec4852020-10-27 00:05:51 -05002556 !--contexts_remaining,
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002557 NBD_META_ID_BASE_ALLOCATION,
2558 errp);
Eric Blake73e064c2020-02-06 11:38:32 -06002559 if (ret < 0) {
2560 return ret;
2561 }
2562 }
2563
Eric Blake71719cd2020-10-27 00:05:54 -05002564 if (client->export_meta.allocation_depth) {
2565 ret = nbd_co_send_block_status(client, request->handle,
2566 blk_bs(exp->common.blk),
2567 request->from, request->len,
2568 dont_fragment,
2569 !--contexts_remaining,
2570 NBD_META_ID_ALLOCATION_DEPTH,
2571 errp);
2572 if (ret < 0) {
2573 return ret;
2574 }
2575 }
2576
Eric Blake3b1f2442020-10-27 00:05:52 -05002577 for (i = 0; i < client->exp->nr_export_bitmaps; i++) {
2578 if (!client->export_meta.bitmaps[i]) {
2579 continue;
2580 }
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002581 ret = nbd_co_send_bitmap(client, request->handle,
Eric Blake3b1f2442020-10-27 00:05:52 -05002582 client->exp->export_bitmaps[i],
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002583 request->from, request->len,
Eric Blake47ec4852020-10-27 00:05:51 -05002584 dont_fragment, !--contexts_remaining,
Eric Blake3b1f2442020-10-27 00:05:52 -05002585 NBD_META_ID_DIRTY_BITMAP + i, errp);
Eric Blake73e064c2020-02-06 11:38:32 -06002586 if (ret < 0) {
2587 return ret;
2588 }
Vladimir Sementsov-Ogievskiy3d068af2018-06-09 18:17:56 +03002589 }
2590
Eric Blake47ec4852020-10-27 00:05:51 -05002591 assert(!contexts_remaining);
2592
Eric Blake73e064c2020-02-06 11:38:32 -06002593 return 0;
Vladimir Sementsov-Ogievskiye7b19482018-03-12 18:21:21 +03002594 } else {
2595 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2596 "CMD_BLOCK_STATUS not negotiated",
2597 errp);
2598 }
2599
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002600 default:
2601 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2602 request->type);
2603 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2604 errp);
2605 g_free(msg);
2606 return ret;
2607 }
2608}
2609
Paolo Bonziniff829112017-02-13 14:52:24 +01002610/* Owns a reference to the NBDClient passed as opaque. */
2611static coroutine_fn void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00002612{
Paolo Bonzini262db382011-09-19 15:19:27 +02002613 NBDClient *client = opaque;
Eric Blake315f78a2016-10-14 13:33:05 -05002614 NBDRequestData *req;
Paolo Bonziniff829112017-02-13 14:52:24 +01002615 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
Vladimir Sementsov-Ogievskiya0dc63a2017-06-02 18:01:42 +03002616 int ret;
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002617 Error *local_err = NULL;
ths75818252008-07-03 13:41:03 +00002618
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03002619 trace_nbd_trip();
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02002620 if (client->closing) {
Paolo Bonziniff829112017-02-13 14:52:24 +01002621 nbd_client_put(client);
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02002622 return;
2623 }
ths75818252008-07-03 13:41:03 +00002624
Sergio Lopezf148ae72020-12-14 18:05:18 +01002625 if (client->quiescing) {
2626 /*
2627 * We're switching between AIO contexts. Don't attempt to receive a new
2628 * request and kick the main context which may be waiting for us.
2629 */
2630 nbd_client_put(client);
2631 client->recv_coroutine = NULL;
2632 aio_wait_kick();
2633 return;
2634 }
2635
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02002636 req = nbd_request_get(client);
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002637 ret = nbd_co_receive_request(req, &request, &local_err);
Vladimir Sementsov-Ogievskiyee898b82017-06-02 18:01:45 +03002638 client->recv_coroutine = NULL;
bellard7a5ca862008-05-27 21:13:40 +00002639
Wen Congyangd6268342015-09-16 16:35:46 +08002640 if (client->closing) {
2641 /*
2642 * The client may be closed when we are blocked in
2643 * nbd_co_receive_request()
2644 */
2645 goto done;
2646 }
2647
Sergio Lopezf148ae72020-12-14 18:05:18 +01002648 if (ret == -EAGAIN) {
2649 assert(client->quiescing);
2650 goto done;
2651 }
2652
Vladimir Sementsov-Ogievskiya0d7ce22018-03-08 21:46:34 +03002653 nbd_client_receive_next_request(client);
2654 if (ret == -EIO) {
2655 goto disconnect;
2656 }
2657
2658 if (ret < 0) {
Vladimir Sementsov-Ogievskiy6a417592018-03-08 21:46:35 +03002659 /* It wans't -EIO, so, according to nbd_co_receive_request()
2660 * semantics, we should return the error to the client. */
2661 Error *export_err = local_err;
2662
2663 local_err = NULL;
2664 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2665 error_get_pretty(export_err), &local_err);
2666 error_free(export_err);
Vladimir Sementsov-Ogievskiy6f302e62018-03-12 17:14:28 -05002667 } else {
2668 ret = nbd_handle_request(client, &request, req->data, &local_err);
Vladimir Sementsov-Ogievskiya0d7ce22018-03-08 21:46:34 +03002669 }
Vladimir Sementsov-Ogievskiy5c54e7f2017-10-27 12:40:32 +02002670 if (ret < 0) {
Vladimir Sementsov-Ogievskiyc7b97282017-07-07 18:29:12 +03002671 error_prepend(&local_err, "Failed to send reply: ");
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002672 goto disconnect;
2673 }
2674
Vladimir Sementsov-Ogievskiy8c372a02017-06-02 18:01:50 +03002675 /* We must disconnect after NBD_CMD_WRITE if we did not
2676 * read the payload.
2677 */
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002678 if (!req->complete) {
2679 error_setg(&local_err, "Request handling failed in intermediate state");
Vladimir Sementsov-Ogievskiy8c372a02017-06-02 18:01:50 +03002680 goto disconnect;
Nick Thomasb2e3d872011-02-22 15:44:51 +00002681 }
bellard7a5ca862008-05-27 21:13:40 +00002682
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01002683done:
Paolo Bonzini262db382011-09-19 15:19:27 +02002684 nbd_request_put(req);
Paolo Bonziniff829112017-02-13 14:52:24 +01002685 nbd_client_put(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02002686 return;
2687
Vladimir Sementsov-Ogievskiy8c372a02017-06-02 18:01:50 +03002688disconnect:
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002689 if (local_err) {
2690 error_reportf_err(local_err, "Disconnect client, due to: ");
2691 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +02002692 nbd_request_put(req);
Eric Blake0c9390d2017-06-08 17:26:17 -05002693 client_close(client, true);
Paolo Bonziniff829112017-02-13 14:52:24 +01002694 nbd_client_put(client);
bellard7a5ca862008-05-27 21:13:40 +00002695}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02002696
Paolo Bonziniff829112017-02-13 14:52:24 +01002697static void nbd_client_receive_next_request(NBDClient *client)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02002698{
Sergio Lopezf148ae72020-12-14 18:05:18 +01002699 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS &&
2700 !client->quiescing) {
Paolo Bonziniff829112017-02-13 14:52:24 +01002701 nbd_client_get(client);
2702 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
Kevin Wolf8612c682020-09-24 17:27:00 +02002703 aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);
Max Reitz958c7172014-06-20 21:57:32 +02002704 }
2705}
2706
Fam Zheng1a6245a2016-01-14 16:41:03 +08002707static coroutine_fn void nbd_co_client_start(void *opaque)
Paolo Bonzini1743b512011-09-19 14:33:23 +02002708{
Vladimir Sementsov-Ogievskiyc84087f2017-06-02 18:01:46 +03002709 NBDClient *client = opaque;
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002710 Error *local_err = NULL;
Fam Zheng1a6245a2016-01-14 16:41:03 +08002711
Paolo Bonzini262db382011-09-19 15:19:27 +02002712 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02002713
Vladimir Sementsov-Ogievskiy2fd2c842017-07-07 18:29:11 +03002714 if (nbd_negotiate(client, &local_err)) {
2715 if (local_err) {
2716 error_report_err(local_err);
2717 }
Eric Blake0c9390d2017-06-08 17:26:17 -05002718 client_close(client, false);
Vladimir Sementsov-Ogievskiyc84087f2017-06-02 18:01:46 +03002719 return;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02002720 }
Paolo Bonziniff829112017-02-13 14:52:24 +01002721
2722 nbd_client_receive_next_request(client);
Fam Zheng1a6245a2016-01-14 16:41:03 +08002723}
2724
Eric Blake0c9390d2017-06-08 17:26:17 -05002725/*
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03002726 * Create a new client listener using the given channel @sioc.
2727 * Begin servicing it in a coroutine. When the connection closes, call
2728 * @close_fn with an indication of whether the client completed negotiation.
Eric Blake0c9390d2017-06-08 17:26:17 -05002729 */
Vladimir Sementsov-Ogievskiy7f7dfe22018-10-03 20:02:28 +03002730void nbd_client_new(QIOChannelSocket *sioc,
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00002731 QCryptoTLSCreds *tlscreds,
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +00002732 const char *tlsauthz,
Eric Blake0c9390d2017-06-08 17:26:17 -05002733 void (*close_fn)(NBDClient *, bool))
Fam Zheng1a6245a2016-01-14 16:41:03 +08002734{
2735 NBDClient *client;
Vladimir Sementsov-Ogievskiyc84087f2017-06-02 18:01:46 +03002736 Coroutine *co;
Fam Zheng1a6245a2016-01-14 16:41:03 +08002737
Marc-André Lureaue8d3eb72017-10-06 20:49:16 -03002738 client = g_new0(NBDClient, 1);
Fam Zheng1a6245a2016-01-14 16:41:03 +08002739 client->refcount = 1;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +00002740 client->tlscreds = tlscreds;
2741 if (tlscreds) {
2742 object_ref(OBJECT(client->tlscreds));
2743 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +00002744 client->tlsauthz = g_strdup(tlsauthz);
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +00002745 client->sioc = sioc;
2746 object_ref(OBJECT(client->sioc));
2747 client->ioc = QIO_CHANNEL(sioc);
2748 object_ref(OBJECT(client->ioc));
Eric Blake0c9390d2017-06-08 17:26:17 -05002749 client->close_fn = close_fn;
Fam Zheng1a6245a2016-01-14 16:41:03 +08002750
Vladimir Sementsov-Ogievskiyc84087f2017-06-02 18:01:46 +03002751 co = qemu_coroutine_create(nbd_co_client_start, client);
2752 qemu_coroutine_enter(co);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02002753}