1b1b8ab3lling/*
2b1b8ab3lling *  GRUB  --  GRand Unified Bootloader
3b1b8ab3lling *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
4b1b8ab3lling *
5b1b8ab3lling *  This program is free software; you can redistribute it and/or modify
6b1b8ab3lling *  it under the terms of the GNU General Public License as published by
7b1b8ab3lling *  the Free Software Foundation; either version 2 of the License, or
8b1b8ab3lling *  (at your option) any later version.
9b1b8ab3lling *
10b1b8ab3lling *  This program is distributed in the hope that it will be useful,
11b1b8ab3lling *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12b1b8ab3lling *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13b1b8ab3lling *  GNU General Public License for more details.
14b1b8ab3lling *
15b1b8ab3lling *  You should have received a copy of the GNU General Public License
16b1b8ab3lling *  along with this program; if not, write to the Free Software
17b1b8ab3lling *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18b1b8ab3lling */
19ad135b5Christopher Siden
20b1b8ab3lling/*
216e1f5caNeil Perrin * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
22b1b8ab3lling * Use is subject to license terms.
23b1b8ab3lling */
24b1b8ab3lling
25b1b8ab3lling/*
26d94527bDan Kimmel * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
27a6f561bSašo Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
29ad135b5Christopher Siden */
30ad135b5Christopher Siden
31ad135b5Christopher Siden/*
32b1b8ab3lling * The zfs plug-in routines for GRUB are:
33b1b8ab3lling *
34b1b8ab3lling * zfs_mount() - locates a valid uberblock of the root pool and reads
35b1b8ab3lling *		in its MOS at the memory address MOS.
36b1b8ab3lling *
37b1b8ab3lling * zfs_open() - locates a plain file object by following the MOS
38b1b8ab3lling *		and places its dnode at the memory address DNODE.
39b1b8ab3lling *
40b1b8ab3lling * zfs_read() - read in the data blocks pointed by the DNODE.
41b1b8ab3lling *
42b1b8ab3lling * ZFS_SCRATCH is used as a working area.
43b1b8ab3lling *
44b1b8ab3lling * (memory addr)   MOS      DNODE	ZFS_SCRATCH
45b1b8ab3lling *		    |         |          |
46b1b8ab3lling *	    +-------V---------V----------V---------------+
47b1b8ab3lling *   memory |       | dnode   | dnode    |  scratch      |
48b1b8ab3lling *	    |       | 512B    | 512B     |  area         |
49b1b8ab3lling *	    +--------------------------------------------+
50b1b8ab3lling */
51b1b8ab3lling
52b1b8ab3lling#ifdef	FSYS_ZFS
53b1b8ab3lling
54b1b8ab3lling#include "shared.h"
55b1b8ab3lling#include "filesys.h"
56b1b8ab3lling#include "fsys_zfs.h"
57b1b8ab3lling
58b1b8ab3lling/* cache for a file block of the currently zfs_open()-ed file */
59b1b8ab3llingstatic void *file_buf = NULL;
60b1b8ab3llingstatic uint64_t file_start = 0;
61b1b8ab3llingstatic uint64_t file_end = 0;
62b1b8ab3lling
63b1b8ab3lling/* cache for a dnode block */
64b1b8ab3llingstatic dnode_phys_t *dnode_buf = NULL;
65b1b8ab3llingstatic dnode_phys_t *dnode_mdn = NULL;
66b1b8ab3llingstatic uint64_t dnode_start = 0;
67b1b8ab3llingstatic uint64_t dnode_end = 0;
68b1b8ab3lling
69e23347bEric Taylorstatic uint64_t pool_guid = 0;
70051aabetaylorstatic uberblock_t current_uberblock;
71b1b8ab3llingstatic char *stackbase;
72b1b8ab3lling
73b1b8ab3llingdecomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
74b1b8ab3lling{
7515e6edfgw	{"inherit", 0},			/* ZIO_COMPRESS_INHERIT */
76b1b8ab3lling	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
7715e6edfgw	{"off", 0},			/* ZIO_COMPRESS_OFF */
7815e6edfgw	{"lzjb", lzjb_decompress},	/* ZIO_COMPRESS_LZJB */
79a6f561bSašo Kiselkov	{"empty", 0},			/* ZIO_COMPRESS_EMPTY */
80a6f561bSašo Kiselkov	{"gzip-1", 0},			/* ZIO_COMPRESS_GZIP_1 */
81a6f561bSašo Kiselkov	{"gzip-2", 0},			/* ZIO_COMPRESS_GZIP_2 */
82a6f561bSašo Kiselkov	{"gzip-3", 0},			/* ZIO_COMPRESS_GZIP_3 */
83a6f561bSašo Kiselkov	{"gzip-4", 0},			/* ZIO_COMPRESS_GZIP_4 */
84a6f561bSašo Kiselkov	{"gzip-5", 0},			/* ZIO_COMPRESS_GZIP_5 */
85a6f561bSašo Kiselkov	{"gzip-6", 0},			/* ZIO_COMPRESS_GZIP_6 */
86a6f561bSašo Kiselkov	{"gzip-7", 0},			/* ZIO_COMPRESS_GZIP_7 */
87a6f561bSašo Kiselkov	{"gzip-8", 0},			/* ZIO_COMPRESS_GZIP_8 */
88a6f561bSašo Kiselkov	{"gzip-9", 0},			/* ZIO_COMPRESS_GZIP_9 */
89a6f561bSašo Kiselkov	{"zle", 0},			/* ZIO_COMPRESS_ZLE */
90a6f561bSašo Kiselkov	{"lz4", lz4_decompress}		/* ZIO_COMPRESS_LZ4 */
91b1b8ab3lling};
92b1b8ab3lling
93cd9c78dLin Lingstatic int zio_read_data(blkptr_t *bp, void *buf, char *stack);
94cd9c78dLin Ling
95b1b8ab3lling/*
96b1b8ab3lling * Our own version of bcmp().
97b1b8ab3lling */
98b1b8ab3llingstatic int
99b1b8ab3llingzfs_bcmp(const void *s1, const void *s2, size_t n)
100b1b8ab3lling{
101b1b8ab3lling	const uchar_t *ps1 = s1;
102b1b8ab3lling	const uchar_t *ps2 = s2;
103b1b8ab3lling
104b1b8ab3lling	if (s1 != s2 && n != 0) {
105b1b8ab3lling		do {
106b1b8ab3lling			if (*ps1++ != *ps2++)
107b1b8ab3lling				return (1);
108b1b8ab3lling		} while (--n != 0);
109b1b8ab3lling	}
110b1b8ab3lling
111b1b8ab3lling	return (0);
112b1b8ab3lling}
113b1b8ab3lling
114b1b8ab3lling/*
115b1b8ab3lling * Our own version of log2().  Same thing as highbit()-1.
116b1b8ab3lling */
117b1b8ab3llingstatic int
118b1b8ab3llingzfs_log2(uint64_t num)
119b1b8ab3lling{
120b1b8ab3lling	int i = 0;
121b1b8ab3lling
122b1b8ab3lling	while (num > 1) {
123b1b8ab3lling		i++;
124b1b8ab3lling		num = num >> 1;
125b1b8ab3lling	}
126b1b8ab3lling
127b1b8ab3lling	return (i);
128b1b8ab3lling}
129b1b8ab3lling
130b1b8ab3lling/* Checksum Functions */
131b1b8ab3llingstatic void
132b1b8ab3llingzio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
133b1b8ab3lling{
134b1b8ab3lling	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
135b1b8ab3lling}
136b1b8ab3lling
137b1b8ab3lling/* Checksum Table and Values */
138b1b8ab3llingzio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
139ad135b5Christopher Siden	{{NULL,			NULL},			0, 0,	"inherit"},
140ad135b5Christopher Siden	{{NULL,			NULL},			0, 0,	"on"},
141ad135b5Christopher Siden	{{zio_checksum_off,	zio_checksum_off},	0, 0,	"off"},
142ad135b5Christopher Siden	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"label"},
143ad135b5Christopher Siden	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"gang_header"},
144ad135b5Christopher Siden	{{NULL,			NULL},			0, 0,	"zilog"},
145ad135b5Christopher Siden	{{fletcher_2_native,	fletcher_2_byteswap},	0, 0,	"fletcher2"},
146ad135b5Christopher Siden	{{fletcher_4_native,	fletcher_4_byteswap},	1, 0,	"fletcher4"},
147ad135b5Christopher Siden	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 0,	"SHA256"},
148ad135b5Christopher Siden	{{NULL,			NULL},			0, 0,	"zilog2"},
14945818eeMatthew Ahrens	{{zio_checksum_off,	zio_checksum_off},	0, 0,	"noparity"},
15045818eeMatthew Ahrens	{{zio_checksum_SHA512,	NULL},			0, 0,	"SHA512"}
151b1b8ab3lling};
152b1b8ab3lling
153b1b8ab3lling/*
154b1b8ab3lling * zio_checksum_verify: Provides support for checksum verification.
155b1b8ab3lling *
15645818eeMatthew Ahrens * Fletcher2, Fletcher4, SHA-256 and SHA-512/256 are supported.
157b1b8ab3lling *
158b1b8ab3lling * Return:
159b1b8ab3lling * 	-1 = Failure
160b1b8ab3lling *	 0 = Success
161b1b8ab3lling */
162b1b8ab3llingstatic int
163b1b8ab3llingzio_checksum_verify(blkptr_t *bp, char *data, int size)
164b1b8ab3lling{
165b1b8ab3lling	zio_cksum_t zc = bp->blk_cksum;
166cd9c78dLin Ling	uint32_t checksum = BP_GET_CHECKSUM(bp);
167b1b8ab3lling	int byteswap = BP_SHOULD_BYTESWAP(bp);
1686e1f5caNeil Perrin	zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
169b1b8ab3lling	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
170b1b8ab3lling	zio_cksum_t actual_cksum, expected_cksum;
171b1b8ab3lling
1725d7b4d4Matthew Ahrens	if (byteswap) {
1735d7b4d4Matthew Ahrens		grub_printf("byteswap not supported\n");
174b1b8ab3lling		return (-1);
1755d7b4d4Matthew Ahrens	}
176b1b8ab3lling
1775d7b4d4Matthew Ahrens	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL) {
1785d7b4d4Matthew Ahrens		grub_printf("checksum algorithm %u not supported\n", checksum);
179b1b8ab3lling		return (-1);
1805d7b4d4Matthew Ahrens	}
181b1b8ab3lling
1826e1f5caNeil Perrin	if (ci->ci_eck) {
1836e1f5caNeil Perrin		expected_cksum = zec->zec_cksum;
1846e1f5caNeil Perrin		zec->zec_cksum = zc;
185cd9c78dLin Ling		ci->ci_func[0](data, size, &actual_cksum);
1866e1f5caNeil Perrin		zec->zec_cksum = expected_cksum;
187b1b8ab3lling		zc = expected_cksum;
188b1b8ab3lling	} else {
189b1b8ab3lling		ci->ci_func[byteswap](data, size, &actual_cksum);
190b1b8ab3lling	}
191b1b8ab3lling
192b1b8ab3lling	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
193b1b8ab3lling	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
194b1b8ab3lling	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
195b1b8ab3lling	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
196b1b8ab3lling		return (-1);
197b1b8ab3lling
198b1b8ab3lling	return (0);
199b1b8ab3lling}
200b1b8ab3lling
201b1b8ab3lling/*
202e23347bEric Taylor * vdev_label_start returns the physical disk offset (in bytes) of
203e23347bEric Taylor * label "l".
204b1b8ab3lling */
205e7cbe64gwstatic uint64_t
206e23347bEric Taylorvdev_label_start(uint64_t psize, int l)
207b1b8ab3lling{
208e23347bEric Taylor	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
209b1b8ab3lling	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
210b1b8ab3lling}
211b1b8ab3lling
212b1b8ab3lling/*
213b1b8ab3lling * vdev_uberblock_compare takes two uberblock structures and returns an integer
214b1b8ab3lling * indicating the more recent of the two.
215b1b8ab3lling * 	Return Value = 1 if ub2 is more recent
216b1b8ab3lling * 	Return Value = -1 if ub1 is more recent
217b1b8ab3lling * The most recent uberblock is determined using its transaction number and
218b1b8ab3lling * timestamp.  The uberblock with the highest transaction number is
219b1b8ab3lling * considered "newer".  If the transaction numbers of the two blocks match, the
220b1b8ab3lling * timestamps are compared to determine the "newer" of the two.
221b1b8ab3lling */
222b1b8ab3llingstatic int
223b1b8ab3llingvdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
224b1b8ab3lling{
225b1b8ab3lling	if (ub1->ub_txg < ub2->ub_txg)
226b1b8ab3lling		return (-1);
227b1b8ab3lling	if (ub1->ub_txg > ub2->ub_txg)
228b1b8ab3lling		return (1);
229b1b8ab3lling
230b1b8ab3lling	if (ub1->ub_timestamp < ub2->ub_timestamp)
231b1b8ab3lling		return (-1);
232b1b8ab3lling	if (ub1->ub_timestamp > ub2->ub_timestamp)
233b1b8ab3lling		return (1);
234b1b8ab3lling
235b1b8ab3lling	return (0);
236b1b8ab3lling}
237b1b8ab3lling
238b1b8ab3lling/*
239b1b8ab3lling * Three pieces of information are needed to verify an uberblock: the magic
240b1b8ab3lling * number, the version number, and the checksum.
241b1b8ab3lling *
242b1b8ab3lling * Return:
243b1b8ab3lling *     0 - Success
244b1b8ab3lling *    -1 - Failure
245b1b8ab3lling */
246b1b8ab3llingstatic int
24781b2d57Hans Rosenfelduberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
248b1b8ab3lling{
249b1b8ab3lling	blkptr_t bp;
250b1b8ab3lling
251b1b8ab3lling	BP_ZERO(&bp);
252b1b8ab3lling	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
253b1b8ab3lling	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
254b1b8ab3lling	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
255b1b8ab3lling
25681b2d57Hans Rosenfeld	if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
257b1b8ab3lling		return (-1);
258b1b8ab3lling
259b1b8ab3lling	if (uber->ub_magic == UBERBLOCK_MAGIC &&
260ad135b5Christopher Siden	    SPA_VERSION_IS_SUPPORTED(uber->ub_version))
261b1b8ab3lling		return (0);
262b1b8ab3lling
263b1b8ab3lling	return (-1);
264b1b8ab3lling}
265b1b8ab3lling
266b1b8ab3lling/*
267b1b8ab3lling * Find the best uberblock.
268b1b8ab3lling * Return:
269b1b8ab3lling *    Success - Pointer to the best uberblock.
270b1b8ab3lling *    Failure - NULL
271b1b8ab3lling */
27281b2d57Hans Rosenfeldstatic uberblock_t *
27381b2d57Hans Rosenfeldfind_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
274b1b8ab3lling{
27581b2d57Hans Rosenfeld	uberblock_t *ubbest = NULL;
27681b2d57Hans Rosenfeld	uberblock_t *ubnext;
27781b2d57Hans Rosenfeld	uint64_t offset, ub_size;
278e23347bEric Taylor	int i;
279b1b8ab3lling
28081b2d57Hans Rosenfeld	ub_size = VDEV_UBERBLOCK_SIZE(ashift);
28181b2d57Hans Rosenfeld
28281b2d57Hans Rosenfeld	for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
28381b2d57Hans Rosenfeld		ubnext = (uberblock_t *)ub_array;
28481b2d57Hans Rosenfeld		ub_array += ub_size;
285e23347bEric Taylor		offset = (sector << SPA_MINBLOCKSHIFT) +
28681b2d57Hans Rosenfeld		    VDEV_UBERBLOCK_OFFSET(ashift, i);
28781b2d57Hans Rosenfeld
28881b2d57Hans Rosenfeld		if (uberblock_verify(ubnext, ub_size, offset) != 0)
28981b2d57Hans Rosenfeld			continue;
29081b2d57Hans Rosenfeld
29181b2d57Hans Rosenfeld		if (ubbest == NULL ||
29281b2d57Hans Rosenfeld		    vdev_uberblock_compare(ubnext, ubbest) > 0)
29381b2d57Hans Rosenfeld			ubbest = ubnext;
294b1b8ab3lling	}
295b1b8ab3lling
296b1b8ab3lling	return (ubbest);
297b1b8ab3lling}
298b1b8ab3lling
299b1b8ab3lling/*
300cd9c78dLin Ling * Read a block of data based on the gang block address dva,
301cd9c78dLin Ling * and put its data in buf.
302b1b8ab3lling *
303b1b8ab3lling * Return:
304b1b8ab3lling *	0 - success
305cd9c78dLin Ling *	1 - failure
306b1b8ab3lling */
307b1b8ab3llingstatic int
308cd9c78dLin Lingzio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
309b1b8ab3lling{
310cd9c78dLin Ling	zio_gbh_phys_t *zio_gb;
311b1b8ab3lling	uint64_t offset, sector;
312cd9c78dLin Ling	blkptr_t tmpbp;
313cd9c78dLin Ling	int i;
314b1b8ab3lling
315cd9c78dLin Ling	zio_gb = (zio_gbh_phys_t *)stack;
316cd9c78dLin Ling	stack += SPA_GANGBLOCKSIZE;
317cd9c78dLin Ling	offset = DVA_GET_OFFSET(dva);
318ad135b5Christopher Siden	sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
319b1b8ab3lling
320cd9c78dLin Ling	/* read in the gang block header */
321cd9c78dLin Ling	if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
322cd9c78dLin Ling		grub_printf("failed to read in a gang block header\n");
323cd9c78dLin Ling		return (1);
324cd9c78dLin Ling	}
325cd9c78dLin Ling
326cd9c78dLin Ling	/* self checksuming the gang block header */
327cd9c78dLin Ling	BP_ZERO(&tmpbp);
328cd9c78dLin Ling	BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
329cd9c78dLin Ling	BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
330cd9c78dLin Ling	ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
331cd9c78dLin Ling	    DVA_GET_OFFSET(dva), bp->blk_birth, 0);
332cd9c78dLin Ling	if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
333cd9c78dLin Ling		grub_printf("failed to checksum a gang block header\n");
334cd9c78dLin Ling		return (1);
335cd9c78dLin Ling	}
336cd9c78dLin Ling
337cd9c78dLin Ling	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
33843466aaMax Grossman		if (BP_IS_HOLE(&zio_gb->zg_blkptr[i]))
339cd9c78dLin Ling			continue;
340cd9c78dLin Ling
341cd9c78dLin Ling		if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
342cd9c78dLin Ling			return (1);
343cd9c78dLin Ling		buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
344cd9c78dLin Ling	}
345cd9c78dLin Ling
346cd9c78dLin Ling	return (0);
347cd9c78dLin Ling}
348cd9c78dLin Ling
349cd9c78dLin Ling/*
350cd9c78dLin Ling * Read in a block of raw data to buf.
351cd9c78dLin Ling *
352cd9c78dLin Ling * Return:
353cd9c78dLin Ling *	0 - success
354cd9c78dLin Ling *	1 - failure
355cd9c78dLin Ling */
356cd9c78dLin Lingstatic int
357cd9c78dLin Lingzio_read_data(blkptr_t *bp, void *buf, char *stack)
358cd9c78dLin Ling{
359cd9c78dLin Ling	int i, psize;
360cd9c78dLin Ling
361cd9c78dLin Ling	psize = BP_GET_PSIZE(bp);
362ae8180dlling
363b1b8ab3lling	/* pick a good dva from the block pointer */
364b1b8ab3lling	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
365cd9c78dLin Ling		uint64_t offset, sector;
366b1b8ab3lling
367b1b8ab3lling		if (bp->blk_dva[i].dva_word[0] == 0 &&
368b1b8ab3lling		    bp->blk_dva[i].dva_word[1] == 0)
369b1b8ab3lling			continue;
370b1b8ab3lling
371cd9c78dLin Ling		if (DVA_GET_GANG(&bp->blk_dva[i])) {
372d94527bDan Kimmel			if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) != 0)
373d94527bDan Kimmel				continue;
374b1b8ab3lling		} else {
375cd9c78dLin Ling			/* read in a data block */
376cd9c78dLin Ling			offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
377ad135b5Christopher Siden			sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
378d94527bDan Kimmel			if (devread(sector, 0, psize, buf) == 0)
379d94527bDan Kimmel				continue;
380d94527bDan Kimmel		}
381d94527bDan Kimmel
382d94527bDan Kimmel		/* verify that the checksum matches */
383d94527bDan Kimmel		if (zio_checksum_verify(bp, buf, psize) == 0) {
384d94527bDan Kimmel			return (0);
385b1b8ab3lling		}
386b1b8ab3lling	}
387b1b8ab3lling
388d94527bDan Kimmel	grub_printf("could not read block due to EIO or ECKSUM\n");
389cd9c78dLin Ling	return (1);
390cd9c78dLin Ling}
391cd9c78dLin Ling
392cd9c78dLin Ling/*
3935d7b4d4Matthew Ahrens * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
3945d7b4d4Matthew Ahrens * more than BPE_PAYLOAD_SIZE bytes).
3955d7b4d4Matthew Ahrens */
3965d7b4d4Matthew Ahrensstatic void
3975d7b4d4Matthew Ahrensdecode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
3985d7b4d4Matthew Ahrens{
3995d7b4d4Matthew Ahrens	int psize, i;
4005d7b4d4Matthew Ahrens	uint8_t *buf8 = buf;
4015d7b4d4Matthew Ahrens	uint64_t w = 0;
4025d7b4d4Matthew Ahrens	const uint64_t *bp64 = (const uint64_t *)bp;
4035d7b4d4Matthew Ahrens
4045d7b4d4Matthew Ahrens	psize = BPE_GET_PSIZE(bp);
4055d7b4d4Matthew Ahrens
4065d7b4d4Matthew Ahrens	/*
4075d7b4d4Matthew Ahrens	 * Decode the words of the block pointer into the byte array.
4085d7b4d4Matthew Ahrens	 * Low bits of first word are the first byte (little endian).
4095d7b4d4Matthew Ahrens	 */
4105d7b4d4Matthew Ahrens	for (i = 0; i < psize; i++) {
4115d7b4d4Matthew Ahrens		if (i % sizeof (w) == 0) {
4125d7b4d4Matthew Ahrens			/* beginning of a word */
4135d7b4d4Matthew Ahrens			w = *bp64;
4145d7b4d4Matthew Ahrens			bp64++;
4155d7b4d4Matthew Ahrens			if (!BPE_IS_PAYLOADWORD(bp, bp64))
4165d7b4d4Matthew Ahrens				bp64++;
4175d7b4d4Matthew Ahrens		}
4185d7b4d4Matthew Ahrens		buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
4195d7b4d4Matthew Ahrens	}
4205d7b4d4Matthew Ahrens}
4215d7b4d4Matthew Ahrens
4225d7b4d4Matthew Ahrens/*
4235d7b4d4Matthew Ahrens * Fill in the buffer with the (decompressed) payload of the embedded
4245d7b4d4Matthew Ahrens * blkptr_t.  Takes into account compression and byteorder (the payload is
4255d7b4d4Matthew Ahrens * treated as a stream of bytes).
4265d7b4d4Matthew Ahrens * Return 0 on success, or ENOSPC if it won't fit in the buffer.
4275d7b4d4Matthew Ahrens */
4285d7b4d4Matthew Ahrensstatic int
4295d7b4d4Matthew Ahrensdecode_embedded_bp(const blkptr_t *bp, void *buf)
4305d7b4d4Matthew Ahrens{
4315d7b4d4Matthew Ahrens	int comp;
4325d7b4d4Matthew Ahrens	int lsize, psize;
4335d7b4d4Matthew Ahrens	uint8_t *dst = buf;
4345d7b4d4Matthew Ahrens	uint64_t w = 0;
4355d7b4d4Matthew Ahrens
4365d7b4d4Matthew Ahrens	lsize = BPE_GET_LSIZE(bp);
4375d7b4d4Matthew Ahrens	psize = BPE_GET_PSIZE(bp);
4385d7b4d4Matthew Ahrens	comp = BP_GET_COMPRESS(bp);
4395d7b4d4Matthew Ahrens
4405d7b4d4Matthew Ahrens	if (comp != ZIO_COMPRESS_OFF) {
4415d7b4d4Matthew Ahrens		uint8_t dstbuf[BPE_PAYLOAD_SIZE];
4425d7b4d4Matthew Ahrens
4435d7b4d4Matthew Ahrens		if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
4445d7b4d4Matthew Ahrens		    decomp_table[comp].decomp_func == NULL) {
4455d7b4d4Matthew Ahrens			grub_printf("compression algorithm not supported\n");
4465d7b4d4Matthew Ahrens			return (ERR_FSYS_CORRUPT);
4475d7b4d4Matthew Ahrens		}
4485d7b4d4Matthew Ahrens
4495d7b4d4Matthew Ahrens		decode_embedded_bp_compressed(bp, dstbuf);
4505d7b4d4Matthew Ahrens		decomp_table[comp].decomp_func(dstbuf, buf, psize, lsize);
4515d7b4d4Matthew Ahrens	} else {
4525d7b4d4Matthew Ahrens		decode_embedded_bp_compressed(bp, buf);
4535d7b4d4Matthew Ahrens	}
4545d7b4d4Matthew Ahrens
4555d7b4d4Matthew Ahrens	return (0);
4565d7b4d4Matthew Ahrens}
4575d7b4d4Matthew Ahrens
4585d7b4d4Matthew Ahrens/*
459cd9c78dLin Ling * Read in a block of data, verify its checksum, decompress if needed,
460cd9c78dLin Ling * and put the uncompressed data in buf.
461cd9c78dLin Ling *
462cd9c78dLin Ling * Return:
463cd9c78dLin Ling *	0 - success
464cd9c78dLin Ling *	errnum - failure
465cd9c78dLin Ling */
466cd9c78dLin Lingstatic int
467cd9c78dLin Lingzio_read(blkptr_t *bp, void *buf, char *stack)
468cd9c78dLin Ling{
469cd9c78dLin Ling	int lsize, psize, comp;
470cd9c78dLin Ling	char *retbuf;
471cd9c78dLin Ling
4725d7b4d4Matthew Ahrens	if (BP_IS_EMBEDDED(bp)) {
4735d7b4d4Matthew Ahrens		if (BPE_GET_ETYPE(bp) != BP_EMBEDDED_TYPE_DATA) {
4745d7b4d4Matthew Ahrens			grub_printf("unsupported embedded BP (type=%u)\n",
4755d7b4d4Matthew Ahrens			    (int)BPE_GET_ETYPE(bp));
4765d7b4d4Matthew Ahrens			return (ERR_FSYS_CORRUPT);
4775d7b4d4Matthew Ahrens		}
4785d7b4d4Matthew Ahrens		return (decode_embedded_bp(bp, buf));
4795d7b4d4Matthew Ahrens	}
4805d7b4d4Matthew Ahrens
481cd9c78dLin Ling	comp = BP_GET_COMPRESS(bp);
482cd9c78dLin Ling	lsize = BP_GET_LSIZE(bp);
483cd9c78dLin Ling	psize = BP_GET_PSIZE(bp);
484cd9c78dLin Ling
485cd9c78dLin Ling	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
486cd9c78dLin Ling	    (comp != ZIO_COMPRESS_OFF &&
487cd9c78dLin Ling	    decomp_table[comp].decomp_func == NULL)) {
488cd9c78dLin Ling		grub_printf("compression algorithm not supported\n");
489cd9c78dLin Ling		return (ERR_FSYS_CORRUPT);
490cd9c78dLin Ling	}
491cd9c78dLin Ling
492cd9c78dLin Ling	if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
4935d7b4d4Matthew Ahrens		grub_printf("not enough memory to fit %u bytes on stack\n",
4945d7b4d4Matthew Ahrens		    lsize);
495cd9c78dLin Ling		return (ERR_WONT_FIT);
496cd9c78dLin Ling	}
497cd9c78dLin Ling
498cd9c78dLin Ling	retbuf = buf;
499cd9c78dLin Ling	if (comp != ZIO_COMPRESS_OFF) {
500cd9c78dLin Ling		buf = stack;
501cd9c78dLin Ling		stack += psize;
502cd9c78dLin Ling	}
503cd9c78dLin Ling
504ad135b5Christopher Siden	if (zio_read_data(bp, buf, stack) != 0) {
505cd9c78dLin Ling		grub_printf("zio_read_data failed\n");
506cd9c78dLin Ling		return (ERR_FSYS_CORRUPT);
507cd9c78dLin Ling	}
508cd9c78dLin Ling
509a6f561bSašo Kiselkov	if (comp != ZIO_COMPRESS_OFF) {
510a6f561bSašo Kiselkov		if (decomp_table[comp].decomp_func(buf, retbuf, psize,
511a6f561bSašo Kiselkov		    lsize) != 0) {
512a6f561bSašo Kiselkov			grub_printf("zio_read decompression failed\n");
513a6f561bSašo Kiselkov			return (ERR_FSYS_CORRUPT);
514a6f561bSašo Kiselkov		}
515a6f561bSašo Kiselkov	}
516cd9c78dLin Ling
517cd9c78dLin Ling	return (0);
518b1b8ab3lling}
519b1b8ab3lling
520b1b8ab3lling/*
521b1b8ab3lling * Get the block from a block id.
522b1b8ab3lling * push the block onto the stack.
523b1b8ab3lling *
524b1b8ab3lling * Return:
525b1b8ab3lling * 	0 - success
526b1b8ab3lling * 	errnum - failure
527b1b8ab3lling */
528b1b8ab3llingstatic int
529b1b8ab3llingdmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
530b1b8ab3lling{
531b1b8ab3lling	int idx, level;
532b1b8ab3lling	blkptr_t *bp_array = dn->dn_blkptr;
533b1b8ab3lling	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
534b1b8ab3lling	blkptr_t *bp, *tmpbuf;
535b1b8ab3lling
536b1b8ab3lling	bp = (blkptr_t *)stack;
537b1b8ab3lling	stack += sizeof (blkptr_t);
538b1b8ab3lling
539b1b8ab3lling	tmpbuf = (blkptr_t *)stack;
540b1b8ab3lling	stack += 1<<dn->dn_indblkshift;
541b1b8ab3lling
542b1b8ab3lling	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
543b1b8ab3lling		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
544b1b8ab3lling		*bp = bp_array[idx];
545b1b8ab3lling		if (level == 0)
546b1b8ab3lling			tmpbuf = buf;
547ae8180dlling		if (BP_IS_HOLE(bp)) {
548ae8180dlling			grub_memset(buf, 0,
549ae8180dlling			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
550ae8180dlling			break;
551ae8180dlling		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
552b1b8ab3lling			return (errnum);
553ae8180dlling		}
554b1b8ab3lling
555b1b8ab3lling		bp_array = tmpbuf;
556b1b8ab3lling	}
557b1b8ab3lling
558b1b8ab3lling	return (0);
559b1b8ab3lling}
560b1b8ab3lling
561b1b8ab3lling/*
562b1b8ab3lling * mzap_lookup: Looks up property described by "name" and returns the value
563b1b8ab3lling * in "value".
564b1b8ab3lling *
565b1b8ab3lling * Return:
566b1b8ab3lling *	0 - success
567b1b8ab3lling *	errnum - failure
568b1b8ab3lling */
569b1b8ab3llingstatic int
570ad135b5Christopher Sidenmzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
5719a686fbPaul Dagnelie    uint64_t *value)
572b1b8ab3lling{
573b1b8ab3lling	int i, chunks;
574b1b8ab3lling	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
575b1b8ab3lling
576ad135b5Christopher Siden	chunks = objsize / MZAP_ENT_LEN - 1;
577b1b8ab3lling	for (i = 0; i < chunks; i++) {
578b1b8ab3lling		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
579b1b8ab3lling			*value = mzap_ent[i].mze_value;
580b1b8ab3lling			return (0);
581b1b8ab3lling		}
582b1b8ab3lling	}
583b1b8ab3lling
584b1b8ab3lling	return (ERR_FSYS_CORRUPT);
585b1b8ab3lling}
586b1b8ab3lling
587b1b8ab3llingstatic uint64_t
588b1b8ab3llingzap_hash(uint64_t salt, const char *name)
589b1b8ab3lling{
590b1b8ab3lling	static uint64_t table[256];
591b1b8ab3lling	const uint8_t *cp;
592b1b8ab3lling	uint8_t c;
593b1b8ab3lling	uint64_t crc = salt;
594b1b8ab3lling
595b1b8ab3lling	if (table[128] == 0) {
596b1b8ab3lling		uint64_t *ct;
597b1b8ab3lling		int i, j;
598b1b8ab3lling		for (i = 0; i < 256; i++) {
599b1b8ab3lling			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
600b1b8ab3lling				*ct = (*ct >> 1) ^ (-(*ct & 1) &
601b1b8ab3lling				    ZFS_CRC64_POLY);
602b1b8ab3lling		}
603b1b8ab3lling	}
604b1b8ab3lling
605b1b8ab3lling	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
606b1b8ab3lling		errnum = ERR_FSYS_CORRUPT;
607b1b8ab3lling		return (0);
608b1b8ab3lling	}
609b1b8ab3lling
610b1b8ab3lling	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
611b1b8ab3lling		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
612b1b8ab3lling
613b1b8ab3lling	/*
614b1b8ab3lling	 * Only use 28 bits, since we need 4 bits in the cookie for the
615b1b8ab3lling	 * collision differentiator.  We MUST use the high bits, since
616ad135b5Christopher Siden	 * those are the ones that we first pay attention to when
617ad135b5Christopher Siden	 * choosing the bucket.
618b1b8ab3lling	 */
619b24ab67Jeff Bonwick	crc &= ~((1ULL << (64 - 28)) - 1);
620b1b8ab3lling
621b1b8ab3lling	return (crc);
622b1b8ab3lling}
623b1b8ab3lling
624b1b8ab3lling/*
625b1b8ab3lling * Only to be used on 8-bit arrays.
626b1b8ab3lling * array_len is actual len in bytes (not encoded le_value_length).
627b1b8ab3lling * buf is null-terminated.
628b1b8ab3lling */
629b1b8ab3llingstatic int
630b1b8ab3llingzap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
631b1b8ab3lling    int array_len, const char *buf)
632b1b8ab3lling{
633b1b8ab3lling	int bseen = 0;
634b1b8ab3lling
635b1b8ab3lling	while (bseen < array_len) {
636b1b8ab3lling		struct zap_leaf_array *la =
637b1b8ab3lling		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
638b1b8ab3lling		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
639b1b8ab3lling
640b1b8ab3lling		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
641b1b8ab3lling			return (0);
642b1b8ab3lling
643b1b8ab3lling		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
644b1b8ab3lling			break;
645b1b8ab3lling		chunk = la->la_next;
646b1b8ab3lling		bseen += toread;
647b1b8ab3lling	}
648b1b8ab3lling	return (bseen == array_len);
649b1b8ab3lling}
650b1b8ab3lling
651b1b8ab3lling/*
652b1b8ab3lling * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
653b1b8ab3lling * value for the property "name".
654b1b8ab3lling *
655b1b8ab3lling * Return:
656b1b8ab3lling *	0 - success
657b1b8ab3lling *	errnum - failure
658b1b8ab3lling */
659e7cbe64gwstatic int
660b1b8ab3llingzap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
661b1b8ab3lling    const char *name, uint64_t *value)
662b1b8ab3lling{
663b1b8ab3lling	uint16_t chunk;
664b1b8ab3lling	struct zap_leaf_entry *le;
665b1b8ab3lling
666b1b8ab3lling	/* Verify if this is a valid leaf block */
667b1b8ab3lling	if (l->l_hdr.lh_block_type != ZBT_LEAF)
668b1b8ab3lling		return (ERR_FSYS_CORRUPT);
669b1b8ab3lling	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
670b1b8ab3lling		return (ERR_FSYS_CORRUPT);
671b1b8ab3lling
672b1b8ab3lling	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
673b1b8ab3lling	    chunk != CHAIN_END; chunk = le->le_next) {
674b1b8ab3lling
675b1b8ab3lling		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
676b1b8ab3lling			return (ERR_FSYS_CORRUPT);
677b1b8ab3lling
678b1b8ab3lling		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
679b1b8ab3lling
680b1b8ab3lling		/* Verify the chunk entry */
681b1b8ab3lling		if (le->le_type != ZAP_CHUNK_ENTRY)
682b1b8ab3lling			return (ERR_FSYS_CORRUPT);
683b1b8ab3lling
684b1b8ab3lling		if (le->le_hash != h)
685b1b8ab3lling			continue;
686b1b8ab3lling
687b1b8ab3lling		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
688b1b8ab3lling		    le->le_name_length, name)) {
689b1b8ab3lling
690b1b8ab3lling			struct zap_leaf_array *la;
691b1b8ab3lling			uint8_t *ip;
692b1b8ab3lling
693b1b8ab3lling			if (le->le_int_size != 8 || le->le_value_length != 1)
694e37b211taylor				return (ERR_FSYS_CORRUPT);
695b1b8ab3lling
696b1b8ab3lling			/* get the uint64_t property value */
697b1b8ab3lling			la = &ZAP_LEAF_CHUNK(l, blksft,
698b1b8ab3lling			    le->le_value_chunk).l_array;
699b1b8ab3lling			ip = la->la_array;
700b1b8ab3lling
701b1b8ab3lling			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
702b1b8ab3lling			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
703b1b8ab3lling			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
704b1b8ab3lling			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
705b1b8ab3lling
706b1b8ab3lling			return (0);
707b1b8ab3lling		}
708b1b8ab3lling	}
709b1b8ab3lling
710b1b8ab3lling	return (ERR_FSYS_CORRUPT);
711b1b8ab3lling}
712b1b8ab3lling
713b1b8ab3lling/*
714b1b8ab3lling * Fat ZAP lookup
715b1b8ab3lling *
716b1b8ab3lling * Return:
717b1b8ab3lling *	0 - success
718b1b8ab3lling *	errnum - failure
719b1b8ab3lling */
720e7cbe64gwstatic int
721b1b8ab3llingfzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
722ad135b5Christopher Siden    const char *name, uint64_t *value, char *stack)
723b1b8ab3lling{
724b1b8ab3lling	zap_leaf_phys_t *l;
725b1b8ab3lling	uint64_t hash, idx, blkid;
726b1b8ab3lling	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
727b1b8ab3lling
728b1b8ab3lling	/* Verify if this is a fat zap header block */
729b24ab67Jeff Bonwick	if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
730b24ab67Jeff Bonwick	    zap->zap_flags != 0)
731b1b8ab3lling		return (ERR_FSYS_CORRUPT);
732b1b8ab3lling
733b1b8ab3lling	hash = zap_hash(zap->zap_salt, name);
734b1b8ab3lling	if (errnum)
735b1b8ab3lling		return (errnum);
736b1b8ab3lling
737b1b8ab3lling	/* get block id from index */
738b1b8ab3lling	if (zap->zap_ptrtbl.zt_numblks != 0) {
739b1b8ab3lling		/* external pointer tables not supported */
740b1b8ab3lling		return (ERR_FSYS_CORRUPT);
741b1b8ab3lling	}
742b1b8ab3lling	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
743b1b8ab3lling	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
744b1b8ab3lling
745b1b8ab3lling	/* Get the leaf block */
746b1b8ab3lling	l = (zap_leaf_phys_t *)stack;
747b1b8ab3lling	stack += 1<<blksft;
748051aabetaylor	if ((1<<blksft) < sizeof (zap_leaf_phys_t))
749e37b211taylor		return (ERR_FSYS_CORRUPT);
750b1b8ab3lling	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
751b1b8ab3lling		return (errnum);
752b1b8ab3lling
753b1b8ab3lling	return (zap_leaf_lookup(l, blksft, hash, name, value));
754b1b8ab3lling}
755b1b8ab3lling
756b1b8ab3lling/*
757b1b8ab3lling * Read in the data of a zap object and find the value for a matching
758b1b8ab3lling * property name.
759b1b8ab3lling *
760b1b8ab3lling * Return:
761b1b8ab3lling *	0 - success
762b1b8ab3lling *	errnum - failure
763b1b8ab3lling */
764b1b8ab3llingstatic int
765ad135b5Christopher Sidenzap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
766ad135b5Christopher Siden    char *stack)
767b1b8ab3lling{
768b1b8ab3lling	uint64_t block_type;
769b1b8ab3lling	int size;
770b1b8ab3lling	void *zapbuf;
771b1b8ab3lling
772b1b8ab3lling	/* Read in the first block of the zap object data. */
773b1b8ab3lling	zapbuf = stack;
774b1b8ab3lling	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
775b1b8ab3lling	stack += size;
7760a586ceMark Shellenbaum
777ad135b5Christopher Siden	if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
778b1b8ab3lling		return (errnum);
779b1b8ab3lling
780b1b8ab3lling	block_type = *((uint64_t *)zapbuf);
781b1b8ab3lling
782b1b8ab3lling	if (block_type == ZBT_MICRO) {
783b1b8ab3lling		return (mzap_lookup(zapbuf, size, name, val));
784b1b8ab3lling	} else if (block_type == ZBT_HEADER) {
785b1b8ab3lling		/* this is a fat zap */
786b1b8ab3lling		return (fzap_lookup(zap_dnode, zapbuf, name,
787b1b8ab3lling		    val, stack));
788b1b8ab3lling	}
789b1b8ab3lling
790b1b8ab3lling	return (ERR_FSYS_CORRUPT);
791b1b8ab3lling}
792b1b8ab3lling
793ad135b5Christopher Sidentypedef struct zap_attribute {
794ad135b5Christopher Siden	int za_integer_length;
795ad135b5Christopher Siden	uint64_t za_num_integers;
796ad135b5Christopher Siden	uint64_t za_first_integer;
797ad135b5Christopher Siden	char *za_name;
798ad135b5Christopher Siden} zap_attribute_t;
799ad135b5Christopher Siden
800ad135b5Christopher Sidentypedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
801ad135b5Christopher Siden
802ad135b5Christopher Sidenstatic int
803ad135b5Christopher Sidenzap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
804ad135b5Christopher Siden{
805ad135b5Christopher Siden	uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
806ad135b5Christopher Siden	zap_attribute_t za;
807ad135b5Christopher Siden	int i;
808ad135b5Christopher Siden	mzap_phys_t *mzp = (mzap_phys_t *)stack;
809ad135b5Christopher Siden	stack += size;
810ad135b5Christopher Siden
811ad135b5Christopher Siden	if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
812ad135b5Christopher Siden		return (errnum);
813ad135b5Christopher Siden
814ad135b5Christopher Siden	/*
815ad135b5Christopher Siden	 * Iteration over fatzap objects has not yet been implemented.
816ad135b5Christopher Siden	 * If we encounter a pool in which there are more features for
817ad135b5Christopher Siden	 * read than can fit inside a microzap (i.e., more than 2048
818ad135b5Christopher Siden	 * features for read), we can add support for fatzap iteration.
819ad135b5Christopher Siden	 * For now, fail.
820ad135b5Christopher Siden	 */
821ad135b5Christopher Siden	if (mzp->mz_block_type != ZBT_MICRO) {
822ad135b5Christopher Siden		grub_printf("feature information stored in fatzap, pool "
823ad135b5Christopher Siden		    "version not supported\n");
824ad135b5Christopher Siden		return (1);
825ad135b5Christopher Siden	}
826ad135b5Christopher Siden
827ad135b5Christopher Siden	za.za_integer_length = 8;
828ad135b5Christopher Siden	za.za_num_integers = 1;
829ad135b5Christopher Siden	for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
830ad135b5Christopher Siden		mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
831ad135b5Christopher Siden		int err;
832ad135b5Christopher Siden
833ad135b5Christopher Siden		za.za_first_integer = mzep->mze_value;
834ad135b5Christopher Siden		za.za_name = mzep->mze_name;
835ad135b5Christopher Siden		err = cb(&za, arg, stack);
836ad135b5Christopher Siden		if (err != 0)
837ad135b5Christopher Siden			return (err);
838ad135b5Christopher Siden	}
839ad135b5Christopher Siden
840ad135b5Christopher Siden	return (0);
841ad135b5Christopher Siden}
842ad135b5Christopher Siden
843b1b8ab3lling/*
844b1b8ab3lling * Get the dnode of an object number from the metadnode of an object set.
845b1b8ab3lling *
846b1b8ab3lling * Input
847b1b8ab3lling *	mdn - metadnode to get the object dnode
848b1b8ab3lling *	objnum - object number for the object dnode
8495d7b4d4Matthew Ahrens *	type - if nonzero, object must be of this type
850b1b8ab3lling *	buf - data buffer that holds the returning dnode
851b1b8ab3lling *	stack - scratch area
852b1b8ab3lling *
853b1b8ab3lling * Return:
854b1b8ab3lling *	0 - success
855b1b8ab3lling *	errnum - failure
856b1b8ab3lling */
857b1b8ab3llingstatic int
858b1b8ab3llingdnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
8599a686fbPaul Dagnelie    char *stack)
860b1b8ab3lling{
861b1b8ab3lling	uint64_t blkid, blksz; /* the block id this object dnode is in */
862b1b8ab3lling	int epbs; /* shift of number of dnodes in a block */
863b1b8ab3lling	int idx; /* index within a block */
864b1b8ab3lling	dnode_phys_t *dnbuf;
865b1b8ab3lling
866b1b8ab3lling	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
867b1b8ab3lling	epbs = zfs_log2(blksz) - DNODE_SHIFT;
868b1b8ab3lling	blkid = objnum >> epbs;
869b1b8ab3lling	idx = objnum & ((1<<epbs)-1);
870b1b8ab3lling
871b1b8ab3lling	if (dnode_buf != NULL && dnode_mdn == mdn &&
872b1b8ab3lling	    objnum >= dnode_start && objnum < dnode_end) {
873b1b8ab3lling		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
874b1b8ab3lling		VERIFY_DN_TYPE(buf, type);
875b1b8ab3lling		return (0);
876b1b8ab3lling	}
877b1b8ab3lling
878b1b8ab3lling	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
879b1b8ab3lling		dnbuf = dnode_buf;
880b1b8ab3lling		dnode_mdn = mdn;
881b1b8ab3lling		dnode_start = blkid << epbs;
882b1b8ab3lling		dnode_end = (blkid + 1) << epbs;
883b1b8ab3lling	} else {
884b1b8ab3lling		dnbuf = (dnode_phys_t *)stack;
885b1b8ab3lling		stack += blksz;
886b1b8ab3lling	}
887b1b8ab3lling
888b1b8ab3lling	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
889b1b8ab3lling		return (errnum);
890b1b8ab3lling
891b1b8ab3lling	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
892b1b8ab3lling	VERIFY_DN_TYPE(buf, type);
893b1b8ab3lling
894b1b8ab3lling	return (0);
895b1b8ab3lling}
896b1b8ab3lling
897b1b8ab3lling/*
898eb2bd66vikram * Check if this is a special file that resides at the top
899eb2bd66vikram * dataset of the pool. Currently this is the GRUB menu,
900eb2bd66vikram * boot signature and boot signature backup.
901b1b8ab3lling * str starts with '/'.
902b1b8ab3lling */
903b1b8ab3llingstatic int
904eb2bd66vikramis_top_dataset_file(char *str)
905b1b8ab3lling{
906b1b8ab3lling	char *tptr;
907b1b8ab3lling
908b1b8ab3lling	if ((tptr = grub_strstr(str, "menu.lst")) &&
909b1b8ab3lling	    (tptr[8] == '\0' || tptr[8] == ' ') &&
910b1b8ab3lling	    *(tptr-1) == '/')
911b1b8ab3lling		return (1);
912b1b8ab3lling
913eb2bd66vikram	if (grub_strncmp(str, BOOTSIGN_DIR"/",
9141183b40vikram	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
915eb2bd66vikram		return (1);
916eb2bd66vikram
917eb2bd66vikram	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
918eb2bd66vikram		return (1);
919eb2bd66vikram
920b1b8ab3lling	return (0);
921b1b8ab3lling}
922b1b8ab3lling
923ad135b5Christopher Sidenstatic int
924ad135b5Christopher Sidencheck_feature(zap_attribute_t *za, void *arg, char *stack)
925ad135b5Christopher Siden{
926ad135b5Christopher Siden	const char **names = arg;
927ad135b5Christopher Siden	int i;
928ad135b5Christopher Siden
929ad135b5Christopher Siden	if (za->za_first_integer == 0)
930ad135b5Christopher Siden		return (0);
931ad135b5Christopher Siden
932ad135b5Christopher Siden	for (i = 0; names[i] != NULL; i++) {
933ad135b5Christopher Siden		if (grub_strcmp(za->za_name, names[i]) == 0) {
934ad135b5Christopher Siden			return (0);
935ad135b5Christopher Siden		}
936ad135b5Christopher Siden	}
937ad135b5Christopher Siden	grub_printf("missing feature for read '%s'\n", za->za_name);
938ad135b5Christopher Siden	return (ERR_NEWER_VERSION);
939ad135b5Christopher Siden}
940ad135b5Christopher Siden
941b1b8ab3lling/*
942b1b8ab3lling * Get the file dnode for a given file name where mdn is the meta dnode
943b1b8ab3lling * for this ZFS object set. When found, place the file dnode in dn.
944b1b8ab3lling * The 'path' argument will be mangled.
945b1b8ab3lling *
946b1b8ab3lling * Return:
947b1b8ab3lling *	0 - success
948b1b8ab3lling *	errnum - failure
949b1b8ab3lling */
950b1b8ab3llingstatic int
951b1b8ab3llingdnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
952b1b8ab3lling    char *stack)
953b1b8ab3lling{
954e743726ahrens	uint64_t objnum, version;
955b1b8ab3lling	char *cname, ch;
956b1b8ab3lling
957b1b8ab3lling	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
958b1b8ab3lling	    dn, stack))
959b1b8ab3lling		return (errnum);
960b1b8ab3lling
961e743726ahrens	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
962e743726ahrens		return (errnum);
963e743726ahrens	if (version > ZPL_VERSION)
964e743726ahrens		return (-1);
965e743726ahrens
966b1b8ab3lling	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
967b1b8ab3lling		return (errnum);
968b1b8ab3lling
969b1b8ab3lling	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
970b1b8ab3lling	    dn, stack))
971b1b8ab3lling		return (errnum);
972b1b8ab3lling
973b1b8ab3lling	/* skip leading slashes */
974b1b8ab3lling	while (*path == '/')
975b1b8ab3lling		path++;
976b1b8ab3lling
977ad135b5Christopher Siden	while (*path && !grub_isspace(*path)) {
978b1b8ab3lling
979b1b8ab3lling		/* get the next component name */
980b1b8ab3lling		cname = path;
981ad135b5Christopher Siden		while (*path && !grub_isspace(*path) && *path != '/')
982b1b8ab3lling			path++;
983b1b8ab3lling		ch = *path;
984b1b8ab3lling		*path = 0;   /* ensure null termination */
985b1b8ab3lling
986b1b8ab3lling		if (errnum = zap_lookup(dn, cname, &objnum, stack))
987b1b8ab3lling			return (errnum);
988b1b8ab3lling
989e743726ahrens		objnum = ZFS_DIRENT_OBJ(objnum);
990b1b8ab3lling		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
991b1b8ab3lling			return (errnum);
992b1b8ab3lling
993b1b8ab3lling		*path = ch;
994b1b8ab3lling		while (*path == '/')
995b1b8ab3lling			path++;
996b1b8ab3lling	}
997b1b8ab3lling
998b1b8ab3lling	/* We found the dnode for this file. Verify if it is a plain file. */
999b1b8ab3lling	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
1000b1b8ab3lling
1001b1b8ab3lling	return (0);
1002b1b8ab3lling}
1003b1b8ab3lling
1004b1b8ab3lling/*
1005b1b8ab3lling * Get the default 'bootfs' property value from the rootpool.
1006b1b8ab3lling *
1007b1b8ab3lling * Return:
1008b1b8ab3lling *	0 - success
1009b1b8ab3lling *	errnum -failure
1010b1b8ab3lling */
1011b1b8ab3llingstatic int
1012b1b8ab3llingget_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
1013b1b8ab3lling{
1014b1b8ab3lling	uint64_t objnum = 0;
1015b1b8ab3lling	dnode_phys_t *dn = (dnode_phys_t *)stack;
1016b1b8ab3lling	stack += DNODE_SIZE;
1017b1b8ab3lling
1018ae8180dlling	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1019b1b8ab3lling	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
1020ae8180dlling		return (errnum);
1021b1b8ab3lling
1022b1b8ab3lling	/*
1023b1b8ab3lling	 * find the object number for 'pool_props', and get the dnode
1024b1b8ab3lling	 * of the 'pool_props'.
1025b1b8ab3lling	 */
1026b1b8ab3lling	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
1027b1b8ab3lling		return (ERR_FILESYSTEM_NOT_FOUND);
1028b1b8ab3lling
1029ae8180dlling	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
1030ae8180dlling		return (errnum);
1031b1b8ab3lling
1032b1b8ab3lling	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
1033b1b8ab3lling		return (ERR_FILESYSTEM_NOT_FOUND);
1034b1b8ab3lling
1035b1b8ab3lling	if (!objnum)
1036b1b8ab3lling		return (ERR_FILESYSTEM_NOT_FOUND);
1037b1b8ab3lling
1038b1b8ab3lling	*obj = objnum;
1039b1b8ab3lling	return (0);
1040b1b8ab3lling}
1041b1b8ab3lling
1042b1b8ab3lling/*
1043ad135b5Christopher Siden * List of pool features that the grub implementation of ZFS supports for
1044ad135b5Christopher Siden * read. Note that features that are only required for write do not need
1045ad135b5Christopher Siden * to be listed here since grub opens pools in read-only mode.
104633915f3Richard Lowe *
104733915f3Richard Lowe * When this list is updated the version number in usr/src/grub/capability
104833915f3Richard Lowe * must be incremented to ensure the new grub gets installed.
1049ad135b5Christopher Siden */
1050ad135b5Christopher Sidenstatic const char *spa_feature_names[] = {
1051a6f561bSašo Kiselkov	"org.illumos:lz4_compress",
105243466aaMax Grossman	"com.delphix:hole_birth",
10532acef22Matthew Ahrens	"com.delphix:extensible_dataset",
10545d7b4d4Matthew Ahrens	"com.delphix:embedded_data",
1055b515258Matthew Ahrens	"org.open-zfs:large_blocks",
105645818eeMatthew Ahrens	"org.illumos:sha512",
1057ad135b5Christopher Siden	NULL
1058ad135b5Christopher Siden};
1059ad135b5Christopher Siden
1060ad135b5Christopher Siden/*
1061ad135b5Christopher Siden * Checks whether the MOS features that are active are supported by this
1062ad135b5Christopher Siden * (GRUB's) implementation of ZFS.
1063ad135b5Christopher Siden *
1064ad135b5Christopher Siden * Return:
1065ad135b5Christopher Siden *	0: Success.
1066ad135b5Christopher Siden *	errnum: Failure.
1067ad135b5Christopher Siden */
1068ad135b5Christopher Sidenstatic int
1069ad135b5Christopher Sidencheck_mos_features(dnode_phys_t *mosmdn, char *stack)
1070ad135b5Christopher Siden{
1071ad135b5Christopher Siden	uint64_t objnum;
1072ad135b5Christopher Siden	dnode_phys_t *dn;
1073ad135b5Christopher Siden	uint8_t error = 0;
1074ad135b5Christopher Siden
1075ad135b5Christopher Siden	dn = (dnode_phys_t *)stack;
1076ad135b5Christopher Siden	stack += DNODE_SIZE;
1077ad135b5Christopher Siden
1078ad135b5Christopher Siden	if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1079ad135b5Christopher Siden	    DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
1080ad135b5Christopher Siden		return (errnum);
1081ad135b5Christopher Siden
1082ad135b5Christopher Siden	/*
1083ad135b5Christopher Siden	 * Find the object number for 'features_for_read' and retrieve its
1084ad135b5Christopher Siden	 * corresponding dnode. Note that we don't check features_for_write
1085ad135b5Christopher Siden	 * because GRUB is not opening the pool for write.
1086ad135b5Christopher Siden	 */
1087ad135b5Christopher Siden	if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
1088ad135b5Christopher Siden	    stack)) != 0)
1089ad135b5Christopher Siden		return (errnum);
1090ad135b5Christopher Siden
1091ad135b5Christopher Siden	if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
1092ad135b5Christopher Siden	    dn, stack)) != 0)
1093ad135b5Christopher Siden		return (errnum);
1094ad135b5Christopher Siden
1095ad135b5Christopher Siden	return (zap_iterate(dn, check_feature, spa_feature_names, stack));
1096ad135b5Christopher Siden}
1097ad135b5Christopher Siden
1098ad135b5Christopher Siden/*
1099b1b8ab3lling * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1100b1b8ab3lling * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1101b1b8ab3lling * of pool/rootfs.
1102b1b8ab3lling *
1103b1b8ab3lling * If no fsname and no obj are given, return the DSL_DIR metadnode.
1104b1b8ab3lling * If fsname is given, return its metadnode and its matching object number.
1105b1b8ab3lling * If only obj is given, return the metadnode for this object number.
1106b1b8ab3lling *
1107b1b8ab3lling * Return:
1108b1b8ab3lling *	0 - success
1109b1b8ab3lling *	errnum - failure
1110b1b8ab3lling */
1111b1b8ab3llingstatic int
1112b1b8ab3llingget_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
1113b1b8ab3lling    dnode_phys_t *mdn, char *stack)
1114b1b8ab3lling{
1115b1b8ab3lling	uint64_t objnum, headobj;
1116b1b8ab3lling	char *cname, ch;
1117b1b8ab3lling	blkptr_t *bp;
1118b1b8ab3lling	objset_phys_t *osp;
1119fe3e263Eric Taylor	int issnapshot = 0;
1120fe3e263Eric Taylor	char *snapname;
1121b1b8ab3lling
1122b1b8ab3lling	if (fsname == NULL && obj) {
1123b1b8ab3lling		headobj = *obj;
1124b1b8ab3lling		goto skip;
1125b1b8ab3lling	}
1126b1b8ab3lling
1127b1b8ab3lling	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1128b1b8ab3lling	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
1129b1b8ab3lling		return (errnum);
1130b1b8ab3lling
1131b1b8ab3lling	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
1132b1b8ab3lling	    stack))
1133b1b8ab3lling		return (errnum);
1134b1b8ab3lling
11352acef22Matthew Ahrens	if (errnum = dnode_get(mosmdn, objnum, 0, mdn, stack))
1136b1b8ab3lling		return (errnum);
1137b1b8ab3lling
1138b1b8ab3lling	if (fsname == NULL) {
1139b1b8ab3lling		headobj =
1140b1b8ab3lling		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1141b1b8ab3lling		goto skip;
1142b1b8ab3lling	}
1143b1b8ab3lling
1144b1b8ab3lling	/* take out the pool name */
1145ad135b5Christopher Siden	while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1146b1b8ab3lling		fsname++;
1147b1b8ab3lling
1148ad135b5Christopher Siden	while (*fsname && !grub_isspace(*fsname)) {
1149b1b8ab3lling		uint64_t childobj;
1150b1b8ab3lling
1151b1b8ab3lling		while (*fsname == '/')
1152b1b8ab3lling			fsname++;
1153b1b8ab3lling
1154b1b8ab3lling		cname = fsname;
1155ad135b5Christopher Siden		while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1156b1b8ab3lling			fsname++;
1157b1b8ab3lling		ch = *fsname;
1158b1b8ab3lling		*fsname = 0;
1159b1b8ab3lling
1160fe3e263Eric Taylor		snapname = cname;
1161ad135b5Christopher Siden		while (*snapname && !grub_isspace(*snapname) && *snapname !=
1162ad135b5Christopher Siden		    '@')
1163fe3e263Eric Taylor			snapname++;
1164fe3e263Eric Taylor		if (*snapname == '@') {
1165fe3e263Eric Taylor			issnapshot = 1;
1166fe3e263Eric Taylor			*snapname = 0;
1167fe3e263Eric Taylor		}
1168b1b8ab3lling		childobj =
1169b1b8ab3lling		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
1170b1b8ab3lling		if (errnum = dnode_get(mosmdn, childobj,
1171b1b8ab3lling		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
1172b1b8ab3lling			return (errnum);
1173b1b8ab3lling
1174ae8180dlling		if (zap_lookup(mdn, cname, &objnum, stack))
1175ae8180dlling			return (ERR_FILESYSTEM_NOT_FOUND);
1176b1b8ab3lling
11772acef22Matthew Ahrens		if (errnum = dnode_get(mosmdn, objnum, 0,
1178b1b8ab3lling		    mdn, stack))
1179b1b8ab3lling			return (errnum);
1180b1b8ab3lling
1181b1b8ab3lling		*fsname = ch;
1182fe3e263Eric Taylor		if (issnapshot)
1183fe3e263Eric Taylor			*snapname = '@';
1184b1b8ab3lling	}
1185b1b8ab3lling	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1186b1b8ab3lling	if (obj)
1187b1b8ab3lling		*obj = headobj;
1188b1b8ab3lling
1189b1b8ab3llingskip:
11902acef22Matthew Ahrens	if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1191b1b8ab3lling		return (errnum);
1192fe3e263Eric Taylor	if (issnapshot) {
1193fe3e263Eric Taylor		uint64_t snapobj;
1194fe3e263Eric Taylor
1195fe3e263Eric Taylor		snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
1196fe3e263Eric Taylor		    ds_snapnames_zapobj;
1197b1b8ab3lling
1198fe3e263Eric Taylor		if (errnum = dnode_get(mosmdn, snapobj,
1199fe3e263Eric Taylor		    DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
1200fe3e263Eric Taylor			return (errnum);
1201fe3e263Eric Taylor		if (zap_lookup(mdn, snapname + 1, &headobj, stack))
1202fe3e263Eric Taylor			return (ERR_FILESYSTEM_NOT_FOUND);
12032acef22Matthew Ahrens		if (errnum = dnode_get(mosmdn, headobj, 0, mdn, stack))
1204fe3e263Eric Taylor			return (errnum);
1205fe3e263Eric Taylor		if (obj)
1206fe3e263Eric Taylor			*obj = headobj;
1207fe3e263Eric Taylor	}
1208b1b8ab3lling
1209b1b8ab3lling	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
1210b1b8ab3lling	osp = (objset_phys_t *)stack;
1211b1b8ab3lling	stack += sizeof (objset_phys_t);
1212b1b8ab3lling	if (errnum = zio_read(bp, osp, stack))
1213b1b8ab3lling		return (errnum);
1214b1b8ab3lling
1215b1b8ab3lling	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
1216b1b8ab3lling
1217b1b8ab3lling	return (0);
1218b1b8ab3lling}
1219b1b8ab3lling
1220b1b8ab3lling/*
1221e7cbe64gw * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1222b1b8ab3lling *
1223e7cbe64gw * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1224b1b8ab3lling *
1225b1b8ab3lling *      encoding method/host endian     (4 bytes)
1226b1b8ab3lling *      nvl_version                     (4 bytes)
1227b1b8ab3lling *      nvl_nvflag                      (4 bytes)
1228b1b8ab3lling *	encoded nvpairs:
1229b1b8ab3lling *		encoded size of the nvpair      (4 bytes)
1230b1b8ab3lling *		decoded size of the nvpair      (4 bytes)
1231b1b8ab3lling *		name string size                (4 bytes)
1232b1b8ab3lling *		name string data                (sizeof(NV_ALIGN4(string))
1233b1b8ab3lling *		data type                       (4 bytes)
1234b1b8ab3lling *		# of elements in the nvpair     (4 bytes)
1235b1b8ab3lling *		data
1236b1b8ab3lling *      2 zero's for the last nvpair
1237b1b8ab3lling *		(end of the entire list)	(8 bytes)
1238b1b8ab3lling *
1239b1b8ab3lling * Return:
1240b1b8ab3lling *	0 - success
1241b1b8ab3lling *	1 - failure
1242b1b8ab3lling */
1243e7cbe64gwstatic int
1244e7cbe64gwnvlist_unpack(char *nvlist, char **out)
1245b1b8ab3lling{
1246b1b8ab3lling	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
1247b1b8ab3lling	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1248b1b8ab3lling		return (1);
1249b1b8ab3lling
1250ad135b5Christopher Siden	*out = nvlist + 4;
1251e7cbe64gw	return (0);
1252e7cbe64gw}
1253e7cbe64gw
1254e7cbe64gwstatic char *
1255e7cbe64gwnvlist_array(char *nvlist, int index)
1256e7cbe64gw{
1257e7cbe64gw	int i, encode_size;
1258e7cbe64gw
1259e7cbe64gw	for (i = 0; i < index; i++) {
1260e7cbe64gw		/* skip the header, nvl_version, and nvl_nvflag */
1261e7cbe64gw		nvlist = nvlist + 4 * 2;
1262e7cbe64gw
1263e7cbe64gw		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1264e7cbe64gw			nvlist += encode_size; /* goto the next nvpair */
1265e7cbe64gw
1266e7cbe64gw		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1267e7cbe64gw	}
1268e7cbe64gw
1269e7cbe64gw	return (nvlist);
1270e7cbe64gw}
1271e7cbe64gw
1272ad135b5Christopher Siden/*
1273ad135b5Christopher Siden * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
1274ad135b5Christopher Siden * list following nvpair. If nvpair is NULL, the first pair is returned. If
1275ad135b5Christopher Siden * nvpair is the last pair in the nvlist, NULL is returned.
1276ad135b5Christopher Siden */
1277ad135b5Christopher Sidenstatic char *
1278ad135b5Christopher Sidennvlist_next_nvpair(char *nvl, char *nvpair)
1279ad135b5Christopher Siden{
1280ad135b5Christopher Siden	char *cur, *prev;
1281ad135b5Christopher Siden	int encode_size;
1282ad135b5Christopher Siden
1283ad135b5Christopher Siden	if (nvl == NULL)
1284ad135b5Christopher Siden		return (NULL);
1285ad135b5Christopher Siden
1286ad135b5Christopher Siden	if (nvpair == NULL) {
1287ad135b5Christopher Siden		/* skip over nvl_version and nvl_nvflag */
1288ad135b5Christopher Siden		nvpair = nvl + 4 * 2;
1289ad135b5Christopher Siden	} else {
1290ad135b5Christopher Siden		/* skip to the next nvpair */
1291ad135b5Christopher Siden		encode_size = BSWAP_32(*(uint32_t *)nvpair);
1292ad135b5Christopher Siden		nvpair += encode_size;
1293ad135b5Christopher Siden	}
1294ad135b5Christopher Siden
1295ad135b5Christopher Siden	/* 8 bytes of 0 marks the end of the list */
1296ad135b5Christopher Siden	if (*(uint64_t *)nvpair == 0)
1297ad135b5Christopher Siden		return (NULL);
1298ad135b5Christopher Siden
1299ad135b5Christopher Siden	return (nvpair);
1300ad135b5Christopher Siden}
1301ad135b5Christopher Siden
1302ad135b5Christopher Siden/*
1303ad135b5Christopher Siden * This function returns 0 on success and 1 on failure. On success, a string
1304ad135b5Christopher Siden * containing the name of nvpair is saved in buf.
1305ad135b5Christopher Siden */
1306e7cbe64gwstatic int
1307ad135b5Christopher Sidennvpair_name(char *nvp, char *buf, int buflen)
1308ad135b5Christopher Siden{
1309ad135b5Christopher Siden	int len;
1310ad135b5Christopher Siden
1311ad135b5Christopher Siden	/* skip over encode/decode size */
1312ad135b5Christopher Siden	nvp += 4 * 2;
1313ad135b5Christopher Siden
1314ad135b5Christopher Siden	len = BSWAP_32(*(uint32_t *)nvp);
1315ad135b5Christopher Siden	if (buflen < len + 1)
1316ad135b5Christopher Siden		return (1);
1317ad135b5Christopher Siden
1318ad135b5Christopher Siden	grub_memmove(buf, nvp + 4, len);
1319ad135b5Christopher Siden	buf[len] = '\0';
1320ad135b5Christopher Siden
1321ad135b5Christopher Siden	return (0);
1322ad135b5Christopher Siden}
1323ad135b5Christopher Siden
1324ad135b5Christopher Siden/*
1325ad135b5Christopher Siden * This function retrieves the value of the nvpair in the form of enumerated
1326ad135b5Christopher Siden * type data_type_t. This is used to determine the appropriate type to pass to
1327ad135b5Christopher Siden * nvpair_value().
1328ad135b5Christopher Siden */
1329ad135b5Christopher Sidenstatic int
1330ad135b5Christopher Sidennvpair_type(char *nvp)
1331e7cbe64gw{
1332ad135b5Christopher Siden	int name_len, type;
1333ad135b5Christopher Siden
1334ad135b5Christopher Siden	/* skip over encode/decode size */
1335ad135b5Christopher Siden	nvp += 4 * 2;
1336ad135b5Christopher Siden
1337ad135b5Christopher Siden	/* skip over name_len */
1338ad135b5Christopher Siden	name_len = BSWAP_32(*(uint32_t *)nvp);
1339ad135b5Christopher Siden	nvp += 4;
1340ad135b5Christopher Siden
1341ad135b5Christopher Siden	/* skip over name */
1342ad135b5Christopher Siden	nvp = nvp + ((name_len + 3) & ~3); /* align */
1343ad135b5Christopher Siden
1344ad135b5Christopher Siden	type = BSWAP_32(*(uint32_t *)nvp);
1345ad135b5Christopher Siden
1346ad135b5Christopher Siden	return (type);
1347ad135b5Christopher Siden}
1348ad135b5Christopher Siden
1349ad135b5Christopher Sidenstatic int
1350ad135b5Christopher Sidennvpair_value(char *nvp, void *val, int valtype, int *nelmp)
1351ad135b5Christopher Siden{
1352ad135b5Christopher Siden	int name_len, type, slen;
1353ad135b5Christopher Siden	char *strval = val;
1354e7cbe64gw	uint64_t *intval = val;
1355e7cbe64gw
1356ad135b5Christopher Siden	/* skip over encode/decode size */
1357ad135b5Christopher Siden	nvp += 4 * 2;
1358b1b8ab3lling
1359ad135b5Christopher Siden	/* skip over name_len */
1360ad135b5Christopher Siden	name_len = BSWAP_32(*(uint32_t *)nvp);
1361ad135b5Christopher Siden	nvp += 4;
1362b1b8ab3lling
1363ad135b5Christopher Siden	/* skip over name */
1364ad135b5Christopher Siden	nvp = nvp + ((name_len + 3) & ~3); /* align */
1365b1b8ab3lling
1366ad135b5Christopher Siden	/* skip over type */
1367ad135b5Christopher Siden	type = BSWAP_32(*(uint32_t *)nvp);
1368ad135b5Christopher Siden	nvp += 4;
1369b1b8ab3lling
1370ad135b5Christopher Siden	if (type == valtype) {
1371ad135b5Christopher Siden		int nelm;
1372b1b8ab3lling
1373ad135b5Christopher Siden		nelm = BSWAP_32(*(uint32_t *)nvp);
1374ad135b5Christopher Siden		if (valtype != DATA_TYPE_BOOLEAN && nelm < 1)
1375ad135b5Christopher Siden			return (1);
1376ad135b5Christopher Siden		nvp += 4;
1377b1b8ab3lling
1378ad135b5Christopher Siden		switch (valtype) {
1379ad135b5Christopher Siden		case DATA_TYPE_BOOLEAN:
1380ad135b5Christopher Siden			return (0);
1381b1b8ab3lling
1382ad135b5Christopher Siden		case DATA_TYPE_STRING:
1383ad135b5Christopher Siden			slen = BSWAP_32(*(uint32_t *)nvp);
1384ad135b5Christopher Siden			nvp += 4;
1385ad135b5Christopher Siden			grub_memmove(strval, nvp, slen);
1386ad135b5Christopher Siden			strval[slen] = '\0';
1387ad135b5Christopher Siden			return (0);
1388b1b8ab3lling
1389ad135b5Christopher Siden		case DATA_TYPE_UINT64:
1390ad135b5Christopher Siden			*intval = BSWAP_64(*(uint64_t *)nvp);
1391ad135b5Christopher Siden			return (0);
1392e7cbe64gw
1393ad135b5Christopher Siden		case DATA_TYPE_NVLIST:
1394ad135b5Christopher Siden			*(void **)val = (void *)nvp;
1395ad135b5Christopher Siden			return (0);
1396e7cbe64gw
1397ad135b5Christopher Siden		case DATA_TYPE_NVLIST_ARRAY:
1398ad135b5Christopher Siden			*(void **)val = (void *)nvp;
1399ad135b5Christopher Siden			if (nelmp)
1400ad135b5Christopher Siden				*nelmp = nelm;
1401ad135b5Christopher Siden			return (0);
1402b1b8ab3lling		}
1403b1b8ab3lling	}
1404b1b8ab3lling
1405b1b8ab3lling	return (1);
1406b1b8ab3lling}
1407b1b8ab3lling
1408ad135b5Christopher Sidenstatic int
1409ad135b5Christopher Sidennvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1410ad135b5Christopher Siden    int *nelmp)
1411ad135b5Christopher Siden{
1412ad135b5Christopher Siden	char *nvpair;
1413ad135b5Christopher Siden
1414ad135b5Christopher Siden	for (nvpair = nvlist_next_nvpair(nvlist, NULL);
1415ad135b5Christopher Siden	    nvpair != NULL;
1416ad135b5Christopher Siden	    nvpair = nvlist_next_nvpair(nvlist, nvpair)) {
1417ad135b5Christopher Siden		int name_len = BSWAP_32(*(uint32_t *)(nvpair + 4 * 2));
1418ad135b5Christopher Siden		char *nvp_name = nvpair + 4 * 3;
1419ad135b5Christopher Siden
1420ad135b5Christopher Siden		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1421ad135b5Christopher Siden		    nvpair_type(nvpair) == valtype) {
1422ad135b5Christopher Siden			return (nvpair_value(nvpair, val, valtype, nelmp));
1423ad135b5Christopher Siden		}
1424ad135b5Christopher Siden	}
1425ad135b5Christopher Siden	return (1);
1426ad135b5Christopher Siden}
1427ad135b5Christopher Siden
1428b1b8ab3lling/*
1429e7cbe64gw * Check if this vdev is online and is in a good state.
1430e7cbe64gw */
1431e7cbe64gwstatic int
1432e7cbe64gwvdev_validate(char *nv)
1433e7cbe64gw{
1434e7cbe64gw	uint64_t ival;
1435e7cbe64gw
1436e7cbe64gw	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1437e7cbe64gw	    DATA_TYPE_UINT64, NULL) == 0 ||
1438e7cbe64gw	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1439e7cbe64gw	    DATA_TYPE_UINT64, NULL) == 0 ||
1440e7cbe64gw	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1441e7cbe64gw	    DATA_TYPE_UINT64, NULL) == 0)
1442e7cbe64gw		return (ERR_DEV_VALUES);
1443e7cbe64gw
1444e7cbe64gw	return (0);
1445e7cbe64gw}
1446e7cbe64gw
1447e7cbe64gw/*
144821ecdf6Lin Ling * Get a valid vdev pathname/devid from the boot device.
1449ffb5616Lin Ling * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1450e7cbe64gw */
145121ecdf6Lin Lingstatic int
145221ecdf6Lin Lingvdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
145321ecdf6Lin Ling    int is_spare)
1454e7cbe64gw{
1455e7cbe64gw	char type[16];
1456e7cbe64gw
1457e7cbe64gw	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1458e7cbe64gw	    NULL))
1459e7cbe64gw		return (ERR_FSYS_CORRUPT);
1460e7cbe64gw
1461ad135b5Christopher Siden	if (grub_strcmp(type, VDEV_TYPE_DISK) == 0) {
1462ffb5616Lin Ling		uint64_t guid;
1463ffb5616Lin Ling
1464ffb5616Lin Ling		if (vdev_validate(nv) != 0)
1465ffb5616Lin Ling			return (ERR_NO_BOOTPATH);
1466ffb5616Lin Ling
1467ffb5616Lin Ling		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1468ffb5616Lin Ling		    &guid, DATA_TYPE_UINT64, NULL) != 0)
1469ffb5616Lin Ling			return (ERR_NO_BOOTPATH);
1470ffb5616Lin Ling
1471ffb5616Lin Ling		if (guid != inguid)
1472e7cbe64gw			return (ERR_NO_BOOTPATH);
1473e7cbe64gw
147421ecdf6Lin Ling		/* for a spare vdev, pick the disk labeled with "is_spare" */
147521ecdf6Lin Ling		if (is_spare) {
147621ecdf6Lin Ling			uint64_t spare = 0;
147721ecdf6Lin Ling			(void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
147821ecdf6Lin Ling			    &spare, DATA_TYPE_UINT64, NULL);
147921ecdf6