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