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 static struct zfsmount zfsmount __unused;
57 * The indirect_child_t represents the vdev that we will read from, when we
58 * need to read all copies of the data (e.g. for scrub or reconstruction).
59 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
60 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
61 * ic_vdev is a child of the mirror.
63 typedef struct indirect_child {
69 * The indirect_split_t represents one mapped segment of an i/o to the
70 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
71 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
72 * For split blocks, there will be several of these.
74 typedef struct indirect_split {
75 list_node_t is_node; /* link on iv_splits */
78 * is_split_offset is the offset into the i/o.
79 * This is the sum of the previous splits' is_size's.
81 uint64_t is_split_offset;
83 vdev_t *is_vdev; /* top-level vdev */
84 uint64_t is_target_offset; /* offset on is_vdev */
86 int is_children; /* number of entries in is_child[] */
89 * is_good_child is the child that we are currently using to
90 * attempt reconstruction.
94 indirect_child_t is_child[1]; /* variable-length */
98 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
99 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
101 typedef struct indirect_vsd {
102 boolean_t iv_split_block;
103 boolean_t iv_reconstruct;
105 list_t iv_splits; /* list of indirect_split_t's */
109 * List of all vdevs, chained through v_alllink.
111 static vdev_list_t zfs_vdevs;
114 * List of ZFS features supported for read
116 static const char *features_for_read[] = {
117 "org.illumos:lz4_compress",
118 "com.delphix:hole_birth",
119 "com.delphix:extensible_dataset",
120 "com.delphix:embedded_data",
121 "org.open-zfs:large_blocks",
122 "org.illumos:sha512",
124 "org.zfsonlinux:large_dnode",
125 "com.joyent:multi_vdev_crash_dump",
126 "com.delphix:spacemap_histogram",
127 "com.delphix:zpool_checkpoint",
128 "com.delphix:spacemap_v2",
129 "com.datto:encryption",
130 "com.datto:bookmark_v2",
131 "org.zfsonlinux:allocation_classes",
132 "com.datto:resilver_defer",
133 "com.delphix:device_removal",
134 "com.delphix:obsolete_counts",
135 "com.intel:allocation_classes",
136 "org.freebsd:zstd_compress",
137 "com.delphix:bookmark_written",
142 * List of all pools, chained through spa_link.
144 static spa_list_t zfs_pools;
146 static const dnode_phys_t *dnode_cache_obj;
147 static uint64_t dnode_cache_bn;
148 static char *dnode_cache_buf;
150 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
151 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
152 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
153 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
154 const char *name, uint64_t integer_size, uint64_t num_integers,
156 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
158 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
160 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
162 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
163 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
165 vdev_indirect_mapping_entry_phys_t *
166 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
167 uint64_t, uint64_t *);
172 STAILQ_INIT(&zfs_vdevs);
173 STAILQ_INIT(&zfs_pools);
175 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
184 nvlist_check_features_for_read(nvlist_t *nvl)
186 nvlist_t *features = NULL;
189 nv_string_t *nvp_name;
192 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
193 DATA_TYPE_NVLIST, NULL, &features, NULL);
197 data = (nvs_data_t *)features->nv_data;
198 nvp = &data->nvl_pair; /* first pair in nvlist */
200 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
203 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
206 for (i = 0; features_for_read[i] != NULL; i++) {
207 if (memcmp(nvp_name->nv_data, features_for_read[i],
208 nvp_name->nv_size) == 0) {
215 printf("ZFS: unsupported feature: %.*s\n",
216 nvp_name->nv_size, nvp_name->nv_data);
219 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
221 nvlist_destroy(features);
227 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
228 off_t offset, size_t size)
233 if (vdev->v_phys_read == NULL)
237 psize = BP_GET_PSIZE(bp);
242 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
245 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
252 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
254 if (vdev->v_phys_write == NULL)
257 return (vdev->v_phys_write(vdev, offset, buf, size));
260 typedef struct remap_segment {
264 uint64_t rs_split_offset;
268 static remap_segment_t *
269 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
271 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
275 rs->rs_offset = offset;
276 rs->rs_asize = asize;
277 rs->rs_split_offset = split_offset;
283 vdev_indirect_mapping_t *
284 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
285 uint64_t mapping_object)
287 vdev_indirect_mapping_t *vim;
288 vdev_indirect_mapping_phys_t *vim_phys;
291 vim = calloc(1, sizeof (*vim));
295 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
296 if (vim->vim_dn == NULL) {
301 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
309 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
310 if (vim->vim_phys == NULL) {
316 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
317 *vim->vim_phys = *vim_phys;
319 vim->vim_objset = os;
320 vim->vim_object = mapping_object;
321 vim->vim_entries = NULL;
323 vim->vim_havecounts =
324 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
330 * Compare an offset with an indirect mapping entry; there are three
331 * possible scenarios:
333 * 1. The offset is "less than" the mapping entry; meaning the
334 * offset is less than the source offset of the mapping entry. In
335 * this case, there is no overlap between the offset and the
336 * mapping entry and -1 will be returned.
338 * 2. The offset is "greater than" the mapping entry; meaning the
339 * offset is greater than the mapping entry's source offset plus
340 * the entry's size. In this case, there is no overlap between
341 * the offset and the mapping entry and 1 will be returned.
343 * NOTE: If the offset is actually equal to the entry's offset
344 * plus size, this is considered to be "greater" than the entry,
345 * and this case applies (i.e. 1 will be returned). Thus, the
346 * entry's "range" can be considered to be inclusive at its
347 * start, but exclusive at its end: e.g. [src, src + size).
349 * 3. The last case to consider is if the offset actually falls
350 * within the mapping entry's range. If this is the case, the
351 * offset is considered to be "equal to" the mapping entry and
352 * 0 will be returned.
354 * NOTE: If the offset is equal to the entry's source offset,
355 * this case applies and 0 will be returned. If the offset is
356 * equal to the entry's source plus its size, this case does
357 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
361 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
363 const uint64_t *key = v_key;
364 const vdev_indirect_mapping_entry_phys_t *array_elem =
366 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
368 if (*key < src_offset) {
370 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
378 * Return array entry.
380 static vdev_indirect_mapping_entry_phys_t *
381 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
387 if (vim->vim_phys->vimp_num_entries == 0)
390 if (vim->vim_entries == NULL) {
393 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
394 size = vim->vim_phys->vimp_num_entries *
395 sizeof (*vim->vim_entries);
397 size = bsize / sizeof (*vim->vim_entries);
398 size *= sizeof (*vim->vim_entries);
400 vim->vim_entries = malloc(size);
401 if (vim->vim_entries == NULL)
403 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
404 offset = index * sizeof (*vim->vim_entries);
407 /* We have data in vim_entries */
409 if (index >= vim->vim_entry_offset &&
410 index <= vim->vim_entry_offset + vim->vim_num_entries) {
411 index -= vim->vim_entry_offset;
412 return (&vim->vim_entries[index]);
414 offset = index * sizeof (*vim->vim_entries);
417 vim->vim_entry_offset = index;
418 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
419 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
422 /* Read error, invalidate vim_entries. */
423 free(vim->vim_entries);
424 vim->vim_entries = NULL;
427 index -= vim->vim_entry_offset;
428 return (&vim->vim_entries[index]);
432 * Returns the mapping entry for the given offset.
434 * It's possible that the given offset will not be in the mapping table
435 * (i.e. no mapping entries contain this offset), in which case, the
436 * return value value depends on the "next_if_missing" parameter.
438 * If the offset is not found in the table and "next_if_missing" is
439 * B_FALSE, then NULL will always be returned. The behavior is intended
440 * to allow consumers to get the entry corresponding to the offset
441 * parameter, iff the offset overlaps with an entry in the table.
443 * If the offset is not found in the table and "next_if_missing" is
444 * B_TRUE, then the entry nearest to the given offset will be returned,
445 * such that the entry's source offset is greater than the offset
446 * passed in (i.e. the "next" mapping entry in the table is returned, if
447 * the offset is missing from the table). If there are no entries whose
448 * source offset is greater than the passed in offset, NULL is returned.
450 static vdev_indirect_mapping_entry_phys_t *
451 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
454 ASSERT(vim->vim_phys->vimp_num_entries > 0);
456 vdev_indirect_mapping_entry_phys_t *entry;
458 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
462 * We don't define these inside of the while loop because we use
463 * their value in the case that offset isn't in the mapping.
468 while (last >= base) {
469 mid = base + ((last - base) >> 1);
471 entry = vdev_indirect_mapping_entry(vim, mid);
474 result = dva_mapping_overlap_compare(&offset, entry);
478 } else if (result < 0) {
488 * Given an indirect vdev and an extent on that vdev, it duplicates the
489 * physical entries of the indirect mapping that correspond to the extent
490 * to a new array and returns a pointer to it. In addition, copied_entries
491 * is populated with the number of mapping entries that were duplicated.
493 * Finally, since we are doing an allocation, it is up to the caller to
494 * free the array allocated in this function.
496 vdev_indirect_mapping_entry_phys_t *
497 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
498 uint64_t asize, uint64_t *copied_entries)
500 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
501 vdev_indirect_mapping_t *vim = vd->v_mapping;
502 uint64_t entries = 0;
504 vdev_indirect_mapping_entry_phys_t *first_mapping =
505 vdev_indirect_mapping_entry_for_offset(vim, offset);
506 ASSERT3P(first_mapping, !=, NULL);
508 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
510 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
511 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
512 uint64_t inner_size = MIN(asize, size - inner_offset);
514 offset += inner_size;
520 size_t copy_length = entries * sizeof (*first_mapping);
521 duplicate_mappings = malloc(copy_length);
522 if (duplicate_mappings != NULL)
523 bcopy(first_mapping, duplicate_mappings, copy_length);
527 *copied_entries = entries;
529 return (duplicate_mappings);
533 vdev_lookup_top(spa_t *spa, uint64_t vdev)
538 vlist = &spa->spa_root_vdev->v_children;
539 STAILQ_FOREACH(rvd, vlist, v_childlink)
540 if (rvd->v_id == vdev)
547 * This is a callback for vdev_indirect_remap() which allocates an
548 * indirect_split_t for each split segment and adds it to iv_splits.
551 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
552 uint64_t size, void *arg)
556 indirect_vsd_t *iv = zio->io_vsd;
558 if (vd->v_read == vdev_indirect_read)
561 if (vd->v_read == vdev_mirror_read)
564 indirect_split_t *is =
565 malloc(offsetof(indirect_split_t, is_child[n]));
567 zio->io_error = ENOMEM;
570 bzero(is, offsetof(indirect_split_t, is_child[n]));
574 is->is_split_offset = split_offset;
575 is->is_target_offset = offset;
579 * Note that we only consider multiple copies of the data for
580 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
581 * though they use the same ops as mirror, because there's only one
582 * "good" copy under the replacing/spare.
584 if (vd->v_read == vdev_mirror_read) {
588 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
589 is->is_child[i++].ic_vdev = kid;
592 is->is_child[0].ic_vdev = vd;
595 list_insert_tail(&iv->iv_splits, is);
599 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
602 spa_t *spa = vd->v_spa;
606 list_create(&stack, sizeof (remap_segment_t),
607 offsetof(remap_segment_t, rs_node));
609 rs = rs_alloc(vd, offset, asize, 0);
611 printf("vdev_indirect_remap: out of memory.\n");
612 zio->io_error = ENOMEM;
614 for (; rs != NULL; rs = list_remove_head(&stack)) {
615 vdev_t *v = rs->rs_vd;
616 uint64_t num_entries = 0;
617 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
618 vdev_indirect_mapping_entry_phys_t *mapping =
619 vdev_indirect_mapping_duplicate_adjacent_entries(v,
620 rs->rs_offset, rs->rs_asize, &num_entries);
622 if (num_entries == 0)
623 zio->io_error = ENOMEM;
625 for (uint64_t i = 0; i < num_entries; i++) {
626 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
627 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
628 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
629 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
630 uint64_t inner_offset = rs->rs_offset -
631 DVA_MAPPING_GET_SRC_OFFSET(m);
632 uint64_t inner_size =
633 MIN(rs->rs_asize, size - inner_offset);
634 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
636 if (dst_v->v_read == vdev_indirect_read) {
639 o = rs_alloc(dst_v, dst_offset + inner_offset,
640 inner_size, rs->rs_split_offset);
642 printf("vdev_indirect_remap: "
644 zio->io_error = ENOMEM;
648 list_insert_head(&stack, o);
650 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
651 dst_offset + inner_offset,
655 * vdev_indirect_gather_splits can have memory
656 * allocation error, we can not recover from it.
658 if (zio->io_error != 0)
660 rs->rs_offset += inner_size;
661 rs->rs_asize -= inner_size;
662 rs->rs_split_offset += inner_size;
667 if (zio->io_error != 0)
671 list_destroy(&stack);
675 vdev_indirect_map_free(zio_t *zio)
677 indirect_vsd_t *iv = zio->io_vsd;
678 indirect_split_t *is;
680 while ((is = list_head(&iv->iv_splits)) != NULL) {
681 for (int c = 0; c < is->is_children; c++) {
682 indirect_child_t *ic = &is->is_child[c];
685 list_remove(&iv->iv_splits, is);
692 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
693 off_t offset, size_t bytes)
696 spa_t *spa = vdev->v_spa;
698 indirect_split_t *first;
701 iv = calloc(1, sizeof(*iv));
705 list_create(&iv->iv_splits,
706 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
708 bzero(&zio, sizeof(zio));
710 zio.io_bp = (blkptr_t *)bp;
713 zio.io_offset = offset;
717 if (vdev->v_mapping == NULL) {
718 vdev_indirect_config_t *vic;
720 vic = &vdev->vdev_indirect_config;
721 vdev->v_mapping = vdev_indirect_mapping_open(spa,
722 spa->spa_mos, vic->vic_mapping_object);
725 vdev_indirect_remap(vdev, offset, bytes, &zio);
726 if (zio.io_error != 0)
727 return (zio.io_error);
729 first = list_head(&iv->iv_splits);
730 if (first->is_size == zio.io_size) {
732 * This is not a split block; we are pointing to the entire
733 * data, which will checksum the same as the original data.
734 * Pass the BP down so that the child i/o can verify the
735 * checksum, and try a different location if available
736 * (e.g. on a mirror).
738 * While this special case could be handled the same as the
739 * general (split block) case, doing it this way ensures
740 * that the vast majority of blocks on indirect vdevs
741 * (which are not split) are handled identically to blocks
742 * on non-indirect vdevs. This allows us to be less strict
743 * about performance in the general (but rare) case.
745 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
746 zio.io_data, first->is_target_offset, bytes);
748 iv->iv_split_block = B_TRUE;
750 * Read one copy of each split segment, from the
751 * top-level vdev. Since we don't know the
752 * checksum of each split individually, the child
753 * zio can't ensure that we get the right data.
754 * E.g. if it's a mirror, it will just read from a
755 * random (healthy) leaf vdev. We have to verify
756 * the checksum in vdev_indirect_io_done().
758 for (indirect_split_t *is = list_head(&iv->iv_splits);
759 is != NULL; is = list_next(&iv->iv_splits, is)) {
760 char *ptr = zio.io_data;
762 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
763 ptr + is->is_split_offset, is->is_target_offset,
766 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
772 vdev_indirect_map_free(&zio);
780 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
781 off_t offset, size_t bytes)
784 return (vdev_read_phys(vdev, bp, buf,
785 offset + VDEV_LABEL_START_SIZE, bytes));
789 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
790 void *buf __unused, off_t offset __unused, size_t bytes __unused)
797 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
798 off_t offset, size_t bytes)
804 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
805 if (kid->v_state != VDEV_STATE_HEALTHY)
807 rc = kid->v_read(kid, bp, buf, offset, bytes);
816 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
817 off_t offset, size_t bytes)
822 * Here we should have two kids:
823 * First one which is the one we are replacing and we can trust
824 * only this one to have valid data, but it might not be present.
825 * Second one is that one we are replacing with. It is most likely
826 * healthy, but we can't trust it has needed data, so we won't use it.
828 kid = STAILQ_FIRST(&vdev->v_children);
831 if (kid->v_state != VDEV_STATE_HEALTHY)
833 return (kid->v_read(kid, bp, buf, offset, bytes));
837 vdev_find(uint64_t guid)
841 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
842 if (vdev->v_guid == guid)
849 vdev_create(uint64_t guid, vdev_read_t *_read)
852 vdev_indirect_config_t *vic;
854 vdev = calloc(1, sizeof(vdev_t));
856 STAILQ_INIT(&vdev->v_children);
858 vdev->v_read = _read;
861 * root vdev has no read function, we use this fact to
862 * skip setting up data we do not need for root vdev.
863 * We only point root vdev from spa.
866 vic = &vdev->vdev_indirect_config;
867 vic->vic_prev_indirect_vdev = UINT64_MAX;
868 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
876 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
878 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
881 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
883 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
885 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
887 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
889 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
890 NULL, &is_degraded, NULL);
891 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
892 NULL, &isnt_present, NULL);
893 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
897 vdev->v_state = VDEV_STATE_OFFLINE;
898 else if (is_removed != 0)
899 vdev->v_state = VDEV_STATE_REMOVED;
900 else if (is_faulted != 0)
901 vdev->v_state = VDEV_STATE_FAULTED;
902 else if (is_degraded != 0)
903 vdev->v_state = VDEV_STATE_DEGRADED;
904 else if (isnt_present != 0)
905 vdev->v_state = VDEV_STATE_CANT_OPEN;
907 vdev->v_islog = is_log != 0;
911 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
913 uint64_t id, ashift, asize, nparity;
920 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
922 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
927 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
928 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
930 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
932 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
933 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
934 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
935 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
936 printf("ZFS: can only boot from disk, mirror, raidz1, "
937 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
941 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
942 vdev = vdev_create(guid, vdev_mirror_read);
943 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
944 vdev = vdev_create(guid, vdev_raidz_read);
945 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
946 vdev = vdev_create(guid, vdev_replacing_read);
947 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
948 vdev_indirect_config_t *vic;
950 vdev = vdev_create(guid, vdev_indirect_read);
952 vdev->v_state = VDEV_STATE_HEALTHY;
953 vic = &vdev->vdev_indirect_config;
956 ZPOOL_CONFIG_INDIRECT_OBJECT,
958 NULL, &vic->vic_mapping_object, NULL);
960 ZPOOL_CONFIG_INDIRECT_BIRTHS,
962 NULL, &vic->vic_births_object, NULL);
964 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
966 NULL, &vic->vic_prev_indirect_vdev, NULL);
968 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
969 vdev = vdev_create(guid, vdev_missing_read);
971 vdev = vdev_create(guid, vdev_disk_read);
977 vdev_set_initial_state(vdev, nvlist);
979 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
980 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
981 vdev->v_ashift = ashift;
983 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
984 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
985 vdev->v_psize = asize +
986 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
989 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
990 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
991 vdev->v_nparity = nparity;
993 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
994 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
995 char prefix[] = "/dev/";
997 len = strlen(prefix);
998 if (len < pathlen && memcmp(path, prefix, len) == 0) {
1002 name = malloc(pathlen + 1);
1003 bcopy(path, name, pathlen);
1004 name[pathlen] = '\0';
1005 vdev->v_name = name;
1008 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1009 if (vdev->v_nparity < 1 ||
1010 vdev->v_nparity > 3) {
1011 printf("ZFS: invalid raidz parity: %d\n",
1015 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1016 vdev->v_nparity, id);
1018 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1020 vdev->v_name = name;
1027 * Find slot for vdev. We return either NULL to signal to use
1028 * STAILQ_INSERT_HEAD, or we return link element to be used with
1029 * STAILQ_INSERT_AFTER.
1032 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1034 vdev_t *v, *previous;
1036 if (STAILQ_EMPTY(&top_vdev->v_children))
1040 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1041 if (v->v_id > vdev->v_id)
1044 if (v->v_id == vdev->v_id)
1047 if (v->v_id < vdev->v_id)
1054 vdev_child_count(vdev_t *vdev)
1060 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1067 * Insert vdev into top_vdev children list. List is ordered by v_id.
1070 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1076 * The top level vdev can appear in random order, depending how
1077 * the firmware is presenting the disk devices.
1078 * However, we will insert vdev to create list ordered by v_id,
1079 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1080 * as STAILQ does not have insert before.
1082 previous = vdev_find_previous(top_vdev, vdev);
1084 if (previous == NULL) {
1085 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1086 } else if (previous->v_id == vdev->v_id) {
1088 * This vdev was configured from label config,
1089 * do not insert duplicate.
1093 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1097 count = vdev_child_count(top_vdev);
1098 if (top_vdev->v_nchildren < count)
1099 top_vdev->v_nchildren = count;
1103 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1105 vdev_t *top_vdev, *vdev;
1106 nvlist_t **kids = NULL;
1110 top_vdev = vdev_find(top_guid);
1111 if (top_vdev == NULL) {
1112 rc = vdev_init(top_guid, nvlist, &top_vdev);
1115 top_vdev->v_spa = spa;
1116 top_vdev->v_top = top_vdev;
1117 vdev_insert(spa->spa_root_vdev, top_vdev);
1120 /* Add children if there are any. */
1121 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1122 &nkids, &kids, NULL);
1124 for (int i = 0; i < nkids; i++) {
1127 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1128 DATA_TYPE_UINT64, NULL, &guid, NULL);
1132 rc = vdev_init(guid, kids[i], &vdev);
1137 vdev->v_top = top_vdev;
1138 vdev_insert(top_vdev, vdev);
1142 * When there are no children, nvlist_find() does return
1143 * error, reset it because leaf devices have no children.
1149 for (int i = 0; i < nkids; i++)
1150 nvlist_destroy(kids[i]);
1158 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1160 uint64_t pool_guid, top_guid;
1164 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1165 NULL, &pool_guid, NULL) ||
1166 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1167 NULL, &top_guid, NULL) ||
1168 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1169 NULL, &vdevs, NULL)) {
1170 printf("ZFS: can't find vdev details\n");
1174 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1175 nvlist_destroy(vdevs);
1180 vdev_set_state(vdev_t *vdev)
1186 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1187 vdev_set_state(kid);
1191 * A mirror or raidz is healthy if all its kids are healthy. A
1192 * mirror is degraded if any of its kids is healthy; a raidz
1193 * is degraded if at most nparity kids are offline.
1195 if (STAILQ_FIRST(&vdev->v_children)) {
1198 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1199 if (kid->v_state == VDEV_STATE_HEALTHY)
1204 if (bad_kids == 0) {
1205 vdev->v_state = VDEV_STATE_HEALTHY;
1207 if (vdev->v_read == vdev_mirror_read) {
1209 vdev->v_state = VDEV_STATE_DEGRADED;
1211 vdev->v_state = VDEV_STATE_OFFLINE;
1213 } else if (vdev->v_read == vdev_raidz_read) {
1214 if (bad_kids > vdev->v_nparity) {
1215 vdev->v_state = VDEV_STATE_OFFLINE;
1217 vdev->v_state = VDEV_STATE_DEGRADED;
1225 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1228 nvlist_t **kids = NULL;
1231 /* Update top vdev. */
1232 vdev = vdev_find(top_guid);
1234 vdev_set_initial_state(vdev, nvlist);
1236 /* Update children if there are any. */
1237 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1238 &nkids, &kids, NULL);
1240 for (int i = 0; i < nkids; i++) {
1243 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1244 DATA_TYPE_UINT64, NULL, &guid, NULL);
1248 vdev = vdev_find(guid);
1250 vdev_set_initial_state(vdev, kids[i]);
1256 for (int i = 0; i < nkids; i++)
1257 nvlist_destroy(kids[i]);
1265 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1267 uint64_t pool_guid, vdev_children;
1268 nvlist_t *vdevs = NULL, **kids = NULL;
1271 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1272 NULL, &pool_guid, NULL) ||
1273 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1274 NULL, &vdev_children, NULL) ||
1275 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1276 NULL, &vdevs, NULL)) {
1277 printf("ZFS: can't find vdev details\n");
1282 if (spa->spa_guid != pool_guid) {
1283 nvlist_destroy(vdevs);
1287 spa->spa_root_vdev->v_nchildren = vdev_children;
1289 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1290 &nkids, &kids, NULL);
1291 nvlist_destroy(vdevs);
1294 * MOS config has at least one child for root vdev.
1299 for (int i = 0; i < nkids; i++) {
1303 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1307 vdev = vdev_find(guid);
1309 * Top level vdev is missing, create it.
1312 rc = vdev_from_nvlist(spa, guid, kids[i]);
1314 rc = vdev_update_from_nvlist(guid, kids[i]);
1319 for (int i = 0; i < nkids; i++)
1320 nvlist_destroy(kids[i]);
1325 * Re-evaluate top-level vdev state.
1327 vdev_set_state(spa->spa_root_vdev);
1333 spa_find_by_guid(uint64_t guid)
1337 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1338 if (spa->spa_guid == guid)
1345 spa_find_by_name(const char *name)
1349 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1350 if (strcmp(spa->spa_name, name) == 0)
1357 spa_find_by_dev(struct zfs_devdesc *dev)
1360 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1363 if (dev->pool_guid == 0)
1364 return (STAILQ_FIRST(&zfs_pools));
1366 return (spa_find_by_guid(dev->pool_guid));
1370 spa_create(uint64_t guid, const char *name)
1374 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1376 if ((spa->spa_name = strdup(name)) == NULL) {
1380 spa->spa_uberblock = &spa->spa_uberblock_master;
1381 spa->spa_mos = &spa->spa_mos_master;
1382 spa->spa_guid = guid;
1383 spa->spa_root_vdev = vdev_create(guid, NULL);
1384 if (spa->spa_root_vdev == NULL) {
1385 free(spa->spa_name);
1389 spa->spa_root_vdev->v_name = strdup("root");
1390 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1396 state_name(vdev_state_t state)
1398 static const char *names[] = {
1408 return (names[state]);
1413 #define pager_printf printf
1418 pager_printf(const char *fmt, ...)
1423 va_start(args, fmt);
1424 vsnprintf(line, sizeof(line), fmt, args);
1426 return (pager_output(line));
1431 #define STATUS_FORMAT " %s %s\n"
1434 print_state(int indent, const char *name, vdev_state_t state)
1440 for (i = 0; i < indent; i++)
1443 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1447 vdev_status(vdev_t *vdev, int indent)
1452 if (vdev->v_islog) {
1453 (void) pager_output(" logs\n");
1457 ret = print_state(indent, vdev->v_name, vdev->v_state);
1461 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1462 ret = vdev_status(kid, indent + 1);
1470 spa_status(spa_t *spa)
1472 static char bootfs[ZFS_MAXNAMELEN];
1476 int good_kids, bad_kids, degraded_kids, ret;
1479 ret = pager_printf(" pool: %s\n", spa->spa_name);
1483 if (zfs_get_root(spa, &rootid) == 0 &&
1484 zfs_rlookup(spa, rootid, bootfs) == 0) {
1485 if (bootfs[0] == '\0')
1486 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1488 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1493 ret = pager_printf("config:\n\n");
1496 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1503 vlist = &spa->spa_root_vdev->v_children;
1504 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1505 if (vdev->v_state == VDEV_STATE_HEALTHY)
1507 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1513 state = VDEV_STATE_CLOSED;
1514 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1515 state = VDEV_STATE_HEALTHY;
1516 else if ((good_kids + degraded_kids) > 0)
1517 state = VDEV_STATE_DEGRADED;
1519 ret = print_state(0, spa->spa_name, state);
1523 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1524 ret = vdev_status(vdev, 1);
1532 spa_all_status(void)
1535 int first = 1, ret = 0;
1537 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1539 ret = pager_printf("\n");
1544 ret = spa_status(spa);
1552 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1554 uint64_t label_offset;
1556 if (l < VDEV_LABELS / 2)
1559 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1561 return (offset + l * sizeof (vdev_label_t) + label_offset);
1565 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1567 unsigned int seq1 = 0;
1568 unsigned int seq2 = 0;
1569 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1574 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1578 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1579 seq1 = MMP_SEQ(ub1);
1581 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1582 seq2 = MMP_SEQ(ub2);
1584 return (AVL_CMP(seq1, seq2));
1588 uberblock_verify(uberblock_t *ub)
1590 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1591 byteswap_uint64_array(ub, sizeof (uberblock_t));
1594 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1595 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1602 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1608 off = vdev_label_offset(vd->v_psize, l, offset);
1611 BP_SET_LSIZE(&bp, size);
1612 BP_SET_PSIZE(&bp, size);
1613 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1614 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1615 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1616 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1618 return (vdev_read_phys(vd, &bp, buf, off, size));
1622 * We do need to be sure we write to correct location.
1623 * Our vdev label does consist of 4 fields:
1624 * pad1 (8k), reserved.
1625 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1626 * vdev_phys (112k), checksummed
1627 * uberblock ring (128k), checksummed.
1629 * Since bootenv area may contain garbage, we can not reliably read it, as
1630 * we can get checksum errors.
1631 * Next best thing is vdev_phys - it is just after bootenv. It still may
1632 * be corrupted, but in such case we will miss this one write.
1635 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1637 uint64_t off, o_phys;
1639 size_t size = VDEV_PHYS_SIZE;
1642 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1643 off = vdev_label_offset(vd->v_psize, l, o_phys);
1645 /* off should be 8K from bootenv */
1646 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1653 /* Read vdev_phys */
1654 rc = vdev_label_read(vd, l, buf, o_phys, size);
1660 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1662 zio_checksum_info_t *ci;
1665 size_t size = VDEV_PAD_SIZE;
1668 if (vd->v_phys_write == NULL)
1671 off = vdev_label_offset(vd->v_psize, l, offset);
1673 rc = vdev_label_write_validate(vd, l, offset);
1678 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1679 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1680 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1681 ci->ci_func[0](be, size, NULL, &cksum);
1682 be->vbe_zbt.zec_cksum = cksum;
1684 return (vdev_write_phys(vd, be, off, size));
1688 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1693 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1694 if (kid->v_state != VDEV_STATE_HEALTHY)
1696 rc = vdev_write_bootenv_impl(kid, be);
1702 * Non-leaf vdevs do not have v_phys_write.
1704 if (vdev->v_phys_write == NULL)
1707 for (int l = 0; l < VDEV_LABELS; l++) {
1708 rc = vdev_label_write(vdev, l, be,
1709 offsetof(vdev_label_t, vl_be));
1711 printf("failed to write bootenv to %s label %d: %d\n",
1712 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1720 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1722 vdev_boot_envblock_t *be;
1727 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1731 nvp = vdev_read_bootenv(vdev);
1733 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1735 nvlist_destroy(nvp);
1738 be = calloc(1, sizeof(*be));
1742 be->vbe_version = version;
1746 * If there is no envmap, we will just wipe bootenv.
1748 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1749 be->vbe_bootenv, NULL);
1754 nv.nv_header = nvl->nv_header;
1755 nv.nv_asize = nvl->nv_asize;
1756 nv.nv_size = nvl->nv_size;
1758 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1759 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1760 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1761 rv = nvlist_export(&nv);
1770 be->vbe_version = htobe64(be->vbe_version);
1771 rv = vdev_write_bootenv_impl(vdev, be);
1778 * Read the bootenv area from pool label, return the nvlist from it.
1779 * We return from first successful read.
1782 vdev_read_bootenv(vdev_t *vdev)
1786 vdev_boot_envblock_t *be;
1791 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1792 if (kid->v_state != VDEV_STATE_HEALTHY)
1795 benv = vdev_read_bootenv(kid);
1800 be = malloc(sizeof (*be));
1805 for (int l = 0; l < VDEV_LABELS; l++) {
1806 rv = vdev_label_read(vdev, l, be,
1807 offsetof(vdev_label_t, vl_be),
1817 be->vbe_version = be64toh(be->vbe_version);
1818 switch (be->vbe_version) {
1821 * we have textual data in vbe_bootenv, create nvlist
1822 * with key "envmap".
1824 benv = nvlist_create(NV_UNIQUE_NAME);
1826 if (*be->vbe_bootenv == '\0') {
1827 nvlist_add_uint64(benv, BOOTENV_VERSION,
1831 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1832 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1833 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1838 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1842 command = (char *)be;
1845 /* Check for legacy zfsbootcfg command string */
1846 for (int i = 0; command[i] != '\0'; i++) {
1847 if (iscntrl(command[i])) {
1854 benv = nvlist_create(NV_UNIQUE_NAME);
1857 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1860 nvlist_add_uint64(benv, BOOTENV_VERSION,
1870 vdev_get_label_asize(nvlist_t *nvl)
1879 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1880 NULL, &vdevs, NULL) != 0)
1884 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1885 * For raidz, the asize is raw size of all children.
1887 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1888 NULL, &type, &len) != 0)
1891 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1892 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1893 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1896 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1897 NULL, &asize, NULL) != 0)
1900 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1904 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1905 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1911 for (int i = 0; i < nkids; i++)
1912 nvlist_destroy(kids[i]);
1916 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1918 nvlist_destroy(vdevs);
1923 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1926 uint64_t best_txg = 0;
1927 uint64_t label_txg = 0;
1929 nvlist_t *nvl = NULL, *tmp;
1932 label = malloc(sizeof (vdev_phys_t));
1936 for (int l = 0; l < VDEV_LABELS; l++) {
1937 if (vdev_label_read(vd, l, label,
1938 offsetof(vdev_label_t, vl_vdev_phys),
1939 sizeof (vdev_phys_t)))
1942 tmp = nvlist_import(label->vp_nvlist,
1943 sizeof(label->vp_nvlist));
1947 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1948 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1949 if (error != 0 || label_txg == 0) {
1950 nvlist_destroy(nvl);
1955 if (label_txg <= txg && label_txg > best_txg) {
1956 best_txg = label_txg;
1957 nvlist_destroy(nvl);
1962 * Use asize from pool config. We need this
1963 * because we can get bad value from BIOS.
1965 asize = vdev_get_label_asize(nvl);
1967 vd->v_psize = asize;
1970 nvlist_destroy(tmp);
1973 if (best_txg == 0) {
1974 nvlist_destroy(nvl);
1983 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1987 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1991 for (int l = 0; l < VDEV_LABELS; l++) {
1992 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1993 if (vdev_label_read(vd, l, buf,
1994 VDEV_UBERBLOCK_OFFSET(vd, n),
1995 VDEV_UBERBLOCK_SIZE(vd)))
1997 if (uberblock_verify(buf) != 0)
2000 if (vdev_uberblock_compare(buf, ub) > 0)
2008 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2016 uint64_t guid, vdev_children;
2017 uint64_t pool_txg, pool_guid;
2018 const char *pool_name;
2022 * Load the vdev label and figure out which
2023 * uberblock is most current.
2025 memset(&vtmp, 0, sizeof(vtmp));
2026 vtmp.v_phys_read = _read;
2027 vtmp.v_phys_write = _write;
2029 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2030 (uint64_t)sizeof (vdev_label_t));
2032 /* Test for minimum device size. */
2033 if (vtmp.v_psize < SPA_MINDEVSIZE)
2036 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2040 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2041 NULL, &val, NULL) != 0) {
2042 nvlist_destroy(nvl);
2046 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2047 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2048 (unsigned)val, (unsigned)SPA_VERSION);
2049 nvlist_destroy(nvl);
2053 /* Check ZFS features for read */
2054 rc = nvlist_check_features_for_read(nvl);
2056 nvlist_destroy(nvl);
2060 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2061 NULL, &val, NULL) != 0) {
2062 nvlist_destroy(nvl);
2066 if (val == POOL_STATE_DESTROYED) {
2067 /* We don't boot only from destroyed pools. */
2068 nvlist_destroy(nvl);
2072 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2073 NULL, &pool_txg, NULL) != 0 ||
2074 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2075 NULL, &pool_guid, NULL) != 0 ||
2076 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2077 NULL, &pool_name, &namelen) != 0) {
2079 * Cache and spare devices end up here - just ignore
2082 nvlist_destroy(nvl);
2087 * Create the pool if this is the first time we've seen it.
2089 spa = spa_find_by_guid(pool_guid);
2093 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2094 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2095 name = malloc(namelen + 1);
2097 nvlist_destroy(nvl);
2100 bcopy(pool_name, name, namelen);
2101 name[namelen] = '\0';
2102 spa = spa_create(pool_guid, name);
2105 nvlist_destroy(nvl);
2108 spa->spa_root_vdev->v_nchildren = vdev_children;
2110 if (pool_txg > spa->spa_txg)
2111 spa->spa_txg = pool_txg;
2114 * Get the vdev tree and create our in-core copy of it.
2115 * If we already have a vdev with this guid, this must
2116 * be some kind of alias (overlapping slices, dangerously dedicated
2119 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2120 NULL, &guid, NULL) != 0) {
2121 nvlist_destroy(nvl);
2124 vdev = vdev_find(guid);
2125 /* Has this vdev already been inited? */
2126 if (vdev && vdev->v_phys_read) {
2127 nvlist_destroy(nvl);
2131 rc = vdev_init_from_label(spa, nvl);
2132 nvlist_destroy(nvl);
2137 * We should already have created an incomplete vdev for this
2138 * vdev. Find it and initialise it with our read proc.
2140 vdev = vdev_find(guid);
2142 vdev->v_phys_read = _read;
2143 vdev->v_phys_write = _write;
2144 vdev->v_priv = priv;
2145 vdev->v_psize = vtmp.v_psize;
2147 * If no other state is set, mark vdev healthy.
2149 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2150 vdev->v_state = VDEV_STATE_HEALTHY;
2152 printf("ZFS: inconsistent nvlist contents\n");
2157 spa->spa_with_log = vdev->v_islog;
2160 * Re-evaluate top-level vdev state.
2162 vdev_set_state(vdev->v_top);
2165 * Ok, we are happy with the pool so far. Lets find
2166 * the best uberblock and then we can actually access
2167 * the contents of the pool.
2169 vdev_uberblock_load(vdev, spa->spa_uberblock);
2181 for (v = 0; v < 32; v++)
2188 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2191 zio_gbh_phys_t zio_gb;
2195 /* Artificial BP for gang block header. */
2197 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2198 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2199 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2200 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2201 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2202 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2204 /* Read gang header block using the artificial BP. */
2205 if (zio_read(spa, &gbh_bp, &zio_gb))
2209 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2210 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2212 if (BP_IS_HOLE(gbp))
2214 if (zio_read(spa, gbp, pbuf))
2216 pbuf += BP_GET_PSIZE(gbp);
2219 if (zio_checksum_verify(spa, bp, buf))
2225 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2227 int cpfunc = BP_GET_COMPRESS(bp);
2228 uint64_t align, size;
2233 * Process data embedded in block pointer
2235 if (BP_IS_EMBEDDED(bp)) {
2236 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2238 size = BPE_GET_PSIZE(bp);
2239 ASSERT(size <= BPE_PAYLOAD_SIZE);
2241 if (cpfunc != ZIO_COMPRESS_OFF)
2242 pbuf = malloc(size);
2249 decode_embedded_bp_compressed(bp, pbuf);
2252 if (cpfunc != ZIO_COMPRESS_OFF) {
2253 error = zio_decompress_data(cpfunc, pbuf,
2254 size, buf, BP_GET_LSIZE(bp));
2258 printf("ZFS: i/o error - unable to decompress "
2259 "block pointer data, error %d\n", error);
2265 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2266 const dva_t *dva = &bp->blk_dva[i];
2272 if (!dva->dva_word[0] && !dva->dva_word[1])
2275 vdevid = DVA_GET_VDEV(dva);
2276 offset = DVA_GET_OFFSET(dva);
2277 vlist = &spa->spa_root_vdev->v_children;
2278 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2279 if (vdev->v_id == vdevid)
2282 if (!vdev || !vdev->v_read)
2285 size = BP_GET_PSIZE(bp);
2286 if (vdev->v_read == vdev_raidz_read) {
2287 align = 1ULL << vdev->v_ashift;
2288 if (P2PHASE(size, align) != 0)
2289 size = P2ROUNDUP(size, align);
2291 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2292 pbuf = malloc(size);
2301 if (DVA_GET_GANG(dva))
2302 error = zio_read_gang(spa, bp, pbuf);
2304 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2306 if (cpfunc != ZIO_COMPRESS_OFF)
2307 error = zio_decompress_data(cpfunc, pbuf,
2308 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2309 else if (size != BP_GET_PSIZE(bp))
2310 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2312 printf("zio_read error: %d\n", error);
2320 printf("ZFS: i/o error - all block copies unavailable\n");
2326 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2327 void *buf, size_t buflen)
2329 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2330 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2331 int nlevels = dnode->dn_nlevels;
2334 if (bsize > SPA_MAXBLOCKSIZE) {
2335 printf("ZFS: I/O error - blocks larger than %llu are not "
2336 "supported\n", SPA_MAXBLOCKSIZE);
2341 * Note: bsize may not be a power of two here so we need to do an
2342 * actual divide rather than a bitshift.
2344 while (buflen > 0) {
2345 uint64_t bn = offset / bsize;
2346 int boff = offset % bsize;
2348 const blkptr_t *indbp;
2351 if (bn > dnode->dn_maxblkid)
2354 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2357 indbp = dnode->dn_blkptr;
2358 for (i = 0; i < nlevels; i++) {
2360 * Copy the bp from the indirect array so that
2361 * we can re-use the scratch buffer for multi-level
2364 ibn = bn >> ((nlevels - i - 1) * ibshift);
2365 ibn &= ((1 << ibshift) - 1);
2367 if (BP_IS_HOLE(&bp)) {
2368 memset(dnode_cache_buf, 0, bsize);
2371 rc = zio_read(spa, &bp, dnode_cache_buf);
2374 indbp = (const blkptr_t *) dnode_cache_buf;
2376 dnode_cache_obj = dnode;
2377 dnode_cache_bn = bn;
2381 * The buffer contains our data block. Copy what we
2382 * need from it and loop.
2385 if (i > buflen) i = buflen;
2386 memcpy(buf, &dnode_cache_buf[boff], i);
2387 buf = ((char *)buf) + i;
2396 * Lookup a value in a microzap directory.
2399 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2402 const mzap_ent_phys_t *mze;
2406 * Microzap objects use exactly one block. Read the whole
2409 chunks = size / MZAP_ENT_LEN - 1;
2410 for (i = 0; i < chunks; i++) {
2411 mze = &mz->mz_chunk[i];
2412 if (strcmp(mze->mze_name, name) == 0) {
2413 *value = mze->mze_value;
2422 * Compare a name with a zap leaf entry. Return non-zero if the name
2426 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2430 const zap_leaf_chunk_t *nc;
2433 namelen = zc->l_entry.le_name_numints;
2435 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2437 while (namelen > 0) {
2441 if (len > ZAP_LEAF_ARRAY_BYTES)
2442 len = ZAP_LEAF_ARRAY_BYTES;
2443 if (memcmp(p, nc->l_array.la_array, len))
2447 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2454 * Extract a uint64_t value from a zap leaf entry.
2457 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2459 const zap_leaf_chunk_t *vc;
2464 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2465 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2466 value = (value << 8) | p[i];
2473 stv(int len, void *addr, uint64_t value)
2477 *(uint8_t *)addr = value;
2480 *(uint16_t *)addr = value;
2483 *(uint32_t *)addr = value;
2486 *(uint64_t *)addr = value;
2492 * Extract a array from a zap leaf entry.
2495 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2496 uint64_t integer_size, uint64_t num_integers, void *buf)
2498 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2500 uint64_t *u64 = buf;
2502 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2503 int chunk = zc->l_entry.le_value_chunk;
2506 if (integer_size == 8 && len == 1) {
2507 *u64 = fzap_leaf_value(zl, zc);
2512 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2515 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2516 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2517 value = (value << 8) | la->la_array[i];
2519 if (byten == array_int_len) {
2520 stv(integer_size, p, value);
2528 chunk = la->la_next;
2533 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2536 switch (integer_size) {
2546 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2553 zap_leaf_free(zap_leaf_t *leaf)
2560 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2562 int bs = FZAP_BLOCK_SHIFT(zap);
2565 *lp = malloc(sizeof(**lp));
2570 (*lp)->l_phys = malloc(1 << bs);
2572 if ((*lp)->l_phys == NULL) {
2576 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2585 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2588 int bs = FZAP_BLOCK_SHIFT(zap);
2589 uint64_t blk = idx >> (bs - 3);
2590 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2594 buf = malloc(1 << zap->zap_block_shift);
2597 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2598 buf, 1 << zap->zap_block_shift);
2606 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2608 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2609 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2612 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2617 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2619 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2624 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2625 err = zap_idx_to_blk(zap, idx, &blk);
2628 return (zap_get_leaf_byblk(zap, blk, lp));
2631 #define CHAIN_END 0xffff /* end of the chunk chain */
2632 #define LEAF_HASH(l, h) \
2633 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2635 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2636 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2639 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2640 uint64_t integer_size, uint64_t num_integers, void *value)
2644 struct zap_leaf_entry *le;
2647 * Make sure this chunk matches our hash.
2649 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2650 zl->l_phys->l_hdr.lh_prefix !=
2651 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2655 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2656 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2657 zap_leaf_chunk_t *zc;
2658 uint16_t chunk = *chunkp;
2660 le = ZAP_LEAF_ENTRY(zl, chunk);
2661 if (le->le_hash != hash)
2663 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2664 if (fzap_name_equal(zl, zc, name)) {
2665 if (zc->l_entry.le_value_intlen > integer_size) {
2668 fzap_leaf_array(zl, zc, integer_size,
2669 num_integers, value);
2679 * Lookup a value in a fatzap directory.
2682 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2683 const char *name, uint64_t integer_size, uint64_t num_integers,
2686 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2692 if (zh->zap_magic != ZAP_MAGIC)
2695 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2699 z.zap_block_shift = ilog2(bsize);
2702 z.zap_dnode = dnode;
2704 hash = zap_hash(zh->zap_salt, name);
2705 rc = zap_deref_leaf(&z, hash, &zl);
2709 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2716 * Lookup a name in a zap object and return its value as a uint64_t.
2719 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2720 uint64_t integer_size, uint64_t num_integers, void *value)
2724 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2730 rc = dnode_read(spa, dnode, 0, zap, size);
2734 switch (zap->zap_block_type) {
2736 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2739 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2740 num_integers, value);
2743 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2744 zap->zap_block_type);
2753 * List a microzap directory.
2756 mzap_list(const mzap_phys_t *mz, size_t size,
2757 int (*callback)(const char *, uint64_t))
2759 const mzap_ent_phys_t *mze;
2763 * Microzap objects use exactly one block. Read the whole
2767 chunks = size / MZAP_ENT_LEN - 1;
2768 for (i = 0; i < chunks; i++) {
2769 mze = &mz->mz_chunk[i];
2770 if (mze->mze_name[0]) {
2771 rc = callback(mze->mze_name, mze->mze_value);
2781 * List a fatzap directory.
2784 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2785 int (*callback)(const char *, uint64_t))
2787 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2792 if (zh->zap_magic != ZAP_MAGIC)
2795 z.zap_block_shift = ilog2(bsize);
2799 * This assumes that the leaf blocks start at block 1. The
2800 * documentation isn't exactly clear on this.
2803 zl.l_bs = z.zap_block_shift;
2804 zl.l_phys = malloc(bsize);
2805 if (zl.l_phys == NULL)
2808 for (i = 0; i < zh->zap_num_leafs; i++) {
2809 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2813 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2818 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2819 zap_leaf_chunk_t *zc, *nc;
2822 zc = &ZAP_LEAF_CHUNK(&zl, j);
2823 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2825 namelen = zc->l_entry.le_name_numints;
2826 if (namelen > sizeof(name))
2827 namelen = sizeof(name);
2830 * Paste the name back together.
2832 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2834 while (namelen > 0) {
2837 if (len > ZAP_LEAF_ARRAY_BYTES)
2838 len = ZAP_LEAF_ARRAY_BYTES;
2839 memcpy(p, nc->l_array.la_array, len);
2842 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2846 * Assume the first eight bytes of the value are
2849 value = fzap_leaf_value(&zl, zc);
2851 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2852 rc = callback((const char *)name, value);
2864 static int zfs_printf(const char *name, uint64_t value __unused)
2867 printf("%s\n", name);
2873 * List a zap directory.
2876 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2879 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2886 rc = dnode_read(spa, dnode, 0, zap, size);
2888 if (zap->zap_block_type == ZBT_MICRO)
2889 rc = mzap_list((const mzap_phys_t *)zap, size,
2892 rc = fzap_list(spa, dnode, zap, zfs_printf);
2899 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2900 dnode_phys_t *dnode)
2904 offset = objnum * sizeof(dnode_phys_t);
2905 return dnode_read(spa, &os->os_meta_dnode, offset,
2906 dnode, sizeof(dnode_phys_t));
2910 * Lookup a name in a microzap directory.
2913 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2915 const mzap_ent_phys_t *mze;
2919 * Microzap objects use exactly one block. Read the whole
2922 chunks = size / MZAP_ENT_LEN - 1;
2923 for (i = 0; i < chunks; i++) {
2924 mze = &mz->mz_chunk[i];
2925 if (value == mze->mze_value) {
2926 strcpy(name, mze->mze_name);
2935 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2938 const zap_leaf_chunk_t *nc;
2941 namelen = zc->l_entry.le_name_numints;
2943 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2945 while (namelen > 0) {
2948 if (len > ZAP_LEAF_ARRAY_BYTES)
2949 len = ZAP_LEAF_ARRAY_BYTES;
2950 memcpy(p, nc->l_array.la_array, len);
2953 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2960 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2961 char *name, uint64_t value)
2963 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2968 if (zh->zap_magic != ZAP_MAGIC)
2971 z.zap_block_shift = ilog2(bsize);
2975 * This assumes that the leaf blocks start at block 1. The
2976 * documentation isn't exactly clear on this.
2979 zl.l_bs = z.zap_block_shift;
2980 zl.l_phys = malloc(bsize);
2981 if (zl.l_phys == NULL)
2984 for (i = 0; i < zh->zap_num_leafs; i++) {
2985 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2987 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
2991 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2992 zap_leaf_chunk_t *zc;
2994 zc = &ZAP_LEAF_CHUNK(&zl, j);
2995 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2997 if (zc->l_entry.le_value_intlen != 8 ||
2998 zc->l_entry.le_value_numints != 1)
3001 if (fzap_leaf_value(&zl, zc) == value) {
3002 fzap_name_copy(&zl, zc, name);
3015 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3019 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3026 rc = dnode_read(spa, dnode, 0, zap, size);
3028 if (zap->zap_block_type == ZBT_MICRO)
3029 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3032 rc = fzap_rlookup(spa, dnode, zap, name, value);
3039 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3042 char component[256];
3043 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3044 dnode_phys_t child_dir_zap, dataset, dir, parent;
3046 dsl_dataset_phys_t *ds;
3050 p = &name[sizeof(name) - 1];
3053 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3054 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3057 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3058 dir_obj = ds->ds_dir_obj;
3061 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3063 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3065 /* Actual loop condition. */
3066 parent_obj = dd->dd_parent_obj;
3067 if (parent_obj == 0)
3070 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3073 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3074 child_dir_zapobj = dd->dd_child_dir_zapobj;
3075 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3076 &child_dir_zap) != 0)
3078 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3081 len = strlen(component);
3083 memcpy(p, component, len);
3087 /* Actual loop iteration. */
3088 dir_obj = parent_obj;
3099 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3102 uint64_t dir_obj, child_dir_zapobj;
3103 dnode_phys_t child_dir_zap, dir;
3107 if (objset_get_dnode(spa, spa->spa_mos,
3108 DMU_POOL_DIRECTORY_OBJECT, &dir))
3110 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3116 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3118 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3122 /* Actual loop condition #1. */
3128 memcpy(element, p, q - p);
3129 element[q - p] = '\0';
3136 child_dir_zapobj = dd->dd_child_dir_zapobj;
3137 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3138 &child_dir_zap) != 0)
3141 /* Actual loop condition #2. */
3142 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3147 *objnum = dd->dd_head_dataset_obj;
3153 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3155 uint64_t dir_obj, child_dir_zapobj;
3156 dnode_phys_t child_dir_zap, dir, dataset;
3157 dsl_dataset_phys_t *ds;
3160 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3161 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3164 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3165 dir_obj = ds->ds_dir_obj;
3167 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3168 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3171 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3173 child_dir_zapobj = dd->dd_child_dir_zapobj;
3174 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3175 &child_dir_zap) != 0) {
3176 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3180 return (zap_list(spa, &child_dir_zap) != 0);
3184 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3185 int (*callback)(const char *, uint64_t))
3187 uint64_t dir_obj, child_dir_zapobj;
3188 dnode_phys_t child_dir_zap, dir, dataset;
3189 dsl_dataset_phys_t *ds;
3195 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3197 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3200 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3201 dir_obj = ds->ds_dir_obj;
3203 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3205 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3208 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3210 child_dir_zapobj = dd->dd_child_dir_zapobj;
3211 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3214 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3218 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3221 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3225 if (zap->zap_block_type == ZBT_MICRO)
3226 err = mzap_list((const mzap_phys_t *)zap, size,
3229 err = fzap_list(spa, &child_dir_zap, zap, callback);
3240 * Find the object set given the object number of its dataset object
3241 * and return its details in *objset
3244 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3246 dnode_phys_t dataset;
3247 dsl_dataset_phys_t *ds;
3249 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3250 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3254 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3255 if (zio_read(spa, &ds->ds_bp, objset)) {
3256 printf("ZFS: can't read object set for dataset %ju\n",
3265 * Find the object set pointed to by the BOOTFS property or the root
3266 * dataset if there is none and return its details in *objset
3269 zfs_get_root(const spa_t *spa, uint64_t *objid)
3271 dnode_phys_t dir, propdir;
3272 uint64_t props, bootfs, root;
3277 * Start with the MOS directory object.
3279 if (objset_get_dnode(spa, spa->spa_mos,
3280 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3281 printf("ZFS: can't read MOS object directory\n");
3286 * Lookup the pool_props and see if we can find a bootfs.
3288 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3289 sizeof(props), 1, &props) == 0 &&
3290 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3291 zap_lookup(spa, &propdir, "bootfs",
3292 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3297 * Lookup the root dataset directory
3299 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3300 sizeof(root), 1, &root) ||
3301 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3302 printf("ZFS: can't find root dsl_dir\n");
3307 * Use the information from the dataset directory's bonus buffer
3308 * to find the dataset object and from that the object set itself.
3310 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3311 *objid = dd->dd_head_dataset_obj;
3316 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3322 * Find the root object set if not explicitly provided
3324 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3325 printf("ZFS: can't find root filesystem\n");
3329 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3330 printf("ZFS: can't open root filesystem\n");
3334 mount->rootobj = rootobj;
3340 * callback function for feature name checks.
3343 check_feature(const char *name, uint64_t value)
3349 if (name[0] == '\0')
3352 for (i = 0; features_for_read[i] != NULL; i++) {
3353 if (strcmp(name, features_for_read[i]) == 0)
3356 printf("ZFS: unsupported feature: %s\n", name);
3361 * Checks whether the MOS features that are active are supported.
3364 check_mos_features(const spa_t *spa)
3372 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3375 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3376 sizeof (objnum), 1, &objnum)) != 0) {
3378 * It is older pool without features. As we have already
3379 * tested the label, just return without raising the error.
3384 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3387 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3390 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3395 if (dnode_read(spa, &dir, 0, zap, size)) {
3400 if (zap->zap_block_type == ZBT_MICRO)
3401 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3403 rc = fzap_list(spa, &dir, zap, check_feature);
3410 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3418 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3420 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3421 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3425 if (dir.dn_bonuslen != sizeof (uint64_t))
3428 size = *(uint64_t *)DN_BONUS(&dir);
3433 rc = dnode_read(spa, &dir, 0, nv, size);
3439 *value = nvlist_import(nv, size);
3445 zfs_spa_init(spa_t *spa)
3447 struct uberblock checkpoint;
3449 uint64_t config_object;
3453 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3454 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3457 if (spa->spa_mos->os_type != DMU_OST_META) {
3458 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3462 if (objset_get_dnode(spa, &spa->spa_mos_master,
3463 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3464 printf("ZFS: failed to read pool %s directory object\n",
3468 /* this is allowed to fail, older pools do not have salt */
3469 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3470 sizeof (spa->spa_cksum_salt.zcs_bytes),
3471 spa->spa_cksum_salt.zcs_bytes);
3473 rc = check_mos_features(spa);
3475 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3479 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3480 sizeof (config_object), 1, &config_object);
3482 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3485 rc = load_nvlist(spa, config_object, &nvlist);
3489 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3490 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3492 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3493 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3494 sizeof(checkpoint));
3495 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3496 &spa->spa_mos_checkpoint)) {
3497 printf("ZFS: can not read checkpoint data.\n");
3503 * Update vdevs from MOS config. Note, we do skip encoding bytes
3504 * here. See also vdev_label_read_config().
3506 rc = vdev_init_from_nvlist(spa, nvlist);
3507 nvlist_destroy(nvlist);
3512 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3515 if (dn->dn_bonustype != DMU_OT_SA) {
3516 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3518 sb->st_mode = zp->zp_mode;
3519 sb->st_uid = zp->zp_uid;
3520 sb->st_gid = zp->zp_gid;
3521 sb->st_size = zp->zp_size;
3523 sa_hdr_phys_t *sahdrp;
3528 if (dn->dn_bonuslen != 0)
3529 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3531 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3532 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3535 size = BP_GET_LSIZE(bp);
3540 error = zio_read(spa, bp, buf);
3551 hdrsize = SA_HDR_SIZE(sahdrp);
3552 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3554 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3556 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3558 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3567 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3571 if (dn->dn_bonustype == DMU_OT_SA) {
3572 sa_hdr_phys_t *sahdrp = NULL;
3578 if (dn->dn_bonuslen != 0) {
3579 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3583 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3585 bp = DN_SPILL_BLKPTR(dn);
3587 size = BP_GET_LSIZE(bp);
3592 rc = zio_read(spa, bp, buf);
3599 hdrsize = SA_HDR_SIZE(sahdrp);
3600 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3601 memcpy(path, p, psize);
3606 * Second test is purely to silence bogus compiler
3607 * warning about accessing past the end of dn_bonus.
3609 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3610 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3611 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3613 rc = dnode_read(spa, dn, 0, path, psize);
3620 STAILQ_ENTRY(obj_list) entry;
3624 * Lookup a file and return its dnode.
3627 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3636 int symlinks_followed = 0;
3638 struct obj_list *entry, *tentry;
3639 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3642 if (mount->objset.os_type != DMU_OST_ZFS) {
3643 printf("ZFS: unexpected object set type %ju\n",
3644 (uintmax_t)mount->objset.os_type);
3648 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3652 * Get the root directory dnode.
3654 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3660 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3665 entry->objnum = objnum;
3666 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3668 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3674 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3683 while (*q != '\0' && *q != '/')
3687 if (p + 1 == q && p[0] == '.') {
3692 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3694 if (STAILQ_FIRST(&on_cache) ==
3695 STAILQ_LAST(&on_cache, obj_list, entry)) {
3699 entry = STAILQ_FIRST(&on_cache);
3700 STAILQ_REMOVE_HEAD(&on_cache, entry);
3702 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3705 if (q - p + 1 > sizeof(element)) {
3709 memcpy(element, p, q - p);
3713 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3715 if (!S_ISDIR(sb.st_mode)) {
3720 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3723 objnum = ZFS_DIRENT_OBJ(objnum);
3725 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3729 entry->objnum = objnum;
3730 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3731 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3736 * Check for symlink.
3738 rc = zfs_dnode_stat(spa, &dn, &sb);
3741 if (S_ISLNK(sb.st_mode)) {
3742 if (symlinks_followed > 10) {
3746 symlinks_followed++;
3749 * Read the link value and copy the tail of our
3750 * current path onto the end.
3752 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3756 strcpy(&path[sb.st_size], p);
3758 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3763 * Restart with the new path, starting either at
3764 * the root or at the parent depending whether or
3765 * not the link is relative.
3769 while (STAILQ_FIRST(&on_cache) !=
3770 STAILQ_LAST(&on_cache, obj_list, entry)) {
3771 entry = STAILQ_FIRST(&on_cache);
3772 STAILQ_REMOVE_HEAD(&on_cache, entry);
3776 entry = STAILQ_FIRST(&on_cache);
3777 STAILQ_REMOVE_HEAD(&on_cache, entry);
3780 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3786 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)