Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/monitor/bitmap-qmp-cmds.c
Line
Count
Source
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
0
{
58
0
    BlockDriverState *bs;
59
0
    BdrvDirtyBitmap *bitmap;
60
61
0
    GLOBAL_STATE_CODE();
62
63
0
    if (!node) {
64
0
        error_setg(errp, "Node cannot be NULL");
65
0
        return NULL;
66
0
    }
67
0
    if (!name) {
68
0
        error_setg(errp, "Bitmap name cannot be NULL");
69
0
        return NULL;
70
0
    }
71
0
    bs = bdrv_lookup_bs(node, node, NULL);
72
0
    if (!bs) {
73
0
        error_setg(errp, "Node '%s' not found", node);
74
0
        return NULL;
75
0
    }
76
77
0
    bitmap = bdrv_find_dirty_bitmap(bs, name);
78
0
    if (!bitmap) {
79
0
        error_setg(errp, "Dirty bitmap '%s' not found", name);
80
0
        return NULL;
81
0
    }
82
83
0
    if (pbs) {
84
0
        *pbs = bs;
85
0
    }
86
87
0
    return bitmap;
88
0
}
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
0
{
96
0
    BlockDriverState *bs;
97
0
    BdrvDirtyBitmap *bitmap;
98
99
0
    if (!name || name[0] == '\0') {
100
0
        error_setg(errp, "Bitmap name cannot be empty");
101
0
        return;
102
0
    }
103
104
0
    bs = bdrv_lookup_bs(node, node, errp);
105
0
    if (!bs) {
106
0
        return;
107
0
    }
108
109
0
    if (has_granularity) {
110
0
        if (granularity < 512 || !is_power_of_2(granularity)) {
111
0
            error_setg(errp, "Granularity must be power of 2 "
112
0
                             "and at least 512");
113
0
            return;
114
0
        }
115
0
    } else {
116
        /* Default to cluster size, if available: */
117
0
        granularity = bdrv_get_default_bitmap_granularity(bs);
118
0
    }
119
120
0
    if (!has_persistent) {
121
0
        persistent = false;
122
0
    }
123
124
0
    if (!has_disabled) {
125
0
        disabled = false;
126
0
    }
127
128
0
    if (persistent) {
129
0
        if (!bdrv_is_writable(bs)) {
130
0
            error_setg(errp, "Cannot add a persistent bitmap to "
131
0
                       "read-only or inactive node '%s'",
132
0
                       bdrv_get_node_name(bs));
133
0
            return;
134
0
        }
135
136
0
        if (!bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) {
137
0
            return;
138
0
        }
139
0
    }
140
141
0
    bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
142
0
    if (bitmap == NULL) {
143
0
        return;
144
0
    }
145
146
0
    if (disabled) {
147
0
        bdrv_disable_dirty_bitmap(bitmap);
148
0
    }
149
150
0
    bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
151
0
}
152
153
BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name,
154
                                           bool release,
155
                                           BlockDriverState **bitmap_bs,
156
                                           Error **errp)
157
0
{
158
0
    BlockDriverState *bs;
159
0
    BdrvDirtyBitmap *bitmap;
160
161
0
    GLOBAL_STATE_CODE();
162
163
0
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
164
0
    if (!bitmap || !bs) {
165
0
        return NULL;
166
0
    }
167
168
0
    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
169
0
        return NULL;
170
0
    }
171
172
    /* Dropping a bitmap needs no write access unless it is actually stored. */
173
0
    if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
174
0
        bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
175
0
    {
176
0
        return NULL;
177
0
    }
178
179
0
    if (release) {
180
0
        bdrv_release_dirty_bitmap(bitmap);
181
0
    }
182
183
0
    if (bitmap_bs) {
184
0
        *bitmap_bs = bs;
185
0
    }
186
187
0
    return release ? NULL : bitmap;
188
0
}
189
190
void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
191
                                   Error **errp)
192
0
{
193
0
    block_dirty_bitmap_remove(node, name, true, NULL, errp);
194
0
}
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
0
{
203
0
    BdrvDirtyBitmap *bitmap;
204
0
    BlockDriverState *bs;
205
206
0
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
207
0
    if (!bitmap || !bs) {
208
0
        return;
209
0
    }
210
211
0
    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
212
0
        return;
213
0
    }
214
215
0
    bdrv_clear_dirty_bitmap(bitmap, NULL);
216
0
}
217
218
void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
219
                                   Error **errp)
220
0
{
221
0
    BlockDriverState *bs;
222
0
    BdrvDirtyBitmap *bitmap;
223
224
0
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
225
0
    if (!bitmap) {
226
0
        return;
227
0
    }
228
229
0
    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
230
0
        return;
231
0
    }
232
233
0
    bdrv_enable_dirty_bitmap(bitmap);
234
0
}
235
236
void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
237
                                    Error **errp)
238
0
{
239
0
    BlockDriverState *bs;
240
0
    BdrvDirtyBitmap *bitmap;
241
242
0
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
243
0
    if (!bitmap) {
244
0
        return;
245
0
    }
246
247
0
    if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
248
0
        return;
249
0
    }
250
251
0
    bdrv_disable_dirty_bitmap(bitmap);
252
0
}
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
0
{
259
0
    BlockDriverState *bs;
260
0
    BdrvDirtyBitmap *dst, *src;
261
0
    BlockDirtyBitmapOrStrList *lst;
262
0
    const char *src_node, *src_bitmap;
263
0
    HBitmap *local_backup = NULL;
264
265
0
    GLOBAL_STATE_CODE();
266
267
0
    dst = block_dirty_bitmap_lookup(dst_node, dst_bitmap, &bs, errp);
268
0
    if (!dst) {
269
0
        return NULL;
270
0
    }
271
272
0
    for (lst = bms; lst; lst = lst->next) {
273
0
        switch (lst->value->type) {
274
0
        case QTYPE_QSTRING:
275
0
            src_bitmap = lst->value->u.local;
276
0
            src = bdrv_find_dirty_bitmap(bs, src_bitmap);
277
0
            if (!src) {
278
0
                error_setg(errp, "Dirty bitmap '%s' not found", src_bitmap);
279
0
                goto fail;
280
0
            }
281
0
            break;
282
0
        case QTYPE_QDICT:
283
0
            src_node = lst->value->u.external.node;
284
0
            src_bitmap = lst->value->u.external.name;
285
0
            src = block_dirty_bitmap_lookup(src_node, src_bitmap, NULL, errp);
286
0
            if (!src) {
287
0
                goto fail;
288
0
            }
289
0
            break;
290
0
        default:
291
0
            abort();
292
0
        }
293
294
        /* We do backup only for first merge operation */
295
0
        if (!bdrv_merge_dirty_bitmap(dst, src,
296
0
                                     local_backup ? NULL : &local_backup,
297
0
                                     errp))
298
0
        {
299
0
            goto fail;
300
0
        }
301
0
    }
302
303
0
    if (backup) {
304
0
        *backup = local_backup;
305
0
    } else {
306
0
        hbitmap_free(local_backup);
307
0
    }
308
309
0
    return dst;
310
311
0
fail:
312
0
    if (local_backup) {
313
0
        bdrv_restore_dirty_bitmap(dst, local_backup);
314
0
    }
315
316
0
    return NULL;
317
0
}
318
319
void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
320
                                  BlockDirtyBitmapOrStrList *bitmaps,
321
                                  Error **errp)
322
0
{
323
    block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
324
0
}