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.
40 * List of all vdevs, chained through v_alllink.
42 static vdev_list_t zfs_vdevs;
45 * List of all pools, chained through spa_link.
47 static spa_list_t zfs_pools;
49 static uint64_t zfs_crc64_table[256];
50 static const dnode_phys_t *dnode_cache_obj = 0;
51 static uint64_t dnode_cache_bn;
52 static char *dnode_cache_buf;
53 static char *zap_scratch;
54 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
56 #define TEMP_SIZE (1024 * 1024)
58 static int zio_read(spa_t *spa, const blkptr_t *bp, void *buf);
63 STAILQ_INIT(&zfs_vdevs);
64 STAILQ_INIT(&zfs_pools);
66 zfs_temp_buf = malloc(TEMP_SIZE);
67 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
68 zfs_temp_ptr = zfs_temp_buf;
69 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
70 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
76 zfs_alloc(size_t size)
80 if (zfs_temp_ptr + size > zfs_temp_end) {
81 printf("ZFS: out of temporary buffer space\n");
91 zfs_free(void *ptr, size_t size)
95 if (zfs_temp_ptr != ptr) {
96 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
102 xdr_int(const unsigned char **xdr, int *ip)
104 *ip = ((*xdr)[0] << 24)
113 xdr_u_int(const unsigned char **xdr, u_int *ip)
115 *ip = ((*xdr)[0] << 24)
124 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
130 *lp = (((uint64_t) hi) << 32) | lo;
135 nvlist_find(const unsigned char *nvlist, const char *name, int type,
136 int* elementsp, void *valuep)
138 const unsigned char *p, *pair;
140 int encoded_size, decoded_size;
147 xdr_int(&p, &encoded_size);
148 xdr_int(&p, &decoded_size);
149 while (encoded_size && decoded_size) {
150 int namelen, pairtype, elements;
151 const char *pairname;
153 xdr_int(&p, &namelen);
154 pairname = (const char*) p;
155 p += roundup(namelen, 4);
156 xdr_int(&p, &pairtype);
158 if (!memcmp(name, pairname, namelen) && type == pairtype) {
159 xdr_int(&p, &elements);
161 *elementsp = elements;
162 if (type == DATA_TYPE_UINT64) {
163 xdr_uint64_t(&p, (uint64_t *) valuep);
165 } else if (type == DATA_TYPE_STRING) {
168 (*(const char**) valuep) = (const char*) p;
170 } else if (type == DATA_TYPE_NVLIST
171 || type == DATA_TYPE_NVLIST_ARRAY) {
172 (*(const unsigned char**) valuep) =
173 (const unsigned char*) p;
180 * Not the pair we are looking for, skip to the next one.
182 p = pair + encoded_size;
186 xdr_int(&p, &encoded_size);
187 xdr_int(&p, &decoded_size);
194 * Return the next nvlist in an nvlist array.
196 static const unsigned char *
197 nvlist_next(const unsigned char *nvlist)
199 const unsigned char *p, *pair;
201 int encoded_size, decoded_size;
208 xdr_int(&p, &encoded_size);
209 xdr_int(&p, &decoded_size);
210 while (encoded_size && decoded_size) {
211 p = pair + encoded_size;
214 xdr_int(&p, &encoded_size);
215 xdr_int(&p, &decoded_size);
223 static const unsigned char *
224 nvlist_print(const unsigned char *nvlist, unsigned int indent)
226 static const char* typenames[] = {
237 "DATA_TYPE_BYTE_ARRAY",
238 "DATA_TYPE_INT16_ARRAY",
239 "DATA_TYPE_UINT16_ARRAY",
240 "DATA_TYPE_INT32_ARRAY",
241 "DATA_TYPE_UINT32_ARRAY",
242 "DATA_TYPE_INT64_ARRAY",
243 "DATA_TYPE_UINT64_ARRAY",
244 "DATA_TYPE_STRING_ARRAY",
247 "DATA_TYPE_NVLIST_ARRAY",
248 "DATA_TYPE_BOOLEAN_VALUE",
251 "DATA_TYPE_BOOLEAN_ARRAY",
252 "DATA_TYPE_INT8_ARRAY",
253 "DATA_TYPE_UINT8_ARRAY"
257 const unsigned char *p, *pair;
259 int encoded_size, decoded_size;
266 xdr_int(&p, &encoded_size);
267 xdr_int(&p, &decoded_size);
268 while (encoded_size && decoded_size) {
269 int namelen, pairtype, elements;
270 const char *pairname;
272 xdr_int(&p, &namelen);
273 pairname = (const char*) p;
274 p += roundup(namelen, 4);
275 xdr_int(&p, &pairtype);
277 for (i = 0; i < indent; i++)
279 printf("%s %s", typenames[pairtype], pairname);
281 xdr_int(&p, &elements);
283 case DATA_TYPE_UINT64: {
285 xdr_uint64_t(&p, &val);
286 printf(" = 0x%llx\n", val);
290 case DATA_TYPE_STRING: {
293 printf(" = \"%s\"\n", p);
297 case DATA_TYPE_NVLIST:
299 nvlist_print(p, indent + 1);
302 case DATA_TYPE_NVLIST_ARRAY:
303 for (j = 0; j < elements; j++) {
305 p = nvlist_print(p, indent + 1);
306 if (j != elements - 1) {
307 for (i = 0; i < indent; i++)
309 printf("%s %s", typenames[pairtype], pairname);
318 p = pair + encoded_size;
321 xdr_int(&p, &encoded_size);
322 xdr_int(&p, &decoded_size);
331 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
332 off_t offset, size_t size)
337 if (!vdev->v_phys_read)
341 psize = BP_GET_PSIZE(bp);
346 /*printf("ZFS: reading %d bytes at 0x%llx to %p\n", psize, offset, buf);*/
347 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
350 if (bp && zio_checksum_verify(bp, buf))
357 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
358 off_t offset, size_t bytes)
361 return (vdev_read_phys(vdev, bp, buf,
362 offset + VDEV_LABEL_START_SIZE, bytes));
367 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
368 off_t offset, size_t bytes)
374 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
375 if (kid->v_state != VDEV_STATE_HEALTHY)
377 rc = kid->v_read(kid, bp, buf, offset, bytes);
386 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
387 off_t offset, size_t bytes)
392 * Here we should have two kids:
393 * First one which is the one we are replacing and we can trust
394 * only this one to have valid data, but it might not be present.
395 * Second one is that one we are replacing with. It is most likely
396 * healthy, but we can't trust it has needed data, so we won't use it.
398 kid = STAILQ_FIRST(&vdev->v_children);
401 if (kid->v_state != VDEV_STATE_HEALTHY)
403 return (kid->v_read(kid, bp, buf, offset, bytes));
407 vdev_find(uint64_t guid)
411 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
412 if (vdev->v_guid == guid)
419 vdev_create(uint64_t guid, vdev_read_t *read)
423 vdev = malloc(sizeof(vdev_t));
424 memset(vdev, 0, sizeof(vdev_t));
425 STAILQ_INIT(&vdev->v_children);
427 vdev->v_state = VDEV_STATE_OFFLINE;
429 vdev->v_phys_read = 0;
430 vdev->v_read_priv = 0;
431 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
437 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
438 vdev_t **vdevp, int is_newer)
441 uint64_t guid, id, ashift, nparity;
445 const unsigned char *kids;
446 int nkids, i, is_new;
447 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
449 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
450 DATA_TYPE_UINT64, 0, &guid)
451 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
452 DATA_TYPE_UINT64, 0, &id)
453 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
454 DATA_TYPE_STRING, 0, &type)) {
455 printf("ZFS: can't find vdev details\n");
459 if (strcmp(type, VDEV_TYPE_MIRROR)
460 && strcmp(type, VDEV_TYPE_DISK)
461 && strcmp(type, VDEV_TYPE_RAIDZ)
462 && strcmp(type, VDEV_TYPE_REPLACING)) {
463 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
467 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
469 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
471 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
473 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
475 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
477 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
480 vdev = vdev_find(guid);
484 if (!strcmp(type, VDEV_TYPE_MIRROR))
485 vdev = vdev_create(guid, vdev_mirror_read);
486 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
487 vdev = vdev_create(guid, vdev_raidz_read);
488 else if (!strcmp(type, VDEV_TYPE_REPLACING))
489 vdev = vdev_create(guid, vdev_replacing_read);
491 vdev = vdev_create(guid, vdev_disk_read);
494 vdev->v_top = pvdev != NULL ? pvdev : vdev;
495 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
496 DATA_TYPE_UINT64, 0, &ashift) == 0)
497 vdev->v_ashift = ashift;
500 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
501 DATA_TYPE_UINT64, 0, &nparity) == 0)
502 vdev->v_nparity = nparity;
505 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
506 DATA_TYPE_STRING, 0, &path) == 0) {
507 if (strncmp(path, "/dev/", 5) == 0)
509 vdev->v_name = strdup(path);
511 if (!strcmp(type, "raidz")) {
512 if (vdev->v_nparity == 1)
513 vdev->v_name = "raidz1";
514 else if (vdev->v_nparity == 2)
515 vdev->v_name = "raidz2";
516 else if (vdev->v_nparity == 3)
517 vdev->v_name = "raidz3";
519 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
523 vdev->v_name = strdup(type);
530 if (is_new || is_newer) {
532 * This is either new vdev or we've already seen this vdev,
533 * but from an older vdev label, so let's refresh its state
534 * from the newer label.
537 vdev->v_state = VDEV_STATE_OFFLINE;
539 vdev->v_state = VDEV_STATE_REMOVED;
541 vdev->v_state = VDEV_STATE_FAULTED;
542 else if (is_degraded)
543 vdev->v_state = VDEV_STATE_DEGRADED;
544 else if (isnt_present)
545 vdev->v_state = VDEV_STATE_CANT_OPEN;
548 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
549 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
551 * Its ok if we don't have any kids.
554 vdev->v_nchildren = nkids;
555 for (i = 0; i < nkids; i++) {
556 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
560 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
562 kids = nvlist_next(kids);
565 vdev->v_nchildren = 0;
574 vdev_set_state(vdev_t *vdev)
581 * A mirror or raidz is healthy if all its kids are healthy. A
582 * mirror is degraded if any of its kids is healthy; a raidz
583 * is degraded if at most nparity kids are offline.
585 if (STAILQ_FIRST(&vdev->v_children)) {
588 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
589 if (kid->v_state == VDEV_STATE_HEALTHY)
595 vdev->v_state = VDEV_STATE_HEALTHY;
597 if (vdev->v_read == vdev_mirror_read) {
599 vdev->v_state = VDEV_STATE_DEGRADED;
601 vdev->v_state = VDEV_STATE_OFFLINE;
603 } else if (vdev->v_read == vdev_raidz_read) {
604 if (bad_kids > vdev->v_nparity) {
605 vdev->v_state = VDEV_STATE_OFFLINE;
607 vdev->v_state = VDEV_STATE_DEGRADED;
615 spa_find_by_guid(uint64_t guid)
619 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
620 if (spa->spa_guid == guid)
629 spa_find_by_name(const char *name)
633 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
634 if (!strcmp(spa->spa_name, name))
643 spa_create(uint64_t guid)
647 spa = malloc(sizeof(spa_t));
648 memset(spa, 0, sizeof(spa_t));
649 STAILQ_INIT(&spa->spa_vdevs);
650 spa->spa_guid = guid;
651 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
657 state_name(vdev_state_t state)
659 static const char* names[] = {
674 #define pager_printf printf
679 pager_printf(const char *fmt, ...)
685 vsprintf(line, fmt, args);
692 #define STATUS_FORMAT " %s %s\n"
695 print_state(int indent, const char *name, vdev_state_t state)
701 for (i = 0; i < indent; i++)
704 pager_printf(STATUS_FORMAT, buf, state_name(state));
709 vdev_status(vdev_t *vdev, int indent)
712 print_state(indent, vdev->v_name, vdev->v_state);
714 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
715 vdev_status(kid, indent + 1);
720 spa_status(spa_t *spa)
723 int good_kids, bad_kids, degraded_kids;
726 pager_printf(" pool: %s\n", spa->spa_name);
727 pager_printf("config:\n\n");
728 pager_printf(STATUS_FORMAT, "NAME", "STATE");
733 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
734 if (vdev->v_state == VDEV_STATE_HEALTHY)
736 else if (vdev->v_state == VDEV_STATE_DEGRADED)
742 state = VDEV_STATE_CLOSED;
743 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
744 state = VDEV_STATE_HEALTHY;
745 else if ((good_kids + degraded_kids) > 0)
746 state = VDEV_STATE_DEGRADED;
748 print_state(0, spa->spa_name, state);
749 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
750 vdev_status(vdev, 1);
760 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
769 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
772 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
774 vdev_t *vdev, *top_vdev, *pool_vdev;
777 const unsigned char *nvlist;
780 uint64_t pool_txg, pool_guid;
782 const char *pool_name;
783 const unsigned char *vdevs;
786 const struct uberblock *up;
789 * Load the vdev label and figure out which
790 * uberblock is most current.
792 memset(&vtmp, 0, sizeof(vtmp));
793 vtmp.v_phys_read = read;
794 vtmp.v_read_priv = read_priv;
795 off = offsetof(vdev_label_t, vl_vdev_phys);
797 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
798 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
799 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
800 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
801 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
802 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
803 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
806 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
810 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
812 if (nvlist_find(nvlist,
813 ZPOOL_CONFIG_VERSION,
814 DATA_TYPE_UINT64, 0, &val)) {
818 if (val > SPA_VERSION) {
819 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
820 (unsigned) val, (unsigned) SPA_VERSION);
824 if (nvlist_find(nvlist,
825 ZPOOL_CONFIG_POOL_STATE,
826 DATA_TYPE_UINT64, 0, &val)) {
830 if (val == POOL_STATE_DESTROYED) {
831 /* We don't boot only from destroyed pools. */
835 if (nvlist_find(nvlist,
836 ZPOOL_CONFIG_POOL_TXG,
837 DATA_TYPE_UINT64, 0, &pool_txg)
838 || nvlist_find(nvlist,
839 ZPOOL_CONFIG_POOL_GUID,
840 DATA_TYPE_UINT64, 0, &pool_guid)
841 || nvlist_find(nvlist,
842 ZPOOL_CONFIG_POOL_NAME,
843 DATA_TYPE_STRING, 0, &pool_name)) {
845 * Cache and spare devices end up here - just ignore
848 /*printf("ZFS: can't find pool details\n");*/
853 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
859 * Create the pool if this is the first time we've seen it.
861 spa = spa_find_by_guid(pool_guid);
863 spa = spa_create(pool_guid);
864 spa->spa_name = strdup(pool_name);
866 if (pool_txg > spa->spa_txg) {
867 spa->spa_txg = pool_txg;
873 * Get the vdev tree and create our in-core copy of it.
874 * If we already have a vdev with this guid, this must
875 * be some kind of alias (overlapping slices, dangerously dedicated
878 if (nvlist_find(nvlist,
880 DATA_TYPE_UINT64, 0, &guid)) {
883 vdev = vdev_find(guid);
884 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
887 if (nvlist_find(nvlist,
888 ZPOOL_CONFIG_VDEV_TREE,
889 DATA_TYPE_NVLIST, 0, &vdevs)) {
893 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
898 * Add the toplevel vdev to the pool if its not already there.
900 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
901 if (top_vdev == pool_vdev)
903 if (!pool_vdev && top_vdev)
904 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
907 * We should already have created an incomplete vdev for this
908 * vdev. Find it and initialise it with our read proc.
910 vdev = vdev_find(guid);
912 vdev->v_phys_read = read;
913 vdev->v_read_priv = read_priv;
914 vdev->v_state = VDEV_STATE_HEALTHY;
916 printf("ZFS: inconsistent nvlist contents\n");
921 * Re-evaluate top-level vdev state.
923 vdev_set_state(top_vdev);
926 * Ok, we are happy with the pool so far. Lets find
927 * the best uberblock and then we can actually access
928 * the contents of the pool.
930 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
931 up = (const struct uberblock *)upbuf;
933 i < VDEV_UBERBLOCK_COUNT(vdev);
935 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
937 DVA_SET_OFFSET(&bp.blk_dva[0], off);
938 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
939 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
940 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
941 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
942 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
944 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
947 if (up->ub_magic != UBERBLOCK_MAGIC)
949 if (up->ub_txg < spa->spa_txg)
951 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
952 spa->spa_uberblock = *up;
953 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
954 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
955 spa->spa_uberblock = *up;
958 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
970 for (v = 0; v < 32; v++)
977 zio_read_gang(spa_t *spa, const blkptr_t *bp, void *buf)
980 zio_gbh_phys_t zio_gb;
984 /* Artificial BP for gang block header. */
986 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
987 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
988 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
989 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
990 for (i = 0; i < SPA_DVAS_PER_BP; i++)
991 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
993 /* Read gang header block using the artificial BP. */
994 if (zio_read(spa, &gbh_bp, &zio_gb))
998 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
999 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1001 if (BP_IS_HOLE(gbp))
1003 if (zio_read(spa, gbp, pbuf))
1005 pbuf += BP_GET_PSIZE(gbp);
1008 if (zio_checksum_verify(bp, buf))
1014 zio_read(spa_t *spa, const blkptr_t *bp, void *buf)
1016 int cpfunc = BP_GET_COMPRESS(bp);
1017 uint64_t align, size;
1023 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1024 const dva_t *dva = &bp->blk_dva[i];
1029 if (!dva->dva_word[0] && !dva->dva_word[1])
1032 vdevid = DVA_GET_VDEV(dva);
1033 offset = DVA_GET_OFFSET(dva);
1034 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1035 if (vdev->v_id == vdevid)
1038 if (!vdev || !vdev->v_read)
1041 size = BP_GET_PSIZE(bp);
1042 if (vdev->v_read == vdev_raidz_read) {
1043 align = 1ULL << vdev->v_top->v_ashift;
1044 if (P2PHASE(size, align) != 0)
1045 size = P2ROUNDUP(size, align);
1047 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1048 pbuf = zfs_alloc(size);
1052 if (DVA_GET_GANG(dva))
1053 error = zio_read_gang(spa, bp, pbuf);
1055 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1057 if (cpfunc != ZIO_COMPRESS_OFF)
1058 error = zio_decompress_data(cpfunc, pbuf,
1059 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1060 else if (size != BP_GET_PSIZE(bp))
1061 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1064 zfs_free(pbuf, size);
1069 printf("ZFS: i/o error - all block copies unavailable\n");
1074 dnode_read(spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1076 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1077 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1078 int nlevels = dnode->dn_nlevels;
1082 * Note: bsize may not be a power of two here so we need to do an
1083 * actual divide rather than a bitshift.
1085 while (buflen > 0) {
1086 uint64_t bn = offset / bsize;
1087 int boff = offset % bsize;
1089 const blkptr_t *indbp;
1092 if (bn > dnode->dn_maxblkid)
1095 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1098 indbp = dnode->dn_blkptr;
1099 for (i = 0; i < nlevels; i++) {
1101 * Copy the bp from the indirect array so that
1102 * we can re-use the scratch buffer for multi-level
1105 ibn = bn >> ((nlevels - i - 1) * ibshift);
1106 ibn &= ((1 << ibshift) - 1);
1108 rc = zio_read(spa, &bp, dnode_cache_buf);
1111 indbp = (const blkptr_t *) dnode_cache_buf;
1113 dnode_cache_obj = dnode;
1114 dnode_cache_bn = bn;
1118 * The buffer contains our data block. Copy what we
1119 * need from it and loop.
1122 if (i > buflen) i = buflen;
1123 memcpy(buf, &dnode_cache_buf[boff], i);
1124 buf = ((char*) buf) + i;
1133 * Lookup a value in a microzap directory. Assumes that the zap
1134 * scratch buffer contains the directory contents.
1137 mzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1139 const mzap_phys_t *mz;
1140 const mzap_ent_phys_t *mze;
1145 * Microzap objects use exactly one block. Read the whole
1148 size = dnode->dn_datablkszsec * 512;
1150 mz = (const mzap_phys_t *) zap_scratch;
1151 chunks = size / MZAP_ENT_LEN - 1;
1153 for (i = 0; i < chunks; i++) {
1154 mze = &mz->mz_chunk[i];
1155 if (!strcmp(mze->mze_name, name)) {
1156 *value = mze->mze_value;
1165 * Compare a name with a zap leaf entry. Return non-zero if the name
1169 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1172 const zap_leaf_chunk_t *nc;
1175 namelen = zc->l_entry.le_name_length;
1177 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1179 while (namelen > 0) {
1182 if (len > ZAP_LEAF_ARRAY_BYTES)
1183 len = ZAP_LEAF_ARRAY_BYTES;
1184 if (memcmp(p, nc->l_array.la_array, len))
1188 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1195 * Extract a uint64_t value from a zap leaf entry.
1198 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1200 const zap_leaf_chunk_t *vc;
1205 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1206 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1207 value = (value << 8) | p[i];
1214 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1215 * buffer contains the directory header.
1218 fzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1220 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1221 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1227 if (zh.zap_magic != ZAP_MAGIC)
1230 z.zap_block_shift = ilog2(bsize);
1231 z.zap_phys = (zap_phys_t *) zap_scratch;
1234 * Figure out where the pointer table is and read it in if necessary.
1236 if (zh.zap_ptrtbl.zt_blk) {
1237 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1238 zap_scratch, bsize);
1241 ptrtbl = (uint64_t *) zap_scratch;
1243 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1246 hash = zap_hash(zh.zap_salt, name);
1249 zl.l_bs = z.zap_block_shift;
1251 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1252 zap_leaf_chunk_t *zc;
1254 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1258 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1261 * Make sure this chunk matches our hash.
1263 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1264 && zl.l_phys->l_hdr.lh_prefix
1265 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1269 * Hash within the chunk to find our entry.
1271 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1272 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1273 h = zl.l_phys->l_hash[h];
1276 zc = &ZAP_LEAF_CHUNK(&zl, h);
1277 while (zc->l_entry.le_hash != hash) {
1278 if (zc->l_entry.le_next == 0xffff) {
1282 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1284 if (fzap_name_equal(&zl, zc, name)) {
1285 *value = fzap_leaf_value(&zl, zc);
1293 * Lookup a name in a zap object and return its value as a uint64_t.
1296 zap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1300 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1302 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1306 zap_type = *(uint64_t *) zap_scratch;
1307 if (zap_type == ZBT_MICRO)
1308 return mzap_lookup(spa, dnode, name, value);
1309 else if (zap_type == ZBT_HEADER)
1310 return fzap_lookup(spa, dnode, name, value);
1311 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1318 * List a microzap directory. Assumes that the zap scratch buffer contains
1319 * the directory contents.
1322 mzap_list(spa_t *spa, const dnode_phys_t *dnode)
1324 const mzap_phys_t *mz;
1325 const mzap_ent_phys_t *mze;
1330 * Microzap objects use exactly one block. Read the whole
1333 size = dnode->dn_datablkszsec * 512;
1334 mz = (const mzap_phys_t *) zap_scratch;
1335 chunks = size / MZAP_ENT_LEN - 1;
1337 for (i = 0; i < chunks; i++) {
1338 mze = &mz->mz_chunk[i];
1339 if (mze->mze_name[0])
1340 //printf("%-32s 0x%llx\n", mze->mze_name, mze->mze_value);
1341 printf("%s\n", mze->mze_name);
1348 * List a fatzap directory. Assumes that the zap scratch buffer contains
1349 * the directory header.
1352 fzap_list(spa_t *spa, const dnode_phys_t *dnode)
1354 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1355 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1359 if (zh.zap_magic != ZAP_MAGIC)
1362 z.zap_block_shift = ilog2(bsize);
1363 z.zap_phys = (zap_phys_t *) zap_scratch;
1366 * This assumes that the leaf blocks start at block 1. The
1367 * documentation isn't exactly clear on this.
1370 zl.l_bs = z.zap_block_shift;
1371 for (i = 0; i < zh.zap_num_leafs; i++) {
1372 off_t off = (i + 1) << zl.l_bs;
1376 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1379 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1381 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1382 zap_leaf_chunk_t *zc, *nc;
1385 zc = &ZAP_LEAF_CHUNK(&zl, j);
1386 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1388 namelen = zc->l_entry.le_name_length;
1389 if (namelen > sizeof(name))
1390 namelen = sizeof(name);
1393 * Paste the name back together.
1395 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1397 while (namelen > 0) {
1400 if (len > ZAP_LEAF_ARRAY_BYTES)
1401 len = ZAP_LEAF_ARRAY_BYTES;
1402 memcpy(p, nc->l_array.la_array, len);
1405 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1409 * Assume the first eight bytes of the value are
1412 value = fzap_leaf_value(&zl, zc);
1414 printf("%s 0x%llx\n", name, value);
1422 * List a zap directory.
1425 zap_list(spa_t *spa, const dnode_phys_t *dnode)
1428 size_t size = dnode->dn_datablkszsec * 512;
1430 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1433 zap_type = *(uint64_t *) zap_scratch;
1434 if (zap_type == ZBT_MICRO)
1435 return mzap_list(spa, dnode);
1437 return fzap_list(spa, dnode);
1443 objset_get_dnode(spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1447 offset = objnum * sizeof(dnode_phys_t);
1448 return dnode_read(spa, &os->os_meta_dnode, offset,
1449 dnode, sizeof(dnode_phys_t));
1453 * Find the object set given the object number of its dataset object
1454 * and return its details in *objset
1457 zfs_mount_dataset(spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1459 dnode_phys_t dataset;
1460 dsl_dataset_phys_t *ds;
1462 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1463 printf("ZFS: can't find dataset %llu\n", objnum);
1467 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1468 if (zio_read(spa, &ds->ds_bp, objset)) {
1469 printf("ZFS: can't read object set for dataset %llu\n", objnum);
1477 * Find the object set pointed to by the BOOTFS property or the root
1478 * dataset if there is none and return its details in *objset
1481 zfs_mount_root(spa_t *spa, objset_phys_t *objset)
1483 dnode_phys_t dir, propdir;
1484 uint64_t props, bootfs, root;
1487 * Start with the MOS directory object.
1489 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1490 printf("ZFS: can't read MOS object directory\n");
1495 * Lookup the pool_props and see if we can find a bootfs.
1497 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1498 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1499 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1501 return zfs_mount_dataset(spa, bootfs, objset);
1504 * Lookup the root dataset directory
1506 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1507 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1508 printf("ZFS: can't find root dsl_dir\n");
1513 * Use the information from the dataset directory's bonus buffer
1514 * to find the dataset object and from that the object set itself.
1516 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1517 return zfs_mount_dataset(spa, dd->dd_head_dataset_obj, objset);
1521 zfs_mount_pool(spa_t *spa)
1525 * Find the MOS and work our way in from there.
1527 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1528 printf("ZFS: can't read MOS\n");
1533 * Find the root object set
1535 if (zfs_mount_root(spa, &spa->spa_root_objset)) {
1536 printf("Can't find root filesystem - giving up\n");
1544 zfs_dnode_stat(spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1547 if (dn->dn_bonustype != DMU_OT_SA) {
1548 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1550 sb->st_mode = zp->zp_mode;
1551 sb->st_uid = zp->zp_uid;
1552 sb->st_gid = zp->zp_gid;
1553 sb->st_size = zp->zp_size;
1555 sa_hdr_phys_t *sahdrp;
1560 if (dn->dn_bonuslen != 0)
1561 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1563 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1564 blkptr_t *bp = &dn->dn_spill;
1567 size = BP_GET_LSIZE(bp);
1568 buf = zfs_alloc(size);
1569 error = zio_read(spa, bp, buf);
1571 zfs_free(buf, size);
1579 hdrsize = SA_HDR_SIZE(sahdrp);
1580 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1582 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1584 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1586 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1589 zfs_free(buf, size);
1596 * Lookup a file and return its dnode.
1599 zfs_lookup(spa_t *spa, const char *upath, dnode_phys_t *dnode)
1602 uint64_t objnum, rootnum, parentnum;
1607 int symlinks_followed = 0;
1610 if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
1611 printf("ZFS: unexpected object set type %llu\n",
1612 spa->spa_root_objset.os_type);
1617 * Get the root directory dnode.
1619 rc = objset_get_dnode(spa, &spa->spa_root_objset, MASTER_NODE_OBJ, &dn);
1623 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1627 rc = objset_get_dnode(spa, &spa->spa_root_objset, rootnum, &dn);
1640 memcpy(element, p, q - p);
1648 rc = zfs_dnode_stat(spa, &dn, &sb);
1651 if (!S_ISDIR(sb.st_mode))
1655 rc = zap_lookup(spa, &dn, element, &objnum);
1658 objnum = ZFS_DIRENT_OBJ(objnum);
1660 rc = objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1665 * Check for symlink.
1667 rc = zfs_dnode_stat(spa, &dn, &sb);
1670 if (S_ISLNK(sb.st_mode)) {
1671 if (symlinks_followed > 10)
1673 symlinks_followed++;
1676 * Read the link value and copy the tail of our
1677 * current path onto the end.
1680 strcpy(&path[sb.st_size], p);
1682 path[sb.st_size] = 0;
1683 if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1684 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1687 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
1693 * Restart with the new path, starting either at
1694 * the root or at the parent depending whether or
1695 * not the link is relative.
1702 objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);