master
c 324 lines 9.02 KB
Raw
1 /*
2 * QEMU block dirty bitmap QMP commands
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
8 *
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
11 *
12 * Copyright (c) 2003-2008 Fabrice Bellard
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 */
32
33 #include "qemu/osdep.h"
34
35 #include "block/block-io.h"
36 #include "block/block_int.h"
37 #include "block/dirty-bitmap.h"
38 #include "qapi/qapi-commands-block.h"
39 #include "qapi/error.h"
40
41 /**
42 * block_dirty_bitmap_lookup:
43 * Return a dirty bitmap (if present), after validating
44 * the node reference and bitmap names.
45 *
46 * @node: The name of the BDS node to search for bitmaps
47 * @name: The name of the bitmap to search for
48 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
49 * @errp: Output pointer for error information. Can be NULL.
50 *
51 * @return: A bitmap object on success, or NULL on failure.
52 */
53 BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
54 const char *name,
55 BlockDriverState **pbs,
56 Error **errp)
57 {
58 BlockDriverState *bs;
59 BdrvDirtyBitmap *bitmap;
60
61 GLOBAL_STATE_CODE();
62
63 if (!node) {
64 error_setg(errp, "Node cannot be NULL");
65 return NULL;
66 }
67 if (!name) {
68 error_setg(errp, "Bitmap name cannot be NULL");
69 return NULL;
70 }
71 bs = bdrv_lookup_bs(node, node, NULL);
72 if (!bs) {
73 error_setg(errp, "Node '%s' not found", node);
74 return NULL;
75 }
76
77 bitmap = bdrv_find_dirty_bitmap(bs, name);
78 if (!bitmap) {
79 error_setg(errp, "Dirty bitmap '%s' not found", name);
80 return NULL;
81 }
82
83 if (pbs) {
84 *pbs = bs;
85 }
86
87 return bitmap;
88 }
89
90 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
91 bool has_granularity, uint32_t granularity,
92 bool has_persistent, bool persistent,
93 bool has_disabled, bool disabled,
94 Error **errp)
95 {
96 BlockDriverState *bs;
97 BdrvDirtyBitmap *bitmap;
98
99 if (!name || name[0] == '\0') {
100 error_setg(errp, "Bitmap name cannot be empty");
101 return;
102 }
103
104 bs = bdrv_lookup_bs(node, node, errp);
105 if (!bs) {
106 return;
107 }
108
109 if (has_granularity) {
110 if (granularity < 512 || !is_power_of_2(granularity)) {
111 error_setg(errp, "Granularity must be power of 2 "
112 "and at least 512");
113 return;
114 }
115 } else {
116 /* Default to cluster size, if available: */
117 granularity = bdrv_get_default_bitmap_granularity(bs);
118 }
119
120 if (!has_persistent) {
121 persistent = false;
122 }
123
124 if (!has_disabled) {
125 disabled = false;
126 }
127
128 if (persistent) {
129 if (!bdrv_is_writable(bs)) {
130 error_setg(errp, "Cannot add a persistent bitmap to "
131 "read-only or inactive node '%s'",
132 bdrv_get_node_name(bs));
133 return;
134 }
135
136 if (!bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) {
137 return;
138 }
139 }
140
141 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
142 if (bitmap == NULL) {
143 return;
144 }
145
146 if (disabled) {
147 bdrv_disable_dirty_bitmap(bitmap);
148 }
149
150 bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
151 }
152
153 BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,
154 bool release,
155 BlockDriverState **bitmap_bs,
156 Error **errp)
157 {
158 BlockDriverState *bs;
159 BdrvDirtyBitmap *bitmap;
160
161 GLOBAL_STATE_CODE();
162
163 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
164 if (!bitmap || !bs) {
165 return NULL;
166 }
167
168 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
169 return NULL;
170 }
171
172 /* Dropping a bitmap needs no write access unless it is actually stored. */
173 if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
174 bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
175 {
176 return NULL;
177 }
178
179 if (release) {
180 bdrv_release_dirty_bitmap(bitmap);
181 }
182
183 if (bitmap_bs) {
184 *bitmap_bs = bs;
185 }
186
187 return release ? NULL : bitmap;
188 }
189
190 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
191 Error **errp)
192 {
193 block_dirty_bitmap_remove(node, name, true, NULL, errp);
194 }
195
196 /**
197 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
198 * immediately after a full backup operation.
199 */
200 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
201 Error **errp)
202 {
203 BdrvDirtyBitmap *bitmap;
204 BlockDriverState *bs;
205
206 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
207 if (!bitmap || !bs) {
208 return;
209 }
210
211 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
212 return;
213 }
214
215 bdrv_clear_dirty_bitmap(bitmap, NULL);
216 }
217
218 void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
219 Error **errp)
220 {
221 BlockDriverState *bs;
222 BdrvDirtyBitmap *bitmap;
223
224 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
225 if (!bitmap) {
226 return;
227 }
228
229 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
230 return;
231 }
232
233 bdrv_enable_dirty_bitmap(bitmap);
234 }
235
236 void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
237 Error **errp)
238 {
239 BlockDriverState *bs;
240 BdrvDirtyBitmap *bitmap;
241
242 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
243 if (!bitmap) {
244 return;
245 }
246
247 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
248 return;
249 }
250
251 bdrv_disable_dirty_bitmap(bitmap);
252 }
253
254 BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *dst_node,
255 const char *dst_bitmap,
256 BlockDirtyBitmapOrStrList *bms,
257 HBitmap **backup, Error **errp)
258 {
259 BlockDriverState *bs;
260 BdrvDirtyBitmap *dst, *src;
261 BlockDirtyBitmapOrStrList *lst;
262 const char *src_node, *src_bitmap;
263 HBitmap *local_backup = NULL;
264
265 GLOBAL_STATE_CODE();
266
267 dst = block_dirty_bitmap_lookup(dst_node, dst_bitmap, &bs, errp);
268 if (!dst) {
269 return NULL;
270 }
271
272 for (lst = bms; lst; lst = lst->next) {
273 switch (lst->value->type) {
274 case QTYPE_QSTRING:
275 src_bitmap = lst->value->u.local;
276 src = bdrv_find_dirty_bitmap(bs, src_bitmap);
277 if (!src) {
278 error_setg(errp, "Dirty bitmap '%s' not found", src_bitmap);
279 goto fail;
280 }
281 break;
282 case QTYPE_QDICT:
283 src_node = lst->value->u.external.node;
284 src_bitmap = lst->value->u.external.name;
285 src = block_dirty_bitmap_lookup(src_node, src_bitmap, NULL, errp);
286 if (!src) {
287 goto fail;
288 }
289 break;
290 default:
291 abort();
292 }
293
294 /* We do backup only for first merge operation */
295 if (!bdrv_merge_dirty_bitmap(dst, src,
296 local_backup ? NULL : &local_backup,
297 errp))
298 {
299 goto fail;
300 }
301 }
302
303 if (backup) {
304 *backup = local_backup;
305 } else {
306 hbitmap_free(local_backup);
307 }
308
309 return dst;
310
311 fail:
312 if (local_backup) {
313 bdrv_restore_dirty_bitmap(dst, local_backup);
314 }
315
316 return NULL;
317 }
318
319 void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
320 BlockDirtyBitmapOrStrList *bitmaps,
321 Error **errp)
322 {
323 block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
324 }