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