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