virtio: add checks for the size of the indirect table
[qemu.git] / block / copy-on-read.c
blob64dcc424b51c1a431dc52db3139d9ff69f4252b6
1 /*
2 * Copy-on-read filter block driver
4 * Copyright (c) 2018 Red Hat, Inc.
6 * Author:
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_int.h"
27 static int cor_open(BlockDriverState *bs, QDict *options, int flags,
28 Error **errp)
30 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, false,
31 errp);
32 if (!bs->file) {
33 return -EINVAL;
36 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
37 (BDRV_REQ_FUA &
38 bs->file->bs->supported_write_flags);
40 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
41 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
42 bs->file->bs->supported_zero_flags);
44 return 0;
48 #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
49 | BLK_PERM_WRITE \
50 | BLK_PERM_RESIZE)
51 #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
53 static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
54 const BdrvChildRole *role,
55 BlockReopenQueue *reopen_queue,
56 uint64_t perm, uint64_t shared,
57 uint64_t *nperm, uint64_t *nshared)
59 if (c == NULL) {
60 *nperm = (perm & PERM_PASSTHROUGH) | BLK_PERM_WRITE_UNCHANGED;
61 *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
62 return;
65 *nperm = (perm & PERM_PASSTHROUGH) |
66 (c->perm & PERM_UNCHANGED);
67 *nshared = (shared & PERM_PASSTHROUGH) |
68 (c->shared_perm & PERM_UNCHANGED);
72 static int64_t cor_getlength(BlockDriverState *bs)
74 return bdrv_getlength(bs->file->bs);
78 static int coroutine_fn cor_co_truncate(BlockDriverState *bs, int64_t offset,
79 PreallocMode prealloc, Error **errp)
81 return bdrv_co_truncate(bs->file, offset, prealloc, errp);
85 static int coroutine_fn cor_co_preadv(BlockDriverState *bs,
86 uint64_t offset, uint64_t bytes,
87 QEMUIOVector *qiov, int flags)
89 return bdrv_co_preadv(bs->file, offset, bytes, qiov,
90 flags | BDRV_REQ_COPY_ON_READ);
94 static int coroutine_fn cor_co_pwritev(BlockDriverState *bs,
95 uint64_t offset, uint64_t bytes,
96 QEMUIOVector *qiov, int flags)
99 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
103 static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs,
104 int64_t offset, int bytes,
105 BdrvRequestFlags flags)
107 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
111 static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,
112 int64_t offset, int bytes)
114 return bdrv_co_pdiscard(bs->file, offset, bytes);
118 static void cor_eject(BlockDriverState *bs, bool eject_flag)
120 bdrv_eject(bs->file->bs, eject_flag);
124 static void cor_lock_medium(BlockDriverState *bs, bool locked)
126 bdrv_lock_medium(bs->file->bs, locked);
130 static bool cor_recurse_is_first_non_filter(BlockDriverState *bs,
131 BlockDriverState *candidate)
133 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
137 BlockDriver bdrv_copy_on_read = {
138 .format_name = "copy-on-read",
140 .bdrv_open = cor_open,
141 .bdrv_child_perm = cor_child_perm,
143 .bdrv_getlength = cor_getlength,
144 .bdrv_co_truncate = cor_co_truncate,
146 .bdrv_co_preadv = cor_co_preadv,
147 .bdrv_co_pwritev = cor_co_pwritev,
148 .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes,
149 .bdrv_co_pdiscard = cor_co_pdiscard,
151 .bdrv_eject = cor_eject,
152 .bdrv_lock_medium = cor_lock_medium,
154 .bdrv_co_block_status = bdrv_co_block_status_from_file,
156 .bdrv_recurse_is_first_non_filter = cor_recurse_is_first_non_filter,
158 .has_variable_length = true,
159 .is_filter = true,
162 static void bdrv_copy_on_read_init(void)
164 bdrv_register(&bdrv_copy_on_read);
167 block_init(bdrv_copy_on_read_init);