xref: /qemu/include/block/block-copy.h (revision e1ecf8c8)
1 /*
2  * block_copy API
3  *
4  * Copyright (C) 2013 Proxmox Server Solutions
5  * Copyright (c) 2019 Virtuozzo International GmbH.
6  *
7  * Authors:
8  *  Dietmar Maurer (dietmar@proxmox.com)
9  *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2 or later.
12  * See the COPYING file in the top-level directory.
13  */
14 
15 #ifndef BLOCK_COPY_H
16 #define BLOCK_COPY_H
17 
18 #include "block/block.h"
19 
20 typedef struct BlockCopyInFlightReq {
21     int64_t start_byte;
22     int64_t end_byte;
23     QLIST_ENTRY(BlockCopyInFlightReq) list;
24     CoQueue wait_queue; /* coroutines blocked on this request */
25 } BlockCopyInFlightReq;
26 
27 typedef void (*ProgressBytesCallbackFunc)(int64_t bytes, void *opaque);
28 typedef void (*ProgressResetCallbackFunc)(void *opaque);
29 typedef struct BlockCopyState {
30     /*
31      * BdrvChild objects are not owned or managed by block-copy. They are
32      * provided by block-copy user and user is responsible for appropriate
33      * permissions on these children.
34      */
35     BdrvChild *source;
36     BdrvChild *target;
37     BdrvDirtyBitmap *copy_bitmap;
38     int64_t cluster_size;
39     bool use_copy_range;
40     int64_t copy_range_size;
41     uint64_t len;
42     QLIST_HEAD(, BlockCopyInFlightReq) inflight_reqs;
43 
44     BdrvRequestFlags write_flags;
45 
46     /*
47      * skip_unallocated:
48      *
49      * Used by sync=top jobs, which first scan the source node for unallocated
50      * areas and clear them in the copy_bitmap.  During this process, the bitmap
51      * is thus not fully initialized: It may still have bits set for areas that
52      * are unallocated and should actually not be copied.
53      *
54      * This is indicated by skip_unallocated.
55      *
56      * In this case, block_copy() will query the source’s allocation status,
57      * skip unallocated regions, clear them in the copy_bitmap, and invoke
58      * block_copy_reset_unallocated() every time it does.
59      */
60     bool skip_unallocated;
61 
62     /* progress_bytes_callback: called when some copying progress is done. */
63     ProgressBytesCallbackFunc progress_bytes_callback;
64 
65     /*
66      * progress_reset_callback: called when some bytes reset from copy_bitmap
67      * (see @skip_unallocated above). The callee is assumed to recalculate how
68      * many bytes remain based on the dirty bit count of copy_bitmap.
69      */
70     ProgressResetCallbackFunc progress_reset_callback;
71     void *progress_opaque;
72 } BlockCopyState;
73 
74 BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target,
75                                      int64_t cluster_size,
76                                      BdrvRequestFlags write_flags,
77                                      Error **errp);
78 
79 void block_copy_set_callbacks(
80         BlockCopyState *s,
81         ProgressBytesCallbackFunc progress_bytes_callback,
82         ProgressResetCallbackFunc progress_reset_callback,
83         void *progress_opaque);
84 
85 void block_copy_state_free(BlockCopyState *s);
86 
87 int64_t block_copy_reset_unallocated(BlockCopyState *s,
88                                      int64_t offset, int64_t *count);
89 
90 int coroutine_fn block_copy(BlockCopyState *s, int64_t start, uint64_t bytes,
91                             bool *error_is_read);
92 
93 #endif /* BLOCK_COPY_H */
94