]> git.proxmox.com Git - mirror_qemu.git/blame - io/channel-file.c
block/copy-before-write: implement cbw-timeout option
[mirror_qemu.git] / io / channel-file.c
CommitLineData
d6e48869
DB
1/*
2 * QEMU I/O channels files driver
3 *
4 * Copyright (c) 2015 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
c8198bd5 9 * version 2.1 of the License, or (at your option) any later version.
d6e48869
DB
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
cae9fc56 21#include "qemu/osdep.h"
d6e48869
DB
22#include "io/channel-file.h"
23#include "io/channel-watch.h"
da34e65c 24#include "qapi/error.h"
0b8fa32f 25#include "qemu/module.h"
d6e48869
DB
26#include "qemu/sockets.h"
27#include "trace.h"
28
29QIOChannelFile *
30qio_channel_file_new_fd(int fd)
31{
32 QIOChannelFile *ioc;
33
34 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
35
36 ioc->fd = fd;
37
38 trace_qio_channel_file_new_fd(ioc, fd);
39
40 return ioc;
41}
42
43
44QIOChannelFile *
45qio_channel_file_new_path(const char *path,
46 int flags,
47 mode_t mode,
48 Error **errp)
49{
50 QIOChannelFile *ioc;
51
52 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
53
448058aa 54 ioc->fd = qemu_open_old(path, flags, mode);
d6e48869
DB
55 if (ioc->fd < 0) {
56 object_unref(OBJECT(ioc));
57 error_setg_errno(errp, errno,
58 "Unable to open %s", path);
59 return NULL;
60 }
61
62 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
63
64 return ioc;
65}
66
67
68static void qio_channel_file_init(Object *obj)
69{
70 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
71 ioc->fd = -1;
72}
73
74static void qio_channel_file_finalize(Object *obj)
75{
76 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
77 if (ioc->fd != -1) {
b8f244b1 78 qemu_close(ioc->fd);
d6e48869
DB
79 ioc->fd = -1;
80 }
81}
82
83
84static ssize_t qio_channel_file_readv(QIOChannel *ioc,
85 const struct iovec *iov,
86 size_t niov,
87 int **fds,
88 size_t *nfds,
89 Error **errp)
90{
91 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
92 ssize_t ret;
93
94 retry:
95 ret = readv(fioc->fd, iov, niov);
96 if (ret < 0) {
30fd3e27 97 if (errno == EAGAIN) {
d6e48869
DB
98 return QIO_CHANNEL_ERR_BLOCK;
99 }
100 if (errno == EINTR) {
101 goto retry;
102 }
103
104 error_setg_errno(errp, errno,
105 "Unable to read from file");
106 return -1;
107 }
108
109 return ret;
110}
111
112static ssize_t qio_channel_file_writev(QIOChannel *ioc,
113 const struct iovec *iov,
114 size_t niov,
115 int *fds,
116 size_t nfds,
b88651cb 117 int flags,
d6e48869
DB
118 Error **errp)
119{
120 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
121 ssize_t ret;
122
123 retry:
124 ret = writev(fioc->fd, iov, niov);
125 if (ret <= 0) {
30fd3e27 126 if (errno == EAGAIN) {
d6e48869
DB
127 return QIO_CHANNEL_ERR_BLOCK;
128 }
129 if (errno == EINTR) {
130 goto retry;
131 }
132 error_setg_errno(errp, errno,
133 "Unable to write to file");
134 return -1;
135 }
136 return ret;
137}
138
139static int qio_channel_file_set_blocking(QIOChannel *ioc,
140 bool enabled,
141 Error **errp)
142{
17fc1245
MAL
143#ifdef WIN32
144 /* not implemented */
145 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
146 return -1;
147#else
d6e48869
DB
148 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
149
17fc1245
MAL
150 if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) {
151 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
152 return -1;
d6e48869
DB
153 }
154 return 0;
17fc1245 155#endif
d6e48869
DB
156}
157
158
159static off_t qio_channel_file_seek(QIOChannel *ioc,
160 off_t offset,
161 int whence,
162 Error **errp)
163{
164 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
165 off_t ret;
166
167 ret = lseek(fioc->fd, offset, whence);
168 if (ret == (off_t)-1) {
169 error_setg_errno(errp, errno,
170 "Unable to seek to offset %lld whence %d in file",
171 (long long int)offset, whence);
172 return -1;
173 }
174 return ret;
175}
176
177
178static int qio_channel_file_close(QIOChannel *ioc,
179 Error **errp)
180{
181 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
182
b8f244b1 183 if (qemu_close(fioc->fd) < 0) {
d6e48869
DB
184 error_setg_errno(errp, errno,
185 "Unable to close file");
186 return -1;
187 }
a2565df1 188 fioc->fd = -1;
d6e48869
DB
189 return 0;
190}
191
192
bf88c124
PB
193static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
194 AioContext *ctx,
195 IOHandler *io_read,
196 IOHandler *io_write,
197 void *opaque)
198{
199 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
826cc324
SH
200 aio_set_fd_handler(ctx, fioc->fd, false, io_read, io_write,
201 NULL, NULL, opaque);
bf88c124
PB
202}
203
d6e48869
DB
204static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
205 GIOCondition condition)
206{
207 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
208 return qio_channel_create_fd_watch(ioc,
209 fioc->fd,
210 condition);
211}
212
213static void qio_channel_file_class_init(ObjectClass *klass,
214 void *class_data G_GNUC_UNUSED)
215{
216 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
217
218 ioc_klass->io_writev = qio_channel_file_writev;
219 ioc_klass->io_readv = qio_channel_file_readv;
220 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
221 ioc_klass->io_seek = qio_channel_file_seek;
222 ioc_klass->io_close = qio_channel_file_close;
223 ioc_klass->io_create_watch = qio_channel_file_create_watch;
bf88c124 224 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
d6e48869
DB
225}
226
227static const TypeInfo qio_channel_file_info = {
228 .parent = TYPE_QIO_CHANNEL,
229 .name = TYPE_QIO_CHANNEL_FILE,
230 .instance_size = sizeof(QIOChannelFile),
231 .instance_init = qio_channel_file_init,
232 .instance_finalize = qio_channel_file_finalize,
233 .class_init = qio_channel_file_class_init,
234};
235
236static void qio_channel_file_register_types(void)
237{
238 type_register_static(&qio_channel_file_info);
239}
240
241type_init(qio_channel_file_register_types);