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