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",
65 * List of all pools, chained through spa_link.
67 static spa_list_t zfs_pools;
69 static uint64_t zfs_crc64_table[256];
70 static const dnode_phys_t *dnode_cache_obj = 0;
71 static uint64_t dnode_cache_bn;
72 static char *dnode_cache_buf;
73 static char *zap_scratch;
74 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
76 #define TEMP_SIZE (1024 * 1024)
78 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
79 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
80 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
85 STAILQ_INIT(&zfs_vdevs);
86 STAILQ_INIT(&zfs_pools);
88 zfs_temp_buf = malloc(TEMP_SIZE);
89 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
90 zfs_temp_ptr = zfs_temp_buf;
91 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
92 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
98 zfs_alloc(size_t size)
102 if (zfs_temp_ptr + size > zfs_temp_end) {
103 printf("ZFS: out of temporary buffer space\n");
107 zfs_temp_ptr += size;
113 zfs_free(void *ptr, size_t size)
116 zfs_temp_ptr -= size;
117 if (zfs_temp_ptr != ptr) {
118 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
124 xdr_int(const unsigned char **xdr, int *ip)
126 *ip = ((*xdr)[0] << 24)
135 xdr_u_int(const unsigned char **xdr, u_int *ip)
137 *ip = ((*xdr)[0] << 24)
146 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
152 *lp = (((uint64_t) hi) << 32) | lo;
157 nvlist_find(const unsigned char *nvlist, const char *name, int type,
158 int* elementsp, void *valuep)
160 const unsigned char *p, *pair;
162 int encoded_size, decoded_size;
169 xdr_int(&p, &encoded_size);
170 xdr_int(&p, &decoded_size);
171 while (encoded_size && decoded_size) {
172 int namelen, pairtype, elements;
173 const char *pairname;
175 xdr_int(&p, &namelen);
176 pairname = (const char*) p;
177 p += roundup(namelen, 4);
178 xdr_int(&p, &pairtype);
180 if (!memcmp(name, pairname, namelen) && type == pairtype) {
181 xdr_int(&p, &elements);
183 *elementsp = elements;
184 if (type == DATA_TYPE_UINT64) {
185 xdr_uint64_t(&p, (uint64_t *) valuep);
187 } else if (type == DATA_TYPE_STRING) {
190 (*(const char**) valuep) = (const char*) p;
192 } else if (type == DATA_TYPE_NVLIST
193 || type == DATA_TYPE_NVLIST_ARRAY) {
194 (*(const unsigned char**) valuep) =
195 (const unsigned char*) p;
202 * Not the pair we are looking for, skip to the next one.
204 p = pair + encoded_size;
208 xdr_int(&p, &encoded_size);
209 xdr_int(&p, &decoded_size);
216 nvlist_check_features_for_read(const unsigned char *nvlist)
218 const unsigned char *p, *pair;
220 int encoded_size, decoded_size;
230 xdr_int(&p, &encoded_size);
231 xdr_int(&p, &decoded_size);
232 while (encoded_size && decoded_size) {
233 int namelen, pairtype;
234 const char *pairname;
239 xdr_int(&p, &namelen);
240 pairname = (const char*) p;
241 p += roundup(namelen, 4);
242 xdr_int(&p, &pairtype);
244 for (i = 0; features_for_read[i] != NULL; i++) {
245 if (!memcmp(pairname, features_for_read[i], namelen)) {
252 printf("ZFS: unsupported feature: %s\n", pairname);
256 p = pair + encoded_size;
259 xdr_int(&p, &encoded_size);
260 xdr_int(&p, &decoded_size);
267 * Return the next nvlist in an nvlist array.
269 static const unsigned char *
270 nvlist_next(const unsigned char *nvlist)
272 const unsigned char *p, *pair;
274 int encoded_size, decoded_size;
281 xdr_int(&p, &encoded_size);
282 xdr_int(&p, &decoded_size);
283 while (encoded_size && decoded_size) {
284 p = pair + encoded_size;
287 xdr_int(&p, &encoded_size);
288 xdr_int(&p, &decoded_size);
296 static const unsigned char *
297 nvlist_print(const unsigned char *nvlist, unsigned int indent)
299 static const char* typenames[] = {
310 "DATA_TYPE_BYTE_ARRAY",
311 "DATA_TYPE_INT16_ARRAY",
312 "DATA_TYPE_UINT16_ARRAY",
313 "DATA_TYPE_INT32_ARRAY",
314 "DATA_TYPE_UINT32_ARRAY",
315 "DATA_TYPE_INT64_ARRAY",
316 "DATA_TYPE_UINT64_ARRAY",
317 "DATA_TYPE_STRING_ARRAY",
320 "DATA_TYPE_NVLIST_ARRAY",
321 "DATA_TYPE_BOOLEAN_VALUE",
324 "DATA_TYPE_BOOLEAN_ARRAY",
325 "DATA_TYPE_INT8_ARRAY",
326 "DATA_TYPE_UINT8_ARRAY"
330 const unsigned char *p, *pair;
332 int encoded_size, decoded_size;
339 xdr_int(&p, &encoded_size);
340 xdr_int(&p, &decoded_size);
341 while (encoded_size && decoded_size) {
342 int namelen, pairtype, elements;
343 const char *pairname;
345 xdr_int(&p, &namelen);
346 pairname = (const char*) p;
347 p += roundup(namelen, 4);
348 xdr_int(&p, &pairtype);
350 for (i = 0; i < indent; i++)
352 printf("%s %s", typenames[pairtype], pairname);
354 xdr_int(&p, &elements);
356 case DATA_TYPE_UINT64: {
358 xdr_uint64_t(&p, &val);
359 printf(" = 0x%jx\n", (uintmax_t)val);
363 case DATA_TYPE_STRING: {
366 printf(" = \"%s\"\n", p);
370 case DATA_TYPE_NVLIST:
372 nvlist_print(p, indent + 1);
375 case DATA_TYPE_NVLIST_ARRAY:
376 for (j = 0; j < elements; j++) {
378 p = nvlist_print(p, indent + 1);
379 if (j != elements - 1) {
380 for (i = 0; i < indent; i++)
382 printf("%s %s", typenames[pairtype], pairname);
391 p = pair + encoded_size;
394 xdr_int(&p, &encoded_size);
395 xdr_int(&p, &decoded_size);
404 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
405 off_t offset, size_t size)
410 if (!vdev->v_phys_read)
414 psize = BP_GET_PSIZE(bp);
419 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
420 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
423 if (bp && zio_checksum_verify(bp, buf))
430 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
431 off_t offset, size_t bytes)
434 return (vdev_read_phys(vdev, bp, buf,
435 offset + VDEV_LABEL_START_SIZE, bytes));
440 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
441 off_t offset, size_t bytes)
447 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
448 if (kid->v_state != VDEV_STATE_HEALTHY)
450 rc = kid->v_read(kid, bp, buf, offset, bytes);
459 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
460 off_t offset, size_t bytes)
465 * Here we should have two kids:
466 * First one which is the one we are replacing and we can trust
467 * only this one to have valid data, but it might not be present.
468 * Second one is that one we are replacing with. It is most likely
469 * healthy, but we can't trust it has needed data, so we won't use it.
471 kid = STAILQ_FIRST(&vdev->v_children);
474 if (kid->v_state != VDEV_STATE_HEALTHY)
476 return (kid->v_read(kid, bp, buf, offset, bytes));
480 vdev_find(uint64_t guid)
484 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
485 if (vdev->v_guid == guid)
492 vdev_create(uint64_t guid, vdev_read_t *read)
496 vdev = malloc(sizeof(vdev_t));
497 memset(vdev, 0, sizeof(vdev_t));
498 STAILQ_INIT(&vdev->v_children);
500 vdev->v_state = VDEV_STATE_OFFLINE;
502 vdev->v_phys_read = 0;
503 vdev->v_read_priv = 0;
504 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
510 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
511 vdev_t **vdevp, int is_newer)
514 uint64_t guid, id, ashift, nparity;
518 const unsigned char *kids;
519 int nkids, i, is_new;
520 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
522 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
523 DATA_TYPE_UINT64, 0, &guid)
524 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
525 DATA_TYPE_UINT64, 0, &id)
526 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
527 DATA_TYPE_STRING, 0, &type)) {
528 printf("ZFS: can't find vdev details\n");
532 if (strcmp(type, VDEV_TYPE_MIRROR)
533 && strcmp(type, VDEV_TYPE_DISK)
535 && strcmp(type, VDEV_TYPE_FILE)
537 && strcmp(type, VDEV_TYPE_RAIDZ)
538 && strcmp(type, VDEV_TYPE_REPLACING)) {
539 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
543 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
545 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
547 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
549 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
551 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
553 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
556 vdev = vdev_find(guid);
560 if (!strcmp(type, VDEV_TYPE_MIRROR))
561 vdev = vdev_create(guid, vdev_mirror_read);
562 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
563 vdev = vdev_create(guid, vdev_raidz_read);
564 else if (!strcmp(type, VDEV_TYPE_REPLACING))
565 vdev = vdev_create(guid, vdev_replacing_read);
567 vdev = vdev_create(guid, vdev_disk_read);
570 vdev->v_top = pvdev != NULL ? pvdev : vdev;
571 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
572 DATA_TYPE_UINT64, 0, &ashift) == 0)
573 vdev->v_ashift = ashift;
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
577 DATA_TYPE_UINT64, 0, &nparity) == 0)
578 vdev->v_nparity = nparity;
581 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
582 DATA_TYPE_STRING, 0, &path) == 0) {
583 if (strncmp(path, "/dev/", 5) == 0)
585 vdev->v_name = strdup(path);
587 if (!strcmp(type, "raidz")) {
588 if (vdev->v_nparity == 1)
589 vdev->v_name = "raidz1";
590 else if (vdev->v_nparity == 2)
591 vdev->v_name = "raidz2";
592 else if (vdev->v_nparity == 3)
593 vdev->v_name = "raidz3";
595 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
599 vdev->v_name = strdup(type);
606 if (is_new || is_newer) {
608 * This is either new vdev or we've already seen this vdev,
609 * but from an older vdev label, so let's refresh its state
610 * from the newer label.
613 vdev->v_state = VDEV_STATE_OFFLINE;
615 vdev->v_state = VDEV_STATE_REMOVED;
617 vdev->v_state = VDEV_STATE_FAULTED;
618 else if (is_degraded)
619 vdev->v_state = VDEV_STATE_DEGRADED;
620 else if (isnt_present)
621 vdev->v_state = VDEV_STATE_CANT_OPEN;
624 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
625 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
627 * Its ok if we don't have any kids.
630 vdev->v_nchildren = nkids;
631 for (i = 0; i < nkids; i++) {
632 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
636 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
638 kids = nvlist_next(kids);
641 vdev->v_nchildren = 0;
650 vdev_set_state(vdev_t *vdev)
657 * A mirror or raidz is healthy if all its kids are healthy. A
658 * mirror is degraded if any of its kids is healthy; a raidz
659 * is degraded if at most nparity kids are offline.
661 if (STAILQ_FIRST(&vdev->v_children)) {
664 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
665 if (kid->v_state == VDEV_STATE_HEALTHY)
671 vdev->v_state = VDEV_STATE_HEALTHY;
673 if (vdev->v_read == vdev_mirror_read) {
675 vdev->v_state = VDEV_STATE_DEGRADED;
677 vdev->v_state = VDEV_STATE_OFFLINE;
679 } else if (vdev->v_read == vdev_raidz_read) {
680 if (bad_kids > vdev->v_nparity) {
681 vdev->v_state = VDEV_STATE_OFFLINE;
683 vdev->v_state = VDEV_STATE_DEGRADED;
691 spa_find_by_guid(uint64_t guid)
695 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
696 if (spa->spa_guid == guid)
703 spa_find_by_name(const char *name)
707 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
708 if (!strcmp(spa->spa_name, name))
716 spa_get_primary(void)
719 return (STAILQ_FIRST(&zfs_pools));
723 spa_get_primary_vdev(const spa_t *spa)
729 spa = spa_get_primary();
732 vdev = STAILQ_FIRST(&spa->spa_vdevs);
735 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
736 kid = STAILQ_FIRST(&vdev->v_children))
743 spa_create(uint64_t guid)
747 spa = malloc(sizeof(spa_t));
748 memset(spa, 0, sizeof(spa_t));
749 STAILQ_INIT(&spa->spa_vdevs);
750 spa->spa_guid = guid;
751 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
757 state_name(vdev_state_t state)
759 static const char* names[] = {
774 #define pager_printf printf
779 pager_printf(const char *fmt, ...)
785 vsprintf(line, fmt, args);
792 #define STATUS_FORMAT " %s %s\n"
795 print_state(int indent, const char *name, vdev_state_t state)
801 for (i = 0; i < indent; i++)
804 pager_printf(STATUS_FORMAT, buf, state_name(state));
809 vdev_status(vdev_t *vdev, int indent)
812 print_state(indent, vdev->v_name, vdev->v_state);
814 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
815 vdev_status(kid, indent + 1);
820 spa_status(spa_t *spa)
822 static char bootfs[ZFS_MAXNAMELEN];
825 int good_kids, bad_kids, degraded_kids;
828 pager_printf(" pool: %s\n", spa->spa_name);
829 if (zfs_get_root(spa, &rootid) == 0 &&
830 zfs_rlookup(spa, rootid, bootfs) == 0) {
831 if (bootfs[0] == '\0')
832 pager_printf("bootfs: %s\n", spa->spa_name);
834 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
836 pager_printf("config:\n\n");
837 pager_printf(STATUS_FORMAT, "NAME", "STATE");
842 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
843 if (vdev->v_state == VDEV_STATE_HEALTHY)
845 else if (vdev->v_state == VDEV_STATE_DEGRADED)
851 state = VDEV_STATE_CLOSED;
852 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
853 state = VDEV_STATE_HEALTHY;
854 else if ((good_kids + degraded_kids) > 0)
855 state = VDEV_STATE_DEGRADED;
857 print_state(0, spa->spa_name, state);
858 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
859 vdev_status(vdev, 1);
869 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
878 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
881 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
883 vdev_t *vdev, *top_vdev, *pool_vdev;
886 const unsigned char *nvlist;
889 uint64_t pool_txg, pool_guid;
891 const char *pool_name;
892 const unsigned char *vdevs;
893 const unsigned char *features;
896 const struct uberblock *up;
899 * Load the vdev label and figure out which
900 * uberblock is most current.
902 memset(&vtmp, 0, sizeof(vtmp));
903 vtmp.v_phys_read = read;
904 vtmp.v_read_priv = read_priv;
905 off = offsetof(vdev_label_t, vl_vdev_phys);
907 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
908 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
909 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
910 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
911 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
912 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
913 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
916 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
920 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
922 if (nvlist_find(nvlist,
923 ZPOOL_CONFIG_VERSION,
924 DATA_TYPE_UINT64, 0, &val)) {
928 if (!SPA_VERSION_IS_SUPPORTED(val)) {
929 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
930 (unsigned) val, (unsigned) SPA_VERSION);
934 /* Check ZFS features for read */
935 if (nvlist_find(nvlist,
936 ZPOOL_CONFIG_FEATURES_FOR_READ,
937 DATA_TYPE_NVLIST, 0, &features) == 0
938 && nvlist_check_features_for_read(features) != 0)
941 if (nvlist_find(nvlist,
942 ZPOOL_CONFIG_POOL_STATE,
943 DATA_TYPE_UINT64, 0, &val)) {
947 if (val == POOL_STATE_DESTROYED) {
948 /* We don't boot only from destroyed pools. */
952 if (nvlist_find(nvlist,
953 ZPOOL_CONFIG_POOL_TXG,
954 DATA_TYPE_UINT64, 0, &pool_txg)
955 || nvlist_find(nvlist,
956 ZPOOL_CONFIG_POOL_GUID,
957 DATA_TYPE_UINT64, 0, &pool_guid)
958 || nvlist_find(nvlist,
959 ZPOOL_CONFIG_POOL_NAME,
960 DATA_TYPE_STRING, 0, &pool_name)) {
962 * Cache and spare devices end up here - just ignore
965 /*printf("ZFS: can't find pool details\n");*/
970 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
976 * Create the pool if this is the first time we've seen it.
978 spa = spa_find_by_guid(pool_guid);
980 spa = spa_create(pool_guid);
981 spa->spa_name = strdup(pool_name);
983 if (pool_txg > spa->spa_txg) {
984 spa->spa_txg = pool_txg;
990 * Get the vdev tree and create our in-core copy of it.
991 * If we already have a vdev with this guid, this must
992 * be some kind of alias (overlapping slices, dangerously dedicated
995 if (nvlist_find(nvlist,
997 DATA_TYPE_UINT64, 0, &guid)) {
1000 vdev = vdev_find(guid);
1001 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1004 if (nvlist_find(nvlist,
1005 ZPOOL_CONFIG_VDEV_TREE,
1006 DATA_TYPE_NVLIST, 0, &vdevs)) {
1010 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1015 * Add the toplevel vdev to the pool if its not already there.
1017 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1018 if (top_vdev == pool_vdev)
1020 if (!pool_vdev && top_vdev)
1021 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1024 * We should already have created an incomplete vdev for this
1025 * vdev. Find it and initialise it with our read proc.
1027 vdev = vdev_find(guid);
1029 vdev->v_phys_read = read;
1030 vdev->v_read_priv = read_priv;
1031 vdev->v_state = VDEV_STATE_HEALTHY;
1033 printf("ZFS: inconsistent nvlist contents\n");
1038 * Re-evaluate top-level vdev state.
1040 vdev_set_state(top_vdev);
1043 * Ok, we are happy with the pool so far. Lets find
1044 * the best uberblock and then we can actually access
1045 * the contents of the pool.
1047 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1048 up = (const struct uberblock *)upbuf;
1050 i < VDEV_UBERBLOCK_COUNT(vdev);
1052 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1054 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1055 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1056 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1057 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1058 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1059 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1061 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1064 if (up->ub_magic != UBERBLOCK_MAGIC)
1066 if (up->ub_txg < spa->spa_txg)
1068 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1069 spa->spa_uberblock = *up;
1070 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1071 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1072 spa->spa_uberblock = *up;
1075 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1087 for (v = 0; v < 32; v++)
1094 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1097 zio_gbh_phys_t zio_gb;
1101 /* Artificial BP for gang block header. */
1103 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1104 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1105 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1106 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1107 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1108 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1110 /* Read gang header block using the artificial BP. */
1111 if (zio_read(spa, &gbh_bp, &zio_gb))
1115 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1116 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1118 if (BP_IS_HOLE(gbp))
1120 if (zio_read(spa, gbp, pbuf))
1122 pbuf += BP_GET_PSIZE(gbp);
1125 if (zio_checksum_verify(bp, buf))
1131 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1133 int cpfunc = BP_GET_COMPRESS(bp);
1134 uint64_t align, size;
1139 * Process data embedded in block pointer
1141 if (BP_IS_EMBEDDED(bp)) {
1142 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1144 size = BPE_GET_PSIZE(bp);
1145 ASSERT(size <= BPE_PAYLOAD_SIZE);
1147 if (cpfunc != ZIO_COMPRESS_OFF)
1148 pbuf = zfs_alloc(size);
1152 decode_embedded_bp_compressed(bp, pbuf);
1155 if (cpfunc != ZIO_COMPRESS_OFF) {
1156 error = zio_decompress_data(cpfunc, pbuf,
1157 size, buf, BP_GET_LSIZE(bp));
1158 zfs_free(pbuf, size);
1161 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1168 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1169 const dva_t *dva = &bp->blk_dva[i];
1174 if (!dva->dva_word[0] && !dva->dva_word[1])
1177 vdevid = DVA_GET_VDEV(dva);
1178 offset = DVA_GET_OFFSET(dva);
1179 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1180 if (vdev->v_id == vdevid)
1183 if (!vdev || !vdev->v_read)
1186 size = BP_GET_PSIZE(bp);
1187 if (vdev->v_read == vdev_raidz_read) {
1188 align = 1ULL << vdev->v_top->v_ashift;
1189 if (P2PHASE(size, align) != 0)
1190 size = P2ROUNDUP(size, align);
1192 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1193 pbuf = zfs_alloc(size);
1197 if (DVA_GET_GANG(dva))
1198 error = zio_read_gang(spa, bp, pbuf);
1200 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1202 if (cpfunc != ZIO_COMPRESS_OFF)
1203 error = zio_decompress_data(cpfunc, pbuf,
1204 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1205 else if (size != BP_GET_PSIZE(bp))
1206 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1209 zfs_free(pbuf, size);
1214 printf("ZFS: i/o error - all block copies unavailable\n");
1219 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1221 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1222 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1223 int nlevels = dnode->dn_nlevels;
1226 if (bsize > SPA_MAXBLOCKSIZE) {
1227 printf("ZFS: I/O error - blocks larger than 128K are not supported\n");
1232 * Note: bsize may not be a power of two here so we need to do an
1233 * actual divide rather than a bitshift.
1235 while (buflen > 0) {
1236 uint64_t bn = offset / bsize;
1237 int boff = offset % bsize;
1239 const blkptr_t *indbp;
1242 if (bn > dnode->dn_maxblkid)
1245 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1248 indbp = dnode->dn_blkptr;
1249 for (i = 0; i < nlevels; i++) {
1251 * Copy the bp from the indirect array so that
1252 * we can re-use the scratch buffer for multi-level
1255 ibn = bn >> ((nlevels - i - 1) * ibshift);
1256 ibn &= ((1 << ibshift) - 1);
1258 if (BP_IS_HOLE(&bp)) {
1259 memset(dnode_cache_buf, 0, bsize);
1262 rc = zio_read(spa, &bp, dnode_cache_buf);
1265 indbp = (const blkptr_t *) dnode_cache_buf;
1267 dnode_cache_obj = dnode;
1268 dnode_cache_bn = bn;
1272 * The buffer contains our data block. Copy what we
1273 * need from it and loop.
1276 if (i > buflen) i = buflen;
1277 memcpy(buf, &dnode_cache_buf[boff], i);
1278 buf = ((char*) buf) + i;
1287 * Lookup a value in a microzap directory. Assumes that the zap
1288 * scratch buffer contains the directory contents.
1291 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1293 const mzap_phys_t *mz;
1294 const mzap_ent_phys_t *mze;
1299 * Microzap objects use exactly one block. Read the whole
1302 size = dnode->dn_datablkszsec * 512;
1304 mz = (const mzap_phys_t *) zap_scratch;
1305 chunks = size / MZAP_ENT_LEN - 1;
1307 for (i = 0; i < chunks; i++) {
1308 mze = &mz->mz_chunk[i];
1309 if (!strcmp(mze->mze_name, name)) {
1310 *value = mze->mze_value;
1319 * Compare a name with a zap leaf entry. Return non-zero if the name
1323 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1326 const zap_leaf_chunk_t *nc;
1329 namelen = zc->l_entry.le_name_numints;
1331 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1333 while (namelen > 0) {
1336 if (len > ZAP_LEAF_ARRAY_BYTES)
1337 len = ZAP_LEAF_ARRAY_BYTES;
1338 if (memcmp(p, nc->l_array.la_array, len))
1342 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1349 * Extract a uint64_t value from a zap leaf entry.
1352 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1354 const zap_leaf_chunk_t *vc;
1359 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1360 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1361 value = (value << 8) | p[i];
1368 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1369 * buffer contains the directory header.
1372 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1374 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1375 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1381 if (zh.zap_magic != ZAP_MAGIC)
1384 z.zap_block_shift = ilog2(bsize);
1385 z.zap_phys = (zap_phys_t *) zap_scratch;
1388 * Figure out where the pointer table is and read it in if necessary.
1390 if (zh.zap_ptrtbl.zt_blk) {
1391 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1392 zap_scratch, bsize);
1395 ptrtbl = (uint64_t *) zap_scratch;
1397 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1400 hash = zap_hash(zh.zap_salt, name);
1403 zl.l_bs = z.zap_block_shift;
1405 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1406 zap_leaf_chunk_t *zc;
1408 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1412 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1415 * Make sure this chunk matches our hash.
1417 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1418 && zl.l_phys->l_hdr.lh_prefix
1419 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1423 * Hash within the chunk to find our entry.
1425 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1426 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1427 h = zl.l_phys->l_hash[h];
1430 zc = &ZAP_LEAF_CHUNK(&zl, h);
1431 while (zc->l_entry.le_hash != hash) {
1432 if (zc->l_entry.le_next == 0xffff) {
1436 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1438 if (fzap_name_equal(&zl, zc, name)) {
1439 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1441 *value = fzap_leaf_value(&zl, zc);
1449 * Lookup a name in a zap object and return its value as a uint64_t.
1452 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1456 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1458 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1462 zap_type = *(uint64_t *) zap_scratch;
1463 if (zap_type == ZBT_MICRO)
1464 return mzap_lookup(dnode, name, value);
1465 else if (zap_type == ZBT_HEADER)
1466 return fzap_lookup(spa, dnode, name, value);
1467 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1472 * List a microzap directory. Assumes that the zap scratch buffer contains
1473 * the directory contents.
1476 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *))
1478 const mzap_phys_t *mz;
1479 const mzap_ent_phys_t *mze;
1484 * Microzap objects use exactly one block. Read the whole
1487 size = dnode->dn_datablkszsec * 512;
1488 mz = (const mzap_phys_t *) zap_scratch;
1489 chunks = size / MZAP_ENT_LEN - 1;
1491 for (i = 0; i < chunks; i++) {
1492 mze = &mz->mz_chunk[i];
1493 if (mze->mze_name[0])
1494 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1495 callback(mze->mze_name);
1502 * List a fatzap directory. Assumes that the zap scratch buffer contains
1503 * the directory header.
1506 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *))
1508 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1509 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1513 if (zh.zap_magic != ZAP_MAGIC)
1516 z.zap_block_shift = ilog2(bsize);
1517 z.zap_phys = (zap_phys_t *) zap_scratch;
1520 * This assumes that the leaf blocks start at block 1. The
1521 * documentation isn't exactly clear on this.
1524 zl.l_bs = z.zap_block_shift;
1525 for (i = 0; i < zh.zap_num_leafs; i++) {
1526 off_t off = (i + 1) << zl.l_bs;
1530 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1533 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1535 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1536 zap_leaf_chunk_t *zc, *nc;
1539 zc = &ZAP_LEAF_CHUNK(&zl, j);
1540 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1542 namelen = zc->l_entry.le_name_numints;
1543 if (namelen > sizeof(name))
1544 namelen = sizeof(name);
1547 * Paste the name back together.
1549 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1551 while (namelen > 0) {
1554 if (len > ZAP_LEAF_ARRAY_BYTES)
1555 len = ZAP_LEAF_ARRAY_BYTES;
1556 memcpy(p, nc->l_array.la_array, len);
1559 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1563 * Assume the first eight bytes of the value are
1566 value = fzap_leaf_value(&zl, zc);
1568 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1569 callback((const char *)name);
1576 static int zfs_printf(const char *name)
1579 printf("%s\n", name);
1585 * List a zap directory.
1588 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1591 size_t size = dnode->dn_datablkszsec * 512;
1593 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1596 zap_type = *(uint64_t *) zap_scratch;
1597 if (zap_type == ZBT_MICRO)
1598 return mzap_list(dnode, zfs_printf);
1600 return fzap_list(spa, dnode, zfs_printf);
1604 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1608 offset = objnum * sizeof(dnode_phys_t);
1609 return dnode_read(spa, &os->os_meta_dnode, offset,
1610 dnode, sizeof(dnode_phys_t));
1614 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1616 const mzap_phys_t *mz;
1617 const mzap_ent_phys_t *mze;
1622 * Microzap objects use exactly one block. Read the whole
1625 size = dnode->dn_datablkszsec * 512;
1627 mz = (const mzap_phys_t *) zap_scratch;
1628 chunks = size / MZAP_ENT_LEN - 1;
1630 for (i = 0; i < chunks; i++) {
1631 mze = &mz->mz_chunk[i];
1632 if (value == mze->mze_value) {
1633 strcpy(name, mze->mze_name);
1642 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1645 const zap_leaf_chunk_t *nc;
1648 namelen = zc->l_entry.le_name_numints;
1650 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1652 while (namelen > 0) {
1655 if (len > ZAP_LEAF_ARRAY_BYTES)
1656 len = ZAP_LEAF_ARRAY_BYTES;
1657 memcpy(p, nc->l_array.la_array, len);
1660 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1667 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1669 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1670 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1674 if (zh.zap_magic != ZAP_MAGIC)
1677 z.zap_block_shift = ilog2(bsize);
1678 z.zap_phys = (zap_phys_t *) zap_scratch;
1681 * This assumes that the leaf blocks start at block 1. The
1682 * documentation isn't exactly clear on this.
1685 zl.l_bs = z.zap_block_shift;
1686 for (i = 0; i < zh.zap_num_leafs; i++) {
1687 off_t off = (i + 1) << zl.l_bs;
1689 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1692 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1694 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1695 zap_leaf_chunk_t *zc;
1697 zc = &ZAP_LEAF_CHUNK(&zl, j);
1698 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1700 if (zc->l_entry.le_value_intlen != 8 ||
1701 zc->l_entry.le_value_numints != 1)
1704 if (fzap_leaf_value(&zl, zc) == value) {
1705 fzap_name_copy(&zl, zc, name);
1715 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1719 size_t size = dnode->dn_datablkszsec * 512;
1721 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1725 zap_type = *(uint64_t *) zap_scratch;
1726 if (zap_type == ZBT_MICRO)
1727 return mzap_rlookup(spa, dnode, name, value);
1729 return fzap_rlookup(spa, dnode, name, value);
1733 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1736 char component[256];
1737 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1738 dnode_phys_t child_dir_zap, dataset, dir, parent;
1740 dsl_dataset_phys_t *ds;
1744 p = &name[sizeof(name) - 1];
1747 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1748 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1751 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1752 dir_obj = ds->ds_dir_obj;
1755 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1757 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1759 /* Actual loop condition. */
1760 parent_obj = dd->dd_parent_obj;
1761 if (parent_obj == 0)
1764 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1766 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1767 child_dir_zapobj = dd->dd_child_dir_zapobj;
1768 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1770 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1773 len = strlen(component);
1775 memcpy(p, component, len);
1779 /* Actual loop iteration. */
1780 dir_obj = parent_obj;
1791 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1794 uint64_t dir_obj, child_dir_zapobj;
1795 dnode_phys_t child_dir_zap, dir;
1799 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1801 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1806 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1808 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1812 /* Actual loop condition #1. */
1818 memcpy(element, p, q - p);
1819 element[q - p] = '\0';
1826 child_dir_zapobj = dd->dd_child_dir_zapobj;
1827 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1830 /* Actual loop condition #2. */
1831 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1835 *objnum = dd->dd_head_dataset_obj;
1841 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1843 uint64_t dir_obj, child_dir_zapobj;
1844 dnode_phys_t child_dir_zap, dir, dataset;
1845 dsl_dataset_phys_t *ds;
1848 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1849 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1852 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1853 dir_obj = ds->ds_dir_obj;
1855 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1856 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1859 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1861 child_dir_zapobj = dd->dd_child_dir_zapobj;
1862 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1863 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1867 return (zap_list(spa, &child_dir_zap) != 0);
1871 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *name))
1873 uint64_t dir_obj, child_dir_zapobj, zap_type;
1874 dnode_phys_t child_dir_zap, dir, dataset;
1875 dsl_dataset_phys_t *ds;
1879 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
1881 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1884 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1885 dir_obj = ds->ds_dir_obj;
1887 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
1889 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1892 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1894 child_dir_zapobj = dd->dd_child_dir_zapobj;
1895 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
1897 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1901 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
1905 zap_type = *(uint64_t *) zap_scratch;
1906 if (zap_type == ZBT_MICRO)
1907 return mzap_list(&child_dir_zap, callback);
1909 return fzap_list(spa, &child_dir_zap, callback);
1914 * Find the object set given the object number of its dataset object
1915 * and return its details in *objset
1918 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1920 dnode_phys_t dataset;
1921 dsl_dataset_phys_t *ds;
1923 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1924 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1928 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1929 if (zio_read(spa, &ds->ds_bp, objset)) {
1930 printf("ZFS: can't read object set for dataset %ju\n",
1939 * Find the object set pointed to by the BOOTFS property or the root
1940 * dataset if there is none and return its details in *objset
1943 zfs_get_root(const spa_t *spa, uint64_t *objid)
1945 dnode_phys_t dir, propdir;
1946 uint64_t props, bootfs, root;
1951 * Start with the MOS directory object.
1953 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1954 printf("ZFS: can't read MOS object directory\n");
1959 * Lookup the pool_props and see if we can find a bootfs.
1961 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1962 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1963 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1970 * Lookup the root dataset directory
1972 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1973 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1974 printf("ZFS: can't find root dsl_dir\n");
1979 * Use the information from the dataset directory's bonus buffer
1980 * to find the dataset object and from that the object set itself.
1982 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1983 *objid = dd->dd_head_dataset_obj;
1988 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1994 * Find the root object set if not explicitly provided
1996 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1997 printf("ZFS: can't find root filesystem\n");
2001 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2002 printf("ZFS: can't open root filesystem\n");
2006 mount->rootobj = rootobj;
2012 zfs_spa_init(spa_t *spa)
2015 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2016 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2019 if (spa->spa_mos.os_type != DMU_OST_META) {
2020 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2027 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2030 if (dn->dn_bonustype != DMU_OT_SA) {
2031 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2033 sb->st_mode = zp->zp_mode;
2034 sb->st_uid = zp->zp_uid;
2035 sb->st_gid = zp->zp_gid;
2036 sb->st_size = zp->zp_size;
2038 sa_hdr_phys_t *sahdrp;
2043 if (dn->dn_bonuslen != 0)
2044 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2046 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2047 blkptr_t *bp = &dn->dn_spill;
2050 size = BP_GET_LSIZE(bp);
2051 buf = zfs_alloc(size);
2052 error = zio_read(spa, bp, buf);
2054 zfs_free(buf, size);
2062 hdrsize = SA_HDR_SIZE(sahdrp);
2063 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2065 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2067 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2069 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2072 zfs_free(buf, size);
2079 * Lookup a file and return its dnode.
2082 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2085 uint64_t objnum, rootnum, parentnum;
2091 int symlinks_followed = 0;
2095 if (mount->objset.os_type != DMU_OST_ZFS) {
2096 printf("ZFS: unexpected object set type %ju\n",
2097 (uintmax_t)mount->objset.os_type);
2102 * Get the root directory dnode.
2104 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2108 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
2112 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2125 memcpy(element, p, q - p);
2133 rc = zfs_dnode_stat(spa, &dn, &sb);
2136 if (!S_ISDIR(sb.st_mode))
2140 rc = zap_lookup(spa, &dn, element, &objnum);
2143 objnum = ZFS_DIRENT_OBJ(objnum);
2145 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2150 * Check for symlink.
2152 rc = zfs_dnode_stat(spa, &dn, &sb);
2155 if (S_ISLNK(sb.st_mode)) {
2156 if (symlinks_followed > 10)
2158 symlinks_followed++;
2161 * Read the link value and copy the tail of our
2162 * current path onto the end.
2165 strcpy(&path[sb.st_size], p);
2167 path[sb.st_size] = 0;
2169 * Second test is purely to silence bogus compiler
2170 * warning about accessing past the end of dn_bonus.
2172 if (sb.st_size + sizeof(znode_phys_t) <=
2173 dn.dn_bonuslen && sizeof(znode_phys_t) <=
2174 sizeof(dn.dn_bonus)) {
2175 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2178 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2184 * Restart with the new path, starting either at
2185 * the root or at the parent depending whether or
2186 * not the link is relative.
2193 objset_get_dnode(spa, &mount->objset, objnum, &dn);