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 const dnode_phys_t *dnode_cache_obj = 0;
70 static uint64_t dnode_cache_bn;
71 static char *dnode_cache_buf;
72 static char *zap_scratch;
73 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
75 #define TEMP_SIZE (1024 * 1024)
77 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
78 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
79 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
84 STAILQ_INIT(&zfs_vdevs);
85 STAILQ_INIT(&zfs_pools);
87 zfs_temp_buf = malloc(TEMP_SIZE);
88 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
89 zfs_temp_ptr = zfs_temp_buf;
90 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
91 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
97 zfs_alloc(size_t size)
101 if (zfs_temp_ptr + size > zfs_temp_end) {
102 printf("ZFS: out of temporary buffer space\n");
106 zfs_temp_ptr += size;
112 zfs_free(void *ptr, size_t size)
115 zfs_temp_ptr -= size;
116 if (zfs_temp_ptr != ptr) {
117 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
123 xdr_int(const unsigned char **xdr, int *ip)
125 *ip = ((*xdr)[0] << 24)
134 xdr_u_int(const unsigned char **xdr, u_int *ip)
136 *ip = ((*xdr)[0] << 24)
145 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
151 *lp = (((uint64_t) hi) << 32) | lo;
156 nvlist_find(const unsigned char *nvlist, const char *name, int type,
157 int* elementsp, void *valuep)
159 const unsigned char *p, *pair;
161 int encoded_size, decoded_size;
168 xdr_int(&p, &encoded_size);
169 xdr_int(&p, &decoded_size);
170 while (encoded_size && decoded_size) {
171 int namelen, pairtype, elements;
172 const char *pairname;
174 xdr_int(&p, &namelen);
175 pairname = (const char*) p;
176 p += roundup(namelen, 4);
177 xdr_int(&p, &pairtype);
179 if (!memcmp(name, pairname, namelen) && type == pairtype) {
180 xdr_int(&p, &elements);
182 *elementsp = elements;
183 if (type == DATA_TYPE_UINT64) {
184 xdr_uint64_t(&p, (uint64_t *) valuep);
186 } else if (type == DATA_TYPE_STRING) {
189 (*(const char**) valuep) = (const char*) p;
191 } else if (type == DATA_TYPE_NVLIST
192 || type == DATA_TYPE_NVLIST_ARRAY) {
193 (*(const unsigned char**) valuep) =
194 (const unsigned char*) p;
201 * Not the pair we are looking for, skip to the next one.
203 p = pair + encoded_size;
207 xdr_int(&p, &encoded_size);
208 xdr_int(&p, &decoded_size);
215 nvlist_check_features_for_read(const unsigned char *nvlist)
217 const unsigned char *p, *pair;
219 int encoded_size, decoded_size;
229 xdr_int(&p, &encoded_size);
230 xdr_int(&p, &decoded_size);
231 while (encoded_size && decoded_size) {
232 int namelen, pairtype;
233 const char *pairname;
238 xdr_int(&p, &namelen);
239 pairname = (const char*) p;
240 p += roundup(namelen, 4);
241 xdr_int(&p, &pairtype);
243 for (i = 0; features_for_read[i] != NULL; i++) {
244 if (!memcmp(pairname, features_for_read[i], namelen)) {
251 printf("ZFS: unsupported feature: %s\n", pairname);
255 p = pair + encoded_size;
258 xdr_int(&p, &encoded_size);
259 xdr_int(&p, &decoded_size);
266 * Return the next nvlist in an nvlist array.
268 static const unsigned char *
269 nvlist_next(const unsigned char *nvlist)
271 const unsigned char *p, *pair;
273 int encoded_size, decoded_size;
280 xdr_int(&p, &encoded_size);
281 xdr_int(&p, &decoded_size);
282 while (encoded_size && decoded_size) {
283 p = pair + encoded_size;
286 xdr_int(&p, &encoded_size);
287 xdr_int(&p, &decoded_size);
295 static const unsigned char *
296 nvlist_print(const unsigned char *nvlist, unsigned int indent)
298 static const char* typenames[] = {
309 "DATA_TYPE_BYTE_ARRAY",
310 "DATA_TYPE_INT16_ARRAY",
311 "DATA_TYPE_UINT16_ARRAY",
312 "DATA_TYPE_INT32_ARRAY",
313 "DATA_TYPE_UINT32_ARRAY",
314 "DATA_TYPE_INT64_ARRAY",
315 "DATA_TYPE_UINT64_ARRAY",
316 "DATA_TYPE_STRING_ARRAY",
319 "DATA_TYPE_NVLIST_ARRAY",
320 "DATA_TYPE_BOOLEAN_VALUE",
323 "DATA_TYPE_BOOLEAN_ARRAY",
324 "DATA_TYPE_INT8_ARRAY",
325 "DATA_TYPE_UINT8_ARRAY"
329 const unsigned char *p, *pair;
331 int encoded_size, decoded_size;
338 xdr_int(&p, &encoded_size);
339 xdr_int(&p, &decoded_size);
340 while (encoded_size && decoded_size) {
341 int namelen, pairtype, elements;
342 const char *pairname;
344 xdr_int(&p, &namelen);
345 pairname = (const char*) p;
346 p += roundup(namelen, 4);
347 xdr_int(&p, &pairtype);
349 for (i = 0; i < indent; i++)
351 printf("%s %s", typenames[pairtype], pairname);
353 xdr_int(&p, &elements);
355 case DATA_TYPE_UINT64: {
357 xdr_uint64_t(&p, &val);
358 printf(" = 0x%jx\n", (uintmax_t)val);
362 case DATA_TYPE_STRING: {
365 printf(" = \"%s\"\n", p);
369 case DATA_TYPE_NVLIST:
371 nvlist_print(p, indent + 1);
374 case DATA_TYPE_NVLIST_ARRAY:
375 for (j = 0; j < elements; j++) {
377 p = nvlist_print(p, indent + 1);
378 if (j != elements - 1) {
379 for (i = 0; i < indent; i++)
381 printf("%s %s", typenames[pairtype], pairname);
390 p = pair + encoded_size;
393 xdr_int(&p, &encoded_size);
394 xdr_int(&p, &decoded_size);
403 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
404 off_t offset, size_t size)
409 if (!vdev->v_phys_read)
413 psize = BP_GET_PSIZE(bp);
418 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
419 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
422 if (bp && zio_checksum_verify(bp, buf))
429 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
430 off_t offset, size_t bytes)
433 return (vdev_read_phys(vdev, bp, buf,
434 offset + VDEV_LABEL_START_SIZE, bytes));
439 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
440 off_t offset, size_t bytes)
446 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
447 if (kid->v_state != VDEV_STATE_HEALTHY)
449 rc = kid->v_read(kid, bp, buf, offset, bytes);
458 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
459 off_t offset, size_t bytes)
464 * Here we should have two kids:
465 * First one which is the one we are replacing and we can trust
466 * only this one to have valid data, but it might not be present.
467 * Second one is that one we are replacing with. It is most likely
468 * healthy, but we can't trust it has needed data, so we won't use it.
470 kid = STAILQ_FIRST(&vdev->v_children);
473 if (kid->v_state != VDEV_STATE_HEALTHY)
475 return (kid->v_read(kid, bp, buf, offset, bytes));
479 vdev_find(uint64_t guid)
483 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
484 if (vdev->v_guid == guid)
491 vdev_create(uint64_t guid, vdev_read_t *_read)
495 vdev = malloc(sizeof(vdev_t));
496 memset(vdev, 0, sizeof(vdev_t));
497 STAILQ_INIT(&vdev->v_children);
499 vdev->v_state = VDEV_STATE_OFFLINE;
500 vdev->v_read = _read;
501 vdev->v_phys_read = 0;
502 vdev->v_read_priv = 0;
503 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
509 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
510 vdev_t **vdevp, int is_newer)
513 uint64_t guid, id, ashift, nparity;
517 const unsigned char *kids;
518 int nkids, i, is_new;
519 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
521 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
522 DATA_TYPE_UINT64, 0, &guid)
523 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
524 DATA_TYPE_UINT64, 0, &id)
525 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
526 DATA_TYPE_STRING, 0, &type)) {
527 printf("ZFS: can't find vdev details\n");
531 if (strcmp(type, VDEV_TYPE_MIRROR)
532 && strcmp(type, VDEV_TYPE_DISK)
534 && strcmp(type, VDEV_TYPE_FILE)
536 && strcmp(type, VDEV_TYPE_RAIDZ)
537 && strcmp(type, VDEV_TYPE_REPLACING)) {
538 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
542 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
544 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
546 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
548 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
550 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
552 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
555 vdev = vdev_find(guid);
559 if (!strcmp(type, VDEV_TYPE_MIRROR))
560 vdev = vdev_create(guid, vdev_mirror_read);
561 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
562 vdev = vdev_create(guid, vdev_raidz_read);
563 else if (!strcmp(type, VDEV_TYPE_REPLACING))
564 vdev = vdev_create(guid, vdev_replacing_read);
566 vdev = vdev_create(guid, vdev_disk_read);
569 vdev->v_top = pvdev != NULL ? pvdev : vdev;
570 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
571 DATA_TYPE_UINT64, 0, &ashift) == 0)
572 vdev->v_ashift = ashift;
575 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
576 DATA_TYPE_UINT64, 0, &nparity) == 0)
577 vdev->v_nparity = nparity;
580 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
581 DATA_TYPE_STRING, 0, &path) == 0) {
582 if (strncmp(path, "/dev/", 5) == 0)
584 vdev->v_name = strdup(path);
586 if (!strcmp(type, "raidz")) {
587 if (vdev->v_nparity == 1)
588 vdev->v_name = "raidz1";
589 else if (vdev->v_nparity == 2)
590 vdev->v_name = "raidz2";
591 else if (vdev->v_nparity == 3)
592 vdev->v_name = "raidz3";
594 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
598 vdev->v_name = strdup(type);
605 if (is_new || is_newer) {
607 * This is either new vdev or we've already seen this vdev,
608 * but from an older vdev label, so let's refresh its state
609 * from the newer label.
612 vdev->v_state = VDEV_STATE_OFFLINE;
614 vdev->v_state = VDEV_STATE_REMOVED;
616 vdev->v_state = VDEV_STATE_FAULTED;
617 else if (is_degraded)
618 vdev->v_state = VDEV_STATE_DEGRADED;
619 else if (isnt_present)
620 vdev->v_state = VDEV_STATE_CANT_OPEN;
623 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
624 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
626 * Its ok if we don't have any kids.
629 vdev->v_nchildren = nkids;
630 for (i = 0; i < nkids; i++) {
631 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
635 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
637 kids = nvlist_next(kids);
640 vdev->v_nchildren = 0;
649 vdev_set_state(vdev_t *vdev)
656 * A mirror or raidz is healthy if all its kids are healthy. A
657 * mirror is degraded if any of its kids is healthy; a raidz
658 * is degraded if at most nparity kids are offline.
660 if (STAILQ_FIRST(&vdev->v_children)) {
663 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
664 if (kid->v_state == VDEV_STATE_HEALTHY)
670 vdev->v_state = VDEV_STATE_HEALTHY;
672 if (vdev->v_read == vdev_mirror_read) {
674 vdev->v_state = VDEV_STATE_DEGRADED;
676 vdev->v_state = VDEV_STATE_OFFLINE;
678 } else if (vdev->v_read == vdev_raidz_read) {
679 if (bad_kids > vdev->v_nparity) {
680 vdev->v_state = VDEV_STATE_OFFLINE;
682 vdev->v_state = VDEV_STATE_DEGRADED;
690 spa_find_by_guid(uint64_t guid)
694 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
695 if (spa->spa_guid == guid)
702 spa_find_by_name(const char *name)
706 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
707 if (!strcmp(spa->spa_name, name))
715 spa_get_primary(void)
718 return (STAILQ_FIRST(&zfs_pools));
722 spa_get_primary_vdev(const spa_t *spa)
728 spa = spa_get_primary();
731 vdev = STAILQ_FIRST(&spa->spa_vdevs);
734 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
735 kid = STAILQ_FIRST(&vdev->v_children))
742 spa_create(uint64_t guid)
746 spa = malloc(sizeof(spa_t));
747 memset(spa, 0, sizeof(spa_t));
748 STAILQ_INIT(&spa->spa_vdevs);
749 spa->spa_guid = guid;
750 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
756 state_name(vdev_state_t state)
758 static const char* names[] = {
773 #define pager_printf printf
778 pager_printf(const char *fmt, ...)
784 vsprintf(line, fmt, args);
791 #define STATUS_FORMAT " %s %s\n"
794 print_state(int indent, const char *name, vdev_state_t state)
800 for (i = 0; i < indent; i++)
803 pager_printf(STATUS_FORMAT, buf, state_name(state));
808 vdev_status(vdev_t *vdev, int indent)
811 print_state(indent, vdev->v_name, vdev->v_state);
813 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
814 vdev_status(kid, indent + 1);
819 spa_status(spa_t *spa)
821 static char bootfs[ZFS_MAXNAMELEN];
824 int good_kids, bad_kids, degraded_kids;
827 pager_printf(" pool: %s\n", spa->spa_name);
828 if (zfs_get_root(spa, &rootid) == 0 &&
829 zfs_rlookup(spa, rootid, bootfs) == 0) {
830 if (bootfs[0] == '\0')
831 pager_printf("bootfs: %s\n", spa->spa_name);
833 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
835 pager_printf("config:\n\n");
836 pager_printf(STATUS_FORMAT, "NAME", "STATE");
841 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
842 if (vdev->v_state == VDEV_STATE_HEALTHY)
844 else if (vdev->v_state == VDEV_STATE_DEGRADED)
850 state = VDEV_STATE_CLOSED;
851 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
852 state = VDEV_STATE_HEALTHY;
853 else if ((good_kids + degraded_kids) > 0)
854 state = VDEV_STATE_DEGRADED;
856 print_state(0, spa->spa_name, state);
857 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
858 vdev_status(vdev, 1);
868 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
877 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
880 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
882 vdev_t *vdev, *top_vdev, *pool_vdev;
885 const unsigned char *nvlist;
888 uint64_t pool_txg, pool_guid;
890 const char *pool_name;
891 const unsigned char *vdevs;
892 const unsigned char *features;
895 const struct uberblock *up;
898 * Load the vdev label and figure out which
899 * uberblock is most current.
901 memset(&vtmp, 0, sizeof(vtmp));
902 vtmp.v_phys_read = _read;
903 vtmp.v_read_priv = read_priv;
904 off = offsetof(vdev_label_t, vl_vdev_phys);
906 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
907 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
908 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
909 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
910 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
911 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
912 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
915 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
919 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
921 if (nvlist_find(nvlist,
922 ZPOOL_CONFIG_VERSION,
923 DATA_TYPE_UINT64, 0, &val)) {
927 if (!SPA_VERSION_IS_SUPPORTED(val)) {
928 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
929 (unsigned) val, (unsigned) SPA_VERSION);
933 /* Check ZFS features for read */
934 if (nvlist_find(nvlist,
935 ZPOOL_CONFIG_FEATURES_FOR_READ,
936 DATA_TYPE_NVLIST, 0, &features) == 0
937 && nvlist_check_features_for_read(features) != 0)
940 if (nvlist_find(nvlist,
941 ZPOOL_CONFIG_POOL_STATE,
942 DATA_TYPE_UINT64, 0, &val)) {
946 if (val == POOL_STATE_DESTROYED) {
947 /* We don't boot only from destroyed pools. */
951 if (nvlist_find(nvlist,
952 ZPOOL_CONFIG_POOL_TXG,
953 DATA_TYPE_UINT64, 0, &pool_txg)
954 || nvlist_find(nvlist,
955 ZPOOL_CONFIG_POOL_GUID,
956 DATA_TYPE_UINT64, 0, &pool_guid)
957 || nvlist_find(nvlist,
958 ZPOOL_CONFIG_POOL_NAME,
959 DATA_TYPE_STRING, 0, &pool_name)) {
961 * Cache and spare devices end up here - just ignore
964 /*printf("ZFS: can't find pool details\n");*/
969 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
975 * Create the pool if this is the first time we've seen it.
977 spa = spa_find_by_guid(pool_guid);
979 spa = spa_create(pool_guid);
980 spa->spa_name = strdup(pool_name);
982 if (pool_txg > spa->spa_txg) {
983 spa->spa_txg = pool_txg;
989 * Get the vdev tree and create our in-core copy of it.
990 * If we already have a vdev with this guid, this must
991 * be some kind of alias (overlapping slices, dangerously dedicated
994 if (nvlist_find(nvlist,
996 DATA_TYPE_UINT64, 0, &guid)) {
999 vdev = vdev_find(guid);
1000 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1003 if (nvlist_find(nvlist,
1004 ZPOOL_CONFIG_VDEV_TREE,
1005 DATA_TYPE_NVLIST, 0, &vdevs)) {
1009 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1014 * Add the toplevel vdev to the pool if its not already there.
1016 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1017 if (top_vdev == pool_vdev)
1019 if (!pool_vdev && top_vdev)
1020 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1023 * We should already have created an incomplete vdev for this
1024 * vdev. Find it and initialise it with our read proc.
1026 vdev = vdev_find(guid);
1028 vdev->v_phys_read = _read;
1029 vdev->v_read_priv = read_priv;
1030 vdev->v_state = VDEV_STATE_HEALTHY;
1032 printf("ZFS: inconsistent nvlist contents\n");
1037 * Re-evaluate top-level vdev state.
1039 vdev_set_state(top_vdev);
1042 * Ok, we are happy with the pool so far. Lets find
1043 * the best uberblock and then we can actually access
1044 * the contents of the pool.
1046 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1047 up = (const struct uberblock *)upbuf;
1049 i < VDEV_UBERBLOCK_COUNT(vdev);
1051 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1053 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1054 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1055 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1056 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1057 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1058 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1060 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1063 if (up->ub_magic != UBERBLOCK_MAGIC)
1065 if (up->ub_txg < spa->spa_txg)
1067 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1068 spa->spa_uberblock = *up;
1069 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1070 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1071 spa->spa_uberblock = *up;
1074 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1086 for (v = 0; v < 32; v++)
1093 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1096 zio_gbh_phys_t zio_gb;
1100 /* Artificial BP for gang block header. */
1102 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1103 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1104 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1105 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1106 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1107 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1109 /* Read gang header block using the artificial BP. */
1110 if (zio_read(spa, &gbh_bp, &zio_gb))
1114 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1115 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1117 if (BP_IS_HOLE(gbp))
1119 if (zio_read(spa, gbp, pbuf))
1121 pbuf += BP_GET_PSIZE(gbp);
1124 if (zio_checksum_verify(bp, buf))
1130 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1132 int cpfunc = BP_GET_COMPRESS(bp);
1133 uint64_t align, size;
1138 * Process data embedded in block pointer
1140 if (BP_IS_EMBEDDED(bp)) {
1141 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1143 size = BPE_GET_PSIZE(bp);
1144 ASSERT(size <= BPE_PAYLOAD_SIZE);
1146 if (cpfunc != ZIO_COMPRESS_OFF)
1147 pbuf = zfs_alloc(size);
1151 decode_embedded_bp_compressed(bp, pbuf);
1154 if (cpfunc != ZIO_COMPRESS_OFF) {
1155 error = zio_decompress_data(cpfunc, pbuf,
1156 size, buf, BP_GET_LSIZE(bp));
1157 zfs_free(pbuf, size);
1160 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1167 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1168 const dva_t *dva = &bp->blk_dva[i];
1173 if (!dva->dva_word[0] && !dva->dva_word[1])
1176 vdevid = DVA_GET_VDEV(dva);
1177 offset = DVA_GET_OFFSET(dva);
1178 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1179 if (vdev->v_id == vdevid)
1182 if (!vdev || !vdev->v_read)
1185 size = BP_GET_PSIZE(bp);
1186 if (vdev->v_read == vdev_raidz_read) {
1187 align = 1ULL << vdev->v_top->v_ashift;
1188 if (P2PHASE(size, align) != 0)
1189 size = P2ROUNDUP(size, align);
1191 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1192 pbuf = zfs_alloc(size);
1196 if (DVA_GET_GANG(dva))
1197 error = zio_read_gang(spa, bp, pbuf);
1199 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1201 if (cpfunc != ZIO_COMPRESS_OFF)
1202 error = zio_decompress_data(cpfunc, pbuf,
1203 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1204 else if (size != BP_GET_PSIZE(bp))
1205 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1208 zfs_free(pbuf, size);
1213 printf("ZFS: i/o error - all block copies unavailable\n");
1218 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1220 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1221 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1222 int nlevels = dnode->dn_nlevels;
1225 if (bsize > SPA_MAXBLOCKSIZE) {
1226 printf("ZFS: I/O error - blocks larger than 128K are not supported\n");
1231 * Note: bsize may not be a power of two here so we need to do an
1232 * actual divide rather than a bitshift.
1234 while (buflen > 0) {
1235 uint64_t bn = offset / bsize;
1236 int boff = offset % bsize;
1238 const blkptr_t *indbp;
1241 if (bn > dnode->dn_maxblkid)
1244 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1247 indbp = dnode->dn_blkptr;
1248 for (i = 0; i < nlevels; i++) {
1250 * Copy the bp from the indirect array so that
1251 * we can re-use the scratch buffer for multi-level
1254 ibn = bn >> ((nlevels - i - 1) * ibshift);
1255 ibn &= ((1 << ibshift) - 1);
1257 if (BP_IS_HOLE(&bp)) {
1258 memset(dnode_cache_buf, 0, bsize);
1261 rc = zio_read(spa, &bp, dnode_cache_buf);
1264 indbp = (const blkptr_t *) dnode_cache_buf;
1266 dnode_cache_obj = dnode;
1267 dnode_cache_bn = bn;
1271 * The buffer contains our data block. Copy what we
1272 * need from it and loop.
1275 if (i > buflen) i = buflen;
1276 memcpy(buf, &dnode_cache_buf[boff], i);
1277 buf = ((char*) buf) + i;
1286 * Lookup a value in a microzap directory. Assumes that the zap
1287 * scratch buffer contains the directory contents.
1290 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1292 const mzap_phys_t *mz;
1293 const mzap_ent_phys_t *mze;
1298 * Microzap objects use exactly one block. Read the whole
1301 size = dnode->dn_datablkszsec * 512;
1303 mz = (const mzap_phys_t *) zap_scratch;
1304 chunks = size / MZAP_ENT_LEN - 1;
1306 for (i = 0; i < chunks; i++) {
1307 mze = &mz->mz_chunk[i];
1308 if (!strcmp(mze->mze_name, name)) {
1309 *value = mze->mze_value;
1318 * Compare a name with a zap leaf entry. Return non-zero if the name
1322 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1325 const zap_leaf_chunk_t *nc;
1328 namelen = zc->l_entry.le_name_numints;
1330 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1332 while (namelen > 0) {
1335 if (len > ZAP_LEAF_ARRAY_BYTES)
1336 len = ZAP_LEAF_ARRAY_BYTES;
1337 if (memcmp(p, nc->l_array.la_array, len))
1341 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1348 * Extract a uint64_t value from a zap leaf entry.
1351 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1353 const zap_leaf_chunk_t *vc;
1358 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1359 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1360 value = (value << 8) | p[i];
1367 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1368 * buffer contains the directory header.
1371 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1373 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1374 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1380 if (zh.zap_magic != ZAP_MAGIC)
1383 z.zap_block_shift = ilog2(bsize);
1384 z.zap_phys = (zap_phys_t *) zap_scratch;
1387 * Figure out where the pointer table is and read it in if necessary.
1389 if (zh.zap_ptrtbl.zt_blk) {
1390 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1391 zap_scratch, bsize);
1394 ptrtbl = (uint64_t *) zap_scratch;
1396 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1399 hash = zap_hash(zh.zap_salt, name);
1402 zl.l_bs = z.zap_block_shift;
1404 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1405 zap_leaf_chunk_t *zc;
1407 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1411 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1414 * Make sure this chunk matches our hash.
1416 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1417 && zl.l_phys->l_hdr.lh_prefix
1418 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1422 * Hash within the chunk to find our entry.
1424 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1425 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1426 h = zl.l_phys->l_hash[h];
1429 zc = &ZAP_LEAF_CHUNK(&zl, h);
1430 while (zc->l_entry.le_hash != hash) {
1431 if (zc->l_entry.le_next == 0xffff) {
1435 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1437 if (fzap_name_equal(&zl, zc, name)) {
1438 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1440 *value = fzap_leaf_value(&zl, zc);
1448 * Lookup a name in a zap object and return its value as a uint64_t.
1451 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1455 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1457 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1461 zap_type = *(uint64_t *) zap_scratch;
1462 if (zap_type == ZBT_MICRO)
1463 return mzap_lookup(dnode, name, value);
1464 else if (zap_type == ZBT_HEADER)
1465 return fzap_lookup(spa, dnode, name, value);
1466 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1471 * List a microzap directory. Assumes that the zap scratch buffer contains
1472 * the directory contents.
1475 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *))
1477 const mzap_phys_t *mz;
1478 const mzap_ent_phys_t *mze;
1483 * Microzap objects use exactly one block. Read the whole
1486 size = dnode->dn_datablkszsec * 512;
1487 mz = (const mzap_phys_t *) zap_scratch;
1488 chunks = size / MZAP_ENT_LEN - 1;
1490 for (i = 0; i < chunks; i++) {
1491 mze = &mz->mz_chunk[i];
1492 if (mze->mze_name[0])
1493 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1494 callback(mze->mze_name);
1501 * List a fatzap directory. Assumes that the zap scratch buffer contains
1502 * the directory header.
1505 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *))
1507 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1508 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1512 if (zh.zap_magic != ZAP_MAGIC)
1515 z.zap_block_shift = ilog2(bsize);
1516 z.zap_phys = (zap_phys_t *) zap_scratch;
1519 * This assumes that the leaf blocks start at block 1. The
1520 * documentation isn't exactly clear on this.
1523 zl.l_bs = z.zap_block_shift;
1524 for (i = 0; i < zh.zap_num_leafs; i++) {
1525 off_t off = (i + 1) << zl.l_bs;
1529 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1532 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1534 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1535 zap_leaf_chunk_t *zc, *nc;
1538 zc = &ZAP_LEAF_CHUNK(&zl, j);
1539 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1541 namelen = zc->l_entry.le_name_numints;
1542 if (namelen > sizeof(name))
1543 namelen = sizeof(name);
1546 * Paste the name back together.
1548 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1550 while (namelen > 0) {
1553 if (len > ZAP_LEAF_ARRAY_BYTES)
1554 len = ZAP_LEAF_ARRAY_BYTES;
1555 memcpy(p, nc->l_array.la_array, len);
1558 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1562 * Assume the first eight bytes of the value are
1565 value = fzap_leaf_value(&zl, zc);
1567 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1568 callback((const char *)name);
1575 static int zfs_printf(const char *name)
1578 printf("%s\n", name);
1584 * List a zap directory.
1587 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1590 size_t size = dnode->dn_datablkszsec * 512;
1592 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1595 zap_type = *(uint64_t *) zap_scratch;
1596 if (zap_type == ZBT_MICRO)
1597 return mzap_list(dnode, zfs_printf);
1599 return fzap_list(spa, dnode, zfs_printf);
1603 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1607 offset = objnum * sizeof(dnode_phys_t);
1608 return dnode_read(spa, &os->os_meta_dnode, offset,
1609 dnode, sizeof(dnode_phys_t));
1613 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1615 const mzap_phys_t *mz;
1616 const mzap_ent_phys_t *mze;
1621 * Microzap objects use exactly one block. Read the whole
1624 size = dnode->dn_datablkszsec * 512;
1626 mz = (const mzap_phys_t *) zap_scratch;
1627 chunks = size / MZAP_ENT_LEN - 1;
1629 for (i = 0; i < chunks; i++) {
1630 mze = &mz->mz_chunk[i];
1631 if (value == mze->mze_value) {
1632 strcpy(name, mze->mze_name);
1641 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1644 const zap_leaf_chunk_t *nc;
1647 namelen = zc->l_entry.le_name_numints;
1649 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1651 while (namelen > 0) {
1654 if (len > ZAP_LEAF_ARRAY_BYTES)
1655 len = ZAP_LEAF_ARRAY_BYTES;
1656 memcpy(p, nc->l_array.la_array, len);
1659 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1666 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1668 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1669 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1673 if (zh.zap_magic != ZAP_MAGIC)
1676 z.zap_block_shift = ilog2(bsize);
1677 z.zap_phys = (zap_phys_t *) zap_scratch;
1680 * This assumes that the leaf blocks start at block 1. The
1681 * documentation isn't exactly clear on this.
1684 zl.l_bs = z.zap_block_shift;
1685 for (i = 0; i < zh.zap_num_leafs; i++) {
1686 off_t off = (i + 1) << zl.l_bs;
1688 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1691 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1693 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1694 zap_leaf_chunk_t *zc;
1696 zc = &ZAP_LEAF_CHUNK(&zl, j);
1697 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1699 if (zc->l_entry.le_value_intlen != 8 ||
1700 zc->l_entry.le_value_numints != 1)
1703 if (fzap_leaf_value(&zl, zc) == value) {
1704 fzap_name_copy(&zl, zc, name);
1714 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1718 size_t size = dnode->dn_datablkszsec * 512;
1720 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1724 zap_type = *(uint64_t *) zap_scratch;
1725 if (zap_type == ZBT_MICRO)
1726 return mzap_rlookup(spa, dnode, name, value);
1728 return fzap_rlookup(spa, dnode, name, value);
1732 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1735 char component[256];
1736 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1737 dnode_phys_t child_dir_zap, dataset, dir, parent;
1739 dsl_dataset_phys_t *ds;
1743 p = &name[sizeof(name) - 1];
1746 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1747 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1750 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1751 dir_obj = ds->ds_dir_obj;
1754 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1756 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1758 /* Actual loop condition. */
1759 parent_obj = dd->dd_parent_obj;
1760 if (parent_obj == 0)
1763 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1765 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1766 child_dir_zapobj = dd->dd_child_dir_zapobj;
1767 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1769 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1772 len = strlen(component);
1774 memcpy(p, component, len);
1778 /* Actual loop iteration. */
1779 dir_obj = parent_obj;
1790 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1793 uint64_t dir_obj, child_dir_zapobj;
1794 dnode_phys_t child_dir_zap, dir;
1798 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1800 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1805 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1807 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1811 /* Actual loop condition #1. */
1817 memcpy(element, p, q - p);
1818 element[q - p] = '\0';
1825 child_dir_zapobj = dd->dd_child_dir_zapobj;
1826 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1829 /* Actual loop condition #2. */
1830 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1834 *objnum = dd->dd_head_dataset_obj;
1840 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1842 uint64_t dir_obj, child_dir_zapobj;
1843 dnode_phys_t child_dir_zap, dir, dataset;
1844 dsl_dataset_phys_t *ds;
1847 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1848 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1851 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1852 dir_obj = ds->ds_dir_obj;
1854 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1855 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1858 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1860 child_dir_zapobj = dd->dd_child_dir_zapobj;
1861 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1862 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1866 return (zap_list(spa, &child_dir_zap) != 0);
1870 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *name))
1872 uint64_t dir_obj, child_dir_zapobj, zap_type;
1873 dnode_phys_t child_dir_zap, dir, dataset;
1874 dsl_dataset_phys_t *ds;
1878 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
1880 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1883 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1884 dir_obj = ds->ds_dir_obj;
1886 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
1888 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1891 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1893 child_dir_zapobj = dd->dd_child_dir_zapobj;
1894 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
1896 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1900 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
1904 zap_type = *(uint64_t *) zap_scratch;
1905 if (zap_type == ZBT_MICRO)
1906 return mzap_list(&child_dir_zap, callback);
1908 return fzap_list(spa, &child_dir_zap, callback);
1913 * Find the object set given the object number of its dataset object
1914 * and return its details in *objset
1917 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1919 dnode_phys_t dataset;
1920 dsl_dataset_phys_t *ds;
1922 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1923 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1927 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1928 if (zio_read(spa, &ds->ds_bp, objset)) {
1929 printf("ZFS: can't read object set for dataset %ju\n",
1938 * Find the object set pointed to by the BOOTFS property or the root
1939 * dataset if there is none and return its details in *objset
1942 zfs_get_root(const spa_t *spa, uint64_t *objid)
1944 dnode_phys_t dir, propdir;
1945 uint64_t props, bootfs, root;
1950 * Start with the MOS directory object.
1952 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1953 printf("ZFS: can't read MOS object directory\n");
1958 * Lookup the pool_props and see if we can find a bootfs.
1960 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1961 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1962 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1969 * Lookup the root dataset directory
1971 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1972 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1973 printf("ZFS: can't find root dsl_dir\n");
1978 * Use the information from the dataset directory's bonus buffer
1979 * to find the dataset object and from that the object set itself.
1981 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1982 *objid = dd->dd_head_dataset_obj;
1987 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1993 * Find the root object set if not explicitly provided
1995 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1996 printf("ZFS: can't find root filesystem\n");
2000 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2001 printf("ZFS: can't open root filesystem\n");
2005 mount->rootobj = rootobj;
2011 zfs_spa_init(spa_t *spa)
2014 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2015 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2018 if (spa->spa_mos.os_type != DMU_OST_META) {
2019 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2026 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2029 if (dn->dn_bonustype != DMU_OT_SA) {
2030 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2032 sb->st_mode = zp->zp_mode;
2033 sb->st_uid = zp->zp_uid;
2034 sb->st_gid = zp->zp_gid;
2035 sb->st_size = zp->zp_size;
2037 sa_hdr_phys_t *sahdrp;
2042 if (dn->dn_bonuslen != 0)
2043 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2045 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2046 blkptr_t *bp = &dn->dn_spill;
2049 size = BP_GET_LSIZE(bp);
2050 buf = zfs_alloc(size);
2051 error = zio_read(spa, bp, buf);
2053 zfs_free(buf, size);
2061 hdrsize = SA_HDR_SIZE(sahdrp);
2062 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2064 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2066 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2068 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2071 zfs_free(buf, size);
2078 * Lookup a file and return its dnode.
2081 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2084 uint64_t objnum, rootnum, parentnum;
2090 int symlinks_followed = 0;
2094 if (mount->objset.os_type != DMU_OST_ZFS) {
2095 printf("ZFS: unexpected object set type %ju\n",
2096 (uintmax_t)mount->objset.os_type);
2101 * Get the root directory dnode.
2103 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2107 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
2111 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2124 memcpy(element, p, q - p);
2132 rc = zfs_dnode_stat(spa, &dn, &sb);
2135 if (!S_ISDIR(sb.st_mode))
2139 rc = zap_lookup(spa, &dn, element, &objnum);
2142 objnum = ZFS_DIRENT_OBJ(objnum);
2144 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2149 * Check for symlink.
2151 rc = zfs_dnode_stat(spa, &dn, &sb);
2154 if (S_ISLNK(sb.st_mode)) {
2155 if (symlinks_followed > 10)
2157 symlinks_followed++;
2160 * Read the link value and copy the tail of our
2161 * current path onto the end.
2164 strcpy(&path[sb.st_size], p);
2166 path[sb.st_size] = 0;
2168 * Second test is purely to silence bogus compiler
2169 * warning about accessing past the end of dn_bonus.
2171 if (sb.st_size + sizeof(znode_phys_t) <=
2172 dn.dn_bonuslen && sizeof(znode_phys_t) <=
2173 sizeof(dn.dn_bonus)) {
2174 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2177 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2183 * Restart with the new path, starting either at
2184 * the root or at the parent depending whether or
2185 * not the link is relative.
2192 objset_get_dnode(spa, &mount->objset, objnum, &dn);