]> git.proxmox.com Git - qemu.git/blame - block/nbd.c
nbd: consistently check for <0 or >=0
[qemu.git] / block / nbd.c
CommitLineData
75818250
TS
1/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
bd5921b4 5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
75818250
TS
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
29#include "qemu-common.h"
30#include "nbd.h"
ab359cd1 31#include "block_int.h"
5efa9d5a 32#include "module.h"
33897dc7 33#include "qemu_socket.h"
75818250
TS
34
35#include <sys/types.h>
36#include <unistd.h>
75818250 37
1d45f8b5
LV
38#define EN_OPTSTR ":exportname="
39
33897dc7
NT
40/* #define DEBUG_NBD */
41
42#if defined(DEBUG_NBD)
43#define logout(fmt, ...) \
44 fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
45#else
46#define logout(fmt, ...) ((void)0)
47#endif
48
ecda3447
PB
49#define MAX_NBD_REQUESTS 16
50#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
51#define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
52
75818250
TS
53typedef struct BDRVNBDState {
54 int sock;
b90fb4b8 55 uint32_t nbdflags;
75818250
TS
56 off_t size;
57 size_t blocksize;
33897dc7
NT
58 char *export_name; /* An NBD server may export several devices */
59
ecda3447
PB
60 CoMutex send_mutex;
61 CoMutex free_sema;
62 Coroutine *send_coroutine;
63 int in_flight;
ae255e52 64
ecda3447 65 Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
ae255e52
PB
66 struct nbd_reply reply;
67
33897dc7
NT
68 /* If it begins with '/', this is a UNIX domain socket. Otherwise,
69 * it's a string of the form <hostname|ip4|\[ip6\]>:port
70 */
71 char *host_spec;
75818250
TS
72} BDRVNBDState;
73
33897dc7 74static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
75818250 75{
1d45f8b5 76 char *file;
33897dc7
NT
77 char *export_name;
78 const char *host_spec;
75818250 79 const char *unixpath;
1d45f8b5 80 int err = -EINVAL;
75818250 81
7267c094 82 file = g_strdup(filename);
1d45f8b5 83
33897dc7
NT
84 export_name = strstr(file, EN_OPTSTR);
85 if (export_name) {
86 if (export_name[strlen(EN_OPTSTR)] == 0) {
1d45f8b5
LV
87 goto out;
88 }
33897dc7
NT
89 export_name[0] = 0; /* truncate 'file' */
90 export_name += strlen(EN_OPTSTR);
7267c094 91 s->export_name = g_strdup(export_name);
1d45f8b5
LV
92 }
93
33897dc7
NT
94 /* extract the host_spec - fail if it's not nbd:... */
95 if (!strstart(file, "nbd:", &host_spec)) {
1d45f8b5
LV
96 goto out;
97 }
75818250 98
33897dc7
NT
99 /* are we a UNIX or TCP socket? */
100 if (strstart(host_spec, "unix:", &unixpath)) {
101 if (unixpath[0] != '/') { /* We demand an absolute path*/
1d45f8b5
LV
102 goto out;
103 }
7267c094 104 s->host_spec = g_strdup(unixpath);
75818250 105 } else {
7267c094 106 s->host_spec = g_strdup(host_spec);
33897dc7 107 }
75818250 108
33897dc7 109 err = 0;
75818250 110
33897dc7 111out:
7267c094 112 g_free(file);
33897dc7 113 if (err != 0) {
7267c094
AL
114 g_free(s->export_name);
115 g_free(s->host_spec);
33897dc7
NT
116 }
117 return err;
118}
1d45f8b5 119
ae255e52
PB
120static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request)
121{
ecda3447
PB
122 int i;
123
124 /* Poor man semaphore. The free_sema is locked when no other request
125 * can be accepted, and unlocked after receiving one reply. */
126 if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
127 qemu_co_mutex_lock(&s->free_sema);
128 assert(s->in_flight < MAX_NBD_REQUESTS);
129 }
130 s->in_flight++;
131
132 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
133 if (s->recv_coroutine[i] == NULL) {
134 s->recv_coroutine[i] = qemu_coroutine_self();
135 break;
136 }
137 }
138
139 assert(i < MAX_NBD_REQUESTS);
140 request->handle = INDEX_TO_HANDLE(s, i);
ae255e52
PB
141}
142
143static int nbd_have_request(void *opaque)
144{
145 BDRVNBDState *s = opaque;
146
ecda3447 147 return s->in_flight > 0;
ae255e52
PB
148}
149
150static void nbd_reply_ready(void *opaque)
151{
152 BDRVNBDState *s = opaque;
dd3e8ac4 153 uint64_t i;
ae255e52
PB
154
155 if (s->reply.handle == 0) {
156 /* No reply already in flight. Fetch a header. */
157 if (nbd_receive_reply(s->sock, &s->reply) < 0) {
158 s->reply.handle = 0;
ecda3447 159 goto fail;
ae255e52
PB
160 }
161 }
162
163 /* There's no need for a mutex on the receive side, because the
164 * handler acts as a synchronization point and ensures that only
165 * one coroutine is called until the reply finishes. */
ecda3447 166 i = HANDLE_TO_INDEX(s, s->reply.handle);
dd3e8ac4
PB
167 if (i >= MAX_NBD_REQUESTS) {
168 goto fail;
169 }
170
ecda3447
PB
171 if (s->recv_coroutine[i]) {
172 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
173 return;
174 }
175
176fail:
177 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
178 if (s->recv_coroutine[i]) {
179 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
180 }
ae255e52
PB
181 }
182}
183
184static void nbd_restart_write(void *opaque)
185{
186 BDRVNBDState *s = opaque;
ecda3447 187 qemu_coroutine_enter(s->send_coroutine, NULL);
ae255e52
PB
188}
189
190static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
191 struct iovec *iov, int offset)
192{
193 int rc, ret;
194
ecda3447
PB
195 qemu_co_mutex_lock(&s->send_mutex);
196 s->send_coroutine = qemu_coroutine_self();
ae255e52
PB
197 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
198 nbd_have_request, NULL, s);
199 rc = nbd_send_request(s->sock, request);
fc19f8a0 200 if (rc >= 0 && iov) {
ae255e52
PB
201 ret = qemu_co_sendv(s->sock, iov, request->len, offset);
202 if (ret != request->len) {
203 errno = -EIO;
204 rc = -1;
205 }
206 }
207 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
208 nbd_have_request, NULL, s);
ecda3447
PB
209 s->send_coroutine = NULL;
210 qemu_co_mutex_unlock(&s->send_mutex);
ae255e52
PB
211 return rc;
212}
213
214static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
215 struct nbd_reply *reply,
216 struct iovec *iov, int offset)
217{
218 int ret;
219
ecda3447
PB
220 /* Wait until we're woken up by the read handler. TODO: perhaps
221 * peek at the next reply and avoid yielding if it's ours? */
ae255e52
PB
222 qemu_coroutine_yield();
223 *reply = s->reply;
224 if (reply->handle != request->handle) {
225 reply->error = EIO;
226 } else {
227 if (iov && reply->error == 0) {
228 ret = qemu_co_recvv(s->sock, iov, request->len, offset);
229 if (ret != request->len) {
230 reply->error = EIO;
231 }
232 }
233
234 /* Tell the read handler to read another header. */
235 s->reply.handle = 0;
236 }
237}
238
239static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
240{
ecda3447
PB
241 int i = HANDLE_TO_INDEX(s, request->handle);
242 s->recv_coroutine[i] = NULL;
243 if (s->in_flight-- == MAX_NBD_REQUESTS) {
244 qemu_co_mutex_unlock(&s->free_sema);
245 }
ae255e52
PB
246}
247
33897dc7
NT
248static int nbd_establish_connection(BlockDriverState *bs)
249{
250 BDRVNBDState *s = bs->opaque;
251 int sock;
252 int ret;
253 off_t size;
254 size_t blocksize;
75818250 255
33897dc7
NT
256 if (s->host_spec[0] == '/') {
257 sock = unix_socket_outgoing(s->host_spec);
258 } else {
259 sock = tcp_socket_outgoing_spec(s->host_spec);
75818250
TS
260 }
261
33897dc7 262 /* Failed to establish connection */
fc19f8a0 263 if (sock < 0) {
33897dc7
NT
264 logout("Failed to establish connection to NBD server\n");
265 return -errno;
1d45f8b5 266 }
75818250 267
33897dc7 268 /* NBD handshake */
b90fb4b8 269 ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
33897dc7 270 &blocksize);
fc19f8a0 271 if (ret < 0) {
33897dc7
NT
272 logout("Failed to negotiate with the NBD server\n");
273 closesocket(sock);
274 return -errno;
1d45f8b5 275 }
75818250 276
ae255e52
PB
277 /* Now that we're connected, set the socket to be non-blocking and
278 * kick the reply mechanism. */
33897dc7 279 socket_set_nonblock(sock);
ae255e52
PB
280 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
281 nbd_have_request, NULL, s);
33897dc7 282
75818250
TS
283 s->sock = sock;
284 s->size = size;
285 s->blocksize = blocksize;
286
33897dc7
NT
287 logout("Established connection with NBD server\n");
288 return 0;
289}
290
291static void nbd_teardown_connection(BlockDriverState *bs)
292{
293 BDRVNBDState *s = bs->opaque;
294 struct nbd_request request;
295
296 request.type = NBD_CMD_DISC;
33897dc7
NT
297 request.from = 0;
298 request.len = 0;
299 nbd_send_request(s->sock, &request);
300
ae255e52 301 qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL, NULL);
33897dc7
NT
302 closesocket(s->sock);
303}
304
305static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
306{
307 BDRVNBDState *s = bs->opaque;
308 int result;
309
ecda3447
PB
310 qemu_co_mutex_init(&s->send_mutex);
311 qemu_co_mutex_init(&s->free_sema);
ae255e52 312
33897dc7
NT
313 /* Pop the config into our state object. Exit if invalid. */
314 result = nbd_config(s, filename, flags);
315 if (result != 0) {
316 return result;
317 }
318
319 /* establish TCP connection, return error if it fails
320 * TODO: Configurable retry-until-timeout behaviour.
321 */
322 result = nbd_establish_connection(bs);
323
324 return result;
75818250
TS
325}
326
d9b09f13
PB
327static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
328 int nb_sectors, QEMUIOVector *qiov,
329 int offset)
75818250
TS
330{
331 BDRVNBDState *s = bs->opaque;
332 struct nbd_request request;
333 struct nbd_reply reply;
fc19f8a0 334 ssize_t ret;
75818250
TS
335
336 request.type = NBD_CMD_READ;
3a93113a 337 request.from = sector_num * 512;
75818250
TS
338 request.len = nb_sectors * 512;
339
ae255e52 340 nbd_coroutine_start(s, &request);
fc19f8a0
PB
341 ret = nbd_co_send_request(s, &request, NULL, 0);
342 if (ret < 0) {
ae255e52
PB
343 reply.error = errno;
344 } else {
d9b09f13 345 nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset);
ae255e52
PB
346 }
347 nbd_coroutine_end(s, &request);
348 return -reply.error;
75818250 349
75818250
TS
350}
351
d9b09f13
PB
352static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
353 int nb_sectors, QEMUIOVector *qiov,
354 int offset)
75818250
TS
355{
356 BDRVNBDState *s = bs->opaque;
357 struct nbd_request request;
358 struct nbd_reply reply;
fc19f8a0 359 ssize_t ret;
75818250
TS
360
361 request.type = NBD_CMD_WRITE;
2c7989a9
PB
362 if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
363 request.type |= NBD_CMD_FLAG_FUA;
364 }
365
3a93113a 366 request.from = sector_num * 512;
75818250
TS
367 request.len = nb_sectors * 512;
368
ae255e52 369 nbd_coroutine_start(s, &request);
fc19f8a0
PB
370 ret = nbd_co_send_request(s, &request, qiov->iov, offset);
371 if (ret < 0) {
ae255e52
PB
372 reply.error = errno;
373 } else {
374 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
375 }
376 nbd_coroutine_end(s, &request);
377 return -reply.error;
e183ef75
PB
378}
379
d9b09f13
PB
380/* qemu-nbd has a limit of slightly less than 1M per request. Try to
381 * remain aligned to 4K. */
382#define NBD_MAX_SECTORS 2040
383
384static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
385 int nb_sectors, QEMUIOVector *qiov)
386{
387 int offset = 0;
388 int ret;
389 while (nb_sectors > NBD_MAX_SECTORS) {
390 ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
391 if (ret < 0) {
392 return ret;
393 }
394 offset += NBD_MAX_SECTORS * 512;
395 sector_num += NBD_MAX_SECTORS;
396 nb_sectors -= NBD_MAX_SECTORS;
397 }
398 return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
399}
400
401static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
402 int nb_sectors, QEMUIOVector *qiov)
403{
404 int offset = 0;
405 int ret;
406 while (nb_sectors > NBD_MAX_SECTORS) {
407 ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
408 if (ret < 0) {
409 return ret;
410 }
411 offset += NBD_MAX_SECTORS * 512;
412 sector_num += NBD_MAX_SECTORS;
413 nb_sectors -= NBD_MAX_SECTORS;
414 }
415 return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
416}
417
1486d04a
PB
418static int nbd_co_flush(BlockDriverState *bs)
419{
420 BDRVNBDState *s = bs->opaque;
421 struct nbd_request request;
422 struct nbd_reply reply;
fc19f8a0 423 ssize_t ret;
1486d04a
PB
424
425 if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
426 return 0;
427 }
428
429 request.type = NBD_CMD_FLUSH;
430 if (s->nbdflags & NBD_FLAG_SEND_FUA) {
431 request.type |= NBD_CMD_FLAG_FUA;
432 }
433
434 request.from = 0;
435 request.len = 0;
436
437 nbd_coroutine_start(s, &request);
fc19f8a0
PB
438 ret = nbd_co_send_request(s, &request, NULL, 0);
439 if (ret < 0) {
1486d04a
PB
440 reply.error = errno;
441 } else {
442 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
443 }
444 nbd_coroutine_end(s, &request);
445 return -reply.error;
446}
447
7a706633
PB
448static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
449 int nb_sectors)
450{
451 BDRVNBDState *s = bs->opaque;
452 struct nbd_request request;
453 struct nbd_reply reply;
fc19f8a0 454 ssize_t ret;
7a706633
PB
455
456 if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
457 return 0;
458 }
459 request.type = NBD_CMD_TRIM;
460 request.from = sector_num * 512;;
461 request.len = nb_sectors * 512;
462
463 nbd_coroutine_start(s, &request);
fc19f8a0
PB
464 ret = nbd_co_send_request(s, &request, NULL, 0);
465 if (ret < 0) {
7a706633
PB
466 reply.error = errno;
467 } else {
468 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
469 }
470 nbd_coroutine_end(s, &request);
471 return -reply.error;
472}
473
75818250
TS
474static void nbd_close(BlockDriverState *bs)
475{
d2d979c6 476 BDRVNBDState *s = bs->opaque;
7267c094
AL
477 g_free(s->export_name);
478 g_free(s->host_spec);
d2d979c6 479
33897dc7 480 nbd_teardown_connection(bs);
75818250
TS
481}
482
483static int64_t nbd_getlength(BlockDriverState *bs)
484{
485 BDRVNBDState *s = bs->opaque;
486
487 return s->size;
488}
489
5efa9d5a 490static BlockDriver bdrv_nbd = {
1486d04a
PB
491 .format_name = "nbd",
492 .instance_size = sizeof(BDRVNBDState),
493 .bdrv_file_open = nbd_open,
494 .bdrv_co_readv = nbd_co_readv,
495 .bdrv_co_writev = nbd_co_writev,
496 .bdrv_close = nbd_close,
497 .bdrv_co_flush_to_os = nbd_co_flush,
7a706633 498 .bdrv_co_discard = nbd_co_discard,
1486d04a
PB
499 .bdrv_getlength = nbd_getlength,
500 .protocol_name = "nbd",
75818250 501};
5efa9d5a
AL
502
503static void bdrv_nbd_init(void)
504{
505 bdrv_register(&bdrv_nbd);
506}
507
508block_init(bdrv_nbd_init);