vhdx: Fix "log that needs to be replayed" error message
[qemu/cris-port.git] / block / nbd-client.c
blobb7fd17a1156c79efc5c65e57bea034a20f758592
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
29 #include "nbd-client.h"
30 #include "qemu/sockets.h"
32 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
33 #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
35 static void nbd_recv_coroutines_enter_all(NbdClientSession *s)
37 int i;
39 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
40 if (s->recv_coroutine[i]) {
41 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
46 static void nbd_teardown_connection(BlockDriverState *bs)
48 NbdClientSession *client = nbd_get_client_session(bs);
50 /* finish any pending coroutines */
51 shutdown(client->sock, 2);
52 nbd_recv_coroutines_enter_all(client);
54 nbd_client_detach_aio_context(bs);
55 closesocket(client->sock);
56 client->sock = -1;
59 static void nbd_reply_ready(void *opaque)
61 BlockDriverState *bs = opaque;
62 NbdClientSession *s = nbd_get_client_session(bs);
63 uint64_t i;
64 int ret;
66 if (s->reply.handle == 0) {
67 /* No reply already in flight. Fetch a header. It is possible
68 * that another thread has done the same thing in parallel, so
69 * the socket is not readable anymore.
71 ret = nbd_receive_reply(s->sock, &s->reply);
72 if (ret == -EAGAIN) {
73 return;
75 if (ret < 0) {
76 s->reply.handle = 0;
77 goto fail;
81 /* There's no need for a mutex on the receive side, because the
82 * handler acts as a synchronization point and ensures that only
83 * one coroutine is called until the reply finishes. */
84 i = HANDLE_TO_INDEX(s, s->reply.handle);
85 if (i >= MAX_NBD_REQUESTS) {
86 goto fail;
89 if (s->recv_coroutine[i]) {
90 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
91 return;
94 fail:
95 nbd_teardown_connection(bs);
98 static void nbd_restart_write(void *opaque)
100 BlockDriverState *bs = opaque;
102 qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine, NULL);
105 static int nbd_co_send_request(BlockDriverState *bs,
106 struct nbd_request *request,
107 QEMUIOVector *qiov, int offset)
109 NbdClientSession *s = nbd_get_client_session(bs);
110 AioContext *aio_context;
111 int rc, ret, i;
113 qemu_co_mutex_lock(&s->send_mutex);
115 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
116 if (s->recv_coroutine[i] == NULL) {
117 s->recv_coroutine[i] = qemu_coroutine_self();
118 break;
122 assert(i < MAX_NBD_REQUESTS);
123 request->handle = INDEX_TO_HANDLE(s, i);
124 s->send_coroutine = qemu_coroutine_self();
125 aio_context = bdrv_get_aio_context(bs);
127 aio_set_fd_handler(aio_context, s->sock, false,
128 nbd_reply_ready, nbd_restart_write, bs);
129 if (qiov) {
130 if (!s->is_unix) {
131 socket_set_cork(s->sock, 1);
133 rc = nbd_send_request(s->sock, request);
134 if (rc >= 0) {
135 ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
136 offset, request->len);
137 if (ret != request->len) {
138 rc = -EIO;
141 if (!s->is_unix) {
142 socket_set_cork(s->sock, 0);
144 } else {
145 rc = nbd_send_request(s->sock, request);
147 aio_set_fd_handler(aio_context, s->sock, false,
148 nbd_reply_ready, NULL, bs);
149 s->send_coroutine = NULL;
150 qemu_co_mutex_unlock(&s->send_mutex);
151 return rc;
154 static void nbd_co_receive_reply(NbdClientSession *s,
155 struct nbd_request *request, struct nbd_reply *reply,
156 QEMUIOVector *qiov, int offset)
158 int ret;
160 /* Wait until we're woken up by the read handler. TODO: perhaps
161 * peek at the next reply and avoid yielding if it's ours? */
162 qemu_coroutine_yield();
163 *reply = s->reply;
164 if (reply->handle != request->handle) {
165 reply->error = EIO;
166 } else {
167 if (qiov && reply->error == 0) {
168 ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
169 offset, request->len);
170 if (ret != request->len) {
171 reply->error = EIO;
175 /* Tell the read handler to read another header. */
176 s->reply.handle = 0;
180 static void nbd_coroutine_start(NbdClientSession *s,
181 struct nbd_request *request)
183 /* Poor man semaphore. The free_sema is locked when no other request
184 * can be accepted, and unlocked after receiving one reply. */
185 if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
186 qemu_co_mutex_lock(&s->free_sema);
187 assert(s->in_flight < MAX_NBD_REQUESTS);
189 s->in_flight++;
191 /* s->recv_coroutine[i] is set as soon as we get the send_lock. */
194 static void nbd_coroutine_end(NbdClientSession *s,
195 struct nbd_request *request)
197 int i = HANDLE_TO_INDEX(s, request->handle);
198 s->recv_coroutine[i] = NULL;
199 if (s->in_flight-- == MAX_NBD_REQUESTS) {
200 qemu_co_mutex_unlock(&s->free_sema);
204 static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
205 int nb_sectors, QEMUIOVector *qiov,
206 int offset)
208 NbdClientSession *client = nbd_get_client_session(bs);
209 struct nbd_request request = { .type = NBD_CMD_READ };
210 struct nbd_reply reply;
211 ssize_t ret;
213 request.from = sector_num * 512;
214 request.len = nb_sectors * 512;
216 nbd_coroutine_start(client, &request);
217 ret = nbd_co_send_request(bs, &request, NULL, 0);
218 if (ret < 0) {
219 reply.error = -ret;
220 } else {
221 nbd_co_receive_reply(client, &request, &reply, qiov, offset);
223 nbd_coroutine_end(client, &request);
224 return -reply.error;
228 static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
229 int nb_sectors, QEMUIOVector *qiov,
230 int offset)
232 NbdClientSession *client = nbd_get_client_session(bs);
233 struct nbd_request request = { .type = NBD_CMD_WRITE };
234 struct nbd_reply reply;
235 ssize_t ret;
237 if (!bdrv_enable_write_cache(bs) &&
238 (client->nbdflags & NBD_FLAG_SEND_FUA)) {
239 request.type |= NBD_CMD_FLAG_FUA;
242 request.from = sector_num * 512;
243 request.len = nb_sectors * 512;
245 nbd_coroutine_start(client, &request);
246 ret = nbd_co_send_request(bs, &request, qiov, offset);
247 if (ret < 0) {
248 reply.error = -ret;
249 } else {
250 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
252 nbd_coroutine_end(client, &request);
253 return -reply.error;
256 /* qemu-nbd has a limit of slightly less than 1M per request. Try to
257 * remain aligned to 4K. */
258 #define NBD_MAX_SECTORS 2040
260 int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
261 int nb_sectors, QEMUIOVector *qiov)
263 int offset = 0;
264 int ret;
265 while (nb_sectors > NBD_MAX_SECTORS) {
266 ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
267 if (ret < 0) {
268 return ret;
270 offset += NBD_MAX_SECTORS * 512;
271 sector_num += NBD_MAX_SECTORS;
272 nb_sectors -= NBD_MAX_SECTORS;
274 return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
277 int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
278 int nb_sectors, QEMUIOVector *qiov)
280 int offset = 0;
281 int ret;
282 while (nb_sectors > NBD_MAX_SECTORS) {
283 ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
284 if (ret < 0) {
285 return ret;
287 offset += NBD_MAX_SECTORS * 512;
288 sector_num += NBD_MAX_SECTORS;
289 nb_sectors -= NBD_MAX_SECTORS;
291 return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
294 int nbd_client_co_flush(BlockDriverState *bs)
296 NbdClientSession *client = nbd_get_client_session(bs);
297 struct nbd_request request = { .type = NBD_CMD_FLUSH };
298 struct nbd_reply reply;
299 ssize_t ret;
301 if (!(client->nbdflags & NBD_FLAG_SEND_FLUSH)) {
302 return 0;
305 if (client->nbdflags & NBD_FLAG_SEND_FUA) {
306 request.type |= NBD_CMD_FLAG_FUA;
309 request.from = 0;
310 request.len = 0;
312 nbd_coroutine_start(client, &request);
313 ret = nbd_co_send_request(bs, &request, NULL, 0);
314 if (ret < 0) {
315 reply.error = -ret;
316 } else {
317 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
319 nbd_coroutine_end(client, &request);
320 return -reply.error;
323 int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
324 int nb_sectors)
326 NbdClientSession *client = nbd_get_client_session(bs);
327 struct nbd_request request = { .type = NBD_CMD_TRIM };
328 struct nbd_reply reply;
329 ssize_t ret;
331 if (!(client->nbdflags & NBD_FLAG_SEND_TRIM)) {
332 return 0;
334 request.from = sector_num * 512;
335 request.len = nb_sectors * 512;
337 nbd_coroutine_start(client, &request);
338 ret = nbd_co_send_request(bs, &request, NULL, 0);
339 if (ret < 0) {
340 reply.error = -ret;
341 } else {
342 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
344 nbd_coroutine_end(client, &request);
345 return -reply.error;
349 void nbd_client_detach_aio_context(BlockDriverState *bs)
351 aio_set_fd_handler(bdrv_get_aio_context(bs),
352 nbd_get_client_session(bs)->sock,
353 false, NULL, NULL, NULL);
356 void nbd_client_attach_aio_context(BlockDriverState *bs,
357 AioContext *new_context)
359 aio_set_fd_handler(new_context, nbd_get_client_session(bs)->sock,
360 false, nbd_reply_ready, NULL, bs);
363 void nbd_client_close(BlockDriverState *bs)
365 NbdClientSession *client = nbd_get_client_session(bs);
366 struct nbd_request request = {
367 .type = NBD_CMD_DISC,
368 .from = 0,
369 .len = 0
372 if (client->sock == -1) {
373 return;
376 nbd_send_request(client->sock, &request);
378 nbd_teardown_connection(bs);
381 int nbd_client_init(BlockDriverState *bs, int sock, const char *export,
382 Error **errp)
384 NbdClientSession *client = nbd_get_client_session(bs);
385 int ret;
387 /* NBD handshake */
388 logout("session init %s\n", export);
389 qemu_set_block(sock);
390 ret = nbd_receive_negotiate(sock, export,
391 &client->nbdflags, &client->size, errp);
392 if (ret < 0) {
393 logout("Failed to negotiate with the NBD server\n");
394 closesocket(sock);
395 return ret;
398 qemu_co_mutex_init(&client->send_mutex);
399 qemu_co_mutex_init(&client->free_sema);
400 client->sock = sock;
402 /* Now that we're connected, set the socket to be non-blocking and
403 * kick the reply mechanism. */
404 qemu_set_nonblock(sock);
405 nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs));
407 logout("Established connection with NBD server\n");
408 return 0;