ia64/xen-unstable

view tools/blktap2/include/vhd.h @ 19817:b7f73a7f3078

blktap2: portability fixes for NetBSD

- Use standard off_t and lseek() instead of non-portable off64_t and
lseek64()
- Use uuid API as documented in DCE 1.1 RPC specification
- Add NetBSD implementation for blk_getimagesize() and
blk_getsectorsize()
- Use blk_getimagesize() and blk_getsectorsize()
- Fix uuid header check

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jun 23 17:24:14 2009 +0100 (2009-06-23)
parents 1c627434605e
children f1fec38c8228
line source
1 /* Copyright (c) 2008, XenSource Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of XenSource Inc. nor the names of its contributors
12 * may be used to endorse or promote products derived from this software
13 * without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef __VHD_H__
28 #define __VHD_H__
30 #include <inttypes.h>
31 #include "uuid.h"
33 typedef uint32_t u32;
34 typedef uint64_t u64;
36 #define DEBUG 1
38 /* ---------------------------------------------------------------------- */
39 /* General definitions. */
40 /* ---------------------------------------------------------------------- */
42 #define VHD_SECTOR_SIZE 512
43 #define VHD_SECTOR_SHIFT 9
45 /* ---------------------------------------------------------------------- */
46 /* This is the generic disk footer, used by all disks. */
47 /* ---------------------------------------------------------------------- */
49 struct hd_ftr {
50 char cookie[8]; /* Identifies original creator of the disk */
51 u32 features; /* Feature Support -- see below */
52 u32 ff_version; /* (major,minor) version of disk file */
53 u64 data_offset; /* Abs. offset from SOF to next structure */
54 u32 timestamp; /* Creation time. secs since 1/1/2000GMT */
55 char crtr_app[4]; /* Creator application */
56 u32 crtr_ver; /* Creator version (major,minor) */
57 u32 crtr_os; /* Creator host OS */
58 u64 orig_size; /* Size at creation (bytes) */
59 u64 curr_size; /* Current size of disk (bytes) */
60 u32 geometry; /* Disk geometry */
61 u32 type; /* Disk type */
62 u32 checksum; /* 1's comp sum of this struct. */
63 uuid_t uuid; /* Unique disk ID, used for naming parents */
64 char saved; /* one-bit -- is this disk/VM in a saved state? */
65 char hidden; /* tapdisk-specific field: is this vdi hidden? */
66 char reserved[426]; /* padding */
67 };
69 /* VHD cookie string. */
70 static const char HD_COOKIE[9] = "conectix";
72 /* Feature fields in hd_ftr */
73 #define HD_NO_FEATURES 0x00000000
74 #define HD_TEMPORARY 0x00000001 /* disk can be deleted on shutdown */
75 #define HD_RESERVED 0x00000002 /* NOTE: must always be set */
77 /* Version field in hd_ftr */
78 #define HD_FF_VERSION 0x00010000
80 /* Known creator OS type fields in hd_ftr.crtr_os */
81 #define HD_CR_OS_WINDOWS 0x5769326B /* (Wi2k) */
82 #define HD_CR_OS_MACINTOSH 0x4D616320 /* (Mac ) */
84 /*
85 * version 0.1: little endian bitmaps
86 * version 1.1: big endian bitmaps; batmap
87 * version 1.2: libvhd
88 * version 1.3: batmap version bump to 1.2
89 */
90 #define VHD_VERSION(major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))
91 #define VHD_CURRENT_VERSION VHD_VERSION(1, 3)
93 /* Disk geometry accessor macros. */
94 /* Geometry is a triple of (cylinders (2 bytes), tracks (1 byte), and
95 * secotrs-per-track (1 byte))
96 */
97 #define GEOM_GET_CYLS(_g) (((_g) >> 16) & 0xffff)
98 #define GEOM_GET_HEADS(_g) (((_g) >> 8) & 0xff)
99 #define GEOM_GET_SPT(_g) ((_g) & 0xff)
101 #define GEOM_ENCODE(_c, _h, _s) (((_c) << 16) | ((_h) << 8) | (_s))
103 /* type field in hd_ftr */
104 #define HD_TYPE_NONE 0
105 #define HD_TYPE_FIXED 2 /* fixed-allocation disk */
106 #define HD_TYPE_DYNAMIC 3 /* dynamic disk */
107 #define HD_TYPE_DIFF 4 /* differencing disk */
109 /* String table for hd.type */
110 static const char *HD_TYPE_STR[7] = {
111 "None", /* 0 */
112 "Reserved (deprecated)", /* 1 */
113 "Fixed hard disk", /* 2 */
114 "Dynamic hard disk", /* 3 */
115 "Differencing hard disk", /* 4 */
116 "Reserved (deprecated)", /* 5 */
117 "Reserved (deprecated)" /* 6 */
118 };
120 #define HD_TYPE_MAX 6
122 struct prt_loc {
123 u32 code; /* Platform code -- see defines below. */
124 u32 data_space; /* Number of 512-byte sectors to store locator */
125 u32 data_len; /* Actual length of parent locator in bytes */
126 u32 res; /* Must be zero */
127 u64 data_offset; /* Absolute offset of locator data (bytes) */
128 };
130 /* Platform Codes */
131 #define PLAT_CODE_NONE 0x0
132 #define PLAT_CODE_WI2R 0x57693272 /* deprecated */
133 #define PLAT_CODE_WI2K 0x5769326B /* deprecated */
134 #define PLAT_CODE_W2RU 0x57327275 /* Windows relative path (UTF-16) */
135 #define PLAT_CODE_W2KU 0x57326B75 /* Windows absolute path (UTF-16) */
136 #define PLAT_CODE_MAC 0x4D616320 /* MacOS alias stored as a blob. */
137 #define PLAT_CODE_MACX 0x4D616358 /* File URL (UTF-8), see RFC 2396. */
139 /* ---------------------------------------------------------------------- */
140 /* This is the dynamic disk header. */
141 /* ---------------------------------------------------------------------- */
143 struct dd_hdr {
144 char cookie[8]; /* Should contain "cxsparse" */
145 u64 data_offset; /* Byte offset of next record. (Unused) 0xffs */
146 u64 table_offset; /* Absolute offset to the BAT. */
147 u32 hdr_ver; /* Version of the dd_hdr (major,minor) */
148 u32 max_bat_size; /* Maximum number of entries in the BAT */
149 u32 block_size; /* Block size in bytes. Must be power of 2. */
150 u32 checksum; /* Header checksum. 1's comp of all fields. */
151 uuid_t prt_uuid; /* ID of the parent disk. */
152 u32 prt_ts; /* Modification time of the parent disk */
153 u32 res1; /* Reserved. */
154 char prt_name[512]; /* Parent unicode name. */
155 struct prt_loc loc[8]; /* Parent locator entries. */
156 char res2[256]; /* Reserved. */
157 };
159 /* VHD cookie string. */
160 static const char DD_COOKIE[9] = "cxsparse";
162 /* Version field in hd_ftr */
163 #define DD_VERSION 0x00010000
165 /* Default blocksize is 2 meg. */
166 #define DD_BLOCKSIZE_DEFAULT 0x00200000
168 #define DD_BLK_UNUSED 0xFFFFFFFF
170 struct dd_batmap_hdr {
171 char cookie[8]; /* should contain "tdbatmap" */
172 u64 batmap_offset; /* byte offset to batmap */
173 u32 batmap_size; /* batmap size in sectors */
174 u32 batmap_version; /* version of batmap */
175 u32 checksum; /* batmap checksum -- 1's complement of batmap */
176 };
178 static const char VHD_BATMAP_COOKIE[9] = "tdbatmap";
180 /*
181 * version 1.1: signed char checksum
182 */
183 #define VHD_BATMAP_VERSION(major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))
184 #define VHD_BATMAP_CURRENT_VERSION VHD_BATMAP_VERSION(1, 2)
186 /* Layout of a dynamic disk:
187 *
188 * +-------------------------------------------------+
189 * | Mirror image of HD footer (hd_ftr) (512 bytes) |
190 * +-------------------------------------------------+
191 * | Sparse drive header (dd_hdr) (1024 bytes) |
192 * +-------------------------------------------------+
193 * | BAT (Block allocation table) |
194 * | - Array of absolute sector offsets into the |
195 * | file (u32). |
196 * | - Rounded up to a sector boundary. |
197 * | - Unused entries are marked as 0xFFFFFFFF |
198 * | - max entries in dd_hdr->max_bat_size |
199 * +-------------------------------------------------+
200 * | Data Block 0 |
201 * | Bitmap (padded to 512 byte sector boundary) |
202 * | - each bit indicates whether the associated |
203 * | sector within this block is used. |
204 * | Data |
205 * | - power-of-two multiple of sectors. |
206 * | - default 2MB (4096 * 512) |
207 * | - Any entries with zero in bitmap should be |
208 * | zero on disk |
209 * +-------------------------------------------------+
210 * | Data Block 1 |
211 * +-------------------------------------------------+
212 * | ... |
213 * +-------------------------------------------------+
214 * | Data Block n |
215 * +-------------------------------------------------+
216 * | HD Footer (511 bytes) |
217 * +-------------------------------------------------+
218 */
220 #endif