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 (1*SPA_MAXBLOCKSIZE)
59 STAILQ_INIT(&zfs_vdevs);
60 STAILQ_INIT(&zfs_pools);
62 zfs_temp_buf = malloc(TEMP_SIZE);
63 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
64 zfs_temp_ptr = zfs_temp_buf;
65 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
66 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
72 zfs_alloc_temp(size_t sz)
76 if (zfs_temp_ptr + sz > zfs_temp_end) {
77 printf("ZFS: out of temporary buffer space\n");
90 zfs_temp_ptr = zfs_temp_buf;
94 xdr_int(const unsigned char **xdr, int *ip)
96 *ip = ((*xdr)[0] << 24)
105 xdr_u_int(const unsigned char **xdr, u_int *ip)
107 *ip = ((*xdr)[0] << 24)
116 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
122 *lp = (((uint64_t) hi) << 32) | lo;
127 nvlist_find(const unsigned char *nvlist, const char *name, int type,
128 int* elementsp, void *valuep)
130 const unsigned char *p, *pair;
132 int encoded_size, decoded_size;
139 xdr_int(&p, &encoded_size);
140 xdr_int(&p, &decoded_size);
141 while (encoded_size && decoded_size) {
142 int namelen, pairtype, elements;
143 const char *pairname;
145 xdr_int(&p, &namelen);
146 pairname = (const char*) p;
147 p += roundup(namelen, 4);
148 xdr_int(&p, &pairtype);
150 if (!memcmp(name, pairname, namelen) && type == pairtype) {
151 xdr_int(&p, &elements);
153 *elementsp = elements;
154 if (type == DATA_TYPE_UINT64) {
155 xdr_uint64_t(&p, (uint64_t *) valuep);
157 } else if (type == DATA_TYPE_STRING) {
160 (*(const char**) valuep) = (const char*) p;
162 } else if (type == DATA_TYPE_NVLIST
163 || type == DATA_TYPE_NVLIST_ARRAY) {
164 (*(const unsigned char**) valuep) =
165 (const unsigned char*) p;
172 * Not the pair we are looking for, skip to the next one.
174 p = pair + encoded_size;
178 xdr_int(&p, &encoded_size);
179 xdr_int(&p, &decoded_size);
186 * Return the next nvlist in an nvlist array.
188 static const unsigned char *
189 nvlist_next(const unsigned char *nvlist)
191 const unsigned char *p, *pair;
193 int encoded_size, decoded_size;
200 xdr_int(&p, &encoded_size);
201 xdr_int(&p, &decoded_size);
202 while (encoded_size && decoded_size) {
203 p = pair + encoded_size;
206 xdr_int(&p, &encoded_size);
207 xdr_int(&p, &decoded_size);
215 static const unsigned char *
216 nvlist_print(const unsigned char *nvlist, unsigned int indent)
218 static const char* typenames[] = {
229 "DATA_TYPE_BYTE_ARRAY",
230 "DATA_TYPE_INT16_ARRAY",
231 "DATA_TYPE_UINT16_ARRAY",
232 "DATA_TYPE_INT32_ARRAY",
233 "DATA_TYPE_UINT32_ARRAY",
234 "DATA_TYPE_INT64_ARRAY",
235 "DATA_TYPE_UINT64_ARRAY",
236 "DATA_TYPE_STRING_ARRAY",
239 "DATA_TYPE_NVLIST_ARRAY",
240 "DATA_TYPE_BOOLEAN_VALUE",
243 "DATA_TYPE_BOOLEAN_ARRAY",
244 "DATA_TYPE_INT8_ARRAY",
245 "DATA_TYPE_UINT8_ARRAY"
249 const unsigned char *p, *pair;
251 int encoded_size, decoded_size;
258 xdr_int(&p, &encoded_size);
259 xdr_int(&p, &decoded_size);
260 while (encoded_size && decoded_size) {
261 int namelen, pairtype, elements;
262 const char *pairname;
264 xdr_int(&p, &namelen);
265 pairname = (const char*) p;
266 p += roundup(namelen, 4);
267 xdr_int(&p, &pairtype);
269 for (i = 0; i < indent; i++)
271 printf("%s %s", typenames[pairtype], pairname);
273 xdr_int(&p, &elements);
275 case DATA_TYPE_UINT64: {
277 xdr_uint64_t(&p, &val);
278 printf(" = 0x%llx\n", val);
282 case DATA_TYPE_STRING: {
285 printf(" = \"%s\"\n", p);
289 case DATA_TYPE_NVLIST:
291 nvlist_print(p, indent + 1);
294 case DATA_TYPE_NVLIST_ARRAY:
295 for (j = 0; j < elements; j++) {
297 p = nvlist_print(p, indent + 1);
298 if (j != elements - 1) {
299 for (i = 0; i < indent; i++)
301 printf("%s %s", typenames[pairtype], pairname);
310 p = pair + encoded_size;
313 xdr_int(&p, &encoded_size);
314 xdr_int(&p, &decoded_size);
323 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
324 off_t offset, size_t size)
330 psize = BP_GET_PSIZE(bp);
335 /*printf("ZFS: reading %d bytes at 0x%llx to %p\n", psize, offset, buf);*/
336 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
339 if (bp && zio_checksum_error(bp, buf))
346 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
347 off_t offset, size_t bytes)
350 return (vdev_read_phys(vdev, bp, buf,
351 offset + VDEV_LABEL_START_SIZE, bytes));
356 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
357 off_t offset, size_t bytes)
363 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
364 if (kid->v_state != VDEV_STATE_HEALTHY)
366 rc = kid->v_read(kid, bp, buf, offset, bytes);
375 vdev_find(uint64_t guid)
379 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
380 if (vdev->v_guid == guid)
387 vdev_create(uint64_t guid, vdev_read_t *read)
391 vdev = malloc(sizeof(vdev_t));
392 memset(vdev, 0, sizeof(vdev_t));
393 STAILQ_INIT(&vdev->v_children);
395 vdev->v_state = VDEV_STATE_OFFLINE;
397 vdev->v_phys_read = 0;
398 vdev->v_read_priv = 0;
399 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
405 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t **vdevp)
408 uint64_t guid, id, ashift, nparity;
412 const unsigned char *kids;
415 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
416 DATA_TYPE_UINT64, 0, &guid)
417 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
418 DATA_TYPE_UINT64, 0, &id)
419 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
420 DATA_TYPE_STRING, 0, &type)) {
421 printf("ZFS: can't find vdev details\n");
426 * Assume that if we've seen this vdev tree before, this one
429 vdev = vdev_find(guid);
436 if (strcmp(type, VDEV_TYPE_MIRROR)
437 && strcmp(type, VDEV_TYPE_DISK)
438 && strcmp(type, VDEV_TYPE_RAIDZ)) {
439 printf("ZFS: can only boot from disk, mirror or raidz vdevs\n");
443 if (!strcmp(type, VDEV_TYPE_MIRROR))
444 vdev = vdev_create(guid, vdev_mirror_read);
445 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
446 vdev = vdev_create(guid, vdev_raidz_read);
448 vdev = vdev_create(guid, vdev_disk_read);
451 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
452 DATA_TYPE_UINT64, 0, &ashift) == 0)
453 vdev->v_ashift = ashift;
456 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
457 DATA_TYPE_UINT64, 0, &nparity) == 0)
458 vdev->v_nparity = nparity;
461 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
462 DATA_TYPE_STRING, 0, &path) == 0) {
470 vdev->v_name = strdup(path);
472 if (!strcmp(type, "raidz")) {
473 if (vdev->v_nparity == 1)
474 vdev->v_name = "raidz1";
476 vdev->v_name = "raidz2";
478 vdev->v_name = strdup(type);
481 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
482 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
484 * Its ok if we don't have any kids.
487 vdev->v_nchildren = nkids;
488 for (i = 0; i < nkids; i++) {
489 rc = vdev_init_from_nvlist(kids, &kid);
492 STAILQ_INSERT_TAIL(&vdev->v_children, kid, v_childlink);
493 kids = nvlist_next(kids);
496 vdev->v_nchildren = 0;
505 vdev_set_state(vdev_t *vdev)
512 * A mirror or raidz is healthy if all its kids are healthy. A
513 * mirror is degraded if any of its kids is healthy; a raidz
514 * is degraded if at most nparity kids are offline.
516 if (STAILQ_FIRST(&vdev->v_children)) {
519 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
520 if (kid->v_state == VDEV_STATE_HEALTHY)
526 vdev->v_state = VDEV_STATE_HEALTHY;
528 if (vdev->v_read == vdev_mirror_read) {
530 vdev->v_state = VDEV_STATE_DEGRADED;
532 vdev->v_state = VDEV_STATE_OFFLINE;
534 } else if (vdev->v_read == vdev_raidz_read) {
535 if (bad_kids > vdev->v_nparity) {
536 vdev->v_state = VDEV_STATE_OFFLINE;
538 vdev->v_state = VDEV_STATE_DEGRADED;
546 spa_find_by_guid(uint64_t guid)
550 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
551 if (spa->spa_guid == guid)
560 spa_find_by_name(const char *name)
564 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
565 if (!strcmp(spa->spa_name, name))
574 spa_create(uint64_t guid)
578 spa = malloc(sizeof(spa_t));
579 memset(spa, 0, sizeof(spa_t));
580 STAILQ_INIT(&spa->spa_vdevs);
581 spa->spa_guid = guid;
582 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
588 state_name(vdev_state_t state)
590 static const char* names[] = {
603 #define pager_printf printf
608 pager_printf(const char *fmt, ...)
614 vsprintf(line, fmt, args);
621 #define STATUS_FORMAT " %-16s %-10s\n"
624 print_state(int indent, const char *name, vdev_state_t state)
630 for (i = 0; i < indent; i++)
633 pager_printf(STATUS_FORMAT, buf, state_name(state));
638 vdev_status(vdev_t *vdev, int indent)
641 print_state(indent, vdev->v_name, vdev->v_state);
643 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
644 vdev_status(kid, indent + 1);
649 spa_status(spa_t *spa)
652 int good_kids, bad_kids, degraded_kids;
655 pager_printf(" pool: %s\n", spa->spa_name);
656 pager_printf("config:\n\n");
657 pager_printf(STATUS_FORMAT, "NAME", "STATE");
662 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
663 if (vdev->v_state == VDEV_STATE_HEALTHY)
665 else if (vdev->v_state == VDEV_STATE_DEGRADED)
671 state = VDEV_STATE_CLOSED;
672 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
673 state = VDEV_STATE_HEALTHY;
674 else if ((good_kids + degraded_kids) > 0)
675 state = VDEV_STATE_DEGRADED;
677 print_state(0, spa->spa_name, state);
678 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
679 vdev_status(vdev, 1);
689 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
698 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
701 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
703 vdev_t *vdev, *top_vdev, *pool_vdev;
706 const unsigned char *nvlist;
709 uint64_t pool_txg, pool_guid;
710 const char *pool_name;
711 const unsigned char *vdevs;
714 const struct uberblock *up;
717 * Load the vdev label and figure out which
718 * uberblock is most current.
720 memset(&vtmp, 0, sizeof(vtmp));
721 vtmp.v_phys_read = read;
722 vtmp.v_read_priv = read_priv;
723 off = offsetof(vdev_label_t, vl_vdev_phys);
725 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
726 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
727 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
728 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
729 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
730 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
733 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
737 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
739 if (nvlist_find(nvlist,
740 ZPOOL_CONFIG_VERSION,
741 DATA_TYPE_UINT64, 0, &val)) {
745 if (val > SPA_VERSION) {
746 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
747 (unsigned) val, (unsigned) SPA_VERSION);
751 if (nvlist_find(nvlist,
752 ZPOOL_CONFIG_POOL_STATE,
753 DATA_TYPE_UINT64, 0, &val)) {
758 if (val != POOL_STATE_ACTIVE) {
760 * Don't print a message here. If we happen to reboot
761 * while where is an exported pool around, we don't
762 * need a cascade of confusing messages during boot.
764 /*printf("ZFS: pool is not active\n");*/
769 if (nvlist_find(nvlist,
770 ZPOOL_CONFIG_POOL_TXG,
771 DATA_TYPE_UINT64, 0, &pool_txg)
772 || nvlist_find(nvlist,
773 ZPOOL_CONFIG_POOL_GUID,
774 DATA_TYPE_UINT64, 0, &pool_guid)
775 || nvlist_find(nvlist,
776 ZPOOL_CONFIG_POOL_NAME,
777 DATA_TYPE_STRING, 0, &pool_name)) {
779 * Cache and spare devices end up here - just ignore
782 /*printf("ZFS: can't find pool details\n");*/
787 * Create the pool if this is the first time we've seen it.
789 spa = spa_find_by_guid(pool_guid);
791 spa = spa_create(pool_guid);
792 spa->spa_name = strdup(pool_name);
794 if (pool_txg > spa->spa_txg)
795 spa->spa_txg = pool_txg;
798 * Get the vdev tree and create our in-core copy of it.
799 * If we already have a healthy vdev with this guid, this must
800 * be some kind of alias (overlapping slices, dangerously dedicated
803 if (nvlist_find(nvlist,
805 DATA_TYPE_UINT64, 0, &guid)) {
808 vdev = vdev_find(guid);
809 if (vdev && vdev->v_state == VDEV_STATE_HEALTHY) {
813 if (nvlist_find(nvlist,
814 ZPOOL_CONFIG_VDEV_TREE,
815 DATA_TYPE_NVLIST, 0, &vdevs)) {
818 rc = vdev_init_from_nvlist(vdevs, &top_vdev);
823 * Add the toplevel vdev to the pool if its not already there.
825 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
826 if (top_vdev == pool_vdev)
828 if (!pool_vdev && top_vdev)
829 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
832 * We should already have created an incomplete vdev for this
833 * vdev. Find it and initialise it with our read proc.
835 vdev = vdev_find(guid);
837 vdev->v_phys_read = read;
838 vdev->v_read_priv = read_priv;
839 vdev->v_state = VDEV_STATE_HEALTHY;
841 printf("ZFS: inconsistent nvlist contents\n");
846 * Re-evaluate top-level vdev state.
848 vdev_set_state(top_vdev);
851 * Ok, we are happy with the pool so far. Lets find
852 * the best uberblock and then we can actually access
853 * the contents of the pool.
856 i < VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT;
858 off = offsetof(vdev_label_t, vl_uberblock);
859 off += i << UBERBLOCK_SHIFT;
861 DVA_SET_OFFSET(&bp.blk_dva[0], off);
862 BP_SET_LSIZE(&bp, 1 << UBERBLOCK_SHIFT);
863 BP_SET_PSIZE(&bp, 1 << UBERBLOCK_SHIFT);
864 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
865 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
866 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
867 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
870 up = (const struct uberblock *) upbuf;
871 if (up->ub_magic != UBERBLOCK_MAGIC)
873 if (up->ub_txg < spa->spa_txg)
875 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
876 spa->spa_uberblock = *up;
877 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
878 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
879 spa->spa_uberblock = *up;
893 for (v = 0; v < 32; v++)
900 zio_read(spa_t *spa, const blkptr_t *bp, void *buf)
902 int cpfunc = BP_GET_COMPRESS(bp);
903 size_t lsize = BP_GET_LSIZE(bp);
904 size_t psize = BP_GET_PSIZE(bp);
909 if (cpfunc != ZIO_COMPRESS_OFF)
910 pbuf = zfs_alloc_temp(psize);
914 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
915 const dva_t *dva = &bp->blk_dva[i];
920 if (!dva->dva_word[0] && !dva->dva_word[1])
923 vdevid = DVA_GET_VDEV(dva);
924 offset = DVA_GET_OFFSET(dva);
925 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
926 if (vdev->v_id == vdevid)
928 if (!vdev || !vdev->v_read)
930 if (vdev->v_read(vdev, bp, pbuf, offset, psize))
933 if (cpfunc != ZIO_COMPRESS_OFF) {
934 if (zio_decompress_data(cpfunc, pbuf, psize,
941 printf("ZFS: i/o error - all block copies unavailable\n");
947 dnode_read(spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
949 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
950 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
951 int nlevels = dnode->dn_nlevels;
955 * Note: bsize may not be a power of two here so we need to do an
956 * actual divide rather than a bitshift.
959 uint64_t bn = offset / bsize;
960 int boff = offset % bsize;
962 const blkptr_t *indbp;
965 if (bn > dnode->dn_maxblkid)
968 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
971 indbp = dnode->dn_blkptr;
972 for (i = 0; i < nlevels; i++) {
974 * Copy the bp from the indirect array so that
975 * we can re-use the scratch buffer for multi-level
978 ibn = bn >> ((nlevels - i - 1) * ibshift);
979 ibn &= ((1 << ibshift) - 1);
981 rc = zio_read(spa, &bp, dnode_cache_buf);
984 indbp = (const blkptr_t *) dnode_cache_buf;
986 dnode_cache_obj = dnode;
991 * The buffer contains our data block. Copy what we
992 * need from it and loop.
995 if (i > buflen) i = buflen;
996 memcpy(buf, &dnode_cache_buf[boff], i);
997 buf = ((char*) buf) + i;
1006 * Lookup a value in a microzap directory. Assumes that the zap
1007 * scratch buffer contains the directory contents.
1010 mzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1012 const mzap_phys_t *mz;
1013 const mzap_ent_phys_t *mze;
1018 * Microzap objects use exactly one block. Read the whole
1021 size = dnode->dn_datablkszsec * 512;
1023 mz = (const mzap_phys_t *) zap_scratch;
1024 chunks = size / MZAP_ENT_LEN - 1;
1026 for (i = 0; i < chunks; i++) {
1027 mze = &mz->mz_chunk[i];
1028 if (!strcmp(mze->mze_name, name)) {
1029 *value = mze->mze_value;
1038 * Compare a name with a zap leaf entry. Return non-zero if the name
1042 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1045 const zap_leaf_chunk_t *nc;
1048 namelen = zc->l_entry.le_name_length;
1050 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1052 while (namelen > 0) {
1055 if (len > ZAP_LEAF_ARRAY_BYTES)
1056 len = ZAP_LEAF_ARRAY_BYTES;
1057 if (memcmp(p, nc->l_array.la_array, len))
1061 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1068 * Extract a uint64_t value from a zap leaf entry.
1071 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1073 const zap_leaf_chunk_t *vc;
1078 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1079 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1080 value = (value << 8) | p[i];
1087 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1088 * buffer contains the directory header.
1091 fzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1093 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1094 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1100 if (zh.zap_magic != ZAP_MAGIC)
1103 z.zap_block_shift = ilog2(bsize);
1104 z.zap_phys = (zap_phys_t *) zap_scratch;
1107 * Figure out where the pointer table is and read it in if necessary.
1109 if (zh.zap_ptrtbl.zt_blk) {
1110 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1111 zap_scratch, bsize);
1114 ptrtbl = (uint64_t *) zap_scratch;
1116 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1119 hash = zap_hash(zh.zap_salt, name);
1122 zl.l_bs = z.zap_block_shift;
1124 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1125 zap_leaf_chunk_t *zc;
1127 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1131 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1134 * Make sure this chunk matches our hash.
1136 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1137 && zl.l_phys->l_hdr.lh_prefix
1138 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1142 * Hash within the chunk to find our entry.
1144 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1145 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1146 h = zl.l_phys->l_hash[h];
1149 zc = &ZAP_LEAF_CHUNK(&zl, h);
1150 while (zc->l_entry.le_hash != hash) {
1151 if (zc->l_entry.le_next == 0xffff) {
1155 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1157 if (fzap_name_equal(&zl, zc, name)) {
1158 *value = fzap_leaf_value(&zl, zc);
1166 * Lookup a name in a zap object and return its value as a uint64_t.
1169 zap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1173 size_t size = dnode->dn_datablkszsec * 512;
1175 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1179 zap_type = *(uint64_t *) zap_scratch;
1180 if (zap_type == ZBT_MICRO)
1181 return mzap_lookup(spa, dnode, name, value);
1183 return fzap_lookup(spa, dnode, name, value);
1189 * List a microzap directory. Assumes that the zap scratch buffer contains
1190 * the directory contents.
1193 mzap_list(spa_t *spa, const dnode_phys_t *dnode)
1195 const mzap_phys_t *mz;
1196 const mzap_ent_phys_t *mze;
1201 * Microzap objects use exactly one block. Read the whole
1204 size = dnode->dn_datablkszsec * 512;
1205 mz = (const mzap_phys_t *) zap_scratch;
1206 chunks = size / MZAP_ENT_LEN - 1;
1208 for (i = 0; i < chunks; i++) {
1209 mze = &mz->mz_chunk[i];
1210 if (mze->mze_name[0])
1211 //printf("%-32s 0x%llx\n", mze->mze_name, mze->mze_value);
1212 printf("%s\n", mze->mze_name);
1219 * List a fatzap directory. Assumes that the zap scratch buffer contains
1220 * the directory header.
1223 fzap_list(spa_t *spa, const dnode_phys_t *dnode)
1225 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1226 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1230 if (zh.zap_magic != ZAP_MAGIC)
1233 z.zap_block_shift = ilog2(bsize);
1234 z.zap_phys = (zap_phys_t *) zap_scratch;
1237 * This assumes that the leaf blocks start at block 1. The
1238 * documentation isn't exactly clear on this.
1241 zl.l_bs = z.zap_block_shift;
1242 for (i = 0; i < zh.zap_num_leafs; i++) {
1243 off_t off = (i + 1) << zl.l_bs;
1247 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1250 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1252 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1253 zap_leaf_chunk_t *zc, *nc;
1256 zc = &ZAP_LEAF_CHUNK(&zl, j);
1257 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1259 namelen = zc->l_entry.le_name_length;
1260 if (namelen > sizeof(name))
1261 namelen = sizeof(name);
1264 * Paste the name back together.
1266 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1268 while (namelen > 0) {
1271 if (len > ZAP_LEAF_ARRAY_BYTES)
1272 len = ZAP_LEAF_ARRAY_BYTES;
1273 memcpy(p, nc->l_array.la_array, len);
1276 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1280 * Assume the first eight bytes of the value are
1283 value = fzap_leaf_value(&zl, zc);
1285 printf("%-32s 0x%llx\n", name, value);
1293 * List a zap directory.
1296 zap_list(spa_t *spa, const dnode_phys_t *dnode)
1299 size_t size = dnode->dn_datablkszsec * 512;
1301 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1304 zap_type = *(uint64_t *) zap_scratch;
1305 if (zap_type == ZBT_MICRO)
1306 return mzap_list(spa, dnode);
1308 return fzap_list(spa, dnode);
1314 objset_get_dnode(spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1318 offset = objnum * sizeof(dnode_phys_t);
1319 return dnode_read(spa, &os->os_meta_dnode, offset,
1320 dnode, sizeof(dnode_phys_t));
1324 * Find the object set given the object number of its dataset object
1325 * and return its details in *objset
1328 zfs_mount_dataset(spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1330 dnode_phys_t dataset;
1331 dsl_dataset_phys_t *ds;
1333 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1334 printf("ZFS: can't find dataset %lld\n", objnum);
1338 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1339 if (zio_read(spa, &ds->ds_bp, objset)) {
1340 printf("ZFS: can't read object set for dataset %lld\n", objnum);
1348 * Find the object set pointed to by the BOOTFS property or the root
1349 * dataset if there is none and return its details in *objset
1352 zfs_mount_root(spa_t *spa, objset_phys_t *objset)
1354 dnode_phys_t dir, propdir;
1355 uint64_t props, bootfs, root;
1358 * Start with the MOS directory object.
1360 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1361 printf("ZFS: can't read MOS object directory\n");
1366 * Lookup the pool_props and see if we can find a bootfs.
1368 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1369 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1370 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0)
1371 return zfs_mount_dataset(spa, bootfs, objset);
1374 * Lookup the root dataset directory
1376 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1377 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1378 printf("ZFS: can't find root dsl_dir\n");
1383 * Use the information from the dataset directory's bonus buffer
1384 * to find the dataset object and from that the object set itself.
1386 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1387 return zfs_mount_dataset(spa, dd->dd_head_dataset_obj, objset);
1391 zfs_mount_pool(spa_t *spa)
1394 * Find the MOS and work our way in from there.
1396 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1397 printf("ZFS: can't read MOS\n");
1402 * Find the root object set
1404 if (zfs_mount_root(spa, &spa->spa_root_objset)) {
1405 printf("Can't find root filesystem - giving up\n");
1413 * Lookup a file and return its dnode.
1416 zfs_lookup(spa_t *spa, const char *upath, dnode_phys_t *dnode)
1419 uint64_t objnum, rootnum, parentnum;
1421 const znode_phys_t *zp = (const znode_phys_t *) dn.dn_bonus;
1425 int symlinks_followed = 0;
1427 if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
1428 printf("ZFS: unexpected object set type %lld\n",
1429 spa->spa_root_objset.os_type);
1434 * Get the root directory dnode.
1436 rc = objset_get_dnode(spa, &spa->spa_root_objset, MASTER_NODE_OBJ, &dn);
1440 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1444 rc = objset_get_dnode(spa, &spa->spa_root_objset, rootnum, &dn);
1457 memcpy(element, p, q - p);
1465 if ((zp->zp_mode >> 12) != 0x4) {
1470 rc = zap_lookup(spa, &dn, element, &objnum);
1473 objnum = ZFS_DIRENT_OBJ(objnum);
1475 rc = objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1480 * Check for symlink.
1482 if ((zp->zp_mode >> 12) == 0xa) {
1483 if (symlinks_followed > 10)
1485 symlinks_followed++;
1488 * Read the link value and copy the tail of our
1489 * current path onto the end.
1492 strcpy(&path[zp->zp_size], p);
1494 path[zp->zp_size] = 0;
1495 if (zp->zp_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1496 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1499 rc = dnode_read(spa, &dn, 0, path, zp->zp_size);
1505 * Restart with the new path, starting either at
1506 * the root or at the parent depending whether or
1507 * not the link is relative.
1514 objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);