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.
38 * List of all vdevs, chained through v_alllink.
40 static vdev_list_t zfs_vdevs;
43 * List of all pools, chained through spa_link.
45 static spa_list_t zfs_pools;
47 static uint64_t zfs_crc64_table[256];
48 static const dnode_phys_t *dnode_cache_obj = 0;
49 static uint64_t dnode_cache_bn;
50 static char *dnode_cache_buf;
51 static char *zap_scratch;
52 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
54 #define TEMP_SIZE (1024 * 1024)
56 static int zio_read(spa_t *spa, const blkptr_t *bp, void *buf);
61 STAILQ_INIT(&zfs_vdevs);
62 STAILQ_INIT(&zfs_pools);
64 zfs_temp_buf = malloc(TEMP_SIZE);
65 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
66 zfs_temp_ptr = zfs_temp_buf;
67 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
68 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
74 zfs_alloc_temp(size_t sz)
78 if (zfs_temp_ptr + sz > zfs_temp_end) {
79 printf("ZFS: out of temporary buffer space\n");
92 zfs_temp_ptr = zfs_temp_buf;
96 xdr_int(const unsigned char **xdr, int *ip)
98 *ip = ((*xdr)[0] << 24)
107 xdr_u_int(const unsigned char **xdr, u_int *ip)
109 *ip = ((*xdr)[0] << 24)
118 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
124 *lp = (((uint64_t) hi) << 32) | lo;
129 nvlist_find(const unsigned char *nvlist, const char *name, int type,
130 int* elementsp, void *valuep)
132 const unsigned char *p, *pair;
134 int encoded_size, decoded_size;
141 xdr_int(&p, &encoded_size);
142 xdr_int(&p, &decoded_size);
143 while (encoded_size && decoded_size) {
144 int namelen, pairtype, elements;
145 const char *pairname;
147 xdr_int(&p, &namelen);
148 pairname = (const char*) p;
149 p += roundup(namelen, 4);
150 xdr_int(&p, &pairtype);
152 if (!memcmp(name, pairname, namelen) && type == pairtype) {
153 xdr_int(&p, &elements);
155 *elementsp = elements;
156 if (type == DATA_TYPE_UINT64) {
157 xdr_uint64_t(&p, (uint64_t *) valuep);
159 } else if (type == DATA_TYPE_STRING) {
162 (*(const char**) valuep) = (const char*) p;
164 } else if (type == DATA_TYPE_NVLIST
165 || type == DATA_TYPE_NVLIST_ARRAY) {
166 (*(const unsigned char**) valuep) =
167 (const unsigned char*) p;
174 * Not the pair we are looking for, skip to the next one.
176 p = pair + encoded_size;
180 xdr_int(&p, &encoded_size);
181 xdr_int(&p, &decoded_size);
188 * Return the next nvlist in an nvlist array.
190 static const unsigned char *
191 nvlist_next(const unsigned char *nvlist)
193 const unsigned char *p, *pair;
195 int encoded_size, decoded_size;
202 xdr_int(&p, &encoded_size);
203 xdr_int(&p, &decoded_size);
204 while (encoded_size && decoded_size) {
205 p = pair + encoded_size;
208 xdr_int(&p, &encoded_size);
209 xdr_int(&p, &decoded_size);
217 static const unsigned char *
218 nvlist_print(const unsigned char *nvlist, unsigned int indent)
220 static const char* typenames[] = {
231 "DATA_TYPE_BYTE_ARRAY",
232 "DATA_TYPE_INT16_ARRAY",
233 "DATA_TYPE_UINT16_ARRAY",
234 "DATA_TYPE_INT32_ARRAY",
235 "DATA_TYPE_UINT32_ARRAY",
236 "DATA_TYPE_INT64_ARRAY",
237 "DATA_TYPE_UINT64_ARRAY",
238 "DATA_TYPE_STRING_ARRAY",
241 "DATA_TYPE_NVLIST_ARRAY",
242 "DATA_TYPE_BOOLEAN_VALUE",
245 "DATA_TYPE_BOOLEAN_ARRAY",
246 "DATA_TYPE_INT8_ARRAY",
247 "DATA_TYPE_UINT8_ARRAY"
251 const unsigned char *p, *pair;
253 int encoded_size, decoded_size;
260 xdr_int(&p, &encoded_size);
261 xdr_int(&p, &decoded_size);
262 while (encoded_size && decoded_size) {
263 int namelen, pairtype, elements;
264 const char *pairname;
266 xdr_int(&p, &namelen);
267 pairname = (const char*) p;
268 p += roundup(namelen, 4);
269 xdr_int(&p, &pairtype);
271 for (i = 0; i < indent; i++)
273 printf("%s %s", typenames[pairtype], pairname);
275 xdr_int(&p, &elements);
277 case DATA_TYPE_UINT64: {
279 xdr_uint64_t(&p, &val);
280 printf(" = 0x%llx\n", val);
284 case DATA_TYPE_STRING: {
287 printf(" = \"%s\"\n", p);
291 case DATA_TYPE_NVLIST:
293 nvlist_print(p, indent + 1);
296 case DATA_TYPE_NVLIST_ARRAY:
297 for (j = 0; j < elements; j++) {
299 p = nvlist_print(p, indent + 1);
300 if (j != elements - 1) {
301 for (i = 0; i < indent; i++)
303 printf("%s %s", typenames[pairtype], pairname);
312 p = pair + encoded_size;
315 xdr_int(&p, &encoded_size);
316 xdr_int(&p, &decoded_size);
325 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
326 off_t offset, size_t size)
332 psize = BP_GET_PSIZE(bp);
337 /*printf("ZFS: reading %d bytes at 0x%llx to %p\n", psize, offset, buf);*/
338 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
341 if (bp && zio_checksum_error(bp, buf))
348 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
349 off_t offset, size_t bytes)
352 return (vdev_read_phys(vdev, bp, buf,
353 offset + VDEV_LABEL_START_SIZE, bytes));
358 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
359 off_t offset, size_t bytes)
365 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
366 if (kid->v_state != VDEV_STATE_HEALTHY)
368 rc = kid->v_read(kid, bp, buf, offset, bytes);
377 vdev_find(uint64_t guid)
381 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
382 if (vdev->v_guid == guid)
389 vdev_create(uint64_t guid, vdev_read_t *read)
393 vdev = malloc(sizeof(vdev_t));
394 memset(vdev, 0, sizeof(vdev_t));
395 STAILQ_INIT(&vdev->v_children);
397 vdev->v_state = VDEV_STATE_OFFLINE;
399 vdev->v_phys_read = 0;
400 vdev->v_read_priv = 0;
401 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
407 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t **vdevp, int is_newer)
410 uint64_t guid, id, ashift, nparity;
414 const unsigned char *kids;
415 int nkids, i, is_new;
416 uint64_t is_offline, is_faulted, is_degraded, is_removed;
418 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
419 DATA_TYPE_UINT64, 0, &guid)
420 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
421 DATA_TYPE_UINT64, 0, &id)
422 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
423 DATA_TYPE_STRING, 0, &type)) {
424 printf("ZFS: can't find vdev details\n");
428 if (strcmp(type, VDEV_TYPE_MIRROR)
429 && strcmp(type, VDEV_TYPE_DISK)
430 && strcmp(type, VDEV_TYPE_RAIDZ)) {
431 printf("ZFS: can only boot from disk, mirror or raidz vdevs\n");
435 is_offline = is_removed = is_faulted = is_degraded = 0;
437 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
439 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
441 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
443 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
446 vdev = vdev_find(guid);
450 if (!strcmp(type, VDEV_TYPE_MIRROR))
451 vdev = vdev_create(guid, vdev_mirror_read);
452 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
453 vdev = vdev_create(guid, vdev_raidz_read);
455 vdev = vdev_create(guid, vdev_disk_read);
458 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
459 DATA_TYPE_UINT64, 0, &ashift) == 0)
460 vdev->v_ashift = ashift;
463 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
464 DATA_TYPE_UINT64, 0, &nparity) == 0)
465 vdev->v_nparity = nparity;
468 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
469 DATA_TYPE_STRING, 0, &path) == 0) {
477 vdev->v_name = strdup(path);
479 if (!strcmp(type, "raidz")) {
480 if (vdev->v_nparity == 1)
481 vdev->v_name = "raidz1";
483 vdev->v_name = "raidz2";
485 vdev->v_name = strdup(type);
490 vdev->v_state = VDEV_STATE_OFFLINE;
492 vdev->v_state = VDEV_STATE_REMOVED;
494 vdev->v_state = VDEV_STATE_FAULTED;
495 else if (is_degraded)
496 vdev->v_state = VDEV_STATE_DEGRADED;
498 vdev->v_state = VDEV_STATE_HEALTHY;
504 * We've already seen this vdev, but from an older
505 * vdev label, so let's refresh its state from the
509 vdev->v_state = VDEV_STATE_OFFLINE;
511 vdev->v_state = VDEV_STATE_REMOVED;
513 vdev->v_state = VDEV_STATE_FAULTED;
514 else if (is_degraded)
515 vdev->v_state = VDEV_STATE_DEGRADED;
517 vdev->v_state = VDEV_STATE_HEALTHY;
521 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
522 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
524 * Its ok if we don't have any kids.
527 vdev->v_nchildren = nkids;
528 for (i = 0; i < nkids; i++) {
529 rc = vdev_init_from_nvlist(kids, &kid, is_newer);
533 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
535 kids = nvlist_next(kids);
538 vdev->v_nchildren = 0;
547 vdev_set_state(vdev_t *vdev)
554 * A mirror or raidz is healthy if all its kids are healthy. A
555 * mirror is degraded if any of its kids is healthy; a raidz
556 * is degraded if at most nparity kids are offline.
558 if (STAILQ_FIRST(&vdev->v_children)) {
561 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
562 if (kid->v_state == VDEV_STATE_HEALTHY)
568 vdev->v_state = VDEV_STATE_HEALTHY;
570 if (vdev->v_read == vdev_mirror_read) {
572 vdev->v_state = VDEV_STATE_DEGRADED;
574 vdev->v_state = VDEV_STATE_OFFLINE;
576 } else if (vdev->v_read == vdev_raidz_read) {
577 if (bad_kids > vdev->v_nparity) {
578 vdev->v_state = VDEV_STATE_OFFLINE;
580 vdev->v_state = VDEV_STATE_DEGRADED;
588 spa_find_by_guid(uint64_t guid)
592 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
593 if (spa->spa_guid == guid)
602 spa_find_by_name(const char *name)
606 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
607 if (!strcmp(spa->spa_name, name))
616 spa_create(uint64_t guid)
620 spa = malloc(sizeof(spa_t));
621 memset(spa, 0, sizeof(spa_t));
622 STAILQ_INIT(&spa->spa_vdevs);
623 spa->spa_guid = guid;
624 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
630 state_name(vdev_state_t state)
632 static const char* names[] = {
647 #define pager_printf printf
652 pager_printf(const char *fmt, ...)
658 vsprintf(line, fmt, args);
665 #define STATUS_FORMAT " %-16s %-10s\n"
668 print_state(int indent, const char *name, vdev_state_t state)
674 for (i = 0; i < indent; i++)
677 pager_printf(STATUS_FORMAT, buf, state_name(state));
682 vdev_status(vdev_t *vdev, int indent)
685 print_state(indent, vdev->v_name, vdev->v_state);
687 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
688 vdev_status(kid, indent + 1);
693 spa_status(spa_t *spa)
696 int good_kids, bad_kids, degraded_kids;
699 pager_printf(" pool: %s\n", spa->spa_name);
700 pager_printf("config:\n\n");
701 pager_printf(STATUS_FORMAT, "NAME", "STATE");
706 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
707 if (vdev->v_state == VDEV_STATE_HEALTHY)
709 else if (vdev->v_state == VDEV_STATE_DEGRADED)
715 state = VDEV_STATE_CLOSED;
716 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
717 state = VDEV_STATE_HEALTHY;
718 else if ((good_kids + degraded_kids) > 0)
719 state = VDEV_STATE_DEGRADED;
721 print_state(0, spa->spa_name, state);
722 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
723 vdev_status(vdev, 1);
733 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
742 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
745 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
747 vdev_t *vdev, *top_vdev, *pool_vdev;
750 const unsigned char *nvlist;
753 uint64_t pool_txg, pool_guid;
754 const char *pool_name;
755 const unsigned char *vdevs;
758 const struct uberblock *up;
761 * Load the vdev label and figure out which
762 * uberblock is most current.
764 memset(&vtmp, 0, sizeof(vtmp));
765 vtmp.v_phys_read = read;
766 vtmp.v_read_priv = read_priv;
767 off = offsetof(vdev_label_t, vl_vdev_phys);
769 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
770 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
771 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
772 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
773 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
774 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
777 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
781 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
783 if (nvlist_find(nvlist,
784 ZPOOL_CONFIG_VERSION,
785 DATA_TYPE_UINT64, 0, &val)) {
789 if (val > SPA_VERSION) {
790 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
791 (unsigned) val, (unsigned) SPA_VERSION);
795 if (nvlist_find(nvlist,
796 ZPOOL_CONFIG_POOL_STATE,
797 DATA_TYPE_UINT64, 0, &val)) {
802 if (val != POOL_STATE_ACTIVE) {
804 * Don't print a message here. If we happen to reboot
805 * while where is an exported pool around, we don't
806 * need a cascade of confusing messages during boot.
808 /*printf("ZFS: pool is not active\n");*/
813 if (nvlist_find(nvlist,
814 ZPOOL_CONFIG_POOL_TXG,
815 DATA_TYPE_UINT64, 0, &pool_txg)
816 || nvlist_find(nvlist,
817 ZPOOL_CONFIG_POOL_GUID,
818 DATA_TYPE_UINT64, 0, &pool_guid)
819 || nvlist_find(nvlist,
820 ZPOOL_CONFIG_POOL_NAME,
821 DATA_TYPE_STRING, 0, &pool_name)) {
823 * Cache and spare devices end up here - just ignore
826 /*printf("ZFS: can't find pool details\n");*/
831 * Create the pool if this is the first time we've seen it.
833 spa = spa_find_by_guid(pool_guid);
835 spa = spa_create(pool_guid);
836 spa->spa_name = strdup(pool_name);
838 if (pool_txg > spa->spa_txg) {
839 spa->spa_txg = pool_txg;
845 * Get the vdev tree and create our in-core copy of it.
846 * If we already have a vdev with this guid, this must
847 * be some kind of alias (overlapping slices, dangerously dedicated
850 if (nvlist_find(nvlist,
852 DATA_TYPE_UINT64, 0, &guid)) {
855 vdev = vdev_find(guid);
856 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
859 if (nvlist_find(nvlist,
860 ZPOOL_CONFIG_VDEV_TREE,
861 DATA_TYPE_NVLIST, 0, &vdevs)) {
865 rc = vdev_init_from_nvlist(vdevs, &top_vdev, is_newer);
870 * Add the toplevel vdev to the pool if its not already there.
872 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
873 if (top_vdev == pool_vdev)
875 if (!pool_vdev && top_vdev)
876 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
879 * We should already have created an incomplete vdev for this
880 * vdev. Find it and initialise it with our read proc.
882 vdev = vdev_find(guid);
884 vdev->v_phys_read = read;
885 vdev->v_read_priv = read_priv;
887 printf("ZFS: inconsistent nvlist contents\n");
892 * Re-evaluate top-level vdev state.
894 vdev_set_state(top_vdev);
897 * Ok, we are happy with the pool so far. Lets find
898 * the best uberblock and then we can actually access
899 * the contents of the pool.
902 i < VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT;
904 off = offsetof(vdev_label_t, vl_uberblock);
905 off += i << UBERBLOCK_SHIFT;
907 DVA_SET_OFFSET(&bp.blk_dva[0], off);
908 BP_SET_LSIZE(&bp, 1 << UBERBLOCK_SHIFT);
909 BP_SET_PSIZE(&bp, 1 << UBERBLOCK_SHIFT);
910 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
911 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
912 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
913 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
916 up = (const struct uberblock *) upbuf;
917 if (up->ub_magic != UBERBLOCK_MAGIC)
919 if (up->ub_txg < spa->spa_txg)
921 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
922 spa->spa_uberblock = *up;
923 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
924 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
925 spa->spa_uberblock = *up;
939 for (v = 0; v < 32; v++)
946 zio_read_gang(spa_t *spa, const blkptr_t *bp, const dva_t *dva, void *buf)
948 zio_gbh_phys_t zio_gb;
954 vdevid = DVA_GET_VDEV(dva);
955 offset = DVA_GET_OFFSET(dva);
956 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
957 if (vdev->v_id == vdevid)
959 if (!vdev || !vdev->v_read)
961 if (vdev->v_read(vdev, bp, &zio_gb, offset, SPA_GANGBLOCKSIZE))
964 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
965 if (zio_read(spa, &zio_gb.zg_blkptr[i], buf))
973 zio_read(spa_t *spa, const blkptr_t *bp, void *buf)
975 int cpfunc = BP_GET_COMPRESS(bp);
976 size_t lsize = BP_GET_LSIZE(bp);
977 size_t psize = BP_GET_PSIZE(bp);
982 if (cpfunc != ZIO_COMPRESS_OFF)
983 pbuf = zfs_alloc_temp(psize);
987 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
988 const dva_t *dva = &bp->blk_dva[i];
993 if (!dva->dva_word[0] && !dva->dva_word[1])
996 if (DVA_GET_GANG(dva)) {
997 printf("ZFS: gang block detected!\n");
998 if (zio_read_gang(spa, bp, dva, buf))
1001 vdevid = DVA_GET_VDEV(dva);
1002 offset = DVA_GET_OFFSET(dva);
1003 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
1004 if (vdev->v_id == vdevid)
1006 if (!vdev || !vdev->v_read) {
1009 if (vdev->v_read(vdev, bp, pbuf, offset, psize))
1012 if (cpfunc != ZIO_COMPRESS_OFF) {
1013 if (zio_decompress_data(cpfunc, pbuf, psize,
1021 printf("ZFS: i/o error - all block copies unavailable\n");
1027 dnode_read(spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1029 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1030 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1031 int nlevels = dnode->dn_nlevels;
1035 * Note: bsize may not be a power of two here so we need to do an
1036 * actual divide rather than a bitshift.
1038 while (buflen > 0) {
1039 uint64_t bn = offset / bsize;
1040 int boff = offset % bsize;
1042 const blkptr_t *indbp;
1045 if (bn > dnode->dn_maxblkid)
1048 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1051 indbp = dnode->dn_blkptr;
1052 for (i = 0; i < nlevels; i++) {
1054 * Copy the bp from the indirect array so that
1055 * we can re-use the scratch buffer for multi-level
1058 ibn = bn >> ((nlevels - i - 1) * ibshift);
1059 ibn &= ((1 << ibshift) - 1);
1061 rc = zio_read(spa, &bp, dnode_cache_buf);
1064 indbp = (const blkptr_t *) dnode_cache_buf;
1066 dnode_cache_obj = dnode;
1067 dnode_cache_bn = bn;
1071 * The buffer contains our data block. Copy what we
1072 * need from it and loop.
1075 if (i > buflen) i = buflen;
1076 memcpy(buf, &dnode_cache_buf[boff], i);
1077 buf = ((char*) buf) + i;
1086 * Lookup a value in a microzap directory. Assumes that the zap
1087 * scratch buffer contains the directory contents.
1090 mzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1092 const mzap_phys_t *mz;
1093 const mzap_ent_phys_t *mze;
1098 * Microzap objects use exactly one block. Read the whole
1101 size = dnode->dn_datablkszsec * 512;
1103 mz = (const mzap_phys_t *) zap_scratch;
1104 chunks = size / MZAP_ENT_LEN - 1;
1106 for (i = 0; i < chunks; i++) {
1107 mze = &mz->mz_chunk[i];
1108 if (!strcmp(mze->mze_name, name)) {
1109 *value = mze->mze_value;
1118 * Compare a name with a zap leaf entry. Return non-zero if the name
1122 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1125 const zap_leaf_chunk_t *nc;
1128 namelen = zc->l_entry.le_name_length;
1130 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1132 while (namelen > 0) {
1135 if (len > ZAP_LEAF_ARRAY_BYTES)
1136 len = ZAP_LEAF_ARRAY_BYTES;
1137 if (memcmp(p, nc->l_array.la_array, len))
1141 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1148 * Extract a uint64_t value from a zap leaf entry.
1151 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1153 const zap_leaf_chunk_t *vc;
1158 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1159 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1160 value = (value << 8) | p[i];
1167 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1168 * buffer contains the directory header.
1171 fzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1173 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1174 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1180 if (zh.zap_magic != ZAP_MAGIC)
1183 z.zap_block_shift = ilog2(bsize);
1184 z.zap_phys = (zap_phys_t *) zap_scratch;
1187 * Figure out where the pointer table is and read it in if necessary.
1189 if (zh.zap_ptrtbl.zt_blk) {
1190 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1191 zap_scratch, bsize);
1194 ptrtbl = (uint64_t *) zap_scratch;
1196 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1199 hash = zap_hash(zh.zap_salt, name);
1202 zl.l_bs = z.zap_block_shift;
1204 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1205 zap_leaf_chunk_t *zc;
1207 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1211 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1214 * Make sure this chunk matches our hash.
1216 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1217 && zl.l_phys->l_hdr.lh_prefix
1218 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1222 * Hash within the chunk to find our entry.
1224 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1225 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1226 h = zl.l_phys->l_hash[h];
1229 zc = &ZAP_LEAF_CHUNK(&zl, h);
1230 while (zc->l_entry.le_hash != hash) {
1231 if (zc->l_entry.le_next == 0xffff) {
1235 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1237 if (fzap_name_equal(&zl, zc, name)) {
1238 *value = fzap_leaf_value(&zl, zc);
1246 * Lookup a name in a zap object and return its value as a uint64_t.
1249 zap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1253 size_t size = dnode->dn_datablkszsec * 512;
1255 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1259 zap_type = *(uint64_t *) zap_scratch;
1260 if (zap_type == ZBT_MICRO)
1261 return mzap_lookup(spa, dnode, name, value);
1263 return fzap_lookup(spa, dnode, name, value);
1269 * List a microzap directory. Assumes that the zap scratch buffer contains
1270 * the directory contents.
1273 mzap_list(spa_t *spa, const dnode_phys_t *dnode)
1275 const mzap_phys_t *mz;
1276 const mzap_ent_phys_t *mze;
1281 * Microzap objects use exactly one block. Read the whole
1284 size = dnode->dn_datablkszsec * 512;
1285 mz = (const mzap_phys_t *) zap_scratch;
1286 chunks = size / MZAP_ENT_LEN - 1;
1288 for (i = 0; i < chunks; i++) {
1289 mze = &mz->mz_chunk[i];
1290 if (mze->mze_name[0])
1291 //printf("%-32s 0x%llx\n", mze->mze_name, mze->mze_value);
1292 printf("%s\n", mze->mze_name);
1299 * List a fatzap directory. Assumes that the zap scratch buffer contains
1300 * the directory header.
1303 fzap_list(spa_t *spa, const dnode_phys_t *dnode)
1305 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1306 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1310 if (zh.zap_magic != ZAP_MAGIC)
1313 z.zap_block_shift = ilog2(bsize);
1314 z.zap_phys = (zap_phys_t *) zap_scratch;
1317 * This assumes that the leaf blocks start at block 1. The
1318 * documentation isn't exactly clear on this.
1321 zl.l_bs = z.zap_block_shift;
1322 for (i = 0; i < zh.zap_num_leafs; i++) {
1323 off_t off = (i + 1) << zl.l_bs;
1327 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1330 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1332 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1333 zap_leaf_chunk_t *zc, *nc;
1336 zc = &ZAP_LEAF_CHUNK(&zl, j);
1337 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1339 namelen = zc->l_entry.le_name_length;
1340 if (namelen > sizeof(name))
1341 namelen = sizeof(name);
1344 * Paste the name back together.
1346 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1348 while (namelen > 0) {
1351 if (len > ZAP_LEAF_ARRAY_BYTES)
1352 len = ZAP_LEAF_ARRAY_BYTES;
1353 memcpy(p, nc->l_array.la_array, len);
1356 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1360 * Assume the first eight bytes of the value are
1363 value = fzap_leaf_value(&zl, zc);
1365 printf("%-32s 0x%llx\n", name, value);
1373 * List a zap directory.
1376 zap_list(spa_t *spa, const dnode_phys_t *dnode)
1379 size_t size = dnode->dn_datablkszsec * 512;
1381 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1384 zap_type = *(uint64_t *) zap_scratch;
1385 if (zap_type == ZBT_MICRO)
1386 return mzap_list(spa, dnode);
1388 return fzap_list(spa, dnode);
1394 objset_get_dnode(spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1398 offset = objnum * sizeof(dnode_phys_t);
1399 return dnode_read(spa, &os->os_meta_dnode, offset,
1400 dnode, sizeof(dnode_phys_t));
1404 * Find the object set given the object number of its dataset object
1405 * and return its details in *objset
1408 zfs_mount_dataset(spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1410 dnode_phys_t dataset;
1411 dsl_dataset_phys_t *ds;
1413 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1414 printf("ZFS: can't find dataset %llu\n", objnum);
1418 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1419 if (zio_read(spa, &ds->ds_bp, objset)) {
1420 printf("ZFS: can't read object set for dataset %llu\n", objnum);
1428 * Find the object set pointed to by the BOOTFS property or the root
1429 * dataset if there is none and return its details in *objset
1432 zfs_mount_root(spa_t *spa, objset_phys_t *objset)
1434 dnode_phys_t dir, propdir;
1435 uint64_t props, bootfs, root;
1438 * Start with the MOS directory object.
1440 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1441 printf("ZFS: can't read MOS object directory\n");
1446 * Lookup the pool_props and see if we can find a bootfs.
1448 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1449 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1450 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1452 return zfs_mount_dataset(spa, bootfs, objset);
1455 * Lookup the root dataset directory
1457 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1458 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1459 printf("ZFS: can't find root dsl_dir\n");
1464 * Use the information from the dataset directory's bonus buffer
1465 * to find the dataset object and from that the object set itself.
1467 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1468 return zfs_mount_dataset(spa, dd->dd_head_dataset_obj, objset);
1472 zfs_mount_pool(spa_t *spa)
1475 * Find the MOS and work our way in from there.
1477 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1478 printf("ZFS: can't read MOS\n");
1483 * Find the root object set
1485 if (zfs_mount_root(spa, &spa->spa_root_objset)) {
1486 printf("Can't find root filesystem - giving up\n");
1494 * Lookup a file and return its dnode.
1497 zfs_lookup(spa_t *spa, const char *upath, dnode_phys_t *dnode)
1500 uint64_t objnum, rootnum, parentnum;
1502 const znode_phys_t *zp = (const znode_phys_t *) dn.dn_bonus;
1506 int symlinks_followed = 0;
1508 if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
1509 printf("ZFS: unexpected object set type %llu\n",
1510 spa->spa_root_objset.os_type);
1515 * Get the root directory dnode.
1517 rc = objset_get_dnode(spa, &spa->spa_root_objset, MASTER_NODE_OBJ, &dn);
1521 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1525 rc = objset_get_dnode(spa, &spa->spa_root_objset, rootnum, &dn);
1538 memcpy(element, p, q - p);
1546 if ((zp->zp_mode >> 12) != 0x4) {
1551 rc = zap_lookup(spa, &dn, element, &objnum);
1554 objnum = ZFS_DIRENT_OBJ(objnum);
1556 rc = objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1561 * Check for symlink.
1563 if ((zp->zp_mode >> 12) == 0xa) {
1564 if (symlinks_followed > 10)
1566 symlinks_followed++;
1569 * Read the link value and copy the tail of our
1570 * current path onto the end.
1573 strcpy(&path[zp->zp_size], p);
1575 path[zp->zp_size] = 0;
1576 if (zp->zp_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1577 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1580 rc = dnode_read(spa, &dn, 0, path, zp->zp_size);
1586 * Restart with the new path, starting either at
1587 * the root or at the parent depending whether or
1588 * not the link is relative.
1595 objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);