blob: 97879ca204d5ff7af0eb606d030fe5372e5422f6 [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"
ths75818252008-07-03 13:41:03 +000041
aliguori03ff3ca2008-09-15 15:51:35 +000042//#define DEBUG_NBD
43
44#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000045#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000046 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000047} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000048#else
49#define TRACE(msg, ...) \
50 do { } while (0)
51#endif
bellard7a5ca862008-05-27 21:13:40 +000052
53#define LOG(msg, ...) do { \
54 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
55 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
56} while(0)
57
bellard7a5ca862008-05-27 21:13:40 +000058/* This is all part of the "official" NBD API */
59
Paolo Bonzinifa26c262012-08-22 15:13:30 +020060#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
Nick Thomasb2e3d872011-02-22 15:44:51 +000061#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000062#define NBD_REQUEST_MAGIC 0x25609513
63#define NBD_REPLY_MAGIC 0x67446698
Paolo Bonzinifa26c262012-08-22 15:13:30 +020064#define NBD_OPTS_MAGIC 0x49484156454F5054LL
65#define NBD_CLIENT_MAGIC 0x0000420281861253LL
bellard7a5ca862008-05-27 21:13:40 +000066
67#define NBD_SET_SOCK _IO(0xab, 0)
68#define NBD_SET_BLKSIZE _IO(0xab, 1)
69#define NBD_SET_SIZE _IO(0xab, 2)
70#define NBD_DO_IT _IO(0xab, 3)
71#define NBD_CLEAR_SOCK _IO(0xab, 4)
72#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000073#define NBD_PRINT_DEBUG _IO(0xab, 6)
74#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000075#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020076#define NBD_SET_TIMEOUT _IO(0xab, 9)
77#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000078
Nick Thomasb2e3d872011-02-22 15:44:51 +000079#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020080
Paolo Bonzini9a304d22012-08-22 15:30:31 +020081/* Definitions for opaque data types */
82
83typedef struct NBDRequest NBDRequest;
84
85struct NBDRequest {
86 QSIMPLEQ_ENTRY(NBDRequest) entry;
87 NBDClient *client;
88 uint8_t *data;
89};
90
91struct NBDExport {
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +020092 int refcount;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +020093 void (*close)(NBDExport *exp);
94
Paolo Bonzini9a304d22012-08-22 15:30:31 +020095 BlockDriverState *bs;
Paolo Bonziniee0a19e2012-08-22 15:59:23 +020096 char *name;
Paolo Bonzini9a304d22012-08-22 15:30:31 +020097 off_t dev_offset;
98 off_t size;
99 uint32_t nbdflags;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200100 QTAILQ_HEAD(, NBDClient) clients;
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200101 QSIMPLEQ_HEAD(, NBDRequest) requests;
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
Nick Thomasc12504c2011-02-22 15:44:53 +0000191static void combine_addr(char *buf, size_t len, const char* address,
192 uint16_t port)
193{
194 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
195 if (strstr(address, ":")) {
196 snprintf(buf, len, "[%s]:%u", address, port);
197 } else {
198 snprintf(buf, len, "%s:%u", address, port);
199 }
200}
201
ths75818252008-07-03 13:41:03 +0000202int tcp_socket_outgoing(const char *address, uint16_t port)
bellard7a5ca862008-05-27 21:13:40 +0000203{
Nick Thomasc12504c2011-02-22 15:44:53 +0000204 char address_and_port[128];
205 combine_addr(address_and_port, 128, address, port);
206 return tcp_socket_outgoing_spec(address_and_port);
207}
bellard7a5ca862008-05-27 21:13:40 +0000208
Nick Thomasc12504c2011-02-22 15:44:53 +0000209int tcp_socket_outgoing_spec(const char *address_and_port)
210{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200211 Error *local_err = NULL;
212 int fd = inet_connect(address_and_port, &local_err);
213
214 if (local_err != NULL) {
215 qerror_report_err(local_err);
216 error_free(local_err);
217 }
218 return fd;
bellard7a5ca862008-05-27 21:13:40 +0000219}
220
Kevin Wolff17c90b2013-03-15 11:55:29 +0100221int tcp_socket_outgoing_opts(QemuOpts *opts)
222{
223 Error *local_err = NULL;
224 int fd = inet_connect_opts(opts, &local_err, NULL, NULL);
225 if (local_err != NULL) {
226 qerror_report_err(local_err);
227 error_free(local_err);
228 }
229
230 return fd;
231}
232
bellard7a5ca862008-05-27 21:13:40 +0000233int tcp_socket_incoming(const char *address, uint16_t port)
234{
Nick Thomasc12504c2011-02-22 15:44:53 +0000235 char address_and_port[128];
236 combine_addr(address_and_port, 128, address, port);
237 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000238}
239
Nick Thomasc12504c2011-02-22 15:44:53 +0000240int tcp_socket_incoming_spec(const char *address_and_port)
241{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200242 Error *local_err = NULL;
243 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
244
245 if (local_err != NULL) {
246 qerror_report_err(local_err);
247 error_free(local_err);
248 }
249 return fd;
Nick Thomasc12504c2011-02-22 15:44:53 +0000250}
251
thscd831bd2008-07-03 10:23:51 +0000252int unix_socket_incoming(const char *path)
253{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200254 Error *local_err = NULL;
255 int fd = unix_listen(path, NULL, 0, &local_err);
thscd831bd2008-07-03 10:23:51 +0000256
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200257 if (local_err != NULL) {
258 qerror_report_err(local_err);
259 error_free(local_err);
260 }
261 return fd;
thscd831bd2008-07-03 10:23:51 +0000262}
263
264int unix_socket_outgoing(const char *path)
265{
Paolo Bonzinif8430e72012-10-02 10:07:21 +0200266 Error *local_err = NULL;
267 int fd = unix_connect(path, &local_err);
268
269 if (local_err != NULL) {
270 qerror_report_err(local_err);
271 error_free(local_err);
272 }
273 return fd;
thscd831bd2008-07-03 10:23:51 +0000274}
thscd831bd2008-07-03 10:23:51 +0000275
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200276/* Basic flow for negotiation
bellard7a5ca862008-05-27 21:13:40 +0000277
278 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000279 Negotiate
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200280
281 or
282
283 Server Client
284 Negotiate #1
285 Option
286 Negotiate #2
287
288 ----
289
290 followed by
291
292 Server Client
bellard7a5ca862008-05-27 21:13:40 +0000293 Request
294 Response
295 Request
296 Response
297 ...
298 ...
299 Request (type == 2)
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200300
bellard7a5ca862008-05-27 21:13:40 +0000301*/
302
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200303static int nbd_receive_options(NBDClient *client)
304{
305 int csock = client->sock;
306 char name[256];
307 uint32_t tmp, length;
308 uint64_t magic;
309 int rc;
310
311 /* Client sends:
312 [ 0 .. 3] reserved (0)
313 [ 4 .. 11] NBD_OPTS_MAGIC
314 [12 .. 15] NBD_OPT_EXPORT_NAME
315 [16 .. 19] length
316 [20 .. xx] export name (length bytes)
317 */
318
319 rc = -EINVAL;
320 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
321 LOG("read failed");
322 goto fail;
323 }
324 TRACE("Checking reserved");
325 if (tmp != 0) {
326 LOG("Bad reserved received");
327 goto fail;
328 }
329
330 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
331 LOG("read failed");
332 goto fail;
333 }
334 TRACE("Checking reserved");
335 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
336 LOG("Bad magic received");
337 goto fail;
338 }
339
340 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
341 LOG("read failed");
342 goto fail;
343 }
344 TRACE("Checking option");
345 if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
346 LOG("Bad option received");
347 goto fail;
348 }
349
350 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
351 LOG("read failed");
352 goto fail;
353 }
354 TRACE("Checking length");
355 length = be32_to_cpu(length);
356 if (length > 255) {
357 LOG("Bad length received");
358 goto fail;
359 }
360 if (read_sync(csock, name, length) != length) {
361 LOG("read failed");
362 goto fail;
363 }
364 name[length] = '\0';
365
366 client->exp = nbd_export_find(name);
367 if (!client->exp) {
368 LOG("export not found");
369 goto fail;
370 }
371
372 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
373 nbd_export_get(client->exp);
374
375 TRACE("Option negotiation succeeded.");
376 rc = 0;
377fail:
378 return rc;
379}
380
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200381static int nbd_send_negotiate(NBDClient *client)
bellard7a5ca862008-05-27 21:13:40 +0000382{
Paolo Bonzini9a304d22012-08-22 15:30:31 +0200383 int csock = client->sock;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000384 char buf[8 + 8 + 8 + 128];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100385 int rc;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200386 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
387 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bellard7a5ca862008-05-27 21:13:40 +0000388
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200389 /* Negotiation header without options:
390 [ 0 .. 7] passwd ("NBDMAGIC")
391 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000392 [16 .. 23] size
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200393 [24 .. 25] server flags (0)
394 [24 .. 27] export flags
395 [28 .. 151] reserved (0)
396
397 Negotiation header with options, part 1:
398 [ 0 .. 7] passwd ("NBDMAGIC")
399 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
400 [16 .. 17] server flags (0)
401
402 part 2 (after options are sent):
403 [18 .. 25] size
404 [26 .. 27] export flags
405 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000406 */
bellard7a5ca862008-05-27 21:13:40 +0000407
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100408 socket_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100409 rc = -EINVAL;
410
Nick Thomasb2e3d872011-02-22 15:44:51 +0000411 TRACE("Beginning negotiation.");
Paolo Bonzini8ffaaba2012-11-26 15:19:31 +0100412 memset(buf, 0, sizeof(buf));
Nick Thomasb2e3d872011-02-22 15:44:51 +0000413 memcpy(buf, "NBDMAGIC", 8);
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200414 if (client->exp) {
415 assert ((client->exp->nbdflags & ~65535) == 0);
416 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
417 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
418 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
419 } else {
420 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
421 }
bellard7a5ca862008-05-27 21:13:40 +0000422
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200423 if (client->exp) {
424 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
425 LOG("write failed");
426 goto fail;
427 }
428 } else {
429 if (write_sync(csock, buf, 18) != 18) {
430 LOG("write failed");
431 goto fail;
432 }
433 rc = nbd_receive_options(client);
434 if (rc < 0) {
435 LOG("option negotiation failed");
436 goto fail;
437 }
438
439 assert ((client->exp->nbdflags & ~65535) == 0);
440 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
441 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
442 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
443 LOG("write failed");
444 goto fail;
445 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000446 }
bellard7a5ca862008-05-27 21:13:40 +0000447
Dong Xu Wang07f35072011-11-22 18:06:26 +0800448 TRACE("Negotiation succeeded.");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100449 rc = 0;
450fail:
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100451 socket_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100452 return rc;
bellard7a5ca862008-05-27 21:13:40 +0000453}
454
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200455int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
456 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000457{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000458 char buf[256];
459 uint64_t magic, s;
460 uint16_t tmp;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100461 int rc;
bellard7a5ca862008-05-27 21:13:40 +0000462
Dong Xu Wang07f35072011-11-22 18:06:26 +0800463 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000464
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100465 socket_set_block(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100466 rc = -EINVAL;
467
Nick Thomasb2e3d872011-02-22 15:44:51 +0000468 if (read_sync(csock, buf, 8) != 8) {
469 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100470 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000471 }
bellard7a5ca862008-05-27 21:13:40 +0000472
Nick Thomasb2e3d872011-02-22 15:44:51 +0000473 buf[8] = '\0';
474 if (strlen(buf) == 0) {
475 LOG("server connection closed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100476 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000477 }
bellard7a5ca862008-05-27 21:13:40 +0000478
Nick Thomasb2e3d872011-02-22 15:44:51 +0000479 TRACE("Magic is %c%c%c%c%c%c%c%c",
480 qemu_isprint(buf[0]) ? buf[0] : '.',
481 qemu_isprint(buf[1]) ? buf[1] : '.',
482 qemu_isprint(buf[2]) ? buf[2] : '.',
483 qemu_isprint(buf[3]) ? buf[3] : '.',
484 qemu_isprint(buf[4]) ? buf[4] : '.',
485 qemu_isprint(buf[5]) ? buf[5] : '.',
486 qemu_isprint(buf[6]) ? buf[6] : '.',
487 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000488
Nick Thomasb2e3d872011-02-22 15:44:51 +0000489 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
490 LOG("Invalid magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100491 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000492 }
bellard7a5ca862008-05-27 21:13:40 +0000493
Nick Thomasb2e3d872011-02-22 15:44:51 +0000494 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
495 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100496 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000497 }
498 magic = be64_to_cpu(magic);
499 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000500
Nick Thomasb2e3d872011-02-22 15:44:51 +0000501 if (name) {
502 uint32_t reserved = 0;
503 uint32_t opt;
504 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200505
Nick Thomasb2e3d872011-02-22 15:44:51 +0000506 TRACE("Checking magic (opts_magic)");
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200507 if (magic != NBD_OPTS_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000508 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100509 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000510 }
511 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
512 LOG("flags read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100513 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000514 }
515 *flags = be16_to_cpu(tmp) << 16;
516 /* reserved for future use */
517 if (write_sync(csock, &reserved, sizeof(reserved)) !=
518 sizeof(reserved)) {
519 LOG("write failed (reserved)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100520 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000521 }
522 /* write the export name */
523 magic = cpu_to_be64(magic);
524 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
525 LOG("write failed (magic)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100526 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000527 }
528 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
529 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
530 LOG("write failed (opt)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100531 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000532 }
533 namesize = cpu_to_be32(strlen(name));
534 if (write_sync(csock, &namesize, sizeof(namesize)) !=
535 sizeof(namesize)) {
536 LOG("write failed (namesize)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100537 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000538 }
539 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
540 LOG("write failed (name)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100541 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000542 }
543 } else {
544 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200545
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200546 if (magic != NBD_CLIENT_MAGIC) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000547 LOG("Bad magic received");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100548 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000549 }
550 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200551
Nick Thomasb2e3d872011-02-22 15:44:51 +0000552 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
553 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100554 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000555 }
556 *size = be64_to_cpu(s);
557 *blocksize = 1024;
558 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200559
Nick Thomasb2e3d872011-02-22 15:44:51 +0000560 if (!name) {
561 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
562 LOG("read failed (flags)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100563 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000564 }
565 *flags = be32_to_cpup(flags);
566 } else {
567 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
568 LOG("read failed (tmp)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100569 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000570 }
571 *flags |= be32_to_cpu(tmp);
572 }
573 if (read_sync(csock, &buf, 124) != 124) {
574 LOG("read failed (buf)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100575 goto fail;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000576 }
Paolo Bonzini185b4332012-03-05 08:56:10 +0100577 rc = 0;
578
579fail:
Paolo Bonzini7fe7b682012-03-05 09:10:35 +0100580 socket_set_nonblock(csock);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100581 return rc;
thscd831bd2008-07-03 10:23:51 +0000582}
bellard7a5ca862008-05-27 21:13:40 +0000583
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200584#ifdef __linux__
585int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000586{
Chunyan Liu3e05c782011-12-02 23:27:54 +0800587 TRACE("Setting NBD socket");
588
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100589 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
Chunyan Liu3e05c782011-12-02 23:27:54 +0800590 int serrno = errno;
591 LOG("Failed to set NBD socket");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100592 return -serrno;
Chunyan Liu3e05c782011-12-02 23:27:54 +0800593 }
594
Nick Thomasb2e3d872011-02-22 15:44:51 +0000595 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000596
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100597 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000598 int serrno = errno;
599 LOG("Failed setting NBD block size");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100600 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000601 }
bellard7a5ca862008-05-27 21:13:40 +0000602
Blue Swirl0bfcd592010-05-22 08:02:12 +0000603 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000604
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100605 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000606 int serrno = errno;
607 LOG("Failed setting size (in blocks)");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100608 return -serrno;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000609 }
bellard7a5ca862008-05-27 21:13:40 +0000610
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100611 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
612 if (errno == ENOTTY) {
613 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
614 TRACE("Setting readonly attribute");
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200615
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100616 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
617 int serrno = errno;
618 LOG("Failed setting read-only attribute");
619 return -serrno;
620 }
621 } else {
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200622 int serrno = errno;
Paolo Bonzinic8969ed2012-11-13 10:34:17 +0100623 LOG("Failed setting flags");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100624 return -serrno;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200625 }
626 }
627
Nick Thomasb2e3d872011-02-22 15:44:51 +0000628 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000629
Nick Thomasb2e3d872011-02-22 15:44:51 +0000630 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000631}
632
633int nbd_disconnect(int fd)
634{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000635 ioctl(fd, NBD_CLEAR_QUE);
636 ioctl(fd, NBD_DISCONNECT);
637 ioctl(fd, NBD_CLEAR_SOCK);
638 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000639}
640
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200641int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000642{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000643 int ret;
644 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000645
Nick Thomasb2e3d872011-02-22 15:44:51 +0000646 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000647
Nick Thomasb2e3d872011-02-22 15:44:51 +0000648 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100649 if (ret < 0 && errno == EPIPE) {
Paolo Bonzini74624682011-11-04 15:51:18 +0100650 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
651 * the socket via NBD_DISCONNECT. We do not want to return 1 in
652 * that case.
653 */
654 ret = 0;
655 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000656 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000657
Nick Thomasb2e3d872011-02-22 15:44:51 +0000658 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000659
Nick Thomasb2e3d872011-02-22 15:44:51 +0000660 TRACE("Clearing NBD queue");
661 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000662
Nick Thomasb2e3d872011-02-22 15:44:51 +0000663 TRACE("Clearing NBD socket");
664 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000665
Nick Thomasb2e3d872011-02-22 15:44:51 +0000666 errno = serrno;
667 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000668}
aliguori03ff3ca2008-09-15 15:51:35 +0000669#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200670int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000671{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100672 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000673}
674
675int nbd_disconnect(int fd)
676{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100677 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000678}
679
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200680int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000681{
Paolo Bonzini185b4332012-03-05 08:56:10 +0100682 return -ENOTSUP;
aliguori03ff3ca2008-09-15 15:51:35 +0000683}
684#endif
bellard7a5ca862008-05-27 21:13:40 +0000685
Paolo Bonzini94e73402012-03-07 11:25:01 +0100686ssize_t nbd_send_request(int csock, struct nbd_request *request)
ths75818252008-07-03 13:41:03 +0000687{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200688 uint8_t buf[NBD_REQUEST_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100689 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000690
Nick Thomasb2e3d872011-02-22 15:44:51 +0000691 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
692 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
693 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
694 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
695 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000696
Nick Thomasb2e3d872011-02-22 15:44:51 +0000697 TRACE("Sending request to client: "
698 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
699 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000700
Paolo Bonzini185b4332012-03-05 08:56:10 +0100701 ret = write_sync(csock, buf, sizeof(buf));
702 if (ret < 0) {
703 return ret;
704 }
705
706 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000707 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100708 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000709 }
710 return 0;
ths75818252008-07-03 13:41:03 +0000711}
712
Paolo Bonzini94e73402012-03-07 11:25:01 +0100713static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000714{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200715 uint8_t buf[NBD_REQUEST_SIZE];
Nick Thomasb2e3d872011-02-22 15:44:51 +0000716 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100717 ssize_t ret;
bellard7a5ca862008-05-27 21:13:40 +0000718
Paolo Bonzini185b4332012-03-05 08:56:10 +0100719 ret = read_sync(csock, buf, sizeof(buf));
720 if (ret < 0) {
721 return ret;
722 }
723
724 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000725 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100726 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000727 }
bellard7a5ca862008-05-27 21:13:40 +0000728
Nick Thomasb2e3d872011-02-22 15:44:51 +0000729 /* Request
730 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
731 [ 4 .. 7] type (0 == READ, 1 == WRITE)
732 [ 8 .. 15] handle
733 [16 .. 23] from
734 [24 .. 27] len
735 */
bellard7a5ca862008-05-27 21:13:40 +0000736
Nick Thomasb2e3d872011-02-22 15:44:51 +0000737 magic = be32_to_cpup((uint32_t*)buf);
738 request->type = be32_to_cpup((uint32_t*)(buf + 4));
739 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
740 request->from = be64_to_cpup((uint64_t*)(buf + 16));
741 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000742
Nick Thomasb2e3d872011-02-22 15:44:51 +0000743 TRACE("Got request: "
744 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
745 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000746
Nick Thomasb2e3d872011-02-22 15:44:51 +0000747 if (magic != NBD_REQUEST_MAGIC) {
748 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100749 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000750 }
751 return 0;
ths75818252008-07-03 13:41:03 +0000752}
bellard7a5ca862008-05-27 21:13:40 +0000753
Paolo Bonzini94e73402012-03-07 11:25:01 +0100754ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000755{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000756 uint8_t buf[NBD_REPLY_SIZE];
757 uint32_t magic;
Paolo Bonzini185b4332012-03-05 08:56:10 +0100758 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000759
Paolo Bonzini185b4332012-03-05 08:56:10 +0100760 ret = read_sync(csock, buf, sizeof(buf));
761 if (ret < 0) {
762 return ret;
763 }
764
765 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000766 LOG("read failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100767 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000768 }
bellard7a5ca862008-05-27 21:13:40 +0000769
Nick Thomasb2e3d872011-02-22 15:44:51 +0000770 /* Reply
771 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
772 [ 4 .. 7] error (0 == no error)
773 [ 7 .. 15] handle
774 */
ths75818252008-07-03 13:41:03 +0000775
Nick Thomasb2e3d872011-02-22 15:44:51 +0000776 magic = be32_to_cpup((uint32_t*)buf);
777 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
778 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000779
Nick Thomasb2e3d872011-02-22 15:44:51 +0000780 TRACE("Got reply: "
781 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
782 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000783
Nick Thomasb2e3d872011-02-22 15:44:51 +0000784 if (magic != NBD_REPLY_MAGIC) {
785 LOG("invalid magic (got 0x%x)", magic);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100786 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000787 }
788 return 0;
ths75818252008-07-03 13:41:03 +0000789}
790
Paolo Bonzini94e73402012-03-07 11:25:01 +0100791static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
ths75818252008-07-03 13:41:03 +0000792{
Paolo Bonzinifa26c262012-08-22 15:13:30 +0200793 uint8_t buf[NBD_REPLY_SIZE];
Paolo Bonzini185b4332012-03-05 08:56:10 +0100794 ssize_t ret;
ths75818252008-07-03 13:41:03 +0000795
Nick Thomasb2e3d872011-02-22 15:44:51 +0000796 /* Reply
797 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
798 [ 4 .. 7] error (0 == no error)
799 [ 7 .. 15] handle
800 */
801 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
802 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
803 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000804
Nick Thomasb2e3d872011-02-22 15:44:51 +0000805 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000806
Paolo Bonzini185b4332012-03-05 08:56:10 +0100807 ret = write_sync(csock, buf, sizeof(buf));
808 if (ret < 0) {
809 return ret;
810 }
811
812 if (ret != sizeof(buf)) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000813 LOG("writing to socket failed");
Paolo Bonzini185b4332012-03-05 08:56:10 +0100814 return -EINVAL;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000815 }
816 return 0;
ths75818252008-07-03 13:41:03 +0000817}
818
Paolo Bonzini41996e32011-09-19 15:25:40 +0200819#define MAX_NBD_REQUESTS 16
820
Paolo Bonzinice339672012-09-18 13:17:52 +0200821void nbd_client_get(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200822{
823 client->refcount++;
824}
825
Paolo Bonzinice339672012-09-18 13:17:52 +0200826void nbd_client_put(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200827{
828 if (--client->refcount == 0) {
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200829 /* The last reference should be dropped by client->close,
830 * which is called by nbd_client_close.
831 */
832 assert(client->closing);
833
834 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
835 close(client->sock);
836 client->sock = -1;
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +0200837 if (client->exp) {
838 QTAILQ_REMOVE(&client->exp->clients, client, next);
839 nbd_export_put(client->exp);
840 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200841 g_free(client);
842 }
843}
844
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200845void nbd_client_close(NBDClient *client)
Paolo Bonzini1743b512011-09-19 14:33:23 +0200846{
Paolo Bonziniff2b68a2012-08-22 18:45:12 +0200847 if (client->closing) {
848 return;
849 }
850
851 client->closing = true;
852
853 /* Force requests to finish. They will drop their own references,
854 * then we'll close the socket and free the NBDClient.
855 */
856 shutdown(client->sock, 2);
857
858 /* Also tell the client, so that they release their reference. */
Paolo Bonzini1743b512011-09-19 14:33:23 +0200859 if (client->close) {
860 client->close(client);
861 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200862}
863
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200864static NBDRequest *nbd_request_get(NBDClient *client)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200865{
866 NBDRequest *req;
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200867 NBDExport *exp = client->exp;
868
Paolo Bonzini41996e32011-09-19 15:25:40 +0200869 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
870 client->nb_requests++;
871
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200872 if (QSIMPLEQ_EMPTY(&exp->requests)) {
873 req = g_malloc0(sizeof(NBDRequest));
874 req->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
875 } else {
876 req = QSIMPLEQ_FIRST(&exp->requests);
877 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
878 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200879 nbd_client_get(client);
880 req->client = client;
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200881 return req;
882}
883
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200884static void nbd_request_put(NBDRequest *req)
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200885{
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200886 NBDClient *client = req->client;
887 QSIMPLEQ_INSERT_HEAD(&client->exp->requests, req, entry);
Paolo Bonzini41996e32011-09-19 15:25:40 +0200888 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
889 qemu_notify_event();
890 }
Paolo Bonzini72deddc2011-10-07 16:47:56 +0200891 nbd_client_put(client);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200892}
893
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200894NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200895 off_t size, uint32_t nbdflags,
896 void (*close)(NBDExport *))
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200897{
898 NBDExport *exp = g_malloc0(sizeof(NBDExport));
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200899 QSIMPLEQ_INIT(&exp->requests);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200900 exp->refcount = 1;
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200901 QTAILQ_INIT(&exp->clients);
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200902 exp->bs = bs;
903 exp->dev_offset = dev_offset;
904 exp->nbdflags = nbdflags;
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100905 exp->size = size == -1 ? bdrv_getlength(bs) : size;
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200906 exp->close = close;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200907 return exp;
908}
909
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200910NBDExport *nbd_export_find(const char *name)
911{
912 NBDExport *exp;
913 QTAILQ_FOREACH(exp, &exports, next) {
914 if (strcmp(name, exp->name) == 0) {
915 return exp;
916 }
917 }
918
919 return NULL;
920}
921
922void nbd_export_set_name(NBDExport *exp, const char *name)
923{
924 if (exp->name == name) {
925 return;
926 }
927
928 nbd_export_get(exp);
929 if (exp->name != NULL) {
930 g_free(exp->name);
931 exp->name = NULL;
932 QTAILQ_REMOVE(&exports, exp, next);
933 nbd_export_put(exp);
934 }
935 if (name != NULL) {
936 nbd_export_get(exp);
937 exp->name = g_strdup(name);
938 QTAILQ_INSERT_TAIL(&exports, exp, next);
939 }
940 nbd_export_put(exp);
941}
942
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200943void nbd_export_close(NBDExport *exp)
944{
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200945 NBDClient *client, *next;
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200946
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200947 nbd_export_get(exp);
948 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
949 nbd_client_close(client);
950 }
Paolo Bonzini125afda2012-09-18 14:31:44 +0200951 nbd_export_set_name(exp, NULL);
Paolo Bonzini4b9441f2012-09-18 13:58:25 +0200952 nbd_export_put(exp);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200953}
954
955void nbd_export_get(NBDExport *exp)
956{
957 assert(exp->refcount > 0);
958 exp->refcount++;
959}
960
961void nbd_export_put(NBDExport *exp)
962{
963 assert(exp->refcount > 0);
964 if (exp->refcount == 1) {
965 nbd_export_close(exp);
Paolo Bonzinid9a73802011-09-19 14:18:33 +0200966 }
967
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200968 if (--exp->refcount == 0) {
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200969 assert(exp->name == NULL);
970
Paolo Bonzini0ddf08d2012-09-18 13:59:03 +0200971 if (exp->close) {
972 exp->close(exp);
973 }
974
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +0200975 while (!QSIMPLEQ_EMPTY(&exp->requests)) {
976 NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
977 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
978 qemu_vfree(first->data);
979 g_free(first);
980 }
981
982 g_free(exp);
983 }
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200984}
985
Paolo Bonzini125afda2012-09-18 14:31:44 +0200986BlockDriverState *nbd_export_get_blockdev(NBDExport *exp)
987{
988 return exp->bs;
989}
990
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200991void nbd_export_close_all(void)
992{
993 NBDExport *exp, *next;
994
995 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
996 nbd_export_close(exp);
Paolo Bonziniee0a19e2012-08-22 15:59:23 +0200997 }
998}
999
Paolo Bonzini41996e32011-09-19 15:25:40 +02001000static int nbd_can_read(void *opaque);
Paolo Bonzini262db382011-09-19 15:19:27 +02001001static void nbd_read(void *opaque);
1002static void nbd_restart_write(void *opaque);
1003
Paolo Bonzini94e73402012-03-07 11:25:01 +01001004static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
1005 int len)
Paolo Bonzini22045592011-09-19 14:25:30 +02001006{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001007 NBDClient *client = req->client;
1008 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001009 ssize_t rc, ret;
Paolo Bonzini22045592011-09-19 14:25:30 +02001010
Paolo Bonzini262db382011-09-19 15:19:27 +02001011 qemu_co_mutex_lock(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001012 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
1013 nbd_restart_write, client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001014 client->send_coroutine = qemu_coroutine_self();
1015
Paolo Bonzini22045592011-09-19 14:25:30 +02001016 if (!len) {
1017 rc = nbd_send_reply(csock, reply);
Paolo Bonzini22045592011-09-19 14:25:30 +02001018 } else {
1019 socket_set_cork(csock, 1);
1020 rc = nbd_send_reply(csock, reply);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001021 if (rc >= 0) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001022 ret = qemu_co_send(csock, req->data, len);
Paolo Bonzini22045592011-09-19 14:25:30 +02001023 if (ret != len) {
Paolo Bonzini185b4332012-03-05 08:56:10 +01001024 rc = -EIO;
Paolo Bonzini22045592011-09-19 14:25:30 +02001025 }
1026 }
Paolo Bonzini22045592011-09-19 14:25:30 +02001027 socket_set_cork(csock, 0);
1028 }
Paolo Bonzini262db382011-09-19 15:19:27 +02001029
1030 client->send_coroutine = NULL;
Paolo Bonzini41996e32011-09-19 15:25:40 +02001031 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001032 qemu_co_mutex_unlock(&client->send_lock);
Paolo Bonzini22045592011-09-19 14:25:30 +02001033 return rc;
1034}
1035
Paolo Bonzini94e73402012-03-07 11:25:01 +01001036static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
Paolo Bonzinia030b342011-09-19 15:07:54 +02001037{
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001038 NBDClient *client = req->client;
1039 int csock = client->sock;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001040 ssize_t rc;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001041
Paolo Bonzini262db382011-09-19 15:19:27 +02001042 client->recv_coroutine = qemu_coroutine_self();
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001043 rc = nbd_receive_request(csock, request);
1044 if (rc < 0) {
1045 if (rc != -EAGAIN) {
1046 rc = -EIO;
1047 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001048 goto out;
1049 }
1050
1051 if (request->len > NBD_BUFFER_SIZE) {
1052 LOG("len (%u) is larger than max len (%u)",
1053 request->len, NBD_BUFFER_SIZE);
1054 rc = -EINVAL;
1055 goto out;
1056 }
1057
1058 if ((request->from + request->len) < request->from) {
1059 LOG("integer overflow detected! "
1060 "you're probably being attacked");
1061 rc = -EINVAL;
1062 goto out;
1063 }
1064
1065 TRACE("Decoding type");
1066
1067 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
1068 TRACE("Reading %u byte(s)", request->len);
1069
Paolo Bonzini262db382011-09-19 15:19:27 +02001070 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
Paolo Bonzinia030b342011-09-19 15:07:54 +02001071 LOG("reading from socket failed");
1072 rc = -EIO;
1073 goto out;
1074 }
1075 }
1076 rc = 0;
1077
1078out:
Paolo Bonzini262db382011-09-19 15:19:27 +02001079 client->recv_coroutine = NULL;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001080 return rc;
1081}
1082
Paolo Bonzini262db382011-09-19 15:19:27 +02001083static void nbd_trip(void *opaque)
ths75818252008-07-03 13:41:03 +00001084{
Paolo Bonzini262db382011-09-19 15:19:27 +02001085 NBDClient *client = opaque;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001086 NBDExport *exp = client->exp;
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001087 NBDRequest *req;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001088 struct nbd_request request;
1089 struct nbd_reply reply;
Paolo Bonzini94e73402012-03-07 11:25:01 +01001090 ssize_t ret;
ths75818252008-07-03 13:41:03 +00001091
Nick Thomasb2e3d872011-02-22 15:44:51 +00001092 TRACE("Reading request.");
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001093 if (client->closing) {
1094 return;
1095 }
ths75818252008-07-03 13:41:03 +00001096
Paolo Bonziniff2b68a2012-08-22 18:45:12 +02001097 req = nbd_request_get(client);
Paolo Bonzini262db382011-09-19 15:19:27 +02001098 ret = nbd_co_receive_request(req, &request);
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001099 if (ret == -EAGAIN) {
1100 goto done;
1101 }
Paolo Bonzinia030b342011-09-19 15:07:54 +02001102 if (ret == -EIO) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001103 goto out;
Paolo Bonzinia030b342011-09-19 15:07:54 +02001104 }
ths75818252008-07-03 13:41:03 +00001105
Paolo Bonzinifae69412011-09-19 16:04:36 +02001106 reply.handle = request.handle;
1107 reply.error = 0;
1108
Paolo Bonzinia030b342011-09-19 15:07:54 +02001109 if (ret < 0) {
1110 reply.error = -ret;
1111 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001112 }
bellard7a5ca862008-05-27 21:13:40 +00001113
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001114 if ((request.from + request.len) > exp->size) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001115 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1116 ", Offset: %" PRIu64 "\n",
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001117 request.from, request.len,
Stefan Weil0fee8f32012-04-12 22:30:16 +02001118 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +00001119 LOG("requested operation past EOF--bad client?");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001120 goto invalid_request;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001121 }
bellard7a5ca862008-05-27 21:13:40 +00001122
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001123 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001124 case NBD_CMD_READ:
1125 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +00001126
Paolo Bonzinie25ceb72012-04-19 11:59:11 +02001127 if (request.type & NBD_CMD_FLAG_FUA) {
1128 ret = bdrv_co_flush(exp->bs);
1129 if (ret < 0) {
1130 LOG("flush failed");
1131 reply.error = -ret;
1132 goto error_reply;
1133 }
1134 }
1135
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001136 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001137 req->data, request.len / 512);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001138 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001139 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001140 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001141 goto error_reply;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001142 }
bellard7a5ca862008-05-27 21:13:40 +00001143
Nick Thomasb2e3d872011-02-22 15:44:51 +00001144 TRACE("Read %u byte(s)", request.len);
Paolo Bonzini262db382011-09-19 15:19:27 +02001145 if (nbd_co_send_reply(req, &reply, request.len) < 0)
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001146 goto out;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001147 break;
1148 case NBD_CMD_WRITE:
1149 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +00001150
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001151 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +00001152 TRACE("Server is read-only, return error");
Paolo Bonzinifae69412011-09-19 16:04:36 +02001153 reply.error = EROFS;
1154 goto error_reply;
1155 }
bellard7a5ca862008-05-27 21:13:40 +00001156
Paolo Bonzinifae69412011-09-19 16:04:36 +02001157 TRACE("Writing to device");
1158
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001159 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001160 req->data, request.len / 512);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001161 if (ret < 0) {
1162 LOG("writing to file failed");
1163 reply.error = -ret;
1164 goto error_reply;
1165 }
1166
1167 if (request.type & NBD_CMD_FLAG_FUA) {
Paolo Bonzini262db382011-09-19 15:19:27 +02001168 ret = bdrv_co_flush(exp->bs);
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001169 if (ret < 0) {
Paolo Bonzinifae69412011-09-19 16:04:36 +02001170 LOG("flush failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +02001171 reply.error = -ret;
Paolo Bonzinifae69412011-09-19 16:04:36 +02001172 goto error_reply;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +02001173 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001174 }
bellard7a5ca862008-05-27 21:13:40 +00001175
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001176 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001177 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001178 }
Nick Thomasb2e3d872011-02-22 15:44:51 +00001179 break;
1180 case NBD_CMD_DISC:
1181 TRACE("Request type is DISCONNECT");
1182 errno = 0;
Paolo Bonzini262db382011-09-19 15:19:27 +02001183 goto out;
Paolo Bonzini1486d042011-10-21 13:17:14 +02001184 case NBD_CMD_FLUSH:
1185 TRACE("Request type is FLUSH");
1186
Paolo Bonzini262db382011-09-19 15:19:27 +02001187 ret = bdrv_co_flush(exp->bs);
Paolo Bonzini1486d042011-10-21 13:17:14 +02001188 if (ret < 0) {
1189 LOG("flush failed");
1190 reply.error = -ret;
1191 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001192 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001193 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001194 }
Paolo Bonzini1486d042011-10-21 13:17:14 +02001195 break;
Paolo Bonzini7a706632011-10-21 13:17:14 +02001196 case NBD_CMD_TRIM:
1197 TRACE("Request type is TRIM");
Paolo Bonzini262db382011-09-19 15:19:27 +02001198 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
1199 request.len / 512);
Paolo Bonzini7a706632011-10-21 13:17:14 +02001200 if (ret < 0) {
1201 LOG("discard failed");
1202 reply.error = -ret;
1203 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001204 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001205 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001206 }
Paolo Bonzini7a706632011-10-21 13:17:14 +02001207 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001208 default:
1209 LOG("invalid request type (%u) received", request.type);
Paolo Bonzinifae69412011-09-19 16:04:36 +02001210 invalid_request:
1211 reply.error = -EINVAL;
1212 error_reply:
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001213 if (nbd_co_send_reply(req, &reply, 0) < 0) {
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001214 goto out;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001215 }
Paolo Bonzinifae69412011-09-19 16:04:36 +02001216 break;
Nick Thomasb2e3d872011-02-22 15:44:51 +00001217 }
bellard7a5ca862008-05-27 21:13:40 +00001218
Nick Thomasb2e3d872011-02-22 15:44:51 +00001219 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +00001220
Paolo Bonzini7fe7b682012-03-05 09:10:35 +01001221done:
Paolo Bonzini262db382011-09-19 15:19:27 +02001222 nbd_request_put(req);
1223 return;
1224
Paolo Bonzinid9a73802011-09-19 14:18:33 +02001225out:
Paolo Bonzini72deddc2011-10-07 16:47:56 +02001226 nbd_request_put(req);
Paolo Bonzini262db382011-09-19 15:19:27 +02001227 nbd_client_close(client);
bellard7a5ca862008-05-27 21:13:40 +00001228}
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001229
Paolo Bonzini41996e32011-09-19 15:25:40 +02001230static int nbd_can_read(void *opaque)
1231{
1232 NBDClient *client = opaque;
1233
1234 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
1235}
1236
Paolo Bonzini1743b512011-09-19 14:33:23 +02001237static void nbd_read(void *opaque)
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001238{
Paolo Bonzini1743b512011-09-19 14:33:23 +02001239 NBDClient *client = opaque;
1240
Paolo Bonzini262db382011-09-19 15:19:27 +02001241 if (client->recv_coroutine) {
1242 qemu_coroutine_enter(client->recv_coroutine, NULL);
1243 } else {
1244 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
Paolo Bonzini1743b512011-09-19 14:33:23 +02001245 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001246}
1247
Paolo Bonzini262db382011-09-19 15:19:27 +02001248static void nbd_restart_write(void *opaque)
1249{
1250 NBDClient *client = opaque;
1251
1252 qemu_coroutine_enter(client->send_coroutine, NULL);
1253}
1254
Paolo Bonzini1743b512011-09-19 14:33:23 +02001255NBDClient *nbd_client_new(NBDExport *exp, int csock,
1256 void (*close)(NBDClient *))
1257{
1258 NBDClient *client;
Paolo Bonzini1743b512011-09-19 14:33:23 +02001259 client = g_malloc0(sizeof(NBDClient));
1260 client->refcount = 1;
1261 client->exp = exp;
1262 client->sock = csock;
Paolo Bonzini9a304d22012-08-22 15:30:31 +02001263 if (nbd_send_negotiate(client) < 0) {
1264 g_free(client);
1265 return NULL;
1266 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001267 client->close = close;
Paolo Bonzini262db382011-09-19 15:19:27 +02001268 qemu_co_mutex_init(&client->send_lock);
Paolo Bonzini41996e32011-09-19 15:25:40 +02001269 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
Paolo Bonzini2c8d9f02012-09-18 13:26:25 +02001270
Paolo Bonzini6b8c01e2012-08-23 14:57:11 +02001271 if (exp) {
1272 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1273 nbd_export_get(exp);
1274 }
Paolo Bonzini1743b512011-09-19 14:33:23 +02001275 return client;
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +02001276}