]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cddl/boot/zfs/zfsimpl.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / cddl / boot / zfs / zfsimpl.h
1 /*-
2  * Copyright (c) 2002 McAfee, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and McAfee Research,, the Security Research Division of
7  * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8  * part of the DARPA CHATS research program
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * CDDL HEADER START
33  *
34  * The contents of this file are subject to the terms of the
35  * Common Development and Distribution License (the "License").
36  * You may not use this file except in compliance with the License.
37  *
38  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39  * or http://www.opensolaris.org/os/licensing.
40  * See the License for the specific language governing permissions
41  * and limitations under the License.
42  *
43  * When distributing Covered Code, include this CDDL HEADER in each
44  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45  * If applicable, add the following below this CDDL HEADER, with the
46  * fields enclosed by brackets "[]" replaced with your own identifying
47  * information: Portions Copyright [yyyy] [name of copyright owner]
48  *
49  * CDDL HEADER END
50  */
51 /*
52  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
53  * Use is subject to license terms.
54  */
55 /*
56  * Copyright 2013 by Saso Kiselkov. All rights reserved.
57  */
58
59 #define MAXNAMELEN      256
60
61 /* CRC64 table */
62 #define ZFS_CRC64_POLY  0xC96C5795D7870F42ULL   /* ECMA-182, reflected form */
63
64 /*
65  * Macros for various sorts of alignment and rounding when the alignment
66  * is known to be a power of 2.
67  */
68 #define P2ALIGN(x, align)               ((x) & -(align))
69 #define P2PHASE(x, align)               ((x) & ((align) - 1))
70 #define P2NPHASE(x, align)              (-(x) & ((align) - 1))
71 #define P2ROUNDUP(x, align)             (-(-(x) & -(align)))
72 #define P2END(x, align)                 (-(~(x) & -(align)))
73 #define P2PHASEUP(x, align, phase)      ((phase) - (((phase) - (x)) & -(align)))
74 #define P2BOUNDARY(off, len, align)     (((off) ^ ((off) + (len) - 1)) > (align) - 1)
75
76 /*
77  * General-purpose 32-bit and 64-bit bitfield encodings.
78  */
79 #define BF32_DECODE(x, low, len)        P2PHASE((x) >> (low), 1U << (len))
80 #define BF64_DECODE(x, low, len)        P2PHASE((x) >> (low), 1ULL << (len))
81 #define BF32_ENCODE(x, low, len)        (P2PHASE((x), 1U << (len)) << (low))
82 #define BF64_ENCODE(x, low, len)        (P2PHASE((x), 1ULL << (len)) << (low))
83
84 #define BF32_GET(x, low, len)           BF32_DECODE(x, low, len)
85 #define BF64_GET(x, low, len)           BF64_DECODE(x, low, len)
86
87 #define BF32_SET(x, low, len, val)      \
88         ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
89 #define BF64_SET(x, low, len, val)      \
90         ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
91
92 #define BF32_GET_SB(x, low, len, shift, bias)   \
93         ((BF32_GET(x, low, len) + (bias)) << (shift))
94 #define BF64_GET_SB(x, low, len, shift, bias)   \
95         ((BF64_GET(x, low, len) + (bias)) << (shift))
96
97 #define BF32_SET_SB(x, low, len, shift, bias, val)      \
98         BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
99 #define BF64_SET_SB(x, low, len, shift, bias, val)      \
100         BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
101
102 /*
103  * Macros to reverse byte order
104  */
105 #define BSWAP_8(x)      ((x) & 0xff)
106 #define BSWAP_16(x)     ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
107 #define BSWAP_32(x)     ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
108 #define BSWAP_64(x)     ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
109
110 /*
111  * We currently support nine block sizes, from 512 bytes to 128K.
112  * We could go higher, but the benefits are near-zero and the cost
113  * of COWing a giant block to modify one byte would become excessive.
114  */
115 #define SPA_MINBLOCKSHIFT       9
116 #define SPA_MAXBLOCKSHIFT       17
117 #define SPA_MINBLOCKSIZE        (1ULL << SPA_MINBLOCKSHIFT)
118 #define SPA_MAXBLOCKSIZE        (1ULL << SPA_MAXBLOCKSHIFT)
119
120 #define SPA_BLOCKSIZES          (SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
121
122 /*
123  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
124  * The ASIZE encoding should be at least 64 times larger (6 more bits)
125  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
126  * overhead, three DVAs per bp, plus one more bit in case we do anything
127  * else that expands the ASIZE.
128  */
129 #define SPA_LSIZEBITS           16      /* LSIZE up to 32M (2^16 * 512) */
130 #define SPA_PSIZEBITS           16      /* PSIZE up to 32M (2^16 * 512) */
131 #define SPA_ASIZEBITS           24      /* ASIZE up to 64 times larger  */
132
133 /*
134  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
135  * The members of the dva_t should be considered opaque outside the SPA.
136  */
137 typedef struct dva {
138         uint64_t        dva_word[2];
139 } dva_t;
140
141 /*
142  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
143  */
144 typedef struct zio_cksum {
145         uint64_t        zc_word[4];
146 } zio_cksum_t;
147
148 /*
149  * Each block is described by its DVAs, time of birth, checksum, etc.
150  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
151  *
152  *      64      56      48      40      32      24      16      8       0
153  *      +-------+-------+-------+-------+-------+-------+-------+-------+
154  * 0    |               vdev1           | GRID  |         ASIZE         |
155  *      +-------+-------+-------+-------+-------+-------+-------+-------+
156  * 1    |G|                      offset1                                |
157  *      +-------+-------+-------+-------+-------+-------+-------+-------+
158  * 2    |               vdev2           | GRID  |         ASIZE         |
159  *      +-------+-------+-------+-------+-------+-------+-------+-------+
160  * 3    |G|                      offset2                                |
161  *      +-------+-------+-------+-------+-------+-------+-------+-------+
162  * 4    |               vdev3           | GRID  |         ASIZE         |
163  *      +-------+-------+-------+-------+-------+-------+-------+-------+
164  * 5    |G|                      offset3                                |
165  *      +-------+-------+-------+-------+-------+-------+-------+-------+
166  * 6    |BDX|lvl| type  | cksum | comp  |     PSIZE     |     LSIZE     |
167  *      +-------+-------+-------+-------+-------+-------+-------+-------+
168  * 7    |                       padding                                 |
169  *      +-------+-------+-------+-------+-------+-------+-------+-------+
170  * 8    |                       padding                                 |
171  *      +-------+-------+-------+-------+-------+-------+-------+-------+
172  * 9    |                       physical birth txg                      |
173  *      +-------+-------+-------+-------+-------+-------+-------+-------+
174  * a    |                       logical birth txg                       |
175  *      +-------+-------+-------+-------+-------+-------+-------+-------+
176  * b    |                       fill count                              |
177  *      +-------+-------+-------+-------+-------+-------+-------+-------+
178  * c    |                       checksum[0]                             |
179  *      +-------+-------+-------+-------+-------+-------+-------+-------+
180  * d    |                       checksum[1]                             |
181  *      +-------+-------+-------+-------+-------+-------+-------+-------+
182  * e    |                       checksum[2]                             |
183  *      +-------+-------+-------+-------+-------+-------+-------+-------+
184  * f    |                       checksum[3]                             |
185  *      +-------+-------+-------+-------+-------+-------+-------+-------+
186  *
187  * Legend:
188  *
189  * vdev         virtual device ID
190  * offset       offset into virtual device
191  * LSIZE        logical size
192  * PSIZE        physical size (after compression)
193  * ASIZE        allocated size (including RAID-Z parity and gang block headers)
194  * GRID         RAID-Z layout information (reserved for future use)
195  * cksum        checksum function
196  * comp         compression function
197  * G            gang block indicator
198  * B            byteorder (endianness)
199  * D            dedup
200  * X            unused
201  * lvl          level of indirection
202  * type         DMU object type
203  * phys birth   txg of block allocation; zero if same as logical birth txg
204  * log. birth   transaction group in which the block was logically born
205  * fill count   number of non-zero blocks under this bp
206  * checksum[4]  256-bit checksum of the data this bp describes
207  */
208 #define SPA_BLKPTRSHIFT 7               /* blkptr_t is 128 bytes        */
209 #define SPA_DVAS_PER_BP 3               /* Number of DVAs in a bp       */
210
211 typedef struct blkptr {
212         dva_t           blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
213         uint64_t        blk_prop;       /* size, compression, type, etc     */
214         uint64_t        blk_pad[2];     /* Extra space for the future       */
215         uint64_t        blk_phys_birth; /* txg when block was allocated     */
216         uint64_t        blk_birth;      /* transaction group at birth       */
217         uint64_t        blk_fill;       /* fill count                       */
218         zio_cksum_t     blk_cksum;      /* 256-bit checksum                 */
219 } blkptr_t;
220
221 /*
222  * Macros to get and set fields in a bp or DVA.
223  */
224 #define DVA_GET_ASIZE(dva)      \
225         BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
226 #define DVA_SET_ASIZE(dva, x)   \
227         BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
228
229 #define DVA_GET_GRID(dva)       BF64_GET((dva)->dva_word[0], 24, 8)
230 #define DVA_SET_GRID(dva, x)    BF64_SET((dva)->dva_word[0], 24, 8, x)
231
232 #define DVA_GET_VDEV(dva)       BF64_GET((dva)->dva_word[0], 32, 32)
233 #define DVA_SET_VDEV(dva, x)    BF64_SET((dva)->dva_word[0], 32, 32, x)
234
235 #define DVA_GET_OFFSET(dva)     \
236         BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
237 #define DVA_SET_OFFSET(dva, x)  \
238         BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
239
240 #define DVA_GET_GANG(dva)       BF64_GET((dva)->dva_word[1], 63, 1)
241 #define DVA_SET_GANG(dva, x)    BF64_SET((dva)->dva_word[1], 63, 1, x)
242
243 #define BP_GET_LSIZE(bp)        \
244         (BP_IS_HOLE(bp) ? 0 : \
245         BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1))
246 #define BP_SET_LSIZE(bp, x)     \
247         BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
248
249 #define BP_GET_PSIZE(bp)        \
250         BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
251 #define BP_SET_PSIZE(bp, x)     \
252         BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
253
254 #define BP_GET_COMPRESS(bp)     BF64_GET((bp)->blk_prop, 32, 8)
255 #define BP_SET_COMPRESS(bp, x)  BF64_SET((bp)->blk_prop, 32, 8, x)
256
257 #define BP_GET_CHECKSUM(bp)     BF64_GET((bp)->blk_prop, 40, 8)
258 #define BP_SET_CHECKSUM(bp, x)  BF64_SET((bp)->blk_prop, 40, 8, x)
259
260 #define BP_GET_TYPE(bp)         BF64_GET((bp)->blk_prop, 48, 8)
261 #define BP_SET_TYPE(bp, x)      BF64_SET((bp)->blk_prop, 48, 8, x)
262
263 #define BP_GET_LEVEL(bp)        BF64_GET((bp)->blk_prop, 56, 5)
264 #define BP_SET_LEVEL(bp, x)     BF64_SET((bp)->blk_prop, 56, 5, x)
265
266 #define BP_GET_DEDUP(bp)        BF64_GET((bp)->blk_prop, 62, 1)
267 #define BP_SET_DEDUP(bp, x)     BF64_SET((bp)->blk_prop, 62, 1, x)
268
269 #define BP_GET_BYTEORDER(bp)    (0 - BF64_GET((bp)->blk_prop, 63, 1))
270 #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
271
272 #define BP_PHYSICAL_BIRTH(bp)           \
273         ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
274
275 #define BP_GET_ASIZE(bp)        \
276         (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
277                 DVA_GET_ASIZE(&(bp)->blk_dva[2]))
278
279 #define BP_GET_UCSIZE(bp) \
280         ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
281         BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
282
283 #define BP_GET_NDVAS(bp)        \
284         (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
285         !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
286         !!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
287
288 #define BP_COUNT_GANG(bp)       \
289         (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
290         DVA_GET_GANG(&(bp)->blk_dva[1]) + \
291         DVA_GET_GANG(&(bp)->blk_dva[2]))
292
293 #define DVA_EQUAL(dva1, dva2)   \
294         ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
295         (dva1)->dva_word[0] == (dva2)->dva_word[0])
296
297 #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \
298         (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
299         ((zc1).zc_word[1] - (zc2).zc_word[1]) | \
300         ((zc1).zc_word[2] - (zc2).zc_word[2]) | \
301         ((zc1).zc_word[3] - (zc2).zc_word[3])))
302
303
304 #define DVA_IS_VALID(dva)       (DVA_GET_ASIZE(dva) != 0)
305
306 #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)   \
307 {                                               \
308         (zcp)->zc_word[0] = w0;                 \
309         (zcp)->zc_word[1] = w1;                 \
310         (zcp)->zc_word[2] = w2;                 \
311         (zcp)->zc_word[3] = w3;                 \
312 }
313
314 #define BP_IDENTITY(bp)         (&(bp)->blk_dva[0])
315 #define BP_IS_GANG(bp)          DVA_GET_GANG(BP_IDENTITY(bp))
316 #define BP_IS_HOLE(bp)          ((bp)->blk_birth == 0)
317 #define BP_IS_OLDER(bp, txg)    (!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
318
319 #define BP_ZERO(bp)                             \
320 {                                               \
321         (bp)->blk_dva[0].dva_word[0] = 0;       \
322         (bp)->blk_dva[0].dva_word[1] = 0;       \
323         (bp)->blk_dva[1].dva_word[0] = 0;       \
324         (bp)->blk_dva[1].dva_word[1] = 0;       \
325         (bp)->blk_dva[2].dva_word[0] = 0;       \
326         (bp)->blk_dva[2].dva_word[1] = 0;       \
327         (bp)->blk_prop = 0;                     \
328         (bp)->blk_pad[0] = 0;                   \
329         (bp)->blk_pad[1] = 0;                   \
330         (bp)->blk_phys_birth = 0;               \
331         (bp)->blk_birth = 0;                    \
332         (bp)->blk_fill = 0;                     \
333         ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
334 }
335
336 /*
337  * Embedded checksum
338  */
339 #define ZEC_MAGIC       0x210da7ab10c7a11ULL
340
341 typedef struct zio_eck {
342         uint64_t        zec_magic;      /* for validation, endianness   */
343         zio_cksum_t     zec_cksum;      /* 256-bit checksum             */
344 } zio_eck_t;
345
346 /*
347  * Gang block headers are self-checksumming and contain an array
348  * of block pointers.
349  */
350 #define SPA_GANGBLOCKSIZE       SPA_MINBLOCKSIZE
351 #define SPA_GBH_NBLKPTRS        ((SPA_GANGBLOCKSIZE - \
352         sizeof (zio_eck_t)) / sizeof (blkptr_t))
353 #define SPA_GBH_FILLER          ((SPA_GANGBLOCKSIZE - \
354         sizeof (zio_eck_t) - \
355         (SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
356         sizeof (uint64_t))
357
358 typedef struct zio_gbh {
359         blkptr_t                zg_blkptr[SPA_GBH_NBLKPTRS];
360         uint64_t                zg_filler[SPA_GBH_FILLER];
361         zio_eck_t               zg_tail;
362 } zio_gbh_phys_t;
363
364 #define VDEV_RAIDZ_MAXPARITY    3
365
366 #define VDEV_PAD_SIZE           (8 << 10)
367 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
368 #define VDEV_SKIP_SIZE          VDEV_PAD_SIZE * 2
369 #define VDEV_PHYS_SIZE          (112 << 10)
370 #define VDEV_UBERBLOCK_RING     (128 << 10)
371
372 #define VDEV_UBERBLOCK_SHIFT(vd)        \
373         MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT)
374 #define VDEV_UBERBLOCK_COUNT(vd)        \
375         (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
376 #define VDEV_UBERBLOCK_OFFSET(vd, n)    \
377         offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
378 #define VDEV_UBERBLOCK_SIZE(vd)         (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
379
380 typedef struct vdev_phys {
381         char            vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
382         zio_eck_t       vp_zbt;
383 } vdev_phys_t;
384
385 typedef struct vdev_label {
386         char            vl_pad1[VDEV_PAD_SIZE];                 /*  8K  */
387         char            vl_pad2[VDEV_PAD_SIZE];                 /*  8K  */
388         vdev_phys_t     vl_vdev_phys;                           /* 112K */
389         char            vl_uberblock[VDEV_UBERBLOCK_RING];      /* 128K */
390 } vdev_label_t;                                                 /* 256K total */
391
392 /*
393  * vdev_dirty() flags
394  */
395 #define VDD_METASLAB    0x01
396 #define VDD_DTL         0x02
397
398 /*
399  * Size and offset of embedded boot loader region on each label.
400  * The total size of the first two labels plus the boot area is 4MB.
401  */
402 #define VDEV_BOOT_OFFSET        (2 * sizeof (vdev_label_t))
403 #define VDEV_BOOT_SIZE          (7ULL << 19)                    /* 3.5M */
404
405 /*
406  * Size of label regions at the start and end of each leaf device.
407  */
408 #define VDEV_LABEL_START_SIZE   (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
409 #define VDEV_LABEL_END_SIZE     (2 * sizeof (vdev_label_t))
410 #define VDEV_LABELS             4
411
412 enum zio_checksum {
413         ZIO_CHECKSUM_INHERIT = 0,
414         ZIO_CHECKSUM_ON,
415         ZIO_CHECKSUM_OFF,
416         ZIO_CHECKSUM_LABEL,
417         ZIO_CHECKSUM_GANG_HEADER,
418         ZIO_CHECKSUM_ZILOG,
419         ZIO_CHECKSUM_FLETCHER_2,
420         ZIO_CHECKSUM_FLETCHER_4,
421         ZIO_CHECKSUM_SHA256,
422         ZIO_CHECKSUM_ZILOG2,
423         ZIO_CHECKSUM_FUNCTIONS
424 };
425
426 #define ZIO_CHECKSUM_ON_VALUE   ZIO_CHECKSUM_FLETCHER_4
427 #define ZIO_CHECKSUM_DEFAULT    ZIO_CHECKSUM_ON
428
429 enum zio_compress {
430         ZIO_COMPRESS_INHERIT = 0,
431         ZIO_COMPRESS_ON,
432         ZIO_COMPRESS_OFF,
433         ZIO_COMPRESS_LZJB,
434         ZIO_COMPRESS_EMPTY,
435         ZIO_COMPRESS_GZIP_1,
436         ZIO_COMPRESS_GZIP_2,
437         ZIO_COMPRESS_GZIP_3,
438         ZIO_COMPRESS_GZIP_4,
439         ZIO_COMPRESS_GZIP_5,
440         ZIO_COMPRESS_GZIP_6,
441         ZIO_COMPRESS_GZIP_7,
442         ZIO_COMPRESS_GZIP_8,
443         ZIO_COMPRESS_GZIP_9,
444         ZIO_COMPRESS_ZLE,
445         ZIO_COMPRESS_LZ4,
446         ZIO_COMPRESS_FUNCTIONS
447 };
448
449 #define ZIO_COMPRESS_ON_VALUE   ZIO_COMPRESS_LZJB
450 #define ZIO_COMPRESS_DEFAULT    ZIO_COMPRESS_OFF
451
452 /* nvlist pack encoding */
453 #define NV_ENCODE_NATIVE        0
454 #define NV_ENCODE_XDR           1
455
456 typedef enum {
457         DATA_TYPE_UNKNOWN = 0,
458         DATA_TYPE_BOOLEAN,
459         DATA_TYPE_BYTE,
460         DATA_TYPE_INT16,
461         DATA_TYPE_UINT16,
462         DATA_TYPE_INT32,
463         DATA_TYPE_UINT32,
464         DATA_TYPE_INT64,
465         DATA_TYPE_UINT64,
466         DATA_TYPE_STRING,
467         DATA_TYPE_BYTE_ARRAY,
468         DATA_TYPE_INT16_ARRAY,
469         DATA_TYPE_UINT16_ARRAY,
470         DATA_TYPE_INT32_ARRAY,
471         DATA_TYPE_UINT32_ARRAY,
472         DATA_TYPE_INT64_ARRAY,
473         DATA_TYPE_UINT64_ARRAY,
474         DATA_TYPE_STRING_ARRAY,
475         DATA_TYPE_HRTIME,
476         DATA_TYPE_NVLIST,
477         DATA_TYPE_NVLIST_ARRAY,
478         DATA_TYPE_BOOLEAN_VALUE,
479         DATA_TYPE_INT8,
480         DATA_TYPE_UINT8,
481         DATA_TYPE_BOOLEAN_ARRAY,
482         DATA_TYPE_INT8_ARRAY,
483         DATA_TYPE_UINT8_ARRAY
484 } data_type_t;
485
486 /*
487  * On-disk version number.
488  */
489 #define SPA_VERSION_1                   1ULL
490 #define SPA_VERSION_2                   2ULL
491 #define SPA_VERSION_3                   3ULL
492 #define SPA_VERSION_4                   4ULL
493 #define SPA_VERSION_5                   5ULL
494 #define SPA_VERSION_6                   6ULL
495 #define SPA_VERSION_7                   7ULL
496 #define SPA_VERSION_8                   8ULL
497 #define SPA_VERSION_9                   9ULL
498 #define SPA_VERSION_10                  10ULL
499 #define SPA_VERSION_11                  11ULL
500 #define SPA_VERSION_12                  12ULL
501 #define SPA_VERSION_13                  13ULL
502 #define SPA_VERSION_14                  14ULL
503 #define SPA_VERSION_15                  15ULL
504 #define SPA_VERSION_16                  16ULL
505 #define SPA_VERSION_17                  17ULL
506 #define SPA_VERSION_18                  18ULL
507 #define SPA_VERSION_19                  19ULL
508 #define SPA_VERSION_20                  20ULL
509 #define SPA_VERSION_21                  21ULL
510 #define SPA_VERSION_22                  22ULL
511 #define SPA_VERSION_23                  23ULL
512 #define SPA_VERSION_24                  24ULL
513 #define SPA_VERSION_25                  25ULL
514 #define SPA_VERSION_26                  26ULL
515 #define SPA_VERSION_27                  27ULL
516 #define SPA_VERSION_28                  28ULL
517 #define SPA_VERSION_5000                5000ULL
518
519 /*
520  * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
521  * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
522  * and do the appropriate changes.  Also bump the version number in
523  * usr/src/grub/capability.
524  */
525 #define SPA_VERSION                     SPA_VERSION_5000
526 #define SPA_VERSION_STRING              "5000"
527
528 /*
529  * Symbolic names for the changes that caused a SPA_VERSION switch.
530  * Used in the code when checking for presence or absence of a feature.
531  * Feel free to define multiple symbolic names for each version if there
532  * were multiple changes to on-disk structures during that version.
533  *
534  * NOTE: When checking the current SPA_VERSION in your code, be sure
535  *       to use spa_version() since it reports the version of the
536  *       last synced uberblock.  Checking the in-flight version can
537  *       be dangerous in some cases.
538  */
539 #define SPA_VERSION_INITIAL             SPA_VERSION_1
540 #define SPA_VERSION_DITTO_BLOCKS        SPA_VERSION_2
541 #define SPA_VERSION_SPARES              SPA_VERSION_3
542 #define SPA_VERSION_RAID6               SPA_VERSION_3
543 #define SPA_VERSION_BPLIST_ACCOUNT      SPA_VERSION_3
544 #define SPA_VERSION_RAIDZ_DEFLATE       SPA_VERSION_3
545 #define SPA_VERSION_DNODE_BYTES         SPA_VERSION_3
546 #define SPA_VERSION_ZPOOL_HISTORY       SPA_VERSION_4
547 #define SPA_VERSION_GZIP_COMPRESSION    SPA_VERSION_5
548 #define SPA_VERSION_BOOTFS              SPA_VERSION_6
549 #define SPA_VERSION_SLOGS               SPA_VERSION_7
550 #define SPA_VERSION_DELEGATED_PERMS     SPA_VERSION_8
551 #define SPA_VERSION_FUID                SPA_VERSION_9
552 #define SPA_VERSION_REFRESERVATION      SPA_VERSION_9
553 #define SPA_VERSION_REFQUOTA            SPA_VERSION_9
554 #define SPA_VERSION_UNIQUE_ACCURATE     SPA_VERSION_9
555 #define SPA_VERSION_L2CACHE             SPA_VERSION_10
556 #define SPA_VERSION_NEXT_CLONES         SPA_VERSION_11
557 #define SPA_VERSION_ORIGIN              SPA_VERSION_11
558 #define SPA_VERSION_DSL_SCRUB           SPA_VERSION_11
559 #define SPA_VERSION_SNAP_PROPS          SPA_VERSION_12
560 #define SPA_VERSION_USED_BREAKDOWN      SPA_VERSION_13
561 #define SPA_VERSION_PASSTHROUGH_X       SPA_VERSION_14
562 #define SPA_VERSION_USERSPACE           SPA_VERSION_15
563 #define SPA_VERSION_STMF_PROP           SPA_VERSION_16
564 #define SPA_VERSION_RAIDZ3              SPA_VERSION_17
565 #define SPA_VERSION_USERREFS            SPA_VERSION_18
566 #define SPA_VERSION_HOLES               SPA_VERSION_19
567 #define SPA_VERSION_ZLE_COMPRESSION     SPA_VERSION_20
568 #define SPA_VERSION_DEDUP               SPA_VERSION_21
569 #define SPA_VERSION_RECVD_PROPS         SPA_VERSION_22
570 #define SPA_VERSION_SLIM_ZIL            SPA_VERSION_23
571 #define SPA_VERSION_SA                  SPA_VERSION_24
572 #define SPA_VERSION_SCAN                SPA_VERSION_25
573 #define SPA_VERSION_DIR_CLONES          SPA_VERSION_26
574 #define SPA_VERSION_DEADLISTS           SPA_VERSION_26
575 #define SPA_VERSION_FAST_SNAP           SPA_VERSION_27
576 #define SPA_VERSION_MULTI_REPLACE       SPA_VERSION_28
577 #define SPA_VERSION_BEFORE_FEATURES     SPA_VERSION_28
578 #define SPA_VERSION_FEATURES            SPA_VERSION_5000
579
580 #define SPA_VERSION_IS_SUPPORTED(v) \
581         (((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
582         ((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
583
584 /*
585  * The following are configuration names used in the nvlist describing a pool's
586  * configuration.
587  */
588 #define ZPOOL_CONFIG_VERSION            "version"
589 #define ZPOOL_CONFIG_POOL_NAME          "name"
590 #define ZPOOL_CONFIG_POOL_STATE         "state"
591 #define ZPOOL_CONFIG_POOL_TXG           "txg"
592 #define ZPOOL_CONFIG_POOL_GUID          "pool_guid"
593 #define ZPOOL_CONFIG_CREATE_TXG         "create_txg"
594 #define ZPOOL_CONFIG_TOP_GUID           "top_guid"
595 #define ZPOOL_CONFIG_VDEV_TREE          "vdev_tree"
596 #define ZPOOL_CONFIG_TYPE               "type"
597 #define ZPOOL_CONFIG_CHILDREN           "children"
598 #define ZPOOL_CONFIG_ID                 "id"
599 #define ZPOOL_CONFIG_GUID               "guid"
600 #define ZPOOL_CONFIG_PATH               "path"
601 #define ZPOOL_CONFIG_DEVID              "devid"
602 #define ZPOOL_CONFIG_METASLAB_ARRAY     "metaslab_array"
603 #define ZPOOL_CONFIG_METASLAB_SHIFT     "metaslab_shift"
604 #define ZPOOL_CONFIG_ASHIFT             "ashift"
605 #define ZPOOL_CONFIG_ASIZE              "asize"
606 #define ZPOOL_CONFIG_DTL                "DTL"
607 #define ZPOOL_CONFIG_STATS              "stats"
608 #define ZPOOL_CONFIG_WHOLE_DISK         "whole_disk"
609 #define ZPOOL_CONFIG_ERRCOUNT           "error_count"
610 #define ZPOOL_CONFIG_NOT_PRESENT        "not_present"
611 #define ZPOOL_CONFIG_SPARES             "spares"
612 #define ZPOOL_CONFIG_IS_SPARE           "is_spare"
613 #define ZPOOL_CONFIG_NPARITY            "nparity"
614 #define ZPOOL_CONFIG_HOSTID             "hostid"
615 #define ZPOOL_CONFIG_HOSTNAME           "hostname"
616 #define ZPOOL_CONFIG_IS_LOG             "is_log"
617 #define ZPOOL_CONFIG_TIMESTAMP          "timestamp" /* not stored on disk */
618 #define ZPOOL_CONFIG_FEATURES_FOR_READ  "features_for_read"
619
620 /*
621  * The persistent vdev state is stored as separate values rather than a single
622  * 'vdev_state' entry.  This is because a device can be in multiple states, such
623  * as offline and degraded.
624  */
625 #define ZPOOL_CONFIG_OFFLINE            "offline"
626 #define ZPOOL_CONFIG_FAULTED            "faulted"
627 #define ZPOOL_CONFIG_DEGRADED           "degraded"
628 #define ZPOOL_CONFIG_REMOVED            "removed"
629 #define ZPOOL_CONFIG_FRU                "fru"
630 #define ZPOOL_CONFIG_AUX_STATE          "aux_state"
631
632 #define VDEV_TYPE_ROOT                  "root"
633 #define VDEV_TYPE_MIRROR                "mirror"
634 #define VDEV_TYPE_REPLACING             "replacing"
635 #define VDEV_TYPE_RAIDZ                 "raidz"
636 #define VDEV_TYPE_DISK                  "disk"
637 #define VDEV_TYPE_FILE                  "file"
638 #define VDEV_TYPE_MISSING               "missing"
639 #define VDEV_TYPE_HOLE                  "hole"
640 #define VDEV_TYPE_SPARE                 "spare"
641 #define VDEV_TYPE_LOG                   "log"
642 #define VDEV_TYPE_L2CACHE               "l2cache"
643
644 /*
645  * This is needed in userland to report the minimum necessary device size.
646  */
647 #define SPA_MINDEVSIZE          (64ULL << 20)
648
649 /*
650  * The location of the pool configuration repository, shared between kernel and
651  * userland.
652  */
653 #define ZPOOL_CACHE             "/boot/zfs/zpool.cache"
654
655 /*
656  * vdev states are ordered from least to most healthy.
657  * A vdev that's CANT_OPEN or below is considered unusable.
658  */
659 typedef enum vdev_state {
660         VDEV_STATE_UNKNOWN = 0, /* Uninitialized vdev                   */
661         VDEV_STATE_CLOSED,      /* Not currently open                   */
662         VDEV_STATE_OFFLINE,     /* Not allowed to open                  */
663         VDEV_STATE_REMOVED,     /* Explicitly removed from system       */
664         VDEV_STATE_CANT_OPEN,   /* Tried to open, but failed            */
665         VDEV_STATE_FAULTED,     /* External request to fault device     */
666         VDEV_STATE_DEGRADED,    /* Replicated vdev with unhealthy kids  */
667         VDEV_STATE_HEALTHY      /* Presumed good                        */
668 } vdev_state_t;
669
670 /*
671  * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
672  * of the vdev stats structure uses these constants to distinguish why.
673  */
674 typedef enum vdev_aux {
675         VDEV_AUX_NONE,          /* no error                             */
676         VDEV_AUX_OPEN_FAILED,   /* ldi_open_*() or vn_open() failed     */
677         VDEV_AUX_CORRUPT_DATA,  /* bad label or disk contents           */
678         VDEV_AUX_NO_REPLICAS,   /* insufficient number of replicas      */
679         VDEV_AUX_BAD_GUID_SUM,  /* vdev guid sum doesn't match          */
680         VDEV_AUX_TOO_SMALL,     /* vdev size is too small               */
681         VDEV_AUX_BAD_LABEL,     /* the label is OK but invalid          */
682         VDEV_AUX_VERSION_NEWER, /* on-disk version is too new           */
683         VDEV_AUX_VERSION_OLDER, /* on-disk version is too old           */
684         VDEV_AUX_SPARED         /* hot spare used in another pool       */
685 } vdev_aux_t;
686
687 /*
688  * pool state.  The following states are written to disk as part of the normal
689  * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
690  * software abstractions used at various levels to communicate pool state.
691  */
692 typedef enum pool_state {
693         POOL_STATE_ACTIVE = 0,          /* In active use                */
694         POOL_STATE_EXPORTED,            /* Explicitly exported          */
695         POOL_STATE_DESTROYED,           /* Explicitly destroyed         */
696         POOL_STATE_SPARE,               /* Reserved for hot spare use   */
697         POOL_STATE_UNINITIALIZED,       /* Internal spa_t state         */
698         POOL_STATE_UNAVAIL,             /* Internal libzfs state        */
699         POOL_STATE_POTENTIALLY_ACTIVE   /* Internal libzfs state        */
700 } pool_state_t;
701
702 /*
703  * The uberblock version is incremented whenever an incompatible on-disk
704  * format change is made to the SPA, DMU, or ZAP.
705  *
706  * Note: the first two fields should never be moved.  When a storage pool
707  * is opened, the uberblock must be read off the disk before the version
708  * can be checked.  If the ub_version field is moved, we may not detect
709  * version mismatch.  If the ub_magic field is moved, applications that
710  * expect the magic number in the first word won't work.
711  */
712 #define UBERBLOCK_MAGIC         0x00bab10c              /* oo-ba-bloc!  */
713 #define UBERBLOCK_SHIFT         10                      /* up to 1K     */
714
715 struct uberblock {
716         uint64_t        ub_magic;       /* UBERBLOCK_MAGIC              */
717         uint64_t        ub_version;     /* SPA_VERSION                  */
718         uint64_t        ub_txg;         /* txg of last sync             */
719         uint64_t        ub_guid_sum;    /* sum of all vdev guids        */
720         uint64_t        ub_timestamp;   /* UTC time of last sync        */
721         blkptr_t        ub_rootbp;      /* MOS objset_phys_t            */
722 };
723
724 /*
725  * Flags.
726  */
727 #define DNODE_MUST_BE_ALLOCATED 1
728 #define DNODE_MUST_BE_FREE      2
729
730 /*
731  * Fixed constants.
732  */
733 #define DNODE_SHIFT             9       /* 512 bytes */
734 #define DN_MIN_INDBLKSHIFT      10      /* 1k */
735 #define DN_MAX_INDBLKSHIFT      14      /* 16k */
736 #define DNODE_BLOCK_SHIFT       14      /* 16k */
737 #define DNODE_CORE_SIZE         64      /* 64 bytes for dnode sans blkptrs */
738 #define DN_MAX_OBJECT_SHIFT     48      /* 256 trillion (zfs_fid_t limit) */
739 #define DN_MAX_OFFSET_SHIFT     64      /* 2^64 bytes in a dnode */
740
741 /*
742  * Derived constants.
743  */
744 #define DNODE_SIZE      (1 << DNODE_SHIFT)
745 #define DN_MAX_NBLKPTR  ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
746 #define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT))
747 #define DN_MAX_OBJECT   (1ULL << DN_MAX_OBJECT_SHIFT)
748
749 #define DNODES_PER_BLOCK_SHIFT  (DNODE_BLOCK_SHIFT - DNODE_SHIFT)
750 #define DNODES_PER_BLOCK        (1ULL << DNODES_PER_BLOCK_SHIFT)
751 #define DNODES_PER_LEVEL_SHIFT  (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
752
753 /* The +2 here is a cheesy way to round up */
754 #define DN_MAX_LEVELS   (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
755         (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
756
757 #define DN_BONUS(dnp)   ((void*)((dnp)->dn_bonus + \
758         (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
759
760 #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
761         (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
762
763 #define EPB(blkshift, typeshift)        (1 << (blkshift - typeshift))
764
765 /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
766 #define DNODE_FLAG_USED_BYTES           (1<<0)
767 #define DNODE_FLAG_USERUSED_ACCOUNTED   (1<<1)
768
769 /* Does dnode have a SA spill blkptr in bonus? */
770 #define DNODE_FLAG_SPILL_BLKPTR (1<<2)
771
772 typedef struct dnode_phys {
773         uint8_t dn_type;                /* dmu_object_type_t */
774         uint8_t dn_indblkshift;         /* ln2(indirect block size) */
775         uint8_t dn_nlevels;             /* 1=dn_blkptr->data blocks */
776         uint8_t dn_nblkptr;             /* length of dn_blkptr */
777         uint8_t dn_bonustype;           /* type of data in bonus buffer */
778         uint8_t dn_checksum;            /* ZIO_CHECKSUM type */
779         uint8_t dn_compress;            /* ZIO_COMPRESS type */
780         uint8_t dn_flags;               /* DNODE_FLAG_* */
781         uint16_t dn_datablkszsec;       /* data block size in 512b sectors */
782         uint16_t dn_bonuslen;           /* length of dn_bonus */
783         uint8_t dn_pad2[4];
784
785         /* accounting is protected by dn_dirty_mtx */
786         uint64_t dn_maxblkid;           /* largest allocated block ID */
787         uint64_t dn_used;               /* bytes (or sectors) of disk space */
788
789         uint64_t dn_pad3[4];
790
791         blkptr_t dn_blkptr[1];
792         uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)];
793         blkptr_t dn_spill;
794 } dnode_phys_t;
795
796 typedef enum dmu_object_type {
797         DMU_OT_NONE,
798         /* general: */
799         DMU_OT_OBJECT_DIRECTORY,        /* ZAP */
800         DMU_OT_OBJECT_ARRAY,            /* UINT64 */
801         DMU_OT_PACKED_NVLIST,           /* UINT8 (XDR by nvlist_pack/unpack) */
802         DMU_OT_PACKED_NVLIST_SIZE,      /* UINT64 */
803         DMU_OT_BPLIST,                  /* UINT64 */
804         DMU_OT_BPLIST_HDR,              /* UINT64 */
805         /* spa: */
806         DMU_OT_SPACE_MAP_HEADER,        /* UINT64 */
807         DMU_OT_SPACE_MAP,               /* UINT64 */
808         /* zil: */
809         DMU_OT_INTENT_LOG,              /* UINT64 */
810         /* dmu: */
811         DMU_OT_DNODE,                   /* DNODE */
812         DMU_OT_OBJSET,                  /* OBJSET */
813         /* dsl: */
814         DMU_OT_DSL_DIR,                 /* UINT64 */
815         DMU_OT_DSL_DIR_CHILD_MAP,       /* ZAP */
816         DMU_OT_DSL_DS_SNAP_MAP,         /* ZAP */
817         DMU_OT_DSL_PROPS,               /* ZAP */
818         DMU_OT_DSL_DATASET,             /* UINT64 */
819         /* zpl: */
820         DMU_OT_ZNODE,                   /* ZNODE */
821         DMU_OT_OLDACL,                  /* Old ACL */
822         DMU_OT_PLAIN_FILE_CONTENTS,     /* UINT8 */
823         DMU_OT_DIRECTORY_CONTENTS,      /* ZAP */
824         DMU_OT_MASTER_NODE,             /* ZAP */
825         DMU_OT_UNLINKED_SET,            /* ZAP */
826         /* zvol: */
827         DMU_OT_ZVOL,                    /* UINT8 */
828         DMU_OT_ZVOL_PROP,               /* ZAP */
829         /* other; for testing only! */
830         DMU_OT_PLAIN_OTHER,             /* UINT8 */
831         DMU_OT_UINT64_OTHER,            /* UINT64 */
832         DMU_OT_ZAP_OTHER,               /* ZAP */
833         /* new object types: */
834         DMU_OT_ERROR_LOG,               /* ZAP */
835         DMU_OT_SPA_HISTORY,             /* UINT8 */
836         DMU_OT_SPA_HISTORY_OFFSETS,     /* spa_his_phys_t */
837         DMU_OT_POOL_PROPS,              /* ZAP */
838         DMU_OT_DSL_PERMS,               /* ZAP */
839         DMU_OT_ACL,                     /* ACL */
840         DMU_OT_SYSACL,                  /* SYSACL */
841         DMU_OT_FUID,                    /* FUID table (Packed NVLIST UINT8) */
842         DMU_OT_FUID_SIZE,               /* FUID table size UINT64 */
843         DMU_OT_NEXT_CLONES,             /* ZAP */
844         DMU_OT_SCAN_QUEUE,              /* ZAP */
845         DMU_OT_USERGROUP_USED,          /* ZAP */
846         DMU_OT_USERGROUP_QUOTA,         /* ZAP */
847         DMU_OT_USERREFS,                /* ZAP */
848         DMU_OT_DDT_ZAP,                 /* ZAP */
849         DMU_OT_DDT_STATS,               /* ZAP */
850         DMU_OT_SA,                      /* System attr */
851         DMU_OT_SA_MASTER_NODE,          /* ZAP */
852         DMU_OT_SA_ATTR_REGISTRATION,    /* ZAP */
853         DMU_OT_SA_ATTR_LAYOUTS,         /* ZAP */
854         DMU_OT_SCAN_XLATE,              /* ZAP */
855         DMU_OT_DEDUP,                   /* fake dedup BP from ddt_bp_create() */
856         DMU_OT_NUMTYPES
857 } dmu_object_type_t;
858
859 typedef enum dmu_objset_type {
860         DMU_OST_NONE,
861         DMU_OST_META,
862         DMU_OST_ZFS,
863         DMU_OST_ZVOL,
864         DMU_OST_OTHER,                  /* For testing only! */
865         DMU_OST_ANY,                    /* Be careful! */
866         DMU_OST_NUMTYPES
867 } dmu_objset_type_t;
868
869 /*
870  * header for all bonus and spill buffers.
871  * The header has a fixed portion with a variable number
872  * of "lengths" depending on the number of variable sized
873  * attribues which are determined by the "layout number"
874  */
875
876 #define SA_MAGIC        0x2F505A  /* ZFS SA */
877 typedef struct sa_hdr_phys {
878         uint32_t sa_magic;
879         uint16_t sa_layout_info;  /* Encoded with hdrsize and layout number */
880         uint16_t sa_lengths[1]; /* optional sizes for variable length attrs */
881         /* ... Data follows the lengths.  */
882 } sa_hdr_phys_t;
883
884 /*
885  * sa_hdr_phys -> sa_layout_info
886  *
887  * 16      10       0
888  * +--------+-------+
889  * | hdrsz  |layout |
890  * +--------+-------+
891  *
892  * Bits 0-10 are the layout number
893  * Bits 11-16 are the size of the header.
894  * The hdrsize is the number * 8
895  *
896  * For example.
897  * hdrsz of 1 ==> 8 byte header
898  *          2 ==> 16 byte header
899  *
900  */
901
902 #define SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
903 #define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0)
904 #define SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
905 { \
906         BF32_SET_SB(x, 10, 6, 3, 0, size); \
907         BF32_SET(x, 0, 10, num); \
908 }
909
910 #define SA_MODE_OFFSET          0
911 #define SA_SIZE_OFFSET          8
912 #define SA_GEN_OFFSET           16
913 #define SA_UID_OFFSET           24
914 #define SA_GID_OFFSET           32
915 #define SA_PARENT_OFFSET        40
916
917 /*
918  * Intent log header - this on disk structure holds fields to manage
919  * the log.  All fields are 64 bit to easily handle cross architectures.
920  */
921 typedef struct zil_header {
922         uint64_t zh_claim_txg;  /* txg in which log blocks were claimed */
923         uint64_t zh_replay_seq; /* highest replayed sequence number */
924         blkptr_t zh_log;        /* log chain */
925         uint64_t zh_claim_seq;  /* highest claimed sequence number */
926         uint64_t zh_pad[5];
927 } zil_header_t;
928
929 #define OBJSET_PHYS_SIZE 2048
930
931 typedef struct objset_phys {
932         dnode_phys_t os_meta_dnode;
933         zil_header_t os_zil_header;
934         uint64_t os_type;
935         uint64_t os_flags;
936         char os_pad[OBJSET_PHYS_SIZE - sizeof (dnode_phys_t)*3 -
937             sizeof (zil_header_t) - sizeof (uint64_t)*2];
938         dnode_phys_t os_userused_dnode;
939         dnode_phys_t os_groupused_dnode;
940 } objset_phys_t;
941
942 typedef struct dsl_dir_phys {
943         uint64_t dd_creation_time; /* not actually used */
944         uint64_t dd_head_dataset_obj;
945         uint64_t dd_parent_obj;
946         uint64_t dd_clone_parent_obj;
947         uint64_t dd_child_dir_zapobj;
948         /*
949          * how much space our children are accounting for; for leaf
950          * datasets, == physical space used by fs + snaps
951          */
952         uint64_t dd_used_bytes;
953         uint64_t dd_compressed_bytes;
954         uint64_t dd_uncompressed_bytes;
955         /* Administrative quota setting */
956         uint64_t dd_quota;
957         /* Administrative reservation setting */
958         uint64_t dd_reserved;
959         uint64_t dd_props_zapobj;
960         uint64_t dd_pad[21]; /* pad out to 256 bytes for good measure */
961 } dsl_dir_phys_t;
962
963 typedef struct dsl_dataset_phys {
964         uint64_t ds_dir_obj;
965         uint64_t ds_prev_snap_obj;
966         uint64_t ds_prev_snap_txg;
967         uint64_t ds_next_snap_obj;
968         uint64_t ds_snapnames_zapobj;   /* zap obj of snaps; ==0 for snaps */
969         uint64_t ds_num_children;       /* clone/snap children; ==0 for head */
970         uint64_t ds_creation_time;      /* seconds since 1970 */
971         uint64_t ds_creation_txg;
972         uint64_t ds_deadlist_obj;
973         uint64_t ds_used_bytes;
974         uint64_t ds_compressed_bytes;
975         uint64_t ds_uncompressed_bytes;
976         uint64_t ds_unique_bytes;       /* only relevant to snapshots */
977         /*
978          * The ds_fsid_guid is a 56-bit ID that can change to avoid
979          * collisions.  The ds_guid is a 64-bit ID that will never
980          * change, so there is a small probability that it will collide.
981          */
982         uint64_t ds_fsid_guid;
983         uint64_t ds_guid;
984         uint64_t ds_flags;
985         blkptr_t ds_bp;
986         uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */
987 } dsl_dataset_phys_t;
988
989 /*
990  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
991  */
992 #define DMU_POOL_DIRECTORY_OBJECT       1
993 #define DMU_POOL_CONFIG                 "config"
994 #define DMU_POOL_ROOT_DATASET           "root_dataset"
995 #define DMU_POOL_SYNC_BPLIST            "sync_bplist"
996 #define DMU_POOL_ERRLOG_SCRUB           "errlog_scrub"
997 #define DMU_POOL_ERRLOG_LAST            "errlog_last"
998 #define DMU_POOL_SPARES                 "spares"
999 #define DMU_POOL_DEFLATE                "deflate"
1000 #define DMU_POOL_HISTORY                "history"
1001 #define DMU_POOL_PROPS                  "pool_props"
1002
1003 #define ZAP_MAGIC 0x2F52AB2ABULL
1004
1005 #define FZAP_BLOCK_SHIFT(zap)   ((zap)->zap_block_shift)
1006
1007 #define ZAP_MAXCD               (uint32_t)(-1)
1008 #define ZAP_HASHBITS            28
1009 #define MZAP_ENT_LEN            64
1010 #define MZAP_NAME_LEN           (MZAP_ENT_LEN - 8 - 4 - 2)
1011 #define MZAP_MAX_BLKSHIFT       SPA_MAXBLOCKSHIFT
1012 #define MZAP_MAX_BLKSZ          (1 << MZAP_MAX_BLKSHIFT)
1013
1014 typedef struct mzap_ent_phys {
1015         uint64_t mze_value;
1016         uint32_t mze_cd;
1017         uint16_t mze_pad;       /* in case we want to chain them someday */
1018         char mze_name[MZAP_NAME_LEN];
1019 } mzap_ent_phys_t;
1020
1021 typedef struct mzap_phys {
1022         uint64_t mz_block_type; /* ZBT_MICRO */
1023         uint64_t mz_salt;
1024         uint64_t mz_pad[6];
1025         mzap_ent_phys_t mz_chunk[1];
1026         /* actually variable size depending on block size */
1027 } mzap_phys_t;
1028
1029 /*
1030  * The (fat) zap is stored in one object. It is an array of
1031  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
1032  *
1033  * ptrtbl fits in first block:
1034  *      [zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
1035  *
1036  * ptrtbl too big for first block:
1037  *      [zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
1038  *
1039  */
1040
1041 #define ZBT_LEAF                ((1ULL << 63) + 0)
1042 #define ZBT_HEADER              ((1ULL << 63) + 1)
1043 #define ZBT_MICRO               ((1ULL << 63) + 3)
1044 /* any other values are ptrtbl blocks */
1045
1046 /*
1047  * the embedded pointer table takes up half a block:
1048  * block size / entry size (2^3) / 2
1049  */
1050 #define ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
1051
1052 /*
1053  * The embedded pointer table starts half-way through the block.  Since
1054  * the pointer table itself is half the block, it starts at (64-bit)
1055  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
1056  */
1057 #define ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
1058         ((uint64_t *)(zap)->zap_phys) \
1059         [(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
1060
1061 /*
1062  * TAKE NOTE:
1063  * If zap_phys_t is modified, zap_byteswap() must be modified.
1064  */
1065 typedef struct zap_phys {
1066         uint64_t zap_block_type;        /* ZBT_HEADER */
1067         uint64_t zap_magic;             /* ZAP_MAGIC */
1068
1069         struct zap_table_phys {
1070                 uint64_t zt_blk;        /* starting block number */
1071                 uint64_t zt_numblks;    /* number of blocks */
1072                 uint64_t zt_shift;      /* bits to index it */
1073                 uint64_t zt_nextblk;    /* next (larger) copy start block */
1074                 uint64_t zt_blks_copied; /* number source blocks copied */
1075         } zap_ptrtbl;
1076
1077         uint64_t zap_freeblk;           /* the next free block */
1078         uint64_t zap_num_leafs;         /* number of leafs */
1079         uint64_t zap_num_entries;       /* number of entries */
1080         uint64_t zap_salt;              /* salt to stir into hash function */
1081         /*
1082          * This structure is followed by padding, and then the embedded
1083          * pointer table.  The embedded pointer table takes up second
1084          * half of the block.  It is accessed using the
1085          * ZAP_EMBEDDED_PTRTBL_ENT() macro.
1086          */
1087 } zap_phys_t;
1088
1089 typedef struct zap_table_phys zap_table_phys_t;
1090
1091 typedef struct fat_zap {
1092         int zap_block_shift;                    /* block size shift */
1093         zap_phys_t *zap_phys;
1094 } fat_zap_t;
1095
1096 #define ZAP_LEAF_MAGIC 0x2AB1EAF
1097
1098 /* chunk size = 24 bytes */
1099 #define ZAP_LEAF_CHUNKSIZE 24
1100
1101 /*
1102  * The amount of space available for chunks is:
1103  * block size (1<<l->l_bs) - hash entry size (2) * number of hash
1104  * entries - header space (2*chunksize)
1105  */
1106 #define ZAP_LEAF_NUMCHUNKS(l) \
1107         (((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
1108         ZAP_LEAF_CHUNKSIZE - 2)
1109
1110 /*
1111  * The amount of space within the chunk available for the array is:
1112  * chunk size - space for type (1) - space for next pointer (2)
1113  */
1114 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
1115
1116 #define ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
1117         (((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
1118
1119 /*
1120  * Low water mark:  when there are only this many chunks free, start
1121  * growing the ptrtbl.  Ideally, this should be larger than a
1122  * "reasonably-sized" entry.  20 chunks is more than enough for the
1123  * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
1124  * while still being only around 3% for 16k blocks.
1125  */
1126 #define ZAP_LEAF_LOW_WATER (20)
1127
1128 /*
1129  * The leaf hash table has block size / 2^5 (32) number of entries,
1130  * which should be more than enough for the maximum number of entries,
1131  * which is less than block size / CHUNKSIZE (24) / minimum number of
1132  * chunks per entry (3).
1133  */
1134 #define ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
1135 #define ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
1136
1137 /*
1138  * The chunks start immediately after the hash table.  The end of the
1139  * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
1140  * chunk_t.
1141  */
1142 #define ZAP_LEAF_CHUNK(l, idx) \
1143         ((zap_leaf_chunk_t *) \
1144         ((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
1145 #define ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
1146
1147 typedef enum zap_chunk_type {
1148         ZAP_CHUNK_FREE = 253,
1149         ZAP_CHUNK_ENTRY = 252,
1150         ZAP_CHUNK_ARRAY = 251,
1151         ZAP_CHUNK_TYPE_MAX = 250
1152 } zap_chunk_type_t;
1153
1154 /*
1155  * TAKE NOTE:
1156  * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
1157  */
1158 typedef struct zap_leaf_phys {
1159         struct zap_leaf_header {
1160                 uint64_t lh_block_type;         /* ZBT_LEAF */
1161                 uint64_t lh_pad1;
1162                 uint64_t lh_prefix;             /* hash prefix of this leaf */
1163                 uint32_t lh_magic;              /* ZAP_LEAF_MAGIC */
1164                 uint16_t lh_nfree;              /* number free chunks */
1165                 uint16_t lh_nentries;           /* number of entries */
1166                 uint16_t lh_prefix_len;         /* num bits used to id this */
1167
1168 /* above is accessable to zap, below is zap_leaf private */
1169
1170                 uint16_t lh_freelist;           /* chunk head of free list */
1171                 uint8_t lh_pad2[12];
1172         } l_hdr; /* 2 24-byte chunks */
1173
1174         /*
1175          * The header is followed by a hash table with
1176          * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
1177          * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
1178          * zap_leaf_chunk structures.  These structures are accessed
1179          * with the ZAP_LEAF_CHUNK() macro.
1180          */
1181
1182         uint16_t l_hash[1];
1183 } zap_leaf_phys_t;
1184
1185 typedef union zap_leaf_chunk {
1186         struct zap_leaf_entry {
1187                 uint8_t le_type;                /* always ZAP_CHUNK_ENTRY */
1188                 uint8_t le_value_intlen;        /* size of ints */
1189                 uint16_t le_next;               /* next entry in hash chain */
1190                 uint16_t le_name_chunk;         /* first chunk of the name */
1191                 uint16_t le_name_numints;       /* bytes in name, incl null */
1192                 uint16_t le_value_chunk;        /* first chunk of the value */
1193                 uint16_t le_value_numints;      /* value length in ints */
1194                 uint32_t le_cd;                 /* collision differentiator */
1195                 uint64_t le_hash;               /* hash value of the name */
1196         } l_entry;
1197         struct zap_leaf_array {
1198                 uint8_t la_type;                /* always ZAP_CHUNK_ARRAY */
1199                 uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
1200                 uint16_t la_next;               /* next blk or CHAIN_END */
1201         } l_array;
1202         struct zap_leaf_free {
1203                 uint8_t lf_type;                /* always ZAP_CHUNK_FREE */
1204                 uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
1205                 uint16_t lf_next;       /* next in free list, or CHAIN_END */
1206         } l_free;
1207 } zap_leaf_chunk_t;
1208
1209 typedef struct zap_leaf {
1210         int l_bs;                       /* block size shift */
1211         zap_leaf_phys_t *l_phys;
1212 } zap_leaf_t;
1213
1214 /*
1215  * Define special zfs pflags
1216  */
1217 #define ZFS_XATTR       0x1             /* is an extended attribute */
1218 #define ZFS_INHERIT_ACE 0x2             /* ace has inheritable ACEs */
1219 #define ZFS_ACL_TRIVIAL 0x4             /* files ACL is trivial */
1220
1221 #define MASTER_NODE_OBJ 1
1222
1223 /*
1224  * special attributes for master node.
1225  */
1226
1227 #define ZFS_FSID                "FSID"
1228 #define ZFS_UNLINKED_SET        "DELETE_QUEUE"
1229 #define ZFS_ROOT_OBJ            "ROOT"
1230 #define ZPL_VERSION_OBJ         "VERSION"
1231 #define ZFS_PROP_BLOCKPERPAGE   "BLOCKPERPAGE"
1232 #define ZFS_PROP_NOGROWBLOCKS   "NOGROWBLOCKS"
1233
1234 #define ZFS_FLAG_BLOCKPERPAGE   0x1
1235 #define ZFS_FLAG_NOGROWBLOCKS   0x2
1236
1237 /*
1238  * ZPL version - rev'd whenever an incompatible on-disk format change
1239  * occurs.  Independent of SPA/DMU/ZAP versioning.
1240  */
1241
1242 #define ZPL_VERSION             1ULL
1243
1244 /*
1245  * The directory entry has the type (currently unused on Solaris) in the
1246  * top 4 bits, and the object number in the low 48 bits.  The "middle"
1247  * 12 bits are unused.
1248  */
1249 #define ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4)
1250 #define ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48)
1251 #define ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj)
1252
1253 typedef struct ace {
1254         uid_t           a_who;          /* uid or gid */
1255         uint32_t        a_access_mask;  /* read,write,... */
1256         uint16_t        a_flags;        /* see below */
1257         uint16_t        a_type;         /* allow or deny */
1258 } ace_t;
1259
1260 #define ACE_SLOT_CNT    6
1261
1262 typedef struct zfs_znode_acl {
1263         uint64_t        z_acl_extern_obj;         /* ext acl pieces */
1264         uint32_t        z_acl_count;              /* Number of ACEs */
1265         uint16_t        z_acl_version;            /* acl version */
1266         uint16_t        z_acl_pad;                /* pad */
1267         ace_t           z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
1268 } zfs_znode_acl_t;
1269
1270 /*
1271  * This is the persistent portion of the znode.  It is stored
1272  * in the "bonus buffer" of the file.  Short symbolic links
1273  * are also stored in the bonus buffer.
1274  */
1275 typedef struct znode_phys {
1276         uint64_t zp_atime[2];           /*  0 - last file access time */
1277         uint64_t zp_mtime[2];           /* 16 - last file modification time */
1278         uint64_t zp_ctime[2];           /* 32 - last file change time */
1279         uint64_t zp_crtime[2];          /* 48 - creation time */
1280         uint64_t zp_gen;                /* 64 - generation (txg of creation) */
1281         uint64_t zp_mode;               /* 72 - file mode bits */
1282         uint64_t zp_size;               /* 80 - size of file */
1283         uint64_t zp_parent;             /* 88 - directory parent (`..') */
1284         uint64_t zp_links;              /* 96 - number of links to file */
1285         uint64_t zp_xattr;              /* 104 - DMU object for xattrs */
1286         uint64_t zp_rdev;               /* 112 - dev_t for VBLK & VCHR files */
1287         uint64_t zp_flags;              /* 120 - persistent flags */
1288         uint64_t zp_uid;                /* 128 - file owner */
1289         uint64_t zp_gid;                /* 136 - owning group */
1290         uint64_t zp_pad[4];             /* 144 - future */
1291         zfs_znode_acl_t zp_acl;         /* 176 - 263 ACL */
1292         /*
1293          * Data may pad out any remaining bytes in the znode buffer, eg:
1294          *
1295          * |<---------------------- dnode_phys (512) ------------------------>|
1296          * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->|
1297          *                      |<---- znode (264) ---->|<---- data (56) ---->|
1298          *
1299          * At present, we only use this space to store symbolic links.
1300          */
1301 } znode_phys_t;
1302
1303 /*
1304  * In-core vdev representation.
1305  */
1306 struct vdev;
1307 typedef int vdev_phys_read_t(struct vdev *vdev, void *priv,
1308     off_t offset, void *buf, size_t bytes);
1309 typedef int vdev_read_t(struct vdev *vdev, const blkptr_t *bp,
1310     void *buf, off_t offset, size_t bytes);
1311
1312 typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t;
1313
1314 typedef struct vdev {
1315         STAILQ_ENTRY(vdev) v_childlink; /* link in parent's child list */
1316         STAILQ_ENTRY(vdev) v_alllink;   /* link in global vdev list */
1317         vdev_list_t     v_children;     /* children of this vdev */
1318         const char      *v_name;        /* vdev name */
1319         uint64_t        v_guid;         /* vdev guid */
1320         int             v_id;           /* index in parent */
1321         int             v_ashift;       /* offset to block shift */
1322         int             v_nparity;      /* # parity for raidz */
1323         struct vdev     *v_top;         /* parent vdev */
1324         int             v_nchildren;    /* # children */
1325         vdev_state_t    v_state;        /* current state */
1326         vdev_phys_read_t *v_phys_read;  /* read from raw leaf vdev */
1327         vdev_read_t     *v_read;        /* read from vdev */
1328         void            *v_read_priv;   /* private data for read function */
1329 } vdev_t;
1330
1331 /*
1332  * In-core pool representation.
1333  */
1334 typedef STAILQ_HEAD(spa_list, spa) spa_list_t;
1335
1336 typedef struct spa {
1337         STAILQ_ENTRY(spa) spa_link;     /* link in global pool list */
1338         char            *spa_name;      /* pool name */
1339         uint64_t        spa_guid;       /* pool guid */
1340         uint64_t        spa_txg;        /* most recent transaction */
1341         struct uberblock spa_uberblock; /* best uberblock so far */
1342         vdev_list_t     spa_vdevs;      /* list of all toplevel vdevs */
1343         objset_phys_t   spa_mos;        /* MOS for this pool */
1344         int             spa_inited;     /* initialized */
1345 } spa_t;