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 all pools, chained through spa_link.
55 static spa_list_t zfs_pools;
57 static uint64_t zfs_crc64_table[256];
58 static const dnode_phys_t *dnode_cache_obj = 0;
59 static uint64_t dnode_cache_bn;
60 static char *dnode_cache_buf;
61 static char *zap_scratch;
62 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
64 #define TEMP_SIZE (1024 * 1024)
66 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
67 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
68 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
73 STAILQ_INIT(&zfs_vdevs);
74 STAILQ_INIT(&zfs_pools);
76 zfs_temp_buf = malloc(TEMP_SIZE);
77 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
78 zfs_temp_ptr = zfs_temp_buf;
79 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
80 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
86 zfs_alloc(size_t size)
90 if (zfs_temp_ptr + size > zfs_temp_end) {
91 printf("ZFS: out of temporary buffer space\n");
101 zfs_free(void *ptr, size_t size)
104 zfs_temp_ptr -= size;
105 if (zfs_temp_ptr != ptr) {
106 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
112 xdr_int(const unsigned char **xdr, int *ip)
114 *ip = ((*xdr)[0] << 24)
123 xdr_u_int(const unsigned char **xdr, u_int *ip)
125 *ip = ((*xdr)[0] << 24)
134 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
140 *lp = (((uint64_t) hi) << 32) | lo;
145 nvlist_find(const unsigned char *nvlist, const char *name, int type,
146 int* elementsp, void *valuep)
148 const unsigned char *p, *pair;
150 int encoded_size, decoded_size;
157 xdr_int(&p, &encoded_size);
158 xdr_int(&p, &decoded_size);
159 while (encoded_size && decoded_size) {
160 int namelen, pairtype, elements;
161 const char *pairname;
163 xdr_int(&p, &namelen);
164 pairname = (const char*) p;
165 p += roundup(namelen, 4);
166 xdr_int(&p, &pairtype);
168 if (!memcmp(name, pairname, namelen) && type == pairtype) {
169 xdr_int(&p, &elements);
171 *elementsp = elements;
172 if (type == DATA_TYPE_UINT64) {
173 xdr_uint64_t(&p, (uint64_t *) valuep);
175 } else if (type == DATA_TYPE_STRING) {
178 (*(const char**) valuep) = (const char*) p;
180 } else if (type == DATA_TYPE_NVLIST
181 || type == DATA_TYPE_NVLIST_ARRAY) {
182 (*(const unsigned char**) valuep) =
183 (const unsigned char*) p;
190 * Not the pair we are looking for, skip to the next one.
192 p = pair + encoded_size;
196 xdr_int(&p, &encoded_size);
197 xdr_int(&p, &decoded_size);
204 * Return the next nvlist in an nvlist array.
206 static const unsigned char *
207 nvlist_next(const unsigned char *nvlist)
209 const unsigned char *p, *pair;
211 int encoded_size, decoded_size;
218 xdr_int(&p, &encoded_size);
219 xdr_int(&p, &decoded_size);
220 while (encoded_size && decoded_size) {
221 p = pair + encoded_size;
224 xdr_int(&p, &encoded_size);
225 xdr_int(&p, &decoded_size);
233 static const unsigned char *
234 nvlist_print(const unsigned char *nvlist, unsigned int indent)
236 static const char* typenames[] = {
247 "DATA_TYPE_BYTE_ARRAY",
248 "DATA_TYPE_INT16_ARRAY",
249 "DATA_TYPE_UINT16_ARRAY",
250 "DATA_TYPE_INT32_ARRAY",
251 "DATA_TYPE_UINT32_ARRAY",
252 "DATA_TYPE_INT64_ARRAY",
253 "DATA_TYPE_UINT64_ARRAY",
254 "DATA_TYPE_STRING_ARRAY",
257 "DATA_TYPE_NVLIST_ARRAY",
258 "DATA_TYPE_BOOLEAN_VALUE",
261 "DATA_TYPE_BOOLEAN_ARRAY",
262 "DATA_TYPE_INT8_ARRAY",
263 "DATA_TYPE_UINT8_ARRAY"
267 const unsigned char *p, *pair;
269 int encoded_size, decoded_size;
276 xdr_int(&p, &encoded_size);
277 xdr_int(&p, &decoded_size);
278 while (encoded_size && decoded_size) {
279 int namelen, pairtype, elements;
280 const char *pairname;
282 xdr_int(&p, &namelen);
283 pairname = (const char*) p;
284 p += roundup(namelen, 4);
285 xdr_int(&p, &pairtype);
287 for (i = 0; i < indent; i++)
289 printf("%s %s", typenames[pairtype], pairname);
291 xdr_int(&p, &elements);
293 case DATA_TYPE_UINT64: {
295 xdr_uint64_t(&p, &val);
296 printf(" = 0x%jx\n", (uintmax_t)val);
300 case DATA_TYPE_STRING: {
303 printf(" = \"%s\"\n", p);
307 case DATA_TYPE_NVLIST:
309 nvlist_print(p, indent + 1);
312 case DATA_TYPE_NVLIST_ARRAY:
313 for (j = 0; j < elements; j++) {
315 p = nvlist_print(p, indent + 1);
316 if (j != elements - 1) {
317 for (i = 0; i < indent; i++)
319 printf("%s %s", typenames[pairtype], pairname);
328 p = pair + encoded_size;
331 xdr_int(&p, &encoded_size);
332 xdr_int(&p, &decoded_size);
341 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
342 off_t offset, size_t size)
347 if (!vdev->v_phys_read)
351 psize = BP_GET_PSIZE(bp);
356 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
357 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
360 if (bp && zio_checksum_verify(bp, buf))
367 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
368 off_t offset, size_t bytes)
371 return (vdev_read_phys(vdev, bp, buf,
372 offset + VDEV_LABEL_START_SIZE, bytes));
377 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
378 off_t offset, size_t bytes)
384 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
385 if (kid->v_state != VDEV_STATE_HEALTHY)
387 rc = kid->v_read(kid, bp, buf, offset, bytes);
396 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
397 off_t offset, size_t bytes)
402 * Here we should have two kids:
403 * First one which is the one we are replacing and we can trust
404 * only this one to have valid data, but it might not be present.
405 * Second one is that one we are replacing with. It is most likely
406 * healthy, but we can't trust it has needed data, so we won't use it.
408 kid = STAILQ_FIRST(&vdev->v_children);
411 if (kid->v_state != VDEV_STATE_HEALTHY)
413 return (kid->v_read(kid, bp, buf, offset, bytes));
417 vdev_find(uint64_t guid)
421 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
422 if (vdev->v_guid == guid)
429 vdev_create(uint64_t guid, vdev_read_t *read)
433 vdev = malloc(sizeof(vdev_t));
434 memset(vdev, 0, sizeof(vdev_t));
435 STAILQ_INIT(&vdev->v_children);
437 vdev->v_state = VDEV_STATE_OFFLINE;
439 vdev->v_phys_read = 0;
440 vdev->v_read_priv = 0;
441 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
447 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
448 vdev_t **vdevp, int is_newer)
451 uint64_t guid, id, ashift, nparity;
455 const unsigned char *kids;
456 int nkids, i, is_new;
457 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
459 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
460 DATA_TYPE_UINT64, 0, &guid)
461 || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
462 DATA_TYPE_UINT64, 0, &id)
463 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
464 DATA_TYPE_STRING, 0, &type)) {
465 printf("ZFS: can't find vdev details\n");
469 if (strcmp(type, VDEV_TYPE_MIRROR)
470 && strcmp(type, VDEV_TYPE_DISK)
472 && strcmp(type, VDEV_TYPE_FILE)
474 && strcmp(type, VDEV_TYPE_RAIDZ)
475 && strcmp(type, VDEV_TYPE_REPLACING)) {
476 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
480 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
482 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
484 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
486 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
488 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
490 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
493 vdev = vdev_find(guid);
497 if (!strcmp(type, VDEV_TYPE_MIRROR))
498 vdev = vdev_create(guid, vdev_mirror_read);
499 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
500 vdev = vdev_create(guid, vdev_raidz_read);
501 else if (!strcmp(type, VDEV_TYPE_REPLACING))
502 vdev = vdev_create(guid, vdev_replacing_read);
504 vdev = vdev_create(guid, vdev_disk_read);
507 vdev->v_top = pvdev != NULL ? pvdev : vdev;
508 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
509 DATA_TYPE_UINT64, 0, &ashift) == 0)
510 vdev->v_ashift = ashift;
513 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
514 DATA_TYPE_UINT64, 0, &nparity) == 0)
515 vdev->v_nparity = nparity;
518 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
519 DATA_TYPE_STRING, 0, &path) == 0) {
520 if (strncmp(path, "/dev/", 5) == 0)
522 vdev->v_name = strdup(path);
524 if (!strcmp(type, "raidz")) {
525 if (vdev->v_nparity == 1)
526 vdev->v_name = "raidz1";
527 else if (vdev->v_nparity == 2)
528 vdev->v_name = "raidz2";
529 else if (vdev->v_nparity == 3)
530 vdev->v_name = "raidz3";
532 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
536 vdev->v_name = strdup(type);
543 if (is_new || is_newer) {
545 * This is either new vdev or we've already seen this vdev,
546 * but from an older vdev label, so let's refresh its state
547 * from the newer label.
550 vdev->v_state = VDEV_STATE_OFFLINE;
552 vdev->v_state = VDEV_STATE_REMOVED;
554 vdev->v_state = VDEV_STATE_FAULTED;
555 else if (is_degraded)
556 vdev->v_state = VDEV_STATE_DEGRADED;
557 else if (isnt_present)
558 vdev->v_state = VDEV_STATE_CANT_OPEN;
561 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
562 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
564 * Its ok if we don't have any kids.
567 vdev->v_nchildren = nkids;
568 for (i = 0; i < nkids; i++) {
569 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
573 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
575 kids = nvlist_next(kids);
578 vdev->v_nchildren = 0;
587 vdev_set_state(vdev_t *vdev)
594 * A mirror or raidz is healthy if all its kids are healthy. A
595 * mirror is degraded if any of its kids is healthy; a raidz
596 * is degraded if at most nparity kids are offline.
598 if (STAILQ_FIRST(&vdev->v_children)) {
601 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
602 if (kid->v_state == VDEV_STATE_HEALTHY)
608 vdev->v_state = VDEV_STATE_HEALTHY;
610 if (vdev->v_read == vdev_mirror_read) {
612 vdev->v_state = VDEV_STATE_DEGRADED;
614 vdev->v_state = VDEV_STATE_OFFLINE;
616 } else if (vdev->v_read == vdev_raidz_read) {
617 if (bad_kids > vdev->v_nparity) {
618 vdev->v_state = VDEV_STATE_OFFLINE;
620 vdev->v_state = VDEV_STATE_DEGRADED;
628 spa_find_by_guid(uint64_t guid)
632 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
633 if (spa->spa_guid == guid)
640 spa_find_by_name(const char *name)
644 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
645 if (!strcmp(spa->spa_name, name))
652 spa_create(uint64_t guid)
656 spa = malloc(sizeof(spa_t));
657 memset(spa, 0, sizeof(spa_t));
658 STAILQ_INIT(&spa->spa_vdevs);
659 spa->spa_guid = guid;
660 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
666 state_name(vdev_state_t state)
668 static const char* names[] = {
683 #define pager_printf printf
688 pager_printf(const char *fmt, ...)
694 vsprintf(line, fmt, args);
701 #define STATUS_FORMAT " %s %s\n"
704 print_state(int indent, const char *name, vdev_state_t state)
710 for (i = 0; i < indent; i++)
713 pager_printf(STATUS_FORMAT, buf, state_name(state));
718 vdev_status(vdev_t *vdev, int indent)
721 print_state(indent, vdev->v_name, vdev->v_state);
723 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
724 vdev_status(kid, indent + 1);
729 spa_status(spa_t *spa)
731 static char bootfs[ZFS_MAXNAMELEN];
734 int good_kids, bad_kids, degraded_kids;
737 pager_printf(" pool: %s\n", spa->spa_name);
738 if (zfs_get_root(spa, &rootid) == 0 &&
739 zfs_rlookup(spa, rootid, bootfs) == 0) {
740 if (bootfs[0] == '\0')
741 pager_printf("bootfs: %s\n", spa->spa_name);
743 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
745 pager_printf("config:\n\n");
746 pager_printf(STATUS_FORMAT, "NAME", "STATE");
751 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
752 if (vdev->v_state == VDEV_STATE_HEALTHY)
754 else if (vdev->v_state == VDEV_STATE_DEGRADED)
760 state = VDEV_STATE_CLOSED;
761 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
762 state = VDEV_STATE_HEALTHY;
763 else if ((good_kids + degraded_kids) > 0)
764 state = VDEV_STATE_DEGRADED;
766 print_state(0, spa->spa_name, state);
767 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
768 vdev_status(vdev, 1);
778 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
787 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
790 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
792 vdev_t *vdev, *top_vdev, *pool_vdev;
795 const unsigned char *nvlist;
798 uint64_t pool_txg, pool_guid;
800 const char *pool_name;
801 const unsigned char *vdevs;
804 const struct uberblock *up;
807 * Load the vdev label and figure out which
808 * uberblock is most current.
810 memset(&vtmp, 0, sizeof(vtmp));
811 vtmp.v_phys_read = read;
812 vtmp.v_read_priv = read_priv;
813 off = offsetof(vdev_label_t, vl_vdev_phys);
815 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
816 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
817 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
818 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
819 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
820 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
821 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
824 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
828 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
830 if (nvlist_find(nvlist,
831 ZPOOL_CONFIG_VERSION,
832 DATA_TYPE_UINT64, 0, &val)) {
836 if (val > SPA_VERSION) {
837 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
838 (unsigned) val, (unsigned) SPA_VERSION);
842 if (nvlist_find(nvlist,
843 ZPOOL_CONFIG_POOL_STATE,
844 DATA_TYPE_UINT64, 0, &val)) {
848 if (val == POOL_STATE_DESTROYED) {
849 /* We don't boot only from destroyed pools. */
853 if (nvlist_find(nvlist,
854 ZPOOL_CONFIG_POOL_TXG,
855 DATA_TYPE_UINT64, 0, &pool_txg)
856 || nvlist_find(nvlist,
857 ZPOOL_CONFIG_POOL_GUID,
858 DATA_TYPE_UINT64, 0, &pool_guid)
859 || nvlist_find(nvlist,
860 ZPOOL_CONFIG_POOL_NAME,
861 DATA_TYPE_STRING, 0, &pool_name)) {
863 * Cache and spare devices end up here - just ignore
866 /*printf("ZFS: can't find pool details\n");*/
871 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
877 * Create the pool if this is the first time we've seen it.
879 spa = spa_find_by_guid(pool_guid);
881 spa = spa_create(pool_guid);
882 spa->spa_name = strdup(pool_name);
884 if (pool_txg > spa->spa_txg) {
885 spa->spa_txg = pool_txg;
891 * Get the vdev tree and create our in-core copy of it.
892 * If we already have a vdev with this guid, this must
893 * be some kind of alias (overlapping slices, dangerously dedicated
896 if (nvlist_find(nvlist,
898 DATA_TYPE_UINT64, 0, &guid)) {
901 vdev = vdev_find(guid);
902 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
905 if (nvlist_find(nvlist,
906 ZPOOL_CONFIG_VDEV_TREE,
907 DATA_TYPE_NVLIST, 0, &vdevs)) {
911 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
916 * Add the toplevel vdev to the pool if its not already there.
918 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
919 if (top_vdev == pool_vdev)
921 if (!pool_vdev && top_vdev)
922 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
925 * We should already have created an incomplete vdev for this
926 * vdev. Find it and initialise it with our read proc.
928 vdev = vdev_find(guid);
930 vdev->v_phys_read = read;
931 vdev->v_read_priv = read_priv;
932 vdev->v_state = VDEV_STATE_HEALTHY;
934 printf("ZFS: inconsistent nvlist contents\n");
939 * Re-evaluate top-level vdev state.
941 vdev_set_state(top_vdev);
944 * Ok, we are happy with the pool so far. Lets find
945 * the best uberblock and then we can actually access
946 * the contents of the pool.
948 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
949 up = (const struct uberblock *)upbuf;
951 i < VDEV_UBERBLOCK_COUNT(vdev);
953 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
955 DVA_SET_OFFSET(&bp.blk_dva[0], off);
956 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
957 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
958 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
959 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
960 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
962 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
965 if (up->ub_magic != UBERBLOCK_MAGIC)
967 if (up->ub_txg < spa->spa_txg)
969 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
970 spa->spa_uberblock = *up;
971 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
972 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
973 spa->spa_uberblock = *up;
976 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
988 for (v = 0; v < 32; v++)
995 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
998 zio_gbh_phys_t zio_gb;
1002 /* Artificial BP for gang block header. */
1004 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1005 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1006 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1007 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1008 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1009 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1011 /* Read gang header block using the artificial BP. */
1012 if (zio_read(spa, &gbh_bp, &zio_gb))
1016 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1017 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1019 if (BP_IS_HOLE(gbp))
1021 if (zio_read(spa, gbp, pbuf))
1023 pbuf += BP_GET_PSIZE(gbp);
1026 if (zio_checksum_verify(bp, buf))
1032 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1034 int cpfunc = BP_GET_COMPRESS(bp);
1035 uint64_t align, size;
1041 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1042 const dva_t *dva = &bp->blk_dva[i];
1047 if (!dva->dva_word[0] && !dva->dva_word[1])
1050 vdevid = DVA_GET_VDEV(dva);
1051 offset = DVA_GET_OFFSET(dva);
1052 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1053 if (vdev->v_id == vdevid)
1056 if (!vdev || !vdev->v_read)
1059 size = BP_GET_PSIZE(bp);
1060 if (vdev->v_read == vdev_raidz_read) {
1061 align = 1ULL << vdev->v_top->v_ashift;
1062 if (P2PHASE(size, align) != 0)
1063 size = P2ROUNDUP(size, align);
1065 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1066 pbuf = zfs_alloc(size);
1070 if (DVA_GET_GANG(dva))
1071 error = zio_read_gang(spa, bp, pbuf);
1073 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1075 if (cpfunc != ZIO_COMPRESS_OFF)
1076 error = zio_decompress_data(cpfunc, pbuf,
1077 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1078 else if (size != BP_GET_PSIZE(bp))
1079 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1082 zfs_free(pbuf, size);
1087 printf("ZFS: i/o error - all block copies unavailable\n");
1092 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1094 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1095 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1096 int nlevels = dnode->dn_nlevels;
1100 * Note: bsize may not be a power of two here so we need to do an
1101 * actual divide rather than a bitshift.
1103 while (buflen > 0) {
1104 uint64_t bn = offset / bsize;
1105 int boff = offset % bsize;
1107 const blkptr_t *indbp;
1110 if (bn > dnode->dn_maxblkid)
1113 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1116 indbp = dnode->dn_blkptr;
1117 for (i = 0; i < nlevels; i++) {
1119 * Copy the bp from the indirect array so that
1120 * we can re-use the scratch buffer for multi-level
1123 ibn = bn >> ((nlevels - i - 1) * ibshift);
1124 ibn &= ((1 << ibshift) - 1);
1126 rc = zio_read(spa, &bp, dnode_cache_buf);
1129 indbp = (const blkptr_t *) dnode_cache_buf;
1131 dnode_cache_obj = dnode;
1132 dnode_cache_bn = bn;
1136 * The buffer contains our data block. Copy what we
1137 * need from it and loop.
1140 if (i > buflen) i = buflen;
1141 memcpy(buf, &dnode_cache_buf[boff], i);
1142 buf = ((char*) buf) + i;
1151 * Lookup a value in a microzap directory. Assumes that the zap
1152 * scratch buffer contains the directory contents.
1155 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1157 const mzap_phys_t *mz;
1158 const mzap_ent_phys_t *mze;
1163 * Microzap objects use exactly one block. Read the whole
1166 size = dnode->dn_datablkszsec * 512;
1168 mz = (const mzap_phys_t *) zap_scratch;
1169 chunks = size / MZAP_ENT_LEN - 1;
1171 for (i = 0; i < chunks; i++) {
1172 mze = &mz->mz_chunk[i];
1173 if (!strcmp(mze->mze_name, name)) {
1174 *value = mze->mze_value;
1183 * Compare a name with a zap leaf entry. Return non-zero if the name
1187 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1190 const zap_leaf_chunk_t *nc;
1193 namelen = zc->l_entry.le_name_numints;
1195 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1197 while (namelen > 0) {
1200 if (len > ZAP_LEAF_ARRAY_BYTES)
1201 len = ZAP_LEAF_ARRAY_BYTES;
1202 if (memcmp(p, nc->l_array.la_array, len))
1206 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1213 * Extract a uint64_t value from a zap leaf entry.
1216 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1218 const zap_leaf_chunk_t *vc;
1223 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1224 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1225 value = (value << 8) | p[i];
1232 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1233 * buffer contains the directory header.
1236 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1238 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1239 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1245 if (zh.zap_magic != ZAP_MAGIC)
1248 z.zap_block_shift = ilog2(bsize);
1249 z.zap_phys = (zap_phys_t *) zap_scratch;
1252 * Figure out where the pointer table is and read it in if necessary.
1254 if (zh.zap_ptrtbl.zt_blk) {
1255 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1256 zap_scratch, bsize);
1259 ptrtbl = (uint64_t *) zap_scratch;
1261 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1264 hash = zap_hash(zh.zap_salt, name);
1267 zl.l_bs = z.zap_block_shift;
1269 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1270 zap_leaf_chunk_t *zc;
1272 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1276 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1279 * Make sure this chunk matches our hash.
1281 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1282 && zl.l_phys->l_hdr.lh_prefix
1283 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1287 * Hash within the chunk to find our entry.
1289 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1290 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1291 h = zl.l_phys->l_hash[h];
1294 zc = &ZAP_LEAF_CHUNK(&zl, h);
1295 while (zc->l_entry.le_hash != hash) {
1296 if (zc->l_entry.le_next == 0xffff) {
1300 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1302 if (fzap_name_equal(&zl, zc, name)) {
1303 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1305 *value = fzap_leaf_value(&zl, zc);
1313 * Lookup a name in a zap object and return its value as a uint64_t.
1316 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1320 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1322 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1326 zap_type = *(uint64_t *) zap_scratch;
1327 if (zap_type == ZBT_MICRO)
1328 return mzap_lookup(dnode, name, value);
1329 else if (zap_type == ZBT_HEADER)
1330 return fzap_lookup(spa, dnode, name, value);
1331 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1336 * List a microzap directory. Assumes that the zap scratch buffer contains
1337 * the directory contents.
1340 mzap_list(const dnode_phys_t *dnode)
1342 const mzap_phys_t *mz;
1343 const mzap_ent_phys_t *mze;
1348 * Microzap objects use exactly one block. Read the whole
1351 size = dnode->dn_datablkszsec * 512;
1352 mz = (const mzap_phys_t *) zap_scratch;
1353 chunks = size / MZAP_ENT_LEN - 1;
1355 for (i = 0; i < chunks; i++) {
1356 mze = &mz->mz_chunk[i];
1357 if (mze->mze_name[0])
1358 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1359 printf("%s\n", mze->mze_name);
1366 * List a fatzap directory. Assumes that the zap scratch buffer contains
1367 * the directory header.
1370 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1372 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1373 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1377 if (zh.zap_magic != ZAP_MAGIC)
1380 z.zap_block_shift = ilog2(bsize);
1381 z.zap_phys = (zap_phys_t *) zap_scratch;
1384 * This assumes that the leaf blocks start at block 1. The
1385 * documentation isn't exactly clear on this.
1388 zl.l_bs = z.zap_block_shift;
1389 for (i = 0; i < zh.zap_num_leafs; i++) {
1390 off_t off = (i + 1) << zl.l_bs;
1394 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1397 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1399 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1400 zap_leaf_chunk_t *zc, *nc;
1403 zc = &ZAP_LEAF_CHUNK(&zl, j);
1404 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1406 namelen = zc->l_entry.le_name_numints;
1407 if (namelen > sizeof(name))
1408 namelen = sizeof(name);
1411 * Paste the name back together.
1413 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1415 while (namelen > 0) {
1418 if (len > ZAP_LEAF_ARRAY_BYTES)
1419 len = ZAP_LEAF_ARRAY_BYTES;
1420 memcpy(p, nc->l_array.la_array, len);
1423 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1427 * Assume the first eight bytes of the value are
1430 value = fzap_leaf_value(&zl, zc);
1432 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1433 printf("%s\n", name);
1441 * List a zap directory.
1444 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1447 size_t size = dnode->dn_datablkszsec * 512;
1449 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1452 zap_type = *(uint64_t *) zap_scratch;
1453 if (zap_type == ZBT_MICRO)
1454 return mzap_list(dnode);
1456 return fzap_list(spa, dnode);
1460 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1464 offset = objnum * sizeof(dnode_phys_t);
1465 return dnode_read(spa, &os->os_meta_dnode, offset,
1466 dnode, sizeof(dnode_phys_t));
1470 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1472 const mzap_phys_t *mz;
1473 const mzap_ent_phys_t *mze;
1478 * Microzap objects use exactly one block. Read the whole
1481 size = dnode->dn_datablkszsec * 512;
1483 mz = (const mzap_phys_t *) zap_scratch;
1484 chunks = size / MZAP_ENT_LEN - 1;
1486 for (i = 0; i < chunks; i++) {
1487 mze = &mz->mz_chunk[i];
1488 if (value == mze->mze_value) {
1489 strcpy(name, mze->mze_name);
1498 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1501 const zap_leaf_chunk_t *nc;
1504 namelen = zc->l_entry.le_name_numints;
1506 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1508 while (namelen > 0) {
1511 if (len > ZAP_LEAF_ARRAY_BYTES)
1512 len = ZAP_LEAF_ARRAY_BYTES;
1513 memcpy(p, nc->l_array.la_array, len);
1516 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1523 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1525 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1526 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1530 if (zh.zap_magic != ZAP_MAGIC)
1533 z.zap_block_shift = ilog2(bsize);
1534 z.zap_phys = (zap_phys_t *) zap_scratch;
1537 * This assumes that the leaf blocks start at block 1. The
1538 * documentation isn't exactly clear on this.
1541 zl.l_bs = z.zap_block_shift;
1542 for (i = 0; i < zh.zap_num_leafs; i++) {
1543 off_t off = (i + 1) << zl.l_bs;
1545 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1548 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1550 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1551 zap_leaf_chunk_t *zc;
1553 zc = &ZAP_LEAF_CHUNK(&zl, j);
1554 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1556 if (zc->l_entry.le_value_intlen != 8 ||
1557 zc->l_entry.le_value_numints != 1)
1560 if (fzap_leaf_value(&zl, zc) == value) {
1561 fzap_name_copy(&zl, zc, name);
1571 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1575 size_t size = dnode->dn_datablkszsec * 512;
1577 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1581 zap_type = *(uint64_t *) zap_scratch;
1582 if (zap_type == ZBT_MICRO)
1583 return mzap_rlookup(spa, dnode, name, value);
1585 return fzap_rlookup(spa, dnode, name, value);
1589 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1592 char component[256];
1593 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1594 dnode_phys_t child_dir_zap, dataset, dir, parent;
1596 dsl_dataset_phys_t *ds;
1600 p = &name[sizeof(name) - 1];
1603 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1604 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1607 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1608 dir_obj = ds->ds_dir_obj;
1611 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1613 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1615 /* Actual loop condition. */
1616 parent_obj = dd->dd_parent_obj;
1617 if (parent_obj == 0)
1620 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1622 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1623 child_dir_zapobj = dd->dd_child_dir_zapobj;
1624 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1626 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1629 len = strlen(component);
1631 memcpy(p, component, len);
1635 /* Actual loop iteration. */
1636 dir_obj = parent_obj;
1647 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1650 uint64_t dir_obj, child_dir_zapobj;
1651 dnode_phys_t child_dir_zap, dir;
1655 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1657 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1662 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1664 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1668 /* Actual loop condition #1. */
1674 memcpy(element, p, q - p);
1675 element[q - p] = '\0';
1682 child_dir_zapobj = dd->dd_child_dir_zapobj;
1683 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1686 /* Actual loop condition #2. */
1687 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1691 *objnum = dd->dd_head_dataset_obj;
1697 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1699 uint64_t dir_obj, child_dir_zapobj;
1700 dnode_phys_t child_dir_zap, dir, dataset;
1701 dsl_dataset_phys_t *ds;
1704 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1705 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1708 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1709 dir_obj = ds->ds_dir_obj;
1711 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1712 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1715 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1717 child_dir_zapobj = dd->dd_child_dir_zapobj;
1718 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1719 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1723 return (zap_list(spa, &child_dir_zap) != 0);
1728 * Find the object set given the object number of its dataset object
1729 * and return its details in *objset
1732 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1734 dnode_phys_t dataset;
1735 dsl_dataset_phys_t *ds;
1737 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1738 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1742 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1743 if (zio_read(spa, &ds->ds_bp, objset)) {
1744 printf("ZFS: can't read object set for dataset %ju\n",
1753 * Find the object set pointed to by the BOOTFS property or the root
1754 * dataset if there is none and return its details in *objset
1757 zfs_get_root(const spa_t *spa, uint64_t *objid)
1759 dnode_phys_t dir, propdir;
1760 uint64_t props, bootfs, root;
1765 * Start with the MOS directory object.
1767 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1768 printf("ZFS: can't read MOS object directory\n");
1773 * Lookup the pool_props and see if we can find a bootfs.
1775 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1776 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1777 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1784 * Lookup the root dataset directory
1786 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1787 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1788 printf("ZFS: can't find root dsl_dir\n");
1793 * Use the information from the dataset directory's bonus buffer
1794 * to find the dataset object and from that the object set itself.
1796 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1797 *objid = dd->dd_head_dataset_obj;
1802 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1808 * Find the root object set if not explicitly provided
1810 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1811 printf("ZFS: can't find root filesystem\n");
1815 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1816 printf("ZFS: can't open root filesystem\n");
1820 mount->rootobj = rootobj;
1826 zfs_spa_init(spa_t *spa)
1829 if (spa->spa_inited)
1831 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1832 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1835 spa->spa_inited = 1;
1840 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1843 if (dn->dn_bonustype != DMU_OT_SA) {
1844 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1846 sb->st_mode = zp->zp_mode;
1847 sb->st_uid = zp->zp_uid;
1848 sb->st_gid = zp->zp_gid;
1849 sb->st_size = zp->zp_size;
1851 sa_hdr_phys_t *sahdrp;
1856 if (dn->dn_bonuslen != 0)
1857 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1859 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1860 blkptr_t *bp = &dn->dn_spill;
1863 size = BP_GET_LSIZE(bp);
1864 buf = zfs_alloc(size);
1865 error = zio_read(spa, bp, buf);
1867 zfs_free(buf, size);
1875 hdrsize = SA_HDR_SIZE(sahdrp);
1876 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1878 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1880 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1882 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1885 zfs_free(buf, size);
1892 * Lookup a file and return its dnode.
1895 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1898 uint64_t objnum, rootnum, parentnum;
1904 int symlinks_followed = 0;
1908 if (mount->objset.os_type != DMU_OST_ZFS) {
1909 printf("ZFS: unexpected object set type %ju\n",
1910 (uintmax_t)mount->objset.os_type);
1915 * Get the root directory dnode.
1917 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
1921 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1925 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
1938 memcpy(element, p, q - p);
1946 rc = zfs_dnode_stat(spa, &dn, &sb);
1949 if (!S_ISDIR(sb.st_mode))
1953 rc = zap_lookup(spa, &dn, element, &objnum);
1956 objnum = ZFS_DIRENT_OBJ(objnum);
1958 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
1963 * Check for symlink.
1965 rc = zfs_dnode_stat(spa, &dn, &sb);
1968 if (S_ISLNK(sb.st_mode)) {
1969 if (symlinks_followed > 10)
1971 symlinks_followed++;
1974 * Read the link value and copy the tail of our
1975 * current path onto the end.
1978 strcpy(&path[sb.st_size], p);
1980 path[sb.st_size] = 0;
1981 if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1982 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1985 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
1991 * Restart with the new path, starting either at
1992 * the root or at the parent depending whether or
1993 * not the link is relative.
2000 objset_get_dnode(spa, &mount->objset, objnum, &dn);