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.
34 #include <sys/endian.h>
36 #include <sys/stdint.h>
38 #include <machine/_inttypes.h>
49 static struct zfsmount zfsmount __unused;
52 * The indirect_child_t represents the vdev that we will read from, when we
53 * need to read all copies of the data (e.g. for scrub or reconstruction).
54 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
55 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
56 * ic_vdev is a child of the mirror.
58 typedef struct indirect_child {
64 * The indirect_split_t represents one mapped segment of an i/o to the
65 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
66 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
67 * For split blocks, there will be several of these.
69 typedef struct indirect_split {
70 list_node_t is_node; /* link on iv_splits */
73 * is_split_offset is the offset into the i/o.
74 * This is the sum of the previous splits' is_size's.
76 uint64_t is_split_offset;
78 vdev_t *is_vdev; /* top-level vdev */
79 uint64_t is_target_offset; /* offset on is_vdev */
81 int is_children; /* number of entries in is_child[] */
84 * is_good_child is the child that we are currently using to
85 * attempt reconstruction.
89 indirect_child_t is_child[1]; /* variable-length */
93 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
94 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
96 typedef struct indirect_vsd {
97 boolean_t iv_split_block;
98 boolean_t iv_reconstruct;
100 list_t iv_splits; /* list of indirect_split_t's */
104 * List of all vdevs, chained through v_alllink.
106 static vdev_list_t zfs_vdevs;
109 * List of ZFS features supported for read
111 static const char *features_for_read[] = {
112 "org.illumos:lz4_compress",
113 "com.delphix:hole_birth",
114 "com.delphix:extensible_dataset",
115 "com.delphix:embedded_data",
116 "org.open-zfs:large_blocks",
117 "org.illumos:sha512",
119 "org.zfsonlinux:large_dnode",
120 "com.joyent:multi_vdev_crash_dump",
121 "com.delphix:spacemap_histogram",
122 "com.delphix:zpool_checkpoint",
123 "com.delphix:spacemap_v2",
124 "com.datto:encryption",
125 "org.zfsonlinux:allocation_classes",
126 "com.datto:resilver_defer",
127 "com.delphix:device_removal",
128 "com.delphix:obsolete_counts",
129 "com.intel:allocation_classes",
134 * List of all pools, chained through spa_link.
136 static spa_list_t zfs_pools;
138 static const dnode_phys_t *dnode_cache_obj;
139 static uint64_t dnode_cache_bn;
140 static char *dnode_cache_buf;
141 static char *zap_scratch;
142 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
144 #define TEMP_SIZE (1024 * 1024)
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 zfs_temp_buf = malloc(TEMP_SIZE);
172 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
173 zfs_temp_ptr = zfs_temp_buf;
174 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
175 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
181 zfs_alloc(size_t size)
185 if (zfs_temp_ptr + size > zfs_temp_end) {
186 panic("ZFS: out of temporary buffer space");
189 zfs_temp_ptr += size;
195 zfs_free(void *ptr, size_t size)
198 zfs_temp_ptr -= size;
199 if (zfs_temp_ptr != ptr) {
200 panic("ZFS: zfs_alloc()/zfs_free() mismatch");
205 xdr_int(const unsigned char **xdr, int *ip)
213 xdr_u_int(const unsigned char **xdr, u_int *ip)
221 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
227 *lp = (((uint64_t)hi) << 32) | lo;
232 nvlist_find(const unsigned char *nvlist, const char *name, int type,
233 int *elementsp, void *valuep)
235 const unsigned char *p, *pair;
237 int encoded_size, decoded_size;
244 xdr_int(&p, &encoded_size);
245 xdr_int(&p, &decoded_size);
246 while (encoded_size && decoded_size) {
247 int namelen, pairtype, elements;
248 const char *pairname;
250 xdr_int(&p, &namelen);
251 pairname = (const char *)p;
252 p += roundup(namelen, 4);
253 xdr_int(&p, &pairtype);
255 if (memcmp(name, pairname, namelen) == 0 && type == pairtype) {
256 xdr_int(&p, &elements);
258 *elementsp = elements;
259 if (type == DATA_TYPE_UINT64) {
260 xdr_uint64_t(&p, (uint64_t *)valuep);
262 } else if (type == DATA_TYPE_STRING) {
265 (*(const char **)valuep) = (const char *)p;
267 } else if (type == DATA_TYPE_NVLIST ||
268 type == DATA_TYPE_NVLIST_ARRAY) {
269 (*(const unsigned char **)valuep) =
270 (const unsigned char *)p;
277 * Not the pair we are looking for, skip to the
280 p = pair + encoded_size;
284 xdr_int(&p, &encoded_size);
285 xdr_int(&p, &decoded_size);
292 nvlist_check_features_for_read(const unsigned char *nvlist)
294 const unsigned char *p, *pair;
296 int encoded_size, decoded_size;
306 xdr_int(&p, &encoded_size);
307 xdr_int(&p, &decoded_size);
308 while (encoded_size && decoded_size) {
309 int namelen, pairtype;
310 const char *pairname;
315 xdr_int(&p, &namelen);
316 pairname = (const char *)p;
317 p += roundup(namelen, 4);
318 xdr_int(&p, &pairtype);
320 for (i = 0; features_for_read[i] != NULL; i++) {
321 if (memcmp(pairname, features_for_read[i],
329 printf("ZFS: unsupported feature: %s\n", pairname);
333 p = pair + encoded_size;
336 xdr_int(&p, &encoded_size);
337 xdr_int(&p, &decoded_size);
344 * Return the next nvlist in an nvlist array.
346 static const unsigned char *
347 nvlist_next(const unsigned char *nvlist)
349 const unsigned char *p, *pair;
351 int encoded_size, decoded_size;
358 xdr_int(&p, &encoded_size);
359 xdr_int(&p, &decoded_size);
360 while (encoded_size && decoded_size) {
361 p = pair + encoded_size;
364 xdr_int(&p, &encoded_size);
365 xdr_int(&p, &decoded_size);
373 static const unsigned char *
374 nvlist_print(const unsigned char *nvlist, unsigned int indent)
376 static const char *typenames[] = {
387 "DATA_TYPE_BYTE_ARRAY",
388 "DATA_TYPE_INT16_ARRAY",
389 "DATA_TYPE_UINT16_ARRAY",
390 "DATA_TYPE_INT32_ARRAY",
391 "DATA_TYPE_UINT32_ARRAY",
392 "DATA_TYPE_INT64_ARRAY",
393 "DATA_TYPE_UINT64_ARRAY",
394 "DATA_TYPE_STRING_ARRAY",
397 "DATA_TYPE_NVLIST_ARRAY",
398 "DATA_TYPE_BOOLEAN_VALUE",
401 "DATA_TYPE_BOOLEAN_ARRAY",
402 "DATA_TYPE_INT8_ARRAY",
403 "DATA_TYPE_UINT8_ARRAY"
407 const unsigned char *p, *pair;
409 int encoded_size, decoded_size;
416 xdr_int(&p, &encoded_size);
417 xdr_int(&p, &decoded_size);
418 while (encoded_size && decoded_size) {
419 int namelen, pairtype, elements;
420 const char *pairname;
422 xdr_int(&p, &namelen);
423 pairname = (const char *)p;
424 p += roundup(namelen, 4);
425 xdr_int(&p, &pairtype);
427 for (i = 0; i < indent; i++)
429 printf("%s %s", typenames[pairtype], pairname);
431 xdr_int(&p, &elements);
433 case DATA_TYPE_UINT64: {
435 xdr_uint64_t(&p, &val);
436 printf(" = 0x%jx\n", (uintmax_t)val);
440 case DATA_TYPE_STRING: {
443 printf(" = \"%s\"\n", p);
447 case DATA_TYPE_NVLIST:
449 nvlist_print(p, indent + 1);
452 case DATA_TYPE_NVLIST_ARRAY:
453 for (j = 0; j < elements; j++) {
455 p = nvlist_print(p, indent + 1);
456 if (j != elements - 1) {
457 for (i = 0; i < indent; i++)
459 printf("%s %s", typenames[pairtype],
469 p = pair + encoded_size;
472 xdr_int(&p, &encoded_size);
473 xdr_int(&p, &decoded_size);
482 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
483 off_t offset, size_t size)
488 if (!vdev->v_phys_read)
492 psize = BP_GET_PSIZE(bp);
497 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
500 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
506 typedef struct remap_segment {
510 uint64_t rs_split_offset;
514 static remap_segment_t *
515 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
517 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
521 rs->rs_offset = offset;
522 rs->rs_asize = asize;
523 rs->rs_split_offset = split_offset;
529 vdev_indirect_mapping_t *
530 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
531 uint64_t mapping_object)
533 vdev_indirect_mapping_t *vim;
534 vdev_indirect_mapping_phys_t *vim_phys;
537 vim = calloc(1, sizeof (*vim));
541 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
542 if (vim->vim_dn == NULL) {
547 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
555 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
556 if (vim->vim_phys == NULL) {
562 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
563 *vim->vim_phys = *vim_phys;
565 vim->vim_objset = os;
566 vim->vim_object = mapping_object;
567 vim->vim_entries = NULL;
569 vim->vim_havecounts =
570 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
576 * Compare an offset with an indirect mapping entry; there are three
577 * possible scenarios:
579 * 1. The offset is "less than" the mapping entry; meaning the
580 * offset is less than the source offset of the mapping entry. In
581 * this case, there is no overlap between the offset and the
582 * mapping entry and -1 will be returned.
584 * 2. The offset is "greater than" the mapping entry; meaning the
585 * offset is greater than the mapping entry's source offset plus
586 * the entry's size. In this case, there is no overlap between
587 * the offset and the mapping entry and 1 will be returned.
589 * NOTE: If the offset is actually equal to the entry's offset
590 * plus size, this is considered to be "greater" than the entry,
591 * and this case applies (i.e. 1 will be returned). Thus, the
592 * entry's "range" can be considered to be inclusive at its
593 * start, but exclusive at its end: e.g. [src, src + size).
595 * 3. The last case to consider is if the offset actually falls
596 * within the mapping entry's range. If this is the case, the
597 * offset is considered to be "equal to" the mapping entry and
598 * 0 will be returned.
600 * NOTE: If the offset is equal to the entry's source offset,
601 * this case applies and 0 will be returned. If the offset is
602 * equal to the entry's source plus its size, this case does
603 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
607 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
609 const uint64_t *key = v_key;
610 const vdev_indirect_mapping_entry_phys_t *array_elem =
612 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
614 if (*key < src_offset) {
616 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
624 * Return array entry.
626 static vdev_indirect_mapping_entry_phys_t *
627 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
633 if (vim->vim_phys->vimp_num_entries == 0)
636 if (vim->vim_entries == NULL) {
639 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
640 size = vim->vim_phys->vimp_num_entries *
641 sizeof (*vim->vim_entries);
643 size = bsize / sizeof (*vim->vim_entries);
644 size *= sizeof (*vim->vim_entries);
646 vim->vim_entries = malloc(size);
647 if (vim->vim_entries == NULL)
649 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
650 offset = index * sizeof (*vim->vim_entries);
653 /* We have data in vim_entries */
655 if (index >= vim->vim_entry_offset &&
656 index <= vim->vim_entry_offset + vim->vim_num_entries) {
657 index -= vim->vim_entry_offset;
658 return (&vim->vim_entries[index]);
660 offset = index * sizeof (*vim->vim_entries);
663 vim->vim_entry_offset = index;
664 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
665 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
668 /* Read error, invalidate vim_entries. */
669 free(vim->vim_entries);
670 vim->vim_entries = NULL;
673 index -= vim->vim_entry_offset;
674 return (&vim->vim_entries[index]);
678 * Returns the mapping entry for the given offset.
680 * It's possible that the given offset will not be in the mapping table
681 * (i.e. no mapping entries contain this offset), in which case, the
682 * return value value depends on the "next_if_missing" parameter.
684 * If the offset is not found in the table and "next_if_missing" is
685 * B_FALSE, then NULL will always be returned. The behavior is intended
686 * to allow consumers to get the entry corresponding to the offset
687 * parameter, iff the offset overlaps with an entry in the table.
689 * If the offset is not found in the table and "next_if_missing" is
690 * B_TRUE, then the entry nearest to the given offset will be returned,
691 * such that the entry's source offset is greater than the offset
692 * passed in (i.e. the "next" mapping entry in the table is returned, if
693 * the offset is missing from the table). If there are no entries whose
694 * source offset is greater than the passed in offset, NULL is returned.
696 static vdev_indirect_mapping_entry_phys_t *
697 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
700 ASSERT(vim->vim_phys->vimp_num_entries > 0);
702 vdev_indirect_mapping_entry_phys_t *entry;
704 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
708 * We don't define these inside of the while loop because we use
709 * their value in the case that offset isn't in the mapping.
714 while (last >= base) {
715 mid = base + ((last - base) >> 1);
717 entry = vdev_indirect_mapping_entry(vim, mid);
720 result = dva_mapping_overlap_compare(&offset, entry);
724 } else if (result < 0) {
734 * Given an indirect vdev and an extent on that vdev, it duplicates the
735 * physical entries of the indirect mapping that correspond to the extent
736 * to a new array and returns a pointer to it. In addition, copied_entries
737 * is populated with the number of mapping entries that were duplicated.
739 * Finally, since we are doing an allocation, it is up to the caller to
740 * free the array allocated in this function.
742 vdev_indirect_mapping_entry_phys_t *
743 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
744 uint64_t asize, uint64_t *copied_entries)
746 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
747 vdev_indirect_mapping_t *vim = vd->v_mapping;
748 uint64_t entries = 0;
750 vdev_indirect_mapping_entry_phys_t *first_mapping =
751 vdev_indirect_mapping_entry_for_offset(vim, offset);
752 ASSERT3P(first_mapping, !=, NULL);
754 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
756 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
757 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
758 uint64_t inner_size = MIN(asize, size - inner_offset);
760 offset += inner_size;
766 size_t copy_length = entries * sizeof (*first_mapping);
767 duplicate_mappings = malloc(copy_length);
768 if (duplicate_mappings != NULL)
769 bcopy(first_mapping, duplicate_mappings, copy_length);
773 *copied_entries = entries;
775 return (duplicate_mappings);
779 vdev_lookup_top(spa_t *spa, uint64_t vdev)
784 vlist = &spa->spa_root_vdev->v_children;
785 STAILQ_FOREACH(rvd, vlist, v_childlink)
786 if (rvd->v_id == vdev)
793 * This is a callback for vdev_indirect_remap() which allocates an
794 * indirect_split_t for each split segment and adds it to iv_splits.
797 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
798 uint64_t size, void *arg)
802 indirect_vsd_t *iv = zio->io_vsd;
804 if (vd->v_read == vdev_indirect_read)
807 if (vd->v_read == vdev_mirror_read)
810 indirect_split_t *is =
811 malloc(offsetof(indirect_split_t, is_child[n]));
813 zio->io_error = ENOMEM;
816 bzero(is, offsetof(indirect_split_t, is_child[n]));
820 is->is_split_offset = split_offset;
821 is->is_target_offset = offset;
825 * Note that we only consider multiple copies of the data for
826 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
827 * though they use the same ops as mirror, because there's only one
828 * "good" copy under the replacing/spare.
830 if (vd->v_read == vdev_mirror_read) {
834 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
835 is->is_child[i++].ic_vdev = kid;
838 is->is_child[0].ic_vdev = vd;
841 list_insert_tail(&iv->iv_splits, is);
845 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
848 spa_t *spa = vd->v_spa;
852 list_create(&stack, sizeof (remap_segment_t),
853 offsetof(remap_segment_t, rs_node));
855 rs = rs_alloc(vd, offset, asize, 0);
857 printf("vdev_indirect_remap: out of memory.\n");
858 zio->io_error = ENOMEM;
860 for (; rs != NULL; rs = list_remove_head(&stack)) {
861 vdev_t *v = rs->rs_vd;
862 uint64_t num_entries = 0;
863 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
864 vdev_indirect_mapping_entry_phys_t *mapping =
865 vdev_indirect_mapping_duplicate_adjacent_entries(v,
866 rs->rs_offset, rs->rs_asize, &num_entries);
868 if (num_entries == 0)
869 zio->io_error = ENOMEM;
871 for (uint64_t i = 0; i < num_entries; i++) {
872 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
873 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
874 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
875 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
876 uint64_t inner_offset = rs->rs_offset -
877 DVA_MAPPING_GET_SRC_OFFSET(m);
878 uint64_t inner_size =
879 MIN(rs->rs_asize, size - inner_offset);
880 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
882 if (dst_v->v_read == vdev_indirect_read) {
885 o = rs_alloc(dst_v, dst_offset + inner_offset,
886 inner_size, rs->rs_split_offset);
888 printf("vdev_indirect_remap: "
890 zio->io_error = ENOMEM;
894 list_insert_head(&stack, o);
896 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
897 dst_offset + inner_offset,
901 * vdev_indirect_gather_splits can have memory
902 * allocation error, we can not recover from it.
904 if (zio->io_error != 0)
906 rs->rs_offset += inner_size;
907 rs->rs_asize -= inner_size;
908 rs->rs_split_offset += inner_size;
913 if (zio->io_error != 0)
917 list_destroy(&stack);
921 vdev_indirect_map_free(zio_t *zio)
923 indirect_vsd_t *iv = zio->io_vsd;
924 indirect_split_t *is;
926 while ((is = list_head(&iv->iv_splits)) != NULL) {
927 for (int c = 0; c < is->is_children; c++) {
928 indirect_child_t *ic = &is->is_child[c];
931 list_remove(&iv->iv_splits, is);
938 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
939 off_t offset, size_t bytes)
942 spa_t *spa = vdev->v_spa;
944 indirect_split_t *first;
947 iv = calloc(1, sizeof(*iv));
951 list_create(&iv->iv_splits,
952 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
954 bzero(&zio, sizeof(zio));
956 zio.io_bp = (blkptr_t *)bp;
959 zio.io_offset = offset;
963 if (vdev->v_mapping == NULL) {
964 vdev_indirect_config_t *vic;
966 vic = &vdev->vdev_indirect_config;
967 vdev->v_mapping = vdev_indirect_mapping_open(spa,
968 &spa->spa_mos, vic->vic_mapping_object);
971 vdev_indirect_remap(vdev, offset, bytes, &zio);
972 if (zio.io_error != 0)
973 return (zio.io_error);
975 first = list_head(&iv->iv_splits);
976 if (first->is_size == zio.io_size) {
978 * This is not a split block; we are pointing to the entire
979 * data, which will checksum the same as the original data.
980 * Pass the BP down so that the child i/o can verify the
981 * checksum, and try a different location if available
982 * (e.g. on a mirror).
984 * While this special case could be handled the same as the
985 * general (split block) case, doing it this way ensures
986 * that the vast majority of blocks on indirect vdevs
987 * (which are not split) are handled identically to blocks
988 * on non-indirect vdevs. This allows us to be less strict
989 * about performance in the general (but rare) case.
991 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
992 zio.io_data, first->is_target_offset, bytes);
994 iv->iv_split_block = B_TRUE;
996 * Read one copy of each split segment, from the
997 * top-level vdev. Since we don't know the
998 * checksum of each split individually, the child
999 * zio can't ensure that we get the right data.
1000 * E.g. if it's a mirror, it will just read from a
1001 * random (healthy) leaf vdev. We have to verify
1002 * the checksum in vdev_indirect_io_done().
1004 for (indirect_split_t *is = list_head(&iv->iv_splits);
1005 is != NULL; is = list_next(&iv->iv_splits, is)) {
1006 char *ptr = zio.io_data;
1008 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
1009 ptr + is->is_split_offset, is->is_target_offset,
1012 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
1018 vdev_indirect_map_free(&zio);
1026 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1027 off_t offset, size_t bytes)
1030 return (vdev_read_phys(vdev, bp, buf,
1031 offset + VDEV_LABEL_START_SIZE, bytes));
1036 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1037 off_t offset, size_t bytes)
1043 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1044 if (kid->v_state != VDEV_STATE_HEALTHY)
1046 rc = kid->v_read(kid, bp, buf, offset, bytes);
1055 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1056 off_t offset, size_t bytes)
1061 * Here we should have two kids:
1062 * First one which is the one we are replacing and we can trust
1063 * only this one to have valid data, but it might not be present.
1064 * Second one is that one we are replacing with. It is most likely
1065 * healthy, but we can't trust it has needed data, so we won't use it.
1067 kid = STAILQ_FIRST(&vdev->v_children);
1070 if (kid->v_state != VDEV_STATE_HEALTHY)
1072 return (kid->v_read(kid, bp, buf, offset, bytes));
1076 vdev_find(uint64_t guid)
1080 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
1081 if (vdev->v_guid == guid)
1088 vdev_create(uint64_t guid, vdev_read_t *_read)
1091 vdev_indirect_config_t *vic;
1093 vdev = calloc(1, sizeof(vdev_t));
1095 STAILQ_INIT(&vdev->v_children);
1096 vdev->v_guid = guid;
1097 vdev->v_read = _read;
1100 * root vdev has no read function.
1101 * We only point root vdev from spa.
1103 if (_read != NULL) {
1104 vic = &vdev->vdev_indirect_config;
1105 vic->vic_prev_indirect_vdev = UINT64_MAX;
1106 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
1114 vdev_set_initial_state(vdev_t *vdev, const unsigned char *nvlist)
1116 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
1119 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
1121 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
1123 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
1125 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
1127 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
1128 NULL, &is_degraded);
1129 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
1130 NULL, &isnt_present);
1131 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
1134 if (is_offline != 0)
1135 vdev->v_state = VDEV_STATE_OFFLINE;
1136 else if (is_removed != 0)
1137 vdev->v_state = VDEV_STATE_REMOVED;
1138 else if (is_faulted != 0)
1139 vdev->v_state = VDEV_STATE_FAULTED;
1140 else if (is_degraded != 0)
1141 vdev->v_state = VDEV_STATE_DEGRADED;
1142 else if (isnt_present != 0)
1143 vdev->v_state = VDEV_STATE_CANT_OPEN;
1145 vdev->v_islog = is_log == 1;
1149 vdev_init(uint64_t guid, const unsigned char *nvlist, vdev_t **vdevp)
1151 uint64_t id, ashift, asize, nparity;
1156 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id) ||
1157 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1162 if (strcmp(type, VDEV_TYPE_MIRROR) != 0 &&
1163 strcmp(type, VDEV_TYPE_DISK) != 0 &&
1165 strcmp(type, VDEV_TYPE_FILE) != 0 &&
1167 strcmp(type, VDEV_TYPE_RAIDZ) != 0 &&
1168 strcmp(type, VDEV_TYPE_INDIRECT) != 0 &&
1169 strcmp(type, VDEV_TYPE_REPLACING) != 0) {
1170 printf("ZFS: can only boot from disk, mirror, raidz1, "
1171 "raidz2 and raidz3 vdevs\n");
1175 if (strcmp(type, VDEV_TYPE_MIRROR) == 0)
1176 vdev = vdev_create(guid, vdev_mirror_read);
1177 else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0)
1178 vdev = vdev_create(guid, vdev_raidz_read);
1179 else if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
1180 vdev = vdev_create(guid, vdev_replacing_read);
1181 else if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
1182 vdev_indirect_config_t *vic;
1184 vdev = vdev_create(guid, vdev_indirect_read);
1186 vdev->v_state = VDEV_STATE_HEALTHY;
1187 vic = &vdev->vdev_indirect_config;
1190 ZPOOL_CONFIG_INDIRECT_OBJECT,
1192 NULL, &vic->vic_mapping_object);
1194 ZPOOL_CONFIG_INDIRECT_BIRTHS,
1196 NULL, &vic->vic_births_object);
1198 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
1200 NULL, &vic->vic_prev_indirect_vdev);
1203 vdev = vdev_create(guid, vdev_disk_read);
1209 vdev_set_initial_state(vdev, nvlist);
1211 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
1212 DATA_TYPE_UINT64, NULL, &ashift) == 0)
1213 vdev->v_ashift = ashift;
1215 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1216 DATA_TYPE_UINT64, NULL, &asize) == 0) {
1217 vdev->v_psize = asize +
1218 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1221 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1222 DATA_TYPE_UINT64, NULL, &nparity) == 0)
1223 vdev->v_nparity = nparity;
1225 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1226 DATA_TYPE_STRING, NULL, &path) == 0) {
1227 if (strncmp(path, "/dev/", 5) == 0)
1229 vdev->v_name = strdup(path);
1234 if (strcmp(type, "raidz") == 0) {
1235 if (vdev->v_nparity < 1 ||
1236 vdev->v_nparity > 3) {
1237 printf("ZFS: can only boot from disk, "
1238 "mirror, raidz1, raidz2 and raidz3 "
1242 (void) asprintf(&name, "%s%d-%" PRIu64, type,
1243 vdev->v_nparity, id);
1245 (void) asprintf(&name, "%s-%" PRIu64, type, id);
1247 vdev->v_name = name;
1254 * Find slot for vdev. We return either NULL to signal to use
1255 * STAILQ_INSERT_HEAD, or we return link element to be used with
1256 * STAILQ_INSERT_AFTER.
1259 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1261 vdev_t *v, *previous;
1263 if (STAILQ_EMPTY(&top_vdev->v_children))
1267 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1268 if (v->v_id > vdev->v_id)
1271 if (v->v_id == vdev->v_id)
1274 if (v->v_id < vdev->v_id)
1281 vdev_child_count(vdev_t *vdev)
1287 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1294 * Insert vdev into top_vdev children list. List is ordered by v_id.
1297 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1303 * The top level vdev can appear in random order, depending how
1304 * the firmware is presenting the disk devices.
1305 * However, we will insert vdev to create list ordered by v_id,
1306 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1307 * as STAILQ does not have insert before.
1309 previous = vdev_find_previous(top_vdev, vdev);
1311 if (previous == NULL) {
1312 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1313 count = vdev_child_count(top_vdev);
1314 if (top_vdev->v_nchildren < count)
1315 top_vdev->v_nchildren = count;
1319 if (previous->v_id == vdev->v_id)
1322 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev, v_childlink);
1323 count = vdev_child_count(top_vdev);
1324 if (top_vdev->v_nchildren < count)
1325 top_vdev->v_nchildren = count;
1329 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const unsigned char *nvlist)
1331 vdev_t *top_vdev, *vdev;
1332 const unsigned char *kids;
1336 top_vdev = vdev_find(top_guid);
1337 if (top_vdev == NULL) {
1338 rc = vdev_init(top_guid, nvlist, &top_vdev);
1341 top_vdev->v_spa = spa;
1342 top_vdev->v_top = top_vdev;
1343 vdev_insert(spa->spa_root_vdev, top_vdev);
1346 /* Add children if there are any. */
1347 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1350 for (int i = 0; i < nkids; i++) {
1353 rc = nvlist_find(kids, ZPOOL_CONFIG_GUID,
1354 DATA_TYPE_UINT64, NULL, &guid);
1357 rc = vdev_init(guid, kids, &vdev);
1362 vdev->v_top = top_vdev;
1363 vdev_insert(top_vdev, vdev);
1365 kids = nvlist_next(kids);
1375 vdev_init_from_label(spa_t *spa, const unsigned char *nvlist)
1377 uint64_t pool_guid, top_guid;
1378 const unsigned char *vdevs;
1380 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1381 NULL, &pool_guid) ||
1382 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1384 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1386 printf("ZFS: can't find vdev details\n");
1390 return (vdev_from_nvlist(spa, top_guid, vdevs));
1394 vdev_set_state(vdev_t *vdev)
1400 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1401 vdev_set_state(kid);
1405 * A mirror or raidz is healthy if all its kids are healthy. A
1406 * mirror is degraded if any of its kids is healthy; a raidz
1407 * is degraded if at most nparity kids are offline.
1409 if (STAILQ_FIRST(&vdev->v_children)) {
1412 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1413 if (kid->v_state == VDEV_STATE_HEALTHY)
1418 if (bad_kids == 0) {
1419 vdev->v_state = VDEV_STATE_HEALTHY;
1421 if (vdev->v_read == vdev_mirror_read) {
1423 vdev->v_state = VDEV_STATE_DEGRADED;
1425 vdev->v_state = VDEV_STATE_OFFLINE;
1427 } else if (vdev->v_read == vdev_raidz_read) {
1428 if (bad_kids > vdev->v_nparity) {
1429 vdev->v_state = VDEV_STATE_OFFLINE;
1431 vdev->v_state = VDEV_STATE_DEGRADED;
1439 vdev_update_from_nvlist(uint64_t top_guid, const unsigned char *nvlist)
1442 const unsigned char *kids;
1445 /* Update top vdev. */
1446 vdev = vdev_find(top_guid);
1448 vdev_set_initial_state(vdev, nvlist);
1450 /* Update children if there are any. */
1451 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1454 for (int i = 0; i < nkids; i++) {
1457 rc = nvlist_find(kids, ZPOOL_CONFIG_GUID,
1458 DATA_TYPE_UINT64, NULL, &guid);
1462 vdev = vdev_find(guid);
1464 vdev_set_initial_state(vdev, kids);
1466 kids = nvlist_next(kids);
1476 vdev_init_from_nvlist(spa_t *spa, const unsigned char *nvlist)
1478 uint64_t pool_guid, vdev_children;
1479 const unsigned char *vdevs, *kids;
1482 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1483 NULL, &pool_guid) ||
1484 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1485 NULL, &vdev_children) ||
1486 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1488 printf("ZFS: can't find vdev details\n");
1493 if (spa->spa_guid != pool_guid)
1496 spa->spa_root_vdev->v_nchildren = vdev_children;
1498 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1502 * MOS config has at least one child for root vdev.
1507 for (int i = 0; i < nkids; i++) {
1511 rc = nvlist_find(kids, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1515 vdev = vdev_find(guid);
1517 * Top level vdev is missing, create it.
1520 rc = vdev_from_nvlist(spa, guid, kids);
1522 rc = vdev_update_from_nvlist(guid, kids);
1525 kids = nvlist_next(kids);
1529 * Re-evaluate top-level vdev state.
1531 vdev_set_state(spa->spa_root_vdev);
1537 spa_find_by_guid(uint64_t guid)
1541 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1542 if (spa->spa_guid == guid)
1549 spa_find_by_name(const char *name)
1553 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1554 if (strcmp(spa->spa_name, name) == 0)
1562 spa_get_primary(void)
1565 return (STAILQ_FIRST(&zfs_pools));
1569 spa_get_primary_vdev(const spa_t *spa)
1575 spa = spa_get_primary();
1578 vdev = spa->spa_root_vdev;
1581 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
1582 kid = STAILQ_FIRST(&vdev->v_children))
1589 spa_create(uint64_t guid, const char *name)
1593 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1595 if ((spa->spa_name = strdup(name)) == NULL) {
1599 spa->spa_guid = guid;
1600 spa->spa_root_vdev = vdev_create(guid, NULL);
1601 if (spa->spa_root_vdev == NULL) {
1602 free(spa->spa_name);
1606 spa->spa_root_vdev->v_name = strdup("root");
1607 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1613 state_name(vdev_state_t state)
1615 static const char *names[] = {
1625 return (names[state]);
1630 #define pager_printf printf
1635 pager_printf(const char *fmt, ...)
1640 va_start(args, fmt);
1641 vsnprintf(line, sizeof(line), fmt, args);
1643 return (pager_output(line));
1648 #define STATUS_FORMAT " %s %s\n"
1651 print_state(int indent, const char *name, vdev_state_t state)
1657 for (i = 0; i < indent; i++)
1660 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1664 vdev_status(vdev_t *vdev, int indent)
1669 if (vdev->v_islog) {
1670 (void) pager_output(" logs\n");
1674 ret = print_state(indent, vdev->v_name, vdev->v_state);
1678 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1679 ret = vdev_status(kid, indent + 1);
1687 spa_status(spa_t *spa)
1689 static char bootfs[ZFS_MAXNAMELEN];
1693 int good_kids, bad_kids, degraded_kids, ret;
1696 ret = pager_printf(" pool: %s\n", spa->spa_name);
1700 if (zfs_get_root(spa, &rootid) == 0 &&
1701 zfs_rlookup(spa, rootid, bootfs) == 0) {
1702 if (bootfs[0] == '\0')
1703 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1705 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1710 ret = pager_printf("config:\n\n");
1713 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1720 vlist = &spa->spa_root_vdev->v_children;
1721 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1722 if (vdev->v_state == VDEV_STATE_HEALTHY)
1724 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1730 state = VDEV_STATE_CLOSED;
1731 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1732 state = VDEV_STATE_HEALTHY;
1733 else if ((good_kids + degraded_kids) > 0)
1734 state = VDEV_STATE_DEGRADED;
1736 ret = print_state(0, spa->spa_name, state);
1740 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1741 ret = vdev_status(vdev, 1);
1749 spa_all_status(void)
1752 int first = 1, ret = 0;
1754 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1756 ret = pager_printf("\n");
1761 ret = spa_status(spa);
1769 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1771 uint64_t label_offset;
1773 if (l < VDEV_LABELS / 2)
1776 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1778 return (offset + l * sizeof (vdev_label_t) + label_offset);
1782 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1784 unsigned int seq1 = 0;
1785 unsigned int seq2 = 0;
1786 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1791 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1795 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1796 seq1 = MMP_SEQ(ub1);
1798 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1799 seq2 = MMP_SEQ(ub2);
1801 return (AVL_CMP(seq1, seq2));
1805 uberblock_verify(uberblock_t *ub)
1807 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1808 byteswap_uint64_array(ub, sizeof (uberblock_t));
1811 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1812 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1819 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1825 off = vdev_label_offset(vd->v_psize, l, offset);
1828 BP_SET_LSIZE(&bp, size);
1829 BP_SET_PSIZE(&bp, size);
1830 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1831 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1832 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1833 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1835 return (vdev_read_phys(vd, &bp, buf, off, size));
1838 static unsigned char *
1839 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1842 uint64_t best_txg = 0;
1843 uint64_t label_txg = 0;
1849 label = malloc(sizeof (vdev_phys_t));
1853 nvl_size = VDEV_PHYS_SIZE - sizeof (zio_eck_t) - 4;
1854 nvl = malloc(nvl_size);
1858 for (int l = 0; l < VDEV_LABELS; l++) {
1859 const unsigned char *nvlist;
1861 if (vdev_label_read(vd, l, label,
1862 offsetof(vdev_label_t, vl_vdev_phys),
1863 sizeof (vdev_phys_t)))
1866 if (label->vp_nvlist[0] != NV_ENCODE_XDR)
1869 nvlist = (const unsigned char *) label->vp_nvlist + 4;
1870 error = nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
1871 DATA_TYPE_UINT64, NULL, &label_txg);
1872 if (error != 0 || label_txg == 0) {
1873 memcpy(nvl, nvlist, nvl_size);
1877 if (label_txg <= txg && label_txg > best_txg) {
1878 best_txg = label_txg;
1879 memcpy(nvl, nvlist, nvl_size);
1882 * Use asize from pool config. We need this
1883 * because we can get bad value from BIOS.
1885 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1886 DATA_TYPE_UINT64, NULL, &asize) == 0) {
1887 vd->v_psize = asize +
1888 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1893 if (best_txg == 0) {
1903 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1907 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1911 for (int l = 0; l < VDEV_LABELS; l++) {
1912 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1913 if (vdev_label_read(vd, l, buf,
1914 VDEV_UBERBLOCK_OFFSET(vd, n),
1915 VDEV_UBERBLOCK_SIZE(vd)))
1917 if (uberblock_verify(buf) != 0)
1920 if (vdev_uberblock_compare(buf, ub) > 0)
1928 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
1933 unsigned char *nvlist;
1935 uint64_t guid, vdev_children;
1936 uint64_t pool_txg, pool_guid;
1937 const char *pool_name;
1938 const unsigned char *features;
1942 * Load the vdev label and figure out which
1943 * uberblock is most current.
1945 memset(&vtmp, 0, sizeof(vtmp));
1946 vtmp.v_phys_read = _read;
1947 vtmp.v_read_priv = read_priv;
1948 vtmp.v_psize = P2ALIGN(ldi_get_size(read_priv),
1949 (uint64_t)sizeof (vdev_label_t));
1951 /* Test for minimum device size. */
1952 if (vtmp.v_psize < SPA_MINDEVSIZE)
1955 nvlist = vdev_label_read_config(&vtmp, UINT64_MAX);
1959 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1965 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1966 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1967 (unsigned)val, (unsigned)SPA_VERSION);
1972 /* Check ZFS features for read */
1973 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1974 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1975 nvlist_check_features_for_read(features) != 0) {
1980 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1986 if (val == POOL_STATE_DESTROYED) {
1987 /* We don't boot only from destroyed pools. */
1992 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1993 NULL, &pool_txg) != 0 ||
1994 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1995 NULL, &pool_guid) != 0 ||
1996 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1997 NULL, &pool_name) != 0) {
1999 * Cache and spare devices end up here - just ignore
2007 * Create the pool if this is the first time we've seen it.
2009 spa = spa_find_by_guid(pool_guid);
2011 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN,
2012 DATA_TYPE_UINT64, NULL, &vdev_children);
2013 spa = spa_create(pool_guid, pool_name);
2018 spa->spa_root_vdev->v_nchildren = vdev_children;
2020 if (pool_txg > spa->spa_txg)
2021 spa->spa_txg = pool_txg;
2024 * Get the vdev tree and create our in-core copy of it.
2025 * If we already have a vdev with this guid, this must
2026 * be some kind of alias (overlapping slices, dangerously dedicated
2029 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2030 NULL, &guid) != 0) {
2034 vdev = vdev_find(guid);
2035 /* Has this vdev already been inited? */
2036 if (vdev && vdev->v_phys_read) {
2041 rc = vdev_init_from_label(spa, nvlist);
2047 * We should already have created an incomplete vdev for this
2048 * vdev. Find it and initialise it with our read proc.
2050 vdev = vdev_find(guid);
2052 vdev->v_phys_read = _read;
2053 vdev->v_read_priv = read_priv;
2054 vdev->v_psize = vtmp.v_psize;
2056 * If no other state is set, mark vdev healthy.
2058 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2059 vdev->v_state = VDEV_STATE_HEALTHY;
2061 printf("ZFS: inconsistent nvlist contents\n");
2066 spa->spa_with_log = vdev->v_islog;
2069 * Re-evaluate top-level vdev state.
2071 vdev_set_state(vdev->v_top);
2074 * Ok, we are happy with the pool so far. Lets find
2075 * the best uberblock and then we can actually access
2076 * the contents of the pool.
2078 vdev_uberblock_load(vdev, &spa->spa_uberblock);
2090 for (v = 0; v < 32; v++)
2097 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2100 zio_gbh_phys_t zio_gb;
2104 /* Artificial BP for gang block header. */
2106 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2107 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2108 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2109 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2110 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2111 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2113 /* Read gang header block using the artificial BP. */
2114 if (zio_read(spa, &gbh_bp, &zio_gb))
2118 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2119 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2121 if (BP_IS_HOLE(gbp))
2123 if (zio_read(spa, gbp, pbuf))
2125 pbuf += BP_GET_PSIZE(gbp);
2128 if (zio_checksum_verify(spa, bp, buf))
2134 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2136 int cpfunc = BP_GET_COMPRESS(bp);
2137 uint64_t align, size;
2142 * Process data embedded in block pointer
2144 if (BP_IS_EMBEDDED(bp)) {
2145 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2147 size = BPE_GET_PSIZE(bp);
2148 ASSERT(size <= BPE_PAYLOAD_SIZE);
2150 if (cpfunc != ZIO_COMPRESS_OFF)
2151 pbuf = zfs_alloc(size);
2155 decode_embedded_bp_compressed(bp, pbuf);
2158 if (cpfunc != ZIO_COMPRESS_OFF) {
2159 error = zio_decompress_data(cpfunc, pbuf,
2160 size, buf, BP_GET_LSIZE(bp));
2161 zfs_free(pbuf, size);
2164 printf("ZFS: i/o error - unable to decompress "
2165 "block pointer data, error %d\n", error);
2171 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2172 const dva_t *dva = &bp->blk_dva[i];
2178 if (!dva->dva_word[0] && !dva->dva_word[1])
2181 vdevid = DVA_GET_VDEV(dva);
2182 offset = DVA_GET_OFFSET(dva);
2183 vlist = &spa->spa_root_vdev->v_children;
2184 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2185 if (vdev->v_id == vdevid)
2188 if (!vdev || !vdev->v_read)
2191 size = BP_GET_PSIZE(bp);
2192 if (vdev->v_read == vdev_raidz_read) {
2193 align = 1ULL << vdev->v_ashift;
2194 if (P2PHASE(size, align) != 0)
2195 size = P2ROUNDUP(size, align);
2197 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2198 pbuf = zfs_alloc(size);
2202 if (DVA_GET_GANG(dva))
2203 error = zio_read_gang(spa, bp, pbuf);
2205 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2207 if (cpfunc != ZIO_COMPRESS_OFF)
2208 error = zio_decompress_data(cpfunc, pbuf,
2209 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2210 else if (size != BP_GET_PSIZE(bp))
2211 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2214 zfs_free(pbuf, size);
2219 printf("ZFS: i/o error - all block copies unavailable\n");
2224 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2225 void *buf, size_t buflen)
2227 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2228 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2229 int nlevels = dnode->dn_nlevels;
2232 if (bsize > SPA_MAXBLOCKSIZE) {
2233 printf("ZFS: I/O error - blocks larger than %llu are not "
2234 "supported\n", SPA_MAXBLOCKSIZE);
2239 * Note: bsize may not be a power of two here so we need to do an
2240 * actual divide rather than a bitshift.
2242 while (buflen > 0) {
2243 uint64_t bn = offset / bsize;
2244 int boff = offset % bsize;
2246 const blkptr_t *indbp;
2249 if (bn > dnode->dn_maxblkid)
2252 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2255 indbp = dnode->dn_blkptr;
2256 for (i = 0; i < nlevels; i++) {
2258 * Copy the bp from the indirect array so that
2259 * we can re-use the scratch buffer for multi-level
2262 ibn = bn >> ((nlevels - i - 1) * ibshift);
2263 ibn &= ((1 << ibshift) - 1);
2265 if (BP_IS_HOLE(&bp)) {
2266 memset(dnode_cache_buf, 0, bsize);
2269 rc = zio_read(spa, &bp, dnode_cache_buf);
2272 indbp = (const blkptr_t *) dnode_cache_buf;
2274 dnode_cache_obj = dnode;
2275 dnode_cache_bn = bn;
2279 * The buffer contains our data block. Copy what we
2280 * need from it and loop.
2283 if (i > buflen) i = buflen;
2284 memcpy(buf, &dnode_cache_buf[boff], i);
2285 buf = ((char *)buf) + i;
2294 * Lookup a value in a microzap directory. Assumes that the zap
2295 * scratch buffer contains the directory contents.
2298 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
2300 const mzap_phys_t *mz;
2301 const mzap_ent_phys_t *mze;
2306 * Microzap objects use exactly one block. Read the whole
2309 size = dnode->dn_datablkszsec * 512;
2311 mz = (const mzap_phys_t *) zap_scratch;
2312 chunks = size / MZAP_ENT_LEN - 1;
2314 for (i = 0; i < chunks; i++) {
2315 mze = &mz->mz_chunk[i];
2316 if (strcmp(mze->mze_name, name) == 0) {
2317 *value = mze->mze_value;
2326 * Compare a name with a zap leaf entry. Return non-zero if the name
2330 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2334 const zap_leaf_chunk_t *nc;
2337 namelen = zc->l_entry.le_name_numints;
2339 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2341 while (namelen > 0) {
2345 if (len > ZAP_LEAF_ARRAY_BYTES)
2346 len = ZAP_LEAF_ARRAY_BYTES;
2347 if (memcmp(p, nc->l_array.la_array, len))
2351 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2358 * Extract a uint64_t value from a zap leaf entry.
2361 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2363 const zap_leaf_chunk_t *vc;
2368 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2369 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2370 value = (value << 8) | p[i];
2377 stv(int len, void *addr, uint64_t value)
2381 *(uint8_t *)addr = value;
2384 *(uint16_t *)addr = value;
2387 *(uint32_t *)addr = value;
2390 *(uint64_t *)addr = value;
2396 * Extract a array from a zap leaf entry.
2399 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2400 uint64_t integer_size, uint64_t num_integers, void *buf)
2402 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2404 uint64_t *u64 = buf;
2406 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2407 int chunk = zc->l_entry.le_value_chunk;
2410 if (integer_size == 8 && len == 1) {
2411 *u64 = fzap_leaf_value(zl, zc);
2416 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2419 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2420 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2421 value = (value << 8) | la->la_array[i];
2423 if (byten == array_int_len) {
2424 stv(integer_size, p, value);
2432 chunk = la->la_next;
2437 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2440 switch (integer_size) {
2450 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2457 * Lookup a value in a fatzap directory. Assumes that the zap scratch
2458 * buffer contains the directory header.
2461 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2462 uint64_t integer_size, uint64_t num_integers, void *value)
2464 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2465 zap_phys_t zh = *(zap_phys_t *)zap_scratch;
2471 if (zh.zap_magic != ZAP_MAGIC)
2474 if ((rc = fzap_check_size(integer_size, num_integers)) != 0)
2477 z.zap_block_shift = ilog2(bsize);
2478 z.zap_phys = (zap_phys_t *)zap_scratch;
2481 * Figure out where the pointer table is and read it in if necessary.
2483 if (zh.zap_ptrtbl.zt_blk) {
2484 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
2485 zap_scratch, bsize);
2488 ptrtbl = (uint64_t *)zap_scratch;
2490 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
2493 hash = zap_hash(zh.zap_salt, name);
2496 zl.l_bs = z.zap_block_shift;
2498 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
2499 zap_leaf_chunk_t *zc;
2501 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
2505 zl.l_phys = (zap_leaf_phys_t *)zap_scratch;
2508 * Make sure this chunk matches our hash.
2510 if (zl.l_phys->l_hdr.lh_prefix_len > 0 &&
2511 zl.l_phys->l_hdr.lh_prefix !=
2512 hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
2516 * Hash within the chunk to find our entry.
2518 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) -
2519 zl.l_phys->l_hdr.lh_prefix_len);
2520 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
2521 h = zl.l_phys->l_hash[h];
2524 zc = &ZAP_LEAF_CHUNK(&zl, h);
2525 while (zc->l_entry.le_hash != hash) {
2526 if (zc->l_entry.le_next == 0xffff)
2528 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
2530 if (fzap_name_equal(&zl, zc, name)) {
2531 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
2532 integer_size * num_integers)
2534 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
2542 * Lookup a name in a zap object and return its value as a uint64_t.
2545 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2546 uint64_t integer_size, uint64_t num_integers, void *value)
2550 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2552 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
2556 zap_type = *(uint64_t *)zap_scratch;
2557 if (zap_type == ZBT_MICRO)
2558 return (mzap_lookup(dnode, name, value));
2559 else if (zap_type == ZBT_HEADER) {
2560 return (fzap_lookup(spa, dnode, name, integer_size,
2561 num_integers, value));
2563 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
2568 * List a microzap directory. Assumes that the zap scratch buffer contains
2569 * the directory contents.
2572 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
2574 const mzap_phys_t *mz;
2575 const mzap_ent_phys_t *mze;
2580 * Microzap objects use exactly one block. Read the whole
2583 size = dnode->dn_datablkszsec * 512;
2584 mz = (const mzap_phys_t *) zap_scratch;
2585 chunks = size / MZAP_ENT_LEN - 1;
2587 for (i = 0; i < chunks; i++) {
2588 mze = &mz->mz_chunk[i];
2589 if (mze->mze_name[0]) {
2590 rc = callback(mze->mze_name, mze->mze_value);
2600 * List a fatzap directory. Assumes that the zap scratch buffer contains
2601 * the directory header.
2604 fzap_list(const spa_t *spa, const dnode_phys_t *dnode,
2605 int (*callback)(const char *, uint64_t))
2607 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2608 zap_phys_t zh = *(zap_phys_t *)zap_scratch;
2612 if (zh.zap_magic != ZAP_MAGIC)
2615 z.zap_block_shift = ilog2(bsize);
2616 z.zap_phys = (zap_phys_t *)zap_scratch;
2619 * This assumes that the leaf blocks start at block 1. The
2620 * documentation isn't exactly clear on this.
2623 zl.l_bs = z.zap_block_shift;
2624 for (i = 0; i < zh.zap_num_leafs; i++) {
2625 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2629 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
2632 zl.l_phys = (zap_leaf_phys_t *)zap_scratch;
2634 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2635 zap_leaf_chunk_t *zc, *nc;
2638 zc = &ZAP_LEAF_CHUNK(&zl, j);
2639 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2641 namelen = zc->l_entry.le_name_numints;
2642 if (namelen > sizeof(name))
2643 namelen = sizeof(name);
2646 * Paste the name back together.
2648 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2650 while (namelen > 0) {
2653 if (len > ZAP_LEAF_ARRAY_BYTES)
2654 len = ZAP_LEAF_ARRAY_BYTES;
2655 memcpy(p, nc->l_array.la_array, len);
2658 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2662 * Assume the first eight bytes of the value are
2665 value = fzap_leaf_value(&zl, zc);
2667 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2668 rc = callback((const char *)name, value);
2677 static int zfs_printf(const char *name, uint64_t value __unused)
2680 printf("%s\n", name);
2686 * List a zap directory.
2689 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2692 size_t size = dnode->dn_datablkszsec * 512;
2694 if (dnode_read(spa, dnode, 0, zap_scratch, size))
2697 zap_type = *(uint64_t *)zap_scratch;
2698 if (zap_type == ZBT_MICRO)
2699 return (mzap_list(dnode, zfs_printf));
2701 return (fzap_list(spa, dnode, zfs_printf));
2705 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2706 dnode_phys_t *dnode)
2710 offset = objnum * sizeof(dnode_phys_t);
2711 return dnode_read(spa, &os->os_meta_dnode, offset,
2712 dnode, sizeof(dnode_phys_t));
2716 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
2719 const mzap_phys_t *mz;
2720 const mzap_ent_phys_t *mze;
2725 * Microzap objects use exactly one block. Read the whole
2728 size = dnode->dn_datablkszsec * 512;
2730 mz = (const mzap_phys_t *)zap_scratch;
2731 chunks = size / MZAP_ENT_LEN - 1;
2733 for (i = 0; i < chunks; i++) {
2734 mze = &mz->mz_chunk[i];
2735 if (value == mze->mze_value) {
2736 strcpy(name, mze->mze_name);
2745 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2748 const zap_leaf_chunk_t *nc;
2751 namelen = zc->l_entry.le_name_numints;
2753 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2755 while (namelen > 0) {
2758 if (len > ZAP_LEAF_ARRAY_BYTES)
2759 len = ZAP_LEAF_ARRAY_BYTES;
2760 memcpy(p, nc->l_array.la_array, len);
2763 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2770 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
2773 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2774 zap_phys_t zh = *(zap_phys_t *)zap_scratch;
2778 if (zh.zap_magic != ZAP_MAGIC)
2781 z.zap_block_shift = ilog2(bsize);
2782 z.zap_phys = (zap_phys_t *)zap_scratch;
2785 * This assumes that the leaf blocks start at block 1. The
2786 * documentation isn't exactly clear on this.
2789 zl.l_bs = z.zap_block_shift;
2790 for (i = 0; i < zh.zap_num_leafs; i++) {
2791 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2793 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
2796 zl.l_phys = (zap_leaf_phys_t *)zap_scratch;
2798 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2799 zap_leaf_chunk_t *zc;
2801 zc = &ZAP_LEAF_CHUNK(&zl, j);
2802 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2804 if (zc->l_entry.le_value_intlen != 8 ||
2805 zc->l_entry.le_value_numints != 1)
2808 if (fzap_leaf_value(&zl, zc) == value) {
2809 fzap_name_copy(&zl, zc, name);
2819 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
2824 size_t size = dnode->dn_datablkszsec * 512;
2826 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
2830 zap_type = *(uint64_t *)zap_scratch;
2831 if (zap_type == ZBT_MICRO)
2832 return (mzap_rlookup(spa, dnode, name, value));
2834 return (fzap_rlookup(spa, dnode, name, value));
2838 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
2841 char component[256];
2842 uint64_t dir_obj, parent_obj, child_dir_zapobj;
2843 dnode_phys_t child_dir_zap, dataset, dir, parent;
2845 dsl_dataset_phys_t *ds;
2849 p = &name[sizeof(name) - 1];
2852 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2853 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2856 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
2857 dir_obj = ds->ds_dir_obj;
2860 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
2862 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2864 /* Actual loop condition. */
2865 parent_obj = dd->dd_parent_obj;
2866 if (parent_obj == 0)
2869 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj,
2872 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
2873 child_dir_zapobj = dd->dd_child_dir_zapobj;
2874 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
2875 &child_dir_zap) != 0)
2877 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
2880 len = strlen(component);
2882 memcpy(p, component, len);
2886 /* Actual loop iteration. */
2887 dir_obj = parent_obj;
2898 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
2901 uint64_t dir_obj, child_dir_zapobj;
2902 dnode_phys_t child_dir_zap, dir;
2906 if (objset_get_dnode(spa, &spa->spa_mos,
2907 DMU_POOL_DIRECTORY_OBJECT, &dir))
2909 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
2915 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
2917 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2921 /* Actual loop condition #1. */
2927 memcpy(element, p, q - p);
2928 element[q - p] = '\0';
2935 child_dir_zapobj = dd->dd_child_dir_zapobj;
2936 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
2937 &child_dir_zap) != 0)
2940 /* Actual loop condition #2. */
2941 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
2946 *objnum = dd->dd_head_dataset_obj;
2952 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
2954 uint64_t dir_obj, child_dir_zapobj;
2955 dnode_phys_t child_dir_zap, dir, dataset;
2956 dsl_dataset_phys_t *ds;
2959 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2960 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2963 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
2964 dir_obj = ds->ds_dir_obj;
2966 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2967 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2970 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2972 child_dir_zapobj = dd->dd_child_dir_zapobj;
2973 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
2974 &child_dir_zap) != 0) {
2975 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2979 return (zap_list(spa, &child_dir_zap) != 0);
2983 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
2984 int (*callback)(const char *, uint64_t))
2986 uint64_t dir_obj, child_dir_zapobj, zap_type;
2987 dnode_phys_t child_dir_zap, dir, dataset;
2988 dsl_dataset_phys_t *ds;
2992 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2994 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2997 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
2998 dir_obj = ds->ds_dir_obj;
3000 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
3002 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3005 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3007 child_dir_zapobj = dd->dd_child_dir_zapobj;
3008 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3011 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3015 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch,
3016 child_dir_zap.dn_datablkszsec * 512);
3020 zap_type = *(uint64_t *)zap_scratch;
3021 if (zap_type == ZBT_MICRO)
3022 return (mzap_list(&child_dir_zap, callback));
3024 return (fzap_list(spa, &child_dir_zap, callback));
3029 * Find the object set given the object number of its dataset object
3030 * and return its details in *objset
3033 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3035 dnode_phys_t dataset;
3036 dsl_dataset_phys_t *ds;
3038 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
3039 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3043 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3044 if (zio_read(spa, &ds->ds_bp, objset)) {
3045 printf("ZFS: can't read object set for dataset %ju\n",
3054 * Find the object set pointed to by the BOOTFS property or the root
3055 * dataset if there is none and return its details in *objset
3058 zfs_get_root(const spa_t *spa, uint64_t *objid)
3060 dnode_phys_t dir, propdir;
3061 uint64_t props, bootfs, root;
3066 * Start with the MOS directory object.
3068 if (objset_get_dnode(spa, &spa->spa_mos,
3069 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3070 printf("ZFS: can't read MOS object directory\n");
3075 * Lookup the pool_props and see if we can find a bootfs.
3077 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3078 sizeof(props), 1, &props) == 0 &&
3079 objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0 &&
3080 zap_lookup(spa, &propdir, "bootfs",
3081 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3086 * Lookup the root dataset directory
3088 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3089 sizeof(root), 1, &root) ||
3090 objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
3091 printf("ZFS: can't find root dsl_dir\n");
3096 * Use the information from the dataset directory's bonus buffer
3097 * to find the dataset object and from that the object set itself.
3099 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3100 *objid = dd->dd_head_dataset_obj;
3105 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3111 * Find the root object set if not explicitly provided
3113 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3114 printf("ZFS: can't find root filesystem\n");
3118 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3119 printf("ZFS: can't open root filesystem\n");
3123 mount->rootobj = rootobj;
3129 * callback function for feature name checks.
3132 check_feature(const char *name, uint64_t value)
3138 if (name[0] == '\0')
3141 for (i = 0; features_for_read[i] != NULL; i++) {
3142 if (strcmp(name, features_for_read[i]) == 0)
3145 printf("ZFS: unsupported feature: %s\n", name);
3150 * Checks whether the MOS features that are active are supported.
3153 check_mos_features(const spa_t *spa)
3156 uint64_t objnum, zap_type;
3160 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3163 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3164 sizeof (objnum), 1, &objnum)) != 0) {
3166 * It is older pool without features. As we have already
3167 * tested the label, just return without raising the error.
3172 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
3175 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3178 size = dir.dn_datablkszsec * 512;
3179 if (dnode_read(spa, &dir, 0, zap_scratch, size))
3182 zap_type = *(uint64_t *)zap_scratch;
3183 if (zap_type == ZBT_MICRO)
3184 rc = mzap_list(&dir, check_feature);
3186 rc = fzap_list(spa, &dir, check_feature);
3192 load_nvlist(spa_t *spa, uint64_t obj, unsigned char **value)
3200 if ((rc = objset_get_dnode(spa, &spa->spa_mos, obj, &dir)) != 0)
3202 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3203 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3207 if (dir.dn_bonuslen != sizeof (uint64_t))
3210 size = *(uint64_t *)DN_BONUS(&dir);
3215 rc = dnode_read(spa, &dir, 0, nv, size);
3226 zfs_spa_init(spa_t *spa)
3229 uint64_t config_object;
3230 unsigned char *nvlist;
3233 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
3234 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3237 if (spa->spa_mos.os_type != DMU_OST_META) {
3238 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3242 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
3244 printf("ZFS: failed to read pool %s directory object\n",
3248 /* this is allowed to fail, older pools do not have salt */
3249 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3250 sizeof (spa->spa_cksum_salt.zcs_bytes),
3251 spa->spa_cksum_salt.zcs_bytes);
3253 rc = check_mos_features(spa);
3255 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3259 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3260 sizeof (config_object), 1, &config_object);
3262 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3265 rc = load_nvlist(spa, config_object, &nvlist);
3269 /* Update vdevs from MOS config. */
3270 rc = vdev_init_from_nvlist(spa, nvlist + 4);
3276 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3279 if (dn->dn_bonustype != DMU_OT_SA) {
3280 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3282 sb->st_mode = zp->zp_mode;
3283 sb->st_uid = zp->zp_uid;
3284 sb->st_gid = zp->zp_gid;
3285 sb->st_size = zp->zp_size;
3287 sa_hdr_phys_t *sahdrp;
3292 if (dn->dn_bonuslen != 0)
3293 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3295 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3296 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3299 size = BP_GET_LSIZE(bp);
3300 buf = zfs_alloc(size);
3301 error = zio_read(spa, bp, buf);
3303 zfs_free(buf, size);
3311 hdrsize = SA_HDR_SIZE(sahdrp);
3312 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3314 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3316 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3318 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3321 zfs_free(buf, size);
3328 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3332 if (dn->dn_bonustype == DMU_OT_SA) {
3333 sa_hdr_phys_t *sahdrp = NULL;
3339 if (dn->dn_bonuslen != 0)
3340 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3344 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3346 bp = DN_SPILL_BLKPTR(dn);
3348 size = BP_GET_LSIZE(bp);
3349 buf = zfs_alloc(size);
3350 rc = zio_read(spa, bp, buf);
3352 zfs_free(buf, size);
3357 hdrsize = SA_HDR_SIZE(sahdrp);
3358 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3359 memcpy(path, p, psize);
3361 zfs_free(buf, size);
3365 * Second test is purely to silence bogus compiler
3366 * warning about accessing past the end of dn_bonus.
3368 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3369 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3370 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3372 rc = dnode_read(spa, dn, 0, path, psize);
3379 STAILQ_ENTRY(obj_list) entry;
3383 * Lookup a file and return its dnode.
3386 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3395 int symlinks_followed = 0;
3397 struct obj_list *entry, *tentry;
3398 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3401 if (mount->objset.os_type != DMU_OST_ZFS) {
3402 printf("ZFS: unexpected object set type %ju\n",
3403 (uintmax_t)mount->objset.os_type);
3407 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3411 * Get the root directory dnode.
3413 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3419 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3424 entry->objnum = objnum;
3425 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3427 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3433 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3442 while (*q != '\0' && *q != '/')
3446 if (p + 1 == q && p[0] == '.') {
3451 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3453 if (STAILQ_FIRST(&on_cache) ==
3454 STAILQ_LAST(&on_cache, obj_list, entry)) {
3458 entry = STAILQ_FIRST(&on_cache);
3459 STAILQ_REMOVE_HEAD(&on_cache, entry);
3461 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3464 if (q - p + 1 > sizeof(element)) {
3468 memcpy(element, p, q - p);
3472 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3474 if (!S_ISDIR(sb.st_mode)) {
3479 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3482 objnum = ZFS_DIRENT_OBJ(objnum);
3484 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3488 entry->objnum = objnum;
3489 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3490 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3495 * Check for symlink.
3497 rc = zfs_dnode_stat(spa, &dn, &sb);
3500 if (S_ISLNK(sb.st_mode)) {
3501 if (symlinks_followed > 10) {
3505 symlinks_followed++;
3508 * Read the link value and copy the tail of our
3509 * current path onto the end.
3511 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3515 strcpy(&path[sb.st_size], p);
3517 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3522 * Restart with the new path, starting either at
3523 * the root or at the parent depending whether or
3524 * not the link is relative.
3528 while (STAILQ_FIRST(&on_cache) !=
3529 STAILQ_LAST(&on_cache, obj_list, entry)) {
3530 entry = STAILQ_FIRST(&on_cache);
3531 STAILQ_REMOVE_HEAD(&on_cache, entry);
3535 entry = STAILQ_FIRST(&on_cache);
3536 STAILQ_REMOVE_HEAD(&on_cache, entry);
3539 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3545 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)