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",
63 * List of all pools, chained through spa_link.
65 static spa_list_t zfs_pools;
67 static uint64_t zfs_crc64_table[256];
68 static const dnode_phys_t *dnode_cache_obj = 0;
69 static uint64_t dnode_cache_bn;
70 static char *dnode_cache_buf;
71 static char *zap_scratch;
72 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
74 #define TEMP_SIZE (1024 * 1024)
76 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
77 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
78 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
83 STAILQ_INIT(&zfs_vdevs);
84 STAILQ_INIT(&zfs_pools);
86 zfs_temp_buf = malloc(TEMP_SIZE);
87 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
88 zfs_temp_ptr = zfs_temp_buf;
89 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
90 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
96 zfs_alloc(size_t size)
100 if (zfs_temp_ptr + size > zfs_temp_end) {
101 printf("ZFS: out of temporary buffer space\n");
105 zfs_temp_ptr += size;
111 zfs_free(void *ptr, size_t size)
114 zfs_temp_ptr -= size;
115 if (zfs_temp_ptr != ptr) {
116 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
122 xdr_int(const unsigned char **xdr, int *ip)
124 *ip = ((*xdr)[0] << 24)
133 xdr_u_int(const unsigned char **xdr, u_int *ip)
135 *ip = ((*xdr)[0] << 24)
144 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
150 *lp = (((uint64_t) hi) << 32) | lo;
155 nvlist_find(const unsigned char *nvlist, const char *name, int type,
156 int* elementsp, void *valuep)
158 const unsigned char *p, *pair;
160 int encoded_size, decoded_size;
167 xdr_int(&p, &encoded_size);
168 xdr_int(&p, &decoded_size);
169 while (encoded_size && decoded_size) {
170 int namelen, pairtype, elements;
171 const char *pairname;
173 xdr_int(&p, &namelen);
174 pairname = (const char*) p;
175 p += roundup(namelen, 4);
176 xdr_int(&p, &pairtype);
178 if (!memcmp(name, pairname, namelen) && type == pairtype) {
179 xdr_int(&p, &elements);
181 *elementsp = elements;
182 if (type == DATA_TYPE_UINT64) {
183 xdr_uint64_t(&p, (uint64_t *) valuep);
185 } else if (type == DATA_TYPE_STRING) {
188 (*(const char**) valuep) = (const char*) p;
190 } else if (type == DATA_TYPE_NVLIST
191 || type == DATA_TYPE_NVLIST_ARRAY) {
192 (*(const unsigned char**) valuep) =
193 (const unsigned char*) p;
200 * Not the pair we are looking for, skip to the next one.
202 p = pair + encoded_size;
206 xdr_int(&p, &encoded_size);
207 xdr_int(&p, &decoded_size);
214 nvlist_check_features_for_read(const unsigned char *nvlist)
216 const unsigned char *p, *pair;
218 int encoded_size, decoded_size;
228 xdr_int(&p, &encoded_size);
229 xdr_int(&p, &decoded_size);
230 while (encoded_size && decoded_size) {
231 int namelen, pairtype;
232 const char *pairname;
237 xdr_int(&p, &namelen);
238 pairname = (const char*) p;
239 p += roundup(namelen, 4);
240 xdr_int(&p, &pairtype);
242 for (i = 0; features_for_read[i] != NULL; i++) {
243 if (!memcmp(pairname, features_for_read[i], namelen)) {
250 printf("ZFS: unsupported feature: %s\n", pairname);
254 p = pair + encoded_size;
257 xdr_int(&p, &encoded_size);
258 xdr_int(&p, &decoded_size);
265 * Return the next nvlist in an nvlist array.
267 static const unsigned char *
268 nvlist_next(const unsigned char *nvlist)
270 const unsigned char *p, *pair;
272 int encoded_size, decoded_size;
279 xdr_int(&p, &encoded_size);
280 xdr_int(&p, &decoded_size);
281 while (encoded_size && decoded_size) {
282 p = pair + encoded_size;
285 xdr_int(&p, &encoded_size);
286 xdr_int(&p, &decoded_size);
294 static const unsigned char *
295 nvlist_print(const unsigned char *nvlist, unsigned int indent)
297 static const char* typenames[] = {
308 "DATA_TYPE_BYTE_ARRAY",
309 "DATA_TYPE_INT16_ARRAY",
310 "DATA_TYPE_UINT16_ARRAY",
311 "DATA_TYPE_INT32_ARRAY",
312 "DATA_TYPE_UINT32_ARRAY",
313 "DATA_TYPE_INT64_ARRAY",
314 "DATA_TYPE_UINT64_ARRAY",
315 "DATA_TYPE_STRING_ARRAY",
318 "DATA_TYPE_NVLIST_ARRAY",
319 "DATA_TYPE_BOOLEAN_VALUE",
322 "DATA_TYPE_BOOLEAN_ARRAY",
323 "DATA_TYPE_INT8_ARRAY",
324 "DATA_TYPE_UINT8_ARRAY"
328 const unsigned char *p, *pair;
330 int encoded_size, decoded_size;
337 xdr_int(&p, &encoded_size);
338 xdr_int(&p, &decoded_size);
339 while (encoded_size && decoded_size) {
340 int namelen, pairtype, elements;
341 const char *pairname;
343 xdr_int(&p, &namelen);
344 pairname = (const char*) p;
345 p += roundup(namelen, 4);
346 xdr_int(&p, &pairtype);
348 for (i = 0; i < indent; i++)
350 printf("%s %s", typenames[pairtype], pairname);
352 xdr_int(&p, &elements);
354 case DATA_TYPE_UINT64: {
356 xdr_uint64_t(&p, &val);
357 printf(" = 0x%jx\n", (uintmax_t)val);
361 case DATA_TYPE_STRING: {
364 printf(" = \"%s\"\n", p);
368 case DATA_TYPE_NVLIST:
370 nvlist_print(p, indent + 1);
373 case DATA_TYPE_NVLIST_ARRAY:
374 for (j = 0; j < elements; j++) {
376 p = nvlist_print(p, indent + 1);
377 if (j != elements - 1) {
378 for (i = 0; i < indent; i++)
380 printf("%s %s", typenames[pairtype], pairname);
389 p = pair + encoded_size;
392 xdr_int(&p, &encoded_size);
393 xdr_int(&p, &decoded_size);
402 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
403 off_t offset, size_t size)
408 if (!vdev->v_phys_read)
412 psize = BP_GET_PSIZE(bp);
417 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
418 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
421 if (bp && zio_checksum_verify(bp, buf))
428 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
429 off_t offset, size_t bytes)
432 return (vdev_read_phys(vdev, bp, buf,
433 offset + VDEV_LABEL_START_SIZE, bytes));
438 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
439 off_t offset, size_t bytes)
445 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
446 if (kid->v_state != VDEV_STATE_HEALTHY)
448 rc = kid->v_read(kid, bp, buf, offset, bytes);
457 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
458 off_t offset, size_t bytes)
463 * Here we should have two kids:
464 * First one which is the one we are replacing and we can trust
465 * only this one to have valid data, but it might not be present.
466 * Second one is that one we are replacing with. It is most likely
467 * healthy, but we can't trust it has needed data, so we won't use it.
469 kid = STAILQ_FIRST(&vdev->v_children);
472 if (kid->v_state != VDEV_STATE_HEALTHY)
474 return (kid->v_read(kid, bp, buf, offset, bytes));
478 vdev_find(uint64_t guid)
482 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
483 if (vdev->v_guid == guid)
490 vdev_create(uint64_t guid, vdev_read_t *read)
494 vdev = malloc(sizeof(vdev_t));
495 memset(vdev, 0, sizeof(vdev_t));
496 STAILQ_INIT(&vdev->v_children);
498 vdev->v_state = VDEV_STATE_OFFLINE;
500 vdev->v_phys_read = 0;
501 vdev->v_read_priv = 0;
502 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
508 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
509 vdev_t **vdevp, int is_newer)
512 uint64_t guid, id, ashift, nparity;
516 const unsigned char *kids;
517 int nkids, i, is_new;
518 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
520 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
521 DATA_TYPE_UINT64, 0, &guid)
522 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
523 DATA_TYPE_UINT64, 0, &id)
524 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
525 DATA_TYPE_STRING, 0, &type)) {
526 printf("ZFS: can't find vdev details\n");
530 if (strcmp(type, VDEV_TYPE_MIRROR)
531 && strcmp(type, VDEV_TYPE_DISK)
533 && strcmp(type, VDEV_TYPE_FILE)
535 && strcmp(type, VDEV_TYPE_RAIDZ)
536 && strcmp(type, VDEV_TYPE_REPLACING)) {
537 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
541 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
543 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
545 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
547 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
549 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
551 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
554 vdev = vdev_find(guid);
558 if (!strcmp(type, VDEV_TYPE_MIRROR))
559 vdev = vdev_create(guid, vdev_mirror_read);
560 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
561 vdev = vdev_create(guid, vdev_raidz_read);
562 else if (!strcmp(type, VDEV_TYPE_REPLACING))
563 vdev = vdev_create(guid, vdev_replacing_read);
565 vdev = vdev_create(guid, vdev_disk_read);
568 vdev->v_top = pvdev != NULL ? pvdev : vdev;
569 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
570 DATA_TYPE_UINT64, 0, &ashift) == 0)
571 vdev->v_ashift = ashift;
574 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
575 DATA_TYPE_UINT64, 0, &nparity) == 0)
576 vdev->v_nparity = nparity;
579 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
580 DATA_TYPE_STRING, 0, &path) == 0) {
581 if (strncmp(path, "/dev/", 5) == 0)
583 vdev->v_name = strdup(path);
585 if (!strcmp(type, "raidz")) {
586 if (vdev->v_nparity == 1)
587 vdev->v_name = "raidz1";
588 else if (vdev->v_nparity == 2)
589 vdev->v_name = "raidz2";
590 else if (vdev->v_nparity == 3)
591 vdev->v_name = "raidz3";
593 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
597 vdev->v_name = strdup(type);
604 if (is_new || is_newer) {
606 * This is either new vdev or we've already seen this vdev,
607 * but from an older vdev label, so let's refresh its state
608 * from the newer label.
611 vdev->v_state = VDEV_STATE_OFFLINE;
613 vdev->v_state = VDEV_STATE_REMOVED;
615 vdev->v_state = VDEV_STATE_FAULTED;
616 else if (is_degraded)
617 vdev->v_state = VDEV_STATE_DEGRADED;
618 else if (isnt_present)
619 vdev->v_state = VDEV_STATE_CANT_OPEN;
622 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
623 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
625 * Its ok if we don't have any kids.
628 vdev->v_nchildren = nkids;
629 for (i = 0; i < nkids; i++) {
630 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
634 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
636 kids = nvlist_next(kids);
639 vdev->v_nchildren = 0;
648 vdev_set_state(vdev_t *vdev)
655 * A mirror or raidz is healthy if all its kids are healthy. A
656 * mirror is degraded if any of its kids is healthy; a raidz
657 * is degraded if at most nparity kids are offline.
659 if (STAILQ_FIRST(&vdev->v_children)) {
662 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
663 if (kid->v_state == VDEV_STATE_HEALTHY)
669 vdev->v_state = VDEV_STATE_HEALTHY;
671 if (vdev->v_read == vdev_mirror_read) {
673 vdev->v_state = VDEV_STATE_DEGRADED;
675 vdev->v_state = VDEV_STATE_OFFLINE;
677 } else if (vdev->v_read == vdev_raidz_read) {
678 if (bad_kids > vdev->v_nparity) {
679 vdev->v_state = VDEV_STATE_OFFLINE;
681 vdev->v_state = VDEV_STATE_DEGRADED;
689 spa_find_by_guid(uint64_t guid)
693 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
694 if (spa->spa_guid == guid)
701 spa_find_by_name(const char *name)
705 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
706 if (!strcmp(spa->spa_name, name))
714 spa_get_primary(void)
717 return (STAILQ_FIRST(&zfs_pools));
721 spa_get_primary_vdev(const spa_t *spa)
727 spa = spa_get_primary();
730 vdev = STAILQ_FIRST(&spa->spa_vdevs);
733 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
734 kid = STAILQ_FIRST(&vdev->v_children))
741 spa_create(uint64_t guid)
745 spa = malloc(sizeof(spa_t));
746 memset(spa, 0, sizeof(spa_t));
747 STAILQ_INIT(&spa->spa_vdevs);
748 spa->spa_guid = guid;
749 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
755 state_name(vdev_state_t state)
757 static const char* names[] = {
772 #define pager_printf printf
777 pager_printf(const char *fmt, ...)
783 vsprintf(line, fmt, args);
790 #define STATUS_FORMAT " %s %s\n"
793 print_state(int indent, const char *name, vdev_state_t state)
799 for (i = 0; i < indent; i++)
802 pager_printf(STATUS_FORMAT, buf, state_name(state));
807 vdev_status(vdev_t *vdev, int indent)
810 print_state(indent, vdev->v_name, vdev->v_state);
812 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
813 vdev_status(kid, indent + 1);
818 spa_status(spa_t *spa)
820 static char bootfs[ZFS_MAXNAMELEN];
823 int good_kids, bad_kids, degraded_kids;
826 pager_printf(" pool: %s\n", spa->spa_name);
827 if (zfs_get_root(spa, &rootid) == 0 &&
828 zfs_rlookup(spa, rootid, bootfs) == 0) {
829 if (bootfs[0] == '\0')
830 pager_printf("bootfs: %s\n", spa->spa_name);
832 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
834 pager_printf("config:\n\n");
835 pager_printf(STATUS_FORMAT, "NAME", "STATE");
840 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
841 if (vdev->v_state == VDEV_STATE_HEALTHY)
843 else if (vdev->v_state == VDEV_STATE_DEGRADED)
849 state = VDEV_STATE_CLOSED;
850 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
851 state = VDEV_STATE_HEALTHY;
852 else if ((good_kids + degraded_kids) > 0)
853 state = VDEV_STATE_DEGRADED;
855 print_state(0, spa->spa_name, state);
856 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
857 vdev_status(vdev, 1);
867 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
876 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
879 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
881 vdev_t *vdev, *top_vdev, *pool_vdev;
884 const unsigned char *nvlist;
887 uint64_t pool_txg, pool_guid;
889 const char *pool_name;
890 const unsigned char *vdevs;
891 const unsigned char *features;
894 const struct uberblock *up;
897 * Load the vdev label and figure out which
898 * uberblock is most current.
900 memset(&vtmp, 0, sizeof(vtmp));
901 vtmp.v_phys_read = read;
902 vtmp.v_read_priv = read_priv;
903 off = offsetof(vdev_label_t, vl_vdev_phys);
905 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
906 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
907 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
908 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
909 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
910 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
911 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
914 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
918 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
920 if (nvlist_find(nvlist,
921 ZPOOL_CONFIG_VERSION,
922 DATA_TYPE_UINT64, 0, &val)) {
926 if (!SPA_VERSION_IS_SUPPORTED(val)) {
927 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
928 (unsigned) val, (unsigned) SPA_VERSION);
932 /* Check ZFS features for read */
933 if (nvlist_find(nvlist,
934 ZPOOL_CONFIG_FEATURES_FOR_READ,
935 DATA_TYPE_NVLIST, 0, &features) == 0
936 && nvlist_check_features_for_read(features) != 0)
939 if (nvlist_find(nvlist,
940 ZPOOL_CONFIG_POOL_STATE,
941 DATA_TYPE_UINT64, 0, &val)) {
945 if (val == POOL_STATE_DESTROYED) {
946 /* We don't boot only from destroyed pools. */
950 if (nvlist_find(nvlist,
951 ZPOOL_CONFIG_POOL_TXG,
952 DATA_TYPE_UINT64, 0, &pool_txg)
953 || nvlist_find(nvlist,
954 ZPOOL_CONFIG_POOL_GUID,
955 DATA_TYPE_UINT64, 0, &pool_guid)
956 || nvlist_find(nvlist,
957 ZPOOL_CONFIG_POOL_NAME,
958 DATA_TYPE_STRING, 0, &pool_name)) {
960 * Cache and spare devices end up here - just ignore
963 /*printf("ZFS: can't find pool details\n");*/
968 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
974 * Create the pool if this is the first time we've seen it.
976 spa = spa_find_by_guid(pool_guid);
978 spa = spa_create(pool_guid);
979 spa->spa_name = strdup(pool_name);
981 if (pool_txg > spa->spa_txg) {
982 spa->spa_txg = pool_txg;
988 * Get the vdev tree and create our in-core copy of it.
989 * If we already have a vdev with this guid, this must
990 * be some kind of alias (overlapping slices, dangerously dedicated
993 if (nvlist_find(nvlist,
995 DATA_TYPE_UINT64, 0, &guid)) {
998 vdev = vdev_find(guid);
999 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1002 if (nvlist_find(nvlist,
1003 ZPOOL_CONFIG_VDEV_TREE,
1004 DATA_TYPE_NVLIST, 0, &vdevs)) {
1008 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1013 * Add the toplevel vdev to the pool if its not already there.
1015 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1016 if (top_vdev == pool_vdev)
1018 if (!pool_vdev && top_vdev)
1019 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1022 * We should already have created an incomplete vdev for this
1023 * vdev. Find it and initialise it with our read proc.
1025 vdev = vdev_find(guid);
1027 vdev->v_phys_read = read;
1028 vdev->v_read_priv = read_priv;
1029 vdev->v_state = VDEV_STATE_HEALTHY;
1031 printf("ZFS: inconsistent nvlist contents\n");
1036 * Re-evaluate top-level vdev state.
1038 vdev_set_state(top_vdev);
1041 * Ok, we are happy with the pool so far. Lets find
1042 * the best uberblock and then we can actually access
1043 * the contents of the pool.
1045 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1046 up = (const struct uberblock *)upbuf;
1048 i < VDEV_UBERBLOCK_COUNT(vdev);
1050 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1052 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1053 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1054 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1055 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1056 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1057 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1059 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1062 if (up->ub_magic != UBERBLOCK_MAGIC)
1064 if (up->ub_txg < spa->spa_txg)
1066 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1067 spa->spa_uberblock = *up;
1068 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1069 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1070 spa->spa_uberblock = *up;
1073 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1085 for (v = 0; v < 32; v++)
1092 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1095 zio_gbh_phys_t zio_gb;
1099 /* Artificial BP for gang block header. */
1101 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1102 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1103 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1104 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1105 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1106 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1108 /* Read gang header block using the artificial BP. */
1109 if (zio_read(spa, &gbh_bp, &zio_gb))
1113 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1114 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1116 if (BP_IS_HOLE(gbp))
1118 if (zio_read(spa, gbp, pbuf))
1120 pbuf += BP_GET_PSIZE(gbp);
1123 if (zio_checksum_verify(bp, buf))
1129 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1131 int cpfunc = BP_GET_COMPRESS(bp);
1132 uint64_t align, size;
1138 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1139 const dva_t *dva = &bp->blk_dva[i];
1144 if (!dva->dva_word[0] && !dva->dva_word[1])
1147 vdevid = DVA_GET_VDEV(dva);
1148 offset = DVA_GET_OFFSET(dva);
1149 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1150 if (vdev->v_id == vdevid)
1153 if (!vdev || !vdev->v_read)
1156 size = BP_GET_PSIZE(bp);
1157 if (vdev->v_read == vdev_raidz_read) {
1158 align = 1ULL << vdev->v_top->v_ashift;
1159 if (P2PHASE(size, align) != 0)
1160 size = P2ROUNDUP(size, align);
1162 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1163 pbuf = zfs_alloc(size);
1167 if (DVA_GET_GANG(dva))
1168 error = zio_read_gang(spa, bp, pbuf);
1170 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1172 if (cpfunc != ZIO_COMPRESS_OFF)
1173 error = zio_decompress_data(cpfunc, pbuf,
1174 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1175 else if (size != BP_GET_PSIZE(bp))
1176 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1179 zfs_free(pbuf, size);
1184 printf("ZFS: i/o error - all block copies unavailable\n");
1189 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1191 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1192 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1193 int nlevels = dnode->dn_nlevels;
1197 * Note: bsize may not be a power of two here so we need to do an
1198 * actual divide rather than a bitshift.
1200 while (buflen > 0) {
1201 uint64_t bn = offset / bsize;
1202 int boff = offset % bsize;
1204 const blkptr_t *indbp;
1207 if (bn > dnode->dn_maxblkid)
1210 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1213 indbp = dnode->dn_blkptr;
1214 for (i = 0; i < nlevels; i++) {
1216 * Copy the bp from the indirect array so that
1217 * we can re-use the scratch buffer for multi-level
1220 ibn = bn >> ((nlevels - i - 1) * ibshift);
1221 ibn &= ((1 << ibshift) - 1);
1223 rc = zio_read(spa, &bp, dnode_cache_buf);
1226 indbp = (const blkptr_t *) dnode_cache_buf;
1228 dnode_cache_obj = dnode;
1229 dnode_cache_bn = bn;
1233 * The buffer contains our data block. Copy what we
1234 * need from it and loop.
1237 if (i > buflen) i = buflen;
1238 memcpy(buf, &dnode_cache_buf[boff], i);
1239 buf = ((char*) buf) + i;
1248 * Lookup a value in a microzap directory. Assumes that the zap
1249 * scratch buffer contains the directory contents.
1252 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1254 const mzap_phys_t *mz;
1255 const mzap_ent_phys_t *mze;
1260 * Microzap objects use exactly one block. Read the whole
1263 size = dnode->dn_datablkszsec * 512;
1265 mz = (const mzap_phys_t *) zap_scratch;
1266 chunks = size / MZAP_ENT_LEN - 1;
1268 for (i = 0; i < chunks; i++) {
1269 mze = &mz->mz_chunk[i];
1270 if (!strcmp(mze->mze_name, name)) {
1271 *value = mze->mze_value;
1280 * Compare a name with a zap leaf entry. Return non-zero if the name
1284 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1287 const zap_leaf_chunk_t *nc;
1290 namelen = zc->l_entry.le_name_numints;
1292 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1294 while (namelen > 0) {
1297 if (len > ZAP_LEAF_ARRAY_BYTES)
1298 len = ZAP_LEAF_ARRAY_BYTES;
1299 if (memcmp(p, nc->l_array.la_array, len))
1303 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1310 * Extract a uint64_t value from a zap leaf entry.
1313 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1315 const zap_leaf_chunk_t *vc;
1320 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1321 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1322 value = (value << 8) | p[i];
1329 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1330 * buffer contains the directory header.
1333 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1335 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1336 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1342 if (zh.zap_magic != ZAP_MAGIC)
1345 z.zap_block_shift = ilog2(bsize);
1346 z.zap_phys = (zap_phys_t *) zap_scratch;
1349 * Figure out where the pointer table is and read it in if necessary.
1351 if (zh.zap_ptrtbl.zt_blk) {
1352 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1353 zap_scratch, bsize);
1356 ptrtbl = (uint64_t *) zap_scratch;
1358 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1361 hash = zap_hash(zh.zap_salt, name);
1364 zl.l_bs = z.zap_block_shift;
1366 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1367 zap_leaf_chunk_t *zc;
1369 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1373 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1376 * Make sure this chunk matches our hash.
1378 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1379 && zl.l_phys->l_hdr.lh_prefix
1380 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1384 * Hash within the chunk to find our entry.
1386 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1387 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1388 h = zl.l_phys->l_hash[h];
1391 zc = &ZAP_LEAF_CHUNK(&zl, h);
1392 while (zc->l_entry.le_hash != hash) {
1393 if (zc->l_entry.le_next == 0xffff) {
1397 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1399 if (fzap_name_equal(&zl, zc, name)) {
1400 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1402 *value = fzap_leaf_value(&zl, zc);
1410 * Lookup a name in a zap object and return its value as a uint64_t.
1413 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1417 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1419 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1423 zap_type = *(uint64_t *) zap_scratch;
1424 if (zap_type == ZBT_MICRO)
1425 return mzap_lookup(dnode, name, value);
1426 else if (zap_type == ZBT_HEADER)
1427 return fzap_lookup(spa, dnode, name, value);
1428 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1433 * List a microzap directory. Assumes that the zap scratch buffer contains
1434 * the directory contents.
1437 mzap_list(const dnode_phys_t *dnode)
1439 const mzap_phys_t *mz;
1440 const mzap_ent_phys_t *mze;
1445 * Microzap objects use exactly one block. Read the whole
1448 size = dnode->dn_datablkszsec * 512;
1449 mz = (const mzap_phys_t *) zap_scratch;
1450 chunks = size / MZAP_ENT_LEN - 1;
1452 for (i = 0; i < chunks; i++) {
1453 mze = &mz->mz_chunk[i];
1454 if (mze->mze_name[0])
1455 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1456 printf("%s\n", mze->mze_name);
1463 * List a fatzap directory. Assumes that the zap scratch buffer contains
1464 * the directory header.
1467 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1469 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1470 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1474 if (zh.zap_magic != ZAP_MAGIC)
1477 z.zap_block_shift = ilog2(bsize);
1478 z.zap_phys = (zap_phys_t *) zap_scratch;
1481 * This assumes that the leaf blocks start at block 1. The
1482 * documentation isn't exactly clear on this.
1485 zl.l_bs = z.zap_block_shift;
1486 for (i = 0; i < zh.zap_num_leafs; i++) {
1487 off_t off = (i + 1) << zl.l_bs;
1491 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1494 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1496 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1497 zap_leaf_chunk_t *zc, *nc;
1500 zc = &ZAP_LEAF_CHUNK(&zl, j);
1501 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1503 namelen = zc->l_entry.le_name_numints;
1504 if (namelen > sizeof(name))
1505 namelen = sizeof(name);
1508 * Paste the name back together.
1510 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1512 while (namelen > 0) {
1515 if (len > ZAP_LEAF_ARRAY_BYTES)
1516 len = ZAP_LEAF_ARRAY_BYTES;
1517 memcpy(p, nc->l_array.la_array, len);
1520 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1524 * Assume the first eight bytes of the value are
1527 value = fzap_leaf_value(&zl, zc);
1529 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1530 printf("%s\n", name);
1538 * List a zap directory.
1541 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1544 size_t size = dnode->dn_datablkszsec * 512;
1546 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1549 zap_type = *(uint64_t *) zap_scratch;
1550 if (zap_type == ZBT_MICRO)
1551 return mzap_list(dnode);
1553 return fzap_list(spa, dnode);
1557 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1561 offset = objnum * sizeof(dnode_phys_t);
1562 return dnode_read(spa, &os->os_meta_dnode, offset,
1563 dnode, sizeof(dnode_phys_t));
1567 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1569 const mzap_phys_t *mz;
1570 const mzap_ent_phys_t *mze;
1575 * Microzap objects use exactly one block. Read the whole
1578 size = dnode->dn_datablkszsec * 512;
1580 mz = (const mzap_phys_t *) zap_scratch;
1581 chunks = size / MZAP_ENT_LEN - 1;
1583 for (i = 0; i < chunks; i++) {
1584 mze = &mz->mz_chunk[i];
1585 if (value == mze->mze_value) {
1586 strcpy(name, mze->mze_name);
1595 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1598 const zap_leaf_chunk_t *nc;
1601 namelen = zc->l_entry.le_name_numints;
1603 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1605 while (namelen > 0) {
1608 if (len > ZAP_LEAF_ARRAY_BYTES)
1609 len = ZAP_LEAF_ARRAY_BYTES;
1610 memcpy(p, nc->l_array.la_array, len);
1613 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1620 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1622 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1623 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1627 if (zh.zap_magic != ZAP_MAGIC)
1630 z.zap_block_shift = ilog2(bsize);
1631 z.zap_phys = (zap_phys_t *) zap_scratch;
1634 * This assumes that the leaf blocks start at block 1. The
1635 * documentation isn't exactly clear on this.
1638 zl.l_bs = z.zap_block_shift;
1639 for (i = 0; i < zh.zap_num_leafs; i++) {
1640 off_t off = (i + 1) << zl.l_bs;
1642 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1645 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1647 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1648 zap_leaf_chunk_t *zc;
1650 zc = &ZAP_LEAF_CHUNK(&zl, j);
1651 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1653 if (zc->l_entry.le_value_intlen != 8 ||
1654 zc->l_entry.le_value_numints != 1)
1657 if (fzap_leaf_value(&zl, zc) == value) {
1658 fzap_name_copy(&zl, zc, name);
1668 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1672 size_t size = dnode->dn_datablkszsec * 512;
1674 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1678 zap_type = *(uint64_t *) zap_scratch;
1679 if (zap_type == ZBT_MICRO)
1680 return mzap_rlookup(spa, dnode, name, value);
1682 return fzap_rlookup(spa, dnode, name, value);
1686 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1689 char component[256];
1690 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1691 dnode_phys_t child_dir_zap, dataset, dir, parent;
1693 dsl_dataset_phys_t *ds;
1697 p = &name[sizeof(name) - 1];
1700 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1701 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1704 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1705 dir_obj = ds->ds_dir_obj;
1708 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1710 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1712 /* Actual loop condition. */
1713 parent_obj = dd->dd_parent_obj;
1714 if (parent_obj == 0)
1717 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1719 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1720 child_dir_zapobj = dd->dd_child_dir_zapobj;
1721 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1723 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1726 len = strlen(component);
1728 memcpy(p, component, len);
1732 /* Actual loop iteration. */
1733 dir_obj = parent_obj;
1744 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1747 uint64_t dir_obj, child_dir_zapobj;
1748 dnode_phys_t child_dir_zap, dir;
1752 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1754 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1759 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1761 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1765 /* Actual loop condition #1. */
1771 memcpy(element, p, q - p);
1772 element[q - p] = '\0';
1779 child_dir_zapobj = dd->dd_child_dir_zapobj;
1780 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1783 /* Actual loop condition #2. */
1784 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1788 *objnum = dd->dd_head_dataset_obj;
1794 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1796 uint64_t dir_obj, child_dir_zapobj;
1797 dnode_phys_t child_dir_zap, dir, dataset;
1798 dsl_dataset_phys_t *ds;
1801 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1802 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1805 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1806 dir_obj = ds->ds_dir_obj;
1808 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1809 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1812 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1814 child_dir_zapobj = dd->dd_child_dir_zapobj;
1815 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1816 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1820 return (zap_list(spa, &child_dir_zap) != 0);
1825 * Find the object set given the object number of its dataset object
1826 * and return its details in *objset
1829 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1831 dnode_phys_t dataset;
1832 dsl_dataset_phys_t *ds;
1834 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1835 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1839 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1840 if (zio_read(spa, &ds->ds_bp, objset)) {
1841 printf("ZFS: can't read object set for dataset %ju\n",
1850 * Find the object set pointed to by the BOOTFS property or the root
1851 * dataset if there is none and return its details in *objset
1854 zfs_get_root(const spa_t *spa, uint64_t *objid)
1856 dnode_phys_t dir, propdir;
1857 uint64_t props, bootfs, root;
1862 * Start with the MOS directory object.
1864 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1865 printf("ZFS: can't read MOS object directory\n");
1870 * Lookup the pool_props and see if we can find a bootfs.
1872 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1873 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1874 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1881 * Lookup the root dataset directory
1883 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1884 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1885 printf("ZFS: can't find root dsl_dir\n");
1890 * Use the information from the dataset directory's bonus buffer
1891 * to find the dataset object and from that the object set itself.
1893 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1894 *objid = dd->dd_head_dataset_obj;
1899 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1905 * Find the root object set if not explicitly provided
1907 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1908 printf("ZFS: can't find root filesystem\n");
1912 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1913 printf("ZFS: can't open root filesystem\n");
1917 mount->rootobj = rootobj;
1923 zfs_spa_init(spa_t *spa)
1926 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1927 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1930 if (spa->spa_mos.os_type != DMU_OST_META) {
1931 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
1938 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1941 if (dn->dn_bonustype != DMU_OT_SA) {
1942 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1944 sb->st_mode = zp->zp_mode;
1945 sb->st_uid = zp->zp_uid;
1946 sb->st_gid = zp->zp_gid;
1947 sb->st_size = zp->zp_size;
1949 sa_hdr_phys_t *sahdrp;
1954 if (dn->dn_bonuslen != 0)
1955 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1957 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1958 blkptr_t *bp = &dn->dn_spill;
1961 size = BP_GET_LSIZE(bp);
1962 buf = zfs_alloc(size);
1963 error = zio_read(spa, bp, buf);
1965 zfs_free(buf, size);
1973 hdrsize = SA_HDR_SIZE(sahdrp);
1974 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1976 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1978 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1980 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1983 zfs_free(buf, size);
1990 * Lookup a file and return its dnode.
1993 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1996 uint64_t objnum, rootnum, parentnum;
2002 int symlinks_followed = 0;
2006 if (mount->objset.os_type != DMU_OST_ZFS) {
2007 printf("ZFS: unexpected object set type %ju\n",
2008 (uintmax_t)mount->objset.os_type);
2013 * Get the root directory dnode.
2015 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2019 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
2023 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2036 memcpy(element, p, q - p);
2044 rc = zfs_dnode_stat(spa, &dn, &sb);
2047 if (!S_ISDIR(sb.st_mode))
2051 rc = zap_lookup(spa, &dn, element, &objnum);
2054 objnum = ZFS_DIRENT_OBJ(objnum);
2056 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2061 * Check for symlink.
2063 rc = zfs_dnode_stat(spa, &dn, &sb);
2066 if (S_ISLNK(sb.st_mode)) {
2067 if (symlinks_followed > 10)
2069 symlinks_followed++;
2072 * Read the link value and copy the tail of our
2073 * current path onto the end.
2076 strcpy(&path[sb.st_size], p);
2078 path[sb.st_size] = 0;
2079 if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
2080 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2083 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2089 * Restart with the new path, starting either at
2090 * the root or at the parent depending whether or
2091 * not the link is relative.
2098 objset_get_dnode(spa, &mount->objset, objnum, &dn);