]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | QIOChannelBlock * | |
28 | qio_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 | ||
41 | static void | |
42 | qio_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 | ||
50 | static ssize_t | |
51 | qio_channel_block_readv(QIOChannel *ioc, | |
52 | const struct iovec *iov, | |
53 | size_t niov, | |
54 | int **fds, | |
55 | size_t *nfds, | |
56 | Error **errp) | |
57 | { | |
58 | QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc); | |
59 | QEMUIOVector qiov; | |
60 | int ret; | |
61 | ||
62 | qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov); | |
63 | ret = bdrv_readv_vmstate(bioc->bs, &qiov, bioc->offset); | |
64 | if (ret < 0) { | |
65 | return ret; | |
66 | } | |
67 | ||
68 | bioc->offset += qiov.size; | |
69 | return qiov.size; | |
70 | } | |
71 | ||
72 | ||
73 | static ssize_t | |
74 | qio_channel_block_writev(QIOChannel *ioc, | |
75 | const struct iovec *iov, | |
76 | size_t niov, | |
77 | int *fds, | |
78 | size_t nfds, | |
79 | int flags, | |
80 | Error **errp) | |
81 | { | |
82 | QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc); | |
83 | QEMUIOVector qiov; | |
84 | int ret; | |
85 | ||
86 | qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov); | |
87 | ret = bdrv_writev_vmstate(bioc->bs, &qiov, bioc->offset); | |
88 | if (ret < 0) { | |
89 | return ret; | |
90 | } | |
91 | ||
92 | bioc->offset += qiov.size; | |
93 | return qiov.size; | |
94 | } | |
95 | ||
96 | ||
97 | static int | |
98 | qio_channel_block_set_blocking(QIOChannel *ioc, | |
99 | bool enabled, | |
100 | Error **errp) | |
101 | { | |
102 | if (!enabled) { | |
103 | error_setg(errp, "Non-blocking mode not supported for block devices"); | |
104 | return -1; | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | ||
109 | ||
110 | static off_t | |
111 | qio_channel_block_seek(QIOChannel *ioc, | |
112 | off_t offset, | |
113 | int whence, | |
114 | Error **errp) | |
115 | { | |
116 | QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc); | |
117 | ||
118 | switch (whence) { | |
119 | case SEEK_SET: | |
120 | bioc->offset = offset; | |
121 | break; | |
122 | case SEEK_CUR: | |
123 | bioc->offset += whence; | |
124 | break; | |
125 | case SEEK_END: | |
126 | error_setg(errp, "Size of VMstate region is unknown"); | |
127 | return (off_t)-1; | |
128 | default: | |
129 | g_assert_not_reached(); | |
130 | } | |
131 | ||
132 | return bioc->offset; | |
133 | } | |
134 | ||
135 | ||
136 | static int | |
137 | qio_channel_block_close(QIOChannel *ioc, | |
138 | Error **errp) | |
139 | { | |
140 | QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc); | |
141 | int rv = bdrv_flush(bioc->bs); | |
142 | ||
143 | if (rv < 0) { | |
144 | error_setg_errno(errp, -rv, | |
145 | "Unable to flush VMState"); | |
146 | return -1; | |
147 | } | |
148 | ||
149 | g_clear_pointer(&bioc->bs, bdrv_unref); | |
150 | bioc->offset = 0; | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | ||
156 | static void | |
157 | qio_channel_block_set_aio_fd_handler(QIOChannel *ioc, | |
158 | AioContext *ctx, | |
159 | IOHandler *io_read, | |
160 | IOHandler *io_write, | |
161 | void *opaque) | |
162 | { | |
163 | /* XXX anything we can do here ? */ | |
164 | } | |
165 | ||
166 | ||
167 | static void | |
168 | qio_channel_block_class_init(ObjectClass *klass, | |
169 | void *class_data G_GNUC_UNUSED) | |
170 | { | |
171 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); | |
172 | ||
173 | ioc_klass->io_writev = qio_channel_block_writev; | |
174 | ioc_klass->io_readv = qio_channel_block_readv; | |
175 | ioc_klass->io_set_blocking = qio_channel_block_set_blocking; | |
176 | ioc_klass->io_seek = qio_channel_block_seek; | |
177 | ioc_klass->io_close = qio_channel_block_close; | |
178 | ioc_klass->io_set_aio_fd_handler = qio_channel_block_set_aio_fd_handler; | |
179 | } | |
180 | ||
181 | static const TypeInfo qio_channel_block_info = { | |
182 | .parent = TYPE_QIO_CHANNEL, | |
183 | .name = TYPE_QIO_CHANNEL_BLOCK, | |
184 | .instance_size = sizeof(QIOChannelBlock), | |
185 | .instance_finalize = qio_channel_block_finalize, | |
186 | .class_init = qio_channel_block_class_init, | |
187 | }; | |
188 | ||
189 | static void | |
190 | qio_channel_block_register_types(void) | |
191 | { | |
192 | type_register_static(&qio_channel_block_info); | |
193 | } | |
194 | ||
195 | type_init(qio_channel_block_register_types); |