Example #1
0
/**
 * Open the given zpool
 * @param p_libzfshd: the libzfs handle
 * @param psz_zpool: the zpool name
 * @param ppsz_error: the error message if any
 * @return the zpool handle or NULL in case of error
 */
zpool_handle_t *libzfs_zpool_open_canfail(libzfs_handle_t *p_libzfshd, const char* psz_zpool, const char **ppsz_error)
{
        zpool_handle_t *p_zpool;
        nvlist_t *pnv_config;
        int i_error;

        /* Check the zpool name */
        if(libzfs_zpool_name_valid(psz_zpool, ppsz_error))
                return NULL;

        if((p_zpool = calloc(1, sizeof(zpool_handle_t))) == NULL)
        {
                *ppsz_error = "no memory";
                return NULL;
        }
        p_zpool->zpool_hdl = p_libzfshd;
        strlcpy(p_zpool->zpool_name, psz_zpool, sizeof(p_zpool->zpool_name));

        i_error = spa_get_stats(psz_zpool, &pnv_config, NULL, 0);
        if(!pnv_config)
        {
                free(p_zpool);
                *ppsz_error = "unable to get the statistics of the zpool";
                return NULL;
        }

        assert(nvlist_size(pnv_config, &p_zpool->zpool_config_size, NV_ENCODE_NATIVE) == 0);

        if(p_zpool->zpool_config)
        {
                uint64_t oldtxg, newtxg;

                assert(nvlist_lookup_uint64(p_zpool->zpool_config, ZPOOL_CONFIG_POOL_TXG, &oldtxg) == 0);
                assert(nvlist_lookup_uint64(pnv_config, ZPOOL_CONFIG_POOL_TXG, &newtxg) == 0);

                if(p_zpool->zpool_old_config)
                        nvlist_free(p_zpool->zpool_old_config);

                if(oldtxg != newtxg)
                {
                        nvlist_free(p_zpool->zpool_config);
                        p_zpool->zpool_old_config = NULL;
                }
                else
                        p_zpool->zpool_old_config = p_zpool->zpool_config;
        }

        p_zpool->zpool_config = pnv_config;
        if(i_error)
                p_zpool->zpool_state = POOL_STATE_UNAVAIL;
        else
                p_zpool->zpool_state = POOL_STATE_ACTIVE;

        return p_zpool;
}
Example #2
0
void
show_pool_stats(spa_t *spa)
{
	nvlist_t *config, *nvroot;
	char *name;

	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);

	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
	    &nvroot) == 0);
	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
	    &name) == 0);

	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);

	nvlist_free(config);
}