2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Stand-alone ZFS file reader.
35 #include <sys/stdint.h>
48 * List of all vdevs, chained through v_alllink.
50 static vdev_list_t zfs_vdevs;
53 * List of ZFS features supported for read
55 static const char *features_for_read[] = {
56 "org.illumos:lz4_compress",
57 "com.delphix:hole_birth",
58 "com.delphix:extensible_dataset",
59 "com.delphix:embedded_data",
60 "org.open-zfs:large_blocks",
67 * List of all pools, chained through spa_link.
69 static spa_list_t zfs_pools;
71 static const dnode_phys_t *dnode_cache_obj;
72 static uint64_t dnode_cache_bn;
73 static char *dnode_cache_buf;
74 static char *zap_scratch;
75 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
77 #define TEMP_SIZE (1024 * 1024)
79 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
80 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
81 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
82 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
83 const char *name, uint64_t integer_size, uint64_t num_integers,
89 STAILQ_INIT(&zfs_vdevs);
90 STAILQ_INIT(&zfs_pools);
92 zfs_temp_buf = malloc(TEMP_SIZE);
93 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
94 zfs_temp_ptr = zfs_temp_buf;
95 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
96 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
102 zfs_alloc(size_t size)
106 if (zfs_temp_ptr + size > zfs_temp_end) {
107 printf("ZFS: out of temporary buffer space\n");
111 zfs_temp_ptr += size;
117 zfs_free(void *ptr, size_t size)
120 zfs_temp_ptr -= size;
121 if (zfs_temp_ptr != ptr) {
122 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
128 xdr_int(const unsigned char **xdr, int *ip)
130 *ip = ((*xdr)[0] << 24)
139 xdr_u_int(const unsigned char **xdr, u_int *ip)
141 *ip = ((*xdr)[0] << 24)
150 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
156 *lp = (((uint64_t) hi) << 32) | lo;
161 nvlist_find(const unsigned char *nvlist, const char *name, int type,
162 int* elementsp, void *valuep)
164 const unsigned char *p, *pair;
166 int encoded_size, decoded_size;
173 xdr_int(&p, &encoded_size);
174 xdr_int(&p, &decoded_size);
175 while (encoded_size && decoded_size) {
176 int namelen, pairtype, elements;
177 const char *pairname;
179 xdr_int(&p, &namelen);
180 pairname = (const char*) p;
181 p += roundup(namelen, 4);
182 xdr_int(&p, &pairtype);
184 if (!memcmp(name, pairname, namelen) && type == pairtype) {
185 xdr_int(&p, &elements);
187 *elementsp = elements;
188 if (type == DATA_TYPE_UINT64) {
189 xdr_uint64_t(&p, (uint64_t *) valuep);
191 } else if (type == DATA_TYPE_STRING) {
194 (*(const char**) valuep) = (const char*) p;
196 } else if (type == DATA_TYPE_NVLIST
197 || type == DATA_TYPE_NVLIST_ARRAY) {
198 (*(const unsigned char**) valuep) =
199 (const unsigned char*) p;
206 * Not the pair we are looking for, skip to the next one.
208 p = pair + encoded_size;
212 xdr_int(&p, &encoded_size);
213 xdr_int(&p, &decoded_size);
220 nvlist_check_features_for_read(const unsigned char *nvlist)
222 const unsigned char *p, *pair;
224 int encoded_size, decoded_size;
234 xdr_int(&p, &encoded_size);
235 xdr_int(&p, &decoded_size);
236 while (encoded_size && decoded_size) {
237 int namelen, pairtype;
238 const char *pairname;
243 xdr_int(&p, &namelen);
244 pairname = (const char*) p;
245 p += roundup(namelen, 4);
246 xdr_int(&p, &pairtype);
248 for (i = 0; features_for_read[i] != NULL; i++) {
249 if (!memcmp(pairname, features_for_read[i], namelen)) {
256 printf("ZFS: unsupported feature: %s\n", pairname);
260 p = pair + encoded_size;
263 xdr_int(&p, &encoded_size);
264 xdr_int(&p, &decoded_size);
271 * Return the next nvlist in an nvlist array.
273 static const unsigned char *
274 nvlist_next(const unsigned char *nvlist)
276 const unsigned char *p, *pair;
278 int encoded_size, decoded_size;
285 xdr_int(&p, &encoded_size);
286 xdr_int(&p, &decoded_size);
287 while (encoded_size && decoded_size) {
288 p = pair + encoded_size;
291 xdr_int(&p, &encoded_size);
292 xdr_int(&p, &decoded_size);
300 static const unsigned char *
301 nvlist_print(const unsigned char *nvlist, unsigned int indent)
303 static const char* typenames[] = {
314 "DATA_TYPE_BYTE_ARRAY",
315 "DATA_TYPE_INT16_ARRAY",
316 "DATA_TYPE_UINT16_ARRAY",
317 "DATA_TYPE_INT32_ARRAY",
318 "DATA_TYPE_UINT32_ARRAY",
319 "DATA_TYPE_INT64_ARRAY",
320 "DATA_TYPE_UINT64_ARRAY",
321 "DATA_TYPE_STRING_ARRAY",
324 "DATA_TYPE_NVLIST_ARRAY",
325 "DATA_TYPE_BOOLEAN_VALUE",
328 "DATA_TYPE_BOOLEAN_ARRAY",
329 "DATA_TYPE_INT8_ARRAY",
330 "DATA_TYPE_UINT8_ARRAY"
334 const unsigned char *p, *pair;
336 int encoded_size, decoded_size;
343 xdr_int(&p, &encoded_size);
344 xdr_int(&p, &decoded_size);
345 while (encoded_size && decoded_size) {
346 int namelen, pairtype, elements;
347 const char *pairname;
349 xdr_int(&p, &namelen);
350 pairname = (const char*) p;
351 p += roundup(namelen, 4);
352 xdr_int(&p, &pairtype);
354 for (i = 0; i < indent; i++)
356 printf("%s %s", typenames[pairtype], pairname);
358 xdr_int(&p, &elements);
360 case DATA_TYPE_UINT64: {
362 xdr_uint64_t(&p, &val);
363 printf(" = 0x%jx\n", (uintmax_t)val);
367 case DATA_TYPE_STRING: {
370 printf(" = \"%s\"\n", p);
374 case DATA_TYPE_NVLIST:
376 nvlist_print(p, indent + 1);
379 case DATA_TYPE_NVLIST_ARRAY:
380 for (j = 0; j < elements; j++) {
382 p = nvlist_print(p, indent + 1);
383 if (j != elements - 1) {
384 for (i = 0; i < indent; i++)
386 printf("%s %s", typenames[pairtype], pairname);
395 p = pair + encoded_size;
398 xdr_int(&p, &encoded_size);
399 xdr_int(&p, &decoded_size);
408 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
409 off_t offset, size_t size)
414 if (!vdev->v_phys_read)
418 psize = BP_GET_PSIZE(bp);
423 /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
424 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
427 if (bp && zio_checksum_verify(vdev->spa, bp, buf))
434 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
435 off_t offset, size_t bytes)
438 return (vdev_read_phys(vdev, bp, buf,
439 offset + VDEV_LABEL_START_SIZE, bytes));
444 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
445 off_t offset, size_t bytes)
451 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
452 if (kid->v_state != VDEV_STATE_HEALTHY)
454 rc = kid->v_read(kid, bp, buf, offset, bytes);
463 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
464 off_t offset, size_t bytes)
469 * Here we should have two kids:
470 * First one which is the one we are replacing and we can trust
471 * only this one to have valid data, but it might not be present.
472 * Second one is that one we are replacing with. It is most likely
473 * healthy, but we can't trust it has needed data, so we won't use it.
475 kid = STAILQ_FIRST(&vdev->v_children);
478 if (kid->v_state != VDEV_STATE_HEALTHY)
480 return (kid->v_read(kid, bp, buf, offset, bytes));
484 vdev_find(uint64_t guid)
488 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
489 if (vdev->v_guid == guid)
496 vdev_create(uint64_t guid, vdev_read_t *_read)
500 vdev = malloc(sizeof(vdev_t));
501 memset(vdev, 0, sizeof(vdev_t));
502 STAILQ_INIT(&vdev->v_children);
504 vdev->v_state = VDEV_STATE_OFFLINE;
505 vdev->v_read = _read;
506 vdev->v_phys_read = 0;
507 vdev->v_read_priv = 0;
508 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
514 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
515 vdev_t **vdevp, int is_newer)
518 uint64_t guid, id, ashift, nparity;
522 const unsigned char *kids;
523 int nkids, i, is_new;
524 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
526 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
528 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
529 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
531 printf("ZFS: can't find vdev details\n");
535 if (strcmp(type, VDEV_TYPE_MIRROR)
536 && strcmp(type, VDEV_TYPE_DISK)
538 && strcmp(type, VDEV_TYPE_FILE)
540 && strcmp(type, VDEV_TYPE_RAIDZ)
541 && strcmp(type, VDEV_TYPE_REPLACING)) {
542 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
546 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
548 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
550 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
552 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
554 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
556 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
559 vdev = vdev_find(guid);
563 if (!strcmp(type, VDEV_TYPE_MIRROR))
564 vdev = vdev_create(guid, vdev_mirror_read);
565 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
566 vdev = vdev_create(guid, vdev_raidz_read);
567 else if (!strcmp(type, VDEV_TYPE_REPLACING))
568 vdev = vdev_create(guid, vdev_replacing_read);
570 vdev = vdev_create(guid, vdev_disk_read);
573 vdev->v_top = pvdev != NULL ? pvdev : vdev;
574 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
575 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
576 vdev->v_ashift = ashift;
580 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
581 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
582 vdev->v_nparity = nparity;
586 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
587 DATA_TYPE_STRING, NULL, &path) == 0) {
588 if (strncmp(path, "/dev/", 5) == 0)
590 vdev->v_name = strdup(path);
592 if (!strcmp(type, "raidz")) {
593 if (vdev->v_nparity == 1)
594 vdev->v_name = "raidz1";
595 else if (vdev->v_nparity == 2)
596 vdev->v_name = "raidz2";
597 else if (vdev->v_nparity == 3)
598 vdev->v_name = "raidz3";
600 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
604 vdev->v_name = strdup(type);
611 if (is_new || is_newer) {
613 * This is either new vdev or we've already seen this vdev,
614 * but from an older vdev label, so let's refresh its state
615 * from the newer label.
618 vdev->v_state = VDEV_STATE_OFFLINE;
620 vdev->v_state = VDEV_STATE_REMOVED;
622 vdev->v_state = VDEV_STATE_FAULTED;
623 else if (is_degraded)
624 vdev->v_state = VDEV_STATE_DEGRADED;
625 else if (isnt_present)
626 vdev->v_state = VDEV_STATE_CANT_OPEN;
629 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
632 * Its ok if we don't have any kids.
635 vdev->v_nchildren = nkids;
636 for (i = 0; i < nkids; i++) {
637 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
641 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
643 kids = nvlist_next(kids);
646 vdev->v_nchildren = 0;
655 vdev_set_state(vdev_t *vdev)
662 * A mirror or raidz is healthy if all its kids are healthy. A
663 * mirror is degraded if any of its kids is healthy; a raidz
664 * is degraded if at most nparity kids are offline.
666 if (STAILQ_FIRST(&vdev->v_children)) {
669 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
670 if (kid->v_state == VDEV_STATE_HEALTHY)
676 vdev->v_state = VDEV_STATE_HEALTHY;
678 if (vdev->v_read == vdev_mirror_read) {
680 vdev->v_state = VDEV_STATE_DEGRADED;
682 vdev->v_state = VDEV_STATE_OFFLINE;
684 } else if (vdev->v_read == vdev_raidz_read) {
685 if (bad_kids > vdev->v_nparity) {
686 vdev->v_state = VDEV_STATE_OFFLINE;
688 vdev->v_state = VDEV_STATE_DEGRADED;
696 spa_find_by_guid(uint64_t guid)
700 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
701 if (spa->spa_guid == guid)
708 spa_find_by_name(const char *name)
712 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
713 if (!strcmp(spa->spa_name, name))
721 spa_get_primary(void)
724 return (STAILQ_FIRST(&zfs_pools));
728 spa_get_primary_vdev(const spa_t *spa)
734 spa = spa_get_primary();
737 vdev = STAILQ_FIRST(&spa->spa_vdevs);
740 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
741 kid = STAILQ_FIRST(&vdev->v_children))
748 spa_create(uint64_t guid, const char *name)
752 if ((spa = malloc(sizeof(spa_t))) == NULL)
754 memset(spa, 0, sizeof(spa_t));
755 if ((spa->spa_name = strdup(name)) == NULL) {
759 STAILQ_INIT(&spa->spa_vdevs);
760 spa->spa_guid = guid;
761 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
767 state_name(vdev_state_t state)
769 static const char* names[] = {
784 #define pager_printf printf
789 pager_printf(const char *fmt, ...)
795 vsprintf(line, fmt, args);
797 return (pager_output(line));
802 #define STATUS_FORMAT " %s %s\n"
805 print_state(int indent, const char *name, vdev_state_t state)
811 for (i = 0; i < indent; i++)
814 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
819 vdev_status(vdev_t *vdev, int indent)
823 ret = print_state(indent, vdev->v_name, vdev->v_state);
827 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
828 ret = vdev_status(kid, indent + 1);
836 spa_status(spa_t *spa)
838 static char bootfs[ZFS_MAXNAMELEN];
841 int good_kids, bad_kids, degraded_kids, ret;
844 ret = pager_printf(" pool: %s\n", spa->spa_name);
848 if (zfs_get_root(spa, &rootid) == 0 &&
849 zfs_rlookup(spa, rootid, bootfs) == 0) {
850 if (bootfs[0] == '\0')
851 ret = pager_printf("bootfs: %s\n", spa->spa_name);
853 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
858 ret = pager_printf("config:\n\n");
861 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
868 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
869 if (vdev->v_state == VDEV_STATE_HEALTHY)
871 else if (vdev->v_state == VDEV_STATE_DEGRADED)
877 state = VDEV_STATE_CLOSED;
878 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
879 state = VDEV_STATE_HEALTHY;
880 else if ((good_kids + degraded_kids) > 0)
881 state = VDEV_STATE_DEGRADED;
883 ret = print_state(0, spa->spa_name, state);
886 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
887 ret = vdev_status(vdev, 1);
898 int first = 1, ret = 0;
900 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
902 ret = pager_printf("\n");
907 ret = spa_status(spa);
915 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
917 uint64_t label_offset;
919 if (l < VDEV_LABELS / 2)
922 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
924 return (offset + l * sizeof (vdev_label_t) + label_offset);
928 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
931 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
932 vdev_phys_t *tmp_label;
934 vdev_t *vdev, *top_vdev, *pool_vdev;
937 const unsigned char *nvlist = NULL;
940 uint64_t best_txg = 0;
941 uint64_t pool_txg, pool_guid;
943 const char *pool_name;
944 const unsigned char *vdevs;
945 const unsigned char *features;
946 int i, l, rc, is_newer;
948 const struct uberblock *up;
951 * Load the vdev label and figure out which
952 * uberblock is most current.
954 memset(&vtmp, 0, sizeof(vtmp));
955 vtmp.v_phys_read = _read;
956 vtmp.v_read_priv = read_priv;
957 psize = P2ALIGN(ldi_get_size(read_priv),
958 (uint64_t)sizeof (vdev_label_t));
960 /* Test for minimum pool size. */
961 if (psize < SPA_MINDEVSIZE)
964 tmp_label = zfs_alloc(sizeof(vdev_phys_t));
966 for (l = 0; l < VDEV_LABELS; l++) {
967 off = vdev_label_offset(psize, l,
968 offsetof(vdev_label_t, vl_vdev_phys));
971 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
972 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
973 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
974 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
975 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
976 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
978 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
981 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
984 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
985 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
986 DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
989 if (best_txg <= pool_txg) {
991 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
995 zfs_free(tmp_label, sizeof (vdev_phys_t));
1000 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
1003 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1005 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1010 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1011 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1012 (unsigned) val, (unsigned) SPA_VERSION);
1016 /* Check ZFS features for read */
1017 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1018 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1019 nvlist_check_features_for_read(features) != 0) {
1023 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1028 if (val == POOL_STATE_DESTROYED) {
1029 /* We don't boot only from destroyed pools. */
1033 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1034 NULL, &pool_txg) != 0 ||
1035 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1036 NULL, &pool_guid) != 0 ||
1037 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1038 NULL, &pool_name) != 0) {
1040 * Cache and spare devices end up here - just ignore
1043 /*printf("ZFS: can't find pool details\n");*/
1047 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1048 NULL, &val) == 0 && val != 0) {
1053 * Create the pool if this is the first time we've seen it.
1055 spa = spa_find_by_guid(pool_guid);
1057 spa = spa_create(pool_guid, pool_name);
1061 if (pool_txg > spa->spa_txg) {
1062 spa->spa_txg = pool_txg;
1069 * Get the vdev tree and create our in-core copy of it.
1070 * If we already have a vdev with this guid, this must
1071 * be some kind of alias (overlapping slices, dangerously dedicated
1074 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1075 NULL, &guid) != 0) {
1078 vdev = vdev_find(guid);
1079 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1082 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1087 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1092 * Add the toplevel vdev to the pool if its not already there.
1094 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1095 if (top_vdev == pool_vdev)
1097 if (!pool_vdev && top_vdev) {
1098 top_vdev->spa = spa;
1099 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1103 * We should already have created an incomplete vdev for this
1104 * vdev. Find it and initialise it with our read proc.
1106 vdev = vdev_find(guid);
1108 vdev->v_phys_read = _read;
1109 vdev->v_read_priv = read_priv;
1110 vdev->v_state = VDEV_STATE_HEALTHY;
1112 printf("ZFS: inconsistent nvlist contents\n");
1117 * Re-evaluate top-level vdev state.
1119 vdev_set_state(top_vdev);
1122 * Ok, we are happy with the pool so far. Lets find
1123 * the best uberblock and then we can actually access
1124 * the contents of the pool.
1126 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1127 up = (const struct uberblock *)upbuf;
1128 for (l = 0; l < VDEV_LABELS; l++) {
1129 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1130 off = vdev_label_offset(psize, l,
1131 VDEV_UBERBLOCK_OFFSET(vdev, i));
1133 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1134 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1135 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1136 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1137 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1138 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1140 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1143 if (up->ub_magic != UBERBLOCK_MAGIC)
1145 if (up->ub_txg < spa->spa_txg)
1147 if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1148 (up->ub_txg == spa->spa_uberblock.ub_txg &&
1150 spa->spa_uberblock.ub_timestamp)) {
1151 spa->spa_uberblock = *up;
1155 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1168 for (v = 0; v < 32; v++)
1175 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1178 zio_gbh_phys_t zio_gb;
1182 /* Artificial BP for gang block header. */
1184 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1185 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1186 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1187 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1188 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1189 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1191 /* Read gang header block using the artificial BP. */
1192 if (zio_read(spa, &gbh_bp, &zio_gb))
1196 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1197 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1199 if (BP_IS_HOLE(gbp))
1201 if (zio_read(spa, gbp, pbuf))
1203 pbuf += BP_GET_PSIZE(gbp);
1206 if (zio_checksum_verify(spa, bp, buf))
1212 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1214 int cpfunc = BP_GET_COMPRESS(bp);
1215 uint64_t align, size;
1220 * Process data embedded in block pointer
1222 if (BP_IS_EMBEDDED(bp)) {
1223 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1225 size = BPE_GET_PSIZE(bp);
1226 ASSERT(size <= BPE_PAYLOAD_SIZE);
1228 if (cpfunc != ZIO_COMPRESS_OFF)
1229 pbuf = zfs_alloc(size);
1233 decode_embedded_bp_compressed(bp, pbuf);
1236 if (cpfunc != ZIO_COMPRESS_OFF) {
1237 error = zio_decompress_data(cpfunc, pbuf,
1238 size, buf, BP_GET_LSIZE(bp));
1239 zfs_free(pbuf, size);
1242 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1249 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1250 const dva_t *dva = &bp->blk_dva[i];
1255 if (!dva->dva_word[0] && !dva->dva_word[1])
1258 vdevid = DVA_GET_VDEV(dva);
1259 offset = DVA_GET_OFFSET(dva);
1260 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1261 if (vdev->v_id == vdevid)
1264 if (!vdev || !vdev->v_read)
1267 size = BP_GET_PSIZE(bp);
1268 if (vdev->v_read == vdev_raidz_read) {
1269 align = 1ULL << vdev->v_top->v_ashift;
1270 if (P2PHASE(size, align) != 0)
1271 size = P2ROUNDUP(size, align);
1273 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1274 pbuf = zfs_alloc(size);
1278 if (DVA_GET_GANG(dva))
1279 error = zio_read_gang(spa, bp, pbuf);
1281 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1283 if (cpfunc != ZIO_COMPRESS_OFF)
1284 error = zio_decompress_data(cpfunc, pbuf,
1285 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1286 else if (size != BP_GET_PSIZE(bp))
1287 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1290 zfs_free(pbuf, size);
1295 printf("ZFS: i/o error - all block copies unavailable\n");
1300 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1302 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1303 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1304 int nlevels = dnode->dn_nlevels;
1307 if (bsize > SPA_MAXBLOCKSIZE) {
1308 printf("ZFS: I/O error - blocks larger than %llu are not "
1309 "supported\n", SPA_MAXBLOCKSIZE);
1314 * Note: bsize may not be a power of two here so we need to do an
1315 * actual divide rather than a bitshift.
1317 while (buflen > 0) {
1318 uint64_t bn = offset / bsize;
1319 int boff = offset % bsize;
1321 const blkptr_t *indbp;
1324 if (bn > dnode->dn_maxblkid)
1327 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1330 indbp = dnode->dn_blkptr;
1331 for (i = 0; i < nlevels; i++) {
1333 * Copy the bp from the indirect array so that
1334 * we can re-use the scratch buffer for multi-level
1337 ibn = bn >> ((nlevels - i - 1) * ibshift);
1338 ibn &= ((1 << ibshift) - 1);
1340 if (BP_IS_HOLE(&bp)) {
1341 memset(dnode_cache_buf, 0, bsize);
1344 rc = zio_read(spa, &bp, dnode_cache_buf);
1347 indbp = (const blkptr_t *) dnode_cache_buf;
1349 dnode_cache_obj = dnode;
1350 dnode_cache_bn = bn;
1354 * The buffer contains our data block. Copy what we
1355 * need from it and loop.
1358 if (i > buflen) i = buflen;
1359 memcpy(buf, &dnode_cache_buf[boff], i);
1360 buf = ((char*) buf) + i;
1369 * Lookup a value in a microzap directory. Assumes that the zap
1370 * scratch buffer contains the directory contents.
1373 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1375 const mzap_phys_t *mz;
1376 const mzap_ent_phys_t *mze;
1381 * Microzap objects use exactly one block. Read the whole
1384 size = dnode->dn_datablkszsec * 512;
1386 mz = (const mzap_phys_t *) zap_scratch;
1387 chunks = size / MZAP_ENT_LEN - 1;
1389 for (i = 0; i < chunks; i++) {
1390 mze = &mz->mz_chunk[i];
1391 if (!strcmp(mze->mze_name, name)) {
1392 *value = mze->mze_value;
1401 * Compare a name with a zap leaf entry. Return non-zero if the name
1405 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1408 const zap_leaf_chunk_t *nc;
1411 namelen = zc->l_entry.le_name_numints;
1413 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1415 while (namelen > 0) {
1418 if (len > ZAP_LEAF_ARRAY_BYTES)
1419 len = ZAP_LEAF_ARRAY_BYTES;
1420 if (memcmp(p, nc->l_array.la_array, len))
1424 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1431 * Extract a uint64_t value from a zap leaf entry.
1434 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1436 const zap_leaf_chunk_t *vc;
1441 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1442 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1443 value = (value << 8) | p[i];
1450 stv(int len, void *addr, uint64_t value)
1454 *(uint8_t *)addr = value;
1457 *(uint16_t *)addr = value;
1460 *(uint32_t *)addr = value;
1463 *(uint64_t *)addr = value;
1469 * Extract a array from a zap leaf entry.
1472 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1473 uint64_t integer_size, uint64_t num_integers, void *buf)
1475 uint64_t array_int_len = zc->l_entry.le_value_intlen;
1477 uint64_t *u64 = buf;
1479 int len = MIN(zc->l_entry.le_value_numints, num_integers);
1480 int chunk = zc->l_entry.le_value_chunk;
1483 if (integer_size == 8 && len == 1) {
1484 *u64 = fzap_leaf_value(zl, zc);
1489 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1492 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1493 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1494 value = (value << 8) | la->la_array[i];
1496 if (byten == array_int_len) {
1497 stv(integer_size, p, value);
1505 chunk = la->la_next;
1510 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1511 * buffer contains the directory header.
1514 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1515 uint64_t integer_size, uint64_t num_integers, void *value)
1517 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1518 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1524 if (zh.zap_magic != ZAP_MAGIC)
1527 z.zap_block_shift = ilog2(bsize);
1528 z.zap_phys = (zap_phys_t *) zap_scratch;
1531 * Figure out where the pointer table is and read it in if necessary.
1533 if (zh.zap_ptrtbl.zt_blk) {
1534 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1535 zap_scratch, bsize);
1538 ptrtbl = (uint64_t *) zap_scratch;
1540 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1543 hash = zap_hash(zh.zap_salt, name);
1546 zl.l_bs = z.zap_block_shift;
1548 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1549 zap_leaf_chunk_t *zc;
1551 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1555 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1558 * Make sure this chunk matches our hash.
1560 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1561 && zl.l_phys->l_hdr.lh_prefix
1562 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1566 * Hash within the chunk to find our entry.
1568 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1569 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1570 h = zl.l_phys->l_hash[h];
1573 zc = &ZAP_LEAF_CHUNK(&zl, h);
1574 while (zc->l_entry.le_hash != hash) {
1575 if (zc->l_entry.le_next == 0xffff) {
1579 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1581 if (fzap_name_equal(&zl, zc, name)) {
1582 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1583 integer_size * num_integers)
1585 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1593 * Lookup a name in a zap object and return its value as a uint64_t.
1596 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1597 uint64_t integer_size, uint64_t num_integers, void *value)
1601 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1603 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1607 zap_type = *(uint64_t *) zap_scratch;
1608 if (zap_type == ZBT_MICRO)
1609 return mzap_lookup(dnode, name, value);
1610 else if (zap_type == ZBT_HEADER) {
1611 return fzap_lookup(spa, dnode, name, integer_size,
1612 num_integers, value);
1614 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1619 * List a microzap directory. Assumes that the zap scratch buffer contains
1620 * the directory contents.
1623 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1625 const mzap_phys_t *mz;
1626 const mzap_ent_phys_t *mze;
1631 * Microzap objects use exactly one block. Read the whole
1634 size = dnode->dn_datablkszsec * 512;
1635 mz = (const mzap_phys_t *) zap_scratch;
1636 chunks = size / MZAP_ENT_LEN - 1;
1638 for (i = 0; i < chunks; i++) {
1639 mze = &mz->mz_chunk[i];
1640 if (mze->mze_name[0]) {
1641 rc = callback(mze->mze_name, mze->mze_value);
1651 * List a fatzap directory. Assumes that the zap scratch buffer contains
1652 * the directory header.
1655 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1657 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1658 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1662 if (zh.zap_magic != ZAP_MAGIC)
1665 z.zap_block_shift = ilog2(bsize);
1666 z.zap_phys = (zap_phys_t *) zap_scratch;
1669 * This assumes that the leaf blocks start at block 1. The
1670 * documentation isn't exactly clear on this.
1673 zl.l_bs = z.zap_block_shift;
1674 for (i = 0; i < zh.zap_num_leafs; i++) {
1675 off_t off = (i + 1) << zl.l_bs;
1679 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1682 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1684 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1685 zap_leaf_chunk_t *zc, *nc;
1688 zc = &ZAP_LEAF_CHUNK(&zl, j);
1689 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1691 namelen = zc->l_entry.le_name_numints;
1692 if (namelen > sizeof(name))
1693 namelen = sizeof(name);
1696 * Paste the name back together.
1698 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1700 while (namelen > 0) {
1703 if (len > ZAP_LEAF_ARRAY_BYTES)
1704 len = ZAP_LEAF_ARRAY_BYTES;
1705 memcpy(p, nc->l_array.la_array, len);
1708 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1712 * Assume the first eight bytes of the value are
1715 value = fzap_leaf_value(&zl, zc);
1717 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1718 rc = callback((const char *)name, value);
1727 static int zfs_printf(const char *name, uint64_t value __unused)
1730 printf("%s\n", name);
1736 * List a zap directory.
1739 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1742 size_t size = dnode->dn_datablkszsec * 512;
1744 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1747 zap_type = *(uint64_t *) zap_scratch;
1748 if (zap_type == ZBT_MICRO)
1749 return mzap_list(dnode, zfs_printf);
1751 return fzap_list(spa, dnode, zfs_printf);
1755 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1759 offset = objnum * sizeof(dnode_phys_t);
1760 return dnode_read(spa, &os->os_meta_dnode, offset,
1761 dnode, sizeof(dnode_phys_t));
1765 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1767 const mzap_phys_t *mz;
1768 const mzap_ent_phys_t *mze;
1773 * Microzap objects use exactly one block. Read the whole
1776 size = dnode->dn_datablkszsec * 512;
1778 mz = (const mzap_phys_t *) zap_scratch;
1779 chunks = size / MZAP_ENT_LEN - 1;
1781 for (i = 0; i < chunks; i++) {
1782 mze = &mz->mz_chunk[i];
1783 if (value == mze->mze_value) {
1784 strcpy(name, mze->mze_name);
1793 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1796 const zap_leaf_chunk_t *nc;
1799 namelen = zc->l_entry.le_name_numints;
1801 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1803 while (namelen > 0) {
1806 if (len > ZAP_LEAF_ARRAY_BYTES)
1807 len = ZAP_LEAF_ARRAY_BYTES;
1808 memcpy(p, nc->l_array.la_array, len);
1811 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1818 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1820 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1821 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1825 if (zh.zap_magic != ZAP_MAGIC)
1828 z.zap_block_shift = ilog2(bsize);
1829 z.zap_phys = (zap_phys_t *) zap_scratch;
1832 * This assumes that the leaf blocks start at block 1. The
1833 * documentation isn't exactly clear on this.
1836 zl.l_bs = z.zap_block_shift;
1837 for (i = 0; i < zh.zap_num_leafs; i++) {
1838 off_t off = (i + 1) << zl.l_bs;
1840 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1843 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1845 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1846 zap_leaf_chunk_t *zc;
1848 zc = &ZAP_LEAF_CHUNK(&zl, j);
1849 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1851 if (zc->l_entry.le_value_intlen != 8 ||
1852 zc->l_entry.le_value_numints != 1)
1855 if (fzap_leaf_value(&zl, zc) == value) {
1856 fzap_name_copy(&zl, zc, name);
1866 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1870 size_t size = dnode->dn_datablkszsec * 512;
1872 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1876 zap_type = *(uint64_t *) zap_scratch;
1877 if (zap_type == ZBT_MICRO)
1878 return mzap_rlookup(spa, dnode, name, value);
1880 return fzap_rlookup(spa, dnode, name, value);
1884 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1887 char component[256];
1888 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1889 dnode_phys_t child_dir_zap, dataset, dir, parent;
1891 dsl_dataset_phys_t *ds;
1895 p = &name[sizeof(name) - 1];
1898 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1899 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1902 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1903 dir_obj = ds->ds_dir_obj;
1906 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1908 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1910 /* Actual loop condition. */
1911 parent_obj = dd->dd_parent_obj;
1912 if (parent_obj == 0)
1915 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1917 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1918 child_dir_zapobj = dd->dd_child_dir_zapobj;
1919 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1921 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1924 len = strlen(component);
1926 memcpy(p, component, len);
1930 /* Actual loop iteration. */
1931 dir_obj = parent_obj;
1942 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1945 uint64_t dir_obj, child_dir_zapobj;
1946 dnode_phys_t child_dir_zap, dir;
1950 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1952 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1958 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1960 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1964 /* Actual loop condition #1. */
1970 memcpy(element, p, q - p);
1971 element[q - p] = '\0';
1978 child_dir_zapobj = dd->dd_child_dir_zapobj;
1979 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1982 /* Actual loop condition #2. */
1983 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1988 *objnum = dd->dd_head_dataset_obj;
1994 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1996 uint64_t dir_obj, child_dir_zapobj;
1997 dnode_phys_t child_dir_zap, dir, dataset;
1998 dsl_dataset_phys_t *ds;
2001 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2002 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2005 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2006 dir_obj = ds->ds_dir_obj;
2008 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2009 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2012 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2014 child_dir_zapobj = dd->dd_child_dir_zapobj;
2015 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2016 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2020 return (zap_list(spa, &child_dir_zap) != 0);
2024 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2026 uint64_t dir_obj, child_dir_zapobj, zap_type;
2027 dnode_phys_t child_dir_zap, dir, dataset;
2028 dsl_dataset_phys_t *ds;
2032 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2034 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2037 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2038 dir_obj = ds->ds_dir_obj;
2040 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2042 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2045 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2047 child_dir_zapobj = dd->dd_child_dir_zapobj;
2048 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2050 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2054 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2058 zap_type = *(uint64_t *) zap_scratch;
2059 if (zap_type == ZBT_MICRO)
2060 return mzap_list(&child_dir_zap, callback);
2062 return fzap_list(spa, &child_dir_zap, callback);
2067 * Find the object set given the object number of its dataset object
2068 * and return its details in *objset
2071 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2073 dnode_phys_t dataset;
2074 dsl_dataset_phys_t *ds;
2076 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2077 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2081 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2082 if (zio_read(spa, &ds->ds_bp, objset)) {
2083 printf("ZFS: can't read object set for dataset %ju\n",
2092 * Find the object set pointed to by the BOOTFS property or the root
2093 * dataset if there is none and return its details in *objset
2096 zfs_get_root(const spa_t *spa, uint64_t *objid)
2098 dnode_phys_t dir, propdir;
2099 uint64_t props, bootfs, root;
2104 * Start with the MOS directory object.
2106 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2107 printf("ZFS: can't read MOS object directory\n");
2112 * Lookup the pool_props and see if we can find a bootfs.
2114 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2115 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2116 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2123 * Lookup the root dataset directory
2125 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2126 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2127 printf("ZFS: can't find root dsl_dir\n");
2132 * Use the information from the dataset directory's bonus buffer
2133 * to find the dataset object and from that the object set itself.
2135 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2136 *objid = dd->dd_head_dataset_obj;
2141 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2147 * Find the root object set if not explicitly provided
2149 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2150 printf("ZFS: can't find root filesystem\n");
2154 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2155 printf("ZFS: can't open root filesystem\n");
2159 mount->rootobj = rootobj;
2165 * callback function for feature name checks.
2168 check_feature(const char *name, uint64_t value)
2174 if (name[0] == '\0')
2177 for (i = 0; features_for_read[i] != NULL; i++) {
2178 if (strcmp(name, features_for_read[i]) == 0)
2181 printf("ZFS: unsupported feature: %s\n", name);
2186 * Checks whether the MOS features that are active are supported.
2189 check_mos_features(const spa_t *spa)
2192 uint64_t objnum, zap_type;
2196 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2199 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2200 sizeof (objnum), 1, &objnum)) != 0) {
2202 * It is older pool without features. As we have already
2203 * tested the label, just return without raising the error.
2208 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2211 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2214 size = dir.dn_datablkszsec * 512;
2215 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2218 zap_type = *(uint64_t *) zap_scratch;
2219 if (zap_type == ZBT_MICRO)
2220 rc = mzap_list(&dir, check_feature);
2222 rc = fzap_list(spa, &dir, check_feature);
2228 zfs_spa_init(spa_t *spa)
2233 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2234 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2237 if (spa->spa_mos.os_type != DMU_OST_META) {
2238 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2242 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2244 printf("ZFS: failed to read pool %s directory object\n",
2248 /* this is allowed to fail, older pools do not have salt */
2249 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2250 sizeof (spa->spa_cksum_salt.zcs_bytes),
2251 spa->spa_cksum_salt.zcs_bytes);
2253 rc = check_mos_features(spa);
2255 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2262 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2265 if (dn->dn_bonustype != DMU_OT_SA) {
2266 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2268 sb->st_mode = zp->zp_mode;
2269 sb->st_uid = zp->zp_uid;
2270 sb->st_gid = zp->zp_gid;
2271 sb->st_size = zp->zp_size;
2273 sa_hdr_phys_t *sahdrp;
2278 if (dn->dn_bonuslen != 0)
2279 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2281 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2282 blkptr_t *bp = &dn->dn_spill;
2285 size = BP_GET_LSIZE(bp);
2286 buf = zfs_alloc(size);
2287 error = zio_read(spa, bp, buf);
2289 zfs_free(buf, size);
2297 hdrsize = SA_HDR_SIZE(sahdrp);
2298 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2300 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2302 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2304 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2307 zfs_free(buf, size);
2314 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2318 if (dn->dn_bonustype == DMU_OT_SA) {
2319 sa_hdr_phys_t *sahdrp = NULL;
2325 if (dn->dn_bonuslen != 0)
2326 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2330 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2334 size = BP_GET_LSIZE(bp);
2335 buf = zfs_alloc(size);
2336 rc = zio_read(spa, bp, buf);
2338 zfs_free(buf, size);
2343 hdrsize = SA_HDR_SIZE(sahdrp);
2344 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2345 memcpy(path, p, psize);
2347 zfs_free(buf, size);
2351 * Second test is purely to silence bogus compiler
2352 * warning about accessing past the end of dn_bonus.
2354 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2355 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2356 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2358 rc = dnode_read(spa, dn, 0, path, psize);
2365 STAILQ_ENTRY(obj_list) entry;
2369 * Lookup a file and return its dnode.
2372 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2381 int symlinks_followed = 0;
2383 struct obj_list *entry, *tentry;
2384 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2387 if (mount->objset.os_type != DMU_OST_ZFS) {
2388 printf("ZFS: unexpected object set type %ju\n",
2389 (uintmax_t)mount->objset.os_type);
2393 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2397 * Get the root directory dnode.
2399 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2405 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
2410 entry->objnum = objnum;
2411 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2413 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2419 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2428 while (*q != '\0' && *q != '/')
2432 if (p + 1 == q && p[0] == '.') {
2437 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2439 if (STAILQ_FIRST(&on_cache) ==
2440 STAILQ_LAST(&on_cache, obj_list, entry)) {
2444 entry = STAILQ_FIRST(&on_cache);
2445 STAILQ_REMOVE_HEAD(&on_cache, entry);
2447 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2450 if (q - p + 1 > sizeof(element)) {
2454 memcpy(element, p, q - p);
2458 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2460 if (!S_ISDIR(sb.st_mode)) {
2465 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2468 objnum = ZFS_DIRENT_OBJ(objnum);
2470 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2474 entry->objnum = objnum;
2475 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2476 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2481 * Check for symlink.
2483 rc = zfs_dnode_stat(spa, &dn, &sb);
2486 if (S_ISLNK(sb.st_mode)) {
2487 if (symlinks_followed > 10) {
2491 symlinks_followed++;
2494 * Read the link value and copy the tail of our
2495 * current path onto the end.
2497 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2501 strcpy(&path[sb.st_size], p);
2503 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2508 * Restart with the new path, starting either at
2509 * the root or at the parent depending whether or
2510 * not the link is relative.
2514 while (STAILQ_FIRST(&on_cache) !=
2515 STAILQ_LAST(&on_cache, obj_list, entry)) {
2516 entry = STAILQ_FIRST(&on_cache);
2517 STAILQ_REMOVE_HEAD(&on_cache, entry);
2521 entry = STAILQ_FIRST(&on_cache);
2522 STAILQ_REMOVE_HEAD(&on_cache, entry);
2525 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2531 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)