root / block.c @ 8c99506c
History | View | Annotate | Download (35.9 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 | faf07963 | pbrook | #include "qemu-common.h" |
25 | 87ecb68b | pbrook | #ifndef QEMU_IMG
|
26 | 87ecb68b | pbrook | #include "console.h" |
27 | faf07963 | pbrook | #endif
|
28 | ea2384d3 | bellard | #include "block_int.h" |
29 | fc01f7e7 | bellard | |
30 | 7674e7bf | bellard | #ifdef _BSD
|
31 | 7674e7bf | bellard | #include <sys/types.h> |
32 | 7674e7bf | bellard | #include <sys/stat.h> |
33 | 7674e7bf | bellard | #include <sys/ioctl.h> |
34 | 7674e7bf | bellard | #include <sys/queue.h> |
35 | 7674e7bf | bellard | #include <sys/disk.h> |
36 | 7674e7bf | bellard | #endif
|
37 | 7674e7bf | bellard | |
38 | 83f64091 | bellard | #define SECTOR_BITS 9 |
39 | 83f64091 | bellard | #define SECTOR_SIZE (1 << SECTOR_BITS) |
40 | 83f64091 | bellard | |
41 | 90765429 | bellard | typedef struct BlockDriverAIOCBSync { |
42 | 90765429 | bellard | BlockDriverAIOCB common; |
43 | 90765429 | bellard | QEMUBH *bh; |
44 | 90765429 | bellard | int ret;
|
45 | 90765429 | bellard | } BlockDriverAIOCBSync; |
46 | 90765429 | bellard | |
47 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
|
48 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
49 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
50 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
|
51 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
52 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
53 | 83f64091 | bellard | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
54 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
55 | 83f64091 | bellard | uint8_t *buf, int nb_sectors);
|
56 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
57 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors); |
58 | ec530c81 | bellard | |
59 | faf07963 | pbrook | BlockDriverState *bdrv_first; |
60 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
61 | ea2384d3 | bellard | |
62 | 83f64091 | bellard | int path_is_absolute(const char *path) |
63 | 3b0d4f61 | bellard | { |
64 | 83f64091 | bellard | const char *p; |
65 | 21664424 | bellard | #ifdef _WIN32
|
66 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
67 | 21664424 | bellard | if (*path == '/' || *path == '\\') |
68 | 21664424 | bellard | return 1; |
69 | 21664424 | bellard | #endif
|
70 | 83f64091 | bellard | p = strchr(path, ':');
|
71 | 83f64091 | bellard | if (p)
|
72 | 83f64091 | bellard | p++; |
73 | 83f64091 | bellard | else
|
74 | 83f64091 | bellard | p = path; |
75 | 3b9f94e1 | bellard | #ifdef _WIN32
|
76 | 3b9f94e1 | bellard | return (*p == '/' || *p == '\\'); |
77 | 3b9f94e1 | bellard | #else
|
78 | 3b9f94e1 | bellard | return (*p == '/'); |
79 | 3b9f94e1 | bellard | #endif
|
80 | 3b0d4f61 | bellard | } |
81 | 3b0d4f61 | bellard | |
82 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
83 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
84 | 83f64091 | bellard | supported. */
|
85 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
86 | 83f64091 | bellard | const char *base_path, |
87 | 83f64091 | bellard | const char *filename) |
88 | 3b0d4f61 | bellard | { |
89 | 83f64091 | bellard | const char *p, *p1; |
90 | 83f64091 | bellard | int len;
|
91 | 83f64091 | bellard | |
92 | 83f64091 | bellard | if (dest_size <= 0) |
93 | 83f64091 | bellard | return;
|
94 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
95 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
96 | 83f64091 | bellard | } else {
|
97 | 83f64091 | bellard | p = strchr(base_path, ':');
|
98 | 83f64091 | bellard | if (p)
|
99 | 83f64091 | bellard | p++; |
100 | 83f64091 | bellard | else
|
101 | 83f64091 | bellard | p = base_path; |
102 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
103 | 3b9f94e1 | bellard | #ifdef _WIN32
|
104 | 3b9f94e1 | bellard | { |
105 | 3b9f94e1 | bellard | const char *p2; |
106 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
107 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
108 | 3b9f94e1 | bellard | p1 = p2; |
109 | 3b9f94e1 | bellard | } |
110 | 3b9f94e1 | bellard | #endif
|
111 | 83f64091 | bellard | if (p1)
|
112 | 83f64091 | bellard | p1++; |
113 | 83f64091 | bellard | else
|
114 | 83f64091 | bellard | p1 = base_path; |
115 | 83f64091 | bellard | if (p1 > p)
|
116 | 83f64091 | bellard | p = p1; |
117 | 83f64091 | bellard | len = p - base_path; |
118 | 83f64091 | bellard | if (len > dest_size - 1) |
119 | 83f64091 | bellard | len = dest_size - 1;
|
120 | 83f64091 | bellard | memcpy(dest, base_path, len); |
121 | 83f64091 | bellard | dest[len] = '\0';
|
122 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
123 | 3b0d4f61 | bellard | } |
124 | 3b0d4f61 | bellard | } |
125 | 3b0d4f61 | bellard | |
126 | 3b0d4f61 | bellard | |
127 | 9596ebb7 | pbrook | static void bdrv_register(BlockDriver *bdrv) |
128 | ea2384d3 | bellard | { |
129 | ce1a14dc | pbrook | if (!bdrv->bdrv_aio_read) {
|
130 | 83f64091 | bellard | /* add AIO emulation layer */
|
131 | 83f64091 | bellard | bdrv->bdrv_aio_read = bdrv_aio_read_em; |
132 | 83f64091 | bellard | bdrv->bdrv_aio_write = bdrv_aio_write_em; |
133 | 83f64091 | bellard | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; |
134 | 90765429 | bellard | bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
|
135 | 83f64091 | bellard | } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { |
136 | 83f64091 | bellard | /* add synchronous IO emulation layer */
|
137 | 83f64091 | bellard | bdrv->bdrv_read = bdrv_read_em; |
138 | 83f64091 | bellard | bdrv->bdrv_write = bdrv_write_em; |
139 | 83f64091 | bellard | } |
140 | ea2384d3 | bellard | bdrv->next = first_drv; |
141 | ea2384d3 | bellard | first_drv = bdrv; |
142 | ea2384d3 | bellard | } |
143 | b338082b | bellard | |
144 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
145 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
146 | b338082b | bellard | { |
147 | b338082b | bellard | BlockDriverState **pbs, *bs; |
148 | b338082b | bellard | |
149 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
150 | b338082b | bellard | if(!bs)
|
151 | b338082b | bellard | return NULL; |
152 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
153 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
154 | ea2384d3 | bellard | /* insert at the end */
|
155 | ea2384d3 | bellard | pbs = &bdrv_first; |
156 | ea2384d3 | bellard | while (*pbs != NULL) |
157 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
158 | ea2384d3 | bellard | *pbs = bs; |
159 | ea2384d3 | bellard | } |
160 | b338082b | bellard | return bs;
|
161 | b338082b | bellard | } |
162 | b338082b | bellard | |
163 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
164 | ea2384d3 | bellard | { |
165 | ea2384d3 | bellard | BlockDriver *drv1; |
166 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
167 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
168 | ea2384d3 | bellard | return drv1;
|
169 | ea2384d3 | bellard | } |
170 | ea2384d3 | bellard | return NULL; |
171 | ea2384d3 | bellard | } |
172 | ea2384d3 | bellard | |
173 | 5fafdf24 | ths | int bdrv_create(BlockDriver *drv,
|
174 | ea2384d3 | bellard | const char *filename, int64_t size_in_sectors, |
175 | ea2384d3 | bellard | const char *backing_file, int flags) |
176 | ea2384d3 | bellard | { |
177 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
178 | ea2384d3 | bellard | return -ENOTSUP;
|
179 | ea2384d3 | bellard | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
|
180 | ea2384d3 | bellard | } |
181 | ea2384d3 | bellard | |
182 | d5249393 | bellard | #ifdef _WIN32
|
183 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
184 | d5249393 | bellard | { |
185 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
186 | 3b46e624 | ths | |
187 | 3b9f94e1 | bellard | GetTempPath(MAX_PATH, temp_dir); |
188 | 3b9f94e1 | bellard | GetTempFileName(temp_dir, "qem", 0, filename); |
189 | d5249393 | bellard | } |
190 | d5249393 | bellard | #else
|
191 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
192 | fc01f7e7 | bellard | { |
193 | 67b915a5 | bellard | int fd;
|
194 | 0badc1ee | aurel32 | char *tmpdir;
|
195 | d5249393 | bellard | /* XXX: race condition possible */
|
196 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
197 | 0badc1ee | aurel32 | if (!tmpdir)
|
198 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
199 | 0badc1ee | aurel32 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
|
200 | ea2384d3 | bellard | fd = mkstemp(filename); |
201 | ea2384d3 | bellard | close(fd); |
202 | ea2384d3 | bellard | } |
203 | d5249393 | bellard | #endif
|
204 | fc01f7e7 | bellard | |
205 | 19cb3738 | bellard | #ifdef _WIN32
|
206 | f45512fe | bellard | static int is_windows_drive_prefix(const char *filename) |
207 | f45512fe | bellard | { |
208 | f45512fe | bellard | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
209 | f45512fe | bellard | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
210 | f45512fe | bellard | filename[1] == ':'); |
211 | f45512fe | bellard | } |
212 | 3b46e624 | ths | |
213 | 19cb3738 | bellard | static int is_windows_drive(const char *filename) |
214 | 19cb3738 | bellard | { |
215 | 5fafdf24 | ths | if (is_windows_drive_prefix(filename) &&
|
216 | f45512fe | bellard | filename[2] == '\0') |
217 | 19cb3738 | bellard | return 1; |
218 | 19cb3738 | bellard | if (strstart(filename, "\\\\.\\", NULL) || |
219 | 19cb3738 | bellard | strstart(filename, "//./", NULL)) |
220 | 19cb3738 | bellard | return 1; |
221 | 19cb3738 | bellard | return 0; |
222 | 19cb3738 | bellard | } |
223 | 19cb3738 | bellard | #endif
|
224 | 19cb3738 | bellard | |
225 | 83f64091 | bellard | static BlockDriver *find_protocol(const char *filename) |
226 | 83f64091 | bellard | { |
227 | 83f64091 | bellard | BlockDriver *drv1; |
228 | 83f64091 | bellard | char protocol[128]; |
229 | 83f64091 | bellard | int len;
|
230 | 83f64091 | bellard | const char *p; |
231 | 19cb3738 | bellard | |
232 | 19cb3738 | bellard | #ifdef _WIN32
|
233 | f45512fe | bellard | if (is_windows_drive(filename) ||
|
234 | f45512fe | bellard | is_windows_drive_prefix(filename)) |
235 | 19cb3738 | bellard | return &bdrv_raw;
|
236 | 19cb3738 | bellard | #endif
|
237 | 83f64091 | bellard | p = strchr(filename, ':');
|
238 | 83f64091 | bellard | if (!p)
|
239 | 83f64091 | bellard | return &bdrv_raw;
|
240 | 83f64091 | bellard | len = p - filename; |
241 | 83f64091 | bellard | if (len > sizeof(protocol) - 1) |
242 | 83f64091 | bellard | len = sizeof(protocol) - 1; |
243 | 83f64091 | bellard | memcpy(protocol, filename, len); |
244 | 83f64091 | bellard | protocol[len] = '\0';
|
245 | 83f64091 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
246 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
247 | 83f64091 | bellard | !strcmp(drv1->protocol_name, protocol)) |
248 | 83f64091 | bellard | return drv1;
|
249 | 83f64091 | bellard | } |
250 | 83f64091 | bellard | return NULL; |
251 | 83f64091 | bellard | } |
252 | 83f64091 | bellard | |
253 | 7674e7bf | bellard | /* XXX: force raw format if block or character device ? It would
|
254 | 7674e7bf | bellard | simplify the BSD case */
|
255 | ea2384d3 | bellard | static BlockDriver *find_image_format(const char *filename) |
256 | ea2384d3 | bellard | { |
257 | 83f64091 | bellard | int ret, score, score_max;
|
258 | ea2384d3 | bellard | BlockDriver *drv1, *drv; |
259 | 83f64091 | bellard | uint8_t buf[2048];
|
260 | 83f64091 | bellard | BlockDriverState *bs; |
261 | 3b46e624 | ths | |
262 | 19cb3738 | bellard | /* detect host devices. By convention, /dev/cdrom[N] is always
|
263 | 19cb3738 | bellard | recognized as a host CDROM */
|
264 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) |
265 | 19cb3738 | bellard | return &bdrv_host_device;
|
266 | 19cb3738 | bellard | #ifdef _WIN32
|
267 | 19cb3738 | bellard | if (is_windows_drive(filename))
|
268 | 19cb3738 | bellard | return &bdrv_host_device;
|
269 | 19cb3738 | bellard | #else
|
270 | 19cb3738 | bellard | { |
271 | 19cb3738 | bellard | struct stat st;
|
272 | 5fafdf24 | ths | if (stat(filename, &st) >= 0 && |
273 | 19cb3738 | bellard | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
274 | 19cb3738 | bellard | return &bdrv_host_device;
|
275 | 19cb3738 | bellard | } |
276 | 19cb3738 | bellard | } |
277 | 19cb3738 | bellard | #endif
|
278 | 3b46e624 | ths | |
279 | 83f64091 | bellard | drv = find_protocol(filename); |
280 | 19cb3738 | bellard | /* no need to test disk image formats for vvfat */
|
281 | 83f64091 | bellard | if (drv == &bdrv_vvfat)
|
282 | 83f64091 | bellard | return drv;
|
283 | 19cb3738 | bellard | |
284 | 83f64091 | bellard | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
285 | 83f64091 | bellard | if (ret < 0) |
286 | 83f64091 | bellard | return NULL; |
287 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
288 | 83f64091 | bellard | bdrv_delete(bs); |
289 | 83f64091 | bellard | if (ret < 0) { |
290 | 83f64091 | bellard | return NULL; |
291 | 83f64091 | bellard | } |
292 | 83f64091 | bellard | |
293 | ea2384d3 | bellard | score_max = 0;
|
294 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
295 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
296 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
297 | 83f64091 | bellard | if (score > score_max) {
|
298 | 83f64091 | bellard | score_max = score; |
299 | 83f64091 | bellard | drv = drv1; |
300 | 83f64091 | bellard | } |
301 | 0849bf08 | bellard | } |
302 | fc01f7e7 | bellard | } |
303 | ea2384d3 | bellard | return drv;
|
304 | ea2384d3 | bellard | } |
305 | ea2384d3 | bellard | |
306 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
307 | ea2384d3 | bellard | { |
308 | 83f64091 | bellard | BlockDriverState *bs; |
309 | 83f64091 | bellard | int ret;
|
310 | 83f64091 | bellard | |
311 | 83f64091 | bellard | bs = bdrv_new("");
|
312 | 83f64091 | bellard | if (!bs)
|
313 | 83f64091 | bellard | return -ENOMEM;
|
314 | 83f64091 | bellard | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
|
315 | 83f64091 | bellard | if (ret < 0) { |
316 | 83f64091 | bellard | bdrv_delete(bs); |
317 | 83f64091 | bellard | return ret;
|
318 | 3b0d4f61 | bellard | } |
319 | 83f64091 | bellard | *pbs = bs; |
320 | 83f64091 | bellard | return 0; |
321 | 83f64091 | bellard | } |
322 | 83f64091 | bellard | |
323 | 83f64091 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
324 | 83f64091 | bellard | { |
325 | 83f64091 | bellard | return bdrv_open2(bs, filename, flags, NULL); |
326 | ea2384d3 | bellard | } |
327 | ea2384d3 | bellard | |
328 | 83f64091 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
329 | ea2384d3 | bellard | BlockDriver *drv) |
330 | ea2384d3 | bellard | { |
331 | 83f64091 | bellard | int ret, open_flags;
|
332 | eb5c851f | ths | char tmp_filename[PATH_MAX];
|
333 | eb5c851f | ths | char backing_filename[PATH_MAX];
|
334 | 3b46e624 | ths | |
335 | ea2384d3 | bellard | bs->read_only = 0;
|
336 | ea2384d3 | bellard | bs->is_temporary = 0;
|
337 | ea2384d3 | bellard | bs->encrypted = 0;
|
338 | 712e7874 | bellard | |
339 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
340 | ea2384d3 | bellard | BlockDriverState *bs1; |
341 | ea2384d3 | bellard | int64_t total_size; |
342 | 3b46e624 | ths | |
343 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
344 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
345 | 33e3963e | bellard | |
346 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
347 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
348 | ea2384d3 | bellard | if (!bs1) {
|
349 | 83f64091 | bellard | return -ENOMEM;
|
350 | ea2384d3 | bellard | } |
351 | ea2384d3 | bellard | if (bdrv_open(bs1, filename, 0) < 0) { |
352 | ea2384d3 | bellard | bdrv_delete(bs1); |
353 | ea2384d3 | bellard | return -1; |
354 | ea2384d3 | bellard | } |
355 | 83f64091 | bellard | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
356 | ea2384d3 | bellard | bdrv_delete(bs1); |
357 | 3b46e624 | ths | |
358 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
359 | a817d936 | bellard | realpath(filename, backing_filename); |
360 | 5fafdf24 | ths | if (bdrv_create(&bdrv_qcow2, tmp_filename,
|
361 | a817d936 | bellard | total_size, backing_filename, 0) < 0) { |
362 | ea2384d3 | bellard | return -1; |
363 | ea2384d3 | bellard | } |
364 | ea2384d3 | bellard | filename = tmp_filename; |
365 | ea2384d3 | bellard | bs->is_temporary = 1;
|
366 | ea2384d3 | bellard | } |
367 | 712e7874 | bellard | |
368 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
369 | 83f64091 | bellard | if (flags & BDRV_O_FILE) {
|
370 | 83f64091 | bellard | drv = find_protocol(filename); |
371 | ea2384d3 | bellard | if (!drv)
|
372 | 83f64091 | bellard | return -ENOENT;
|
373 | 83f64091 | bellard | } else {
|
374 | 83f64091 | bellard | if (!drv) {
|
375 | 83f64091 | bellard | drv = find_image_format(filename); |
376 | 83f64091 | bellard | if (!drv)
|
377 | 83f64091 | bellard | return -1; |
378 | 83f64091 | bellard | } |
379 | ea2384d3 | bellard | } |
380 | ea2384d3 | bellard | bs->drv = drv; |
381 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
382 | ea2384d3 | bellard | if (bs->opaque == NULL && drv->instance_size > 0) |
383 | ea2384d3 | bellard | return -1; |
384 | 83f64091 | bellard | /* Note: for compatibility, we open disk image files as RDWR, and
|
385 | 83f64091 | bellard | RDONLY as fallback */
|
386 | 83f64091 | bellard | if (!(flags & BDRV_O_FILE))
|
387 | 33f00271 | balrog | open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT); |
388 | 83f64091 | bellard | else
|
389 | 83f64091 | bellard | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
390 | 83f64091 | bellard | ret = drv->bdrv_open(bs, filename, open_flags); |
391 | 83f64091 | bellard | if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
|
392 | 83f64091 | bellard | ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); |
393 | 83f64091 | bellard | bs->read_only = 1;
|
394 | 83f64091 | bellard | } |
395 | ea2384d3 | bellard | if (ret < 0) { |
396 | ea2384d3 | bellard | qemu_free(bs->opaque); |
397 | 6b21b973 | bellard | bs->opaque = NULL;
|
398 | 6b21b973 | bellard | bs->drv = NULL;
|
399 | 83f64091 | bellard | return ret;
|
400 | 33e3963e | bellard | } |
401 | d15a771d | bellard | if (drv->bdrv_getlength) {
|
402 | d15a771d | bellard | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
403 | d15a771d | bellard | } |
404 | 67b915a5 | bellard | #ifndef _WIN32
|
405 | ea2384d3 | bellard | if (bs->is_temporary) {
|
406 | ea2384d3 | bellard | unlink(filename); |
407 | ea2384d3 | bellard | } |
408 | ea2384d3 | bellard | #endif
|
409 | 83f64091 | bellard | if (bs->backing_file[0] != '\0') { |
410 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
411 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
412 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
413 | ea2384d3 | bellard | fail:
|
414 | ea2384d3 | bellard | bdrv_close(bs); |
415 | 6b21b973 | bellard | return -ENOMEM;
|
416 | 33e3963e | bellard | } |
417 | 83f64091 | bellard | path_combine(backing_filename, sizeof(backing_filename),
|
418 | 83f64091 | bellard | filename, bs->backing_file); |
419 | 83f64091 | bellard | if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) |
420 | 33e3963e | bellard | goto fail;
|
421 | 33e3963e | bellard | } |
422 | 33e3963e | bellard | |
423 | b338082b | bellard | /* call the change callback */
|
424 | 19cb3738 | bellard | bs->media_changed = 1;
|
425 | b338082b | bellard | if (bs->change_cb)
|
426 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
427 | b338082b | bellard | |
428 | b338082b | bellard | return 0; |
429 | fc01f7e7 | bellard | } |
430 | fc01f7e7 | bellard | |
431 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
432 | fc01f7e7 | bellard | { |
433 | 19cb3738 | bellard | if (bs->drv) {
|
434 | ea2384d3 | bellard | if (bs->backing_hd)
|
435 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
436 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
437 | ea2384d3 | bellard | qemu_free(bs->opaque); |
438 | ea2384d3 | bellard | #ifdef _WIN32
|
439 | ea2384d3 | bellard | if (bs->is_temporary) {
|
440 | ea2384d3 | bellard | unlink(bs->filename); |
441 | ea2384d3 | bellard | } |
442 | 67b915a5 | bellard | #endif
|
443 | ea2384d3 | bellard | bs->opaque = NULL;
|
444 | ea2384d3 | bellard | bs->drv = NULL;
|
445 | b338082b | bellard | |
446 | b338082b | bellard | /* call the change callback */
|
447 | 19cb3738 | bellard | bs->media_changed = 1;
|
448 | b338082b | bellard | if (bs->change_cb)
|
449 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
450 | b338082b | bellard | } |
451 | b338082b | bellard | } |
452 | b338082b | bellard | |
453 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
454 | b338082b | bellard | { |
455 | 34c6f050 | aurel32 | BlockDriverState **pbs; |
456 | 34c6f050 | aurel32 | |
457 | 34c6f050 | aurel32 | pbs = &bdrv_first; |
458 | 34c6f050 | aurel32 | while (*pbs != bs && *pbs != NULL) |
459 | 34c6f050 | aurel32 | pbs = &(*pbs)->next; |
460 | 34c6f050 | aurel32 | if (*pbs == bs)
|
461 | 34c6f050 | aurel32 | *pbs = bs->next; |
462 | 34c6f050 | aurel32 | |
463 | b338082b | bellard | bdrv_close(bs); |
464 | b338082b | bellard | qemu_free(bs); |
465 | fc01f7e7 | bellard | } |
466 | fc01f7e7 | bellard | |
467 | 33e3963e | bellard | /* commit COW file into the raw image */
|
468 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
469 | 33e3963e | bellard | { |
470 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
471 | 83f64091 | bellard | int64_t i, total_sectors; |
472 | ea2384d3 | bellard | int n, j;
|
473 | ea2384d3 | bellard | unsigned char sector[512]; |
474 | 33e3963e | bellard | |
475 | 19cb3738 | bellard | if (!drv)
|
476 | 19cb3738 | bellard | return -ENOMEDIUM;
|
477 | 33e3963e | bellard | |
478 | 33e3963e | bellard | if (bs->read_only) {
|
479 | ea2384d3 | bellard | return -EACCES;
|
480 | 33e3963e | bellard | } |
481 | 33e3963e | bellard | |
482 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
483 | ea2384d3 | bellard | return -ENOTSUP;
|
484 | ea2384d3 | bellard | } |
485 | 33e3963e | bellard | |
486 | 83f64091 | bellard | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
487 | 83f64091 | bellard | for (i = 0; i < total_sectors;) { |
488 | 19cb3738 | bellard | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
489 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
490 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
491 | ea2384d3 | bellard | return -EIO;
|
492 | ea2384d3 | bellard | } |
493 | ea2384d3 | bellard | |
494 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
495 | ea2384d3 | bellard | return -EIO;
|
496 | ea2384d3 | bellard | } |
497 | ea2384d3 | bellard | i++; |
498 | 33e3963e | bellard | } |
499 | ea2384d3 | bellard | } else {
|
500 | ea2384d3 | bellard | i += n; |
501 | ea2384d3 | bellard | } |
502 | 33e3963e | bellard | } |
503 | 95389c86 | bellard | |
504 | 19cb3738 | bellard | if (drv->bdrv_make_empty)
|
505 | 19cb3738 | bellard | return drv->bdrv_make_empty(bs);
|
506 | 95389c86 | bellard | |
507 | 33e3963e | bellard | return 0; |
508 | 33e3963e | bellard | } |
509 | 33e3963e | bellard | |
510 | 19cb3738 | bellard | /* return < 0 if error. See bdrv_write() for the return codes */
|
511 | 5fafdf24 | ths | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
512 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
513 | fc01f7e7 | bellard | { |
514 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
515 | ea2384d3 | bellard | |
516 | 19cb3738 | bellard | if (!drv)
|
517 | 19cb3738 | bellard | return -ENOMEDIUM;
|
518 | b338082b | bellard | |
519 | 83f64091 | bellard | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
520 | cf98951b | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
521 | 83f64091 | bellard | sector_num++; |
522 | 83f64091 | bellard | nb_sectors--; |
523 | 83f64091 | bellard | buf += 512;
|
524 | 83f64091 | bellard | if (nb_sectors == 0) |
525 | 83f64091 | bellard | return 0; |
526 | 83f64091 | bellard | } |
527 | 83f64091 | bellard | if (drv->bdrv_pread) {
|
528 | 83f64091 | bellard | int ret, len;
|
529 | 83f64091 | bellard | len = nb_sectors * 512;
|
530 | 83f64091 | bellard | ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
|
531 | 83f64091 | bellard | if (ret < 0) |
532 | 83f64091 | bellard | return ret;
|
533 | 83f64091 | bellard | else if (ret != len) |
534 | 19cb3738 | bellard | return -EINVAL;
|
535 | a36e69dd | ths | else {
|
536 | a36e69dd | ths | bs->rd_bytes += (unsigned) len;
|
537 | a36e69dd | ths | bs->rd_ops ++; |
538 | 83f64091 | bellard | return 0; |
539 | a36e69dd | ths | } |
540 | 83f64091 | bellard | } else {
|
541 | 83f64091 | bellard | return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
|
542 | 33e3963e | bellard | } |
543 | fc01f7e7 | bellard | } |
544 | fc01f7e7 | bellard | |
545 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
546 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
547 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
548 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
549 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
550 | 19cb3738 | bellard | */
|
551 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
552 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
553 | fc01f7e7 | bellard | { |
554 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
555 | 19cb3738 | bellard | if (!bs->drv)
|
556 | 19cb3738 | bellard | return -ENOMEDIUM;
|
557 | 0849bf08 | bellard | if (bs->read_only)
|
558 | 19cb3738 | bellard | return -EACCES;
|
559 | 79639d42 | bellard | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
560 | 3b46e624 | ths | memcpy(bs->boot_sector_data, buf, 512);
|
561 | 79639d42 | bellard | } |
562 | 83f64091 | bellard | if (drv->bdrv_pwrite) {
|
563 | 83f64091 | bellard | int ret, len;
|
564 | 83f64091 | bellard | len = nb_sectors * 512;
|
565 | 83f64091 | bellard | ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
|
566 | 83f64091 | bellard | if (ret < 0) |
567 | 83f64091 | bellard | return ret;
|
568 | 83f64091 | bellard | else if (ret != len) |
569 | 83f64091 | bellard | return -EIO;
|
570 | a36e69dd | ths | else {
|
571 | a36e69dd | ths | bs->wr_bytes += (unsigned) len;
|
572 | a36e69dd | ths | bs->wr_ops ++; |
573 | 83f64091 | bellard | return 0; |
574 | a36e69dd | ths | } |
575 | 83f64091 | bellard | } else {
|
576 | 83f64091 | bellard | return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
577 | 83f64091 | bellard | } |
578 | 83f64091 | bellard | } |
579 | 83f64091 | bellard | |
580 | 5fafdf24 | ths | static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, |
581 | faea38e7 | bellard | uint8_t *buf, int count1)
|
582 | 83f64091 | bellard | { |
583 | 83f64091 | bellard | uint8_t tmp_buf[SECTOR_SIZE]; |
584 | 83f64091 | bellard | int len, nb_sectors, count;
|
585 | 83f64091 | bellard | int64_t sector_num; |
586 | 83f64091 | bellard | |
587 | 83f64091 | bellard | count = count1; |
588 | 83f64091 | bellard | /* first read to align to sector start */
|
589 | 83f64091 | bellard | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
|
590 | 83f64091 | bellard | if (len > count)
|
591 | 83f64091 | bellard | len = count; |
592 | 83f64091 | bellard | sector_num = offset >> SECTOR_BITS; |
593 | 83f64091 | bellard | if (len > 0) { |
594 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
595 | 83f64091 | bellard | return -EIO;
|
596 | 83f64091 | bellard | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
|
597 | 83f64091 | bellard | count -= len; |
598 | 83f64091 | bellard | if (count == 0) |
599 | 83f64091 | bellard | return count1;
|
600 | 83f64091 | bellard | sector_num++; |
601 | 83f64091 | bellard | buf += len; |
602 | 83f64091 | bellard | } |
603 | 83f64091 | bellard | |
604 | 83f64091 | bellard | /* read the sectors "in place" */
|
605 | 83f64091 | bellard | nb_sectors = count >> SECTOR_BITS; |
606 | 83f64091 | bellard | if (nb_sectors > 0) { |
607 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
608 | 83f64091 | bellard | return -EIO;
|
609 | 83f64091 | bellard | sector_num += nb_sectors; |
610 | 83f64091 | bellard | len = nb_sectors << SECTOR_BITS; |
611 | 83f64091 | bellard | buf += len; |
612 | 83f64091 | bellard | count -= len; |
613 | 83f64091 | bellard | } |
614 | 83f64091 | bellard | |
615 | 83f64091 | bellard | /* add data from the last sector */
|
616 | 83f64091 | bellard | if (count > 0) { |
617 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
618 | 83f64091 | bellard | return -EIO;
|
619 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
620 | 83f64091 | bellard | } |
621 | 83f64091 | bellard | return count1;
|
622 | 83f64091 | bellard | } |
623 | 83f64091 | bellard | |
624 | 5fafdf24 | ths | static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, |
625 | faea38e7 | bellard | const uint8_t *buf, int count1) |
626 | 83f64091 | bellard | { |
627 | 83f64091 | bellard | uint8_t tmp_buf[SECTOR_SIZE]; |
628 | 83f64091 | bellard | int len, nb_sectors, count;
|
629 | 83f64091 | bellard | int64_t sector_num; |
630 | 83f64091 | bellard | |
631 | 83f64091 | bellard | count = count1; |
632 | 83f64091 | bellard | /* first write to align to sector start */
|
633 | 83f64091 | bellard | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
|
634 | 83f64091 | bellard | if (len > count)
|
635 | 83f64091 | bellard | len = count; |
636 | 83f64091 | bellard | sector_num = offset >> SECTOR_BITS; |
637 | 83f64091 | bellard | if (len > 0) { |
638 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
639 | 83f64091 | bellard | return -EIO;
|
640 | 83f64091 | bellard | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
|
641 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
642 | 83f64091 | bellard | return -EIO;
|
643 | 83f64091 | bellard | count -= len; |
644 | 83f64091 | bellard | if (count == 0) |
645 | 83f64091 | bellard | return count1;
|
646 | 83f64091 | bellard | sector_num++; |
647 | 83f64091 | bellard | buf += len; |
648 | 83f64091 | bellard | } |
649 | 83f64091 | bellard | |
650 | 83f64091 | bellard | /* write the sectors "in place" */
|
651 | 83f64091 | bellard | nb_sectors = count >> SECTOR_BITS; |
652 | 83f64091 | bellard | if (nb_sectors > 0) { |
653 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
654 | 83f64091 | bellard | return -EIO;
|
655 | 83f64091 | bellard | sector_num += nb_sectors; |
656 | 83f64091 | bellard | len = nb_sectors << SECTOR_BITS; |
657 | 83f64091 | bellard | buf += len; |
658 | 83f64091 | bellard | count -= len; |
659 | 83f64091 | bellard | } |
660 | 83f64091 | bellard | |
661 | 83f64091 | bellard | /* add data from the last sector */
|
662 | 83f64091 | bellard | if (count > 0) { |
663 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
664 | 83f64091 | bellard | return -EIO;
|
665 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
666 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
667 | 83f64091 | bellard | return -EIO;
|
668 | 83f64091 | bellard | } |
669 | 83f64091 | bellard | return count1;
|
670 | 83f64091 | bellard | } |
671 | 83f64091 | bellard | |
672 | 83f64091 | bellard | /**
|
673 | 5fafdf24 | ths | * Read with byte offsets (needed only for file protocols)
|
674 | 83f64091 | bellard | */
|
675 | 5fafdf24 | ths | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
676 | 83f64091 | bellard | void *buf1, int count1) |
677 | 83f64091 | bellard | { |
678 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
679 | 83f64091 | bellard | |
680 | 83f64091 | bellard | if (!drv)
|
681 | 19cb3738 | bellard | return -ENOMEDIUM;
|
682 | 83f64091 | bellard | if (!drv->bdrv_pread)
|
683 | faea38e7 | bellard | return bdrv_pread_em(bs, offset, buf1, count1);
|
684 | 83f64091 | bellard | return drv->bdrv_pread(bs, offset, buf1, count1);
|
685 | 83f64091 | bellard | } |
686 | 83f64091 | bellard | |
687 | 5fafdf24 | ths | /**
|
688 | 5fafdf24 | ths | * Write with byte offsets (needed only for file protocols)
|
689 | 83f64091 | bellard | */
|
690 | 5fafdf24 | ths | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
691 | 83f64091 | bellard | const void *buf1, int count1) |
692 | 83f64091 | bellard | { |
693 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
694 | 83f64091 | bellard | |
695 | 83f64091 | bellard | if (!drv)
|
696 | 19cb3738 | bellard | return -ENOMEDIUM;
|
697 | 83f64091 | bellard | if (!drv->bdrv_pwrite)
|
698 | faea38e7 | bellard | return bdrv_pwrite_em(bs, offset, buf1, count1);
|
699 | 83f64091 | bellard | return drv->bdrv_pwrite(bs, offset, buf1, count1);
|
700 | 83f64091 | bellard | } |
701 | 83f64091 | bellard | |
702 | 83f64091 | bellard | /**
|
703 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
704 | 83f64091 | bellard | */
|
705 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
706 | 83f64091 | bellard | { |
707 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
708 | 83f64091 | bellard | if (!drv)
|
709 | 19cb3738 | bellard | return -ENOMEDIUM;
|
710 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
711 | 83f64091 | bellard | return -ENOTSUP;
|
712 | 83f64091 | bellard | return drv->bdrv_truncate(bs, offset);
|
713 | 83f64091 | bellard | } |
714 | 83f64091 | bellard | |
715 | 83f64091 | bellard | /**
|
716 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
717 | 83f64091 | bellard | */
|
718 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
719 | 83f64091 | bellard | { |
720 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
721 | 83f64091 | bellard | if (!drv)
|
722 | 19cb3738 | bellard | return -ENOMEDIUM;
|
723 | 83f64091 | bellard | if (!drv->bdrv_getlength) {
|
724 | 83f64091 | bellard | /* legacy mode */
|
725 | 83f64091 | bellard | return bs->total_sectors * SECTOR_SIZE;
|
726 | 83f64091 | bellard | } |
727 | 83f64091 | bellard | return drv->bdrv_getlength(bs);
|
728 | fc01f7e7 | bellard | } |
729 | fc01f7e7 | bellard | |
730 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
731 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
732 | fc01f7e7 | bellard | { |
733 | 19cb3738 | bellard | int64_t length; |
734 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
735 | 19cb3738 | bellard | if (length < 0) |
736 | 19cb3738 | bellard | length = 0;
|
737 | 19cb3738 | bellard | else
|
738 | 19cb3738 | bellard | length = length >> SECTOR_BITS; |
739 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
740 | fc01f7e7 | bellard | } |
741 | cf98951b | bellard | |
742 | cf98951b | bellard | /* force a given boot sector. */
|
743 | cf98951b | bellard | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
744 | cf98951b | bellard | { |
745 | cf98951b | bellard | bs->boot_sector_enabled = 1;
|
746 | cf98951b | bellard | if (size > 512) |
747 | cf98951b | bellard | size = 512;
|
748 | cf98951b | bellard | memcpy(bs->boot_sector_data, data, size); |
749 | cf98951b | bellard | memset(bs->boot_sector_data + size, 0, 512 - size); |
750 | cf98951b | bellard | } |
751 | b338082b | bellard | |
752 | 5fafdf24 | ths | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
753 | b338082b | bellard | int cyls, int heads, int secs) |
754 | b338082b | bellard | { |
755 | b338082b | bellard | bs->cyls = cyls; |
756 | b338082b | bellard | bs->heads = heads; |
757 | b338082b | bellard | bs->secs = secs; |
758 | b338082b | bellard | } |
759 | b338082b | bellard | |
760 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
761 | b338082b | bellard | { |
762 | b338082b | bellard | bs->type = type; |
763 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
764 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
765 | b338082b | bellard | } |
766 | b338082b | bellard | |
767 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
768 | 46d4767d | bellard | { |
769 | 46d4767d | bellard | bs->translation = translation; |
770 | 46d4767d | bellard | } |
771 | 46d4767d | bellard | |
772 | 5fafdf24 | ths | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
773 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
774 | b338082b | bellard | { |
775 | b338082b | bellard | *pcyls = bs->cyls; |
776 | b338082b | bellard | *pheads = bs->heads; |
777 | b338082b | bellard | *psecs = bs->secs; |
778 | b338082b | bellard | } |
779 | b338082b | bellard | |
780 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
781 | b338082b | bellard | { |
782 | b338082b | bellard | return bs->type;
|
783 | b338082b | bellard | } |
784 | b338082b | bellard | |
785 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
786 | 46d4767d | bellard | { |
787 | 46d4767d | bellard | return bs->translation;
|
788 | 46d4767d | bellard | } |
789 | 46d4767d | bellard | |
790 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
791 | b338082b | bellard | { |
792 | b338082b | bellard | return bs->removable;
|
793 | b338082b | bellard | } |
794 | b338082b | bellard | |
795 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
796 | b338082b | bellard | { |
797 | b338082b | bellard | return bs->read_only;
|
798 | b338082b | bellard | } |
799 | b338082b | bellard | |
800 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
801 | 985a03b0 | ths | { |
802 | 985a03b0 | ths | return bs->sg;
|
803 | 985a03b0 | ths | } |
804 | 985a03b0 | ths | |
805 | 19cb3738 | bellard | /* XXX: no longer used */
|
806 | 5fafdf24 | ths | void bdrv_set_change_cb(BlockDriverState *bs,
|
807 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
808 | b338082b | bellard | { |
809 | b338082b | bellard | bs->change_cb = change_cb; |
810 | b338082b | bellard | bs->change_opaque = opaque; |
811 | b338082b | bellard | } |
812 | b338082b | bellard | |
813 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
814 | ea2384d3 | bellard | { |
815 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
816 | ea2384d3 | bellard | return 1; |
817 | ea2384d3 | bellard | return bs->encrypted;
|
818 | ea2384d3 | bellard | } |
819 | ea2384d3 | bellard | |
820 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
821 | ea2384d3 | bellard | { |
822 | ea2384d3 | bellard | int ret;
|
823 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
824 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
825 | ea2384d3 | bellard | if (ret < 0) |
826 | ea2384d3 | bellard | return ret;
|
827 | ea2384d3 | bellard | if (!bs->encrypted)
|
828 | ea2384d3 | bellard | return 0; |
829 | ea2384d3 | bellard | } |
830 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
831 | ea2384d3 | bellard | return -1; |
832 | ea2384d3 | bellard | return bs->drv->bdrv_set_key(bs, key);
|
833 | ea2384d3 | bellard | } |
834 | ea2384d3 | bellard | |
835 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
836 | ea2384d3 | bellard | { |
837 | 19cb3738 | bellard | if (!bs->drv) {
|
838 | ea2384d3 | bellard | buf[0] = '\0'; |
839 | ea2384d3 | bellard | } else {
|
840 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
841 | ea2384d3 | bellard | } |
842 | ea2384d3 | bellard | } |
843 | ea2384d3 | bellard | |
844 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
845 | ea2384d3 | bellard | void *opaque)
|
846 | ea2384d3 | bellard | { |
847 | ea2384d3 | bellard | BlockDriver *drv; |
848 | ea2384d3 | bellard | |
849 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
850 | ea2384d3 | bellard | it(opaque, drv->format_name); |
851 | ea2384d3 | bellard | } |
852 | ea2384d3 | bellard | } |
853 | ea2384d3 | bellard | |
854 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
855 | b338082b | bellard | { |
856 | b338082b | bellard | BlockDriverState *bs; |
857 | b338082b | bellard | |
858 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
859 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
860 | b338082b | bellard | return bs;
|
861 | b338082b | bellard | } |
862 | b338082b | bellard | return NULL; |
863 | b338082b | bellard | } |
864 | b338082b | bellard | |
865 | 81d0912d | bellard | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
866 | 81d0912d | bellard | { |
867 | 81d0912d | bellard | BlockDriverState *bs; |
868 | 81d0912d | bellard | |
869 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
870 | 81d0912d | bellard | it(opaque, bs->device_name); |
871 | 81d0912d | bellard | } |
872 | 81d0912d | bellard | } |
873 | 81d0912d | bellard | |
874 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
875 | ea2384d3 | bellard | { |
876 | ea2384d3 | bellard | return bs->device_name;
|
877 | ea2384d3 | bellard | } |
878 | ea2384d3 | bellard | |
879 | 7a6cba61 | pbrook | void bdrv_flush(BlockDriverState *bs)
|
880 | 7a6cba61 | pbrook | { |
881 | 7a6cba61 | pbrook | if (bs->drv->bdrv_flush)
|
882 | 7a6cba61 | pbrook | bs->drv->bdrv_flush(bs); |
883 | 7a6cba61 | pbrook | if (bs->backing_hd)
|
884 | 7a6cba61 | pbrook | bdrv_flush(bs->backing_hd); |
885 | 7a6cba61 | pbrook | } |
886 | 7a6cba61 | pbrook | |
887 | faf07963 | pbrook | #ifndef QEMU_IMG
|
888 | b338082b | bellard | void bdrv_info(void) |
889 | b338082b | bellard | { |
890 | b338082b | bellard | BlockDriverState *bs; |
891 | b338082b | bellard | |
892 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
893 | b338082b | bellard | term_printf("%s:", bs->device_name);
|
894 | b338082b | bellard | term_printf(" type=");
|
895 | b338082b | bellard | switch(bs->type) {
|
896 | b338082b | bellard | case BDRV_TYPE_HD:
|
897 | b338082b | bellard | term_printf("hd");
|
898 | b338082b | bellard | break;
|
899 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
900 | b338082b | bellard | term_printf("cdrom");
|
901 | b338082b | bellard | break;
|
902 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
903 | b338082b | bellard | term_printf("floppy");
|
904 | b338082b | bellard | break;
|
905 | b338082b | bellard | } |
906 | b338082b | bellard | term_printf(" removable=%d", bs->removable);
|
907 | b338082b | bellard | if (bs->removable) {
|
908 | b338082b | bellard | term_printf(" locked=%d", bs->locked);
|
909 | b338082b | bellard | } |
910 | 19cb3738 | bellard | if (bs->drv) {
|
911 | fef30743 | ths | term_printf(" file=");
|
912 | fef30743 | ths | term_print_filename(bs->filename); |
913 | fef30743 | ths | if (bs->backing_file[0] != '\0') { |
914 | fef30743 | ths | term_printf(" backing_file=");
|
915 | fef30743 | ths | term_print_filename(bs->backing_file); |
916 | fef30743 | ths | } |
917 | b338082b | bellard | term_printf(" ro=%d", bs->read_only);
|
918 | ea2384d3 | bellard | term_printf(" drv=%s", bs->drv->format_name);
|
919 | ea2384d3 | bellard | if (bs->encrypted)
|
920 | ea2384d3 | bellard | term_printf(" encrypted");
|
921 | b338082b | bellard | } else {
|
922 | b338082b | bellard | term_printf(" [not inserted]");
|
923 | b338082b | bellard | } |
924 | b338082b | bellard | term_printf("\n");
|
925 | b338082b | bellard | } |
926 | b338082b | bellard | } |
927 | a36e69dd | ths | |
928 | a36e69dd | ths | /* The "info blockstats" command. */
|
929 | a36e69dd | ths | void bdrv_info_stats (void) |
930 | a36e69dd | ths | { |
931 | a36e69dd | ths | BlockDriverState *bs; |
932 | a36e69dd | ths | |
933 | a36e69dd | ths | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
934 | a36e69dd | ths | term_printf ("%s:"
|
935 | a36e69dd | ths | " rd_bytes=%" PRIu64
|
936 | a36e69dd | ths | " wr_bytes=%" PRIu64
|
937 | a36e69dd | ths | " rd_operations=%" PRIu64
|
938 | a36e69dd | ths | " wr_operations=%" PRIu64
|
939 | a36e69dd | ths | "\n",
|
940 | a36e69dd | ths | bs->device_name, |
941 | a36e69dd | ths | bs->rd_bytes, bs->wr_bytes, |
942 | a36e69dd | ths | bs->rd_ops, bs->wr_ops); |
943 | a36e69dd | ths | } |
944 | a36e69dd | ths | } |
945 | faf07963 | pbrook | #endif
|
946 | ea2384d3 | bellard | |
947 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
948 | 83f64091 | bellard | char *filename, int filename_size) |
949 | 83f64091 | bellard | { |
950 | 83f64091 | bellard | if (!bs->backing_hd) {
|
951 | 83f64091 | bellard | pstrcpy(filename, filename_size, "");
|
952 | 83f64091 | bellard | } else {
|
953 | 83f64091 | bellard | pstrcpy(filename, filename_size, bs->backing_file); |
954 | 83f64091 | bellard | } |
955 | 83f64091 | bellard | } |
956 | 83f64091 | bellard | |
957 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
958 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
959 | faea38e7 | bellard | { |
960 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
961 | faea38e7 | bellard | if (!drv)
|
962 | 19cb3738 | bellard | return -ENOMEDIUM;
|
963 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
964 | faea38e7 | bellard | return -ENOTSUP;
|
965 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
966 | faea38e7 | bellard | } |
967 | 3b46e624 | ths | |
968 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
969 | faea38e7 | bellard | { |
970 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
971 | faea38e7 | bellard | if (!drv)
|
972 | 19cb3738 | bellard | return -ENOMEDIUM;
|
973 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
974 | faea38e7 | bellard | return -ENOTSUP;
|
975 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
976 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
977 | faea38e7 | bellard | } |
978 | faea38e7 | bellard | |
979 | faea38e7 | bellard | /**************************************************************/
|
980 | faea38e7 | bellard | /* handling of snapshots */
|
981 | faea38e7 | bellard | |
982 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
983 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
984 | faea38e7 | bellard | { |
985 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
986 | faea38e7 | bellard | if (!drv)
|
987 | 19cb3738 | bellard | return -ENOMEDIUM;
|
988 | faea38e7 | bellard | if (!drv->bdrv_snapshot_create)
|
989 | faea38e7 | bellard | return -ENOTSUP;
|
990 | faea38e7 | bellard | return drv->bdrv_snapshot_create(bs, sn_info);
|
991 | faea38e7 | bellard | } |
992 | faea38e7 | bellard | |
993 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
994 | faea38e7 | bellard | const char *snapshot_id) |
995 | faea38e7 | bellard | { |
996 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
997 | faea38e7 | bellard | if (!drv)
|
998 | 19cb3738 | bellard | return -ENOMEDIUM;
|
999 | faea38e7 | bellard | if (!drv->bdrv_snapshot_goto)
|
1000 | faea38e7 | bellard | return -ENOTSUP;
|
1001 | faea38e7 | bellard | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
1002 | faea38e7 | bellard | } |
1003 | faea38e7 | bellard | |
1004 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
1005 | faea38e7 | bellard | { |
1006 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1007 | faea38e7 | bellard | if (!drv)
|
1008 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1009 | faea38e7 | bellard | if (!drv->bdrv_snapshot_delete)
|
1010 | faea38e7 | bellard | return -ENOTSUP;
|
1011 | faea38e7 | bellard | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
1012 | faea38e7 | bellard | } |
1013 | faea38e7 | bellard | |
1014 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
1015 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
1016 | faea38e7 | bellard | { |
1017 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1018 | faea38e7 | bellard | if (!drv)
|
1019 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1020 | faea38e7 | bellard | if (!drv->bdrv_snapshot_list)
|
1021 | faea38e7 | bellard | return -ENOTSUP;
|
1022 | faea38e7 | bellard | return drv->bdrv_snapshot_list(bs, psn_info);
|
1023 | faea38e7 | bellard | } |
1024 | faea38e7 | bellard | |
1025 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
1026 | faea38e7 | bellard | |
1027 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
1028 | faea38e7 | bellard | { |
1029 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
1030 | faea38e7 | bellard | int64_t base; |
1031 | faea38e7 | bellard | int i;
|
1032 | faea38e7 | bellard | |
1033 | faea38e7 | bellard | if (size <= 999) { |
1034 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
1035 | faea38e7 | bellard | } else {
|
1036 | faea38e7 | bellard | base = 1024;
|
1037 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
1038 | faea38e7 | bellard | if (size < (10 * base)) { |
1039 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
1040 | faea38e7 | bellard | (double)size / base,
|
1041 | faea38e7 | bellard | suffixes[i]); |
1042 | faea38e7 | bellard | break;
|
1043 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
1044 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
1045 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
1046 | faea38e7 | bellard | suffixes[i]); |
1047 | faea38e7 | bellard | break;
|
1048 | faea38e7 | bellard | } |
1049 | faea38e7 | bellard | base = base * 1024;
|
1050 | faea38e7 | bellard | } |
1051 | faea38e7 | bellard | } |
1052 | faea38e7 | bellard | return buf;
|
1053 | faea38e7 | bellard | } |
1054 | faea38e7 | bellard | |
1055 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
1056 | faea38e7 | bellard | { |
1057 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
1058 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1059 | 3b9f94e1 | bellard | struct tm *ptm;
|
1060 | 3b9f94e1 | bellard | #else
|
1061 | faea38e7 | bellard | struct tm tm;
|
1062 | 3b9f94e1 | bellard | #endif
|
1063 | faea38e7 | bellard | time_t ti; |
1064 | faea38e7 | bellard | int64_t secs; |
1065 | faea38e7 | bellard | |
1066 | faea38e7 | bellard | if (!sn) {
|
1067 | 5fafdf24 | ths | snprintf(buf, buf_size, |
1068 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1069 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1070 | faea38e7 | bellard | } else {
|
1071 | faea38e7 | bellard | ti = sn->date_sec; |
1072 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1073 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
1074 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
1075 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
1076 | 3b9f94e1 | bellard | #else
|
1077 | faea38e7 | bellard | localtime_r(&ti, &tm); |
1078 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
1079 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
1080 | 3b9f94e1 | bellard | #endif
|
1081 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
1082 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
1083 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
1084 | faea38e7 | bellard | (int)(secs / 3600), |
1085 | faea38e7 | bellard | (int)((secs / 60) % 60), |
1086 | 5fafdf24 | ths | (int)(secs % 60), |
1087 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1088 | faea38e7 | bellard | snprintf(buf, buf_size, |
1089 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1090 | faea38e7 | bellard | sn->id_str, sn->name, |
1091 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
1092 | faea38e7 | bellard | date_buf, |
1093 | faea38e7 | bellard | clock_buf); |
1094 | faea38e7 | bellard | } |
1095 | faea38e7 | bellard | return buf;
|
1096 | faea38e7 | bellard | } |
1097 | faea38e7 | bellard | |
1098 | 83f64091 | bellard | |
1099 | ea2384d3 | bellard | /**************************************************************/
|
1100 | 83f64091 | bellard | /* async I/Os */
|
1101 | ea2384d3 | bellard | |
1102 | ce1a14dc | pbrook | BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, |
1103 | ce1a14dc | pbrook | uint8_t *buf, int nb_sectors,
|
1104 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1105 | 83f64091 | bellard | { |
1106 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1107 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1108 | 83f64091 | bellard | |
1109 | 19cb3738 | bellard | if (!drv)
|
1110 | ce1a14dc | pbrook | return NULL; |
1111 | 3b46e624 | ths | |
1112 | 83f64091 | bellard | /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
|
1113 | 83f64091 | bellard | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
1114 | 83f64091 | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
1115 | 83f64091 | bellard | sector_num++; |
1116 | 83f64091 | bellard | nb_sectors--; |
1117 | 83f64091 | bellard | buf += 512;
|
1118 | 83f64091 | bellard | } |
1119 | 83f64091 | bellard | |
1120 | a36e69dd | ths | ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); |
1121 | a36e69dd | ths | |
1122 | a36e69dd | ths | if (ret) {
|
1123 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1124 | a36e69dd | ths | bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
|
1125 | a36e69dd | ths | bs->rd_ops ++; |
1126 | a36e69dd | ths | } |
1127 | a36e69dd | ths | |
1128 | a36e69dd | ths | return ret;
|
1129 | ea2384d3 | bellard | } |
1130 | ea2384d3 | bellard | |
1131 | ce1a14dc | pbrook | BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, |
1132 | ce1a14dc | pbrook | const uint8_t *buf, int nb_sectors, |
1133 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1134 | ea2384d3 | bellard | { |
1135 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1136 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1137 | ea2384d3 | bellard | |
1138 | 19cb3738 | bellard | if (!drv)
|
1139 | ce1a14dc | pbrook | return NULL; |
1140 | 83f64091 | bellard | if (bs->read_only)
|
1141 | ce1a14dc | pbrook | return NULL; |
1142 | 83f64091 | bellard | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
1143 | 3b46e624 | ths | memcpy(bs->boot_sector_data, buf, 512);
|
1144 | ea2384d3 | bellard | } |
1145 | 83f64091 | bellard | |
1146 | a36e69dd | ths | ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); |
1147 | a36e69dd | ths | |
1148 | a36e69dd | ths | if (ret) {
|
1149 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1150 | a36e69dd | ths | bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
|
1151 | a36e69dd | ths | bs->wr_ops ++; |
1152 | a36e69dd | ths | } |
1153 | a36e69dd | ths | |
1154 | a36e69dd | ths | return ret;
|
1155 | 83f64091 | bellard | } |
1156 | 83f64091 | bellard | |
1157 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
1158 | 83f64091 | bellard | { |
1159 | ce1a14dc | pbrook | BlockDriver *drv = acb->bs->drv; |
1160 | 83f64091 | bellard | |
1161 | ce1a14dc | pbrook | drv->bdrv_aio_cancel(acb); |
1162 | 83f64091 | bellard | } |
1163 | 83f64091 | bellard | |
1164 | ce1a14dc | pbrook | |
1165 | 83f64091 | bellard | /**************************************************************/
|
1166 | 83f64091 | bellard | /* async block device emulation */
|
1167 | 83f64091 | bellard | |
1168 | faf07963 | pbrook | #ifdef QEMU_IMG
|
1169 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
|
1170 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
1171 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1172 | ea2384d3 | bellard | { |
1173 | ea2384d3 | bellard | int ret;
|
1174 | ce1a14dc | pbrook | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
1175 | ce1a14dc | pbrook | cb(opaque, ret); |
1176 | ce1a14dc | pbrook | return NULL; |
1177 | ea2384d3 | bellard | } |
1178 | ea2384d3 | bellard | |
1179 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
|
1180 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
1181 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1182 | ea2384d3 | bellard | { |
1183 | ea2384d3 | bellard | int ret;
|
1184 | ce1a14dc | pbrook | ret = bdrv_write(bs, sector_num, buf, nb_sectors); |
1185 | ce1a14dc | pbrook | cb(opaque, ret); |
1186 | ce1a14dc | pbrook | return NULL; |
1187 | ea2384d3 | bellard | } |
1188 | ea2384d3 | bellard | |
1189 | 83f64091 | bellard | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) |
1190 | ea2384d3 | bellard | { |
1191 | ea2384d3 | bellard | } |
1192 | 83f64091 | bellard | #else
|
1193 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
1194 | 83f64091 | bellard | { |
1195 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
1196 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
1197 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1198 | 83f64091 | bellard | } |
1199 | beac80cd | bellard | |
1200 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
|
1201 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
1202 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1203 | 83f64091 | bellard | { |
1204 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
1205 | 83f64091 | bellard | int ret;
|
1206 | ce1a14dc | pbrook | |
1207 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
1208 | ce1a14dc | pbrook | if (!acb->bh)
|
1209 | ce1a14dc | pbrook | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
1210 | ce1a14dc | pbrook | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
1211 | ce1a14dc | pbrook | acb->ret = ret; |
1212 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
1213 | ce1a14dc | pbrook | return &acb->common;
|
1214 | beac80cd | bellard | } |
1215 | beac80cd | bellard | |
1216 | ce1a14dc | pbrook | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
|
1217 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
1218 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1219 | beac80cd | bellard | { |
1220 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
1221 | 83f64091 | bellard | int ret;
|
1222 | 83f64091 | bellard | |
1223 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
1224 | ce1a14dc | pbrook | if (!acb->bh)
|
1225 | ce1a14dc | pbrook | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
1226 | ce1a14dc | pbrook | ret = bdrv_write(bs, sector_num, buf, nb_sectors); |
1227 | ce1a14dc | pbrook | acb->ret = ret; |
1228 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
1229 | ce1a14dc | pbrook | return &acb->common;
|
1230 | beac80cd | bellard | } |
1231 | beac80cd | bellard | |
1232 | ce1a14dc | pbrook | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
1233 | ea2384d3 | bellard | { |
1234 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
1235 | ce1a14dc | pbrook | qemu_bh_cancel(acb->bh); |
1236 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1237 | 83f64091 | bellard | } |
1238 | faf07963 | pbrook | #endif /* !QEMU_IMG */ |
1239 | ea2384d3 | bellard | |
1240 | 83f64091 | bellard | /**************************************************************/
|
1241 | 83f64091 | bellard | /* sync block device emulation */
|
1242 | ea2384d3 | bellard | |
1243 | 83f64091 | bellard | static void bdrv_rw_em_cb(void *opaque, int ret) |
1244 | 83f64091 | bellard | { |
1245 | 83f64091 | bellard | *(int *)opaque = ret;
|
1246 | ea2384d3 | bellard | } |
1247 | ea2384d3 | bellard | |
1248 | 83f64091 | bellard | #define NOT_DONE 0x7fffffff |
1249 | 83f64091 | bellard | |
1250 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
1251 | 83f64091 | bellard | uint8_t *buf, int nb_sectors)
|
1252 | 7a6cba61 | pbrook | { |
1253 | ce1a14dc | pbrook | int async_ret;
|
1254 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1255 | 83f64091 | bellard | |
1256 | 83f64091 | bellard | async_ret = NOT_DONE; |
1257 | 83f64091 | bellard | qemu_aio_wait_start(); |
1258 | 5fafdf24 | ths | acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, |
1259 | 83f64091 | bellard | bdrv_rw_em_cb, &async_ret); |
1260 | ce1a14dc | pbrook | if (acb == NULL) { |
1261 | 83f64091 | bellard | qemu_aio_wait_end(); |
1262 | ce1a14dc | pbrook | return -1; |
1263 | 83f64091 | bellard | } |
1264 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1265 | 83f64091 | bellard | qemu_aio_wait(); |
1266 | 83f64091 | bellard | } |
1267 | 83f64091 | bellard | qemu_aio_wait_end(); |
1268 | 83f64091 | bellard | return async_ret;
|
1269 | 7a6cba61 | pbrook | } |
1270 | 7a6cba61 | pbrook | |
1271 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1272 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors) |
1273 | 83f64091 | bellard | { |
1274 | ce1a14dc | pbrook | int async_ret;
|
1275 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1276 | 83f64091 | bellard | |
1277 | 83f64091 | bellard | async_ret = NOT_DONE; |
1278 | 83f64091 | bellard | qemu_aio_wait_start(); |
1279 | 5fafdf24 | ths | acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, |
1280 | 83f64091 | bellard | bdrv_rw_em_cb, &async_ret); |
1281 | ce1a14dc | pbrook | if (acb == NULL) { |
1282 | 83f64091 | bellard | qemu_aio_wait_end(); |
1283 | ce1a14dc | pbrook | return -1; |
1284 | 83f64091 | bellard | } |
1285 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1286 | 83f64091 | bellard | qemu_aio_wait(); |
1287 | 83f64091 | bellard | } |
1288 | 83f64091 | bellard | qemu_aio_wait_end(); |
1289 | 83f64091 | bellard | return async_ret;
|
1290 | 83f64091 | bellard | } |
1291 | ea2384d3 | bellard | |
1292 | ea2384d3 | bellard | void bdrv_init(void) |
1293 | ea2384d3 | bellard | { |
1294 | ea2384d3 | bellard | bdrv_register(&bdrv_raw); |
1295 | 19cb3738 | bellard | bdrv_register(&bdrv_host_device); |
1296 | ea2384d3 | bellard | #ifndef _WIN32
|
1297 | ea2384d3 | bellard | bdrv_register(&bdrv_cow); |
1298 | ea2384d3 | bellard | #endif
|
1299 | ea2384d3 | bellard | bdrv_register(&bdrv_qcow); |
1300 | ea2384d3 | bellard | bdrv_register(&bdrv_vmdk); |
1301 | 3c56521b | bellard | bdrv_register(&bdrv_cloop); |
1302 | 585d0ed9 | bellard | bdrv_register(&bdrv_dmg); |
1303 | a8753c34 | bellard | bdrv_register(&bdrv_bochs); |
1304 | 6a0f9e82 | bellard | bdrv_register(&bdrv_vpc); |
1305 | 712e7874 | bellard | bdrv_register(&bdrv_vvfat); |
1306 | faea38e7 | bellard | bdrv_register(&bdrv_qcow2); |
1307 | 6ada7453 | ths | bdrv_register(&bdrv_parallels); |
1308 | ea2384d3 | bellard | } |
1309 | ce1a14dc | pbrook | |
1310 | ce1a14dc | pbrook | void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
|
1311 | ce1a14dc | pbrook | void *opaque)
|
1312 | ce1a14dc | pbrook | { |
1313 | ce1a14dc | pbrook | BlockDriver *drv; |
1314 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1315 | ce1a14dc | pbrook | |
1316 | ce1a14dc | pbrook | drv = bs->drv; |
1317 | ce1a14dc | pbrook | if (drv->free_aiocb) {
|
1318 | ce1a14dc | pbrook | acb = drv->free_aiocb; |
1319 | ce1a14dc | pbrook | drv->free_aiocb = acb->next; |
1320 | ce1a14dc | pbrook | } else {
|
1321 | ce1a14dc | pbrook | acb = qemu_mallocz(drv->aiocb_size); |
1322 | ce1a14dc | pbrook | if (!acb)
|
1323 | ce1a14dc | pbrook | return NULL; |
1324 | ce1a14dc | pbrook | } |
1325 | ce1a14dc | pbrook | acb->bs = bs; |
1326 | ce1a14dc | pbrook | acb->cb = cb; |
1327 | ce1a14dc | pbrook | acb->opaque = opaque; |
1328 | ce1a14dc | pbrook | return acb;
|
1329 | ce1a14dc | pbrook | } |
1330 | ce1a14dc | pbrook | |
1331 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
1332 | ce1a14dc | pbrook | { |
1333 | ce1a14dc | pbrook | BlockDriverAIOCB *acb = p; |
1334 | ce1a14dc | pbrook | BlockDriver *drv = acb->bs->drv; |
1335 | ce1a14dc | pbrook | acb->next = drv->free_aiocb; |
1336 | ce1a14dc | pbrook | drv->free_aiocb = acb; |
1337 | ce1a14dc | pbrook | } |
1338 | 19cb3738 | bellard | |
1339 | 19cb3738 | bellard | /**************************************************************/
|
1340 | 19cb3738 | bellard | /* removable device support */
|
1341 | 19cb3738 | bellard | |
1342 | 19cb3738 | bellard | /**
|
1343 | 19cb3738 | bellard | * Return TRUE if the media is present
|
1344 | 19cb3738 | bellard | */
|
1345 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
1346 | 19cb3738 | bellard | { |
1347 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1348 | 19cb3738 | bellard | int ret;
|
1349 | 19cb3738 | bellard | if (!drv)
|
1350 | 19cb3738 | bellard | return 0; |
1351 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
1352 | 19cb3738 | bellard | return 1; |
1353 | 19cb3738 | bellard | ret = drv->bdrv_is_inserted(bs); |
1354 | 19cb3738 | bellard | return ret;
|
1355 | 19cb3738 | bellard | } |
1356 | 19cb3738 | bellard | |
1357 | 19cb3738 | bellard | /**
|
1358 | 19cb3738 | bellard | * Return TRUE if the media changed since the last call to this
|
1359 | 5fafdf24 | ths | * function. It is currently only used for floppy disks
|
1360 | 19cb3738 | bellard | */
|
1361 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
1362 | 19cb3738 | bellard | { |
1363 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1364 | 19cb3738 | bellard | int ret;
|
1365 | 19cb3738 | bellard | |
1366 | 19cb3738 | bellard | if (!drv || !drv->bdrv_media_changed)
|
1367 | 19cb3738 | bellard | ret = -ENOTSUP; |
1368 | 19cb3738 | bellard | else
|
1369 | 19cb3738 | bellard | ret = drv->bdrv_media_changed(bs); |
1370 | 19cb3738 | bellard | if (ret == -ENOTSUP)
|
1371 | 19cb3738 | bellard | ret = bs->media_changed; |
1372 | 19cb3738 | bellard | bs->media_changed = 0;
|
1373 | 19cb3738 | bellard | return ret;
|
1374 | 19cb3738 | bellard | } |
1375 | 19cb3738 | bellard | |
1376 | 19cb3738 | bellard | /**
|
1377 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
1378 | 19cb3738 | bellard | */
|
1379 | 19cb3738 | bellard | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
1380 | 19cb3738 | bellard | { |
1381 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1382 | 19cb3738 | bellard | int ret;
|
1383 | 19cb3738 | bellard | |
1384 | 19cb3738 | bellard | if (!drv || !drv->bdrv_eject) {
|
1385 | 19cb3738 | bellard | ret = -ENOTSUP; |
1386 | 19cb3738 | bellard | } else {
|
1387 | 19cb3738 | bellard | ret = drv->bdrv_eject(bs, eject_flag); |
1388 | 19cb3738 | bellard | } |
1389 | 19cb3738 | bellard | if (ret == -ENOTSUP) {
|
1390 | 19cb3738 | bellard | if (eject_flag)
|
1391 | 19cb3738 | bellard | bdrv_close(bs); |
1392 | 19cb3738 | bellard | } |
1393 | 19cb3738 | bellard | } |
1394 | 19cb3738 | bellard | |
1395 | 19cb3738 | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
1396 | 19cb3738 | bellard | { |
1397 | 19cb3738 | bellard | return bs->locked;
|
1398 | 19cb3738 | bellard | } |
1399 | 19cb3738 | bellard | |
1400 | 19cb3738 | bellard | /**
|
1401 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
1402 | 19cb3738 | bellard | * to eject it manually).
|
1403 | 19cb3738 | bellard | */
|
1404 | 19cb3738 | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
1405 | 19cb3738 | bellard | { |
1406 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1407 | 19cb3738 | bellard | |
1408 | 19cb3738 | bellard | bs->locked = locked; |
1409 | 19cb3738 | bellard | if (drv && drv->bdrv_set_locked) {
|
1410 | 19cb3738 | bellard | drv->bdrv_set_locked(bs, locked); |
1411 | 19cb3738 | bellard | } |
1412 | 19cb3738 | bellard | } |
1413 | 985a03b0 | ths | |
1414 | 985a03b0 | ths | /* needed for generic scsi interface */
|
1415 | 985a03b0 | ths | |
1416 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1417 | 985a03b0 | ths | { |
1418 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
1419 | 985a03b0 | ths | |
1420 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
1421 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
1422 | 985a03b0 | ths | return -ENOTSUP;
|
1423 | 985a03b0 | ths | } |