]> git.proxmox.com Git - mirror_qemu.git/blame - block/copy-on-read.c
hw/char: riscv_htif: Drop {to, from}host_size in HTIFState
[mirror_qemu.git] / block / copy-on-read.c
CommitLineData
6c6f24fd
HR
1/*
2 * Copy-on-read filter block driver
3 *
4 * Copyright (c) 2018 Red Hat, Inc.
5 *
6 * Author:
7 * Max Reitz <mreitz@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 or
12 * (at your option) version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "qemu/osdep.h"
24#include "block/block_int.h"
0b8fa32f 25#include "qemu/module.h"
16e09a21 26#include "qapi/error.h"
e4c8fddd 27#include "qapi/qmp/qdict.h"
16e09a21
AS
28#include "block/copy-on-read.h"
29
30
31typedef struct BDRVStateCOR {
e4c8fddd
AS
32 BlockDriverState *bottom_bs;
33 bool chain_frozen;
16e09a21 34} BDRVStateCOR;
6c6f24fd
HR
35
36
37static int cor_open(BlockDriverState *bs, QDict *options, int flags,
38 Error **errp)
39{
e4c8fddd 40 BlockDriverState *bottom_bs = NULL;
16e09a21 41 BDRVStateCOR *state = bs->opaque;
e4c8fddd
AS
42 /* Find a bottom node name, if any */
43 const char *bottom_node = qdict_get_try_str(options, "bottom");
83930780 44 int ret;
16e09a21 45
83930780
VSO
46 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
47 if (ret < 0) {
48 return ret;
6c6f24fd
HR
49 }
50
e275458b
AS
51 bs->supported_read_flags = BDRV_REQ_PREFETCH;
52
228345bf 53 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
80f5c33f 54 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
6c6f24fd 55
228345bf 56 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
80f5c33f
KW
57 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
58 bs->file->bs->supported_zero_flags);
6c6f24fd 59
e4c8fddd
AS
60 if (bottom_node) {
61 bottom_bs = bdrv_find_node(bottom_node);
62 if (!bottom_bs) {
63 error_setg(errp, "Bottom node '%s' not found", bottom_node);
64 qdict_del(options, "bottom");
65 return -EINVAL;
66 }
67 qdict_del(options, "bottom");
68
69 if (!bottom_bs->drv) {
70 error_setg(errp, "Bottom node '%s' not opened", bottom_node);
71 return -EINVAL;
72 }
73
74 if (bottom_bs->drv->is_filter) {
75 error_setg(errp, "Bottom node '%s' is a filter", bottom_node);
76 return -EINVAL;
77 }
78
79 if (bdrv_freeze_backing_chain(bs, bottom_bs, errp) < 0) {
80 return -EINVAL;
81 }
82 state->chain_frozen = true;
83
84 /*
85 * We do freeze the chain, so it shouldn't be removed. Still, storing a
86 * pointer worth bdrv_ref().
87 */
88 bdrv_ref(bottom_bs);
89 }
e4c8fddd 90 state->bottom_bs = bottom_bs;
16e09a21
AS
91
92 /*
93 * We don't need to call bdrv_child_refresh_perms() now as the permissions
94 * will be updated later when the filter node gets its parent.
95 */
96
6c6f24fd
HR
97 return 0;
98}
99
100
6c6f24fd
HR
101#define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
102 | BLK_PERM_WRITE \
103 | BLK_PERM_RESIZE)
104#define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
105
106static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
bf8e925e 107 BdrvChildRole role,
6c6f24fd
HR
108 BlockReopenQueue *reopen_queue,
109 uint64_t perm, uint64_t shared,
110 uint64_t *nperm, uint64_t *nshared)
111{
2b23f286
KW
112 *nperm = perm & PERM_PASSTHROUGH;
113 *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
6c6f24fd 114
2b23f286
KW
115 /* We must not request write permissions for an inactive node, the child
116 * cannot provide it. */
117 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
118 *nperm |= BLK_PERM_WRITE_UNCHANGED;
119 }
6c6f24fd
HR
120}
121
122
123static int64_t cor_getlength(BlockDriverState *bs)
124{
125 return bdrv_getlength(bs->file->bs);
126}
127
128
1252e03b 129static int coroutine_fn cor_co_preadv_part(BlockDriverState *bs,
f7ef38dd 130 int64_t offset, int64_t bytes,
1252e03b
AS
131 QEMUIOVector *qiov,
132 size_t qiov_offset,
f7ef38dd 133 BdrvRequestFlags flags)
6c6f24fd 134{
e4c8fddd
AS
135 int64_t n;
136 int local_flags;
137 int ret;
138 BDRVStateCOR *state = bs->opaque;
139
140 if (!state->bottom_bs) {
141 return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset,
142 flags | BDRV_REQ_COPY_ON_READ);
143 }
144
145 while (bytes) {
146 local_flags = flags;
147
148 /* In case of failure, try to copy-on-read anyway */
149 ret = bdrv_is_allocated(bs->file->bs, offset, bytes, &n);
150 if (ret <= 0) {
151 ret = bdrv_is_allocated_above(bdrv_backing_chain_next(bs->file->bs),
152 state->bottom_bs, true, offset,
153 n, &n);
154 if (ret > 0 || ret < 0) {
155 local_flags |= BDRV_REQ_COPY_ON_READ;
156 }
157 /* Finish earlier if the end of a backing file has been reached */
158 if (n == 0) {
159 break;
160 }
161 }
162
e275458b
AS
163 /* Skip if neither read nor write are needed */
164 if ((local_flags & (BDRV_REQ_PREFETCH | BDRV_REQ_COPY_ON_READ)) !=
165 BDRV_REQ_PREFETCH) {
166 ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset,
167 local_flags);
168 if (ret < 0) {
169 return ret;
170 }
e4c8fddd
AS
171 }
172
173 offset += n;
174 qiov_offset += n;
175 bytes -= n;
176 }
177
178 return 0;
6c6f24fd
HR
179}
180
181
1252e03b 182static int coroutine_fn cor_co_pwritev_part(BlockDriverState *bs,
e75abeda
VSO
183 int64_t offset,
184 int64_t bytes,
1252e03b 185 QEMUIOVector *qiov,
e75abeda
VSO
186 size_t qiov_offset,
187 BdrvRequestFlags flags)
6c6f24fd 188{
1252e03b
AS
189 return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
190 flags);
6c6f24fd
HR
191}
192
193
194static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs,
f34b2bcf 195 int64_t offset, int64_t bytes,
6c6f24fd
HR
196 BdrvRequestFlags flags)
197{
198 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
199}
200
201
202static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,
0c802287 203 int64_t offset, int64_t bytes)
6c6f24fd 204{
0b9fd3f4 205 return bdrv_co_pdiscard(bs->file, offset, bytes);
6c6f24fd
HR
206}
207
208
4935e8be 209static int coroutine_fn cor_co_pwritev_compressed(BlockDriverState *bs,
e75abeda
VSO
210 int64_t offset,
211 int64_t bytes,
4935e8be
HR
212 QEMUIOVector *qiov)
213{
214 return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
215 BDRV_REQ_WRITE_COMPRESSED);
216}
217
218
6c6f24fd
HR
219static void cor_eject(BlockDriverState *bs, bool eject_flag)
220{
221 bdrv_eject(bs->file->bs, eject_flag);
222}
223
224
225static void cor_lock_medium(BlockDriverState *bs, bool locked)
226{
227 bdrv_lock_medium(bs->file->bs, locked);
228}
229
230
e4c8fddd
AS
231static void cor_close(BlockDriverState *bs)
232{
233 BDRVStateCOR *s = bs->opaque;
234
235 if (s->chain_frozen) {
236 s->chain_frozen = false;
237 bdrv_unfreeze_backing_chain(bs, s->bottom_bs);
238 }
239
240 bdrv_unref(s->bottom_bs);
241}
242
243
782b9d06 244static BlockDriver bdrv_copy_on_read = {
6c6f24fd 245 .format_name = "copy-on-read",
16e09a21 246 .instance_size = sizeof(BDRVStateCOR),
6c6f24fd
HR
247
248 .bdrv_open = cor_open,
e4c8fddd 249 .bdrv_close = cor_close,
6c6f24fd
HR
250 .bdrv_child_perm = cor_child_perm,
251
252 .bdrv_getlength = cor_getlength,
6c6f24fd 253
1252e03b
AS
254 .bdrv_co_preadv_part = cor_co_preadv_part,
255 .bdrv_co_pwritev_part = cor_co_pwritev_part,
6c6f24fd
HR
256 .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes,
257 .bdrv_co_pdiscard = cor_co_pdiscard,
4935e8be 258 .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed,
6c6f24fd
HR
259
260 .bdrv_eject = cor_eject,
261 .bdrv_lock_medium = cor_lock_medium,
262
6c6f24fd
HR
263 .has_variable_length = true,
264 .is_filter = true,
265};
266
16e09a21
AS
267
268void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
269{
16e09a21
AS
270 BDRVStateCOR *s = cor_filter_bs->opaque;
271
e4c8fddd
AS
272 /* unfreeze, as otherwise bdrv_replace_node() will fail */
273 if (s->chain_frozen) {
274 s->chain_frozen = false;
275 bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs);
276 }
bcc8584c 277 bdrv_drop_filter(cor_filter_bs, &error_abort);
16e09a21
AS
278 bdrv_unref(cor_filter_bs);
279}
280
281
6c6f24fd
HR
282static void bdrv_copy_on_read_init(void)
283{
284 bdrv_register(&bdrv_copy_on_read);
285}
286
287block_init(bdrv_copy_on_read_init);