2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Stand-alone ZFS file reader.
35 #include <sys/endian.h>
37 #include <sys/stdint.h>
39 #include <sys/zfs_bootenv.h>
40 #include <machine/_inttypes.h>
46 extern int zstd_init(void);
54 STAILQ_ENTRY(zfsmount) next;
57 typedef STAILQ_HEAD(zfs_mnt_list, zfsmount) zfs_mnt_list_t;
58 static zfs_mnt_list_t zfsmount = STAILQ_HEAD_INITIALIZER(zfsmount);
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.
67 typedef struct indirect_child {
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.
78 typedef struct indirect_split {
79 list_node_t is_node; /* link on iv_splits */
82 * is_split_offset is the offset into the i/o.
83 * This is the sum of the previous splits' is_size's.
85 uint64_t is_split_offset;
87 vdev_t *is_vdev; /* top-level vdev */
88 uint64_t is_target_offset; /* offset on is_vdev */
90 int is_children; /* number of entries in is_child[] */
93 * is_good_child is the child that we are currently using to
94 * attempt reconstruction.
98 indirect_child_t is_child[1]; /* variable-length */
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.
105 typedef struct indirect_vsd {
106 boolean_t iv_split_block;
107 boolean_t iv_reconstruct;
109 list_t iv_splits; /* list of indirect_split_t's */
113 * List of all vdevs, chained through v_alllink.
115 static vdev_list_t zfs_vdevs;
118 * List of ZFS features supported for read
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 "org.freebsd:zstd_compress",
137 "org.illumos:lz4_compress",
138 "org.illumos:sha512",
140 "org.open-zfs:large_blocks",
141 "org.openzfs:blake3",
142 "org.zfsonlinux:allocation_classes",
143 "org.zfsonlinux:large_dnode",
148 * List of all pools, chained through spa_link.
150 static spa_list_t zfs_pools;
152 static const dnode_phys_t *dnode_cache_obj;
153 static uint64_t dnode_cache_bn;
154 static char *dnode_cache_buf;
156 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
157 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
158 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
159 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
160 const char *name, uint64_t integer_size, uint64_t num_integers,
162 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
164 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
166 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
168 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
169 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
171 vdev_indirect_mapping_entry_phys_t *
172 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
173 uint64_t, uint64_t *);
178 STAILQ_INIT(&zfs_vdevs);
179 STAILQ_INIT(&zfs_pools);
181 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
190 nvlist_check_features_for_read(nvlist_t *nvl)
192 nvlist_t *features = NULL;
195 nv_string_t *nvp_name;
198 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
199 DATA_TYPE_NVLIST, NULL, &features, NULL);
202 break; /* Continue with checks */
205 return (0); /* All features are disabled */
208 return (rc); /* Error while reading nvlist */
211 data = (nvs_data_t *)features->nv_data;
212 nvp = &data->nvl_pair; /* first pair in nvlist */
214 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
217 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
220 for (i = 0; features_for_read[i] != NULL; i++) {
221 if (memcmp(nvp_name->nv_data, features_for_read[i],
222 nvp_name->nv_size) == 0) {
229 printf("ZFS: unsupported feature: %.*s\n",
230 nvp_name->nv_size, nvp_name->nv_data);
233 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
235 nvlist_destroy(features);
241 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
242 off_t offset, size_t size)
247 if (vdev->v_phys_read == NULL)
251 psize = BP_GET_PSIZE(bp);
256 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
259 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
266 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
268 if (vdev->v_phys_write == NULL)
271 return (vdev->v_phys_write(vdev, offset, buf, size));
274 typedef struct remap_segment {
278 uint64_t rs_split_offset;
282 static remap_segment_t *
283 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
285 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
289 rs->rs_offset = offset;
290 rs->rs_asize = asize;
291 rs->rs_split_offset = split_offset;
297 vdev_indirect_mapping_t *
298 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
299 uint64_t mapping_object)
301 vdev_indirect_mapping_t *vim;
302 vdev_indirect_mapping_phys_t *vim_phys;
305 vim = calloc(1, sizeof (*vim));
309 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
310 if (vim->vim_dn == NULL) {
315 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
323 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
324 if (vim->vim_phys == NULL) {
330 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
331 *vim->vim_phys = *vim_phys;
333 vim->vim_objset = os;
334 vim->vim_object = mapping_object;
335 vim->vim_entries = NULL;
337 vim->vim_havecounts =
338 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
344 * Compare an offset with an indirect mapping entry; there are three
345 * possible scenarios:
347 * 1. The offset is "less than" the mapping entry; meaning the
348 * offset is less than the source offset of the mapping entry. In
349 * this case, there is no overlap between the offset and the
350 * mapping entry and -1 will be returned.
352 * 2. The offset is "greater than" the mapping entry; meaning the
353 * offset is greater than the mapping entry's source offset plus
354 * the entry's size. In this case, there is no overlap between
355 * the offset and the mapping entry and 1 will be returned.
357 * NOTE: If the offset is actually equal to the entry's offset
358 * plus size, this is considered to be "greater" than the entry,
359 * and this case applies (i.e. 1 will be returned). Thus, the
360 * entry's "range" can be considered to be inclusive at its
361 * start, but exclusive at its end: e.g. [src, src + size).
363 * 3. The last case to consider is if the offset actually falls
364 * within the mapping entry's range. If this is the case, the
365 * offset is considered to be "equal to" the mapping entry and
366 * 0 will be returned.
368 * NOTE: If the offset is equal to the entry's source offset,
369 * this case applies and 0 will be returned. If the offset is
370 * equal to the entry's source plus its size, this case does
371 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
375 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
377 const uint64_t *key = v_key;
378 const vdev_indirect_mapping_entry_phys_t *array_elem =
380 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
382 if (*key < src_offset) {
384 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
392 * Return array entry.
394 static vdev_indirect_mapping_entry_phys_t *
395 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
401 if (vim->vim_phys->vimp_num_entries == 0)
404 if (vim->vim_entries == NULL) {
407 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
408 size = vim->vim_phys->vimp_num_entries *
409 sizeof (*vim->vim_entries);
411 size = bsize / sizeof (*vim->vim_entries);
412 size *= sizeof (*vim->vim_entries);
414 vim->vim_entries = malloc(size);
415 if (vim->vim_entries == NULL)
417 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
418 offset = index * sizeof (*vim->vim_entries);
421 /* We have data in vim_entries */
423 if (index >= vim->vim_entry_offset &&
424 index <= vim->vim_entry_offset + vim->vim_num_entries) {
425 index -= vim->vim_entry_offset;
426 return (&vim->vim_entries[index]);
428 offset = index * sizeof (*vim->vim_entries);
431 vim->vim_entry_offset = index;
432 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
433 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
436 /* Read error, invalidate vim_entries. */
437 free(vim->vim_entries);
438 vim->vim_entries = NULL;
441 index -= vim->vim_entry_offset;
442 return (&vim->vim_entries[index]);
446 * Returns the mapping entry for the given offset.
448 * It's possible that the given offset will not be in the mapping table
449 * (i.e. no mapping entries contain this offset), in which case, the
450 * return value depends on the "next_if_missing" parameter.
452 * If the offset is not found in the table and "next_if_missing" is
453 * B_FALSE, then NULL will always be returned. The behavior is intended
454 * to allow consumers to get the entry corresponding to the offset
455 * parameter, iff the offset overlaps with an entry in the table.
457 * If the offset is not found in the table and "next_if_missing" is
458 * B_TRUE, then the entry nearest to the given offset will be returned,
459 * such that the entry's source offset is greater than the offset
460 * passed in (i.e. the "next" mapping entry in the table is returned, if
461 * the offset is missing from the table). If there are no entries whose
462 * source offset is greater than the passed in offset, NULL is returned.
464 static vdev_indirect_mapping_entry_phys_t *
465 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
468 ASSERT(vim->vim_phys->vimp_num_entries > 0);
470 vdev_indirect_mapping_entry_phys_t *entry;
472 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
476 * We don't define these inside of the while loop because we use
477 * their value in the case that offset isn't in the mapping.
482 while (last >= base) {
483 mid = base + ((last - base) >> 1);
485 entry = vdev_indirect_mapping_entry(vim, mid);
488 result = dva_mapping_overlap_compare(&offset, entry);
492 } else if (result < 0) {
502 * Given an indirect vdev and an extent on that vdev, it duplicates the
503 * physical entries of the indirect mapping that correspond to the extent
504 * to a new array and returns a pointer to it. In addition, copied_entries
505 * is populated with the number of mapping entries that were duplicated.
507 * Finally, since we are doing an allocation, it is up to the caller to
508 * free the array allocated in this function.
510 vdev_indirect_mapping_entry_phys_t *
511 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
512 uint64_t asize, uint64_t *copied_entries)
514 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
515 vdev_indirect_mapping_t *vim = vd->v_mapping;
516 uint64_t entries = 0;
518 vdev_indirect_mapping_entry_phys_t *first_mapping =
519 vdev_indirect_mapping_entry_for_offset(vim, offset);
520 ASSERT3P(first_mapping, !=, NULL);
522 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
524 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
525 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
526 uint64_t inner_size = MIN(asize, size - inner_offset);
528 offset += inner_size;
534 size_t copy_length = entries * sizeof (*first_mapping);
535 duplicate_mappings = malloc(copy_length);
536 if (duplicate_mappings != NULL)
537 bcopy(first_mapping, duplicate_mappings, copy_length);
541 *copied_entries = entries;
543 return (duplicate_mappings);
547 vdev_lookup_top(spa_t *spa, uint64_t vdev)
552 vlist = &spa->spa_root_vdev->v_children;
553 STAILQ_FOREACH(rvd, vlist, v_childlink)
554 if (rvd->v_id == vdev)
561 * This is a callback for vdev_indirect_remap() which allocates an
562 * indirect_split_t for each split segment and adds it to iv_splits.
565 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
566 uint64_t size, void *arg)
570 indirect_vsd_t *iv = zio->io_vsd;
572 if (vd->v_read == vdev_indirect_read)
575 if (vd->v_read == vdev_mirror_read)
578 indirect_split_t *is =
579 malloc(offsetof(indirect_split_t, is_child[n]));
581 zio->io_error = ENOMEM;
584 bzero(is, offsetof(indirect_split_t, is_child[n]));
588 is->is_split_offset = split_offset;
589 is->is_target_offset = offset;
593 * Note that we only consider multiple copies of the data for
594 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
595 * though they use the same ops as mirror, because there's only one
596 * "good" copy under the replacing/spare.
598 if (vd->v_read == vdev_mirror_read) {
602 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
603 is->is_child[i++].ic_vdev = kid;
606 is->is_child[0].ic_vdev = vd;
609 list_insert_tail(&iv->iv_splits, is);
613 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
616 spa_t *spa = vd->v_spa;
620 list_create(&stack, sizeof (remap_segment_t),
621 offsetof(remap_segment_t, rs_node));
623 rs = rs_alloc(vd, offset, asize, 0);
625 printf("vdev_indirect_remap: out of memory.\n");
626 zio->io_error = ENOMEM;
628 for (; rs != NULL; rs = list_remove_head(&stack)) {
629 vdev_t *v = rs->rs_vd;
630 uint64_t num_entries = 0;
631 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
632 vdev_indirect_mapping_entry_phys_t *mapping =
633 vdev_indirect_mapping_duplicate_adjacent_entries(v,
634 rs->rs_offset, rs->rs_asize, &num_entries);
636 if (num_entries == 0)
637 zio->io_error = ENOMEM;
639 for (uint64_t i = 0; i < num_entries; i++) {
640 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
641 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
642 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
643 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
644 uint64_t inner_offset = rs->rs_offset -
645 DVA_MAPPING_GET_SRC_OFFSET(m);
646 uint64_t inner_size =
647 MIN(rs->rs_asize, size - inner_offset);
648 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
650 if (dst_v->v_read == vdev_indirect_read) {
653 o = rs_alloc(dst_v, dst_offset + inner_offset,
654 inner_size, rs->rs_split_offset);
656 printf("vdev_indirect_remap: "
658 zio->io_error = ENOMEM;
662 list_insert_head(&stack, o);
664 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
665 dst_offset + inner_offset,
669 * vdev_indirect_gather_splits can have memory
670 * allocation error, we can not recover from it.
672 if (zio->io_error != 0)
674 rs->rs_offset += inner_size;
675 rs->rs_asize -= inner_size;
676 rs->rs_split_offset += inner_size;
681 if (zio->io_error != 0)
685 list_destroy(&stack);
689 vdev_indirect_map_free(zio_t *zio)
691 indirect_vsd_t *iv = zio->io_vsd;
692 indirect_split_t *is;
694 while ((is = list_head(&iv->iv_splits)) != NULL) {
695 for (int c = 0; c < is->is_children; c++) {
696 indirect_child_t *ic = &is->is_child[c];
699 list_remove(&iv->iv_splits, is);
706 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
707 off_t offset, size_t bytes)
710 spa_t *spa = vdev->v_spa;
712 indirect_split_t *first;
715 iv = calloc(1, sizeof(*iv));
719 list_create(&iv->iv_splits,
720 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
722 bzero(&zio, sizeof(zio));
724 zio.io_bp = (blkptr_t *)bp;
727 zio.io_offset = offset;
731 if (vdev->v_mapping == NULL) {
732 vdev_indirect_config_t *vic;
734 vic = &vdev->vdev_indirect_config;
735 vdev->v_mapping = vdev_indirect_mapping_open(spa,
736 spa->spa_mos, vic->vic_mapping_object);
739 vdev_indirect_remap(vdev, offset, bytes, &zio);
740 if (zio.io_error != 0)
741 return (zio.io_error);
743 first = list_head(&iv->iv_splits);
744 if (first->is_size == zio.io_size) {
746 * This is not a split block; we are pointing to the entire
747 * data, which will checksum the same as the original data.
748 * Pass the BP down so that the child i/o can verify the
749 * checksum, and try a different location if available
750 * (e.g. on a mirror).
752 * While this special case could be handled the same as the
753 * general (split block) case, doing it this way ensures
754 * that the vast majority of blocks on indirect vdevs
755 * (which are not split) are handled identically to blocks
756 * on non-indirect vdevs. This allows us to be less strict
757 * about performance in the general (but rare) case.
759 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
760 zio.io_data, first->is_target_offset, bytes);
762 iv->iv_split_block = B_TRUE;
764 * Read one copy of each split segment, from the
765 * top-level vdev. Since we don't know the
766 * checksum of each split individually, the child
767 * zio can't ensure that we get the right data.
768 * E.g. if it's a mirror, it will just read from a
769 * random (healthy) leaf vdev. We have to verify
770 * the checksum in vdev_indirect_io_done().
772 for (indirect_split_t *is = list_head(&iv->iv_splits);
773 is != NULL; is = list_next(&iv->iv_splits, is)) {
774 char *ptr = zio.io_data;
776 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
777 ptr + is->is_split_offset, is->is_target_offset,
780 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
786 vdev_indirect_map_free(&zio);
794 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
795 off_t offset, size_t bytes)
798 return (vdev_read_phys(vdev, bp, buf,
799 offset + VDEV_LABEL_START_SIZE, bytes));
803 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
804 void *buf __unused, off_t offset __unused, size_t bytes __unused)
811 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
812 off_t offset, size_t bytes)
818 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
819 if (kid->v_state != VDEV_STATE_HEALTHY)
821 rc = kid->v_read(kid, bp, buf, offset, bytes);
830 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
831 off_t offset, size_t bytes)
836 * Here we should have two kids:
837 * First one which is the one we are replacing and we can trust
838 * only this one to have valid data, but it might not be present.
839 * Second one is that one we are replacing with. It is most likely
840 * healthy, but we can't trust it has needed data, so we won't use it.
842 kid = STAILQ_FIRST(&vdev->v_children);
845 if (kid->v_state != VDEV_STATE_HEALTHY)
847 return (kid->v_read(kid, bp, buf, offset, bytes));
851 vdev_find(uint64_t guid)
855 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
856 if (vdev->v_guid == guid)
863 vdev_create(uint64_t guid, vdev_read_t *_read)
866 vdev_indirect_config_t *vic;
868 vdev = calloc(1, sizeof(vdev_t));
870 STAILQ_INIT(&vdev->v_children);
872 vdev->v_read = _read;
875 * root vdev has no read function, we use this fact to
876 * skip setting up data we do not need for root vdev.
877 * We only point root vdev from spa.
880 vic = &vdev->vdev_indirect_config;
881 vic->vic_prev_indirect_vdev = UINT64_MAX;
882 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
890 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
892 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
895 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
897 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
899 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
901 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
903 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
904 NULL, &is_degraded, NULL);
905 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
906 NULL, &isnt_present, NULL);
907 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
911 vdev->v_state = VDEV_STATE_OFFLINE;
912 else if (is_removed != 0)
913 vdev->v_state = VDEV_STATE_REMOVED;
914 else if (is_faulted != 0)
915 vdev->v_state = VDEV_STATE_FAULTED;
916 else if (is_degraded != 0)
917 vdev->v_state = VDEV_STATE_DEGRADED;
918 else if (isnt_present != 0)
919 vdev->v_state = VDEV_STATE_CANT_OPEN;
921 vdev->v_islog = is_log != 0;
925 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
927 uint64_t id, ashift, asize, nparity;
934 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
936 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
941 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
942 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
944 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
946 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
947 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
948 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
949 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
950 printf("ZFS: can only boot from disk, mirror, raidz1, "
951 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
955 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
956 vdev = vdev_create(guid, vdev_mirror_read);
957 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
958 vdev = vdev_create(guid, vdev_raidz_read);
959 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
960 vdev = vdev_create(guid, vdev_replacing_read);
961 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
962 vdev_indirect_config_t *vic;
964 vdev = vdev_create(guid, vdev_indirect_read);
966 vdev->v_state = VDEV_STATE_HEALTHY;
967 vic = &vdev->vdev_indirect_config;
970 ZPOOL_CONFIG_INDIRECT_OBJECT,
972 NULL, &vic->vic_mapping_object, NULL);
974 ZPOOL_CONFIG_INDIRECT_BIRTHS,
976 NULL, &vic->vic_births_object, NULL);
978 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
980 NULL, &vic->vic_prev_indirect_vdev, NULL);
982 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
983 vdev = vdev_create(guid, vdev_missing_read);
985 vdev = vdev_create(guid, vdev_disk_read);
991 vdev_set_initial_state(vdev, nvlist);
993 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
994 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
995 vdev->v_ashift = ashift;
997 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
998 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
999 vdev->v_psize = asize +
1000 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1003 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1004 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
1005 vdev->v_nparity = nparity;
1007 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1008 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1009 char prefix[] = "/dev/";
1011 len = strlen(prefix);
1012 if (len < pathlen && memcmp(path, prefix, len) == 0) {
1016 name = malloc(pathlen + 1);
1017 bcopy(path, name, pathlen);
1018 name[pathlen] = '\0';
1019 vdev->v_name = name;
1022 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1023 if (vdev->v_nparity < 1 ||
1024 vdev->v_nparity > 3) {
1025 printf("ZFS: invalid raidz parity: %d\n",
1029 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1030 vdev->v_nparity, id);
1032 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1034 vdev->v_name = name;
1041 * Find slot for vdev. We return either NULL to signal to use
1042 * STAILQ_INSERT_HEAD, or we return link element to be used with
1043 * STAILQ_INSERT_AFTER.
1046 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1048 vdev_t *v, *previous;
1050 if (STAILQ_EMPTY(&top_vdev->v_children))
1054 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1055 if (v->v_id > vdev->v_id)
1058 if (v->v_id == vdev->v_id)
1061 if (v->v_id < vdev->v_id)
1068 vdev_child_count(vdev_t *vdev)
1074 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1081 * Insert vdev into top_vdev children list. List is ordered by v_id.
1084 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1090 * The top level vdev can appear in random order, depending how
1091 * the firmware is presenting the disk devices.
1092 * However, we will insert vdev to create list ordered by v_id,
1093 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1094 * as STAILQ does not have insert before.
1096 previous = vdev_find_previous(top_vdev, vdev);
1098 if (previous == NULL) {
1099 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1100 } else if (previous->v_id == vdev->v_id) {
1102 * This vdev was configured from label config,
1103 * do not insert duplicate.
1107 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1111 count = vdev_child_count(top_vdev);
1112 if (top_vdev->v_nchildren < count)
1113 top_vdev->v_nchildren = count;
1117 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1119 vdev_t *top_vdev, *vdev;
1120 nvlist_t **kids = NULL;
1124 top_vdev = vdev_find(top_guid);
1125 if (top_vdev == NULL) {
1126 rc = vdev_init(top_guid, nvlist, &top_vdev);
1129 top_vdev->v_spa = spa;
1130 top_vdev->v_top = top_vdev;
1131 vdev_insert(spa->spa_root_vdev, top_vdev);
1134 /* Add children if there are any. */
1135 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1136 &nkids, &kids, NULL);
1138 for (int i = 0; i < nkids; i++) {
1141 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1142 DATA_TYPE_UINT64, NULL, &guid, NULL);
1146 rc = vdev_init(guid, kids[i], &vdev);
1151 vdev->v_top = top_vdev;
1152 vdev_insert(top_vdev, vdev);
1156 * When there are no children, nvlist_find() does return
1157 * error, reset it because leaf devices have no children.
1163 for (int i = 0; i < nkids; i++)
1164 nvlist_destroy(kids[i]);
1172 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1174 uint64_t pool_guid, top_guid;
1178 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1179 NULL, &pool_guid, NULL) ||
1180 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1181 NULL, &top_guid, NULL) ||
1182 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1183 NULL, &vdevs, NULL)) {
1184 printf("ZFS: can't find vdev details\n");
1188 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1189 nvlist_destroy(vdevs);
1194 vdev_set_state(vdev_t *vdev)
1200 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1201 vdev_set_state(kid);
1205 * A mirror or raidz is healthy if all its kids are healthy. A
1206 * mirror is degraded if any of its kids is healthy; a raidz
1207 * is degraded if at most nparity kids are offline.
1209 if (STAILQ_FIRST(&vdev->v_children)) {
1212 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1213 if (kid->v_state == VDEV_STATE_HEALTHY)
1218 if (bad_kids == 0) {
1219 vdev->v_state = VDEV_STATE_HEALTHY;
1221 if (vdev->v_read == vdev_mirror_read) {
1223 vdev->v_state = VDEV_STATE_DEGRADED;
1225 vdev->v_state = VDEV_STATE_OFFLINE;
1227 } else if (vdev->v_read == vdev_raidz_read) {
1228 if (bad_kids > vdev->v_nparity) {
1229 vdev->v_state = VDEV_STATE_OFFLINE;
1231 vdev->v_state = VDEV_STATE_DEGRADED;
1239 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1242 nvlist_t **kids = NULL;
1245 /* Update top vdev. */
1246 vdev = vdev_find(top_guid);
1248 vdev_set_initial_state(vdev, nvlist);
1250 /* Update children if there are any. */
1251 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1252 &nkids, &kids, NULL);
1254 for (int i = 0; i < nkids; i++) {
1257 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1258 DATA_TYPE_UINT64, NULL, &guid, NULL);
1262 vdev = vdev_find(guid);
1264 vdev_set_initial_state(vdev, kids[i]);
1270 for (int i = 0; i < nkids; i++)
1271 nvlist_destroy(kids[i]);
1279 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1281 uint64_t pool_guid, vdev_children;
1282 nvlist_t *vdevs = NULL, **kids = NULL;
1285 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1286 NULL, &pool_guid, NULL) ||
1287 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1288 NULL, &vdev_children, NULL) ||
1289 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1290 NULL, &vdevs, NULL)) {
1291 printf("ZFS: can't find vdev details\n");
1296 if (spa->spa_guid != pool_guid) {
1297 nvlist_destroy(vdevs);
1301 spa->spa_root_vdev->v_nchildren = vdev_children;
1303 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1304 &nkids, &kids, NULL);
1305 nvlist_destroy(vdevs);
1308 * MOS config has at least one child for root vdev.
1313 for (int i = 0; i < nkids; i++) {
1317 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1321 vdev = vdev_find(guid);
1323 * Top level vdev is missing, create it.
1326 rc = vdev_from_nvlist(spa, guid, kids[i]);
1328 rc = vdev_update_from_nvlist(guid, kids[i]);
1333 for (int i = 0; i < nkids; i++)
1334 nvlist_destroy(kids[i]);
1339 * Re-evaluate top-level vdev state.
1341 vdev_set_state(spa->spa_root_vdev);
1347 spa_find_by_guid(uint64_t guid)
1351 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1352 if (spa->spa_guid == guid)
1359 spa_find_by_name(const char *name)
1363 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1364 if (strcmp(spa->spa_name, name) == 0)
1371 spa_create(uint64_t guid, const char *name)
1375 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1377 if ((spa->spa_name = strdup(name)) == NULL) {
1381 spa->spa_uberblock = &spa->spa_uberblock_master;
1382 spa->spa_mos = &spa->spa_mos_master;
1383 spa->spa_guid = guid;
1384 spa->spa_root_vdev = vdev_create(guid, NULL);
1385 if (spa->spa_root_vdev == NULL) {
1386 free(spa->spa_name);
1390 spa->spa_root_vdev->v_name = strdup("root");
1391 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1397 state_name(vdev_state_t state)
1399 static const char *names[] = {
1409 return (names[state]);
1414 #define pager_printf printf
1419 pager_printf(const char *fmt, ...)
1424 va_start(args, fmt);
1425 vsnprintf(line, sizeof(line), fmt, args);
1427 return (pager_output(line));
1432 #define STATUS_FORMAT " %s %s\n"
1435 print_state(int indent, const char *name, vdev_state_t state)
1441 for (i = 0; i < indent; i++)
1444 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1448 vdev_status(vdev_t *vdev, int indent)
1453 if (vdev->v_islog) {
1454 (void) pager_output(" logs\n");
1458 ret = print_state(indent, vdev->v_name, vdev->v_state);
1462 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1463 ret = vdev_status(kid, indent + 1);
1471 spa_status(spa_t *spa)
1473 static char bootfs[ZFS_MAXNAMELEN];
1477 int good_kids, bad_kids, degraded_kids, ret;
1480 ret = pager_printf(" pool: %s\n", spa->spa_name);
1484 if (zfs_get_root(spa, &rootid) == 0 &&
1485 zfs_rlookup(spa, rootid, bootfs) == 0) {
1486 if (bootfs[0] == '\0')
1487 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1489 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1494 ret = pager_printf("config:\n\n");
1497 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1504 vlist = &spa->spa_root_vdev->v_children;
1505 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1506 if (vdev->v_state == VDEV_STATE_HEALTHY)
1508 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1514 state = VDEV_STATE_CLOSED;
1515 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1516 state = VDEV_STATE_HEALTHY;
1517 else if ((good_kids + degraded_kids) > 0)
1518 state = VDEV_STATE_DEGRADED;
1520 ret = print_state(0, spa->spa_name, state);
1524 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1525 ret = vdev_status(vdev, 1);
1533 spa_all_status(void)
1536 int first = 1, ret = 0;
1538 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1540 ret = pager_printf("\n");
1545 ret = spa_status(spa);
1553 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1555 uint64_t label_offset;
1557 if (l < VDEV_LABELS / 2)
1560 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1562 return (offset + l * sizeof (vdev_label_t) + label_offset);
1566 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1568 unsigned int seq1 = 0;
1569 unsigned int seq2 = 0;
1570 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1575 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1579 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1580 seq1 = MMP_SEQ(ub1);
1582 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1583 seq2 = MMP_SEQ(ub2);
1585 return (AVL_CMP(seq1, seq2));
1589 uberblock_verify(uberblock_t *ub)
1591 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1592 byteswap_uint64_array(ub, sizeof (uberblock_t));
1595 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1596 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1603 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1609 off = vdev_label_offset(vd->v_psize, l, offset);
1612 BP_SET_LSIZE(&bp, size);
1613 BP_SET_PSIZE(&bp, size);
1614 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1615 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1616 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1617 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1619 return (vdev_read_phys(vd, &bp, buf, off, size));
1623 * We do need to be sure we write to correct location.
1624 * Our vdev label does consist of 4 fields:
1625 * pad1 (8k), reserved.
1626 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1627 * vdev_phys (112k), checksummed
1628 * uberblock ring (128k), checksummed.
1630 * Since bootenv area may contain garbage, we can not reliably read it, as
1631 * we can get checksum errors.
1632 * Next best thing is vdev_phys - it is just after bootenv. It still may
1633 * be corrupted, but in such case we will miss this one write.
1636 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1638 uint64_t off, o_phys;
1640 size_t size = VDEV_PHYS_SIZE;
1643 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1644 off = vdev_label_offset(vd->v_psize, l, o_phys);
1646 /* off should be 8K from bootenv */
1647 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1654 /* Read vdev_phys */
1655 rc = vdev_label_read(vd, l, buf, o_phys, size);
1661 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1663 zio_checksum_info_t *ci;
1666 size_t size = VDEV_PAD_SIZE;
1669 if (vd->v_phys_write == NULL)
1672 off = vdev_label_offset(vd->v_psize, l, offset);
1674 rc = vdev_label_write_validate(vd, l, offset);
1679 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1680 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1681 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1682 ci->ci_func[0](be, size, NULL, &cksum);
1683 be->vbe_zbt.zec_cksum = cksum;
1685 return (vdev_write_phys(vd, be, off, size));
1689 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1694 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1695 if (kid->v_state != VDEV_STATE_HEALTHY)
1697 rc = vdev_write_bootenv_impl(kid, be);
1703 * Non-leaf vdevs do not have v_phys_write.
1705 if (vdev->v_phys_write == NULL)
1708 for (int l = 0; l < VDEV_LABELS; l++) {
1709 rc = vdev_label_write(vdev, l, be,
1710 offsetof(vdev_label_t, vl_be));
1712 printf("failed to write bootenv to %s label %d: %d\n",
1713 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1721 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1723 vdev_boot_envblock_t *be;
1728 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1732 nvp = vdev_read_bootenv(vdev);
1734 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1736 nvlist_destroy(nvp);
1739 be = calloc(1, sizeof(*be));
1743 be->vbe_version = version;
1747 * If there is no envmap, we will just wipe bootenv.
1749 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1750 be->vbe_bootenv, NULL);
1755 nv.nv_header = nvl->nv_header;
1756 nv.nv_asize = nvl->nv_asize;
1757 nv.nv_size = nvl->nv_size;
1759 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1760 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1761 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1762 rv = nvlist_export(&nv);
1771 be->vbe_version = htobe64(be->vbe_version);
1772 rv = vdev_write_bootenv_impl(vdev, be);
1779 * Read the bootenv area from pool label, return the nvlist from it.
1780 * We return from first successful read.
1783 vdev_read_bootenv(vdev_t *vdev)
1787 vdev_boot_envblock_t *be;
1792 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1793 if (kid->v_state != VDEV_STATE_HEALTHY)
1796 benv = vdev_read_bootenv(kid);
1801 be = malloc(sizeof (*be));
1806 for (int l = 0; l < VDEV_LABELS; l++) {
1807 rv = vdev_label_read(vdev, l, be,
1808 offsetof(vdev_label_t, vl_be),
1818 be->vbe_version = be64toh(be->vbe_version);
1819 switch (be->vbe_version) {
1822 * we have textual data in vbe_bootenv, create nvlist
1823 * with key "envmap".
1825 benv = nvlist_create(NV_UNIQUE_NAME);
1827 if (*be->vbe_bootenv == '\0') {
1828 nvlist_add_uint64(benv, BOOTENV_VERSION,
1832 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1833 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1834 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1839 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1843 command = (char *)be;
1846 /* Check for legacy zfsbootcfg command string */
1847 for (int i = 0; command[i] != '\0'; i++) {
1848 if (iscntrl(command[i])) {
1855 benv = nvlist_create(NV_UNIQUE_NAME);
1858 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1861 nvlist_add_uint64(benv, BOOTENV_VERSION,
1871 vdev_get_label_asize(nvlist_t *nvl)
1880 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1881 NULL, &vdevs, NULL) != 0)
1885 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1886 * For raidz, the asize is raw size of all children.
1888 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1889 NULL, &type, &len) != 0)
1892 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1893 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1894 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1897 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1898 NULL, &asize, NULL) != 0)
1901 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1905 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1906 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1912 for (int i = 0; i < nkids; i++)
1913 nvlist_destroy(kids[i]);
1917 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1919 nvlist_destroy(vdevs);
1924 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1927 uint64_t best_txg = 0;
1928 uint64_t label_txg = 0;
1930 nvlist_t *nvl = NULL, *tmp;
1933 label = malloc(sizeof (vdev_phys_t));
1937 for (int l = 0; l < VDEV_LABELS; l++) {
1938 if (vdev_label_read(vd, l, label,
1939 offsetof(vdev_label_t, vl_vdev_phys),
1940 sizeof (vdev_phys_t)))
1943 tmp = nvlist_import(label->vp_nvlist,
1944 sizeof(label->vp_nvlist));
1948 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1949 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1950 if (error != 0 || label_txg == 0) {
1951 nvlist_destroy(nvl);
1956 if (label_txg <= txg && label_txg > best_txg) {
1957 best_txg = label_txg;
1958 nvlist_destroy(nvl);
1963 * Use asize from pool config. We need this
1964 * because we can get bad value from BIOS.
1966 asize = vdev_get_label_asize(nvl);
1968 vd->v_psize = asize;
1971 nvlist_destroy(tmp);
1974 if (best_txg == 0) {
1975 nvlist_destroy(nvl);
1984 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1988 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1992 for (int l = 0; l < VDEV_LABELS; l++) {
1993 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1994 if (vdev_label_read(vd, l, buf,
1995 VDEV_UBERBLOCK_OFFSET(vd, n),
1996 VDEV_UBERBLOCK_SIZE(vd)))
1998 if (uberblock_verify(buf) != 0)
2001 if (vdev_uberblock_compare(buf, ub) > 0)
2009 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2017 uint64_t guid, vdev_children;
2018 uint64_t pool_txg, pool_guid;
2019 const char *pool_name;
2023 * Load the vdev label and figure out which
2024 * uberblock is most current.
2026 memset(&vtmp, 0, sizeof(vtmp));
2027 vtmp.v_phys_read = _read;
2028 vtmp.v_phys_write = _write;
2030 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2031 (uint64_t)sizeof (vdev_label_t));
2033 /* Test for minimum device size. */
2034 if (vtmp.v_psize < SPA_MINDEVSIZE)
2037 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2041 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2042 NULL, &val, NULL) != 0) {
2043 nvlist_destroy(nvl);
2047 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2048 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2049 (unsigned)val, (unsigned)SPA_VERSION);
2050 nvlist_destroy(nvl);
2054 /* Check ZFS features for read */
2055 rc = nvlist_check_features_for_read(nvl);
2057 nvlist_destroy(nvl);
2061 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2062 NULL, &val, NULL) != 0) {
2063 nvlist_destroy(nvl);
2067 if (val == POOL_STATE_DESTROYED) {
2068 /* We don't boot only from destroyed pools. */
2069 nvlist_destroy(nvl);
2073 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2074 NULL, &pool_txg, NULL) != 0 ||
2075 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2076 NULL, &pool_guid, NULL) != 0 ||
2077 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2078 NULL, &pool_name, &namelen) != 0) {
2080 * Cache and spare devices end up here - just ignore
2083 nvlist_destroy(nvl);
2088 * Create the pool if this is the first time we've seen it.
2090 spa = spa_find_by_guid(pool_guid);
2094 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2095 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2096 name = malloc(namelen + 1);
2098 nvlist_destroy(nvl);
2101 bcopy(pool_name, name, namelen);
2102 name[namelen] = '\0';
2103 spa = spa_create(pool_guid, name);
2106 nvlist_destroy(nvl);
2109 spa->spa_root_vdev->v_nchildren = vdev_children;
2111 if (pool_txg > spa->spa_txg)
2112 spa->spa_txg = pool_txg;
2115 * Get the vdev tree and create our in-core copy of it.
2116 * If we already have a vdev with this guid, this must
2117 * be some kind of alias (overlapping slices, dangerously dedicated
2120 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2121 NULL, &guid, NULL) != 0) {
2122 nvlist_destroy(nvl);
2125 vdev = vdev_find(guid);
2126 /* Has this vdev already been inited? */
2127 if (vdev && vdev->v_phys_read) {
2128 nvlist_destroy(nvl);
2132 rc = vdev_init_from_label(spa, nvl);
2133 nvlist_destroy(nvl);
2138 * We should already have created an incomplete vdev for this
2139 * vdev. Find it and initialise it with our read proc.
2141 vdev = vdev_find(guid);
2143 vdev->v_phys_read = _read;
2144 vdev->v_phys_write = _write;
2145 vdev->v_priv = priv;
2146 vdev->v_psize = vtmp.v_psize;
2148 * If no other state is set, mark vdev healthy.
2150 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2151 vdev->v_state = VDEV_STATE_HEALTHY;
2153 printf("ZFS: inconsistent nvlist contents\n");
2158 spa->spa_with_log = vdev->v_islog;
2161 * Re-evaluate top-level vdev state.
2163 vdev_set_state(vdev->v_top);
2166 * Ok, we are happy with the pool so far. Lets find
2167 * the best uberblock and then we can actually access
2168 * the contents of the pool.
2170 vdev_uberblock_load(vdev, spa->spa_uberblock);
2182 for (v = 0; v < 32; v++)
2189 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2192 zio_gbh_phys_t zio_gb;
2196 /* Artificial BP for gang block header. */
2198 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2199 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2200 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2201 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2202 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2203 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2205 /* Read gang header block using the artificial BP. */
2206 if (zio_read(spa, &gbh_bp, &zio_gb))
2210 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2211 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2213 if (BP_IS_HOLE(gbp))
2215 if (zio_read(spa, gbp, pbuf))
2217 pbuf += BP_GET_PSIZE(gbp);
2220 if (zio_checksum_verify(spa, bp, buf))
2226 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2228 int cpfunc = BP_GET_COMPRESS(bp);
2229 uint64_t align, size;
2234 * Process data embedded in block pointer
2236 if (BP_IS_EMBEDDED(bp)) {
2237 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2239 size = BPE_GET_PSIZE(bp);
2240 ASSERT(size <= BPE_PAYLOAD_SIZE);
2242 if (cpfunc != ZIO_COMPRESS_OFF)
2243 pbuf = malloc(size);
2250 decode_embedded_bp_compressed(bp, pbuf);
2253 if (cpfunc != ZIO_COMPRESS_OFF) {
2254 error = zio_decompress_data(cpfunc, pbuf,
2255 size, buf, BP_GET_LSIZE(bp));
2259 printf("ZFS: i/o error - unable to decompress "
2260 "block pointer data, error %d\n", error);
2266 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2267 const dva_t *dva = &bp->blk_dva[i];
2273 if (!dva->dva_word[0] && !dva->dva_word[1])
2276 vdevid = DVA_GET_VDEV(dva);
2277 offset = DVA_GET_OFFSET(dva);
2278 vlist = &spa->spa_root_vdev->v_children;
2279 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2280 if (vdev->v_id == vdevid)
2283 if (!vdev || !vdev->v_read)
2286 size = BP_GET_PSIZE(bp);
2287 if (vdev->v_read == vdev_raidz_read) {
2288 align = 1ULL << vdev->v_ashift;
2289 if (P2PHASE(size, align) != 0)
2290 size = P2ROUNDUP(size, align);
2292 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2293 pbuf = malloc(size);
2302 if (DVA_GET_GANG(dva))
2303 error = zio_read_gang(spa, bp, pbuf);
2305 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2307 if (cpfunc != ZIO_COMPRESS_OFF)
2308 error = zio_decompress_data(cpfunc, pbuf,
2309 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2310 else if (size != BP_GET_PSIZE(bp))
2311 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2313 printf("zio_read error: %d\n", error);
2321 printf("ZFS: i/o error - all block copies unavailable\n");
2327 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2328 void *buf, size_t buflen)
2330 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2331 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2332 int nlevels = dnode->dn_nlevels;
2335 if (bsize > SPA_MAXBLOCKSIZE) {
2336 printf("ZFS: I/O error - blocks larger than %llu are not "
2337 "supported\n", SPA_MAXBLOCKSIZE);
2342 * Handle odd block sizes, mirrors dmu_read_impl(). Data can't exist
2343 * past the first block, so we'll clip the read to the portion of the
2344 * buffer within bsize and zero out the remainder.
2346 if (dnode->dn_maxblkid == 0) {
2349 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset);
2350 bzero((char *)buf + newbuflen, buflen - newbuflen);
2355 * Note: bsize may not be a power of two here so we need to do an
2356 * actual divide rather than a bitshift.
2358 while (buflen > 0) {
2359 uint64_t bn = offset / bsize;
2360 int boff = offset % bsize;
2362 const blkptr_t *indbp;
2365 if (bn > dnode->dn_maxblkid)
2368 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2371 indbp = dnode->dn_blkptr;
2372 for (i = 0; i < nlevels; i++) {
2374 * Copy the bp from the indirect array so that
2375 * we can re-use the scratch buffer for multi-level
2378 ibn = bn >> ((nlevels - i - 1) * ibshift);
2379 ibn &= ((1 << ibshift) - 1);
2381 if (BP_IS_HOLE(&bp)) {
2382 memset(dnode_cache_buf, 0, bsize);
2385 rc = zio_read(spa, &bp, dnode_cache_buf);
2388 indbp = (const blkptr_t *) dnode_cache_buf;
2390 dnode_cache_obj = dnode;
2391 dnode_cache_bn = bn;
2395 * The buffer contains our data block. Copy what we
2396 * need from it and loop.
2399 if (i > buflen) i = buflen;
2400 memcpy(buf, &dnode_cache_buf[boff], i);
2401 buf = ((char *)buf) + i;
2410 * Lookup a value in a microzap directory.
2413 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2416 const mzap_ent_phys_t *mze;
2420 * Microzap objects use exactly one block. Read the whole
2423 chunks = size / MZAP_ENT_LEN - 1;
2424 for (i = 0; i < chunks; i++) {
2425 mze = &mz->mz_chunk[i];
2426 if (strcmp(mze->mze_name, name) == 0) {
2427 *value = mze->mze_value;
2436 * Compare a name with a zap leaf entry. Return non-zero if the name
2440 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2444 const zap_leaf_chunk_t *nc;
2447 namelen = zc->l_entry.le_name_numints;
2449 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2451 while (namelen > 0) {
2455 if (len > ZAP_LEAF_ARRAY_BYTES)
2456 len = ZAP_LEAF_ARRAY_BYTES;
2457 if (memcmp(p, nc->l_array.la_array, len))
2461 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2468 * Extract a uint64_t value from a zap leaf entry.
2471 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2473 const zap_leaf_chunk_t *vc;
2478 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2479 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2480 value = (value << 8) | p[i];
2487 stv(int len, void *addr, uint64_t value)
2491 *(uint8_t *)addr = value;
2494 *(uint16_t *)addr = value;
2497 *(uint32_t *)addr = value;
2500 *(uint64_t *)addr = value;
2506 * Extract a array from a zap leaf entry.
2509 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2510 uint64_t integer_size, uint64_t num_integers, void *buf)
2512 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2514 uint64_t *u64 = buf;
2516 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2517 int chunk = zc->l_entry.le_value_chunk;
2520 if (integer_size == 8 && len == 1) {
2521 *u64 = fzap_leaf_value(zl, zc);
2526 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2529 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2530 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2531 value = (value << 8) | la->la_array[i];
2533 if (byten == array_int_len) {
2534 stv(integer_size, p, value);
2542 chunk = la->la_next;
2547 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2550 switch (integer_size) {
2560 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2567 zap_leaf_free(zap_leaf_t *leaf)
2574 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2576 int bs = FZAP_BLOCK_SHIFT(zap);
2579 *lp = malloc(sizeof(**lp));
2584 (*lp)->l_phys = malloc(1 << bs);
2586 if ((*lp)->l_phys == NULL) {
2590 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2599 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2602 int bs = FZAP_BLOCK_SHIFT(zap);
2603 uint64_t blk = idx >> (bs - 3);
2604 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2608 buf = malloc(1 << zap->zap_block_shift);
2611 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2612 buf, 1 << zap->zap_block_shift);
2620 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2622 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2623 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2626 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2631 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2633 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2638 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2639 err = zap_idx_to_blk(zap, idx, &blk);
2642 return (zap_get_leaf_byblk(zap, blk, lp));
2645 #define CHAIN_END 0xffff /* end of the chunk chain */
2646 #define LEAF_HASH(l, h) \
2647 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2649 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2650 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2653 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2654 uint64_t integer_size, uint64_t num_integers, void *value)
2658 struct zap_leaf_entry *le;
2661 * Make sure this chunk matches our hash.
2663 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2664 zl->l_phys->l_hdr.lh_prefix !=
2665 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2669 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2670 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2671 zap_leaf_chunk_t *zc;
2672 uint16_t chunk = *chunkp;
2674 le = ZAP_LEAF_ENTRY(zl, chunk);
2675 if (le->le_hash != hash)
2677 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2678 if (fzap_name_equal(zl, zc, name)) {
2679 if (zc->l_entry.le_value_intlen > integer_size) {
2682 fzap_leaf_array(zl, zc, integer_size,
2683 num_integers, value);
2693 * Lookup a value in a fatzap directory.
2696 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2697 const char *name, uint64_t integer_size, uint64_t num_integers,
2700 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2706 if (zh->zap_magic != ZAP_MAGIC)
2709 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2713 z.zap_block_shift = ilog2(bsize);
2716 z.zap_dnode = dnode;
2718 hash = zap_hash(zh->zap_salt, name);
2719 rc = zap_deref_leaf(&z, hash, &zl);
2723 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2730 * Lookup a name in a zap object and return its value as a uint64_t.
2733 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2734 uint64_t integer_size, uint64_t num_integers, void *value)
2738 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2744 rc = dnode_read(spa, dnode, 0, zap, size);
2748 switch (zap->zap_block_type) {
2750 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2753 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2754 num_integers, value);
2757 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2758 zap->zap_block_type);
2767 * List a microzap directory.
2770 mzap_list(const mzap_phys_t *mz, size_t size,
2771 int (*callback)(const char *, uint64_t))
2773 const mzap_ent_phys_t *mze;
2777 * Microzap objects use exactly one block. Read the whole
2781 chunks = size / MZAP_ENT_LEN - 1;
2782 for (i = 0; i < chunks; i++) {
2783 mze = &mz->mz_chunk[i];
2784 if (mze->mze_name[0]) {
2785 rc = callback(mze->mze_name, mze->mze_value);
2795 * List a fatzap directory.
2798 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2799 int (*callback)(const char *, uint64_t))
2801 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2806 if (zh->zap_magic != ZAP_MAGIC)
2809 z.zap_block_shift = ilog2(bsize);
2813 * This assumes that the leaf blocks start at block 1. The
2814 * documentation isn't exactly clear on this.
2817 zl.l_bs = z.zap_block_shift;
2818 zl.l_phys = malloc(bsize);
2819 if (zl.l_phys == NULL)
2822 for (i = 0; i < zh->zap_num_leafs; i++) {
2823 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2827 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2832 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2833 zap_leaf_chunk_t *zc, *nc;
2836 zc = &ZAP_LEAF_CHUNK(&zl, j);
2837 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2839 namelen = zc->l_entry.le_name_numints;
2840 if (namelen > sizeof(name))
2841 namelen = sizeof(name);
2844 * Paste the name back together.
2846 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2848 while (namelen > 0) {
2851 if (len > ZAP_LEAF_ARRAY_BYTES)
2852 len = ZAP_LEAF_ARRAY_BYTES;
2853 memcpy(p, nc->l_array.la_array, len);
2856 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2860 * Assume the first eight bytes of the value are
2863 value = fzap_leaf_value(&zl, zc);
2865 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2866 rc = callback((const char *)name, value);
2878 static int zfs_printf(const char *name, uint64_t value __unused)
2881 printf("%s\n", name);
2887 * List a zap directory.
2890 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2893 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2900 rc = dnode_read(spa, dnode, 0, zap, size);
2902 if (zap->zap_block_type == ZBT_MICRO)
2903 rc = mzap_list((const mzap_phys_t *)zap, size,
2906 rc = fzap_list(spa, dnode, zap, zfs_printf);
2913 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2914 dnode_phys_t *dnode)
2918 offset = objnum * sizeof(dnode_phys_t);
2919 return dnode_read(spa, &os->os_meta_dnode, offset,
2920 dnode, sizeof(dnode_phys_t));
2924 * Lookup a name in a microzap directory.
2927 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2929 const mzap_ent_phys_t *mze;
2933 * Microzap objects use exactly one block. Read the whole
2936 chunks = size / MZAP_ENT_LEN - 1;
2937 for (i = 0; i < chunks; i++) {
2938 mze = &mz->mz_chunk[i];
2939 if (value == mze->mze_value) {
2940 strcpy(name, mze->mze_name);
2949 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2952 const zap_leaf_chunk_t *nc;
2955 namelen = zc->l_entry.le_name_numints;
2957 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2959 while (namelen > 0) {
2962 if (len > ZAP_LEAF_ARRAY_BYTES)
2963 len = ZAP_LEAF_ARRAY_BYTES;
2964 memcpy(p, nc->l_array.la_array, len);
2967 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2974 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2975 char *name, uint64_t value)
2977 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2982 if (zh->zap_magic != ZAP_MAGIC)
2985 z.zap_block_shift = ilog2(bsize);
2989 * This assumes that the leaf blocks start at block 1. The
2990 * documentation isn't exactly clear on this.
2993 zl.l_bs = z.zap_block_shift;
2994 zl.l_phys = malloc(bsize);
2995 if (zl.l_phys == NULL)
2998 for (i = 0; i < zh->zap_num_leafs; i++) {
2999 off_t off = ((off_t)(i + 1)) << zl.l_bs;
3001 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
3005 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
3006 zap_leaf_chunk_t *zc;
3008 zc = &ZAP_LEAF_CHUNK(&zl, j);
3009 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
3011 if (zc->l_entry.le_value_intlen != 8 ||
3012 zc->l_entry.le_value_numints != 1)
3015 if (fzap_leaf_value(&zl, zc) == value) {
3016 fzap_name_copy(&zl, zc, name);
3029 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3033 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3040 rc = dnode_read(spa, dnode, 0, zap, size);
3042 if (zap->zap_block_type == ZBT_MICRO)
3043 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3046 rc = fzap_rlookup(spa, dnode, zap, name, value);
3053 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3056 char component[256];
3057 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3058 dnode_phys_t child_dir_zap, snapnames_zap, dataset, dir, parent;
3060 dsl_dataset_phys_t *ds;
3063 boolean_t issnap = B_FALSE;
3065 p = &name[sizeof(name) - 1];
3068 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3069 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3072 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3073 dir_obj = ds->ds_dir_obj;
3074 if (ds->ds_snapnames_zapobj == 0)
3078 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3080 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3082 /* Actual loop condition. */
3083 parent_obj = dd->dd_parent_obj;
3084 if (parent_obj == 0)
3087 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3090 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3091 if (issnap == B_TRUE) {
3093 * The dataset we are looking up is a snapshot
3094 * the dir_obj is the parent already, we don't want
3095 * the grandparent just yet. Reset to the parent.
3097 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3098 /* Lookup the dataset to get the snapname ZAP */
3099 if (objset_get_dnode(spa, spa->spa_mos,
3100 dd->dd_head_dataset_obj, &dataset))
3102 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3103 if (objset_get_dnode(spa, spa->spa_mos,
3104 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3106 /* Get the name of the snapshot */
3107 if (zap_rlookup(spa, &snapnames_zap, component,
3110 len = strlen(component);
3112 memcpy(p, component, len);
3119 child_dir_zapobj = dd->dd_child_dir_zapobj;
3120 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3121 &child_dir_zap) != 0)
3123 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3126 len = strlen(component);
3128 memcpy(p, component, len);
3132 /* Actual loop iteration. */
3133 dir_obj = parent_obj;
3144 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3147 uint64_t dir_obj, child_dir_zapobj;
3148 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset;
3150 dsl_dataset_phys_t *ds;
3152 boolean_t issnap = B_FALSE;
3154 if (objset_get_dnode(spa, spa->spa_mos,
3155 DMU_POOL_DIRECTORY_OBJECT, &dir))
3157 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3163 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3165 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3169 /* Actual loop condition #1. */
3175 memcpy(element, p, q - p);
3176 element[q - p] = '\0';
3183 if (issnap == B_TRUE) {
3184 if (objset_get_dnode(spa, spa->spa_mos,
3185 dd->dd_head_dataset_obj, &dataset))
3187 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3188 if (objset_get_dnode(spa, spa->spa_mos,
3189 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3191 /* Actual loop condition #2. */
3192 if (zap_lookup(spa, &snapnames_zap, element,
3193 sizeof (dir_obj), 1, &dir_obj) != 0)
3197 } else if ((q = strchr(element, '@')) != NULL) {
3199 element[q - element] = '\0';
3202 child_dir_zapobj = dd->dd_child_dir_zapobj;
3203 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3204 &child_dir_zap) != 0)
3207 /* Actual loop condition #2. */
3208 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3213 *objnum = dd->dd_head_dataset_obj;
3219 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3221 uint64_t dir_obj, child_dir_zapobj;
3222 dnode_phys_t child_dir_zap, dir, dataset;
3223 dsl_dataset_phys_t *ds;
3226 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3227 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3230 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3231 dir_obj = ds->ds_dir_obj;
3233 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3234 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3237 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3239 child_dir_zapobj = dd->dd_child_dir_zapobj;
3240 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3241 &child_dir_zap) != 0) {
3242 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3246 return (zap_list(spa, &child_dir_zap) != 0);
3250 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3251 int (*callback)(const char *, uint64_t))
3253 uint64_t dir_obj, child_dir_zapobj;
3254 dnode_phys_t child_dir_zap, dir, dataset;
3255 dsl_dataset_phys_t *ds;
3261 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3263 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3266 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3267 dir_obj = ds->ds_dir_obj;
3269 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3271 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3274 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3276 child_dir_zapobj = dd->dd_child_dir_zapobj;
3277 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3280 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3284 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3287 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3291 if (zap->zap_block_type == ZBT_MICRO)
3292 err = mzap_list((const mzap_phys_t *)zap, size,
3295 err = fzap_list(spa, &child_dir_zap, zap, callback);
3306 * Find the object set given the object number of its dataset object
3307 * and return its details in *objset
3310 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3312 dnode_phys_t dataset;
3313 dsl_dataset_phys_t *ds;
3315 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3316 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3320 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3321 if (zio_read(spa, &ds->ds_bp, objset)) {
3322 printf("ZFS: can't read object set for dataset %ju\n",
3331 * Find the object set pointed to by the BOOTFS property or the root
3332 * dataset if there is none and return its details in *objset
3335 zfs_get_root(const spa_t *spa, uint64_t *objid)
3337 dnode_phys_t dir, propdir;
3338 uint64_t props, bootfs, root;
3343 * Start with the MOS directory object.
3345 if (objset_get_dnode(spa, spa->spa_mos,
3346 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3347 printf("ZFS: can't read MOS object directory\n");
3352 * Lookup the pool_props and see if we can find a bootfs.
3354 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3355 sizeof(props), 1, &props) == 0 &&
3356 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3357 zap_lookup(spa, &propdir, "bootfs",
3358 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3363 * Lookup the root dataset directory
3365 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3366 sizeof(root), 1, &root) ||
3367 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3368 printf("ZFS: can't find root dsl_dir\n");
3373 * Use the information from the dataset directory's bonus buffer
3374 * to find the dataset object and from that the object set itself.
3376 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3377 *objid = dd->dd_head_dataset_obj;
3382 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3388 * Find the root object set if not explicitly provided
3390 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3391 printf("ZFS: can't find root filesystem\n");
3395 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3396 printf("ZFS: can't open root filesystem\n");
3400 mount->rootobj = rootobj;
3406 * callback function for feature name checks.
3409 check_feature(const char *name, uint64_t value)
3415 if (name[0] == '\0')
3418 for (i = 0; features_for_read[i] != NULL; i++) {
3419 if (strcmp(name, features_for_read[i]) == 0)
3422 printf("ZFS: unsupported feature: %s\n", name);
3427 * Checks whether the MOS features that are active are supported.
3430 check_mos_features(const spa_t *spa)
3438 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3441 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3442 sizeof (objnum), 1, &objnum)) != 0) {
3444 * It is older pool without features. As we have already
3445 * tested the label, just return without raising the error.
3450 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3453 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3456 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3461 if (dnode_read(spa, &dir, 0, zap, size)) {
3466 if (zap->zap_block_type == ZBT_MICRO)
3467 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3469 rc = fzap_list(spa, &dir, zap, check_feature);
3476 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3484 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3486 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3487 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3491 if (dir.dn_bonuslen != sizeof (uint64_t))
3494 size = *(uint64_t *)DN_BONUS(&dir);
3499 rc = dnode_read(spa, &dir, 0, nv, size);
3505 *value = nvlist_import(nv, size);
3511 zfs_spa_init(spa_t *spa)
3513 struct uberblock checkpoint;
3515 uint64_t config_object;
3519 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3520 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3523 if (spa->spa_mos->os_type != DMU_OST_META) {
3524 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3528 if (objset_get_dnode(spa, &spa->spa_mos_master,
3529 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3530 printf("ZFS: failed to read pool %s directory object\n",
3534 /* this is allowed to fail, older pools do not have salt */
3535 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3536 sizeof (spa->spa_cksum_salt.zcs_bytes),
3537 spa->spa_cksum_salt.zcs_bytes);
3539 rc = check_mos_features(spa);
3541 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3545 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3546 sizeof (config_object), 1, &config_object);
3548 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3551 rc = load_nvlist(spa, config_object, &nvlist);
3555 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3556 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3558 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3559 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3560 sizeof(checkpoint));
3561 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3562 &spa->spa_mos_checkpoint)) {
3563 printf("ZFS: can not read checkpoint data.\n");
3569 * Update vdevs from MOS config. Note, we do skip encoding bytes
3570 * here. See also vdev_label_read_config().
3572 rc = vdev_init_from_nvlist(spa, nvlist);
3573 nvlist_destroy(nvlist);
3578 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3581 if (dn->dn_bonustype != DMU_OT_SA) {
3582 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3584 sb->st_mode = zp->zp_mode;
3585 sb->st_uid = zp->zp_uid;
3586 sb->st_gid = zp->zp_gid;
3587 sb->st_size = zp->zp_size;
3589 sa_hdr_phys_t *sahdrp;
3594 if (dn->dn_bonuslen != 0)
3595 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3597 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3598 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3601 size = BP_GET_LSIZE(bp);
3606 error = zio_read(spa, bp, buf);
3617 hdrsize = SA_HDR_SIZE(sahdrp);
3618 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3620 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3622 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3624 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3633 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3637 if (dn->dn_bonustype == DMU_OT_SA) {
3638 sa_hdr_phys_t *sahdrp = NULL;
3644 if (dn->dn_bonuslen != 0) {
3645 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3649 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3651 bp = DN_SPILL_BLKPTR(dn);
3653 size = BP_GET_LSIZE(bp);
3658 rc = zio_read(spa, bp, buf);
3665 hdrsize = SA_HDR_SIZE(sahdrp);
3666 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3667 memcpy(path, p, psize);
3672 * Second test is purely to silence bogus compiler
3673 * warning about accessing past the end of dn_bonus.
3675 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3676 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3677 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3679 rc = dnode_read(spa, dn, 0, path, psize);
3686 STAILQ_ENTRY(obj_list) entry;
3690 * Lookup a file and return its dnode.
3693 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3702 int symlinks_followed = 0;
3704 struct obj_list *entry, *tentry;
3705 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3708 if (mount->objset.os_type != DMU_OST_ZFS) {
3709 printf("ZFS: unexpected object set type %ju\n",
3710 (uintmax_t)mount->objset.os_type);
3714 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3718 * Get the root directory dnode.
3720 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3726 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3731 entry->objnum = objnum;
3732 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3734 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3740 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3749 while (*q != '\0' && *q != '/')
3753 if (p + 1 == q && p[0] == '.') {
3758 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3760 if (STAILQ_FIRST(&on_cache) ==
3761 STAILQ_LAST(&on_cache, obj_list, entry)) {
3765 entry = STAILQ_FIRST(&on_cache);
3766 STAILQ_REMOVE_HEAD(&on_cache, entry);
3768 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3771 if (q - p + 1 > sizeof(element)) {
3775 memcpy(element, p, q - p);
3779 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3781 if (!S_ISDIR(sb.st_mode)) {
3786 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3789 objnum = ZFS_DIRENT_OBJ(objnum);
3791 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3795 entry->objnum = objnum;
3796 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3797 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3802 * Check for symlink.
3804 rc = zfs_dnode_stat(spa, &dn, &sb);
3807 if (S_ISLNK(sb.st_mode)) {
3808 if (symlinks_followed > 10) {
3812 symlinks_followed++;
3815 * Read the link value and copy the tail of our
3816 * current path onto the end.
3818 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3822 strcpy(&path[sb.st_size], p);
3824 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3829 * Restart with the new path, starting either at
3830 * the root or at the parent depending whether or
3831 * not the link is relative.
3835 while (STAILQ_FIRST(&on_cache) !=
3836 STAILQ_LAST(&on_cache, obj_list, entry)) {
3837 entry = STAILQ_FIRST(&on_cache);
3838 STAILQ_REMOVE_HEAD(&on_cache, entry);
3842 entry = STAILQ_FIRST(&on_cache);
3843 STAILQ_REMOVE_HEAD(&on_cache, entry);
3846 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3852 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
3858 * Return either a cached copy of the bootenv, or read each of the vdev children
3859 * looking for the bootenv. Cache what's found and return the results. Returns 0
3860 * when benvp is filled in, and some errno when not.
3863 zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp)
3866 nvlist_t *benv = NULL;
3868 if (spa->spa_bootenv == NULL) {
3869 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
3871 benv = vdev_read_bootenv(vd);
3876 spa->spa_bootenv = benv;
3878 benv = spa->spa_bootenv;
3888 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
3891 zfs_set_bootenv_spa(spa_t *spa, nvlist_t *benv)
3895 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
3896 vdev_write_bootenv(vd, benv);
3899 spa->spa_bootenv = benv;
3904 * Get bootonce value by key. The bootonce <key, value> pair is removed from the
3905 * bootenv nvlist and the remaining nvlist is committed back to disk. This process
3906 * the bootonce flag since we've reached the point in the boot that we've 'used'
3907 * the BE. For chained boot scenarios, we may reach this point multiple times (but
3908 * only remove it and return 0 the first time).
3911 zfs_get_bootonce_spa(spa_t *spa, const char *key, char *buf, size_t size)
3914 char *result = NULL;
3915 int result_size, rv;
3917 if ((rv = zfs_get_bootenv_spa(spa, &benv)) != 0)
3920 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
3921 &result, &result_size)) == 0) {
3922 if (result_size == 0) {
3923 /* ignore empty string */
3925 } else if (buf != NULL) {
3926 size = MIN((size_t)result_size + 1, size);
3927 strlcpy(buf, result, size);
3929 (void)nvlist_remove(benv, key, DATA_TYPE_STRING);
3930 (void)zfs_set_bootenv_spa(spa, benv);