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