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