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