]> git.proxmox.com Git - mirror_qemu.git/blame - migration/channel-block.c
Merge tag 'pull-trivial-patches' of https://gitlab.com/mjt0k/qemu into staging
[mirror_qemu.git] / migration / channel-block.c
CommitLineData
65cf200a
DB
1/*
2 * QEMU I/O channels block driver
3 *
4 * Copyright (c) 2022 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include "qemu/osdep.h"
22#include "migration/channel-block.h"
23#include "qapi/error.h"
24#include "block/block.h"
25#include "trace.h"
26
27QIOChannelBlock *
28qio_channel_block_new(BlockDriverState *bs)
29{
30 QIOChannelBlock *ioc;
31
32 ioc = QIO_CHANNEL_BLOCK(object_new(TYPE_QIO_CHANNEL_BLOCK));
33
34 bdrv_ref(bs);
35 ioc->bs = bs;
36
37 return ioc;
38}
39
40
41static void
42qio_channel_block_finalize(Object *obj)
43{
44 QIOChannelBlock *ioc = QIO_CHANNEL_BLOCK(obj);
45
46 g_clear_pointer(&ioc->bs, bdrv_unref);
47}
48
49
50static ssize_t
51qio_channel_block_readv(QIOChannel *ioc,
52 const struct iovec *iov,
53 size_t niov,
54 int **fds,
55 size_t *nfds,
84615a19 56 int flags,
65cf200a
DB
57 Error **errp)
58{
59 QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
60 QEMUIOVector qiov;
61 int ret;
62
63 qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
64 ret = bdrv_readv_vmstate(bioc->bs, &qiov, bioc->offset);
65 if (ret < 0) {
a216ec85
FE
66 error_setg_errno(errp, -ret, "bdrv_readv_vmstate failed");
67 return -1;
65cf200a
DB
68 }
69
70 bioc->offset += qiov.size;
71 return qiov.size;
72}
73
74
75static ssize_t
76qio_channel_block_writev(QIOChannel *ioc,
77 const struct iovec *iov,
78 size_t niov,
79 int *fds,
80 size_t nfds,
81 int flags,
82 Error **errp)
83{
84 QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
85 QEMUIOVector qiov;
86 int ret;
87
88 qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
89 ret = bdrv_writev_vmstate(bioc->bs, &qiov, bioc->offset);
90 if (ret < 0) {
a216ec85
FE
91 error_setg_errno(errp, -ret, "bdrv_writev_vmstate failed");
92 return -1;
65cf200a
DB
93 }
94
95 bioc->offset += qiov.size;
96 return qiov.size;
97}
98
99
100static int
101qio_channel_block_set_blocking(QIOChannel *ioc,
102 bool enabled,
103 Error **errp)
104{
105 if (!enabled) {
106 error_setg(errp, "Non-blocking mode not supported for block devices");
107 return -1;
108 }
109 return 0;
110}
111
112
113static off_t
114qio_channel_block_seek(QIOChannel *ioc,
115 off_t offset,
116 int whence,
117 Error **errp)
118{
119 QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
120
121 switch (whence) {
122 case SEEK_SET:
123 bioc->offset = offset;
124 break;
125 case SEEK_CUR:
126 bioc->offset += whence;
127 break;
128 case SEEK_END:
129 error_setg(errp, "Size of VMstate region is unknown");
130 return (off_t)-1;
131 default:
132 g_assert_not_reached();
133 }
134
135 return bioc->offset;
136}
137
138
139static int
140qio_channel_block_close(QIOChannel *ioc,
141 Error **errp)
142{
143 QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
144 int rv = bdrv_flush(bioc->bs);
145
146 if (rv < 0) {
147 error_setg_errno(errp, -rv,
148 "Unable to flush VMState");
149 return -1;
150 }
151
152 g_clear_pointer(&bioc->bs, bdrv_unref);
153 bioc->offset = 0;
154
155 return 0;
156}
157
158
159static void
160qio_channel_block_set_aio_fd_handler(QIOChannel *ioc,
06e0f098 161 AioContext *read_ctx,
65cf200a 162 IOHandler *io_read,
06e0f098 163 AioContext *write_ctx,
65cf200a
DB
164 IOHandler *io_write,
165 void *opaque)
166{
167 /* XXX anything we can do here ? */
168}
169
170
171static void
172qio_channel_block_class_init(ObjectClass *klass,
173 void *class_data G_GNUC_UNUSED)
174{
175 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
176
177 ioc_klass->io_writev = qio_channel_block_writev;
178 ioc_klass->io_readv = qio_channel_block_readv;
179 ioc_klass->io_set_blocking = qio_channel_block_set_blocking;
180 ioc_klass->io_seek = qio_channel_block_seek;
181 ioc_klass->io_close = qio_channel_block_close;
182 ioc_klass->io_set_aio_fd_handler = qio_channel_block_set_aio_fd_handler;
183}
184
185static const TypeInfo qio_channel_block_info = {
186 .parent = TYPE_QIO_CHANNEL,
187 .name = TYPE_QIO_CHANNEL_BLOCK,
188 .instance_size = sizeof(QIOChannelBlock),
189 .instance_finalize = qio_channel_block_finalize,
190 .class_init = qio_channel_block_class_init,
191};
192
193static void
194qio_channel_block_register_types(void)
195{
196 type_register_static(&qio_channel_block_info);
197}
198
199type_init(qio_channel_block_register_types);