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>
48 static struct zfsmount zfsmount __unused;
51 * The indirect_child_t represents the vdev that we will read from, when we
52 * need to read all copies of the data (e.g. for scrub or reconstruction).
53 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
54 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
55 * ic_vdev is a child of the mirror.
57 typedef struct indirect_child {
63 * The indirect_split_t represents one mapped segment of an i/o to the
64 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
65 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
66 * For split blocks, there will be several of these.
68 typedef struct indirect_split {
69 list_node_t is_node; /* link on iv_splits */
72 * is_split_offset is the offset into the i/o.
73 * This is the sum of the previous splits' is_size's.
75 uint64_t is_split_offset;
77 vdev_t *is_vdev; /* top-level vdev */
78 uint64_t is_target_offset; /* offset on is_vdev */
80 int is_children; /* number of entries in is_child[] */
83 * is_good_child is the child that we are currently using to
84 * attempt reconstruction.
88 indirect_child_t is_child[1]; /* variable-length */
92 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
93 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
95 typedef struct indirect_vsd {
96 boolean_t iv_split_block;
97 boolean_t iv_reconstruct;
99 list_t iv_splits; /* list of indirect_split_t's */
103 * List of all vdevs, chained through v_alllink.
105 static vdev_list_t zfs_vdevs;
108 * List of ZFS features supported for read
110 static const char *features_for_read[] = {
111 "org.illumos:lz4_compress",
112 "com.delphix:hole_birth",
113 "com.delphix:extensible_dataset",
114 "com.delphix:embedded_data",
115 "org.open-zfs:large_blocks",
116 "org.illumos:sha512",
118 "org.zfsonlinux:large_dnode",
119 "com.joyent:multi_vdev_crash_dump",
120 "com.delphix:spacemap_histogram",
121 "com.delphix:zpool_checkpoint",
122 "com.delphix:spacemap_v2",
123 "com.datto:encryption",
124 "org.zfsonlinux:allocation_classes",
125 "com.datto:resilver_defer",
126 "com.delphix:device_removal",
127 "com.delphix:obsolete_counts",
132 * List of all pools, chained through spa_link.
134 static spa_list_t zfs_pools;
136 static const dnode_phys_t *dnode_cache_obj;
137 static uint64_t dnode_cache_bn;
138 static char *dnode_cache_buf;
139 static char *zap_scratch;
140 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
142 #define TEMP_SIZE (1024 * 1024)
144 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
145 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
146 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
147 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
148 const char *name, uint64_t integer_size, uint64_t num_integers,
150 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
152 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
154 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
156 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
157 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
159 vdev_indirect_mapping_entry_phys_t *
160 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
161 uint64_t, uint64_t *);
166 STAILQ_INIT(&zfs_vdevs);
167 STAILQ_INIT(&zfs_pools);
169 zfs_temp_buf = malloc(TEMP_SIZE);
170 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
171 zfs_temp_ptr = zfs_temp_buf;
172 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
173 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
179 zfs_alloc(size_t size)
183 if (zfs_temp_ptr + size > zfs_temp_end) {
184 panic("ZFS: out of temporary buffer space");
187 zfs_temp_ptr += size;
193 zfs_free(void *ptr, size_t size)
196 zfs_temp_ptr -= size;
197 if (zfs_temp_ptr != ptr) {
198 panic("ZFS: zfs_alloc()/zfs_free() mismatch");
203 xdr_int(const unsigned char **xdr, int *ip)
211 xdr_u_int(const unsigned char **xdr, u_int *ip)
219 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
225 *lp = (((uint64_t) hi) << 32) | lo;
230 nvlist_find(const unsigned char *nvlist, const char *name, int type,
231 int *elementsp, void *valuep)
233 const unsigned char *p, *pair;
235 int encoded_size, decoded_size;
242 xdr_int(&p, &encoded_size);
243 xdr_int(&p, &decoded_size);
244 while (encoded_size && decoded_size) {
245 int namelen, pairtype, elements;
246 const char *pairname;
248 xdr_int(&p, &namelen);
249 pairname = (const char*) p;
250 p += roundup(namelen, 4);
251 xdr_int(&p, &pairtype);
253 if (!memcmp(name, pairname, namelen) && type == pairtype) {
254 xdr_int(&p, &elements);
256 *elementsp = elements;
257 if (type == DATA_TYPE_UINT64) {
258 xdr_uint64_t(&p, (uint64_t *) valuep);
260 } else if (type == DATA_TYPE_STRING) {
263 (*(const char**) valuep) = (const char*) p;
265 } else if (type == DATA_TYPE_NVLIST
266 || type == DATA_TYPE_NVLIST_ARRAY) {
267 (*(const unsigned char**) valuep) =
268 (const unsigned char*) p;
275 * Not the pair we are looking for, skip to the next one.
277 p = pair + encoded_size;
281 xdr_int(&p, &encoded_size);
282 xdr_int(&p, &decoded_size);
289 nvlist_check_features_for_read(const unsigned char *nvlist)
291 const unsigned char *p, *pair;
293 int encoded_size, decoded_size;
303 xdr_int(&p, &encoded_size);
304 xdr_int(&p, &decoded_size);
305 while (encoded_size && decoded_size) {
306 int namelen, pairtype;
307 const char *pairname;
312 xdr_int(&p, &namelen);
313 pairname = (const char*) p;
314 p += roundup(namelen, 4);
315 xdr_int(&p, &pairtype);
317 for (i = 0; features_for_read[i] != NULL; i++) {
318 if (!memcmp(pairname, features_for_read[i], namelen)) {
325 printf("ZFS: unsupported feature: %s\n", pairname);
329 p = pair + encoded_size;
332 xdr_int(&p, &encoded_size);
333 xdr_int(&p, &decoded_size);
340 * Return the next nvlist in an nvlist array.
342 static const unsigned char *
343 nvlist_next(const unsigned char *nvlist)
345 const unsigned char *p, *pair;
347 int encoded_size, decoded_size;
354 xdr_int(&p, &encoded_size);
355 xdr_int(&p, &decoded_size);
356 while (encoded_size && decoded_size) {
357 p = pair + encoded_size;
360 xdr_int(&p, &encoded_size);
361 xdr_int(&p, &decoded_size);
369 static const unsigned char *
370 nvlist_print(const unsigned char *nvlist, unsigned int indent)
372 static const char* typenames[] = {
383 "DATA_TYPE_BYTE_ARRAY",
384 "DATA_TYPE_INT16_ARRAY",
385 "DATA_TYPE_UINT16_ARRAY",
386 "DATA_TYPE_INT32_ARRAY",
387 "DATA_TYPE_UINT32_ARRAY",
388 "DATA_TYPE_INT64_ARRAY",
389 "DATA_TYPE_UINT64_ARRAY",
390 "DATA_TYPE_STRING_ARRAY",
393 "DATA_TYPE_NVLIST_ARRAY",
394 "DATA_TYPE_BOOLEAN_VALUE",
397 "DATA_TYPE_BOOLEAN_ARRAY",
398 "DATA_TYPE_INT8_ARRAY",
399 "DATA_TYPE_UINT8_ARRAY"
403 const unsigned char *p, *pair;
405 int encoded_size, decoded_size;
412 xdr_int(&p, &encoded_size);
413 xdr_int(&p, &decoded_size);
414 while (encoded_size && decoded_size) {
415 int namelen, pairtype, elements;
416 const char *pairname;
418 xdr_int(&p, &namelen);
419 pairname = (const char*) p;
420 p += roundup(namelen, 4);
421 xdr_int(&p, &pairtype);
423 for (i = 0; i < indent; i++)
425 printf("%s %s", typenames[pairtype], pairname);
427 xdr_int(&p, &elements);
429 case DATA_TYPE_UINT64: {
431 xdr_uint64_t(&p, &val);
432 printf(" = 0x%jx\n", (uintmax_t)val);
436 case DATA_TYPE_STRING: {
439 printf(" = \"%s\"\n", p);
443 case DATA_TYPE_NVLIST:
445 nvlist_print(p, indent + 1);
448 case DATA_TYPE_NVLIST_ARRAY:
449 for (j = 0; j < elements; j++) {
451 p = nvlist_print(p, indent + 1);
452 if (j != elements - 1) {
453 for (i = 0; i < indent; i++)
455 printf("%s %s", typenames[pairtype], pairname);
464 p = pair + encoded_size;
467 xdr_int(&p, &encoded_size);
468 xdr_int(&p, &decoded_size);
477 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
478 off_t offset, size_t size)
483 if (!vdev->v_phys_read)
487 psize = BP_GET_PSIZE(bp);
492 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
493 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
497 return (zio_checksum_verify(vdev->spa, bp, buf));
502 typedef struct remap_segment {
506 uint64_t rs_split_offset;
510 static remap_segment_t *
511 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
513 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
517 rs->rs_offset = offset;
518 rs->rs_asize = asize;
519 rs->rs_split_offset = split_offset;
525 vdev_indirect_mapping_t *
526 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
527 uint64_t mapping_object)
529 vdev_indirect_mapping_t *vim;
530 vdev_indirect_mapping_phys_t *vim_phys;
533 vim = calloc(1, sizeof (*vim));
537 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
538 if (vim->vim_dn == NULL) {
543 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
551 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
552 if (vim->vim_phys == NULL) {
558 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
559 *vim->vim_phys = *vim_phys;
561 vim->vim_objset = os;
562 vim->vim_object = mapping_object;
563 vim->vim_entries = NULL;
565 vim->vim_havecounts =
566 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
571 * Compare an offset with an indirect mapping entry; there are three
572 * possible scenarios:
574 * 1. The offset is "less than" the mapping entry; meaning the
575 * offset is less than the source offset of the mapping entry. In
576 * this case, there is no overlap between the offset and the
577 * mapping entry and -1 will be returned.
579 * 2. The offset is "greater than" the mapping entry; meaning the
580 * offset is greater than the mapping entry's source offset plus
581 * the entry's size. In this case, there is no overlap between
582 * the offset and the mapping entry and 1 will be returned.
584 * NOTE: If the offset is actually equal to the entry's offset
585 * plus size, this is considered to be "greater" than the entry,
586 * and this case applies (i.e. 1 will be returned). Thus, the
587 * entry's "range" can be considered to be inclusive at its
588 * start, but exclusive at its end: e.g. [src, src + size).
590 * 3. The last case to consider is if the offset actually falls
591 * within the mapping entry's range. If this is the case, the
592 * offset is considered to be "equal to" the mapping entry and
593 * 0 will be returned.
595 * NOTE: If the offset is equal to the entry's source offset,
596 * this case applies and 0 will be returned. If the offset is
597 * equal to the entry's source plus its size, this case does
598 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
602 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
604 const uint64_t *key = v_key;
605 const vdev_indirect_mapping_entry_phys_t *array_elem =
607 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
609 if (*key < src_offset) {
611 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
619 * Return array entry.
621 static vdev_indirect_mapping_entry_phys_t *
622 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
628 if (vim->vim_phys->vimp_num_entries == 0)
631 if (vim->vim_entries == NULL) {
634 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
635 size = vim->vim_phys->vimp_num_entries *
636 sizeof (*vim->vim_entries);
638 size = bsize / sizeof (*vim->vim_entries);
639 size *= sizeof (*vim->vim_entries);
641 vim->vim_entries = malloc(size);
642 if (vim->vim_entries == NULL)
644 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
645 offset = index * sizeof (*vim->vim_entries);
648 /* We have data in vim_entries */
650 if (index >= vim->vim_entry_offset &&
651 index <= vim->vim_entry_offset + vim->vim_num_entries) {
652 index -= vim->vim_entry_offset;
653 return (&vim->vim_entries[index]);
655 offset = index * sizeof (*vim->vim_entries);
658 vim->vim_entry_offset = index;
659 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
660 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
663 /* Read error, invalidate vim_entries. */
664 free(vim->vim_entries);
665 vim->vim_entries = NULL;
668 index -= vim->vim_entry_offset;
669 return (&vim->vim_entries[index]);
673 * Returns the mapping entry for the given offset.
675 * It's possible that the given offset will not be in the mapping table
676 * (i.e. no mapping entries contain this offset), in which case, the
677 * return value value depends on the "next_if_missing" parameter.
679 * If the offset is not found in the table and "next_if_missing" is
680 * B_FALSE, then NULL will always be returned. The behavior is intended
681 * to allow consumers to get the entry corresponding to the offset
682 * parameter, iff the offset overlaps with an entry in the table.
684 * If the offset is not found in the table and "next_if_missing" is
685 * B_TRUE, then the entry nearest to the given offset will be returned,
686 * such that the entry's source offset is greater than the offset
687 * passed in (i.e. the "next" mapping entry in the table is returned, if
688 * the offset is missing from the table). If there are no entries whose
689 * source offset is greater than the passed in offset, NULL is returned.
691 static vdev_indirect_mapping_entry_phys_t *
692 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
695 ASSERT(vim->vim_phys->vimp_num_entries > 0);
697 vdev_indirect_mapping_entry_phys_t *entry;
699 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
703 * We don't define these inside of the while loop because we use
704 * their value in the case that offset isn't in the mapping.
709 while (last >= base) {
710 mid = base + ((last - base) >> 1);
712 entry = vdev_indirect_mapping_entry(vim, mid);
715 result = dva_mapping_overlap_compare(&offset, entry);
719 } else if (result < 0) {
729 * Given an indirect vdev and an extent on that vdev, it duplicates the
730 * physical entries of the indirect mapping that correspond to the extent
731 * to a new array and returns a pointer to it. In addition, copied_entries
732 * is populated with the number of mapping entries that were duplicated.
734 * Finally, since we are doing an allocation, it is up to the caller to
735 * free the array allocated in this function.
737 vdev_indirect_mapping_entry_phys_t *
738 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
739 uint64_t asize, uint64_t *copied_entries)
741 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
742 vdev_indirect_mapping_t *vim = vd->v_mapping;
743 uint64_t entries = 0;
745 vdev_indirect_mapping_entry_phys_t *first_mapping =
746 vdev_indirect_mapping_entry_for_offset(vim, offset);
747 ASSERT3P(first_mapping, !=, NULL);
749 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
751 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
752 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
753 uint64_t inner_size = MIN(asize, size - inner_offset);
755 offset += inner_size;
761 size_t copy_length = entries * sizeof (*first_mapping);
762 duplicate_mappings = malloc(copy_length);
763 if (duplicate_mappings != NULL)
764 bcopy(first_mapping, duplicate_mappings, copy_length);
768 *copied_entries = entries;
770 return (duplicate_mappings);
774 vdev_lookup_top(spa_t *spa, uint64_t vdev)
778 STAILQ_FOREACH(rvd, &spa->spa_vdevs, v_childlink)
779 if (rvd->v_id == vdev)
786 * This is a callback for vdev_indirect_remap() which allocates an
787 * indirect_split_t for each split segment and adds it to iv_splits.
790 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
791 uint64_t size, void *arg)
795 indirect_vsd_t *iv = zio->io_vsd;
797 if (vd->v_read == vdev_indirect_read)
800 if (vd->v_read == vdev_mirror_read)
803 indirect_split_t *is =
804 malloc(offsetof(indirect_split_t, is_child[n]));
806 zio->io_error = ENOMEM;
809 bzero(is, offsetof(indirect_split_t, is_child[n]));
813 is->is_split_offset = split_offset;
814 is->is_target_offset = offset;
818 * Note that we only consider multiple copies of the data for
819 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
820 * though they use the same ops as mirror, because there's only one
821 * "good" copy under the replacing/spare.
823 if (vd->v_read == vdev_mirror_read) {
827 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
828 is->is_child[i++].ic_vdev = kid;
831 is->is_child[0].ic_vdev = vd;
834 list_insert_tail(&iv->iv_splits, is);
838 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
841 spa_t *spa = vd->spa;
845 list_create(&stack, sizeof (remap_segment_t),
846 offsetof(remap_segment_t, rs_node));
848 rs = rs_alloc(vd, offset, asize, 0);
850 printf("vdev_indirect_remap: out of memory.\n");
851 zio->io_error = ENOMEM;
853 for ( ; rs != NULL; rs = list_remove_head(&stack)) {
854 vdev_t *v = rs->rs_vd;
855 uint64_t num_entries = 0;
856 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
857 vdev_indirect_mapping_entry_phys_t *mapping =
858 vdev_indirect_mapping_duplicate_adjacent_entries(v,
859 rs->rs_offset, rs->rs_asize, &num_entries);
861 if (num_entries == 0)
862 zio->io_error = ENOMEM;
864 for (uint64_t i = 0; i < num_entries; i++) {
865 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
866 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
867 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
868 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
869 uint64_t inner_offset = rs->rs_offset -
870 DVA_MAPPING_GET_SRC_OFFSET(m);
871 uint64_t inner_size =
872 MIN(rs->rs_asize, size - inner_offset);
873 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
875 if (dst_v->v_read == vdev_indirect_read) {
878 o = rs_alloc(dst_v, dst_offset + inner_offset,
879 inner_size, rs->rs_split_offset);
881 printf("vdev_indirect_remap: "
883 zio->io_error = ENOMEM;
887 list_insert_head(&stack, o);
889 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
890 dst_offset + inner_offset,
894 * vdev_indirect_gather_splits can have memory
895 * allocation error, we can not recover from it.
897 if (zio->io_error != 0)
900 rs->rs_offset += inner_size;
901 rs->rs_asize -= inner_size;
902 rs->rs_split_offset += inner_size;
907 if (zio->io_error != 0)
911 list_destroy(&stack);
915 vdev_indirect_map_free(zio_t *zio)
917 indirect_vsd_t *iv = zio->io_vsd;
918 indirect_split_t *is;
920 while ((is = list_head(&iv->iv_splits)) != NULL) {
921 for (int c = 0; c < is->is_children; c++) {
922 indirect_child_t *ic = &is->is_child[c];
925 list_remove(&iv->iv_splits, is);
932 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
933 off_t offset, size_t bytes)
936 spa_t *spa = vdev->spa;
937 indirect_vsd_t *iv = malloc(sizeof (*iv));
938 indirect_split_t *first;
943 bzero(iv, sizeof (*iv));
945 list_create(&iv->iv_splits,
946 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
949 zio.io_bp = (blkptr_t *)bp;
952 zio.io_offset = offset;
956 if (vdev->v_mapping == NULL) {
957 vdev_indirect_config_t *vic;
959 vic = &vdev->vdev_indirect_config;
960 vdev->v_mapping = vdev_indirect_mapping_open(spa,
961 &spa->spa_mos, vic->vic_mapping_object);
964 vdev_indirect_remap(vdev, offset, bytes, &zio);
965 if (zio.io_error != 0)
966 return (zio.io_error);
968 first = list_head(&iv->iv_splits);
969 if (first->is_size == zio.io_size) {
971 * This is not a split block; we are pointing to the entire
972 * data, which will checksum the same as the original data.
973 * Pass the BP down so that the child i/o can verify the
974 * checksum, and try a different location if available
975 * (e.g. on a mirror).
977 * While this special case could be handled the same as the
978 * general (split block) case, doing it this way ensures
979 * that the vast majority of blocks on indirect vdevs
980 * (which are not split) are handled identically to blocks
981 * on non-indirect vdevs. This allows us to be less strict
982 * about performance in the general (but rare) case.
984 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
985 zio.io_data, first->is_target_offset, bytes);
987 iv->iv_split_block = B_TRUE;
989 * Read one copy of each split segment, from the
990 * top-level vdev. Since we don't know the
991 * checksum of each split individually, the child
992 * zio can't ensure that we get the right data.
993 * E.g. if it's a mirror, it will just read from a
994 * random (healthy) leaf vdev. We have to verify
995 * the checksum in vdev_indirect_io_done().
997 for (indirect_split_t *is = list_head(&iv->iv_splits);
998 is != NULL; is = list_next(&iv->iv_splits, is)) {
999 char *ptr = zio.io_data;
1001 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
1002 ptr + is->is_split_offset, is->is_target_offset,
1005 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
1011 vdev_indirect_map_free(&zio);
1019 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1020 off_t offset, size_t bytes)
1023 return (vdev_read_phys(vdev, bp, buf,
1024 offset + VDEV_LABEL_START_SIZE, bytes));
1029 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1030 off_t offset, size_t bytes)
1036 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1037 if (kid->v_state != VDEV_STATE_HEALTHY)
1039 rc = kid->v_read(kid, bp, buf, offset, bytes);
1048 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1049 off_t offset, size_t bytes)
1054 * Here we should have two kids:
1055 * First one which is the one we are replacing and we can trust
1056 * only this one to have valid data, but it might not be present.
1057 * Second one is that one we are replacing with. It is most likely
1058 * healthy, but we can't trust it has needed data, so we won't use it.
1060 kid = STAILQ_FIRST(&vdev->v_children);
1063 if (kid->v_state != VDEV_STATE_HEALTHY)
1065 return (kid->v_read(kid, bp, buf, offset, bytes));
1069 vdev_find(uint64_t guid)
1073 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
1074 if (vdev->v_guid == guid)
1081 vdev_create(uint64_t guid, vdev_read_t *_read)
1084 vdev_indirect_config_t *vic;
1086 vdev = malloc(sizeof(vdev_t));
1087 memset(vdev, 0, sizeof(vdev_t));
1088 STAILQ_INIT(&vdev->v_children);
1089 vdev->v_guid = guid;
1090 vdev->v_state = VDEV_STATE_OFFLINE;
1091 vdev->v_read = _read;
1093 vic = &vdev->vdev_indirect_config;
1094 vic->vic_prev_indirect_vdev = UINT64_MAX;
1095 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
1101 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
1102 vdev_t **vdevp, int is_newer)
1105 uint64_t guid, id, ashift, asize, nparity;
1109 const unsigned char *kids;
1110 int nkids, i, is_new;
1111 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
1114 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1116 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
1117 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1119 printf("ZFS: can't find vdev details\n");
1123 if (strcmp(type, VDEV_TYPE_MIRROR)
1124 && strcmp(type, VDEV_TYPE_DISK)
1126 && strcmp(type, VDEV_TYPE_FILE)
1128 && strcmp(type, VDEV_TYPE_RAIDZ)
1129 && strcmp(type, VDEV_TYPE_INDIRECT)
1130 && strcmp(type, VDEV_TYPE_REPLACING)) {
1131 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
1135 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
1138 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
1140 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
1142 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
1144 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
1146 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
1148 nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
1151 vdev = vdev_find(guid);
1155 if (!strcmp(type, VDEV_TYPE_MIRROR))
1156 vdev = vdev_create(guid, vdev_mirror_read);
1157 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
1158 vdev = vdev_create(guid, vdev_raidz_read);
1159 else if (!strcmp(type, VDEV_TYPE_REPLACING))
1160 vdev = vdev_create(guid, vdev_replacing_read);
1161 else if (!strcmp(type, VDEV_TYPE_INDIRECT)) {
1162 vdev_indirect_config_t *vic;
1164 vdev = vdev_create(guid, vdev_indirect_read);
1165 vdev->v_state = VDEV_STATE_HEALTHY;
1166 vic = &vdev->vdev_indirect_config;
1169 ZPOOL_CONFIG_INDIRECT_OBJECT, DATA_TYPE_UINT64,
1170 NULL, &vic->vic_mapping_object);
1172 ZPOOL_CONFIG_INDIRECT_BIRTHS, DATA_TYPE_UINT64,
1173 NULL, &vic->vic_births_object);
1175 ZPOOL_CONFIG_PREV_INDIRECT_VDEV, DATA_TYPE_UINT64,
1176 NULL, &vic->vic_prev_indirect_vdev);
1178 vdev = vdev_create(guid, vdev_disk_read);
1181 vdev->v_top = pvdev != NULL ? pvdev : vdev;
1182 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
1183 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
1184 vdev->v_ashift = ashift;
1188 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1189 DATA_TYPE_UINT64, NULL, &asize) == 0) {
1190 vdev->v_psize = asize +
1191 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1193 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1194 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
1195 vdev->v_nparity = nparity;
1197 vdev->v_nparity = 0;
1199 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1200 DATA_TYPE_STRING, NULL, &path) == 0) {
1201 if (strncmp(path, "/dev/", 5) == 0)
1203 vdev->v_name = strdup(path);
1207 if (!strcmp(type, "raidz")) {
1208 if (vdev->v_nparity < 1 ||
1209 vdev->v_nparity > 3) {
1210 printf("ZFS: can only boot from disk, "
1211 "mirror, raidz1, raidz2 and raidz3 "
1215 asprintf(&name, "%s%d-%jd", type,
1216 vdev->v_nparity, id);
1218 asprintf(&name, "%s-%jd", type, id);
1222 vdev->v_name = name;
1224 vdev->v_islog = is_log == 1;
1229 if (is_new || is_newer) {
1231 * This is either new vdev or we've already seen this vdev,
1232 * but from an older vdev label, so let's refresh its state
1233 * from the newer label.
1236 vdev->v_state = VDEV_STATE_OFFLINE;
1237 else if (is_removed)
1238 vdev->v_state = VDEV_STATE_REMOVED;
1239 else if (is_faulted)
1240 vdev->v_state = VDEV_STATE_FAULTED;
1241 else if (is_degraded)
1242 vdev->v_state = VDEV_STATE_DEGRADED;
1243 else if (isnt_present)
1244 vdev->v_state = VDEV_STATE_CANT_OPEN;
1247 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1250 * Its ok if we don't have any kids.
1253 vdev->v_nchildren = nkids;
1254 for (i = 0; i < nkids; i++) {
1255 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
1259 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
1261 kids = nvlist_next(kids);
1264 vdev->v_nchildren = 0;
1273 vdev_set_state(vdev_t *vdev)
1280 * A mirror or raidz is healthy if all its kids are healthy. A
1281 * mirror is degraded if any of its kids is healthy; a raidz
1282 * is degraded if at most nparity kids are offline.
1284 if (STAILQ_FIRST(&vdev->v_children)) {
1287 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1288 if (kid->v_state == VDEV_STATE_HEALTHY)
1293 if (bad_kids == 0) {
1294 vdev->v_state = VDEV_STATE_HEALTHY;
1296 if (vdev->v_read == vdev_mirror_read) {
1298 vdev->v_state = VDEV_STATE_DEGRADED;
1300 vdev->v_state = VDEV_STATE_OFFLINE;
1302 } else if (vdev->v_read == vdev_raidz_read) {
1303 if (bad_kids > vdev->v_nparity) {
1304 vdev->v_state = VDEV_STATE_OFFLINE;
1306 vdev->v_state = VDEV_STATE_DEGRADED;
1314 spa_find_by_guid(uint64_t guid)
1318 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1319 if (spa->spa_guid == guid)
1326 spa_find_by_name(const char *name)
1330 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1331 if (!strcmp(spa->spa_name, name))
1339 spa_get_primary(void)
1342 return (STAILQ_FIRST(&zfs_pools));
1346 spa_get_primary_vdev(const spa_t *spa)
1352 spa = spa_get_primary();
1355 vdev = STAILQ_FIRST(&spa->spa_vdevs);
1358 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
1359 kid = STAILQ_FIRST(&vdev->v_children))
1366 spa_create(uint64_t guid, const char *name)
1370 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1372 if ((spa->spa_name = strdup(name)) == NULL) {
1376 STAILQ_INIT(&spa->spa_vdevs);
1377 spa->spa_guid = guid;
1378 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1384 state_name(vdev_state_t state)
1386 static const char* names[] = {
1396 return names[state];
1401 #define pager_printf printf
1406 pager_printf(const char *fmt, ...)
1411 va_start(args, fmt);
1412 vsprintf(line, fmt, args);
1415 return (pager_output(line));
1420 #define STATUS_FORMAT " %s %s\n"
1423 print_state(int indent, const char *name, vdev_state_t state)
1429 for (i = 0; i < indent; i++)
1433 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1437 vdev_status(vdev_t *vdev, int indent)
1442 if (vdev->v_islog) {
1443 (void)pager_output(" logs\n");
1447 ret = print_state(indent, vdev->v_name, vdev->v_state);
1451 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1452 ret = vdev_status(kid, indent + 1);
1460 spa_status(spa_t *spa)
1462 static char bootfs[ZFS_MAXNAMELEN];
1465 int good_kids, bad_kids, degraded_kids, ret;
1468 ret = pager_printf(" pool: %s\n", spa->spa_name);
1472 if (zfs_get_root(spa, &rootid) == 0 &&
1473 zfs_rlookup(spa, rootid, bootfs) == 0) {
1474 if (bootfs[0] == '\0')
1475 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1477 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1482 ret = pager_printf("config:\n\n");
1485 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1492 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1493 if (vdev->v_state == VDEV_STATE_HEALTHY)
1495 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1501 state = VDEV_STATE_CLOSED;
1502 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1503 state = VDEV_STATE_HEALTHY;
1504 else if ((good_kids + degraded_kids) > 0)
1505 state = VDEV_STATE_DEGRADED;
1507 ret = print_state(0, spa->spa_name, state);
1510 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1511 ret = vdev_status(vdev, 1);
1519 spa_all_status(void)
1522 int first = 1, ret = 0;
1524 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1526 ret = pager_printf("\n");
1531 ret = spa_status(spa);
1539 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1541 uint64_t label_offset;
1543 if (l < VDEV_LABELS / 2)
1546 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1548 return (offset + l * sizeof (vdev_label_t) + label_offset);
1552 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1554 unsigned int seq1 = 0;
1555 unsigned int seq2 = 0;
1556 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1561 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1565 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1566 seq1 = MMP_SEQ(ub1);
1568 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1569 seq2 = MMP_SEQ(ub2);
1571 return (AVL_CMP(seq1, seq2));
1575 uberblock_verify(uberblock_t *ub)
1577 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1578 byteswap_uint64_array(ub, sizeof (uberblock_t));
1581 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1582 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1589 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1595 off = vdev_label_offset(vd->v_psize, l, offset);
1598 BP_SET_LSIZE(&bp, size);
1599 BP_SET_PSIZE(&bp, size);
1600 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1601 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1602 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1603 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1605 return (vdev_read_phys(vd, &bp, buf, off, size));
1608 static unsigned char *
1609 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1612 uint64_t best_txg = 0;
1613 uint64_t label_txg = 0;
1619 label = malloc(sizeof (vdev_phys_t));
1623 nvl_size = VDEV_PHYS_SIZE - sizeof (zio_eck_t) - 4;
1624 nvl = malloc(nvl_size);
1628 for (int l = 0; l < VDEV_LABELS; l++) {
1629 const unsigned char *nvlist;
1631 if (vdev_label_read(vd, l, label,
1632 offsetof(vdev_label_t, vl_vdev_phys),
1633 sizeof (vdev_phys_t)))
1636 if (label->vp_nvlist[0] != NV_ENCODE_XDR)
1639 nvlist = (const unsigned char *) label->vp_nvlist + 4;
1640 error = nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
1641 DATA_TYPE_UINT64, NULL, &label_txg);
1642 if (error != 0 || label_txg == 0) {
1643 memcpy(nvl, nvlist, nvl_size);
1647 if (label_txg <= txg && label_txg > best_txg) {
1648 best_txg = label_txg;
1649 memcpy(nvl, nvlist, nvl_size);
1652 * Use asize from pool config. We need this
1653 * because we can get bad value from BIOS.
1655 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
1656 DATA_TYPE_UINT64, NULL, &asize) == 0) {
1657 vd->v_psize = asize +
1658 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1663 if (best_txg == 0) {
1673 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1677 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1681 for (int l = 0; l < VDEV_LABELS; l++) {
1682 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1683 if (vdev_label_read(vd, l, buf,
1684 VDEV_UBERBLOCK_OFFSET(vd, n),
1685 VDEV_UBERBLOCK_SIZE(vd)))
1687 if (uberblock_verify(buf) != 0)
1690 if (vdev_uberblock_compare(buf, ub) > 0)
1698 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
1702 vdev_t *vdev, *top_vdev, *pool_vdev;
1703 unsigned char *nvlist;
1706 uint64_t pool_txg, pool_guid;
1707 const char *pool_name;
1708 const unsigned char *vdevs;
1709 const unsigned char *features;
1713 * Load the vdev label and figure out which
1714 * uberblock is most current.
1716 memset(&vtmp, 0, sizeof(vtmp));
1717 vtmp.v_phys_read = _read;
1718 vtmp.v_read_priv = read_priv;
1719 vtmp.v_psize = P2ALIGN(ldi_get_size(read_priv),
1720 (uint64_t)sizeof (vdev_label_t));
1722 /* Test for minimum device size. */
1723 if (vtmp.v_psize < SPA_MINDEVSIZE)
1726 nvlist = vdev_label_read_config(&vtmp, UINT64_MAX);
1730 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1736 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1737 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1738 (unsigned) val, (unsigned) SPA_VERSION);
1743 /* Check ZFS features for read */
1744 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1745 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1746 nvlist_check_features_for_read(features) != 0) {
1751 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1757 if (val == POOL_STATE_DESTROYED) {
1758 /* We don't boot only from destroyed pools. */
1763 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1764 NULL, &pool_txg) != 0 ||
1765 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1766 NULL, &pool_guid) != 0 ||
1767 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1768 NULL, &pool_name) != 0) {
1770 * Cache and spare devices end up here - just ignore
1778 * Create the pool if this is the first time we've seen it.
1780 spa = spa_find_by_guid(pool_guid);
1782 spa = spa_create(pool_guid, pool_name);
1788 if (pool_txg > spa->spa_txg) {
1789 spa->spa_txg = pool_txg;
1796 * Get the vdev tree and create our in-core copy of it.
1797 * If we already have a vdev with this guid, this must
1798 * be some kind of alias (overlapping slices, dangerously dedicated
1801 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1802 NULL, &guid) != 0) {
1806 vdev = vdev_find(guid);
1807 /* Has this vdev already been inited? */
1808 if (vdev && vdev->v_phys_read) {
1813 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1819 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1825 * Add the toplevel vdev to the pool if its not already there.
1827 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1828 if (top_vdev == pool_vdev)
1831 if (!pool_vdev && top_vdev) {
1832 top_vdev->spa = spa;
1833 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1837 * We should already have created an incomplete vdev for this
1838 * vdev. Find it and initialise it with our read proc.
1840 vdev = vdev_find(guid);
1842 vdev->v_phys_read = _read;
1843 vdev->v_read_priv = read_priv;
1844 vdev->v_state = VDEV_STATE_HEALTHY;
1845 vdev->v_psize = vtmp.v_psize;
1847 printf("ZFS: inconsistent nvlist contents\n");
1852 spa->spa_with_log = vdev->v_islog;
1855 * Re-evaluate top-level vdev state.
1857 vdev_set_state(top_vdev);
1860 * Ok, we are happy with the pool so far. Lets find
1861 * the best uberblock and then we can actually access
1862 * the contents of the pool.
1864 vdev_uberblock_load(vdev, &spa->spa_uberblock);
1877 for (v = 0; v < 32; v++)
1884 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1887 zio_gbh_phys_t zio_gb;
1891 /* Artificial BP for gang block header. */
1893 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1894 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1895 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1896 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1897 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1898 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1900 /* Read gang header block using the artificial BP. */
1901 if (zio_read(spa, &gbh_bp, &zio_gb))
1905 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1906 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1908 if (BP_IS_HOLE(gbp))
1910 if (zio_read(spa, gbp, pbuf))
1912 pbuf += BP_GET_PSIZE(gbp);
1915 if (zio_checksum_verify(spa, bp, buf))
1921 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1923 int cpfunc = BP_GET_COMPRESS(bp);
1924 uint64_t align, size;
1929 * Process data embedded in block pointer
1931 if (BP_IS_EMBEDDED(bp)) {
1932 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1934 size = BPE_GET_PSIZE(bp);
1935 ASSERT(size <= BPE_PAYLOAD_SIZE);
1937 if (cpfunc != ZIO_COMPRESS_OFF)
1938 pbuf = zfs_alloc(size);
1942 decode_embedded_bp_compressed(bp, pbuf);
1945 if (cpfunc != ZIO_COMPRESS_OFF) {
1946 error = zio_decompress_data(cpfunc, pbuf,
1947 size, buf, BP_GET_LSIZE(bp));
1948 zfs_free(pbuf, size);
1951 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1958 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1959 const dva_t *dva = &bp->blk_dva[i];
1964 if (!dva->dva_word[0] && !dva->dva_word[1])
1967 vdevid = DVA_GET_VDEV(dva);
1968 offset = DVA_GET_OFFSET(dva);
1969 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1970 if (vdev->v_id == vdevid)
1973 if (!vdev || !vdev->v_read)
1976 size = BP_GET_PSIZE(bp);
1977 if (vdev->v_read == vdev_raidz_read) {
1978 align = 1ULL << vdev->v_top->v_ashift;
1979 if (P2PHASE(size, align) != 0)
1980 size = P2ROUNDUP(size, align);
1982 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1983 pbuf = zfs_alloc(size);
1987 if (DVA_GET_GANG(dva))
1988 error = zio_read_gang(spa, bp, pbuf);
1990 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1992 if (cpfunc != ZIO_COMPRESS_OFF)
1993 error = zio_decompress_data(cpfunc, pbuf,
1994 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1995 else if (size != BP_GET_PSIZE(bp))
1996 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1999 zfs_free(pbuf, size);
2004 printf("ZFS: i/o error - all block copies unavailable\n");
2009 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
2011 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2012 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2013 int nlevels = dnode->dn_nlevels;
2016 if (bsize > SPA_MAXBLOCKSIZE) {
2017 printf("ZFS: I/O error - blocks larger than %llu are not "
2018 "supported\n", SPA_MAXBLOCKSIZE);
2023 * Note: bsize may not be a power of two here so we need to do an
2024 * actual divide rather than a bitshift.
2026 while (buflen > 0) {
2027 uint64_t bn = offset / bsize;
2028 int boff = offset % bsize;
2030 const blkptr_t *indbp;
2033 if (bn > dnode->dn_maxblkid)
2036 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2039 indbp = dnode->dn_blkptr;
2040 for (i = 0; i < nlevels; i++) {
2042 * Copy the bp from the indirect array so that
2043 * we can re-use the scratch buffer for multi-level
2046 ibn = bn >> ((nlevels - i - 1) * ibshift);
2047 ibn &= ((1 << ibshift) - 1);
2049 if (BP_IS_HOLE(&bp)) {
2050 memset(dnode_cache_buf, 0, bsize);
2053 rc = zio_read(spa, &bp, dnode_cache_buf);
2056 indbp = (const blkptr_t *) dnode_cache_buf;
2058 dnode_cache_obj = dnode;
2059 dnode_cache_bn = bn;
2063 * The buffer contains our data block. Copy what we
2064 * need from it and loop.
2067 if (i > buflen) i = buflen;
2068 memcpy(buf, &dnode_cache_buf[boff], i);
2069 buf = ((char*) buf) + i;
2078 * Lookup a value in a microzap directory. Assumes that the zap
2079 * scratch buffer contains the directory contents.
2082 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
2084 const mzap_phys_t *mz;
2085 const mzap_ent_phys_t *mze;
2090 * Microzap objects use exactly one block. Read the whole
2093 size = dnode->dn_datablkszsec * 512;
2095 mz = (const mzap_phys_t *) zap_scratch;
2096 chunks = size / MZAP_ENT_LEN - 1;
2098 for (i = 0; i < chunks; i++) {
2099 mze = &mz->mz_chunk[i];
2100 if (!strcmp(mze->mze_name, name)) {
2101 *value = mze->mze_value;
2110 * Compare a name with a zap leaf entry. Return non-zero if the name
2114 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
2117 const zap_leaf_chunk_t *nc;
2120 namelen = zc->l_entry.le_name_numints;
2122 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2124 while (namelen > 0) {
2127 if (len > ZAP_LEAF_ARRAY_BYTES)
2128 len = ZAP_LEAF_ARRAY_BYTES;
2129 if (memcmp(p, nc->l_array.la_array, len))
2133 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2140 * Extract a uint64_t value from a zap leaf entry.
2143 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2145 const zap_leaf_chunk_t *vc;
2150 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2151 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2152 value = (value << 8) | p[i];
2159 stv(int len, void *addr, uint64_t value)
2163 *(uint8_t *)addr = value;
2166 *(uint16_t *)addr = value;
2169 *(uint32_t *)addr = value;
2172 *(uint64_t *)addr = value;
2178 * Extract a array from a zap leaf entry.
2181 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2182 uint64_t integer_size, uint64_t num_integers, void *buf)
2184 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2186 uint64_t *u64 = buf;
2188 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2189 int chunk = zc->l_entry.le_value_chunk;
2192 if (integer_size == 8 && len == 1) {
2193 *u64 = fzap_leaf_value(zl, zc);
2198 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2201 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2202 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2203 value = (value << 8) | la->la_array[i];
2205 if (byten == array_int_len) {
2206 stv(integer_size, p, value);
2214 chunk = la->la_next;
2219 * Lookup a value in a fatzap directory. Assumes that the zap scratch
2220 * buffer contains the directory header.
2223 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2224 uint64_t integer_size, uint64_t num_integers, void *value)
2226 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2227 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
2233 if (zh.zap_magic != ZAP_MAGIC)
2236 z.zap_block_shift = ilog2(bsize);
2237 z.zap_phys = (zap_phys_t *) zap_scratch;
2240 * Figure out where the pointer table is and read it in if necessary.
2242 if (zh.zap_ptrtbl.zt_blk) {
2243 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
2244 zap_scratch, bsize);
2247 ptrtbl = (uint64_t *) zap_scratch;
2249 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
2252 hash = zap_hash(zh.zap_salt, name);
2255 zl.l_bs = z.zap_block_shift;
2257 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
2258 zap_leaf_chunk_t *zc;
2260 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
2264 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
2267 * Make sure this chunk matches our hash.
2269 if (zl.l_phys->l_hdr.lh_prefix_len > 0
2270 && zl.l_phys->l_hdr.lh_prefix
2271 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
2275 * Hash within the chunk to find our entry.
2277 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
2278 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
2279 h = zl.l_phys->l_hash[h];
2282 zc = &ZAP_LEAF_CHUNK(&zl, h);
2283 while (zc->l_entry.le_hash != hash) {
2284 if (zc->l_entry.le_next == 0xffff) {
2288 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
2290 if (fzap_name_equal(&zl, zc, name)) {
2291 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
2292 integer_size * num_integers)
2294 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
2302 * Lookup a name in a zap object and return its value as a uint64_t.
2305 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2306 uint64_t integer_size, uint64_t num_integers, void *value)
2310 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2312 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
2316 zap_type = *(uint64_t *) zap_scratch;
2317 if (zap_type == ZBT_MICRO)
2318 return mzap_lookup(dnode, name, value);
2319 else if (zap_type == ZBT_HEADER) {
2320 return fzap_lookup(spa, dnode, name, integer_size,
2321 num_integers, value);
2323 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
2328 * List a microzap directory. Assumes that the zap scratch buffer contains
2329 * the directory contents.
2332 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
2334 const mzap_phys_t *mz;
2335 const mzap_ent_phys_t *mze;
2340 * Microzap objects use exactly one block. Read the whole
2343 size = dnode->dn_datablkszsec * 512;
2344 mz = (const mzap_phys_t *) zap_scratch;
2345 chunks = size / MZAP_ENT_LEN - 1;
2347 for (i = 0; i < chunks; i++) {
2348 mze = &mz->mz_chunk[i];
2349 if (mze->mze_name[0]) {
2350 rc = callback(mze->mze_name, mze->mze_value);
2360 * List a fatzap directory. Assumes that the zap scratch buffer contains
2361 * the directory header.
2364 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
2366 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2367 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
2371 if (zh.zap_magic != ZAP_MAGIC)
2374 z.zap_block_shift = ilog2(bsize);
2375 z.zap_phys = (zap_phys_t *) zap_scratch;
2378 * This assumes that the leaf blocks start at block 1. The
2379 * documentation isn't exactly clear on this.
2382 zl.l_bs = z.zap_block_shift;
2383 for (i = 0; i < zh.zap_num_leafs; i++) {
2384 off_t off = (i + 1) << zl.l_bs;
2388 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
2391 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
2393 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2394 zap_leaf_chunk_t *zc, *nc;
2397 zc = &ZAP_LEAF_CHUNK(&zl, j);
2398 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2400 namelen = zc->l_entry.le_name_numints;
2401 if (namelen > sizeof(name))
2402 namelen = sizeof(name);
2405 * Paste the name back together.
2407 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2409 while (namelen > 0) {
2412 if (len > ZAP_LEAF_ARRAY_BYTES)
2413 len = ZAP_LEAF_ARRAY_BYTES;
2414 memcpy(p, nc->l_array.la_array, len);
2417 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2421 * Assume the first eight bytes of the value are
2424 value = fzap_leaf_value(&zl, zc);
2426 //printf("%s 0x%jx\n", name, (uintmax_t)value);
2427 rc = callback((const char *)name, value);
2436 static int zfs_printf(const char *name, uint64_t value __unused)
2439 printf("%s\n", name);
2445 * List a zap directory.
2448 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2451 size_t size = dnode->dn_datablkszsec * 512;
2453 if (dnode_read(spa, dnode, 0, zap_scratch, size))
2456 zap_type = *(uint64_t *) zap_scratch;
2457 if (zap_type == ZBT_MICRO)
2458 return mzap_list(dnode, zfs_printf);
2460 return fzap_list(spa, dnode, zfs_printf);
2464 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
2468 offset = objnum * sizeof(dnode_phys_t);
2469 return dnode_read(spa, &os->os_meta_dnode, offset,
2470 dnode, sizeof(dnode_phys_t));
2474 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
2476 const mzap_phys_t *mz;
2477 const mzap_ent_phys_t *mze;
2482 * Microzap objects use exactly one block. Read the whole
2485 size = dnode->dn_datablkszsec * 512;
2487 mz = (const mzap_phys_t *) zap_scratch;
2488 chunks = size / MZAP_ENT_LEN - 1;
2490 for (i = 0; i < chunks; i++) {
2491 mze = &mz->mz_chunk[i];
2492 if (value == mze->mze_value) {
2493 strcpy(name, mze->mze_name);
2502 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2505 const zap_leaf_chunk_t *nc;
2508 namelen = zc->l_entry.le_name_numints;
2510 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2512 while (namelen > 0) {
2515 if (len > ZAP_LEAF_ARRAY_BYTES)
2516 len = ZAP_LEAF_ARRAY_BYTES;
2517 memcpy(p, nc->l_array.la_array, len);
2520 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2527 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
2529 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2530 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
2534 if (zh.zap_magic != ZAP_MAGIC)
2537 z.zap_block_shift = ilog2(bsize);
2538 z.zap_phys = (zap_phys_t *) zap_scratch;
2541 * This assumes that the leaf blocks start at block 1. The
2542 * documentation isn't exactly clear on this.
2545 zl.l_bs = z.zap_block_shift;
2546 for (i = 0; i < zh.zap_num_leafs; i++) {
2547 off_t off = (i + 1) << zl.l_bs;
2549 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
2552 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
2554 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2555 zap_leaf_chunk_t *zc;
2557 zc = &ZAP_LEAF_CHUNK(&zl, j);
2558 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2560 if (zc->l_entry.le_value_intlen != 8 ||
2561 zc->l_entry.le_value_numints != 1)
2564 if (fzap_leaf_value(&zl, zc) == value) {
2565 fzap_name_copy(&zl, zc, name);
2575 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
2579 size_t size = dnode->dn_datablkszsec * 512;
2581 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
2585 zap_type = *(uint64_t *) zap_scratch;
2586 if (zap_type == ZBT_MICRO)
2587 return mzap_rlookup(spa, dnode, name, value);
2589 return fzap_rlookup(spa, dnode, name, value);
2593 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
2596 char component[256];
2597 uint64_t dir_obj, parent_obj, child_dir_zapobj;
2598 dnode_phys_t child_dir_zap, dataset, dir, parent;
2600 dsl_dataset_phys_t *ds;
2604 p = &name[sizeof(name) - 1];
2607 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2608 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2611 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
2612 dir_obj = ds->ds_dir_obj;
2615 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
2617 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2619 /* Actual loop condition. */
2620 parent_obj = dd->dd_parent_obj;
2621 if (parent_obj == 0)
2624 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
2626 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
2627 child_dir_zapobj = dd->dd_child_dir_zapobj;
2628 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
2630 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
2633 len = strlen(component);
2635 memcpy(p, component, len);
2639 /* Actual loop iteration. */
2640 dir_obj = parent_obj;
2651 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
2654 uint64_t dir_obj, child_dir_zapobj;
2655 dnode_phys_t child_dir_zap, dir;
2659 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
2661 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
2667 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
2669 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2673 /* Actual loop condition #1. */
2679 memcpy(element, p, q - p);
2680 element[q - p] = '\0';
2687 child_dir_zapobj = dd->dd_child_dir_zapobj;
2688 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
2691 /* Actual loop condition #2. */
2692 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
2697 *objnum = dd->dd_head_dataset_obj;
2703 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
2705 uint64_t dir_obj, child_dir_zapobj;
2706 dnode_phys_t child_dir_zap, dir, dataset;
2707 dsl_dataset_phys_t *ds;
2710 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2711 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2714 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2715 dir_obj = ds->ds_dir_obj;
2717 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2718 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2721 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2723 child_dir_zapobj = dd->dd_child_dir_zapobj;
2724 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2725 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2729 return (zap_list(spa, &child_dir_zap) != 0);
2733 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2735 uint64_t dir_obj, child_dir_zapobj, zap_type;
2736 dnode_phys_t child_dir_zap, dir, dataset;
2737 dsl_dataset_phys_t *ds;
2741 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2743 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2746 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2747 dir_obj = ds->ds_dir_obj;
2749 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2751 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2754 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2756 child_dir_zapobj = dd->dd_child_dir_zapobj;
2757 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2759 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2763 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2767 zap_type = *(uint64_t *) zap_scratch;
2768 if (zap_type == ZBT_MICRO)
2769 return mzap_list(&child_dir_zap, callback);
2771 return fzap_list(spa, &child_dir_zap, callback);
2776 * Find the object set given the object number of its dataset object
2777 * and return its details in *objset
2780 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2782 dnode_phys_t dataset;
2783 dsl_dataset_phys_t *ds;
2785 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2786 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2790 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2791 if (zio_read(spa, &ds->ds_bp, objset)) {
2792 printf("ZFS: can't read object set for dataset %ju\n",
2801 * Find the object set pointed to by the BOOTFS property or the root
2802 * dataset if there is none and return its details in *objset
2805 zfs_get_root(const spa_t *spa, uint64_t *objid)
2807 dnode_phys_t dir, propdir;
2808 uint64_t props, bootfs, root;
2813 * Start with the MOS directory object.
2815 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2816 printf("ZFS: can't read MOS object directory\n");
2821 * Lookup the pool_props and see if we can find a bootfs.
2823 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2824 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2825 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2832 * Lookup the root dataset directory
2834 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2835 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2836 printf("ZFS: can't find root dsl_dir\n");
2841 * Use the information from the dataset directory's bonus buffer
2842 * to find the dataset object and from that the object set itself.
2844 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2845 *objid = dd->dd_head_dataset_obj;
2850 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2856 * Find the root object set if not explicitly provided
2858 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2859 printf("ZFS: can't find root filesystem\n");
2863 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2864 printf("ZFS: can't open root filesystem\n");
2868 mount->rootobj = rootobj;
2874 * callback function for feature name checks.
2877 check_feature(const char *name, uint64_t value)
2883 if (name[0] == '\0')
2886 for (i = 0; features_for_read[i] != NULL; i++) {
2887 if (strcmp(name, features_for_read[i]) == 0)
2890 printf("ZFS: unsupported feature: %s\n", name);
2895 * Checks whether the MOS features that are active are supported.
2898 check_mos_features(const spa_t *spa)
2901 uint64_t objnum, zap_type;
2905 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2908 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2909 sizeof (objnum), 1, &objnum)) != 0) {
2911 * It is older pool without features. As we have already
2912 * tested the label, just return without raising the error.
2917 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2920 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2923 size = dir.dn_datablkszsec * 512;
2924 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2927 zap_type = *(uint64_t *) zap_scratch;
2928 if (zap_type == ZBT_MICRO)
2929 rc = mzap_list(&dir, check_feature);
2931 rc = fzap_list(spa, &dir, check_feature);
2937 load_nvlist(spa_t *spa, uint64_t obj, unsigned char **value)
2945 if ((rc = objset_get_dnode(spa, &spa->spa_mos, obj, &dir)) != 0)
2947 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
2948 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
2952 if (dir.dn_bonuslen != sizeof (uint64_t))
2955 size = *(uint64_t *)DN_BONUS(&dir);
2960 rc = dnode_read(spa, &dir, 0, nv, size);
2971 zfs_spa_init(spa_t *spa)
2974 uint64_t config_object;
2975 unsigned char *nvlist;
2977 const unsigned char *nv;
2980 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2981 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2984 if (spa->spa_mos.os_type != DMU_OST_META) {
2985 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2989 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2991 printf("ZFS: failed to read pool %s directory object\n",
2995 /* this is allowed to fail, older pools do not have salt */
2996 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2997 sizeof (spa->spa_cksum_salt.zcs_bytes),
2998 spa->spa_cksum_salt.zcs_bytes);
3000 rc = check_mos_features(spa);
3002 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3006 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3007 sizeof (config_object), 1, &config_object);
3009 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3012 rc = load_nvlist(spa, config_object, &nvlist);
3016 /* Update vdevs from MOS config. */
3017 if (nvlist_find(nvlist + 4, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
3023 if (nvlist_find(nv, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
3025 printf("ZFS: can't find vdev details\n");
3029 if (strcmp(type, VDEV_TYPE_ROOT) != 0) {
3034 rc = nvlist_find(nv, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
3039 for (int i = 0; i < nkids; i++) {
3040 vdev_t *vd, *prev, *kid = NULL;
3041 rc = vdev_init_from_nvlist(nv, NULL, &kid, 0);
3043 printf("vdev_init_from_nvlist: %d\n", rc);
3048 STAILQ_FOREACH(vd, &spa->spa_vdevs, v_childlink) {
3049 /* Already present? */
3050 if (kid->v_id == vd->v_id) {
3054 if (vd->v_id > kid->v_id) {
3056 STAILQ_INSERT_HEAD(&spa->spa_vdevs,
3059 STAILQ_INSERT_AFTER(&spa->spa_vdevs,
3060 prev, kid, v_childlink);
3068 STAILQ_INSERT_TAIL(&spa->spa_vdevs, kid, v_childlink);
3069 nv = nvlist_next(nv);
3078 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3081 if (dn->dn_bonustype != DMU_OT_SA) {
3082 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3084 sb->st_mode = zp->zp_mode;
3085 sb->st_uid = zp->zp_uid;
3086 sb->st_gid = zp->zp_gid;
3087 sb->st_size = zp->zp_size;
3089 sa_hdr_phys_t *sahdrp;
3094 if (dn->dn_bonuslen != 0)
3095 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3097 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3098 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3101 size = BP_GET_LSIZE(bp);
3102 buf = zfs_alloc(size);
3103 error = zio_read(spa, bp, buf);
3105 zfs_free(buf, size);
3113 hdrsize = SA_HDR_SIZE(sahdrp);
3114 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3116 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3118 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3120 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3123 zfs_free(buf, size);
3130 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3134 if (dn->dn_bonustype == DMU_OT_SA) {
3135 sa_hdr_phys_t *sahdrp = NULL;
3141 if (dn->dn_bonuslen != 0)
3142 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3146 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3148 bp = DN_SPILL_BLKPTR(dn);
3150 size = BP_GET_LSIZE(bp);
3151 buf = zfs_alloc(size);
3152 rc = zio_read(spa, bp, buf);
3154 zfs_free(buf, size);
3159 hdrsize = SA_HDR_SIZE(sahdrp);
3160 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3161 memcpy(path, p, psize);
3163 zfs_free(buf, size);
3167 * Second test is purely to silence bogus compiler
3168 * warning about accessing past the end of dn_bonus.
3170 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3171 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3172 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3174 rc = dnode_read(spa, dn, 0, path, psize);
3181 STAILQ_ENTRY(obj_list) entry;
3185 * Lookup a file and return its dnode.
3188 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3197 int symlinks_followed = 0;
3199 struct obj_list *entry, *tentry;
3200 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3203 if (mount->objset.os_type != DMU_OST_ZFS) {
3204 printf("ZFS: unexpected object set type %ju\n",
3205 (uintmax_t)mount->objset.os_type);
3209 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3213 * Get the root directory dnode.
3215 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3221 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
3226 entry->objnum = objnum;
3227 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3229 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3235 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3244 while (*q != '\0' && *q != '/')
3248 if (p + 1 == q && p[0] == '.') {
3253 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3255 if (STAILQ_FIRST(&on_cache) ==
3256 STAILQ_LAST(&on_cache, obj_list, entry)) {
3260 entry = STAILQ_FIRST(&on_cache);
3261 STAILQ_REMOVE_HEAD(&on_cache, entry);
3263 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3266 if (q - p + 1 > sizeof(element)) {
3270 memcpy(element, p, q - p);
3274 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3276 if (!S_ISDIR(sb.st_mode)) {
3281 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3284 objnum = ZFS_DIRENT_OBJ(objnum);
3286 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3290 entry->objnum = objnum;
3291 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3292 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3297 * Check for symlink.
3299 rc = zfs_dnode_stat(spa, &dn, &sb);
3302 if (S_ISLNK(sb.st_mode)) {
3303 if (symlinks_followed > 10) {
3307 symlinks_followed++;
3310 * Read the link value and copy the tail of our
3311 * current path onto the end.
3313 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3317 strcpy(&path[sb.st_size], p);
3319 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3324 * Restart with the new path, starting either at
3325 * the root or at the parent depending whether or
3326 * not the link is relative.
3330 while (STAILQ_FIRST(&on_cache) !=
3331 STAILQ_LAST(&on_cache, obj_list, entry)) {
3332 entry = STAILQ_FIRST(&on_cache);
3333 STAILQ_REMOVE_HEAD(&on_cache, entry);
3337 entry = STAILQ_FIRST(&on_cache);
3338 STAILQ_REMOVE_HEAD(&on_cache, entry);
3341 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3347 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)