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