1b1b8ab34Slling /*
2b1b8ab34Slling  *  GRUB  --  GRand Unified Bootloader
3b1b8ab34Slling  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
4b1b8ab34Slling  *
5b1b8ab34Slling  *  This program is free software; you can redistribute it and/or modify
6b1b8ab34Slling  *  it under the terms of the GNU General Public License as published by
7b1b8ab34Slling  *  the Free Software Foundation; either version 2 of the License, or
8b1b8ab34Slling  *  (at your option) any later version.
9b1b8ab34Slling  *
10b1b8ab34Slling  *  This program is distributed in the hope that it will be useful,
11b1b8ab34Slling  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12b1b8ab34Slling  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13b1b8ab34Slling  *  GNU General Public License for more details.
14b1b8ab34Slling  *
15b1b8ab34Slling  *  You should have received a copy of the GNU General Public License
16b1b8ab34Slling  *  along with this program; if not, write to the Free Software
17b1b8ab34Slling  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18b1b8ab34Slling  */
19*ad135b5dSChristopher Siden 
20b1b8ab34Slling /*
216e1f5caaSNeil Perrin  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
22b1b8ab34Slling  * Use is subject to license terms.
23b1b8ab34Slling  */
24*ad135b5dSChristopher Siden 
25*ad135b5dSChristopher Siden /*
26*ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
27*ad135b5dSChristopher Siden  */
28*ad135b5dSChristopher Siden 
29b1b8ab34Slling #ifndef _FSYS_ZFS_H
30b1b8ab34Slling #define	_FSYS_ZFS_H
31b1b8ab34Slling 
32b1b8ab34Slling #ifdef	FSYS_ZFS
33b1b8ab34Slling 
342dd5848fSMark Johnson #ifndef	FSIMAGE
35b1b8ab34Slling typedef unsigned long long uint64_t;
36b1b8ab34Slling typedef unsigned int uint32_t;
37b1b8ab34Slling typedef unsigned short uint16_t;
38b1b8ab34Slling typedef unsigned char uint8_t;
39b1b8ab34Slling typedef unsigned char uchar_t;
40b1b8ab34Slling 
41b1b8ab34Slling #if defined(_LP64) || defined(_I32LPx)
42b1b8ab34Slling typedef	unsigned long size_t;
43b1b8ab34Slling #else
44b1b8ab34Slling typedef	unsigned int size_t;
45b1b8ab34Slling #endif
462dd5848fSMark Johnson #else
472dd5848fSMark Johnson #include "fsi_zfs.h"
482dd5848fSMark Johnson #endif	/* !FSIMAGE */
49b1b8ab34Slling 
50b1b8ab34Slling #include <zfs-include/zfs.h>
51b1b8ab34Slling #include <zfs-include/dmu.h>
52b1b8ab34Slling #include <zfs-include/spa.h>
53b1b8ab34Slling #include <zfs-include/zio.h>
54b1b8ab34Slling #include <zfs-include/zio_checksum.h>
55b1b8ab34Slling #include <zfs-include/vdev_impl.h>
56b1b8ab34Slling #include <zfs-include/zap_impl.h>
57b1b8ab34Slling #include <zfs-include/zap_leaf.h>
58b1b8ab34Slling #include <zfs-include/uberblock_impl.h>
59b1b8ab34Slling #include <zfs-include/dnode.h>
60b1b8ab34Slling #include <zfs-include/dsl_dir.h>
61b1b8ab34Slling #include <zfs-include/zfs_acl.h>
62b1b8ab34Slling #include <zfs-include/zfs_znode.h>
63b1b8ab34Slling #include <zfs-include/dsl_dataset.h>
64b1b8ab34Slling #include <zfs-include/zil.h>
65b1b8ab34Slling #include <zfs-include/dmu_objset.h>
660a586ceaSMark Shellenbaum #include <zfs-include/sa_impl.h>
67b1b8ab34Slling 
68b1b8ab34Slling /*
69b1b8ab34Slling  * Global Memory addresses to store MOS and DNODE data
70b1b8ab34Slling  */
71843e1988Sjohnlev #define	MOS		((dnode_phys_t *)\
72bc896b5aSjohnlev 	(RAW_ADDR((mbi.mem_upper << 10) + 0x100000) - ZFS_SCRATCH_SIZE))
73ae8180dbSlling #define	DNODE		(MOS+1) /* move sizeof(dnode_phys_t) bytes */
74ae8180dbSlling #define	ZFS_SCRATCH	((char *)(DNODE+1))
75b1b8ab34Slling 
76b1b8ab34Slling /*
77b1b8ab34Slling  * Verify dnode type.
78b1b8ab34Slling  * Can only be used in functions returning non-0 for failure.
79b1b8ab34Slling  */
80b1b8ab34Slling #define	VERIFY_DN_TYPE(dnp, type) \
81b1b8ab34Slling 	if (type && (dnp)->dn_type != type) { \
82b1b8ab34Slling 		return (ERR_FSYS_CORRUPT); \
83b1b8ab34Slling 	}
84b1b8ab34Slling 
85b1b8ab34Slling /*
86b1b8ab34Slling  * Verify object set type.
87b1b8ab34Slling  * Can only be used in functions returning 0 for failure.
88b1b8ab34Slling  */
89b1b8ab34Slling #define	VERIFY_OS_TYPE(osp, type) \
90b1b8ab34Slling 	if (type && (osp)->os_type != type) { \
91b1b8ab34Slling 		errnum = ERR_FSYS_CORRUPT; \
92b1b8ab34Slling 		return (0); \
93b1b8ab34Slling 	}
94b1b8ab34Slling 
95b1b8ab34Slling #define	ZPOOL_PROP_BOOTFS		"bootfs"
96b1b8ab34Slling 
97b1b8ab34Slling /* General macros */
98b1b8ab34Slling #define	BSWAP_8(x)	((x) & 0xff)
99b1b8ab34Slling #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
100b1b8ab34Slling #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
101b1b8ab34Slling #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
102b1b8ab34Slling #define	P2ROUNDUP(x, align)	(-(-(x) & -(align)))
103b1b8ab34Slling 
104b1b8ab34Slling typedef struct uberblock uberblock_t;
105b1b8ab34Slling 
106b1b8ab34Slling /*
107b1b8ab34Slling  * Macros to get fields in a bp or DVA.
108b1b8ab34Slling  */
109b1b8ab34Slling #define	P2PHASE(x, align)		((x) & ((align) - 1))
110b1b8ab34Slling #define	DVA_OFFSET_TO_PHYS_SECTOR(offset) \
111b1b8ab34Slling 	((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT)
112b1b8ab34Slling 
113e23347b1SEric Taylor /*
114e23347b1SEric Taylor  * return x rounded down to an align boundary
115e23347b1SEric Taylor  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
116e23347b1SEric Taylor  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
117e23347b1SEric Taylor  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
118e23347b1SEric Taylor  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
119e23347b1SEric Taylor  */
120e23347b1SEric Taylor #define	P2ALIGN(x, align)		((x) & -(align))
121e23347b1SEric Taylor 
122b1b8ab34Slling /*
123b1b8ab34Slling  * For nvlist manipulation. (from nvpair.h)
124b1b8ab34Slling  */
125b1b8ab34Slling #define	NV_ENCODE_NATIVE	0
126b1b8ab34Slling #define	NV_ENCODE_XDR		1
127b1b8ab34Slling #define	HOST_ENDIAN		1	/* for x86 machine */
128*ad135b5dSChristopher Siden typedef enum {
129*ad135b5dSChristopher Siden 	DATA_TYPE_UNKNOWN = 0,
130*ad135b5dSChristopher Siden 	DATA_TYPE_BOOLEAN,
131*ad135b5dSChristopher Siden 	DATA_TYPE_BYTE,
132*ad135b5dSChristopher Siden 	DATA_TYPE_INT16,
133*ad135b5dSChristopher Siden 	DATA_TYPE_UINT16,
134*ad135b5dSChristopher Siden 	DATA_TYPE_INT32,
135*ad135b5dSChristopher Siden 	DATA_TYPE_UINT32,
136*ad135b5dSChristopher Siden 	DATA_TYPE_INT64,
137*ad135b5dSChristopher Siden 	DATA_TYPE_UINT64,
138*ad135b5dSChristopher Siden 	DATA_TYPE_STRING,
139*ad135b5dSChristopher Siden 	DATA_TYPE_BYTE_ARRAY,
140*ad135b5dSChristopher Siden 	DATA_TYPE_INT16_ARRAY,
141*ad135b5dSChristopher Siden 	DATA_TYPE_UINT16_ARRAY,
142*ad135b5dSChristopher Siden 	DATA_TYPE_INT32_ARRAY,
143*ad135b5dSChristopher Siden 	DATA_TYPE_UINT32_ARRAY,
144*ad135b5dSChristopher Siden 	DATA_TYPE_INT64_ARRAY,
145*ad135b5dSChristopher Siden 	DATA_TYPE_UINT64_ARRAY,
146*ad135b5dSChristopher Siden 	DATA_TYPE_STRING_ARRAY,
147*ad135b5dSChristopher Siden 	DATA_TYPE_HRTIME,
148*ad135b5dSChristopher Siden 	DATA_TYPE_NVLIST,
149*ad135b5dSChristopher Siden 	DATA_TYPE_NVLIST_ARRAY,
150*ad135b5dSChristopher Siden 	DATA_TYPE_BOOLEAN_VALUE,
151*ad135b5dSChristopher Siden 	DATA_TYPE_INT8,
152*ad135b5dSChristopher Siden 	DATA_TYPE_UINT8,
153*ad135b5dSChristopher Siden 	DATA_TYPE_BOOLEAN_ARRAY,
154*ad135b5dSChristopher Siden 	DATA_TYPE_INT8_ARRAY,
155*ad135b5dSChristopher Siden 	DATA_TYPE_UINT8_ARRAY,
156*ad135b5dSChristopher Siden 	DATA_TYPE_DOUBLE
157*ad135b5dSChristopher Siden } data_type_t;
158b1b8ab34Slling 
159b1b8ab34Slling /*
160b1b8ab34Slling  * Decompression Entry - lzjb
161b1b8ab34Slling  */
162b1b8ab34Slling #ifndef	NBBY
163b1b8ab34Slling #define	NBBY	8
164b1b8ab34Slling #endif
165b1b8ab34Slling 
166b1b8ab34Slling typedef int zfs_decomp_func_t(void *s_start, void *d_start, size_t s_len,
167b1b8ab34Slling 			size_t d_len);
168b1b8ab34Slling typedef struct decomp_entry {
169b1b8ab34Slling 	char *name;
170b1b8ab34Slling 	zfs_decomp_func_t *decomp_func;
171b1b8ab34Slling } decomp_entry_t;
172b1b8ab34Slling 
173b1b8ab34Slling /*
174b1b8ab34Slling  * FAT ZAP data structures
175b1b8ab34Slling  */
176b1b8ab34Slling #define	ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
177b1b8ab34Slling #define	ZAP_HASH_IDX(hash, n)	(((n) == 0) ? 0 : ((hash) >> (64 - (n))))
178b1b8ab34Slling #define	CHAIN_END	0xffff	/* end of the chunk chain */
179b1b8ab34Slling 
180b1b8ab34Slling /*
181b1b8ab34Slling  * The amount of space within the chunk available for the array is:
182b1b8ab34Slling  * chunk size - space for type (1) - space for next pointer (2)
183b1b8ab34Slling  */
184b1b8ab34Slling #define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
185b1b8ab34Slling 
186b1b8ab34Slling #define	ZAP_LEAF_HASH_SHIFT(bs)	(bs - 5)
187b1b8ab34Slling #define	ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs))
188b1b8ab34Slling #define	LEAF_HASH(bs, h) \
189b1b8ab34Slling 	((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \
190b1b8ab34Slling 	((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len)))
191b1b8ab34Slling 
192b1b8ab34Slling /*
193b1b8ab34Slling  * The amount of space available for chunks is:
194b1b8ab34Slling  * block size shift - hash entry size (2) * number of hash
195b1b8ab34Slling  * entries - header space (2*chunksize)
196b1b8ab34Slling  */
197b1b8ab34Slling #define	ZAP_LEAF_NUMCHUNKS(bs) \
198b1b8ab34Slling 	(((1<<bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(bs)) / \
199b1b8ab34Slling 	ZAP_LEAF_CHUNKSIZE - 2)
200b1b8ab34Slling 
201b1b8ab34Slling /*
202b1b8ab34Slling  * The chunks start immediately after the hash table.  The end of the
203b1b8ab34Slling  * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
204b1b8ab34Slling  * chunk_t.
205b1b8ab34Slling  */
206b1b8ab34Slling #define	ZAP_LEAF_CHUNK(l, bs, idx) \
207b1b8ab34Slling 	((zap_leaf_chunk_t *)(l->l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx]
208b1b8ab34Slling #define	ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry)
209b1b8ab34Slling 
210b1b8ab34Slling extern void fletcher_2_native(const void *, uint64_t, zio_cksum_t *);
211b1b8ab34Slling extern void fletcher_2_byteswap(const void *, uint64_t, zio_cksum_t *);
212b1b8ab34Slling extern void fletcher_4_native(const void *, uint64_t, zio_cksum_t *);
213b1b8ab34Slling extern void fletcher_4_byteswap(const void *, uint64_t, zio_cksum_t *);
214b1b8ab34Slling extern void zio_checksum_SHA256(const void *, uint64_t, zio_cksum_t *);
215b1b8ab34Slling extern int lzjb_decompress(void *, void *, size_t, size_t);
216b1b8ab34Slling 
217b1b8ab34Slling #endif	/* FSYS_ZFS */
218b1b8ab34Slling 
219b1b8ab34Slling #endif /* !_FSYS_ZFS_H */
220