blob: 1e53b3dffddf8f245685c7c70307ca30152c0044 [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010026#include "trace.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
29#include "block/blockjob.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/module.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010031#include "qapi/qmp/qjson.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/notify.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010034#include "block/coroutine.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030035#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/timer.h"
bellardfc01f7e2003-06-30 10:03:06 +000037
Juan Quintela71e72a12009-07-27 16:12:56 +020038#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/types.h>
40#include <sys/stat.h>
41#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000042#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000043#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000044#include <sys/disk.h>
45#endif
blueswir1c5e97232009-03-07 20:06:23 +000046#endif
bellard7674e7b2005-04-26 21:59:26 +000047
aliguori49dc7682009-03-08 16:26:59 +000048#ifdef _WIN32
49#include <windows.h>
50#endif
51
Fam Zhenge4654d22013-11-13 18:29:43 +080052struct BdrvDirtyBitmap {
53 HBitmap *bitmap;
54 QLIST_ENTRY(BdrvDirtyBitmap) list;
55};
56
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010057#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
58
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +020059static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
aliguorif141eaf2009-04-07 18:43:24 +000060static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
61 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000062 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000063static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
64 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000065 BlockDriverCompletionFunc *cb, void *opaque);
Kevin Wolff9f05dc2011-07-15 13:50:26 +020066static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
67 int64_t sector_num, int nb_sectors,
68 QEMUIOVector *iov);
69static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
70 int64_t sector_num, int nb_sectors,
71 QEMUIOVector *iov);
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +010072static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
Stefan Hajnoczi470c0502012-01-18 14:40:42 +000073 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
74 BdrvRequestFlags flags);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010075static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +000076 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
77 BdrvRequestFlags flags);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010078static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
79 int64_t sector_num,
80 QEMUIOVector *qiov,
81 int nb_sectors,
Paolo Bonzinid20d9b72013-11-22 13:39:44 +010082 BdrvRequestFlags flags,
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010083 BlockDriverCompletionFunc *cb,
84 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +010085 bool is_write);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010086static void coroutine_fn bdrv_co_do_rw(void *opaque);
Kevin Wolf621f0582012-03-20 15:12:58 +010087static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +020088 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
bellardec530c82006-04-25 22:36:06 +000089
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010090static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
91 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000092
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010093static QLIST_HEAD(, BlockDriver) bdrv_drivers =
94 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000095
Markus Armbrustereb852012009-10-27 18:41:44 +010096/* If non-zero, use only whitelisted block drivers */
97static int use_bdrv_whitelist;
98
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000099#ifdef _WIN32
100static int is_windows_drive_prefix(const char *filename)
101{
102 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
103 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
104 filename[1] == ':');
105}
106
107int is_windows_drive(const char *filename)
108{
109 if (is_windows_drive_prefix(filename) &&
110 filename[2] == '\0')
111 return 1;
112 if (strstart(filename, "\\\\.\\", NULL) ||
113 strstart(filename, "//./", NULL))
114 return 1;
115 return 0;
116}
117#endif
118
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800119/* throttling disk I/O limits */
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200120void bdrv_set_io_limits(BlockDriverState *bs,
121 ThrottleConfig *cfg)
122{
123 int i;
124
125 throttle_config(&bs->throttle_state, cfg);
126
127 for (i = 0; i < 2; i++) {
128 qemu_co_enter_next(&bs->throttled_reqs[i]);
129 }
130}
131
132/* this function drain all the throttled IOs */
133static bool bdrv_start_throttled_reqs(BlockDriverState *bs)
134{
135 bool drained = false;
136 bool enabled = bs->io_limits_enabled;
137 int i;
138
139 bs->io_limits_enabled = false;
140
141 for (i = 0; i < 2; i++) {
142 while (qemu_co_enter_next(&bs->throttled_reqs[i])) {
143 drained = true;
144 }
145 }
146
147 bs->io_limits_enabled = enabled;
148
149 return drained;
150}
151
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800152void bdrv_io_limits_disable(BlockDriverState *bs)
153{
154 bs->io_limits_enabled = false;
155
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200156 bdrv_start_throttled_reqs(bs);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800157
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200158 throttle_destroy(&bs->throttle_state);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800159}
160
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200161static void bdrv_throttle_read_timer_cb(void *opaque)
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800162{
163 BlockDriverState *bs = opaque;
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200164 qemu_co_enter_next(&bs->throttled_reqs[0]);
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800165}
166
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200167static void bdrv_throttle_write_timer_cb(void *opaque)
168{
169 BlockDriverState *bs = opaque;
170 qemu_co_enter_next(&bs->throttled_reqs[1]);
171}
172
173/* should be called before bdrv_set_io_limits if a limit is set */
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800174void bdrv_io_limits_enable(BlockDriverState *bs)
175{
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200176 assert(!bs->io_limits_enabled);
177 throttle_init(&bs->throttle_state,
178 QEMU_CLOCK_VIRTUAL,
179 bdrv_throttle_read_timer_cb,
180 bdrv_throttle_write_timer_cb,
181 bs);
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800182 bs->io_limits_enabled = true;
183}
184
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200185/* This function makes an IO wait if needed
186 *
187 * @nb_sectors: the number of sectors of the IO
188 * @is_write: is the IO a write
189 */
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800190static void bdrv_io_limits_intercept(BlockDriverState *bs,
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200191 int nb_sectors,
192 bool is_write)
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800193{
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200194 /* does this io must wait */
195 bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800196
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200197 /* if must wait or any request of this type throttled queue the IO */
198 if (must_wait ||
199 !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) {
200 qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800201 }
202
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200203 /* the IO will be executed, do the accounting */
204 throttle_account(&bs->throttle_state,
205 is_write,
206 nb_sectors * BDRV_SECTOR_SIZE);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800207
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200208 /* if the next request must wait -> do nothing */
209 if (throttle_schedule_timer(&bs->throttle_state, is_write)) {
210 return;
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800211 }
212
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200213 /* else queue next request for execution */
214 qemu_co_queue_next(&bs->throttled_reqs[is_write]);
Zhi Yong Wu98f90db2011-11-08 13:00:14 +0800215}
216
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000217/* check if the path starts with "<protocol>:" */
218static int path_has_protocol(const char *path)
219{
Paolo Bonzini947995c2012-05-08 16:51:48 +0200220 const char *p;
221
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000222#ifdef _WIN32
223 if (is_windows_drive(path) ||
224 is_windows_drive_prefix(path)) {
225 return 0;
226 }
Paolo Bonzini947995c2012-05-08 16:51:48 +0200227 p = path + strcspn(path, ":/\\");
228#else
229 p = path + strcspn(path, ":/");
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000230#endif
231
Paolo Bonzini947995c2012-05-08 16:51:48 +0200232 return *p == ':';
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000233}
234
bellard83f64092006-08-01 16:21:11 +0000235int path_is_absolute(const char *path)
236{
bellard21664422007-01-07 18:22:37 +0000237#ifdef _WIN32
238 /* specific case for names like: "\\.\d:" */
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200239 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
bellard21664422007-01-07 18:22:37 +0000240 return 1;
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200241 }
242 return (*path == '/' || *path == '\\');
bellard3b9f94e2007-01-07 17:27:07 +0000243#else
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200244 return (*path == '/');
bellard3b9f94e2007-01-07 17:27:07 +0000245#endif
bellard83f64092006-08-01 16:21:11 +0000246}
247
248/* if filename is absolute, just copy it to dest. Otherwise, build a
249 path to it by considering it is relative to base_path. URL are
250 supported. */
251void path_combine(char *dest, int dest_size,
252 const char *base_path,
253 const char *filename)
254{
255 const char *p, *p1;
256 int len;
257
258 if (dest_size <= 0)
259 return;
260 if (path_is_absolute(filename)) {
261 pstrcpy(dest, dest_size, filename);
262 } else {
263 p = strchr(base_path, ':');
264 if (p)
265 p++;
266 else
267 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000268 p1 = strrchr(base_path, '/');
269#ifdef _WIN32
270 {
271 const char *p2;
272 p2 = strrchr(base_path, '\\');
273 if (!p1 || p2 > p1)
274 p1 = p2;
275 }
276#endif
bellard83f64092006-08-01 16:21:11 +0000277 if (p1)
278 p1++;
279 else
280 p1 = base_path;
281 if (p1 > p)
282 p = p1;
283 len = p - base_path;
284 if (len > dest_size - 1)
285 len = dest_size - 1;
286 memcpy(dest, base_path, len);
287 dest[len] = '\0';
288 pstrcat(dest, dest_size, filename);
289 }
290}
291
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200292void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz)
293{
294 if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) {
295 pstrcpy(dest, sz, bs->backing_file);
296 } else {
297 path_combine(dest, sz, bs->filename, bs->backing_file);
298 }
299}
300
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500301void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000302{
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +0100303 /* Block drivers without coroutine functions need emulation */
304 if (!bdrv->bdrv_co_readv) {
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200305 bdrv->bdrv_co_readv = bdrv_co_readv_em;
306 bdrv->bdrv_co_writev = bdrv_co_writev_em;
307
Stefan Hajnoczif8c35c12011-10-13 21:09:31 +0100308 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
309 * the block driver lacks aio we need to emulate that too.
310 */
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200311 if (!bdrv->bdrv_aio_readv) {
312 /* add AIO emulation layer */
313 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
314 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200315 }
bellard83f64092006-08-01 16:21:11 +0000316 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200317
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100318 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000319}
bellardb3380822004-03-14 21:38:54 +0000320
321/* create a new block device (by default it is empty) */
322BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000323{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100324 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000325
Anthony Liguori7267c092011-08-20 22:09:37 -0500326 bs = g_malloc0(sizeof(BlockDriverState));
Fam Zhenge4654d22013-11-13 18:29:43 +0800327 QLIST_INIT(&bs->dirty_bitmaps);
bellardb3380822004-03-14 21:38:54 +0000328 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000329 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100330 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000331 }
Luiz Capitulino28a72822011-09-26 17:43:50 -0300332 bdrv_iostatus_disable(bs);
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200333 notifier_list_init(&bs->close_notifiers);
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200334 notifier_with_return_list_init(&bs->before_write_notifiers);
BenoƮt Canetcc0681c2013-09-02 14:14:39 +0200335 qemu_co_queue_init(&bs->throttled_reqs[0]);
336 qemu_co_queue_init(&bs->throttled_reqs[1]);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800337 bs->refcnt = 1;
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200338
bellardb3380822004-03-14 21:38:54 +0000339 return bs;
340}
341
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200342void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
343{
344 notifier_list_add(&bs->close_notifiers, notify);
345}
346
bellardea2384d2004-08-01 21:59:26 +0000347BlockDriver *bdrv_find_format(const char *format_name)
348{
349 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100350 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
351 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000352 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100353 }
bellardea2384d2004-08-01 21:59:26 +0000354 }
355 return NULL;
356}
357
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800358static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100359{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800360 static const char *whitelist_rw[] = {
361 CONFIG_BDRV_RW_WHITELIST
362 };
363 static const char *whitelist_ro[] = {
364 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100365 };
366 const char **p;
367
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800368 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100369 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800370 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100371
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800372 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100373 if (!strcmp(drv->format_name, *p)) {
374 return 1;
375 }
376 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800377 if (read_only) {
378 for (p = whitelist_ro; *p; p++) {
379 if (!strcmp(drv->format_name, *p)) {
380 return 1;
381 }
382 }
383 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100384 return 0;
385}
386
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800387BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
388 bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100389{
390 BlockDriver *drv = bdrv_find_format(format_name);
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800391 return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
Markus Armbrustereb852012009-10-27 18:41:44 +0100392}
393
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800394typedef struct CreateCo {
395 BlockDriver *drv;
396 char *filename;
397 QEMUOptionParameter *options;
398 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200399 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800400} CreateCo;
401
402static void coroutine_fn bdrv_create_co_entry(void *opaque)
403{
Max Reitzcc84d902013-09-06 17:14:26 +0200404 Error *local_err = NULL;
405 int ret;
406
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800407 CreateCo *cco = opaque;
408 assert(cco->drv);
409
Max Reitzcc84d902013-09-06 17:14:26 +0200410 ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err);
411 if (error_is_set(&local_err)) {
412 error_propagate(&cco->err, local_err);
413 }
414 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800415}
416
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200417int bdrv_create(BlockDriver *drv, const char* filename,
Max Reitzcc84d902013-09-06 17:14:26 +0200418 QEMUOptionParameter *options, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000419{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800420 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200421
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800422 Coroutine *co;
423 CreateCo cco = {
424 .drv = drv,
425 .filename = g_strdup(filename),
426 .options = options,
427 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200428 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800429 };
430
431 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200432 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300433 ret = -ENOTSUP;
434 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800435 }
436
437 if (qemu_in_coroutine()) {
438 /* Fast-path if already in coroutine context */
439 bdrv_create_co_entry(&cco);
440 } else {
441 co = qemu_coroutine_create(bdrv_create_co_entry);
442 qemu_coroutine_enter(co, &cco);
443 while (cco.ret == NOT_DONE) {
444 qemu_aio_wait();
445 }
446 }
447
448 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200449 if (ret < 0) {
450 if (error_is_set(&cco.err)) {
451 error_propagate(errp, cco.err);
452 } else {
453 error_setg_errno(errp, -ret, "Could not create image");
454 }
455 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800456
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300457out:
458 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800459 return ret;
bellardea2384d2004-08-01 21:59:26 +0000460}
461
Max Reitzcc84d902013-09-06 17:14:26 +0200462int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
463 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200464{
465 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200466 Error *local_err = NULL;
467 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200468
Kevin Wolf98289622013-07-10 15:47:39 +0200469 drv = bdrv_find_protocol(filename, true);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200470 if (drv == NULL) {
Max Reitzcc84d902013-09-06 17:14:26 +0200471 error_setg(errp, "Could not find protocol for file '%s'", filename);
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000472 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200473 }
474
Max Reitzcc84d902013-09-06 17:14:26 +0200475 ret = bdrv_create(drv, filename, options, &local_err);
476 if (error_is_set(&local_err)) {
477 error_propagate(errp, local_err);
478 }
479 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200480}
481
Jim Meyeringeba25052012-05-28 09:27:54 +0200482/*
483 * Create a uniquely-named empty temporary file.
484 * Return 0 upon success, otherwise a negative errno value.
485 */
486int get_tmp_filename(char *filename, int size)
487{
bellardd5249392004-08-03 21:14:23 +0000488#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000489 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200490 /* GetTempFileName requires that its output buffer (4th param)
491 have length MAX_PATH or greater. */
492 assert(size >= MAX_PATH);
493 return (GetTempPath(MAX_PATH, temp_dir)
494 && GetTempFileName(temp_dir, "qem", 0, filename)
495 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000496#else
bellardea2384d2004-08-01 21:59:26 +0000497 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000498 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000499 tmpdir = getenv("TMPDIR");
500 if (!tmpdir)
501 tmpdir = "/tmp";
Jim Meyeringeba25052012-05-28 09:27:54 +0200502 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
503 return -EOVERFLOW;
504 }
bellardea2384d2004-08-01 21:59:26 +0000505 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800506 if (fd < 0) {
507 return -errno;
508 }
509 if (close(fd) != 0) {
510 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200511 return -errno;
512 }
513 return 0;
bellardd5249392004-08-03 21:14:23 +0000514#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200515}
bellardea2384d2004-08-01 21:59:26 +0000516
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200517/*
518 * Detect host devices. By convention, /dev/cdrom[N] is always
519 * recognized as a host CDROM.
520 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200521static BlockDriver *find_hdev_driver(const char *filename)
522{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200523 int score_max = 0, score;
524 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200525
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100526 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200527 if (d->bdrv_probe_device) {
528 score = d->bdrv_probe_device(filename);
529 if (score > score_max) {
530 score_max = score;
531 drv = d;
532 }
533 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200534 }
535
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200536 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200537}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200538
Kevin Wolf98289622013-07-10 15:47:39 +0200539BlockDriver *bdrv_find_protocol(const char *filename,
540 bool allow_protocol_prefix)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200541{
542 BlockDriver *drv1;
543 char protocol[128];
544 int len;
545 const char *p;
546
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200547 /* TODO Drivers without bdrv_file_open must be specified explicitly */
548
Christoph Hellwig39508e72010-06-23 12:25:17 +0200549 /*
550 * XXX(hch): we really should not let host device detection
551 * override an explicit protocol specification, but moving this
552 * later breaks access to device names with colons in them.
553 * Thanks to the brain-dead persistent naming schemes on udev-
554 * based Linux systems those actually are quite common.
555 */
556 drv1 = find_hdev_driver(filename);
557 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200558 return drv1;
559 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200560
Kevin Wolf98289622013-07-10 15:47:39 +0200561 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Christoph Hellwig39508e72010-06-23 12:25:17 +0200562 return bdrv_find_format("file");
563 }
Kevin Wolf98289622013-07-10 15:47:39 +0200564
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000565 p = strchr(filename, ':');
566 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200567 len = p - filename;
568 if (len > sizeof(protocol) - 1)
569 len = sizeof(protocol) - 1;
570 memcpy(protocol, filename, len);
571 protocol[len] = '\0';
572 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
573 if (drv1->protocol_name &&
574 !strcmp(drv1->protocol_name, protocol)) {
575 return drv1;
576 }
577 }
578 return NULL;
579}
580
Kevin Wolff500a6d2012-11-12 17:35:27 +0100581static int find_image_format(BlockDriverState *bs, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200582 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000583{
Kevin Wolff500a6d2012-11-12 17:35:27 +0100584 int score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000585 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000586 uint8_t buf[2048];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100587 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700588
Kevin Wolf08a00552010-06-01 18:37:31 +0200589 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Paolo Bonzini8e895592013-01-10 15:39:27 +0100590 if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200591 drv = bdrv_find_format("raw");
592 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200593 error_setg(errp, "Could not find raw image format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200594 ret = -ENOENT;
595 }
596 *pdrv = drv;
597 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700598 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700599
bellard83f64092006-08-01 16:21:11 +0000600 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000601 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200602 error_setg_errno(errp, -ret, "Could not read image for determining its "
603 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200604 *pdrv = NULL;
605 return ret;
bellard83f64092006-08-01 16:21:11 +0000606 }
607
bellardea2384d2004-08-01 21:59:26 +0000608 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200609 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100610 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000611 if (drv1->bdrv_probe) {
612 score = drv1->bdrv_probe(buf, ret, filename);
613 if (score > score_max) {
614 score_max = score;
615 drv = drv1;
616 }
bellardea2384d2004-08-01 21:59:26 +0000617 }
618 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200619 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200620 error_setg(errp, "Could not determine image format: No compatible "
621 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200622 ret = -ENOENT;
623 }
624 *pdrv = drv;
625 return ret;
bellardea2384d2004-08-01 21:59:26 +0000626}
627
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100628/**
629 * Set the current 'total_sectors' value
630 */
631static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
632{
633 BlockDriver *drv = bs->drv;
634
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700635 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
636 if (bs->sg)
637 return 0;
638
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100639 /* query actual device if possible, otherwise just trust the hint */
640 if (drv->bdrv_getlength) {
641 int64_t length = drv->bdrv_getlength(bs);
642 if (length < 0) {
643 return length;
644 }
Fam Zheng7e382002013-11-06 19:48:06 +0800645 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100646 }
647
648 bs->total_sectors = hint;
649 return 0;
650}
651
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100652/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100653 * Set open flags for a given discard mode
654 *
655 * Return 0 on success, -1 if the discard mode was invalid.
656 */
657int bdrv_parse_discard_flags(const char *mode, int *flags)
658{
659 *flags &= ~BDRV_O_UNMAP;
660
661 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
662 /* do nothing */
663 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
664 *flags |= BDRV_O_UNMAP;
665 } else {
666 return -1;
667 }
668
669 return 0;
670}
671
672/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100673 * Set open flags for a given cache mode
674 *
675 * Return 0 on success, -1 if the cache mode was invalid.
676 */
677int bdrv_parse_cache_flags(const char *mode, int *flags)
678{
679 *flags &= ~BDRV_O_CACHE_MASK;
680
681 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
682 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100683 } else if (!strcmp(mode, "directsync")) {
684 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100685 } else if (!strcmp(mode, "writeback")) {
686 *flags |= BDRV_O_CACHE_WB;
687 } else if (!strcmp(mode, "unsafe")) {
688 *flags |= BDRV_O_CACHE_WB;
689 *flags |= BDRV_O_NO_FLUSH;
690 } else if (!strcmp(mode, "writethrough")) {
691 /* this is the default */
692 } else {
693 return -1;
694 }
695
696 return 0;
697}
698
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000699/**
700 * The copy-on-read flag is actually a reference count so multiple users may
701 * use the feature without worrying about clobbering its previous state.
702 * Copy-on-read stays enabled until all users have called to disable it.
703 */
704void bdrv_enable_copy_on_read(BlockDriverState *bs)
705{
706 bs->copy_on_read++;
707}
708
709void bdrv_disable_copy_on_read(BlockDriverState *bs)
710{
711 assert(bs->copy_on_read > 0);
712 bs->copy_on_read--;
713}
714
Kevin Wolf7b272452012-11-12 17:05:39 +0100715static int bdrv_open_flags(BlockDriverState *bs, int flags)
716{
717 int open_flags = flags | BDRV_O_CACHE_WB;
718
719 /*
720 * Clear flags that are internal to the block layer before opening the
721 * image.
722 */
723 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
724
725 /*
726 * Snapshots should be writable.
727 */
728 if (bs->is_temporary) {
729 open_flags |= BDRV_O_RDWR;
730 }
731
732 return open_flags;
733}
734
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200735/*
Kevin Wolf57915332010-04-14 15:24:50 +0200736 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +0100737 *
738 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +0200739 */
Kevin Wolff500a6d2012-11-12 17:35:27 +0100740static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200741 QDict *options, int flags, BlockDriver *drv, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +0200742{
743 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200744 const char *filename;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200745 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +0200746
747 assert(drv != NULL);
Paolo Bonzini64058752012-05-08 16:51:49 +0200748 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +0100749 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +0200750
Kevin Wolf45673672013-04-22 17:48:40 +0200751 if (file != NULL) {
752 filename = file->filename;
753 } else {
754 filename = qdict_get_try_str(options, "filename");
755 }
756
757 trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
Stefan Hajnoczi28dcee12011-09-22 20:14:12 +0100758
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100759 /* bdrv_open() with directly using a protocol as drv. This layer is already
760 * opened, so assign it to bs (while file becomes a closed BlockDriverState)
761 * and return immediately. */
762 if (file != NULL && drv->bdrv_file_open) {
763 bdrv_swap(file, bs);
764 return 0;
765 }
766
Kevin Wolf57915332010-04-14 15:24:50 +0200767 bs->open_flags = flags;
Kevin Wolf57915332010-04-14 15:24:50 +0200768 bs->buffer_alignment = 512;
Asias He0d51b4d2013-08-22 15:24:14 +0800769 bs->zero_beyond_eof = true;
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800770 open_flags = bdrv_open_flags(bs, flags);
771 bs->read_only = !(open_flags & BDRV_O_RDWR);
772
773 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +0200774 error_setg(errp,
775 !bs->read_only && bdrv_is_whitelisted(drv, true)
776 ? "Driver '%s' can only be used for read-only devices"
777 : "Driver '%s' is not whitelisted",
778 drv->format_name);
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800779 return -ENOTSUP;
780 }
Kevin Wolf57915332010-04-14 15:24:50 +0200781
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000782 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200783 if (flags & BDRV_O_COPY_ON_READ) {
784 if (!bs->read_only) {
785 bdrv_enable_copy_on_read(bs);
786 } else {
787 error_setg(errp, "Can't use copy-on-read on read-only device");
788 return -EINVAL;
789 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000790 }
791
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100792 if (filename != NULL) {
793 pstrcpy(bs->filename, sizeof(bs->filename), filename);
794 } else {
795 bs->filename[0] = '\0';
796 }
Kevin Wolf57915332010-04-14 15:24:50 +0200797
Kevin Wolf57915332010-04-14 15:24:50 +0200798 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500799 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200800
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100801 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
Stefan Hajnoczie7c63792011-10-27 10:54:27 +0100802
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200803 /* Open the image, either directly or using a protocol */
804 if (drv->bdrv_file_open) {
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100805 assert(file == NULL);
BenoƮt Canet030be322013-09-24 17:07:04 +0200806 assert(!drv->bdrv_needs_filename || filename != NULL);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200807 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +0100808 } else {
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200809 if (file == NULL) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200810 error_setg(errp, "Can't use '%s' as a block driver for the "
811 "protocol level", drv->format_name);
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200812 ret = -EINVAL;
813 goto free_and_fail;
814 }
Kevin Wolff500a6d2012-11-12 17:35:27 +0100815 bs->file = file;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200816 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200817 }
818
Kevin Wolf57915332010-04-14 15:24:50 +0200819 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200820 if (error_is_set(&local_err)) {
821 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +0800822 } else if (bs->filename[0]) {
823 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200824 } else {
825 error_setg_errno(errp, -ret, "Could not open image");
826 }
Kevin Wolf57915332010-04-14 15:24:50 +0200827 goto free_and_fail;
828 }
829
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100830 ret = refresh_total_sectors(bs, bs->total_sectors);
831 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200832 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100833 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200834 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100835
Kevin Wolf57915332010-04-14 15:24:50 +0200836#ifndef _WIN32
837 if (bs->is_temporary) {
Dunrong Huangd4cea8d2013-10-03 01:31:27 +0800838 assert(bs->filename[0] != '\0');
839 unlink(bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +0200840 }
841#endif
842 return 0;
843
844free_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +0100845 bs->file = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500846 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +0200847 bs->opaque = NULL;
848 bs->drv = NULL;
849 return ret;
850}
851
852/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200853 * Opens a file using a protocol (file, host_device, nbd, ...)
Kevin Wolf787e4a82013-03-06 11:52:48 +0100854 *
855 * options is a QDict of options to pass to the block drivers, or NULL for an
856 * empty set of options. The reference to the QDict belongs to the block layer
857 * after the call (even on failure), so if the caller intends to reuse the
858 * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200859 */
Kevin Wolf787e4a82013-03-06 11:52:48 +0100860int bdrv_file_open(BlockDriverState **pbs, const char *filename,
Max Reitz72daa722013-12-20 19:28:08 +0100861 const char *reference, QDict *options, int flags,
862 Error **errp)
bellardb3380822004-03-14 21:38:54 +0000863{
Max Reitz72daa722013-12-20 19:28:08 +0100864 BlockDriverState *bs = NULL;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200865 BlockDriver *drv;
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100866 const char *drvname;
Kevin Wolf98289622013-07-10 15:47:39 +0200867 bool allow_protocol_prefix = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200868 Error *local_err = NULL;
bellard83f64092006-08-01 16:21:11 +0000869 int ret;
870
Kevin Wolf707ff822013-03-06 12:20:31 +0100871 /* NULL means an empty set of options */
872 if (options == NULL) {
873 options = qdict_new();
bellard3b0d4f62005-10-30 18:30:10 +0000874 }
Kevin Wolf707ff822013-03-06 12:20:31 +0100875
Max Reitz72daa722013-12-20 19:28:08 +0100876 if (reference) {
877 if (filename || qdict_size(options)) {
878 error_setg(errp, "Cannot reference an existing block device with "
879 "additional options or a new filename");
880 return -EINVAL;
881 }
882 QDECREF(options);
883
884 bs = bdrv_find(reference);
885 if (!bs) {
886 error_setg(errp, "Cannot find block device '%s'", reference);
887 return -ENODEV;
888 }
889 bdrv_ref(bs);
890 *pbs = bs;
891 return 0;
892 }
893
Kevin Wolf707ff822013-03-06 12:20:31 +0100894 bs = bdrv_new("");
895 bs->options = options;
896 options = qdict_clone_shallow(options);
897
Kevin Wolf035fccd2013-04-09 14:34:19 +0200898 /* Fetch the file name from the options QDict if necessary */
899 if (!filename) {
900 filename = qdict_get_try_str(options, "filename");
901 } else if (filename && !qdict_haskey(options, "filename")) {
902 qdict_put(options, "filename", qstring_from_str(filename));
Kevin Wolf98289622013-07-10 15:47:39 +0200903 allow_protocol_prefix = true;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200904 } else {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200905 error_setg(errp, "Can't specify 'file' and 'filename' options at the "
906 "same time");
Kevin Wolf035fccd2013-04-09 14:34:19 +0200907 ret = -EINVAL;
908 goto fail;
909 }
910
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100911 /* Find the right block driver */
912 drvname = qdict_get_try_str(options, "driver");
913 if (drvname) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +0200914 drv = bdrv_find_format(drvname);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200915 if (!drv) {
916 error_setg(errp, "Unknown driver '%s'", drvname);
917 }
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100918 qdict_del(options, "driver");
919 } else if (filename) {
Kevin Wolf98289622013-07-10 15:47:39 +0200920 drv = bdrv_find_protocol(filename, allow_protocol_prefix);
921 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200922 error_setg(errp, "Unknown protocol");
Kevin Wolf98289622013-07-10 15:47:39 +0200923 }
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100924 } else {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200925 error_setg(errp, "Must specify either driver or file");
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100926 drv = NULL;
927 }
928
929 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200930 /* errp has been set already */
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100931 ret = -ENOENT;
932 goto fail;
933 }
934
935 /* Parse the filename and open it */
936 if (drv->bdrv_parse_filename && filename) {
Kevin Wolf6963a302013-03-15 18:47:22 +0100937 drv->bdrv_parse_filename(filename, options, &local_err);
938 if (error_is_set(&local_err)) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200939 error_propagate(errp, local_err);
Kevin Wolf6963a302013-03-15 18:47:22 +0100940 ret = -EINVAL;
941 goto fail;
942 }
Kevin Wolf56d1b4d2013-04-12 20:02:37 +0200943 qdict_del(options, "filename");
BenoƮt Canet030be322013-09-24 17:07:04 +0200944 } else if (drv->bdrv_needs_filename && !filename) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200945 error_setg(errp, "The '%s' block driver requires a file name",
946 drv->format_name);
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100947 ret = -EINVAL;
948 goto fail;
Kevin Wolf6963a302013-03-15 18:47:22 +0100949 }
950
Max Reitz34b5d2c2013-09-05 14:45:29 +0200951 ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err);
Kevin Wolf707ff822013-03-06 12:20:31 +0100952 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200953 error_propagate(errp, local_err);
Kevin Wolf707ff822013-03-06 12:20:31 +0100954 goto fail;
955 }
956
957 /* Check if any unknown options were used */
958 if (qdict_size(options) != 0) {
959 const QDictEntry *entry = qdict_first(options);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200960 error_setg(errp, "Block protocol '%s' doesn't support the option '%s'",
961 drv->format_name, entry->key);
Kevin Wolf707ff822013-03-06 12:20:31 +0100962 ret = -EINVAL;
963 goto fail;
964 }
965 QDECREF(options);
966
aliguori71d07702009-03-03 17:37:16 +0000967 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000968 *pbs = bs;
969 return 0;
Kevin Wolf707ff822013-03-06 12:20:31 +0100970
971fail:
972 QDECREF(options);
973 if (!bs->drv) {
974 QDECREF(bs->options);
975 }
Fam Zheng4f6fd342013-08-23 09:14:47 +0800976 bdrv_unref(bs);
Kevin Wolf707ff822013-03-06 12:20:31 +0100977 return ret;
bellardea2384d2004-08-01 21:59:26 +0000978}
bellardfc01f7e2003-06-30 10:03:06 +0000979
Kevin Wolf31ca6d02013-03-28 15:29:24 +0100980/*
981 * Opens the backing file for a BlockDriverState if not yet open
982 *
983 * options is a QDict of options to pass to the block drivers, or NULL for an
984 * empty set of options. The reference to the QDict is transferred to this
985 * function (even on failure), so if the caller intends to reuse the dictionary,
986 * it needs to use QINCREF() before calling bdrv_file_open.
987 */
Max Reitz34b5d2c2013-09-05 14:45:29 +0200988int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +0200989{
990 char backing_filename[PATH_MAX];
991 int back_flags, ret;
992 BlockDriver *back_drv = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200993 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +0200994
995 if (bs->backing_hd != NULL) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +0100996 QDECREF(options);
Paolo Bonzini9156df12012-10-18 16:49:17 +0200997 return 0;
998 }
999
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001000 /* NULL means an empty set of options */
1001 if (options == NULL) {
1002 options = qdict_new();
1003 }
1004
Paolo Bonzini9156df12012-10-18 16:49:17 +02001005 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001006 if (qdict_haskey(options, "file.filename")) {
1007 backing_filename[0] = '\0';
1008 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001009 QDECREF(options);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001010 return 0;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001011 } else {
1012 bdrv_get_full_backing_filename(bs, backing_filename,
1013 sizeof(backing_filename));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001014 }
1015
1016 bs->backing_hd = bdrv_new("");
Paolo Bonzini9156df12012-10-18 16:49:17 +02001017
1018 if (bs->backing_format[0] != '\0') {
1019 back_drv = bdrv_find_format(bs->backing_format);
1020 }
1021
1022 /* backing files always opened read-only */
Thibaut LAURENT87a5deb2013-10-25 02:15:07 +02001023 back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT |
1024 BDRV_O_COPY_ON_READ);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001025
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001026 ret = bdrv_open(bs->backing_hd,
1027 *backing_filename ? backing_filename : NULL, options,
Max Reitz34b5d2c2013-09-05 14:45:29 +02001028 back_flags, back_drv, &local_err);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001029 if (ret < 0) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001030 bdrv_unref(bs->backing_hd);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001031 bs->backing_hd = NULL;
1032 bs->open_flags |= BDRV_O_NO_BACKING;
Fam Zhengb04b6b62013-11-08 11:26:49 +08001033 error_setg(errp, "Could not open backing file: %s",
1034 error_get_pretty(local_err));
1035 error_free(local_err);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001036 return ret;
1037 }
Max Reitz61ed2682013-10-26 15:44:43 +02001038 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
1039 bs->backing_hd->file->filename);
Paolo Bonzini9156df12012-10-18 16:49:17 +02001040 return 0;
1041}
1042
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001043/*
1044 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001045 *
1046 * options is a QDict of options to pass to the block drivers, or NULL for an
1047 * empty set of options. The reference to the QDict belongs to the block layer
1048 * after the call (even on failure), so if the caller intends to reuse the
1049 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001050 */
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001051int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
Max Reitz34b5d2c2013-09-05 14:45:29 +02001052 int flags, BlockDriver *drv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001053{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001054 int ret;
Stefan Weil89c9bc32012-11-22 07:25:48 +01001055 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1056 char tmp_filename[PATH_MAX + 1];
Kevin Wolff500a6d2012-11-12 17:35:27 +01001057 BlockDriverState *file = NULL;
Kevin Wolf707ff822013-03-06 12:20:31 +01001058 QDict *file_options = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001059 const char *drvname;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001060 Error *local_err = NULL;
bellard712e7872005-04-28 21:09:32 +00001061
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001062 /* NULL means an empty set of options */
1063 if (options == NULL) {
1064 options = qdict_new();
1065 }
1066
1067 bs->options = options;
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001068 options = qdict_clone_shallow(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001069
1070 /* For snapshot=on, create a temporary qcow2 overlay */
bellard83f64092006-08-01 16:21:11 +00001071 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +00001072 BlockDriverState *bs1;
1073 int64_t total_size;
Kevin Wolf91a073a2009-05-27 14:48:06 +02001074 BlockDriver *bdrv_qcow2;
Kevin Wolf08b392e2013-03-18 16:17:44 +01001075 QEMUOptionParameter *create_options;
Kevin Wolf9fd31712013-11-14 15:37:12 +01001076 QDict *snapshot_options;
Kevin Wolfc2ad1b02013-03-18 16:40:51 +01001077
bellardea2384d2004-08-01 21:59:26 +00001078 /* if snapshot, we create a temporary backing file and open it
1079 instead of opening 'filename' directly */
1080
Kevin Wolf9fd31712013-11-14 15:37:12 +01001081 /* Get the required size from the image */
bellardea2384d2004-08-01 21:59:26 +00001082 bs1 = bdrv_new("");
Kevin Wolf9fd31712013-11-14 15:37:12 +01001083 QINCREF(options);
Kevin Wolfc9fbb992013-11-28 11:58:02 +01001084 ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING,
1085 drv, &local_err);
aliguori51d7c002009-03-05 23:00:29 +00001086 if (ret < 0) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001087 bdrv_unref(bs1);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001088 goto fail;
bellardea2384d2004-08-01 21:59:26 +00001089 }
Jes Sorensen3e829902010-05-27 16:20:30 +02001090 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +00001091
Fam Zheng4f6fd342013-08-23 09:14:47 +08001092 bdrv_unref(bs1);
ths3b46e622007-09-17 08:09:54 +00001093
Kevin Wolf9fd31712013-11-14 15:37:12 +01001094 /* Create the temporary image */
Jim Meyeringeba25052012-05-28 09:27:54 +02001095 ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
1096 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001097 error_setg_errno(errp, -ret, "Could not get temporary filename");
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001098 goto fail;
Jim Meyeringeba25052012-05-28 09:27:54 +02001099 }
aliguori7c96d462008-09-12 17:54:13 +00001100
Kevin Wolf91a073a2009-05-27 14:48:06 +02001101 bdrv_qcow2 = bdrv_find_format("qcow2");
Kevin Wolf08b392e2013-03-18 16:17:44 +01001102 create_options = parse_option_parameters("", bdrv_qcow2->create_options,
1103 NULL);
Kevin Wolf91a073a2009-05-27 14:48:06 +02001104
Kevin Wolf08b392e2013-03-18 16:17:44 +01001105 set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +02001106
Max Reitzcc84d902013-09-06 17:14:26 +02001107 ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err);
Kevin Wolf08b392e2013-03-18 16:17:44 +01001108 free_option_parameters(create_options);
aliguori51d7c002009-03-05 23:00:29 +00001109 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001110 error_setg_errno(errp, -ret, "Could not create temporary overlay "
Max Reitzcc84d902013-09-06 17:14:26 +02001111 "'%s': %s", tmp_filename,
1112 error_get_pretty(local_err));
1113 error_free(local_err);
1114 local_err = NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001115 goto fail;
bellardea2384d2004-08-01 21:59:26 +00001116 }
Kevin Wolf91a073a2009-05-27 14:48:06 +02001117
Kevin Wolf9fd31712013-11-14 15:37:12 +01001118 /* Prepare a new options QDict for the temporary file, where user
1119 * options refer to the backing file */
1120 if (filename) {
1121 qdict_put(options, "file.filename", qstring_from_str(filename));
1122 }
1123 if (drv) {
1124 qdict_put(options, "driver", qstring_from_str(drv->format_name));
1125 }
1126
1127 snapshot_options = qdict_new();
1128 qdict_put(snapshot_options, "backing", options);
1129 qdict_flatten(snapshot_options);
1130
1131 bs->options = snapshot_options;
1132 options = qdict_clone_shallow(bs->options);
1133
bellardea2384d2004-08-01 21:59:26 +00001134 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +02001135 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +00001136 bs->is_temporary = 1;
1137 }
bellard712e7872005-04-28 21:09:32 +00001138
Kevin Wolff500a6d2012-11-12 17:35:27 +01001139 /* Open image file without format layer */
Jeff Codybe028ad2012-09-20 15:13:17 -04001140 if (flags & BDRV_O_RDWR) {
1141 flags |= BDRV_O_ALLOW_RDWR;
1142 }
1143
BenoƮt Canet5726d872013-09-25 13:30:01 +02001144 qdict_extract_subqdict(options, &file_options, "file.");
Kevin Wolf707ff822013-03-06 12:20:31 +01001145
Max Reitz72daa722013-12-20 19:28:08 +01001146 ret = bdrv_file_open(&file, filename, NULL, file_options,
Max Reitz34b5d2c2013-09-05 14:45:29 +02001147 bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001148 if (ret < 0) {
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001149 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001150 }
1151
1152 /* Find the right image format driver */
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001153 drvname = qdict_get_try_str(options, "driver");
1154 if (drvname) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +02001155 drv = bdrv_find_format(drvname);
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001156 qdict_del(options, "driver");
Kevin Wolf06d22aa2013-08-08 17:44:52 +02001157 if (!drv) {
1158 error_setg(errp, "Invalid driver: '%s'", drvname);
1159 ret = -EINVAL;
1160 goto unlink_and_fail;
1161 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001162 }
1163
Kevin Wolff500a6d2012-11-12 17:35:27 +01001164 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001165 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001166 }
1167
1168 if (!drv) {
1169 goto unlink_and_fail;
1170 }
1171
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001172 /* Open the image */
Max Reitz34b5d2c2013-09-05 14:45:29 +02001173 ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001174 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +01001175 goto unlink_and_fail;
1176 }
1177
Kevin Wolff500a6d2012-11-12 17:35:27 +01001178 if (bs->file != file) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001179 bdrv_unref(file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001180 file = NULL;
1181 }
1182
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001183 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001184 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001185 QDict *backing_options;
1186
BenoƮt Canet5726d872013-09-25 13:30:01 +02001187 qdict_extract_subqdict(options, &backing_options, "backing.");
Max Reitz34b5d2c2013-09-05 14:45:29 +02001188 ret = bdrv_open_backing_file(bs, backing_options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001189 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001190 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001191 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001192 }
1193
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001194 /* Check if any unknown options were used */
1195 if (qdict_size(options) != 0) {
1196 const QDictEntry *entry = qdict_first(options);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001197 error_setg(errp, "Block format '%s' used by device '%s' doesn't "
1198 "support the option '%s'", drv->format_name, bs->device_name,
1199 entry->key);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001200
1201 ret = -EINVAL;
1202 goto close_and_fail;
1203 }
1204 QDECREF(options);
1205
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001206 if (!bdrv_key_required(bs)) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001207 bdrv_dev_change_media_cb(bs, true);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001208 }
1209
1210 return 0;
1211
1212unlink_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001213 if (file != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001214 bdrv_unref(file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001215 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001216 if (bs->is_temporary) {
1217 unlink(filename);
1218 }
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001219fail:
1220 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001221 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001222 bs->options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001223 if (error_is_set(&local_err)) {
1224 error_propagate(errp, local_err);
1225 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001226 return ret;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001227
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001228close_and_fail:
1229 bdrv_close(bs);
1230 QDECREF(options);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001231 if (error_is_set(&local_err)) {
1232 error_propagate(errp, local_err);
1233 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001234 return ret;
1235}
1236
Jeff Codye971aa12012-09-20 15:13:19 -04001237typedef struct BlockReopenQueueEntry {
1238 bool prepared;
1239 BDRVReopenState state;
1240 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1241} BlockReopenQueueEntry;
1242
1243/*
1244 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1245 * reopen of multiple devices.
1246 *
1247 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1248 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1249 * be created and initialized. This newly created BlockReopenQueue should be
1250 * passed back in for subsequent calls that are intended to be of the same
1251 * atomic 'set'.
1252 *
1253 * bs is the BlockDriverState to add to the reopen queue.
1254 *
1255 * flags contains the open flags for the associated bs
1256 *
1257 * returns a pointer to bs_queue, which is either the newly allocated
1258 * bs_queue, or the existing bs_queue being used.
1259 *
1260 */
1261BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1262 BlockDriverState *bs, int flags)
1263{
1264 assert(bs != NULL);
1265
1266 BlockReopenQueueEntry *bs_entry;
1267 if (bs_queue == NULL) {
1268 bs_queue = g_new0(BlockReopenQueue, 1);
1269 QSIMPLEQ_INIT(bs_queue);
1270 }
1271
1272 if (bs->file) {
1273 bdrv_reopen_queue(bs_queue, bs->file, flags);
1274 }
1275
1276 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1277 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1278
1279 bs_entry->state.bs = bs;
1280 bs_entry->state.flags = flags;
1281
1282 return bs_queue;
1283}
1284
1285/*
1286 * Reopen multiple BlockDriverStates atomically & transactionally.
1287 *
1288 * The queue passed in (bs_queue) must have been built up previous
1289 * via bdrv_reopen_queue().
1290 *
1291 * Reopens all BDS specified in the queue, with the appropriate
1292 * flags. All devices are prepared for reopen, and failure of any
1293 * device will cause all device changes to be abandonded, and intermediate
1294 * data cleaned up.
1295 *
1296 * If all devices prepare successfully, then the changes are committed
1297 * to all devices.
1298 *
1299 */
1300int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1301{
1302 int ret = -1;
1303 BlockReopenQueueEntry *bs_entry, *next;
1304 Error *local_err = NULL;
1305
1306 assert(bs_queue != NULL);
1307
1308 bdrv_drain_all();
1309
1310 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1311 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1312 error_propagate(errp, local_err);
1313 goto cleanup;
1314 }
1315 bs_entry->prepared = true;
1316 }
1317
1318 /* If we reach this point, we have success and just need to apply the
1319 * changes
1320 */
1321 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1322 bdrv_reopen_commit(&bs_entry->state);
1323 }
1324
1325 ret = 0;
1326
1327cleanup:
1328 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1329 if (ret && bs_entry->prepared) {
1330 bdrv_reopen_abort(&bs_entry->state);
1331 }
1332 g_free(bs_entry);
1333 }
1334 g_free(bs_queue);
1335 return ret;
1336}
1337
1338
1339/* Reopen a single BlockDriverState with the specified flags. */
1340int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1341{
1342 int ret = -1;
1343 Error *local_err = NULL;
1344 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags);
1345
1346 ret = bdrv_reopen_multiple(queue, &local_err);
1347 if (local_err != NULL) {
1348 error_propagate(errp, local_err);
1349 }
1350 return ret;
1351}
1352
1353
1354/*
1355 * Prepares a BlockDriverState for reopen. All changes are staged in the
1356 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1357 * the block driver layer .bdrv_reopen_prepare()
1358 *
1359 * bs is the BlockDriverState to reopen
1360 * flags are the new open flags
1361 * queue is the reopen queue
1362 *
1363 * Returns 0 on success, non-zero on error. On error errp will be set
1364 * as well.
1365 *
1366 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1367 * It is the responsibility of the caller to then call the abort() or
1368 * commit() for any other BDS that have been left in a prepare() state
1369 *
1370 */
1371int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1372 Error **errp)
1373{
1374 int ret = -1;
1375 Error *local_err = NULL;
1376 BlockDriver *drv;
1377
1378 assert(reopen_state != NULL);
1379 assert(reopen_state->bs->drv != NULL);
1380 drv = reopen_state->bs->drv;
1381
1382 /* if we are to stay read-only, do not allow permission change
1383 * to r/w */
1384 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1385 reopen_state->flags & BDRV_O_RDWR) {
1386 error_set(errp, QERR_DEVICE_IS_READ_ONLY,
1387 reopen_state->bs->device_name);
1388 goto error;
1389 }
1390
1391
1392 ret = bdrv_flush(reopen_state->bs);
1393 if (ret) {
1394 error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive",
1395 strerror(-ret));
1396 goto error;
1397 }
1398
1399 if (drv->bdrv_reopen_prepare) {
1400 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1401 if (ret) {
1402 if (local_err != NULL) {
1403 error_propagate(errp, local_err);
1404 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04001405 error_setg(errp, "failed while preparing to reopen image '%s'",
1406 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04001407 }
1408 goto error;
1409 }
1410 } else {
1411 /* It is currently mandatory to have a bdrv_reopen_prepare()
1412 * handler for each supported drv. */
1413 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1414 drv->format_name, reopen_state->bs->device_name,
1415 "reopening of file");
1416 ret = -1;
1417 goto error;
1418 }
1419
1420 ret = 0;
1421
1422error:
1423 return ret;
1424}
1425
1426/*
1427 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1428 * makes them final by swapping the staging BlockDriverState contents into
1429 * the active BlockDriverState contents.
1430 */
1431void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1432{
1433 BlockDriver *drv;
1434
1435 assert(reopen_state != NULL);
1436 drv = reopen_state->bs->drv;
1437 assert(drv != NULL);
1438
1439 /* If there are any driver level actions to take */
1440 if (drv->bdrv_reopen_commit) {
1441 drv->bdrv_reopen_commit(reopen_state);
1442 }
1443
1444 /* set BDS specific flags now */
1445 reopen_state->bs->open_flags = reopen_state->flags;
1446 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1447 BDRV_O_CACHE_WB);
1448 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
1449}
1450
1451/*
1452 * Abort the reopen, and delete and free the staged changes in
1453 * reopen_state
1454 */
1455void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1456{
1457 BlockDriver *drv;
1458
1459 assert(reopen_state != NULL);
1460 drv = reopen_state->bs->drv;
1461 assert(drv != NULL);
1462
1463 if (drv->bdrv_reopen_abort) {
1464 drv->bdrv_reopen_abort(reopen_state);
1465 }
1466}
1467
1468
bellardfc01f7e2003-06-30 10:03:06 +00001469void bdrv_close(BlockDriverState *bs)
1470{
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02001471 if (bs->job) {
1472 block_job_cancel_sync(bs->job);
1473 }
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02001474 bdrv_drain_all(); /* complete I/O */
1475 bdrv_flush(bs);
1476 bdrv_drain_all(); /* in case flush left pending I/O */
Paolo Bonzinid7d512f2012-08-23 11:20:36 +02001477 notifier_list_notify(&bs->close_notifiers, bs);
Kevin Wolf7094f122012-04-11 11:06:37 +02001478
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02001479 if (bs->drv) {
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +01001480 if (bs->backing_hd) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001481 bdrv_unref(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +01001482 bs->backing_hd = NULL;
1483 }
bellardea2384d2004-08-01 21:59:26 +00001484 bs->drv->bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -05001485 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00001486#ifdef _WIN32
1487 if (bs->is_temporary) {
1488 unlink(bs->filename);
1489 }
bellard67b915a2004-03-31 23:37:16 +00001490#endif
bellardea2384d2004-08-01 21:59:26 +00001491 bs->opaque = NULL;
1492 bs->drv = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001493 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02001494 bs->backing_file[0] = '\0';
1495 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02001496 bs->total_sectors = 0;
1497 bs->encrypted = 0;
1498 bs->valid_key = 0;
1499 bs->sg = 0;
1500 bs->growable = 0;
Asias He0d51b4d2013-08-22 15:24:14 +08001501 bs->zero_beyond_eof = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001502 QDECREF(bs->options);
1503 bs->options = NULL;
bellardb3380822004-03-14 21:38:54 +00001504
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001505 if (bs->file != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001506 bdrv_unref(bs->file);
Paolo Bonzini0ac93772012-05-08 16:51:44 +02001507 bs->file = NULL;
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001508 }
bellardb3380822004-03-14 21:38:54 +00001509 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08001510
Pavel Hrdina9ca11152012-08-09 12:44:48 +02001511 bdrv_dev_change_media_cb(bs, false);
1512
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08001513 /*throttling disk I/O limits*/
1514 if (bs->io_limits_enabled) {
1515 bdrv_io_limits_disable(bs);
1516 }
bellardb3380822004-03-14 21:38:54 +00001517}
1518
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001519void bdrv_close_all(void)
1520{
1521 BlockDriverState *bs;
1522
1523 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1524 bdrv_close(bs);
1525 }
1526}
1527
Stefan Hajnoczi88266f52013-04-11 15:41:13 +02001528/* Check if any requests are in-flight (including throttled requests) */
1529static bool bdrv_requests_pending(BlockDriverState *bs)
1530{
1531 if (!QLIST_EMPTY(&bs->tracked_requests)) {
1532 return true;
1533 }
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02001534 if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) {
1535 return true;
1536 }
1537 if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) {
Stefan Hajnoczi88266f52013-04-11 15:41:13 +02001538 return true;
1539 }
1540 if (bs->file && bdrv_requests_pending(bs->file)) {
1541 return true;
1542 }
1543 if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) {
1544 return true;
1545 }
1546 return false;
1547}
1548
1549static bool bdrv_requests_pending_all(void)
1550{
1551 BlockDriverState *bs;
1552 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1553 if (bdrv_requests_pending(bs)) {
1554 return true;
1555 }
1556 }
1557 return false;
1558}
1559
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001560/*
1561 * Wait for pending requests to complete across all BlockDriverStates
1562 *
1563 * This function does not flush data to disk, use bdrv_flush_all() for that
1564 * after calling this function.
Zhi Yong Wu4c355d52012-04-12 14:00:57 +02001565 *
1566 * Note that completion of an asynchronous I/O operation can trigger any
1567 * number of other I/O operations on other devices---for example a coroutine
1568 * can be arbitrarily complex and a constant flow of I/O can come until the
1569 * coroutine is complete. Because of this, it is not possible to have a
1570 * function to drain a single device's I/O queue.
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001571 */
1572void bdrv_drain_all(void)
1573{
Stefan Hajnoczi88266f52013-04-11 15:41:13 +02001574 /* Always run first iteration so any pending completion BHs run */
1575 bool busy = true;
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001576 BlockDriverState *bs;
1577
Stefan Hajnoczi88266f52013-04-11 15:41:13 +02001578 while (busy) {
Zhi Yong Wu4c355d52012-04-12 14:00:57 +02001579 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Stefan Hajnoczi0b06ef32013-11-26 16:18:00 +01001580 bdrv_start_throttled_reqs(bs);
Zhi Yong Wu4c355d52012-04-12 14:00:57 +02001581 }
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001582
Stefan Hajnoczi88266f52013-04-11 15:41:13 +02001583 busy = bdrv_requests_pending_all();
1584 busy |= aio_poll(qemu_get_aio_context(), busy);
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001585 }
1586}
1587
Ryan Harperd22b2f42011-03-29 20:51:47 -05001588/* make a BlockDriverState anonymous by removing from bdrv_state list.
1589 Also, NULL terminate the device_name to prevent double remove */
1590void bdrv_make_anon(BlockDriverState *bs)
1591{
1592 if (bs->device_name[0] != '\0') {
1593 QTAILQ_REMOVE(&bdrv_states, bs, list);
1594 }
1595 bs->device_name[0] = '\0';
1596}
1597
Paolo Bonzinie023b2e2012-05-08 16:51:41 +02001598static void bdrv_rebind(BlockDriverState *bs)
1599{
1600 if (bs->drv && bs->drv->bdrv_rebind) {
1601 bs->drv->bdrv_rebind(bs);
1602 }
1603}
1604
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001605static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
1606 BlockDriverState *bs_src)
1607{
1608 /* move some fields that need to stay attached to the device */
1609 bs_dest->open_flags = bs_src->open_flags;
1610
1611 /* dev info */
1612 bs_dest->dev_ops = bs_src->dev_ops;
1613 bs_dest->dev_opaque = bs_src->dev_opaque;
1614 bs_dest->dev = bs_src->dev;
1615 bs_dest->buffer_alignment = bs_src->buffer_alignment;
1616 bs_dest->copy_on_read = bs_src->copy_on_read;
1617
1618 bs_dest->enable_write_cache = bs_src->enable_write_cache;
1619
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02001620 /* i/o throttled req */
1621 memcpy(&bs_dest->throttle_state,
1622 &bs_src->throttle_state,
1623 sizeof(ThrottleState));
1624 bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0];
1625 bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1];
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001626 bs_dest->io_limits_enabled = bs_src->io_limits_enabled;
1627
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001628 /* r/w error */
1629 bs_dest->on_read_error = bs_src->on_read_error;
1630 bs_dest->on_write_error = bs_src->on_write_error;
1631
1632 /* i/o status */
1633 bs_dest->iostatus_enabled = bs_src->iostatus_enabled;
1634 bs_dest->iostatus = bs_src->iostatus;
1635
1636 /* dirty bitmap */
Fam Zhenge4654d22013-11-13 18:29:43 +08001637 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001638
Fam Zheng9fcb0252013-08-23 09:14:46 +08001639 /* reference count */
1640 bs_dest->refcnt = bs_src->refcnt;
1641
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001642 /* job */
1643 bs_dest->in_use = bs_src->in_use;
1644 bs_dest->job = bs_src->job;
1645
1646 /* keep the same entry in bdrv_states */
1647 pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
1648 bs_src->device_name);
1649 bs_dest->list = bs_src->list;
1650}
1651
1652/*
1653 * Swap bs contents for two image chains while they are live,
1654 * while keeping required fields on the BlockDriverState that is
1655 * actually attached to a device.
1656 *
1657 * This will modify the BlockDriverState fields, and swap contents
1658 * between bs_new and bs_old. Both bs_new and bs_old are modified.
1659 *
1660 * bs_new is required to be anonymous.
1661 *
1662 * This function does not create any image files.
1663 */
1664void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
1665{
1666 BlockDriverState tmp;
1667
1668 /* bs_new must be anonymous and shouldn't have anything fancy enabled */
1669 assert(bs_new->device_name[0] == '\0');
Fam Zhenge4654d22013-11-13 18:29:43 +08001670 assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001671 assert(bs_new->job == NULL);
1672 assert(bs_new->dev == NULL);
1673 assert(bs_new->in_use == 0);
1674 assert(bs_new->io_limits_enabled == false);
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02001675 assert(!throttle_have_timer(&bs_new->throttle_state));
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001676
1677 tmp = *bs_new;
1678 *bs_new = *bs_old;
1679 *bs_old = tmp;
1680
1681 /* there are some fields that should not be swapped, move them back */
1682 bdrv_move_feature_fields(&tmp, bs_old);
1683 bdrv_move_feature_fields(bs_old, bs_new);
1684 bdrv_move_feature_fields(bs_new, &tmp);
1685
1686 /* bs_new shouldn't be in bdrv_states even after the swap! */
1687 assert(bs_new->device_name[0] == '\0');
1688
1689 /* Check a few fields that should remain attached to the device */
1690 assert(bs_new->dev == NULL);
1691 assert(bs_new->job == NULL);
1692 assert(bs_new->in_use == 0);
1693 assert(bs_new->io_limits_enabled == false);
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02001694 assert(!throttle_have_timer(&bs_new->throttle_state));
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001695
1696 bdrv_rebind(bs_new);
1697 bdrv_rebind(bs_old);
1698}
1699
Jeff Cody8802d1f2012-02-28 15:54:06 -05001700/*
1701 * Add new bs contents at the top of an image chain while the chain is
1702 * live, while keeping required fields on the top layer.
1703 *
1704 * This will modify the BlockDriverState fields, and swap contents
1705 * between bs_new and bs_top. Both bs_new and bs_top are modified.
1706 *
Jeff Codyf6801b82012-03-27 16:30:19 -04001707 * bs_new is required to be anonymous.
1708 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05001709 * This function does not create any image files.
1710 */
1711void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
1712{
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001713 bdrv_swap(bs_new, bs_top);
Jeff Cody8802d1f2012-02-28 15:54:06 -05001714
1715 /* The contents of 'tmp' will become bs_top, as we are
1716 * swapping bs_new and bs_top contents. */
Paolo Bonzini4ddc07c2012-06-14 16:55:02 +02001717 bs_top->backing_hd = bs_new;
1718 bs_top->open_flags &= ~BDRV_O_NO_BACKING;
1719 pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
1720 bs_new->filename);
1721 pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
1722 bs_new->drv ? bs_new->drv->format_name : "");
Jeff Cody8802d1f2012-02-28 15:54:06 -05001723}
1724
Fam Zheng4f6fd342013-08-23 09:14:47 +08001725static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00001726{
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001727 assert(!bs->dev);
Paolo Bonzini3e914652012-03-30 13:17:11 +02001728 assert(!bs->job);
1729 assert(!bs->in_use);
Fam Zheng4f6fd342013-08-23 09:14:47 +08001730 assert(!bs->refcnt);
Fam Zhenge4654d22013-11-13 18:29:43 +08001731 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
Markus Armbruster18846de2010-06-29 16:58:30 +02001732
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02001733 bdrv_close(bs);
1734
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001735 /* remove from list, if necessary */
Ryan Harperd22b2f42011-03-29 20:51:47 -05001736 bdrv_make_anon(bs);
aurel3234c6f052008-04-08 19:51:21 +00001737
Anthony Liguori7267c092011-08-20 22:09:37 -05001738 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001739}
1740
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001741int bdrv_attach_dev(BlockDriverState *bs, void *dev)
1742/* TODO change to DeviceState *dev when all users are qdevified */
Markus Armbruster18846de2010-06-29 16:58:30 +02001743{
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001744 if (bs->dev) {
Markus Armbruster18846de2010-06-29 16:58:30 +02001745 return -EBUSY;
1746 }
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001747 bs->dev = dev;
Luiz Capitulino28a72822011-09-26 17:43:50 -03001748 bdrv_iostatus_reset(bs);
Markus Armbruster18846de2010-06-29 16:58:30 +02001749 return 0;
1750}
1751
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001752/* TODO qdevified devices don't use this, remove when devices are qdevified */
1753void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
Markus Armbruster18846de2010-06-29 16:58:30 +02001754{
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001755 if (bdrv_attach_dev(bs, dev) < 0) {
1756 abort();
1757 }
1758}
1759
1760void bdrv_detach_dev(BlockDriverState *bs, void *dev)
1761/* TODO change to DeviceState *dev when all users are qdevified */
1762{
1763 assert(bs->dev == dev);
1764 bs->dev = NULL;
Markus Armbruster0e49de52011-08-03 15:07:41 +02001765 bs->dev_ops = NULL;
1766 bs->dev_opaque = NULL;
Markus Armbruster29e05f22011-09-06 18:58:57 +02001767 bs->buffer_alignment = 512;
Markus Armbruster18846de2010-06-29 16:58:30 +02001768}
1769
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001770/* TODO change to return DeviceState * when all users are qdevified */
1771void *bdrv_get_attached_dev(BlockDriverState *bs)
Markus Armbruster18846de2010-06-29 16:58:30 +02001772{
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001773 return bs->dev;
Markus Armbruster18846de2010-06-29 16:58:30 +02001774}
1775
Markus Armbruster0e49de52011-08-03 15:07:41 +02001776void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
1777 void *opaque)
1778{
1779 bs->dev_ops = ops;
1780 bs->dev_opaque = opaque;
1781}
1782
Paolo Bonzini32c81a42012-09-28 17:22:58 +02001783void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1784 enum MonitorEvent ev,
1785 BlockErrorAction action, bool is_read)
Luiz Capitulino329c0a42012-01-25 16:59:43 -02001786{
1787 QObject *data;
1788 const char *action_str;
1789
1790 switch (action) {
1791 case BDRV_ACTION_REPORT:
1792 action_str = "report";
1793 break;
1794 case BDRV_ACTION_IGNORE:
1795 action_str = "ignore";
1796 break;
1797 case BDRV_ACTION_STOP:
1798 action_str = "stop";
1799 break;
1800 default:
1801 abort();
1802 }
1803
1804 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1805 bdrv->device_name,
1806 action_str,
1807 is_read ? "read" : "write");
Paolo Bonzini32c81a42012-09-28 17:22:58 +02001808 monitor_protocol_event(ev, data);
Luiz Capitulino329c0a42012-01-25 16:59:43 -02001809
1810 qobject_decref(data);
1811}
1812
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02001813static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
1814{
1815 QObject *data;
1816
1817 data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
1818 bdrv_get_device_name(bs), ejected);
1819 monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
1820
1821 qobject_decref(data);
1822}
1823
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001824static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
Markus Armbruster0e49de52011-08-03 15:07:41 +02001825{
Markus Armbruster145feb12011-08-03 15:07:42 +02001826 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02001827 bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001828 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02001829 if (tray_was_closed) {
1830 /* tray open */
1831 bdrv_emit_qmp_eject_event(bs, true);
1832 }
1833 if (load) {
1834 /* tray close */
1835 bdrv_emit_qmp_eject_event(bs, false);
1836 }
Markus Armbruster145feb12011-08-03 15:07:42 +02001837 }
1838}
1839
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001840bool bdrv_dev_has_removable_media(BlockDriverState *bs)
1841{
1842 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
1843}
1844
Paolo Bonzini025ccaa2011-11-07 17:50:13 +01001845void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
1846{
1847 if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
1848 bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
1849 }
1850}
1851
Markus Armbrustere4def802011-09-06 18:58:53 +02001852bool bdrv_dev_is_tray_open(BlockDriverState *bs)
1853{
1854 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
1855 return bs->dev_ops->is_tray_open(bs->dev_opaque);
1856 }
1857 return false;
1858}
1859
Markus Armbruster145feb12011-08-03 15:07:42 +02001860static void bdrv_dev_resize_cb(BlockDriverState *bs)
1861{
1862 if (bs->dev_ops && bs->dev_ops->resize_cb) {
1863 bs->dev_ops->resize_cb(bs->dev_opaque);
Markus Armbruster0e49de52011-08-03 15:07:41 +02001864 }
1865}
1866
Markus Armbrusterf1076392011-09-06 18:58:46 +02001867bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
1868{
1869 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
1870 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
1871 }
1872 return false;
1873}
1874
aliguorie97fc192009-04-21 23:11:50 +00001875/*
1876 * Run consistency checks on an image
1877 *
Kevin Wolfe076f332010-06-29 11:43:13 +02001878 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02001879 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02001880 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00001881 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02001882int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00001883{
1884 if (bs->drv->bdrv_check == NULL) {
1885 return -ENOTSUP;
1886 }
1887
Kevin Wolfe076f332010-06-29 11:43:13 +02001888 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02001889 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00001890}
1891
Kevin Wolf8a426612010-07-16 17:17:01 +02001892#define COMMIT_BUF_SECTORS 2048
1893
bellard33e39632003-07-06 17:15:21 +00001894/* commit COW file into the raw image */
1895int bdrv_commit(BlockDriverState *bs)
1896{
bellard19cb3732006-08-19 11:45:59 +00001897 BlockDriver *drv = bs->drv;
Kevin Wolf8a426612010-07-16 17:17:01 +02001898 int64_t sector, total_sectors;
1899 int n, ro, open_flags;
Jeff Cody0bce5972012-09-20 15:13:34 -04001900 int ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +02001901 uint8_t *buf;
Jim Meyeringc2cba3d2012-10-04 13:09:46 +02001902 char filename[PATH_MAX];
bellard33e39632003-07-06 17:15:21 +00001903
bellard19cb3732006-08-19 11:45:59 +00001904 if (!drv)
1905 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001906
1907 if (!bs->backing_hd) {
1908 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +00001909 }
1910
Stefan Hajnoczi2d3735d2012-01-18 14:40:41 +00001911 if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
1912 return -EBUSY;
1913 }
1914
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001915 ro = bs->backing_hd->read_only;
Jim Meyeringc2cba3d2012-10-04 13:09:46 +02001916 /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
1917 pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001918 open_flags = bs->backing_hd->open_flags;
1919
1920 if (ro) {
Jeff Cody0bce5972012-09-20 15:13:34 -04001921 if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
1922 return -EACCES;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001923 }
bellard33e39632003-07-06 17:15:21 +00001924 }
bellardea2384d2004-08-01 21:59:26 +00001925
Jan Kiszka6ea44302009-11-30 18:21:19 +01001926 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -05001927 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +00001928
Kevin Wolf8a426612010-07-16 17:17:01 +02001929 for (sector = 0; sector < total_sectors; sector += n) {
Paolo Bonzinid6636402013-09-04 19:00:25 +02001930 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
1931 if (ret < 0) {
1932 goto ro_cleanup;
1933 }
1934 if (ret) {
Kevin Wolf8a426612010-07-16 17:17:01 +02001935 if (bdrv_read(bs, sector, buf, n) != 0) {
1936 ret = -EIO;
1937 goto ro_cleanup;
1938 }
1939
1940 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
1941 ret = -EIO;
1942 goto ro_cleanup;
1943 }
bellardea2384d2004-08-01 21:59:26 +00001944 }
1945 }
bellard95389c82005-12-18 18:28:15 +00001946
Christoph Hellwig1d449522010-01-17 12:32:30 +01001947 if (drv->bdrv_make_empty) {
1948 ret = drv->bdrv_make_empty(bs);
1949 bdrv_flush(bs);
1950 }
bellard95389c82005-12-18 18:28:15 +00001951
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001952 /*
1953 * Make sure all data we wrote to the backing device is actually
1954 * stable on disk.
1955 */
1956 if (bs->backing_hd)
1957 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001958
1959ro_cleanup:
Anthony Liguori7267c092011-08-20 22:09:37 -05001960 g_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001961
1962 if (ro) {
Jeff Cody0bce5972012-09-20 15:13:34 -04001963 /* ignoring error return here */
1964 bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02001965 }
1966
Christoph Hellwig1d449522010-01-17 12:32:30 +01001967 return ret;
bellard33e39632003-07-06 17:15:21 +00001968}
1969
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001970int bdrv_commit_all(void)
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001971{
1972 BlockDriverState *bs;
1973
1974 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Jeff Cody272d2d82013-02-26 09:55:48 -05001975 if (bs->drv && bs->backing_hd) {
1976 int ret = bdrv_commit(bs);
1977 if (ret < 0) {
1978 return ret;
1979 }
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001980 }
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001981 }
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001982 return 0;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001983}
1984
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00001985/**
1986 * Remove an active request from the tracked requests list
1987 *
1988 * This function should be called when a tracked request is completing.
1989 */
1990static void tracked_request_end(BdrvTrackedRequest *req)
1991{
1992 QLIST_REMOVE(req, list);
Stefan Hajnoczif4658282011-11-17 13:40:29 +00001993 qemu_co_queue_restart_all(&req->wait_queue);
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00001994}
1995
1996/**
1997 * Add an active request to the tracked requests list
1998 */
1999static void tracked_request_begin(BdrvTrackedRequest *req,
2000 BlockDriverState *bs,
2001 int64_t sector_num,
2002 int nb_sectors, bool is_write)
2003{
2004 *req = (BdrvTrackedRequest){
2005 .bs = bs,
2006 .sector_num = sector_num,
2007 .nb_sectors = nb_sectors,
2008 .is_write = is_write,
Stefan Hajnoczi5f8b6492011-11-30 12:23:42 +00002009 .co = qemu_coroutine_self(),
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002010 };
2011
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002012 qemu_co_queue_init(&req->wait_queue);
2013
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002014 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
2015}
2016
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002017/**
2018 * Round a region to cluster boundaries
2019 */
Paolo Bonzini343bded2013-01-21 17:09:42 +01002020void bdrv_round_to_clusters(BlockDriverState *bs,
2021 int64_t sector_num, int nb_sectors,
2022 int64_t *cluster_sector_num,
2023 int *cluster_nb_sectors)
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002024{
2025 BlockDriverInfo bdi;
2026
2027 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
2028 *cluster_sector_num = sector_num;
2029 *cluster_nb_sectors = nb_sectors;
2030 } else {
2031 int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
2032 *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
2033 *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
2034 nb_sectors, c);
2035 }
2036}
2037
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002038static bool tracked_request_overlaps(BdrvTrackedRequest *req,
2039 int64_t sector_num, int nb_sectors) {
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002040 /* aaaa bbbb */
2041 if (sector_num >= req->sector_num + req->nb_sectors) {
2042 return false;
2043 }
2044 /* bbbb aaaa */
2045 if (req->sector_num >= sector_num + nb_sectors) {
2046 return false;
2047 }
2048 return true;
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002049}
2050
2051static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
2052 int64_t sector_num, int nb_sectors)
2053{
2054 BdrvTrackedRequest *req;
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002055 int64_t cluster_sector_num;
2056 int cluster_nb_sectors;
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002057 bool retry;
2058
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002059 /* If we touch the same cluster it counts as an overlap. This guarantees
2060 * that allocating writes will be serialized and not race with each other
2061 * for the same cluster. For example, in copy-on-read it ensures that the
2062 * CoR read and write operations are atomic and guest writes cannot
2063 * interleave between them.
2064 */
Paolo Bonzini343bded2013-01-21 17:09:42 +01002065 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2066 &cluster_sector_num, &cluster_nb_sectors);
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002067
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002068 do {
2069 retry = false;
2070 QLIST_FOREACH(req, &bs->tracked_requests, list) {
Stefan Hajnoczid83947a2011-11-23 11:47:56 +00002071 if (tracked_request_overlaps(req, cluster_sector_num,
2072 cluster_nb_sectors)) {
Stefan Hajnoczi5f8b6492011-11-30 12:23:42 +00002073 /* Hitting this means there was a reentrant request, for
2074 * example, a block driver issuing nested requests. This must
2075 * never happen since it means deadlock.
2076 */
2077 assert(qemu_coroutine_self() != req->co);
2078
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002079 qemu_co_queue_wait(&req->wait_queue);
2080 retry = true;
2081 break;
2082 }
2083 }
2084 } while (retry);
2085}
2086
Kevin Wolf756e6732010-01-12 12:55:17 +01002087/*
2088 * Return values:
2089 * 0 - success
2090 * -EINVAL - backing format specified, but no file
2091 * -ENOSPC - can't update the backing file because no space is left in the
2092 * image file header
2093 * -ENOTSUP - format driver doesn't support changing the backing file
2094 */
2095int bdrv_change_backing_file(BlockDriverState *bs,
2096 const char *backing_file, const char *backing_fmt)
2097{
2098 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002099 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002100
Paolo Bonzini5f377792012-04-12 14:01:01 +02002101 /* Backing file format doesn't make sense without a backing file */
2102 if (backing_fmt && !backing_file) {
2103 return -EINVAL;
2104 }
2105
Kevin Wolf756e6732010-01-12 12:55:17 +01002106 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002107 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002108 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002109 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002110 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002111
2112 if (ret == 0) {
2113 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2114 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2115 }
2116 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002117}
2118
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002119/*
2120 * Finds the image layer in the chain that has 'bs' as its backing file.
2121 *
2122 * active is the current topmost image.
2123 *
2124 * Returns NULL if bs is not found in active's image chain,
2125 * or if active == bs.
2126 */
2127BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2128 BlockDriverState *bs)
2129{
2130 BlockDriverState *overlay = NULL;
2131 BlockDriverState *intermediate;
2132
2133 assert(active != NULL);
2134 assert(bs != NULL);
2135
2136 /* if bs is the same as active, then by definition it has no overlay
2137 */
2138 if (active == bs) {
2139 return NULL;
2140 }
2141
2142 intermediate = active;
2143 while (intermediate->backing_hd) {
2144 if (intermediate->backing_hd == bs) {
2145 overlay = intermediate;
2146 break;
2147 }
2148 intermediate = intermediate->backing_hd;
2149 }
2150
2151 return overlay;
2152}
2153
2154typedef struct BlkIntermediateStates {
2155 BlockDriverState *bs;
2156 QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
2157} BlkIntermediateStates;
2158
2159
2160/*
2161 * Drops images above 'base' up to and including 'top', and sets the image
2162 * above 'top' to have base as its backing file.
2163 *
2164 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2165 * information in 'bs' can be properly updated.
2166 *
2167 * E.g., this will convert the following chain:
2168 * bottom <- base <- intermediate <- top <- active
2169 *
2170 * to
2171 *
2172 * bottom <- base <- active
2173 *
2174 * It is allowed for bottom==base, in which case it converts:
2175 *
2176 * base <- intermediate <- top <- active
2177 *
2178 * to
2179 *
2180 * base <- active
2181 *
2182 * Error conditions:
2183 * if active == top, that is considered an error
2184 *
2185 */
2186int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2187 BlockDriverState *base)
2188{
2189 BlockDriverState *intermediate;
2190 BlockDriverState *base_bs = NULL;
2191 BlockDriverState *new_top_bs = NULL;
2192 BlkIntermediateStates *intermediate_state, *next;
2193 int ret = -EIO;
2194
2195 QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
2196 QSIMPLEQ_INIT(&states_to_delete);
2197
2198 if (!top->drv || !base->drv) {
2199 goto exit;
2200 }
2201
2202 new_top_bs = bdrv_find_overlay(active, top);
2203
2204 if (new_top_bs == NULL) {
2205 /* we could not find the image above 'top', this is an error */
2206 goto exit;
2207 }
2208
2209 /* special case of new_top_bs->backing_hd already pointing to base - nothing
2210 * to do, no intermediate images */
2211 if (new_top_bs->backing_hd == base) {
2212 ret = 0;
2213 goto exit;
2214 }
2215
2216 intermediate = top;
2217
2218 /* now we will go down through the list, and add each BDS we find
2219 * into our deletion queue, until we hit the 'base'
2220 */
2221 while (intermediate) {
2222 intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
2223 intermediate_state->bs = intermediate;
2224 QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
2225
2226 if (intermediate->backing_hd == base) {
2227 base_bs = intermediate->backing_hd;
2228 break;
2229 }
2230 intermediate = intermediate->backing_hd;
2231 }
2232 if (base_bs == NULL) {
2233 /* something went wrong, we did not end at the base. safely
2234 * unravel everything, and exit with error */
2235 goto exit;
2236 }
2237
2238 /* success - we can delete the intermediate states, and link top->base */
2239 ret = bdrv_change_backing_file(new_top_bs, base_bs->filename,
2240 base_bs->drv ? base_bs->drv->format_name : "");
2241 if (ret) {
2242 goto exit;
2243 }
2244 new_top_bs->backing_hd = base_bs;
2245
2246
2247 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2248 /* so that bdrv_close() does not recursively close the chain */
2249 intermediate_state->bs->backing_hd = NULL;
Fam Zheng4f6fd342013-08-23 09:14:47 +08002250 bdrv_unref(intermediate_state->bs);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002251 }
2252 ret = 0;
2253
2254exit:
2255 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2256 g_free(intermediate_state);
2257 }
2258 return ret;
2259}
2260
2261
aliguori71d07702009-03-03 17:37:16 +00002262static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
2263 size_t size)
2264{
2265 int64_t len;
2266
2267 if (!bdrv_is_inserted(bs))
2268 return -ENOMEDIUM;
2269
2270 if (bs->growable)
2271 return 0;
2272
2273 len = bdrv_getlength(bs);
2274
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02002275 if (offset < 0)
2276 return -EIO;
2277
2278 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +00002279 return -EIO;
2280
2281 return 0;
2282}
2283
2284static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
2285 int nb_sectors)
2286{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002287 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
2288 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +00002289}
2290
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002291typedef struct RwCo {
2292 BlockDriverState *bs;
2293 int64_t sector_num;
2294 int nb_sectors;
2295 QEMUIOVector *qiov;
2296 bool is_write;
2297 int ret;
Peter Lieven4105eaa2013-07-11 14:16:22 +02002298 BdrvRequestFlags flags;
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002299} RwCo;
2300
2301static void coroutine_fn bdrv_rw_co_entry(void *opaque)
2302{
2303 RwCo *rwco = opaque;
2304
2305 if (!rwco->is_write) {
2306 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
Peter Lieven4105eaa2013-07-11 14:16:22 +02002307 rwco->nb_sectors, rwco->qiov,
2308 rwco->flags);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002309 } else {
2310 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
Peter Lieven4105eaa2013-07-11 14:16:22 +02002311 rwco->nb_sectors, rwco->qiov,
2312 rwco->flags);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002313 }
2314}
2315
2316/*
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002317 * Process a vectored synchronous request using coroutines
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002318 */
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002319static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num,
Peter Lieven4105eaa2013-07-11 14:16:22 +02002320 QEMUIOVector *qiov, bool is_write,
2321 BdrvRequestFlags flags)
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002322{
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002323 Coroutine *co;
2324 RwCo rwco = {
2325 .bs = bs,
2326 .sector_num = sector_num,
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002327 .nb_sectors = qiov->size >> BDRV_SECTOR_BITS,
2328 .qiov = qiov,
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002329 .is_write = is_write,
2330 .ret = NOT_DONE,
Peter Lieven4105eaa2013-07-11 14:16:22 +02002331 .flags = flags,
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002332 };
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002333 assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002334
Zhi Yong Wu498e3862012-04-02 18:59:34 +08002335 /**
2336 * In sync call context, when the vcpu is blocked, this throttling timer
2337 * will not fire; so the I/O throttling function has to be disabled here
2338 * if it has been enabled.
2339 */
2340 if (bs->io_limits_enabled) {
2341 fprintf(stderr, "Disabling I/O throttling on '%s' due "
2342 "to synchronous I/O.\n", bdrv_get_device_name(bs));
2343 bdrv_io_limits_disable(bs);
2344 }
2345
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01002346 if (qemu_in_coroutine()) {
2347 /* Fast-path if already in coroutine context */
2348 bdrv_rw_co_entry(&rwco);
2349 } else {
2350 co = qemu_coroutine_create(bdrv_rw_co_entry);
2351 qemu_coroutine_enter(co, &rwco);
2352 while (rwco.ret == NOT_DONE) {
2353 qemu_aio_wait();
2354 }
2355 }
2356 return rwco.ret;
2357}
2358
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002359/*
2360 * Process a synchronous request using coroutines
2361 */
2362static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
Peter Lieven4105eaa2013-07-11 14:16:22 +02002363 int nb_sectors, bool is_write, BdrvRequestFlags flags)
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002364{
2365 QEMUIOVector qiov;
2366 struct iovec iov = {
2367 .iov_base = (void *)buf,
2368 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
2369 };
2370
2371 qemu_iovec_init_external(&qiov, &iov, 1);
Peter Lieven4105eaa2013-07-11 14:16:22 +02002372 return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags);
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002373}
2374
bellard19cb3732006-08-19 11:45:59 +00002375/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +00002376int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00002377 uint8_t *buf, int nb_sectors)
2378{
Peter Lieven4105eaa2013-07-11 14:16:22 +02002379 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
bellardfc01f7e2003-06-30 10:03:06 +00002380}
2381
Markus Armbruster07d27a42012-06-29 17:34:29 +02002382/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2383int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
2384 uint8_t *buf, int nb_sectors)
2385{
2386 bool enabled;
2387 int ret;
2388
2389 enabled = bs->io_limits_enabled;
2390 bs->io_limits_enabled = false;
Peter Lieven4e7395e2013-07-18 10:37:32 +02002391 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
Markus Armbruster07d27a42012-06-29 17:34:29 +02002392 bs->io_limits_enabled = enabled;
2393 return ret;
2394}
2395
ths5fafdf22007-09-16 21:08:06 +00002396/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +00002397 -EIO generic I/O error (may happen for all errors)
2398 -ENOMEDIUM No media inserted.
2399 -EINVAL Invalid sector number or nb_sectors
2400 -EACCES Trying to write a read-only device
2401*/
ths5fafdf22007-09-16 21:08:06 +00002402int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00002403 const uint8_t *buf, int nb_sectors)
2404{
Peter Lieven4105eaa2013-07-11 14:16:22 +02002405 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
bellard83f64092006-08-01 16:21:11 +00002406}
2407
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002408int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2409{
Peter Lieven4105eaa2013-07-11 14:16:22 +02002410 return bdrv_rwv_co(bs, sector_num, qiov, true, 0);
2411}
2412
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002413int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
2414 int nb_sectors, BdrvRequestFlags flags)
Peter Lieven4105eaa2013-07-11 14:16:22 +02002415{
2416 return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002417 BDRV_REQ_ZERO_WRITE | flags);
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002418}
2419
Peter Lievend75cbb52013-10-24 12:07:03 +02002420/*
2421 * Completely zero out a block device with the help of bdrv_write_zeroes.
2422 * The operation is sped up by checking the block status and only writing
2423 * zeroes to the device if they currently do not return zeroes. Optional
2424 * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
2425 *
2426 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
2427 */
2428int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)
2429{
2430 int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
2431 int64_t ret, nb_sectors, sector_num = 0;
2432 int n;
2433
2434 for (;;) {
2435 nb_sectors = target_size - sector_num;
2436 if (nb_sectors <= 0) {
2437 return 0;
2438 }
2439 if (nb_sectors > INT_MAX) {
2440 nb_sectors = INT_MAX;
2441 }
2442 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);
Peter Lieven3d94ce62013-12-12 13:57:05 +01002443 if (ret < 0) {
2444 error_report("error getting block status at sector %" PRId64 ": %s",
2445 sector_num, strerror(-ret));
2446 return ret;
2447 }
Peter Lievend75cbb52013-10-24 12:07:03 +02002448 if (ret & BDRV_BLOCK_ZERO) {
2449 sector_num += n;
2450 continue;
2451 }
2452 ret = bdrv_write_zeroes(bs, sector_num, n, flags);
2453 if (ret < 0) {
2454 error_report("error writing zeroes at sector %" PRId64 ": %s",
2455 sector_num, strerror(-ret));
2456 return ret;
2457 }
2458 sector_num += n;
2459 }
2460}
2461
aliguorieda578e2009-03-12 19:57:16 +00002462int bdrv_pread(BlockDriverState *bs, int64_t offset,
2463 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00002464{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002465 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00002466 int len, nb_sectors, count;
2467 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002468 int ret;
bellard83f64092006-08-01 16:21:11 +00002469
2470 count = count1;
2471 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01002472 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00002473 if (len > count)
2474 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01002475 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002476 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002477 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2478 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01002479 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +00002480 count -= len;
2481 if (count == 0)
2482 return count1;
2483 sector_num++;
2484 buf += len;
2485 }
2486
2487 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01002488 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002489 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002490 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
2491 return ret;
bellard83f64092006-08-01 16:21:11 +00002492 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01002493 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002494 buf += len;
2495 count -= len;
2496 }
2497
2498 /* add data from the last sector */
2499 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002500 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2501 return ret;
bellard83f64092006-08-01 16:21:11 +00002502 memcpy(buf, tmp_buf, count);
2503 }
2504 return count1;
2505}
2506
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002507int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
bellard83f64092006-08-01 16:21:11 +00002508{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002509 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00002510 int len, nb_sectors, count;
2511 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002512 int ret;
bellard83f64092006-08-01 16:21:11 +00002513
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002514 count = qiov->size;
2515
bellard83f64092006-08-01 16:21:11 +00002516 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01002517 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00002518 if (len > count)
2519 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01002520 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002521 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002522 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2523 return ret;
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002524 qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)),
2525 len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002526 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2527 return ret;
bellard83f64092006-08-01 16:21:11 +00002528 count -= len;
2529 if (count == 0)
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002530 return qiov->size;
bellard83f64092006-08-01 16:21:11 +00002531 sector_num++;
bellard83f64092006-08-01 16:21:11 +00002532 }
2533
2534 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01002535 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002536 if (nb_sectors > 0) {
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002537 QEMUIOVector qiov_inplace;
2538
2539 qemu_iovec_init(&qiov_inplace, qiov->niov);
2540 qemu_iovec_concat(&qiov_inplace, qiov, len,
2541 nb_sectors << BDRV_SECTOR_BITS);
2542 ret = bdrv_writev(bs, sector_num, &qiov_inplace);
2543 qemu_iovec_destroy(&qiov_inplace);
2544 if (ret < 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002545 return ret;
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002546 }
2547
bellard83f64092006-08-01 16:21:11 +00002548 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01002549 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00002550 count -= len;
2551 }
2552
2553 /* add data from the last sector */
2554 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002555 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2556 return ret;
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002557 qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01002558 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2559 return ret;
bellard83f64092006-08-01 16:21:11 +00002560 }
Kevin Wolf8d3b1a22013-04-05 21:27:55 +02002561 return qiov->size;
2562}
2563
2564int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
2565 const void *buf, int count1)
2566{
2567 QEMUIOVector qiov;
2568 struct iovec iov = {
2569 .iov_base = (void *) buf,
2570 .iov_len = count1,
2571 };
2572
2573 qemu_iovec_init_external(&qiov, &iov, 1);
2574 return bdrv_pwritev(bs, offset, &qiov);
bellard83f64092006-08-01 16:21:11 +00002575}
bellard83f64092006-08-01 16:21:11 +00002576
Kevin Wolff08145f2010-06-16 16:38:15 +02002577/*
2578 * Writes to the file and ensures that no writes are reordered across this
2579 * request (acts as a barrier)
2580 *
2581 * Returns 0 on success, -errno in error cases.
2582 */
2583int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
2584 const void *buf, int count)
2585{
2586 int ret;
2587
2588 ret = bdrv_pwrite(bs, offset, buf, count);
2589 if (ret < 0) {
2590 return ret;
2591 }
2592
Paolo Bonzinif05fa4a2012-06-06 00:04:49 +02002593 /* No flush needed for cache modes that already do it */
2594 if (bs->enable_write_cache) {
Kevin Wolff08145f2010-06-16 16:38:15 +02002595 bdrv_flush(bs);
2596 }
2597
2598 return 0;
2599}
2600
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002601static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
Stefan Hajnocziab185922011-11-17 13:40:31 +00002602 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2603{
2604 /* Perform I/O through a temporary buffer so that users who scribble over
2605 * their read buffer while the operation is in progress do not end up
2606 * modifying the image file. This is critical for zero-copy guest I/O
2607 * where anything might happen inside guest memory.
2608 */
2609 void *bounce_buffer;
2610
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002611 BlockDriver *drv = bs->drv;
Stefan Hajnocziab185922011-11-17 13:40:31 +00002612 struct iovec iov;
2613 QEMUIOVector bounce_qiov;
2614 int64_t cluster_sector_num;
2615 int cluster_nb_sectors;
2616 size_t skip_bytes;
2617 int ret;
2618
2619 /* Cover entire cluster so no additional backing file I/O is required when
2620 * allocating cluster in the image file.
2621 */
Paolo Bonzini343bded2013-01-21 17:09:42 +01002622 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2623 &cluster_sector_num, &cluster_nb_sectors);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002624
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002625 trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
2626 cluster_sector_num, cluster_nb_sectors);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002627
2628 iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
2629 iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
2630 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
2631
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002632 ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
2633 &bounce_qiov);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002634 if (ret < 0) {
2635 goto err;
2636 }
2637
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002638 if (drv->bdrv_co_write_zeroes &&
2639 buffer_is_zero(bounce_buffer, iov.iov_len)) {
Kevin Wolf621f0582012-03-20 15:12:58 +01002640 ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002641 cluster_nb_sectors, 0);
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002642 } else {
Paolo Bonzinif05fa4a2012-06-06 00:04:49 +02002643 /* This does not change the data on the disk, it is not necessary
2644 * to flush even in cache=writethrough mode.
2645 */
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002646 ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
Stefan Hajnocziab185922011-11-17 13:40:31 +00002647 &bounce_qiov);
Stefan Hajnoczi79c053b2012-02-07 13:27:26 +00002648 }
2649
Stefan Hajnocziab185922011-11-17 13:40:31 +00002650 if (ret < 0) {
2651 /* It might be okay to ignore write errors for guest requests. If this
2652 * is a deliberate copy-on-read then we don't want to ignore the error.
2653 * Simply report it in all cases.
2654 */
2655 goto err;
2656 }
2657
2658 skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
Michael Tokarev03396142012-06-07 20:17:55 +04002659 qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
2660 nb_sectors * BDRV_SECTOR_SIZE);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002661
2662err:
2663 qemu_vfree(bounce_buffer);
2664 return ret;
2665}
2666
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002667/*
2668 * Handle a read request in coroutine context
2669 */
2670static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002671 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2672 BdrvRequestFlags flags)
Kevin Wolfda1fa912011-07-14 17:27:13 +02002673{
2674 BlockDriver *drv = bs->drv;
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002675 BdrvTrackedRequest req;
2676 int ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02002677
Kevin Wolfda1fa912011-07-14 17:27:13 +02002678 if (!drv) {
2679 return -ENOMEDIUM;
2680 }
2681 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2682 return -EIO;
2683 }
2684
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002685 if (bs->copy_on_read) {
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002686 flags |= BDRV_REQ_COPY_ON_READ;
2687 }
2688 if (flags & BDRV_REQ_COPY_ON_READ) {
2689 bs->copy_on_read_in_flight++;
2690 }
2691
2692 if (bs->copy_on_read_in_flight) {
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002693 wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2694 }
2695
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02002696 /* throttling disk I/O */
2697 if (bs->io_limits_enabled) {
2698 bdrv_io_limits_intercept(bs, nb_sectors, false);
2699 }
2700
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002701 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002702
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002703 if (flags & BDRV_REQ_COPY_ON_READ) {
Stefan Hajnocziab185922011-11-17 13:40:31 +00002704 int pnum;
2705
Paolo Bonzinibdad13b2013-09-04 19:00:22 +02002706 ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002707 if (ret < 0) {
2708 goto out;
2709 }
2710
2711 if (!ret || pnum != nb_sectors) {
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002712 ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
Stefan Hajnocziab185922011-11-17 13:40:31 +00002713 goto out;
2714 }
2715 }
2716
MORITA Kazutaka893a8f62013-08-06 09:53:40 +08002717 if (!(bs->zero_beyond_eof && bs->growable)) {
2718 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
2719 } else {
2720 /* Read zeros after EOF of growable BDSes */
2721 int64_t len, total_sectors, max_nb_sectors;
2722
2723 len = bdrv_getlength(bs);
2724 if (len < 0) {
2725 ret = len;
2726 goto out;
2727 }
2728
Fam Zhengd055a1f2013-09-26 19:55:33 +08002729 total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE);
MORITA Kazutaka893a8f62013-08-06 09:53:40 +08002730 max_nb_sectors = MAX(0, total_sectors - sector_num);
2731 if (max_nb_sectors > 0) {
2732 ret = drv->bdrv_co_readv(bs, sector_num,
2733 MIN(nb_sectors, max_nb_sectors), qiov);
2734 } else {
2735 ret = 0;
2736 }
2737
2738 /* Reading beyond end of file is supposed to produce zeroes */
2739 if (ret == 0 && total_sectors < sector_num + nb_sectors) {
2740 uint64_t offset = MAX(0, total_sectors - sector_num);
2741 uint64_t bytes = (sector_num + nb_sectors - offset) *
2742 BDRV_SECTOR_SIZE;
2743 qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes);
2744 }
2745 }
Stefan Hajnocziab185922011-11-17 13:40:31 +00002746
2747out:
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002748 tracked_request_end(&req);
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002749
2750 if (flags & BDRV_REQ_COPY_ON_READ) {
2751 bs->copy_on_read_in_flight--;
2752 }
2753
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002754 return ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02002755}
2756
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002757int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
Kevin Wolfda1fa912011-07-14 17:27:13 +02002758 int nb_sectors, QEMUIOVector *qiov)
2759{
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002760 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
Kevin Wolfda1fa912011-07-14 17:27:13 +02002761
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002762 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
2763}
2764
2765int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
2766 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2767{
2768 trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
2769
2770 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
2771 BDRV_REQ_COPY_ON_READ);
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002772}
2773
Peter Lievenc31cb702013-10-24 12:06:58 +02002774/* if no limit is specified in the BlockLimits use a default
2775 * of 32768 512-byte sectors (16 MiB) per request.
2776 */
2777#define MAX_WRITE_ZEROES_DEFAULT 32768
2778
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002779static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002780 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002781{
2782 BlockDriver *drv = bs->drv;
2783 QEMUIOVector qiov;
Peter Lievenc31cb702013-10-24 12:06:58 +02002784 struct iovec iov = {0};
2785 int ret = 0;
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002786
Peter Lievenc31cb702013-10-24 12:06:58 +02002787 int max_write_zeroes = bs->bl.max_write_zeroes ?
2788 bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT;
Kevin Wolf621f0582012-03-20 15:12:58 +01002789
Peter Lievenc31cb702013-10-24 12:06:58 +02002790 while (nb_sectors > 0 && !ret) {
2791 int num = nb_sectors;
2792
Paolo Bonzinib8d71c02013-11-22 13:39:48 +01002793 /* Align request. Block drivers can expect the "bulk" of the request
2794 * to be aligned.
2795 */
2796 if (bs->bl.write_zeroes_alignment
2797 && num > bs->bl.write_zeroes_alignment) {
2798 if (sector_num % bs->bl.write_zeroes_alignment != 0) {
2799 /* Make a small request up to the first aligned sector. */
Peter Lievenc31cb702013-10-24 12:06:58 +02002800 num = bs->bl.write_zeroes_alignment;
Paolo Bonzinib8d71c02013-11-22 13:39:48 +01002801 num -= sector_num % bs->bl.write_zeroes_alignment;
2802 } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) {
2803 /* Shorten the request to the last aligned sector. num cannot
2804 * underflow because num > bs->bl.write_zeroes_alignment.
2805 */
2806 num -= (sector_num + num) % bs->bl.write_zeroes_alignment;
Peter Lievenc31cb702013-10-24 12:06:58 +02002807 }
Kevin Wolf621f0582012-03-20 15:12:58 +01002808 }
Peter Lievenc31cb702013-10-24 12:06:58 +02002809
2810 /* limit request size */
2811 if (num > max_write_zeroes) {
2812 num = max_write_zeroes;
2813 }
2814
2815 ret = -ENOTSUP;
2816 /* First try the efficient write zeroes operation */
2817 if (drv->bdrv_co_write_zeroes) {
2818 ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags);
2819 }
2820
2821 if (ret == -ENOTSUP) {
2822 /* Fall back to bounce buffer if write zeroes is unsupported */
2823 iov.iov_len = num * BDRV_SECTOR_SIZE;
2824 if (iov.iov_base == NULL) {
Paolo Bonzinib8d71c02013-11-22 13:39:48 +01002825 iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE);
2826 memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE);
Peter Lievenc31cb702013-10-24 12:06:58 +02002827 }
2828 qemu_iovec_init_external(&qiov, &iov, 1);
2829
2830 ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov);
Paolo Bonzinib8d71c02013-11-22 13:39:48 +01002831
2832 /* Keep bounce buffer around if it is big enough for all
2833 * all future requests.
2834 */
2835 if (num < max_write_zeroes) {
2836 qemu_vfree(iov.iov_base);
2837 iov.iov_base = NULL;
2838 }
Peter Lievenc31cb702013-10-24 12:06:58 +02002839 }
2840
2841 sector_num += num;
2842 nb_sectors -= num;
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002843 }
2844
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002845 qemu_vfree(iov.iov_base);
2846 return ret;
2847}
2848
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002849/*
2850 * Handle a write request in coroutine context
2851 */
2852static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002853 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2854 BdrvRequestFlags flags)
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002855{
2856 BlockDriver *drv = bs->drv;
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002857 BdrvTrackedRequest req;
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01002858 int ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02002859
2860 if (!bs->drv) {
2861 return -ENOMEDIUM;
2862 }
2863 if (bs->read_only) {
2864 return -EACCES;
2865 }
2866 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2867 return -EIO;
2868 }
2869
Stefan Hajnoczi470c0502012-01-18 14:40:42 +00002870 if (bs->copy_on_read_in_flight) {
Stefan Hajnoczif4658282011-11-17 13:40:29 +00002871 wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2872 }
2873
BenoƮt Canetcc0681c2013-09-02 14:14:39 +02002874 /* throttling disk I/O */
2875 if (bs->io_limits_enabled) {
2876 bdrv_io_limits_intercept(bs, nb_sectors, true);
2877 }
2878
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002879 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2880
Stefan Hajnoczid616b222013-06-24 17:13:10 +02002881 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
2882
2883 if (ret < 0) {
2884 /* Do nothing, write notifier decided to fail this request */
2885 } else if (flags & BDRV_REQ_ZERO_WRITE) {
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002886 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags);
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002887 } else {
2888 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
2889 }
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01002890
Paolo Bonzinif05fa4a2012-06-06 00:04:49 +02002891 if (ret == 0 && !bs->enable_write_cache) {
2892 ret = bdrv_co_flush(bs);
2893 }
2894
Fam Zhenge4654d22013-11-13 18:29:43 +08002895 bdrv_set_dirty(bs, sector_num, nb_sectors);
Kevin Wolfda1fa912011-07-14 17:27:13 +02002896
2897 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2898 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2899 }
Paolo Bonzinidf2a6f22013-09-04 19:00:21 +02002900 if (bs->growable && ret >= 0) {
2901 bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
2902 }
Kevin Wolfda1fa912011-07-14 17:27:13 +02002903
Stefan Hajnoczidbffbdc2011-11-17 13:40:27 +00002904 tracked_request_end(&req);
2905
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01002906 return ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02002907}
2908
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002909int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
2910 int nb_sectors, QEMUIOVector *qiov)
2911{
2912 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
2913
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002914 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
2915}
2916
2917int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002918 int64_t sector_num, int nb_sectors,
2919 BdrvRequestFlags flags)
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002920{
Paolo Bonzini94d6ff22013-11-22 13:39:45 +01002921 trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002922
Peter Lievend32f35c2013-10-24 12:06:52 +02002923 if (!(bs->open_flags & BDRV_O_UNMAP)) {
2924 flags &= ~BDRV_REQ_MAY_UNMAP;
2925 }
2926
Stefan Hajnoczif08f2dd2012-02-07 13:27:25 +00002927 return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
Peter Lievenaa7bfbf2013-10-24 12:06:51 +02002928 BDRV_REQ_ZERO_WRITE | flags);
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01002929}
2930
bellard83f64092006-08-01 16:21:11 +00002931/**
bellard83f64092006-08-01 16:21:11 +00002932 * Truncate file to 'offset' bytes (needed only for file protocols)
2933 */
2934int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2935{
2936 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002937 int ret;
bellard83f64092006-08-01 16:21:11 +00002938 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002939 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002940 if (!drv->bdrv_truncate)
2941 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002942 if (bs->read_only)
2943 return -EACCES;
Marcelo Tosatti85916752011-01-26 12:12:35 -02002944 if (bdrv_in_use(bs))
2945 return -EBUSY;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002946 ret = drv->bdrv_truncate(bs, offset);
2947 if (ret == 0) {
2948 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
Markus Armbruster145feb12011-08-03 15:07:42 +02002949 bdrv_dev_resize_cb(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002950 }
2951 return ret;
bellard83f64092006-08-01 16:21:11 +00002952}
2953
2954/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002955 * Length of a allocated file in bytes. Sparse files are counted by actual
2956 * allocated space. Return < 0 if error or unknown.
2957 */
2958int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2959{
2960 BlockDriver *drv = bs->drv;
2961 if (!drv) {
2962 return -ENOMEDIUM;
2963 }
2964 if (drv->bdrv_get_allocated_file_size) {
2965 return drv->bdrv_get_allocated_file_size(bs);
2966 }
2967 if (bs->file) {
2968 return bdrv_get_allocated_file_size(bs->file);
2969 }
2970 return -ENOTSUP;
2971}
2972
2973/**
bellard83f64092006-08-01 16:21:11 +00002974 * Length of a file in bytes. Return < 0 if error or unknown.
2975 */
2976int64_t bdrv_getlength(BlockDriverState *bs)
2977{
2978 BlockDriver *drv = bs->drv;
2979 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002980 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002981
Kevin Wolfb94a2612013-10-29 12:18:58 +01002982 if (drv->has_variable_length) {
2983 int ret = refresh_total_sectors(bs, bs->total_sectors);
2984 if (ret < 0) {
2985 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002986 }
bellard83f64092006-08-01 16:21:11 +00002987 }
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002988 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002989}
2990
bellard19cb3732006-08-19 11:45:59 +00002991/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002992void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002993{
bellard19cb3732006-08-19 11:45:59 +00002994 int64_t length;
2995 length = bdrv_getlength(bs);
2996 if (length < 0)
2997 length = 0;
2998 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01002999 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00003000 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00003001}
bellardcf989512004-02-16 21:56:36 +00003002
Paolo Bonziniff06f5f2012-09-28 17:22:54 +02003003void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
3004 BlockdevOnError on_write_error)
Markus Armbrusterabd7f682010-06-02 18:55:17 +02003005{
3006 bs->on_read_error = on_read_error;
3007 bs->on_write_error = on_write_error;
3008}
3009
Paolo Bonzini1ceee0d2012-09-28 17:22:56 +02003010BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
Markus Armbrusterabd7f682010-06-02 18:55:17 +02003011{
3012 return is_read ? bs->on_read_error : bs->on_write_error;
3013}
3014
Paolo Bonzini3e1caa52012-09-28 17:22:57 +02003015BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error)
3016{
3017 BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error;
3018
3019 switch (on_err) {
3020 case BLOCKDEV_ON_ERROR_ENOSPC:
3021 return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
3022 case BLOCKDEV_ON_ERROR_STOP:
3023 return BDRV_ACTION_STOP;
3024 case BLOCKDEV_ON_ERROR_REPORT:
3025 return BDRV_ACTION_REPORT;
3026 case BLOCKDEV_ON_ERROR_IGNORE:
3027 return BDRV_ACTION_IGNORE;
3028 default:
3029 abort();
3030 }
3031}
3032
3033/* This is done by device models because, while the block layer knows
3034 * about the error, it does not know whether an operation comes from
3035 * the device or the block layer (from a job, for example).
3036 */
3037void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
3038 bool is_read, int error)
3039{
3040 assert(error >= 0);
Paolo Bonzini32c81a42012-09-28 17:22:58 +02003041 bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read);
Paolo Bonzini3e1caa52012-09-28 17:22:57 +02003042 if (action == BDRV_ACTION_STOP) {
3043 vm_stop(RUN_STATE_IO_ERROR);
3044 bdrv_iostatus_set_err(bs, error);
3045 }
3046}
3047
bellardb3380822004-03-14 21:38:54 +00003048int bdrv_is_read_only(BlockDriverState *bs)
3049{
3050 return bs->read_only;
3051}
3052
ths985a03b2007-12-24 16:10:43 +00003053int bdrv_is_sg(BlockDriverState *bs)
3054{
3055 return bs->sg;
3056}
3057
Christoph Hellwige900a7b2009-09-04 19:01:15 +02003058int bdrv_enable_write_cache(BlockDriverState *bs)
3059{
3060 return bs->enable_write_cache;
3061}
3062
Paolo Bonzini425b0142012-06-06 00:04:52 +02003063void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
3064{
3065 bs->enable_write_cache = wce;
Jeff Cody55b110f2012-09-20 15:13:18 -04003066
3067 /* so a reopen() will preserve wce */
3068 if (wce) {
3069 bs->open_flags |= BDRV_O_CACHE_WB;
3070 } else {
3071 bs->open_flags &= ~BDRV_O_CACHE_WB;
3072 }
Paolo Bonzini425b0142012-06-06 00:04:52 +02003073}
3074
bellardea2384d2004-08-01 21:59:26 +00003075int bdrv_is_encrypted(BlockDriverState *bs)
3076{
3077 if (bs->backing_hd && bs->backing_hd->encrypted)
3078 return 1;
3079 return bs->encrypted;
3080}
3081
aliguoric0f4ce72009-03-05 23:01:01 +00003082int bdrv_key_required(BlockDriverState *bs)
3083{
3084 BlockDriverState *backing_hd = bs->backing_hd;
3085
3086 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
3087 return 1;
3088 return (bs->encrypted && !bs->valid_key);
3089}
3090
bellardea2384d2004-08-01 21:59:26 +00003091int bdrv_set_key(BlockDriverState *bs, const char *key)
3092{
3093 int ret;
3094 if (bs->backing_hd && bs->backing_hd->encrypted) {
3095 ret = bdrv_set_key(bs->backing_hd, key);
3096 if (ret < 0)
3097 return ret;
3098 if (!bs->encrypted)
3099 return 0;
3100 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02003101 if (!bs->encrypted) {
3102 return -EINVAL;
3103 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
3104 return -ENOMEDIUM;
3105 }
aliguoric0f4ce72009-03-05 23:01:01 +00003106 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00003107 if (ret < 0) {
3108 bs->valid_key = 0;
3109 } else if (!bs->valid_key) {
3110 bs->valid_key = 1;
3111 /* call the change callback now, we skipped it on open */
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02003112 bdrv_dev_change_media_cb(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00003113 }
aliguoric0f4ce72009-03-05 23:01:01 +00003114 return ret;
bellardea2384d2004-08-01 21:59:26 +00003115}
3116
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02003117const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00003118{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02003119 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00003120}
3121
ths5fafdf22007-09-16 21:08:06 +00003122void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00003123 void *opaque)
3124{
3125 BlockDriver *drv;
3126
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01003127 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00003128 it(opaque, drv->format_name);
3129 }
3130}
3131
bellardb3380822004-03-14 21:38:54 +00003132BlockDriverState *bdrv_find(const char *name)
3133{
3134 BlockDriverState *bs;
3135
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003136 QTAILQ_FOREACH(bs, &bdrv_states, list) {
3137 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00003138 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003139 }
bellardb3380822004-03-14 21:38:54 +00003140 }
3141 return NULL;
3142}
3143
Markus Armbruster2f399b02010-06-02 18:55:20 +02003144BlockDriverState *bdrv_next(BlockDriverState *bs)
3145{
3146 if (!bs) {
3147 return QTAILQ_FIRST(&bdrv_states);
3148 }
3149 return QTAILQ_NEXT(bs, list);
3150}
3151
aliguori51de9762009-03-05 23:00:43 +00003152void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00003153{
3154 BlockDriverState *bs;
3155
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003156 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00003157 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00003158 }
3159}
3160
bellardea2384d2004-08-01 21:59:26 +00003161const char *bdrv_get_device_name(BlockDriverState *bs)
3162{
3163 return bs->device_name;
3164}
3165
Markus Armbrusterc8433282012-06-05 16:49:24 +02003166int bdrv_get_flags(BlockDriverState *bs)
3167{
3168 return bs->open_flags;
3169}
3170
Kevin Wolff0f0fdf2013-07-05 13:48:01 +02003171int bdrv_flush_all(void)
aliguoric6ca28d2008-10-06 13:55:43 +00003172{
3173 BlockDriverState *bs;
Kevin Wolff0f0fdf2013-07-05 13:48:01 +02003174 int result = 0;
aliguoric6ca28d2008-10-06 13:55:43 +00003175
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003176 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolff0f0fdf2013-07-05 13:48:01 +02003177 int ret = bdrv_flush(bs);
3178 if (ret < 0 && !result) {
3179 result = ret;
3180 }
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003181 }
Kevin Wolff0f0fdf2013-07-05 13:48:01 +02003182
3183 return result;
aliguoric6ca28d2008-10-06 13:55:43 +00003184}
3185
Peter Lieven3ac21622013-06-28 12:47:42 +02003186int bdrv_has_zero_init_1(BlockDriverState *bs)
3187{
3188 return 1;
3189}
3190
Kevin Wolff2feebb2010-04-14 17:30:35 +02003191int bdrv_has_zero_init(BlockDriverState *bs)
3192{
3193 assert(bs->drv);
3194
Paolo Bonzini11212d82013-09-04 19:00:27 +02003195 /* If BS is a copy on write image, it is initialized to
3196 the contents of the base image, which may not be zeroes. */
3197 if (bs->backing_hd) {
3198 return 0;
3199 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02003200 if (bs->drv->bdrv_has_zero_init) {
3201 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02003202 }
3203
Peter Lieven3ac21622013-06-28 12:47:42 +02003204 /* safe default */
3205 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02003206}
3207
Peter Lieven4ce78692013-10-24 12:06:54 +02003208bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
3209{
3210 BlockDriverInfo bdi;
3211
3212 if (bs->backing_hd) {
3213 return false;
3214 }
3215
3216 if (bdrv_get_info(bs, &bdi) == 0) {
3217 return bdi.unallocated_blocks_are_zero;
3218 }
3219
3220 return false;
3221}
3222
3223bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3224{
3225 BlockDriverInfo bdi;
3226
3227 if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) {
3228 return false;
3229 }
3230
3231 if (bdrv_get_info(bs, &bdi) == 0) {
3232 return bdi.can_write_zeroes_with_unmap;
3233 }
3234
3235 return false;
3236}
3237
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003238typedef struct BdrvCoGetBlockStatusData {
Stefan Hajnoczi376ae3f2011-11-14 12:44:19 +00003239 BlockDriverState *bs;
Miroslav Rezaninab35b2bb2013-02-13 09:09:39 +01003240 BlockDriverState *base;
Stefan Hajnoczi376ae3f2011-11-14 12:44:19 +00003241 int64_t sector_num;
3242 int nb_sectors;
3243 int *pnum;
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003244 int64_t ret;
Stefan Hajnoczi376ae3f2011-11-14 12:44:19 +00003245 bool done;
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003246} BdrvCoGetBlockStatusData;
Stefan Hajnoczi376ae3f2011-11-14 12:44:19 +00003247
thsf58c7b32008-06-05 21:53:49 +00003248/*
3249 * Returns true iff the specified sector is present in the disk image. Drivers
3250 * not implementing the functionality are assumed to not support backing files,
3251 * hence all their sectors are reported as allocated.
3252 *
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003253 * If 'sector_num' is beyond the end of the disk image the return value is 0
3254 * and 'pnum' is set to 0.
3255 *
thsf58c7b32008-06-05 21:53:49 +00003256 * 'pnum' is set to the number of sectors (including and immediately following
3257 * the specified sector) that are known to be in the same
3258 * allocated/unallocated state.
3259 *
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003260 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
3261 * beyond the end of the disk image it will be clamped.
thsf58c7b32008-06-05 21:53:49 +00003262 */
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003263static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
3264 int64_t sector_num,
3265 int nb_sectors, int *pnum)
thsf58c7b32008-06-05 21:53:49 +00003266{
Paolo Bonzini617ccb42013-09-04 19:00:23 +02003267 int64_t length;
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003268 int64_t n;
Paolo Bonzini5daa74a2013-09-04 19:00:38 +02003269 int64_t ret, ret2;
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003270
Paolo Bonzini617ccb42013-09-04 19:00:23 +02003271 length = bdrv_getlength(bs);
3272 if (length < 0) {
3273 return length;
3274 }
3275
3276 if (sector_num >= (length >> BDRV_SECTOR_BITS)) {
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003277 *pnum = 0;
3278 return 0;
3279 }
3280
3281 n = bs->total_sectors - sector_num;
3282 if (n < nb_sectors) {
3283 nb_sectors = n;
3284 }
3285
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003286 if (!bs->drv->bdrv_co_get_block_status) {
Stefan Hajnoczibd9533e2011-11-29 13:49:51 +00003287 *pnum = nb_sectors;
Paolo Bonzini918e92d2013-09-04 19:00:37 +02003288 ret = BDRV_BLOCK_DATA;
3289 if (bs->drv->protocol_name) {
3290 ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
3291 }
3292 return ret;
thsf58c7b32008-06-05 21:53:49 +00003293 }
Stefan Hajnoczi6aebab12011-11-14 12:44:25 +00003294
Paolo Bonzini415b5b02013-09-04 19:00:31 +02003295 ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
3296 if (ret < 0) {
Peter Lieven3e0a2332013-09-24 15:35:08 +02003297 *pnum = 0;
Paolo Bonzini415b5b02013-09-04 19:00:31 +02003298 return ret;
3299 }
3300
Peter Lieven92bc50a2013-10-08 14:43:14 +02003301 if (ret & BDRV_BLOCK_RAW) {
3302 assert(ret & BDRV_BLOCK_OFFSET_VALID);
3303 return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
3304 *pnum, pnum);
3305 }
3306
Peter Lievenc3d86882013-10-24 12:07:04 +02003307 if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
3308 if (bdrv_unallocated_blocks_are_zero(bs)) {
Paolo Bonzinif0ad5712013-09-04 19:00:32 +02003309 ret |= BDRV_BLOCK_ZERO;
Peter Lieven1f9db222013-09-24 15:35:09 +02003310 } else if (bs->backing_hd) {
Paolo Bonzinif0ad5712013-09-04 19:00:32 +02003311 BlockDriverState *bs2 = bs->backing_hd;
3312 int64_t length2 = bdrv_getlength(bs2);
3313 if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) {
3314 ret |= BDRV_BLOCK_ZERO;
3315 }
3316 }
Paolo Bonzini415b5b02013-09-04 19:00:31 +02003317 }
Paolo Bonzini5daa74a2013-09-04 19:00:38 +02003318
3319 if (bs->file &&
3320 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
3321 (ret & BDRV_BLOCK_OFFSET_VALID)) {
3322 ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
3323 *pnum, pnum);
3324 if (ret2 >= 0) {
3325 /* Ignore errors. This is just providing extra information, it
3326 * is useful but not necessary.
3327 */
3328 ret |= (ret2 & BDRV_BLOCK_ZERO);
3329 }
3330 }
3331
Paolo Bonzini415b5b02013-09-04 19:00:31 +02003332 return ret;
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003333}
3334
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003335/* Coroutine wrapper for bdrv_get_block_status() */
3336static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque)
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003337{
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003338 BdrvCoGetBlockStatusData *data = opaque;
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003339 BlockDriverState *bs = data->bs;
3340
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003341 data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors,
3342 data->pnum);
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003343 data->done = true;
3344}
3345
3346/*
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003347 * Synchronous wrapper around bdrv_co_get_block_status().
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003348 *
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003349 * See bdrv_co_get_block_status() for details.
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003350 */
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003351int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
3352 int nb_sectors, int *pnum)
Stefan Hajnoczi060f51c2011-11-14 12:44:26 +00003353{
Stefan Hajnoczi6aebab12011-11-14 12:44:25 +00003354 Coroutine *co;
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003355 BdrvCoGetBlockStatusData data = {
Stefan Hajnoczi6aebab12011-11-14 12:44:25 +00003356 .bs = bs,
3357 .sector_num = sector_num,
3358 .nb_sectors = nb_sectors,
3359 .pnum = pnum,
3360 .done = false,
3361 };
3362
Paolo Bonzinibdad13b2013-09-04 19:00:22 +02003363 if (qemu_in_coroutine()) {
3364 /* Fast-path if already in coroutine context */
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003365 bdrv_get_block_status_co_entry(&data);
Paolo Bonzinibdad13b2013-09-04 19:00:22 +02003366 } else {
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003367 co = qemu_coroutine_create(bdrv_get_block_status_co_entry);
Paolo Bonzinibdad13b2013-09-04 19:00:22 +02003368 qemu_coroutine_enter(co, &data);
3369 while (!data.done) {
3370 qemu_aio_wait();
3371 }
Stefan Hajnoczi6aebab12011-11-14 12:44:25 +00003372 }
3373 return data.ret;
thsf58c7b32008-06-05 21:53:49 +00003374}
3375
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003376int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
3377 int nb_sectors, int *pnum)
3378{
Paolo Bonzini4333bb72013-09-04 19:00:29 +02003379 int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
3380 if (ret < 0) {
3381 return ret;
3382 }
3383 return
3384 (ret & BDRV_BLOCK_DATA) ||
3385 ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003386}
3387
Paolo Bonzini188a7bb2012-05-08 16:52:01 +02003388/*
3389 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
3390 *
3391 * Return true if the given sector is allocated in any image between
3392 * BASE and TOP (inclusive). BASE can be NULL to check if the given
3393 * sector is allocated in any image of the chain. Return false otherwise.
3394 *
3395 * 'pnum' is set to the number of sectors (including and immediately following
3396 * the specified sector) that are known to be in the same
3397 * allocated/unallocated state.
3398 *
3399 */
Paolo Bonzini4f578632013-09-04 19:00:24 +02003400int bdrv_is_allocated_above(BlockDriverState *top,
3401 BlockDriverState *base,
3402 int64_t sector_num,
3403 int nb_sectors, int *pnum)
Paolo Bonzini188a7bb2012-05-08 16:52:01 +02003404{
3405 BlockDriverState *intermediate;
3406 int ret, n = nb_sectors;
3407
3408 intermediate = top;
3409 while (intermediate && intermediate != base) {
3410 int pnum_inter;
Paolo Bonzinibdad13b2013-09-04 19:00:22 +02003411 ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors,
3412 &pnum_inter);
Paolo Bonzini188a7bb2012-05-08 16:52:01 +02003413 if (ret < 0) {
3414 return ret;
3415 } else if (ret) {
3416 *pnum = pnum_inter;
3417 return 1;
3418 }
3419
3420 /*
3421 * [sector_num, nb_sectors] is unallocated on top but intermediate
3422 * might have
3423 *
3424 * [sector_num+x, nr_sectors] allocated.
3425 */
Vishvananda Ishaya63ba17d2013-01-24 10:02:08 -08003426 if (n > pnum_inter &&
3427 (intermediate == top ||
3428 sector_num + pnum_inter < intermediate->total_sectors)) {
Paolo Bonzini188a7bb2012-05-08 16:52:01 +02003429 n = pnum_inter;
3430 }
3431
3432 intermediate = intermediate->backing_hd;
3433 }
3434
3435 *pnum = n;
3436 return 0;
3437}
3438
aliguori045df332009-03-05 23:00:48 +00003439const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3440{
3441 if (bs->backing_hd && bs->backing_hd->encrypted)
3442 return bs->backing_file;
3443 else if (bs->encrypted)
3444 return bs->filename;
3445 else
3446 return NULL;
3447}
3448
ths5fafdf22007-09-16 21:08:06 +00003449void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00003450 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00003451{
Kevin Wolf3574c602011-10-26 11:02:11 +02003452 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00003453}
3454
ths5fafdf22007-09-16 21:08:06 +00003455int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00003456 const uint8_t *buf, int nb_sectors)
3457{
3458 BlockDriver *drv = bs->drv;
3459 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003460 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00003461 if (!drv->bdrv_write_compressed)
3462 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02003463 if (bdrv_check_request(bs, sector_num, nb_sectors))
3464 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003465
Fam Zhenge4654d22013-11-13 18:29:43 +08003466 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003467
bellardfaea38e2006-08-05 21:31:00 +00003468 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
3469}
ths3b46e622007-09-17 08:09:54 +00003470
bellardfaea38e2006-08-05 21:31:00 +00003471int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3472{
3473 BlockDriver *drv = bs->drv;
3474 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003475 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00003476 if (!drv->bdrv_get_info)
3477 return -ENOTSUP;
3478 memset(bdi, 0, sizeof(*bdi));
3479 return drv->bdrv_get_info(bs, bdi);
3480}
3481
Max Reitzeae041f2013-10-09 10:46:16 +02003482ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3483{
3484 BlockDriver *drv = bs->drv;
3485 if (drv && drv->bdrv_get_specific_info) {
3486 return drv->bdrv_get_specific_info(bs);
3487 }
3488 return NULL;
3489}
3490
Christoph Hellwig45566e92009-07-10 23:11:57 +02003491int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
3492 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00003493{
Kevin Wolfcf8074b2013-04-05 21:27:53 +02003494 QEMUIOVector qiov;
3495 struct iovec iov = {
3496 .iov_base = (void *) buf,
3497 .iov_len = size,
3498 };
3499
3500 qemu_iovec_init_external(&qiov, &iov, 1);
3501 return bdrv_writev_vmstate(bs, &qiov, pos);
3502}
3503
3504int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
3505{
aliguori178e08a2009-04-05 19:10:55 +00003506 BlockDriver *drv = bs->drv;
Kevin Wolfcf8074b2013-04-05 21:27:53 +02003507
3508 if (!drv) {
aliguori178e08a2009-04-05 19:10:55 +00003509 return -ENOMEDIUM;
Kevin Wolfcf8074b2013-04-05 21:27:53 +02003510 } else if (drv->bdrv_save_vmstate) {
3511 return drv->bdrv_save_vmstate(bs, qiov, pos);
3512 } else if (bs->file) {
3513 return bdrv_writev_vmstate(bs->file, qiov, pos);
3514 }
3515
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09003516 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00003517}
3518
Christoph Hellwig45566e92009-07-10 23:11:57 +02003519int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
3520 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00003521{
3522 BlockDriver *drv = bs->drv;
3523 if (!drv)
3524 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09003525 if (drv->bdrv_load_vmstate)
3526 return drv->bdrv_load_vmstate(bs, buf, pos, size);
3527 if (bs->file)
3528 return bdrv_load_vmstate(bs->file, buf, pos, size);
3529 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00003530}
3531
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003532void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
3533{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003534 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003535 return;
3536 }
3537
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003538 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01003539}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003540
Kevin Wolf41c695c2012-12-06 14:32:58 +01003541int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3542 const char *tag)
3543{
3544 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
3545 bs = bs->file;
3546 }
3547
3548 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3549 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3550 }
3551
3552 return -ENOTSUP;
3553}
3554
Fam Zheng4cc70e92013-11-20 10:01:54 +08003555int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3556{
3557 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
3558 bs = bs->file;
3559 }
3560
3561 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3562 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3563 }
3564
3565 return -ENOTSUP;
3566}
3567
Kevin Wolf41c695c2012-12-06 14:32:58 +01003568int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3569{
3570 while (bs && bs->drv && !bs->drv->bdrv_debug_resume) {
3571 bs = bs->file;
3572 }
3573
3574 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3575 return bs->drv->bdrv_debug_resume(bs, tag);
3576 }
3577
3578 return -ENOTSUP;
3579}
3580
3581bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3582{
3583 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
3584 bs = bs->file;
3585 }
3586
3587 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3588 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3589 }
3590
3591 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003592}
3593
Blue Swirl199630b2010-07-25 20:49:34 +00003594int bdrv_is_snapshot(BlockDriverState *bs)
3595{
3596 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3597}
3598
Jeff Codyb1b1d782012-10-16 15:49:09 -04003599/* backing_file can either be relative, or absolute, or a protocol. If it is
3600 * relative, it must be relative to the chain. So, passing in bs->filename
3601 * from a BDS as backing_file should not be done, as that may be relative to
3602 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003603BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3604 const char *backing_file)
3605{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003606 char *filename_full = NULL;
3607 char *backing_file_full = NULL;
3608 char *filename_tmp = NULL;
3609 int is_protocol = 0;
3610 BlockDriverState *curr_bs = NULL;
3611 BlockDriverState *retval = NULL;
3612
3613 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003614 return NULL;
3615 }
3616
Jeff Codyb1b1d782012-10-16 15:49:09 -04003617 filename_full = g_malloc(PATH_MAX);
3618 backing_file_full = g_malloc(PATH_MAX);
3619 filename_tmp = g_malloc(PATH_MAX);
3620
3621 is_protocol = path_has_protocol(backing_file);
3622
3623 for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
3624
3625 /* If either of the filename paths is actually a protocol, then
3626 * compare unmodified paths; otherwise make paths relative */
3627 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3628 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3629 retval = curr_bs->backing_hd;
3630 break;
3631 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003632 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003633 /* If not an absolute filename path, make it relative to the current
3634 * image's filename path */
3635 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3636 backing_file);
3637
3638 /* We are going to compare absolute pathnames */
3639 if (!realpath(filename_tmp, filename_full)) {
3640 continue;
3641 }
3642
3643 /* We need to make sure the backing filename we are comparing against
3644 * is relative to the current image filename (or absolute) */
3645 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3646 curr_bs->backing_file);
3647
3648 if (!realpath(filename_tmp, backing_file_full)) {
3649 continue;
3650 }
3651
3652 if (strcmp(backing_file_full, filename_full) == 0) {
3653 retval = curr_bs->backing_hd;
3654 break;
3655 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003656 }
3657 }
3658
Jeff Codyb1b1d782012-10-16 15:49:09 -04003659 g_free(filename_full);
3660 g_free(backing_file_full);
3661 g_free(filename_tmp);
3662 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003663}
3664
BenoƮt Canetf198fd12012-08-02 10:22:47 +02003665int bdrv_get_backing_file_depth(BlockDriverState *bs)
3666{
3667 if (!bs->drv) {
3668 return 0;
3669 }
3670
3671 if (!bs->backing_hd) {
3672 return 0;
3673 }
3674
3675 return 1 + bdrv_get_backing_file_depth(bs->backing_hd);
3676}
3677
Jeff Cody79fac562012-09-27 13:29:15 -04003678BlockDriverState *bdrv_find_base(BlockDriverState *bs)
3679{
3680 BlockDriverState *curr_bs = NULL;
3681
3682 if (!bs) {
3683 return NULL;
3684 }
3685
3686 curr_bs = bs;
3687
3688 while (curr_bs->backing_hd) {
3689 curr_bs = curr_bs->backing_hd;
3690 }
3691 return curr_bs;
3692}
3693
bellard83f64092006-08-01 16:21:11 +00003694/**************************************************************/
3695/* async I/Os */
3696
aliguori3b69e4b2009-01-22 16:59:24 +00003697BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00003698 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00003699 BlockDriverCompletionFunc *cb, void *opaque)
3700{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01003701 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
3702
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01003703 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01003704 cb, opaque, false);
bellard83f64092006-08-01 16:21:11 +00003705}
3706
aliguorif141eaf2009-04-07 18:43:24 +00003707BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
3708 QEMUIOVector *qiov, int nb_sectors,
3709 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00003710{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01003711 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
3712
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01003713 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01003714 cb, opaque, true);
bellard83f64092006-08-01 16:21:11 +00003715}
3716
Paolo Bonzinid5ef94d2013-11-22 13:39:46 +01003717BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs,
3718 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags,
3719 BlockDriverCompletionFunc *cb, void *opaque)
3720{
3721 trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque);
3722
3723 return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors,
3724 BDRV_REQ_ZERO_WRITE | flags,
3725 cb, opaque, true);
3726}
3727
Kevin Wolf40b4f532009-09-09 17:53:37 +02003728
3729typedef struct MultiwriteCB {
3730 int error;
3731 int num_requests;
3732 int num_callbacks;
3733 struct {
3734 BlockDriverCompletionFunc *cb;
3735 void *opaque;
3736 QEMUIOVector *free_qiov;
Kevin Wolf40b4f532009-09-09 17:53:37 +02003737 } callbacks[];
3738} MultiwriteCB;
3739
3740static void multiwrite_user_cb(MultiwriteCB *mcb)
3741{
3742 int i;
3743
3744 for (i = 0; i < mcb->num_callbacks; i++) {
3745 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01003746 if (mcb->callbacks[i].free_qiov) {
3747 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
3748 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003749 g_free(mcb->callbacks[i].free_qiov);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003750 }
3751}
3752
3753static void multiwrite_cb(void *opaque, int ret)
3754{
3755 MultiwriteCB *mcb = opaque;
3756
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01003757 trace_multiwrite_cb(mcb, ret);
3758
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02003759 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02003760 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02003761 }
3762
3763 mcb->num_requests--;
3764 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02003765 multiwrite_user_cb(mcb);
Anthony Liguori7267c092011-08-20 22:09:37 -05003766 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003767 }
3768}
3769
3770static int multiwrite_req_compare(const void *a, const void *b)
3771{
Christoph Hellwig77be4362010-05-19 20:53:10 +02003772 const BlockRequest *req1 = a, *req2 = b;
3773
3774 /*
3775 * Note that we can't simply subtract req2->sector from req1->sector
3776 * here as that could overflow the return value.
3777 */
3778 if (req1->sector > req2->sector) {
3779 return 1;
3780 } else if (req1->sector < req2->sector) {
3781 return -1;
3782 } else {
3783 return 0;
3784 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02003785}
3786
3787/*
3788 * Takes a bunch of requests and tries to merge them. Returns the number of
3789 * requests that remain after merging.
3790 */
3791static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
3792 int num_reqs, MultiwriteCB *mcb)
3793{
3794 int i, outidx;
3795
3796 // Sort requests by start sector
3797 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
3798
3799 // Check if adjacent requests touch the same clusters. If so, combine them,
3800 // filling up gaps with zero sectors.
3801 outidx = 0;
3802 for (i = 1; i < num_reqs; i++) {
3803 int merge = 0;
3804 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
3805
Paolo Bonzinib6a127a2012-02-21 16:43:52 +01003806 // Handle exactly sequential writes and overlapping writes.
Kevin Wolf40b4f532009-09-09 17:53:37 +02003807 if (reqs[i].sector <= oldreq_last) {
3808 merge = 1;
3809 }
3810
Christoph Hellwige2a305f2010-01-26 14:49:08 +01003811 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
3812 merge = 0;
3813 }
3814
Kevin Wolf40b4f532009-09-09 17:53:37 +02003815 if (merge) {
3816 size_t size;
Anthony Liguori7267c092011-08-20 22:09:37 -05003817 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
Kevin Wolf40b4f532009-09-09 17:53:37 +02003818 qemu_iovec_init(qiov,
3819 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
3820
3821 // Add the first request to the merged one. If the requests are
3822 // overlapping, drop the last sectors of the first request.
3823 size = (reqs[i].sector - reqs[outidx].sector) << 9;
Michael Tokarev1b093c42012-03-12 21:28:06 +04003824 qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003825
Paolo Bonzinib6a127a2012-02-21 16:43:52 +01003826 // We should need to add any zeros between the two requests
3827 assert (reqs[i].sector <= oldreq_last);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003828
3829 // Add the second request
Michael Tokarev1b093c42012-03-12 21:28:06 +04003830 qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003831
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02003832 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02003833 reqs[outidx].qiov = qiov;
3834
3835 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
3836 } else {
3837 outidx++;
3838 reqs[outidx].sector = reqs[i].sector;
3839 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
3840 reqs[outidx].qiov = reqs[i].qiov;
3841 }
3842 }
3843
3844 return outidx + 1;
3845}
3846
3847/*
3848 * Submit multiple AIO write requests at once.
3849 *
3850 * On success, the function returns 0 and all requests in the reqs array have
3851 * been submitted. In error case this function returns -1, and any of the
3852 * requests may or may not be submitted yet. In particular, this means that the
3853 * callback will be called for some of the requests, for others it won't. The
3854 * caller must check the error field of the BlockRequest to wait for the right
3855 * callbacks (if error != 0, no callback will be called).
3856 *
3857 * The implementation may modify the contents of the reqs array, e.g. to merge
3858 * requests. However, the fields opaque and error are left unmodified as they
3859 * are used to signal failure for a single request to the caller.
3860 */
3861int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
3862{
Kevin Wolf40b4f532009-09-09 17:53:37 +02003863 MultiwriteCB *mcb;
3864 int i;
3865
Ryan Harper301db7c2011-03-07 10:01:04 -06003866 /* don't submit writes if we don't have a medium */
3867 if (bs->drv == NULL) {
3868 for (i = 0; i < num_reqs; i++) {
3869 reqs[i].error = -ENOMEDIUM;
3870 }
3871 return -1;
3872 }
3873
Kevin Wolf40b4f532009-09-09 17:53:37 +02003874 if (num_reqs == 0) {
3875 return 0;
3876 }
3877
3878 // Create MultiwriteCB structure
Anthony Liguori7267c092011-08-20 22:09:37 -05003879 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
Kevin Wolf40b4f532009-09-09 17:53:37 +02003880 mcb->num_requests = 0;
3881 mcb->num_callbacks = num_reqs;
3882
3883 for (i = 0; i < num_reqs; i++) {
3884 mcb->callbacks[i].cb = reqs[i].cb;
3885 mcb->callbacks[i].opaque = reqs[i].opaque;
3886 }
3887
3888 // Check for mergable requests
3889 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
3890
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01003891 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
3892
Paolo Bonzinidf9309f2011-11-14 17:50:50 +01003893 /* Run the aio requests. */
3894 mcb->num_requests = num_reqs;
Kevin Wolf40b4f532009-09-09 17:53:37 +02003895 for (i = 0; i < num_reqs; i++) {
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01003896 bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov,
3897 reqs[i].nb_sectors, reqs[i].flags,
3898 multiwrite_cb, mcb,
3899 true);
Kevin Wolf40b4f532009-09-09 17:53:37 +02003900 }
3901
3902 return 0;
Kevin Wolf40b4f532009-09-09 17:53:37 +02003903}
3904
bellard83f64092006-08-01 16:21:11 +00003905void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00003906{
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01003907 acb->aiocb_info->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00003908}
3909
3910/**************************************************************/
3911/* async block device emulation */
3912
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02003913typedef struct BlockDriverAIOCBSync {
3914 BlockDriverAIOCB common;
3915 QEMUBH *bh;
3916 int ret;
3917 /* vector translation state */
3918 QEMUIOVector *qiov;
3919 uint8_t *bounce;
3920 int is_write;
3921} BlockDriverAIOCBSync;
3922
3923static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
3924{
Kevin Wolfb666d232010-05-05 11:44:39 +02003925 BlockDriverAIOCBSync *acb =
3926 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03003927 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03003928 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02003929 qemu_aio_release(acb);
3930}
3931
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01003932static const AIOCBInfo bdrv_em_aiocb_info = {
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02003933 .aiocb_size = sizeof(BlockDriverAIOCBSync),
3934 .cancel = bdrv_aio_cancel_em,
3935};
3936
bellard83f64092006-08-01 16:21:11 +00003937static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00003938{
pbrookce1a14d2006-08-07 02:38:06 +00003939 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00003940
aliguorif141eaf2009-04-07 18:43:24 +00003941 if (!acb->is_write)
Michael Tokarev03396142012-06-07 20:17:55 +04003942 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00003943 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00003944 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03003945 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03003946 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00003947 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00003948}
bellardbeac80c2006-06-26 20:08:57 +00003949
aliguorif141eaf2009-04-07 18:43:24 +00003950static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
3951 int64_t sector_num,
3952 QEMUIOVector *qiov,
3953 int nb_sectors,
3954 BlockDriverCompletionFunc *cb,
3955 void *opaque,
3956 int is_write)
3957
bellardea2384d2004-08-01 21:59:26 +00003958{
pbrookce1a14d2006-08-07 02:38:06 +00003959 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00003960
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01003961 acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00003962 acb->is_write = is_write;
3963 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00003964 acb->bounce = qemu_blockalign(bs, qiov->size);
Paolo Bonzini3f3aace2011-11-14 17:50:54 +01003965 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00003966
3967 if (is_write) {
Michael Tokarevd5e6b162012-06-07 20:21:06 +04003968 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01003969 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00003970 } else {
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01003971 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00003972 }
3973
pbrookce1a14d2006-08-07 02:38:06 +00003974 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00003975
pbrookce1a14d2006-08-07 02:38:06 +00003976 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00003977}
3978
aliguorif141eaf2009-04-07 18:43:24 +00003979static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
3980 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00003981 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00003982{
aliguorif141eaf2009-04-07 18:43:24 +00003983 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00003984}
3985
aliguorif141eaf2009-04-07 18:43:24 +00003986static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
3987 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
3988 BlockDriverCompletionFunc *cb, void *opaque)
3989{
3990 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
3991}
3992
Kevin Wolf68485422011-06-30 10:05:46 +02003993
3994typedef struct BlockDriverAIOCBCoroutine {
3995 BlockDriverAIOCB common;
3996 BlockRequest req;
3997 bool is_write;
Kevin Wolfd318aea2012-11-13 16:35:08 +01003998 bool *done;
Kevin Wolf68485422011-06-30 10:05:46 +02003999 QEMUBH* bh;
4000} BlockDriverAIOCBCoroutine;
4001
4002static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
4003{
Kevin Wolfd318aea2012-11-13 16:35:08 +01004004 BlockDriverAIOCBCoroutine *acb =
4005 container_of(blockacb, BlockDriverAIOCBCoroutine, common);
4006 bool done = false;
4007
4008 acb->done = &done;
4009 while (!done) {
4010 qemu_aio_wait();
4011 }
Kevin Wolf68485422011-06-30 10:05:46 +02004012}
4013
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004014static const AIOCBInfo bdrv_em_co_aiocb_info = {
Kevin Wolf68485422011-06-30 10:05:46 +02004015 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
4016 .cancel = bdrv_aio_co_cancel_em,
4017};
4018
Paolo Bonzini35246a62011-10-14 10:41:29 +02004019static void bdrv_co_em_bh(void *opaque)
Kevin Wolf68485422011-06-30 10:05:46 +02004020{
4021 BlockDriverAIOCBCoroutine *acb = opaque;
4022
4023 acb->common.cb(acb->common.opaque, acb->req.error);
Kevin Wolfd318aea2012-11-13 16:35:08 +01004024
4025 if (acb->done) {
4026 *acb->done = true;
4027 }
4028
Kevin Wolf68485422011-06-30 10:05:46 +02004029 qemu_bh_delete(acb->bh);
4030 qemu_aio_release(acb);
4031}
4032
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01004033/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
4034static void coroutine_fn bdrv_co_do_rw(void *opaque)
4035{
4036 BlockDriverAIOCBCoroutine *acb = opaque;
4037 BlockDriverState *bs = acb->common.bs;
4038
4039 if (!acb->is_write) {
4040 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01004041 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01004042 } else {
4043 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01004044 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01004045 }
4046
Paolo Bonzini35246a62011-10-14 10:41:29 +02004047 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01004048 qemu_bh_schedule(acb->bh);
4049}
4050
Kevin Wolf68485422011-06-30 10:05:46 +02004051static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
4052 int64_t sector_num,
4053 QEMUIOVector *qiov,
4054 int nb_sectors,
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01004055 BdrvRequestFlags flags,
Kevin Wolf68485422011-06-30 10:05:46 +02004056 BlockDriverCompletionFunc *cb,
4057 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01004058 bool is_write)
Kevin Wolf68485422011-06-30 10:05:46 +02004059{
4060 Coroutine *co;
4061 BlockDriverAIOCBCoroutine *acb;
4062
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004063 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
Kevin Wolf68485422011-06-30 10:05:46 +02004064 acb->req.sector = sector_num;
4065 acb->req.nb_sectors = nb_sectors;
4066 acb->req.qiov = qiov;
Paolo Bonzinid20d9b72013-11-22 13:39:44 +01004067 acb->req.flags = flags;
Kevin Wolf68485422011-06-30 10:05:46 +02004068 acb->is_write = is_write;
Kevin Wolfd318aea2012-11-13 16:35:08 +01004069 acb->done = NULL;
Kevin Wolf68485422011-06-30 10:05:46 +02004070
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01004071 co = qemu_coroutine_create(bdrv_co_do_rw);
Kevin Wolf68485422011-06-30 10:05:46 +02004072 qemu_coroutine_enter(co, acb);
4073
4074 return &acb->common;
4075}
4076
Paolo Bonzini07f07612011-10-17 12:32:12 +02004077static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02004078{
Paolo Bonzini07f07612011-10-17 12:32:12 +02004079 BlockDriverAIOCBCoroutine *acb = opaque;
4080 BlockDriverState *bs = acb->common.bs;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02004081
Paolo Bonzini07f07612011-10-17 12:32:12 +02004082 acb->req.error = bdrv_co_flush(bs);
4083 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02004084 qemu_bh_schedule(acb->bh);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02004085}
4086
Paolo Bonzini07f07612011-10-17 12:32:12 +02004087BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
Alexander Graf016f5cf2010-05-26 17:51:49 +02004088 BlockDriverCompletionFunc *cb, void *opaque)
4089{
Paolo Bonzini07f07612011-10-17 12:32:12 +02004090 trace_bdrv_aio_flush(bs, opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +02004091
Paolo Bonzini07f07612011-10-17 12:32:12 +02004092 Coroutine *co;
4093 BlockDriverAIOCBCoroutine *acb;
Alexander Graf016f5cf2010-05-26 17:51:49 +02004094
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004095 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
Kevin Wolfd318aea2012-11-13 16:35:08 +01004096 acb->done = NULL;
4097
Paolo Bonzini07f07612011-10-17 12:32:12 +02004098 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
4099 qemu_coroutine_enter(co, acb);
Alexander Graf016f5cf2010-05-26 17:51:49 +02004100
Alexander Graf016f5cf2010-05-26 17:51:49 +02004101 return &acb->common;
4102}
4103
Paolo Bonzini4265d622011-10-17 12:32:14 +02004104static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
4105{
4106 BlockDriverAIOCBCoroutine *acb = opaque;
4107 BlockDriverState *bs = acb->common.bs;
4108
4109 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
4110 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
4111 qemu_bh_schedule(acb->bh);
4112}
4113
4114BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
4115 int64_t sector_num, int nb_sectors,
4116 BlockDriverCompletionFunc *cb, void *opaque)
4117{
4118 Coroutine *co;
4119 BlockDriverAIOCBCoroutine *acb;
4120
4121 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
4122
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004123 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
Paolo Bonzini4265d622011-10-17 12:32:14 +02004124 acb->req.sector = sector_num;
4125 acb->req.nb_sectors = nb_sectors;
Kevin Wolfd318aea2012-11-13 16:35:08 +01004126 acb->done = NULL;
Paolo Bonzini4265d622011-10-17 12:32:14 +02004127 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
4128 qemu_coroutine_enter(co, acb);
4129
4130 return &acb->common;
4131}
4132
bellardea2384d2004-08-01 21:59:26 +00004133void bdrv_init(void)
4134{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05004135 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00004136}
pbrookce1a14d2006-08-07 02:38:06 +00004137
Markus Armbrustereb852012009-10-27 18:41:44 +01004138void bdrv_init_with_whitelist(void)
4139{
4140 use_bdrv_whitelist = 1;
4141 bdrv_init();
4142}
4143
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004144void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02004145 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00004146{
pbrookce1a14d2006-08-07 02:38:06 +00004147 BlockDriverAIOCB *acb;
4148
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004149 acb = g_slice_alloc(aiocb_info->aiocb_size);
4150 acb->aiocb_info = aiocb_info;
pbrookce1a14d2006-08-07 02:38:06 +00004151 acb->bs = bs;
4152 acb->cb = cb;
4153 acb->opaque = opaque;
4154 return acb;
4155}
4156
4157void qemu_aio_release(void *p)
4158{
Stefan Hajnoczid37c9752012-10-31 16:34:36 +01004159 BlockDriverAIOCB *acb = p;
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01004160 g_slice_free1(acb->aiocb_info->aiocb_size, acb);
pbrookce1a14d2006-08-07 02:38:06 +00004161}
bellard19cb3732006-08-19 11:45:59 +00004162
4163/**************************************************************/
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004164/* Coroutine block device emulation */
4165
4166typedef struct CoroutineIOCompletion {
4167 Coroutine *coroutine;
4168 int ret;
4169} CoroutineIOCompletion;
4170
4171static void bdrv_co_io_em_complete(void *opaque, int ret)
4172{
4173 CoroutineIOCompletion *co = opaque;
4174
4175 co->ret = ret;
4176 qemu_coroutine_enter(co->coroutine, NULL);
4177}
4178
4179static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
4180 int nb_sectors, QEMUIOVector *iov,
4181 bool is_write)
4182{
4183 CoroutineIOCompletion co = {
4184 .coroutine = qemu_coroutine_self(),
4185 };
4186 BlockDriverAIOCB *acb;
4187
4188 if (is_write) {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01004189 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
4190 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004191 } else {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01004192 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
4193 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004194 }
4195
Stefan Hajnoczi59370aa2011-09-30 17:34:58 +01004196 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004197 if (!acb) {
4198 return -EIO;
4199 }
4200 qemu_coroutine_yield();
4201
4202 return co.ret;
4203}
4204
4205static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
4206 int64_t sector_num, int nb_sectors,
4207 QEMUIOVector *iov)
4208{
4209 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
4210}
4211
4212static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
4213 int64_t sector_num, int nb_sectors,
4214 QEMUIOVector *iov)
4215{
4216 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
4217}
4218
Paolo Bonzini07f07612011-10-17 12:32:12 +02004219static void coroutine_fn bdrv_flush_co_entry(void *opaque)
Kevin Wolfe7a8a782011-07-15 16:05:00 +02004220{
Paolo Bonzini07f07612011-10-17 12:32:12 +02004221 RwCo *rwco = opaque;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02004222
Paolo Bonzini07f07612011-10-17 12:32:12 +02004223 rwco->ret = bdrv_co_flush(rwco->bs);
4224}
4225
4226int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
4227{
Kevin Wolfeb489bb2011-11-10 18:10:11 +01004228 int ret;
4229
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004230 if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Paolo Bonzini07f07612011-10-17 12:32:12 +02004231 return 0;
Kevin Wolfeb489bb2011-11-10 18:10:11 +01004232 }
4233
Kevin Wolfca716362011-11-10 18:13:59 +01004234 /* Write back cached data to the OS even with cache=unsafe */
Kevin Wolfbf736fe2013-06-05 15:17:55 +02004235 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
Kevin Wolfeb489bb2011-11-10 18:10:11 +01004236 if (bs->drv->bdrv_co_flush_to_os) {
4237 ret = bs->drv->bdrv_co_flush_to_os(bs);
4238 if (ret < 0) {
4239 return ret;
4240 }
4241 }
4242
Kevin Wolfca716362011-11-10 18:13:59 +01004243 /* But don't actually force it to the disk with cache=unsafe */
4244 if (bs->open_flags & BDRV_O_NO_FLUSH) {
Kevin Wolfd4c82322012-08-15 12:52:45 +02004245 goto flush_parent;
Kevin Wolfca716362011-11-10 18:13:59 +01004246 }
4247
Kevin Wolfbf736fe2013-06-05 15:17:55 +02004248 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
Kevin Wolfeb489bb2011-11-10 18:10:11 +01004249 if (bs->drv->bdrv_co_flush_to_disk) {
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004250 ret = bs->drv->bdrv_co_flush_to_disk(bs);
Paolo Bonzini07f07612011-10-17 12:32:12 +02004251 } else if (bs->drv->bdrv_aio_flush) {
4252 BlockDriverAIOCB *acb;
4253 CoroutineIOCompletion co = {
4254 .coroutine = qemu_coroutine_self(),
4255 };
4256
4257 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
4258 if (acb == NULL) {
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004259 ret = -EIO;
Paolo Bonzini07f07612011-10-17 12:32:12 +02004260 } else {
4261 qemu_coroutine_yield();
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004262 ret = co.ret;
Paolo Bonzini07f07612011-10-17 12:32:12 +02004263 }
Paolo Bonzini07f07612011-10-17 12:32:12 +02004264 } else {
4265 /*
4266 * Some block drivers always operate in either writethrough or unsafe
4267 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
4268 * know how the server works (because the behaviour is hardcoded or
4269 * depends on server-side configuration), so we can't ensure that
4270 * everything is safe on disk. Returning an error doesn't work because
4271 * that would break guests even if the server operates in writethrough
4272 * mode.
4273 *
4274 * Let's hope the user knows what he's doing.
4275 */
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004276 ret = 0;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02004277 }
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004278 if (ret < 0) {
4279 return ret;
4280 }
4281
4282 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
4283 * in the case of cache=unsafe, so there are no useless flushes.
4284 */
Kevin Wolfd4c82322012-08-15 12:52:45 +02004285flush_parent:
Paolo Bonzini29cdb252012-03-12 18:26:01 +01004286 return bdrv_co_flush(bs->file);
Paolo Bonzini07f07612011-10-17 12:32:12 +02004287}
4288
Anthony Liguori0f154232011-11-14 15:09:45 -06004289void bdrv_invalidate_cache(BlockDriverState *bs)
4290{
4291 if (bs->drv && bs->drv->bdrv_invalidate_cache) {
4292 bs->drv->bdrv_invalidate_cache(bs);
4293 }
4294}
4295
4296void bdrv_invalidate_cache_all(void)
4297{
4298 BlockDriverState *bs;
4299
4300 QTAILQ_FOREACH(bs, &bdrv_states, list) {
4301 bdrv_invalidate_cache(bs);
4302 }
4303}
4304
BenoƮt Canet07789262012-03-23 08:36:49 +01004305void bdrv_clear_incoming_migration_all(void)
4306{
4307 BlockDriverState *bs;
4308
4309 QTAILQ_FOREACH(bs, &bdrv_states, list) {
4310 bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING);
4311 }
4312}
4313
Paolo Bonzini07f07612011-10-17 12:32:12 +02004314int bdrv_flush(BlockDriverState *bs)
4315{
4316 Coroutine *co;
4317 RwCo rwco = {
4318 .bs = bs,
4319 .ret = NOT_DONE,
4320 };
4321
4322 if (qemu_in_coroutine()) {
4323 /* Fast-path if already in coroutine context */
4324 bdrv_flush_co_entry(&rwco);
4325 } else {
4326 co = qemu_coroutine_create(bdrv_flush_co_entry);
4327 qemu_coroutine_enter(co, &rwco);
4328 while (rwco.ret == NOT_DONE) {
4329 qemu_aio_wait();
4330 }
4331 }
4332
4333 return rwco.ret;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02004334}
4335
Paolo Bonzini4265d622011-10-17 12:32:14 +02004336static void coroutine_fn bdrv_discard_co_entry(void *opaque)
4337{
4338 RwCo *rwco = opaque;
4339
4340 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
4341}
4342
Peter Lieven6f14da52013-10-24 12:06:59 +02004343/* if no limit is specified in the BlockLimits use a default
4344 * of 32768 512-byte sectors (16 MiB) per request.
4345 */
4346#define MAX_DISCARD_DEFAULT 32768
4347
Paolo Bonzini4265d622011-10-17 12:32:14 +02004348int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
4349 int nb_sectors)
4350{
Paolo Bonzinid51e9fe2013-11-22 13:39:43 +01004351 int max_discard;
4352
Paolo Bonzini4265d622011-10-17 12:32:14 +02004353 if (!bs->drv) {
4354 return -ENOMEDIUM;
4355 } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
4356 return -EIO;
4357 } else if (bs->read_only) {
4358 return -EROFS;
Paolo Bonzinidf702c92013-01-14 16:26:58 +01004359 }
4360
Fam Zhenge4654d22013-11-13 18:29:43 +08004361 bdrv_reset_dirty(bs, sector_num, nb_sectors);
Paolo Bonzinidf702c92013-01-14 16:26:58 +01004362
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01004363 /* Do nothing if disabled. */
4364 if (!(bs->open_flags & BDRV_O_UNMAP)) {
4365 return 0;
4366 }
4367
Paolo Bonzinid51e9fe2013-11-22 13:39:43 +01004368 if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) {
Paolo Bonzini4265d622011-10-17 12:32:14 +02004369 return 0;
4370 }
Paolo Bonzinid51e9fe2013-11-22 13:39:43 +01004371
4372 max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT;
4373 while (nb_sectors > 0) {
4374 int ret;
4375 int num = nb_sectors;
4376
4377 /* align request */
4378 if (bs->bl.discard_alignment &&
4379 num >= bs->bl.discard_alignment &&
4380 sector_num % bs->bl.discard_alignment) {
4381 if (num > bs->bl.discard_alignment) {
4382 num = bs->bl.discard_alignment;
4383 }
4384 num -= sector_num % bs->bl.discard_alignment;
4385 }
4386
4387 /* limit request size */
4388 if (num > max_discard) {
4389 num = max_discard;
4390 }
4391
4392 if (bs->drv->bdrv_co_discard) {
4393 ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
4394 } else {
4395 BlockDriverAIOCB *acb;
4396 CoroutineIOCompletion co = {
4397 .coroutine = qemu_coroutine_self(),
4398 };
4399
4400 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
4401 bdrv_co_io_em_complete, &co);
4402 if (acb == NULL) {
4403 return -EIO;
4404 } else {
4405 qemu_coroutine_yield();
4406 ret = co.ret;
4407 }
4408 }
Paolo Bonzini7ce21012013-11-22 13:39:47 +01004409 if (ret && ret != -ENOTSUP) {
Paolo Bonzinid51e9fe2013-11-22 13:39:43 +01004410 return ret;
4411 }
4412
4413 sector_num += num;
4414 nb_sectors -= num;
4415 }
4416 return 0;
Paolo Bonzini4265d622011-10-17 12:32:14 +02004417}
4418
4419int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
4420{
4421 Coroutine *co;
4422 RwCo rwco = {
4423 .bs = bs,
4424 .sector_num = sector_num,
4425 .nb_sectors = nb_sectors,
4426 .ret = NOT_DONE,
4427 };
4428
4429 if (qemu_in_coroutine()) {
4430 /* Fast-path if already in coroutine context */
4431 bdrv_discard_co_entry(&rwco);
4432 } else {
4433 co = qemu_coroutine_create(bdrv_discard_co_entry);
4434 qemu_coroutine_enter(co, &rwco);
4435 while (rwco.ret == NOT_DONE) {
4436 qemu_aio_wait();
4437 }
4438 }
4439
4440 return rwco.ret;
4441}
4442
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004443/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00004444/* removable device support */
4445
4446/**
4447 * Return TRUE if the media is present
4448 */
4449int bdrv_is_inserted(BlockDriverState *bs)
4450{
4451 BlockDriver *drv = bs->drv;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02004452
bellard19cb3732006-08-19 11:45:59 +00004453 if (!drv)
4454 return 0;
4455 if (!drv->bdrv_is_inserted)
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02004456 return 1;
4457 return drv->bdrv_is_inserted(bs);
bellard19cb3732006-08-19 11:45:59 +00004458}
4459
4460/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02004461 * Return whether the media changed since the last call to this
4462 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00004463 */
4464int bdrv_media_changed(BlockDriverState *bs)
4465{
4466 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00004467
Markus Armbruster8e49ca42011-08-03 15:08:08 +02004468 if (drv && drv->bdrv_media_changed) {
4469 return drv->bdrv_media_changed(bs);
4470 }
4471 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00004472}
4473
4474/**
4475 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
4476 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02004477void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00004478{
4479 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00004480
Markus Armbruster822e1cd2011-07-20 18:23:42 +02004481 if (drv && drv->bdrv_eject) {
4482 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00004483 }
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02004484
4485 if (bs->device_name[0] != '\0') {
4486 bdrv_emit_qmp_eject_event(bs, eject_flag);
4487 }
bellard19cb3732006-08-19 11:45:59 +00004488}
4489
bellard19cb3732006-08-19 11:45:59 +00004490/**
4491 * Lock or unlock the media (if it is locked, the user won't be able
4492 * to eject it manually).
4493 */
Markus Armbruster025e8492011-09-06 18:58:47 +02004494void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00004495{
4496 BlockDriver *drv = bs->drv;
4497
Markus Armbruster025e8492011-09-06 18:58:47 +02004498 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01004499
Markus Armbruster025e8492011-09-06 18:58:47 +02004500 if (drv && drv->bdrv_lock_medium) {
4501 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00004502 }
4503}
ths985a03b2007-12-24 16:10:43 +00004504
4505/* needed for generic scsi interface */
4506
4507int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
4508{
4509 BlockDriver *drv = bs->drv;
4510
4511 if (drv && drv->bdrv_ioctl)
4512 return drv->bdrv_ioctl(bs, req, buf);
4513 return -ENOTSUP;
4514}
aliguori7d780662009-03-12 19:57:08 +00004515
aliguori221f7152009-03-28 17:28:41 +00004516BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
4517 unsigned long int req, void *buf,
4518 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00004519{
aliguori221f7152009-03-28 17:28:41 +00004520 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00004521
aliguori221f7152009-03-28 17:28:41 +00004522 if (drv && drv->bdrv_aio_ioctl)
4523 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
4524 return NULL;
aliguori7d780662009-03-12 19:57:08 +00004525}
aliguorie268ca52009-04-22 20:20:00 +00004526
Markus Armbruster7b6f9302011-09-06 18:58:56 +02004527void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
4528{
4529 bs->buffer_alignment = align;
4530}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004531
aliguorie268ca52009-04-22 20:20:00 +00004532void *qemu_blockalign(BlockDriverState *bs, size_t size)
4533{
4534 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
4535}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004536
Stefan Hajnoczic53b1c52013-01-11 16:41:27 +01004537/*
4538 * Check if all memory in this vector is sector aligned.
4539 */
4540bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
4541{
4542 int i;
4543
4544 for (i = 0; i < qiov->niov; i++) {
4545 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
4546 return false;
4547 }
4548 }
4549
4550 return true;
4551}
4552
Fam Zhenge4654d22013-11-13 18:29:43 +08004553BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004554{
4555 int64_t bitmap_size;
Fam Zhenge4654d22013-11-13 18:29:43 +08004556 BdrvDirtyBitmap *bitmap;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01004557
Paolo Bonzini50717e92013-01-21 17:09:45 +01004558 assert((granularity & (granularity - 1)) == 0);
4559
Fam Zhenge4654d22013-11-13 18:29:43 +08004560 granularity >>= BDRV_SECTOR_BITS;
4561 assert(granularity);
4562 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS);
4563 bitmap = g_malloc0(sizeof(BdrvDirtyBitmap));
4564 bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1);
4565 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
4566 return bitmap;
4567}
4568
4569void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
4570{
4571 BdrvDirtyBitmap *bm, *next;
4572 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
4573 if (bm == bitmap) {
4574 QLIST_REMOVE(bitmap, list);
4575 hbitmap_free(bitmap->bitmap);
4576 g_free(bitmap);
4577 return;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01004578 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004579 }
4580}
4581
Fam Zheng21b56832013-11-13 18:29:44 +08004582BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
4583{
4584 BdrvDirtyBitmap *bm;
4585 BlockDirtyInfoList *list = NULL;
4586 BlockDirtyInfoList **plist = &list;
4587
4588 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
4589 BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo));
4590 BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList));
4591 info->count = bdrv_get_dirty_count(bs, bm);
4592 info->granularity =
4593 ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap));
4594 entry->value = info;
4595 *plist = entry;
4596 plist = &entry->next;
4597 }
4598
4599 return list;
4600}
4601
Fam Zhenge4654d22013-11-13 18:29:43 +08004602int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004603{
Fam Zhenge4654d22013-11-13 18:29:43 +08004604 if (bitmap) {
4605 return hbitmap_get(bitmap->bitmap, sector);
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02004606 } else {
4607 return 0;
4608 }
4609}
4610
Fam Zhenge4654d22013-11-13 18:29:43 +08004611void bdrv_dirty_iter_init(BlockDriverState *bs,
4612 BdrvDirtyBitmap *bitmap, HBitmapIter *hbi)
Paolo Bonzini1755da12012-10-18 16:49:18 +02004613{
Fam Zhenge4654d22013-11-13 18:29:43 +08004614 hbitmap_iter_init(hbi, bitmap->bitmap, 0);
Paolo Bonzini1755da12012-10-18 16:49:18 +02004615}
4616
4617void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
4618 int nr_sectors)
4619{
Fam Zhenge4654d22013-11-13 18:29:43 +08004620 BdrvDirtyBitmap *bitmap;
4621 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
4622 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
Paolo Bonzini8f0720e2013-01-21 17:09:41 +01004623 }
Liran Schouraaa0eb72010-01-26 10:31:48 +02004624}
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004625
Fam Zhenge4654d22013-11-13 18:29:43 +08004626void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors)
4627{
4628 BdrvDirtyBitmap *bitmap;
4629 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
4630 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
4631 }
4632}
4633
4634int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
4635{
4636 return hbitmap_count(bitmap->bitmap);
4637}
4638
Fam Zheng9fcb0252013-08-23 09:14:46 +08004639/* Get a reference to bs */
4640void bdrv_ref(BlockDriverState *bs)
4641{
4642 bs->refcnt++;
4643}
4644
4645/* Release a previously grabbed reference to bs.
4646 * If after releasing, reference count is zero, the BlockDriverState is
4647 * deleted. */
4648void bdrv_unref(BlockDriverState *bs)
4649{
4650 assert(bs->refcnt > 0);
4651 if (--bs->refcnt == 0) {
4652 bdrv_delete(bs);
4653 }
4654}
4655
Marcelo Tosattidb593f22011-01-26 12:12:34 -02004656void bdrv_set_in_use(BlockDriverState *bs, int in_use)
4657{
4658 assert(bs->in_use != in_use);
4659 bs->in_use = in_use;
4660}
4661
4662int bdrv_in_use(BlockDriverState *bs)
4663{
4664 return bs->in_use;
4665}
4666
Luiz Capitulino28a72822011-09-26 17:43:50 -03004667void bdrv_iostatus_enable(BlockDriverState *bs)
4668{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03004669 bs->iostatus_enabled = true;
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03004670 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Luiz Capitulino28a72822011-09-26 17:43:50 -03004671}
4672
4673/* The I/O status is only enabled if the drive explicitly
4674 * enables it _and_ the VM is configured to stop on errors */
4675bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
4676{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03004677 return (bs->iostatus_enabled &&
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02004678 (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
4679 bs->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
4680 bs->on_read_error == BLOCKDEV_ON_ERROR_STOP));
Luiz Capitulino28a72822011-09-26 17:43:50 -03004681}
4682
4683void bdrv_iostatus_disable(BlockDriverState *bs)
4684{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03004685 bs->iostatus_enabled = false;
Luiz Capitulino28a72822011-09-26 17:43:50 -03004686}
4687
4688void bdrv_iostatus_reset(BlockDriverState *bs)
4689{
4690 if (bdrv_iostatus_is_enabled(bs)) {
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03004691 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Paolo Bonzini3bd293c2012-10-18 16:49:27 +02004692 if (bs->job) {
4693 block_job_iostatus_reset(bs->job);
4694 }
Luiz Capitulino28a72822011-09-26 17:43:50 -03004695 }
4696}
4697
Luiz Capitulino28a72822011-09-26 17:43:50 -03004698void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
4699{
Paolo Bonzini3e1caa52012-09-28 17:22:57 +02004700 assert(bdrv_iostatus_is_enabled(bs));
4701 if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03004702 bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
4703 BLOCK_DEVICE_IO_STATUS_FAILED;
Luiz Capitulino28a72822011-09-26 17:43:50 -03004704 }
4705}
4706
Christoph Hellwiga597e792011-08-25 08:26:01 +02004707void
4708bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
4709 enum BlockAcctType type)
4710{
4711 assert(type < BDRV_MAX_IOTYPE);
4712
4713 cookie->bytes = bytes;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02004714 cookie->start_time_ns = get_clock();
Christoph Hellwiga597e792011-08-25 08:26:01 +02004715 cookie->type = type;
4716}
4717
4718void
4719bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
4720{
4721 assert(cookie->type < BDRV_MAX_IOTYPE);
4722
4723 bs->nr_bytes[cookie->type] += cookie->bytes;
4724 bs->nr_ops[cookie->type]++;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02004725 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
Christoph Hellwiga597e792011-08-25 08:26:01 +02004726}
4727
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004728void bdrv_img_create(const char *filename, const char *fmt,
4729 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01004730 char *options, uint64_t img_size, int flags,
4731 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004732{
4733 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfd2208942011-06-01 14:03:31 +02004734 QEMUOptionParameter *backing_fmt, *backing_file, *size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004735 BlockDriver *drv, *proto_drv;
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00004736 BlockDriver *backing_drv = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02004737 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004738 int ret = 0;
4739
4740 /* Find driver and parse its options */
4741 drv = bdrv_find_format(fmt);
4742 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004743 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004744 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004745 }
4746
Kevin Wolf98289622013-07-10 15:47:39 +02004747 proto_drv = bdrv_find_protocol(filename, true);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004748 if (!proto_drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004749 error_setg(errp, "Unknown protocol '%s'", filename);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004750 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004751 }
4752
4753 create_options = append_option_parameters(create_options,
4754 drv->create_options);
4755 create_options = append_option_parameters(create_options,
4756 proto_drv->create_options);
4757
4758 /* Create parameter list with default values */
4759 param = parse_option_parameters("", create_options, param);
4760
4761 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
4762
4763 /* Parse -o options */
4764 if (options) {
4765 param = parse_option_parameters(options, create_options, param);
4766 if (param == NULL) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004767 error_setg(errp, "Invalid options for file format '%s'.", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004768 goto out;
4769 }
4770 }
4771
4772 if (base_filename) {
4773 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
4774 base_filename)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004775 error_setg(errp, "Backing file not supported for file format '%s'",
4776 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004777 goto out;
4778 }
4779 }
4780
4781 if (base_fmt) {
4782 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004783 error_setg(errp, "Backing file format not supported for file "
4784 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004785 goto out;
4786 }
4787 }
4788
Jes Sorensen792da932010-12-16 13:52:17 +01004789 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
4790 if (backing_file && backing_file->value.s) {
4791 if (!strcmp(filename, backing_file->value.s)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004792 error_setg(errp, "Error: Trying to create an image with the "
4793 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01004794 goto out;
4795 }
4796 }
4797
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004798 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
4799 if (backing_fmt && backing_fmt->value.s) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00004800 backing_drv = bdrv_find_format(backing_fmt->value.s);
4801 if (!backing_drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004802 error_setg(errp, "Unknown backing file format '%s'",
4803 backing_fmt->value.s);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004804 goto out;
4805 }
4806 }
4807
4808 // The size for the image must always be specified, with one exception:
4809 // If we are using a backing file, we can obtain the size from there
Kevin Wolfd2208942011-06-01 14:03:31 +02004810 size = get_option_parameter(param, BLOCK_OPT_SIZE);
4811 if (size && size->value.n == -1) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004812 if (backing_file && backing_file->value.s) {
Max Reitz66f6b812013-12-03 14:57:52 +01004813 BlockDriverState *bs;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004814 uint64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004815 char buf[32];
Paolo Bonzini63090da2012-04-12 14:01:03 +02004816 int back_flags;
4817
4818 /* backing files always opened read-only */
4819 back_flags =
4820 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004821
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004822 bs = bdrv_new("");
4823
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01004824 ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
Max Reitzcc84d902013-09-06 17:14:26 +02004825 backing_drv, &local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004826 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +02004827 error_setg_errno(errp, -ret, "Could not open '%s': %s",
4828 backing_file->value.s,
4829 error_get_pretty(local_err));
4830 error_free(local_err);
4831 local_err = NULL;
Max Reitz66f6b812013-12-03 14:57:52 +01004832 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004833 goto out;
4834 }
4835 bdrv_get_geometry(bs, &size);
4836 size *= 512;
4837
4838 snprintf(buf, sizeof(buf), "%" PRId64, size);
4839 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
Max Reitz66f6b812013-12-03 14:57:52 +01004840
4841 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004842 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004843 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004844 goto out;
4845 }
4846 }
4847
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01004848 if (!quiet) {
4849 printf("Formatting '%s', fmt=%s ", filename, fmt);
4850 print_option_parameters(param);
4851 puts("");
4852 }
Max Reitzcc84d902013-09-06 17:14:26 +02004853 ret = bdrv_create(drv, filename, param, &local_err);
4854 if (ret == -EFBIG) {
4855 /* This is generally a better message than whatever the driver would
4856 * deliver (especially because of the cluster_size_hint), since that
4857 * is most probably not much different from "image too large". */
4858 const char *cluster_size_hint = "";
4859 if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
4860 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004861 }
Max Reitzcc84d902013-09-06 17:14:26 +02004862 error_setg(errp, "The image size is too large for file format '%s'"
4863 "%s", fmt, cluster_size_hint);
4864 error_free(local_err);
4865 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004866 }
4867
4868out:
4869 free_option_parameters(create_options);
4870 free_option_parameters(param);
4871
Max Reitzcc84d902013-09-06 17:14:26 +02004872 if (error_is_set(&local_err)) {
4873 error_propagate(errp, local_err);
4874 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004875}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01004876
4877AioContext *bdrv_get_aio_context(BlockDriverState *bs)
4878{
4879 /* Currently BlockDriverState always uses the main loop AioContext */
4880 return qemu_get_aio_context();
4881}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02004882
4883void bdrv_add_before_write_notifier(BlockDriverState *bs,
4884 NotifierWithReturn *notifier)
4885{
4886 notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
4887}
Max Reitz6f176b42013-09-03 10:09:50 +02004888
4889int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options)
4890{
4891 if (bs->drv->bdrv_amend_options == NULL) {
4892 return -ENOTSUP;
4893 }
4894 return bs->drv->bdrv_amend_options(bs, options);
4895}
BenoƮt Canetf6186f42013-10-02 14:33:48 +02004896
4897ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs)
4898{
4899 if (bs->drv->bdrv_check_ext_snapshot) {
4900 return bs->drv->bdrv_check_ext_snapshot(bs);
4901 }
4902
4903 if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) {
4904 return bs->file->drv->bdrv_check_ext_snapshot(bs);
4905 }
4906
4907 /* external snapshots are allowed by default */
4908 return EXT_SNAPSHOT_ALLOWED;
4909}
4910
4911ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs)
4912{
4913 return EXT_SNAPSHOT_FORBIDDEN;
4914}