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