2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Stand-alone ZFS file reader.
35 #include <sys/endian.h>
37 #include <sys/stdint.h>
39 #include <sys/zfs_bootenv.h>
40 #include <machine/_inttypes.h>
46 extern int zstd_init(void);
54 STAILQ_ENTRY(zfsmount) next;
57 typedef STAILQ_HEAD(zfs_mnt_list, zfsmount) zfs_mnt_list_t;
58 static zfs_mnt_list_t zfsmount = STAILQ_HEAD_INITIALIZER(zfsmount);
61 * The indirect_child_t represents the vdev that we will read from, when we
62 * need to read all copies of the data (e.g. for scrub or reconstruction).
63 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
64 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
65 * ic_vdev is a child of the mirror.
67 typedef struct indirect_child {
73 * The indirect_split_t represents one mapped segment of an i/o to the
74 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
75 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
76 * For split blocks, there will be several of these.
78 typedef struct indirect_split {
79 list_node_t is_node; /* link on iv_splits */
82 * is_split_offset is the offset into the i/o.
83 * This is the sum of the previous splits' is_size's.
85 uint64_t is_split_offset;
87 vdev_t *is_vdev; /* top-level vdev */
88 uint64_t is_target_offset; /* offset on is_vdev */
90 int is_children; /* number of entries in is_child[] */
93 * is_good_child is the child that we are currently using to
94 * attempt reconstruction.
98 indirect_child_t is_child[1]; /* variable-length */
102 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
103 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
105 typedef struct indirect_vsd {
106 boolean_t iv_split_block;
107 boolean_t iv_reconstruct;
109 list_t iv_splits; /* list of indirect_split_t's */
113 * List of all vdevs, chained through v_alllink.
115 static vdev_list_t zfs_vdevs;
118 * List of ZFS features supported for read
120 static const char *features_for_read[] = {
121 "com.datto:bookmark_v2",
122 "com.datto:encryption",
123 "com.datto:resilver_defer",
124 "com.delphix:bookmark_written",
125 "com.delphix:device_removal",
126 "com.delphix:embedded_data",
127 "com.delphix:extensible_dataset",
128 "com.delphix:head_errlog",
129 "com.delphix:hole_birth",
130 "com.delphix:obsolete_counts",
131 "com.delphix:spacemap_histogram",
132 "com.delphix:spacemap_v2",
133 "com.delphix:zpool_checkpoint",
134 "com.intel:allocation_classes",
135 "com.joyent:multi_vdev_crash_dump",
136 "org.freebsd:zstd_compress",
137 "org.illumos:lz4_compress",
138 "org.illumos:sha512",
140 "org.open-zfs:large_blocks",
141 "org.openzfs:blake3",
142 "org.zfsonlinux:allocation_classes",
143 "org.zfsonlinux:large_dnode",
148 * List of all pools, chained through spa_link.
150 static spa_list_t zfs_pools;
152 static const dnode_phys_t *dnode_cache_obj;
153 static uint64_t dnode_cache_bn;
154 static char *dnode_cache_buf;
156 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
157 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
158 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
159 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
160 const char *name, uint64_t integer_size, uint64_t num_integers,
162 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
164 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
166 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
168 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
169 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
171 vdev_indirect_mapping_entry_phys_t *
172 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
173 uint64_t, uint64_t *);
178 STAILQ_INIT(&zfs_vdevs);
179 STAILQ_INIT(&zfs_pools);
181 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
190 nvlist_check_features_for_read(nvlist_t *nvl)
192 nvlist_t *features = NULL;
195 nv_string_t *nvp_name;
198 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
199 DATA_TYPE_NVLIST, NULL, &features, NULL);
202 break; /* Continue with checks */
205 return (0); /* All features are disabled */
208 return (rc); /* Error while reading nvlist */
211 data = (nvs_data_t *)features->nv_data;
212 nvp = &data->nvl_pair; /* first pair in nvlist */
214 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
217 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
220 for (i = 0; features_for_read[i] != NULL; i++) {
221 if (memcmp(nvp_name->nv_data, features_for_read[i],
222 nvp_name->nv_size) == 0) {
229 printf("ZFS: unsupported feature: %.*s\n",
230 nvp_name->nv_size, nvp_name->nv_data);
233 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
235 nvlist_destroy(features);
241 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
242 off_t offset, size_t size)
247 if (vdev->v_phys_read == NULL)
251 psize = BP_GET_PSIZE(bp);
256 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
259 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
266 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
268 if (vdev->v_phys_write == NULL)
271 return (vdev->v_phys_write(vdev, offset, buf, size));
274 typedef struct remap_segment {
278 uint64_t rs_split_offset;
282 static remap_segment_t *
283 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
285 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
289 rs->rs_offset = offset;
290 rs->rs_asize = asize;
291 rs->rs_split_offset = split_offset;
297 vdev_indirect_mapping_t *
298 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
299 uint64_t mapping_object)
301 vdev_indirect_mapping_t *vim;
302 vdev_indirect_mapping_phys_t *vim_phys;
305 vim = calloc(1, sizeof (*vim));
309 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
310 if (vim->vim_dn == NULL) {
315 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
323 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
324 if (vim->vim_phys == NULL) {
330 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
331 *vim->vim_phys = *vim_phys;
333 vim->vim_objset = os;
334 vim->vim_object = mapping_object;
335 vim->vim_entries = NULL;
337 vim->vim_havecounts =
338 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
344 * Compare an offset with an indirect mapping entry; there are three
345 * possible scenarios:
347 * 1. The offset is "less than" the mapping entry; meaning the
348 * offset is less than the source offset of the mapping entry. In
349 * this case, there is no overlap between the offset and the
350 * mapping entry and -1 will be returned.
352 * 2. The offset is "greater than" the mapping entry; meaning the
353 * offset is greater than the mapping entry's source offset plus
354 * the entry's size. In this case, there is no overlap between
355 * the offset and the mapping entry and 1 will be returned.
357 * NOTE: If the offset is actually equal to the entry's offset
358 * plus size, this is considered to be "greater" than the entry,
359 * and this case applies (i.e. 1 will be returned). Thus, the
360 * entry's "range" can be considered to be inclusive at its
361 * start, but exclusive at its end: e.g. [src, src + size).
363 * 3. The last case to consider is if the offset actually falls
364 * within the mapping entry's range. If this is the case, the
365 * offset is considered to be "equal to" the mapping entry and
366 * 0 will be returned.
368 * NOTE: If the offset is equal to the entry's source offset,
369 * this case applies and 0 will be returned. If the offset is
370 * equal to the entry's source plus its size, this case does
371 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
375 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
377 const uint64_t *key = v_key;
378 const vdev_indirect_mapping_entry_phys_t *array_elem =
380 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
382 if (*key < src_offset) {
384 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
392 * Return array entry.
394 static vdev_indirect_mapping_entry_phys_t *
395 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
401 if (vim->vim_phys->vimp_num_entries == 0)
404 if (vim->vim_entries == NULL) {
407 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
408 size = vim->vim_phys->vimp_num_entries *
409 sizeof (*vim->vim_entries);
411 size = bsize / sizeof (*vim->vim_entries);
412 size *= sizeof (*vim->vim_entries);
414 vim->vim_entries = malloc(size);
415 if (vim->vim_entries == NULL)
417 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
418 offset = index * sizeof (*vim->vim_entries);
421 /* We have data in vim_entries */
423 if (index >= vim->vim_entry_offset &&
424 index <= vim->vim_entry_offset + vim->vim_num_entries) {
425 index -= vim->vim_entry_offset;
426 return (&vim->vim_entries[index]);
428 offset = index * sizeof (*vim->vim_entries);
431 vim->vim_entry_offset = index;
432 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
433 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
436 /* Read error, invalidate vim_entries. */
437 free(vim->vim_entries);
438 vim->vim_entries = NULL;
441 index -= vim->vim_entry_offset;
442 return (&vim->vim_entries[index]);
446 * Returns the mapping entry for the given offset.
448 * It's possible that the given offset will not be in the mapping table
449 * (i.e. no mapping entries contain this offset), in which case, the
450 * return value depends on the "next_if_missing" parameter.
452 * If the offset is not found in the table and "next_if_missing" is
453 * B_FALSE, then NULL will always be returned. The behavior is intended
454 * to allow consumers to get the entry corresponding to the offset
455 * parameter, iff the offset overlaps with an entry in the table.
457 * If the offset is not found in the table and "next_if_missing" is
458 * B_TRUE, then the entry nearest to the given offset will be returned,
459 * such that the entry's source offset is greater than the offset
460 * passed in (i.e. the "next" mapping entry in the table is returned, if
461 * the offset is missing from the table). If there are no entries whose
462 * source offset is greater than the passed in offset, NULL is returned.
464 static vdev_indirect_mapping_entry_phys_t *
465 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
468 ASSERT(vim->vim_phys->vimp_num_entries > 0);
470 vdev_indirect_mapping_entry_phys_t *entry;
472 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
476 * We don't define these inside of the while loop because we use
477 * their value in the case that offset isn't in the mapping.
482 while (last >= base) {
483 mid = base + ((last - base) >> 1);
485 entry = vdev_indirect_mapping_entry(vim, mid);
488 result = dva_mapping_overlap_compare(&offset, entry);
492 } else if (result < 0) {
502 * Given an indirect vdev and an extent on that vdev, it duplicates the
503 * physical entries of the indirect mapping that correspond to the extent
504 * to a new array and returns a pointer to it. In addition, copied_entries
505 * is populated with the number of mapping entries that were duplicated.
507 * Finally, since we are doing an allocation, it is up to the caller to
508 * free the array allocated in this function.
510 vdev_indirect_mapping_entry_phys_t *
511 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
512 uint64_t asize, uint64_t *copied_entries)
514 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
515 vdev_indirect_mapping_t *vim = vd->v_mapping;
516 uint64_t entries = 0;
518 vdev_indirect_mapping_entry_phys_t *first_mapping =
519 vdev_indirect_mapping_entry_for_offset(vim, offset);
520 ASSERT3P(first_mapping, !=, NULL);
522 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
524 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
525 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
526 uint64_t inner_size = MIN(asize, size - inner_offset);
528 offset += inner_size;
534 size_t copy_length = entries * sizeof (*first_mapping);
535 duplicate_mappings = malloc(copy_length);
536 if (duplicate_mappings != NULL)
537 bcopy(first_mapping, duplicate_mappings, copy_length);
541 *copied_entries = entries;
543 return (duplicate_mappings);
547 vdev_lookup_top(spa_t *spa, uint64_t vdev)
552 vlist = &spa->spa_root_vdev->v_children;
553 STAILQ_FOREACH(rvd, vlist, v_childlink)
554 if (rvd->v_id == vdev)
561 * This is a callback for vdev_indirect_remap() which allocates an
562 * indirect_split_t for each split segment and adds it to iv_splits.
565 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
566 uint64_t size, void *arg)
570 indirect_vsd_t *iv = zio->io_vsd;
572 if (vd->v_read == vdev_indirect_read)
575 if (vd->v_read == vdev_mirror_read)
578 indirect_split_t *is =
579 malloc(offsetof(indirect_split_t, is_child[n]));
581 zio->io_error = ENOMEM;
584 bzero(is, offsetof(indirect_split_t, is_child[n]));
588 is->is_split_offset = split_offset;
589 is->is_target_offset = offset;
593 * Note that we only consider multiple copies of the data for
594 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
595 * though they use the same ops as mirror, because there's only one
596 * "good" copy under the replacing/spare.
598 if (vd->v_read == vdev_mirror_read) {
602 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
603 is->is_child[i++].ic_vdev = kid;
606 is->is_child[0].ic_vdev = vd;
609 list_insert_tail(&iv->iv_splits, is);
613 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
616 spa_t *spa = vd->v_spa;
620 list_create(&stack, sizeof (remap_segment_t),
621 offsetof(remap_segment_t, rs_node));
623 rs = rs_alloc(vd, offset, asize, 0);
625 printf("vdev_indirect_remap: out of memory.\n");
626 zio->io_error = ENOMEM;
628 for (; rs != NULL; rs = list_remove_head(&stack)) {
629 vdev_t *v = rs->rs_vd;
630 uint64_t num_entries = 0;
631 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
632 vdev_indirect_mapping_entry_phys_t *mapping =
633 vdev_indirect_mapping_duplicate_adjacent_entries(v,
634 rs->rs_offset, rs->rs_asize, &num_entries);
636 if (num_entries == 0)
637 zio->io_error = ENOMEM;
639 for (uint64_t i = 0; i < num_entries; i++) {
640 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
641 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
642 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
643 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
644 uint64_t inner_offset = rs->rs_offset -
645 DVA_MAPPING_GET_SRC_OFFSET(m);
646 uint64_t inner_size =
647 MIN(rs->rs_asize, size - inner_offset);
648 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
650 if (dst_v->v_read == vdev_indirect_read) {
653 o = rs_alloc(dst_v, dst_offset + inner_offset,
654 inner_size, rs->rs_split_offset);
656 printf("vdev_indirect_remap: "
658 zio->io_error = ENOMEM;
662 list_insert_head(&stack, o);
664 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
665 dst_offset + inner_offset,
669 * vdev_indirect_gather_splits can have memory
670 * allocation error, we can not recover from it.
672 if (zio->io_error != 0)
674 rs->rs_offset += inner_size;
675 rs->rs_asize -= inner_size;
676 rs->rs_split_offset += inner_size;
681 if (zio->io_error != 0)
685 list_destroy(&stack);
689 vdev_indirect_map_free(zio_t *zio)
691 indirect_vsd_t *iv = zio->io_vsd;
692 indirect_split_t *is;
694 while ((is = list_head(&iv->iv_splits)) != NULL) {
695 for (int c = 0; c < is->is_children; c++) {
696 indirect_child_t *ic = &is->is_child[c];
699 list_remove(&iv->iv_splits, is);
706 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
707 off_t offset, size_t bytes)
710 spa_t *spa = vdev->v_spa;
712 indirect_split_t *first;
715 iv = calloc(1, sizeof(*iv));
719 list_create(&iv->iv_splits,
720 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
722 bzero(&zio, sizeof(zio));
724 zio.io_bp = (blkptr_t *)bp;
727 zio.io_offset = offset;
731 if (vdev->v_mapping == NULL) {
732 vdev_indirect_config_t *vic;
734 vic = &vdev->vdev_indirect_config;
735 vdev->v_mapping = vdev_indirect_mapping_open(spa,
736 spa->spa_mos, vic->vic_mapping_object);
739 vdev_indirect_remap(vdev, offset, bytes, &zio);
740 if (zio.io_error != 0)
741 return (zio.io_error);
743 first = list_head(&iv->iv_splits);
744 if (first->is_size == zio.io_size) {
746 * This is not a split block; we are pointing to the entire
747 * data, which will checksum the same as the original data.
748 * Pass the BP down so that the child i/o can verify the
749 * checksum, and try a different location if available
750 * (e.g. on a mirror).
752 * While this special case could be handled the same as the
753 * general (split block) case, doing it this way ensures
754 * that the vast majority of blocks on indirect vdevs
755 * (which are not split) are handled identically to blocks
756 * on non-indirect vdevs. This allows us to be less strict
757 * about performance in the general (but rare) case.
759 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
760 zio.io_data, first->is_target_offset, bytes);
762 iv->iv_split_block = B_TRUE;
764 * Read one copy of each split segment, from the
765 * top-level vdev. Since we don't know the
766 * checksum of each split individually, the child
767 * zio can't ensure that we get the right data.
768 * E.g. if it's a mirror, it will just read from a
769 * random (healthy) leaf vdev. We have to verify
770 * the checksum in vdev_indirect_io_done().
772 for (indirect_split_t *is = list_head(&iv->iv_splits);
773 is != NULL; is = list_next(&iv->iv_splits, is)) {
774 char *ptr = zio.io_data;
776 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
777 ptr + is->is_split_offset, is->is_target_offset,
780 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
786 vdev_indirect_map_free(&zio);
794 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
795 off_t offset, size_t bytes)
798 return (vdev_read_phys(vdev, bp, buf,
799 offset + VDEV_LABEL_START_SIZE, bytes));
803 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
804 void *buf __unused, off_t offset __unused, size_t bytes __unused)
811 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
812 off_t offset, size_t bytes)
818 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
819 if (kid->v_state != VDEV_STATE_HEALTHY)
821 rc = kid->v_read(kid, bp, buf, offset, bytes);
830 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
831 off_t offset, size_t bytes)
836 * Here we should have two kids:
837 * First one which is the one we are replacing and we can trust
838 * only this one to have valid data, but it might not be present.
839 * Second one is that one we are replacing with. It is most likely
840 * healthy, but we can't trust it has needed data, so we won't use it.
842 kid = STAILQ_FIRST(&vdev->v_children);
845 if (kid->v_state != VDEV_STATE_HEALTHY)
847 return (kid->v_read(kid, bp, buf, offset, bytes));
851 vdev_find(uint64_t guid)
855 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
856 if (vdev->v_guid == guid)
863 vdev_create(uint64_t guid, vdev_read_t *_read)
866 vdev_indirect_config_t *vic;
868 vdev = calloc(1, sizeof(vdev_t));
870 STAILQ_INIT(&vdev->v_children);
872 vdev->v_read = _read;
875 * root vdev has no read function, we use this fact to
876 * skip setting up data we do not need for root vdev.
877 * We only point root vdev from spa.
880 vic = &vdev->vdev_indirect_config;
881 vic->vic_prev_indirect_vdev = UINT64_MAX;
882 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
890 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
892 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
895 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
897 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
899 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
901 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
903 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
904 NULL, &is_degraded, NULL);
905 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
906 NULL, &isnt_present, NULL);
907 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
911 vdev->v_state = VDEV_STATE_OFFLINE;
912 else if (is_removed != 0)
913 vdev->v_state = VDEV_STATE_REMOVED;
914 else if (is_faulted != 0)
915 vdev->v_state = VDEV_STATE_FAULTED;
916 else if (is_degraded != 0)
917 vdev->v_state = VDEV_STATE_DEGRADED;
918 else if (isnt_present != 0)
919 vdev->v_state = VDEV_STATE_CANT_OPEN;
921 vdev->v_islog = is_log != 0;
925 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
927 uint64_t id, ashift, asize, nparity;
934 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
936 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
941 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
942 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
944 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
946 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
947 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
948 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
949 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
950 printf("ZFS: can only boot from disk, mirror, raidz1, "
951 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
955 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
956 vdev = vdev_create(guid, vdev_mirror_read);
957 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
958 vdev = vdev_create(guid, vdev_raidz_read);
959 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
960 vdev = vdev_create(guid, vdev_replacing_read);
961 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
962 vdev_indirect_config_t *vic;
964 vdev = vdev_create(guid, vdev_indirect_read);
966 vdev->v_state = VDEV_STATE_HEALTHY;
967 vic = &vdev->vdev_indirect_config;
970 ZPOOL_CONFIG_INDIRECT_OBJECT,
972 NULL, &vic->vic_mapping_object, NULL);
974 ZPOOL_CONFIG_INDIRECT_BIRTHS,
976 NULL, &vic->vic_births_object, NULL);
978 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
980 NULL, &vic->vic_prev_indirect_vdev, NULL);
982 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
983 vdev = vdev_create(guid, vdev_missing_read);
985 vdev = vdev_create(guid, vdev_disk_read);
991 vdev_set_initial_state(vdev, nvlist);
993 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
994 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
995 vdev->v_ashift = ashift;
997 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
998 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
999 vdev->v_psize = asize +
1000 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1003 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1004 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
1005 vdev->v_nparity = nparity;
1007 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1008 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1009 char prefix[] = "/dev/";
1011 len = strlen(prefix);
1012 if (len < pathlen && memcmp(path, prefix, len) == 0) {
1016 name = malloc(pathlen + 1);
1017 bcopy(path, name, pathlen);
1018 name[pathlen] = '\0';
1019 vdev->v_name = name;
1022 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1023 if (vdev->v_nparity < 1 ||
1024 vdev->v_nparity > 3) {
1025 printf("ZFS: invalid raidz parity: %d\n",
1029 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1030 vdev->v_nparity, id);
1032 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1034 vdev->v_name = name;
1041 * Find slot for vdev. We return either NULL to signal to use
1042 * STAILQ_INSERT_HEAD, or we return link element to be used with
1043 * STAILQ_INSERT_AFTER.
1046 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1048 vdev_t *v, *previous;
1050 if (STAILQ_EMPTY(&top_vdev->v_children))
1054 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1055 if (v->v_id > vdev->v_id)
1058 if (v->v_id == vdev->v_id)
1061 if (v->v_id < vdev->v_id)
1068 vdev_child_count(vdev_t *vdev)
1074 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1081 * Insert vdev into top_vdev children list. List is ordered by v_id.
1084 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1090 * The top level vdev can appear in random order, depending how
1091 * the firmware is presenting the disk devices.
1092 * However, we will insert vdev to create list ordered by v_id,
1093 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1094 * as STAILQ does not have insert before.
1096 previous = vdev_find_previous(top_vdev, vdev);
1098 if (previous == NULL) {
1099 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1100 } else if (previous->v_id == vdev->v_id) {
1102 * This vdev was configured from label config,
1103 * do not insert duplicate.
1107 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1111 count = vdev_child_count(top_vdev);
1112 if (top_vdev->v_nchildren < count)
1113 top_vdev->v_nchildren = count;
1117 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1119 vdev_t *top_vdev, *vdev;
1120 nvlist_t **kids = NULL;
1124 top_vdev = vdev_find(top_guid);
1125 if (top_vdev == NULL) {
1126 rc = vdev_init(top_guid, nvlist, &top_vdev);
1129 top_vdev->v_spa = spa;
1130 top_vdev->v_top = top_vdev;
1131 vdev_insert(spa->spa_root_vdev, top_vdev);
1134 /* Add children if there are any. */
1135 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1136 &nkids, &kids, NULL);
1138 for (int i = 0; i < nkids; i++) {
1141 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1142 DATA_TYPE_UINT64, NULL, &guid, NULL);
1146 rc = vdev_init(guid, kids[i], &vdev);
1151 vdev->v_top = top_vdev;
1152 vdev_insert(top_vdev, vdev);
1156 * When there are no children, nvlist_find() does return
1157 * error, reset it because leaf devices have no children.
1163 for (int i = 0; i < nkids; i++)
1164 nvlist_destroy(kids[i]);
1172 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1174 uint64_t pool_guid, top_guid;
1178 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1179 NULL, &pool_guid, NULL) ||
1180 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1181 NULL, &top_guid, NULL) ||
1182 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1183 NULL, &vdevs, NULL)) {
1184 printf("ZFS: can't find vdev details\n");
1188 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1189 nvlist_destroy(vdevs);
1194 vdev_set_state(vdev_t *vdev)
1200 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1201 vdev_set_state(kid);
1205 * A mirror or raidz is healthy if all its kids are healthy. A
1206 * mirror is degraded if any of its kids is healthy; a raidz
1207 * is degraded if at most nparity kids are offline.
1209 if (STAILQ_FIRST(&vdev->v_children)) {
1212 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1213 if (kid->v_state == VDEV_STATE_HEALTHY)
1218 if (bad_kids == 0) {
1219 vdev->v_state = VDEV_STATE_HEALTHY;
1221 if (vdev->v_read == vdev_mirror_read) {
1223 vdev->v_state = VDEV_STATE_DEGRADED;
1225 vdev->v_state = VDEV_STATE_OFFLINE;
1227 } else if (vdev->v_read == vdev_raidz_read) {
1228 if (bad_kids > vdev->v_nparity) {
1229 vdev->v_state = VDEV_STATE_OFFLINE;
1231 vdev->v_state = VDEV_STATE_DEGRADED;
1239 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1242 nvlist_t **kids = NULL;
1245 /* Update top vdev. */
1246 vdev = vdev_find(top_guid);
1248 vdev_set_initial_state(vdev, nvlist);
1250 /* Update children if there are any. */
1251 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1252 &nkids, &kids, NULL);
1254 for (int i = 0; i < nkids; i++) {
1257 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1258 DATA_TYPE_UINT64, NULL, &guid, NULL);
1262 vdev = vdev_find(guid);
1264 vdev_set_initial_state(vdev, kids[i]);
1270 for (int i = 0; i < nkids; i++)
1271 nvlist_destroy(kids[i]);
1279 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1281 uint64_t pool_guid, vdev_children;
1282 nvlist_t *vdevs = NULL, **kids = NULL;
1285 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1286 NULL, &pool_guid, NULL) ||
1287 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1288 NULL, &vdev_children, NULL) ||
1289 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1290 NULL, &vdevs, NULL)) {
1291 printf("ZFS: can't find vdev details\n");
1296 if (spa->spa_guid != pool_guid) {
1297 nvlist_destroy(vdevs);
1301 spa->spa_root_vdev->v_nchildren = vdev_children;
1303 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1304 &nkids, &kids, NULL);
1305 nvlist_destroy(vdevs);
1308 * MOS config has at least one child for root vdev.
1313 for (int i = 0; i < nkids; i++) {
1317 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1321 vdev = vdev_find(guid);
1323 * Top level vdev is missing, create it.
1326 rc = vdev_from_nvlist(spa, guid, kids[i]);
1328 rc = vdev_update_from_nvlist(guid, kids[i]);
1333 for (int i = 0; i < nkids; i++)
1334 nvlist_destroy(kids[i]);
1339 * Re-evaluate top-level vdev state.
1341 vdev_set_state(spa->spa_root_vdev);
1347 spa_find_by_guid(uint64_t guid)
1351 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1352 if (spa->spa_guid == guid)
1359 spa_find_by_name(const char *name)
1363 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1364 if (strcmp(spa->spa_name, name) == 0)
1371 spa_find_by_dev(struct zfs_devdesc *dev)
1374 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1377 if (dev->pool_guid == 0)
1378 return (STAILQ_FIRST(&zfs_pools));
1380 return (spa_find_by_guid(dev->pool_guid));
1384 spa_create(uint64_t guid, const char *name)
1388 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1390 if ((spa->spa_name = strdup(name)) == NULL) {
1394 spa->spa_uberblock = &spa->spa_uberblock_master;
1395 spa->spa_mos = &spa->spa_mos_master;
1396 spa->spa_guid = guid;
1397 spa->spa_root_vdev = vdev_create(guid, NULL);
1398 if (spa->spa_root_vdev == NULL) {
1399 free(spa->spa_name);
1403 spa->spa_root_vdev->v_name = strdup("root");
1404 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1410 state_name(vdev_state_t state)
1412 static const char *names[] = {
1422 return (names[state]);
1427 #define pager_printf printf
1432 pager_printf(const char *fmt, ...)
1437 va_start(args, fmt);
1438 vsnprintf(line, sizeof(line), fmt, args);
1440 return (pager_output(line));
1445 #define STATUS_FORMAT " %s %s\n"
1448 print_state(int indent, const char *name, vdev_state_t state)
1454 for (i = 0; i < indent; i++)
1457 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1461 vdev_status(vdev_t *vdev, int indent)
1466 if (vdev->v_islog) {
1467 (void) pager_output(" logs\n");
1471 ret = print_state(indent, vdev->v_name, vdev->v_state);
1475 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1476 ret = vdev_status(kid, indent + 1);
1484 spa_status(spa_t *spa)
1486 static char bootfs[ZFS_MAXNAMELEN];
1490 int good_kids, bad_kids, degraded_kids, ret;
1493 ret = pager_printf(" pool: %s\n", spa->spa_name);
1497 if (zfs_get_root(spa, &rootid) == 0 &&
1498 zfs_rlookup(spa, rootid, bootfs) == 0) {
1499 if (bootfs[0] == '\0')
1500 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1502 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1507 ret = pager_printf("config:\n\n");
1510 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1517 vlist = &spa->spa_root_vdev->v_children;
1518 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1519 if (vdev->v_state == VDEV_STATE_HEALTHY)
1521 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1527 state = VDEV_STATE_CLOSED;
1528 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1529 state = VDEV_STATE_HEALTHY;
1530 else if ((good_kids + degraded_kids) > 0)
1531 state = VDEV_STATE_DEGRADED;
1533 ret = print_state(0, spa->spa_name, state);
1537 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1538 ret = vdev_status(vdev, 1);
1546 spa_all_status(void)
1549 int first = 1, ret = 0;
1551 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1553 ret = pager_printf("\n");
1558 ret = spa_status(spa);
1566 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1568 uint64_t label_offset;
1570 if (l < VDEV_LABELS / 2)
1573 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1575 return (offset + l * sizeof (vdev_label_t) + label_offset);
1579 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1581 unsigned int seq1 = 0;
1582 unsigned int seq2 = 0;
1583 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1588 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1592 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1593 seq1 = MMP_SEQ(ub1);
1595 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1596 seq2 = MMP_SEQ(ub2);
1598 return (AVL_CMP(seq1, seq2));
1602 uberblock_verify(uberblock_t *ub)
1604 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1605 byteswap_uint64_array(ub, sizeof (uberblock_t));
1608 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1609 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1616 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1622 off = vdev_label_offset(vd->v_psize, l, offset);
1625 BP_SET_LSIZE(&bp, size);
1626 BP_SET_PSIZE(&bp, size);
1627 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1628 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1629 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1630 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1632 return (vdev_read_phys(vd, &bp, buf, off, size));
1636 * We do need to be sure we write to correct location.
1637 * Our vdev label does consist of 4 fields:
1638 * pad1 (8k), reserved.
1639 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1640 * vdev_phys (112k), checksummed
1641 * uberblock ring (128k), checksummed.
1643 * Since bootenv area may contain garbage, we can not reliably read it, as
1644 * we can get checksum errors.
1645 * Next best thing is vdev_phys - it is just after bootenv. It still may
1646 * be corrupted, but in such case we will miss this one write.
1649 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1651 uint64_t off, o_phys;
1653 size_t size = VDEV_PHYS_SIZE;
1656 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1657 off = vdev_label_offset(vd->v_psize, l, o_phys);
1659 /* off should be 8K from bootenv */
1660 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1667 /* Read vdev_phys */
1668 rc = vdev_label_read(vd, l, buf, o_phys, size);
1674 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1676 zio_checksum_info_t *ci;
1679 size_t size = VDEV_PAD_SIZE;
1682 if (vd->v_phys_write == NULL)
1685 off = vdev_label_offset(vd->v_psize, l, offset);
1687 rc = vdev_label_write_validate(vd, l, offset);
1692 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1693 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1694 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1695 ci->ci_func[0](be, size, NULL, &cksum);
1696 be->vbe_zbt.zec_cksum = cksum;
1698 return (vdev_write_phys(vd, be, off, size));
1702 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1707 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1708 if (kid->v_state != VDEV_STATE_HEALTHY)
1710 rc = vdev_write_bootenv_impl(kid, be);
1716 * Non-leaf vdevs do not have v_phys_write.
1718 if (vdev->v_phys_write == NULL)
1721 for (int l = 0; l < VDEV_LABELS; l++) {
1722 rc = vdev_label_write(vdev, l, be,
1723 offsetof(vdev_label_t, vl_be));
1725 printf("failed to write bootenv to %s label %d: %d\n",
1726 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1734 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1736 vdev_boot_envblock_t *be;
1741 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1745 nvp = vdev_read_bootenv(vdev);
1747 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1749 nvlist_destroy(nvp);
1752 be = calloc(1, sizeof(*be));
1756 be->vbe_version = version;
1760 * If there is no envmap, we will just wipe bootenv.
1762 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1763 be->vbe_bootenv, NULL);
1768 nv.nv_header = nvl->nv_header;
1769 nv.nv_asize = nvl->nv_asize;
1770 nv.nv_size = nvl->nv_size;
1772 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1773 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1774 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1775 rv = nvlist_export(&nv);
1784 be->vbe_version = htobe64(be->vbe_version);
1785 rv = vdev_write_bootenv_impl(vdev, be);
1792 * Read the bootenv area from pool label, return the nvlist from it.
1793 * We return from first successful read.
1796 vdev_read_bootenv(vdev_t *vdev)
1800 vdev_boot_envblock_t *be;
1805 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1806 if (kid->v_state != VDEV_STATE_HEALTHY)
1809 benv = vdev_read_bootenv(kid);
1814 be = malloc(sizeof (*be));
1819 for (int l = 0; l < VDEV_LABELS; l++) {
1820 rv = vdev_label_read(vdev, l, be,
1821 offsetof(vdev_label_t, vl_be),
1831 be->vbe_version = be64toh(be->vbe_version);
1832 switch (be->vbe_version) {
1835 * we have textual data in vbe_bootenv, create nvlist
1836 * with key "envmap".
1838 benv = nvlist_create(NV_UNIQUE_NAME);
1840 if (*be->vbe_bootenv == '\0') {
1841 nvlist_add_uint64(benv, BOOTENV_VERSION,
1845 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1846 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1847 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1852 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1856 command = (char *)be;
1859 /* Check for legacy zfsbootcfg command string */
1860 for (int i = 0; command[i] != '\0'; i++) {
1861 if (iscntrl(command[i])) {
1868 benv = nvlist_create(NV_UNIQUE_NAME);
1871 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1874 nvlist_add_uint64(benv, BOOTENV_VERSION,
1884 vdev_get_label_asize(nvlist_t *nvl)
1893 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1894 NULL, &vdevs, NULL) != 0)
1898 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1899 * For raidz, the asize is raw size of all children.
1901 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1902 NULL, &type, &len) != 0)
1905 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1906 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1907 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1910 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1911 NULL, &asize, NULL) != 0)
1914 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1918 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1919 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1925 for (int i = 0; i < nkids; i++)
1926 nvlist_destroy(kids[i]);
1930 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1932 nvlist_destroy(vdevs);
1937 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1940 uint64_t best_txg = 0;
1941 uint64_t label_txg = 0;
1943 nvlist_t *nvl = NULL, *tmp;
1946 label = malloc(sizeof (vdev_phys_t));
1950 for (int l = 0; l < VDEV_LABELS; l++) {
1951 if (vdev_label_read(vd, l, label,
1952 offsetof(vdev_label_t, vl_vdev_phys),
1953 sizeof (vdev_phys_t)))
1956 tmp = nvlist_import(label->vp_nvlist,
1957 sizeof(label->vp_nvlist));
1961 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1962 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1963 if (error != 0 || label_txg == 0) {
1964 nvlist_destroy(nvl);
1969 if (label_txg <= txg && label_txg > best_txg) {
1970 best_txg = label_txg;
1971 nvlist_destroy(nvl);
1976 * Use asize from pool config. We need this
1977 * because we can get bad value from BIOS.
1979 asize = vdev_get_label_asize(nvl);
1981 vd->v_psize = asize;
1984 nvlist_destroy(tmp);
1987 if (best_txg == 0) {
1988 nvlist_destroy(nvl);
1997 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
2001 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
2005 for (int l = 0; l < VDEV_LABELS; l++) {
2006 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
2007 if (vdev_label_read(vd, l, buf,
2008 VDEV_UBERBLOCK_OFFSET(vd, n),
2009 VDEV_UBERBLOCK_SIZE(vd)))
2011 if (uberblock_verify(buf) != 0)
2014 if (vdev_uberblock_compare(buf, ub) > 0)
2022 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2030 uint64_t guid, vdev_children;
2031 uint64_t pool_txg, pool_guid;
2032 const char *pool_name;
2036 * Load the vdev label and figure out which
2037 * uberblock is most current.
2039 memset(&vtmp, 0, sizeof(vtmp));
2040 vtmp.v_phys_read = _read;
2041 vtmp.v_phys_write = _write;
2043 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2044 (uint64_t)sizeof (vdev_label_t));
2046 /* Test for minimum device size. */
2047 if (vtmp.v_psize < SPA_MINDEVSIZE)
2050 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2054 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2055 NULL, &val, NULL) != 0) {
2056 nvlist_destroy(nvl);
2060 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2061 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2062 (unsigned)val, (unsigned)SPA_VERSION);
2063 nvlist_destroy(nvl);
2067 /* Check ZFS features for read */
2068 rc = nvlist_check_features_for_read(nvl);
2070 nvlist_destroy(nvl);
2074 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2075 NULL, &val, NULL) != 0) {
2076 nvlist_destroy(nvl);
2080 if (val == POOL_STATE_DESTROYED) {
2081 /* We don't boot only from destroyed pools. */
2082 nvlist_destroy(nvl);
2086 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2087 NULL, &pool_txg, NULL) != 0 ||
2088 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2089 NULL, &pool_guid, NULL) != 0 ||
2090 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2091 NULL, &pool_name, &namelen) != 0) {
2093 * Cache and spare devices end up here - just ignore
2096 nvlist_destroy(nvl);
2101 * Create the pool if this is the first time we've seen it.
2103 spa = spa_find_by_guid(pool_guid);
2107 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2108 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2109 name = malloc(namelen + 1);
2111 nvlist_destroy(nvl);
2114 bcopy(pool_name, name, namelen);
2115 name[namelen] = '\0';
2116 spa = spa_create(pool_guid, name);
2119 nvlist_destroy(nvl);
2122 spa->spa_root_vdev->v_nchildren = vdev_children;
2124 if (pool_txg > spa->spa_txg)
2125 spa->spa_txg = pool_txg;
2128 * Get the vdev tree and create our in-core copy of it.
2129 * If we already have a vdev with this guid, this must
2130 * be some kind of alias (overlapping slices, dangerously dedicated
2133 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2134 NULL, &guid, NULL) != 0) {
2135 nvlist_destroy(nvl);
2138 vdev = vdev_find(guid);
2139 /* Has this vdev already been inited? */
2140 if (vdev && vdev->v_phys_read) {
2141 nvlist_destroy(nvl);
2145 rc = vdev_init_from_label(spa, nvl);
2146 nvlist_destroy(nvl);
2151 * We should already have created an incomplete vdev for this
2152 * vdev. Find it and initialise it with our read proc.
2154 vdev = vdev_find(guid);
2156 vdev->v_phys_read = _read;
2157 vdev->v_phys_write = _write;
2158 vdev->v_priv = priv;
2159 vdev->v_psize = vtmp.v_psize;
2161 * If no other state is set, mark vdev healthy.
2163 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2164 vdev->v_state = VDEV_STATE_HEALTHY;
2166 printf("ZFS: inconsistent nvlist contents\n");
2171 spa->spa_with_log = vdev->v_islog;
2174 * Re-evaluate top-level vdev state.
2176 vdev_set_state(vdev->v_top);
2179 * Ok, we are happy with the pool so far. Lets find
2180 * the best uberblock and then we can actually access
2181 * the contents of the pool.
2183 vdev_uberblock_load(vdev, spa->spa_uberblock);
2195 for (v = 0; v < 32; v++)
2202 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2205 zio_gbh_phys_t zio_gb;
2209 /* Artificial BP for gang block header. */
2211 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2212 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2213 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2214 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2215 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2216 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2218 /* Read gang header block using the artificial BP. */
2219 if (zio_read(spa, &gbh_bp, &zio_gb))
2223 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2224 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2226 if (BP_IS_HOLE(gbp))
2228 if (zio_read(spa, gbp, pbuf))
2230 pbuf += BP_GET_PSIZE(gbp);
2233 if (zio_checksum_verify(spa, bp, buf))
2239 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2241 int cpfunc = BP_GET_COMPRESS(bp);
2242 uint64_t align, size;
2247 * Process data embedded in block pointer
2249 if (BP_IS_EMBEDDED(bp)) {
2250 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2252 size = BPE_GET_PSIZE(bp);
2253 ASSERT(size <= BPE_PAYLOAD_SIZE);
2255 if (cpfunc != ZIO_COMPRESS_OFF)
2256 pbuf = malloc(size);
2263 decode_embedded_bp_compressed(bp, pbuf);
2266 if (cpfunc != ZIO_COMPRESS_OFF) {
2267 error = zio_decompress_data(cpfunc, pbuf,
2268 size, buf, BP_GET_LSIZE(bp));
2272 printf("ZFS: i/o error - unable to decompress "
2273 "block pointer data, error %d\n", error);
2279 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2280 const dva_t *dva = &bp->blk_dva[i];
2286 if (!dva->dva_word[0] && !dva->dva_word[1])
2289 vdevid = DVA_GET_VDEV(dva);
2290 offset = DVA_GET_OFFSET(dva);
2291 vlist = &spa->spa_root_vdev->v_children;
2292 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2293 if (vdev->v_id == vdevid)
2296 if (!vdev || !vdev->v_read)
2299 size = BP_GET_PSIZE(bp);
2300 if (vdev->v_read == vdev_raidz_read) {
2301 align = 1ULL << vdev->v_ashift;
2302 if (P2PHASE(size, align) != 0)
2303 size = P2ROUNDUP(size, align);
2305 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2306 pbuf = malloc(size);
2315 if (DVA_GET_GANG(dva))
2316 error = zio_read_gang(spa, bp, pbuf);
2318 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2320 if (cpfunc != ZIO_COMPRESS_OFF)
2321 error = zio_decompress_data(cpfunc, pbuf,
2322 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2323 else if (size != BP_GET_PSIZE(bp))
2324 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2326 printf("zio_read error: %d\n", error);
2334 printf("ZFS: i/o error - all block copies unavailable\n");
2340 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2341 void *buf, size_t buflen)
2343 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2344 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2345 int nlevels = dnode->dn_nlevels;
2348 if (bsize > SPA_MAXBLOCKSIZE) {
2349 printf("ZFS: I/O error - blocks larger than %llu are not "
2350 "supported\n", SPA_MAXBLOCKSIZE);
2355 * Handle odd block sizes, mirrors dmu_read_impl(). Data can't exist
2356 * past the first block, so we'll clip the read to the portion of the
2357 * buffer within bsize and zero out the remainder.
2359 if (dnode->dn_maxblkid == 0) {
2362 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset);
2363 bzero((char *)buf + newbuflen, buflen - newbuflen);
2368 * Note: bsize may not be a power of two here so we need to do an
2369 * actual divide rather than a bitshift.
2371 while (buflen > 0) {
2372 uint64_t bn = offset / bsize;
2373 int boff = offset % bsize;
2375 const blkptr_t *indbp;
2378 if (bn > dnode->dn_maxblkid)
2381 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2384 indbp = dnode->dn_blkptr;
2385 for (i = 0; i < nlevels; i++) {
2387 * Copy the bp from the indirect array so that
2388 * we can re-use the scratch buffer for multi-level
2391 ibn = bn >> ((nlevels - i - 1) * ibshift);
2392 ibn &= ((1 << ibshift) - 1);
2394 if (BP_IS_HOLE(&bp)) {
2395 memset(dnode_cache_buf, 0, bsize);
2398 rc = zio_read(spa, &bp, dnode_cache_buf);
2401 indbp = (const blkptr_t *) dnode_cache_buf;
2403 dnode_cache_obj = dnode;
2404 dnode_cache_bn = bn;
2408 * The buffer contains our data block. Copy what we
2409 * need from it and loop.
2412 if (i > buflen) i = buflen;
2413 memcpy(buf, &dnode_cache_buf[boff], i);
2414 buf = ((char *)buf) + i;
2423 * Lookup a value in a microzap directory.
2426 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2429 const mzap_ent_phys_t *mze;
2433 * Microzap objects use exactly one block. Read the whole
2436 chunks = size / MZAP_ENT_LEN - 1;
2437 for (i = 0; i < chunks; i++) {
2438 mze = &mz->mz_chunk[i];
2439 if (strcmp(mze->mze_name, name) == 0) {
2440 *value = mze->mze_value;
2449 * Compare a name with a zap leaf entry. Return non-zero if the name
2453 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2457 const zap_leaf_chunk_t *nc;
2460 namelen = zc->l_entry.le_name_numints;
2462 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2464 while (namelen > 0) {
2468 if (len > ZAP_LEAF_ARRAY_BYTES)
2469 len = ZAP_LEAF_ARRAY_BYTES;
2470 if (memcmp(p, nc->l_array.la_array, len))
2474 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2481 * Extract a uint64_t value from a zap leaf entry.
2484 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2486 const zap_leaf_chunk_t *vc;
2491 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2492 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2493 value = (value << 8) | p[i];
2500 stv(int len, void *addr, uint64_t value)
2504 *(uint8_t *)addr = value;
2507 *(uint16_t *)addr = value;
2510 *(uint32_t *)addr = value;
2513 *(uint64_t *)addr = value;
2519 * Extract a array from a zap leaf entry.
2522 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2523 uint64_t integer_size, uint64_t num_integers, void *buf)
2525 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2527 uint64_t *u64 = buf;
2529 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2530 int chunk = zc->l_entry.le_value_chunk;
2533 if (integer_size == 8 && len == 1) {
2534 *u64 = fzap_leaf_value(zl, zc);
2539 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2542 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2543 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2544 value = (value << 8) | la->la_array[i];
2546 if (byten == array_int_len) {
2547 stv(integer_size, p, value);
2555 chunk = la->la_next;
2560 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2563 switch (integer_size) {
2573 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2580 zap_leaf_free(zap_leaf_t *leaf)
2587 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2589 int bs = FZAP_BLOCK_SHIFT(zap);
2592 *lp = malloc(sizeof(**lp));
2597 (*lp)->l_phys = malloc(1 << bs);
2599 if ((*lp)->l_phys == NULL) {
2603 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2612 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2615 int bs = FZAP_BLOCK_SHIFT(zap);
2616 uint64_t blk = idx >> (bs - 3);
2617 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2621 buf = malloc(1 << zap->zap_block_shift);
2624 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2625 buf, 1 << zap->zap_block_shift);
2633 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2635 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2636 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2639 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2644 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2646 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2651 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2652 err = zap_idx_to_blk(zap, idx, &blk);
2655 return (zap_get_leaf_byblk(zap, blk, lp));
2658 #define CHAIN_END 0xffff /* end of the chunk chain */
2659 #define LEAF_HASH(l, h) \
2660 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2662 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2663 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2666 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2667 uint64_t integer_size, uint64_t num_integers, void *value)
2671 struct zap_leaf_entry *le;
2674 * Make sure this chunk matches our hash.
2676 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2677 zl->l_phys->l_hdr.lh_prefix !=
2678 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2682 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2683 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2684 zap_leaf_chunk_t *zc;
2685 uint16_t chunk = *chunkp;
2687 le = ZAP_LEAF_ENTRY(zl, chunk);
2688 if (le->le_hash != hash)
2690 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2691 if (fzap_name_equal(zl, zc, name)) {
2692 if (zc->l_entry.le_value_intlen > integer_size) {
2695 fzap_leaf_array(zl, zc, integer_size,
2696 num_integers, value);
2706 * Lookup a value in a fatzap directory.
2709 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2710 const char *name, uint64_t integer_size, uint64_t num_integers,
2713 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2719 if (zh->zap_magic != ZAP_MAGIC)
2722 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2726 z.zap_block_shift = ilog2(bsize);
2729 z.zap_dnode = dnode;
2731 hash = zap_hash(zh->zap_salt, name);
2732 rc = zap_deref_leaf(&z, hash, &zl);
2736 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2743 * Lookup a name in a zap object and return its value as a uint64_t.
2746 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2747 uint64_t integer_size, uint64_t num_integers, void *value)
2751 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2757 rc = dnode_read(spa, dnode, 0, zap, size);
2761 switch (zap->zap_block_type) {
2763 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2766 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2767 num_integers, value);
2770 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2771 zap->zap_block_type);
2780 * List a microzap directory.
2783 mzap_list(const mzap_phys_t *mz, size_t size,
2784 int (*callback)(const char *, uint64_t))
2786 const mzap_ent_phys_t *mze;
2790 * Microzap objects use exactly one block. Read the whole
2794 chunks = size / MZAP_ENT_LEN - 1;
2795 for (i = 0; i < chunks; i++) {
2796 mze = &mz->mz_chunk[i];
2797 if (mze->mze_name[0]) {
2798 rc = callback(mze->mze_name, mze->mze_value);
2808 * List a fatzap directory.
2811 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2812 int (*callback)(const char *, uint64_t))
2814 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2819 if (zh->zap_magic != ZAP_MAGIC)
2822 z.zap_block_shift = ilog2(bsize);
2826 * This assumes that the leaf blocks start at block 1. The
2827 * documentation isn't exactly clear on this.
2830 zl.l_bs = z.zap_block_shift;
2831 zl.l_phys = malloc(bsize);
2832 if (zl.l_phys == NULL)
2835 for (i = 0; i < zh->zap_num_leafs; i++) {
2836 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2840 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2845 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2846 zap_leaf_chunk_t *zc, *nc;
2849 zc = &ZAP_LEAF_CHUNK(&zl, j);
2850 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2852 namelen = zc->l_entry.le_name_numints;
2853 if (namelen > sizeof(name))
2854 namelen = sizeof(name);
2857 * Paste the name back together.
2859 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2861 while (namelen > 0) {
2864 if (len > ZAP_LEAF_ARRAY_BYTES)
2865 len = ZAP_LEAF_ARRAY_BYTES;
2866 memcpy(p, nc->l_array.la_array, len);
2869 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2873 * Assume the first eight bytes of the value are
2876 value = fzap_leaf_value(&zl, zc);
2878 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2879 rc = callback((const char *)name, value);
2891 static int zfs_printf(const char *name, uint64_t value __unused)
2894 printf("%s\n", name);
2900 * List a zap directory.
2903 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2906 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2913 rc = dnode_read(spa, dnode, 0, zap, size);
2915 if (zap->zap_block_type == ZBT_MICRO)
2916 rc = mzap_list((const mzap_phys_t *)zap, size,
2919 rc = fzap_list(spa, dnode, zap, zfs_printf);
2926 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2927 dnode_phys_t *dnode)
2931 offset = objnum * sizeof(dnode_phys_t);
2932 return dnode_read(spa, &os->os_meta_dnode, offset,
2933 dnode, sizeof(dnode_phys_t));
2937 * Lookup a name in a microzap directory.
2940 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2942 const mzap_ent_phys_t *mze;
2946 * Microzap objects use exactly one block. Read the whole
2949 chunks = size / MZAP_ENT_LEN - 1;
2950 for (i = 0; i < chunks; i++) {
2951 mze = &mz->mz_chunk[i];
2952 if (value == mze->mze_value) {
2953 strcpy(name, mze->mze_name);
2962 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2965 const zap_leaf_chunk_t *nc;
2968 namelen = zc->l_entry.le_name_numints;
2970 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2972 while (namelen > 0) {
2975 if (len > ZAP_LEAF_ARRAY_BYTES)
2976 len = ZAP_LEAF_ARRAY_BYTES;
2977 memcpy(p, nc->l_array.la_array, len);
2980 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2987 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2988 char *name, uint64_t value)
2990 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2995 if (zh->zap_magic != ZAP_MAGIC)
2998 z.zap_block_shift = ilog2(bsize);
3002 * This assumes that the leaf blocks start at block 1. The
3003 * documentation isn't exactly clear on this.
3006 zl.l_bs = z.zap_block_shift;
3007 zl.l_phys = malloc(bsize);
3008 if (zl.l_phys == NULL)
3011 for (i = 0; i < zh->zap_num_leafs; i++) {
3012 off_t off = ((off_t)(i + 1)) << zl.l_bs;
3014 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
3018 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
3019 zap_leaf_chunk_t *zc;
3021 zc = &ZAP_LEAF_CHUNK(&zl, j);
3022 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
3024 if (zc->l_entry.le_value_intlen != 8 ||
3025 zc->l_entry.le_value_numints != 1)
3028 if (fzap_leaf_value(&zl, zc) == value) {
3029 fzap_name_copy(&zl, zc, name);
3042 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3046 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3053 rc = dnode_read(spa, dnode, 0, zap, size);
3055 if (zap->zap_block_type == ZBT_MICRO)
3056 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3059 rc = fzap_rlookup(spa, dnode, zap, name, value);
3066 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3069 char component[256];
3070 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3071 dnode_phys_t child_dir_zap, snapnames_zap, dataset, dir, parent;
3073 dsl_dataset_phys_t *ds;
3076 boolean_t issnap = B_FALSE;
3078 p = &name[sizeof(name) - 1];
3081 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3082 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3085 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3086 dir_obj = ds->ds_dir_obj;
3087 if (ds->ds_snapnames_zapobj == 0)
3091 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3093 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3095 /* Actual loop condition. */
3096 parent_obj = dd->dd_parent_obj;
3097 if (parent_obj == 0)
3100 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3103 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3104 if (issnap == B_TRUE) {
3106 * The dataset we are looking up is a snapshot
3107 * the dir_obj is the parent already, we don't want
3108 * the grandparent just yet. Reset to the parent.
3110 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3111 /* Lookup the dataset to get the snapname ZAP */
3112 if (objset_get_dnode(spa, spa->spa_mos,
3113 dd->dd_head_dataset_obj, &dataset))
3115 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3116 if (objset_get_dnode(spa, spa->spa_mos,
3117 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3119 /* Get the name of the snapshot */
3120 if (zap_rlookup(spa, &snapnames_zap, component,
3123 len = strlen(component);
3125 memcpy(p, component, len);
3132 child_dir_zapobj = dd->dd_child_dir_zapobj;
3133 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3134 &child_dir_zap) != 0)
3136 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3139 len = strlen(component);
3141 memcpy(p, component, len);
3145 /* Actual loop iteration. */
3146 dir_obj = parent_obj;
3157 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3160 uint64_t dir_obj, child_dir_zapobj;
3161 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset;
3163 dsl_dataset_phys_t *ds;
3165 boolean_t issnap = B_FALSE;
3167 if (objset_get_dnode(spa, spa->spa_mos,
3168 DMU_POOL_DIRECTORY_OBJECT, &dir))
3170 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3176 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3178 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3182 /* Actual loop condition #1. */
3188 memcpy(element, p, q - p);
3189 element[q - p] = '\0';
3196 if (issnap == B_TRUE) {
3197 if (objset_get_dnode(spa, spa->spa_mos,
3198 dd->dd_head_dataset_obj, &dataset))
3200 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3201 if (objset_get_dnode(spa, spa->spa_mos,
3202 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3204 /* Actual loop condition #2. */
3205 if (zap_lookup(spa, &snapnames_zap, element,
3206 sizeof (dir_obj), 1, &dir_obj) != 0)
3210 } else if ((q = strchr(element, '@')) != NULL) {
3212 element[q - element] = '\0';
3215 child_dir_zapobj = dd->dd_child_dir_zapobj;
3216 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3217 &child_dir_zap) != 0)
3220 /* Actual loop condition #2. */
3221 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3226 *objnum = dd->dd_head_dataset_obj;
3232 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3234 uint64_t dir_obj, child_dir_zapobj;
3235 dnode_phys_t child_dir_zap, dir, dataset;
3236 dsl_dataset_phys_t *ds;
3239 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3240 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3243 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3244 dir_obj = ds->ds_dir_obj;
3246 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3247 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3250 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3252 child_dir_zapobj = dd->dd_child_dir_zapobj;
3253 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3254 &child_dir_zap) != 0) {
3255 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3259 return (zap_list(spa, &child_dir_zap) != 0);
3263 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3264 int (*callback)(const char *, uint64_t))
3266 uint64_t dir_obj, child_dir_zapobj;
3267 dnode_phys_t child_dir_zap, dir, dataset;
3268 dsl_dataset_phys_t *ds;
3274 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3276 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3279 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3280 dir_obj = ds->ds_dir_obj;
3282 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3284 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3287 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3289 child_dir_zapobj = dd->dd_child_dir_zapobj;
3290 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3293 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3297 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3300 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3304 if (zap->zap_block_type == ZBT_MICRO)
3305 err = mzap_list((const mzap_phys_t *)zap, size,
3308 err = fzap_list(spa, &child_dir_zap, zap, callback);
3319 * Find the object set given the object number of its dataset object
3320 * and return its details in *objset
3323 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3325 dnode_phys_t dataset;
3326 dsl_dataset_phys_t *ds;
3328 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3329 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3333 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3334 if (zio_read(spa, &ds->ds_bp, objset)) {
3335 printf("ZFS: can't read object set for dataset %ju\n",
3344 * Find the object set pointed to by the BOOTFS property or the root
3345 * dataset if there is none and return its details in *objset
3348 zfs_get_root(const spa_t *spa, uint64_t *objid)
3350 dnode_phys_t dir, propdir;
3351 uint64_t props, bootfs, root;
3356 * Start with the MOS directory object.
3358 if (objset_get_dnode(spa, spa->spa_mos,
3359 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3360 printf("ZFS: can't read MOS object directory\n");
3365 * Lookup the pool_props and see if we can find a bootfs.
3367 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3368 sizeof(props), 1, &props) == 0 &&
3369 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3370 zap_lookup(spa, &propdir, "bootfs",
3371 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3376 * Lookup the root dataset directory
3378 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3379 sizeof(root), 1, &root) ||
3380 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3381 printf("ZFS: can't find root dsl_dir\n");
3386 * Use the information from the dataset directory's bonus buffer
3387 * to find the dataset object and from that the object set itself.
3389 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3390 *objid = dd->dd_head_dataset_obj;
3395 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3401 * Find the root object set if not explicitly provided
3403 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3404 printf("ZFS: can't find root filesystem\n");
3408 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3409 printf("ZFS: can't open root filesystem\n");
3413 mount->rootobj = rootobj;
3419 * callback function for feature name checks.
3422 check_feature(const char *name, uint64_t value)
3428 if (name[0] == '\0')
3431 for (i = 0; features_for_read[i] != NULL; i++) {
3432 if (strcmp(name, features_for_read[i]) == 0)
3435 printf("ZFS: unsupported feature: %s\n", name);
3440 * Checks whether the MOS features that are active are supported.
3443 check_mos_features(const spa_t *spa)
3451 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3454 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3455 sizeof (objnum), 1, &objnum)) != 0) {
3457 * It is older pool without features. As we have already
3458 * tested the label, just return without raising the error.
3463 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3466 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3469 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3474 if (dnode_read(spa, &dir, 0, zap, size)) {
3479 if (zap->zap_block_type == ZBT_MICRO)
3480 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3482 rc = fzap_list(spa, &dir, zap, check_feature);
3489 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3497 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3499 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3500 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3504 if (dir.dn_bonuslen != sizeof (uint64_t))
3507 size = *(uint64_t *)DN_BONUS(&dir);
3512 rc = dnode_read(spa, &dir, 0, nv, size);
3518 *value = nvlist_import(nv, size);
3524 zfs_spa_init(spa_t *spa)
3526 struct uberblock checkpoint;
3528 uint64_t config_object;
3532 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3533 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3536 if (spa->spa_mos->os_type != DMU_OST_META) {
3537 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3541 if (objset_get_dnode(spa, &spa->spa_mos_master,
3542 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3543 printf("ZFS: failed to read pool %s directory object\n",
3547 /* this is allowed to fail, older pools do not have salt */
3548 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3549 sizeof (spa->spa_cksum_salt.zcs_bytes),
3550 spa->spa_cksum_salt.zcs_bytes);
3552 rc = check_mos_features(spa);
3554 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3558 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3559 sizeof (config_object), 1, &config_object);
3561 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3564 rc = load_nvlist(spa, config_object, &nvlist);
3568 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3569 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3571 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3572 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3573 sizeof(checkpoint));
3574 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3575 &spa->spa_mos_checkpoint)) {
3576 printf("ZFS: can not read checkpoint data.\n");
3582 * Update vdevs from MOS config. Note, we do skip encoding bytes
3583 * here. See also vdev_label_read_config().
3585 rc = vdev_init_from_nvlist(spa, nvlist);
3586 nvlist_destroy(nvlist);
3591 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3594 if (dn->dn_bonustype != DMU_OT_SA) {
3595 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3597 sb->st_mode = zp->zp_mode;
3598 sb->st_uid = zp->zp_uid;
3599 sb->st_gid = zp->zp_gid;
3600 sb->st_size = zp->zp_size;
3602 sa_hdr_phys_t *sahdrp;
3607 if (dn->dn_bonuslen != 0)
3608 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3610 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3611 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3614 size = BP_GET_LSIZE(bp);
3619 error = zio_read(spa, bp, buf);
3630 hdrsize = SA_HDR_SIZE(sahdrp);
3631 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3633 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3635 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3637 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3646 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3650 if (dn->dn_bonustype == DMU_OT_SA) {
3651 sa_hdr_phys_t *sahdrp = NULL;
3657 if (dn->dn_bonuslen != 0) {
3658 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3662 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3664 bp = DN_SPILL_BLKPTR(dn);
3666 size = BP_GET_LSIZE(bp);
3671 rc = zio_read(spa, bp, buf);
3678 hdrsize = SA_HDR_SIZE(sahdrp);
3679 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3680 memcpy(path, p, psize);
3685 * Second test is purely to silence bogus compiler
3686 * warning about accessing past the end of dn_bonus.
3688 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3689 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3690 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3692 rc = dnode_read(spa, dn, 0, path, psize);
3699 STAILQ_ENTRY(obj_list) entry;
3703 * Lookup a file and return its dnode.
3706 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3715 int symlinks_followed = 0;
3717 struct obj_list *entry, *tentry;
3718 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3721 if (mount->objset.os_type != DMU_OST_ZFS) {
3722 printf("ZFS: unexpected object set type %ju\n",
3723 (uintmax_t)mount->objset.os_type);
3727 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3731 * Get the root directory dnode.
3733 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3739 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3744 entry->objnum = objnum;
3745 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3747 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3753 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3762 while (*q != '\0' && *q != '/')
3766 if (p + 1 == q && p[0] == '.') {
3771 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3773 if (STAILQ_FIRST(&on_cache) ==
3774 STAILQ_LAST(&on_cache, obj_list, entry)) {
3778 entry = STAILQ_FIRST(&on_cache);
3779 STAILQ_REMOVE_HEAD(&on_cache, entry);
3781 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3784 if (q - p + 1 > sizeof(element)) {
3788 memcpy(element, p, q - p);
3792 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3794 if (!S_ISDIR(sb.st_mode)) {
3799 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3802 objnum = ZFS_DIRENT_OBJ(objnum);
3804 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3808 entry->objnum = objnum;
3809 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3810 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3815 * Check for symlink.
3817 rc = zfs_dnode_stat(spa, &dn, &sb);
3820 if (S_ISLNK(sb.st_mode)) {
3821 if (symlinks_followed > 10) {
3825 symlinks_followed++;
3828 * Read the link value and copy the tail of our
3829 * current path onto the end.
3831 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3835 strcpy(&path[sb.st_size], p);
3837 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3842 * Restart with the new path, starting either at
3843 * the root or at the parent depending whether or
3844 * not the link is relative.
3848 while (STAILQ_FIRST(&on_cache) !=
3849 STAILQ_LAST(&on_cache, obj_list, entry)) {
3850 entry = STAILQ_FIRST(&on_cache);
3851 STAILQ_REMOVE_HEAD(&on_cache, entry);
3855 entry = STAILQ_FIRST(&on_cache);
3856 STAILQ_REMOVE_HEAD(&on_cache, entry);
3859 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3865 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)