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