]>
Commit | Line | Data |
---|---|---|
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" | |
737e150e | 30 | #include "block/nbd.h" |
1de7afc9 | 31 | #include "qemu/uri.h" |
737e150e | 32 | #include "block/block_int.h" |
1de7afc9 PB |
33 | #include "qemu/module.h" |
34 | #include "qemu/sockets.h" | |
f53a1feb KW |
35 | #include "qapi/qmp/qjson.h" |
36 | #include "qapi/qmp/qint.h" | |
75818250 TS |
37 | |
38 | #include <sys/types.h> | |
39 | #include <unistd.h> | |
75818250 | 40 | |
1d45f8b5 LV |
41 | #define EN_OPTSTR ":exportname=" |
42 | ||
33897dc7 NT |
43 | /* #define DEBUG_NBD */ |
44 | ||
45 | #if defined(DEBUG_NBD) | |
46 | #define logout(fmt, ...) \ | |
47 | fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__) | |
48 | #else | |
49 | #define logout(fmt, ...) ((void)0) | |
50 | #endif | |
51 | ||
ecda3447 PB |
52 | #define MAX_NBD_REQUESTS 16 |
53 | #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs)) | |
54 | #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs)) | |
55 | ||
75818250 TS |
56 | typedef struct BDRVNBDState { |
57 | int sock; | |
b90fb4b8 | 58 | uint32_t nbdflags; |
75818250 TS |
59 | off_t size; |
60 | size_t blocksize; | |
33897dc7 | 61 | |
ecda3447 PB |
62 | CoMutex send_mutex; |
63 | CoMutex free_sema; | |
64 | Coroutine *send_coroutine; | |
65 | int in_flight; | |
ae255e52 | 66 | |
ecda3447 | 67 | Coroutine *recv_coroutine[MAX_NBD_REQUESTS]; |
ae255e52 PB |
68 | struct nbd_reply reply; |
69 | ||
f53a1feb KW |
70 | bool is_unix; |
71 | QemuOpts *socket_opts; | |
f17c90be | 72 | |
d04b0bbb | 73 | char *export_name; /* An NBD server may export several devices */ |
75818250 TS |
74 | } BDRVNBDState; |
75 | ||
f53a1feb | 76 | static int nbd_parse_uri(const char *filename, QDict *options) |
1d7d2a9d PB |
77 | { |
78 | URI *uri; | |
79 | const char *p; | |
80 | QueryParams *qp = NULL; | |
81 | int ret = 0; | |
f53a1feb | 82 | bool is_unix; |
1d7d2a9d PB |
83 | |
84 | uri = uri_parse(filename); | |
85 | if (!uri) { | |
86 | return -EINVAL; | |
87 | } | |
88 | ||
89 | /* transport */ | |
90 | if (!strcmp(uri->scheme, "nbd")) { | |
f53a1feb | 91 | is_unix = false; |
1d7d2a9d | 92 | } else if (!strcmp(uri->scheme, "nbd+tcp")) { |
f53a1feb | 93 | is_unix = false; |
1d7d2a9d | 94 | } else if (!strcmp(uri->scheme, "nbd+unix")) { |
f53a1feb | 95 | is_unix = true; |
1d7d2a9d PB |
96 | } else { |
97 | ret = -EINVAL; | |
98 | goto out; | |
99 | } | |
100 | ||
101 | p = uri->path ? uri->path : "/"; | |
102 | p += strspn(p, "/"); | |
103 | if (p[0]) { | |
f53a1feb | 104 | qdict_put(options, "export", qstring_from_str(p)); |
1d7d2a9d PB |
105 | } |
106 | ||
107 | qp = query_params_parse(uri->query); | |
f53a1feb | 108 | if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { |
1d7d2a9d PB |
109 | ret = -EINVAL; |
110 | goto out; | |
111 | } | |
112 | ||
f53a1feb | 113 | if (is_unix) { |
1d7d2a9d PB |
114 | /* nbd+unix:///export?socket=path */ |
115 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
116 | ret = -EINVAL; | |
117 | goto out; | |
118 | } | |
f53a1feb | 119 | qdict_put(options, "path", qstring_from_str(qp->p[0].value)); |
1d7d2a9d PB |
120 | } else { |
121 | /* nbd[+tcp]://host:port/export */ | |
f53a1feb KW |
122 | char *port_str; |
123 | ||
1d7d2a9d PB |
124 | if (!uri->server) { |
125 | ret = -EINVAL; | |
126 | goto out; | |
127 | } | |
128 | if (!uri->port) { | |
129 | uri->port = NBD_DEFAULT_PORT; | |
130 | } | |
f17c90be | 131 | |
f53a1feb KW |
132 | port_str = g_strdup_printf("%d", uri->port); |
133 | qdict_put(options, "host", qstring_from_str(uri->server)); | |
134 | qdict_put(options, "port", qstring_from_str(port_str)); | |
135 | g_free(port_str); | |
1d7d2a9d PB |
136 | } |
137 | ||
138 | out: | |
139 | if (qp) { | |
140 | query_params_free(qp); | |
141 | } | |
142 | uri_free(uri); | |
143 | return ret; | |
144 | } | |
145 | ||
f53a1feb | 146 | static int nbd_parse_filename(const char *filename, QDict *options) |
75818250 | 147 | { |
1d45f8b5 | 148 | char *file; |
33897dc7 NT |
149 | char *export_name; |
150 | const char *host_spec; | |
75818250 | 151 | const char *unixpath; |
f53a1feb | 152 | int ret = -EINVAL; |
f17c90be | 153 | Error *local_err = NULL; |
75818250 | 154 | |
1d7d2a9d | 155 | if (strstr(filename, "://")) { |
f53a1feb | 156 | return nbd_parse_uri(filename, options); |
1d7d2a9d PB |
157 | } |
158 | ||
7267c094 | 159 | file = g_strdup(filename); |
1d45f8b5 | 160 | |
33897dc7 NT |
161 | export_name = strstr(file, EN_OPTSTR); |
162 | if (export_name) { | |
163 | if (export_name[strlen(EN_OPTSTR)] == 0) { | |
1d45f8b5 LV |
164 | goto out; |
165 | } | |
33897dc7 NT |
166 | export_name[0] = 0; /* truncate 'file' */ |
167 | export_name += strlen(EN_OPTSTR); | |
f53a1feb KW |
168 | |
169 | qdict_put(options, "export", qstring_from_str(export_name)); | |
1d45f8b5 LV |
170 | } |
171 | ||
33897dc7 NT |
172 | /* extract the host_spec - fail if it's not nbd:... */ |
173 | if (!strstart(file, "nbd:", &host_spec)) { | |
1d45f8b5 LV |
174 | goto out; |
175 | } | |
75818250 | 176 | |
f53a1feb KW |
177 | if (!*host_spec) { |
178 | ret = 1; | |
179 | goto out; | |
180 | } | |
181 | ||
33897dc7 NT |
182 | /* are we a UNIX or TCP socket? */ |
183 | if (strstart(host_spec, "unix:", &unixpath)) { | |
f53a1feb | 184 | qdict_put(options, "path", qstring_from_str(unixpath)); |
75818250 | 185 | } else { |
f53a1feb KW |
186 | InetSocketAddress *addr = NULL; |
187 | ||
188 | addr = inet_parse(host_spec, &local_err); | |
f17c90be KW |
189 | if (local_err != NULL) { |
190 | qerror_report_err(local_err); | |
191 | error_free(local_err); | |
192 | goto out; | |
193 | } | |
75818250 | 194 | |
f53a1feb KW |
195 | qdict_put(options, "host", qstring_from_str(addr->host)); |
196 | qdict_put(options, "port", qstring_from_str(addr->port)); | |
197 | qapi_free_InetSocketAddress(addr); | |
198 | } | |
75818250 | 199 | |
f53a1feb | 200 | ret = 1; |
33897dc7 | 201 | out: |
7267c094 | 202 | g_free(file); |
f53a1feb KW |
203 | return ret; |
204 | } | |
205 | ||
206 | static int nbd_config(BDRVNBDState *s, QDict *options) | |
207 | { | |
208 | Error *local_err = NULL; | |
209 | ||
210 | if (qdict_haskey(options, "path")) { | |
211 | s->is_unix = true; | |
212 | } else if (qdict_haskey(options, "host")) { | |
213 | s->is_unix = false; | |
214 | } else { | |
215 | return -EINVAL; | |
33897dc7 | 216 | } |
f53a1feb KW |
217 | |
218 | s->socket_opts = qemu_opts_create_nofail(&socket_optslist); | |
219 | ||
220 | qemu_opts_absorb_qdict(s->socket_opts, options, &local_err); | |
221 | if (error_is_set(&local_err)) { | |
222 | qerror_report_err(local_err); | |
223 | error_free(local_err); | |
224 | return -EINVAL; | |
225 | } | |
226 | ||
227 | s->export_name = g_strdup(qdict_get_try_str(options, "export")); | |
228 | if (s->export_name) { | |
229 | qdict_del(options, "export"); | |
230 | } | |
231 | ||
232 | return 0; | |
33897dc7 | 233 | } |
1d45f8b5 | 234 | |
f53a1feb | 235 | |
ae255e52 PB |
236 | static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request) |
237 | { | |
ecda3447 PB |
238 | int i; |
239 | ||
240 | /* Poor man semaphore. The free_sema is locked when no other request | |
241 | * can be accepted, and unlocked after receiving one reply. */ | |
242 | if (s->in_flight >= MAX_NBD_REQUESTS - 1) { | |
243 | qemu_co_mutex_lock(&s->free_sema); | |
244 | assert(s->in_flight < MAX_NBD_REQUESTS); | |
245 | } | |
246 | s->in_flight++; | |
247 | ||
248 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
249 | if (s->recv_coroutine[i] == NULL) { | |
250 | s->recv_coroutine[i] = qemu_coroutine_self(); | |
251 | break; | |
252 | } | |
253 | } | |
254 | ||
255 | assert(i < MAX_NBD_REQUESTS); | |
256 | request->handle = INDEX_TO_HANDLE(s, i); | |
ae255e52 PB |
257 | } |
258 | ||
259 | static int nbd_have_request(void *opaque) | |
260 | { | |
261 | BDRVNBDState *s = opaque; | |
262 | ||
ecda3447 | 263 | return s->in_flight > 0; |
ae255e52 PB |
264 | } |
265 | ||
266 | static void nbd_reply_ready(void *opaque) | |
267 | { | |
268 | BDRVNBDState *s = opaque; | |
dd3e8ac4 | 269 | uint64_t i; |
7fe7b68b | 270 | int ret; |
ae255e52 PB |
271 | |
272 | if (s->reply.handle == 0) { | |
7fe7b68b PB |
273 | /* No reply already in flight. Fetch a header. It is possible |
274 | * that another thread has done the same thing in parallel, so | |
275 | * the socket is not readable anymore. | |
276 | */ | |
277 | ret = nbd_receive_reply(s->sock, &s->reply); | |
278 | if (ret == -EAGAIN) { | |
279 | return; | |
280 | } | |
281 | if (ret < 0) { | |
ae255e52 | 282 | s->reply.handle = 0; |
ecda3447 | 283 | goto fail; |
ae255e52 PB |
284 | } |
285 | } | |
286 | ||
287 | /* There's no need for a mutex on the receive side, because the | |
288 | * handler acts as a synchronization point and ensures that only | |
289 | * one coroutine is called until the reply finishes. */ | |
ecda3447 | 290 | i = HANDLE_TO_INDEX(s, s->reply.handle); |
dd3e8ac4 PB |
291 | if (i >= MAX_NBD_REQUESTS) { |
292 | goto fail; | |
293 | } | |
294 | ||
ecda3447 PB |
295 | if (s->recv_coroutine[i]) { |
296 | qemu_coroutine_enter(s->recv_coroutine[i], NULL); | |
297 | return; | |
298 | } | |
299 | ||
300 | fail: | |
301 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
302 | if (s->recv_coroutine[i]) { | |
303 | qemu_coroutine_enter(s->recv_coroutine[i], NULL); | |
304 | } | |
ae255e52 PB |
305 | } |
306 | } | |
307 | ||
308 | static void nbd_restart_write(void *opaque) | |
309 | { | |
310 | BDRVNBDState *s = opaque; | |
ecda3447 | 311 | qemu_coroutine_enter(s->send_coroutine, NULL); |
ae255e52 PB |
312 | } |
313 | ||
314 | static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request, | |
2fc8ae1d | 315 | QEMUIOVector *qiov, int offset) |
ae255e52 PB |
316 | { |
317 | int rc, ret; | |
318 | ||
ecda3447 PB |
319 | qemu_co_mutex_lock(&s->send_mutex); |
320 | s->send_coroutine = qemu_coroutine_self(); | |
ae255e52 | 321 | qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write, |
bafbd6a1 | 322 | nbd_have_request, s); |
ae255e52 | 323 | rc = nbd_send_request(s->sock, request); |
2fc8ae1d MT |
324 | if (rc >= 0 && qiov) { |
325 | ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov, | |
326 | offset, request->len); | |
ae255e52 | 327 | if (ret != request->len) { |
185b4338 | 328 | return -EIO; |
ae255e52 PB |
329 | } |
330 | } | |
331 | qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL, | |
bafbd6a1 | 332 | nbd_have_request, s); |
ecda3447 PB |
333 | s->send_coroutine = NULL; |
334 | qemu_co_mutex_unlock(&s->send_mutex); | |
ae255e52 PB |
335 | return rc; |
336 | } | |
337 | ||
338 | static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request, | |
339 | struct nbd_reply *reply, | |
2fc8ae1d | 340 | QEMUIOVector *qiov, int offset) |
ae255e52 PB |
341 | { |
342 | int ret; | |
343 | ||
ecda3447 PB |
344 | /* Wait until we're woken up by the read handler. TODO: perhaps |
345 | * peek at the next reply and avoid yielding if it's ours? */ | |
ae255e52 PB |
346 | qemu_coroutine_yield(); |
347 | *reply = s->reply; | |
348 | if (reply->handle != request->handle) { | |
349 | reply->error = EIO; | |
350 | } else { | |
2fc8ae1d MT |
351 | if (qiov && reply->error == 0) { |
352 | ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov, | |
353 | offset, request->len); | |
ae255e52 PB |
354 | if (ret != request->len) { |
355 | reply->error = EIO; | |
356 | } | |
357 | } | |
358 | ||
359 | /* Tell the read handler to read another header. */ | |
360 | s->reply.handle = 0; | |
361 | } | |
362 | } | |
363 | ||
364 | static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request) | |
365 | { | |
ecda3447 PB |
366 | int i = HANDLE_TO_INDEX(s, request->handle); |
367 | s->recv_coroutine[i] = NULL; | |
368 | if (s->in_flight-- == MAX_NBD_REQUESTS) { | |
369 | qemu_co_mutex_unlock(&s->free_sema); | |
370 | } | |
ae255e52 PB |
371 | } |
372 | ||
33897dc7 NT |
373 | static int nbd_establish_connection(BlockDriverState *bs) |
374 | { | |
375 | BDRVNBDState *s = bs->opaque; | |
376 | int sock; | |
377 | int ret; | |
378 | off_t size; | |
379 | size_t blocksize; | |
75818250 | 380 | |
d04b0bbb | 381 | if (s->is_unix) { |
f53a1feb | 382 | sock = unix_socket_outgoing(qemu_opt_get(s->socket_opts, "path")); |
33897dc7 | 383 | } else { |
f53a1feb | 384 | sock = tcp_socket_outgoing_opts(s->socket_opts); |
75818250 TS |
385 | } |
386 | ||
33897dc7 | 387 | /* Failed to establish connection */ |
fc19f8a0 | 388 | if (sock < 0) { |
33897dc7 NT |
389 | logout("Failed to establish connection to NBD server\n"); |
390 | return -errno; | |
1d45f8b5 | 391 | } |
75818250 | 392 | |
33897dc7 | 393 | /* NBD handshake */ |
b90fb4b8 | 394 | ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size, |
33897dc7 | 395 | &blocksize); |
fc19f8a0 | 396 | if (ret < 0) { |
33897dc7 NT |
397 | logout("Failed to negotiate with the NBD server\n"); |
398 | closesocket(sock); | |
185b4338 | 399 | return ret; |
1d45f8b5 | 400 | } |
75818250 | 401 | |
ae255e52 PB |
402 | /* Now that we're connected, set the socket to be non-blocking and |
403 | * kick the reply mechanism. */ | |
33897dc7 | 404 | socket_set_nonblock(sock); |
b3adf53a | 405 | qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL, |
bafbd6a1 | 406 | nbd_have_request, s); |
33897dc7 | 407 | |
75818250 TS |
408 | s->sock = sock; |
409 | s->size = size; | |
410 | s->blocksize = blocksize; | |
411 | ||
33897dc7 NT |
412 | logout("Established connection with NBD server\n"); |
413 | return 0; | |
414 | } | |
415 | ||
416 | static void nbd_teardown_connection(BlockDriverState *bs) | |
417 | { | |
418 | BDRVNBDState *s = bs->opaque; | |
419 | struct nbd_request request; | |
420 | ||
421 | request.type = NBD_CMD_DISC; | |
33897dc7 NT |
422 | request.from = 0; |
423 | request.len = 0; | |
424 | nbd_send_request(s->sock, &request); | |
425 | ||
bafbd6a1 | 426 | qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL); |
33897dc7 NT |
427 | closesocket(s->sock); |
428 | } | |
429 | ||
787e4a85 KW |
430 | static int nbd_open(BlockDriverState *bs, const char* filename, |
431 | QDict *options, int flags) | |
33897dc7 NT |
432 | { |
433 | BDRVNBDState *s = bs->opaque; | |
434 | int result; | |
435 | ||
ecda3447 PB |
436 | qemu_co_mutex_init(&s->send_mutex); |
437 | qemu_co_mutex_init(&s->free_sema); | |
ae255e52 | 438 | |
33897dc7 | 439 | /* Pop the config into our state object. Exit if invalid. */ |
f53a1feb KW |
440 | result = nbd_parse_filename(filename, options); |
441 | if (result < 0) { | |
442 | return result; | |
443 | } | |
444 | ||
445 | result = nbd_config(s, options); | |
33897dc7 NT |
446 | if (result != 0) { |
447 | return result; | |
448 | } | |
449 | ||
450 | /* establish TCP connection, return error if it fails | |
451 | * TODO: Configurable retry-until-timeout behaviour. | |
452 | */ | |
453 | result = nbd_establish_connection(bs); | |
454 | ||
455 | return result; | |
75818250 TS |
456 | } |
457 | ||
d9b09f13 PB |
458 | static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num, |
459 | int nb_sectors, QEMUIOVector *qiov, | |
460 | int offset) | |
75818250 TS |
461 | { |
462 | BDRVNBDState *s = bs->opaque; | |
463 | struct nbd_request request; | |
464 | struct nbd_reply reply; | |
fc19f8a0 | 465 | ssize_t ret; |
75818250 TS |
466 | |
467 | request.type = NBD_CMD_READ; | |
3a93113a | 468 | request.from = sector_num * 512; |
75818250 TS |
469 | request.len = nb_sectors * 512; |
470 | ||
ae255e52 | 471 | nbd_coroutine_start(s, &request); |
fc19f8a0 PB |
472 | ret = nbd_co_send_request(s, &request, NULL, 0); |
473 | if (ret < 0) { | |
185b4338 | 474 | reply.error = -ret; |
ae255e52 | 475 | } else { |
2fc8ae1d | 476 | nbd_co_receive_reply(s, &request, &reply, qiov, offset); |
ae255e52 PB |
477 | } |
478 | nbd_coroutine_end(s, &request); | |
479 | return -reply.error; | |
75818250 | 480 | |
75818250 TS |
481 | } |
482 | ||
d9b09f13 PB |
483 | static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num, |
484 | int nb_sectors, QEMUIOVector *qiov, | |
485 | int offset) | |
75818250 TS |
486 | { |
487 | BDRVNBDState *s = bs->opaque; | |
488 | struct nbd_request request; | |
489 | struct nbd_reply reply; | |
fc19f8a0 | 490 | ssize_t ret; |
75818250 TS |
491 | |
492 | request.type = NBD_CMD_WRITE; | |
2c7989a9 PB |
493 | if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) { |
494 | request.type |= NBD_CMD_FLAG_FUA; | |
495 | } | |
496 | ||
3a93113a | 497 | request.from = sector_num * 512; |
75818250 TS |
498 | request.len = nb_sectors * 512; |
499 | ||
ae255e52 | 500 | nbd_coroutine_start(s, &request); |
2fc8ae1d | 501 | ret = nbd_co_send_request(s, &request, qiov, offset); |
fc19f8a0 | 502 | if (ret < 0) { |
185b4338 | 503 | reply.error = -ret; |
ae255e52 PB |
504 | } else { |
505 | nbd_co_receive_reply(s, &request, &reply, NULL, 0); | |
506 | } | |
507 | nbd_coroutine_end(s, &request); | |
508 | return -reply.error; | |
e183ef75 PB |
509 | } |
510 | ||
d9b09f13 PB |
511 | /* qemu-nbd has a limit of slightly less than 1M per request. Try to |
512 | * remain aligned to 4K. */ | |
513 | #define NBD_MAX_SECTORS 2040 | |
514 | ||
515 | static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, | |
516 | int nb_sectors, QEMUIOVector *qiov) | |
517 | { | |
518 | int offset = 0; | |
519 | int ret; | |
520 | while (nb_sectors > NBD_MAX_SECTORS) { | |
521 | ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); | |
522 | if (ret < 0) { | |
523 | return ret; | |
524 | } | |
525 | offset += NBD_MAX_SECTORS * 512; | |
526 | sector_num += NBD_MAX_SECTORS; | |
527 | nb_sectors -= NBD_MAX_SECTORS; | |
528 | } | |
529 | return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset); | |
530 | } | |
531 | ||
532 | static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num, | |
533 | int nb_sectors, QEMUIOVector *qiov) | |
534 | { | |
535 | int offset = 0; | |
536 | int ret; | |
537 | while (nb_sectors > NBD_MAX_SECTORS) { | |
538 | ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); | |
539 | if (ret < 0) { | |
540 | return ret; | |
541 | } | |
542 | offset += NBD_MAX_SECTORS * 512; | |
543 | sector_num += NBD_MAX_SECTORS; | |
544 | nb_sectors -= NBD_MAX_SECTORS; | |
545 | } | |
546 | return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset); | |
547 | } | |
548 | ||
1486d04a PB |
549 | static int nbd_co_flush(BlockDriverState *bs) |
550 | { | |
551 | BDRVNBDState *s = bs->opaque; | |
552 | struct nbd_request request; | |
553 | struct nbd_reply reply; | |
fc19f8a0 | 554 | ssize_t ret; |
1486d04a PB |
555 | |
556 | if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) { | |
557 | return 0; | |
558 | } | |
559 | ||
560 | request.type = NBD_CMD_FLUSH; | |
561 | if (s->nbdflags & NBD_FLAG_SEND_FUA) { | |
562 | request.type |= NBD_CMD_FLAG_FUA; | |
563 | } | |
564 | ||
565 | request.from = 0; | |
566 | request.len = 0; | |
567 | ||
568 | nbd_coroutine_start(s, &request); | |
fc19f8a0 PB |
569 | ret = nbd_co_send_request(s, &request, NULL, 0); |
570 | if (ret < 0) { | |
185b4338 | 571 | reply.error = -ret; |
1486d04a PB |
572 | } else { |
573 | nbd_co_receive_reply(s, &request, &reply, NULL, 0); | |
574 | } | |
575 | nbd_coroutine_end(s, &request); | |
576 | return -reply.error; | |
577 | } | |
578 | ||
7a706633 PB |
579 | static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num, |
580 | int nb_sectors) | |
581 | { | |
582 | BDRVNBDState *s = bs->opaque; | |
583 | struct nbd_request request; | |
584 | struct nbd_reply reply; | |
fc19f8a0 | 585 | ssize_t ret; |
7a706633 PB |
586 | |
587 | if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) { | |
588 | return 0; | |
589 | } | |
590 | request.type = NBD_CMD_TRIM; | |
591 | request.from = sector_num * 512;; | |
592 | request.len = nb_sectors * 512; | |
593 | ||
594 | nbd_coroutine_start(s, &request); | |
fc19f8a0 PB |
595 | ret = nbd_co_send_request(s, &request, NULL, 0); |
596 | if (ret < 0) { | |
185b4338 | 597 | reply.error = -ret; |
7a706633 PB |
598 | } else { |
599 | nbd_co_receive_reply(s, &request, &reply, NULL, 0); | |
600 | } | |
601 | nbd_coroutine_end(s, &request); | |
602 | return -reply.error; | |
603 | } | |
604 | ||
75818250 TS |
605 | static void nbd_close(BlockDriverState *bs) |
606 | { | |
d2d979c6 | 607 | BDRVNBDState *s = bs->opaque; |
7267c094 | 608 | g_free(s->export_name); |
f53a1feb | 609 | qemu_opts_del(s->socket_opts); |
d2d979c6 | 610 | |
33897dc7 | 611 | nbd_teardown_connection(bs); |
75818250 TS |
612 | } |
613 | ||
614 | static int64_t nbd_getlength(BlockDriverState *bs) | |
615 | { | |
616 | BDRVNBDState *s = bs->opaque; | |
617 | ||
618 | return s->size; | |
619 | } | |
620 | ||
5efa9d5a | 621 | static BlockDriver bdrv_nbd = { |
1486d04a | 622 | .format_name = "nbd", |
1d7d2a9d PB |
623 | .protocol_name = "nbd", |
624 | .instance_size = sizeof(BDRVNBDState), | |
625 | .bdrv_file_open = nbd_open, | |
626 | .bdrv_co_readv = nbd_co_readv, | |
627 | .bdrv_co_writev = nbd_co_writev, | |
628 | .bdrv_close = nbd_close, | |
629 | .bdrv_co_flush_to_os = nbd_co_flush, | |
630 | .bdrv_co_discard = nbd_co_discard, | |
631 | .bdrv_getlength = nbd_getlength, | |
632 | }; | |
633 | ||
634 | static BlockDriver bdrv_nbd_tcp = { | |
635 | .format_name = "nbd", | |
636 | .protocol_name = "nbd+tcp", | |
637 | .instance_size = sizeof(BDRVNBDState), | |
638 | .bdrv_file_open = nbd_open, | |
639 | .bdrv_co_readv = nbd_co_readv, | |
640 | .bdrv_co_writev = nbd_co_writev, | |
641 | .bdrv_close = nbd_close, | |
642 | .bdrv_co_flush_to_os = nbd_co_flush, | |
643 | .bdrv_co_discard = nbd_co_discard, | |
644 | .bdrv_getlength = nbd_getlength, | |
645 | }; | |
646 | ||
647 | static BlockDriver bdrv_nbd_unix = { | |
648 | .format_name = "nbd", | |
649 | .protocol_name = "nbd+unix", | |
1486d04a PB |
650 | .instance_size = sizeof(BDRVNBDState), |
651 | .bdrv_file_open = nbd_open, | |
652 | .bdrv_co_readv = nbd_co_readv, | |
653 | .bdrv_co_writev = nbd_co_writev, | |
654 | .bdrv_close = nbd_close, | |
655 | .bdrv_co_flush_to_os = nbd_co_flush, | |
7a706633 | 656 | .bdrv_co_discard = nbd_co_discard, |
1486d04a | 657 | .bdrv_getlength = nbd_getlength, |
75818250 | 658 | }; |
5efa9d5a AL |
659 | |
660 | static void bdrv_nbd_init(void) | |
661 | { | |
662 | bdrv_register(&bdrv_nbd); | |
1d7d2a9d PB |
663 | bdrv_register(&bdrv_nbd_tcp); |
664 | bdrv_register(&bdrv_nbd_unix); | |
5efa9d5a AL |
665 | } |
666 | ||
667 | block_init(bdrv_nbd_init); |