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