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