2 * Copyright (c) 2021-2023 Oracle and/or its affiliates.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
8 #include "qemu/osdep.h"
9 #include "exec/ramblock.h"
10 #include "qemu/cutils.h"
11 #include "qemu/error-report.h"
12 #include "qapi/error.h"
15 #include "migration.h"
16 #include "io/channel-file.h"
17 #include "io/channel-socket.h"
18 #include "io/channel-util.h"
22 #define OFFSET_OPTION ",offset="
24 static struct FileOutgoingArgs
{
28 /* Remove the offset option from @filespec and return it in @offsetp. */
30 int file_parse_offset(char *filespec
, uint64_t *offsetp
, Error
**errp
)
32 char *option
= strstr(filespec
, OFFSET_OPTION
);
37 option
+= sizeof(OFFSET_OPTION
) - 1;
38 ret
= qemu_strtosz(option
, NULL
, offsetp
);
40 error_setg_errno(errp
, -ret
, "file URI has bad offset %s", option
);
47 void file_cleanup_outgoing_migration(void)
49 g_free(outgoing_args
.fname
);
50 outgoing_args
.fname
= NULL
;
53 static void file_enable_direct_io(int *flags
)
58 /* it should have been rejected when setting the parameter */
59 g_assert_not_reached();
63 bool file_send_channel_create(gpointer opaque
, Error
**errp
)
69 if (migrate_direct_io()) {
71 * Enable O_DIRECT for the secondary channels. These are used
72 * for sending ram pages and writes should be guaranteed to be
73 * aligned to at least page size.
75 file_enable_direct_io(&flags
);
78 ioc
= qio_channel_file_new_path(outgoing_args
.fname
, flags
, 0, errp
);
84 multifd_channel_connect(opaque
, QIO_CHANNEL(ioc
));
88 * File channel creation is synchronous. However posting this
89 * semaphore here is simpler than adding a special case.
91 multifd_send_channel_created();
96 void file_start_outgoing_migration(MigrationState
*s
,
97 FileMigrationArgs
*file_args
, Error
**errp
)
99 g_autoptr(QIOChannelFile
) fioc
= NULL
;
100 g_autofree
char *filename
= g_strdup(file_args
->filename
);
101 uint64_t offset
= file_args
->offset
;
104 trace_migration_file_outgoing(filename
);
106 fioc
= qio_channel_file_new_path(filename
, O_CREAT
| O_WRONLY
, 0600, errp
);
111 if (ftruncate(fioc
->fd
, offset
)) {
112 error_setg_errno(errp
, errno
,
113 "failed to truncate migration file to offset %" PRIx64
,
115 object_unref(OBJECT(fioc
));
119 outgoing_args
.fname
= g_strdup(filename
);
121 ioc
= QIO_CHANNEL(fioc
);
122 if (offset
&& qio_channel_io_seek(ioc
, offset
, SEEK_SET
, errp
) < 0) {
123 object_unref(OBJECT(fioc
));
126 qio_channel_set_name(ioc
, "migration-file-outgoing");
127 migration_channel_connect(s
, ioc
, NULL
, NULL
);
130 static gboolean
file_accept_incoming_migration(QIOChannel
*ioc
,
131 GIOCondition condition
,
134 migration_channel_process_incoming(ioc
);
135 object_unref(OBJECT(ioc
));
136 return G_SOURCE_REMOVE
;
139 static void file_create_incoming_channels(QIOChannel
*ioc
, char *filename
,
143 g_autofree QIOChannel
**iocs
= NULL
;
144 int flags
= O_RDONLY
;
146 if (migrate_multifd()) {
147 channels
+= migrate_multifd_channels();
148 if (migrate_direct_io()) {
149 file_enable_direct_io(&flags
);
153 iocs
= g_new0(QIOChannel
*, channels
);
156 for (i
= 1; i
< channels
; i
++) {
157 QIOChannelFile
*fioc
= qio_channel_file_new_path(filename
, flags
, 0, errp
);
161 object_unref(iocs
[--i
]);
166 iocs
[i
] = QIO_CHANNEL(fioc
);
169 for (i
= 0; i
< channels
; i
++) {
170 qio_channel_set_name(iocs
[i
], "migration-file-incoming");
171 qio_channel_add_watch_full(iocs
[i
], G_IO_IN
,
172 file_accept_incoming_migration
,
174 g_main_context_get_thread_default());
178 void file_start_incoming_migration(FileMigrationArgs
*file_args
, Error
**errp
)
180 g_autofree
char *filename
= g_strdup(file_args
->filename
);
181 QIOChannelFile
*fioc
= NULL
;
182 uint64_t offset
= file_args
->offset
;
184 trace_migration_file_incoming(filename
);
186 fioc
= qio_channel_file_new_path(filename
, O_RDONLY
, 0, errp
);
192 qio_channel_io_seek(QIO_CHANNEL(fioc
), offset
, SEEK_SET
, errp
) < 0) {
193 object_unref(OBJECT(fioc
));
197 file_create_incoming_channels(QIO_CHANNEL(fioc
), filename
, errp
);
200 int file_write_ramblock_iov(QIOChannel
*ioc
, const struct iovec
*iov
,
201 int niov
, RAMBlock
*block
, Error
**errp
)
204 int i
, slice_idx
, slice_num
;
205 uintptr_t base
, next
, offset
;
212 * If the iov array doesn't have contiguous elements, we need to
213 * split it in slices because we only have one file offset for the
214 * whole iov. Do this here so callers don't need to break the iov
217 for (i
= 0; i
< niov
; i
++, slice_num
++) {
218 base
= (uintptr_t) iov
[i
].iov_base
;
221 len
= iov
[i
].iov_len
;
222 next
= (uintptr_t) iov
[i
+ 1].iov_base
;
224 if (base
+ len
== next
) {
230 * Use the offset of the first element of the segment that
233 offset
= (uintptr_t) iov
[slice_idx
].iov_base
- (uintptr_t) block
->host
;
234 if (offset
>= block
->used_length
) {
235 error_setg(errp
, "offset %" PRIxPTR
236 "outside of ramblock %s range", offset
, block
->idstr
);
241 ret
= qio_channel_pwritev(ioc
, &iov
[slice_idx
], slice_num
,
242 block
->pages_offset
+ offset
, errp
);
247 slice_idx
+= slice_num
;
251 return (ret
< 0) ? ret
: 0;
254 int multifd_file_recv_data(MultiFDRecvParams
*p
, Error
**errp
)
256 MultiFDRecvData
*data
= p
->data
;
259 ret
= qio_channel_pread(p
->c
, (char *) data
->opaque
,
260 data
->size
, data
->file_offset
, errp
);
261 if (ret
!= data
->size
) {
263 "multifd recv (%u): read 0x%zx, expected 0x%zx",
264 p
->id
, ret
, data
->size
);