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