]> git.proxmox.com Git - mirror_qemu.git/blame - block/nbd.c
Rename target_phys_addr_t to hwaddr
[mirror_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;
7fe7b68b 154 int ret;
ae255e52
PB
155
156 if (s->reply.handle == 0) {
7fe7b68b
PB
157 /* No reply already in flight. Fetch a header. It is possible
158 * that another thread has done the same thing in parallel, so
159 * the socket is not readable anymore.
160 */
161 ret = nbd_receive_reply(s->sock, &s->reply);
162 if (ret == -EAGAIN) {
163 return;
164 }
165 if (ret < 0) {
ae255e52 166 s->reply.handle = 0;
ecda3447 167 goto fail;
ae255e52
PB
168 }
169 }
170
171 /* There's no need for a mutex on the receive side, because the
172 * handler acts as a synchronization point and ensures that only
173 * one coroutine is called until the reply finishes. */
ecda3447 174 i = HANDLE_TO_INDEX(s, s->reply.handle);
dd3e8ac4
PB
175 if (i >= MAX_NBD_REQUESTS) {
176 goto fail;
177 }
178
ecda3447
PB
179 if (s->recv_coroutine[i]) {
180 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
181 return;
182 }
183
184fail:
185 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
186 if (s->recv_coroutine[i]) {
187 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
188 }
ae255e52
PB
189 }
190}
191
192static void nbd_restart_write(void *opaque)
193{
194 BDRVNBDState *s = opaque;
ecda3447 195 qemu_coroutine_enter(s->send_coroutine, NULL);
ae255e52
PB
196}
197
198static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
2fc8ae1d 199 QEMUIOVector *qiov, int offset)
ae255e52
PB
200{
201 int rc, ret;
202
ecda3447
PB
203 qemu_co_mutex_lock(&s->send_mutex);
204 s->send_coroutine = qemu_coroutine_self();
ae255e52 205 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
bafbd6a1 206 nbd_have_request, s);
ae255e52 207 rc = nbd_send_request(s->sock, request);
2fc8ae1d
MT
208 if (rc >= 0 && qiov) {
209 ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
210 offset, request->len);
ae255e52 211 if (ret != request->len) {
185b4338 212 return -EIO;
ae255e52
PB
213 }
214 }
215 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
bafbd6a1 216 nbd_have_request, s);
ecda3447
PB
217 s->send_coroutine = NULL;
218 qemu_co_mutex_unlock(&s->send_mutex);
ae255e52
PB
219 return rc;
220}
221
222static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
223 struct nbd_reply *reply,
2fc8ae1d 224 QEMUIOVector *qiov, int offset)
ae255e52
PB
225{
226 int ret;
227
ecda3447
PB
228 /* Wait until we're woken up by the read handler. TODO: perhaps
229 * peek at the next reply and avoid yielding if it's ours? */
ae255e52
PB
230 qemu_coroutine_yield();
231 *reply = s->reply;
232 if (reply->handle != request->handle) {
233 reply->error = EIO;
234 } else {
2fc8ae1d
MT
235 if (qiov && reply->error == 0) {
236 ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
237 offset, request->len);
ae255e52
PB
238 if (ret != request->len) {
239 reply->error = EIO;
240 }
241 }
242
243 /* Tell the read handler to read another header. */
244 s->reply.handle = 0;
245 }
246}
247
248static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
249{
ecda3447
PB
250 int i = HANDLE_TO_INDEX(s, request->handle);
251 s->recv_coroutine[i] = NULL;
252 if (s->in_flight-- == MAX_NBD_REQUESTS) {
253 qemu_co_mutex_unlock(&s->free_sema);
254 }
ae255e52
PB
255}
256
33897dc7
NT
257static int nbd_establish_connection(BlockDriverState *bs)
258{
259 BDRVNBDState *s = bs->opaque;
260 int sock;
261 int ret;
262 off_t size;
263 size_t blocksize;
75818250 264
33897dc7
NT
265 if (s->host_spec[0] == '/') {
266 sock = unix_socket_outgoing(s->host_spec);
267 } else {
268 sock = tcp_socket_outgoing_spec(s->host_spec);
75818250
TS
269 }
270
33897dc7 271 /* Failed to establish connection */
fc19f8a0 272 if (sock < 0) {
33897dc7
NT
273 logout("Failed to establish connection to NBD server\n");
274 return -errno;
1d45f8b5 275 }
75818250 276
33897dc7 277 /* NBD handshake */
b90fb4b8 278 ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
33897dc7 279 &blocksize);
fc19f8a0 280 if (ret < 0) {
33897dc7
NT
281 logout("Failed to negotiate with the NBD server\n");
282 closesocket(sock);
185b4338 283 return ret;
1d45f8b5 284 }
75818250 285
ae255e52
PB
286 /* Now that we're connected, set the socket to be non-blocking and
287 * kick the reply mechanism. */
33897dc7 288 socket_set_nonblock(sock);
b3adf53a 289 qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL,
bafbd6a1 290 nbd_have_request, s);
33897dc7 291
75818250
TS
292 s->sock = sock;
293 s->size = size;
294 s->blocksize = blocksize;
295
33897dc7
NT
296 logout("Established connection with NBD server\n");
297 return 0;
298}
299
300static void nbd_teardown_connection(BlockDriverState *bs)
301{
302 BDRVNBDState *s = bs->opaque;
303 struct nbd_request request;
304
305 request.type = NBD_CMD_DISC;
33897dc7
NT
306 request.from = 0;
307 request.len = 0;
308 nbd_send_request(s->sock, &request);
309
bafbd6a1 310 qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
33897dc7
NT
311 closesocket(s->sock);
312}
313
314static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
315{
316 BDRVNBDState *s = bs->opaque;
317 int result;
318
ecda3447
PB
319 qemu_co_mutex_init(&s->send_mutex);
320 qemu_co_mutex_init(&s->free_sema);
ae255e52 321
33897dc7
NT
322 /* Pop the config into our state object. Exit if invalid. */
323 result = nbd_config(s, filename, flags);
324 if (result != 0) {
325 return result;
326 }
327
328 /* establish TCP connection, return error if it fails
329 * TODO: Configurable retry-until-timeout behaviour.
330 */
331 result = nbd_establish_connection(bs);
332
333 return result;
75818250
TS
334}
335
d9b09f13
PB
336static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
337 int nb_sectors, QEMUIOVector *qiov,
338 int offset)
75818250
TS
339{
340 BDRVNBDState *s = bs->opaque;
341 struct nbd_request request;
342 struct nbd_reply reply;
fc19f8a0 343 ssize_t ret;
75818250
TS
344
345 request.type = NBD_CMD_READ;
3a93113a 346 request.from = sector_num * 512;
75818250
TS
347 request.len = nb_sectors * 512;
348
ae255e52 349 nbd_coroutine_start(s, &request);
fc19f8a0
PB
350 ret = nbd_co_send_request(s, &request, NULL, 0);
351 if (ret < 0) {
185b4338 352 reply.error = -ret;
ae255e52 353 } else {
2fc8ae1d 354 nbd_co_receive_reply(s, &request, &reply, qiov, offset);
ae255e52
PB
355 }
356 nbd_coroutine_end(s, &request);
357 return -reply.error;
75818250 358
75818250
TS
359}
360
d9b09f13
PB
361static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
362 int nb_sectors, QEMUIOVector *qiov,
363 int offset)
75818250
TS
364{
365 BDRVNBDState *s = bs->opaque;
366 struct nbd_request request;
367 struct nbd_reply reply;
fc19f8a0 368 ssize_t ret;
75818250
TS
369
370 request.type = NBD_CMD_WRITE;
2c7989a9
PB
371 if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
372 request.type |= NBD_CMD_FLAG_FUA;
373 }
374
3a93113a 375 request.from = sector_num * 512;
75818250
TS
376 request.len = nb_sectors * 512;
377
ae255e52 378 nbd_coroutine_start(s, &request);
2fc8ae1d 379 ret = nbd_co_send_request(s, &request, qiov, offset);
fc19f8a0 380 if (ret < 0) {
185b4338 381 reply.error = -ret;
ae255e52
PB
382 } else {
383 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
384 }
385 nbd_coroutine_end(s, &request);
386 return -reply.error;
e183ef75
PB
387}
388
d9b09f13
PB
389/* qemu-nbd has a limit of slightly less than 1M per request. Try to
390 * remain aligned to 4K. */
391#define NBD_MAX_SECTORS 2040
392
393static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
394 int nb_sectors, QEMUIOVector *qiov)
395{
396 int offset = 0;
397 int ret;
398 while (nb_sectors > NBD_MAX_SECTORS) {
399 ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
400 if (ret < 0) {
401 return ret;
402 }
403 offset += NBD_MAX_SECTORS * 512;
404 sector_num += NBD_MAX_SECTORS;
405 nb_sectors -= NBD_MAX_SECTORS;
406 }
407 return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
408}
409
410static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
411 int nb_sectors, QEMUIOVector *qiov)
412{
413 int offset = 0;
414 int ret;
415 while (nb_sectors > NBD_MAX_SECTORS) {
416 ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
417 if (ret < 0) {
418 return ret;
419 }
420 offset += NBD_MAX_SECTORS * 512;
421 sector_num += NBD_MAX_SECTORS;
422 nb_sectors -= NBD_MAX_SECTORS;
423 }
424 return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
425}
426
1486d04a
PB
427static int nbd_co_flush(BlockDriverState *bs)
428{
429 BDRVNBDState *s = bs->opaque;
430 struct nbd_request request;
431 struct nbd_reply reply;
fc19f8a0 432 ssize_t ret;
1486d04a
PB
433
434 if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
435 return 0;
436 }
437
438 request.type = NBD_CMD_FLUSH;
439 if (s->nbdflags & NBD_FLAG_SEND_FUA) {
440 request.type |= NBD_CMD_FLAG_FUA;
441 }
442
443 request.from = 0;
444 request.len = 0;
445
446 nbd_coroutine_start(s, &request);
fc19f8a0
PB
447 ret = nbd_co_send_request(s, &request, NULL, 0);
448 if (ret < 0) {
185b4338 449 reply.error = -ret;
1486d04a
PB
450 } else {
451 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
452 }
453 nbd_coroutine_end(s, &request);
454 return -reply.error;
455}
456
7a706633
PB
457static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
458 int nb_sectors)
459{
460 BDRVNBDState *s = bs->opaque;
461 struct nbd_request request;
462 struct nbd_reply reply;
fc19f8a0 463 ssize_t ret;
7a706633
PB
464
465 if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
466 return 0;
467 }
468 request.type = NBD_CMD_TRIM;
469 request.from = sector_num * 512;;
470 request.len = nb_sectors * 512;
471
472 nbd_coroutine_start(s, &request);
fc19f8a0
PB
473 ret = nbd_co_send_request(s, &request, NULL, 0);
474 if (ret < 0) {
185b4338 475 reply.error = -ret;
7a706633
PB
476 } else {
477 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
478 }
479 nbd_coroutine_end(s, &request);
480 return -reply.error;
481}
482
75818250
TS
483static void nbd_close(BlockDriverState *bs)
484{
d2d979c6 485 BDRVNBDState *s = bs->opaque;
7267c094
AL
486 g_free(s->export_name);
487 g_free(s->host_spec);
d2d979c6 488
33897dc7 489 nbd_teardown_connection(bs);
75818250
TS
490}
491
492static int64_t nbd_getlength(BlockDriverState *bs)
493{
494 BDRVNBDState *s = bs->opaque;
495
496 return s->size;
497}
498
5efa9d5a 499static BlockDriver bdrv_nbd = {
1486d04a
PB
500 .format_name = "nbd",
501 .instance_size = sizeof(BDRVNBDState),
502 .bdrv_file_open = nbd_open,
503 .bdrv_co_readv = nbd_co_readv,
504 .bdrv_co_writev = nbd_co_writev,
505 .bdrv_close = nbd_close,
506 .bdrv_co_flush_to_os = nbd_co_flush,
7a706633 507 .bdrv_co_discard = nbd_co_discard,
1486d04a
PB
508 .bdrv_getlength = nbd_getlength,
509 .protocol_name = "nbd",
75818250 510};
5efa9d5a
AL
511
512static void bdrv_nbd_init(void)
513{
514 bdrv_register(&bdrv_nbd);
515}
516
517block_init(bdrv_nbd_init);