bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator block driver |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6 | * 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 | */ |
blueswir1 | 3990d09 | 2008-12-05 17:53:21 +0000 | [diff] [blame] | 24 | #include "config-host.h" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 25 | #include "qemu-common.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 26 | #include "monitor.h" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 27 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 28 | #include "module.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 29 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 30 | #ifdef CONFIG_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/ioctl.h> |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 34 | #include <sys/queue.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 35 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 36 | #include <sys/disk.h> |
| 37 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 38 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 39 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 40 | #ifdef _WIN32 |
| 41 | #include <windows.h> |
| 42 | #endif |
| 43 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 44 | #define SECTOR_BITS 9 |
| 45 | #define SECTOR_SIZE (1 << SECTOR_BITS) |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 46 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 47 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 48 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 49 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 50 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 51 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 52 | BlockDriverCompletionFunc *cb, void *opaque); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 53 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 54 | BlockDriverCompletionFunc *cb, void *opaque); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 55 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 56 | uint8_t *buf, int nb_sectors); |
| 57 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 58 | const uint8_t *buf, int nb_sectors); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 59 | |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 60 | BlockDriverState *bdrv_first; |
| 61 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 62 | static BlockDriver *first_drv; |
| 63 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 64 | int path_is_absolute(const char *path) |
| 65 | { |
| 66 | const char *p; |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 67 | #ifdef _WIN32 |
| 68 | /* specific case for names like: "\\.\d:" */ |
| 69 | if (*path == '/' || *path == '\\') |
| 70 | return 1; |
| 71 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 72 | p = strchr(path, ':'); |
| 73 | if (p) |
| 74 | p++; |
| 75 | else |
| 76 | p = path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 77 | #ifdef _WIN32 |
| 78 | return (*p == '/' || *p == '\\'); |
| 79 | #else |
| 80 | return (*p == '/'); |
| 81 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 85 | path to it by considering it is relative to base_path. URL are |
| 86 | supported. */ |
| 87 | void path_combine(char *dest, int dest_size, |
| 88 | const char *base_path, |
| 89 | const char *filename) |
| 90 | { |
| 91 | const char *p, *p1; |
| 92 | int len; |
| 93 | |
| 94 | if (dest_size <= 0) |
| 95 | return; |
| 96 | if (path_is_absolute(filename)) { |
| 97 | pstrcpy(dest, dest_size, filename); |
| 98 | } else { |
| 99 | p = strchr(base_path, ':'); |
| 100 | if (p) |
| 101 | p++; |
| 102 | else |
| 103 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 104 | p1 = strrchr(base_path, '/'); |
| 105 | #ifdef _WIN32 |
| 106 | { |
| 107 | const char *p2; |
| 108 | p2 = strrchr(base_path, '\\'); |
| 109 | if (!p1 || p2 > p1) |
| 110 | p1 = p2; |
| 111 | } |
| 112 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 113 | if (p1) |
| 114 | p1++; |
| 115 | else |
| 116 | p1 = base_path; |
| 117 | if (p1 > p) |
| 118 | p = p1; |
| 119 | len = p - base_path; |
| 120 | if (len > dest_size - 1) |
| 121 | len = dest_size - 1; |
| 122 | memcpy(dest, base_path, len); |
| 123 | dest[len] = '\0'; |
| 124 | pstrcat(dest, dest_size, filename); |
| 125 | } |
| 126 | } |
| 127 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 128 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 129 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 130 | if (!bdrv->bdrv_aio_readv) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 131 | /* add AIO emulation layer */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 132 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 133 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 134 | } else if (!bdrv->bdrv_read) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 135 | /* add synchronous IO emulation layer */ |
| 136 | bdrv->bdrv_read = bdrv_read_em; |
| 137 | bdrv->bdrv_write = bdrv_write_em; |
| 138 | } |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 139 | |
| 140 | if (!bdrv->bdrv_aio_flush) |
| 141 | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; |
| 142 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 143 | bdrv->next = first_drv; |
| 144 | first_drv = bdrv; |
| 145 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 146 | |
| 147 | /* create a new block device (by default it is empty) */ |
| 148 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 149 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 150 | BlockDriverState **pbs, *bs; |
| 151 | |
| 152 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 153 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 154 | if (device_name[0] != '\0') { |
| 155 | /* insert at the end */ |
| 156 | pbs = &bdrv_first; |
| 157 | while (*pbs != NULL) |
| 158 | pbs = &(*pbs)->next; |
| 159 | *pbs = bs; |
| 160 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 161 | return bs; |
| 162 | } |
| 163 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 164 | BlockDriver *bdrv_find_format(const char *format_name) |
| 165 | { |
| 166 | BlockDriver *drv1; |
| 167 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
| 168 | if (!strcmp(drv1->format_name, format_name)) |
| 169 | return drv1; |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 174 | int bdrv_create(BlockDriver *drv, const char* filename, |
| 175 | QEMUOptionParameter *options) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 176 | { |
| 177 | if (!drv->bdrv_create) |
| 178 | return -ENOTSUP; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 179 | |
| 180 | return drv->bdrv_create(filename, options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 181 | } |
| 182 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 183 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 184 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 185 | { |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 186 | char temp_dir[MAX_PATH]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 187 | |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 188 | GetTempPath(MAX_PATH, temp_dir); |
| 189 | GetTempFileName(temp_dir, "qem", 0, filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 190 | } |
| 191 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 192 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 193 | { |
| 194 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 195 | const char *tmpdir; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 196 | /* XXX: race condition possible */ |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 197 | tmpdir = getenv("TMPDIR"); |
| 198 | if (!tmpdir) |
| 199 | tmpdir = "/tmp"; |
| 200 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 201 | fd = mkstemp(filename); |
| 202 | close(fd); |
| 203 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 204 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 205 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 206 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 207 | static int is_windows_drive_prefix(const char *filename) |
| 208 | { |
| 209 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 210 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 211 | filename[1] == ':'); |
| 212 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 213 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 214 | int is_windows_drive(const char *filename) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 215 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 216 | if (is_windows_drive_prefix(filename) && |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 217 | filename[2] == '\0') |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 218 | return 1; |
| 219 | if (strstart(filename, "\\\\.\\", NULL) || |
| 220 | strstart(filename, "//./", NULL)) |
| 221 | return 1; |
| 222 | return 0; |
| 223 | } |
| 224 | #endif |
| 225 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 226 | static BlockDriver *find_protocol(const char *filename) |
| 227 | { |
| 228 | BlockDriver *drv1; |
| 229 | char protocol[128]; |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 230 | int len; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 231 | const char *p; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 232 | |
| 233 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 234 | if (is_windows_drive(filename) || |
| 235 | is_windows_drive_prefix(filename)) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 236 | return bdrv_find_format("raw"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 237 | #endif |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 238 | p = strchr(filename, ':'); |
| 239 | if (!p) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 240 | return bdrv_find_format("raw"); |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 241 | len = p - filename; |
| 242 | if (len > sizeof(protocol) - 1) |
| 243 | len = sizeof(protocol) - 1; |
| 244 | memcpy(protocol, filename, len); |
| 245 | protocol[len] = '\0'; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 246 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 247 | if (drv1->protocol_name && |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 248 | !strcmp(drv1->protocol_name, protocol)) |
| 249 | return drv1; |
| 250 | } |
| 251 | return NULL; |
| 252 | } |
| 253 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 254 | /* |
| 255 | * Detect host devices. By convention, /dev/cdrom[N] is always |
| 256 | * recognized as a host CDROM. |
| 257 | */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 258 | static BlockDriver *find_hdev_driver(const char *filename) |
| 259 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 260 | int score_max = 0, score; |
| 261 | BlockDriver *drv = NULL, *d; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 262 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 263 | for (d = first_drv; d; d = d->next) { |
| 264 | if (d->bdrv_probe_device) { |
| 265 | score = d->bdrv_probe_device(filename); |
| 266 | if (score > score_max) { |
| 267 | score_max = score; |
| 268 | drv = d; |
| 269 | } |
| 270 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 273 | return drv; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 274 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 275 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 276 | static BlockDriver *find_image_format(const char *filename) |
| 277 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 278 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 279 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 280 | uint8_t buf[2048]; |
| 281 | BlockDriverState *bs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 282 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 283 | drv = find_protocol(filename); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 284 | /* no need to test disk image formats for vvfat */ |
Anthony Liguori | c833ab7 | 2009-05-22 08:17:55 -0500 | [diff] [blame] | 285 | if (drv && strcmp(drv->format_name, "vvfat") == 0) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 286 | return drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 287 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 288 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
| 289 | if (ret < 0) |
| 290 | return NULL; |
| 291 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 292 | bdrv_delete(bs); |
| 293 | if (ret < 0) { |
| 294 | return NULL; |
| 295 | } |
| 296 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 297 | score_max = 0; |
| 298 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 299 | if (drv1->bdrv_probe) { |
| 300 | score = drv1->bdrv_probe(buf, ret, filename); |
| 301 | if (score > score_max) { |
| 302 | score_max = score; |
| 303 | drv = drv1; |
| 304 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | return drv; |
| 308 | } |
| 309 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 310 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 311 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 312 | BlockDriverState *bs; |
| 313 | int ret; |
| 314 | |
| 315 | bs = bdrv_new(""); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 316 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); |
| 317 | if (ret < 0) { |
| 318 | bdrv_delete(bs); |
| 319 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 320 | } |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 321 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 322 | *pbs = bs; |
| 323 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 324 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 325 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 326 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
| 327 | { |
| 328 | return bdrv_open2(bs, filename, flags, NULL); |
| 329 | } |
| 330 | |
| 331 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 332 | BlockDriver *drv) |
| 333 | { |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 334 | int ret, open_flags, try_rw; |
ths | eb5c851 | 2007-02-11 15:06:09 +0000 | [diff] [blame] | 335 | char tmp_filename[PATH_MAX]; |
| 336 | char backing_filename[PATH_MAX]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 337 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 338 | bs->is_temporary = 0; |
| 339 | bs->encrypted = 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 340 | bs->valid_key = 0; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 341 | /* buffer_alignment defaulted to 512, drivers can change this value */ |
| 342 | bs->buffer_alignment = 512; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 343 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 344 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 345 | BlockDriverState *bs1; |
| 346 | int64_t total_size; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 347 | int is_protocol = 0; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 348 | BlockDriver *bdrv_qcow2; |
| 349 | QEMUOptionParameter *options; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 350 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 351 | /* if snapshot, we create a temporary backing file and open it |
| 352 | instead of opening 'filename' directly */ |
| 353 | |
| 354 | /* if there is a backing file, use it */ |
| 355 | bs1 = bdrv_new(""); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 356 | ret = bdrv_open2(bs1, filename, 0, drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 357 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 358 | bdrv_delete(bs1); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 359 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 360 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 361 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 362 | |
| 363 | if (bs1->drv && bs1->drv->protocol_name) |
| 364 | is_protocol = 1; |
| 365 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 366 | bdrv_delete(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 367 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 368 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 369 | |
| 370 | /* Real path is meaningless for protocols */ |
| 371 | if (is_protocol) |
| 372 | snprintf(backing_filename, sizeof(backing_filename), |
| 373 | "%s", filename); |
| 374 | else |
| 375 | realpath(filename, backing_filename); |
| 376 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 377 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
| 378 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
| 379 | |
| 380 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512); |
| 381 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
| 382 | if (drv) { |
| 383 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
| 384 | drv->format_name); |
| 385 | } |
| 386 | |
| 387 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 388 | if (ret < 0) { |
| 389 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 390 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 391 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 392 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 393 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 394 | bs->is_temporary = 1; |
| 395 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 396 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 397 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 398 | if (flags & BDRV_O_FILE) { |
| 399 | drv = find_protocol(filename); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 400 | } else if (!drv) { |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 401 | drv = find_hdev_driver(filename); |
| 402 | if (!drv) { |
| 403 | drv = find_image_format(filename); |
| 404 | } |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 405 | } |
| 406 | if (!drv) { |
| 407 | ret = -ENOENT; |
| 408 | goto unlink_and_fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 409 | } |
| 410 | bs->drv = drv; |
| 411 | bs->opaque = qemu_mallocz(drv->instance_size); |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a |
| 415 | * write cache to the guest. We do need the fdatasync to flush |
| 416 | * out transactions for block allocations, and we maybe have a |
| 417 | * volatile write cache in our backing device to deal with. |
| 418 | */ |
| 419 | if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) |
| 420 | bs->enable_write_cache = 1; |
| 421 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 422 | /* Note: for compatibility, we open disk image files as RDWR, and |
| 423 | RDONLY as fallback */ |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 424 | try_rw = !bs->read_only || bs->is_temporary; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 425 | if (!(flags & BDRV_O_FILE)) |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 426 | open_flags = (try_rw ? BDRV_O_RDWR : 0) | |
| 427 | (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 428 | else |
| 429 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 430 | ret = drv->bdrv_open(bs, filename, open_flags); |
aurel32 | a0a8353 | 2008-10-13 21:08:34 +0000 | [diff] [blame] | 431 | if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 432 | ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 433 | bs->read_only = 1; |
| 434 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 435 | if (ret < 0) { |
| 436 | qemu_free(bs->opaque); |
bellard | 6b21b97 | 2006-08-23 21:14:37 +0000 | [diff] [blame] | 437 | bs->opaque = NULL; |
| 438 | bs->drv = NULL; |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 439 | unlink_and_fail: |
| 440 | if (bs->is_temporary) |
| 441 | unlink(filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 442 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 443 | } |
bellard | d15a771 | 2006-08-06 13:35:09 +0000 | [diff] [blame] | 444 | if (drv->bdrv_getlength) { |
| 445 | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
| 446 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 447 | #ifndef _WIN32 |
| 448 | if (bs->is_temporary) { |
| 449 | unlink(filename); |
| 450 | } |
| 451 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 452 | if (bs->backing_file[0] != '\0') { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 453 | /* if there is a backing file, use it */ |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 454 | BlockDriver *back_drv = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 455 | bs->backing_hd = bdrv_new(""); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 456 | /* pass on read_only property to the backing_hd */ |
| 457 | bs->backing_hd->read_only = bs->read_only; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 458 | path_combine(backing_filename, sizeof(backing_filename), |
| 459 | filename, bs->backing_file); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 460 | if (bs->backing_format[0] != '\0') |
| 461 | back_drv = bdrv_find_format(bs->backing_format); |
| 462 | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, |
| 463 | back_drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 464 | if (ret < 0) { |
| 465 | bdrv_close(bs); |
| 466 | return ret; |
| 467 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 468 | } |
| 469 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 470 | if (!bdrv_key_required(bs)) { |
| 471 | /* call the change callback */ |
| 472 | bs->media_changed = 1; |
| 473 | if (bs->change_cb) |
| 474 | bs->change_cb(bs->change_opaque); |
| 475 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 476 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void bdrv_close(BlockDriverState *bs) |
| 480 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 481 | if (bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 482 | if (bs->backing_hd) |
| 483 | bdrv_delete(bs->backing_hd); |
| 484 | bs->drv->bdrv_close(bs); |
| 485 | qemu_free(bs->opaque); |
| 486 | #ifdef _WIN32 |
| 487 | if (bs->is_temporary) { |
| 488 | unlink(bs->filename); |
| 489 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 490 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 491 | bs->opaque = NULL; |
| 492 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 493 | |
| 494 | /* call the change callback */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 495 | bs->media_changed = 1; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 496 | if (bs->change_cb) |
| 497 | bs->change_cb(bs->change_opaque); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void bdrv_delete(BlockDriverState *bs) |
| 502 | { |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 503 | BlockDriverState **pbs; |
| 504 | |
| 505 | pbs = &bdrv_first; |
| 506 | while (*pbs != bs && *pbs != NULL) |
| 507 | pbs = &(*pbs)->next; |
| 508 | if (*pbs == bs) |
| 509 | *pbs = bs->next; |
| 510 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 511 | bdrv_close(bs); |
| 512 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 513 | } |
| 514 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 515 | /* |
| 516 | * Run consistency checks on an image |
| 517 | * |
| 518 | * Returns the number of errors or -errno when an internal error occurs |
| 519 | */ |
| 520 | int bdrv_check(BlockDriverState *bs) |
| 521 | { |
| 522 | if (bs->drv->bdrv_check == NULL) { |
| 523 | return -ENOTSUP; |
| 524 | } |
| 525 | |
| 526 | return bs->drv->bdrv_check(bs); |
| 527 | } |
| 528 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 529 | /* commit COW file into the raw image */ |
| 530 | int bdrv_commit(BlockDriverState *bs) |
| 531 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 532 | BlockDriver *drv = bs->drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 533 | int64_t i, total_sectors; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 534 | int n, j; |
| 535 | unsigned char sector[512]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 536 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 537 | if (!drv) |
| 538 | return -ENOMEDIUM; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 539 | |
| 540 | if (bs->read_only) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 541 | return -EACCES; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 542 | } |
| 543 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 544 | if (!bs->backing_hd) { |
| 545 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 546 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 547 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 548 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
| 549 | for (i = 0; i < total_sectors;) { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 550 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 551 | for(j = 0; j < n; j++) { |
| 552 | if (bdrv_read(bs, i, sector, 1) != 0) { |
| 553 | return -EIO; |
| 554 | } |
| 555 | |
| 556 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
| 557 | return -EIO; |
| 558 | } |
| 559 | i++; |
| 560 | } |
| 561 | } else { |
| 562 | i += n; |
| 563 | } |
| 564 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 565 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 566 | if (drv->bdrv_make_empty) |
| 567 | return drv->bdrv_make_empty(bs); |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 568 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 572 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 573 | size_t size) |
| 574 | { |
| 575 | int64_t len; |
| 576 | |
| 577 | if (!bdrv_is_inserted(bs)) |
| 578 | return -ENOMEDIUM; |
| 579 | |
| 580 | if (bs->growable) |
| 581 | return 0; |
| 582 | |
| 583 | len = bdrv_getlength(bs); |
| 584 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 585 | if (offset < 0) |
| 586 | return -EIO; |
| 587 | |
| 588 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 589 | return -EIO; |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 595 | int nb_sectors) |
| 596 | { |
aliguori | 999dec5 | 2009-03-29 01:31:48 +0000 | [diff] [blame] | 597 | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 598 | } |
| 599 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 600 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 601 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 602 | uint8_t *buf, int nb_sectors) |
| 603 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 604 | BlockDriver *drv = bs->drv; |
| 605 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 606 | if (!drv) |
| 607 | return -ENOMEDIUM; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 608 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 609 | return -EIO; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 610 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 611 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 612 | } |
| 613 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 614 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 615 | -EIO generic I/O error (may happen for all errors) |
| 616 | -ENOMEDIUM No media inserted. |
| 617 | -EINVAL Invalid sector number or nb_sectors |
| 618 | -EACCES Trying to write a read-only device |
| 619 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 620 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 621 | const uint8_t *buf, int nb_sectors) |
| 622 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 623 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 624 | if (!bs->drv) |
| 625 | return -ENOMEDIUM; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 626 | if (bs->read_only) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 627 | return -EACCES; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 628 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 629 | return -EIO; |
| 630 | |
aliguori | 42fb280 | 2009-01-15 20:43:39 +0000 | [diff] [blame] | 631 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 632 | } |
| 633 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 634 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 635 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 636 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 637 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 638 | int len, nb_sectors, count; |
| 639 | int64_t sector_num; |
| 640 | |
| 641 | count = count1; |
| 642 | /* first read to align to sector start */ |
| 643 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 644 | if (len > count) |
| 645 | len = count; |
| 646 | sector_num = offset >> SECTOR_BITS; |
| 647 | if (len > 0) { |
| 648 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 649 | return -EIO; |
| 650 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); |
| 651 | count -= len; |
| 652 | if (count == 0) |
| 653 | return count1; |
| 654 | sector_num++; |
| 655 | buf += len; |
| 656 | } |
| 657 | |
| 658 | /* read the sectors "in place" */ |
| 659 | nb_sectors = count >> SECTOR_BITS; |
| 660 | if (nb_sectors > 0) { |
| 661 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
| 662 | return -EIO; |
| 663 | sector_num += nb_sectors; |
| 664 | len = nb_sectors << SECTOR_BITS; |
| 665 | buf += len; |
| 666 | count -= len; |
| 667 | } |
| 668 | |
| 669 | /* add data from the last sector */ |
| 670 | if (count > 0) { |
| 671 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 672 | return -EIO; |
| 673 | memcpy(buf, tmp_buf, count); |
| 674 | } |
| 675 | return count1; |
| 676 | } |
| 677 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 678 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 679 | const void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 680 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 681 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 682 | int len, nb_sectors, count; |
| 683 | int64_t sector_num; |
| 684 | |
| 685 | count = count1; |
| 686 | /* first write to align to sector start */ |
| 687 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 688 | if (len > count) |
| 689 | len = count; |
| 690 | sector_num = offset >> SECTOR_BITS; |
| 691 | if (len > 0) { |
| 692 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 693 | return -EIO; |
| 694 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); |
| 695 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 696 | return -EIO; |
| 697 | count -= len; |
| 698 | if (count == 0) |
| 699 | return count1; |
| 700 | sector_num++; |
| 701 | buf += len; |
| 702 | } |
| 703 | |
| 704 | /* write the sectors "in place" */ |
| 705 | nb_sectors = count >> SECTOR_BITS; |
| 706 | if (nb_sectors > 0) { |
| 707 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
| 708 | return -EIO; |
| 709 | sector_num += nb_sectors; |
| 710 | len = nb_sectors << SECTOR_BITS; |
| 711 | buf += len; |
| 712 | count -= len; |
| 713 | } |
| 714 | |
| 715 | /* add data from the last sector */ |
| 716 | if (count > 0) { |
| 717 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 718 | return -EIO; |
| 719 | memcpy(tmp_buf, buf, count); |
| 720 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 721 | return -EIO; |
| 722 | } |
| 723 | return count1; |
| 724 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 725 | |
| 726 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 727 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 728 | */ |
| 729 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 730 | { |
| 731 | BlockDriver *drv = bs->drv; |
| 732 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 733 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 734 | if (!drv->bdrv_truncate) |
| 735 | return -ENOTSUP; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 736 | if (bs->read_only) |
| 737 | return -EACCES; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 738 | return drv->bdrv_truncate(bs, offset); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 743 | */ |
| 744 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 745 | { |
| 746 | BlockDriver *drv = bs->drv; |
| 747 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 748 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 749 | if (!drv->bdrv_getlength) { |
| 750 | /* legacy mode */ |
| 751 | return bs->total_sectors * SECTOR_SIZE; |
| 752 | } |
| 753 | return drv->bdrv_getlength(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 754 | } |
| 755 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 756 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 757 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 758 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 759 | int64_t length; |
| 760 | length = bdrv_getlength(bs); |
| 761 | if (length < 0) |
| 762 | length = 0; |
| 763 | else |
| 764 | length = length >> SECTOR_BITS; |
| 765 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 766 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 767 | |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 768 | struct partition { |
| 769 | uint8_t boot_ind; /* 0x80 - active */ |
| 770 | uint8_t head; /* starting head */ |
| 771 | uint8_t sector; /* starting sector */ |
| 772 | uint8_t cyl; /* starting cylinder */ |
| 773 | uint8_t sys_ind; /* What partition type */ |
| 774 | uint8_t end_head; /* end head */ |
| 775 | uint8_t end_sector; /* end sector */ |
| 776 | uint8_t end_cyl; /* end cylinder */ |
| 777 | uint32_t start_sect; /* starting sector counting from 0 */ |
| 778 | uint32_t nr_sects; /* nr of sectors in partition */ |
| 779 | } __attribute__((packed)); |
| 780 | |
| 781 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ |
| 782 | static int guess_disk_lchs(BlockDriverState *bs, |
| 783 | int *pcylinders, int *pheads, int *psectors) |
| 784 | { |
| 785 | uint8_t buf[512]; |
| 786 | int ret, i, heads, sectors, cylinders; |
| 787 | struct partition *p; |
| 788 | uint32_t nr_sects; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 789 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 790 | |
| 791 | bdrv_get_geometry(bs, &nb_sectors); |
| 792 | |
| 793 | ret = bdrv_read(bs, 0, buf, 1); |
| 794 | if (ret < 0) |
| 795 | return -1; |
| 796 | /* test msdos magic */ |
| 797 | if (buf[510] != 0x55 || buf[511] != 0xaa) |
| 798 | return -1; |
| 799 | for(i = 0; i < 4; i++) { |
| 800 | p = ((struct partition *)(buf + 0x1be)) + i; |
| 801 | nr_sects = le32_to_cpu(p->nr_sects); |
| 802 | if (nr_sects && p->end_head) { |
| 803 | /* We make the assumption that the partition terminates on |
| 804 | a cylinder boundary */ |
| 805 | heads = p->end_head + 1; |
| 806 | sectors = p->end_sector & 63; |
| 807 | if (sectors == 0) |
| 808 | continue; |
| 809 | cylinders = nb_sectors / (heads * sectors); |
| 810 | if (cylinders < 1 || cylinders > 16383) |
| 811 | continue; |
| 812 | *pheads = heads; |
| 813 | *psectors = sectors; |
| 814 | *pcylinders = cylinders; |
| 815 | #if 0 |
| 816 | printf("guessed geometry: LCHS=%d %d %d\n", |
| 817 | cylinders, heads, sectors); |
| 818 | #endif |
| 819 | return 0; |
| 820 | } |
| 821 | } |
| 822 | return -1; |
| 823 | } |
| 824 | |
| 825 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
| 826 | { |
| 827 | int translation, lba_detected = 0; |
| 828 | int cylinders, heads, secs; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 829 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 830 | |
| 831 | /* if a geometry hint is available, use it */ |
| 832 | bdrv_get_geometry(bs, &nb_sectors); |
| 833 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
| 834 | translation = bdrv_get_translation_hint(bs); |
| 835 | if (cylinders != 0) { |
| 836 | *pcyls = cylinders; |
| 837 | *pheads = heads; |
| 838 | *psecs = secs; |
| 839 | } else { |
| 840 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
| 841 | if (heads > 16) { |
| 842 | /* if heads > 16, it means that a BIOS LBA |
| 843 | translation was active, so the default |
| 844 | hardware geometry is OK */ |
| 845 | lba_detected = 1; |
| 846 | goto default_geometry; |
| 847 | } else { |
| 848 | *pcyls = cylinders; |
| 849 | *pheads = heads; |
| 850 | *psecs = secs; |
| 851 | /* disable any translation to be in sync with |
| 852 | the logical geometry */ |
| 853 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { |
| 854 | bdrv_set_translation_hint(bs, |
| 855 | BIOS_ATA_TRANSLATION_NONE); |
| 856 | } |
| 857 | } |
| 858 | } else { |
| 859 | default_geometry: |
| 860 | /* if no geometry, use a standard physical disk geometry */ |
| 861 | cylinders = nb_sectors / (16 * 63); |
| 862 | |
| 863 | if (cylinders > 16383) |
| 864 | cylinders = 16383; |
| 865 | else if (cylinders < 2) |
| 866 | cylinders = 2; |
| 867 | *pcyls = cylinders; |
| 868 | *pheads = 16; |
| 869 | *psecs = 63; |
| 870 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
| 871 | if ((*pcyls * *pheads) <= 131072) { |
| 872 | bdrv_set_translation_hint(bs, |
| 873 | BIOS_ATA_TRANSLATION_LARGE); |
| 874 | } else { |
| 875 | bdrv_set_translation_hint(bs, |
| 876 | BIOS_ATA_TRANSLATION_LBA); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
| 881 | } |
| 882 | } |
| 883 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 884 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 885 | int cyls, int heads, int secs) |
| 886 | { |
| 887 | bs->cyls = cyls; |
| 888 | bs->heads = heads; |
| 889 | bs->secs = secs; |
| 890 | } |
| 891 | |
| 892 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 893 | { |
| 894 | bs->type = type; |
| 895 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 896 | type == BDRV_TYPE_FLOPPY)); |
| 897 | } |
| 898 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 899 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 900 | { |
| 901 | bs->translation = translation; |
| 902 | } |
| 903 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 904 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 905 | int *pcyls, int *pheads, int *psecs) |
| 906 | { |
| 907 | *pcyls = bs->cyls; |
| 908 | *pheads = bs->heads; |
| 909 | *psecs = bs->secs; |
| 910 | } |
| 911 | |
| 912 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 913 | { |
| 914 | return bs->type; |
| 915 | } |
| 916 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 917 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 918 | { |
| 919 | return bs->translation; |
| 920 | } |
| 921 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 922 | int bdrv_is_removable(BlockDriverState *bs) |
| 923 | { |
| 924 | return bs->removable; |
| 925 | } |
| 926 | |
| 927 | int bdrv_is_read_only(BlockDriverState *bs) |
| 928 | { |
| 929 | return bs->read_only; |
| 930 | } |
| 931 | |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame^] | 932 | int bdrv_set_read_only(BlockDriverState *bs, int read_only) |
| 933 | { |
| 934 | int ret = bs->read_only; |
| 935 | bs->read_only = read_only; |
| 936 | return ret; |
| 937 | } |
| 938 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 939 | int bdrv_is_sg(BlockDriverState *bs) |
| 940 | { |
| 941 | return bs->sg; |
| 942 | } |
| 943 | |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 944 | int bdrv_enable_write_cache(BlockDriverState *bs) |
| 945 | { |
| 946 | return bs->enable_write_cache; |
| 947 | } |
| 948 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 949 | /* XXX: no longer used */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 950 | void bdrv_set_change_cb(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 951 | void (*change_cb)(void *opaque), void *opaque) |
| 952 | { |
| 953 | bs->change_cb = change_cb; |
| 954 | bs->change_opaque = opaque; |
| 955 | } |
| 956 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 957 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 958 | { |
| 959 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 960 | return 1; |
| 961 | return bs->encrypted; |
| 962 | } |
| 963 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 964 | int bdrv_key_required(BlockDriverState *bs) |
| 965 | { |
| 966 | BlockDriverState *backing_hd = bs->backing_hd; |
| 967 | |
| 968 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 969 | return 1; |
| 970 | return (bs->encrypted && !bs->valid_key); |
| 971 | } |
| 972 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 973 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 974 | { |
| 975 | int ret; |
| 976 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 977 | ret = bdrv_set_key(bs->backing_hd, key); |
| 978 | if (ret < 0) |
| 979 | return ret; |
| 980 | if (!bs->encrypted) |
| 981 | return 0; |
| 982 | } |
| 983 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) |
| 984 | return -1; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 985 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 986 | if (ret < 0) { |
| 987 | bs->valid_key = 0; |
| 988 | } else if (!bs->valid_key) { |
| 989 | bs->valid_key = 1; |
| 990 | /* call the change callback now, we skipped it on open */ |
| 991 | bs->media_changed = 1; |
| 992 | if (bs->change_cb) |
| 993 | bs->change_cb(bs->change_opaque); |
| 994 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 995 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 999 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1000 | if (!bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1001 | buf[0] = '\0'; |
| 1002 | } else { |
| 1003 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 1004 | } |
| 1005 | } |
| 1006 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1007 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1008 | void *opaque) |
| 1009 | { |
| 1010 | BlockDriver *drv; |
| 1011 | |
| 1012 | for (drv = first_drv; drv != NULL; drv = drv->next) { |
| 1013 | it(opaque, drv->format_name); |
| 1014 | } |
| 1015 | } |
| 1016 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1017 | BlockDriverState *bdrv_find(const char *name) |
| 1018 | { |
| 1019 | BlockDriverState *bs; |
| 1020 | |
| 1021 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 1022 | if (!strcmp(name, bs->device_name)) |
| 1023 | return bs; |
| 1024 | } |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1028 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1029 | { |
| 1030 | BlockDriverState *bs; |
| 1031 | |
| 1032 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1033 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1037 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 1038 | { |
| 1039 | return bs->device_name; |
| 1040 | } |
| 1041 | |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1042 | void bdrv_flush(BlockDriverState *bs) |
| 1043 | { |
aliguori | 081501d | 2009-03-29 01:31:51 +0000 | [diff] [blame] | 1044 | if (!bs->drv) |
| 1045 | return; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1046 | if (bs->drv->bdrv_flush) |
| 1047 | bs->drv->bdrv_flush(bs); |
| 1048 | if (bs->backing_hd) |
| 1049 | bdrv_flush(bs->backing_hd); |
| 1050 | } |
| 1051 | |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1052 | void bdrv_flush_all(void) |
| 1053 | { |
| 1054 | BlockDriverState *bs; |
| 1055 | |
| 1056 | for (bs = bdrv_first; bs != NULL; bs = bs->next) |
| 1057 | if (bs->drv && !bdrv_is_read_only(bs) && |
| 1058 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) |
| 1059 | bdrv_flush(bs); |
| 1060 | } |
| 1061 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1062 | /* |
| 1063 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 1064 | * not implementing the functionality are assumed to not support backing files, |
| 1065 | * hence all their sectors are reported as allocated. |
| 1066 | * |
| 1067 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1068 | * the specified sector) that are known to be in the same |
| 1069 | * allocated/unallocated state. |
| 1070 | * |
| 1071 | * 'nb_sectors' is the max value 'pnum' should be set to. |
| 1072 | */ |
| 1073 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1074 | int *pnum) |
| 1075 | { |
| 1076 | int64_t n; |
| 1077 | if (!bs->drv->bdrv_is_allocated) { |
| 1078 | if (sector_num >= bs->total_sectors) { |
| 1079 | *pnum = 0; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | n = bs->total_sectors - sector_num; |
| 1083 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
| 1084 | return 1; |
| 1085 | } |
| 1086 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); |
| 1087 | } |
| 1088 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1089 | void bdrv_info(Monitor *mon) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1090 | { |
| 1091 | BlockDriverState *bs; |
| 1092 | |
| 1093 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1094 | monitor_printf(mon, "%s:", bs->device_name); |
| 1095 | monitor_printf(mon, " type="); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1096 | switch(bs->type) { |
| 1097 | case BDRV_TYPE_HD: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1098 | monitor_printf(mon, "hd"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | case BDRV_TYPE_CDROM: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1101 | monitor_printf(mon, "cdrom"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1102 | break; |
| 1103 | case BDRV_TYPE_FLOPPY: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1104 | monitor_printf(mon, "floppy"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1107 | monitor_printf(mon, " removable=%d", bs->removable); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1108 | if (bs->removable) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1109 | monitor_printf(mon, " locked=%d", bs->locked); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1110 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1111 | if (bs->drv) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1112 | monitor_printf(mon, " file="); |
| 1113 | monitor_print_filename(mon, bs->filename); |
ths | fef3074 | 2006-12-22 14:11:32 +0000 | [diff] [blame] | 1114 | if (bs->backing_file[0] != '\0') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1115 | monitor_printf(mon, " backing_file="); |
| 1116 | monitor_print_filename(mon, bs->backing_file); |
| 1117 | } |
| 1118 | monitor_printf(mon, " ro=%d", bs->read_only); |
| 1119 | monitor_printf(mon, " drv=%s", bs->drv->format_name); |
| 1120 | monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1121 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1122 | monitor_printf(mon, " [not inserted]"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1123 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1124 | monitor_printf(mon, "\n"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* The "info blockstats" command. */ |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1129 | void bdrv_info_stats(Monitor *mon) |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1130 | { |
| 1131 | BlockDriverState *bs; |
| 1132 | |
| 1133 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1134 | monitor_printf(mon, "%s:" |
| 1135 | " rd_bytes=%" PRIu64 |
| 1136 | " wr_bytes=%" PRIu64 |
| 1137 | " rd_operations=%" PRIu64 |
| 1138 | " wr_operations=%" PRIu64 |
aliguori | ebf53fc | 2009-03-11 20:05:29 +0000 | [diff] [blame] | 1139 | "\n", |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1140 | bs->device_name, |
| 1141 | bs->rd_bytes, bs->wr_bytes, |
| 1142 | bs->rd_ops, bs->wr_ops); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1145 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 1146 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 1147 | { |
| 1148 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1149 | return bs->backing_file; |
| 1150 | else if (bs->encrypted) |
| 1151 | return bs->filename; |
| 1152 | else |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1156 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1157 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1158 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1159 | if (!bs->backing_hd) { |
| 1160 | pstrcpy(filename, filename_size, ""); |
| 1161 | } else { |
| 1162 | pstrcpy(filename, filename_size, bs->backing_file); |
| 1163 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1166 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1167 | const uint8_t *buf, int nb_sectors) |
| 1168 | { |
| 1169 | BlockDriver *drv = bs->drv; |
| 1170 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1171 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1172 | if (!drv->bdrv_write_compressed) |
| 1173 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 1174 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1175 | return -EIO; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1176 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 1177 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1178 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1179 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1180 | { |
| 1181 | BlockDriver *drv = bs->drv; |
| 1182 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1183 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1184 | if (!drv->bdrv_get_info) |
| 1185 | return -ENOTSUP; |
| 1186 | memset(bdi, 0, sizeof(*bdi)); |
| 1187 | return drv->bdrv_get_info(bs, bdi); |
| 1188 | } |
| 1189 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1190 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 1191 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1192 | { |
| 1193 | BlockDriver *drv = bs->drv; |
| 1194 | if (!drv) |
| 1195 | return -ENOMEDIUM; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1196 | if (!drv->bdrv_save_vmstate) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1197 | return -ENOTSUP; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1198 | return drv->bdrv_save_vmstate(bs, buf, pos, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1201 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 1202 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1203 | { |
| 1204 | BlockDriver *drv = bs->drv; |
| 1205 | if (!drv) |
| 1206 | return -ENOMEDIUM; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1207 | if (!drv->bdrv_load_vmstate) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1208 | return -ENOTSUP; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1209 | return drv->bdrv_load_vmstate(bs, buf, pos, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1212 | /**************************************************************/ |
| 1213 | /* handling of snapshots */ |
| 1214 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1215 | int bdrv_snapshot_create(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1216 | QEMUSnapshotInfo *sn_info) |
| 1217 | { |
| 1218 | BlockDriver *drv = bs->drv; |
| 1219 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1220 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1221 | if (!drv->bdrv_snapshot_create) |
| 1222 | return -ENOTSUP; |
| 1223 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 1224 | } |
| 1225 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1226 | int bdrv_snapshot_goto(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1227 | const char *snapshot_id) |
| 1228 | { |
| 1229 | BlockDriver *drv = bs->drv; |
| 1230 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1231 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1232 | if (!drv->bdrv_snapshot_goto) |
| 1233 | return -ENOTSUP; |
| 1234 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 1235 | } |
| 1236 | |
| 1237 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 1238 | { |
| 1239 | BlockDriver *drv = bs->drv; |
| 1240 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1241 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1242 | if (!drv->bdrv_snapshot_delete) |
| 1243 | return -ENOTSUP; |
| 1244 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 1245 | } |
| 1246 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1247 | int bdrv_snapshot_list(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1248 | QEMUSnapshotInfo **psn_info) |
| 1249 | { |
| 1250 | BlockDriver *drv = bs->drv; |
| 1251 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1252 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1253 | if (!drv->bdrv_snapshot_list) |
| 1254 | return -ENOTSUP; |
| 1255 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 1256 | } |
| 1257 | |
| 1258 | #define NB_SUFFIXES 4 |
| 1259 | |
| 1260 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 1261 | { |
| 1262 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 1263 | int64_t base; |
| 1264 | int i; |
| 1265 | |
| 1266 | if (size <= 999) { |
| 1267 | snprintf(buf, buf_size, "%" PRId64, size); |
| 1268 | } else { |
| 1269 | base = 1024; |
| 1270 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 1271 | if (size < (10 * base)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1272 | snprintf(buf, buf_size, "%0.1f%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1273 | (double)size / base, |
| 1274 | suffixes[i]); |
| 1275 | break; |
| 1276 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1277 | snprintf(buf, buf_size, "%" PRId64 "%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1278 | ((size + (base >> 1)) / base), |
| 1279 | suffixes[i]); |
| 1280 | break; |
| 1281 | } |
| 1282 | base = base * 1024; |
| 1283 | } |
| 1284 | } |
| 1285 | return buf; |
| 1286 | } |
| 1287 | |
| 1288 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 1289 | { |
| 1290 | char buf1[128], date_buf[128], clock_buf[128]; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1291 | #ifdef _WIN32 |
| 1292 | struct tm *ptm; |
| 1293 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1294 | struct tm tm; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1295 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1296 | time_t ti; |
| 1297 | int64_t secs; |
| 1298 | |
| 1299 | if (!sn) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1300 | snprintf(buf, buf_size, |
| 1301 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1302 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 1303 | } else { |
| 1304 | ti = sn->date_sec; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1305 | #ifdef _WIN32 |
| 1306 | ptm = localtime(&ti); |
| 1307 | strftime(date_buf, sizeof(date_buf), |
| 1308 | "%Y-%m-%d %H:%M:%S", ptm); |
| 1309 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1310 | localtime_r(&ti, &tm); |
| 1311 | strftime(date_buf, sizeof(date_buf), |
| 1312 | "%Y-%m-%d %H:%M:%S", &tm); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1313 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1314 | secs = sn->vm_clock_nsec / 1000000000; |
| 1315 | snprintf(clock_buf, sizeof(clock_buf), |
| 1316 | "%02d:%02d:%02d.%03d", |
| 1317 | (int)(secs / 3600), |
| 1318 | (int)((secs / 60) % 60), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1319 | (int)(secs % 60), |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1320 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 1321 | snprintf(buf, buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1322 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1323 | sn->id_str, sn->name, |
| 1324 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 1325 | date_buf, |
| 1326 | clock_buf); |
| 1327 | } |
| 1328 | return buf; |
| 1329 | } |
| 1330 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1331 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1332 | /**************************************************************/ |
| 1333 | /* async I/Os */ |
| 1334 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1335 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1336 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1337 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1338 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1339 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1340 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1341 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1342 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1343 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1344 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1345 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1346 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1347 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
| 1348 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (ret) { |
| 1351 | /* Update stats even though technically transfer has not happened. */ |
| 1352 | bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; |
| 1353 | bs->rd_ops ++; |
| 1354 | } |
| 1355 | |
| 1356 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1359 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 1360 | QEMUIOVector *qiov, int nb_sectors, |
| 1361 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1362 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1363 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1364 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1365 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1366 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1367 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1368 | if (bs->read_only) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1369 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1370 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1371 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1372 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1373 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
| 1374 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (ret) { |
| 1377 | /* Update stats even though technically transfer has not happened. */ |
| 1378 | bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; |
| 1379 | bs->wr_ops ++; |
| 1380 | } |
| 1381 | |
| 1382 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 1385 | |
| 1386 | typedef struct MultiwriteCB { |
| 1387 | int error; |
| 1388 | int num_requests; |
| 1389 | int num_callbacks; |
| 1390 | struct { |
| 1391 | BlockDriverCompletionFunc *cb; |
| 1392 | void *opaque; |
| 1393 | QEMUIOVector *free_qiov; |
| 1394 | void *free_buf; |
| 1395 | } callbacks[]; |
| 1396 | } MultiwriteCB; |
| 1397 | |
| 1398 | static void multiwrite_user_cb(MultiwriteCB *mcb) |
| 1399 | { |
| 1400 | int i; |
| 1401 | |
| 1402 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 1403 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
| 1404 | qemu_free(mcb->callbacks[i].free_qiov); |
| 1405 | qemu_free(mcb->callbacks[i].free_buf); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | static void multiwrite_cb(void *opaque, int ret) |
| 1410 | { |
| 1411 | MultiwriteCB *mcb = opaque; |
| 1412 | |
| 1413 | if (ret < 0) { |
| 1414 | mcb->error = ret; |
| 1415 | multiwrite_user_cb(mcb); |
| 1416 | } |
| 1417 | |
| 1418 | mcb->num_requests--; |
| 1419 | if (mcb->num_requests == 0) { |
| 1420 | if (mcb->error == 0) { |
| 1421 | multiwrite_user_cb(mcb); |
| 1422 | } |
| 1423 | qemu_free(mcb); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | static int multiwrite_req_compare(const void *a, const void *b) |
| 1428 | { |
| 1429 | return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector); |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Takes a bunch of requests and tries to merge them. Returns the number of |
| 1434 | * requests that remain after merging. |
| 1435 | */ |
| 1436 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
| 1437 | int num_reqs, MultiwriteCB *mcb) |
| 1438 | { |
| 1439 | int i, outidx; |
| 1440 | |
| 1441 | // Sort requests by start sector |
| 1442 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); |
| 1443 | |
| 1444 | // Check if adjacent requests touch the same clusters. If so, combine them, |
| 1445 | // filling up gaps with zero sectors. |
| 1446 | outidx = 0; |
| 1447 | for (i = 1; i < num_reqs; i++) { |
| 1448 | int merge = 0; |
| 1449 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
| 1450 | |
| 1451 | // This handles the cases that are valid for all block drivers, namely |
| 1452 | // exactly sequential writes and overlapping writes. |
| 1453 | if (reqs[i].sector <= oldreq_last) { |
| 1454 | merge = 1; |
| 1455 | } |
| 1456 | |
| 1457 | // The block driver may decide that it makes sense to combine requests |
| 1458 | // even if there is a gap of some sectors between them. In this case, |
| 1459 | // the gap is filled with zeros (therefore only applicable for yet |
| 1460 | // unused space in format like qcow2). |
| 1461 | if (!merge && bs->drv->bdrv_merge_requests) { |
| 1462 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
| 1463 | } |
| 1464 | |
| 1465 | if (merge) { |
| 1466 | size_t size; |
| 1467 | QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); |
| 1468 | qemu_iovec_init(qiov, |
| 1469 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); |
| 1470 | |
| 1471 | // Add the first request to the merged one. If the requests are |
| 1472 | // overlapping, drop the last sectors of the first request. |
| 1473 | size = (reqs[i].sector - reqs[outidx].sector) << 9; |
| 1474 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
| 1475 | |
| 1476 | // We might need to add some zeros between the two requests |
| 1477 | if (reqs[i].sector > oldreq_last) { |
| 1478 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; |
| 1479 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
| 1480 | memset(buf, 0, zero_bytes); |
| 1481 | qemu_iovec_add(qiov, buf, zero_bytes); |
| 1482 | mcb->callbacks[i].free_buf = buf; |
| 1483 | } |
| 1484 | |
| 1485 | // Add the second request |
| 1486 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
| 1487 | |
| 1488 | reqs[outidx].nb_sectors += reqs[i].nb_sectors; |
| 1489 | reqs[outidx].qiov = qiov; |
| 1490 | |
| 1491 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
| 1492 | } else { |
| 1493 | outidx++; |
| 1494 | reqs[outidx].sector = reqs[i].sector; |
| 1495 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
| 1496 | reqs[outidx].qiov = reqs[i].qiov; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | return outidx + 1; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Submit multiple AIO write requests at once. |
| 1505 | * |
| 1506 | * On success, the function returns 0 and all requests in the reqs array have |
| 1507 | * been submitted. In error case this function returns -1, and any of the |
| 1508 | * requests may or may not be submitted yet. In particular, this means that the |
| 1509 | * callback will be called for some of the requests, for others it won't. The |
| 1510 | * caller must check the error field of the BlockRequest to wait for the right |
| 1511 | * callbacks (if error != 0, no callback will be called). |
| 1512 | * |
| 1513 | * The implementation may modify the contents of the reqs array, e.g. to merge |
| 1514 | * requests. However, the fields opaque and error are left unmodified as they |
| 1515 | * are used to signal failure for a single request to the caller. |
| 1516 | */ |
| 1517 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
| 1518 | { |
| 1519 | BlockDriverAIOCB *acb; |
| 1520 | MultiwriteCB *mcb; |
| 1521 | int i; |
| 1522 | |
| 1523 | if (num_reqs == 0) { |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | // Create MultiwriteCB structure |
| 1528 | mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
| 1529 | mcb->num_requests = 0; |
| 1530 | mcb->num_callbacks = num_reqs; |
| 1531 | |
| 1532 | for (i = 0; i < num_reqs; i++) { |
| 1533 | mcb->callbacks[i].cb = reqs[i].cb; |
| 1534 | mcb->callbacks[i].opaque = reqs[i].opaque; |
| 1535 | } |
| 1536 | |
| 1537 | // Check for mergable requests |
| 1538 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
| 1539 | |
| 1540 | // Run the aio requests |
| 1541 | for (i = 0; i < num_reqs; i++) { |
| 1542 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
| 1543 | reqs[i].nb_sectors, multiwrite_cb, mcb); |
| 1544 | |
| 1545 | if (acb == NULL) { |
| 1546 | // We can only fail the whole thing if no request has been |
| 1547 | // submitted yet. Otherwise we'll wait for the submitted AIOs to |
| 1548 | // complete and report the error in the callback. |
| 1549 | if (mcb->num_requests == 0) { |
| 1550 | reqs[i].error = EIO; |
| 1551 | goto fail; |
| 1552 | } else { |
| 1553 | mcb->error = EIO; |
| 1554 | break; |
| 1555 | } |
| 1556 | } else { |
| 1557 | mcb->num_requests++; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | return 0; |
| 1562 | |
| 1563 | fail: |
| 1564 | free(mcb); |
| 1565 | return -1; |
| 1566 | } |
| 1567 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 1568 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
| 1569 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1570 | { |
| 1571 | BlockDriver *drv = bs->drv; |
| 1572 | |
| 1573 | if (!drv) |
| 1574 | return NULL; |
| 1575 | |
| 1576 | /* |
| 1577 | * Note that unlike bdrv_flush the driver is reponsible for flushing a |
| 1578 | * backing image if it exists. |
| 1579 | */ |
| 1580 | return drv->bdrv_aio_flush(bs, cb, opaque); |
| 1581 | } |
| 1582 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1583 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1584 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1585 | acb->pool->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1588 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1589 | /**************************************************************/ |
| 1590 | /* async block device emulation */ |
| 1591 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1592 | typedef struct BlockDriverAIOCBSync { |
| 1593 | BlockDriverAIOCB common; |
| 1594 | QEMUBH *bh; |
| 1595 | int ret; |
| 1596 | /* vector translation state */ |
| 1597 | QEMUIOVector *qiov; |
| 1598 | uint8_t *bounce; |
| 1599 | int is_write; |
| 1600 | } BlockDriverAIOCBSync; |
| 1601 | |
| 1602 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
| 1603 | { |
| 1604 | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 1605 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 1606 | acb->bh = NULL; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1607 | qemu_aio_release(acb); |
| 1608 | } |
| 1609 | |
| 1610 | static AIOPool bdrv_em_aio_pool = { |
| 1611 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
| 1612 | .cancel = bdrv_aio_cancel_em, |
| 1613 | }; |
| 1614 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1615 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1616 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1617 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1618 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1619 | if (!acb->is_write) |
| 1620 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 1621 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1622 | acb->common.cb(acb->common.opaque, acb->ret); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 1623 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 1624 | acb->bh = NULL; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1625 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1626 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1627 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1628 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 1629 | int64_t sector_num, |
| 1630 | QEMUIOVector *qiov, |
| 1631 | int nb_sectors, |
| 1632 | BlockDriverCompletionFunc *cb, |
| 1633 | void *opaque, |
| 1634 | int is_write) |
| 1635 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1636 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1637 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1638 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1639 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1640 | acb->is_write = is_write; |
| 1641 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1642 | acb->bounce = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1643 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1644 | if (!acb->bh) |
| 1645 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (is_write) { |
| 1648 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
| 1649 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
| 1650 | } else { |
| 1651 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
| 1652 | } |
| 1653 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1654 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1655 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1656 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1659 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 1660 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1661 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1662 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1663 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1666 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 1667 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 1668 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1669 | { |
| 1670 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 1671 | } |
| 1672 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 1673 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 1674 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1675 | { |
| 1676 | BlockDriverAIOCBSync *acb; |
| 1677 | |
| 1678 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 1679 | acb->is_write = 1; /* don't bounce in the completion hadler */ |
| 1680 | acb->qiov = NULL; |
| 1681 | acb->bounce = NULL; |
| 1682 | acb->ret = 0; |
| 1683 | |
| 1684 | if (!acb->bh) |
| 1685 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 1686 | |
| 1687 | bdrv_flush(bs); |
| 1688 | qemu_bh_schedule(acb->bh); |
| 1689 | return &acb->common; |
| 1690 | } |
| 1691 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1692 | /**************************************************************/ |
| 1693 | /* sync block device emulation */ |
| 1694 | |
| 1695 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 1696 | { |
| 1697 | *(int *)opaque = ret; |
| 1698 | } |
| 1699 | |
| 1700 | #define NOT_DONE 0x7fffffff |
| 1701 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1702 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1703 | uint8_t *buf, int nb_sectors) |
| 1704 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1705 | int async_ret; |
| 1706 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1707 | struct iovec iov; |
| 1708 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1709 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1710 | async_context_push(); |
| 1711 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1712 | async_ret = NOT_DONE; |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 1713 | iov.iov_base = (void *)buf; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1714 | iov.iov_len = nb_sectors * 512; |
| 1715 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1716 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
| 1717 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1718 | if (acb == NULL) { |
| 1719 | async_ret = -1; |
| 1720 | goto fail; |
| 1721 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1722 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1723 | while (async_ret == NOT_DONE) { |
| 1724 | qemu_aio_wait(); |
| 1725 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1726 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1727 | |
| 1728 | fail: |
| 1729 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1730 | return async_ret; |
| 1731 | } |
| 1732 | |
| 1733 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 1734 | const uint8_t *buf, int nb_sectors) |
| 1735 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1736 | int async_ret; |
| 1737 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1738 | struct iovec iov; |
| 1739 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1740 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1741 | async_context_push(); |
| 1742 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1743 | async_ret = NOT_DONE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1744 | iov.iov_base = (void *)buf; |
| 1745 | iov.iov_len = nb_sectors * 512; |
| 1746 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1747 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
| 1748 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1749 | if (acb == NULL) { |
| 1750 | async_ret = -1; |
| 1751 | goto fail; |
| 1752 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1753 | while (async_ret == NOT_DONE) { |
| 1754 | qemu_aio_wait(); |
| 1755 | } |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1756 | |
| 1757 | fail: |
| 1758 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1759 | return async_ret; |
| 1760 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1761 | |
| 1762 | void bdrv_init(void) |
| 1763 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1764 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1765 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1766 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1767 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
| 1768 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1769 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1770 | BlockDriverAIOCB *acb; |
| 1771 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1772 | if (pool->free_aiocb) { |
| 1773 | acb = pool->free_aiocb; |
| 1774 | pool->free_aiocb = acb->next; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1775 | } else { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1776 | acb = qemu_mallocz(pool->aiocb_size); |
| 1777 | acb->pool = pool; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1778 | } |
| 1779 | acb->bs = bs; |
| 1780 | acb->cb = cb; |
| 1781 | acb->opaque = opaque; |
| 1782 | return acb; |
| 1783 | } |
| 1784 | |
| 1785 | void qemu_aio_release(void *p) |
| 1786 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1787 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
| 1788 | AIOPool *pool = acb->pool; |
| 1789 | acb->next = pool->free_aiocb; |
| 1790 | pool->free_aiocb = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1791 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1792 | |
| 1793 | /**************************************************************/ |
| 1794 | /* removable device support */ |
| 1795 | |
| 1796 | /** |
| 1797 | * Return TRUE if the media is present |
| 1798 | */ |
| 1799 | int bdrv_is_inserted(BlockDriverState *bs) |
| 1800 | { |
| 1801 | BlockDriver *drv = bs->drv; |
| 1802 | int ret; |
| 1803 | if (!drv) |
| 1804 | return 0; |
| 1805 | if (!drv->bdrv_is_inserted) |
| 1806 | return 1; |
| 1807 | ret = drv->bdrv_is_inserted(bs); |
| 1808 | return ret; |
| 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * Return TRUE if the media changed since the last call to this |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1813 | * function. It is currently only used for floppy disks |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1814 | */ |
| 1815 | int bdrv_media_changed(BlockDriverState *bs) |
| 1816 | { |
| 1817 | BlockDriver *drv = bs->drv; |
| 1818 | int ret; |
| 1819 | |
| 1820 | if (!drv || !drv->bdrv_media_changed) |
| 1821 | ret = -ENOTSUP; |
| 1822 | else |
| 1823 | ret = drv->bdrv_media_changed(bs); |
| 1824 | if (ret == -ENOTSUP) |
| 1825 | ret = bs->media_changed; |
| 1826 | bs->media_changed = 0; |
| 1827 | return ret; |
| 1828 | } |
| 1829 | |
| 1830 | /** |
| 1831 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 1832 | */ |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1833 | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1834 | { |
| 1835 | BlockDriver *drv = bs->drv; |
| 1836 | int ret; |
| 1837 | |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1838 | if (bs->locked) { |
| 1839 | return -EBUSY; |
| 1840 | } |
| 1841 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1842 | if (!drv || !drv->bdrv_eject) { |
| 1843 | ret = -ENOTSUP; |
| 1844 | } else { |
| 1845 | ret = drv->bdrv_eject(bs, eject_flag); |
| 1846 | } |
| 1847 | if (ret == -ENOTSUP) { |
| 1848 | if (eject_flag) |
| 1849 | bdrv_close(bs); |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1850 | ret = 0; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1851 | } |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1852 | |
| 1853 | return ret; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | int bdrv_is_locked(BlockDriverState *bs) |
| 1857 | { |
| 1858 | return bs->locked; |
| 1859 | } |
| 1860 | |
| 1861 | /** |
| 1862 | * Lock or unlock the media (if it is locked, the user won't be able |
| 1863 | * to eject it manually). |
| 1864 | */ |
| 1865 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 1866 | { |
| 1867 | BlockDriver *drv = bs->drv; |
| 1868 | |
| 1869 | bs->locked = locked; |
| 1870 | if (drv && drv->bdrv_set_locked) { |
| 1871 | drv->bdrv_set_locked(bs, locked); |
| 1872 | } |
| 1873 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1874 | |
| 1875 | /* needed for generic scsi interface */ |
| 1876 | |
| 1877 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 1878 | { |
| 1879 | BlockDriver *drv = bs->drv; |
| 1880 | |
| 1881 | if (drv && drv->bdrv_ioctl) |
| 1882 | return drv->bdrv_ioctl(bs, req, buf); |
| 1883 | return -ENOTSUP; |
| 1884 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1885 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1886 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 1887 | unsigned long int req, void *buf, |
| 1888 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1889 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1890 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1891 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1892 | if (drv && drv->bdrv_aio_ioctl) |
| 1893 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 1894 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1895 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1896 | |
| 1897 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 1898 | { |
| 1899 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 1900 | } |