xref: /freebsd/stand/libsa/zfs/zfsimpl.c (revision b8902de147f46c9dcf7f54711b47d6b512fc5c1c)
1*b8902de1SWarner Losh /*-
2*b8902de1SWarner Losh  * Copyright (c) 2007 Doug Rabson
3*b8902de1SWarner Losh  * All rights reserved.
4*b8902de1SWarner Losh  *
5*b8902de1SWarner Losh  * Redistribution and use in source and binary forms, with or without
6*b8902de1SWarner Losh  * modification, are permitted provided that the following conditions
7*b8902de1SWarner Losh  * are met:
8*b8902de1SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9*b8902de1SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10*b8902de1SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11*b8902de1SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12*b8902de1SWarner Losh  *    documentation and/or other materials provided with the distribution.
13*b8902de1SWarner Losh  *
14*b8902de1SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*b8902de1SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*b8902de1SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*b8902de1SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*b8902de1SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*b8902de1SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*b8902de1SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*b8902de1SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*b8902de1SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*b8902de1SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*b8902de1SWarner Losh  * SUCH DAMAGE.
25*b8902de1SWarner Losh  */
26*b8902de1SWarner Losh 
27*b8902de1SWarner Losh #include <sys/cdefs.h>
28*b8902de1SWarner Losh __FBSDID("$FreeBSD$");
29*b8902de1SWarner Losh 
30*b8902de1SWarner Losh /*
31*b8902de1SWarner Losh  *	Stand-alone ZFS file reader.
32*b8902de1SWarner Losh  */
33*b8902de1SWarner Losh 
34*b8902de1SWarner Losh #include <sys/stat.h>
35*b8902de1SWarner Losh #include <sys/stdint.h>
36*b8902de1SWarner Losh 
37*b8902de1SWarner Losh #include "zfsimpl.h"
38*b8902de1SWarner Losh #include "zfssubr.c"
39*b8902de1SWarner Losh 
40*b8902de1SWarner Losh 
41*b8902de1SWarner Losh struct zfsmount {
42*b8902de1SWarner Losh 	const spa_t	*spa;
43*b8902de1SWarner Losh 	objset_phys_t	objset;
44*b8902de1SWarner Losh 	uint64_t	rootobj;
45*b8902de1SWarner Losh };
46*b8902de1SWarner Losh static struct zfsmount zfsmount __unused;
47*b8902de1SWarner Losh 
48*b8902de1SWarner Losh /*
49*b8902de1SWarner Losh  * List of all vdevs, chained through v_alllink.
50*b8902de1SWarner Losh  */
51*b8902de1SWarner Losh static vdev_list_t zfs_vdevs;
52*b8902de1SWarner Losh 
53*b8902de1SWarner Losh  /*
54*b8902de1SWarner Losh  * List of ZFS features supported for read
55*b8902de1SWarner Losh  */
56*b8902de1SWarner Losh static const char *features_for_read[] = {
57*b8902de1SWarner Losh 	"org.illumos:lz4_compress",
58*b8902de1SWarner Losh 	"com.delphix:hole_birth",
59*b8902de1SWarner Losh 	"com.delphix:extensible_dataset",
60*b8902de1SWarner Losh 	"com.delphix:embedded_data",
61*b8902de1SWarner Losh 	"org.open-zfs:large_blocks",
62*b8902de1SWarner Losh 	"org.illumos:sha512",
63*b8902de1SWarner Losh 	"org.illumos:skein",
64*b8902de1SWarner Losh 	"org.zfsonlinux:large_dnode",
65*b8902de1SWarner Losh 	NULL
66*b8902de1SWarner Losh };
67*b8902de1SWarner Losh 
68*b8902de1SWarner Losh /*
69*b8902de1SWarner Losh  * List of all pools, chained through spa_link.
70*b8902de1SWarner Losh  */
71*b8902de1SWarner Losh static spa_list_t zfs_pools;
72*b8902de1SWarner Losh 
73*b8902de1SWarner Losh static const dnode_phys_t *dnode_cache_obj;
74*b8902de1SWarner Losh static uint64_t dnode_cache_bn;
75*b8902de1SWarner Losh static char *dnode_cache_buf;
76*b8902de1SWarner Losh static char *zap_scratch;
77*b8902de1SWarner Losh static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
78*b8902de1SWarner Losh 
79*b8902de1SWarner Losh #define TEMP_SIZE	(1024 * 1024)
80*b8902de1SWarner Losh 
81*b8902de1SWarner Losh static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
82*b8902de1SWarner Losh static int zfs_get_root(const spa_t *spa, uint64_t *objid);
83*b8902de1SWarner Losh static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
84*b8902de1SWarner Losh static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
85*b8902de1SWarner Losh     const char *name, uint64_t integer_size, uint64_t num_integers,
86*b8902de1SWarner Losh     void *value);
87*b8902de1SWarner Losh 
88*b8902de1SWarner Losh static void
89*b8902de1SWarner Losh zfs_init(void)
90*b8902de1SWarner Losh {
91*b8902de1SWarner Losh 	STAILQ_INIT(&zfs_vdevs);
92*b8902de1SWarner Losh 	STAILQ_INIT(&zfs_pools);
93*b8902de1SWarner Losh 
94*b8902de1SWarner Losh 	zfs_temp_buf = malloc(TEMP_SIZE);
95*b8902de1SWarner Losh 	zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
96*b8902de1SWarner Losh 	zfs_temp_ptr = zfs_temp_buf;
97*b8902de1SWarner Losh 	dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
98*b8902de1SWarner Losh 	zap_scratch = malloc(SPA_MAXBLOCKSIZE);
99*b8902de1SWarner Losh 
100*b8902de1SWarner Losh 	zfs_init_crc();
101*b8902de1SWarner Losh }
102*b8902de1SWarner Losh 
103*b8902de1SWarner Losh static void *
104*b8902de1SWarner Losh zfs_alloc(size_t size)
105*b8902de1SWarner Losh {
106*b8902de1SWarner Losh 	char *ptr;
107*b8902de1SWarner Losh 
108*b8902de1SWarner Losh 	if (zfs_temp_ptr + size > zfs_temp_end) {
109*b8902de1SWarner Losh 		printf("ZFS: out of temporary buffer space\n");
110*b8902de1SWarner Losh 		for (;;) ;
111*b8902de1SWarner Losh 	}
112*b8902de1SWarner Losh 	ptr = zfs_temp_ptr;
113*b8902de1SWarner Losh 	zfs_temp_ptr += size;
114*b8902de1SWarner Losh 
115*b8902de1SWarner Losh 	return (ptr);
116*b8902de1SWarner Losh }
117*b8902de1SWarner Losh 
118*b8902de1SWarner Losh static void
119*b8902de1SWarner Losh zfs_free(void *ptr, size_t size)
120*b8902de1SWarner Losh {
121*b8902de1SWarner Losh 
122*b8902de1SWarner Losh 	zfs_temp_ptr -= size;
123*b8902de1SWarner Losh 	if (zfs_temp_ptr != ptr) {
124*b8902de1SWarner Losh 		printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
125*b8902de1SWarner Losh 		for (;;) ;
126*b8902de1SWarner Losh 	}
127*b8902de1SWarner Losh }
128*b8902de1SWarner Losh 
129*b8902de1SWarner Losh static int
130*b8902de1SWarner Losh xdr_int(const unsigned char **xdr, int *ip)
131*b8902de1SWarner Losh {
132*b8902de1SWarner Losh 	*ip = ((*xdr)[0] << 24)
133*b8902de1SWarner Losh 		| ((*xdr)[1] << 16)
134*b8902de1SWarner Losh 		| ((*xdr)[2] << 8)
135*b8902de1SWarner Losh 		| ((*xdr)[3] << 0);
136*b8902de1SWarner Losh 	(*xdr) += 4;
137*b8902de1SWarner Losh 	return (0);
138*b8902de1SWarner Losh }
139*b8902de1SWarner Losh 
140*b8902de1SWarner Losh static int
141*b8902de1SWarner Losh xdr_u_int(const unsigned char **xdr, u_int *ip)
142*b8902de1SWarner Losh {
143*b8902de1SWarner Losh 	*ip = ((*xdr)[0] << 24)
144*b8902de1SWarner Losh 		| ((*xdr)[1] << 16)
145*b8902de1SWarner Losh 		| ((*xdr)[2] << 8)
146*b8902de1SWarner Losh 		| ((*xdr)[3] << 0);
147*b8902de1SWarner Losh 	(*xdr) += 4;
148*b8902de1SWarner Losh 	return (0);
149*b8902de1SWarner Losh }
150*b8902de1SWarner Losh 
151*b8902de1SWarner Losh static int
152*b8902de1SWarner Losh xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
153*b8902de1SWarner Losh {
154*b8902de1SWarner Losh 	u_int hi, lo;
155*b8902de1SWarner Losh 
156*b8902de1SWarner Losh 	xdr_u_int(xdr, &hi);
157*b8902de1SWarner Losh 	xdr_u_int(xdr, &lo);
158*b8902de1SWarner Losh 	*lp = (((uint64_t) hi) << 32) | lo;
159*b8902de1SWarner Losh 	return (0);
160*b8902de1SWarner Losh }
161*b8902de1SWarner Losh 
162*b8902de1SWarner Losh static int
163*b8902de1SWarner Losh nvlist_find(const unsigned char *nvlist, const char *name, int type,
164*b8902de1SWarner Losh 	    int* elementsp, void *valuep)
165*b8902de1SWarner Losh {
166*b8902de1SWarner Losh 	const unsigned char *p, *pair;
167*b8902de1SWarner Losh 	int junk;
168*b8902de1SWarner Losh 	int encoded_size, decoded_size;
169*b8902de1SWarner Losh 
170*b8902de1SWarner Losh 	p = nvlist;
171*b8902de1SWarner Losh 	xdr_int(&p, &junk);
172*b8902de1SWarner Losh 	xdr_int(&p, &junk);
173*b8902de1SWarner Losh 
174*b8902de1SWarner Losh 	pair = p;
175*b8902de1SWarner Losh 	xdr_int(&p, &encoded_size);
176*b8902de1SWarner Losh 	xdr_int(&p, &decoded_size);
177*b8902de1SWarner Losh 	while (encoded_size && decoded_size) {
178*b8902de1SWarner Losh 		int namelen, pairtype, elements;
179*b8902de1SWarner Losh 		const char *pairname;
180*b8902de1SWarner Losh 
181*b8902de1SWarner Losh 		xdr_int(&p, &namelen);
182*b8902de1SWarner Losh 		pairname = (const char*) p;
183*b8902de1SWarner Losh 		p += roundup(namelen, 4);
184*b8902de1SWarner Losh 		xdr_int(&p, &pairtype);
185*b8902de1SWarner Losh 
186*b8902de1SWarner Losh 		if (!memcmp(name, pairname, namelen) && type == pairtype) {
187*b8902de1SWarner Losh 			xdr_int(&p, &elements);
188*b8902de1SWarner Losh 			if (elementsp)
189*b8902de1SWarner Losh 				*elementsp = elements;
190*b8902de1SWarner Losh 			if (type == DATA_TYPE_UINT64) {
191*b8902de1SWarner Losh 				xdr_uint64_t(&p, (uint64_t *) valuep);
192*b8902de1SWarner Losh 				return (0);
193*b8902de1SWarner Losh 			} else if (type == DATA_TYPE_STRING) {
194*b8902de1SWarner Losh 				int len;
195*b8902de1SWarner Losh 				xdr_int(&p, &len);
196*b8902de1SWarner Losh 				(*(const char**) valuep) = (const char*) p;
197*b8902de1SWarner Losh 				return (0);
198*b8902de1SWarner Losh 			} else if (type == DATA_TYPE_NVLIST
199*b8902de1SWarner Losh 				   || type == DATA_TYPE_NVLIST_ARRAY) {
200*b8902de1SWarner Losh 				(*(const unsigned char**) valuep) =
201*b8902de1SWarner Losh 					 (const unsigned char*) p;
202*b8902de1SWarner Losh 				return (0);
203*b8902de1SWarner Losh 			} else {
204*b8902de1SWarner Losh 				return (EIO);
205*b8902de1SWarner Losh 			}
206*b8902de1SWarner Losh 		} else {
207*b8902de1SWarner Losh 			/*
208*b8902de1SWarner Losh 			 * Not the pair we are looking for, skip to the next one.
209*b8902de1SWarner Losh 			 */
210*b8902de1SWarner Losh 			p = pair + encoded_size;
211*b8902de1SWarner Losh 		}
212*b8902de1SWarner Losh 
213*b8902de1SWarner Losh 		pair = p;
214*b8902de1SWarner Losh 		xdr_int(&p, &encoded_size);
215*b8902de1SWarner Losh 		xdr_int(&p, &decoded_size);
216*b8902de1SWarner Losh 	}
217*b8902de1SWarner Losh 
218*b8902de1SWarner Losh 	return (EIO);
219*b8902de1SWarner Losh }
220*b8902de1SWarner Losh 
221*b8902de1SWarner Losh static int
222*b8902de1SWarner Losh nvlist_check_features_for_read(const unsigned char *nvlist)
223*b8902de1SWarner Losh {
224*b8902de1SWarner Losh 	const unsigned char *p, *pair;
225*b8902de1SWarner Losh 	int junk;
226*b8902de1SWarner Losh 	int encoded_size, decoded_size;
227*b8902de1SWarner Losh 	int rc;
228*b8902de1SWarner Losh 
229*b8902de1SWarner Losh 	rc = 0;
230*b8902de1SWarner Losh 
231*b8902de1SWarner Losh 	p = nvlist;
232*b8902de1SWarner Losh 	xdr_int(&p, &junk);
233*b8902de1SWarner Losh 	xdr_int(&p, &junk);
234*b8902de1SWarner Losh 
235*b8902de1SWarner Losh 	pair = p;
236*b8902de1SWarner Losh 	xdr_int(&p, &encoded_size);
237*b8902de1SWarner Losh 	xdr_int(&p, &decoded_size);
238*b8902de1SWarner Losh 	while (encoded_size && decoded_size) {
239*b8902de1SWarner Losh 		int namelen, pairtype;
240*b8902de1SWarner Losh 		const char *pairname;
241*b8902de1SWarner Losh 		int i, found;
242*b8902de1SWarner Losh 
243*b8902de1SWarner Losh 		found = 0;
244*b8902de1SWarner Losh 
245*b8902de1SWarner Losh 		xdr_int(&p, &namelen);
246*b8902de1SWarner Losh 		pairname = (const char*) p;
247*b8902de1SWarner Losh 		p += roundup(namelen, 4);
248*b8902de1SWarner Losh 		xdr_int(&p, &pairtype);
249*b8902de1SWarner Losh 
250*b8902de1SWarner Losh 		for (i = 0; features_for_read[i] != NULL; i++) {
251*b8902de1SWarner Losh 			if (!memcmp(pairname, features_for_read[i], namelen)) {
252*b8902de1SWarner Losh 				found = 1;
253*b8902de1SWarner Losh 				break;
254*b8902de1SWarner Losh 			}
255*b8902de1SWarner Losh 		}
256*b8902de1SWarner Losh 
257*b8902de1SWarner Losh 		if (!found) {
258*b8902de1SWarner Losh 			printf("ZFS: unsupported feature: %s\n", pairname);
259*b8902de1SWarner Losh 			rc = EIO;
260*b8902de1SWarner Losh 		}
261*b8902de1SWarner Losh 
262*b8902de1SWarner Losh 		p = pair + encoded_size;
263*b8902de1SWarner Losh 
264*b8902de1SWarner Losh 		pair = p;
265*b8902de1SWarner Losh 		xdr_int(&p, &encoded_size);
266*b8902de1SWarner Losh 		xdr_int(&p, &decoded_size);
267*b8902de1SWarner Losh 	}
268*b8902de1SWarner Losh 
269*b8902de1SWarner Losh 	return (rc);
270*b8902de1SWarner Losh }
271*b8902de1SWarner Losh 
272*b8902de1SWarner Losh /*
273*b8902de1SWarner Losh  * Return the next nvlist in an nvlist array.
274*b8902de1SWarner Losh  */
275*b8902de1SWarner Losh static const unsigned char *
276*b8902de1SWarner Losh nvlist_next(const unsigned char *nvlist)
277*b8902de1SWarner Losh {
278*b8902de1SWarner Losh 	const unsigned char *p, *pair;
279*b8902de1SWarner Losh 	int junk;
280*b8902de1SWarner Losh 	int encoded_size, decoded_size;
281*b8902de1SWarner Losh 
282*b8902de1SWarner Losh 	p = nvlist;
283*b8902de1SWarner Losh 	xdr_int(&p, &junk);
284*b8902de1SWarner Losh 	xdr_int(&p, &junk);
285*b8902de1SWarner Losh 
286*b8902de1SWarner Losh 	pair = p;
287*b8902de1SWarner Losh 	xdr_int(&p, &encoded_size);
288*b8902de1SWarner Losh 	xdr_int(&p, &decoded_size);
289*b8902de1SWarner Losh 	while (encoded_size && decoded_size) {
290*b8902de1SWarner Losh 		p = pair + encoded_size;
291*b8902de1SWarner Losh 
292*b8902de1SWarner Losh 		pair = p;
293*b8902de1SWarner Losh 		xdr_int(&p, &encoded_size);
294*b8902de1SWarner Losh 		xdr_int(&p, &decoded_size);
295*b8902de1SWarner Losh 	}
296*b8902de1SWarner Losh 
297*b8902de1SWarner Losh 	return p;
298*b8902de1SWarner Losh }
299*b8902de1SWarner Losh 
300*b8902de1SWarner Losh #ifdef TEST
301*b8902de1SWarner Losh 
302*b8902de1SWarner Losh static const unsigned char *
303*b8902de1SWarner Losh nvlist_print(const unsigned char *nvlist, unsigned int indent)
304*b8902de1SWarner Losh {
305*b8902de1SWarner Losh 	static const char* typenames[] = {
306*b8902de1SWarner Losh 		"DATA_TYPE_UNKNOWN",
307*b8902de1SWarner Losh 		"DATA_TYPE_BOOLEAN",
308*b8902de1SWarner Losh 		"DATA_TYPE_BYTE",
309*b8902de1SWarner Losh 		"DATA_TYPE_INT16",
310*b8902de1SWarner Losh 		"DATA_TYPE_UINT16",
311*b8902de1SWarner Losh 		"DATA_TYPE_INT32",
312*b8902de1SWarner Losh 		"DATA_TYPE_UINT32",
313*b8902de1SWarner Losh 		"DATA_TYPE_INT64",
314*b8902de1SWarner Losh 		"DATA_TYPE_UINT64",
315*b8902de1SWarner Losh 		"DATA_TYPE_STRING",
316*b8902de1SWarner Losh 		"DATA_TYPE_BYTE_ARRAY",
317*b8902de1SWarner Losh 		"DATA_TYPE_INT16_ARRAY",
318*b8902de1SWarner Losh 		"DATA_TYPE_UINT16_ARRAY",
319*b8902de1SWarner Losh 		"DATA_TYPE_INT32_ARRAY",
320*b8902de1SWarner Losh 		"DATA_TYPE_UINT32_ARRAY",
321*b8902de1SWarner Losh 		"DATA_TYPE_INT64_ARRAY",
322*b8902de1SWarner Losh 		"DATA_TYPE_UINT64_ARRAY",
323*b8902de1SWarner Losh 		"DATA_TYPE_STRING_ARRAY",
324*b8902de1SWarner Losh 		"DATA_TYPE_HRTIME",
325*b8902de1SWarner Losh 		"DATA_TYPE_NVLIST",
326*b8902de1SWarner Losh 		"DATA_TYPE_NVLIST_ARRAY",
327*b8902de1SWarner Losh 		"DATA_TYPE_BOOLEAN_VALUE",
328*b8902de1SWarner Losh 		"DATA_TYPE_INT8",
329*b8902de1SWarner Losh 		"DATA_TYPE_UINT8",
330*b8902de1SWarner Losh 		"DATA_TYPE_BOOLEAN_ARRAY",
331*b8902de1SWarner Losh 		"DATA_TYPE_INT8_ARRAY",
332*b8902de1SWarner Losh 		"DATA_TYPE_UINT8_ARRAY"
333*b8902de1SWarner Losh 	};
334*b8902de1SWarner Losh 
335*b8902de1SWarner Losh 	unsigned int i, j;
336*b8902de1SWarner Losh 	const unsigned char *p, *pair;
337*b8902de1SWarner Losh 	int junk;
338*b8902de1SWarner Losh 	int encoded_size, decoded_size;
339*b8902de1SWarner Losh 
340*b8902de1SWarner Losh 	p = nvlist;
341*b8902de1SWarner Losh 	xdr_int(&p, &junk);
342*b8902de1SWarner Losh 	xdr_int(&p, &junk);
343*b8902de1SWarner Losh 
344*b8902de1SWarner Losh 	pair = p;
345*b8902de1SWarner Losh 	xdr_int(&p, &encoded_size);
346*b8902de1SWarner Losh 	xdr_int(&p, &decoded_size);
347*b8902de1SWarner Losh 	while (encoded_size && decoded_size) {
348*b8902de1SWarner Losh 		int namelen, pairtype, elements;
349*b8902de1SWarner Losh 		const char *pairname;
350*b8902de1SWarner Losh 
351*b8902de1SWarner Losh 		xdr_int(&p, &namelen);
352*b8902de1SWarner Losh 		pairname = (const char*) p;
353*b8902de1SWarner Losh 		p += roundup(namelen, 4);
354*b8902de1SWarner Losh 		xdr_int(&p, &pairtype);
355*b8902de1SWarner Losh 
356*b8902de1SWarner Losh 		for (i = 0; i < indent; i++)
357*b8902de1SWarner Losh 			printf(" ");
358*b8902de1SWarner Losh 		printf("%s %s", typenames[pairtype], pairname);
359*b8902de1SWarner Losh 
360*b8902de1SWarner Losh 		xdr_int(&p, &elements);
361*b8902de1SWarner Losh 		switch (pairtype) {
362*b8902de1SWarner Losh 		case DATA_TYPE_UINT64: {
363*b8902de1SWarner Losh 			uint64_t val;
364*b8902de1SWarner Losh 			xdr_uint64_t(&p, &val);
365*b8902de1SWarner Losh 			printf(" = 0x%jx\n", (uintmax_t)val);
366*b8902de1SWarner Losh 			break;
367*b8902de1SWarner Losh 		}
368*b8902de1SWarner Losh 
369*b8902de1SWarner Losh 		case DATA_TYPE_STRING: {
370*b8902de1SWarner Losh 			int len;
371*b8902de1SWarner Losh 			xdr_int(&p, &len);
372*b8902de1SWarner Losh 			printf(" = \"%s\"\n", p);
373*b8902de1SWarner Losh 			break;
374*b8902de1SWarner Losh 		}
375*b8902de1SWarner Losh 
376*b8902de1SWarner Losh 		case DATA_TYPE_NVLIST:
377*b8902de1SWarner Losh 			printf("\n");
378*b8902de1SWarner Losh 			nvlist_print(p, indent + 1);
379*b8902de1SWarner Losh 			break;
380*b8902de1SWarner Losh 
381*b8902de1SWarner Losh 		case DATA_TYPE_NVLIST_ARRAY:
382*b8902de1SWarner Losh 			for (j = 0; j < elements; j++) {
383*b8902de1SWarner Losh 				printf("[%d]\n", j);
384*b8902de1SWarner Losh 				p = nvlist_print(p, indent + 1);
385*b8902de1SWarner Losh 				if (j != elements - 1) {
386*b8902de1SWarner Losh 					for (i = 0; i < indent; i++)
387*b8902de1SWarner Losh 						printf(" ");
388*b8902de1SWarner Losh 					printf("%s %s", typenames[pairtype], pairname);
389*b8902de1SWarner Losh 				}
390*b8902de1SWarner Losh 			}
391*b8902de1SWarner Losh 			break;
392*b8902de1SWarner Losh 
393*b8902de1SWarner Losh 		default:
394*b8902de1SWarner Losh 			printf("\n");
395*b8902de1SWarner Losh 		}
396*b8902de1SWarner Losh 
397*b8902de1SWarner Losh 		p = pair + encoded_size;
398*b8902de1SWarner Losh 
399*b8902de1SWarner Losh 		pair = p;
400*b8902de1SWarner Losh 		xdr_int(&p, &encoded_size);
401*b8902de1SWarner Losh 		xdr_int(&p, &decoded_size);
402*b8902de1SWarner Losh 	}
403*b8902de1SWarner Losh 
404*b8902de1SWarner Losh 	return p;
405*b8902de1SWarner Losh }
406*b8902de1SWarner Losh 
407*b8902de1SWarner Losh #endif
408*b8902de1SWarner Losh 
409*b8902de1SWarner Losh static int
410*b8902de1SWarner Losh vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
411*b8902de1SWarner Losh     off_t offset, size_t size)
412*b8902de1SWarner Losh {
413*b8902de1SWarner Losh 	size_t psize;
414*b8902de1SWarner Losh 	int rc;
415*b8902de1SWarner Losh 
416*b8902de1SWarner Losh 	if (!vdev->v_phys_read)
417*b8902de1SWarner Losh 		return (EIO);
418*b8902de1SWarner Losh 
419*b8902de1SWarner Losh 	if (bp) {
420*b8902de1SWarner Losh 		psize = BP_GET_PSIZE(bp);
421*b8902de1SWarner Losh 	} else {
422*b8902de1SWarner Losh 		psize = size;
423*b8902de1SWarner Losh 	}
424*b8902de1SWarner Losh 
425*b8902de1SWarner Losh 	/*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
426*b8902de1SWarner Losh 	rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
427*b8902de1SWarner Losh 	if (rc)
428*b8902de1SWarner Losh 		return (rc);
429*b8902de1SWarner Losh 	if (bp && zio_checksum_verify(vdev->spa, bp, buf))
430*b8902de1SWarner Losh 		return (EIO);
431*b8902de1SWarner Losh 
432*b8902de1SWarner Losh 	return (0);
433*b8902de1SWarner Losh }
434*b8902de1SWarner Losh 
435*b8902de1SWarner Losh static int
436*b8902de1SWarner Losh vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
437*b8902de1SWarner Losh     off_t offset, size_t bytes)
438*b8902de1SWarner Losh {
439*b8902de1SWarner Losh 
440*b8902de1SWarner Losh 	return (vdev_read_phys(vdev, bp, buf,
441*b8902de1SWarner Losh 		offset + VDEV_LABEL_START_SIZE, bytes));
442*b8902de1SWarner Losh }
443*b8902de1SWarner Losh 
444*b8902de1SWarner Losh 
445*b8902de1SWarner Losh static int
446*b8902de1SWarner Losh vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
447*b8902de1SWarner Losh     off_t offset, size_t bytes)
448*b8902de1SWarner Losh {
449*b8902de1SWarner Losh 	vdev_t *kid;
450*b8902de1SWarner Losh 	int rc;
451*b8902de1SWarner Losh 
452*b8902de1SWarner Losh 	rc = EIO;
453*b8902de1SWarner Losh 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
454*b8902de1SWarner Losh 		if (kid->v_state != VDEV_STATE_HEALTHY)
455*b8902de1SWarner Losh 			continue;
456*b8902de1SWarner Losh 		rc = kid->v_read(kid, bp, buf, offset, bytes);
457*b8902de1SWarner Losh 		if (!rc)
458*b8902de1SWarner Losh 			return (0);
459*b8902de1SWarner Losh 	}
460*b8902de1SWarner Losh 
461*b8902de1SWarner Losh 	return (rc);
462*b8902de1SWarner Losh }
463*b8902de1SWarner Losh 
464*b8902de1SWarner Losh static int
465*b8902de1SWarner Losh vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
466*b8902de1SWarner Losh     off_t offset, size_t bytes)
467*b8902de1SWarner Losh {
468*b8902de1SWarner Losh 	vdev_t *kid;
469*b8902de1SWarner Losh 
470*b8902de1SWarner Losh 	/*
471*b8902de1SWarner Losh 	 * Here we should have two kids:
472*b8902de1SWarner Losh 	 * First one which is the one we are replacing and we can trust
473*b8902de1SWarner Losh 	 * only this one to have valid data, but it might not be present.
474*b8902de1SWarner Losh 	 * Second one is that one we are replacing with. It is most likely
475*b8902de1SWarner Losh 	 * healthy, but we can't trust it has needed data, so we won't use it.
476*b8902de1SWarner Losh 	 */
477*b8902de1SWarner Losh 	kid = STAILQ_FIRST(&vdev->v_children);
478*b8902de1SWarner Losh 	if (kid == NULL)
479*b8902de1SWarner Losh 		return (EIO);
480*b8902de1SWarner Losh 	if (kid->v_state != VDEV_STATE_HEALTHY)
481*b8902de1SWarner Losh 		return (EIO);
482*b8902de1SWarner Losh 	return (kid->v_read(kid, bp, buf, offset, bytes));
483*b8902de1SWarner Losh }
484*b8902de1SWarner Losh 
485*b8902de1SWarner Losh static vdev_t *
486*b8902de1SWarner Losh vdev_find(uint64_t guid)
487*b8902de1SWarner Losh {
488*b8902de1SWarner Losh 	vdev_t *vdev;
489*b8902de1SWarner Losh 
490*b8902de1SWarner Losh 	STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
491*b8902de1SWarner Losh 		if (vdev->v_guid == guid)
492*b8902de1SWarner Losh 			return (vdev);
493*b8902de1SWarner Losh 
494*b8902de1SWarner Losh 	return (0);
495*b8902de1SWarner Losh }
496*b8902de1SWarner Losh 
497*b8902de1SWarner Losh static vdev_t *
498*b8902de1SWarner Losh vdev_create(uint64_t guid, vdev_read_t *_read)
499*b8902de1SWarner Losh {
500*b8902de1SWarner Losh 	vdev_t *vdev;
501*b8902de1SWarner Losh 
502*b8902de1SWarner Losh 	vdev = malloc(sizeof(vdev_t));
503*b8902de1SWarner Losh 	memset(vdev, 0, sizeof(vdev_t));
504*b8902de1SWarner Losh 	STAILQ_INIT(&vdev->v_children);
505*b8902de1SWarner Losh 	vdev->v_guid = guid;
506*b8902de1SWarner Losh 	vdev->v_state = VDEV_STATE_OFFLINE;
507*b8902de1SWarner Losh 	vdev->v_read = _read;
508*b8902de1SWarner Losh 	vdev->v_phys_read = 0;
509*b8902de1SWarner Losh 	vdev->v_read_priv = 0;
510*b8902de1SWarner Losh 	STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
511*b8902de1SWarner Losh 
512*b8902de1SWarner Losh 	return (vdev);
513*b8902de1SWarner Losh }
514*b8902de1SWarner Losh 
515*b8902de1SWarner Losh static int
516*b8902de1SWarner Losh vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
517*b8902de1SWarner Losh     vdev_t **vdevp, int is_newer)
518*b8902de1SWarner Losh {
519*b8902de1SWarner Losh 	int rc;
520*b8902de1SWarner Losh 	uint64_t guid, id, ashift, nparity;
521*b8902de1SWarner Losh 	const char *type;
522*b8902de1SWarner Losh 	const char *path;
523*b8902de1SWarner Losh 	vdev_t *vdev, *kid;
524*b8902de1SWarner Losh 	const unsigned char *kids;
525*b8902de1SWarner Losh 	int nkids, i, is_new;
526*b8902de1SWarner Losh 	uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
527*b8902de1SWarner Losh 
528*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
529*b8902de1SWarner Losh 	    NULL, &guid)
530*b8902de1SWarner Losh 	    || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
531*b8902de1SWarner Losh 	    || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
532*b8902de1SWarner Losh 	    NULL, &type)) {
533*b8902de1SWarner Losh 		printf("ZFS: can't find vdev details\n");
534*b8902de1SWarner Losh 		return (ENOENT);
535*b8902de1SWarner Losh 	}
536*b8902de1SWarner Losh 
537*b8902de1SWarner Losh 	if (strcmp(type, VDEV_TYPE_MIRROR)
538*b8902de1SWarner Losh 	    && strcmp(type, VDEV_TYPE_DISK)
539*b8902de1SWarner Losh #ifdef ZFS_TEST
540*b8902de1SWarner Losh 	    && strcmp(type, VDEV_TYPE_FILE)
541*b8902de1SWarner Losh #endif
542*b8902de1SWarner Losh 	    && strcmp(type, VDEV_TYPE_RAIDZ)
543*b8902de1SWarner Losh 	    && strcmp(type, VDEV_TYPE_REPLACING)) {
544*b8902de1SWarner Losh 		printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
545*b8902de1SWarner Losh 		return (EIO);
546*b8902de1SWarner Losh 	}
547*b8902de1SWarner Losh 
548*b8902de1SWarner Losh 	is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
549*b8902de1SWarner Losh 
550*b8902de1SWarner Losh 	nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
551*b8902de1SWarner Losh 			&is_offline);
552*b8902de1SWarner Losh 	nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
553*b8902de1SWarner Losh 			&is_removed);
554*b8902de1SWarner Losh 	nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
555*b8902de1SWarner Losh 			&is_faulted);
556*b8902de1SWarner Losh 	nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
557*b8902de1SWarner Losh 			&is_degraded);
558*b8902de1SWarner Losh 	nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
559*b8902de1SWarner Losh 			&isnt_present);
560*b8902de1SWarner Losh 
561*b8902de1SWarner Losh 	vdev = vdev_find(guid);
562*b8902de1SWarner Losh 	if (!vdev) {
563*b8902de1SWarner Losh 		is_new = 1;
564*b8902de1SWarner Losh 
565*b8902de1SWarner Losh 		if (!strcmp(type, VDEV_TYPE_MIRROR))
566*b8902de1SWarner Losh 			vdev = vdev_create(guid, vdev_mirror_read);
567*b8902de1SWarner Losh 		else if (!strcmp(type, VDEV_TYPE_RAIDZ))
568*b8902de1SWarner Losh 			vdev = vdev_create(guid, vdev_raidz_read);
569*b8902de1SWarner Losh 		else if (!strcmp(type, VDEV_TYPE_REPLACING))
570*b8902de1SWarner Losh 			vdev = vdev_create(guid, vdev_replacing_read);
571*b8902de1SWarner Losh 		else
572*b8902de1SWarner Losh 			vdev = vdev_create(guid, vdev_disk_read);
573*b8902de1SWarner Losh 
574*b8902de1SWarner Losh 		vdev->v_id = id;
575*b8902de1SWarner Losh 		vdev->v_top = pvdev != NULL ? pvdev : vdev;
576*b8902de1SWarner Losh 		if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
577*b8902de1SWarner Losh 			DATA_TYPE_UINT64, NULL, &ashift) == 0) {
578*b8902de1SWarner Losh 			vdev->v_ashift = ashift;
579*b8902de1SWarner Losh 		} else {
580*b8902de1SWarner Losh 			vdev->v_ashift = 0;
581*b8902de1SWarner Losh 		}
582*b8902de1SWarner Losh 		if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
583*b8902de1SWarner Losh 			DATA_TYPE_UINT64, NULL, &nparity) == 0) {
584*b8902de1SWarner Losh 			vdev->v_nparity = nparity;
585*b8902de1SWarner Losh 		} else {
586*b8902de1SWarner Losh 			vdev->v_nparity = 0;
587*b8902de1SWarner Losh 		}
588*b8902de1SWarner Losh 		if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
589*b8902de1SWarner Losh 				DATA_TYPE_STRING, NULL, &path) == 0) {
590*b8902de1SWarner Losh 			if (strncmp(path, "/dev/", 5) == 0)
591*b8902de1SWarner Losh 				path += 5;
592*b8902de1SWarner Losh 			vdev->v_name = strdup(path);
593*b8902de1SWarner Losh 		} else {
594*b8902de1SWarner Losh 			if (!strcmp(type, "raidz")) {
595*b8902de1SWarner Losh 				if (vdev->v_nparity == 1)
596*b8902de1SWarner Losh 					vdev->v_name = "raidz1";
597*b8902de1SWarner Losh 				else if (vdev->v_nparity == 2)
598*b8902de1SWarner Losh 					vdev->v_name = "raidz2";
599*b8902de1SWarner Losh 				else if (vdev->v_nparity == 3)
600*b8902de1SWarner Losh 					vdev->v_name = "raidz3";
601*b8902de1SWarner Losh 				else {
602*b8902de1SWarner Losh 					printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
603*b8902de1SWarner Losh 					return (EIO);
604*b8902de1SWarner Losh 				}
605*b8902de1SWarner Losh 			} else {
606*b8902de1SWarner Losh 				vdev->v_name = strdup(type);
607*b8902de1SWarner Losh 			}
608*b8902de1SWarner Losh 		}
609*b8902de1SWarner Losh 	} else {
610*b8902de1SWarner Losh 		is_new = 0;
611*b8902de1SWarner Losh 	}
612*b8902de1SWarner Losh 
613*b8902de1SWarner Losh 	if (is_new || is_newer) {
614*b8902de1SWarner Losh 		/*
615*b8902de1SWarner Losh 		 * This is either new vdev or we've already seen this vdev,
616*b8902de1SWarner Losh 		 * but from an older vdev label, so let's refresh its state
617*b8902de1SWarner Losh 		 * from the newer label.
618*b8902de1SWarner Losh 		 */
619*b8902de1SWarner Losh 		if (is_offline)
620*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_OFFLINE;
621*b8902de1SWarner Losh 		else if (is_removed)
622*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_REMOVED;
623*b8902de1SWarner Losh 		else if (is_faulted)
624*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_FAULTED;
625*b8902de1SWarner Losh 		else if (is_degraded)
626*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_DEGRADED;
627*b8902de1SWarner Losh 		else if (isnt_present)
628*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_CANT_OPEN;
629*b8902de1SWarner Losh 	}
630*b8902de1SWarner Losh 
631*b8902de1SWarner Losh 	rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
632*b8902de1SWarner Losh 	    &nkids, &kids);
633*b8902de1SWarner Losh 	/*
634*b8902de1SWarner Losh 	 * Its ok if we don't have any kids.
635*b8902de1SWarner Losh 	 */
636*b8902de1SWarner Losh 	if (rc == 0) {
637*b8902de1SWarner Losh 		vdev->v_nchildren = nkids;
638*b8902de1SWarner Losh 		for (i = 0; i < nkids; i++) {
639*b8902de1SWarner Losh 			rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
640*b8902de1SWarner Losh 			if (rc)
641*b8902de1SWarner Losh 				return (rc);
642*b8902de1SWarner Losh 			if (is_new)
643*b8902de1SWarner Losh 				STAILQ_INSERT_TAIL(&vdev->v_children, kid,
644*b8902de1SWarner Losh 						   v_childlink);
645*b8902de1SWarner Losh 			kids = nvlist_next(kids);
646*b8902de1SWarner Losh 		}
647*b8902de1SWarner Losh 	} else {
648*b8902de1SWarner Losh 		vdev->v_nchildren = 0;
649*b8902de1SWarner Losh 	}
650*b8902de1SWarner Losh 
651*b8902de1SWarner Losh 	if (vdevp)
652*b8902de1SWarner Losh 		*vdevp = vdev;
653*b8902de1SWarner Losh 	return (0);
654*b8902de1SWarner Losh }
655*b8902de1SWarner Losh 
656*b8902de1SWarner Losh static void
657*b8902de1SWarner Losh vdev_set_state(vdev_t *vdev)
658*b8902de1SWarner Losh {
659*b8902de1SWarner Losh 	vdev_t *kid;
660*b8902de1SWarner Losh 	int good_kids;
661*b8902de1SWarner Losh 	int bad_kids;
662*b8902de1SWarner Losh 
663*b8902de1SWarner Losh 	/*
664*b8902de1SWarner Losh 	 * A mirror or raidz is healthy if all its kids are healthy. A
665*b8902de1SWarner Losh 	 * mirror is degraded if any of its kids is healthy; a raidz
666*b8902de1SWarner Losh 	 * is degraded if at most nparity kids are offline.
667*b8902de1SWarner Losh 	 */
668*b8902de1SWarner Losh 	if (STAILQ_FIRST(&vdev->v_children)) {
669*b8902de1SWarner Losh 		good_kids = 0;
670*b8902de1SWarner Losh 		bad_kids = 0;
671*b8902de1SWarner Losh 		STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
672*b8902de1SWarner Losh 			if (kid->v_state == VDEV_STATE_HEALTHY)
673*b8902de1SWarner Losh 				good_kids++;
674*b8902de1SWarner Losh 			else
675*b8902de1SWarner Losh 				bad_kids++;
676*b8902de1SWarner Losh 		}
677*b8902de1SWarner Losh 		if (bad_kids == 0) {
678*b8902de1SWarner Losh 			vdev->v_state = VDEV_STATE_HEALTHY;
679*b8902de1SWarner Losh 		} else {
680*b8902de1SWarner Losh 			if (vdev->v_read == vdev_mirror_read) {
681*b8902de1SWarner Losh 				if (good_kids) {
682*b8902de1SWarner Losh 					vdev->v_state = VDEV_STATE_DEGRADED;
683*b8902de1SWarner Losh 				} else {
684*b8902de1SWarner Losh 					vdev->v_state = VDEV_STATE_OFFLINE;
685*b8902de1SWarner Losh 				}
686*b8902de1SWarner Losh 			} else if (vdev->v_read == vdev_raidz_read) {
687*b8902de1SWarner Losh 				if (bad_kids > vdev->v_nparity) {
688*b8902de1SWarner Losh 					vdev->v_state = VDEV_STATE_OFFLINE;
689*b8902de1SWarner Losh 				} else {
690*b8902de1SWarner Losh 					vdev->v_state = VDEV_STATE_DEGRADED;
691*b8902de1SWarner Losh 				}
692*b8902de1SWarner Losh 			}
693*b8902de1SWarner Losh 		}
694*b8902de1SWarner Losh 	}
695*b8902de1SWarner Losh }
696*b8902de1SWarner Losh 
697*b8902de1SWarner Losh static spa_t *
698*b8902de1SWarner Losh spa_find_by_guid(uint64_t guid)
699*b8902de1SWarner Losh {
700*b8902de1SWarner Losh 	spa_t *spa;
701*b8902de1SWarner Losh 
702*b8902de1SWarner Losh 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
703*b8902de1SWarner Losh 		if (spa->spa_guid == guid)
704*b8902de1SWarner Losh 			return (spa);
705*b8902de1SWarner Losh 
706*b8902de1SWarner Losh 	return (0);
707*b8902de1SWarner Losh }
708*b8902de1SWarner Losh 
709*b8902de1SWarner Losh static spa_t *
710*b8902de1SWarner Losh spa_find_by_name(const char *name)
711*b8902de1SWarner Losh {
712*b8902de1SWarner Losh 	spa_t *spa;
713*b8902de1SWarner Losh 
714*b8902de1SWarner Losh 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
715*b8902de1SWarner Losh 		if (!strcmp(spa->spa_name, name))
716*b8902de1SWarner Losh 			return (spa);
717*b8902de1SWarner Losh 
718*b8902de1SWarner Losh 	return (0);
719*b8902de1SWarner Losh }
720*b8902de1SWarner Losh 
721*b8902de1SWarner Losh #ifdef BOOT2
722*b8902de1SWarner Losh static spa_t *
723*b8902de1SWarner Losh spa_get_primary(void)
724*b8902de1SWarner Losh {
725*b8902de1SWarner Losh 
726*b8902de1SWarner Losh 	return (STAILQ_FIRST(&zfs_pools));
727*b8902de1SWarner Losh }
728*b8902de1SWarner Losh 
729*b8902de1SWarner Losh static vdev_t *
730*b8902de1SWarner Losh spa_get_primary_vdev(const spa_t *spa)
731*b8902de1SWarner Losh {
732*b8902de1SWarner Losh 	vdev_t *vdev;
733*b8902de1SWarner Losh 	vdev_t *kid;
734*b8902de1SWarner Losh 
735*b8902de1SWarner Losh 	if (spa == NULL)
736*b8902de1SWarner Losh 		spa = spa_get_primary();
737*b8902de1SWarner Losh 	if (spa == NULL)
738*b8902de1SWarner Losh 		return (NULL);
739*b8902de1SWarner Losh 	vdev = STAILQ_FIRST(&spa->spa_vdevs);
740*b8902de1SWarner Losh 	if (vdev == NULL)
741*b8902de1SWarner Losh 		return (NULL);
742*b8902de1SWarner Losh 	for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
743*b8902de1SWarner Losh 	     kid = STAILQ_FIRST(&vdev->v_children))
744*b8902de1SWarner Losh 		vdev = kid;
745*b8902de1SWarner Losh 	return (vdev);
746*b8902de1SWarner Losh }
747*b8902de1SWarner Losh #endif
748*b8902de1SWarner Losh 
749*b8902de1SWarner Losh static spa_t *
750*b8902de1SWarner Losh spa_create(uint64_t guid, const char *name)
751*b8902de1SWarner Losh {
752*b8902de1SWarner Losh 	spa_t *spa;
753*b8902de1SWarner Losh 
754*b8902de1SWarner Losh 	if ((spa = malloc(sizeof(spa_t))) == NULL)
755*b8902de1SWarner Losh 		return (NULL);
756*b8902de1SWarner Losh 	memset(spa, 0, sizeof(spa_t));
757*b8902de1SWarner Losh 	if ((spa->spa_name = strdup(name)) == NULL) {
758*b8902de1SWarner Losh 		free(spa);
759*b8902de1SWarner Losh 		return (NULL);
760*b8902de1SWarner Losh 	}
761*b8902de1SWarner Losh 	STAILQ_INIT(&spa->spa_vdevs);
762*b8902de1SWarner Losh 	spa->spa_guid = guid;
763*b8902de1SWarner Losh 	STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
764*b8902de1SWarner Losh 
765*b8902de1SWarner Losh 	return (spa);
766*b8902de1SWarner Losh }
767*b8902de1SWarner Losh 
768*b8902de1SWarner Losh static const char *
769*b8902de1SWarner Losh state_name(vdev_state_t state)
770*b8902de1SWarner Losh {
771*b8902de1SWarner Losh 	static const char* names[] = {
772*b8902de1SWarner Losh 		"UNKNOWN",
773*b8902de1SWarner Losh 		"CLOSED",
774*b8902de1SWarner Losh 		"OFFLINE",
775*b8902de1SWarner Losh 		"REMOVED",
776*b8902de1SWarner Losh 		"CANT_OPEN",
777*b8902de1SWarner Losh 		"FAULTED",
778*b8902de1SWarner Losh 		"DEGRADED",
779*b8902de1SWarner Losh 		"ONLINE"
780*b8902de1SWarner Losh 	};
781*b8902de1SWarner Losh 	return names[state];
782*b8902de1SWarner Losh }
783*b8902de1SWarner Losh 
784*b8902de1SWarner Losh #ifdef BOOT2
785*b8902de1SWarner Losh 
786*b8902de1SWarner Losh #define pager_printf printf
787*b8902de1SWarner Losh 
788*b8902de1SWarner Losh #else
789*b8902de1SWarner Losh 
790*b8902de1SWarner Losh static int
791*b8902de1SWarner Losh pager_printf(const char *fmt, ...)
792*b8902de1SWarner Losh {
793*b8902de1SWarner Losh 	char line[80];
794*b8902de1SWarner Losh 	va_list args;
795*b8902de1SWarner Losh 
796*b8902de1SWarner Losh 	va_start(args, fmt);
797*b8902de1SWarner Losh 	vsprintf(line, fmt, args);
798*b8902de1SWarner Losh 	va_end(args);
799*b8902de1SWarner Losh 
800*b8902de1SWarner Losh 	return (pager_output(line));
801*b8902de1SWarner Losh }
802*b8902de1SWarner Losh 
803*b8902de1SWarner Losh #endif
804*b8902de1SWarner Losh 
805*b8902de1SWarner Losh #define STATUS_FORMAT	"        %s %s\n"
806*b8902de1SWarner Losh 
807*b8902de1SWarner Losh static int
808*b8902de1SWarner Losh print_state(int indent, const char *name, vdev_state_t state)
809*b8902de1SWarner Losh {
810*b8902de1SWarner Losh 	char buf[512];
811*b8902de1SWarner Losh 	int i;
812*b8902de1SWarner Losh 
813*b8902de1SWarner Losh 	buf[0] = 0;
814*b8902de1SWarner Losh 	for (i = 0; i < indent; i++)
815*b8902de1SWarner Losh 		strcat(buf, "  ");
816*b8902de1SWarner Losh 	strcat(buf, name);
817*b8902de1SWarner Losh 
818*b8902de1SWarner Losh 	return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
819*b8902de1SWarner Losh }
820*b8902de1SWarner Losh 
821*b8902de1SWarner Losh static int
822*b8902de1SWarner Losh vdev_status(vdev_t *vdev, int indent)
823*b8902de1SWarner Losh {
824*b8902de1SWarner Losh 	vdev_t *kid;
825*b8902de1SWarner Losh 	int ret;
826*b8902de1SWarner Losh 	ret = print_state(indent, vdev->v_name, vdev->v_state);
827*b8902de1SWarner Losh 	if (ret != 0)
828*b8902de1SWarner Losh 		return (ret);
829*b8902de1SWarner Losh 
830*b8902de1SWarner Losh 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
831*b8902de1SWarner Losh 		ret = vdev_status(kid, indent + 1);
832*b8902de1SWarner Losh 		if (ret != 0)
833*b8902de1SWarner Losh 			return (ret);
834*b8902de1SWarner Losh 	}
835*b8902de1SWarner Losh 	return (ret);
836*b8902de1SWarner Losh }
837*b8902de1SWarner Losh 
838*b8902de1SWarner Losh static int
839*b8902de1SWarner Losh spa_status(spa_t *spa)
840*b8902de1SWarner Losh {
841*b8902de1SWarner Losh 	static char bootfs[ZFS_MAXNAMELEN];
842*b8902de1SWarner Losh 	uint64_t rootid;
843*b8902de1SWarner Losh 	vdev_t *vdev;
844*b8902de1SWarner Losh 	int good_kids, bad_kids, degraded_kids, ret;
845*b8902de1SWarner Losh 	vdev_state_t state;
846*b8902de1SWarner Losh 
847*b8902de1SWarner Losh 	ret = pager_printf("  pool: %s\n", spa->spa_name);
848*b8902de1SWarner Losh 	if (ret != 0)
849*b8902de1SWarner Losh 		return (ret);
850*b8902de1SWarner Losh 
851*b8902de1SWarner Losh 	if (zfs_get_root(spa, &rootid) == 0 &&
852*b8902de1SWarner Losh 	    zfs_rlookup(spa, rootid, bootfs) == 0) {
853*b8902de1SWarner Losh 		if (bootfs[0] == '\0')
854*b8902de1SWarner Losh 			ret = pager_printf("bootfs: %s\n", spa->spa_name);
855*b8902de1SWarner Losh 		else
856*b8902de1SWarner Losh 			ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
857*b8902de1SWarner Losh 			    bootfs);
858*b8902de1SWarner Losh 		if (ret != 0)
859*b8902de1SWarner Losh 			return (ret);
860*b8902de1SWarner Losh 	}
861*b8902de1SWarner Losh 	ret = pager_printf("config:\n\n");
862*b8902de1SWarner Losh 	if (ret != 0)
863*b8902de1SWarner Losh 		return (ret);
864*b8902de1SWarner Losh 	ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
865*b8902de1SWarner Losh 	if (ret != 0)
866*b8902de1SWarner Losh 		return (ret);
867*b8902de1SWarner Losh 
868*b8902de1SWarner Losh 	good_kids = 0;
869*b8902de1SWarner Losh 	degraded_kids = 0;
870*b8902de1SWarner Losh 	bad_kids = 0;
871*b8902de1SWarner Losh 	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
872*b8902de1SWarner Losh 		if (vdev->v_state == VDEV_STATE_HEALTHY)
873*b8902de1SWarner Losh 			good_kids++;
874*b8902de1SWarner Losh 		else if (vdev->v_state == VDEV_STATE_DEGRADED)
875*b8902de1SWarner Losh 			degraded_kids++;
876*b8902de1SWarner Losh 		else
877*b8902de1SWarner Losh 			bad_kids++;
878*b8902de1SWarner Losh 	}
879*b8902de1SWarner Losh 
880*b8902de1SWarner Losh 	state = VDEV_STATE_CLOSED;
881*b8902de1SWarner Losh 	if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
882*b8902de1SWarner Losh 		state = VDEV_STATE_HEALTHY;
883*b8902de1SWarner Losh 	else if ((good_kids + degraded_kids) > 0)
884*b8902de1SWarner Losh 		state = VDEV_STATE_DEGRADED;
885*b8902de1SWarner Losh 
886*b8902de1SWarner Losh 	ret = print_state(0, spa->spa_name, state);
887*b8902de1SWarner Losh 	if (ret != 0)
888*b8902de1SWarner Losh 		return (ret);
889*b8902de1SWarner Losh 	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
890*b8902de1SWarner Losh 		ret = vdev_status(vdev, 1);
891*b8902de1SWarner Losh 		if (ret != 0)
892*b8902de1SWarner Losh 			return (ret);
893*b8902de1SWarner Losh 	}
894*b8902de1SWarner Losh 	return (ret);
895*b8902de1SWarner Losh }
896*b8902de1SWarner Losh 
897*b8902de1SWarner Losh static int
898*b8902de1SWarner Losh spa_all_status(void)
899*b8902de1SWarner Losh {
900*b8902de1SWarner Losh 	spa_t *spa;
901*b8902de1SWarner Losh 	int first = 1, ret = 0;
902*b8902de1SWarner Losh 
903*b8902de1SWarner Losh 	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
904*b8902de1SWarner Losh 		if (!first) {
905*b8902de1SWarner Losh 			ret = pager_printf("\n");
906*b8902de1SWarner Losh 			if (ret != 0)
907*b8902de1SWarner Losh 				return (ret);
908*b8902de1SWarner Losh 		}
909*b8902de1SWarner Losh 		first = 0;
910*b8902de1SWarner Losh 		ret = spa_status(spa);
911*b8902de1SWarner Losh 		if (ret != 0)
912*b8902de1SWarner Losh 			return (ret);
913*b8902de1SWarner Losh 	}
914*b8902de1SWarner Losh 	return (ret);
915*b8902de1SWarner Losh }
916*b8902de1SWarner Losh 
917*b8902de1SWarner Losh static uint64_t
918*b8902de1SWarner Losh vdev_label_offset(uint64_t psize, int l, uint64_t offset)
919*b8902de1SWarner Losh {
920*b8902de1SWarner Losh 	uint64_t label_offset;
921*b8902de1SWarner Losh 
922*b8902de1SWarner Losh 	if (l < VDEV_LABELS / 2)
923*b8902de1SWarner Losh 		label_offset = 0;
924*b8902de1SWarner Losh 	else
925*b8902de1SWarner Losh 		label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
926*b8902de1SWarner Losh 
927*b8902de1SWarner Losh 	return (offset + l * sizeof (vdev_label_t) + label_offset);
928*b8902de1SWarner Losh }
929*b8902de1SWarner Losh 
930*b8902de1SWarner Losh static int
931*b8902de1SWarner Losh vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
932*b8902de1SWarner Losh {
933*b8902de1SWarner Losh 	vdev_t vtmp;
934*b8902de1SWarner Losh 	vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
935*b8902de1SWarner Losh 	vdev_phys_t *tmp_label;
936*b8902de1SWarner Losh 	spa_t *spa;
937*b8902de1SWarner Losh 	vdev_t *vdev, *top_vdev, *pool_vdev;
938*b8902de1SWarner Losh 	off_t off;
939*b8902de1SWarner Losh 	blkptr_t bp;
940*b8902de1SWarner Losh 	const unsigned char *nvlist = NULL;
941*b8902de1SWarner Losh 	uint64_t val;
942*b8902de1SWarner Losh 	uint64_t guid;
943*b8902de1SWarner Losh 	uint64_t best_txg = 0;
944*b8902de1SWarner Losh 	uint64_t pool_txg, pool_guid;
945*b8902de1SWarner Losh 	uint64_t psize;
946*b8902de1SWarner Losh 	const char *pool_name;
947*b8902de1SWarner Losh 	const unsigned char *vdevs;
948*b8902de1SWarner Losh 	const unsigned char *features;
949*b8902de1SWarner Losh 	int i, l, rc, is_newer;
950*b8902de1SWarner Losh 	char *upbuf;
951*b8902de1SWarner Losh 	const struct uberblock *up;
952*b8902de1SWarner Losh 
953*b8902de1SWarner Losh 	/*
954*b8902de1SWarner Losh 	 * Load the vdev label and figure out which
955*b8902de1SWarner Losh 	 * uberblock is most current.
956*b8902de1SWarner Losh 	 */
957*b8902de1SWarner Losh 	memset(&vtmp, 0, sizeof(vtmp));
958*b8902de1SWarner Losh 	vtmp.v_phys_read = _read;
959*b8902de1SWarner Losh 	vtmp.v_read_priv = read_priv;
960*b8902de1SWarner Losh 	psize = P2ALIGN(ldi_get_size(read_priv),
961*b8902de1SWarner Losh 	    (uint64_t)sizeof (vdev_label_t));
962*b8902de1SWarner Losh 
963*b8902de1SWarner Losh 	/* Test for minimum pool size. */
964*b8902de1SWarner Losh 	if (psize < SPA_MINDEVSIZE)
965*b8902de1SWarner Losh 		return (EIO);
966*b8902de1SWarner Losh 
967*b8902de1SWarner Losh 	tmp_label = zfs_alloc(sizeof(vdev_phys_t));
968*b8902de1SWarner Losh 
969*b8902de1SWarner Losh 	for (l = 0; l < VDEV_LABELS; l++) {
970*b8902de1SWarner Losh 		off = vdev_label_offset(psize, l,
971*b8902de1SWarner Losh 		    offsetof(vdev_label_t, vl_vdev_phys));
972*b8902de1SWarner Losh 
973*b8902de1SWarner Losh 		BP_ZERO(&bp);
974*b8902de1SWarner Losh 		BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
975*b8902de1SWarner Losh 		BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
976*b8902de1SWarner Losh 		BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
977*b8902de1SWarner Losh 		BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
978*b8902de1SWarner Losh 		DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
979*b8902de1SWarner Losh 		ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
980*b8902de1SWarner Losh 
981*b8902de1SWarner Losh 		if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
982*b8902de1SWarner Losh 			continue;
983*b8902de1SWarner Losh 
984*b8902de1SWarner Losh 		if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
985*b8902de1SWarner Losh 			continue;
986*b8902de1SWarner Losh 
987*b8902de1SWarner Losh 		nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
988*b8902de1SWarner Losh 		if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
989*b8902de1SWarner Losh 		    DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
990*b8902de1SWarner Losh 			continue;
991*b8902de1SWarner Losh 
992*b8902de1SWarner Losh 		if (best_txg <= pool_txg) {
993*b8902de1SWarner Losh 			best_txg = pool_txg;
994*b8902de1SWarner Losh 			memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
995*b8902de1SWarner Losh 		}
996*b8902de1SWarner Losh 	}
997*b8902de1SWarner Losh 
998*b8902de1SWarner Losh 	zfs_free(tmp_label, sizeof (vdev_phys_t));
999*b8902de1SWarner Losh 
1000*b8902de1SWarner Losh 	if (best_txg == 0)
1001*b8902de1SWarner Losh 		return (EIO);
1002*b8902de1SWarner Losh 
1003*b8902de1SWarner Losh 	if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
1004*b8902de1SWarner Losh 		return (EIO);
1005*b8902de1SWarner Losh 
1006*b8902de1SWarner Losh 	nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1007*b8902de1SWarner Losh 
1008*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1009*b8902de1SWarner Losh 	    NULL, &val) != 0) {
1010*b8902de1SWarner Losh 		return (EIO);
1011*b8902de1SWarner Losh 	}
1012*b8902de1SWarner Losh 
1013*b8902de1SWarner Losh 	if (!SPA_VERSION_IS_SUPPORTED(val)) {
1014*b8902de1SWarner Losh 		printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1015*b8902de1SWarner Losh 		    (unsigned) val, (unsigned) SPA_VERSION);
1016*b8902de1SWarner Losh 		return (EIO);
1017*b8902de1SWarner Losh 	}
1018*b8902de1SWarner Losh 
1019*b8902de1SWarner Losh 	/* Check ZFS features for read */
1020*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1021*b8902de1SWarner Losh 	    DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1022*b8902de1SWarner Losh 	    nvlist_check_features_for_read(features) != 0) {
1023*b8902de1SWarner Losh 		return (EIO);
1024*b8902de1SWarner Losh 	}
1025*b8902de1SWarner Losh 
1026*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1027*b8902de1SWarner Losh 	    NULL, &val) != 0) {
1028*b8902de1SWarner Losh 		return (EIO);
1029*b8902de1SWarner Losh 	}
1030*b8902de1SWarner Losh 
1031*b8902de1SWarner Losh 	if (val == POOL_STATE_DESTROYED) {
1032*b8902de1SWarner Losh 		/* We don't boot only from destroyed pools. */
1033*b8902de1SWarner Losh 		return (EIO);
1034*b8902de1SWarner Losh 	}
1035*b8902de1SWarner Losh 
1036*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1037*b8902de1SWarner Losh 	    NULL, &pool_txg) != 0 ||
1038*b8902de1SWarner Losh 	    nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1039*b8902de1SWarner Losh 	    NULL, &pool_guid) != 0 ||
1040*b8902de1SWarner Losh 	    nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1041*b8902de1SWarner Losh 	    NULL, &pool_name) != 0) {
1042*b8902de1SWarner Losh 		/*
1043*b8902de1SWarner Losh 		 * Cache and spare devices end up here - just ignore
1044*b8902de1SWarner Losh 		 * them.
1045*b8902de1SWarner Losh 		 */
1046*b8902de1SWarner Losh 		/*printf("ZFS: can't find pool details\n");*/
1047*b8902de1SWarner Losh 		return (EIO);
1048*b8902de1SWarner Losh 	}
1049*b8902de1SWarner Losh 
1050*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1051*b8902de1SWarner Losh 	    NULL, &val) == 0 && val != 0) {
1052*b8902de1SWarner Losh 		return (EIO);
1053*b8902de1SWarner Losh 	}
1054*b8902de1SWarner Losh 
1055*b8902de1SWarner Losh 	/*
1056*b8902de1SWarner Losh 	 * Create the pool if this is the first time we've seen it.
1057*b8902de1SWarner Losh 	 */
1058*b8902de1SWarner Losh 	spa = spa_find_by_guid(pool_guid);
1059*b8902de1SWarner Losh 	if (spa == NULL) {
1060*b8902de1SWarner Losh 		spa = spa_create(pool_guid, pool_name);
1061*b8902de1SWarner Losh 		if (spa == NULL)
1062*b8902de1SWarner Losh 			return (ENOMEM);
1063*b8902de1SWarner Losh 	}
1064*b8902de1SWarner Losh 	if (pool_txg > spa->spa_txg) {
1065*b8902de1SWarner Losh 		spa->spa_txg = pool_txg;
1066*b8902de1SWarner Losh 		is_newer = 1;
1067*b8902de1SWarner Losh 	} else {
1068*b8902de1SWarner Losh 		is_newer = 0;
1069*b8902de1SWarner Losh 	}
1070*b8902de1SWarner Losh 
1071*b8902de1SWarner Losh 	/*
1072*b8902de1SWarner Losh 	 * Get the vdev tree and create our in-core copy of it.
1073*b8902de1SWarner Losh 	 * If we already have a vdev with this guid, this must
1074*b8902de1SWarner Losh 	 * be some kind of alias (overlapping slices, dangerously dedicated
1075*b8902de1SWarner Losh 	 * disks etc).
1076*b8902de1SWarner Losh 	 */
1077*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1078*b8902de1SWarner Losh 	    NULL, &guid) != 0) {
1079*b8902de1SWarner Losh 		return (EIO);
1080*b8902de1SWarner Losh 	}
1081*b8902de1SWarner Losh 	vdev = vdev_find(guid);
1082*b8902de1SWarner Losh 	if (vdev && vdev->v_phys_read)	/* Has this vdev already been inited? */
1083*b8902de1SWarner Losh 		return (EIO);
1084*b8902de1SWarner Losh 
1085*b8902de1SWarner Losh 	if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1086*b8902de1SWarner Losh 	    NULL, &vdevs)) {
1087*b8902de1SWarner Losh 		return (EIO);
1088*b8902de1SWarner Losh 	}
1089*b8902de1SWarner Losh 
1090*b8902de1SWarner Losh 	rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1091*b8902de1SWarner Losh 	if (rc != 0)
1092*b8902de1SWarner Losh 		return (rc);
1093*b8902de1SWarner Losh 
1094*b8902de1SWarner Losh 	/*
1095*b8902de1SWarner Losh 	 * Add the toplevel vdev to the pool if its not already there.
1096*b8902de1SWarner Losh 	 */
1097*b8902de1SWarner Losh 	STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1098*b8902de1SWarner Losh 		if (top_vdev == pool_vdev)
1099*b8902de1SWarner Losh 			break;
1100*b8902de1SWarner Losh 	if (!pool_vdev && top_vdev) {
1101*b8902de1SWarner Losh 		top_vdev->spa = spa;
1102*b8902de1SWarner Losh 		STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1103*b8902de1SWarner Losh 	}
1104*b8902de1SWarner Losh 
1105*b8902de1SWarner Losh 	/*
1106*b8902de1SWarner Losh 	 * We should already have created an incomplete vdev for this
1107*b8902de1SWarner Losh 	 * vdev. Find it and initialise it with our read proc.
1108*b8902de1SWarner Losh 	 */
1109*b8902de1SWarner Losh 	vdev = vdev_find(guid);
1110*b8902de1SWarner Losh 	if (vdev) {
1111*b8902de1SWarner Losh 		vdev->v_phys_read = _read;
1112*b8902de1SWarner Losh 		vdev->v_read_priv = read_priv;
1113*b8902de1SWarner Losh 		vdev->v_state = VDEV_STATE_HEALTHY;
1114*b8902de1SWarner Losh 	} else {
1115*b8902de1SWarner Losh 		printf("ZFS: inconsistent nvlist contents\n");
1116*b8902de1SWarner Losh 		return (EIO);
1117*b8902de1SWarner Losh 	}
1118*b8902de1SWarner Losh 
1119*b8902de1SWarner Losh 	/*
1120*b8902de1SWarner Losh 	 * Re-evaluate top-level vdev state.
1121*b8902de1SWarner Losh 	 */
1122*b8902de1SWarner Losh 	vdev_set_state(top_vdev);
1123*b8902de1SWarner Losh 
1124*b8902de1SWarner Losh 	/*
1125*b8902de1SWarner Losh 	 * Ok, we are happy with the pool so far. Lets find
1126*b8902de1SWarner Losh 	 * the best uberblock and then we can actually access
1127*b8902de1SWarner Losh 	 * the contents of the pool.
1128*b8902de1SWarner Losh 	 */
1129*b8902de1SWarner Losh 	upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1130*b8902de1SWarner Losh 	up = (const struct uberblock *)upbuf;
1131*b8902de1SWarner Losh 	for (l = 0; l < VDEV_LABELS; l++) {
1132*b8902de1SWarner Losh 		for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1133*b8902de1SWarner Losh 			off = vdev_label_offset(psize, l,
1134*b8902de1SWarner Losh 			    VDEV_UBERBLOCK_OFFSET(vdev, i));
1135*b8902de1SWarner Losh 			BP_ZERO(&bp);
1136*b8902de1SWarner Losh 			DVA_SET_OFFSET(&bp.blk_dva[0], off);
1137*b8902de1SWarner Losh 			BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1138*b8902de1SWarner Losh 			BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1139*b8902de1SWarner Losh 			BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1140*b8902de1SWarner Losh 			BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1141*b8902de1SWarner Losh 			ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1142*b8902de1SWarner Losh 
1143*b8902de1SWarner Losh 			if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1144*b8902de1SWarner Losh 				continue;
1145*b8902de1SWarner Losh 
1146*b8902de1SWarner Losh 			if (up->ub_magic != UBERBLOCK_MAGIC)
1147*b8902de1SWarner Losh 				continue;
1148*b8902de1SWarner Losh 			if (up->ub_txg < spa->spa_txg)
1149*b8902de1SWarner Losh 				continue;
1150*b8902de1SWarner Losh 			if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1151*b8902de1SWarner Losh 			    (up->ub_txg == spa->spa_uberblock.ub_txg &&
1152*b8902de1SWarner Losh 			    up->ub_timestamp >
1153*b8902de1SWarner Losh 			    spa->spa_uberblock.ub_timestamp)) {
1154*b8902de1SWarner Losh 				spa->spa_uberblock = *up;
1155*b8902de1SWarner Losh 			}
1156*b8902de1SWarner Losh 		}
1157*b8902de1SWarner Losh 	}
1158*b8902de1SWarner Losh 	zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1159*b8902de1SWarner Losh 
1160*b8902de1SWarner Losh 	vdev->spa = spa;
1161*b8902de1SWarner Losh 	if (spap != NULL)
1162*b8902de1SWarner Losh 		*spap = spa;
1163*b8902de1SWarner Losh 	return (0);
1164*b8902de1SWarner Losh }
1165*b8902de1SWarner Losh 
1166*b8902de1SWarner Losh static int
1167*b8902de1SWarner Losh ilog2(int n)
1168*b8902de1SWarner Losh {
1169*b8902de1SWarner Losh 	int v;
1170*b8902de1SWarner Losh 
1171*b8902de1SWarner Losh 	for (v = 0; v < 32; v++)
1172*b8902de1SWarner Losh 		if (n == (1 << v))
1173*b8902de1SWarner Losh 			return v;
1174*b8902de1SWarner Losh 	return -1;
1175*b8902de1SWarner Losh }
1176*b8902de1SWarner Losh 
1177*b8902de1SWarner Losh static int
1178*b8902de1SWarner Losh zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1179*b8902de1SWarner Losh {
1180*b8902de1SWarner Losh 	blkptr_t gbh_bp;
1181*b8902de1SWarner Losh 	zio_gbh_phys_t zio_gb;
1182*b8902de1SWarner Losh 	char *pbuf;
1183*b8902de1SWarner Losh 	int i;
1184*b8902de1SWarner Losh 
1185*b8902de1SWarner Losh 	/* Artificial BP for gang block header. */
1186*b8902de1SWarner Losh 	gbh_bp = *bp;
1187*b8902de1SWarner Losh 	BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1188*b8902de1SWarner Losh 	BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1189*b8902de1SWarner Losh 	BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1190*b8902de1SWarner Losh 	BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1191*b8902de1SWarner Losh 	for (i = 0; i < SPA_DVAS_PER_BP; i++)
1192*b8902de1SWarner Losh 		DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1193*b8902de1SWarner Losh 
1194*b8902de1SWarner Losh 	/* Read gang header block using the artificial BP. */
1195*b8902de1SWarner Losh 	if (zio_read(spa, &gbh_bp, &zio_gb))
1196*b8902de1SWarner Losh 		return (EIO);
1197*b8902de1SWarner Losh 
1198*b8902de1SWarner Losh 	pbuf = buf;
1199*b8902de1SWarner Losh 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1200*b8902de1SWarner Losh 		blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1201*b8902de1SWarner Losh 
1202*b8902de1SWarner Losh 		if (BP_IS_HOLE(gbp))
1203*b8902de1SWarner Losh 			continue;
1204*b8902de1SWarner Losh 		if (zio_read(spa, gbp, pbuf))
1205*b8902de1SWarner Losh 			return (EIO);
1206*b8902de1SWarner Losh 		pbuf += BP_GET_PSIZE(gbp);
1207*b8902de1SWarner Losh 	}
1208*b8902de1SWarner Losh 
1209*b8902de1SWarner Losh 	if (zio_checksum_verify(spa, bp, buf))
1210*b8902de1SWarner Losh 		return (EIO);
1211*b8902de1SWarner Losh 	return (0);
1212*b8902de1SWarner Losh }
1213*b8902de1SWarner Losh 
1214*b8902de1SWarner Losh static int
1215*b8902de1SWarner Losh zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1216*b8902de1SWarner Losh {
1217*b8902de1SWarner Losh 	int cpfunc = BP_GET_COMPRESS(bp);
1218*b8902de1SWarner Losh 	uint64_t align, size;
1219*b8902de1SWarner Losh 	void *pbuf;
1220*b8902de1SWarner Losh 	int i, error;
1221*b8902de1SWarner Losh 
1222*b8902de1SWarner Losh 	/*
1223*b8902de1SWarner Losh 	 * Process data embedded in block pointer
1224*b8902de1SWarner Losh 	 */
1225*b8902de1SWarner Losh 	if (BP_IS_EMBEDDED(bp)) {
1226*b8902de1SWarner Losh 		ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1227*b8902de1SWarner Losh 
1228*b8902de1SWarner Losh 		size = BPE_GET_PSIZE(bp);
1229*b8902de1SWarner Losh 		ASSERT(size <= BPE_PAYLOAD_SIZE);
1230*b8902de1SWarner Losh 
1231*b8902de1SWarner Losh 		if (cpfunc != ZIO_COMPRESS_OFF)
1232*b8902de1SWarner Losh 			pbuf = zfs_alloc(size);
1233*b8902de1SWarner Losh 		else
1234*b8902de1SWarner Losh 			pbuf = buf;
1235*b8902de1SWarner Losh 
1236*b8902de1SWarner Losh 		decode_embedded_bp_compressed(bp, pbuf);
1237*b8902de1SWarner Losh 		error = 0;
1238*b8902de1SWarner Losh 
1239*b8902de1SWarner Losh 		if (cpfunc != ZIO_COMPRESS_OFF) {
1240*b8902de1SWarner Losh 			error = zio_decompress_data(cpfunc, pbuf,
1241*b8902de1SWarner Losh 			    size, buf, BP_GET_LSIZE(bp));
1242*b8902de1SWarner Losh 			zfs_free(pbuf, size);
1243*b8902de1SWarner Losh 		}
1244*b8902de1SWarner Losh 		if (error != 0)
1245*b8902de1SWarner Losh 			printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1246*b8902de1SWarner Losh 			    error);
1247*b8902de1SWarner Losh 		return (error);
1248*b8902de1SWarner Losh 	}
1249*b8902de1SWarner Losh 
1250*b8902de1SWarner Losh 	error = EIO;
1251*b8902de1SWarner Losh 
1252*b8902de1SWarner Losh 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1253*b8902de1SWarner Losh 		const dva_t *dva = &bp->blk_dva[i];
1254*b8902de1SWarner Losh 		vdev_t *vdev;
1255*b8902de1SWarner Losh 		int vdevid;
1256*b8902de1SWarner Losh 		off_t offset;
1257*b8902de1SWarner Losh 
1258*b8902de1SWarner Losh 		if (!dva->dva_word[0] && !dva->dva_word[1])
1259*b8902de1SWarner Losh 			continue;
1260*b8902de1SWarner Losh 
1261*b8902de1SWarner Losh 		vdevid = DVA_GET_VDEV(dva);
1262*b8902de1SWarner Losh 		offset = DVA_GET_OFFSET(dva);
1263*b8902de1SWarner Losh 		STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1264*b8902de1SWarner Losh 			if (vdev->v_id == vdevid)
1265*b8902de1SWarner Losh 				break;
1266*b8902de1SWarner Losh 		}
1267*b8902de1SWarner Losh 		if (!vdev || !vdev->v_read)
1268*b8902de1SWarner Losh 			continue;
1269*b8902de1SWarner Losh 
1270*b8902de1SWarner Losh 		size = BP_GET_PSIZE(bp);
1271*b8902de1SWarner Losh 		if (vdev->v_read == vdev_raidz_read) {
1272*b8902de1SWarner Losh 			align = 1ULL << vdev->v_top->v_ashift;
1273*b8902de1SWarner Losh 			if (P2PHASE(size, align) != 0)
1274*b8902de1SWarner Losh 				size = P2ROUNDUP(size, align);
1275*b8902de1SWarner Losh 		}
1276*b8902de1SWarner Losh 		if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1277*b8902de1SWarner Losh 			pbuf = zfs_alloc(size);
1278*b8902de1SWarner Losh 		else
1279*b8902de1SWarner Losh 			pbuf = buf;
1280*b8902de1SWarner Losh 
1281*b8902de1SWarner Losh 		if (DVA_GET_GANG(dva))
1282*b8902de1SWarner Losh 			error = zio_read_gang(spa, bp, pbuf);
1283*b8902de1SWarner Losh 		else
1284*b8902de1SWarner Losh 			error = vdev->v_read(vdev, bp, pbuf, offset, size);
1285*b8902de1SWarner Losh 		if (error == 0) {
1286*b8902de1SWarner Losh 			if (cpfunc != ZIO_COMPRESS_OFF)
1287*b8902de1SWarner Losh 				error = zio_decompress_data(cpfunc, pbuf,
1288*b8902de1SWarner Losh 				    BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1289*b8902de1SWarner Losh 			else if (size != BP_GET_PSIZE(bp))
1290*b8902de1SWarner Losh 				bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1291*b8902de1SWarner Losh 		}
1292*b8902de1SWarner Losh 		if (buf != pbuf)
1293*b8902de1SWarner Losh 			zfs_free(pbuf, size);
1294*b8902de1SWarner Losh 		if (error == 0)
1295*b8902de1SWarner Losh 			break;
1296*b8902de1SWarner Losh 	}
1297*b8902de1SWarner Losh 	if (error != 0)
1298*b8902de1SWarner Losh 		printf("ZFS: i/o error - all block copies unavailable\n");
1299*b8902de1SWarner Losh 	return (error);
1300*b8902de1SWarner Losh }
1301*b8902de1SWarner Losh 
1302*b8902de1SWarner Losh static int
1303*b8902de1SWarner Losh dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1304*b8902de1SWarner Losh {
1305*b8902de1SWarner Losh 	int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1306*b8902de1SWarner Losh 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1307*b8902de1SWarner Losh 	int nlevels = dnode->dn_nlevels;
1308*b8902de1SWarner Losh 	int i, rc;
1309*b8902de1SWarner Losh 
1310*b8902de1SWarner Losh 	if (bsize > SPA_MAXBLOCKSIZE) {
1311*b8902de1SWarner Losh 		printf("ZFS: I/O error - blocks larger than %llu are not "
1312*b8902de1SWarner Losh 		    "supported\n", SPA_MAXBLOCKSIZE);
1313*b8902de1SWarner Losh 		return (EIO);
1314*b8902de1SWarner Losh 	}
1315*b8902de1SWarner Losh 
1316*b8902de1SWarner Losh 	/*
1317*b8902de1SWarner Losh 	 * Note: bsize may not be a power of two here so we need to do an
1318*b8902de1SWarner Losh 	 * actual divide rather than a bitshift.
1319*b8902de1SWarner Losh 	 */
1320*b8902de1SWarner Losh 	while (buflen > 0) {
1321*b8902de1SWarner Losh 		uint64_t bn = offset / bsize;
1322*b8902de1SWarner Losh 		int boff = offset % bsize;
1323*b8902de1SWarner Losh 		int ibn;
1324*b8902de1SWarner Losh 		const blkptr_t *indbp;
1325*b8902de1SWarner Losh 		blkptr_t bp;
1326*b8902de1SWarner Losh 
1327*b8902de1SWarner Losh 		if (bn > dnode->dn_maxblkid)
1328*b8902de1SWarner Losh 			return (EIO);
1329*b8902de1SWarner Losh 
1330*b8902de1SWarner Losh 		if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1331*b8902de1SWarner Losh 			goto cached;
1332*b8902de1SWarner Losh 
1333*b8902de1SWarner Losh 		indbp = dnode->dn_blkptr;
1334*b8902de1SWarner Losh 		for (i = 0; i < nlevels; i++) {
1335*b8902de1SWarner Losh 			/*
1336*b8902de1SWarner Losh 			 * Copy the bp from the indirect array so that
1337*b8902de1SWarner Losh 			 * we can re-use the scratch buffer for multi-level
1338*b8902de1SWarner Losh 			 * objects.
1339*b8902de1SWarner Losh 			 */
1340*b8902de1SWarner Losh 			ibn = bn >> ((nlevels - i - 1) * ibshift);
1341*b8902de1SWarner Losh 			ibn &= ((1 << ibshift) - 1);
1342*b8902de1SWarner Losh 			bp = indbp[ibn];
1343*b8902de1SWarner Losh 			if (BP_IS_HOLE(&bp)) {
1344*b8902de1SWarner Losh 				memset(dnode_cache_buf, 0, bsize);
1345*b8902de1SWarner Losh 				break;
1346*b8902de1SWarner Losh 			}
1347*b8902de1SWarner Losh 			rc = zio_read(spa, &bp, dnode_cache_buf);
1348*b8902de1SWarner Losh 			if (rc)
1349*b8902de1SWarner Losh 				return (rc);
1350*b8902de1SWarner Losh 			indbp = (const blkptr_t *) dnode_cache_buf;
1351*b8902de1SWarner Losh 		}
1352*b8902de1SWarner Losh 		dnode_cache_obj = dnode;
1353*b8902de1SWarner Losh 		dnode_cache_bn = bn;
1354*b8902de1SWarner Losh 	cached:
1355*b8902de1SWarner Losh 
1356*b8902de1SWarner Losh 		/*
1357*b8902de1SWarner Losh 		 * The buffer contains our data block. Copy what we
1358*b8902de1SWarner Losh 		 * need from it and loop.
1359*b8902de1SWarner Losh 		 */
1360*b8902de1SWarner Losh 		i = bsize - boff;
1361*b8902de1SWarner Losh 		if (i > buflen) i = buflen;
1362*b8902de1SWarner Losh 		memcpy(buf, &dnode_cache_buf[boff], i);
1363*b8902de1SWarner Losh 		buf = ((char*) buf) + i;
1364*b8902de1SWarner Losh 		offset += i;
1365*b8902de1SWarner Losh 		buflen -= i;
1366*b8902de1SWarner Losh 	}
1367*b8902de1SWarner Losh 
1368*b8902de1SWarner Losh 	return (0);
1369*b8902de1SWarner Losh }
1370*b8902de1SWarner Losh 
1371*b8902de1SWarner Losh /*
1372*b8902de1SWarner Losh  * Lookup a value in a microzap directory. Assumes that the zap
1373*b8902de1SWarner Losh  * scratch buffer contains the directory contents.
1374*b8902de1SWarner Losh  */
1375*b8902de1SWarner Losh static int
1376*b8902de1SWarner Losh mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1377*b8902de1SWarner Losh {
1378*b8902de1SWarner Losh 	const mzap_phys_t *mz;
1379*b8902de1SWarner Losh 	const mzap_ent_phys_t *mze;
1380*b8902de1SWarner Losh 	size_t size;
1381*b8902de1SWarner Losh 	int chunks, i;
1382*b8902de1SWarner Losh 
1383*b8902de1SWarner Losh 	/*
1384*b8902de1SWarner Losh 	 * Microzap objects use exactly one block. Read the whole
1385*b8902de1SWarner Losh 	 * thing.
1386*b8902de1SWarner Losh 	 */
1387*b8902de1SWarner Losh 	size = dnode->dn_datablkszsec * 512;
1388*b8902de1SWarner Losh 
1389*b8902de1SWarner Losh 	mz = (const mzap_phys_t *) zap_scratch;
1390*b8902de1SWarner Losh 	chunks = size / MZAP_ENT_LEN - 1;
1391*b8902de1SWarner Losh 
1392*b8902de1SWarner Losh 	for (i = 0; i < chunks; i++) {
1393*b8902de1SWarner Losh 		mze = &mz->mz_chunk[i];
1394*b8902de1SWarner Losh 		if (!strcmp(mze->mze_name, name)) {
1395*b8902de1SWarner Losh 			*value = mze->mze_value;
1396*b8902de1SWarner Losh 			return (0);
1397*b8902de1SWarner Losh 		}
1398*b8902de1SWarner Losh 	}
1399*b8902de1SWarner Losh 
1400*b8902de1SWarner Losh 	return (ENOENT);
1401*b8902de1SWarner Losh }
1402*b8902de1SWarner Losh 
1403*b8902de1SWarner Losh /*
1404*b8902de1SWarner Losh  * Compare a name with a zap leaf entry. Return non-zero if the name
1405*b8902de1SWarner Losh  * matches.
1406*b8902de1SWarner Losh  */
1407*b8902de1SWarner Losh static int
1408*b8902de1SWarner Losh fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1409*b8902de1SWarner Losh {
1410*b8902de1SWarner Losh 	size_t namelen;
1411*b8902de1SWarner Losh 	const zap_leaf_chunk_t *nc;
1412*b8902de1SWarner Losh 	const char *p;
1413*b8902de1SWarner Losh 
1414*b8902de1SWarner Losh 	namelen = zc->l_entry.le_name_numints;
1415*b8902de1SWarner Losh 
1416*b8902de1SWarner Losh 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1417*b8902de1SWarner Losh 	p = name;
1418*b8902de1SWarner Losh 	while (namelen > 0) {
1419*b8902de1SWarner Losh 		size_t len;
1420*b8902de1SWarner Losh 		len = namelen;
1421*b8902de1SWarner Losh 		if (len > ZAP_LEAF_ARRAY_BYTES)
1422*b8902de1SWarner Losh 			len = ZAP_LEAF_ARRAY_BYTES;
1423*b8902de1SWarner Losh 		if (memcmp(p, nc->l_array.la_array, len))
1424*b8902de1SWarner Losh 			return (0);
1425*b8902de1SWarner Losh 		p += len;
1426*b8902de1SWarner Losh 		namelen -= len;
1427*b8902de1SWarner Losh 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1428*b8902de1SWarner Losh 	}
1429*b8902de1SWarner Losh 
1430*b8902de1SWarner Losh 	return 1;
1431*b8902de1SWarner Losh }
1432*b8902de1SWarner Losh 
1433*b8902de1SWarner Losh /*
1434*b8902de1SWarner Losh  * Extract a uint64_t value from a zap leaf entry.
1435*b8902de1SWarner Losh  */
1436*b8902de1SWarner Losh static uint64_t
1437*b8902de1SWarner Losh fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1438*b8902de1SWarner Losh {
1439*b8902de1SWarner Losh 	const zap_leaf_chunk_t *vc;
1440*b8902de1SWarner Losh 	int i;
1441*b8902de1SWarner Losh 	uint64_t value;
1442*b8902de1SWarner Losh 	const uint8_t *p;
1443*b8902de1SWarner Losh 
1444*b8902de1SWarner Losh 	vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1445*b8902de1SWarner Losh 	for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1446*b8902de1SWarner Losh 		value = (value << 8) | p[i];
1447*b8902de1SWarner Losh 	}
1448*b8902de1SWarner Losh 
1449*b8902de1SWarner Losh 	return value;
1450*b8902de1SWarner Losh }
1451*b8902de1SWarner Losh 
1452*b8902de1SWarner Losh static void
1453*b8902de1SWarner Losh stv(int len, void *addr, uint64_t value)
1454*b8902de1SWarner Losh {
1455*b8902de1SWarner Losh 	switch (len) {
1456*b8902de1SWarner Losh 	case 1:
1457*b8902de1SWarner Losh 		*(uint8_t *)addr = value;
1458*b8902de1SWarner Losh 		return;
1459*b8902de1SWarner Losh 	case 2:
1460*b8902de1SWarner Losh 		*(uint16_t *)addr = value;
1461*b8902de1SWarner Losh 		return;
1462*b8902de1SWarner Losh 	case 4:
1463*b8902de1SWarner Losh 		*(uint32_t *)addr = value;
1464*b8902de1SWarner Losh 		return;
1465*b8902de1SWarner Losh 	case 8:
1466*b8902de1SWarner Losh 		*(uint64_t *)addr = value;
1467*b8902de1SWarner Losh 		return;
1468*b8902de1SWarner Losh 	}
1469*b8902de1SWarner Losh }
1470*b8902de1SWarner Losh 
1471*b8902de1SWarner Losh /*
1472*b8902de1SWarner Losh  * Extract a array from a zap leaf entry.
1473*b8902de1SWarner Losh  */
1474*b8902de1SWarner Losh static void
1475*b8902de1SWarner Losh fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1476*b8902de1SWarner Losh     uint64_t integer_size, uint64_t num_integers, void *buf)
1477*b8902de1SWarner Losh {
1478*b8902de1SWarner Losh 	uint64_t array_int_len = zc->l_entry.le_value_intlen;
1479*b8902de1SWarner Losh 	uint64_t value = 0;
1480*b8902de1SWarner Losh 	uint64_t *u64 = buf;
1481*b8902de1SWarner Losh 	char *p = buf;
1482*b8902de1SWarner Losh 	int len = MIN(zc->l_entry.le_value_numints, num_integers);
1483*b8902de1SWarner Losh 	int chunk = zc->l_entry.le_value_chunk;
1484*b8902de1SWarner Losh 	int byten = 0;
1485*b8902de1SWarner Losh 
1486*b8902de1SWarner Losh 	if (integer_size == 8 && len == 1) {
1487*b8902de1SWarner Losh 		*u64 = fzap_leaf_value(zl, zc);
1488*b8902de1SWarner Losh 		return;
1489*b8902de1SWarner Losh 	}
1490*b8902de1SWarner Losh 
1491*b8902de1SWarner Losh 	while (len > 0) {
1492*b8902de1SWarner Losh 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1493*b8902de1SWarner Losh 		int i;
1494*b8902de1SWarner Losh 
1495*b8902de1SWarner Losh 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1496*b8902de1SWarner Losh 		for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1497*b8902de1SWarner Losh 			value = (value << 8) | la->la_array[i];
1498*b8902de1SWarner Losh 			byten++;
1499*b8902de1SWarner Losh 			if (byten == array_int_len) {
1500*b8902de1SWarner Losh 				stv(integer_size, p, value);
1501*b8902de1SWarner Losh 				byten = 0;
1502*b8902de1SWarner Losh 				len--;
1503*b8902de1SWarner Losh 				if (len == 0)
1504*b8902de1SWarner Losh 					return;
1505*b8902de1SWarner Losh 				p += integer_size;
1506*b8902de1SWarner Losh 			}
1507*b8902de1SWarner Losh 		}
1508*b8902de1SWarner Losh 		chunk = la->la_next;
1509*b8902de1SWarner Losh 	}
1510*b8902de1SWarner Losh }
1511*b8902de1SWarner Losh 
1512*b8902de1SWarner Losh /*
1513*b8902de1SWarner Losh  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1514*b8902de1SWarner Losh  * buffer contains the directory header.
1515*b8902de1SWarner Losh  */
1516*b8902de1SWarner Losh static int
1517*b8902de1SWarner Losh fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1518*b8902de1SWarner Losh     uint64_t integer_size, uint64_t num_integers, void *value)
1519*b8902de1SWarner Losh {
1520*b8902de1SWarner Losh 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1521*b8902de1SWarner Losh 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1522*b8902de1SWarner Losh 	fat_zap_t z;
1523*b8902de1SWarner Losh 	uint64_t *ptrtbl;
1524*b8902de1SWarner Losh 	uint64_t hash;
1525*b8902de1SWarner Losh 	int rc;
1526*b8902de1SWarner Losh 
1527*b8902de1SWarner Losh 	if (zh.zap_magic != ZAP_MAGIC)
1528*b8902de1SWarner Losh 		return (EIO);
1529*b8902de1SWarner Losh 
1530*b8902de1SWarner Losh 	z.zap_block_shift = ilog2(bsize);
1531*b8902de1SWarner Losh 	z.zap_phys = (zap_phys_t *) zap_scratch;
1532*b8902de1SWarner Losh 
1533*b8902de1SWarner Losh 	/*
1534*b8902de1SWarner Losh 	 * Figure out where the pointer table is and read it in if necessary.
1535*b8902de1SWarner Losh 	 */
1536*b8902de1SWarner Losh 	if (zh.zap_ptrtbl.zt_blk) {
1537*b8902de1SWarner Losh 		rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1538*b8902de1SWarner Losh 			       zap_scratch, bsize);
1539*b8902de1SWarner Losh 		if (rc)
1540*b8902de1SWarner Losh 			return (rc);
1541*b8902de1SWarner Losh 		ptrtbl = (uint64_t *) zap_scratch;
1542*b8902de1SWarner Losh 	} else {
1543*b8902de1SWarner Losh 		ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1544*b8902de1SWarner Losh 	}
1545*b8902de1SWarner Losh 
1546*b8902de1SWarner Losh 	hash = zap_hash(zh.zap_salt, name);
1547*b8902de1SWarner Losh 
1548*b8902de1SWarner Losh 	zap_leaf_t zl;
1549*b8902de1SWarner Losh 	zl.l_bs = z.zap_block_shift;
1550*b8902de1SWarner Losh 
1551*b8902de1SWarner Losh 	off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1552*b8902de1SWarner Losh 	zap_leaf_chunk_t *zc;
1553*b8902de1SWarner Losh 
1554*b8902de1SWarner Losh 	rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1555*b8902de1SWarner Losh 	if (rc)
1556*b8902de1SWarner Losh 		return (rc);
1557*b8902de1SWarner Losh 
1558*b8902de1SWarner Losh 	zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1559*b8902de1SWarner Losh 
1560*b8902de1SWarner Losh 	/*
1561*b8902de1SWarner Losh 	 * Make sure this chunk matches our hash.
1562*b8902de1SWarner Losh 	 */
1563*b8902de1SWarner Losh 	if (zl.l_phys->l_hdr.lh_prefix_len > 0
1564*b8902de1SWarner Losh 	    && zl.l_phys->l_hdr.lh_prefix
1565*b8902de1SWarner Losh 	    != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1566*b8902de1SWarner Losh 		return (ENOENT);
1567*b8902de1SWarner Losh 
1568*b8902de1SWarner Losh 	/*
1569*b8902de1SWarner Losh 	 * Hash within the chunk to find our entry.
1570*b8902de1SWarner Losh 	 */
1571*b8902de1SWarner Losh 	int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1572*b8902de1SWarner Losh 	int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1573*b8902de1SWarner Losh 	h = zl.l_phys->l_hash[h];
1574*b8902de1SWarner Losh 	if (h == 0xffff)
1575*b8902de1SWarner Losh 		return (ENOENT);
1576*b8902de1SWarner Losh 	zc = &ZAP_LEAF_CHUNK(&zl, h);
1577*b8902de1SWarner Losh 	while (zc->l_entry.le_hash != hash) {
1578*b8902de1SWarner Losh 		if (zc->l_entry.le_next == 0xffff) {
1579*b8902de1SWarner Losh 			zc = NULL;
1580*b8902de1SWarner Losh 			break;
1581*b8902de1SWarner Losh 		}
1582*b8902de1SWarner Losh 		zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1583*b8902de1SWarner Losh 	}
1584*b8902de1SWarner Losh 	if (fzap_name_equal(&zl, zc, name)) {
1585*b8902de1SWarner Losh 		if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1586*b8902de1SWarner Losh 		    integer_size * num_integers)
1587*b8902de1SWarner Losh 			return (E2BIG);
1588*b8902de1SWarner Losh 		fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1589*b8902de1SWarner Losh 		return (0);
1590*b8902de1SWarner Losh 	}
1591*b8902de1SWarner Losh 
1592*b8902de1SWarner Losh 	return (ENOENT);
1593*b8902de1SWarner Losh }
1594*b8902de1SWarner Losh 
1595*b8902de1SWarner Losh /*
1596*b8902de1SWarner Losh  * Lookup a name in a zap object and return its value as a uint64_t.
1597*b8902de1SWarner Losh  */
1598*b8902de1SWarner Losh static int
1599*b8902de1SWarner Losh zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1600*b8902de1SWarner Losh     uint64_t integer_size, uint64_t num_integers, void *value)
1601*b8902de1SWarner Losh {
1602*b8902de1SWarner Losh 	int rc;
1603*b8902de1SWarner Losh 	uint64_t zap_type;
1604*b8902de1SWarner Losh 	size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1605*b8902de1SWarner Losh 
1606*b8902de1SWarner Losh 	rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1607*b8902de1SWarner Losh 	if (rc)
1608*b8902de1SWarner Losh 		return (rc);
1609*b8902de1SWarner Losh 
1610*b8902de1SWarner Losh 	zap_type = *(uint64_t *) zap_scratch;
1611*b8902de1SWarner Losh 	if (zap_type == ZBT_MICRO)
1612*b8902de1SWarner Losh 		return mzap_lookup(dnode, name, value);
1613*b8902de1SWarner Losh 	else if (zap_type == ZBT_HEADER) {
1614*b8902de1SWarner Losh 		return fzap_lookup(spa, dnode, name, integer_size,
1615*b8902de1SWarner Losh 		    num_integers, value);
1616*b8902de1SWarner Losh 	}
1617*b8902de1SWarner Losh 	printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1618*b8902de1SWarner Losh 	return (EIO);
1619*b8902de1SWarner Losh }
1620*b8902de1SWarner Losh 
1621*b8902de1SWarner Losh /*
1622*b8902de1SWarner Losh  * List a microzap directory. Assumes that the zap scratch buffer contains
1623*b8902de1SWarner Losh  * the directory contents.
1624*b8902de1SWarner Losh  */
1625*b8902de1SWarner Losh static int
1626*b8902de1SWarner Losh mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1627*b8902de1SWarner Losh {
1628*b8902de1SWarner Losh 	const mzap_phys_t *mz;
1629*b8902de1SWarner Losh 	const mzap_ent_phys_t *mze;
1630*b8902de1SWarner Losh 	size_t size;
1631*b8902de1SWarner Losh 	int chunks, i, rc;
1632*b8902de1SWarner Losh 
1633*b8902de1SWarner Losh 	/*
1634*b8902de1SWarner Losh 	 * Microzap objects use exactly one block. Read the whole
1635*b8902de1SWarner Losh 	 * thing.
1636*b8902de1SWarner Losh 	 */
1637*b8902de1SWarner Losh 	size = dnode->dn_datablkszsec * 512;
1638*b8902de1SWarner Losh 	mz = (const mzap_phys_t *) zap_scratch;
1639*b8902de1SWarner Losh 	chunks = size / MZAP_ENT_LEN - 1;
1640*b8902de1SWarner Losh 
1641*b8902de1SWarner Losh 	for (i = 0; i < chunks; i++) {
1642*b8902de1SWarner Losh 		mze = &mz->mz_chunk[i];
1643*b8902de1SWarner Losh 		if (mze->mze_name[0]) {
1644*b8902de1SWarner Losh 			rc = callback(mze->mze_name, mze->mze_value);
1645*b8902de1SWarner Losh 			if (rc != 0)
1646*b8902de1SWarner Losh 				return (rc);
1647*b8902de1SWarner Losh 		}
1648*b8902de1SWarner Losh 	}
1649*b8902de1SWarner Losh 
1650*b8902de1SWarner Losh 	return (0);
1651*b8902de1SWarner Losh }
1652*b8902de1SWarner Losh 
1653*b8902de1SWarner Losh /*
1654*b8902de1SWarner Losh  * List a fatzap directory. Assumes that the zap scratch buffer contains
1655*b8902de1SWarner Losh  * the directory header.
1656*b8902de1SWarner Losh  */
1657*b8902de1SWarner Losh static int
1658*b8902de1SWarner Losh fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1659*b8902de1SWarner Losh {
1660*b8902de1SWarner Losh 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1661*b8902de1SWarner Losh 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1662*b8902de1SWarner Losh 	fat_zap_t z;
1663*b8902de1SWarner Losh 	int i, j, rc;
1664*b8902de1SWarner Losh 
1665*b8902de1SWarner Losh 	if (zh.zap_magic != ZAP_MAGIC)
1666*b8902de1SWarner Losh 		return (EIO);
1667*b8902de1SWarner Losh 
1668*b8902de1SWarner Losh 	z.zap_block_shift = ilog2(bsize);
1669*b8902de1SWarner Losh 	z.zap_phys = (zap_phys_t *) zap_scratch;
1670*b8902de1SWarner Losh 
1671*b8902de1SWarner Losh 	/*
1672*b8902de1SWarner Losh 	 * This assumes that the leaf blocks start at block 1. The
1673*b8902de1SWarner Losh 	 * documentation isn't exactly clear on this.
1674*b8902de1SWarner Losh 	 */
1675*b8902de1SWarner Losh 	zap_leaf_t zl;
1676*b8902de1SWarner Losh 	zl.l_bs = z.zap_block_shift;
1677*b8902de1SWarner Losh 	for (i = 0; i < zh.zap_num_leafs; i++) {
1678*b8902de1SWarner Losh 		off_t off = (i + 1) << zl.l_bs;
1679*b8902de1SWarner Losh 		char name[256], *p;
1680*b8902de1SWarner Losh 		uint64_t value;
1681*b8902de1SWarner Losh 
1682*b8902de1SWarner Losh 		if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1683*b8902de1SWarner Losh 			return (EIO);
1684*b8902de1SWarner Losh 
1685*b8902de1SWarner Losh 		zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1686*b8902de1SWarner Losh 
1687*b8902de1SWarner Losh 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1688*b8902de1SWarner Losh 			zap_leaf_chunk_t *zc, *nc;
1689*b8902de1SWarner Losh 			int namelen;
1690*b8902de1SWarner Losh 
1691*b8902de1SWarner Losh 			zc = &ZAP_LEAF_CHUNK(&zl, j);
1692*b8902de1SWarner Losh 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1693*b8902de1SWarner Losh 				continue;
1694*b8902de1SWarner Losh 			namelen = zc->l_entry.le_name_numints;
1695*b8902de1SWarner Losh 			if (namelen > sizeof(name))
1696*b8902de1SWarner Losh 				namelen = sizeof(name);
1697*b8902de1SWarner Losh 
1698*b8902de1SWarner Losh 			/*
1699*b8902de1SWarner Losh 			 * Paste the name back together.
1700*b8902de1SWarner Losh 			 */
1701*b8902de1SWarner Losh 			nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1702*b8902de1SWarner Losh 			p = name;
1703*b8902de1SWarner Losh 			while (namelen > 0) {
1704*b8902de1SWarner Losh 				int len;
1705*b8902de1SWarner Losh 				len = namelen;
1706*b8902de1SWarner Losh 				if (len > ZAP_LEAF_ARRAY_BYTES)
1707*b8902de1SWarner Losh 					len = ZAP_LEAF_ARRAY_BYTES;
1708*b8902de1SWarner Losh 				memcpy(p, nc->l_array.la_array, len);
1709*b8902de1SWarner Losh 				p += len;
1710*b8902de1SWarner Losh 				namelen -= len;
1711*b8902de1SWarner Losh 				nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1712*b8902de1SWarner Losh 			}
1713*b8902de1SWarner Losh 
1714*b8902de1SWarner Losh 			/*
1715*b8902de1SWarner Losh 			 * Assume the first eight bytes of the value are
1716*b8902de1SWarner Losh 			 * a uint64_t.
1717*b8902de1SWarner Losh 			 */
1718*b8902de1SWarner Losh 			value = fzap_leaf_value(&zl, zc);
1719*b8902de1SWarner Losh 
1720*b8902de1SWarner Losh 			//printf("%s 0x%jx\n", name, (uintmax_t)value);
1721*b8902de1SWarner Losh 			rc = callback((const char *)name, value);
1722*b8902de1SWarner Losh 			if (rc != 0)
1723*b8902de1SWarner Losh 				return (rc);
1724*b8902de1SWarner Losh 		}
1725*b8902de1SWarner Losh 	}
1726*b8902de1SWarner Losh 
1727*b8902de1SWarner Losh 	return (0);
1728*b8902de1SWarner Losh }
1729*b8902de1SWarner Losh 
1730*b8902de1SWarner Losh static int zfs_printf(const char *name, uint64_t value __unused)
1731*b8902de1SWarner Losh {
1732*b8902de1SWarner Losh 
1733*b8902de1SWarner Losh 	printf("%s\n", name);
1734*b8902de1SWarner Losh 
1735*b8902de1SWarner Losh 	return (0);
1736*b8902de1SWarner Losh }
1737*b8902de1SWarner Losh 
1738*b8902de1SWarner Losh /*
1739*b8902de1SWarner Losh  * List a zap directory.
1740*b8902de1SWarner Losh  */
1741*b8902de1SWarner Losh static int
1742*b8902de1SWarner Losh zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1743*b8902de1SWarner Losh {
1744*b8902de1SWarner Losh 	uint64_t zap_type;
1745*b8902de1SWarner Losh 	size_t size = dnode->dn_datablkszsec * 512;
1746*b8902de1SWarner Losh 
1747*b8902de1SWarner Losh 	if (dnode_read(spa, dnode, 0, zap_scratch, size))
1748*b8902de1SWarner Losh 		return (EIO);
1749*b8902de1SWarner Losh 
1750*b8902de1SWarner Losh 	zap_type = *(uint64_t *) zap_scratch;
1751*b8902de1SWarner Losh 	if (zap_type == ZBT_MICRO)
1752*b8902de1SWarner Losh 		return mzap_list(dnode, zfs_printf);
1753*b8902de1SWarner Losh 	else
1754*b8902de1SWarner Losh 		return fzap_list(spa, dnode, zfs_printf);
1755*b8902de1SWarner Losh }
1756*b8902de1SWarner Losh 
1757*b8902de1SWarner Losh static int
1758*b8902de1SWarner Losh objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1759*b8902de1SWarner Losh {
1760*b8902de1SWarner Losh 	off_t offset;
1761*b8902de1SWarner Losh 
1762*b8902de1SWarner Losh 	offset = objnum * sizeof(dnode_phys_t);
1763*b8902de1SWarner Losh 	return dnode_read(spa, &os->os_meta_dnode, offset,
1764*b8902de1SWarner Losh 		dnode, sizeof(dnode_phys_t));
1765*b8902de1SWarner Losh }
1766*b8902de1SWarner Losh 
1767*b8902de1SWarner Losh static int
1768*b8902de1SWarner Losh mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1769*b8902de1SWarner Losh {
1770*b8902de1SWarner Losh 	const mzap_phys_t *mz;
1771*b8902de1SWarner Losh 	const mzap_ent_phys_t *mze;
1772*b8902de1SWarner Losh 	size_t size;
1773*b8902de1SWarner Losh 	int chunks, i;
1774*b8902de1SWarner Losh 
1775*b8902de1SWarner Losh 	/*
1776*b8902de1SWarner Losh 	 * Microzap objects use exactly one block. Read the whole
1777*b8902de1SWarner Losh 	 * thing.
1778*b8902de1SWarner Losh 	 */
1779*b8902de1SWarner Losh 	size = dnode->dn_datablkszsec * 512;
1780*b8902de1SWarner Losh 
1781*b8902de1SWarner Losh 	mz = (const mzap_phys_t *) zap_scratch;
1782*b8902de1SWarner Losh 	chunks = size / MZAP_ENT_LEN - 1;
1783*b8902de1SWarner Losh 
1784*b8902de1SWarner Losh 	for (i = 0; i < chunks; i++) {
1785*b8902de1SWarner Losh 		mze = &mz->mz_chunk[i];
1786*b8902de1SWarner Losh 		if (value == mze->mze_value) {
1787*b8902de1SWarner Losh 			strcpy(name, mze->mze_name);
1788*b8902de1SWarner Losh 			return (0);
1789*b8902de1SWarner Losh 		}
1790*b8902de1SWarner Losh 	}
1791*b8902de1SWarner Losh 
1792*b8902de1SWarner Losh 	return (ENOENT);
1793*b8902de1SWarner Losh }
1794*b8902de1SWarner Losh 
1795*b8902de1SWarner Losh static void
1796*b8902de1SWarner Losh fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1797*b8902de1SWarner Losh {
1798*b8902de1SWarner Losh 	size_t namelen;
1799*b8902de1SWarner Losh 	const zap_leaf_chunk_t *nc;
1800*b8902de1SWarner Losh 	char *p;
1801*b8902de1SWarner Losh 
1802*b8902de1SWarner Losh 	namelen = zc->l_entry.le_name_numints;
1803*b8902de1SWarner Losh 
1804*b8902de1SWarner Losh 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1805*b8902de1SWarner Losh 	p = name;
1806*b8902de1SWarner Losh 	while (namelen > 0) {
1807*b8902de1SWarner Losh 		size_t len;
1808*b8902de1SWarner Losh 		len = namelen;
1809*b8902de1SWarner Losh 		if (len > ZAP_LEAF_ARRAY_BYTES)
1810*b8902de1SWarner Losh 			len = ZAP_LEAF_ARRAY_BYTES;
1811*b8902de1SWarner Losh 		memcpy(p, nc->l_array.la_array, len);
1812*b8902de1SWarner Losh 		p += len;
1813*b8902de1SWarner Losh 		namelen -= len;
1814*b8902de1SWarner Losh 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1815*b8902de1SWarner Losh 	}
1816*b8902de1SWarner Losh 
1817*b8902de1SWarner Losh 	*p = '\0';
1818*b8902de1SWarner Losh }
1819*b8902de1SWarner Losh 
1820*b8902de1SWarner Losh static int
1821*b8902de1SWarner Losh fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1822*b8902de1SWarner Losh {
1823*b8902de1SWarner Losh 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1824*b8902de1SWarner Losh 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1825*b8902de1SWarner Losh 	fat_zap_t z;
1826*b8902de1SWarner Losh 	int i, j;
1827*b8902de1SWarner Losh 
1828*b8902de1SWarner Losh 	if (zh.zap_magic != ZAP_MAGIC)
1829*b8902de1SWarner Losh 		return (EIO);
1830*b8902de1SWarner Losh 
1831*b8902de1SWarner Losh 	z.zap_block_shift = ilog2(bsize);
1832*b8902de1SWarner Losh 	z.zap_phys = (zap_phys_t *) zap_scratch;
1833*b8902de1SWarner Losh 
1834*b8902de1SWarner Losh 	/*
1835*b8902de1SWarner Losh 	 * This assumes that the leaf blocks start at block 1. The
1836*b8902de1SWarner Losh 	 * documentation isn't exactly clear on this.
1837*b8902de1SWarner Losh 	 */
1838*b8902de1SWarner Losh 	zap_leaf_t zl;
1839*b8902de1SWarner Losh 	zl.l_bs = z.zap_block_shift;
1840*b8902de1SWarner Losh 	for (i = 0; i < zh.zap_num_leafs; i++) {
1841*b8902de1SWarner Losh 		off_t off = (i + 1) << zl.l_bs;
1842*b8902de1SWarner Losh 
1843*b8902de1SWarner Losh 		if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1844*b8902de1SWarner Losh 			return (EIO);
1845*b8902de1SWarner Losh 
1846*b8902de1SWarner Losh 		zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1847*b8902de1SWarner Losh 
1848*b8902de1SWarner Losh 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1849*b8902de1SWarner Losh 			zap_leaf_chunk_t *zc;
1850*b8902de1SWarner Losh 
1851*b8902de1SWarner Losh 			zc = &ZAP_LEAF_CHUNK(&zl, j);
1852*b8902de1SWarner Losh 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1853*b8902de1SWarner Losh 				continue;
1854*b8902de1SWarner Losh 			if (zc->l_entry.le_value_intlen != 8 ||
1855*b8902de1SWarner Losh 			    zc->l_entry.le_value_numints != 1)
1856*b8902de1SWarner Losh 				continue;
1857*b8902de1SWarner Losh 
1858*b8902de1SWarner Losh 			if (fzap_leaf_value(&zl, zc) == value) {
1859*b8902de1SWarner Losh 				fzap_name_copy(&zl, zc, name);
1860*b8902de1SWarner Losh 				return (0);
1861*b8902de1SWarner Losh 			}
1862*b8902de1SWarner Losh 		}
1863*b8902de1SWarner Losh 	}
1864*b8902de1SWarner Losh 
1865*b8902de1SWarner Losh 	return (ENOENT);
1866*b8902de1SWarner Losh }
1867*b8902de1SWarner Losh 
1868*b8902de1SWarner Losh static int
1869*b8902de1SWarner Losh zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1870*b8902de1SWarner Losh {
1871*b8902de1SWarner Losh 	int rc;
1872*b8902de1SWarner Losh 	uint64_t zap_type;
1873*b8902de1SWarner Losh 	size_t size = dnode->dn_datablkszsec * 512;
1874*b8902de1SWarner Losh 
1875*b8902de1SWarner Losh 	rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1876*b8902de1SWarner Losh 	if (rc)
1877*b8902de1SWarner Losh 		return (rc);
1878*b8902de1SWarner Losh 
1879*b8902de1SWarner Losh 	zap_type = *(uint64_t *) zap_scratch;
1880*b8902de1SWarner Losh 	if (zap_type == ZBT_MICRO)
1881*b8902de1SWarner Losh 		return mzap_rlookup(spa, dnode, name, value);
1882*b8902de1SWarner Losh 	else
1883*b8902de1SWarner Losh 		return fzap_rlookup(spa, dnode, name, value);
1884*b8902de1SWarner Losh }
1885*b8902de1SWarner Losh 
1886*b8902de1SWarner Losh static int
1887*b8902de1SWarner Losh zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1888*b8902de1SWarner Losh {
1889*b8902de1SWarner Losh 	char name[256];
1890*b8902de1SWarner Losh 	char component[256];
1891*b8902de1SWarner Losh 	uint64_t dir_obj, parent_obj, child_dir_zapobj;
1892*b8902de1SWarner Losh 	dnode_phys_t child_dir_zap, dataset, dir, parent;
1893*b8902de1SWarner Losh 	dsl_dir_phys_t *dd;
1894*b8902de1SWarner Losh 	dsl_dataset_phys_t *ds;
1895*b8902de1SWarner Losh 	char *p;
1896*b8902de1SWarner Losh 	int len;
1897*b8902de1SWarner Losh 
1898*b8902de1SWarner Losh 	p = &name[sizeof(name) - 1];
1899*b8902de1SWarner Losh 	*p = '\0';
1900*b8902de1SWarner Losh 
1901*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1902*b8902de1SWarner Losh 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1903*b8902de1SWarner Losh 		return (EIO);
1904*b8902de1SWarner Losh 	}
1905*b8902de1SWarner Losh 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1906*b8902de1SWarner Losh 	dir_obj = ds->ds_dir_obj;
1907*b8902de1SWarner Losh 
1908*b8902de1SWarner Losh 	for (;;) {
1909*b8902de1SWarner Losh 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1910*b8902de1SWarner Losh 			return (EIO);
1911*b8902de1SWarner Losh 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1912*b8902de1SWarner Losh 
1913*b8902de1SWarner Losh 		/* Actual loop condition. */
1914*b8902de1SWarner Losh 		parent_obj  = dd->dd_parent_obj;
1915*b8902de1SWarner Losh 		if (parent_obj == 0)
1916*b8902de1SWarner Losh 			break;
1917*b8902de1SWarner Losh 
1918*b8902de1SWarner Losh 		if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1919*b8902de1SWarner Losh 			return (EIO);
1920*b8902de1SWarner Losh 		dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1921*b8902de1SWarner Losh 		child_dir_zapobj = dd->dd_child_dir_zapobj;
1922*b8902de1SWarner Losh 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1923*b8902de1SWarner Losh 			return (EIO);
1924*b8902de1SWarner Losh 		if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1925*b8902de1SWarner Losh 			return (EIO);
1926*b8902de1SWarner Losh 
1927*b8902de1SWarner Losh 		len = strlen(component);
1928*b8902de1SWarner Losh 		p -= len;
1929*b8902de1SWarner Losh 		memcpy(p, component, len);
1930*b8902de1SWarner Losh 		--p;
1931*b8902de1SWarner Losh 		*p = '/';
1932*b8902de1SWarner Losh 
1933*b8902de1SWarner Losh 		/* Actual loop iteration. */
1934*b8902de1SWarner Losh 		dir_obj = parent_obj;
1935*b8902de1SWarner Losh 	}
1936*b8902de1SWarner Losh 
1937*b8902de1SWarner Losh 	if (*p != '\0')
1938*b8902de1SWarner Losh 		++p;
1939*b8902de1SWarner Losh 	strcpy(result, p);
1940*b8902de1SWarner Losh 
1941*b8902de1SWarner Losh 	return (0);
1942*b8902de1SWarner Losh }
1943*b8902de1SWarner Losh 
1944*b8902de1SWarner Losh static int
1945*b8902de1SWarner Losh zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1946*b8902de1SWarner Losh {
1947*b8902de1SWarner Losh 	char element[256];
1948*b8902de1SWarner Losh 	uint64_t dir_obj, child_dir_zapobj;
1949*b8902de1SWarner Losh 	dnode_phys_t child_dir_zap, dir;
1950*b8902de1SWarner Losh 	dsl_dir_phys_t *dd;
1951*b8902de1SWarner Losh 	const char *p, *q;
1952*b8902de1SWarner Losh 
1953*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1954*b8902de1SWarner Losh 		return (EIO);
1955*b8902de1SWarner Losh 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1956*b8902de1SWarner Losh 	    1, &dir_obj))
1957*b8902de1SWarner Losh 		return (EIO);
1958*b8902de1SWarner Losh 
1959*b8902de1SWarner Losh 	p = name;
1960*b8902de1SWarner Losh 	for (;;) {
1961*b8902de1SWarner Losh 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1962*b8902de1SWarner Losh 			return (EIO);
1963*b8902de1SWarner Losh 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1964*b8902de1SWarner Losh 
1965*b8902de1SWarner Losh 		while (*p == '/')
1966*b8902de1SWarner Losh 			p++;
1967*b8902de1SWarner Losh 		/* Actual loop condition #1. */
1968*b8902de1SWarner Losh 		if (*p == '\0')
1969*b8902de1SWarner Losh 			break;
1970*b8902de1SWarner Losh 
1971*b8902de1SWarner Losh 		q = strchr(p, '/');
1972*b8902de1SWarner Losh 		if (q) {
1973*b8902de1SWarner Losh 			memcpy(element, p, q - p);
1974*b8902de1SWarner Losh 			element[q - p] = '\0';
1975*b8902de1SWarner Losh 			p = q + 1;
1976*b8902de1SWarner Losh 		} else {
1977*b8902de1SWarner Losh 			strcpy(element, p);
1978*b8902de1SWarner Losh 			p += strlen(p);
1979*b8902de1SWarner Losh 		}
1980*b8902de1SWarner Losh 
1981*b8902de1SWarner Losh 		child_dir_zapobj = dd->dd_child_dir_zapobj;
1982*b8902de1SWarner Losh 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1983*b8902de1SWarner Losh 			return (EIO);
1984*b8902de1SWarner Losh 
1985*b8902de1SWarner Losh 		/* Actual loop condition #2. */
1986*b8902de1SWarner Losh 		if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1987*b8902de1SWarner Losh 		    1, &dir_obj) != 0)
1988*b8902de1SWarner Losh 			return (ENOENT);
1989*b8902de1SWarner Losh 	}
1990*b8902de1SWarner Losh 
1991*b8902de1SWarner Losh 	*objnum = dd->dd_head_dataset_obj;
1992*b8902de1SWarner Losh 	return (0);
1993*b8902de1SWarner Losh }
1994*b8902de1SWarner Losh 
1995*b8902de1SWarner Losh #ifndef BOOT2
1996*b8902de1SWarner Losh static int
1997*b8902de1SWarner Losh zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1998*b8902de1SWarner Losh {
1999*b8902de1SWarner Losh 	uint64_t dir_obj, child_dir_zapobj;
2000*b8902de1SWarner Losh 	dnode_phys_t child_dir_zap, dir, dataset;
2001*b8902de1SWarner Losh 	dsl_dataset_phys_t *ds;
2002*b8902de1SWarner Losh 	dsl_dir_phys_t *dd;
2003*b8902de1SWarner Losh 
2004*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2005*b8902de1SWarner Losh 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2006*b8902de1SWarner Losh 		return (EIO);
2007*b8902de1SWarner Losh 	}
2008*b8902de1SWarner Losh 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2009*b8902de1SWarner Losh 	dir_obj = ds->ds_dir_obj;
2010*b8902de1SWarner Losh 
2011*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2012*b8902de1SWarner Losh 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2013*b8902de1SWarner Losh 		return (EIO);
2014*b8902de1SWarner Losh 	}
2015*b8902de1SWarner Losh 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2016*b8902de1SWarner Losh 
2017*b8902de1SWarner Losh 	child_dir_zapobj = dd->dd_child_dir_zapobj;
2018*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2019*b8902de1SWarner Losh 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2020*b8902de1SWarner Losh 		return (EIO);
2021*b8902de1SWarner Losh 	}
2022*b8902de1SWarner Losh 
2023*b8902de1SWarner Losh 	return (zap_list(spa, &child_dir_zap) != 0);
2024*b8902de1SWarner Losh }
2025*b8902de1SWarner Losh 
2026*b8902de1SWarner Losh int
2027*b8902de1SWarner Losh zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2028*b8902de1SWarner Losh {
2029*b8902de1SWarner Losh 	uint64_t dir_obj, child_dir_zapobj, zap_type;
2030*b8902de1SWarner Losh 	dnode_phys_t child_dir_zap, dir, dataset;
2031*b8902de1SWarner Losh 	dsl_dataset_phys_t *ds;
2032*b8902de1SWarner Losh 	dsl_dir_phys_t *dd;
2033*b8902de1SWarner Losh 	int err;
2034*b8902de1SWarner Losh 
2035*b8902de1SWarner Losh 	err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2036*b8902de1SWarner Losh 	if (err != 0) {
2037*b8902de1SWarner Losh 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2038*b8902de1SWarner Losh 		return (err);
2039*b8902de1SWarner Losh 	}
2040*b8902de1SWarner Losh 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2041*b8902de1SWarner Losh 	dir_obj = ds->ds_dir_obj;
2042*b8902de1SWarner Losh 
2043*b8902de1SWarner Losh 	err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2044*b8902de1SWarner Losh 	if (err != 0) {
2045*b8902de1SWarner Losh 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2046*b8902de1SWarner Losh 		return (err);
2047*b8902de1SWarner Losh 	}
2048*b8902de1SWarner Losh 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2049*b8902de1SWarner Losh 
2050*b8902de1SWarner Losh 	child_dir_zapobj = dd->dd_child_dir_zapobj;
2051*b8902de1SWarner Losh 	err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2052*b8902de1SWarner Losh 	if (err != 0) {
2053*b8902de1SWarner Losh 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2054*b8902de1SWarner Losh 		return (err);
2055*b8902de1SWarner Losh 	}
2056*b8902de1SWarner Losh 
2057*b8902de1SWarner Losh 	err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2058*b8902de1SWarner Losh 	if (err != 0)
2059*b8902de1SWarner Losh 		return (err);
2060*b8902de1SWarner Losh 
2061*b8902de1SWarner Losh 	zap_type = *(uint64_t *) zap_scratch;
2062*b8902de1SWarner Losh 	if (zap_type == ZBT_MICRO)
2063*b8902de1SWarner Losh 		return mzap_list(&child_dir_zap, callback);
2064*b8902de1SWarner Losh 	else
2065*b8902de1SWarner Losh 		return fzap_list(spa, &child_dir_zap, callback);
2066*b8902de1SWarner Losh }
2067*b8902de1SWarner Losh #endif
2068*b8902de1SWarner Losh 
2069*b8902de1SWarner Losh /*
2070*b8902de1SWarner Losh  * Find the object set given the object number of its dataset object
2071*b8902de1SWarner Losh  * and return its details in *objset
2072*b8902de1SWarner Losh  */
2073*b8902de1SWarner Losh static int
2074*b8902de1SWarner Losh zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2075*b8902de1SWarner Losh {
2076*b8902de1SWarner Losh 	dnode_phys_t dataset;
2077*b8902de1SWarner Losh 	dsl_dataset_phys_t *ds;
2078*b8902de1SWarner Losh 
2079*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2080*b8902de1SWarner Losh 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2081*b8902de1SWarner Losh 		return (EIO);
2082*b8902de1SWarner Losh 	}
2083*b8902de1SWarner Losh 
2084*b8902de1SWarner Losh 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2085*b8902de1SWarner Losh 	if (zio_read(spa, &ds->ds_bp, objset)) {
2086*b8902de1SWarner Losh 		printf("ZFS: can't read object set for dataset %ju\n",
2087*b8902de1SWarner Losh 		    (uintmax_t)objnum);
2088*b8902de1SWarner Losh 		return (EIO);
2089*b8902de1SWarner Losh 	}
2090*b8902de1SWarner Losh 
2091*b8902de1SWarner Losh 	return (0);
2092*b8902de1SWarner Losh }
2093*b8902de1SWarner Losh 
2094*b8902de1SWarner Losh /*
2095*b8902de1SWarner Losh  * Find the object set pointed to by the BOOTFS property or the root
2096*b8902de1SWarner Losh  * dataset if there is none and return its details in *objset
2097*b8902de1SWarner Losh  */
2098*b8902de1SWarner Losh static int
2099*b8902de1SWarner Losh zfs_get_root(const spa_t *spa, uint64_t *objid)
2100*b8902de1SWarner Losh {
2101*b8902de1SWarner Losh 	dnode_phys_t dir, propdir;
2102*b8902de1SWarner Losh 	uint64_t props, bootfs, root;
2103*b8902de1SWarner Losh 
2104*b8902de1SWarner Losh 	*objid = 0;
2105*b8902de1SWarner Losh 
2106*b8902de1SWarner Losh 	/*
2107*b8902de1SWarner Losh 	 * Start with the MOS directory object.
2108*b8902de1SWarner Losh 	 */
2109*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2110*b8902de1SWarner Losh 		printf("ZFS: can't read MOS object directory\n");
2111*b8902de1SWarner Losh 		return (EIO);
2112*b8902de1SWarner Losh 	}
2113*b8902de1SWarner Losh 
2114*b8902de1SWarner Losh 	/*
2115*b8902de1SWarner Losh 	 * Lookup the pool_props and see if we can find a bootfs.
2116*b8902de1SWarner Losh 	 */
2117*b8902de1SWarner Losh 	if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2118*b8902de1SWarner Losh 	     && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2119*b8902de1SWarner Losh 	     && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2120*b8902de1SWarner Losh 	     && bootfs != 0)
2121*b8902de1SWarner Losh 	{
2122*b8902de1SWarner Losh 		*objid = bootfs;
2123*b8902de1SWarner Losh 		return (0);
2124*b8902de1SWarner Losh 	}
2125*b8902de1SWarner Losh 	/*
2126*b8902de1SWarner Losh 	 * Lookup the root dataset directory
2127*b8902de1SWarner Losh 	 */
2128*b8902de1SWarner Losh 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2129*b8902de1SWarner Losh 	    || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2130*b8902de1SWarner Losh 		printf("ZFS: can't find root dsl_dir\n");
2131*b8902de1SWarner Losh 		return (EIO);
2132*b8902de1SWarner Losh 	}
2133*b8902de1SWarner Losh 
2134*b8902de1SWarner Losh 	/*
2135*b8902de1SWarner Losh 	 * Use the information from the dataset directory's bonus buffer
2136*b8902de1SWarner Losh 	 * to find the dataset object and from that the object set itself.
2137*b8902de1SWarner Losh 	 */
2138*b8902de1SWarner Losh 	dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2139*b8902de1SWarner Losh 	*objid = dd->dd_head_dataset_obj;
2140*b8902de1SWarner Losh 	return (0);
2141*b8902de1SWarner Losh }
2142*b8902de1SWarner Losh 
2143*b8902de1SWarner Losh static int
2144*b8902de1SWarner Losh zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2145*b8902de1SWarner Losh {
2146*b8902de1SWarner Losh 
2147*b8902de1SWarner Losh 	mount->spa = spa;
2148*b8902de1SWarner Losh 
2149*b8902de1SWarner Losh 	/*
2150*b8902de1SWarner Losh 	 * Find the root object set if not explicitly provided
2151*b8902de1SWarner Losh 	 */
2152*b8902de1SWarner Losh 	if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2153*b8902de1SWarner Losh 		printf("ZFS: can't find root filesystem\n");
2154*b8902de1SWarner Losh 		return (EIO);
2155*b8902de1SWarner Losh 	}
2156*b8902de1SWarner Losh 
2157*b8902de1SWarner Losh 	if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2158*b8902de1SWarner Losh 		printf("ZFS: can't open root filesystem\n");
2159*b8902de1SWarner Losh 		return (EIO);
2160*b8902de1SWarner Losh 	}
2161*b8902de1SWarner Losh 
2162*b8902de1SWarner Losh 	mount->rootobj = rootobj;
2163*b8902de1SWarner Losh 
2164*b8902de1SWarner Losh 	return (0);
2165*b8902de1SWarner Losh }
2166*b8902de1SWarner Losh 
2167*b8902de1SWarner Losh /*
2168*b8902de1SWarner Losh  * callback function for feature name checks.
2169*b8902de1SWarner Losh  */
2170*b8902de1SWarner Losh static int
2171*b8902de1SWarner Losh check_feature(const char *name, uint64_t value)
2172*b8902de1SWarner Losh {
2173*b8902de1SWarner Losh 	int i;
2174*b8902de1SWarner Losh 
2175*b8902de1SWarner Losh 	if (value == 0)
2176*b8902de1SWarner Losh 		return (0);
2177*b8902de1SWarner Losh 	if (name[0] == '\0')
2178*b8902de1SWarner Losh 		return (0);
2179*b8902de1SWarner Losh 
2180*b8902de1SWarner Losh 	for (i = 0; features_for_read[i] != NULL; i++) {
2181*b8902de1SWarner Losh 		if (strcmp(name, features_for_read[i]) == 0)
2182*b8902de1SWarner Losh 			return (0);
2183*b8902de1SWarner Losh 	}
2184*b8902de1SWarner Losh 	printf("ZFS: unsupported feature: %s\n", name);
2185*b8902de1SWarner Losh 	return (EIO);
2186*b8902de1SWarner Losh }
2187*b8902de1SWarner Losh 
2188*b8902de1SWarner Losh /*
2189*b8902de1SWarner Losh  * Checks whether the MOS features that are active are supported.
2190*b8902de1SWarner Losh  */
2191*b8902de1SWarner Losh static int
2192*b8902de1SWarner Losh check_mos_features(const spa_t *spa)
2193*b8902de1SWarner Losh {
2194*b8902de1SWarner Losh 	dnode_phys_t dir;
2195*b8902de1SWarner Losh 	uint64_t objnum, zap_type;
2196*b8902de1SWarner Losh 	size_t size;
2197*b8902de1SWarner Losh 	int rc;
2198*b8902de1SWarner Losh 
2199*b8902de1SWarner Losh 	if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2200*b8902de1SWarner Losh 	    &dir)) != 0)
2201*b8902de1SWarner Losh 		return (rc);
2202*b8902de1SWarner Losh 	if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2203*b8902de1SWarner Losh 	    sizeof (objnum), 1, &objnum)) != 0) {
2204*b8902de1SWarner Losh 		/*
2205*b8902de1SWarner Losh 		 * It is older pool without features. As we have already
2206*b8902de1SWarner Losh 		 * tested the label, just return without raising the error.
2207*b8902de1SWarner Losh 		 */
2208*b8902de1SWarner Losh 		return (0);
2209*b8902de1SWarner Losh 	}
2210*b8902de1SWarner Losh 
2211*b8902de1SWarner Losh 	if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2212*b8902de1SWarner Losh 		return (rc);
2213*b8902de1SWarner Losh 
2214*b8902de1SWarner Losh 	if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2215*b8902de1SWarner Losh 		return (EIO);
2216*b8902de1SWarner Losh 
2217*b8902de1SWarner Losh 	size = dir.dn_datablkszsec * 512;
2218*b8902de1SWarner Losh 	if (dnode_read(spa, &dir, 0, zap_scratch, size))
2219*b8902de1SWarner Losh 		return (EIO);
2220*b8902de1SWarner Losh 
2221*b8902de1SWarner Losh 	zap_type = *(uint64_t *) zap_scratch;
2222*b8902de1SWarner Losh 	if (zap_type == ZBT_MICRO)
2223*b8902de1SWarner Losh 		rc = mzap_list(&dir, check_feature);
2224*b8902de1SWarner Losh 	else
2225*b8902de1SWarner Losh 		rc = fzap_list(spa, &dir, check_feature);
2226*b8902de1SWarner Losh 
2227*b8902de1SWarner Losh 	return (rc);
2228*b8902de1SWarner Losh }
2229*b8902de1SWarner Losh 
2230*b8902de1SWarner Losh static int
2231*b8902de1SWarner Losh zfs_spa_init(spa_t *spa)
2232*b8902de1SWarner Losh {
2233*b8902de1SWarner Losh 	dnode_phys_t dir;
2234*b8902de1SWarner Losh 	int rc;
2235*b8902de1SWarner Losh 
2236*b8902de1SWarner Losh 	if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2237*b8902de1SWarner Losh 		printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2238*b8902de1SWarner Losh 		return (EIO);
2239*b8902de1SWarner Losh 	}
2240*b8902de1SWarner Losh 	if (spa->spa_mos.os_type != DMU_OST_META) {
2241*b8902de1SWarner Losh 		printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2242*b8902de1SWarner Losh 		return (EIO);
2243*b8902de1SWarner Losh 	}
2244*b8902de1SWarner Losh 
2245*b8902de1SWarner Losh 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2246*b8902de1SWarner Losh 	    &dir)) {
2247*b8902de1SWarner Losh 		printf("ZFS: failed to read pool %s directory object\n",
2248*b8902de1SWarner Losh 		    spa->spa_name);
2249*b8902de1SWarner Losh 		return (EIO);
2250*b8902de1SWarner Losh 	}
2251*b8902de1SWarner Losh 	/* this is allowed to fail, older pools do not have salt */
2252*b8902de1SWarner Losh 	rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2253*b8902de1SWarner Losh 	    sizeof (spa->spa_cksum_salt.zcs_bytes),
2254*b8902de1SWarner Losh 	    spa->spa_cksum_salt.zcs_bytes);
2255*b8902de1SWarner Losh 
2256*b8902de1SWarner Losh 	rc = check_mos_features(spa);
2257*b8902de1SWarner Losh 	if (rc != 0) {
2258*b8902de1SWarner Losh 		printf("ZFS: pool %s is not supported\n", spa->spa_name);
2259*b8902de1SWarner Losh 	}
2260*b8902de1SWarner Losh 
2261*b8902de1SWarner Losh 	return (rc);
2262*b8902de1SWarner Losh }
2263*b8902de1SWarner Losh 
2264*b8902de1SWarner Losh static int
2265*b8902de1SWarner Losh zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2266*b8902de1SWarner Losh {
2267*b8902de1SWarner Losh 
2268*b8902de1SWarner Losh 	if (dn->dn_bonustype != DMU_OT_SA) {
2269*b8902de1SWarner Losh 		znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2270*b8902de1SWarner Losh 
2271*b8902de1SWarner Losh 		sb->st_mode = zp->zp_mode;
2272*b8902de1SWarner Losh 		sb->st_uid = zp->zp_uid;
2273*b8902de1SWarner Losh 		sb->st_gid = zp->zp_gid;
2274*b8902de1SWarner Losh 		sb->st_size = zp->zp_size;
2275*b8902de1SWarner Losh 	} else {
2276*b8902de1SWarner Losh 		sa_hdr_phys_t *sahdrp;
2277*b8902de1SWarner Losh 		int hdrsize;
2278*b8902de1SWarner Losh 		size_t size = 0;
2279*b8902de1SWarner Losh 		void *buf = NULL;
2280*b8902de1SWarner Losh 
2281*b8902de1SWarner Losh 		if (dn->dn_bonuslen != 0)
2282*b8902de1SWarner Losh 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2283*b8902de1SWarner Losh 		else {
2284*b8902de1SWarner Losh 			if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2285*b8902de1SWarner Losh 				blkptr_t *bp = DN_SPILL_BLKPTR(dn);
2286*b8902de1SWarner Losh 				int error;
2287*b8902de1SWarner Losh 
2288*b8902de1SWarner Losh 				size = BP_GET_LSIZE(bp);
2289*b8902de1SWarner Losh 				buf = zfs_alloc(size);
2290*b8902de1SWarner Losh 				error = zio_read(spa, bp, buf);
2291*b8902de1SWarner Losh 				if (error != 0) {
2292*b8902de1SWarner Losh 					zfs_free(buf, size);
2293*b8902de1SWarner Losh 					return (error);
2294*b8902de1SWarner Losh 				}
2295*b8902de1SWarner Losh 				sahdrp = buf;
2296*b8902de1SWarner Losh 			} else {
2297*b8902de1SWarner Losh 				return (EIO);
2298*b8902de1SWarner Losh 			}
2299*b8902de1SWarner Losh 		}
2300*b8902de1SWarner Losh 		hdrsize = SA_HDR_SIZE(sahdrp);
2301*b8902de1SWarner Losh 		sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2302*b8902de1SWarner Losh 		    SA_MODE_OFFSET);
2303*b8902de1SWarner Losh 		sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2304*b8902de1SWarner Losh 		    SA_UID_OFFSET);
2305*b8902de1SWarner Losh 		sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2306*b8902de1SWarner Losh 		    SA_GID_OFFSET);
2307*b8902de1SWarner Losh 		sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2308*b8902de1SWarner Losh 		    SA_SIZE_OFFSET);
2309*b8902de1SWarner Losh 		if (buf != NULL)
2310*b8902de1SWarner Losh 			zfs_free(buf, size);
2311*b8902de1SWarner Losh 	}
2312*b8902de1SWarner Losh 
2313*b8902de1SWarner Losh 	return (0);
2314*b8902de1SWarner Losh }
2315*b8902de1SWarner Losh 
2316*b8902de1SWarner Losh static int
2317*b8902de1SWarner Losh zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2318*b8902de1SWarner Losh {
2319*b8902de1SWarner Losh 	int rc = 0;
2320*b8902de1SWarner Losh 
2321*b8902de1SWarner Losh 	if (dn->dn_bonustype == DMU_OT_SA) {
2322*b8902de1SWarner Losh 		sa_hdr_phys_t *sahdrp = NULL;
2323*b8902de1SWarner Losh 		size_t size = 0;
2324*b8902de1SWarner Losh 		void *buf = NULL;
2325*b8902de1SWarner Losh 		int hdrsize;
2326*b8902de1SWarner Losh 		char *p;
2327*b8902de1SWarner Losh 
2328*b8902de1SWarner Losh 		if (dn->dn_bonuslen != 0)
2329*b8902de1SWarner Losh 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2330*b8902de1SWarner Losh 		else {
2331*b8902de1SWarner Losh 			blkptr_t *bp;
2332*b8902de1SWarner Losh 
2333*b8902de1SWarner Losh 			if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2334*b8902de1SWarner Losh 				return (EIO);
2335*b8902de1SWarner Losh 			bp = DN_SPILL_BLKPTR(dn);
2336*b8902de1SWarner Losh 
2337*b8902de1SWarner Losh 			size = BP_GET_LSIZE(bp);
2338*b8902de1SWarner Losh 			buf = zfs_alloc(size);
2339*b8902de1SWarner Losh 			rc = zio_read(spa, bp, buf);
2340*b8902de1SWarner Losh 			if (rc != 0) {
2341*b8902de1SWarner Losh 				zfs_free(buf, size);
2342*b8902de1SWarner Losh 				return (rc);
2343*b8902de1SWarner Losh 			}
2344*b8902de1SWarner Losh 			sahdrp = buf;
2345*b8902de1SWarner Losh 		}
2346*b8902de1SWarner Losh 		hdrsize = SA_HDR_SIZE(sahdrp);
2347*b8902de1SWarner Losh 		p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2348*b8902de1SWarner Losh 		memcpy(path, p, psize);
2349*b8902de1SWarner Losh 		if (buf != NULL)
2350*b8902de1SWarner Losh 			zfs_free(buf, size);
2351*b8902de1SWarner Losh 		return (0);
2352*b8902de1SWarner Losh 	}
2353*b8902de1SWarner Losh 	/*
2354*b8902de1SWarner Losh 	 * Second test is purely to silence bogus compiler
2355*b8902de1SWarner Losh 	 * warning about accessing past the end of dn_bonus.
2356*b8902de1SWarner Losh 	 */
2357*b8902de1SWarner Losh 	if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2358*b8902de1SWarner Losh 	    sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2359*b8902de1SWarner Losh 		memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2360*b8902de1SWarner Losh 	} else {
2361*b8902de1SWarner Losh 		rc = dnode_read(spa, dn, 0, path, psize);
2362*b8902de1SWarner Losh 	}
2363*b8902de1SWarner Losh 	return (rc);
2364*b8902de1SWarner Losh }
2365*b8902de1SWarner Losh 
2366*b8902de1SWarner Losh struct obj_list {
2367*b8902de1SWarner Losh 	uint64_t		objnum;
2368*b8902de1SWarner Losh 	STAILQ_ENTRY(obj_list)	entry;
2369*b8902de1SWarner Losh };
2370*b8902de1SWarner Losh 
2371*b8902de1SWarner Losh /*
2372*b8902de1SWarner Losh  * Lookup a file and return its dnode.
2373*b8902de1SWarner Losh  */
2374*b8902de1SWarner Losh static int
2375*b8902de1SWarner Losh zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2376*b8902de1SWarner Losh {
2377*b8902de1SWarner Losh 	int rc;
2378*b8902de1SWarner Losh 	uint64_t objnum;
2379*b8902de1SWarner Losh 	const spa_t *spa;
2380*b8902de1SWarner Losh 	dnode_phys_t dn;
2381*b8902de1SWarner Losh 	const char *p, *q;
2382*b8902de1SWarner Losh 	char element[256];
2383*b8902de1SWarner Losh 	char path[1024];
2384*b8902de1SWarner Losh 	int symlinks_followed = 0;
2385*b8902de1SWarner Losh 	struct stat sb;
2386*b8902de1SWarner Losh 	struct obj_list *entry, *tentry;
2387*b8902de1SWarner Losh 	STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2388*b8902de1SWarner Losh 
2389*b8902de1SWarner Losh 	spa = mount->spa;
2390*b8902de1SWarner Losh 	if (mount->objset.os_type != DMU_OST_ZFS) {
2391*b8902de1SWarner Losh 		printf("ZFS: unexpected object set type %ju\n",
2392*b8902de1SWarner Losh 		    (uintmax_t)mount->objset.os_type);
2393*b8902de1SWarner Losh 		return (EIO);
2394*b8902de1SWarner Losh 	}
2395*b8902de1SWarner Losh 
2396*b8902de1SWarner Losh 	if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2397*b8902de1SWarner Losh 		return (ENOMEM);
2398*b8902de1SWarner Losh 
2399*b8902de1SWarner Losh 	/*
2400*b8902de1SWarner Losh 	 * Get the root directory dnode.
2401*b8902de1SWarner Losh 	 */
2402*b8902de1SWarner Losh 	rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2403*b8902de1SWarner Losh 	if (rc) {
2404*b8902de1SWarner Losh 		free(entry);
2405*b8902de1SWarner Losh 		return (rc);
2406*b8902de1SWarner Losh 	}
2407*b8902de1SWarner Losh 
2408*b8902de1SWarner Losh 	rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
2409*b8902de1SWarner Losh 	if (rc) {
2410*b8902de1SWarner Losh 		free(entry);
2411*b8902de1SWarner Losh 		return (rc);
2412*b8902de1SWarner Losh 	}
2413*b8902de1SWarner Losh 	entry->objnum = objnum;
2414*b8902de1SWarner Losh 	STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2415*b8902de1SWarner Losh 
2416*b8902de1SWarner Losh 	rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2417*b8902de1SWarner Losh 	if (rc != 0)
2418*b8902de1SWarner Losh 		goto done;
2419*b8902de1SWarner Losh 
2420*b8902de1SWarner Losh 	p = upath;
2421*b8902de1SWarner Losh 	while (p && *p) {
2422*b8902de1SWarner Losh 		rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2423*b8902de1SWarner Losh 		if (rc != 0)
2424*b8902de1SWarner Losh 			goto done;
2425*b8902de1SWarner Losh 
2426*b8902de1SWarner Losh 		while (*p == '/')
2427*b8902de1SWarner Losh 			p++;
2428*b8902de1SWarner Losh 		if (*p == '\0')
2429*b8902de1SWarner Losh 			break;
2430*b8902de1SWarner Losh 		q = p;
2431*b8902de1SWarner Losh 		while (*q != '\0' && *q != '/')
2432*b8902de1SWarner Losh 			q++;
2433*b8902de1SWarner Losh 
2434*b8902de1SWarner Losh 		/* skip dot */
2435*b8902de1SWarner Losh 		if (p + 1 == q && p[0] == '.') {
2436*b8902de1SWarner Losh 			p++;
2437*b8902de1SWarner Losh 			continue;
2438*b8902de1SWarner Losh 		}
2439*b8902de1SWarner Losh 		/* double dot */
2440*b8902de1SWarner Losh 		if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2441*b8902de1SWarner Losh 			p += 2;
2442*b8902de1SWarner Losh 			if (STAILQ_FIRST(&on_cache) ==
2443*b8902de1SWarner Losh 			    STAILQ_LAST(&on_cache, obj_list, entry)) {
2444*b8902de1SWarner Losh 				rc = ENOENT;
2445*b8902de1SWarner Losh 				goto done;
2446*b8902de1SWarner Losh 			}
2447*b8902de1SWarner Losh 			entry = STAILQ_FIRST(&on_cache);
2448*b8902de1SWarner Losh 			STAILQ_REMOVE_HEAD(&on_cache, entry);
2449*b8902de1SWarner Losh 			free(entry);
2450*b8902de1SWarner Losh 			objnum = (STAILQ_FIRST(&on_cache))->objnum;
2451*b8902de1SWarner Losh 			continue;
2452*b8902de1SWarner Losh 		}
2453*b8902de1SWarner Losh 		if (q - p + 1 > sizeof(element)) {
2454*b8902de1SWarner Losh 			rc = ENAMETOOLONG;
2455*b8902de1SWarner Losh 			goto done;
2456*b8902de1SWarner Losh 		}
2457*b8902de1SWarner Losh 		memcpy(element, p, q - p);
2458*b8902de1SWarner Losh 		element[q - p] = 0;
2459*b8902de1SWarner Losh 		p = q;
2460*b8902de1SWarner Losh 
2461*b8902de1SWarner Losh 		if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2462*b8902de1SWarner Losh 			goto done;
2463*b8902de1SWarner Losh 		if (!S_ISDIR(sb.st_mode)) {
2464*b8902de1SWarner Losh 			rc = ENOTDIR;
2465*b8902de1SWarner Losh 			goto done;
2466*b8902de1SWarner Losh 		}
2467*b8902de1SWarner Losh 
2468*b8902de1SWarner Losh 		rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2469*b8902de1SWarner Losh 		if (rc)
2470*b8902de1SWarner Losh 			goto done;
2471*b8902de1SWarner Losh 		objnum = ZFS_DIRENT_OBJ(objnum);
2472*b8902de1SWarner Losh 
2473*b8902de1SWarner Losh 		if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2474*b8902de1SWarner Losh 			rc = ENOMEM;
2475*b8902de1SWarner Losh 			goto done;
2476*b8902de1SWarner Losh 		}
2477*b8902de1SWarner Losh 		entry->objnum = objnum;
2478*b8902de1SWarner Losh 		STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2479*b8902de1SWarner Losh 		rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2480*b8902de1SWarner Losh 		if (rc)
2481*b8902de1SWarner Losh 			goto done;
2482*b8902de1SWarner Losh 
2483*b8902de1SWarner Losh 		/*
2484*b8902de1SWarner Losh 		 * Check for symlink.
2485*b8902de1SWarner Losh 		 */
2486*b8902de1SWarner Losh 		rc = zfs_dnode_stat(spa, &dn, &sb);
2487*b8902de1SWarner Losh 		if (rc)
2488*b8902de1SWarner Losh 			goto done;
2489*b8902de1SWarner Losh 		if (S_ISLNK(sb.st_mode)) {
2490*b8902de1SWarner Losh 			if (symlinks_followed > 10) {
2491*b8902de1SWarner Losh 				rc = EMLINK;
2492*b8902de1SWarner Losh 				goto done;
2493*b8902de1SWarner Losh 			}
2494*b8902de1SWarner Losh 			symlinks_followed++;
2495*b8902de1SWarner Losh 
2496*b8902de1SWarner Losh 			/*
2497*b8902de1SWarner Losh 			 * Read the link value and copy the tail of our
2498*b8902de1SWarner Losh 			 * current path onto the end.
2499*b8902de1SWarner Losh 			 */
2500*b8902de1SWarner Losh 			if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2501*b8902de1SWarner Losh 				rc = ENAMETOOLONG;
2502*b8902de1SWarner Losh 				goto done;
2503*b8902de1SWarner Losh 			}
2504*b8902de1SWarner Losh 			strcpy(&path[sb.st_size], p);
2505*b8902de1SWarner Losh 
2506*b8902de1SWarner Losh 			rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2507*b8902de1SWarner Losh 			if (rc != 0)
2508*b8902de1SWarner Losh 				goto done;
2509*b8902de1SWarner Losh 
2510*b8902de1SWarner Losh 			/*
2511*b8902de1SWarner Losh 			 * Restart with the new path, starting either at
2512*b8902de1SWarner Losh 			 * the root or at the parent depending whether or
2513*b8902de1SWarner Losh 			 * not the link is relative.
2514*b8902de1SWarner Losh 			 */
2515*b8902de1SWarner Losh 			p = path;
2516*b8902de1SWarner Losh 			if (*p == '/') {
2517*b8902de1SWarner Losh 				while (STAILQ_FIRST(&on_cache) !=
2518*b8902de1SWarner Losh 				    STAILQ_LAST(&on_cache, obj_list, entry)) {
2519*b8902de1SWarner Losh 					entry = STAILQ_FIRST(&on_cache);
2520*b8902de1SWarner Losh 					STAILQ_REMOVE_HEAD(&on_cache, entry);
2521*b8902de1SWarner Losh 					free(entry);
2522*b8902de1SWarner Losh 				}
2523*b8902de1SWarner Losh 			} else {
2524*b8902de1SWarner Losh 				entry = STAILQ_FIRST(&on_cache);
2525*b8902de1SWarner Losh 				STAILQ_REMOVE_HEAD(&on_cache, entry);
2526*b8902de1SWarner Losh 				free(entry);
2527*b8902de1SWarner Losh 			}
2528*b8902de1SWarner Losh 			objnum = (STAILQ_FIRST(&on_cache))->objnum;
2529*b8902de1SWarner Losh 		}
2530*b8902de1SWarner Losh 	}
2531*b8902de1SWarner Losh 
2532*b8902de1SWarner Losh 	*dnode = dn;
2533*b8902de1SWarner Losh done:
2534*b8902de1SWarner Losh 	STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
2535*b8902de1SWarner Losh 		free(entry);
2536*b8902de1SWarner Losh 	return (rc);
2537*b8902de1SWarner Losh }
2538