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