]> Git Repo - qemu.git/blob - block/dirty-bitmap.c
block/backup: add 'always' bitmap sync policy
[qemu.git] / block / dirty-bitmap.c
1 /*
2  * Block Dirty Bitmap
3  *
4  * Copyright (c) 2016-2017 Red Hat. Inc
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "trace.h"
27 #include "block/block_int.h"
28 #include "block/blockjob.h"
29
30 struct BdrvDirtyBitmap {
31     QemuMutex *mutex;
32     HBitmap *bitmap;            /* Dirty bitmap implementation */
33     HBitmap *meta;              /* Meta dirty bitmap */
34     bool busy;                  /* Bitmap is busy, it can't be used via QMP */
35     BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
36     char *name;                 /* Optional non-empty unique ID */
37     int64_t size;               /* Size of the bitmap, in bytes */
38     bool disabled;              /* Bitmap is disabled. It ignores all writes to
39                                    the device */
40     int active_iterators;       /* How many iterators are active */
41     bool readonly;              /* Bitmap is read-only. This field also
42                                    prevents the respective image from being
43                                    modified (i.e. blocks writes and discards).
44                                    Such operations must fail and both the image
45                                    and this bitmap must remain unchanged while
46                                    this flag is set. */
47     bool persistent;            /* bitmap must be saved to owner disk image */
48     bool inconsistent;          /* bitmap is persistent, but inconsistent.
49                                    It cannot be used at all in any way, except
50                                    a QMP user can remove it. */
51     bool migration;             /* Bitmap is selected for migration, it should
52                                    not be stored on the next inactivation
53                                    (persistent flag doesn't matter until next
54                                    invalidation).*/
55     QLIST_ENTRY(BdrvDirtyBitmap) list;
56 };
57
58 struct BdrvDirtyBitmapIter {
59     HBitmapIter hbi;
60     BdrvDirtyBitmap *bitmap;
61 };
62
63 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
64 {
65     qemu_mutex_lock(&bs->dirty_bitmap_mutex);
66 }
67
68 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
69 {
70     qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
71 }
72
73 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
74 {
75     qemu_mutex_lock(bitmap->mutex);
76 }
77
78 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
79 {
80     qemu_mutex_unlock(bitmap->mutex);
81 }
82
83 /* Called with BQL or dirty_bitmap lock taken.  */
84 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
85 {
86     BdrvDirtyBitmap *bm;
87
88     assert(name);
89     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
90         if (bm->name && !strcmp(name, bm->name)) {
91             return bm;
92         }
93     }
94     return NULL;
95 }
96
97 /* Called with BQL taken.  */
98 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
99                                           uint32_t granularity,
100                                           const char *name,
101                                           Error **errp)
102 {
103     int64_t bitmap_size;
104     BdrvDirtyBitmap *bitmap;
105
106     assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
107
108     if (name && bdrv_find_dirty_bitmap(bs, name)) {
109         error_setg(errp, "Bitmap already exists: %s", name);
110         return NULL;
111     }
112     bitmap_size = bdrv_getlength(bs);
113     if (bitmap_size < 0) {
114         error_setg_errno(errp, -bitmap_size, "could not get length of device");
115         errno = -bitmap_size;
116         return NULL;
117     }
118     bitmap = g_new0(BdrvDirtyBitmap, 1);
119     bitmap->mutex = &bs->dirty_bitmap_mutex;
120     bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
121     bitmap->size = bitmap_size;
122     bitmap->name = g_strdup(name);
123     bitmap->disabled = false;
124     bdrv_dirty_bitmaps_lock(bs);
125     QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
126     bdrv_dirty_bitmaps_unlock(bs);
127     return bitmap;
128 }
129
130 /* bdrv_create_meta_dirty_bitmap
131  *
132  * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
133  * when a dirty status bit in @bitmap is changed (either from reset to set or
134  * the other way around), its respective meta dirty bitmap bit will be marked
135  * dirty as well.
136  *
137  * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
138  * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
139  * track.
140  */
141 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
142                                    int chunk_size)
143 {
144     assert(!bitmap->meta);
145     qemu_mutex_lock(bitmap->mutex);
146     bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
147                                        chunk_size * BITS_PER_BYTE);
148     qemu_mutex_unlock(bitmap->mutex);
149 }
150
151 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
152 {
153     assert(bitmap->meta);
154     qemu_mutex_lock(bitmap->mutex);
155     hbitmap_free_meta(bitmap->bitmap);
156     bitmap->meta = NULL;
157     qemu_mutex_unlock(bitmap->mutex);
158 }
159
160 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
161 {
162     return bitmap->size;
163 }
164
165 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
166 {
167     return bitmap->name;
168 }
169
170 /* Called with BQL taken.  */
171 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
172 {
173     return bitmap->successor;
174 }
175
176 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
177 {
178     return bitmap->busy;
179 }
180
181 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
182 {
183     qemu_mutex_lock(bitmap->mutex);
184     bitmap->busy = busy;
185     qemu_mutex_unlock(bitmap->mutex);
186 }
187
188 /* Called with BQL taken.  */
189 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
190 {
191     return !bitmap->disabled;
192 }
193
194 /**
195  * bdrv_dirty_bitmap_status: This API is now deprecated.
196  * Called with BQL taken.
197  *
198  * A BdrvDirtyBitmap can be in four possible user-visible states:
199  * (1) Active:   successor is NULL, and disabled is false: full r/w mode
200  * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
201  *               guest writes are dropped, but monitor writes are possible,
202  *               through commands like merge and clear.
203  * (3) Frozen:   successor is not NULL.
204  *               A frozen bitmap cannot be renamed, deleted, cleared, set,
205  *               enabled, merged to, etc. A frozen bitmap can only abdicate()
206  *               or reclaim().
207  *               In this state, the anonymous successor bitmap may be either
208  *               Active and recording writes from the guest (e.g. backup jobs),
209  *               or it can be Disabled and not recording writes.
210  * (4) Locked:   Whether Active or Disabled, the user cannot modify this bitmap
211  *               in any way from the monitor.
212  * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
213  *                   is unusable by QEMU. It can be deleted to remove it from
214  *                   the qcow2.
215  */
216 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
217 {
218     if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
219         return DIRTY_BITMAP_STATUS_INCONSISTENT;
220     } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
221         return DIRTY_BITMAP_STATUS_FROZEN;
222     } else if (bdrv_dirty_bitmap_busy(bitmap)) {
223         return DIRTY_BITMAP_STATUS_LOCKED;
224     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
225         return DIRTY_BITMAP_STATUS_DISABLED;
226     } else {
227         return DIRTY_BITMAP_STATUS_ACTIVE;
228     }
229 }
230
231 /* Called with BQL taken.  */
232 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
233 {
234     return !bitmap->disabled || (bitmap->successor &&
235                                  !bitmap->successor->disabled);
236 }
237
238 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
239                             Error **errp)
240 {
241     if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
242         error_setg(errp, "Bitmap '%s' is currently in use by another"
243                    " operation and cannot be used", bitmap->name);
244         return -1;
245     }
246
247     if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
248         error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
249                    bitmap->name);
250         return -1;
251     }
252
253     if ((flags & BDRV_BITMAP_INCONSISTENT) &&
254         bdrv_dirty_bitmap_inconsistent(bitmap)) {
255         error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
256                    bitmap->name);
257         error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
258                           " this bitmap from disk");
259         return -1;
260     }
261
262     return 0;
263 }
264
265 /**
266  * Create a successor bitmap destined to replace this bitmap after an operation.
267  * Requires that the bitmap is not marked busy and has no successor.
268  * The successor will be enabled if the parent bitmap was.
269  * Called with BQL taken.
270  */
271 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
272                                        BdrvDirtyBitmap *bitmap, Error **errp)
273 {
274     uint64_t granularity;
275     BdrvDirtyBitmap *child;
276
277     if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
278         return -1;
279     }
280     if (bdrv_dirty_bitmap_has_successor(bitmap)) {
281         error_setg(errp, "Cannot create a successor for a bitmap that already "
282                    "has one");
283         return -1;
284     }
285
286     /* Create an anonymous successor */
287     granularity = bdrv_dirty_bitmap_granularity(bitmap);
288     child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
289     if (!child) {
290         return -1;
291     }
292
293     /* Successor will be on or off based on our current state. */
294     child->disabled = bitmap->disabled;
295     bitmap->disabled = true;
296
297     /* Install the successor and mark the parent as busy */
298     bitmap->successor = child;
299     bitmap->busy = true;
300     return 0;
301 }
302
303 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
304 {
305     bitmap->disabled = false;
306 }
307
308 /* Called with BQL taken. */
309 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
310 {
311     assert(bitmap->mutex == bitmap->successor->mutex);
312     qemu_mutex_lock(bitmap->mutex);
313     bdrv_enable_dirty_bitmap_locked(bitmap->successor);
314     qemu_mutex_unlock(bitmap->mutex);
315 }
316
317 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.  */
318 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
319 {
320     assert(!bitmap->active_iterators);
321     assert(!bdrv_dirty_bitmap_busy(bitmap));
322     assert(!bdrv_dirty_bitmap_has_successor(bitmap));
323     assert(!bitmap->meta);
324     QLIST_REMOVE(bitmap, list);
325     hbitmap_free(bitmap->bitmap);
326     g_free(bitmap->name);
327     g_free(bitmap);
328 }
329
330 /**
331  * For a bitmap with a successor, yield our name to the successor,
332  * delete the old bitmap, and return a handle to the new bitmap.
333  * Called with BQL taken.
334  */
335 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
336                                             BdrvDirtyBitmap *bitmap,
337                                             Error **errp)
338 {
339     char *name;
340     BdrvDirtyBitmap *successor = bitmap->successor;
341
342     if (successor == NULL) {
343         error_setg(errp, "Cannot relinquish control if "
344                    "there's no successor present");
345         return NULL;
346     }
347
348     name = bitmap->name;
349     bitmap->name = NULL;
350     successor->name = name;
351     bitmap->successor = NULL;
352     successor->persistent = bitmap->persistent;
353     bitmap->persistent = false;
354     bitmap->busy = false;
355     bdrv_release_dirty_bitmap(bs, bitmap);
356
357     return successor;
358 }
359
360 /**
361  * In cases of failure where we can no longer safely delete the parent,
362  * we may wish to re-join the parent and child/successor.
363  * The merged parent will be marked as not busy.
364  * The marged parent will be enabled if and only if the successor was enabled.
365  * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
366  */
367 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
368                                                   BdrvDirtyBitmap *parent,
369                                                   Error **errp)
370 {
371     BdrvDirtyBitmap *successor = parent->successor;
372
373     if (!successor) {
374         error_setg(errp, "Cannot reclaim a successor when none is present");
375         return NULL;
376     }
377
378     if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
379         error_setg(errp, "Merging of parent and successor bitmap failed");
380         return NULL;
381     }
382
383     parent->disabled = successor->disabled;
384     parent->busy = false;
385     bdrv_release_dirty_bitmap_locked(successor);
386     parent->successor = NULL;
387
388     return parent;
389 }
390
391 /* Called with BQL taken. */
392 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
393                                            BdrvDirtyBitmap *parent,
394                                            Error **errp)
395 {
396     BdrvDirtyBitmap *ret;
397
398     qemu_mutex_lock(parent->mutex);
399     ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
400     qemu_mutex_unlock(parent->mutex);
401
402     return ret;
403 }
404
405 /**
406  * Truncates _all_ bitmaps attached to a BDS.
407  * Called with BQL taken.
408  */
409 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
410 {
411     BdrvDirtyBitmap *bitmap;
412
413     bdrv_dirty_bitmaps_lock(bs);
414     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
415         assert(!bdrv_dirty_bitmap_busy(bitmap));
416         assert(!bdrv_dirty_bitmap_has_successor(bitmap));
417         assert(!bitmap->active_iterators);
418         hbitmap_truncate(bitmap->bitmap, bytes);
419         bitmap->size = bytes;
420     }
421     bdrv_dirty_bitmaps_unlock(bs);
422 }
423
424 /* Called with BQL taken.  */
425 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
426 {
427     bdrv_dirty_bitmaps_lock(bs);
428     bdrv_release_dirty_bitmap_locked(bitmap);
429     bdrv_dirty_bitmaps_unlock(bs);
430 }
431
432 /**
433  * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
434  * There must not be any busy bitmaps attached.
435  * This function does not remove persistent bitmaps from the storage.
436  * Called with BQL taken.
437  */
438 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
439 {
440     BdrvDirtyBitmap *bm, *next;
441
442     bdrv_dirty_bitmaps_lock(bs);
443     QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
444         if (bdrv_dirty_bitmap_name(bm)) {
445             bdrv_release_dirty_bitmap_locked(bm);
446         }
447     }
448     bdrv_dirty_bitmaps_unlock(bs);
449 }
450
451 /**
452  * Remove persistent dirty bitmap from the storage if it exists.
453  * Absence of bitmap is not an error, because we have the following scenario:
454  * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
455  * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
456  * not fail.
457  * This function doesn't release corresponding BdrvDirtyBitmap.
458  */
459 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
460                                          const char *name,
461                                          Error **errp)
462 {
463     if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
464         bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
465     }
466 }
467
468 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
469 {
470     bdrv_dirty_bitmap_lock(bitmap);
471     bitmap->disabled = true;
472     bdrv_dirty_bitmap_unlock(bitmap);
473 }
474
475 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
476 {
477     bdrv_dirty_bitmap_lock(bitmap);
478     bdrv_enable_dirty_bitmap_locked(bitmap);
479     bdrv_dirty_bitmap_unlock(bitmap);
480 }
481
482 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
483 {
484     BdrvDirtyBitmap *bm;
485     BlockDirtyInfoList *list = NULL;
486     BlockDirtyInfoList **plist = &list;
487
488     bdrv_dirty_bitmaps_lock(bs);
489     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
490         BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
491         BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
492         info->count = bdrv_get_dirty_count(bm);
493         info->granularity = bdrv_dirty_bitmap_granularity(bm);
494         info->has_name = !!bm->name;
495         info->name = g_strdup(bm->name);
496         info->status = bdrv_dirty_bitmap_status(bm);
497         info->recording = bdrv_dirty_bitmap_recording(bm);
498         info->busy = bdrv_dirty_bitmap_busy(bm);
499         info->persistent = bm->persistent;
500         info->has_inconsistent = bm->inconsistent;
501         info->inconsistent = bm->inconsistent;
502         entry->value = info;
503         *plist = entry;
504         plist = &entry->next;
505     }
506     bdrv_dirty_bitmaps_unlock(bs);
507
508     return list;
509 }
510
511 /* Called within bdrv_dirty_bitmap_lock..unlock */
512 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
513 {
514     return hbitmap_get(bitmap->bitmap, offset);
515 }
516
517 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
518 {
519     bool ret;
520     bdrv_dirty_bitmap_lock(bitmap);
521     ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
522     bdrv_dirty_bitmap_unlock(bitmap);
523
524     return ret;
525 }
526
527 /**
528  * Chooses a default granularity based on the existing cluster size,
529  * but clamped between [4K, 64K]. Defaults to 64K in the case that there
530  * is no cluster size information available.
531  */
532 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
533 {
534     BlockDriverInfo bdi;
535     uint32_t granularity;
536
537     if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
538         granularity = MAX(4096, bdi.cluster_size);
539         granularity = MIN(65536, granularity);
540     } else {
541         granularity = 65536;
542     }
543
544     return granularity;
545 }
546
547 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
548 {
549     return 1U << hbitmap_granularity(bitmap->bitmap);
550 }
551
552 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
553 {
554     BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
555     hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
556     iter->bitmap = bitmap;
557     bitmap->active_iterators++;
558     return iter;
559 }
560
561 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
562 {
563     BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
564     hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
565     iter->bitmap = bitmap;
566     bitmap->active_iterators++;
567     return iter;
568 }
569
570 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
571 {
572     if (!iter) {
573         return;
574     }
575     assert(iter->bitmap->active_iterators > 0);
576     iter->bitmap->active_iterators--;
577     g_free(iter);
578 }
579
580 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
581 {
582     return hbitmap_iter_next(&iter->hbi);
583 }
584
585 /* Called within bdrv_dirty_bitmap_lock..unlock */
586 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
587                                   int64_t offset, int64_t bytes)
588 {
589     assert(!bdrv_dirty_bitmap_readonly(bitmap));
590     hbitmap_set(bitmap->bitmap, offset, bytes);
591 }
592
593 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
594                            int64_t offset, int64_t bytes)
595 {
596     bdrv_dirty_bitmap_lock(bitmap);
597     bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
598     bdrv_dirty_bitmap_unlock(bitmap);
599 }
600
601 /* Called within bdrv_dirty_bitmap_lock..unlock */
602 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
603                                     int64_t offset, int64_t bytes)
604 {
605     assert(!bdrv_dirty_bitmap_readonly(bitmap));
606     hbitmap_reset(bitmap->bitmap, offset, bytes);
607 }
608
609 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
610                              int64_t offset, int64_t bytes)
611 {
612     bdrv_dirty_bitmap_lock(bitmap);
613     bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
614     bdrv_dirty_bitmap_unlock(bitmap);
615 }
616
617 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
618 {
619     assert(!bdrv_dirty_bitmap_readonly(bitmap));
620     bdrv_dirty_bitmap_lock(bitmap);
621     if (!out) {
622         hbitmap_reset_all(bitmap->bitmap);
623     } else {
624         HBitmap *backup = bitmap->bitmap;
625         bitmap->bitmap = hbitmap_alloc(bitmap->size,
626                                        hbitmap_granularity(backup));
627         *out = backup;
628     }
629     bdrv_dirty_bitmap_unlock(bitmap);
630 }
631
632 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
633 {
634     HBitmap *tmp = bitmap->bitmap;
635     assert(!bdrv_dirty_bitmap_readonly(bitmap));
636     bitmap->bitmap = backup;
637     hbitmap_free(tmp);
638 }
639
640 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
641                                               uint64_t offset, uint64_t bytes)
642 {
643     return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
644 }
645
646 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
647 {
648     return hbitmap_serialization_align(bitmap->bitmap);
649 }
650
651 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
652                                       uint8_t *buf, uint64_t offset,
653                                       uint64_t bytes)
654 {
655     hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
656 }
657
658 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
659                                         uint8_t *buf, uint64_t offset,
660                                         uint64_t bytes, bool finish)
661 {
662     hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
663 }
664
665 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
666                                           uint64_t offset, uint64_t bytes,
667                                           bool finish)
668 {
669     hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
670 }
671
672 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
673                                         uint64_t offset, uint64_t bytes,
674                                         bool finish)
675 {
676     hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
677 }
678
679 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
680 {
681     hbitmap_deserialize_finish(bitmap->bitmap);
682 }
683
684 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
685 {
686     BdrvDirtyBitmap *bitmap;
687
688     if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
689         return;
690     }
691
692     bdrv_dirty_bitmaps_lock(bs);
693     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
694         if (!bdrv_dirty_bitmap_enabled(bitmap)) {
695             continue;
696         }
697         assert(!bdrv_dirty_bitmap_readonly(bitmap));
698         hbitmap_set(bitmap->bitmap, offset, bytes);
699     }
700     bdrv_dirty_bitmaps_unlock(bs);
701 }
702
703 /**
704  * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
705  */
706 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
707 {
708     hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
709 }
710
711 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
712 {
713     return hbitmap_count(bitmap->bitmap);
714 }
715
716 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
717 {
718     return hbitmap_count(bitmap->meta);
719 }
720
721 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
722 {
723     return bitmap->readonly;
724 }
725
726 /* Called with BQL taken. */
727 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
728 {
729     qemu_mutex_lock(bitmap->mutex);
730     bitmap->readonly = value;
731     qemu_mutex_unlock(bitmap->mutex);
732 }
733
734 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
735 {
736     BdrvDirtyBitmap *bm;
737     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
738         if (bm->readonly) {
739             return true;
740         }
741     }
742
743     return false;
744 }
745
746 /* Called with BQL taken. */
747 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
748 {
749     qemu_mutex_lock(bitmap->mutex);
750     bitmap->persistent = persistent;
751     qemu_mutex_unlock(bitmap->mutex);
752 }
753
754 /* Called with BQL taken. */
755 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
756 {
757     qemu_mutex_lock(bitmap->mutex);
758     assert(bitmap->persistent == true);
759     bitmap->inconsistent = true;
760     bitmap->disabled = true;
761     qemu_mutex_unlock(bitmap->mutex);
762 }
763
764 /* Called with BQL taken. */
765 void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap *bitmap, bool migration)
766 {
767     qemu_mutex_lock(bitmap->mutex);
768     bitmap->migration = migration;
769     qemu_mutex_unlock(bitmap->mutex);
770 }
771
772 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
773 {
774     return bitmap->persistent && !bitmap->migration;
775 }
776
777 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
778 {
779     return bitmap->inconsistent;
780 }
781
782 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
783 {
784     BdrvDirtyBitmap *bm;
785     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
786         if (bm->persistent && !bm->readonly && !bm->migration) {
787             return true;
788         }
789     }
790
791     return false;
792 }
793
794 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
795                                         BdrvDirtyBitmap *bitmap)
796 {
797     return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
798                             QLIST_NEXT(bitmap, list);
799 }
800
801 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
802 {
803     return hbitmap_sha256(bitmap->bitmap, errp);
804 }
805
806 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
807                                     uint64_t bytes)
808 {
809     return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
810 }
811
812 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
813                                        uint64_t *offset, uint64_t *bytes)
814 {
815     return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
816 }
817
818 /**
819  * bdrv_merge_dirty_bitmap: merge src into dest.
820  * Ensures permissions on bitmaps are reasonable; use for public API.
821  *
822  * @backup: If provided, make a copy of dest here prior to merge.
823  */
824 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
825                              HBitmap **backup, Error **errp)
826 {
827     bool ret;
828
829     qemu_mutex_lock(dest->mutex);
830     if (src->mutex != dest->mutex) {
831         qemu_mutex_lock(src->mutex);
832     }
833
834     if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
835         goto out;
836     }
837
838     if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
839         goto out;
840     }
841
842     if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
843         error_setg(errp, "Bitmaps are incompatible and can't be merged");
844         goto out;
845     }
846
847     ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
848     assert(ret);
849
850 out:
851     qemu_mutex_unlock(dest->mutex);
852     if (src->mutex != dest->mutex) {
853         qemu_mutex_unlock(src->mutex);
854     }
855 }
856
857 /**
858  * bdrv_dirty_bitmap_merge_internal: merge src into dest.
859  * Does NOT check bitmap permissions; not suitable for use as public API.
860  *
861  * @backup: If provided, make a copy of dest here prior to merge.
862  * @lock: If true, lock and unlock bitmaps on the way in/out.
863  * returns true if the merge succeeded; false if unattempted.
864  */
865 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
866                                       const BdrvDirtyBitmap *src,
867                                       HBitmap **backup,
868                                       bool lock)
869 {
870     bool ret;
871
872     assert(!bdrv_dirty_bitmap_readonly(dest));
873     assert(!bdrv_dirty_bitmap_inconsistent(dest));
874     assert(!bdrv_dirty_bitmap_inconsistent(src));
875
876     if (lock) {
877         qemu_mutex_lock(dest->mutex);
878         if (src->mutex != dest->mutex) {
879             qemu_mutex_lock(src->mutex);
880         }
881     }
882
883     if (backup) {
884         *backup = dest->bitmap;
885         dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
886         ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
887     } else {
888         ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
889     }
890
891     if (lock) {
892         qemu_mutex_unlock(dest->mutex);
893         if (src->mutex != dest->mutex) {
894             qemu_mutex_unlock(src->mutex);
895         }
896     }
897
898     return ret;
899 }
This page took 0.074361 seconds and 4 git commands to generate.