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