xref: /openbmc/qemu/blockdev.c (revision c8a83e8500329d82f1282c4905be11a39078007f)
1666daa68SMarkus Armbruster /*
2666daa68SMarkus Armbruster  * QEMU host block devices
3666daa68SMarkus Armbruster  *
4666daa68SMarkus Armbruster  * Copyright (c) 2003-2008 Fabrice Bellard
5666daa68SMarkus Armbruster  *
6666daa68SMarkus Armbruster  * This work is licensed under the terms of the GNU GPL, version 2 or
7666daa68SMarkus Armbruster  * later.  See the COPYING file in the top-level directory.
83618a094SMarkus Armbruster  *
93618a094SMarkus Armbruster  * This file incorporates work covered by the following copyright and
103618a094SMarkus Armbruster  * permission notice:
113618a094SMarkus Armbruster  *
123618a094SMarkus Armbruster  * Copyright (c) 2003-2008 Fabrice Bellard
133618a094SMarkus Armbruster  *
143618a094SMarkus Armbruster  * Permission is hereby granted, free of charge, to any person obtaining a copy
153618a094SMarkus Armbruster  * of this software and associated documentation files (the "Software"), to deal
163618a094SMarkus Armbruster  * in the Software without restriction, including without limitation the rights
173618a094SMarkus Armbruster  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
183618a094SMarkus Armbruster  * copies of the Software, and to permit persons to whom the Software is
193618a094SMarkus Armbruster  * furnished to do so, subject to the following conditions:
203618a094SMarkus Armbruster  *
213618a094SMarkus Armbruster  * The above copyright notice and this permission notice shall be included in
223618a094SMarkus Armbruster  * all copies or substantial portions of the Software.
233618a094SMarkus Armbruster  *
243618a094SMarkus Armbruster  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253618a094SMarkus Armbruster  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
263618a094SMarkus Armbruster  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
273618a094SMarkus Armbruster  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
283618a094SMarkus Armbruster  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
293618a094SMarkus Armbruster  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
303618a094SMarkus Armbruster  * THE SOFTWARE.
31666daa68SMarkus Armbruster  */
32666daa68SMarkus Armbruster 
339c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
340d09e41aSPaolo Bonzini #include "hw/block/block.h"
35737e150eSPaolo Bonzini #include "block/blockjob.h"
3683c9089eSPaolo Bonzini #include "monitor/monitor.h"
377b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h"
381de7afc9SPaolo Bonzini #include "qemu/option.h"
391de7afc9SPaolo Bonzini #include "qemu/config-file.h"
407b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
419c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
42737e150eSPaolo Bonzini #include "block/block_int.h"
43a4dea8a9SLuiz Capitulino #include "qmp-commands.h"
4412bd451fSStefan Hajnoczi #include "trace.h"
459c17d615SPaolo Bonzini #include "sysemu/arch_init.h"
46666daa68SMarkus Armbruster 
47c9b62a7eSMarkus Armbruster static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
480006383eSKevin Wolf extern QemuOptsList qemu_common_drive_opts;
49666daa68SMarkus Armbruster 
501960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
511960966dSMarkus Armbruster     [IF_NONE] = "none",
521960966dSMarkus Armbruster     [IF_IDE] = "ide",
531960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
541960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
551960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
561960966dSMarkus Armbruster     [IF_MTD] = "mtd",
571960966dSMarkus Armbruster     [IF_SD] = "sd",
581960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
591960966dSMarkus Armbruster     [IF_XEN] = "xen",
601960966dSMarkus Armbruster };
611960966dSMarkus Armbruster 
621960966dSMarkus Armbruster static const int if_max_devs[IF_COUNT] = {
6327d6bf40SMarkus Armbruster     /*
6427d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
6527d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
6627d6bf40SMarkus Armbruster      *
6727d6bf40SMarkus Armbruster      * All controllers used to imlement if=T drives need to support
6827d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
6927d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
7027d6bf40SMarkus Armbruster      * values.
7127d6bf40SMarkus Armbruster      *
7227d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
7327d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
7427d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
7527d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
7627d6bf40SMarkus Armbruster      */
7727d6bf40SMarkus Armbruster     [IF_IDE] = 2,
7827d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
791960966dSMarkus Armbruster };
801960966dSMarkus Armbruster 
8114bafc54SMarkus Armbruster /*
8214bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
8314bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
8414bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
8514bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
8614bafc54SMarkus Armbruster  * when deletion is actually safe.
8714bafc54SMarkus Armbruster  */
8814bafc54SMarkus Armbruster void blockdev_mark_auto_del(BlockDriverState *bs)
8914bafc54SMarkus Armbruster {
9014bafc54SMarkus Armbruster     DriveInfo *dinfo = drive_get_by_blockdev(bs);
9114bafc54SMarkus Armbruster 
9212bde0eeSPaolo Bonzini     if (bs->job) {
9312bde0eeSPaolo Bonzini         block_job_cancel(bs->job);
9412bde0eeSPaolo Bonzini     }
950fc0f1faSRyan Harper     if (dinfo) {
9614bafc54SMarkus Armbruster         dinfo->auto_del = 1;
9714bafc54SMarkus Armbruster     }
980fc0f1faSRyan Harper }
9914bafc54SMarkus Armbruster 
10014bafc54SMarkus Armbruster void blockdev_auto_del(BlockDriverState *bs)
10114bafc54SMarkus Armbruster {
10214bafc54SMarkus Armbruster     DriveInfo *dinfo = drive_get_by_blockdev(bs);
10314bafc54SMarkus Armbruster 
1040fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
10584fb3925SMarcelo Tosatti         drive_put_ref(dinfo);
10614bafc54SMarkus Armbruster     }
10714bafc54SMarkus Armbruster }
10814bafc54SMarkus Armbruster 
109505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
110505a7fb1SMarkus Armbruster {
111505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
112505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
113505a7fb1SMarkus Armbruster }
114505a7fb1SMarkus Armbruster 
115505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
116505a7fb1SMarkus Armbruster {
117505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
118505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
119505a7fb1SMarkus Armbruster }
120505a7fb1SMarkus Armbruster 
1212292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
1222292ddaeSMarkus Armbruster {
1232292ddaeSMarkus Armbruster     return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
1242292ddaeSMarkus Armbruster }
1252292ddaeSMarkus Armbruster 
1262292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1275645b0f4SMarkus Armbruster                     const char *optstr)
128666daa68SMarkus Armbruster {
129666daa68SMarkus Armbruster     QemuOpts *opts;
1302292ddaeSMarkus Armbruster     char buf[32];
131666daa68SMarkus Armbruster 
1322292ddaeSMarkus Armbruster     opts = drive_def(optstr);
133666daa68SMarkus Armbruster     if (!opts) {
134666daa68SMarkus Armbruster         return NULL;
135666daa68SMarkus Armbruster     }
1362292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
1372292ddaeSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type]);
1382292ddaeSMarkus Armbruster     }
1392292ddaeSMarkus Armbruster     if (index >= 0) {
1402292ddaeSMarkus Armbruster         snprintf(buf, sizeof(buf), "%d", index);
1412292ddaeSMarkus Armbruster         qemu_opt_set(opts, "index", buf);
1422292ddaeSMarkus Armbruster     }
143666daa68SMarkus Armbruster     if (file)
144666daa68SMarkus Armbruster         qemu_opt_set(opts, "file", file);
145666daa68SMarkus Armbruster     return opts;
146666daa68SMarkus Armbruster }
147666daa68SMarkus Armbruster 
148666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
149666daa68SMarkus Armbruster {
150666daa68SMarkus Armbruster     DriveInfo *dinfo;
151666daa68SMarkus Armbruster 
152666daa68SMarkus Armbruster     /* seek interface, bus and unit */
153666daa68SMarkus Armbruster 
154666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
155666daa68SMarkus Armbruster         if (dinfo->type == type &&
156666daa68SMarkus Armbruster 	    dinfo->bus == bus &&
157666daa68SMarkus Armbruster 	    dinfo->unit == unit)
158666daa68SMarkus Armbruster             return dinfo;
159666daa68SMarkus Armbruster     }
160666daa68SMarkus Armbruster 
161666daa68SMarkus Armbruster     return NULL;
162666daa68SMarkus Armbruster }
163666daa68SMarkus Armbruster 
164f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
165f1bd51acSMarkus Armbruster {
166f1bd51acSMarkus Armbruster     return drive_get(type,
167f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
168f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
169f1bd51acSMarkus Armbruster }
170f1bd51acSMarkus Armbruster 
171666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
172666daa68SMarkus Armbruster {
173666daa68SMarkus Armbruster     int max_bus;
174666daa68SMarkus Armbruster     DriveInfo *dinfo;
175666daa68SMarkus Armbruster 
176666daa68SMarkus Armbruster     max_bus = -1;
177666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
178666daa68SMarkus Armbruster         if(dinfo->type == type &&
179666daa68SMarkus Armbruster            dinfo->bus > max_bus)
180666daa68SMarkus Armbruster             max_bus = dinfo->bus;
181666daa68SMarkus Armbruster     }
182666daa68SMarkus Armbruster     return max_bus;
183666daa68SMarkus Armbruster }
184666daa68SMarkus Armbruster 
18513839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
18613839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
18713839974SMarkus Armbruster    appropriate bus.  */
18813839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
18913839974SMarkus Armbruster {
19013839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
19113839974SMarkus Armbruster 
19213839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
19313839974SMarkus Armbruster }
19413839974SMarkus Armbruster 
195e4700e59SMarkus Armbruster DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
196666daa68SMarkus Armbruster {
197666daa68SMarkus Armbruster     DriveInfo *dinfo;
198666daa68SMarkus Armbruster 
199666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
200e4700e59SMarkus Armbruster         if (dinfo->bdrv == bs) {
201e4700e59SMarkus Armbruster             return dinfo;
202666daa68SMarkus Armbruster         }
203e4700e59SMarkus Armbruster     }
204e4700e59SMarkus Armbruster     return NULL;
205666daa68SMarkus Armbruster }
206666daa68SMarkus Armbruster 
207666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
208666daa68SMarkus Armbruster {
209807105a7SMarkus Armbruster     error_printf(" %s", name);
210666daa68SMarkus Armbruster }
211666daa68SMarkus Armbruster 
21284fb3925SMarcelo Tosatti static void drive_uninit(DriveInfo *dinfo)
213666daa68SMarkus Armbruster {
214666daa68SMarkus Armbruster     qemu_opts_del(dinfo->opts);
215666daa68SMarkus Armbruster     bdrv_delete(dinfo->bdrv);
2167267c094SAnthony Liguori     g_free(dinfo->id);
217666daa68SMarkus Armbruster     QTAILQ_REMOVE(&drives, dinfo, next);
218bb44619bSKevin Wolf     g_free(dinfo->serial);
2197267c094SAnthony Liguori     g_free(dinfo);
220666daa68SMarkus Armbruster }
221666daa68SMarkus Armbruster 
22284fb3925SMarcelo Tosatti void drive_put_ref(DriveInfo *dinfo)
22384fb3925SMarcelo Tosatti {
22484fb3925SMarcelo Tosatti     assert(dinfo->refcount);
22584fb3925SMarcelo Tosatti     if (--dinfo->refcount == 0) {
22684fb3925SMarcelo Tosatti         drive_uninit(dinfo);
22784fb3925SMarcelo Tosatti     }
22884fb3925SMarcelo Tosatti }
22984fb3925SMarcelo Tosatti 
23084fb3925SMarcelo Tosatti void drive_get_ref(DriveInfo *dinfo)
23184fb3925SMarcelo Tosatti {
23284fb3925SMarcelo Tosatti     dinfo->refcount++;
23384fb3925SMarcelo Tosatti }
23484fb3925SMarcelo Tosatti 
235aa398a5cSStefan Hajnoczi typedef struct {
236aa398a5cSStefan Hajnoczi     QEMUBH *bh;
237aa398a5cSStefan Hajnoczi     DriveInfo *dinfo;
238aa398a5cSStefan Hajnoczi } DrivePutRefBH;
239aa398a5cSStefan Hajnoczi 
240aa398a5cSStefan Hajnoczi static void drive_put_ref_bh(void *opaque)
241aa398a5cSStefan Hajnoczi {
242aa398a5cSStefan Hajnoczi     DrivePutRefBH *s = opaque;
243aa398a5cSStefan Hajnoczi 
244aa398a5cSStefan Hajnoczi     drive_put_ref(s->dinfo);
245aa398a5cSStefan Hajnoczi     qemu_bh_delete(s->bh);
246aa398a5cSStefan Hajnoczi     g_free(s);
247aa398a5cSStefan Hajnoczi }
248aa398a5cSStefan Hajnoczi 
249aa398a5cSStefan Hajnoczi /*
250aa398a5cSStefan Hajnoczi  * Release a drive reference in a BH
251aa398a5cSStefan Hajnoczi  *
252aa398a5cSStefan Hajnoczi  * It is not possible to use drive_put_ref() from a callback function when the
253aa398a5cSStefan Hajnoczi  * callers still need the drive.  In such cases we schedule a BH to release the
254aa398a5cSStefan Hajnoczi  * reference.
255aa398a5cSStefan Hajnoczi  */
256aa398a5cSStefan Hajnoczi static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
257aa398a5cSStefan Hajnoczi {
258aa398a5cSStefan Hajnoczi     DrivePutRefBH *s;
259aa398a5cSStefan Hajnoczi 
260aa398a5cSStefan Hajnoczi     s = g_new(DrivePutRefBH, 1);
261aa398a5cSStefan Hajnoczi     s->bh = qemu_bh_new(drive_put_ref_bh, s);
262aa398a5cSStefan Hajnoczi     s->dinfo = dinfo;
263aa398a5cSStefan Hajnoczi     qemu_bh_schedule(s->bh);
264aa398a5cSStefan Hajnoczi }
265aa398a5cSStefan Hajnoczi 
2661ceee0d5SPaolo Bonzini static int parse_block_error_action(const char *buf, bool is_read)
267666daa68SMarkus Armbruster {
268666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
26992aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
270666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
27192aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
272666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
27392aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
274666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
27592aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
276666daa68SMarkus Armbruster     } else {
277807105a7SMarkus Armbruster         error_report("'%s' invalid %s error action",
278666daa68SMarkus Armbruster                      buf, is_read ? "read" : "write");
279666daa68SMarkus Armbruster         return -1;
280666daa68SMarkus Armbruster     }
281666daa68SMarkus Armbruster }
282666daa68SMarkus Armbruster 
283c546194fSStefan Hajnoczi static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
2840563e191SZhi Yong Wu {
2850563e191SZhi Yong Wu     bool bps_flag;
2860563e191SZhi Yong Wu     bool iops_flag;
2870563e191SZhi Yong Wu 
2880563e191SZhi Yong Wu     assert(io_limits);
2890563e191SZhi Yong Wu 
2900563e191SZhi Yong Wu     bps_flag  = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
2910563e191SZhi Yong Wu                  && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
2920563e191SZhi Yong Wu                  || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
2930563e191SZhi Yong Wu     iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
2940563e191SZhi Yong Wu                  && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
2950563e191SZhi Yong Wu                  || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
2960563e191SZhi Yong Wu     if (bps_flag || iops_flag) {
297c546194fSStefan Hajnoczi         error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
298c546194fSStefan Hajnoczi                          "cannot be used at the same time");
2990563e191SZhi Yong Wu         return false;
3000563e191SZhi Yong Wu     }
3010563e191SZhi Yong Wu 
3027d81c141SStefan Hajnoczi     if (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] < 0 ||
3037d81c141SStefan Hajnoczi         io_limits->bps[BLOCK_IO_LIMIT_WRITE] < 0 ||
3047d81c141SStefan Hajnoczi         io_limits->bps[BLOCK_IO_LIMIT_READ] < 0 ||
3057d81c141SStefan Hajnoczi         io_limits->iops[BLOCK_IO_LIMIT_TOTAL] < 0 ||
3067d81c141SStefan Hajnoczi         io_limits->iops[BLOCK_IO_LIMIT_WRITE] < 0 ||
3077d81c141SStefan Hajnoczi         io_limits->iops[BLOCK_IO_LIMIT_READ] < 0) {
3087d81c141SStefan Hajnoczi         error_setg(errp, "bps and iops values must be 0 or greater");
3097d81c141SStefan Hajnoczi         return false;
3107d81c141SStefan Hajnoczi     }
3117d81c141SStefan Hajnoczi 
3120563e191SZhi Yong Wu     return true;
3130563e191SZhi Yong Wu }
3140563e191SZhi Yong Wu 
3150006383eSKevin Wolf DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
316666daa68SMarkus Armbruster {
317666daa68SMarkus Armbruster     const char *buf;
318666daa68SMarkus Armbruster     const char *file = NULL;
319666daa68SMarkus Armbruster     const char *serial;
320666daa68SMarkus Armbruster     const char *mediastr = "";
321666daa68SMarkus Armbruster     BlockInterfaceType type;
322666daa68SMarkus Armbruster     enum { MEDIA_DISK, MEDIA_CDROM } media;
323666daa68SMarkus Armbruster     int bus_id, unit_id;
324666daa68SMarkus Armbruster     int cyls, heads, secs, translation;
325666daa68SMarkus Armbruster     BlockDriver *drv = NULL;
326666daa68SMarkus Armbruster     int max_devs;
327666daa68SMarkus Armbruster     int index;
328666daa68SMarkus Armbruster     int ro = 0;
329666daa68SMarkus Armbruster     int bdrv_flags = 0;
330666daa68SMarkus Armbruster     int on_read_error, on_write_error;
331666daa68SMarkus Armbruster     const char *devaddr;
332666daa68SMarkus Armbruster     DriveInfo *dinfo;
3330563e191SZhi Yong Wu     BlockIOLimit io_limits;
334666daa68SMarkus Armbruster     int snapshot = 0;
335fb0490f6SStefan Hajnoczi     bool copy_on_read;
336666daa68SMarkus Armbruster     int ret;
337c546194fSStefan Hajnoczi     Error *error = NULL;
3380006383eSKevin Wolf     QemuOpts *opts;
3390006383eSKevin Wolf     QDict *bs_opts;
3400006383eSKevin Wolf     const char *id;
341666daa68SMarkus Armbruster 
342666daa68SMarkus Armbruster     translation = BIOS_ATA_TRANSLATION_AUTO;
343666daa68SMarkus Armbruster     media = MEDIA_DISK;
344666daa68SMarkus Armbruster 
3450006383eSKevin Wolf     /* Check common options by copying from all_opts to opts, all other options
3460006383eSKevin Wolf      * are stored in bs_opts. */
3470006383eSKevin Wolf     id = qemu_opts_id(all_opts);
3480006383eSKevin Wolf     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
3490006383eSKevin Wolf     if (error_is_set(&error)) {
3500006383eSKevin Wolf         qerror_report_err(error);
3510006383eSKevin Wolf         error_free(error);
3520006383eSKevin Wolf         return NULL;
3530006383eSKevin Wolf     }
3540006383eSKevin Wolf 
3550006383eSKevin Wolf     bs_opts = qdict_new();
3560006383eSKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
3570006383eSKevin Wolf     qemu_opts_absorb_qdict(opts, bs_opts, &error);
3580006383eSKevin Wolf     if (error_is_set(&error)) {
3590006383eSKevin Wolf         qerror_report_err(error);
3600006383eSKevin Wolf         error_free(error);
3610006383eSKevin Wolf         return NULL;
3620006383eSKevin Wolf     }
3630006383eSKevin Wolf 
3640006383eSKevin Wolf     if (id) {
3650006383eSKevin Wolf         qdict_del(bs_opts, "id");
3660006383eSKevin Wolf     }
3670006383eSKevin Wolf 
368666daa68SMarkus Armbruster     /* extract parameters */
369666daa68SMarkus Armbruster     bus_id  = qemu_opt_get_number(opts, "bus", 0);
370666daa68SMarkus Armbruster     unit_id = qemu_opt_get_number(opts, "unit", -1);
371666daa68SMarkus Armbruster     index   = qemu_opt_get_number(opts, "index", -1);
372666daa68SMarkus Armbruster 
373666daa68SMarkus Armbruster     cyls  = qemu_opt_get_number(opts, "cyls", 0);
374666daa68SMarkus Armbruster     heads = qemu_opt_get_number(opts, "heads", 0);
375666daa68SMarkus Armbruster     secs  = qemu_opt_get_number(opts, "secs", 0);
376666daa68SMarkus Armbruster 
377666daa68SMarkus Armbruster     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
378666daa68SMarkus Armbruster     ro = qemu_opt_get_bool(opts, "readonly", 0);
379fb0490f6SStefan Hajnoczi     copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
380666daa68SMarkus Armbruster 
381666daa68SMarkus Armbruster     file = qemu_opt_get(opts, "file");
382666daa68SMarkus Armbruster     serial = qemu_opt_get(opts, "serial");
383666daa68SMarkus Armbruster 
384666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "if")) != NULL) {
3851960966dSMarkus Armbruster         for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
3861960966dSMarkus Armbruster             ;
3871960966dSMarkus Armbruster         if (type == IF_COUNT) {
388807105a7SMarkus Armbruster             error_report("unsupported bus type '%s'", buf);
389666daa68SMarkus Armbruster             return NULL;
390666daa68SMarkus Armbruster 	}
3912d3999feSLuiz Capitulino     } else {
3922d0d2837SChristian Borntraeger         type = block_default_type;
393666daa68SMarkus Armbruster     }
3942d3999feSLuiz Capitulino 
3951960966dSMarkus Armbruster     max_devs = if_max_devs[type];
396666daa68SMarkus Armbruster 
397666daa68SMarkus Armbruster     if (cyls || heads || secs) {
398aaea3f36SMarkus Armbruster         if (cyls < 1) {
399807105a7SMarkus Armbruster             error_report("invalid physical cyls number");
400666daa68SMarkus Armbruster 	    return NULL;
401666daa68SMarkus Armbruster 	}
402aaea3f36SMarkus Armbruster         if (heads < 1) {
403807105a7SMarkus Armbruster             error_report("invalid physical heads number");
404666daa68SMarkus Armbruster 	    return NULL;
405666daa68SMarkus Armbruster 	}
406aaea3f36SMarkus Armbruster         if (secs < 1) {
407807105a7SMarkus Armbruster             error_report("invalid physical secs number");
408666daa68SMarkus Armbruster 	    return NULL;
409666daa68SMarkus Armbruster 	}
410666daa68SMarkus Armbruster     }
411666daa68SMarkus Armbruster 
412666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
413666daa68SMarkus Armbruster         if (!cyls) {
414807105a7SMarkus Armbruster             error_report("'%s' trans must be used with cyls, heads and secs",
415666daa68SMarkus Armbruster                          buf);
416666daa68SMarkus Armbruster             return NULL;
417666daa68SMarkus Armbruster         }
418666daa68SMarkus Armbruster         if (!strcmp(buf, "none"))
419666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_NONE;
420666daa68SMarkus Armbruster         else if (!strcmp(buf, "lba"))
421666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_LBA;
422666daa68SMarkus Armbruster         else if (!strcmp(buf, "auto"))
423666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_AUTO;
424666daa68SMarkus Armbruster 	else {
425807105a7SMarkus Armbruster             error_report("'%s' invalid translation type", buf);
426666daa68SMarkus Armbruster 	    return NULL;
427666daa68SMarkus Armbruster 	}
428666daa68SMarkus Armbruster     }
429666daa68SMarkus Armbruster 
430666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "media")) != NULL) {
431666daa68SMarkus Armbruster         if (!strcmp(buf, "disk")) {
432666daa68SMarkus Armbruster 	    media = MEDIA_DISK;
433666daa68SMarkus Armbruster 	} else if (!strcmp(buf, "cdrom")) {
434666daa68SMarkus Armbruster             if (cyls || secs || heads) {
435e7ff8f0eSLuiz Capitulino                 error_report("CHS can't be set with media=%s", buf);
436666daa68SMarkus Armbruster 	        return NULL;
437666daa68SMarkus Armbruster             }
438666daa68SMarkus Armbruster 	    media = MEDIA_CDROM;
439666daa68SMarkus Armbruster 	} else {
440807105a7SMarkus Armbruster 	    error_report("'%s' invalid media", buf);
441666daa68SMarkus Armbruster 	    return NULL;
442666daa68SMarkus Armbruster 	}
443666daa68SMarkus Armbruster     }
444666daa68SMarkus Armbruster 
445a9384affSPaolo Bonzini     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
446a9384affSPaolo Bonzini         if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
447a9384affSPaolo Bonzini             error_report("invalid discard option");
448a9384affSPaolo Bonzini             return NULL;
449a9384affSPaolo Bonzini         }
450a9384affSPaolo Bonzini     }
451a9384affSPaolo Bonzini 
4521f212b9dSPaolo Bonzini     bdrv_flags |= BDRV_O_CACHE_WB;
453666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
454c3993cdcSStefan Hajnoczi         if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
455807105a7SMarkus Armbruster             error_report("invalid cache option");
456666daa68SMarkus Armbruster             return NULL;
457666daa68SMarkus Armbruster         }
458666daa68SMarkus Armbruster     }
459666daa68SMarkus Armbruster 
460666daa68SMarkus Armbruster #ifdef CONFIG_LINUX_AIO
461666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
462666daa68SMarkus Armbruster         if (!strcmp(buf, "native")) {
463666daa68SMarkus Armbruster             bdrv_flags |= BDRV_O_NATIVE_AIO;
464666daa68SMarkus Armbruster         } else if (!strcmp(buf, "threads")) {
465666daa68SMarkus Armbruster             /* this is the default */
466666daa68SMarkus Armbruster         } else {
467807105a7SMarkus Armbruster            error_report("invalid aio option");
468666daa68SMarkus Armbruster            return NULL;
469666daa68SMarkus Armbruster         }
470666daa68SMarkus Armbruster     }
471666daa68SMarkus Armbruster #endif
472666daa68SMarkus Armbruster 
473666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
474c8057f95SPeter Maydell         if (is_help_option(buf)) {
475807105a7SMarkus Armbruster             error_printf("Supported formats:");
476666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
477807105a7SMarkus Armbruster             error_printf("\n");
478666daa68SMarkus Armbruster             return NULL;
479666daa68SMarkus Armbruster         }
480666daa68SMarkus Armbruster         drv = bdrv_find_whitelisted_format(buf);
481666daa68SMarkus Armbruster         if (!drv) {
482807105a7SMarkus Armbruster             error_report("'%s' invalid format", buf);
483666daa68SMarkus Armbruster             return NULL;
484666daa68SMarkus Armbruster         }
485666daa68SMarkus Armbruster     }
486666daa68SMarkus Armbruster 
4870563e191SZhi Yong Wu     /* disk I/O throttling */
4880563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =
4890563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps", 0);
4900563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_READ]   =
4910563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps_rd", 0);
4920563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =
4930563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps_wr", 0);
4940563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
4950563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops", 0);
4960563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_READ]  =
4970563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops_rd", 0);
4980563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
4990563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops_wr", 0);
5000563e191SZhi Yong Wu 
501c546194fSStefan Hajnoczi     if (!do_check_io_limits(&io_limits, &error)) {
502c546194fSStefan Hajnoczi         error_report("%s", error_get_pretty(error));
503c546194fSStefan Hajnoczi         error_free(error);
5040563e191SZhi Yong Wu         return NULL;
5050563e191SZhi Yong Wu     }
5060563e191SZhi Yong Wu 
5070d92d17aSJan Kiszka     if (qemu_opt_get(opts, "boot") != NULL) {
5080d92d17aSJan Kiszka         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
5090d92d17aSJan Kiszka                 "ignored. Future versions will reject this parameter. Please "
5100d92d17aSJan Kiszka                 "update your scripts.\n");
5110d92d17aSJan Kiszka     }
5120d92d17aSJan Kiszka 
51392aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
514666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
515666daa68SMarkus Armbruster         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
516807105a7SMarkus Armbruster             error_report("werror is not supported by this bus type");
517666daa68SMarkus Armbruster             return NULL;
518666daa68SMarkus Armbruster         }
519666daa68SMarkus Armbruster 
520666daa68SMarkus Armbruster         on_write_error = parse_block_error_action(buf, 0);
521666daa68SMarkus Armbruster         if (on_write_error < 0) {
522666daa68SMarkus Armbruster             return NULL;
523666daa68SMarkus Armbruster         }
524666daa68SMarkus Armbruster     }
525666daa68SMarkus Armbruster 
52692aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
527666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
5285dba48a8SKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
529807105a7SMarkus Armbruster             error_report("rerror is not supported by this bus type");
530666daa68SMarkus Armbruster             return NULL;
531666daa68SMarkus Armbruster         }
532666daa68SMarkus Armbruster 
533666daa68SMarkus Armbruster         on_read_error = parse_block_error_action(buf, 1);
534666daa68SMarkus Armbruster         if (on_read_error < 0) {
535666daa68SMarkus Armbruster             return NULL;
536666daa68SMarkus Armbruster         }
537666daa68SMarkus Armbruster     }
538666daa68SMarkus Armbruster 
539666daa68SMarkus Armbruster     if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
540666daa68SMarkus Armbruster         if (type != IF_VIRTIO) {
541807105a7SMarkus Armbruster             error_report("addr is not supported by this bus type");
542666daa68SMarkus Armbruster             return NULL;
543666daa68SMarkus Armbruster         }
544666daa68SMarkus Armbruster     }
545666daa68SMarkus Armbruster 
546666daa68SMarkus Armbruster     /* compute bus and unit according index */
547666daa68SMarkus Armbruster 
548666daa68SMarkus Armbruster     if (index != -1) {
549666daa68SMarkus Armbruster         if (bus_id != 0 || unit_id != -1) {
550807105a7SMarkus Armbruster             error_report("index cannot be used with bus and unit");
551666daa68SMarkus Armbruster             return NULL;
552666daa68SMarkus Armbruster         }
553505a7fb1SMarkus Armbruster         bus_id = drive_index_to_bus_id(type, index);
554505a7fb1SMarkus Armbruster         unit_id = drive_index_to_unit_id(type, index);
555666daa68SMarkus Armbruster     }
556666daa68SMarkus Armbruster 
557666daa68SMarkus Armbruster     /* if user doesn't specify a unit_id,
558666daa68SMarkus Armbruster      * try to find the first free
559666daa68SMarkus Armbruster      */
560666daa68SMarkus Armbruster 
561666daa68SMarkus Armbruster     if (unit_id == -1) {
562666daa68SMarkus Armbruster        unit_id = 0;
563666daa68SMarkus Armbruster        while (drive_get(type, bus_id, unit_id) != NULL) {
564666daa68SMarkus Armbruster            unit_id++;
565666daa68SMarkus Armbruster            if (max_devs && unit_id >= max_devs) {
566666daa68SMarkus Armbruster                unit_id -= max_devs;
567666daa68SMarkus Armbruster                bus_id++;
568666daa68SMarkus Armbruster            }
569666daa68SMarkus Armbruster        }
570666daa68SMarkus Armbruster     }
571666daa68SMarkus Armbruster 
572666daa68SMarkus Armbruster     /* check unit id */
573666daa68SMarkus Armbruster 
574666daa68SMarkus Armbruster     if (max_devs && unit_id >= max_devs) {
575807105a7SMarkus Armbruster         error_report("unit %d too big (max is %d)",
576666daa68SMarkus Armbruster                      unit_id, max_devs - 1);
577666daa68SMarkus Armbruster         return NULL;
578666daa68SMarkus Armbruster     }
579666daa68SMarkus Armbruster 
580666daa68SMarkus Armbruster     /*
5814e5d9b57SMarkus Armbruster      * catch multiple definitions
582666daa68SMarkus Armbruster      */
583666daa68SMarkus Armbruster 
584666daa68SMarkus Armbruster     if (drive_get(type, bus_id, unit_id) != NULL) {
5854e5d9b57SMarkus Armbruster         error_report("drive with bus=%d, unit=%d (index=%d) exists",
5864e5d9b57SMarkus Armbruster                      bus_id, unit_id, index);
587666daa68SMarkus Armbruster         return NULL;
588666daa68SMarkus Armbruster     }
589666daa68SMarkus Armbruster 
590666daa68SMarkus Armbruster     /* init */
591666daa68SMarkus Armbruster 
5927267c094SAnthony Liguori     dinfo = g_malloc0(sizeof(*dinfo));
593666daa68SMarkus Armbruster     if ((buf = qemu_opts_id(opts)) != NULL) {
5947267c094SAnthony Liguori         dinfo->id = g_strdup(buf);
595666daa68SMarkus Armbruster     } else {
596666daa68SMarkus Armbruster         /* no id supplied -> create one */
5977267c094SAnthony Liguori         dinfo->id = g_malloc0(32);
598666daa68SMarkus Armbruster         if (type == IF_IDE || type == IF_SCSI)
599666daa68SMarkus Armbruster             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
600666daa68SMarkus Armbruster         if (max_devs)
601666daa68SMarkus Armbruster             snprintf(dinfo->id, 32, "%s%i%s%i",
60279d21d5bSMarkus Armbruster                      if_name[type], bus_id, mediastr, unit_id);
603666daa68SMarkus Armbruster         else
604666daa68SMarkus Armbruster             snprintf(dinfo->id, 32, "%s%s%i",
60579d21d5bSMarkus Armbruster                      if_name[type], mediastr, unit_id);
606666daa68SMarkus Armbruster     }
607666daa68SMarkus Armbruster     dinfo->bdrv = bdrv_new(dinfo->id);
60880dd1aaeSKevin Shanahan     dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
60980dd1aaeSKevin Shanahan     dinfo->bdrv->read_only = ro;
610666daa68SMarkus Armbruster     dinfo->devaddr = devaddr;
611666daa68SMarkus Armbruster     dinfo->type = type;
612666daa68SMarkus Armbruster     dinfo->bus = bus_id;
613666daa68SMarkus Armbruster     dinfo->unit = unit_id;
614317bb412SMarkus Armbruster     dinfo->cyls = cyls;
615317bb412SMarkus Armbruster     dinfo->heads = heads;
616317bb412SMarkus Armbruster     dinfo->secs = secs;
617317bb412SMarkus Armbruster     dinfo->trans = translation;
6180006383eSKevin Wolf     dinfo->opts = all_opts;
61984fb3925SMarcelo Tosatti     dinfo->refcount = 1;
620bb44619bSKevin Wolf     if (serial != NULL) {
621bb44619bSKevin Wolf         dinfo->serial = g_strdup(serial);
622bb44619bSKevin Wolf     }
623666daa68SMarkus Armbruster     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
624666daa68SMarkus Armbruster 
625abd7f68dSMarkus Armbruster     bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
626abd7f68dSMarkus Armbruster 
6270563e191SZhi Yong Wu     /* disk I/O throttling */
6280563e191SZhi Yong Wu     bdrv_set_io_limits(dinfo->bdrv, &io_limits);
6290563e191SZhi Yong Wu 
630666daa68SMarkus Armbruster     switch(type) {
631666daa68SMarkus Armbruster     case IF_IDE:
632666daa68SMarkus Armbruster     case IF_SCSI:
633666daa68SMarkus Armbruster     case IF_XEN:
634666daa68SMarkus Armbruster     case IF_NONE:
6352b584959SMarkus Armbruster         dinfo->media_cd = media == MEDIA_CDROM;
636666daa68SMarkus Armbruster         break;
637666daa68SMarkus Armbruster     case IF_SD:
638666daa68SMarkus Armbruster     case IF_FLOPPY:
639666daa68SMarkus Armbruster     case IF_PFLASH:
640666daa68SMarkus Armbruster     case IF_MTD:
641666daa68SMarkus Armbruster         break;
642666daa68SMarkus Armbruster     case IF_VIRTIO:
6430006383eSKevin Wolf     {
644666daa68SMarkus Armbruster         /* add virtio block device */
6450006383eSKevin Wolf         QemuOpts *devopts;
6460006383eSKevin Wolf         devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
647eeb9c1b5SAnthony Liguori         if (arch_type == QEMU_ARCH_S390X) {
6480006383eSKevin Wolf             qemu_opt_set(devopts, "driver", "virtio-blk-s390");
649eeb9c1b5SAnthony Liguori         } else {
6500006383eSKevin Wolf             qemu_opt_set(devopts, "driver", "virtio-blk-pci");
651eeb9c1b5SAnthony Liguori         }
6520006383eSKevin Wolf         qemu_opt_set(devopts, "drive", dinfo->id);
653666daa68SMarkus Armbruster         if (devaddr)
6540006383eSKevin Wolf             qemu_opt_set(devopts, "addr", devaddr);
655666daa68SMarkus Armbruster         break;
6560006383eSKevin Wolf     }
6572292ddaeSMarkus Armbruster     default:
658666daa68SMarkus Armbruster         abort();
659666daa68SMarkus Armbruster     }
660dd5b0d71SMarkus Armbruster     if (!file || !*file) {
661c2ad1b0cSKevin Wolf         if (qdict_size(bs_opts)) {
662c2ad1b0cSKevin Wolf             file = NULL;
663c2ad1b0cSKevin Wolf         } else {
664319ae529SMarkus Armbruster             return dinfo;
665666daa68SMarkus Armbruster         }
666c2ad1b0cSKevin Wolf     }
667666daa68SMarkus Armbruster     if (snapshot) {
668666daa68SMarkus Armbruster         /* always use cache=unsafe with snapshot */
669666daa68SMarkus Armbruster         bdrv_flags &= ~BDRV_O_CACHE_MASK;
670666daa68SMarkus Armbruster         bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
671666daa68SMarkus Armbruster     }
672666daa68SMarkus Armbruster 
673fb0490f6SStefan Hajnoczi     if (copy_on_read) {
674fb0490f6SStefan Hajnoczi         bdrv_flags |= BDRV_O_COPY_ON_READ;
675fb0490f6SStefan Hajnoczi     }
676fb0490f6SStefan Hajnoczi 
677ed9d4205SBenoît Canet     if (runstate_check(RUN_STATE_INMIGRATE)) {
678ed9d4205SBenoît Canet         bdrv_flags |= BDRV_O_INCOMING;
679ed9d4205SBenoît Canet     }
680ed9d4205SBenoît Canet 
681666daa68SMarkus Armbruster     if (media == MEDIA_CDROM) {
682666daa68SMarkus Armbruster         /* CDROM is fine for any interface, don't check.  */
683666daa68SMarkus Armbruster         ro = 1;
684666daa68SMarkus Armbruster     } else if (ro == 1) {
6851e9eb78aSJordan Justen         if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
6861e9eb78aSJordan Justen             type != IF_NONE && type != IF_PFLASH) {
687807105a7SMarkus Armbruster             error_report("readonly not supported by this bus type");
688a9ae2bffSMarkus Armbruster             goto err;
689666daa68SMarkus Armbruster         }
690666daa68SMarkus Armbruster     }
691666daa68SMarkus Armbruster 
692666daa68SMarkus Armbruster     bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
693666daa68SMarkus Armbruster 
69404d4abe9SStefan Hajnoczi     if (ro && copy_on_read) {
69504d4abe9SStefan Hajnoczi         error_report("warning: disabling copy_on_read on readonly drive");
69604d4abe9SStefan Hajnoczi     }
69704d4abe9SStefan Hajnoczi 
6980006383eSKevin Wolf     ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
6990006383eSKevin Wolf     bs_opts = NULL;
7000006383eSKevin Wolf 
701666daa68SMarkus Armbruster     if (ret < 0) {
70202582abdSStefan Weil         if (ret == -EMEDIUMTYPE) {
70302582abdSStefan Weil             error_report("could not open disk image %s: not in %s format",
704c2ad1b0cSKevin Wolf                          file ?: dinfo->id, drv->format_name);
70502582abdSStefan Weil         } else {
706807105a7SMarkus Armbruster             error_report("could not open disk image %s: %s",
707c2ad1b0cSKevin Wolf                          file ?: dinfo->id, strerror(-ret));
70802582abdSStefan Weil         }
709a9ae2bffSMarkus Armbruster         goto err;
710666daa68SMarkus Armbruster     }
711666daa68SMarkus Armbruster 
712666daa68SMarkus Armbruster     if (bdrv_key_required(dinfo->bdrv))
713666daa68SMarkus Armbruster         autostart = 0;
7140006383eSKevin Wolf 
7150006383eSKevin Wolf     qemu_opts_del(opts);
7160006383eSKevin Wolf 
717666daa68SMarkus Armbruster     return dinfo;
718a9ae2bffSMarkus Armbruster 
719a9ae2bffSMarkus Armbruster err:
7200006383eSKevin Wolf     qemu_opts_del(opts);
7210006383eSKevin Wolf     QDECREF(bs_opts);
722a9ae2bffSMarkus Armbruster     bdrv_delete(dinfo->bdrv);
7237267c094SAnthony Liguori     g_free(dinfo->id);
724a9ae2bffSMarkus Armbruster     QTAILQ_REMOVE(&drives, dinfo, next);
7257267c094SAnthony Liguori     g_free(dinfo);
726a9ae2bffSMarkus Armbruster     return NULL;
727666daa68SMarkus Armbruster }
728666daa68SMarkus Armbruster 
729666daa68SMarkus Armbruster void do_commit(Monitor *mon, const QDict *qdict)
730666daa68SMarkus Armbruster {
731666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
7326ab4b5abSMarkus Armbruster     BlockDriverState *bs;
7332d3735d3SStefan Hajnoczi     int ret;
7342d3735d3SStefan Hajnoczi 
735e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
736e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
737e8877497SStefan Hajnoczi     } else {
7386ab4b5abSMarkus Armbruster         bs = bdrv_find(device);
739ac59eb95SMarkus Armbruster         if (!bs) {
74058513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
741ac59eb95SMarkus Armbruster             return;
7426ab4b5abSMarkus Armbruster         }
7432d3735d3SStefan Hajnoczi         ret = bdrv_commit(bs);
7442d3735d3SStefan Hajnoczi     }
74558513bdeSJeff Cody     if (ret < 0) {
74658513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
74758513bdeSJeff Cody                        strerror(-ret));
748666daa68SMarkus Armbruster     }
749666daa68SMarkus Armbruster }
750666daa68SMarkus Armbruster 
7516cc2a415SPaolo Bonzini static void blockdev_do_action(int kind, void *data, Error **errp)
7526cc2a415SPaolo Bonzini {
753*c8a83e85SKevin Wolf     TransactionAction action;
754*c8a83e85SKevin Wolf     TransactionActionList list;
7556cc2a415SPaolo Bonzini 
7566cc2a415SPaolo Bonzini     action.kind = kind;
7576cc2a415SPaolo Bonzini     action.data = data;
7586cc2a415SPaolo Bonzini     list.value = &action;
7596cc2a415SPaolo Bonzini     list.next = NULL;
7606cc2a415SPaolo Bonzini     qmp_transaction(&list, errp);
7616cc2a415SPaolo Bonzini }
7626cc2a415SPaolo Bonzini 
7636106e249SLuiz Capitulino void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
7646106e249SLuiz Capitulino                                 bool has_format, const char *format,
7656cc2a415SPaolo Bonzini                                 bool has_mode, enum NewImageMode mode,
7666106e249SLuiz Capitulino                                 Error **errp)
767f8882568SJes Sorensen {
7686cc2a415SPaolo Bonzini     BlockdevSnapshot snapshot = {
7696cc2a415SPaolo Bonzini         .device = (char *) device,
7706cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
7716cc2a415SPaolo Bonzini         .has_format = has_format,
7726cc2a415SPaolo Bonzini         .format = (char *) format,
7736cc2a415SPaolo Bonzini         .has_mode = has_mode,
7746cc2a415SPaolo Bonzini         .mode = mode,
7756cc2a415SPaolo Bonzini     };
776*c8a83e85SKevin Wolf     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
777*c8a83e85SKevin Wolf                        &snapshot, errp);
778f8882568SJes Sorensen }
779f8882568SJes Sorensen 
7808802d1fdSJeff Cody 
7818802d1fdSJeff Cody /* New and old BlockDriverState structs for group snapshots */
782ba0c86a3SWenchao Xia 
783ba0c86a3SWenchao Xia typedef struct BlkTransactionStates BlkTransactionStates;
784ba0c86a3SWenchao Xia 
785ba0c86a3SWenchao Xia /* Only prepare() may fail. In a single transaction, only one of commit() or
786ba0c86a3SWenchao Xia    abort() will be called, clean() will always be called if it present. */
787ba0c86a3SWenchao Xia typedef struct BdrvActionOps {
788ba0c86a3SWenchao Xia     /* Size of state struct, in bytes. */
789ba0c86a3SWenchao Xia     size_t instance_size;
790ba0c86a3SWenchao Xia     /* Prepare the work, must NOT be NULL. */
791ba0c86a3SWenchao Xia     void (*prepare)(BlkTransactionStates *common, Error **errp);
792ba0c86a3SWenchao Xia     /* Commit the changes, must NOT be NULL. */
793ba0c86a3SWenchao Xia     void (*commit)(BlkTransactionStates *common);
794ba0c86a3SWenchao Xia     /* Abort the changes on fail, can be NULL. */
795ba0c86a3SWenchao Xia     void (*abort)(BlkTransactionStates *common);
796ba0c86a3SWenchao Xia     /* Clean up resource in the end, can be NULL. */
797ba0c86a3SWenchao Xia     void (*clean)(BlkTransactionStates *common);
798ba0c86a3SWenchao Xia } BdrvActionOps;
799ba0c86a3SWenchao Xia 
800ba0c86a3SWenchao Xia /*
801ba0c86a3SWenchao Xia  * This structure must be arranged as first member in child type, assuming
802ba0c86a3SWenchao Xia  * that compiler will also arrange it to the same address with parent instance.
803ba0c86a3SWenchao Xia  * Later it will be used in free().
804ba0c86a3SWenchao Xia  */
805ba0c86a3SWenchao Xia struct BlkTransactionStates {
806*c8a83e85SKevin Wolf     TransactionAction *action;
807ba0c86a3SWenchao Xia     const BdrvActionOps *ops;
808ba0c86a3SWenchao Xia     QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
809ba0c86a3SWenchao Xia };
810ba0c86a3SWenchao Xia 
811ba0c86a3SWenchao Xia /* external snapshot private data */
812ba0c86a3SWenchao Xia typedef struct ExternalSnapshotStates {
813ba0c86a3SWenchao Xia     BlkTransactionStates common;
8148802d1fdSJeff Cody     BlockDriverState *old_bs;
8158802d1fdSJeff Cody     BlockDriverState *new_bs;
816ba0c86a3SWenchao Xia } ExternalSnapshotStates;
8178802d1fdSJeff Cody 
818ba0c86a3SWenchao Xia static void external_snapshot_prepare(BlkTransactionStates *common,
8199b9877eeSWenchao Xia                                       Error **errp)
8209b9877eeSWenchao Xia {
8219b9877eeSWenchao Xia     BlockDriver *proto_drv;
8229b9877eeSWenchao Xia     BlockDriver *drv;
8239b9877eeSWenchao Xia     int flags, ret;
8249b9877eeSWenchao Xia     Error *local_err = NULL;
825e2a31e87SWenchao Xia     const char *device;
826e2a31e87SWenchao Xia     const char *new_image_file;
827e2a31e87SWenchao Xia     const char *format = "qcow2";
828e2a31e87SWenchao Xia     enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
829ba0c86a3SWenchao Xia     ExternalSnapshotStates *states =
830ba0c86a3SWenchao Xia                              DO_UPCAST(ExternalSnapshotStates, common, common);
831*c8a83e85SKevin Wolf     TransactionAction *action = common->action;
8329b9877eeSWenchao Xia 
833e2a31e87SWenchao Xia     /* get parameters */
834*c8a83e85SKevin Wolf     g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
835e2a31e87SWenchao Xia 
836e2a31e87SWenchao Xia     device = action->blockdev_snapshot_sync->device;
837e2a31e87SWenchao Xia     new_image_file = action->blockdev_snapshot_sync->snapshot_file;
838e2a31e87SWenchao Xia     if (action->blockdev_snapshot_sync->has_format) {
839e2a31e87SWenchao Xia         format = action->blockdev_snapshot_sync->format;
840e2a31e87SWenchao Xia     }
841e2a31e87SWenchao Xia     if (action->blockdev_snapshot_sync->has_mode) {
842e2a31e87SWenchao Xia         mode = action->blockdev_snapshot_sync->mode;
843e2a31e87SWenchao Xia     }
844e2a31e87SWenchao Xia 
845e2a31e87SWenchao Xia     /* start processing */
8469b9877eeSWenchao Xia     drv = bdrv_find_format(format);
8479b9877eeSWenchao Xia     if (!drv) {
8489b9877eeSWenchao Xia         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
8499b9877eeSWenchao Xia         return;
8509b9877eeSWenchao Xia     }
8519b9877eeSWenchao Xia 
8529b9877eeSWenchao Xia     states->old_bs = bdrv_find(device);
8539b9877eeSWenchao Xia     if (!states->old_bs) {
8549b9877eeSWenchao Xia         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
8559b9877eeSWenchao Xia         return;
8569b9877eeSWenchao Xia     }
8579b9877eeSWenchao Xia 
8589b9877eeSWenchao Xia     if (!bdrv_is_inserted(states->old_bs)) {
8599b9877eeSWenchao Xia         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
8609b9877eeSWenchao Xia         return;
8619b9877eeSWenchao Xia     }
8629b9877eeSWenchao Xia 
8639b9877eeSWenchao Xia     if (bdrv_in_use(states->old_bs)) {
8649b9877eeSWenchao Xia         error_set(errp, QERR_DEVICE_IN_USE, device);
8659b9877eeSWenchao Xia         return;
8669b9877eeSWenchao Xia     }
8679b9877eeSWenchao Xia 
8689b9877eeSWenchao Xia     if (!bdrv_is_read_only(states->old_bs)) {
8699b9877eeSWenchao Xia         if (bdrv_flush(states->old_bs)) {
8709b9877eeSWenchao Xia             error_set(errp, QERR_IO_ERROR);
8719b9877eeSWenchao Xia             return;
8729b9877eeSWenchao Xia         }
8739b9877eeSWenchao Xia     }
8749b9877eeSWenchao Xia 
8759b9877eeSWenchao Xia     flags = states->old_bs->open_flags;
8769b9877eeSWenchao Xia 
8779b9877eeSWenchao Xia     proto_drv = bdrv_find_protocol(new_image_file);
8789b9877eeSWenchao Xia     if (!proto_drv) {
8799b9877eeSWenchao Xia         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
8809b9877eeSWenchao Xia         return;
8819b9877eeSWenchao Xia     }
8829b9877eeSWenchao Xia 
8839b9877eeSWenchao Xia     /* create new image w/backing file */
8849b9877eeSWenchao Xia     if (mode != NEW_IMAGE_MODE_EXISTING) {
8859b9877eeSWenchao Xia         bdrv_img_create(new_image_file, format,
8869b9877eeSWenchao Xia                         states->old_bs->filename,
8879b9877eeSWenchao Xia                         states->old_bs->drv->format_name,
8889b9877eeSWenchao Xia                         NULL, -1, flags, &local_err, false);
8899b9877eeSWenchao Xia         if (error_is_set(&local_err)) {
8909b9877eeSWenchao Xia             error_propagate(errp, local_err);
8919b9877eeSWenchao Xia             return;
8929b9877eeSWenchao Xia         }
8939b9877eeSWenchao Xia     }
8949b9877eeSWenchao Xia 
8959b9877eeSWenchao Xia     /* We will manually add the backing_hd field to the bs later */
8969b9877eeSWenchao Xia     states->new_bs = bdrv_new("");
8979b9877eeSWenchao Xia     /* TODO Inherit bs->options or only take explicit options with an
8989b9877eeSWenchao Xia      * extended QMP command? */
8999b9877eeSWenchao Xia     ret = bdrv_open(states->new_bs, new_image_file, NULL,
9009b9877eeSWenchao Xia                     flags | BDRV_O_NO_BACKING, drv);
9019b9877eeSWenchao Xia     if (ret != 0) {
9029b9877eeSWenchao Xia         error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
9039b9877eeSWenchao Xia     }
9049b9877eeSWenchao Xia }
9059b9877eeSWenchao Xia 
906ba0c86a3SWenchao Xia static void external_snapshot_commit(BlkTransactionStates *common)
9073b0047e8SWenchao Xia {
908ba0c86a3SWenchao Xia     ExternalSnapshotStates *states =
909ba0c86a3SWenchao Xia                              DO_UPCAST(ExternalSnapshotStates, common, common);
910ba0c86a3SWenchao Xia 
9113b0047e8SWenchao Xia     /* This removes our old bs from the bdrv_states, and adds the new bs */
9123b0047e8SWenchao Xia     bdrv_append(states->new_bs, states->old_bs);
9133b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
9143b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
9153b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
9163b0047e8SWenchao Xia     bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
9173b0047e8SWenchao Xia                 NULL);
9183b0047e8SWenchao Xia }
9193b0047e8SWenchao Xia 
920ba0c86a3SWenchao Xia static void external_snapshot_abort(BlkTransactionStates *common)
92196b86bf7SWenchao Xia {
922ba0c86a3SWenchao Xia     ExternalSnapshotStates *states =
923ba0c86a3SWenchao Xia                              DO_UPCAST(ExternalSnapshotStates, common, common);
92496b86bf7SWenchao Xia     if (states->new_bs) {
92596b86bf7SWenchao Xia         bdrv_delete(states->new_bs);
92696b86bf7SWenchao Xia     }
92796b86bf7SWenchao Xia }
92896b86bf7SWenchao Xia 
929ba0c86a3SWenchao Xia static const BdrvActionOps actions[] = {
930*c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
931ba0c86a3SWenchao Xia         .instance_size = sizeof(ExternalSnapshotStates),
932ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
933ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
934ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
935ba0c86a3SWenchao Xia     },
936ba0c86a3SWenchao Xia };
937ba0c86a3SWenchao Xia 
9388802d1fdSJeff Cody /*
9398802d1fdSJeff Cody  * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
9408802d1fdSJeff Cody  *  then we do not pivot any of the devices in the group, and abandon the
9418802d1fdSJeff Cody  *  snapshots
9428802d1fdSJeff Cody  */
943*c8a83e85SKevin Wolf void qmp_transaction(TransactionActionList *dev_list, Error **errp)
9448802d1fdSJeff Cody {
945*c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
94652e7c241SPaolo Bonzini     BlkTransactionStates *states, *next;
94743e17041SLuiz Capitulino     Error *local_err = NULL;
9488802d1fdSJeff Cody 
94952e7c241SPaolo Bonzini     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
9508802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
9518802d1fdSJeff Cody 
9528802d1fdSJeff Cody     /* drain all i/o before any snapshots */
9538802d1fdSJeff Cody     bdrv_drain_all();
9548802d1fdSJeff Cody 
9558802d1fdSJeff Cody     /* We don't do anything in this loop that commits us to the snapshot */
9568802d1fdSJeff Cody     while (NULL != dev_entry) {
957*c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
958ba0c86a3SWenchao Xia         const BdrvActionOps *ops;
95952e7c241SPaolo Bonzini 
9608802d1fdSJeff Cody         dev_info = dev_entry->value;
9618802d1fdSJeff Cody         dev_entry = dev_entry->next;
9628802d1fdSJeff Cody 
963ba0c86a3SWenchao Xia         assert(dev_info->kind < ARRAY_SIZE(actions));
964ba0c86a3SWenchao Xia 
965ba0c86a3SWenchao Xia         ops = &actions[dev_info->kind];
966ba0c86a3SWenchao Xia         states = g_malloc0(ops->instance_size);
967ba0c86a3SWenchao Xia         states->ops = ops;
968ba0c86a3SWenchao Xia         states->action = dev_info;
9698802d1fdSJeff Cody         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
9708802d1fdSJeff Cody 
971ba0c86a3SWenchao Xia         states->ops->prepare(states, &local_err);
9729b9877eeSWenchao Xia         if (error_is_set(&local_err)) {
9739b9877eeSWenchao Xia             error_propagate(errp, local_err);
9749b9877eeSWenchao Xia             goto delete_and_fail;
9759b9877eeSWenchao Xia         }
97652e7c241SPaolo Bonzini     }
9778802d1fdSJeff Cody 
9788802d1fdSJeff Cody     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
979ba0c86a3SWenchao Xia         states->ops->commit(states);
9808802d1fdSJeff Cody     }
9818802d1fdSJeff Cody 
9828802d1fdSJeff Cody     /* success */
9838802d1fdSJeff Cody     goto exit;
9848802d1fdSJeff Cody 
9858802d1fdSJeff Cody delete_and_fail:
9868802d1fdSJeff Cody     /*
9878802d1fdSJeff Cody     * failure, and it is all-or-none; abandon each new bs, and keep using
9888802d1fdSJeff Cody     * the original bs for all images
9898802d1fdSJeff Cody     */
9908802d1fdSJeff Cody     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
991ba0c86a3SWenchao Xia         if (states->ops->abort) {
992ba0c86a3SWenchao Xia             states->ops->abort(states);
993ba0c86a3SWenchao Xia         }
9948802d1fdSJeff Cody     }
9958802d1fdSJeff Cody exit:
996622d2419SPaolo Bonzini     QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
997ba0c86a3SWenchao Xia         if (states->ops->clean) {
998ba0c86a3SWenchao Xia             states->ops->clean(states);
999ba0c86a3SWenchao Xia         }
10008802d1fdSJeff Cody         g_free(states);
10018802d1fdSJeff Cody     }
10028802d1fdSJeff Cody }
10038802d1fdSJeff Cody 
10048802d1fdSJeff Cody 
100592d48558SLuiz Capitulino static void eject_device(BlockDriverState *bs, int force, Error **errp)
1006666daa68SMarkus Armbruster {
10072d3735d3SStefan Hajnoczi     if (bdrv_in_use(bs)) {
10082d3735d3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
10092d3735d3SStefan Hajnoczi         return;
10102d3735d3SStefan Hajnoczi     }
10112c6942faSMarkus Armbruster     if (!bdrv_dev_has_removable_media(bs)) {
101292d48558SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
101392d48558SLuiz Capitulino         return;
1014666daa68SMarkus Armbruster     }
101592d48558SLuiz Capitulino 
1016025ccaa7SPaolo Bonzini     if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1017025ccaa7SPaolo Bonzini         bdrv_dev_eject_request(bs, force);
1018025ccaa7SPaolo Bonzini         if (!force) {
101992d48558SLuiz Capitulino             error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
102092d48558SLuiz Capitulino             return;
1021666daa68SMarkus Armbruster         }
1022025ccaa7SPaolo Bonzini     }
102392d48558SLuiz Capitulino 
1024666daa68SMarkus Armbruster     bdrv_close(bs);
1025666daa68SMarkus Armbruster }
1026666daa68SMarkus Armbruster 
1027c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1028666daa68SMarkus Armbruster {
1029666daa68SMarkus Armbruster     BlockDriverState *bs;
1030666daa68SMarkus Armbruster 
1031c245b6a3SLuiz Capitulino     bs = bdrv_find(device);
1032666daa68SMarkus Armbruster     if (!bs) {
1033c245b6a3SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1034c245b6a3SLuiz Capitulino         return;
1035666daa68SMarkus Armbruster     }
103692d48558SLuiz Capitulino 
1037c245b6a3SLuiz Capitulino     eject_device(bs, force, errp);
1038666daa68SMarkus Armbruster }
1039666daa68SMarkus Armbruster 
1040a4dea8a9SLuiz Capitulino void qmp_block_passwd(const char *device, const char *password, Error **errp)
1041666daa68SMarkus Armbruster {
1042666daa68SMarkus Armbruster     BlockDriverState *bs;
1043666daa68SMarkus Armbruster     int err;
1044666daa68SMarkus Armbruster 
1045a4dea8a9SLuiz Capitulino     bs = bdrv_find(device);
1046666daa68SMarkus Armbruster     if (!bs) {
1047a4dea8a9SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1048a4dea8a9SLuiz Capitulino         return;
1049666daa68SMarkus Armbruster     }
1050666daa68SMarkus Armbruster 
1051a4dea8a9SLuiz Capitulino     err = bdrv_set_key(bs, password);
1052666daa68SMarkus Armbruster     if (err == -EINVAL) {
1053a4dea8a9SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1054a4dea8a9SLuiz Capitulino         return;
1055666daa68SMarkus Armbruster     } else if (err < 0) {
1056a4dea8a9SLuiz Capitulino         error_set(errp, QERR_INVALID_PASSWORD);
1057a4dea8a9SLuiz Capitulino         return;
1058666daa68SMarkus Armbruster     }
1059666daa68SMarkus Armbruster }
1060666daa68SMarkus Armbruster 
1061333a96ecSLuiz Capitulino static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1062333a96ecSLuiz Capitulino                                     int bdrv_flags, BlockDriver *drv,
1063333a96ecSLuiz Capitulino                                     const char *password, Error **errp)
1064333a96ecSLuiz Capitulino {
1065de9c0cecSKevin Wolf     if (bdrv_open(bs, filename, NULL, bdrv_flags, drv) < 0) {
1066333a96ecSLuiz Capitulino         error_set(errp, QERR_OPEN_FILE_FAILED, filename);
1067333a96ecSLuiz Capitulino         return;
1068333a96ecSLuiz Capitulino     }
1069333a96ecSLuiz Capitulino 
1070333a96ecSLuiz Capitulino     if (bdrv_key_required(bs)) {
1071333a96ecSLuiz Capitulino         if (password) {
1072333a96ecSLuiz Capitulino             if (bdrv_set_key(bs, password) < 0) {
1073333a96ecSLuiz Capitulino                 error_set(errp, QERR_INVALID_PASSWORD);
1074333a96ecSLuiz Capitulino             }
1075333a96ecSLuiz Capitulino         } else {
1076333a96ecSLuiz Capitulino             error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1077333a96ecSLuiz Capitulino                       bdrv_get_encrypted_filename(bs));
1078333a96ecSLuiz Capitulino         }
1079333a96ecSLuiz Capitulino     } else if (password) {
1080333a96ecSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1081333a96ecSLuiz Capitulino     }
1082333a96ecSLuiz Capitulino }
1083333a96ecSLuiz Capitulino 
1084333a96ecSLuiz Capitulino void qmp_change_blockdev(const char *device, const char *filename,
1085333a96ecSLuiz Capitulino                          bool has_format, const char *format, Error **errp)
1086666daa68SMarkus Armbruster {
1087666daa68SMarkus Armbruster     BlockDriverState *bs;
1088666daa68SMarkus Armbruster     BlockDriver *drv = NULL;
1089666daa68SMarkus Armbruster     int bdrv_flags;
109092d48558SLuiz Capitulino     Error *err = NULL;
1091666daa68SMarkus Armbruster 
1092666daa68SMarkus Armbruster     bs = bdrv_find(device);
1093666daa68SMarkus Armbruster     if (!bs) {
1094333a96ecSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1095333a96ecSLuiz Capitulino         return;
1096666daa68SMarkus Armbruster     }
1097333a96ecSLuiz Capitulino 
1098333a96ecSLuiz Capitulino     if (format) {
1099333a96ecSLuiz Capitulino         drv = bdrv_find_whitelisted_format(format);
1100666daa68SMarkus Armbruster         if (!drv) {
1101333a96ecSLuiz Capitulino             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1102333a96ecSLuiz Capitulino             return;
1103666daa68SMarkus Armbruster         }
1104666daa68SMarkus Armbruster     }
1105333a96ecSLuiz Capitulino 
110692d48558SLuiz Capitulino     eject_device(bs, 0, &err);
110792d48558SLuiz Capitulino     if (error_is_set(&err)) {
1108333a96ecSLuiz Capitulino         error_propagate(errp, err);
1109333a96ecSLuiz Capitulino         return;
1110666daa68SMarkus Armbruster     }
1111333a96ecSLuiz Capitulino 
1112528f7663SMarkus Armbruster     bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1113199630b6SBlue Swirl     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1114333a96ecSLuiz Capitulino 
1115333a96ecSLuiz Capitulino     qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1116666daa68SMarkus Armbruster }
11179063f814SRyan Harper 
1118727f005eSZhi Yong Wu /* throttling disk I/O limits */
111980047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
112080047da5SLuiz Capitulino                                int64_t bps_wr, int64_t iops, int64_t iops_rd,
112180047da5SLuiz Capitulino                                int64_t iops_wr, Error **errp)
1122727f005eSZhi Yong Wu {
1123727f005eSZhi Yong Wu     BlockIOLimit io_limits;
1124727f005eSZhi Yong Wu     BlockDriverState *bs;
1125727f005eSZhi Yong Wu 
112680047da5SLuiz Capitulino     bs = bdrv_find(device);
1127727f005eSZhi Yong Wu     if (!bs) {
112880047da5SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
112980047da5SLuiz Capitulino         return;
1130727f005eSZhi Yong Wu     }
1131727f005eSZhi Yong Wu 
113280047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
113380047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_READ]  = bps_rd;
113480047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
113580047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
113680047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
113780047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
1138727f005eSZhi Yong Wu 
1139c546194fSStefan Hajnoczi     if (!do_check_io_limits(&io_limits, errp)) {
114080047da5SLuiz Capitulino         return;
1141727f005eSZhi Yong Wu     }
1142727f005eSZhi Yong Wu 
1143727f005eSZhi Yong Wu     bs->io_limits = io_limits;
1144727f005eSZhi Yong Wu 
1145727f005eSZhi Yong Wu     if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1146727f005eSZhi Yong Wu         bdrv_io_limits_enable(bs);
1147727f005eSZhi Yong Wu     } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1148727f005eSZhi Yong Wu         bdrv_io_limits_disable(bs);
1149727f005eSZhi Yong Wu     } else {
1150727f005eSZhi Yong Wu         if (bs->block_timer) {
1151727f005eSZhi Yong Wu             qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1152727f005eSZhi Yong Wu         }
1153727f005eSZhi Yong Wu     }
1154727f005eSZhi Yong Wu }
1155727f005eSZhi Yong Wu 
11569063f814SRyan Harper int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
11579063f814SRyan Harper {
11589063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
11599063f814SRyan Harper     BlockDriverState *bs;
11609063f814SRyan Harper 
11619063f814SRyan Harper     bs = bdrv_find(id);
11629063f814SRyan Harper     if (!bs) {
11639063f814SRyan Harper         qerror_report(QERR_DEVICE_NOT_FOUND, id);
11649063f814SRyan Harper         return -1;
11659063f814SRyan Harper     }
11668591675fSMarcelo Tosatti     if (bdrv_in_use(bs)) {
11678591675fSMarcelo Tosatti         qerror_report(QERR_DEVICE_IN_USE, id);
11688591675fSMarcelo Tosatti         return -1;
11698591675fSMarcelo Tosatti     }
11709063f814SRyan Harper 
11719063f814SRyan Harper     /* quiesce block driver; prevent further io */
1172922453bcSStefan Hajnoczi     bdrv_drain_all();
11739063f814SRyan Harper     bdrv_flush(bs);
11749063f814SRyan Harper     bdrv_close(bs);
11759063f814SRyan Harper 
1176fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
1177d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
1178d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
1179d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
1180d22b2f41SRyan Harper      */
1181fa879d62SMarkus Armbruster     if (bdrv_get_attached_dev(bs)) {
1182d22b2f41SRyan Harper         bdrv_make_anon(bs);
1183d22b2f41SRyan Harper     } else {
11849063f814SRyan Harper         drive_uninit(drive_get_by_blockdev(bs));
1185d22b2f41SRyan Harper     }
11869063f814SRyan Harper 
11879063f814SRyan Harper     return 0;
11889063f814SRyan Harper }
11896d4a2b3aSChristoph Hellwig 
11905e7caacbSLuiz Capitulino void qmp_block_resize(const char *device, int64_t size, Error **errp)
11916d4a2b3aSChristoph Hellwig {
11926d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
11938732901eSKevin Wolf     int ret;
11946d4a2b3aSChristoph Hellwig 
11956d4a2b3aSChristoph Hellwig     bs = bdrv_find(device);
11966d4a2b3aSChristoph Hellwig     if (!bs) {
11975e7caacbSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
11985e7caacbSLuiz Capitulino         return;
11996d4a2b3aSChristoph Hellwig     }
12006d4a2b3aSChristoph Hellwig 
12016d4a2b3aSChristoph Hellwig     if (size < 0) {
1202939a1cc3SStefan Hajnoczi         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
12035e7caacbSLuiz Capitulino         return;
12046d4a2b3aSChristoph Hellwig     }
12056d4a2b3aSChristoph Hellwig 
120692b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
120792b7a08dSPeter Lieven     bdrv_drain_all();
120892b7a08dSPeter Lieven 
12098732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
12108732901eSKevin Wolf     switch (ret) {
1211939a1cc3SStefan Hajnoczi     case 0:
1212939a1cc3SStefan Hajnoczi         break;
1213939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
1214939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1215939a1cc3SStefan Hajnoczi         break;
1216939a1cc3SStefan Hajnoczi     case -ENOTSUP:
1217939a1cc3SStefan Hajnoczi         error_set(errp, QERR_UNSUPPORTED);
1218939a1cc3SStefan Hajnoczi         break;
1219939a1cc3SStefan Hajnoczi     case -EACCES:
1220939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1221939a1cc3SStefan Hajnoczi         break;
1222939a1cc3SStefan Hajnoczi     case -EBUSY:
1223939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IN_USE, device);
1224939a1cc3SStefan Hajnoczi         break;
1225939a1cc3SStefan Hajnoczi     default:
12268732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
1227939a1cc3SStefan Hajnoczi         break;
12286d4a2b3aSChristoph Hellwig     }
12296d4a2b3aSChristoph Hellwig }
123012bd451fSStefan Hajnoczi 
12319abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
123212bd451fSStefan Hajnoczi {
123312bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
123412bd451fSStefan Hajnoczi     QObject *obj;
123512bd451fSStefan Hajnoczi 
12369abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
123712bd451fSStefan Hajnoczi 
123812bd451fSStefan Hajnoczi     assert(bs->job);
123912bd451fSStefan Hajnoczi     obj = qobject_from_block_job(bs->job);
124012bd451fSStefan Hajnoczi     if (ret < 0) {
124112bd451fSStefan Hajnoczi         QDict *dict = qobject_to_qdict(obj);
124212bd451fSStefan Hajnoczi         qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
124312bd451fSStefan Hajnoczi     }
124412bd451fSStefan Hajnoczi 
1245370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
1246370521a1SStefan Hajnoczi         monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1247370521a1SStefan Hajnoczi     } else {
124812bd451fSStefan Hajnoczi         monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1249370521a1SStefan Hajnoczi     }
125012bd451fSStefan Hajnoczi     qobject_decref(obj);
1251aa398a5cSStefan Hajnoczi 
1252aa398a5cSStefan Hajnoczi     drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
125312bd451fSStefan Hajnoczi }
125412bd451fSStefan Hajnoczi 
125512bd451fSStefan Hajnoczi void qmp_block_stream(const char *device, bool has_base,
12561d809098SPaolo Bonzini                       const char *base, bool has_speed, int64_t speed,
12571d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
12581d809098SPaolo Bonzini                       Error **errp)
125912bd451fSStefan Hajnoczi {
126012bd451fSStefan Hajnoczi     BlockDriverState *bs;
1261c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
1262fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
126312bd451fSStefan Hajnoczi 
12641d809098SPaolo Bonzini     if (!has_on_error) {
12651d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
12661d809098SPaolo Bonzini     }
12671d809098SPaolo Bonzini 
126812bd451fSStefan Hajnoczi     bs = bdrv_find(device);
126912bd451fSStefan Hajnoczi     if (!bs) {
127012bd451fSStefan Hajnoczi         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
127112bd451fSStefan Hajnoczi         return;
127212bd451fSStefan Hajnoczi     }
127312bd451fSStefan Hajnoczi 
127412bd451fSStefan Hajnoczi     if (base) {
1275c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
1276c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
1277c8c3080fSMarcelo Tosatti             error_set(errp, QERR_BASE_NOT_FOUND, base);
127812bd451fSStefan Hajnoczi             return;
127912bd451fSStefan Hajnoczi         }
1280c8c3080fSMarcelo Tosatti     }
128112bd451fSStefan Hajnoczi 
1282c83c66c3SStefan Hajnoczi     stream_start(bs, base_bs, base, has_speed ? speed : 0,
12831d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
1284fd7f8c65SStefan Hajnoczi     if (error_is_set(&local_err)) {
1285fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
128612bd451fSStefan Hajnoczi         return;
128712bd451fSStefan Hajnoczi     }
128812bd451fSStefan Hajnoczi 
1289aa398a5cSStefan Hajnoczi     /* Grab a reference so hotplug does not delete the BlockDriverState from
1290aa398a5cSStefan Hajnoczi      * underneath us.
1291aa398a5cSStefan Hajnoczi      */
1292aa398a5cSStefan Hajnoczi     drive_get_ref(drive_get_by_blockdev(bs));
1293aa398a5cSStefan Hajnoczi 
129412bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
129512bd451fSStefan Hajnoczi }
12962d47c6e9SStefan Hajnoczi 
1297ed61fc10SJeff Cody void qmp_block_commit(const char *device,
1298ed61fc10SJeff Cody                       bool has_base, const char *base, const char *top,
1299ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
1300ed61fc10SJeff Cody                       Error **errp)
1301ed61fc10SJeff Cody {
1302ed61fc10SJeff Cody     BlockDriverState *bs;
1303ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
1304ed61fc10SJeff Cody     Error *local_err = NULL;
1305ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
1306ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
1307ed61fc10SJeff Cody      */
130892aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1309ed61fc10SJeff Cody 
1310ed61fc10SJeff Cody     /* drain all i/o before commits */
1311ed61fc10SJeff Cody     bdrv_drain_all();
1312ed61fc10SJeff Cody 
1313ed61fc10SJeff Cody     bs = bdrv_find(device);
1314ed61fc10SJeff Cody     if (!bs) {
1315ed61fc10SJeff Cody         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1316ed61fc10SJeff Cody         return;
1317ed61fc10SJeff Cody     }
1318ed61fc10SJeff Cody 
1319ed61fc10SJeff Cody     /* default top_bs is the active layer */
1320ed61fc10SJeff Cody     top_bs = bs;
1321ed61fc10SJeff Cody 
1322ed61fc10SJeff Cody     if (top) {
1323ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
1324ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
1325ed61fc10SJeff Cody         }
1326ed61fc10SJeff Cody     }
1327ed61fc10SJeff Cody 
1328ed61fc10SJeff Cody     if (top_bs == NULL) {
1329ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1330ed61fc10SJeff Cody         return;
1331ed61fc10SJeff Cody     }
1332ed61fc10SJeff Cody 
1333d5208c45SJeff Cody     if (has_base && base) {
1334d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
1335d5208c45SJeff Cody     } else {
1336d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
1337d5208c45SJeff Cody     }
1338d5208c45SJeff Cody 
1339d5208c45SJeff Cody     if (base_bs == NULL) {
1340d5208c45SJeff Cody         error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1341d5208c45SJeff Cody         return;
1342d5208c45SJeff Cody     }
1343d5208c45SJeff Cody 
1344ed61fc10SJeff Cody     commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1345ed61fc10SJeff Cody                 &local_err);
1346ed61fc10SJeff Cody     if (local_err != NULL) {
1347ed61fc10SJeff Cody         error_propagate(errp, local_err);
1348ed61fc10SJeff Cody         return;
1349ed61fc10SJeff Cody     }
1350ed61fc10SJeff Cody     /* Grab a reference so hotplug does not delete the BlockDriverState from
1351ed61fc10SJeff Cody      * underneath us.
1352ed61fc10SJeff Cody      */
1353ed61fc10SJeff Cody     drive_get_ref(drive_get_by_blockdev(bs));
1354ed61fc10SJeff Cody }
1355ed61fc10SJeff Cody 
135608e4ed6cSPaolo Bonzini #define DEFAULT_MIRROR_BUF_SIZE   (10 << 20)
135708e4ed6cSPaolo Bonzini 
1358d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target,
1359d9b902dbSPaolo Bonzini                       bool has_format, const char *format,
1360d9b902dbSPaolo Bonzini                       enum MirrorSyncMode sync,
1361d9b902dbSPaolo Bonzini                       bool has_mode, enum NewImageMode mode,
1362b952b558SPaolo Bonzini                       bool has_speed, int64_t speed,
1363eee13dfeSPaolo Bonzini                       bool has_granularity, uint32_t granularity,
136408e4ed6cSPaolo Bonzini                       bool has_buf_size, int64_t buf_size,
1365b952b558SPaolo Bonzini                       bool has_on_source_error, BlockdevOnError on_source_error,
1366b952b558SPaolo Bonzini                       bool has_on_target_error, BlockdevOnError on_target_error,
1367b952b558SPaolo Bonzini                       Error **errp)
1368d9b902dbSPaolo Bonzini {
1369d9b902dbSPaolo Bonzini     BlockDriverState *bs;
1370d9b902dbSPaolo Bonzini     BlockDriverState *source, *target_bs;
1371d9b902dbSPaolo Bonzini     BlockDriver *proto_drv;
1372d9b902dbSPaolo Bonzini     BlockDriver *drv = NULL;
1373d9b902dbSPaolo Bonzini     Error *local_err = NULL;
1374d9b902dbSPaolo Bonzini     int flags;
1375d9b902dbSPaolo Bonzini     uint64_t size;
1376d9b902dbSPaolo Bonzini     int ret;
1377d9b902dbSPaolo Bonzini 
1378d9b902dbSPaolo Bonzini     if (!has_speed) {
1379d9b902dbSPaolo Bonzini         speed = 0;
1380d9b902dbSPaolo Bonzini     }
1381b952b558SPaolo Bonzini     if (!has_on_source_error) {
1382b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1383b952b558SPaolo Bonzini     }
1384b952b558SPaolo Bonzini     if (!has_on_target_error) {
1385b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1386b952b558SPaolo Bonzini     }
1387d9b902dbSPaolo Bonzini     if (!has_mode) {
1388d9b902dbSPaolo Bonzini         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1389d9b902dbSPaolo Bonzini     }
1390eee13dfeSPaolo Bonzini     if (!has_granularity) {
1391eee13dfeSPaolo Bonzini         granularity = 0;
1392eee13dfeSPaolo Bonzini     }
139308e4ed6cSPaolo Bonzini     if (!has_buf_size) {
139408e4ed6cSPaolo Bonzini         buf_size = DEFAULT_MIRROR_BUF_SIZE;
139508e4ed6cSPaolo Bonzini     }
139608e4ed6cSPaolo Bonzini 
1397eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1398eee13dfeSPaolo Bonzini         error_set(errp, QERR_INVALID_PARAMETER, device);
1399eee13dfeSPaolo Bonzini         return;
1400eee13dfeSPaolo Bonzini     }
1401eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
1402eee13dfeSPaolo Bonzini         error_set(errp, QERR_INVALID_PARAMETER, device);
1403eee13dfeSPaolo Bonzini         return;
1404eee13dfeSPaolo Bonzini     }
1405d9b902dbSPaolo Bonzini 
1406d9b902dbSPaolo Bonzini     bs = bdrv_find(device);
1407d9b902dbSPaolo Bonzini     if (!bs) {
1408d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1409d9b902dbSPaolo Bonzini         return;
1410d9b902dbSPaolo Bonzini     }
1411d9b902dbSPaolo Bonzini 
1412d9b902dbSPaolo Bonzini     if (!bdrv_is_inserted(bs)) {
1413d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1414d9b902dbSPaolo Bonzini         return;
1415d9b902dbSPaolo Bonzini     }
1416d9b902dbSPaolo Bonzini 
1417d9b902dbSPaolo Bonzini     if (!has_format) {
1418d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1419d9b902dbSPaolo Bonzini     }
1420d9b902dbSPaolo Bonzini     if (format) {
1421d9b902dbSPaolo Bonzini         drv = bdrv_find_format(format);
1422d9b902dbSPaolo Bonzini         if (!drv) {
1423d9b902dbSPaolo Bonzini             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1424d9b902dbSPaolo Bonzini             return;
1425d9b902dbSPaolo Bonzini         }
1426d9b902dbSPaolo Bonzini     }
1427d9b902dbSPaolo Bonzini 
1428d9b902dbSPaolo Bonzini     if (bdrv_in_use(bs)) {
1429d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_IN_USE, device);
1430d9b902dbSPaolo Bonzini         return;
1431d9b902dbSPaolo Bonzini     }
1432d9b902dbSPaolo Bonzini 
1433d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
1434d9b902dbSPaolo Bonzini     source = bs->backing_hd;
1435d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1436d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
1437d9b902dbSPaolo Bonzini     }
1438d9b902dbSPaolo Bonzini 
1439d9b902dbSPaolo Bonzini     proto_drv = bdrv_find_protocol(target);
1440d9b902dbSPaolo Bonzini     if (!proto_drv) {
1441d9b902dbSPaolo Bonzini         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1442d9b902dbSPaolo Bonzini         return;
1443d9b902dbSPaolo Bonzini     }
1444d9b902dbSPaolo Bonzini 
144586899072SVishvananda Ishaya     bdrv_get_geometry(bs, &size);
144686899072SVishvananda Ishaya     size *= 512;
1447d9b902dbSPaolo Bonzini     if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1448d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
1449d9b902dbSPaolo Bonzini         assert(format && drv);
1450cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
1451f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
1452d9b902dbSPaolo Bonzini     } else {
1453d9b902dbSPaolo Bonzini         switch (mode) {
1454d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
1455d9b902dbSPaolo Bonzini             ret = 0;
1456d9b902dbSPaolo Bonzini             break;
1457d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1458d9b902dbSPaolo Bonzini             /* create new image with backing file */
1459cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
1460d9b902dbSPaolo Bonzini                             source->filename,
1461d9b902dbSPaolo Bonzini                             source->drv->format_name,
1462f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
1463d9b902dbSPaolo Bonzini             break;
1464d9b902dbSPaolo Bonzini         default:
1465d9b902dbSPaolo Bonzini             abort();
1466d9b902dbSPaolo Bonzini         }
1467d9b902dbSPaolo Bonzini     }
1468d9b902dbSPaolo Bonzini 
1469cf8f2426SLuiz Capitulino     if (error_is_set(&local_err)) {
1470cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
1471d9b902dbSPaolo Bonzini         return;
1472d9b902dbSPaolo Bonzini     }
1473d9b902dbSPaolo Bonzini 
1474b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
1475b812f671SPaolo Bonzini      * file.
1476b812f671SPaolo Bonzini      */
1477d9b902dbSPaolo Bonzini     target_bs = bdrv_new("");
1478de9c0cecSKevin Wolf     ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
1479d9b902dbSPaolo Bonzini 
1480d9b902dbSPaolo Bonzini     if (ret < 0) {
1481d9b902dbSPaolo Bonzini         bdrv_delete(target_bs);
1482d9b902dbSPaolo Bonzini         error_set(errp, QERR_OPEN_FILE_FAILED, target);
1483d9b902dbSPaolo Bonzini         return;
1484d9b902dbSPaolo Bonzini     }
1485d9b902dbSPaolo Bonzini 
148608e4ed6cSPaolo Bonzini     mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1487eee13dfeSPaolo Bonzini                  on_source_error, on_target_error,
1488b952b558SPaolo Bonzini                  block_job_cb, bs, &local_err);
1489d9b902dbSPaolo Bonzini     if (local_err != NULL) {
1490d9b902dbSPaolo Bonzini         bdrv_delete(target_bs);
1491d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
1492d9b902dbSPaolo Bonzini         return;
1493d9b902dbSPaolo Bonzini     }
1494d9b902dbSPaolo Bonzini 
1495d9b902dbSPaolo Bonzini     /* Grab a reference so hotplug does not delete the BlockDriverState from
1496d9b902dbSPaolo Bonzini      * underneath us.
1497d9b902dbSPaolo Bonzini      */
1498d9b902dbSPaolo Bonzini     drive_get_ref(drive_get_by_blockdev(bs));
1499d9b902dbSPaolo Bonzini }
1500d9b902dbSPaolo Bonzini 
15012d47c6e9SStefan Hajnoczi static BlockJob *find_block_job(const char *device)
15022d47c6e9SStefan Hajnoczi {
15032d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
15042d47c6e9SStefan Hajnoczi 
15052d47c6e9SStefan Hajnoczi     bs = bdrv_find(device);
15062d47c6e9SStefan Hajnoczi     if (!bs || !bs->job) {
15072d47c6e9SStefan Hajnoczi         return NULL;
15082d47c6e9SStefan Hajnoczi     }
15092d47c6e9SStefan Hajnoczi     return bs->job;
15102d47c6e9SStefan Hajnoczi }
15112d47c6e9SStefan Hajnoczi 
1512882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
15132d47c6e9SStefan Hajnoczi {
15142d47c6e9SStefan Hajnoczi     BlockJob *job = find_block_job(device);
15152d47c6e9SStefan Hajnoczi 
15162d47c6e9SStefan Hajnoczi     if (!job) {
15177ef15070SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
15182d47c6e9SStefan Hajnoczi         return;
15192d47c6e9SStefan Hajnoczi     }
15202d47c6e9SStefan Hajnoczi 
1521882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
15222d47c6e9SStefan Hajnoczi }
1523370521a1SStefan Hajnoczi 
15246e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
15256e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
15266e37fb81SPaolo Bonzini {
15276e37fb81SPaolo Bonzini     BlockJob *job = find_block_job(device);
15286e37fb81SPaolo Bonzini 
15296e37fb81SPaolo Bonzini     if (!has_force) {
15306e37fb81SPaolo Bonzini         force = false;
15316e37fb81SPaolo Bonzini     }
15326e37fb81SPaolo Bonzini 
15336e37fb81SPaolo Bonzini     if (!job) {
15346e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
15356e37fb81SPaolo Bonzini         return;
15366e37fb81SPaolo Bonzini     }
15376e37fb81SPaolo Bonzini     if (job->paused && !force) {
15386e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
15396e37fb81SPaolo Bonzini         return;
15406e37fb81SPaolo Bonzini     }
15416e37fb81SPaolo Bonzini 
15426e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
15436e37fb81SPaolo Bonzini     block_job_cancel(job);
15446e37fb81SPaolo Bonzini }
15456e37fb81SPaolo Bonzini 
15466e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
1547370521a1SStefan Hajnoczi {
1548370521a1SStefan Hajnoczi     BlockJob *job = find_block_job(device);
1549370521a1SStefan Hajnoczi 
1550370521a1SStefan Hajnoczi     if (!job) {
15517ef15070SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1552370521a1SStefan Hajnoczi         return;
1553370521a1SStefan Hajnoczi     }
15546e37fb81SPaolo Bonzini 
15556e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
15566e37fb81SPaolo Bonzini     block_job_pause(job);
15576e37fb81SPaolo Bonzini }
15586e37fb81SPaolo Bonzini 
15596e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
15606e37fb81SPaolo Bonzini {
15616e37fb81SPaolo Bonzini     BlockJob *job = find_block_job(device);
15626e37fb81SPaolo Bonzini 
15636e37fb81SPaolo Bonzini     if (!job) {
15646e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
15658acc72a4SPaolo Bonzini         return;
15668acc72a4SPaolo Bonzini     }
1567370521a1SStefan Hajnoczi 
15686e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
15696e37fb81SPaolo Bonzini     block_job_resume(job);
1570370521a1SStefan Hajnoczi }
1571fb5458cdSStefan Hajnoczi 
1572aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
1573aeae883bSPaolo Bonzini {
1574aeae883bSPaolo Bonzini     BlockJob *job = find_block_job(device);
1575aeae883bSPaolo Bonzini 
1576aeae883bSPaolo Bonzini     if (!job) {
1577aeae883bSPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1578aeae883bSPaolo Bonzini         return;
1579aeae883bSPaolo Bonzini     }
1580aeae883bSPaolo Bonzini 
1581aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
1582aeae883bSPaolo Bonzini     block_job_complete(job, errp);
1583aeae883bSPaolo Bonzini }
1584aeae883bSPaolo Bonzini 
1585fb5458cdSStefan Hajnoczi static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1586fb5458cdSStefan Hajnoczi {
1587fb5458cdSStefan Hajnoczi     BlockJobInfoList **prev = opaque;
1588fb5458cdSStefan Hajnoczi     BlockJob *job = bs->job;
1589fb5458cdSStefan Hajnoczi 
1590fb5458cdSStefan Hajnoczi     if (job) {
159130e628b7SPaolo Bonzini         BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
159230e628b7SPaolo Bonzini         elem->value = block_job_query(bs->job);
1593fb5458cdSStefan Hajnoczi         (*prev)->next = elem;
1594fb5458cdSStefan Hajnoczi         *prev = elem;
1595fb5458cdSStefan Hajnoczi     }
1596fb5458cdSStefan Hajnoczi }
1597fb5458cdSStefan Hajnoczi 
1598fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1599fb5458cdSStefan Hajnoczi {
1600fb5458cdSStefan Hajnoczi     /* Dummy is a fake list element for holding the head pointer */
1601fb5458cdSStefan Hajnoczi     BlockJobInfoList dummy = {};
1602fb5458cdSStefan Hajnoczi     BlockJobInfoList *prev = &dummy;
1603fb5458cdSStefan Hajnoczi     bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1604fb5458cdSStefan Hajnoczi     return dummy.next;
1605fb5458cdSStefan Hajnoczi }
16064d454574SPaolo Bonzini 
16070006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
16084d454574SPaolo Bonzini     .name = "drive",
16090006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
16104d454574SPaolo Bonzini     .desc = {
16114d454574SPaolo Bonzini         {
16124d454574SPaolo Bonzini             .name = "bus",
16134d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16144d454574SPaolo Bonzini             .help = "bus number",
16154d454574SPaolo Bonzini         },{
16164d454574SPaolo Bonzini             .name = "unit",
16174d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16184d454574SPaolo Bonzini             .help = "unit number (i.e. lun for scsi)",
16194d454574SPaolo Bonzini         },{
16204d454574SPaolo Bonzini             .name = "if",
16214d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16224d454574SPaolo Bonzini             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
16234d454574SPaolo Bonzini         },{
16244d454574SPaolo Bonzini             .name = "index",
16254d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16264d454574SPaolo Bonzini             .help = "index number",
16274d454574SPaolo Bonzini         },{
16284d454574SPaolo Bonzini             .name = "cyls",
16294d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16304d454574SPaolo Bonzini             .help = "number of cylinders (ide disk geometry)",
16314d454574SPaolo Bonzini         },{
16324d454574SPaolo Bonzini             .name = "heads",
16334d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16344d454574SPaolo Bonzini             .help = "number of heads (ide disk geometry)",
16354d454574SPaolo Bonzini         },{
16364d454574SPaolo Bonzini             .name = "secs",
16374d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16384d454574SPaolo Bonzini             .help = "number of sectors (ide disk geometry)",
16394d454574SPaolo Bonzini         },{
16404d454574SPaolo Bonzini             .name = "trans",
16414d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16424d454574SPaolo Bonzini             .help = "chs translation (auto, lba. none)",
16434d454574SPaolo Bonzini         },{
16444d454574SPaolo Bonzini             .name = "media",
16454d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16464d454574SPaolo Bonzini             .help = "media type (disk, cdrom)",
16474d454574SPaolo Bonzini         },{
16484d454574SPaolo Bonzini             .name = "snapshot",
16494d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
16504d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
16514d454574SPaolo Bonzini         },{
16524d454574SPaolo Bonzini             .name = "file",
16534d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16544d454574SPaolo Bonzini             .help = "disk image",
16554d454574SPaolo Bonzini         },{
1656a9384affSPaolo Bonzini             .name = "discard",
1657a9384affSPaolo Bonzini             .type = QEMU_OPT_STRING,
1658a9384affSPaolo Bonzini             .help = "discard operation (ignore/off, unmap/on)",
1659a9384affSPaolo Bonzini         },{
16604d454574SPaolo Bonzini             .name = "cache",
16614d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16624d454574SPaolo Bonzini             .help = "host cache usage (none, writeback, writethrough, "
16634d454574SPaolo Bonzini                     "directsync, unsafe)",
16644d454574SPaolo Bonzini         },{
16654d454574SPaolo Bonzini             .name = "aio",
16664d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16674d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
16684d454574SPaolo Bonzini         },{
16694d454574SPaolo Bonzini             .name = "format",
16704d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16714d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
16724d454574SPaolo Bonzini         },{
16734d454574SPaolo Bonzini             .name = "serial",
16744d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16754d454574SPaolo Bonzini             .help = "disk serial number",
16764d454574SPaolo Bonzini         },{
16774d454574SPaolo Bonzini             .name = "rerror",
16784d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16794d454574SPaolo Bonzini             .help = "read error action",
16804d454574SPaolo Bonzini         },{
16814d454574SPaolo Bonzini             .name = "werror",
16824d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16834d454574SPaolo Bonzini             .help = "write error action",
16844d454574SPaolo Bonzini         },{
16854d454574SPaolo Bonzini             .name = "addr",
16864d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
16874d454574SPaolo Bonzini             .help = "pci address (virtio only)",
16884d454574SPaolo Bonzini         },{
16894d454574SPaolo Bonzini             .name = "readonly",
16904d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
16914d454574SPaolo Bonzini             .help = "open drive file as read-only",
16924d454574SPaolo Bonzini         },{
16934d454574SPaolo Bonzini             .name = "iops",
16944d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16954d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
16964d454574SPaolo Bonzini         },{
16974d454574SPaolo Bonzini             .name = "iops_rd",
16984d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
16994d454574SPaolo Bonzini             .help = "limit read operations per second",
17004d454574SPaolo Bonzini         },{
17014d454574SPaolo Bonzini             .name = "iops_wr",
17024d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
17034d454574SPaolo Bonzini             .help = "limit write operations per second",
17044d454574SPaolo Bonzini         },{
17054d454574SPaolo Bonzini             .name = "bps",
17064d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
17074d454574SPaolo Bonzini             .help = "limit total bytes per second",
17084d454574SPaolo Bonzini         },{
17094d454574SPaolo Bonzini             .name = "bps_rd",
17104d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
17114d454574SPaolo Bonzini             .help = "limit read bytes per second",
17124d454574SPaolo Bonzini         },{
17134d454574SPaolo Bonzini             .name = "bps_wr",
17144d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
17154d454574SPaolo Bonzini             .help = "limit write bytes per second",
17164d454574SPaolo Bonzini         },{
17174d454574SPaolo Bonzini             .name = "copy-on-read",
17184d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
17194d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
17204d454574SPaolo Bonzini         },{
17214d454574SPaolo Bonzini             .name = "boot",
17224d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
17234d454574SPaolo Bonzini             .help = "(deprecated, ignored)",
17244d454574SPaolo Bonzini         },
17254d454574SPaolo Bonzini         { /* end of list */ }
17264d454574SPaolo Bonzini     },
17274d454574SPaolo Bonzini };
17280006383eSKevin Wolf 
17290006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
17300006383eSKevin Wolf     .name = "drive",
17310006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
17320006383eSKevin Wolf     .desc = {
17338ec7d390SKevin Wolf         {
17348ec7d390SKevin Wolf             .name = "bus",
17358ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17368ec7d390SKevin Wolf             .help = "bus number",
17378ec7d390SKevin Wolf         },{
17388ec7d390SKevin Wolf             .name = "unit",
17398ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17408ec7d390SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
17418ec7d390SKevin Wolf         },{
17428ec7d390SKevin Wolf             .name = "if",
17438ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17448ec7d390SKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
17458ec7d390SKevin Wolf         },{
17468ec7d390SKevin Wolf             .name = "index",
17478ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17488ec7d390SKevin Wolf             .help = "index number",
17498ec7d390SKevin Wolf         },{
17508ec7d390SKevin Wolf             .name = "cyls",
17518ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17528ec7d390SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
17538ec7d390SKevin Wolf         },{
17548ec7d390SKevin Wolf             .name = "heads",
17558ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17568ec7d390SKevin Wolf             .help = "number of heads (ide disk geometry)",
17578ec7d390SKevin Wolf         },{
17588ec7d390SKevin Wolf             .name = "secs",
17598ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
17608ec7d390SKevin Wolf             .help = "number of sectors (ide disk geometry)",
17618ec7d390SKevin Wolf         },{
17628ec7d390SKevin Wolf             .name = "trans",
17638ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17648ec7d390SKevin Wolf             .help = "chs translation (auto, lba. none)",
17658ec7d390SKevin Wolf         },{
17668ec7d390SKevin Wolf             .name = "media",
17678ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17688ec7d390SKevin Wolf             .help = "media type (disk, cdrom)",
17698ec7d390SKevin Wolf         },{
17708ec7d390SKevin Wolf             .name = "snapshot",
17718ec7d390SKevin Wolf             .type = QEMU_OPT_BOOL,
17728ec7d390SKevin Wolf             .help = "enable/disable snapshot mode",
17738ec7d390SKevin Wolf         },{
17748ec7d390SKevin Wolf             .name = "file",
17758ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17768ec7d390SKevin Wolf             .help = "disk image",
17778ec7d390SKevin Wolf         },{
17788ec7d390SKevin Wolf             .name = "discard",
17798ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17808ec7d390SKevin Wolf             .help = "discard operation (ignore/off, unmap/on)",
17818ec7d390SKevin Wolf         },{
17828ec7d390SKevin Wolf             .name = "cache",
17838ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17848ec7d390SKevin Wolf             .help = "host cache usage (none, writeback, writethrough, "
17858ec7d390SKevin Wolf                     "directsync, unsafe)",
17868ec7d390SKevin Wolf         },{
17878ec7d390SKevin Wolf             .name = "aio",
17888ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17898ec7d390SKevin Wolf             .help = "host AIO implementation (threads, native)",
17908ec7d390SKevin Wolf         },{
17918ec7d390SKevin Wolf             .name = "format",
17928ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17938ec7d390SKevin Wolf             .help = "disk format (raw, qcow2, ...)",
17948ec7d390SKevin Wolf         },{
17958ec7d390SKevin Wolf             .name = "serial",
17968ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
17978ec7d390SKevin Wolf             .help = "disk serial number",
17988ec7d390SKevin Wolf         },{
17998ec7d390SKevin Wolf             .name = "rerror",
18008ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
18018ec7d390SKevin Wolf             .help = "read error action",
18028ec7d390SKevin Wolf         },{
18038ec7d390SKevin Wolf             .name = "werror",
18048ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
18058ec7d390SKevin Wolf             .help = "write error action",
18068ec7d390SKevin Wolf         },{
18078ec7d390SKevin Wolf             .name = "addr",
18088ec7d390SKevin Wolf             .type = QEMU_OPT_STRING,
18098ec7d390SKevin Wolf             .help = "pci address (virtio only)",
18108ec7d390SKevin Wolf         },{
18118ec7d390SKevin Wolf             .name = "readonly",
18128ec7d390SKevin Wolf             .type = QEMU_OPT_BOOL,
18138ec7d390SKevin Wolf             .help = "open drive file as read-only",
18148ec7d390SKevin Wolf         },{
18158ec7d390SKevin Wolf             .name = "iops",
18168ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18178ec7d390SKevin Wolf             .help = "limit total I/O operations per second",
18188ec7d390SKevin Wolf         },{
18198ec7d390SKevin Wolf             .name = "iops_rd",
18208ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18218ec7d390SKevin Wolf             .help = "limit read operations per second",
18228ec7d390SKevin Wolf         },{
18238ec7d390SKevin Wolf             .name = "iops_wr",
18248ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18258ec7d390SKevin Wolf             .help = "limit write operations per second",
18268ec7d390SKevin Wolf         },{
18278ec7d390SKevin Wolf             .name = "bps",
18288ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18298ec7d390SKevin Wolf             .help = "limit total bytes per second",
18308ec7d390SKevin Wolf         },{
18318ec7d390SKevin Wolf             .name = "bps_rd",
18328ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18338ec7d390SKevin Wolf             .help = "limit read bytes per second",
18348ec7d390SKevin Wolf         },{
18358ec7d390SKevin Wolf             .name = "bps_wr",
18368ec7d390SKevin Wolf             .type = QEMU_OPT_NUMBER,
18378ec7d390SKevin Wolf             .help = "limit write bytes per second",
18388ec7d390SKevin Wolf         },{
18398ec7d390SKevin Wolf             .name = "copy-on-read",
18408ec7d390SKevin Wolf             .type = QEMU_OPT_BOOL,
18418ec7d390SKevin Wolf             .help = "copy read data from backing file into image file",
18428ec7d390SKevin Wolf         },{
18438ec7d390SKevin Wolf             .name = "boot",
18448ec7d390SKevin Wolf             .type = QEMU_OPT_BOOL,
18458ec7d390SKevin Wolf             .help = "(deprecated, ignored)",
18468ec7d390SKevin Wolf         },
18470006383eSKevin Wolf         { /* end of list */ }
18480006383eSKevin Wolf     },
18490006383eSKevin Wolf };
1850