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[] = {
60 * List of all pools, chained through spa_link.
62 static spa_list_t zfs_pools;
64 static uint64_t zfs_crc64_table[256];
65 static const dnode_phys_t *dnode_cache_obj = 0;
66 static uint64_t dnode_cache_bn;
67 static char *dnode_cache_buf;
68 static char *zap_scratch;
69 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
71 #define TEMP_SIZE (1024 * 1024)
73 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
74 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
75 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
80 STAILQ_INIT(&zfs_vdevs);
81 STAILQ_INIT(&zfs_pools);
83 zfs_temp_buf = malloc(TEMP_SIZE);
84 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
85 zfs_temp_ptr = zfs_temp_buf;
86 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
87 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
93 zfs_alloc(size_t size)
97 if (zfs_temp_ptr + size > zfs_temp_end) {
98 printf("ZFS: out of temporary buffer space\n");
102 zfs_temp_ptr += size;
108 zfs_free(void *ptr, size_t size)
111 zfs_temp_ptr -= size;
112 if (zfs_temp_ptr != ptr) {
113 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
119 xdr_int(const unsigned char **xdr, int *ip)
121 *ip = ((*xdr)[0] << 24)
130 xdr_u_int(const unsigned char **xdr, u_int *ip)
132 *ip = ((*xdr)[0] << 24)
141 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
147 *lp = (((uint64_t) hi) << 32) | lo;
152 nvlist_find(const unsigned char *nvlist, const char *name, int type,
153 int* elementsp, void *valuep)
155 const unsigned char *p, *pair;
157 int encoded_size, decoded_size;
164 xdr_int(&p, &encoded_size);
165 xdr_int(&p, &decoded_size);
166 while (encoded_size && decoded_size) {
167 int namelen, pairtype, elements;
168 const char *pairname;
170 xdr_int(&p, &namelen);
171 pairname = (const char*) p;
172 p += roundup(namelen, 4);
173 xdr_int(&p, &pairtype);
175 if (!memcmp(name, pairname, namelen) && type == pairtype) {
176 xdr_int(&p, &elements);
178 *elementsp = elements;
179 if (type == DATA_TYPE_UINT64) {
180 xdr_uint64_t(&p, (uint64_t *) valuep);
182 } else if (type == DATA_TYPE_STRING) {
185 (*(const char**) valuep) = (const char*) p;
187 } else if (type == DATA_TYPE_NVLIST
188 || type == DATA_TYPE_NVLIST_ARRAY) {
189 (*(const unsigned char**) valuep) =
190 (const unsigned char*) p;
197 * Not the pair we are looking for, skip to the next one.
199 p = pair + encoded_size;
203 xdr_int(&p, &encoded_size);
204 xdr_int(&p, &decoded_size);
211 nvlist_check_features_for_read(const unsigned char *nvlist)
213 const unsigned char *p, *pair;
215 int encoded_size, decoded_size;
225 xdr_int(&p, &encoded_size);
226 xdr_int(&p, &decoded_size);
227 while (encoded_size && decoded_size) {
228 int namelen, pairtype;
229 const char *pairname;
234 xdr_int(&p, &namelen);
235 pairname = (const char*) p;
236 p += roundup(namelen, 4);
237 xdr_int(&p, &pairtype);
239 for (i = 0; features_for_read[i] != NULL; i++) {
240 if (!memcmp(pairname, features_for_read[i], namelen)) {
247 printf("ZFS: unsupported feature: %s\n", pairname);
251 p = pair + encoded_size;
254 xdr_int(&p, &encoded_size);
255 xdr_int(&p, &decoded_size);
262 * Return the next nvlist in an nvlist array.
264 static const unsigned char *
265 nvlist_next(const unsigned char *nvlist)
267 const unsigned char *p, *pair;
269 int encoded_size, decoded_size;
276 xdr_int(&p, &encoded_size);
277 xdr_int(&p, &decoded_size);
278 while (encoded_size && decoded_size) {
279 p = pair + encoded_size;
282 xdr_int(&p, &encoded_size);
283 xdr_int(&p, &decoded_size);
291 static const unsigned char *
292 nvlist_print(const unsigned char *nvlist, unsigned int indent)
294 static const char* typenames[] = {
305 "DATA_TYPE_BYTE_ARRAY",
306 "DATA_TYPE_INT16_ARRAY",
307 "DATA_TYPE_UINT16_ARRAY",
308 "DATA_TYPE_INT32_ARRAY",
309 "DATA_TYPE_UINT32_ARRAY",
310 "DATA_TYPE_INT64_ARRAY",
311 "DATA_TYPE_UINT64_ARRAY",
312 "DATA_TYPE_STRING_ARRAY",
315 "DATA_TYPE_NVLIST_ARRAY",
316 "DATA_TYPE_BOOLEAN_VALUE",
319 "DATA_TYPE_BOOLEAN_ARRAY",
320 "DATA_TYPE_INT8_ARRAY",
321 "DATA_TYPE_UINT8_ARRAY"
325 const unsigned char *p, *pair;
327 int encoded_size, decoded_size;
334 xdr_int(&p, &encoded_size);
335 xdr_int(&p, &decoded_size);
336 while (encoded_size && decoded_size) {
337 int namelen, pairtype, elements;
338 const char *pairname;
340 xdr_int(&p, &namelen);
341 pairname = (const char*) p;
342 p += roundup(namelen, 4);
343 xdr_int(&p, &pairtype);
345 for (i = 0; i < indent; i++)
347 printf("%s %s", typenames[pairtype], pairname);
349 xdr_int(&p, &elements);
351 case DATA_TYPE_UINT64: {
353 xdr_uint64_t(&p, &val);
354 printf(" = 0x%jx\n", (uintmax_t)val);
358 case DATA_TYPE_STRING: {
361 printf(" = \"%s\"\n", p);
365 case DATA_TYPE_NVLIST:
367 nvlist_print(p, indent + 1);
370 case DATA_TYPE_NVLIST_ARRAY:
371 for (j = 0; j < elements; j++) {
373 p = nvlist_print(p, indent + 1);
374 if (j != elements - 1) {
375 for (i = 0; i < indent; i++)
377 printf("%s %s", typenames[pairtype], pairname);
386 p = pair + encoded_size;
389 xdr_int(&p, &encoded_size);
390 xdr_int(&p, &decoded_size);
399 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
400 off_t offset, size_t size)
405 if (!vdev->v_phys_read)
409 psize = BP_GET_PSIZE(bp);
414 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
415 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
418 if (bp && zio_checksum_verify(bp, buf))
425 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
426 off_t offset, size_t bytes)
429 return (vdev_read_phys(vdev, bp, buf,
430 offset + VDEV_LABEL_START_SIZE, bytes));
435 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
436 off_t offset, size_t bytes)
442 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
443 if (kid->v_state != VDEV_STATE_HEALTHY)
445 rc = kid->v_read(kid, bp, buf, offset, bytes);
454 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
455 off_t offset, size_t bytes)
460 * Here we should have two kids:
461 * First one which is the one we are replacing and we can trust
462 * only this one to have valid data, but it might not be present.
463 * Second one is that one we are replacing with. It is most likely
464 * healthy, but we can't trust it has needed data, so we won't use it.
466 kid = STAILQ_FIRST(&vdev->v_children);
469 if (kid->v_state != VDEV_STATE_HEALTHY)
471 return (kid->v_read(kid, bp, buf, offset, bytes));
475 vdev_find(uint64_t guid)
479 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
480 if (vdev->v_guid == guid)
487 vdev_create(uint64_t guid, vdev_read_t *read)
491 vdev = malloc(sizeof(vdev_t));
492 memset(vdev, 0, sizeof(vdev_t));
493 STAILQ_INIT(&vdev->v_children);
495 vdev->v_state = VDEV_STATE_OFFLINE;
497 vdev->v_phys_read = 0;
498 vdev->v_read_priv = 0;
499 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
505 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
506 vdev_t **vdevp, int is_newer)
509 uint64_t guid, id, ashift, nparity;
513 const unsigned char *kids;
514 int nkids, i, is_new;
515 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
517 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
518 DATA_TYPE_UINT64, 0, &guid)
519 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
520 DATA_TYPE_UINT64, 0, &id)
521 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
522 DATA_TYPE_STRING, 0, &type)) {
523 printf("ZFS: can't find vdev details\n");
527 if (strcmp(type, VDEV_TYPE_MIRROR)
528 && strcmp(type, VDEV_TYPE_DISK)
530 && strcmp(type, VDEV_TYPE_FILE)
532 && strcmp(type, VDEV_TYPE_RAIDZ)
533 && strcmp(type, VDEV_TYPE_REPLACING)) {
534 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
538 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
540 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
542 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
544 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
546 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
548 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
551 vdev = vdev_find(guid);
555 if (!strcmp(type, VDEV_TYPE_MIRROR))
556 vdev = vdev_create(guid, vdev_mirror_read);
557 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
558 vdev = vdev_create(guid, vdev_raidz_read);
559 else if (!strcmp(type, VDEV_TYPE_REPLACING))
560 vdev = vdev_create(guid, vdev_replacing_read);
562 vdev = vdev_create(guid, vdev_disk_read);
565 vdev->v_top = pvdev != NULL ? pvdev : vdev;
566 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
567 DATA_TYPE_UINT64, 0, &ashift) == 0)
568 vdev->v_ashift = ashift;
571 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
572 DATA_TYPE_UINT64, 0, &nparity) == 0)
573 vdev->v_nparity = nparity;
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
577 DATA_TYPE_STRING, 0, &path) == 0) {
578 if (strncmp(path, "/dev/", 5) == 0)
580 vdev->v_name = strdup(path);
582 if (!strcmp(type, "raidz")) {
583 if (vdev->v_nparity == 1)
584 vdev->v_name = "raidz1";
585 else if (vdev->v_nparity == 2)
586 vdev->v_name = "raidz2";
587 else if (vdev->v_nparity == 3)
588 vdev->v_name = "raidz3";
590 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
594 vdev->v_name = strdup(type);
601 if (is_new || is_newer) {
603 * This is either new vdev or we've already seen this vdev,
604 * but from an older vdev label, so let's refresh its state
605 * from the newer label.
608 vdev->v_state = VDEV_STATE_OFFLINE;
610 vdev->v_state = VDEV_STATE_REMOVED;
612 vdev->v_state = VDEV_STATE_FAULTED;
613 else if (is_degraded)
614 vdev->v_state = VDEV_STATE_DEGRADED;
615 else if (isnt_present)
616 vdev->v_state = VDEV_STATE_CANT_OPEN;
619 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
620 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
622 * Its ok if we don't have any kids.
625 vdev->v_nchildren = nkids;
626 for (i = 0; i < nkids; i++) {
627 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
631 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
633 kids = nvlist_next(kids);
636 vdev->v_nchildren = 0;
645 vdev_set_state(vdev_t *vdev)
652 * A mirror or raidz is healthy if all its kids are healthy. A
653 * mirror is degraded if any of its kids is healthy; a raidz
654 * is degraded if at most nparity kids are offline.
656 if (STAILQ_FIRST(&vdev->v_children)) {
659 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
660 if (kid->v_state == VDEV_STATE_HEALTHY)
666 vdev->v_state = VDEV_STATE_HEALTHY;
668 if (vdev->v_read == vdev_mirror_read) {
670 vdev->v_state = VDEV_STATE_DEGRADED;
672 vdev->v_state = VDEV_STATE_OFFLINE;
674 } else if (vdev->v_read == vdev_raidz_read) {
675 if (bad_kids > vdev->v_nparity) {
676 vdev->v_state = VDEV_STATE_OFFLINE;
678 vdev->v_state = VDEV_STATE_DEGRADED;
686 spa_find_by_guid(uint64_t guid)
690 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
691 if (spa->spa_guid == guid)
698 spa_find_by_name(const char *name)
702 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
703 if (!strcmp(spa->spa_name, name))
711 spa_get_primary(void)
714 return (STAILQ_FIRST(&zfs_pools));
718 spa_get_primary_vdev(const spa_t *spa)
724 spa = spa_get_primary();
727 vdev = STAILQ_FIRST(&spa->spa_vdevs);
730 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
731 kid = STAILQ_FIRST(&vdev->v_children))
738 spa_create(uint64_t guid)
742 spa = malloc(sizeof(spa_t));
743 memset(spa, 0, sizeof(spa_t));
744 STAILQ_INIT(&spa->spa_vdevs);
745 spa->spa_guid = guid;
746 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
752 state_name(vdev_state_t state)
754 static const char* names[] = {
769 #define pager_printf printf
774 pager_printf(const char *fmt, ...)
780 vsprintf(line, fmt, args);
787 #define STATUS_FORMAT " %s %s\n"
790 print_state(int indent, const char *name, vdev_state_t state)
796 for (i = 0; i < indent; i++)
799 pager_printf(STATUS_FORMAT, buf, state_name(state));
804 vdev_status(vdev_t *vdev, int indent)
807 print_state(indent, vdev->v_name, vdev->v_state);
809 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
810 vdev_status(kid, indent + 1);
815 spa_status(spa_t *spa)
817 static char bootfs[ZFS_MAXNAMELEN];
820 int good_kids, bad_kids, degraded_kids;
823 pager_printf(" pool: %s\n", spa->spa_name);
824 if (zfs_get_root(spa, &rootid) == 0 &&
825 zfs_rlookup(spa, rootid, bootfs) == 0) {
826 if (bootfs[0] == '\0')
827 pager_printf("bootfs: %s\n", spa->spa_name);
829 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
831 pager_printf("config:\n\n");
832 pager_printf(STATUS_FORMAT, "NAME", "STATE");
837 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
838 if (vdev->v_state == VDEV_STATE_HEALTHY)
840 else if (vdev->v_state == VDEV_STATE_DEGRADED)
846 state = VDEV_STATE_CLOSED;
847 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
848 state = VDEV_STATE_HEALTHY;
849 else if ((good_kids + degraded_kids) > 0)
850 state = VDEV_STATE_DEGRADED;
852 print_state(0, spa->spa_name, state);
853 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
854 vdev_status(vdev, 1);
864 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
873 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
876 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
878 vdev_t *vdev, *top_vdev, *pool_vdev;
881 const unsigned char *nvlist;
884 uint64_t pool_txg, pool_guid;
886 const char *pool_name;
887 const unsigned char *vdevs;
888 const unsigned char *features;
891 const struct uberblock *up;
894 * Load the vdev label and figure out which
895 * uberblock is most current.
897 memset(&vtmp, 0, sizeof(vtmp));
898 vtmp.v_phys_read = read;
899 vtmp.v_read_priv = read_priv;
900 off = offsetof(vdev_label_t, vl_vdev_phys);
902 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
903 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
904 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
905 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
906 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
907 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
908 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
911 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
915 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
917 if (nvlist_find(nvlist,
918 ZPOOL_CONFIG_VERSION,
919 DATA_TYPE_UINT64, 0, &val)) {
923 if (!SPA_VERSION_IS_SUPPORTED(val)) {
924 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
925 (unsigned) val, (unsigned) SPA_VERSION);
929 /* Check ZFS features for read */
930 if (nvlist_find(nvlist,
931 ZPOOL_CONFIG_FEATURES_FOR_READ,
932 DATA_TYPE_NVLIST, 0, &features) == 0
933 && nvlist_check_features_for_read(features) != 0)
936 if (nvlist_find(nvlist,
937 ZPOOL_CONFIG_POOL_STATE,
938 DATA_TYPE_UINT64, 0, &val)) {
942 if (val == POOL_STATE_DESTROYED) {
943 /* We don't boot only from destroyed pools. */
947 if (nvlist_find(nvlist,
948 ZPOOL_CONFIG_POOL_TXG,
949 DATA_TYPE_UINT64, 0, &pool_txg)
950 || nvlist_find(nvlist,
951 ZPOOL_CONFIG_POOL_GUID,
952 DATA_TYPE_UINT64, 0, &pool_guid)
953 || nvlist_find(nvlist,
954 ZPOOL_CONFIG_POOL_NAME,
955 DATA_TYPE_STRING, 0, &pool_name)) {
957 * Cache and spare devices end up here - just ignore
960 /*printf("ZFS: can't find pool details\n");*/
965 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
971 * Create the pool if this is the first time we've seen it.
973 spa = spa_find_by_guid(pool_guid);
975 spa = spa_create(pool_guid);
976 spa->spa_name = strdup(pool_name);
978 if (pool_txg > spa->spa_txg) {
979 spa->spa_txg = pool_txg;
985 * Get the vdev tree and create our in-core copy of it.
986 * If we already have a vdev with this guid, this must
987 * be some kind of alias (overlapping slices, dangerously dedicated
990 if (nvlist_find(nvlist,
992 DATA_TYPE_UINT64, 0, &guid)) {
995 vdev = vdev_find(guid);
996 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
999 if (nvlist_find(nvlist,
1000 ZPOOL_CONFIG_VDEV_TREE,
1001 DATA_TYPE_NVLIST, 0, &vdevs)) {
1005 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1010 * Add the toplevel vdev to the pool if its not already there.
1012 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1013 if (top_vdev == pool_vdev)
1015 if (!pool_vdev && top_vdev)
1016 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1019 * We should already have created an incomplete vdev for this
1020 * vdev. Find it and initialise it with our read proc.
1022 vdev = vdev_find(guid);
1024 vdev->v_phys_read = read;
1025 vdev->v_read_priv = read_priv;
1026 vdev->v_state = VDEV_STATE_HEALTHY;
1028 printf("ZFS: inconsistent nvlist contents\n");
1033 * Re-evaluate top-level vdev state.
1035 vdev_set_state(top_vdev);
1038 * Ok, we are happy with the pool so far. Lets find
1039 * the best uberblock and then we can actually access
1040 * the contents of the pool.
1042 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1043 up = (const struct uberblock *)upbuf;
1045 i < VDEV_UBERBLOCK_COUNT(vdev);
1047 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1049 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1050 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1051 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1052 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1053 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1054 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1056 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1059 if (up->ub_magic != UBERBLOCK_MAGIC)
1061 if (up->ub_txg < spa->spa_txg)
1063 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1064 spa->spa_uberblock = *up;
1065 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1066 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1067 spa->spa_uberblock = *up;
1070 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1082 for (v = 0; v < 32; v++)
1089 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1092 zio_gbh_phys_t zio_gb;
1096 /* Artificial BP for gang block header. */
1098 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1099 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1100 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1101 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1102 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1103 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1105 /* Read gang header block using the artificial BP. */
1106 if (zio_read(spa, &gbh_bp, &zio_gb))
1110 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1111 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1113 if (BP_IS_HOLE(gbp))
1115 if (zio_read(spa, gbp, pbuf))
1117 pbuf += BP_GET_PSIZE(gbp);
1120 if (zio_checksum_verify(bp, buf))
1126 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1128 int cpfunc = BP_GET_COMPRESS(bp);
1129 uint64_t align, size;
1135 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1136 const dva_t *dva = &bp->blk_dva[i];
1141 if (!dva->dva_word[0] && !dva->dva_word[1])
1144 vdevid = DVA_GET_VDEV(dva);
1145 offset = DVA_GET_OFFSET(dva);
1146 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1147 if (vdev->v_id == vdevid)
1150 if (!vdev || !vdev->v_read)
1153 size = BP_GET_PSIZE(bp);
1154 if (vdev->v_read == vdev_raidz_read) {
1155 align = 1ULL << vdev->v_top->v_ashift;
1156 if (P2PHASE(size, align) != 0)
1157 size = P2ROUNDUP(size, align);
1159 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1160 pbuf = zfs_alloc(size);
1164 if (DVA_GET_GANG(dva))
1165 error = zio_read_gang(spa, bp, pbuf);
1167 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1169 if (cpfunc != ZIO_COMPRESS_OFF)
1170 error = zio_decompress_data(cpfunc, pbuf,
1171 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1172 else if (size != BP_GET_PSIZE(bp))
1173 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1176 zfs_free(pbuf, size);
1181 printf("ZFS: i/o error - all block copies unavailable\n");
1186 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1188 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1189 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1190 int nlevels = dnode->dn_nlevels;
1194 * Note: bsize may not be a power of two here so we need to do an
1195 * actual divide rather than a bitshift.
1197 while (buflen > 0) {
1198 uint64_t bn = offset / bsize;
1199 int boff = offset % bsize;
1201 const blkptr_t *indbp;
1204 if (bn > dnode->dn_maxblkid)
1207 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1210 indbp = dnode->dn_blkptr;
1211 for (i = 0; i < nlevels; i++) {
1213 * Copy the bp from the indirect array so that
1214 * we can re-use the scratch buffer for multi-level
1217 ibn = bn >> ((nlevels - i - 1) * ibshift);
1218 ibn &= ((1 << ibshift) - 1);
1220 rc = zio_read(spa, &bp, dnode_cache_buf);
1223 indbp = (const blkptr_t *) dnode_cache_buf;
1225 dnode_cache_obj = dnode;
1226 dnode_cache_bn = bn;
1230 * The buffer contains our data block. Copy what we
1231 * need from it and loop.
1234 if (i > buflen) i = buflen;
1235 memcpy(buf, &dnode_cache_buf[boff], i);
1236 buf = ((char*) buf) + i;
1245 * Lookup a value in a microzap directory. Assumes that the zap
1246 * scratch buffer contains the directory contents.
1249 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1251 const mzap_phys_t *mz;
1252 const mzap_ent_phys_t *mze;
1257 * Microzap objects use exactly one block. Read the whole
1260 size = dnode->dn_datablkszsec * 512;
1262 mz = (const mzap_phys_t *) zap_scratch;
1263 chunks = size / MZAP_ENT_LEN - 1;
1265 for (i = 0; i < chunks; i++) {
1266 mze = &mz->mz_chunk[i];
1267 if (!strcmp(mze->mze_name, name)) {
1268 *value = mze->mze_value;
1277 * Compare a name with a zap leaf entry. Return non-zero if the name
1281 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1284 const zap_leaf_chunk_t *nc;
1287 namelen = zc->l_entry.le_name_numints;
1289 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1291 while (namelen > 0) {
1294 if (len > ZAP_LEAF_ARRAY_BYTES)
1295 len = ZAP_LEAF_ARRAY_BYTES;
1296 if (memcmp(p, nc->l_array.la_array, len))
1300 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1307 * Extract a uint64_t value from a zap leaf entry.
1310 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1312 const zap_leaf_chunk_t *vc;
1317 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1318 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1319 value = (value << 8) | p[i];
1326 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1327 * buffer contains the directory header.
1330 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1332 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1333 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1339 if (zh.zap_magic != ZAP_MAGIC)
1342 z.zap_block_shift = ilog2(bsize);
1343 z.zap_phys = (zap_phys_t *) zap_scratch;
1346 * Figure out where the pointer table is and read it in if necessary.
1348 if (zh.zap_ptrtbl.zt_blk) {
1349 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1350 zap_scratch, bsize);
1353 ptrtbl = (uint64_t *) zap_scratch;
1355 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1358 hash = zap_hash(zh.zap_salt, name);
1361 zl.l_bs = z.zap_block_shift;
1363 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1364 zap_leaf_chunk_t *zc;
1366 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1370 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1373 * Make sure this chunk matches our hash.
1375 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1376 && zl.l_phys->l_hdr.lh_prefix
1377 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1381 * Hash within the chunk to find our entry.
1383 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1384 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1385 h = zl.l_phys->l_hash[h];
1388 zc = &ZAP_LEAF_CHUNK(&zl, h);
1389 while (zc->l_entry.le_hash != hash) {
1390 if (zc->l_entry.le_next == 0xffff) {
1394 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1396 if (fzap_name_equal(&zl, zc, name)) {
1397 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1399 *value = fzap_leaf_value(&zl, zc);
1407 * Lookup a name in a zap object and return its value as a uint64_t.
1410 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1414 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1416 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1420 zap_type = *(uint64_t *) zap_scratch;
1421 if (zap_type == ZBT_MICRO)
1422 return mzap_lookup(dnode, name, value);
1423 else if (zap_type == ZBT_HEADER)
1424 return fzap_lookup(spa, dnode, name, value);
1425 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1430 * List a microzap directory. Assumes that the zap scratch buffer contains
1431 * the directory contents.
1434 mzap_list(const dnode_phys_t *dnode)
1436 const mzap_phys_t *mz;
1437 const mzap_ent_phys_t *mze;
1442 * Microzap objects use exactly one block. Read the whole
1445 size = dnode->dn_datablkszsec * 512;
1446 mz = (const mzap_phys_t *) zap_scratch;
1447 chunks = size / MZAP_ENT_LEN - 1;
1449 for (i = 0; i < chunks; i++) {
1450 mze = &mz->mz_chunk[i];
1451 if (mze->mze_name[0])
1452 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1453 printf("%s\n", mze->mze_name);
1460 * List a fatzap directory. Assumes that the zap scratch buffer contains
1461 * the directory header.
1464 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1466 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1467 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1471 if (zh.zap_magic != ZAP_MAGIC)
1474 z.zap_block_shift = ilog2(bsize);
1475 z.zap_phys = (zap_phys_t *) zap_scratch;
1478 * This assumes that the leaf blocks start at block 1. The
1479 * documentation isn't exactly clear on this.
1482 zl.l_bs = z.zap_block_shift;
1483 for (i = 0; i < zh.zap_num_leafs; i++) {
1484 off_t off = (i + 1) << zl.l_bs;
1488 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1491 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1493 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1494 zap_leaf_chunk_t *zc, *nc;
1497 zc = &ZAP_LEAF_CHUNK(&zl, j);
1498 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1500 namelen = zc->l_entry.le_name_numints;
1501 if (namelen > sizeof(name))
1502 namelen = sizeof(name);
1505 * Paste the name back together.
1507 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1509 while (namelen > 0) {
1512 if (len > ZAP_LEAF_ARRAY_BYTES)
1513 len = ZAP_LEAF_ARRAY_BYTES;
1514 memcpy(p, nc->l_array.la_array, len);
1517 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1521 * Assume the first eight bytes of the value are
1524 value = fzap_leaf_value(&zl, zc);
1526 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1527 printf("%s\n", name);
1535 * List a zap directory.
1538 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1541 size_t size = dnode->dn_datablkszsec * 512;
1543 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1546 zap_type = *(uint64_t *) zap_scratch;
1547 if (zap_type == ZBT_MICRO)
1548 return mzap_list(dnode);
1550 return fzap_list(spa, dnode);
1554 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1558 offset = objnum * sizeof(dnode_phys_t);
1559 return dnode_read(spa, &os->os_meta_dnode, offset,
1560 dnode, sizeof(dnode_phys_t));
1564 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1566 const mzap_phys_t *mz;
1567 const mzap_ent_phys_t *mze;
1572 * Microzap objects use exactly one block. Read the whole
1575 size = dnode->dn_datablkszsec * 512;
1577 mz = (const mzap_phys_t *) zap_scratch;
1578 chunks = size / MZAP_ENT_LEN - 1;
1580 for (i = 0; i < chunks; i++) {
1581 mze = &mz->mz_chunk[i];
1582 if (value == mze->mze_value) {
1583 strcpy(name, mze->mze_name);
1592 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1595 const zap_leaf_chunk_t *nc;
1598 namelen = zc->l_entry.le_name_numints;
1600 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1602 while (namelen > 0) {
1605 if (len > ZAP_LEAF_ARRAY_BYTES)
1606 len = ZAP_LEAF_ARRAY_BYTES;
1607 memcpy(p, nc->l_array.la_array, len);
1610 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1617 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1619 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1620 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1624 if (zh.zap_magic != ZAP_MAGIC)
1627 z.zap_block_shift = ilog2(bsize);
1628 z.zap_phys = (zap_phys_t *) zap_scratch;
1631 * This assumes that the leaf blocks start at block 1. The
1632 * documentation isn't exactly clear on this.
1635 zl.l_bs = z.zap_block_shift;
1636 for (i = 0; i < zh.zap_num_leafs; i++) {
1637 off_t off = (i + 1) << zl.l_bs;
1639 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1642 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1644 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1645 zap_leaf_chunk_t *zc;
1647 zc = &ZAP_LEAF_CHUNK(&zl, j);
1648 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1650 if (zc->l_entry.le_value_intlen != 8 ||
1651 zc->l_entry.le_value_numints != 1)
1654 if (fzap_leaf_value(&zl, zc) == value) {
1655 fzap_name_copy(&zl, zc, name);
1665 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1669 size_t size = dnode->dn_datablkszsec * 512;
1671 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1675 zap_type = *(uint64_t *) zap_scratch;
1676 if (zap_type == ZBT_MICRO)
1677 return mzap_rlookup(spa, dnode, name, value);
1679 return fzap_rlookup(spa, dnode, name, value);
1683 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1686 char component[256];
1687 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1688 dnode_phys_t child_dir_zap, dataset, dir, parent;
1690 dsl_dataset_phys_t *ds;
1694 p = &name[sizeof(name) - 1];
1697 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1698 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1701 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1702 dir_obj = ds->ds_dir_obj;
1705 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1707 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1709 /* Actual loop condition. */
1710 parent_obj = dd->dd_parent_obj;
1711 if (parent_obj == 0)
1714 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1716 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1717 child_dir_zapobj = dd->dd_child_dir_zapobj;
1718 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1720 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1723 len = strlen(component);
1725 memcpy(p, component, len);
1729 /* Actual loop iteration. */
1730 dir_obj = parent_obj;
1741 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1744 uint64_t dir_obj, child_dir_zapobj;
1745 dnode_phys_t child_dir_zap, dir;
1749 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1751 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1756 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1758 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1762 /* Actual loop condition #1. */
1768 memcpy(element, p, q - p);
1769 element[q - p] = '\0';
1776 child_dir_zapobj = dd->dd_child_dir_zapobj;
1777 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1780 /* Actual loop condition #2. */
1781 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1785 *objnum = dd->dd_head_dataset_obj;
1791 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1793 uint64_t dir_obj, child_dir_zapobj;
1794 dnode_phys_t child_dir_zap, dir, dataset;
1795 dsl_dataset_phys_t *ds;
1798 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1799 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1802 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1803 dir_obj = ds->ds_dir_obj;
1805 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1806 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1809 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1811 child_dir_zapobj = dd->dd_child_dir_zapobj;
1812 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1813 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1817 return (zap_list(spa, &child_dir_zap) != 0);
1822 * Find the object set given the object number of its dataset object
1823 * and return its details in *objset
1826 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1828 dnode_phys_t dataset;
1829 dsl_dataset_phys_t *ds;
1831 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1832 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1836 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1837 if (zio_read(spa, &ds->ds_bp, objset)) {
1838 printf("ZFS: can't read object set for dataset %ju\n",
1847 * Find the object set pointed to by the BOOTFS property or the root
1848 * dataset if there is none and return its details in *objset
1851 zfs_get_root(const spa_t *spa, uint64_t *objid)
1853 dnode_phys_t dir, propdir;
1854 uint64_t props, bootfs, root;
1859 * Start with the MOS directory object.
1861 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1862 printf("ZFS: can't read MOS object directory\n");
1867 * Lookup the pool_props and see if we can find a bootfs.
1869 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1870 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1871 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1878 * Lookup the root dataset directory
1880 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1881 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1882 printf("ZFS: can't find root dsl_dir\n");
1887 * Use the information from the dataset directory's bonus buffer
1888 * to find the dataset object and from that the object set itself.
1890 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1891 *objid = dd->dd_head_dataset_obj;
1896 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1902 * Find the root object set if not explicitly provided
1904 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1905 printf("ZFS: can't find root filesystem\n");
1909 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1910 printf("ZFS: can't open root filesystem\n");
1914 mount->rootobj = rootobj;
1920 zfs_spa_init(spa_t *spa)
1923 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1924 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1927 if (spa->spa_mos.os_type != DMU_OST_META) {
1928 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
1935 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1938 if (dn->dn_bonustype != DMU_OT_SA) {
1939 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1941 sb->st_mode = zp->zp_mode;
1942 sb->st_uid = zp->zp_uid;
1943 sb->st_gid = zp->zp_gid;
1944 sb->st_size = zp->zp_size;
1946 sa_hdr_phys_t *sahdrp;
1951 if (dn->dn_bonuslen != 0)
1952 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1954 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1955 blkptr_t *bp = &dn->dn_spill;
1958 size = BP_GET_LSIZE(bp);
1959 buf = zfs_alloc(size);
1960 error = zio_read(spa, bp, buf);
1962 zfs_free(buf, size);
1970 hdrsize = SA_HDR_SIZE(sahdrp);
1971 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1973 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1975 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1977 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1980 zfs_free(buf, size);
1987 * Lookup a file and return its dnode.
1990 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1993 uint64_t objnum, rootnum, parentnum;
1999 int symlinks_followed = 0;
2003 if (mount->objset.os_type != DMU_OST_ZFS) {
2004 printf("ZFS: unexpected object set type %ju\n",
2005 (uintmax_t)mount->objset.os_type);
2010 * Get the root directory dnode.
2012 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2016 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
2020 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2033 memcpy(element, p, q - p);
2041 rc = zfs_dnode_stat(spa, &dn, &sb);
2044 if (!S_ISDIR(sb.st_mode))
2048 rc = zap_lookup(spa, &dn, element, &objnum);
2051 objnum = ZFS_DIRENT_OBJ(objnum);
2053 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2058 * Check for symlink.
2060 rc = zfs_dnode_stat(spa, &dn, &sb);
2063 if (S_ISLNK(sb.st_mode)) {
2064 if (symlinks_followed > 10)
2066 symlinks_followed++;
2069 * Read the link value and copy the tail of our
2070 * current path onto the end.
2073 strcpy(&path[sb.st_size], p);
2075 path[sb.st_size] = 0;
2076 if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
2077 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2080 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2086 * Restart with the new path, starting either at
2087 * the root or at the parent depending whether or
2088 * not the link is relative.
2095 objset_get_dnode(spa, &mount->objset, objnum, &dn);