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>
46 static struct zfsmount zfsmount __unused;
49 * List of all vdevs, chained through v_alllink.
51 static vdev_list_t zfs_vdevs;
54 * List of ZFS features supported for read
56 static const char *features_for_read[] = {
57 "org.illumos:lz4_compress",
58 "com.delphix:hole_birth",
59 "com.delphix:extensible_dataset",
60 "com.delphix:embedded_data",
61 "org.open-zfs:large_blocks",
64 "org.zfsonlinux:large_dnode",
65 "com.joyent:multi_vdev_crash_dump",
70 * List of all pools, chained through spa_link.
72 static spa_list_t zfs_pools;
74 static const dnode_phys_t *dnode_cache_obj;
75 static uint64_t dnode_cache_bn;
76 static char *dnode_cache_buf;
77 static char *zap_scratch;
78 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
80 #define TEMP_SIZE (1024 * 1024)
82 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
83 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
84 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
85 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
86 const char *name, uint64_t integer_size, uint64_t num_integers,
92 STAILQ_INIT(&zfs_vdevs);
93 STAILQ_INIT(&zfs_pools);
95 zfs_temp_buf = malloc(TEMP_SIZE);
96 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
97 zfs_temp_ptr = zfs_temp_buf;
98 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
99 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
105 zfs_alloc(size_t size)
109 if (zfs_temp_ptr + size > zfs_temp_end) {
110 printf("ZFS: out of temporary buffer space\n");
114 zfs_temp_ptr += size;
120 zfs_free(void *ptr, size_t size)
123 zfs_temp_ptr -= size;
124 if (zfs_temp_ptr != ptr) {
125 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
131 xdr_int(const unsigned char **xdr, int *ip)
133 *ip = ((*xdr)[0] << 24)
142 xdr_u_int(const unsigned char **xdr, u_int *ip)
144 *ip = ((*xdr)[0] << 24)
153 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
159 *lp = (((uint64_t) hi) << 32) | lo;
164 nvlist_find(const unsigned char *nvlist, const char *name, int type,
165 int* elementsp, void *valuep)
167 const unsigned char *p, *pair;
169 int encoded_size, decoded_size;
176 xdr_int(&p, &encoded_size);
177 xdr_int(&p, &decoded_size);
178 while (encoded_size && decoded_size) {
179 int namelen, pairtype, elements;
180 const char *pairname;
182 xdr_int(&p, &namelen);
183 pairname = (const char*) p;
184 p += roundup(namelen, 4);
185 xdr_int(&p, &pairtype);
187 if (!memcmp(name, pairname, namelen) && type == pairtype) {
188 xdr_int(&p, &elements);
190 *elementsp = elements;
191 if (type == DATA_TYPE_UINT64) {
192 xdr_uint64_t(&p, (uint64_t *) valuep);
194 } else if (type == DATA_TYPE_STRING) {
197 (*(const char**) valuep) = (const char*) p;
199 } else if (type == DATA_TYPE_NVLIST
200 || type == DATA_TYPE_NVLIST_ARRAY) {
201 (*(const unsigned char**) valuep) =
202 (const unsigned char*) p;
209 * Not the pair we are looking for, skip to the next one.
211 p = pair + encoded_size;
215 xdr_int(&p, &encoded_size);
216 xdr_int(&p, &decoded_size);
223 nvlist_check_features_for_read(const unsigned char *nvlist)
225 const unsigned char *p, *pair;
227 int encoded_size, decoded_size;
237 xdr_int(&p, &encoded_size);
238 xdr_int(&p, &decoded_size);
239 while (encoded_size && decoded_size) {
240 int namelen, pairtype;
241 const char *pairname;
246 xdr_int(&p, &namelen);
247 pairname = (const char*) p;
248 p += roundup(namelen, 4);
249 xdr_int(&p, &pairtype);
251 for (i = 0; features_for_read[i] != NULL; i++) {
252 if (!memcmp(pairname, features_for_read[i], namelen)) {
259 printf("ZFS: unsupported feature: %s\n", pairname);
263 p = pair + encoded_size;
266 xdr_int(&p, &encoded_size);
267 xdr_int(&p, &decoded_size);
274 * Return the next nvlist in an nvlist array.
276 static const unsigned char *
277 nvlist_next(const unsigned char *nvlist)
279 const unsigned char *p, *pair;
281 int encoded_size, decoded_size;
288 xdr_int(&p, &encoded_size);
289 xdr_int(&p, &decoded_size);
290 while (encoded_size && decoded_size) {
291 p = pair + encoded_size;
294 xdr_int(&p, &encoded_size);
295 xdr_int(&p, &decoded_size);
303 static const unsigned char *
304 nvlist_print(const unsigned char *nvlist, unsigned int indent)
306 static const char* typenames[] = {
317 "DATA_TYPE_BYTE_ARRAY",
318 "DATA_TYPE_INT16_ARRAY",
319 "DATA_TYPE_UINT16_ARRAY",
320 "DATA_TYPE_INT32_ARRAY",
321 "DATA_TYPE_UINT32_ARRAY",
322 "DATA_TYPE_INT64_ARRAY",
323 "DATA_TYPE_UINT64_ARRAY",
324 "DATA_TYPE_STRING_ARRAY",
327 "DATA_TYPE_NVLIST_ARRAY",
328 "DATA_TYPE_BOOLEAN_VALUE",
331 "DATA_TYPE_BOOLEAN_ARRAY",
332 "DATA_TYPE_INT8_ARRAY",
333 "DATA_TYPE_UINT8_ARRAY"
337 const unsigned char *p, *pair;
339 int encoded_size, decoded_size;
346 xdr_int(&p, &encoded_size);
347 xdr_int(&p, &decoded_size);
348 while (encoded_size && decoded_size) {
349 int namelen, pairtype, elements;
350 const char *pairname;
352 xdr_int(&p, &namelen);
353 pairname = (const char*) p;
354 p += roundup(namelen, 4);
355 xdr_int(&p, &pairtype);
357 for (i = 0; i < indent; i++)
359 printf("%s %s", typenames[pairtype], pairname);
361 xdr_int(&p, &elements);
363 case DATA_TYPE_UINT64: {
365 xdr_uint64_t(&p, &val);
366 printf(" = 0x%jx\n", (uintmax_t)val);
370 case DATA_TYPE_STRING: {
373 printf(" = \"%s\"\n", p);
377 case DATA_TYPE_NVLIST:
379 nvlist_print(p, indent + 1);
382 case DATA_TYPE_NVLIST_ARRAY:
383 for (j = 0; j < elements; j++) {
385 p = nvlist_print(p, indent + 1);
386 if (j != elements - 1) {
387 for (i = 0; i < indent; i++)
389 printf("%s %s", typenames[pairtype], pairname);
398 p = pair + encoded_size;
401 xdr_int(&p, &encoded_size);
402 xdr_int(&p, &decoded_size);
411 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
412 off_t offset, size_t size)
417 if (!vdev->v_phys_read)
421 psize = BP_GET_PSIZE(bp);
426 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
427 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
430 if (bp && zio_checksum_verify(vdev->spa, bp, buf))
437 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
438 off_t offset, size_t bytes)
441 return (vdev_read_phys(vdev, bp, buf,
442 offset + VDEV_LABEL_START_SIZE, bytes));
447 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
448 off_t offset, size_t bytes)
454 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
455 if (kid->v_state != VDEV_STATE_HEALTHY)
457 rc = kid->v_read(kid, bp, buf, offset, bytes);
466 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
467 off_t offset, size_t bytes)
472 * Here we should have two kids:
473 * First one which is the one we are replacing and we can trust
474 * only this one to have valid data, but it might not be present.
475 * Second one is that one we are replacing with. It is most likely
476 * healthy, but we can't trust it has needed data, so we won't use it.
478 kid = STAILQ_FIRST(&vdev->v_children);
481 if (kid->v_state != VDEV_STATE_HEALTHY)
483 return (kid->v_read(kid, bp, buf, offset, bytes));
487 vdev_find(uint64_t guid)
491 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
492 if (vdev->v_guid == guid)
499 vdev_create(uint64_t guid, vdev_read_t *_read)
503 vdev = malloc(sizeof(vdev_t));
504 memset(vdev, 0, sizeof(vdev_t));
505 STAILQ_INIT(&vdev->v_children);
507 vdev->v_state = VDEV_STATE_OFFLINE;
508 vdev->v_read = _read;
509 vdev->v_phys_read = 0;
510 vdev->v_read_priv = 0;
511 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
517 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
518 vdev_t **vdevp, int is_newer)
521 uint64_t guid, id, ashift, nparity;
525 const unsigned char *kids;
526 int nkids, i, is_new;
527 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
529 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
531 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
532 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
534 printf("ZFS: can't find vdev details\n");
538 if (strcmp(type, VDEV_TYPE_MIRROR)
539 && strcmp(type, VDEV_TYPE_DISK)
541 && strcmp(type, VDEV_TYPE_FILE)
543 && strcmp(type, VDEV_TYPE_RAIDZ)
544 && strcmp(type, VDEV_TYPE_REPLACING)) {
545 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
549 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
551 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
553 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
555 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
557 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
559 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
562 vdev = vdev_find(guid);
566 if (!strcmp(type, VDEV_TYPE_MIRROR))
567 vdev = vdev_create(guid, vdev_mirror_read);
568 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
569 vdev = vdev_create(guid, vdev_raidz_read);
570 else if (!strcmp(type, VDEV_TYPE_REPLACING))
571 vdev = vdev_create(guid, vdev_replacing_read);
573 vdev = vdev_create(guid, vdev_disk_read);
576 vdev->v_top = pvdev != NULL ? pvdev : vdev;
577 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
578 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
579 vdev->v_ashift = ashift;
583 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
584 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
585 vdev->v_nparity = nparity;
589 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
590 DATA_TYPE_STRING, NULL, &path) == 0) {
591 if (strncmp(path, "/dev/", 5) == 0)
593 vdev->v_name = strdup(path);
595 if (!strcmp(type, "raidz")) {
596 if (vdev->v_nparity == 1)
597 vdev->v_name = "raidz1";
598 else if (vdev->v_nparity == 2)
599 vdev->v_name = "raidz2";
600 else if (vdev->v_nparity == 3)
601 vdev->v_name = "raidz3";
603 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
607 vdev->v_name = strdup(type);
614 if (is_new || is_newer) {
616 * This is either new vdev or we've already seen this vdev,
617 * but from an older vdev label, so let's refresh its state
618 * from the newer label.
621 vdev->v_state = VDEV_STATE_OFFLINE;
623 vdev->v_state = VDEV_STATE_REMOVED;
625 vdev->v_state = VDEV_STATE_FAULTED;
626 else if (is_degraded)
627 vdev->v_state = VDEV_STATE_DEGRADED;
628 else if (isnt_present)
629 vdev->v_state = VDEV_STATE_CANT_OPEN;
632 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
635 * Its ok if we don't have any kids.
638 vdev->v_nchildren = nkids;
639 for (i = 0; i < nkids; i++) {
640 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
644 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
646 kids = nvlist_next(kids);
649 vdev->v_nchildren = 0;
658 vdev_set_state(vdev_t *vdev)
665 * A mirror or raidz is healthy if all its kids are healthy. A
666 * mirror is degraded if any of its kids is healthy; a raidz
667 * is degraded if at most nparity kids are offline.
669 if (STAILQ_FIRST(&vdev->v_children)) {
672 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
673 if (kid->v_state == VDEV_STATE_HEALTHY)
679 vdev->v_state = VDEV_STATE_HEALTHY;
681 if (vdev->v_read == vdev_mirror_read) {
683 vdev->v_state = VDEV_STATE_DEGRADED;
685 vdev->v_state = VDEV_STATE_OFFLINE;
687 } else if (vdev->v_read == vdev_raidz_read) {
688 if (bad_kids > vdev->v_nparity) {
689 vdev->v_state = VDEV_STATE_OFFLINE;
691 vdev->v_state = VDEV_STATE_DEGRADED;
699 spa_find_by_guid(uint64_t guid)
703 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
704 if (spa->spa_guid == guid)
711 spa_find_by_name(const char *name)
715 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
716 if (!strcmp(spa->spa_name, name))
724 spa_get_primary(void)
727 return (STAILQ_FIRST(&zfs_pools));
731 spa_get_primary_vdev(const spa_t *spa)
737 spa = spa_get_primary();
740 vdev = STAILQ_FIRST(&spa->spa_vdevs);
743 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
744 kid = STAILQ_FIRST(&vdev->v_children))
751 spa_create(uint64_t guid, const char *name)
755 if ((spa = malloc(sizeof(spa_t))) == NULL)
757 memset(spa, 0, sizeof(spa_t));
758 if ((spa->spa_name = strdup(name)) == NULL) {
762 STAILQ_INIT(&spa->spa_vdevs);
763 spa->spa_guid = guid;
764 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
770 state_name(vdev_state_t state)
772 static const char* names[] = {
787 #define pager_printf printf
792 pager_printf(const char *fmt, ...)
798 vsprintf(line, fmt, args);
801 return (pager_output(line));
806 #define STATUS_FORMAT " %s %s\n"
809 print_state(int indent, const char *name, vdev_state_t state)
815 for (i = 0; i < indent; i++)
819 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
823 vdev_status(vdev_t *vdev, int indent)
827 ret = print_state(indent, vdev->v_name, vdev->v_state);
831 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
832 ret = vdev_status(kid, indent + 1);
840 spa_status(spa_t *spa)
842 static char bootfs[ZFS_MAXNAMELEN];
845 int good_kids, bad_kids, degraded_kids, ret;
848 ret = pager_printf(" pool: %s\n", spa->spa_name);
852 if (zfs_get_root(spa, &rootid) == 0 &&
853 zfs_rlookup(spa, rootid, bootfs) == 0) {
854 if (bootfs[0] == '\0')
855 ret = pager_printf("bootfs: %s\n", spa->spa_name);
857 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
862 ret = pager_printf("config:\n\n");
865 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
872 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
873 if (vdev->v_state == VDEV_STATE_HEALTHY)
875 else if (vdev->v_state == VDEV_STATE_DEGRADED)
881 state = VDEV_STATE_CLOSED;
882 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
883 state = VDEV_STATE_HEALTHY;
884 else if ((good_kids + degraded_kids) > 0)
885 state = VDEV_STATE_DEGRADED;
887 ret = print_state(0, spa->spa_name, state);
890 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
891 ret = vdev_status(vdev, 1);
902 int first = 1, ret = 0;
904 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
906 ret = pager_printf("\n");
911 ret = spa_status(spa);
919 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
921 uint64_t label_offset;
923 if (l < VDEV_LABELS / 2)
926 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
928 return (offset + l * sizeof (vdev_label_t) + label_offset);
932 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
935 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
936 vdev_phys_t *tmp_label;
938 vdev_t *vdev, *top_vdev, *pool_vdev;
941 const unsigned char *nvlist = NULL;
944 uint64_t best_txg = 0;
945 uint64_t pool_txg, pool_guid;
947 const char *pool_name;
948 const unsigned char *vdevs;
949 const unsigned char *features;
950 int i, l, rc, is_newer;
952 const struct uberblock *up;
955 * Load the vdev label and figure out which
956 * uberblock is most current.
958 memset(&vtmp, 0, sizeof(vtmp));
959 vtmp.v_phys_read = _read;
960 vtmp.v_read_priv = read_priv;
961 psize = P2ALIGN(ldi_get_size(read_priv),
962 (uint64_t)sizeof (vdev_label_t));
964 /* Test for minimum pool size. */
965 if (psize < SPA_MINDEVSIZE)
968 tmp_label = zfs_alloc(sizeof(vdev_phys_t));
970 for (l = 0; l < VDEV_LABELS; l++) {
971 off = vdev_label_offset(psize, l,
972 offsetof(vdev_label_t, vl_vdev_phys));
975 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
976 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
977 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
978 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
979 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
980 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
982 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
985 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
988 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
989 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
990 DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
993 if (best_txg <= pool_txg) {
995 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
999 zfs_free(tmp_label, sizeof (vdev_phys_t));
1004 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
1007 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1009 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1014 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1015 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1016 (unsigned) val, (unsigned) SPA_VERSION);
1020 /* Check ZFS features for read */
1021 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1022 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1023 nvlist_check_features_for_read(features) != 0) {
1027 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1032 if (val == POOL_STATE_DESTROYED) {
1033 /* We don't boot only from destroyed pools. */
1037 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1038 NULL, &pool_txg) != 0 ||
1039 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1040 NULL, &pool_guid) != 0 ||
1041 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1042 NULL, &pool_name) != 0) {
1044 * Cache and spare devices end up here - just ignore
1047 /*printf("ZFS: can't find pool details\n");*/
1051 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1052 NULL, &val) == 0 && val != 0) {
1057 * Create the pool if this is the first time we've seen it.
1059 spa = spa_find_by_guid(pool_guid);
1061 spa = spa_create(pool_guid, pool_name);
1065 if (pool_txg > spa->spa_txg) {
1066 spa->spa_txg = pool_txg;
1073 * Get the vdev tree and create our in-core copy of it.
1074 * If we already have a vdev with this guid, this must
1075 * be some kind of alias (overlapping slices, dangerously dedicated
1078 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1079 NULL, &guid) != 0) {
1082 vdev = vdev_find(guid);
1083 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1086 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1091 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1096 * Add the toplevel vdev to the pool if its not already there.
1098 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1099 if (top_vdev == pool_vdev)
1101 if (!pool_vdev && top_vdev) {
1102 top_vdev->spa = spa;
1103 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1107 * We should already have created an incomplete vdev for this
1108 * vdev. Find it and initialise it with our read proc.
1110 vdev = vdev_find(guid);
1112 vdev->v_phys_read = _read;
1113 vdev->v_read_priv = read_priv;
1114 vdev->v_state = VDEV_STATE_HEALTHY;
1116 printf("ZFS: inconsistent nvlist contents\n");
1121 * Re-evaluate top-level vdev state.
1123 vdev_set_state(top_vdev);
1126 * Ok, we are happy with the pool so far. Lets find
1127 * the best uberblock and then we can actually access
1128 * the contents of the pool.
1130 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1131 up = (const struct uberblock *)upbuf;
1132 for (l = 0; l < VDEV_LABELS; l++) {
1133 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1134 off = vdev_label_offset(psize, l,
1135 VDEV_UBERBLOCK_OFFSET(vdev, i));
1137 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1138 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1139 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1140 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1141 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1142 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1144 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1147 if (up->ub_magic != UBERBLOCK_MAGIC)
1149 if (up->ub_txg < spa->spa_txg)
1151 if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1152 (up->ub_txg == spa->spa_uberblock.ub_txg &&
1154 spa->spa_uberblock.ub_timestamp)) {
1155 spa->spa_uberblock = *up;
1159 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1172 for (v = 0; v < 32; v++)
1179 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1182 zio_gbh_phys_t zio_gb;
1186 /* Artificial BP for gang block header. */
1188 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1189 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1190 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1191 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1192 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1193 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1195 /* Read gang header block using the artificial BP. */
1196 if (zio_read(spa, &gbh_bp, &zio_gb))
1200 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1201 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1203 if (BP_IS_HOLE(gbp))
1205 if (zio_read(spa, gbp, pbuf))
1207 pbuf += BP_GET_PSIZE(gbp);
1210 if (zio_checksum_verify(spa, bp, buf))
1216 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1218 int cpfunc = BP_GET_COMPRESS(bp);
1219 uint64_t align, size;
1224 * Process data embedded in block pointer
1226 if (BP_IS_EMBEDDED(bp)) {
1227 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1229 size = BPE_GET_PSIZE(bp);
1230 ASSERT(size <= BPE_PAYLOAD_SIZE);
1232 if (cpfunc != ZIO_COMPRESS_OFF)
1233 pbuf = zfs_alloc(size);
1237 decode_embedded_bp_compressed(bp, pbuf);
1240 if (cpfunc != ZIO_COMPRESS_OFF) {
1241 error = zio_decompress_data(cpfunc, pbuf,
1242 size, buf, BP_GET_LSIZE(bp));
1243 zfs_free(pbuf, size);
1246 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1253 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1254 const dva_t *dva = &bp->blk_dva[i];
1259 if (!dva->dva_word[0] && !dva->dva_word[1])
1262 vdevid = DVA_GET_VDEV(dva);
1263 offset = DVA_GET_OFFSET(dva);
1264 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1265 if (vdev->v_id == vdevid)
1268 if (!vdev || !vdev->v_read)
1271 size = BP_GET_PSIZE(bp);
1272 if (vdev->v_read == vdev_raidz_read) {
1273 align = 1ULL << vdev->v_top->v_ashift;
1274 if (P2PHASE(size, align) != 0)
1275 size = P2ROUNDUP(size, align);
1277 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1278 pbuf = zfs_alloc(size);
1282 if (DVA_GET_GANG(dva))
1283 error = zio_read_gang(spa, bp, pbuf);
1285 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1287 if (cpfunc != ZIO_COMPRESS_OFF)
1288 error = zio_decompress_data(cpfunc, pbuf,
1289 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1290 else if (size != BP_GET_PSIZE(bp))
1291 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1294 zfs_free(pbuf, size);
1299 printf("ZFS: i/o error - all block copies unavailable\n");
1304 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1306 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1307 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1308 int nlevels = dnode->dn_nlevels;
1311 if (bsize > SPA_MAXBLOCKSIZE) {
1312 printf("ZFS: I/O error - blocks larger than %llu are not "
1313 "supported\n", SPA_MAXBLOCKSIZE);
1318 * Note: bsize may not be a power of two here so we need to do an
1319 * actual divide rather than a bitshift.
1321 while (buflen > 0) {
1322 uint64_t bn = offset / bsize;
1323 int boff = offset % bsize;
1325 const blkptr_t *indbp;
1328 if (bn > dnode->dn_maxblkid)
1331 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1334 indbp = dnode->dn_blkptr;
1335 for (i = 0; i < nlevels; i++) {
1337 * Copy the bp from the indirect array so that
1338 * we can re-use the scratch buffer for multi-level
1341 ibn = bn >> ((nlevels - i - 1) * ibshift);
1342 ibn &= ((1 << ibshift) - 1);
1344 if (BP_IS_HOLE(&bp)) {
1345 memset(dnode_cache_buf, 0, bsize);
1348 rc = zio_read(spa, &bp, dnode_cache_buf);
1351 indbp = (const blkptr_t *) dnode_cache_buf;
1353 dnode_cache_obj = dnode;
1354 dnode_cache_bn = bn;
1358 * The buffer contains our data block. Copy what we
1359 * need from it and loop.
1362 if (i > buflen) i = buflen;
1363 memcpy(buf, &dnode_cache_buf[boff], i);
1364 buf = ((char*) buf) + i;
1373 * Lookup a value in a microzap directory. Assumes that the zap
1374 * scratch buffer contains the directory contents.
1377 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1379 const mzap_phys_t *mz;
1380 const mzap_ent_phys_t *mze;
1385 * Microzap objects use exactly one block. Read the whole
1388 size = dnode->dn_datablkszsec * 512;
1390 mz = (const mzap_phys_t *) zap_scratch;
1391 chunks = size / MZAP_ENT_LEN - 1;
1393 for (i = 0; i < chunks; i++) {
1394 mze = &mz->mz_chunk[i];
1395 if (!strcmp(mze->mze_name, name)) {
1396 *value = mze->mze_value;
1405 * Compare a name with a zap leaf entry. Return non-zero if the name
1409 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1412 const zap_leaf_chunk_t *nc;
1415 namelen = zc->l_entry.le_name_numints;
1417 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1419 while (namelen > 0) {
1422 if (len > ZAP_LEAF_ARRAY_BYTES)
1423 len = ZAP_LEAF_ARRAY_BYTES;
1424 if (memcmp(p, nc->l_array.la_array, len))
1428 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1435 * Extract a uint64_t value from a zap leaf entry.
1438 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1440 const zap_leaf_chunk_t *vc;
1445 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1446 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1447 value = (value << 8) | p[i];
1454 stv(int len, void *addr, uint64_t value)
1458 *(uint8_t *)addr = value;
1461 *(uint16_t *)addr = value;
1464 *(uint32_t *)addr = value;
1467 *(uint64_t *)addr = value;
1473 * Extract a array from a zap leaf entry.
1476 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1477 uint64_t integer_size, uint64_t num_integers, void *buf)
1479 uint64_t array_int_len = zc->l_entry.le_value_intlen;
1481 uint64_t *u64 = buf;
1483 int len = MIN(zc->l_entry.le_value_numints, num_integers);
1484 int chunk = zc->l_entry.le_value_chunk;
1487 if (integer_size == 8 && len == 1) {
1488 *u64 = fzap_leaf_value(zl, zc);
1493 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1496 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1497 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1498 value = (value << 8) | la->la_array[i];
1500 if (byten == array_int_len) {
1501 stv(integer_size, p, value);
1509 chunk = la->la_next;
1514 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1515 * buffer contains the directory header.
1518 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1519 uint64_t integer_size, uint64_t num_integers, void *value)
1521 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1522 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1528 if (zh.zap_magic != ZAP_MAGIC)
1531 z.zap_block_shift = ilog2(bsize);
1532 z.zap_phys = (zap_phys_t *) zap_scratch;
1535 * Figure out where the pointer table is and read it in if necessary.
1537 if (zh.zap_ptrtbl.zt_blk) {
1538 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1539 zap_scratch, bsize);
1542 ptrtbl = (uint64_t *) zap_scratch;
1544 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1547 hash = zap_hash(zh.zap_salt, name);
1550 zl.l_bs = z.zap_block_shift;
1552 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1553 zap_leaf_chunk_t *zc;
1555 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1559 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1562 * Make sure this chunk matches our hash.
1564 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1565 && zl.l_phys->l_hdr.lh_prefix
1566 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1570 * Hash within the chunk to find our entry.
1572 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1573 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1574 h = zl.l_phys->l_hash[h];
1577 zc = &ZAP_LEAF_CHUNK(&zl, h);
1578 while (zc->l_entry.le_hash != hash) {
1579 if (zc->l_entry.le_next == 0xffff) {
1583 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1585 if (fzap_name_equal(&zl, zc, name)) {
1586 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1587 integer_size * num_integers)
1589 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1597 * Lookup a name in a zap object and return its value as a uint64_t.
1600 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1601 uint64_t integer_size, uint64_t num_integers, void *value)
1605 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1607 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1611 zap_type = *(uint64_t *) zap_scratch;
1612 if (zap_type == ZBT_MICRO)
1613 return mzap_lookup(dnode, name, value);
1614 else if (zap_type == ZBT_HEADER) {
1615 return fzap_lookup(spa, dnode, name, integer_size,
1616 num_integers, value);
1618 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1623 * List a microzap directory. Assumes that the zap scratch buffer contains
1624 * the directory contents.
1627 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1629 const mzap_phys_t *mz;
1630 const mzap_ent_phys_t *mze;
1635 * Microzap objects use exactly one block. Read the whole
1638 size = dnode->dn_datablkszsec * 512;
1639 mz = (const mzap_phys_t *) zap_scratch;
1640 chunks = size / MZAP_ENT_LEN - 1;
1642 for (i = 0; i < chunks; i++) {
1643 mze = &mz->mz_chunk[i];
1644 if (mze->mze_name[0]) {
1645 rc = callback(mze->mze_name, mze->mze_value);
1655 * List a fatzap directory. Assumes that the zap scratch buffer contains
1656 * the directory header.
1659 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1661 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1662 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1666 if (zh.zap_magic != ZAP_MAGIC)
1669 z.zap_block_shift = ilog2(bsize);
1670 z.zap_phys = (zap_phys_t *) zap_scratch;
1673 * This assumes that the leaf blocks start at block 1. The
1674 * documentation isn't exactly clear on this.
1677 zl.l_bs = z.zap_block_shift;
1678 for (i = 0; i < zh.zap_num_leafs; i++) {
1679 off_t off = (i + 1) << zl.l_bs;
1683 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1686 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1688 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1689 zap_leaf_chunk_t *zc, *nc;
1692 zc = &ZAP_LEAF_CHUNK(&zl, j);
1693 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1695 namelen = zc->l_entry.le_name_numints;
1696 if (namelen > sizeof(name))
1697 namelen = sizeof(name);
1700 * Paste the name back together.
1702 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1704 while (namelen > 0) {
1707 if (len > ZAP_LEAF_ARRAY_BYTES)
1708 len = ZAP_LEAF_ARRAY_BYTES;
1709 memcpy(p, nc->l_array.la_array, len);
1712 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1716 * Assume the first eight bytes of the value are
1719 value = fzap_leaf_value(&zl, zc);
1721 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1722 rc = callback((const char *)name, value);
1731 static int zfs_printf(const char *name, uint64_t value __unused)
1734 printf("%s\n", name);
1740 * List a zap directory.
1743 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1746 size_t size = dnode->dn_datablkszsec * 512;
1748 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1751 zap_type = *(uint64_t *) zap_scratch;
1752 if (zap_type == ZBT_MICRO)
1753 return mzap_list(dnode, zfs_printf);
1755 return fzap_list(spa, dnode, zfs_printf);
1759 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1763 offset = objnum * sizeof(dnode_phys_t);
1764 return dnode_read(spa, &os->os_meta_dnode, offset,
1765 dnode, sizeof(dnode_phys_t));
1769 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1771 const mzap_phys_t *mz;
1772 const mzap_ent_phys_t *mze;
1777 * Microzap objects use exactly one block. Read the whole
1780 size = dnode->dn_datablkszsec * 512;
1782 mz = (const mzap_phys_t *) zap_scratch;
1783 chunks = size / MZAP_ENT_LEN - 1;
1785 for (i = 0; i < chunks; i++) {
1786 mze = &mz->mz_chunk[i];
1787 if (value == mze->mze_value) {
1788 strcpy(name, mze->mze_name);
1797 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1800 const zap_leaf_chunk_t *nc;
1803 namelen = zc->l_entry.le_name_numints;
1805 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1807 while (namelen > 0) {
1810 if (len > ZAP_LEAF_ARRAY_BYTES)
1811 len = ZAP_LEAF_ARRAY_BYTES;
1812 memcpy(p, nc->l_array.la_array, len);
1815 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1822 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1824 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1825 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1829 if (zh.zap_magic != ZAP_MAGIC)
1832 z.zap_block_shift = ilog2(bsize);
1833 z.zap_phys = (zap_phys_t *) zap_scratch;
1836 * This assumes that the leaf blocks start at block 1. The
1837 * documentation isn't exactly clear on this.
1840 zl.l_bs = z.zap_block_shift;
1841 for (i = 0; i < zh.zap_num_leafs; i++) {
1842 off_t off = (i + 1) << zl.l_bs;
1844 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1847 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1849 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1850 zap_leaf_chunk_t *zc;
1852 zc = &ZAP_LEAF_CHUNK(&zl, j);
1853 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1855 if (zc->l_entry.le_value_intlen != 8 ||
1856 zc->l_entry.le_value_numints != 1)
1859 if (fzap_leaf_value(&zl, zc) == value) {
1860 fzap_name_copy(&zl, zc, name);
1870 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1874 size_t size = dnode->dn_datablkszsec * 512;
1876 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1880 zap_type = *(uint64_t *) zap_scratch;
1881 if (zap_type == ZBT_MICRO)
1882 return mzap_rlookup(spa, dnode, name, value);
1884 return fzap_rlookup(spa, dnode, name, value);
1888 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1891 char component[256];
1892 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1893 dnode_phys_t child_dir_zap, dataset, dir, parent;
1895 dsl_dataset_phys_t *ds;
1899 p = &name[sizeof(name) - 1];
1902 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1903 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1906 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1907 dir_obj = ds->ds_dir_obj;
1910 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1912 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1914 /* Actual loop condition. */
1915 parent_obj = dd->dd_parent_obj;
1916 if (parent_obj == 0)
1919 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1921 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1922 child_dir_zapobj = dd->dd_child_dir_zapobj;
1923 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1925 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1928 len = strlen(component);
1930 memcpy(p, component, len);
1934 /* Actual loop iteration. */
1935 dir_obj = parent_obj;
1946 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1949 uint64_t dir_obj, child_dir_zapobj;
1950 dnode_phys_t child_dir_zap, dir;
1954 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1956 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1962 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1964 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1968 /* Actual loop condition #1. */
1974 memcpy(element, p, q - p);
1975 element[q - p] = '\0';
1982 child_dir_zapobj = dd->dd_child_dir_zapobj;
1983 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1986 /* Actual loop condition #2. */
1987 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1992 *objnum = dd->dd_head_dataset_obj;
1998 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
2000 uint64_t dir_obj, child_dir_zapobj;
2001 dnode_phys_t child_dir_zap, dir, dataset;
2002 dsl_dataset_phys_t *ds;
2005 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2006 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2009 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2010 dir_obj = ds->ds_dir_obj;
2012 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2013 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2016 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2018 child_dir_zapobj = dd->dd_child_dir_zapobj;
2019 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2020 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2024 return (zap_list(spa, &child_dir_zap) != 0);
2028 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2030 uint64_t dir_obj, child_dir_zapobj, zap_type;
2031 dnode_phys_t child_dir_zap, dir, dataset;
2032 dsl_dataset_phys_t *ds;
2036 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2038 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2041 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2042 dir_obj = ds->ds_dir_obj;
2044 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2046 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2049 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2051 child_dir_zapobj = dd->dd_child_dir_zapobj;
2052 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2054 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2058 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2062 zap_type = *(uint64_t *) zap_scratch;
2063 if (zap_type == ZBT_MICRO)
2064 return mzap_list(&child_dir_zap, callback);
2066 return fzap_list(spa, &child_dir_zap, callback);
2071 * Find the object set given the object number of its dataset object
2072 * and return its details in *objset
2075 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2077 dnode_phys_t dataset;
2078 dsl_dataset_phys_t *ds;
2081 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2082 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2086 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2087 if ((err = zio_read(spa, &ds->ds_bp, objset)) != 0) {
2088 printf("ZFS: can't read object set for dataset %ju (error %d)\n",
2089 (uintmax_t)objnum, err);
2097 * Find the object set pointed to by the BOOTFS property or the root
2098 * dataset if there is none and return its details in *objset
2101 zfs_get_root(const spa_t *spa, uint64_t *objid)
2103 dnode_phys_t dir, propdir;
2104 uint64_t props, bootfs, root;
2109 * Start with the MOS directory object.
2111 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2112 printf("ZFS: can't read MOS object directory\n");
2117 * Lookup the pool_props and see if we can find a bootfs.
2119 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2120 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2121 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2128 * Lookup the root dataset directory
2130 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2131 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2132 printf("ZFS: can't find root dsl_dir\n");
2137 * Use the information from the dataset directory's bonus buffer
2138 * to find the dataset object and from that the object set itself.
2140 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2141 *objid = dd->dd_head_dataset_obj;
2146 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2152 * Find the root object set if not explicitly provided
2154 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2155 printf("ZFS: can't find root filesystem\n");
2159 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2160 printf("ZFS: can't open root filesystem\n");
2164 mount->rootobj = rootobj;
2170 * callback function for feature name checks.
2173 check_feature(const char *name, uint64_t value)
2179 if (name[0] == '\0')
2182 for (i = 0; features_for_read[i] != NULL; i++) {
2183 if (strcmp(name, features_for_read[i]) == 0)
2186 printf("ZFS: unsupported feature: %s\n", name);
2191 * Checks whether the MOS features that are active are supported.
2194 check_mos_features(const spa_t *spa)
2197 uint64_t objnum, zap_type;
2201 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2204 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2205 sizeof (objnum), 1, &objnum)) != 0) {
2207 * It is older pool without features. As we have already
2208 * tested the label, just return without raising the error.
2213 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2216 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2219 size = dir.dn_datablkszsec * 512;
2220 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2223 zap_type = *(uint64_t *) zap_scratch;
2224 if (zap_type == ZBT_MICRO)
2225 rc = mzap_list(&dir, check_feature);
2227 rc = fzap_list(spa, &dir, check_feature);
2233 zfs_spa_init(spa_t *spa)
2238 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2239 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2242 if (spa->spa_mos.os_type != DMU_OST_META) {
2243 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2247 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2249 printf("ZFS: failed to read pool %s directory object\n",
2253 /* this is allowed to fail, older pools do not have salt */
2254 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2255 sizeof (spa->spa_cksum_salt.zcs_bytes),
2256 spa->spa_cksum_salt.zcs_bytes);
2258 rc = check_mos_features(spa);
2260 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2267 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2270 if (dn->dn_bonustype != DMU_OT_SA) {
2271 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2273 sb->st_mode = zp->zp_mode;
2274 sb->st_uid = zp->zp_uid;
2275 sb->st_gid = zp->zp_gid;
2276 sb->st_size = zp->zp_size;
2278 sa_hdr_phys_t *sahdrp;
2283 if (dn->dn_bonuslen != 0)
2284 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2286 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2287 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
2290 size = BP_GET_LSIZE(bp);
2291 buf = zfs_alloc(size);
2292 error = zio_read(spa, bp, buf);
2294 zfs_free(buf, size);
2302 hdrsize = SA_HDR_SIZE(sahdrp);
2303 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2305 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2307 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2309 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2312 zfs_free(buf, size);
2319 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2323 if (dn->dn_bonustype == DMU_OT_SA) {
2324 sa_hdr_phys_t *sahdrp = NULL;
2330 if (dn->dn_bonuslen != 0)
2331 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2335 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2337 bp = DN_SPILL_BLKPTR(dn);
2339 size = BP_GET_LSIZE(bp);
2340 buf = zfs_alloc(size);
2341 rc = zio_read(spa, bp, buf);
2343 zfs_free(buf, size);
2348 hdrsize = SA_HDR_SIZE(sahdrp);
2349 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2350 memcpy(path, p, psize);
2352 zfs_free(buf, size);
2356 * Second test is purely to silence bogus compiler
2357 * warning about accessing past the end of dn_bonus.
2359 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2360 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2361 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2363 rc = dnode_read(spa, dn, 0, path, psize);
2370 STAILQ_ENTRY(obj_list) entry;
2374 * Lookup a file and return its dnode.
2377 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2386 int symlinks_followed = 0;
2388 struct obj_list *entry, *tentry;
2389 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2392 if (mount->objset.os_type != DMU_OST_ZFS) {
2393 printf("ZFS: unexpected object set type %ju\n",
2394 (uintmax_t)mount->objset.os_type);
2398 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2402 * Get the root directory dnode.
2404 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2410 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
2415 entry->objnum = objnum;
2416 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2418 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2424 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2433 while (*q != '\0' && *q != '/')
2437 if (p + 1 == q && p[0] == '.') {
2442 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2444 if (STAILQ_FIRST(&on_cache) ==
2445 STAILQ_LAST(&on_cache, obj_list, entry)) {
2449 entry = STAILQ_FIRST(&on_cache);
2450 STAILQ_REMOVE_HEAD(&on_cache, entry);
2452 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2455 if (q - p + 1 > sizeof(element)) {
2459 memcpy(element, p, q - p);
2463 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2465 if (!S_ISDIR(sb.st_mode)) {
2470 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2473 objnum = ZFS_DIRENT_OBJ(objnum);
2475 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2479 entry->objnum = objnum;
2480 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2481 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2486 * Check for symlink.
2488 rc = zfs_dnode_stat(spa, &dn, &sb);
2491 if (S_ISLNK(sb.st_mode)) {
2492 if (symlinks_followed > 10) {
2496 symlinks_followed++;
2499 * Read the link value and copy the tail of our
2500 * current path onto the end.
2502 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2506 strcpy(&path[sb.st_size], p);
2508 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2513 * Restart with the new path, starting either at
2514 * the root or at the parent depending whether or
2515 * not the link is relative.
2519 while (STAILQ_FIRST(&on_cache) !=
2520 STAILQ_LAST(&on_cache, obj_list, entry)) {
2521 entry = STAILQ_FIRST(&on_cache);
2522 STAILQ_REMOVE_HEAD(&on_cache, entry);
2526 entry = STAILQ_FIRST(&on_cache);
2527 STAILQ_REMOVE_HEAD(&on_cache, entry);
2530 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2536 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)