]> Git Repo - qemu.git/blob - block/copy-on-read.c
qapi: copy-on-read filter: add 'bottom' option
[qemu.git] / block / copy-on-read.c
1 /*
2  * Copy-on-read filter block driver
3  *
4  * Copyright (c) 2018 Red Hat, Inc.
5  *
6  * Author:
7  *   Max Reitz <[email protected]>
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 #include "qemu/module.h"
26 #include "qapi/error.h"
27 #include "qapi/qmp/qdict.h"
28 #include "block/copy-on-read.h"
29
30
31 typedef struct BDRVStateCOR {
32     bool active;
33     BlockDriverState *bottom_bs;
34     bool chain_frozen;
35 } BDRVStateCOR;
36
37
38 static int cor_open(BlockDriverState *bs, QDict *options, int flags,
39                     Error **errp)
40 {
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");
45
46     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
47                                BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
48                                false, errp);
49     if (!bs->file) {
50         return -EINVAL;
51     }
52
53     bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
54         (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
55
56     bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
57         ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
58             bs->file->bs->supported_zero_flags);
59
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     }
90     state->active = true;
91     state->bottom_bs = bottom_bs;
92
93     /*
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.
96      */
97
98     return 0;
99 }
100
101
102 #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
103                           | BLK_PERM_WRITE \
104                           | BLK_PERM_RESIZE)
105 #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
106
107 static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
108                            BdrvChildRole role,
109                            BlockReopenQueue *reopen_queue,
110                            uint64_t perm, uint64_t shared,
111                            uint64_t *nperm, uint64_t *nshared)
112 {
113     BDRVStateCOR *s = bs->opaque;
114
115     if (!s->active) {
116         /*
117          * While the filter is being removed
118          */
119         *nperm = 0;
120         *nshared = BLK_PERM_ALL;
121         return;
122     }
123
124     *nperm = perm & PERM_PASSTHROUGH;
125     *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
126
127     /* We must not request write permissions for an inactive node, the child
128      * cannot provide it. */
129     if (!(bs->open_flags & BDRV_O_INACTIVE)) {
130         *nperm |= BLK_PERM_WRITE_UNCHANGED;
131     }
132 }
133
134
135 static int64_t cor_getlength(BlockDriverState *bs)
136 {
137     return bdrv_getlength(bs->file->bs);
138 }
139
140
141 static int coroutine_fn cor_co_preadv_part(BlockDriverState *bs,
142                                            uint64_t offset, uint64_t bytes,
143                                            QEMUIOVector *qiov,
144                                            size_t qiov_offset,
145                                            int flags)
146 {
147     int64_t n;
148     int local_flags;
149     int ret;
150     BDRVStateCOR *state = bs->opaque;
151
152     if (!state->bottom_bs) {
153         return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset,
154                                    flags | BDRV_REQ_COPY_ON_READ);
155     }
156
157     while (bytes) {
158         local_flags = flags;
159
160         /* In case of failure, try to copy-on-read anyway */
161         ret = bdrv_is_allocated(bs->file->bs, offset, bytes, &n);
162         if (ret <= 0) {
163             ret = bdrv_is_allocated_above(bdrv_backing_chain_next(bs->file->bs),
164                                           state->bottom_bs, true, offset,
165                                           n, &n);
166             if (ret > 0 || ret < 0) {
167                 local_flags |= BDRV_REQ_COPY_ON_READ;
168             }
169             /* Finish earlier if the end of a backing file has been reached */
170             if (n == 0) {
171                 break;
172             }
173         }
174
175         ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset,
176                                   local_flags);
177         if (ret < 0) {
178             return ret;
179         }
180
181         offset += n;
182         qiov_offset += n;
183         bytes -= n;
184     }
185
186     return 0;
187 }
188
189
190 static int coroutine_fn cor_co_pwritev_part(BlockDriverState *bs,
191                                             uint64_t offset,
192                                             uint64_t bytes,
193                                             QEMUIOVector *qiov,
194                                             size_t qiov_offset, int flags)
195 {
196     return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
197                                 flags);
198 }
199
200
201 static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs,
202                                              int64_t offset, int bytes,
203                                              BdrvRequestFlags flags)
204 {
205     return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
206 }
207
208
209 static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,
210                                         int64_t offset, int bytes)
211 {
212     return bdrv_co_pdiscard(bs->file, offset, bytes);
213 }
214
215
216 static int coroutine_fn cor_co_pwritev_compressed(BlockDriverState *bs,
217                                                   uint64_t offset,
218                                                   uint64_t bytes,
219                                                   QEMUIOVector *qiov)
220 {
221     return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
222                            BDRV_REQ_WRITE_COMPRESSED);
223 }
224
225
226 static void cor_eject(BlockDriverState *bs, bool eject_flag)
227 {
228     bdrv_eject(bs->file->bs, eject_flag);
229 }
230
231
232 static void cor_lock_medium(BlockDriverState *bs, bool locked)
233 {
234     bdrv_lock_medium(bs->file->bs, locked);
235 }
236
237
238 static void cor_close(BlockDriverState *bs)
239 {
240     BDRVStateCOR *s = bs->opaque;
241
242     if (s->chain_frozen) {
243         s->chain_frozen = false;
244         bdrv_unfreeze_backing_chain(bs, s->bottom_bs);
245     }
246
247     bdrv_unref(s->bottom_bs);
248 }
249
250
251 static BlockDriver bdrv_copy_on_read = {
252     .format_name                        = "copy-on-read",
253     .instance_size                      = sizeof(BDRVStateCOR),
254
255     .bdrv_open                          = cor_open,
256     .bdrv_close                         = cor_close,
257     .bdrv_child_perm                    = cor_child_perm,
258
259     .bdrv_getlength                     = cor_getlength,
260
261     .bdrv_co_preadv_part                = cor_co_preadv_part,
262     .bdrv_co_pwritev_part               = cor_co_pwritev_part,
263     .bdrv_co_pwrite_zeroes              = cor_co_pwrite_zeroes,
264     .bdrv_co_pdiscard                   = cor_co_pdiscard,
265     .bdrv_co_pwritev_compressed         = cor_co_pwritev_compressed,
266
267     .bdrv_eject                         = cor_eject,
268     .bdrv_lock_medium                   = cor_lock_medium,
269
270     .has_variable_length                = true,
271     .is_filter                          = true,
272 };
273
274
275 void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
276 {
277     BdrvChild *child;
278     BlockDriverState *bs;
279     BDRVStateCOR *s = cor_filter_bs->opaque;
280
281     child = bdrv_filter_child(cor_filter_bs);
282     if (!child) {
283         return;
284     }
285     bs = child->bs;
286
287     /* Retain the BDS until we complete the graph change. */
288     bdrv_ref(bs);
289     /* Hold a guest back from writing while permissions are being reset. */
290     bdrv_drained_begin(bs);
291     /* Drop permissions before the graph change. */
292     s->active = false;
293     /* unfreeze, as otherwise bdrv_replace_node() will fail */
294     if (s->chain_frozen) {
295         s->chain_frozen = false;
296         bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs);
297     }
298     bdrv_child_refresh_perms(cor_filter_bs, child, &error_abort);
299     bdrv_replace_node(cor_filter_bs, bs, &error_abort);
300
301     bdrv_drained_end(bs);
302     bdrv_unref(bs);
303     bdrv_unref(cor_filter_bs);
304 }
305
306
307 static void bdrv_copy_on_read_init(void)
308 {
309     bdrv_register(&bdrv_copy_on_read);
310 }
311
312 block_init(bdrv_copy_on_read_init);
This page took 0.040797 seconds and 4 git commands to generate.