Home | History | Annotate | Download | only in stage2
      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 	{"noop", 0},
     68 	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
     69 	{"off", 0},
     70 	{"lzjb", lzjb_decompress}	/* ZIO_COMPRESS_LZJB */
     71 };
     72 
     73 /*
     74  * Our own version of bcmp().
     75  */
     76 static int
     77 zfs_bcmp(const void *s1, const void *s2, size_t n)
     78 {
     79 	const uchar_t *ps1 = s1;
     80 	const uchar_t *ps2 = s2;
     81 
     82 	if (s1 != s2 && n != 0) {
     83 		do {
     84 			if (*ps1++ != *ps2++)
     85 				return (1);
     86 		} while (--n != 0);
     87 	}
     88 
     89 	return (0);
     90 }
     91 
     92 /*
     93  * Our own version of log2().  Same thing as highbit()-1.
     94  */
     95 static int
     96 zfs_log2(uint64_t num)
     97 {
     98 	int i = 0;
     99 
    100 	while (num > 1) {
    101 		i++;
    102 		num = num >> 1;
    103 	}
    104 
    105 	return (i);
    106 }
    107 
    108 /* Checksum Functions */
    109 static void
    110 zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
    111 {
    112 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
    113 }
    114 
    115 /* Checksum Table and Values */
    116 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
    117 	NULL,			NULL,			0, 0,	"inherit",
    118 	NULL,			NULL,			0, 0,	"on",
    119 	zio_checksum_off,	zio_checksum_off,	0, 0,	"off",
    120 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 1,	"label",
    121 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 1,	"gang_header",
    122 	fletcher_2_native,	fletcher_2_byteswap,	0, 1,	"zilog",
    123 	fletcher_2_native,	fletcher_2_byteswap,	0, 0,	"fletcher2",
    124 	fletcher_4_native,	fletcher_4_byteswap,	1, 0,	"fletcher4",
    125 	zio_checksum_SHA256,	zio_checksum_SHA256,	1, 0,	"SHA256",
    126 };
    127 
    128 /*
    129  * zio_checksum_verify: Provides support for checksum verification.
    130  *
    131  * Fletcher2, Fletcher4, and SHA256 are supported.
    132  *
    133  * Return:
    134  * 	-1 = Failure
    135  *	 0 = Success
    136  */
    137 static int
    138 zio_checksum_verify(blkptr_t *bp, char *data, int size)
    139 {
    140 	zio_cksum_t zc = bp->blk_cksum;
    141 	uint32_t checksum = BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER :
    142 	    BP_GET_CHECKSUM(bp);
    143 	int byteswap = BP_SHOULD_BYTESWAP(bp);
    144 	zio_block_tail_t *zbt = (zio_block_tail_t *)(data + size) - 1;
    145 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
    146 	zio_cksum_t actual_cksum, expected_cksum;
    147 
    148 	/* byteswap is not supported */
    149 	if (byteswap)
    150 		return (-1);
    151 
    152 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
    153 		return (-1);
    154 
    155 	if (ci->ci_zbt) {
    156 		if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
    157 			/*
    158 			 * 'gang blocks' is not supported.
    159 			 */
    160 			return (-1);
    161 		}
    162 
    163 		if (zbt->zbt_magic == BSWAP_64(ZBT_MAGIC)) {
    164 			/* byte swapping is not supported */
    165 			return (-1);
    166 		} else {
    167 			expected_cksum = zbt->zbt_cksum;
    168 			zbt->zbt_cksum = zc;
    169 			ci->ci_func[0](data, size, &actual_cksum);
    170 			zbt->zbt_cksum = expected_cksum;
    171 		}
    172 		zc = expected_cksum;
    173 
    174 	} else {
    175 		if (BP_IS_GANG(bp))
    176 			return (-1);
    177 		ci->ci_func[byteswap](data, size, &actual_cksum);
    178 	}
    179 
    180 	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
    181 	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
    182 	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
    183 	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
    184 		return (-1);
    185 
    186 	return (0);
    187 }
    188 
    189 /*
    190  * vdev_label_offset takes "offset" (the offset within a vdev_label) and
    191  * returns its physical disk offset (starting from the beginning of the vdev).
    192  *
    193  * Input:
    194  *	psize	: Physical size of this vdev
    195  *      l	: Label Number (0-3)
    196  *	offset	: The offset with a vdev_label in which we want the physical
    197  *		  address
    198  * Return:
    199  * 	Success : physical disk offset
    200  * 	Failure : errnum = ERR_BAD_ARGUMENT, return value is meaningless
    201  */
    202 static uint64_t
    203 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
    204 {
    205 	/* XXX Need to add back label support! */
    206 	if (l >= VDEV_LABELS/2 || offset > sizeof (vdev_label_t)) {
    207 		errnum = ERR_BAD_ARGUMENT;
    208 		return (0);
    209 	}
    210 
    211 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
    212 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
    213 
    214 }
    215 
    216 /*
    217  * vdev_uberblock_compare takes two uberblock structures and returns an integer
    218  * indicating the more recent of the two.
    219  * 	Return Value = 1 if ub2 is more recent
    220  * 	Return Value = -1 if ub1 is more recent
    221  * The most recent uberblock is determined using its transaction number and
    222  * timestamp.  The uberblock with the highest transaction number is
    223  * considered "newer".  If the transaction numbers of the two blocks match, the
    224  * timestamps are compared to determine the "newer" of the two.
    225  */
    226 static int
    227 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
    228 {
    229 	if (ub1->ub_txg < ub2->ub_txg)
    230 		return (-1);
    231 	if (ub1->ub_txg > ub2->ub_txg)
    232 		return (1);
    233 
    234 	if (ub1->ub_timestamp < ub2->ub_timestamp)
    235 		return (-1);
    236 	if (ub1->ub_timestamp > ub2->ub_timestamp)
    237 		return (1);
    238 
    239 	return (0);
    240 }
    241 
    242 /*
    243  * Three pieces of information are needed to verify an uberblock: the magic
    244  * number, the version number, and the checksum.
    245  *
    246  * Currently Implemented: version number, magic number
    247  * Need to Implement: checksum
    248  *
    249  * Return:
    250  *     0 - Success
    251  *    -1 - Failure
    252  */
    253 static int
    254 uberblock_verify(uberblock_phys_t *ub, int offset)
    255 {
    256 
    257 	uberblock_t *uber = &ub->ubp_uberblock;
    258 	blkptr_t bp;
    259 
    260 	BP_ZERO(&bp);
    261 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
    262 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
    263 	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
    264 
    265 	if (zio_checksum_verify(&bp, (char *)ub, UBERBLOCK_SIZE) != 0)
    266 		return (-1);
    267 
    268 	if (uber->ub_magic == UBERBLOCK_MAGIC &&
    269 	    uber->ub_version >= SPA_VERSION_1 &&
    270 	    uber->ub_version <= SPA_VERSION)
    271 		return (0);
    272 
    273 	return (-1);
    274 }
    275 
    276 /*
    277  * Find the best uberblock.
    278  * Return:
    279  *    Success - Pointer to the best uberblock.
    280  *    Failure - NULL
    281  */
    282 static uberblock_phys_t *
    283 find_bestub(uberblock_phys_t *ub_array, int label)
    284 {
    285 	uberblock_phys_t *ubbest = NULL;
    286 	int i, offset;
    287 
    288 	for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++) {
    289 		offset = vdev_label_offset(0, label, VDEV_UBERBLOCK_OFFSET(i));
    290 		if (errnum == ERR_BAD_ARGUMENT)
    291 			return (NULL);
    292 		if (uberblock_verify(&ub_array[i], offset) == 0) {
    293 			if (ubbest == NULL) {
    294 				ubbest = &ub_array[i];
    295 			} else if (vdev_uberblock_compare(
    296 			    &(ub_array[i].ubp_uberblock),
    297 			    &(ubbest->ubp_uberblock)) > 0) {
    298 				ubbest = &ub_array[i];
    299 			}
    300 		}
    301 	}
    302 
    303 	return (ubbest);
    304 }
    305 
    306 /*
    307  * Read in a block and put its uncompressed data in buf.
    308  *
    309  * Return:
    310  *	0 - success
    311  *	errnum - failure
    312  */
    313 static int
    314 zio_read(blkptr_t *bp, void *buf, char *stack)
    315 {
    316 	uint64_t offset, sector;
    317 	int psize, lsize;
    318 	int i, comp, cksum;
    319 
    320 	psize = BP_GET_PSIZE(bp);
    321 	lsize = BP_GET_LSIZE(bp);
    322 	comp = BP_GET_COMPRESS(bp);
    323 	cksum = BP_GET_CHECKSUM(bp);
    324 
    325 	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
    326 	    comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL)
    327 		return (ERR_FSYS_CORRUPT);
    328 
    329 	/* pick a good dva from the block pointer */
    330 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
    331 
    332 		if (bp->blk_dva[i].dva_word[0] == 0 &&
    333 		    bp->blk_dva[i].dva_word[1] == 0)
    334 			continue;
    335 
    336 		/* read in a block */
    337 		offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
    338 		sector =  DVA_OFFSET_TO_PHYS_SECTOR(offset);
    339 
    340 		if (comp != ZIO_COMPRESS_OFF) {
    341 
    342 			if (devread(sector, 0, psize, stack) == 0)
    343 				continue;
    344 			if (zio_checksum_verify(bp, stack, psize) != 0)
    345 				continue;
    346 			decomp_table[comp].decomp_func(stack, buf, psize,
    347 			    lsize);
    348 		} else {
    349 			if (devread(sector, 0, psize, buf) == 0)
    350 				continue;
    351 			if (zio_checksum_verify(bp, buf, psize) != 0)
    352 				continue;
    353 		}
    354 		return (0);
    355 	}
    356 
    357 	return (ERR_FSYS_CORRUPT);
    358 }
    359 
    360 /*
    361  * Get the block from a block id.
    362  * push the block onto the stack.
    363  *
    364  * Return:
    365  * 	0 - success
    366  * 	errnum - failure
    367  */
    368 static int
    369 dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
    370 {
    371 	int idx, level;
    372 	blkptr_t *bp_array = dn->dn_blkptr;
    373 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
    374 	blkptr_t *bp, *tmpbuf;
    375 
    376 	bp = (blkptr_t *)stack;
    377 	stack += sizeof (blkptr_t);
    378 
    379 	tmpbuf = (blkptr_t *)stack;
    380 	stack += 1<<dn->dn_indblkshift;
    381 
    382 	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
    383 		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
    384 		*bp = bp_array[idx];
    385 		if (level == 0)
    386 			tmpbuf = buf;
    387 		if (BP_IS_HOLE(bp)) {
    388 			grub_memset(buf, 0,
    389 			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    390 			break;
    391 		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
    392 			return (errnum);
    393 		}
    394 
    395 		bp_array = tmpbuf;
    396 	}
    397 
    398 	return (0);
    399 }
    400 
    401 /*
    402  * mzap_lookup: Looks up property described by "name" and returns the value
    403  * in "value".
    404  *
    405  * Return:
    406  *	0 - success
    407  *	errnum - failure
    408  */
    409 static int
    410 mzap_lookup(mzap_phys_t *zapobj, int objsize, char *name,
    411 	uint64_t *value)
    412 {
    413 	int i, chunks;
    414 	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
    415 
    416 	chunks = objsize/MZAP_ENT_LEN - 1;
    417 	for (i = 0; i < chunks; i++) {
    418 		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
    419 			*value = mzap_ent[i].mze_value;
    420 			return (0);
    421 		}
    422 	}
    423 
    424 	return (ERR_FSYS_CORRUPT);
    425 }
    426 
    427 static uint64_t
    428 zap_hash(uint64_t salt, const char *name)
    429 {
    430 	static uint64_t table[256];
    431 	const uint8_t *cp;
    432 	uint8_t c;
    433 	uint64_t crc = salt;
    434 
    435 	if (table[128] == 0) {
    436 		uint64_t *ct;
    437 		int i, j;
    438 		for (i = 0; i < 256; i++) {
    439 			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
    440 				*ct = (*ct >> 1) ^ (-(*ct & 1) &
    441 				    ZFS_CRC64_POLY);
    442 		}
    443 	}
    444 
    445 	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
    446 		errnum = ERR_FSYS_CORRUPT;
    447 		return (0);
    448 	}
    449 
    450 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
    451 		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
    452 
    453 	/*
    454 	 * Only use 28 bits, since we need 4 bits in the cookie for the
    455 	 * collision differentiator.  We MUST use the high bits, since
    456 	 * those are the onces that we first pay attention to when
    457 	 * chosing the bucket.
    458 	 */
    459 	crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
    460 
    461 	return (crc);
    462 }
    463 
    464 /*
    465  * Only to be used on 8-bit arrays.
    466  * array_len is actual len in bytes (not encoded le_value_length).
    467  * buf is null-terminated.
    468  */
    469 static int
    470 zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
    471     int array_len, const char *buf)
    472 {
    473 	int bseen = 0;
    474 
    475 	while (bseen < array_len) {
    476 		struct zap_leaf_array *la =
    477 		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
    478 		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
    479 
    480 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
    481 			return (0);
    482 
    483 		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
    484 			break;
    485 		chunk = la->la_next;
    486 		bseen += toread;
    487 	}
    488 	return (bseen == array_len);
    489 }
    490 
    491 /*
    492  * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
    493  * value for the property "name".
    494  *
    495  * Return:
    496  *	0 - success
    497  *	errnum - failure
    498  */
    499 static int
    500 zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
    501     const char *name, uint64_t *value)
    502 {
    503 	uint16_t chunk;
    504 	struct zap_leaf_entry *le;
    505 
    506 	/* Verify if this is a valid leaf block */
    507 	if (l->l_hdr.lh_block_type != ZBT_LEAF)
    508 		return (ERR_FSYS_CORRUPT);
    509 	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
    510 		return (ERR_FSYS_CORRUPT);
    511 
    512 	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
    513 	    chunk != CHAIN_END; chunk = le->le_next) {
    514 
    515 		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
    516 			return (ERR_FSYS_CORRUPT);
    517 
    518 		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
    519 
    520 		/* Verify the chunk entry */
    521 		if (le->le_type != ZAP_CHUNK_ENTRY)
    522 			return (ERR_FSYS_CORRUPT);
    523 
    524 		if (le->le_hash != h)
    525 			continue;
    526 
    527 		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
    528 		    le->le_name_length, name)) {
    529 
    530 			struct zap_leaf_array *la;
    531 			uint8_t *ip;
    532 
    533 			if (le->le_int_size != 8 || le->le_value_length != 1)
    534 				return (ERR_FSYS_CORRUPT);
    535 
    536 			/* get the uint64_t property value */
    537 			la = &ZAP_LEAF_CHUNK(l, blksft,
    538 			    le->le_value_chunk).l_array;
    539 			ip = la->la_array;
    540 
    541 			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
    542 			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
    543 			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
    544 			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
    545 
    546 			return (0);
    547 		}
    548 	}
    549 
    550 	return (ERR_FSYS_CORRUPT);
    551 }
    552 
    553 /*
    554  * Fat ZAP lookup
    555  *
    556  * Return:
    557  *	0 - success
    558  *	errnum - failure
    559  */
    560 static int
    561 fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
    562     char *name, uint64_t *value, char *stack)
    563 {
    564 	zap_leaf_phys_t *l;
    565 	uint64_t hash, idx, blkid;
    566 	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
    567 
    568 	/* Verify if this is a fat zap header block */
    569 	if (zap->zap_magic != (uint64_t)ZAP_MAGIC)
    570 		return (ERR_FSYS_CORRUPT);
    571 
    572 	hash = zap_hash(zap->zap_salt, name);
    573 	if (errnum)
    574 		return (errnum);
    575 
    576 	/* get block id from index */
    577 	if (zap->zap_ptrtbl.zt_numblks != 0) {
    578 		/* external pointer tables not supported */
    579 		return (ERR_FSYS_CORRUPT);
    580 	}
    581 	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
    582 	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
    583 
    584 	/* Get the leaf block */
    585 	l = (zap_leaf_phys_t *)stack;
    586 	stack += 1<<blksft;
    587 	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
    588 		return (errnum);
    589 
    590 	return (zap_leaf_lookup(l, blksft, hash, name, value));
    591 }
    592 
    593 /*
    594  * Read in the data of a zap object and find the value for a matching
    595  * property name.
    596  *
    597  * Return:
    598  *	0 - success
    599  *	errnum - failure
    600  */
    601 static int
    602 zap_lookup(dnode_phys_t *zap_dnode, char *name, uint64_t *val, char *stack)
    603 {
    604 	uint64_t block_type;
    605 	int size;
    606 	void *zapbuf;
    607 
    608 	/* Read in the first block of the zap object data. */
    609 	zapbuf = stack;
    610 	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
    611 	stack += size;
    612 	if (errnum = dmu_read(zap_dnode, 0, zapbuf, stack))
    613 		return (errnum);
    614 
    615 	block_type = *((uint64_t *)zapbuf);
    616 
    617 	if (block_type == ZBT_MICRO) {
    618 		return (mzap_lookup(zapbuf, size, name, val));
    619 	} else if (block_type == ZBT_HEADER) {
    620 		/* this is a fat zap */
    621 		return (fzap_lookup(zap_dnode, zapbuf, name,
    622 		    val, stack));
    623 	}
    624 
    625 	return (ERR_FSYS_CORRUPT);
    626 }
    627 
    628 /*
    629  * Get the dnode of an object number from the metadnode of an object set.
    630  *
    631  * Input
    632  *	mdn - metadnode to get the object dnode
    633  *	objnum - object number for the object dnode
    634  *	buf - data buffer that holds the returning dnode
    635  *	stack - scratch area
    636  *
    637  * Return:
    638  *	0 - success
    639  *	errnum - failure
    640  */
    641 static int
    642 dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
    643 	char *stack)
    644 {
    645 	uint64_t blkid, blksz; /* the block id this object dnode is in */
    646 	int epbs; /* shift of number of dnodes in a block */
    647 	int idx; /* index within a block */
    648 	dnode_phys_t *dnbuf;
    649 
    650 	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
    651 	epbs = zfs_log2(blksz) - DNODE_SHIFT;
    652 	blkid = objnum >> epbs;
    653 	idx = objnum & ((1<<epbs)-1);
    654 
    655 	if (dnode_buf != NULL && dnode_mdn == mdn &&
    656 	    objnum >= dnode_start && objnum < dnode_end) {
    657 		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
    658 		VERIFY_DN_TYPE(buf, type);
    659 		return (0);
    660 	}
    661 
    662 	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
    663 		dnbuf = dnode_buf;
    664 		dnode_mdn = mdn;
    665 		dnode_start = blkid << epbs;
    666 		dnode_end = (blkid + 1) << epbs;
    667 	} else {
    668 		dnbuf = (dnode_phys_t *)stack;
    669 		stack += blksz;
    670 	}
    671 
    672 	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
    673 		return (errnum);
    674 
    675 	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
    676 	VERIFY_DN_TYPE(buf, type);
    677 
    678 	return (0);
    679 }
    680 
    681 /*
    682  * Check if this is a special file that resides at the top
    683  * dataset of the pool. Currently this is the GRUB menu,
    684  * boot signature and boot signature backup.
    685  * str starts with '/'.
    686  */
    687 static int
    688 is_top_dataset_file(char *str)
    689 {
    690 	char *tptr;
    691 
    692 	if ((tptr = grub_strstr(str, "menu.lst")) &&
    693 	    (tptr[8] == '\0' || tptr[8] == ' ') &&
    694 	    *(tptr-1) == '/')
    695 		return (1);
    696 
    697 	if (grub_strncmp(str, BOOTSIGN_DIR"/",
    698 	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
    699 		return (1);
    700 
    701 	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
    702 		return (1);
    703 
    704 	return (0);
    705 }
    706 
    707 /*
    708  * Get the file dnode for a given file name where mdn is the meta dnode
    709  * for this ZFS object set. When found, place the file dnode in dn.
    710  * The 'path' argument will be mangled.
    711  *
    712  * Return:
    713  *	0 - success
    714  *	errnum - failure
    715  */
    716 static int
    717 dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
    718     char *stack)
    719 {
    720 	uint64_t objnum, version;
    721 	char *cname, ch;
    722 
    723 	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
    724 	    dn, stack))
    725 		return (errnum);
    726 
    727 	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
    728 		return (errnum);
    729 	if (version > ZPL_VERSION)
    730 		return (-1);
    731 
    732 	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
    733 		return (errnum);
    734 
    735 	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
    736 	    dn, stack))
    737 		return (errnum);
    738 
    739 	/* skip leading slashes */
    740 	while (*path == '/')
    741 		path++;
    742 
    743 	while (*path && !isspace(*path)) {
    744 
    745 		/* get the next component name */
    746 		cname = path;
    747 		while (*path && !isspace(*path) && *path != '/')
    748 			path++;
    749 		ch = *path;
    750 		*path = 0;   /* ensure null termination */
    751 
    752 		if (errnum = zap_lookup(dn, cname, &objnum, stack))
    753 			return (errnum);
    754 
    755 		objnum = ZFS_DIRENT_OBJ(objnum);
    756 		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
    757 			return (errnum);
    758 
    759 		*path = ch;
    760 		while (*path == '/')
    761 			path++;
    762 	}
    763 
    764 	/* We found the dnode for this file. Verify if it is a plain file. */
    765 	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
    766 
    767 	return (0);
    768 }
    769 
    770 /*
    771  * Get the default 'bootfs' property value from the rootpool.
    772  *
    773  * Return:
    774  *	0 - success
    775  *	errnum -failure
    776  */
    777 static int
    778 get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
    779 {
    780 	uint64_t objnum = 0;
    781 	dnode_phys_t *dn = (dnode_phys_t *)stack;
    782 	stack += DNODE_SIZE;
    783 
    784 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
    785 	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
    786 		return (errnum);
    787 
    788 	/*
    789 	 * find the object number for 'pool_props', and get the dnode
    790 	 * of the 'pool_props'.
    791 	 */
    792 	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
    793 		return (ERR_FILESYSTEM_NOT_FOUND);
    794 
    795 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
    796 		return (errnum);
    797 
    798 	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
    799 		return (ERR_FILESYSTEM_NOT_FOUND);
    800 
    801 	if (!objnum)
    802 		return (ERR_FILESYSTEM_NOT_FOUND);
    803 
    804 	*obj = objnum;
    805 	return (0);
    806 }
    807 
    808 /*
    809  * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
    810  * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
    811  * of pool/rootfs.
    812  *
    813  * If no fsname and no obj are given, return the DSL_DIR metadnode.
    814  * If fsname is given, return its metadnode and its matching object number.
    815  * If only obj is given, return the metadnode for this object number.
    816  *
    817  * Return:
    818  *	0 - success
    819  *	errnum - failure
    820  */
    821 static int
    822 get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
    823     dnode_phys_t *mdn, char *stack)
    824 {
    825 	uint64_t objnum, headobj;
    826 	char *cname, ch;
    827 	blkptr_t *bp;
    828 	objset_phys_t *osp;
    829 
    830 	if (fsname == NULL && obj) {
    831 		headobj = *obj;
    832 		goto skip;
    833 	}
    834 
    835 	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
    836 	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
    837 		return (errnum);
    838 
    839 	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
    840 	    stack))
    841 		return (errnum);
    842 
    843 	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
    844 		return (errnum);
    845 
    846 	if (fsname == NULL) {
    847 		headobj =
    848 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
    849 		goto skip;
    850 	}
    851 
    852 	/* take out the pool name */
    853 	while (*fsname && !isspace(*fsname) && *fsname != '/')
    854 		fsname++;
    855 
    856 	while (*fsname && !isspace(*fsname)) {
    857 		uint64_t childobj;
    858 
    859 		while (*fsname == '/')
    860 			fsname++;
    861 
    862 		cname = fsname;
    863 		while (*fsname && !isspace(*fsname) && *fsname != '/')
    864 			fsname++;
    865 		ch = *fsname;
    866 		*fsname = 0;
    867 
    868 		childobj =
    869 		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
    870 		if (errnum = dnode_get(mosmdn, childobj,
    871 		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
    872 			return (errnum);
    873 
    874 		if (zap_lookup(mdn, cname, &objnum, stack))
    875 			return (ERR_FILESYSTEM_NOT_FOUND);
    876 
    877 		if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
    878 		    mdn, stack))
    879 			return (errnum);
    880 
    881 		*fsname = ch;
    882 	}
    883 	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
    884 	if (obj)
    885 		*obj = headobj;
    886 
    887 skip:
    888 	if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
    889 		return (errnum);
    890 
    891 	/* TODO: Add snapshot support here - for fsname=snapshot-name */
    892 
    893 	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
    894 	osp = (objset_phys_t *)stack;
    895 	stack += sizeof (objset_phys_t);
    896 	if (errnum = zio_read(bp, osp, stack))
    897 		return (errnum);
    898 
    899 	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
    900 
    901 	return (0);
    902 }
    903 
    904 /*
    905  * For a given XDR packed nvlist, verify the first 4 bytes and move on.
    906  *
    907  * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
    908  *
    909  *      encoding method/host endian     (4 bytes)
    910  *      nvl_version                     (4 bytes)
    911  *      nvl_nvflag                      (4 bytes)
    912  *	encoded nvpairs:
    913  *		encoded size of the nvpair      (4 bytes)
    914  *		decoded size of the nvpair      (4 bytes)
    915  *		name string size                (4 bytes)
    916  *		name string data                (sizeof(NV_ALIGN4(string))
    917  *		data type                       (4 bytes)
    918  *		# of elements in the nvpair     (4 bytes)
    919  *		data
    920  *      2 zero's for the last nvpair
    921  *		(end of the entire list)	(8 bytes)
    922  *
    923  * Return:
    924  *	0 - success
    925  *	1 - failure
    926  */
    927 static int
    928 nvlist_unpack(char *nvlist, char **out)
    929 {
    930 	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
    931 	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
    932 		return (1);
    933 
    934 	nvlist += 4;
    935 	*out = nvlist;
    936 	return (0);
    937 }
    938 
    939 static char *
    940 nvlist_array(char *nvlist, int index)
    941 {
    942 	int i, encode_size;
    943 
    944 	for (i = 0; i < index; i++) {
    945 		/* skip the header, nvl_version, and nvl_nvflag */
    946 		nvlist = nvlist + 4 * 2;
    947 
    948 		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
    949 			nvlist += encode_size; /* goto the next nvpair */
    950 
    951 		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
    952 	}
    953 
    954 	return (nvlist);
    955 }
    956 
    957 static int
    958 nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
    959     int *nelmp)
    960 {
    961 	int name_len, type, slen, encode_size;
    962 	char *nvpair, *nvp_name, *strval = val;
    963 	uint64_t *intval = val;
    964 
    965 	/* skip the header, nvl_version, and nvl_nvflag */
    966 	nvlist = nvlist + 4 * 2;
    967 
    968 	/*
    969 	 * Loop thru the nvpair list
    970 	 * The XDR representation of an integer is in big-endian byte order.
    971 	 */
    972 	while (encode_size = BSWAP_32(*(uint32_t *)nvlist))  {
    973 
    974 		nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
    975 
    976 		name_len = BSWAP_32(*(uint32_t *)nvpair);
    977 		nvpair += 4;
    978 
    979 		nvp_name = nvpair;
    980 		nvpair = nvpair + ((name_len + 3) & ~3); /* align */
    981 
    982 		type = BSWAP_32(*(uint32_t *)nvpair);
    983 		nvpair += 4;
    984 
    985 		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
    986 		    type == valtype) {
    987 			int nelm;
    988 
    989 			if ((nelm = BSWAP_32(*(uint32_t *)nvpair)) < 1)
    990 				return (1);
    991 			nvpair += 4;
    992 
    993 			switch (valtype) {
    994 			case DATA_TYPE_STRING:
    995 				slen = BSWAP_32(*(uint32_t *)nvpair);
    996 				nvpair += 4;
    997 				grub_memmove(strval, nvpair, slen);
    998 				strval[slen] = '\0';
    999 				return (0);
   1000 
   1001 			case DATA_TYPE_UINT64:
   1002 				*intval = BSWAP_64(*(uint64_t *)nvpair);
   1003 				return (0);
   1004 
   1005 			case DATA_TYPE_NVLIST:
   1006 				*(void **)val = (void *)nvpair;
   1007 				return (0);
   1008 
   1009 			case DATA_TYPE_NVLIST_ARRAY:
   1010 				*(void **)val = (void *)nvpair;
   1011 				if (nelmp)
   1012 					*nelmp = nelm;
   1013 				return (0);
   1014 			}
   1015 		}
   1016 
   1017 		nvlist += encode_size; /* goto the next nvpair */
   1018 	}
   1019 
   1020 	return (1);
   1021 }
   1022 
   1023 /*
   1024  * Check if this vdev is online and is in a good state.
   1025  */
   1026 static int
   1027 vdev_validate(char *nv)
   1028 {
   1029 	uint64_t ival;
   1030 
   1031 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
   1032 	    DATA_TYPE_UINT64, NULL) == 0 ||
   1033 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
   1034 	    DATA_TYPE_UINT64, NULL) == 0 ||
   1035 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_DEGRADED, &ival,
   1036 	    DATA_TYPE_UINT64, NULL) == 0 ||
   1037 	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
   1038 	    DATA_TYPE_UINT64, NULL) == 0)
   1039 		return (ERR_DEV_VALUES);
   1040 
   1041 	return (0);
   1042 }
   1043 
   1044 /*
   1045  * Get a list of valid vdev pathname from the boot device.
   1046  * The caller should already allocate MAXNAMELEN memory for bootpath.
   1047  */
   1048 static int
   1049 vdev_get_bootpath(char *nv, char *bootpath)
   1050 {
   1051 	char type[16];
   1052 
   1053 	bootpath[0] = '\0';
   1054 	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
   1055 	    NULL))
   1056 		return (ERR_FSYS_CORRUPT);
   1057 
   1058 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
   1059 		if (vdev_validate(nv) != 0 ||
   1060 		    nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH, bootpath,
   1061 		    DATA_TYPE_STRING, NULL) != 0)
   1062 			return (ERR_NO_BOOTPATH);
   1063 
   1064 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
   1065 		int nelm, i;
   1066 		char *child;
   1067 
   1068 		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
   1069 		    DATA_TYPE_NVLIST_ARRAY, &nelm))
   1070 			return (ERR_FSYS_CORRUPT);
   1071 
   1072 		for (i = 0; i < nelm; i++) {
   1073 			char tmp_path[MAXNAMELEN];
   1074 			char *child_i;
   1075 
   1076 			child_i = nvlist_array(child, i);
   1077 			if (vdev_validate(child_i) != 0)
   1078 				continue;
   1079 
   1080 			if (nvlist_lookup_value(child_i, ZPOOL_CONFIG_PHYS_PATH,
   1081 			    tmp_path, DATA_TYPE_STRING, NULL) != 0)
   1082 				return (ERR_NO_BOOTPATH);
   1083 
   1084 			if ((strlen(bootpath) + strlen(tmp_path)) > MAXNAMELEN)
   1085 				return (ERR_WONT_FIT);
   1086 
   1087 			if (strlen(bootpath) == 0)
   1088 				sprintf(bootpath, "%s", tmp_path);
   1089 			else
   1090 				sprintf(bootpath, "%s %s", bootpath, tmp_path);
   1091 		}
   1092 	}
   1093 
   1094 	return (strlen(bootpath) > 0 ? 0 : ERR_NO_BOOTPATH);
   1095 }
   1096 
   1097 /*
   1098  * Check the disk label information and retrieve needed vdev name-value pairs.
   1099  *
   1100  * Return:
   1101  *	0 - success
   1102  *	ERR_* - failure
   1103  */
   1104 static int
   1105 check_pool_label(int label, char *stack)
   1106 {
   1107 	vdev_phys_t *vdev;
   1108