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 "com.klarasystems:vdev_zaps_v2",
137 "org.freebsd:zstd_compress",
138 "org.illumos:lz4_compress",
139 "org.illumos:sha512",
141 "org.open-zfs:large_blocks",
142 "org.openzfs:blake3",
143 "org.zfsonlinux:allocation_classes",
144 "org.zfsonlinux:large_dnode",
149 * List of all pools, chained through spa_link.
151 static spa_list_t zfs_pools;
153 static const dnode_phys_t *dnode_cache_obj;
154 static uint64_t dnode_cache_bn;
155 static char *dnode_cache_buf;
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,
163 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
165 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
167 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_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 *,
172 vdev_indirect_mapping_entry_phys_t *
173 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
174 uint64_t, uint64_t *);
179 STAILQ_INIT(&zfs_vdevs);
180 STAILQ_INIT(&zfs_pools);
182 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
191 nvlist_check_features_for_read(nvlist_t *nvl)
193 nvlist_t *features = NULL;
196 nv_string_t *nvp_name;
199 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
200 DATA_TYPE_NVLIST, NULL, &features, NULL);
203 break; /* Continue with checks */
206 return (0); /* All features are disabled */
209 return (rc); /* Error while reading nvlist */
212 data = (nvs_data_t *)features->nv_data;
213 nvp = &data->nvl_pair; /* first pair in nvlist */
215 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
218 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
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) {
230 printf("ZFS: unsupported feature: %.*s\n",
231 nvp_name->nv_size, nvp_name->nv_data);
234 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
236 nvlist_destroy(features);
242 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
243 off_t offset, size_t size)
248 if (vdev->v_phys_read == NULL)
252 psize = BP_GET_PSIZE(bp);
257 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
260 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
267 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
269 if (vdev->v_phys_write == NULL)
272 return (vdev->v_phys_write(vdev, offset, buf, size));
275 typedef struct remap_segment {
279 uint64_t rs_split_offset;
283 static remap_segment_t *
284 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
286 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
290 rs->rs_offset = offset;
291 rs->rs_asize = asize;
292 rs->rs_split_offset = split_offset;
298 vdev_indirect_mapping_t *
299 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
300 uint64_t mapping_object)
302 vdev_indirect_mapping_t *vim;
303 vdev_indirect_mapping_phys_t *vim_phys;
306 vim = calloc(1, sizeof (*vim));
310 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
311 if (vim->vim_dn == NULL) {
316 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
324 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
325 if (vim->vim_phys == NULL) {
331 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
332 *vim->vim_phys = *vim_phys;
334 vim->vim_objset = os;
335 vim->vim_object = mapping_object;
336 vim->vim_entries = NULL;
338 vim->vim_havecounts =
339 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
345 * Compare an offset with an indirect mapping entry; there are three
346 * possible scenarios:
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.
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.
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).
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.
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
376 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
378 const uint64_t *key = v_key;
379 const vdev_indirect_mapping_entry_phys_t *array_elem =
381 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
383 if (*key < src_offset) {
385 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
393 * Return array entry.
395 static vdev_indirect_mapping_entry_phys_t *
396 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
402 if (vim->vim_phys->vimp_num_entries == 0)
405 if (vim->vim_entries == NULL) {
408 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
409 size = vim->vim_phys->vimp_num_entries *
410 sizeof (*vim->vim_entries);
412 size = bsize / sizeof (*vim->vim_entries);
413 size *= sizeof (*vim->vim_entries);
415 vim->vim_entries = malloc(size);
416 if (vim->vim_entries == NULL)
418 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
419 offset = index * sizeof (*vim->vim_entries);
422 /* We have data in vim_entries */
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]);
429 offset = index * sizeof (*vim->vim_entries);
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,
437 /* Read error, invalidate vim_entries. */
438 free(vim->vim_entries);
439 vim->vim_entries = NULL;
442 index -= vim->vim_entry_offset;
443 return (&vim->vim_entries[index]);
447 * Returns the mapping entry for the given offset.
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.
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.
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.
465 static vdev_indirect_mapping_entry_phys_t *
466 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
469 ASSERT(vim->vim_phys->vimp_num_entries > 0);
471 vdev_indirect_mapping_entry_phys_t *entry;
473 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
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.
483 while (last >= base) {
484 mid = base + ((last - base) >> 1);
486 entry = vdev_indirect_mapping_entry(vim, mid);
489 result = dva_mapping_overlap_compare(&offset, entry);
493 } else if (result < 0) {
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.
508 * Finally, since we are doing an allocation, it is up to the caller to
509 * free the array allocated in this function.
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)
515 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
516 vdev_indirect_mapping_t *vim = vd->v_mapping;
517 uint64_t entries = 0;
519 vdev_indirect_mapping_entry_phys_t *first_mapping =
520 vdev_indirect_mapping_entry_for_offset(vim, offset);
521 ASSERT3P(first_mapping, !=, NULL);
523 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
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);
529 offset += inner_size;
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);
542 *copied_entries = entries;
544 return (duplicate_mappings);
548 vdev_lookup_top(spa_t *spa, uint64_t vdev)
553 vlist = &spa->spa_root_vdev->v_children;
554 STAILQ_FOREACH(rvd, vlist, v_childlink)
555 if (rvd->v_id == vdev)
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.
566 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
567 uint64_t size, void *arg)
571 indirect_vsd_t *iv = zio->io_vsd;
573 if (vd->v_read == vdev_indirect_read)
576 if (vd->v_read == vdev_mirror_read)
579 indirect_split_t *is =
580 malloc(offsetof(indirect_split_t, is_child[n]));
582 zio->io_error = ENOMEM;
585 bzero(is, offsetof(indirect_split_t, is_child[n]));
589 is->is_split_offset = split_offset;
590 is->is_target_offset = offset;
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.
599 if (vd->v_read == vdev_mirror_read) {
603 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
604 is->is_child[i++].ic_vdev = kid;
607 is->is_child[0].ic_vdev = vd;
610 list_insert_tail(&iv->iv_splits, is);
614 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
617 spa_t *spa = vd->v_spa;
621 list_create(&stack, sizeof (remap_segment_t),
622 offsetof(remap_segment_t, rs_node));
624 rs = rs_alloc(vd, offset, asize, 0);
626 printf("vdev_indirect_remap: out of memory.\n");
627 zio->io_error = ENOMEM;
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);
637 if (num_entries == 0)
638 zio->io_error = ENOMEM;
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);
651 if (dst_v->v_read == vdev_indirect_read) {
654 o = rs_alloc(dst_v, dst_offset + inner_offset,
655 inner_size, rs->rs_split_offset);
657 printf("vdev_indirect_remap: "
659 zio->io_error = ENOMEM;
663 list_insert_head(&stack, o);
665 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
666 dst_offset + inner_offset,
670 * vdev_indirect_gather_splits can have memory
671 * allocation error, we can not recover from it.
673 if (zio->io_error != 0)
675 rs->rs_offset += inner_size;
676 rs->rs_asize -= inner_size;
677 rs->rs_split_offset += inner_size;
682 if (zio->io_error != 0)
686 list_destroy(&stack);
690 vdev_indirect_map_free(zio_t *zio)
692 indirect_vsd_t *iv = zio->io_vsd;
693 indirect_split_t *is;
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];
700 list_remove(&iv->iv_splits, is);
707 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
708 off_t offset, size_t bytes)
711 spa_t *spa = vdev->v_spa;
713 indirect_split_t *first;
716 iv = calloc(1, sizeof(*iv));
720 list_create(&iv->iv_splits,
721 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
723 bzero(&zio, sizeof(zio));
725 zio.io_bp = (blkptr_t *)bp;
728 zio.io_offset = offset;
732 if (vdev->v_mapping == NULL) {
733 vdev_indirect_config_t *vic;
735 vic = &vdev->vdev_indirect_config;
736 vdev->v_mapping = vdev_indirect_mapping_open(spa,
737 spa->spa_mos, vic->vic_mapping_object);
740 vdev_indirect_remap(vdev, offset, bytes, &zio);
741 if (zio.io_error != 0)
742 return (zio.io_error);
744 first = list_head(&iv->iv_splits);
745 if (first->is_size == zio.io_size) {
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).
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.
760 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
761 zio.io_data, first->is_target_offset, bytes);
763 iv->iv_split_block = B_TRUE;
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().
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;
777 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
778 ptr + is->is_split_offset, is->is_target_offset,
781 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
787 vdev_indirect_map_free(&zio);
795 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
796 off_t offset, size_t bytes)
799 return (vdev_read_phys(vdev, bp, buf,
800 offset + VDEV_LABEL_START_SIZE, bytes));
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)
812 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
813 off_t offset, size_t bytes)
819 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
820 if (kid->v_state != VDEV_STATE_HEALTHY)
822 rc = kid->v_read(kid, bp, buf, offset, bytes);
831 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
832 off_t offset, size_t bytes)
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.
843 kid = STAILQ_FIRST(&vdev->v_children);
846 if (kid->v_state != VDEV_STATE_HEALTHY)
848 return (kid->v_read(kid, bp, buf, offset, bytes));
852 vdev_find(uint64_t guid)
856 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
857 if (vdev->v_guid == guid)
864 vdev_create(uint64_t guid, vdev_read_t *_read)
867 vdev_indirect_config_t *vic;
869 vdev = calloc(1, sizeof(vdev_t));
871 STAILQ_INIT(&vdev->v_children);
873 vdev->v_read = _read;
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.
881 vic = &vdev->vdev_indirect_config;
882 vic->vic_prev_indirect_vdev = UINT64_MAX;
883 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
891 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
893 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
896 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
898 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
900 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
902 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 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,
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;
922 vdev->v_islog = is_log != 0;
926 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
928 uint64_t id, ashift, asize, nparity;
935 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
937 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
942 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
943 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
945 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
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);
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;
965 vdev = vdev_create(guid, vdev_indirect_read);
967 vdev->v_state = VDEV_STATE_HEALTHY;
968 vic = &vdev->vdev_indirect_config;
971 ZPOOL_CONFIG_INDIRECT_OBJECT,
973 NULL, &vic->vic_mapping_object, NULL);
975 ZPOOL_CONFIG_INDIRECT_BIRTHS,
977 NULL, &vic->vic_births_object, NULL);
979 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
981 NULL, &vic->vic_prev_indirect_vdev, NULL);
983 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
984 vdev = vdev_create(guid, vdev_missing_read);
986 vdev = vdev_create(guid, vdev_disk_read);
992 vdev_set_initial_state(vdev, nvlist);
994 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
995 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
996 vdev->v_ashift = ashift;
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;
1004 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1005 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
1006 vdev->v_nparity = nparity;
1008 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1009 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1010 char prefix[] = "/dev/";
1012 len = strlen(prefix);
1013 if (len < pathlen && memcmp(path, prefix, len) == 0) {
1017 name = malloc(pathlen + 1);
1018 bcopy(path, name, pathlen);
1019 name[pathlen] = '\0';
1020 vdev->v_name = name;
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",
1030 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1031 vdev->v_nparity, id);
1033 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1035 vdev->v_name = name;
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.
1047 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1049 vdev_t *v, *previous;
1051 if (STAILQ_EMPTY(&top_vdev->v_children))
1055 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1056 if (v->v_id > vdev->v_id)
1059 if (v->v_id == vdev->v_id)
1062 if (v->v_id < vdev->v_id)
1069 vdev_child_count(vdev_t *vdev)
1075 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1082 * Insert vdev into top_vdev children list. List is ordered by v_id.
1085 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
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.
1097 previous = vdev_find_previous(top_vdev, vdev);
1099 if (previous == NULL) {
1100 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1101 } else if (previous->v_id == vdev->v_id) {
1103 * This vdev was configured from label config,
1104 * do not insert duplicate.
1108 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1112 count = vdev_child_count(top_vdev);
1113 if (top_vdev->v_nchildren < count)
1114 top_vdev->v_nchildren = count;
1118 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1120 vdev_t *top_vdev, *vdev;
1121 nvlist_t **kids = NULL;
1125 top_vdev = vdev_find(top_guid);
1126 if (top_vdev == NULL) {
1127 rc = vdev_init(top_guid, nvlist, &top_vdev);
1130 top_vdev->v_spa = spa;
1131 top_vdev->v_top = top_vdev;
1132 vdev_insert(spa->spa_root_vdev, top_vdev);
1135 /* Add children if there are any. */
1136 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1137 &nkids, &kids, NULL);
1139 for (int i = 0; i < nkids; i++) {
1142 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1143 DATA_TYPE_UINT64, NULL, &guid, NULL);
1147 rc = vdev_init(guid, kids[i], &vdev);
1152 vdev->v_top = top_vdev;
1153 vdev_insert(top_vdev, vdev);
1157 * When there are no children, nvlist_find() does return
1158 * error, reset it because leaf devices have no children.
1164 for (int i = 0; i < nkids; i++)
1165 nvlist_destroy(kids[i]);
1173 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1175 uint64_t pool_guid, top_guid;
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");
1189 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1190 nvlist_destroy(vdevs);
1195 vdev_set_state(vdev_t *vdev)
1201 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1202 vdev_set_state(kid);
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.
1210 if (STAILQ_FIRST(&vdev->v_children)) {
1213 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1214 if (kid->v_state == VDEV_STATE_HEALTHY)
1219 if (bad_kids == 0) {
1220 vdev->v_state = VDEV_STATE_HEALTHY;
1222 if (vdev->v_read == vdev_mirror_read) {
1224 vdev->v_state = VDEV_STATE_DEGRADED;
1226 vdev->v_state = VDEV_STATE_OFFLINE;
1228 } else if (vdev->v_read == vdev_raidz_read) {
1229 if (bad_kids > vdev->v_nparity) {
1230 vdev->v_state = VDEV_STATE_OFFLINE;
1232 vdev->v_state = VDEV_STATE_DEGRADED;
1240 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1243 nvlist_t **kids = NULL;
1246 /* Update top vdev. */
1247 vdev = vdev_find(top_guid);
1249 vdev_set_initial_state(vdev, nvlist);
1251 /* Update children if there are any. */
1252 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1253 &nkids, &kids, NULL);
1255 for (int i = 0; i < nkids; i++) {
1258 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1259 DATA_TYPE_UINT64, NULL, &guid, NULL);
1263 vdev = vdev_find(guid);
1265 vdev_set_initial_state(vdev, kids[i]);
1271 for (int i = 0; i < nkids; i++)
1272 nvlist_destroy(kids[i]);
1280 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1282 uint64_t pool_guid, vdev_children;
1283 nvlist_t *vdevs = NULL, **kids = NULL;
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");
1297 if (spa->spa_guid != pool_guid) {
1298 nvlist_destroy(vdevs);
1302 spa->spa_root_vdev->v_nchildren = vdev_children;
1304 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1305 &nkids, &kids, NULL);
1306 nvlist_destroy(vdevs);
1309 * MOS config has at least one child for root vdev.
1314 for (int i = 0; i < nkids; i++) {
1318 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1322 vdev = vdev_find(guid);
1324 * Top level vdev is missing, create it.
1327 rc = vdev_from_nvlist(spa, guid, kids[i]);
1329 rc = vdev_update_from_nvlist(guid, kids[i]);
1334 for (int i = 0; i < nkids; i++)
1335 nvlist_destroy(kids[i]);
1340 * Re-evaluate top-level vdev state.
1342 vdev_set_state(spa->spa_root_vdev);
1348 spa_find_by_guid(uint64_t guid)
1352 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1353 if (spa->spa_guid == guid)
1360 spa_find_by_name(const char *name)
1364 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1365 if (strcmp(spa->spa_name, name) == 0)
1372 spa_create(uint64_t guid, const char *name)
1376 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1378 if ((spa->spa_name = strdup(name)) == NULL) {
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);
1391 spa->spa_root_vdev->v_name = strdup("root");
1392 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1398 state_name(vdev_state_t state)
1400 static const char *names[] = {
1410 return (names[state]);
1415 #define pager_printf printf
1420 pager_printf(const char *fmt, ...)
1425 va_start(args, fmt);
1426 vsnprintf(line, sizeof(line), fmt, args);
1428 return (pager_output(line));
1433 #define STATUS_FORMAT " %s %s\n"
1436 print_state(int indent, const char *name, vdev_state_t state)
1442 for (i = 0; i < indent; i++)
1445 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1449 vdev_status(vdev_t *vdev, int indent)
1454 if (vdev->v_islog) {
1455 (void) pager_output(" logs\n");
1459 ret = print_state(indent, vdev->v_name, vdev->v_state);
1463 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1464 ret = vdev_status(kid, indent + 1);
1472 spa_status(spa_t *spa)
1474 static char bootfs[ZFS_MAXNAMELEN];
1478 int good_kids, bad_kids, degraded_kids, ret;
1481 ret = pager_printf(" pool: %s\n", spa->spa_name);
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);
1490 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1495 ret = pager_printf("config:\n\n");
1498 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1505 vlist = &spa->spa_root_vdev->v_children;
1506 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1507 if (vdev->v_state == VDEV_STATE_HEALTHY)
1509 else if (vdev->v_state == VDEV_STATE_DEGRADED)
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;
1521 ret = print_state(0, spa->spa_name, state);
1525 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1526 ret = vdev_status(vdev, 1);
1534 spa_all_status(void)
1537 int first = 1, ret = 0;
1539 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1541 ret = pager_printf("\n");
1546 ret = spa_status(spa);
1554 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1556 uint64_t label_offset;
1558 if (l < VDEV_LABELS / 2)
1561 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1563 return (offset + l * sizeof (vdev_label_t) + label_offset);
1567 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1569 unsigned int seq1 = 0;
1570 unsigned int seq2 = 0;
1571 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1576 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1580 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1581 seq1 = MMP_SEQ(ub1);
1583 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1584 seq2 = MMP_SEQ(ub2);
1586 return (AVL_CMP(seq1, seq2));
1590 uberblock_verify(uberblock_t *ub)
1592 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1593 byteswap_uint64_array(ub, sizeof (uberblock_t));
1596 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1597 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1604 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1610 off = vdev_label_offset(vd->v_psize, l, offset);
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);
1620 return (vdev_read_phys(vd, &bp, buf, off, size));
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.
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.
1637 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1639 uint64_t off, o_phys;
1641 size_t size = VDEV_PHYS_SIZE;
1644 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1645 off = vdev_label_offset(vd->v_psize, l, o_phys);
1647 /* off should be 8K from bootenv */
1648 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1655 /* Read vdev_phys */
1656 rc = vdev_label_read(vd, l, buf, o_phys, size);
1662 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1664 zio_checksum_info_t *ci;
1667 size_t size = VDEV_PAD_SIZE;
1670 if (vd->v_phys_write == NULL)
1673 off = vdev_label_offset(vd->v_psize, l, offset);
1675 rc = vdev_label_write_validate(vd, l, offset);
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;
1686 return (vdev_write_phys(vd, be, off, size));
1690 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1695 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1696 if (kid->v_state != VDEV_STATE_HEALTHY)
1698 rc = vdev_write_bootenv_impl(kid, be);
1704 * Non-leaf vdevs do not have v_phys_write.
1706 if (vdev->v_phys_write == NULL)
1709 for (int l = 0; l < VDEV_LABELS; l++) {
1710 rc = vdev_label_write(vdev, l, be,
1711 offsetof(vdev_label_t, vl_be));
1713 printf("failed to write bootenv to %s label %d: %d\n",
1714 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1722 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1724 vdev_boot_envblock_t *be;
1729 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1733 nvp = vdev_read_bootenv(vdev);
1735 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1737 nvlist_destroy(nvp);
1740 be = calloc(1, sizeof(*be));
1744 be->vbe_version = version;
1748 * If there is no envmap, we will just wipe bootenv.
1750 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1751 be->vbe_bootenv, NULL);
1756 nv.nv_header = nvl->nv_header;
1757 nv.nv_asize = nvl->nv_asize;
1758 nv.nv_size = nvl->nv_size;
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);
1772 be->vbe_version = htobe64(be->vbe_version);
1773 rv = vdev_write_bootenv_impl(vdev, be);
1780 * Read the bootenv area from pool label, return the nvlist from it.
1781 * We return from first successful read.
1784 vdev_read_bootenv(vdev_t *vdev)
1788 vdev_boot_envblock_t *be;
1793 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1794 if (kid->v_state != VDEV_STATE_HEALTHY)
1797 benv = vdev_read_bootenv(kid);
1802 be = malloc(sizeof (*be));
1807 for (int l = 0; l < VDEV_LABELS; l++) {
1808 rv = vdev_label_read(vdev, l, be,
1809 offsetof(vdev_label_t, vl_be),
1819 be->vbe_version = be64toh(be->vbe_version);
1820 switch (be->vbe_version) {
1823 * we have textual data in vbe_bootenv, create nvlist
1824 * with key "envmap".
1826 benv = nvlist_create(NV_UNIQUE_NAME);
1828 if (*be->vbe_bootenv == '\0') {
1829 nvlist_add_uint64(benv, BOOTENV_VERSION,
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);
1840 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1844 command = (char *)be;
1847 /* Check for legacy zfsbootcfg command string */
1848 for (int i = 0; command[i] != '\0'; i++) {
1849 if (iscntrl(command[i])) {
1856 benv = nvlist_create(NV_UNIQUE_NAME);
1859 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1862 nvlist_add_uint64(benv, BOOTENV_VERSION,
1872 vdev_get_label_asize(nvlist_t *nvl)
1881 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1882 NULL, &vdevs, NULL) != 0)
1886 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1887 * For raidz, the asize is raw size of all children.
1889 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1890 NULL, &type, &len) != 0)
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)
1898 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1899 NULL, &asize, NULL) != 0)
1902 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1906 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1907 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1913 for (int i = 0; i < nkids; i++)
1914 nvlist_destroy(kids[i]);
1918 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1920 nvlist_destroy(vdevs);
1925 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1928 uint64_t best_txg = 0;
1929 uint64_t label_txg = 0;
1931 nvlist_t *nvl = NULL, *tmp;
1934 label = malloc(sizeof (vdev_phys_t));
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)))
1944 tmp = nvlist_import(label->vp_nvlist,
1945 sizeof(label->vp_nvlist));
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);
1957 if (label_txg <= txg && label_txg > best_txg) {
1958 best_txg = label_txg;
1959 nvlist_destroy(nvl);
1964 * Use asize from pool config. We need this
1965 * because we can get bad value from BIOS.
1967 asize = vdev_get_label_asize(nvl);
1969 vd->v_psize = asize;
1972 nvlist_destroy(tmp);
1975 if (best_txg == 0) {
1976 nvlist_destroy(nvl);
1985 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1989 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
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)))
1999 if (uberblock_verify(buf) != 0)
2002 if (vdev_uberblock_compare(buf, ub) > 0)
2010 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2018 uint64_t guid, vdev_children;
2019 uint64_t pool_txg, pool_guid;
2020 const char *pool_name;
2024 * Load the vdev label and figure out which
2025 * uberblock is most current.
2027 memset(&vtmp, 0, sizeof(vtmp));
2028 vtmp.v_phys_read = _read;
2029 vtmp.v_phys_write = _write;
2031 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2032 (uint64_t)sizeof (vdev_label_t));
2034 /* Test for minimum device size. */
2035 if (vtmp.v_psize < SPA_MINDEVSIZE)
2038 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2042 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2043 NULL, &val, NULL) != 0) {
2044 nvlist_destroy(nvl);
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);
2055 /* Check ZFS features for read */
2056 rc = nvlist_check_features_for_read(nvl);
2058 nvlist_destroy(nvl);
2062 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2063 NULL, &val, NULL) != 0) {
2064 nvlist_destroy(nvl);
2068 if (val == POOL_STATE_DESTROYED) {
2069 /* We don't boot only from destroyed pools. */
2070 nvlist_destroy(nvl);
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) {
2081 * Cache and spare devices end up here - just ignore
2084 nvlist_destroy(nvl);
2089 * Create the pool if this is the first time we've seen it.
2091 spa = spa_find_by_guid(pool_guid);
2095 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2096 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2097 name = malloc(namelen + 1);
2099 nvlist_destroy(nvl);
2102 bcopy(pool_name, name, namelen);
2103 name[namelen] = '\0';
2104 spa = spa_create(pool_guid, name);
2107 nvlist_destroy(nvl);
2110 spa->spa_root_vdev->v_nchildren = vdev_children;
2112 if (pool_txg > spa->spa_txg)
2113 spa->spa_txg = pool_txg;
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
2121 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2122 NULL, &guid, NULL) != 0) {
2123 nvlist_destroy(nvl);
2126 vdev = vdev_find(guid);
2127 /* Has this vdev already been inited? */
2128 if (vdev && vdev->v_phys_read) {
2129 nvlist_destroy(nvl);
2133 rc = vdev_init_from_label(spa, nvl);
2134 nvlist_destroy(nvl);
2139 * We should already have created an incomplete vdev for this
2140 * vdev. Find it and initialise it with our read proc.
2142 vdev = vdev_find(guid);
2144 vdev->v_phys_read = _read;
2145 vdev->v_phys_write = _write;
2146 vdev->v_priv = priv;
2147 vdev->v_psize = vtmp.v_psize;
2149 * If no other state is set, mark vdev healthy.
2151 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2152 vdev->v_state = VDEV_STATE_HEALTHY;
2154 printf("ZFS: inconsistent nvlist contents\n");
2159 spa->spa_with_log = vdev->v_islog;
2162 * Re-evaluate top-level vdev state.
2164 vdev_set_state(vdev->v_top);
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.
2171 vdev_uberblock_load(vdev, spa->spa_uberblock);
2183 for (v = 0; v < 32; v++)
2190 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2193 zio_gbh_phys_t zio_gb;
2197 /* Artificial BP for gang block header. */
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);
2206 /* Read gang header block using the artificial BP. */
2207 if (zio_read(spa, &gbh_bp, &zio_gb))
2211 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2212 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2214 if (BP_IS_HOLE(gbp))
2216 if (zio_read(spa, gbp, pbuf))
2218 pbuf += BP_GET_PSIZE(gbp);
2221 if (zio_checksum_verify(spa, bp, buf))
2227 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2229 int cpfunc = BP_GET_COMPRESS(bp);
2230 uint64_t align, size;
2235 * Process data embedded in block pointer
2237 if (BP_IS_EMBEDDED(bp)) {
2238 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2240 size = BPE_GET_PSIZE(bp);
2241 ASSERT(size <= BPE_PAYLOAD_SIZE);
2243 if (cpfunc != ZIO_COMPRESS_OFF)
2244 pbuf = malloc(size);
2251 decode_embedded_bp_compressed(bp, pbuf);
2254 if (cpfunc != ZIO_COMPRESS_OFF) {
2255 error = zio_decompress_data(cpfunc, pbuf,
2256 size, buf, BP_GET_LSIZE(bp));
2260 printf("ZFS: i/o error - unable to decompress "
2261 "block pointer data, error %d\n", error);
2267 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2268 const dva_t *dva = &bp->blk_dva[i];
2274 if (!dva->dva_word[0] && !dva->dva_word[1])
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)
2284 if (!vdev || !vdev->v_read)
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);
2293 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2294 pbuf = malloc(size);
2303 if (DVA_GET_GANG(dva))
2304 error = zio_read_gang(spa, bp, pbuf);
2306 error = vdev->v_read(vdev, bp, pbuf, offset, size);
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));
2314 printf("zio_read error: %d\n", error);
2322 printf("ZFS: i/o error - all block copies unavailable\n");
2328 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2329 void *buf, size_t buflen)
2331 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2332 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2333 int nlevels = dnode->dn_nlevels;
2336 if (bsize > SPA_MAXBLOCKSIZE) {
2337 printf("ZFS: I/O error - blocks larger than %llu are not "
2338 "supported\n", SPA_MAXBLOCKSIZE);
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.
2347 if (dnode->dn_maxblkid == 0) {
2350 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset);
2351 bzero((char *)buf + newbuflen, buflen - newbuflen);
2356 * Note: bsize may not be a power of two here so we need to do an
2357 * actual divide rather than a bitshift.
2359 while (buflen > 0) {
2360 uint64_t bn = offset / bsize;
2361 int boff = offset % bsize;
2363 const blkptr_t *indbp;
2366 if (bn > dnode->dn_maxblkid)
2369 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2372 indbp = dnode->dn_blkptr;
2373 for (i = 0; i < nlevels; i++) {
2375 * Copy the bp from the indirect array so that
2376 * we can re-use the scratch buffer for multi-level
2379 ibn = bn >> ((nlevels - i - 1) * ibshift);
2380 ibn &= ((1 << ibshift) - 1);
2382 if (BP_IS_HOLE(&bp)) {
2383 memset(dnode_cache_buf, 0, bsize);
2386 rc = zio_read(spa, &bp, dnode_cache_buf);
2389 indbp = (const blkptr_t *) dnode_cache_buf;
2391 dnode_cache_obj = dnode;
2392 dnode_cache_bn = bn;
2396 * The buffer contains our data block. Copy what we
2397 * need from it and loop.
2400 if (i > buflen) i = buflen;
2401 memcpy(buf, &dnode_cache_buf[boff], i);
2402 buf = ((char *)buf) + i;
2411 * Lookup a value in a microzap directory.
2414 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2417 const mzap_ent_phys_t *mze;
2421 * Microzap objects use exactly one block. Read the whole
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;
2437 * Compare a name with a zap leaf entry. Return non-zero if the name
2441 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2445 const zap_leaf_chunk_t *nc;
2448 namelen = zc->l_entry.le_name_numints;
2450 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2452 while (namelen > 0) {
2456 if (len > ZAP_LEAF_ARRAY_BYTES)
2457 len = ZAP_LEAF_ARRAY_BYTES;
2458 if (memcmp(p, nc->l_array.la_array, len))
2462 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2469 * Extract a uint64_t value from a zap leaf entry.
2472 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2474 const zap_leaf_chunk_t *vc;
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];
2488 stv(int len, void *addr, uint64_t value)
2492 *(uint8_t *)addr = value;
2495 *(uint16_t *)addr = value;
2498 *(uint32_t *)addr = value;
2501 *(uint64_t *)addr = value;
2507 * Extract a array from a zap leaf entry.
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)
2513 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2515 uint64_t *u64 = buf;
2517 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2518 int chunk = zc->l_entry.le_value_chunk;
2521 if (integer_size == 8 && len == 1) {
2522 *u64 = fzap_leaf_value(zl, zc);
2527 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
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];
2534 if (byten == array_int_len) {
2535 stv(integer_size, p, value);
2543 chunk = la->la_next;
2548 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2551 switch (integer_size) {
2561 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2568 zap_leaf_free(zap_leaf_t *leaf)
2575 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2577 int bs = FZAP_BLOCK_SHIFT(zap);
2580 *lp = malloc(sizeof(**lp));
2585 (*lp)->l_phys = malloc(1 << bs);
2587 if ((*lp)->l_phys == NULL) {
2591 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2600 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2603 int bs = FZAP_BLOCK_SHIFT(zap);
2604 uint64_t blk = idx >> (bs - 3);
2605 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2609 buf = malloc(1 << zap->zap_block_shift);
2612 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2613 buf, 1 << zap->zap_block_shift);
2621 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2623 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2624 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2627 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2632 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2634 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2639 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2640 err = zap_idx_to_blk(zap, idx, &blk);
2643 return (zap_get_leaf_byblk(zap, blk, lp));
2646 #define CHAIN_END 0xffff /* end of the chunk chain */
2647 #define LEAF_HASH(l, h) \
2648 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
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)])
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)
2659 struct zap_leaf_entry *le;
2662 * Make sure this chunk matches our hash.
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))
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;
2675 le = ZAP_LEAF_ENTRY(zl, chunk);
2676 if (le->le_hash != hash)
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) {
2683 fzap_leaf_array(zl, zc, integer_size,
2684 num_integers, value);
2694 * Lookup a value in a fatzap directory.
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,
2701 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2707 if (zh->zap_magic != ZAP_MAGIC)
2710 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2714 z.zap_block_shift = ilog2(bsize);
2717 z.zap_dnode = dnode;
2719 hash = zap_hash(zh->zap_salt, name);
2720 rc = zap_deref_leaf(&z, hash, &zl);
2724 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2731 * Lookup a name in a zap object and return its value as a uint64_t.
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)
2739 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2745 rc = dnode_read(spa, dnode, 0, zap, size);
2749 switch (zap->zap_block_type) {
2751 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2754 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2755 num_integers, value);
2758 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2759 zap->zap_block_type);
2768 * List a microzap directory.
2771 mzap_list(const mzap_phys_t *mz, size_t size,
2772 int (*callback)(const char *, uint64_t))
2774 const mzap_ent_phys_t *mze;
2778 * Microzap objects use exactly one block. Read the whole
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);
2796 * List a fatzap directory.
2799 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2800 int (*callback)(const char *, uint64_t))
2802 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2807 if (zh->zap_magic != ZAP_MAGIC)
2810 z.zap_block_shift = ilog2(bsize);
2814 * This assumes that the leaf blocks start at block 1. The
2815 * documentation isn't exactly clear on this.
2818 zl.l_bs = z.zap_block_shift;
2819 zl.l_phys = malloc(bsize);
2820 if (zl.l_phys == NULL)
2823 for (i = 0; i < zh->zap_num_leafs; i++) {
2824 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2828 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2833 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2834 zap_leaf_chunk_t *zc, *nc;
2837 zc = &ZAP_LEAF_CHUNK(&zl, j);
2838 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2840 namelen = zc->l_entry.le_name_numints;
2841 if (namelen > sizeof(name))
2842 namelen = sizeof(name);
2845 * Paste the name back together.
2847 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2849 while (namelen > 0) {
2852 if (len > ZAP_LEAF_ARRAY_BYTES)
2853 len = ZAP_LEAF_ARRAY_BYTES;
2854 memcpy(p, nc->l_array.la_array, len);
2857 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2861 * Assume the first eight bytes of the value are
2864 value = fzap_leaf_value(&zl, zc);
2866 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2867 rc = callback((const char *)name, value);
2879 static int zfs_printf(const char *name, uint64_t value __unused)
2882 printf("%s\n", name);
2888 * List a zap directory.
2891 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2894 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2901 rc = dnode_read(spa, dnode, 0, zap, size);
2903 if (zap->zap_block_type == ZBT_MICRO)
2904 rc = mzap_list((const mzap_phys_t *)zap, size,
2907 rc = fzap_list(spa, dnode, zap, zfs_printf);
2914 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2915 dnode_phys_t *dnode)
2919 offset = objnum * sizeof(dnode_phys_t);
2920 return dnode_read(spa, &os->os_meta_dnode, offset,
2921 dnode, sizeof(dnode_phys_t));
2925 * Lookup a name in a microzap directory.
2928 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2930 const mzap_ent_phys_t *mze;
2934 * Microzap objects use exactly one block. Read the whole
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);
2950 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2953 const zap_leaf_chunk_t *nc;
2956 namelen = zc->l_entry.le_name_numints;
2958 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2960 while (namelen > 0) {
2963 if (len > ZAP_LEAF_ARRAY_BYTES)
2964 len = ZAP_LEAF_ARRAY_BYTES;
2965 memcpy(p, nc->l_array.la_array, len);
2968 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2975 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2976 char *name, uint64_t value)
2978 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2983 if (zh->zap_magic != ZAP_MAGIC)
2986 z.zap_block_shift = ilog2(bsize);
2990 * This assumes that the leaf blocks start at block 1. The
2991 * documentation isn't exactly clear on this.
2994 zl.l_bs = z.zap_block_shift;
2995 zl.l_phys = malloc(bsize);
2996 if (zl.l_phys == NULL)
2999 for (i = 0; i < zh->zap_num_leafs; i++) {
3000 off_t off = ((off_t)(i + 1)) << zl.l_bs;
3002 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
3006 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
3007 zap_leaf_chunk_t *zc;
3009 zc = &ZAP_LEAF_CHUNK(&zl, j);
3010 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
3012 if (zc->l_entry.le_value_intlen != 8 ||
3013 zc->l_entry.le_value_numints != 1)
3016 if (fzap_leaf_value(&zl, zc) == value) {
3017 fzap_name_copy(&zl, zc, name);
3030 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3034 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3041 rc = dnode_read(spa, dnode, 0, zap, size);
3043 if (zap->zap_block_type == ZBT_MICRO)
3044 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3047 rc = fzap_rlookup(spa, dnode, zap, name, value);
3054 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
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;
3061 dsl_dataset_phys_t *ds;
3064 boolean_t issnap = B_FALSE;
3066 p = &name[sizeof(name) - 1];
3069 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3070 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3073 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3074 dir_obj = ds->ds_dir_obj;
3075 if (ds->ds_snapnames_zapobj == 0)
3079 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3081 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3083 /* Actual loop condition. */
3084 parent_obj = dd->dd_parent_obj;
3085 if (parent_obj == 0)
3088 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3091 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3092 if (issnap == B_TRUE) {
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.
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))
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)
3107 /* Get the name of the snapshot */
3108 if (zap_rlookup(spa, &snapnames_zap, component,
3111 len = strlen(component);
3113 memcpy(p, component, len);
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)
3124 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3127 len = strlen(component);
3129 memcpy(p, component, len);
3133 /* Actual loop iteration. */
3134 dir_obj = parent_obj;
3145 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3148 uint64_t dir_obj, child_dir_zapobj;
3149 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset;
3151 dsl_dataset_phys_t *ds;
3153 boolean_t issnap = B_FALSE;
3155 if (objset_get_dnode(spa, spa->spa_mos,
3156 DMU_POOL_DIRECTORY_OBJECT, &dir))
3158 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3164 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3166 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3170 /* Actual loop condition #1. */
3176 memcpy(element, p, q - p);
3177 element[q - p] = '\0';
3184 if (issnap == B_TRUE) {
3185 if (objset_get_dnode(spa, spa->spa_mos,
3186 dd->dd_head_dataset_obj, &dataset))
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)
3192 /* Actual loop condition #2. */
3193 if (zap_lookup(spa, &snapnames_zap, element,
3194 sizeof (dir_obj), 1, &dir_obj) != 0)
3198 } else if ((q = strchr(element, '@')) != NULL) {
3200 element[q - element] = '\0';
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)
3208 /* Actual loop condition #2. */
3209 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3214 *objnum = dd->dd_head_dataset_obj;
3220 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3222 uint64_t dir_obj, child_dir_zapobj;
3223 dnode_phys_t child_dir_zap, dir, dataset;
3224 dsl_dataset_phys_t *ds;
3227 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3228 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3231 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3232 dir_obj = ds->ds_dir_obj;
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);
3238 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
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);
3247 return (zap_list(spa, &child_dir_zap) != 0);
3251 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3252 int (*callback)(const char *, uint64_t))
3254 uint64_t dir_obj, child_dir_zapobj;
3255 dnode_phys_t child_dir_zap, dir, dataset;
3256 dsl_dataset_phys_t *ds;
3262 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3264 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3267 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3268 dir_obj = ds->ds_dir_obj;
3270 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3272 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3275 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3277 child_dir_zapobj = dd->dd_child_dir_zapobj;
3278 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3281 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3285 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3288 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3292 if (zap->zap_block_type == ZBT_MICRO)
3293 err = mzap_list((const mzap_phys_t *)zap, size,
3296 err = fzap_list(spa, &child_dir_zap, zap, callback);
3307 * Find the object set given the object number of its dataset object
3308 * and return its details in *objset
3311 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3313 dnode_phys_t dataset;
3314 dsl_dataset_phys_t *ds;
3316 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3317 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
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",
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
3336 zfs_get_root(const spa_t *spa, uint64_t *objid)
3338 dnode_phys_t dir, propdir;
3339 uint64_t props, bootfs, root;
3344 * Start with the MOS directory object.
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");
3353 * Lookup the pool_props and see if we can find a bootfs.
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) {
3364 * Lookup the root dataset directory
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");
3374 * Use the information from the dataset directory's bonus buffer
3375 * to find the dataset object and from that the object set itself.
3377 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3378 *objid = dd->dd_head_dataset_obj;
3383 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3389 * Find the root object set if not explicitly provided
3391 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3392 printf("ZFS: can't find root filesystem\n");
3396 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3397 printf("ZFS: can't open root filesystem\n");
3401 mount->rootobj = rootobj;
3407 * callback function for feature name checks.
3410 check_feature(const char *name, uint64_t value)
3416 if (name[0] == '\0')
3419 for (i = 0; features_for_read[i] != NULL; i++) {
3420 if (strcmp(name, features_for_read[i]) == 0)
3423 printf("ZFS: unsupported feature: %s\n", name);
3428 * Checks whether the MOS features that are active are supported.
3431 check_mos_features(const spa_t *spa)
3439 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3442 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3443 sizeof (objnum), 1, &objnum)) != 0) {
3445 * It is older pool without features. As we have already
3446 * tested the label, just return without raising the error.
3451 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3454 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3457 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3462 if (dnode_read(spa, &dir, 0, zap, size)) {
3467 if (zap->zap_block_type == ZBT_MICRO)
3468 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3470 rc = fzap_list(spa, &dir, zap, check_feature);
3477 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3485 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3487 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3488 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3492 if (dir.dn_bonuslen != sizeof (uint64_t))
3495 size = *(uint64_t *)DN_BONUS(&dir);
3500 rc = dnode_read(spa, &dir, 0, nv, size);
3506 *value = nvlist_import(nv, size);
3512 zfs_spa_init(spa_t *spa)
3514 struct uberblock checkpoint;
3516 uint64_t config_object;
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);
3524 if (spa->spa_mos->os_type != DMU_OST_META) {
3525 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
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",
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);
3540 rc = check_mos_features(spa);
3542 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3546 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3547 sizeof (config_object), 1, &config_object);
3549 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3552 rc = load_nvlist(spa, config_object, &nvlist);
3556 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3557 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
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");
3570 * Update vdevs from MOS config. Note, we do skip encoding bytes
3571 * here. See also vdev_label_read_config().
3573 rc = vdev_init_from_nvlist(spa, nvlist);
3574 nvlist_destroy(nvlist);
3579 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3582 if (dn->dn_bonustype != DMU_OT_SA) {
3583 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
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;
3590 sa_hdr_phys_t *sahdrp;
3595 if (dn->dn_bonuslen != 0)
3596 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3598 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3599 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3602 size = BP_GET_LSIZE(bp);
3607 error = zio_read(spa, bp, buf);
3618 hdrsize = SA_HDR_SIZE(sahdrp);
3619 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3621 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3623 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3625 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3634 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3638 if (dn->dn_bonustype == DMU_OT_SA) {
3639 sa_hdr_phys_t *sahdrp = NULL;
3645 if (dn->dn_bonuslen != 0) {
3646 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3650 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3652 bp = DN_SPILL_BLKPTR(dn);
3654 size = BP_GET_LSIZE(bp);
3659 rc = zio_read(spa, bp, buf);
3666 hdrsize = SA_HDR_SIZE(sahdrp);
3667 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3668 memcpy(path, p, psize);
3673 * Second test is purely to silence bogus compiler
3674 * warning about accessing past the end of dn_bonus.
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);
3680 rc = dnode_read(spa, dn, 0, path, psize);
3687 STAILQ_ENTRY(obj_list) entry;
3691 * Lookup a file and return its dnode.
3694 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3703 int symlinks_followed = 0;
3705 struct obj_list *entry, *tentry;
3706 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
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);
3715 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3719 * Get the root directory dnode.
3721 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3727 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3732 entry->objnum = objnum;
3733 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3735 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3741 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3750 while (*q != '\0' && *q != '/')
3754 if (p + 1 == q && p[0] == '.') {
3759 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3761 if (STAILQ_FIRST(&on_cache) ==
3762 STAILQ_LAST(&on_cache, obj_list, entry)) {
3766 entry = STAILQ_FIRST(&on_cache);
3767 STAILQ_REMOVE_HEAD(&on_cache, entry);
3769 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3772 if (q - p + 1 > sizeof(element)) {
3776 memcpy(element, p, q - p);
3780 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3782 if (!S_ISDIR(sb.st_mode)) {
3787 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3790 objnum = ZFS_DIRENT_OBJ(objnum);
3792 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3796 entry->objnum = objnum;
3797 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3798 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3803 * Check for symlink.
3805 rc = zfs_dnode_stat(spa, &dn, &sb);
3808 if (S_ISLNK(sb.st_mode)) {
3809 if (symlinks_followed > 10) {
3813 symlinks_followed++;
3816 * Read the link value and copy the tail of our
3817 * current path onto the end.
3819 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3823 strcpy(&path[sb.st_size], p);
3825 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
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.
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);
3843 entry = STAILQ_FIRST(&on_cache);
3844 STAILQ_REMOVE_HEAD(&on_cache, entry);
3847 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3853 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
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.
3864 zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp)
3867 nvlist_t *benv = NULL;
3869 if (spa->spa_bootenv == NULL) {
3870 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
3872 benv = vdev_read_bootenv(vd);
3877 spa->spa_bootenv = benv;
3879 benv = spa->spa_bootenv;
3889 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
3892 zfs_set_bootenv_spa(spa_t *spa, nvlist_t *benv)
3896 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
3897 vdev_write_bootenv(vd, benv);
3900 spa->spa_bootenv = benv;
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).
3912 zfs_get_bootonce_spa(spa_t *spa, const char *key, char *buf, size_t size)
3915 char *result = NULL;
3916 int result_size, rv;
3918 if ((rv = zfs_get_bootenv_spa(spa, &benv)) != 0)
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 */
3926 } else if (buf != NULL) {
3927 size = MIN((size_t)result_size + 1, size);
3928 strlcpy(buf, result, size);
3930 (void)nvlist_remove(benv, key, DATA_TYPE_STRING);
3931 (void)zfs_set_bootenv_spa(spa, benv);