Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / include / grub / zfs / spa.h
blob19a2b640ad6b76b5bf062bb31c88e0541873633d
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc.
4 * Copyright 2010 Sun Microsystems, Inc.
6 * GRUB is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef GRUB_ZFS_SPA_HEADER
21 #define GRUB_ZFS_SPA_HEADER 1
23 #define grub_zfs_to_cpu16(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) ? \
24 grub_be_to_cpu16(x) \
25 : grub_le_to_cpu16(x))
26 #define grub_cpu_to_zfs16(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) ? \
27 grub_cpu_to_be16(x) \
28 : grub_cpu_to_le16(x))
30 #define grub_zfs_to_cpu32(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) ? \
31 grub_be_to_cpu32(x) \
32 : grub_le_to_cpu32(x))
33 #define grub_cpu_to_zfs32(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) ? \
34 grub_cpu_to_be32(x) \
35 : grub_cpu_to_le32(x))
37 #define grub_zfs_to_cpu64(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) \
38 ? grub_be_to_cpu64(x) \
39 : grub_le_to_cpu64(x))
40 #define grub_cpu_to_zfs64(x,a) (((a) == GRUB_ZFS_BIG_ENDIAN) ? grub_cpu_to_be64(x) \
41 : grub_cpu_to_le64(x))
44 * General-purpose 32-bit and 64-bit bitfield encodings.
46 #define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len))
47 #define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len))
48 #define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low))
49 #define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low))
51 #define BF32_GET(x, low, len) BF32_DECODE(x, low, len)
52 #define BF64_GET(x, low, len) BF64_DECODE(x, low, len)
54 #define BF32_SET(x, low, len, val) \
55 ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
56 #define BF64_SET(x, low, len, val) \
57 ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
59 #define BF32_GET_SB(x, low, len, shift, bias) \
60 ((BF32_GET(x, low, len) + (bias)) << (shift))
61 #define BF64_GET_SB(x, low, len, shift, bias) \
62 ((BF64_GET(x, low, len) + (bias)) << (shift))
64 #define BF32_SET_SB(x, low, len, shift, bias, val) \
65 BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
66 #define BF64_SET_SB(x, low, len, shift, bias, val) \
67 BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
69 #define SPA_MINBLOCKSHIFT 9
70 #define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT)
73 * Size of block to hold the configuration data (a packed nvlist)
75 #define SPA_CONFIG_BLOCKSIZE (1 << 14)
78 * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
79 * The ASIZE encoding should be at least 64 times larger (6 more bits)
80 * to support up to 4-way RAID-Z mirror mode with worst-case gang block
81 * overhead, three DVAs per bp, plus one more bit in case we do anything
82 * else that expands the ASIZE.
84 #define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */
85 #define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */
86 #define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */
89 * All SPA data is represented by 128-bit data virtual addresses (DVAs).
90 * The members of the dva_t should be considered opaque outside the SPA.
92 typedef struct dva {
93 grub_uint64_t dva_word[2];
94 } dva_t;
97 * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
99 typedef struct zio_cksum {
100 grub_uint64_t zc_word[4];
101 } zio_cksum_t;
104 * Each block is described by its DVAs, time of birth, checksum, etc.
105 * The word-by-word, bit-by-bit layout of the blkptr is as follows:
107 * 64 56 48 40 32 24 16 8 0
108 * +-------+-------+-------+-------+-------+-------+-------+-------+
109 * 0 | vdev1 | GRID | ASIZE |
110 * +-------+-------+-------+-------+-------+-------+-------+-------+
111 * 1 |G| offset1 |
112 * +-------+-------+-------+-------+-------+-------+-------+-------+
113 * 2 | vdev2 | GRID | ASIZE |
114 * +-------+-------+-------+-------+-------+-------+-------+-------+
115 * 3 |G| offset2 |
116 * +-------+-------+-------+-------+-------+-------+-------+-------+
117 * 4 | vdev3 | GRID | ASIZE |
118 * +-------+-------+-------+-------+-------+-------+-------+-------+
119 * 5 |G| offset3 |
120 * +-------+-------+-------+-------+-------+-------+-------+-------+
121 * 6 |BDX|lvl| type | cksum | comp | PSIZE | LSIZE |
122 * +-------+-------+-------+-------+-------+-------+-------+-------+
123 * 7 | padding |
124 * +-------+-------+-------+-------+-------+-------+-------+-------+
125 * 8 | padding |
126 * +-------+-------+-------+-------+-------+-------+-------+-------+
127 * 9 | physical birth txg |
128 * +-------+-------+-------+-------+-------+-------+-------+-------+
129 * a | logical birth txg |
130 * +-------+-------+-------+-------+-------+-------+-------+-------+
131 * b | fill count |
132 * +-------+-------+-------+-------+-------+-------+-------+-------+
133 * c | checksum[0] |
134 * +-------+-------+-------+-------+-------+-------+-------+-------+
135 * d | checksum[1] |
136 * +-------+-------+-------+-------+-------+-------+-------+-------+
137 * e | checksum[2] |
138 * +-------+-------+-------+-------+-------+-------+-------+-------+
139 * f | checksum[3] |
140 * +-------+-------+-------+-------+-------+-------+-------+-------+
142 * Legend:
144 * vdev virtual device ID
145 * offset offset into virtual device
146 * LSIZE logical size
147 * PSIZE physical size (after compression)
148 * ASIZE allocated size (including RAID-Z parity and gang block headers)
149 * GRID RAID-Z layout information (reserved for future use)
150 * cksum checksum function
151 * comp compression function
152 * G gang block indicator
153 * B byteorder (endianness)
154 * D dedup
155 * X unused
156 * lvl level of indirection
157 * type DMU object type
158 * phys birth txg of block allocation; zero if same as logical birth txg
159 * log. birth transaction group in which the block was logically born
160 * fill count number of non-zero blocks under this bp
161 * checksum[4] 256-bit checksum of the data this bp describes
163 #define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */
164 #define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */
166 typedef struct blkptr {
167 dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
168 grub_uint64_t blk_prop; /* size, compression, type, etc */
169 grub_uint64_t blk_pad[2]; /* Extra space for the future */
170 grub_uint64_t blk_phys_birth; /* txg when block was allocated */
171 grub_uint64_t blk_birth; /* transaction group at birth */
172 grub_uint64_t blk_fill; /* fill count */
173 zio_cksum_t blk_cksum; /* 256-bit checksum */
174 } blkptr_t;
177 * Macros to get and set fields in a bp or DVA.
179 #define DVA_GET_ASIZE(dva) \
180 BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
181 #define DVA_SET_ASIZE(dva, x) \
182 BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
184 #define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8)
185 #define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x)
187 #define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32)
188 #define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x)
190 #define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1)
191 #define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x)
193 #define BP_GET_LSIZE(bp) \
194 BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
195 #define BP_SET_LSIZE(bp, x) \
196 BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
198 #define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 8)
199 #define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 8, x)
201 #define BP_GET_CHECKSUM(bp) BF64_GET((bp)->blk_prop, 40, 8)
202 #define BP_SET_CHECKSUM(bp, x) BF64_SET((bp)->blk_prop, 40, 8, x)
204 #define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8)
205 #define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x)
207 #define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5)
208 #define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x)
210 #define BP_GET_PROP_BIT_61(bp) BF64_GET((bp)->blk_prop, 61, 1)
211 #define BP_SET_PROP_BIT_61(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x)
213 #define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1)
214 #define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x)
216 #define BP_GET_BYTEORDER(bp) (0 - BF64_GET((bp)->blk_prop, 63, 1))
217 #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
219 #define BP_PHYSICAL_BIRTH(bp) \
220 ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
222 #define BP_SET_BIRTH(bp, logical, physical) \
224 (bp)->blk_birth = (logical); \
225 (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
228 #define BP_GET_ASIZE(bp) \
229 (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
230 DVA_GET_ASIZE(&(bp)->blk_dva[2]))
232 #define BP_GET_UCSIZE(bp) \
233 ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
234 BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
236 #define BP_GET_NDVAS(bp) \
237 (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
238 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
239 !!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
241 #define BP_COUNT_GANG(bp) \
242 (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
243 DVA_GET_GANG(&(bp)->blk_dva[1]) + \
244 DVA_GET_GANG(&(bp)->blk_dva[2]))
246 #define DVA_EQUAL(dva1, dva2) \
247 ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
248 (dva1)->dva_word[0] == (dva2)->dva_word[0])
250 #define BP_EQUAL(bp1, bp2) \
251 (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \
252 DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \
253 DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \
254 DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
256 #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \
257 (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
258 ((zc1).zc_word[1] - (zc2).zc_word[1]) | \
259 ((zc1).zc_word[2] - (zc2).zc_word[2]) | \
260 ((zc1).zc_word[3] - (zc2).zc_word[3])))
262 #define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0)
264 #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \
266 (zcp)->zc_word[0] = w0; \
267 (zcp)->zc_word[1] = w1; \
268 (zcp)->zc_word[2] = w2; \
269 (zcp)->zc_word[3] = w3; \
272 #define BP_IDENTITY(bp) (&(bp)->blk_dva[0])
273 #define BP_IS_GANG(bp) DVA_GET_GANG(BP_IDENTITY(bp))
274 #define BP_IS_HOLE(bp) ((bp)->blk_birth == 0)
276 /* BP_IS_RAIDZ(bp) assumes no block compression */
277 #define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
278 BP_GET_PSIZE(bp))
280 #define BP_ZERO(bp) \
282 (bp)->blk_dva[0].dva_word[0] = 0; \
283 (bp)->blk_dva[0].dva_word[1] = 0; \
284 (bp)->blk_dva[1].dva_word[0] = 0; \
285 (bp)->blk_dva[1].dva_word[1] = 0; \
286 (bp)->blk_dva[2].dva_word[0] = 0; \
287 (bp)->blk_dva[2].dva_word[1] = 0; \
288 (bp)->blk_prop = 0; \
289 (bp)->blk_pad[0] = 0; \
290 (bp)->blk_pad[1] = 0; \
291 (bp)->blk_phys_birth = 0; \
292 (bp)->blk_birth = 0; \
293 (bp)->blk_fill = 0; \
294 ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
297 #define BP_SPRINTF_LEN 320
299 #endif /* ! GRUB_ZFS_SPA_HEADER */