blob: e0d032c2523a09ff40cdd3260772e3011b05090c [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ths75818252008-07-03 13:41:03 +000017 */
bellard7a5ca862008-05-27 21:13:40 +000018
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/nbd.h"
20#include "block/block.h"
bellard7a5ca862008-05-27 21:13:40 +000021
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/coroutine.h"
Paolo Bonzini262db382011-09-19 15:19:27 +020023
bellard7a5ca862008-05-27 21:13:40 +000024#include <errno.h>
25#include <string.h>
aliguori03ff3ca2008-09-15 15:51:35 +000026#ifndef _WIN32
bellard7a5ca862008-05-27 21:13:40 +000027#include <sys/ioctl.h>
aliguori03ff3ca2008-09-15 15:51:35 +000028#endif
Andreas Färber5dc2eec2010-09-20 00:50:46 +020029#if defined(__sun__) || defined(__HAIKU__)
aliguori7e00eb92008-08-02 01:57:02 +000030#include <sys/ioccom.h>
31#endif
bellard7a5ca862008-05-27 21:13:40 +000032#include <ctype.h>
33#include <inttypes.h>
bellard7a5ca862008-05-27 21:13:40 +000034
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +020035#ifdef __linux__
36#include <linux/fs.h>
37#endif
38
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/sockets.h"
40#include "qemu/queue.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010041#include "qemu/main-loop.h"
ths75818252008-07-03 13:41:03 +000042
aliguori03ff3ca2008-09-15 15:51:35 +000043//#define DEBUG_NBD
44
45#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000046#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000047 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000048} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000049#else
50#define TRACE(msg, ...) \
51 do { } while (0)
52#endif
bellard7a5ca862008-05-27 21:13:40 +000053
54#define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
57} while(0)
58
bellard7a5ca862008-05-27 21:13:40 +000059/* This is all part of the "official" NBD API */
60
Paolo Bonzinifa26c262012-08-22 15:13:30 +020061#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
Nick Thomasb2e3d872011-02-22 15:44:51 +000062#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000063#define NBD_REQUEST_MAGIC 0x25609513
64#define NBD_REPLY_MAGIC 0x67446698
Paolo Bonzinifa26c262012-08-22 15:13:30 +020065#define NBD_OPTS_MAGIC 0x49484156454F5054LL
66#define NBD_CLIENT_MAGIC 0x0000420281861253LL
bellard7a5ca862008-05-27 21:13:40 +000067
68#define NBD_SET_SOCK _IO(0xab, 0)
69#define NBD_SET_BLKSIZE _IO(0xab, 1)
70#define NBD_SET_SIZE _IO(0xab, 2)
71#define NBD_DO_IT _IO(0xab, 3)
72#define NBD_CLEAR_SOCK _IO(0xab, 4)
73#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000074#define NBD_PRINT_DEBUG _IO(0xab, 6)
75#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000076#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020077#define NBD_SET_TIMEOUT _IO(0xab, 9)
78#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000079
Nick Thomasb2e3d872011-02-22 15:44:51 +000080#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020081
Paolo Bonzini9a304d22012-08-22 15:30:31 +020082/* Definitions for opaque data types */
83
84typedef struct NBDRequest NBDRequest;
85
86struct NBDRequest {
87 QSIMPLEQ_ENTRY(NBDRequest) entry;
88 NBDClient *client;
89 uint8_t *data;
90};
91
92struct NBDExport {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +020093 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +020094 void (*close)(NBDExport *exp);
95
Paolo Bonzini9a304d22012-08-22 15:30:31 +020096 BlockDriverState *bs;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020097 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020098 off_t dev_offset;
99 off_t size;
100 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200101 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200102 QTAILQ_ENTRY(NBDExport) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200103};
104
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200105static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
106
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200107struct NBDClient {
108 int refcount;
109 void (*close)(NBDClient *client);
110
111 NBDExport *exp;
112 int sock;
113
114 Coroutine *recv_coroutine;
115
116 CoMutex send_lock;
117 Coroutine *send_coroutine;
118
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200119 QTAILQ_ENTRY(NBDClient) next;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200120 int nb_requests;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200121 bool closing;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200122};
123
bellard7a5ca862008-05-27 21:13:40 +0000124/* That's all folks */
125
Paolo Bonzini185b4332012-03-05 08:56:10 +0100126ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +0000127{
128 size_t offset = 0;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100129 int err;
bellard7a5ca862008-05-27 21:13:40 +0000130
Paolo Bonziniae255e52011-09-08 14:28:59 +0200131 if (qemu_in_coroutine()) {
132 if (do_read) {
133 return qemu_co_recv(fd, buffer, size);
134 } else {
135 return qemu_co_send(fd, buffer, size);
136 }
137 }
138
bellard7a5ca862008-05-27 21:13:40 +0000139 while (offset < size) {
140 ssize_t len;
141
142 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000143 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000144 } else {
aliguori03ff3ca2008-09-15 15:51:35 +0000145 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +0000146 }
147
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100148 if (len < 0) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100149 err = socket_error();
aliguori03ff3ca2008-09-15 15:51:35 +0000150
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100151 /* recoverable error */
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100152 if (err == EINTR || (offset > 0 && err == EAGAIN)) {
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100153 continue;
154 }
155
156 /* unrecoverable error */
Paolo Bonzini185b4332012-03-05 08:56:10 +0100157 return -err;
bellard7a5ca862008-05-27 21:13:40 +0000158 }
159
160 /* eof */
161 if (len == 0) {
162 break;
163 }
164
bellard7a5ca862008-05-27 21:13:40 +0000165 offset += len;
166 }
167
168 return offset;
169}
170
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100171static ssize_t read_sync(int fd, void *buffer, size_t size)
172{
173 /* Sockets are kept in blocking mode in the negotiation phase. After
174 * that, a non-readable socket simply means that another thread stole
175 * our request/reply. Synchronization is done with recv_coroutine, so
176 * that this is coroutine-safe.
177 */
178 return nbd_wr_sync(fd, buffer, size, true);
179}
180
181static ssize_t write_sync(int fd, void *buffer, size_t size)
182{
183 int ret;
184 do {
185 /* For writes, we do expect the socket to be writable. */
186 ret = nbd_wr_sync(fd, buffer, size, false);
187 } while (ret == -EAGAIN);
188 return ret;
189}
190
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200191/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000192
193 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000194 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200195
196 or
197
198 Server Client
199 Negotiate #1
200 Option
201 Negotiate #2
202
203 ----
204
205 followed by
206
207 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000208 Request
209 Response
210 Request
211 Response
212 ...
213 ...
214 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200215
bellard7a5ca862008-05-27 21:13:40 +0000216*/
217
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200218static int nbd_receive_options(NBDClient *client)
219{
220 int csock = client->sock;
221 char name[256];
222 uint32_t tmp, length;
223 uint64_t magic;
224 int rc;
225
226 /* Client sends:
227 [ 0 .. 3] reserved (0)
228 [ 4 .. 11] NBD_OPTS_MAGIC
229 [12 .. 15] NBD_OPT_EXPORT_NAME
230 [16 .. 19] length
231 [20 .. xx] export name (length bytes)
232 */
233
234 rc = -EINVAL;
235 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
236 LOG("read failed");
237 goto fail;
238 }
239 TRACE("Checking reserved");
240 if (tmp != 0) {
241 LOG("Bad reserved received");
242 goto fail;
243 }
244
245 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
246 LOG("read failed");
247 goto fail;
248 }
249 TRACE("Checking reserved");
250 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
251 LOG("Bad magic received");
252 goto fail;
253 }
254
255 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
256 LOG("read failed");
257 goto fail;
258 }
259 TRACE("Checking option");
260 if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
261 LOG("Bad option received");
262 goto fail;
263 }
264
265 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
266 LOG("read failed");
267 goto fail;
268 }
269 TRACE("Checking length");
270 length = be32_to_cpu(length);
271 if (length > 255) {
272 LOG("Bad length received");
273 goto fail;
274 }
275 if (read_sync(csock, name, length) != length) {
276 LOG("read failed");
277 goto fail;
278 }
279 name[length] = '\0';
280
281 client->exp = nbd_export_find(name);
282 if (!client->exp) {
283 LOG("export not found");
284 goto fail;
285 }
286
287 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
288 nbd_export_get(client->exp);
289
290 TRACE("Option negotiation succeeded.");
291 rc = 0;
292fail:
293 return rc;
294}
295
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200296static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000297{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200298 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000299 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100300 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200301 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
302 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000303
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200304 /* Negotiation header without options:
305 [ 0 .. 7] passwd ("NBDMAGIC")
306 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000307 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200308 [24 .. 25] server flags (0)
Hani Benhabiles5672ee52014-05-13 00:35:16 +0100309 [26 .. 27] export flags
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200310 [28 .. 151] reserved (0)
311
312 Negotiation header with options, part 1:
313 [ 0 .. 7] passwd ("NBDMAGIC")
314 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
315 [16 .. 17] server flags (0)
316
317 part 2 (after options are sent):
318 [18 .. 25] size
319 [26 .. 27] export flags
320 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000321 */
bellard7a5ca862008-05-27 21:13:40 +0000322
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100323 qemu_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100324 rc = -EINVAL;
325
Nick Thomasb2e3d872011-02-22 15:44:51 +0000326 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100327 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000328 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200329 if (client->exp) {
330 assert ((client->exp->nbdflags & ~65535) == 0);
331 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
332 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
333 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
334 } else {
335 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
336 }
bellard7a5ca862008-05-27 21:13:40 +0000337
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200338 if (client->exp) {
339 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
340 LOG("write failed");
341 goto fail;
342 }
343 } else {
344 if (write_sync(csock, buf, 18) != 18) {
345 LOG("write failed");
346 goto fail;
347 }
348 rc = nbd_receive_options(client);
349 if (rc < 0) {
350 LOG("option negotiation failed");
351 goto fail;
352 }
353
354 assert ((client->exp->nbdflags & ~65535) == 0);
355 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
356 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
357 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
358 LOG("write failed");
359 goto fail;
360 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000361 }
bellard7a5ca862008-05-27 21:13:40 +0000362
Dong Xu Wang07f35072011-11-22 18:06:26 +0800363 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100364 rc = 0;
365fail:
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100366 qemu_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100367 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000368}
369
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200370int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
371 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000372{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000373 char buf[256];
374 uint64_t magic, s;
375 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100376 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000377
Dong Xu Wang07f35072011-11-22 18:06:26 +0800378 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000379
Paolo Bonzini185b4332012-03-05 08:56:10 +0100380 rc = -EINVAL;
381
Nick Thomasb2e3d872011-02-22 15:44:51 +0000382 if (read_sync(csock, buf, 8) != 8) {
383 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100384 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000385 }
bellard7a5ca862008-05-27 21:13:40 +0000386
Nick Thomasb2e3d872011-02-22 15:44:51 +0000387 buf[8] = '\0';
388 if (strlen(buf) == 0) {
389 LOG("server connection closed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100390 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000391 }
bellard7a5ca862008-05-27 21:13:40 +0000392
Nick Thomasb2e3d872011-02-22 15:44:51 +0000393 TRACE("Magic is %c%c%c%c%c%c%c%c",
394 qemu_isprint(buf[0]) ? buf[0] : '.',
395 qemu_isprint(buf[1]) ? buf[1] : '.',
396 qemu_isprint(buf[2]) ? buf[2] : '.',
397 qemu_isprint(buf[3]) ? buf[3] : '.',
398 qemu_isprint(buf[4]) ? buf[4] : '.',
399 qemu_isprint(buf[5]) ? buf[5] : '.',
400 qemu_isprint(buf[6]) ? buf[6] : '.',
401 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000402
Nick Thomasb2e3d872011-02-22 15:44:51 +0000403 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
404 LOG("Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100405 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000406 }
bellard7a5ca862008-05-27 21:13:40 +0000407
Nick Thomasb2e3d872011-02-22 15:44:51 +0000408 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
409 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100410 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000411 }
412 magic = be64_to_cpu(magic);
413 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000414
Nick Thomasb2e3d872011-02-22 15:44:51 +0000415 if (name) {
416 uint32_t reserved = 0;
417 uint32_t opt;
418 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200419
Nick Thomasb2e3d872011-02-22 15:44:51 +0000420 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200421 if (magic != NBD_OPTS_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000422 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100423 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000424 }
425 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
426 LOG("flags read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100427 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000428 }
429 *flags = be16_to_cpu(tmp) << 16;
430 /* reserved for future use */
431 if (write_sync(csock, &reserved, sizeof(reserved)) !=
432 sizeof(reserved)) {
433 LOG("write failed (reserved)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100434 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000435 }
436 /* write the export name */
437 magic = cpu_to_be64(magic);
438 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
439 LOG("write failed (magic)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100440 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000441 }
442 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
443 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
444 LOG("write failed (opt)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100445 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000446 }
447 namesize = cpu_to_be32(strlen(name));
448 if (write_sync(csock, &namesize, sizeof(namesize)) !=
449 sizeof(namesize)) {
450 LOG("write failed (namesize)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100451 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000452 }
453 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
454 LOG("write failed (name)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100455 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000456 }
457 } else {
458 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200459
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200460 if (magic != NBD_CLIENT_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000461 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100462 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000463 }
464 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200465
Nick Thomasb2e3d872011-02-22 15:44:51 +0000466 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
467 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100468 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000469 }
470 *size = be64_to_cpu(s);
471 *blocksize = 1024;
472 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200473
Nick Thomasb2e3d872011-02-22 15:44:51 +0000474 if (!name) {
475 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
476 LOG("read failed (flags)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100477 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000478 }
479 *flags = be32_to_cpup(flags);
480 } else {
481 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
482 LOG("read failed (tmp)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100483 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000484 }
485 *flags |= be32_to_cpu(tmp);
486 }
487 if (read_sync(csock, &buf, 124) != 124) {
488 LOG("read failed (buf)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100489 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000490 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100491 rc = 0;
492
493fail:
494 return rc;
thscd831bd2008-07-03 10:23:51 +0000495}
bellard7a5ca862008-05-27 21:13:40 +0000496
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200497#ifdef __linux__
498int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000499{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800500 TRACE("Setting NBD socket");
501
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100502 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800503 int serrno = errno;
504 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100505 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800506 }
507
Nick Thomasb2e3d872011-02-22 15:44:51 +0000508 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000509
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100510 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000511 int serrno = errno;
512 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100513 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000514 }
bellard7a5ca862008-05-27 21:13:40 +0000515
Blue Swirl0bfcd592010-05-22 08:02:12 +0000516 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000517
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100518 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000519 int serrno = errno;
520 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100521 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000522 }
bellard7a5ca862008-05-27 21:13:40 +0000523
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100524 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
525 if (errno == ENOTTY) {
526 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
527 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200528
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100529 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
530 int serrno = errno;
531 LOG("Failed setting read-only attribute");
532 return -serrno;
533 }
534 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200535 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100536 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100537 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200538 }
539 }
540
Nick Thomasb2e3d872011-02-22 15:44:51 +0000541 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000542
Nick Thomasb2e3d872011-02-22 15:44:51 +0000543 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000544}
545
546int nbd_disconnect(int fd)
547{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000548 ioctl(fd, NBD_CLEAR_QUE);
549 ioctl(fd, NBD_DISCONNECT);
550 ioctl(fd, NBD_CLEAR_SOCK);
551 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000552}
553
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200554int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000555{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000556 int ret;
557 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000558
Nick Thomasb2e3d872011-02-22 15:44:51 +0000559 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000560
Nick Thomasb2e3d872011-02-22 15:44:51 +0000561 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100562 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100563 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
564 * the socket via NBD_DISCONNECT. We do not want to return 1 in
565 * that case.
566 */
567 ret = 0;
568 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000569 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000570
Nick Thomasb2e3d872011-02-22 15:44:51 +0000571 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000572
Nick Thomasb2e3d872011-02-22 15:44:51 +0000573 TRACE("Clearing NBD queue");
574 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000575
Nick Thomasb2e3d872011-02-22 15:44:51 +0000576 TRACE("Clearing NBD socket");
577 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000578
Nick Thomasb2e3d872011-02-22 15:44:51 +0000579 errno = serrno;
580 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000581}
aliguori03ff3ca2008-09-15 15:51:35 +0000582#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200583int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000584{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100585 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000586}
587
588int nbd_disconnect(int fd)
589{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100590 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000591}
592
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200593int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000594{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100595 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000596}
597#endif
bellard7a5ca862008-05-27 21:13:40 +0000598
Paolo Bonzini94e73402012-03-07 11:25:01 +0100599ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000600{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200601 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100602 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000603
Nick Thomasb2e3d872011-02-22 15:44:51 +0000604 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
605 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
606 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
607 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
608 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000609
Nick Thomasb2e3d872011-02-22 15:44:51 +0000610 TRACE("Sending request to client: "
611 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
612 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000613
Paolo Bonzini185b4332012-03-05 08:56:10 +0100614 ret = write_sync(csock, buf, sizeof(buf));
615 if (ret < 0) {
616 return ret;
617 }
618
619 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000620 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100621 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000622 }
623 return 0;
ths75818252008-07-03 13:41:03 +0000624}
625
Paolo Bonzini94e73402012-03-07 11:25:01 +0100626static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000627{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200628 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000629 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100630 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000631
Paolo Bonzini185b4332012-03-05 08:56:10 +0100632 ret = read_sync(csock, buf, sizeof(buf));
633 if (ret < 0) {
634 return ret;
635 }
636
637 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000638 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100639 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000640 }
bellard7a5ca862008-05-27 21:13:40 +0000641
Nick Thomasb2e3d872011-02-22 15:44:51 +0000642 /* Request
643 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
644 [ 4 .. 7] type (0 == READ, 1 == WRITE)
645 [ 8 .. 15] handle
646 [16 .. 23] from
647 [24 .. 27] len
648 */
bellard7a5ca862008-05-27 21:13:40 +0000649
Nick Thomasb2e3d872011-02-22 15:44:51 +0000650 magic = be32_to_cpup((uint32_t*)buf);
651 request->type = be32_to_cpup((uint32_t*)(buf + 4));
652 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
653 request->from = be64_to_cpup((uint64_t*)(buf + 16));
654 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000655
Nick Thomasb2e3d872011-02-22 15:44:51 +0000656 TRACE("Got request: "
657 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
658 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000659
Nick Thomasb2e3d872011-02-22 15:44:51 +0000660 if (magic != NBD_REQUEST_MAGIC) {
661 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100662 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000663 }
664 return 0;
ths75818252008-07-03 13:41:03 +0000665}
bellard7a5ca862008-05-27 21:13:40 +0000666
Paolo Bonzini94e73402012-03-07 11:25:01 +0100667ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000668{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000669 uint8_t buf[NBD_REPLY_SIZE];
670 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100671 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000672
Paolo Bonzini185b4332012-03-05 08:56:10 +0100673 ret = read_sync(csock, buf, sizeof(buf));
674 if (ret < 0) {
675 return ret;
676 }
677
678 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000679 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100680 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000681 }
bellard7a5ca862008-05-27 21:13:40 +0000682
Nick Thomasb2e3d872011-02-22 15:44:51 +0000683 /* Reply
684 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
685 [ 4 .. 7] error (0 == no error)
686 [ 7 .. 15] handle
687 */
ths75818252008-07-03 13:41:03 +0000688
Nick Thomasb2e3d872011-02-22 15:44:51 +0000689 magic = be32_to_cpup((uint32_t*)buf);
690 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
691 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000692
Nick Thomasb2e3d872011-02-22 15:44:51 +0000693 TRACE("Got reply: "
694 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
695 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000696
Nick Thomasb2e3d872011-02-22 15:44:51 +0000697 if (magic != NBD_REPLY_MAGIC) {
698 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100699 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000700 }
701 return 0;
ths75818252008-07-03 13:41:03 +0000702}
703
Paolo Bonzini94e73402012-03-07 11:25:01 +0100704static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000705{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200706 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100707 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000708
Nick Thomasb2e3d872011-02-22 15:44:51 +0000709 /* Reply
710 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
711 [ 4 .. 7] error (0 == no error)
712 [ 7 .. 15] handle
713 */
714 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
715 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
716 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000717
Nick Thomasb2e3d872011-02-22 15:44:51 +0000718 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000719
Paolo Bonzini185b4332012-03-05 08:56:10 +0100720 ret = write_sync(csock, buf, sizeof(buf));
721 if (ret < 0) {
722 return ret;
723 }
724
725 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000726 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100727 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000728 }
729 return 0;
ths75818252008-07-03 13:41:03 +0000730}
731
Paolo Bonzini41996e32011-09-19 15:25:40 +0200732#define MAX_NBD_REQUESTS 16
733
Paolo Bonzinice339672012-09-18 13:17:52 +0200734void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200735{
736 client->refcount++;
737}
738
Paolo Bonzinice339672012-09-18 13:17:52 +0200739void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200740{
741 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200742 /* The last reference should be dropped by client->close,
743 * which is called by nbd_client_close.
744 */
745 assert(client->closing);
746
747 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
748 close(client->sock);
749 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200750 if (client->exp) {
751 QTAILQ_REMOVE(&client->exp->clients, client, next);
752 nbd_export_put(client->exp);
753 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200754 g_free(client);
755 }
756}
757
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200758void nbd_client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200759{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200760 if (client->closing) {
761 return;
762 }
763
764 client->closing = true;
765
766 /* Force requests to finish. They will drop their own references,
767 * then we'll close the socket and free the NBDClient.
768 */
769 shutdown(client->sock, 2);
770
771 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200772 if (client->close) {
773 client->close(client);
774 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200775}
776
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200777static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200778{
779 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200780
Paolo Bonzini41996e32011-09-19 15:25:40 +0200781 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
782 client->nb_requests++;
783
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200784 req = g_slice_new0(NBDRequest);
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200785 nbd_client_get(client);
786 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200787 return req;
788}
789
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200790static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200791{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200792 NBDClient *client = req->client;
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200793
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200794 if (req->data) {
795 qemu_vfree(req->data);
796 }
Stefan Hajnoczie1adb272013-05-02 14:23:07 +0200797 g_slice_free(NBDRequest, req);
798
Paolo Bonzini41996e32011-09-19 15:25:40 +0200799 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
800 qemu_notify_event();
801 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200802 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200803}
804
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200805NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200806 off_t size, uint32_t nbdflags,
807 void (*close)(NBDExport *))
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200808{
809 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200810 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200811 QTAILQ_INIT(&exp->clients);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200812 exp->bs = bs;
813 exp->dev_offset = dev_offset;
814 exp->nbdflags = nbdflags;
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100815 exp->size = size == -1 ? bdrv_getlength(bs) : size;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200816 exp->close = close;
Fam Zheng38b54b62013-08-23 09:14:50 +0800817 bdrv_ref(bs);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200818 return exp;
819}
820
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200821NBDExport *nbd_export_find(const char *name)
822{
823 NBDExport *exp;
824 QTAILQ_FOREACH(exp, &exports, next) {
825 if (strcmp(name, exp->name) == 0) {
826 return exp;
827 }
828 }
829
830 return NULL;
831}
832
833void nbd_export_set_name(NBDExport *exp, const char *name)
834{
835 if (exp->name == name) {
836 return;
837 }
838
839 nbd_export_get(exp);
840 if (exp->name != NULL) {
841 g_free(exp->name);
842 exp->name = NULL;
843 QTAILQ_REMOVE(&exports, exp, next);
844 nbd_export_put(exp);
845 }
846 if (name != NULL) {
847 nbd_export_get(exp);
848 exp->name = g_strdup(name);
849 QTAILQ_INSERT_TAIL(&exports, exp, next);
850 }
851 nbd_export_put(exp);
852}
853
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200854void nbd_export_close(NBDExport *exp)
855{
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200856 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200857
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200858 nbd_export_get(exp);
859 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
860 nbd_client_close(client);
861 }
Paolo Bonzini125afda2012-09-18 14:31:44 +0200862 nbd_export_set_name(exp, NULL);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200863 nbd_export_put(exp);
Fam Zheng38b54b62013-08-23 09:14:50 +0800864 if (exp->bs) {
865 bdrv_unref(exp->bs);
866 exp->bs = NULL;
867 }
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200868}
869
870void nbd_export_get(NBDExport *exp)
871{
872 assert(exp->refcount > 0);
873 exp->refcount++;
874}
875
876void nbd_export_put(NBDExport *exp)
877{
878 assert(exp->refcount > 0);
879 if (exp->refcount == 1) {
880 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200881 }
882
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200883 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200884 assert(exp->name == NULL);
885
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200886 if (exp->close) {
887 exp->close(exp);
888 }
889
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200890 g_free(exp);
891 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200892}
893
Paolo Bonzini125afda2012-09-18 14:31:44 +0200894BlockDriverState *nbd_export_get_blockdev(NBDExport *exp)
895{
896 return exp->bs;
897}
898
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200899void nbd_export_close_all(void)
900{
901 NBDExport *exp, *next;
902
903 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
904 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200905 }
906}
907
Paolo Bonzini41996e32011-09-19 15:25:40 +0200908static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +0200909static void nbd_read(void *opaque);
910static void nbd_restart_write(void *opaque);
911
Paolo Bonzini94e73402012-03-07 11:25:01 +0100912static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
913 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +0200914{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200915 NBDClient *client = req->client;
916 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100917 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +0200918
Paolo Bonzini262db382011-09-19 15:19:27 +0200919 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200920 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
921 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200922 client->send_coroutine = qemu_coroutine_self();
923
Paolo Bonzini22045592011-09-19 14:25:30 +0200924 if (!len) {
925 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +0200926 } else {
927 socket_set_cork(csock, 1);
928 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100929 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +0200930 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +0200931 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100932 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +0200933 }
934 }
Paolo Bonzini22045592011-09-19 14:25:30 +0200935 socket_set_cork(csock, 0);
936 }
Paolo Bonzini262db382011-09-19 15:19:27 +0200937
938 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +0200939 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +0200940 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +0200941 return rc;
942}
943
Paolo Bonzini94e73402012-03-07 11:25:01 +0100944static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +0200945{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200946 NBDClient *client = req->client;
947 int csock = client->sock;
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200948 uint32_t command;
Paolo Bonzini94e73402012-03-07 11:25:01 +0100949 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200950
Paolo Bonzini262db382011-09-19 15:19:27 +0200951 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100952 rc = nbd_receive_request(csock, request);
953 if (rc < 0) {
954 if (rc != -EAGAIN) {
955 rc = -EIO;
956 }
Paolo Bonzinia030b342011-09-19 15:07:54 +0200957 goto out;
958 }
959
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200960 if (request->len > NBD_MAX_BUFFER_SIZE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +0200961 LOG("len (%u) is larger than max len (%u)",
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200962 request->len, NBD_MAX_BUFFER_SIZE);
Paolo Bonzinia030b342011-09-19 15:07:54 +0200963 rc = -EINVAL;
964 goto out;
965 }
966
967 if ((request->from + request->len) < request->from) {
968 LOG("integer overflow detected! "
969 "you're probably being attacked");
970 rc = -EINVAL;
971 goto out;
972 }
973
974 TRACE("Decoding type");
975
Stefan Hajnoczi2d821482013-05-02 14:23:08 +0200976 command = request->type & NBD_CMD_MASK_COMMAND;
977 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
978 req->data = qemu_blockalign(client->exp->bs, request->len);
979 }
980 if (command == NBD_CMD_WRITE) {
Paolo Bonzinia030b342011-09-19 15:07:54 +0200981 TRACE("Reading %u byte(s)", request->len);
982
Paolo Bonzini262db382011-09-19 15:19:27 +0200983 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +0200984 LOG("reading from socket failed");
985 rc = -EIO;
986 goto out;
987 }
988 }
989 rc = 0;
990
991out:
Paolo Bonzini262db382011-09-19 15:19:27 +0200992 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +0200993 return rc;
994}
995
Paolo Bonzini262db382011-09-19 15:19:27 +0200996static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +0000997{
Paolo Bonzini262db382011-09-19 15:19:27 +0200998 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +0200999 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001000 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001001 struct nbd_request request;
1002 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001003 ssize_t ret;
ths75818252008-07-03 13:41:03 +00001004
Nick Thomasb2e3d872011-02-22 15:44:51 +00001005 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001006 if (client->closing) {
1007 return;
1008 }
ths75818252008-07-03 13:41:03 +00001009
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001010 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001011 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001012 if (ret == -EAGAIN) {
1013 goto done;
1014 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001015 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001016 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001017 }
ths75818252008-07-03 13:41:03 +00001018
Paolo Bonzinifae69412011-09-19 16:04:36 +02001019 reply.handle = request.handle;
1020 reply.error = 0;
1021
Paolo Bonzinia030b342011-09-19 15:07:54 +02001022 if (ret < 0) {
1023 reply.error = -ret;
1024 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001025 }
bellard7a5ca862008-05-27 21:13:40 +00001026
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001027 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001028 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1029 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001030 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001031 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001032 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001033 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001034 }
bellard7a5ca862008-05-27 21:13:40 +00001035
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001036 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001037 case NBD_CMD_READ:
1038 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001039
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001040 if (request.type & NBD_CMD_FLAG_FUA) {
1041 ret = bdrv_co_flush(exp->bs);
1042 if (ret < 0) {
1043 LOG("flush failed");
1044 reply.error = -ret;
1045 goto error_reply;
1046 }
1047 }
1048
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001049 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001050 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001051 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001052 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001053 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001054 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001055 }
bellard7a5ca862008-05-27 21:13:40 +00001056
Nick Thomasb2e3d872011-02-22 15:44:51 +00001057 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001058 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001059 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001060 break;
1061 case NBD_CMD_WRITE:
1062 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001063
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001064 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001065 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001066 reply.error = EROFS;
1067 goto error_reply;
1068 }
bellard7a5ca862008-05-27 21:13:40 +00001069
Paolo Bonzinifae69412011-09-19 16:04:36 +02001070 TRACE("Writing to device");
1071
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001072 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001073 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001074 if (ret < 0) {
1075 LOG("writing to file failed");
1076 reply.error = -ret;
1077 goto error_reply;
1078 }
1079
1080 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001081 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001082 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001083 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001084 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001085 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001086 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001087 }
bellard7a5ca862008-05-27 21:13:40 +00001088
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001089 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001090 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001091 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001092 break;
1093 case NBD_CMD_DISC:
1094 TRACE("Request type is DISCONNECT");
1095 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001096 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001097 case NBD_CMD_FLUSH:
1098 TRACE("Request type is FLUSH");
1099
Paolo Bonzini262db382011-09-19 15:19:27 +02001100 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001101 if (ret < 0) {
1102 LOG("flush failed");
1103 reply.error = -ret;
1104 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001105 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001106 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001107 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001108 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001109 case NBD_CMD_TRIM:
1110 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +02001111 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
1112 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001113 if (ret < 0) {
1114 LOG("discard failed");
1115 reply.error = -ret;
1116 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001117 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001118 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001119 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001120 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001121 default:
1122 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001123 invalid_request:
1124 reply.error = -EINVAL;
1125 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001126 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001127 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001128 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001129 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001130 }
bellard7a5ca862008-05-27 21:13:40 +00001131
Nick Thomasb2e3d872011-02-22 15:44:51 +00001132 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001133
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001134done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001135 nbd_request_put(req);
1136 return;
1137
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001138out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001139 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +02001140 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001141}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001142
Paolo Bonzini41996e32011-09-19 15:25:40 +02001143static int nbd_can_read(void *opaque)
1144{
1145 NBDClient *client = opaque;
1146
1147 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
1148}
1149
Paolo Bonzini1743b512011-09-19 14:33:23 +02001150static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001151{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001152 NBDClient *client = opaque;
1153
Paolo Bonzini262db382011-09-19 15:19:27 +02001154 if (client->recv_coroutine) {
1155 qemu_coroutine_enter(client->recv_coroutine, NULL);
1156 } else {
1157 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001158 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001159}
1160
Paolo Bonzini262db382011-09-19 15:19:27 +02001161static void nbd_restart_write(void *opaque)
1162{
1163 NBDClient *client = opaque;
1164
1165 qemu_coroutine_enter(client->send_coroutine, NULL);
1166}
1167
Paolo Bonzini1743b512011-09-19 14:33:23 +02001168NBDClient *nbd_client_new(NBDExport *exp, int csock,
1169 void (*close)(NBDClient *))
1170{
1171 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001172 client = g_malloc0(sizeof(NBDClient));
1173 client->refcount = 1;
1174 client->exp = exp;
1175 client->sock = csock;
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001176 if (nbd_send_negotiate(client) < 0) {
1177 g_free(client);
1178 return NULL;
1179 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001180 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001181 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001182 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001183
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001184 if (exp) {
1185 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1186 nbd_export_get(exp);
1187 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001188 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001189}