]>
Commit | Line | Data |
---|---|---|
666daa68 MA |
1 | /* |
2 | * QEMU host block devices | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
7 | * later. See the COPYING file in the top-level directory. | |
3618a094 MA |
8 | * |
9 | * This file incorporates work covered by the following copyright and | |
10 | * permission notice: | |
11 | * | |
12 | * Copyright (c) 2003-2008 Fabrice Bellard | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
666daa68 MA |
31 | */ |
32 | ||
26f54e9a | 33 | #include "sysemu/block-backend.h" |
9c17d615 | 34 | #include "sysemu/blockdev.h" |
0d09e41a | 35 | #include "hw/block/block.h" |
737e150e | 36 | #include "block/blockjob.h" |
76f4afb4 | 37 | #include "block/throttle-groups.h" |
83c9089e | 38 | #include "monitor/monitor.h" |
d49b6836 | 39 | #include "qemu/error-report.h" |
1de7afc9 PB |
40 | #include "qemu/option.h" |
41 | #include "qemu/config-file.h" | |
7b1b5d19 | 42 | #include "qapi/qmp/types.h" |
d26c9a15 | 43 | #include "qapi-visit.h" |
cc7a8ea7 | 44 | #include "qapi/qmp/qerror.h" |
d26c9a15 | 45 | #include "qapi/qmp-output-visitor.h" |
9e7dac7c | 46 | #include "qapi/util.h" |
9c17d615 | 47 | #include "sysemu/sysemu.h" |
737e150e | 48 | #include "block/block_int.h" |
a4dea8a9 | 49 | #include "qmp-commands.h" |
12bd451f | 50 | #include "trace.h" |
9c17d615 | 51 | #include "sysemu/arch_init.h" |
666daa68 | 52 | |
1960966d MA |
53 | static const char *const if_name[IF_COUNT] = { |
54 | [IF_NONE] = "none", | |
55 | [IF_IDE] = "ide", | |
56 | [IF_SCSI] = "scsi", | |
57 | [IF_FLOPPY] = "floppy", | |
58 | [IF_PFLASH] = "pflash", | |
59 | [IF_MTD] = "mtd", | |
60 | [IF_SD] = "sd", | |
61 | [IF_VIRTIO] = "virtio", | |
62 | [IF_XEN] = "xen", | |
63 | }; | |
64 | ||
21dff8cf | 65 | static int if_max_devs[IF_COUNT] = { |
27d6bf40 MA |
66 | /* |
67 | * Do not change these numbers! They govern how drive option | |
68 | * index maps to unit and bus. That mapping is ABI. | |
69 | * | |
70 | * All controllers used to imlement if=T drives need to support | |
71 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. | |
72 | * Otherwise, some index values map to "impossible" bus, unit | |
73 | * values. | |
74 | * | |
75 | * For instance, if you change [IF_SCSI] to 255, -drive | |
76 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
77 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
78 | * the drive can't be set up. Regression. | |
79 | */ | |
80 | [IF_IDE] = 2, | |
81 | [IF_SCSI] = 7, | |
1960966d MA |
82 | }; |
83 | ||
21dff8cf JS |
84 | /** |
85 | * Boards may call this to offer board-by-board overrides | |
86 | * of the default, global values. | |
87 | */ | |
88 | void override_max_devs(BlockInterfaceType type, int max_devs) | |
89 | { | |
18e46a03 | 90 | BlockBackend *blk; |
21dff8cf JS |
91 | DriveInfo *dinfo; |
92 | ||
93 | if (max_devs <= 0) { | |
94 | return; | |
95 | } | |
96 | ||
18e46a03 MA |
97 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
98 | dinfo = blk_legacy_dinfo(blk); | |
21dff8cf JS |
99 | if (dinfo->type == type) { |
100 | fprintf(stderr, "Cannot override units-per-bus property of" | |
101 | " the %s interface, because a drive of that type has" | |
102 | " already been added.\n", if_name[type]); | |
103 | g_assert_not_reached(); | |
104 | } | |
105 | } | |
106 | ||
107 | if_max_devs[type] = max_devs; | |
108 | } | |
109 | ||
14bafc54 MA |
110 | /* |
111 | * We automatically delete the drive when a device using it gets | |
112 | * unplugged. Questionable feature, but we can't just drop it. | |
113 | * Device models call blockdev_mark_auto_del() to schedule the | |
114 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
115 | * when deletion is actually safe. | |
116 | */ | |
4be74634 | 117 | void blockdev_mark_auto_del(BlockBackend *blk) |
14bafc54 | 118 | { |
18e46a03 | 119 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
4be74634 | 120 | BlockDriverState *bs = blk_bs(blk); |
91fddb0d | 121 | AioContext *aio_context; |
14bafc54 | 122 | |
26f8b3a8 | 123 | if (!dinfo) { |
2d246f01 KW |
124 | return; |
125 | } | |
126 | ||
5433c24f HR |
127 | if (bs) { |
128 | aio_context = bdrv_get_aio_context(bs); | |
129 | aio_context_acquire(aio_context); | |
91fddb0d | 130 | |
5433c24f HR |
131 | if (bs->job) { |
132 | block_job_cancel(bs->job); | |
133 | } | |
91fddb0d | 134 | |
5433c24f HR |
135 | aio_context_release(aio_context); |
136 | } | |
91fddb0d | 137 | |
26f8b3a8 | 138 | dinfo->auto_del = 1; |
14bafc54 MA |
139 | } |
140 | ||
4be74634 | 141 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 142 | { |
18e46a03 | 143 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 144 | |
0fc0f1fa | 145 | if (dinfo && dinfo->auto_del) { |
b9fe8a7a | 146 | blk_unref(blk); |
14bafc54 MA |
147 | } |
148 | } | |
149 | ||
d8f94e1b JS |
150 | /** |
151 | * Returns the current mapping of how many units per bus | |
152 | * a particular interface can support. | |
153 | * | |
154 | * A positive integer indicates n units per bus. | |
155 | * 0 implies the mapping has not been established. | |
156 | * -1 indicates an invalid BlockInterfaceType was given. | |
157 | */ | |
158 | int drive_get_max_devs(BlockInterfaceType type) | |
159 | { | |
160 | if (type >= IF_IDE && type < IF_COUNT) { | |
161 | return if_max_devs[type]; | |
162 | } | |
163 | ||
164 | return -1; | |
165 | } | |
166 | ||
505a7fb1 MA |
167 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
168 | { | |
169 | int max_devs = if_max_devs[type]; | |
170 | return max_devs ? index / max_devs : 0; | |
171 | } | |
172 | ||
173 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
174 | { | |
175 | int max_devs = if_max_devs[type]; | |
176 | return max_devs ? index % max_devs : index; | |
177 | } | |
178 | ||
2292ddae MA |
179 | QemuOpts *drive_def(const char *optstr) |
180 | { | |
70b94331 | 181 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); |
2292ddae MA |
182 | } |
183 | ||
184 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 185 | const char *optstr) |
666daa68 | 186 | { |
666daa68 MA |
187 | QemuOpts *opts; |
188 | ||
2292ddae | 189 | opts = drive_def(optstr); |
666daa68 MA |
190 | if (!opts) { |
191 | return NULL; | |
192 | } | |
2292ddae | 193 | if (type != IF_DEFAULT) { |
f43e47db | 194 | qemu_opt_set(opts, "if", if_name[type], &error_abort); |
2292ddae MA |
195 | } |
196 | if (index >= 0) { | |
a8b18f8f | 197 | qemu_opt_set_number(opts, "index", index, &error_abort); |
2292ddae | 198 | } |
666daa68 | 199 | if (file) |
f43e47db | 200 | qemu_opt_set(opts, "file", file, &error_abort); |
666daa68 MA |
201 | return opts; |
202 | } | |
203 | ||
204 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
205 | { | |
18e46a03 | 206 | BlockBackend *blk; |
666daa68 MA |
207 | DriveInfo *dinfo; |
208 | ||
18e46a03 MA |
209 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
210 | dinfo = blk_legacy_dinfo(blk); | |
211 | if (dinfo && dinfo->type == type | |
212 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 213 | return dinfo; |
18e46a03 | 214 | } |
666daa68 MA |
215 | } |
216 | ||
217 | return NULL; | |
218 | } | |
219 | ||
a66c9dc7 JS |
220 | bool drive_check_orphaned(void) |
221 | { | |
18e46a03 | 222 | BlockBackend *blk; |
a66c9dc7 JS |
223 | DriveInfo *dinfo; |
224 | bool rs = false; | |
225 | ||
18e46a03 MA |
226 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
227 | dinfo = blk_legacy_dinfo(blk); | |
a66c9dc7 JS |
228 | /* If dinfo->bdrv->dev is NULL, it has no device attached. */ |
229 | /* Unless this is a default drive, this may be an oversight. */ | |
a7f53e26 | 230 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
a66c9dc7 JS |
231 | dinfo->type != IF_NONE) { |
232 | fprintf(stderr, "Warning: Orphaned drive without device: " | |
233 | "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", | |
5433c24f HR |
234 | blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", |
235 | if_name[dinfo->type], dinfo->bus, dinfo->unit); | |
a66c9dc7 JS |
236 | rs = true; |
237 | } | |
238 | } | |
239 | ||
240 | return rs; | |
241 | } | |
242 | ||
f1bd51ac MA |
243 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
244 | { | |
245 | return drive_get(type, | |
246 | drive_index_to_bus_id(type, index), | |
247 | drive_index_to_unit_id(type, index)); | |
248 | } | |
249 | ||
666daa68 MA |
250 | int drive_get_max_bus(BlockInterfaceType type) |
251 | { | |
252 | int max_bus; | |
18e46a03 | 253 | BlockBackend *blk; |
666daa68 MA |
254 | DriveInfo *dinfo; |
255 | ||
256 | max_bus = -1; | |
18e46a03 MA |
257 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
258 | dinfo = blk_legacy_dinfo(blk); | |
259 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 260 | max_bus = dinfo->bus; |
18e46a03 | 261 | } |
666daa68 MA |
262 | } |
263 | return max_bus; | |
264 | } | |
265 | ||
13839974 MA |
266 | /* Get a block device. This should only be used for single-drive devices |
267 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
268 | appropriate bus. */ | |
269 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
270 | { | |
271 | static int next_block_unit[IF_COUNT]; | |
272 | ||
273 | return drive_get(type, 0, next_block_unit[type]++); | |
274 | } | |
275 | ||
666daa68 MA |
276 | static void bdrv_format_print(void *opaque, const char *name) |
277 | { | |
807105a7 | 278 | error_printf(" %s", name); |
666daa68 MA |
279 | } |
280 | ||
aa398a5c SH |
281 | typedef struct { |
282 | QEMUBH *bh; | |
fa510ebf FZ |
283 | BlockDriverState *bs; |
284 | } BDRVPutRefBH; | |
aa398a5c | 285 | |
b681072d | 286 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
287 | { |
288 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 289 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 290 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 291 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 292 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 293 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 294 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 295 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 296 | } else { |
b681072d KW |
297 | error_setg(errp, "'%s' invalid %s error action", |
298 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
299 | return -1; |
300 | } | |
301 | } | |
302 | ||
cc0681c4 | 303 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
0563e191 | 304 | { |
cc0681c4 BC |
305 | if (throttle_conflicting(cfg)) { |
306 | error_setg(errp, "bps/iops/max total values and read/write values" | |
307 | " cannot be used at the same time"); | |
0563e191 ZYW |
308 | return false; |
309 | } | |
310 | ||
cc0681c4 BC |
311 | if (!throttle_is_valid(cfg)) { |
312 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); | |
7d81c141 SH |
313 | return false; |
314 | } | |
315 | ||
ee2bdc33 SH |
316 | if (throttle_max_is_missing_limit(cfg)) { |
317 | error_setg(errp, "bps_max/iops_max require corresponding" | |
318 | " bps/iops values"); | |
319 | return false; | |
320 | } | |
321 | ||
0563e191 ZYW |
322 | return true; |
323 | } | |
324 | ||
33cb7dc8 KW |
325 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
326 | ||
fbf8175e HR |
327 | /* All parameters but @opts are optional and may be set to NULL. */ |
328 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, | |
329 | const char **throttling_group, ThrottleConfig *throttle_cfg, | |
330 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) | |
331 | { | |
332 | const char *discard; | |
333 | Error *local_error = NULL; | |
334 | const char *aio; | |
335 | ||
336 | if (bdrv_flags) { | |
337 | if (!qemu_opt_get_bool(opts, "read-only", false)) { | |
338 | *bdrv_flags |= BDRV_O_RDWR; | |
339 | } | |
340 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { | |
341 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
342 | } | |
343 | ||
344 | if ((discard = qemu_opt_get(opts, "discard")) != NULL) { | |
345 | if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { | |
346 | error_setg(errp, "Invalid discard option"); | |
347 | return; | |
348 | } | |
349 | } | |
350 | ||
351 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) { | |
352 | *bdrv_flags |= BDRV_O_CACHE_WB; | |
353 | } | |
354 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { | |
355 | *bdrv_flags |= BDRV_O_NOCACHE; | |
356 | } | |
357 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { | |
358 | *bdrv_flags |= BDRV_O_NO_FLUSH; | |
359 | } | |
360 | ||
361 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { | |
362 | if (!strcmp(aio, "native")) { | |
363 | *bdrv_flags |= BDRV_O_NATIVE_AIO; | |
364 | } else if (!strcmp(aio, "threads")) { | |
365 | /* this is the default */ | |
366 | } else { | |
367 | error_setg(errp, "invalid aio option"); | |
368 | return; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | /* disk I/O throttling */ | |
374 | if (throttling_group) { | |
375 | *throttling_group = qemu_opt_get(opts, "throttling.group"); | |
376 | } | |
377 | ||
378 | if (throttle_cfg) { | |
379 | memset(throttle_cfg, 0, sizeof(*throttle_cfg)); | |
380 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = | |
381 | qemu_opt_get_number(opts, "throttling.bps-total", 0); | |
382 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = | |
383 | qemu_opt_get_number(opts, "throttling.bps-read", 0); | |
384 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = | |
385 | qemu_opt_get_number(opts, "throttling.bps-write", 0); | |
386 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = | |
387 | qemu_opt_get_number(opts, "throttling.iops-total", 0); | |
388 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = | |
389 | qemu_opt_get_number(opts, "throttling.iops-read", 0); | |
390 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = | |
391 | qemu_opt_get_number(opts, "throttling.iops-write", 0); | |
392 | ||
393 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = | |
394 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
395 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = | |
396 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
397 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = | |
398 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
399 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = | |
400 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
401 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = | |
402 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
403 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = | |
404 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
405 | ||
406 | throttle_cfg->op_size = | |
407 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
408 | ||
409 | if (!check_throttle_config(throttle_cfg, errp)) { | |
410 | return; | |
411 | } | |
412 | } | |
413 | ||
414 | if (detect_zeroes) { | |
415 | *detect_zeroes = | |
416 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, | |
417 | qemu_opt_get(opts, "detect-zeroes"), | |
418 | BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, | |
419 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
420 | &local_error); | |
421 | if (local_error) { | |
422 | error_propagate(errp, local_error); | |
423 | return; | |
424 | } | |
425 | ||
426 | if (bdrv_flags && | |
427 | *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
428 | !(*bdrv_flags & BDRV_O_UNMAP)) | |
429 | { | |
430 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
431 | "without setting discard operation to unmap"); | |
432 | return; | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
f298d071 | 437 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
438 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
439 | Error **errp) | |
666daa68 MA |
440 | { |
441 | const char *buf; | |
666daa68 MA |
442 | int bdrv_flags = 0; |
443 | int on_read_error, on_write_error; | |
362e9299 | 444 | bool account_invalid, account_failed; |
26f54e9a | 445 | BlockBackend *blk; |
a0f1eab1 | 446 | BlockDriverState *bs; |
cc0681c4 | 447 | ThrottleConfig cfg; |
666daa68 | 448 | int snapshot = 0; |
c546194f | 449 | Error *error = NULL; |
0006383e | 450 | QemuOpts *opts; |
0006383e | 451 | const char *id; |
74fe54f2 | 452 | bool has_driver_specific_opts; |
fbf8175e HR |
453 | BlockdevDetectZeroesOptions detect_zeroes = |
454 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
455 | const char *throttling_group = NULL; | |
666daa68 | 456 | |
f298d071 KW |
457 | /* Check common options by copying from bs_opts to opts, all other options |
458 | * stay in bs_opts for processing by bdrv_open(). */ | |
459 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 460 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 461 | if (error) { |
b681072d | 462 | error_propagate(errp, error); |
6376f952 | 463 | goto err_no_opts; |
0006383e KW |
464 | } |
465 | ||
0006383e | 466 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 467 | if (error) { |
b681072d | 468 | error_propagate(errp, error); |
ec9c10d2 | 469 | goto early_err; |
0006383e KW |
470 | } |
471 | ||
472 | if (id) { | |
473 | qdict_del(bs_opts, "id"); | |
474 | } | |
475 | ||
74fe54f2 KW |
476 | has_driver_specific_opts = !!qdict_size(bs_opts); |
477 | ||
666daa68 | 478 | /* extract parameters */ |
666daa68 | 479 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
666daa68 | 480 | |
362e9299 AG |
481 | account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); |
482 | account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); | |
483 | ||
fbf8175e HR |
484 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
485 | &detect_zeroes, &error); | |
486 | if (error) { | |
487 | error_propagate(errp, error); | |
488 | goto early_err; | |
666daa68 | 489 | } |
666daa68 MA |
490 | |
491 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
492 | if (is_help_option(buf)) { |
493 | error_printf("Supported formats:"); | |
494 | bdrv_iterate_format(bdrv_format_print, NULL); | |
495 | error_printf("\n"); | |
ec9c10d2 | 496 | goto early_err; |
666daa68 | 497 | } |
74fe54f2 | 498 | |
e4342ce5 HR |
499 | if (qdict_haskey(bs_opts, "driver")) { |
500 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
ec9c10d2 | 501 | goto early_err; |
6db5f5d6 | 502 | } |
e4342ce5 | 503 | qdict_put(bs_opts, "driver", qstring_from_str(buf)); |
666daa68 MA |
504 | } |
505 | ||
92aa5c6d | 506 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 507 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 508 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 509 | if (error) { |
b681072d | 510 | error_propagate(errp, error); |
ec9c10d2 | 511 | goto early_err; |
666daa68 MA |
512 | } |
513 | } | |
514 | ||
92aa5c6d | 515 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 516 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 517 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 518 | if (error) { |
b681072d | 519 | error_propagate(errp, error); |
ec9c10d2 | 520 | goto early_err; |
666daa68 MA |
521 | } |
522 | } | |
523 | ||
5ec18f8c HR |
524 | if (snapshot) { |
525 | /* always use cache=unsafe with snapshot */ | |
526 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
527 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
528 | } | |
529 | ||
326642bc | 530 | /* init */ |
e4342ce5 | 531 | if ((!file || !*file) && !has_driver_specific_opts) { |
5ec18f8c HR |
532 | BlockBackendRootState *blk_rs; |
533 | ||
534 | blk = blk_new(qemu_opts_id(opts), errp); | |
e4342ce5 HR |
535 | if (!blk) { |
536 | goto early_err; | |
537 | } | |
abd7f68d | 538 | |
5ec18f8c HR |
539 | blk_rs = blk_get_root_state(blk); |
540 | blk_rs->open_flags = bdrv_flags; | |
fbf8175e | 541 | blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); |
5ec18f8c HR |
542 | blk_rs->detect_zeroes = detect_zeroes; |
543 | ||
544 | if (throttle_enabled(&cfg)) { | |
545 | if (!throttling_group) { | |
546 | throttling_group = blk_name(blk); | |
547 | } | |
548 | blk_rs->throttle_group = g_strdup(throttling_group); | |
549 | blk_rs->throttle_state = throttle_group_incref(throttling_group); | |
550 | blk_rs->throttle_state->cfg = cfg; | |
551 | } | |
0563e191 | 552 | |
e4342ce5 HR |
553 | QDECREF(bs_opts); |
554 | } else { | |
555 | if (file && !*file) { | |
c2ad1b0c | 556 | file = NULL; |
c2ad1b0c | 557 | } |
666daa68 | 558 | |
e4342ce5 HR |
559 | blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, |
560 | errp); | |
561 | if (!blk) { | |
562 | goto err_no_bs_opts; | |
563 | } | |
564 | bs = blk_bs(blk); | |
ed9d4205 | 565 | |
5ec18f8c | 566 | bs->detect_zeroes = detect_zeroes; |
666daa68 | 567 | |
5ec18f8c HR |
568 | /* disk I/O throttling */ |
569 | if (throttle_enabled(&cfg)) { | |
570 | if (!throttling_group) { | |
571 | throttling_group = blk_name(blk); | |
572 | } | |
573 | bdrv_io_limits_enable(bs, throttling_group); | |
574 | bdrv_set_io_limits(bs, &cfg); | |
575 | } | |
0006383e | 576 | |
5ec18f8c HR |
577 | if (bdrv_key_required(bs)) { |
578 | autostart = 0; | |
76f4afb4 | 579 | } |
362e9299 AG |
580 | |
581 | block_acct_init(blk_get_stats(blk), account_invalid, account_failed); | |
666daa68 MA |
582 | } |
583 | ||
5ec18f8c | 584 | blk_set_on_error(blk, on_read_error, on_write_error); |
0006383e | 585 | |
e4342ce5 | 586 | err_no_bs_opts: |
0006383e | 587 | qemu_opts_del(opts); |
18e46a03 | 588 | return blk; |
a9ae2bff | 589 | |
ec9c10d2 | 590 | early_err: |
ec9c10d2 | 591 | qemu_opts_del(opts); |
6376f952 MA |
592 | err_no_opts: |
593 | QDECREF(bs_opts); | |
a9ae2bff | 594 | return NULL; |
666daa68 MA |
595 | } |
596 | ||
bd745e23 HR |
597 | static QemuOptsList qemu_root_bds_opts; |
598 | ||
599 | /* Takes the ownership of bs_opts */ | |
600 | static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) | |
601 | { | |
602 | BlockDriverState *bs; | |
603 | QemuOpts *opts; | |
604 | Error *local_error = NULL; | |
605 | BlockdevDetectZeroesOptions detect_zeroes; | |
606 | int ret; | |
607 | int bdrv_flags = 0; | |
608 | ||
609 | opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); | |
610 | if (!opts) { | |
611 | goto fail; | |
612 | } | |
613 | ||
614 | qemu_opts_absorb_qdict(opts, bs_opts, &local_error); | |
615 | if (local_error) { | |
616 | error_propagate(errp, local_error); | |
617 | goto fail; | |
618 | } | |
619 | ||
620 | extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, | |
621 | &detect_zeroes, &local_error); | |
622 | if (local_error) { | |
623 | error_propagate(errp, local_error); | |
624 | goto fail; | |
625 | } | |
626 | ||
627 | bs = NULL; | |
628 | ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); | |
629 | if (ret < 0) { | |
630 | goto fail_no_bs_opts; | |
631 | } | |
632 | ||
633 | bs->detect_zeroes = detect_zeroes; | |
634 | ||
635 | fail_no_bs_opts: | |
636 | qemu_opts_del(opts); | |
637 | return bs; | |
638 | ||
639 | fail: | |
640 | qemu_opts_del(opts); | |
641 | QDECREF(bs_opts); | |
642 | return NULL; | |
643 | } | |
644 | ||
5abbf0ee KW |
645 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
646 | Error **errp) | |
57975222 KW |
647 | { |
648 | const char *value; | |
649 | ||
650 | value = qemu_opt_get(opts, from); | |
651 | if (value) { | |
5abbf0ee KW |
652 | if (qemu_opt_find(opts, to)) { |
653 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
654 | "same time", to, from); | |
655 | return; | |
656 | } | |
20d6cd47 JL |
657 | } |
658 | ||
659 | /* rename all items in opts */ | |
660 | while ((value = qemu_opt_get(opts, from))) { | |
f43e47db | 661 | qemu_opt_set(opts, to, value, &error_abort); |
57975222 KW |
662 | qemu_opt_unset(opts, from); |
663 | } | |
664 | } | |
665 | ||
33cb7dc8 KW |
666 | QemuOptsList qemu_legacy_drive_opts = { |
667 | .name = "drive", | |
668 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
669 | .desc = { | |
670 | { | |
87a899c5 KW |
671 | .name = "bus", |
672 | .type = QEMU_OPT_NUMBER, | |
673 | .help = "bus number", | |
674 | },{ | |
675 | .name = "unit", | |
676 | .type = QEMU_OPT_NUMBER, | |
677 | .help = "unit number (i.e. lun for scsi)", | |
678 | },{ | |
679 | .name = "index", | |
680 | .type = QEMU_OPT_NUMBER, | |
681 | .help = "index number", | |
682 | },{ | |
33cb7dc8 KW |
683 | .name = "media", |
684 | .type = QEMU_OPT_STRING, | |
685 | .help = "media type (disk, cdrom)", | |
593d464b KW |
686 | },{ |
687 | .name = "if", | |
688 | .type = QEMU_OPT_STRING, | |
689 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
b41a7338 KW |
690 | },{ |
691 | .name = "cyls", | |
692 | .type = QEMU_OPT_NUMBER, | |
693 | .help = "number of cylinders (ide disk geometry)", | |
694 | },{ | |
695 | .name = "heads", | |
696 | .type = QEMU_OPT_NUMBER, | |
697 | .help = "number of heads (ide disk geometry)", | |
698 | },{ | |
699 | .name = "secs", | |
700 | .type = QEMU_OPT_NUMBER, | |
701 | .help = "number of sectors (ide disk geometry)", | |
702 | },{ | |
703 | .name = "trans", | |
704 | .type = QEMU_OPT_STRING, | |
705 | .help = "chs translation (auto, lba, none)", | |
26929298 KW |
706 | },{ |
707 | .name = "boot", | |
708 | .type = QEMU_OPT_BOOL, | |
709 | .help = "(deprecated, ignored)", | |
394c7d4d KW |
710 | },{ |
711 | .name = "addr", | |
712 | .type = QEMU_OPT_STRING, | |
713 | .help = "pci address (virtio only)", | |
bcf83158 KW |
714 | },{ |
715 | .name = "serial", | |
716 | .type = QEMU_OPT_STRING, | |
717 | .help = "disk serial number", | |
d095b465 HR |
718 | },{ |
719 | .name = "file", | |
720 | .type = QEMU_OPT_STRING, | |
721 | .help = "file name", | |
33cb7dc8 | 722 | }, |
0ebd24e0 KW |
723 | |
724 | /* Options that are passed on, but have special semantics with -drive */ | |
725 | { | |
726 | .name = "read-only", | |
727 | .type = QEMU_OPT_BOOL, | |
728 | .help = "open drive file as read-only", | |
ee13ed1c KW |
729 | },{ |
730 | .name = "rerror", | |
731 | .type = QEMU_OPT_STRING, | |
732 | .help = "read error action", | |
733 | },{ | |
734 | .name = "werror", | |
735 | .type = QEMU_OPT_STRING, | |
736 | .help = "write error action", | |
0ebd24e0 KW |
737 | },{ |
738 | .name = "copy-on-read", | |
739 | .type = QEMU_OPT_BOOL, | |
740 | .help = "copy read data from backing file into image file", | |
741 | }, | |
742 | ||
33cb7dc8 KW |
743 | { /* end of list */ } |
744 | }, | |
745 | }; | |
746 | ||
60e19e06 | 747 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
57975222 | 748 | { |
29c4e2b5 | 749 | const char *value; |
18e46a03 | 750 | BlockBackend *blk; |
33cb7dc8 | 751 | DriveInfo *dinfo = NULL; |
f298d071 | 752 | QDict *bs_opts; |
33cb7dc8 KW |
753 | QemuOpts *legacy_opts; |
754 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 755 | BlockInterfaceType type; |
b41a7338 | 756 | int cyls, heads, secs, translation; |
87a899c5 | 757 | int max_devs, bus_id, unit_id, index; |
394c7d4d | 758 | const char *devaddr; |
ee13ed1c | 759 | const char *werror, *rerror; |
a7fdbcf0 FZ |
760 | bool read_only = false; |
761 | bool copy_on_read; | |
bcf83158 | 762 | const char *serial; |
d095b465 | 763 | const char *filename; |
33cb7dc8 | 764 | Error *local_err = NULL; |
247147fb | 765 | int i; |
29c4e2b5 | 766 | |
57975222 | 767 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
768 | static const struct { |
769 | const char *from; | |
770 | const char *to; | |
771 | } opt_renames[] = { | |
772 | { "iops", "throttling.iops-total" }, | |
773 | { "iops_rd", "throttling.iops-read" }, | |
774 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 775 | |
247147fb KW |
776 | { "bps", "throttling.bps-total" }, |
777 | { "bps_rd", "throttling.bps-read" }, | |
778 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 779 | |
247147fb KW |
780 | { "iops_max", "throttling.iops-total-max" }, |
781 | { "iops_rd_max", "throttling.iops-read-max" }, | |
782 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 783 | |
247147fb KW |
784 | { "bps_max", "throttling.bps-total-max" }, |
785 | { "bps_rd_max", "throttling.bps-read-max" }, | |
786 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 787 | |
247147fb | 788 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 789 | |
76f4afb4 AG |
790 | { "group", "throttling.group" }, |
791 | ||
247147fb KW |
792 | { "readonly", "read-only" }, |
793 | }; | |
794 | ||
795 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
796 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
797 | &local_err); | |
798 | if (local_err) { | |
565f65d2 | 799 | error_report_err(local_err); |
5abbf0ee KW |
800 | return NULL; |
801 | } | |
247147fb | 802 | } |
0f227a94 | 803 | |
29c4e2b5 KW |
804 | value = qemu_opt_get(all_opts, "cache"); |
805 | if (value) { | |
806 | int flags = 0; | |
807 | ||
808 | if (bdrv_parse_cache_flags(value, &flags) != 0) { | |
809 | error_report("invalid cache option"); | |
810 | return NULL; | |
811 | } | |
812 | ||
813 | /* Specific options take precedence */ | |
54861b92 KW |
814 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
815 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
cccb7967 | 816 | !!(flags & BDRV_O_CACHE_WB), &error_abort); |
29c4e2b5 | 817 | } |
54861b92 KW |
818 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
819 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
cccb7967 | 820 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
29c4e2b5 | 821 | } |
54861b92 KW |
822 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
823 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
cccb7967 | 824 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
29c4e2b5 KW |
825 | } |
826 | qemu_opt_unset(all_opts, "cache"); | |
827 | } | |
828 | ||
f298d071 KW |
829 | /* Get a QDict for processing the options */ |
830 | bs_opts = qdict_new(); | |
831 | qemu_opts_to_qdict(all_opts, bs_opts); | |
832 | ||
87ea75d5 PC |
833 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
834 | &error_abort); | |
33cb7dc8 | 835 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 836 | if (local_err) { |
565f65d2 | 837 | error_report_err(local_err); |
33cb7dc8 KW |
838 | goto fail; |
839 | } | |
840 | ||
26929298 KW |
841 | /* Deprecated option boot=[on|off] */ |
842 | if (qemu_opt_get(legacy_opts, "boot") != NULL) { | |
843 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " | |
844 | "ignored. Future versions will reject this parameter. Please " | |
845 | "update your scripts.\n"); | |
846 | } | |
847 | ||
33cb7dc8 KW |
848 | /* Media type */ |
849 | value = qemu_opt_get(legacy_opts, "media"); | |
850 | if (value) { | |
851 | if (!strcmp(value, "disk")) { | |
852 | media = MEDIA_DISK; | |
853 | } else if (!strcmp(value, "cdrom")) { | |
854 | media = MEDIA_CDROM; | |
a7fdbcf0 | 855 | read_only = true; |
33cb7dc8 KW |
856 | } else { |
857 | error_report("'%s' invalid media", value); | |
858 | goto fail; | |
859 | } | |
860 | } | |
861 | ||
0ebd24e0 | 862 | /* copy-on-read is disabled with a warning for read-only devices */ |
a7fdbcf0 | 863 | read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); |
0ebd24e0 KW |
864 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
865 | ||
866 | if (read_only && copy_on_read) { | |
867 | error_report("warning: disabling copy-on-read on read-only drive"); | |
868 | copy_on_read = false; | |
869 | } | |
870 | ||
871 | qdict_put(bs_opts, "read-only", | |
872 | qstring_from_str(read_only ? "on" : "off")); | |
873 | qdict_put(bs_opts, "copy-on-read", | |
874 | qstring_from_str(copy_on_read ? "on" :"off")); | |
875 | ||
593d464b KW |
876 | /* Controller type */ |
877 | value = qemu_opt_get(legacy_opts, "if"); | |
878 | if (value) { | |
879 | for (type = 0; | |
880 | type < IF_COUNT && strcmp(value, if_name[type]); | |
881 | type++) { | |
882 | } | |
883 | if (type == IF_COUNT) { | |
884 | error_report("unsupported bus type '%s'", value); | |
885 | goto fail; | |
886 | } | |
887 | } else { | |
888 | type = block_default_type; | |
889 | } | |
890 | ||
b41a7338 KW |
891 | /* Geometry */ |
892 | cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); | |
893 | heads = qemu_opt_get_number(legacy_opts, "heads", 0); | |
894 | secs = qemu_opt_get_number(legacy_opts, "secs", 0); | |
895 | ||
896 | if (cyls || heads || secs) { | |
897 | if (cyls < 1) { | |
898 | error_report("invalid physical cyls number"); | |
899 | goto fail; | |
900 | } | |
901 | if (heads < 1) { | |
902 | error_report("invalid physical heads number"); | |
903 | goto fail; | |
904 | } | |
905 | if (secs < 1) { | |
906 | error_report("invalid physical secs number"); | |
907 | goto fail; | |
908 | } | |
909 | } | |
910 | ||
911 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
912 | value = qemu_opt_get(legacy_opts, "trans"); | |
913 | if (value != NULL) { | |
914 | if (!cyls) { | |
915 | error_report("'%s' trans must be used with cyls, heads and secs", | |
916 | value); | |
917 | goto fail; | |
918 | } | |
919 | if (!strcmp(value, "none")) { | |
920 | translation = BIOS_ATA_TRANSLATION_NONE; | |
921 | } else if (!strcmp(value, "lba")) { | |
922 | translation = BIOS_ATA_TRANSLATION_LBA; | |
f31c41ff PB |
923 | } else if (!strcmp(value, "large")) { |
924 | translation = BIOS_ATA_TRANSLATION_LARGE; | |
925 | } else if (!strcmp(value, "rechs")) { | |
926 | translation = BIOS_ATA_TRANSLATION_RECHS; | |
b41a7338 KW |
927 | } else if (!strcmp(value, "auto")) { |
928 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
929 | } else { | |
930 | error_report("'%s' invalid translation type", value); | |
931 | goto fail; | |
932 | } | |
933 | } | |
934 | ||
935 | if (media == MEDIA_CDROM) { | |
936 | if (cyls || secs || heads) { | |
937 | error_report("CHS can't be set with media=cdrom"); | |
938 | goto fail; | |
939 | } | |
940 | } | |
941 | ||
87a899c5 KW |
942 | /* Device address specified by bus/unit or index. |
943 | * If none was specified, try to find the first free one. */ | |
944 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
945 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
946 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
947 | ||
948 | max_devs = if_max_devs[type]; | |
949 | ||
950 | if (index != -1) { | |
951 | if (bus_id != 0 || unit_id != -1) { | |
952 | error_report("index cannot be used with bus and unit"); | |
953 | goto fail; | |
954 | } | |
955 | bus_id = drive_index_to_bus_id(type, index); | |
956 | unit_id = drive_index_to_unit_id(type, index); | |
957 | } | |
958 | ||
959 | if (unit_id == -1) { | |
960 | unit_id = 0; | |
961 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
962 | unit_id++; | |
963 | if (max_devs && unit_id >= max_devs) { | |
964 | unit_id -= max_devs; | |
965 | bus_id++; | |
966 | } | |
967 | } | |
968 | } | |
969 | ||
970 | if (max_devs && unit_id >= max_devs) { | |
971 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); | |
972 | goto fail; | |
973 | } | |
974 | ||
975 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
976 | error_report("drive with bus=%d, unit=%d (index=%d) exists", | |
977 | bus_id, unit_id, index); | |
978 | goto fail; | |
979 | } | |
980 | ||
bcf83158 KW |
981 | /* Serial number */ |
982 | serial = qemu_opt_get(legacy_opts, "serial"); | |
983 | ||
87a899c5 KW |
984 | /* no id supplied -> create one */ |
985 | if (qemu_opts_id(all_opts) == NULL) { | |
986 | char *new_id; | |
987 | const char *mediastr = ""; | |
988 | if (type == IF_IDE || type == IF_SCSI) { | |
989 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
990 | } | |
991 | if (max_devs) { | |
992 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
993 | mediastr, unit_id); | |
994 | } else { | |
995 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
996 | mediastr, unit_id); | |
997 | } | |
998 | qdict_put(bs_opts, "id", qstring_from_str(new_id)); | |
999 | g_free(new_id); | |
1000 | } | |
1001 | ||
394c7d4d KW |
1002 | /* Add virtio block device */ |
1003 | devaddr = qemu_opt_get(legacy_opts, "addr"); | |
1004 | if (devaddr && type != IF_VIRTIO) { | |
1005 | error_report("addr is not supported by this bus type"); | |
1006 | goto fail; | |
1007 | } | |
1008 | ||
1009 | if (type == IF_VIRTIO) { | |
1010 | QemuOpts *devopts; | |
87ea75d5 PC |
1011 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
1012 | &error_abort); | |
394c7d4d | 1013 | if (arch_type == QEMU_ARCH_S390X) { |
1f68f1d3 | 1014 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); |
394c7d4d | 1015 | } else { |
f43e47db | 1016 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); |
394c7d4d | 1017 | } |
f43e47db MA |
1018 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
1019 | &error_abort); | |
394c7d4d | 1020 | if (devaddr) { |
f43e47db | 1021 | qemu_opt_set(devopts, "addr", devaddr, &error_abort); |
394c7d4d KW |
1022 | } |
1023 | } | |
1024 | ||
d095b465 HR |
1025 | filename = qemu_opt_get(legacy_opts, "file"); |
1026 | ||
ee13ed1c KW |
1027 | /* Check werror/rerror compatibility with if=... */ |
1028 | werror = qemu_opt_get(legacy_opts, "werror"); | |
1029 | if (werror != NULL) { | |
1030 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
1031 | type != IF_NONE) { | |
1032 | error_report("werror is not supported by this bus type"); | |
1033 | goto fail; | |
1034 | } | |
1035 | qdict_put(bs_opts, "werror", qstring_from_str(werror)); | |
1036 | } | |
1037 | ||
1038 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
1039 | if (rerror != NULL) { | |
1040 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
1041 | type != IF_NONE) { | |
1042 | error_report("rerror is not supported by this bus type"); | |
1043 | goto fail; | |
1044 | } | |
1045 | qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); | |
1046 | } | |
1047 | ||
2d246f01 | 1048 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 1049 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 1050 | bs_opts = NULL; |
18e46a03 | 1051 | if (!blk) { |
84d18f06 | 1052 | if (local_err) { |
565f65d2 | 1053 | error_report_err(local_err); |
b681072d | 1054 | } |
2d246f01 | 1055 | goto fail; |
b681072d | 1056 | } else { |
84d18f06 | 1057 | assert(!local_err); |
2d246f01 KW |
1058 | } |
1059 | ||
26f8b3a8 MA |
1060 | /* Create legacy DriveInfo */ |
1061 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 1062 | dinfo->opts = all_opts; |
2d246f01 | 1063 | |
b41a7338 KW |
1064 | dinfo->cyls = cyls; |
1065 | dinfo->heads = heads; | |
1066 | dinfo->secs = secs; | |
1067 | dinfo->trans = translation; | |
1068 | ||
ee13ed1c | 1069 | dinfo->type = type; |
87a899c5 KW |
1070 | dinfo->bus = bus_id; |
1071 | dinfo->unit = unit_id; | |
394c7d4d | 1072 | dinfo->devaddr = devaddr; |
bcf83158 KW |
1073 | dinfo->serial = g_strdup(serial); |
1074 | ||
26f8b3a8 MA |
1075 | blk_set_legacy_dinfo(blk, dinfo); |
1076 | ||
e34ef046 KW |
1077 | switch(type) { |
1078 | case IF_IDE: | |
1079 | case IF_SCSI: | |
1080 | case IF_XEN: | |
1081 | case IF_NONE: | |
1082 | dinfo->media_cd = media == MEDIA_CDROM; | |
1083 | break; | |
1084 | default: | |
1085 | break; | |
1086 | } | |
1087 | ||
2d246f01 | 1088 | fail: |
33cb7dc8 | 1089 | qemu_opts_del(legacy_opts); |
3cb0e25c | 1090 | QDECREF(bs_opts); |
2d246f01 | 1091 | return dinfo; |
57975222 KW |
1092 | } |
1093 | ||
3e5a50d6 | 1094 | void hmp_commit(Monitor *mon, const QDict *qdict) |
666daa68 | 1095 | { |
666daa68 | 1096 | const char *device = qdict_get_str(qdict, "device"); |
a0e8544c | 1097 | BlockBackend *blk; |
e8877497 | 1098 | int ret; |
666daa68 | 1099 | |
6ab4b5ab | 1100 | if (!strcmp(device, "all")) { |
e8877497 | 1101 | ret = bdrv_commit_all(); |
6ab4b5ab | 1102 | } else { |
84aa0140 SH |
1103 | BlockDriverState *bs; |
1104 | AioContext *aio_context; | |
1105 | ||
a0e8544c FZ |
1106 | blk = blk_by_name(device); |
1107 | if (!blk) { | |
58513bde | 1108 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 1109 | return; |
6ab4b5ab | 1110 | } |
5433c24f HR |
1111 | if (!blk_is_available(blk)) { |
1112 | monitor_printf(mon, "Device '%s' has no medium\n", device); | |
1113 | return; | |
1114 | } | |
84aa0140 SH |
1115 | |
1116 | bs = blk_bs(blk); | |
1117 | aio_context = bdrv_get_aio_context(bs); | |
1118 | aio_context_acquire(aio_context); | |
1119 | ||
1120 | ret = bdrv_commit(bs); | |
1121 | ||
1122 | aio_context_release(aio_context); | |
58513bde JC |
1123 | } |
1124 | if (ret < 0) { | |
1125 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1126 | strerror(-ret)); | |
666daa68 MA |
1127 | } |
1128 | } | |
1129 | ||
6a8f9661 EB |
1130 | static void blockdev_do_action(TransactionActionKind type, void *data, |
1131 | Error **errp) | |
6cc2a415 | 1132 | { |
c8a83e85 KW |
1133 | TransactionAction action; |
1134 | TransactionActionList list; | |
6cc2a415 | 1135 | |
6a8f9661 EB |
1136 | action.type = type; |
1137 | action.u.data = data; | |
6cc2a415 PB |
1138 | list.value = &action; |
1139 | list.next = NULL; | |
94d16a64 | 1140 | qmp_transaction(&list, false, NULL, errp); |
6cc2a415 PB |
1141 | } |
1142 | ||
0901f67e BC |
1143 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1144 | bool has_node_name, const char *node_name, | |
1145 | const char *snapshot_file, | |
1146 | bool has_snapshot_node_name, | |
1147 | const char *snapshot_node_name, | |
6106e249 | 1148 | bool has_format, const char *format, |
0901f67e | 1149 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1150 | { |
a911e6ae | 1151 | BlockdevSnapshotSync snapshot = { |
0901f67e | 1152 | .has_device = has_device, |
6cc2a415 | 1153 | .device = (char *) device, |
0901f67e BC |
1154 | .has_node_name = has_node_name, |
1155 | .node_name = (char *) node_name, | |
6cc2a415 | 1156 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1157 | .has_snapshot_node_name = has_snapshot_node_name, |
1158 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1159 | .has_format = has_format, |
1160 | .format = (char *) format, | |
1161 | .has_mode = has_mode, | |
1162 | .mode = mode, | |
1163 | }; | |
c8a83e85 KW |
1164 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
1165 | &snapshot, errp); | |
f8882568 JS |
1166 | } |
1167 | ||
43de7e2d AG |
1168 | void qmp_blockdev_snapshot(const char *node, const char *overlay, |
1169 | Error **errp) | |
1170 | { | |
1171 | BlockdevSnapshot snapshot_data = { | |
1172 | .node = (char *) node, | |
1173 | .overlay = (char *) overlay | |
1174 | }; | |
1175 | ||
1176 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, | |
1177 | &snapshot_data, errp); | |
1178 | } | |
1179 | ||
f323bc9e WX |
1180 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1181 | const char *name, | |
1182 | Error **errp) | |
1183 | { | |
1184 | BlockdevSnapshotInternal snapshot = { | |
1185 | .device = (char *) device, | |
1186 | .name = (char *) name | |
1187 | }; | |
1188 | ||
1189 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
1190 | &snapshot, errp); | |
1191 | } | |
1192 | ||
44e3e053 WX |
1193 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1194 | bool has_id, | |
1195 | const char *id, | |
1196 | bool has_name, | |
1197 | const char *name, | |
1198 | Error **errp) | |
1199 | { | |
a0e8544c FZ |
1200 | BlockDriverState *bs; |
1201 | BlockBackend *blk; | |
4ef3982a | 1202 | AioContext *aio_context; |
44e3e053 WX |
1203 | QEMUSnapshotInfo sn; |
1204 | Error *local_err = NULL; | |
1205 | SnapshotInfo *info = NULL; | |
1206 | int ret; | |
1207 | ||
a0e8544c FZ |
1208 | blk = blk_by_name(device); |
1209 | if (!blk) { | |
75158ebb MA |
1210 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1211 | "Device '%s' not found", device); | |
44e3e053 WX |
1212 | return NULL; |
1213 | } | |
5433c24f HR |
1214 | |
1215 | aio_context = blk_get_aio_context(blk); | |
1216 | aio_context_acquire(aio_context); | |
44e3e053 WX |
1217 | |
1218 | if (!has_id) { | |
1219 | id = NULL; | |
1220 | } | |
1221 | ||
1222 | if (!has_name) { | |
1223 | name = NULL; | |
1224 | } | |
1225 | ||
1226 | if (!id && !name) { | |
1227 | error_setg(errp, "Name or id must be provided"); | |
5433c24f | 1228 | goto out_aio_context; |
44e3e053 WX |
1229 | } |
1230 | ||
5433c24f HR |
1231 | if (!blk_is_available(blk)) { |
1232 | error_setg(errp, "Device '%s' has no medium", device); | |
1233 | goto out_aio_context; | |
1234 | } | |
1235 | bs = blk_bs(blk); | |
4ef3982a | 1236 | |
0b928854 SH |
1237 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1238 | goto out_aio_context; | |
1239 | } | |
1240 | ||
44e3e053 | 1241 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1242 | if (local_err) { |
44e3e053 | 1243 | error_propagate(errp, local_err); |
4ef3982a | 1244 | goto out_aio_context; |
44e3e053 WX |
1245 | } |
1246 | if (!ret) { | |
1247 | error_setg(errp, | |
1248 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1249 | "device '%s'", | |
1250 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1251 | goto out_aio_context; |
44e3e053 WX |
1252 | } |
1253 | ||
1254 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1255 | if (local_err) { |
44e3e053 | 1256 | error_propagate(errp, local_err); |
4ef3982a | 1257 | goto out_aio_context; |
44e3e053 WX |
1258 | } |
1259 | ||
4ef3982a SH |
1260 | aio_context_release(aio_context); |
1261 | ||
5839e53b | 1262 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1263 | info->id = g_strdup(sn.id_str); |
1264 | info->name = g_strdup(sn.name); | |
1265 | info->date_nsec = sn.date_nsec; | |
1266 | info->date_sec = sn.date_sec; | |
1267 | info->vm_state_size = sn.vm_state_size; | |
1268 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1269 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1270 | ||
1271 | return info; | |
4ef3982a SH |
1272 | |
1273 | out_aio_context: | |
1274 | aio_context_release(aio_context); | |
1275 | return NULL; | |
44e3e053 | 1276 | } |
8802d1fd | 1277 | |
341ebc2f JS |
1278 | /** |
1279 | * block_dirty_bitmap_lookup: | |
1280 | * Return a dirty bitmap (if present), after validating | |
1281 | * the node reference and bitmap names. | |
1282 | * | |
1283 | * @node: The name of the BDS node to search for bitmaps | |
1284 | * @name: The name of the bitmap to search for | |
1285 | * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. | |
1286 | * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. | |
1287 | * @errp: Output pointer for error information. Can be NULL. | |
1288 | * | |
1289 | * @return: A bitmap object on success, or NULL on failure. | |
1290 | */ | |
1291 | static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, | |
1292 | const char *name, | |
1293 | BlockDriverState **pbs, | |
1294 | AioContext **paio, | |
1295 | Error **errp) | |
1296 | { | |
1297 | BlockDriverState *bs; | |
1298 | BdrvDirtyBitmap *bitmap; | |
1299 | AioContext *aio_context; | |
1300 | ||
1301 | if (!node) { | |
1302 | error_setg(errp, "Node cannot be NULL"); | |
1303 | return NULL; | |
1304 | } | |
1305 | if (!name) { | |
1306 | error_setg(errp, "Bitmap name cannot be NULL"); | |
1307 | return NULL; | |
1308 | } | |
1309 | bs = bdrv_lookup_bs(node, node, NULL); | |
1310 | if (!bs) { | |
1311 | error_setg(errp, "Node '%s' not found", node); | |
1312 | return NULL; | |
1313 | } | |
1314 | ||
1315 | aio_context = bdrv_get_aio_context(bs); | |
1316 | aio_context_acquire(aio_context); | |
1317 | ||
1318 | bitmap = bdrv_find_dirty_bitmap(bs, name); | |
1319 | if (!bitmap) { | |
1320 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
1321 | goto fail; | |
1322 | } | |
1323 | ||
1324 | if (pbs) { | |
1325 | *pbs = bs; | |
1326 | } | |
1327 | if (paio) { | |
1328 | *paio = aio_context; | |
1329 | } else { | |
1330 | aio_context_release(aio_context); | |
1331 | } | |
1332 | ||
1333 | return bitmap; | |
1334 | ||
1335 | fail: | |
1336 | aio_context_release(aio_context); | |
1337 | return NULL; | |
1338 | } | |
1339 | ||
b756b9ce | 1340 | /* New and old BlockDriverState structs for atomic group operations */ |
ba0c86a3 | 1341 | |
50f43f0f | 1342 | typedef struct BlkActionState BlkActionState; |
ba0c86a3 | 1343 | |
50f43f0f JS |
1344 | /** |
1345 | * BlkActionOps: | |
1346 | * Table of operations that define an Action. | |
1347 | * | |
1348 | * @instance_size: Size of state struct, in bytes. | |
1349 | * @prepare: Prepare the work, must NOT be NULL. | |
1350 | * @commit: Commit the changes, can be NULL. | |
1351 | * @abort: Abort the changes on fail, can be NULL. | |
1352 | * @clean: Clean up resources after all transaction actions have called | |
1353 | * commit() or abort(). Can be NULL. | |
1354 | * | |
1355 | * Only prepare() may fail. In a single transaction, only one of commit() or | |
1356 | * abort() will be called. clean() will always be called if it is present. | |
1357 | */ | |
1358 | typedef struct BlkActionOps { | |
ba0c86a3 | 1359 | size_t instance_size; |
50f43f0f JS |
1360 | void (*prepare)(BlkActionState *common, Error **errp); |
1361 | void (*commit)(BlkActionState *common); | |
1362 | void (*abort)(BlkActionState *common); | |
1363 | void (*clean)(BlkActionState *common); | |
1364 | } BlkActionOps; | |
ba0c86a3 | 1365 | |
50f43f0f JS |
1366 | /** |
1367 | * BlkActionState: | |
1368 | * Describes one Action's state within a Transaction. | |
1369 | * | |
1370 | * @action: QAPI-defined enum identifying which Action to perform. | |
1371 | * @ops: Table of ActionOps this Action can perform. | |
94d16a64 | 1372 | * @block_job_txn: Transaction which this action belongs to. |
50f43f0f JS |
1373 | * @entry: List membership for all Actions in this Transaction. |
1374 | * | |
1375 | * This structure must be arranged as first member in a subclassed type, | |
1376 | * assuming that the compiler will also arrange it to the same offsets as the | |
1377 | * base class. | |
ba0c86a3 | 1378 | */ |
50f43f0f | 1379 | struct BlkActionState { |
c8a83e85 | 1380 | TransactionAction *action; |
50f43f0f | 1381 | const BlkActionOps *ops; |
94d16a64 JS |
1382 | BlockJobTxn *block_job_txn; |
1383 | TransactionProperties *txn_props; | |
50f43f0f | 1384 | QSIMPLEQ_ENTRY(BlkActionState) entry; |
ba0c86a3 WX |
1385 | }; |
1386 | ||
bbe86010 WX |
1387 | /* internal snapshot private data */ |
1388 | typedef struct InternalSnapshotState { | |
50f43f0f | 1389 | BlkActionState common; |
bbe86010 | 1390 | BlockDriverState *bs; |
5d6e96ef | 1391 | AioContext *aio_context; |
bbe86010 | 1392 | QEMUSnapshotInfo sn; |
507306cc | 1393 | bool created; |
bbe86010 WX |
1394 | } InternalSnapshotState; |
1395 | ||
94d16a64 JS |
1396 | |
1397 | static int action_check_completion_mode(BlkActionState *s, Error **errp) | |
1398 | { | |
1399 | if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
1400 | error_setg(errp, | |
1401 | "Action '%s' does not support Transaction property " | |
1402 | "completion-mode = %s", | |
1403 | TransactionActionKind_lookup[s->action->type], | |
1404 | ActionCompletionMode_lookup[s->txn_props->completion_mode]); | |
1405 | return -1; | |
1406 | } | |
1407 | return 0; | |
1408 | } | |
1409 | ||
50f43f0f | 1410 | static void internal_snapshot_prepare(BlkActionState *common, |
bbe86010 WX |
1411 | Error **errp) |
1412 | { | |
f70edf99 | 1413 | Error *local_err = NULL; |
bbe86010 WX |
1414 | const char *device; |
1415 | const char *name; | |
a0e8544c | 1416 | BlockBackend *blk; |
bbe86010 WX |
1417 | BlockDriverState *bs; |
1418 | QEMUSnapshotInfo old_sn, *sn; | |
1419 | bool ret; | |
1420 | qemu_timeval tv; | |
1421 | BlockdevSnapshotInternal *internal; | |
1422 | InternalSnapshotState *state; | |
1423 | int ret1; | |
1424 | ||
6a8f9661 | 1425 | g_assert(common->action->type == |
bbe86010 | 1426 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
6a8f9661 | 1427 | internal = common->action->u.blockdev_snapshot_internal_sync; |
bbe86010 WX |
1428 | state = DO_UPCAST(InternalSnapshotState, common, common); |
1429 | ||
1430 | /* 1. parse input */ | |
1431 | device = internal->device; | |
1432 | name = internal->name; | |
1433 | ||
1434 | /* 2. check for validation */ | |
94d16a64 JS |
1435 | if (action_check_completion_mode(common, errp) < 0) { |
1436 | return; | |
1437 | } | |
1438 | ||
a0e8544c FZ |
1439 | blk = blk_by_name(device); |
1440 | if (!blk) { | |
75158ebb MA |
1441 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1442 | "Device '%s' not found", device); | |
bbe86010 WX |
1443 | return; |
1444 | } | |
1445 | ||
5d6e96ef | 1446 | /* AioContext is released in .clean() */ |
5433c24f | 1447 | state->aio_context = blk_get_aio_context(blk); |
5d6e96ef SH |
1448 | aio_context_acquire(state->aio_context); |
1449 | ||
5433c24f | 1450 | if (!blk_is_available(blk)) { |
c6bd8c70 | 1451 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
bbe86010 WX |
1452 | return; |
1453 | } | |
5433c24f | 1454 | bs = blk_bs(blk); |
bbe86010 | 1455 | |
507306cc FZ |
1456 | state->bs = bs; |
1457 | bdrv_drained_begin(bs); | |
1458 | ||
3dc7ca3c SH |
1459 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
1460 | return; | |
1461 | } | |
1462 | ||
bbe86010 | 1463 | if (bdrv_is_read_only(bs)) { |
81e5f78a | 1464 | error_setg(errp, "Device '%s' is read only", device); |
bbe86010 WX |
1465 | return; |
1466 | } | |
1467 | ||
1468 | if (!bdrv_can_snapshot(bs)) { | |
81e5f78a AG |
1469 | error_setg(errp, "Block format '%s' used by device '%s' " |
1470 | "does not support internal snapshots", | |
1471 | bs->drv->format_name, device); | |
bbe86010 WX |
1472 | return; |
1473 | } | |
1474 | ||
1475 | if (!strlen(name)) { | |
1476 | error_setg(errp, "Name is empty"); | |
1477 | return; | |
1478 | } | |
1479 | ||
1480 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1481 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1482 | &local_err); | |
1483 | if (local_err) { | |
1484 | error_propagate(errp, local_err); | |
bbe86010 WX |
1485 | return; |
1486 | } else if (ret) { | |
1487 | error_setg(errp, | |
1488 | "Snapshot with name '%s' already exists on device '%s'", | |
1489 | name, device); | |
1490 | return; | |
1491 | } | |
1492 | ||
1493 | /* 3. take the snapshot */ | |
1494 | sn = &state->sn; | |
1495 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1496 | qemu_gettimeofday(&tv); | |
1497 | sn->date_sec = tv.tv_sec; | |
1498 | sn->date_nsec = tv.tv_usec * 1000; | |
1499 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1500 | ||
1501 | ret1 = bdrv_snapshot_create(bs, sn); | |
1502 | if (ret1 < 0) { | |
1503 | error_setg_errno(errp, -ret1, | |
1504 | "Failed to create snapshot '%s' on device '%s'", | |
1505 | name, device); | |
1506 | return; | |
1507 | } | |
1508 | ||
1509 | /* 4. succeed, mark a snapshot is created */ | |
507306cc | 1510 | state->created = true; |
bbe86010 WX |
1511 | } |
1512 | ||
50f43f0f | 1513 | static void internal_snapshot_abort(BlkActionState *common) |
bbe86010 WX |
1514 | { |
1515 | InternalSnapshotState *state = | |
1516 | DO_UPCAST(InternalSnapshotState, common, common); | |
1517 | BlockDriverState *bs = state->bs; | |
1518 | QEMUSnapshotInfo *sn = &state->sn; | |
1519 | Error *local_error = NULL; | |
1520 | ||
507306cc | 1521 | if (!state->created) { |
bbe86010 WX |
1522 | return; |
1523 | } | |
1524 | ||
1525 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
1526 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " | |
1527 | "device '%s' in abort: %s", | |
1528 | sn->id_str, | |
1529 | sn->name, | |
1530 | bdrv_get_device_name(bs), | |
1531 | error_get_pretty(local_error)); | |
1532 | error_free(local_error); | |
1533 | } | |
1534 | } | |
1535 | ||
50f43f0f | 1536 | static void internal_snapshot_clean(BlkActionState *common) |
5d6e96ef SH |
1537 | { |
1538 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1539 | common, common); | |
1540 | ||
1541 | if (state->aio_context) { | |
507306cc FZ |
1542 | if (state->bs) { |
1543 | bdrv_drained_end(state->bs); | |
1544 | } | |
5d6e96ef SH |
1545 | aio_context_release(state->aio_context); |
1546 | } | |
1547 | } | |
1548 | ||
ba0c86a3 | 1549 | /* external snapshot private data */ |
ba5d6ab6 | 1550 | typedef struct ExternalSnapshotState { |
50f43f0f | 1551 | BlkActionState common; |
8802d1fd JC |
1552 | BlockDriverState *old_bs; |
1553 | BlockDriverState *new_bs; | |
5d6e96ef | 1554 | AioContext *aio_context; |
ba5d6ab6 | 1555 | } ExternalSnapshotState; |
8802d1fd | 1556 | |
50f43f0f | 1557 | static void external_snapshot_prepare(BlkActionState *common, |
9b9877ee WX |
1558 | Error **errp) |
1559 | { | |
43de7e2d AG |
1560 | int flags = 0, ret; |
1561 | QDict *options = NULL; | |
9b9877ee | 1562 | Error *local_err = NULL; |
43de7e2d | 1563 | /* Device and node name of the image to generate the snapshot from */ |
e2a31e87 | 1564 | const char *device; |
0901f67e | 1565 | const char *node_name; |
43de7e2d AG |
1566 | /* Reference to the new image (for 'blockdev-snapshot') */ |
1567 | const char *snapshot_ref; | |
1568 | /* File name of the new image (for 'blockdev-snapshot-sync') */ | |
e2a31e87 | 1569 | const char *new_image_file; |
ba5d6ab6 SH |
1570 | ExternalSnapshotState *state = |
1571 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1572 | TransactionAction *action = common->action; |
9b9877ee | 1573 | |
43de7e2d AG |
1574 | /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar |
1575 | * purpose but a different set of parameters */ | |
1576 | switch (action->type) { | |
1577 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: | |
1578 | { | |
1579 | BlockdevSnapshot *s = action->u.blockdev_snapshot; | |
1580 | device = s->node; | |
1581 | node_name = s->node; | |
1582 | new_image_file = NULL; | |
1583 | snapshot_ref = s->overlay; | |
1584 | } | |
1585 | break; | |
1586 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: | |
1587 | { | |
1588 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; | |
1589 | device = s->has_device ? s->device : NULL; | |
1590 | node_name = s->has_node_name ? s->node_name : NULL; | |
1591 | new_image_file = s->snapshot_file; | |
1592 | snapshot_ref = NULL; | |
1593 | } | |
1594 | break; | |
1595 | default: | |
1596 | g_assert_not_reached(); | |
e2a31e87 WX |
1597 | } |
1598 | ||
1599 | /* start processing */ | |
94d16a64 JS |
1600 | if (action_check_completion_mode(common, errp) < 0) { |
1601 | return; | |
1602 | } | |
1603 | ||
43de7e2d AG |
1604 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); |
1605 | if (!state->old_bs) { | |
9b9877ee WX |
1606 | return; |
1607 | } | |
1608 | ||
5d6e96ef SH |
1609 | /* Acquire AioContext now so any threads operating on old_bs stop */ |
1610 | state->aio_context = bdrv_get_aio_context(state->old_bs); | |
1611 | aio_context_acquire(state->aio_context); | |
da763e83 | 1612 | bdrv_drained_begin(state->old_bs); |
5d6e96ef | 1613 | |
ba5d6ab6 | 1614 | if (!bdrv_is_inserted(state->old_bs)) { |
c6bd8c70 | 1615 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
9b9877ee WX |
1616 | return; |
1617 | } | |
1618 | ||
3718d8ab FZ |
1619 | if (bdrv_op_is_blocked(state->old_bs, |
1620 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
9b9877ee WX |
1621 | return; |
1622 | } | |
1623 | ||
ba5d6ab6 SH |
1624 | if (!bdrv_is_read_only(state->old_bs)) { |
1625 | if (bdrv_flush(state->old_bs)) { | |
c6bd8c70 | 1626 | error_setg(errp, QERR_IO_ERROR); |
9b9877ee WX |
1627 | return; |
1628 | } | |
1629 | } | |
1630 | ||
212a5a8f | 1631 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
c6bd8c70 | 1632 | error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); |
f6186f49 BC |
1633 | return; |
1634 | } | |
1635 | ||
43de7e2d AG |
1636 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { |
1637 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; | |
1638 | const char *format = s->has_format ? s->format : "qcow2"; | |
1639 | enum NewImageMode mode; | |
1640 | const char *snapshot_node_name = | |
1641 | s->has_snapshot_node_name ? s->snapshot_node_name : NULL; | |
9b9877ee | 1642 | |
43de7e2d AG |
1643 | if (node_name && !snapshot_node_name) { |
1644 | error_setg(errp, "New snapshot node name missing"); | |
9b9877ee WX |
1645 | return; |
1646 | } | |
9b9877ee | 1647 | |
43de7e2d AG |
1648 | if (snapshot_node_name && |
1649 | bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { | |
1650 | error_setg(errp, "New snapshot node name already in use"); | |
1651 | return; | |
1652 | } | |
1653 | ||
1654 | flags = state->old_bs->open_flags; | |
1655 | ||
1656 | /* create new image w/backing file */ | |
1657 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1658 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1659 | bdrv_img_create(new_image_file, format, | |
1660 | state->old_bs->filename, | |
1661 | state->old_bs->drv->format_name, | |
1662 | NULL, -1, flags, &local_err, false); | |
1663 | if (local_err) { | |
1664 | error_propagate(errp, local_err); | |
1665 | return; | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | options = qdict_new(); | |
1670 | if (s->has_snapshot_node_name) { | |
1671 | qdict_put(options, "node-name", | |
1672 | qstring_from_str(snapshot_node_name)); | |
1673 | } | |
1674 | qdict_put(options, "driver", qstring_from_str(format)); | |
1675 | ||
1676 | flags |= BDRV_O_NO_BACKING; | |
0901f67e BC |
1677 | } |
1678 | ||
f67503e5 | 1679 | assert(state->new_bs == NULL); |
43de7e2d AG |
1680 | ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, |
1681 | flags, errp); | |
f67503e5 | 1682 | /* We will manually add the backing_hd field to the bs later */ |
9b9877ee | 1683 | if (ret != 0) { |
43de7e2d AG |
1684 | return; |
1685 | } | |
1686 | ||
1687 | if (state->new_bs->blk != NULL) { | |
1688 | error_setg(errp, "The snapshot is already in use by %s", | |
1689 | blk_name(state->new_bs->blk)); | |
1690 | return; | |
1691 | } | |
1692 | ||
1693 | if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, | |
1694 | errp)) { | |
1695 | return; | |
1696 | } | |
1697 | ||
1698 | if (state->new_bs->backing != NULL) { | |
1699 | error_setg(errp, "The snapshot already has a backing image"); | |
08b24cfe AG |
1700 | return; |
1701 | } | |
1702 | ||
1703 | if (!state->new_bs->drv->supports_backing) { | |
1704 | error_setg(errp, "The snapshot does not support backing images"); | |
9b9877ee WX |
1705 | } |
1706 | } | |
1707 | ||
50f43f0f | 1708 | static void external_snapshot_commit(BlkActionState *common) |
3b0047e8 | 1709 | { |
ba5d6ab6 SH |
1710 | ExternalSnapshotState *state = |
1711 | DO_UPCAST(ExternalSnapshotState, common, common); | |
ba0c86a3 | 1712 | |
5d6e96ef SH |
1713 | bdrv_set_aio_context(state->new_bs, state->aio_context); |
1714 | ||
ba5d6ab6 SH |
1715 | /* This removes our old bs and adds the new bs */ |
1716 | bdrv_append(state->new_bs, state->old_bs); | |
3b0047e8 WX |
1717 | /* We don't need (or want) to use the transactional |
1718 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1719 | * don't want to abort all of them if one of them fails the reopen */ | |
dd62f1ca | 1720 | bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, |
3b0047e8 WX |
1721 | NULL); |
1722 | } | |
1723 | ||
50f43f0f | 1724 | static void external_snapshot_abort(BlkActionState *common) |
96b86bf7 | 1725 | { |
ba5d6ab6 SH |
1726 | ExternalSnapshotState *state = |
1727 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1728 | if (state->new_bs) { | |
4f6fd349 | 1729 | bdrv_unref(state->new_bs); |
96b86bf7 | 1730 | } |
da763e83 FZ |
1731 | } |
1732 | ||
50f43f0f | 1733 | static void external_snapshot_clean(BlkActionState *common) |
da763e83 FZ |
1734 | { |
1735 | ExternalSnapshotState *state = | |
1736 | DO_UPCAST(ExternalSnapshotState, common, common); | |
5d6e96ef | 1737 | if (state->aio_context) { |
da763e83 | 1738 | bdrv_drained_end(state->old_bs); |
5d6e96ef SH |
1739 | aio_context_release(state->aio_context); |
1740 | } | |
96b86bf7 WX |
1741 | } |
1742 | ||
3037f364 | 1743 | typedef struct DriveBackupState { |
50f43f0f | 1744 | BlkActionState common; |
3037f364 | 1745 | BlockDriverState *bs; |
5d6e96ef | 1746 | AioContext *aio_context; |
3037f364 SH |
1747 | BlockJob *job; |
1748 | } DriveBackupState; | |
1749 | ||
78f51fde JS |
1750 | static void do_drive_backup(const char *device, const char *target, |
1751 | bool has_format, const char *format, | |
1752 | enum MirrorSyncMode sync, | |
1753 | bool has_mode, enum NewImageMode mode, | |
1754 | bool has_speed, int64_t speed, | |
1755 | bool has_bitmap, const char *bitmap, | |
1756 | bool has_on_source_error, | |
1757 | BlockdevOnError on_source_error, | |
1758 | bool has_on_target_error, | |
1759 | BlockdevOnError on_target_error, | |
1760 | BlockJobTxn *txn, Error **errp); | |
1761 | ||
50f43f0f | 1762 | static void drive_backup_prepare(BlkActionState *common, Error **errp) |
3037f364 SH |
1763 | { |
1764 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
a0e8544c | 1765 | BlockBackend *blk; |
3037f364 SH |
1766 | DriveBackup *backup; |
1767 | Error *local_err = NULL; | |
1768 | ||
6a8f9661 EB |
1769 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
1770 | backup = common->action->u.drive_backup; | |
3037f364 | 1771 | |
a0e8544c FZ |
1772 | blk = blk_by_name(backup->device); |
1773 | if (!blk) { | |
75158ebb MA |
1774 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1775 | "Device '%s' not found", backup->device); | |
5d6e96ef SH |
1776 | return; |
1777 | } | |
1778 | ||
1fdd4b7b FZ |
1779 | if (!blk_is_available(blk)) { |
1780 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); | |
1781 | return; | |
1782 | } | |
1783 | ||
5d6e96ef | 1784 | /* AioContext is released in .clean() */ |
5433c24f | 1785 | state->aio_context = blk_get_aio_context(blk); |
5d6e96ef | 1786 | aio_context_acquire(state->aio_context); |
1fdd4b7b FZ |
1787 | bdrv_drained_begin(blk_bs(blk)); |
1788 | state->bs = blk_bs(blk); | |
5d6e96ef | 1789 | |
78f51fde JS |
1790 | do_drive_backup(backup->device, backup->target, |
1791 | backup->has_format, backup->format, | |
1792 | backup->sync, | |
1793 | backup->has_mode, backup->mode, | |
1794 | backup->has_speed, backup->speed, | |
1795 | backup->has_bitmap, backup->bitmap, | |
1796 | backup->has_on_source_error, backup->on_source_error, | |
1797 | backup->has_on_target_error, backup->on_target_error, | |
94d16a64 | 1798 | common->block_job_txn, &local_err); |
84d18f06 | 1799 | if (local_err) { |
3037f364 | 1800 | error_propagate(errp, local_err); |
3037f364 SH |
1801 | return; |
1802 | } | |
1803 | ||
3037f364 SH |
1804 | state->job = state->bs->job; |
1805 | } | |
1806 | ||
50f43f0f | 1807 | static void drive_backup_abort(BlkActionState *common) |
3037f364 SH |
1808 | { |
1809 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1810 | BlockDriverState *bs = state->bs; | |
1811 | ||
1812 | /* Only cancel if it's the job we started */ | |
1813 | if (bs && bs->job && bs->job == state->job) { | |
1814 | block_job_cancel_sync(bs->job); | |
1815 | } | |
1816 | } | |
1817 | ||
50f43f0f | 1818 | static void drive_backup_clean(BlkActionState *common) |
5d6e96ef SH |
1819 | { |
1820 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1821 | ||
1822 | if (state->aio_context) { | |
1fdd4b7b | 1823 | bdrv_drained_end(state->bs); |
5d6e96ef SH |
1824 | aio_context_release(state->aio_context); |
1825 | } | |
1826 | } | |
1827 | ||
bd8baecd | 1828 | typedef struct BlockdevBackupState { |
50f43f0f | 1829 | BlkActionState common; |
bd8baecd FZ |
1830 | BlockDriverState *bs; |
1831 | BlockJob *job; | |
1832 | AioContext *aio_context; | |
1833 | } BlockdevBackupState; | |
1834 | ||
78f51fde JS |
1835 | static void do_blockdev_backup(const char *device, const char *target, |
1836 | enum MirrorSyncMode sync, | |
1837 | bool has_speed, int64_t speed, | |
1838 | bool has_on_source_error, | |
1839 | BlockdevOnError on_source_error, | |
1840 | bool has_on_target_error, | |
1841 | BlockdevOnError on_target_error, | |
1842 | BlockJobTxn *txn, Error **errp); | |
1843 | ||
50f43f0f | 1844 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) |
bd8baecd FZ |
1845 | { |
1846 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1847 | BlockdevBackup *backup; | |
5433c24f | 1848 | BlockBackend *blk, *target; |
bd8baecd FZ |
1849 | Error *local_err = NULL; |
1850 | ||
6a8f9661 EB |
1851 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); |
1852 | backup = common->action->u.blockdev_backup; | |
bd8baecd | 1853 | |
a0e8544c FZ |
1854 | blk = blk_by_name(backup->device); |
1855 | if (!blk) { | |
5b347c54 | 1856 | error_setg(errp, "Device '%s' not found", backup->device); |
bd8baecd FZ |
1857 | return; |
1858 | } | |
1859 | ||
ff52bf36 FZ |
1860 | if (!blk_is_available(blk)) { |
1861 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); | |
1862 | return; | |
1863 | } | |
1864 | ||
5433c24f HR |
1865 | target = blk_by_name(backup->target); |
1866 | if (!target) { | |
5b347c54 | 1867 | error_setg(errp, "Device '%s' not found", backup->target); |
bd8baecd FZ |
1868 | return; |
1869 | } | |
1870 | ||
1871 | /* AioContext is released in .clean() */ | |
5433c24f HR |
1872 | state->aio_context = blk_get_aio_context(blk); |
1873 | if (state->aio_context != blk_get_aio_context(target)) { | |
bd8baecd FZ |
1874 | state->aio_context = NULL; |
1875 | error_setg(errp, "Backup between two IO threads is not implemented"); | |
1876 | return; | |
1877 | } | |
1878 | aio_context_acquire(state->aio_context); | |
ff52bf36 FZ |
1879 | state->bs = blk_bs(blk); |
1880 | bdrv_drained_begin(state->bs); | |
bd8baecd | 1881 | |
78f51fde JS |
1882 | do_blockdev_backup(backup->device, backup->target, |
1883 | backup->sync, | |
1884 | backup->has_speed, backup->speed, | |
1885 | backup->has_on_source_error, backup->on_source_error, | |
1886 | backup->has_on_target_error, backup->on_target_error, | |
94d16a64 | 1887 | common->block_job_txn, &local_err); |
bd8baecd FZ |
1888 | if (local_err) { |
1889 | error_propagate(errp, local_err); | |
1890 | return; | |
1891 | } | |
1892 | ||
bd8baecd FZ |
1893 | state->job = state->bs->job; |
1894 | } | |
1895 | ||
50f43f0f | 1896 | static void blockdev_backup_abort(BlkActionState *common) |
bd8baecd FZ |
1897 | { |
1898 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1899 | BlockDriverState *bs = state->bs; | |
1900 | ||
1901 | /* Only cancel if it's the job we started */ | |
1902 | if (bs && bs->job && bs->job == state->job) { | |
1903 | block_job_cancel_sync(bs->job); | |
1904 | } | |
1905 | } | |
1906 | ||
50f43f0f | 1907 | static void blockdev_backup_clean(BlkActionState *common) |
bd8baecd FZ |
1908 | { |
1909 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1910 | ||
1911 | if (state->aio_context) { | |
ff52bf36 | 1912 | bdrv_drained_end(state->bs); |
bd8baecd FZ |
1913 | aio_context_release(state->aio_context); |
1914 | } | |
1915 | } | |
1916 | ||
df9a681d | 1917 | typedef struct BlockDirtyBitmapState { |
50f43f0f | 1918 | BlkActionState common; |
df9a681d FZ |
1919 | BdrvDirtyBitmap *bitmap; |
1920 | BlockDriverState *bs; | |
1921 | AioContext *aio_context; | |
1922 | HBitmap *backup; | |
1923 | bool prepared; | |
1924 | } BlockDirtyBitmapState; | |
1925 | ||
50f43f0f | 1926 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, |
df9a681d FZ |
1927 | Error **errp) |
1928 | { | |
1929 | Error *local_err = NULL; | |
1930 | BlockDirtyBitmapAdd *action; | |
1931 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1932 | common, common); | |
1933 | ||
94d16a64 JS |
1934 | if (action_check_completion_mode(common, errp) < 0) { |
1935 | return; | |
1936 | } | |
1937 | ||
50f43f0f | 1938 | action = common->action->u.block_dirty_bitmap_add; |
df9a681d FZ |
1939 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ |
1940 | qmp_block_dirty_bitmap_add(action->node, action->name, | |
1941 | action->has_granularity, action->granularity, | |
1942 | &local_err); | |
1943 | ||
1944 | if (!local_err) { | |
1945 | state->prepared = true; | |
1946 | } else { | |
1947 | error_propagate(errp, local_err); | |
1948 | } | |
1949 | } | |
1950 | ||
50f43f0f | 1951 | static void block_dirty_bitmap_add_abort(BlkActionState *common) |
df9a681d FZ |
1952 | { |
1953 | BlockDirtyBitmapAdd *action; | |
1954 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1955 | common, common); | |
1956 | ||
50f43f0f | 1957 | action = common->action->u.block_dirty_bitmap_add; |
df9a681d FZ |
1958 | /* Should not be able to fail: IF the bitmap was added via .prepare(), |
1959 | * then the node reference and bitmap name must have been valid. | |
1960 | */ | |
1961 | if (state->prepared) { | |
1962 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); | |
1963 | } | |
1964 | } | |
1965 | ||
50f43f0f | 1966 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, |
df9a681d FZ |
1967 | Error **errp) |
1968 | { | |
1969 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1970 | common, common); | |
1971 | BlockDirtyBitmap *action; | |
1972 | ||
94d16a64 JS |
1973 | if (action_check_completion_mode(common, errp) < 0) { |
1974 | return; | |
1975 | } | |
1976 | ||
50f43f0f | 1977 | action = common->action->u.block_dirty_bitmap_clear; |
df9a681d FZ |
1978 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
1979 | action->name, | |
1980 | &state->bs, | |
1981 | &state->aio_context, | |
1982 | errp); | |
1983 | if (!state->bitmap) { | |
1984 | return; | |
1985 | } | |
1986 | ||
1987 | if (bdrv_dirty_bitmap_frozen(state->bitmap)) { | |
1988 | error_setg(errp, "Cannot modify a frozen bitmap"); | |
1989 | return; | |
1990 | } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { | |
1991 | error_setg(errp, "Cannot clear a disabled bitmap"); | |
1992 | return; | |
1993 | } | |
1994 | ||
1995 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); | |
1996 | /* AioContext is released in .clean() */ | |
1997 | } | |
1998 | ||
50f43f0f | 1999 | static void block_dirty_bitmap_clear_abort(BlkActionState *common) |
df9a681d FZ |
2000 | { |
2001 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2002 | common, common); | |
2003 | ||
2004 | bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); | |
2005 | } | |
2006 | ||
50f43f0f | 2007 | static void block_dirty_bitmap_clear_commit(BlkActionState *common) |
df9a681d FZ |
2008 | { |
2009 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2010 | common, common); | |
2011 | ||
2012 | hbitmap_free(state->backup); | |
2013 | } | |
2014 | ||
50f43f0f | 2015 | static void block_dirty_bitmap_clear_clean(BlkActionState *common) |
df9a681d FZ |
2016 | { |
2017 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2018 | common, common); | |
2019 | ||
2020 | if (state->aio_context) { | |
2021 | aio_context_release(state->aio_context); | |
2022 | } | |
2023 | } | |
2024 | ||
50f43f0f | 2025 | static void abort_prepare(BlkActionState *common, Error **errp) |
78b18b78 SH |
2026 | { |
2027 | error_setg(errp, "Transaction aborted using Abort action"); | |
2028 | } | |
2029 | ||
50f43f0f | 2030 | static void abort_commit(BlkActionState *common) |
78b18b78 | 2031 | { |
dfc6f865 | 2032 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
2033 | } |
2034 | ||
50f43f0f | 2035 | static const BlkActionOps actions[] = { |
43de7e2d AG |
2036 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { |
2037 | .instance_size = sizeof(ExternalSnapshotState), | |
2038 | .prepare = external_snapshot_prepare, | |
2039 | .commit = external_snapshot_commit, | |
2040 | .abort = external_snapshot_abort, | |
2041 | }, | |
c8a83e85 | 2042 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 2043 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
2044 | .prepare = external_snapshot_prepare, |
2045 | .commit = external_snapshot_commit, | |
2046 | .abort = external_snapshot_abort, | |
da763e83 | 2047 | .clean = external_snapshot_clean, |
ba0c86a3 | 2048 | }, |
3037f364 SH |
2049 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
2050 | .instance_size = sizeof(DriveBackupState), | |
2051 | .prepare = drive_backup_prepare, | |
2052 | .abort = drive_backup_abort, | |
5d6e96ef | 2053 | .clean = drive_backup_clean, |
3037f364 | 2054 | }, |
bd8baecd FZ |
2055 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
2056 | .instance_size = sizeof(BlockdevBackupState), | |
2057 | .prepare = blockdev_backup_prepare, | |
2058 | .abort = blockdev_backup_abort, | |
2059 | .clean = blockdev_backup_clean, | |
2060 | }, | |
78b18b78 | 2061 | [TRANSACTION_ACTION_KIND_ABORT] = { |
50f43f0f | 2062 | .instance_size = sizeof(BlkActionState), |
78b18b78 SH |
2063 | .prepare = abort_prepare, |
2064 | .commit = abort_commit, | |
2065 | }, | |
bbe86010 WX |
2066 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
2067 | .instance_size = sizeof(InternalSnapshotState), | |
2068 | .prepare = internal_snapshot_prepare, | |
2069 | .abort = internal_snapshot_abort, | |
5d6e96ef | 2070 | .clean = internal_snapshot_clean, |
bbe86010 | 2071 | }, |
df9a681d FZ |
2072 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { |
2073 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2074 | .prepare = block_dirty_bitmap_add_prepare, | |
2075 | .abort = block_dirty_bitmap_add_abort, | |
2076 | }, | |
2077 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { | |
2078 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2079 | .prepare = block_dirty_bitmap_clear_prepare, | |
2080 | .commit = block_dirty_bitmap_clear_commit, | |
2081 | .abort = block_dirty_bitmap_clear_abort, | |
2082 | .clean = block_dirty_bitmap_clear_clean, | |
2083 | } | |
ba0c86a3 WX |
2084 | }; |
2085 | ||
94d16a64 JS |
2086 | /** |
2087 | * Allocate a TransactionProperties structure if necessary, and fill | |
2088 | * that structure with desired defaults if they are unset. | |
2089 | */ | |
2090 | static TransactionProperties *get_transaction_properties( | |
2091 | TransactionProperties *props) | |
2092 | { | |
2093 | if (!props) { | |
2094 | props = g_new0(TransactionProperties, 1); | |
2095 | } | |
2096 | ||
2097 | if (!props->has_completion_mode) { | |
2098 | props->has_completion_mode = true; | |
2099 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; | |
2100 | } | |
2101 | ||
2102 | return props; | |
2103 | } | |
2104 | ||
8802d1fd | 2105 | /* |
b756b9ce SH |
2106 | * 'Atomic' group operations. The operations are performed as a set, and if |
2107 | * any fail then we roll back all operations in the group. | |
8802d1fd | 2108 | */ |
94d16a64 JS |
2109 | void qmp_transaction(TransactionActionList *dev_list, |
2110 | bool has_props, | |
2111 | struct TransactionProperties *props, | |
2112 | Error **errp) | |
8802d1fd | 2113 | { |
c8a83e85 | 2114 | TransactionActionList *dev_entry = dev_list; |
94d16a64 | 2115 | BlockJobTxn *block_job_txn = NULL; |
50f43f0f | 2116 | BlkActionState *state, *next; |
43e17041 | 2117 | Error *local_err = NULL; |
8802d1fd | 2118 | |
50f43f0f | 2119 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; |
8802d1fd JC |
2120 | QSIMPLEQ_INIT(&snap_bdrv_states); |
2121 | ||
94d16a64 JS |
2122 | /* Does this transaction get canceled as a group on failure? |
2123 | * If not, we don't really need to make a BlockJobTxn. | |
2124 | */ | |
2125 | props = get_transaction_properties(props); | |
2126 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
2127 | block_job_txn = block_job_txn_new(); | |
2128 | } | |
2129 | ||
b756b9ce | 2130 | /* drain all i/o before any operations */ |
8802d1fd JC |
2131 | bdrv_drain_all(); |
2132 | ||
b756b9ce | 2133 | /* We don't do anything in this loop that commits us to the operations */ |
8802d1fd | 2134 | while (NULL != dev_entry) { |
c8a83e85 | 2135 | TransactionAction *dev_info = NULL; |
50f43f0f | 2136 | const BlkActionOps *ops; |
52e7c241 | 2137 | |
8802d1fd JC |
2138 | dev_info = dev_entry->value; |
2139 | dev_entry = dev_entry->next; | |
2140 | ||
6a8f9661 | 2141 | assert(dev_info->type < ARRAY_SIZE(actions)); |
ba0c86a3 | 2142 | |
6a8f9661 | 2143 | ops = &actions[dev_info->type]; |
aa3fe714 HR |
2144 | assert(ops->instance_size > 0); |
2145 | ||
ba5d6ab6 SH |
2146 | state = g_malloc0(ops->instance_size); |
2147 | state->ops = ops; | |
2148 | state->action = dev_info; | |
94d16a64 JS |
2149 | state->block_job_txn = block_job_txn; |
2150 | state->txn_props = props; | |
ba5d6ab6 | 2151 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
8802d1fd | 2152 | |
ba5d6ab6 | 2153 | state->ops->prepare(state, &local_err); |
84d18f06 | 2154 | if (local_err) { |
ba0c86a3 WX |
2155 | error_propagate(errp, local_err); |
2156 | goto delete_and_fail; | |
8802d1fd | 2157 | } |
8802d1fd JC |
2158 | } |
2159 | ||
ba5d6ab6 | 2160 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
2161 | if (state->ops->commit) { |
2162 | state->ops->commit(state); | |
2163 | } | |
8802d1fd JC |
2164 | } |
2165 | ||
2166 | /* success */ | |
2167 | goto exit; | |
2168 | ||
2169 | delete_and_fail: | |
b756b9ce | 2170 | /* failure, and it is all-or-none; roll back all operations */ |
ba5d6ab6 SH |
2171 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
2172 | if (state->ops->abort) { | |
2173 | state->ops->abort(state); | |
ba0c86a3 | 2174 | } |
8802d1fd JC |
2175 | } |
2176 | exit: | |
ba5d6ab6 SH |
2177 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
2178 | if (state->ops->clean) { | |
2179 | state->ops->clean(state); | |
ba0c86a3 | 2180 | } |
ba5d6ab6 | 2181 | g_free(state); |
8802d1fd | 2182 | } |
94d16a64 JS |
2183 | if (!has_props) { |
2184 | qapi_free_TransactionProperties(props); | |
2185 | } | |
2186 | block_job_txn_unref(block_job_txn); | |
8802d1fd JC |
2187 | } |
2188 | ||
c245b6a3 | 2189 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
666daa68 | 2190 | { |
38f54bd1 | 2191 | Error *local_err = NULL; |
666daa68 | 2192 | |
38f54bd1 HR |
2193 | qmp_blockdev_open_tray(device, has_force, force, &local_err); |
2194 | if (local_err) { | |
2195 | error_propagate(errp, local_err); | |
c245b6a3 | 2196 | return; |
92d48558 LC |
2197 | } |
2198 | ||
38f54bd1 | 2199 | qmp_blockdev_remove_medium(device, errp); |
666daa68 MA |
2200 | } |
2201 | ||
12d3ba82 BC |
2202 | void qmp_block_passwd(bool has_device, const char *device, |
2203 | bool has_node_name, const char *node_name, | |
2204 | const char *password, Error **errp) | |
666daa68 | 2205 | { |
12d3ba82 | 2206 | Error *local_err = NULL; |
666daa68 | 2207 | BlockDriverState *bs; |
e3442099 | 2208 | AioContext *aio_context; |
666daa68 | 2209 | |
12d3ba82 BC |
2210 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
2211 | has_node_name ? node_name : NULL, | |
2212 | &local_err); | |
84d18f06 | 2213 | if (local_err) { |
12d3ba82 | 2214 | error_propagate(errp, local_err); |
a4dea8a9 | 2215 | return; |
666daa68 MA |
2216 | } |
2217 | ||
e3442099 SH |
2218 | aio_context = bdrv_get_aio_context(bs); |
2219 | aio_context_acquire(aio_context); | |
2220 | ||
4d2855a3 | 2221 | bdrv_add_key(bs, password, errp); |
e3442099 | 2222 | |
e3442099 | 2223 | aio_context_release(aio_context); |
666daa68 MA |
2224 | } |
2225 | ||
7d8a9f71 HR |
2226 | void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, |
2227 | Error **errp) | |
2228 | { | |
2229 | BlockBackend *blk; | |
2230 | bool locked; | |
2231 | ||
2232 | if (!has_force) { | |
2233 | force = false; | |
2234 | } | |
2235 | ||
2236 | blk = blk_by_name(device); | |
2237 | if (!blk) { | |
2238 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2239 | "Device '%s' not found", device); | |
2240 | return; | |
2241 | } | |
2242 | ||
2243 | if (!blk_dev_has_removable_media(blk)) { | |
2244 | error_setg(errp, "Device '%s' is not removable", device); | |
2245 | return; | |
2246 | } | |
2247 | ||
2248 | if (blk_dev_is_tray_open(blk)) { | |
2249 | return; | |
2250 | } | |
2251 | ||
2252 | locked = blk_dev_is_medium_locked(blk); | |
2253 | if (locked) { | |
2254 | blk_dev_eject_request(blk, force); | |
2255 | } | |
2256 | ||
2257 | if (!locked || force) { | |
2258 | blk_dev_change_media_cb(blk, false); | |
2259 | } | |
2260 | } | |
2261 | ||
abaaf59d HR |
2262 | void qmp_blockdev_close_tray(const char *device, Error **errp) |
2263 | { | |
2264 | BlockBackend *blk; | |
2265 | ||
2266 | blk = blk_by_name(device); | |
2267 | if (!blk) { | |
2268 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2269 | "Device '%s' not found", device); | |
2270 | return; | |
2271 | } | |
2272 | ||
2273 | if (!blk_dev_has_removable_media(blk)) { | |
2274 | error_setg(errp, "Device '%s' is not removable", device); | |
2275 | return; | |
2276 | } | |
2277 | ||
2278 | if (!blk_dev_is_tray_open(blk)) { | |
2279 | return; | |
2280 | } | |
2281 | ||
2282 | blk_dev_change_media_cb(blk, true); | |
2283 | } | |
2284 | ||
2814f672 HR |
2285 | void qmp_blockdev_remove_medium(const char *device, Error **errp) |
2286 | { | |
2287 | BlockBackend *blk; | |
2288 | BlockDriverState *bs; | |
2289 | AioContext *aio_context; | |
2290 | bool has_device; | |
2291 | ||
2292 | blk = blk_by_name(device); | |
2293 | if (!blk) { | |
2294 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2295 | "Device '%s' not found", device); | |
2296 | return; | |
2297 | } | |
2298 | ||
2299 | /* For BBs without a device, we can exchange the BDS tree at will */ | |
2300 | has_device = blk_get_attached_dev(blk); | |
2301 | ||
2302 | if (has_device && !blk_dev_has_removable_media(blk)) { | |
2303 | error_setg(errp, "Device '%s' is not removable", device); | |
2304 | return; | |
2305 | } | |
2306 | ||
2307 | if (has_device && !blk_dev_is_tray_open(blk)) { | |
2308 | error_setg(errp, "Tray of device '%s' is not open", device); | |
2309 | return; | |
2310 | } | |
2311 | ||
2312 | bs = blk_bs(blk); | |
2313 | if (!bs) { | |
2314 | return; | |
2315 | } | |
2316 | ||
2317 | aio_context = bdrv_get_aio_context(bs); | |
2318 | aio_context_acquire(aio_context); | |
2319 | ||
2320 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { | |
2321 | goto out; | |
2322 | } | |
2323 | ||
2324 | /* This follows the convention established by bdrv_make_anon() */ | |
2325 | if (bs->device_list.tqe_prev) { | |
2326 | QTAILQ_REMOVE(&bdrv_states, bs, device_list); | |
2327 | bs->device_list.tqe_prev = NULL; | |
2328 | } | |
2329 | ||
2330 | blk_remove_bs(blk); | |
2331 | ||
2332 | out: | |
2333 | aio_context_release(aio_context); | |
2334 | } | |
2335 | ||
d1299882 HR |
2336 | static void qmp_blockdev_insert_anon_medium(const char *device, |
2337 | BlockDriverState *bs, Error **errp) | |
2338 | { | |
2339 | BlockBackend *blk; | |
2340 | bool has_device; | |
2341 | ||
2342 | blk = blk_by_name(device); | |
2343 | if (!blk) { | |
2344 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2345 | "Device '%s' not found", device); | |
2346 | return; | |
2347 | } | |
2348 | ||
2349 | /* For BBs without a device, we can exchange the BDS tree at will */ | |
2350 | has_device = blk_get_attached_dev(blk); | |
2351 | ||
2352 | if (has_device && !blk_dev_has_removable_media(blk)) { | |
2353 | error_setg(errp, "Device '%s' is not removable", device); | |
2354 | return; | |
2355 | } | |
2356 | ||
2357 | if (has_device && !blk_dev_is_tray_open(blk)) { | |
2358 | error_setg(errp, "Tray of device '%s' is not open", device); | |
2359 | return; | |
2360 | } | |
2361 | ||
2362 | if (blk_bs(blk)) { | |
2363 | error_setg(errp, "There already is a medium in device '%s'", device); | |
2364 | return; | |
2365 | } | |
2366 | ||
2367 | blk_insert_bs(blk, bs); | |
2368 | ||
2369 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); | |
2370 | } | |
2371 | ||
2372 | void qmp_blockdev_insert_medium(const char *device, const char *node_name, | |
2373 | Error **errp) | |
2374 | { | |
2375 | BlockDriverState *bs; | |
2376 | ||
2377 | bs = bdrv_find_node(node_name); | |
2378 | if (!bs) { | |
2379 | error_setg(errp, "Node '%s' not found", node_name); | |
2380 | return; | |
2381 | } | |
2382 | ||
2383 | if (bs->blk) { | |
2384 | error_setg(errp, "Node '%s' is already in use by '%s'", node_name, | |
2385 | blk_name(bs->blk)); | |
2386 | return; | |
2387 | } | |
2388 | ||
2389 | qmp_blockdev_insert_anon_medium(device, bs, errp); | |
2390 | } | |
2391 | ||
24fb4133 HR |
2392 | void qmp_blockdev_change_medium(const char *device, const char *filename, |
2393 | bool has_format, const char *format, | |
39ff43d9 HR |
2394 | bool has_read_only, |
2395 | BlockdevChangeReadOnlyMode read_only, | |
24fb4133 | 2396 | Error **errp) |
de2c6c05 HR |
2397 | { |
2398 | BlockBackend *blk; | |
2399 | BlockDriverState *medium_bs = NULL; | |
2400 | int bdrv_flags, ret; | |
2401 | QDict *options = NULL; | |
2402 | Error *err = NULL; | |
2403 | ||
2404 | blk = blk_by_name(device); | |
2405 | if (!blk) { | |
2406 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2407 | "Device '%s' not found", device); | |
2408 | goto fail; | |
2409 | } | |
2410 | ||
2411 | if (blk_bs(blk)) { | |
2412 | blk_update_root_state(blk); | |
2413 | } | |
2414 | ||
2415 | bdrv_flags = blk_get_open_flags_from_root_state(blk); | |
2416 | ||
39ff43d9 HR |
2417 | if (!has_read_only) { |
2418 | read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; | |
2419 | } | |
2420 | ||
2421 | switch (read_only) { | |
2422 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: | |
2423 | break; | |
2424 | ||
2425 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: | |
2426 | bdrv_flags &= ~BDRV_O_RDWR; | |
2427 | break; | |
2428 | ||
2429 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: | |
2430 | bdrv_flags |= BDRV_O_RDWR; | |
2431 | break; | |
2432 | ||
2433 | default: | |
2434 | abort(); | |
2435 | } | |
2436 | ||
24fb4133 | 2437 | if (has_format) { |
de2c6c05 HR |
2438 | options = qdict_new(); |
2439 | qdict_put(options, "driver", qstring_from_str(format)); | |
2440 | } | |
2441 | ||
2442 | assert(!medium_bs); | |
2443 | ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); | |
2444 | if (ret < 0) { | |
2445 | goto fail; | |
2446 | } | |
2447 | ||
2448 | blk_apply_root_state(blk, medium_bs); | |
2449 | ||
2450 | bdrv_add_key(medium_bs, NULL, &err); | |
2451 | if (err) { | |
2452 | error_propagate(errp, err); | |
2453 | goto fail; | |
2454 | } | |
2455 | ||
2456 | qmp_blockdev_open_tray(device, false, false, &err); | |
2457 | if (err) { | |
2458 | error_propagate(errp, err); | |
2459 | goto fail; | |
2460 | } | |
2461 | ||
2462 | qmp_blockdev_remove_medium(device, &err); | |
2463 | if (err) { | |
2464 | error_propagate(errp, err); | |
2465 | goto fail; | |
2466 | } | |
2467 | ||
2468 | qmp_blockdev_insert_anon_medium(device, medium_bs, &err); | |
2469 | if (err) { | |
2470 | error_propagate(errp, err); | |
2471 | goto fail; | |
2472 | } | |
2473 | ||
2474 | qmp_blockdev_close_tray(device, errp); | |
2475 | ||
2476 | fail: | |
2477 | /* If the medium has been inserted, the device has its own reference, so | |
2478 | * ours must be relinquished; and if it has not been inserted successfully, | |
2479 | * the reference must be relinquished anyway */ | |
2480 | bdrv_unref(medium_bs); | |
2481 | } | |
2482 | ||
727f005e | 2483 | /* throttling disk I/O limits */ |
80047da5 | 2484 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
3e9fab69 BC |
2485 | int64_t bps_wr, |
2486 | int64_t iops, | |
2487 | int64_t iops_rd, | |
2488 | int64_t iops_wr, | |
2489 | bool has_bps_max, | |
2490 | int64_t bps_max, | |
2491 | bool has_bps_rd_max, | |
2492 | int64_t bps_rd_max, | |
2493 | bool has_bps_wr_max, | |
2494 | int64_t bps_wr_max, | |
2495 | bool has_iops_max, | |
2496 | int64_t iops_max, | |
2497 | bool has_iops_rd_max, | |
2498 | int64_t iops_rd_max, | |
2499 | bool has_iops_wr_max, | |
2024c1df BC |
2500 | int64_t iops_wr_max, |
2501 | bool has_iops_size, | |
76f4afb4 AG |
2502 | int64_t iops_size, |
2503 | bool has_group, | |
2504 | const char *group, Error **errp) | |
727f005e | 2505 | { |
cc0681c4 | 2506 | ThrottleConfig cfg; |
727f005e | 2507 | BlockDriverState *bs; |
a0e8544c | 2508 | BlockBackend *blk; |
b15446fd | 2509 | AioContext *aio_context; |
727f005e | 2510 | |
a0e8544c FZ |
2511 | blk = blk_by_name(device); |
2512 | if (!blk) { | |
75158ebb MA |
2513 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2514 | "Device '%s' not found", device); | |
80047da5 | 2515 | return; |
727f005e | 2516 | } |
5433c24f HR |
2517 | |
2518 | aio_context = blk_get_aio_context(blk); | |
2519 | aio_context_acquire(aio_context); | |
2520 | ||
a0e8544c | 2521 | bs = blk_bs(blk); |
5433c24f HR |
2522 | if (!bs) { |
2523 | error_setg(errp, "Device '%s' has no medium", device); | |
2524 | goto out; | |
2525 | } | |
727f005e | 2526 | |
cc0681c4 BC |
2527 | memset(&cfg, 0, sizeof(cfg)); |
2528 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; | |
2529 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; | |
2530 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; | |
2531 | ||
2532 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; | |
2533 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; | |
2534 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; | |
2535 | ||
3e9fab69 BC |
2536 | if (has_bps_max) { |
2537 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; | |
2538 | } | |
2539 | if (has_bps_rd_max) { | |
2540 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; | |
2541 | } | |
2542 | if (has_bps_wr_max) { | |
2543 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; | |
2544 | } | |
2545 | if (has_iops_max) { | |
2546 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; | |
2547 | } | |
2548 | if (has_iops_rd_max) { | |
2549 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; | |
2550 | } | |
2551 | if (has_iops_wr_max) { | |
2552 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; | |
2553 | } | |
727f005e | 2554 | |
2024c1df BC |
2555 | if (has_iops_size) { |
2556 | cfg.op_size = iops_size; | |
2557 | } | |
cc0681c4 BC |
2558 | |
2559 | if (!check_throttle_config(&cfg, errp)) { | |
5433c24f | 2560 | goto out; |
727f005e ZYW |
2561 | } |
2562 | ||
76f4afb4 AG |
2563 | if (throttle_enabled(&cfg)) { |
2564 | /* Enable I/O limits if they're not enabled yet, otherwise | |
2565 | * just update the throttling group. */ | |
a0d64a61 | 2566 | if (!bs->throttle_state) { |
76f4afb4 AG |
2567 | bdrv_io_limits_enable(bs, has_group ? group : device); |
2568 | } else if (has_group) { | |
2569 | bdrv_io_limits_update_group(bs, group); | |
2570 | } | |
2571 | /* Set the new throttling configuration */ | |
cc0681c4 | 2572 | bdrv_set_io_limits(bs, &cfg); |
a0d64a61 | 2573 | } else if (bs->throttle_state) { |
76f4afb4 AG |
2574 | /* If all throttling settings are set to 0, disable I/O limits */ |
2575 | bdrv_io_limits_disable(bs); | |
727f005e | 2576 | } |
b15446fd | 2577 | |
5433c24f | 2578 | out: |
b15446fd | 2579 | aio_context_release(aio_context); |
727f005e ZYW |
2580 | } |
2581 | ||
341ebc2f JS |
2582 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, |
2583 | bool has_granularity, uint32_t granularity, | |
2584 | Error **errp) | |
2585 | { | |
2586 | AioContext *aio_context; | |
2587 | BlockDriverState *bs; | |
2588 | ||
2589 | if (!name || name[0] == '\0') { | |
2590 | error_setg(errp, "Bitmap name cannot be empty"); | |
2591 | return; | |
2592 | } | |
2593 | ||
2594 | bs = bdrv_lookup_bs(node, node, errp); | |
2595 | if (!bs) { | |
2596 | return; | |
2597 | } | |
2598 | ||
2599 | aio_context = bdrv_get_aio_context(bs); | |
2600 | aio_context_acquire(aio_context); | |
2601 | ||
2602 | if (has_granularity) { | |
2603 | if (granularity < 512 || !is_power_of_2(granularity)) { | |
2604 | error_setg(errp, "Granularity must be power of 2 " | |
2605 | "and at least 512"); | |
2606 | goto out; | |
2607 | } | |
2608 | } else { | |
2609 | /* Default to cluster size, if available: */ | |
2610 | granularity = bdrv_get_default_bitmap_granularity(bs); | |
2611 | } | |
2612 | ||
2613 | bdrv_create_dirty_bitmap(bs, granularity, name, errp); | |
2614 | ||
2615 | out: | |
2616 | aio_context_release(aio_context); | |
2617 | } | |
2618 | ||
2619 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, | |
2620 | Error **errp) | |
2621 | { | |
2622 | AioContext *aio_context; | |
2623 | BlockDriverState *bs; | |
2624 | BdrvDirtyBitmap *bitmap; | |
2625 | ||
2626 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); | |
2627 | if (!bitmap || !bs) { | |
2628 | return; | |
2629 | } | |
2630 | ||
9bd2b08f JS |
2631 | if (bdrv_dirty_bitmap_frozen(bitmap)) { |
2632 | error_setg(errp, | |
2633 | "Bitmap '%s' is currently frozen and cannot be removed", | |
2634 | name); | |
2635 | goto out; | |
2636 | } | |
20dca810 | 2637 | bdrv_dirty_bitmap_make_anon(bitmap); |
341ebc2f JS |
2638 | bdrv_release_dirty_bitmap(bs, bitmap); |
2639 | ||
9bd2b08f | 2640 | out: |
341ebc2f JS |
2641 | aio_context_release(aio_context); |
2642 | } | |
2643 | ||
e74e6b78 JS |
2644 | /** |
2645 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap | |
2646 | * immediately after a full backup operation. | |
2647 | */ | |
2648 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, | |
2649 | Error **errp) | |
2650 | { | |
2651 | AioContext *aio_context; | |
2652 | BdrvDirtyBitmap *bitmap; | |
2653 | BlockDriverState *bs; | |
2654 | ||
2655 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); | |
2656 | if (!bitmap || !bs) { | |
2657 | return; | |
2658 | } | |
2659 | ||
2660 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2661 | error_setg(errp, | |
2662 | "Bitmap '%s' is currently frozen and cannot be modified", | |
2663 | name); | |
2664 | goto out; | |
2665 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
2666 | error_setg(errp, | |
2667 | "Bitmap '%s' is currently disabled and cannot be cleared", | |
2668 | name); | |
2669 | goto out; | |
2670 | } | |
2671 | ||
df9a681d | 2672 | bdrv_clear_dirty_bitmap(bitmap, NULL); |
e74e6b78 JS |
2673 | |
2674 | out: | |
2675 | aio_context_release(aio_context); | |
2676 | } | |
2677 | ||
072ebe6b | 2678 | void hmp_drive_del(Monitor *mon, const QDict *qdict) |
9063f814 RH |
2679 | { |
2680 | const char *id = qdict_get_str(qdict, "id"); | |
7e7d56d9 | 2681 | BlockBackend *blk; |
9063f814 | 2682 | BlockDriverState *bs; |
8ad4202b | 2683 | AioContext *aio_context; |
3718d8ab | 2684 | Error *local_err = NULL; |
9063f814 | 2685 | |
7e7d56d9 MA |
2686 | blk = blk_by_name(id); |
2687 | if (!blk) { | |
b1422f20 | 2688 | error_report("Device '%s' not found", id); |
072ebe6b | 2689 | return; |
9063f814 | 2690 | } |
8ad4202b | 2691 | |
26f8b3a8 | 2692 | if (!blk_legacy_dinfo(blk)) { |
48f364dd MA |
2693 | error_report("Deleting device added with blockdev-add" |
2694 | " is not supported"); | |
072ebe6b | 2695 | return; |
48f364dd MA |
2696 | } |
2697 | ||
5433c24f | 2698 | aio_context = blk_get_aio_context(blk); |
8ad4202b SH |
2699 | aio_context_acquire(aio_context); |
2700 | ||
5433c24f HR |
2701 | bs = blk_bs(blk); |
2702 | if (bs) { | |
2703 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { | |
2704 | error_report_err(local_err); | |
2705 | aio_context_release(aio_context); | |
2706 | return; | |
2707 | } | |
9063f814 | 2708 | |
5433c24f HR |
2709 | bdrv_close(bs); |
2710 | } | |
9063f814 | 2711 | |
fa879d62 | 2712 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
2713 | * then we need to make the drive anonymous until the device |
2714 | * can be removed. If this is a drive with no device backing | |
2715 | * then we can just get rid of the block driver state right here. | |
2716 | */ | |
a7f53e26 | 2717 | if (blk_get_attached_dev(blk)) { |
3e5a50d6 | 2718 | blk_hide_on_behalf_of_hmp_drive_del(blk); |
293c51a6 | 2719 | /* Further I/O must not pause the guest */ |
373340b2 HR |
2720 | blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, |
2721 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 | 2722 | } else { |
b9fe8a7a | 2723 | blk_unref(blk); |
9063f814 RH |
2724 | } |
2725 | ||
8ad4202b | 2726 | aio_context_release(aio_context); |
9063f814 | 2727 | } |
6d4a2b3a | 2728 | |
3b1dbd11 BC |
2729 | void qmp_block_resize(bool has_device, const char *device, |
2730 | bool has_node_name, const char *node_name, | |
2731 | int64_t size, Error **errp) | |
6d4a2b3a | 2732 | { |
3b1dbd11 | 2733 | Error *local_err = NULL; |
6d4a2b3a | 2734 | BlockDriverState *bs; |
927e0e76 | 2735 | AioContext *aio_context; |
8732901e | 2736 | int ret; |
6d4a2b3a | 2737 | |
3b1dbd11 BC |
2738 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
2739 | has_node_name ? node_name : NULL, | |
2740 | &local_err); | |
84d18f06 | 2741 | if (local_err) { |
3b1dbd11 BC |
2742 | error_propagate(errp, local_err); |
2743 | return; | |
2744 | } | |
2745 | ||
927e0e76 SH |
2746 | aio_context = bdrv_get_aio_context(bs); |
2747 | aio_context_acquire(aio_context); | |
2748 | ||
3b1dbd11 | 2749 | if (!bdrv_is_first_non_filter(bs)) { |
c6bd8c70 | 2750 | error_setg(errp, QERR_FEATURE_DISABLED, "resize"); |
927e0e76 | 2751 | goto out; |
6d4a2b3a CH |
2752 | } |
2753 | ||
2754 | if (size < 0) { | |
c6bd8c70 | 2755 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 2756 | goto out; |
6d4a2b3a CH |
2757 | } |
2758 | ||
9c75e168 | 2759 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
c6bd8c70 | 2760 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
927e0e76 | 2761 | goto out; |
9c75e168 JC |
2762 | } |
2763 | ||
92b7a08d PL |
2764 | /* complete all in-flight operations before resizing the device */ |
2765 | bdrv_drain_all(); | |
2766 | ||
8732901e KW |
2767 | ret = bdrv_truncate(bs, size); |
2768 | switch (ret) { | |
939a1cc3 SH |
2769 | case 0: |
2770 | break; | |
2771 | case -ENOMEDIUM: | |
c6bd8c70 | 2772 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
939a1cc3 SH |
2773 | break; |
2774 | case -ENOTSUP: | |
c6bd8c70 | 2775 | error_setg(errp, QERR_UNSUPPORTED); |
939a1cc3 SH |
2776 | break; |
2777 | case -EACCES: | |
81e5f78a | 2778 | error_setg(errp, "Device '%s' is read only", device); |
939a1cc3 SH |
2779 | break; |
2780 | case -EBUSY: | |
c6bd8c70 | 2781 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
939a1cc3 SH |
2782 | break; |
2783 | default: | |
8732901e | 2784 | error_setg_errno(errp, -ret, "Could not resize"); |
939a1cc3 | 2785 | break; |
6d4a2b3a | 2786 | } |
927e0e76 SH |
2787 | |
2788 | out: | |
2789 | aio_context_release(aio_context); | |
6d4a2b3a | 2790 | } |
12bd451f | 2791 | |
9abf2dba | 2792 | static void block_job_cb(void *opaque, int ret) |
12bd451f | 2793 | { |
723c5d93 SH |
2794 | /* Note that this function may be executed from another AioContext besides |
2795 | * the QEMU main loop. If you need to access anything that assumes the | |
2796 | * QEMU global mutex, use a BH or introduce a mutex. | |
2797 | */ | |
2798 | ||
12bd451f | 2799 | BlockDriverState *bs = opaque; |
bcada37b | 2800 | const char *msg = NULL; |
12bd451f | 2801 | |
9abf2dba | 2802 | trace_block_job_cb(bs, bs->job, ret); |
12bd451f SH |
2803 | |
2804 | assert(bs->job); | |
bcada37b | 2805 | |
12bd451f | 2806 | if (ret < 0) { |
bcada37b | 2807 | msg = strerror(-ret); |
12bd451f SH |
2808 | } |
2809 | ||
370521a1 | 2810 | if (block_job_is_cancelled(bs->job)) { |
bcada37b | 2811 | block_job_event_cancelled(bs->job); |
370521a1 | 2812 | } else { |
bcada37b | 2813 | block_job_event_completed(bs->job, msg); |
370521a1 | 2814 | } |
12bd451f SH |
2815 | } |
2816 | ||
13d8cc51 JC |
2817 | void qmp_block_stream(const char *device, |
2818 | bool has_base, const char *base, | |
2819 | bool has_backing_file, const char *backing_file, | |
2820 | bool has_speed, int64_t speed, | |
1d809098 PB |
2821 | bool has_on_error, BlockdevOnError on_error, |
2822 | Error **errp) | |
12bd451f | 2823 | { |
a0e8544c | 2824 | BlockBackend *blk; |
12bd451f | 2825 | BlockDriverState *bs; |
c8c3080f | 2826 | BlockDriverState *base_bs = NULL; |
f3e69beb | 2827 | AioContext *aio_context; |
fd7f8c65 | 2828 | Error *local_err = NULL; |
13d8cc51 | 2829 | const char *base_name = NULL; |
12bd451f | 2830 | |
1d809098 PB |
2831 | if (!has_on_error) { |
2832 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2833 | } | |
2834 | ||
a0e8544c FZ |
2835 | blk = blk_by_name(device); |
2836 | if (!blk) { | |
75158ebb MA |
2837 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2838 | "Device '%s' not found", device); | |
12bd451f SH |
2839 | return; |
2840 | } | |
2841 | ||
5433c24f | 2842 | aio_context = blk_get_aio_context(blk); |
f3e69beb SH |
2843 | aio_context_acquire(aio_context); |
2844 | ||
5433c24f HR |
2845 | if (!blk_is_available(blk)) { |
2846 | error_setg(errp, "Device '%s' has no medium", device); | |
2847 | goto out; | |
2848 | } | |
2849 | bs = blk_bs(blk); | |
2850 | ||
628ff683 | 2851 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { |
f3e69beb | 2852 | goto out; |
628ff683 FZ |
2853 | } |
2854 | ||
13d8cc51 | 2855 | if (has_base) { |
c8c3080f MT |
2856 | base_bs = bdrv_find_backing_image(bs, base); |
2857 | if (base_bs == NULL) { | |
c6bd8c70 | 2858 | error_setg(errp, QERR_BASE_NOT_FOUND, base); |
f3e69beb | 2859 | goto out; |
c8c3080f | 2860 | } |
f3e69beb | 2861 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
13d8cc51 | 2862 | base_name = base; |
12bd451f SH |
2863 | } |
2864 | ||
13d8cc51 JC |
2865 | /* if we are streaming the entire chain, the result will have no backing |
2866 | * file, and specifying one is therefore an error */ | |
2867 | if (base_bs == NULL && has_backing_file) { | |
2868 | error_setg(errp, "backing file specified, but streaming the " | |
2869 | "entire chain"); | |
f3e69beb | 2870 | goto out; |
13d8cc51 JC |
2871 | } |
2872 | ||
2873 | /* backing_file string overrides base bs filename */ | |
2874 | base_name = has_backing_file ? backing_file : base_name; | |
2875 | ||
2876 | stream_start(bs, base_bs, base_name, has_speed ? speed : 0, | |
1d809098 | 2877 | on_error, block_job_cb, bs, &local_err); |
84d18f06 | 2878 | if (local_err) { |
fd7f8c65 | 2879 | error_propagate(errp, local_err); |
f3e69beb | 2880 | goto out; |
12bd451f SH |
2881 | } |
2882 | ||
2883 | trace_qmp_block_stream(bs, bs->job); | |
f3e69beb SH |
2884 | |
2885 | out: | |
2886 | aio_context_release(aio_context); | |
12bd451f | 2887 | } |
2d47c6e9 | 2888 | |
ed61fc10 | 2889 | void qmp_block_commit(const char *device, |
7676e2c5 JC |
2890 | bool has_base, const char *base, |
2891 | bool has_top, const char *top, | |
54e26900 | 2892 | bool has_backing_file, const char *backing_file, |
ed61fc10 JC |
2893 | bool has_speed, int64_t speed, |
2894 | Error **errp) | |
2895 | { | |
a0e8544c | 2896 | BlockBackend *blk; |
ed61fc10 JC |
2897 | BlockDriverState *bs; |
2898 | BlockDriverState *base_bs, *top_bs; | |
9e85cd5c | 2899 | AioContext *aio_context; |
ed61fc10 JC |
2900 | Error *local_err = NULL; |
2901 | /* This will be part of the QMP command, if/when the | |
2902 | * BlockdevOnError change for blkmirror makes it in | |
2903 | */ | |
92aa5c6d | 2904 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 | 2905 | |
54504663 HR |
2906 | if (!has_speed) { |
2907 | speed = 0; | |
2908 | } | |
2909 | ||
7676e2c5 JC |
2910 | /* Important Note: |
2911 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2912 | * live commit feature versions; for this to work, we must make sure to | |
2913 | * perform the device lookup before any generic errors that may occur in a | |
2914 | * scenario in which all optional arguments are omitted. */ | |
a0e8544c FZ |
2915 | blk = blk_by_name(device); |
2916 | if (!blk) { | |
75158ebb MA |
2917 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2918 | "Device '%s' not found", device); | |
ed61fc10 | 2919 | return; |
628ff683 FZ |
2920 | } |
2921 | ||
5433c24f | 2922 | aio_context = blk_get_aio_context(blk); |
9e85cd5c SH |
2923 | aio_context_acquire(aio_context); |
2924 | ||
5433c24f HR |
2925 | if (!blk_is_available(blk)) { |
2926 | error_setg(errp, "Device '%s' has no medium", device); | |
2927 | goto out; | |
2928 | } | |
2929 | bs = blk_bs(blk); | |
2930 | ||
bb00021d | 2931 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
9e85cd5c | 2932 | goto out; |
ed61fc10 | 2933 | } |
ed61fc10 JC |
2934 | |
2935 | /* default top_bs is the active layer */ | |
2936 | top_bs = bs; | |
2937 | ||
7676e2c5 | 2938 | if (has_top && top) { |
ed61fc10 JC |
2939 | if (strcmp(bs->filename, top) != 0) { |
2940 | top_bs = bdrv_find_backing_image(bs, top); | |
2941 | } | |
2942 | } | |
2943 | ||
2944 | if (top_bs == NULL) { | |
2945 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 2946 | goto out; |
ed61fc10 JC |
2947 | } |
2948 | ||
9e85cd5c SH |
2949 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
2950 | ||
d5208c45 JC |
2951 | if (has_base && base) { |
2952 | base_bs = bdrv_find_backing_image(top_bs, base); | |
2953 | } else { | |
2954 | base_bs = bdrv_find_base(top_bs); | |
2955 | } | |
2956 | ||
2957 | if (base_bs == NULL) { | |
c6bd8c70 | 2958 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
9e85cd5c | 2959 | goto out; |
d5208c45 JC |
2960 | } |
2961 | ||
9e85cd5c SH |
2962 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
2963 | ||
bb00021d FZ |
2964 | if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { |
2965 | goto out; | |
2966 | } | |
2967 | ||
7676e2c5 JC |
2968 | /* Do not allow attempts to commit an image into itself */ |
2969 | if (top_bs == base_bs) { | |
2970 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 2971 | goto out; |
7676e2c5 JC |
2972 | } |
2973 | ||
20a63d2c | 2974 | if (top_bs == bs) { |
54e26900 JC |
2975 | if (has_backing_file) { |
2976 | error_setg(errp, "'backing-file' specified," | |
2977 | " but 'top' is the active layer"); | |
9e85cd5c | 2978 | goto out; |
54e26900 | 2979 | } |
20a63d2c FZ |
2980 | commit_active_start(bs, base_bs, speed, on_error, block_job_cb, |
2981 | bs, &local_err); | |
2982 | } else { | |
2983 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, | |
54e26900 | 2984 | has_backing_file ? backing_file : NULL, &local_err); |
20a63d2c | 2985 | } |
ed61fc10 JC |
2986 | if (local_err != NULL) { |
2987 | error_propagate(errp, local_err); | |
9e85cd5c | 2988 | goto out; |
ed61fc10 | 2989 | } |
9e85cd5c SH |
2990 | |
2991 | out: | |
2992 | aio_context_release(aio_context); | |
ed61fc10 JC |
2993 | } |
2994 | ||
78f51fde JS |
2995 | static void do_drive_backup(const char *device, const char *target, |
2996 | bool has_format, const char *format, | |
2997 | enum MirrorSyncMode sync, | |
2998 | bool has_mode, enum NewImageMode mode, | |
2999 | bool has_speed, int64_t speed, | |
3000 | bool has_bitmap, const char *bitmap, | |
3001 | bool has_on_source_error, | |
3002 | BlockdevOnError on_source_error, | |
3003 | bool has_on_target_error, | |
3004 | BlockdevOnError on_target_error, | |
3005 | BlockJobTxn *txn, Error **errp) | |
99a9addf | 3006 | { |
a0e8544c | 3007 | BlockBackend *blk; |
99a9addf SH |
3008 | BlockDriverState *bs; |
3009 | BlockDriverState *target_bs; | |
fc5d3f84 | 3010 | BlockDriverState *source = NULL; |
d58d8453 | 3011 | BdrvDirtyBitmap *bmap = NULL; |
761731b1 | 3012 | AioContext *aio_context; |
e6641719 | 3013 | QDict *options = NULL; |
99a9addf SH |
3014 | Error *local_err = NULL; |
3015 | int flags; | |
3016 | int64_t size; | |
3017 | int ret; | |
3018 | ||
3019 | if (!has_speed) { | |
3020 | speed = 0; | |
3021 | } | |
3022 | if (!has_on_source_error) { | |
3023 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3024 | } | |
3025 | if (!has_on_target_error) { | |
3026 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3027 | } | |
3028 | if (!has_mode) { | |
3029 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
3030 | } | |
3031 | ||
a0e8544c FZ |
3032 | blk = blk_by_name(device); |
3033 | if (!blk) { | |
75158ebb MA |
3034 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
3035 | "Device '%s' not found", device); | |
99a9addf SH |
3036 | return; |
3037 | } | |
3038 | ||
5433c24f | 3039 | aio_context = blk_get_aio_context(blk); |
761731b1 SH |
3040 | aio_context_acquire(aio_context); |
3041 | ||
c29c1dd3 FZ |
3042 | /* Although backup_run has this check too, we need to use bs->drv below, so |
3043 | * do an early check redundantly. */ | |
5433c24f | 3044 | if (!blk_is_available(blk)) { |
c6bd8c70 | 3045 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
761731b1 | 3046 | goto out; |
99a9addf | 3047 | } |
5433c24f | 3048 | bs = blk_bs(blk); |
99a9addf SH |
3049 | |
3050 | if (!has_format) { | |
3051 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
3052 | } | |
99a9addf | 3053 | |
c29c1dd3 | 3054 | /* Early check to avoid creating target */ |
3718d8ab | 3055 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
761731b1 | 3056 | goto out; |
99a9addf SH |
3057 | } |
3058 | ||
3059 | flags = bs->open_flags | BDRV_O_RDWR; | |
3060 | ||
fc5d3f84 IM |
3061 | /* See if we have a backing HD we can use to create our new image |
3062 | * on top of. */ | |
3063 | if (sync == MIRROR_SYNC_MODE_TOP) { | |
760e0063 | 3064 | source = backing_bs(bs); |
fc5d3f84 IM |
3065 | if (!source) { |
3066 | sync = MIRROR_SYNC_MODE_FULL; | |
3067 | } | |
3068 | } | |
3069 | if (sync == MIRROR_SYNC_MODE_NONE) { | |
3070 | source = bs; | |
3071 | } | |
3072 | ||
99a9addf SH |
3073 | size = bdrv_getlength(bs); |
3074 | if (size < 0) { | |
3075 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
761731b1 | 3076 | goto out; |
99a9addf SH |
3077 | } |
3078 | ||
3079 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
e6641719 | 3080 | assert(format); |
fc5d3f84 IM |
3081 | if (source) { |
3082 | bdrv_img_create(target, format, source->filename, | |
3083 | source->drv->format_name, NULL, | |
3084 | size, flags, &local_err, false); | |
3085 | } else { | |
3086 | bdrv_img_create(target, format, NULL, NULL, NULL, | |
3087 | size, flags, &local_err, false); | |
3088 | } | |
99a9addf SH |
3089 | } |
3090 | ||
84d18f06 | 3091 | if (local_err) { |
99a9addf | 3092 | error_propagate(errp, local_err); |
761731b1 | 3093 | goto out; |
99a9addf SH |
3094 | } |
3095 | ||
e6641719 HR |
3096 | if (format) { |
3097 | options = qdict_new(); | |
3098 | qdict_put(options, "driver", qstring_from_str(format)); | |
3099 | } | |
3100 | ||
f67503e5 | 3101 | target_bs = NULL; |
6ebf9aa2 | 3102 | ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); |
99a9addf | 3103 | if (ret < 0) { |
34b5d2c6 | 3104 | error_propagate(errp, local_err); |
761731b1 | 3105 | goto out; |
99a9addf SH |
3106 | } |
3107 | ||
761731b1 SH |
3108 | bdrv_set_aio_context(target_bs, aio_context); |
3109 | ||
d58d8453 JS |
3110 | if (has_bitmap) { |
3111 | bmap = bdrv_find_dirty_bitmap(bs, bitmap); | |
3112 | if (!bmap) { | |
3113 | error_setg(errp, "Bitmap '%s' could not be found", bitmap); | |
3114 | goto out; | |
3115 | } | |
3116 | } | |
3117 | ||
3118 | backup_start(bs, target_bs, speed, sync, bmap, | |
3119 | on_source_error, on_target_error, | |
78f51fde | 3120 | block_job_cb, bs, txn, &local_err); |
99a9addf | 3121 | if (local_err != NULL) { |
4f6fd349 | 3122 | bdrv_unref(target_bs); |
99a9addf | 3123 | error_propagate(errp, local_err); |
761731b1 | 3124 | goto out; |
99a9addf | 3125 | } |
761731b1 SH |
3126 | |
3127 | out: | |
3128 | aio_context_release(aio_context); | |
99a9addf SH |
3129 | } |
3130 | ||
78f51fde JS |
3131 | void qmp_drive_backup(const char *device, const char *target, |
3132 | bool has_format, const char *format, | |
3133 | enum MirrorSyncMode sync, | |
3134 | bool has_mode, enum NewImageMode mode, | |
3135 | bool has_speed, int64_t speed, | |
3136 | bool has_bitmap, const char *bitmap, | |
3137 | bool has_on_source_error, BlockdevOnError on_source_error, | |
3138 | bool has_on_target_error, BlockdevOnError on_target_error, | |
3139 | Error **errp) | |
3140 | { | |
3141 | return do_drive_backup(device, target, has_format, format, sync, | |
3142 | has_mode, mode, has_speed, speed, | |
3143 | has_bitmap, bitmap, | |
3144 | has_on_source_error, on_source_error, | |
3145 | has_on_target_error, on_target_error, | |
3146 | NULL, errp); | |
3147 | } | |
3148 | ||
c13163fb BC |
3149 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
3150 | { | |
d5a8ee60 | 3151 | return bdrv_named_nodes_list(errp); |
c13163fb BC |
3152 | } |
3153 | ||
78f51fde | 3154 | void do_blockdev_backup(const char *device, const char *target, |
c29c1dd3 FZ |
3155 | enum MirrorSyncMode sync, |
3156 | bool has_speed, int64_t speed, | |
3157 | bool has_on_source_error, | |
3158 | BlockdevOnError on_source_error, | |
3159 | bool has_on_target_error, | |
3160 | BlockdevOnError on_target_error, | |
78f51fde | 3161 | BlockJobTxn *txn, Error **errp) |
c29c1dd3 | 3162 | { |
5433c24f | 3163 | BlockBackend *blk, *target_blk; |
c29c1dd3 FZ |
3164 | BlockDriverState *bs; |
3165 | BlockDriverState *target_bs; | |
3166 | Error *local_err = NULL; | |
3167 | AioContext *aio_context; | |
3168 | ||
3169 | if (!has_speed) { | |
3170 | speed = 0; | |
3171 | } | |
3172 | if (!has_on_source_error) { | |
3173 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3174 | } | |
3175 | if (!has_on_target_error) { | |
3176 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3177 | } | |
3178 | ||
a0e8544c FZ |
3179 | blk = blk_by_name(device); |
3180 | if (!blk) { | |
5b347c54 | 3181 | error_setg(errp, "Device '%s' not found", device); |
c29c1dd3 FZ |
3182 | return; |
3183 | } | |
3184 | ||
5433c24f | 3185 | aio_context = blk_get_aio_context(blk); |
c29c1dd3 FZ |
3186 | aio_context_acquire(aio_context); |
3187 | ||
5433c24f HR |
3188 | if (!blk_is_available(blk)) { |
3189 | error_setg(errp, "Device '%s' has no medium", device); | |
3190 | goto out; | |
3191 | } | |
3192 | bs = blk_bs(blk); | |
3193 | ||
3194 | target_blk = blk_by_name(target); | |
3195 | if (!target_blk) { | |
5b347c54 | 3196 | error_setg(errp, "Device '%s' not found", target); |
c29c1dd3 FZ |
3197 | goto out; |
3198 | } | |
5433c24f HR |
3199 | |
3200 | if (!blk_is_available(target_blk)) { | |
3201 | error_setg(errp, "Device '%s' has no medium", target); | |
3202 | goto out; | |
3203 | } | |
3204 | target_bs = blk_bs(target_blk); | |
c29c1dd3 FZ |
3205 | |
3206 | bdrv_ref(target_bs); | |
3207 | bdrv_set_aio_context(target_bs, aio_context); | |
d58d8453 | 3208 | backup_start(bs, target_bs, speed, sync, NULL, on_source_error, |
78f51fde | 3209 | on_target_error, block_job_cb, bs, txn, &local_err); |
c29c1dd3 FZ |
3210 | if (local_err != NULL) { |
3211 | bdrv_unref(target_bs); | |
3212 | error_propagate(errp, local_err); | |
3213 | } | |
3214 | out: | |
3215 | aio_context_release(aio_context); | |
3216 | } | |
3217 | ||
78f51fde JS |
3218 | void qmp_blockdev_backup(const char *device, const char *target, |
3219 | enum MirrorSyncMode sync, | |
3220 | bool has_speed, int64_t speed, | |
3221 | bool has_on_source_error, | |
3222 | BlockdevOnError on_source_error, | |
3223 | bool has_on_target_error, | |
3224 | BlockdevOnError on_target_error, | |
3225 | Error **errp) | |
3226 | { | |
3227 | do_blockdev_backup(device, target, sync, has_speed, speed, | |
3228 | has_on_source_error, on_source_error, | |
3229 | has_on_target_error, on_target_error, | |
3230 | NULL, errp); | |
3231 | } | |
3232 | ||
d9b902db PB |
3233 | void qmp_drive_mirror(const char *device, const char *target, |
3234 | bool has_format, const char *format, | |
4c828dc6 | 3235 | bool has_node_name, const char *node_name, |
09158f00 | 3236 | bool has_replaces, const char *replaces, |
d9b902db PB |
3237 | enum MirrorSyncMode sync, |
3238 | bool has_mode, enum NewImageMode mode, | |
b952b558 | 3239 | bool has_speed, int64_t speed, |
eee13dfe | 3240 | bool has_granularity, uint32_t granularity, |
08e4ed6c | 3241 | bool has_buf_size, int64_t buf_size, |
b952b558 PB |
3242 | bool has_on_source_error, BlockdevOnError on_source_error, |
3243 | bool has_on_target_error, BlockdevOnError on_target_error, | |
0fc9f8ea | 3244 | bool has_unmap, bool unmap, |
b952b558 | 3245 | Error **errp) |
d9b902db | 3246 | { |
a0e8544c | 3247 | BlockBackend *blk; |
d9b902db PB |
3248 | BlockDriverState *bs; |
3249 | BlockDriverState *source, *target_bs; | |
5a7e7a0b | 3250 | AioContext *aio_context; |
d9b902db | 3251 | Error *local_err = NULL; |
e6641719 | 3252 | QDict *options; |
d9b902db | 3253 | int flags; |
ac3c5d83 | 3254 | int64_t size; |
d9b902db PB |
3255 | int ret; |
3256 | ||
3257 | if (!has_speed) { | |
3258 | speed = 0; | |
3259 | } | |
b952b558 PB |
3260 | if (!has_on_source_error) { |
3261 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3262 | } | |
3263 | if (!has_on_target_error) { | |
3264 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3265 | } | |
d9b902db PB |
3266 | if (!has_mode) { |
3267 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
3268 | } | |
eee13dfe PB |
3269 | if (!has_granularity) { |
3270 | granularity = 0; | |
3271 | } | |
08e4ed6c | 3272 | if (!has_buf_size) { |
48ac0a4d | 3273 | buf_size = 0; |
08e4ed6c | 3274 | } |
0fc9f8ea FZ |
3275 | if (!has_unmap) { |
3276 | unmap = true; | |
3277 | } | |
08e4ed6c | 3278 | |
eee13dfe | 3279 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
c6bd8c70 MA |
3280 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3281 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
3282 | return; |
3283 | } | |
3284 | if (granularity & (granularity - 1)) { | |
c6bd8c70 MA |
3285 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3286 | "power of 2"); | |
eee13dfe PB |
3287 | return; |
3288 | } | |
d9b902db | 3289 | |
a0e8544c FZ |
3290 | blk = blk_by_name(device); |
3291 | if (!blk) { | |
75158ebb MA |
3292 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
3293 | "Device '%s' not found", device); | |
d9b902db PB |
3294 | return; |
3295 | } | |
3296 | ||
5433c24f | 3297 | aio_context = blk_get_aio_context(blk); |
5a7e7a0b SH |
3298 | aio_context_acquire(aio_context); |
3299 | ||
5433c24f | 3300 | if (!blk_is_available(blk)) { |
c6bd8c70 | 3301 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
5a7e7a0b | 3302 | goto out; |
d9b902db | 3303 | } |
5433c24f | 3304 | bs = blk_bs(blk); |
d9b902db PB |
3305 | |
3306 | if (!has_format) { | |
3307 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
3308 | } | |
d9b902db | 3309 | |
3718d8ab | 3310 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { |
5a7e7a0b | 3311 | goto out; |
d9b902db PB |
3312 | } |
3313 | ||
3314 | flags = bs->open_flags | BDRV_O_RDWR; | |
760e0063 | 3315 | source = backing_bs(bs); |
d9b902db PB |
3316 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { |
3317 | sync = MIRROR_SYNC_MODE_FULL; | |
3318 | } | |
117e0c82 HR |
3319 | if (sync == MIRROR_SYNC_MODE_NONE) { |
3320 | source = bs; | |
3321 | } | |
d9b902db | 3322 | |
ac3c5d83 SH |
3323 | size = bdrv_getlength(bs); |
3324 | if (size < 0) { | |
3325 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 3326 | goto out; |
ac3c5d83 SH |
3327 | } |
3328 | ||
09158f00 BC |
3329 | if (has_replaces) { |
3330 | BlockDriverState *to_replace_bs; | |
5a7e7a0b SH |
3331 | AioContext *replace_aio_context; |
3332 | int64_t replace_size; | |
09158f00 BC |
3333 | |
3334 | if (!has_node_name) { | |
3335 | error_setg(errp, "a node-name must be provided when replacing a" | |
3336 | " named node of the graph"); | |
5a7e7a0b | 3337 | goto out; |
09158f00 BC |
3338 | } |
3339 | ||
e12f3784 | 3340 | to_replace_bs = check_to_replace_node(bs, replaces, &local_err); |
09158f00 BC |
3341 | |
3342 | if (!to_replace_bs) { | |
3343 | error_propagate(errp, local_err); | |
5a7e7a0b | 3344 | goto out; |
09158f00 BC |
3345 | } |
3346 | ||
5a7e7a0b SH |
3347 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); |
3348 | aio_context_acquire(replace_aio_context); | |
3349 | replace_size = bdrv_getlength(to_replace_bs); | |
3350 | aio_context_release(replace_aio_context); | |
3351 | ||
3352 | if (size != replace_size) { | |
09158f00 BC |
3353 | error_setg(errp, "cannot replace image with a mirror image of " |
3354 | "different size"); | |
5a7e7a0b | 3355 | goto out; |
09158f00 BC |
3356 | } |
3357 | } | |
3358 | ||
14526864 HR |
3359 | if ((sync == MIRROR_SYNC_MODE_FULL || !source) |
3360 | && mode != NEW_IMAGE_MODE_EXISTING) | |
3361 | { | |
d9b902db | 3362 | /* create new image w/o backing file */ |
e6641719 | 3363 | assert(format); |
cf8f2426 | 3364 | bdrv_img_create(target, format, |
f382d43a | 3365 | NULL, NULL, NULL, size, flags, &local_err, false); |
d9b902db PB |
3366 | } else { |
3367 | switch (mode) { | |
3368 | case NEW_IMAGE_MODE_EXISTING: | |
d9b902db PB |
3369 | break; |
3370 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
3371 | /* create new image with backing file */ | |
cf8f2426 LC |
3372 | bdrv_img_create(target, format, |
3373 | source->filename, | |
3374 | source->drv->format_name, | |
f382d43a | 3375 | NULL, size, flags, &local_err, false); |
d9b902db PB |
3376 | break; |
3377 | default: | |
3378 | abort(); | |
3379 | } | |
3380 | } | |
3381 | ||
84d18f06 | 3382 | if (local_err) { |
cf8f2426 | 3383 | error_propagate(errp, local_err); |
5a7e7a0b | 3384 | goto out; |
d9b902db PB |
3385 | } |
3386 | ||
e6641719 | 3387 | options = qdict_new(); |
4c828dc6 | 3388 | if (has_node_name) { |
4c828dc6 BC |
3389 | qdict_put(options, "node-name", qstring_from_str(node_name)); |
3390 | } | |
e6641719 HR |
3391 | if (format) { |
3392 | qdict_put(options, "driver", qstring_from_str(format)); | |
3393 | } | |
4c828dc6 | 3394 | |
b812f671 PB |
3395 | /* Mirroring takes care of copy-on-write using the source's backing |
3396 | * file. | |
3397 | */ | |
f67503e5 | 3398 | target_bs = NULL; |
4c828dc6 | 3399 | ret = bdrv_open(&target_bs, target, NULL, options, |
6ebf9aa2 | 3400 | flags | BDRV_O_NO_BACKING, &local_err); |
d9b902db | 3401 | if (ret < 0) { |
34b5d2c6 | 3402 | error_propagate(errp, local_err); |
5a7e7a0b | 3403 | goto out; |
d9b902db PB |
3404 | } |
3405 | ||
5a7e7a0b SH |
3406 | bdrv_set_aio_context(target_bs, aio_context); |
3407 | ||
09158f00 BC |
3408 | /* pass the node name to replace to mirror start since it's loose coupling |
3409 | * and will allow to check whether the node still exist at mirror completion | |
3410 | */ | |
3411 | mirror_start(bs, target_bs, | |
3412 | has_replaces ? replaces : NULL, | |
3413 | speed, granularity, buf_size, sync, | |
eee13dfe | 3414 | on_source_error, on_target_error, |
0fc9f8ea | 3415 | unmap, |
b952b558 | 3416 | block_job_cb, bs, &local_err); |
d9b902db | 3417 | if (local_err != NULL) { |
4f6fd349 | 3418 | bdrv_unref(target_bs); |
d9b902db | 3419 | error_propagate(errp, local_err); |
5a7e7a0b | 3420 | goto out; |
d9b902db | 3421 | } |
5a7e7a0b SH |
3422 | |
3423 | out: | |
3424 | aio_context_release(aio_context); | |
d9b902db PB |
3425 | } |
3426 | ||
3d948cdf | 3427 | /* Get the block job for a given device name and acquire its AioContext */ |
24d6bffe MA |
3428 | static BlockJob *find_block_job(const char *device, AioContext **aio_context, |
3429 | Error **errp) | |
2d47c6e9 | 3430 | { |
a0e8544c | 3431 | BlockBackend *blk; |
2d47c6e9 SH |
3432 | BlockDriverState *bs; |
3433 | ||
5433c24f HR |
3434 | *aio_context = NULL; |
3435 | ||
a0e8544c FZ |
3436 | blk = blk_by_name(device); |
3437 | if (!blk) { | |
3d948cdf SH |
3438 | goto notfound; |
3439 | } | |
3440 | ||
5433c24f | 3441 | *aio_context = blk_get_aio_context(blk); |
3d948cdf SH |
3442 | aio_context_acquire(*aio_context); |
3443 | ||
5433c24f HR |
3444 | if (!blk_is_available(blk)) { |
3445 | goto notfound; | |
3446 | } | |
3447 | bs = blk_bs(blk); | |
3448 | ||
3d948cdf | 3449 | if (!bs->job) { |
3d948cdf | 3450 | goto notfound; |
2d47c6e9 | 3451 | } |
3d948cdf | 3452 | |
2d47c6e9 | 3453 | return bs->job; |
3d948cdf SH |
3454 | |
3455 | notfound: | |
2e3a0266 MA |
3456 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, |
3457 | "No active block job on device '%s'", device); | |
5433c24f HR |
3458 | if (*aio_context) { |
3459 | aio_context_release(*aio_context); | |
3460 | *aio_context = NULL; | |
3461 | } | |
3d948cdf | 3462 | return NULL; |
2d47c6e9 SH |
3463 | } |
3464 | ||
882ec7ce | 3465 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 3466 | { |
3d948cdf | 3467 | AioContext *aio_context; |
24d6bffe | 3468 | BlockJob *job = find_block_job(device, &aio_context, errp); |
2d47c6e9 SH |
3469 | |
3470 | if (!job) { | |
2d47c6e9 SH |
3471 | return; |
3472 | } | |
3473 | ||
882ec7ce | 3474 | block_job_set_speed(job, speed, errp); |
3d948cdf | 3475 | aio_context_release(aio_context); |
2d47c6e9 | 3476 | } |
370521a1 | 3477 | |
6e37fb81 PB |
3478 | void qmp_block_job_cancel(const char *device, |
3479 | bool has_force, bool force, Error **errp) | |
370521a1 | 3480 | { |
3d948cdf | 3481 | AioContext *aio_context; |
24d6bffe | 3482 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3483 | |
370521a1 | 3484 | if (!job) { |
370521a1 SH |
3485 | return; |
3486 | } | |
3d948cdf SH |
3487 | |
3488 | if (!has_force) { | |
3489 | force = false; | |
3490 | } | |
3491 | ||
751ebd76 | 3492 | if (job->user_paused && !force) { |
f231b88d CR |
3493 | error_setg(errp, "The block job for device '%s' is currently paused", |
3494 | device); | |
3d948cdf | 3495 | goto out; |
8acc72a4 | 3496 | } |
370521a1 SH |
3497 | |
3498 | trace_qmp_block_job_cancel(job); | |
3499 | block_job_cancel(job); | |
3d948cdf SH |
3500 | out: |
3501 | aio_context_release(aio_context); | |
370521a1 | 3502 | } |
fb5458cd | 3503 | |
6e37fb81 PB |
3504 | void qmp_block_job_pause(const char *device, Error **errp) |
3505 | { | |
3d948cdf | 3506 | AioContext *aio_context; |
24d6bffe | 3507 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3508 | |
751ebd76 | 3509 | if (!job || job->user_paused) { |
6e37fb81 PB |
3510 | return; |
3511 | } | |
3512 | ||
751ebd76 | 3513 | job->user_paused = true; |
6e37fb81 PB |
3514 | trace_qmp_block_job_pause(job); |
3515 | block_job_pause(job); | |
3d948cdf | 3516 | aio_context_release(aio_context); |
6e37fb81 PB |
3517 | } |
3518 | ||
3519 | void qmp_block_job_resume(const char *device, Error **errp) | |
3520 | { | |
3d948cdf | 3521 | AioContext *aio_context; |
24d6bffe | 3522 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3523 | |
751ebd76 | 3524 | if (!job || !job->user_paused) { |
6e37fb81 PB |
3525 | return; |
3526 | } | |
3527 | ||
751ebd76 | 3528 | job->user_paused = false; |
6e37fb81 PB |
3529 | trace_qmp_block_job_resume(job); |
3530 | block_job_resume(job); | |
3d948cdf | 3531 | aio_context_release(aio_context); |
6e37fb81 PB |
3532 | } |
3533 | ||
aeae883b PB |
3534 | void qmp_block_job_complete(const char *device, Error **errp) |
3535 | { | |
3d948cdf | 3536 | AioContext *aio_context; |
24d6bffe | 3537 | BlockJob *job = find_block_job(device, &aio_context, errp); |
aeae883b PB |
3538 | |
3539 | if (!job) { | |
aeae883b PB |
3540 | return; |
3541 | } | |
3542 | ||
3543 | trace_qmp_block_job_complete(job); | |
3544 | block_job_complete(job, errp); | |
3d948cdf | 3545 | aio_context_release(aio_context); |
aeae883b PB |
3546 | } |
3547 | ||
fa40e656 JC |
3548 | void qmp_change_backing_file(const char *device, |
3549 | const char *image_node_name, | |
3550 | const char *backing_file, | |
3551 | Error **errp) | |
3552 | { | |
a0e8544c | 3553 | BlockBackend *blk; |
fa40e656 | 3554 | BlockDriverState *bs = NULL; |
729962f6 | 3555 | AioContext *aio_context; |
fa40e656 JC |
3556 | BlockDriverState *image_bs = NULL; |
3557 | Error *local_err = NULL; | |
3558 | bool ro; | |
3559 | int open_flags; | |
3560 | int ret; | |
3561 | ||
a0e8544c FZ |
3562 | blk = blk_by_name(device); |
3563 | if (!blk) { | |
75158ebb MA |
3564 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
3565 | "Device '%s' not found", device); | |
fa40e656 JC |
3566 | return; |
3567 | } | |
3568 | ||
5433c24f | 3569 | aio_context = blk_get_aio_context(blk); |
729962f6 SH |
3570 | aio_context_acquire(aio_context); |
3571 | ||
5433c24f HR |
3572 | if (!blk_is_available(blk)) { |
3573 | error_setg(errp, "Device '%s' has no medium", device); | |
3574 | goto out; | |
3575 | } | |
3576 | bs = blk_bs(blk); | |
3577 | ||
fa40e656 JC |
3578 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
3579 | if (local_err) { | |
3580 | error_propagate(errp, local_err); | |
729962f6 | 3581 | goto out; |
fa40e656 JC |
3582 | } |
3583 | ||
3584 | if (!image_bs) { | |
3585 | error_setg(errp, "image file not found"); | |
729962f6 | 3586 | goto out; |
fa40e656 JC |
3587 | } |
3588 | ||
3589 | if (bdrv_find_base(image_bs) == image_bs) { | |
3590 | error_setg(errp, "not allowing backing file change on an image " | |
3591 | "without a backing file"); | |
729962f6 | 3592 | goto out; |
fa40e656 JC |
3593 | } |
3594 | ||
3595 | /* even though we are not necessarily operating on bs, we need it to | |
3596 | * determine if block ops are currently prohibited on the chain */ | |
3597 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 3598 | goto out; |
fa40e656 JC |
3599 | } |
3600 | ||
3601 | /* final sanity check */ | |
3602 | if (!bdrv_chain_contains(bs, image_bs)) { | |
3603 | error_setg(errp, "'%s' and image file are not in the same chain", | |
3604 | device); | |
729962f6 | 3605 | goto out; |
fa40e656 JC |
3606 | } |
3607 | ||
3608 | /* if not r/w, reopen to make r/w */ | |
3609 | open_flags = image_bs->open_flags; | |
3610 | ro = bdrv_is_read_only(image_bs); | |
3611 | ||
3612 | if (ro) { | |
3613 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
3614 | if (local_err) { | |
3615 | error_propagate(errp, local_err); | |
729962f6 | 3616 | goto out; |
fa40e656 JC |
3617 | } |
3618 | } | |
3619 | ||
3620 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
3621 | image_bs->drv ? image_bs->drv->format_name : ""); | |
3622 | ||
3623 | if (ret < 0) { | |
3624 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
3625 | backing_file); | |
3626 | /* don't exit here, so we can try to restore open flags if | |
3627 | * appropriate */ | |
3628 | } | |
3629 | ||
3630 | if (ro) { | |
3631 | bdrv_reopen(image_bs, open_flags, &local_err); | |
3632 | if (local_err) { | |
3633 | error_propagate(errp, local_err); /* will preserve prior errp */ | |
3634 | } | |
3635 | } | |
729962f6 SH |
3636 | |
3637 | out: | |
3638 | aio_context_release(aio_context); | |
fa40e656 JC |
3639 | } |
3640 | ||
d26c9a15 KW |
3641 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
3642 | { | |
3643 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
be4b67bc HR |
3644 | BlockDriverState *bs; |
3645 | BlockBackend *blk = NULL; | |
d26c9a15 KW |
3646 | QObject *obj; |
3647 | QDict *qdict; | |
d26c9a15 KW |
3648 | Error *local_err = NULL; |
3649 | ||
60e19e06 | 3650 | /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with |
d26c9a15 | 3651 | * cache.direct=false instead of silently switching to aio=threads, except |
60e19e06 | 3652 | * when called from drive_new(). |
d26c9a15 KW |
3653 | * |
3654 | * For now, simply forbidding the combination for all drivers will do. */ | |
3655 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { | |
c6e0bd9b KW |
3656 | bool direct = options->has_cache && |
3657 | options->cache->has_direct && | |
3658 | options->cache->direct; | |
3659 | if (!direct) { | |
d26c9a15 KW |
3660 | error_setg(errp, "aio=native requires cache.direct=true"); |
3661 | goto fail; | |
3662 | } | |
3663 | } | |
3664 | ||
3665 | visit_type_BlockdevOptions(qmp_output_get_visitor(ov), | |
3666 | &options, NULL, &local_err); | |
84d18f06 | 3667 | if (local_err) { |
d26c9a15 KW |
3668 | error_propagate(errp, local_err); |
3669 | goto fail; | |
3670 | } | |
3671 | ||
3672 | obj = qmp_output_get_qobject(ov); | |
3673 | qdict = qobject_to_qdict(obj); | |
3674 | ||
3675 | qdict_flatten(qdict); | |
3676 | ||
be4b67bc HR |
3677 | if (options->has_id) { |
3678 | blk = blockdev_init(NULL, qdict, &local_err); | |
3679 | if (local_err) { | |
3680 | error_propagate(errp, local_err); | |
3681 | goto fail; | |
3682 | } | |
3683 | ||
3684 | bs = blk_bs(blk); | |
3685 | } else { | |
be4b67bc HR |
3686 | if (!qdict_get_try_str(qdict, "node-name")) { |
3687 | error_setg(errp, "'id' and/or 'node-name' need to be specified for " | |
3688 | "the root node"); | |
3689 | goto fail; | |
3690 | } | |
3691 | ||
bd745e23 HR |
3692 | bs = bds_tree_init(qdict, errp); |
3693 | if (!bs) { | |
be4b67bc HR |
3694 | goto fail; |
3695 | } | |
d26c9a15 KW |
3696 | } |
3697 | ||
be4b67bc HR |
3698 | if (bs && bdrv_key_required(bs)) { |
3699 | if (blk) { | |
3700 | blk_unref(blk); | |
3701 | } else { | |
3702 | bdrv_unref(bs); | |
3703 | } | |
8ae8e904 KW |
3704 | error_setg(errp, "blockdev-add doesn't support encrypted devices"); |
3705 | goto fail; | |
3706 | } | |
3707 | ||
d26c9a15 KW |
3708 | fail: |
3709 | qmp_output_visitor_cleanup(ov); | |
3710 | } | |
3711 | ||
81b936ae AG |
3712 | void qmp_x_blockdev_del(bool has_id, const char *id, |
3713 | bool has_node_name, const char *node_name, Error **errp) | |
3714 | { | |
3715 | AioContext *aio_context; | |
3716 | BlockBackend *blk; | |
3717 | BlockDriverState *bs; | |
3718 | ||
3719 | if (has_id && has_node_name) { | |
3720 | error_setg(errp, "Only one of id and node-name must be specified"); | |
3721 | return; | |
3722 | } else if (!has_id && !has_node_name) { | |
3723 | error_setg(errp, "No block device specified"); | |
3724 | return; | |
3725 | } | |
3726 | ||
3727 | if (has_id) { | |
3728 | blk = blk_by_name(id); | |
3729 | if (!blk) { | |
3730 | error_setg(errp, "Cannot find block backend %s", id); | |
3731 | return; | |
3732 | } | |
3733 | if (blk_get_refcnt(blk) > 1) { | |
3734 | error_setg(errp, "Block backend %s is in use", id); | |
3735 | return; | |
3736 | } | |
3737 | bs = blk_bs(blk); | |
3738 | aio_context = blk_get_aio_context(blk); | |
3739 | } else { | |
3740 | bs = bdrv_find_node(node_name); | |
3741 | if (!bs) { | |
3742 | error_setg(errp, "Cannot find node %s", node_name); | |
3743 | return; | |
3744 | } | |
3745 | blk = bs->blk; | |
3746 | if (blk) { | |
3747 | error_setg(errp, "Node %s is in use by %s", | |
3748 | node_name, blk_name(blk)); | |
3749 | return; | |
3750 | } | |
3751 | aio_context = bdrv_get_aio_context(bs); | |
3752 | } | |
3753 | ||
3754 | aio_context_acquire(aio_context); | |
3755 | ||
3756 | if (bs) { | |
3757 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { | |
3758 | goto out; | |
3759 | } | |
3760 | ||
3761 | if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { | |
3762 | error_setg(errp, "Block device %s is in use", | |
3763 | bdrv_get_device_or_node_name(bs)); | |
3764 | goto out; | |
3765 | } | |
3766 | } | |
3767 | ||
3768 | if (blk) { | |
3769 | blk_unref(blk); | |
3770 | } else { | |
3771 | bdrv_unref(bs); | |
3772 | } | |
3773 | ||
3774 | out: | |
3775 | aio_context_release(aio_context); | |
3776 | } | |
3777 | ||
fea68bb6 | 3778 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 3779 | { |
fea68bb6 MA |
3780 | BlockJobInfoList *head = NULL, **p_next = &head; |
3781 | BlockDriverState *bs; | |
fb5458cd | 3782 | |
fea68bb6 | 3783 | for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { |
69691e72 SH |
3784 | AioContext *aio_context = bdrv_get_aio_context(bs); |
3785 | ||
3786 | aio_context_acquire(aio_context); | |
3787 | ||
fea68bb6 MA |
3788 | if (bs->job) { |
3789 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); | |
3790 | elem->value = block_job_query(bs->job); | |
3791 | *p_next = elem; | |
3792 | p_next = &elem->next; | |
3793 | } | |
69691e72 SH |
3794 | |
3795 | aio_context_release(aio_context); | |
fb5458cd | 3796 | } |
fb5458cd | 3797 | |
fea68bb6 | 3798 | return head; |
fb5458cd | 3799 | } |
4d454574 | 3800 | |
0006383e | 3801 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 3802 | .name = "drive", |
0006383e | 3803 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
3804 | .desc = { |
3805 | { | |
4d454574 PB |
3806 | .name = "snapshot", |
3807 | .type = QEMU_OPT_BOOL, | |
3808 | .help = "enable/disable snapshot mode", | |
a9384aff PB |
3809 | },{ |
3810 | .name = "discard", | |
3811 | .type = QEMU_OPT_STRING, | |
3812 | .help = "discard operation (ignore/off, unmap/on)", | |
4d454574 | 3813 | },{ |
54861b92 | 3814 | .name = BDRV_OPT_CACHE_WB, |
29c4e2b5 KW |
3815 | .type = QEMU_OPT_BOOL, |
3816 | .help = "enables writeback mode for any caches", | |
3817 | },{ | |
54861b92 | 3818 | .name = BDRV_OPT_CACHE_DIRECT, |
29c4e2b5 KW |
3819 | .type = QEMU_OPT_BOOL, |
3820 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
3821 | },{ | |
54861b92 | 3822 | .name = BDRV_OPT_CACHE_NO_FLUSH, |
29c4e2b5 KW |
3823 | .type = QEMU_OPT_BOOL, |
3824 | .help = "ignore any flush requests for the device", | |
4d454574 PB |
3825 | },{ |
3826 | .name = "aio", | |
3827 | .type = QEMU_OPT_STRING, | |
3828 | .help = "host AIO implementation (threads, native)", | |
3829 | },{ | |
3830 | .name = "format", | |
3831 | .type = QEMU_OPT_STRING, | |
3832 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
3833 | },{ |
3834 | .name = "rerror", | |
3835 | .type = QEMU_OPT_STRING, | |
3836 | .help = "read error action", | |
3837 | },{ | |
3838 | .name = "werror", | |
3839 | .type = QEMU_OPT_STRING, | |
3840 | .help = "write error action", | |
4d454574 | 3841 | },{ |
0f227a94 | 3842 | .name = "read-only", |
4d454574 PB |
3843 | .type = QEMU_OPT_BOOL, |
3844 | .help = "open drive file as read-only", | |
3845 | },{ | |
57975222 | 3846 | .name = "throttling.iops-total", |
4d454574 PB |
3847 | .type = QEMU_OPT_NUMBER, |
3848 | .help = "limit total I/O operations per second", | |
3849 | },{ | |
57975222 | 3850 | .name = "throttling.iops-read", |
4d454574 PB |
3851 | .type = QEMU_OPT_NUMBER, |
3852 | .help = "limit read operations per second", | |
3853 | },{ | |
57975222 | 3854 | .name = "throttling.iops-write", |
4d454574 PB |
3855 | .type = QEMU_OPT_NUMBER, |
3856 | .help = "limit write operations per second", | |
3857 | },{ | |
57975222 | 3858 | .name = "throttling.bps-total", |
4d454574 PB |
3859 | .type = QEMU_OPT_NUMBER, |
3860 | .help = "limit total bytes per second", | |
3861 | },{ | |
57975222 | 3862 | .name = "throttling.bps-read", |
4d454574 PB |
3863 | .type = QEMU_OPT_NUMBER, |
3864 | .help = "limit read bytes per second", | |
3865 | },{ | |
57975222 | 3866 | .name = "throttling.bps-write", |
4d454574 PB |
3867 | .type = QEMU_OPT_NUMBER, |
3868 | .help = "limit write bytes per second", | |
3e9fab69 BC |
3869 | },{ |
3870 | .name = "throttling.iops-total-max", | |
3871 | .type = QEMU_OPT_NUMBER, | |
3872 | .help = "I/O operations burst", | |
3873 | },{ | |
3874 | .name = "throttling.iops-read-max", | |
3875 | .type = QEMU_OPT_NUMBER, | |
3876 | .help = "I/O operations read burst", | |
3877 | },{ | |
3878 | .name = "throttling.iops-write-max", | |
3879 | .type = QEMU_OPT_NUMBER, | |
3880 | .help = "I/O operations write burst", | |
3881 | },{ | |
3882 | .name = "throttling.bps-total-max", | |
3883 | .type = QEMU_OPT_NUMBER, | |
3884 | .help = "total bytes burst", | |
3885 | },{ | |
3886 | .name = "throttling.bps-read-max", | |
3887 | .type = QEMU_OPT_NUMBER, | |
3888 | .help = "total bytes read burst", | |
3889 | },{ | |
3890 | .name = "throttling.bps-write-max", | |
3891 | .type = QEMU_OPT_NUMBER, | |
3892 | .help = "total bytes write burst", | |
2024c1df BC |
3893 | },{ |
3894 | .name = "throttling.iops-size", | |
3895 | .type = QEMU_OPT_NUMBER, | |
3896 | .help = "when limiting by iops max size of an I/O in bytes", | |
76f4afb4 AG |
3897 | },{ |
3898 | .name = "throttling.group", | |
3899 | .type = QEMU_OPT_STRING, | |
3900 | .help = "name of the block throttling group", | |
4d454574 PB |
3901 | },{ |
3902 | .name = "copy-on-read", | |
3903 | .type = QEMU_OPT_BOOL, | |
3904 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
3905 | },{ |
3906 | .name = "detect-zeroes", | |
3907 | .type = QEMU_OPT_STRING, | |
3908 | .help = "try to optimize zero writes (off, on, unmap)", | |
362e9299 AG |
3909 | },{ |
3910 | .name = "stats-account-invalid", | |
3911 | .type = QEMU_OPT_BOOL, | |
3912 | .help = "whether to account for invalid I/O operations " | |
3913 | "in the statistics", | |
3914 | },{ | |
3915 | .name = "stats-account-failed", | |
3916 | .type = QEMU_OPT_BOOL, | |
3917 | .help = "whether to account for failed I/O operations " | |
3918 | "in the statistics", | |
4d454574 PB |
3919 | }, |
3920 | { /* end of list */ } | |
3921 | }, | |
3922 | }; | |
0006383e | 3923 | |
bd745e23 HR |
3924 | static QemuOptsList qemu_root_bds_opts = { |
3925 | .name = "root-bds", | |
3926 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), | |
3927 | .desc = { | |
3928 | { | |
3929 | .name = "discard", | |
3930 | .type = QEMU_OPT_STRING, | |
3931 | .help = "discard operation (ignore/off, unmap/on)", | |
3932 | },{ | |
3933 | .name = "cache.writeback", | |
3934 | .type = QEMU_OPT_BOOL, | |
3935 | .help = "enables writeback mode for any caches", | |
3936 | },{ | |
3937 | .name = "cache.direct", | |
3938 | .type = QEMU_OPT_BOOL, | |
3939 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
3940 | },{ | |
3941 | .name = "cache.no-flush", | |
3942 | .type = QEMU_OPT_BOOL, | |
3943 | .help = "ignore any flush requests for the device", | |
3944 | },{ | |
3945 | .name = "aio", | |
3946 | .type = QEMU_OPT_STRING, | |
3947 | .help = "host AIO implementation (threads, native)", | |
3948 | },{ | |
3949 | .name = "read-only", | |
3950 | .type = QEMU_OPT_BOOL, | |
3951 | .help = "open drive file as read-only", | |
3952 | },{ | |
3953 | .name = "copy-on-read", | |
3954 | .type = QEMU_OPT_BOOL, | |
3955 | .help = "copy read data from backing file into image file", | |
3956 | },{ | |
3957 | .name = "detect-zeroes", | |
3958 | .type = QEMU_OPT_STRING, | |
3959 | .help = "try to optimize zero writes (off, on, unmap)", | |
3960 | }, | |
3961 | { /* end of list */ } | |
3962 | }, | |
3963 | }; | |
3964 | ||
0006383e KW |
3965 | QemuOptsList qemu_drive_opts = { |
3966 | .name = "drive", | |
3967 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
3968 | .desc = { | |
492fdc6f KW |
3969 | /* |
3970 | * no elements => accept any params | |
3971 | * validation will happen later | |
3972 | */ | |
0006383e KW |
3973 | { /* end of list */ } |
3974 | }, | |
3975 | }; |