]>
Commit | Line | Data |
---|---|---|
666daa68 MA |
1 | /* |
2 | * QEMU host block devices | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
7 | * later. See the COPYING file in the top-level directory. | |
3618a094 MA |
8 | * |
9 | * This file incorporates work covered by the following copyright and | |
10 | * permission notice: | |
11 | * | |
12 | * Copyright (c) 2003-2008 Fabrice Bellard | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
666daa68 MA |
31 | */ |
32 | ||
26f54e9a | 33 | #include "sysemu/block-backend.h" |
9c17d615 | 34 | #include "sysemu/blockdev.h" |
0d09e41a | 35 | #include "hw/block/block.h" |
737e150e | 36 | #include "block/blockjob.h" |
83c9089e | 37 | #include "monitor/monitor.h" |
1de7afc9 PB |
38 | #include "qemu/option.h" |
39 | #include "qemu/config-file.h" | |
7b1b5d19 | 40 | #include "qapi/qmp/types.h" |
d26c9a15 KW |
41 | #include "qapi-visit.h" |
42 | #include "qapi/qmp-output-visitor.h" | |
9e7dac7c | 43 | #include "qapi/util.h" |
9c17d615 | 44 | #include "sysemu/sysemu.h" |
737e150e | 45 | #include "block/block_int.h" |
a4dea8a9 | 46 | #include "qmp-commands.h" |
12bd451f | 47 | #include "trace.h" |
9c17d615 | 48 | #include "sysemu/arch_init.h" |
666daa68 | 49 | |
1960966d MA |
50 | static const char *const if_name[IF_COUNT] = { |
51 | [IF_NONE] = "none", | |
52 | [IF_IDE] = "ide", | |
53 | [IF_SCSI] = "scsi", | |
54 | [IF_FLOPPY] = "floppy", | |
55 | [IF_PFLASH] = "pflash", | |
56 | [IF_MTD] = "mtd", | |
57 | [IF_SD] = "sd", | |
58 | [IF_VIRTIO] = "virtio", | |
59 | [IF_XEN] = "xen", | |
60 | }; | |
61 | ||
21dff8cf | 62 | static int if_max_devs[IF_COUNT] = { |
27d6bf40 MA |
63 | /* |
64 | * Do not change these numbers! They govern how drive option | |
65 | * index maps to unit and bus. That mapping is ABI. | |
66 | * | |
67 | * All controllers used to imlement if=T drives need to support | |
68 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. | |
69 | * Otherwise, some index values map to "impossible" bus, unit | |
70 | * values. | |
71 | * | |
72 | * For instance, if you change [IF_SCSI] to 255, -drive | |
73 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
74 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
75 | * the drive can't be set up. Regression. | |
76 | */ | |
77 | [IF_IDE] = 2, | |
78 | [IF_SCSI] = 7, | |
1960966d MA |
79 | }; |
80 | ||
21dff8cf JS |
81 | /** |
82 | * Boards may call this to offer board-by-board overrides | |
83 | * of the default, global values. | |
84 | */ | |
85 | void override_max_devs(BlockInterfaceType type, int max_devs) | |
86 | { | |
18e46a03 | 87 | BlockBackend *blk; |
21dff8cf JS |
88 | DriveInfo *dinfo; |
89 | ||
90 | if (max_devs <= 0) { | |
91 | return; | |
92 | } | |
93 | ||
18e46a03 MA |
94 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
95 | dinfo = blk_legacy_dinfo(blk); | |
21dff8cf JS |
96 | if (dinfo->type == type) { |
97 | fprintf(stderr, "Cannot override units-per-bus property of" | |
98 | " the %s interface, because a drive of that type has" | |
99 | " already been added.\n", if_name[type]); | |
100 | g_assert_not_reached(); | |
101 | } | |
102 | } | |
103 | ||
104 | if_max_devs[type] = max_devs; | |
105 | } | |
106 | ||
14bafc54 MA |
107 | /* |
108 | * We automatically delete the drive when a device using it gets | |
109 | * unplugged. Questionable feature, but we can't just drop it. | |
110 | * Device models call blockdev_mark_auto_del() to schedule the | |
111 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
112 | * when deletion is actually safe. | |
113 | */ | |
4be74634 | 114 | void blockdev_mark_auto_del(BlockBackend *blk) |
14bafc54 | 115 | { |
18e46a03 | 116 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
4be74634 | 117 | BlockDriverState *bs = blk_bs(blk); |
91fddb0d | 118 | AioContext *aio_context; |
14bafc54 | 119 | |
26f8b3a8 | 120 | if (!dinfo) { |
2d246f01 KW |
121 | return; |
122 | } | |
123 | ||
91fddb0d SH |
124 | aio_context = bdrv_get_aio_context(bs); |
125 | aio_context_acquire(aio_context); | |
126 | ||
12bde0ee PB |
127 | if (bs->job) { |
128 | block_job_cancel(bs->job); | |
129 | } | |
91fddb0d SH |
130 | |
131 | aio_context_release(aio_context); | |
132 | ||
26f8b3a8 | 133 | dinfo->auto_del = 1; |
14bafc54 MA |
134 | } |
135 | ||
4be74634 | 136 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 137 | { |
18e46a03 | 138 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 139 | |
0fc0f1fa | 140 | if (dinfo && dinfo->auto_del) { |
b9fe8a7a | 141 | blk_unref(blk); |
14bafc54 MA |
142 | } |
143 | } | |
144 | ||
d8f94e1b JS |
145 | /** |
146 | * Returns the current mapping of how many units per bus | |
147 | * a particular interface can support. | |
148 | * | |
149 | * A positive integer indicates n units per bus. | |
150 | * 0 implies the mapping has not been established. | |
151 | * -1 indicates an invalid BlockInterfaceType was given. | |
152 | */ | |
153 | int drive_get_max_devs(BlockInterfaceType type) | |
154 | { | |
155 | if (type >= IF_IDE && type < IF_COUNT) { | |
156 | return if_max_devs[type]; | |
157 | } | |
158 | ||
159 | return -1; | |
160 | } | |
161 | ||
505a7fb1 MA |
162 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
163 | { | |
164 | int max_devs = if_max_devs[type]; | |
165 | return max_devs ? index / max_devs : 0; | |
166 | } | |
167 | ||
168 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
169 | { | |
170 | int max_devs = if_max_devs[type]; | |
171 | return max_devs ? index % max_devs : index; | |
172 | } | |
173 | ||
2292ddae MA |
174 | QemuOpts *drive_def(const char *optstr) |
175 | { | |
176 | return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); | |
177 | } | |
178 | ||
179 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 180 | const char *optstr) |
666daa68 | 181 | { |
666daa68 | 182 | QemuOpts *opts; |
2292ddae | 183 | char buf[32]; |
666daa68 | 184 | |
2292ddae | 185 | opts = drive_def(optstr); |
666daa68 MA |
186 | if (!opts) { |
187 | return NULL; | |
188 | } | |
2292ddae MA |
189 | if (type != IF_DEFAULT) { |
190 | qemu_opt_set(opts, "if", if_name[type]); | |
191 | } | |
192 | if (index >= 0) { | |
193 | snprintf(buf, sizeof(buf), "%d", index); | |
194 | qemu_opt_set(opts, "index", buf); | |
195 | } | |
666daa68 MA |
196 | if (file) |
197 | qemu_opt_set(opts, "file", file); | |
198 | return opts; | |
199 | } | |
200 | ||
201 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
202 | { | |
18e46a03 | 203 | BlockBackend *blk; |
666daa68 MA |
204 | DriveInfo *dinfo; |
205 | ||
18e46a03 MA |
206 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
207 | dinfo = blk_legacy_dinfo(blk); | |
208 | if (dinfo && dinfo->type == type | |
209 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 210 | return dinfo; |
18e46a03 | 211 | } |
666daa68 MA |
212 | } |
213 | ||
214 | return NULL; | |
215 | } | |
216 | ||
a66c9dc7 JS |
217 | bool drive_check_orphaned(void) |
218 | { | |
18e46a03 | 219 | BlockBackend *blk; |
a66c9dc7 JS |
220 | DriveInfo *dinfo; |
221 | bool rs = false; | |
222 | ||
18e46a03 MA |
223 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
224 | dinfo = blk_legacy_dinfo(blk); | |
a66c9dc7 JS |
225 | /* If dinfo->bdrv->dev is NULL, it has no device attached. */ |
226 | /* Unless this is a default drive, this may be an oversight. */ | |
a7f53e26 | 227 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
a66c9dc7 JS |
228 | dinfo->type != IF_NONE) { |
229 | fprintf(stderr, "Warning: Orphaned drive without device: " | |
230 | "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", | |
d3aeb1b7 | 231 | blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type], |
a66c9dc7 JS |
232 | dinfo->bus, dinfo->unit); |
233 | rs = true; | |
234 | } | |
235 | } | |
236 | ||
237 | return rs; | |
238 | } | |
239 | ||
f1bd51ac MA |
240 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
241 | { | |
242 | return drive_get(type, | |
243 | drive_index_to_bus_id(type, index), | |
244 | drive_index_to_unit_id(type, index)); | |
245 | } | |
246 | ||
666daa68 MA |
247 | int drive_get_max_bus(BlockInterfaceType type) |
248 | { | |
249 | int max_bus; | |
18e46a03 | 250 | BlockBackend *blk; |
666daa68 MA |
251 | DriveInfo *dinfo; |
252 | ||
253 | max_bus = -1; | |
18e46a03 MA |
254 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
255 | dinfo = blk_legacy_dinfo(blk); | |
256 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 257 | max_bus = dinfo->bus; |
18e46a03 | 258 | } |
666daa68 MA |
259 | } |
260 | return max_bus; | |
261 | } | |
262 | ||
13839974 MA |
263 | /* Get a block device. This should only be used for single-drive devices |
264 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
265 | appropriate bus. */ | |
266 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
267 | { | |
268 | static int next_block_unit[IF_COUNT]; | |
269 | ||
270 | return drive_get(type, 0, next_block_unit[type]++); | |
271 | } | |
272 | ||
666daa68 MA |
273 | static void bdrv_format_print(void *opaque, const char *name) |
274 | { | |
807105a7 | 275 | error_printf(" %s", name); |
666daa68 MA |
276 | } |
277 | ||
aa398a5c SH |
278 | typedef struct { |
279 | QEMUBH *bh; | |
fa510ebf FZ |
280 | BlockDriverState *bs; |
281 | } BDRVPutRefBH; | |
aa398a5c | 282 | |
fa510ebf | 283 | static void bdrv_put_ref_bh(void *opaque) |
aa398a5c | 284 | { |
fa510ebf | 285 | BDRVPutRefBH *s = opaque; |
aa398a5c | 286 | |
fa510ebf | 287 | bdrv_unref(s->bs); |
aa398a5c SH |
288 | qemu_bh_delete(s->bh); |
289 | g_free(s); | |
290 | } | |
291 | ||
292 | /* | |
fa510ebf | 293 | * Release a BDS reference in a BH |
aa398a5c | 294 | * |
fa510ebf FZ |
295 | * It is not safe to use bdrv_unref() from a callback function when the callers |
296 | * still need the BlockDriverState. In such cases we schedule a BH to release | |
297 | * the reference. | |
aa398a5c | 298 | */ |
fa510ebf | 299 | static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) |
aa398a5c | 300 | { |
fa510ebf | 301 | BDRVPutRefBH *s; |
aa398a5c | 302 | |
fa510ebf FZ |
303 | s = g_new(BDRVPutRefBH, 1); |
304 | s->bh = qemu_bh_new(bdrv_put_ref_bh, s); | |
305 | s->bs = bs; | |
aa398a5c SH |
306 | qemu_bh_schedule(s->bh); |
307 | } | |
308 | ||
b681072d | 309 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
310 | { |
311 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 312 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 313 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 314 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 315 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 316 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 317 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 318 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 319 | } else { |
b681072d KW |
320 | error_setg(errp, "'%s' invalid %s error action", |
321 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
322 | return -1; |
323 | } | |
324 | } | |
325 | ||
cc0681c4 | 326 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
0563e191 | 327 | { |
cc0681c4 BC |
328 | if (throttle_conflicting(cfg)) { |
329 | error_setg(errp, "bps/iops/max total values and read/write values" | |
330 | " cannot be used at the same time"); | |
0563e191 ZYW |
331 | return false; |
332 | } | |
333 | ||
cc0681c4 BC |
334 | if (!throttle_is_valid(cfg)) { |
335 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); | |
7d81c141 SH |
336 | return false; |
337 | } | |
338 | ||
0563e191 ZYW |
339 | return true; |
340 | } | |
341 | ||
33cb7dc8 KW |
342 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
343 | ||
f298d071 | 344 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
345 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
346 | Error **errp) | |
666daa68 MA |
347 | { |
348 | const char *buf; | |
666daa68 MA |
349 | int ro = 0; |
350 | int bdrv_flags = 0; | |
351 | int on_read_error, on_write_error; | |
26f54e9a | 352 | BlockBackend *blk; |
a0f1eab1 | 353 | BlockDriverState *bs; |
cc0681c4 | 354 | ThrottleConfig cfg; |
666daa68 | 355 | int snapshot = 0; |
fb0490f6 | 356 | bool copy_on_read; |
666daa68 | 357 | int ret; |
c546194f | 358 | Error *error = NULL; |
0006383e | 359 | QemuOpts *opts; |
0006383e | 360 | const char *id; |
74fe54f2 | 361 | bool has_driver_specific_opts; |
465bee1d | 362 | BlockdevDetectZeroesOptions detect_zeroes; |
6db5f5d6 | 363 | BlockDriver *drv = NULL; |
666daa68 | 364 | |
f298d071 KW |
365 | /* Check common options by copying from bs_opts to opts, all other options |
366 | * stay in bs_opts for processing by bdrv_open(). */ | |
367 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 368 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 369 | if (error) { |
b681072d | 370 | error_propagate(errp, error); |
6376f952 | 371 | goto err_no_opts; |
0006383e KW |
372 | } |
373 | ||
0006383e | 374 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 375 | if (error) { |
b681072d | 376 | error_propagate(errp, error); |
ec9c10d2 | 377 | goto early_err; |
0006383e KW |
378 | } |
379 | ||
380 | if (id) { | |
381 | qdict_del(bs_opts, "id"); | |
382 | } | |
383 | ||
74fe54f2 KW |
384 | has_driver_specific_opts = !!qdict_size(bs_opts); |
385 | ||
666daa68 | 386 | /* extract parameters */ |
666daa68 | 387 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
0f227a94 | 388 | ro = qemu_opt_get_bool(opts, "read-only", 0); |
fb0490f6 | 389 | copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); |
666daa68 | 390 | |
a9384aff PB |
391 | if ((buf = qemu_opt_get(opts, "discard")) != NULL) { |
392 | if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { | |
b681072d | 393 | error_setg(errp, "invalid discard option"); |
ec9c10d2 | 394 | goto early_err; |
a9384aff PB |
395 | } |
396 | } | |
397 | ||
29c4e2b5 KW |
398 | if (qemu_opt_get_bool(opts, "cache.writeback", true)) { |
399 | bdrv_flags |= BDRV_O_CACHE_WB; | |
400 | } | |
401 | if (qemu_opt_get_bool(opts, "cache.direct", false)) { | |
402 | bdrv_flags |= BDRV_O_NOCACHE; | |
403 | } | |
1df6fa4b | 404 | if (qemu_opt_get_bool(opts, "cache.no-flush", false)) { |
29c4e2b5 | 405 | bdrv_flags |= BDRV_O_NO_FLUSH; |
666daa68 MA |
406 | } |
407 | ||
408 | #ifdef CONFIG_LINUX_AIO | |
409 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
410 | if (!strcmp(buf, "native")) { | |
411 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
412 | } else if (!strcmp(buf, "threads")) { | |
413 | /* this is the default */ | |
414 | } else { | |
b681072d | 415 | error_setg(errp, "invalid aio option"); |
ec9c10d2 | 416 | goto early_err; |
666daa68 MA |
417 | } |
418 | } | |
419 | #endif | |
420 | ||
421 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
422 | if (is_help_option(buf)) { |
423 | error_printf("Supported formats:"); | |
424 | bdrv_iterate_format(bdrv_format_print, NULL); | |
425 | error_printf("\n"); | |
ec9c10d2 | 426 | goto early_err; |
666daa68 | 427 | } |
74fe54f2 | 428 | |
8f94a6e4 | 429 | drv = bdrv_find_format(buf); |
6db5f5d6 | 430 | if (!drv) { |
b681072d | 431 | error_setg(errp, "'%s' invalid format", buf); |
ec9c10d2 | 432 | goto early_err; |
6db5f5d6 | 433 | } |
666daa68 MA |
434 | } |
435 | ||
0563e191 | 436 | /* disk I/O throttling */ |
cc0681c4 BC |
437 | memset(&cfg, 0, sizeof(cfg)); |
438 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = | |
57975222 | 439 | qemu_opt_get_number(opts, "throttling.bps-total", 0); |
cc0681c4 | 440 | cfg.buckets[THROTTLE_BPS_READ].avg = |
57975222 | 441 | qemu_opt_get_number(opts, "throttling.bps-read", 0); |
cc0681c4 | 442 | cfg.buckets[THROTTLE_BPS_WRITE].avg = |
57975222 | 443 | qemu_opt_get_number(opts, "throttling.bps-write", 0); |
cc0681c4 | 444 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = |
57975222 | 445 | qemu_opt_get_number(opts, "throttling.iops-total", 0); |
cc0681c4 | 446 | cfg.buckets[THROTTLE_OPS_READ].avg = |
57975222 | 447 | qemu_opt_get_number(opts, "throttling.iops-read", 0); |
cc0681c4 | 448 | cfg.buckets[THROTTLE_OPS_WRITE].avg = |
57975222 | 449 | qemu_opt_get_number(opts, "throttling.iops-write", 0); |
0563e191 | 450 | |
3e9fab69 BC |
451 | cfg.buckets[THROTTLE_BPS_TOTAL].max = |
452 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
453 | cfg.buckets[THROTTLE_BPS_READ].max = | |
454 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
455 | cfg.buckets[THROTTLE_BPS_WRITE].max = | |
456 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
457 | cfg.buckets[THROTTLE_OPS_TOTAL].max = | |
458 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
459 | cfg.buckets[THROTTLE_OPS_READ].max = | |
460 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
461 | cfg.buckets[THROTTLE_OPS_WRITE].max = | |
462 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
cc0681c4 | 463 | |
2024c1df | 464 | cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); |
cc0681c4 BC |
465 | |
466 | if (!check_throttle_config(&cfg, &error)) { | |
b681072d | 467 | error_propagate(errp, error); |
ec9c10d2 | 468 | goto early_err; |
0563e191 ZYW |
469 | } |
470 | ||
92aa5c6d | 471 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 472 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 473 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 474 | if (error) { |
b681072d | 475 | error_propagate(errp, error); |
ec9c10d2 | 476 | goto early_err; |
666daa68 MA |
477 | } |
478 | } | |
479 | ||
92aa5c6d | 480 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 481 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 482 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 483 | if (error) { |
b681072d | 484 | error_propagate(errp, error); |
ec9c10d2 | 485 | goto early_err; |
666daa68 MA |
486 | } |
487 | } | |
488 | ||
465bee1d | 489 | detect_zeroes = |
9e7dac7c PL |
490 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, |
491 | qemu_opt_get(opts, "detect-zeroes"), | |
492 | BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, | |
493 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
494 | &error); | |
465bee1d PL |
495 | if (error) { |
496 | error_propagate(errp, error); | |
497 | goto early_err; | |
498 | } | |
499 | ||
500 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
501 | !(bdrv_flags & BDRV_O_UNMAP)) { | |
502 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
503 | "without setting discard operation to unmap"); | |
504 | goto early_err; | |
505 | } | |
506 | ||
326642bc | 507 | /* init */ |
7e7d56d9 | 508 | blk = blk_new_with_bs(qemu_opts_id(opts), errp); |
26f54e9a MA |
509 | if (!blk) { |
510 | goto early_err; | |
511 | } | |
7e7d56d9 | 512 | bs = blk_bs(blk); |
a0f1eab1 MA |
513 | bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; |
514 | bs->read_only = ro; | |
515 | bs->detect_zeroes = detect_zeroes; | |
666daa68 | 516 | |
a0f1eab1 | 517 | bdrv_set_on_error(bs, on_read_error, on_write_error); |
abd7f68d | 518 | |
0563e191 | 519 | /* disk I/O throttling */ |
cc0681c4 | 520 | if (throttle_enabled(&cfg)) { |
a0f1eab1 MA |
521 | bdrv_io_limits_enable(bs); |
522 | bdrv_set_io_limits(bs, &cfg); | |
cc0681c4 | 523 | } |
0563e191 | 524 | |
dd5b0d71 | 525 | if (!file || !*file) { |
74fe54f2 | 526 | if (has_driver_specific_opts) { |
c2ad1b0c KW |
527 | file = NULL; |
528 | } else { | |
ec9c10d2 SH |
529 | QDECREF(bs_opts); |
530 | qemu_opts_del(opts); | |
18e46a03 | 531 | return blk; |
c2ad1b0c | 532 | } |
666daa68 MA |
533 | } |
534 | if (snapshot) { | |
535 | /* always use cache=unsafe with snapshot */ | |
536 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
537 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
538 | } | |
539 | ||
fb0490f6 SH |
540 | if (copy_on_read) { |
541 | bdrv_flags |= BDRV_O_COPY_ON_READ; | |
542 | } | |
543 | ||
ed9d4205 BC |
544 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
545 | bdrv_flags |= BDRV_O_INCOMING; | |
546 | } | |
547 | ||
666daa68 MA |
548 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
549 | ||
74fe54f2 | 550 | QINCREF(bs_opts); |
a0f1eab1 | 551 | ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error); |
fa1d36df | 552 | assert(bs == blk_bs(blk)); |
0006383e | 553 | |
666daa68 | 554 | if (ret < 0) { |
b681072d | 555 | error_setg(errp, "could not open disk image %s: %s", |
d3aeb1b7 | 556 | file ?: blk_name(blk), error_get_pretty(error)); |
b681072d | 557 | error_free(error); |
a9ae2bff | 558 | goto err; |
666daa68 MA |
559 | } |
560 | ||
a0f1eab1 | 561 | if (bdrv_key_required(bs)) { |
666daa68 | 562 | autostart = 0; |
a0f1eab1 | 563 | } |
0006383e | 564 | |
74fe54f2 | 565 | QDECREF(bs_opts); |
0006383e KW |
566 | qemu_opts_del(opts); |
567 | ||
18e46a03 | 568 | return blk; |
a9ae2bff MA |
569 | |
570 | err: | |
26f54e9a | 571 | blk_unref(blk); |
ec9c10d2 | 572 | early_err: |
ec9c10d2 | 573 | qemu_opts_del(opts); |
6376f952 MA |
574 | err_no_opts: |
575 | QDECREF(bs_opts); | |
a9ae2bff | 576 | return NULL; |
666daa68 MA |
577 | } |
578 | ||
5abbf0ee KW |
579 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
580 | Error **errp) | |
57975222 KW |
581 | { |
582 | const char *value; | |
583 | ||
584 | value = qemu_opt_get(opts, from); | |
585 | if (value) { | |
5abbf0ee KW |
586 | if (qemu_opt_find(opts, to)) { |
587 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
588 | "same time", to, from); | |
589 | return; | |
590 | } | |
20d6cd47 JL |
591 | } |
592 | ||
593 | /* rename all items in opts */ | |
594 | while ((value = qemu_opt_get(opts, from))) { | |
57975222 KW |
595 | qemu_opt_set(opts, to, value); |
596 | qemu_opt_unset(opts, from); | |
597 | } | |
598 | } | |
599 | ||
33cb7dc8 KW |
600 | QemuOptsList qemu_legacy_drive_opts = { |
601 | .name = "drive", | |
602 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
603 | .desc = { | |
604 | { | |
87a899c5 KW |
605 | .name = "bus", |
606 | .type = QEMU_OPT_NUMBER, | |
607 | .help = "bus number", | |
608 | },{ | |
609 | .name = "unit", | |
610 | .type = QEMU_OPT_NUMBER, | |
611 | .help = "unit number (i.e. lun for scsi)", | |
612 | },{ | |
613 | .name = "index", | |
614 | .type = QEMU_OPT_NUMBER, | |
615 | .help = "index number", | |
616 | },{ | |
33cb7dc8 KW |
617 | .name = "media", |
618 | .type = QEMU_OPT_STRING, | |
619 | .help = "media type (disk, cdrom)", | |
593d464b KW |
620 | },{ |
621 | .name = "if", | |
622 | .type = QEMU_OPT_STRING, | |
623 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
b41a7338 KW |
624 | },{ |
625 | .name = "cyls", | |
626 | .type = QEMU_OPT_NUMBER, | |
627 | .help = "number of cylinders (ide disk geometry)", | |
628 | },{ | |
629 | .name = "heads", | |
630 | .type = QEMU_OPT_NUMBER, | |
631 | .help = "number of heads (ide disk geometry)", | |
632 | },{ | |
633 | .name = "secs", | |
634 | .type = QEMU_OPT_NUMBER, | |
635 | .help = "number of sectors (ide disk geometry)", | |
636 | },{ | |
637 | .name = "trans", | |
638 | .type = QEMU_OPT_STRING, | |
639 | .help = "chs translation (auto, lba, none)", | |
26929298 KW |
640 | },{ |
641 | .name = "boot", | |
642 | .type = QEMU_OPT_BOOL, | |
643 | .help = "(deprecated, ignored)", | |
394c7d4d KW |
644 | },{ |
645 | .name = "addr", | |
646 | .type = QEMU_OPT_STRING, | |
647 | .help = "pci address (virtio only)", | |
bcf83158 KW |
648 | },{ |
649 | .name = "serial", | |
650 | .type = QEMU_OPT_STRING, | |
651 | .help = "disk serial number", | |
d095b465 HR |
652 | },{ |
653 | .name = "file", | |
654 | .type = QEMU_OPT_STRING, | |
655 | .help = "file name", | |
33cb7dc8 | 656 | }, |
0ebd24e0 KW |
657 | |
658 | /* Options that are passed on, but have special semantics with -drive */ | |
659 | { | |
660 | .name = "read-only", | |
661 | .type = QEMU_OPT_BOOL, | |
662 | .help = "open drive file as read-only", | |
ee13ed1c KW |
663 | },{ |
664 | .name = "rerror", | |
665 | .type = QEMU_OPT_STRING, | |
666 | .help = "read error action", | |
667 | },{ | |
668 | .name = "werror", | |
669 | .type = QEMU_OPT_STRING, | |
670 | .help = "write error action", | |
0ebd24e0 KW |
671 | },{ |
672 | .name = "copy-on-read", | |
673 | .type = QEMU_OPT_BOOL, | |
674 | .help = "copy read data from backing file into image file", | |
675 | }, | |
676 | ||
33cb7dc8 KW |
677 | { /* end of list */ } |
678 | }, | |
679 | }; | |
680 | ||
60e19e06 | 681 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
57975222 | 682 | { |
29c4e2b5 | 683 | const char *value; |
18e46a03 | 684 | BlockBackend *blk; |
33cb7dc8 | 685 | DriveInfo *dinfo = NULL; |
f298d071 | 686 | QDict *bs_opts; |
33cb7dc8 KW |
687 | QemuOpts *legacy_opts; |
688 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 689 | BlockInterfaceType type; |
b41a7338 | 690 | int cyls, heads, secs, translation; |
87a899c5 | 691 | int max_devs, bus_id, unit_id, index; |
394c7d4d | 692 | const char *devaddr; |
ee13ed1c | 693 | const char *werror, *rerror; |
a7fdbcf0 FZ |
694 | bool read_only = false; |
695 | bool copy_on_read; | |
bcf83158 | 696 | const char *serial; |
d095b465 | 697 | const char *filename; |
33cb7dc8 | 698 | Error *local_err = NULL; |
247147fb | 699 | int i; |
29c4e2b5 | 700 | |
57975222 | 701 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
702 | static const struct { |
703 | const char *from; | |
704 | const char *to; | |
705 | } opt_renames[] = { | |
706 | { "iops", "throttling.iops-total" }, | |
707 | { "iops_rd", "throttling.iops-read" }, | |
708 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 709 | |
247147fb KW |
710 | { "bps", "throttling.bps-total" }, |
711 | { "bps_rd", "throttling.bps-read" }, | |
712 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 713 | |
247147fb KW |
714 | { "iops_max", "throttling.iops-total-max" }, |
715 | { "iops_rd_max", "throttling.iops-read-max" }, | |
716 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 717 | |
247147fb KW |
718 | { "bps_max", "throttling.bps-total-max" }, |
719 | { "bps_rd_max", "throttling.bps-read-max" }, | |
720 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 721 | |
247147fb | 722 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 723 | |
247147fb KW |
724 | { "readonly", "read-only" }, |
725 | }; | |
726 | ||
727 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
728 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
729 | &local_err); | |
730 | if (local_err) { | |
731 | error_report("%s", error_get_pretty(local_err)); | |
732 | error_free(local_err); | |
733 | return NULL; | |
734 | } | |
247147fb | 735 | } |
0f227a94 | 736 | |
29c4e2b5 KW |
737 | value = qemu_opt_get(all_opts, "cache"); |
738 | if (value) { | |
739 | int flags = 0; | |
740 | ||
741 | if (bdrv_parse_cache_flags(value, &flags) != 0) { | |
742 | error_report("invalid cache option"); | |
743 | return NULL; | |
744 | } | |
745 | ||
746 | /* Specific options take precedence */ | |
747 | if (!qemu_opt_get(all_opts, "cache.writeback")) { | |
748 | qemu_opt_set_bool(all_opts, "cache.writeback", | |
749 | !!(flags & BDRV_O_CACHE_WB)); | |
750 | } | |
751 | if (!qemu_opt_get(all_opts, "cache.direct")) { | |
752 | qemu_opt_set_bool(all_opts, "cache.direct", | |
753 | !!(flags & BDRV_O_NOCACHE)); | |
754 | } | |
755 | if (!qemu_opt_get(all_opts, "cache.no-flush")) { | |
756 | qemu_opt_set_bool(all_opts, "cache.no-flush", | |
757 | !!(flags & BDRV_O_NO_FLUSH)); | |
758 | } | |
759 | qemu_opt_unset(all_opts, "cache"); | |
760 | } | |
761 | ||
f298d071 KW |
762 | /* Get a QDict for processing the options */ |
763 | bs_opts = qdict_new(); | |
764 | qemu_opts_to_qdict(all_opts, bs_opts); | |
765 | ||
87ea75d5 PC |
766 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
767 | &error_abort); | |
33cb7dc8 | 768 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 769 | if (local_err) { |
e8817e7b | 770 | error_report("%s", error_get_pretty(local_err)); |
33cb7dc8 KW |
771 | error_free(local_err); |
772 | goto fail; | |
773 | } | |
774 | ||
26929298 KW |
775 | /* Deprecated option boot=[on|off] */ |
776 | if (qemu_opt_get(legacy_opts, "boot") != NULL) { | |
777 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " | |
778 | "ignored. Future versions will reject this parameter. Please " | |
779 | "update your scripts.\n"); | |
780 | } | |
781 | ||
33cb7dc8 KW |
782 | /* Media type */ |
783 | value = qemu_opt_get(legacy_opts, "media"); | |
784 | if (value) { | |
785 | if (!strcmp(value, "disk")) { | |
786 | media = MEDIA_DISK; | |
787 | } else if (!strcmp(value, "cdrom")) { | |
788 | media = MEDIA_CDROM; | |
a7fdbcf0 | 789 | read_only = true; |
33cb7dc8 KW |
790 | } else { |
791 | error_report("'%s' invalid media", value); | |
792 | goto fail; | |
793 | } | |
794 | } | |
795 | ||
0ebd24e0 | 796 | /* copy-on-read is disabled with a warning for read-only devices */ |
a7fdbcf0 | 797 | read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); |
0ebd24e0 KW |
798 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
799 | ||
800 | if (read_only && copy_on_read) { | |
801 | error_report("warning: disabling copy-on-read on read-only drive"); | |
802 | copy_on_read = false; | |
803 | } | |
804 | ||
805 | qdict_put(bs_opts, "read-only", | |
806 | qstring_from_str(read_only ? "on" : "off")); | |
807 | qdict_put(bs_opts, "copy-on-read", | |
808 | qstring_from_str(copy_on_read ? "on" :"off")); | |
809 | ||
593d464b KW |
810 | /* Controller type */ |
811 | value = qemu_opt_get(legacy_opts, "if"); | |
812 | if (value) { | |
813 | for (type = 0; | |
814 | type < IF_COUNT && strcmp(value, if_name[type]); | |
815 | type++) { | |
816 | } | |
817 | if (type == IF_COUNT) { | |
818 | error_report("unsupported bus type '%s'", value); | |
819 | goto fail; | |
820 | } | |
821 | } else { | |
822 | type = block_default_type; | |
823 | } | |
824 | ||
b41a7338 KW |
825 | /* Geometry */ |
826 | cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); | |
827 | heads = qemu_opt_get_number(legacy_opts, "heads", 0); | |
828 | secs = qemu_opt_get_number(legacy_opts, "secs", 0); | |
829 | ||
830 | if (cyls || heads || secs) { | |
831 | if (cyls < 1) { | |
832 | error_report("invalid physical cyls number"); | |
833 | goto fail; | |
834 | } | |
835 | if (heads < 1) { | |
836 | error_report("invalid physical heads number"); | |
837 | goto fail; | |
838 | } | |
839 | if (secs < 1) { | |
840 | error_report("invalid physical secs number"); | |
841 | goto fail; | |
842 | } | |
843 | } | |
844 | ||
845 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
846 | value = qemu_opt_get(legacy_opts, "trans"); | |
847 | if (value != NULL) { | |
848 | if (!cyls) { | |
849 | error_report("'%s' trans must be used with cyls, heads and secs", | |
850 | value); | |
851 | goto fail; | |
852 | } | |
853 | if (!strcmp(value, "none")) { | |
854 | translation = BIOS_ATA_TRANSLATION_NONE; | |
855 | } else if (!strcmp(value, "lba")) { | |
856 | translation = BIOS_ATA_TRANSLATION_LBA; | |
f31c41ff PB |
857 | } else if (!strcmp(value, "large")) { |
858 | translation = BIOS_ATA_TRANSLATION_LARGE; | |
859 | } else if (!strcmp(value, "rechs")) { | |
860 | translation = BIOS_ATA_TRANSLATION_RECHS; | |
b41a7338 KW |
861 | } else if (!strcmp(value, "auto")) { |
862 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
863 | } else { | |
864 | error_report("'%s' invalid translation type", value); | |
865 | goto fail; | |
866 | } | |
867 | } | |
868 | ||
869 | if (media == MEDIA_CDROM) { | |
870 | if (cyls || secs || heads) { | |
871 | error_report("CHS can't be set with media=cdrom"); | |
872 | goto fail; | |
873 | } | |
874 | } | |
875 | ||
87a899c5 KW |
876 | /* Device address specified by bus/unit or index. |
877 | * If none was specified, try to find the first free one. */ | |
878 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
879 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
880 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
881 | ||
882 | max_devs = if_max_devs[type]; | |
883 | ||
884 | if (index != -1) { | |
885 | if (bus_id != 0 || unit_id != -1) { | |
886 | error_report("index cannot be used with bus and unit"); | |
887 | goto fail; | |
888 | } | |
889 | bus_id = drive_index_to_bus_id(type, index); | |
890 | unit_id = drive_index_to_unit_id(type, index); | |
891 | } | |
892 | ||
893 | if (unit_id == -1) { | |
894 | unit_id = 0; | |
895 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
896 | unit_id++; | |
897 | if (max_devs && unit_id >= max_devs) { | |
898 | unit_id -= max_devs; | |
899 | bus_id++; | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
904 | if (max_devs && unit_id >= max_devs) { | |
905 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); | |
906 | goto fail; | |
907 | } | |
908 | ||
909 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
910 | error_report("drive with bus=%d, unit=%d (index=%d) exists", | |
911 | bus_id, unit_id, index); | |
912 | goto fail; | |
913 | } | |
914 | ||
bcf83158 KW |
915 | /* Serial number */ |
916 | serial = qemu_opt_get(legacy_opts, "serial"); | |
917 | ||
87a899c5 KW |
918 | /* no id supplied -> create one */ |
919 | if (qemu_opts_id(all_opts) == NULL) { | |
920 | char *new_id; | |
921 | const char *mediastr = ""; | |
922 | if (type == IF_IDE || type == IF_SCSI) { | |
923 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
924 | } | |
925 | if (max_devs) { | |
926 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
927 | mediastr, unit_id); | |
928 | } else { | |
929 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
930 | mediastr, unit_id); | |
931 | } | |
932 | qdict_put(bs_opts, "id", qstring_from_str(new_id)); | |
933 | g_free(new_id); | |
934 | } | |
935 | ||
394c7d4d KW |
936 | /* Add virtio block device */ |
937 | devaddr = qemu_opt_get(legacy_opts, "addr"); | |
938 | if (devaddr && type != IF_VIRTIO) { | |
939 | error_report("addr is not supported by this bus type"); | |
940 | goto fail; | |
941 | } | |
942 | ||
943 | if (type == IF_VIRTIO) { | |
944 | QemuOpts *devopts; | |
87ea75d5 PC |
945 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
946 | &error_abort); | |
394c7d4d KW |
947 | if (arch_type == QEMU_ARCH_S390X) { |
948 | qemu_opt_set(devopts, "driver", "virtio-blk-s390"); | |
949 | } else { | |
950 | qemu_opt_set(devopts, "driver", "virtio-blk-pci"); | |
951 | } | |
952 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id")); | |
953 | if (devaddr) { | |
954 | qemu_opt_set(devopts, "addr", devaddr); | |
955 | } | |
956 | } | |
957 | ||
d095b465 HR |
958 | filename = qemu_opt_get(legacy_opts, "file"); |
959 | ||
ee13ed1c KW |
960 | /* Check werror/rerror compatibility with if=... */ |
961 | werror = qemu_opt_get(legacy_opts, "werror"); | |
962 | if (werror != NULL) { | |
963 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
964 | type != IF_NONE) { | |
965 | error_report("werror is not supported by this bus type"); | |
966 | goto fail; | |
967 | } | |
968 | qdict_put(bs_opts, "werror", qstring_from_str(werror)); | |
969 | } | |
970 | ||
971 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
972 | if (rerror != NULL) { | |
973 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
974 | type != IF_NONE) { | |
975 | error_report("rerror is not supported by this bus type"); | |
976 | goto fail; | |
977 | } | |
978 | qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); | |
979 | } | |
980 | ||
2d246f01 | 981 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 982 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 983 | bs_opts = NULL; |
18e46a03 | 984 | if (!blk) { |
84d18f06 | 985 | if (local_err) { |
e8817e7b | 986 | error_report("%s", error_get_pretty(local_err)); |
b681072d KW |
987 | error_free(local_err); |
988 | } | |
2d246f01 | 989 | goto fail; |
b681072d | 990 | } else { |
84d18f06 | 991 | assert(!local_err); |
2d246f01 KW |
992 | } |
993 | ||
26f8b3a8 MA |
994 | /* Create legacy DriveInfo */ |
995 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 996 | dinfo->opts = all_opts; |
2d246f01 | 997 | |
b41a7338 KW |
998 | dinfo->cyls = cyls; |
999 | dinfo->heads = heads; | |
1000 | dinfo->secs = secs; | |
1001 | dinfo->trans = translation; | |
1002 | ||
ee13ed1c | 1003 | dinfo->type = type; |
87a899c5 KW |
1004 | dinfo->bus = bus_id; |
1005 | dinfo->unit = unit_id; | |
394c7d4d | 1006 | dinfo->devaddr = devaddr; |
bcf83158 KW |
1007 | dinfo->serial = g_strdup(serial); |
1008 | ||
26f8b3a8 MA |
1009 | blk_set_legacy_dinfo(blk, dinfo); |
1010 | ||
e34ef046 KW |
1011 | switch(type) { |
1012 | case IF_IDE: | |
1013 | case IF_SCSI: | |
1014 | case IF_XEN: | |
1015 | case IF_NONE: | |
1016 | dinfo->media_cd = media == MEDIA_CDROM; | |
1017 | break; | |
1018 | default: | |
1019 | break; | |
1020 | } | |
1021 | ||
2d246f01 | 1022 | fail: |
33cb7dc8 | 1023 | qemu_opts_del(legacy_opts); |
3cb0e25c | 1024 | QDECREF(bs_opts); |
2d246f01 | 1025 | return dinfo; |
57975222 KW |
1026 | } |
1027 | ||
666daa68 MA |
1028 | void do_commit(Monitor *mon, const QDict *qdict) |
1029 | { | |
666daa68 | 1030 | const char *device = qdict_get_str(qdict, "device"); |
6ab4b5ab | 1031 | BlockDriverState *bs; |
e8877497 | 1032 | int ret; |
666daa68 | 1033 | |
6ab4b5ab | 1034 | if (!strcmp(device, "all")) { |
e8877497 | 1035 | ret = bdrv_commit_all(); |
6ab4b5ab MA |
1036 | } else { |
1037 | bs = bdrv_find(device); | |
ac59eb95 | 1038 | if (!bs) { |
58513bde | 1039 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 1040 | return; |
6ab4b5ab | 1041 | } |
2d3735d3 | 1042 | ret = bdrv_commit(bs); |
58513bde JC |
1043 | } |
1044 | if (ret < 0) { | |
1045 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1046 | strerror(-ret)); | |
666daa68 MA |
1047 | } |
1048 | } | |
1049 | ||
6cc2a415 PB |
1050 | static void blockdev_do_action(int kind, void *data, Error **errp) |
1051 | { | |
c8a83e85 KW |
1052 | TransactionAction action; |
1053 | TransactionActionList list; | |
6cc2a415 PB |
1054 | |
1055 | action.kind = kind; | |
1056 | action.data = data; | |
1057 | list.value = &action; | |
1058 | list.next = NULL; | |
1059 | qmp_transaction(&list, errp); | |
1060 | } | |
1061 | ||
0901f67e BC |
1062 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1063 | bool has_node_name, const char *node_name, | |
1064 | const char *snapshot_file, | |
1065 | bool has_snapshot_node_name, | |
1066 | const char *snapshot_node_name, | |
6106e249 | 1067 | bool has_format, const char *format, |
0901f67e | 1068 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1069 | { |
6cc2a415 | 1070 | BlockdevSnapshot snapshot = { |
0901f67e | 1071 | .has_device = has_device, |
6cc2a415 | 1072 | .device = (char *) device, |
0901f67e BC |
1073 | .has_node_name = has_node_name, |
1074 | .node_name = (char *) node_name, | |
6cc2a415 | 1075 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1076 | .has_snapshot_node_name = has_snapshot_node_name, |
1077 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1078 | .has_format = has_format, |
1079 | .format = (char *) format, | |
1080 | .has_mode = has_mode, | |
1081 | .mode = mode, | |
1082 | }; | |
c8a83e85 KW |
1083 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
1084 | &snapshot, errp); | |
f8882568 JS |
1085 | } |
1086 | ||
f323bc9e WX |
1087 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1088 | const char *name, | |
1089 | Error **errp) | |
1090 | { | |
1091 | BlockdevSnapshotInternal snapshot = { | |
1092 | .device = (char *) device, | |
1093 | .name = (char *) name | |
1094 | }; | |
1095 | ||
1096 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
1097 | &snapshot, errp); | |
1098 | } | |
1099 | ||
44e3e053 WX |
1100 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1101 | bool has_id, | |
1102 | const char *id, | |
1103 | bool has_name, | |
1104 | const char *name, | |
1105 | Error **errp) | |
1106 | { | |
1107 | BlockDriverState *bs = bdrv_find(device); | |
4ef3982a | 1108 | AioContext *aio_context; |
44e3e053 WX |
1109 | QEMUSnapshotInfo sn; |
1110 | Error *local_err = NULL; | |
1111 | SnapshotInfo *info = NULL; | |
1112 | int ret; | |
1113 | ||
1114 | if (!bs) { | |
1115 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1116 | return NULL; | |
1117 | } | |
1118 | ||
1119 | if (!has_id) { | |
1120 | id = NULL; | |
1121 | } | |
1122 | ||
1123 | if (!has_name) { | |
1124 | name = NULL; | |
1125 | } | |
1126 | ||
1127 | if (!id && !name) { | |
1128 | error_setg(errp, "Name or id must be provided"); | |
1129 | return NULL; | |
1130 | } | |
1131 | ||
4ef3982a SH |
1132 | aio_context = bdrv_get_aio_context(bs); |
1133 | aio_context_acquire(aio_context); | |
1134 | ||
0b928854 SH |
1135 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1136 | goto out_aio_context; | |
1137 | } | |
1138 | ||
44e3e053 | 1139 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1140 | if (local_err) { |
44e3e053 | 1141 | error_propagate(errp, local_err); |
4ef3982a | 1142 | goto out_aio_context; |
44e3e053 WX |
1143 | } |
1144 | if (!ret) { | |
1145 | error_setg(errp, | |
1146 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1147 | "device '%s'", | |
1148 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1149 | goto out_aio_context; |
44e3e053 WX |
1150 | } |
1151 | ||
1152 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1153 | if (local_err) { |
44e3e053 | 1154 | error_propagate(errp, local_err); |
4ef3982a | 1155 | goto out_aio_context; |
44e3e053 WX |
1156 | } |
1157 | ||
4ef3982a SH |
1158 | aio_context_release(aio_context); |
1159 | ||
5839e53b | 1160 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1161 | info->id = g_strdup(sn.id_str); |
1162 | info->name = g_strdup(sn.name); | |
1163 | info->date_nsec = sn.date_nsec; | |
1164 | info->date_sec = sn.date_sec; | |
1165 | info->vm_state_size = sn.vm_state_size; | |
1166 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1167 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1168 | ||
1169 | return info; | |
4ef3982a SH |
1170 | |
1171 | out_aio_context: | |
1172 | aio_context_release(aio_context); | |
1173 | return NULL; | |
44e3e053 | 1174 | } |
8802d1fd JC |
1175 | |
1176 | /* New and old BlockDriverState structs for group snapshots */ | |
ba0c86a3 | 1177 | |
ba5d6ab6 | 1178 | typedef struct BlkTransactionState BlkTransactionState; |
ba0c86a3 WX |
1179 | |
1180 | /* Only prepare() may fail. In a single transaction, only one of commit() or | |
1181 | abort() will be called, clean() will always be called if it present. */ | |
1182 | typedef struct BdrvActionOps { | |
1183 | /* Size of state struct, in bytes. */ | |
1184 | size_t instance_size; | |
1185 | /* Prepare the work, must NOT be NULL. */ | |
ba5d6ab6 | 1186 | void (*prepare)(BlkTransactionState *common, Error **errp); |
f9ea81e8 | 1187 | /* Commit the changes, can be NULL. */ |
ba5d6ab6 | 1188 | void (*commit)(BlkTransactionState *common); |
ba0c86a3 | 1189 | /* Abort the changes on fail, can be NULL. */ |
ba5d6ab6 | 1190 | void (*abort)(BlkTransactionState *common); |
ba0c86a3 | 1191 | /* Clean up resource in the end, can be NULL. */ |
ba5d6ab6 | 1192 | void (*clean)(BlkTransactionState *common); |
ba0c86a3 WX |
1193 | } BdrvActionOps; |
1194 | ||
1195 | /* | |
1196 | * This structure must be arranged as first member in child type, assuming | |
1197 | * that compiler will also arrange it to the same address with parent instance. | |
1198 | * Later it will be used in free(). | |
1199 | */ | |
ba5d6ab6 | 1200 | struct BlkTransactionState { |
c8a83e85 | 1201 | TransactionAction *action; |
ba0c86a3 | 1202 | const BdrvActionOps *ops; |
ba5d6ab6 | 1203 | QSIMPLEQ_ENTRY(BlkTransactionState) entry; |
ba0c86a3 WX |
1204 | }; |
1205 | ||
bbe86010 WX |
1206 | /* internal snapshot private data */ |
1207 | typedef struct InternalSnapshotState { | |
1208 | BlkTransactionState common; | |
1209 | BlockDriverState *bs; | |
1210 | QEMUSnapshotInfo sn; | |
1211 | } InternalSnapshotState; | |
1212 | ||
1213 | static void internal_snapshot_prepare(BlkTransactionState *common, | |
1214 | Error **errp) | |
1215 | { | |
f70edf99 | 1216 | Error *local_err = NULL; |
bbe86010 WX |
1217 | const char *device; |
1218 | const char *name; | |
1219 | BlockDriverState *bs; | |
1220 | QEMUSnapshotInfo old_sn, *sn; | |
1221 | bool ret; | |
1222 | qemu_timeval tv; | |
1223 | BlockdevSnapshotInternal *internal; | |
1224 | InternalSnapshotState *state; | |
1225 | int ret1; | |
1226 | ||
1227 | g_assert(common->action->kind == | |
1228 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); | |
1229 | internal = common->action->blockdev_snapshot_internal_sync; | |
1230 | state = DO_UPCAST(InternalSnapshotState, common, common); | |
1231 | ||
1232 | /* 1. parse input */ | |
1233 | device = internal->device; | |
1234 | name = internal->name; | |
1235 | ||
1236 | /* 2. check for validation */ | |
1237 | bs = bdrv_find(device); | |
1238 | if (!bs) { | |
1239 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1240 | return; | |
1241 | } | |
1242 | ||
1243 | if (!bdrv_is_inserted(bs)) { | |
1244 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1245 | return; | |
1246 | } | |
1247 | ||
1248 | if (bdrv_is_read_only(bs)) { | |
1249 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1250 | return; | |
1251 | } | |
1252 | ||
1253 | if (!bdrv_can_snapshot(bs)) { | |
1254 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
1255 | bs->drv->format_name, device, "internal snapshot"); | |
1256 | return; | |
1257 | } | |
1258 | ||
1259 | if (!strlen(name)) { | |
1260 | error_setg(errp, "Name is empty"); | |
1261 | return; | |
1262 | } | |
1263 | ||
1264 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1265 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1266 | &local_err); | |
1267 | if (local_err) { | |
1268 | error_propagate(errp, local_err); | |
bbe86010 WX |
1269 | return; |
1270 | } else if (ret) { | |
1271 | error_setg(errp, | |
1272 | "Snapshot with name '%s' already exists on device '%s'", | |
1273 | name, device); | |
1274 | return; | |
1275 | } | |
1276 | ||
1277 | /* 3. take the snapshot */ | |
1278 | sn = &state->sn; | |
1279 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1280 | qemu_gettimeofday(&tv); | |
1281 | sn->date_sec = tv.tv_sec; | |
1282 | sn->date_nsec = tv.tv_usec * 1000; | |
1283 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1284 | ||
1285 | ret1 = bdrv_snapshot_create(bs, sn); | |
1286 | if (ret1 < 0) { | |
1287 | error_setg_errno(errp, -ret1, | |
1288 | "Failed to create snapshot '%s' on device '%s'", | |
1289 | name, device); | |
1290 | return; | |
1291 | } | |
1292 | ||
1293 | /* 4. succeed, mark a snapshot is created */ | |
1294 | state->bs = bs; | |
1295 | } | |
1296 | ||
1297 | static void internal_snapshot_abort(BlkTransactionState *common) | |
1298 | { | |
1299 | InternalSnapshotState *state = | |
1300 | DO_UPCAST(InternalSnapshotState, common, common); | |
1301 | BlockDriverState *bs = state->bs; | |
1302 | QEMUSnapshotInfo *sn = &state->sn; | |
1303 | Error *local_error = NULL; | |
1304 | ||
1305 | if (!bs) { | |
1306 | return; | |
1307 | } | |
1308 | ||
1309 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
1310 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " | |
1311 | "device '%s' in abort: %s", | |
1312 | sn->id_str, | |
1313 | sn->name, | |
1314 | bdrv_get_device_name(bs), | |
1315 | error_get_pretty(local_error)); | |
1316 | error_free(local_error); | |
1317 | } | |
1318 | } | |
1319 | ||
ba0c86a3 | 1320 | /* external snapshot private data */ |
ba5d6ab6 SH |
1321 | typedef struct ExternalSnapshotState { |
1322 | BlkTransactionState common; | |
8802d1fd JC |
1323 | BlockDriverState *old_bs; |
1324 | BlockDriverState *new_bs; | |
ba5d6ab6 | 1325 | } ExternalSnapshotState; |
8802d1fd | 1326 | |
ba5d6ab6 | 1327 | static void external_snapshot_prepare(BlkTransactionState *common, |
9b9877ee WX |
1328 | Error **errp) |
1329 | { | |
9b9877ee WX |
1330 | BlockDriver *drv; |
1331 | int flags, ret; | |
0901f67e | 1332 | QDict *options = NULL; |
9b9877ee | 1333 | Error *local_err = NULL; |
0901f67e | 1334 | bool has_device = false; |
e2a31e87 | 1335 | const char *device; |
0901f67e BC |
1336 | bool has_node_name = false; |
1337 | const char *node_name; | |
1338 | bool has_snapshot_node_name = false; | |
1339 | const char *snapshot_node_name; | |
e2a31e87 WX |
1340 | const char *new_image_file; |
1341 | const char *format = "qcow2"; | |
1342 | enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
ba5d6ab6 SH |
1343 | ExternalSnapshotState *state = |
1344 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1345 | TransactionAction *action = common->action; |
9b9877ee | 1346 | |
e2a31e87 | 1347 | /* get parameters */ |
c8a83e85 | 1348 | g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); |
e2a31e87 | 1349 | |
0901f67e | 1350 | has_device = action->blockdev_snapshot_sync->has_device; |
e2a31e87 | 1351 | device = action->blockdev_snapshot_sync->device; |
0901f67e BC |
1352 | has_node_name = action->blockdev_snapshot_sync->has_node_name; |
1353 | node_name = action->blockdev_snapshot_sync->node_name; | |
1354 | has_snapshot_node_name = | |
1355 | action->blockdev_snapshot_sync->has_snapshot_node_name; | |
1356 | snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name; | |
1357 | ||
e2a31e87 WX |
1358 | new_image_file = action->blockdev_snapshot_sync->snapshot_file; |
1359 | if (action->blockdev_snapshot_sync->has_format) { | |
1360 | format = action->blockdev_snapshot_sync->format; | |
1361 | } | |
1362 | if (action->blockdev_snapshot_sync->has_mode) { | |
1363 | mode = action->blockdev_snapshot_sync->mode; | |
1364 | } | |
1365 | ||
1366 | /* start processing */ | |
9b9877ee WX |
1367 | drv = bdrv_find_format(format); |
1368 | if (!drv) { | |
1369 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
1370 | return; | |
1371 | } | |
1372 | ||
0901f67e BC |
1373 | state->old_bs = bdrv_lookup_bs(has_device ? device : NULL, |
1374 | has_node_name ? node_name : NULL, | |
1375 | &local_err); | |
84d18f06 | 1376 | if (local_err) { |
0901f67e BC |
1377 | error_propagate(errp, local_err); |
1378 | return; | |
1379 | } | |
1380 | ||
1381 | if (has_node_name && !has_snapshot_node_name) { | |
1382 | error_setg(errp, "New snapshot node name missing"); | |
1383 | return; | |
1384 | } | |
1385 | ||
1386 | if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) { | |
1387 | error_setg(errp, "New snapshot node name already existing"); | |
9b9877ee WX |
1388 | return; |
1389 | } | |
1390 | ||
ba5d6ab6 | 1391 | if (!bdrv_is_inserted(state->old_bs)) { |
9b9877ee WX |
1392 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
1393 | return; | |
1394 | } | |
1395 | ||
3718d8ab FZ |
1396 | if (bdrv_op_is_blocked(state->old_bs, |
1397 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
9b9877ee WX |
1398 | return; |
1399 | } | |
1400 | ||
ba5d6ab6 SH |
1401 | if (!bdrv_is_read_only(state->old_bs)) { |
1402 | if (bdrv_flush(state->old_bs)) { | |
9b9877ee WX |
1403 | error_set(errp, QERR_IO_ERROR); |
1404 | return; | |
1405 | } | |
1406 | } | |
1407 | ||
212a5a8f | 1408 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
f6186f49 BC |
1409 | error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); |
1410 | return; | |
1411 | } | |
1412 | ||
ba5d6ab6 | 1413 | flags = state->old_bs->open_flags; |
9b9877ee | 1414 | |
9b9877ee WX |
1415 | /* create new image w/backing file */ |
1416 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1417 | bdrv_img_create(new_image_file, format, | |
ba5d6ab6 SH |
1418 | state->old_bs->filename, |
1419 | state->old_bs->drv->format_name, | |
9b9877ee | 1420 | NULL, -1, flags, &local_err, false); |
84d18f06 | 1421 | if (local_err) { |
9b9877ee WX |
1422 | error_propagate(errp, local_err); |
1423 | return; | |
1424 | } | |
1425 | } | |
1426 | ||
0901f67e BC |
1427 | if (has_snapshot_node_name) { |
1428 | options = qdict_new(); | |
1429 | qdict_put(options, "node-name", | |
1430 | qstring_from_str(snapshot_node_name)); | |
1431 | } | |
1432 | ||
9b9877ee WX |
1433 | /* TODO Inherit bs->options or only take explicit options with an |
1434 | * extended QMP command? */ | |
f67503e5 | 1435 | assert(state->new_bs == NULL); |
ddf5636d | 1436 | ret = bdrv_open(&state->new_bs, new_image_file, NULL, options, |
34b5d2c6 | 1437 | flags | BDRV_O_NO_BACKING, drv, &local_err); |
f67503e5 | 1438 | /* We will manually add the backing_hd field to the bs later */ |
9b9877ee | 1439 | if (ret != 0) { |
34b5d2c6 | 1440 | error_propagate(errp, local_err); |
9b9877ee WX |
1441 | } |
1442 | } | |
1443 | ||
ba5d6ab6 | 1444 | static void external_snapshot_commit(BlkTransactionState *common) |
3b0047e8 | 1445 | { |
ba5d6ab6 SH |
1446 | ExternalSnapshotState *state = |
1447 | DO_UPCAST(ExternalSnapshotState, common, common); | |
ba0c86a3 | 1448 | |
ba5d6ab6 SH |
1449 | /* This removes our old bs and adds the new bs */ |
1450 | bdrv_append(state->new_bs, state->old_bs); | |
3b0047e8 WX |
1451 | /* We don't need (or want) to use the transactional |
1452 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1453 | * don't want to abort all of them if one of them fails the reopen */ | |
ba5d6ab6 | 1454 | bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, |
3b0047e8 WX |
1455 | NULL); |
1456 | } | |
1457 | ||
ba5d6ab6 | 1458 | static void external_snapshot_abort(BlkTransactionState *common) |
96b86bf7 | 1459 | { |
ba5d6ab6 SH |
1460 | ExternalSnapshotState *state = |
1461 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1462 | if (state->new_bs) { | |
4f6fd349 | 1463 | bdrv_unref(state->new_bs); |
96b86bf7 WX |
1464 | } |
1465 | } | |
1466 | ||
3037f364 SH |
1467 | typedef struct DriveBackupState { |
1468 | BlkTransactionState common; | |
1469 | BlockDriverState *bs; | |
1470 | BlockJob *job; | |
1471 | } DriveBackupState; | |
1472 | ||
1473 | static void drive_backup_prepare(BlkTransactionState *common, Error **errp) | |
1474 | { | |
1475 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1476 | DriveBackup *backup; | |
1477 | Error *local_err = NULL; | |
1478 | ||
1479 | assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); | |
1480 | backup = common->action->drive_backup; | |
1481 | ||
1482 | qmp_drive_backup(backup->device, backup->target, | |
1483 | backup->has_format, backup->format, | |
b53169ea | 1484 | backup->sync, |
3037f364 SH |
1485 | backup->has_mode, backup->mode, |
1486 | backup->has_speed, backup->speed, | |
1487 | backup->has_on_source_error, backup->on_source_error, | |
1488 | backup->has_on_target_error, backup->on_target_error, | |
1489 | &local_err); | |
84d18f06 | 1490 | if (local_err) { |
3037f364 SH |
1491 | error_propagate(errp, local_err); |
1492 | state->bs = NULL; | |
1493 | state->job = NULL; | |
1494 | return; | |
1495 | } | |
1496 | ||
1497 | state->bs = bdrv_find(backup->device); | |
1498 | state->job = state->bs->job; | |
1499 | } | |
1500 | ||
1501 | static void drive_backup_abort(BlkTransactionState *common) | |
1502 | { | |
1503 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1504 | BlockDriverState *bs = state->bs; | |
1505 | ||
1506 | /* Only cancel if it's the job we started */ | |
1507 | if (bs && bs->job && bs->job == state->job) { | |
1508 | block_job_cancel_sync(bs->job); | |
1509 | } | |
1510 | } | |
1511 | ||
78b18b78 SH |
1512 | static void abort_prepare(BlkTransactionState *common, Error **errp) |
1513 | { | |
1514 | error_setg(errp, "Transaction aborted using Abort action"); | |
1515 | } | |
1516 | ||
1517 | static void abort_commit(BlkTransactionState *common) | |
1518 | { | |
dfc6f865 | 1519 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
1520 | } |
1521 | ||
ba0c86a3 | 1522 | static const BdrvActionOps actions[] = { |
c8a83e85 | 1523 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 1524 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
1525 | .prepare = external_snapshot_prepare, |
1526 | .commit = external_snapshot_commit, | |
1527 | .abort = external_snapshot_abort, | |
1528 | }, | |
3037f364 SH |
1529 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
1530 | .instance_size = sizeof(DriveBackupState), | |
1531 | .prepare = drive_backup_prepare, | |
1532 | .abort = drive_backup_abort, | |
1533 | }, | |
78b18b78 SH |
1534 | [TRANSACTION_ACTION_KIND_ABORT] = { |
1535 | .instance_size = sizeof(BlkTransactionState), | |
1536 | .prepare = abort_prepare, | |
1537 | .commit = abort_commit, | |
1538 | }, | |
bbe86010 WX |
1539 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
1540 | .instance_size = sizeof(InternalSnapshotState), | |
1541 | .prepare = internal_snapshot_prepare, | |
1542 | .abort = internal_snapshot_abort, | |
1543 | }, | |
ba0c86a3 WX |
1544 | }; |
1545 | ||
8802d1fd JC |
1546 | /* |
1547 | * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail | |
1548 | * then we do not pivot any of the devices in the group, and abandon the | |
1549 | * snapshots | |
1550 | */ | |
c8a83e85 | 1551 | void qmp_transaction(TransactionActionList *dev_list, Error **errp) |
8802d1fd | 1552 | { |
c8a83e85 | 1553 | TransactionActionList *dev_entry = dev_list; |
ba5d6ab6 | 1554 | BlkTransactionState *state, *next; |
43e17041 | 1555 | Error *local_err = NULL; |
8802d1fd | 1556 | |
ba5d6ab6 | 1557 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; |
8802d1fd JC |
1558 | QSIMPLEQ_INIT(&snap_bdrv_states); |
1559 | ||
1560 | /* drain all i/o before any snapshots */ | |
1561 | bdrv_drain_all(); | |
1562 | ||
1563 | /* We don't do anything in this loop that commits us to the snapshot */ | |
1564 | while (NULL != dev_entry) { | |
c8a83e85 | 1565 | TransactionAction *dev_info = NULL; |
ba0c86a3 | 1566 | const BdrvActionOps *ops; |
52e7c241 | 1567 | |
8802d1fd JC |
1568 | dev_info = dev_entry->value; |
1569 | dev_entry = dev_entry->next; | |
1570 | ||
ba0c86a3 WX |
1571 | assert(dev_info->kind < ARRAY_SIZE(actions)); |
1572 | ||
1573 | ops = &actions[dev_info->kind]; | |
aa3fe714 HR |
1574 | assert(ops->instance_size > 0); |
1575 | ||
ba5d6ab6 SH |
1576 | state = g_malloc0(ops->instance_size); |
1577 | state->ops = ops; | |
1578 | state->action = dev_info; | |
1579 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); | |
8802d1fd | 1580 | |
ba5d6ab6 | 1581 | state->ops->prepare(state, &local_err); |
84d18f06 | 1582 | if (local_err) { |
ba0c86a3 WX |
1583 | error_propagate(errp, local_err); |
1584 | goto delete_and_fail; | |
8802d1fd | 1585 | } |
8802d1fd JC |
1586 | } |
1587 | ||
ba5d6ab6 | 1588 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
1589 | if (state->ops->commit) { |
1590 | state->ops->commit(state); | |
1591 | } | |
8802d1fd JC |
1592 | } |
1593 | ||
1594 | /* success */ | |
1595 | goto exit; | |
1596 | ||
1597 | delete_and_fail: | |
1598 | /* | |
1599 | * failure, and it is all-or-none; abandon each new bs, and keep using | |
1600 | * the original bs for all images | |
1601 | */ | |
ba5d6ab6 SH |
1602 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
1603 | if (state->ops->abort) { | |
1604 | state->ops->abort(state); | |
ba0c86a3 | 1605 | } |
8802d1fd JC |
1606 | } |
1607 | exit: | |
ba5d6ab6 SH |
1608 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
1609 | if (state->ops->clean) { | |
1610 | state->ops->clean(state); | |
ba0c86a3 | 1611 | } |
ba5d6ab6 | 1612 | g_free(state); |
8802d1fd | 1613 | } |
8802d1fd JC |
1614 | } |
1615 | ||
1616 | ||
6007cdd4 | 1617 | static void eject_device(BlockBackend *blk, int force, Error **errp) |
666daa68 | 1618 | { |
6007cdd4 | 1619 | BlockDriverState *bs = blk_bs(blk); |
e3442099 SH |
1620 | AioContext *aio_context; |
1621 | ||
1622 | aio_context = bdrv_get_aio_context(bs); | |
1623 | aio_context_acquire(aio_context); | |
6007cdd4 | 1624 | |
3718d8ab | 1625 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { |
e3442099 | 1626 | goto out; |
2d3735d3 | 1627 | } |
a7f53e26 | 1628 | if (!blk_dev_has_removable_media(blk)) { |
f231b88d CR |
1629 | error_setg(errp, "Device '%s' is not removable", |
1630 | bdrv_get_device_name(bs)); | |
e3442099 | 1631 | goto out; |
ea8f942f | 1632 | } |
92d48558 | 1633 | |
a7f53e26 MA |
1634 | if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) { |
1635 | blk_dev_eject_request(blk, force); | |
025ccaa7 | 1636 | if (!force) { |
f231b88d CR |
1637 | error_setg(errp, "Device '%s' is locked", |
1638 | bdrv_get_device_name(bs)); | |
e3442099 | 1639 | goto out; |
025ccaa7 | 1640 | } |
666daa68 | 1641 | } |
92d48558 | 1642 | |
3b5276b5 | 1643 | bdrv_close(bs); |
e3442099 SH |
1644 | |
1645 | out: | |
1646 | aio_context_release(aio_context); | |
666daa68 MA |
1647 | } |
1648 | ||
c245b6a3 | 1649 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
666daa68 | 1650 | { |
6007cdd4 | 1651 | BlockBackend *blk; |
666daa68 | 1652 | |
6007cdd4 MA |
1653 | blk = blk_by_name(device); |
1654 | if (!blk) { | |
c245b6a3 LC |
1655 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1656 | return; | |
92d48558 LC |
1657 | } |
1658 | ||
6007cdd4 | 1659 | eject_device(blk, force, errp); |
666daa68 MA |
1660 | } |
1661 | ||
12d3ba82 BC |
1662 | void qmp_block_passwd(bool has_device, const char *device, |
1663 | bool has_node_name, const char *node_name, | |
1664 | const char *password, Error **errp) | |
666daa68 | 1665 | { |
12d3ba82 | 1666 | Error *local_err = NULL; |
666daa68 | 1667 | BlockDriverState *bs; |
e3442099 | 1668 | AioContext *aio_context; |
666daa68 MA |
1669 | int err; |
1670 | ||
12d3ba82 BC |
1671 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
1672 | has_node_name ? node_name : NULL, | |
1673 | &local_err); | |
84d18f06 | 1674 | if (local_err) { |
12d3ba82 | 1675 | error_propagate(errp, local_err); |
a4dea8a9 | 1676 | return; |
666daa68 MA |
1677 | } |
1678 | ||
e3442099 SH |
1679 | aio_context = bdrv_get_aio_context(bs); |
1680 | aio_context_acquire(aio_context); | |
1681 | ||
a4dea8a9 | 1682 | err = bdrv_set_key(bs, password); |
666daa68 | 1683 | if (err == -EINVAL) { |
a4dea8a9 | 1684 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
e3442099 | 1685 | goto out; |
666daa68 | 1686 | } else if (err < 0) { |
a4dea8a9 | 1687 | error_set(errp, QERR_INVALID_PASSWORD); |
e3442099 | 1688 | goto out; |
666daa68 | 1689 | } |
e3442099 SH |
1690 | |
1691 | out: | |
1692 | aio_context_release(aio_context); | |
666daa68 MA |
1693 | } |
1694 | ||
e3442099 | 1695 | /* Assumes AioContext is held */ |
333a96ec LC |
1696 | static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, |
1697 | int bdrv_flags, BlockDriver *drv, | |
1698 | const char *password, Error **errp) | |
1699 | { | |
34b5d2c6 | 1700 | Error *local_err = NULL; |
0eef407c LC |
1701 | int ret; |
1702 | ||
ddf5636d | 1703 | ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err); |
0eef407c | 1704 | if (ret < 0) { |
34b5d2c6 | 1705 | error_propagate(errp, local_err); |
333a96ec LC |
1706 | return; |
1707 | } | |
1708 | ||
1709 | if (bdrv_key_required(bs)) { | |
1710 | if (password) { | |
1711 | if (bdrv_set_key(bs, password) < 0) { | |
1712 | error_set(errp, QERR_INVALID_PASSWORD); | |
1713 | } | |
1714 | } else { | |
1715 | error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), | |
1716 | bdrv_get_encrypted_filename(bs)); | |
1717 | } | |
1718 | } else if (password) { | |
1719 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | void qmp_change_blockdev(const char *device, const char *filename, | |
314f7ea7 | 1724 | const char *format, Error **errp) |
666daa68 | 1725 | { |
6007cdd4 | 1726 | BlockBackend *blk; |
666daa68 | 1727 | BlockDriverState *bs; |
e3442099 | 1728 | AioContext *aio_context; |
666daa68 MA |
1729 | BlockDriver *drv = NULL; |
1730 | int bdrv_flags; | |
92d48558 | 1731 | Error *err = NULL; |
666daa68 | 1732 | |
6007cdd4 MA |
1733 | blk = blk_by_name(device); |
1734 | if (!blk) { | |
333a96ec LC |
1735 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1736 | return; | |
666daa68 | 1737 | } |
6007cdd4 | 1738 | bs = blk_bs(blk); |
333a96ec | 1739 | |
e3442099 SH |
1740 | aio_context = bdrv_get_aio_context(bs); |
1741 | aio_context_acquire(aio_context); | |
1742 | ||
333a96ec | 1743 | if (format) { |
b64ec4e4 | 1744 | drv = bdrv_find_whitelisted_format(format, bs->read_only); |
666daa68 | 1745 | if (!drv) { |
333a96ec | 1746 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
e3442099 | 1747 | goto out; |
666daa68 MA |
1748 | } |
1749 | } | |
333a96ec | 1750 | |
6007cdd4 | 1751 | eject_device(blk, 0, &err); |
84d18f06 | 1752 | if (err) { |
333a96ec | 1753 | error_propagate(errp, err); |
e3442099 | 1754 | goto out; |
666daa68 | 1755 | } |
333a96ec | 1756 | |
528f7663 | 1757 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
199630b6 | 1758 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
333a96ec LC |
1759 | |
1760 | qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); | |
e3442099 SH |
1761 | |
1762 | out: | |
1763 | aio_context_release(aio_context); | |
666daa68 | 1764 | } |
9063f814 | 1765 | |
727f005e | 1766 | /* throttling disk I/O limits */ |
80047da5 | 1767 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
3e9fab69 BC |
1768 | int64_t bps_wr, |
1769 | int64_t iops, | |
1770 | int64_t iops_rd, | |
1771 | int64_t iops_wr, | |
1772 | bool has_bps_max, | |
1773 | int64_t bps_max, | |
1774 | bool has_bps_rd_max, | |
1775 | int64_t bps_rd_max, | |
1776 | bool has_bps_wr_max, | |
1777 | int64_t bps_wr_max, | |
1778 | bool has_iops_max, | |
1779 | int64_t iops_max, | |
1780 | bool has_iops_rd_max, | |
1781 | int64_t iops_rd_max, | |
1782 | bool has_iops_wr_max, | |
2024c1df BC |
1783 | int64_t iops_wr_max, |
1784 | bool has_iops_size, | |
1785 | int64_t iops_size, Error **errp) | |
727f005e | 1786 | { |
cc0681c4 | 1787 | ThrottleConfig cfg; |
727f005e | 1788 | BlockDriverState *bs; |
b15446fd | 1789 | AioContext *aio_context; |
727f005e | 1790 | |
80047da5 | 1791 | bs = bdrv_find(device); |
727f005e | 1792 | if (!bs) { |
80047da5 LC |
1793 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1794 | return; | |
727f005e ZYW |
1795 | } |
1796 | ||
cc0681c4 BC |
1797 | memset(&cfg, 0, sizeof(cfg)); |
1798 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; | |
1799 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; | |
1800 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; | |
1801 | ||
1802 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; | |
1803 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; | |
1804 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; | |
1805 | ||
3e9fab69 BC |
1806 | if (has_bps_max) { |
1807 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; | |
1808 | } | |
1809 | if (has_bps_rd_max) { | |
1810 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; | |
1811 | } | |
1812 | if (has_bps_wr_max) { | |
1813 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; | |
1814 | } | |
1815 | if (has_iops_max) { | |
1816 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; | |
1817 | } | |
1818 | if (has_iops_rd_max) { | |
1819 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; | |
1820 | } | |
1821 | if (has_iops_wr_max) { | |
1822 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; | |
1823 | } | |
727f005e | 1824 | |
2024c1df BC |
1825 | if (has_iops_size) { |
1826 | cfg.op_size = iops_size; | |
1827 | } | |
cc0681c4 BC |
1828 | |
1829 | if (!check_throttle_config(&cfg, errp)) { | |
80047da5 | 1830 | return; |
727f005e ZYW |
1831 | } |
1832 | ||
b15446fd SH |
1833 | aio_context = bdrv_get_aio_context(bs); |
1834 | aio_context_acquire(aio_context); | |
1835 | ||
cc0681c4 | 1836 | if (!bs->io_limits_enabled && throttle_enabled(&cfg)) { |
727f005e | 1837 | bdrv_io_limits_enable(bs); |
cc0681c4 | 1838 | } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) { |
727f005e | 1839 | bdrv_io_limits_disable(bs); |
cc0681c4 BC |
1840 | } |
1841 | ||
1842 | if (bs->io_limits_enabled) { | |
1843 | bdrv_set_io_limits(bs, &cfg); | |
727f005e | 1844 | } |
b15446fd SH |
1845 | |
1846 | aio_context_release(aio_context); | |
727f005e ZYW |
1847 | } |
1848 | ||
9063f814 RH |
1849 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1850 | { | |
1851 | const char *id = qdict_get_str(qdict, "id"); | |
7e7d56d9 | 1852 | BlockBackend *blk; |
9063f814 | 1853 | BlockDriverState *bs; |
8ad4202b | 1854 | AioContext *aio_context; |
3718d8ab | 1855 | Error *local_err = NULL; |
9063f814 | 1856 | |
7e7d56d9 MA |
1857 | blk = blk_by_name(id); |
1858 | if (!blk) { | |
b1422f20 | 1859 | error_report("Device '%s' not found", id); |
9063f814 RH |
1860 | return -1; |
1861 | } | |
7e7d56d9 | 1862 | bs = blk_bs(blk); |
8ad4202b | 1863 | |
26f8b3a8 | 1864 | if (!blk_legacy_dinfo(blk)) { |
48f364dd MA |
1865 | error_report("Deleting device added with blockdev-add" |
1866 | " is not supported"); | |
1867 | return -1; | |
1868 | } | |
1869 | ||
8ad4202b SH |
1870 | aio_context = bdrv_get_aio_context(bs); |
1871 | aio_context_acquire(aio_context); | |
1872 | ||
3718d8ab FZ |
1873 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { |
1874 | error_report("%s", error_get_pretty(local_err)); | |
1875 | error_free(local_err); | |
8ad4202b | 1876 | aio_context_release(aio_context); |
8591675f MT |
1877 | return -1; |
1878 | } | |
9063f814 RH |
1879 | |
1880 | /* quiesce block driver; prevent further io */ | |
922453bc | 1881 | bdrv_drain_all(); |
9063f814 RH |
1882 | bdrv_flush(bs); |
1883 | bdrv_close(bs); | |
1884 | ||
fa879d62 | 1885 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
1886 | * then we need to make the drive anonymous until the device |
1887 | * can be removed. If this is a drive with no device backing | |
1888 | * then we can just get rid of the block driver state right here. | |
1889 | */ | |
a7f53e26 | 1890 | if (blk_get_attached_dev(blk)) { |
7e7d56d9 | 1891 | blk_hide_on_behalf_of_do_drive_del(blk); |
293c51a6 SH |
1892 | /* Further I/O must not pause the guest */ |
1893 | bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, | |
1894 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 | 1895 | } else { |
b9fe8a7a | 1896 | blk_unref(blk); |
9063f814 RH |
1897 | } |
1898 | ||
8ad4202b | 1899 | aio_context_release(aio_context); |
9063f814 RH |
1900 | return 0; |
1901 | } | |
6d4a2b3a | 1902 | |
3b1dbd11 BC |
1903 | void qmp_block_resize(bool has_device, const char *device, |
1904 | bool has_node_name, const char *node_name, | |
1905 | int64_t size, Error **errp) | |
6d4a2b3a | 1906 | { |
3b1dbd11 | 1907 | Error *local_err = NULL; |
6d4a2b3a | 1908 | BlockDriverState *bs; |
927e0e76 | 1909 | AioContext *aio_context; |
8732901e | 1910 | int ret; |
6d4a2b3a | 1911 | |
3b1dbd11 BC |
1912 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
1913 | has_node_name ? node_name : NULL, | |
1914 | &local_err); | |
84d18f06 | 1915 | if (local_err) { |
3b1dbd11 BC |
1916 | error_propagate(errp, local_err); |
1917 | return; | |
1918 | } | |
1919 | ||
927e0e76 SH |
1920 | aio_context = bdrv_get_aio_context(bs); |
1921 | aio_context_acquire(aio_context); | |
1922 | ||
3b1dbd11 BC |
1923 | if (!bdrv_is_first_non_filter(bs)) { |
1924 | error_set(errp, QERR_FEATURE_DISABLED, "resize"); | |
927e0e76 | 1925 | goto out; |
6d4a2b3a CH |
1926 | } |
1927 | ||
1928 | if (size < 0) { | |
939a1cc3 | 1929 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 1930 | goto out; |
6d4a2b3a CH |
1931 | } |
1932 | ||
9c75e168 JC |
1933 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
1934 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
927e0e76 | 1935 | goto out; |
9c75e168 JC |
1936 | } |
1937 | ||
92b7a08d PL |
1938 | /* complete all in-flight operations before resizing the device */ |
1939 | bdrv_drain_all(); | |
1940 | ||
8732901e KW |
1941 | ret = bdrv_truncate(bs, size); |
1942 | switch (ret) { | |
939a1cc3 SH |
1943 | case 0: |
1944 | break; | |
1945 | case -ENOMEDIUM: | |
1946 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1947 | break; | |
1948 | case -ENOTSUP: | |
1949 | error_set(errp, QERR_UNSUPPORTED); | |
1950 | break; | |
1951 | case -EACCES: | |
1952 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1953 | break; | |
1954 | case -EBUSY: | |
1955 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
1956 | break; | |
1957 | default: | |
8732901e | 1958 | error_setg_errno(errp, -ret, "Could not resize"); |
939a1cc3 | 1959 | break; |
6d4a2b3a | 1960 | } |
927e0e76 SH |
1961 | |
1962 | out: | |
1963 | aio_context_release(aio_context); | |
6d4a2b3a | 1964 | } |
12bd451f | 1965 | |
9abf2dba | 1966 | static void block_job_cb(void *opaque, int ret) |
12bd451f | 1967 | { |
723c5d93 SH |
1968 | /* Note that this function may be executed from another AioContext besides |
1969 | * the QEMU main loop. If you need to access anything that assumes the | |
1970 | * QEMU global mutex, use a BH or introduce a mutex. | |
1971 | */ | |
1972 | ||
12bd451f | 1973 | BlockDriverState *bs = opaque; |
bcada37b | 1974 | const char *msg = NULL; |
12bd451f | 1975 | |
9abf2dba | 1976 | trace_block_job_cb(bs, bs->job, ret); |
12bd451f SH |
1977 | |
1978 | assert(bs->job); | |
bcada37b | 1979 | |
12bd451f | 1980 | if (ret < 0) { |
bcada37b | 1981 | msg = strerror(-ret); |
12bd451f SH |
1982 | } |
1983 | ||
370521a1 | 1984 | if (block_job_is_cancelled(bs->job)) { |
bcada37b | 1985 | block_job_event_cancelled(bs->job); |
370521a1 | 1986 | } else { |
bcada37b | 1987 | block_job_event_completed(bs->job, msg); |
370521a1 | 1988 | } |
aa398a5c | 1989 | |
fa510ebf | 1990 | bdrv_put_ref_bh_schedule(bs); |
12bd451f SH |
1991 | } |
1992 | ||
13d8cc51 JC |
1993 | void qmp_block_stream(const char *device, |
1994 | bool has_base, const char *base, | |
1995 | bool has_backing_file, const char *backing_file, | |
1996 | bool has_speed, int64_t speed, | |
1d809098 PB |
1997 | bool has_on_error, BlockdevOnError on_error, |
1998 | Error **errp) | |
12bd451f SH |
1999 | { |
2000 | BlockDriverState *bs; | |
c8c3080f | 2001 | BlockDriverState *base_bs = NULL; |
f3e69beb | 2002 | AioContext *aio_context; |
fd7f8c65 | 2003 | Error *local_err = NULL; |
13d8cc51 | 2004 | const char *base_name = NULL; |
12bd451f | 2005 | |
1d809098 PB |
2006 | if (!has_on_error) { |
2007 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2008 | } | |
2009 | ||
12bd451f SH |
2010 | bs = bdrv_find(device); |
2011 | if (!bs) { | |
2012 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2013 | return; | |
2014 | } | |
2015 | ||
f3e69beb SH |
2016 | aio_context = bdrv_get_aio_context(bs); |
2017 | aio_context_acquire(aio_context); | |
2018 | ||
628ff683 | 2019 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { |
f3e69beb | 2020 | goto out; |
628ff683 FZ |
2021 | } |
2022 | ||
13d8cc51 | 2023 | if (has_base) { |
c8c3080f MT |
2024 | base_bs = bdrv_find_backing_image(bs, base); |
2025 | if (base_bs == NULL) { | |
2026 | error_set(errp, QERR_BASE_NOT_FOUND, base); | |
f3e69beb | 2027 | goto out; |
c8c3080f | 2028 | } |
f3e69beb | 2029 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
13d8cc51 | 2030 | base_name = base; |
12bd451f SH |
2031 | } |
2032 | ||
13d8cc51 JC |
2033 | /* if we are streaming the entire chain, the result will have no backing |
2034 | * file, and specifying one is therefore an error */ | |
2035 | if (base_bs == NULL && has_backing_file) { | |
2036 | error_setg(errp, "backing file specified, but streaming the " | |
2037 | "entire chain"); | |
f3e69beb | 2038 | goto out; |
13d8cc51 JC |
2039 | } |
2040 | ||
2041 | /* backing_file string overrides base bs filename */ | |
2042 | base_name = has_backing_file ? backing_file : base_name; | |
2043 | ||
2044 | stream_start(bs, base_bs, base_name, has_speed ? speed : 0, | |
1d809098 | 2045 | on_error, block_job_cb, bs, &local_err); |
84d18f06 | 2046 | if (local_err) { |
fd7f8c65 | 2047 | error_propagate(errp, local_err); |
f3e69beb | 2048 | goto out; |
12bd451f SH |
2049 | } |
2050 | ||
2051 | trace_qmp_block_stream(bs, bs->job); | |
f3e69beb SH |
2052 | |
2053 | out: | |
2054 | aio_context_release(aio_context); | |
12bd451f | 2055 | } |
2d47c6e9 | 2056 | |
ed61fc10 | 2057 | void qmp_block_commit(const char *device, |
7676e2c5 JC |
2058 | bool has_base, const char *base, |
2059 | bool has_top, const char *top, | |
54e26900 | 2060 | bool has_backing_file, const char *backing_file, |
ed61fc10 JC |
2061 | bool has_speed, int64_t speed, |
2062 | Error **errp) | |
2063 | { | |
2064 | BlockDriverState *bs; | |
2065 | BlockDriverState *base_bs, *top_bs; | |
9e85cd5c | 2066 | AioContext *aio_context; |
ed61fc10 JC |
2067 | Error *local_err = NULL; |
2068 | /* This will be part of the QMP command, if/when the | |
2069 | * BlockdevOnError change for blkmirror makes it in | |
2070 | */ | |
92aa5c6d | 2071 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 | 2072 | |
54504663 HR |
2073 | if (!has_speed) { |
2074 | speed = 0; | |
2075 | } | |
2076 | ||
7676e2c5 JC |
2077 | /* Important Note: |
2078 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2079 | * live commit feature versions; for this to work, we must make sure to | |
2080 | * perform the device lookup before any generic errors that may occur in a | |
2081 | * scenario in which all optional arguments are omitted. */ | |
ed61fc10 JC |
2082 | bs = bdrv_find(device); |
2083 | if (!bs) { | |
2084 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2085 | return; | |
628ff683 FZ |
2086 | } |
2087 | ||
9e85cd5c SH |
2088 | aio_context = bdrv_get_aio_context(bs); |
2089 | aio_context_acquire(aio_context); | |
2090 | ||
2091 | /* drain all i/o before commits */ | |
2092 | bdrv_drain_all(); | |
2093 | ||
628ff683 | 2094 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) { |
9e85cd5c | 2095 | goto out; |
ed61fc10 | 2096 | } |
ed61fc10 JC |
2097 | |
2098 | /* default top_bs is the active layer */ | |
2099 | top_bs = bs; | |
2100 | ||
7676e2c5 | 2101 | if (has_top && top) { |
ed61fc10 JC |
2102 | if (strcmp(bs->filename, top) != 0) { |
2103 | top_bs = bdrv_find_backing_image(bs, top); | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | if (top_bs == NULL) { | |
2108 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 2109 | goto out; |
ed61fc10 JC |
2110 | } |
2111 | ||
9e85cd5c SH |
2112 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
2113 | ||
d5208c45 JC |
2114 | if (has_base && base) { |
2115 | base_bs = bdrv_find_backing_image(top_bs, base); | |
2116 | } else { | |
2117 | base_bs = bdrv_find_base(top_bs); | |
2118 | } | |
2119 | ||
2120 | if (base_bs == NULL) { | |
2121 | error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); | |
9e85cd5c | 2122 | goto out; |
d5208c45 JC |
2123 | } |
2124 | ||
9e85cd5c SH |
2125 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
2126 | ||
7676e2c5 JC |
2127 | /* Do not allow attempts to commit an image into itself */ |
2128 | if (top_bs == base_bs) { | |
2129 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 2130 | goto out; |
7676e2c5 JC |
2131 | } |
2132 | ||
20a63d2c | 2133 | if (top_bs == bs) { |
54e26900 JC |
2134 | if (has_backing_file) { |
2135 | error_setg(errp, "'backing-file' specified," | |
2136 | " but 'top' is the active layer"); | |
9e85cd5c | 2137 | goto out; |
54e26900 | 2138 | } |
20a63d2c FZ |
2139 | commit_active_start(bs, base_bs, speed, on_error, block_job_cb, |
2140 | bs, &local_err); | |
2141 | } else { | |
2142 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, | |
54e26900 | 2143 | has_backing_file ? backing_file : NULL, &local_err); |
20a63d2c | 2144 | } |
ed61fc10 JC |
2145 | if (local_err != NULL) { |
2146 | error_propagate(errp, local_err); | |
9e85cd5c | 2147 | goto out; |
ed61fc10 | 2148 | } |
9e85cd5c SH |
2149 | |
2150 | out: | |
2151 | aio_context_release(aio_context); | |
ed61fc10 JC |
2152 | } |
2153 | ||
99a9addf SH |
2154 | void qmp_drive_backup(const char *device, const char *target, |
2155 | bool has_format, const char *format, | |
b53169ea | 2156 | enum MirrorSyncMode sync, |
99a9addf SH |
2157 | bool has_mode, enum NewImageMode mode, |
2158 | bool has_speed, int64_t speed, | |
2159 | bool has_on_source_error, BlockdevOnError on_source_error, | |
2160 | bool has_on_target_error, BlockdevOnError on_target_error, | |
2161 | Error **errp) | |
2162 | { | |
2163 | BlockDriverState *bs; | |
2164 | BlockDriverState *target_bs; | |
fc5d3f84 | 2165 | BlockDriverState *source = NULL; |
761731b1 | 2166 | AioContext *aio_context; |
99a9addf SH |
2167 | BlockDriver *drv = NULL; |
2168 | Error *local_err = NULL; | |
2169 | int flags; | |
2170 | int64_t size; | |
2171 | int ret; | |
2172 | ||
2173 | if (!has_speed) { | |
2174 | speed = 0; | |
2175 | } | |
2176 | if (!has_on_source_error) { | |
2177 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2178 | } | |
2179 | if (!has_on_target_error) { | |
2180 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2181 | } | |
2182 | if (!has_mode) { | |
2183 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2184 | } | |
2185 | ||
2186 | bs = bdrv_find(device); | |
2187 | if (!bs) { | |
2188 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2189 | return; | |
2190 | } | |
2191 | ||
761731b1 SH |
2192 | aio_context = bdrv_get_aio_context(bs); |
2193 | aio_context_acquire(aio_context); | |
2194 | ||
99a9addf SH |
2195 | if (!bdrv_is_inserted(bs)) { |
2196 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
761731b1 | 2197 | goto out; |
99a9addf SH |
2198 | } |
2199 | ||
2200 | if (!has_format) { | |
2201 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2202 | } | |
2203 | if (format) { | |
2204 | drv = bdrv_find_format(format); | |
2205 | if (!drv) { | |
2206 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
761731b1 | 2207 | goto out; |
99a9addf SH |
2208 | } |
2209 | } | |
2210 | ||
3718d8ab | 2211 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
761731b1 | 2212 | goto out; |
99a9addf SH |
2213 | } |
2214 | ||
2215 | flags = bs->open_flags | BDRV_O_RDWR; | |
2216 | ||
fc5d3f84 IM |
2217 | /* See if we have a backing HD we can use to create our new image |
2218 | * on top of. */ | |
2219 | if (sync == MIRROR_SYNC_MODE_TOP) { | |
2220 | source = bs->backing_hd; | |
2221 | if (!source) { | |
2222 | sync = MIRROR_SYNC_MODE_FULL; | |
2223 | } | |
2224 | } | |
2225 | if (sync == MIRROR_SYNC_MODE_NONE) { | |
2226 | source = bs; | |
2227 | } | |
2228 | ||
99a9addf SH |
2229 | size = bdrv_getlength(bs); |
2230 | if (size < 0) { | |
2231 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
761731b1 | 2232 | goto out; |
99a9addf SH |
2233 | } |
2234 | ||
2235 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
2236 | assert(format && drv); | |
fc5d3f84 IM |
2237 | if (source) { |
2238 | bdrv_img_create(target, format, source->filename, | |
2239 | source->drv->format_name, NULL, | |
2240 | size, flags, &local_err, false); | |
2241 | } else { | |
2242 | bdrv_img_create(target, format, NULL, NULL, NULL, | |
2243 | size, flags, &local_err, false); | |
2244 | } | |
99a9addf SH |
2245 | } |
2246 | ||
84d18f06 | 2247 | if (local_err) { |
99a9addf | 2248 | error_propagate(errp, local_err); |
761731b1 | 2249 | goto out; |
99a9addf SH |
2250 | } |
2251 | ||
f67503e5 | 2252 | target_bs = NULL; |
ddf5636d | 2253 | ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err); |
99a9addf | 2254 | if (ret < 0) { |
34b5d2c6 | 2255 | error_propagate(errp, local_err); |
761731b1 | 2256 | goto out; |
99a9addf SH |
2257 | } |
2258 | ||
761731b1 SH |
2259 | bdrv_set_aio_context(target_bs, aio_context); |
2260 | ||
fc5d3f84 | 2261 | backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error, |
99a9addf SH |
2262 | block_job_cb, bs, &local_err); |
2263 | if (local_err != NULL) { | |
4f6fd349 | 2264 | bdrv_unref(target_bs); |
99a9addf | 2265 | error_propagate(errp, local_err); |
761731b1 | 2266 | goto out; |
99a9addf | 2267 | } |
761731b1 SH |
2268 | |
2269 | out: | |
2270 | aio_context_release(aio_context); | |
99a9addf SH |
2271 | } |
2272 | ||
c13163fb BC |
2273 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
2274 | { | |
2275 | return bdrv_named_nodes_list(); | |
2276 | } | |
2277 | ||
08e4ed6c PB |
2278 | #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) |
2279 | ||
d9b902db PB |
2280 | void qmp_drive_mirror(const char *device, const char *target, |
2281 | bool has_format, const char *format, | |
4c828dc6 | 2282 | bool has_node_name, const char *node_name, |
09158f00 | 2283 | bool has_replaces, const char *replaces, |
d9b902db PB |
2284 | enum MirrorSyncMode sync, |
2285 | bool has_mode, enum NewImageMode mode, | |
b952b558 | 2286 | bool has_speed, int64_t speed, |
eee13dfe | 2287 | bool has_granularity, uint32_t granularity, |
08e4ed6c | 2288 | bool has_buf_size, int64_t buf_size, |
b952b558 PB |
2289 | bool has_on_source_error, BlockdevOnError on_source_error, |
2290 | bool has_on_target_error, BlockdevOnError on_target_error, | |
2291 | Error **errp) | |
d9b902db | 2292 | { |
d9b902db PB |
2293 | BlockDriverState *bs; |
2294 | BlockDriverState *source, *target_bs; | |
5a7e7a0b | 2295 | AioContext *aio_context; |
d9b902db PB |
2296 | BlockDriver *drv = NULL; |
2297 | Error *local_err = NULL; | |
4c828dc6 | 2298 | QDict *options = NULL; |
d9b902db | 2299 | int flags; |
ac3c5d83 | 2300 | int64_t size; |
d9b902db PB |
2301 | int ret; |
2302 | ||
2303 | if (!has_speed) { | |
2304 | speed = 0; | |
2305 | } | |
b952b558 PB |
2306 | if (!has_on_source_error) { |
2307 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2308 | } | |
2309 | if (!has_on_target_error) { | |
2310 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2311 | } | |
d9b902db PB |
2312 | if (!has_mode) { |
2313 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2314 | } | |
eee13dfe PB |
2315 | if (!has_granularity) { |
2316 | granularity = 0; | |
2317 | } | |
08e4ed6c PB |
2318 | if (!has_buf_size) { |
2319 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
2320 | } | |
2321 | ||
eee13dfe | 2322 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
3cbbe9fd SH |
2323 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
2324 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
2325 | return; |
2326 | } | |
2327 | if (granularity & (granularity - 1)) { | |
3cbbe9fd | 2328 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2"); |
eee13dfe PB |
2329 | return; |
2330 | } | |
d9b902db PB |
2331 | |
2332 | bs = bdrv_find(device); | |
2333 | if (!bs) { | |
2334 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2335 | return; | |
2336 | } | |
2337 | ||
5a7e7a0b SH |
2338 | aio_context = bdrv_get_aio_context(bs); |
2339 | aio_context_acquire(aio_context); | |
2340 | ||
d9b902db PB |
2341 | if (!bdrv_is_inserted(bs)) { |
2342 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
5a7e7a0b | 2343 | goto out; |
d9b902db PB |
2344 | } |
2345 | ||
2346 | if (!has_format) { | |
2347 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2348 | } | |
2349 | if (format) { | |
2350 | drv = bdrv_find_format(format); | |
2351 | if (!drv) { | |
2352 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
5a7e7a0b | 2353 | goto out; |
d9b902db PB |
2354 | } |
2355 | } | |
2356 | ||
3718d8ab | 2357 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { |
5a7e7a0b | 2358 | goto out; |
d9b902db PB |
2359 | } |
2360 | ||
2361 | flags = bs->open_flags | BDRV_O_RDWR; | |
2362 | source = bs->backing_hd; | |
2363 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { | |
2364 | sync = MIRROR_SYNC_MODE_FULL; | |
2365 | } | |
117e0c82 HR |
2366 | if (sync == MIRROR_SYNC_MODE_NONE) { |
2367 | source = bs; | |
2368 | } | |
d9b902db | 2369 | |
ac3c5d83 SH |
2370 | size = bdrv_getlength(bs); |
2371 | if (size < 0) { | |
2372 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 2373 | goto out; |
ac3c5d83 SH |
2374 | } |
2375 | ||
09158f00 BC |
2376 | if (has_replaces) { |
2377 | BlockDriverState *to_replace_bs; | |
5a7e7a0b SH |
2378 | AioContext *replace_aio_context; |
2379 | int64_t replace_size; | |
09158f00 BC |
2380 | |
2381 | if (!has_node_name) { | |
2382 | error_setg(errp, "a node-name must be provided when replacing a" | |
2383 | " named node of the graph"); | |
5a7e7a0b | 2384 | goto out; |
09158f00 BC |
2385 | } |
2386 | ||
2387 | to_replace_bs = check_to_replace_node(replaces, &local_err); | |
2388 | ||
2389 | if (!to_replace_bs) { | |
2390 | error_propagate(errp, local_err); | |
5a7e7a0b | 2391 | goto out; |
09158f00 BC |
2392 | } |
2393 | ||
5a7e7a0b SH |
2394 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); |
2395 | aio_context_acquire(replace_aio_context); | |
2396 | replace_size = bdrv_getlength(to_replace_bs); | |
2397 | aio_context_release(replace_aio_context); | |
2398 | ||
2399 | if (size != replace_size) { | |
09158f00 BC |
2400 | error_setg(errp, "cannot replace image with a mirror image of " |
2401 | "different size"); | |
5a7e7a0b | 2402 | goto out; |
09158f00 BC |
2403 | } |
2404 | } | |
2405 | ||
14526864 HR |
2406 | if ((sync == MIRROR_SYNC_MODE_FULL || !source) |
2407 | && mode != NEW_IMAGE_MODE_EXISTING) | |
2408 | { | |
d9b902db PB |
2409 | /* create new image w/o backing file */ |
2410 | assert(format && drv); | |
cf8f2426 | 2411 | bdrv_img_create(target, format, |
f382d43a | 2412 | NULL, NULL, NULL, size, flags, &local_err, false); |
d9b902db PB |
2413 | } else { |
2414 | switch (mode) { | |
2415 | case NEW_IMAGE_MODE_EXISTING: | |
d9b902db PB |
2416 | break; |
2417 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
2418 | /* create new image with backing file */ | |
cf8f2426 LC |
2419 | bdrv_img_create(target, format, |
2420 | source->filename, | |
2421 | source->drv->format_name, | |
f382d43a | 2422 | NULL, size, flags, &local_err, false); |
d9b902db PB |
2423 | break; |
2424 | default: | |
2425 | abort(); | |
2426 | } | |
2427 | } | |
2428 | ||
84d18f06 | 2429 | if (local_err) { |
cf8f2426 | 2430 | error_propagate(errp, local_err); |
5a7e7a0b | 2431 | goto out; |
d9b902db PB |
2432 | } |
2433 | ||
4c828dc6 BC |
2434 | if (has_node_name) { |
2435 | options = qdict_new(); | |
2436 | qdict_put(options, "node-name", qstring_from_str(node_name)); | |
2437 | } | |
2438 | ||
b812f671 PB |
2439 | /* Mirroring takes care of copy-on-write using the source's backing |
2440 | * file. | |
2441 | */ | |
f67503e5 | 2442 | target_bs = NULL; |
4c828dc6 BC |
2443 | ret = bdrv_open(&target_bs, target, NULL, options, |
2444 | flags | BDRV_O_NO_BACKING, drv, &local_err); | |
d9b902db | 2445 | if (ret < 0) { |
34b5d2c6 | 2446 | error_propagate(errp, local_err); |
5a7e7a0b | 2447 | goto out; |
d9b902db PB |
2448 | } |
2449 | ||
5a7e7a0b SH |
2450 | bdrv_set_aio_context(target_bs, aio_context); |
2451 | ||
09158f00 BC |
2452 | /* pass the node name to replace to mirror start since it's loose coupling |
2453 | * and will allow to check whether the node still exist at mirror completion | |
2454 | */ | |
2455 | mirror_start(bs, target_bs, | |
2456 | has_replaces ? replaces : NULL, | |
2457 | speed, granularity, buf_size, sync, | |
eee13dfe | 2458 | on_source_error, on_target_error, |
b952b558 | 2459 | block_job_cb, bs, &local_err); |
d9b902db | 2460 | if (local_err != NULL) { |
4f6fd349 | 2461 | bdrv_unref(target_bs); |
d9b902db | 2462 | error_propagate(errp, local_err); |
5a7e7a0b | 2463 | goto out; |
d9b902db | 2464 | } |
5a7e7a0b SH |
2465 | |
2466 | out: | |
2467 | aio_context_release(aio_context); | |
d9b902db PB |
2468 | } |
2469 | ||
3d948cdf SH |
2470 | /* Get the block job for a given device name and acquire its AioContext */ |
2471 | static BlockJob *find_block_job(const char *device, AioContext **aio_context) | |
2d47c6e9 SH |
2472 | { |
2473 | BlockDriverState *bs; | |
2474 | ||
2475 | bs = bdrv_find(device); | |
3d948cdf SH |
2476 | if (!bs) { |
2477 | goto notfound; | |
2478 | } | |
2479 | ||
2480 | *aio_context = bdrv_get_aio_context(bs); | |
2481 | aio_context_acquire(*aio_context); | |
2482 | ||
2483 | if (!bs->job) { | |
2484 | aio_context_release(*aio_context); | |
2485 | goto notfound; | |
2d47c6e9 | 2486 | } |
3d948cdf | 2487 | |
2d47c6e9 | 2488 | return bs->job; |
3d948cdf SH |
2489 | |
2490 | notfound: | |
2491 | *aio_context = NULL; | |
2492 | return NULL; | |
2d47c6e9 SH |
2493 | } |
2494 | ||
882ec7ce | 2495 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 2496 | { |
3d948cdf SH |
2497 | AioContext *aio_context; |
2498 | BlockJob *job = find_block_job(device, &aio_context); | |
2d47c6e9 SH |
2499 | |
2500 | if (!job) { | |
7ef15070 | 2501 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
2d47c6e9 SH |
2502 | return; |
2503 | } | |
2504 | ||
882ec7ce | 2505 | block_job_set_speed(job, speed, errp); |
3d948cdf | 2506 | aio_context_release(aio_context); |
2d47c6e9 | 2507 | } |
370521a1 | 2508 | |
6e37fb81 PB |
2509 | void qmp_block_job_cancel(const char *device, |
2510 | bool has_force, bool force, Error **errp) | |
370521a1 | 2511 | { |
3d948cdf SH |
2512 | AioContext *aio_context; |
2513 | BlockJob *job = find_block_job(device, &aio_context); | |
6e37fb81 | 2514 | |
370521a1 | 2515 | if (!job) { |
7ef15070 | 2516 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
370521a1 SH |
2517 | return; |
2518 | } | |
3d948cdf SH |
2519 | |
2520 | if (!has_force) { | |
2521 | force = false; | |
2522 | } | |
2523 | ||
6e37fb81 | 2524 | if (job->paused && !force) { |
f231b88d CR |
2525 | error_setg(errp, "The block job for device '%s' is currently paused", |
2526 | device); | |
3d948cdf | 2527 | goto out; |
8acc72a4 | 2528 | } |
370521a1 SH |
2529 | |
2530 | trace_qmp_block_job_cancel(job); | |
2531 | block_job_cancel(job); | |
3d948cdf SH |
2532 | out: |
2533 | aio_context_release(aio_context); | |
370521a1 | 2534 | } |
fb5458cd | 2535 | |
6e37fb81 PB |
2536 | void qmp_block_job_pause(const char *device, Error **errp) |
2537 | { | |
3d948cdf SH |
2538 | AioContext *aio_context; |
2539 | BlockJob *job = find_block_job(device, &aio_context); | |
6e37fb81 PB |
2540 | |
2541 | if (!job) { | |
2542 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2543 | return; | |
2544 | } | |
2545 | ||
2546 | trace_qmp_block_job_pause(job); | |
2547 | block_job_pause(job); | |
3d948cdf | 2548 | aio_context_release(aio_context); |
6e37fb81 PB |
2549 | } |
2550 | ||
2551 | void qmp_block_job_resume(const char *device, Error **errp) | |
2552 | { | |
3d948cdf SH |
2553 | AioContext *aio_context; |
2554 | BlockJob *job = find_block_job(device, &aio_context); | |
6e37fb81 PB |
2555 | |
2556 | if (!job) { | |
2557 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2558 | return; | |
2559 | } | |
2560 | ||
2561 | trace_qmp_block_job_resume(job); | |
2562 | block_job_resume(job); | |
3d948cdf | 2563 | aio_context_release(aio_context); |
6e37fb81 PB |
2564 | } |
2565 | ||
aeae883b PB |
2566 | void qmp_block_job_complete(const char *device, Error **errp) |
2567 | { | |
3d948cdf SH |
2568 | AioContext *aio_context; |
2569 | BlockJob *job = find_block_job(device, &aio_context); | |
aeae883b PB |
2570 | |
2571 | if (!job) { | |
2572 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2573 | return; | |
2574 | } | |
2575 | ||
2576 | trace_qmp_block_job_complete(job); | |
2577 | block_job_complete(job, errp); | |
3d948cdf | 2578 | aio_context_release(aio_context); |
aeae883b PB |
2579 | } |
2580 | ||
fa40e656 JC |
2581 | void qmp_change_backing_file(const char *device, |
2582 | const char *image_node_name, | |
2583 | const char *backing_file, | |
2584 | Error **errp) | |
2585 | { | |
2586 | BlockDriverState *bs = NULL; | |
729962f6 | 2587 | AioContext *aio_context; |
fa40e656 JC |
2588 | BlockDriverState *image_bs = NULL; |
2589 | Error *local_err = NULL; | |
2590 | bool ro; | |
2591 | int open_flags; | |
2592 | int ret; | |
2593 | ||
2594 | /* find the top layer BDS of the chain */ | |
2595 | bs = bdrv_find(device); | |
2596 | if (!bs) { | |
2597 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2598 | return; | |
2599 | } | |
2600 | ||
729962f6 SH |
2601 | aio_context = bdrv_get_aio_context(bs); |
2602 | aio_context_acquire(aio_context); | |
2603 | ||
fa40e656 JC |
2604 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
2605 | if (local_err) { | |
2606 | error_propagate(errp, local_err); | |
729962f6 | 2607 | goto out; |
fa40e656 JC |
2608 | } |
2609 | ||
2610 | if (!image_bs) { | |
2611 | error_setg(errp, "image file not found"); | |
729962f6 | 2612 | goto out; |
fa40e656 JC |
2613 | } |
2614 | ||
2615 | if (bdrv_find_base(image_bs) == image_bs) { | |
2616 | error_setg(errp, "not allowing backing file change on an image " | |
2617 | "without a backing file"); | |
729962f6 | 2618 | goto out; |
fa40e656 JC |
2619 | } |
2620 | ||
2621 | /* even though we are not necessarily operating on bs, we need it to | |
2622 | * determine if block ops are currently prohibited on the chain */ | |
2623 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 2624 | goto out; |
fa40e656 JC |
2625 | } |
2626 | ||
2627 | /* final sanity check */ | |
2628 | if (!bdrv_chain_contains(bs, image_bs)) { | |
2629 | error_setg(errp, "'%s' and image file are not in the same chain", | |
2630 | device); | |
729962f6 | 2631 | goto out; |
fa40e656 JC |
2632 | } |
2633 | ||
2634 | /* if not r/w, reopen to make r/w */ | |
2635 | open_flags = image_bs->open_flags; | |
2636 | ro = bdrv_is_read_only(image_bs); | |
2637 | ||
2638 | if (ro) { | |
2639 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
2640 | if (local_err) { | |
2641 | error_propagate(errp, local_err); | |
729962f6 | 2642 | goto out; |
fa40e656 JC |
2643 | } |
2644 | } | |
2645 | ||
2646 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
2647 | image_bs->drv ? image_bs->drv->format_name : ""); | |
2648 | ||
2649 | if (ret < 0) { | |
2650 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
2651 | backing_file); | |
2652 | /* don't exit here, so we can try to restore open flags if | |
2653 | * appropriate */ | |
2654 | } | |
2655 | ||
2656 | if (ro) { | |
2657 | bdrv_reopen(image_bs, open_flags, &local_err); | |
2658 | if (local_err) { | |
2659 | error_propagate(errp, local_err); /* will preserve prior errp */ | |
2660 | } | |
2661 | } | |
729962f6 SH |
2662 | |
2663 | out: | |
2664 | aio_context_release(aio_context); | |
fa40e656 JC |
2665 | } |
2666 | ||
d26c9a15 KW |
2667 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
2668 | { | |
2669 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
18e46a03 | 2670 | BlockBackend *blk; |
d26c9a15 KW |
2671 | QObject *obj; |
2672 | QDict *qdict; | |
d26c9a15 KW |
2673 | Error *local_err = NULL; |
2674 | ||
2675 | /* Require an ID in the top level */ | |
2676 | if (!options->has_id) { | |
2677 | error_setg(errp, "Block device needs an ID"); | |
2678 | goto fail; | |
2679 | } | |
2680 | ||
60e19e06 | 2681 | /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with |
d26c9a15 | 2682 | * cache.direct=false instead of silently switching to aio=threads, except |
60e19e06 | 2683 | * when called from drive_new(). |
d26c9a15 KW |
2684 | * |
2685 | * For now, simply forbidding the combination for all drivers will do. */ | |
2686 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { | |
c6e0bd9b KW |
2687 | bool direct = options->has_cache && |
2688 | options->cache->has_direct && | |
2689 | options->cache->direct; | |
2690 | if (!direct) { | |
d26c9a15 KW |
2691 | error_setg(errp, "aio=native requires cache.direct=true"); |
2692 | goto fail; | |
2693 | } | |
2694 | } | |
2695 | ||
2696 | visit_type_BlockdevOptions(qmp_output_get_visitor(ov), | |
2697 | &options, NULL, &local_err); | |
84d18f06 | 2698 | if (local_err) { |
d26c9a15 KW |
2699 | error_propagate(errp, local_err); |
2700 | goto fail; | |
2701 | } | |
2702 | ||
2703 | obj = qmp_output_get_qobject(ov); | |
2704 | qdict = qobject_to_qdict(obj); | |
2705 | ||
2706 | qdict_flatten(qdict); | |
2707 | ||
18e46a03 | 2708 | blk = blockdev_init(NULL, qdict, &local_err); |
84d18f06 | 2709 | if (local_err) { |
b681072d | 2710 | error_propagate(errp, local_err); |
d26c9a15 KW |
2711 | goto fail; |
2712 | } | |
2713 | ||
18e46a03 | 2714 | if (bdrv_key_required(blk_bs(blk))) { |
18e46a03 | 2715 | blk_unref(blk); |
8ae8e904 KW |
2716 | error_setg(errp, "blockdev-add doesn't support encrypted devices"); |
2717 | goto fail; | |
2718 | } | |
2719 | ||
d26c9a15 KW |
2720 | fail: |
2721 | qmp_output_visitor_cleanup(ov); | |
2722 | } | |
2723 | ||
fea68bb6 | 2724 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 2725 | { |
fea68bb6 MA |
2726 | BlockJobInfoList *head = NULL, **p_next = &head; |
2727 | BlockDriverState *bs; | |
fb5458cd | 2728 | |
fea68bb6 | 2729 | for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { |
69691e72 SH |
2730 | AioContext *aio_context = bdrv_get_aio_context(bs); |
2731 | ||
2732 | aio_context_acquire(aio_context); | |
2733 | ||
fea68bb6 MA |
2734 | if (bs->job) { |
2735 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); | |
2736 | elem->value = block_job_query(bs->job); | |
2737 | *p_next = elem; | |
2738 | p_next = &elem->next; | |
2739 | } | |
69691e72 SH |
2740 | |
2741 | aio_context_release(aio_context); | |
fb5458cd | 2742 | } |
fb5458cd | 2743 | |
fea68bb6 | 2744 | return head; |
fb5458cd | 2745 | } |
4d454574 | 2746 | |
0006383e | 2747 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 2748 | .name = "drive", |
0006383e | 2749 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
2750 | .desc = { |
2751 | { | |
4d454574 PB |
2752 | .name = "snapshot", |
2753 | .type = QEMU_OPT_BOOL, | |
2754 | .help = "enable/disable snapshot mode", | |
a9384aff PB |
2755 | },{ |
2756 | .name = "discard", | |
2757 | .type = QEMU_OPT_STRING, | |
2758 | .help = "discard operation (ignore/off, unmap/on)", | |
4d454574 | 2759 | },{ |
29c4e2b5 KW |
2760 | .name = "cache.writeback", |
2761 | .type = QEMU_OPT_BOOL, | |
2762 | .help = "enables writeback mode for any caches", | |
2763 | },{ | |
2764 | .name = "cache.direct", | |
2765 | .type = QEMU_OPT_BOOL, | |
2766 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
2767 | },{ | |
2768 | .name = "cache.no-flush", | |
2769 | .type = QEMU_OPT_BOOL, | |
2770 | .help = "ignore any flush requests for the device", | |
4d454574 PB |
2771 | },{ |
2772 | .name = "aio", | |
2773 | .type = QEMU_OPT_STRING, | |
2774 | .help = "host AIO implementation (threads, native)", | |
2775 | },{ | |
2776 | .name = "format", | |
2777 | .type = QEMU_OPT_STRING, | |
2778 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
2779 | },{ |
2780 | .name = "rerror", | |
2781 | .type = QEMU_OPT_STRING, | |
2782 | .help = "read error action", | |
2783 | },{ | |
2784 | .name = "werror", | |
2785 | .type = QEMU_OPT_STRING, | |
2786 | .help = "write error action", | |
4d454574 | 2787 | },{ |
0f227a94 | 2788 | .name = "read-only", |
4d454574 PB |
2789 | .type = QEMU_OPT_BOOL, |
2790 | .help = "open drive file as read-only", | |
2791 | },{ | |
57975222 | 2792 | .name = "throttling.iops-total", |
4d454574 PB |
2793 | .type = QEMU_OPT_NUMBER, |
2794 | .help = "limit total I/O operations per second", | |
2795 | },{ | |
57975222 | 2796 | .name = "throttling.iops-read", |
4d454574 PB |
2797 | .type = QEMU_OPT_NUMBER, |
2798 | .help = "limit read operations per second", | |
2799 | },{ | |
57975222 | 2800 | .name = "throttling.iops-write", |
4d454574 PB |
2801 | .type = QEMU_OPT_NUMBER, |
2802 | .help = "limit write operations per second", | |
2803 | },{ | |
57975222 | 2804 | .name = "throttling.bps-total", |
4d454574 PB |
2805 | .type = QEMU_OPT_NUMBER, |
2806 | .help = "limit total bytes per second", | |
2807 | },{ | |
57975222 | 2808 | .name = "throttling.bps-read", |
4d454574 PB |
2809 | .type = QEMU_OPT_NUMBER, |
2810 | .help = "limit read bytes per second", | |
2811 | },{ | |
57975222 | 2812 | .name = "throttling.bps-write", |
4d454574 PB |
2813 | .type = QEMU_OPT_NUMBER, |
2814 | .help = "limit write bytes per second", | |
3e9fab69 BC |
2815 | },{ |
2816 | .name = "throttling.iops-total-max", | |
2817 | .type = QEMU_OPT_NUMBER, | |
2818 | .help = "I/O operations burst", | |
2819 | },{ | |
2820 | .name = "throttling.iops-read-max", | |
2821 | .type = QEMU_OPT_NUMBER, | |
2822 | .help = "I/O operations read burst", | |
2823 | },{ | |
2824 | .name = "throttling.iops-write-max", | |
2825 | .type = QEMU_OPT_NUMBER, | |
2826 | .help = "I/O operations write burst", | |
2827 | },{ | |
2828 | .name = "throttling.bps-total-max", | |
2829 | .type = QEMU_OPT_NUMBER, | |
2830 | .help = "total bytes burst", | |
2831 | },{ | |
2832 | .name = "throttling.bps-read-max", | |
2833 | .type = QEMU_OPT_NUMBER, | |
2834 | .help = "total bytes read burst", | |
2835 | },{ | |
2836 | .name = "throttling.bps-write-max", | |
2837 | .type = QEMU_OPT_NUMBER, | |
2838 | .help = "total bytes write burst", | |
2024c1df BC |
2839 | },{ |
2840 | .name = "throttling.iops-size", | |
2841 | .type = QEMU_OPT_NUMBER, | |
2842 | .help = "when limiting by iops max size of an I/O in bytes", | |
4d454574 PB |
2843 | },{ |
2844 | .name = "copy-on-read", | |
2845 | .type = QEMU_OPT_BOOL, | |
2846 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
2847 | },{ |
2848 | .name = "detect-zeroes", | |
2849 | .type = QEMU_OPT_STRING, | |
2850 | .help = "try to optimize zero writes (off, on, unmap)", | |
4d454574 PB |
2851 | }, |
2852 | { /* end of list */ } | |
2853 | }, | |
2854 | }; | |
0006383e KW |
2855 | |
2856 | QemuOptsList qemu_drive_opts = { | |
2857 | .name = "drive", | |
2858 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
2859 | .desc = { | |
492fdc6f KW |
2860 | /* |
2861 | * no elements => accept any params | |
2862 | * validation will happen later | |
2863 | */ | |
0006383e KW |
2864 | { /* end of list */ } |
2865 | }, | |
2866 | }; |