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