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