block: Introduce reference count for dirty bitmaps

A dirty bitmap may be created by user via QMP, then reference by other
QMP commands, such as backup. We need reference count machanism to
manage the lifecycle of dirty bitmap.

This adds bdrv_release_dirty_bitmap and changes
bdrv_release_dirty_bitmap to only free the structure when refcnt goes to
0.

Signed-off-by: Fam Zheng <famz@redhat.com>
qapi: Add optional field "name" to block dirty bitmap

This field will be set for user created dirty bitmap. Also pass in an
error pointer to bdrv_create_dirty_bitmap, so when a name is already
taken on this BDS, it can report an error message. This is not global
check, two BDSes can have dirty bitmap with a common name.

Implemented bdrv_find_dirty_bitmap to find a dirty bitmap by name, will
be used later when other QMP commands want to reference dirty bitmap by
name.

Signed-off-by: Fam Zheng <famz@redhat.com>
block: Add bdrv_dirty_bitmap_make_anon

This will unset the name of dirty bitmap.

Signed-off-by: Fam Zheng <famz@redhat.com>
qmp: Add dirty-bitmap-add and dirty-bitmap-remove

The new command pair is added to manage user created dirty bitmap. The
dirty bitmap's name is mandatory and must be unique for the same device,
but different devices can have bitmaps with the same names.

Signed-off-by: Fam Zheng <famz@redhat.com>
block: Handle error of bdrv_getlength in bdrv_create_dirty_bitmap

bdrv_getlength could fail, check the return value before using it.

Signed-off-by: Fam Zheng <famz@redhat.com>
block: Introduce bdrv_dirty_bitmap_granularity()

This returns the granularity (in sectors) of dirty bitmap.

Signed-off-by: Fam Zheng <famz@redhat.com>
block: Add support of "dirty-bitmap" sync mode

For "dirty-bitmap" sync mode, the block job will iterate through the
given dirty bitmap to decide if a sector needs backup (backup all the
dirty clusters and skip clean ones), just as allocation conditions of
"top" sync mode.

If the dirty bitmap is updated (because of guest writes) while the block
job is scanning it, an extra time of copy of written sectors could
happen because of overlapping of write interception and dirty bitmap.

User should build a transaction to:
 - Deactive the dirty bitmap.
 - Start a backup job with the dirty bitmap.
 - Create another dirty bitmap for continuity of incremental tracking.

With coming dirty bitmap transaction patches.

Signed-off-by: Fam Zheng <famz@redhat.com>
qmp: Add dirty-bitmap-disable command

This will put the dirty bitmap into a disabled state and no more
writes will be tracked.

It will be used before backup or writing to persistent file.

Signed-off-by: Fam Zheng <famz@redhat.com>
qapi: Add transaction support to dirty-bitmap-{add,disable}

This adds dirty-bitmap-add and dirty-bitmap-disable to transactions.
With this, user can stop a dirty bitmap, start backup of it, and start
another dirty bitmap atomically, so that the dirty bitmap is tracked
incrementally and we don't miss any write.

Signed-off-by: Fam Zheng <famz@redhat.com>