debuggers.hg

view tools/blktap2/drivers/block-qcow.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 9009f9f76441
children
line source
1 /* block-qcow.c
2 *
3 * Asynchronous Qemu copy-on-write disk implementation.
4 * Code based on the Qemu implementation
5 * (see copyright notice below)
6 *
7 * (c) 2006 Andrew Warfield and Julian Chesterfield
8 *
9 */
11 /*
12 * Block driver for the QCOW format
13 *
14 * Copyright (c) 2004 Fabrice Bellard
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this software and associated documentation files(the "Software"), to deal
18 * in the Software without restriction, including without limitation the rights
19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 * copies of the Software, and to permit persons to whom the Software is
21 * furnished to do so, subject to the following conditions:
22 */
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/statvfs.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <string.h>
33 #include <zlib.h>
34 #include <inttypes.h>
35 #include <libaio.h>
36 #include <limits.h>
37 #include "bswap.h"
38 #include "aes.h"
39 #include "md5.h"
41 #include "tapdisk.h"
42 #include "tapdisk-driver.h"
43 #include "tapdisk-interface.h"
44 #include "tapdisk-disktype.h"
45 #include "qcow.h"
46 #include "blk.h"
47 #include "atomicio.h"
49 /* *BSD has no O_LARGEFILE */
50 #ifndef O_LARGEFILE
51 #define O_LARGEFILE 0
52 #endif
54 #if 1
55 #define ASSERT(_p) \
56 if ( !(_p) ) { DPRINTF("Assertion '%s' failed, line %d, file %s", #_p , \
57 __LINE__, __FILE__); *(int*)0=0; }
58 #else
59 #define ASSERT(_p) ((void)0)
60 #endif
62 struct pending_aio {
63 td_callback_t cb;
64 int id;
65 void *private;
66 int nb_sectors;
67 char *buf;
68 uint64_t sector;
69 };
71 #undef IOCB_IDX
72 #define IOCB_IDX(_s, _io) ((_io) - (_s)->iocb_list)
74 #define ZERO_TEST(_b) (_b | 0x00)
76 struct qcow_request {
77 td_request_t treq;
78 struct tiocb tiocb;
79 struct tdqcow_state *state;
80 };
82 static int decompress_cluster(struct tdqcow_state *s, uint64_t cluster_offset);
84 uint32_t gen_cksum(char *ptr, int len)
85 {
86 int i;
87 uint32_t md[4];
89 /* Generate checksum */
90 md5_sum((const uint8_t*)ptr, len, (uint8_t*)md);
92 return md[0];
93 }
95 static void free_aio_state(struct tdqcow_state* s)
96 {
97 free(s->aio_requests);
98 free(s->aio_free_list);
99 }
101 static int init_aio_state(td_driver_t *driver)
102 {
103 int i, ret;
104 td_disk_info_t *bs = &(driver->info);
105 struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
107 // A segment (i.e. a page) can span multiple clusters
108 s->max_aio_reqs = ((getpagesize() / s->cluster_size) + 1) *
109 MAX_SEGMENTS_PER_REQ * MAX_REQUESTS;
111 s->aio_free_count = s->max_aio_reqs;
113 if (!(s->aio_requests = calloc(s->max_aio_reqs, sizeof(struct qcow_request))) ||
114 !(s->aio_free_list = calloc(s->max_aio_reqs, sizeof(struct qcow_request)))) {
115 DPRINTF("Failed to allocate AIO structs (max_aio_reqs = %d)\n",
116 s->max_aio_reqs);
117 goto fail;
118 }
120 for (i = 0; i < s->max_aio_reqs; i++)
121 s->aio_free_list[i] = &s->aio_requests[i];
123 DPRINTF("AIO state initialised\n");
125 return 0;
126 fail:
127 return -1;
128 }
130 int get_filesize(char *filename, uint64_t *size, struct stat *st)
131 {
132 int fd;
133 QCowHeader header;
135 /*Set to the backing file size*/
136 fd = open(filename, O_RDONLY);
137 if (fd < 0)
138 return -1;
139 if (read(fd, &header, sizeof(header)) < sizeof(header)) {
140 close(fd);
141 return -1;
142 }
143 close(fd);
145 be32_to_cpus(&header.magic);
146 be64_to_cpus(&header.size);
147 if (header.magic == QCOW_MAGIC) {
148 *size = header.size >> SECTOR_SHIFT;
149 return 0;
150 }
152 if(S_ISBLK(st->st_mode)) {
153 fd = open(filename, O_RDONLY);
154 if (fd < 0)
155 return -1;
156 if (blk_getimagesize(fd, size) != 0) {
157 printf("Unable to get Block device size\n");
158 close(fd);
159 return -1;
160 }
161 close(fd);
162 } else *size = (st->st_size >> SECTOR_SHIFT);
163 return 0;
164 }
166 static int qcow_set_key(struct tdqcow_state *s, const char *key)
167 {
168 uint8_t keybuf[16];
169 int len, i;
171 memset(keybuf, 0, 16);
172 len = strlen(key);
173 if (len > 16)
174 len = 16;
175 /* XXX: we could compress the chars to 7 bits to increase
176 entropy */
177 for (i = 0; i < len; i++) {
178 keybuf[i] = key[i];
179 }
180 s->crypt_method = s->crypt_method_header;
182 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
183 return -1;
184 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
185 return -1;
186 #if 0
187 /* test */
188 {
189 uint8_t in[16];
190 uint8_t out[16];
191 uint8_t tmp[16];
192 for (i=0; i<16; i++)
193 in[i] = i;
194 AES_encrypt(in, tmp, &s->aes_encrypt_key);
195 AES_decrypt(tmp, out, &s->aes_decrypt_key);
196 for (i = 0; i < 16; i++)
197 DPRINTF(" %02x", tmp[i]);
198 DPRINTF("\n");
199 for (i = 0; i < 16; i++)
200 DPRINTF(" %02x", out[i]);
201 DPRINTF("\n");
202 }
203 #endif
204 return 0;
205 }
207 void tdqcow_complete(void *arg, struct tiocb *tiocb, int err)
208 {
209 struct qcow_request *aio = (struct qcow_request *)arg;
210 struct tdqcow_state *s = aio->state;
212 td_complete_request(aio->treq, err);
214 s->aio_free_list[s->aio_free_count++] = aio;
215 }
217 static void async_read(td_driver_t *driver, td_request_t treq)
218 {
219 int size;
220 uint64_t offset;
221 struct qcow_request *aio;
222 struct tdqcow_state *prv;
224 prv = (struct tdqcow_state *)driver->data;
225 size = treq.secs * driver->info.sector_size;
226 offset = treq.sec * (uint64_t)driver->info.sector_size;
228 if (prv->aio_free_count == 0)
229 goto fail;
231 aio = prv->aio_free_list[--prv->aio_free_count];
232 aio->treq = treq;
233 aio->state = prv;
235 td_prep_read(&aio->tiocb, prv->fd, treq.buf,
236 size, offset, tdqcow_complete, aio);
237 td_queue_tiocb(driver, &aio->tiocb);
239 return;
241 fail:
242 td_complete_request(treq, -EBUSY);
243 }
245 static void async_write(td_driver_t *driver, td_request_t treq)
246 {
247 int size;
248 uint64_t offset;
249 struct qcow_request *aio;
250 struct tdqcow_state *prv;
252 prv = (struct tdqcow_state *)driver->data;
253 size = treq.secs * driver->info.sector_size;
254 offset = treq.sec * (uint64_t)driver->info.sector_size;
256 if (prv->aio_free_count == 0)
257 goto fail;
259 aio = prv->aio_free_list[--prv->aio_free_count];
260 aio->treq = treq;
261 aio->state = prv;
263 td_prep_write(&aio->tiocb, prv->fd, treq.buf,
264 size, offset, tdqcow_complete, aio);
265 td_queue_tiocb(driver, &aio->tiocb);
267 return;
269 fail:
270 td_complete_request(treq, -EBUSY);
271 }
273 /*
274 * The crypt function is compatible with the linux cryptoloop
275 * algorithm for < 4 GB images. NOTE: out_buf == in_buf is
276 * supported .
277 */
278 static void encrypt_sectors(struct tdqcow_state *s, int64_t sector_num,
279 uint8_t *out_buf, const uint8_t *in_buf,
280 int nb_sectors, int enc,
281 const AES_KEY *key)
282 {
283 union {
284 uint64_t ll[2];
285 uint8_t b[16];
286 } ivec;
287 int i;
289 for (i = 0; i < nb_sectors; i++) {
290 ivec.ll[0] = cpu_to_le64(sector_num);
291 ivec.ll[1] = 0;
292 AES_cbc_encrypt(in_buf, out_buf, 512, key,
293 ivec.b, enc);
294 sector_num++;
295 in_buf += 512;
296 out_buf += 512;
297 }
298 }
300 int qtruncate(int fd, off_t length, int sparse)
301 {
302 int ret, i;
303 int current = 0, rem = 0;
304 uint64_t sectors;
305 struct stat st;
306 char *buf;
308 /* If length is greater than the current file len
309 * we synchronously write zeroes to the end of the
310 * file, otherwise we truncate the length down
311 */
312 ret = fstat(fd, &st);
313 if (ret == -1)
314 return -1;
315 if (S_ISBLK(st.st_mode))
316 return 0;
318 sectors = (length + DEFAULT_SECTOR_SIZE - 1)/DEFAULT_SECTOR_SIZE;
319 current = (st.st_size + DEFAULT_SECTOR_SIZE - 1)/DEFAULT_SECTOR_SIZE;
320 rem = st.st_size % DEFAULT_SECTOR_SIZE;
322 /* If we are extending this file, we write zeros to the end --
323 * this tries to ensure that the extents allocated wind up being
324 * contiguous on disk.
325 */
326 if(st.st_size < sectors * DEFAULT_SECTOR_SIZE) {
327 /*We are extending the file*/
328 if ((ret = posix_memalign((void **)&buf,
329 512, DEFAULT_SECTOR_SIZE))) {
330 DPRINTF("posix_memalign failed: %d\n", ret);
331 return -1;
332 }
333 memset(buf, 0x00, DEFAULT_SECTOR_SIZE);
334 if (lseek(fd, 0, SEEK_END)==-1) {
335 DPRINTF("Lseek EOF failed (%d), internal error\n",
336 errno);
337 free(buf);
338 return -1;
339 }
340 if (rem) {
341 ret = write(fd, buf, rem);
342 if (ret != rem) {
343 DPRINTF("write failed: ret = %d, err = %s\n",
344 ret, strerror(errno));
345 free(buf);
346 return -1;
347 }
348 }
349 for (i = current; i < sectors; i++ ) {
350 ret = write(fd, buf, DEFAULT_SECTOR_SIZE);
351 if (ret != DEFAULT_SECTOR_SIZE) {
352 DPRINTF("write failed: ret = %d, err = %s\n",
353 ret, strerror(errno));
354 free(buf);
355 return -1;
356 }
357 }
358 free(buf);
359 } else if(sparse && (st.st_size > sectors * DEFAULT_SECTOR_SIZE))
360 if (ftruncate(fd, (off_t)sectors * DEFAULT_SECTOR_SIZE)==-1) {
361 DPRINTF("Ftruncate failed (%s)\n", strerror(errno));
362 return -1;
363 }
364 return 0;
365 }
367 /* 'allocate' is:
368 *
369 * 0 to not allocate.
370 *
371 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
372 * 'n_end')
373 *
374 * 2 to allocate a compressed cluster of size
375 * 'compressed_size'. 'compressed_size' must be > 0 and <
376 * cluster_size
377 *
378 * return 0 if not allocated.
379 */
380 static uint64_t get_cluster_offset(struct tdqcow_state *s,
381 uint64_t offset, int allocate,
382 int compressed_size,
383 int n_start, int n_end)
384 {
385 int min_index, i, j, l1_index, l2_index, l2_sector, l1_sector;
386 char *tmp_ptr2, *l2_ptr, *l1_ptr;
387 uint64_t *tmp_ptr;
388 uint64_t l2_offset, *l2_table, cluster_offset, tmp;
389 uint32_t min_count;
390 int new_l2_table;
392 /*Check L1 table for the extent offset*/
393 l1_index = offset >> (s->l2_bits + s->cluster_bits);
394 l2_offset = s->l1_table[l1_index];
395 new_l2_table = 0;
396 if (!l2_offset) {
397 if (!allocate)
398 return 0;
399 /*
400 * allocating a new l2 entry + extent
401 * at the end of the file, we must also
402 * update the L1 entry safely.
403 */
404 l2_offset = s->fd_end;
406 /* round to cluster size */
407 l2_offset = (l2_offset + s->cluster_size - 1)
408 & ~(s->cluster_size - 1);
410 /* update the L1 entry */
411 s->l1_table[l1_index] = l2_offset;
413 /*Truncate file for L2 table
414 *(initialised to zero in case we crash)*/
415 if (qtruncate(s->fd,
416 l2_offset + (s->l2_size * sizeof(uint64_t)),
417 s->sparse) != 0) {
418 DPRINTF("ERROR truncating file\n");
419 return 0;
420 }
421 s->fd_end = l2_offset + (s->l2_size * sizeof(uint64_t));
423 /*Update the L1 table entry on disk
424 * (for O_DIRECT we write 4KByte blocks)*/
425 l1_sector = (l1_index * sizeof(uint64_t)) >> 12;
426 l1_ptr = (char *)s->l1_table + (l1_sector << 12);
428 if (posix_memalign((void **)&tmp_ptr, 4096, 4096) != 0) {
429 DPRINTF("ERROR allocating memory for L1 table\n");
430 }
431 memcpy(tmp_ptr, l1_ptr, 4096);
433 /* Convert block to write to big endian */
434 for(i = 0; i < 4096 / sizeof(uint64_t); i++) {
435 cpu_to_be64s(&tmp_ptr[i]);
436 }
438 /*
439 * Issue non-asynchronous L1 write.
440 * For safety, we must ensure that
441 * entry is written before blocks.
442 */
443 lseek(s->fd, s->l1_table_offset + (l1_sector << 12), SEEK_SET);
444 if (write(s->fd, tmp_ptr, 4096) != 4096) {
445 free(tmp_ptr);
446 return 0;
447 }
448 free(tmp_ptr);
450 new_l2_table = 1;
451 goto cache_miss;
452 } else if (s->min_cluster_alloc == s->l2_size) {
453 /*Fast-track the request*/
454 cluster_offset = l2_offset + (s->l2_size * sizeof(uint64_t));
455 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
456 return cluster_offset + (l2_index * s->cluster_size);
457 }
459 /*Check to see if L2 entry is already cached*/
460 for (i = 0; i < L2_CACHE_SIZE; i++) {
461 if (l2_offset == s->l2_cache_offsets[i]) {
462 /* increment the hit count */
463 if (++s->l2_cache_counts[i] == 0xffffffff) {
464 for (j = 0; j < L2_CACHE_SIZE; j++) {
465 s->l2_cache_counts[j] >>= 1;
466 }
467 }
468 l2_table = s->l2_cache + (i << s->l2_bits);
469 goto found;
470 }
471 }
473 cache_miss:
474 /* not found: load a new entry in the least used one */
475 min_index = 0;
476 min_count = 0xffffffff;
477 for (i = 0; i < L2_CACHE_SIZE; i++) {
478 if (s->l2_cache_counts[i] < min_count) {
479 min_count = s->l2_cache_counts[i];
480 min_index = i;
481 }
482 }
483 l2_table = s->l2_cache + (min_index << s->l2_bits);
485 /*If extent pre-allocated, read table from disk,
486 *otherwise write new table to disk*/
487 if (new_l2_table) {
488 /*Should we allocate the whole extent? Adjustable parameter.*/
489 if (s->cluster_alloc == s->l2_size) {
490 cluster_offset = l2_offset +
491 (s->l2_size * sizeof(uint64_t));
492 cluster_offset = (cluster_offset + s->cluster_size - 1)
493 & ~(s->cluster_size - 1);
494 if (qtruncate(s->fd, cluster_offset +
495 (s->cluster_size * s->l2_size),
496 s->sparse) != 0) {
497 DPRINTF("ERROR truncating file\n");
498 return 0;
499 }
500 s->fd_end = cluster_offset +
501 (s->cluster_size * s->l2_size);
502 for (i = 0; i < s->l2_size; i++) {
503 l2_table[i] = cpu_to_be64(cluster_offset +
504 (i*s->cluster_size));
505 }
506 } else memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
508 lseek(s->fd, l2_offset, SEEK_SET);
509 if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
510 s->l2_size * sizeof(uint64_t))
511 return 0;
512 } else {
513 lseek(s->fd, l2_offset, SEEK_SET);
514 if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
515 s->l2_size * sizeof(uint64_t))
516 return 0;
517 }
519 /*Update the cache entries*/
520 s->l2_cache_offsets[min_index] = l2_offset;
521 s->l2_cache_counts[min_index] = 1;
523 found:
524 /*The extent is split into 's->l2_size' blocks of
525 *size 's->cluster_size'*/
526 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
527 cluster_offset = be64_to_cpu(l2_table[l2_index]);
529 if (!cluster_offset ||
530 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1) ) {
531 if (!allocate)
532 return 0;
534 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
535 (n_end - n_start) < s->cluster_sectors) {
536 /* cluster is already allocated but compressed, we must
537 decompress it in the case it is not completely
538 overwritten */
539 if (decompress_cluster(s, cluster_offset) < 0)
540 return 0;
541 cluster_offset = lseek(s->fd, s->fd_end, SEEK_SET);
542 cluster_offset = (cluster_offset + s->cluster_size - 1)
543 & ~(s->cluster_size - 1);
544 /* write the cluster content - not asynchronous */
545 lseek(s->fd, cluster_offset, SEEK_SET);
546 if (write(s->fd, s->cluster_cache, s->cluster_size) !=
547 s->cluster_size)
548 return -1;
549 } else {
550 /* allocate a new cluster */
551 cluster_offset = lseek(s->fd, s->fd_end, SEEK_SET);
552 if (allocate == 1) {
553 /* round to cluster size */
554 cluster_offset =
555 (cluster_offset + s->cluster_size - 1)
556 & ~(s->cluster_size - 1);
557 if (qtruncate(s->fd, cluster_offset +
558 s->cluster_size, s->sparse)!=0) {
559 DPRINTF("ERROR truncating file\n");
560 return 0;
561 }
562 s->fd_end = (cluster_offset + s->cluster_size);
563 /* if encrypted, we must initialize the cluster
564 content which won't be written */
565 if (s->crypt_method &&
566 (n_end - n_start) < s->cluster_sectors) {
567 uint64_t start_sect;
568 start_sect = (offset &
569 ~(s->cluster_size - 1))
570 >> 9;
571 memset(s->cluster_data + 512,
572 0xaa, 512);
573 for (i = 0; i < s->cluster_sectors;i++)
574 {
575 if (i < n_start || i >= n_end)
576 {
577 encrypt_sectors(s, start_sect + i,
578 s->cluster_data,
579 s->cluster_data + 512, 1, 1,
580 &s->aes_encrypt_key);
581 lseek(s->fd, cluster_offset + i * 512, SEEK_SET);
582 if (write(s->fd, s->cluster_data, 512) != 512)
583 return -1;
584 }
585 }
586 }
587 } else {
588 cluster_offset |= QCOW_OFLAG_COMPRESSED |
589 (uint64_t)compressed_size
590 << (63 - s->cluster_bits);
591 }
592 }
593 /* update L2 table */
594 tmp = cpu_to_be64(cluster_offset);
595 l2_table[l2_index] = tmp;
597 /*For IO_DIRECT we write 4KByte blocks*/
598 l2_sector = (l2_index * sizeof(uint64_t)) >> 12;
599 l2_ptr = (char *)l2_table + (l2_sector << 12);
601 if (posix_memalign((void **)&tmp_ptr2, 4096, 4096) != 0) {
602 DPRINTF("ERROR allocating memory for L1 table\n");
603 }
604 memcpy(tmp_ptr2, l2_ptr, 4096);
605 lseek(s->fd, l2_offset + (l2_sector << 12), SEEK_SET);
606 if (write(s->fd, tmp_ptr2, 4096) != 4096) {
607 free(tmp_ptr2);
608 return -1;
609 }
610 free(tmp_ptr2);
611 }
612 return cluster_offset;
613 }
615 static int qcow_is_allocated(struct tdqcow_state *s, int64_t sector_num,
616 int nb_sectors, int *pnum)
617 {
618 int index_in_cluster, n;
619 uint64_t cluster_offset;
621 cluster_offset = get_cluster_offset(s, sector_num << 9, 0, 0, 0, 0);
622 index_in_cluster = sector_num & (s->cluster_sectors - 1);
623 n = s->cluster_sectors - index_in_cluster;
624 if (n > nb_sectors)
625 n = nb_sectors;
626 *pnum = n;
627 return (cluster_offset != 0);
628 }
630 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
631 const uint8_t *buf, int buf_size)
632 {
633 z_stream strm1, *strm = &strm1;
634 int ret, out_len;
636 memset(strm, 0, sizeof(*strm));
638 strm->next_in = (uint8_t *)buf;
639 strm->avail_in = buf_size;
640 strm->next_out = out_buf;
641 strm->avail_out = out_buf_size;
643 ret = inflateInit2(strm, -12);
644 if (ret != Z_OK)
645 return -1;
646 ret = inflate(strm, Z_FINISH);
647 out_len = strm->next_out - out_buf;
648 if ( (ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
649 (out_len != out_buf_size) ) {
650 inflateEnd(strm);
651 return -1;
652 }
653 inflateEnd(strm);
654 return 0;
655 }
657 static int decompress_cluster(struct tdqcow_state *s, uint64_t cluster_offset)
658 {
659 int ret, csize;
660 uint64_t coffset;
662 coffset = cluster_offset & s->cluster_offset_mask;
663 if (s->cluster_cache_offset != coffset) {
664 csize = cluster_offset >> (63 - s->cluster_bits);
665 csize &= (s->cluster_size - 1);
666 lseek(s->fd, coffset, SEEK_SET);
667 ret = read(s->fd, s->cluster_data, csize);
668 if (ret != csize)
669 return -1;
670 if (decompress_buffer(s->cluster_cache, s->cluster_size,
671 s->cluster_data, csize) < 0) {
672 return -1;
673 }
674 s->cluster_cache_offset = coffset;
675 }
676 return 0;
677 }
679 static int
680 tdqcow_read_header(int fd, QCowHeader *header)
681 {
682 int err;
683 char *buf;
684 struct stat st;
685 size_t size, expected;
687 memset(header, 0, sizeof(*header));
689 err = fstat(fd, &st);
690 if (err)
691 return -errno;
693 err = lseek(fd, 0, SEEK_SET);
694 if (err == (off_t)-1)
695 return -errno;
697 size = (sizeof(*header) + 511) & ~511;
698 err = posix_memalign((void **)&buf, 512, size);
699 if (err)
700 return err;
702 expected = size;
703 if (st.st_size < size)
704 expected = st.st_size;
706 errno = 0;
707 err = read(fd, buf, size);
708 if (err != expected) {
709 err = (errno ? -errno : -EIO);
710 goto out;
711 }
713 memcpy(header, buf, sizeof(*header));
714 be32_to_cpus(&header->magic);
715 be32_to_cpus(&header->version);
716 be64_to_cpus(&header->backing_file_offset);
717 be32_to_cpus(&header->backing_file_size);
718 be32_to_cpus(&header->mtime);
719 be64_to_cpus(&header->size);
720 be32_to_cpus(&header->crypt_method);
721 be64_to_cpus(&header->l1_table_offset);
723 err = 0;
725 out:
726 free(buf);
727 return err;
728 }
730 static int
731 tdqcow_load_l1_table(struct tdqcow_state *s, QCowHeader *header)
732 {
733 char *buf;
734 struct stat st;
735 size_t expected;
736 int i, err, shift;
737 QCowHeader_ext *exthdr;
738 uint32_t l1_table_bytes, l1_table_block, l1_table_size;
740 buf = NULL;
741 s->l1_table = NULL;
743 shift = s->cluster_bits + s->l2_bits;
745 s->l1_size = (header->size + (1LL << shift) - 1) >> shift;
746 s->l1_table_offset = header->l1_table_offset;
748 s->min_cluster_alloc = 1; /* default */
750 l1_table_bytes = s->l1_size * sizeof(uint64_t);
751 l1_table_size = (l1_table_bytes + 4095) & ~4095;
752 l1_table_block = (l1_table_bytes + s->l1_table_offset + 4095) & ~4095;
754 DPRINTF("L1 Table offset detected: %"PRIu64", size %d (%d)\n",
755 (uint64_t)s->l1_table_offset,
756 (int) (s->l1_size * sizeof(uint64_t)),
757 l1_table_size);
759 err = fstat(s->fd, &st);
760 if (err) {
761 err = -errno;
762 goto out;
763 }
765 err = lseek(s->fd, 0, SEEK_SET);
766 if (err == (off_t)-1) {
767 err = -errno;
768 goto out;
769 }
771 err = posix_memalign((void **)&buf, 512, l1_table_block);
772 if (err) {
773 buf = NULL;
774 goto out;
775 }
777 err = posix_memalign((void **)&s->l1_table, 4096, l1_table_size);
778 if (err) {
779 s->l1_table = NULL;
780 goto out;
781 }
783 memset(buf, 0, l1_table_block);
784 memset(s->l1_table, 0, l1_table_size);
786 expected = l1_table_block;
787 if (st.st_size < l1_table_block)
788 expected = st.st_size;
790 errno = 0;
791 err = read(s->fd, buf, l1_table_block);
792 if (err != expected) {
793 err = (errno ? -errno : -EIO);
794 goto out;
795 }
797 memcpy(s->l1_table, buf + s->l1_table_offset, l1_table_size);
798 exthdr = (QCowHeader_ext *)(buf + sizeof(QCowHeader));
800 /* check for xen extended header */
801 if (s->l1_table_offset % 4096 == 0 &&
802 be32_to_cpu(exthdr->xmagic) == XEN_MAGIC) {
803 uint32_t flags = be32_to_cpu(exthdr->flags);
804 uint32_t cksum = be32_to_cpu(exthdr->cksum);
806 /*
807 * Try to detect old tapdisk images. They have to be fixed
808 * because they use big endian rather than native endian for
809 * the L1 table. After this block, the l1 table will
810 * definitely be in BIG endian.
811 */
812 if (!(flags & EXTHDR_L1_BIG_ENDIAN)) {
813 DPRINTF("qcow: converting to big endian L1 table\n");
815 /* convert to big endian */
816 for (i = 0; i < s->l1_size; i++)
817 cpu_to_be64s(&s->l1_table[i]);
819 flags |= EXTHDR_L1_BIG_ENDIAN;
820 exthdr->flags = cpu_to_be32(flags);
822 memcpy(buf + s->l1_table_offset,
823 s->l1_table, l1_table_size);
825 err = lseek(s->fd, 0, SEEK_SET);
826 if (err == (off_t)-1) {
827 err = -errno;
828 goto out;
829 }
831 err = atomicio(vwrite, s->fd, buf, l1_table_block);
832 if (err != l1_table_block) {
833 err = -errno;
834 goto out;
835 }
836 }
838 /* check the L1 table checksum */
839 if (cksum != gen_cksum((char *)s->l1_table,
840 s->l1_size * sizeof(uint64_t)))
841 DPRINTF("qcow: bad L1 checksum\n");
842 else {
843 s->extended = 1;
844 s->sparse = (be32_to_cpu(exthdr->flags) & SPARSE_FILE);
845 s->min_cluster_alloc =
846 be32_to_cpu(exthdr->min_cluster_alloc);
847 }
848 }
850 /* convert L1 table to native endian for operation */
851 for (i = 0; i < s->l1_size; i++)
852 be64_to_cpus(&s->l1_table[i]);
854 err = 0;
856 out:
857 if (err) {
858 free(buf);
859 free(s->l1_table);
860 s->l1_table = NULL;
861 }
862 return err;
863 }
865 /* Open the disk file and initialize qcow state. */
866 int tdqcow_open (td_driver_t *driver, const char *name, td_flag_t flags)
867 {
868 int fd, len, i, ret, size, o_flags;
869 td_disk_info_t *bs = &(driver->info);
870 struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
871 QCowHeader header;
872 uint64_t final_cluster = 0;
874 DPRINTF("QCOW: Opening %s\n", name);
876 o_flags = O_DIRECT | O_LARGEFILE |
877 ((flags == TD_OPEN_RDONLY) ? O_RDONLY : O_RDWR);
878 fd = open(name, o_flags);
879 if (fd < 0) {
880 DPRINTF("Unable to open %s (%d)\n", name, -errno);
881 return -1;
882 }
884 s->fd = fd;
885 s->name = strdup(name);
886 if (!s->name)
887 goto fail;
889 if (tdqcow_read_header(fd, &header))
890 goto fail;
892 if (header.magic != QCOW_MAGIC)
893 goto fail;
895 switch (header.version) {
896 case QCOW_VERSION:
897 break;
898 case 2:
899 //TODO: Port qcow2 to new blktap framework.
900 // close(fd);
901 // dd->drv = &tapdisk_qcow2;
902 // return dd->drv->td_open(dd, name, flags);
903 goto fail;
904 default:
905 goto fail;
906 }
908 if (header.size <= 1 || header.cluster_bits < 9)
909 goto fail;
910 if (header.crypt_method > QCOW_CRYPT_AES)
911 goto fail;
912 s->crypt_method_header = header.crypt_method;
913 if (s->crypt_method_header)
914 s->encrypted = 1;
915 s->cluster_bits = header.cluster_bits;
916 s->cluster_size = 1 << s->cluster_bits;
917 s->cluster_sectors = 1 << (s->cluster_bits - 9);
918 s->l2_bits = header.l2_bits;
919 s->l2_size = 1 << s->l2_bits;
920 s->cluster_alloc = s->l2_size;
921 bs->size = header.size / 512;
922 s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
923 s->backing_file_offset = header.backing_file_offset;
924 s->backing_file_size = header.backing_file_size;
926 /* allocate and load l1 table */
927 if (tdqcow_load_l1_table(s, &header))
928 goto fail;
930 /* alloc L2 cache */
931 size = s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t);
932 ret = posix_memalign((void **)&s->l2_cache, 4096, size);
933 if(ret != 0) goto fail;
935 size = s->cluster_size;
936 ret = posix_memalign((void **)&s->cluster_cache, 4096, size);
937 if(ret != 0) goto fail;
939 ret = posix_memalign((void **)&s->cluster_data, 4096, size);
940 if(ret != 0) goto fail;
941 s->cluster_cache_offset = -1;
943 if (s->backing_file_offset != 0)
944 s->cluster_alloc = 1; /*Cannot use pre-alloc*/
946 bs->sector_size = 512;
947 bs->info = 0;
949 for(i = 0; i < s->l1_size; i++)
950 if (s->l1_table[i] > final_cluster)
951 final_cluster = s->l1_table[i];
953 if (init_aio_state(driver)!=0) {
954 DPRINTF("Unable to initialise AIO state\n");
955 free_aio_state(s);
956 goto fail;
957 }
959 if (!final_cluster)
960 s->fd_end = s->l1_table_offset +
961 ((s->l1_size * sizeof(uint64_t) + 4095) & ~4095);
962 else {
963 s->fd_end = lseek(fd, 0, SEEK_END);
964 if (s->fd_end == (off_t)-1)
965 goto fail;
966 }
968 return 0;
970 fail:
971 DPRINTF("QCOW Open failed\n");
973 free_aio_state(s);
974 free(s->l1_table);
975 free(s->l2_cache);
976 free(s->cluster_cache);
977 free(s->cluster_data);
978 close(fd);
979 return -1;
980 }
982 void tdqcow_queue_read(td_driver_t *driver, td_request_t treq)
983 {
984 struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
985 int ret = 0, index_in_cluster, n, i;
986 uint64_t cluster_offset, sector, nb_sectors;
987 struct qcow_prv* prv;
988 td_request_t clone = treq;
989 char* buf = treq.buf;
991 sector = treq.sec;
992 nb_sectors = treq.secs;
994 /*We store a local record of the request*/
995 while (nb_sectors > 0) {
996 cluster_offset =
997 get_cluster_offset(s, sector << 9, 0, 0, 0, 0);
998 index_in_cluster = sector & (s->cluster_sectors - 1);
999 n = s->cluster_sectors - index_in_cluster;
1000 if (n > nb_sectors)
1001 n = nb_sectors;
1003 if (s->aio_free_count == 0) {
1004 td_complete_request(treq, -EBUSY);
1005 return;
1008 if(!cluster_offset) {
1009 int i;
1010 /* Forward entire request if possible. */
1011 for(i=0; i<nb_sectors; i++)
1012 if(get_cluster_offset(s, (sector+i) << 9, 0, 0, 0, 0))
1013 goto coalesce_failed;
1014 treq.buf = buf;
1015 treq.sec = sector;
1016 treq.secs = nb_sectors;
1017 td_forward_request(treq);
1018 return;
1019 coalesce_failed:
1020 treq.buf = buf;
1021 treq.sec = sector;
1022 treq.secs = n;
1023 td_forward_request(treq);
1025 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1026 if (decompress_cluster(s, cluster_offset) < 0) {
1027 td_complete_request(treq, -EIO);
1028 goto done;
1030 memcpy(buf, s->cluster_cache + index_in_cluster * 512,
1031 512 * n);
1033 treq.buf = buf;
1034 treq.sec = sector;
1035 treq.secs = n;
1036 td_complete_request(treq, 0);
1037 } else {
1038 clone.buf = buf;
1039 clone.sec = (cluster_offset>>9)+index_in_cluster;
1040 clone.secs = n;
1041 async_read(driver, clone);
1043 nb_sectors -= n;
1044 sector += n;
1045 buf += n * 512;
1047 done:
1048 return;
1051 void tdqcow_queue_write(td_driver_t *driver, td_request_t treq)
1053 struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
1054 int ret = 0, index_in_cluster, n, i;
1055 uint64_t cluster_offset, sector, nb_sectors;
1056 td_callback_t cb;
1057 struct qcow_prv* prv;
1058 char* buf = treq.buf;
1059 td_request_t clone=treq;
1061 sector = treq.sec;
1062 nb_sectors = treq.secs;
1064 /*We store a local record of the request*/
1065 while (nb_sectors > 0) {
1066 index_in_cluster = sector & (s->cluster_sectors - 1);
1067 n = s->cluster_sectors - index_in_cluster;
1068 if (n > nb_sectors)
1069 n = nb_sectors;
1071 if (s->aio_free_count == 0) {
1072 td_complete_request(treq, -EBUSY);
1073 return;
1076 cluster_offset = get_cluster_offset(s, sector << 9, 1, 0,
1077 index_in_cluster,
1078 index_in_cluster+n);
1079 if (!cluster_offset) {
1080 DPRINTF("Ooops, no write cluster offset!\n");
1081 td_complete_request(treq, -EIO);
1082 return;
1085 if (s->crypt_method) {
1086 encrypt_sectors(s, sector, s->cluster_data,
1087 (unsigned char *)buf, n, 1,
1088 &s->aes_encrypt_key);
1090 clone.buf = buf;
1091 clone.sec = (cluster_offset>>9) + index_in_cluster;
1092 clone.secs = n;
1093 async_write(driver, clone);
1094 } else {
1095 clone.buf = buf;
1096 clone.sec = (cluster_offset>>9) + index_in_cluster;
1097 clone.secs = n;
1099 async_write(driver, clone);
1102 nb_sectors -= n;
1103 sector += n;
1104 buf += n * 512;
1106 s->cluster_cache_offset = -1; /* disable compressed cache */
1108 return;
1111 static int
1112 tdqcow_update_checksum(struct tdqcow_state *s)
1114 int i, fd, err;
1115 uint32_t offset, cksum, out;
1117 if (!s->extended)
1118 return 0;
1120 fd = open(s->name, O_WRONLY | O_LARGEFILE); /* open without O_DIRECT */
1121 if (fd == -1) {
1122 err = errno;
1123 goto out;
1126 offset = sizeof(QCowHeader) + offsetof(QCowHeader_ext, cksum);
1127 if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
1128 err = errno;
1129 goto out;
1132 /* convert to big endian for checksum */
1133 for (i = 0; i < s->l1_size; i++)
1134 cpu_to_be64s(&s->l1_table[i]);
1136 cksum = gen_cksum((char *)s->l1_table, s->l1_size * sizeof(uint64_t));
1138 /* and back again... */
1139 for (i = 0; i < s->l1_size; i++)
1140 be64_to_cpus(&s->l1_table[i]);
1142 DPRINTF("Writing cksum: %d", cksum);
1144 out = cpu_to_be32(cksum);
1145 if (write(fd, &out, sizeof(out)) != sizeof(out)) {
1146 err = errno;
1147 goto out;
1150 err = 0;
1152 out:
1153 if (err)
1154 DPRINTF("failed to update checksum: %d\n", err);
1155 if (fd != -1)
1156 close(fd);
1157 return err;
1160 int tdqcow_close(td_driver_t *driver)
1162 struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
1164 /*Update the hdr cksum*/
1165 tdqcow_update_checksum(s);
1167 free_aio_state(s);
1168 free(s->name);
1169 free(s->l1_table);
1170 free(s->l2_cache);
1171 free(s->cluster_cache);
1172 free(s->cluster_data);
1173 close(s->fd);
1174 return 0;
1177 int qcow_create(const char *filename, uint64_t total_size,
1178 const char *backing_file, int sparse)
1180 int fd, header_size, backing_filename_len, l1_size, i;
1181 int shift, length, adjust, flags = 0, ret = 0;
1182 QCowHeader header;
1183 QCowHeader_ext exthdr;
1184 char backing_filename[PATH_MAX], *ptr;
1185 uint64_t tmp, size, total_length;
1186 struct stat st;
1188 DPRINTF("Qcow_create: size %"PRIu64"\n",total_size);
1190 fd = open(filename,
1191 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1192 0644);
1193 if (fd < 0)
1194 return -1;
1196 memset(&header, 0, sizeof(header));
1197 header.magic = cpu_to_be32(QCOW_MAGIC);
1198 header.version = cpu_to_be32(QCOW_VERSION);
1200 /*Create extended header fields*/
1201 exthdr.xmagic = cpu_to_be32(XEN_MAGIC);
1203 header_size = sizeof(header) + sizeof(QCowHeader_ext);
1204 backing_filename_len = 0;
1205 size = (total_size >> SECTOR_SHIFT);
1206 if (backing_file) {
1207 if (strcmp(backing_file, "fat:")) {
1208 const char *p;
1209 /* XXX: this is a hack: we do not attempt to
1210 *check for URL like syntax */
1211 p = strchr(backing_file, ':');
1212 if (p && (p - backing_file) >= 2) {
1213 /* URL like but exclude "c:" like filenames */
1214 strncpy(backing_filename, backing_file,
1215 sizeof(backing_filename));
1216 } else {
1217 if (realpath(backing_file, backing_filename) == NULL ||
1218 stat(backing_filename, &st) != 0) {
1219 return -1;
1222 header.backing_file_offset = cpu_to_be64(header_size);
1223 backing_filename_len = strlen(backing_filename);
1224 header.backing_file_size = cpu_to_be32(
1225 backing_filename_len);
1226 header_size += backing_filename_len;
1228 /*Set to the backing file size*/
1229 if(get_filesize(backing_filename, &size, &st)) {
1230 return -1;
1232 DPRINTF("Backing file size detected: %"PRId64" sectors"
1233 "(total %"PRId64" [%"PRId64" MB])\n",
1234 size,
1235 (uint64_t)(size << SECTOR_SHIFT),
1236 (uint64_t)(size >> 11));
1237 } else {
1238 backing_file = NULL;
1239 DPRINTF("Setting file size: %"PRId64" (total %"PRId64")\n",
1240 total_size,
1241 (uint64_t) (total_size << SECTOR_SHIFT));
1243 header.mtime = cpu_to_be32(st.st_mtime);
1244 header.cluster_bits = 9; /* 512 byte cluster to avoid copying
1245 unmodifyed sectors */
1246 header.l2_bits = 12; /* 32 KB L2 tables */
1247 exthdr.min_cluster_alloc = cpu_to_be32(1);
1248 } else {
1249 DPRINTF("Setting file size: %"PRId64" sectors"
1250 "(total %"PRId64" [%"PRId64" MB])\n",
1251 size,
1252 (uint64_t) (size << SECTOR_SHIFT),
1253 (uint64_t) (size >> 11));
1254 header.cluster_bits = 12; /* 4 KB clusters */
1255 header.l2_bits = 9; /* 4 KB L2 tables */
1256 exthdr.min_cluster_alloc = cpu_to_be32(1 << 9);
1258 /*Set the header size value*/
1259 header.size = cpu_to_be64(size * 512);
1261 header_size = (header_size + 7) & ~7;
1262 if (header_size % 4096 > 0) {
1263 header_size = ((header_size >> 12) + 1) << 12;
1266 shift = header.cluster_bits + header.l2_bits;
1267 l1_size = ((size * 512) + (1LL << shift) - 1) >> shift;
1269 header.l1_table_offset = cpu_to_be64(header_size);
1270 DPRINTF("L1 Table offset: %d, size %d\n",
1271 header_size,
1272 (int)(l1_size * sizeof(uint64_t)));
1273 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1275 ptr = calloc(1, l1_size * sizeof(uint64_t));
1276 exthdr.cksum = cpu_to_be32(gen_cksum(ptr, l1_size * sizeof(uint64_t)));
1277 printf("Created cksum: %d\n",exthdr.cksum);
1278 free(ptr);
1280 /*adjust file length to system page size boundary*/
1281 length = ROUNDUP(header_size + (l1_size * sizeof(uint64_t)),
1282 getpagesize());
1283 if (qtruncate(fd, length, 0)!=0) {
1284 DPRINTF("ERROR truncating file\n");
1285 return -1;
1288 if (sparse == 0) {
1289 /*Filesize is length+l1_size*(1 << s->l2_bits)+(size*512)*/
1290 total_length = length + (l1_size * (1 << 9)) + (size * 512);
1291 if (qtruncate(fd, total_length, 0)!=0) {
1292 DPRINTF("ERROR truncating file\n");
1293 return -1;
1295 printf("File truncated to length %"PRIu64"\n",total_length);
1296 } else
1297 flags = SPARSE_FILE;
1299 flags |= EXTHDR_L1_BIG_ENDIAN;
1300 exthdr.flags = cpu_to_be32(flags);
1302 /* write all the data */
1303 lseek(fd, 0, SEEK_SET);
1304 ret += write(fd, &header, sizeof(header));
1305 ret += write(fd, &exthdr, sizeof(exthdr));
1306 if (backing_file)
1307 ret += write(fd, backing_filename, backing_filename_len);
1309 lseek(fd, header_size, SEEK_SET);
1310 tmp = 0;
1311 for (i = 0;i < l1_size; i++) {
1312 ret += write(fd, &tmp, sizeof(tmp));
1315 close(fd);
1317 return 0;
1320 static int qcow_make_empty(struct tdqcow_state *s)
1322 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1324 memset(s->l1_table, 0, l1_length);
1325 lseek(s->fd, s->l1_table_offset, SEEK_SET);
1326 if (write(s->fd, s->l1_table, l1_length) < 0)
1327 return -1;
1328 if (qtruncate(s->fd, s->l1_table_offset + l1_length, s->sparse)!=0) {
1329 DPRINTF("ERROR truncating file\n");
1330 return -1;
1333 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
1334 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
1335 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
1337 return 0;
1340 static int qcow_get_cluster_size(struct tdqcow_state *s)
1342 return s->cluster_size;
1345 /* XXX: put compressed sectors first, then all the cluster aligned
1346 tables to avoid losing bytes in alignment */
1347 static int qcow_compress_cluster(struct tdqcow_state *s, int64_t sector_num,
1348 const uint8_t *buf)
1350 z_stream strm;
1351 int ret, out_len;
1352 uint8_t *out_buf;
1353 uint64_t cluster_offset;
1355 out_buf = malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1356 if (!out_buf)
1357 return -1;
1359 /* best compression, small window, no zlib header */
1360 memset(&strm, 0, sizeof(strm));
1361 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1362 Z_DEFLATED, -12,
1363 9, Z_DEFAULT_STRATEGY);
1364 if (ret != 0) {
1365 free(out_buf);
1366 return -1;
1369 strm.avail_in = s->cluster_size;
1370 strm.next_in = (uint8_t *)buf;
1371 strm.avail_out = s->cluster_size;
1372 strm.next_out = out_buf;
1374 ret = deflate(&strm, Z_FINISH);
1375 if (ret != Z_STREAM_END && ret != Z_OK) {
1376 free(out_buf);
1377 deflateEnd(&strm);
1378 return -1;
1380 out_len = strm.next_out - out_buf;
1382 deflateEnd(&strm);
1384 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1385 /* could not compress: write normal cluster */
1386 //tdqcow_queue_write(bs, sector_num, buf, s->cluster_sectors);
1387 } else {
1388 cluster_offset = get_cluster_offset(s, sector_num << 9, 2,
1389 out_len, 0, 0);
1390 cluster_offset &= s->cluster_offset_mask;
1391 lseek(s->fd, cluster_offset, SEEK_SET);
1392 if (write(s->fd, out_buf, out_len) != out_len) {
1393 free(out_buf);
1394 return -1;
1398 free(out_buf);
1399 return 0;
1402 static int
1403 tdqcow_get_image_type(const char *file, int *type)
1405 int fd;
1406 size_t size;
1407 QCowHeader header;
1409 fd = open(file, O_RDONLY);
1410 if (fd == -1)
1411 return -errno;
1413 size = read(fd, &header, sizeof(header));
1414 close(fd);
1415 if (size != sizeof(header))
1416 return (errno ? -errno : -EIO);
1418 be32_to_cpus(&header.magic);
1419 if (header.magic == QCOW_MAGIC)
1420 *type = DISK_TYPE_QCOW;
1421 else
1422 *type = DISK_TYPE_AIO;
1424 return 0;
1427 int tdqcow_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
1429 off_t off;
1430 char *buf, *filename;
1431 int len, secs, type, err = -EINVAL;
1432 struct tdqcow_state *child = (struct tdqcow_state *)driver->data;
1434 if (!child->backing_file_offset)
1435 return TD_NO_PARENT;
1437 /* read the backing file name */
1438 len = child->backing_file_size;
1439 off = child->backing_file_offset - (child->backing_file_offset % 512);
1440 secs = (len + (child->backing_file_offset - off) + 511) >> 9;
1442 if (posix_memalign((void **)&buf, 512, secs << 9))
1443 return -1;
1445 if (lseek(child->fd, off, SEEK_SET) == (off_t)-1)
1446 goto out;
1448 if (read(child->fd, buf, secs << 9) != secs << 9)
1449 goto out;
1450 filename = buf + (child->backing_file_offset - off);
1451 filename[len] = '\0';
1453 if (tdqcow_get_image_type(filename, &type))
1454 goto out;
1456 id->name = strdup(filename);
1457 id->drivertype = type;
1458 err = 0;
1459 out:
1460 free(buf);
1461 return err;
1464 int tdqcow_validate_parent(td_driver_t *driver,
1465 td_driver_t *pdriver, td_flag_t flags)
1467 struct stat stats;
1468 uint64_t psize, csize;
1469 struct tdqcow_state *c = (struct tdqcow_state *)driver->data;
1470 struct tdqcow_state *p = (struct tdqcow_state *)pdriver->data;
1472 if (stat(p->name, &stats))
1473 return -EINVAL;
1474 if (get_filesize(p->name, &psize, &stats))
1475 return -EINVAL;
1477 if (stat(c->name, &stats))
1478 return -EINVAL;
1479 if (get_filesize(c->name, &csize, &stats))
1480 return -EINVAL;
1482 if (csize != psize)
1483 return -EINVAL;
1485 return 0;
1488 struct tap_disk tapdisk_qcow = {
1489 .disk_type = "tapdisk_qcow",
1490 .flags = 0,
1491 .private_data_size = sizeof(struct tdqcow_state),
1492 .td_open = tdqcow_open,
1493 .td_close = tdqcow_close,
1494 .td_queue_read = tdqcow_queue_read,
1495 .td_queue_write = tdqcow_queue_write,
1496 .td_get_parent_id = tdqcow_get_parent_id,
1497 .td_validate_parent = tdqcow_validate_parent,
1498 .td_debug = NULL,
1499 };