2 * QEMU I/O channels block driver
4 * Copyright (c) 2022 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "migration/channel-block.h"
23 #include "qapi/error.h"
24 #include "block/block.h"
28 qio_channel_block_new(BlockDriverState
*bs
)
32 ioc
= QIO_CHANNEL_BLOCK(object_new(TYPE_QIO_CHANNEL_BLOCK
));
42 qio_channel_block_finalize(Object
*obj
)
44 QIOChannelBlock
*ioc
= QIO_CHANNEL_BLOCK(obj
);
46 g_clear_pointer(&ioc
->bs
, bdrv_unref
);
51 qio_channel_block_readv(QIOChannel
*ioc
,
52 const struct iovec
*iov
,
59 QIOChannelBlock
*bioc
= QIO_CHANNEL_BLOCK(ioc
);
63 qemu_iovec_init_external(&qiov
, (struct iovec
*)iov
, niov
);
64 ret
= bdrv_readv_vmstate(bioc
->bs
, &qiov
, bioc
->offset
);
66 error_setg_errno(errp
, -ret
, "bdrv_readv_vmstate failed");
70 bioc
->offset
+= qiov
.size
;
76 qio_channel_block_writev(QIOChannel
*ioc
,
77 const struct iovec
*iov
,
84 QIOChannelBlock
*bioc
= QIO_CHANNEL_BLOCK(ioc
);
88 qemu_iovec_init_external(&qiov
, (struct iovec
*)iov
, niov
);
89 ret
= bdrv_writev_vmstate(bioc
->bs
, &qiov
, bioc
->offset
);
91 error_setg_errno(errp
, -ret
, "bdrv_writev_vmstate failed");
95 bioc
->offset
+= qiov
.size
;
101 qio_channel_block_set_blocking(QIOChannel
*ioc
,
106 error_setg(errp
, "Non-blocking mode not supported for block devices");
114 qio_channel_block_seek(QIOChannel
*ioc
,
119 QIOChannelBlock
*bioc
= QIO_CHANNEL_BLOCK(ioc
);
123 bioc
->offset
= offset
;
126 bioc
->offset
+= whence
;
129 error_setg(errp
, "Size of VMstate region is unknown");
132 g_assert_not_reached();
140 qio_channel_block_close(QIOChannel
*ioc
,
143 QIOChannelBlock
*bioc
= QIO_CHANNEL_BLOCK(ioc
);
144 int rv
= bdrv_flush(bioc
->bs
);
147 error_setg_errno(errp
, -rv
,
148 "Unable to flush VMState");
152 g_clear_pointer(&bioc
->bs
, bdrv_unref
);
160 qio_channel_block_set_aio_fd_handler(QIOChannel
*ioc
,
161 AioContext
*read_ctx
,
163 AioContext
*write_ctx
,
167 /* XXX anything we can do here ? */
172 qio_channel_block_class_init(ObjectClass
*klass
,
173 void *class_data G_GNUC_UNUSED
)
175 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
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
;
185 static 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
,
194 qio_channel_block_register_types(void)
196 type_register_static(&qio_channel_block_info
);
199 type_init(qio_channel_block_register_types
);