xref: /illumos-gate/usr/src/grub/grub-0.97/stage2/fsys_zfs.c (revision e23347b1b88ce2c0847fad6e9467a1f953597aa7)
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  */
19b1b8ab34Slling /*
202264ca7fSLin Ling  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
21b1b8ab34Slling  * Use is subject to license terms.
22b1b8ab34Slling  */
23b1b8ab34Slling 
24b1b8ab34Slling /*
25b1b8ab34Slling  * The zfs plug-in routines for GRUB are:
26b1b8ab34Slling  *
27b1b8ab34Slling  * zfs_mount() - locates a valid uberblock of the root pool and reads
28b1b8ab34Slling  *		in its MOS at the memory address MOS.
29b1b8ab34Slling  *
30b1b8ab34Slling  * zfs_open() - locates a plain file object by following the MOS
31b1b8ab34Slling  *		and places its dnode at the memory address DNODE.
32b1b8ab34Slling  *
33b1b8ab34Slling  * zfs_read() - read in the data blocks pointed by the DNODE.
34b1b8ab34Slling  *
35b1b8ab34Slling  * ZFS_SCRATCH is used as a working area.
36b1b8ab34Slling  *
37b1b8ab34Slling  * (memory addr)   MOS      DNODE	ZFS_SCRATCH
38b1b8ab34Slling  *		    |         |          |
39b1b8ab34Slling  *	    +-------V---------V----------V---------------+
40b1b8ab34Slling  *   memory |       | dnode   | dnode    |  scratch      |
41b1b8ab34Slling  *	    |       | 512B    | 512B     |  area         |
42b1b8ab34Slling  *	    +--------------------------------------------+
43b1b8ab34Slling  */
44b1b8ab34Slling 
45b1b8ab34Slling #ifdef	FSYS_ZFS
46b1b8ab34Slling 
47b1b8ab34Slling #include "shared.h"
48b1b8ab34Slling #include "filesys.h"
49b1b8ab34Slling #include "fsys_zfs.h"
50b1b8ab34Slling 
51b1b8ab34Slling /* cache for a file block of the currently zfs_open()-ed file */
52b1b8ab34Slling static void *file_buf = NULL;
53b1b8ab34Slling static uint64_t file_start = 0;
54b1b8ab34Slling static uint64_t file_end = 0;
55b1b8ab34Slling 
56b1b8ab34Slling /* cache for a dnode block */
57b1b8ab34Slling static dnode_phys_t *dnode_buf = NULL;
58b1b8ab34Slling static dnode_phys_t *dnode_mdn = NULL;
59b1b8ab34Slling static uint64_t dnode_start = 0;
60b1b8ab34Slling static uint64_t dnode_end = 0;
61b1b8ab34Slling 
62*e23347b1SEric Taylor static uint64_t pool_guid = 0;
63051aabe6Staylor static uberblock_t current_uberblock;
64b1b8ab34Slling static char *stackbase;
65b1b8ab34Slling 
66b1b8ab34Slling decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
67b1b8ab34Slling {
6815e6edf1Sgw 	{"inherit", 0},			/* ZIO_COMPRESS_INHERIT */
69b1b8ab34Slling 	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
7015e6edf1Sgw 	{"off", 0},			/* ZIO_COMPRESS_OFF */
7115e6edf1Sgw 	{"lzjb", lzjb_decompress},	/* ZIO_COMPRESS_LZJB */
7215e6edf1Sgw 	{"empty", 0}			/* ZIO_COMPRESS_EMPTY */
73b1b8ab34Slling };
74b1b8ab34Slling 
75cd9c78d9SLin Ling static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
76cd9c78d9SLin Ling 
77b1b8ab34Slling /*
78b1b8ab34Slling  * Our own version of bcmp().
79b1b8ab34Slling  */
80b1b8ab34Slling static int
81b1b8ab34Slling zfs_bcmp(const void *s1, const void *s2, size_t n)
82b1b8ab34Slling {
83b1b8ab34Slling 	const uchar_t *ps1 = s1;
84b1b8ab34Slling 	const uchar_t *ps2 = s2;
85b1b8ab34Slling 
86b1b8ab34Slling 	if (s1 != s2 && n != 0) {
87b1b8ab34Slling 		do {
88b1b8ab34Slling 			if (*ps1++ != *ps2++)
89b1b8ab34Slling 				return (1);
90b1b8ab34Slling 		} while (--n != 0);
91b1b8ab34Slling 	}
92b1b8ab34Slling 
93b1b8ab34Slling 	return (0);
94b1b8ab34Slling }
95b1b8ab34Slling 
96b1b8ab34Slling /*
97b1b8ab34Slling  * Our own version of log2().  Same thing as highbit()-1.
98b1b8ab34Slling  */
99b1b8ab34Slling static int
100b1b8ab34Slling zfs_log2(uint64_t num)
101b1b8ab34Slling {
102b1b8ab34Slling 	int i = 0;
103b1b8ab34Slling 
104b1b8ab34Slling 	while (num > 1) {
105b1b8ab34Slling 		i++;
106b1b8ab34Slling 		num = num >> 1;
107b1b8ab34Slling 	}
108b1b8ab34Slling 
109b1b8ab34Slling 	return (i);
110b1b8ab34Slling }
111b1b8ab34Slling 
112b1b8ab34Slling /* Checksum Functions */
113b1b8ab34Slling static void
114b1b8ab34Slling zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
115b1b8ab34Slling {
116b1b8ab34Slling 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
117b1b8ab34Slling }
118b1b8ab34Slling 
119b1b8ab34Slling /* Checksum Table and Values */
120b1b8ab34Slling zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
121b1b8ab34Slling 	NULL,			NULL,			0, 0,	"inherit",
122b1b8ab34Slling 	NULL,			NULL,			0, 0,	"on",
123b1b8ab34Slling 	zio_checksum_off,	zio_checksum_off,	0, 0,	"off",
124b1b8ab34Slling 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 1,	"label",
125b1b8ab34Slling 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 1,	"gang_header",
126b1b8ab34Slling 	fletcher_2_native,	fletcher_2_byteswap,	0, 1,	"zilog",
127b1b8ab34Slling 	fletcher_2_native,	fletcher_2_byteswap,	0, 0,	"fletcher2",
128b1b8ab34Slling 	fletcher_4_native,	fletcher_4_byteswap,	1, 0,	"fletcher4",
129b1b8ab34Slling 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 0,	"SHA256",
130b1b8ab34Slling };
131b1b8ab34Slling 
132b1b8ab34Slling /*
133b1b8ab34Slling  * zio_checksum_verify: Provides support for checksum verification.
134b1b8ab34Slling  *
135b1b8ab34Slling  * Fletcher2, Fletcher4, and SHA256 are supported.
136b1b8ab34Slling  *
137b1b8ab34Slling  * Return:
138b1b8ab34Slling  * 	-1 = Failure
139b1b8ab34Slling  *	 0 = Success
140b1b8ab34Slling  */
141b1b8ab34Slling static int
142b1b8ab34Slling zio_checksum_verify(blkptr_t *bp, char *data, int size)
143b1b8ab34Slling {
144b1b8ab34Slling 	zio_cksum_t zc = bp->blk_cksum;
145cd9c78d9SLin Ling 	uint32_t checksum = BP_GET_CHECKSUM(bp);
146b1b8ab34Slling 	int byteswap = BP_SHOULD_BYTESWAP(bp);
147b1b8ab34Slling 	zio_block_tail_t *zbt = (zio_block_tail_t *)(data + size) - 1;
148b1b8ab34Slling 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
149b1b8ab34Slling 	zio_cksum_t actual_cksum, expected_cksum;
150b1b8ab34Slling 
151b1b8ab34Slling 	/* byteswap is not supported */
152b1b8ab34Slling 	if (byteswap)
153b1b8ab34Slling 		return (-1);
154b1b8ab34Slling 
155b1b8ab34Slling 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
156b1b8ab34Slling 		return (-1);
157b1b8ab34Slling 
158b1b8ab34Slling 	if (ci->ci_zbt) {
159cd9c78d9SLin Ling 		expected_cksum = zbt->zbt_cksum;
160cd9c78d9SLin Ling 		zbt->zbt_cksum = zc;
161cd9c78d9SLin Ling 		ci->ci_func[0](data, size, &actual_cksum);
162cd9c78d9SLin Ling 		zbt->zbt_cksum = expected_cksum;
163b1b8ab34Slling 		zc = expected_cksum;
164b1b8ab34Slling 
165b1b8ab34Slling 	} else {
166b1b8ab34Slling 		ci->ci_func[byteswap](data, size, &actual_cksum);
167b1b8ab34Slling 	}
168b1b8ab34Slling 
169b1b8ab34Slling 	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
170b1b8ab34Slling 	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
171b1b8ab34Slling 	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
172b1b8ab34Slling 	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
173b1b8ab34Slling 		return (-1);
174b1b8ab34Slling 
175b1b8ab34Slling 	return (0);
176b1b8ab34Slling }
177b1b8ab34Slling 
178b1b8ab34Slling /*
179*e23347b1SEric Taylor  * vdev_label_start returns the physical disk offset (in bytes) of
180*e23347b1SEric Taylor  * label "l".
181b1b8ab34Slling  */
182e7cbe64fSgw static uint64_t
183*e23347b1SEric Taylor vdev_label_start(uint64_t psize, int l)
184b1b8ab34Slling {
185*e23347b1SEric Taylor 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
186b1b8ab34Slling 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
187b1b8ab34Slling }
188b1b8ab34Slling 
189b1b8ab34Slling /*
190b1b8ab34Slling  * vdev_uberblock_compare takes two uberblock structures and returns an integer
191b1b8ab34Slling  * indicating the more recent of the two.
192b1b8ab34Slling  * 	Return Value = 1 if ub2 is more recent
193b1b8ab34Slling  * 	Return Value = -1 if ub1 is more recent
194b1b8ab34Slling  * The most recent uberblock is determined using its transaction number and
195b1b8ab34Slling  * timestamp.  The uberblock with the highest transaction number is
196b1b8ab34Slling  * considered "newer".  If the transaction numbers of the two blocks match, the
197b1b8ab34Slling  * timestamps are compared to determine the "newer" of the two.
198b1b8ab34Slling  */
199b1b8ab34Slling static int
200b1b8ab34Slling vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
201b1b8ab34Slling {
202b1b8ab34Slling 	if (ub1->ub_txg < ub2->ub_txg)
203b1b8ab34Slling 		return (-1);
204b1b8ab34Slling 	if (ub1->ub_txg > ub2->ub_txg)
205b1b8ab34Slling 		return (1);
206b1b8ab34Slling 
207b1b8ab34Slling 	if (ub1->ub_timestamp < ub2->ub_timestamp)
208b1b8ab34Slling 		return (-1);
209b1b8ab34Slling 	if (ub1->ub_timestamp > ub2->ub_timestamp)
210b1b8ab34Slling 		return (1);
211b1b8ab34Slling 
212b1b8ab34Slling 	return (0);
213b1b8ab34Slling }
214b1b8ab34Slling 
215b1b8ab34Slling /*
216b1b8ab34Slling  * Three pieces of information are needed to verify an uberblock: the magic
217b1b8ab34Slling  * number, the version number, and the checksum.
218b1b8ab34Slling  *
219b1b8ab34Slling  * Currently Implemented: version number, magic number
220b1b8ab34Slling  * Need to Implement: checksum
221b1b8ab34Slling  *
222b1b8ab34Slling  * Return:
223b1b8ab34Slling  *     0 - Success
224b1b8ab34Slling  *    -1 - Failure
225b1b8ab34Slling  */
226b1b8ab34Slling static int
227*e23347b1SEric Taylor uberblock_verify(uberblock_phys_t *ub, uint64_t offset)
228b1b8ab34Slling {
229b1b8ab34Slling 
230b1b8ab34Slling 	uberblock_t *uber = &ub->ubp_uberblock;
231b1b8ab34Slling 	blkptr_t bp;
232b1b8ab34Slling 
233b1b8ab34Slling 	BP_ZERO(&bp);
234b1b8ab34Slling 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
235b1b8ab34Slling 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
236b1b8ab34Slling 	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
237b1b8ab34Slling 
238b1b8ab34Slling 	if (zio_checksum_verify(&bp, (char *)ub, UBERBLOCK_SIZE) != 0)
239b1b8ab34Slling 		return (-1);
240b1b8ab34Slling 
241b1b8ab34Slling 	if (uber->ub_magic == UBERBLOCK_MAGIC &&
242bb0ade09Sahrens 	    uber->ub_version > 0 && uber->ub_version <= SPA_VERSION)
243b1b8ab34Slling 		return (0);
244b1b8ab34Slling 
245b1b8ab34Slling 	return (-1);
246b1b8ab34Slling }
247b1b8ab34Slling 
248b1b8ab34Slling /*
249b1b8ab34Slling  * Find the best uberblock.
250b1b8ab34Slling  * Return:
251b1b8ab34Slling  *    Success - Pointer to the best uberblock.
252b1b8ab34Slling  *    Failure - NULL
253b1b8ab34Slling  */
254b1b8ab34Slling static uberblock_phys_t *
255*e23347b1SEric Taylor find_bestub(uberblock_phys_t *ub_array, uint64_t sector)
256b1b8ab34Slling {
257b1b8ab34Slling 	uberblock_phys_t *ubbest = NULL;
258*e23347b1SEric Taylor 	uint64_t offset;
259*e23347b1SEric Taylor 	int i;
260b1b8ab34Slling 
261b1b8ab34Slling 	for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++) {
262*e23347b1SEric Taylor 		offset = (sector << SPA_MINBLOCKSHIFT) +
263*e23347b1SEric Taylor 		    VDEV_UBERBLOCK_OFFSET(i);
264b1b8ab34Slling 		if (uberblock_verify(&ub_array[i], offset) == 0) {
265b1b8ab34Slling 			if (ubbest == NULL) {
266b1b8ab34Slling 				ubbest = &ub_array[i];
267e7437265Sahrens 			} else if (vdev_uberblock_compare(
268e7437265Sahrens 			    &(ub_array[i].ubp_uberblock),
269e7437265Sahrens 			    &(ubbest->ubp_uberblock)) > 0) {
270e7437265Sahrens 				ubbest = &ub_array[i];
271b1b8ab34Slling 			}
272b1b8ab34Slling 		}
273b1b8ab34Slling 	}
274b1b8ab34Slling 
275b1b8ab34Slling 	return (ubbest);
276b1b8ab34Slling }
277b1b8ab34Slling 
278b1b8ab34Slling /*
279cd9c78d9SLin Ling  * Read a block of data based on the gang block address dva,
280cd9c78d9SLin Ling  * and put its data in buf.
281b1b8ab34Slling  *
282b1b8ab34Slling  * Return:
283b1b8ab34Slling  *	0 - success
284cd9c78d9SLin Ling  *	1 - failure
285b1b8ab34Slling  */
286b1b8ab34Slling static int
287cd9c78d9SLin Ling zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
288b1b8ab34Slling {
289cd9c78d9SLin Ling 	zio_gbh_phys_t *zio_gb;
290b1b8ab34Slling 	uint64_t offset, sector;
291cd9c78d9SLin Ling 	blkptr_t tmpbp;
292cd9c78d9SLin Ling 	int i;
293b1b8ab34Slling 
294cd9c78d9SLin Ling 	zio_gb = (zio_gbh_phys_t *)stack;
295cd9c78d9SLin Ling 	stack += SPA_GANGBLOCKSIZE;
296cd9c78d9SLin Ling 	offset = DVA_GET_OFFSET(dva);
297cd9c78d9SLin Ling 	sector =  DVA_OFFSET_TO_PHYS_SECTOR(offset);
298b1b8ab34Slling 
299cd9c78d9SLin Ling 	/* read in the gang block header */
300cd9c78d9SLin Ling 	if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
301cd9c78d9SLin Ling 		grub_printf("failed to read in a gang block header\n");
302cd9c78d9SLin Ling 		return (1);
303cd9c78d9SLin Ling 	}
304cd9c78d9SLin Ling 
305cd9c78d9SLin Ling 	/* self checksuming the gang block header */
306cd9c78d9SLin Ling 	BP_ZERO(&tmpbp);
307cd9c78d9SLin Ling 	BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
308cd9c78d9SLin Ling 	BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
309cd9c78d9SLin Ling 	ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
310cd9c78d9SLin Ling 	    DVA_GET_OFFSET(dva), bp->blk_birth, 0);
311cd9c78d9SLin Ling 	if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
312cd9c78d9SLin Ling 		grub_printf("failed to checksum a gang block header\n");
313cd9c78d9SLin Ling 		return (1);
314cd9c78d9SLin Ling 	}
315cd9c78d9SLin Ling 
316cd9c78d9SLin Ling 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
317cd9c78d9SLin Ling 		if (zio_gb->zg_blkptr[i].blk_birth == 0)
318cd9c78d9SLin Ling 			continue;
319cd9c78d9SLin Ling 
320cd9c78d9SLin Ling 		if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
321cd9c78d9SLin Ling 			return (1);
322cd9c78d9SLin Ling 		buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
323cd9c78d9SLin Ling 	}
324cd9c78d9SLin Ling 
325cd9c78d9SLin Ling 	return (0);
326cd9c78d9SLin Ling }
327cd9c78d9SLin Ling 
328cd9c78d9SLin Ling /*
329cd9c78d9SLin Ling  * Read in a block of raw data to buf.
330cd9c78d9SLin Ling  *
331cd9c78d9SLin Ling  * Return:
332cd9c78d9SLin Ling  *	0 - success
333cd9c78d9SLin Ling  *	1 - failure
334cd9c78d9SLin Ling  */
335cd9c78d9SLin Ling static int
336cd9c78d9SLin Ling zio_read_data(blkptr_t *bp, void *buf, char *stack)
337cd9c78d9SLin Ling {
338cd9c78d9SLin Ling 	int i, psize;
339cd9c78d9SLin Ling 
340cd9c78d9SLin Ling 	psize = BP_GET_PSIZE(bp);
341ae8180dbSlling 
342b1b8ab34Slling 	/* pick a good dva from the block pointer */
343b1b8ab34Slling 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
344cd9c78d9SLin Ling 		uint64_t offset, sector;
345b1b8ab34Slling 
346b1b8ab34Slling 		if (bp->blk_dva[i].dva_word[0] == 0 &&
347b1b8ab34Slling 		    bp->blk_dva[i].dva_word[1] == 0)
348b1b8ab34Slling 			continue;
349b1b8ab34Slling 
350cd9c78d9SLin Ling 		if (DVA_GET_GANG(&bp->blk_dva[i])) {
351cd9c78d9SLin Ling 			if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) == 0)
352cd9c78d9SLin Ling 				return (0);
353b1b8ab34Slling 		} else {
354cd9c78d9SLin Ling 			/* read in a data block */
355cd9c78d9SLin Ling 			offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
356cd9c78d9SLin Ling 			sector =  DVA_OFFSET_TO_PHYS_SECTOR(offset);
357cd9c78d9SLin Ling 			if (devread(sector, 0, psize, buf))
358cd9c78d9SLin Ling 				return (0);
359b1b8ab34Slling 		}
360b1b8ab34Slling 	}
361b1b8ab34Slling 
362cd9c78d9SLin Ling 	return (1);
363cd9c78d9SLin Ling }
364cd9c78d9SLin Ling 
365cd9c78d9SLin Ling /*
366cd9c78d9SLin Ling  * Read in a block of data, verify its checksum, decompress if needed,
367cd9c78d9SLin Ling  * and put the uncompressed data in buf.
368cd9c78d9SLin Ling  *
369cd9c78d9SLin Ling  * Return:
370cd9c78d9SLin Ling  *	0 - success
371cd9c78d9SLin Ling  *	errnum - failure
372cd9c78d9SLin Ling  */
373cd9c78d9SLin Ling static int
374cd9c78d9SLin Ling zio_read(blkptr_t *bp, void *buf, char *stack)
375cd9c78d9SLin Ling {
376cd9c78d9SLin Ling 	int lsize, psize, comp;
377cd9c78d9SLin Ling 	char *retbuf;
378cd9c78d9SLin Ling 
379cd9c78d9SLin Ling 	comp = BP_GET_COMPRESS(bp);
380cd9c78d9SLin Ling 	lsize = BP_GET_LSIZE(bp);
381cd9c78d9SLin Ling 	psize = BP_GET_PSIZE(bp);
382cd9c78d9SLin Ling 
383cd9c78d9SLin Ling 	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
384cd9c78d9SLin Ling 	    (comp != ZIO_COMPRESS_OFF &&
385cd9c78d9SLin Ling 	    decomp_table[comp].decomp_func == NULL)) {
386cd9c78d9SLin Ling 		grub_printf("compression algorithm not supported\n");
387cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
388cd9c78d9SLin Ling 	}
389cd9c78d9SLin Ling 
390cd9c78d9SLin Ling 	if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
391cd9c78d9SLin Ling 		grub_printf("not enough memory allocated\n");
392cd9c78d9SLin Ling 		return (ERR_WONT_FIT);
393cd9c78d9SLin Ling 	}
394cd9c78d9SLin Ling 
395cd9c78d9SLin Ling 	retbuf = buf;
396cd9c78d9SLin Ling 	if (comp != ZIO_COMPRESS_OFF) {
397cd9c78d9SLin Ling 		buf = stack;
398cd9c78d9SLin Ling 		stack += psize;
399cd9c78d9SLin Ling 	}
400cd9c78d9SLin Ling 
401cd9c78d9SLin Ling 	if (zio_read_data(bp, buf, stack)) {
402cd9c78d9SLin Ling 		grub_printf("zio_read_data failed\n");
403cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
404cd9c78d9SLin Ling 	}
405cd9c78d9SLin Ling 
406cd9c78d9SLin Ling 	if (zio_checksum_verify(bp, buf, psize) != 0) {
407cd9c78d9SLin Ling 		grub_printf("checksum verification failed\n");
408cd9c78d9SLin Ling 		return (ERR_FSYS_CORRUPT);
409cd9c78d9SLin Ling 	}
410cd9c78d9SLin Ling 
411cd9c78d9SLin Ling 	if (comp != ZIO_COMPRESS_OFF)
412cd9c78d9SLin Ling 		decomp_table[comp].decomp_func(buf, retbuf, psize, lsize);
413cd9c78d9SLin Ling 
414cd9c78d9SLin Ling 	return (0);
415b1b8ab34Slling }
416b1b8ab34Slling 
417b1b8ab34Slling /*
418b1b8ab34Slling  * Get the block from a block id.
419b1b8ab34Slling  * push the block onto the stack.
420b1b8ab34Slling  *
421b1b8ab34Slling  * Return:
422b1b8ab34Slling  * 	0 - success
423b1b8ab34Slling  * 	errnum - failure
424b1b8ab34Slling  */
425b1b8ab34Slling static int
426b1b8ab34Slling dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
427b1b8ab34Slling {
428b1b8ab34Slling 	int idx, level;
429b1b8ab34Slling 	blkptr_t *bp_array = dn->dn_blkptr;
430b1b8ab34Slling 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
431b1b8ab34Slling 	blkptr_t *bp, *tmpbuf;
432b1b8ab34Slling 
433b1b8ab34Slling 	bp = (blkptr_t *)stack;
434b1b8ab34Slling 	stack += sizeof (blkptr_t);
435b1b8ab34Slling 
436b1b8ab34Slling 	tmpbuf = (blkptr_t *)stack;
437b1b8ab34Slling 	stack += 1<<dn->dn_indblkshift;
438b1b8ab34Slling 
439b1b8ab34Slling 	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
440b1b8ab34Slling 		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
441b1b8ab34Slling 		*bp = bp_array[idx];
442b1b8ab34Slling 		if (level == 0)
443b1b8ab34Slling 			tmpbuf = buf;
444ae8180dbSlling 		if (BP_IS_HOLE(bp)) {
445ae8180dbSlling 			grub_memset(buf, 0,
446ae8180dbSlling 			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
447ae8180dbSlling 			break;
448ae8180dbSlling 		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
449b1b8ab34Slling 			return (errnum);
450ae8180dbSlling 		}
451b1b8ab34Slling 
452b1b8ab34Slling 		bp_array = tmpbuf;
453b1b8ab34Slling 	}
454b1b8ab34Slling 
455b1b8ab34Slling 	return (0);
456b1b8ab34Slling }
457b1b8ab34Slling 
458b1b8ab34Slling /*
459b1b8ab34Slling  * mzap_lookup: Looks up property described by "name" and returns the value
460b1b8ab34Slling  * in "value".
461b1b8ab34Slling  *
462b1b8ab34Slling  * Return:
463b1b8ab34Slling  *	0 - success
464b1b8ab34Slling  *	errnum - failure
465b1b8ab34Slling  */
466b1b8ab34Slling static int
467b1b8ab34Slling mzap_lookup(mzap_phys_t *zapobj, int objsize, char *name,
468b1b8ab34Slling 	uint64_t *value)
469b1b8ab34Slling {
470b1b8ab34Slling 	int i, chunks;
471b1b8ab34Slling 	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
472b1b8ab34Slling 
473b1b8ab34Slling 	chunks = objsize/MZAP_ENT_LEN - 1;
474b1b8ab34Slling 	for (i = 0; i < chunks; i++) {
475b1b8ab34Slling 		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
476b1b8ab34Slling 			*value = mzap_ent[i].mze_value;
477b1b8ab34Slling 			return (0);
478b1b8ab34Slling 		}
479b1b8ab34Slling 	}
480b1b8ab34Slling 
481b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
482b1b8ab34Slling }
483b1b8ab34Slling 
484b1b8ab34Slling static uint64_t
485b1b8ab34Slling zap_hash(uint64_t salt, const char *name)
486b1b8ab34Slling {
487b1b8ab34Slling 	static uint64_t table[256];
488b1b8ab34Slling 	const uint8_t *cp;
489b1b8ab34Slling 	uint8_t c;
490b1b8ab34Slling 	uint64_t crc = salt;
491b1b8ab34Slling 
492b1b8ab34Slling 	if (table[128] == 0) {
493b1b8ab34Slling 		uint64_t *ct;
494b1b8ab34Slling 		int i, j;
495b1b8ab34Slling 		for (i = 0; i < 256; i++) {
496b1b8ab34Slling 			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
497b1b8ab34Slling 				*ct = (*ct >> 1) ^ (-(*ct & 1) &
498b1b8ab34Slling 				    ZFS_CRC64_POLY);
499b1b8ab34Slling 		}
500b1b8ab34Slling 	}
501b1b8ab34Slling 
502b1b8ab34Slling 	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
503b1b8ab34Slling 		errnum = ERR_FSYS_CORRUPT;
504b1b8ab34Slling 		return (0);
505b1b8ab34Slling 	}
506b1b8ab34Slling 
507b1b8ab34Slling 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
508b1b8ab34Slling 		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
509b1b8ab34Slling 
510b1b8ab34Slling 	/*
511b1b8ab34Slling 	 * Only use 28 bits, since we need 4 bits in the cookie for the
512b1b8ab34Slling 	 * collision differentiator.  We MUST use the high bits, since
513b1b8ab34Slling 	 * those are the onces that we first pay attention to when
514b1b8ab34Slling 	 * chosing the bucket.
515b1b8ab34Slling 	 */
516b1b8ab34Slling 	crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
517b1b8ab34Slling 
518b1b8ab34Slling 	return (crc);
519b1b8ab34Slling }
520b1b8ab34Slling 
521b1b8ab34Slling /*
522b1b8ab34Slling  * Only to be used on 8-bit arrays.
523b1b8ab34Slling  * array_len is actual len in bytes (not encoded le_value_length).
524b1b8ab34Slling  * buf is null-terminated.
525b1b8ab34Slling  */
526b1b8ab34Slling static int
527b1b8ab34Slling zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
528b1b8ab34Slling     int array_len, const char *buf)
529b1b8ab34Slling {
530b1b8ab34Slling 	int bseen = 0;
531b1b8ab34Slling 
532b1b8ab34Slling 	while (bseen < array_len) {
533b1b8ab34Slling 		struct zap_leaf_array *la =
534b1b8ab34Slling 		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
535b1b8ab34Slling 		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
536b1b8ab34Slling 
537b1b8ab34Slling 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
538b1b8ab34Slling 			return (0);
539b1b8ab34Slling 
540b1b8ab34Slling 		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
541b1b8ab34Slling 			break;
542b1b8ab34Slling 		chunk = la->la_next;
543b1b8ab34Slling 		bseen += toread;
544b1b8ab34Slling 	}
545b1b8ab34Slling 	return (bseen == array_len);
546b1b8ab34Slling }
547b1b8ab34Slling 
548b1b8ab34Slling /*
549b1b8ab34Slling  * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
550b1b8ab34Slling  * value for the property "name".
551b1b8ab34Slling  *
552b1b8ab34Slling  * Return:
553b1b8ab34Slling  *	0 - success
554b1b8ab34Slling  *	errnum - failure
555b1b8ab34Slling  */
556e7cbe64fSgw static int
557b1b8ab34Slling zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
558b1b8ab34Slling     const char *name, uint64_t *value)
559b1b8ab34Slling {
560b1b8ab34Slling 	uint16_t chunk;
561b1b8ab34Slling 	struct zap_leaf_entry *le;
562b1b8ab34Slling 
563b1b8ab34Slling 	/* Verify if this is a valid leaf block */
564b1b8ab34Slling 	if (l->l_hdr.lh_block_type != ZBT_LEAF)
565b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
566b1b8ab34Slling 	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
567b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
568b1b8ab34Slling 
569b1b8ab34Slling 	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
570b1b8ab34Slling 	    chunk != CHAIN_END; chunk = le->le_next) {
571b1b8ab34Slling 
572b1b8ab34Slling 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
573b1b8ab34Slling 			return (ERR_FSYS_CORRUPT);
574b1b8ab34Slling 
575b1b8ab34Slling 		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
576b1b8ab34Slling 
577b1b8ab34Slling 		/* Verify the chunk entry */
578b1b8ab34Slling 		if (le->le_type != ZAP_CHUNK_ENTRY)
579b1b8ab34Slling 			return (ERR_FSYS_CORRUPT);
580b1b8ab34Slling 
581b1b8ab34Slling 		if (le->le_hash != h)
582b1b8ab34Slling 			continue;
583b1b8ab34Slling 
584b1b8ab34Slling 		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
585b1b8ab34Slling 		    le->le_name_length, name)) {
586b1b8ab34Slling 
587b1b8ab34Slling 			struct zap_leaf_array *la;
588b1b8ab34Slling 			uint8_t *ip;
589b1b8ab34Slling 
590b1b8ab34Slling 			if (le->le_int_size != 8 || le->le_value_length != 1)
591e37b211cStaylor 				return (ERR_FSYS_CORRUPT);
592b1b8ab34Slling 
593b1b8ab34Slling 			/* get the uint64_t property value */
594b1b8ab34Slling 			la = &ZAP_LEAF_CHUNK(l, blksft,
595b1b8ab34Slling 			    le->le_value_chunk).l_array;
596b1b8ab34Slling 			ip = la->la_array;
597b1b8ab34Slling 
598b1b8ab34Slling 			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
599b1b8ab34Slling 			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
600b1b8ab34Slling 			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
601b1b8ab34Slling 			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
602b1b8ab34Slling 
603b1b8ab34Slling 			return (0);
604b1b8ab34Slling 		}
605b1b8ab34Slling 	}
606b1b8ab34Slling 
607b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
608b1b8ab34Slling }
609b1b8ab34Slling 
610b1b8ab34Slling /*
611b1b8ab34Slling  * Fat ZAP lookup
612b1b8ab34Slling  *
613b1b8ab34Slling  * Return:
614b1b8ab34Slling  *	0 - success
615b1b8ab34Slling  *	errnum - failure
616b1b8ab34Slling  */
617e7cbe64fSgw static int
618b1b8ab34Slling fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
619b1b8ab34Slling     char *name, uint64_t *value, char *stack)
620b1b8ab34Slling {
621b1b8ab34Slling 	zap_leaf_phys_t *l;
622b1b8ab34Slling 	uint64_t hash, idx, blkid;
623b1b8ab34Slling 	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
624b1b8ab34Slling 
625b1b8ab34Slling 	/* Verify if this is a fat zap header block */
626b1b8ab34Slling 	if (zap->zap_magic != (uint64_t)ZAP_MAGIC)
627b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
628b1b8ab34Slling 
629b1b8ab34Slling 	hash = zap_hash(zap->zap_salt, name);
630b1b8ab34Slling 	if (errnum)
631b1b8ab34Slling 		return (errnum);
632b1b8ab34Slling 
633b1b8ab34Slling 	/* get block id from index */
634b1b8ab34Slling 	if (zap->zap_ptrtbl.zt_numblks != 0) {
635b1b8ab34Slling 		/* external pointer tables not supported */
636b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
637b1b8ab34Slling 	}
638b1b8ab34Slling 	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
639b1b8ab34Slling 	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
640b1b8ab34Slling 
641b1b8ab34Slling 	/* Get the leaf block */
642b1b8ab34Slling 	l = (zap_leaf_phys_t *)stack;
643b1b8ab34Slling 	stack += 1<<blksft;
644051aabe6Staylor 	if ((1<<blksft) < sizeof (zap_leaf_phys_t))
645e37b211cStaylor 		return (ERR_FSYS_CORRUPT);
646b1b8ab34Slling 	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
647b1b8ab34Slling 		return (errnum);
648b1b8ab34Slling 
649b1b8ab34Slling 	return (zap_leaf_lookup(l, blksft, hash, name, value));
650b1b8ab34Slling }
651b1b8ab34Slling 
652b1b8ab34Slling /*
653b1b8ab34Slling  * Read in the data of a zap object and find the value for a matching
654b1b8ab34Slling  * property name.
655b1b8ab34Slling  *
656b1b8ab34Slling  * Return:
657b1b8ab34Slling  *	0 - success
658b1b8ab34Slling  *	errnum - failure
659b1b8ab34Slling  */
660b1b8ab34Slling static int
661b1b8ab34Slling zap_lookup(dnode_phys_t *zap_dnode, char *name, uint64_t *val, char *stack)
662b1b8ab34Slling {
663b1b8ab34Slling 	uint64_t block_type;
664b1b8ab34Slling 	int size;
665b1b8ab34Slling 	void *zapbuf;
666b1b8ab34Slling 
667b1b8ab34Slling 	/* Read in the first block of the zap object data. */
668b1b8ab34Slling 	zapbuf = stack;
669b1b8ab34Slling 	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
670b1b8ab34Slling 	stack += size;
671b1b8ab34Slling 	if (errnum = dmu_read(zap_dnode, 0, zapbuf, stack))
672b1b8ab34Slling 		return (errnum);
673b1b8ab34Slling 
674b1b8ab34Slling 	block_type = *((uint64_t *)zapbuf);
675b1b8ab34Slling 
676b1b8ab34Slling 	if (block_type == ZBT_MICRO) {
677b1b8ab34Slling 		return (mzap_lookup(zapbuf, size, name, val));
678b1b8ab34Slling 	} else if (block_type == ZBT_HEADER) {
679b1b8ab34Slling 		/* this is a fat zap */
680b1b8ab34Slling 		return (fzap_lookup(zap_dnode, zapbuf, name,
681b1b8ab34Slling 		    val, stack));
682b1b8ab34Slling 	}
683b1b8ab34Slling 
684b1b8ab34Slling 	return (ERR_FSYS_CORRUPT);
685b1b8ab34Slling }
686b1b8ab34Slling 
687b1b8ab34Slling /*
688b1b8ab34Slling  * Get the dnode of an object number from the metadnode of an object set.
689b1b8ab34Slling  *
690b1b8ab34Slling  * Input
691b1b8ab34Slling  *	mdn - metadnode to get the object dnode
692b1b8ab34Slling  *	objnum - object number for the object dnode
693b1b8ab34Slling  *	buf - data buffer that holds the returning dnode
694b1b8ab34Slling  *	stack - scratch area
695b1b8ab34Slling  *
696b1b8ab34Slling  * Return:
697b1b8ab34Slling  *	0 - success
698b1b8ab34Slling  *	errnum - failure
699b1b8ab34Slling  */
700b1b8ab34Slling static int
701b1b8ab34Slling dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
702b1b8ab34Slling 	char *stack)
703b1b8ab34Slling {
704b1b8ab34Slling 	uint64_t blkid, blksz; /* the block id this object dnode is in */
705b1b8ab34Slling 	int epbs; /* shift of number of dnodes in a block */
706b1b8ab34Slling 	int idx; /* index within a block */
707b1b8ab34Slling 	dnode_phys_t *dnbuf;
708b1b8ab34Slling 
709b1b8ab34Slling 	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
710b1b8ab34Slling 	epbs = zfs_log2(blksz) - DNODE_SHIFT;
711b1b8ab34Slling 	blkid = objnum >> epbs;
712b1b8ab34Slling 	idx = objnum & ((1<<epbs)-1);
713b1b8ab34Slling 
714b1b8ab34Slling 	if (dnode_buf != NULL && dnode_mdn == mdn &&
715b1b8ab34Slling 	    objnum >= dnode_start && objnum < dnode_end) {
716b1b8ab34Slling 		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
717b1b8ab34Slling 		VERIFY_DN_TYPE(buf, type);
718b1b8ab34Slling 		return (0);
719b1b8ab34Slling 	}
720b1b8ab34Slling 
721b1b8ab34Slling 	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
722b1b8ab34Slling 		dnbuf = dnode_buf;
723b1b8ab34Slling 		dnode_mdn = mdn;
724b1b8ab34Slling 		dnode_start = blkid << epbs;
725b1b8ab34Slling 		dnode_end = (blkid + 1) << epbs;
726b1b8ab34Slling 	} else {
727b1b8ab34Slling 		dnbuf = (dnode_phys_t *)stack;
728b1b8ab34Slling 		stack += blksz;
729b1b8ab34Slling 	}
730b1b8ab34Slling 
731b1b8ab34Slling 	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
732b1b8ab34Slling 		return (errnum);
733b1b8ab34Slling 
734b1b8ab34Slling 	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
735b1b8ab34Slling 	VERIFY_DN_TYPE(buf, type);
736b1b8ab34Slling 
737b1b8ab34Slling 	return (0);
738b1b8ab34Slling }
739b1b8ab34Slling 
740b1b8ab34Slling /*
741eb2bd662Svikram  * Check if this is a special file that resides at the top
742eb2bd662Svikram  * dataset of the pool. Currently this is the GRUB menu,
743eb2bd662Svikram  * boot signature and boot signature backup.
744b1b8ab34Slling  * str starts with '/'.
745b1b8ab34Slling  */
746b1b8ab34Slling static int
747eb2bd662Svikram is_top_dataset_file(char *str)
748b1b8ab34Slling {
749b1b8ab34Slling 	char *tptr;
750b1b8ab34Slling 
751b1b8ab34Slling 	if ((tptr = grub_strstr(str, "menu.lst")) &&
752b1b8ab34Slling 	    (tptr[8] == '\0' || tptr[8] == ' ') &&
753b1b8ab34Slling 	    *(tptr-1) == '/')
754b1b8ab34Slling 		return (1);
755b1b8ab34Slling 
756eb2bd662Svikram 	if (grub_strncmp(str, BOOTSIGN_DIR"/",
7571183b401Svikram 	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
758eb2bd662Svikram 		return (1);
759eb2bd662Svikram 
760eb2bd662Svikram 	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
761eb2bd662Svikram 		return (1);
762eb2bd662Svikram 
763b1b8ab34Slling 	return (0);
764b1b8ab34Slling }
765b1b8ab34Slling 
766b1b8ab34Slling /*
767b1b8ab34Slling  * Get the file dnode for a given file name where mdn is the meta dnode
768b1b8ab34Slling  * for this ZFS object set. When found, place the file dnode in dn.
769b1b8ab34Slling  * The 'path' argument will be mangled.
770b1b8ab34Slling  *
771b1b8ab34Slling  * Return:
772b1b8ab34Slling  *	0 - success
773b1b8ab34Slling  *	errnum - failure
774b1b8ab34Slling  */
775b1b8ab34Slling static int
776b1b8ab34Slling dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
777b1b8ab34Slling     char *stack)
778b1b8ab34Slling {
779e7437265Sahrens 	uint64_t objnum, version;
780b1b8ab34Slling 	char *cname, ch;
781b1b8ab34Slling 
782b1b8ab34Slling 	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
783b1b8ab34Slling 	    dn, stack))
784b1b8ab34Slling 		return (errnum);
785b1b8ab34Slling 
786e7437265Sahrens 	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
787e7437265Sahrens 		return (errnum);
788e7437265Sahrens 	if (version > ZPL_VERSION)
789e7437265Sahrens 		return (-1);
790e7437265Sahrens 
791b1b8ab34Slling 	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
792b1b8ab34Slling 		return (errnum);
793b1b8ab34Slling 
794b1b8ab34Slling 	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
795b1b8ab34Slling 	    dn, stack))
796b1b8ab34Slling 		return (errnum);
797b1b8ab34Slling 
798b1b8ab34Slling 	/* skip leading slashes */
799b1b8ab34Slling 	while (*path == '/')
800b1b8ab34Slling 		path++;
801b1b8ab34Slling 
802b1b8ab34Slling 	while (*path && !isspace(*path)) {
803b1b8ab34Slling 
804b1b8ab34Slling 		/* get the next component name */
805b1b8ab34Slling 		cname = path;
806b1b8ab34Slling 		while (*path && !isspace(*path) && *path != '/')
807b1b8ab34Slling 			path++;
808b1b8ab34Slling 		ch = *path;
809b1b8ab34Slling 		*path = 0;   /* ensure null termination */
810b1b8ab34Slling 
811b1b8ab34Slling 		if (errnum = zap_lookup(dn, cname, &objnum, stack))
812b1b8ab34Slling 			return (errnum);
813b1b8ab34Slling 
814e7437265Sahrens 		objnum = ZFS_DIRENT_OBJ(objnum);
815b1b8ab34Slling 		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
816b1b8ab34Slling 			return (errnum);
817b1b8ab34Slling 
818b1b8ab34Slling 		*path = ch;
819b1b8ab34Slling 		while (*path == '/')
820b1b8ab34Slling 			path++;
821b1b8ab34Slling 	}
822b1b8ab34Slling 
823b1b8ab34Slling 	/* We found the dnode for this file. Verify if it is a plain file. */
824b1b8ab34Slling 	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
825b1b8ab34Slling 
826b1b8ab34Slling 	return (0);
827b1b8ab34Slling }
828b1b8ab34Slling 
829b1b8ab34Slling /*
830b1b8ab34Slling  * Get the default 'bootfs' property value from the rootpool.
831b1b8ab34Slling  *
832b1b8ab34Slling  * Return:
833b1b8ab34Slling  *	0 - success
834b1b8ab34Slling  *	errnum -failure
835b1b8ab34Slling  */
836b1b8ab34Slling static int
837b1b8ab34Slling get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
838b1b8ab34Slling {
839b1b8ab34Slling 	uint64_t objnum = 0;
840b1b8ab34Slling 	dnode_phys_t *dn = (dnode_phys_t *)stack;
841b1b8ab34Slling 	stack += DNODE_SIZE;
842b1b8ab34Slling 
843ae8180dbSlling 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
844b1b8ab34Slling 	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
845ae8180dbSlling 		return (errnum);
846b1b8ab34Slling 
847b1b8ab34Slling 	/*
848b1b8ab34Slling 	 * find the object number for 'pool_props', and get the dnode
849b1b8ab34Slling 	 * of the 'pool_props'.
850b1b8ab34Slling 	 */
851b1b8ab34Slling 	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
852b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
853b1b8ab34Slling 
854ae8180dbSlling 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
855ae8180dbSlling 		return (errnum);
856b1b8ab34Slling 
857b1b8ab34Slling 	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
858b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
859b1b8ab34Slling 
860b1b8ab34Slling 	if (!objnum)
861b1b8ab34Slling 		return (ERR_FILESYSTEM_NOT_FOUND);
862b1b8ab34Slling 
863b1b8ab34Slling 	*obj = objnum;
864b1b8ab34Slling 	return (0);
865b1b8ab34Slling }
866b1b8ab34Slling 
867b1b8ab34Slling /*
868b1b8ab34Slling  * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
869b1b8ab34Slling  * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
870b1b8ab34Slling  * of pool/rootfs.
871b1b8ab34Slling  *
872b1b8ab34Slling  * If no fsname and no obj are given, return the DSL_DIR metadnode.
873b1b8ab34Slling  * If fsname is given, return its metadnode and its matching object number.
874b1b8ab34Slling  * If only obj is given, return the metadnode for this object number.
875b1b8ab34Slling  *
876b1b8ab34Slling  * Return:
877b1b8ab34Slling  *	0 - success
878b1b8ab34Slling  *	errnum - failure
879b1b8ab34Slling  */
880b1b8ab34Slling static int
881b1b8ab34Slling get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
882b1b8ab34Slling     dnode_phys_t *mdn, char *stack)
883b1b8ab34Slling {
884b1b8ab34Slling 	uint64_t objnum, headobj;
885b1b8ab34Slling 	char *cname, ch;
886b1b8ab34Slling 	blkptr_t *bp;
887b1b8ab34Slling 	objset_phys_t *osp;
888fe3e2633SEric Taylor 	int issnapshot = 0;
889fe3e2633SEric Taylor 	char *snapname;
890b1b8ab34Slling 
891b1b8ab34Slling 	if (fsname == NULL && obj) {
892b1b8ab34Slling 		headobj = *obj;
893b1b8ab34Slling 		goto skip;
894b1b8ab34Slling 	}
895b1b8ab34Slling 
896b1b8ab34Slling 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
897b1b8ab34Slling 	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
898b1b8ab34Slling 		return (errnum);
899b1b8ab34Slling 
900b1b8ab34Slling 	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
901b1b8ab34Slling 	    stack))
902b1b8ab34Slling 		return (errnum);
903b1b8ab34Slling 
904b1b8ab34Slling 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
905b1b8ab34Slling 		return (errnum);
906b1b8ab34Slling 
907b1b8ab34Slling 	if (fsname == NULL) {
908b1b8ab34Slling 		headobj =
909b1b8ab34Slling 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
910b1b8ab34Slling 		goto skip;
911b1b8ab34Slling 	}
912b1b8ab34Slling 
913b1b8ab34Slling 	/* take out the pool name */
914b1b8ab34Slling 	while (*fsname && !isspace(*fsname) && *fsname != '/')
915b1b8ab34Slling 		fsname++;
916b1b8ab34Slling 
917b1b8ab34Slling 	while (*fsname && !isspace(*fsname)) {
918b1b8ab34Slling 		uint64_t childobj;
919b1b8ab34Slling 
920b1b8ab34Slling 		while (*fsname == '/')
921b1b8ab34Slling 			fsname++;
922b1b8ab34Slling 
923b1b8ab34Slling 		cname = fsname;
924b1b8ab34Slling 		while (*fsname && !isspace(*fsname) && *fsname != '/')
925b1b8ab34Slling 			fsname++;
926b1b8ab34Slling 		ch = *fsname;
927b1b8ab34Slling 		*fsname = 0;
928b1b8ab34Slling 
929fe3e2633SEric Taylor 		snapname = cname;
930fe3e2633SEric Taylor 		while (*snapname && !isspace(*snapname) && *snapname != '@')
931fe3e2633SEric Taylor 			snapname++;
932fe3e2633SEric Taylor 		if (*snapname == '@') {
933fe3e2633SEric Taylor 			issnapshot = 1;
934fe3e2633SEric Taylor 			*snapname = 0;
935fe3e2633SEric Taylor 		}
936b1b8ab34Slling 		childobj =
937b1b8ab34Slling 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
938b1b8ab34Slling 		if (errnum = dnode_get(mosmdn, childobj,
939b1b8ab34Slling 		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
940b1b8ab34Slling 			return (errnum);
941b1b8ab34Slling 
942ae8180dbSlling 		if (zap_lookup(mdn, cname, &objnum, stack))
943ae8180dbSlling 			return (ERR_FILESYSTEM_NOT_FOUND);
944b1b8ab34Slling 
945b1b8ab34Slling 		if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
946b1b8ab34Slling 		    mdn, stack))
947b1b8ab34Slling 			return (errnum);
948b1b8ab34Slling 
949b1b8ab34Slling 		*fsname = ch;
950fe3e2633SEric Taylor 		if (issnapshot)
951fe3e2633SEric Taylor 			*snapname = '@';
952b1b8ab34Slling 	}
953b1b8ab34Slling 	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
954b1b8ab34Slling 	if (obj)
955b1b8ab34Slling 		*obj = headobj;
956b1b8ab34Slling 
957b1b8ab34Slling skip:
958b1b8ab34Slling 	if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
959b1b8ab34Slling 		return (errnum);
960fe3e2633SEric Taylor 	if (issnapshot) {
961fe3e2633SEric Taylor 		uint64_t snapobj;
962fe3e2633SEric Taylor 
963fe3e2633SEric Taylor 		snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
964fe3e2633SEric Taylor 		    ds_snapnames_zapobj;
965b1b8ab34Slling 
966fe3e2633SEric Taylor 		if (errnum = dnode_get(mosmdn, snapobj,
967fe3e2633SEric Taylor 		    DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
968fe3e2633SEric Taylor 			return (errnum);
969fe3e2633SEric Taylor 		if (zap_lookup(mdn, snapname + 1, &headobj, stack))
970fe3e2633SEric Taylor 			return (ERR_FILESYSTEM_NOT_FOUND);
971fe3e2633SEric Taylor 		if (errnum = dnode_get(mosmdn, headobj,
972fe3e2633SEric Taylor 		    DMU_OT_DSL_DATASET, mdn, stack))
973fe3e2633SEric Taylor 			return (errnum);
974fe3e2633SEric Taylor 		if (obj)
975fe3e2633SEric Taylor 			*obj = headobj;
976fe3e2633SEric Taylor 	}
977b1b8ab34Slling 
978b1b8ab34Slling 	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
979b1b8ab34Slling 	osp = (objset_phys_t *)stack;
980b1b8ab34Slling 	stack += sizeof (objset_phys_t);
981b1b8ab34Slling 	if (errnum = zio_read(bp, osp, stack))
982b1b8ab34Slling 		return (errnum);
983b1b8ab34Slling 
984b1b8ab34Slling 	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
985b1b8ab34Slling 
986b1b8ab34Slling 	return (0);
987b1b8ab34Slling }
988b1b8ab34Slling 
989b1b8ab34Slling /*
990e7cbe64fSgw  * For a given XDR packed nvlist, verify the first 4 bytes and move on.
991b1b8ab34Slling  *
992e7cbe64fSgw  * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
993b1b8ab34Slling  *
994b1b8ab34Slling  *      encoding method/host endian     (4 bytes)
995b1b8ab34Slling  *      nvl_version                     (4 bytes)
996b1b8ab34Slling  *      nvl_nvflag                      (4 bytes)
997b1b8ab34Slling  *	encoded nvpairs:
998b1b8ab34Slling  *		encoded size of the nvpair      (4 bytes)
999b1b8ab34Slling  *		decoded size of the nvpair      (4 bytes)
1000b1b8ab34Slling  *		name string size                (4 bytes)
1001b1b8ab34Slling  *		name string data                (sizeof(NV_ALIGN4(string))
1002b1b8ab34Slling  *		data type                       (4 bytes)
1003b1b8ab34Slling  *		# of elements in the nvpair     (4 bytes)
1004b1b8ab34Slling  *		data
1005b1b8ab34Slling  *      2 zero's for the last nvpair
1006b1b8ab34Slling  *		(end of the entire list)	(8 bytes)
1007b1b8ab34Slling  *
1008b1b8ab34Slling  * Return:
1009b1b8ab34Slling  *	0 - success
1010b1b8ab34Slling  *	1 - failure
1011b1b8ab34Slling  */
1012e7cbe64fSgw static int
1013e7cbe64fSgw nvlist_unpack(char *nvlist, char **out)
1014b1b8ab34Slling {
1015b1b8ab34Slling 	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
1016b1b8ab34Slling 	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1017b1b8ab34Slling 		return (1);
1018b1b8ab34Slling 
1019e7cbe64fSgw 	nvlist += 4;
1020e7cbe64fSgw 	*out = nvlist;
1021e7cbe64fSgw 	return (0);
1022e7cbe64fSgw }
1023e7cbe64fSgw 
1024e7cbe64fSgw static char *
1025e7cbe64fSgw nvlist_array(char *nvlist, int index)
1026e7cbe64fSgw {
1027e7cbe64fSgw 	int i, encode_size;
1028e7cbe64fSgw 
1029e7cbe64fSgw 	for (i = 0; i < index; i++) {
1030e7cbe64fSgw 		/* skip the header, nvl_version, and nvl_nvflag */
1031e7cbe64fSgw 		nvlist = nvlist + 4 * 2;
1032e7cbe64fSgw 
1033e7cbe64fSgw 		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1034e7cbe64fSgw 			nvlist += encode_size; /* goto the next nvpair */
1035e7cbe64fSgw 
1036e7cbe64fSgw 		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1037e7cbe64fSgw 	}
1038e7cbe64fSgw 
1039e7cbe64fSgw 	return (nvlist);
1040e7cbe64fSgw }
1041e7cbe64fSgw 
1042e7cbe64fSgw static int
1043e7cbe64fSgw nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1044e7cbe64fSgw     int *nelmp)
1045e7cbe64fSgw {
1046e7cbe64fSgw 	int name_len, type, slen, encode_size;
1047e7cbe64fSgw 	char *nvpair, *nvp_name, *strval = val;
1048e7cbe64fSgw 	uint64_t *intval = val;
1049e7cbe64fSgw 
1050b1b8ab34Slling 	/* skip the header, nvl_version, and nvl_nvflag */
1051e7cbe64fSgw 	nvlist = nvlist + 4 * 2;
1052b1b8ab34Slling 
1053b1b8ab34Slling 	/*
1054b1b8ab34Slling 	 * Loop thru the nvpair list
1055b1b8ab34Slling 	 * The XDR representation of an integer is in big-endian byte order.
1056b1b8ab34Slling 	 */
1057b1b8ab34Slling 	while (encode_size = BSWAP_32(*(uint32_t *)nvlist))  {
1058b1b8ab34Slling 
1059b1b8ab34Slling 		nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
1060b1b8ab34Slling 
1061b1b8ab34Slling 		name_len = BSWAP_32(*(uint32_t *)nvpair);
1062b1b8ab34Slling 		nvpair += 4;
1063b1b8ab34Slling 
1064b1b8ab34Slling 		nvp_name = nvpair;
1065b1b8ab34Slling 		nvpair = nvpair + ((name_len + 3) & ~3); /* align */
1066b1b8ab34Slling 
1067b1b8ab34Slling 		type = BSWAP_32(*(uint32_t *)nvpair);
1068b1b8ab34Slling 		nvpair += 4;
1069b1b8ab34Slling 
1070b1b8ab34Slling 		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1071b1b8ab34Slling 		    type == valtype) {
1072e7cbe64fSgw 			int nelm;
1073b1b8ab34Slling 
1074e7cbe64fSgw 			if ((nelm = BSWAP_32(*(uint32_t *)nvpair)) < 1)
1075b1b8ab34Slling 				return (1);
1076b1b8ab34Slling 			nvpair += 4;
1077b1b8ab34Slling 
1078b1b8ab34Slling 			switch (valtype) {
1079b1b8ab34Slling 			case DATA_TYPE_STRING:
1080b1b8ab34Slling 				slen = BSWAP_32(*(uint32_t *)nvpair);
1081b1b8ab34Slling 				nvpair += 4;
1082b1b8ab34Slling 				grub_memmove(strval, nvpair, slen);
1083b1b8ab34Slling 				strval[slen] = '\0';
1084b1b8ab34Slling 				return (0);
1085b1b8ab34Slling 
1086b1b8ab34Slling 			case DATA_TYPE_UINT64:
1087b1b8ab34Slling 				*intval = BSWAP_64(*(uint64_t *)nvpair);
1088b1b8ab34Slling 				return (0);
1089e7cbe64fSgw 
1090e7cbe64fSgw 			case DATA_TYPE_NVLIST:
1091e7cbe64fSgw 				*(void **)val = (void *)nvpair;
1092e7cbe64fSgw 				return (0);
1093e7cbe64fSgw 
1094e7cbe64fSgw 			case DATA_TYPE_NVLIST_ARRAY:
1095e7cbe64fSgw 				*(void **)val = (void *)nvpair;
1096e7cbe64fSgw 				if (nelmp)
1097e7cbe64fSgw 					*nelmp = nelm;
1098e7cbe64fSgw 				return (0);
1099b1b8ab34Slling 			}
1100b1b8ab34Slling 		}
1101b1b8ab34Slling 
1102b1b8ab34Slling 		nvlist += encode_size; /* goto the next nvpair */
1103b1b8ab34Slling 	}
1104b1b8ab34Slling 
1105b1b8ab34Slling 	return (1);
1106b1b8ab34Slling }
1107b1b8ab34Slling 
1108b1b8ab34Slling /*
1109e7cbe64fSgw  * Check if this vdev is online and is in a good state.
1110e7cbe64fSgw  */
1111e7cbe64fSgw static int
1112e7cbe64fSgw vdev_validate(char *nv)
1113e7cbe64fSgw {
1114e7cbe64fSgw 	uint64_t ival;
1115e7cbe64fSgw 
1116e7cbe64fSgw 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1117e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0 ||
1118e7cbe64fSgw 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1119e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0 ||
1120e7cbe64fSgw 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1121e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL) == 0)
1122e7cbe64fSgw 		return (ERR_DEV_VALUES);
1123e7cbe64fSgw 
1124e7cbe64fSgw 	return (0);
1125e7cbe64fSgw }
1126e7cbe64fSgw 
1127e7cbe64fSgw /*
112821ecdf64SLin Ling  * Get a valid vdev pathname/devid from the boot device.
1129ffb5616eSLin Ling  * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1130e7cbe64fSgw  */
113121ecdf64SLin Ling static int
113221ecdf64SLin Ling vdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
113321ecdf64SLin Ling     int is_spare)
1134e7cbe64fSgw {
1135e7cbe64fSgw 	char type[16];
1136e7cbe64fSgw 
1137e7cbe64fSgw 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1138e7cbe64fSgw 	    NULL))
1139e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1140e7cbe64fSgw 
1141e7cbe64fSgw 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1142ffb5616eSLin Ling 		uint64_t guid;
1143ffb5616eSLin Ling 
1144ffb5616eSLin Ling 		if (vdev_validate(nv) != 0)
1145ffb5616eSLin Ling 			return (ERR_NO_BOOTPATH);
1146ffb5616eSLin Ling 
1147ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1148ffb5616eSLin Ling 		    &guid, DATA_TYPE_UINT64, NULL) != 0)
1149ffb5616eSLin Ling 			return (ERR_NO_BOOTPATH);
1150ffb5616eSLin Ling 
1151ffb5616eSLin Ling 		if (guid != inguid)
1152e7cbe64fSgw 			return (ERR_NO_BOOTPATH);
1153e7cbe64fSgw 
115421ecdf64SLin Ling 		/* for a spare vdev, pick the disk labeled with "is_spare" */
115521ecdf64SLin Ling 		if (is_spare) {
115621ecdf64SLin Ling 			uint64_t spare = 0;
115721ecdf64SLin Ling 			(void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
115821ecdf64SLin Ling 			    &spare, DATA_TYPE_UINT64, NULL);
115921ecdf64SLin Ling 			if (!spare)
116021ecdf64SLin Ling 				return (ERR_NO_BOOTPATH);
116121ecdf64SLin Ling 		}
116221ecdf64SLin Ling 
1163ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
1164ffb5616eSLin Ling 		    bootpath, DATA_TYPE_STRING, NULL) != 0)
1165ffb5616eSLin Ling 			bootpath[0] = '\0';
1166ffb5616eSLin Ling 
1167ffb5616eSLin Ling 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
1168ffb5616eSLin Ling 		    devid, DATA_TYPE_STRING, NULL) != 0)
1169ffb5616eSLin Ling 			devid[0] = '\0';
1170ffb5616eSLin Ling 
1171ffb5616eSLin Ling 		if (strlen(bootpath) >= MAXPATHLEN ||
1172ffb5616eSLin Ling 		    strlen(devid) >= MAXPATHLEN)
1173ffb5616eSLin Ling 			return (ERR_WONT_FIT);
1174ffb5616eSLin Ling 
1175ffb5616eSLin Ling 		return (0);
1176ffb5616eSLin Ling 
117721ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
117821ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
117921ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1180e7cbe64fSgw 		int nelm, i;
1181e7cbe64fSgw 		char *child;
1182e7cbe64fSgw 
1183e7cbe64fSgw 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
1184e7cbe64fSgw 		    DATA_TYPE_NVLIST_ARRAY, &nelm))
1185e7cbe64fSgw 			return (ERR_FSYS_CORRUPT);
1186e7cbe64fSgw 
1187e7cbe64fSgw 		for (i = 0; i < nelm; i++) {
1188e7cbe64fSgw 			char *child_i;
1189e7cbe64fSgw 
1190e7cbe64fSgw 			child_i = nvlist_array(child, i);
1191ffb5616eSLin Ling 			if (vdev_get_bootpath(child_i, inguid, devid,
119221ecdf64SLin Ling 			    bootpath, is_spare) == 0)
1193ffb5616eSLin Ling 				return (0);
1194e7cbe64fSgw 		}
1195e7cbe64fSgw 	}
1196e7cbe64fSgw 
1197ffb5616eSLin Ling 	return (ERR_NO_BOOTPATH);
1198e7cbe64fSgw }
1199e7cbe64fSgw 
1200e7cbe64fSgw /*
1201e7cbe64fSgw  * Check the disk label information and retrieve needed vdev name-value pairs.
1202b1b8ab34Slling  *
1203b1b8ab34Slling  * Return:
1204b1b8ab34Slling  *	0 - success
1205e7cbe64fSgw  *	ERR_* - failure
1206b1b8ab34Slling  */
1207051aabe6Staylor int
1208*e23347b1SEric Taylor check_pool_label(uint64_t sector, char *stack, char *outdevid,
1209*e23347b1SEric Taylor     char *outpath, uint64_t *outguid)
1210b1b8ab34Slling {
1211b1b8ab34Slling 	vdev_phys_t *vdev;
1212*e23347b1SEric Taylor 	uint64_t pool_state, txg = 0;
1213e7cbe64fSgw 	char *nvlist, *nv;
1214051aabe6Staylor 	uint64_t diskguid;
1215fe3e2633SEric Taylor 	uint64_t version;
1216b1b8ab34Slling 
1217*e23347b1SEric Taylor 	sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
1218b1b8ab34Slling 
1219b1b8ab34Slling 	/* Read in the vdev name-value pair list (112K). */
1220b1b8ab34Slling 	if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
1221b1b8ab34Slling 		return (ERR_READ);
1222b1b8ab34Slling 
1223b1b8ab34Slling 	vdev = (vdev_phys_t *)stack;
1224e4c3b53dStaylor 	stack += sizeof (vdev_phys_t);
1225b1b8ab34Slling 
1226e7cbe64fSgw 	if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
1227b1b8ab34Slling 		return (ERR_FSYS_CORRUPT);
1228e7cbe64fSgw 
1229e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
1230e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL))
1231e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1232e7cbe64fSgw 
1233e7cbe64fSgw 	if (pool_state == POOL_STATE_DESTROYED)
1234e7cbe64fSgw 		return (ERR_FILESYSTEM_NOT_FOUND);
1235e7cbe64fSgw 
1236e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
1237e7cbe64fSgw 	    current_rootpool, DATA_TYPE_STRING, NULL))
1238e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1239e7cbe64fSgw 
1240e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
1241e7cbe64fSgw 	    DATA_TYPE_UINT64, NULL))
1242e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1243e7cbe64fSgw 
1244e7cbe64fSgw 	/* not an active device */
1245e7cbe64fSgw 	if (txg == 0)
1246e7cbe64fSgw 		return (ERR_NO_BOOTPATH);
1247e7cbe64fSgw 
1248fe3e2633SEric Taylor 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, &version,
1249fe3e2633SEric Taylor 	    DATA_TYPE_UINT64, NULL))
1250fe3e2633SEric Taylor 		return (ERR_FSYS_CORRUPT);
1251fe3e2633SEric Taylor 	if (version > SPA_VERSION)
1252fe3e2633SEric Taylor 		return (ERR_NEWER_VERSION);
1253e7cbe64fSgw 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1254e7cbe64fSgw 	    DATA_TYPE_NVLIST, NULL))
1255e7cbe64fSgw 		return (ERR_FSYS_CORRUPT);
1256051aabe6Staylor 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1257051aabe6Staylor 	    DATA_TYPE_UINT64, NULL))
1258051aabe6Staylor 		return (ERR_FSYS_CORRUPT);
125921ecdf64SLin Ling 	if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
1260e7cbe64fSgw 		return (ERR_NO_BOOTPATH);
1261*e23347b1SEric Taylor 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
1262*e23347b1SEric Taylor 	    DATA_TYPE_UINT64, NULL))
1263*e23347b1SEric Taylor 		return (ERR_FSYS_CORRUPT);
1264e7cbe64fSgw 	return (0);
1265b1b8ab34Slling }
1266b1b8ab34Slling 
1267b1b8ab34Slling /*
1268b1b8ab34Slling  * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1269b1b8ab34Slling  * to the memory address MOS.
1270b1b8ab34Slling  *
1271b1b8ab34Slling  * Return:
1272b1b8ab34Slling  *	1 - success
1273b1b8ab34Slling  *	0 - failure
1274b1b8ab34Slling  */
1275b1b8ab34Slling int
1276b1b8ab34Slling zfs_mount(void)
1277b1b8ab34Slling {
1278b1b8ab34Slling 	char *stack;
1279b1b8ab34Slling 	int label = 0;
1280*e23347b1SEric Taylor 	uberblock_phys_t *ub_array, *ubbest;
1281b1b8ab34Slling 	objset_phys_t *osp;
1282051aabe6Staylor 	char tmp_bootpath[MAXNAMELEN];
1283051aabe6Staylor 	char tmp_devid[MAXNAMELEN];
1284*e23347b1SEric Taylor 	uint64_t tmp_guid;
1285*e23347b1SEric Taylor 	uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
1286051aabe6Staylor 
1287051aabe6Staylor 	/* if it's our first time here, zero the best uberblock out */
1288*e23347b1SEric Taylor 	if (best_drive == 0 && best_part == 0 && find_best_root) {
1289e37b211cStaylor 		grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
1290*e23347b1SEric Taylor 		pool_guid = 0;
1291*e23347b1SEric Taylor 	}
1292b1b8ab34Slling 
1293b1b8ab34Slling 	stackbase = ZFS_SCRATCH;
1294b1b8ab34Slling 	stack = stackbase;
1295b1b8ab34Slling 	ub_array = (uberblock_phys_t *)stack;
1296b1b8ab34Slling 	stack += VDEV_UBERBLOCK_RING;
1297b1b8ab34Slling 
1298b1b8ab34Slling 	osp = (objset_phys_t *)stack;
1299b1b8ab34Slling 	stack += sizeof (objset_phys_t);
1300*e23347b1SEric Taylor 	adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
1301b1b8ab34Slling 
1302*e23347b1SEric Taylor 	for (label = 0; label < VDEV_LABELS; label++) {
1303*e23347b1SEric Taylor 		uint64_t sector = vdev_label_start(adjpl,
1304*e23347b1SEric Taylor 		    label) >> SPA_MINBLOCKSHIFT;
1305b1b8ab34Slling 
1306b1b8ab34Slling 		/* Read in the uberblock ring (128K). */
1307*e23347b1SEric Taylor 		if (devread(sector  +
1308*e23347b1SEric Taylor 		    ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >>
1309*e23347b1SEric Taylor 		    SPA_MINBLOCKSHIFT), 0, VDEV_UBERBLOCK_RING,
1310b1b8ab34Slling 		    (char *)ub_array) == 0)
1311b1b8ab34Slling 			continue;
1312b1b8ab34Slling 
1313*e23347b1SEric Taylor 		if ((ubbest = find_bestub(ub_array, sector)) != NULL &&
1314b1b8ab34Slling 		    zio_read(&ubbest->ubp_uberblock.ub_rootbp, osp, stack)
1315b1b8ab34Slling 		    == 0) {
1316b1b8ab34Slling 
1317b1b8ab34Slling 			VERIFY_OS_TYPE(osp, DMU_OST_META);
1318b1b8ab34Slling 
1319*e23347b1SEric Taylor 			if (check_pool_label(sector, stack, tmp_devid,
1320*e23347b1SEric Taylor 			    tmp_bootpath, &tmp_guid))
1321*e23347b1SEric Taylor 				continue;
1322*e23347b1SEric Taylor 			if (pool_guid == 0)
1323*e23347b1SEric Taylor 				pool_guid = tmp_guid;
1324b1b8ab34Slling 
1325*e23347b1SEric Taylor 			if (find_best_root && ((pool_guid != tmp_guid) ||
1326051aabe6Staylor 			    vdev_uberblock_compare(&ubbest->ubp_uberblock,
1327*e23347b1SEric Taylor 			    &(current_uberblock)) <= 0))
1328051aabe6Staylor 				continue;
1329ffb5616eSLin Ling 
1330051aabe6Staylor 			/* Got the MOS. Save it at the memory addr MOS. */
1331051aabe6Staylor 			grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
1332051aabe6Staylor 			grub_memmove(&current_uberblock,
1333ffb5616eSLin Ling 			    &ubbest->ubp_uberblock, sizeof (uberblock_t));
1334051aabe6Staylor 			grub_memmove(current_bootpath, tmp_bootpath,
1335051aabe6Staylor 			    MAXNAMELEN);
1336051aabe6Staylor 			grub_memmove(current_devid, tmp_devid,
1337051aabe6Staylor 			    grub_strlen(tmp_devid));
1338b1b8ab34Slling 			is_zfs_mount = 1;
1339b1b8ab34Slling 			return (1);
1340b1b8ab34Slling 		}
1341b1b8ab34Slling 	}
1342b1b8ab34Slling 
1343b1b8ab34Slling 	return (0);
1344b1b8ab34Slling }
1345b1b8ab34Slling 
1346b1b8ab34Slling /*
1347b1b8ab34Slling  * zfs_open() locates a file in the rootpool by following the
1348b1b8ab34Slling  * MOS and places the dnode of the file in the memory address DNODE.
1349b1b8ab34Slling  *
1350b1b8ab34Slling  * Return:
1351b1b8ab34Slling  *	1 - success
1352b1b8ab34Slling  *	0 - failure
1353b1b8ab34Slling  */
1354b1b8ab34Slling int
1355b1b8ab34Slling zfs_open(char *filename)
1356b1b8ab34Slling {
1357b1b8ab34Slling 	char *stack;
1358b1b8ab34Slling 	dnode_phys_t *mdn;
1359b1b8ab34Slling 
1360b1b8ab34Slling 	file_buf = NULL;
1361b1b8ab34Slling 	stackbase = ZFS_SCRATCH;
1362b1b8ab34Slling 	stack = stackbase;
1363b1b8ab34Slling 
1364b1b8ab34Slling 	mdn = (dnode_phys_t *)stack;
1365b1b8ab34Slling 	stack += sizeof (dnode_phys_t);
1366b1b8ab34Slling 
1367b1b8ab34Slling 	dnode_mdn = NULL;
1368b1b8ab34Slling 	dnode_buf = (dnode_phys_t *)stack;
1369b1b8ab34Slling 	stack += 1<<DNODE_BLOCK_SHIFT;
1370b1b8ab34Slling 
1371b1b8ab34Slling 	/*
1372b1b8ab34Slling 	 * menu.lst is placed at the root pool filesystem level,
1373b1b8ab34Slling 	 * do not goto 'current_bootfs'.
1374b1b8ab34Slling 	 */
1375eb2bd662Svikram 	if (is_top_dataset_file(filename)) {
1376b1b8ab34Slling 		if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
1377b1b8ab34Slling 			return (0);
1378b1b8ab34Slling 
1379b1b8ab34Slling 		current_bootfs_obj = 0;
1380b1b8ab34Slling 	} else {
1381b1b8ab34Slling 		if (current_bootfs[0] == '\0') {
1382b1b8ab34Slling 			/* Get the default root filesystem object number */
1383ae8180dbSlling 			if (errnum = get_default_bootfsobj(MOS,
1384ae8180dbSlling 			    &current_bootfs_obj, stack))
1385b1b8ab34Slling 				return (0);
1386b1b8ab34Slling 
1387b1b8ab34Slling 			if (errnum = get_objset_mdn(MOS, NULL,
1388b1b8ab34Slling 			    &current_bootfs_obj, mdn, stack))
1389b1b8ab34Slling 				return (0);
1390b1b8ab34Slling 		} else {
1391b35c6776Staylor 			if (errnum = get_objset_mdn(MOS, current_bootfs,
1392b35c6776Staylor 			    &current_bootfs_obj, mdn, stack)) {
1393051aabe6Staylor 				grub_memset(current_bootfs, 0, MAXNAMELEN);
1394b1b8ab34Slling 				return (0);
1395b35c6776Staylor 			}
1396b1b8ab34Slling 		}
1397b1b8ab34Slling 	}
1398b1b8ab34Slling 
1399b1b8ab34Slling 	if (dnode_get_path(mdn, filename, DNODE, stack)) {
1400b1b8ab34Slling 		errnum = ERR_FILE_NOT_FOUND;
1401b1b8ab34Slling 		return (0);
1402b1b8ab34Slling 	}
1403b1b8ab34Slling 
1404b1b8ab34Slling 	/* get the file size and set the file position to 0 */
1405b1b8ab34Slling 	filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
1406b1b8ab34Slling 	filepos = 0;
1407b1b8ab34Slling 
1408b1b8ab34Slling 	dnode_buf = NULL;
1409b1b8ab34Slling 	return (1);
1410b1b8ab34Slling }
1411b1b8ab34Slling 
1412b1b8ab34Slling /*
1413b1b8ab34Slling  * zfs_read reads in the data blocks pointed by the DNODE.
1414b1b8ab34Slling  *
1415b1b8ab34Slling  * Return:
1416b1b8ab34Slling  *	len - the length successfully read in to the buffer
1417b1b8ab34Slling  *	0   - failure
1418b1b8ab34Slling  */
1419b1b8ab34Slling int
1420b1b8ab34Slling zfs_read(char *buf, int len)
1421b1b8ab34Slling {
1422b1b8ab34Slling 	char *stack;
1423b1b8ab34Slling 	char *tmpbuf;
1424b1b8ab34Slling 	int blksz, length, movesize;
1425b1b8ab34Slling 
1426b1b8ab34Slling 	if (file_buf == NULL) {
1427b1b8ab34Slling 		file_buf = stackbase;
1428b1b8ab34Slling 		stackbase += SPA_MAXBLOCKSIZE;
1429b1b8ab34Slling 		file_start = file_end = 0;
1430b1b8ab34Slling 	}
1431b1b8ab34Slling 	stack = stackbase;
1432b1b8ab34Slling 
1433b1b8ab34Slling 	/*
1434b1b8ab34Slling 	 * If offset is in memory, move it into the buffer provided and return.
1435b1b8ab34Slling 	 */
1436b1b8ab34Slling 	if (filepos >= file_start && filepos+len <= file_end) {
1437b1b8ab34Slling 		grub_memmove(buf, file_buf + filepos - file_start, len);
1438b1b8ab34Slling 		filepos += len;
1439b1b8ab34Slling 		return (len);
1440b1b8ab34Slling 	}
1441b1b8ab34Slling 
1442b1b8ab34Slling 	blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1443b1b8ab34Slling 
1444b1b8ab34Slling 	/*
1445b1b8ab34Slling 	 * Entire Dnode is too big to fit into the space available.  We
1446b1b8ab34Slling 	 * will need to read it in chunks.  This could be optimized to
1447b1b8ab34Slling 	 * read in as large a chunk as there is space available, but for
1448b1b8ab34Slling 	 * now, this only reads in one data block at a time.
1449b1b8ab34Slling 	 */
1450b1b8ab34Slling 	length = len;
1451b1b8ab34Slling 	while (length) {
1452b1b8ab34Slling 		/*
1453b1b8ab34Slling 		 * Find requested blkid and the offset within that block.
1454b1b8ab34Slling 		 */
1455b1b8ab34Slling 		uint64_t blkid = filepos / blksz;
1456b1b8ab34Slling 
1457b1b8ab34Slling 		if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
1458b1b8ab34Slling 			return (0);
1459b1b8ab34Slling 
1460b1b8ab34Slling 		file_start = blkid * blksz;
1461b1b8ab34Slling 		file_end = file_start + blksz;
1462b1b8ab34Slling 
1463b1b8ab34Slling 		movesize = MIN(length, file_end - filepos);
1464b1b8ab34Slling 
1465b1b8ab34Slling 		grub_memmove(buf, file_buf + filepos - file_start,
1466b1b8ab34Slling 		    movesize);
1467b1b8ab34Slling 		buf += movesize;
1468b1b8ab34Slling 		length -= movesize;
1469b1b8ab34Slling 		filepos += movesize;
1470b1b8ab34Slling 	}
1471b1b8ab34Slling 
1472b1b8ab34Slling 	return (len);
1473b1b8ab34Slling }
1474b1b8ab34Slling 
1475b1b8ab34Slling /*
1476b1b8ab34Slling  * No-Op
1477b1b8ab34Slling  */
1478b1b8ab34Slling int
1479b1b8ab34Slling zfs_embed(int *start_sector, int needed_sectors)
1480b1b8ab34Slling {
1481b1b8ab34Slling 	return (1);
1482b1b8ab34Slling }
1483b1b8ab34Slling 
1484b1b8ab34Slling #endif /* FSYS_ZFS */
1485