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
;
41 BlockdevOnError on_source_error
;
42 BlockdevOnError on_target_error
;
43 CoRwlock flush_rwlock
;
44 uint64_t sectors_read
;
46 QLIST_HEAD(, CowRequest
) inflight_reqs
;
49 /* See if in-flight requests overlap and wait for them to complete */
50 static void coroutine_fn
wait_for_overlapping_requests(BackupBlockJob
*job
,
59 QLIST_FOREACH(req
, &job
->inflight_reqs
, list
) {
60 if (end
> req
->start
&& start
< req
->end
) {
61 qemu_co_queue_wait(&req
->wait_queue
);
69 /* Keep track of an in-flight request */
70 static void cow_request_begin(CowRequest
*req
, BackupBlockJob
*job
,
71 int64_t start
, int64_t end
)
75 qemu_co_queue_init(&req
->wait_queue
);
76 QLIST_INSERT_HEAD(&job
->inflight_reqs
, req
, list
);
79 /* Forget about a completed request */
80 static void cow_request_end(CowRequest
*req
)
82 QLIST_REMOVE(req
, list
);
83 qemu_co_queue_restart_all(&req
->wait_queue
);
86 static int coroutine_fn
backup_do_cow(BlockDriverState
*bs
,
87 int64_t sector_num
, int nb_sectors
,
90 BackupBlockJob
*job
= (BackupBlockJob
*)bs
->job
;
91 CowRequest cow_request
;
93 QEMUIOVector bounce_qiov
;
94 void *bounce_buffer
= NULL
;
99 qemu_co_rwlock_rdlock(&job
->flush_rwlock
);
101 start
= sector_num
/ BACKUP_SECTORS_PER_CLUSTER
;
102 end
= DIV_ROUND_UP(sector_num
+ nb_sectors
, BACKUP_SECTORS_PER_CLUSTER
);
104 trace_backup_do_cow_enter(job
, start
, sector_num
, nb_sectors
);
106 wait_for_overlapping_requests(job
, start
, end
);
107 cow_request_begin(&cow_request
, job
, start
, end
);
109 for (; start
< end
; start
++) {
110 if (hbitmap_get(job
->bitmap
, start
)) {
111 trace_backup_do_cow_skip(job
, start
);
112 continue; /* already copied */
115 trace_backup_do_cow_process(job
, start
);
117 n
= MIN(BACKUP_SECTORS_PER_CLUSTER
,
118 job
->common
.len
/ BDRV_SECTOR_SIZE
-
119 start
* BACKUP_SECTORS_PER_CLUSTER
);
121 if (!bounce_buffer
) {
122 bounce_buffer
= qemu_blockalign(bs
, BACKUP_CLUSTER_SIZE
);
124 iov
.iov_base
= bounce_buffer
;
125 iov
.iov_len
= n
* BDRV_SECTOR_SIZE
;
126 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
128 ret
= bdrv_co_readv(bs
, start
* BACKUP_SECTORS_PER_CLUSTER
, n
,
131 trace_backup_do_cow_read_fail(job
, start
, ret
);
133 *error_is_read
= true;
138 if (buffer_is_zero(iov
.iov_base
, iov
.iov_len
)) {
139 ret
= bdrv_co_write_zeroes(job
->target
,
140 start
* BACKUP_SECTORS_PER_CLUSTER
, n
);
142 ret
= bdrv_co_writev(job
->target
,
143 start
* BACKUP_SECTORS_PER_CLUSTER
, n
,
147 trace_backup_do_cow_write_fail(job
, start
, ret
);
149 *error_is_read
= false;
154 hbitmap_set(job
->bitmap
, start
, 1);
156 /* Publish progress, guest I/O counts as progress too. Note that the
157 * offset field is an opaque progress value, it is not a disk offset.
159 job
->sectors_read
+= n
;
160 job
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
165 qemu_vfree(bounce_buffer
);
168 cow_request_end(&cow_request
);
170 trace_backup_do_cow_return(job
, sector_num
, nb_sectors
, ret
);
172 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
177 static int coroutine_fn
backup_before_write_notify(
178 NotifierWithReturn
*notifier
,
181 BdrvTrackedRequest
*req
= opaque
;
183 return backup_do_cow(req
->bs
, req
->sector_num
, req
->nb_sectors
, NULL
);
186 static void backup_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
188 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
191 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
194 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
197 static void backup_iostatus_reset(BlockJob
*job
)
199 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
201 bdrv_iostatus_reset(s
->target
);
204 static const BlockJobType backup_job_type
= {
205 .instance_size
= sizeof(BackupBlockJob
),
206 .job_type
= "backup",
207 .set_speed
= backup_set_speed
,
208 .iostatus_reset
= backup_iostatus_reset
,
211 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
212 bool read
, int error
)
215 return block_job_error_action(&job
->common
, job
->common
.bs
,
216 job
->on_source_error
, true, error
);
218 return block_job_error_action(&job
->common
, job
->target
,
219 job
->on_target_error
, false, error
);
223 static void coroutine_fn
backup_run(void *opaque
)
225 BackupBlockJob
*job
= opaque
;
226 BlockDriverState
*bs
= job
->common
.bs
;
227 BlockDriverState
*target
= job
->target
;
228 BlockdevOnError on_target_error
= job
->on_target_error
;
229 NotifierWithReturn before_write
= {
230 .notify
= backup_before_write_notify
,
235 QLIST_INIT(&job
->inflight_reqs
);
236 qemu_co_rwlock_init(&job
->flush_rwlock
);
239 end
= DIV_ROUND_UP(job
->common
.len
/ BDRV_SECTOR_SIZE
,
240 BACKUP_SECTORS_PER_CLUSTER
);
242 job
->bitmap
= hbitmap_alloc(end
, 0);
244 bdrv_set_enable_write_cache(target
, true);
245 bdrv_set_on_error(target
, on_target_error
, on_target_error
);
246 bdrv_iostatus_enable(target
);
248 bdrv_add_before_write_notifier(bs
, &before_write
);
250 for (; start
< end
; start
++) {
253 if (block_job_is_cancelled(&job
->common
)) {
257 /* we need to yield so that qemu_aio_flush() returns.
258 * (without, VM does not reboot)
260 if (job
->common
.speed
) {
261 uint64_t delay_ns
= ratelimit_calculate_delay(
262 &job
->limit
, job
->sectors_read
);
263 job
->sectors_read
= 0;
264 block_job_sleep_ns(&job
->common
, rt_clock
, delay_ns
);
266 block_job_sleep_ns(&job
->common
, rt_clock
, 0);
269 if (block_job_is_cancelled(&job
->common
)) {
273 ret
= backup_do_cow(bs
, start
* BACKUP_SECTORS_PER_CLUSTER
,
274 BACKUP_SECTORS_PER_CLUSTER
, &error_is_read
);
276 /* Depending on error action, fail now or retry cluster */
277 BlockErrorAction action
=
278 backup_error_action(job
, error_is_read
, -ret
);
279 if (action
== BDRV_ACTION_REPORT
) {
288 notifier_with_return_remove(&before_write
);
290 /* wait until pending backup_do_cow() calls have completed */
291 qemu_co_rwlock_wrlock(&job
->flush_rwlock
);
292 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
294 hbitmap_free(job
->bitmap
);
296 bdrv_iostatus_disable(target
);
299 block_job_completed(&job
->common
, ret
);
302 void backup_start(BlockDriverState
*bs
, BlockDriverState
*target
,
304 BlockdevOnError on_source_error
,
305 BlockdevOnError on_target_error
,
306 BlockDriverCompletionFunc
*cb
, void *opaque
,
315 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
316 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
317 !bdrv_iostatus_is_enabled(bs
)) {
318 error_set(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
322 len
= bdrv_getlength(bs
);
324 error_setg_errno(errp
, -len
, "unable to get length for '%s'",
325 bdrv_get_device_name(bs
));
329 BackupBlockJob
*job
= block_job_create(&backup_job_type
, bs
, speed
,
335 job
->on_source_error
= on_source_error
;
336 job
->on_target_error
= on_target_error
;
337 job
->target
= target
;
338 job
->common
.len
= len
;
339 job
->common
.co
= qemu_coroutine_create(backup_run
);
340 qemu_coroutine_enter(job
->common
.co
, job
);