2 * backup-top filter driver
4 * The driver performs Copy-Before-Write (CBW) operation: it is injected above
5 * some node, and before each write it copies _old_ data to the target node.
7 * Copyright (c) 2018-2019 Virtuozzo International GmbH.
10 * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
28 #include "sysemu/block-backend.h"
29 #include "qemu/cutils.h"
30 #include "qapi/error.h"
31 #include "block/block_int.h"
32 #include "block/qdict.h"
33 #include "block/block-copy.h"
35 #include "block/backup-top.h"
37 typedef struct BDRVBackupTopState
{
44 static coroutine_fn
int backup_top_co_preadv(
45 BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
46 QEMUIOVector
*qiov
, int flags
)
48 BDRVBackupTopState
*s
= bs
->opaque
;
54 return bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, flags
);
57 static coroutine_fn
int backup_top_cbw(BlockDriverState
*bs
, uint64_t offset
,
58 uint64_t bytes
, BdrvRequestFlags flags
)
60 BDRVBackupTopState
*s
= bs
->opaque
;
67 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
71 off
= QEMU_ALIGN_DOWN(offset
, s
->cluster_size
);
72 end
= QEMU_ALIGN_UP(offset
+ bytes
, s
->cluster_size
);
74 return block_copy(s
->bcs
, off
, end
- off
, true);
77 static int coroutine_fn
backup_top_co_pdiscard(BlockDriverState
*bs
,
78 int64_t offset
, int bytes
)
80 int ret
= backup_top_cbw(bs
, offset
, bytes
, 0);
85 return bdrv_co_pdiscard(bs
->backing
, offset
, bytes
);
88 static int coroutine_fn
backup_top_co_pwrite_zeroes(BlockDriverState
*bs
,
89 int64_t offset
, int bytes
, BdrvRequestFlags flags
)
91 int ret
= backup_top_cbw(bs
, offset
, bytes
, flags
);
96 return bdrv_co_pwrite_zeroes(bs
->backing
, offset
, bytes
, flags
);
99 static coroutine_fn
int backup_top_co_pwritev(BlockDriverState
*bs
,
102 QEMUIOVector
*qiov
, int flags
)
104 int ret
= backup_top_cbw(bs
, offset
, bytes
, flags
);
109 return bdrv_co_pwritev(bs
->backing
, offset
, bytes
, qiov
, flags
);
112 static int coroutine_fn
backup_top_co_flush(BlockDriverState
*bs
)
118 return bdrv_co_flush(bs
->backing
->bs
);
121 static void backup_top_refresh_filename(BlockDriverState
*bs
)
123 if (bs
->backing
== NULL
) {
125 * we can be here after failed bdrv_attach_child in
126 * bdrv_set_backing_hd
130 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
),
131 bs
->backing
->bs
->filename
);
134 static void backup_top_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
136 BlockReopenQueue
*reopen_queue
,
137 uint64_t perm
, uint64_t shared
,
138 uint64_t *nperm
, uint64_t *nshared
)
140 BDRVBackupTopState
*s
= bs
->opaque
;
144 * The filter node may be in process of bdrv_append(), which firstly do
145 * bdrv_set_backing_hd() and then bdrv_replace_node(). This means that
146 * we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So,
147 * let's require nothing during bdrv_append() and refresh permissions
148 * after it (see bdrv_backup_top_append()).
151 *nshared
= BLK_PERM_ALL
;
155 if (!(role
& BDRV_CHILD_FILTERED
)) {
159 * Share write to target (child_file), to not interfere
160 * with guest writes to its disk which may be in target backing chain.
161 * Can't resize during a backup block job because we check the size
164 *nshared
= BLK_PERM_ALL
& ~BLK_PERM_RESIZE
;
165 *nperm
= BLK_PERM_WRITE
;
168 bdrv_default_perms(bs
, c
, role
, reopen_queue
,
169 perm
, shared
, nperm
, nshared
);
171 if (perm
& BLK_PERM_WRITE
) {
172 *nperm
= *nperm
| BLK_PERM_CONSISTENT_READ
;
174 *nshared
&= ~(BLK_PERM_WRITE
| BLK_PERM_RESIZE
);
178 BlockDriver bdrv_backup_top_filter
= {
179 .format_name
= "backup-top",
180 .instance_size
= sizeof(BDRVBackupTopState
),
182 .bdrv_co_preadv
= backup_top_co_preadv
,
183 .bdrv_co_pwritev
= backup_top_co_pwritev
,
184 .bdrv_co_pwrite_zeroes
= backup_top_co_pwrite_zeroes
,
185 .bdrv_co_pdiscard
= backup_top_co_pdiscard
,
186 .bdrv_co_flush
= backup_top_co_flush
,
188 .bdrv_refresh_filename
= backup_top_refresh_filename
,
190 .bdrv_child_perm
= backup_top_child_perm
,
195 BlockDriverState
*bdrv_backup_top_append(BlockDriverState
*source
,
196 BlockDriverState
*target
,
197 const char *filter_node_name
,
198 uint64_t cluster_size
,
200 BdrvRequestFlags write_flags
,
201 BlockCopyState
**bcs
,
206 BDRVBackupTopState
*state
;
207 BlockDriverState
*top
;
208 bool appended
= false;
210 assert(source
->total_sectors
== target
->total_sectors
);
212 top
= bdrv_new_open_driver(&bdrv_backup_top_filter
, filter_node_name
,
219 top
->total_sectors
= source
->total_sectors
;
220 top
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
|
221 (BDRV_REQ_FUA
& source
->supported_write_flags
);
222 top
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
|
223 ((BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
) &
224 source
->supported_zero_flags
);
227 state
->target
= bdrv_attach_child(top
, target
, "target", &child_of_bds
,
228 BDRV_CHILD_DATA
, errp
);
229 if (!state
->target
) {
235 bdrv_drained_begin(source
);
238 ret
= bdrv_append(top
, source
, errp
);
240 error_prepend(errp
, "Cannot append backup-top filter: ");
246 * bdrv_append() finished successfully, now we can require permissions
249 state
->active
= true;
250 ret
= bdrv_child_refresh_perms(top
, top
->backing
, errp
);
252 error_prepend(errp
, "Cannot set permissions for backup-top filter: ");
256 state
->cluster_size
= cluster_size
;
257 state
->bcs
= block_copy_state_new(top
->backing
, state
->target
,
258 cluster_size
, perf
->use_copy_range
,
261 error_prepend(errp
, "Cannot create block-copy-state: ");
266 bdrv_drained_end(source
);
272 state
->active
= false;
273 bdrv_backup_top_drop(top
);
278 bdrv_drained_end(source
);
283 void bdrv_backup_top_drop(BlockDriverState
*bs
)
285 BDRVBackupTopState
*s
= bs
->opaque
;
287 bdrv_drained_begin(bs
);
289 block_copy_state_free(s
->bcs
);
292 bdrv_child_refresh_perms(bs
, bs
->backing
, &error_abort
);
293 bdrv_replace_node(bs
, bs
->backing
->bs
, &error_abort
);
294 bdrv_set_backing_hd(bs
, NULL
, &error_abort
);
296 bdrv_drained_end(bs
);