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