xref: /qemu/block/copy-on-read.c (revision 5db05230)
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-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"
30 
31 
32 typedef struct BDRVStateCOR {
33     BlockDriverState *bottom_bs;
34     bool chain_frozen;
35 } BDRVStateCOR;
36 
37 
38 static int GRAPH_UNLOCKED
39 cor_open(BlockDriverState *bs, QDict *options, int flags, 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     int ret;
46 
47     GLOBAL_STATE_CODE();
48 
49     ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
50     if (ret < 0) {
51         return ret;
52     }
53 
54     GRAPH_RDLOCK_GUARD_MAINLOOP();
55 
56     bs->supported_read_flags = BDRV_REQ_PREFETCH;
57 
58     bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
59         (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
60 
61     bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
62         ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
63             bs->file->bs->supported_zero_flags);
64 
65     if (bottom_node) {
66         bottom_bs = bdrv_find_node(bottom_node);
67         if (!bottom_bs) {
68             error_setg(errp, "Bottom node '%s' not found", bottom_node);
69             qdict_del(options, "bottom");
70             return -EINVAL;
71         }
72         qdict_del(options, "bottom");
73 
74         if (!bottom_bs->drv) {
75             error_setg(errp, "Bottom node '%s' not opened", bottom_node);
76             return -EINVAL;
77         }
78 
79         if (bottom_bs->drv->is_filter) {
80             error_setg(errp, "Bottom node '%s' is a filter", bottom_node);
81             return -EINVAL;
82         }
83 
84         if (bdrv_freeze_backing_chain(bs, bottom_bs, errp) < 0) {
85             return -EINVAL;
86         }
87         state->chain_frozen = true;
88 
89         /*
90          * We do freeze the chain, so it shouldn't be removed. Still, storing a
91          * pointer worth bdrv_ref().
92          */
93         bdrv_ref(bottom_bs);
94     }
95     state->bottom_bs = bottom_bs;
96 
97     /*
98      * We don't need to call bdrv_child_refresh_perms() now as the permissions
99      * will be updated later when the filter node gets its parent.
100      */
101 
102     return 0;
103 }
104 
105 
106 #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
107                           | BLK_PERM_WRITE \
108                           | BLK_PERM_RESIZE)
109 #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
110 
111 static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
112                            BdrvChildRole role,
113                            BlockReopenQueue *reopen_queue,
114                            uint64_t perm, uint64_t shared,
115                            uint64_t *nperm, uint64_t *nshared)
116 {
117     *nperm = perm & PERM_PASSTHROUGH;
118     *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
119 
120     /* We must not request write permissions for an inactive node, the child
121      * cannot provide it. */
122     if (!(bs->open_flags & BDRV_O_INACTIVE)) {
123         *nperm |= BLK_PERM_WRITE_UNCHANGED;
124     }
125 }
126 
127 
128 static int64_t coroutine_fn GRAPH_RDLOCK cor_co_getlength(BlockDriverState *bs)
129 {
130     return bdrv_co_getlength(bs->file->bs);
131 }
132 
133 
134 static int coroutine_fn GRAPH_RDLOCK
135 cor_co_preadv_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
136                    QEMUIOVector *qiov, size_t qiov_offset,
137                    BdrvRequestFlags flags)
138 {
139     int64_t n;
140     int local_flags;
141     int ret;
142     BDRVStateCOR *state = bs->opaque;
143 
144     if (!state->bottom_bs) {
145         return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset,
146                                    flags | BDRV_REQ_COPY_ON_READ);
147     }
148 
149     while (bytes) {
150         local_flags = flags;
151 
152         /* In case of failure, try to copy-on-read anyway */
153         ret = bdrv_co_is_allocated(bs->file->bs, offset, bytes, &n);
154         if (ret <= 0) {
155             ret = bdrv_co_is_allocated_above(bdrv_backing_chain_next(bs->file->bs),
156                                              state->bottom_bs, true, offset,
157                                              n, &n);
158             if (ret > 0 || ret < 0) {
159                 local_flags |= BDRV_REQ_COPY_ON_READ;
160             }
161             /* Finish earlier if the end of a backing file has been reached */
162             if (n == 0) {
163                 break;
164             }
165         }
166 
167         /* Skip if neither read nor write are needed */
168         if ((local_flags & (BDRV_REQ_PREFETCH | BDRV_REQ_COPY_ON_READ)) !=
169             BDRV_REQ_PREFETCH) {
170             ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset,
171                                       local_flags);
172             if (ret < 0) {
173                 return ret;
174             }
175         }
176 
177         offset += n;
178         qiov_offset += n;
179         bytes -= n;
180     }
181 
182     return 0;
183 }
184 
185 
186 static int coroutine_fn GRAPH_RDLOCK
187 cor_co_pwritev_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
188                     QEMUIOVector *qiov, size_t qiov_offset,
189                     BdrvRequestFlags flags)
190 {
191     return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
192                                 flags);
193 }
194 
195 
196 static int coroutine_fn GRAPH_RDLOCK
197 cor_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
198                      BdrvRequestFlags flags)
199 {
200     return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
201 }
202 
203 
204 static int coroutine_fn GRAPH_RDLOCK
205 cor_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
206 {
207     return bdrv_co_pdiscard(bs->file, offset, bytes);
208 }
209 
210 
211 static int coroutine_fn GRAPH_RDLOCK
212 cor_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
213                           QEMUIOVector *qiov)
214 {
215     return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
216                            BDRV_REQ_WRITE_COMPRESSED);
217 }
218 
219 
220 static void coroutine_fn GRAPH_RDLOCK
221 cor_co_eject(BlockDriverState *bs, bool eject_flag)
222 {
223     bdrv_co_eject(bs->file->bs, eject_flag);
224 }
225 
226 
227 static void coroutine_fn GRAPH_RDLOCK
228 cor_co_lock_medium(BlockDriverState *bs, bool locked)
229 {
230     bdrv_co_lock_medium(bs->file->bs, locked);
231 }
232 
233 
234 static void GRAPH_UNLOCKED cor_close(BlockDriverState *bs)
235 {
236     BDRVStateCOR *s = bs->opaque;
237 
238     GLOBAL_STATE_CODE();
239 
240     if (s->chain_frozen) {
241         bdrv_graph_rdlock_main_loop();
242         s->chain_frozen = false;
243         bdrv_unfreeze_backing_chain(bs, s->bottom_bs);
244         bdrv_graph_rdunlock_main_loop();
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_co_getlength                  = cor_co_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_co_eject                      = cor_co_eject,
268     .bdrv_co_lock_medium                = cor_co_lock_medium,
269 
270     .is_filter                          = true,
271 };
272 
273 
274 void no_coroutine_fn bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
275 {
276     BDRVStateCOR *s = cor_filter_bs->opaque;
277 
278     GLOBAL_STATE_CODE();
279 
280     /* unfreeze, as otherwise bdrv_replace_node() will fail */
281     if (s->chain_frozen) {
282         GRAPH_RDLOCK_GUARD_MAINLOOP();
283         s->chain_frozen = false;
284         bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs);
285     }
286     bdrv_drop_filter(cor_filter_bs, &error_abort);
287     bdrv_unref(cor_filter_bs);
288 }
289 
290 
291 static void bdrv_copy_on_read_init(void)
292 {
293     bdrv_register(&bdrv_copy_on_read);
294 }
295 
296 block_init(bdrv_copy_on_read_init);
297