]> git.proxmox.com Git - mirror_qemu.git/blob - nbd/common.c
nbd: add errp parameter to nbd_wr_syncv()
[mirror_qemu.git] / nbd / common.c
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device Common Code
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "nbd-internal.h"
22
23 /* nbd_wr_syncv
24 * The function may be called from coroutine or from non-coroutine context.
25 * When called from non-coroutine context @ioc must be in blocking mode.
26 */
27 ssize_t nbd_wr_syncv(QIOChannel *ioc,
28 struct iovec *iov,
29 size_t niov,
30 size_t length,
31 bool do_read,
32 Error **errp)
33 {
34 ssize_t done = 0;
35 struct iovec *local_iov = g_new(struct iovec, niov);
36 struct iovec *local_iov_head = local_iov;
37 unsigned int nlocal_iov = niov;
38
39 nlocal_iov = iov_copy(local_iov, nlocal_iov, iov, niov, 0, length);
40
41 while (nlocal_iov > 0) {
42 ssize_t len;
43 if (do_read) {
44 len = qio_channel_readv(ioc, local_iov, nlocal_iov, errp);
45 } else {
46 len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
47 }
48 if (len == QIO_CHANNEL_ERR_BLOCK) {
49 /* errp should not be set */
50 assert(qemu_in_coroutine());
51 qio_channel_yield(ioc, do_read ? G_IO_IN : G_IO_OUT);
52 continue;
53 }
54 if (len < 0) {
55 done = -EIO;
56 goto cleanup;
57 }
58
59 if (do_read && len == 0) {
60 break;
61 }
62
63 iov_discard_front(&local_iov, &nlocal_iov, len);
64 done += len;
65 }
66
67 cleanup:
68 g_free(local_iov_head);
69 return done;
70 }
71
72
73 void nbd_tls_handshake(QIOTask *task,
74 void *opaque)
75 {
76 struct NBDTLSHandshakeData *data = opaque;
77
78 if (qio_task_propagate_error(task, &data->error)) {
79 TRACE("TLS failed %s", error_get_pretty(data->error));
80 }
81 data->complete = true;
82 g_main_loop_quit(data->loop);
83 }