ddt.c revision 837b568b3a2559f8c9b9403f95104271a85d129e
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick/*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * CDDL HEADER START
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * The contents of this file are subject to the terms of the
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Common Development and Distribution License (the "License").
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * You may not use this file except in compliance with the License.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * or http://www.opensolaris.org/os/licensing.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * See the License for the specific language governing permissions
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * and limitations under the License.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * When distributing Covered Code, include this CDDL HEADER in each
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If applicable, add the following below this CDDL HEADER, with the
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * fields enclosed by brackets "[]" replaced with your own identifying
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * information: Portions Copyright [yyyy] [name of copyright owner]
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * CDDL HEADER END
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick/*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/zfs_context.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/spa.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/spa_impl.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/zio.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/ddt.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/zap.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/dmu_tx.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/arc.h>
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick#include <sys/dsl_pool.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/zio_checksum.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/zio_compress.h>
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#include <sys/dsl_scan.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson/*
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson */
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilsonint zfs_dedup_prefetch = 1;
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic const ddt_ops_t *ddt_ops[DDT_TYPES] = {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &ddt_zap_ops,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick};
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic const char *ddt_class_name[DDT_CLASSES] = {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick "ditto",
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick "duplicate",
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick "unique",
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick};
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_tx_t *tx)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick objset_t *os = ddt->ddt_os;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t *objectp = &ddt->ddt_object[type][class];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_dedup;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char name[DDT_NAMELEN];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_name(ddt, type, class, name);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(*objectp == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(*objectp != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (uint64_t), 1, objectp, tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_add(os, spa->spa_ddt_stat_object, name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &ddt->ddt_histogram[type][class], tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_tx_t *tx)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick objset_t *os = ddt->ddt_os;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t *objectp = &ddt->ddt_object[type][class];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char name[DDT_NAMELEN];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_name(ddt, type, class, name);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(*objectp != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_count(ddt, type, class) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *objectp = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson dmu_object_info_t doi;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char name[DDT_NAMELEN];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int error;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_name(ddt, type, class, name);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (error);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &ddt->ddt_histogram[type][class]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson /*
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson * Seed the cached statistics.
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson */
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_count = ddt_object_count(ddt, type, class);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(error == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (error);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_tx_t *tx)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson dmu_object_info_t doi;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char name[DDT_NAMELEN];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_name(ddt, type, class, name);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &ddt->ddt_histogram[type][class], tx) == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson /*
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson * Cache DDT statistics; this is the only time they'll change.
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson */
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_count = ddt_object_count(ddt, type, class);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (!ddt_object_exists(ddt, type, class))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ENOENT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_object[type][class], dde));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
c7cd242109c82107ec2e50013369e92be9d77702George Wilsonstatic void
c7cd242109c82107ec2e50013369e92be9d77702George Wilsonddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_entry_t *dde)
c7cd242109c82107ec2e50013369e92be9d77702George Wilson{
c7cd242109c82107ec2e50013369e92be9d77702George Wilson if (!ddt_object_exists(ddt, type, class))
c7cd242109c82107ec2e50013369e92be9d77702George Wilson return;
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt->ddt_object[type][class], dde);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson}
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Lingint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde, dmu_tx_t *tx)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_exists(ddt, type, class));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_object[type][class], dde, tx));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde, dmu_tx_t *tx)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_exists(ddt, type, class));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_object[type][class], dde, tx));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick uint64_t *walk, ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_exists(ddt, type, class));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_object[type][class], dde, walk));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_exists(ddt, type, class));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_object[type][class]));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_object_info_t *doi)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (!ddt_object_exists(ddt, type, class))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ENOENT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick doi));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickboolean_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (!!ddt->ddt_object[type][class]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char *name)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick (void) sprintf(name, DMU_POOL_DDT,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_checksum_table[ddt->ddt_checksum].ci_name,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(txg != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bp->blk_dva[d] = ddp->ddp_dva[d];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwickddt_bp_create(enum zio_checksum checksum,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_ZERO(bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bp->blk_cksum = ddk->ddk_cksum;
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling bp->blk_fill = 1;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick BP_SET_CHECKSUM(bp, checksum);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling BP_SET_TYPE(bp, DMU_OT_DEDUP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_LEVEL(bp, 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_DEDUP(bp, 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddk->ddk_cksum = bp->blk_cksum;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddk->ddk_prop = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddp->ddp_phys_birth == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp->ddp_dva[d] = bp->blk_dva[d];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_clear(ddt_phys_t *ddp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bzero(ddp, sizeof (*ddp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_addref(ddt_phys_t *ddp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp->ddp_refcnt++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_decref(ddt_phys_t *ddp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT((int64_t)ddp->ddp_refcnt > 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp->ddp_refcnt--;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t blk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_clear(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_free(ddt->ddt_spa, txg, &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_phys_total_refcnt(const ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t refcnt = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick refcnt += dde->dde_phys[p].ddp_refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (refcnt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = dde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t *ddk = &dde->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t lsize = DDK_GET_LSIZE(ddk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t psize = DDK_GET_PSIZE(ddk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bzero(dds, sizeof (*dds));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t dsize = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t refcnt = ddp->ddp_refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_blocks += 1;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_lsize += lsize;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_psize += psize;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_dsize += dsize;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_ref_blocks += refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_ref_lsize += lsize * refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_ref_psize += psize * refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dds->dds_ref_dsize += dsize * refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const uint64_t *s = (const uint64_t *)src;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t *d = (uint64_t *)dst;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t *d_end = (uint64_t *)(dst + 1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick while (d < d_end)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *d++ += (*s++ ^ neg) - neg;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_t dds;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_histogram_t *ddh;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int bucket;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_generate(ddt, dde, &dds);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bucket = highbit(dds.dds_ref_blocks) - 1;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(bucket >= 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int h = 0; h < 64; h++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bzero(dds, sizeof (*dds));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int h = 0; h < 64; h++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickboolean_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_histogram_empty(const ddt_histogram_t *ddh)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const uint64_t *s = (const uint64_t *)ddh;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const uint64_t *s_end = (const uint64_t *)(ddh + 1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick while (s < s_end)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (*s++ != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (B_FALSE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (B_TRUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
9eb19f4d61679ca0382def038665019234458eddGeorge Wilsonvoid
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilsonddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson /* Sum the statistics we cached in ddt_object_sync(). */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = spa->spa_ddt[c];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_class class = 0; class < DDT_CLASSES;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick class++) {
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddt_object_t *ddo =
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson &ddt->ddt_object_stats[type][class];
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo_total->ddo_count += ddo->ddo_count;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo_total->ddo_dspace += ddo->ddo_dspace;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo_total->ddo_mspace += ddo->ddo_mspace;
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson }
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson }
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson }
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson /* ... and compute the averages. */
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson if (ddo_total->ddo_count != 0) {
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo_total->ddo_dspace /= ddo_total->ddo_count;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddo_total->ddo_mspace /= ddo_total->ddo_count;
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson } else {
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ASSERT(ddo_total->ddo_dspace == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ASSERT(ddo_total->ddo_mspace == 0);
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson }
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson}
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson
9eb19f4d61679ca0382def038665019234458eddGeorge Wilsonvoid
9eb19f4d61679ca0382def038665019234458eddGeorge Wilsonddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson{
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddt_t *ddt = spa->spa_ddt[c];
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson for (enum ddt_class class = 0; class < DDT_CLASSES;
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson class++) {
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddt_histogram_add(ddh,
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson &ddt->ddt_histogram_cache[type][class]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson}
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson
9eb19f4d61679ca0382def038665019234458eddGeorge Wilsonvoid
9eb19f4d61679ca0382def038665019234458eddGeorge Wilsonddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson{
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddt_histogram_t *ddh_total;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddt_get_dedup_histogram(spa, ddh_total);
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson ddt_histogram_stat(dds_total, ddh_total);
9eb19f4d61679ca0382def038665019234458eddGeorge Wilson kmem_free(ddh_total, sizeof (ddt_histogram_t));
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson}
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilsonuint64_t
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilsonddt_get_dedup_dspace(spa_t *spa)
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson{
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson ddt_stat_t dds_total = { 0 };
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson ddt_get_dedup_stats(spa, &dds_total);
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson}
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilsonuint64_t
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilsonddt_get_pool_dedup_ratio(spa_t *spa)
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson{
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson ddt_stat_t dds_total = { 0 };
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson ddt_get_dedup_stats(spa, &dds_total);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dds_total.dds_dsize == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (100);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t total_refcnt = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t ditto = spa->spa_dedup_ditto;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int total_copies = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int desired_copies = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *zio = dde->dde_lead_zio[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick refcnt += zio->io_parent_count; /* pending refs */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp == ddp_willref)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick refcnt++; /* caller's ref */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (refcnt != 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick total_refcnt += refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick total_copies += p;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ditto == 0 || ditto > UINT32_MAX)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ditto = UINT32_MAX;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (total_refcnt >= 1)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick desired_copies++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (total_refcnt >= ditto)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick desired_copies++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (total_refcnt >= ditto * ditto)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick desired_copies++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (MAX(desired_copies, total_copies) - total_copies);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_ditto_copies_present(ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dva_t *dva = ddp->ddp_dva;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int copies = 0 - DVA_GET_GANG(dva);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (DVA_IS_VALID(dva))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick copies++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (copies);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwicksize_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uchar_t *version = dst++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int cpfunc = ZIO_COMPRESS_ZLE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_compress_info_t *ci = &zio_compress_table[cpfunc];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick size_t c_len;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (c_len == s_len) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cpfunc = ZIO_COMPRESS_OFF;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcopy(src, dst, s_len);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *version = (ZFS_HOST_BYTEORDER & DDT_COMPRESS_BYTEORDER_MASK) | cpfunc;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (c_len + 1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uchar_t version = *src++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_compress_info_t *ci = &zio_compress_table[cpfunc];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ci->ci_decompress != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcopy(src, dst, d_len);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if ((version ^ ZFS_HOST_BYTEORDER) & DDT_COMPRESS_BYTEORDER_MASK)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick byteswap_uint64_array(dst, d_len);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (spa->spa_ddt[c]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_select(spa_t *spa, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_enter(ddt_t *ddt)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_enter(&ddt->ddt_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_exit(ddt_t *ddt)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_exit(&ddt->ddt_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic ddt_entry_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_alloc(const ddt_key_t *ddk)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_key = *ddk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_free(ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!dde->dde_loading);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_lead_zio[p] == NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_repair_data != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_free(dde->dde_repair_data,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DDK_GET_PSIZE(&dde->dde_key));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cv_destroy(&dde->dde_cv);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick kmem_free(dde, sizeof (*dde));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_remove(ddt_t *ddt, ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(MUTEX_HELD(&ddt->ddt_lock));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_remove(&ddt->ddt_tree, dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_free(dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_entry_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde, dde_search;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_type type;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_class class;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_index_t where;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int error;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(MUTEX_HELD(&ddt->ddt_lock));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_fill(&dde_search.dde_key, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde == NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (!add)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = ddt_alloc(&dde_search.dde_key);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_insert(&ddt->ddt_tree, dde, where);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick while (dde->dde_loading)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cv_wait(&dde->dde_cv, &ddt->ddt_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_loaded)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_loading = B_TRUE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = ENOENT;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (type = 0; type < DDT_TYPES; type++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (class = 0; class < DDT_CLASSES; class++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = ddt_object_lookup(ddt, type, class, dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error != ENOENT)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick break;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error != ENOENT)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick break;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(error == 0 || error == ENOENT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_loaded == B_FALSE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_loading == B_TRUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_type = type; /* will be DDT_TYPES if no entry found */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_loaded = B_TRUE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_loading = B_FALSE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_update(ddt, dde, -1ULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cv_broadcast(&dde->dde_cv);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
c7cd242109c82107ec2e50013369e92be9d77702George Wilsonvoid
c7cd242109c82107ec2e50013369e92be9d77702George Wilsonddt_prefetch(spa_t *spa, const blkptr_t *bp)
c7cd242109c82107ec2e50013369e92be9d77702George Wilson{
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_t *ddt;
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_entry_t dde;
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
c7cd242109c82107ec2e50013369e92be9d77702George Wilson return;
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
c7cd242109c82107ec2e50013369e92be9d77702George Wilson /*
c7cd242109c82107ec2e50013369e92be9d77702George Wilson * We remove the DDT once it's empty and only prefetch dedup blocks
c7cd242109c82107ec2e50013369e92be9d77702George Wilson * when there are entries in the DDT. Thus no locking is required
c7cd242109c82107ec2e50013369e92be9d77702George Wilson * as the DDT can't disappear on us.
c7cd242109c82107ec2e50013369e92be9d77702George Wilson */
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt = ddt_select(spa, bp);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_key_fill(&dde.dde_key, bp);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
c7cd242109c82107ec2e50013369e92be9d77702George Wilson for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
c7cd242109c82107ec2e50013369e92be9d77702George Wilson for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
c7cd242109c82107ec2e50013369e92be9d77702George Wilson ddt_object_prefetch(ddt, type, class, &dde);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson }
c7cd242109c82107ec2e50013369e92be9d77702George Wilson }
c7cd242109c82107ec2e50013369e92be9d77702George Wilson}
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_entry_compare(const void *x1, const void *x2)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const ddt_entry_t *dde1 = x1;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const ddt_entry_t *dde2 = x2;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int i = 0; i < DDT_KEY_WORDS; i++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (u1[i] < u2[i])
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (-1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (u1[i] > u2[i])
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic ddt_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_table_alloc(spa_t *spa, enum zio_checksum c)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_create(&ddt->ddt_tree, ddt_entry_compare,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_checksum = c;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_spa = spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt->ddt_os = spa->spa_meta_objset;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_table_free(ddt_t *ddt)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_destroy(&ddt->ddt_tree);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_destroy(&ddt->ddt_repair_tree);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_destroy(&ddt->ddt_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick kmem_free(ddt, sizeof (*ddt));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_create(spa_t *spa)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa->spa_ddt[c] = ddt_table_alloc(spa, c);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_load(spa_t *spa)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int error;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_create(spa);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &spa->spa_ddt_stat_object);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (error == ENOENT ? 0 : error);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson ddt_t *ddt = spa->spa_ddt[c];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_class class = 0; class < DDT_CLASSES;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick class++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = ddt_object_load(ddt, type, class);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error != 0 && error != ENOENT)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (error);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson /*
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson * Seed the cached histograms.
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson */
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson sizeof (ddt->ddt_histogram));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_unload(spa_t *spa)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (spa->spa_ddt[c]) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_table_free(spa->spa_ddt[c]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa->spa_ddt[c] = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwickboolean_t
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwickddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick{
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_t *ddt;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_entry_t dde;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (!BP_GET_DEDUP(bp))
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (B_FALSE);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (max_class == DDT_CLASS_UNIQUE)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (B_TRUE);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_key_fill(&dde.dde_key, bp);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick for (enum ddt_class class = 0; class <= max_class; class++)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (ddt_object_lookup(ddt, type, class, &dde) == 0)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (B_TRUE);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (B_FALSE);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick}
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_entry_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t ddk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_fill(&ddk, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = ddt_alloc(&ddk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * We can only do repair if there are multiple copies
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * of the block. For anything in the UNIQUE class,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * there's definitely only one copy, so don't even try.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (class != DDT_CLASS_UNIQUE &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_lookup(ddt, type, class, dde) == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bzero(dde->dde_phys, sizeof (dde->dde_phys));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_index_t where;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_insert(&ddt->ddt_repair_tree, dde, where);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_free(dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_repair_entry_done(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *rdde = zio->io_private;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_free(rdde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = dde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *rddp = rdde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t *ddk = &dde->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t *rddk = &rdde->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *zio;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t blk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio = zio_null(rio, rio->io_spa, NULL,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_repair_entry_done, rdde, rio->io_flags);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth == 0 ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_repair_table(ddt_t *ddt, zio_t *rio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde, *rdde_next, *rdde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_tree_t *t = &ddt->ddt_repair_tree;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t blk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (spa_sync_pass(spa) > 1)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick rdde_next = AVL_NEXT(t, rdde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick avl_remove(&ddt->ddt_repair_tree, rdde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = ddt_repair_start(ddt, &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_repair_entry(ddt, dde, rdde, rio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_repair_done(ddt, dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = dde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t *ddk = &dde->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_type otype = dde->dde_type;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_type ntype = DDT_TYPE_CURRENT;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_class oclass = dde->dde_class;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum ddt_class nclass;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t total_refcnt = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_loaded);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!dde->dde_loading);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_lead_zio[p] == NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT((int64_t)ddp->ddp_refcnt >= 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth == 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddp->ddp_refcnt == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (p == DDT_PHYS_DITTO) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_free(ddt, ddk, ddp, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_refcnt == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_free(ddt, ddk, ddp, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick total_refcnt += ddp->ddp_refcnt;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick nclass = DDT_CLASS_DITTO;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else if (total_refcnt > 1)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick nclass = DDT_CLASS_DUPLICATE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick nclass = DDT_CLASS_UNIQUE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (otype != DDT_TYPES &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick (otype != ntype || oclass != nclass || total_refcnt == 0)) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (total_refcnt != 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_type = ntype;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_class = nclass;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_stat_update(ddt, dde, 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (!ddt_object_exists(ddt, ntype, nclass))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_create(ddt, ntype, nclass, tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling /*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * If the class changes, the order that we scan this bp
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * changes. If it decreases, we could miss it, so
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * scan it right now. (This covers both class changing
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * while we are doing ddt_walk(), and when we are
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * traversing.)
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling */
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling if (nclass < oclass) {
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dsl_scan_ddt_entry(dp->dp_scan,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling ddt->ddt_checksum, dde, tx);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = ddt->ddt_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick void *cookie = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (avl_numnodes(&ddt->ddt_tree) == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (spa->spa_ddt_stat_object == 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa->spa_ddt_stat_object = zap_create(ddt->ddt_os,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DMU_OT_DDT_STATS, DMU_OT_NONE, 0, tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick VERIFY(zap_add(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &spa->spa_ddt_stat_object, tx) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_sync_entry(ddt, dde, tx, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_free(dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (!ddt_object_exists(ddt, type, class))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_sync(ddt, type, class, tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddt_object_count(ddt, type, class) == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_object_destroy(ddt, type, class, tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
30f608a433ef7b19cb4981bba0a0f7ccd7c92d03George Wilson sizeof (ddt->ddt_histogram));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickddt_sync(spa_t *spa, uint64_t txg)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_tx_t *tx;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *rio = zio_root(spa, NULL, NULL,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(spa_syncing_txg(spa) == txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = spa->spa_ddt[c];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddt == NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_sync_table(ddt, tx, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_repair_table(ddt, rio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick (void) zio_wait(rio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dmu_tx_commit(tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwickint
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwickddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick{
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick do {
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick do {
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick do {
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick int error = ENOENT;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (ddt_object_exists(ddt, ddb->ddb_type,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddb->ddb_class)) {
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick error = ddt_object_walk(ddt,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddb->ddb_type, ddb->ddb_class,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick &ddb->ddb_cursor, dde);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick }
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dde->dde_type = ddb->ddb_type;
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dde->dde_class = ddb->ddb_class;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (error == 0)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (0);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (error != ENOENT)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (error);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddb->ddb_cursor = 0;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddb->ddb_checksum = 0;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick } while (++ddb->ddb_type < DDT_TYPES);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddb->ddb_type = 0;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick } while (++ddb->ddb_class < DDT_CLASSES);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick return (ENOENT);
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick}