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>
45 extern int zstd_init(void);
52 static struct zfsmount zfsmount __unused;
55 * The indirect_child_t represents the vdev that we will read from, when we
56 * need to read all copies of the data (e.g. for scrub or reconstruction).
57 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
58 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
59 * ic_vdev is a child of the mirror.
61 typedef struct indirect_child {
67 * The indirect_split_t represents one mapped segment of an i/o to the
68 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
69 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
70 * For split blocks, there will be several of these.
72 typedef struct indirect_split {
73 list_node_t is_node; /* link on iv_splits */
76 * is_split_offset is the offset into the i/o.
77 * This is the sum of the previous splits' is_size's.
79 uint64_t is_split_offset;
81 vdev_t *is_vdev; /* top-level vdev */
82 uint64_t is_target_offset; /* offset on is_vdev */
84 int is_children; /* number of entries in is_child[] */
87 * is_good_child is the child that we are currently using to
88 * attempt reconstruction.
92 indirect_child_t is_child[1]; /* variable-length */
96 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
97 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
99 typedef struct indirect_vsd {
100 boolean_t iv_split_block;
101 boolean_t iv_reconstruct;
103 list_t iv_splits; /* list of indirect_split_t's */
107 * List of all vdevs, chained through v_alllink.
109 static vdev_list_t zfs_vdevs;
112 * List of ZFS features supported for read
114 static const char *features_for_read[] = {
115 "org.illumos:lz4_compress",
116 "com.delphix:hole_birth",
117 "com.delphix:extensible_dataset",
118 "com.delphix:embedded_data",
119 "org.open-zfs:large_blocks",
120 "org.illumos:sha512",
122 "org.zfsonlinux:large_dnode",
123 "com.joyent:multi_vdev_crash_dump",
124 "com.delphix:spacemap_histogram",
125 "com.delphix:zpool_checkpoint",
126 "com.delphix:spacemap_v2",
127 "com.datto:encryption",
128 "org.zfsonlinux:allocation_classes",
129 "com.datto:resilver_defer",
130 "com.delphix:device_removal",
131 "com.delphix:obsolete_counts",
132 "com.intel:allocation_classes",
133 "org.freebsd:zstd_compress",
138 * List of all pools, chained through spa_link.
140 static spa_list_t zfs_pools;
142 static const dnode_phys_t *dnode_cache_obj;
143 static uint64_t dnode_cache_bn;
144 static char *dnode_cache_buf;
146 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
147 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
148 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
149 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
150 const char *name, uint64_t integer_size, uint64_t num_integers,
152 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
154 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
156 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
158 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
159 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
161 vdev_indirect_mapping_entry_phys_t *
162 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
163 uint64_t, uint64_t *);
168 STAILQ_INIT(&zfs_vdevs);
169 STAILQ_INIT(&zfs_pools);
171 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
178 nvlist_check_features_for_read(nvlist_t *nvl)
180 nvlist_t *features = NULL;
183 nv_string_t *nvp_name;
186 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
187 DATA_TYPE_NVLIST, NULL, &features, NULL);
191 data = (nvs_data_t *)features->nv_data;
192 nvp = &data->nvl_pair; /* first pair in nvlist */
194 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
197 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
200 for (i = 0; features_for_read[i] != NULL; i++) {
201 if (memcmp(nvp_name->nv_data, features_for_read[i],
202 nvp_name->nv_size) == 0) {
209 printf("ZFS: unsupported feature: %.*s\n",
210 nvp_name->nv_size, nvp_name->nv_data);
213 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
215 nvlist_destroy(features);
221 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
222 off_t offset, size_t size)
227 if (vdev->v_phys_read == NULL)
231 psize = BP_GET_PSIZE(bp);
236 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
239 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
246 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
248 if (vdev->v_phys_write == NULL)
251 return (vdev->v_phys_write(vdev, offset, buf, size));
254 typedef struct remap_segment {
258 uint64_t rs_split_offset;
262 static remap_segment_t *
263 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
265 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
269 rs->rs_offset = offset;
270 rs->rs_asize = asize;
271 rs->rs_split_offset = split_offset;
277 vdev_indirect_mapping_t *
278 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
279 uint64_t mapping_object)
281 vdev_indirect_mapping_t *vim;
282 vdev_indirect_mapping_phys_t *vim_phys;
285 vim = calloc(1, sizeof (*vim));
289 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
290 if (vim->vim_dn == NULL) {
295 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
303 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
304 if (vim->vim_phys == NULL) {
310 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
311 *vim->vim_phys = *vim_phys;
313 vim->vim_objset = os;
314 vim->vim_object = mapping_object;
315 vim->vim_entries = NULL;
317 vim->vim_havecounts =
318 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
324 * Compare an offset with an indirect mapping entry; there are three
325 * possible scenarios:
327 * 1. The offset is "less than" the mapping entry; meaning the
328 * offset is less than the source offset of the mapping entry. In
329 * this case, there is no overlap between the offset and the
330 * mapping entry and -1 will be returned.
332 * 2. The offset is "greater than" the mapping entry; meaning the
333 * offset is greater than the mapping entry's source offset plus
334 * the entry's size. In this case, there is no overlap between
335 * the offset and the mapping entry and 1 will be returned.
337 * NOTE: If the offset is actually equal to the entry's offset
338 * plus size, this is considered to be "greater" than the entry,
339 * and this case applies (i.e. 1 will be returned). Thus, the
340 * entry's "range" can be considered to be inclusive at its
341 * start, but exclusive at its end: e.g. [src, src + size).
343 * 3. The last case to consider is if the offset actually falls
344 * within the mapping entry's range. If this is the case, the
345 * offset is considered to be "equal to" the mapping entry and
346 * 0 will be returned.
348 * NOTE: If the offset is equal to the entry's source offset,
349 * this case applies and 0 will be returned. If the offset is
350 * equal to the entry's source plus its size, this case does
351 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
355 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
357 const uint64_t *key = v_key;
358 const vdev_indirect_mapping_entry_phys_t *array_elem =
360 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
362 if (*key < src_offset) {
364 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
372 * Return array entry.
374 static vdev_indirect_mapping_entry_phys_t *
375 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
381 if (vim->vim_phys->vimp_num_entries == 0)
384 if (vim->vim_entries == NULL) {
387 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
388 size = vim->vim_phys->vimp_num_entries *
389 sizeof (*vim->vim_entries);
391 size = bsize / sizeof (*vim->vim_entries);
392 size *= sizeof (*vim->vim_entries);
394 vim->vim_entries = malloc(size);
395 if (vim->vim_entries == NULL)
397 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
398 offset = index * sizeof (*vim->vim_entries);
401 /* We have data in vim_entries */
403 if (index >= vim->vim_entry_offset &&
404 index <= vim->vim_entry_offset + vim->vim_num_entries) {
405 index -= vim->vim_entry_offset;
406 return (&vim->vim_entries[index]);
408 offset = index * sizeof (*vim->vim_entries);
411 vim->vim_entry_offset = index;
412 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
413 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
416 /* Read error, invalidate vim_entries. */
417 free(vim->vim_entries);
418 vim->vim_entries = NULL;
421 index -= vim->vim_entry_offset;
422 return (&vim->vim_entries[index]);
426 * Returns the mapping entry for the given offset.
428 * It's possible that the given offset will not be in the mapping table
429 * (i.e. no mapping entries contain this offset), in which case, the
430 * return value value depends on the "next_if_missing" parameter.
432 * If the offset is not found in the table and "next_if_missing" is
433 * B_FALSE, then NULL will always be returned. The behavior is intended
434 * to allow consumers to get the entry corresponding to the offset
435 * parameter, iff the offset overlaps with an entry in the table.
437 * If the offset is not found in the table and "next_if_missing" is
438 * B_TRUE, then the entry nearest to the given offset will be returned,
439 * such that the entry's source offset is greater than the offset
440 * passed in (i.e. the "next" mapping entry in the table is returned, if
441 * the offset is missing from the table). If there are no entries whose
442 * source offset is greater than the passed in offset, NULL is returned.
444 static vdev_indirect_mapping_entry_phys_t *
445 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
448 ASSERT(vim->vim_phys->vimp_num_entries > 0);
450 vdev_indirect_mapping_entry_phys_t *entry;
452 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
456 * We don't define these inside of the while loop because we use
457 * their value in the case that offset isn't in the mapping.
462 while (last >= base) {
463 mid = base + ((last - base) >> 1);
465 entry = vdev_indirect_mapping_entry(vim, mid);
468 result = dva_mapping_overlap_compare(&offset, entry);
472 } else if (result < 0) {
482 * Given an indirect vdev and an extent on that vdev, it duplicates the
483 * physical entries of the indirect mapping that correspond to the extent
484 * to a new array and returns a pointer to it. In addition, copied_entries
485 * is populated with the number of mapping entries that were duplicated.
487 * Finally, since we are doing an allocation, it is up to the caller to
488 * free the array allocated in this function.
490 vdev_indirect_mapping_entry_phys_t *
491 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
492 uint64_t asize, uint64_t *copied_entries)
494 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
495 vdev_indirect_mapping_t *vim = vd->v_mapping;
496 uint64_t entries = 0;
498 vdev_indirect_mapping_entry_phys_t *first_mapping =
499 vdev_indirect_mapping_entry_for_offset(vim, offset);
500 ASSERT3P(first_mapping, !=, NULL);
502 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
504 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
505 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
506 uint64_t inner_size = MIN(asize, size - inner_offset);
508 offset += inner_size;
514 size_t copy_length = entries * sizeof (*first_mapping);
515 duplicate_mappings = malloc(copy_length);
516 if (duplicate_mappings != NULL)
517 bcopy(first_mapping, duplicate_mappings, copy_length);
521 *copied_entries = entries;
523 return (duplicate_mappings);
527 vdev_lookup_top(spa_t *spa, uint64_t vdev)
532 vlist = &spa->spa_root_vdev->v_children;
533 STAILQ_FOREACH(rvd, vlist, v_childlink)
534 if (rvd->v_id == vdev)
541 * This is a callback for vdev_indirect_remap() which allocates an
542 * indirect_split_t for each split segment and adds it to iv_splits.
545 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
546 uint64_t size, void *arg)
550 indirect_vsd_t *iv = zio->io_vsd;
552 if (vd->v_read == vdev_indirect_read)
555 if (vd->v_read == vdev_mirror_read)
558 indirect_split_t *is =
559 malloc(offsetof(indirect_split_t, is_child[n]));
561 zio->io_error = ENOMEM;
564 bzero(is, offsetof(indirect_split_t, is_child[n]));
568 is->is_split_offset = split_offset;
569 is->is_target_offset = offset;
573 * Note that we only consider multiple copies of the data for
574 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
575 * though they use the same ops as mirror, because there's only one
576 * "good" copy under the replacing/spare.
578 if (vd->v_read == vdev_mirror_read) {
582 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
583 is->is_child[i++].ic_vdev = kid;
586 is->is_child[0].ic_vdev = vd;
589 list_insert_tail(&iv->iv_splits, is);
593 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
596 spa_t *spa = vd->v_spa;
600 list_create(&stack, sizeof (remap_segment_t),
601 offsetof(remap_segment_t, rs_node));
603 rs = rs_alloc(vd, offset, asize, 0);
605 printf("vdev_indirect_remap: out of memory.\n");
606 zio->io_error = ENOMEM;
608 for (; rs != NULL; rs = list_remove_head(&stack)) {
609 vdev_t *v = rs->rs_vd;
610 uint64_t num_entries = 0;
611 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
612 vdev_indirect_mapping_entry_phys_t *mapping =
613 vdev_indirect_mapping_duplicate_adjacent_entries(v,
614 rs->rs_offset, rs->rs_asize, &num_entries);
616 if (num_entries == 0)
617 zio->io_error = ENOMEM;
619 for (uint64_t i = 0; i < num_entries; i++) {
620 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
621 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
622 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
623 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
624 uint64_t inner_offset = rs->rs_offset -
625 DVA_MAPPING_GET_SRC_OFFSET(m);
626 uint64_t inner_size =
627 MIN(rs->rs_asize, size - inner_offset);
628 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
630 if (dst_v->v_read == vdev_indirect_read) {
633 o = rs_alloc(dst_v, dst_offset + inner_offset,
634 inner_size, rs->rs_split_offset);
636 printf("vdev_indirect_remap: "
638 zio->io_error = ENOMEM;
642 list_insert_head(&stack, o);
644 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
645 dst_offset + inner_offset,
649 * vdev_indirect_gather_splits can have memory
650 * allocation error, we can not recover from it.
652 if (zio->io_error != 0)
654 rs->rs_offset += inner_size;
655 rs->rs_asize -= inner_size;
656 rs->rs_split_offset += inner_size;
661 if (zio->io_error != 0)
665 list_destroy(&stack);
669 vdev_indirect_map_free(zio_t *zio)
671 indirect_vsd_t *iv = zio->io_vsd;
672 indirect_split_t *is;
674 while ((is = list_head(&iv->iv_splits)) != NULL) {
675 for (int c = 0; c < is->is_children; c++) {
676 indirect_child_t *ic = &is->is_child[c];
679 list_remove(&iv->iv_splits, is);
686 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
687 off_t offset, size_t bytes)
690 spa_t *spa = vdev->v_spa;
692 indirect_split_t *first;
695 iv = calloc(1, sizeof(*iv));
699 list_create(&iv->iv_splits,
700 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
702 bzero(&zio, sizeof(zio));
704 zio.io_bp = (blkptr_t *)bp;
707 zio.io_offset = offset;
711 if (vdev->v_mapping == NULL) {
712 vdev_indirect_config_t *vic;
714 vic = &vdev->vdev_indirect_config;
715 vdev->v_mapping = vdev_indirect_mapping_open(spa,
716 spa->spa_mos, vic->vic_mapping_object);
719 vdev_indirect_remap(vdev, offset, bytes, &zio);
720 if (zio.io_error != 0)
721 return (zio.io_error);
723 first = list_head(&iv->iv_splits);
724 if (first->is_size == zio.io_size) {
726 * This is not a split block; we are pointing to the entire
727 * data, which will checksum the same as the original data.
728 * Pass the BP down so that the child i/o can verify the
729 * checksum, and try a different location if available
730 * (e.g. on a mirror).
732 * While this special case could be handled the same as the
733 * general (split block) case, doing it this way ensures
734 * that the vast majority of blocks on indirect vdevs
735 * (which are not split) are handled identically to blocks
736 * on non-indirect vdevs. This allows us to be less strict
737 * about performance in the general (but rare) case.
739 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
740 zio.io_data, first->is_target_offset, bytes);
742 iv->iv_split_block = B_TRUE;
744 * Read one copy of each split segment, from the
745 * top-level vdev. Since we don't know the
746 * checksum of each split individually, the child
747 * zio can't ensure that we get the right data.
748 * E.g. if it's a mirror, it will just read from a
749 * random (healthy) leaf vdev. We have to verify
750 * the checksum in vdev_indirect_io_done().
752 for (indirect_split_t *is = list_head(&iv->iv_splits);
753 is != NULL; is = list_next(&iv->iv_splits, is)) {
754 char *ptr = zio.io_data;
756 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
757 ptr + is->is_split_offset, is->is_target_offset,
760 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
766 vdev_indirect_map_free(&zio);
774 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
775 off_t offset, size_t bytes)
778 return (vdev_read_phys(vdev, bp, buf,
779 offset + VDEV_LABEL_START_SIZE, bytes));
783 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
784 void *buf __unused, off_t offset __unused, size_t bytes __unused)
791 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
792 off_t offset, size_t bytes)
798 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
799 if (kid->v_state != VDEV_STATE_HEALTHY)
801 rc = kid->v_read(kid, bp, buf, offset, bytes);
810 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
811 off_t offset, size_t bytes)
816 * Here we should have two kids:
817 * First one which is the one we are replacing and we can trust
818 * only this one to have valid data, but it might not be present.
819 * Second one is that one we are replacing with. It is most likely
820 * healthy, but we can't trust it has needed data, so we won't use it.
822 kid = STAILQ_FIRST(&vdev->v_children);
825 if (kid->v_state != VDEV_STATE_HEALTHY)
827 return (kid->v_read(kid, bp, buf, offset, bytes));
831 vdev_find(uint64_t guid)
835 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
836 if (vdev->v_guid == guid)
843 vdev_create(uint64_t guid, vdev_read_t *_read)
846 vdev_indirect_config_t *vic;
848 vdev = calloc(1, sizeof(vdev_t));
850 STAILQ_INIT(&vdev->v_children);
852 vdev->v_read = _read;
855 * root vdev has no read function, we use this fact to
856 * skip setting up data we do not need for root vdev.
857 * We only point root vdev from spa.
860 vic = &vdev->vdev_indirect_config;
861 vic->vic_prev_indirect_vdev = UINT64_MAX;
862 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
870 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
872 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
875 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
877 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
879 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
881 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
883 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
884 NULL, &is_degraded, NULL);
885 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
886 NULL, &isnt_present, NULL);
887 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
891 vdev->v_state = VDEV_STATE_OFFLINE;
892 else if (is_removed != 0)
893 vdev->v_state = VDEV_STATE_REMOVED;
894 else if (is_faulted != 0)
895 vdev->v_state = VDEV_STATE_FAULTED;
896 else if (is_degraded != 0)
897 vdev->v_state = VDEV_STATE_DEGRADED;
898 else if (isnt_present != 0)
899 vdev->v_state = VDEV_STATE_CANT_OPEN;
901 vdev->v_islog = is_log != 0;
905 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
907 uint64_t id, ashift, asize, nparity;
914 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
916 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
921 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
922 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
924 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
926 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
927 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
928 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
929 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
930 printf("ZFS: can only boot from disk, mirror, raidz1, "
931 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
935 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
936 vdev = vdev_create(guid, vdev_mirror_read);
937 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
938 vdev = vdev_create(guid, vdev_raidz_read);
939 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
940 vdev = vdev_create(guid, vdev_replacing_read);
941 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
942 vdev_indirect_config_t *vic;
944 vdev = vdev_create(guid, vdev_indirect_read);
946 vdev->v_state = VDEV_STATE_HEALTHY;
947 vic = &vdev->vdev_indirect_config;
950 ZPOOL_CONFIG_INDIRECT_OBJECT,
952 NULL, &vic->vic_mapping_object, NULL);
954 ZPOOL_CONFIG_INDIRECT_BIRTHS,
956 NULL, &vic->vic_births_object, NULL);
958 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
960 NULL, &vic->vic_prev_indirect_vdev, NULL);
962 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
963 vdev = vdev_create(guid, vdev_missing_read);
965 vdev = vdev_create(guid, vdev_disk_read);
971 vdev_set_initial_state(vdev, nvlist);
973 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
974 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
975 vdev->v_ashift = ashift;
977 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
978 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
979 vdev->v_psize = asize +
980 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
983 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
984 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
985 vdev->v_nparity = nparity;
987 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
988 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
989 char prefix[] = "/dev/";
991 len = strlen(prefix);
992 if (len < pathlen && memcmp(path, prefix, len) == 0) {
996 name = malloc(pathlen + 1);
997 bcopy(path, name, pathlen);
998 name[pathlen] = '\0';
1002 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1003 if (vdev->v_nparity < 1 ||
1004 vdev->v_nparity > 3) {
1005 printf("ZFS: invalid raidz parity: %d\n",
1009 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1010 vdev->v_nparity, id);
1012 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1014 vdev->v_name = name;
1021 * Find slot for vdev. We return either NULL to signal to use
1022 * STAILQ_INSERT_HEAD, or we return link element to be used with
1023 * STAILQ_INSERT_AFTER.
1026 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1028 vdev_t *v, *previous;
1030 if (STAILQ_EMPTY(&top_vdev->v_children))
1034 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1035 if (v->v_id > vdev->v_id)
1038 if (v->v_id == vdev->v_id)
1041 if (v->v_id < vdev->v_id)
1048 vdev_child_count(vdev_t *vdev)
1054 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1061 * Insert vdev into top_vdev children list. List is ordered by v_id.
1064 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1070 * The top level vdev can appear in random order, depending how
1071 * the firmware is presenting the disk devices.
1072 * However, we will insert vdev to create list ordered by v_id,
1073 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1074 * as STAILQ does not have insert before.
1076 previous = vdev_find_previous(top_vdev, vdev);
1078 if (previous == NULL) {
1079 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1080 } else if (previous->v_id == vdev->v_id) {
1082 * This vdev was configured from label config,
1083 * do not insert duplicate.
1087 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1091 count = vdev_child_count(top_vdev);
1092 if (top_vdev->v_nchildren < count)
1093 top_vdev->v_nchildren = count;
1097 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1099 vdev_t *top_vdev, *vdev;
1100 nvlist_t **kids = NULL;
1104 top_vdev = vdev_find(top_guid);
1105 if (top_vdev == NULL) {
1106 rc = vdev_init(top_guid, nvlist, &top_vdev);
1109 top_vdev->v_spa = spa;
1110 top_vdev->v_top = top_vdev;
1111 vdev_insert(spa->spa_root_vdev, top_vdev);
1114 /* Add children if there are any. */
1115 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1116 &nkids, &kids, NULL);
1118 for (int i = 0; i < nkids; i++) {
1121 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1122 DATA_TYPE_UINT64, NULL, &guid, NULL);
1126 rc = vdev_init(guid, kids[i], &vdev);
1131 vdev->v_top = top_vdev;
1132 vdev_insert(top_vdev, vdev);
1136 * When there are no children, nvlist_find() does return
1137 * error, reset it because leaf devices have no children.
1143 for (int i = 0; i < nkids; i++)
1144 nvlist_destroy(kids[i]);
1152 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1154 uint64_t pool_guid, top_guid;
1158 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1159 NULL, &pool_guid, NULL) ||
1160 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1161 NULL, &top_guid, NULL) ||
1162 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1163 NULL, &vdevs, NULL)) {
1164 printf("ZFS: can't find vdev details\n");
1168 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1169 nvlist_destroy(vdevs);
1174 vdev_set_state(vdev_t *vdev)
1180 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1181 vdev_set_state(kid);
1185 * A mirror or raidz is healthy if all its kids are healthy. A
1186 * mirror is degraded if any of its kids is healthy; a raidz
1187 * is degraded if at most nparity kids are offline.
1189 if (STAILQ_FIRST(&vdev->v_children)) {
1192 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1193 if (kid->v_state == VDEV_STATE_HEALTHY)
1198 if (bad_kids == 0) {
1199 vdev->v_state = VDEV_STATE_HEALTHY;
1201 if (vdev->v_read == vdev_mirror_read) {
1203 vdev->v_state = VDEV_STATE_DEGRADED;
1205 vdev->v_state = VDEV_STATE_OFFLINE;
1207 } else if (vdev->v_read == vdev_raidz_read) {
1208 if (bad_kids > vdev->v_nparity) {
1209 vdev->v_state = VDEV_STATE_OFFLINE;
1211 vdev->v_state = VDEV_STATE_DEGRADED;
1219 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1222 nvlist_t **kids = NULL;
1225 /* Update top vdev. */
1226 vdev = vdev_find(top_guid);
1228 vdev_set_initial_state(vdev, nvlist);
1230 /* Update children if there are any. */
1231 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1232 &nkids, &kids, NULL);
1234 for (int i = 0; i < nkids; i++) {
1237 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1238 DATA_TYPE_UINT64, NULL, &guid, NULL);
1242 vdev = vdev_find(guid);
1244 vdev_set_initial_state(vdev, kids[i]);
1250 for (int i = 0; i < nkids; i++)
1251 nvlist_destroy(kids[i]);
1259 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1261 uint64_t pool_guid, vdev_children;
1262 nvlist_t *vdevs = NULL, **kids = NULL;
1265 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1266 NULL, &pool_guid, NULL) ||
1267 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1268 NULL, &vdev_children, NULL) ||
1269 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1270 NULL, &vdevs, NULL)) {
1271 printf("ZFS: can't find vdev details\n");
1276 if (spa->spa_guid != pool_guid) {
1277 nvlist_destroy(vdevs);
1281 spa->spa_root_vdev->v_nchildren = vdev_children;
1283 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1284 &nkids, &kids, NULL);
1285 nvlist_destroy(vdevs);
1288 * MOS config has at least one child for root vdev.
1293 for (int i = 0; i < nkids; i++) {
1297 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1301 vdev = vdev_find(guid);
1303 * Top level vdev is missing, create it.
1306 rc = vdev_from_nvlist(spa, guid, kids[i]);
1308 rc = vdev_update_from_nvlist(guid, kids[i]);
1313 for (int i = 0; i < nkids; i++)
1314 nvlist_destroy(kids[i]);
1319 * Re-evaluate top-level vdev state.
1321 vdev_set_state(spa->spa_root_vdev);
1327 spa_find_by_guid(uint64_t guid)
1331 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1332 if (spa->spa_guid == guid)
1339 spa_find_by_name(const char *name)
1343 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1344 if (strcmp(spa->spa_name, name) == 0)
1351 spa_find_by_dev(struct zfs_devdesc *dev)
1354 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1357 if (dev->pool_guid == 0)
1358 return (STAILQ_FIRST(&zfs_pools));
1360 return (spa_find_by_guid(dev->pool_guid));
1364 spa_create(uint64_t guid, const char *name)
1368 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1370 if ((spa->spa_name = strdup(name)) == NULL) {
1374 spa->spa_uberblock = &spa->spa_uberblock_master;
1375 spa->spa_mos = &spa->spa_mos_master;
1376 spa->spa_guid = guid;
1377 spa->spa_root_vdev = vdev_create(guid, NULL);
1378 if (spa->spa_root_vdev == NULL) {
1379 free(spa->spa_name);
1383 spa->spa_root_vdev->v_name = strdup("root");
1384 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1390 state_name(vdev_state_t state)
1392 static const char *names[] = {
1402 return (names[state]);
1407 #define pager_printf printf
1412 pager_printf(const char *fmt, ...)
1417 va_start(args, fmt);
1418 vsnprintf(line, sizeof(line), fmt, args);
1420 return (pager_output(line));
1425 #define STATUS_FORMAT " %s %s\n"
1428 print_state(int indent, const char *name, vdev_state_t state)
1434 for (i = 0; i < indent; i++)
1437 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1441 vdev_status(vdev_t *vdev, int indent)
1446 if (vdev->v_islog) {
1447 (void) pager_output(" logs\n");
1451 ret = print_state(indent, vdev->v_name, vdev->v_state);
1455 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1456 ret = vdev_status(kid, indent + 1);
1464 spa_status(spa_t *spa)
1466 static char bootfs[ZFS_MAXNAMELEN];
1470 int good_kids, bad_kids, degraded_kids, ret;
1473 ret = pager_printf(" pool: %s\n", spa->spa_name);
1477 if (zfs_get_root(spa, &rootid) == 0 &&
1478 zfs_rlookup(spa, rootid, bootfs) == 0) {
1479 if (bootfs[0] == '\0')
1480 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1482 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1487 ret = pager_printf("config:\n\n");
1490 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1497 vlist = &spa->spa_root_vdev->v_children;
1498 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1499 if (vdev->v_state == VDEV_STATE_HEALTHY)
1501 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1507 state = VDEV_STATE_CLOSED;
1508 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1509 state = VDEV_STATE_HEALTHY;
1510 else if ((good_kids + degraded_kids) > 0)
1511 state = VDEV_STATE_DEGRADED;
1513 ret = print_state(0, spa->spa_name, state);
1517 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1518 ret = vdev_status(vdev, 1);
1526 spa_all_status(void)
1529 int first = 1, ret = 0;
1531 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1533 ret = pager_printf("\n");
1538 ret = spa_status(spa);
1546 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1548 uint64_t label_offset;
1550 if (l < VDEV_LABELS / 2)
1553 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1555 return (offset + l * sizeof (vdev_label_t) + label_offset);
1559 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1561 unsigned int seq1 = 0;
1562 unsigned int seq2 = 0;
1563 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1568 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1572 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1573 seq1 = MMP_SEQ(ub1);
1575 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1576 seq2 = MMP_SEQ(ub2);
1578 return (AVL_CMP(seq1, seq2));
1582 uberblock_verify(uberblock_t *ub)
1584 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1585 byteswap_uint64_array(ub, sizeof (uberblock_t));
1588 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1589 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1596 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1602 off = vdev_label_offset(vd->v_psize, l, offset);
1605 BP_SET_LSIZE(&bp, size);
1606 BP_SET_PSIZE(&bp, size);
1607 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1608 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1609 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1610 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1612 return (vdev_read_phys(vd, &bp, buf, off, size));
1616 * We do need to be sure we write to correct location.
1617 * Our vdev label does consist of 4 fields:
1618 * pad1 (8k), reserved.
1619 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1620 * vdev_phys (112k), checksummed
1621 * uberblock ring (128k), checksummed.
1623 * Since bootenv area may contain garbage, we can not reliably read it, as
1624 * we can get checksum errors.
1625 * Next best thing is vdev_phys - it is just after bootenv. It still may
1626 * be corrupted, but in such case we will miss this one write.
1629 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1631 uint64_t off, o_phys;
1633 size_t size = VDEV_PHYS_SIZE;
1636 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1637 off = vdev_label_offset(vd->v_psize, l, o_phys);
1639 /* off should be 8K from bootenv */
1640 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1647 /* Read vdev_phys */
1648 rc = vdev_label_read(vd, l, buf, o_phys, size);
1654 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1656 zio_checksum_info_t *ci;
1659 size_t size = VDEV_PAD_SIZE;
1662 if (vd->v_phys_write == NULL)
1665 off = vdev_label_offset(vd->v_psize, l, offset);
1667 rc = vdev_label_write_validate(vd, l, offset);
1672 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1673 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1674 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1675 ci->ci_func[0](be, size, NULL, &cksum);
1676 be->vbe_zbt.zec_cksum = cksum;
1678 return (vdev_write_phys(vd, be, off, size));
1682 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1687 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1688 if (kid->v_state != VDEV_STATE_HEALTHY)
1690 rc = vdev_write_bootenv_impl(kid, be);
1696 * Non-leaf vdevs do not have v_phys_write.
1698 if (vdev->v_phys_write == NULL)
1701 for (int l = 0; l < VDEV_LABELS; l++) {
1702 rc = vdev_label_write(vdev, l, be,
1703 offsetof(vdev_label_t, vl_be));
1705 printf("failed to write bootenv to %s label %d: %d\n",
1706 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1714 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1716 vdev_boot_envblock_t *be;
1721 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1725 nvp = vdev_read_bootenv(vdev);
1727 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1729 nvlist_destroy(nvp);
1732 be = calloc(1, sizeof(*be));
1736 be->vbe_version = version;
1740 * If there is no envmap, we will just wipe bootenv.
1742 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1743 be->vbe_bootenv, NULL);
1748 nv.nv_header = nvl->nv_header;
1749 nv.nv_asize = nvl->nv_asize;
1750 nv.nv_size = nvl->nv_size;
1752 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1753 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1754 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1755 rv = nvlist_export(&nv);
1764 be->vbe_version = htobe64(be->vbe_version);
1765 rv = vdev_write_bootenv_impl(vdev, be);
1772 * Read the bootenv area from pool label, return the nvlist from it.
1773 * We return from first successful read.
1776 vdev_read_bootenv(vdev_t *vdev)
1780 vdev_boot_envblock_t *be;
1785 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1786 if (kid->v_state != VDEV_STATE_HEALTHY)
1789 benv = vdev_read_bootenv(kid);
1794 be = malloc(sizeof (*be));
1799 for (int l = 0; l < VDEV_LABELS; l++) {
1800 rv = vdev_label_read(vdev, l, be,
1801 offsetof(vdev_label_t, vl_be),
1811 be->vbe_version = be64toh(be->vbe_version);
1812 switch (be->vbe_version) {
1815 * we have textual data in vbe_bootenv, create nvlist
1816 * with key "envmap".
1818 benv = nvlist_create(NV_UNIQUE_NAME);
1820 if (*be->vbe_bootenv == '\0') {
1821 nvlist_add_uint64(benv, BOOTENV_VERSION,
1825 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1826 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1827 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1832 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1836 command = (char *)be;
1839 /* Check for legacy zfsbootcfg command string */
1840 for (int i = 0; command[i] != '\0'; i++) {
1841 if (iscntrl(command[i])) {
1848 benv = nvlist_create(NV_UNIQUE_NAME);
1851 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1854 nvlist_add_uint64(benv, BOOTENV_VERSION,
1864 vdev_get_label_asize(nvlist_t *nvl)
1873 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1874 NULL, &vdevs, NULL) != 0)
1878 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1879 * For raidz, the asize is raw size of all children.
1881 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1882 NULL, &type, &len) != 0)
1885 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1886 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1887 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1890 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1891 NULL, &asize, NULL) != 0)
1894 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1898 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1899 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1905 for (int i = 0; i < nkids; i++)
1906 nvlist_destroy(kids[i]);
1910 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1912 nvlist_destroy(vdevs);
1917 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1920 uint64_t best_txg = 0;
1921 uint64_t label_txg = 0;
1923 nvlist_t *nvl = NULL, *tmp;
1926 label = malloc(sizeof (vdev_phys_t));
1930 for (int l = 0; l < VDEV_LABELS; l++) {
1931 if (vdev_label_read(vd, l, label,
1932 offsetof(vdev_label_t, vl_vdev_phys),
1933 sizeof (vdev_phys_t)))
1936 tmp = nvlist_import(label->vp_nvlist,
1937 sizeof(label->vp_nvlist));
1941 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1942 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1943 if (error != 0 || label_txg == 0) {
1944 nvlist_destroy(nvl);
1949 if (label_txg <= txg && label_txg > best_txg) {
1950 best_txg = label_txg;
1951 nvlist_destroy(nvl);
1956 * Use asize from pool config. We need this
1957 * because we can get bad value from BIOS.
1959 asize = vdev_get_label_asize(nvl);
1961 vd->v_psize = asize;
1964 nvlist_destroy(tmp);
1967 if (best_txg == 0) {
1968 nvlist_destroy(nvl);
1977 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1981 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1985 for (int l = 0; l < VDEV_LABELS; l++) {
1986 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1987 if (vdev_label_read(vd, l, buf,
1988 VDEV_UBERBLOCK_OFFSET(vd, n),
1989 VDEV_UBERBLOCK_SIZE(vd)))
1991 if (uberblock_verify(buf) != 0)
1994 if (vdev_uberblock_compare(buf, ub) > 0)
2002 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2010 uint64_t guid, vdev_children;
2011 uint64_t pool_txg, pool_guid;
2012 const char *pool_name;
2016 * Load the vdev label and figure out which
2017 * uberblock is most current.
2019 memset(&vtmp, 0, sizeof(vtmp));
2020 vtmp.v_phys_read = _read;
2021 vtmp.v_phys_write = _write;
2023 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2024 (uint64_t)sizeof (vdev_label_t));
2026 /* Test for minimum device size. */
2027 if (vtmp.v_psize < SPA_MINDEVSIZE)
2030 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2034 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2035 NULL, &val, NULL) != 0) {
2036 nvlist_destroy(nvl);
2040 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2041 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2042 (unsigned)val, (unsigned)SPA_VERSION);
2043 nvlist_destroy(nvl);
2047 /* Check ZFS features for read */
2048 rc = nvlist_check_features_for_read(nvl);
2050 nvlist_destroy(nvl);
2054 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2055 NULL, &val, NULL) != 0) {
2056 nvlist_destroy(nvl);
2060 if (val == POOL_STATE_DESTROYED) {
2061 /* We don't boot only from destroyed pools. */
2062 nvlist_destroy(nvl);
2066 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2067 NULL, &pool_txg, NULL) != 0 ||
2068 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2069 NULL, &pool_guid, NULL) != 0 ||
2070 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2071 NULL, &pool_name, &namelen) != 0) {
2073 * Cache and spare devices end up here - just ignore
2076 nvlist_destroy(nvl);
2081 * Create the pool if this is the first time we've seen it.
2083 spa = spa_find_by_guid(pool_guid);
2087 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2088 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2089 name = malloc(namelen + 1);
2091 nvlist_destroy(nvl);
2094 bcopy(pool_name, name, namelen);
2095 name[namelen] = '\0';
2096 spa = spa_create(pool_guid, name);
2099 nvlist_destroy(nvl);
2102 spa->spa_root_vdev->v_nchildren = vdev_children;
2104 if (pool_txg > spa->spa_txg)
2105 spa->spa_txg = pool_txg;
2108 * Get the vdev tree and create our in-core copy of it.
2109 * If we already have a vdev with this guid, this must
2110 * be some kind of alias (overlapping slices, dangerously dedicated
2113 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2114 NULL, &guid, NULL) != 0) {
2115 nvlist_destroy(nvl);
2118 vdev = vdev_find(guid);
2119 /* Has this vdev already been inited? */
2120 if (vdev && vdev->v_phys_read) {
2121 nvlist_destroy(nvl);
2125 rc = vdev_init_from_label(spa, nvl);
2126 nvlist_destroy(nvl);
2131 * We should already have created an incomplete vdev for this
2132 * vdev. Find it and initialise it with our read proc.
2134 vdev = vdev_find(guid);
2136 vdev->v_phys_read = _read;
2137 vdev->v_phys_write = _write;
2138 vdev->v_priv = priv;
2139 vdev->v_psize = vtmp.v_psize;
2141 * If no other state is set, mark vdev healthy.
2143 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2144 vdev->v_state = VDEV_STATE_HEALTHY;
2146 printf("ZFS: inconsistent nvlist contents\n");
2151 spa->spa_with_log = vdev->v_islog;
2154 * Re-evaluate top-level vdev state.
2156 vdev_set_state(vdev->v_top);
2159 * Ok, we are happy with the pool so far. Lets find
2160 * the best uberblock and then we can actually access
2161 * the contents of the pool.
2163 vdev_uberblock_load(vdev, spa->spa_uberblock);
2175 for (v = 0; v < 32; v++)
2182 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2185 zio_gbh_phys_t zio_gb;
2189 /* Artificial BP for gang block header. */
2191 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2192 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2193 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2194 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2195 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2196 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2198 /* Read gang header block using the artificial BP. */
2199 if (zio_read(spa, &gbh_bp, &zio_gb))
2203 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2204 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2206 if (BP_IS_HOLE(gbp))
2208 if (zio_read(spa, gbp, pbuf))
2210 pbuf += BP_GET_PSIZE(gbp);
2213 if (zio_checksum_verify(spa, bp, buf))
2219 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2221 int cpfunc = BP_GET_COMPRESS(bp);
2222 uint64_t align, size;
2227 * Process data embedded in block pointer
2229 if (BP_IS_EMBEDDED(bp)) {
2230 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2232 size = BPE_GET_PSIZE(bp);
2233 ASSERT(size <= BPE_PAYLOAD_SIZE);
2235 if (cpfunc != ZIO_COMPRESS_OFF)
2236 pbuf = malloc(size);
2243 decode_embedded_bp_compressed(bp, pbuf);
2246 if (cpfunc != ZIO_COMPRESS_OFF) {
2247 error = zio_decompress_data(cpfunc, pbuf,
2248 size, buf, BP_GET_LSIZE(bp));
2252 printf("ZFS: i/o error - unable to decompress "
2253 "block pointer data, error %d\n", error);
2259 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2260 const dva_t *dva = &bp->blk_dva[i];
2266 if (!dva->dva_word[0] && !dva->dva_word[1])
2269 vdevid = DVA_GET_VDEV(dva);
2270 offset = DVA_GET_OFFSET(dva);
2271 vlist = &spa->spa_root_vdev->v_children;
2272 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2273 if (vdev->v_id == vdevid)
2276 if (!vdev || !vdev->v_read)
2279 size = BP_GET_PSIZE(bp);
2280 if (vdev->v_read == vdev_raidz_read) {
2281 align = 1ULL << vdev->v_ashift;
2282 if (P2PHASE(size, align) != 0)
2283 size = P2ROUNDUP(size, align);
2285 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2286 pbuf = malloc(size);
2295 if (DVA_GET_GANG(dva))
2296 error = zio_read_gang(spa, bp, pbuf);
2298 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2300 if (cpfunc != ZIO_COMPRESS_OFF)
2301 error = zio_decompress_data(cpfunc, pbuf,
2302 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2303 else if (size != BP_GET_PSIZE(bp))
2304 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2306 printf("zio_read error: %d\n", error);
2314 printf("ZFS: i/o error - all block copies unavailable\n");
2320 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2321 void *buf, size_t buflen)
2323 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2324 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2325 int nlevels = dnode->dn_nlevels;
2328 if (bsize > SPA_MAXBLOCKSIZE) {
2329 printf("ZFS: I/O error - blocks larger than %llu are not "
2330 "supported\n", SPA_MAXBLOCKSIZE);
2335 * Note: bsize may not be a power of two here so we need to do an
2336 * actual divide rather than a bitshift.
2338 while (buflen > 0) {
2339 uint64_t bn = offset / bsize;
2340 int boff = offset % bsize;
2342 const blkptr_t *indbp;
2345 if (bn > dnode->dn_maxblkid)
2348 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2351 indbp = dnode->dn_blkptr;
2352 for (i = 0; i < nlevels; i++) {
2354 * Copy the bp from the indirect array so that
2355 * we can re-use the scratch buffer for multi-level
2358 ibn = bn >> ((nlevels - i - 1) * ibshift);
2359 ibn &= ((1 << ibshift) - 1);
2361 if (BP_IS_HOLE(&bp)) {
2362 memset(dnode_cache_buf, 0, bsize);
2365 rc = zio_read(spa, &bp, dnode_cache_buf);
2368 indbp = (const blkptr_t *) dnode_cache_buf;
2370 dnode_cache_obj = dnode;
2371 dnode_cache_bn = bn;
2375 * The buffer contains our data block. Copy what we
2376 * need from it and loop.
2379 if (i > buflen) i = buflen;
2380 memcpy(buf, &dnode_cache_buf[boff], i);
2381 buf = ((char *)buf) + i;
2390 * Lookup a value in a microzap directory.
2393 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2396 const mzap_ent_phys_t *mze;
2400 * Microzap objects use exactly one block. Read the whole
2403 chunks = size / MZAP_ENT_LEN - 1;
2404 for (i = 0; i < chunks; i++) {
2405 mze = &mz->mz_chunk[i];
2406 if (strcmp(mze->mze_name, name) == 0) {
2407 *value = mze->mze_value;
2416 * Compare a name with a zap leaf entry. Return non-zero if the name
2420 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2424 const zap_leaf_chunk_t *nc;
2427 namelen = zc->l_entry.le_name_numints;
2429 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2431 while (namelen > 0) {
2435 if (len > ZAP_LEAF_ARRAY_BYTES)
2436 len = ZAP_LEAF_ARRAY_BYTES;
2437 if (memcmp(p, nc->l_array.la_array, len))
2441 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2448 * Extract a uint64_t value from a zap leaf entry.
2451 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2453 const zap_leaf_chunk_t *vc;
2458 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2459 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2460 value = (value << 8) | p[i];
2467 stv(int len, void *addr, uint64_t value)
2471 *(uint8_t *)addr = value;
2474 *(uint16_t *)addr = value;
2477 *(uint32_t *)addr = value;
2480 *(uint64_t *)addr = value;
2486 * Extract a array from a zap leaf entry.
2489 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2490 uint64_t integer_size, uint64_t num_integers, void *buf)
2492 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2494 uint64_t *u64 = buf;
2496 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2497 int chunk = zc->l_entry.le_value_chunk;
2500 if (integer_size == 8 && len == 1) {
2501 *u64 = fzap_leaf_value(zl, zc);
2506 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2509 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2510 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2511 value = (value << 8) | la->la_array[i];
2513 if (byten == array_int_len) {
2514 stv(integer_size, p, value);
2522 chunk = la->la_next;
2527 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2530 switch (integer_size) {
2540 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2547 zap_leaf_free(zap_leaf_t *leaf)
2554 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2556 int bs = FZAP_BLOCK_SHIFT(zap);
2559 *lp = malloc(sizeof(**lp));
2564 (*lp)->l_phys = malloc(1 << bs);
2566 if ((*lp)->l_phys == NULL) {
2570 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2579 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2582 int bs = FZAP_BLOCK_SHIFT(zap);
2583 uint64_t blk = idx >> (bs - 3);
2584 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2588 buf = malloc(1 << zap->zap_block_shift);
2591 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2592 buf, 1 << zap->zap_block_shift);
2600 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2602 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2603 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2606 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2611 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2613 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2618 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2619 err = zap_idx_to_blk(zap, idx, &blk);
2622 return (zap_get_leaf_byblk(zap, blk, lp));
2625 #define CHAIN_END 0xffff /* end of the chunk chain */
2626 #define LEAF_HASH(l, h) \
2627 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2629 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2630 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2633 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2634 uint64_t integer_size, uint64_t num_integers, void *value)
2638 struct zap_leaf_entry *le;
2641 * Make sure this chunk matches our hash.
2643 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2644 zl->l_phys->l_hdr.lh_prefix !=
2645 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2649 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2650 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2651 zap_leaf_chunk_t *zc;
2652 uint16_t chunk = *chunkp;
2654 le = ZAP_LEAF_ENTRY(zl, chunk);
2655 if (le->le_hash != hash)
2657 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2658 if (fzap_name_equal(zl, zc, name)) {
2659 if (zc->l_entry.le_value_intlen > integer_size) {
2662 fzap_leaf_array(zl, zc, integer_size,
2663 num_integers, value);
2673 * Lookup a value in a fatzap directory.
2676 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2677 const char *name, uint64_t integer_size, uint64_t num_integers,
2680 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2686 if (zh->zap_magic != ZAP_MAGIC)
2689 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2693 z.zap_block_shift = ilog2(bsize);
2696 z.zap_dnode = dnode;
2698 hash = zap_hash(zh->zap_salt, name);
2699 rc = zap_deref_leaf(&z, hash, &zl);
2703 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2710 * Lookup a name in a zap object and return its value as a uint64_t.
2713 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2714 uint64_t integer_size, uint64_t num_integers, void *value)
2718 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2724 rc = dnode_read(spa, dnode, 0, zap, size);
2728 switch (zap->zap_block_type) {
2730 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2733 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2734 num_integers, value);
2737 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2738 zap->zap_block_type);
2747 * List a microzap directory.
2750 mzap_list(const mzap_phys_t *mz, size_t size,
2751 int (*callback)(const char *, uint64_t))
2753 const mzap_ent_phys_t *mze;
2757 * Microzap objects use exactly one block. Read the whole
2761 chunks = size / MZAP_ENT_LEN - 1;
2762 for (i = 0; i < chunks; i++) {
2763 mze = &mz->mz_chunk[i];
2764 if (mze->mze_name[0]) {
2765 rc = callback(mze->mze_name, mze->mze_value);
2775 * List a fatzap directory.
2778 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2779 int (*callback)(const char *, uint64_t))
2781 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2786 if (zh->zap_magic != ZAP_MAGIC)
2789 z.zap_block_shift = ilog2(bsize);
2793 * This assumes that the leaf blocks start at block 1. The
2794 * documentation isn't exactly clear on this.
2797 zl.l_bs = z.zap_block_shift;
2798 zl.l_phys = malloc(bsize);
2799 if (zl.l_phys == NULL)
2802 for (i = 0; i < zh->zap_num_leafs; i++) {
2803 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2807 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2812 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2813 zap_leaf_chunk_t *zc, *nc;
2816 zc = &ZAP_LEAF_CHUNK(&zl, j);
2817 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2819 namelen = zc->l_entry.le_name_numints;
2820 if (namelen > sizeof(name))
2821 namelen = sizeof(name);
2824 * Paste the name back together.
2826 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2828 while (namelen > 0) {
2831 if (len > ZAP_LEAF_ARRAY_BYTES)
2832 len = ZAP_LEAF_ARRAY_BYTES;
2833 memcpy(p, nc->l_array.la_array, len);
2836 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2840 * Assume the first eight bytes of the value are
2843 value = fzap_leaf_value(&zl, zc);
2845 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2846 rc = callback((const char *)name, value);
2858 static int zfs_printf(const char *name, uint64_t value __unused)
2861 printf("%s\n", name);
2867 * List a zap directory.
2870 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2873 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2880 rc = dnode_read(spa, dnode, 0, zap, size);
2882 if (zap->zap_block_type == ZBT_MICRO)
2883 rc = mzap_list((const mzap_phys_t *)zap, size,
2886 rc = fzap_list(spa, dnode, zap, zfs_printf);
2893 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2894 dnode_phys_t *dnode)
2898 offset = objnum * sizeof(dnode_phys_t);
2899 return dnode_read(spa, &os->os_meta_dnode, offset,
2900 dnode, sizeof(dnode_phys_t));
2904 * Lookup a name in a microzap directory.
2907 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2909 const mzap_ent_phys_t *mze;
2913 * Microzap objects use exactly one block. Read the whole
2916 chunks = size / MZAP_ENT_LEN - 1;
2917 for (i = 0; i < chunks; i++) {
2918 mze = &mz->mz_chunk[i];
2919 if (value == mze->mze_value) {
2920 strcpy(name, mze->mze_name);
2929 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2932 const zap_leaf_chunk_t *nc;
2935 namelen = zc->l_entry.le_name_numints;
2937 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2939 while (namelen > 0) {
2942 if (len > ZAP_LEAF_ARRAY_BYTES)
2943 len = ZAP_LEAF_ARRAY_BYTES;
2944 memcpy(p, nc->l_array.la_array, len);
2947 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2954 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2955 char *name, uint64_t value)
2957 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2962 if (zh->zap_magic != ZAP_MAGIC)
2965 z.zap_block_shift = ilog2(bsize);
2969 * This assumes that the leaf blocks start at block 1. The
2970 * documentation isn't exactly clear on this.
2973 zl.l_bs = z.zap_block_shift;
2974 zl.l_phys = malloc(bsize);
2975 if (zl.l_phys == NULL)
2978 for (i = 0; i < zh->zap_num_leafs; i++) {
2979 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2981 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
2985 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2986 zap_leaf_chunk_t *zc;
2988 zc = &ZAP_LEAF_CHUNK(&zl, j);
2989 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2991 if (zc->l_entry.le_value_intlen != 8 ||
2992 zc->l_entry.le_value_numints != 1)
2995 if (fzap_leaf_value(&zl, zc) == value) {
2996 fzap_name_copy(&zl, zc, name);
3009 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3013 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3020 rc = dnode_read(spa, dnode, 0, zap, size);
3022 if (zap->zap_block_type == ZBT_MICRO)
3023 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3026 rc = fzap_rlookup(spa, dnode, zap, name, value);
3033 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3036 char component[256];
3037 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3038 dnode_phys_t child_dir_zap, dataset, dir, parent;
3040 dsl_dataset_phys_t *ds;
3044 p = &name[sizeof(name) - 1];
3047 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3048 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3051 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3052 dir_obj = ds->ds_dir_obj;
3055 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3057 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3059 /* Actual loop condition. */
3060 parent_obj = dd->dd_parent_obj;
3061 if (parent_obj == 0)
3064 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3067 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3068 child_dir_zapobj = dd->dd_child_dir_zapobj;
3069 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3070 &child_dir_zap) != 0)
3072 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3075 len = strlen(component);
3077 memcpy(p, component, len);
3081 /* Actual loop iteration. */
3082 dir_obj = parent_obj;
3093 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3096 uint64_t dir_obj, child_dir_zapobj;
3097 dnode_phys_t child_dir_zap, dir;
3101 if (objset_get_dnode(spa, spa->spa_mos,
3102 DMU_POOL_DIRECTORY_OBJECT, &dir))
3104 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3110 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3112 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3116 /* Actual loop condition #1. */
3122 memcpy(element, p, q - p);
3123 element[q - p] = '\0';
3130 child_dir_zapobj = dd->dd_child_dir_zapobj;
3131 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3132 &child_dir_zap) != 0)
3135 /* Actual loop condition #2. */
3136 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3141 *objnum = dd->dd_head_dataset_obj;
3147 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3149 uint64_t dir_obj, child_dir_zapobj;
3150 dnode_phys_t child_dir_zap, dir, dataset;
3151 dsl_dataset_phys_t *ds;
3154 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3155 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3158 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3159 dir_obj = ds->ds_dir_obj;
3161 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3162 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3165 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3167 child_dir_zapobj = dd->dd_child_dir_zapobj;
3168 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3169 &child_dir_zap) != 0) {
3170 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3174 return (zap_list(spa, &child_dir_zap) != 0);
3178 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3179 int (*callback)(const char *, uint64_t))
3181 uint64_t dir_obj, child_dir_zapobj;
3182 dnode_phys_t child_dir_zap, dir, dataset;
3183 dsl_dataset_phys_t *ds;
3189 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3191 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3194 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3195 dir_obj = ds->ds_dir_obj;
3197 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3199 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3202 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3204 child_dir_zapobj = dd->dd_child_dir_zapobj;
3205 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3208 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3212 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3215 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3219 if (zap->zap_block_type == ZBT_MICRO)
3220 err = mzap_list((const mzap_phys_t *)zap, size,
3223 err = fzap_list(spa, &child_dir_zap, zap, callback);
3234 * Find the object set given the object number of its dataset object
3235 * and return its details in *objset
3238 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3240 dnode_phys_t dataset;
3241 dsl_dataset_phys_t *ds;
3243 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3244 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3248 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3249 if (zio_read(spa, &ds->ds_bp, objset)) {
3250 printf("ZFS: can't read object set for dataset %ju\n",
3259 * Find the object set pointed to by the BOOTFS property or the root
3260 * dataset if there is none and return its details in *objset
3263 zfs_get_root(const spa_t *spa, uint64_t *objid)
3265 dnode_phys_t dir, propdir;
3266 uint64_t props, bootfs, root;
3271 * Start with the MOS directory object.
3273 if (objset_get_dnode(spa, spa->spa_mos,
3274 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3275 printf("ZFS: can't read MOS object directory\n");
3280 * Lookup the pool_props and see if we can find a bootfs.
3282 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3283 sizeof(props), 1, &props) == 0 &&
3284 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3285 zap_lookup(spa, &propdir, "bootfs",
3286 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3291 * Lookup the root dataset directory
3293 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3294 sizeof(root), 1, &root) ||
3295 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3296 printf("ZFS: can't find root dsl_dir\n");
3301 * Use the information from the dataset directory's bonus buffer
3302 * to find the dataset object and from that the object set itself.
3304 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3305 *objid = dd->dd_head_dataset_obj;
3310 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3316 * Find the root object set if not explicitly provided
3318 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3319 printf("ZFS: can't find root filesystem\n");
3323 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3324 printf("ZFS: can't open root filesystem\n");
3328 mount->rootobj = rootobj;
3334 * callback function for feature name checks.
3337 check_feature(const char *name, uint64_t value)
3343 if (name[0] == '\0')
3346 for (i = 0; features_for_read[i] != NULL; i++) {
3347 if (strcmp(name, features_for_read[i]) == 0)
3350 printf("ZFS: unsupported feature: %s\n", name);
3355 * Checks whether the MOS features that are active are supported.
3358 check_mos_features(const spa_t *spa)
3366 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3369 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3370 sizeof (objnum), 1, &objnum)) != 0) {
3372 * It is older pool without features. As we have already
3373 * tested the label, just return without raising the error.
3378 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3381 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3384 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3389 if (dnode_read(spa, &dir, 0, zap, size)) {
3394 if (zap->zap_block_type == ZBT_MICRO)
3395 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3397 rc = fzap_list(spa, &dir, zap, check_feature);
3404 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3412 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3414 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3415 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3419 if (dir.dn_bonuslen != sizeof (uint64_t))
3422 size = *(uint64_t *)DN_BONUS(&dir);
3427 rc = dnode_read(spa, &dir, 0, nv, size);
3433 *value = nvlist_import(nv, size);
3439 zfs_spa_init(spa_t *spa)
3441 struct uberblock checkpoint;
3443 uint64_t config_object;
3447 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3448 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3451 if (spa->spa_mos->os_type != DMU_OST_META) {
3452 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3456 if (objset_get_dnode(spa, &spa->spa_mos_master,
3457 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3458 printf("ZFS: failed to read pool %s directory object\n",
3462 /* this is allowed to fail, older pools do not have salt */
3463 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3464 sizeof (spa->spa_cksum_salt.zcs_bytes),
3465 spa->spa_cksum_salt.zcs_bytes);
3467 rc = check_mos_features(spa);
3469 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3473 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3474 sizeof (config_object), 1, &config_object);
3476 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3479 rc = load_nvlist(spa, config_object, &nvlist);
3483 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3484 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3486 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3487 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3488 sizeof(checkpoint));
3489 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3490 &spa->spa_mos_checkpoint)) {
3491 printf("ZFS: can not read checkpoint data.\n");
3497 * Update vdevs from MOS config. Note, we do skip encoding bytes
3498 * here. See also vdev_label_read_config().
3500 rc = vdev_init_from_nvlist(spa, nvlist);
3501 nvlist_destroy(nvlist);
3506 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3509 if (dn->dn_bonustype != DMU_OT_SA) {
3510 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3512 sb->st_mode = zp->zp_mode;
3513 sb->st_uid = zp->zp_uid;
3514 sb->st_gid = zp->zp_gid;
3515 sb->st_size = zp->zp_size;
3517 sa_hdr_phys_t *sahdrp;
3522 if (dn->dn_bonuslen != 0)
3523 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3525 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3526 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3529 size = BP_GET_LSIZE(bp);
3534 error = zio_read(spa, bp, buf);
3545 hdrsize = SA_HDR_SIZE(sahdrp);
3546 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3548 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3550 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3552 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3561 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3565 if (dn->dn_bonustype == DMU_OT_SA) {
3566 sa_hdr_phys_t *sahdrp = NULL;
3572 if (dn->dn_bonuslen != 0) {
3573 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3577 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3579 bp = DN_SPILL_BLKPTR(dn);
3581 size = BP_GET_LSIZE(bp);
3586 rc = zio_read(spa, bp, buf);
3593 hdrsize = SA_HDR_SIZE(sahdrp);
3594 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3595 memcpy(path, p, psize);
3600 * Second test is purely to silence bogus compiler
3601 * warning about accessing past the end of dn_bonus.
3603 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3604 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3605 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3607 rc = dnode_read(spa, dn, 0, path, psize);
3614 STAILQ_ENTRY(obj_list) entry;
3618 * Lookup a file and return its dnode.
3621 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3630 int symlinks_followed = 0;
3632 struct obj_list *entry, *tentry;
3633 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3636 if (mount->objset.os_type != DMU_OST_ZFS) {
3637 printf("ZFS: unexpected object set type %ju\n",
3638 (uintmax_t)mount->objset.os_type);
3642 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3646 * Get the root directory dnode.
3648 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3654 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3659 entry->objnum = objnum;
3660 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3662 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3668 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3677 while (*q != '\0' && *q != '/')
3681 if (p + 1 == q && p[0] == '.') {
3686 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3688 if (STAILQ_FIRST(&on_cache) ==
3689 STAILQ_LAST(&on_cache, obj_list, entry)) {
3693 entry = STAILQ_FIRST(&on_cache);
3694 STAILQ_REMOVE_HEAD(&on_cache, entry);
3696 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3699 if (q - p + 1 > sizeof(element)) {
3703 memcpy(element, p, q - p);
3707 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3709 if (!S_ISDIR(sb.st_mode)) {
3714 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3717 objnum = ZFS_DIRENT_OBJ(objnum);
3719 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3723 entry->objnum = objnum;
3724 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3725 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3730 * Check for symlink.
3732 rc = zfs_dnode_stat(spa, &dn, &sb);
3735 if (S_ISLNK(sb.st_mode)) {
3736 if (symlinks_followed > 10) {
3740 symlinks_followed++;
3743 * Read the link value and copy the tail of our
3744 * current path onto the end.
3746 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3750 strcpy(&path[sb.st_size], p);
3752 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3757 * Restart with the new path, starting either at
3758 * the root or at the parent depending whether or
3759 * not the link is relative.
3763 while (STAILQ_FIRST(&on_cache) !=
3764 STAILQ_LAST(&on_cache, obj_list, entry)) {
3765 entry = STAILQ_FIRST(&on_cache);
3766 STAILQ_REMOVE_HEAD(&on_cache, entry);
3770 entry = STAILQ_FIRST(&on_cache);
3771 STAILQ_REMOVE_HEAD(&on_cache, entry);
3774 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3780 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)