2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Stand-alone ZFS file reader.
35 #include <sys/stdint.h>
46 static struct zfsmount zfsmount __unused;
49 * List of all vdevs, chained through v_alllink.
51 static vdev_list_t zfs_vdevs;
54 * List of ZFS features supported for read
56 static const char *features_for_read[] = {
57 "org.illumos:lz4_compress",
58 "com.delphix:hole_birth",
59 "com.delphix:extensible_dataset",
60 "com.delphix:embedded_data",
61 "org.open-zfs:large_blocks",
64 "org.zfsonlinux:large_dnode",
69 * List of all pools, chained through spa_link.
71 static spa_list_t zfs_pools;
73 static const dnode_phys_t *dnode_cache_obj;
74 static uint64_t dnode_cache_bn;
75 static char *dnode_cache_buf;
76 static char *zap_scratch;
77 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
79 #define TEMP_SIZE (1024 * 1024)
81 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
82 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
83 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
84 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
85 const char *name, uint64_t integer_size, uint64_t num_integers,
91 STAILQ_INIT(&zfs_vdevs);
92 STAILQ_INIT(&zfs_pools);
94 zfs_temp_buf = malloc(TEMP_SIZE);
95 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
96 zfs_temp_ptr = zfs_temp_buf;
97 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
98 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
104 zfs_alloc(size_t size)
108 if (zfs_temp_ptr + size > zfs_temp_end) {
109 printf("ZFS: out of temporary buffer space\n");
113 zfs_temp_ptr += size;
119 zfs_free(void *ptr, size_t size)
122 zfs_temp_ptr -= size;
123 if (zfs_temp_ptr != ptr) {
124 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
130 xdr_int(const unsigned char **xdr, int *ip)
132 *ip = ((*xdr)[0] << 24)
141 xdr_u_int(const unsigned char **xdr, u_int *ip)
143 *ip = ((*xdr)[0] << 24)
152 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
158 *lp = (((uint64_t) hi) << 32) | lo;
163 nvlist_find(const unsigned char *nvlist, const char *name, int type,
164 int* elementsp, void *valuep)
166 const unsigned char *p, *pair;
168 int encoded_size, decoded_size;
175 xdr_int(&p, &encoded_size);
176 xdr_int(&p, &decoded_size);
177 while (encoded_size && decoded_size) {
178 int namelen, pairtype, elements;
179 const char *pairname;
181 xdr_int(&p, &namelen);
182 pairname = (const char*) p;
183 p += roundup(namelen, 4);
184 xdr_int(&p, &pairtype);
186 if (!memcmp(name, pairname, namelen) && type == pairtype) {
187 xdr_int(&p, &elements);
189 *elementsp = elements;
190 if (type == DATA_TYPE_UINT64) {
191 xdr_uint64_t(&p, (uint64_t *) valuep);
193 } else if (type == DATA_TYPE_STRING) {
196 (*(const char**) valuep) = (const char*) p;
198 } else if (type == DATA_TYPE_NVLIST
199 || type == DATA_TYPE_NVLIST_ARRAY) {
200 (*(const unsigned char**) valuep) =
201 (const unsigned char*) p;
208 * Not the pair we are looking for, skip to the next one.
210 p = pair + encoded_size;
214 xdr_int(&p, &encoded_size);
215 xdr_int(&p, &decoded_size);
222 nvlist_check_features_for_read(const unsigned char *nvlist)
224 const unsigned char *p, *pair;
226 int encoded_size, decoded_size;
236 xdr_int(&p, &encoded_size);
237 xdr_int(&p, &decoded_size);
238 while (encoded_size && decoded_size) {
239 int namelen, pairtype;
240 const char *pairname;
245 xdr_int(&p, &namelen);
246 pairname = (const char*) p;
247 p += roundup(namelen, 4);
248 xdr_int(&p, &pairtype);
250 for (i = 0; features_for_read[i] != NULL; i++) {
251 if (!memcmp(pairname, features_for_read[i], namelen)) {
258 printf("ZFS: unsupported feature: %s\n", pairname);
262 p = pair + encoded_size;
265 xdr_int(&p, &encoded_size);
266 xdr_int(&p, &decoded_size);
273 * Return the next nvlist in an nvlist array.
275 static const unsigned char *
276 nvlist_next(const unsigned char *nvlist)
278 const unsigned char *p, *pair;
280 int encoded_size, decoded_size;
287 xdr_int(&p, &encoded_size);
288 xdr_int(&p, &decoded_size);
289 while (encoded_size && decoded_size) {
290 p = pair + encoded_size;
293 xdr_int(&p, &encoded_size);
294 xdr_int(&p, &decoded_size);
302 static const unsigned char *
303 nvlist_print(const unsigned char *nvlist, unsigned int indent)
305 static const char* typenames[] = {
316 "DATA_TYPE_BYTE_ARRAY",
317 "DATA_TYPE_INT16_ARRAY",
318 "DATA_TYPE_UINT16_ARRAY",
319 "DATA_TYPE_INT32_ARRAY",
320 "DATA_TYPE_UINT32_ARRAY",
321 "DATA_TYPE_INT64_ARRAY",
322 "DATA_TYPE_UINT64_ARRAY",
323 "DATA_TYPE_STRING_ARRAY",
326 "DATA_TYPE_NVLIST_ARRAY",
327 "DATA_TYPE_BOOLEAN_VALUE",
330 "DATA_TYPE_BOOLEAN_ARRAY",
331 "DATA_TYPE_INT8_ARRAY",
332 "DATA_TYPE_UINT8_ARRAY"
336 const unsigned char *p, *pair;
338 int encoded_size, decoded_size;
345 xdr_int(&p, &encoded_size);
346 xdr_int(&p, &decoded_size);
347 while (encoded_size && decoded_size) {
348 int namelen, pairtype, elements;
349 const char *pairname;
351 xdr_int(&p, &namelen);
352 pairname = (const char*) p;
353 p += roundup(namelen, 4);
354 xdr_int(&p, &pairtype);
356 for (i = 0; i < indent; i++)
358 printf("%s %s", typenames[pairtype], pairname);
360 xdr_int(&p, &elements);
362 case DATA_TYPE_UINT64: {
364 xdr_uint64_t(&p, &val);
365 printf(" = 0x%jx\n", (uintmax_t)val);
369 case DATA_TYPE_STRING: {
372 printf(" = \"%s\"\n", p);
376 case DATA_TYPE_NVLIST:
378 nvlist_print(p, indent + 1);
381 case DATA_TYPE_NVLIST_ARRAY:
382 for (j = 0; j < elements; j++) {
384 p = nvlist_print(p, indent + 1);
385 if (j != elements - 1) {
386 for (i = 0; i < indent; i++)
388 printf("%s %s", typenames[pairtype], pairname);
397 p = pair + encoded_size;
400 xdr_int(&p, &encoded_size);
401 xdr_int(&p, &decoded_size);
410 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
411 off_t offset, size_t size)
416 if (!vdev->v_phys_read)
420 psize = BP_GET_PSIZE(bp);
425 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
426 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
429 if (bp && zio_checksum_verify(vdev->spa, bp, buf))
436 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
437 off_t offset, size_t bytes)
440 return (vdev_read_phys(vdev, bp, buf,
441 offset + VDEV_LABEL_START_SIZE, bytes));
446 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
447 off_t offset, size_t bytes)
453 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
454 if (kid->v_state != VDEV_STATE_HEALTHY)
456 rc = kid->v_read(kid, bp, buf, offset, bytes);
465 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
466 off_t offset, size_t bytes)
471 * Here we should have two kids:
472 * First one which is the one we are replacing and we can trust
473 * only this one to have valid data, but it might not be present.
474 * Second one is that one we are replacing with. It is most likely
475 * healthy, but we can't trust it has needed data, so we won't use it.
477 kid = STAILQ_FIRST(&vdev->v_children);
480 if (kid->v_state != VDEV_STATE_HEALTHY)
482 return (kid->v_read(kid, bp, buf, offset, bytes));
486 vdev_find(uint64_t guid)
490 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
491 if (vdev->v_guid == guid)
498 vdev_create(uint64_t guid, vdev_read_t *_read)
502 vdev = malloc(sizeof(vdev_t));
503 memset(vdev, 0, sizeof(vdev_t));
504 STAILQ_INIT(&vdev->v_children);
506 vdev->v_state = VDEV_STATE_OFFLINE;
507 vdev->v_read = _read;
508 vdev->v_phys_read = 0;
509 vdev->v_read_priv = 0;
510 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
516 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
517 vdev_t **vdevp, int is_newer)
520 uint64_t guid, id, ashift, nparity;
524 const unsigned char *kids;
525 int nkids, i, is_new;
526 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
528 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
530 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id)
531 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
533 printf("ZFS: can't find vdev details\n");
537 if (strcmp(type, VDEV_TYPE_MIRROR)
538 && strcmp(type, VDEV_TYPE_DISK)
540 && strcmp(type, VDEV_TYPE_FILE)
542 && strcmp(type, VDEV_TYPE_RAIDZ)
543 && strcmp(type, VDEV_TYPE_REPLACING)) {
544 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
548 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
550 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
552 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
554 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
556 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
558 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
561 vdev = vdev_find(guid);
565 if (!strcmp(type, VDEV_TYPE_MIRROR))
566 vdev = vdev_create(guid, vdev_mirror_read);
567 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
568 vdev = vdev_create(guid, vdev_raidz_read);
569 else if (!strcmp(type, VDEV_TYPE_REPLACING))
570 vdev = vdev_create(guid, vdev_replacing_read);
572 vdev = vdev_create(guid, vdev_disk_read);
575 vdev->v_top = pvdev != NULL ? pvdev : vdev;
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
577 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
578 vdev->v_ashift = ashift;
582 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
583 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
584 vdev->v_nparity = nparity;
588 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
589 DATA_TYPE_STRING, NULL, &path) == 0) {
590 if (strncmp(path, "/dev/", 5) == 0)
592 vdev->v_name = strdup(path);
594 if (!strcmp(type, "raidz")) {
595 if (vdev->v_nparity == 1)
596 vdev->v_name = "raidz1";
597 else if (vdev->v_nparity == 2)
598 vdev->v_name = "raidz2";
599 else if (vdev->v_nparity == 3)
600 vdev->v_name = "raidz3";
602 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
606 vdev->v_name = strdup(type);
613 if (is_new || is_newer) {
615 * This is either new vdev or we've already seen this vdev,
616 * but from an older vdev label, so let's refresh its state
617 * from the newer label.
620 vdev->v_state = VDEV_STATE_OFFLINE;
622 vdev->v_state = VDEV_STATE_REMOVED;
624 vdev->v_state = VDEV_STATE_FAULTED;
625 else if (is_degraded)
626 vdev->v_state = VDEV_STATE_DEGRADED;
627 else if (isnt_present)
628 vdev->v_state = VDEV_STATE_CANT_OPEN;
631 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
634 * Its ok if we don't have any kids.
637 vdev->v_nchildren = nkids;
638 for (i = 0; i < nkids; i++) {
639 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
643 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
645 kids = nvlist_next(kids);
648 vdev->v_nchildren = 0;
657 vdev_set_state(vdev_t *vdev)
664 * A mirror or raidz is healthy if all its kids are healthy. A
665 * mirror is degraded if any of its kids is healthy; a raidz
666 * is degraded if at most nparity kids are offline.
668 if (STAILQ_FIRST(&vdev->v_children)) {
671 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
672 if (kid->v_state == VDEV_STATE_HEALTHY)
678 vdev->v_state = VDEV_STATE_HEALTHY;
680 if (vdev->v_read == vdev_mirror_read) {
682 vdev->v_state = VDEV_STATE_DEGRADED;
684 vdev->v_state = VDEV_STATE_OFFLINE;
686 } else if (vdev->v_read == vdev_raidz_read) {
687 if (bad_kids > vdev->v_nparity) {
688 vdev->v_state = VDEV_STATE_OFFLINE;
690 vdev->v_state = VDEV_STATE_DEGRADED;
698 spa_find_by_guid(uint64_t guid)
702 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
703 if (spa->spa_guid == guid)
710 spa_find_by_name(const char *name)
714 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
715 if (!strcmp(spa->spa_name, name))
723 spa_get_primary(void)
726 return (STAILQ_FIRST(&zfs_pools));
730 spa_get_primary_vdev(const spa_t *spa)
736 spa = spa_get_primary();
739 vdev = STAILQ_FIRST(&spa->spa_vdevs);
742 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
743 kid = STAILQ_FIRST(&vdev->v_children))
750 spa_create(uint64_t guid, const char *name)
754 if ((spa = malloc(sizeof(spa_t))) == NULL)
756 memset(spa, 0, sizeof(spa_t));
757 if ((spa->spa_name = strdup(name)) == NULL) {
761 STAILQ_INIT(&spa->spa_vdevs);
762 spa->spa_guid = guid;
763 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
769 state_name(vdev_state_t state)
771 static const char* names[] = {
786 #define pager_printf printf
791 pager_printf(const char *fmt, ...)
797 vsprintf(line, fmt, args);
800 return (pager_output(line));
805 #define STATUS_FORMAT " %s %s\n"
808 print_state(int indent, const char *name, vdev_state_t state)
814 for (i = 0; i < indent; i++)
818 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
822 vdev_status(vdev_t *vdev, int indent)
826 ret = print_state(indent, vdev->v_name, vdev->v_state);
830 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
831 ret = vdev_status(kid, indent + 1);
839 spa_status(spa_t *spa)
841 static char bootfs[ZFS_MAXNAMELEN];
844 int good_kids, bad_kids, degraded_kids, ret;
847 ret = pager_printf(" pool: %s\n", spa->spa_name);
851 if (zfs_get_root(spa, &rootid) == 0 &&
852 zfs_rlookup(spa, rootid, bootfs) == 0) {
853 if (bootfs[0] == '\0')
854 ret = pager_printf("bootfs: %s\n", spa->spa_name);
856 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
861 ret = pager_printf("config:\n\n");
864 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
871 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
872 if (vdev->v_state == VDEV_STATE_HEALTHY)
874 else if (vdev->v_state == VDEV_STATE_DEGRADED)
880 state = VDEV_STATE_CLOSED;
881 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
882 state = VDEV_STATE_HEALTHY;
883 else if ((good_kids + degraded_kids) > 0)
884 state = VDEV_STATE_DEGRADED;
886 ret = print_state(0, spa->spa_name, state);
889 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
890 ret = vdev_status(vdev, 1);
901 int first = 1, ret = 0;
903 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
905 ret = pager_printf("\n");
910 ret = spa_status(spa);
918 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
920 uint64_t label_offset;
922 if (l < VDEV_LABELS / 2)
925 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
927 return (offset + l * sizeof (vdev_label_t) + label_offset);
931 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
934 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
935 vdev_phys_t *tmp_label;
937 vdev_t *vdev, *top_vdev, *pool_vdev;
940 const unsigned char *nvlist = NULL;
943 uint64_t best_txg = 0;
944 uint64_t pool_txg, pool_guid;
946 const char *pool_name;
947 const unsigned char *vdevs;
948 const unsigned char *features;
949 int i, l, rc, is_newer;
951 const struct uberblock *up;
954 * Load the vdev label and figure out which
955 * uberblock is most current.
957 memset(&vtmp, 0, sizeof(vtmp));
958 vtmp.v_phys_read = _read;
959 vtmp.v_read_priv = read_priv;
960 psize = P2ALIGN(ldi_get_size(read_priv),
961 (uint64_t)sizeof (vdev_label_t));
963 /* Test for minimum pool size. */
964 if (psize < SPA_MINDEVSIZE)
967 tmp_label = zfs_alloc(sizeof(vdev_phys_t));
969 for (l = 0; l < VDEV_LABELS; l++) {
970 off = vdev_label_offset(psize, l,
971 offsetof(vdev_label_t, vl_vdev_phys));
974 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
975 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
976 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
977 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
978 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
979 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
981 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
984 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
987 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
988 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
989 DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
992 if (best_txg <= pool_txg) {
994 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
998 zfs_free(tmp_label, sizeof (vdev_phys_t));
1003 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
1006 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1008 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1013 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1014 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1015 (unsigned) val, (unsigned) SPA_VERSION);
1019 /* Check ZFS features for read */
1020 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1021 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1022 nvlist_check_features_for_read(features) != 0) {
1026 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1031 if (val == POOL_STATE_DESTROYED) {
1032 /* We don't boot only from destroyed pools. */
1036 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1037 NULL, &pool_txg) != 0 ||
1038 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1039 NULL, &pool_guid) != 0 ||
1040 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1041 NULL, &pool_name) != 0) {
1043 * Cache and spare devices end up here - just ignore
1046 /*printf("ZFS: can't find pool details\n");*/
1050 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1051 NULL, &val) == 0 && val != 0) {
1056 * Create the pool if this is the first time we've seen it.
1058 spa = spa_find_by_guid(pool_guid);
1060 spa = spa_create(pool_guid, pool_name);
1064 if (pool_txg > spa->spa_txg) {
1065 spa->spa_txg = pool_txg;
1072 * Get the vdev tree and create our in-core copy of it.
1073 * If we already have a vdev with this guid, this must
1074 * be some kind of alias (overlapping slices, dangerously dedicated
1077 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1078 NULL, &guid) != 0) {
1081 vdev = vdev_find(guid);
1082 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1085 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1090 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1095 * Add the toplevel vdev to the pool if its not already there.
1097 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1098 if (top_vdev == pool_vdev)
1100 if (!pool_vdev && top_vdev) {
1101 top_vdev->spa = spa;
1102 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1106 * We should already have created an incomplete vdev for this
1107 * vdev. Find it and initialise it with our read proc.
1109 vdev = vdev_find(guid);
1111 vdev->v_phys_read = _read;
1112 vdev->v_read_priv = read_priv;
1113 vdev->v_state = VDEV_STATE_HEALTHY;
1115 printf("ZFS: inconsistent nvlist contents\n");
1120 * Re-evaluate top-level vdev state.
1122 vdev_set_state(top_vdev);
1125 * Ok, we are happy with the pool so far. Lets find
1126 * the best uberblock and then we can actually access
1127 * the contents of the pool.
1129 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1130 up = (const struct uberblock *)upbuf;
1131 for (l = 0; l < VDEV_LABELS; l++) {
1132 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1133 off = vdev_label_offset(psize, l,
1134 VDEV_UBERBLOCK_OFFSET(vdev, i));
1136 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1137 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1138 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1139 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1140 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1141 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1143 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1146 if (up->ub_magic != UBERBLOCK_MAGIC)
1148 if (up->ub_txg < spa->spa_txg)
1150 if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1151 (up->ub_txg == spa->spa_uberblock.ub_txg &&
1153 spa->spa_uberblock.ub_timestamp)) {
1154 spa->spa_uberblock = *up;
1158 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1171 for (v = 0; v < 32; v++)
1178 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1181 zio_gbh_phys_t zio_gb;
1185 /* Artificial BP for gang block header. */
1187 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1188 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1189 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1190 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1191 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1192 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1194 /* Read gang header block using the artificial BP. */
1195 if (zio_read(spa, &gbh_bp, &zio_gb))
1199 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1200 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1202 if (BP_IS_HOLE(gbp))
1204 if (zio_read(spa, gbp, pbuf))
1206 pbuf += BP_GET_PSIZE(gbp);
1209 if (zio_checksum_verify(spa, bp, buf))
1215 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1217 int cpfunc = BP_GET_COMPRESS(bp);
1218 uint64_t align, size;
1223 * Process data embedded in block pointer
1225 if (BP_IS_EMBEDDED(bp)) {
1226 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1228 size = BPE_GET_PSIZE(bp);
1229 ASSERT(size <= BPE_PAYLOAD_SIZE);
1231 if (cpfunc != ZIO_COMPRESS_OFF)
1232 pbuf = zfs_alloc(size);
1236 decode_embedded_bp_compressed(bp, pbuf);
1239 if (cpfunc != ZIO_COMPRESS_OFF) {
1240 error = zio_decompress_data(cpfunc, pbuf,
1241 size, buf, BP_GET_LSIZE(bp));
1242 zfs_free(pbuf, size);
1245 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1252 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1253 const dva_t *dva = &bp->blk_dva[i];
1258 if (!dva->dva_word[0] && !dva->dva_word[1])
1261 vdevid = DVA_GET_VDEV(dva);
1262 offset = DVA_GET_OFFSET(dva);
1263 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1264 if (vdev->v_id == vdevid)
1267 if (!vdev || !vdev->v_read)
1270 size = BP_GET_PSIZE(bp);
1271 if (vdev->v_read == vdev_raidz_read) {
1272 align = 1ULL << vdev->v_top->v_ashift;
1273 if (P2PHASE(size, align) != 0)
1274 size = P2ROUNDUP(size, align);
1276 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1277 pbuf = zfs_alloc(size);
1281 if (DVA_GET_GANG(dva))
1282 error = zio_read_gang(spa, bp, pbuf);
1284 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1286 if (cpfunc != ZIO_COMPRESS_OFF)
1287 error = zio_decompress_data(cpfunc, pbuf,
1288 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1289 else if (size != BP_GET_PSIZE(bp))
1290 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1293 zfs_free(pbuf, size);
1298 printf("ZFS: i/o error - all block copies unavailable\n");
1303 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1305 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1306 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1307 int nlevels = dnode->dn_nlevels;
1310 if (bsize > SPA_MAXBLOCKSIZE) {
1311 printf("ZFS: I/O error - blocks larger than %llu are not "
1312 "supported\n", SPA_MAXBLOCKSIZE);
1317 * Note: bsize may not be a power of two here so we need to do an
1318 * actual divide rather than a bitshift.
1320 while (buflen > 0) {
1321 uint64_t bn = offset / bsize;
1322 int boff = offset % bsize;
1324 const blkptr_t *indbp;
1327 if (bn > dnode->dn_maxblkid)
1330 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1333 indbp = dnode->dn_blkptr;
1334 for (i = 0; i < nlevels; i++) {
1336 * Copy the bp from the indirect array so that
1337 * we can re-use the scratch buffer for multi-level
1340 ibn = bn >> ((nlevels - i - 1) * ibshift);
1341 ibn &= ((1 << ibshift) - 1);
1343 if (BP_IS_HOLE(&bp)) {
1344 memset(dnode_cache_buf, 0, bsize);
1347 rc = zio_read(spa, &bp, dnode_cache_buf);
1350 indbp = (const blkptr_t *) dnode_cache_buf;
1352 dnode_cache_obj = dnode;
1353 dnode_cache_bn = bn;
1357 * The buffer contains our data block. Copy what we
1358 * need from it and loop.
1361 if (i > buflen) i = buflen;
1362 memcpy(buf, &dnode_cache_buf[boff], i);
1363 buf = ((char*) buf) + i;
1372 * Lookup a value in a microzap directory. Assumes that the zap
1373 * scratch buffer contains the directory contents.
1376 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1378 const mzap_phys_t *mz;
1379 const mzap_ent_phys_t *mze;
1384 * Microzap objects use exactly one block. Read the whole
1387 size = dnode->dn_datablkszsec * 512;
1389 mz = (const mzap_phys_t *) zap_scratch;
1390 chunks = size / MZAP_ENT_LEN - 1;
1392 for (i = 0; i < chunks; i++) {
1393 mze = &mz->mz_chunk[i];
1394 if (!strcmp(mze->mze_name, name)) {
1395 *value = mze->mze_value;
1404 * Compare a name with a zap leaf entry. Return non-zero if the name
1408 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1411 const zap_leaf_chunk_t *nc;
1414 namelen = zc->l_entry.le_name_numints;
1416 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1418 while (namelen > 0) {
1421 if (len > ZAP_LEAF_ARRAY_BYTES)
1422 len = ZAP_LEAF_ARRAY_BYTES;
1423 if (memcmp(p, nc->l_array.la_array, len))
1427 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1434 * Extract a uint64_t value from a zap leaf entry.
1437 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1439 const zap_leaf_chunk_t *vc;
1444 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1445 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1446 value = (value << 8) | p[i];
1453 stv(int len, void *addr, uint64_t value)
1457 *(uint8_t *)addr = value;
1460 *(uint16_t *)addr = value;
1463 *(uint32_t *)addr = value;
1466 *(uint64_t *)addr = value;
1472 * Extract a array from a zap leaf entry.
1475 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
1476 uint64_t integer_size, uint64_t num_integers, void *buf)
1478 uint64_t array_int_len = zc->l_entry.le_value_intlen;
1480 uint64_t *u64 = buf;
1482 int len = MIN(zc->l_entry.le_value_numints, num_integers);
1483 int chunk = zc->l_entry.le_value_chunk;
1486 if (integer_size == 8 && len == 1) {
1487 *u64 = fzap_leaf_value(zl, zc);
1492 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
1495 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
1496 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
1497 value = (value << 8) | la->la_array[i];
1499 if (byten == array_int_len) {
1500 stv(integer_size, p, value);
1508 chunk = la->la_next;
1513 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1514 * buffer contains the directory header.
1517 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1518 uint64_t integer_size, uint64_t num_integers, void *value)
1520 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1521 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1527 if (zh.zap_magic != ZAP_MAGIC)
1530 z.zap_block_shift = ilog2(bsize);
1531 z.zap_phys = (zap_phys_t *) zap_scratch;
1534 * Figure out where the pointer table is and read it in if necessary.
1536 if (zh.zap_ptrtbl.zt_blk) {
1537 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1538 zap_scratch, bsize);
1541 ptrtbl = (uint64_t *) zap_scratch;
1543 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1546 hash = zap_hash(zh.zap_salt, name);
1549 zl.l_bs = z.zap_block_shift;
1551 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1552 zap_leaf_chunk_t *zc;
1554 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1558 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1561 * Make sure this chunk matches our hash.
1563 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1564 && zl.l_phys->l_hdr.lh_prefix
1565 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1569 * Hash within the chunk to find our entry.
1571 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1572 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1573 h = zl.l_phys->l_hash[h];
1576 zc = &ZAP_LEAF_CHUNK(&zl, h);
1577 while (zc->l_entry.le_hash != hash) {
1578 if (zc->l_entry.le_next == 0xffff) {
1582 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1584 if (fzap_name_equal(&zl, zc, name)) {
1585 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints >
1586 integer_size * num_integers)
1588 fzap_leaf_array(&zl, zc, integer_size, num_integers, value);
1596 * Lookup a name in a zap object and return its value as a uint64_t.
1599 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
1600 uint64_t integer_size, uint64_t num_integers, void *value)
1604 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1606 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1610 zap_type = *(uint64_t *) zap_scratch;
1611 if (zap_type == ZBT_MICRO)
1612 return mzap_lookup(dnode, name, value);
1613 else if (zap_type == ZBT_HEADER) {
1614 return fzap_lookup(spa, dnode, name, integer_size,
1615 num_integers, value);
1617 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1622 * List a microzap directory. Assumes that the zap scratch buffer contains
1623 * the directory contents.
1626 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1628 const mzap_phys_t *mz;
1629 const mzap_ent_phys_t *mze;
1634 * Microzap objects use exactly one block. Read the whole
1637 size = dnode->dn_datablkszsec * 512;
1638 mz = (const mzap_phys_t *) zap_scratch;
1639 chunks = size / MZAP_ENT_LEN - 1;
1641 for (i = 0; i < chunks; i++) {
1642 mze = &mz->mz_chunk[i];
1643 if (mze->mze_name[0]) {
1644 rc = callback(mze->mze_name, mze->mze_value);
1654 * List a fatzap directory. Assumes that the zap scratch buffer contains
1655 * the directory header.
1658 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1660 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1661 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1665 if (zh.zap_magic != ZAP_MAGIC)
1668 z.zap_block_shift = ilog2(bsize);
1669 z.zap_phys = (zap_phys_t *) zap_scratch;
1672 * This assumes that the leaf blocks start at block 1. The
1673 * documentation isn't exactly clear on this.
1676 zl.l_bs = z.zap_block_shift;
1677 for (i = 0; i < zh.zap_num_leafs; i++) {
1678 off_t off = (i + 1) << zl.l_bs;
1682 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1685 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1687 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1688 zap_leaf_chunk_t *zc, *nc;
1691 zc = &ZAP_LEAF_CHUNK(&zl, j);
1692 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1694 namelen = zc->l_entry.le_name_numints;
1695 if (namelen > sizeof(name))
1696 namelen = sizeof(name);
1699 * Paste the name back together.
1701 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1703 while (namelen > 0) {
1706 if (len > ZAP_LEAF_ARRAY_BYTES)
1707 len = ZAP_LEAF_ARRAY_BYTES;
1708 memcpy(p, nc->l_array.la_array, len);
1711 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1715 * Assume the first eight bytes of the value are
1718 value = fzap_leaf_value(&zl, zc);
1720 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1721 rc = callback((const char *)name, value);
1730 static int zfs_printf(const char *name, uint64_t value __unused)
1733 printf("%s\n", name);
1739 * List a zap directory.
1742 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1745 size_t size = dnode->dn_datablkszsec * 512;
1747 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1750 zap_type = *(uint64_t *) zap_scratch;
1751 if (zap_type == ZBT_MICRO)
1752 return mzap_list(dnode, zfs_printf);
1754 return fzap_list(spa, dnode, zfs_printf);
1758 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1762 offset = objnum * sizeof(dnode_phys_t);
1763 return dnode_read(spa, &os->os_meta_dnode, offset,
1764 dnode, sizeof(dnode_phys_t));
1768 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1770 const mzap_phys_t *mz;
1771 const mzap_ent_phys_t *mze;
1776 * Microzap objects use exactly one block. Read the whole
1779 size = dnode->dn_datablkszsec * 512;
1781 mz = (const mzap_phys_t *) zap_scratch;
1782 chunks = size / MZAP_ENT_LEN - 1;
1784 for (i = 0; i < chunks; i++) {
1785 mze = &mz->mz_chunk[i];
1786 if (value == mze->mze_value) {
1787 strcpy(name, mze->mze_name);
1796 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1799 const zap_leaf_chunk_t *nc;
1802 namelen = zc->l_entry.le_name_numints;
1804 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1806 while (namelen > 0) {
1809 if (len > ZAP_LEAF_ARRAY_BYTES)
1810 len = ZAP_LEAF_ARRAY_BYTES;
1811 memcpy(p, nc->l_array.la_array, len);
1814 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1821 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1823 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1824 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1828 if (zh.zap_magic != ZAP_MAGIC)
1831 z.zap_block_shift = ilog2(bsize);
1832 z.zap_phys = (zap_phys_t *) zap_scratch;
1835 * This assumes that the leaf blocks start at block 1. The
1836 * documentation isn't exactly clear on this.
1839 zl.l_bs = z.zap_block_shift;
1840 for (i = 0; i < zh.zap_num_leafs; i++) {
1841 off_t off = (i + 1) << zl.l_bs;
1843 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1846 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1848 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1849 zap_leaf_chunk_t *zc;
1851 zc = &ZAP_LEAF_CHUNK(&zl, j);
1852 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1854 if (zc->l_entry.le_value_intlen != 8 ||
1855 zc->l_entry.le_value_numints != 1)
1858 if (fzap_leaf_value(&zl, zc) == value) {
1859 fzap_name_copy(&zl, zc, name);
1869 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1873 size_t size = dnode->dn_datablkszsec * 512;
1875 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1879 zap_type = *(uint64_t *) zap_scratch;
1880 if (zap_type == ZBT_MICRO)
1881 return mzap_rlookup(spa, dnode, name, value);
1883 return fzap_rlookup(spa, dnode, name, value);
1887 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1890 char component[256];
1891 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1892 dnode_phys_t child_dir_zap, dataset, dir, parent;
1894 dsl_dataset_phys_t *ds;
1898 p = &name[sizeof(name) - 1];
1901 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1902 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1905 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1906 dir_obj = ds->ds_dir_obj;
1909 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1911 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1913 /* Actual loop condition. */
1914 parent_obj = dd->dd_parent_obj;
1915 if (parent_obj == 0)
1918 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1920 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1921 child_dir_zapobj = dd->dd_child_dir_zapobj;
1922 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1924 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1927 len = strlen(component);
1929 memcpy(p, component, len);
1933 /* Actual loop iteration. */
1934 dir_obj = parent_obj;
1945 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1948 uint64_t dir_obj, child_dir_zapobj;
1949 dnode_phys_t child_dir_zap, dir;
1953 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1955 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
1961 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1963 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1967 /* Actual loop condition #1. */
1973 memcpy(element, p, q - p);
1974 element[q - p] = '\0';
1981 child_dir_zapobj = dd->dd_child_dir_zapobj;
1982 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1985 /* Actual loop condition #2. */
1986 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
1991 *objnum = dd->dd_head_dataset_obj;
1997 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1999 uint64_t dir_obj, child_dir_zapobj;
2000 dnode_phys_t child_dir_zap, dir, dataset;
2001 dsl_dataset_phys_t *ds;
2004 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2005 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2008 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2009 dir_obj = ds->ds_dir_obj;
2011 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
2012 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2015 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2017 child_dir_zapobj = dd->dd_child_dir_zapobj;
2018 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
2019 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2023 return (zap_list(spa, &child_dir_zap) != 0);
2027 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
2029 uint64_t dir_obj, child_dir_zapobj, zap_type;
2030 dnode_phys_t child_dir_zap, dir, dataset;
2031 dsl_dataset_phys_t *ds;
2035 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
2037 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2040 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2041 dir_obj = ds->ds_dir_obj;
2043 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
2045 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
2048 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
2050 child_dir_zapobj = dd->dd_child_dir_zapobj;
2051 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
2053 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
2057 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
2061 zap_type = *(uint64_t *) zap_scratch;
2062 if (zap_type == ZBT_MICRO)
2063 return mzap_list(&child_dir_zap, callback);
2065 return fzap_list(spa, &child_dir_zap, callback);
2070 * Find the object set given the object number of its dataset object
2071 * and return its details in *objset
2074 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2076 dnode_phys_t dataset;
2077 dsl_dataset_phys_t *ds;
2079 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2080 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2084 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2085 if (zio_read(spa, &ds->ds_bp, objset)) {
2086 printf("ZFS: can't read object set for dataset %ju\n",
2095 * Find the object set pointed to by the BOOTFS property or the root
2096 * dataset if there is none and return its details in *objset
2099 zfs_get_root(const spa_t *spa, uint64_t *objid)
2101 dnode_phys_t dir, propdir;
2102 uint64_t props, bootfs, root;
2107 * Start with the MOS directory object.
2109 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2110 printf("ZFS: can't read MOS object directory\n");
2115 * Lookup the pool_props and see if we can find a bootfs.
2117 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0
2118 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2119 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0
2126 * Lookup the root dataset directory
2128 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root)
2129 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2130 printf("ZFS: can't find root dsl_dir\n");
2135 * Use the information from the dataset directory's bonus buffer
2136 * to find the dataset object and from that the object set itself.
2138 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2139 *objid = dd->dd_head_dataset_obj;
2144 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
2150 * Find the root object set if not explicitly provided
2152 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2153 printf("ZFS: can't find root filesystem\n");
2157 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2158 printf("ZFS: can't open root filesystem\n");
2162 mount->rootobj = rootobj;
2168 * callback function for feature name checks.
2171 check_feature(const char *name, uint64_t value)
2177 if (name[0] == '\0')
2180 for (i = 0; features_for_read[i] != NULL; i++) {
2181 if (strcmp(name, features_for_read[i]) == 0)
2184 printf("ZFS: unsupported feature: %s\n", name);
2189 * Checks whether the MOS features that are active are supported.
2192 check_mos_features(const spa_t *spa)
2195 uint64_t objnum, zap_type;
2199 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2202 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2203 sizeof (objnum), 1, &objnum)) != 0) {
2205 * It is older pool without features. As we have already
2206 * tested the label, just return without raising the error.
2211 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2214 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2217 size = dir.dn_datablkszsec * 512;
2218 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2221 zap_type = *(uint64_t *) zap_scratch;
2222 if (zap_type == ZBT_MICRO)
2223 rc = mzap_list(&dir, check_feature);
2225 rc = fzap_list(spa, &dir, check_feature);
2231 zfs_spa_init(spa_t *spa)
2236 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2237 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2240 if (spa->spa_mos.os_type != DMU_OST_META) {
2241 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2245 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT,
2247 printf("ZFS: failed to read pool %s directory object\n",
2251 /* this is allowed to fail, older pools do not have salt */
2252 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
2253 sizeof (spa->spa_cksum_salt.zcs_bytes),
2254 spa->spa_cksum_salt.zcs_bytes);
2256 rc = check_mos_features(spa);
2258 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2265 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2268 if (dn->dn_bonustype != DMU_OT_SA) {
2269 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2271 sb->st_mode = zp->zp_mode;
2272 sb->st_uid = zp->zp_uid;
2273 sb->st_gid = zp->zp_gid;
2274 sb->st_size = zp->zp_size;
2276 sa_hdr_phys_t *sahdrp;
2281 if (dn->dn_bonuslen != 0)
2282 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2284 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2285 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
2288 size = BP_GET_LSIZE(bp);
2289 buf = zfs_alloc(size);
2290 error = zio_read(spa, bp, buf);
2292 zfs_free(buf, size);
2300 hdrsize = SA_HDR_SIZE(sahdrp);
2301 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2303 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2305 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2307 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2310 zfs_free(buf, size);
2317 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2321 if (dn->dn_bonustype == DMU_OT_SA) {
2322 sa_hdr_phys_t *sahdrp = NULL;
2328 if (dn->dn_bonuslen != 0)
2329 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2333 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2335 bp = DN_SPILL_BLKPTR(dn);
2337 size = BP_GET_LSIZE(bp);
2338 buf = zfs_alloc(size);
2339 rc = zio_read(spa, bp, buf);
2341 zfs_free(buf, size);
2346 hdrsize = SA_HDR_SIZE(sahdrp);
2347 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2348 memcpy(path, p, psize);
2350 zfs_free(buf, size);
2354 * Second test is purely to silence bogus compiler
2355 * warning about accessing past the end of dn_bonus.
2357 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2358 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2359 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2361 rc = dnode_read(spa, dn, 0, path, psize);
2368 STAILQ_ENTRY(obj_list) entry;
2372 * Lookup a file and return its dnode.
2375 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2384 int symlinks_followed = 0;
2386 struct obj_list *entry, *tentry;
2387 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2390 if (mount->objset.os_type != DMU_OST_ZFS) {
2391 printf("ZFS: unexpected object set type %ju\n",
2392 (uintmax_t)mount->objset.os_type);
2396 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2400 * Get the root directory dnode.
2402 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2408 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum);
2413 entry->objnum = objnum;
2414 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2416 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2422 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2431 while (*q != '\0' && *q != '/')
2435 if (p + 1 == q && p[0] == '.') {
2440 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2442 if (STAILQ_FIRST(&on_cache) ==
2443 STAILQ_LAST(&on_cache, obj_list, entry)) {
2447 entry = STAILQ_FIRST(&on_cache);
2448 STAILQ_REMOVE_HEAD(&on_cache, entry);
2450 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2453 if (q - p + 1 > sizeof(element)) {
2457 memcpy(element, p, q - p);
2461 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2463 if (!S_ISDIR(sb.st_mode)) {
2468 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
2471 objnum = ZFS_DIRENT_OBJ(objnum);
2473 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2477 entry->objnum = objnum;
2478 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2479 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2484 * Check for symlink.
2486 rc = zfs_dnode_stat(spa, &dn, &sb);
2489 if (S_ISLNK(sb.st_mode)) {
2490 if (symlinks_followed > 10) {
2494 symlinks_followed++;
2497 * Read the link value and copy the tail of our
2498 * current path onto the end.
2500 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2504 strcpy(&path[sb.st_size], p);
2506 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2511 * Restart with the new path, starting either at
2512 * the root or at the parent depending whether or
2513 * not the link is relative.
2517 while (STAILQ_FIRST(&on_cache) !=
2518 STAILQ_LAST(&on_cache, obj_list, entry)) {
2519 entry = STAILQ_FIRST(&on_cache);
2520 STAILQ_REMOVE_HEAD(&on_cache, entry);
2524 entry = STAILQ_FIRST(&on_cache);
2525 STAILQ_REMOVE_HEAD(&on_cache, entry);
2528 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2534 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)