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