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