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