xref: /freebsd/stand/libsa/zfs/zfsimpl.c (revision 13138422bc354a1ec35f53a27c4efeccdffc5639)
1 /*-
2  * Copyright (c) 2007 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  *	Stand-alone ZFS file reader.
32  */
33 
34 #include <sys/endian.h>
35 #include <sys/stat.h>
36 #include <sys/stdint.h>
37 #include <sys/list.h>
38 #include <machine/_inttypes.h>
39 
40 #include "zfsimpl.h"
41 #include "zfssubr.c"
42 
43 
44 struct zfsmount {
45 	const spa_t	*spa;
46 	objset_phys_t	objset;
47 	uint64_t	rootobj;
48 };
49 static struct zfsmount zfsmount __unused;
50 
51 /*
52  * The indirect_child_t represents the vdev that we will read from, when we
53  * need to read all copies of the data (e.g. for scrub or reconstruction).
54  * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
55  * ic_vdev is the same as is_vdev.  However, for mirror top-level vdevs,
56  * ic_vdev is a child of the mirror.
57  */
58 typedef struct indirect_child {
59 	void *ic_data;
60 	vdev_t *ic_vdev;
61 } indirect_child_t;
62 
63 /*
64  * The indirect_split_t represents one mapped segment of an i/o to the
65  * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
66  * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
67  * For split blocks, there will be several of these.
68  */
69 typedef struct indirect_split {
70 	list_node_t is_node; /* link on iv_splits */
71 
72 	/*
73 	 * is_split_offset is the offset into the i/o.
74 	 * This is the sum of the previous splits' is_size's.
75 	 */
76 	uint64_t is_split_offset;
77 
78 	vdev_t *is_vdev; /* top-level vdev */
79 	uint64_t is_target_offset; /* offset on is_vdev */
80 	uint64_t is_size;
81 	int is_children; /* number of entries in is_child[] */
82 
83 	/*
84 	 * is_good_child is the child that we are currently using to
85 	 * attempt reconstruction.
86 	 */
87 	int is_good_child;
88 
89 	indirect_child_t is_child[1]; /* variable-length */
90 } indirect_split_t;
91 
92 /*
93  * The indirect_vsd_t is associated with each i/o to the indirect vdev.
94  * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
95  */
96 typedef struct indirect_vsd {
97 	boolean_t iv_split_block;
98 	boolean_t iv_reconstruct;
99 
100 	list_t iv_splits; /* list of indirect_split_t's */
101 } indirect_vsd_t;
102 
103 /*
104  * List of all vdevs, chained through v_alllink.
105  */
106 static vdev_list_t zfs_vdevs;
107 
108 /*
109  * List of ZFS features supported for read
110  */
111 static const char *features_for_read[] = {
112 	"org.illumos:lz4_compress",
113 	"com.delphix:hole_birth",
114 	"com.delphix:extensible_dataset",
115 	"com.delphix:embedded_data",
116 	"org.open-zfs:large_blocks",
117 	"org.illumos:sha512",
118 	"org.illumos:skein",
119 	"org.zfsonlinux:large_dnode",
120 	"com.joyent:multi_vdev_crash_dump",
121 	"com.delphix:spacemap_histogram",
122 	"com.delphix:zpool_checkpoint",
123 	"com.delphix:spacemap_v2",
124 	"com.datto:encryption",
125 	"org.zfsonlinux:allocation_classes",
126 	"com.datto:resilver_defer",
127 	"com.delphix:device_removal",
128 	"com.delphix:obsolete_counts",
129 	"com.intel:allocation_classes",
130 	NULL
131 };
132 
133 /*
134  * List of all pools, chained through spa_link.
135  */
136 static spa_list_t zfs_pools;
137 
138 static const dnode_phys_t *dnode_cache_obj;
139 static uint64_t dnode_cache_bn;
140 static char *dnode_cache_buf;
141 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
142 
143 #define	TEMP_SIZE	(1024 * 1024)
144 
145 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
146 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
147 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
148 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
149     const char *name, uint64_t integer_size, uint64_t num_integers,
150     void *value);
151 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
152     dnode_phys_t *);
153 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
154     size_t);
155 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
156     size_t);
157 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
158 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
159     uint64_t);
160 vdev_indirect_mapping_entry_phys_t *
161     vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
162     uint64_t, uint64_t *);
163 
164 static void
165 zfs_init(void)
166 {
167 	STAILQ_INIT(&zfs_vdevs);
168 	STAILQ_INIT(&zfs_pools);
169 
170 	zfs_temp_buf = malloc(TEMP_SIZE);
171 	zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
172 	zfs_temp_ptr = zfs_temp_buf;
173 	dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
174 
175 	zfs_init_crc();
176 }
177 
178 static void *
179 zfs_alloc(size_t size)
180 {
181 	char *ptr;
182 
183 	if (zfs_temp_ptr + size > zfs_temp_end) {
184 		panic("ZFS: out of temporary buffer space");
185 	}
186 	ptr = zfs_temp_ptr;
187 	zfs_temp_ptr += size;
188 
189 	return (ptr);
190 }
191 
192 static void
193 zfs_free(void *ptr, size_t size)
194 {
195 
196 	zfs_temp_ptr -= size;
197 	if (zfs_temp_ptr != ptr) {
198 		panic("ZFS: zfs_alloc()/zfs_free() mismatch");
199 	}
200 }
201 
202 static int
203 xdr_int(const unsigned char **xdr, int *ip)
204 {
205 	*ip = be32dec(*xdr);
206 	(*xdr) += 4;
207 	return (0);
208 }
209 
210 static int
211 xdr_u_int(const unsigned char **xdr, u_int *ip)
212 {
213 	*ip = be32dec(*xdr);
214 	(*xdr) += 4;
215 	return (0);
216 }
217 
218 static int
219 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
220 {
221 	u_int hi, lo;
222 
223 	xdr_u_int(xdr, &hi);
224 	xdr_u_int(xdr, &lo);
225 	*lp = (((uint64_t)hi) << 32) | lo;
226 	return (0);
227 }
228 
229 static int
230 nvlist_find(const unsigned char *nvlist, const char *name, int type,
231     int *elementsp, void *valuep)
232 {
233 	const unsigned char *p, *pair;
234 	int junk;
235 	int encoded_size, decoded_size;
236 
237 	p = nvlist;
238 	xdr_int(&p, &junk);
239 	xdr_int(&p, &junk);
240 
241 	pair = p;
242 	xdr_int(&p, &encoded_size);
243 	xdr_int(&p, &decoded_size);
244 	while (encoded_size && decoded_size) {
245 		int namelen, pairtype, elements;
246 		const char *pairname;
247 
248 		xdr_int(&p, &namelen);
249 		pairname = (const char *)p;
250 		p += roundup(namelen, 4);
251 		xdr_int(&p, &pairtype);
252 
253 		if (memcmp(name, pairname, namelen) == 0 && type == pairtype) {
254 			xdr_int(&p, &elements);
255 			if (elementsp)
256 				*elementsp = elements;
257 			if (type == DATA_TYPE_UINT64) {
258 				xdr_uint64_t(&p, (uint64_t *)valuep);
259 				return (0);
260 			} else if (type == DATA_TYPE_STRING) {
261 				int len;
262 				xdr_int(&p, &len);
263 				(*(const char **)valuep) = (const char *)p;
264 				return (0);
265 			} else if (type == DATA_TYPE_NVLIST ||
266 			    type == DATA_TYPE_NVLIST_ARRAY) {
267 				(*(const unsigned char **)valuep) =
268 				    (const unsigned char *)p;
269 				return (0);
270 			} else {
271 				return (EIO);
272 			}
273 		} else {
274 			/*
275 			 * Not the pair we are looking for, skip to the
276 			 * next one.
277 			 */
278 			p = pair + encoded_size;
279 		}
280 
281 		pair = p;
282 		xdr_int(&p, &encoded_size);
283 		xdr_int(&p, &decoded_size);
284 	}
285 
286 	return (EIO);
287 }
288 
289 static int
290 nvlist_check_features_for_read(const unsigned char *nvlist)
291 {
292 	const unsigned char *p, *pair;
293 	int junk;
294 	int encoded_size, decoded_size;
295 	int rc;
296 
297 	rc = 0;
298 
299 	p = nvlist;
300 	xdr_int(&p, &junk);
301 	xdr_int(&p, &junk);
302 
303 	pair = p;
304 	xdr_int(&p, &encoded_size);
305 	xdr_int(&p, &decoded_size);
306 	while (encoded_size && decoded_size) {
307 		int namelen, pairtype;
308 		const char *pairname;
309 		int i, found;
310 
311 		found = 0;
312 
313 		xdr_int(&p, &namelen);
314 		pairname = (const char *)p;
315 		p += roundup(namelen, 4);
316 		xdr_int(&p, &pairtype);
317 
318 		for (i = 0; features_for_read[i] != NULL; i++) {
319 			if (memcmp(pairname, features_for_read[i],
320 			    namelen) == 0) {
321 				found = 1;
322 				break;
323 			}
324 		}
325 
326 		if (!found) {
327 			printf("ZFS: unsupported feature: %s\n", pairname);
328 			rc = EIO;
329 		}
330 
331 		p = pair + encoded_size;
332 
333 		pair = p;
334 		xdr_int(&p, &encoded_size);
335 		xdr_int(&p, &decoded_size);
336 	}
337 
338 	return (rc);
339 }
340 
341 /*
342  * Return the next nvlist in an nvlist array.
343  */
344 static const unsigned char *
345 nvlist_next(const unsigned char *nvlist)
346 {
347 	const unsigned char *p, *pair;
348 	int junk;
349 	int encoded_size, decoded_size;
350 
351 	p = nvlist;
352 	xdr_int(&p, &junk);
353 	xdr_int(&p, &junk);
354 
355 	pair = p;
356 	xdr_int(&p, &encoded_size);
357 	xdr_int(&p, &decoded_size);
358 	while (encoded_size && decoded_size) {
359 		p = pair + encoded_size;
360 
361 		pair = p;
362 		xdr_int(&p, &encoded_size);
363 		xdr_int(&p, &decoded_size);
364 	}
365 
366 	return (p);
367 }
368 
369 #ifdef TEST
370 
371 static const unsigned char *
372 nvlist_print(const unsigned char *nvlist, unsigned int indent)
373 {
374 	static const char *typenames[] = {
375 		"DATA_TYPE_UNKNOWN",
376 		"DATA_TYPE_BOOLEAN",
377 		"DATA_TYPE_BYTE",
378 		"DATA_TYPE_INT16",
379 		"DATA_TYPE_UINT16",
380 		"DATA_TYPE_INT32",
381 		"DATA_TYPE_UINT32",
382 		"DATA_TYPE_INT64",
383 		"DATA_TYPE_UINT64",
384 		"DATA_TYPE_STRING",
385 		"DATA_TYPE_BYTE_ARRAY",
386 		"DATA_TYPE_INT16_ARRAY",
387 		"DATA_TYPE_UINT16_ARRAY",
388 		"DATA_TYPE_INT32_ARRAY",
389 		"DATA_TYPE_UINT32_ARRAY",
390 		"DATA_TYPE_INT64_ARRAY",
391 		"DATA_TYPE_UINT64_ARRAY",
392 		"DATA_TYPE_STRING_ARRAY",
393 		"DATA_TYPE_HRTIME",
394 		"DATA_TYPE_NVLIST",
395 		"DATA_TYPE_NVLIST_ARRAY",
396 		"DATA_TYPE_BOOLEAN_VALUE",
397 		"DATA_TYPE_INT8",
398 		"DATA_TYPE_UINT8",
399 		"DATA_TYPE_BOOLEAN_ARRAY",
400 		"DATA_TYPE_INT8_ARRAY",
401 		"DATA_TYPE_UINT8_ARRAY"
402 	};
403 
404 	unsigned int i, j;
405 	const unsigned char *p, *pair;
406 	int junk;
407 	int encoded_size, decoded_size;
408 
409 	p = nvlist;
410 	xdr_int(&p, &junk);
411 	xdr_int(&p, &junk);
412 
413 	pair = p;
414 	xdr_int(&p, &encoded_size);
415 	xdr_int(&p, &decoded_size);
416 	while (encoded_size && decoded_size) {
417 		int namelen, pairtype, elements;
418 		const char *pairname;
419 
420 		xdr_int(&p, &namelen);
421 		pairname = (const char *)p;
422 		p += roundup(namelen, 4);
423 		xdr_int(&p, &pairtype);
424 
425 		for (i = 0; i < indent; i++)
426 			printf(" ");
427 		printf("%s %s", typenames[pairtype], pairname);
428 
429 		xdr_int(&p, &elements);
430 		switch (pairtype) {
431 		case DATA_TYPE_UINT64: {
432 			uint64_t val;
433 			xdr_uint64_t(&p, &val);
434 			printf(" = 0x%jx\n", (uintmax_t)val);
435 			break;
436 		}
437 
438 		case DATA_TYPE_STRING: {
439 			int len;
440 			xdr_int(&p, &len);
441 			printf(" = \"%s\"\n", p);
442 			break;
443 		}
444 
445 		case DATA_TYPE_NVLIST:
446 			printf("\n");
447 			nvlist_print(p, indent + 1);
448 			break;
449 
450 		case DATA_TYPE_NVLIST_ARRAY:
451 			for (j = 0; j < elements; j++) {
452 				printf("[%d]\n", j);
453 				p = nvlist_print(p, indent + 1);
454 				if (j != elements - 1) {
455 					for (i = 0; i < indent; i++)
456 						printf(" ");
457 					printf("%s %s", typenames[pairtype],
458 					    pairname);
459 				}
460 			}
461 			break;
462 
463 		default:
464 			printf("\n");
465 		}
466 
467 		p = pair + encoded_size;
468 
469 		pair = p;
470 		xdr_int(&p, &encoded_size);
471 		xdr_int(&p, &decoded_size);
472 	}
473 
474 	return (p);
475 }
476 
477 #endif
478 
479 static int
480 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
481     off_t offset, size_t size)
482 {
483 	size_t psize;
484 	int rc;
485 
486 	if (!vdev->v_phys_read)
487 		return (EIO);
488 
489 	if (bp) {
490 		psize = BP_GET_PSIZE(bp);
491 	} else {
492 		psize = size;
493 	}
494 
495 	rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
496 	if (rc == 0) {
497 		if (bp != NULL)
498 			rc = zio_checksum_verify(vdev->v_spa, bp, buf);
499 	}
500 
501 	return (rc);
502 }
503 
504 typedef struct remap_segment {
505 	vdev_t *rs_vd;
506 	uint64_t rs_offset;
507 	uint64_t rs_asize;
508 	uint64_t rs_split_offset;
509 	list_node_t rs_node;
510 } remap_segment_t;
511 
512 static remap_segment_t *
513 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
514 {
515 	remap_segment_t *rs = malloc(sizeof (remap_segment_t));
516 
517 	if (rs != NULL) {
518 		rs->rs_vd = vd;
519 		rs->rs_offset = offset;
520 		rs->rs_asize = asize;
521 		rs->rs_split_offset = split_offset;
522 	}
523 
524 	return (rs);
525 }
526 
527 vdev_indirect_mapping_t *
528 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
529     uint64_t mapping_object)
530 {
531 	vdev_indirect_mapping_t *vim;
532 	vdev_indirect_mapping_phys_t *vim_phys;
533 	int rc;
534 
535 	vim = calloc(1, sizeof (*vim));
536 	if (vim == NULL)
537 		return (NULL);
538 
539 	vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
540 	if (vim->vim_dn == NULL) {
541 		free(vim);
542 		return (NULL);
543 	}
544 
545 	rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
546 	if (rc != 0) {
547 		free(vim->vim_dn);
548 		free(vim);
549 		return (NULL);
550 	}
551 
552 	vim->vim_spa = spa;
553 	vim->vim_phys = malloc(sizeof (*vim->vim_phys));
554 	if (vim->vim_phys == NULL) {
555 		free(vim->vim_dn);
556 		free(vim);
557 		return (NULL);
558 	}
559 
560 	vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
561 	*vim->vim_phys = *vim_phys;
562 
563 	vim->vim_objset = os;
564 	vim->vim_object = mapping_object;
565 	vim->vim_entries = NULL;
566 
567 	vim->vim_havecounts =
568 	    (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
569 
570 	return (vim);
571 }
572 
573 /*
574  * Compare an offset with an indirect mapping entry; there are three
575  * possible scenarios:
576  *
577  *     1. The offset is "less than" the mapping entry; meaning the
578  *        offset is less than the source offset of the mapping entry. In
579  *        this case, there is no overlap between the offset and the
580  *        mapping entry and -1 will be returned.
581  *
582  *     2. The offset is "greater than" the mapping entry; meaning the
583  *        offset is greater than the mapping entry's source offset plus
584  *        the entry's size. In this case, there is no overlap between
585  *        the offset and the mapping entry and 1 will be returned.
586  *
587  *        NOTE: If the offset is actually equal to the entry's offset
588  *        plus size, this is considered to be "greater" than the entry,
589  *        and this case applies (i.e. 1 will be returned). Thus, the
590  *        entry's "range" can be considered to be inclusive at its
591  *        start, but exclusive at its end: e.g. [src, src + size).
592  *
593  *     3. The last case to consider is if the offset actually falls
594  *        within the mapping entry's range. If this is the case, the
595  *        offset is considered to be "equal to" the mapping entry and
596  *        0 will be returned.
597  *
598  *        NOTE: If the offset is equal to the entry's source offset,
599  *        this case applies and 0 will be returned. If the offset is
600  *        equal to the entry's source plus its size, this case does
601  *        *not* apply (see "NOTE" above for scenario 2), and 1 will be
602  *        returned.
603  */
604 static int
605 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
606 {
607 	const uint64_t *key = v_key;
608 	const vdev_indirect_mapping_entry_phys_t *array_elem =
609 	    v_array_elem;
610 	uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
611 
612 	if (*key < src_offset) {
613 		return (-1);
614 	} else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
615 		return (0);
616 	} else {
617 		return (1);
618 	}
619 }
620 
621 /*
622  * Return array entry.
623  */
624 static vdev_indirect_mapping_entry_phys_t *
625 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
626 {
627 	uint64_t size;
628 	off_t offset = 0;
629 	int rc;
630 
631 	if (vim->vim_phys->vimp_num_entries == 0)
632 		return (NULL);
633 
634 	if (vim->vim_entries == NULL) {
635 		uint64_t bsize;
636 
637 		bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
638 		size = vim->vim_phys->vimp_num_entries *
639 		    sizeof (*vim->vim_entries);
640 		if (size > bsize) {
641 			size = bsize / sizeof (*vim->vim_entries);
642 			size *= sizeof (*vim->vim_entries);
643 		}
644 		vim->vim_entries = malloc(size);
645 		if (vim->vim_entries == NULL)
646 			return (NULL);
647 		vim->vim_num_entries = size / sizeof (*vim->vim_entries);
648 		offset = index * sizeof (*vim->vim_entries);
649 	}
650 
651 	/* We have data in vim_entries */
652 	if (offset == 0) {
653 		if (index >= vim->vim_entry_offset &&
654 		    index <= vim->vim_entry_offset + vim->vim_num_entries) {
655 			index -= vim->vim_entry_offset;
656 			return (&vim->vim_entries[index]);
657 		}
658 		offset = index * sizeof (*vim->vim_entries);
659 	}
660 
661 	vim->vim_entry_offset = index;
662 	size = vim->vim_num_entries * sizeof (*vim->vim_entries);
663 	rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
664 	    size);
665 	if (rc != 0) {
666 		/* Read error, invalidate vim_entries. */
667 		free(vim->vim_entries);
668 		vim->vim_entries = NULL;
669 		return (NULL);
670 	}
671 	index -= vim->vim_entry_offset;
672 	return (&vim->vim_entries[index]);
673 }
674 
675 /*
676  * Returns the mapping entry for the given offset.
677  *
678  * It's possible that the given offset will not be in the mapping table
679  * (i.e. no mapping entries contain this offset), in which case, the
680  * return value value depends on the "next_if_missing" parameter.
681  *
682  * If the offset is not found in the table and "next_if_missing" is
683  * B_FALSE, then NULL will always be returned. The behavior is intended
684  * to allow consumers to get the entry corresponding to the offset
685  * parameter, iff the offset overlaps with an entry in the table.
686  *
687  * If the offset is not found in the table and "next_if_missing" is
688  * B_TRUE, then the entry nearest to the given offset will be returned,
689  * such that the entry's source offset is greater than the offset
690  * passed in (i.e. the "next" mapping entry in the table is returned, if
691  * the offset is missing from the table). If there are no entries whose
692  * source offset is greater than the passed in offset, NULL is returned.
693  */
694 static vdev_indirect_mapping_entry_phys_t *
695 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
696     uint64_t offset)
697 {
698 	ASSERT(vim->vim_phys->vimp_num_entries > 0);
699 
700 	vdev_indirect_mapping_entry_phys_t *entry;
701 
702 	uint64_t last = vim->vim_phys->vimp_num_entries - 1;
703 	uint64_t base = 0;
704 
705 	/*
706 	 * We don't define these inside of the while loop because we use
707 	 * their value in the case that offset isn't in the mapping.
708 	 */
709 	uint64_t mid;
710 	int result;
711 
712 	while (last >= base) {
713 		mid = base + ((last - base) >> 1);
714 
715 		entry = vdev_indirect_mapping_entry(vim, mid);
716 		if (entry == NULL)
717 			break;
718 		result = dva_mapping_overlap_compare(&offset, entry);
719 
720 		if (result == 0) {
721 			break;
722 		} else if (result < 0) {
723 			last = mid - 1;
724 		} else {
725 			base = mid + 1;
726 		}
727 	}
728 	return (entry);
729 }
730 
731 /*
732  * Given an indirect vdev and an extent on that vdev, it duplicates the
733  * physical entries of the indirect mapping that correspond to the extent
734  * to a new array and returns a pointer to it. In addition, copied_entries
735  * is populated with the number of mapping entries that were duplicated.
736  *
737  * Finally, since we are doing an allocation, it is up to the caller to
738  * free the array allocated in this function.
739  */
740 vdev_indirect_mapping_entry_phys_t *
741 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
742     uint64_t asize, uint64_t *copied_entries)
743 {
744 	vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
745 	vdev_indirect_mapping_t *vim = vd->v_mapping;
746 	uint64_t entries = 0;
747 
748 	vdev_indirect_mapping_entry_phys_t *first_mapping =
749 	    vdev_indirect_mapping_entry_for_offset(vim, offset);
750 	ASSERT3P(first_mapping, !=, NULL);
751 
752 	vdev_indirect_mapping_entry_phys_t *m = first_mapping;
753 	while (asize > 0) {
754 		uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
755 		uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
756 		uint64_t inner_size = MIN(asize, size - inner_offset);
757 
758 		offset += inner_size;
759 		asize -= inner_size;
760 		entries++;
761 		m++;
762 	}
763 
764 	size_t copy_length = entries * sizeof (*first_mapping);
765 	duplicate_mappings = malloc(copy_length);
766 	if (duplicate_mappings != NULL)
767 		bcopy(first_mapping, duplicate_mappings, copy_length);
768 	else
769 		entries = 0;
770 
771 	*copied_entries = entries;
772 
773 	return (duplicate_mappings);
774 }
775 
776 static vdev_t *
777 vdev_lookup_top(spa_t *spa, uint64_t vdev)
778 {
779 	vdev_t *rvd;
780 	vdev_list_t *vlist;
781 
782 	vlist = &spa->spa_root_vdev->v_children;
783 	STAILQ_FOREACH(rvd, vlist, v_childlink)
784 		if (rvd->v_id == vdev)
785 			break;
786 
787 	return (rvd);
788 }
789 
790 /*
791  * This is a callback for vdev_indirect_remap() which allocates an
792  * indirect_split_t for each split segment and adds it to iv_splits.
793  */
794 static void
795 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
796     uint64_t size, void *arg)
797 {
798 	int n = 1;
799 	zio_t *zio = arg;
800 	indirect_vsd_t *iv = zio->io_vsd;
801 
802 	if (vd->v_read == vdev_indirect_read)
803 		return;
804 
805 	if (vd->v_read == vdev_mirror_read)
806 		n = vd->v_nchildren;
807 
808 	indirect_split_t *is =
809 	    malloc(offsetof(indirect_split_t, is_child[n]));
810 	if (is == NULL) {
811 		zio->io_error = ENOMEM;
812 		return;
813 	}
814 	bzero(is, offsetof(indirect_split_t, is_child[n]));
815 
816 	is->is_children = n;
817 	is->is_size = size;
818 	is->is_split_offset = split_offset;
819 	is->is_target_offset = offset;
820 	is->is_vdev = vd;
821 
822 	/*
823 	 * Note that we only consider multiple copies of the data for
824 	 * *mirror* vdevs.  We don't for "replacing" or "spare" vdevs, even
825 	 * though they use the same ops as mirror, because there's only one
826 	 * "good" copy under the replacing/spare.
827 	 */
828 	if (vd->v_read == vdev_mirror_read) {
829 		int i = 0;
830 		vdev_t *kid;
831 
832 		STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
833 			is->is_child[i++].ic_vdev = kid;
834 		}
835 	} else {
836 		is->is_child[0].ic_vdev = vd;
837 	}
838 
839 	list_insert_tail(&iv->iv_splits, is);
840 }
841 
842 static void
843 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
844 {
845 	list_t stack;
846 	spa_t *spa = vd->v_spa;
847 	zio_t *zio = arg;
848 	remap_segment_t *rs;
849 
850 	list_create(&stack, sizeof (remap_segment_t),
851 	    offsetof(remap_segment_t, rs_node));
852 
853 	rs = rs_alloc(vd, offset, asize, 0);
854 	if (rs == NULL) {
855 		printf("vdev_indirect_remap: out of memory.\n");
856 		zio->io_error = ENOMEM;
857 	}
858 	for (; rs != NULL; rs = list_remove_head(&stack)) {
859 		vdev_t *v = rs->rs_vd;
860 		uint64_t num_entries = 0;
861 		/* vdev_indirect_mapping_t *vim = v->v_mapping; */
862 		vdev_indirect_mapping_entry_phys_t *mapping =
863 		    vdev_indirect_mapping_duplicate_adjacent_entries(v,
864 		    rs->rs_offset, rs->rs_asize, &num_entries);
865 
866 		if (num_entries == 0)
867 			zio->io_error = ENOMEM;
868 
869 		for (uint64_t i = 0; i < num_entries; i++) {
870 			vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
871 			uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
872 			uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
873 			uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
874 			uint64_t inner_offset = rs->rs_offset -
875 			    DVA_MAPPING_GET_SRC_OFFSET(m);
876 			uint64_t inner_size =
877 			    MIN(rs->rs_asize, size - inner_offset);
878 			vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
879 
880 			if (dst_v->v_read == vdev_indirect_read) {
881 				remap_segment_t *o;
882 
883 				o = rs_alloc(dst_v, dst_offset + inner_offset,
884 				    inner_size, rs->rs_split_offset);
885 				if (o == NULL) {
886 					printf("vdev_indirect_remap: "
887 					    "out of memory.\n");
888 					zio->io_error = ENOMEM;
889 					break;
890 				}
891 
892 				list_insert_head(&stack, o);
893 			}
894 			vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
895 			    dst_offset + inner_offset,
896 			    inner_size, arg);
897 
898 			/*
899 			 * vdev_indirect_gather_splits can have memory
900 			 * allocation error, we can not recover from it.
901 			 */
902 			if (zio->io_error != 0)
903 				break;
904 			rs->rs_offset += inner_size;
905 			rs->rs_asize -= inner_size;
906 			rs->rs_split_offset += inner_size;
907 		}
908 
909 		free(mapping);
910 		free(rs);
911 		if (zio->io_error != 0)
912 			break;
913 	}
914 
915 	list_destroy(&stack);
916 }
917 
918 static void
919 vdev_indirect_map_free(zio_t *zio)
920 {
921 	indirect_vsd_t *iv = zio->io_vsd;
922 	indirect_split_t *is;
923 
924 	while ((is = list_head(&iv->iv_splits)) != NULL) {
925 		for (int c = 0; c < is->is_children; c++) {
926 			indirect_child_t *ic = &is->is_child[c];
927 			free(ic->ic_data);
928 		}
929 		list_remove(&iv->iv_splits, is);
930 		free(is);
931 	}
932 	free(iv);
933 }
934 
935 static int
936 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
937     off_t offset, size_t bytes)
938 {
939 	zio_t zio;
940 	spa_t *spa = vdev->v_spa;
941 	indirect_vsd_t *iv;
942 	indirect_split_t *first;
943 	int rc = EIO;
944 
945 	iv = calloc(1, sizeof(*iv));
946 	if (iv == NULL)
947 		return (ENOMEM);
948 
949 	list_create(&iv->iv_splits,
950 	    sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
951 
952 	bzero(&zio, sizeof(zio));
953 	zio.io_spa = spa;
954 	zio.io_bp = (blkptr_t *)bp;
955 	zio.io_data = buf;
956 	zio.io_size = bytes;
957 	zio.io_offset = offset;
958 	zio.io_vd = vdev;
959 	zio.io_vsd = iv;
960 
961 	if (vdev->v_mapping == NULL) {
962 		vdev_indirect_config_t *vic;
963 
964 		vic = &vdev->vdev_indirect_config;
965 		vdev->v_mapping = vdev_indirect_mapping_open(spa,
966 		    &spa->spa_mos, vic->vic_mapping_object);
967 	}
968 
969 	vdev_indirect_remap(vdev, offset, bytes, &zio);
970 	if (zio.io_error != 0)
971 		return (zio.io_error);
972 
973 	first = list_head(&iv->iv_splits);
974 	if (first->is_size == zio.io_size) {
975 		/*
976 		 * This is not a split block; we are pointing to the entire
977 		 * data, which will checksum the same as the original data.
978 		 * Pass the BP down so that the child i/o can verify the
979 		 * checksum, and try a different location if available
980 		 * (e.g. on a mirror).
981 		 *
982 		 * While this special case could be handled the same as the
983 		 * general (split block) case, doing it this way ensures
984 		 * that the vast majority of blocks on indirect vdevs
985 		 * (which are not split) are handled identically to blocks
986 		 * on non-indirect vdevs.  This allows us to be less strict
987 		 * about performance in the general (but rare) case.
988 		 */
989 		rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
990 		    zio.io_data, first->is_target_offset, bytes);
991 	} else {
992 		iv->iv_split_block = B_TRUE;
993 		/*
994 		 * Read one copy of each split segment, from the
995 		 * top-level vdev.  Since we don't know the
996 		 * checksum of each split individually, the child
997 		 * zio can't ensure that we get the right data.
998 		 * E.g. if it's a mirror, it will just read from a
999 		 * random (healthy) leaf vdev.  We have to verify
1000 		 * the checksum in vdev_indirect_io_done().
1001 		 */
1002 		for (indirect_split_t *is = list_head(&iv->iv_splits);
1003 		    is != NULL; is = list_next(&iv->iv_splits, is)) {
1004 			char *ptr = zio.io_data;
1005 
1006 			rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
1007 			    ptr + is->is_split_offset, is->is_target_offset,
1008 			    is->is_size);
1009 		}
1010 		if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
1011 			rc = ECKSUM;
1012 		else
1013 			rc = 0;
1014 	}
1015 
1016 	vdev_indirect_map_free(&zio);
1017 	if (rc == 0)
1018 		rc = zio.io_error;
1019 
1020 	return (rc);
1021 }
1022 
1023 static int
1024 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1025     off_t offset, size_t bytes)
1026 {
1027 
1028 	return (vdev_read_phys(vdev, bp, buf,
1029 	    offset + VDEV_LABEL_START_SIZE, bytes));
1030 }
1031 
1032 
1033 static int
1034 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1035     off_t offset, size_t bytes)
1036 {
1037 	vdev_t *kid;
1038 	int rc;
1039 
1040 	rc = EIO;
1041 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1042 		if (kid->v_state != VDEV_STATE_HEALTHY)
1043 			continue;
1044 		rc = kid->v_read(kid, bp, buf, offset, bytes);
1045 		if (!rc)
1046 			return (0);
1047 	}
1048 
1049 	return (rc);
1050 }
1051 
1052 static int
1053 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1054     off_t offset, size_t bytes)
1055 {
1056 	vdev_t *kid;
1057 
1058 	/*
1059 	 * Here we should have two kids:
1060 	 * First one which is the one we are replacing and we can trust
1061 	 * only this one to have valid data, but it might not be present.
1062 	 * Second one is that one we are replacing with. It is most likely
1063 	 * healthy, but we can't trust it has needed data, so we won't use it.
1064 	 */
1065 	kid = STAILQ_FIRST(&vdev->v_children);
1066 	if (kid == NULL)
1067 		return (EIO);
1068 	if (kid->v_state != VDEV_STATE_HEALTHY)
1069 		return (EIO);
1070 	return (kid->v_read(kid, bp, buf, offset, bytes));
1071 }
1072 
1073 static vdev_t *
1074 vdev_find(uint64_t guid)
1075 {
1076 	vdev_t *vdev;
1077 
1078 	STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
1079 		if (vdev->v_guid == guid)
1080 			return (vdev);
1081 
1082 	return (0);
1083 }
1084 
1085 static vdev_t *
1086 vdev_create(uint64_t guid, vdev_read_t *_read)
1087 {
1088 	vdev_t *vdev;
1089 	vdev_indirect_config_t *vic;
1090 
1091 	vdev = calloc(1, sizeof(vdev_t));
1092 	if (vdev != NULL) {
1093 		STAILQ_INIT(&vdev->v_children);
1094 		vdev->v_guid = guid;
1095 		vdev->v_read = _read;
1096 
1097 		/*
1098 		 * root vdev has no read function, we use this fact to
1099 		 * skip setting up data we do not need for root vdev.
1100 		 * We only point root vdev from spa.
1101 		 */
1102 		if (_read != NULL) {
1103 			vic = &vdev->vdev_indirect_config;
1104 			vic->vic_prev_indirect_vdev = UINT64_MAX;
1105 			STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
1106 		}
1107 	}
1108 
1109 	return (vdev);
1110 }
1111 
1112 static void
1113 vdev_set_initial_state(vdev_t *vdev, const unsigned char *nvlist)
1114 {
1115 	uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
1116 	uint64_t is_log;
1117 
1118 	is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
1119 	is_log = 0;
1120 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
1121 	    &is_offline);
1122 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
1123 	    &is_removed);
1124 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
1125 	    &is_faulted);
1126 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
1127 	    NULL, &is_degraded);
1128 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
1129 	    NULL, &isnt_present);
1130 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
1131 	    &is_log);
1132 
1133 	if (is_offline != 0)
1134 		vdev->v_state = VDEV_STATE_OFFLINE;
1135 	else if (is_removed != 0)
1136 		vdev->v_state = VDEV_STATE_REMOVED;
1137 	else if (is_faulted != 0)
1138 		vdev->v_state = VDEV_STATE_FAULTED;
1139 	else if (is_degraded != 0)
1140 		vdev->v_state = VDEV_STATE_DEGRADED;
1141 	else if (isnt_present != 0)
1142 		vdev->v_state = VDEV_STATE_CANT_OPEN;
1143 
1144 	vdev->v_islog = is_log != 0;
1145 }
1146 
1147 static int
1148 vdev_init(uint64_t guid, const unsigned char *nvlist, vdev_t **vdevp)
1149 {
1150 	uint64_t id, ashift, asize, nparity;
1151 	const char *path;
1152 	const char *type;
1153 	vdev_t *vdev;
1154 
1155 	if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id) ||
1156 	    nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1157 	    NULL, &type)) {
1158 		return (ENOENT);
1159 	}
1160 
1161 	if (strcmp(type, VDEV_TYPE_MIRROR) != 0 &&
1162 	    strcmp(type, VDEV_TYPE_DISK) != 0 &&
1163 #ifdef ZFS_TEST
1164 	    strcmp(type, VDEV_TYPE_FILE) != 0 &&
1165 #endif
1166 	    strcmp(type, VDEV_TYPE_RAIDZ) != 0 &&
1167 	    strcmp(type, VDEV_TYPE_INDIRECT) != 0 &&
1168 	    strcmp(type, VDEV_TYPE_REPLACING) != 0) {
1169 		printf("ZFS: can only boot from disk, mirror, raidz1, "
1170 		    "raidz2 and raidz3 vdevs\n");
1171 		return (EIO);
1172 	}
1173 
1174 	if (strcmp(type, VDEV_TYPE_MIRROR) == 0)
1175 		vdev = vdev_create(guid, vdev_mirror_read);
1176 	else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0)
1177 		vdev = vdev_create(guid, vdev_raidz_read);
1178 	else if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
1179 		vdev = vdev_create(guid, vdev_replacing_read);
1180 	else if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
1181 		vdev_indirect_config_t *vic;
1182 
1183 		vdev = vdev_create(guid, vdev_indirect_read);
1184 		if (vdev != NULL) {
1185 			vdev->v_state = VDEV_STATE_HEALTHY;
1186 			vic = &vdev->vdev_indirect_config;
1187 
1188 			nvlist_find(nvlist,
1189 			    ZPOOL_CONFIG_INDIRECT_OBJECT,
1190 			    DATA_TYPE_UINT64,
1191 			    NULL, &vic->vic_mapping_object);
1192 			nvlist_find(nvlist,
1193 			    ZPOOL_CONFIG_INDIRECT_BIRTHS,
1194 			    DATA_TYPE_UINT64,
1195 			    NULL, &vic->vic_births_object);
1196 			nvlist_find(nvlist,
1197 			    ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
1198 			    DATA_TYPE_UINT64,
1199 			    NULL, &vic->vic_prev_indirect_vdev);
1200 		}
1201 	} else {
1202 		vdev = vdev_create(guid, vdev_disk_read);
1203 	}
1204 
1205 	if (vdev == NULL)
1206 		return (ENOMEM);
1207 
1208 	vdev_set_initial_state(vdev, nvlist);
1209 	vdev->v_id = id;
1210 	if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
1211 	    DATA_TYPE_UINT64, NULL, &ashift) == 0)
1212 		vdev->v_ashift = ashift;
1213 
1214 	if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1215 	    DATA_TYPE_UINT64, NULL, &asize) == 0) {
1216 		vdev->v_psize = asize +
1217 		    VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1218 	}
1219 
1220 	if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1221 	    DATA_TYPE_UINT64, NULL, &nparity) == 0)
1222 		vdev->v_nparity = nparity;
1223 
1224 	if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1225 	    DATA_TYPE_STRING, NULL, &path) == 0) {
1226 		if (strncmp(path, "/dev/", 5) == 0)
1227 			path += 5;
1228 		vdev->v_name = strdup(path);
1229 	} else {
1230 		char *name;
1231 
1232 		name = NULL;
1233 		if (strcmp(type, "raidz") == 0) {
1234 			if (vdev->v_nparity < 1 ||
1235 			    vdev->v_nparity > 3) {
1236 				printf("ZFS: invalid raidz parity: %d\n",
1237 				    vdev->v_nparity);
1238 				return (EIO);
1239 			}
1240 			(void) asprintf(&name, "%s%d-%" PRIu64, type,
1241 			    vdev->v_nparity, id);
1242 		} else {
1243 			(void) asprintf(&name, "%s-%" PRIu64, type, id);
1244 		}
1245 		vdev->v_name = name;
1246 	}
1247 	*vdevp = vdev;
1248 	return (0);
1249 }
1250 
1251 /*
1252  * Find slot for vdev. We return either NULL to signal to use
1253  * STAILQ_INSERT_HEAD, or we return link element to be used with
1254  * STAILQ_INSERT_AFTER.
1255  */
1256 static vdev_t *
1257 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1258 {
1259 	vdev_t *v, *previous;
1260 
1261 	if (STAILQ_EMPTY(&top_vdev->v_children))
1262 		return (NULL);
1263 
1264 	previous = NULL;
1265 	STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1266 		if (v->v_id > vdev->v_id)
1267 			return (previous);
1268 
1269 		if (v->v_id == vdev->v_id)
1270 			return (v);
1271 
1272 		if (v->v_id < vdev->v_id)
1273 			previous = v;
1274 	}
1275 	return (previous);
1276 }
1277 
1278 static size_t
1279 vdev_child_count(vdev_t *vdev)
1280 {
1281 	vdev_t *v;
1282 	size_t count;
1283 
1284 	count = 0;
1285 	STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1286 		count++;
1287 	}
1288 	return (count);
1289 }
1290 
1291 /*
1292  * Insert vdev into top_vdev children list. List is ordered by v_id.
1293  */
1294 static void
1295 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1296 {
1297 	vdev_t *previous;
1298 	size_t count;
1299 
1300 	/*
1301 	 * The top level vdev can appear in random order, depending how
1302 	 * the firmware is presenting the disk devices.
1303 	 * However, we will insert vdev to create list ordered by v_id,
1304 	 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1305 	 * as STAILQ does not have insert before.
1306 	 */
1307 	previous = vdev_find_previous(top_vdev, vdev);
1308 
1309 	if (previous == NULL) {
1310 		STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1311 	} else if (previous->v_id == vdev->v_id) {
1312 		/*
1313 		 * This vdev was configured from label config,
1314 		 * do not insert duplicate.
1315 		 */
1316 		return;
1317 	} else {
1318 		STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1319 		    v_childlink);
1320 	}
1321 
1322 	count = vdev_child_count(top_vdev);
1323 	if (top_vdev->v_nchildren < count)
1324 		top_vdev->v_nchildren = count;
1325 }
1326 
1327 static int
1328 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const unsigned char *nvlist)
1329 {
1330 	vdev_t *top_vdev, *vdev;
1331 	const unsigned char *kids;
1332 	int rc, nkids;
1333 
1334 	/* Get top vdev. */
1335 	top_vdev = vdev_find(top_guid);
1336 	if (top_vdev == NULL) {
1337 		rc = vdev_init(top_guid, nvlist, &top_vdev);
1338 		if (rc != 0)
1339 			return (rc);
1340 		top_vdev->v_spa = spa;
1341 		top_vdev->v_top = top_vdev;
1342 		vdev_insert(spa->spa_root_vdev, top_vdev);
1343 	}
1344 
1345 	/* Add children if there are any. */
1346 	rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1347 	    &nkids, &kids);
1348 	if (rc == 0) {
1349 		for (int i = 0; i < nkids; i++) {
1350 			uint64_t guid;
1351 
1352 			rc = nvlist_find(kids, ZPOOL_CONFIG_GUID,
1353 			    DATA_TYPE_UINT64, NULL, &guid);
1354 			if (rc != 0)
1355 				return (rc);
1356 			rc = vdev_init(guid, kids, &vdev);
1357 			if (rc != 0)
1358 				return (rc);
1359 
1360 			vdev->v_spa = spa;
1361 			vdev->v_top = top_vdev;
1362 			vdev_insert(top_vdev, vdev);
1363 
1364 			kids = nvlist_next(kids);
1365 		}
1366 	} else {
1367 		/*
1368 		 * When there are no children, nvlist_find() does return
1369 		 * error, reset it because leaf devices have no children.
1370 		 */
1371 		rc = 0;
1372 	}
1373 
1374 	return (rc);
1375 }
1376 
1377 static int
1378 vdev_init_from_label(spa_t *spa, const unsigned char *nvlist)
1379 {
1380 	uint64_t pool_guid, top_guid;
1381 	const unsigned char *vdevs;
1382 
1383 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1384 	    NULL, &pool_guid) ||
1385 	    nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1386 	    NULL, &top_guid) ||
1387 	    nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1388 	    NULL, &vdevs)) {
1389 		printf("ZFS: can't find vdev details\n");
1390 		return (ENOENT);
1391 	}
1392 
1393 	return (vdev_from_nvlist(spa, top_guid, vdevs));
1394 }
1395 
1396 static void
1397 vdev_set_state(vdev_t *vdev)
1398 {
1399 	vdev_t *kid;
1400 	int good_kids;
1401 	int bad_kids;
1402 
1403 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1404 		vdev_set_state(kid);
1405 	}
1406 
1407 	/*
1408 	 * A mirror or raidz is healthy if all its kids are healthy. A
1409 	 * mirror is degraded if any of its kids is healthy; a raidz
1410 	 * is degraded if at most nparity kids are offline.
1411 	 */
1412 	if (STAILQ_FIRST(&vdev->v_children)) {
1413 		good_kids = 0;
1414 		bad_kids = 0;
1415 		STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1416 			if (kid->v_state == VDEV_STATE_HEALTHY)
1417 				good_kids++;
1418 			else
1419 				bad_kids++;
1420 		}
1421 		if (bad_kids == 0) {
1422 			vdev->v_state = VDEV_STATE_HEALTHY;
1423 		} else {
1424 			if (vdev->v_read == vdev_mirror_read) {
1425 				if (good_kids) {
1426 					vdev->v_state = VDEV_STATE_DEGRADED;
1427 				} else {
1428 					vdev->v_state = VDEV_STATE_OFFLINE;
1429 				}
1430 			} else if (vdev->v_read == vdev_raidz_read) {
1431 				if (bad_kids > vdev->v_nparity) {
1432 					vdev->v_state = VDEV_STATE_OFFLINE;
1433 				} else {
1434 					vdev->v_state = VDEV_STATE_DEGRADED;
1435 				}
1436 			}
1437 		}
1438 	}
1439 }
1440 
1441 static int
1442 vdev_update_from_nvlist(uint64_t top_guid, const unsigned char *nvlist)
1443 {
1444 	vdev_t *vdev;
1445 	const unsigned char *kids;
1446 	int rc, nkids;
1447 
1448 	/* Update top vdev. */
1449 	vdev = vdev_find(top_guid);
1450 	if (vdev != NULL)
1451 		vdev_set_initial_state(vdev, nvlist);
1452 
1453 	/* Update children if there are any. */
1454 	rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1455 	    &nkids, &kids);
1456 	if (rc == 0) {
1457 		for (int i = 0; i < nkids; i++) {
1458 			uint64_t guid;
1459 
1460 			rc = nvlist_find(kids, ZPOOL_CONFIG_GUID,
1461 			    DATA_TYPE_UINT64, NULL, &guid);
1462 			if (rc != 0)
1463 				break;
1464 
1465 			vdev = vdev_find(guid);
1466 			if (vdev != NULL)
1467 				vdev_set_initial_state(vdev, kids);
1468 
1469 			kids = nvlist_next(kids);
1470 		}
1471 	} else {
1472 		rc = 0;
1473 	}
1474 
1475 	return (rc);
1476 }
1477 
1478 static int
1479 vdev_init_from_nvlist(spa_t *spa, const unsigned char *nvlist)
1480 {
1481 	uint64_t pool_guid, vdev_children;
1482 	const unsigned char *vdevs, *kids;
1483 	int rc, nkids;
1484 
1485 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1486 	    NULL, &pool_guid) ||
1487 	    nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1488 	    NULL, &vdev_children) ||
1489 	    nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1490 	    NULL, &vdevs)) {
1491 		printf("ZFS: can't find vdev details\n");
1492 		return (ENOENT);
1493 	}
1494 
1495 	/* Wrong guid?! */
1496 	if (spa->spa_guid != pool_guid)
1497 		return (EINVAL);
1498 
1499 	spa->spa_root_vdev->v_nchildren = vdev_children;
1500 
1501 	rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1502 	    &nkids, &kids);
1503 
1504 	/*
1505 	 * MOS config has at least one child for root vdev.
1506 	 */
1507 	if (rc != 0)
1508 		return (rc);
1509 
1510 	for (int i = 0; i < nkids; i++) {
1511 		uint64_t guid;
1512 		vdev_t *vdev;
1513 
1514 		rc = nvlist_find(kids, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1515 		    NULL, &guid);
1516 		if (rc != 0)
1517 			break;
1518 		vdev = vdev_find(guid);
1519 		/*
1520 		 * Top level vdev is missing, create it.
1521 		 */
1522 		if (vdev == NULL)
1523 			rc = vdev_from_nvlist(spa, guid, kids);
1524 		else
1525 			rc = vdev_update_from_nvlist(guid, kids);
1526 		if (rc != 0)
1527 			break;
1528 		kids = nvlist_next(kids);
1529 	}
1530 
1531 	/*
1532 	 * Re-evaluate top-level vdev state.
1533 	 */
1534 	vdev_set_state(spa->spa_root_vdev);
1535 
1536 	return (rc);
1537 }
1538 
1539 static spa_t *
1540 spa_find_by_guid(uint64_t guid)
1541 {
1542 	spa_t *spa;
1543 
1544 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1545 		if (spa->spa_guid == guid)
1546 			return (spa);
1547 
1548 	return (NULL);
1549 }
1550 
1551 static spa_t *
1552 spa_find_by_name(const char *name)
1553 {
1554 	spa_t *spa;
1555 
1556 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1557 		if (strcmp(spa->spa_name, name) == 0)
1558 			return (spa);
1559 
1560 	return (NULL);
1561 }
1562 
1563 #ifdef BOOT2
1564 static spa_t *
1565 spa_get_primary(void)
1566 {
1567 
1568 	return (STAILQ_FIRST(&zfs_pools));
1569 }
1570 
1571 static vdev_t *
1572 spa_get_primary_vdev(const spa_t *spa)
1573 {
1574 	vdev_t *vdev;
1575 	vdev_t *kid;
1576 
1577 	if (spa == NULL)
1578 		spa = spa_get_primary();
1579 	if (spa == NULL)
1580 		return (NULL);
1581 	vdev = spa->spa_root_vdev;
1582 	if (vdev == NULL)
1583 		return (NULL);
1584 	for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
1585 	    kid = STAILQ_FIRST(&vdev->v_children))
1586 		vdev = kid;
1587 	return (vdev);
1588 }
1589 #endif
1590 
1591 static spa_t *
1592 spa_create(uint64_t guid, const char *name)
1593 {
1594 	spa_t *spa;
1595 
1596 	if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1597 		return (NULL);
1598 	if ((spa->spa_name = strdup(name)) == NULL) {
1599 		free(spa);
1600 		return (NULL);
1601 	}
1602 	spa->spa_guid = guid;
1603 	spa->spa_root_vdev = vdev_create(guid, NULL);
1604 	if (spa->spa_root_vdev == NULL) {
1605 		free(spa->spa_name);
1606 		free(spa);
1607 		return (NULL);
1608 	}
1609 	spa->spa_root_vdev->v_name = strdup("root");
1610 	STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1611 
1612 	return (spa);
1613 }
1614 
1615 static const char *
1616 state_name(vdev_state_t state)
1617 {
1618 	static const char *names[] = {
1619 		"UNKNOWN",
1620 		"CLOSED",
1621 		"OFFLINE",
1622 		"REMOVED",
1623 		"CANT_OPEN",
1624 		"FAULTED",
1625 		"DEGRADED",
1626 		"ONLINE"
1627 	};
1628 	return (names[state]);
1629 }
1630 
1631 #ifdef BOOT2
1632 
1633 #define pager_printf printf
1634 
1635 #else
1636 
1637 static int
1638 pager_printf(const char *fmt, ...)
1639 {
1640 	char line[80];
1641 	va_list args;
1642 
1643 	va_start(args, fmt);
1644 	vsnprintf(line, sizeof(line), fmt, args);
1645 	va_end(args);
1646 	return (pager_output(line));
1647 }
1648 
1649 #endif
1650 
1651 #define	STATUS_FORMAT	"        %s %s\n"
1652 
1653 static int
1654 print_state(int indent, const char *name, vdev_state_t state)
1655 {
1656 	int i;
1657 	char buf[512];
1658 
1659 	buf[0] = 0;
1660 	for (i = 0; i < indent; i++)
1661 		strcat(buf, "  ");
1662 	strcat(buf, name);
1663 	return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1664 }
1665 
1666 static int
1667 vdev_status(vdev_t *vdev, int indent)
1668 {
1669 	vdev_t *kid;
1670 	int ret;
1671 
1672 	if (vdev->v_islog) {
1673 		(void) pager_output("        logs\n");
1674 		indent++;
1675 	}
1676 
1677 	ret = print_state(indent, vdev->v_name, vdev->v_state);
1678 	if (ret != 0)
1679 		return (ret);
1680 
1681 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1682 		ret = vdev_status(kid, indent + 1);
1683 		if (ret != 0)
1684 			return (ret);
1685 	}
1686 	return (ret);
1687 }
1688 
1689 static int
1690 spa_status(spa_t *spa)
1691 {
1692 	static char bootfs[ZFS_MAXNAMELEN];
1693 	uint64_t rootid;
1694 	vdev_list_t *vlist;
1695 	vdev_t *vdev;
1696 	int good_kids, bad_kids, degraded_kids, ret;
1697 	vdev_state_t state;
1698 
1699 	ret = pager_printf("  pool: %s\n", spa->spa_name);
1700 	if (ret != 0)
1701 		return (ret);
1702 
1703 	if (zfs_get_root(spa, &rootid) == 0 &&
1704 	    zfs_rlookup(spa, rootid, bootfs) == 0) {
1705 		if (bootfs[0] == '\0')
1706 			ret = pager_printf("bootfs: %s\n", spa->spa_name);
1707 		else
1708 			ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1709 			    bootfs);
1710 		if (ret != 0)
1711 			return (ret);
1712 	}
1713 	ret = pager_printf("config:\n\n");
1714 	if (ret != 0)
1715 		return (ret);
1716 	ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1717 	if (ret != 0)
1718 		return (ret);
1719 
1720 	good_kids = 0;
1721 	degraded_kids = 0;
1722 	bad_kids = 0;
1723 	vlist = &spa->spa_root_vdev->v_children;
1724 	STAILQ_FOREACH(vdev, vlist, v_childlink) {
1725 		if (vdev->v_state == VDEV_STATE_HEALTHY)
1726 			good_kids++;
1727 		else if (vdev->v_state == VDEV_STATE_DEGRADED)
1728 			degraded_kids++;
1729 		else
1730 			bad_kids++;
1731 	}
1732 
1733 	state = VDEV_STATE_CLOSED;
1734 	if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1735 		state = VDEV_STATE_HEALTHY;
1736 	else if ((good_kids + degraded_kids) > 0)
1737 		state = VDEV_STATE_DEGRADED;
1738 
1739 	ret = print_state(0, spa->spa_name, state);
1740 	if (ret != 0)
1741 		return (ret);
1742 
1743 	STAILQ_FOREACH(vdev, vlist, v_childlink) {
1744 		ret = vdev_status(vdev, 1);
1745 		if (ret != 0)
1746 			return (ret);
1747 	}
1748 	return (ret);
1749 }
1750 
1751 static int
1752 spa_all_status(void)
1753 {
1754 	spa_t *spa;
1755 	int first = 1, ret = 0;
1756 
1757 	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1758 		if (!first) {
1759 			ret = pager_printf("\n");
1760 			if (ret != 0)
1761 				return (ret);
1762 		}
1763 		first = 0;
1764 		ret = spa_status(spa);
1765 		if (ret != 0)
1766 			return (ret);
1767 	}
1768 	return (ret);
1769 }
1770 
1771 static uint64_t
1772 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1773 {
1774 	uint64_t label_offset;
1775 
1776 	if (l < VDEV_LABELS / 2)
1777 		label_offset = 0;
1778 	else
1779 		label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1780 
1781 	return (offset + l * sizeof (vdev_label_t) + label_offset);
1782 }
1783 
1784 static int
1785 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1786 {
1787 	unsigned int seq1 = 0;
1788 	unsigned int seq2 = 0;
1789 	int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1790 
1791 	if (cmp != 0)
1792 		return (cmp);
1793 
1794 	cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1795 	if (cmp != 0)
1796 		return (cmp);
1797 
1798 	if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1799 		seq1 = MMP_SEQ(ub1);
1800 
1801 	if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1802 		seq2 = MMP_SEQ(ub2);
1803 
1804 	return (AVL_CMP(seq1, seq2));
1805 }
1806 
1807 static int
1808 uberblock_verify(uberblock_t *ub)
1809 {
1810 	if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1811 		byteswap_uint64_array(ub, sizeof (uberblock_t));
1812 	}
1813 
1814 	if (ub->ub_magic != UBERBLOCK_MAGIC ||
1815 	    !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1816 		return (EINVAL);
1817 
1818 	return (0);
1819 }
1820 
1821 static int
1822 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1823     size_t size)
1824 {
1825 	blkptr_t bp;
1826 	off_t off;
1827 
1828 	off = vdev_label_offset(vd->v_psize, l, offset);
1829 
1830 	BP_ZERO(&bp);
1831 	BP_SET_LSIZE(&bp, size);
1832 	BP_SET_PSIZE(&bp, size);
1833 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1834 	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1835 	DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1836 	ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1837 
1838 	return (vdev_read_phys(vd, &bp, buf, off, size));
1839 }
1840 
1841 static unsigned char *
1842 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1843 {
1844 	vdev_phys_t *label;
1845 	uint64_t best_txg = 0;
1846 	uint64_t label_txg = 0;
1847 	uint64_t asize;
1848 	unsigned char *nvl;
1849 	size_t nvl_size;
1850 	int error;
1851 
1852 	label = malloc(sizeof (vdev_phys_t));
1853 	if (label == NULL)
1854 		return (NULL);
1855 
1856 	nvl_size = VDEV_PHYS_SIZE - sizeof (zio_eck_t) - 4;
1857 	nvl = malloc(nvl_size);
1858 	if (nvl == NULL)
1859 		goto done;
1860 
1861 	for (int l = 0; l < VDEV_LABELS; l++) {
1862 		const unsigned char *nvlist;
1863 
1864 		if (vdev_label_read(vd, l, label,
1865 		    offsetof(vdev_label_t, vl_vdev_phys),
1866 		    sizeof (vdev_phys_t)))
1867 			continue;
1868 
1869 		if (label->vp_nvlist[0] != NV_ENCODE_XDR)
1870 			continue;
1871 
1872 		nvlist = (const unsigned char *) label->vp_nvlist + 4;
1873 		error = nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
1874 		    DATA_TYPE_UINT64, NULL, &label_txg);
1875 		if (error != 0 || label_txg == 0) {
1876 			memcpy(nvl, nvlist, nvl_size);
1877 			goto done;
1878 		}
1879 
1880 		if (label_txg <= txg && label_txg > best_txg) {
1881 			best_txg = label_txg;
1882 			memcpy(nvl, nvlist, nvl_size);
1883 
1884 			/*
1885 			 * Use asize from pool config. We need this
1886 			 * because we can get bad value from BIOS.
1887 			 */
1888 			if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1889 			    DATA_TYPE_UINT64, NULL, &asize) == 0) {
1890 				vd->v_psize = asize +
1891 				    VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1892 			}
1893 		}
1894 	}
1895 
1896 	if (best_txg == 0) {
1897 		free(nvl);
1898 		nvl = NULL;
1899 	}
1900 done:
1901 	free(label);
1902 	return (nvl);
1903 }
1904 
1905 static void
1906 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1907 {
1908 	uberblock_t *buf;
1909 
1910 	buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1911 	if (buf == NULL)
1912 		return;
1913 
1914 	for (int l = 0; l < VDEV_LABELS; l++) {
1915 		for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1916 			if (vdev_label_read(vd, l, buf,
1917 			    VDEV_UBERBLOCK_OFFSET(vd, n),
1918 			    VDEV_UBERBLOCK_SIZE(vd)))
1919 				continue;
1920 			if (uberblock_verify(buf) != 0)
1921 				continue;
1922 
1923 			if (vdev_uberblock_compare(buf, ub) > 0)
1924 				*ub = *buf;
1925 		}
1926 	}
1927 	free(buf);
1928 }
1929 
1930 static int
1931 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
1932 {
1933 	vdev_t vtmp;
1934 	spa_t *spa;
1935 	vdev_t *vdev;
1936 	unsigned char *nvlist;
1937 	uint64_t val;
1938 	uint64_t guid, vdev_children;
1939 	uint64_t pool_txg, pool_guid;
1940 	const char *pool_name;
1941 	const unsigned char *features;
1942 	int rc;
1943 
1944 	/*
1945 	 * Load the vdev label and figure out which
1946 	 * uberblock is most current.
1947 	 */
1948 	memset(&vtmp, 0, sizeof(vtmp));
1949 	vtmp.v_phys_read = _read;
1950 	vtmp.v_read_priv = read_priv;
1951 	vtmp.v_psize = P2ALIGN(ldi_get_size(read_priv),
1952 	    (uint64_t)sizeof (vdev_label_t));
1953 
1954 	/* Test for minimum device size. */
1955 	if (vtmp.v_psize < SPA_MINDEVSIZE)
1956 		return (EIO);
1957 
1958 	nvlist = vdev_label_read_config(&vtmp, UINT64_MAX);
1959 	if (nvlist == NULL)
1960 		return (EIO);
1961 
1962 	if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1963 	    NULL, &val) != 0) {
1964 		free(nvlist);
1965 		return (EIO);
1966 	}
1967 
1968 	if (!SPA_VERSION_IS_SUPPORTED(val)) {
1969 		printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1970 		    (unsigned)val, (unsigned)SPA_VERSION);
1971 		free(nvlist);
1972 		return (EIO);
1973 	}
1974 
1975 	/* Check ZFS features for read */
1976 	if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1977 	    DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1978 	    nvlist_check_features_for_read(features) != 0) {
1979 		free(nvlist);
1980 		return (EIO);
1981 	}
1982 
1983 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1984 	    NULL, &val) != 0) {
1985 		free(nvlist);
1986 		return (EIO);
1987 	}
1988 
1989 	if (val == POOL_STATE_DESTROYED) {
1990 		/* We don't boot only from destroyed pools. */
1991 		free(nvlist);
1992 		return (EIO);
1993 	}
1994 
1995 	if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1996 	    NULL, &pool_txg) != 0 ||
1997 	    nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1998 	    NULL, &pool_guid) != 0 ||
1999 	    nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2000 	    NULL, &pool_name) != 0) {
2001 		/*
2002 		 * Cache and spare devices end up here - just ignore
2003 		 * them.
2004 		 */
2005 		free(nvlist);
2006 		return (EIO);
2007 	}
2008 
2009 	/*
2010 	 * Create the pool if this is the first time we've seen it.
2011 	 */
2012 	spa = spa_find_by_guid(pool_guid);
2013 	if (spa == NULL) {
2014 		nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN,
2015 		    DATA_TYPE_UINT64, NULL, &vdev_children);
2016 		spa = spa_create(pool_guid, pool_name);
2017 		if (spa == NULL) {
2018 			free(nvlist);
2019 			return (ENOMEM);
2020 		}
2021 		spa->spa_root_vdev->v_nchildren = vdev_children;
2022 	}
2023 	if (pool_txg > spa->spa_txg)
2024 		spa->spa_txg = pool_txg;
2025 
2026 	/*
2027 	 * Get the vdev tree and create our in-core copy of it.
2028 	 * If we already have a vdev with this guid, this must
2029 	 * be some kind of alias (overlapping slices, dangerously dedicated
2030 	 * disks etc).
2031 	 */
2032 	if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2033 	    NULL, &guid) != 0) {
2034 		free(nvlist);
2035 		return (EIO);
2036 	}
2037 	vdev = vdev_find(guid);
2038 	/* Has this vdev already been inited? */
2039 	if (vdev && vdev->v_phys_read) {
2040 		free(nvlist);
2041 		return (EIO);
2042 	}
2043 
2044 	rc = vdev_init_from_label(spa, nvlist);
2045 	free(nvlist);
2046 	if (rc != 0)
2047 		return (rc);
2048 
2049 	/*
2050 	 * We should already have created an incomplete vdev for this
2051 	 * vdev. Find it and initialise it with our read proc.
2052 	 */
2053 	vdev = vdev_find(guid);
2054 	if (vdev != NULL) {
2055 		vdev->v_phys_read = _read;
2056 		vdev->v_read_priv = read_priv;
2057 		vdev->v_psize = vtmp.v_psize;
2058 		/*
2059 		 * If no other state is set, mark vdev healthy.
2060 		 */
2061 		if (vdev->v_state == VDEV_STATE_UNKNOWN)
2062 			vdev->v_state = VDEV_STATE_HEALTHY;
2063 	} else {
2064 		printf("ZFS: inconsistent nvlist contents\n");
2065 		return (EIO);
2066 	}
2067 
2068 	if (vdev->v_islog)
2069 		spa->spa_with_log = vdev->v_islog;
2070 
2071 	/*
2072 	 * Re-evaluate top-level vdev state.
2073 	 */
2074 	vdev_set_state(vdev->v_top);
2075 
2076 	/*
2077 	 * Ok, we are happy with the pool so far. Lets find
2078 	 * the best uberblock and then we can actually access
2079 	 * the contents of the pool.
2080 	 */
2081 	vdev_uberblock_load(vdev, &spa->spa_uberblock);
2082 
2083 	if (spap != NULL)
2084 		*spap = spa;
2085 	return (0);
2086 }
2087 
2088 static int
2089 ilog2(int n)
2090 {
2091 	int v;
2092 
2093 	for (v = 0; v < 32; v++)
2094 		if (n == (1 << v))
2095 			return (v);
2096 	return (-1);
2097 }
2098 
2099 static int
2100 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2101 {
2102 	blkptr_t gbh_bp;
2103 	zio_gbh_phys_t zio_gb;
2104 	char *pbuf;
2105 	int i;
2106 
2107 	/* Artificial BP for gang block header. */
2108 	gbh_bp = *bp;
2109 	BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2110 	BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2111 	BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2112 	BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2113 	for (i = 0; i < SPA_DVAS_PER_BP; i++)
2114 		DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2115 
2116 	/* Read gang header block using the artificial BP. */
2117 	if (zio_read(spa, &gbh_bp, &zio_gb))
2118 		return (EIO);
2119 
2120 	pbuf = buf;
2121 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2122 		blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2123 
2124 		if (BP_IS_HOLE(gbp))
2125 			continue;
2126 		if (zio_read(spa, gbp, pbuf))
2127 			return (EIO);
2128 		pbuf += BP_GET_PSIZE(gbp);
2129 	}
2130 
2131 	if (zio_checksum_verify(spa, bp, buf))
2132 		return (EIO);
2133 	return (0);
2134 }
2135 
2136 static int
2137 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2138 {
2139 	int cpfunc = BP_GET_COMPRESS(bp);
2140 	uint64_t align, size;
2141 	void *pbuf;
2142 	int i, error;
2143 
2144 	/*
2145 	 * Process data embedded in block pointer
2146 	 */
2147 	if (BP_IS_EMBEDDED(bp)) {
2148 		ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2149 
2150 		size = BPE_GET_PSIZE(bp);
2151 		ASSERT(size <= BPE_PAYLOAD_SIZE);
2152 
2153 		if (cpfunc != ZIO_COMPRESS_OFF)
2154 			pbuf = zfs_alloc(size);
2155 		else
2156 			pbuf = buf;
2157 
2158 		decode_embedded_bp_compressed(bp, pbuf);
2159 		error = 0;
2160 
2161 		if (cpfunc != ZIO_COMPRESS_OFF) {
2162 			error = zio_decompress_data(cpfunc, pbuf,
2163 			    size, buf, BP_GET_LSIZE(bp));
2164 			zfs_free(pbuf, size);
2165 		}
2166 		if (error != 0)
2167 			printf("ZFS: i/o error - unable to decompress "
2168 			    "block pointer data, error %d\n", error);
2169 		return (error);
2170 	}
2171 
2172 	error = EIO;
2173 
2174 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2175 		const dva_t *dva = &bp->blk_dva[i];
2176 		vdev_t *vdev;
2177 		vdev_list_t *vlist;
2178 		uint64_t vdevid;
2179 		off_t offset;
2180 
2181 		if (!dva->dva_word[0] && !dva->dva_word[1])
2182 			continue;
2183 
2184 		vdevid = DVA_GET_VDEV(dva);
2185 		offset = DVA_GET_OFFSET(dva);
2186 		vlist = &spa->spa_root_vdev->v_children;
2187 		STAILQ_FOREACH(vdev, vlist, v_childlink) {
2188 			if (vdev->v_id == vdevid)
2189 				break;
2190 		}
2191 		if (!vdev || !vdev->v_read)
2192 			continue;
2193 
2194 		size = BP_GET_PSIZE(bp);
2195 		if (vdev->v_read == vdev_raidz_read) {
2196 			align = 1ULL << vdev->v_ashift;
2197 			if (P2PHASE(size, align) != 0)
2198 				size = P2ROUNDUP(size, align);
2199 		}
2200 		if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2201 			pbuf = zfs_alloc(size);
2202 		else
2203 			pbuf = buf;
2204 
2205 		if (DVA_GET_GANG(dva))
2206 			error = zio_read_gang(spa, bp, pbuf);
2207 		else
2208 			error = vdev->v_read(vdev, bp, pbuf, offset, size);
2209 		if (error == 0) {
2210 			if (cpfunc != ZIO_COMPRESS_OFF)
2211 				error = zio_decompress_data(cpfunc, pbuf,
2212 				    BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2213 			else if (size != BP_GET_PSIZE(bp))
2214 				bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2215 		}
2216 		if (buf != pbuf)
2217 			zfs_free(pbuf, size);
2218 		if (error == 0)
2219 			break;
2220 	}
2221 	if (error != 0)
2222 		printf("ZFS: i/o error - all block copies unavailable\n");
2223 	return (error);
2224 }
2225 
2226 static int
2227 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2228     void *buf, size_t buflen)
2229 {
2230 	int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2231 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2232 	int nlevels = dnode->dn_nlevels;
2233 	int i, rc;
2234 
2235 	if (bsize > SPA_MAXBLOCKSIZE) {
2236 		printf("ZFS: I/O error - blocks larger than %llu are not "
2237 		    "supported\n", SPA_MAXBLOCKSIZE);
2238 		return (EIO);
2239 	}
2240 
2241 	/*
2242 	 * Note: bsize may not be a power of two here so we need to do an
2243 	 * actual divide rather than a bitshift.
2244 	 */
2245 	while (buflen > 0) {
2246 		uint64_t bn = offset / bsize;
2247 		int boff = offset % bsize;
2248 		int ibn;
2249 		const blkptr_t *indbp;
2250 		blkptr_t bp;
2251 
2252 		if (bn > dnode->dn_maxblkid)
2253 			return (EIO);
2254 
2255 		if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2256 			goto cached;
2257 
2258 		indbp = dnode->dn_blkptr;
2259 		for (i = 0; i < nlevels; i++) {
2260 			/*
2261 			 * Copy the bp from the indirect array so that
2262 			 * we can re-use the scratch buffer for multi-level
2263 			 * objects.
2264 			 */
2265 			ibn = bn >> ((nlevels - i - 1) * ibshift);
2266 			ibn &= ((1 << ibshift) - 1);
2267 			bp = indbp[ibn];
2268 			if (BP_IS_HOLE(&bp)) {
2269 				memset(dnode_cache_buf, 0, bsize);
2270 				break;
2271 			}
2272 			rc = zio_read(spa, &bp, dnode_cache_buf);
2273 			if (rc)
2274 				return (rc);
2275 			indbp = (const blkptr_t *) dnode_cache_buf;
2276 		}
2277 		dnode_cache_obj = dnode;
2278 		dnode_cache_bn = bn;
2279 	cached:
2280 
2281 		/*
2282 		 * The buffer contains our data block. Copy what we
2283 		 * need from it and loop.
2284 		 */
2285 		i = bsize - boff;
2286 		if (i > buflen) i = buflen;
2287 		memcpy(buf, &dnode_cache_buf[boff], i);
2288 		buf = ((char *)buf) + i;
2289 		offset += i;
2290 		buflen -= i;
2291 	}
2292 
2293 	return (0);
2294 }
2295 
2296 /*
2297  * Lookup a value in a microzap directory.
2298  */
2299 static int
2300 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2301     uint64_t *value)
2302 {
2303 	const mzap_ent_phys_t *mze;
2304 	int chunks, i;
2305 
2306 	/*
2307 	 * Microzap objects use exactly one block. Read the whole
2308 	 * thing.
2309 	 */
2310 	chunks = size / MZAP_ENT_LEN - 1;
2311 	for (i = 0; i < chunks; i++) {
2312 		mze = &mz->mz_chunk[i];
2313 		if (strcmp(mze->mze_name, name) == 0) {
2314 			*value = mze->mze_value;
2315 			return (0);
2316 		}
2317 	}
2318 
2319 	return (ENOENT);
2320 }
2321 
2322 /*
2323  * Compare a name with a zap leaf entry. Return non-zero if the name
2324  * matches.
2325  */
2326 static int
2327 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2328     const char *name)
2329 {
2330 	size_t namelen;
2331 	const zap_leaf_chunk_t *nc;
2332 	const char *p;
2333 
2334 	namelen = zc->l_entry.le_name_numints;
2335 
2336 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2337 	p = name;
2338 	while (namelen > 0) {
2339 		size_t len;
2340 
2341 		len = namelen;
2342 		if (len > ZAP_LEAF_ARRAY_BYTES)
2343 			len = ZAP_LEAF_ARRAY_BYTES;
2344 		if (memcmp(p, nc->l_array.la_array, len))
2345 			return (0);
2346 		p += len;
2347 		namelen -= len;
2348 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2349 	}
2350 
2351 	return (1);
2352 }
2353 
2354 /*
2355  * Extract a uint64_t value from a zap leaf entry.
2356  */
2357 static uint64_t
2358 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2359 {
2360 	const zap_leaf_chunk_t *vc;
2361 	int i;
2362 	uint64_t value;
2363 	const uint8_t *p;
2364 
2365 	vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2366 	for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2367 		value = (value << 8) | p[i];
2368 	}
2369 
2370 	return (value);
2371 }
2372 
2373 static void
2374 stv(int len, void *addr, uint64_t value)
2375 {
2376 	switch (len) {
2377 	case 1:
2378 		*(uint8_t *)addr = value;
2379 		return;
2380 	case 2:
2381 		*(uint16_t *)addr = value;
2382 		return;
2383 	case 4:
2384 		*(uint32_t *)addr = value;
2385 		return;
2386 	case 8:
2387 		*(uint64_t *)addr = value;
2388 		return;
2389 	}
2390 }
2391 
2392 /*
2393  * Extract a array from a zap leaf entry.
2394  */
2395 static void
2396 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2397     uint64_t integer_size, uint64_t num_integers, void *buf)
2398 {
2399 	uint64_t array_int_len = zc->l_entry.le_value_intlen;
2400 	uint64_t value = 0;
2401 	uint64_t *u64 = buf;
2402 	char *p = buf;
2403 	int len = MIN(zc->l_entry.le_value_numints, num_integers);
2404 	int chunk = zc->l_entry.le_value_chunk;
2405 	int byten = 0;
2406 
2407 	if (integer_size == 8 && len == 1) {
2408 		*u64 = fzap_leaf_value(zl, zc);
2409 		return;
2410 	}
2411 
2412 	while (len > 0) {
2413 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2414 		int i;
2415 
2416 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2417 		for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2418 			value = (value << 8) | la->la_array[i];
2419 			byten++;
2420 			if (byten == array_int_len) {
2421 				stv(integer_size, p, value);
2422 				byten = 0;
2423 				len--;
2424 				if (len == 0)
2425 					return;
2426 				p += integer_size;
2427 			}
2428 		}
2429 		chunk = la->la_next;
2430 	}
2431 }
2432 
2433 static int
2434 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2435 {
2436 
2437 	switch (integer_size) {
2438 	case 1:
2439 	case 2:
2440 	case 4:
2441 	case 8:
2442 		break;
2443 	default:
2444 		return (EINVAL);
2445 	}
2446 
2447 	if (integer_size * num_integers > ZAP_MAXVALUELEN)
2448 		return (E2BIG);
2449 
2450 	return (0);
2451 }
2452 
2453 static void
2454 zap_leaf_free(zap_leaf_t *leaf)
2455 {
2456 	free(leaf->l_phys);
2457 	free(leaf);
2458 }
2459 
2460 static int
2461 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2462 {
2463 	int bs = FZAP_BLOCK_SHIFT(zap);
2464 	int err;
2465 
2466 	*lp = malloc(sizeof(**lp));
2467 	if (*lp == NULL)
2468 		return (ENOMEM);
2469 
2470 	(*lp)->l_bs = bs;
2471 	(*lp)->l_phys = malloc(1 << bs);
2472 
2473 	if ((*lp)->l_phys == NULL) {
2474 		free(*lp);
2475 		return (ENOMEM);
2476 	}
2477 	err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2478 	    1 << bs);
2479 	if (err != 0) {
2480 		zap_leaf_free(*lp);
2481 	}
2482 	return (err);
2483 }
2484 
2485 static int
2486 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2487     uint64_t *valp)
2488 {
2489 	int bs = FZAP_BLOCK_SHIFT(zap);
2490 	uint64_t blk = idx >> (bs - 3);
2491 	uint64_t off = idx & ((1 << (bs - 3)) - 1);
2492 	uint64_t *buf;
2493 	int rc;
2494 
2495 	buf = malloc(1 << zap->zap_block_shift);
2496 	if (buf == NULL)
2497 		return (ENOMEM);
2498 	rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2499 	    buf, 1 << zap->zap_block_shift);
2500 	if (rc == 0)
2501 		*valp = buf[off];
2502 	free(buf);
2503 	return (rc);
2504 }
2505 
2506 static int
2507 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2508 {
2509 	if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2510 		*valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2511 		return (0);
2512 	} else {
2513 		return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2514 		    idx, valp));
2515 	}
2516 }
2517 
2518 #define	ZAP_HASH_IDX(hash, n)	(((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2519 static int
2520 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2521 {
2522 	uint64_t idx, blk;
2523 	int err;
2524 
2525 	idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2526 	err = zap_idx_to_blk(zap, idx, &blk);
2527 	if (err != 0)
2528 		return (err);
2529 	return (zap_get_leaf_byblk(zap, blk, lp));
2530 }
2531 
2532 #define	CHAIN_END	0xffff	/* end of the chunk chain */
2533 #define	LEAF_HASH(l, h) \
2534 	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2535 	((h) >> \
2536 	(64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2537 #define	LEAF_HASH_ENTPTR(l, h)	(&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2538 
2539 static int
2540 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2541     uint64_t integer_size, uint64_t num_integers, void *value)
2542 {
2543 	int rc;
2544 	uint16_t *chunkp;
2545 	struct zap_leaf_entry *le;
2546 
2547 	/*
2548 	 * Make sure this chunk matches our hash.
2549 	 */
2550 	if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2551 	    zl->l_phys->l_hdr.lh_prefix !=
2552 	    hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2553 		return (EIO);
2554 
2555 	rc = ENOENT;
2556 	for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2557 	    *chunkp != CHAIN_END; chunkp = &le->le_next) {
2558 		zap_leaf_chunk_t *zc;
2559 		uint16_t chunk = *chunkp;
2560 
2561 		le = ZAP_LEAF_ENTRY(zl, chunk);
2562 		if (le->le_hash != hash)
2563 			continue;
2564 		zc = &ZAP_LEAF_CHUNK(zl, chunk);
2565 		if (fzap_name_equal(zl, zc, name)) {
2566 			if (zc->l_entry.le_value_intlen > integer_size) {
2567 				rc = EINVAL;
2568 			} else {
2569 				fzap_leaf_array(zl, zc, integer_size,
2570 				    num_integers, value);
2571 				rc = 0;
2572 			}
2573 			break;
2574 		}
2575 	}
2576 	return (rc);
2577 }
2578 
2579 /*
2580  * Lookup a value in a fatzap directory.
2581  */
2582 static int
2583 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2584     const char *name, uint64_t integer_size, uint64_t num_integers,
2585     void *value)
2586 {
2587 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2588 	fat_zap_t z;
2589 	zap_leaf_t *zl;
2590 	uint64_t hash;
2591 	int rc;
2592 
2593 	if (zh->zap_magic != ZAP_MAGIC)
2594 		return (EIO);
2595 
2596 	if ((rc = fzap_check_size(integer_size, num_integers)) != 0)
2597 		return (rc);
2598 
2599 	z.zap_block_shift = ilog2(bsize);
2600 	z.zap_phys = zh;
2601 	z.zap_spa = spa;
2602 	z.zap_dnode = dnode;
2603 
2604 	hash = zap_hash(zh->zap_salt, name);
2605 	rc = zap_deref_leaf(&z, hash, &zl);
2606 	if (rc != 0)
2607 		return (rc);
2608 
2609 	rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2610 
2611 	zap_leaf_free(zl);
2612 	return (rc);
2613 }
2614 
2615 /*
2616  * Lookup a name in a zap object and return its value as a uint64_t.
2617  */
2618 static int
2619 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2620     uint64_t integer_size, uint64_t num_integers, void *value)
2621 {
2622 	int rc;
2623 	zap_phys_t *zap;
2624 	size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2625 
2626 	zap = malloc(size);
2627 	if (zap == NULL)
2628 		return (ENOMEM);
2629 
2630 	rc = dnode_read(spa, dnode, 0, zap, size);
2631 	if (rc)
2632 		goto done;
2633 
2634 	switch (zap->zap_block_type) {
2635 	case ZBT_MICRO:
2636 		rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2637 		break;
2638 	case ZBT_HEADER:
2639 		rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2640 		    num_integers, value);
2641 		break;
2642 	default:
2643 		printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2644 		    zap->zap_block_type);
2645 		rc = EIO;
2646 	}
2647 done:
2648 	free(zap);
2649 	return (rc);
2650 }
2651 
2652 /*
2653  * List a microzap directory.
2654  */
2655 static int
2656 mzap_list(const mzap_phys_t *mz, size_t size,
2657     int (*callback)(const char *, uint64_t))
2658 {
2659 	const mzap_ent_phys_t *mze;
2660 	int chunks, i, rc;
2661 
2662 	/*
2663 	 * Microzap objects use exactly one block. Read the whole
2664 	 * thing.
2665 	 */
2666 	rc = 0;
2667 	chunks = size / MZAP_ENT_LEN - 1;
2668 	for (i = 0; i < chunks; i++) {
2669 		mze = &mz->mz_chunk[i];
2670 		if (mze->mze_name[0]) {
2671 			rc = callback(mze->mze_name, mze->mze_value);
2672 			if (rc != 0)
2673 				break;
2674 		}
2675 	}
2676 
2677 	return (rc);
2678 }
2679 
2680 /*
2681  * List a fatzap directory.
2682  */
2683 static int
2684 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2685     int (*callback)(const char *, uint64_t))
2686 {
2687 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2688 	fat_zap_t z;
2689 	uint64_t i;
2690 	int j, rc;
2691 
2692 	if (zh->zap_magic != ZAP_MAGIC)
2693 		return (EIO);
2694 
2695 	z.zap_block_shift = ilog2(bsize);
2696 	z.zap_phys = zh;
2697 
2698 	/*
2699 	 * This assumes that the leaf blocks start at block 1. The
2700 	 * documentation isn't exactly clear on this.
2701 	 */
2702 	zap_leaf_t zl;
2703 	zl.l_bs = z.zap_block_shift;
2704 	zl.l_phys = malloc(bsize);
2705 	if (zl.l_phys == NULL)
2706 		return (ENOMEM);
2707 
2708 	for (i = 0; i < zh->zap_num_leafs; i++) {
2709 		off_t off = ((off_t)(i + 1)) << zl.l_bs;
2710 		char name[256], *p;
2711 		uint64_t value;
2712 
2713 		if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2714 			free(zl.l_phys);
2715 			return (EIO);
2716 		}
2717 
2718 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2719 			zap_leaf_chunk_t *zc, *nc;
2720 			int namelen;
2721 
2722 			zc = &ZAP_LEAF_CHUNK(&zl, j);
2723 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2724 				continue;
2725 			namelen = zc->l_entry.le_name_numints;
2726 			if (namelen > sizeof(name))
2727 				namelen = sizeof(name);
2728 
2729 			/*
2730 			 * Paste the name back together.
2731 			 */
2732 			nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2733 			p = name;
2734 			while (namelen > 0) {
2735 				int len;
2736 				len = namelen;
2737 				if (len > ZAP_LEAF_ARRAY_BYTES)
2738 					len = ZAP_LEAF_ARRAY_BYTES;
2739 				memcpy(p, nc->l_array.la_array, len);
2740 				p += len;
2741 				namelen -= len;
2742 				nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2743 			}
2744 
2745 			/*
2746 			 * Assume the first eight bytes of the value are
2747 			 * a uint64_t.
2748 			 */
2749 			value = fzap_leaf_value(&zl, zc);
2750 
2751 			/* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2752 			rc = callback((const char *)name, value);
2753 			if (rc != 0) {
2754 				free(zl.l_phys);
2755 				return (rc);
2756 			}
2757 		}
2758 	}
2759 
2760 	free(zl.l_phys);
2761 	return (0);
2762 }
2763 
2764 static int zfs_printf(const char *name, uint64_t value __unused)
2765 {
2766 
2767 	printf("%s\n", name);
2768 
2769 	return (0);
2770 }
2771 
2772 /*
2773  * List a zap directory.
2774  */
2775 static int
2776 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2777 {
2778 	zap_phys_t *zap;
2779 	size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2780 	int rc;
2781 
2782 	zap = malloc(size);
2783 	if (zap == NULL)
2784 		return (ENOMEM);
2785 
2786 	rc = dnode_read(spa, dnode, 0, zap, size);
2787 	if (rc == 0) {
2788 		if (zap->zap_block_type == ZBT_MICRO)
2789 			rc = mzap_list((const mzap_phys_t *)zap, size,
2790 			    zfs_printf);
2791 		else
2792 			rc = fzap_list(spa, dnode, zap, zfs_printf);
2793 	}
2794 	free(zap);
2795 	return (rc);
2796 }
2797 
2798 static int
2799 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2800     dnode_phys_t *dnode)
2801 {
2802 	off_t offset;
2803 
2804 	offset = objnum * sizeof(dnode_phys_t);
2805 	return dnode_read(spa, &os->os_meta_dnode, offset,
2806 		dnode, sizeof(dnode_phys_t));
2807 }
2808 
2809 /*
2810  * Lookup a name in a microzap directory.
2811  */
2812 static int
2813 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2814 {
2815 	const mzap_ent_phys_t *mze;
2816 	int chunks, i;
2817 
2818 	/*
2819 	 * Microzap objects use exactly one block. Read the whole
2820 	 * thing.
2821 	 */
2822 	chunks = size / MZAP_ENT_LEN - 1;
2823 	for (i = 0; i < chunks; i++) {
2824 		mze = &mz->mz_chunk[i];
2825 		if (value == mze->mze_value) {
2826 			strcpy(name, mze->mze_name);
2827 			return (0);
2828 		}
2829 	}
2830 
2831 	return (ENOENT);
2832 }
2833 
2834 static void
2835 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2836 {
2837 	size_t namelen;
2838 	const zap_leaf_chunk_t *nc;
2839 	char *p;
2840 
2841 	namelen = zc->l_entry.le_name_numints;
2842 
2843 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2844 	p = name;
2845 	while (namelen > 0) {
2846 		size_t len;
2847 		len = namelen;
2848 		if (len > ZAP_LEAF_ARRAY_BYTES)
2849 			len = ZAP_LEAF_ARRAY_BYTES;
2850 		memcpy(p, nc->l_array.la_array, len);
2851 		p += len;
2852 		namelen -= len;
2853 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2854 	}
2855 
2856 	*p = '\0';
2857 }
2858 
2859 static int
2860 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2861     char *name, uint64_t value)
2862 {
2863 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2864 	fat_zap_t z;
2865 	uint64_t i;
2866 	int j, rc;
2867 
2868 	if (zh->zap_magic != ZAP_MAGIC)
2869 		return (EIO);
2870 
2871 	z.zap_block_shift = ilog2(bsize);
2872 	z.zap_phys = zh;
2873 
2874 	/*
2875 	 * This assumes that the leaf blocks start at block 1. The
2876 	 * documentation isn't exactly clear on this.
2877 	 */
2878 	zap_leaf_t zl;
2879 	zl.l_bs = z.zap_block_shift;
2880 	zl.l_phys = malloc(bsize);
2881 	if (zl.l_phys == NULL)
2882 		return (ENOMEM);
2883 
2884 	for (i = 0; i < zh->zap_num_leafs; i++) {
2885 		off_t off = ((off_t)(i + 1)) << zl.l_bs;
2886 
2887 		rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
2888 		if (rc != 0)
2889 			goto done;
2890 
2891 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2892 			zap_leaf_chunk_t *zc;
2893 
2894 			zc = &ZAP_LEAF_CHUNK(&zl, j);
2895 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2896 				continue;
2897 			if (zc->l_entry.le_value_intlen != 8 ||
2898 			    zc->l_entry.le_value_numints != 1)
2899 				continue;
2900 
2901 			if (fzap_leaf_value(&zl, zc) == value) {
2902 				fzap_name_copy(&zl, zc, name);
2903 				goto done;
2904 			}
2905 		}
2906 	}
2907 
2908 	rc = ENOENT;
2909 done:
2910 	free(zl.l_phys);
2911 	return (rc);
2912 }
2913 
2914 static int
2915 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
2916     uint64_t value)
2917 {
2918 	zap_phys_t *zap;
2919 	size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2920 	int rc;
2921 
2922 	zap = malloc(size);
2923 	if (zap == NULL)
2924 		return (ENOMEM);
2925 
2926 	rc = dnode_read(spa, dnode, 0, zap, size);
2927 	if (rc == 0) {
2928 		if (zap->zap_block_type == ZBT_MICRO)
2929 			rc = mzap_rlookup((const mzap_phys_t *)zap, size,
2930 			    name, value);
2931 		else
2932 			rc = fzap_rlookup(spa, dnode, zap, name, value);
2933 	}
2934 	free(zap);
2935 	return (rc);
2936 }
2937 
2938 static int
2939 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
2940 {
2941 	char name[256];
2942 	char component[256];
2943 	uint64_t dir_obj, parent_obj, child_dir_zapobj;
2944 	dnode_phys_t child_dir_zap, dataset, dir, parent;
2945 	dsl_dir_phys_t *dd;
2946 	dsl_dataset_phys_t *ds;
2947 	char *p;
2948 	int len;
2949 
2950 	p = &name[sizeof(name) - 1];
2951 	*p = '\0';
2952 
2953 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2954 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2955 		return (EIO);
2956 	}
2957 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
2958 	dir_obj = ds->ds_dir_obj;
2959 
2960 	for (;;) {
2961 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
2962 			return (EIO);
2963 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2964 
2965 		/* Actual loop condition. */
2966 		parent_obj = dd->dd_parent_obj;
2967 		if (parent_obj == 0)
2968 			break;
2969 
2970 		if (objset_get_dnode(spa, &spa->spa_mos, parent_obj,
2971 		    &parent) != 0)
2972 			return (EIO);
2973 		dd = (dsl_dir_phys_t *)&parent.dn_bonus;
2974 		child_dir_zapobj = dd->dd_child_dir_zapobj;
2975 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
2976 		    &child_dir_zap) != 0)
2977 			return (EIO);
2978 		if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
2979 			return (EIO);
2980 
2981 		len = strlen(component);
2982 		p -= len;
2983 		memcpy(p, component, len);
2984 		--p;
2985 		*p = '/';
2986 
2987 		/* Actual loop iteration. */
2988 		dir_obj = parent_obj;
2989 	}
2990 
2991 	if (*p != '\0')
2992 		++p;
2993 	strcpy(result, p);
2994 
2995 	return (0);
2996 }
2997 
2998 static int
2999 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3000 {
3001 	char element[256];
3002 	uint64_t dir_obj, child_dir_zapobj;
3003 	dnode_phys_t child_dir_zap, dir;
3004 	dsl_dir_phys_t *dd;
3005 	const char *p, *q;
3006 
3007 	if (objset_get_dnode(spa, &spa->spa_mos,
3008 	    DMU_POOL_DIRECTORY_OBJECT, &dir))
3009 		return (EIO);
3010 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3011 	    1, &dir_obj))
3012 		return (EIO);
3013 
3014 	p = name;
3015 	for (;;) {
3016 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
3017 			return (EIO);
3018 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3019 
3020 		while (*p == '/')
3021 			p++;
3022 		/* Actual loop condition #1. */
3023 		if (*p == '\0')
3024 			break;
3025 
3026 		q = strchr(p, '/');
3027 		if (q) {
3028 			memcpy(element, p, q - p);
3029 			element[q - p] = '\0';
3030 			p = q + 1;
3031 		} else {
3032 			strcpy(element, p);
3033 			p += strlen(p);
3034 		}
3035 
3036 		child_dir_zapobj = dd->dd_child_dir_zapobj;
3037 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3038 		    &child_dir_zap) != 0)
3039 			return (EIO);
3040 
3041 		/* Actual loop condition #2. */
3042 		if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3043 		    1, &dir_obj) != 0)
3044 			return (ENOENT);
3045 	}
3046 
3047 	*objnum = dd->dd_head_dataset_obj;
3048 	return (0);
3049 }
3050 
3051 #ifndef BOOT2
3052 static int
3053 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3054 {
3055 	uint64_t dir_obj, child_dir_zapobj;
3056 	dnode_phys_t child_dir_zap, dir, dataset;
3057 	dsl_dataset_phys_t *ds;
3058 	dsl_dir_phys_t *dd;
3059 
3060 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
3061 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3062 		return (EIO);
3063 	}
3064 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3065 	dir_obj = ds->ds_dir_obj;
3066 
3067 	if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
3068 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3069 		return (EIO);
3070 	}
3071 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3072 
3073 	child_dir_zapobj = dd->dd_child_dir_zapobj;
3074 	if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3075 	    &child_dir_zap) != 0) {
3076 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3077 		return (EIO);
3078 	}
3079 
3080 	return (zap_list(spa, &child_dir_zap) != 0);
3081 }
3082 
3083 int
3084 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3085     int (*callback)(const char *, uint64_t))
3086 {
3087 	uint64_t dir_obj, child_dir_zapobj;
3088 	dnode_phys_t child_dir_zap, dir, dataset;
3089 	dsl_dataset_phys_t *ds;
3090 	dsl_dir_phys_t *dd;
3091 	zap_phys_t *zap;
3092 	size_t size;
3093 	int err;
3094 
3095 	err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
3096 	if (err != 0) {
3097 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3098 		return (err);
3099 	}
3100 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3101 	dir_obj = ds->ds_dir_obj;
3102 
3103 	err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
3104 	if (err != 0) {
3105 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3106 		return (err);
3107 	}
3108 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3109 
3110 	child_dir_zapobj = dd->dd_child_dir_zapobj;
3111 	err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3112 	    &child_dir_zap);
3113 	if (err != 0) {
3114 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3115 		return (err);
3116 	}
3117 
3118 	size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3119 	zap = malloc(size);
3120 	if (zap != NULL) {
3121 		err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3122 		if (err != 0)
3123 			goto done;
3124 
3125 		if (zap->zap_block_type == ZBT_MICRO)
3126 			err = mzap_list((const mzap_phys_t *)zap, size,
3127 			    callback);
3128 		else
3129 			err = fzap_list(spa, &child_dir_zap, zap, callback);
3130 	} else {
3131 		err = ENOMEM;
3132 	}
3133 done:
3134 	free(zap);
3135 	return (err);
3136 }
3137 #endif
3138 
3139 /*
3140  * Find the object set given the object number of its dataset object
3141  * and return its details in *objset
3142  */
3143 static int
3144 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3145 {
3146 	dnode_phys_t dataset;
3147 	dsl_dataset_phys_t *ds;
3148 
3149 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
3150 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3151 		return (EIO);
3152 	}
3153 
3154 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3155 	if (zio_read(spa, &ds->ds_bp, objset)) {
3156 		printf("ZFS: can't read object set for dataset %ju\n",
3157 		    (uintmax_t)objnum);
3158 		return (EIO);
3159 	}
3160 
3161 	return (0);
3162 }
3163 
3164 /*
3165  * Find the object set pointed to by the BOOTFS property or the root
3166  * dataset if there is none and return its details in *objset
3167  */
3168 static int
3169 zfs_get_root(const spa_t *spa, uint64_t *objid)
3170 {
3171 	dnode_phys_t dir, propdir;
3172 	uint64_t props, bootfs, root;
3173 
3174 	*objid = 0;
3175 
3176 	/*
3177 	 * Start with the MOS directory object.
3178 	 */
3179 	if (objset_get_dnode(spa, &spa->spa_mos,
3180 	    DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3181 		printf("ZFS: can't read MOS object directory\n");
3182 		return (EIO);
3183 	}
3184 
3185 	/*
3186 	 * Lookup the pool_props and see if we can find a bootfs.
3187 	 */
3188 	if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3189 	    sizeof(props), 1, &props) == 0 &&
3190 	    objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0 &&
3191 	    zap_lookup(spa, &propdir, "bootfs",
3192 	    sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3193 		*objid = bootfs;
3194 		return (0);
3195 	}
3196 	/*
3197 	 * Lookup the root dataset directory
3198 	 */
3199 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3200 	    sizeof(root), 1, &root) ||
3201 	    objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
3202 		printf("ZFS: can't find root dsl_dir\n");
3203 		return (EIO);
3204 	}
3205 
3206 	/*
3207 	 * Use the information from the dataset directory's bonus buffer
3208 	 * to find the dataset object and from that the object set itself.
3209 	 */
3210 	dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3211 	*objid = dd->dd_head_dataset_obj;
3212 	return (0);
3213 }
3214 
3215 static int
3216 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3217 {
3218 
3219 	mount->spa = spa;
3220 
3221 	/*
3222 	 * Find the root object set if not explicitly provided
3223 	 */
3224 	if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3225 		printf("ZFS: can't find root filesystem\n");
3226 		return (EIO);
3227 	}
3228 
3229 	if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3230 		printf("ZFS: can't open root filesystem\n");
3231 		return (EIO);
3232 	}
3233 
3234 	mount->rootobj = rootobj;
3235 
3236 	return (0);
3237 }
3238 
3239 /*
3240  * callback function for feature name checks.
3241  */
3242 static int
3243 check_feature(const char *name, uint64_t value)
3244 {
3245 	int i;
3246 
3247 	if (value == 0)
3248 		return (0);
3249 	if (name[0] == '\0')
3250 		return (0);
3251 
3252 	for (i = 0; features_for_read[i] != NULL; i++) {
3253 		if (strcmp(name, features_for_read[i]) == 0)
3254 			return (0);
3255 	}
3256 	printf("ZFS: unsupported feature: %s\n", name);
3257 	return (EIO);
3258 }
3259 
3260 /*
3261  * Checks whether the MOS features that are active are supported.
3262  */
3263 static int
3264 check_mos_features(const spa_t *spa)
3265 {
3266 	dnode_phys_t dir;
3267 	zap_phys_t *zap;
3268 	uint64_t objnum;
3269 	size_t size;
3270 	int rc;
3271 
3272 	if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3273 	    &dir)) != 0)
3274 		return (rc);
3275 	if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3276 	    sizeof (objnum), 1, &objnum)) != 0) {
3277 		/*
3278 		 * It is older pool without features. As we have already
3279 		 * tested the label, just return without raising the error.
3280 		 */
3281 		return (0);
3282 	}
3283 
3284 	if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
3285 		return (rc);
3286 
3287 	if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3288 		return (EIO);
3289 
3290 	size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3291 	zap = malloc(size);
3292 	if (zap == NULL)
3293 		return (ENOMEM);
3294 
3295 	if (dnode_read(spa, &dir, 0, zap, size)) {
3296 		free(zap);
3297 		return (EIO);
3298 	}
3299 
3300 	if (zap->zap_block_type == ZBT_MICRO)
3301 		rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3302 	else
3303 		rc = fzap_list(spa, &dir, zap, check_feature);
3304 
3305 	free(zap);
3306 	return (rc);
3307 }
3308 
3309 static int
3310 load_nvlist(spa_t *spa, uint64_t obj, unsigned char **value)
3311 {
3312 	dnode_phys_t dir;
3313 	size_t size;
3314 	int rc;
3315 	unsigned char *nv;
3316 
3317 	*value = NULL;
3318 	if ((rc = objset_get_dnode(spa, &spa->spa_mos, obj, &dir)) != 0)
3319 		return (rc);
3320 	if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3321 	    dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3322 		return (EIO);
3323 	}
3324 
3325 	if (dir.dn_bonuslen != sizeof (uint64_t))
3326 		return (EIO);
3327 
3328 	size = *(uint64_t *)DN_BONUS(&dir);
3329 	nv = malloc(size);
3330 	if (nv == NULL)
3331 		return (ENOMEM);
3332 
3333 	rc = dnode_read(spa, &dir, 0, nv, size);
3334 	if (rc != 0) {
3335 		free(nv);
3336 		nv = NULL;
3337 		return (rc);
3338 	}
3339 	*value = nv;
3340 	return (rc);
3341 }
3342 
3343 static int
3344 zfs_spa_init(spa_t *spa)
3345 {
3346 	dnode_phys_t dir;
3347 	uint64_t config_object;
3348 	unsigned char *nvlist;
3349 	int rc;
3350 
3351 	if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
3352 		printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3353 		return (EIO);
3354 	}
3355 	if (spa->spa_mos.os_type != DMU_OST_META) {
3356 		printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3357 		return (EIO);
3358 	}
3359 
3360 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
3361 	    &dir)) {
3362 		printf("ZFS: failed to read pool %s directory object\n",
3363 		    spa->spa_name);
3364 		return (EIO);
3365 	}
3366 	/* this is allowed to fail, older pools do not have salt */
3367 	rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3368 	    sizeof (spa->spa_cksum_salt.zcs_bytes),
3369 	    spa->spa_cksum_salt.zcs_bytes);
3370 
3371 	rc = check_mos_features(spa);
3372 	if (rc != 0) {
3373 		printf("ZFS: pool %s is not supported\n", spa->spa_name);
3374 		return (rc);
3375 	}
3376 
3377 	rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3378 	    sizeof (config_object), 1, &config_object);
3379 	if (rc != 0) {
3380 		printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3381 		return (EIO);
3382 	}
3383 	rc = load_nvlist(spa, config_object, &nvlist);
3384 	if (rc != 0)
3385 		return (rc);
3386 
3387 	/*
3388 	 * Update vdevs from MOS config. Note, we do skip encoding bytes
3389 	 * here. See also vdev_label_read_config().
3390 	 */
3391 	rc = vdev_init_from_nvlist(spa, nvlist + 4);
3392 	free(nvlist);
3393 	return (rc);
3394 }
3395 
3396 static int
3397 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3398 {
3399 
3400 	if (dn->dn_bonustype != DMU_OT_SA) {
3401 		znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3402 
3403 		sb->st_mode = zp->zp_mode;
3404 		sb->st_uid = zp->zp_uid;
3405 		sb->st_gid = zp->zp_gid;
3406 		sb->st_size = zp->zp_size;
3407 	} else {
3408 		sa_hdr_phys_t *sahdrp;
3409 		int hdrsize;
3410 		size_t size = 0;
3411 		void *buf = NULL;
3412 
3413 		if (dn->dn_bonuslen != 0)
3414 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3415 		else {
3416 			if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3417 				blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3418 				int error;
3419 
3420 				size = BP_GET_LSIZE(bp);
3421 				buf = zfs_alloc(size);
3422 				error = zio_read(spa, bp, buf);
3423 				if (error != 0) {
3424 					zfs_free(buf, size);
3425 					return (error);
3426 				}
3427 				sahdrp = buf;
3428 			} else {
3429 				return (EIO);
3430 			}
3431 		}
3432 		hdrsize = SA_HDR_SIZE(sahdrp);
3433 		sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3434 		    SA_MODE_OFFSET);
3435 		sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3436 		    SA_UID_OFFSET);
3437 		sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3438 		    SA_GID_OFFSET);
3439 		sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3440 		    SA_SIZE_OFFSET);
3441 		if (buf != NULL)
3442 			zfs_free(buf, size);
3443 	}
3444 
3445 	return (0);
3446 }
3447 
3448 static int
3449 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3450 {
3451 	int rc = 0;
3452 
3453 	if (dn->dn_bonustype == DMU_OT_SA) {
3454 		sa_hdr_phys_t *sahdrp = NULL;
3455 		size_t size = 0;
3456 		void *buf = NULL;
3457 		int hdrsize;
3458 		char *p;
3459 
3460 		if (dn->dn_bonuslen != 0)
3461 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3462 		else {
3463 			blkptr_t *bp;
3464 
3465 			if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3466 				return (EIO);
3467 			bp = DN_SPILL_BLKPTR(dn);
3468 
3469 			size = BP_GET_LSIZE(bp);
3470 			buf = zfs_alloc(size);
3471 			rc = zio_read(spa, bp, buf);
3472 			if (rc != 0) {
3473 				zfs_free(buf, size);
3474 				return (rc);
3475 			}
3476 			sahdrp = buf;
3477 		}
3478 		hdrsize = SA_HDR_SIZE(sahdrp);
3479 		p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3480 		memcpy(path, p, psize);
3481 		if (buf != NULL)
3482 			zfs_free(buf, size);
3483 		return (0);
3484 	}
3485 	/*
3486 	 * Second test is purely to silence bogus compiler
3487 	 * warning about accessing past the end of dn_bonus.
3488 	 */
3489 	if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3490 	    sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3491 		memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3492 	} else {
3493 		rc = dnode_read(spa, dn, 0, path, psize);
3494 	}
3495 	return (rc);
3496 }
3497 
3498 struct obj_list {
3499 	uint64_t		objnum;
3500 	STAILQ_ENTRY(obj_list)	entry;
3501 };
3502 
3503 /*
3504  * Lookup a file and return its dnode.
3505  */
3506 static int
3507 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3508 {
3509 	int rc;
3510 	uint64_t objnum;
3511 	const spa_t *spa;
3512 	dnode_phys_t dn;
3513 	const char *p, *q;
3514 	char element[256];
3515 	char path[1024];
3516 	int symlinks_followed = 0;
3517 	struct stat sb;
3518 	struct obj_list *entry, *tentry;
3519 	STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3520 
3521 	spa = mount->spa;
3522 	if (mount->objset.os_type != DMU_OST_ZFS) {
3523 		printf("ZFS: unexpected object set type %ju\n",
3524 		    (uintmax_t)mount->objset.os_type);
3525 		return (EIO);
3526 	}
3527 
3528 	if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3529 		return (ENOMEM);
3530 
3531 	/*
3532 	 * Get the root directory dnode.
3533 	 */
3534 	rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3535 	if (rc) {
3536 		free(entry);
3537 		return (rc);
3538 	}
3539 
3540 	rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3541 	if (rc) {
3542 		free(entry);
3543 		return (rc);
3544 	}
3545 	entry->objnum = objnum;
3546 	STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3547 
3548 	rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3549 	if (rc != 0)
3550 		goto done;
3551 
3552 	p = upath;
3553 	while (p && *p) {
3554 		rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3555 		if (rc != 0)
3556 			goto done;
3557 
3558 		while (*p == '/')
3559 			p++;
3560 		if (*p == '\0')
3561 			break;
3562 		q = p;
3563 		while (*q != '\0' && *q != '/')
3564 			q++;
3565 
3566 		/* skip dot */
3567 		if (p + 1 == q && p[0] == '.') {
3568 			p++;
3569 			continue;
3570 		}
3571 		/* double dot */
3572 		if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3573 			p += 2;
3574 			if (STAILQ_FIRST(&on_cache) ==
3575 			    STAILQ_LAST(&on_cache, obj_list, entry)) {
3576 				rc = ENOENT;
3577 				goto done;
3578 			}
3579 			entry = STAILQ_FIRST(&on_cache);
3580 			STAILQ_REMOVE_HEAD(&on_cache, entry);
3581 			free(entry);
3582 			objnum = (STAILQ_FIRST(&on_cache))->objnum;
3583 			continue;
3584 		}
3585 		if (q - p + 1 > sizeof(element)) {
3586 			rc = ENAMETOOLONG;
3587 			goto done;
3588 		}
3589 		memcpy(element, p, q - p);
3590 		element[q - p] = 0;
3591 		p = q;
3592 
3593 		if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3594 			goto done;
3595 		if (!S_ISDIR(sb.st_mode)) {
3596 			rc = ENOTDIR;
3597 			goto done;
3598 		}
3599 
3600 		rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3601 		if (rc)
3602 			goto done;
3603 		objnum = ZFS_DIRENT_OBJ(objnum);
3604 
3605 		if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3606 			rc = ENOMEM;
3607 			goto done;
3608 		}
3609 		entry->objnum = objnum;
3610 		STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3611 		rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3612 		if (rc)
3613 			goto done;
3614 
3615 		/*
3616 		 * Check for symlink.
3617 		 */
3618 		rc = zfs_dnode_stat(spa, &dn, &sb);
3619 		if (rc)
3620 			goto done;
3621 		if (S_ISLNK(sb.st_mode)) {
3622 			if (symlinks_followed > 10) {
3623 				rc = EMLINK;
3624 				goto done;
3625 			}
3626 			symlinks_followed++;
3627 
3628 			/*
3629 			 * Read the link value and copy the tail of our
3630 			 * current path onto the end.
3631 			 */
3632 			if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3633 				rc = ENAMETOOLONG;
3634 				goto done;
3635 			}
3636 			strcpy(&path[sb.st_size], p);
3637 
3638 			rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3639 			if (rc != 0)
3640 				goto done;
3641 
3642 			/*
3643 			 * Restart with the new path, starting either at
3644 			 * the root or at the parent depending whether or
3645 			 * not the link is relative.
3646 			 */
3647 			p = path;
3648 			if (*p == '/') {
3649 				while (STAILQ_FIRST(&on_cache) !=
3650 				    STAILQ_LAST(&on_cache, obj_list, entry)) {
3651 					entry = STAILQ_FIRST(&on_cache);
3652 					STAILQ_REMOVE_HEAD(&on_cache, entry);
3653 					free(entry);
3654 				}
3655 			} else {
3656 				entry = STAILQ_FIRST(&on_cache);
3657 				STAILQ_REMOVE_HEAD(&on_cache, entry);
3658 				free(entry);
3659 			}
3660 			objnum = (STAILQ_FIRST(&on_cache))->objnum;
3661 		}
3662 	}
3663 
3664 	*dnode = dn;
3665 done:
3666 	STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
3667 		free(entry);
3668 	return (rc);
3669 }
3670