2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Stand-alone ZFS file reader.
35 #include <sys/stdint.h>
48 * List of all vdevs, chained through v_alllink.
50 static vdev_list_t zfs_vdevs;
53 * List of ZFS features supported for read
55 static const char *features_for_read[] = {
56 "org.illumos:lz4_compress",
57 "com.delphix:hole_birth",
58 "com.delphix:extensible_dataset",
59 "com.delphix:embedded_data",
60 "org.open-zfs:large_blocks",
67 * List of all pools, chained through spa_link.
69 static spa_list_t zfs_pools;
71 static uint64_t zfs_crc64_table[256];
72 static const dnode_phys_t *dnode_cache_obj = 0;
73 static uint64_t dnode_cache_bn;
74 static char *dnode_cache_buf;
75 static char *zap_scratch;
76 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
78 #define TEMP_SIZE (1024 * 1024)
80 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
81 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
82 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
83 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
84 const char *name, uint64_t integer_size, uint64_t num_integers,
90 STAILQ_INIT(&zfs_vdevs);
91 STAILQ_INIT(&zfs_pools);
93 zfs_temp_buf = malloc(TEMP_SIZE);
94 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
95 zfs_temp_ptr = zfs_temp_buf;
96 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
97 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
103 zfs_alloc(size_t size)
107 if (zfs_temp_ptr + size > zfs_temp_end) {
108 printf("ZFS: out of temporary buffer space\n");
112 zfs_temp_ptr += size;
118 zfs_free(void *ptr, size_t size)
121 zfs_temp_ptr -= size;
122 if (zfs_temp_ptr != ptr) {
123 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
129 xdr_int(const unsigned char **xdr, int *ip)
131 *ip = ((*xdr)[0] << 24)
140 xdr_u_int(const unsigned char **xdr, u_int *ip)
142 *ip = ((*xdr)[0] << 24)
151 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
157 *lp = (((uint64_t) hi) << 32) | lo;
162 nvlist_find(const unsigned char *nvlist, const char *name, int type,
163 int* elementsp, void *valuep)
165 const unsigned char *p, *pair;
167 int encoded_size, decoded_size;
174 xdr_int(&p, &encoded_size);
175 xdr_int(&p, &decoded_size);
176 while (encoded_size && decoded_size) {
177 int namelen, pairtype, elements;
178 const char *pairname;
180 xdr_int(&p, &namelen);
181 pairname = (const char*) p;
182 p += roundup(namelen, 4);
183 xdr_int(&p, &pairtype);
185 if (!memcmp(name, pairname, namelen) && type == pairtype) {
186 xdr_int(&p, &elements);
188 *elementsp = elements;
189 if (type == DATA_TYPE_UINT64) {
190 xdr_uint64_t(&p, (uint64_t *) valuep);
192 } else if (type == DATA_TYPE_STRING) {
195 (*(const char**) valuep) = (const char*) p;
197 } else if (type == DATA_TYPE_NVLIST
198 || type == DATA_TYPE_NVLIST_ARRAY) {
199 (*(const unsigned char**) valuep) =
200 (const unsigned char*) p;
207 * Not the pair we are looking for, skip to the next one.
209 p = pair + encoded_size;
213 xdr_int(&p, &encoded_size);
214 xdr_int(&p, &decoded_size);
221 nvlist_check_features_for_read(const unsigned char *nvlist)
223 const unsigned char *p, *pair;
225 int encoded_size, decoded_size;
235 xdr_int(&p, &encoded_size);
236 xdr_int(&p, &decoded_size);
237 while (encoded_size && decoded_size) {
238 int namelen, pairtype;
239 const char *pairname;
244 xdr_int(&p, &namelen);
245 pairname = (const char*) p;
246 p += roundup(namelen, 4);
247 xdr_int(&p, &pairtype);
249 for (i = 0; features_for_read[i] != NULL; i++) {
250 if (!memcmp(pairname, features_for_read[i], namelen)) {
257 printf("ZFS: unsupported feature: %s\n", pairname);
261 p = pair + encoded_size;
264 xdr_int(&p, &encoded_size);
265 xdr_int(&p, &decoded_size);
272 * Return the next nvlist in an nvlist array.
274 static const unsigned char *
275 nvlist_next(const unsigned char *nvlist)
277 const unsigned char *p, *pair;
279 int encoded_size, decoded_size;
286 xdr_int(&p, &encoded_size);
287 xdr_int(&p, &decoded_size);
288 while (encoded_size && decoded_size) {
289 p = pair + encoded_size;
292 xdr_int(&p, &encoded_size);
293 xdr_int(&p, &decoded_size);
301 static const unsigned char *
302 nvlist_print(const unsigned char *nvlist, unsigned int indent)
304 static const char* typenames[] = {
315 "DATA_TYPE_BYTE_ARRAY",
316 "DATA_TYPE_INT16_ARRAY",
317 "DATA_TYPE_UINT16_ARRAY",
318 "DATA_TYPE_INT32_ARRAY",
319 "DATA_TYPE_UINT32_ARRAY",
320 "DATA_TYPE_INT64_ARRAY",
321 "DATA_TYPE_UINT64_ARRAY",
322 "DATA_TYPE_STRING_ARRAY",
325 "DATA_TYPE_NVLIST_ARRAY",
326 "DATA_TYPE_BOOLEAN_VALUE",
329 "DATA_TYPE_BOOLEAN_ARRAY",
330 "DATA_TYPE_INT8_ARRAY",
331 "DATA_TYPE_UINT8_ARRAY"
335 const unsigned char *p, *pair;
337 int encoded_size, decoded_size;
344 xdr_int(&p, &encoded_size);
345 xdr_int(&p, &decoded_size);
346 while (encoded_size && decoded_size) {
347 int namelen, pairtype, elements;
348 const char *pairname;
350 xdr_int(&p, &namelen);
351 pairname = (const char*) p;
352 p += roundup(namelen, 4);
353 xdr_int(&p, &pairtype);
355 for (i = 0; i < indent; i++)
357 printf("%s %s", typenames[pairtype], pairname);
359 xdr_int(&p, &elements);
361 case DATA_TYPE_UINT64: {
363 xdr_uint64_t(&p, &val);
364 printf(" = 0x%jx\n", (uintmax_t)val);
368 case DATA_TYPE_STRING: {
371 printf(" = \"%s\"\n", p);
375 case DATA_TYPE_NVLIST:
377 nvlist_print(p, indent + 1);
380 case DATA_TYPE_NVLIST_ARRAY:
381 for (j = 0; j < elements; j++) {
383 p = nvlist_print(p, indent + 1);
384 if (j != elements - 1) {
385 for (i = 0; i < indent; i++)
387 printf("%s %s", typenames[pairtype], pairname);
396 p = pair + encoded_size;
399 xdr_int(&p, &encoded_size);
400 xdr_int(&p, &decoded_size);
409 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
410 off_t offset, size_t size)
415 if (!vdev->v_phys_read)
419 psize = BP_GET_PSIZE(bp);
424 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
425 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
428 if (bp && zio_checksum_verify(vdev->spa, bp, buf))
435 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
436 off_t offset, size_t bytes)
439 return (vdev_read_phys(vdev, bp, buf,
440 offset + VDEV_LABEL_START_SIZE, bytes));
445 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
446 off_t offset, size_t bytes)
452 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
453 if (kid->v_state != VDEV_STATE_HEALTHY)
455 rc = kid->v_read(kid, bp, buf, offset, bytes);
464 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
465 off_t offset, size_t bytes)
470 * Here we should have two kids:
471 * First one which is the one we are replacing and we can trust
472 * only this one to have valid data, but it might not be present.
473 * Second one is that one we are replacing with. It is most likely
474 * healthy, but we can't trust it has needed data, so we won't use it.
476 kid = STAILQ_FIRST(&vdev->v_children);
479 if (kid->v_state != VDEV_STATE_HEALTHY)
481 return (kid->v_read(kid, bp, buf, offset, bytes));
485 vdev_find(uint64_t guid)
489 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
490 if (vdev->v_guid == guid)
497 vdev_create(uint64_t guid, vdev_read_t *read)
501 vdev = malloc(sizeof(vdev_t));
502 memset(vdev, 0, sizeof(vdev_t));
503 STAILQ_INIT(&vdev->v_children);
505 vdev->v_state = VDEV_STATE_OFFLINE;
507 vdev->v_phys_read = 0;
508 vdev->v_read_priv = 0;
509 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
515 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
516 vdev_t **vdevp, int is_newer)
519 uint64_t guid, id, ashift, nparity;
523 const unsigned char *kids;
524 int nkids, i, is_new;
525 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
527 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
528 DATA_TYPE_UINT64, 0, &guid)
529 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
530 DATA_TYPE_UINT64, 0, &id)
531 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
532 DATA_TYPE_STRING, 0, &type)) {
533 printf("ZFS: can't find vdev details\n");
537 if (strcmp(type, VDEV_TYPE_MIRROR)
538 && strcmp(type, VDEV_TYPE_DISK)
540 && strcmp(type, VDEV_TYPE_FILE)
542 && strcmp(type, VDEV_TYPE_RAIDZ)
543 && strcmp(type, VDEV_TYPE_REPLACING)) {
544 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
548 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
550 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
552 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
554 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
556 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
558 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
561 vdev = vdev_find(guid);
565 if (!strcmp(type, VDEV_TYPE_MIRROR))
566 vdev = vdev_create(guid, vdev_mirror_read);
567 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
568 vdev = vdev_create(guid, vdev_raidz_read);
569 else if (!strcmp(type, VDEV_TYPE_REPLACING))
570 vdev = vdev_create(guid, vdev_replacing_read);
572 vdev = vdev_create(guid, vdev_disk_read);
575 vdev->v_top = pvdev != NULL ? pvdev : vdev;
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
577 DATA_TYPE_UINT64, 0, &ashift) == 0)
578 vdev->v_ashift = ashift;
581 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
582 DATA_TYPE_UINT64, 0, &nparity) == 0)
583 vdev->v_nparity = nparity;
586 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
587 DATA_TYPE_STRING, 0, &path) == 0) {
588 if (strncmp(path, "/dev/", 5) == 0)
590 vdev->v_name = strdup(path);
592 if (!strcmp(type, "raidz")) {
593 if (vdev->v_nparity == 1)
594 vdev->v_name = "raidz1";
595 else if (vdev->v_nparity == 2)
596 vdev->v_name = "raidz2";
597 else if (vdev->v_nparity == 3)
598 vdev->v_name = "raidz3";
600 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
604 vdev->v_name = strdup(type);
611 if (is_new || is_newer) {
613 * This is either new vdev or we've already seen this vdev,
614 * but from an older vdev label, so let's refresh its state
615 * from the newer label.
618 vdev->v_state = VDEV_STATE_OFFLINE;
620 vdev->v_state = VDEV_STATE_REMOVED;
622 vdev->v_state = VDEV_STATE_FAULTED;
623 else if (is_degraded)
624 vdev->v_state = VDEV_STATE_DEGRADED;
625 else if (isnt_present)
626 vdev->v_state = VDEV_STATE_CANT_OPEN;
629 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
630 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
632 * Its ok if we don't have any kids.
635 vdev->v_nchildren = nkids;
636 for (i = 0; i < nkids; i++) {
637 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
641 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
643 kids = nvlist_next(kids);
646 vdev->v_nchildren = 0;
655 vdev_set_state(vdev_t *vdev)
662 * A mirror or raidz is healthy if all its kids are healthy. A
663 * mirror is degraded if any of its kids is healthy; a raidz
664 * is degraded if at most nparity kids are offline.
666 if (STAILQ_FIRST(&vdev->v_children)) {
669 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
670 if (kid->v_state == VDEV_STATE_HEALTHY)
676 vdev->v_state = VDEV_STATE_HEALTHY;
678 if (vdev->v_read == vdev_mirror_read) {
680 vdev->v_state = VDEV_STATE_DEGRADED;
682 vdev->v_state = VDEV_STATE_OFFLINE;
684 } else if (vdev->v_read == vdev_raidz_read) {
685 if (bad_kids > vdev->v_nparity) {
686 vdev->v_state = VDEV_STATE_OFFLINE;
688 vdev->v_state = VDEV_STATE_DEGRADED;
696 spa_find_by_guid(uint64_t guid)
700 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
701 if (spa->spa_guid == guid)
708 spa_find_by_name(const char *name)
712 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
713 if (!strcmp(spa->spa_name, name))
721 spa_get_primary(void)
724 return (STAILQ_FIRST(&zfs_pools));
728 spa_get_primary_vdev(const spa_t *spa)
734 spa = spa_get_primary();
737 vdev = STAILQ_FIRST(&spa->spa_vdevs);
740 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
741 kid = STAILQ_FIRST(&vdev->v_children))
748 spa_create(uint64_t guid)
752 spa = malloc(sizeof(spa_t));
753 memset(spa, 0, sizeof(spa_t));
754 STAILQ_INIT(&spa->spa_vdevs);
755 spa->spa_guid = guid;
756 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
762 state_name(vdev_state_t state)
764 static const char* names[] = {
779 #define pager_printf printf
784 pager_printf(const char *fmt, ...)
790 vsprintf(line, fmt, args);
797 #define STATUS_FORMAT " %s %s\n"
800 print_state(int indent, const char *name, vdev_state_t state)
806 for (i = 0; i < indent; i++)
809 pager_printf(STATUS_FORMAT, buf, state_name(state));
814 vdev_status(vdev_t *vdev, int indent)
817 print_state(indent, vdev->v_name, vdev->v_state);
819 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
820 vdev_status(kid, indent + 1);
825 spa_status(spa_t *spa)
827 static char bootfs[ZFS_MAXNAMELEN];
830 int good_kids, bad_kids, degraded_kids;
833 pager_printf(" pool: %s\n", spa->spa_name);
834 if (zfs_get_root(spa, &rootid) == 0 &&
835 zfs_rlookup(spa, rootid, bootfs) == 0) {
836 if (bootfs[0] == '\0')
837 pager_printf("bootfs: %s\n", spa->spa_name);
839 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
841 pager_printf("config:\n\n");
842 pager_printf(STATUS_FORMAT, "NAME", "STATE");
847 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
848 if (vdev->v_state == VDEV_STATE_HEALTHY)
850 else if (vdev->v_state == VDEV_STATE_DEGRADED)
856 state = VDEV_STATE_CLOSED;
857 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
858 state = VDEV_STATE_HEALTHY;
859 else if ((good_kids + degraded_kids) > 0)
860 state = VDEV_STATE_DEGRADED;
862 print_state(0, spa->spa_name, state);
863 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
864 vdev_status(vdev, 1);
874 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
883 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
886 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
888 vdev_t *vdev, *top_vdev, *pool_vdev;
891 const unsigned char *nvlist;
894 uint64_t pool_txg, pool_guid;
896 const char *pool_name;
897 const unsigned char *vdevs;
898 const unsigned char *features;
901 const struct uberblock *up;
904 * Load the vdev label and figure out which
905 * uberblock is most current.
907 memset(&vtmp, 0, sizeof(vtmp));
908 vtmp.v_phys_read = read;
909 vtmp.v_read_priv = read_priv;
910 off = offsetof(vdev_label_t, vl_vdev_phys);
912 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
913 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
914 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
915 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
916 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
917 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
918 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
921 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
925 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
927 if (nvlist_find(nvlist,
928 ZPOOL_CONFIG_VERSION,
929 DATA_TYPE_UINT64, 0, &val)) {
933 if (!SPA_VERSION_IS_SUPPORTED(val)) {
934 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
935 (unsigned) val, (unsigned) SPA_VERSION);
939 /* Check ZFS features for read */
940 if (nvlist_find(nvlist,
941 ZPOOL_CONFIG_FEATURES_FOR_READ,
942 DATA_TYPE_NVLIST, 0, &features) == 0
943 && nvlist_check_features_for_read(features) != 0)
946 if (nvlist_find(nvlist,
947 ZPOOL_CONFIG_POOL_STATE,
948 DATA_TYPE_UINT64, 0, &val)) {
952 if (val == POOL_STATE_DESTROYED) {
953 /* We don't boot only from destroyed pools. */
957 if (nvlist_find(nvlist,
958 ZPOOL_CONFIG_POOL_TXG,
959 DATA_TYPE_UINT64, 0, &pool_txg)
960 || nvlist_find(nvlist,
961 ZPOOL_CONFIG_POOL_GUID,
962 DATA_TYPE_UINT64, 0, &pool_guid)
963 || nvlist_find(nvlist,
964 ZPOOL_CONFIG_POOL_NAME,
965 DATA_TYPE_STRING, 0, &pool_name)) {
967 * Cache and spare devices end up here - just ignore
970 /*printf("ZFS: can't find pool details\n");*/
975 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
981 * Create the pool if this is the first time we've seen it.
983 spa = spa_find_by_guid(pool_guid);
985 spa = spa_create(pool_guid);
986 spa->spa_name = strdup(pool_name);
988 if (pool_txg > spa->spa_txg) {
989 spa->spa_txg = pool_txg;
995 * Get the vdev tree and create our in-core copy of it.
996 * If we already have a vdev with this guid, this must
997 * be some kind of alias (overlapping slices, dangerously dedicated
1000 if (nvlist_find(nvlist,
1002 DATA_TYPE_UINT64, 0, &guid)) {
1005 vdev = vdev_find(guid);
1006 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1009 if (nvlist_find(nvlist,
1010 ZPOOL_CONFIG_VDEV_TREE,
1011 DATA_TYPE_NVLIST, 0, &vdevs)) {
1015 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1020 * Add the toplevel vdev to the pool if its not already there.
1022 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1023 if (top_vdev == pool_vdev)
1025 if (!pool_vdev && top_vdev)
1026 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1029 * We should already have created an incomplete vdev for this
1030 * vdev. Find it and initialise it with our read proc.
1032 vdev = vdev_find(guid);
1034 vdev->v_phys_read = read;
1035 vdev->v_read_priv = read_priv;
1036 vdev->v_state = VDEV_STATE_HEALTHY;
1038 printf("ZFS: inconsistent nvlist contents\n");
1043 * Re-evaluate top-level vdev state.
1045 vdev_set_state(top_vdev);
1048 * Ok, we are happy with the pool so far. Lets find
1049 * the best uberblock and then we can actually access
1050 * the contents of the pool.
1052 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1053 up = (const struct uberblock *)upbuf;
1055 i < VDEV_UBERBLOCK_COUNT(vdev);
1057 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1059 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1060 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1061 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1062 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1063 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1064 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1066 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1069 if (up->ub_magic != UBERBLOCK_MAGIC)
1071 if (up->ub_txg < spa->spa_txg)
1073 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1074 spa->spa_uberblock = *up;
1075 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1076 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1077 spa->spa_uberblock = *up;
1080 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1093 for (v = 0; v < 32; v++)
1100 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1103 zio_gbh_phys_t zio_gb;
1107 /* Artificial BP for gang block header. */
1109 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1110 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1111 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1112 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1113 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1114 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1116 /* Read gang header block using the artificial BP. */
1117 if (zio_read(spa, &gbh_bp, &zio_gb))
1121 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1122 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1124 if (BP_IS_HOLE(gbp))
1126 if (zio_read(spa, gbp, pbuf))
1128 pbuf += BP_GET_PSIZE(gbp);
1131 if (zio_checksum_verify(spa, bp, buf))
1137 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1139 int cpfunc = BP_GET_COMPRESS(bp);
1140 uint64_t align, size;
1145 * Process data embedded in block pointer
1147 if (BP_IS_EMBEDDED(bp)) {
1148 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1150 size = BPE_GET_PSIZE(bp);
1151 ASSERT(size <= BPE_PAYLOAD_SIZE);
1153 if (cpfunc != ZIO_COMPRESS_OFF)
1154 pbuf = zfs_alloc(size);
1158 decode_embedded_bp_compressed(bp, pbuf);
1161 if (cpfunc != ZIO_COMPRESS_OFF) {
1162 error = zio_decompress_data(cpfunc, pbuf,
1163 size, buf, BP_GET_LSIZE(bp));
1164 zfs_free(pbuf, size);
1167 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1174 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1175 const dva_t *dva = &bp->blk_dva[i];
1180 if (!dva->dva_word[0] && !dva->dva_word[1])
1183 vdevid = DVA_GET_VDEV(dva);
1184 offset = DVA_GET_OFFSET(dva);
1185 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1186 if (vdev->v_id == vdevid)
1189 if (!vdev || !vdev->v_read)
1192 size = BP_GET_PSIZE(bp);
1193 if (vdev->v_read == vdev_raidz_read) {
1194 align = 1ULL << vdev->v_top->v_ashift;
1195 if (P2PHASE(size, align) != 0)
1196 size = P2ROUNDUP(size, align);
1198 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1199 pbuf = zfs_alloc(size);
1203 if (DVA_GET_GANG(dva))
1204 error = zio_read_gang(spa, bp, pbuf);
1206 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1208 if (cpfunc != ZIO_COMPRESS_OFF)
1209 error = zio_decompress_data(cpfunc, pbuf,
1210 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1211 else if (size != BP_GET_PSIZE(bp))
1212 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1215 zfs_free(pbuf, size);
1220 printf("ZFS: i/o error - all block copies unavailable\n");
1225 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1227 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1228 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1229 int nlevels = dnode->dn_nlevels;
1232 if (bsize > SPA_MAXBLOCKSIZE) {
1233 printf("ZFS: I/O error - blocks larger than %llu are not "
1234 "supported\n", SPA_MAXBLOCKSIZE);
1239 * Note: bsize may not be a power of two here so we need to do an
1240 * actual divide rather than a bitshift.
1242 while (buflen > 0) {
1243 uint64_t bn = offset / bsize;
1244 int boff = offset % bsize;
1246 const blkptr_t *indbp;
1249 if (bn > dnode->dn_maxblkid)
1252 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1255 indbp = dnode->dn_blkptr;
1256 for (i = 0; i < nlevels; i++) {
1258 * Copy the bp from the indirect array so that
1259 * we can re-use the scratch buffer for multi-level
1262 ibn = bn >> ((nlevels - i - 1) * ibshift);
1263 ibn &= ((1 << ibshift) - 1);
1265 if (BP_IS_HOLE(&bp)) {
1266 memset(dnode_cache_buf, 0, bsize);
1269 rc = zio_read(spa, &bp, dnode_cache_buf);
1272 indbp = (const blkptr_t *) dnode_cache_buf;
1274 dnode_cache_obj = dnode;
1275 dnode_cache_bn = bn;
1279 * The buffer contains our data block. Copy what we
1280 * need from it and loop.
1283 if (i > buflen) i = buflen;
1284 memcpy(buf, &dnode_cache_buf[boff], i);
1285 buf = ((char*) buf) + i;
1294 * Lookup a value in a microzap directory. Assumes that the zap
1295 * scratch buffer contains the directory contents.
1298 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1300 const mzap_phys_t *mz;
1301 const mzap_ent_phys_t *mze;
1306 * Microzap objects use exactly one block. Read the whole
1309 size = dnode->dn_datablkszsec * 512;
1311 mz = (const mzap_phys_t *) zap_scratch;
1312 chunks = size / MZAP_ENT_LEN - 1;
1314 for (i = 0; i < chunks; i++) {
1315 mze = &mz->mz_chunk[i];
1316 if (!strcmp(mze->mze_name, name)) {
1317 *value = mze->mze_value;
1326 * Compare a name with a zap leaf entry. Return non-zero if the name
1330 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1333 const zap_leaf_chunk_t *nc;
1336 namelen = zc->l_entry.le_name_numints;
1338 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1340 while (namelen > 0) {
1343 if (len > ZAP_LEAF_ARRAY_BYTES)
1344 len = ZAP_LEAF_ARRAY_BYTES;
1345 if (memcmp(p, nc->l_array.la_array, len))
1349 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1356 * Extract a uint64_t value from a zap leaf entry.
1359 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1361 const zap_leaf_chunk_t *vc;
1366 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1367 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1368 value = (value << 8) | p[i];
1375 stv(int len, void *addr, uint64_t value)
1379 *(uint8_t *)addr = value;
1382 *(uint16_t *)addr = value;
1385 *(uint32_t *)addr = value;
1388 *(uint64_t *)addr = value;
1394 * Extract a array from a zap leaf entry.
1397 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1398 uint64_t integer_size, uint64_t num_integers, void *buf)
1400 uint64_t array_int_len = zc->l_entry.le_value_intlen;
1402 uint64_t *u64 = buf;
1404 int len = MIN(zc->l_entry.le_value_numints, num_integers);
1405 int chunk = zc->l_entry.le_value_chunk;
1408 if (integer_size == 8 && len == 1) {
1409 *u64 = fzap_leaf_value(zl, zc);
1414 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1417 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1418 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1419 value = (value << 8) | la->la_array[i];
1421 if (byten == array_int_len) {
1422 stv(integer_size, p, value);
1430 chunk = la->la_next;
1435 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1436 * buffer contains the directory header.
1439 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1440 uint64_t integer_size, uint64_t num_integers, void *value)
1442 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1443 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1449 if (zh.zap_magic != ZAP_MAGIC)
1452 z.zap_block_shift = ilog2(bsize);
1453 z.zap_phys = (zap_phys_t *) zap_scratch;
1456 * Figure out where the pointer table is and read it in if necessary.
1458 if (zh.zap_ptrtbl.zt_blk) {
1459 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1460 zap_scratch, bsize);
1463 ptrtbl = (uint64_t *) zap_scratch;
1465 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1468 hash = zap_hash(zh.zap_salt, name);
1471 zl.l_bs = z.zap_block_shift;
1473 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1474 zap_leaf_chunk_t *zc;
1476 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1480 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1483 * Make sure this chunk matches our hash.
1485 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1486 && zl.l_phys->l_hdr.lh_prefix
1487 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1491 * Hash within the chunk to find our entry.
1493 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1494 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1495 h = zl.l_phys->l_hash[h];
1498 zc = &ZAP_LEAF_CHUNK(&zl, h);
1499 while (zc->l_entry.le_hash != hash) {
1500 if (zc->l_entry.le_next == 0xffff) {
1504 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1506 if (fzap_name_equal(&zl, zc, name)) {
1507 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1508 integer_size * num_integers)
1510 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1518 * Lookup a name in a zap object and return its value as a uint64_t.
1521 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1522 uint64_t integer_size, uint64_t num_integers, void *value)
1526 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1528 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1532 zap_type = *(uint64_t *) zap_scratch;
1533 if (zap_type == ZBT_MICRO)
1534 return mzap_lookup(dnode, name, value);
1535 else if (zap_type == ZBT_HEADER) {
1536 return fzap_lookup(spa, dnode, name, integer_size,
1537 num_integers, value);
1539 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1544 * List a microzap directory. Assumes that the zap scratch buffer contains
1545 * the directory contents.
1548 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1550 const mzap_phys_t *mz;
1551 const mzap_ent_phys_t *mze;
1556 * Microzap objects use exactly one block. Read the whole
1559 size = dnode->dn_datablkszsec * 512;
1560 mz = (const mzap_phys_t *) zap_scratch;
1561 chunks = size / MZAP_ENT_LEN - 1;
1563 for (i = 0; i < chunks; i++) {
1564 mze = &mz->mz_chunk[i];
1565 if (mze->mze_name[0]) {
1566 rc = callback(mze->mze_name, mze->mze_value);
1576 * List a fatzap directory. Assumes that the zap scratch buffer contains
1577 * the directory header.
1580 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1582 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1583 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1587 if (zh.zap_magic != ZAP_MAGIC)
1590 z.zap_block_shift = ilog2(bsize);
1591 z.zap_phys = (zap_phys_t *) zap_scratch;
1594 * This assumes that the leaf blocks start at block 1. The
1595 * documentation isn't exactly clear on this.
1598 zl.l_bs = z.zap_block_shift;
1599 for (i = 0; i < zh.zap_num_leafs; i++) {
1600 off_t off = (i + 1) << zl.l_bs;
1604 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1607 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1609 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1610 zap_leaf_chunk_t *zc, *nc;
1613 zc = &ZAP_LEAF_CHUNK(&zl, j);
1614 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1616 namelen = zc->l_entry.le_name_numints;
1617 if (namelen > sizeof(name))
1618 namelen = sizeof(name);
1621 * Paste the name back together.
1623 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1625 while (namelen > 0) {
1628 if (len > ZAP_LEAF_ARRAY_BYTES)
1629 len = ZAP_LEAF_ARRAY_BYTES;
1630 memcpy(p, nc->l_array.la_array, len);
1633 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1637 * Assume the first eight bytes of the value are
1640 value = fzap_leaf_value(&zl, zc);
1642 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1643 rc = callback((const char *)name, value);
1652 static int zfs_printf(const char *name, uint64_t value __unused)
1655 printf("%s\n", name);
1661 * List a zap directory.
1664 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1667 size_t size = dnode->dn_datablkszsec * 512;
1669 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1672 zap_type = *(uint64_t *) zap_scratch;
1673 if (zap_type == ZBT_MICRO)
1674 return mzap_list(dnode, zfs_printf);
1676 return fzap_list(spa, dnode, zfs_printf);
1680 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1684 offset = objnum * sizeof(dnode_phys_t);
1685 return dnode_read(spa, &os->os_meta_dnode, offset,
1686 dnode, sizeof(dnode_phys_t));
1690 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1692 const mzap_phys_t *mz;
1693 const mzap_ent_phys_t *mze;
1698 * Microzap objects use exactly one block. Read the whole
1701 size = dnode->dn_datablkszsec * 512;
1703 mz = (const mzap_phys_t *) zap_scratch;
1704 chunks = size / MZAP_ENT_LEN - 1;
1706 for (i = 0; i < chunks; i++) {
1707 mze = &mz->mz_chunk[i];
1708 if (value == mze->mze_value) {
1709 strcpy(name, mze->mze_name);
1718 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1721 const zap_leaf_chunk_t *nc;
1724 namelen = zc->l_entry.le_name_numints;
1726 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1728 while (namelen > 0) {
1731 if (len > ZAP_LEAF_ARRAY_BYTES)
1732 len = ZAP_LEAF_ARRAY_BYTES;
1733 memcpy(p, nc->l_array.la_array, len);
1736 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1743 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1745 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1746 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1750 if (zh.zap_magic != ZAP_MAGIC)
1753 z.zap_block_shift = ilog2(bsize);
1754 z.zap_phys = (zap_phys_t *) zap_scratch;
1757 * This assumes that the leaf blocks start at block 1. The
1758 * documentation isn't exactly clear on this.
1761 zl.l_bs = z.zap_block_shift;
1762 for (i = 0; i < zh.zap_num_leafs; i++) {
1763 off_t off = (i + 1) << zl.l_bs;
1765 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1768 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1770 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1771 zap_leaf_chunk_t *zc;
1773 zc = &ZAP_LEAF_CHUNK(&zl, j);
1774 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1776 if (zc->l_entry.le_value_intlen != 8 ||
1777 zc->l_entry.le_value_numints != 1)
1780 if (fzap_leaf_value(&zl, zc) == value) {
1781 fzap_name_copy(&zl, zc, name);
1791 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1795 size_t size = dnode->dn_datablkszsec * 512;
1797 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1801 zap_type = *(uint64_t *) zap_scratch;
1802 if (zap_type == ZBT_MICRO)
1803 return mzap_rlookup(spa, dnode, name, value);
1805 return fzap_rlookup(spa, dnode, name, value);
1809 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1812 char component[256];
1813 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1814 dnode_phys_t child_dir_zap, dataset, dir, parent;
1816 dsl_dataset_phys_t *ds;
1820 p = &name[sizeof(name) - 1];
1823 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1824 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1827 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1828 dir_obj = ds->ds_dir_obj;
1831 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1833 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1835 /* Actual loop condition. */
1836 parent_obj = dd->dd_parent_obj;
1837 if (parent_obj == 0)
1840 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1842 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1843 child_dir_zapobj = dd->dd_child_dir_zapobj;
1844 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1846 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1849 len = strlen(component);
1851 memcpy(p, component, len);
1855 /* Actual loop iteration. */
1856 dir_obj = parent_obj;
1867 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1870 uint64_t dir_obj, child_dir_zapobj;
1871 dnode_phys_t child_dir_zap, dir;
1875 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1877 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1883 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1885 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1889 /* Actual loop condition #1. */
1895 memcpy(element, p, q - p);
1896 element[q - p] = '\0';
1903 child_dir_zapobj = dd->dd_child_dir_zapobj;
1904 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1907 /* Actual loop condition #2. */
1908 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1913 *objnum = dd->dd_head_dataset_obj;
1919 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1921 uint64_t dir_obj, child_dir_zapobj;
1922 dnode_phys_t child_dir_zap, dir, dataset;
1923 dsl_dataset_phys_t *ds;
1926 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1927 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1930 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1931 dir_obj = ds->ds_dir_obj;
1933 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1934 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1937 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1939 child_dir_zapobj = dd->dd_child_dir_zapobj;
1940 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1941 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1945 return (zap_list(spa, &child_dir_zap) != 0);
1949 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
1951 uint64_t dir_obj, child_dir_zapobj, zap_type;
1952 dnode_phys_t child_dir_zap, dir, dataset;
1953 dsl_dataset_phys_t *ds;
1957 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
1959 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1962 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1963 dir_obj = ds->ds_dir_obj;
1965 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
1967 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1970 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1972 child_dir_zapobj = dd->dd_child_dir_zapobj;
1973 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
1975 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1979 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
1983 zap_type = *(uint64_t *) zap_scratch;
1984 if (zap_type == ZBT_MICRO)
1985 return mzap_list(&child_dir_zap, callback);
1987 return fzap_list(spa, &child_dir_zap, callback);
1992 * Find the object set given the object number of its dataset object
1993 * and return its details in *objset
1996 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1998 dnode_phys_t dataset;
1999 dsl_dataset_phys_t *ds;
2001 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2002 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2006 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2007 if (zio_read(spa, &ds->ds_bp, objset)) {
2008 printf("ZFS: can't read object set for dataset %ju\n",
2017 * Find the object set pointed to by the BOOTFS property or the root
2018 * dataset if there is none and return its details in *objset
2021 zfs_get_root(const spa_t *spa, uint64_t *objid)
2023 dnode_phys_t dir, propdir;
2024 uint64_t props, bootfs, root;
2029 * Start with the MOS directory object.
2031 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2032 printf("ZFS: can't read MOS object directory\n");
2037 * Lookup the pool_props and see if we can find a bootfs.
2039 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2040 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2041 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2048 * Lookup the root dataset directory
2050 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2051 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2052 printf("ZFS: can't find root dsl_dir\n");
2057 * Use the information from the dataset directory's bonus buffer
2058 * to find the dataset object and from that the object set itself.
2060 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2061 *objid = dd->dd_head_dataset_obj;
2066 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2072 * Find the root object set if not explicitly provided
2074 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2075 printf("ZFS: can't find root filesystem\n");
2079 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2080 printf("ZFS: can't open root filesystem\n");
2084 mount->rootobj = rootobj;
2090 * callback function for feature name checks.
2093 check_feature(const char *name, uint64_t value)
2099 if (name[0] == '\0')
2102 for (i = 0; features_for_read[i] != NULL; i++) {
2103 if (strcmp(name, features_for_read[i]) == 0)
2106 printf("ZFS: unsupported feature: %s\n", name);
2111 * Checks whether the MOS features that are active are supported.
2114 check_mos_features(const spa_t *spa)
2117 uint64_t objnum, zap_type;
2121 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2124 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2125 sizeof (objnum), 1, &objnum)) != 0)
2128 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2131 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2134 size = dir.dn_datablkszsec * 512;
2135 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2138 zap_type = *(uint64_t *) zap_scratch;
2139 if (zap_type == ZBT_MICRO)
2140 rc = mzap_list(&dir, check_feature);
2142 rc = fzap_list(spa, &dir, check_feature);
2148 zfs_spa_init(spa_t *spa)
2153 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2154 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2157 if (spa->spa_mos.os_type != DMU_OST_META) {
2158 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2162 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2164 printf("ZFS: failed to read pool %s directory object\n",
2168 /* this is allowed to fail, older pools do not have salt */
2169 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2170 sizeof (spa->spa_cksum_salt.zcs_bytes),
2171 spa->spa_cksum_salt.zcs_bytes);
2173 rc = check_mos_features(spa);
2175 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2182 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2185 if (dn->dn_bonustype != DMU_OT_SA) {
2186 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2188 sb->st_mode = zp->zp_mode;
2189 sb->st_uid = zp->zp_uid;
2190 sb->st_gid = zp->zp_gid;
2191 sb->st_size = zp->zp_size;
2193 sa_hdr_phys_t *sahdrp;
2198 if (dn->dn_bonuslen != 0)
2199 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2201 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2202 blkptr_t *bp = &dn->dn_spill;
2205 size = BP_GET_LSIZE(bp);
2206 buf = zfs_alloc(size);
2207 error = zio_read(spa, bp, buf);
2209 zfs_free(buf, size);
2217 hdrsize = SA_HDR_SIZE(sahdrp);
2218 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2220 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2222 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2224 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2227 zfs_free(buf, size);
2234 * Lookup a file and return its dnode.
2237 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2240 uint64_t objnum, rootnum, parentnum;
2246 int symlinks_followed = 0;
2250 if (mount->objset.os_type != DMU_OST_ZFS) {
2251 printf("ZFS: unexpected object set type %ju\n",
2252 (uintmax_t)mount->objset.os_type);
2257 * Get the root directory dnode.
2259 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2263 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (rootnum), 1, &rootnum);
2267 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2280 memcpy(element, p, q - p);
2288 rc = zfs_dnode_stat(spa, &dn, &sb);
2291 if (!S_ISDIR(sb.st_mode))
2295 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2298 objnum = ZFS_DIRENT_OBJ(objnum);
2300 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2305 * Check for symlink.
2307 rc = zfs_dnode_stat(spa, &dn, &sb);
2310 if (S_ISLNK(sb.st_mode)) {
2311 if (symlinks_followed > 10)
2313 symlinks_followed++;
2316 * Read the link value and copy the tail of our
2317 * current path onto the end.
2320 strcpy(&path[sb.st_size], p);
2322 path[sb.st_size] = 0;
2324 * Second test is purely to silence bogus compiler
2325 * warning about accessing past the end of dn_bonus.
2327 if (sb.st_size + sizeof(znode_phys_t) <=
2328 dn.dn_bonuslen && sizeof(znode_phys_t) <=
2329 sizeof(dn.dn_bonus)) {
2330 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2333 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2339 * Restart with the new path, starting either at
2340 * the root or at the parent depending whether or
2341 * not the link is relative.
2348 objset_get_dnode(spa, &mount->objset, objnum, &dn);