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