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.
34 #include <sys/endian.h>
36 #include <sys/stdint.h>
47 static struct zfsmount zfsmount __unused;
50 * List of all vdevs, chained through v_alllink.
52 static vdev_list_t zfs_vdevs;
55 * List of ZFS features supported for read
57 static const char *features_for_read[] = {
58 "org.illumos:lz4_compress",
59 "com.delphix:hole_birth",
60 "com.delphix:extensible_dataset",
61 "com.delphix:embedded_data",
62 "org.open-zfs:large_blocks",
65 "org.zfsonlinux:large_dnode",
66 "com.joyent:multi_vdev_crash_dump",
71 * List of all pools, chained through spa_link.
73 static spa_list_t zfs_pools;
75 static const dnode_phys_t *dnode_cache_obj;
76 static uint64_t dnode_cache_bn;
77 static char *dnode_cache_buf;
78 static char *zap_scratch;
79 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
81 #define TEMP_SIZE (1024 * 1024)
83 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
84 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
85 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
86 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
87 const char *name, uint64_t integer_size, uint64_t num_integers,
93 STAILQ_INIT(&zfs_vdevs);
94 STAILQ_INIT(&zfs_pools);
96 zfs_temp_buf = malloc(TEMP_SIZE);
97 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
98 zfs_temp_ptr = zfs_temp_buf;
99 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
100 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
106 zfs_alloc(size_t size)
110 if (zfs_temp_ptr + size > zfs_temp_end) {
111 panic("ZFS: out of temporary buffer space");
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 panic("ZFS: zfs_alloc()/zfs_free() mismatch");
130 xdr_int(const unsigned char **xdr, int *ip)
138 xdr_u_int(const unsigned char **xdr, u_int *ip)
146 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
152 *lp = (((uint64_t) hi) << 32) | lo;
157 nvlist_find(const unsigned char *nvlist, const char *name, int type,
158 int* elementsp, void *valuep)
160 const unsigned char *p, *pair;
162 int encoded_size, decoded_size;
169 xdr_int(&p, &encoded_size);
170 xdr_int(&p, &decoded_size);
171 while (encoded_size && decoded_size) {
172 int namelen, pairtype, elements;
173 const char *pairname;
175 xdr_int(&p, &namelen);
176 pairname = (const char*) p;
177 p += roundup(namelen, 4);
178 xdr_int(&p, &pairtype);
180 if (!memcmp(name, pairname, namelen) && type == pairtype) {
181 xdr_int(&p, &elements);
183 *elementsp = elements;
184 if (type == DATA_TYPE_UINT64) {
185 xdr_uint64_t(&p, (uint64_t *) valuep);
187 } else if (type == DATA_TYPE_STRING) {
190 (*(const char**) valuep) = (const char*) p;
192 } else if (type == DATA_TYPE_NVLIST
193 || type == DATA_TYPE_NVLIST_ARRAY) {
194 (*(const unsigned char**) valuep) =
195 (const unsigned char*) p;
202 * Not the pair we are looking for, skip to the next one.
204 p = pair + encoded_size;
208 xdr_int(&p, &encoded_size);
209 xdr_int(&p, &decoded_size);
216 nvlist_check_features_for_read(const unsigned char *nvlist)
218 const unsigned char *p, *pair;
220 int encoded_size, decoded_size;
230 xdr_int(&p, &encoded_size);
231 xdr_int(&p, &decoded_size);
232 while (encoded_size && decoded_size) {
233 int namelen, pairtype;
234 const char *pairname;
239 xdr_int(&p, &namelen);
240 pairname = (const char*) p;
241 p += roundup(namelen, 4);
242 xdr_int(&p, &pairtype);
244 for (i = 0; features_for_read[i] != NULL; i++) {
245 if (!memcmp(pairname, features_for_read[i], namelen)) {
252 printf("ZFS: unsupported feature: %s\n", pairname);
256 p = pair + encoded_size;
259 xdr_int(&p, &encoded_size);
260 xdr_int(&p, &decoded_size);
267 * Return the next nvlist in an nvlist array.
269 static const unsigned char *
270 nvlist_next(const unsigned char *nvlist)
272 const unsigned char *p, *pair;
274 int encoded_size, decoded_size;
281 xdr_int(&p, &encoded_size);
282 xdr_int(&p, &decoded_size);
283 while (encoded_size && decoded_size) {
284 p = pair + encoded_size;
287 xdr_int(&p, &encoded_size);
288 xdr_int(&p, &decoded_size);
296 static const unsigned char *
297 nvlist_print(const unsigned char *nvlist, unsigned int indent)
299 static const char* typenames[] = {
310 "DATA_TYPE_BYTE_ARRAY",
311 "DATA_TYPE_INT16_ARRAY",
312 "DATA_TYPE_UINT16_ARRAY",
313 "DATA_TYPE_INT32_ARRAY",
314 "DATA_TYPE_UINT32_ARRAY",
315 "DATA_TYPE_INT64_ARRAY",
316 "DATA_TYPE_UINT64_ARRAY",
317 "DATA_TYPE_STRING_ARRAY",
320 "DATA_TYPE_NVLIST_ARRAY",
321 "DATA_TYPE_BOOLEAN_VALUE",
324 "DATA_TYPE_BOOLEAN_ARRAY",
325 "DATA_TYPE_INT8_ARRAY",
326 "DATA_TYPE_UINT8_ARRAY"
330 const unsigned char *p, *pair;
332 int encoded_size, decoded_size;
339 xdr_int(&p, &encoded_size);
340 xdr_int(&p, &decoded_size);
341 while (encoded_size && decoded_size) {
342 int namelen, pairtype, elements;
343 const char *pairname;
345 xdr_int(&p, &namelen);
346 pairname = (const char*) p;
347 p += roundup(namelen, 4);
348 xdr_int(&p, &pairtype);
350 for (i = 0; i < indent; i++)
352 printf("%s %s", typenames[pairtype], pairname);
354 xdr_int(&p, &elements);
356 case DATA_TYPE_UINT64: {
358 xdr_uint64_t(&p, &val);
359 printf(" = 0x%jx\n", (uintmax_t)val);
363 case DATA_TYPE_STRING: {
366 printf(" = \"%s\"\n", p);
370 case DATA_TYPE_NVLIST:
372 nvlist_print(p, indent + 1);
375 case DATA_TYPE_NVLIST_ARRAY:
376 for (j = 0; j < elements; j++) {
378 p = nvlist_print(p, indent + 1);
379 if (j != elements - 1) {
380 for (i = 0; i < indent; i++)
382 printf("%s %s", typenames[pairtype], pairname);
391 p = pair + encoded_size;
394 xdr_int(&p, &encoded_size);
395 xdr_int(&p, &decoded_size);
404 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
405 off_t offset, size_t size)
410 if (!vdev->v_phys_read)
414 psize = BP_GET_PSIZE(bp);
419 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
420 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
423 if (bp && zio_checksum_verify(vdev->spa, bp, buf))
430 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
431 off_t offset, size_t bytes)
434 return (vdev_read_phys(vdev, bp, buf,
435 offset + VDEV_LABEL_START_SIZE, bytes));
440 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
441 off_t offset, size_t bytes)
447 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
448 if (kid->v_state != VDEV_STATE_HEALTHY)
450 rc = kid->v_read(kid, bp, buf, offset, bytes);
459 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
460 off_t offset, size_t bytes)
465 * Here we should have two kids:
466 * First one which is the one we are replacing and we can trust
467 * only this one to have valid data, but it might not be present.
468 * Second one is that one we are replacing with. It is most likely
469 * healthy, but we can't trust it has needed data, so we won't use it.
471 kid = STAILQ_FIRST(&vdev->v_children);
474 if (kid->v_state != VDEV_STATE_HEALTHY)
476 return (kid->v_read(kid, bp, buf, offset, bytes));
480 vdev_find(uint64_t guid)
484 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
485 if (vdev->v_guid == guid)
492 vdev_create(uint64_t guid, vdev_read_t *_read)
496 vdev = malloc(sizeof(vdev_t));
497 memset(vdev, 0, sizeof(vdev_t));
498 STAILQ_INIT(&vdev->v_children);
500 vdev->v_state = VDEV_STATE_OFFLINE;
501 vdev->v_read = _read;
502 vdev->v_phys_read = 0;
503 vdev->v_read_priv = 0;
504 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
510 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
511 vdev_t **vdevp, int is_newer)
514 uint64_t guid, id, ashift, nparity;
518 const unsigned char *kids;
519 int nkids, i, is_new;
520 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
522 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
524 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
525 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
527 printf("ZFS: can't find vdev details\n");
531 if (strcmp(type, VDEV_TYPE_MIRROR)
532 && strcmp(type, VDEV_TYPE_DISK)
534 && strcmp(type, VDEV_TYPE_FILE)
536 && strcmp(type, VDEV_TYPE_RAIDZ)
537 && strcmp(type, VDEV_TYPE_REPLACING)) {
538 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
542 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
544 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
546 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
548 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
550 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
552 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
555 vdev = vdev_find(guid);
559 if (!strcmp(type, VDEV_TYPE_MIRROR))
560 vdev = vdev_create(guid, vdev_mirror_read);
561 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
562 vdev = vdev_create(guid, vdev_raidz_read);
563 else if (!strcmp(type, VDEV_TYPE_REPLACING))
564 vdev = vdev_create(guid, vdev_replacing_read);
566 vdev = vdev_create(guid, vdev_disk_read);
569 vdev->v_top = pvdev != NULL ? pvdev : vdev;
570 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
571 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
572 vdev->v_ashift = ashift;
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
577 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
578 vdev->v_nparity = nparity;
582 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
583 DATA_TYPE_STRING, NULL, &path) == 0) {
584 if (strncmp(path, "/dev/", 5) == 0)
586 vdev->v_name = strdup(path);
588 if (!strcmp(type, "raidz")) {
589 if (vdev->v_nparity == 1)
590 vdev->v_name = "raidz1";
591 else if (vdev->v_nparity == 2)
592 vdev->v_name = "raidz2";
593 else if (vdev->v_nparity == 3)
594 vdev->v_name = "raidz3";
596 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
600 vdev->v_name = strdup(type);
607 if (is_new || is_newer) {
609 * This is either new vdev or we've already seen this vdev,
610 * but from an older vdev label, so let's refresh its state
611 * from the newer label.
614 vdev->v_state = VDEV_STATE_OFFLINE;
616 vdev->v_state = VDEV_STATE_REMOVED;
618 vdev->v_state = VDEV_STATE_FAULTED;
619 else if (is_degraded)
620 vdev->v_state = VDEV_STATE_DEGRADED;
621 else if (isnt_present)
622 vdev->v_state = VDEV_STATE_CANT_OPEN;
625 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
628 * Its ok if we don't have any kids.
631 vdev->v_nchildren = nkids;
632 for (i = 0; i < nkids; i++) {
633 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
637 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
639 kids = nvlist_next(kids);
642 vdev->v_nchildren = 0;
651 vdev_set_state(vdev_t *vdev)
658 * A mirror or raidz is healthy if all its kids are healthy. A
659 * mirror is degraded if any of its kids is healthy; a raidz
660 * is degraded if at most nparity kids are offline.
662 if (STAILQ_FIRST(&vdev->v_children)) {
665 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
666 if (kid->v_state == VDEV_STATE_HEALTHY)
672 vdev->v_state = VDEV_STATE_HEALTHY;
674 if (vdev->v_read == vdev_mirror_read) {
676 vdev->v_state = VDEV_STATE_DEGRADED;
678 vdev->v_state = VDEV_STATE_OFFLINE;
680 } else if (vdev->v_read == vdev_raidz_read) {
681 if (bad_kids > vdev->v_nparity) {
682 vdev->v_state = VDEV_STATE_OFFLINE;
684 vdev->v_state = VDEV_STATE_DEGRADED;
692 spa_find_by_guid(uint64_t guid)
696 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
697 if (spa->spa_guid == guid)
704 spa_find_by_name(const char *name)
708 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
709 if (!strcmp(spa->spa_name, name))
717 spa_get_primary(void)
720 return (STAILQ_FIRST(&zfs_pools));
724 spa_get_primary_vdev(const spa_t *spa)
730 spa = spa_get_primary();
733 vdev = STAILQ_FIRST(&spa->spa_vdevs);
736 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
737 kid = STAILQ_FIRST(&vdev->v_children))
744 spa_create(uint64_t guid, const char *name)
748 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
750 if ((spa->spa_name = strdup(name)) == NULL) {
754 STAILQ_INIT(&spa->spa_vdevs);
755 spa->spa_guid = guid;
756 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
762 state_name(vdev_state_t state)
764 static const char* names[] = {
779 #define pager_printf printf
784 pager_printf(const char *fmt, ...)
790 vsprintf(line, fmt, args);
793 return (pager_output(line));
798 #define STATUS_FORMAT " %s %s\n"
801 print_state(int indent, const char *name, vdev_state_t state)
807 for (i = 0; i < indent; i++)
811 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
815 vdev_status(vdev_t *vdev, int indent)
819 ret = print_state(indent, vdev->v_name, vdev->v_state);
823 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
824 ret = vdev_status(kid, indent + 1);
832 spa_status(spa_t *spa)
834 static char bootfs[ZFS_MAXNAMELEN];
837 int good_kids, bad_kids, degraded_kids, ret;
840 ret = pager_printf(" pool: %s\n", spa->spa_name);
844 if (zfs_get_root(spa, &rootid) == 0 &&
845 zfs_rlookup(spa, rootid, bootfs) == 0) {
846 if (bootfs[0] == '\0')
847 ret = pager_printf("bootfs: %s\n", spa->spa_name);
849 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
854 ret = pager_printf("config:\n\n");
857 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
864 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
865 if (vdev->v_state == VDEV_STATE_HEALTHY)
867 else if (vdev->v_state == VDEV_STATE_DEGRADED)
873 state = VDEV_STATE_CLOSED;
874 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
875 state = VDEV_STATE_HEALTHY;
876 else if ((good_kids + degraded_kids) > 0)
877 state = VDEV_STATE_DEGRADED;
879 ret = print_state(0, spa->spa_name, state);
882 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
883 ret = vdev_status(vdev, 1);
894 int first = 1, ret = 0;
896 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
898 ret = pager_printf("\n");
903 ret = spa_status(spa);
911 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
913 uint64_t label_offset;
915 if (l < VDEV_LABELS / 2)
918 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
920 return (offset + l * sizeof (vdev_label_t) + label_offset);
924 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
927 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
928 vdev_phys_t *tmp_label;
930 vdev_t *vdev, *top_vdev, *pool_vdev;
933 const unsigned char *nvlist = NULL;
936 uint64_t best_txg = 0;
937 uint64_t pool_txg, pool_guid;
939 const char *pool_name;
940 const unsigned char *vdevs;
941 const unsigned char *features;
942 int i, l, rc, is_newer;
944 const struct uberblock *up;
947 * Load the vdev label and figure out which
948 * uberblock is most current.
950 memset(&vtmp, 0, sizeof(vtmp));
951 vtmp.v_phys_read = _read;
952 vtmp.v_read_priv = read_priv;
953 psize = P2ALIGN(ldi_get_size(read_priv),
954 (uint64_t)sizeof (vdev_label_t));
956 /* Test for minimum pool size. */
957 if (psize < SPA_MINDEVSIZE)
960 tmp_label = zfs_alloc(sizeof(vdev_phys_t));
962 for (l = 0; l < VDEV_LABELS; l++) {
963 off = vdev_label_offset(psize, l,
964 offsetof(vdev_label_t, vl_vdev_phys));
967 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
968 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
969 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
970 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
971 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
972 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
974 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
977 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
980 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
981 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
982 DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
985 if (best_txg <= pool_txg) {
987 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
991 zfs_free(tmp_label, sizeof (vdev_phys_t));
996 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
999 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1001 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1006 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1007 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1008 (unsigned) val, (unsigned) SPA_VERSION);
1012 /* Check ZFS features for read */
1013 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1014 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1015 nvlist_check_features_for_read(features) != 0) {
1019 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1024 if (val == POOL_STATE_DESTROYED) {
1025 /* We don't boot only from destroyed pools. */
1029 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1030 NULL, &pool_txg) != 0 ||
1031 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1032 NULL, &pool_guid) != 0 ||
1033 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1034 NULL, &pool_name) != 0) {
1036 * Cache and spare devices end up here - just ignore
1039 /*printf("ZFS: can't find pool details\n");*/
1043 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1044 NULL, &val) == 0 && val != 0) {
1049 * Create the pool if this is the first time we've seen it.
1051 spa = spa_find_by_guid(pool_guid);
1053 spa = spa_create(pool_guid, pool_name);
1057 if (pool_txg > spa->spa_txg) {
1058 spa->spa_txg = pool_txg;
1065 * Get the vdev tree and create our in-core copy of it.
1066 * If we already have a vdev with this guid, this must
1067 * be some kind of alias (overlapping slices, dangerously dedicated
1070 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1071 NULL, &guid) != 0) {
1074 vdev = vdev_find(guid);
1075 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1078 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1083 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1088 * Add the toplevel vdev to the pool if its not already there.
1090 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1091 if (top_vdev == pool_vdev)
1093 if (!pool_vdev && top_vdev) {
1094 top_vdev->spa = spa;
1095 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1099 * We should already have created an incomplete vdev for this
1100 * vdev. Find it and initialise it with our read proc.
1102 vdev = vdev_find(guid);
1104 vdev->v_phys_read = _read;
1105 vdev->v_read_priv = read_priv;
1106 vdev->v_state = VDEV_STATE_HEALTHY;
1108 printf("ZFS: inconsistent nvlist contents\n");
1113 * Re-evaluate top-level vdev state.
1115 vdev_set_state(top_vdev);
1118 * Ok, we are happy with the pool so far. Lets find
1119 * the best uberblock and then we can actually access
1120 * the contents of the pool.
1122 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1123 up = (const struct uberblock *)upbuf;
1124 for (l = 0; l < VDEV_LABELS; l++) {
1125 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1126 off = vdev_label_offset(psize, l,
1127 VDEV_UBERBLOCK_OFFSET(vdev, i));
1129 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1130 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1131 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1132 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1133 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1134 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1136 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1139 if (up->ub_magic != UBERBLOCK_MAGIC)
1141 if (up->ub_txg < spa->spa_txg)
1143 if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1144 (up->ub_txg == spa->spa_uberblock.ub_txg &&
1146 spa->spa_uberblock.ub_timestamp)) {
1147 spa->spa_uberblock = *up;
1151 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1164 for (v = 0; v < 32; v++)
1171 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1174 zio_gbh_phys_t zio_gb;
1178 /* Artificial BP for gang block header. */
1180 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1181 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1182 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1183 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1184 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1185 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1187 /* Read gang header block using the artificial BP. */
1188 if (zio_read(spa, &gbh_bp, &zio_gb))
1192 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1193 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1195 if (BP_IS_HOLE(gbp))
1197 if (zio_read(spa, gbp, pbuf))
1199 pbuf += BP_GET_PSIZE(gbp);
1202 if (zio_checksum_verify(spa, bp, buf))
1208 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1210 int cpfunc = BP_GET_COMPRESS(bp);
1211 uint64_t align, size;
1216 * Process data embedded in block pointer
1218 if (BP_IS_EMBEDDED(bp)) {
1219 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1221 size = BPE_GET_PSIZE(bp);
1222 ASSERT(size <= BPE_PAYLOAD_SIZE);
1224 if (cpfunc != ZIO_COMPRESS_OFF)
1225 pbuf = zfs_alloc(size);
1229 decode_embedded_bp_compressed(bp, pbuf);
1232 if (cpfunc != ZIO_COMPRESS_OFF) {
1233 error = zio_decompress_data(cpfunc, pbuf,
1234 size, buf, BP_GET_LSIZE(bp));
1235 zfs_free(pbuf, size);
1238 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1245 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1246 const dva_t *dva = &bp->blk_dva[i];
1251 if (!dva->dva_word[0] && !dva->dva_word[1])
1254 vdevid = DVA_GET_VDEV(dva);
1255 offset = DVA_GET_OFFSET(dva);
1256 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1257 if (vdev->v_id == vdevid)
1260 if (!vdev || !vdev->v_read)
1263 size = BP_GET_PSIZE(bp);
1264 if (vdev->v_read == vdev_raidz_read) {
1265 align = 1ULL << vdev->v_top->v_ashift;
1266 if (P2PHASE(size, align) != 0)
1267 size = P2ROUNDUP(size, align);
1269 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1270 pbuf = zfs_alloc(size);
1274 if (DVA_GET_GANG(dva))
1275 error = zio_read_gang(spa, bp, pbuf);
1277 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1279 if (cpfunc != ZIO_COMPRESS_OFF)
1280 error = zio_decompress_data(cpfunc, pbuf,
1281 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1282 else if (size != BP_GET_PSIZE(bp))
1283 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1286 zfs_free(pbuf, size);
1291 printf("ZFS: i/o error - all block copies unavailable\n");
1296 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1298 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1299 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1300 int nlevels = dnode->dn_nlevels;
1303 if (bsize > SPA_MAXBLOCKSIZE) {
1304 printf("ZFS: I/O error - blocks larger than %llu are not "
1305 "supported\n", SPA_MAXBLOCKSIZE);
1310 * Note: bsize may not be a power of two here so we need to do an
1311 * actual divide rather than a bitshift.
1313 while (buflen > 0) {
1314 uint64_t bn = offset / bsize;
1315 int boff = offset % bsize;
1317 const blkptr_t *indbp;
1320 if (bn > dnode->dn_maxblkid)
1323 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1326 indbp = dnode->dn_blkptr;
1327 for (i = 0; i < nlevels; i++) {
1329 * Copy the bp from the indirect array so that
1330 * we can re-use the scratch buffer for multi-level
1333 ibn = bn >> ((nlevels - i - 1) * ibshift);
1334 ibn &= ((1 << ibshift) - 1);
1336 if (BP_IS_HOLE(&bp)) {
1337 memset(dnode_cache_buf, 0, bsize);
1340 rc = zio_read(spa, &bp, dnode_cache_buf);
1343 indbp = (const blkptr_t *) dnode_cache_buf;
1345 dnode_cache_obj = dnode;
1346 dnode_cache_bn = bn;
1350 * The buffer contains our data block. Copy what we
1351 * need from it and loop.
1354 if (i > buflen) i = buflen;
1355 memcpy(buf, &dnode_cache_buf[boff], i);
1356 buf = ((char*) buf) + i;
1365 * Lookup a value in a microzap directory. Assumes that the zap
1366 * scratch buffer contains the directory contents.
1369 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1371 const mzap_phys_t *mz;
1372 const mzap_ent_phys_t *mze;
1377 * Microzap objects use exactly one block. Read the whole
1380 size = dnode->dn_datablkszsec * 512;
1382 mz = (const mzap_phys_t *) zap_scratch;
1383 chunks = size / MZAP_ENT_LEN - 1;
1385 for (i = 0; i < chunks; i++) {
1386 mze = &mz->mz_chunk[i];
1387 if (!strcmp(mze->mze_name, name)) {
1388 *value = mze->mze_value;
1397 * Compare a name with a zap leaf entry. Return non-zero if the name
1401 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1404 const zap_leaf_chunk_t *nc;
1407 namelen = zc->l_entry.le_name_numints;
1409 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1411 while (namelen > 0) {
1414 if (len > ZAP_LEAF_ARRAY_BYTES)
1415 len = ZAP_LEAF_ARRAY_BYTES;
1416 if (memcmp(p, nc->l_array.la_array, len))
1420 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1427 * Extract a uint64_t value from a zap leaf entry.
1430 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1432 const zap_leaf_chunk_t *vc;
1437 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1438 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1439 value = (value << 8) | p[i];
1446 stv(int len, void *addr, uint64_t value)
1450 *(uint8_t *)addr = value;
1453 *(uint16_t *)addr = value;
1456 *(uint32_t *)addr = value;
1459 *(uint64_t *)addr = value;
1465 * Extract a array from a zap leaf entry.
1468 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1469 uint64_t integer_size, uint64_t num_integers, void *buf)
1471 uint64_t array_int_len = zc->l_entry.le_value_intlen;
1473 uint64_t *u64 = buf;
1475 int len = MIN(zc->l_entry.le_value_numints, num_integers);
1476 int chunk = zc->l_entry.le_value_chunk;
1479 if (integer_size == 8 && len == 1) {
1480 *u64 = fzap_leaf_value(zl, zc);
1485 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1488 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1489 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1490 value = (value << 8) | la->la_array[i];
1492 if (byten == array_int_len) {
1493 stv(integer_size, p, value);
1501 chunk = la->la_next;
1506 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1507 * buffer contains the directory header.
1510 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1511 uint64_t integer_size, uint64_t num_integers, void *value)
1513 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1514 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1520 if (zh.zap_magic != ZAP_MAGIC)
1523 z.zap_block_shift = ilog2(bsize);
1524 z.zap_phys = (zap_phys_t *) zap_scratch;
1527 * Figure out where the pointer table is and read it in if necessary.
1529 if (zh.zap_ptrtbl.zt_blk) {
1530 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1531 zap_scratch, bsize);
1534 ptrtbl = (uint64_t *) zap_scratch;
1536 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1539 hash = zap_hash(zh.zap_salt, name);
1542 zl.l_bs = z.zap_block_shift;
1544 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1545 zap_leaf_chunk_t *zc;
1547 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1551 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1554 * Make sure this chunk matches our hash.
1556 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1557 && zl.l_phys->l_hdr.lh_prefix
1558 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1562 * Hash within the chunk to find our entry.
1564 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1565 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1566 h = zl.l_phys->l_hash[h];
1569 zc = &ZAP_LEAF_CHUNK(&zl, h);
1570 while (zc->l_entry.le_hash != hash) {
1571 if (zc->l_entry.le_next == 0xffff) {
1575 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1577 if (fzap_name_equal(&zl, zc, name)) {
1578 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1579 integer_size * num_integers)
1581 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1589 * Lookup a name in a zap object and return its value as a uint64_t.
1592 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1593 uint64_t integer_size, uint64_t num_integers, void *value)
1597 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1599 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1603 zap_type = *(uint64_t *) zap_scratch;
1604 if (zap_type == ZBT_MICRO)
1605 return mzap_lookup(dnode, name, value);
1606 else if (zap_type == ZBT_HEADER) {
1607 return fzap_lookup(spa, dnode, name, integer_size,
1608 num_integers, value);
1610 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1615 * List a microzap directory. Assumes that the zap scratch buffer contains
1616 * the directory contents.
1619 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1621 const mzap_phys_t *mz;
1622 const mzap_ent_phys_t *mze;
1627 * Microzap objects use exactly one block. Read the whole
1630 size = dnode->dn_datablkszsec * 512;
1631 mz = (const mzap_phys_t *) zap_scratch;
1632 chunks = size / MZAP_ENT_LEN - 1;
1634 for (i = 0; i < chunks; i++) {
1635 mze = &mz->mz_chunk[i];
1636 if (mze->mze_name[0]) {
1637 rc = callback(mze->mze_name, mze->mze_value);
1647 * List a fatzap directory. Assumes that the zap scratch buffer contains
1648 * the directory header.
1651 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1653 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1654 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1658 if (zh.zap_magic != ZAP_MAGIC)
1661 z.zap_block_shift = ilog2(bsize);
1662 z.zap_phys = (zap_phys_t *) zap_scratch;
1665 * This assumes that the leaf blocks start at block 1. The
1666 * documentation isn't exactly clear on this.
1669 zl.l_bs = z.zap_block_shift;
1670 for (i = 0; i < zh.zap_num_leafs; i++) {
1671 off_t off = (i + 1) << zl.l_bs;
1675 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1678 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1680 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1681 zap_leaf_chunk_t *zc, *nc;
1684 zc = &ZAP_LEAF_CHUNK(&zl, j);
1685 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1687 namelen = zc->l_entry.le_name_numints;
1688 if (namelen > sizeof(name))
1689 namelen = sizeof(name);
1692 * Paste the name back together.
1694 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1696 while (namelen > 0) {
1699 if (len > ZAP_LEAF_ARRAY_BYTES)
1700 len = ZAP_LEAF_ARRAY_BYTES;
1701 memcpy(p, nc->l_array.la_array, len);
1704 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1708 * Assume the first eight bytes of the value are
1711 value = fzap_leaf_value(&zl, zc);
1713 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1714 rc = callback((const char *)name, value);
1723 static int zfs_printf(const char *name, uint64_t value __unused)
1726 printf("%s\n", name);
1732 * List a zap directory.
1735 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1738 size_t size = dnode->dn_datablkszsec * 512;
1740 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1743 zap_type = *(uint64_t *) zap_scratch;
1744 if (zap_type == ZBT_MICRO)
1745 return mzap_list(dnode, zfs_printf);
1747 return fzap_list(spa, dnode, zfs_printf);
1751 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1755 offset = objnum * sizeof(dnode_phys_t);
1756 return dnode_read(spa, &os->os_meta_dnode, offset,
1757 dnode, sizeof(dnode_phys_t));
1761 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1763 const mzap_phys_t *mz;
1764 const mzap_ent_phys_t *mze;
1769 * Microzap objects use exactly one block. Read the whole
1772 size = dnode->dn_datablkszsec * 512;
1774 mz = (const mzap_phys_t *) zap_scratch;
1775 chunks = size / MZAP_ENT_LEN - 1;
1777 for (i = 0; i < chunks; i++) {
1778 mze = &mz->mz_chunk[i];
1779 if (value == mze->mze_value) {
1780 strcpy(name, mze->mze_name);
1789 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1792 const zap_leaf_chunk_t *nc;
1795 namelen = zc->l_entry.le_name_numints;
1797 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1799 while (namelen > 0) {
1802 if (len > ZAP_LEAF_ARRAY_BYTES)
1803 len = ZAP_LEAF_ARRAY_BYTES;
1804 memcpy(p, nc->l_array.la_array, len);
1807 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1814 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1816 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1817 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1821 if (zh.zap_magic != ZAP_MAGIC)
1824 z.zap_block_shift = ilog2(bsize);
1825 z.zap_phys = (zap_phys_t *) zap_scratch;
1828 * This assumes that the leaf blocks start at block 1. The
1829 * documentation isn't exactly clear on this.
1832 zl.l_bs = z.zap_block_shift;
1833 for (i = 0; i < zh.zap_num_leafs; i++) {
1834 off_t off = (i + 1) << zl.l_bs;
1836 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1839 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1841 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1842 zap_leaf_chunk_t *zc;
1844 zc = &ZAP_LEAF_CHUNK(&zl, j);
1845 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1847 if (zc->l_entry.le_value_intlen != 8 ||
1848 zc->l_entry.le_value_numints != 1)
1851 if (fzap_leaf_value(&zl, zc) == value) {
1852 fzap_name_copy(&zl, zc, name);
1862 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1866 size_t size = dnode->dn_datablkszsec * 512;
1868 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1872 zap_type = *(uint64_t *) zap_scratch;
1873 if (zap_type == ZBT_MICRO)
1874 return mzap_rlookup(spa, dnode, name, value);
1876 return fzap_rlookup(spa, dnode, name, value);
1880 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1883 char component[256];
1884 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1885 dnode_phys_t child_dir_zap, dataset, dir, parent;
1887 dsl_dataset_phys_t *ds;
1891 p = &name[sizeof(name) - 1];
1894 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1895 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1898 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1899 dir_obj = ds->ds_dir_obj;
1902 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1904 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1906 /* Actual loop condition. */
1907 parent_obj = dd->dd_parent_obj;
1908 if (parent_obj == 0)
1911 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1913 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1914 child_dir_zapobj = dd->dd_child_dir_zapobj;
1915 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1917 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1920 len = strlen(component);
1922 memcpy(p, component, len);
1926 /* Actual loop iteration. */
1927 dir_obj = parent_obj;
1938 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1941 uint64_t dir_obj, child_dir_zapobj;
1942 dnode_phys_t child_dir_zap, dir;
1946 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1948 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1954 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1956 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1960 /* Actual loop condition #1. */
1966 memcpy(element, p, q - p);
1967 element[q - p] = '\0';
1974 child_dir_zapobj = dd->dd_child_dir_zapobj;
1975 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1978 /* Actual loop condition #2. */
1979 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1984 *objnum = dd->dd_head_dataset_obj;
1990 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1992 uint64_t dir_obj, child_dir_zapobj;
1993 dnode_phys_t child_dir_zap, dir, dataset;
1994 dsl_dataset_phys_t *ds;
1997 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1998 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2001 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2002 dir_obj = ds->ds_dir_obj;
2004 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2005 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2008 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2010 child_dir_zapobj = dd->dd_child_dir_zapobj;
2011 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2012 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2016 return (zap_list(spa, &child_dir_zap) != 0);
2020 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2022 uint64_t dir_obj, child_dir_zapobj, zap_type;
2023 dnode_phys_t child_dir_zap, dir, dataset;
2024 dsl_dataset_phys_t *ds;
2028 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2030 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2033 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2034 dir_obj = ds->ds_dir_obj;
2036 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2038 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2041 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2043 child_dir_zapobj = dd->dd_child_dir_zapobj;
2044 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2046 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2050 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2054 zap_type = *(uint64_t *) zap_scratch;
2055 if (zap_type == ZBT_MICRO)
2056 return mzap_list(&child_dir_zap, callback);
2058 return fzap_list(spa, &child_dir_zap, callback);
2063 * Find the object set given the object number of its dataset object
2064 * and return its details in *objset
2067 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2069 dnode_phys_t dataset;
2070 dsl_dataset_phys_t *ds;
2072 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2073 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2077 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2078 if (zio_read(spa, &ds->ds_bp, objset)) {
2079 printf("ZFS: can't read object set for dataset %ju\n",
2088 * Find the object set pointed to by the BOOTFS property or the root
2089 * dataset if there is none and return its details in *objset
2092 zfs_get_root(const spa_t *spa, uint64_t *objid)
2094 dnode_phys_t dir, propdir;
2095 uint64_t props, bootfs, root;
2100 * Start with the MOS directory object.
2102 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2103 printf("ZFS: can't read MOS object directory\n");
2108 * Lookup the pool_props and see if we can find a bootfs.
2110 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2111 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2112 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2119 * Lookup the root dataset directory
2121 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2122 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2123 printf("ZFS: can't find root dsl_dir\n");
2128 * Use the information from the dataset directory's bonus buffer
2129 * to find the dataset object and from that the object set itself.
2131 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2132 *objid = dd->dd_head_dataset_obj;
2137 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2143 * Find the root object set if not explicitly provided
2145 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2146 printf("ZFS: can't find root filesystem\n");
2150 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2151 printf("ZFS: can't open root filesystem\n");
2155 mount->rootobj = rootobj;
2161 * callback function for feature name checks.
2164 check_feature(const char *name, uint64_t value)
2170 if (name[0] == '\0')
2173 for (i = 0; features_for_read[i] != NULL; i++) {
2174 if (strcmp(name, features_for_read[i]) == 0)
2177 printf("ZFS: unsupported feature: %s\n", name);
2182 * Checks whether the MOS features that are active are supported.
2185 check_mos_features(const spa_t *spa)
2188 uint64_t objnum, zap_type;
2192 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2195 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2196 sizeof (objnum), 1, &objnum)) != 0) {
2198 * It is older pool without features. As we have already
2199 * tested the label, just return without raising the error.
2204 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2207 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2210 size = dir.dn_datablkszsec * 512;
2211 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2214 zap_type = *(uint64_t *) zap_scratch;
2215 if (zap_type == ZBT_MICRO)
2216 rc = mzap_list(&dir, check_feature);
2218 rc = fzap_list(spa, &dir, check_feature);
2224 zfs_spa_init(spa_t *spa)
2229 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2230 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2233 if (spa->spa_mos.os_type != DMU_OST_META) {
2234 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2238 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2240 printf("ZFS: failed to read pool %s directory object\n",
2244 /* this is allowed to fail, older pools do not have salt */
2245 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2246 sizeof (spa->spa_cksum_salt.zcs_bytes),
2247 spa->spa_cksum_salt.zcs_bytes);
2249 rc = check_mos_features(spa);
2251 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2258 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2261 if (dn->dn_bonustype != DMU_OT_SA) {
2262 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2264 sb->st_mode = zp->zp_mode;
2265 sb->st_uid = zp->zp_uid;
2266 sb->st_gid = zp->zp_gid;
2267 sb->st_size = zp->zp_size;
2269 sa_hdr_phys_t *sahdrp;
2274 if (dn->dn_bonuslen != 0)
2275 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2277 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2278 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
2281 size = BP_GET_LSIZE(bp);
2282 buf = zfs_alloc(size);
2283 error = zio_read(spa, bp, buf);
2285 zfs_free(buf, size);
2293 hdrsize = SA_HDR_SIZE(sahdrp);
2294 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2296 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2298 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2300 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2303 zfs_free(buf, size);
2310 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2314 if (dn->dn_bonustype == DMU_OT_SA) {
2315 sa_hdr_phys_t *sahdrp = NULL;
2321 if (dn->dn_bonuslen != 0)
2322 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2326 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2328 bp = DN_SPILL_BLKPTR(dn);
2330 size = BP_GET_LSIZE(bp);
2331 buf = zfs_alloc(size);
2332 rc = zio_read(spa, bp, buf);
2334 zfs_free(buf, size);
2339 hdrsize = SA_HDR_SIZE(sahdrp);
2340 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2341 memcpy(path, p, psize);
2343 zfs_free(buf, size);
2347 * Second test is purely to silence bogus compiler
2348 * warning about accessing past the end of dn_bonus.
2350 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2351 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2352 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2354 rc = dnode_read(spa, dn, 0, path, psize);
2361 STAILQ_ENTRY(obj_list) entry;
2365 * Lookup a file and return its dnode.
2368 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2377 int symlinks_followed = 0;
2379 struct obj_list *entry, *tentry;
2380 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2383 if (mount->objset.os_type != DMU_OST_ZFS) {
2384 printf("ZFS: unexpected object set type %ju\n",
2385 (uintmax_t)mount->objset.os_type);
2389 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2393 * Get the root directory dnode.
2395 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2401 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
2406 entry->objnum = objnum;
2407 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2409 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2415 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2424 while (*q != '\0' && *q != '/')
2428 if (p + 1 == q && p[0] == '.') {
2433 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2435 if (STAILQ_FIRST(&on_cache) ==
2436 STAILQ_LAST(&on_cache, obj_list, entry)) {
2440 entry = STAILQ_FIRST(&on_cache);
2441 STAILQ_REMOVE_HEAD(&on_cache, entry);
2443 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2446 if (q - p + 1 > sizeof(element)) {
2450 memcpy(element, p, q - p);
2454 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2456 if (!S_ISDIR(sb.st_mode)) {
2461 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2464 objnum = ZFS_DIRENT_OBJ(objnum);
2466 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2470 entry->objnum = objnum;
2471 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2472 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2477 * Check for symlink.
2479 rc = zfs_dnode_stat(spa, &dn, &sb);
2482 if (S_ISLNK(sb.st_mode)) {
2483 if (symlinks_followed > 10) {
2487 symlinks_followed++;
2490 * Read the link value and copy the tail of our
2491 * current path onto the end.
2493 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2497 strcpy(&path[sb.st_size], p);
2499 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2504 * Restart with the new path, starting either at
2505 * the root or at the parent depending whether or
2506 * not the link is relative.
2510 while (STAILQ_FIRST(&on_cache) !=
2511 STAILQ_LAST(&on_cache, obj_list, entry)) {
2512 entry = STAILQ_FIRST(&on_cache);
2513 STAILQ_REMOVE_HEAD(&on_cache, entry);
2517 entry = STAILQ_FIRST(&on_cache);
2518 STAILQ_REMOVE_HEAD(&on_cache, entry);
2521 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2527 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)