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  */
19ad135b5dSChristopher Siden 
20b1b8ab34Slling /*
216e1f5caaSNeil Perrin  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
22b1b8ab34Slling  * Use is subject to license terms.
23b1b8ab34Slling  */
24b1b8ab34Slling 
25ad135b5dSChristopher Siden /*
262acef22dSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
27a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28ad135b5dSChristopher Siden  */
29ad135b5dSChristopher Siden 
30b1b8ab34Slling /*
31b1b8ab34Slling  * The zfs plug-in routines for GRUB are:
32b1b8ab34Slling  *
33b1b8ab34Slling  * zfs_mount() - locates a valid uberblock of the root pool and reads
34b1b8ab34Slling  *		in its MOS at the memory address MOS.
35b1b8ab34Slling  *
36b1b8ab34Slling  * zfs_open() - locates a plain file object by following the MOS
37b1b8ab34Slling  *		and places its dnode at the memory address DNODE.
38b1b8ab34Slling  *
39b1b8ab34Slling  * zfs_read() - read in the data blocks pointed by the DNODE.
40b1b8ab34Slling  *
41b1b8ab34Slling  * ZFS_SCRATCH is used as a working area.
42b1b8ab34Slling  *
43b1b8ab34Slling  * (memory addr)   MOS      DNODE	ZFS_SCRATCH
44b1b8ab34Slling  *		    |         |          |
45b1b8ab34Slling  *	    +-------V---------V----------V---------------+
46b1b8ab34Slling  *   memory |       | dnode   | dnode    |  scratch      |
47b1b8ab34Slling  *	    |       | 512B    | 512B     |  area         |
48b1b8ab34Slling  *	    +--------------------------------------------+
49b1b8ab34Slling  */
50b1b8ab34Slling 
51b1b8ab34Slling #ifdef	FSYS_ZFS
52b1b8ab34Slling 
53b1b8ab34Slling #include "shared.h"
54b1b8ab34Slling #include "filesys.h"
55b1b8ab34Slling #include "fsys_zfs.h"
56b1b8ab34Slling 
57b1b8ab34Slling /* cache for a file block of the currently zfs_open()-ed file */
58b1b8ab34Slling static void *file_buf = NULL;
59b1b8ab34Slling static uint64_t file_start = 0;
60b1b8ab34Slling static uint64_t file_end = 0;
61b1b8ab34Slling 
62b1b8ab34Slling /* cache for a dnode block */
63b1b8ab34Slling static dnode_phys_t *dnode_buf = NULL;
64b1b8ab34Slling static dnode_phys_t *dnode_mdn = NULL;
65b1b8ab34Slling static uint64_t dnode_start = 0;
66b1b8ab34Slling static uint64_t dnode_end = 0;
67b1b8ab34Slling 
68e23347b1SEric Taylor static uint64_t pool_guid = 0;
69051aabe6Staylor static uberblock_t current_uberblock;
70b1b8ab34Slling static char *stackbase;
71b1b8ab34Slling 
72b1b8ab34Slling decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
73b1b8ab34Slling {
7415e6edf1Sgw 	{"inherit", 0},			/* ZIO_COMPRESS_INHERIT */
75b1b8ab34Slling 	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
7615e6edf1Sgw 	{"off", 0},			/* ZIO_COMPRESS_OFF */
7715e6edf1Sgw 	{"lzjb", lzjb_decompress},	/* ZIO_COMPRESS_LZJB */
78a6f561b4SSašo Kiselkov 	{"empty", 0},			/* ZIO_COMPRESS_EMPTY */
79a6f561b4SSašo Kiselkov 	{"gzip-1", 0},			/* ZIO_COMPRESS_GZIP_1 */
80a6f561b4SSašo Kiselkov 	{"gzip-2", 0},			/* ZIO_COMPRESS_GZIP_2 */
81a6f561b4SSašo Kiselkov 	{"gzip-3", 0},			/* ZIO_COMPRESS_GZIP_3 */
82a6f561b4SSašo Kiselkov 	{"gzip-4", 0},			/* ZIO_COMPRESS_GZIP_4 */
83a6f561b4SSašo Kiselkov 	{"gzip-5", 0},			/* ZIO_COMPRESS_GZIP_5 */
84a6f561b4SSašo Kiselkov 	{"gzip-6", 0},			/* ZIO_COMPRESS_GZIP_6 */
85a6f561b4SSašo Kiselkov 	{"gzip-7", 0},			/* ZIO_COMPRESS_GZIP_7 */
86a6f561b4SSašo Kiselkov 	{"gzip-8", 0},			/* ZIO_COMPRESS_GZIP_8 */
87a6f561b4SSašo Kiselkov 	{"gzip-9", 0},			/* ZIO_COMPRESS_GZIP_9 */
88a6f561b4SSašo Kiselkov 	{"zle", 0},			/* ZIO_COMPRESS_ZLE */
89a6f561b4SSašo Kiselkov 	{"lz4", lz4_decompress}		/* ZIO_COMPRESS_LZ4 */
90b1b8ab34Slling };
91b1b8ab34Slling 
92cd9c78d9SLin Ling static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
93cd9c78d9SLin Ling 
94b1b8ab34Slling /*
95b1b8ab34Slling  * Our own version of bcmp().
96b1b8ab34Slling  */
97b1b8ab34Slling static int
98b1b8ab34Slling zfs_bcmp(const void *s1, const void *s2, size_t n)
99b1b8ab34Slling {
100b1b8ab34Slling 	const uchar_t *ps1 = s1;
101b1b8ab34Slling 	const uchar_t *ps2 = s2;
102b1b8ab34Slling 
103b1b8ab34Slling 	if (s1 != s2 && n != 0) {
104b1b8ab34Slling 		do {
105b1b8ab34Slling 			if (*ps1++ != *ps2++)
106b1b8ab34Slling 				return (1);
107b1b8ab34Slling 		} while (--n != 0);
108b1b8ab34Slling 	}
109b1b8ab34Slling 
110b1b8ab34Slling 	return (0);
111b1b8ab34Slling }
112b1b8ab34Slling 
113b1b8ab34Slling /*
114b1b8ab34Slling  * Our own version of log2().  Same thing as highbit()-1.
115b1b8ab34Slling  */
116b1b8ab34Slling static int
117b1b8ab34Slling zfs_log2(uint64_t num)
118b1b8ab34Slling {
119b1b8ab34Slling 	int i = 0;
120b1b8ab34Slling 
121b1b8ab34Slling 	while (num > 1) {
122b1b8ab34Slling 		i++;
123b1b8ab34Slling 		num = num >> 1;
124b1b8ab34Slling 	}
125b1b8ab34Slling 
126b1b8ab34Slling 	return (i);
127b1b8ab34Slling }
128b1b8ab34Slling 
129b1b8ab34Slling /* Checksum Functions */
130b1b8ab34Slling static void
131b1b8ab34Slling zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
132b1b8ab34Slling {
133b1b8ab34Slling 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
134b1b8ab34Slling }
135b1b8ab34Slling 
136b1b8ab34Slling /* Checksum Table and Values */
137b1b8ab34Slling zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
138ad135b5dSChristopher Siden 	{{NULL,			NULL},			0, 0,	"inherit"},
139ad135b5dSChristopher Siden 	{{NULL,			NULL},			0, 0,	"on"},
140ad135b5dSChristopher Siden 	{{zio_checksum_off,	zio_checksum_off},	0, 0,	"off"},
141ad135b5dSChristopher Siden 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"label"},
142ad135b5dSChristopher Siden 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"gang_header"},
143ad135b5dSChristopher Siden 	{{NULL,			NULL},			0, 0,	"zilog"},
144ad135b5dSChristopher Siden 	{{fletcher_2_native,	fletcher_2_byteswap},	0, 0,	"fletcher2"},
145ad135b5dSChristopher Siden 	{{fletcher_4_native,	fletcher_4_byteswap},	1, 0,	"fletcher4"},
146ad135b5dSChristopher Siden 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 0,	"SHA256"},
147ad135b5dSChristopher Siden 	{{NULL,			NULL},			0, 0,	"zilog2"},
148b1b8ab34Slling };
149b1b8ab34Slling 
150b1b8ab34Slling /*
151b1b8ab34Slling  * zio_checksum_verify: Provides support for checksum verification.
152b1b8ab34Slling  *
153b1b8ab34Slling  * Fletcher2, Fletcher4, and SHA256 are supported.
154b1b8ab34Slling  *
155b1b8ab34Slling  * Return:
156b1b8ab34Slling  * 	-1 = Failure
157b1b8ab34Slling  *	 0 = Success
158b1b8ab34Slling  */
159b1b8ab34Slling static int
160b1b8ab34Slling zio_checksum_verify(blkptr_t *bp, char *data, int size)
161b1b8ab34Slling {
162b1b8ab34Slling 	zio_cksum_t zc = bp->blk_cksum;
163cd9c78d9SLin Ling 	uint32_t checksum = BP_GET_CHECKSUM(bp);
164b1b8ab34Slling 	int byteswap = BP_SHOULD_BYTESWAP(bp);
1656e1f5caaSNeil Perrin 	zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
166b1b8ab34Slling 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
167b1b8ab34Slling 	zio_cksum_t actual_cksum, expected_cksum;
168b1b8ab34Slling 
169b1b8ab34Slling 	/* byteswap is not supported */
170b1b8ab34Slling 	if (byteswap)
171b1b8ab34Slling 		return (-1);
172b1b8ab34Slling 
173b1b8ab34Slling 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
174b1b8ab34Slling 		return (-1);
175b1b8ab34Slling 
1766e1f5caaSNeil Perrin 	if (ci->ci_eck) {
1776e1f5caaSNeil Perrin 		expected_cksum = zec->zec_cksum;
1786e1f5caaSNeil Perrin 		zec->zec_cksum = zc;
179cd9c78d9SLin Ling 		ci->ci_func[0](data, size, &actual_cksum);
1806e1f5caaSNeil Perrin 		zec->zec_cksum = expected_cksum;
181b1b8ab34Slling 		zc = expected_cksum;
182b1b8ab34Slling 
183b1b8ab34Slling 	} else {
184b1b8ab34Slling 		ci->ci_func[byteswap](data, size, &actual_cksum);
185b1b8ab34Slling 	}
186b1b8ab34Slling 
187b1b8ab34Slling 	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
188b1b8ab34Slling 	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
189b1b8ab34Slling 	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
190b1b8ab34Slling 	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
191b1b8ab34Slling 		return (-1);
192b1b8ab34Slling 
193b1b8ab34Slling 	return (0);
194b1b8ab34Slling }
195b1b8ab34Slling 
196b1b8ab34Slling /*
197e23347b1SEric Taylor  * vdev_label_start returns the physical disk offset (in bytes) of
198e23347b1SEric Taylor  * label "l".
199b1b8ab34Slling  */
200e7cbe64fSgw static uint64_t
201e23347b1SEric Taylor vdev_label_start(uint64_t psize, int l)
202b1b8ab34Slling {
203e23347b1SEric Taylor 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
204b1b8ab34Slling 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
205b1b8ab34Slling }
206b1b8ab34Slling 
207b1b8ab34Slling /*
208b1b8ab34Slling  * vdev_uberblock_compare takes two uberblock structures and returns an integer
209b1b8ab34Slling  * indicating the more recent of the two.
210b1b8ab34Slling  * 	Return Value = 1 if ub2 is more recent
211b1b8ab34Slling  * 	Return Value = -1 if ub1 is more recent
212b1b8ab34Slling  * The most recent uberblock is determined using its transaction number and
213b1b8ab34Slling  * timestamp.  The uberblock with the highest transaction number is
214b1b8ab34Slling  * considered "newer".  If the transaction numbers of the two blocks match, the
215b1b8ab34Slling  * timestamps are compared to determine the "newer" of the two.
216b1b8ab34Slling  */
217b1b8ab34Slling static int
218b1b8ab34Slling vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
219b1b8ab34Slling {
220b1b8ab34Slling 	if (ub1->ub_txg < ub2->ub_txg)
221b1b8ab34Slling 		return (-1);
222b1b8ab34Slling 	if (ub1->ub_txg > ub2->ub_txg)
223b1b8ab34Slling 		return (1);
224b1b8ab34Slling 
225b1b8ab34Slling 	if (ub1->ub_timestamp < ub2->ub_timestamp)
226b1b8ab34Slling 		return (-1);
227b1b8ab34Slling 	if (ub1->ub_timestamp > ub2->ub_timestamp)
228b1b8ab34Slling 		return (1);
229b1b8ab34Slling 
230b1b8ab34Slling 	return (0);
231b1b8ab34Slling }
232b1b8ab34Slling 
233b1b8ab34Slling /*
234b1b8ab34Slling  * Three pieces of information are needed to verify an uberblock: the magic
235b1b8ab34Slling  * number, the version number, and the checksum.
236b1b8ab34Slling  *
237b1b8ab34Slling  * Return:
238b1b8ab34Slling  *     0 - Success
239b1b8ab34Slling  *    -1 - Failure
240b1b8ab34Slling  */
241b1b8ab34Slling static int
24281b2d573SHans Rosenfeld uberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
243b1b8ab34Slling {
244b1b8ab34Slling 	blkptr_t bp;
245b1b8ab34Slling 
246b1b8ab34Slling 	BP_ZERO(&bp);
247b1b8ab34Slling 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
248b1b8ab34Slling 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
249b1b8ab34Slling 	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
250b1b8ab34Slling 
25181b2d573SHans Rosenfeld 	if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
252b1b8ab34Slling 		return (-1);
253b1b8ab34Slling 
254b1b8ab34Slling 	if (uber->ub_magic == UBERBLOCK_MAGIC &&
255ad135b5dSChristopher Siden 	    SPA_VERSION_IS_SUPPORTED(uber->ub_version))
256b1b8ab34Slling 		return (0);
257b1b8ab34Slling 
258b1b8ab34Slling 	return (-1);
259b1b8ab34Slling }
260b1b8ab34Slling 
261b1b8ab34Slling /*
262b1b8ab34Slling  * Find the best uberblock.
263b1b8ab34Slling  * Return:
264b1b8ab34Slling  *    Success - Pointer to the best uberblock.
265b1b8ab34Slling  *    Failure - NULL
266b1b8ab34Slling  */
26781b2d573SHans Rosenfeld static uberblock_t *
26881b2d573SHans Rosenfeld find_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
269b1b8ab34Slling {
27081b2d573SHans Rosenfeld 	uberblock_t *ubbest = NULL;
27181b2d573SHans Rosenfeld 	uberblock_t *ubnext;
27281b2d573SHans Rosenfeld 	uint64_t offset, ub_size;
273e23347b1SEric Taylor 	int i;
274b1b8ab34Slling 
27581b2d573SHans Rosenfeld 	ub_size = VDEV_UBERBLOCK_SIZE(ashift);
27681b2d573SHans Rosenfeld 
27781b2d573SHans Rosenfeld 	for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
27881b2d573SHans Rosenfeld 		ubnext = (uberblock_t *)ub_array;
27981b2d573SHans Rosenfeld 		ub_array += ub_size;
280e23347b1SEric Taylor 		offset = (sector << SPA_MINBLOCKSHIFT) +
28181b2d573SHans Rosenfeld 		    VDEV_UBERBLOCK_OFFSET(ashift, i);
28281b2d573SHans Rosenfeld 
28381b2d573SHans Rosenfeld 		if (uberblock_verify(ubnext, ub_size, offset) != 0)
28481b2d573SHans Rosenfeld 			continue;
28581b2d573SHans Rosenfeld 
28681b2d573SHans Rosenfeld 		if (ubbest == NULL ||
28781b2d573SHans Rosenfeld 		    vdev_uberblock_compare(ubnext, ubbest) > 0)
28881b2d573SHans Rosenfeld 			ubbest = ubnext;
289b1b8ab34Slling 	}
290b1b8ab34Slling 
291b1b8ab34Slling 	return (ubbest);
292b1b8ab34Slling }
293b1b8ab34Slling 
294b1b8ab34Slling /*
295cd9c78d9SLin Ling  * Read a block of data based on the gang block address dva,
296cd9c78d9SLin Ling  * and put its data in buf.
297b1b8ab34Slling  *
298b1b8ab34Slling  * Return:
299b1b8ab34Slling  *	0 - success
300cd9c78d9SLin Ling  *	1 - failure
301b1b8ab34Slling  */
302b1b8ab34Slling static int
303cd9c78d9SLin Ling zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
304b1b8ab34Slling {
305cd9c78d9SLin Ling 	zio_gbh_phys_t *zio_gb;
306b1b8ab34Slling 	uint64_t offset, sector;
307cd9c78d9SLin Ling 	blkptr_t tmpbp;
308cd9c78d9SLin Ling 	int i;
309b1b8ab34Slling 
310cd9c78d9SLin Ling 	zio_gb = (zio_gbh_phys_t *)stack;
311cd9c78d9SLin Ling 	stack += SPA_GANGBLOCKSIZE;
312cd9c78d9SLin Ling 	offset = DVA_GET_OFFSET(dva);
313ad135b5dSChristopher Siden 	sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
314b1b8ab34Slling 
315cd9c78d9SLin Ling 	/* read in the gang block header */
316cd9c78d9SLin Ling 	if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
317cd9c78d9SLin Ling 		grub_printf("failed to read in a gang block header\n");
318cd9c78d9SLin Ling 		return (1);
319cd9c78d9SLin Ling 	}
320cd9c78d9SLin Ling 
321cd9c78d9SLin Ling 	/* self checksuming the gang block header */
322cd9c78d9SLin Ling 	BP_ZERO(&tmpbp);
323cd9c78d9SLin Ling 	BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
324cd9c78d9SLin Ling 	BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
325cd9c78d9SLin Ling 	ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
326cd9c78d9SLin Ling 	    DVA_GET_OFFSET(dva), bp->blk_birth, 0);
327cd9c78d9SLin Ling 	if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
328cd9c78d9SLin Ling 		grub_printf("failed to checksum a gang block header\n");
329cd9c78d9SLin Ling 		return (1);
330cd9c78d9SLin Ling 	}
331cd9c78d9SLin Ling 
332cd9c78d9SLin Ling 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
333*43466aaeSMax Grossman 		if (BP_IS_HOLE(&zio_gb->zg_blkptr[i]))
334cd9c78d9SLin Ling 			continue;
335cd9c78d9SLin Ling 
336cd9c78d9SLin Ling 		if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
337cd9c78d9SLin Ling 			return (1);
338cd9c78d9SLin Ling 		buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
339cd9c78d9SLin Ling 	}
340cd9c78d9SLin Ling 
341cd9c78d9SLin Ling 	return (0);
342cd9c78d9SLin Ling }
343cd9c78d9SLin Ling 
344cd9c78d9SLin Ling /*
345cd9c78d9SLin Ling  * Read in a block of raw data to buf.
346cd9c78d9SLin Ling  *
347cd9c78d9SLin Ling  * Return:
348cd9c78d9SLin Ling  *	0 - success
349cd9c78d9SLin Ling  *	1 - failure
350cd9c78d9SLin Ling  */
351cd9c78d9SLin Ling static int
352cd9c78d9SLin Ling zio_read_data(blkptr_t *bp, void *buf, char *stack)
353cd9c78d9SLin Ling {
354cd9c78d9SLin Ling 	int i, psize;
355cd9c78d9SLin Ling 
356cd9c78d9SLin Ling 	psize = BP_GET_PSIZE(bp);
357ae8180dbSlling 
358b1b8ab34Slling 	/* pick a good dva from the block pointer */
359b1b8ab34Slling 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
360cd9c78d9SLin Ling 		uint64_t offset, sector;
361b1b8ab34Slling 
362b1b8ab34Slling 		if (bp->blk_dva[i].dva_word[0] == 0 &&
363b1b8ab34Slling 		    bp->blk_dva[i].dva_word[1] == 0)
364b1b8ab34Slling 			continue;
365b1b8ab34Slling 
366cd9c78d9SLin Ling 		if (DVA_GET_GANG(&bp->blk_dva[i])) {
367cd9c78d9SLin Ling 			if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) == 0)
368cd9c78d9SLin Ling 				return (0);
369b1b8ab34Slling 		} else {
370cd9c78d9SLin Ling 			/* read in a data block */
371cd9c78d9SLin Ling 			offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
372ad135b5dSChristopher Siden 			sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
373ad135b5dSChristopher Siden 			if (devread(sector, 0, psize, buf) != 0)
374cd9c78d9SLin Ling 				return (0);
375b1b8ab34Slling 		}
376b1b8ab34Slling 	}
377b1b8ab34Slling 
378cd9c78d9SLin Ling 	return (1);
379cd9c78d9SLin Ling }
380cd9c78d9SLin Ling 
381cd9c78d9SLin Ling /*
382cd9c78d9SLin Ling  * Read in a block of data, verify its checksum, decompress if needed,
383cd9c78d9SLin Ling  * and put the uncompressed data in buf.
384cd9c78d9SLin Ling  *
385cd9c78d9SLin Ling  * Return:
386cd9c78d9SLin Ling  *	0 - success
387cd9c78d9SLin Ling  *	errnum - failure
388cd9c78d9SLin Ling  */
389cd9c78d9SLin Ling static int
390cd9c78d9SLin Ling zio_read(blkptr_t *bp, void *buf, char *stack)
391cd9c78d9SLin Ling {
392cd9c78d9SLin Ling 	int lsize, psize, comp;
393cd9c78d9SLin Ling 	char *retbuf;
394cd9c78d9SLin Ling 
395cd9c78d9SLin Ling 	comp = BP_GET_COMPRESS(bp);
396cd9c78d9SLin Ling 	lsize = BP_GET_LSIZE(bp);
397cd9c78d9SLin Ling 	psize = BP_GET_PSIZE(bp);
398cd9c78d9SLin Ling 
399cd9c78d9SLin Ling 	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
400cd9c78d9SLin Ling 	    (comp != ZIO_COMPRESS_OFF &&
401cd9c78d9SLin Ling 	    decomp_table[comp].decomp_func == NULL)) {
402cd9c78d9SLin Ling 		grub_printf("compression algorithm not supported\n");
403cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
404cd9c78d9SLin Ling 	}
405cd9c78d9SLin Ling 
406cd9c78d9SLin Ling 	if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
407cd9c78d9SLin Ling 		grub_printf("not enough memory allocated\n");
408cd9c78d9SLin Ling 		return (ERR_WONT_FIT);
409cd9c78d9SLin Ling 	}
410cd9c78d9SLin Ling 
411cd9c78d9SLin Ling 	retbuf = buf;
412cd9c78d9SLin Ling 	if (comp != ZIO_COMPRESS_OFF) {
413cd9c78d9SLin Ling 		buf = stack;
414cd9c78d9SLin Ling 		stack += psize;
415cd9c78d9SLin Ling 	}
416cd9c78d9SLin Ling 
417ad135b5dSChristopher Siden 	if (zio_read_data(bp, buf, stack) != 0) {
418cd9c78d9SLin Ling 		grub_printf("zio_read_data failed\n");
419cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
420cd9c78d9SLin Ling 	}
421cd9c78d9SLin Ling 
422cd9c78d9SLin Ling 	if (zio_checksum_verify(bp, buf, psize) != 0) {
423cd9c78d9SLin Ling 		grub_printf("checksum verification failed\n");
424cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
425cd9c78d9SLin Ling 	}
426cd9c78d9SLin Ling 
427a6f561b4SSašo Kiselkov 	if (comp != ZIO_COMPRESS_OFF) {
428a6f561b4SSašo Kiselkov 		if (decomp_table[comp].decomp_func(buf, retbuf, psize,
429a6f561b4SSašo Kiselkov 		    lsize) != 0) {
430a6f561b4SSašo Kiselkov 			grub_printf("zio_read decompression failed\n");
431a6f561b4SSašo Kiselkov 			return (ERR_FSYS_CORRUPT);
432a6f561b4SSašo Kiselkov 		}
433a6f561b4SSašo Kiselkov 	}
434cd9c78d9SLin Ling 
435cd9c78d9SLin Ling 	return (0);
436b1b8ab34Slling }
437b1b8ab34Slling 
438b1b8ab34Slling /*
439b1b8ab34Slling  * Get the block from a block id.
440b1b8ab34Slling  * push the block onto the stack.
441b1b8ab34Slling  *
442b1b8ab34Slling  * Return:
443b1b8ab34Slling  * 	0 - success
444b1b8ab34Slling  * 	errnum - failure
445b1b8ab34Slling  */
446b1b8ab34Slling static int
447b1b8ab34Slling dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
448b1b8ab34Slling {
449b1b8ab34Slling 	int idx, level;
450b1b8ab34Slling 	blkptr_t *bp_array = dn->dn_blkptr;
451b1b8ab34Slling 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
452b1b8ab34Slling 	blkptr_t *bp, *tmpbuf;
453b1b8ab34Slling 
454b1b8ab34Slling 	bp = (blkptr_t *)stack;
455b1b8ab34Slling 	stack += sizeof (blkptr_t);
456b1b8ab34Slling 
457b1b8ab34Slling 	tmpbuf = (blkptr_t *)stack;
458b1b8ab34Slling 	stack += 1<<dn->dn_indblkshift;
459b1b8ab34Slling 
460b1b8ab34Slling 	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
461b1b8ab34Slling 		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
462b1b8ab34Slling 		*bp = bp_array[idx];
463b1b8ab34Slling 		if (level == 0)
464b1b8ab34Slling 			tmpbuf = buf;
465ae8180dbSlling 		if (BP_IS_HOLE(bp)) {
466ae8180dbSlling 			grub_memset(buf, 0,
467ae8180dbSlling 			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
468ae8180dbSlling 			break;
469ae8180dbSlling 		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
470b1b8ab34Slling 			return (errnum);
471ae8180dbSlling 		}
472b1b8ab34Slling 
473b1b8ab34Slling 		bp_array = tmpbuf;
474b1b8ab34Slling 	}
475b1b8ab34Slling 
476b1b8ab34Slling 	return (0);
477b1b8ab34Slling }
478b1b8ab34Slling 
479b1b8ab34Slling /*
480b1b8ab34Slling  * mzap_lookup: Looks up property described by "name" and returns the value
481b1b8ab34Slling  * in "value".
482b1b8ab34Slling  *
483b1b8ab34Slling  * Return:
484b1b8ab34Slling  *	0 - success
485b1b8ab34Slling  *	errnum - failure
486b1b8ab34Slling  */
487b1b8ab34Slling static int
488ad135b5dSChristopher Siden mzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
489b1b8ab34Slling 	uint64_t *value)
490b1b8ab34Slling {
491b1b8ab34Slling 	int i, chunks;
492b1b8ab34Slling 	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
493b1b8ab34Slling 
494ad135b5dSChristopher Siden 	chunks = objsize / MZAP_ENT_LEN - 1;
495b1b8ab34Slling 	for (i = 0; i < chunks; i++) {
496b1b8ab34Slling 		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
497b1b8ab34Slling 			*value = mzap_ent[i].mze_value;
498b1b8ab34Slling 			return (0);
499b1b8ab34Slling 		}
500b1b8ab34Slling 	}
501b1b8ab34Slling 
502b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
503b1b8ab34Slling }
504b1b8ab34Slling 
505b1b8ab34Slling static uint64_t
506b1b8ab34Slling zap_hash(uint64_t salt, const char *name)
507b1b8ab34Slling {
508b1b8ab34Slling 	static uint64_t table[256];
509b1b8ab34Slling 	const uint8_t *cp;
510b1b8ab34Slling 	uint8_t c;
511b1b8ab34Slling 	uint64_t crc = salt;
512b1b8ab34Slling 
513b1b8ab34Slling 	if (table[128] == 0) {
514b1b8ab34Slling 		uint64_t *ct;
515b1b8ab34Slling 		int i, j;
516b1b8ab34Slling 		for (i = 0; i < 256; i++) {
517b1b8ab34Slling 			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
518b1b8ab34Slling 				*ct = (*ct >> 1) ^ (-(*ct & 1) &
519b1b8ab34Slling 				    ZFS_CRC64_POLY);
520b1b8ab34Slling 		}
521b1b8ab34Slling 	}
522b1b8ab34Slling 
523b1b8ab34Slling 	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
524b1b8ab34Slling 		errnum = ERR_FSYS_CORRUPT;
525b1b8ab34Slling 		return (0);
526b1b8ab34Slling 	}
527b1b8ab34Slling 
528b1b8ab34Slling 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
529b1b8ab34Slling 		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
530b1b8ab34Slling 
531b1b8ab34Slling 	/*
532b1b8ab34Slling 	 * Only use 28 bits, since we need 4 bits in the cookie for the
533b1b8ab34Slling 	 * collision differentiator.  We MUST use the high bits, since
534ad135b5dSChristopher Siden 	 * those are the ones that we first pay attention to when
535ad135b5dSChristopher Siden 	 * choosing the bucket.
536b1b8ab34Slling 	 */
537b24ab676SJeff Bonwick 	crc &= ~((1ULL << (64 - 28)) - 1);
538b1b8ab34Slling 
539b1b8ab34Slling 	return (crc);
540b1b8ab34Slling }
541b1b8ab34Slling 
542b1b8ab34Slling /*
543b1b8ab34Slling  * Only to be used on 8-bit arrays.
544b1b8ab34Slling  * array_len is actual len in bytes (not encoded le_value_length).
545b1b8ab34Slling  * buf is null-terminated.
546b1b8ab34Slling  */
547b1b8ab34Slling static int
548b1b8ab34Slling zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
549b1b8ab34Slling     int array_len, const char *buf)
550b1b8ab34Slling {
551b1b8ab34Slling 	int bseen = 0;
552b1b8ab34Slling 
553b1b8ab34Slling 	while (bseen < array_len) {
554b1b8ab34Slling 		struct zap_leaf_array *la =
555b1b8ab34Slling 		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
556b1b8ab34Slling 		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
557b1b8ab34Slling 
558b1b8ab34Slling 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
559b1b8ab34Slling 			return (0);
560b1b8ab34Slling 
561b1b8ab34Slling 		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
562b1b8ab34Slling 			break;
563b1b8ab34Slling 		chunk = la->la_next;
564b1b8ab34Slling 		bseen += toread;
565b1b8ab34Slling 	}
566b1b8ab34Slling 	return (bseen == array_len);
567b1b8ab34Slling }
568b1b8ab34Slling 
569b1b8ab34Slling /*
570b1b8ab34Slling  * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
571b1b8ab34Slling  * value for the property "name".
572b1b8ab34Slling  *
573b1b8ab34Slling  * Return:
574b1b8ab34Slling  *	0 - success
575b1b8ab34Slling  *	errnum - failure
576b1b8ab34Slling  */
577e7cbe64fSgw static int
578b1b8ab34Slling zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
579b1b8ab34Slling     const char *name, uint64_t *value)
580b1b8ab34Slling {
581b1b8ab34Slling 	uint16_t chunk;
582b1b8ab34Slling 	struct zap_leaf_entry *le;
583b1b8ab34Slling 
584b1b8ab34Slling 	/* Verify if this is a valid leaf block */
585b1b8ab34Slling 	if (l->l_hdr.lh_block_type != ZBT_LEAF)
586b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
587b1b8ab34Slling 	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
588b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
589b1b8ab34Slling 
590b1b8ab34Slling 	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
591b1b8ab34Slling 	    chunk != CHAIN_END; chunk = le->le_next) {
592b1b8ab34Slling 
593b1b8ab34Slling 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
594b1b8ab34Slling 			return (ERR_FSYS_CORRUPT);
595b1b8ab34Slling 
596b1b8ab34Slling 		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
597b1b8ab34Slling 
598b1b8ab34Slling 		/* Verify the chunk entry */
599b1b8ab34Slling 		if (le->le_type != ZAP_CHUNK_ENTRY)
600b1b8ab34Slling 			return (ERR_FSYS_CORRUPT);
601b1b8ab34Slling 
602b1b8ab34Slling 		if (le->le_hash != h)
603b1b8ab34Slling 			continue;
604b1b8ab34Slling 
605b1b8ab34Slling 		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
606b1b8ab34Slling 		    le->le_name_length, name)) {
607b1b8ab34Slling 
608b1b8ab34Slling 			struct zap_leaf_array *la;
609b1b8ab34Slling 			uint8_t *ip;
610b1b8ab34Slling 
611b1b8ab34Slling 			if (le->le_int_size != 8 || le->le_value_length != 1)
612e37b211cStaylor 				return (ERR_FSYS_CORRUPT);
613b1b8ab34Slling 
614b1b8ab34Slling 			/* get the uint64_t property value */
615b1b8ab34Slling 			la = &ZAP_LEAF_CHUNK(l, blksft,
616b1b8ab34Slling 			    le->le_value_chunk).l_array;
617b1b8ab34Slling 			ip = la->la_array;
618b1b8ab34Slling 
619b1b8ab34Slling 			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
620b1b8ab34Slling 			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
621b1b8ab34Slling 			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
622b1b8ab34Slling 			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
623b1b8ab34Slling 
624b1b8ab34Slling 			return (0);
625b1b8ab34Slling 		}
626b1b8ab34Slling 	}
627b1b8ab34Slling 
628b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
629b1b8ab34Slling }
630b1b8ab34Slling 
631b1b8ab34Slling /*
632b1b8ab34Slling  * Fat ZAP lookup
633b1b8ab34Slling  *
634b1b8ab34Slling  * Return:
635b1b8ab34Slling  *	0 - success
636b1b8ab34Slling  *	errnum - failure
637b1b8ab34Slling  */
638e7cbe64fSgw static int
639b1b8ab34Slling fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
640ad135b5dSChristopher Siden     const char *name, uint64_t *value, char *stack)
641b1b8ab34Slling {
642b1b8ab34Slling 	zap_leaf_phys_t *l;
643b1b8ab34Slling 	uint64_t hash, idx, blkid;
644b1b8ab34Slling 	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
645b1b8ab34Slling 
646b1b8ab34Slling 	/* Verify if this is a fat zap header block */
647b24ab676SJeff Bonwick 	if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
648b24ab676SJeff Bonwick 	    zap->zap_flags != 0)
649b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
650b1b8ab34Slling 
651b1b8ab34Slling 	hash = zap_hash(zap->zap_salt, name);
652b1b8ab34Slling 	if (errnum)
653b1b8ab34Slling 		return (errnum);
654b1b8ab34Slling 
655b1b8ab34Slling 	/* get block id from index */
656b1b8ab34Slling 	if (zap->zap_ptrtbl.zt_numblks != 0) {
657b1b8ab34Slling 		/* external pointer tables not supported */
658b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
659b1b8ab34Slling 	}
660b1b8ab34Slling 	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
661b1b8ab34Slling 	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
662b1b8ab34Slling 
663b1b8ab34Slling 	/* Get the leaf block */
664b1b8ab34Slling 	l = (zap_leaf_phys_t *)stack;
665b1b8ab34Slling 	stack += 1<<blksft;
666051aabe6Staylor 	if ((1<<blksft) < sizeof (zap_leaf_phys_t))
667e37b211cStaylor 		return (ERR_FSYS_CORRUPT);
668b1b8ab34Slling 	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
669b1b8ab34Slling 		return (errnum);
670b1b8ab34Slling 
671b1b8ab34Slling 	return (zap_leaf_lookup(l, blksft, hash, name, value));
672b1b8ab34Slling }
673b1b8ab34Slling 
674b1b8ab34Slling /*
675b1b8ab34Slling  * Read in the data of a zap object and find the value for a matching
676b1b8ab34Slling  * property name.
677b1b8ab34Slling  *
678b1b8ab34Slling  * Return:
679b1b8ab34Slling  *	0 - success
680b1b8ab34Slling  *	errnum - failure
681b1b8ab34Slling  */
682b1b8ab34Slling static int
683ad135b5dSChristopher Siden zap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
684ad135b5dSChristopher Siden     char *stack)
685b1b8ab34Slling {
686b1b8ab34Slling 	uint64_t block_type;
687b1b8ab34Slling 	int size;
688b1b8ab34Slling 	void *zapbuf;
689b1b8ab34Slling 
690b1b8ab34Slling 	/* Read in the first block of the zap object data. */
691b1b8ab34Slling 	zapbuf = stack;
692b1b8ab34Slling 	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
693b1b8ab34Slling 	stack += size;
6940a586ceaSMark Shellenbaum 
695ad135b5dSChristopher Siden 	if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
696b1b8ab34Slling 		return (errnum);
697b1b8ab34Slling 
698b1b8ab34Slling 	block_type = *((uint64_t *)zapbuf);
699b1b8ab34Slling 
700b1b8ab34Slling 	if (block_type == ZBT_MICRO) {
701b1b8ab34Slling 		return (mzap_lookup(zapbuf, size, name, val));
702b1b8ab34Slling 	} else if (block_type == ZBT_HEADER) {
703b1b8ab34Slling 		/* this is a fat zap */
704b1b8ab34Slling 		return (fzap_lookup(zap_dnode, zapbuf, name,
705b1b8ab34Slling 		    val, stack));
706b1b8ab34Slling 	}
707b1b8ab34Slling 
708b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
709b1b8ab34Slling }
710b1b8ab34Slling 
711ad135b5dSChristopher Siden typedef struct zap_attribute {
712ad135b5dSChristopher Siden 	int za_integer_length;
713ad135b5dSChristopher Siden 	uint64_t za_num_integers;
714ad135b5dSChristopher Siden 	uint64_t za_first_integer;
715ad135b5dSChristopher Siden 	char *za_name;
716ad135b5dSChristopher Siden } zap_attribute_t;
717ad135b5dSChristopher Siden 
718ad135b5dSChristopher Siden typedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
719ad135b5dSChristopher Siden 
720ad135b5dSChristopher Siden static int
721ad135b5dSChristopher Siden zap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
722ad135b5dSChristopher Siden {
723ad135b5dSChristopher Siden 	uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
724ad135b5dSChristopher Siden 	zap_attribute_t za;
725ad135b5dSChristopher Siden 	int i;
726ad135b5dSChristopher Siden 	mzap_phys_t *mzp = (mzap_phys_t *)stack;
727ad135b5dSChristopher Siden 	stack += size;
728ad135b5dSChristopher Siden 
729ad135b5dSChristopher Siden 	if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
730ad135b5dSChristopher Siden 		return (errnum);
731ad135b5dSChristopher Siden 
732ad135b5dSChristopher Siden 	/*
733ad135b5dSChristopher Siden 	 * Iteration over fatzap objects has not yet been implemented.
734ad135b5dSChristopher Siden 	 * If we encounter a pool in which there are more features for
735ad135b5dSChristopher Siden 	 * read than can fit inside a microzap (i.e., more than 2048
736ad135b5dSChristopher Siden 	 * features for read), we can add support for fatzap iteration.
737ad135b5dSChristopher Siden 	 * For now, fail.
738ad135b5dSChristopher Siden 	 */
739ad135b5dSChristopher Siden 	if (mzp->mz_block_type != ZBT_MICRO) {
740ad135b5dSChristopher Siden 		grub_printf("feature information stored in fatzap, pool "
741ad135b5dSChristopher Siden 		    "version not supported\n");
742ad135b5dSChristopher Siden 		return (1);
743ad135b5dSChristopher Siden 	}
744ad135b5dSChristopher Siden 
745ad135b5dSChristopher Siden 	za.za_integer_length = 8;
746ad135b5dSChristopher Siden 	za.za_num_integers = 1;
747ad135b5dSChristopher Siden 	for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
748ad135b5dSChristopher Siden 		mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
749ad135b5dSChristopher Siden 		int err;
750ad135b5dSChristopher Siden 
751ad135b5dSChristopher Siden 		za.za_first_integer = mzep->mze_value;
752ad135b5dSChristopher Siden 		za.za_name = mzep->mze_name;
753ad135b5dSChristopher Siden 		err = cb(&za, arg, stack);
754ad135b5dSChristopher Siden 		if (err != 0)
755ad135b5dSChristopher Siden 			return (err);
756ad135b5dSChristopher Siden 	}
757ad135b5dSChristopher Siden 
758ad135b5dSChristopher Siden 	return (0);
759ad135b5dSChristopher Siden }
760ad135b5dSChristopher Siden 
761b1b8ab34Slling /*
762b1b8ab34Slling  * Get the dnode of an object number from the metadnode of an object set.
763b1b8ab34Slling  *
764b1b8ab34Slling  * Input
765b1b8ab34Slling  *	mdn - metadnode to get the object dnode
766b1b8ab34Slling  *	objnum - object number for the object dnode
767b1b8ab34Slling  *	buf - data buffer that holds the returning dnode
768b1b8ab34Slling  *	stack - scratch area
769b1b8ab34Slling  *
770b1b8ab34Slling  * Return:
771b1b8ab34Slling  *	0 - success
772b1b8ab34Slling  *	errnum - failure
773b1b8ab34Slling  */
774b1b8ab34Slling static int
775b1b8ab34Slling dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
776b1b8ab34Slling 	char *stack)
777b1b8ab34Slling {
778b1b8ab34Slling 	uint64_t blkid, blksz; /* the block id this object dnode is in */
779b1b8ab34Slling 	int epbs; /* shift of number of dnodes in a block */
780b1b8ab34Slling 	int idx; /* index within a block */
781b1b8ab34Slling 	dnode_phys_t *dnbuf;
782b1b8ab34Slling 
783b1b8ab34Slling 	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
784b1b8ab34Slling 	epbs = zfs_log2(blksz) - DNODE_SHIFT;
785b1b8ab34Slling 	blkid = objnum >> epbs;
786b1b8ab34Slling 	idx = objnum & ((1<<epbs)-1);
787b1b8ab34Slling 
788b1b8ab34Slling 	if (dnode_buf != NULL && dnode_mdn == mdn &&
789b1b8ab34Slling 	    objnum >= dnode_start && objnum < dnode_end) {
790b1b8ab34Slling 		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
791b1b8ab34Slling 		VERIFY_DN_TYPE(buf, type);
792b1b8ab34Slling 		return (0);
793b1b8ab34Slling 	}
794b1b8ab34Slling 
795b1b8ab34Slling 	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
796b1b8ab34Slling 		dnbuf = dnode_buf;
797b1b8ab34Slling 		dnode_mdn = mdn;
798b1b8ab34Slling 		dnode_start = blkid << epbs;
799b1b8ab34Slling 		dnode_end = (blkid + 1) << epbs;
800b1b8ab34Slling 	} else {
801b1b8ab34Slling 		dnbuf = (dnode_phys_t *)stack;
802b1b8ab34Slling 		stack += blksz;
803b1b8ab34Slling 	}
804b1b8ab34Slling 
805b1b8ab34Slling 	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
806b1b8ab34Slling 		return (errnum);
807b1b8ab34Slling 
808b1b8ab34Slling 	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
809b1b8ab34Slling 	VERIFY_DN_TYPE(buf, type);
810b1b8ab34Slling 
811b1b8ab34Slling 	return (0);
812b1b8ab34Slling }
813b1b8ab34Slling 
814b1b8ab34Slling /*
815eb2bd662Svikram  * Check if this is a special file that resides at the top
816eb2bd662Svikram  * dataset of the pool. Currently this is the GRUB menu,
817eb2bd662Svikram  * boot signature and boot signature backup.
818b1b8ab34Slling  * str starts with '/'.
819b1b8ab34Slling  */
820b1b8ab34Slling static int
821eb2bd662Svikram is_top_dataset_file(char *str)
822b1b8ab34Slling {
823b1b8ab34Slling 	char *tptr;
824b1b8ab34Slling 
825b1b8ab34Slling 	if ((tptr = grub_strstr(str, "menu.lst")) &&
826b1b8ab34Slling 	    (tptr[8] == '\0' || tptr[8] == ' ') &&
827b1b8ab34Slling 	    *(tptr-1) == '/')
828b1b8ab34Slling 		return (1);
829b1b8ab34Slling 
830eb2bd662Svikram 	if (grub_strncmp(str, BOOTSIGN_DIR"/",
8311183b401Svikram 	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
832eb2bd662Svikram 		return (1);
833eb2bd662Svikram 
834eb2bd662Svikram 	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
835eb2bd662Svikram 		return (1);
836eb2bd662Svikram 
837b1b8ab34Slling 	return (0);
838b1b8ab34Slling }
839b1b8ab34Slling 
840ad135b5dSChristopher Siden static int
841ad135b5dSChristopher Siden check_feature(zap_attribute_t *za, void *arg, char *stack)
842ad135b5dSChristopher Siden {
843ad135b5dSChristopher Siden 	const char **names = arg;
844ad135b5dSChristopher Siden 	int i;
845ad135b5dSChristopher Siden 
846ad135b5dSChristopher Siden 	if (za->za_first_integer == 0)
847ad135b5dSChristopher Siden 		return (0);
848ad135b5dSChristopher Siden 
849ad135b5dSChristopher Siden 	for (i = 0; names[i] != NULL; i++) {
850ad135b5dSChristopher Siden 		if (grub_strcmp(za->za_name, names[i]) == 0) {
851ad135b5dSChristopher Siden 			return (0);
852ad135b5dSChristopher Siden 		}
853ad135b5dSChristopher Siden 	}
854ad135b5dSChristopher Siden 	grub_printf("missing feature for read '%s'\n", za->za_name);
855ad135b5dSChristopher Siden 	return (ERR_NEWER_VERSION);
856ad135b5dSChristopher Siden }
857ad135b5dSChristopher Siden 
858b1b8ab34Slling /*
859b1b8ab34Slling  * Get the file dnode for a given file name where mdn is the meta dnode
860b1b8ab34Slling  * for this ZFS object set. When found, place the file dnode in dn.
861b1b8ab34Slling  * The 'path' argument will be mangled.
862b1b8ab34Slling  *
863b1b8ab34Slling  * Return:
864b1b8ab34Slling  *	0 - success
865b1b8ab34Slling  *	errnum - failure
866b1b8ab34Slling  */
867b1b8ab34Slling static int
868b1b8ab34Slling dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
869b1b8ab34Slling     char *stack)
870b1b8ab34Slling {
871e7437265Sahrens 	uint64_t objnum, version;
872b1b8ab34Slling 	char *cname, ch;
873b1b8ab34Slling 
874b1b8ab34Slling 	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
875b1b8ab34Slling 	    dn, stack))
876b1b8ab34Slling 		return (errnum);
877b1b8ab34Slling 
878e7437265Sahrens 	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
879e7437265Sahrens 		return (errnum);
880e7437265Sahrens 	if (version > ZPL_VERSION)
881e7437265Sahrens 		return (-1);
882e7437265Sahrens 
883b1b8ab34Slling 	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
884b1b8ab34Slling 		return (errnum);
885b1b8ab34Slling 
886b1b8ab34Slling 	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
887b1b8ab34Slling 	    dn, stack))
888b1b8ab34Slling 		return (errnum);
889b1b8ab34Slling 
890b1b8ab34Slling 	/* skip leading slashes */
891b1b8ab34Slling 	while (*path == '/')
892b1b8ab34Slling 		path++;
893b1b8ab34Slling 
894ad135b5dSChristopher Siden 	while (*path && !grub_isspace(*path)) {
895b1b8ab34Slling 
896b1b8ab34Slling 		/* get the next component name */
897b1b8ab34Slling 		cname = path;
898ad135b5dSChristopher Siden 		while (*path && !grub_isspace(*path) && *path != '/')
899b1b8ab34Slling 			path++;
900b1b8ab34Slling 		ch = *path;
901b1b8ab34Slling 		*path = 0;   /* ensure null termination */
902b1b8ab34Slling 
903b1b8ab34Slling 		if (errnum = zap_lookup(dn, cname, &objnum, stack))
904b1b8ab34Slling 			return (errnum);
905b1b8ab34Slling 
906e7437265Sahrens 		objnum = ZFS_DIRENT_OBJ(objnum);
907b1b8ab34Slling 		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
908b1b8ab34Slling 			return (errnum);
909b1b8ab34Slling 
910b1b8ab34Slling 		*path = ch;
911b1b8ab34Slling 		while (*path == '/')
912b1b8ab34Slling 			path++;
913b1b8ab34Slling 	}
914b1b8ab34Slling 
915b1b8ab34Slling 	/* We found the dnode for this file. Verify if it is a plain file. */
916b1b8ab34Slling 	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
917b1b8ab34Slling 
918b1b8ab34Slling 	return (0);
919b1b8ab34Slling }
920b1b8ab34Slling 
921b1b8ab34Slling /*
922b1b8ab34Slling  * Get the default 'bootfs' property value from the rootpool.
923b1b8ab34Slling  *
924b1b8ab34Slling  * Return:
925b1b8ab34Slling  *	0 - success
926b1b8ab34Slling  *	errnum -failure
927b1b8ab34Slling  */
928b1b8ab34Slling static int
929b1b8ab34Slling get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
930b1b8ab34Slling {
931b1b8ab34Slling 	uint64_t objnum = 0;
932b1b8ab34Slling 	dnode_phys_t *dn = (dnode_phys_t *)stack;
933b1b8ab34Slling 	stack += DNODE_SIZE;
934b1b8ab34Slling 
935ae8180dbSlling 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
936b1b8ab34Slling 	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
937ae8180dbSlling 		return (errnum);
938b1b8ab34Slling 
939b1b8ab34Slling 	/*
940b1b8ab34Slling 	 * find the object number for 'pool_props', and get the dnode
941b1b8ab34Slling 	 * of the 'pool_props'.
942b1b8ab34Slling 	 */
943b1b8ab34Slling 	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
944b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
945b1b8ab34Slling 
946ae8180dbSlling 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
947ae8180dbSlling 		return (errnum);
948b1b8ab34Slling 
949b1b8ab34Slling 	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
950b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
951b1b8ab34Slling 
952b1b8ab34Slling 	if (!objnum)
953b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
954b1b8ab34Slling 
955b1b8ab34Slling 	*obj = objnum;
956b1b8ab34Slling 	return (0);
957b1b8ab34Slling }
958b1b8ab34Slling 
959ad135b5dSChristopher Siden /*
960ad135b5dSChristopher Siden  * List of pool features that the grub implementation of ZFS supports for
961ad135b5dSChristopher Siden  * read. Note that features that are only required for write do not need
962ad135b5dSChristopher Siden  * to be listed here since grub opens pools in read-only mode.
96333915f34SRichard Lowe  *
96433915f34SRichard Lowe  * When this list is updated the version number in usr/src/grub/capability
96533915f34SRichard Lowe  * must be incremented to ensure the new grub gets installed.
966ad135b5dSChristopher Siden  */
967ad135b5dSChristopher Siden static const char *spa_feature_names[] = {
968a6f561b4SSašo Kiselkov 	"org.illumos:lz4_compress",
969*43466aaeSMax Grossman 	"com.delphix:hole_birth",
9702acef22dSMatthew Ahrens 	"com.delphix:extensible_dataset",
971ad135b5dSChristopher Siden 	NULL
972ad135b5dSChristopher Siden };
973ad135b5dSChristopher Siden 
974ad135b5dSChristopher Siden /*
975ad135b5dSChristopher Siden  * Checks whether the MOS features that are active are supported by this
976ad135b5dSChristopher Siden  * (GRUB's) implementation of ZFS.
977ad135b5dSChristopher Siden  *
978ad135b5dSChristopher Siden  * Return:
979ad135b5dSChristopher Siden  *	0: Success.
980ad135b5dSChristopher Siden  *	errnum: Failure.
981ad135b5dSChristopher Siden  */
982ad135b5dSChristopher Siden static int
983ad135b5dSChristopher Siden check_mos_features(dnode_phys_t *mosmdn, char *stack)
984ad135b5dSChristopher Siden {
985ad135b5dSChristopher Siden 	uint64_t objnum;
986ad135b5dSChristopher Siden 	dnode_phys_t *dn;
987ad135b5dSChristopher Siden 	uint8_t error = 0;
988ad135b5dSChristopher Siden 
989ad135b5dSChristopher Siden 	dn = (dnode_phys_t *)stack;
990ad135b5dSChristopher Siden 	stack += DNODE_SIZE;
991ad135b5dSChristopher Siden 
992ad135b5dSChristopher Siden 	if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
993ad135b5dSChristopher Siden 	    DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
994ad135b5dSChristopher Siden 		return (errnum);
995ad135b5dSChristopher Siden 
996ad135b5dSChristopher Siden 	/*
997ad135b5dSChristopher Siden 	 * Find the object number for 'features_for_read' and retrieve its
998ad135b5dSChristopher Siden 	 * corresponding dnode. Note that we don't check features_for_write
999ad135b5dSChristopher Siden 	 * because GRUB is not opening the pool for write.
1000ad135b5dSChristopher Siden 	 */
1001ad135b5dSChristopher Siden 	if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
1002ad135b5dSChristopher Siden 	    stack)) != 0)
1003ad135b5dSChristopher Siden 		return (errnum);
1004ad135b5dSChristopher Siden 
1005ad135b5dSChristopher Siden 	if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
1006ad135b5dSChristopher Siden 	    dn, stack)) != 0)
1007ad135b5dSChristopher Siden 		return (errnum);
1008ad135b5dSChristopher Siden 
1009ad135b5dSChristopher Siden 	return (zap_iterate(dn, check_feature, spa_feature_names, stack));
1010ad135b5dSChristopher Siden }
1011ad135b5dSChristopher Siden 
1012b1b8ab34Slling /*
1013b1b8ab34Slling  * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1014b1b8ab34Slling  * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1015b1b8ab34Slling  * of pool/rootfs.
1016b1b8ab34Slling  *
1017b1b8ab34Slling  * If no fsname and no obj are given, return the DSL_DIR metadnode.
1018b1b8ab34Slling  * If fsname is given, return its metadnode and its matching object number.
1019b1b8ab34Slling  * If only obj is given, return the metadnode for this object number.
1020b1b8ab34Slling  *
1021b1b8ab34Slling  * Return:
1022b1b8ab34Slling  *	0 - success
1023b1b8ab34Slling  *	errnum - failure
1024b1b8ab34Slling  */
1025b1b8ab34Slling static int
1026b1b8ab34Slling get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
1027b1b8ab34Slling     dnode_phys_t *mdn, char *stack)
1028b1b8ab34Slling {
1029b1b8ab34Slling 	uint64_t objnum, headobj;
1030b1b8ab34Slling 	char *cname, ch;
1031b1b8ab34Slling 	blkptr_t *bp;
1032b1b8ab34Slling 	objset_phys_t *osp;
1033fe3e2633SEric Taylor 	int issnapshot = 0;
1034fe3e2633SEric Taylor 	char *snapname;
1035b1b8ab34Slling 
1036b1b8ab34Slling 	if (fsname == NULL && obj) {
1037b1b8ab34Slling 		headobj = *obj;
1038b1b8ab34Slling 		goto skip;
1039b1b8ab34Slling 	}
1040b1b8ab34Slling 
1041b1b8ab34Slling 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1042b1b8ab34Slling 	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
1043b1b8ab34Slling 		return (errnum);
1044b1b8ab34Slling 
1045b1b8ab34Slling 	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
1046b1b8ab34Slling 	    stack))
1047b1b8ab34Slling 		return (errnum);
1048b1b8ab34Slling 
10492acef22dSMatthew Ahrens 	if (errnum = dnode_get(mosmdn, objnum, 0, mdn, stack))
1050b1b8ab34Slling 		return (errnum);
1051b1b8ab34Slling 
1052b1b8ab34Slling 	if (fsname == NULL) {
1053b1b8ab34Slling 		headobj =
1054b1b8ab34Slling 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1055b1b8ab34Slling 		goto skip;
1056b1b8ab34Slling 	}
1057b1b8ab34Slling 
1058b1b8ab34Slling 	/* take out the pool name */
1059ad135b5dSChristopher Siden 	while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1060b1b8ab34Slling 		fsname++;
1061b1b8ab34Slling 
1062ad135b5dSChristopher Siden 	while (*fsname && !grub_isspace(*fsname)) {
1063b1b8ab34Slling 		uint64_t childobj;
1064b1b8ab34Slling 
1065b1b8ab34Slling 		while (*fsname == '/')
1066b1b8ab34Slling 			fsname++;
1067b1b8ab34Slling 
1068b1b8ab34Slling 		cname = fsname;
1069ad135b5dSChristopher Siden 		while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1070b1b8ab34Slling 			fsname++;
1071b1b8ab34Slling 		ch = *fsname;
1072b1b8ab34Slling 		*fsname = 0;
1073b1b8ab34Slling 
1074fe3e2633SEric Taylor 		snapname = cname;
1075ad135b5dSChristopher Siden 		while (*snapname && !grub_isspace(*snapname) && *snapname !=
1076ad135b5dSChristopher Siden 		    '@')
1077fe3e2633SEric Taylor 			snapname++;
1078fe3e2633SEric Taylor 		if (*snapname == '@') {
1079fe3e2633SEric Taylor 			issnapshot = 1;
1080fe3e2633SEric Taylor 			*snapname = 0;
1081fe3e2633SEric Taylor 		}
1082b1b8ab34Slling 		childobj =
1083b1b8ab34Slling 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
1084b1b8ab34Slling 		if (errnum = dnode_get(mosmdn, childobj,
1085b1b8ab34Slling 		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
1086b1b8ab34Slling 			return (errnum);
1087b1b8ab34Slling 
1088ae8180dbSlling 		if (zap_lookup(mdn, cname, &objnum, stack))
1089ae8180dbSlling 			return (ERR_FILESYSTEM_NOT_FOUND);
1090b1b8ab34Slling 
10912acef22dSMatthew Ahrens 		if (errnum = dnode_get(mosmdn, objnum, 0,
1092b1b8ab34Slling 		    mdn, stack))
1093b1b8ab34Slling 			return (errnum);
1094b1b8ab34Slling 
1095b1b8ab34Slling 		*fsname = ch;
1096fe3e2633SEric Taylor 		if (issnapshot)
1097fe3e2633SEric Taylor 			*snapname = '@';
1098b1b8ab34Slling 	}
1099b1b8ab34Slling 	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1100b1b8ab34Slling 	if (obj)
1101b1b8ab34Slling 		*obj = headobj;
1102b1b8ab34Slling 
1103b1b8ab34Slling skip:
11042acef22dSMatthew Ahrens 	if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1105b1b8ab34Slling 		return (errnum);
1106fe3e2633SEric Taylor 	if (issnapshot) {
1107fe3e2633SEric Taylor 		uint64_t snapobj;
1108fe3e2633SEric Taylor 
1109fe3e2633SEric Taylor 		snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
1110fe3e2633SEric Taylor 		    ds_snapnames_zapobj;
1111b1b8ab34Slling 
1112fe3e2633SEric Taylor 		if (errnum = dnode_get(mosmdn, snapobj,
1113fe3e2633SEric Taylor 		    DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
1114fe3e2633SEric Taylor 			return (errnum);
1115fe3e2633SEric Taylor 		if (zap_lookup(mdn, snapname + 1, &headobj, stack))
1116fe3e2633SEric Taylor 			return (ERR_FILESYSTEM_NOT_FOUND);
11172acef22dSMatthew Ahrens 		if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1118fe3e2633SEric Taylor 			return (errnum);
1119fe3e2633SEric Taylor 		if (obj)
1120fe3e2633SEric Taylor 			*obj = headobj;
1121fe3e2633SEric Taylor 	}
1122b1b8ab34Slling 
1123b1b8ab34Slling 	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
1124b1b8ab34Slling 	osp = (objset_phys_t *)stack;
1125b1b8ab34Slling 	stack += sizeof (objset_phys_t);
1126b1b8ab34Slling 	if (errnum = zio_read(bp, osp, stack))
1127b1b8ab34Slling 		return (errnum);
1128b1b8ab34Slling 
1129b1b8ab34Slling 	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
1130b1b8ab34Slling 
1131b1b8ab34Slling 	return (0);
1132b1b8ab34Slling }
1133b1b8ab34Slling 
1134b1b8ab34Slling /*
1135e7cbe64fSgw  * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1136b1b8ab34Slling  *
1137e7cbe64fSgw  * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1138b1b8ab34Slling  *
1139b1b8ab34Slling  *      encoding method/host endian     (4 bytes)
1140b1b8ab34Slling  *      nvl_version                     (4 bytes)
1141b1b8ab34Slling  *      nvl_nvflag                      (4 bytes)
1142b1b8ab34Slling  *	encoded nvpairs:
1143b1b8ab34Slling  *		encoded size of the nvpair      (4 bytes)
1144b1b8ab34Slling  *		decoded size of the nvpair      (4 bytes)
1145b1b8ab34Slling  *		name string size                (4 bytes)
1146b1b8ab34Slling  *		name string data                (sizeof(NV_ALIGN4(string))
1147b1b8ab34Slling  *		data type                       (4 bytes)
1148b1b8ab34Slling  *		# of elements in the nvpair     (4 bytes)
1149b1b8ab34Slling  *		data
1150b1b8ab34Slling  *      2 zero's for the last nvpair
1151b1b8ab34Slling  *		(end of the entire list)	(8 bytes)
1152b1b8ab34Slling  *
1153b1b8ab34Slling  * Return:
1154b1b8ab34Slling  *	0 - success
1155b1b8ab34Slling  *	1 - failure
1156b1b8ab34Slling  */
1157e7cbe64fSgw static int
1158e7cbe64fSgw nvlist_unpack(char *nvlist, char **out)
1159b1b8ab34Slling {
1160b1b8ab34Slling 	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
1161b1b8ab34Slling 	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1162b1b8ab34Slling 		return (1);
1163b1b8ab34Slling 
1164ad135b5dSChristopher Siden 	*out = nvlist + 4;
1165e7cbe64fSgw 	return (0);
1166e7cbe64fSgw }
1167e7cbe64fSgw 
1168e7cbe64fSgw static char *
1169e7cbe64fSgw nvlist_array(char *nvlist, int index)
1170e7cbe64fSgw {
1171e7cbe64fSgw 	int i, encode_size;
1172e7cbe64fSgw 
1173e7cbe64fSgw 	for (i = 0; i < index; i++) {
1174e7cbe64fSgw 		/* skip the header, nvl_version, and nvl_nvflag */
1175e7cbe64fSgw 		nvlist = nvlist + 4 * 2;
1176e7cbe64fSgw 
1177e7cbe64fSgw 		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1178e7cbe64fSgw 			nvlist += encode_size; /* goto the next nvpair */
1179e7cbe64fSgw 
1180e7cbe64fSgw 		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1181e7cbe64fSgw 	}
1182e7cbe64fSgw 
1183e7cbe64fSgw 	return (nvlist);
1184e7cbe64fSgw }
1185e7cbe64fSgw 
1186ad135b5dSChristopher Siden /*
1187ad135b5dSChristopher Siden  * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
1188ad135b5dSChristopher Siden  * list following nvpair. If nvpair is NULL, the first pair is returned. If
1189ad135b5dSChristopher Siden  * nvpair is the last pair in the nvlist, NULL is returned.
1190ad135b5dSChristopher Siden  */
1191ad135b5dSChristopher Siden static char *
1192ad135b5dSChristopher Siden nvlist_next_nvpair(char *nvl, char *nvpair)
1193ad135b5dSChristopher Siden {
1194ad135b5dSChristopher Siden 	char *cur, *prev;
1195ad135b5dSChristopher Siden 	int encode_size;
1196ad135b5dSChristopher Siden 
1197ad135b5dSChristopher Siden 	if (nvl == NULL)
1198ad135b5dSChristopher Siden 		return (NULL);
1199ad135b5dSChristopher Siden 
1200ad135b5dSChristopher Siden 	if (nvpair == NULL) {
1201ad135b5dSChristopher Siden 		/* skip over nvl_version and nvl_nvflag */
1202ad135b5dSChristopher Siden 		nvpair = nvl + 4 * 2;
1203ad135b5dSChristopher Siden 	} else {
1204ad135b5dSChristopher Siden 		/* skip to the next nvpair */
1205ad135b5dSChristopher Siden 		encode_size = BSWAP_32(*(uint32_t *)nvpair);
1206ad135b5dSChristopher Siden 		nvpair += encode_size;
1207ad135b5dSChristopher Siden 	}
1208ad135b5dSChristopher Siden 
1209ad135b5dSChristopher Siden 	/* 8 bytes of 0 marks the end of the list */
1210ad135b5dSChristopher Siden 	if (*(uint64_t *)nvpair == 0)
1211ad135b5dSChristopher Siden 		return (NULL);
1212ad135b5dSChristopher Siden 
1213ad135b5dSChristopher Siden 	return (nvpair);
1214ad135b5dSChristopher Siden }
1215ad135b5dSChristopher Siden 
1216ad135b5dSChristopher Siden /*
1217ad135b5dSChristopher Siden  * This function returns 0 on success and 1 on failure. On success, a string
1218ad135b5dSChristopher Siden  * containing the name of nvpair is saved in buf.
1219ad135b5dSChristopher Siden  */
1220e7cbe64fSgw static int
1221ad135b5dSChristopher Siden nvpair_name(char *nvp, char *buf, int buflen)
1222ad135b5dSChristopher Siden {
1223ad135b5dSChristopher Siden 	int len;
1224ad135b5dSChristopher Siden 
1225ad135b5dSChristopher Siden 	/* skip over encode/decode size */
1226ad135b5dSChristopher Siden 	nvp += 4 * 2;
1227ad135b5dSChristopher Siden 
1228ad135b5dSChristopher Siden 	len = BSWAP_32(*(uint32_t *)nvp);
1229ad135b5dSChristopher Siden 	if (buflen < len + 1)
1230ad135b5dSChristopher Siden 		return (1);
1231ad135b5dSChristopher Siden 
1232ad135b5dSChristopher Siden 	grub_memmove(buf, nvp + 4, len);
1233ad135b5dSChristopher Siden 	buf[len] = '\0';
1234ad135b5dSChristopher Siden 
1235ad135b5dSChristopher Siden 	return (0);
1236ad135b5dSChristopher Siden }
1237ad135b5dSChristopher Siden 
1238ad135b5dSChristopher Siden /*
1239ad135b5dSChristopher Siden  * This function retrieves the value of the nvpair in the form of enumerated
1240ad135b5dSChristopher Siden  * type data_type_t. This is used to determine the appropriate type to pass to
1241ad135b5dSChristopher Siden  * nvpair_value().
1242ad135b5dSChristopher Siden  */
1243ad135b5dSChristopher Siden static int
1244ad135b5dSChristopher Siden nvpair_type(char *nvp)
1245e7cbe64fSgw {
1246ad135b5dSChristopher Siden 	int name_len, type;
1247ad135b5dSChristopher Siden 
1248ad135b5dSChristopher Siden 	/* skip over encode/decode size */
1249ad135b5dSChristopher Siden 	nvp += 4 * 2;
1250ad135b5dSChristopher Siden 
1251ad135b5dSChristopher Siden 	/* skip over name_len */
1252ad135b5dSChristopher Siden 	name_len = BSWAP_32(*(uint32_t *)nvp);
1253ad135b5dSChristopher Siden 	nvp += 4;
1254ad135b5dSChristopher Siden 
1255ad135b5dSChristopher Siden 	/* skip over name */
1256ad135b5dSChristopher Siden 	nvp = nvp + ((name_len + 3) & ~3); /* align */
1257ad135b5dSChristopher Siden 
1258ad135b5dSChristopher Siden 	type = BSWAP_32(*(uint32_t *)nvp);
1259ad135b5dSChristopher Siden 
1260ad135b5dSChristopher Siden 	return (type);
1261ad135b5dSChristopher Siden }
1262ad135b5dSChristopher Siden 
1263ad135b5dSChristopher Siden static int
1264ad135b5dSChristopher Siden nvpair_value(char *nvp, void *val, int valtype, int *nelmp)
1265ad135b5dSChristopher Siden {
1266ad135b5dSChristopher Siden 	int name_len, type, slen;
1267ad135b5dSChristopher Siden 	char *strval = val;
1268e7cbe64fSgw 	uint64_t *intval = val;
1269e7cbe64fSgw 
1270ad135b5dSChristopher Siden 	/* skip over encode/decode size */
1271ad135b5dSChristopher Siden 	nvp += 4 * 2;
1272b1b8ab34Slling 
1273ad135b5dSChristopher Siden 	/* skip over name_len */
1274ad135b5dSChristopher Siden 	name_len = BSWAP_32(*(uint32_t *)nvp);
1275ad135b5dSChristopher Siden 	nvp += 4;
1276b1b8ab34Slling 
1277ad135b5dSChristopher Siden 	/* skip over name */
1278ad135b5dSChristopher Siden 	nvp = nvp + ((name_len + 3) & ~3); /* align */
1279b1b8ab34Slling 
1280ad135b5dSChristopher Siden 	/* skip over type */
1281ad135b5dSChristopher Siden 	type = BSWAP_32(*(uint32_t *)nvp);
1282ad135b5dSChristopher Siden 	nvp += 4;
1283b1b8ab34Slling 
1284ad135b5dSChristopher Siden 	if (type == valtype) {
1285ad135b5dSChristopher Siden 		int nelm;
1286b1b8ab34Slling 
1287ad135b5dSChristopher Siden 		nelm = BSWAP_32(*(uint32_t *)nvp);
1288ad135b5dSChristopher Siden 		if (valtype != DATA_TYPE_BOOLEAN && nelm < 1)
1289ad135b5dSChristopher Siden 			return (1);
1290ad135b5dSChristopher Siden 		nvp += 4;
1291b1b8ab34Slling 
1292ad135b5dSChristopher Siden 		switch (valtype) {
1293ad135b5dSChristopher Siden 		case DATA_TYPE_BOOLEAN:
1294ad135b5dSChristopher Siden 			return (0);
1295b1b8ab34Slling 
1296ad135b5dSChristopher Siden 		case DATA_TYPE_STRING:
1297ad135b5dSChristopher Siden 			slen = BSWAP_32(*(uint32_t *)nvp);
1298ad135b5dSChristopher Siden 			nvp += 4;
1299ad135b5dSChristopher Siden 			grub_memmove(strval, nvp, slen);
1300ad135b5dSChristopher Siden 			strval[slen] = '\0';
1301ad135b5dSChristopher Siden 			return (0);
1302b1b8ab34Slling 
1303ad135b5dSChristopher Siden 		case DATA_TYPE_UINT64:
1304ad135b5dSChristopher Siden 			*intval = BSWAP_64(*(uint64_t *)nvp);
1305ad135b5dSChristopher Siden 			return (0);
1306e7cbe64fSgw 
1307ad135b5dSChristopher Siden 		case DATA_TYPE_NVLIST:
1308ad135b5dSChristopher Siden 			*(void **)val = (void *)nvp;
1309ad135b5dSChristopher Siden 			return (0);
1310e7cbe64fSgw 
1311ad135b5dSChristopher Siden 		case DATA_TYPE_NVLIST_ARRAY:
1312ad135b5dSChristopher Siden 			*(void **)val = (void *)nvp;
1313ad135b5dSChristopher Siden 			if (nelmp)
1314ad135b5dSChristopher Siden 				*nelmp = nelm;
1315ad135b5dSChristopher Siden 			return (0);
1316b1b8ab34Slling 		}
1317b1b8ab34Slling 	}
1318b1b8ab34Slling 
1319b1b8ab34Slling 	return (1);
1320b1b8ab34Slling }
1321b1b8ab34Slling 
1322ad135b5dSChristopher Siden static int
1323ad135b5dSChristopher Siden nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1324ad135b5dSChristopher Siden     int *nelmp)
1325ad135b5dSChristopher Siden {
1326ad135b5dSChristopher Siden 	char *nvpair;
1327ad135b5dSChristopher Siden 
1328ad135b5dSChristopher Siden 	for (nvpair = nvlist_next_nvpair(nvlist, NULL);
1329ad135b5dSChristopher Siden 	    nvpair != NULL;
1330ad135b5dSChristopher Siden 	    nvpair = nvlist_next_nvpair(nvlist, nvpair)) {
1331ad135b5dSChristopher Siden 		int name_len = BSWAP_32(*(uint32_t *)(nvpair + 4 * 2));
1332ad135b5dSChristopher Siden 		char *nvp_name = nvpair + 4 * 3;
1333ad135b5dSChristopher Siden 
1334ad135b5dSChristopher Siden 		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1335ad135b5dSChristopher Siden 		    nvpair_type(nvpair) == valtype) {
1336ad135b5dSChristopher Siden 			return (nvpair_value(nvpair, val, valtype, nelmp));
1337ad135b5dSChristopher Siden 		}
1338ad135b5dSChristopher Siden 	}
1339ad135b5dSChristopher Siden 	return (1);
1340ad135b5dSChristopher Siden }
1341ad135b5dSChristopher Siden 
1342b1b8ab34Slling /*
1343e7cbe64fSgw  * Check if this vdev is online and is in a good state.
1344e7cbe64fSgw  */
1345e7cbe64fSgw static int
1346e7cbe64fSgw vdev_validate(char *nv)
1347e7cbe64fSgw {
1348e7cbe64fSgw 	uint64_t ival;
1349e7cbe64fSgw 
1350e7cbe64fSgw 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1351e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0 ||
1352e7cbe64fSgw 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1353e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0 ||
1354e7cbe64fSgw 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1355e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0)
1356e7cbe64fSgw 		return (ERR_DEV_VALUES);
1357e7cbe64fSgw 
1358e7cbe64fSgw 	return (0);
1359e7cbe64fSgw }
1360e7cbe64fSgw 
1361e7cbe64fSgw /*
136221ecdf64SLin Ling  * Get a valid vdev pathname/devid from the boot device.
1363ffb5616eSLin Ling  * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1364e7cbe64fSgw  */
136521ecdf64SLin Ling static int
136621ecdf64SLin Ling vdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
136721ecdf64SLin Ling     int is_spare)
1368e7cbe64fSgw {
1369e7cbe64fSgw 	char type[16];
1370e7cbe64fSgw 
1371e7cbe64fSgw 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1372e7cbe64fSgw 	    NULL))
1373e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1374e7cbe64fSgw 
1375ad135b5dSChristopher Siden 	if (grub_strcmp(type, VDEV_TYPE_DISK) == 0) {
1376ffb5616eSLin Ling 		uint64_t guid;
1377ffb5616eSLin Ling 
1378ffb5616eSLin Ling 		if (vdev_validate(nv) != 0)
1379ffb5616eSLin Ling 			return (ERR_NO_BOOTPATH);
1380ffb5616eSLin Ling 
1381ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1382ffb5616eSLin Ling 		    &guid, DATA_TYPE_UINT64, NULL) != 0)
1383ffb5616eSLin Ling 			return (ERR_NO_BOOTPATH);
1384ffb5616eSLin Ling 
1385ffb5616eSLin Ling 		if (guid != inguid)
1386e7cbe64fSgw 			return (ERR_NO_BOOTPATH);
1387e7cbe64fSgw 
138821ecdf64SLin Ling 		/* for a spare vdev, pick the disk labeled with "is_spare" */
138921ecdf64SLin Ling 		if (is_spare) {
139021ecdf64SLin Ling 			uint64_t spare = 0;
139121ecdf64SLin Ling 			(void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
139221ecdf64SLin Ling 			    &spare, DATA_TYPE_UINT64, NULL);
139321ecdf64SLin Ling 			if (!spare)
139421ecdf64SLin Ling 				return (ERR_NO_BOOTPATH);
139521ecdf64SLin Ling 		}
139621ecdf64SLin Ling 
1397ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
1398ffb5616eSLin Ling 		    bootpath, DATA_TYPE_STRING, NULL) != 0)
1399ffb5616eSLin Ling 			bootpath[0] = '\0';
1400ffb5616eSLin Ling 
1401ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
1402ffb5616eSLin Ling 		    devid, DATA_TYPE_STRING, NULL) != 0)
1403ffb5616eSLin Ling 			devid[0] = '\0';
1404ffb5616eSLin Ling 
1405ad135b5dSChristopher Siden 		if (grub_strlen(bootpath) >= MAXPATHLEN ||
1406ad135b5dSChristopher Siden 		    grub_strlen(devid) >= MAXPATHLEN)
1407ffb5616eSLin Ling 			return (ERR_WONT_FIT);
1408ffb5616eSLin Ling 
1409ffb5616eSLin Ling 		return (0);
1410ffb5616eSLin Ling 
1411ad135b5dSChristopher Siden 	} else if (grub_strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1412ad135b5dSChristopher Siden 	    grub_strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1413ad135b5dSChristopher Siden 	    (is_spare = (grub_strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1414e7cbe64fSgw 		int nelm, i;
1415e7cbe64fSgw 		char *child;
1416e7cbe64fSgw 
1417e7cbe64fSgw 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
1418e7cbe64fSgw 		    DATA_TYPE_NVLIST_ARRAY, &nelm))
1419e7cbe64fSgw 			return (ERR_FSYS_CORRUPT);
1420e7cbe64fSgw 
1421e7cbe64fSgw 		for (i = 0; i < nelm; i++) {
1422e7cbe64fSgw 			char *child_i;
1423e7cbe64fSgw 
1424e7cbe64fSgw 			child_i = nvlist_array(child, i);
1425ffb5616eSLin Ling 			if (vdev_get_bootpath(child_i, inguid, devid,
142621ecdf64SLin Ling 			    bootpath, is_spare) == 0)
1427ffb5616eSLin Ling 				return (0);
1428e7cbe64fSgw 		}
1429e7cbe64fSgw 	}
1430e7cbe64fSgw 
1431ffb5616eSLin Ling 	return (ERR_NO_BOOTPATH);
1432e7cbe64fSgw }
1433e7cbe64fSgw 
1434e7cbe64fSgw /*
1435e7cbe64fSgw  * Check the disk label information and retrieve needed vdev name-value pairs.
1436b1b8ab34Slling  *
1437b1b8ab34Slling  * Return:
1438b1b8ab34Slling  *	0 - success
1439e7cbe64fSgw  *	ERR_* - failure
1440b1b8ab34Slling  */
1441ad135b5dSChristopher Siden static int
1442e23347b1SEric Taylor check_pool_label(uint64_t sector, char *stack, char *outdevid,
1443ad135b5dSChristopher Siden     char *outpath, uint64_t *outguid, uint64_t *outashift, uint64_t *outversion)
1444b1b8ab34Slling {
1445b1b8ab34Slling 	vdev_phys_t *vdev;
1446e23347b1SEric Taylor 	uint64_t pool_state, txg = 0;
1447ad135b5dSChristopher Siden 	char *nvlist, *nv, *features;
1448051aabe6Staylor 	uint64_t diskguid;
1449b1b8ab34Slling 
1450e23347b1SEric Taylor 	sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
1451b1b8ab34Slling 
1452b1b8ab34Slling 	/* Read in the vdev name-value pair list (112K). */
1453b1b8ab34Slling 	if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
1454b1b8ab34Slling 		return (ERR_READ);
1455b1b8ab34Slling 
1456b1b8ab34Slling 	vdev = (vdev_phys_t *)stack;
1457e4c3b53dStaylor 	stack += sizeof (vdev_phys_t);
1458b1b8ab34Slling 
1459e7cbe64fSgw 	if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
1460b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
1461e7cbe64fSgw 
1462e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
1463e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL))
1464e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1465e7cbe64fSgw 
1466e7cbe64fSgw 	if (pool_state == POOL_STATE_DESTROYED)
1467e7cbe64fSgw 		return (ERR_FILESYSTEM_NOT_FOUND);
1468e7cbe64fSgw 
1469e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
1470e7cbe64fSgw 	    current_rootpool, DATA_TYPE_STRING, NULL))
1471e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1472e7cbe64fSgw 
1473e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
1474e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL))
1475e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1476e7cbe64fSgw 
1477e7cbe64fSgw 	/* not an active device */
1478e7cbe64fSgw 	if (txg == 0)
1479e7cbe64fSgw 		return (ERR_NO_BOOTPATH);
1480e7cbe64fSgw 
1481ad135b5dSChristopher Siden 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, outversion,
1482fe3e2633SEric Taylor 	    DATA_TYPE_UINT64, NULL))
1483fe3e2633SEric Taylor 		return (ERR_FSYS_CORRUPT);
1484ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(*outversion))
1485fe3e2633SEric Taylor 		return (ERR_NEWER_VERSION);
1486e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1487e7cbe64fSgw 	    DATA_TYPE_NVLIST, NULL))
1488e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1489051aabe6Staylor 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1490051aabe6Staylor 	    DATA_TYPE_UINT64, NULL))
1491051aabe6Staylor 		return (ERR_FSYS_CORRUPT);
149281b2d573SHans Rosenfeld 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_ASHIFT, outashift,
149381b2d573SHans Rosenfeld 	    DATA_TYPE_UINT64, NULL) != 0)
149481b2d573SHans Rosenfeld 		return (ERR_FSYS_CORRUPT);
149521ecdf64SLin Ling 	if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
1496e7cbe64fSgw 		return (ERR_NO_BOOTPATH);
1497e23347b1SEric Taylor 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
1498e23347b1SEric Taylor 	    DATA_TYPE_UINT64, NULL))
1499e23347b1SEric Taylor 		return (ERR_FSYS_CORRUPT);
1500ad135b5dSChristopher Siden 
1501ad135b5dSChristopher Siden 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1502ad135b5dSChristopher Siden 	    &features, DATA_TYPE_NVLIST, NULL) == 0) {
1503ad135b5dSChristopher Siden 		char *nvp;
1504ad135b5dSChristopher Siden 		char *name = stack;
1505ad135b5dSChristopher Siden 		stack += MAXNAMELEN;
1506ad135b5dSChristopher Siden 
1507ad135b5dSChristopher Siden 		for (nvp = nvlist_next_nvpair(features, NULL);
1508ad135b5dSChristopher Siden 		    nvp != NULL;
1509ad135b5dSChristopher Siden 		    nvp = nvlist_next_nvpair(features, nvp)) {
1510ad135b5dSChristopher Siden 			zap_attribute_t za;
1511ad135b5dSChristopher Siden 
1512ad135b5dSChristopher Siden 			if (nvpair_name(nvp, name, MAXNAMELEN) != 0)
1513ad135b5dSChristopher Siden 				return (ERR_FSYS_CORRUPT);
1514ad135b5dSChristopher Siden 
1515ad135b5dSChristopher Siden 			za.za_integer_length = 8;
1516ad135b5dSChristopher Siden 			za.za_num_integers = 1;
1517ad135b5dSChristopher Siden 			za.za_first_integer = 1;
1518ad135b5dSChristopher Siden 			za.za_name = name;
1519ad135b5dSChristopher Siden 			if (check_feature(&za, spa_feature_names, stack) != 0)
1520ad135b5dSChristopher Siden 				return (ERR_NEWER_VERSION);
1521ad135b5dSChristopher Siden 		}
1522ad135b5dSChristopher Siden 	}
1523ad135b5dSChristopher Siden 
1524e7cbe64fSgw 	return (0);
1525b1b8ab34Slling }
1526b1b8ab34Slling 
1527b1b8ab34Slling /*
1528b1b8ab34Slling  * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1529b1b8ab34Slling  * to the memory address MOS.
1530b1b8ab34Slling  *
1531b1b8ab34Slling  * Return:
1532b1b8ab34Slling  *	1 - success
1533b1b8ab34Slling  *	0 - failure
1534b1b8ab34Slling  */
1535b1b8ab34Slling int
1536b1b8ab34Slling zfs_mount(void)
1537b1b8ab34Slling {
153881b2d573SHans Rosenfeld 	char *stack, *ub_array;
1539b1b8ab34Slling 	int label = 0;
154081b2d573SHans Rosenfeld 	uberblock_t *ubbest;
1541b1b8ab34Slling 	objset_phys_t *osp;
1542051aabe6Staylor 	char tmp_bootpath[MAXNAMELEN];
1543051aabe6Staylor 	char tmp_devid[MAXNAMELEN];
1544ad135b5dSChristopher Siden 	uint64_t tmp_guid, ashift, version;
1545e23347b1SEric Taylor 	uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
1546bbe6aa77SJan Setje-Eilers 	int err = errnum; /* preserve previous errnum state */
1547051aabe6Staylor 
1548051aabe6Staylor 	/* if it's our first time here, zero the best uberblock out */
1549e23347b1SEric Taylor 	if (best_drive == 0 && best_part == 0 && find_best_root) {
1550e37b211cStaylor 		grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
1551e23347b1SEric Taylor 		pool_guid = 0;
1552e23347b1SEric Taylor 	}
1553b1b8ab34Slling 
1554b1b8ab34Slling 	stackbase = ZFS_SCRATCH;
1555b1b8ab34Slling 	stack = stackbase;
155681b2d573SHans Rosenfeld 	ub_array = stack;
1557b1b8ab34Slling 	stack += VDEV_UBERBLOCK_RING;
1558b1b8ab34Slling 
1559b1b8ab34Slling 	osp = (objset_phys_t *)stack;
1560b1b8ab34Slling 	stack += sizeof (objset_phys_t);
1561e23347b1SEric Taylor 	adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
1562b1b8ab34Slling 
1563e23347b1SEric Taylor 	for (label = 0; label < VDEV_LABELS; label++) {
156498c507c4SJan Setje-Eilers 
156598c507c4SJan Setje-Eilers 		/*
156698c507c4SJan Setje-Eilers 		 * some eltorito stacks don't give us a size and
156798c507c4SJan Setje-Eilers 		 * we end up setting the size to MAXUINT, further
156898c507c4SJan Setje-Eilers 		 * some of these devices stop working once a single
156998c507c4SJan Setje-Eilers 		 * read past the end has been issued. Checking
157098c507c4SJan Setje-Eilers 		 * for a maximum part_length and skipping the backup
157198c507c4SJan Setje-Eilers 		 * labels at the end of the slice/partition/device
157298c507c4SJan Setje-Eilers 		 * avoids breaking down on such devices.
157398c507c4SJan Setje-Eilers 		 */
157498c507c4SJan Setje-Eilers 		if (part_length == MAXUINT && label == 2)
157598c507c4SJan Setje-Eilers 			break;
157698c507c4SJan Setje-Eilers 
1577e23347b1SEric Taylor 		uint64_t sector = vdev_label_start(adjpl,
1578e23347b1SEric Taylor 		    label) >> SPA_MINBLOCKSHIFT;
1579b1b8ab34Slling 
1580b1b8ab34Slling 		/* Read in the uberblock ring (128K). */
1581e23347b1SEric Taylor 		if (devread(sector  +
158281b2d573SHans Rosenfeld 		    ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT),
158381b2d573SHans Rosenfeld 		    0, VDEV_UBERBLOCK_RING, ub_array) == 0)
1584b1b8ab34Slling 			continue;
1585b1b8ab34Slling 
158681b2d573SHans Rosenfeld 		if (check_pool_label(sector, stack, tmp_devid,
1587ad135b5dSChristopher Siden 		    tmp_bootpath, &tmp_guid, &ashift, &version))
158881b2d573SHans Rosenfeld 			continue;
158981b2d573SHans Rosenfeld 
159081b2d573SHans Rosenfeld 		if (pool_guid == 0)
159181b2d573SHans Rosenfeld 			pool_guid = tmp_guid;
159281b2d573SHans Rosenfeld 
159381b2d573SHans Rosenfeld 		if ((ubbest = find_bestub(ub_array, ashift, sector)) == NULL ||
159481b2d573SHans Rosenfeld 		    zio_read(&ubbest->ub_rootbp, osp, stack) != 0)
159581b2d573SHans Rosenfeld 			continue;
159681b2d573SHans Rosenfeld 
159781b2d573SHans Rosenfeld 		VERIFY_OS_TYPE(osp, DMU_OST_META);
159881b2d573SHans Rosenfeld 
1599ad135b5dSChristopher Siden 		if (version >= SPA_VERSION_FEATURES &&
1600ad135b5dSChristopher Siden 		    check_mos_features(&osp->os_meta_dnode, stack) != 0)
1601ad135b5dSChristopher Siden 			continue;
1602ad135b5dSChristopher Siden 
160381b2d573SHans Rosenfeld 		if (find_best_root && ((pool_guid != tmp_guid) ||
160481b2d573SHans Rosenfeld 		    vdev_uberblock_compare(ubbest, &(current_uberblock)) <= 0))
160581b2d573SHans Rosenfeld 			continue;
160681b2d573SHans Rosenfeld 
160781b2d573SHans Rosenfeld 		/* Got the MOS. Save it at the memory addr MOS. */
160881b2d573SHans Rosenfeld 		grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
160981b2d573SHans Rosenfeld 		grub_memmove(&current_uberblock, ubbest, sizeof (uberblock_t));
161081b2d573SHans Rosenfeld 		grub_memmove(current_bootpath, tmp_bootpath, MAXNAMELEN);
161181b2d573SHans Rosenfeld 		grub_memmove(current_devid, tmp_devid, grub_strlen(tmp_devid));
161281b2d573SHans Rosenfeld 		is_zfs_mount = 1;
161381b2d573SHans Rosenfeld 		return (1);
1614b1b8ab34Slling 	}
1615b1b8ab34Slling 
1616bbe6aa77SJan Setje-Eilers 	/*
1617bbe6aa77SJan Setje-Eilers 	 * While some fs impls. (tftp) rely on setting and keeping
1618bbe6aa77SJan Setje-Eilers 	 * global errnums set, others won't reset it and will break
1619bbe6aa77SJan Setje-Eilers 	 * when issuing rawreads. The goal here is to simply not
1620bbe6aa77SJan Setje-Eilers 	 * have zfs mount attempts impact the previous state.
1621bbe6aa77SJan Setje-Eilers 	 */
1622bbe6aa77SJan Setje-Eilers 	errnum = err;
1623b1b8ab34Slling 	return (0);
1624b1b8ab34Slling }
1625b1b8ab34Slling 
1626b1b8ab34Slling /*
1627b1b8ab34Slling  * zfs_open() locates a file in the rootpool by following the
1628b1b8ab34Slling  * MOS and places the dnode of the file in the memory address DNODE.
1629b1b8ab34Slling  *
1630b1b8ab34Slling  * Return:
1631b1b8ab34Slling  *	1 - success
1632b1b8ab34Slling  *	0 - failure
1633b1b8ab34Slling  */
1634b1b8ab34Slling int
1635b1b8ab34Slling zfs_open(char *filename)
1636b1b8ab34Slling {
1637b1b8ab34Slling 	char *stack;
1638b1b8ab34Slling 	dnode_phys_t *mdn;
1639b1b8ab34Slling 
1640b1b8ab34Slling 	file_buf = NULL;
1641b1b8ab34Slling 	stackbase = ZFS_SCRATCH;
1642b1b8ab34Slling 	stack = stackbase;
1643b1b8ab34Slling 
1644b1b8ab34Slling 	mdn = (dnode_phys_t *)stack;
1645b1b8ab34Slling 	stack += sizeof (dnode_phys_t);
1646b1b8ab34Slling 
1647b1b8ab34Slling 	dnode_mdn = NULL;
1648b1b8ab34Slling 	dnode_buf = (dnode_phys_t *)stack;
1649b1b8ab34Slling 	stack += 1<<DNODE_BLOCK_SHIFT;
1650b1b8ab34Slling 
1651b1b8ab34Slling 	/*
1652b1b8ab34Slling 	 * menu.lst is placed at the root pool filesystem level,
1653b1b8ab34Slling 	 * do not goto 'current_bootfs'.
1654b1b8ab34Slling 	 */
1655eb2bd662Svikram 	if (is_top_dataset_file(filename)) {
1656b1b8ab34Slling 		if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
1657b1b8ab34Slling 			return (0);
1658b1b8ab34Slling 
1659b1b8ab34Slling 		current_bootfs_obj = 0;
1660b1b8ab34Slling 	} else {
1661b1b8ab34Slling 		if (current_bootfs[0] == '\0') {
1662b1b8ab34Slling 			/* Get the default root filesystem object number */
1663ae8180dbSlling 			if (errnum = get_default_bootfsobj(MOS,
1664ae8180dbSlling 			    &current_bootfs_obj, stack))
1665b1b8ab34Slling 				return (0);
1666b1b8ab34Slling 
1667b1b8ab34Slling 			if (errnum = get_objset_mdn(MOS, NULL,
1668b1b8ab34Slling 			    &current_bootfs_obj, mdn, stack))
1669b1b8ab34Slling 				return (0);
1670b1b8ab34Slling 		} else {
1671b35c6776Staylor 			if (errnum = get_objset_mdn(MOS, current_bootfs,
1672b35c6776Staylor 			    &current_bootfs_obj, mdn, stack)) {
1673051aabe6Staylor 				grub_memset(current_bootfs, 0, MAXNAMELEN);
1674b1b8ab34Slling 				return (0);
1675b35c6776Staylor 			}
1676b1b8ab34Slling 		}
1677b1b8ab34Slling 	}
1678b1b8ab34Slling 
1679b1b8ab34Slling 	if (dnode_get_path(mdn, filename, DNODE, stack)) {
1680b1b8ab34Slling 		errnum = ERR_FILE_NOT_FOUND;
1681b1b8ab34Slling 		return (0);
1682b1b8ab34Slling 	}
1683b1b8ab34Slling 
1684b1b8ab34Slling 	/* get the file size and set the file position to 0 */
16850a586ceaSMark Shellenbaum 
16860a586ceaSMark Shellenbaum 	/*
16870a586ceaSMark Shellenbaum 	 * For DMU_OT_SA we will need to locate the SIZE attribute
16880a586ceaSMark Shellenbaum 	 * attribute, which could be either in the bonus buffer
16890a586ceaSMark Shellenbaum 	 * or the "spill" block.
16900a586ceaSMark Shellenbaum 	 */
16910a586ceaSMark Shellenbaum 	if (DNODE->dn_bonustype == DMU_OT_SA) {
16920a586ceaSMark Shellenbaum 		sa_hdr_phys_t *sahdrp;
16930a586ceaSMark Shellenbaum 		int hdrsize;
16940a586ceaSMark Shellenbaum 
16950a586ceaSMark Shellenbaum 		if (DNODE->dn_bonuslen != 0) {
16960a586ceaSMark Shellenbaum 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(DNODE);
16970a586ceaSMark Shellenbaum 		} else {
16980a586ceaSMark Shellenbaum 			if (DNODE->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
16990a586ceaSMark Shellenbaum 				blkptr_t *bp = &DNODE->dn_spill;
17000a586ceaSMark Shellenbaum 				void *buf;
17010a586ceaSMark Shellenbaum 
17020a586ceaSMark Shellenbaum 				buf = (void *)stack;
17030a586ceaSMark Shellenbaum 				stack += BP_GET_LSIZE(bp);
17040a586ceaSMark Shellenbaum 
17050a586ceaSMark Shellenbaum 				/* reset errnum to rawread() failure */
17060a586ceaSMark Shellenbaum 				errnum = 0;
17070a586ceaSMark Shellenbaum 				if (zio_read(bp, buf, stack) != 0) {
17080a586ceaSMark Shellenbaum 					return (0);
17090a586ceaSMark Shellenbaum 				}
17100a586ceaSMark Shellenbaum 				sahdrp = buf;
17110a586ceaSMark Shellenbaum 			} else {
17120a586ceaSMark Shellenbaum 				errnum = ERR_FSYS_CORRUPT;
17130a586ceaSMark Shellenbaum 				return (0);
17140a586ceaSMark Shellenbaum 			}
17150a586ceaSMark Shellenbaum 		}
17160a586ceaSMark Shellenbaum 		hdrsize = SA_HDR_SIZE(sahdrp);
17170a586ceaSMark Shellenbaum 		filemax = *(uint64_t *)((char *)sahdrp + hdrsize +
17180a586ceaSMark Shellenbaum 		    SA_SIZE_OFFSET);
17190a586ceaSMark Shellenbaum 	} else {
17200a586ceaSMark Shellenbaum 		filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
17210a586ceaSMark Shellenbaum 	}
1722b1b8ab34Slling 	filepos = 0;
1723b1b8ab34Slling 
1724b1b8ab34Slling 	dnode_buf = NULL;
1725b1b8ab34Slling 	return (1);
1726b1b8ab34Slling }
1727b1b8ab34Slling 
1728b1b8ab34Slling /*
1729b1b8ab34Slling  * zfs_read reads in the data blocks pointed by the DNODE.
1730b1b8ab34Slling  *
1731b1b8ab34Slling  * Return:
1732b1b8ab34Slling  *	len - the length successfully read in to the buffer
1733b1b8ab34Slling  *	0   - failure
1734b1b8ab34Slling  */
1735b1b8ab34Slling int
1736b1b8ab34Slling zfs_read(char *buf, int len)
1737b1b8ab34Slling {
1738b1b8ab34Slling 	char *stack;
1739b1b8ab34Slling 	int blksz, length, movesize;
1740b1b8ab34Slling 
1741b1b8ab34Slling 	if (file_buf == NULL) {
1742b1b8ab34Slling 		file_buf = stackbase;
1743b1b8ab34Slling 		stackbase += SPA_MAXBLOCKSIZE;
1744b1b8ab34Slling 		file_start = file_end = 0;
1745b1b8ab34Slling 	}
1746b1b8ab34Slling 	stack = stackbase;
1747b1b8ab34Slling 
1748b1b8ab34Slling 	/*
1749b1b8ab34Slling 	 * If offset is in memory, move it into the buffer provided and return.
1750b1b8ab34Slling 	 */
1751b1b8ab34Slling 	if (filepos >= file_start && filepos+len <= file_end) {
1752b1b8ab34Slling 		grub_memmove(buf, file_buf + filepos - file_start, len);
1753b1b8ab34Slling 		filepos += len;
1754b1b8ab34Slling 		return (len);
1755b1b8ab34Slling 	}
1756b1b8ab34Slling 
1757b1b8ab34Slling 	blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1758b1b8ab34Slling 
1759b1b8ab34Slling 	/*
1760b1b8ab34Slling 	 * Entire Dnode is too big to fit into the space available.  We
1761b1b8ab34Slling 	 * will need to read it in chunks.  This could be optimized to
1762b1b8ab34Slling 	 * read in as large a chunk as there is space available, but for
1763b1b8ab34Slling 	 * now, this only reads in one data block at a time.
1764b1b8ab34Slling 	 */
1765b1b8ab34Slling 	length = len;
1766b1b8ab34Slling 	while (length) {
1767b1b8ab34Slling 		/*
1768b1b8ab34Slling 		 * Find requested blkid and the offset within that block.
1769b1b8ab34Slling 		 */
1770b1b8ab34Slling 		uint64_t blkid = filepos / blksz;
1771b1b8ab34Slling 
1772b1b8ab34Slling 		if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
1773b1b8ab34Slling 			return (0);
1774b1b8ab34Slling 
1775b1b8ab34Slling 		file_start = blkid * blksz;
1776b1b8ab34Slling 		file_end = file_start + blksz;
1777b1b8ab34Slling 
1778b1b8ab34Slling 		movesize = MIN(length, file_end - filepos);
1779b1b8ab34Slling 
1780b1b8ab34Slling 		grub_memmove(buf, file_buf + filepos - file_start,
1781b1b8ab34Slling 		    movesize);
1782b1b8ab34Slling 		buf += movesize;
1783b1b8ab34Slling 		length -= movesize;
1784b1b8ab34Slling 		filepos += movesize;
1785b1b8ab34Slling 	}
1786b1b8ab34Slling 
1787b1b8ab34Slling 	return (len);
1788b1b8ab34Slling }
1789b1b8ab34Slling 
1790b1b8ab34Slling /*
1791b1b8ab34Slling  * No-Op
1792b1b8ab34Slling  */
1793b1b8ab34Slling int
1794b1b8ab34Slling zfs_embed(int *start_sector, int needed_sectors)
1795b1b8ab34Slling {
1796b1b8ab34Slling 	return (1);
1797b1b8ab34Slling }
1798b1b8ab34Slling 
1799b1b8ab34Slling #endif /* FSYS_ZFS */
1800