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 /*
21  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 /*
26  * Copyright (c) 2013 by Delphix. All rights reserved.
27  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28  */
29 
30 /*
31  * The zfs plug-in routines for GRUB are:
32  *
33  * zfs_mount() - locates a valid uberblock of the root pool and reads
34  *		in its MOS at the memory address MOS.
35  *
36  * zfs_open() - locates a plain file object by following the MOS
37  *		and places its dnode at the memory address DNODE.
38  *
39  * zfs_read() - read in the data blocks pointed by the DNODE.
40  *
41  * ZFS_SCRATCH is used as a working area.
42  *
43  * (memory addr)   MOS      DNODE	ZFS_SCRATCH
44  *		    |         |          |
45  *	    +-------V---------V----------V---------------+
46  *   memory |       | dnode   | dnode    |  scratch      |
47  *	    |       | 512B    | 512B     |  area         |
48  *	    +--------------------------------------------+
49  */
50 
51 #ifdef	FSYS_ZFS
52 
53 #include "shared.h"
54 #include "filesys.h"
55 #include "fsys_zfs.h"
56 
57 /* cache for a file block of the currently zfs_open()-ed file */
58 static void *file_buf = NULL;
59 static uint64_t file_start = 0;
60 static uint64_t file_end = 0;
61 
62 /* cache for a dnode block */
63 static dnode_phys_t *dnode_buf = NULL;
64 static dnode_phys_t *dnode_mdn = NULL;
65 static uint64_t dnode_start = 0;
66 static uint64_t dnode_end = 0;
67 
68 static uint64_t pool_guid = 0;
69 static uberblock_t current_uberblock;
70 static char *stackbase;
71 
72 decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
73 {
74 	{"inherit", 0},			/* ZIO_COMPRESS_INHERIT */
75 	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
76 	{"off", 0},			/* ZIO_COMPRESS_OFF */
77 	{"lzjb", lzjb_decompress},	/* ZIO_COMPRESS_LZJB */
78 	{"empty", 0},			/* ZIO_COMPRESS_EMPTY */
79 	{"gzip-1", 0},			/* ZIO_COMPRESS_GZIP_1 */
80 	{"gzip-2", 0},			/* ZIO_COMPRESS_GZIP_2 */
81 	{"gzip-3", 0},			/* ZIO_COMPRESS_GZIP_3 */
82 	{"gzip-4", 0},			/* ZIO_COMPRESS_GZIP_4 */
83 	{"gzip-5", 0},			/* ZIO_COMPRESS_GZIP_5 */
84 	{"gzip-6", 0},			/* ZIO_COMPRESS_GZIP_6 */
85 	{"gzip-7", 0},			/* ZIO_COMPRESS_GZIP_7 */
86 	{"gzip-8", 0},			/* ZIO_COMPRESS_GZIP_8 */
87 	{"gzip-9", 0},			/* ZIO_COMPRESS_GZIP_9 */
88 	{"zle", 0},			/* ZIO_COMPRESS_ZLE */
89 	{"lz4", lz4_decompress}		/* ZIO_COMPRESS_LZ4 */
90 };
91 
92 static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
93 
94 /*
95  * Our own version of bcmp().
96  */
97 static int
98 zfs_bcmp(const void *s1, const void *s2, size_t n)
99 {
100 	const uchar_t *ps1 = s1;
101 	const uchar_t *ps2 = s2;
102 
103 	if (s1 != s2 && n != 0) {
104 		do {
105 			if (*ps1++ != *ps2++)
106 				return (1);
107 		} while (--n != 0);
108 	}
109 
110 	return (0);
111 }
112 
113 /*
114  * Our own version of log2().  Same thing as highbit()-1.
115  */
116 static int
117 zfs_log2(uint64_t num)
118 {
119 	int i = 0;
120 
121 	while (num > 1) {
122 		i++;
123 		num = num >> 1;
124 	}
125 
126 	return (i);
127 }
128 
129 /* Checksum Functions */
130 static void
131 zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
132 {
133 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
134 }
135 
136 /* Checksum Table and Values */
137 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
138 	{{NULL,			NULL},			0, 0,	"inherit"},
139 	{{NULL,			NULL},			0, 0,	"on"},
140 	{{zio_checksum_off,	zio_checksum_off},	0, 0,	"off"},
141 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"label"},
142 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"gang_header"},
143 	{{NULL,			NULL},			0, 0,	"zilog"},
144 	{{fletcher_2_native,	fletcher_2_byteswap},	0, 0,	"fletcher2"},
145 	{{fletcher_4_native,	fletcher_4_byteswap},	1, 0,	"fletcher4"},
146 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 0,	"SHA256"},
147 	{{NULL,			NULL},			0, 0,	"zilog2"},
148 };
149 
150 /*
151  * zio_checksum_verify: Provides support for checksum verification.
152  *
153  * Fletcher2, Fletcher4, and SHA256 are supported.
154  *
155  * Return:
156  * 	-1 = Failure
157  *	 0 = Success
158  */
159 static int
160 zio_checksum_verify(blkptr_t *bp, char *data, int size)
161 {
162 	zio_cksum_t zc = bp->blk_cksum;
163 	uint32_t checksum = BP_GET_CHECKSUM(bp);
164 	int byteswap = BP_SHOULD_BYTESWAP(bp);
165 	zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
166 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
167 	zio_cksum_t actual_cksum, expected_cksum;
168 
169 	if (byteswap) {
170 		grub_printf("byteswap not supported\n");
171 		return (-1);
172 	}
173 
174 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL) {
175 		grub_printf("checksum algorithm %u not supported\n", checksum);
176 		return (-1);
177 	}
178 
179 	if (ci->ci_eck) {
180 		expected_cksum = zec->zec_cksum;
181 		zec->zec_cksum = zc;
182 		ci->ci_func[0](data, size, &actual_cksum);
183 		zec->zec_cksum = expected_cksum;
184 		zc = expected_cksum;
185 	} else {
186 		ci->ci_func[byteswap](data, size, &actual_cksum);
187 	}
188 
189 	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
190 	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
191 	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
192 	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
193 		return (-1);
194 
195 	return (0);
196 }
197 
198 /*
199  * vdev_label_start returns the physical disk offset (in bytes) of
200  * label "l".
201  */
202 static uint64_t
203 vdev_label_start(uint64_t psize, int l)
204 {
205 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
206 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
207 }
208 
209 /*
210  * vdev_uberblock_compare takes two uberblock structures and returns an integer
211  * indicating the more recent of the two.
212  * 	Return Value = 1 if ub2 is more recent
213  * 	Return Value = -1 if ub1 is more recent
214  * The most recent uberblock is determined using its transaction number and
215  * timestamp.  The uberblock with the highest transaction number is
216  * considered "newer".  If the transaction numbers of the two blocks match, the
217  * timestamps are compared to determine the "newer" of the two.
218  */
219 static int
220 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
221 {
222 	if (ub1->ub_txg < ub2->ub_txg)
223 		return (-1);
224 	if (ub1->ub_txg > ub2->ub_txg)
225 		return (1);
226 
227 	if (ub1->ub_timestamp < ub2->ub_timestamp)
228 		return (-1);
229 	if (ub1->ub_timestamp > ub2->ub_timestamp)
230 		return (1);
231 
232 	return (0);
233 }
234 
235 /*
236  * Three pieces of information are needed to verify an uberblock: the magic
237  * number, the version number, and the checksum.
238  *
239  * Return:
240  *     0 - Success
241  *    -1 - Failure
242  */
243 static int
244 uberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
245 {
246 	blkptr_t bp;
247 
248 	BP_ZERO(&bp);
249 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
250 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
251 	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
252 
253 	if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
254 		return (-1);
255 
256 	if (uber->ub_magic == UBERBLOCK_MAGIC &&
257 	    SPA_VERSION_IS_SUPPORTED(uber->ub_version))
258 		return (0);
259 
260 	return (-1);
261 }
262 
263 /*
264  * Find the best uberblock.
265  * Return:
266  *    Success - Pointer to the best uberblock.
267  *    Failure - NULL
268  */
269 static uberblock_t *
270 find_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
271 {
272 	uberblock_t *ubbest = NULL;
273 	uberblock_t *ubnext;
274 	uint64_t offset, ub_size;
275 	int i;
276 
277 	ub_size = VDEV_UBERBLOCK_SIZE(ashift);
278 
279 	for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
280 		ubnext = (uberblock_t *)ub_array;
281 		ub_array += ub_size;
282 		offset = (sector << SPA_MINBLOCKSHIFT) +
283 		    VDEV_UBERBLOCK_OFFSET(ashift, i);
284 
285 		if (uberblock_verify(ubnext, ub_size, offset) != 0)
286 			continue;
287 
288 		if (ubbest == NULL ||
289 		    vdev_uberblock_compare(ubnext, ubbest) > 0)
290 			ubbest = ubnext;
291 	}
292 
293 	return (ubbest);
294 }
295 
296 /*
297  * Read a block of data based on the gang block address dva,
298  * and put its data in buf.
299  *
300  * Return:
301  *	0 - success
302  *	1 - failure
303  */
304 static int
305 zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
306 {
307 	zio_gbh_phys_t *zio_gb;
308 	uint64_t offset, sector;
309 	blkptr_t tmpbp;
310 	int i;
311 
312 	zio_gb = (zio_gbh_phys_t *)stack;
313 	stack += SPA_GANGBLOCKSIZE;
314 	offset = DVA_GET_OFFSET(dva);
315 	sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
316 
317 	/* read in the gang block header */
318 	if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
319 		grub_printf("failed to read in a gang block header\n");
320 		return (1);
321 	}
322 
323 	/* self checksuming the gang block header */
324 	BP_ZERO(&tmpbp);
325 	BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
326 	BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
327 	ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
328 	    DVA_GET_OFFSET(dva), bp->blk_birth, 0);
329 	if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
330 		grub_printf("failed to checksum a gang block header\n");
331 		return (1);
332 	}
333 
334 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
335 		if (BP_IS_HOLE(&zio_gb->zg_blkptr[i]))
336 			continue;
337 
338 		if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
339 			return (1);
340 		buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
341 	}
342 
343 	return (0);
344 }
345 
346 /*
347  * Read in a block of raw data to buf.
348  *
349  * Return:
350  *	0 - success
351  *	1 - failure
352  */
353 static int
354 zio_read_data(blkptr_t *bp, void *buf, char *stack)
355 {
356 	int i, psize;
357 
358 	psize = BP_GET_PSIZE(bp);
359 
360 	/* pick a good dva from the block pointer */
361 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
362 		uint64_t offset, sector;
363 
364 		if (bp->blk_dva[i].dva_word[0] == 0 &&
365 		    bp->blk_dva[i].dva_word[1] == 0)
366 			continue;
367 
368 		if (DVA_GET_GANG(&bp->blk_dva[i])) {
369 			if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) == 0)
370 				return (0);
371 		} else {
372 			/* read in a data block */
373 			offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
374 			sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
375 			if (devread(sector, 0, psize, buf) != 0)
376 				return (0);
377 		}
378 	}
379 
380 	return (1);
381 }
382 
383 /*
384  * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
385  * more than BPE_PAYLOAD_SIZE bytes).
386  */
387 static void
388 decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
389 {
390 	int psize, i;
391 	uint8_t *buf8 = buf;
392 	uint64_t w = 0;
393 	const uint64_t *bp64 = (const uint64_t *)bp;
394 
395 	psize = BPE_GET_PSIZE(bp);
396 
397 	/*
398 	 * Decode the words of the block pointer into the byte array.
399 	 * Low bits of first word are the first byte (little endian).
400 	 */
401 	for (i = 0; i < psize; i++) {
402 		if (i % sizeof (w) == 0) {
403 			/* beginning of a word */
404 			w = *bp64;
405 			bp64++;
406 			if (!BPE_IS_PAYLOADWORD(bp, bp64))
407 				bp64++;
408 		}
409 		buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
410 	}
411 }
412 
413 /*
414  * Fill in the buffer with the (decompressed) payload of the embedded
415  * blkptr_t.  Takes into account compression and byteorder (the payload is
416  * treated as a stream of bytes).
417  * Return 0 on success, or ENOSPC if it won't fit in the buffer.
418  */
419 static int
420 decode_embedded_bp(const blkptr_t *bp, void *buf)
421 {
422 	int comp;
423 	int lsize, psize;
424 	uint8_t *dst = buf;
425 	uint64_t w = 0;
426 
427 	lsize = BPE_GET_LSIZE(bp);
428 	psize = BPE_GET_PSIZE(bp);
429 	comp = BP_GET_COMPRESS(bp);
430 
431 	if (comp != ZIO_COMPRESS_OFF) {
432 		uint8_t dstbuf[BPE_PAYLOAD_SIZE];
433 
434 		if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
435 		    decomp_table[comp].decomp_func == NULL) {
436 			grub_printf("compression algorithm not supported\n");
437 			return (ERR_FSYS_CORRUPT);
438 		}
439 
440 		decode_embedded_bp_compressed(bp, dstbuf);
441 		decomp_table[comp].decomp_func(dstbuf, buf, psize, lsize);
442 	} else {
443 		decode_embedded_bp_compressed(bp, buf);
444 	}
445 
446 	return (0);
447 }
448 
449 /*
450  * Read in a block of data, verify its checksum, decompress if needed,
451  * and put the uncompressed data in buf.
452  *
453  * Return:
454  *	0 - success
455  *	errnum - failure
456  */
457 static int
458 zio_read(blkptr_t *bp, void *buf, char *stack)
459 {
460 	int lsize, psize, comp;
461 	char *retbuf;
462 
463 	if (BP_IS_EMBEDDED(bp)) {
464 		if (BPE_GET_ETYPE(bp) != BP_EMBEDDED_TYPE_DATA) {
465 			grub_printf("unsupported embedded BP (type=%u)\n",
466 			    (int)BPE_GET_ETYPE(bp));
467 			return (ERR_FSYS_CORRUPT);
468 		}
469 		return (decode_embedded_bp(bp, buf));
470 	}
471 
472 	comp = BP_GET_COMPRESS(bp);
473 	lsize = BP_GET_LSIZE(bp);
474 	psize = BP_GET_PSIZE(bp);
475 
476 	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
477 	    (comp != ZIO_COMPRESS_OFF &&
478 	    decomp_table[comp].decomp_func == NULL)) {
479 		grub_printf("compression algorithm not supported\n");
480 		return (ERR_FSYS_CORRUPT);
481 	}
482 
483 	if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
484 		grub_printf("not enough memory to fit %u bytes on stack\n",
485 		    lsize);
486 		return (ERR_WONT_FIT);
487 	}
488 
489 	retbuf = buf;
490 	if (comp != ZIO_COMPRESS_OFF) {
491 		buf = stack;
492 		stack += psize;
493 	}
494 
495 	if (zio_read_data(bp, buf, stack) != 0) {
496 		grub_printf("zio_read_data failed\n");
497 		return (ERR_FSYS_CORRUPT);
498 	}
499 
500 	if (zio_checksum_verify(bp, buf, psize) != 0) {
501 		grub_printf("checksum verification failed\n");
502 		return (ERR_FSYS_CORRUPT);
503 	}
504 
505 	if (comp != ZIO_COMPRESS_OFF) {
506 		if (decomp_table[comp].decomp_func(buf, retbuf, psize,
507 		    lsize) != 0) {
508 			grub_printf("zio_read decompression failed\n");
509 			return (ERR_FSYS_CORRUPT);
510 		}
511 	}
512 
513 	return (0);
514 }
515 
516 /*
517  * Get the block from a block id.
518  * push the block onto the stack.
519  *
520  * Return:
521  * 	0 - success
522  * 	errnum - failure
523  */
524 static int
525 dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
526 {
527 	int idx, level;
528 	blkptr_t *bp_array = dn->dn_blkptr;
529 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
530 	blkptr_t *bp, *tmpbuf;
531 
532 	bp = (blkptr_t *)stack;
533 	stack += sizeof (blkptr_t);
534 
535 	tmpbuf = (blkptr_t *)stack;
536 	stack += 1<<dn->dn_indblkshift;
537 
538 	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
539 		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
540 		*bp = bp_array[idx];
541 		if (level == 0)
542 			tmpbuf = buf;
543 		if (BP_IS_HOLE(bp)) {
544 			grub_memset(buf, 0,
545 			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
546 			break;
547 		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
548 			return (errnum);
549 		}
550 
551 		bp_array = tmpbuf;
552 	}
553 
554 	return (0);
555 }
556 
557 /*
558  * mzap_lookup: Looks up property described by "name" and returns the value
559  * in "value".
560  *
561  * Return:
562  *	0 - success
563  *	errnum - failure
564  */
565 static int
566 mzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
567 	uint64_t *value)
568 {
569 	int i, chunks;
570 	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
571 
572 	chunks = objsize / MZAP_ENT_LEN - 1;
573 	for (i = 0; i < chunks; i++) {
574 		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
575 			*value = mzap_ent[i].mze_value;
576 			return (0);
577 		}
578 	}
579 
580 	return (ERR_FSYS_CORRUPT);
581 }
582 
583 static uint64_t
584 zap_hash(uint64_t salt, const char *name)
585 {
586 	static uint64_t table[256];
587 	const uint8_t *cp;
588 	uint8_t c;
589 	uint64_t crc = salt;
590 
591 	if (table[128] == 0) {
592 		uint64_t *ct;
593 		int i, j;
594 		for (i = 0; i < 256; i++) {
595 			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
596 				*ct = (*ct >> 1) ^ (-(*ct & 1) &
597 				    ZFS_CRC64_POLY);
598 		}
599 	}
600 
601 	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
602 		errnum = ERR_FSYS_CORRUPT;
603 		return (0);
604 	}
605 
606 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
607 		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
608 
609 	/*
610 	 * Only use 28 bits, since we need 4 bits in the cookie for the
611 	 * collision differentiator.  We MUST use the high bits, since
612 	 * those are the ones that we first pay attention to when
613 	 * choosing the bucket.
614 	 */
615 	crc &= ~((1ULL << (64 - 28)) - 1);
616 
617 	return (crc);
618 }
619 
620 /*
621  * Only to be used on 8-bit arrays.
622  * array_len is actual len in bytes (not encoded le_value_length).
623  * buf is null-terminated.
624  */
625 static int
626 zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
627     int array_len, const char *buf)
628 {
629 	int bseen = 0;
630 
631 	while (bseen < array_len) {
632 		struct zap_leaf_array *la =
633 		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
634 		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
635 
636 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
637 			return (0);
638 
639 		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
640 			break;
641 		chunk = la->la_next;
642 		bseen += toread;
643 	}
644 	return (bseen == array_len);
645 }
646 
647 /*
648  * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
649  * value for the property "name".
650  *
651  * Return:
652  *	0 - success
653  *	errnum - failure
654  */
655 static int
656 zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
657     const char *name, uint64_t *value)
658 {
659 	uint16_t chunk;
660 	struct zap_leaf_entry *le;
661 
662 	/* Verify if this is a valid leaf block */
663 	if (l->l_hdr.lh_block_type != ZBT_LEAF)
664 		return (ERR_FSYS_CORRUPT);
665 	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
666 		return (ERR_FSYS_CORRUPT);
667 
668 	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
669 	    chunk != CHAIN_END; chunk = le->le_next) {
670 
671 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
672 			return (ERR_FSYS_CORRUPT);
673 
674 		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
675 
676 		/* Verify the chunk entry */
677 		if (le->le_type != ZAP_CHUNK_ENTRY)
678 			return (ERR_FSYS_CORRUPT);
679 
680 		if (le->le_hash != h)
681 			continue;
682 
683 		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
684 		    le->le_name_length, name)) {
685 
686 			struct zap_leaf_array *la;
687 			uint8_t *ip;
688 
689 			if (le->le_int_size != 8 || le->le_value_length != 1)
690 				return (ERR_FSYS_CORRUPT);
691 
692 			/* get the uint64_t property value */
693 			la = &ZAP_LEAF_CHUNK(l, blksft,
694 			    le->le_value_chunk).l_array;
695 			ip = la->la_array;
696 
697 			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
698 			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
699 			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
700 			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
701 
702 			return (0);
703 		}
704 	}
705 
706 	return (ERR_FSYS_CORRUPT);
707 }
708 
709 /*
710  * Fat ZAP lookup
711  *
712  * Return:
713  *	0 - success
714  *	errnum - failure
715  */
716 static int
717 fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
718     const char *name, uint64_t *value, char *stack)
719 {
720 	zap_leaf_phys_t *l;
721 	uint64_t hash, idx, blkid;
722 	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
723 
724 	/* Verify if this is a fat zap header block */
725 	if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
726 	    zap->zap_flags != 0)
727 		return (ERR_FSYS_CORRUPT);
728 
729 	hash = zap_hash(zap->zap_salt, name);
730 	if (errnum)
731 		return (errnum);
732 
733 	/* get block id from index */
734 	if (zap->zap_ptrtbl.zt_numblks != 0) {
735 		/* external pointer tables not supported */
736 		return (ERR_FSYS_CORRUPT);
737 	}
738 	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
739 	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
740 
741 	/* Get the leaf block */
742 	l = (zap_leaf_phys_t *)stack;
743 	stack += 1<<blksft;
744 	if ((1<<blksft) < sizeof (zap_leaf_phys_t))
745 		return (ERR_FSYS_CORRUPT);
746 	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
747 		return (errnum);
748 
749 	return (zap_leaf_lookup(l, blksft, hash, name, value));
750 }
751 
752 /*
753  * Read in the data of a zap object and find the value for a matching
754  * property name.
755  *
756  * Return:
757  *	0 - success
758  *	errnum - failure
759  */
760 static int
761 zap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
762     char *stack)
763 {
764 	uint64_t block_type;
765 	int size;
766 	void *zapbuf;
767 
768 	/* Read in the first block of the zap object data. */
769 	zapbuf = stack;
770 	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
771 	stack += size;
772 
773 	if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
774 		return (errnum);
775 
776 	block_type = *((uint64_t *)zapbuf);
777 
778 	if (block_type == ZBT_MICRO) {
779 		return (mzap_lookup(zapbuf, size, name, val));
780 	} else if (block_type == ZBT_HEADER) {
781 		/* this is a fat zap */
782 		return (fzap_lookup(zap_dnode, zapbuf, name,
783 		    val, stack));
784 	}
785 
786 	return (ERR_FSYS_CORRUPT);
787 }
788 
789 typedef struct zap_attribute {
790 	int za_integer_length;
791 	uint64_t za_num_integers;
792 	uint64_t za_first_integer;
793 	char *za_name;
794 } zap_attribute_t;
795 
796 typedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
797 
798 static int
799 zap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
800 {
801 	uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
802 	zap_attribute_t za;
803 	int i;
804 	mzap_phys_t *mzp = (mzap_phys_t *)stack;
805 	stack += size;
806 
807 	if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
808 		return (errnum);
809 
810 	/*
811 	 * Iteration over fatzap objects has not yet been implemented.
812 	 * If we encounter a pool in which there are more features for
813 	 * read than can fit inside a microzap (i.e., more than 2048
814 	 * features for read), we can add support for fatzap iteration.
815 	 * For now, fail.
816 	 */
817 	if (mzp->mz_block_type != ZBT_MICRO) {
818 		grub_printf("feature information stored in fatzap, pool "
819 		    "version not supported\n");
820 		return (1);
821 	}
822 
823 	za.za_integer_length = 8;
824 	za.za_num_integers = 1;
825 	for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
826 		mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
827 		int err;
828 
829 		za.za_first_integer = mzep->mze_value;
830 		za.za_name = mzep->mze_name;
831 		err = cb(&za, arg, stack);
832 		if (err != 0)
833 			return (err);
834 	}
835 
836 	return (0);
837 }
838 
839 /*
840  * Get the dnode of an object number from the metadnode of an object set.
841  *
842  * Input
843  *	mdn - metadnode to get the object dnode
844  *	objnum - object number for the object dnode
845  *	type - if nonzero, object must be of this type
846  *	buf - data buffer that holds the returning dnode
847  *	stack - scratch area
848  *
849  * Return:
850  *	0 - success
851  *	errnum - failure
852  */
853 static int
854 dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
855 	char *stack)
856 {
857 	uint64_t blkid, blksz; /* the block id this object dnode is in */
858 	int epbs; /* shift of number of dnodes in a block */
859 	int idx; /* index within a block */
860 	dnode_phys_t *dnbuf;
861 
862 	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
863 	epbs = zfs_log2(blksz) - DNODE_SHIFT;
864 	blkid = objnum >> epbs;
865 	idx = objnum & ((1<<epbs)-1);
866 
867 	if (dnode_buf != NULL && dnode_mdn == mdn &&
868 	    objnum >= dnode_start && objnum < dnode_end) {
869 		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
870 		VERIFY_DN_TYPE(buf, type);
871 		return (0);
872 	}
873 
874 	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
875 		dnbuf = dnode_buf;
876 		dnode_mdn = mdn;
877 		dnode_start = blkid << epbs;
878 		dnode_end = (blkid + 1) << epbs;
879 	} else {
880 		dnbuf = (dnode_phys_t *)stack;
881 		stack += blksz;
882 	}
883 
884 	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
885 		return (errnum);
886 
887 	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
888 	VERIFY_DN_TYPE(buf, type);
889 
890 	return (0);
891 }
892 
893 /*
894  * Check if this is a special file that resides at the top
895  * dataset of the pool. Currently this is the GRUB menu,
896  * boot signature and boot signature backup.
897  * str starts with '/'.
898  */
899 static int
900 is_top_dataset_file(char *str)
901 {
902 	char *tptr;
903 
904 	if ((tptr = grub_strstr(str, "menu.lst")) &&
905 	    (tptr[8] == '\0' || tptr[8] == ' ') &&
906 	    *(tptr-1) == '/')
907 		return (1);
908 
909 	if (grub_strncmp(str, BOOTSIGN_DIR"/",
910 	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
911 		return (1);
912 
913 	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
914 		return (1);
915 
916 	return (0);
917 }
918 
919 static int
920 check_feature(zap_attribute_t *za, void *arg, char *stack)
921 {
922 	const char **names = arg;
923 	int i;
924 
925 	if (za->za_first_integer == 0)
926 		return (0);
927 
928 	for (i = 0; names[i] != NULL; i++) {
929 		if (grub_strcmp(za->za_name, names[i]) == 0) {
930 			return (0);
931 		}
932 	}
933 	grub_printf("missing feature for read '%s'\n", za->za_name);
934 	return (ERR_NEWER_VERSION);
935 }
936 
937 /*
938  * Get the file dnode for a given file name where mdn is the meta dnode
939  * for this ZFS object set. When found, place the file dnode in dn.
940  * The 'path' argument will be mangled.
941  *
942  * Return:
943  *	0 - success
944  *	errnum - failure
945  */
946 static int
947 dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
948     char *stack)
949 {
950 	uint64_t objnum, version;
951 	char *cname, ch;
952 
953 	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
954 	    dn, stack))
955 		return (errnum);
956 
957 	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
958 		return (errnum);
959 	if (version > ZPL_VERSION)
960 		return (-1);
961 
962 	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
963 		return (errnum);
964 
965 	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
966 	    dn, stack))
967 		return (errnum);
968 
969 	/* skip leading slashes */
970 	while (*path == '/')
971 		path++;
972 
973 	while (*path && !grub_isspace(*path)) {
974 
975 		/* get the next component name */
976 		cname = path;
977 		while (*path && !grub_isspace(*path) && *path != '/')
978 			path++;
979 		ch = *path;
980 		*path = 0;   /* ensure null termination */
981 
982 		if (errnum = zap_lookup(dn, cname, &objnum, stack))
983 			return (errnum);
984 
985 		objnum = ZFS_DIRENT_OBJ(objnum);
986 		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
987 			return (errnum);
988 
989 		*path = ch;
990 		while (*path == '/')
991 			path++;
992 	}
993 
994 	/* We found the dnode for this file. Verify if it is a plain file. */
995 	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
996 
997 	return (0);
998 }
999 
1000 /*
1001  * Get the default 'bootfs' property value from the rootpool.
1002  *
1003  * Return:
1004  *	0 - success
1005  *	errnum -failure
1006  */
1007 static int
1008 get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
1009 {
1010 	uint64_t objnum = 0;
1011 	dnode_phys_t *dn = (dnode_phys_t *)stack;
1012 	stack += DNODE_SIZE;
1013 
1014 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1015 	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
1016 		return (errnum);
1017 
1018 	/*
1019 	 * find the object number for 'pool_props', and get the dnode
1020 	 * of the 'pool_props'.
1021 	 */
1022 	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
1023 		return (ERR_FILESYSTEM_NOT_FOUND);
1024 
1025 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
1026 		return (errnum);
1027 
1028 	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
1029 		return (ERR_FILESYSTEM_NOT_FOUND);
1030 
1031 	if (!objnum)
1032 		return (ERR_FILESYSTEM_NOT_FOUND);
1033 
1034 	*obj = objnum;
1035 	return (0);
1036 }
1037 
1038 /*
1039  * List of pool features that the grub implementation of ZFS supports for
1040  * read. Note that features that are only required for write do not need
1041  * to be listed here since grub opens pools in read-only mode.
1042  *
1043  * When this list is updated the version number in usr/src/grub/capability
1044  * must be incremented to ensure the new grub gets installed.
1045  */
1046 static const char *spa_feature_names[] = {
1047 	"org.illumos:lz4_compress",
1048 	"com.delphix:hole_birth",
1049 	"com.delphix:extensible_dataset",
1050 	"com.delphix:embedded_data",
1051 	NULL
1052 };
1053 
1054 /*
1055  * Checks whether the MOS features that are active are supported by this
1056  * (GRUB's) implementation of ZFS.
1057  *
1058  * Return:
1059  *	0: Success.
1060  *	errnum: Failure.
1061  */
1062 static int
1063 check_mos_features(dnode_phys_t *mosmdn, char *stack)
1064 {
1065 	uint64_t objnum;
1066 	dnode_phys_t *dn;
1067 	uint8_t error = 0;
1068 
1069 	dn = (dnode_phys_t *)stack;
1070 	stack += DNODE_SIZE;
1071 
1072 	if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1073 	    DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
1074 		return (errnum);
1075 
1076 	/*
1077 	 * Find the object number for 'features_for_read' and retrieve its
1078 	 * corresponding dnode. Note that we don't check features_for_write
1079 	 * because GRUB is not opening the pool for write.
1080 	 */
1081 	if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
1082 	    stack)) != 0)
1083 		return (errnum);
1084 
1085 	if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
1086 	    dn, stack)) != 0)
1087 		return (errnum);
1088 
1089 	return (zap_iterate(dn, check_feature, spa_feature_names, stack));
1090 }
1091 
1092 /*
1093  * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1094  * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1095  * of pool/rootfs.
1096  *
1097  * If no fsname and no obj are given, return the DSL_DIR metadnode.
1098  * If fsname is given, return its metadnode and its matching object number.
1099  * If only obj is given, return the metadnode for this object number.
1100  *
1101  * Return:
1102  *	0 - success
1103  *	errnum - failure
1104  */
1105 static int
1106 get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
1107     dnode_phys_t *mdn, char *stack)
1108 {
1109 	uint64_t objnum, headobj;
1110 	char *cname, ch;
1111 	blkptr_t *bp;
1112 	objset_phys_t *osp;
1113 	int issnapshot = 0;
1114 	char *snapname;
1115 
1116 	if (fsname == NULL && obj) {
1117 		headobj = *obj;
1118 		goto skip;
1119 	}
1120 
1121 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1122 	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
1123 		return (errnum);
1124 
1125 	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
1126 	    stack))
1127 		return (errnum);
1128 
1129 	if (errnum = dnode_get(mosmdn, objnum, 0, mdn, stack))
1130 		return (errnum);
1131 
1132 	if (fsname == NULL) {
1133 		headobj =
1134 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1135 		goto skip;
1136 	}
1137 
1138 	/* take out the pool name */
1139 	while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1140 		fsname++;
1141 
1142 	while (*fsname && !grub_isspace(*fsname)) {
1143 		uint64_t childobj;
1144 
1145 		while (*fsname == '/')
1146 			fsname++;
1147 
1148 		cname = fsname;
1149 		while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1150 			fsname++;
1151 		ch = *fsname;
1152 		*fsname = 0;
1153 
1154 		snapname = cname;
1155 		while (*snapname && !grub_isspace(*snapname) && *snapname !=
1156 		    '@')
1157 			snapname++;
1158 		if (*snapname == '@') {
1159 			issnapshot = 1;
1160 			*snapname = 0;
1161 		}
1162 		childobj =
1163 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
1164 		if (errnum = dnode_get(mosmdn, childobj,
1165 		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
1166 			return (errnum);
1167 
1168 		if (zap_lookup(mdn, cname, &objnum, stack))
1169 			return (ERR_FILESYSTEM_NOT_FOUND);
1170 
1171 		if (errnum = dnode_get(mosmdn, objnum, 0,
1172 		    mdn, stack))
1173 			return (errnum);
1174 
1175 		*fsname = ch;
1176 		if (issnapshot)
1177 			*snapname = '@';
1178 	}
1179 	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1180 	if (obj)
1181 		*obj = headobj;
1182 
1183 skip:
1184 	if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1185 		return (errnum);
1186 	if (issnapshot) {
1187 		uint64_t snapobj;
1188 
1189 		snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
1190 		    ds_snapnames_zapobj;
1191 
1192 		if (errnum = dnode_get(mosmdn, snapobj,
1193 		    DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
1194 			return (errnum);
1195 		if (zap_lookup(mdn, snapname + 1, &headobj, stack))
1196 			return (ERR_FILESYSTEM_NOT_FOUND);
1197 		if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1198 			return (errnum);
1199 		if (obj)
1200 			*obj = headobj;
1201 	}
1202 
1203 	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
1204 	osp = (objset_phys_t *)stack;
1205 	stack += sizeof (objset_phys_t);
1206 	if (errnum = zio_read(bp, osp, stack))
1207 		return (errnum);
1208 
1209 	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
1210 
1211 	return (0);
1212 }
1213 
1214 /*
1215  * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1216  *
1217  * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1218  *
1219  *      encoding method/host endian     (4 bytes)
1220  *      nvl_version                     (4 bytes)
1221  *      nvl_nvflag                      (4 bytes)
1222  *	encoded nvpairs:
1223  *		encoded size of the nvpair      (4 bytes)
1224  *		decoded size of the nvpair      (4 bytes)
1225  *		name string size                (4 bytes)
1226  *		name string data                (sizeof(NV_ALIGN4(string))
1227  *		data type                       (4 bytes)
1228  *		# of elements in the nvpair     (4 bytes)
1229  *		data
1230  *      2 zero's for the last nvpair
1231  *		(end of the entire list)	(8 bytes)
1232  *
1233  * Return:
1234  *	0 - success
1235  *	1 - failure
1236  */
1237 static int
1238 nvlist_unpack(char *nvlist, char **out)
1239 {
1240 	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
1241 	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1242 		return (1);
1243 
1244 	*out = nvlist + 4;
1245 	return (0);
1246 }
1247 
1248 static char *
1249 nvlist_array(char *nvlist, int index)
1250 {
1251 	int i, encode_size;
1252 
1253 	for (i = 0; i < index; i++) {
1254 		/* skip the header, nvl_version, and nvl_nvflag */
1255 		nvlist = nvlist + 4 * 2;
1256 
1257 		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1258 			nvlist += encode_size; /* goto the next nvpair */
1259 
1260 		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1261 	}
1262 
1263 	return (nvlist);
1264 }
1265 
1266 /*
1267  * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
1268  * list following nvpair. If nvpair is NULL, the first pair is returned. If
1269  * nvpair is the last pair in the nvlist, NULL is returned.
1270  */
1271 static char *
1272 nvlist_next_nvpair(char *nvl, char *nvpair)
1273 {
1274 	char *cur, *prev;
1275 	int encode_size;
1276 
1277 	if (nvl == NULL)
1278 		return (NULL);
1279 
1280 	if (nvpair == NULL) {
1281 		/* skip over nvl_version and nvl_nvflag */
1282 		nvpair = nvl + 4 * 2;
1283 	} else {
1284 		/* skip to the next nvpair */
1285 		encode_size = BSWAP_32(*(uint32_t *)nvpair);
1286 		nvpair += encode_size;
1287 	}
1288 
1289 	/* 8 bytes of 0 marks the end of the list */
1290 	if (*(uint64_t *)nvpair == 0)
1291 		return (NULL);
1292 
1293 	return (nvpair);
1294 }
1295 
1296 /*
1297  * This function returns 0 on success and 1 on failure. On success, a string
1298  * containing the name of nvpair is saved in buf.
1299  */
1300 static int
1301 nvpair_name(char *nvp, char *buf, int buflen)
1302 {
1303 	int len;
1304 
1305 	/* skip over encode/decode size */
1306 	nvp += 4 * 2;
1307 
1308 	len = BSWAP_32(*(uint32_t *)nvp);
1309 	if (buflen < len + 1)
1310 		return (1);
1311 
1312 	grub_memmove(buf, nvp + 4, len);
1313 	buf[len] = '\0';
1314 
1315 	return (0);
1316 }
1317 
1318 /*
1319  * This function retrieves the value of the nvpair in the form of enumerated
1320  * type data_type_t. This is used to determine the appropriate type to pass to
1321  * nvpair_value().
1322  */
1323 static int
1324 nvpair_type(char *nvp)
1325 {
1326 	int name_len, type;
1327 
1328 	/* skip over encode/decode size */
1329 	nvp += 4 * 2;
1330 
1331 	/* skip over name_len */
1332 	name_len = BSWAP_32(*(uint32_t *)nvp);
1333 	nvp += 4;
1334 
1335 	/* skip over name */
1336 	nvp = nvp + ((name_len + 3) & ~3); /* align */
1337 
1338 	type = BSWAP_32(*(uint32_t *)nvp);
1339 
1340 	return (type);
1341 }
1342 
1343 static int
1344 nvpair_value(char *nvp, void *val, int valtype, int *nelmp)
1345 {
1346 	int name_len, type, slen;
1347 	char *strval = val;
1348 	uint64_t *intval = val;
1349 
1350 	/* skip over encode/decode size */
1351 	nvp += 4 * 2;
1352 
1353 	/* skip over name_len */
1354 	name_len = BSWAP_32(*(uint32_t *)nvp);
1355 	nvp += 4;
1356 
1357 	/* skip over name */
1358 	nvp = nvp + ((name_len + 3) & ~3); /* align */
1359 
1360 	/* skip over type */
1361 	type = BSWAP_32(*(uint32_t *)nvp);
1362 	nvp += 4;
1363 
1364 	if (type == valtype) {
1365 		int nelm;
1366 
1367 		nelm = BSWAP_32(*(uint32_t *)nvp);
1368 		if (valtype != DATA_TYPE_BOOLEAN && nelm < 1)
1369 			return (1);
1370 		nvp += 4;
1371 
1372 		switch (valtype) {
1373 		case DATA_TYPE_BOOLEAN:
1374 			return (0);
1375 
1376 		case DATA_TYPE_STRING:
1377 			slen = BSWAP_32(*(uint32_t *)nvp);
1378 			nvp += 4;
1379 			grub_memmove(strval, nvp, slen);
1380 			strval[slen] = '\0';
1381 			return (0);
1382 
1383 		case DATA_TYPE_UINT64:
1384 			*intval = BSWAP_64(*(uint64_t *)nvp);
1385 			return (0);
1386 
1387 		case DATA_TYPE_NVLIST:
1388 			*(void **)val = (void *)nvp;
1389 			return (0);
1390 
1391 		case DATA_TYPE_NVLIST_ARRAY:
1392 			*(void **)val = (void *)nvp;
1393 			if (nelmp)
1394 				*nelmp = nelm;
1395 			return (0);
1396 		}
1397 	}
1398 
1399 	return (1);
1400 }
1401 
1402 static int
1403 nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1404     int *nelmp)
1405 {
1406 	char *nvpair;
1407 
1408 	for (nvpair = nvlist_next_nvpair(nvlist, NULL);
1409 	    nvpair != NULL;
1410 	    nvpair = nvlist_next_nvpair(nvlist, nvpair)) {
1411 		int name_len = BSWAP_32(*(uint32_t *)(nvpair + 4 * 2));
1412 		char *nvp_name = nvpair + 4 * 3;
1413 
1414 		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1415 		    nvpair_type(nvpair) == valtype) {
1416 			return (nvpair_value(nvpair, val, valtype, nelmp));
1417 		}
1418 	}
1419 	return (1);
1420 }
1421 
1422 /*
1423  * Check if this vdev is online and is in a good state.
1424  */
1425 static int
1426 vdev_validate(char *nv)
1427 {
1428 	uint64_t ival;
1429 
1430 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1431 	    DATA_TYPE_UINT64, NULL) == 0 ||
1432 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1433 	    DATA_TYPE_UINT64, NULL) == 0 ||
1434 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1435 	    DATA_TYPE_UINT64, NULL) == 0)
1436 		return (ERR_DEV_VALUES);
1437 
1438 	return (0);
1439 }
1440 
1441 /*
1442  * Get a valid vdev pathname/devid from the boot device.
1443  * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1444  */
1445 static int
1446 vdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
1447     int is_spare)
1448 {
1449 	char type[16];
1450 
1451 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1452 	    NULL))
1453 		return (ERR_FSYS_CORRUPT);
1454 
1455 	if (grub_strcmp(type, VDEV_TYPE_DISK) == 0) {
1456 		uint64_t guid;
1457 
1458 		if (vdev_validate(nv) != 0)
1459 			return (ERR_NO_BOOTPATH);
1460 
1461 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1462 		    &guid, DATA_TYPE_UINT64, NULL) != 0)
1463 			return (ERR_NO_BOOTPATH);
1464 
1465 		if (guid != inguid)
1466 			return (ERR_NO_BOOTPATH);
1467 
1468 		/* for a spare vdev, pick the disk labeled with "is_spare" */
1469 		if (is_spare) {
1470 			uint64_t spare = 0;
1471 			(void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
1472 			    &spare, DATA_TYPE_UINT64, NULL);
1473 			if (!spare)
1474 				return (ERR_NO_BOOTPATH);
1475 		}
1476 
1477 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
1478 		    bootpath, DATA_TYPE_STRING, NULL) != 0)
1479 			bootpath[0] = '\0';
1480 
1481 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
1482 		    devid, DATA_TYPE_STRING, NULL) != 0)
1483 			devid[0] = '\0';
1484 
1485 		if (grub_strlen(bootpath) >= MAXPATHLEN ||
1486 		    grub_strlen(devid) >= MAXPATHLEN)
1487 			return (ERR_WONT_FIT);
1488 
1489 		return (0);
1490 
1491 	} else if (grub_strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1492 	    grub_strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1493 	    (is_spare = (grub_strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1494 		int nelm, i;
1495 		char *child;
1496 
1497 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
1498 		    DATA_TYPE_NVLIST_ARRAY, &nelm))
1499 			return (ERR_FSYS_CORRUPT);
1500 
1501 		for (i = 0; i < nelm; i++) {
1502 			char *child_i;
1503 
1504 			child_i = nvlist_array(child, i);
1505 			if (vdev_get_bootpath(child_i, inguid, devid,
1506 			    bootpath, is_spare) == 0)
1507 				return (0);
1508 		}
1509 	}
1510 
1511 	return (ERR_NO_BOOTPATH);
1512 }
1513 
1514 /*
1515  * Check the disk label information and retrieve needed vdev name-value pairs.
1516  *
1517  * Return:
1518  *	0 - success
1519  *	ERR_* - failure
1520  */
1521 static int
1522 check_pool_label(uint64_t sector, char *stack, char *outdevid,
1523     char *outpath, uint64_t *outguid, uint64_t *outashift, uint64_t *outversion)
1524 {
1525 	vdev_phys_t *vdev;
1526 	uint64_t pool_state, txg = 0;
1527 	char *nvlist, *nv, *features;
1528 	uint64_t diskguid;
1529 
1530 	sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
1531 
1532 	/* Read in the vdev name-value pair list (112K). */
1533 	if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
1534 		return (ERR_READ);
1535 
1536 	vdev = (vdev_phys_t *)stack;
1537 	stack += sizeof (vdev_phys_t);
1538 
1539 	if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
1540 		return (ERR_FSYS_CORRUPT);
1541 
1542 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
1543 	    DATA_TYPE_UINT64, NULL))
1544 		return (ERR_FSYS_CORRUPT);
1545 
1546 	if (pool_state == POOL_STATE_DESTROYED)
1547 		return (ERR_FILESYSTEM_NOT_FOUND);
1548 
1549 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
1550 	    current_rootpool, DATA_TYPE_STRING, NULL))
1551 		return (ERR_FSYS_CORRUPT);
1552 
1553 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
1554 	    DATA_TYPE_UINT64, NULL))
1555 		return (ERR_FSYS_CORRUPT);
1556 
1557 	/* not an active device */
1558 	if (txg == 0)
1559 		return (ERR_NO_BOOTPATH);
1560 
1561 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, outversion,
1562 	    DATA_TYPE_UINT64, NULL))
1563 		return (ERR_FSYS_CORRUPT);
1564 	if (!SPA_VERSION_IS_SUPPORTED(*outversion))
1565 		return (ERR_NEWER_VERSION);
1566 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1567 	    DATA_TYPE_NVLIST, NULL))
1568 		return (ERR_FSYS_CORRUPT);
1569 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1570 	    DATA_TYPE_UINT64, NULL))
1571 		return (ERR_FSYS_CORRUPT);
1572 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_ASHIFT, outashift,
1573 	    DATA_TYPE_UINT64, NULL) != 0)
1574 		return (ERR_FSYS_CORRUPT);
1575 	if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
1576 		return (ERR_NO_BOOTPATH);
1577 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
1578 	    DATA_TYPE_UINT64, NULL))
1579 		return (ERR_FSYS_CORRUPT);
1580 
1581 	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1582 	    &features, DATA_TYPE_NVLIST, NULL) == 0) {
1583 		char *nvp;
1584 		char *name = stack;
1585 		stack += MAXNAMELEN;
1586 
1587 		for (nvp = nvlist_next_nvpair(features, NULL);
1588 		    nvp != NULL;
1589 		    nvp = nvlist_next_nvpair(features, nvp)) {
1590 			zap_attribute_t za;
1591 
1592 			if (nvpair_name(nvp, name, MAXNAMELEN) != 0)
1593 				return (ERR_FSYS_CORRUPT);
1594 
1595 			za.za_integer_length = 8;
1596 			za.za_num_integers = 1;
1597 			za.za_first_integer = 1;
1598 			za.za_name = name;
1599 			if (check_feature(&za, spa_feature_names, stack) != 0)
1600 				return (ERR_NEWER_VERSION);
1601 		}
1602 	}
1603 
1604 	return (0);
1605 }
1606 
1607 /*
1608  * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1609  * to the memory address MOS.
1610  *
1611  * Return:
1612  *	1 - success
1613  *	0 - failure
1614  */
1615 int
1616 zfs_mount(void)
1617 {
1618 	char *stack, *ub_array;
1619 	int label = 0;
1620 	uberblock_t *ubbest;
1621 	objset_phys_t *osp;
1622 	char tmp_bootpath[MAXNAMELEN];
1623 	char tmp_devid[MAXNAMELEN];
1624 	uint64_t tmp_guid, ashift, version;
1625 	uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
1626 	int err = errnum; /* preserve previous errnum state */
1627 
1628 	/* if it's our first time here, zero the best uberblock out */
1629 	if (best_drive == 0 && best_part == 0 && find_best_root) {
1630 		grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
1631 		pool_guid = 0;
1632 	}
1633 
1634 	stackbase = ZFS_SCRATCH;
1635 	stack = stackbase;
1636 	ub_array = stack;
1637 	stack += VDEV_UBERBLOCK_RING;
1638 
1639 	osp = (objset_phys_t *)stack;
1640 	stack += sizeof (objset_phys_t);
1641 	adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
1642 
1643 	for (label = 0; label < VDEV_LABELS; label++) {
1644 
1645 		/*
1646 		 * some eltorito stacks don't give us a size and
1647 		 * we end up setting the size to MAXUINT, further
1648 		 * some of these devices stop working once a single
1649 		 * read past the end has been issued. Checking
1650 		 * for a maximum part_length and skipping the backup
1651 		 * labels at the end of the slice/partition/device
1652 		 * avoids breaking down on such devices.
1653 		 */
1654 		if (part_length == MAXUINT && label == 2)
1655 			break;
1656 
1657 		uint64_t sector = vdev_label_start(adjpl,
1658 		    label) >> SPA_MINBLOCKSHIFT;
1659 
1660 		/* Read in the uberblock ring (128K). */
1661 		if (devread(sector  +
1662 		    ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT),
1663 		    0, VDEV_UBERBLOCK_RING, ub_array) == 0)
1664 			continue;
1665 
1666 		if (check_pool_label(sector, stack, tmp_devid,
1667 		    tmp_bootpath, &tmp_guid, &ashift, &version))
1668 			continue;
1669 
1670 		if (pool_guid == 0)
1671 			pool_guid = tmp_guid;
1672 
1673 		if ((ubbest = find_bestub(ub_array, ashift, sector)) == NULL ||
1674 		    zio_read(&ubbest->ub_rootbp, osp, stack) != 0)
1675 			continue;
1676 
1677 		VERIFY_OS_TYPE(osp, DMU_OST_META);
1678 
1679 		if (version >= SPA_VERSION_FEATURES &&
1680 		    check_mos_features(&osp->os_meta_dnode, stack) != 0)
1681 			continue;
1682 
1683 		if (find_best_root && ((pool_guid != tmp_guid) ||
1684 		    vdev_uberblock_compare(ubbest, &(current_uberblock)) <= 0))
1685 			continue;
1686 
1687 		/* Got the MOS. Save it at the memory addr MOS. */
1688 		grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
1689 		grub_memmove(&current_uberblock, ubbest, sizeof (uberblock_t));
1690 		grub_memmove(current_bootpath, tmp_bootpath, MAXNAMELEN);
1691 		grub_memmove(current_devid, tmp_devid, grub_strlen(tmp_devid));
1692 		is_zfs_mount = 1;
1693 		return (1);
1694 	}
1695 
1696 	/*
1697 	 * While some fs impls. (tftp) rely on setting and keeping
1698 	 * global errnums set, others won't reset it and will break
1699 	 * when issuing rawreads. The goal here is to simply not
1700 	 * have zfs mount attempts impact the previous state.
1701 	 */
1702 	errnum = err;
1703 	return (0);
1704 }
1705 
1706 /*
1707  * zfs_open() locates a file in the rootpool by following the
1708  * MOS and places the dnode of the file in the memory address DNODE.
1709  *
1710  * Return:
1711  *	1 - success
1712  *	0 - failure
1713  */
1714 int
1715 zfs_open(char *filename)
1716 {
1717 	char *stack;
1718 	dnode_phys_t *mdn;
1719 
1720 	file_buf = NULL;
1721 	stackbase = ZFS_SCRATCH;
1722 	stack = stackbase;
1723 
1724 	mdn = (dnode_phys_t *)stack;
1725 	stack += sizeof (dnode_phys_t);
1726 
1727 	dnode_mdn = NULL;
1728 	dnode_buf = (dnode_phys_t *)stack;
1729 	stack += 1<<DNODE_BLOCK_SHIFT;
1730 
1731 	/*
1732 	 * menu.lst is placed at the root pool filesystem level,
1733 	 * do not goto 'current_bootfs'.
1734 	 */
1735 	if (is_top_dataset_file(filename)) {
1736 		if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
1737 			return (0);
1738 
1739 		current_bootfs_obj = 0;
1740 	} else {
1741 		if (current_bootfs[0] == '\0') {
1742 			/* Get the default root filesystem object number */
1743 			if (errnum = get_default_bootfsobj(MOS,
1744 			    &current_bootfs_obj, stack))
1745 				return (0);
1746 
1747 			if (errnum = get_objset_mdn(MOS, NULL,
1748 			    &current_bootfs_obj, mdn, stack))
1749 				return (0);
1750 		} else {
1751 			if (errnum = get_objset_mdn(MOS, current_bootfs,
1752 			    &current_bootfs_obj, mdn, stack)) {
1753 				grub_memset(current_bootfs, 0, MAXNAMELEN);
1754 				return (0);
1755 			}
1756 		}
1757 	}
1758 
1759 	if (dnode_get_path(mdn, filename, DNODE, stack)) {
1760 		errnum = ERR_FILE_NOT_FOUND;
1761 		return (0);
1762 	}
1763 
1764 	/* get the file size and set the file position to 0 */
1765 
1766 	/*
1767 	 * For DMU_OT_SA we will need to locate the SIZE attribute
1768 	 * attribute, which could be either in the bonus buffer
1769 	 * or the "spill" block.
1770 	 */
1771 	if (DNODE->dn_bonustype == DMU_OT_SA) {
1772 		sa_hdr_phys_t *sahdrp;
1773 		int hdrsize;
1774 
1775 		if (DNODE->dn_bonuslen != 0) {
1776 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(DNODE);
1777 		} else {
1778 			if (DNODE->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1779 				blkptr_t *bp = &DNODE->dn_spill;
1780 				void *buf;
1781 
1782 				buf = (void *)stack;
1783 				stack += BP_GET_LSIZE(bp);
1784 
1785 				/* reset errnum to rawread() failure */
1786 				errnum = 0;
1787 				if (zio_read(bp, buf, stack) != 0) {
1788 					return (0);
1789 				}
1790 				sahdrp = buf;
1791 			} else {
1792 				errnum = ERR_FSYS_CORRUPT;
1793 				return (0);
1794 			}
1795 		}
1796 		hdrsize = SA_HDR_SIZE(sahdrp);
1797 		filemax = *(uint64_t *)((char *)sahdrp + hdrsize +
1798 		    SA_SIZE_OFFSET);
1799 	} else {
1800 		filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
1801 	}
1802 	filepos = 0;
1803 
1804 	dnode_buf = NULL;
1805 	return (1);
1806 }
1807 
1808 /*
1809  * zfs_read reads in the data blocks pointed by the DNODE.
1810  *
1811  * Return:
1812  *	len - the length successfully read in to the buffer
1813  *	0   - failure
1814  */
1815 int
1816 zfs_read(char *buf, int len)
1817 {
1818 	char *stack;
1819 	int blksz, length, movesize;
1820 
1821 	if (file_buf == NULL) {
1822 		file_buf = stackbase;
1823 		stackbase += SPA_MAXBLOCKSIZE;
1824 		file_start = file_end = 0;
1825 	}
1826 	stack = stackbase;
1827 
1828 	/*
1829 	 * If offset is in memory, move it into the buffer provided and return.
1830 	 */
1831 	if (filepos >= file_start && filepos+len <= file_end) {
1832 		grub_memmove(buf, file_buf + filepos - file_start, len);
1833 		filepos += len;
1834 		return (len);
1835 	}
1836 
1837 	blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1838 
1839 	/*
1840 	 * Entire Dnode is too big to fit into the space available.  We
1841 	 * will need to read it in chunks.  This could be optimized to
1842 	 * read in as large a chunk as there is space available, but for
1843 	 * now, this only reads in one data block at a time.
1844 	 */
1845 	length = len;
1846 	while (length) {
1847 		/*
1848 		 * Find requested blkid and the offset within that block.
1849 		 */
1850 		uint64_t blkid = filepos / blksz;
1851 
1852 		if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
1853 			return (0);
1854 
1855 		file_start = blkid * blksz;
1856 		file_end = file_start + blksz;
1857 
1858 		movesize = MIN(length, file_end - filepos);
1859 
1860 		grub_memmove(buf, file_buf + filepos - file_start,
1861 		    movesize);
1862 		buf += movesize;
1863 		length -= movesize;
1864 		filepos += movesize;
1865 	}
1866 
1867 	return (len);
1868 }
1869 
1870 /*
1871  * No-Op
1872  */
1873 int
1874 zfs_embed(int *start_sector, int needed_sectors)
1875 {
1876 	return (1);
1877 }
1878 
1879 #endif /* FSYS_ZFS */
1880