xref: /openbmc/qemu/blockdev.c (revision c546194f260fb3e391193cb8cc33505618077ecb)
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.
8666daa68SMarkus Armbruster  */
9666daa68SMarkus Armbruster 
109c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
112b584959SMarkus Armbruster #include "hw/block-common.h"
12737e150eSPaolo Bonzini #include "block/blockjob.h"
1383c9089eSPaolo Bonzini #include "monitor/monitor.h"
147b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h"
151de7afc9SPaolo Bonzini #include "qemu/option.h"
161de7afc9SPaolo Bonzini #include "qemu/config-file.h"
177b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
189c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
19737e150eSPaolo Bonzini #include "block/block_int.h"
20a4dea8a9SLuiz Capitulino #include "qmp-commands.h"
2112bd451fSStefan Hajnoczi #include "trace.h"
229c17d615SPaolo Bonzini #include "sysemu/arch_init.h"
23666daa68SMarkus Armbruster 
24c9b62a7eSMarkus Armbruster static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
25666daa68SMarkus Armbruster 
261960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
271960966dSMarkus Armbruster     [IF_NONE] = "none",
281960966dSMarkus Armbruster     [IF_IDE] = "ide",
291960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
301960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
311960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
321960966dSMarkus Armbruster     [IF_MTD] = "mtd",
331960966dSMarkus Armbruster     [IF_SD] = "sd",
341960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
351960966dSMarkus Armbruster     [IF_XEN] = "xen",
361960966dSMarkus Armbruster };
371960966dSMarkus Armbruster 
381960966dSMarkus Armbruster static const int if_max_devs[IF_COUNT] = {
3927d6bf40SMarkus Armbruster     /*
4027d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
4127d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
4227d6bf40SMarkus Armbruster      *
4327d6bf40SMarkus Armbruster      * All controllers used to imlement if=T drives need to support
4427d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
4527d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
4627d6bf40SMarkus Armbruster      * values.
4727d6bf40SMarkus Armbruster      *
4827d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
4927d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
5027d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
5127d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
5227d6bf40SMarkus Armbruster      */
5327d6bf40SMarkus Armbruster     [IF_IDE] = 2,
5427d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
551960966dSMarkus Armbruster };
561960966dSMarkus Armbruster 
5714bafc54SMarkus Armbruster /*
5814bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
5914bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
6014bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
6114bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
6214bafc54SMarkus Armbruster  * when deletion is actually safe.
6314bafc54SMarkus Armbruster  */
6414bafc54SMarkus Armbruster void blockdev_mark_auto_del(BlockDriverState *bs)
6514bafc54SMarkus Armbruster {
6614bafc54SMarkus Armbruster     DriveInfo *dinfo = drive_get_by_blockdev(bs);
6714bafc54SMarkus Armbruster 
6812bde0eeSPaolo Bonzini     if (bs->job) {
6912bde0eeSPaolo Bonzini         block_job_cancel(bs->job);
7012bde0eeSPaolo Bonzini     }
710fc0f1faSRyan Harper     if (dinfo) {
7214bafc54SMarkus Armbruster         dinfo->auto_del = 1;
7314bafc54SMarkus Armbruster     }
740fc0f1faSRyan Harper }
7514bafc54SMarkus Armbruster 
7614bafc54SMarkus Armbruster void blockdev_auto_del(BlockDriverState *bs)
7714bafc54SMarkus Armbruster {
7814bafc54SMarkus Armbruster     DriveInfo *dinfo = drive_get_by_blockdev(bs);
7914bafc54SMarkus Armbruster 
800fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
8184fb3925SMarcelo Tosatti         drive_put_ref(dinfo);
8214bafc54SMarkus Armbruster     }
8314bafc54SMarkus Armbruster }
8414bafc54SMarkus Armbruster 
85505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
86505a7fb1SMarkus Armbruster {
87505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
88505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
89505a7fb1SMarkus Armbruster }
90505a7fb1SMarkus Armbruster 
91505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
92505a7fb1SMarkus Armbruster {
93505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
94505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
95505a7fb1SMarkus Armbruster }
96505a7fb1SMarkus Armbruster 
972292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
982292ddaeSMarkus Armbruster {
992292ddaeSMarkus Armbruster     return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
1002292ddaeSMarkus Armbruster }
1012292ddaeSMarkus Armbruster 
1022292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1035645b0f4SMarkus Armbruster                     const char *optstr)
104666daa68SMarkus Armbruster {
105666daa68SMarkus Armbruster     QemuOpts *opts;
1062292ddaeSMarkus Armbruster     char buf[32];
107666daa68SMarkus Armbruster 
1082292ddaeSMarkus Armbruster     opts = drive_def(optstr);
109666daa68SMarkus Armbruster     if (!opts) {
110666daa68SMarkus Armbruster         return NULL;
111666daa68SMarkus Armbruster     }
1122292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
1132292ddaeSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type]);
1142292ddaeSMarkus Armbruster     }
1152292ddaeSMarkus Armbruster     if (index >= 0) {
1162292ddaeSMarkus Armbruster         snprintf(buf, sizeof(buf), "%d", index);
1172292ddaeSMarkus Armbruster         qemu_opt_set(opts, "index", buf);
1182292ddaeSMarkus Armbruster     }
119666daa68SMarkus Armbruster     if (file)
120666daa68SMarkus Armbruster         qemu_opt_set(opts, "file", file);
121666daa68SMarkus Armbruster     return opts;
122666daa68SMarkus Armbruster }
123666daa68SMarkus Armbruster 
124666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
125666daa68SMarkus Armbruster {
126666daa68SMarkus Armbruster     DriveInfo *dinfo;
127666daa68SMarkus Armbruster 
128666daa68SMarkus Armbruster     /* seek interface, bus and unit */
129666daa68SMarkus Armbruster 
130666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
131666daa68SMarkus Armbruster         if (dinfo->type == type &&
132666daa68SMarkus Armbruster 	    dinfo->bus == bus &&
133666daa68SMarkus Armbruster 	    dinfo->unit == unit)
134666daa68SMarkus Armbruster             return dinfo;
135666daa68SMarkus Armbruster     }
136666daa68SMarkus Armbruster 
137666daa68SMarkus Armbruster     return NULL;
138666daa68SMarkus Armbruster }
139666daa68SMarkus Armbruster 
140f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
141f1bd51acSMarkus Armbruster {
142f1bd51acSMarkus Armbruster     return drive_get(type,
143f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
144f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
145f1bd51acSMarkus Armbruster }
146f1bd51acSMarkus Armbruster 
147666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
148666daa68SMarkus Armbruster {
149666daa68SMarkus Armbruster     int max_bus;
150666daa68SMarkus Armbruster     DriveInfo *dinfo;
151666daa68SMarkus Armbruster 
152666daa68SMarkus Armbruster     max_bus = -1;
153666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
154666daa68SMarkus Armbruster         if(dinfo->type == type &&
155666daa68SMarkus Armbruster            dinfo->bus > max_bus)
156666daa68SMarkus Armbruster             max_bus = dinfo->bus;
157666daa68SMarkus Armbruster     }
158666daa68SMarkus Armbruster     return max_bus;
159666daa68SMarkus Armbruster }
160666daa68SMarkus Armbruster 
16113839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
16213839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
16313839974SMarkus Armbruster    appropriate bus.  */
16413839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
16513839974SMarkus Armbruster {
16613839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
16713839974SMarkus Armbruster 
16813839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
16913839974SMarkus Armbruster }
17013839974SMarkus Armbruster 
171e4700e59SMarkus Armbruster DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
172666daa68SMarkus Armbruster {
173666daa68SMarkus Armbruster     DriveInfo *dinfo;
174666daa68SMarkus Armbruster 
175666daa68SMarkus Armbruster     QTAILQ_FOREACH(dinfo, &drives, next) {
176e4700e59SMarkus Armbruster         if (dinfo->bdrv == bs) {
177e4700e59SMarkus Armbruster             return dinfo;
178666daa68SMarkus Armbruster         }
179e4700e59SMarkus Armbruster     }
180e4700e59SMarkus Armbruster     return NULL;
181666daa68SMarkus Armbruster }
182666daa68SMarkus Armbruster 
183666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
184666daa68SMarkus Armbruster {
185807105a7SMarkus Armbruster     error_printf(" %s", name);
186666daa68SMarkus Armbruster }
187666daa68SMarkus Armbruster 
18884fb3925SMarcelo Tosatti static void drive_uninit(DriveInfo *dinfo)
189666daa68SMarkus Armbruster {
190666daa68SMarkus Armbruster     qemu_opts_del(dinfo->opts);
191666daa68SMarkus Armbruster     bdrv_delete(dinfo->bdrv);
1927267c094SAnthony Liguori     g_free(dinfo->id);
193666daa68SMarkus Armbruster     QTAILQ_REMOVE(&drives, dinfo, next);
1947267c094SAnthony Liguori     g_free(dinfo);
195666daa68SMarkus Armbruster }
196666daa68SMarkus Armbruster 
19784fb3925SMarcelo Tosatti void drive_put_ref(DriveInfo *dinfo)
19884fb3925SMarcelo Tosatti {
19984fb3925SMarcelo Tosatti     assert(dinfo->refcount);
20084fb3925SMarcelo Tosatti     if (--dinfo->refcount == 0) {
20184fb3925SMarcelo Tosatti         drive_uninit(dinfo);
20284fb3925SMarcelo Tosatti     }
20384fb3925SMarcelo Tosatti }
20484fb3925SMarcelo Tosatti 
20584fb3925SMarcelo Tosatti void drive_get_ref(DriveInfo *dinfo)
20684fb3925SMarcelo Tosatti {
20784fb3925SMarcelo Tosatti     dinfo->refcount++;
20884fb3925SMarcelo Tosatti }
20984fb3925SMarcelo Tosatti 
210aa398a5cSStefan Hajnoczi typedef struct {
211aa398a5cSStefan Hajnoczi     QEMUBH *bh;
212aa398a5cSStefan Hajnoczi     DriveInfo *dinfo;
213aa398a5cSStefan Hajnoczi } DrivePutRefBH;
214aa398a5cSStefan Hajnoczi 
215aa398a5cSStefan Hajnoczi static void drive_put_ref_bh(void *opaque)
216aa398a5cSStefan Hajnoczi {
217aa398a5cSStefan Hajnoczi     DrivePutRefBH *s = opaque;
218aa398a5cSStefan Hajnoczi 
219aa398a5cSStefan Hajnoczi     drive_put_ref(s->dinfo);
220aa398a5cSStefan Hajnoczi     qemu_bh_delete(s->bh);
221aa398a5cSStefan Hajnoczi     g_free(s);
222aa398a5cSStefan Hajnoczi }
223aa398a5cSStefan Hajnoczi 
224aa398a5cSStefan Hajnoczi /*
225aa398a5cSStefan Hajnoczi  * Release a drive reference in a BH
226aa398a5cSStefan Hajnoczi  *
227aa398a5cSStefan Hajnoczi  * It is not possible to use drive_put_ref() from a callback function when the
228aa398a5cSStefan Hajnoczi  * callers still need the drive.  In such cases we schedule a BH to release the
229aa398a5cSStefan Hajnoczi  * reference.
230aa398a5cSStefan Hajnoczi  */
231aa398a5cSStefan Hajnoczi static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
232aa398a5cSStefan Hajnoczi {
233aa398a5cSStefan Hajnoczi     DrivePutRefBH *s;
234aa398a5cSStefan Hajnoczi 
235aa398a5cSStefan Hajnoczi     s = g_new(DrivePutRefBH, 1);
236aa398a5cSStefan Hajnoczi     s->bh = qemu_bh_new(drive_put_ref_bh, s);
237aa398a5cSStefan Hajnoczi     s->dinfo = dinfo;
238aa398a5cSStefan Hajnoczi     qemu_bh_schedule(s->bh);
239aa398a5cSStefan Hajnoczi }
240aa398a5cSStefan Hajnoczi 
2411ceee0d5SPaolo Bonzini static int parse_block_error_action(const char *buf, bool is_read)
242666daa68SMarkus Armbruster {
243666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
24492aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
245666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
24692aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
247666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
24892aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
249666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
25092aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
251666daa68SMarkus Armbruster     } else {
252807105a7SMarkus Armbruster         error_report("'%s' invalid %s error action",
253666daa68SMarkus Armbruster                      buf, is_read ? "read" : "write");
254666daa68SMarkus Armbruster         return -1;
255666daa68SMarkus Armbruster     }
256666daa68SMarkus Armbruster }
257666daa68SMarkus Armbruster 
258*c546194fSStefan Hajnoczi static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
2590563e191SZhi Yong Wu {
2600563e191SZhi Yong Wu     bool bps_flag;
2610563e191SZhi Yong Wu     bool iops_flag;
2620563e191SZhi Yong Wu 
2630563e191SZhi Yong Wu     assert(io_limits);
2640563e191SZhi Yong Wu 
2650563e191SZhi Yong Wu     bps_flag  = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
2660563e191SZhi Yong Wu                  && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
2670563e191SZhi Yong Wu                  || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
2680563e191SZhi Yong Wu     iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
2690563e191SZhi Yong Wu                  && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
2700563e191SZhi Yong Wu                  || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
2710563e191SZhi Yong Wu     if (bps_flag || iops_flag) {
272*c546194fSStefan Hajnoczi         error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
273*c546194fSStefan Hajnoczi                          "cannot be used at the same time");
2740563e191SZhi Yong Wu         return false;
2750563e191SZhi Yong Wu     }
2760563e191SZhi Yong Wu 
2770563e191SZhi Yong Wu     return true;
2780563e191SZhi Yong Wu }
2790563e191SZhi Yong Wu 
2802d0d2837SChristian Borntraeger DriveInfo *drive_init(QemuOpts *opts, BlockInterfaceType block_default_type)
281666daa68SMarkus Armbruster {
282666daa68SMarkus Armbruster     const char *buf;
283666daa68SMarkus Armbruster     const char *file = NULL;
284666daa68SMarkus Armbruster     const char *serial;
285666daa68SMarkus Armbruster     const char *mediastr = "";
286666daa68SMarkus Armbruster     BlockInterfaceType type;
287666daa68SMarkus Armbruster     enum { MEDIA_DISK, MEDIA_CDROM } media;
288666daa68SMarkus Armbruster     int bus_id, unit_id;
289666daa68SMarkus Armbruster     int cyls, heads, secs, translation;
290666daa68SMarkus Armbruster     BlockDriver *drv = NULL;
291666daa68SMarkus Armbruster     int max_devs;
292666daa68SMarkus Armbruster     int index;
293666daa68SMarkus Armbruster     int ro = 0;
294666daa68SMarkus Armbruster     int bdrv_flags = 0;
295666daa68SMarkus Armbruster     int on_read_error, on_write_error;
296666daa68SMarkus Armbruster     const char *devaddr;
297666daa68SMarkus Armbruster     DriveInfo *dinfo;
2980563e191SZhi Yong Wu     BlockIOLimit io_limits;
299666daa68SMarkus Armbruster     int snapshot = 0;
300fb0490f6SStefan Hajnoczi     bool copy_on_read;
301666daa68SMarkus Armbruster     int ret;
302*c546194fSStefan Hajnoczi     Error *error = NULL;
303666daa68SMarkus Armbruster 
304666daa68SMarkus Armbruster     translation = BIOS_ATA_TRANSLATION_AUTO;
305666daa68SMarkus Armbruster     media = MEDIA_DISK;
306666daa68SMarkus Armbruster 
307666daa68SMarkus Armbruster     /* extract parameters */
308666daa68SMarkus Armbruster     bus_id  = qemu_opt_get_number(opts, "bus", 0);
309666daa68SMarkus Armbruster     unit_id = qemu_opt_get_number(opts, "unit", -1);
310666daa68SMarkus Armbruster     index   = qemu_opt_get_number(opts, "index", -1);
311666daa68SMarkus Armbruster 
312666daa68SMarkus Armbruster     cyls  = qemu_opt_get_number(opts, "cyls", 0);
313666daa68SMarkus Armbruster     heads = qemu_opt_get_number(opts, "heads", 0);
314666daa68SMarkus Armbruster     secs  = qemu_opt_get_number(opts, "secs", 0);
315666daa68SMarkus Armbruster 
316666daa68SMarkus Armbruster     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
317666daa68SMarkus Armbruster     ro = qemu_opt_get_bool(opts, "readonly", 0);
318fb0490f6SStefan Hajnoczi     copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
319666daa68SMarkus Armbruster 
320666daa68SMarkus Armbruster     file = qemu_opt_get(opts, "file");
321666daa68SMarkus Armbruster     serial = qemu_opt_get(opts, "serial");
322666daa68SMarkus Armbruster 
323666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "if")) != NULL) {
3241960966dSMarkus Armbruster         for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
3251960966dSMarkus Armbruster             ;
3261960966dSMarkus Armbruster         if (type == IF_COUNT) {
327807105a7SMarkus Armbruster             error_report("unsupported bus type '%s'", buf);
328666daa68SMarkus Armbruster             return NULL;
329666daa68SMarkus Armbruster 	}
3302d3999feSLuiz Capitulino     } else {
3312d0d2837SChristian Borntraeger         type = block_default_type;
332666daa68SMarkus Armbruster     }
3332d3999feSLuiz Capitulino 
3341960966dSMarkus Armbruster     max_devs = if_max_devs[type];
335666daa68SMarkus Armbruster 
336666daa68SMarkus Armbruster     if (cyls || heads || secs) {
337aaea3f36SMarkus Armbruster         if (cyls < 1) {
338807105a7SMarkus Armbruster             error_report("invalid physical cyls number");
339666daa68SMarkus Armbruster 	    return NULL;
340666daa68SMarkus Armbruster 	}
341aaea3f36SMarkus Armbruster         if (heads < 1) {
342807105a7SMarkus Armbruster             error_report("invalid physical heads number");
343666daa68SMarkus Armbruster 	    return NULL;
344666daa68SMarkus Armbruster 	}
345aaea3f36SMarkus Armbruster         if (secs < 1) {
346807105a7SMarkus Armbruster             error_report("invalid physical secs number");
347666daa68SMarkus Armbruster 	    return NULL;
348666daa68SMarkus Armbruster 	}
349666daa68SMarkus Armbruster     }
350666daa68SMarkus Armbruster 
351666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
352666daa68SMarkus Armbruster         if (!cyls) {
353807105a7SMarkus Armbruster             error_report("'%s' trans must be used with cyls, heads and secs",
354666daa68SMarkus Armbruster                          buf);
355666daa68SMarkus Armbruster             return NULL;
356666daa68SMarkus Armbruster         }
357666daa68SMarkus Armbruster         if (!strcmp(buf, "none"))
358666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_NONE;
359666daa68SMarkus Armbruster         else if (!strcmp(buf, "lba"))
360666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_LBA;
361666daa68SMarkus Armbruster         else if (!strcmp(buf, "auto"))
362666daa68SMarkus Armbruster             translation = BIOS_ATA_TRANSLATION_AUTO;
363666daa68SMarkus Armbruster 	else {
364807105a7SMarkus Armbruster             error_report("'%s' invalid translation type", buf);
365666daa68SMarkus Armbruster 	    return NULL;
366666daa68SMarkus Armbruster 	}
367666daa68SMarkus Armbruster     }
368666daa68SMarkus Armbruster 
369666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "media")) != NULL) {
370666daa68SMarkus Armbruster         if (!strcmp(buf, "disk")) {
371666daa68SMarkus Armbruster 	    media = MEDIA_DISK;
372666daa68SMarkus Armbruster 	} else if (!strcmp(buf, "cdrom")) {
373666daa68SMarkus Armbruster             if (cyls || secs || heads) {
374e7ff8f0eSLuiz Capitulino                 error_report("CHS can't be set with media=%s", buf);
375666daa68SMarkus Armbruster 	        return NULL;
376666daa68SMarkus Armbruster             }
377666daa68SMarkus Armbruster 	    media = MEDIA_CDROM;
378666daa68SMarkus Armbruster 	} else {
379807105a7SMarkus Armbruster 	    error_report("'%s' invalid media", buf);
380666daa68SMarkus Armbruster 	    return NULL;
381666daa68SMarkus Armbruster 	}
382666daa68SMarkus Armbruster     }
383666daa68SMarkus Armbruster 
3841f212b9dSPaolo Bonzini     bdrv_flags |= BDRV_O_CACHE_WB;
385666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
386c3993cdcSStefan Hajnoczi         if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
387807105a7SMarkus Armbruster             error_report("invalid cache option");
388666daa68SMarkus Armbruster             return NULL;
389666daa68SMarkus Armbruster         }
390666daa68SMarkus Armbruster     }
391666daa68SMarkus Armbruster 
392666daa68SMarkus Armbruster #ifdef CONFIG_LINUX_AIO
393666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
394666daa68SMarkus Armbruster         if (!strcmp(buf, "native")) {
395666daa68SMarkus Armbruster             bdrv_flags |= BDRV_O_NATIVE_AIO;
396666daa68SMarkus Armbruster         } else if (!strcmp(buf, "threads")) {
397666daa68SMarkus Armbruster             /* this is the default */
398666daa68SMarkus Armbruster         } else {
399807105a7SMarkus Armbruster            error_report("invalid aio option");
400666daa68SMarkus Armbruster            return NULL;
401666daa68SMarkus Armbruster         }
402666daa68SMarkus Armbruster     }
403666daa68SMarkus Armbruster #endif
404666daa68SMarkus Armbruster 
405666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
406c8057f95SPeter Maydell         if (is_help_option(buf)) {
407807105a7SMarkus Armbruster             error_printf("Supported formats:");
408666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
409807105a7SMarkus Armbruster             error_printf("\n");
410666daa68SMarkus Armbruster             return NULL;
411666daa68SMarkus Armbruster         }
412666daa68SMarkus Armbruster         drv = bdrv_find_whitelisted_format(buf);
413666daa68SMarkus Armbruster         if (!drv) {
414807105a7SMarkus Armbruster             error_report("'%s' invalid format", buf);
415666daa68SMarkus Armbruster             return NULL;
416666daa68SMarkus Armbruster         }
417666daa68SMarkus Armbruster     }
418666daa68SMarkus Armbruster 
4190563e191SZhi Yong Wu     /* disk I/O throttling */
4200563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =
4210563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps", 0);
4220563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_READ]   =
4230563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps_rd", 0);
4240563e191SZhi Yong Wu     io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =
4250563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "bps_wr", 0);
4260563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
4270563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops", 0);
4280563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_READ]  =
4290563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops_rd", 0);
4300563e191SZhi Yong Wu     io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
4310563e191SZhi Yong Wu                            qemu_opt_get_number(opts, "iops_wr", 0);
4320563e191SZhi Yong Wu 
433*c546194fSStefan Hajnoczi     if (!do_check_io_limits(&io_limits, &error)) {
434*c546194fSStefan Hajnoczi         error_report("%s", error_get_pretty(error));
435*c546194fSStefan Hajnoczi         error_free(error);
4360563e191SZhi Yong Wu         return NULL;
4370563e191SZhi Yong Wu     }
4380563e191SZhi Yong Wu 
4390d92d17aSJan Kiszka     if (qemu_opt_get(opts, "boot") != NULL) {
4400d92d17aSJan Kiszka         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
4410d92d17aSJan Kiszka                 "ignored. Future versions will reject this parameter. Please "
4420d92d17aSJan Kiszka                 "update your scripts.\n");
4430d92d17aSJan Kiszka     }
4440d92d17aSJan Kiszka 
44592aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
446666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
447666daa68SMarkus Armbruster         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
448807105a7SMarkus Armbruster             error_report("werror is not supported by this bus type");
449666daa68SMarkus Armbruster             return NULL;
450666daa68SMarkus Armbruster         }
451666daa68SMarkus Armbruster 
452666daa68SMarkus Armbruster         on_write_error = parse_block_error_action(buf, 0);
453666daa68SMarkus Armbruster         if (on_write_error < 0) {
454666daa68SMarkus Armbruster             return NULL;
455666daa68SMarkus Armbruster         }
456666daa68SMarkus Armbruster     }
457666daa68SMarkus Armbruster 
45892aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
459666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
4605dba48a8SKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
461807105a7SMarkus Armbruster             error_report("rerror is not supported by this bus type");
462666daa68SMarkus Armbruster             return NULL;
463666daa68SMarkus Armbruster         }
464666daa68SMarkus Armbruster 
465666daa68SMarkus Armbruster         on_read_error = parse_block_error_action(buf, 1);
466666daa68SMarkus Armbruster         if (on_read_error < 0) {
467666daa68SMarkus Armbruster             return NULL;
468666daa68SMarkus Armbruster         }
469666daa68SMarkus Armbruster     }
470666daa68SMarkus Armbruster 
471666daa68SMarkus Armbruster     if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
472666daa68SMarkus Armbruster         if (type != IF_VIRTIO) {
473807105a7SMarkus Armbruster             error_report("addr is not supported by this bus type");
474666daa68SMarkus Armbruster             return NULL;
475666daa68SMarkus Armbruster         }
476666daa68SMarkus Armbruster     }
477666daa68SMarkus Armbruster 
478666daa68SMarkus Armbruster     /* compute bus and unit according index */
479666daa68SMarkus Armbruster 
480666daa68SMarkus Armbruster     if (index != -1) {
481666daa68SMarkus Armbruster         if (bus_id != 0 || unit_id != -1) {
482807105a7SMarkus Armbruster             error_report("index cannot be used with bus and unit");
483666daa68SMarkus Armbruster             return NULL;
484666daa68SMarkus Armbruster         }
485505a7fb1SMarkus Armbruster         bus_id = drive_index_to_bus_id(type, index);
486505a7fb1SMarkus Armbruster         unit_id = drive_index_to_unit_id(type, index);
487666daa68SMarkus Armbruster     }
488666daa68SMarkus Armbruster 
489666daa68SMarkus Armbruster     /* if user doesn't specify a unit_id,
490666daa68SMarkus Armbruster      * try to find the first free
491666daa68SMarkus Armbruster      */
492666daa68SMarkus Armbruster 
493666daa68SMarkus Armbruster     if (unit_id == -1) {
494666daa68SMarkus Armbruster        unit_id = 0;
495666daa68SMarkus Armbruster        while (drive_get(type, bus_id, unit_id) != NULL) {
496666daa68SMarkus Armbruster            unit_id++;
497666daa68SMarkus Armbruster            if (max_devs && unit_id >= max_devs) {
498666daa68SMarkus Armbruster                unit_id -= max_devs;
499666daa68SMarkus Armbruster                bus_id++;
500666daa68SMarkus Armbruster            }
501666daa68SMarkus Armbruster        }
502666daa68SMarkus Armbruster     }
503666daa68SMarkus Armbruster 
504666daa68SMarkus Armbruster     /* check unit id */
505666daa68SMarkus Armbruster 
506666daa68SMarkus Armbruster     if (max_devs && unit_id >= max_devs) {
507807105a7SMarkus Armbruster         error_report("unit %d too big (max is %d)",
508666daa68SMarkus Armbruster                      unit_id, max_devs - 1);
509666daa68SMarkus Armbruster         return NULL;
510666daa68SMarkus Armbruster     }
511666daa68SMarkus Armbruster 
512666daa68SMarkus Armbruster     /*
5134e5d9b57SMarkus Armbruster      * catch multiple definitions
514666daa68SMarkus Armbruster      */
515666daa68SMarkus Armbruster 
516666daa68SMarkus Armbruster     if (drive_get(type, bus_id, unit_id) != NULL) {
5174e5d9b57SMarkus Armbruster         error_report("drive with bus=%d, unit=%d (index=%d) exists",
5184e5d9b57SMarkus Armbruster                      bus_id, unit_id, index);
519666daa68SMarkus Armbruster         return NULL;
520666daa68SMarkus Armbruster     }
521666daa68SMarkus Armbruster 
522666daa68SMarkus Armbruster     /* init */
523666daa68SMarkus Armbruster 
5247267c094SAnthony Liguori     dinfo = g_malloc0(sizeof(*dinfo));
525666daa68SMarkus Armbruster     if ((buf = qemu_opts_id(opts)) != NULL) {
5267267c094SAnthony Liguori         dinfo->id = g_strdup(buf);
527666daa68SMarkus Armbruster     } else {
528666daa68SMarkus Armbruster         /* no id supplied -> create one */
5297267c094SAnthony Liguori         dinfo->id = g_malloc0(32);
530666daa68SMarkus Armbruster         if (type == IF_IDE || type == IF_SCSI)
531666daa68SMarkus Armbruster             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
532666daa68SMarkus Armbruster         if (max_devs)
533666daa68SMarkus Armbruster             snprintf(dinfo->id, 32, "%s%i%s%i",
53479d21d5bSMarkus Armbruster                      if_name[type], bus_id, mediastr, unit_id);
535666daa68SMarkus Armbruster         else
536666daa68SMarkus Armbruster             snprintf(dinfo->id, 32, "%s%s%i",
53779d21d5bSMarkus Armbruster                      if_name[type], mediastr, unit_id);
538666daa68SMarkus Armbruster     }
539666daa68SMarkus Armbruster     dinfo->bdrv = bdrv_new(dinfo->id);
54080dd1aaeSKevin Shanahan     dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
54180dd1aaeSKevin Shanahan     dinfo->bdrv->read_only = ro;
542666daa68SMarkus Armbruster     dinfo->devaddr = devaddr;
543666daa68SMarkus Armbruster     dinfo->type = type;
544666daa68SMarkus Armbruster     dinfo->bus = bus_id;
545666daa68SMarkus Armbruster     dinfo->unit = unit_id;
546317bb412SMarkus Armbruster     dinfo->cyls = cyls;
547317bb412SMarkus Armbruster     dinfo->heads = heads;
548317bb412SMarkus Armbruster     dinfo->secs = secs;
549317bb412SMarkus Armbruster     dinfo->trans = translation;
550666daa68SMarkus Armbruster     dinfo->opts = opts;
55184fb3925SMarcelo Tosatti     dinfo->refcount = 1;
552577d0a38SMarkus Armbruster     dinfo->serial = serial;
553666daa68SMarkus Armbruster     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
554666daa68SMarkus Armbruster 
555abd7f68dSMarkus Armbruster     bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
556abd7f68dSMarkus Armbruster 
5570563e191SZhi Yong Wu     /* disk I/O throttling */
5580563e191SZhi Yong Wu     bdrv_set_io_limits(dinfo->bdrv, &io_limits);
5590563e191SZhi Yong Wu 
560666daa68SMarkus Armbruster     switch(type) {
561666daa68SMarkus Armbruster     case IF_IDE:
562666daa68SMarkus Armbruster     case IF_SCSI:
563666daa68SMarkus Armbruster     case IF_XEN:
564666daa68SMarkus Armbruster     case IF_NONE:
5652b584959SMarkus Armbruster         dinfo->media_cd = media == MEDIA_CDROM;
566666daa68SMarkus Armbruster         break;
567666daa68SMarkus Armbruster     case IF_SD:
568666daa68SMarkus Armbruster     case IF_FLOPPY:
569666daa68SMarkus Armbruster     case IF_PFLASH:
570666daa68SMarkus Armbruster     case IF_MTD:
571666daa68SMarkus Armbruster         break;
572666daa68SMarkus Armbruster     case IF_VIRTIO:
573666daa68SMarkus Armbruster         /* add virtio block device */
574e478b448SDong Xu Wang         opts = qemu_opts_create_nofail(qemu_find_opts("device"));
575eeb9c1b5SAnthony Liguori         if (arch_type == QEMU_ARCH_S390X) {
576eeb9c1b5SAnthony Liguori             qemu_opt_set(opts, "driver", "virtio-blk-s390");
577eeb9c1b5SAnthony Liguori         } else {
578eeb9c1b5SAnthony Liguori             qemu_opt_set(opts, "driver", "virtio-blk-pci");
579eeb9c1b5SAnthony Liguori         }
580666daa68SMarkus Armbruster         qemu_opt_set(opts, "drive", dinfo->id);
581666daa68SMarkus Armbruster         if (devaddr)
582666daa68SMarkus Armbruster             qemu_opt_set(opts, "addr", devaddr);
583666daa68SMarkus Armbruster         break;
5842292ddaeSMarkus Armbruster     default:
585666daa68SMarkus Armbruster         abort();
586666daa68SMarkus Armbruster     }
587dd5b0d71SMarkus Armbruster     if (!file || !*file) {
588319ae529SMarkus Armbruster         return dinfo;
589666daa68SMarkus Armbruster     }
590666daa68SMarkus Armbruster     if (snapshot) {
591666daa68SMarkus Armbruster         /* always use cache=unsafe with snapshot */
592666daa68SMarkus Armbruster         bdrv_flags &= ~BDRV_O_CACHE_MASK;
593666daa68SMarkus Armbruster         bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
594666daa68SMarkus Armbruster     }
595666daa68SMarkus Armbruster 
596fb0490f6SStefan Hajnoczi     if (copy_on_read) {
597fb0490f6SStefan Hajnoczi         bdrv_flags |= BDRV_O_COPY_ON_READ;
598fb0490f6SStefan Hajnoczi     }
599fb0490f6SStefan Hajnoczi 
600ed9d4205SBenoît Canet     if (runstate_check(RUN_STATE_INMIGRATE)) {
601ed9d4205SBenoît Canet         bdrv_flags |= BDRV_O_INCOMING;
602ed9d4205SBenoît Canet     }
603ed9d4205SBenoît Canet 
604666daa68SMarkus Armbruster     if (media == MEDIA_CDROM) {
605666daa68SMarkus Armbruster         /* CDROM is fine for any interface, don't check.  */
606666daa68SMarkus Armbruster         ro = 1;
607666daa68SMarkus Armbruster     } else if (ro == 1) {
6081e9eb78aSJordan Justen         if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
6091e9eb78aSJordan Justen             type != IF_NONE && type != IF_PFLASH) {
610807105a7SMarkus Armbruster             error_report("readonly not supported by this bus type");
611a9ae2bffSMarkus Armbruster             goto err;
612666daa68SMarkus Armbruster         }
613666daa68SMarkus Armbruster     }
614666daa68SMarkus Armbruster 
615666daa68SMarkus Armbruster     bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
616666daa68SMarkus Armbruster 
61704d4abe9SStefan Hajnoczi     if (ro && copy_on_read) {
61804d4abe9SStefan Hajnoczi         error_report("warning: disabling copy_on_read on readonly drive");
61904d4abe9SStefan Hajnoczi     }
62004d4abe9SStefan Hajnoczi 
621666daa68SMarkus Armbruster     ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
622666daa68SMarkus Armbruster     if (ret < 0) {
62302582abdSStefan Weil         if (ret == -EMEDIUMTYPE) {
62402582abdSStefan Weil             error_report("could not open disk image %s: not in %s format",
62502582abdSStefan Weil                          file, drv->format_name);
62602582abdSStefan Weil         } else {
627807105a7SMarkus Armbruster             error_report("could not open disk image %s: %s",
628666daa68SMarkus Armbruster                          file, strerror(-ret));
62902582abdSStefan Weil         }
630a9ae2bffSMarkus Armbruster         goto err;
631666daa68SMarkus Armbruster     }
632666daa68SMarkus Armbruster 
633666daa68SMarkus Armbruster     if (bdrv_key_required(dinfo->bdrv))
634666daa68SMarkus Armbruster         autostart = 0;
635666daa68SMarkus Armbruster     return dinfo;
636a9ae2bffSMarkus Armbruster 
637a9ae2bffSMarkus Armbruster err:
638a9ae2bffSMarkus Armbruster     bdrv_delete(dinfo->bdrv);
6397267c094SAnthony Liguori     g_free(dinfo->id);
640a9ae2bffSMarkus Armbruster     QTAILQ_REMOVE(&drives, dinfo, next);
6417267c094SAnthony Liguori     g_free(dinfo);
642a9ae2bffSMarkus Armbruster     return NULL;
643666daa68SMarkus Armbruster }
644666daa68SMarkus Armbruster 
645666daa68SMarkus Armbruster void do_commit(Monitor *mon, const QDict *qdict)
646666daa68SMarkus Armbruster {
647666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
6486ab4b5abSMarkus Armbruster     BlockDriverState *bs;
6492d3735d3SStefan Hajnoczi     int ret;
6502d3735d3SStefan Hajnoczi 
651e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
652e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
653e8877497SStefan Hajnoczi     } else {
6546ab4b5abSMarkus Armbruster         bs = bdrv_find(device);
655ac59eb95SMarkus Armbruster         if (!bs) {
65658513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
657ac59eb95SMarkus Armbruster             return;
6586ab4b5abSMarkus Armbruster         }
6592d3735d3SStefan Hajnoczi         ret = bdrv_commit(bs);
6602d3735d3SStefan Hajnoczi     }
66158513bdeSJeff Cody     if (ret < 0) {
66258513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
66358513bdeSJeff Cody                        strerror(-ret));
664666daa68SMarkus Armbruster     }
665666daa68SMarkus Armbruster }
666666daa68SMarkus Armbruster 
6676cc2a415SPaolo Bonzini static void blockdev_do_action(int kind, void *data, Error **errp)
6686cc2a415SPaolo Bonzini {
6696cc2a415SPaolo Bonzini     BlockdevAction action;
6706cc2a415SPaolo Bonzini     BlockdevActionList list;
6716cc2a415SPaolo Bonzini 
6726cc2a415SPaolo Bonzini     action.kind = kind;
6736cc2a415SPaolo Bonzini     action.data = data;
6746cc2a415SPaolo Bonzini     list.value = &action;
6756cc2a415SPaolo Bonzini     list.next = NULL;
6766cc2a415SPaolo Bonzini     qmp_transaction(&list, errp);
6776cc2a415SPaolo Bonzini }
6786cc2a415SPaolo Bonzini 
6796106e249SLuiz Capitulino void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
6806106e249SLuiz Capitulino                                 bool has_format, const char *format,
6816cc2a415SPaolo Bonzini                                 bool has_mode, enum NewImageMode mode,
6826106e249SLuiz Capitulino                                 Error **errp)
683f8882568SJes Sorensen {
6846cc2a415SPaolo Bonzini     BlockdevSnapshot snapshot = {
6856cc2a415SPaolo Bonzini         .device = (char *) device,
6866cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
6876cc2a415SPaolo Bonzini         .has_format = has_format,
6886cc2a415SPaolo Bonzini         .format = (char *) format,
6896cc2a415SPaolo Bonzini         .has_mode = has_mode,
6906cc2a415SPaolo Bonzini         .mode = mode,
6916cc2a415SPaolo Bonzini     };
6926cc2a415SPaolo Bonzini     blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
6936cc2a415SPaolo Bonzini                        errp);
694f8882568SJes Sorensen }
695f8882568SJes Sorensen 
6968802d1fdSJeff Cody 
6978802d1fdSJeff Cody /* New and old BlockDriverState structs for group snapshots */
69852e7c241SPaolo Bonzini typedef struct BlkTransactionStates {
6998802d1fdSJeff Cody     BlockDriverState *old_bs;
7008802d1fdSJeff Cody     BlockDriverState *new_bs;
70152e7c241SPaolo Bonzini     QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
70252e7c241SPaolo Bonzini } BlkTransactionStates;
7038802d1fdSJeff Cody 
7048802d1fdSJeff Cody /*
7058802d1fdSJeff Cody  * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
7068802d1fdSJeff Cody  *  then we do not pivot any of the devices in the group, and abandon the
7078802d1fdSJeff Cody  *  snapshots
7088802d1fdSJeff Cody  */
70952e7c241SPaolo Bonzini void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
7108802d1fdSJeff Cody {
7118802d1fdSJeff Cody     int ret = 0;
71252e7c241SPaolo Bonzini     BlockdevActionList *dev_entry = dev_list;
71352e7c241SPaolo Bonzini     BlkTransactionStates *states, *next;
71443e17041SLuiz Capitulino     Error *local_err = NULL;
7158802d1fdSJeff Cody 
71652e7c241SPaolo Bonzini     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
7178802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
7188802d1fdSJeff Cody 
7198802d1fdSJeff Cody     /* drain all i/o before any snapshots */
7208802d1fdSJeff Cody     bdrv_drain_all();
7218802d1fdSJeff Cody 
7228802d1fdSJeff Cody     /* We don't do anything in this loop that commits us to the snapshot */
7238802d1fdSJeff Cody     while (NULL != dev_entry) {
72452e7c241SPaolo Bonzini         BlockdevAction *dev_info = NULL;
72552e7c241SPaolo Bonzini         BlockDriver *proto_drv;
72652e7c241SPaolo Bonzini         BlockDriver *drv;
72752e7c241SPaolo Bonzini         int flags;
728bc8b094fSPaolo Bonzini         enum NewImageMode mode;
729bc8b094fSPaolo Bonzini         const char *new_image_file;
73052e7c241SPaolo Bonzini         const char *device;
73152e7c241SPaolo Bonzini         const char *format = "qcow2";
73252e7c241SPaolo Bonzini 
7338802d1fdSJeff Cody         dev_info = dev_entry->value;
7348802d1fdSJeff Cody         dev_entry = dev_entry->next;
7358802d1fdSJeff Cody 
73652e7c241SPaolo Bonzini         states = g_malloc0(sizeof(BlkTransactionStates));
7378802d1fdSJeff Cody         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
7388802d1fdSJeff Cody 
73952e7c241SPaolo Bonzini         switch (dev_info->kind) {
74052e7c241SPaolo Bonzini         case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
74152e7c241SPaolo Bonzini             device = dev_info->blockdev_snapshot_sync->device;
742bc8b094fSPaolo Bonzini             if (!dev_info->blockdev_snapshot_sync->has_mode) {
743bc8b094fSPaolo Bonzini                 dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
744bc8b094fSPaolo Bonzini             }
745bc8b094fSPaolo Bonzini             new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
74652e7c241SPaolo Bonzini             if (dev_info->blockdev_snapshot_sync->has_format) {
74752e7c241SPaolo Bonzini                 format = dev_info->blockdev_snapshot_sync->format;
74852e7c241SPaolo Bonzini             }
749bc8b094fSPaolo Bonzini             mode = dev_info->blockdev_snapshot_sync->mode;
75052e7c241SPaolo Bonzini             break;
75152e7c241SPaolo Bonzini         default:
75252e7c241SPaolo Bonzini             abort();
75352e7c241SPaolo Bonzini         }
7548802d1fdSJeff Cody 
75552e7c241SPaolo Bonzini         drv = bdrv_find_format(format);
75652e7c241SPaolo Bonzini         if (!drv) {
75752e7c241SPaolo Bonzini             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
75852e7c241SPaolo Bonzini             goto delete_and_fail;
75952e7c241SPaolo Bonzini         }
76052e7c241SPaolo Bonzini 
76152e7c241SPaolo Bonzini         states->old_bs = bdrv_find(device);
7628802d1fdSJeff Cody         if (!states->old_bs) {
76352e7c241SPaolo Bonzini             error_set(errp, QERR_DEVICE_NOT_FOUND, device);
7648802d1fdSJeff Cody             goto delete_and_fail;
7658802d1fdSJeff Cody         }
7668802d1fdSJeff Cody 
767e86fe18aSPaolo Bonzini         if (!bdrv_is_inserted(states->old_bs)) {
768e86fe18aSPaolo Bonzini             error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
769e86fe18aSPaolo Bonzini             goto delete_and_fail;
770e86fe18aSPaolo Bonzini         }
771e86fe18aSPaolo Bonzini 
7728802d1fdSJeff Cody         if (bdrv_in_use(states->old_bs)) {
77352e7c241SPaolo Bonzini             error_set(errp, QERR_DEVICE_IN_USE, device);
7748802d1fdSJeff Cody             goto delete_and_fail;
7758802d1fdSJeff Cody         }
7768802d1fdSJeff Cody 
777e86fe18aSPaolo Bonzini         if (!bdrv_is_read_only(states->old_bs)) {
7788802d1fdSJeff Cody             if (bdrv_flush(states->old_bs)) {
7798802d1fdSJeff Cody                 error_set(errp, QERR_IO_ERROR);
7808802d1fdSJeff Cody                 goto delete_and_fail;
7818802d1fdSJeff Cody             }
7828802d1fdSJeff Cody         }
7838802d1fdSJeff Cody 
7848802d1fdSJeff Cody         flags = states->old_bs->open_flags;
7858802d1fdSJeff Cody 
78652e7c241SPaolo Bonzini         proto_drv = bdrv_find_protocol(new_image_file);
7878802d1fdSJeff Cody         if (!proto_drv) {
7888802d1fdSJeff Cody             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
7898802d1fdSJeff Cody             goto delete_and_fail;
7908802d1fdSJeff Cody         }
7918802d1fdSJeff Cody 
7928802d1fdSJeff Cody         /* create new image w/backing file */
793bc8b094fSPaolo Bonzini         if (mode != NEW_IMAGE_MODE_EXISTING) {
79443e17041SLuiz Capitulino             bdrv_img_create(new_image_file, format,
7958802d1fdSJeff Cody                             states->old_bs->filename,
796d0895d6eSPaolo Bonzini                             states->old_bs->drv->format_name,
79743e17041SLuiz Capitulino                             NULL, -1, flags, &local_err);
79843e17041SLuiz Capitulino             if (error_is_set(&local_err)) {
79943e17041SLuiz Capitulino                 error_propagate(errp, local_err);
8008802d1fdSJeff Cody                 goto delete_and_fail;
8018802d1fdSJeff Cody             }
802bc8b094fSPaolo Bonzini         }
8038802d1fdSJeff Cody 
8048802d1fdSJeff Cody         /* We will manually add the backing_hd field to the bs later */
8058802d1fdSJeff Cody         states->new_bs = bdrv_new("");
80652e7c241SPaolo Bonzini         ret = bdrv_open(states->new_bs, new_image_file,
8078802d1fdSJeff Cody                         flags | BDRV_O_NO_BACKING, drv);
8088802d1fdSJeff Cody         if (ret != 0) {
80952e7c241SPaolo Bonzini             error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
8108802d1fdSJeff Cody             goto delete_and_fail;
8118802d1fdSJeff Cody         }
8128802d1fdSJeff Cody     }
8138802d1fdSJeff Cody 
8148802d1fdSJeff Cody 
8158802d1fdSJeff Cody     /* Now we are going to do the actual pivot.  Everything up to this point
8168802d1fdSJeff Cody      * is reversible, but we are committed at this point */
8178802d1fdSJeff Cody     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
8188802d1fdSJeff Cody         /* This removes our old bs from the bdrv_states, and adds the new bs */
8198802d1fdSJeff Cody         bdrv_append(states->new_bs, states->old_bs);
820870f5681SJeff Cody         /* We don't need (or want) to use the transactional
821870f5681SJeff Cody          * bdrv_reopen_multiple() across all the entries at once, because we
822870f5681SJeff Cody          * don't want to abort all of them if one of them fails the reopen */
823870f5681SJeff Cody         bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
824870f5681SJeff Cody                     NULL);
8258802d1fdSJeff Cody     }
8268802d1fdSJeff Cody 
8278802d1fdSJeff Cody     /* success */
8288802d1fdSJeff Cody     goto exit;
8298802d1fdSJeff Cody 
8308802d1fdSJeff Cody delete_and_fail:
8318802d1fdSJeff Cody     /*
8328802d1fdSJeff Cody     * failure, and it is all-or-none; abandon each new bs, and keep using
8338802d1fdSJeff Cody     * the original bs for all images
8348802d1fdSJeff Cody     */
8358802d1fdSJeff Cody     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
8368802d1fdSJeff Cody         if (states->new_bs) {
8378802d1fdSJeff Cody              bdrv_delete(states->new_bs);
8388802d1fdSJeff Cody         }
8398802d1fdSJeff Cody     }
8408802d1fdSJeff Cody exit:
841622d2419SPaolo Bonzini     QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
8428802d1fdSJeff Cody         g_free(states);
8438802d1fdSJeff Cody     }
8448802d1fdSJeff Cody }
8458802d1fdSJeff Cody 
8468802d1fdSJeff Cody 
84792d48558SLuiz Capitulino static void eject_device(BlockDriverState *bs, int force, Error **errp)
848666daa68SMarkus Armbruster {
8492d3735d3SStefan Hajnoczi     if (bdrv_in_use(bs)) {
8502d3735d3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
8512d3735d3SStefan Hajnoczi         return;
8522d3735d3SStefan Hajnoczi     }
8532c6942faSMarkus Armbruster     if (!bdrv_dev_has_removable_media(bs)) {
85492d48558SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
85592d48558SLuiz Capitulino         return;
856666daa68SMarkus Armbruster     }
85792d48558SLuiz Capitulino 
858025ccaa7SPaolo Bonzini     if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
859025ccaa7SPaolo Bonzini         bdrv_dev_eject_request(bs, force);
860025ccaa7SPaolo Bonzini         if (!force) {
86192d48558SLuiz Capitulino             error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
86292d48558SLuiz Capitulino             return;
863666daa68SMarkus Armbruster         }
864025ccaa7SPaolo Bonzini     }
86592d48558SLuiz Capitulino 
866666daa68SMarkus Armbruster     bdrv_close(bs);
867666daa68SMarkus Armbruster }
868666daa68SMarkus Armbruster 
869c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
870666daa68SMarkus Armbruster {
871666daa68SMarkus Armbruster     BlockDriverState *bs;
872666daa68SMarkus Armbruster 
873c245b6a3SLuiz Capitulino     bs = bdrv_find(device);
874666daa68SMarkus Armbruster     if (!bs) {
875c245b6a3SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
876c245b6a3SLuiz Capitulino         return;
877666daa68SMarkus Armbruster     }
87892d48558SLuiz Capitulino 
879c245b6a3SLuiz Capitulino     eject_device(bs, force, errp);
880666daa68SMarkus Armbruster }
881666daa68SMarkus Armbruster 
882a4dea8a9SLuiz Capitulino void qmp_block_passwd(const char *device, const char *password, Error **errp)
883666daa68SMarkus Armbruster {
884666daa68SMarkus Armbruster     BlockDriverState *bs;
885666daa68SMarkus Armbruster     int err;
886666daa68SMarkus Armbruster 
887a4dea8a9SLuiz Capitulino     bs = bdrv_find(device);
888666daa68SMarkus Armbruster     if (!bs) {
889a4dea8a9SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
890a4dea8a9SLuiz Capitulino         return;
891666daa68SMarkus Armbruster     }
892666daa68SMarkus Armbruster 
893a4dea8a9SLuiz Capitulino     err = bdrv_set_key(bs, password);
894666daa68SMarkus Armbruster     if (err == -EINVAL) {
895a4dea8a9SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
896a4dea8a9SLuiz Capitulino         return;
897666daa68SMarkus Armbruster     } else if (err < 0) {
898a4dea8a9SLuiz Capitulino         error_set(errp, QERR_INVALID_PASSWORD);
899a4dea8a9SLuiz Capitulino         return;
900666daa68SMarkus Armbruster     }
901666daa68SMarkus Armbruster }
902666daa68SMarkus Armbruster 
903333a96ecSLuiz Capitulino static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
904333a96ecSLuiz Capitulino                                     int bdrv_flags, BlockDriver *drv,
905333a96ecSLuiz Capitulino                                     const char *password, Error **errp)
906333a96ecSLuiz Capitulino {
907333a96ecSLuiz Capitulino     if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
908333a96ecSLuiz Capitulino         error_set(errp, QERR_OPEN_FILE_FAILED, filename);
909333a96ecSLuiz Capitulino         return;
910333a96ecSLuiz Capitulino     }
911333a96ecSLuiz Capitulino 
912333a96ecSLuiz Capitulino     if (bdrv_key_required(bs)) {
913333a96ecSLuiz Capitulino         if (password) {
914333a96ecSLuiz Capitulino             if (bdrv_set_key(bs, password) < 0) {
915333a96ecSLuiz Capitulino                 error_set(errp, QERR_INVALID_PASSWORD);
916333a96ecSLuiz Capitulino             }
917333a96ecSLuiz Capitulino         } else {
918333a96ecSLuiz Capitulino             error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
919333a96ecSLuiz Capitulino                       bdrv_get_encrypted_filename(bs));
920333a96ecSLuiz Capitulino         }
921333a96ecSLuiz Capitulino     } else if (password) {
922333a96ecSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
923333a96ecSLuiz Capitulino     }
924333a96ecSLuiz Capitulino }
925333a96ecSLuiz Capitulino 
926333a96ecSLuiz Capitulino void qmp_change_blockdev(const char *device, const char *filename,
927333a96ecSLuiz Capitulino                          bool has_format, const char *format, Error **errp)
928666daa68SMarkus Armbruster {
929666daa68SMarkus Armbruster     BlockDriverState *bs;
930666daa68SMarkus Armbruster     BlockDriver *drv = NULL;
931666daa68SMarkus Armbruster     int bdrv_flags;
93292d48558SLuiz Capitulino     Error *err = NULL;
933666daa68SMarkus Armbruster 
934666daa68SMarkus Armbruster     bs = bdrv_find(device);
935666daa68SMarkus Armbruster     if (!bs) {
936333a96ecSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
937333a96ecSLuiz Capitulino         return;
938666daa68SMarkus Armbruster     }
939333a96ecSLuiz Capitulino 
940333a96ecSLuiz Capitulino     if (format) {
941333a96ecSLuiz Capitulino         drv = bdrv_find_whitelisted_format(format);
942666daa68SMarkus Armbruster         if (!drv) {
943333a96ecSLuiz Capitulino             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
944333a96ecSLuiz Capitulino             return;
945666daa68SMarkus Armbruster         }
946666daa68SMarkus Armbruster     }
947333a96ecSLuiz Capitulino 
94892d48558SLuiz Capitulino     eject_device(bs, 0, &err);
94992d48558SLuiz Capitulino     if (error_is_set(&err)) {
950333a96ecSLuiz Capitulino         error_propagate(errp, err);
951333a96ecSLuiz Capitulino         return;
952666daa68SMarkus Armbruster     }
953333a96ecSLuiz Capitulino 
954528f7663SMarkus Armbruster     bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
955199630b6SBlue Swirl     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
956333a96ecSLuiz Capitulino 
957333a96ecSLuiz Capitulino     qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
958666daa68SMarkus Armbruster }
9599063f814SRyan Harper 
960727f005eSZhi Yong Wu /* throttling disk I/O limits */
96180047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
96280047da5SLuiz Capitulino                                int64_t bps_wr, int64_t iops, int64_t iops_rd,
96380047da5SLuiz Capitulino                                int64_t iops_wr, Error **errp)
964727f005eSZhi Yong Wu {
965727f005eSZhi Yong Wu     BlockIOLimit io_limits;
966727f005eSZhi Yong Wu     BlockDriverState *bs;
967727f005eSZhi Yong Wu 
96880047da5SLuiz Capitulino     bs = bdrv_find(device);
969727f005eSZhi Yong Wu     if (!bs) {
97080047da5SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
97180047da5SLuiz Capitulino         return;
972727f005eSZhi Yong Wu     }
973727f005eSZhi Yong Wu 
97480047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
97580047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_READ]  = bps_rd;
97680047da5SLuiz Capitulino     io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
97780047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
97880047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
97980047da5SLuiz Capitulino     io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
980727f005eSZhi Yong Wu 
981*c546194fSStefan Hajnoczi     if (!do_check_io_limits(&io_limits, errp)) {
98280047da5SLuiz Capitulino         return;
983727f005eSZhi Yong Wu     }
984727f005eSZhi Yong Wu 
985727f005eSZhi Yong Wu     bs->io_limits = io_limits;
986727f005eSZhi Yong Wu     bs->slice_time = BLOCK_IO_SLICE_TIME;
987727f005eSZhi Yong Wu 
988727f005eSZhi Yong Wu     if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
989727f005eSZhi Yong Wu         bdrv_io_limits_enable(bs);
990727f005eSZhi Yong Wu     } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
991727f005eSZhi Yong Wu         bdrv_io_limits_disable(bs);
992727f005eSZhi Yong Wu     } else {
993727f005eSZhi Yong Wu         if (bs->block_timer) {
994727f005eSZhi Yong Wu             qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
995727f005eSZhi Yong Wu         }
996727f005eSZhi Yong Wu     }
997727f005eSZhi Yong Wu }
998727f005eSZhi Yong Wu 
9999063f814SRyan Harper int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
10009063f814SRyan Harper {
10019063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
10029063f814SRyan Harper     BlockDriverState *bs;
10039063f814SRyan Harper 
10049063f814SRyan Harper     bs = bdrv_find(id);
10059063f814SRyan Harper     if (!bs) {
10069063f814SRyan Harper         qerror_report(QERR_DEVICE_NOT_FOUND, id);
10079063f814SRyan Harper         return -1;
10089063f814SRyan Harper     }
10098591675fSMarcelo Tosatti     if (bdrv_in_use(bs)) {
10108591675fSMarcelo Tosatti         qerror_report(QERR_DEVICE_IN_USE, id);
10118591675fSMarcelo Tosatti         return -1;
10128591675fSMarcelo Tosatti     }
10139063f814SRyan Harper 
10149063f814SRyan Harper     /* quiesce block driver; prevent further io */
1015922453bcSStefan Hajnoczi     bdrv_drain_all();
10169063f814SRyan Harper     bdrv_flush(bs);
10179063f814SRyan Harper     bdrv_close(bs);
10189063f814SRyan Harper 
1019fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
1020d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
1021d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
1022d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
1023d22b2f41SRyan Harper      */
1024fa879d62SMarkus Armbruster     if (bdrv_get_attached_dev(bs)) {
1025d22b2f41SRyan Harper         bdrv_make_anon(bs);
1026d22b2f41SRyan Harper     } else {
10279063f814SRyan Harper         drive_uninit(drive_get_by_blockdev(bs));
1028d22b2f41SRyan Harper     }
10299063f814SRyan Harper 
10309063f814SRyan Harper     return 0;
10319063f814SRyan Harper }
10326d4a2b3aSChristoph Hellwig 
10335e7caacbSLuiz Capitulino void qmp_block_resize(const char *device, int64_t size, Error **errp)
10346d4a2b3aSChristoph Hellwig {
10356d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
10366d4a2b3aSChristoph Hellwig 
10376d4a2b3aSChristoph Hellwig     bs = bdrv_find(device);
10386d4a2b3aSChristoph Hellwig     if (!bs) {
10395e7caacbSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
10405e7caacbSLuiz Capitulino         return;
10416d4a2b3aSChristoph Hellwig     }
10426d4a2b3aSChristoph Hellwig 
10436d4a2b3aSChristoph Hellwig     if (size < 0) {
1044939a1cc3SStefan Hajnoczi         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
10455e7caacbSLuiz Capitulino         return;
10466d4a2b3aSChristoph Hellwig     }
10476d4a2b3aSChristoph Hellwig 
1048939a1cc3SStefan Hajnoczi     switch (bdrv_truncate(bs, size)) {
1049939a1cc3SStefan Hajnoczi     case 0:
1050939a1cc3SStefan Hajnoczi         break;
1051939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
1052939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1053939a1cc3SStefan Hajnoczi         break;
1054939a1cc3SStefan Hajnoczi     case -ENOTSUP:
1055939a1cc3SStefan Hajnoczi         error_set(errp, QERR_UNSUPPORTED);
1056939a1cc3SStefan Hajnoczi         break;
1057939a1cc3SStefan Hajnoczi     case -EACCES:
1058939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1059939a1cc3SStefan Hajnoczi         break;
1060939a1cc3SStefan Hajnoczi     case -EBUSY:
1061939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IN_USE, device);
1062939a1cc3SStefan Hajnoczi         break;
1063939a1cc3SStefan Hajnoczi     default:
10645e7caacbSLuiz Capitulino         error_set(errp, QERR_UNDEFINED_ERROR);
1065939a1cc3SStefan Hajnoczi         break;
10666d4a2b3aSChristoph Hellwig     }
10676d4a2b3aSChristoph Hellwig }
106812bd451fSStefan Hajnoczi 
10699abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
107012bd451fSStefan Hajnoczi {
107112bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
107212bd451fSStefan Hajnoczi     QObject *obj;
107312bd451fSStefan Hajnoczi 
10749abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
107512bd451fSStefan Hajnoczi 
107612bd451fSStefan Hajnoczi     assert(bs->job);
107712bd451fSStefan Hajnoczi     obj = qobject_from_block_job(bs->job);
107812bd451fSStefan Hajnoczi     if (ret < 0) {
107912bd451fSStefan Hajnoczi         QDict *dict = qobject_to_qdict(obj);
108012bd451fSStefan Hajnoczi         qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
108112bd451fSStefan Hajnoczi     }
108212bd451fSStefan Hajnoczi 
1083370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
1084370521a1SStefan Hajnoczi         monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1085370521a1SStefan Hajnoczi     } else {
108612bd451fSStefan Hajnoczi         monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1087370521a1SStefan Hajnoczi     }
108812bd451fSStefan Hajnoczi     qobject_decref(obj);
1089aa398a5cSStefan Hajnoczi 
1090aa398a5cSStefan Hajnoczi     drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
109112bd451fSStefan Hajnoczi }
109212bd451fSStefan Hajnoczi 
109312bd451fSStefan Hajnoczi void qmp_block_stream(const char *device, bool has_base,
10941d809098SPaolo Bonzini                       const char *base, bool has_speed, int64_t speed,
10951d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
10961d809098SPaolo Bonzini                       Error **errp)
109712bd451fSStefan Hajnoczi {
109812bd451fSStefan Hajnoczi     BlockDriverState *bs;
1099c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
1100fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
110112bd451fSStefan Hajnoczi 
11021d809098SPaolo Bonzini     if (!has_on_error) {
11031d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
11041d809098SPaolo Bonzini     }
11051d809098SPaolo Bonzini 
110612bd451fSStefan Hajnoczi     bs = bdrv_find(device);
110712bd451fSStefan Hajnoczi     if (!bs) {
110812bd451fSStefan Hajnoczi         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
110912bd451fSStefan Hajnoczi         return;
111012bd451fSStefan Hajnoczi     }
111112bd451fSStefan Hajnoczi 
111212bd451fSStefan Hajnoczi     if (base) {
1113c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
1114c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
1115c8c3080fSMarcelo Tosatti             error_set(errp, QERR_BASE_NOT_FOUND, base);
111612bd451fSStefan Hajnoczi             return;
111712bd451fSStefan Hajnoczi         }
1118c8c3080fSMarcelo Tosatti     }
111912bd451fSStefan Hajnoczi 
1120c83c66c3SStefan Hajnoczi     stream_start(bs, base_bs, base, has_speed ? speed : 0,
11211d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
1122fd7f8c65SStefan Hajnoczi     if (error_is_set(&local_err)) {
1123fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
112412bd451fSStefan Hajnoczi         return;
112512bd451fSStefan Hajnoczi     }
112612bd451fSStefan Hajnoczi 
1127aa398a5cSStefan Hajnoczi     /* Grab a reference so hotplug does not delete the BlockDriverState from
1128aa398a5cSStefan Hajnoczi      * underneath us.
1129aa398a5cSStefan Hajnoczi      */
1130aa398a5cSStefan Hajnoczi     drive_get_ref(drive_get_by_blockdev(bs));
1131aa398a5cSStefan Hajnoczi 
113212bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
113312bd451fSStefan Hajnoczi }
11342d47c6e9SStefan Hajnoczi 
1135ed61fc10SJeff Cody void qmp_block_commit(const char *device,
1136ed61fc10SJeff Cody                       bool has_base, const char *base, const char *top,
1137ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
1138ed61fc10SJeff Cody                       Error **errp)
1139ed61fc10SJeff Cody {
1140ed61fc10SJeff Cody     BlockDriverState *bs;
1141ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
1142ed61fc10SJeff Cody     Error *local_err = NULL;
1143ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
1144ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
1145ed61fc10SJeff Cody      */
114692aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1147ed61fc10SJeff Cody 
1148ed61fc10SJeff Cody     /* drain all i/o before commits */
1149ed61fc10SJeff Cody     bdrv_drain_all();
1150ed61fc10SJeff Cody 
1151ed61fc10SJeff Cody     bs = bdrv_find(device);
1152ed61fc10SJeff Cody     if (!bs) {
1153ed61fc10SJeff Cody         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1154ed61fc10SJeff Cody         return;
1155ed61fc10SJeff Cody     }
1156ed61fc10SJeff Cody 
1157ed61fc10SJeff Cody     /* default top_bs is the active layer */
1158ed61fc10SJeff Cody     top_bs = bs;
1159ed61fc10SJeff Cody 
1160ed61fc10SJeff Cody     if (top) {
1161ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
1162ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
1163ed61fc10SJeff Cody         }
1164ed61fc10SJeff Cody     }
1165ed61fc10SJeff Cody 
1166ed61fc10SJeff Cody     if (top_bs == NULL) {
1167ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1168ed61fc10SJeff Cody         return;
1169ed61fc10SJeff Cody     }
1170ed61fc10SJeff Cody 
1171d5208c45SJeff Cody     if (has_base && base) {
1172d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
1173d5208c45SJeff Cody     } else {
1174d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
1175d5208c45SJeff Cody     }
1176d5208c45SJeff Cody 
1177d5208c45SJeff Cody     if (base_bs == NULL) {
1178d5208c45SJeff Cody         error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1179d5208c45SJeff Cody         return;
1180d5208c45SJeff Cody     }
1181d5208c45SJeff Cody 
1182ed61fc10SJeff Cody     commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1183ed61fc10SJeff Cody                 &local_err);
1184ed61fc10SJeff Cody     if (local_err != NULL) {
1185ed61fc10SJeff Cody         error_propagate(errp, local_err);
1186ed61fc10SJeff Cody         return;
1187ed61fc10SJeff Cody     }
1188ed61fc10SJeff Cody     /* Grab a reference so hotplug does not delete the BlockDriverState from
1189ed61fc10SJeff Cody      * underneath us.
1190ed61fc10SJeff Cody      */
1191ed61fc10SJeff Cody     drive_get_ref(drive_get_by_blockdev(bs));
1192ed61fc10SJeff Cody }
1193ed61fc10SJeff Cody 
119408e4ed6cSPaolo Bonzini #define DEFAULT_MIRROR_BUF_SIZE   (10 << 20)
119508e4ed6cSPaolo Bonzini 
1196d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target,
1197d9b902dbSPaolo Bonzini                       bool has_format, const char *format,
1198d9b902dbSPaolo Bonzini                       enum MirrorSyncMode sync,
1199d9b902dbSPaolo Bonzini                       bool has_mode, enum NewImageMode mode,
1200b952b558SPaolo Bonzini                       bool has_speed, int64_t speed,
1201eee13dfeSPaolo Bonzini                       bool has_granularity, uint32_t granularity,
120208e4ed6cSPaolo Bonzini                       bool has_buf_size, int64_t buf_size,
1203b952b558SPaolo Bonzini                       bool has_on_source_error, BlockdevOnError on_source_error,
1204b952b558SPaolo Bonzini                       bool has_on_target_error, BlockdevOnError on_target_error,
1205b952b558SPaolo Bonzini                       Error **errp)
1206d9b902dbSPaolo Bonzini {
1207d9b902dbSPaolo Bonzini     BlockDriverState *bs;
1208d9b902dbSPaolo Bonzini     BlockDriverState *source, *target_bs;
1209d9b902dbSPaolo Bonzini     BlockDriver *proto_drv;
1210d9b902dbSPaolo Bonzini     BlockDriver *drv = NULL;
1211d9b902dbSPaolo Bonzini     Error *local_err = NULL;
1212d9b902dbSPaolo Bonzini     int flags;
1213d9b902dbSPaolo Bonzini     uint64_t size;
1214d9b902dbSPaolo Bonzini     int ret;
1215d9b902dbSPaolo Bonzini 
1216d9b902dbSPaolo Bonzini     if (!has_speed) {
1217d9b902dbSPaolo Bonzini         speed = 0;
1218d9b902dbSPaolo Bonzini     }
1219b952b558SPaolo Bonzini     if (!has_on_source_error) {
1220b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1221b952b558SPaolo Bonzini     }
1222b952b558SPaolo Bonzini     if (!has_on_target_error) {
1223b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1224b952b558SPaolo Bonzini     }
1225d9b902dbSPaolo Bonzini     if (!has_mode) {
1226d9b902dbSPaolo Bonzini         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1227d9b902dbSPaolo Bonzini     }
1228eee13dfeSPaolo Bonzini     if (!has_granularity) {
1229eee13dfeSPaolo Bonzini         granularity = 0;
1230eee13dfeSPaolo Bonzini     }
123108e4ed6cSPaolo Bonzini     if (!has_buf_size) {
123208e4ed6cSPaolo Bonzini         buf_size = DEFAULT_MIRROR_BUF_SIZE;
123308e4ed6cSPaolo Bonzini     }
123408e4ed6cSPaolo Bonzini 
1235eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1236eee13dfeSPaolo Bonzini         error_set(errp, QERR_INVALID_PARAMETER, device);
1237eee13dfeSPaolo Bonzini         return;
1238eee13dfeSPaolo Bonzini     }
1239eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
1240eee13dfeSPaolo Bonzini         error_set(errp, QERR_INVALID_PARAMETER, device);
1241eee13dfeSPaolo Bonzini         return;
1242eee13dfeSPaolo Bonzini     }
1243d9b902dbSPaolo Bonzini 
1244d9b902dbSPaolo Bonzini     bs = bdrv_find(device);
1245d9b902dbSPaolo Bonzini     if (!bs) {
1246d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1247d9b902dbSPaolo Bonzini         return;
1248d9b902dbSPaolo Bonzini     }
1249d9b902dbSPaolo Bonzini 
1250d9b902dbSPaolo Bonzini     if (!bdrv_is_inserted(bs)) {
1251d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1252d9b902dbSPaolo Bonzini         return;
1253d9b902dbSPaolo Bonzini     }
1254d9b902dbSPaolo Bonzini 
1255d9b902dbSPaolo Bonzini     if (!has_format) {
1256d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1257d9b902dbSPaolo Bonzini     }
1258d9b902dbSPaolo Bonzini     if (format) {
1259d9b902dbSPaolo Bonzini         drv = bdrv_find_format(format);
1260d9b902dbSPaolo Bonzini         if (!drv) {
1261d9b902dbSPaolo Bonzini             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1262d9b902dbSPaolo Bonzini             return;
1263d9b902dbSPaolo Bonzini         }
1264d9b902dbSPaolo Bonzini     }
1265d9b902dbSPaolo Bonzini 
1266d9b902dbSPaolo Bonzini     if (bdrv_in_use(bs)) {
1267d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_IN_USE, device);
1268d9b902dbSPaolo Bonzini         return;
1269d9b902dbSPaolo Bonzini     }
1270d9b902dbSPaolo Bonzini 
1271d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
1272d9b902dbSPaolo Bonzini     source = bs->backing_hd;
1273d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1274d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
1275d9b902dbSPaolo Bonzini     }
1276d9b902dbSPaolo Bonzini 
1277d9b902dbSPaolo Bonzini     proto_drv = bdrv_find_protocol(target);
1278d9b902dbSPaolo Bonzini     if (!proto_drv) {
1279d9b902dbSPaolo Bonzini         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1280d9b902dbSPaolo Bonzini         return;
1281d9b902dbSPaolo Bonzini     }
1282d9b902dbSPaolo Bonzini 
128386899072SVishvananda Ishaya     bdrv_get_geometry(bs, &size);
128486899072SVishvananda Ishaya     size *= 512;
1285d9b902dbSPaolo Bonzini     if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1286d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
1287d9b902dbSPaolo Bonzini         assert(format && drv);
1288cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
1289cf8f2426SLuiz Capitulino                         NULL, NULL, NULL, size, flags, &local_err);
1290d9b902dbSPaolo Bonzini     } else {
1291d9b902dbSPaolo Bonzini         switch (mode) {
1292d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
1293d9b902dbSPaolo Bonzini             ret = 0;
1294d9b902dbSPaolo Bonzini             break;
1295d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1296d9b902dbSPaolo Bonzini             /* create new image with backing file */
1297cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
1298d9b902dbSPaolo Bonzini                             source->filename,
1299d9b902dbSPaolo Bonzini                             source->drv->format_name,
130086899072SVishvananda Ishaya                             NULL, size, flags, &local_err);
1301d9b902dbSPaolo Bonzini             break;
1302d9b902dbSPaolo Bonzini         default:
1303d9b902dbSPaolo Bonzini             abort();
1304d9b902dbSPaolo Bonzini         }
1305d9b902dbSPaolo Bonzini     }
1306d9b902dbSPaolo Bonzini 
1307cf8f2426SLuiz Capitulino     if (error_is_set(&local_err)) {
1308cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
1309d9b902dbSPaolo Bonzini         return;
1310d9b902dbSPaolo Bonzini     }
1311d9b902dbSPaolo Bonzini 
1312b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
1313b812f671SPaolo Bonzini      * file.
1314b812f671SPaolo Bonzini      */
1315d9b902dbSPaolo Bonzini     target_bs = bdrv_new("");
1316d9b902dbSPaolo Bonzini     ret = bdrv_open(target_bs, target, flags | BDRV_O_NO_BACKING, drv);
1317d9b902dbSPaolo Bonzini 
1318d9b902dbSPaolo Bonzini     if (ret < 0) {
1319d9b902dbSPaolo Bonzini         bdrv_delete(target_bs);
1320d9b902dbSPaolo Bonzini         error_set(errp, QERR_OPEN_FILE_FAILED, target);
1321d9b902dbSPaolo Bonzini         return;
1322d9b902dbSPaolo Bonzini     }
1323d9b902dbSPaolo Bonzini 
132408e4ed6cSPaolo Bonzini     mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1325eee13dfeSPaolo Bonzini                  on_source_error, on_target_error,
1326b952b558SPaolo Bonzini                  block_job_cb, bs, &local_err);
1327d9b902dbSPaolo Bonzini     if (local_err != NULL) {
1328d9b902dbSPaolo Bonzini         bdrv_delete(target_bs);
1329d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
1330d9b902dbSPaolo Bonzini         return;
1331d9b902dbSPaolo Bonzini     }
1332d9b902dbSPaolo Bonzini 
1333d9b902dbSPaolo Bonzini     /* Grab a reference so hotplug does not delete the BlockDriverState from
1334d9b902dbSPaolo Bonzini      * underneath us.
1335d9b902dbSPaolo Bonzini      */
1336d9b902dbSPaolo Bonzini     drive_get_ref(drive_get_by_blockdev(bs));
1337d9b902dbSPaolo Bonzini }
1338d9b902dbSPaolo Bonzini 
13392d47c6e9SStefan Hajnoczi static BlockJob *find_block_job(const char *device)
13402d47c6e9SStefan Hajnoczi {
13412d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
13422d47c6e9SStefan Hajnoczi 
13432d47c6e9SStefan Hajnoczi     bs = bdrv_find(device);
13442d47c6e9SStefan Hajnoczi     if (!bs || !bs->job) {
13452d47c6e9SStefan Hajnoczi         return NULL;
13462d47c6e9SStefan Hajnoczi     }
13472d47c6e9SStefan Hajnoczi     return bs->job;
13482d47c6e9SStefan Hajnoczi }
13492d47c6e9SStefan Hajnoczi 
1350882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
13512d47c6e9SStefan Hajnoczi {
13522d47c6e9SStefan Hajnoczi     BlockJob *job = find_block_job(device);
13532d47c6e9SStefan Hajnoczi 
13542d47c6e9SStefan Hajnoczi     if (!job) {
13557ef15070SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
13562d47c6e9SStefan Hajnoczi         return;
13572d47c6e9SStefan Hajnoczi     }
13582d47c6e9SStefan Hajnoczi 
1359882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
13602d47c6e9SStefan Hajnoczi }
1361370521a1SStefan Hajnoczi 
13626e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
13636e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
13646e37fb81SPaolo Bonzini {
13656e37fb81SPaolo Bonzini     BlockJob *job = find_block_job(device);
13666e37fb81SPaolo Bonzini 
13676e37fb81SPaolo Bonzini     if (!has_force) {
13686e37fb81SPaolo Bonzini         force = false;
13696e37fb81SPaolo Bonzini     }
13706e37fb81SPaolo Bonzini 
13716e37fb81SPaolo Bonzini     if (!job) {
13726e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
13736e37fb81SPaolo Bonzini         return;
13746e37fb81SPaolo Bonzini     }
13756e37fb81SPaolo Bonzini     if (job->paused && !force) {
13766e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
13776e37fb81SPaolo Bonzini         return;
13786e37fb81SPaolo Bonzini     }
13796e37fb81SPaolo Bonzini 
13806e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
13816e37fb81SPaolo Bonzini     block_job_cancel(job);
13826e37fb81SPaolo Bonzini }
13836e37fb81SPaolo Bonzini 
13846e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
1385370521a1SStefan Hajnoczi {
1386370521a1SStefan Hajnoczi     BlockJob *job = find_block_job(device);
1387370521a1SStefan Hajnoczi 
1388370521a1SStefan Hajnoczi     if (!job) {
13897ef15070SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1390370521a1SStefan Hajnoczi         return;
1391370521a1SStefan Hajnoczi     }
13926e37fb81SPaolo Bonzini 
13936e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
13946e37fb81SPaolo Bonzini     block_job_pause(job);
13956e37fb81SPaolo Bonzini }
13966e37fb81SPaolo Bonzini 
13976e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
13986e37fb81SPaolo Bonzini {
13996e37fb81SPaolo Bonzini     BlockJob *job = find_block_job(device);
14006e37fb81SPaolo Bonzini 
14016e37fb81SPaolo Bonzini     if (!job) {
14026e37fb81SPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
14038acc72a4SPaolo Bonzini         return;
14048acc72a4SPaolo Bonzini     }
1405370521a1SStefan Hajnoczi 
14066e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
14076e37fb81SPaolo Bonzini     block_job_resume(job);
1408370521a1SStefan Hajnoczi }
1409fb5458cdSStefan Hajnoczi 
1410aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
1411aeae883bSPaolo Bonzini {
1412aeae883bSPaolo Bonzini     BlockJob *job = find_block_job(device);
1413aeae883bSPaolo Bonzini 
1414aeae883bSPaolo Bonzini     if (!job) {
1415aeae883bSPaolo Bonzini         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1416aeae883bSPaolo Bonzini         return;
1417aeae883bSPaolo Bonzini     }
1418aeae883bSPaolo Bonzini 
1419aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
1420aeae883bSPaolo Bonzini     block_job_complete(job, errp);
1421aeae883bSPaolo Bonzini }
1422aeae883bSPaolo Bonzini 
1423fb5458cdSStefan Hajnoczi static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1424fb5458cdSStefan Hajnoczi {
1425fb5458cdSStefan Hajnoczi     BlockJobInfoList **prev = opaque;
1426fb5458cdSStefan Hajnoczi     BlockJob *job = bs->job;
1427fb5458cdSStefan Hajnoczi 
1428fb5458cdSStefan Hajnoczi     if (job) {
142930e628b7SPaolo Bonzini         BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
143030e628b7SPaolo Bonzini         elem->value = block_job_query(bs->job);
1431fb5458cdSStefan Hajnoczi         (*prev)->next = elem;
1432fb5458cdSStefan Hajnoczi         *prev = elem;
1433fb5458cdSStefan Hajnoczi     }
1434fb5458cdSStefan Hajnoczi }
1435fb5458cdSStefan Hajnoczi 
1436fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1437fb5458cdSStefan Hajnoczi {
1438fb5458cdSStefan Hajnoczi     /* Dummy is a fake list element for holding the head pointer */
1439fb5458cdSStefan Hajnoczi     BlockJobInfoList dummy = {};
1440fb5458cdSStefan Hajnoczi     BlockJobInfoList *prev = &dummy;
1441fb5458cdSStefan Hajnoczi     bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1442fb5458cdSStefan Hajnoczi     return dummy.next;
1443fb5458cdSStefan Hajnoczi }
14444d454574SPaolo Bonzini 
14454d454574SPaolo Bonzini QemuOptsList qemu_drive_opts = {
14464d454574SPaolo Bonzini     .name = "drive",
14474d454574SPaolo Bonzini     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
14484d454574SPaolo Bonzini     .desc = {
14494d454574SPaolo Bonzini         {
14504d454574SPaolo Bonzini             .name = "bus",
14514d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14524d454574SPaolo Bonzini             .help = "bus number",
14534d454574SPaolo Bonzini         },{
14544d454574SPaolo Bonzini             .name = "unit",
14554d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14564d454574SPaolo Bonzini             .help = "unit number (i.e. lun for scsi)",
14574d454574SPaolo Bonzini         },{
14584d454574SPaolo Bonzini             .name = "if",
14594d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
14604d454574SPaolo Bonzini             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
14614d454574SPaolo Bonzini         },{
14624d454574SPaolo Bonzini             .name = "index",
14634d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14644d454574SPaolo Bonzini             .help = "index number",
14654d454574SPaolo Bonzini         },{
14664d454574SPaolo Bonzini             .name = "cyls",
14674d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14684d454574SPaolo Bonzini             .help = "number of cylinders (ide disk geometry)",
14694d454574SPaolo Bonzini         },{
14704d454574SPaolo Bonzini             .name = "heads",
14714d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14724d454574SPaolo Bonzini             .help = "number of heads (ide disk geometry)",
14734d454574SPaolo Bonzini         },{
14744d454574SPaolo Bonzini             .name = "secs",
14754d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
14764d454574SPaolo Bonzini             .help = "number of sectors (ide disk geometry)",
14774d454574SPaolo Bonzini         },{
14784d454574SPaolo Bonzini             .name = "trans",
14794d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
14804d454574SPaolo Bonzini             .help = "chs translation (auto, lba. none)",
14814d454574SPaolo Bonzini         },{
14824d454574SPaolo Bonzini             .name = "media",
14834d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
14844d454574SPaolo Bonzini             .help = "media type (disk, cdrom)",
14854d454574SPaolo Bonzini         },{
14864d454574SPaolo Bonzini             .name = "snapshot",
14874d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
14884d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
14894d454574SPaolo Bonzini         },{
14904d454574SPaolo Bonzini             .name = "file",
14914d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
14924d454574SPaolo Bonzini             .help = "disk image",
14934d454574SPaolo Bonzini         },{
14944d454574SPaolo Bonzini             .name = "cache",
14954d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
14964d454574SPaolo Bonzini             .help = "host cache usage (none, writeback, writethrough, "
14974d454574SPaolo Bonzini                     "directsync, unsafe)",
14984d454574SPaolo Bonzini         },{
14994d454574SPaolo Bonzini             .name = "aio",
15004d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15014d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
15024d454574SPaolo Bonzini         },{
15034d454574SPaolo Bonzini             .name = "format",
15044d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15054d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
15064d454574SPaolo Bonzini         },{
15074d454574SPaolo Bonzini             .name = "serial",
15084d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15094d454574SPaolo Bonzini             .help = "disk serial number",
15104d454574SPaolo Bonzini         },{
15114d454574SPaolo Bonzini             .name = "rerror",
15124d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15134d454574SPaolo Bonzini             .help = "read error action",
15144d454574SPaolo Bonzini         },{
15154d454574SPaolo Bonzini             .name = "werror",
15164d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15174d454574SPaolo Bonzini             .help = "write error action",
15184d454574SPaolo Bonzini         },{
15194d454574SPaolo Bonzini             .name = "addr",
15204d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
15214d454574SPaolo Bonzini             .help = "pci address (virtio only)",
15224d454574SPaolo Bonzini         },{
15234d454574SPaolo Bonzini             .name = "readonly",
15244d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
15254d454574SPaolo Bonzini             .help = "open drive file as read-only",
15264d454574SPaolo Bonzini         },{
15274d454574SPaolo Bonzini             .name = "iops",
15284d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15294d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
15304d454574SPaolo Bonzini         },{
15314d454574SPaolo Bonzini             .name = "iops_rd",
15324d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15334d454574SPaolo Bonzini             .help = "limit read operations per second",
15344d454574SPaolo Bonzini         },{
15354d454574SPaolo Bonzini             .name = "iops_wr",
15364d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15374d454574SPaolo Bonzini             .help = "limit write operations per second",
15384d454574SPaolo Bonzini         },{
15394d454574SPaolo Bonzini             .name = "bps",
15404d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15414d454574SPaolo Bonzini             .help = "limit total bytes per second",
15424d454574SPaolo Bonzini         },{
15434d454574SPaolo Bonzini             .name = "bps_rd",
15444d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15454d454574SPaolo Bonzini             .help = "limit read bytes per second",
15464d454574SPaolo Bonzini         },{
15474d454574SPaolo Bonzini             .name = "bps_wr",
15484d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
15494d454574SPaolo Bonzini             .help = "limit write bytes per second",
15504d454574SPaolo Bonzini         },{
15514d454574SPaolo Bonzini             .name = "copy-on-read",
15524d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
15534d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
15544d454574SPaolo Bonzini         },{
15554d454574SPaolo Bonzini             .name = "boot",
15564d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
15574d454574SPaolo Bonzini             .help = "(deprecated, ignored)",
15584d454574SPaolo Bonzini         },
15594d454574SPaolo Bonzini         { /* end of list */ }
15604d454574SPaolo Bonzini     },
15614d454574SPaolo Bonzini };
1562