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