199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2007 Doug Rabson
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Stand-alone ZFS file reader.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stdint.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "zfsimpl.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "zfssubr.c"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct zfsmount {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome objset_phys_t objset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t rootobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List of all vdevs, chained through v_alllink.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vdev_list_t zfs_vdevs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List of ZFS features supported for read
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *features_for_read[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "org.illumos:lz4_compress",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "com.delphix:hole_birth",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "com.delphix:extensible_dataset",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "com.delphix:embedded_data",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "org.open-zfs:large_blocks",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "org.illumos:sha512",
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List of all pools, chained through spa_link.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic spa_list_t zfs_pools;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint64_t zfs_crc64_table[256];
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const dnode_phys_t *dnode_cache_obj = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint64_t dnode_cache_bn;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *dnode_cache_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define TEMP_SIZE (1024 * 1024)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zfs_get_root(const spa_t *spa, uint64_t *objid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&zfs_vdevs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&zfs_pools);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_temp_buf = malloc(TEMP_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_temp_ptr = zfs_temp_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_scratch = malloc(SPA_MAXBLOCKSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_init_crc();
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void *
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_alloc(size_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *ptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zfs_temp_ptr + size > zfs_temp_end) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: out of temporary buffer space\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptr = zfs_temp_ptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_temp_ptr += size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_free(void *ptr, size_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_temp_ptr -= size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zfs_temp_ptr != ptr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_int(const unsigned char **xdr, int *ip)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip = ((*xdr)[0] << 24)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[1] << 16)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[2] << 8)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[3] << 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*xdr) += 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_u_int(const unsigned char **xdr, u_int *ip)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip = ((*xdr)[0] << 24)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[1] << 16)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[2] << 8)
199767f8919635c4928607450d9e0abb932109ceToomas Soome | ((*xdr)[3] << 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*xdr) += 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int hi, lo;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_u_int(xdr, &hi);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_u_int(xdr, &lo);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lp = (((uint64_t) hi) << 32) | lo;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenvlist_find(const unsigned char *nvlist, const char *name, int type,
199767f8919635c4928607450d9e0abb932109ceToomas Soome int* elementsp, void *valuep)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p, *pair;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int junk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int encoded_size, decoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nvlist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (encoded_size && decoded_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namelen, pairtype, elements;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *pairname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &namelen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pairname = (const char*) p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += roundup(namelen, 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &pairtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!memcmp(name, pairname, namelen) && type == pairtype) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &elements);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (elementsp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *elementsp = elements;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (type == DATA_TYPE_UINT64) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_uint64_t(&p, (uint64_t *) valuep);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (type == DATA_TYPE_STRING) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*(const char**) valuep) = (const char*) p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (type == DATA_TYPE_NVLIST
199767f8919635c4928607450d9e0abb932109ceToomas Soome || type == DATA_TYPE_NVLIST_ARRAY) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*(const unsigned char**) valuep) =
199767f8919635c4928607450d9e0abb932109ceToomas Soome (const unsigned char*) p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Not the pair we are looking for, skip to the next one.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = pair + encoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenvlist_check_features_for_read(const unsigned char *nvlist)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p, *pair;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int junk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int encoded_size, decoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nvlist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (encoded_size && decoded_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namelen, pairtype;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *pairname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, found;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome found = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &namelen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pairname = (const char*) p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += roundup(namelen, 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &pairtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; features_for_read[i] != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!memcmp(pairname, features_for_read[i], namelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome found = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!found) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: unsupported feature: %s\n", pairname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = pair + encoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return the next nvlist in an nvlist array.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomenvlist_next(const unsigned char *nvlist)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p, *pair;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int junk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int encoded_size, decoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nvlist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (encoded_size && decoded_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = pair + encoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TEST
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomenvlist_print(const unsigned char *nvlist, unsigned int indent)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static const char* typenames[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UNKNOWN",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_BOOLEAN",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_BYTE",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT16",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT16",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT32",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT32",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT64",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT64",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_STRING",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_BYTE_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT16_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT16_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT32_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT32_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT64_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT64_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_STRING_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_HRTIME",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_NVLIST",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_NVLIST_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_BOOLEAN_VALUE",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT8",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT8",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_BOOLEAN_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_INT8_ARRAY",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DATA_TYPE_UINT8_ARRAY"
199767f8919635c4928607450d9e0abb932109ceToomas Soome };
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int i, j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p, *pair;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int junk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int encoded_size, decoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nvlist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &junk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (encoded_size && decoded_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namelen, pairtype, elements;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *pairname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &namelen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pairname = (const char*) p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += roundup(namelen, 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &pairtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < indent; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s %s", typenames[pairtype], pairname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &elements);
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (pairtype) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA_TYPE_UINT64: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_uint64_t(&p, &val);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" = 0x%jx\n", (uintmax_t)val);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA_TYPE_STRING: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" = \"%s\"\n", p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA_TYPE_NVLIST:
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_print(p, indent + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA_TYPE_NVLIST_ARRAY:
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < elements; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("[%d]\n", j);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = nvlist_print(p, indent + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (j != elements - 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < indent; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s %s", typenames[pairtype], pairname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = pair + encoded_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pair = p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &encoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xdr_int(&p, &decoded_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset, size_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t psize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!vdev->v_phys_read)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome psize = BP_GET_PSIZE(bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome psize = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp && zio_checksum_verify(bp, buf))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset, size_t bytes)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (vdev_read_phys(vdev, bp, buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset + VDEV_LABEL_START_SIZE, bytes));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset, size_t bytes)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kid->v_state != VDEV_STATE_HEALTHY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = kid->v_read(kid, bp, buf, offset, bytes);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset, size_t bytes)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Here we should have two kids:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * First one which is the one we are replacing and we can trust
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only this one to have valid data, but it might not be present.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Second one is that one we are replacing with. It is most likely
199767f8919635c4928607450d9e0abb932109ceToomas Soome * healthy, but we can't trust it has needed data, so we won't use it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome kid = STAILQ_FIRST(&vdev->v_children);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kid == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kid->v_state != VDEV_STATE_HEALTHY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (kid->v_read(kid, bp, buf, offset, bytes));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vdev_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_find(uint64_t guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_guid == guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (vdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vdev_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_create(uint64_t guid, vdev_read_t *vdev_read)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = malloc(sizeof(vdev_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(vdev, 0, sizeof(vdev_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&vdev->v_children);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_guid = guid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_OFFLINE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_read = vdev_read;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_phys_read = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_read_priv = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (vdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t **vdevp, int is_newer)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t guid, id, ashift, nparity;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev, *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *kids;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nkids, i, is_new;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &id)
199767f8919635c4928607450d9e0abb932109ceToomas Soome || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_STRING, 0, &type)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find vdev details\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(type, VDEV_TYPE_MIRROR)
199767f8919635c4928607450d9e0abb932109ceToomas Soome && strcmp(type, VDEV_TYPE_DISK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ZFS_TEST
199767f8919635c4928607450d9e0abb932109ceToomas Soome && strcmp(type, VDEV_TYPE_FILE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome && strcmp(type, VDEV_TYPE_RAIDZ)
199767f8919635c4928607450d9e0abb932109ceToomas Soome && strcmp(type, VDEV_TYPE_REPLACING)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &is_offline);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &is_removed);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &is_faulted);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &is_degraded);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &isnt_present);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_find(guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!vdev) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_new = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(type, VDEV_TYPE_MIRROR))
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_create(guid, vdev_mirror_read);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (!strcmp(type, VDEV_TYPE_RAIDZ))
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_create(guid, vdev_raidz_read);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (!strcmp(type, VDEV_TYPE_REPLACING))
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_create(guid, vdev_replacing_read);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_create(guid, vdev_disk_read);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_id = id;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_top = pvdev != NULL ? pvdev : vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &ashift) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_ashift = ashift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_ashift = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &nparity) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_nparity = nparity;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_nparity = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_STRING, 0, &path) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(path, "/dev/dsk/", 9) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path += 9;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_name = strdup(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_PHYS_PATH,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_STRING, 0, &path) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_phys_path = strdup(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_phys_path = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist, ZPOOL_CONFIG_DEVID,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_STRING, 0, &path) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_devid = strdup(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_devid = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(type, "raidz")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_nparity == 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_name = "raidz1";
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (vdev->v_nparity == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_name = "raidz2";
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (vdev->v_nparity == 3)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_name = "raidz3";
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_name = strdup(type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_new = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_new || is_newer) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is either new vdev or we've already seen this vdev,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but from an older vdev label, so let's refresh its state
199767f8919635c4928607450d9e0abb932109ceToomas Soome * from the newer label.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_offline)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_OFFLINE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (is_removed)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_REMOVED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (is_faulted)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_FAULTED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (is_degraded)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_DEGRADED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (isnt_present)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_CANT_OPEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Its ok if we don't have any kids.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_nchildren = nkids;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nkids; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_new)
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&vdev->v_children, kid,
199767f8919635c4928607450d9e0abb932109ceToomas Soome v_childlink);
199767f8919635c4928607450d9e0abb932109ceToomas Soome kids = nvlist_next(kids);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_nchildren = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdevp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *vdevp = vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_set_state(vdev_t *vdev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int good_kids;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bad_kids;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A mirror or raidz is healthy if all its kids are healthy. A
199767f8919635c4928607450d9e0abb932109ceToomas Soome * mirror is degraded if any of its kids is healthy; a raidz
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is degraded if at most nparity kids are offline.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (STAILQ_FIRST(&vdev->v_children)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome good_kids = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bad_kids = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kid->v_state == VDEV_STATE_HEALTHY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome good_kids++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome bad_kids++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bad_kids == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_HEALTHY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_read == vdev_mirror_read) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (good_kids) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_DEGRADED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_OFFLINE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (vdev->v_read == vdev_raidz_read) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bad_kids > vdev->v_nparity) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_OFFLINE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_DEGRADED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic spa_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_find_by_guid(uint64_t guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(spa, &zfs_pools, spa_link)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (spa->spa_guid == guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (spa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic spa_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_find_by_name(const char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(spa, &zfs_pools, spa_link)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(spa->spa_name, name))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (spa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_get_primary(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (STAILQ_FIRST(&zfs_pools));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_get_primary_vdev(const spa_t *spa)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (spa == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa = spa_get_primary();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (spa == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = STAILQ_FIRST(&spa->spa_vdevs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome kid = STAILQ_FIRST(&vdev->v_children))
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (vdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic spa_t *
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_create(uint64_t guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa = malloc(sizeof(spa_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(spa, 0, sizeof(spa_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&spa->spa_vdevs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa->spa_guid = guid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (spa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomestate_name(vdev_state_t state)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static const char* names[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "UNKNOWN",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "CLOSED",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "OFFLINE",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "REMOVED",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "CANT_OPEN",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "FAULTED",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "DEGRADED",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "ONLINE"
199767f8919635c4928607450d9e0abb932109ceToomas Soome };
199767f8919635c4928607450d9e0abb932109ceToomas Soome return names[state];
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepager_printf(const char *fmt, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(args, fmt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vsnprintf(line, sizeof (line), fmt, args);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(args);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pager_output(line));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define STATUS_FORMAT " %s %s\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_state(int indent, const char *name, vdev_state_t state)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[512];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < indent; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(buf, " ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(buf, name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_status(vdev_t *vdev, int indent)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *kid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = print_state(indent, vdev->v_name, vdev->v_state);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = vdev_status(kid, indent + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_status(spa_t *spa)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char bootfs[ZFS_MAXNAMELEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t rootid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int good_kids, bad_kids, degraded_kids, ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_state_t state;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf(" pool: %s\n", spa->spa_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zfs_get_root(spa, &rootid) == 0 &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_rlookup(spa, rootid, bootfs) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bootfs[0] == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf("bootfs: %s\n", spa->spa_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
199767f8919635c4928607450d9e0abb932109ceToomas Soome bootfs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf("config:\n\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome good_kids = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome degraded_kids = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bad_kids = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_state == VDEV_STATE_HEALTHY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome good_kids++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (vdev->v_state == VDEV_STATE_DEGRADED)
199767f8919635c4928607450d9e0abb932109ceToomas Soome degraded_kids++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome bad_kids++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome state = VDEV_STATE_CLOSED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome state = VDEV_STATE_HEALTHY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if ((good_kids + degraded_kids) > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome state = VDEV_STATE_DEGRADED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = print_state(0, spa->spa_name, state);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = vdev_status(vdev, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomespa_all_status(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int first = 1, ret = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!first) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome first = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = spa_status(spa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomevdev_probe(vdev_phys_read_t *phys_read, void *read_priv, spa_t **spap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t vtmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev, *top_vdev, *pool_vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkptr_t bp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *nvlist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t guid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t pool_txg, pool_guid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t is_log;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *pool_name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *vdevs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *features;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, rc, is_newer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *upbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const struct uberblock *up;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Load the vdev label and figure out which
199767f8919635c4928607450d9e0abb932109ceToomas Soome * uberblock is most current.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(&vtmp, 0, sizeof(vtmp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome vtmp.v_phys_read = phys_read;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vtmp.v_read_priv = read_priv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = offsetof(vdev_label_t, vl_vdev_phys);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_ZERO(&bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_VERSION,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &val)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!SPA_VERSION_IS_SUPPORTED(val)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: unsupported ZFS version %u (should be %u)\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned) val, (unsigned) SPA_VERSION);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check ZFS features for read */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_FEATURES_FOR_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_NVLIST, 0, &features) == 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome && nvlist_check_features_for_read(features) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_POOL_STATE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &val)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (val == POOL_STATE_DESTROYED) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We don't boot only from destroyed pools. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_POOL_TXG,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &pool_txg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome || nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_POOL_GUID,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &pool_guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome || nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_POOL_NAME,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_STRING, 0, &pool_name)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Cache and spare devices end up here - just ignore
199767f8919635c4928607450d9e0abb932109ceToomas Soome * them.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*printf("ZFS: can't find pool details\n");*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_log = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &is_log);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_log)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Create the pool if this is the first time we've seen it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa = spa_find_by_guid(pool_guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!spa) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa = spa_create(pool_guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa->spa_name = strdup(pool_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pool_txg > spa->spa_txg) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa->spa_txg = pool_txg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_newer = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_newer = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the vdev tree and create our in-core copy of it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we already have a vdev with this guid, this must
199767f8919635c4928607450d9e0abb932109ceToomas Soome * be some kind of alias (overlapping slices, dangerously dedicated
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disks etc).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_GUID,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_UINT64, 0, &guid)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_find(guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nvlist_find(nvlist,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZPOOL_CONFIG_VDEV_TREE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DATA_TYPE_NVLIST, 0, &vdevs)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Add the toplevel vdev to the pool if its not already there.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (top_vdev == pool_vdev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!pool_vdev && top_vdev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We should already have created an incomplete vdev for this
199767f8919635c4928607450d9e0abb932109ceToomas Soome * vdev. Find it and initialise it with our read proc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev = vdev_find(guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_phys_read = phys_read;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_read_priv = read_priv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev->v_state = VDEV_STATE_HEALTHY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: inconsistent nvlist contents\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Re-evaluate top-level vdev state.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_set_state(top_vdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ok, we are happy with the pool so far. Lets find
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the best uberblock and then we can actually access
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the contents of the pool.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome up = (const struct uberblock *)upbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome i < VDEV_UBERBLOCK_COUNT(vdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = VDEV_UBERBLOCK_OFFSET(vdev, i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_ZERO(&bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DVA_SET_OFFSET(&bp.blk_dva[0], off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (up->ub_magic != UBERBLOCK_MAGIC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (up->ub_txg < spa->spa_txg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (up->ub_txg > spa->spa_uberblock.ub_txg) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa->spa_uberblock = *up;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa->spa_uberblock = *up;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (spap)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *spap = spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeilog2(int n)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (v = 0; v < 32; v++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (n == (1 << v))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkptr_t gbh_bp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zio_gbh_phys_t zio_gb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *pbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Artificial BP for gang block header. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome gbh_bp = *bp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < SPA_DVAS_PER_BP; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read gang header block using the artificial BP. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zio_read(spa, &gbh_bp, &zio_gb))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkptr_t *gbp = &zio_gb.zg_blkptr[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (BP_IS_HOLE(gbp))
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zio_read(spa, gbp, pbuf))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf += BP_GET_PSIZE(gbp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zio_checksum_verify(bp, buf))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cpfunc = BP_GET_COMPRESS(bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t align, size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *pbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Process data embedded in block pointer
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (BP_IS_EMBEDDED(bp)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = BPE_GET_PSIZE(bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ASSERT(size <= BPE_PAYLOAD_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cpfunc != ZIO_COMPRESS_OFF)
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf = zfs_alloc(size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome decode_embedded_bp_compressed(bp, pbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cpfunc != ZIO_COMPRESS_OFF) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = zio_decompress_data(cpfunc, pbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size, buf, BP_GET_LSIZE(bp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_free(pbuf, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < SPA_DVAS_PER_BP; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const dva_t *dva = &bp->blk_dva[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdev_t *vdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int vdevid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!dva->dva_word[0] && !dva->dva_word[1])
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome vdevid = DVA_GET_VDEV(dva);
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = DVA_GET_OFFSET(dva);
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_id == vdevid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!vdev || !vdev->v_read)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = BP_GET_PSIZE(bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vdev->v_read == vdev_raidz_read) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome align = 1ULL << vdev->v_top->v_ashift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (P2PHASE(size, align) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = P2ROUNDUP(size, align);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf = zfs_alloc(size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome pbuf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (DVA_GET_GANG(dva))
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = zio_read_gang(spa, bp, pbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = vdev->v_read(vdev, bp, pbuf, offset, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cpfunc != ZIO_COMPRESS_OFF)
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = zio_decompress_data(cpfunc, pbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (size != BP_GET_PSIZE(bp))
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(pbuf, buf, BP_GET_PSIZE(bp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf != pbuf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_free(pbuf, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: i/o error - all block copies unavailable\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomednode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nlevels = dnode->dn_nlevels;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bsize > SPA_MAXBLOCKSIZE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: I/O error - blocks larger than %llu are not "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "supported\n", SPA_MAXBLOCKSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: bsize may not be a power of two here so we need to do an
199767f8919635c4928607450d9e0abb932109ceToomas Soome * actual divide rather than a bitshift.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (buflen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t bn = offset / bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int boff = offset % bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ibn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const blkptr_t *indbp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkptr_t bp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bn > dnode->dn_maxblkid) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("warning: zfs bug: bn %llx > dn_maxblkid %llx\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)bn,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)dnode->dn_maxblkid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * zfs bug, will not return error
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto cached;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome indbp = dnode->dn_blkptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nlevels; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy the bp from the indirect array so that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we can re-use the scratch buffer for multi-level
199767f8919635c4928607450d9e0abb932109ceToomas Soome * objects.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ibn = bn >> ((nlevels - i - 1) * ibshift);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ibn &= ((1 << ibshift) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bp = indbp[ibn];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (BP_IS_HOLE(&bp)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(dnode_cache_buf, 0, bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = zio_read(spa, &bp, dnode_cache_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome indbp = (const blkptr_t *) dnode_cache_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_cache_obj = dnode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_cache_bn = bn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cached:
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The buffer contains our data block. Copy what we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * need from it and loop.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome i = bsize - boff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i > buflen) i = buflen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(buf, &dnode_cache_buf[boff], i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = ((char*) buf) + i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset += i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome buflen -= i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a value in a microzap directory. Assumes that the zap
199767f8919635c4928607450d9e0abb932109ceToomas Soome * scratch buffer contains the directory contents.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomemzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_phys_t *mz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_ent_phys_t *mze;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int chunks, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Microzap objects use exactly one block. Read the whole
199767f8919635c4928607450d9e0abb932109ceToomas Soome * thing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = dnode->dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome mz = (const mzap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunks = size / MZAP_ENT_LEN - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < chunks; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome mze = &mz->mz_chunk[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(mze->mze_name, name)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *value = mze->mze_value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Compare a name with a zap leaf entry. Return non-zero if the name
199767f8919635c4928607450d9e0abb932109ceToomas Soome * matches.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const zap_leaf_chunk_t *nc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen = zc->l_entry.le_name_numints;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (namelen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > ZAP_LEAF_ARRAY_BYTES)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = ZAP_LEAF_ARRAY_BYTES;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memcmp(p, nc->l_array.la_array, len))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen -= len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Extract a uint64_t value from a zap leaf entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint64_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const zap_leaf_chunk_t *vc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const uint8_t *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = (value << 8) | p[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a value in a fatzap directory. Assumes that the zap scratch
199767f8919635c4928607450d9e0abb932109ceToomas Soome * buffer contains the directory header.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_phys_t zh = *(zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fat_zap_t z;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t *ptrtbl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t hash;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zh.zap_magic != ZAP_MAGIC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_block_shift = ilog2(bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_phys = (zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Figure out where the pointer table is and read it in if necessary.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zh.zap_ptrtbl.zt_blk) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_scratch, bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptrtbl = (uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome hash = zap_hash(zh.zap_salt, name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_t zl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_bs = z.zap_block_shift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_chunk_t *zc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Make sure this chunk matches our hash.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zl.l_phys->l_hdr.lh_prefix_len > 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome && zl.l_phys->l_hdr.lh_prefix
199767f8919635c4928607450d9e0abb932109ceToomas Soome != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Hash within the chunk to find our entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome h = zl.l_phys->l_hash[h];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (h == 0xffff)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc = &ZAP_LEAF_CHUNK(&zl, h);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (zc->l_entry.le_hash != hash) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zc->l_entry.le_next == 0xffff) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fzap_name_equal(&zl, zc, name)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (E2BIG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *value = fzap_leaf_value(&zl, zc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a name in a zap object and return its value as a uint64_t.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t zap_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = dnode_read(spa, dnode, 0, zap_scratch, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_type = *(uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_type == ZBT_MICRO)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return mzap_lookup(dnode, name, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (zap_type == ZBT_HEADER)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return fzap_lookup(spa, dnode, name, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List a microzap directory. Assumes that the zap scratch buffer contains
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the directory contents.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomemzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_phys_t *mz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_ent_phys_t *mze;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int chunks, i, rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Microzap objects use exactly one block. Read the whole
199767f8919635c4928607450d9e0abb932109ceToomas Soome * thing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = dnode->dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome mz = (const mzap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunks = size / MZAP_ENT_LEN - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < chunks; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome mze = &mz->mz_chunk[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mze->mze_name[0]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = callback(mze->mze_name, mze->mze_value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List a fatzap directory. Assumes that the zap scratch buffer contains
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the directory header.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_phys_t zh = *(zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fat_zap_t z;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, j, rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zh.zap_magic != ZAP_MAGIC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_block_shift = ilog2(bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_phys = (zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This assumes that the leaf blocks start at block 1. The
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation isn't exactly clear on this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_t zl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_bs = z.zap_block_shift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < zh.zap_num_leafs; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off = (i + 1) << zl.l_bs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char name[256], *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnode_read(spa, dnode, off, zap_scratch, bsize))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_chunk_t *zc, *nc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc = &ZAP_LEAF_CHUNK(&zl, j);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen = zc->l_entry.le_name_numints;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (namelen > sizeof(name))
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen = sizeof(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Paste the name back together.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (namelen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > ZAP_LEAF_ARRAY_BYTES)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = ZAP_LEAF_ARRAY_BYTES;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(p, nc->l_array.la_array, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen -= len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Assume the first eight bytes of the value are
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a uint64_t.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = fzap_leaf_value(&zl, zc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome //printf("%s 0x%jx\n", name, (uintmax_t)value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = callback((const char *)name, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zfs_printf(const char *name, uint64_t value __unused)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s\n", name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List a zap directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezap_list(const spa_t *spa, const dnode_phys_t *dnode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t zap_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size = dnode->dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnode_read(spa, dnode, 0, zap_scratch, size))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_type = *(uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_type == ZBT_MICRO)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return mzap_list(dnode, zfs_printf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return fzap_list(spa, dnode, zfs_printf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeobjset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = objnum * sizeof(dnode_phys_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return dnode_read(spa, &os->os_meta_dnode, offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode, sizeof(dnode_phys_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomemzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_phys_t *mz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const mzap_ent_phys_t *mze;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int chunks, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Microzap objects use exactly one block. Read the whole
199767f8919635c4928607450d9e0abb932109ceToomas Soome * thing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = dnode->dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome mz = (const mzap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunks = size / MZAP_ENT_LEN - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < chunks; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome mze = &mz->mz_chunk[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (value == mze->mze_value) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(name, mze->mze_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const zap_leaf_chunk_t *nc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen = zc->l_entry.le_name_numints;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (namelen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > ZAP_LEAF_ARRAY_BYTES)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = ZAP_LEAF_ARRAY_BYTES;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(p, nc->l_array.la_array, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen -= len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_phys_t zh = *(zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fat_zap_t z;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zh.zap_magic != ZAP_MAGIC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_block_shift = ilog2(bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome z.zap_phys = (zap_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This assumes that the leaf blocks start at block 1. The
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation isn't exactly clear on this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_t zl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_bs = z.zap_block_shift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < zh.zap_num_leafs; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off = (i + 1) << zl.l_bs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnode_read(spa, dnode, off, zap_scratch, bsize))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_leaf_chunk_t *zc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc = &ZAP_LEAF_CHUNK(&zl, j);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zc->l_entry.le_value_intlen != 8 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome zc->l_entry.le_value_numints != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fzap_leaf_value(&zl, zc) == value) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fzap_name_copy(&zl, zc, name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t zap_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size = dnode->dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = dnode_read(spa, dnode, 0, zap_scratch, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_type = *(uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_type == ZBT_MICRO)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return mzap_rlookup(spa, dnode, name, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return fzap_rlookup(spa, dnode, name, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char name[256];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char component[256];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t dir_obj, parent_obj, child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t child_dir_zap, dataset, dir, parent;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dir_phys_t *dd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dataset_phys_t *ds;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = &name[sizeof(name) - 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_obj = ds->ds_dir_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dd = (dsl_dir_phys_t *)&dir.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Actual loop condition. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome parent_obj = dd->dd_parent_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (parent_obj == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dd = (dsl_dir_phys_t *)&parent.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome child_dir_zapobj = dd->dd_child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(component);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p -= len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(p, component, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome --p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '/';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Actual loop iteration. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_obj = parent_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(result, p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char element[256];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t dir_obj, child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t child_dir_zap, dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dir_phys_t *dd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dd = (dsl_dir_phys_t *)&dir.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Actual loop condition #1. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome q = strchr(p, '/');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (q) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(element, p, q - p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome element[q - p] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = q + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(element, p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += strlen(p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome child_dir_zapobj = dd->dd_child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Actual loop condition #2. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *objnum = dd->dd_head_dataset_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#pragma GCC diagnostic ignored "-Wstrict-aliasing"
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t dir_obj, child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t child_dir_zap, dir, dataset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dataset_phys_t *ds;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dir_phys_t *dd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_obj = ds->ds_dir_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome dd = (dsl_dir_phys_t *)&dir.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome child_dir_zapobj = dd->dd_child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (zap_list(spa, &child_dir_zap) != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t dir_obj, child_dir_zapobj, zap_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t child_dir_zap, dir, dataset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dataset_phys_t *ds;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dir_phys_t *dd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_obj = ds->ds_dir_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome dd = (dsl_dir_phys_t *)&dir.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome child_dir_zapobj = dd->dd_child_dir_zapobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_type = *(uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_type == ZBT_MICRO)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return mzap_list(&child_dir_zap, callback);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return fzap_list(spa, &child_dir_zap, callback);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Find the object set given the object number of its dataset object
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and return its details in *objset
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t dataset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dataset_phys_t *ds;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zio_read(spa, &ds->ds_bp, objset)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't read object set for dataset %ju\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Find the object set pointed to by the BOOTFS property or the root
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dataset if there is none and return its details in *objset
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_get_root(const spa_t *spa, uint64_t *objid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t dir, propdir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t props, bootfs, root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *objid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Start with the MOS directory object.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't read MOS object directory\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup the pool_props and see if we can find a bootfs.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome && bootfs != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *objid = bootfs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup the root dataset directory
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
199767f8919635c4928607450d9e0abb932109ceToomas Soome || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find root dsl_dir\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use the information from the dataset directory's bonus buffer
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to find the dataset object and from that the object set itself.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *objid = dd->dd_head_dataset_obj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mnt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome mnt->spa = spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Find the root object set if not explicitly provided
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't find root filesystem\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zfs_mount_dataset(spa, rootobj, &mnt->objset)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't open root filesystem\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome mnt->rootobj = rootobj;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * callback function for feature name checks.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecheck_feature(const char *name, uint64_t value)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (value == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (name[0] == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; features_for_read[i] != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(name, features_for_read[i]) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: unsupported feature: %s\n", name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Checks whether the MOS features that are active are supported.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecheck_mos_features(const spa_t *spa)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t objnum, zap_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &dir)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome &objnum)) != 0) {
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome /*
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome * It is older pool without features. As we have already
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome * tested the label, just return without raising the error.
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome */
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome if (rc == ENOENT)
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome rc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
5e069aaf824cc0abc9835809924db4929507ee32Toomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir.dn_type != DMU_OTN_ZAP_METADATA)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = dir.dn_datablkszsec * 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnode_read(spa, &dir, 0, zap_scratch, size))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zap_type = *(uint64_t *) zap_scratch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zap_type == ZBT_MICRO)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = mzap_list(&dir, check_feature);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = fzap_list(spa, &dir, check_feature);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_spa_init(spa_t *spa)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (spa->spa_mos.os_type != DMU_OST_META) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = check_mos_features(spa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: pool %s is not supported\n", spa->spa_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dn->dn_bonustype != DMU_OT_SA) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = zp->zp_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = zp->zp_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = zp->zp_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = zp->zp_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sa_hdr_phys_t *sahdrp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int hdrsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dn->dn_bonuslen != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkptr_t *bp = &dn->dn_spill;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = BP_GET_LSIZE(bp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = zfs_alloc(size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = zio_read(spa, bp, buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_free(buf, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome sahdrp = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome hdrsize = SA_HDR_SIZE(sahdrp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
199767f8919635c4928607450d9e0abb932109ceToomas Soome SA_MODE_OFFSET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
199767f8919635c4928607450d9e0abb932109ceToomas Soome SA_UID_OFFSET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
199767f8919635c4928607450d9e0abb932109ceToomas Soome SA_GID_OFFSET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
199767f8919635c4928607450d9e0abb932109ceToomas Soome SA_SIZE_OFFSET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_free(buf, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a file and return its dnode.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezfs_lookup(const struct zfsmount *mnt, const char *upath, dnode_phys_t *dnode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t objnum, rootnum, parentnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const spa_t *spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dnode_phys_t dn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char element[256];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char path[1024];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int symlinks_followed = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat sb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome spa = mnt->spa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mnt->objset.os_type != DMU_OST_ZFS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ZFS: unexpected object set type %ju\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)mnt->objset.os_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the root directory dnode.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = objset_get_dnode(spa, &mnt->objset, MASTER_NODE_OBJ, &dn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = objset_get_dnode(spa, &mnt->objset, rootnum, &dn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome objnum = rootnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = upath;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p && *p) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!*p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome q = strchr(p, '/');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (q) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(element, p, q - p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome element[q - p] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(element, p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = zfs_dnode_stat(spa, &dn, &sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!S_ISDIR(sb.st_mode))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOTDIR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome parentnum = objnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = zap_lookup(spa, &dn, element, &objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome objnum = ZFS_DIRENT_OBJ(objnum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = objset_get_dnode(spa, &mnt->objset, objnum, &dn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check for symlink.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = zfs_dnode_stat(spa, &dn, &sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (S_ISLNK(sb.st_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (symlinks_followed > 10)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EMLINK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome symlinks_followed++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read the link value and copy the tail of our
199767f8919635c4928607450d9e0abb932109ceToomas Soome * current path onto the end.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(&path[sb.st_size], p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome path[sb.st_size] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Second test is purely to silence bogus compiler
199767f8919635c4928607450d9e0abb932109ceToomas Soome * warning about accessing past the end of dn_bonus.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sb.st_size + sizeof(znode_phys_t) <=
199767f8919635c4928607450d9e0abb932109ceToomas Soome dn.dn_bonuslen && sizeof(znode_phys_t) <=
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(dn.dn_bonus)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb.st_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = dnode_read(spa, &dn, 0, path, sb.st_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Restart with the new path, starting either at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the root or at the parent depending whether or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * not the link is relative.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome objnum = rootnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome objnum = parentnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome objset_get_dnode(spa, &mnt->objset, objnum, &dn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *dnode = dn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}