4 * Copyright (C) 2013 Proxmox Server Solutions
7 * Dietmar Maurer (dietmar@proxmox.com)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
19 #include "block/block.h"
20 #include "block/block_int.h"
21 #include "block/blockjob.h"
22 #include "qemu/ratelimit.h"
24 #define BACKUP_CLUSTER_BITS 16
25 #define BACKUP_CLUSTER_SIZE (1 << BACKUP_CLUSTER_BITS)
26 #define BACKUP_SECTORS_PER_CLUSTER (BACKUP_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
28 #define SLICE_TIME 100000000ULL /* ns */
30 typedef struct CowRequest
{
33 QLIST_ENTRY(CowRequest
) list
;
34 CoQueue wait_queue
; /* coroutines blocked on this request */
37 typedef struct BackupBlockJob
{
39 BlockDriverState
*target
;
40 MirrorSyncMode sync_mode
;
42 BlockdevOnError on_source_error
;
43 BlockdevOnError on_target_error
;
44 CoRwlock flush_rwlock
;
45 uint64_t sectors_read
;
47 QLIST_HEAD(, CowRequest
) inflight_reqs
;
50 /* See if in-flight requests overlap and wait for them to complete */
51 static void coroutine_fn
wait_for_overlapping_requests(BackupBlockJob
*job
,
60 QLIST_FOREACH(req
, &job
->inflight_reqs
, list
) {
61 if (end
> req
->start
&& start
< req
->end
) {
62 qemu_co_queue_wait(&req
->wait_queue
);
70 /* Keep track of an in-flight request */
71 static void cow_request_begin(CowRequest
*req
, BackupBlockJob
*job
,
72 int64_t start
, int64_t end
)
76 qemu_co_queue_init(&req
->wait_queue
);
77 QLIST_INSERT_HEAD(&job
->inflight_reqs
, req
, list
);
80 /* Forget about a completed request */
81 static void cow_request_end(CowRequest
*req
)
83 QLIST_REMOVE(req
, list
);
84 qemu_co_queue_restart_all(&req
->wait_queue
);
87 static int coroutine_fn
backup_do_cow(BlockDriverState
*bs
,
88 int64_t sector_num
, int nb_sectors
,
91 BackupBlockJob
*job
= (BackupBlockJob
*)bs
->job
;
92 CowRequest cow_request
;
94 QEMUIOVector bounce_qiov
;
95 void *bounce_buffer
= NULL
;
100 qemu_co_rwlock_rdlock(&job
->flush_rwlock
);
102 start
= sector_num
/ BACKUP_SECTORS_PER_CLUSTER
;
103 end
= DIV_ROUND_UP(sector_num
+ nb_sectors
, BACKUP_SECTORS_PER_CLUSTER
);
105 trace_backup_do_cow_enter(job
, start
, sector_num
, nb_sectors
);
107 wait_for_overlapping_requests(job
, start
, end
);
108 cow_request_begin(&cow_request
, job
, start
, end
);
110 for (; start
< end
; start
++) {
111 if (hbitmap_get(job
->bitmap
, start
)) {
112 trace_backup_do_cow_skip(job
, start
);
113 continue; /* already copied */
116 trace_backup_do_cow_process(job
, start
);
118 n
= MIN(BACKUP_SECTORS_PER_CLUSTER
,
119 job
->common
.len
/ BDRV_SECTOR_SIZE
-
120 start
* BACKUP_SECTORS_PER_CLUSTER
);
122 if (!bounce_buffer
) {
123 bounce_buffer
= qemu_blockalign(bs
, BACKUP_CLUSTER_SIZE
);
125 iov
.iov_base
= bounce_buffer
;
126 iov
.iov_len
= n
* BDRV_SECTOR_SIZE
;
127 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
129 ret
= bdrv_co_readv(bs
, start
* BACKUP_SECTORS_PER_CLUSTER
, n
,
132 trace_backup_do_cow_read_fail(job
, start
, ret
);
134 *error_is_read
= true;
139 if (buffer_is_zero(iov
.iov_base
, iov
.iov_len
)) {
140 ret
= bdrv_co_write_zeroes(job
->target
,
141 start
* BACKUP_SECTORS_PER_CLUSTER
,
142 n
, BDRV_REQ_MAY_UNMAP
);
144 ret
= bdrv_co_writev(job
->target
,
145 start
* BACKUP_SECTORS_PER_CLUSTER
, n
,
149 trace_backup_do_cow_write_fail(job
, start
, ret
);
151 *error_is_read
= false;
156 hbitmap_set(job
->bitmap
, start
, 1);
158 /* Publish progress, guest I/O counts as progress too. Note that the
159 * offset field is an opaque progress value, it is not a disk offset.
161 job
->sectors_read
+= n
;
162 job
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
167 qemu_vfree(bounce_buffer
);
170 cow_request_end(&cow_request
);
172 trace_backup_do_cow_return(job
, sector_num
, nb_sectors
, ret
);
174 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
179 static int coroutine_fn
backup_before_write_notify(
180 NotifierWithReturn
*notifier
,
183 BdrvTrackedRequest
*req
= opaque
;
185 return backup_do_cow(req
->bs
, req
->sector_num
, req
->nb_sectors
, NULL
);
188 static void backup_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
190 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
193 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
196 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
199 static void backup_iostatus_reset(BlockJob
*job
)
201 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
203 bdrv_iostatus_reset(s
->target
);
206 static const BlockJobDriver backup_job_driver
= {
207 .instance_size
= sizeof(BackupBlockJob
),
208 .job_type
= BLOCK_JOB_TYPE_BACKUP
,
209 .set_speed
= backup_set_speed
,
210 .iostatus_reset
= backup_iostatus_reset
,
213 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
214 bool read
, int error
)
217 return block_job_error_action(&job
->common
, job
->common
.bs
,
218 job
->on_source_error
, true, error
);
220 return block_job_error_action(&job
->common
, job
->target
,
221 job
->on_target_error
, false, error
);
225 static void coroutine_fn
backup_run(void *opaque
)
227 BackupBlockJob
*job
= opaque
;
228 BlockDriverState
*bs
= job
->common
.bs
;
229 BlockDriverState
*target
= job
->target
;
230 BlockdevOnError on_target_error
= job
->on_target_error
;
231 NotifierWithReturn before_write
= {
232 .notify
= backup_before_write_notify
,
237 QLIST_INIT(&job
->inflight_reqs
);
238 qemu_co_rwlock_init(&job
->flush_rwlock
);
241 end
= DIV_ROUND_UP(job
->common
.len
/ BDRV_SECTOR_SIZE
,
242 BACKUP_SECTORS_PER_CLUSTER
);
244 job
->bitmap
= hbitmap_alloc(end
, 0);
246 bdrv_set_enable_write_cache(target
, true);
247 bdrv_set_on_error(target
, on_target_error
, on_target_error
);
248 bdrv_iostatus_enable(target
);
250 bdrv_add_before_write_notifier(bs
, &before_write
);
252 if (job
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
253 while (!block_job_is_cancelled(&job
->common
)) {
254 /* Yield until the job is cancelled. We just let our before_write
255 * notify callback service CoW requests. */
256 job
->common
.busy
= false;
257 qemu_coroutine_yield();
258 job
->common
.busy
= true;
261 /* Both FULL and TOP SYNC_MODE's require copying.. */
262 for (; start
< end
; start
++) {
265 if (block_job_is_cancelled(&job
->common
)) {
269 /* we need to yield so that qemu_aio_flush() returns.
270 * (without, VM does not reboot)
272 if (job
->common
.speed
) {
273 uint64_t delay_ns
= ratelimit_calculate_delay(
274 &job
->limit
, job
->sectors_read
);
275 job
->sectors_read
= 0;
276 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
278 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, 0);
281 if (block_job_is_cancelled(&job
->common
)) {
285 if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
289 /* Check to see if these blocks are already in the
292 for (i
= 0; i
< BACKUP_SECTORS_PER_CLUSTER
;) {
293 /* bdrv_is_allocated() only returns true/false based
294 * on the first set of sectors it comes across that
295 * are are all in the same state.
296 * For that reason we must verify each sector in the
297 * backup cluster length. We end up copying more than
298 * needed but at some point that is always the case. */
300 bdrv_is_allocated(bs
,
301 start
* BACKUP_SECTORS_PER_CLUSTER
+ i
,
302 BACKUP_SECTORS_PER_CLUSTER
- i
, &n
);
310 /* If the above loop never found any sectors that are in
311 * the topmost image, skip this backup. */
316 /* FULL sync mode we copy the whole drive. */
317 ret
= backup_do_cow(bs
, start
* BACKUP_SECTORS_PER_CLUSTER
,
318 BACKUP_SECTORS_PER_CLUSTER
, &error_is_read
);
320 /* Depending on error action, fail now or retry cluster */
321 BlockErrorAction action
=
322 backup_error_action(job
, error_is_read
, -ret
);
323 if (action
== BDRV_ACTION_REPORT
) {
333 notifier_with_return_remove(&before_write
);
335 /* wait until pending backup_do_cow() calls have completed */
336 qemu_co_rwlock_wrlock(&job
->flush_rwlock
);
337 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
339 hbitmap_free(job
->bitmap
);
341 bdrv_iostatus_disable(target
);
344 block_job_completed(&job
->common
, ret
);
347 void backup_start(BlockDriverState
*bs
, BlockDriverState
*target
,
348 int64_t speed
, MirrorSyncMode sync_mode
,
349 BlockdevOnError on_source_error
,
350 BlockdevOnError on_target_error
,
351 BlockDriverCompletionFunc
*cb
, void *opaque
,
360 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
361 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
362 !bdrv_iostatus_is_enabled(bs
)) {
363 error_set(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
367 len
= bdrv_getlength(bs
);
369 error_setg_errno(errp
, -len
, "unable to get length for '%s'",
370 bdrv_get_device_name(bs
));
374 BackupBlockJob
*job
= block_job_create(&backup_job_driver
, bs
, speed
,
380 job
->on_source_error
= on_source_error
;
381 job
->on_target_error
= on_target_error
;
382 job
->target
= target
;
383 job
->sync_mode
= sync_mode
;
384 job
->common
.len
= len
;
385 job
->common
.co
= qemu_coroutine_create(backup_run
);
386 qemu_coroutine_enter(job
->common
.co
, job
);