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))
653 spa_get_primary(void)
656 return (STAILQ_FIRST(&zfs_pools));
660 spa_get_primary_vdev(const spa_t *spa)
666 spa = spa_get_primary();
669 vdev = STAILQ_FIRST(&spa->spa_vdevs);
672 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
673 kid = STAILQ_FIRST(&vdev->v_children))
680 spa_create(uint64_t guid)
684 spa = malloc(sizeof(spa_t));
685 memset(spa, 0, sizeof(spa_t));
686 STAILQ_INIT(&spa->spa_vdevs);
687 spa->spa_guid = guid;
688 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
694 state_name(vdev_state_t state)
696 static const char* names[] = {
711 #define pager_printf printf
716 pager_printf(const char *fmt, ...)
722 vsprintf(line, fmt, args);
729 #define STATUS_FORMAT " %s %s\n"
732 print_state(int indent, const char *name, vdev_state_t state)
738 for (i = 0; i < indent; i++)
741 pager_printf(STATUS_FORMAT, buf, state_name(state));
746 vdev_status(vdev_t *vdev, int indent)
749 print_state(indent, vdev->v_name, vdev->v_state);
751 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
752 vdev_status(kid, indent + 1);
757 spa_status(spa_t *spa)
759 static char bootfs[ZFS_MAXNAMELEN];
762 int good_kids, bad_kids, degraded_kids;
765 pager_printf(" pool: %s\n", spa->spa_name);
766 if (zfs_get_root(spa, &rootid) == 0 &&
767 zfs_rlookup(spa, rootid, bootfs) == 0) {
768 if (bootfs[0] == '\0')
769 pager_printf("bootfs: %s\n", spa->spa_name);
771 pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
773 pager_printf("config:\n\n");
774 pager_printf(STATUS_FORMAT, "NAME", "STATE");
779 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
780 if (vdev->v_state == VDEV_STATE_HEALTHY)
782 else if (vdev->v_state == VDEV_STATE_DEGRADED)
788 state = VDEV_STATE_CLOSED;
789 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
790 state = VDEV_STATE_HEALTHY;
791 else if ((good_kids + degraded_kids) > 0)
792 state = VDEV_STATE_DEGRADED;
794 print_state(0, spa->spa_name, state);
795 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
796 vdev_status(vdev, 1);
806 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
815 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
818 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
820 vdev_t *vdev, *top_vdev, *pool_vdev;
823 const unsigned char *nvlist;
826 uint64_t pool_txg, pool_guid;
828 const char *pool_name;
829 const unsigned char *vdevs;
832 const struct uberblock *up;
835 * Load the vdev label and figure out which
836 * uberblock is most current.
838 memset(&vtmp, 0, sizeof(vtmp));
839 vtmp.v_phys_read = read;
840 vtmp.v_read_priv = read_priv;
841 off = offsetof(vdev_label_t, vl_vdev_phys);
843 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
844 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
845 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
846 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
847 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
848 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
849 if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
852 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
856 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
858 if (nvlist_find(nvlist,
859 ZPOOL_CONFIG_VERSION,
860 DATA_TYPE_UINT64, 0, &val)) {
864 if (val > SPA_VERSION) {
865 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
866 (unsigned) val, (unsigned) SPA_VERSION);
870 if (nvlist_find(nvlist,
871 ZPOOL_CONFIG_POOL_STATE,
872 DATA_TYPE_UINT64, 0, &val)) {
876 if (val == POOL_STATE_DESTROYED) {
877 /* We don't boot only from destroyed pools. */
881 if (nvlist_find(nvlist,
882 ZPOOL_CONFIG_POOL_TXG,
883 DATA_TYPE_UINT64, 0, &pool_txg)
884 || nvlist_find(nvlist,
885 ZPOOL_CONFIG_POOL_GUID,
886 DATA_TYPE_UINT64, 0, &pool_guid)
887 || nvlist_find(nvlist,
888 ZPOOL_CONFIG_POOL_NAME,
889 DATA_TYPE_STRING, 0, &pool_name)) {
891 * Cache and spare devices end up here - just ignore
894 /*printf("ZFS: can't find pool details\n");*/
899 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
905 * Create the pool if this is the first time we've seen it.
907 spa = spa_find_by_guid(pool_guid);
909 spa = spa_create(pool_guid);
910 spa->spa_name = strdup(pool_name);
912 if (pool_txg > spa->spa_txg) {
913 spa->spa_txg = pool_txg;
919 * Get the vdev tree and create our in-core copy of it.
920 * If we already have a vdev with this guid, this must
921 * be some kind of alias (overlapping slices, dangerously dedicated
924 if (nvlist_find(nvlist,
926 DATA_TYPE_UINT64, 0, &guid)) {
929 vdev = vdev_find(guid);
930 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
933 if (nvlist_find(nvlist,
934 ZPOOL_CONFIG_VDEV_TREE,
935 DATA_TYPE_NVLIST, 0, &vdevs)) {
939 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
944 * Add the toplevel vdev to the pool if its not already there.
946 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
947 if (top_vdev == pool_vdev)
949 if (!pool_vdev && top_vdev)
950 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
953 * We should already have created an incomplete vdev for this
954 * vdev. Find it and initialise it with our read proc.
956 vdev = vdev_find(guid);
958 vdev->v_phys_read = read;
959 vdev->v_read_priv = read_priv;
960 vdev->v_state = VDEV_STATE_HEALTHY;
962 printf("ZFS: inconsistent nvlist contents\n");
967 * Re-evaluate top-level vdev state.
969 vdev_set_state(top_vdev);
972 * Ok, we are happy with the pool so far. Lets find
973 * the best uberblock and then we can actually access
974 * the contents of the pool.
976 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
977 up = (const struct uberblock *)upbuf;
979 i < VDEV_UBERBLOCK_COUNT(vdev);
981 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
983 DVA_SET_OFFSET(&bp.blk_dva[0], off);
984 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
985 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
986 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
987 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
988 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
990 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
993 if (up->ub_magic != UBERBLOCK_MAGIC)
995 if (up->ub_txg < spa->spa_txg)
997 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
998 spa->spa_uberblock = *up;
999 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1000 if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1001 spa->spa_uberblock = *up;
1004 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1016 for (v = 0; v < 32; v++)
1023 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1026 zio_gbh_phys_t zio_gb;
1030 /* Artificial BP for gang block header. */
1032 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1033 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1034 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1035 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1036 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1037 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1039 /* Read gang header block using the artificial BP. */
1040 if (zio_read(spa, &gbh_bp, &zio_gb))
1044 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1045 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1047 if (BP_IS_HOLE(gbp))
1049 if (zio_read(spa, gbp, pbuf))
1051 pbuf += BP_GET_PSIZE(gbp);
1054 if (zio_checksum_verify(bp, buf))
1060 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1062 int cpfunc = BP_GET_COMPRESS(bp);
1063 uint64_t align, size;
1069 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1070 const dva_t *dva = &bp->blk_dva[i];
1075 if (!dva->dva_word[0] && !dva->dva_word[1])
1078 vdevid = DVA_GET_VDEV(dva);
1079 offset = DVA_GET_OFFSET(dva);
1080 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1081 if (vdev->v_id == vdevid)
1084 if (!vdev || !vdev->v_read)
1087 size = BP_GET_PSIZE(bp);
1088 if (vdev->v_read == vdev_raidz_read) {
1089 align = 1ULL << vdev->v_top->v_ashift;
1090 if (P2PHASE(size, align) != 0)
1091 size = P2ROUNDUP(size, align);
1093 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1094 pbuf = zfs_alloc(size);
1098 if (DVA_GET_GANG(dva))
1099 error = zio_read_gang(spa, bp, pbuf);
1101 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1103 if (cpfunc != ZIO_COMPRESS_OFF)
1104 error = zio_decompress_data(cpfunc, pbuf,
1105 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1106 else if (size != BP_GET_PSIZE(bp))
1107 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1110 zfs_free(pbuf, size);
1115 printf("ZFS: i/o error - all block copies unavailable\n");
1120 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1122 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1123 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1124 int nlevels = dnode->dn_nlevels;
1128 * Note: bsize may not be a power of two here so we need to do an
1129 * actual divide rather than a bitshift.
1131 while (buflen > 0) {
1132 uint64_t bn = offset / bsize;
1133 int boff = offset % bsize;
1135 const blkptr_t *indbp;
1138 if (bn > dnode->dn_maxblkid)
1141 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1144 indbp = dnode->dn_blkptr;
1145 for (i = 0; i < nlevels; i++) {
1147 * Copy the bp from the indirect array so that
1148 * we can re-use the scratch buffer for multi-level
1151 ibn = bn >> ((nlevels - i - 1) * ibshift);
1152 ibn &= ((1 << ibshift) - 1);
1154 rc = zio_read(spa, &bp, dnode_cache_buf);
1157 indbp = (const blkptr_t *) dnode_cache_buf;
1159 dnode_cache_obj = dnode;
1160 dnode_cache_bn = bn;
1164 * The buffer contains our data block. Copy what we
1165 * need from it and loop.
1168 if (i > buflen) i = buflen;
1169 memcpy(buf, &dnode_cache_buf[boff], i);
1170 buf = ((char*) buf) + i;
1179 * Lookup a value in a microzap directory. Assumes that the zap
1180 * scratch buffer contains the directory contents.
1183 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1185 const mzap_phys_t *mz;
1186 const mzap_ent_phys_t *mze;
1191 * Microzap objects use exactly one block. Read the whole
1194 size = dnode->dn_datablkszsec * 512;
1196 mz = (const mzap_phys_t *) zap_scratch;
1197 chunks = size / MZAP_ENT_LEN - 1;
1199 for (i = 0; i < chunks; i++) {
1200 mze = &mz->mz_chunk[i];
1201 if (!strcmp(mze->mze_name, name)) {
1202 *value = mze->mze_value;
1211 * Compare a name with a zap leaf entry. Return non-zero if the name
1215 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1218 const zap_leaf_chunk_t *nc;
1221 namelen = zc->l_entry.le_name_numints;
1223 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1225 while (namelen > 0) {
1228 if (len > ZAP_LEAF_ARRAY_BYTES)
1229 len = ZAP_LEAF_ARRAY_BYTES;
1230 if (memcmp(p, nc->l_array.la_array, len))
1234 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1241 * Extract a uint64_t value from a zap leaf entry.
1244 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1246 const zap_leaf_chunk_t *vc;
1251 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1252 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1253 value = (value << 8) | p[i];
1260 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1261 * buffer contains the directory header.
1264 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1266 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1267 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1273 if (zh.zap_magic != ZAP_MAGIC)
1276 z.zap_block_shift = ilog2(bsize);
1277 z.zap_phys = (zap_phys_t *) zap_scratch;
1280 * Figure out where the pointer table is and read it in if necessary.
1282 if (zh.zap_ptrtbl.zt_blk) {
1283 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1284 zap_scratch, bsize);
1287 ptrtbl = (uint64_t *) zap_scratch;
1289 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1292 hash = zap_hash(zh.zap_salt, name);
1295 zl.l_bs = z.zap_block_shift;
1297 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1298 zap_leaf_chunk_t *zc;
1300 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1304 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1307 * Make sure this chunk matches our hash.
1309 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1310 && zl.l_phys->l_hdr.lh_prefix
1311 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1315 * Hash within the chunk to find our entry.
1317 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1318 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1319 h = zl.l_phys->l_hash[h];
1322 zc = &ZAP_LEAF_CHUNK(&zl, h);
1323 while (zc->l_entry.le_hash != hash) {
1324 if (zc->l_entry.le_next == 0xffff) {
1328 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1330 if (fzap_name_equal(&zl, zc, name)) {
1331 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1333 *value = fzap_leaf_value(&zl, zc);
1341 * Lookup a name in a zap object and return its value as a uint64_t.
1344 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1348 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1350 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1354 zap_type = *(uint64_t *) zap_scratch;
1355 if (zap_type == ZBT_MICRO)
1356 return mzap_lookup(dnode, name, value);
1357 else if (zap_type == ZBT_HEADER)
1358 return fzap_lookup(spa, dnode, name, value);
1359 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1364 * List a microzap directory. Assumes that the zap scratch buffer contains
1365 * the directory contents.
1368 mzap_list(const dnode_phys_t *dnode)
1370 const mzap_phys_t *mz;
1371 const mzap_ent_phys_t *mze;
1376 * Microzap objects use exactly one block. Read the whole
1379 size = dnode->dn_datablkszsec * 512;
1380 mz = (const mzap_phys_t *) zap_scratch;
1381 chunks = size / MZAP_ENT_LEN - 1;
1383 for (i = 0; i < chunks; i++) {
1384 mze = &mz->mz_chunk[i];
1385 if (mze->mze_name[0])
1386 //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1387 printf("%s\n", mze->mze_name);
1394 * List a fatzap directory. Assumes that the zap scratch buffer contains
1395 * the directory header.
1398 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1400 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1401 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1405 if (zh.zap_magic != ZAP_MAGIC)
1408 z.zap_block_shift = ilog2(bsize);
1409 z.zap_phys = (zap_phys_t *) zap_scratch;
1412 * This assumes that the leaf blocks start at block 1. The
1413 * documentation isn't exactly clear on this.
1416 zl.l_bs = z.zap_block_shift;
1417 for (i = 0; i < zh.zap_num_leafs; i++) {
1418 off_t off = (i + 1) << zl.l_bs;
1422 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1425 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1427 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1428 zap_leaf_chunk_t *zc, *nc;
1431 zc = &ZAP_LEAF_CHUNK(&zl, j);
1432 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1434 namelen = zc->l_entry.le_name_numints;
1435 if (namelen > sizeof(name))
1436 namelen = sizeof(name);
1439 * Paste the name back together.
1441 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1443 while (namelen > 0) {
1446 if (len > ZAP_LEAF_ARRAY_BYTES)
1447 len = ZAP_LEAF_ARRAY_BYTES;
1448 memcpy(p, nc->l_array.la_array, len);
1451 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1455 * Assume the first eight bytes of the value are
1458 value = fzap_leaf_value(&zl, zc);
1460 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1461 printf("%s\n", name);
1469 * List a zap directory.
1472 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1475 size_t size = dnode->dn_datablkszsec * 512;
1477 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1480 zap_type = *(uint64_t *) zap_scratch;
1481 if (zap_type == ZBT_MICRO)
1482 return mzap_list(dnode);
1484 return fzap_list(spa, dnode);
1488 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1492 offset = objnum * sizeof(dnode_phys_t);
1493 return dnode_read(spa, &os->os_meta_dnode, offset,
1494 dnode, sizeof(dnode_phys_t));
1498 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1500 const mzap_phys_t *mz;
1501 const mzap_ent_phys_t *mze;
1506 * Microzap objects use exactly one block. Read the whole
1509 size = dnode->dn_datablkszsec * 512;
1511 mz = (const mzap_phys_t *) zap_scratch;
1512 chunks = size / MZAP_ENT_LEN - 1;
1514 for (i = 0; i < chunks; i++) {
1515 mze = &mz->mz_chunk[i];
1516 if (value == mze->mze_value) {
1517 strcpy(name, mze->mze_name);
1526 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1529 const zap_leaf_chunk_t *nc;
1532 namelen = zc->l_entry.le_name_numints;
1534 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1536 while (namelen > 0) {
1539 if (len > ZAP_LEAF_ARRAY_BYTES)
1540 len = ZAP_LEAF_ARRAY_BYTES;
1541 memcpy(p, nc->l_array.la_array, len);
1544 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1551 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1553 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1554 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1558 if (zh.zap_magic != ZAP_MAGIC)
1561 z.zap_block_shift = ilog2(bsize);
1562 z.zap_phys = (zap_phys_t *) zap_scratch;
1565 * This assumes that the leaf blocks start at block 1. The
1566 * documentation isn't exactly clear on this.
1569 zl.l_bs = z.zap_block_shift;
1570 for (i = 0; i < zh.zap_num_leafs; i++) {
1571 off_t off = (i + 1) << zl.l_bs;
1573 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1576 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1578 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1579 zap_leaf_chunk_t *zc;
1581 zc = &ZAP_LEAF_CHUNK(&zl, j);
1582 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1584 if (zc->l_entry.le_value_intlen != 8 ||
1585 zc->l_entry.le_value_numints != 1)
1588 if (fzap_leaf_value(&zl, zc) == value) {
1589 fzap_name_copy(&zl, zc, name);
1599 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1603 size_t size = dnode->dn_datablkszsec * 512;
1605 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1609 zap_type = *(uint64_t *) zap_scratch;
1610 if (zap_type == ZBT_MICRO)
1611 return mzap_rlookup(spa, dnode, name, value);
1613 return fzap_rlookup(spa, dnode, name, value);
1617 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1620 char component[256];
1621 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1622 dnode_phys_t child_dir_zap, dataset, dir, parent;
1624 dsl_dataset_phys_t *ds;
1628 p = &name[sizeof(name) - 1];
1631 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1632 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1635 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1636 dir_obj = ds->ds_dir_obj;
1639 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1641 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1643 /* Actual loop condition. */
1644 parent_obj = dd->dd_parent_obj;
1645 if (parent_obj == 0)
1648 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1650 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1651 child_dir_zapobj = dd->dd_child_dir_zapobj;
1652 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1654 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1657 len = strlen(component);
1659 memcpy(p, component, len);
1663 /* Actual loop iteration. */
1664 dir_obj = parent_obj;
1675 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1678 uint64_t dir_obj, child_dir_zapobj;
1679 dnode_phys_t child_dir_zap, dir;
1683 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1685 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1690 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1692 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1696 /* Actual loop condition #1. */
1702 memcpy(element, p, q - p);
1703 element[q - p] = '\0';
1710 child_dir_zapobj = dd->dd_child_dir_zapobj;
1711 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1714 /* Actual loop condition #2. */
1715 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1719 *objnum = dd->dd_head_dataset_obj;
1725 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1727 uint64_t dir_obj, child_dir_zapobj;
1728 dnode_phys_t child_dir_zap, dir, dataset;
1729 dsl_dataset_phys_t *ds;
1732 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1733 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1736 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1737 dir_obj = ds->ds_dir_obj;
1739 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1740 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1743 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1745 child_dir_zapobj = dd->dd_child_dir_zapobj;
1746 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1747 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1751 return (zap_list(spa, &child_dir_zap) != 0);
1756 * Find the object set given the object number of its dataset object
1757 * and return its details in *objset
1760 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1762 dnode_phys_t dataset;
1763 dsl_dataset_phys_t *ds;
1765 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1766 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1770 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1771 if (zio_read(spa, &ds->ds_bp, objset)) {
1772 printf("ZFS: can't read object set for dataset %ju\n",
1781 * Find the object set pointed to by the BOOTFS property or the root
1782 * dataset if there is none and return its details in *objset
1785 zfs_get_root(const spa_t *spa, uint64_t *objid)
1787 dnode_phys_t dir, propdir;
1788 uint64_t props, bootfs, root;
1793 * Start with the MOS directory object.
1795 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1796 printf("ZFS: can't read MOS object directory\n");
1801 * Lookup the pool_props and see if we can find a bootfs.
1803 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1804 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1805 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1812 * Lookup the root dataset directory
1814 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1815 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1816 printf("ZFS: can't find root dsl_dir\n");
1821 * Use the information from the dataset directory's bonus buffer
1822 * to find the dataset object and from that the object set itself.
1824 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1825 *objid = dd->dd_head_dataset_obj;
1830 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1836 * Find the root object set if not explicitly provided
1838 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1839 printf("ZFS: can't find root filesystem\n");
1843 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1844 printf("ZFS: can't open root filesystem\n");
1848 mount->rootobj = rootobj;
1854 zfs_spa_init(spa_t *spa)
1857 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1858 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1861 if (spa->spa_mos.os_type != DMU_OST_META) {
1862 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
1869 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1872 if (dn->dn_bonustype != DMU_OT_SA) {
1873 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1875 sb->st_mode = zp->zp_mode;
1876 sb->st_uid = zp->zp_uid;
1877 sb->st_gid = zp->zp_gid;
1878 sb->st_size = zp->zp_size;
1880 sa_hdr_phys_t *sahdrp;
1885 if (dn->dn_bonuslen != 0)
1886 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1888 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1889 blkptr_t *bp = &dn->dn_spill;
1892 size = BP_GET_LSIZE(bp);
1893 buf = zfs_alloc(size);
1894 error = zio_read(spa, bp, buf);
1896 zfs_free(buf, size);
1904 hdrsize = SA_HDR_SIZE(sahdrp);
1905 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1907 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1909 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1911 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1914 zfs_free(buf, size);
1921 * Lookup a file and return its dnode.
1924 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1927 uint64_t objnum, rootnum, parentnum;
1933 int symlinks_followed = 0;
1937 if (mount->objset.os_type != DMU_OST_ZFS) {
1938 printf("ZFS: unexpected object set type %ju\n",
1939 (uintmax_t)mount->objset.os_type);
1944 * Get the root directory dnode.
1946 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
1950 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1954 rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
1967 memcpy(element, p, q - p);
1975 rc = zfs_dnode_stat(spa, &dn, &sb);
1978 if (!S_ISDIR(sb.st_mode))
1982 rc = zap_lookup(spa, &dn, element, &objnum);
1985 objnum = ZFS_DIRENT_OBJ(objnum);
1987 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
1992 * Check for symlink.
1994 rc = zfs_dnode_stat(spa, &dn, &sb);
1997 if (S_ISLNK(sb.st_mode)) {
1998 if (symlinks_followed > 10)
2000 symlinks_followed++;
2003 * Read the link value and copy the tail of our
2004 * current path onto the end.
2007 strcpy(&path[sb.st_size], p);
2009 path[sb.st_size] = 0;
2010 if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
2011 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2014 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2020 * Restart with the new path, starting either at
2021 * the root or at the parent depending whether or
2022 * not the link is relative.
2029 objset_get_dnode(spa, &mount->objset, objnum, &dn);