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