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