]>
Commit | Line | Data |
---|---|---|
7df7868b VSO |
1 | /* |
2 | * backup-top filter driver | |
3 | * | |
4 | * The driver performs Copy-Before-Write (CBW) operation: it is injected above | |
5 | * some node, and before each write it copies _old_ data to the target node. | |
6 | * | |
7 | * Copyright (c) 2018-2019 Virtuozzo International GmbH. | |
8 | * | |
9 | * Author: | |
10 | * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
24 | */ | |
25 | ||
26 | #include "qemu/osdep.h" | |
27 | ||
28 | #include "sysemu/block-backend.h" | |
29 | #include "qemu/cutils.h" | |
30 | #include "qapi/error.h" | |
31 | #include "block/block_int.h" | |
32 | #include "block/qdict.h" | |
33 | #include "block/block-copy.h" | |
34 | ||
35 | #include "block/backup-top.h" | |
36 | ||
37 | typedef struct BDRVBackupTopState { | |
38 | BlockCopyState *bcs; | |
39 | BdrvChild *target; | |
40 | bool active; | |
397f4e9d | 41 | int64_t cluster_size; |
7df7868b VSO |
42 | } BDRVBackupTopState; |
43 | ||
44 | static coroutine_fn int backup_top_co_preadv( | |
45 | BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
46 | QEMUIOVector *qiov, int flags) | |
47 | { | |
48 | return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); | |
49 | } | |
50 | ||
51 | static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset, | |
4bc267a7 | 52 | uint64_t bytes, BdrvRequestFlags flags) |
7df7868b | 53 | { |
00e30f05 | 54 | BDRVBackupTopState *s = bs->opaque; |
4bc267a7 VSO |
55 | uint64_t off, end; |
56 | ||
57 | if (flags & BDRV_REQ_WRITE_UNCHANGED) { | |
58 | return 0; | |
59 | } | |
60 | ||
397f4e9d VSO |
61 | off = QEMU_ALIGN_DOWN(offset, s->cluster_size); |
62 | end = QEMU_ALIGN_UP(offset + bytes, s->cluster_size); | |
7df7868b | 63 | |
00e30f05 | 64 | return block_copy(s->bcs, off, end - off, NULL); |
7df7868b VSO |
65 | } |
66 | ||
67 | static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs, | |
68 | int64_t offset, int bytes) | |
69 | { | |
4bc267a7 | 70 | int ret = backup_top_cbw(bs, offset, bytes, 0); |
7df7868b VSO |
71 | if (ret < 0) { |
72 | return ret; | |
73 | } | |
74 | ||
75 | return bdrv_co_pdiscard(bs->backing, offset, bytes); | |
76 | } | |
77 | ||
78 | static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs, | |
79 | int64_t offset, int bytes, BdrvRequestFlags flags) | |
80 | { | |
4bc267a7 | 81 | int ret = backup_top_cbw(bs, offset, bytes, flags); |
7df7868b VSO |
82 | if (ret < 0) { |
83 | return ret; | |
84 | } | |
85 | ||
86 | return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); | |
87 | } | |
88 | ||
89 | static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs, | |
90 | uint64_t offset, | |
91 | uint64_t bytes, | |
92 | QEMUIOVector *qiov, int flags) | |
93 | { | |
4bc267a7 VSO |
94 | int ret = backup_top_cbw(bs, offset, bytes, flags); |
95 | if (ret < 0) { | |
96 | return ret; | |
7df7868b VSO |
97 | } |
98 | ||
99 | return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); | |
100 | } | |
101 | ||
102 | static int coroutine_fn backup_top_co_flush(BlockDriverState *bs) | |
103 | { | |
104 | if (!bs->backing) { | |
105 | return 0; | |
106 | } | |
107 | ||
108 | return bdrv_co_flush(bs->backing->bs); | |
109 | } | |
110 | ||
111 | static void backup_top_refresh_filename(BlockDriverState *bs) | |
112 | { | |
113 | if (bs->backing == NULL) { | |
114 | /* | |
115 | * we can be here after failed bdrv_attach_child in | |
116 | * bdrv_set_backing_hd | |
117 | */ | |
118 | return; | |
119 | } | |
120 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), | |
121 | bs->backing->bs->filename); | |
122 | } | |
123 | ||
124 | static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, | |
bd86fb99 | 125 | const BdrvChildClass *child_class, |
bf8e925e | 126 | BdrvChildRole role, |
7df7868b VSO |
127 | BlockReopenQueue *reopen_queue, |
128 | uint64_t perm, uint64_t shared, | |
129 | uint64_t *nperm, uint64_t *nshared) | |
130 | { | |
131 | BDRVBackupTopState *s = bs->opaque; | |
132 | ||
133 | if (!s->active) { | |
134 | /* | |
135 | * The filter node may be in process of bdrv_append(), which firstly do | |
136 | * bdrv_set_backing_hd() and then bdrv_replace_node(). This means that | |
137 | * we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, | |
138 | * let's require nothing during bdrv_append() and refresh permissions | |
139 | * after it (see bdrv_backup_top_append()). | |
140 | */ | |
141 | *nperm = 0; | |
142 | *nshared = BLK_PERM_ALL; | |
143 | return; | |
144 | } | |
145 | ||
25191e5f | 146 | if (!(role & BDRV_CHILD_FILTERED)) { |
7df7868b VSO |
147 | /* |
148 | * Target child | |
149 | * | |
150 | * Share write to target (child_file), to not interfere | |
151 | * with guest writes to its disk which may be in target backing chain. | |
958a04bd KW |
152 | * Can't resize during a backup block job because we check the size |
153 | * only upfront. | |
7df7868b | 154 | */ |
958a04bd | 155 | *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; |
7df7868b VSO |
156 | *nperm = BLK_PERM_WRITE; |
157 | } else { | |
158 | /* Source child */ | |
bf8e925e | 159 | bdrv_filter_default_perms(bs, c, child_class, role, reopen_queue, |
bd86fb99 | 160 | perm, shared, nperm, nshared); |
7df7868b VSO |
161 | |
162 | if (perm & BLK_PERM_WRITE) { | |
163 | *nperm = *nperm | BLK_PERM_CONSISTENT_READ; | |
164 | } | |
958a04bd | 165 | *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); |
7df7868b VSO |
166 | } |
167 | } | |
168 | ||
169 | BlockDriver bdrv_backup_top_filter = { | |
170 | .format_name = "backup-top", | |
171 | .instance_size = sizeof(BDRVBackupTopState), | |
172 | ||
173 | .bdrv_co_preadv = backup_top_co_preadv, | |
174 | .bdrv_co_pwritev = backup_top_co_pwritev, | |
175 | .bdrv_co_pwrite_zeroes = backup_top_co_pwrite_zeroes, | |
176 | .bdrv_co_pdiscard = backup_top_co_pdiscard, | |
177 | .bdrv_co_flush = backup_top_co_flush, | |
178 | ||
179 | .bdrv_co_block_status = bdrv_co_block_status_from_backing, | |
180 | ||
181 | .bdrv_refresh_filename = backup_top_refresh_filename, | |
182 | ||
183 | .bdrv_child_perm = backup_top_child_perm, | |
184 | ||
185 | .is_filter = true, | |
186 | }; | |
187 | ||
188 | BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, | |
189 | BlockDriverState *target, | |
190 | const char *filter_node_name, | |
191 | uint64_t cluster_size, | |
192 | BdrvRequestFlags write_flags, | |
193 | BlockCopyState **bcs, | |
194 | Error **errp) | |
195 | { | |
196 | Error *local_err = NULL; | |
197 | BDRVBackupTopState *state; | |
958a04bd | 198 | BlockDriverState *top; |
0df62f45 | 199 | bool appended = false; |
7df7868b | 200 | |
958a04bd KW |
201 | assert(source->total_sectors == target->total_sectors); |
202 | ||
203 | top = bdrv_new_open_driver(&bdrv_backup_top_filter, filter_node_name, | |
204 | BDRV_O_RDWR, errp); | |
7df7868b VSO |
205 | if (!top) { |
206 | return NULL; | |
207 | } | |
208 | ||
fb574de8 | 209 | state = top->opaque; |
4bc267a7 VSO |
210 | top->total_sectors = source->total_sectors; |
211 | top->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | | |
212 | (BDRV_REQ_FUA & source->supported_write_flags); | |
213 | top->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | | |
214 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & | |
215 | source->supported_zero_flags); | |
7df7868b VSO |
216 | |
217 | bdrv_ref(target); | |
58944401 HR |
218 | state->target = bdrv_attach_child(top, target, "target", &child_of_bds, |
219 | BDRV_CHILD_DATA, errp); | |
7df7868b VSO |
220 | if (!state->target) { |
221 | bdrv_unref(target); | |
222 | bdrv_unref(top); | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | bdrv_drained_begin(source); | |
227 | ||
228 | bdrv_ref(top); | |
229 | bdrv_append(top, source, &local_err); | |
230 | if (local_err) { | |
231 | error_prepend(&local_err, "Cannot append backup-top filter: "); | |
0df62f45 | 232 | goto fail; |
7df7868b | 233 | } |
0df62f45 | 234 | appended = true; |
7df7868b VSO |
235 | |
236 | /* | |
237 | * bdrv_append() finished successfully, now we can require permissions | |
238 | * we want. | |
239 | */ | |
240 | state->active = true; | |
241 | bdrv_child_refresh_perms(top, top->backing, &local_err); | |
242 | if (local_err) { | |
243 | error_prepend(&local_err, | |
244 | "Cannot set permissions for backup-top filter: "); | |
0df62f45 | 245 | goto fail; |
7df7868b VSO |
246 | } |
247 | ||
397f4e9d | 248 | state->cluster_size = cluster_size; |
00e30f05 VSO |
249 | state->bcs = block_copy_state_new(top->backing, state->target, |
250 | cluster_size, write_flags, &local_err); | |
251 | if (local_err) { | |
252 | error_prepend(&local_err, "Cannot create block-copy-state: "); | |
0df62f45 | 253 | goto fail; |
7df7868b VSO |
254 | } |
255 | *bcs = state->bcs; | |
256 | ||
257 | bdrv_drained_end(source); | |
258 | ||
259 | return top; | |
260 | ||
0df62f45 VSO |
261 | fail: |
262 | if (appended) { | |
263 | state->active = false; | |
264 | bdrv_backup_top_drop(top); | |
265 | } else { | |
266 | bdrv_unref(top); | |
267 | } | |
7df7868b | 268 | |
7df7868b | 269 | bdrv_drained_end(source); |
7df7868b VSO |
270 | error_propagate(errp, local_err); |
271 | ||
272 | return NULL; | |
273 | } | |
274 | ||
275 | void bdrv_backup_top_drop(BlockDriverState *bs) | |
276 | { | |
277 | BDRVBackupTopState *s = bs->opaque; | |
7df7868b VSO |
278 | |
279 | bdrv_drained_begin(bs); | |
280 | ||
503ca126 HR |
281 | block_copy_state_free(s->bcs); |
282 | ||
7df7868b VSO |
283 | s->active = false; |
284 | bdrv_child_refresh_perms(bs, bs->backing, &error_abort); | |
285 | bdrv_replace_node(bs, backing_bs(bs), &error_abort); | |
286 | bdrv_set_backing_hd(bs, NULL, &error_abort); | |
287 | ||
288 | bdrv_drained_end(bs); | |
289 | ||
290 | bdrv_unref(bs); | |
7df7868b | 291 | } |