1 /*
2  * Copyright (c) 2008, XenSource Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of XenSource Inc. nor the names of its contributors
13  *       may be used to endorse or promote products derived from this software
14  *       without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef _QCOW_H_
30 #define _QCOW_H_
31 
32 #include "aes.h"
33 /**************************************************************/
34 /* QEMU COW block driver with compression and encryption support */
35 
36 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
37 #define XEN_MAGIC  (('X' << 24) | ('E' << 16) | ('N' << 8) | 0xfb)
38 #define QCOW_VERSION 1
39 
40 #define QCOW_CRYPT_NONE 0x00
41 #define QCOW_CRYPT_AES  0x01
42 
43 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
44 #define SPARSE_FILE 0x01
45 #define EXTHDR_L1_BIG_ENDIAN 0x02
46 
47 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
50 
51 #define ROUNDUP(l, s) \
52 ({ \
53     (uint64_t)( \
54         (l + (s - 1)) - ((l + (s - 1)) % s)); \
55 })
56 
57 typedef struct QCowHeader {
58 	uint32_t magic;
59 	uint32_t version;
60 	uint64_t backing_file_offset;
61 	uint32_t backing_file_size;
62 	uint32_t mtime;
63 	uint64_t size; /* in bytes */
64 	uint8_t cluster_bits;
65 	uint8_t l2_bits;
66 	uint32_t crypt_method;
67 	uint64_t l1_table_offset;
68 } QCowHeader;
69 
70 /*Extended header for Xen enhancements*/
71 typedef struct QCowHeader_ext {
72         uint32_t xmagic;
73         uint32_t cksum;
74         uint32_t min_cluster_alloc;
75         uint32_t flags;
76 } QCowHeader_ext;
77 
78 uint32_t gen_cksum(char *ptr, int len);
79 int get_filesize(char *filename, uint64_t *size, struct stat *st);
80 int qtruncate(int fd, off_t length, int sparse);
81 
82 #define L2_CACHE_SIZE 16  /*Fixed allocation in Qemu*/
83 
84 struct tdqcow_state {
85         int fd;                        /*Main Qcow file descriptor */
86 	uint64_t fd_end;               /*Store a local record of file length */
87 	char *name;                    /*Record of the filename*/
88 	uint32_t backing_file_size;
89 	uint64_t backing_file_offset;
90 	uint8_t extended;              /*File contains extended header*/
91 	int encrypted;                 /*File contents are encrypted or plain*/
92 	int cluster_bits;              /*Determines length of cluster as
93 					*indicated by file hdr*/
94 	int cluster_size;              /*Length of cluster*/
95 	int cluster_sectors;           /*Number of sectors per cluster*/
96 	int cluster_alloc;             /*Blktap fix for allocating full
97 					*extents*/
98 	int min_cluster_alloc;         /*Blktap historical extent alloc*/
99 	int sparse;                    /*Indicates whether to preserve sparseness*/
100 	int l2_bits;                   /*Size of L2 table entry*/
101 	int l2_size;                   /*Full table size*/
102 	int l1_size;                   /*L1 table size*/
103 	uint64_t cluster_offset_mask;
104 	uint64_t l1_table_offset;      /*L1 table offset from beginning of
105 					*file*/
106 	uint64_t *l1_table;            /*L1 table entries*/
107 	uint64_t *l2_cache;            /*We maintain a cache of size
108 					*L2_CACHE_SIZE of most read entries*/
109 	uint64_t l2_cache_offsets[L2_CACHE_SIZE];     /*L2 cache entries*/
110 	uint32_t l2_cache_counts[L2_CACHE_SIZE];      /*Cache access record*/
111 	uint8_t *cluster_cache;
112 	uint8_t *cluster_data;
113 	uint64_t cluster_cache_offset; /**/
114 	uint32_t crypt_method;         /*current crypt method, 0 if no
115 					*key yet */
116 	uint32_t crypt_method_header;  /**/
117 	AES_KEY aes_encrypt_key;       /*AES key*/
118 	AES_KEY aes_decrypt_key;       /*AES key*/
119 
120         /* libaio state */
121 	int                  aio_free_count;
122 	int                  max_aio_reqs;
123 	struct qcow_request   *aio_requests;
124 	struct qcow_request  **aio_free_list;
125 
126 };
127 
128 int qcow_create(const char *filename, uint64_t total_size,
129 		const char *backing_file, int sparse);
130 
131 #endif //_QCOW_H_
132