]>
Commit | Line | Data |
---|---|---|
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" | |
25 | ||
26 | ||
27 | static int cor_open(BlockDriverState *bs, QDict *options, int flags, | |
28 | Error **errp) | |
29 | { | |
30 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, false, | |
31 | errp); | |
32 | if (!bs->file) { | |
33 | return -EINVAL; | |
34 | } | |
35 | ||
36 | bs->supported_write_flags = BDRV_REQ_FUA & | |
37 | bs->file->bs->supported_write_flags; | |
38 | ||
39 | bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) & | |
40 | bs->file->bs->supported_zero_flags; | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | ||
46 | static void cor_close(BlockDriverState *bs) | |
47 | { | |
48 | } | |
49 | ||
50 | ||
51 | #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \ | |
52 | | BLK_PERM_WRITE \ | |
53 | | BLK_PERM_RESIZE) | |
54 | #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH) | |
55 | ||
56 | static void cor_child_perm(BlockDriverState *bs, BdrvChild *c, | |
57 | const BdrvChildRole *role, | |
58 | BlockReopenQueue *reopen_queue, | |
59 | uint64_t perm, uint64_t shared, | |
60 | uint64_t *nperm, uint64_t *nshared) | |
61 | { | |
62 | if (c == NULL) { | |
63 | *nperm = (perm & PERM_PASSTHROUGH) | BLK_PERM_WRITE_UNCHANGED; | |
64 | *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED; | |
65 | return; | |
66 | } | |
67 | ||
68 | *nperm = (perm & PERM_PASSTHROUGH) | | |
69 | (c->perm & PERM_UNCHANGED); | |
70 | *nshared = (shared & PERM_PASSTHROUGH) | | |
71 | (c->shared_perm & PERM_UNCHANGED); | |
72 | } | |
73 | ||
74 | ||
75 | static int64_t cor_getlength(BlockDriverState *bs) | |
76 | { | |
77 | return bdrv_getlength(bs->file->bs); | |
78 | } | |
79 | ||
80 | ||
81 | static int cor_truncate(BlockDriverState *bs, int64_t offset, | |
82 | PreallocMode prealloc, Error **errp) | |
83 | { | |
84 | return bdrv_truncate(bs->file, offset, prealloc, errp); | |
85 | } | |
86 | ||
87 | ||
88 | static int coroutine_fn cor_co_preadv(BlockDriverState *bs, | |
89 | uint64_t offset, uint64_t bytes, | |
90 | QEMUIOVector *qiov, int flags) | |
91 | { | |
92 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, | |
93 | flags | BDRV_REQ_COPY_ON_READ); | |
94 | } | |
95 | ||
96 | ||
97 | static int coroutine_fn cor_co_pwritev(BlockDriverState *bs, | |
98 | uint64_t offset, uint64_t bytes, | |
99 | QEMUIOVector *qiov, int flags) | |
100 | { | |
101 | ||
102 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); | |
103 | } | |
104 | ||
105 | ||
106 | static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs, | |
107 | int64_t offset, int bytes, | |
108 | BdrvRequestFlags flags) | |
109 | { | |
110 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
111 | } | |
112 | ||
113 | ||
114 | static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs, | |
115 | int64_t offset, int bytes) | |
116 | { | |
117 | return bdrv_co_pdiscard(bs->file->bs, offset, bytes); | |
118 | } | |
119 | ||
120 | ||
121 | static void cor_eject(BlockDriverState *bs, bool eject_flag) | |
122 | { | |
123 | bdrv_eject(bs->file->bs, eject_flag); | |
124 | } | |
125 | ||
126 | ||
127 | static void cor_lock_medium(BlockDriverState *bs, bool locked) | |
128 | { | |
129 | bdrv_lock_medium(bs->file->bs, locked); | |
130 | } | |
131 | ||
132 | ||
133 | static bool cor_recurse_is_first_non_filter(BlockDriverState *bs, | |
134 | BlockDriverState *candidate) | |
135 | { | |
136 | return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); | |
137 | } | |
138 | ||
139 | ||
140 | BlockDriver bdrv_copy_on_read = { | |
141 | .format_name = "copy-on-read", | |
142 | ||
143 | .bdrv_open = cor_open, | |
144 | .bdrv_close = cor_close, | |
145 | .bdrv_child_perm = cor_child_perm, | |
146 | ||
147 | .bdrv_getlength = cor_getlength, | |
148 | .bdrv_truncate = cor_truncate, | |
149 | ||
150 | .bdrv_co_preadv = cor_co_preadv, | |
151 | .bdrv_co_pwritev = cor_co_pwritev, | |
152 | .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes, | |
153 | .bdrv_co_pdiscard = cor_co_pdiscard, | |
154 | ||
155 | .bdrv_eject = cor_eject, | |
156 | .bdrv_lock_medium = cor_lock_medium, | |
157 | ||
158 | .bdrv_co_block_status = bdrv_co_block_status_from_file, | |
159 | ||
160 | .bdrv_recurse_is_first_non_filter = cor_recurse_is_first_non_filter, | |
161 | ||
162 | .has_variable_length = true, | |
163 | .is_filter = true, | |
164 | }; | |
165 | ||
166 | static void bdrv_copy_on_read_init(void) | |
167 | { | |
168 | bdrv_register(&bdrv_copy_on_read); | |
169 | } | |
170 | ||
171 | block_init(bdrv_copy_on_read_init); |