2 * Copy-on-read filter block driver
4 * Copyright (c) 2018 Red Hat, Inc.
7 * Max Reitz <mreitz@redhat.com>
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.
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.
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/>.
23 #include "qemu/osdep.h"
24 #include "block/block-io.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
27 #include "qapi/error.h"
28 #include "qapi/qmp/qdict.h"
29 #include "block/copy-on-read.h"
32 typedef struct BDRVStateCOR
{
33 BlockDriverState
*bottom_bs
;
38 static int cor_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
41 BlockDriverState
*bottom_bs
= NULL
;
42 BDRVStateCOR
*state
= bs
->opaque
;
43 /* Find a bottom node name, if any */
44 const char *bottom_node
= qdict_get_try_str(options
, "bottom");
47 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
52 bs
->supported_read_flags
= BDRV_REQ_PREFETCH
;
54 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
|
55 (BDRV_REQ_FUA
& bs
->file
->bs
->supported_write_flags
);
57 bs
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
|
58 ((BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
) &
59 bs
->file
->bs
->supported_zero_flags
);
62 bottom_bs
= bdrv_find_node(bottom_node
);
64 error_setg(errp
, "Bottom node '%s' not found", bottom_node
);
65 qdict_del(options
, "bottom");
68 qdict_del(options
, "bottom");
70 if (!bottom_bs
->drv
) {
71 error_setg(errp
, "Bottom node '%s' not opened", bottom_node
);
75 if (bottom_bs
->drv
->is_filter
) {
76 error_setg(errp
, "Bottom node '%s' is a filter", bottom_node
);
80 if (bdrv_freeze_backing_chain(bs
, bottom_bs
, errp
) < 0) {
83 state
->chain_frozen
= true;
86 * We do freeze the chain, so it shouldn't be removed. Still, storing a
87 * pointer worth bdrv_ref().
91 state
->bottom_bs
= bottom_bs
;
94 * We don't need to call bdrv_child_refresh_perms() now as the permissions
95 * will be updated later when the filter node gets its parent.
102 #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
105 #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
107 static void cor_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
109 BlockReopenQueue
*reopen_queue
,
110 uint64_t perm
, uint64_t shared
,
111 uint64_t *nperm
, uint64_t *nshared
)
113 *nperm
= perm
& PERM_PASSTHROUGH
;
114 *nshared
= (shared
& PERM_PASSTHROUGH
) | PERM_UNCHANGED
;
116 /* We must not request write permissions for an inactive node, the child
117 * cannot provide it. */
118 if (!(bs
->open_flags
& BDRV_O_INACTIVE
)) {
119 *nperm
|= BLK_PERM_WRITE_UNCHANGED
;
124 static int64_t cor_getlength(BlockDriverState
*bs
)
126 return bdrv_getlength(bs
->file
->bs
);
130 static int coroutine_fn
cor_co_preadv_part(BlockDriverState
*bs
,
131 int64_t offset
, int64_t bytes
,
134 BdrvRequestFlags flags
)
139 BDRVStateCOR
*state
= bs
->opaque
;
141 if (!state
->bottom_bs
) {
142 return bdrv_co_preadv_part(bs
->file
, offset
, bytes
, qiov
, qiov_offset
,
143 flags
| BDRV_REQ_COPY_ON_READ
);
149 /* In case of failure, try to copy-on-read anyway */
150 ret
= bdrv_is_allocated(bs
->file
->bs
, offset
, bytes
, &n
);
152 ret
= bdrv_is_allocated_above(bdrv_backing_chain_next(bs
->file
->bs
),
153 state
->bottom_bs
, true, offset
,
155 if (ret
> 0 || ret
< 0) {
156 local_flags
|= BDRV_REQ_COPY_ON_READ
;
158 /* Finish earlier if the end of a backing file has been reached */
164 /* Skip if neither read nor write are needed */
165 if ((local_flags
& (BDRV_REQ_PREFETCH
| BDRV_REQ_COPY_ON_READ
)) !=
167 ret
= bdrv_co_preadv_part(bs
->file
, offset
, n
, qiov
, qiov_offset
,
183 static int coroutine_fn
cor_co_pwritev_part(BlockDriverState
*bs
,
188 BdrvRequestFlags flags
)
190 return bdrv_co_pwritev_part(bs
->file
, offset
, bytes
, qiov
, qiov_offset
,
195 static int coroutine_fn
cor_co_pwrite_zeroes(BlockDriverState
*bs
,
196 int64_t offset
, int64_t bytes
,
197 BdrvRequestFlags flags
)
199 return bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
203 static int coroutine_fn
cor_co_pdiscard(BlockDriverState
*bs
,
204 int64_t offset
, int64_t bytes
)
206 return bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
210 static int coroutine_fn
cor_co_pwritev_compressed(BlockDriverState
*bs
,
215 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
,
216 BDRV_REQ_WRITE_COMPRESSED
);
220 static void cor_eject(BlockDriverState
*bs
, bool eject_flag
)
222 bdrv_eject(bs
->file
->bs
, eject_flag
);
226 static void cor_lock_medium(BlockDriverState
*bs
, bool locked
)
228 bdrv_lock_medium(bs
->file
->bs
, locked
);
232 static void cor_close(BlockDriverState
*bs
)
234 BDRVStateCOR
*s
= bs
->opaque
;
236 if (s
->chain_frozen
) {
237 s
->chain_frozen
= false;
238 bdrv_unfreeze_backing_chain(bs
, s
->bottom_bs
);
241 bdrv_unref(s
->bottom_bs
);
245 static BlockDriver bdrv_copy_on_read
= {
246 .format_name
= "copy-on-read",
247 .instance_size
= sizeof(BDRVStateCOR
),
249 .bdrv_open
= cor_open
,
250 .bdrv_close
= cor_close
,
251 .bdrv_child_perm
= cor_child_perm
,
253 .bdrv_getlength
= cor_getlength
,
255 .bdrv_co_preadv_part
= cor_co_preadv_part
,
256 .bdrv_co_pwritev_part
= cor_co_pwritev_part
,
257 .bdrv_co_pwrite_zeroes
= cor_co_pwrite_zeroes
,
258 .bdrv_co_pdiscard
= cor_co_pdiscard
,
259 .bdrv_co_pwritev_compressed
= cor_co_pwritev_compressed
,
261 .bdrv_eject
= cor_eject
,
262 .bdrv_lock_medium
= cor_lock_medium
,
264 .has_variable_length
= true,
269 void bdrv_cor_filter_drop(BlockDriverState
*cor_filter_bs
)
271 BDRVStateCOR
*s
= cor_filter_bs
->opaque
;
273 /* unfreeze, as otherwise bdrv_replace_node() will fail */
274 if (s
->chain_frozen
) {
275 s
->chain_frozen
= false;
276 bdrv_unfreeze_backing_chain(cor_filter_bs
, s
->bottom_bs
);
278 bdrv_drop_filter(cor_filter_bs
, &error_abort
);
279 bdrv_unref(cor_filter_bs
);
283 static void bdrv_copy_on_read_init(void)
285 bdrv_register(&bdrv_copy_on_read
);
288 block_init(bdrv_copy_on_read_init
);